tcmodules.pas 954 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812358133581435815358163581735818358193582035821358223582335824358253582635827358283582935830358313583235833358343583535836358373583835839358403584135842358433584435845358463584735848358493585035851358523585335854358553585635857358583585935860358613586235863358643586535866358673586835869358703587135872358733587435875358763587735878358793588035881358823588335884358853588635887358883588935890358913589235893358943589535896358973589835899359003590135902359033590435905359063590735908359093591035911359123591335914359153591635917359183591935920359213592235923359243592535926359273592835929359303593135932359333593435935359363593735938359393594035941359423594335944359453594635947359483594935950359513595235953359543595535956359573595835959359603596135962359633596435965359663596735968359693597035971359723597335974359753597635977359783597935980359813598235983359843598535986359873598835989359903599135992359933599435995359963599735998359993600036001360023600336004360053600636007360083600936010360113601236013360143601536016360173601836019360203602136022360233602436025360263602736028360293603036031360323603336034360353603636037360383603936040360413604236043360443604536046360473604836049360503605136052360533605436055360563605736058360593606036061360623606336064360653606636067360683606936070360713607236073360743607536076360773607836079360803608136082360833608436085360863608736088360893609036091360923609336094360953609636097360983609936100361013610236103361043610536106361073610836109361103611136112361133611436115361163611736118361193612036121361223612336124361253612636127361283612936130361313613236133361343613536136361373613836139361403614136142361433614436145361463614736148361493615036151361523615336154361553615636157361583615936160361613616236163361643616536166361673616836169361703617136172361733617436175361763617736178361793618036181361823618336184361853618636187361883618936190361913619236193
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2018 by Michael Van Canneyt
  4. Unit tests for Pascal-to-Javascript converter class.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************
  11. Examples:
  12. ./testpas2js --suite=TTestModule.TestEmptyProgram
  13. ./testpas2js --suite=TTestModule.TestEmptyUnit
  14. }
  15. unit TCModules;
  16. {$mode objfpc}{$H+}
  17. interface
  18. uses
  19. Classes, SysUtils, fpcunit, testregistry, contnrs,
  20. jstree, jswriter, jsbase,
  21. PasTree, PScanner, PasResolver, PParser, PasResolveEval, FPPas2Js;
  22. const
  23. // default parser+scanner options
  24. po_tcmodules = po_Pas2js+[po_KeepScannerError];
  25. co_tcmodules = [];
  26. JSONNewLine = {$IFDEF Windows}'\r\n'{$ELSE}'\n'{$ENDIF};
  27. type
  28. TSrcMarkerKind = (
  29. mkLabel,
  30. mkResolverReference,
  31. mkDirectReference
  32. );
  33. const
  34. SrcMarker: array[TSrcMarkerKind] of char = (
  35. '#', // mkLabel
  36. '@', // mkResolverReference
  37. '=' // mkDirectReference
  38. );
  39. type
  40. PSrcMarker = ^TSrcMarker;
  41. TSrcMarker = record
  42. Kind: TSrcMarkerKind;
  43. Filename: string;
  44. Row: integer;
  45. StartCol, EndCol: integer; // token start, end column
  46. Identifier: string;
  47. Next: PSrcMarker;
  48. end;
  49. TSystemUnitPart = (
  50. supTObject,
  51. supTVarRec,
  52. supTypeInfo,
  53. supTInterfacedObject,
  54. supWriteln
  55. );
  56. TSystemUnitParts = set of TSystemUnitPart;
  57. { TTestHintMessage }
  58. TTestHintMessage = class
  59. public
  60. Id: int64;
  61. MsgType: TMessageType;
  62. MsgNumber: integer;
  63. Msg: string;
  64. SourcePos: TPasSourcePos;
  65. end;
  66. TTestResolverReferenceData = record
  67. Filename: string;
  68. Row: integer;
  69. StartCol: integer;
  70. EndCol: integer;
  71. Found: TFPList; // list of TPasElement at this token
  72. end;
  73. PTestResolverReferenceData = ^TTestResolverReferenceData;
  74. { TTestPasParser }
  75. TTestPasParser = Class(TPasParser)
  76. end;
  77. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  78. { TTestEnginePasResolver }
  79. TTestEnginePasResolver = class(TPas2JsResolver)
  80. private
  81. FFilename: string;
  82. FModule: TPasModule;
  83. FOnFindUnit: TOnFindUnit;
  84. FParser: TTestPasParser;
  85. FStreamResolver: TStreamResolver;
  86. FScanner: TPas2jsPasScanner;
  87. FSource: string;
  88. procedure SetModule(const AValue: TPasModule);
  89. public
  90. destructor Destroy; override;
  91. function CreateElement(AClass: TPTreeElement; const AName: String;
  92. AParent: TPasElement; AVisibility: TPasMemberVisibility;
  93. const ASrcPos: TPasSourcePos; TypeParams: TFPList = nil): TPasElement;
  94. overload; override;
  95. function FindUnit(const AName, InFilename: String; NameExpr,
  96. InFileExpr: TPasExpr): TPasModule; override;
  97. procedure UsedInterfacesFinished(Section: TPasSection); override;
  98. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  99. property Filename: string read FFilename write FFilename;
  100. property StreamResolver: TStreamResolver read FStreamResolver write FStreamResolver;
  101. property Scanner: TPas2jsPasScanner read FScanner write FScanner;
  102. property Parser: TTestPasParser read FParser write FParser;
  103. property Source: string read FSource write FSource;
  104. property Module: TPasModule read FModule write SetModule;
  105. end;
  106. { TCustomTestModule }
  107. TCustomTestModule = Class(TTestCase)
  108. private
  109. FWithTypeInfo: boolean;
  110. FSource: TStringList;
  111. FSkipTests: boolean;
  112. FScanner: TPas2jsPasScanner;
  113. FResolvers: TObjectList;// list of TTestEnginePasResolver
  114. FPasProgram: TPasProgram;
  115. FPasLibrary: TPasLibrary;
  116. FParser: TTestPasParser;
  117. FModule: TPasModule;
  118. FJSSource: TStringList;
  119. FJSRegModuleCall: TJSCallExpression;
  120. FJSModuleSrc: TJSSourceElements;
  121. FJSModuleCallArgs: TJSArguments;
  122. FJSModule: TJSSourceElements;
  123. FJSInterfaceUses: TJSArrayLiteral;
  124. FJSInitBody: TJSFunctionBody;
  125. FJSImplentationUses: TJSArrayLiteral;
  126. FJSImplementationUses: TJSArrayLiteral;
  127. FHub: TPas2JSResolverHub;
  128. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  129. FHintMsgs: TObjectList; // list of TTestHintMessage
  130. FFirstPasStatement: TPasImplBlock;
  131. FFileResolver: TStreamResolver;
  132. FFilename: string;
  133. FExpectedErrorNumber: integer;
  134. FExpectedErrorMsg: string;
  135. FExpectedErrorClass: ExceptClass;
  136. FEngine: TTestEnginePasResolver;
  137. FConverter: TPasToJSConverter;
  138. {$IFDEF EnablePasTreeGlobalRefCount}
  139. FElementRefCountAtSetup: int64;
  140. {$ENDIF}
  141. procedure FreeSrcMarkers;
  142. function GetResolverCount: integer;
  143. function GetResolvers(Index: integer): TTestEnginePasResolver;
  144. function GetMsgCount: integer;
  145. function GetMsgs(Index: integer): TTestHintMessage;
  146. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  147. procedure OnParserLog(Sender: TObject; const Msg: String);
  148. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  149. procedure OnScannerLog(Sender: TObject; const Msg: String);
  150. procedure OnCheckElementParent(El: TPasElement; arg: pointer);
  151. procedure OnFindReference(El: TPasElement; FindData: pointer);
  152. procedure SetWithTypeInfo(const AValue: boolean);
  153. protected
  154. procedure SetUp; override;
  155. function CreateConverter: TPasToJSConverter; virtual;
  156. function LoadUnit(const aUnitName: String): TPasModule;
  157. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  158. procedure TearDown; override;
  159. Procedure Add(Line: string); virtual;
  160. Procedure Add(const Lines: array of string);
  161. Procedure StartParsing; virtual;
  162. procedure ParseModuleQueue; virtual;
  163. procedure ParseModule; virtual;
  164. procedure ParseProgram; virtual;
  165. procedure ParseLibrary; virtual;
  166. procedure ParseUnit; virtual;
  167. protected
  168. FirstSrcMarker, LastSrcMarker: PSrcMarker;
  169. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  170. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  171. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  172. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  173. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  174. procedure AddSystemUnit(Parts: TSystemUnitParts = []); virtual;
  175. procedure StartProgram(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  176. procedure StartLibrary(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  177. procedure StartUnit(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  178. procedure ConvertModule; virtual;
  179. procedure ConvertProgram; virtual;
  180. procedure ConvertLibrary; virtual;
  181. procedure ConvertUnit; virtual;
  182. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  183. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  184. function GetDottedIdentifier(El: TJSElement): string;
  185. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  186. ImplStatements: string = ''); virtual;
  187. procedure CheckFullSource(Msg,ExpectedSrc: String); virtual;
  188. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  189. procedure CheckUnit(aFilename, ExpectedSrc: string); virtual;
  190. procedure CheckReferenceDirectives; virtual;
  191. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  192. Msg: string; Marker: PSrcMarker = nil); virtual;
  193. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  194. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  195. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  196. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  197. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  198. function IsErrorExpected(E: Exception): boolean;
  199. procedure RaiseErrorAtSrc(Msg: string; const aFilename: string; aRow, aCol: integer);
  200. procedure RaiseErrorAtSrcMarker(Msg: string; aMarker: PSrcMarker);
  201. procedure HandleScannerError(E: EScannerError);
  202. procedure HandleParserError(E: EParserError);
  203. procedure HandlePasResolveError(E: EPasResolve);
  204. procedure HandlePas2JSError(E: EPas2JS);
  205. procedure HandleException(E: Exception);
  206. procedure FailException(E: Exception);
  207. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  208. function IndexOfResolver(const aFilename: string): integer;
  209. function GetResolver(const aFilename: string): TTestEnginePasResolver;
  210. procedure GetSrc(Index: integer; out SrcLines: TStringList; out aFilename: string);
  211. function FindElementsAt(aFilename: string; aLine, aStartCol, aEndCol: integer): TFPList;// list of TPasElement
  212. function FindElementsAt(aMarker: PSrcMarker; ErrorOnNoElements: boolean = true): TFPList;// list of TPasElement
  213. function FindSrcLabel(const Identifier: string): PSrcMarker;
  214. function FindElementsAtSrcLabel(const Identifier: string; ErrorOnNoElements: boolean = true): TFPList;// list of TPasElement
  215. function GetDefaultNamespace: string;
  216. property PasProgram: TPasProgram Read FPasProgram;
  217. property PasLibrary: TPasLibrary Read FPasLibrary;
  218. property ResolverEngine: TTestEnginePasResolver read FEngine;
  219. property Filename: string read FFilename;
  220. Property Module: TPasModule Read FModule;
  221. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  222. property Converter: TPasToJSConverter read FConverter;
  223. property JSSource: TStringList read FJSSource;
  224. property JSModule: TJSSourceElements read FJSModule;
  225. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  226. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  227. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  228. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  229. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  230. property JSInitBody: TJSFunctionBody read FJSInitBody;
  231. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  232. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  233. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  234. property SkipTests: boolean read FSkipTests write FSkipTests;
  235. public
  236. constructor Create; override;
  237. destructor Destroy; override;
  238. property Hub: TPas2JSResolverHub read FHub;
  239. property Source: TStringList read FSource;
  240. property FileResolver: TStreamResolver read FFileResolver;
  241. property Scanner: TPas2jsPasScanner read FScanner;
  242. property Parser: TTestPasParser read FParser;
  243. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  244. property ResolverCount: integer read GetResolverCount;
  245. property MsgCount: integer read GetMsgCount;
  246. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  247. property WithTypeInfo: boolean read FWithTypeInfo write SetWithTypeInfo;
  248. end;
  249. { TTestModule }
  250. TTestModule = class(TCustomTestModule)
  251. Published
  252. Procedure TestReservedWords;
  253. // program, units, includes
  254. Procedure TestEmptyProgram;
  255. Procedure TestEmptyProgramUseStrict;
  256. Procedure TestEmptyUnit;
  257. Procedure TestEmptyUnitUseStrict;
  258. Procedure TestDottedUnitNames;
  259. Procedure TestDottedUnitNameImpl;
  260. Procedure TestDottedUnitExpr;
  261. Procedure Test_ModeFPCFail;
  262. Procedure Test_ModeSwitchCBlocksFail;
  263. Procedure TestUnit_UseSystem;
  264. Procedure TestUnit_Intf1Impl2Intf1;
  265. Procedure TestIncludeVersion;
  266. // vars/const
  267. Procedure TestVarInt;
  268. Procedure TestVarBaseTypes;
  269. Procedure TestBaseTypeSingleFail;
  270. Procedure TestBaseTypeExtendedFail;
  271. Procedure TestConstBaseTypes;
  272. Procedure TestUnitImplVars;
  273. Procedure TestUnitImplConsts;
  274. Procedure TestUnitImplRecord;
  275. Procedure TestRenameJSNameConflict;
  276. Procedure TestLocalConst;
  277. Procedure TestVarExternal;
  278. Procedure TestVarExternalOtherUnit;
  279. Procedure TestVarAbsoluteFail;
  280. Procedure TestConstExternal;
  281. // numbers
  282. Procedure TestDouble;
  283. Procedure TestDoubleSmall;
  284. Procedure TestInteger;
  285. Procedure TestIntegerRange;
  286. Procedure TestIntegerTypecasts;
  287. Procedure TestInteger_BitwiseShrNativeInt;
  288. Procedure TestInteger_BitwiseShlNativeInt;
  289. Procedure TestInteger_SystemFunc;
  290. Procedure TestInteger_AssignOutsideConst;
  291. Procedure TestCurrency;
  292. Procedure TestForBoolDo;
  293. Procedure TestForIntDo;
  294. Procedure TestForIntInDo;
  295. // strings
  296. Procedure TestCharConst;
  297. Procedure TestChar_Compare;
  298. Procedure TestChar_BuiltInProcs;
  299. Procedure TestStringConst;
  300. Procedure TestStringConst_InvalidUTF16;
  301. Procedure TestStringConstSurrogate;
  302. Procedure TestStringConstWhitespaces;
  303. Procedure TestStringConst_Multiline;
  304. Procedure TestString_Length;
  305. Procedure TestString_Compare;
  306. Procedure TestString_SetLength;
  307. Procedure TestString_CharAt;
  308. Procedure TestStringHMinusFail;
  309. Procedure TestStr;
  310. Procedure TestBaseType_AnsiStringFail;
  311. Procedure TestBaseType_WideStringFail;
  312. Procedure TestBaseType_ShortStringFail;
  313. Procedure TestBaseType_RawByteStringFail;
  314. Procedure TestTypeShortstring_Fail;
  315. Procedure TestCharSet_Custom;
  316. Procedure TestWideChar;
  317. Procedure TestForCharDo;
  318. Procedure TestForCharInDo;
  319. // alias types
  320. Procedure TestAliasTypeRef;
  321. Procedure TestTypeCast_BaseTypes;
  322. Procedure TestTypeCast_AliasBaseTypes;
  323. // functions
  324. Procedure TestEmptyProc;
  325. Procedure TestProcOneParam;
  326. Procedure TestFunctionWithoutParams;
  327. Procedure TestProcedureWithoutParams;
  328. Procedure TestPrgProcVar;
  329. Procedure TestProcTwoArgs;
  330. Procedure TestProc_DefaultValue;
  331. Procedure TestUnitProcVar;
  332. Procedure TestImplProc;
  333. Procedure TestFunctionResult;
  334. Procedure TestNestedProc;
  335. Procedure TestNestedProc_ResultString;
  336. Procedure TestForwardProc;
  337. Procedure TestNestedForwardProc;
  338. Procedure TestAssignFunctionResult;
  339. Procedure TestFunctionResultInCondition;
  340. Procedure TestFunctionResultInForLoop;
  341. Procedure TestFunctionResultInTypeCast;
  342. Procedure TestExit;
  343. Procedure TestExit_ResultInFinally;
  344. Procedure TestBreak;
  345. Procedure TestBreakAsVar;
  346. Procedure TestContinue;
  347. Procedure TestProc_External;
  348. Procedure TestProc_ExternalOtherUnit;
  349. Procedure TestProc_Asm;
  350. Procedure TestProc_AsmSubBlock;
  351. Procedure TestProc_Assembler;
  352. Procedure TestProc_VarParam;
  353. Procedure TestProc_VarParamString;
  354. Procedure TestProc_VarParamV;
  355. Procedure TestProc_Overload;
  356. Procedure TestProc_OverloadForward;
  357. Procedure TestProc_OverloadIntfImpl;
  358. Procedure TestProc_OverloadNested;
  359. Procedure TestProc_OverloadNestedForward;
  360. Procedure TestProc_OverloadUnitCycle;
  361. Procedure TestProc_Varargs;
  362. Procedure TestProc_ConstOrder;
  363. Procedure TestProc_DuplicateConst;
  364. Procedure TestProc_LocalVarAbsolute;
  365. Procedure TestProc_ResultAbsolute;
  366. Procedure TestProc_LocalVarInit;
  367. Procedure TestProc_ReservedWords;
  368. Procedure TestProc_ConstRefWord;
  369. // anonymous functions
  370. Procedure TestAnonymousProc_Assign_ObjFPC;
  371. Procedure TestAnonymousProc_Assign_Delphi;
  372. Procedure TestAnonymousProc_Arg;
  373. Procedure TestAnonymousProc_Typecast;
  374. Procedure TestAnonymousProc_With;
  375. Procedure TestAnonymousProc_ExceptOn;
  376. Procedure TestAnonymousProc_Nested;
  377. Procedure TestAnonymousProc_NestedAssignResult;
  378. Procedure TestAnonymousProc_Class;
  379. Procedure TestAnonymousProc_ForLoop;
  380. Procedure TestAnonymousProc_AsmDelphi;
  381. // enums, sets
  382. Procedure TestEnum_Name;
  383. Procedure TestEnum_Number;
  384. Procedure TestEnum_ConstFail;
  385. Procedure TestEnum_Functions;
  386. Procedure TestEnumRg_Functions;
  387. Procedure TestEnum_AsParams;
  388. Procedure TestEnumRange_Array;
  389. Procedure TestEnum_ForIn;
  390. Procedure TestEnum_ScopedNumber;
  391. Procedure TestEnum_InFunction;
  392. Procedure TestEnum_Name_Anonymous_Unit;
  393. Procedure TestSet_Enum;
  394. Procedure TestSet_Operators;
  395. Procedure TestSet_Operator_In;
  396. Procedure TestSet_Functions;
  397. Procedure TestSet_PassAsArgClone;
  398. Procedure TestSet_AsParams;
  399. Procedure TestSet_Property;
  400. Procedure TestSet_EnumConst;
  401. Procedure TestSet_IntConst;
  402. Procedure TestSet_IntRange;
  403. Procedure TestSet_AnonymousEnumType;
  404. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  405. Procedure TestSet_ConstEnum;
  406. Procedure TestSet_ConstChar;
  407. Procedure TestSet_ConstInt;
  408. Procedure TestSet_InFunction;
  409. Procedure TestSet_ForIn;
  410. // statements
  411. Procedure TestNestBegin;
  412. Procedure TestIncDec;
  413. Procedure TestLoHiFpcMode;
  414. Procedure TestLoHiDelphiMode;
  415. Procedure TestAssignments;
  416. Procedure TestArithmeticOperators1;
  417. Procedure TestMultiAdd;
  418. Procedure TestLogicalOperators;
  419. Procedure TestBitwiseOperators;
  420. Procedure TestBitwiseOperatorsLongword;
  421. Procedure TestFunctionInt;
  422. Procedure TestFunctionString;
  423. Procedure TestIfThen;
  424. Procedure TestForLoop;
  425. Procedure TestForLoopInsideFunction;
  426. Procedure TestForLoop_ReadVarAfter;
  427. Procedure TestForLoop_Nested;
  428. Procedure TestRepeatUntil;
  429. Procedure TestAsmBlock;
  430. Procedure TestAsmPas_Impl; // ToDo
  431. Procedure TestTryFinally;
  432. Procedure TestTryExcept;
  433. Procedure TestTryExcept_ReservedWords;
  434. Procedure TestIfThenRaiseElse;
  435. Procedure TestCaseOf;
  436. Procedure TestCaseOf_UseSwitch;
  437. Procedure TestCaseOfNoElse;
  438. Procedure TestCaseOfNoElse_UseSwitch;
  439. Procedure TestCaseOfRange;
  440. Procedure TestCaseOfString;
  441. Procedure TestCaseOfChar;
  442. Procedure TestCaseOfExternalClassConst;
  443. Procedure TestDebugger;
  444. // arrays
  445. Procedure TestArray_Dynamic;
  446. Procedure TestArray_Dynamic_Nil;
  447. Procedure TestArray_DynMultiDimensional;
  448. Procedure TestArray_DynamicAssign;
  449. Procedure TestArray_StaticInt;
  450. Procedure TestArray_StaticBool;
  451. Procedure TestArray_StaticChar;
  452. Procedure TestArray_StaticMultiDim;
  453. Procedure TestArray_StaticInFunction;
  454. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  455. Procedure TestArrayOfRecord;
  456. Procedure TestArray_StaticRecord;
  457. Procedure TestArrayOfSet;
  458. Procedure TestArray_DynAsParam;
  459. Procedure TestArray_StaticAsParam;
  460. Procedure TestArrayElement_AsParams;
  461. Procedure TestArrayElementFromFuncResult_AsParams;
  462. Procedure TestArrayEnumTypeRange;
  463. Procedure TestArray_SetLengthOutArg;
  464. Procedure TestArray_SetLengthProperty;
  465. Procedure TestArray_SetLengthMultiDim;
  466. Procedure TestArray_SetLengthDynOfStatic;
  467. Procedure TestArray_OpenArrayOfString;
  468. Procedure TestArray_ArrayOfCharAssignString;
  469. Procedure TestArray_ConstRef;
  470. Procedure TestArray_Concat;
  471. Procedure TestArray_Concat_Append;
  472. Procedure TestArray_Concat_Append_Var;
  473. Procedure TestArray_Copy;
  474. Procedure TestArray_InsertDelete;
  475. Procedure TestArray_Add_Append;
  476. Procedure TestArray_DynArrayConstObjFPC;
  477. Procedure TestArray_DynArrayConstDelphi;
  478. Procedure TestArray_ArrayLitAsParam;
  479. Procedure TestArray_ArrayLitMultiDimAsParam;
  480. Procedure TestArray_ArrayLitStaticAsParam;
  481. Procedure TestArray_ForInArrOfString;
  482. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  483. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  484. Procedure TestArrayOfConst_TVarRec;
  485. Procedure TestArrayOfConst_PassBaseTypes;
  486. Procedure TestArrayOfConst_PassObj;
  487. // record
  488. Procedure TestRecord_Empty;
  489. Procedure TestRecord_Var;
  490. Procedure TestRecord_VarExternal;
  491. Procedure TestRecord_WithDo;
  492. Procedure TestRecord_Assign;
  493. Procedure TestRecord_AsParams;
  494. Procedure TestRecord_ConstRef;
  495. Procedure TestRecordElement_AsParams;
  496. Procedure TestRecordElementFromFuncResult_AsParams;
  497. Procedure TestRecordElementFromWith_AsParams;
  498. Procedure TestRecord_Equal;
  499. Procedure TestRecord_JSValue;
  500. Procedure TestRecord_VariantFail;
  501. Procedure TestRecord_FieldArray;
  502. Procedure TestRecord_Const;
  503. Procedure TestRecord_TypecastFail;
  504. Procedure TestRecord_InFunction;
  505. Procedure TestRecord_ArrayConstMultiline;
  506. // anonymous record
  507. Procedure TestRecordAnonym_Field;
  508. Procedure TestRecordAnonym_Assign;
  509. Procedure TestRecordAnonym_Nested;
  510. Procedure TestRecordAnonym_Const;
  511. Procedure TestRecordAnonym_InFunction;
  512. // advanced record
  513. Procedure TestAdvRecord_Function;
  514. Procedure TestAdvRecord_Property;
  515. Procedure TestAdvRecord_PropertyDefault;
  516. Procedure TestAdvRecord_Property_ClassMethod;
  517. Procedure TestAdvRecord_Const;
  518. Procedure TestAdvRecord_ExternalField;
  519. Procedure TestAdvRecord_SubRecord;
  520. Procedure TestAdvRecord_SubClass;
  521. Procedure TestAdvRecord_SubInterfaceFail;
  522. Procedure TestAdvRecord_Constructor;
  523. Procedure TestAdvRecord_ClassConstructor_Program;
  524. Procedure TestAdvRecord_ClassConstructor_Unit;
  525. // classes
  526. Procedure TestClass_TObjectDefaultConstructor;
  527. Procedure TestClass_TObjectConstructorWithParams;
  528. Procedure TestClass_TObjectConstructorWithDefaultParam;
  529. Procedure TestClass_Var;
  530. Procedure TestClass_Method;
  531. Procedure TestClass_Implementation;
  532. Procedure TestClass_Inheritance;
  533. Procedure TestClass_TypeAlias;
  534. Procedure TestClass_AbstractMethod;
  535. Procedure TestClass_CallInherited_ProcNoParams;
  536. Procedure TestClass_CallInherited_WithParams;
  537. Procedure TestClasS_CallInheritedConstructor;
  538. Procedure TestClass_ClassVar_Assign;
  539. Procedure TestClass_CallClassMethod;
  540. Procedure TestClass_CallClassMethodStatic;
  541. Procedure TestClass_Property;
  542. Procedure TestClass_Property_ClassMethod;
  543. Procedure TestClass_Property_ClassMethodStatic;
  544. Procedure TestClass_Property_Indexed;
  545. Procedure TestClass_Property_IndexSpec;
  546. Procedure TestClass_PropertyOfTypeArray;
  547. Procedure TestClass_PropertyDefault;
  548. Procedure TestClass_PropertyDefault_TypecastToOtherDefault;
  549. //Procedure TestClass_PropertyDefault;
  550. Procedure TestClass_PropertyOverride;
  551. Procedure TestClass_PropertyIncVisibility;
  552. Procedure TestClass_Assigned;
  553. Procedure TestClass_WithClassDoCreate;
  554. Procedure TestClass_WithClassInstDoProperty;
  555. Procedure TestClass_WithClassInstDoPropertyWithParams;
  556. Procedure TestClass_WithClassInstDoFunc;
  557. Procedure TestClass_TypeCast;
  558. Procedure TestClass_TypeCastUntypedParam;
  559. Procedure TestClass_Overloads;
  560. Procedure TestClass_OverloadsAncestor;
  561. Procedure TestClass_OverloadConstructor;
  562. Procedure TestClass_OverloadDelphiOverride;
  563. Procedure TestClass_ReintroduceVarDelphi;
  564. Procedure TestClass_ReintroducedVar;
  565. Procedure TestClass_RaiseDescendant;
  566. Procedure TestClass_ExternalMethod;
  567. Procedure TestClass_ExternalVirtualNameMismatchFail;
  568. Procedure TestClass_ExternalOverrideFail;
  569. Procedure TestClass_ExternalVar;
  570. Procedure TestClass_Const;
  571. Procedure TestClass_ConstEnum;
  572. Procedure TestClass_LocalConstDuplicate_Prg;
  573. Procedure TestClass_LocalConstDuplicate_Unit;
  574. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  575. Procedure TestClass_LocalVarSelfFail;
  576. Procedure TestClass_ArgSelfFail;
  577. Procedure TestClass_NestedProcSelf;
  578. Procedure TestClass_NestedProcSelf2;
  579. Procedure TestClass_NestedProcClassSelf;
  580. Procedure TestClass_NestedProcCallInherited;
  581. Procedure TestClass_TObjectFree;
  582. Procedure TestClass_TObjectFree_VarArg;
  583. Procedure TestClass_TObjectFreeNewInstance;
  584. Procedure TestClass_TObjectFreeLowerCase;
  585. Procedure TestClass_TObjectFreeFunctionFail;
  586. Procedure TestClass_TObjectFreePropertyFail;
  587. Procedure TestClass_ForIn;
  588. Procedure TestClass_DispatchMessage;
  589. Procedure TestClass_Message_DuplicateIntFail;
  590. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  591. // class of
  592. Procedure TestClassOf_Create;
  593. Procedure TestClassOf_Call;
  594. Procedure TestClassOf_Assign;
  595. Procedure TestClassOf_Is;
  596. Procedure TestClassOf_Compare;
  597. Procedure TestClassOf_ClassVar;
  598. Procedure TestClassOf_ClassMethod;
  599. Procedure TestClassOf_ClassProperty;
  600. Procedure TestClassOf_ClassMethodSelf;
  601. Procedure TestClassOf_TypeCast;
  602. Procedure TestClassOf_ImplicitFunctionCall;
  603. Procedure TestClassOf_Const;
  604. // nested class
  605. Procedure TestNestedClass_Alias;
  606. Procedure TestNestedClass_Record;
  607. Procedure TestNestedClass_Class;
  608. Procedure TestNestedClass_CallInherited;
  609. // external class
  610. Procedure TestExternalClass_Var;
  611. Procedure TestExternalClass_Const;
  612. Procedure TestExternalClass_Dollar;
  613. Procedure TestExternalClass_DuplicateVarFail;
  614. Procedure TestExternalClass_Method;
  615. Procedure TestExternalClass_ClassMethod;
  616. Procedure TestExternalClass_ClassMethodStatic;
  617. Procedure TestExternalClass_FunctionResultInTypeCast;
  618. Procedure TestExternalClass_NonExternalOverride;
  619. Procedure TestExternalClass_OverloadHint;
  620. Procedure TestExternalClass_SameNamePublishedProperty;
  621. Procedure TestExternalClass_Property;
  622. Procedure TestExternalClass_PropertyDate;
  623. Procedure TestExternalClass_ClassProperty;
  624. Procedure TestExternalClass_ClassOf;
  625. Procedure TestExternalClass_ClassOtherUnit;
  626. Procedure TestExternalClass_Is;
  627. Procedure TestExternalClass_As;
  628. Procedure TestExternalClass_DestructorFail;
  629. Procedure TestExternalClass_New;
  630. Procedure TestExternalClass_ClassOf_New;
  631. Procedure TestExternalClass_FuncClassOf_New;
  632. Procedure TestExternalClass_New_PasClassFail;
  633. Procedure TestExternalClass_New_PasClassBracketsFail;
  634. Procedure TestExternalClass_NewExtName;
  635. Procedure TestExternalClass_Constructor;
  636. Procedure TestExternalClass_ConstructorBrackets;
  637. Procedure TestExternalClass_LocalConstSameName;
  638. Procedure TestExternalClass_ReintroduceOverload;
  639. Procedure TestExternalClass_Inherited;
  640. Procedure TestExternalClass_PascalAncestorFail;
  641. Procedure TestExternalClass_NewInstance;
  642. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  643. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  644. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  645. Procedure TestExternalClass_JSFunctionPasDescendant;
  646. Procedure TestExternalClass_PascalProperty;
  647. Procedure TestExternalClass_TypeCastToRootClass;
  648. Procedure TestExternalClass_TypeCastToJSObject;
  649. Procedure TestExternalClass_TypeCastStringToExternalString;
  650. Procedure TestExternalClass_TypeCastToJSFunction;
  651. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  652. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  653. Procedure TestExternalClass_BracketAccessor;
  654. Procedure TestExternalClass_BracketAccessor_Call;
  655. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  656. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  657. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  658. Procedure TestExternalClass_BracketAccessor_MultiType;
  659. Procedure TestExternalClass_BracketAccessor_Index;
  660. Procedure TestExternalClass_ForInJSObject;
  661. Procedure TestExternalClass_ForInJSArray;
  662. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  663. Procedure TestExternalClass_NestedConstructor;
  664. // class interfaces
  665. Procedure TestClassInterface_Corba;
  666. Procedure TestClassInterface_Corba_ProcExternalFail;
  667. Procedure TestClassInterface_Corba_Overloads;
  668. Procedure TestClassInterface_Corba_DuplicateGUIInIntfListFail;
  669. Procedure TestClassInterface_Corba_DuplicateGUIInAncestorFail;
  670. Procedure TestClassInterface_Corba_AncestorImpl;
  671. Procedure TestClassInterface_Corba_ImplReintroduce;
  672. Procedure TestClassInterface_Corba_MethodResolution;
  673. Procedure TestClassInterface_COM_AncestorMoreInterfaces;
  674. Procedure TestClassInterface_Corba_MethodOverride;
  675. Procedure TestClassInterface_Corba_Delegation;
  676. Procedure TestClassInterface_Corba_DelegationStatic;
  677. Procedure TestClassInterface_Corba_Operators;
  678. Procedure TestClassInterface_Corba_Args;
  679. Procedure TestClassInterface_Corba_ForIn;
  680. Procedure TestClassInterface_Corba_ArrayOfIntf;
  681. Procedure TestClassInterface_COM_AssignVar;
  682. Procedure TestClassInterface_COM_AssignArg;
  683. Procedure TestClassInterface_COM_FunctionResult;
  684. Procedure TestClassInterface_COM_InheritedFuncResult;
  685. Procedure TestClassInterface_COM_IsAsTypeCasts;
  686. Procedure TestClassInterface_COM_PassAsArg;
  687. Procedure TestClassInterface_COM_PassToUntypedParam;
  688. Procedure TestClassInterface_COM_FunctionInExpr;
  689. Procedure TestClassInterface_COM_Property;
  690. Procedure TestClassInterface_COM_IntfProperty;
  691. Procedure TestClassInterface_COM_Delegation;
  692. Procedure TestClassInterface_COM_With;
  693. Procedure TestClassInterface_COM_ForIn;
  694. Procedure TestClassInterface_COM_ArrayOfIntf;
  695. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  696. Procedure TestClassInterface_COM_RecordIntfFail;
  697. Procedure TestClassInterface_COM_UnitInitialization;
  698. Procedure TestClassInterface_Corba_GUID;
  699. Procedure TestClassInterface_Corba_GUIDProperty;
  700. // helpers
  701. Procedure TestClassHelper_ClassVar;
  702. Procedure TestClassHelper_Method_AccessInstanceFields;
  703. Procedure TestClassHelper_Method_Call;
  704. Procedure TestClassHelper_Method_Nested_Call;
  705. Procedure TestClassHelper_ClassMethod_Call;
  706. Procedure TestClassHelper_ClassOf;
  707. Procedure TestClassHelper_MethodRefObjFPC;
  708. Procedure TestClassHelper_Constructor;
  709. Procedure TestClassHelper_InheritedObjFPC;
  710. Procedure TestClassHelper_Property;
  711. Procedure TestClassHelper_Property_Array;
  712. Procedure TestClassHelper_Property_Array_Default;
  713. Procedure TestClassHelper_Property_Array_DefaultDefault;
  714. Procedure TestClassHelper_ClassProperty;
  715. Procedure TestClassHelper_ClassPropertyStatic;
  716. Procedure TestClassHelper_ClassProperty_Array;
  717. Procedure TestClassHelper_ForIn;
  718. Procedure TestClassHelper_PassProperty;
  719. Procedure TestExtClassHelper_ClassVar;
  720. Procedure TestExtClassHelper_Method_Call;
  721. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  722. Procedure TestRecordHelper_ClassVar;
  723. Procedure TestRecordHelper_Method_Call;
  724. Procedure TestRecordHelper_Constructor;
  725. Procedure TestTypeHelper_ClassVar;
  726. Procedure TestTypeHelper_PassResultElement;
  727. Procedure TestTypeHelper_PassArgs;
  728. Procedure TestTypeHelper_PassVarConst;
  729. Procedure TestTypeHelper_PassFuncResult;
  730. Procedure TestTypeHelper_PassPropertyField;
  731. Procedure TestTypeHelper_PassPropertyGetter;
  732. Procedure TestTypeHelper_PassClassPropertyField;
  733. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  734. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  735. Procedure TestTypeHelper_Property;
  736. Procedure TestTypeHelper_Property_Array;
  737. Procedure TestTypeHelper_ClassProperty;
  738. Procedure TestTypeHelper_ClassProperty_Array;
  739. Procedure TestTypeHelper_ClassMethod;
  740. Procedure TestTypeHelper_ExtClassMethodFail;
  741. Procedure TestTypeHelper_Constructor;
  742. Procedure TestTypeHelper_Word;
  743. Procedure TestTypeHelper_Boolean;
  744. Procedure TestTypeHelper_WordBool;
  745. Procedure TestTypeHelper_Double;
  746. Procedure TestTypeHelper_NativeInt;
  747. Procedure TestTypeHelper_StringChar;
  748. Procedure TestTypeHelper_JSValue;
  749. Procedure TestTypeHelper_Array;
  750. Procedure TestTypeHelper_EnumType;
  751. Procedure TestTypeHelper_SetType;
  752. Procedure TestTypeHelper_InterfaceType;
  753. Procedure TestTypeHelper_NestedSelf;
  754. // proc types
  755. Procedure TestProcType;
  756. Procedure TestProcType_Arg;
  757. Procedure TestProcType_FunctionFPC;
  758. Procedure TestProcType_FunctionDelphi;
  759. Procedure TestProcType_ProcedureDelphi;
  760. Procedure TestProcType_AsParam;
  761. Procedure TestProcType_MethodFPC;
  762. Procedure TestProcType_MethodDelphi;
  763. Procedure TestProcType_PropertyFPC;
  764. Procedure TestProcType_PropertyDelphi;
  765. Procedure TestProcType_WithClassInstDoPropertyFPC;
  766. Procedure TestProcType_Nested;
  767. Procedure TestProcType_NestedOfObject;
  768. Procedure TestProcType_ReferenceToProc;
  769. Procedure TestProcType_ReferenceToMethod;
  770. Procedure TestProcType_Typecast;
  771. Procedure TestProcType_PassProcToUntyped;
  772. Procedure TestProcType_PassProcToArray;
  773. Procedure TestProcType_SafeCallObjFPC;
  774. Procedure TestProcType_SafeCallDelphi;
  775. Procedure TestProcType_SafeCall_Arg;
  776. // pointer
  777. Procedure TestPointer;
  778. Procedure TestPointer_Proc;
  779. Procedure TestPointer_AssignRecordFail;
  780. Procedure TestPointer_AssignStaticArrayFail;
  781. Procedure TestPointer_TypeCastJSValueToPointer;
  782. Procedure TestPointer_NonRecordFail;
  783. Procedure TestPointer_AnonymousArgTypeFail;
  784. Procedure TestPointer_AnonymousVarTypeFail;
  785. Procedure TestPointer_AnonymousResultTypeFail;
  786. Procedure TestPointer_AddrOperatorFail;
  787. Procedure TestPointer_ArrayParamsFail;
  788. Procedure TestPointer_PointerAddFail;
  789. Procedure TestPointer_IncPointerFail;
  790. Procedure TestPointer_Record;
  791. Procedure TestPointer_RecordArg;
  792. // jsvalue
  793. Procedure TestJSValue_AssignToJSValue;
  794. Procedure TestJSValue_TypeCastToBaseType;
  795. Procedure TestJSValue_TypecastToJSValue;
  796. Procedure TestJSValue_Equal;
  797. Procedure TestJSValue_If;
  798. Procedure TestJSValue_Not;
  799. Procedure TestJSValue_Enum;
  800. Procedure TestJSValue_ClassInstance;
  801. Procedure TestJSValue_ClassOf;
  802. Procedure TestJSValue_ArrayOfJSValue;
  803. Procedure TestJSValue_ArrayLit;
  804. Procedure TestJSValue_Params;
  805. Procedure TestJSValue_UntypedParam;
  806. Procedure TestJSValue_FuncResultType;
  807. Procedure TestJSValue_ProcType_Assign;
  808. Procedure TestJSValue_ProcType_Equal;
  809. Procedure TestJSValue_ProcType_Param;
  810. Procedure TestJSValue_AssignToPointerFail;
  811. Procedure TestJSValue_OverloadDouble;
  812. Procedure TestJSValue_OverloadNativeInt;
  813. Procedure TestJSValue_OverloadWord;
  814. Procedure TestJSValue_OverloadString;
  815. Procedure TestJSValue_OverloadChar;
  816. Procedure TestJSValue_OverloadPointer;
  817. Procedure TestJSValue_ForIn;
  818. // RTTI
  819. Procedure TestRTTI_IntRange;
  820. Procedure TestRTTI_Double;
  821. Procedure TestRTTI_ProcType;
  822. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  823. Procedure TestRTTI_ProcTypeAnonymous;
  824. Procedure TestRTTI_EnumAndSetType;
  825. Procedure TestRTTI_EnumRange;
  826. Procedure TestRTTI_AnonymousEnumType;
  827. Procedure TestRTTI_StaticArray;
  828. Procedure TestRTTI_DynArray;
  829. Procedure TestRTTI_ArrayNestedAnonymous;
  830. Procedure TestRTTI_PublishedMethodOverloadFail;
  831. Procedure TestRTTI_PublishedMethodHideNoHint;
  832. Procedure TestRTTI_PublishedMethodExternalFail;
  833. Procedure TestRTTI_PublishedClassPropertyFail;
  834. Procedure TestRTTI_PublishedClassFieldFail;
  835. Procedure TestRTTI_PublishedFieldExternalFail;
  836. Procedure TestRTTI_Class_Field;
  837. Procedure TestRTTI_Class_FieldPrivate;
  838. Procedure TestRTTI_Class_Method;
  839. Procedure TestRTTI_Class_MethodArgFlags;
  840. Procedure TestRTTI_Class_MethodPrivate;
  841. Procedure TestRTTI_Class_Property;
  842. Procedure TestRTTI_Class_PropertyParams;
  843. Procedure TestRTTI_Class_PropertyPrivate;
  844. Procedure TestRTTI_Class_ClassProperty;
  845. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  846. Procedure TestRTTI_Class_OmitRTTI;
  847. Procedure TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  848. Procedure TestRTTI_IndexModifier;
  849. Procedure TestRTTI_StoredModifier;
  850. Procedure TestRTTI_DefaultValue;
  851. Procedure TestRTTI_DefaultValueSet;
  852. Procedure TestRTTI_DefaultValueRangeType;
  853. Procedure TestRTTI_DefaultValueInherit;
  854. Procedure TestRTTI_OverrideMethod;
  855. Procedure TestRTTI_ReintroduceMethod;
  856. Procedure TestRTTI_OverloadProperty;
  857. // ToDo: array argument
  858. Procedure TestRTTI_ClassForward;
  859. Procedure TestRTTI_ClassOf;
  860. Procedure TestRTTI_Record;
  861. Procedure TestRTTI_RecordAnonymousArray;
  862. Procedure TestRTTI_Record_ClassVarType;
  863. Procedure TestRTTI_LocalTypes;
  864. Procedure TestRTTI_TypeInfo_BaseTypes;
  865. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  866. Procedure TestRTTI_TypeInfo_LocalFail;
  867. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  868. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  869. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  870. Procedure TestRTTI_TypeInfo_FunctionClassType;
  871. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  872. Procedure TestRTTI_Interface_Corba;
  873. Procedure TestRTTI_Interface_COM;
  874. Procedure TestRTTI_ClassHelper;
  875. Procedure TestRTTI_ExternalClass;
  876. Procedure TestRTTI_Unit;
  877. // Resourcestring
  878. Procedure TestResourcestringProgram;
  879. Procedure TestResourcestringUnit;
  880. Procedure TestResourcestringImplementation;
  881. // Attributes
  882. Procedure TestAttributes_Members;
  883. Procedure TestAttributes_Types;
  884. Procedure TestAttributes_HelperConstructor_Fail;
  885. Procedure TestAttributes_InterfacesList;
  886. // Assertions, checks
  887. procedure TestAssert;
  888. procedure TestAssert_SysUtils;
  889. procedure TestObjectChecks;
  890. procedure TestOverflowChecks_Int;
  891. procedure TestRangeChecks_AssignInt;
  892. procedure TestRangeChecks_AssignIntRange;
  893. procedure TestRangeChecks_AssignEnum;
  894. procedure TestRangeChecks_AssignEnumRange;
  895. procedure TestRangeChecks_AssignChar;
  896. procedure TestRangeChecks_AssignCharRange;
  897. procedure TestRangeChecks_ArrayIndex;
  898. procedure TestRangeChecks_ArrayOfRecIndex;
  899. procedure TestRangeChecks_StringIndex;
  900. procedure TestRangeChecks_TypecastInt;
  901. procedure TestRangeChecks_TypeHelperInt;
  902. procedure TestRangeChecks_AssignCurrency;
  903. // Async/AWait
  904. Procedure TestAsync_Proc;
  905. Procedure TestAsync_CallResultIsPromise;
  906. Procedure TestAsync_ConstructorFail;
  907. Procedure TestAsync_PropertyGetterFail;
  908. Procedure TestAwait_NonPromiseWithTypeFail;
  909. Procedure TestAwait_AsyncCallTypeMismatch;
  910. Procedure TestAWait_OutsideAsyncFail;
  911. Procedure TestAWait_IntegerFail;
  912. Procedure TestAWait_ExternalClassPromise;
  913. Procedure TestAWait_JSValue;
  914. Procedure TestAWait_Result;
  915. Procedure TestAWait_ResultPromiseMissingTypeFail; // await(AsyncCallResultPromise) needs T
  916. Procedure TestAsync_AnonymousProc;
  917. Procedure TestAsync_AnonymousProc_PromiseViaDotContext;
  918. Procedure TestAsync_ProcType;
  919. Procedure TestAsync_ProcTypeAsyncModMismatchFail;
  920. Procedure TestAsync_Inherited;
  921. Procedure TestAsync_ClassInterface;
  922. Procedure TestAsync_ClassInterface_AsyncMissmatchFail;
  923. Procedure TestAWait_ClassAs;
  924. // Library
  925. Procedure TestLibrary_Empty;
  926. Procedure TestLibrary_ExportFunc;
  927. Procedure TestLibrary_ExportFuncOverloadedFail;
  928. Procedure TestLibrary_Export_Index_Fail;
  929. Procedure TestLibrary_ExportVar;
  930. Procedure TestLibrary_ExportUnitFunc;
  931. end;
  932. function LinesToStr(Args: array of const): string;
  933. function ExtractFileUnitName(aFilename: string): string;
  934. function JSToStr(El: TJSElement): string;
  935. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  936. implementation
  937. function LinesToStr(Args: array of const): string;
  938. var
  939. s: String;
  940. i: Integer;
  941. begin
  942. s:='';
  943. for i:=Low(Args) to High(Args) do
  944. case Args[i].VType of
  945. vtChar: s += Args[i].VChar+LineEnding;
  946. vtString: s += Args[i].VString^+LineEnding;
  947. vtPChar: s += Args[i].VPChar+LineEnding;
  948. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  949. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  950. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  951. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  952. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  953. end;
  954. Result:=s;
  955. end;
  956. function ExtractFileUnitName(aFilename: string): string;
  957. var
  958. p: Integer;
  959. begin
  960. Result:=ExtractFileName(aFilename);
  961. if Result='' then exit;
  962. for p:=length(Result) downto 1 do
  963. case Result[p] of
  964. '/','\': exit;
  965. '.':
  966. begin
  967. Delete(Result,p,length(Result));
  968. exit;
  969. end;
  970. end;
  971. end;
  972. function JSToStr(El: TJSElement): string;
  973. var
  974. aWriter: TBufferWriter;
  975. aJSWriter: TJSWriter;
  976. begin
  977. aJSWriter:=nil;
  978. aWriter:=TBufferWriter.Create(1000);
  979. try
  980. aJSWriter:=TJSWriter.Create(aWriter);
  981. aJSWriter.IndentSize:=2;
  982. aJSWriter.WriteJS(El);
  983. Result:=aWriter.AsString;
  984. finally
  985. aJSWriter.Free;
  986. aWriter.Free;
  987. end;
  988. end;
  989. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  990. // search diff, ignore changes in spaces
  991. const
  992. SpaceChars = [#9,#10,#13,' '];
  993. var
  994. ExpectedP, ActualP: PChar;
  995. function FindLineEnd(p: PChar): PChar;
  996. begin
  997. Result:=p;
  998. while not (Result^ in [#0,#10,#13]) do inc(Result);
  999. end;
  1000. function FindLineStart(p, MinP: PChar): PChar;
  1001. begin
  1002. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  1003. Result:=p;
  1004. end;
  1005. procedure SkipLineEnd(var p: PChar);
  1006. begin
  1007. if p^ in [#10,#13] then
  1008. begin
  1009. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  1010. inc(p,2)
  1011. else
  1012. inc(p);
  1013. end;
  1014. end;
  1015. function HasSpecialChar(s: string): boolean;
  1016. var
  1017. i: Integer;
  1018. begin
  1019. for i:=1 to length(s) do
  1020. if s[i] in [#0..#31,#127..#255] then
  1021. exit(true);
  1022. Result:=false;
  1023. end;
  1024. function HashSpecialChars(s: string): string;
  1025. var
  1026. i: Integer;
  1027. begin
  1028. Result:='';
  1029. for i:=1 to length(s) do
  1030. if s[i] in [#0..#31,#127..#255] then
  1031. Result:=Result+'#'+hexstr(ord(s[i]),2)
  1032. else
  1033. Result:=Result+s[i];
  1034. end;
  1035. procedure DiffFound;
  1036. var
  1037. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  1038. ExpLine, ActLine: String;
  1039. i, LineNo, DiffLineNo: Integer;
  1040. begin
  1041. writeln('Diff found "',Msg,'". Lines:');
  1042. // write correct lines
  1043. p:=PChar(Expected);
  1044. LineNo:=0;
  1045. DiffLineNo:=0;
  1046. repeat
  1047. StartPos:=p;
  1048. while not (p^ in [#0,#10,#13]) do inc(p);
  1049. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  1050. SkipLineEnd(p);
  1051. inc(LineNo);
  1052. if (p<=ExpectedP) and (p^<>#0) then
  1053. begin
  1054. writeln('= ',ExpLine);
  1055. end else begin
  1056. // diff line
  1057. if DiffLineNo=0 then DiffLineNo:=LineNo;
  1058. // write actual line
  1059. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  1060. ActLineEndP:=FindLineEnd(ActualP);
  1061. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1062. writeln('- ',ActLine);
  1063. if HasSpecialChar(ActLine) then
  1064. writeln('- ',HashSpecialChars(ActLine));
  1065. // write expected line
  1066. writeln('+ ',ExpLine);
  1067. if HasSpecialChar(ExpLine) then
  1068. writeln('- ',HashSpecialChars(ExpLine));
  1069. // write empty line with pointer ^
  1070. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  1071. writeln('^');
  1072. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  1073. CheckSrcDiff:=false;
  1074. // write up to three following actual lines to get some context
  1075. for i:=1 to 3 do begin
  1076. ActLineStartP:=ActLineEndP;
  1077. SkipLineEnd(ActLineStartP);
  1078. if ActLineStartP^=#0 then break;
  1079. ActLineEndP:=FindLineEnd(ActLineStartP);
  1080. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1081. writeln('~ ',ActLine);
  1082. end;
  1083. exit;
  1084. end;
  1085. until p^=#0;
  1086. writeln('DiffFound Actual:-----------------------');
  1087. writeln(Actual);
  1088. writeln('DiffFound Expected:---------------------');
  1089. writeln(Expected);
  1090. writeln('DiffFound ------------------------------');
  1091. Msg:='diff found, but lines are the same, internal error';
  1092. CheckSrcDiff:=false;
  1093. end;
  1094. var
  1095. IsSpaceNeeded: Boolean;
  1096. LastChar, Quote: Char;
  1097. begin
  1098. Result:=true;
  1099. Msg:='';
  1100. if Expected='' then Expected:=' ';
  1101. if Actual='' then Actual:=' ';
  1102. ExpectedP:=PChar(Expected);
  1103. ActualP:=PChar(Actual);
  1104. repeat
  1105. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  1106. case ExpectedP^ of
  1107. #0:
  1108. begin
  1109. // check that rest of Actual has only spaces
  1110. while ActualP^ in SpaceChars do inc(ActualP);
  1111. if ActualP^<>#0 then
  1112. begin
  1113. DiffFound;
  1114. exit;
  1115. end;
  1116. exit(true);
  1117. end;
  1118. ' ',#9,#10,#13:
  1119. begin
  1120. // skip space in Expected
  1121. IsSpaceNeeded:=false;
  1122. if ExpectedP>PChar(Expected) then
  1123. LastChar:=ExpectedP[-1]
  1124. else
  1125. LastChar:=#0;
  1126. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1127. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1128. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1129. IsSpaceNeeded:=true;
  1130. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1131. begin
  1132. DiffFound;
  1133. exit;
  1134. end;
  1135. while ActualP^ in SpaceChars do inc(ActualP);
  1136. end;
  1137. '''','"':
  1138. begin
  1139. while ActualP^ in SpaceChars do inc(ActualP);
  1140. if ExpectedP^<>ActualP^ then
  1141. begin
  1142. DiffFound;
  1143. exit;
  1144. end;
  1145. Quote:=ExpectedP^;
  1146. repeat
  1147. inc(ExpectedP);
  1148. inc(ActualP);
  1149. if ExpectedP^<>ActualP^ then
  1150. begin
  1151. DiffFound;
  1152. exit;
  1153. end;
  1154. if (ExpectedP^ in [#0,#10,#13]) then
  1155. break
  1156. else if (ExpectedP^=Quote) then
  1157. begin
  1158. inc(ExpectedP);
  1159. inc(ActualP);
  1160. break;
  1161. end;
  1162. until false;
  1163. end;
  1164. else
  1165. while ActualP^ in SpaceChars do inc(ActualP);
  1166. if ExpectedP^<>ActualP^ then
  1167. begin
  1168. DiffFound;
  1169. exit;
  1170. end;
  1171. inc(ExpectedP);
  1172. inc(ActualP);
  1173. end;
  1174. until false;
  1175. end;
  1176. { TTestEnginePasResolver }
  1177. procedure TTestEnginePasResolver.SetModule(const AValue: TPasModule);
  1178. begin
  1179. if FModule=AValue then Exit;
  1180. FModule:=AValue;
  1181. end;
  1182. destructor TTestEnginePasResolver.Destroy;
  1183. begin
  1184. FreeAndNil(FStreamResolver);
  1185. FreeAndNil(FParser);
  1186. FreeAndNil(FScanner);
  1187. FreeAndNil(FStreamResolver);
  1188. Module:=nil;
  1189. inherited Destroy;
  1190. end;
  1191. function TTestEnginePasResolver.CreateElement(AClass: TPTreeElement;
  1192. const AName: String; AParent: TPasElement; AVisibility: TPasMemberVisibility;
  1193. const ASrcPos: TPasSourcePos; TypeParams: TFPList): TPasElement;
  1194. begin
  1195. Result:=inherited CreateElement(AClass, AName, AParent, AVisibility, ASrcPos,
  1196. TypeParams);
  1197. if (FModule=nil) and AClass.InheritsFrom(TPasModule) then
  1198. Module:=TPasModule(Result);
  1199. end;
  1200. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1201. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1202. begin
  1203. Result:=nil;
  1204. if InFilename<>'' then
  1205. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1206. if Assigned(OnFindUnit) then
  1207. Result:=OnFindUnit(AName);
  1208. if NameExpr=nil then ;
  1209. end;
  1210. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1211. begin
  1212. // do not parse recursively
  1213. // parse via the queue
  1214. if Section=nil then ;
  1215. end;
  1216. { TCustomTestModule }
  1217. procedure TCustomTestModule.FreeSrcMarkers;
  1218. var
  1219. aMarker, Last: PSrcMarker;
  1220. begin
  1221. aMarker:=FirstSrcMarker;
  1222. while aMarker<>nil do
  1223. begin
  1224. Last:=aMarker;
  1225. aMarker:=aMarker^.Next;
  1226. Dispose(Last);
  1227. end;
  1228. FirstSrcMarker:=nil;
  1229. LastSrcMarker:=nil;
  1230. end;
  1231. function TCustomTestModule.GetResolverCount: integer;
  1232. begin
  1233. Result:=FResolvers.Count;
  1234. end;
  1235. function TCustomTestModule.GetResolvers(Index: integer): TTestEnginePasResolver;
  1236. begin
  1237. Result:=TTestEnginePasResolver(FResolvers[Index]);
  1238. end;
  1239. function TCustomTestModule.GetMsgCount: integer;
  1240. begin
  1241. Result:=FHintMsgs.Count;
  1242. end;
  1243. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1244. begin
  1245. Result:=TTestHintMessage(FHintMsgs[Index]);
  1246. end;
  1247. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1248. ): TPasModule;
  1249. var
  1250. DefNamespace: String;
  1251. begin
  1252. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1253. if (Pos('.',aUnitName)<1) then
  1254. begin
  1255. DefNamespace:=GetDefaultNamespace;
  1256. if DefNamespace<>'' then
  1257. begin
  1258. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1259. if Result<>nil then exit;
  1260. end;
  1261. end;
  1262. Result:=LoadUnit(aUnitName);
  1263. if Result<>nil then exit;
  1264. {$IFDEF VerbosePas2JS}
  1265. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1266. {$ENDIF}
  1267. Fail('can''t find unit "'+aUnitName+'"');
  1268. end;
  1269. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1270. var
  1271. aParser: TPasParser;
  1272. Item: TTestHintMessage;
  1273. begin
  1274. aParser:=Sender as TPasParser;
  1275. Item:=TTestHintMessage.Create;
  1276. Item.Id:=aParser.LastMsgNumber;
  1277. Item.MsgType:=aParser.LastMsgType;
  1278. Item.MsgNumber:=aParser.LastMsgNumber;
  1279. Item.Msg:=Msg;
  1280. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1281. {$IFDEF VerbosePas2JS}
  1282. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1283. {$ENDIF}
  1284. FHintMsgs.Add(Item);
  1285. end;
  1286. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1287. );
  1288. var
  1289. aResolver: TTestEnginePasResolver;
  1290. Item: TTestHintMessage;
  1291. begin
  1292. aResolver:=Sender as TTestEnginePasResolver;
  1293. Item:=TTestHintMessage.Create;
  1294. Item.Id:=aResolver.LastMsgId;
  1295. Item.MsgType:=aResolver.LastMsgType;
  1296. Item.MsgNumber:=aResolver.LastMsgNumber;
  1297. Item.Msg:=Msg;
  1298. Item.SourcePos:=aResolver.LastSourcePos;
  1299. {$IFDEF VerbosePas2JS}
  1300. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1301. {$ENDIF}
  1302. FHintMsgs.Add(Item);
  1303. end;
  1304. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1305. var
  1306. Item: TTestHintMessage;
  1307. aScanner: TPas2jsPasScanner;
  1308. begin
  1309. aScanner:=Sender as TPas2jsPasScanner;
  1310. Item:=TTestHintMessage.Create;
  1311. Item.Id:=aScanner.LastMsgNumber;
  1312. Item.MsgType:=aScanner.LastMsgType;
  1313. Item.MsgNumber:=aScanner.LastMsgNumber;
  1314. Item.Msg:=Msg;
  1315. Item.SourcePos:=aScanner.CurSourcePos;
  1316. {$IFDEF VerbosePas2JS}
  1317. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1318. {$ENDIF}
  1319. FHintMsgs.Add(Item);
  1320. end;
  1321. procedure TCustomTestModule.OnCheckElementParent(El: TPasElement; arg: pointer);
  1322. var
  1323. SubEl: TPasElement;
  1324. i: Integer;
  1325. procedure E(Msg: string);
  1326. var
  1327. s: String;
  1328. begin
  1329. s:='TCustomTestModule.OnCheckElementParent El='+GetTreeDbg(El)+' '+
  1330. ResolverEngine.GetElementSourcePosStr(El)+' '+Msg;
  1331. writeln('ERROR: ',s);
  1332. Fail(s);
  1333. end;
  1334. begin
  1335. if arg=nil then ;
  1336. if El=nil then exit;
  1337. if El.Parent=El then
  1338. E('El.Parent=El='+GetObjName(El));
  1339. if El is TBinaryExpr then
  1340. begin
  1341. if (TBinaryExpr(El).left<>nil) and (TBinaryExpr(El).left.Parent<>El) then
  1342. E('TBinaryExpr(El).left.Parent='+GetObjName(TBinaryExpr(El).left.Parent)+'<>El');
  1343. if (TBinaryExpr(El).right<>nil) and (TBinaryExpr(El).right.Parent<>El) then
  1344. E('TBinaryExpr(El).right.Parent='+GetObjName(TBinaryExpr(El).right.Parent)+'<>El');
  1345. end
  1346. else if El is TParamsExpr then
  1347. begin
  1348. if (TParamsExpr(El).Value<>nil) and (TParamsExpr(El).Value.Parent<>El) then
  1349. E('TParamsExpr(El).Value.Parent='+GetObjName(TParamsExpr(El).Value.Parent)+'<>El');
  1350. for i:=0 to length(TParamsExpr(El).Params)-1 do
  1351. if TParamsExpr(El).Params[i].Parent<>El then
  1352. E('TParamsExpr(El).Params[i].Parent='+GetObjName(TParamsExpr(El).Params[i].Parent)+'<>El');
  1353. end
  1354. else if El is TProcedureExpr then
  1355. begin
  1356. if (TProcedureExpr(El).Proc<>nil) and (TProcedureExpr(El).Proc.Parent<>El) then
  1357. E('TProcedureExpr(El).Proc.Parent='+GetObjName(TProcedureExpr(El).Proc.Parent)+'<>El');
  1358. end
  1359. else if El is TPasDeclarations then
  1360. begin
  1361. for i:=0 to TPasDeclarations(El).Declarations.Count-1 do
  1362. begin
  1363. SubEl:=TPasElement(TPasDeclarations(El).Declarations[i]);
  1364. if SubEl.Parent<>El then
  1365. E('SubEl=TPasElement(TPasDeclarations(El).Declarations[i])='+GetObjName(SubEl)+' SubEl.Parent='+GetObjName(SubEl.Parent)+'<>El');
  1366. end;
  1367. end
  1368. else if El is TPasImplBlock then
  1369. begin
  1370. for i:=0 to TPasImplBlock(El).Elements.Count-1 do
  1371. begin
  1372. SubEl:=TPasElement(TPasImplBlock(El).Elements[i]);
  1373. if SubEl.Parent<>El then
  1374. E('TPasElement(TPasImplBlock(El).Elements[i]).Parent='+GetObjName(SubEl.Parent)+'<>El');
  1375. end;
  1376. end
  1377. else if El is TPasImplWithDo then
  1378. begin
  1379. for i:=0 to TPasImplWithDo(El).Expressions.Count-1 do
  1380. begin
  1381. SubEl:=TPasExpr(TPasImplWithDo(El).Expressions[i]);
  1382. if SubEl.Parent<>El then
  1383. E('TPasExpr(TPasImplWithDo(El).Expressions[i]).Parent='+GetObjName(SubEl.Parent)+'<>El');
  1384. end;
  1385. end
  1386. else if El is TPasProcedure then
  1387. begin
  1388. if TPasProcedure(El).ProcType.Parent<>El then
  1389. E('TPasProcedure(El).ProcType.Parent='+GetObjName(TPasProcedure(El).ProcType.Parent)+'<>El');
  1390. end
  1391. else if El is TPasProcedureType then
  1392. begin
  1393. for i:=0 to TPasProcedureType(El).Args.Count-1 do
  1394. if TPasArgument(TPasProcedureType(El).Args[i]).Parent<>El then
  1395. E('TPasArgument(TPasProcedureType(El).Args[i]).Parent='+GetObjName(TPasArgument(TPasProcedureType(El).Args[i]).Parent)+'<>El');
  1396. end;
  1397. end;
  1398. procedure TCustomTestModule.OnFindReference(El: TPasElement; FindData: pointer);
  1399. var
  1400. Data: PTestResolverReferenceData absolute FindData;
  1401. Line, Col: integer;
  1402. begin
  1403. ResolverEngine.UnmangleSourceLineNumber(El.SourceLinenumber,Line,Col);
  1404. //writeln('TCustomTestModule.OnFindReference ',El.SourceFilename,' Line=',Line,',Col=',Col,' ',GetObjName(El),' SearchFile=',Data^.Filename,',Line=',Data^.Row,',Col=',Data^.StartCol,'-',Data^.EndCol);
  1405. if (Data^.Filename=El.SourceFilename)
  1406. and (Data^.Row=Line)
  1407. and (Data^.StartCol<=Col)
  1408. and (Data^.EndCol>=Col)
  1409. then
  1410. Data^.Found.Add(El);
  1411. end;
  1412. procedure TCustomTestModule.SetWithTypeInfo(const AValue: boolean);
  1413. begin
  1414. if FWithTypeInfo=AValue then Exit;
  1415. FWithTypeInfo:=AValue;
  1416. if AValue then
  1417. Converter.Options:=Converter.Options-[coNoTypeInfo]
  1418. else
  1419. Converter.Options:=Converter.Options+[coNoTypeInfo];
  1420. end;
  1421. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1422. var
  1423. i: Integer;
  1424. CurEngine: TTestEnginePasResolver;
  1425. CurUnitName: String;
  1426. begin
  1427. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1428. Result:=nil;
  1429. if (Module.ClassType=TPasModule)
  1430. and (CompareText(Module.Name,aUnitName)=0) then
  1431. exit(Module);
  1432. for i:=0 to ResolverCount-1 do
  1433. begin
  1434. CurEngine:=Resolvers[i];
  1435. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1436. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1437. if CompareText(aUnitName,CurUnitName)=0 then
  1438. begin
  1439. Result:=CurEngine.Module;
  1440. if Result<>nil then exit;
  1441. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1442. FileResolver.FindSourceFile(aUnitName);
  1443. CurEngine.StreamResolver:=TStreamResolver.Create;
  1444. CurEngine.StreamResolver.OwnsStreams:=True;
  1445. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1446. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1447. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1448. InitScanner(CurEngine.Scanner);
  1449. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1450. CurEngine.Parser.Options:=po_tcmodules;
  1451. if CompareText(CurUnitName,'System')=0 then
  1452. CurEngine.Parser.ImplicitUses.Clear;
  1453. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1454. try
  1455. CurEngine.Parser.NextToken;
  1456. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1457. except
  1458. on E: Exception do
  1459. HandleException(E);
  1460. end;
  1461. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1462. Result:=CurEngine.Module;
  1463. exit;
  1464. end;
  1465. end;
  1466. end;
  1467. procedure TCustomTestModule.SetUp;
  1468. begin
  1469. {$IFDEF EnablePasTreeGlobalRefCount}
  1470. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1471. {$ENDIF}
  1472. if FResolvers<>nil then
  1473. begin
  1474. writeln('TCustomTestModule.SetUp FModules<>nil');
  1475. Halt;
  1476. end;
  1477. inherited SetUp;
  1478. FSkipTests:=false;
  1479. FWithTypeInfo:=false;
  1480. FSource:=TStringList.Create;
  1481. FHub:=TPas2JSResolverHub.Create(Self);
  1482. FResolvers:=TObjectList.Create(true);
  1483. FFilename:='test1.pp';
  1484. FFileResolver:=TStreamResolver.Create;
  1485. FFileResolver.OwnsStreams:=True;
  1486. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1487. InitScanner(FScanner);
  1488. FEngine:=AddModule(Filename);
  1489. FEngine.Scanner:=FScanner;
  1490. FScanner.Resolver:=FEngine;
  1491. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1492. FParser.OnLog:=@OnParserLog;
  1493. FEngine.Parser:=FParser;
  1494. Parser.Options:=po_tcmodules;
  1495. FModule:=Nil;
  1496. FConverter:=CreateConverter;
  1497. FExpectedErrorClass:=nil;
  1498. end;
  1499. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1500. var
  1501. Options: TPasToJsConverterOptions;
  1502. begin
  1503. Result:=TPasToJSConverter.Create;
  1504. Options:=co_tcmodules;
  1505. if WithTypeInfo then
  1506. Exclude(Options,coNoTypeInfo)
  1507. else
  1508. Include(Options,coNoTypeInfo);
  1509. Result.Options:=Options;
  1510. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1511. end;
  1512. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1513. begin
  1514. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1515. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1516. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1517. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1518. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1519. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1520. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1521. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1522. aScanner.OnLog:=@OnScannerLog;
  1523. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1524. end;
  1525. procedure TCustomTestModule.TearDown;
  1526. {$IFDEF CheckPasTreeRefCount}
  1527. var
  1528. El: TPasElement;
  1529. {$ENDIF}
  1530. var
  1531. i: Integer;
  1532. CurModule: TPasModule;
  1533. begin
  1534. FreeSrcMarkers;
  1535. FHintMsgs.Clear;
  1536. FHintMsgsGood.Clear;
  1537. FSkipTests:=false;
  1538. FWithTypeInfo:=false;
  1539. FJSRegModuleCall:=nil;
  1540. FJSModuleCallArgs:=nil;
  1541. FJSImplentationUses:=nil;
  1542. FJSInterfaceUses:=nil;
  1543. FJSModuleSrc:=nil;
  1544. FJSInitBody:=nil;
  1545. FreeAndNil(FJSSource);
  1546. FreeAndNil(FJSModule);
  1547. FreeAndNil(FConverter);
  1548. ResolverEngine.Clear;
  1549. FreeAndNil(FSource);
  1550. FreeAndNil(FFileResolver);
  1551. if FResolvers<>nil then
  1552. begin
  1553. for i:=0 to FResolvers.Count-1 do
  1554. begin
  1555. CurModule:=TTestEnginePasResolver(FResolvers[i]).Module;
  1556. if CurModule=nil then continue;
  1557. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1558. end;
  1559. for i:=0 to FResolvers.Count-1 do
  1560. begin
  1561. CurModule:=TTestEnginePasResolver(FResolvers[i]).Module;
  1562. if CurModule=nil then continue;
  1563. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1564. end;
  1565. FreeAndNil(FResolvers);
  1566. FModule:=nil;
  1567. FEngine:=nil;
  1568. end;
  1569. FreeAndNil(FHub);
  1570. inherited TearDown;
  1571. {$IFDEF EnablePasTreeGlobalRefCount}
  1572. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1573. begin
  1574. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1575. {$IFDEF CheckPasTreeRefCount}
  1576. El:=TPasElement.FirstRefEl;
  1577. while El<>nil do
  1578. begin
  1579. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1580. for i:=0 to El.RefIds.Count-1 do
  1581. writeln(' ',El.RefIds[i]);
  1582. El:=El.NextRefEl;
  1583. end;
  1584. {$ENDIF}
  1585. Halt;
  1586. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1587. end;
  1588. {$ENDIF}
  1589. end;
  1590. procedure TCustomTestModule.Add(Line: string);
  1591. begin
  1592. Source.Add(Line);
  1593. end;
  1594. procedure TCustomTestModule.Add(const Lines: array of string);
  1595. var
  1596. i: Integer;
  1597. begin
  1598. for i:=low(Lines) to high(Lines) do
  1599. Add(Lines[i]);
  1600. end;
  1601. procedure TCustomTestModule.StartParsing;
  1602. var
  1603. Src: String;
  1604. begin
  1605. Src:=Source.Text;
  1606. FEngine.Source:=Src;
  1607. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1608. Scanner.OpenFile(FileName);
  1609. Writeln('// Test : ',Self.TestName);
  1610. Writeln(Src);
  1611. end;
  1612. procedure TCustomTestModule.ParseModuleQueue;
  1613. var
  1614. i: Integer;
  1615. CurResolver: TTestEnginePasResolver;
  1616. Found: Boolean;
  1617. Section: TPasSection;
  1618. begin
  1619. // parse til exception or all Resolvers finished
  1620. while not SkipTests do
  1621. begin
  1622. Found:=false;
  1623. for i:=0 to ResolverCount-1 do
  1624. begin
  1625. CurResolver:=Resolvers[i];
  1626. if CurResolver.CurrentParser=nil then continue;
  1627. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1628. continue;
  1629. CurResolver.Parser.ParseContinue;
  1630. Found:=true;
  1631. break;
  1632. end;
  1633. if not Found then break;
  1634. end;
  1635. for i:=0 to ResolverCount-1 do
  1636. begin
  1637. CurResolver:=Resolvers[i];
  1638. if CurResolver.Parser=nil then
  1639. begin
  1640. if CurResolver.CurrentParser<>nil then
  1641. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1642. continue;
  1643. end;
  1644. if CurResolver.Parser.CurModule<>nil then
  1645. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1646. end;
  1647. end;
  1648. procedure TCustomTestModule.ParseModule;
  1649. begin
  1650. if SkipTests then exit;
  1651. FFirstPasStatement:=nil;
  1652. try
  1653. StartParsing;
  1654. Parser.ParseMain(FModule);
  1655. ParseModuleQueue;
  1656. except
  1657. on E: Exception do
  1658. HandleException(E);
  1659. end;
  1660. if SkipTests then exit;
  1661. AssertNotNull('Module resulted in Module',Module);
  1662. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1663. TAssert.AssertSame('Has resolver',ResolverEngine,Parser.Engine);
  1664. end;
  1665. procedure TCustomTestModule.ParseProgram;
  1666. begin
  1667. if SkipTests then exit;
  1668. ParseModule;
  1669. if SkipTests then exit;
  1670. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1671. FPasProgram:=TPasProgram(Module);
  1672. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1673. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1674. if (PasProgram.InitializationSection.Elements.Count>0) then
  1675. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1676. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1677. end;
  1678. procedure TCustomTestModule.ParseLibrary;
  1679. var
  1680. Init: TInitializationSection;
  1681. begin
  1682. if SkipTests then exit;
  1683. ParseModule;
  1684. if SkipTests then exit;
  1685. AssertEquals('Has library',TPasLibrary,Module.ClassType);
  1686. FPasLibrary:=TPasLibrary(Module);
  1687. AssertNotNull('Has library section',PasLibrary.LibrarySection);
  1688. Init:=PasLibrary.InitializationSection;
  1689. if (Init<>nil) and (Init.Elements.Count>0) then
  1690. if TObject(Init.Elements[0]) is TPasImplBlock then
  1691. FFirstPasStatement:=TPasImplBlock(PasLibrary.InitializationSection.Elements[0]);
  1692. end;
  1693. procedure TCustomTestModule.ParseUnit;
  1694. begin
  1695. if SkipTests then exit;
  1696. ParseModule;
  1697. if SkipTests then exit;
  1698. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1699. AssertNotNull('Has interface section',Module.InterfaceSection);
  1700. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1701. if (Module.InitializationSection<>nil)
  1702. and (Module.InitializationSection.Elements.Count>0)
  1703. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1704. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1705. end;
  1706. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1707. ): TTestEnginePasResolver;
  1708. var
  1709. i: Integer;
  1710. begin
  1711. for i:=0 to ResolverCount-1 do
  1712. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1713. exit(Resolvers[i]);
  1714. Result:=nil;
  1715. end;
  1716. function TCustomTestModule.AddModule(aFilename: string
  1717. ): TTestEnginePasResolver;
  1718. begin
  1719. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1720. if FindModuleWithFilename(aFilename)<>nil then
  1721. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1722. Result:=TTestEnginePasResolver.Create;
  1723. Result.Filename:=aFilename;
  1724. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1725. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1726. Result.OnLog:=@OnPasResolverLog;
  1727. Result.Hub:=Hub;
  1728. FResolvers.Add(Result);
  1729. end;
  1730. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1731. ): TTestEnginePasResolver;
  1732. begin
  1733. Result:=AddModule(aFilename);
  1734. Result.Source:=Src;
  1735. end;
  1736. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1737. ImplementationSrc: string): TTestEnginePasResolver;
  1738. var
  1739. Src: String;
  1740. begin
  1741. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1742. Src+=LineEnding;
  1743. Src+='interface'+LineEnding;
  1744. Src+=LineEnding;
  1745. Src+=InterfaceSrc;
  1746. Src+='implementation'+LineEnding;
  1747. Src+=LineEnding;
  1748. Src+=ImplementationSrc;
  1749. Src+='end.'+LineEnding;
  1750. Result:=AddModuleWithSrc(aFilename,Src);
  1751. end;
  1752. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1753. var
  1754. Intf, Impl: TStringList;
  1755. begin
  1756. Intf:=TStringList.Create;
  1757. if supTInterfacedObject in Parts then Include(Parts,supTObject);
  1758. // unit interface
  1759. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1760. Intf.Add('{$modeswitch externalclass}');
  1761. Intf.Add('type');
  1762. Intf.Add(' integer=longint;');
  1763. Intf.Add(' sizeint=nativeint;');
  1764. //'const',
  1765. //' LineEnding = #10;',
  1766. //' DirectorySeparator = ''/'';',
  1767. //' DriveSeparator = '''';',
  1768. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1769. //' AllowDriveSeparators : set of char = [];',
  1770. if supTObject in Parts then
  1771. Intf.AddStrings([
  1772. 'type',
  1773. ' TClass = class of TObject;',
  1774. ' TObject = class',
  1775. ' constructor Create;',
  1776. ' destructor Destroy; virtual;',
  1777. ' class function ClassType: TClass; assembler;',
  1778. ' class function ClassName: String; assembler;',
  1779. ' class function ClassNameIs(const Name: string): boolean;',
  1780. ' class function ClassParent: TClass; assembler;',
  1781. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1782. ' class function UnitName: String; assembler;',
  1783. ' procedure AfterConstruction; virtual;',
  1784. ' procedure BeforeDestruction;virtual;',
  1785. ' function Equals(Obj: TObject): boolean; virtual;',
  1786. ' function ToString: String; virtual;',
  1787. ' end;']);
  1788. if supTInterfacedObject in Parts then
  1789. Intf.AddStrings([
  1790. ' {$Interfaces COM}',
  1791. ' IUnknown = interface',
  1792. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1793. //' function QueryInterface(const iid: TGuid; out obj): Integer;',
  1794. ' function _AddRef: Integer;',
  1795. ' function _Release: Integer;',
  1796. ' end;',
  1797. ' IInterface = IUnknown;',
  1798. ' TInterfacedObject = class(TObject,IUnknown)',
  1799. ' protected',
  1800. ' fRefCount: Integer;',
  1801. ' { implement methods of IUnknown }',
  1802. //' function QueryInterface(const iid: TGuid; out obj): Integer; virtual;',
  1803. ' function _AddRef: Integer; virtual;',
  1804. ' function _Release: Integer; virtual;',
  1805. ' end;',
  1806. ' TInterfacedClass = class of TInterfacedObject;',
  1807. '',
  1808. '']);
  1809. if supTVarRec in Parts then
  1810. Intf.AddStrings([
  1811. 'const',
  1812. ' vtInteger = 0;',
  1813. ' vtBoolean = 1;',
  1814. ' vtJSValue = 19;',
  1815. 'type',
  1816. ' PVarRec = ^TVarRec;',
  1817. ' TVarRec = record',
  1818. ' VType : byte;',
  1819. ' VJSValue: JSValue;',
  1820. ' vInteger: longint external name ''VJSValue'';',
  1821. ' vBoolean: boolean external name ''VJSValue'';',
  1822. ' end;',
  1823. ' TVarRecArray = array of TVarRec;',
  1824. 'function VarRecs: TVarRecArray; varargs;',
  1825. '']);
  1826. if supTypeInfo in Parts then
  1827. begin
  1828. Intf.AddStrings([
  1829. 'type',
  1830. ' TTypeKind = (',
  1831. ' tkUnknown, // 0',
  1832. ' tkInteger, // 1',
  1833. ' tkChar, // 2 in Delphi/FPC tkWChar, tkUChar',
  1834. ' tkString, // 3 in Delphi/FPC tkSString, tkWString or tkUString',
  1835. ' tkEnumeration, // 4',
  1836. ' tkSet, // 5',
  1837. ' tkDouble, // 6',
  1838. ' tkBool, // 7',
  1839. ' tkProcVar, // 8 function or procedure',
  1840. ' tkMethod, // 9 proc var of object',
  1841. ' tkArray, // 10 static array',
  1842. ' tkDynArray, // 11',
  1843. ' tkRecord, // 12',
  1844. ' tkClass, // 13',
  1845. ' tkClassRef, // 14',
  1846. ' tkPointer, // 15',
  1847. ' tkJSValue, // 16',
  1848. ' tkRefToProcVar, // 17 variable of procedure type',
  1849. ' tkInterface, // 18',
  1850. ' //tkObject,',
  1851. ' //tkSString,tkLString,tkAString,tkWString,',
  1852. ' //tkVariant,',
  1853. ' //tkWChar,',
  1854. ' //tkInt64,',
  1855. ' //tkQWord,',
  1856. ' //tkInterfaceRaw,',
  1857. ' //tkUString,tkUChar,',
  1858. ' tkHelper, // 19',
  1859. ' //tkFile,',
  1860. ' tkExtClass // 20',
  1861. ' );',
  1862. ' TTypeKinds = set of TTypeKind;',
  1863. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1864. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1865. ' end;',
  1866. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1867. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1868. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1869. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1870. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1871. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1872. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1873. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1874. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1875. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1876. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1877. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1878. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1879. '']);
  1880. end;
  1881. if supWriteln in Parts then
  1882. Intf.Add('procedure writeln; varargs; external name ''console.log'';');
  1883. Intf.Add('var');
  1884. Intf.Add(' ExitCode: Longint = 0;');
  1885. // unit implementation
  1886. Impl:=TStringList.Create;
  1887. if supTObject in Parts then
  1888. Impl.AddStrings([
  1889. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1890. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1891. 'asm',
  1892. 'end;',
  1893. 'constructor TObject.Create; begin end;',
  1894. 'destructor TObject.Destroy; begin end;',
  1895. 'class function TObject.ClassType: TClass; assembler;',
  1896. 'asm',
  1897. 'end;',
  1898. 'class function TObject.ClassName: String; assembler;',
  1899. 'asm',
  1900. 'end;',
  1901. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1902. 'begin',
  1903. ' Result:=SameText(Name,ClassName);',
  1904. 'end;',
  1905. 'class function TObject.ClassParent: TClass; assembler;',
  1906. 'asm',
  1907. 'end;',
  1908. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1909. 'asm',
  1910. 'end;',
  1911. 'class function TObject.UnitName: String; assembler;',
  1912. 'asm',
  1913. 'end;',
  1914. 'procedure TObject.AfterConstruction; begin end;',
  1915. 'procedure TObject.BeforeDestruction; begin end;',
  1916. 'function TObject.Equals(Obj: TObject): boolean;',
  1917. 'begin',
  1918. ' Result:=Obj=Self;',
  1919. 'end;',
  1920. 'function TObject.ToString: String;',
  1921. 'begin',
  1922. ' Result:=ClassName;',
  1923. 'end;'
  1924. ]);
  1925. if supTInterfacedObject in Parts then
  1926. Impl.AddStrings([
  1927. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1928. //'begin',
  1929. //'end;',
  1930. 'function TInterfacedObject._AddRef: Integer;',
  1931. 'begin',
  1932. 'end;',
  1933. 'function TInterfacedObject._Release: Integer;',
  1934. 'begin',
  1935. 'end;',
  1936. '']);
  1937. if supTVarRec in Parts then
  1938. Impl.AddStrings([
  1939. 'function VarRecs: TVarRecArray; varargs;',
  1940. 'var',
  1941. ' v: PVarRec;',
  1942. 'begin',
  1943. ' v^.VType:=1;',
  1944. ' v^.VJSValue:=2;',
  1945. 'end;',
  1946. '']);
  1947. try
  1948. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1949. finally
  1950. Intf.Free;
  1951. Impl.Free;
  1952. end;
  1953. end;
  1954. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1955. SystemUnitParts: TSystemUnitParts);
  1956. begin
  1957. if NeedSystemUnit then
  1958. AddSystemUnit(SystemUnitParts)
  1959. else
  1960. Parser.ImplicitUses.Clear;
  1961. Add('program '+ExtractFileUnitName(Filename)+';');
  1962. Add('');
  1963. end;
  1964. procedure TCustomTestModule.StartLibrary(NeedSystemUnit: boolean;
  1965. SystemUnitParts: TSystemUnitParts);
  1966. begin
  1967. if NeedSystemUnit then
  1968. AddSystemUnit(SystemUnitParts)
  1969. else
  1970. Parser.ImplicitUses.Clear;
  1971. Add('library '+ExtractFileUnitName(Filename)+';');
  1972. Add('');
  1973. end;
  1974. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1975. SystemUnitParts: TSystemUnitParts);
  1976. begin
  1977. if NeedSystemUnit then
  1978. AddSystemUnit(SystemUnitParts)
  1979. else
  1980. Parser.ImplicitUses.Clear;
  1981. Add('unit Test1;');
  1982. Add('');
  1983. end;
  1984. procedure TCustomTestModule.ConvertModule;
  1985. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1986. out UsesLit: TJSArrayLiteral);
  1987. var
  1988. i: Integer;
  1989. Item: TJSElement;
  1990. Lit: TJSLiteral;
  1991. begin
  1992. UsesLit:=nil;
  1993. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1994. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1995. exit; // null is ok
  1996. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1997. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1998. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1999. begin
  2000. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  2001. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  2002. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  2003. Lit:=TJSLiteral(Item);
  2004. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  2005. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  2006. end;
  2007. end;
  2008. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  2009. out Src: TJSSourceElements);
  2010. var
  2011. FunDecl: TJSFunctionDeclarationStatement;
  2012. FunDef: TJSFuncDef;
  2013. FunBody: TJSFunctionBody;
  2014. begin
  2015. Src:=nil;
  2016. AssertNotNull(ParamName,Arg.Expr);
  2017. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  2018. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  2019. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  2020. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  2021. FunDef:=FunDecl.AFunction as TJSFuncDef;
  2022. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  2023. AssertNotNull(ParamName+' body',FunDef.Body);
  2024. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  2025. FunBody:=FunDef.Body as TJSFunctionBody;
  2026. AssertNotNull(ParamName+' body.A',FunBody.A);
  2027. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  2028. Src:=FunBody.A as TJSSourceElements;
  2029. end;
  2030. var
  2031. ModuleNameExpr: TJSLiteral;
  2032. InitFunction: TJSFunctionDeclarationStatement;
  2033. InitAssign: TJSSimpleAssignStatement;
  2034. InitName: String;
  2035. LastNode, FirstNode: TJSElement;
  2036. Arg: TJSArrayLiteralElement;
  2037. IsProg, IsLib: Boolean;
  2038. begin
  2039. if SkipTests then exit;
  2040. IsProg:=false;
  2041. IsLib:=false;
  2042. if Module is TPasProgram then
  2043. IsProg:=true
  2044. else if Module is TPasLibrary then
  2045. IsLib:=true;
  2046. try
  2047. FJSModule:=FConverter.ConvertPasElement(Module,ResolverEngine) as TJSSourceElements;
  2048. except
  2049. on E: Exception do
  2050. HandleException(E);
  2051. end;
  2052. if SkipTests then exit;
  2053. if ExpectedErrorClass<>nil then
  2054. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  2055. FJSSource:=TStringList.Create;
  2056. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  2057. {$IFDEF VerbosePas2JS}
  2058. writeln('TTestModule.ConvertModule JS:');
  2059. write(FJSSource.Text);
  2060. {$ENDIF}
  2061. // rtl.module(...
  2062. if JSModule.Statements.Count<1 then
  2063. AssertEquals('jsmodule has at least one statement - the call',1,JSModule.Statements.Count);
  2064. FirstNode:=JSModule.Statements.Nodes[0].Node;
  2065. AssertNotNull('register module call',FirstNode);
  2066. AssertEquals('register module call',TJSCallExpression,FirstNode.ClassType);
  2067. FJSRegModuleCall:=FirstNode as TJSCallExpression;
  2068. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  2069. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  2070. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  2071. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  2072. // parameter 'unitname'
  2073. if JSModuleCallArgs.Elements.Count<1 then
  2074. Fail('rtl.module first param unit missing');
  2075. Arg:=JSModuleCallArgs.Elements.Elements[0];
  2076. AssertNotNull('module name param',Arg.Expr);
  2077. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  2078. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  2079. if IsProg then
  2080. begin
  2081. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString));
  2082. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  2083. end
  2084. else if IsLib then
  2085. AssertEquals('module name','library',String(ModuleNameExpr.Value.AsString))
  2086. else
  2087. begin
  2088. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  2089. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  2090. end;
  2091. // main uses section
  2092. if JSModuleCallArgs.Elements.Count<2 then
  2093. Fail('rtl.module second param main uses missing');
  2094. Arg:=JSModuleCallArgs.Elements.Elements[1];
  2095. CheckUsesList('interface',Arg,FJSInterfaceUses);
  2096. // program/library/interface function()
  2097. if JSModuleCallArgs.Elements.Count<3 then
  2098. Fail('rtl.module third param intf-function missing');
  2099. Arg:=JSModuleCallArgs.Elements.Elements[2];
  2100. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  2101. // search for $mod.$init or $mod.$main - the last statement
  2102. if IsProg or IsLib then
  2103. begin
  2104. InitName:='$main';
  2105. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  2106. end
  2107. else
  2108. InitName:='$init';
  2109. InitAssign:=nil;
  2110. InitFunction:=nil;
  2111. FJSInitBody:=nil;
  2112. if JSModuleSrc.Statements.Count>0 then
  2113. begin
  2114. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  2115. if LastNode is TJSSimpleAssignStatement then
  2116. begin
  2117. InitAssign:=LastNode as TJSSimpleAssignStatement;
  2118. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  2119. begin
  2120. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  2121. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  2122. end
  2123. else if IsProg or IsLib then
  2124. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  2125. end;
  2126. end;
  2127. // optional: implementation uses section
  2128. if JSModuleCallArgs.Elements.Count<4 then
  2129. exit;
  2130. Arg:=JSModuleCallArgs.Elements.Elements[3];
  2131. CheckUsesList('implementation',Arg,FJSImplentationUses);
  2132. end;
  2133. procedure TCustomTestModule.ConvertProgram;
  2134. begin
  2135. Add('end.');
  2136. ParseProgram;
  2137. ConvertModule;
  2138. end;
  2139. procedure TCustomTestModule.ConvertLibrary;
  2140. begin
  2141. Add('end.');
  2142. ParseLibrary;
  2143. ConvertModule;
  2144. end;
  2145. procedure TCustomTestModule.ConvertUnit;
  2146. begin
  2147. Add('end.');
  2148. ParseUnit;
  2149. ConvertModule;
  2150. end;
  2151. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  2152. begin
  2153. Result:=tcmodules.JSToStr(El);
  2154. end;
  2155. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  2156. DottedName: string);
  2157. begin
  2158. if DottedName='' then
  2159. begin
  2160. AssertNull(Msg,El);
  2161. end
  2162. else
  2163. begin
  2164. AssertNotNull(Msg,El);
  2165. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  2166. end;
  2167. end;
  2168. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  2169. begin
  2170. if El=nil then
  2171. Result:=''
  2172. else if El is TJSPrimaryExpressionIdent then
  2173. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  2174. else if El is TJSDotMemberExpression then
  2175. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  2176. else
  2177. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  2178. end;
  2179. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  2180. InitStatements: string; ImplStatements: string);
  2181. var
  2182. ActualSrc, ExpectedSrc, InitName: String;
  2183. IsProg, IsLib: Boolean;
  2184. begin
  2185. ActualSrc:=JSToStr(JSModuleSrc);
  2186. if coUseStrict in Converter.Options then
  2187. ExpectedSrc:='"use strict";'+LineEnding
  2188. else
  2189. ExpectedSrc:='';
  2190. ExpectedSrc:=ExpectedSrc+'var $mod = this;'+LineEnding;
  2191. ExpectedSrc:=ExpectedSrc+Statements;
  2192. // unit implementation
  2193. if (Trim(ImplStatements)<>'') then
  2194. ExpectedSrc:=ExpectedSrc+LineEnding
  2195. +'$mod.$implcode = function () {'+LineEnding
  2196. +ImplStatements
  2197. +'};'+LineEnding;
  2198. // program main or unit initialization
  2199. IsProg:=false;
  2200. IsLib:=false;
  2201. if Module is TPasProgram then
  2202. IsProg:=true
  2203. else if Module is TPasLibrary then
  2204. IsLib:=true;
  2205. if IsProg or IsLib or (Trim(InitStatements)<>'') then
  2206. begin
  2207. if IsProg or IsLib then
  2208. InitName:='$main'
  2209. else
  2210. InitName:='$init';
  2211. ExpectedSrc:=ExpectedSrc+LineEnding
  2212. +'$mod.'+InitName+' = function () {'+LineEnding
  2213. +InitStatements
  2214. +'};'+LineEnding;
  2215. end;
  2216. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  2217. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  2218. //writeln('TCustomTestModule.CheckSource ',ActualSrc);
  2219. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2220. end;
  2221. procedure TCustomTestModule.CheckFullSource(Msg, ExpectedSrc: String);
  2222. var
  2223. ActualSrc: String;
  2224. begin
  2225. ActualSrc:=JSToStr(JSModule);
  2226. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2227. end;
  2228. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  2229. // search diff, ignore changes in spaces
  2230. var
  2231. s: string;
  2232. begin
  2233. if CheckSrcDiff(Expected,Actual,s) then exit;
  2234. Fail(Msg+': '+s);
  2235. end;
  2236. procedure TCustomTestModule.CheckUnit(aFilename, ExpectedSrc: string);
  2237. var
  2238. aResolver: TTestEnginePasResolver;
  2239. aConverter: TPasToJSConverter;
  2240. aJSModule: TJSSourceElements;
  2241. ActualSrc: String;
  2242. begin
  2243. aResolver:=GetResolver(aFilename);
  2244. AssertNotNull('missing resolver of unit '+aFilename,aResolver);
  2245. AssertNotNull('missing resolver.module of unit '+aFilename,aResolver.Module);
  2246. {$IFDEF VerbosePas2JS}
  2247. writeln('CheckUnit '+aFilename+' converting ...');
  2248. {$ENDIF}
  2249. aConverter:=CreateConverter;
  2250. aJSModule:=nil;
  2251. try
  2252. try
  2253. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  2254. except
  2255. on E: Exception do
  2256. HandleException(E);
  2257. end;
  2258. ActualSrc:=ConvertJSModuleToString(aJSModule);
  2259. {$IFDEF VerbosePas2JS}
  2260. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  2261. write(aResolver.Source);
  2262. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  2263. write(ActualSrc);
  2264. {$ENDIF}
  2265. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  2266. finally
  2267. aJSModule.Free;
  2268. aConverter.Free;
  2269. end;
  2270. end;
  2271. procedure TCustomTestModule.CheckReferenceDirectives;
  2272. var
  2273. CurFilename: string;
  2274. LineNumber: Integer;
  2275. SrcLine: String;
  2276. CommentStartP, CommentEndP: PChar;
  2277. procedure RaiseError(Msg: string; p: PChar);
  2278. begin
  2279. RaiseErrorAtSrc(Msg,CurFilename,LineNumber,p-PChar(SrcLine)+1);
  2280. end;
  2281. procedure AddMarker(Marker: PSrcMarker);
  2282. begin
  2283. if LastSrcMarker<>nil then
  2284. LastSrcMarker^.Next:=Marker
  2285. else
  2286. FirstSrcMarker:=Marker;
  2287. LastSrcMarker:=Marker;
  2288. end;
  2289. function AddMarker(Kind: TSrcMarkerKind; const aFilename: string;
  2290. aLine, aStartCol, aEndCol: integer; const Identifier: string): PSrcMarker;
  2291. begin
  2292. New(Result);
  2293. Result^.Kind:=Kind;
  2294. Result^.Filename:=aFilename;
  2295. Result^.Row:=aLine;
  2296. Result^.StartCol:=aStartCol;
  2297. Result^.EndCol:=aEndCol;
  2298. Result^.Identifier:=Identifier;
  2299. Result^.Next:=nil;
  2300. //writeln('AddMarker Line="',SrcLine,'" Identifier=',Identifier,' Col=',aStartCol,'-',aEndCol,' "',copy(SrcLine,aStartCol,aEndCol-aStartCol),'"');
  2301. AddMarker(Result);
  2302. end;
  2303. function AddMarkerForTokenBehindComment(Kind: TSrcMarkerKind;
  2304. const Identifier: string): PSrcMarker;
  2305. var
  2306. TokenStart, p: PChar;
  2307. begin
  2308. p:=CommentEndP;
  2309. ReadNextPascalToken(p,TokenStart,false,false);
  2310. Result:=AddMarker(Kind,CurFilename,LineNumber,
  2311. CommentEndP-PChar(SrcLine)+1,p-PChar(SrcLine)+1,Identifier);
  2312. end;
  2313. function ReadIdentifier(var p: PChar): string;
  2314. var
  2315. StartP: PChar;
  2316. begin
  2317. if not (p^ in ['a'..'z','A'..'Z','_']) then
  2318. RaiseError('identifier expected',p);
  2319. StartP:=p;
  2320. inc(p);
  2321. while p^ in ['a'..'z','A'..'Z','_','0'..'9'] do inc(p);
  2322. Result:='';
  2323. SetLength(Result,p-StartP);
  2324. Move(StartP^,Result[1],length(Result));
  2325. end;
  2326. procedure AddLabel;
  2327. var
  2328. Identifier: String;
  2329. p: PChar;
  2330. begin
  2331. p:=CommentStartP+2;
  2332. Identifier:=ReadIdentifier(p);
  2333. //writeln('TCustomTestModule.CheckReferenceDirectives.AddLabel ',Identifier);
  2334. if FindSrcLabel(Identifier)<>nil then
  2335. RaiseError('duplicate label "'+Identifier+'"',p);
  2336. AddMarkerForTokenBehindComment(mkLabel,Identifier);
  2337. end;
  2338. procedure AddResolverReference;
  2339. var
  2340. Identifier: String;
  2341. p: PChar;
  2342. begin
  2343. p:=CommentStartP+2;
  2344. Identifier:=ReadIdentifier(p);
  2345. //writeln('TCustomTestModule.CheckReferenceDirectives.AddReference ',Identifier);
  2346. AddMarkerForTokenBehindComment(mkResolverReference,Identifier);
  2347. end;
  2348. procedure AddDirectReference;
  2349. var
  2350. Identifier: String;
  2351. p: PChar;
  2352. begin
  2353. p:=CommentStartP+2;
  2354. Identifier:=ReadIdentifier(p);
  2355. //writeln('TCustomTestModule.CheckReferenceDirectives.AddDirectReference ',Identifier);
  2356. AddMarkerForTokenBehindComment(mkDirectReference,Identifier);
  2357. end;
  2358. procedure ParseCode(SrcLines: TStringList; aFilename: string);
  2359. var
  2360. p: PChar;
  2361. IsDirective: Boolean;
  2362. begin
  2363. //writeln('TCustomTestModule.CheckReferenceDirectives.ParseCode File=',aFilename);
  2364. CurFilename:=aFilename;
  2365. // parse code, find all labels
  2366. LineNumber:=0;
  2367. while LineNumber<SrcLines.Count do
  2368. begin
  2369. inc(LineNumber);
  2370. SrcLine:=SrcLines[LineNumber-1];
  2371. if SrcLine='' then continue;
  2372. //writeln('TCustomTestModule.CheckReferenceDirectives Line=',SrcLine);
  2373. p:=PChar(SrcLine);
  2374. repeat
  2375. case p^ of
  2376. #0: if (p-PChar(SrcLine)=length(SrcLine)) then break;
  2377. '{':
  2378. begin
  2379. CommentStartP:=p;
  2380. inc(p);
  2381. IsDirective:=p^ in ['#','@','='];
  2382. // skip to end of comment
  2383. repeat
  2384. case p^ of
  2385. #0:
  2386. if (p-PChar(SrcLine)=length(SrcLine)) then
  2387. begin
  2388. // multi line comment
  2389. if IsDirective then
  2390. RaiseError('directive missing closing bracket',CommentStartP);
  2391. repeat
  2392. inc(LineNumber);
  2393. if LineNumber>SrcLines.Count then exit;
  2394. SrcLine:=SrcLines[LineNumber-1];
  2395. //writeln('TCustomTestModule.CheckReferenceDirectives Comment Line=',SrcLine);
  2396. until SrcLine<>'';
  2397. p:=PChar(SrcLine);
  2398. continue;
  2399. end;
  2400. '}':
  2401. begin
  2402. inc(p);
  2403. break;
  2404. end;
  2405. end;
  2406. inc(p);
  2407. until false;
  2408. CommentEndP:=p;
  2409. case CommentStartP[1] of
  2410. '#': AddLabel;
  2411. '@': AddResolverReference;
  2412. '=': AddDirectReference;
  2413. end;
  2414. p:=CommentEndP;
  2415. continue;
  2416. end;
  2417. '/':
  2418. if p[1]='/' then
  2419. break; // rest of line is comment -> skip
  2420. end;
  2421. inc(p);
  2422. until false;
  2423. end;
  2424. end;
  2425. procedure CheckResolverReference(aMarker: PSrcMarker);
  2426. // check if one element at {@a} has a TResolvedReference to an element labeled {#a}
  2427. var
  2428. aLabel: PSrcMarker;
  2429. ReferenceElements, LabelElements: TFPList;
  2430. i, j, aLine, aCol: Integer;
  2431. El, Ref, LabelEl: TPasElement;
  2432. begin
  2433. //writeln('TCustomTestModule.CheckResolverReference searching reference: ',aMarker^.Filename,' Line=',aMarker^.Row,' Col=',aMarker^.StartCol,'-',aMarker^.EndCol,' Label="',aMarker^.Identifier,'"');
  2434. aLabel:=FindSrcLabel(aMarker^.Identifier);
  2435. if aLabel=nil then
  2436. RaiseErrorAtSrc('label "'+aMarker^.Identifier+'" not found',aMarker^.Filename,aMarker^.Row,aMarker^.StartCol);
  2437. LabelElements:=nil;
  2438. ReferenceElements:=nil;
  2439. try
  2440. LabelElements:=FindElementsAt(aLabel);
  2441. ReferenceElements:=FindElementsAt(aMarker);
  2442. for i:=0 to ReferenceElements.Count-1 do
  2443. begin
  2444. El:=TPasElement(ReferenceElements[i]);
  2445. Ref:=nil;
  2446. if El.CustomData is TResolvedReference then
  2447. Ref:=TResolvedReference(El.CustomData).Declaration
  2448. else if El.CustomData is TPasPropertyScope then
  2449. Ref:=TPasPropertyScope(El.CustomData).AncestorProp
  2450. else if El.CustomData is TPasSpecializeTypeData then
  2451. Ref:=TPasSpecializeTypeData(El.CustomData).SpecializedType;
  2452. if Ref<>nil then
  2453. for j:=0 to LabelElements.Count-1 do
  2454. begin
  2455. LabelEl:=TPasElement(LabelElements[j]);
  2456. if Ref=LabelEl then
  2457. exit; // success
  2458. end;
  2459. end;
  2460. // failure write candidates
  2461. for i:=0 to ReferenceElements.Count-1 do
  2462. begin
  2463. El:=TPasElement(ReferenceElements[i]);
  2464. write('Reference candidate for "',aMarker^.Identifier,'" at reference ',aMarker^.Filename,'(',aMarker^.Row,',',aMarker^.StartCol,'-',aMarker^.EndCol,')');
  2465. write(' El=',GetObjName(El));
  2466. if EL is TPrimitiveExpr then
  2467. begin
  2468. writeln('TCustomTestModule.CheckResolverReference ',TPrimitiveExpr(El).Value);
  2469. end;
  2470. Ref:=nil;
  2471. if El.CustomData is TResolvedReference then
  2472. Ref:=TResolvedReference(El.CustomData).Declaration
  2473. else if El.CustomData is TPasPropertyScope then
  2474. Ref:=TPasPropertyScope(El.CustomData).AncestorProp
  2475. else if El.CustomData is TPasSpecializeTypeData then
  2476. Ref:=TPasSpecializeTypeData(El.CustomData).SpecializedType;
  2477. if Ref<>nil then
  2478. begin
  2479. write(' Decl=',GetObjName(Ref));
  2480. ResolverEngine.UnmangleSourceLineNumber(Ref.SourceLinenumber,aLine,aCol);
  2481. write(',',Ref.SourceFilename,'(',aLine,',',aCol,')');
  2482. end
  2483. else
  2484. write(' has no TResolvedReference. El.CustomData=',GetObjName(El.CustomData));
  2485. writeln;
  2486. end;
  2487. for i:=0 to LabelElements.Count-1 do
  2488. begin
  2489. El:=TPasElement(LabelElements[i]);
  2490. write('Label candidate for "',aLabel^.Identifier,'" at reference ',aLabel^.Filename,'(',aLabel^.Row,',',aLabel^.StartCol,'-',aLabel^.EndCol,')');
  2491. write(' El=',GetObjName(El));
  2492. writeln;
  2493. end;
  2494. RaiseErrorAtSrcMarker('wrong resolved reference "'+aMarker^.Identifier+'"',aMarker);
  2495. finally
  2496. LabelElements.Free;
  2497. ReferenceElements.Free;
  2498. end;
  2499. end;
  2500. procedure CheckDirectReference(aMarker: PSrcMarker);
  2501. // check if one element at {=a} is a TPasAliasType pointing to an element labeled {#a}
  2502. var
  2503. aLabel: PSrcMarker;
  2504. ReferenceElements, LabelElements: TFPList;
  2505. i, LabelLine, LabelCol, j: Integer;
  2506. El, LabelEl: TPasElement;
  2507. DeclEl, TypeEl: TPasType;
  2508. begin
  2509. //writeln('CheckDirectReference searching pointer: ',aMarker^.Filename,' Line=',aMarker^.Row,' Col=',aMarker^.StartCol,'-',aMarker^.EndCol,' Label="',aMarker^.Identifier,'"');
  2510. aLabel:=FindSrcLabel(aMarker^.Identifier);
  2511. if aLabel=nil then
  2512. RaiseErrorAtSrcMarker('label "'+aMarker^.Identifier+'" not found',aMarker);
  2513. LabelElements:=nil;
  2514. ReferenceElements:=nil;
  2515. try
  2516. //writeln('CheckDirectReference finding elements at label ...');
  2517. LabelElements:=FindElementsAt(aLabel);
  2518. //writeln('CheckDirectReference finding elements at reference ...');
  2519. ReferenceElements:=FindElementsAt(aMarker);
  2520. for i:=0 to ReferenceElements.Count-1 do
  2521. begin
  2522. El:=TPasElement(ReferenceElements[i]);
  2523. //writeln('CheckDirectReference ',i,'/',ReferenceElements.Count,' ',GetTreeDbg(El,2));
  2524. if El.ClassType=TPasVariable then
  2525. begin
  2526. if TPasVariable(El).VarType=nil then
  2527. begin
  2528. //writeln('CheckDirectReference Var without Type: ',GetObjName(El),' El.Parent=',GetObjName(El.Parent));
  2529. AssertNotNull('TPasVariable(El='+El.Name+').VarType',TPasVariable(El).VarType);
  2530. end;
  2531. TypeEl:=TPasVariable(El).VarType;
  2532. for j:=0 to LabelElements.Count-1 do
  2533. begin
  2534. LabelEl:=TPasElement(LabelElements[j]);
  2535. if TypeEl=LabelEl then
  2536. exit; // success
  2537. end;
  2538. end
  2539. else if El is TPasAliasType then
  2540. begin
  2541. DeclEl:=TPasAliasType(El).DestType;
  2542. ResolverEngine.UnmangleSourceLineNumber(DeclEl.SourceLinenumber,LabelLine,LabelCol);
  2543. if (aLabel^.Filename=DeclEl.SourceFilename)
  2544. and (integer(aLabel^.Row)=LabelLine)
  2545. and (aLabel^.StartCol<=LabelCol)
  2546. and (aLabel^.EndCol>=LabelCol) then
  2547. exit; // success
  2548. end
  2549. else if El.ClassType=TPasArgument then
  2550. begin
  2551. TypeEl:=TPasArgument(El).ArgType;
  2552. for j:=0 to LabelElements.Count-1 do
  2553. begin
  2554. LabelEl:=TPasElement(LabelElements[j]);
  2555. if TypeEl=LabelEl then
  2556. exit; // success
  2557. end;
  2558. end;
  2559. end;
  2560. // failed -> show candidates
  2561. writeln('CheckDirectReference failed: Labels:');
  2562. for j:=0 to LabelElements.Count-1 do
  2563. begin
  2564. LabelEl:=TPasElement(LabelElements[j]);
  2565. writeln(' Label ',GetObjName(LabelEl),' at ',ResolverEngine.GetElementSourcePosStr(LabelEl));
  2566. end;
  2567. writeln('CheckDirectReference failed: References:');
  2568. for i:=0 to ReferenceElements.Count-1 do
  2569. begin
  2570. El:=TPasElement(ReferenceElements[i]);
  2571. writeln(' Reference ',GetObjName(El),' at ',ResolverEngine.GetElementSourcePosStr(El));
  2572. //if EL is TPasVariable then
  2573. // writeln('CheckDirectReference ',GetObjPath(TPasVariable(El).VarType),' ',ResolverEngine.GetElementSourcePosStr(TPasVariable(EL).VarType));
  2574. end;
  2575. RaiseErrorAtSrcMarker('wrong direct reference "'+aMarker^.Identifier+'"',aMarker);
  2576. finally
  2577. LabelElements.Free;
  2578. ReferenceElements.Free;
  2579. end;
  2580. end;
  2581. var
  2582. aMarker: PSrcMarker;
  2583. i: Integer;
  2584. SrcLines: TStringList;
  2585. begin
  2586. Module.ForEachCall(@OnCheckElementParent,nil);
  2587. //writeln('TCustomTestModule.CheckReferenceDirectives find all markers');
  2588. // find all markers
  2589. for i:=0 to FileResolver.Streams.Count-1 do
  2590. begin
  2591. GetSrc(i,SrcLines,CurFilename);
  2592. ParseCode(SrcLines,CurFilename);
  2593. SrcLines.Free;
  2594. end;
  2595. //writeln('TCustomTestModule.CheckReferenceDirectives check references');
  2596. // check references
  2597. aMarker:=FirstSrcMarker;
  2598. while aMarker<>nil do
  2599. begin
  2600. case aMarker^.Kind of
  2601. mkResolverReference: CheckResolverReference(aMarker);
  2602. mkDirectReference: CheckDirectReference(aMarker);
  2603. end;
  2604. aMarker:=aMarker^.Next;
  2605. end;
  2606. //writeln('TCustomTestModule.CheckReferenceDirectives COMPLETE');
  2607. end;
  2608. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  2609. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  2610. var
  2611. i: Integer;
  2612. Item: TTestHintMessage;
  2613. Expected,Actual: string;
  2614. begin
  2615. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  2616. for i:=0 to MsgCount-1 do
  2617. begin
  2618. Item:=Msgs[i];
  2619. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  2620. if (Marker<>nil) then
  2621. begin
  2622. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  2623. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  2624. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  2625. end;
  2626. // found
  2627. FHintMsgsGood.Add(Item);
  2628. str(Item.MsgType,Actual);
  2629. str(MsgType,Expected);
  2630. AssertEquals('MsgType',Expected,Actual);
  2631. exit;
  2632. end;
  2633. // needed message missing -> show emitted messages
  2634. WriteSources('',0,0);
  2635. for i:=0 to MsgCount-1 do
  2636. begin
  2637. Item:=Msgs[i];
  2638. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  2639. ' ('+IntToStr(Item.MsgNumber),')');
  2640. if Marker<>nil then
  2641. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  2642. writeln(' {',Item.Msg,'}');
  2643. end;
  2644. str(MsgType,Expected);
  2645. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  2646. if Marker<>nil then
  2647. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  2648. Actual:=Actual+' '+Msg;
  2649. Fail(Actual);
  2650. end;
  2651. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  2652. );
  2653. var
  2654. i: Integer;
  2655. s, Txt: String;
  2656. Msg: TTestHintMessage;
  2657. begin
  2658. for i:=0 to MsgCount-1 do
  2659. begin
  2660. Msg:=Msgs[i];
  2661. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  2662. s:='';
  2663. str(Msg.MsgType,s);
  2664. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  2665. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  2666. if WithSourcePos then
  2667. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  2668. Txt:=Txt+' {'+Msg.Msg+'}';
  2669. Fail(Txt);
  2670. end;
  2671. end;
  2672. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  2673. MsgNumber: integer);
  2674. begin
  2675. ExpectedErrorClass:=EScannerError;
  2676. ExpectedErrorMsg:=Msg;
  2677. ExpectedErrorNumber:=MsgNumber;
  2678. end;
  2679. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2680. MsgNumber: integer);
  2681. begin
  2682. ExpectedErrorClass:=EParserError;
  2683. ExpectedErrorMsg:=Msg;
  2684. ExpectedErrorNumber:=MsgNumber;
  2685. end;
  2686. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2687. MsgNumber: integer);
  2688. begin
  2689. ExpectedErrorClass:=EPasResolve;
  2690. ExpectedErrorMsg:=Msg;
  2691. ExpectedErrorNumber:=MsgNumber;
  2692. end;
  2693. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2694. MsgNumber: integer);
  2695. begin
  2696. ExpectedErrorClass:=EPas2JS;
  2697. ExpectedErrorMsg:=Msg;
  2698. ExpectedErrorNumber:=MsgNumber;
  2699. end;
  2700. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2701. var
  2702. MsgNumber: Integer;
  2703. Msg: String;
  2704. begin
  2705. Result:=false;
  2706. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2707. Msg:=E.Message;
  2708. if E is EPas2JS then
  2709. MsgNumber:=EPas2JS(E).MsgNumber
  2710. else if E is EPasResolve then
  2711. MsgNumber:=EPasResolve(E).MsgNumber
  2712. else if E is EParserError then
  2713. MsgNumber:=Parser.LastMsgNumber
  2714. else if E is EScannerError then
  2715. begin
  2716. MsgNumber:=Scanner.LastMsgNumber;
  2717. Msg:=Scanner.LastMsg;
  2718. end
  2719. else
  2720. MsgNumber:=0;
  2721. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2722. if Result then
  2723. SkipTests:=true;
  2724. end;
  2725. procedure TCustomTestModule.RaiseErrorAtSrc(Msg: string;
  2726. const aFilename: string; aRow, aCol: integer);
  2727. var
  2728. s: String;
  2729. begin
  2730. WriteSources(aFilename,aRow,aCol);
  2731. s:='[TCustomTestModule.RaiseErrorAtSrc] '+aFilename+'('+IntToStr(aRow)+','+IntToStr(aCol)+') Error: '+Msg;
  2732. writeln('ERROR: ',s);
  2733. Fail(s);
  2734. end;
  2735. procedure TCustomTestModule.RaiseErrorAtSrcMarker(Msg: string;
  2736. aMarker: PSrcMarker);
  2737. begin
  2738. RaiseErrorAtSrc(Msg,aMarker^.Filename,aMarker^.Row,aMarker^.StartCol);
  2739. end;
  2740. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2741. begin
  2742. if IsErrorExpected(E) then exit;
  2743. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2744. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2745. +' '+Scanner.CurFilename
  2746. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2747. FailException(E);
  2748. end;
  2749. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2750. begin
  2751. if IsErrorExpected(E) then exit;
  2752. WriteSources(E.Filename,E.Row,E.Column);
  2753. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2754. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2755. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2756. );
  2757. FailException(E);
  2758. end;
  2759. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2760. var
  2761. P: TPasSourcePos;
  2762. begin
  2763. if IsErrorExpected(E) then exit;
  2764. P:=E.SourcePos;
  2765. WriteSources(P.FileName,P.Row,P.Column);
  2766. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2767. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2768. FailException(E);
  2769. end;
  2770. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2771. var
  2772. Row, Col: integer;
  2773. begin
  2774. if IsErrorExpected(E) then exit;
  2775. ResolverEngine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2776. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2777. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2778. +' '+E.PasElement.SourceFilename
  2779. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2780. FailException(E);
  2781. end;
  2782. procedure TCustomTestModule.HandleException(E: Exception);
  2783. begin
  2784. if E is EScannerError then
  2785. HandleScannerError(EScannerError(E))
  2786. else if E is EParserError then
  2787. HandleParserError(EParserError(E))
  2788. else if E is EPasResolve then
  2789. HandlePasResolveError(EPasResolve(E))
  2790. else if E is EPas2JS then
  2791. HandlePas2JSError(EPas2JS(E))
  2792. else
  2793. begin
  2794. if IsErrorExpected(E) then exit;
  2795. if not (E is EAssertionFailedError) then
  2796. begin
  2797. WriteSources('',0,0);
  2798. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2799. end;
  2800. FailException(E);
  2801. end;
  2802. end;
  2803. procedure TCustomTestModule.FailException(E: Exception);
  2804. var
  2805. MsgNumber: Integer;
  2806. begin
  2807. if ExpectedErrorClass<>nil then
  2808. begin
  2809. if FExpectedErrorClass=E.ClassType then
  2810. begin
  2811. if E is EPas2JS then
  2812. MsgNumber:=EPas2JS(E).MsgNumber
  2813. else if E is EPasResolve then
  2814. MsgNumber:=EPasResolve(E).MsgNumber
  2815. else if E is EParserError then
  2816. MsgNumber:=Parser.LastMsgNumber
  2817. else if E is EScannerError then
  2818. MsgNumber:=Scanner.LastMsgNumber
  2819. else
  2820. MsgNumber:=0;
  2821. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2822. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2823. ExpectedErrorNumber,MsgNumber);
  2824. end else begin
  2825. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2826. end;
  2827. end;
  2828. Fail(E.Message);
  2829. end;
  2830. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2831. aCol: integer);
  2832. var
  2833. IsSrc: Boolean;
  2834. i, j: Integer;
  2835. SrcLines: TStringList;
  2836. Line: string;
  2837. aModule: TTestEnginePasResolver;
  2838. begin
  2839. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2840. for i:=0 to ResolverCount-1 do
  2841. begin
  2842. aModule:=Resolvers[i];
  2843. SrcLines:=TStringList.Create;
  2844. try
  2845. SrcLines.Text:=aModule.Source;
  2846. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2847. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2848. for j:=1 to SrcLines.Count do
  2849. begin
  2850. Line:=SrcLines[j-1];
  2851. if IsSrc and (j=aRow) then
  2852. begin
  2853. write('*');
  2854. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2855. end;
  2856. writeln(Format('%:4d: ',[j]),Line);
  2857. end;
  2858. finally
  2859. SrcLines.Free;
  2860. end;
  2861. end;
  2862. end;
  2863. function TCustomTestModule.IndexOfResolver(const aFilename: string): integer;
  2864. var
  2865. i: Integer;
  2866. begin
  2867. for i:=0 to ResolverCount-1 do
  2868. if aFilename=Resolvers[i].Filename then exit(i);
  2869. Result:=-1;
  2870. end;
  2871. function TCustomTestModule.GetResolver(const aFilename: string
  2872. ): TTestEnginePasResolver;
  2873. var
  2874. i: Integer;
  2875. begin
  2876. i:=IndexOfResolver(aFilename);
  2877. if i<0 then exit(nil);
  2878. Result:=Resolvers[i];
  2879. end;
  2880. procedure TCustomTestModule.GetSrc(Index: integer; out SrcLines: TStringList;
  2881. out aFilename: string);
  2882. var
  2883. aStream: TStream;
  2884. begin
  2885. SrcLines:=TStringList.Create;
  2886. aStream:=FileResolver.Streams.Objects[Index] as TStream;
  2887. aStream.Position:=0;
  2888. SrcLines.LoadFromStream(aStream);
  2889. aFilename:=FileResolver.Streams[Index];
  2890. end;
  2891. function TCustomTestModule.FindElementsAt(aFilename: string; aLine, aStartCol,
  2892. aEndCol: integer): TFPList;
  2893. var
  2894. ok: Boolean;
  2895. FoundRefs: TTestResolverReferenceData;
  2896. i: Integer;
  2897. CurResolver: TTestEnginePasResolver;
  2898. begin
  2899. //writeln('TCustomTestModule.FindElementsAt START "',aFilename,'" Line=',aLine,' Col=',aStartCol,'-',aEndCol);
  2900. FoundRefs:=Default(TTestResolverReferenceData);
  2901. FoundRefs.Filename:=aFilename;
  2902. FoundRefs.Row:=aLine;
  2903. FoundRefs.StartCol:=aStartCol;
  2904. FoundRefs.EndCol:=aEndCol;
  2905. FoundRefs.Found:=TFPList.Create;
  2906. ok:=false;
  2907. try
  2908. // find all markers
  2909. Module.ForEachCall(@OnFindReference,@FoundRefs);
  2910. for i:=0 to ResolverCount-1 do
  2911. begin
  2912. CurResolver:=Resolvers[i];
  2913. if CurResolver.Module=Module then continue;
  2914. //writeln('TCustomTestResolver.FindElementsAt ',CurResolver.Filename);
  2915. CurResolver.Module.ForEachCall(@OnFindReference,@FoundRefs);
  2916. end;
  2917. ok:=true;
  2918. finally
  2919. if not ok then
  2920. FreeAndNil(FoundRefs.Found);
  2921. end;
  2922. Result:=FoundRefs.Found;
  2923. FoundRefs.Found:=nil;
  2924. end;
  2925. function TCustomTestModule.FindElementsAt(aMarker: PSrcMarker;
  2926. ErrorOnNoElements: boolean): TFPList;
  2927. begin
  2928. Result:=FindElementsAt(aMarker^.Filename,aMarker^.Row,aMarker^.StartCol,aMarker^.EndCol);
  2929. if ErrorOnNoElements and ((Result=nil) or (Result.Count=0)) then
  2930. RaiseErrorAtSrcMarker('marker '+SrcMarker[aMarker^.Kind]+aMarker^.Identifier+' has no elements',aMarker);
  2931. end;
  2932. function TCustomTestModule.FindSrcLabel(const Identifier: string): PSrcMarker;
  2933. begin
  2934. Result:=FirstSrcMarker;
  2935. while Result<>nil do
  2936. begin
  2937. if (Result^.Kind=mkLabel)
  2938. and (CompareText(Result^.Identifier,Identifier)=0) then
  2939. exit;
  2940. Result:=Result^.Next;
  2941. end;
  2942. end;
  2943. function TCustomTestModule.FindElementsAtSrcLabel(const Identifier: string;
  2944. ErrorOnNoElements: boolean): TFPList;
  2945. var
  2946. SrcLabel: PSrcMarker;
  2947. begin
  2948. SrcLabel:=FindSrcLabel(Identifier);
  2949. if SrcLabel=nil then
  2950. Fail('missing label "'+Identifier+'"');
  2951. Result:=FindElementsAt(SrcLabel,ErrorOnNoElements);
  2952. end;
  2953. function TCustomTestModule.GetDefaultNamespace: string;
  2954. var
  2955. C: TClass;
  2956. begin
  2957. Result:='';
  2958. if FModule=nil then exit;
  2959. C:=FModule.ClassType;
  2960. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2961. Result:=ResolverEngine.DefaultNameSpace;
  2962. end;
  2963. constructor TCustomTestModule.Create;
  2964. begin
  2965. inherited Create;
  2966. FHintMsgs:=TObjectList.Create(true);
  2967. FHintMsgsGood:=TFPList.Create;
  2968. end;
  2969. destructor TCustomTestModule.Destroy;
  2970. begin
  2971. FreeAndNil(FHintMsgs);
  2972. FreeAndNil(FHintMsgsGood);
  2973. inherited Destroy;
  2974. end;
  2975. { TTestModule }
  2976. procedure TTestModule.TestReservedWords;
  2977. var
  2978. i: integer;
  2979. begin
  2980. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2981. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2982. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2983. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2984. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2985. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2986. end;
  2987. procedure TTestModule.TestEmptyProgram;
  2988. begin
  2989. StartProgram(false);
  2990. Add('begin');
  2991. ConvertProgram;
  2992. CheckSource('TestEmptyProgram','','');
  2993. end;
  2994. procedure TTestModule.TestEmptyProgramUseStrict;
  2995. begin
  2996. Converter.Options:=Converter.Options+[coUseStrict];
  2997. StartProgram(false);
  2998. Add('begin');
  2999. ConvertProgram;
  3000. CheckSource('TestEmptyProgramUseStrict','','');
  3001. end;
  3002. procedure TTestModule.TestEmptyUnit;
  3003. begin
  3004. StartUnit(false);
  3005. Add('interface');
  3006. Add('implementation');
  3007. ConvertUnit;
  3008. CheckSource('TestEmptyUnit',
  3009. LinesToStr([
  3010. ]),
  3011. '');
  3012. end;
  3013. procedure TTestModule.TestEmptyUnitUseStrict;
  3014. begin
  3015. Converter.Options:=Converter.Options+[coUseStrict];
  3016. StartUnit(false);
  3017. Add('interface');
  3018. Add('implementation');
  3019. ConvertUnit;
  3020. CheckSource('TestEmptyUnitUseStrict',
  3021. LinesToStr([
  3022. ''
  3023. ]),
  3024. '');
  3025. end;
  3026. procedure TTestModule.TestDottedUnitNames;
  3027. begin
  3028. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  3029. LinesToStr([
  3030. 'var iV: longint;'
  3031. ]),
  3032. '');
  3033. FFilename:='ns1.test1.pp';
  3034. StartProgram(true);
  3035. Add('uses unIt2;');
  3036. Add('var');
  3037. Add(' i: longint;');
  3038. Add('begin');
  3039. Add(' i:=iv;');
  3040. Add(' i:=uNit2.iv;');
  3041. Add(' i:=Ns1.TEst1.i;');
  3042. ConvertProgram;
  3043. CheckSource('TestDottedUnitNames',
  3044. LinesToStr([
  3045. 'this.i = 0;',
  3046. '']),
  3047. LinesToStr([ // this.$init
  3048. '$mod.i = pas["NS1.Unit2"].iV;',
  3049. '$mod.i = pas["NS1.Unit2"].iV;',
  3050. '$mod.i = $mod.i;',
  3051. '']) );
  3052. end;
  3053. procedure TTestModule.TestDottedUnitNameImpl;
  3054. begin
  3055. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  3056. LinesToStr([
  3057. 'type',
  3058. ' TObject = class end;',
  3059. ' TTestA = class',
  3060. ' end;'
  3061. ]),
  3062. LinesToStr(['uses TEST.UnitB;'])
  3063. );
  3064. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  3065. LinesToStr([
  3066. 'uses TEST.UnitA;',
  3067. 'type TTestB = class(TTestA);'
  3068. ]),
  3069. ''
  3070. );
  3071. StartProgram(true);
  3072. Add('uses TEST.UnitA;');
  3073. Add('begin');
  3074. ConvertProgram;
  3075. CheckSource('TestDottedUnitNameImpl',
  3076. LinesToStr([
  3077. '']),
  3078. LinesToStr([ // this.$init
  3079. '']) );
  3080. CheckUnit('TEST.UnitA.pas',
  3081. LinesToStr([
  3082. 'rtl.module("TEST.UnitA", ["system"], function () {',
  3083. ' var $mod = this;',
  3084. ' rtl.createClass(this, "TObject", null, function () {',
  3085. ' this.$init = function () {',
  3086. ' };',
  3087. ' this.$final = function () {',
  3088. ' };',
  3089. ' });',
  3090. ' rtl.createClass(this, "TTestA", this.TObject, function () {',
  3091. ' });',
  3092. '}, ["TEST.UnitB"]);'
  3093. ]));
  3094. CheckUnit('TEST.UnitB.pas',
  3095. LinesToStr([
  3096. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  3097. ' var $mod = this;',
  3098. ' rtl.createClass(this, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  3099. ' });',
  3100. '});'
  3101. ]));
  3102. end;
  3103. procedure TTestModule.TestDottedUnitExpr;
  3104. begin
  3105. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  3106. LinesToStr([
  3107. 'procedure DoIt;'
  3108. ]),
  3109. 'procedure DoIt; begin end;');
  3110. FFilename:='Ns1.SubNs1.Test1.pp';
  3111. StartProgram(true);
  3112. Add('uses Ns2.sUbnS2.unIt2;');
  3113. Add('var');
  3114. Add(' i: longint;');
  3115. Add('begin');
  3116. Add(' ns2.subns2.unit2.doit;');
  3117. Add(' i:=Ns1.SubNS1.TEst1.i;');
  3118. ConvertProgram;
  3119. CheckSource('TestDottedUnitExpr',
  3120. LinesToStr([
  3121. 'this.i = 0;',
  3122. '']),
  3123. LinesToStr([ // this.$init
  3124. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  3125. '$mod.i = $mod.i;',
  3126. '']) );
  3127. end;
  3128. procedure TTestModule.Test_ModeFPCFail;
  3129. begin
  3130. StartProgram(false);
  3131. Add('{$mode FPC}');
  3132. Add('begin');
  3133. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  3134. ConvertProgram;
  3135. end;
  3136. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  3137. begin
  3138. StartProgram(false);
  3139. Add('{$modeswitch cblocks-}');
  3140. Add('begin');
  3141. ConvertProgram;
  3142. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  3143. CheckResolverUnexpectedHints();
  3144. end;
  3145. procedure TTestModule.TestUnit_UseSystem;
  3146. begin
  3147. StartUnit(true);
  3148. Add([
  3149. 'interface',
  3150. 'var i: integer;',
  3151. 'implementation']);
  3152. ConvertUnit;
  3153. CheckSource('TestUnit_UseSystem',
  3154. LinesToStr([
  3155. 'this.i = 0;',
  3156. '']),
  3157. LinesToStr([
  3158. '']) );
  3159. end;
  3160. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  3161. begin
  3162. AddModuleWithIntfImplSrc('unit1.pp',
  3163. LinesToStr([
  3164. 'type number = longint;']),
  3165. LinesToStr([
  3166. 'uses test1;',
  3167. 'procedure DoIt;',
  3168. 'begin',
  3169. ' i:=3;',
  3170. 'end;']));
  3171. StartUnit(true);
  3172. Add([
  3173. 'interface',
  3174. 'uses unit1;',
  3175. 'var i: number;',
  3176. 'implementation']);
  3177. ConvertUnit;
  3178. CheckSource('TestUnit_Intf1Impl2Intf1',
  3179. LinesToStr([
  3180. 'this.i = 0;',
  3181. '']),
  3182. LinesToStr([
  3183. '']) );
  3184. end;
  3185. procedure TTestModule.TestIncludeVersion;
  3186. begin
  3187. StartProgram(false);
  3188. Add([
  3189. 'var',
  3190. ' s: string;',
  3191. ' i: word;',
  3192. 'begin',
  3193. ' s:={$I %line%};',
  3194. ' i:={$I %linenum%};',
  3195. ' s:={$I %currentroutine%};',
  3196. ' s:={$I %pas2jsversion%};',
  3197. ' s:={$I %pas2jstarget%};',
  3198. ' s:={$I %pas2jstargetos%};',
  3199. ' s:={$I %pas2jstargetcpu%};',
  3200. ' s:={$I %file%};',
  3201. '']);
  3202. ConvertProgram;
  3203. CheckSource('TestIncludeVersion',
  3204. LinesToStr([
  3205. 'this.s="";',
  3206. 'this.i = 0;']),
  3207. LinesToStr([
  3208. '$mod.s = "7";',
  3209. '$mod.i = 8;',
  3210. '$mod.s = "<anonymous>";',
  3211. '$mod.s = "Comp.Ver.tcmodules";',
  3212. '$mod.s = "Browser";',
  3213. '$mod.s = "Browser";',
  3214. '$mod.s = "ECMAScript5";',
  3215. '$mod.s = "test1.pp";',
  3216. '']));
  3217. end;
  3218. procedure TTestModule.TestVarInt;
  3219. begin
  3220. StartProgram(false);
  3221. Add('var MyI: longint;');
  3222. Add('begin');
  3223. ConvertProgram;
  3224. CheckSource('TestVarInt','this.MyI=0;','');
  3225. end;
  3226. procedure TTestModule.TestVarBaseTypes;
  3227. begin
  3228. StartProgram(false);
  3229. Add('var');
  3230. Add(' i: longint;');
  3231. Add(' s: string;');
  3232. Add(' c: char;');
  3233. Add(' b: boolean;');
  3234. Add(' d: double;');
  3235. Add(' i2: longint = 3;');
  3236. Add(' s2: string = ''foo'';');
  3237. Add(' c2: char = ''4'';');
  3238. Add(' b2: boolean = true;');
  3239. Add(' d2: double = 5.6;');
  3240. Add(' i3: longint = $707;');
  3241. Add(' i4: nativeint = 9007199254740991;');
  3242. Add(' i5: nativeint = -9007199254740991-1;');
  3243. Add(' i6: nativeint = $fffffffffffff;');
  3244. Add(' i7: nativeint = -$fffffffffffff-1;');
  3245. Add(' i8: byte = 00;');
  3246. Add(' u8: nativeuint = $fffffffffffff;');
  3247. Add(' u9: nativeuint = $0000000000000;');
  3248. Add(' u10: nativeuint = $00ff00;');
  3249. Add('begin');
  3250. ConvertProgram;
  3251. CheckSource('TestVarBaseTypes',
  3252. LinesToStr([
  3253. 'this.i = 0;',
  3254. 'this.s = "";',
  3255. 'this.c = "\x00";',
  3256. 'this.b = false;',
  3257. 'this.d = 0.0;',
  3258. 'this.i2 = 3;',
  3259. 'this.s2 = "foo";',
  3260. 'this.c2 = "4";',
  3261. 'this.b2 = true;',
  3262. 'this.d2 = 5.6;',
  3263. 'this.i3 = 0x707;',
  3264. 'this.i4 = 9007199254740991;',
  3265. 'this.i5 = -9007199254740991-1;',
  3266. 'this.i6 = 0xfffffffffffff;',
  3267. 'this.i7 =-0xfffffffffffff-1;',
  3268. 'this.i8 = 0;',
  3269. 'this.u8 = 0xfffffffffffff;',
  3270. 'this.u9 = 0x0;',
  3271. 'this.u10 = 0xff00;'
  3272. ]),
  3273. '');
  3274. end;
  3275. procedure TTestModule.TestBaseTypeSingleFail;
  3276. begin
  3277. StartProgram(false);
  3278. Add('var s: single;');
  3279. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  3280. ConvertProgram;
  3281. end;
  3282. procedure TTestModule.TestBaseTypeExtendedFail;
  3283. begin
  3284. StartProgram(false);
  3285. Add('var e: extended;');
  3286. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  3287. ConvertProgram;
  3288. end;
  3289. procedure TTestModule.TestConstBaseTypes;
  3290. begin
  3291. StartProgram(false);
  3292. Add('const');
  3293. Add(' i: longint = 3;');
  3294. Add(' s: string = ''foo'';');
  3295. Add(' c: char = ''4'';');
  3296. Add(' b: boolean = true;');
  3297. Add(' d: double = 5.6;');
  3298. Add(' e = low(word);');
  3299. Add(' f = high(word);');
  3300. Add('begin');
  3301. ConvertProgram;
  3302. CheckSource('TestVarBaseTypes',
  3303. LinesToStr([
  3304. 'this.i=3;',
  3305. 'this.s="foo";',
  3306. 'this.c="4";',
  3307. 'this.b=true;',
  3308. 'this.d=5.6;',
  3309. 'this.e = 0;',
  3310. 'this.f = 65535;'
  3311. ]),
  3312. '');
  3313. end;
  3314. procedure TTestModule.TestAliasTypeRef;
  3315. begin
  3316. StartProgram(false);
  3317. Add('type');
  3318. Add(' a=longint;');
  3319. Add(' b=a;');
  3320. Add('var');
  3321. Add(' c: A;');
  3322. Add(' d: B;');
  3323. Add('begin');
  3324. ConvertProgram;
  3325. CheckSource('TestAliasTypeRef',
  3326. LinesToStr([ // statements
  3327. 'this.c = 0;',
  3328. 'this.d = 0;'
  3329. ]),
  3330. LinesToStr([ // this.$main
  3331. ''
  3332. ]));
  3333. end;
  3334. procedure TTestModule.TestTypeCast_BaseTypes;
  3335. begin
  3336. StartProgram(false);
  3337. Add([
  3338. 'var',
  3339. ' i: longint;',
  3340. ' b: boolean;',
  3341. ' d: double;',
  3342. ' s: string;',
  3343. ' c: char;',
  3344. 'begin',
  3345. ' i:=longint(i);',
  3346. ' i:=longint(b);',
  3347. ' b:=boolean(b);',
  3348. ' b:=boolean(i);',
  3349. ' d:=double(d);',
  3350. ' d:=double(i);',
  3351. ' s:=string(s);',
  3352. ' s:=string(c);',
  3353. ' c:=char(c);',
  3354. ' c:=char(i);',
  3355. ' c:=char(65);',
  3356. ' c:=char(#10);',
  3357. ' c:=char(#$E000);',
  3358. '']);
  3359. ConvertProgram;
  3360. CheckSource('TestAliasTypeRef',
  3361. LinesToStr([ // statements
  3362. 'this.i = 0;',
  3363. 'this.b = false;',
  3364. 'this.d = 0.0;',
  3365. 'this.s = "";',
  3366. 'this.c = "\x00";',
  3367. '']),
  3368. LinesToStr([ // this.$main
  3369. '$mod.i = $mod.i;',
  3370. '$mod.i = ($mod.b ? 1 : 0);',
  3371. '$mod.b = $mod.b;',
  3372. '$mod.b = $mod.i != 0;',
  3373. '$mod.d = $mod.d;',
  3374. '$mod.d = $mod.i;',
  3375. '$mod.s = $mod.s;',
  3376. '$mod.s = $mod.c;',
  3377. '$mod.c = $mod.c;',
  3378. '$mod.c = String.fromCharCode($mod.i);',
  3379. '$mod.c = "A";',
  3380. '$mod.c = "\n";',
  3381. '$mod.c = "";',
  3382. '']));
  3383. end;
  3384. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  3385. begin
  3386. StartProgram(false);
  3387. Add('type');
  3388. Add(' integer = longint;');
  3389. Add(' TYesNo = boolean;');
  3390. Add(' TFloat = double;');
  3391. Add(' TCaption = string;');
  3392. Add(' TChar = char;');
  3393. Add('var');
  3394. Add(' i: integer;');
  3395. Add(' b: TYesNo;');
  3396. Add(' d: TFloat;');
  3397. Add(' s: TCaption;');
  3398. Add(' c: TChar;');
  3399. Add('begin');
  3400. Add(' i:=integer(i);');
  3401. Add(' i:=integer(b);');
  3402. Add(' b:=TYesNo(b);');
  3403. Add(' b:=TYesNo(i);');
  3404. Add(' d:=TFloat(d);');
  3405. Add(' d:=TFloat(i);');
  3406. Add(' s:=TCaption(s);');
  3407. Add(' s:=TCaption(c);');
  3408. Add(' c:=TChar(c);');
  3409. ConvertProgram;
  3410. CheckSource('TestAliasTypeRef',
  3411. LinesToStr([ // statements
  3412. 'this.i = 0;',
  3413. 'this.b = false;',
  3414. 'this.d = 0.0;',
  3415. 'this.s = "";',
  3416. 'this.c = "\x00";',
  3417. '']),
  3418. LinesToStr([ // this.$main
  3419. '$mod.i = $mod.i;',
  3420. '$mod.i = ($mod.b ? 1 : 0);',
  3421. '$mod.b = $mod.b;',
  3422. '$mod.b = $mod.i != 0;',
  3423. '$mod.d = $mod.d;',
  3424. '$mod.d = $mod.i;',
  3425. '$mod.s = $mod.s;',
  3426. '$mod.s = $mod.c;',
  3427. '$mod.c = $mod.c;',
  3428. '']));
  3429. end;
  3430. procedure TTestModule.TestEmptyProc;
  3431. begin
  3432. StartProgram(false);
  3433. Add('procedure Test;');
  3434. Add('begin');
  3435. Add('end;');
  3436. Add('begin');
  3437. ConvertProgram;
  3438. CheckSource('TestEmptyProc',
  3439. LinesToStr([ // statements
  3440. 'this.Test = function () {',
  3441. '};'
  3442. ]),
  3443. LinesToStr([ // this.$main
  3444. ''
  3445. ]));
  3446. end;
  3447. procedure TTestModule.TestProcOneParam;
  3448. begin
  3449. StartProgram(false);
  3450. Add('procedure ProcA(i: longint);');
  3451. Add('begin');
  3452. Add('end;');
  3453. Add('begin');
  3454. Add(' PROCA(3);');
  3455. ConvertProgram;
  3456. CheckSource('TestProcOneParam',
  3457. LinesToStr([ // statements
  3458. 'this.ProcA = function (i) {',
  3459. '};'
  3460. ]),
  3461. LinesToStr([ // this.$main
  3462. '$mod.ProcA(3);'
  3463. ]));
  3464. end;
  3465. procedure TTestModule.TestFunctionWithoutParams;
  3466. begin
  3467. StartProgram(false);
  3468. Add('function FuncA: longint;');
  3469. Add('begin');
  3470. Add('end;');
  3471. Add('var i: longint;');
  3472. Add('begin');
  3473. Add(' I:=FUNCA();');
  3474. Add(' I:=FUNCA;');
  3475. Add(' FUNCA();');
  3476. Add(' FUNCA;');
  3477. ConvertProgram;
  3478. CheckSource('TestProcWithoutParams',
  3479. LinesToStr([ // statements
  3480. 'this.FuncA = function () {',
  3481. ' var Result = 0;',
  3482. ' return Result;',
  3483. '};',
  3484. 'this.i=0;'
  3485. ]),
  3486. LinesToStr([ // this.$main
  3487. '$mod.i=$mod.FuncA();',
  3488. '$mod.i=$mod.FuncA();',
  3489. '$mod.FuncA();',
  3490. '$mod.FuncA();'
  3491. ]));
  3492. end;
  3493. procedure TTestModule.TestProcedureWithoutParams;
  3494. begin
  3495. StartProgram(false);
  3496. Add('procedure ProcA;');
  3497. Add('begin');
  3498. Add('end;');
  3499. Add('begin');
  3500. Add(' PROCA();');
  3501. Add(' PROCA;');
  3502. ConvertProgram;
  3503. CheckSource('TestProcWithoutParams',
  3504. LinesToStr([ // statements
  3505. 'this.ProcA = function () {',
  3506. '};'
  3507. ]),
  3508. LinesToStr([ // this.$main
  3509. '$mod.ProcA();',
  3510. '$mod.ProcA();'
  3511. ]));
  3512. end;
  3513. procedure TTestModule.TestIncDec;
  3514. begin
  3515. StartProgram(false);
  3516. Add([
  3517. 'procedure DoIt(var i: longint);',
  3518. 'begin',
  3519. ' inc(i);',
  3520. ' inc(i,2);',
  3521. 'end;',
  3522. 'var',
  3523. ' Bar: longint;',
  3524. 'begin',
  3525. ' inc(bar);',
  3526. ' inc(bar,2);',
  3527. ' dec(bar);',
  3528. ' dec(bar,3);',
  3529. '']);
  3530. ConvertProgram;
  3531. CheckSource('TestIncDec',
  3532. LinesToStr([ // statements
  3533. 'this.DoIt = function (i) {',
  3534. ' i.set(i.get()+1);',
  3535. ' i.set(i.get()+2);',
  3536. '};',
  3537. 'this.Bar = 0;'
  3538. ]),
  3539. LinesToStr([ // this.$main
  3540. '$mod.Bar+=1;',
  3541. '$mod.Bar+=2;',
  3542. '$mod.Bar-=1;',
  3543. '$mod.Bar-=3;'
  3544. ]));
  3545. end;
  3546. procedure TTestModule.TestLoHiFpcMode;
  3547. begin
  3548. StartProgram(false);
  3549. Add([
  3550. '{$mode objfpc}',
  3551. 'const',
  3552. ' LoByte1 = Lo(Word($1234));',
  3553. ' HiByte1 = Hi(Word($1234));',
  3554. ' LoByte2 = Lo(SmallInt($1234));',
  3555. ' HiByte2 = Hi(SmallInt($1234));',
  3556. ' LoWord1 = Lo($1234CDEF);',
  3557. ' HiWord1 = Hi($1234CDEF);',
  3558. ' LoWord2 = Lo(-$1234CDEF);',
  3559. ' HiWord2 = Hi(-$1234CDEF);',
  3560. ' lo4:byte=lo(byte($34));',
  3561. ' hi4:byte=hi(byte($34));',
  3562. ' lo5:byte=lo(shortint(-$34));',
  3563. ' hi5:byte=hi(shortint(-$34));',
  3564. ' lo6:longword=lo($123456789ABCD);',
  3565. ' hi6:longword=hi($123456789ABCD);',
  3566. ' lo7:longword=lo(-$123456789ABCD);',
  3567. ' hi7:longword=hi(-$123456789ABCD);',
  3568. 'var',
  3569. ' b: Byte;',
  3570. ' ss: shortint;',
  3571. ' w: Word;',
  3572. ' si: SmallInt;',
  3573. ' lw: LongWord;',
  3574. ' li: LongInt;',
  3575. ' b2: Byte;',
  3576. ' ni: nativeint;',
  3577. 'begin',
  3578. ' w := $1234;',
  3579. ' ss := -$12;',
  3580. ' b := lo(ss);',
  3581. ' b := HI(ss);',
  3582. ' b := lo(w);',
  3583. ' b := HI(w);',
  3584. ' b2 := lo(b);',
  3585. ' b2 := hi(b);',
  3586. ' lw := $1234CDEF;',
  3587. ' w := lo(lw);',
  3588. ' w := hi(lw);',
  3589. ' ni := $123456789ABCD;',
  3590. ' lw := lo(ni);',
  3591. ' lw := hi(ni);',
  3592. '']);
  3593. ConvertProgram;
  3594. CheckSource('TestLoHiFpcMode',
  3595. LinesToStr([ // statements
  3596. 'this.LoByte1 = 0x1234 & 0xFF;',
  3597. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3598. 'this.LoByte2 = 0x1234 & 0xFF;',
  3599. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3600. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  3601. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  3602. 'this.LoWord2 = -0x1234CDEF >>> 0;',
  3603. 'this.HiWord2 = Math.floor(-0x1234CDEF / 4294967296) >>> 0;',
  3604. 'this.lo4 = 0x34 & 0xF;',
  3605. 'this.hi4 = (0x34 >> 4) & 0xF;',
  3606. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  3607. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  3608. 'this.lo6 = 0x123456789ABCD >>> 0;',
  3609. 'this.hi6 = 74565 >>> 0;',
  3610. 'this.lo7 = -0x123456789ABCD >>> 0;',
  3611. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  3612. 'this.b = 0;',
  3613. 'this.ss = 0;',
  3614. 'this.w = 0;',
  3615. 'this.si = 0;',
  3616. 'this.lw = 0;',
  3617. 'this.li = 0;',
  3618. 'this.b2 = 0;',
  3619. 'this.ni = 0;',
  3620. '']),
  3621. LinesToStr([ // this.$main
  3622. '$mod.w = 0x1234;',
  3623. '$mod.ss = -0x12;',
  3624. '$mod.b = $mod.ss & 0xFF;',
  3625. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  3626. '$mod.b = $mod.w & 0xFF;',
  3627. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3628. '$mod.b2 = $mod.b & 0xF;',
  3629. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  3630. '$mod.lw = 0x1234CDEF;',
  3631. '$mod.w = $mod.lw & 0xFFFF;',
  3632. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  3633. '$mod.ni = 0x123456789ABCD;',
  3634. '$mod.lw = $mod.ni >>> 0;',
  3635. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  3636. '']));
  3637. end;
  3638. procedure TTestModule.TestLoHiDelphiMode;
  3639. begin
  3640. StartProgram(false);
  3641. Add([
  3642. '{$mode delphi}',
  3643. 'const',
  3644. ' LoByte1 = Lo(Word($1234));',
  3645. ' HiByte1 = Hi(Word($1234));',
  3646. ' LoByte2 = Lo(SmallInt($1234));',
  3647. ' HiByte2 = Hi(SmallInt($1234));',
  3648. ' LoByte3 = Lo($1234CDEF);',
  3649. ' HiByte3 = Hi($1234CDEF);',
  3650. ' LoByte4 = Lo(-$1234CDEF);',
  3651. ' HiByte4 = Hi(-$1234CDEF);',
  3652. 'var',
  3653. ' b: Byte;',
  3654. ' w: Word;',
  3655. ' si: SmallInt;',
  3656. ' lw: LongWord;',
  3657. ' li: LongInt;',
  3658. 'begin',
  3659. ' w := $1234;',
  3660. ' b := lo(w);',
  3661. ' b := HI(w);',
  3662. ' lw := $1234CDEF;',
  3663. ' b := lo(lw);',
  3664. ' b := hi(lw);',
  3665. '']);
  3666. ConvertProgram;
  3667. CheckSource('TestLoHiDelphiMode',
  3668. LinesToStr([ // statements
  3669. 'this.LoByte1 = 0x1234 & 0xFF;',
  3670. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3671. 'this.LoByte2 = 0x1234 & 0xFF;',
  3672. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3673. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  3674. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  3675. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  3676. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  3677. 'this.b = 0;',
  3678. 'this.w = 0;',
  3679. 'this.si = 0;',
  3680. 'this.lw = 0;',
  3681. 'this.li = 0;'
  3682. ]),
  3683. LinesToStr([ // this.$main
  3684. '$mod.w = 0x1234;',
  3685. '$mod.b = $mod.w & 0xFF;',
  3686. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3687. '$mod.lw = 0x1234CDEF;',
  3688. '$mod.b = $mod.lw & 0xFF;',
  3689. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  3690. ]));
  3691. end;
  3692. procedure TTestModule.TestAssignments;
  3693. begin
  3694. StartProgram(false);
  3695. Parser.Options:=Parser.Options+[po_cassignments];
  3696. Add('var');
  3697. Add(' Bar:longint;');
  3698. Add('begin');
  3699. Add(' bar:=3;');
  3700. Add(' bar+=4;');
  3701. Add(' bar-=5;');
  3702. Add(' bar*=6;');
  3703. ConvertProgram;
  3704. CheckSource('TestAssignments',
  3705. LinesToStr([ // statements
  3706. 'this.Bar = 0;'
  3707. ]),
  3708. LinesToStr([ // this.$main
  3709. '$mod.Bar=3;',
  3710. '$mod.Bar+=4;',
  3711. '$mod.Bar-=5;',
  3712. '$mod.Bar*=6;'
  3713. ]));
  3714. end;
  3715. procedure TTestModule.TestArithmeticOperators1;
  3716. begin
  3717. StartProgram(false);
  3718. Add('var');
  3719. Add(' vA,vB,vC:longint;');
  3720. Add('begin');
  3721. Add(' va:=1;');
  3722. Add(' vb:=va+va;');
  3723. Add(' vb:=va div vb;');
  3724. Add(' vb:=va mod vb;');
  3725. Add(' vb:=va+va*vb+va div vb;');
  3726. Add(' vc:=-va;');
  3727. Add(' va:=va-vb;');
  3728. Add(' vb:=va;');
  3729. Add(' if va<vb then vc:=va else vc:=vb;');
  3730. ConvertProgram;
  3731. CheckSource('TestArithmeticOperators1',
  3732. LinesToStr([ // statements
  3733. 'this.vA = 0;',
  3734. 'this.vB = 0;',
  3735. 'this.vC = 0;'
  3736. ]),
  3737. LinesToStr([ // this.$main
  3738. '$mod.vA = 1;',
  3739. '$mod.vB = $mod.vA + $mod.vA;',
  3740. '$mod.vB = rtl.trunc($mod.vA / $mod.vB);',
  3741. '$mod.vB = $mod.vA % $mod.vB;',
  3742. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + rtl.trunc($mod.vA / $mod.vB);',
  3743. '$mod.vC = -$mod.vA;',
  3744. '$mod.vA = $mod.vA - $mod.vB;',
  3745. '$mod.vB = $mod.vA;',
  3746. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  3747. ]));
  3748. end;
  3749. procedure TTestModule.TestMultiAdd;
  3750. begin
  3751. StartProgram(false);
  3752. Add([
  3753. 'function Fly: string; external name ''fly'';',
  3754. 'function TryEncodeDate(Year, Month, Day: Word): Boolean;',
  3755. 'var',
  3756. ' Date: double;',
  3757. 'begin',
  3758. ' Result:=(Year>0) and (Year<10000) and',
  3759. ' (Month >= 1) and (Month<=12) and',
  3760. ' (Day>0) and (Day<=31);',
  3761. ' Date := (146097*Year) SHR 2 + (1461*Year) SHR 2 + (153*LongWord(Month)+2) DIV 5 + LongWord(Day);',
  3762. 'end;',
  3763. 'var s: string;',
  3764. 'begin',
  3765. ' s:=''a''+''b''+''c''+''d'';',
  3766. ' s:=s+Fly+''e'';',
  3767. ' s:=Fly+Fly+Fly;',
  3768. '']);
  3769. ConvertProgram;
  3770. CheckSource('TestMultiAdd',
  3771. LinesToStr([ // statements
  3772. 'this.TryEncodeDate = function (Year, Month, Day) {',
  3773. ' var Result = false;',
  3774. ' var date = 0.0;',
  3775. ' Result = (Year > 0) && (Year < 10000) && (Month >= 1) && (Month <= 12) && (Day > 0) && (Day <= 31);',
  3776. ' date = ((146097 * Year) >>> 2) + ((1461 * Year) >>> 2) + rtl.trunc(((153 * Month) + 2) / 5) + Day;',
  3777. ' return Result;',
  3778. '};',
  3779. 'this.s = "";',
  3780. '']),
  3781. LinesToStr([ // this.$main
  3782. '$mod.s = "a" + "b" + "c" + "d";',
  3783. '$mod.s = $mod.s + fly() + "e";',
  3784. '$mod.s = fly() + fly() + fly();',
  3785. '']));
  3786. end;
  3787. procedure TTestModule.TestLogicalOperators;
  3788. begin
  3789. StartProgram(false);
  3790. Add('var');
  3791. Add(' vA,vB,vC:boolean;');
  3792. Add('begin');
  3793. Add(' va:=vb and vc;');
  3794. Add(' va:=vb or vc;');
  3795. Add(' va:=vb xor vc;');
  3796. Add(' va:=true and vc;');
  3797. Add(' va:=(vb and vc) or (va and vb);');
  3798. Add(' va:=not vb;');
  3799. ConvertProgram;
  3800. CheckSource('TestLogicalOperators',
  3801. LinesToStr([ // statements
  3802. 'this.vA = false;',
  3803. 'this.vB = false;',
  3804. 'this.vC = false;'
  3805. ]),
  3806. LinesToStr([ // this.$main
  3807. '$mod.vA = $mod.vB && $mod.vC;',
  3808. '$mod.vA = $mod.vB || $mod.vC;',
  3809. '$mod.vA = $mod.vB ^ $mod.vC;',
  3810. '$mod.vA = true && $mod.vC;',
  3811. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3812. '$mod.vA = !$mod.vB;'
  3813. ]));
  3814. end;
  3815. procedure TTestModule.TestBitwiseOperators;
  3816. begin
  3817. StartProgram(false);
  3818. Add([
  3819. 'var',
  3820. ' vA,vB,vC:longint;',
  3821. ' X,Y,Z: nativeint;',
  3822. 'begin',
  3823. ' va:=vb and vc;',
  3824. ' va:=vb or vc;',
  3825. ' va:=vb xor vc;',
  3826. ' va:=vb shl vc;',
  3827. ' va:=vb shr vc;',
  3828. ' va:=3 and vc;',
  3829. ' va:=(vb and vc) or (va and vb);',
  3830. ' va:=not vb;',
  3831. ' X:=Y and Z;',
  3832. ' X:=Y and va;',
  3833. ' X:=Y or Z;',
  3834. ' X:=Y or va;',
  3835. ' X:=Y xor Z;',
  3836. ' X:=Y xor va;',
  3837. '']);
  3838. ConvertProgram;
  3839. CheckSource('TestBitwiseOperators',
  3840. LinesToStr([ // statements
  3841. 'this.vA = 0;',
  3842. 'this.vB = 0;',
  3843. 'this.vC = 0;',
  3844. 'this.X = 0;',
  3845. 'this.Y = 0;',
  3846. 'this.Z = 0;',
  3847. '']),
  3848. LinesToStr([ // this.$main
  3849. '$mod.vA = $mod.vB & $mod.vC;',
  3850. '$mod.vA = $mod.vB | $mod.vC;',
  3851. '$mod.vA = $mod.vB ^ $mod.vC;',
  3852. '$mod.vA = $mod.vB << $mod.vC;',
  3853. '$mod.vA = $mod.vB >>> $mod.vC;',
  3854. '$mod.vA = 3 & $mod.vC;',
  3855. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3856. '$mod.vA = ~$mod.vB;',
  3857. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3858. '$mod.X = $mod.Y & $mod.vA;',
  3859. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3860. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3861. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3862. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3863. '']));
  3864. end;
  3865. procedure TTestModule.TestBitwiseOperatorsLongword;
  3866. begin
  3867. StartProgram(false);
  3868. Add([
  3869. 'var',
  3870. ' a,b,c:longword;',
  3871. ' i: longint;',
  3872. 'begin',
  3873. ' a:=$12345678;',
  3874. ' b:=$EDCBA987;',
  3875. ' c:=not a;',
  3876. ' c:=a and b;',
  3877. ' c:=a and $ffff0000;',
  3878. ' c:=a or b;',
  3879. ' c:=a or $ff00ff00;',
  3880. ' c:=a xor b;',
  3881. ' c:=a xor $f0f0f0f0;',
  3882. ' c:=a shl 1;',
  3883. ' c:=a shl 16;',
  3884. ' c:=a shl 24;',
  3885. ' c:=a shl b;',
  3886. ' c:=a shr 1;',
  3887. ' c:=a shr 16;',
  3888. ' c:=a shr 24;',
  3889. ' c:=a shr b;',
  3890. ' c:=(b and c) or (a and b);',
  3891. ' c:=i and a;',
  3892. ' c:=i or a;',
  3893. ' c:=i xor a;',
  3894. '']);
  3895. ConvertProgram;
  3896. CheckSource('TestBitwiseOperatorsLongword',
  3897. LinesToStr([ // statements
  3898. 'this.a = 0;',
  3899. 'this.b = 0;',
  3900. 'this.c = 0;',
  3901. 'this.i = 0;',
  3902. '']),
  3903. LinesToStr([ // this.$main
  3904. '$mod.a = 0x12345678;',
  3905. '$mod.b = 0xEDCBA987;',
  3906. '$mod.c = rtl.lw(~$mod.a);',
  3907. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3908. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3909. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3910. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3911. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3912. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3913. '$mod.c = rtl.lw($mod.a << 1);',
  3914. '$mod.c = rtl.lw($mod.a << 16);',
  3915. '$mod.c = rtl.lw($mod.a << 24);',
  3916. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3917. '$mod.c = rtl.lw($mod.a >>> 1);',
  3918. '$mod.c = rtl.lw($mod.a >>> 16);',
  3919. '$mod.c = rtl.lw($mod.a >>> 24);',
  3920. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3921. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3922. '$mod.c = $mod.i & $mod.a;',
  3923. '$mod.c = $mod.i | $mod.a;',
  3924. '$mod.c = $mod.i ^ $mod.a;',
  3925. '']));
  3926. end;
  3927. procedure TTestModule.TestPrgProcVar;
  3928. begin
  3929. StartProgram(false);
  3930. Add('procedure Proc1;');
  3931. Add('type');
  3932. Add(' t1=longint;');
  3933. Add('var');
  3934. Add(' vA:t1;');
  3935. Add('begin');
  3936. Add('end;');
  3937. Add('begin');
  3938. ConvertProgram;
  3939. CheckSource('TestPrgProcVar',
  3940. LinesToStr([ // statements
  3941. 'this.Proc1 = function () {',
  3942. ' var vA=0;',
  3943. '};'
  3944. ]),
  3945. LinesToStr([ // this.$main
  3946. ''
  3947. ]));
  3948. end;
  3949. procedure TTestModule.TestUnitProcVar;
  3950. begin
  3951. StartUnit(false);
  3952. Add('interface');
  3953. Add('');
  3954. Add('type tA=string; // unit scope');
  3955. Add('procedure Proc1;');
  3956. Add('');
  3957. Add('implementation');
  3958. Add('');
  3959. Add('procedure Proc1;');
  3960. Add('type tA=longint; // local proc scope');
  3961. Add('var v1:tA; // using local tA');
  3962. Add('begin');
  3963. Add('end;');
  3964. Add('var v2:tA; // using interface tA');
  3965. ConvertUnit;
  3966. CheckSource('TestUnitProcVar',
  3967. LinesToStr([ // statements
  3968. 'var $impl = $mod.$impl;',
  3969. 'this.Proc1 = function () {',
  3970. ' var v1 = 0;',
  3971. '};',
  3972. '']),
  3973. // this.$init
  3974. '',
  3975. // implementation
  3976. LinesToStr([
  3977. '$impl.v2 = "";',
  3978. '']));
  3979. end;
  3980. procedure TTestModule.TestImplProc;
  3981. begin
  3982. StartUnit(false);
  3983. Add('interface');
  3984. Add('');
  3985. Add('procedure Proc1;');
  3986. Add('');
  3987. Add('implementation');
  3988. Add('');
  3989. Add('procedure Proc1; begin end;');
  3990. Add('procedure Proc2; begin end;');
  3991. Add('initialization');
  3992. Add(' Proc1;');
  3993. Add(' Proc2;');
  3994. ConvertUnit;
  3995. CheckSource('TestImplProc',
  3996. LinesToStr([ // statements
  3997. 'var $impl = $mod.$impl;',
  3998. 'this.Proc1 = function () {',
  3999. '};',
  4000. '']),
  4001. LinesToStr([ // this.$init
  4002. '$mod.Proc1();',
  4003. '$impl.Proc2();',
  4004. '']),
  4005. LinesToStr([ // implementation
  4006. '$impl.Proc2 = function () {',
  4007. '};',
  4008. ''])
  4009. );
  4010. end;
  4011. procedure TTestModule.TestFunctionResult;
  4012. begin
  4013. StartProgram(false);
  4014. Add('function Func1: longint;');
  4015. Add('begin');
  4016. Add(' Result:=3;');
  4017. Add(' Func1:=4;');
  4018. Add('end;');
  4019. Add('begin');
  4020. ConvertProgram;
  4021. CheckSource('TestFunctionResult',
  4022. LinesToStr([ // statements
  4023. 'this.Func1 = function () {',
  4024. ' var Result = 0;',
  4025. ' Result = 3;',
  4026. ' Result = 4;',
  4027. ' return Result;',
  4028. '};'
  4029. ]),
  4030. '');
  4031. end;
  4032. procedure TTestModule.TestNestedProc;
  4033. begin
  4034. StartProgram(false);
  4035. Add([
  4036. 'var vInUnit: longint;',
  4037. 'function DoIt(pA,pD: longint): longint;',
  4038. 'var',
  4039. ' vB: longint;',
  4040. ' vC: longint;',
  4041. ' function Nesty(pA: longint): longint; ',
  4042. ' var vB: longint;',
  4043. ' begin',
  4044. ' Result:=pa+vb+vc+pd+vInUnit;',
  4045. ' nesty:=3;',
  4046. ' doit:=4;',
  4047. ' exit;',
  4048. ' end;',
  4049. 'begin',
  4050. ' Result:=pa+vb+vc;',
  4051. ' doit:=6;',
  4052. ' exit;',
  4053. 'end;',
  4054. 'begin']);
  4055. ConvertProgram;
  4056. CheckSource('TestNestedProc',
  4057. LinesToStr([ // statements
  4058. 'this.vInUnit = 0;',
  4059. 'this.DoIt = function (pA, pD) {',
  4060. ' var Result = 0;',
  4061. ' var vB = 0;',
  4062. ' var vC = 0;',
  4063. ' function Nesty(pA) {',
  4064. ' var Result$1 = 0;',
  4065. ' var vB = 0;',
  4066. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  4067. ' Result$1 = 3;',
  4068. ' Result = 4;',
  4069. ' return Result$1;',
  4070. ' return Result$1;',
  4071. ' };',
  4072. ' Result = pA + vB + vC;',
  4073. ' Result = 6;',
  4074. ' return Result;',
  4075. ' return Result;',
  4076. '};'
  4077. ]),
  4078. '');
  4079. end;
  4080. procedure TTestModule.TestNestedProc_ResultString;
  4081. begin
  4082. StartProgram(false);
  4083. Add([
  4084. 'function DoIt: string;',
  4085. ' function Nesty: string; ',
  4086. ' begin',
  4087. ' nesty:=#65#66;',
  4088. ' nesty[1]:=#67;',
  4089. ' doit:=#68;',
  4090. ' doit[2]:=#69;',
  4091. ' end;',
  4092. 'begin',
  4093. ' doit:=#70;',
  4094. ' doit[3]:=#71;',
  4095. 'end;',
  4096. 'begin']);
  4097. ConvertProgram;
  4098. CheckSource('TestNestedProc_ResultString',
  4099. LinesToStr([ // statements
  4100. 'this.DoIt = function () {',
  4101. ' var Result = "";',
  4102. ' function Nesty() {',
  4103. ' var Result$1 = "";',
  4104. ' Result$1 = "AB";',
  4105. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  4106. ' Result = "D";',
  4107. ' Result = rtl.setCharAt(Result, 1, "E");',
  4108. ' return Result$1;',
  4109. ' };',
  4110. ' Result = "F";',
  4111. ' Result = rtl.setCharAt(Result, 2, "G");',
  4112. ' return Result;',
  4113. '};'
  4114. ]),
  4115. '');
  4116. end;
  4117. procedure TTestModule.TestForwardProc;
  4118. begin
  4119. StartProgram(false);
  4120. Add('procedure FuncA(Bar: longint); forward;');
  4121. Add('procedure FuncB(Bar: longint);');
  4122. Add('begin');
  4123. Add(' funca(bar);');
  4124. Add('end;');
  4125. Add('procedure funca(bar: longint);');
  4126. Add('begin');
  4127. Add(' if bar=3 then ;');
  4128. Add('end;');
  4129. Add('begin');
  4130. Add(' funca(4);');
  4131. Add(' funcb(5);');
  4132. ConvertProgram;
  4133. CheckSource('TestForwardProc',
  4134. LinesToStr([ // statements'
  4135. 'this.FuncB = function (Bar) {',
  4136. ' $mod.FuncA(Bar);',
  4137. '};',
  4138. 'this.FuncA = function (Bar) {',
  4139. ' if (Bar === 3);',
  4140. '};'
  4141. ]),
  4142. LinesToStr([
  4143. '$mod.FuncA(4);',
  4144. '$mod.FuncB(5);'
  4145. ])
  4146. );
  4147. end;
  4148. procedure TTestModule.TestNestedForwardProc;
  4149. begin
  4150. StartProgram(false);
  4151. Add('procedure FuncA;');
  4152. Add(' procedure FuncB(i: longint); forward;');
  4153. Add(' procedure FuncC(i: longint);');
  4154. Add(' begin');
  4155. Add(' funcb(i);');
  4156. Add(' end;');
  4157. Add(' procedure FuncB(i: longint);');
  4158. Add(' begin');
  4159. Add(' if i=3 then ;');
  4160. Add(' end;');
  4161. Add('begin');
  4162. Add(' funcc(4)');
  4163. Add('end;');
  4164. Add('begin');
  4165. Add(' funca;');
  4166. ConvertProgram;
  4167. CheckSource('TestNestedForwardProc',
  4168. LinesToStr([ // statements'
  4169. 'this.FuncA = function () {',
  4170. ' function FuncC(i) {',
  4171. ' FuncB(i);',
  4172. ' };',
  4173. ' function FuncB(i) {',
  4174. ' if (i === 3);',
  4175. ' };',
  4176. ' FuncC(4);',
  4177. '};'
  4178. ]),
  4179. LinesToStr([
  4180. '$mod.FuncA();'
  4181. ])
  4182. );
  4183. end;
  4184. procedure TTestModule.TestAssignFunctionResult;
  4185. begin
  4186. StartProgram(false);
  4187. Add('function Func1: longint;');
  4188. Add('begin');
  4189. Add('end;');
  4190. Add('var i: longint;');
  4191. Add('begin');
  4192. Add(' i:=func1();');
  4193. Add(' i:=func1()+func1();');
  4194. ConvertProgram;
  4195. CheckSource('TestAssignFunctionResult',
  4196. LinesToStr([ // statements
  4197. 'this.Func1 = function () {',
  4198. ' var Result = 0;',
  4199. ' return Result;',
  4200. '};',
  4201. 'this.i = 0;'
  4202. ]),
  4203. LinesToStr([
  4204. '$mod.i = $mod.Func1();',
  4205. '$mod.i = $mod.Func1() + $mod.Func1();'
  4206. ]));
  4207. end;
  4208. procedure TTestModule.TestFunctionResultInCondition;
  4209. begin
  4210. StartProgram(false);
  4211. Add('function Func1: longint;');
  4212. Add('begin');
  4213. Add('end;');
  4214. Add('function Func2: boolean;');
  4215. Add('begin');
  4216. Add('end;');
  4217. Add('var i: longint;');
  4218. Add('begin');
  4219. Add(' if func2 then ;');
  4220. Add(' if i=func1() then ;');
  4221. Add(' if i=func1 then ;');
  4222. ConvertProgram;
  4223. CheckSource('TestFunctionResultInCondition',
  4224. LinesToStr([ // statements
  4225. 'this.Func1 = function () {',
  4226. ' var Result = 0;',
  4227. ' return Result;',
  4228. '};',
  4229. 'this.Func2 = function () {',
  4230. ' var Result = false;',
  4231. ' return Result;',
  4232. '};',
  4233. 'this.i = 0;'
  4234. ]),
  4235. LinesToStr([
  4236. 'if ($mod.Func2());',
  4237. 'if ($mod.i === $mod.Func1());',
  4238. 'if ($mod.i === $mod.Func1());'
  4239. ]));
  4240. end;
  4241. procedure TTestModule.TestFunctionResultInForLoop;
  4242. begin
  4243. StartProgram(false);
  4244. Add([
  4245. 'function Func1(a: array of longint): longint;',
  4246. 'begin',
  4247. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  4248. ' for Result in a do if a[Result]=0 then exit;',
  4249. 'end;',
  4250. 'begin',
  4251. ' Func1([1,2,3])']);
  4252. ConvertProgram;
  4253. CheckSource('TestFunctionResultInForLoop',
  4254. LinesToStr([ // statements
  4255. 'this.Func1 = function (a) {',
  4256. ' var Result = 0;',
  4257. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  4258. ' Result = $l;',
  4259. ' if (a[Result] === 0) return Result;',
  4260. ' };',
  4261. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  4262. ' Result = $in[$l1];',
  4263. ' if (a[Result] === 0) return Result;',
  4264. ' };',
  4265. ' return Result;',
  4266. '};',
  4267. '']),
  4268. LinesToStr([
  4269. '$mod.Func1([1, 2, 3]);'
  4270. ]));
  4271. end;
  4272. procedure TTestModule.TestFunctionResultInTypeCast;
  4273. begin
  4274. StartProgram(false);
  4275. Add([
  4276. 'function GetInt: longint;',
  4277. 'begin',
  4278. 'end;',
  4279. 'begin',
  4280. ' if Byte(GetInt)=0 then ;',
  4281. '']);
  4282. ConvertProgram;
  4283. CheckSource('TestFunctionResultInTypeCast',
  4284. LinesToStr([ // statements
  4285. 'this.GetInt = function () {',
  4286. ' var Result = 0;',
  4287. ' return Result;',
  4288. '};',
  4289. '']),
  4290. LinesToStr([
  4291. 'if (($mod.GetInt() & 255) === 0) ;'
  4292. ]));
  4293. end;
  4294. procedure TTestModule.TestExit;
  4295. begin
  4296. StartProgram(false);
  4297. Add('procedure ProcA;');
  4298. Add('begin');
  4299. Add(' exit;');
  4300. Add('end;');
  4301. Add('function FuncB: longint;');
  4302. Add('begin');
  4303. Add(' exit;');
  4304. Add(' exit(3);');
  4305. Add('end;');
  4306. Add('function FuncC: string;');
  4307. Add('begin');
  4308. Add(' exit;');
  4309. Add(' exit(''a'');');
  4310. Add(' exit(''abc'');');
  4311. Add('end;');
  4312. Add('begin');
  4313. Add(' exit;');
  4314. Add(' exit(1);');
  4315. ConvertProgram;
  4316. CheckSource('TestExit',
  4317. LinesToStr([ // statements
  4318. 'this.ProcA = function () {',
  4319. ' return;',
  4320. '};',
  4321. 'this.FuncB = function () {',
  4322. ' var Result = 0;',
  4323. ' return Result;',
  4324. ' return 3;',
  4325. ' return Result;',
  4326. '};',
  4327. 'this.FuncC = function () {',
  4328. ' var Result = "";',
  4329. ' return Result;',
  4330. ' return "a";',
  4331. ' return "abc";',
  4332. ' return Result;',
  4333. '};'
  4334. ]),
  4335. LinesToStr([
  4336. 'return;',
  4337. 'return 1;',
  4338. '']));
  4339. end;
  4340. procedure TTestModule.TestExit_ResultInFinally;
  4341. begin
  4342. StartProgram(false);
  4343. Add([
  4344. 'function Run: word;',
  4345. 'begin',
  4346. ' try',
  4347. ' exit(3);', // no Result in finally -> use return 3
  4348. ' finally',
  4349. ' end;',
  4350. 'end;',
  4351. 'function Fly: word;',
  4352. 'begin',
  4353. ' try',
  4354. ' exit(3);',
  4355. ' finally',
  4356. ' if Result>0 then ;',
  4357. ' end;',
  4358. 'end;',
  4359. 'function Jump: word;',
  4360. 'begin',
  4361. ' try',
  4362. ' try',
  4363. ' exit(4);',
  4364. ' finally',
  4365. ' end;',
  4366. ' finally',
  4367. ' if Result>0 then ;',
  4368. ' end;',
  4369. 'end;',
  4370. 'begin',
  4371. '']);
  4372. ConvertProgram;
  4373. CheckSource('TestExit_ResultInFinally',
  4374. LinesToStr([ // statements
  4375. 'this.Run = function () {',
  4376. ' var Result = 0;',
  4377. ' try {',
  4378. ' return 3;',
  4379. ' } finally {',
  4380. ' };',
  4381. ' return Result;',
  4382. '};',
  4383. 'this.Fly = function () {',
  4384. ' var Result = 0;',
  4385. ' try {',
  4386. ' Result = 3;',
  4387. ' return Result;',
  4388. ' } finally {',
  4389. ' if (Result > 0) ;',
  4390. ' };',
  4391. ' return Result;',
  4392. '};',
  4393. 'this.Jump = function () {',
  4394. ' var Result = 0;',
  4395. ' try {',
  4396. ' try {',
  4397. ' Result = 4;',
  4398. ' return Result;',
  4399. ' } finally {',
  4400. ' };',
  4401. ' } finally {',
  4402. ' if (Result > 0) ;',
  4403. ' };',
  4404. ' return Result;',
  4405. '};',
  4406. '']),
  4407. LinesToStr([
  4408. '']));
  4409. end;
  4410. procedure TTestModule.TestBreak;
  4411. begin
  4412. StartProgram(false);
  4413. Add([
  4414. 'var',
  4415. ' i: longint;',
  4416. 'begin',
  4417. ' repeat',
  4418. ' break;',
  4419. ' until true;',
  4420. ' while true do',
  4421. ' break;',
  4422. ' for i:=1 to 2 do',
  4423. ' break;']);
  4424. ConvertProgram;
  4425. CheckSource('TestBreak',
  4426. LinesToStr([ // statements
  4427. 'this.i = 0;'
  4428. ]),
  4429. LinesToStr([
  4430. 'do {',
  4431. ' break;',
  4432. '} while (!true);',
  4433. 'while (true) break;',
  4434. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  4435. '']));
  4436. end;
  4437. procedure TTestModule.TestBreakAsVar;
  4438. begin
  4439. StartProgram(false);
  4440. Add([
  4441. 'procedure DoIt(break: boolean);',
  4442. 'begin',
  4443. ' if break then ;',
  4444. 'end;',
  4445. 'var',
  4446. ' break: boolean;',
  4447. 'begin',
  4448. ' if break then ;']);
  4449. ConvertProgram;
  4450. CheckSource('TestBreakAsVar',
  4451. LinesToStr([ // statements
  4452. 'this.DoIt = function (Break) {',
  4453. ' if (Break) ;',
  4454. '};',
  4455. 'this.Break = false;',
  4456. '']),
  4457. LinesToStr([
  4458. 'if($mod.Break) ;',
  4459. '']));
  4460. end;
  4461. procedure TTestModule.TestContinue;
  4462. begin
  4463. StartProgram(false);
  4464. Add('var i: longint;');
  4465. Add('begin');
  4466. Add(' repeat');
  4467. Add(' continue;');
  4468. Add(' until true;');
  4469. Add(' while true do');
  4470. Add(' continue;');
  4471. Add(' for i:=1 to 2 do');
  4472. Add(' continue;');
  4473. ConvertProgram;
  4474. CheckSource('TestContinue',
  4475. LinesToStr([ // statements
  4476. 'this.i = 0;'
  4477. ]),
  4478. LinesToStr([
  4479. 'do {',
  4480. ' continue;',
  4481. '} while (!true);',
  4482. 'while (true) continue;',
  4483. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  4484. '']));
  4485. end;
  4486. procedure TTestModule.TestProc_External;
  4487. begin
  4488. StartProgram(false);
  4489. Add('procedure Foo; external name ''console.log'';');
  4490. Add('function Bar: longint; external name ''get.item'';');
  4491. Add('function Bla(s: string): longint; external name ''apply.something'';');
  4492. Add('var');
  4493. Add(' i: longint;');
  4494. Add('begin');
  4495. Add(' Foo;');
  4496. Add(' i:=Bar;');
  4497. Add(' i:=Bla(''abc'');');
  4498. ConvertProgram;
  4499. CheckSource('TestProc_External',
  4500. LinesToStr([ // statements
  4501. 'this.i = 0;'
  4502. ]),
  4503. LinesToStr([
  4504. 'console.log();',
  4505. '$mod.i = get.item();',
  4506. '$mod.i = apply.something("abc");'
  4507. ]));
  4508. end;
  4509. procedure TTestModule.TestProc_ExternalOtherUnit;
  4510. begin
  4511. AddModuleWithIntfImplSrc('unit2.pas',
  4512. LinesToStr([
  4513. 'procedure Now; external name ''Date.now'';',
  4514. 'procedure DoIt;'
  4515. ]),
  4516. 'procedure doit; begin end;');
  4517. StartUnit(true);
  4518. Add('interface');
  4519. Add('uses unit2;');
  4520. Add('implementation');
  4521. Add('begin');
  4522. Add(' now;');
  4523. Add(' now();');
  4524. Add(' uNit2.now;');
  4525. Add(' uNit2.now();');
  4526. Add(' doit;');
  4527. Add(' uNit2.doit;');
  4528. ConvertUnit;
  4529. CheckSource('TestProc_ExternalOtherUnit',
  4530. LinesToStr([
  4531. '']),
  4532. LinesToStr([
  4533. 'Date.now();',
  4534. 'Date.now();',
  4535. 'Date.now();',
  4536. 'Date.now();',
  4537. 'pas.unit2.DoIt();',
  4538. 'pas.unit2.DoIt();',
  4539. '']));
  4540. end;
  4541. procedure TTestModule.TestProc_Asm;
  4542. begin
  4543. StartProgram(false);
  4544. Add([
  4545. '{$mode delphi}',
  4546. 'function DoIt: longint;',
  4547. 'begin;',
  4548. ' asm',
  4549. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4550. ' end;',
  4551. ' asm console.log(); end;',
  4552. ' asm',
  4553. ' s = "'' ";',
  4554. ' s = ''" '';',
  4555. ' s = s + "world" + "''";',
  4556. ' // end',
  4557. ' s = ''end'';',
  4558. ' s = "end";',
  4559. ' s = "foo\"bar";',
  4560. ' s = ''a\''b'';',
  4561. ' s = `${expr}\`-"-''-`;',
  4562. ' s = `multi',
  4563. 'line`;',
  4564. ' end;',
  4565. 'end;',
  4566. 'procedure Fly;',
  4567. 'asm',
  4568. ' return;',
  4569. 'end;',
  4570. 'begin']);
  4571. ConvertProgram;
  4572. CheckSource('TestProc_Asm',
  4573. LinesToStr([ // statements
  4574. 'this.DoIt = function () {',
  4575. ' var Result = 0;',
  4576. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4577. ' console.log();',
  4578. ' s = "'' ";',
  4579. ' s = ''" '';',
  4580. ' s = s + "world" + "''";',
  4581. ' // end',
  4582. ' s = ''end'';',
  4583. ' s = "end";',
  4584. ' s = "foo\"bar";',
  4585. ' s = ''a\''b'';',
  4586. ' s = `${expr}\`-"-''-`;',
  4587. ' s = `multi',
  4588. 'line`;',
  4589. ' return Result;',
  4590. '};',
  4591. 'this.Fly = function () {',
  4592. ' return;',
  4593. '};',
  4594. '']),
  4595. LinesToStr([
  4596. ''
  4597. ]));
  4598. end;
  4599. procedure TTestModule.TestProc_AsmSubBlock;
  4600. begin
  4601. StartProgram(true,[supTObject]);
  4602. Add([
  4603. '{$mode delphi}',
  4604. 'type',
  4605. ' TBird = class end;',
  4606. 'procedure Run(w: word);',
  4607. 'begin;',
  4608. ' if true then asm console.log(); end;',
  4609. ' if w>3 then asm',
  4610. ' var a = w+1;',
  4611. ' w = a+3;',
  4612. ' end;',
  4613. ' while (w>7) do asm',
  4614. ' w+=3; w*=2;',
  4615. ' end;',
  4616. ' try',
  4617. ' except',
  4618. ' on E: TBird do',
  4619. ' asm console.log(E); end;',
  4620. ' on E: TObject do',
  4621. ' asm var i=3; i--; end;',
  4622. ' else asm Fly; High; end;',
  4623. ' end;',
  4624. 'end;',
  4625. 'begin']);
  4626. ConvertProgram;
  4627. CheckSource('TestProc_AsmSubBlock',
  4628. LinesToStr([ // statements
  4629. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  4630. '});',
  4631. 'this.Run = function (w) {',
  4632. ' if (true) console.log();',
  4633. ' if (w > 3) {',
  4634. ' var a = w+1;',
  4635. ' w = a+3;',
  4636. ' };',
  4637. ' while (w > 7) {',
  4638. ' w+=3; w*=2;',
  4639. ' };',
  4640. ' try {} catch ($e) {',
  4641. ' if ($mod.TBird.isPrototypeOf($e)) {',
  4642. ' var E = $e;',
  4643. ' console.log(E);',
  4644. ' } else if (pas.system.TObject.isPrototypeOf($e)) {',
  4645. ' var E = $e;',
  4646. ' var i=3; i--;',
  4647. ' } else {',
  4648. ' Fly; High;',
  4649. ' }',
  4650. ' };',
  4651. '};',
  4652. '']),
  4653. LinesToStr([
  4654. ''
  4655. ]));
  4656. end;
  4657. procedure TTestModule.TestProc_Assembler;
  4658. begin
  4659. StartProgram(false);
  4660. Add('function DoIt: longint; assembler;');
  4661. Add('asm');
  4662. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  4663. Add('end;');
  4664. Add('begin');
  4665. ConvertProgram;
  4666. CheckSource('TestProc_Assembler',
  4667. LinesToStr([ // statements
  4668. 'this.DoIt = function () {',
  4669. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4670. '};'
  4671. ]),
  4672. LinesToStr([
  4673. ''
  4674. ]));
  4675. end;
  4676. procedure TTestModule.TestProc_VarParam;
  4677. begin
  4678. StartProgram(false);
  4679. Add('type integer = longint;');
  4680. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  4681. Add('var vJ: integer;');
  4682. Add('begin');
  4683. Add(' vg:=vg+1;');
  4684. Add(' vj:=vh+2;');
  4685. Add(' vi:=vi+3;');
  4686. Add(' doit(vg,vg,vg);');
  4687. Add(' doit(vh,vh,vj);');
  4688. Add(' doit(vi,vi,vi);');
  4689. Add(' doit(vj,vj,vj);');
  4690. Add('end;');
  4691. Add('var i: integer;');
  4692. Add('begin');
  4693. Add(' doit(i,i,i);');
  4694. ConvertProgram;
  4695. CheckSource('TestProc_VarParam',
  4696. LinesToStr([ // statements
  4697. 'this.DoIt = function (vG,vH,vI) {',
  4698. ' var vJ = 0;',
  4699. ' vG = vG + 1;',
  4700. ' vJ = vH + 2;',
  4701. ' vI.set(vI.get()+3);',
  4702. ' $mod.DoIt(vG, vG, {',
  4703. ' get: function () {',
  4704. ' return vG;',
  4705. ' },',
  4706. ' set: function (v) {',
  4707. ' vG = v;',
  4708. ' }',
  4709. ' });',
  4710. ' $mod.DoIt(vH, vH, {',
  4711. ' get: function () {',
  4712. ' return vJ;',
  4713. ' },',
  4714. ' set: function (v) {',
  4715. ' vJ = v;',
  4716. ' }',
  4717. ' });',
  4718. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4719. ' $mod.DoIt(vJ, vJ, {',
  4720. ' get: function () {',
  4721. ' return vJ;',
  4722. ' },',
  4723. ' set: function (v) {',
  4724. ' vJ = v;',
  4725. ' }',
  4726. ' });',
  4727. '};',
  4728. 'this.i = 0;'
  4729. ]),
  4730. LinesToStr([
  4731. '$mod.DoIt($mod.i,$mod.i,{',
  4732. ' p: $mod,',
  4733. ' get: function () {',
  4734. ' return this.p.i;',
  4735. ' },',
  4736. ' set: function (v) {',
  4737. ' this.p.i = v;',
  4738. ' }',
  4739. '});'
  4740. ]));
  4741. end;
  4742. procedure TTestModule.TestProc_VarParamString;
  4743. begin
  4744. StartProgram(false);
  4745. Add(['type TCaption = string;',
  4746. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  4747. 'var c: char;',
  4748. 'begin',
  4749. ' va[1]:=c;',
  4750. ' vb[2]:=c;',
  4751. ' vc[3]:=c;',
  4752. 'end;',
  4753. 'begin']);
  4754. ConvertProgram;
  4755. CheckSource('TestProc_VarParamString',
  4756. LinesToStr([ // statements
  4757. 'this.DoIt = function (vA,vB,vC) {',
  4758. ' var c = "\x00";',
  4759. ' vA = rtl.setCharAt(vA, 0, c);',
  4760. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  4761. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  4762. '};',
  4763. '']),
  4764. LinesToStr([
  4765. ]));
  4766. end;
  4767. procedure TTestModule.TestProc_VarParamV;
  4768. begin
  4769. StartProgram(false);
  4770. Add([
  4771. 'procedure Inc2(var i: longint);',
  4772. 'begin',
  4773. ' i:=i+2;',
  4774. 'end;',
  4775. 'procedure DoIt(v: longint);',
  4776. 'var p: array of longint;',
  4777. 'begin',
  4778. ' Inc2(v);',
  4779. ' Inc2(p[v]);',
  4780. 'end;',
  4781. 'begin']);
  4782. ConvertProgram;
  4783. CheckSource('TestProc_VarParamV',
  4784. LinesToStr([ // statements
  4785. 'this.Inc2 = function (i) {',
  4786. ' i.set(i.get()+2);',
  4787. '};',
  4788. 'this.DoIt = function (v) {',
  4789. ' var p = [];',
  4790. ' $mod.Inc2({get: function () {',
  4791. ' return v;',
  4792. ' }, set: function (w) {',
  4793. ' v = w;',
  4794. ' }});',
  4795. ' $mod.Inc2({',
  4796. ' a: v,',
  4797. ' p: p,',
  4798. ' get: function () {',
  4799. ' return this.p[this.a];',
  4800. ' },',
  4801. ' set: function (v) {',
  4802. ' this.p[this.a] = v;',
  4803. ' }',
  4804. ' });',
  4805. '};',
  4806. '']),
  4807. LinesToStr([
  4808. '']));
  4809. end;
  4810. procedure TTestModule.TestProc_Overload;
  4811. begin
  4812. StartProgram(false);
  4813. Add('procedure DoIt(vI: longint); begin end;');
  4814. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4815. Add('procedure DoIt(vD: double); begin end;');
  4816. Add('begin');
  4817. Add(' DoIt(1);');
  4818. Add(' DoIt(2,3);');
  4819. Add(' DoIt(4.5);');
  4820. ConvertProgram;
  4821. CheckSource('TestProcedureOverload',
  4822. LinesToStr([ // statements
  4823. 'this.DoIt = function (vI) {',
  4824. '};',
  4825. 'this.DoIt$1 = function (vI, vJ) {',
  4826. '};',
  4827. 'this.DoIt$2 = function (vD) {',
  4828. '};',
  4829. '']),
  4830. LinesToStr([
  4831. '$mod.DoIt(1);',
  4832. '$mod.DoIt$1(2, 3);',
  4833. '$mod.DoIt$2(4.5);',
  4834. '']));
  4835. end;
  4836. procedure TTestModule.TestProc_OverloadForward;
  4837. begin
  4838. StartProgram(false);
  4839. Add('procedure DoIt(vI: longint); forward;');
  4840. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4841. Add('procedure doit(vi: longint); begin end;');
  4842. Add('begin');
  4843. Add(' doit(1);');
  4844. Add(' doit(2,3);');
  4845. ConvertProgram;
  4846. CheckSource('TestProcedureOverloadForward',
  4847. LinesToStr([ // statements
  4848. 'this.DoIt$1 = function (vI, vJ) {',
  4849. '};',
  4850. 'this.DoIt = function (vI) {',
  4851. '};',
  4852. '']),
  4853. LinesToStr([
  4854. '$mod.DoIt(1);',
  4855. '$mod.DoIt$1(2, 3);',
  4856. '']));
  4857. end;
  4858. procedure TTestModule.TestProc_OverloadIntfImpl;
  4859. begin
  4860. StartUnit(false);
  4861. Add('interface');
  4862. Add('procedure DoIt(vI: longint);');
  4863. Add('procedure DoIt(vI, vJ: longint);');
  4864. Add('implementation');
  4865. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4866. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4867. Add('procedure DoIt(vi: longint); begin end;');
  4868. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4869. Add('procedure DoIt(vi, vj: longint); begin end;');
  4870. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4871. Add('begin');
  4872. Add(' doit(1);');
  4873. Add(' doit(2,3);');
  4874. Add(' doit(4,5,6);');
  4875. Add(' doit(7,8,9,10);');
  4876. Add(' doit(11,12,13,14,15);');
  4877. ConvertUnit;
  4878. CheckSource('TestProcedureOverloadUnit',
  4879. LinesToStr([ // statements
  4880. 'var $impl = $mod.$impl;',
  4881. 'this.DoIt = function (vI) {',
  4882. '};',
  4883. 'this.DoIt$1 = function (vI, vJ) {',
  4884. '};',
  4885. '']),
  4886. LinesToStr([ // this.$init
  4887. '$mod.DoIt(1);',
  4888. '$mod.DoIt$1(2, 3);',
  4889. '$impl.DoIt$3(4,5,6);',
  4890. '$impl.DoIt$4(7,8,9,10);',
  4891. '$impl.DoIt$2(11,12,13,14,15);',
  4892. '']),
  4893. LinesToStr([ // implementation
  4894. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4895. '};',
  4896. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4897. '};',
  4898. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4899. '};',
  4900. '']));
  4901. end;
  4902. procedure TTestModule.TestProc_OverloadNested;
  4903. begin
  4904. StartProgram(false);
  4905. Add([
  4906. 'procedure doit(vA: longint);',
  4907. ' procedure DoIt(vA, vB: longint); overload;',
  4908. ' begin',
  4909. ' doit(1);',
  4910. ' doit(1,2);',
  4911. ' end;',
  4912. ' procedure doit(vA, vB, vC: longint);',
  4913. ' begin',
  4914. ' doit(1);',
  4915. ' doit(1,2);',
  4916. ' doit(1,2,3);',
  4917. ' end;',
  4918. 'begin',
  4919. ' doit(1);',
  4920. ' doit(1,2);',
  4921. ' doit(1,2,3);',
  4922. 'end;',
  4923. 'begin // main',
  4924. ' doit(1);']);
  4925. ConvertProgram;
  4926. CheckSource('TestProcedureOverloadNested',
  4927. LinesToStr([ // statements
  4928. 'this.doit = function (vA) {',
  4929. ' function DoIt$1(vA, vB) {',
  4930. ' $mod.doit(1);',
  4931. ' DoIt$1(1, 2);',
  4932. ' };',
  4933. ' function doit$2(vA, vB, vC) {',
  4934. ' $mod.doit(1);',
  4935. ' DoIt$1(1, 2);',
  4936. ' doit$2(1, 2, 3);',
  4937. ' };',
  4938. ' $mod.doit(1);',
  4939. ' DoIt$1(1, 2);',
  4940. ' doit$2(1, 2, 3);',
  4941. '};',
  4942. '']),
  4943. LinesToStr([
  4944. '$mod.doit(1);',
  4945. '']));
  4946. end;
  4947. procedure TTestModule.TestProc_OverloadNestedForward;
  4948. begin
  4949. StartProgram(false);
  4950. Add([
  4951. 'procedure DoIt(vA: longint); overload; forward;',
  4952. 'procedure DoIt(vB, vC: longint); overload;',
  4953. 'begin // 2 param overload',
  4954. ' doit(1);',
  4955. ' doit(1,2);',
  4956. 'end;',
  4957. 'procedure doit(vA: longint);',
  4958. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4959. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4960. ' begin // 4 param overload',
  4961. ' doit(1);',
  4962. ' doit(1,2);',
  4963. ' doit(1,2,3);',
  4964. ' doit(1,2,3,4);',
  4965. ' end;',
  4966. ' procedure doit(vA, vB, vC: longint);',
  4967. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4968. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4969. ' begin // 6 param overload',
  4970. ' doit(1);',
  4971. ' doit(1,2);',
  4972. ' doit(1,2,3);',
  4973. ' doit(1,2,3,4);',
  4974. ' doit(1,2,3,4,5);',
  4975. ' doit(1,2,3,4,5,6);',
  4976. ' end;',
  4977. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4978. ' begin // 5 param overload',
  4979. ' doit(1);',
  4980. ' doit(1,2);',
  4981. ' doit(1,2,3);',
  4982. ' doit(1,2,3,4);',
  4983. ' doit(1,2,3,4,5);',
  4984. ' doit(1,2,3,4,5,6);',
  4985. ' end;',
  4986. ' begin // 3 param overload',
  4987. ' doit(1);',
  4988. ' doit(1,2);',
  4989. ' doit(1,2,3);',
  4990. ' doit(1,2,3,4);',
  4991. ' doit(1,2,3,4,5);',
  4992. ' doit(1,2,3,4,5,6);',
  4993. ' end;',
  4994. 'begin // 1 param overload',
  4995. ' doit(1);',
  4996. ' doit(1,2);',
  4997. ' doit(1,2,3);',
  4998. ' doit(1,2,3,4);',
  4999. 'end;',
  5000. 'begin // main',
  5001. ' doit(1);',
  5002. ' doit(1,2);']);
  5003. ConvertProgram;
  5004. CheckSource('TestProc_OverloadNestedForward',
  5005. LinesToStr([ // statements
  5006. 'this.DoIt$1 = function (vB, vC) {',
  5007. ' $mod.DoIt(1);',
  5008. ' $mod.DoIt$1(1, 2);',
  5009. '};',
  5010. 'this.DoIt = function (vA) {',
  5011. ' function DoIt$3(vA, vB, vC, vD) {',
  5012. ' $mod.DoIt(1);',
  5013. ' $mod.DoIt$1(1, 2);',
  5014. ' DoIt$2(1, 2, 3);',
  5015. ' DoIt$3(1, 2, 3, 4);',
  5016. ' };',
  5017. ' function DoIt$2(vA, vB, vC) {',
  5018. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  5019. ' $mod.DoIt(1);',
  5020. ' $mod.DoIt$1(1, 2);',
  5021. ' DoIt$2(1, 2, 3);',
  5022. ' DoIt$3(1, 2, 3, 4);',
  5023. ' DoIt$4(1, 2, 3, 4, 5);',
  5024. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5025. ' };',
  5026. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  5027. ' $mod.DoIt(1);',
  5028. ' $mod.DoIt$1(1, 2);',
  5029. ' DoIt$2(1, 2, 3);',
  5030. ' DoIt$3(1, 2, 3, 4);',
  5031. ' DoIt$4(1, 2, 3, 4, 5);',
  5032. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5033. ' };',
  5034. ' $mod.DoIt(1);',
  5035. ' $mod.DoIt$1(1, 2);',
  5036. ' DoIt$2(1, 2, 3);',
  5037. ' DoIt$3(1, 2, 3, 4);',
  5038. ' DoIt$4(1, 2, 3, 4, 5);',
  5039. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5040. ' };',
  5041. ' $mod.DoIt(1);',
  5042. ' $mod.DoIt$1(1, 2);',
  5043. ' DoIt$2(1, 2, 3);',
  5044. ' DoIt$3(1, 2, 3, 4);',
  5045. '};',
  5046. '']),
  5047. LinesToStr([
  5048. '$mod.DoIt(1);',
  5049. '$mod.DoIt$1(1, 2);',
  5050. '']));
  5051. end;
  5052. procedure TTestModule.TestProc_OverloadUnitCycle;
  5053. begin
  5054. AddModuleWithIntfImplSrc('Unit2.pas',
  5055. LinesToStr([
  5056. 'type',
  5057. ' TObject = class',
  5058. ' procedure DoIt(b: boolean); virtual; abstract;',
  5059. ' procedure DoIt(i: longint); virtual; abstract;',
  5060. ' end;',
  5061. '']),
  5062. 'uses test1;');
  5063. StartUnit(true);
  5064. Add([
  5065. 'interface',
  5066. 'uses unit2;',
  5067. 'type',
  5068. ' TEagle = class(TObject)',
  5069. ' procedure DoIt(b: boolean); override;',
  5070. ' procedure DoIt(i: longint); override;',
  5071. ' end;',
  5072. 'implementation',
  5073. 'procedure TEagle.DoIt(b: boolean); begin end;',
  5074. 'procedure TEagle.DoIt(i: longint); begin end;',
  5075. '']);
  5076. ConvertUnit;
  5077. CheckSource('TestProc_OverloadUnitCycle',
  5078. LinesToStr([ // statements
  5079. 'rtl.createClass(this, "TEagle", pas.Unit2.TObject, function () {',
  5080. ' this.DoIt = function (b) {',
  5081. ' };',
  5082. ' this.DoIt$1 = function (i) {',
  5083. ' };',
  5084. '});',
  5085. '']),
  5086. '',
  5087. LinesToStr([
  5088. '']));
  5089. end;
  5090. procedure TTestModule.TestProc_Varargs;
  5091. begin
  5092. StartProgram(false);
  5093. Add([
  5094. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  5095. 'procedure ProcB; varargs; external name ''ProcB'';',
  5096. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  5097. 'function GetIt: longint; begin end;',
  5098. 'begin',
  5099. ' ProcA(1);',
  5100. ' ProcA(1,2);',
  5101. ' ProcA(1,2.0);',
  5102. ' ProcA(1,2,3);',
  5103. ' ProcA(1,''2'');',
  5104. ' ProcA(2,'''');',
  5105. ' ProcA(3,false);',
  5106. ' ProcB;',
  5107. ' ProcB();',
  5108. ' ProcB(4);',
  5109. ' ProcB(''foo'');',
  5110. ' ProcC;',
  5111. ' ProcC();',
  5112. ' ProcC(4);',
  5113. ' ProcC(5,''foo'');',
  5114. ' ProcB(GetIt);',
  5115. ' ProcB(GetIt());',
  5116. ' ProcB(GetIt,GetIt());']);
  5117. ConvertProgram;
  5118. CheckSource('TestProc_Varargs',
  5119. LinesToStr([ // statements
  5120. 'this.GetIt = function () {',
  5121. ' var Result = 0;',
  5122. ' return Result;',
  5123. '};',
  5124. '']),
  5125. LinesToStr([
  5126. 'ProcA(1);',
  5127. 'ProcA(1, 2);',
  5128. 'ProcA(1, 2.0);',
  5129. 'ProcA(1, 2, 3);',
  5130. 'ProcA(1, "2");',
  5131. 'ProcA(2, "");',
  5132. 'ProcA(3, false);',
  5133. 'ProcB();',
  5134. 'ProcB();',
  5135. 'ProcB(4);',
  5136. 'ProcB("foo");',
  5137. 'ProcC(17);',
  5138. 'ProcC(17);',
  5139. 'ProcC(4);',
  5140. 'ProcC(5, "foo");',
  5141. 'ProcB($mod.GetIt());',
  5142. 'ProcB($mod.GetIt());',
  5143. 'ProcB($mod.GetIt(), $mod.GetIt());',
  5144. '']));
  5145. end;
  5146. procedure TTestModule.TestProc_ConstOrder;
  5147. begin
  5148. StartProgram(false);
  5149. Add([
  5150. 'const A = 3;',
  5151. 'const B = A+1;',
  5152. 'procedure DoIt;',
  5153. 'const C = A+1;',
  5154. 'const D = B+1;',
  5155. 'const E = D+C+B+A;',
  5156. 'begin',
  5157. 'end;',
  5158. 'begin'
  5159. ]);
  5160. ConvertProgram;
  5161. CheckSource('TestProc_ConstOrder',
  5162. LinesToStr([ // statements
  5163. 'this.A = 3;',
  5164. 'this.B = 3 + 1;',
  5165. 'var C = 3 + 1;',
  5166. 'var D = 4 + 1;',
  5167. 'var E = 5 + 4 + 4 + 3;',
  5168. 'this.DoIt = function () {',
  5169. '};',
  5170. '']),
  5171. LinesToStr([
  5172. ''
  5173. ]));
  5174. end;
  5175. procedure TTestModule.TestProc_DuplicateConst;
  5176. begin
  5177. StartProgram(false);
  5178. Add([
  5179. 'const A = 1;',
  5180. 'procedure DoIt;',
  5181. 'const A = 2;',
  5182. ' procedure SubIt;',
  5183. ' const A = 21;',
  5184. ' begin',
  5185. ' end;',
  5186. 'begin',
  5187. 'end;',
  5188. 'procedure DoSome;',
  5189. 'const A = 3;',
  5190. 'begin',
  5191. 'end;',
  5192. 'begin'
  5193. ]);
  5194. ConvertProgram;
  5195. CheckSource('TestProc_DuplicateConst',
  5196. LinesToStr([ // statements
  5197. 'this.A = 1;',
  5198. 'var A$1 = 2;',
  5199. 'var A$2 = 21;',
  5200. 'this.DoIt = function () {',
  5201. ' function SubIt() {',
  5202. ' };',
  5203. '};',
  5204. 'var A$3 = 3;',
  5205. 'this.DoSome = function () {',
  5206. '};',
  5207. '']),
  5208. LinesToStr([
  5209. ''
  5210. ]));
  5211. end;
  5212. procedure TTestModule.TestProc_LocalVarAbsolute;
  5213. begin
  5214. StartProgram(false);
  5215. Add([
  5216. 'type',
  5217. ' TObject = class',
  5218. ' Index: longint;',
  5219. ' procedure DoAbs(Item: pointer);',
  5220. ' end;',
  5221. 'procedure TObject.DoAbs(Item: pointer);',
  5222. 'var',
  5223. ' o: TObject absolute Item;',
  5224. 'begin',
  5225. ' if o.Index<o.Index then o.Index:=o.Index;',
  5226. 'end;',
  5227. 'procedure DoIt(i: longint; p: pointer);',
  5228. 'var',
  5229. ' d: double absolute i;',
  5230. ' s: string absolute d;',
  5231. ' oi: TObject absolute i;',
  5232. ' op: TObject absolute p;',
  5233. 'begin',
  5234. ' if d=d then d:=d;',
  5235. ' if s=s then s:=s;',
  5236. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  5237. ' if op.Index=op.Index then op.Index:=op.Index;',
  5238. 'end;',
  5239. 'begin']);
  5240. ConvertProgram;
  5241. CheckSource('TestProc_LocalVarAbsolute',
  5242. LinesToStr([ // statements
  5243. 'rtl.createClass(this, "TObject", null, function () {',
  5244. ' this.$init = function () {',
  5245. ' this.Index = 0;',
  5246. ' };',
  5247. ' this.$final = function () {',
  5248. ' };',
  5249. ' this.DoAbs = function (Item) {',
  5250. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  5251. ' };',
  5252. '});',
  5253. 'this.DoIt = function (i, p) {',
  5254. ' if (i === i) i = i;',
  5255. ' if (i === i) i = i;',
  5256. ' if (i.Index < i.Index) i.Index = i.Index;',
  5257. ' if (p.Index === p.Index) p.Index = p.Index;',
  5258. '};'
  5259. ]),
  5260. LinesToStr([
  5261. ]));
  5262. end;
  5263. procedure TTestModule.TestProc_ResultAbsolute;
  5264. begin
  5265. StartProgram(false);
  5266. Add([
  5267. 'type',
  5268. ' TObject = class',
  5269. ' Index: longint;',
  5270. ' function DoAbs: pointer;',
  5271. ' end;',
  5272. 'function TObject.DoAbs: pointer;',
  5273. 'var',
  5274. ' o: TObject absolute Result;',
  5275. 'begin',
  5276. ' if o.Index<o.Index then o.Index:=o.Index;',
  5277. 'end;',
  5278. 'function DoIt: jsvalue;',
  5279. 'var',
  5280. ' d: double absolute Result;',
  5281. ' s: string absolute Result;',
  5282. ' o: TObject absolute Result;',
  5283. 'begin',
  5284. ' if d=d then d:=d;',
  5285. ' if s=s then s:=s;',
  5286. ' if o.Index<o.Index then o.Index:=o.Index;',
  5287. 'end;',
  5288. 'begin']);
  5289. ConvertProgram;
  5290. CheckSource('TestProc_ResultAbsolute',
  5291. LinesToStr([ // statements
  5292. 'rtl.createClass(this, "TObject", null, function () {',
  5293. ' this.$init = function () {',
  5294. ' this.Index = 0;',
  5295. ' };',
  5296. ' this.$final = function () {',
  5297. ' };',
  5298. ' this.DoAbs = function () {',
  5299. ' var Result = null;',
  5300. ' if (Result.Index < Result.Index) Result.Index = Result.Index;',
  5301. ' return Result;',
  5302. ' };',
  5303. '});',
  5304. 'this.DoIt = function () {',
  5305. ' var Result = undefined;',
  5306. ' if (Result === Result) Result = Result;',
  5307. ' if (Result === Result) Result = Result;',
  5308. ' if (Result.Index < Result.Index) Result.Index = Result.Index;',
  5309. ' return Result;',
  5310. '};',
  5311. '']),
  5312. LinesToStr([
  5313. ]));
  5314. end;
  5315. procedure TTestModule.TestProc_LocalVarInit;
  5316. begin
  5317. StartProgram(false);
  5318. Add([
  5319. 'type TBytes = array of byte;',
  5320. 'procedure DoIt;',
  5321. 'const c = 4;',
  5322. 'var',
  5323. ' b: byte = 1;',
  5324. ' w: word = 2+c;',
  5325. ' p: pointer = nil;',
  5326. ' Buffer: TBytes = nil;',
  5327. 'begin',
  5328. 'end;',
  5329. 'begin']);
  5330. ConvertProgram;
  5331. CheckSource('TestProc_LocalVarInit',
  5332. LinesToStr([ // statements
  5333. 'var c = 4;',
  5334. 'this.DoIt = function () {',
  5335. ' var b = 1;',
  5336. ' var w = 2 + 4;',
  5337. ' var p = null;',
  5338. ' var Buffer = [];',
  5339. '};',
  5340. '']),
  5341. LinesToStr([
  5342. ]));
  5343. end;
  5344. procedure TTestModule.TestProc_ReservedWords;
  5345. begin
  5346. StartProgram(false);
  5347. Add([
  5348. 'procedure Date(ArrayBuffer: longint);',
  5349. 'const',
  5350. ' NaN: longint = 3;',
  5351. 'var',
  5352. ' &Boolean: longint;',
  5353. ' procedure Error(ArrayBuffer: longint);',
  5354. ' begin',
  5355. ' end;',
  5356. 'begin',
  5357. ' Nan:=&bOolean;',
  5358. 'end;',
  5359. 'begin',
  5360. ' Date(1);']);
  5361. ConvertProgram;
  5362. CheckSource('TestProc_ReservedWords',
  5363. LinesToStr([ // statements
  5364. 'var naN = 3;',
  5365. 'this.Date = function (arrayBuffer) {',
  5366. ' var boolean = 0;',
  5367. ' function error(arrayBuffer) {',
  5368. ' };',
  5369. ' naN = boolean;',
  5370. '};',
  5371. '']),
  5372. LinesToStr([
  5373. ' $mod.Date(1);'
  5374. ]));
  5375. end;
  5376. procedure TTestModule.TestProc_ConstRefWord;
  5377. begin
  5378. StartProgram(false);
  5379. Add([
  5380. 'procedure Run(constref w: word);',
  5381. 'var l: word;',
  5382. 'begin',
  5383. ' l:=w;',
  5384. ' Run(w);',
  5385. ' Run(l);',
  5386. 'end;',
  5387. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  5388. 'begin',
  5389. ' Run(a);',
  5390. ' Run(b);',
  5391. ' Run(c);',
  5392. ' Run(d);',
  5393. ' Run(e);',
  5394. 'end;',
  5395. 'begin',
  5396. ' Run(1);']);
  5397. ConvertProgram;
  5398. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  5399. CheckSource('TestProc_ConstRefWord',
  5400. LinesToStr([ // statements
  5401. 'this.Run = function (w) {',
  5402. ' var l = 0;',
  5403. ' l = w;',
  5404. ' $mod.Run(w);',
  5405. ' $mod.Run(l);',
  5406. '};',
  5407. 'this.Fly = function (a, b, c, d, e) {',
  5408. ' $mod.Run(a);',
  5409. ' $mod.Run(b.get());',
  5410. ' $mod.Run(c.get());',
  5411. ' $mod.Run(d);',
  5412. ' $mod.Run(e);',
  5413. '};',
  5414. '']),
  5415. LinesToStr([
  5416. '$mod.Run(1);'
  5417. ]));
  5418. end;
  5419. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  5420. begin
  5421. StartProgram(false);
  5422. Add([
  5423. '{$mode objfpc}',
  5424. 'type',
  5425. ' TFunc = reference to function(x: word): word;',
  5426. 'var Func: TFunc;',
  5427. 'procedure DoIt(a: word);',
  5428. 'begin',
  5429. ' Func:=function(b:word): word',
  5430. ' begin',
  5431. ' Result:=a+b;',
  5432. ' exit(b);',
  5433. ' exit(Result);',
  5434. ' end;',// test semicolon
  5435. ' a:=3;',
  5436. 'end;',
  5437. 'begin',
  5438. ' Func:=function(c:word):word begin',
  5439. ' Result:=3+c;',
  5440. ' exit(c);',
  5441. ' exit(Result);',
  5442. ' end;']);
  5443. ConvertProgram;
  5444. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  5445. LinesToStr([ // statements
  5446. 'this.Func = null;',
  5447. 'this.DoIt = function (a) {',
  5448. ' $mod.Func = function (b) {',
  5449. ' var Result = 0;',
  5450. ' Result = a + b;',
  5451. ' return b;',
  5452. ' return Result;',
  5453. ' return Result;',
  5454. ' };',
  5455. ' a = 3;',
  5456. '};',
  5457. '']),
  5458. LinesToStr([
  5459. '$mod.Func = function (c) {',
  5460. ' var Result = 0;',
  5461. ' Result = 3 + c;',
  5462. ' return c;',
  5463. ' return Result;',
  5464. ' return Result;',
  5465. '};',
  5466. '']));
  5467. end;
  5468. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  5469. begin
  5470. StartProgram(false);
  5471. Add([
  5472. '{$mode delphi}',
  5473. 'type',
  5474. ' TProc = reference to procedure(x: word);',
  5475. 'procedure DoIt(a: word);',
  5476. 'var Proc: TProc;',
  5477. 'begin',
  5478. ' Proc:=procedure(b:word) begin end;',
  5479. 'end;',
  5480. 'var Proc: TProc;',
  5481. 'begin',
  5482. ' Proc:=procedure(c:word) begin end;',
  5483. '']);
  5484. ConvertProgram;
  5485. CheckSource('TestAnonymousProc_Assign_Delphi',
  5486. LinesToStr([ // statements
  5487. 'this.DoIt = function (a) {',
  5488. ' var Proc = null;',
  5489. ' Proc = function (b) {',
  5490. ' };',
  5491. '};',
  5492. 'this.Proc = null;',
  5493. '']),
  5494. LinesToStr([
  5495. '$mod.Proc = function (c) {',
  5496. '};',
  5497. '']));
  5498. end;
  5499. procedure TTestModule.TestAnonymousProc_Arg;
  5500. begin
  5501. StartProgram(false);
  5502. Add([
  5503. 'type',
  5504. ' TProc = reference to procedure;',
  5505. ' TFunc = reference to function(x: word): word;',
  5506. 'procedure DoMore(f,g: TProc);',
  5507. 'begin',
  5508. 'end;',
  5509. 'procedure DoOdd(v: jsvalue);',
  5510. 'begin',
  5511. 'end;',
  5512. 'procedure DoIt(f: TFunc);',
  5513. 'begin',
  5514. ' DoIt(function(b:word): word',
  5515. ' begin',
  5516. ' Result:=1+b;',
  5517. ' end);',
  5518. ' DoMore(procedure begin end, procedure begin end);',
  5519. ' DoOdd(procedure begin end);',
  5520. 'end;',
  5521. 'begin',
  5522. ' DoMore(procedure begin end,',
  5523. ' procedure assembler asm',
  5524. ' console.log("c");',
  5525. ' end);',
  5526. '']);
  5527. ConvertProgram;
  5528. CheckSource('TestAnonymousProc_Arg',
  5529. LinesToStr([ // statements
  5530. 'this.DoMore = function (f, g) {',
  5531. '};',
  5532. 'this.DoOdd = function (v) {',
  5533. '};',
  5534. 'this.DoIt = function (f) {',
  5535. ' $mod.DoIt(function (b) {',
  5536. ' var Result = 0;',
  5537. ' Result = 1 + b;',
  5538. ' return Result;',
  5539. ' });',
  5540. ' $mod.DoMore(function () {',
  5541. ' }, function () {',
  5542. ' });',
  5543. ' $mod.DoOdd(function () {',
  5544. ' });',
  5545. '};',
  5546. '']),
  5547. LinesToStr([
  5548. '$mod.DoMore(function () {',
  5549. '}, function () {',
  5550. ' console.log("c");',
  5551. '});',
  5552. '']));
  5553. end;
  5554. procedure TTestModule.TestAnonymousProc_Typecast;
  5555. begin
  5556. StartProgram(false);
  5557. Add([
  5558. 'type',
  5559. ' TProc = reference to procedure(w: word);',
  5560. ' TArr = array of word;',
  5561. ' TFuncArr = reference to function: TArr;',
  5562. 'procedure DoIt(p: TProc);',
  5563. 'var',
  5564. ' w: word;',
  5565. ' a: TArr;',
  5566. 'begin',
  5567. ' p:=TProc(procedure(b: smallint) begin end);',
  5568. ' a:=TFuncArr(function: TArr begin end)();',
  5569. ' w:=TFuncArr(function: TArr begin end)()[3];',
  5570. 'end;',
  5571. 'begin']);
  5572. ConvertProgram;
  5573. CheckSource('TestAnonymousProc_Typecast',
  5574. LinesToStr([ // statements
  5575. 'this.DoIt = function (p) {',
  5576. ' var w = 0;',
  5577. ' var a = [];',
  5578. ' p = function (b) {',
  5579. ' };',
  5580. ' a = function () {',
  5581. ' var Result = [];',
  5582. ' return Result;',
  5583. ' }();',
  5584. ' w = function () {',
  5585. ' var Result = [];',
  5586. ' return Result;',
  5587. ' }()[3];',
  5588. '};',
  5589. '']),
  5590. LinesToStr([
  5591. '']));
  5592. end;
  5593. procedure TTestModule.TestAnonymousProc_With;
  5594. begin
  5595. StartProgram(false);
  5596. Add([
  5597. 'type',
  5598. ' TProc = reference to procedure(w: word);',
  5599. ' TObject = class',
  5600. ' b: boolean;',
  5601. ' end;',
  5602. 'var',
  5603. ' p: TProc;',
  5604. ' bird: TObject;',
  5605. 'begin',
  5606. ' with bird do',
  5607. ' p:=procedure(w: word)',
  5608. ' begin',
  5609. ' b:=w>2;',
  5610. ' end;',
  5611. '']);
  5612. ConvertProgram;
  5613. CheckSource('TestAnonymousProc_With',
  5614. LinesToStr([ // statements
  5615. 'rtl.createClass(this, "TObject", null, function () {',
  5616. ' this.$init = function () {',
  5617. ' this.b = false;',
  5618. ' };',
  5619. ' this.$final = function () {',
  5620. ' };',
  5621. '});',
  5622. 'this.p = null;',
  5623. 'this.bird = null;',
  5624. '']),
  5625. LinesToStr([
  5626. 'var $with = $mod.bird;',
  5627. '$mod.p = function (w) {',
  5628. ' $with.b = w > 2;',
  5629. '};',
  5630. '']));
  5631. end;
  5632. procedure TTestModule.TestAnonymousProc_ExceptOn;
  5633. begin
  5634. StartProgram(false);
  5635. Add([
  5636. 'type',
  5637. ' TProc = reference to procedure;',
  5638. ' TObject = class',
  5639. ' b: boolean;',
  5640. ' end;',
  5641. 'procedure DoIt;',
  5642. 'var',
  5643. ' p: TProc;',
  5644. 'begin',
  5645. ' try',
  5646. ' except',
  5647. ' on E: TObject do',
  5648. ' p:=procedure',
  5649. ' begin',
  5650. ' E.b:=true;',
  5651. ' end;',
  5652. ' end;',
  5653. 'end;',
  5654. 'begin']);
  5655. ConvertProgram;
  5656. CheckSource('TestAnonymousProc_ExceptOn',
  5657. LinesToStr([ // statements
  5658. 'rtl.createClass(this, "TObject", null, function () {',
  5659. ' this.$init = function () {',
  5660. ' this.b = false;',
  5661. ' };',
  5662. ' this.$final = function () {',
  5663. ' };',
  5664. '});',
  5665. 'this.DoIt = function () {',
  5666. ' var p = null;',
  5667. ' try {} catch ($e) {',
  5668. ' if ($mod.TObject.isPrototypeOf($e)) {',
  5669. ' var E = $e;',
  5670. ' p = function () {',
  5671. ' E.b = true;',
  5672. ' };',
  5673. ' } else throw $e',
  5674. ' };',
  5675. '};',
  5676. '']),
  5677. LinesToStr([
  5678. '']));
  5679. end;
  5680. procedure TTestModule.TestAnonymousProc_Nested;
  5681. begin
  5682. StartProgram(false);
  5683. Add([
  5684. 'type',
  5685. ' TProc = reference to procedure;',
  5686. ' TObject = class',
  5687. ' i: byte;',
  5688. ' procedure DoIt;',
  5689. ' end;',
  5690. 'procedure TObject.DoIt;',
  5691. 'var',
  5692. ' p: TProc;',
  5693. ' procedure Sub;',
  5694. ' begin',
  5695. ' p:=procedure',
  5696. ' begin',
  5697. ' i:=3;',
  5698. ' Self.i:=4;',
  5699. ' p:=procedure',
  5700. ' procedure SubSub;',
  5701. ' begin',
  5702. ' i:=13;',
  5703. ' Self.i:=14;',
  5704. ' end;',
  5705. ' begin',
  5706. ' i:=13;',
  5707. ' Self.i:=14;',
  5708. ' end;',
  5709. ' end;',
  5710. ' end;',
  5711. 'begin',
  5712. 'end;',
  5713. 'begin']);
  5714. ConvertProgram;
  5715. CheckSource('TestAnonymousProc_Nested',
  5716. LinesToStr([ // statements
  5717. 'rtl.createClass(this, "TObject", null, function () {',
  5718. ' this.$init = function () {',
  5719. ' this.i = 0;',
  5720. ' };',
  5721. ' this.$final = function () {',
  5722. ' };',
  5723. ' this.DoIt = function () {',
  5724. ' var $Self = this;',
  5725. ' var p = null;',
  5726. ' function Sub() {',
  5727. ' p = function () {',
  5728. ' $Self.i = 3;',
  5729. ' $Self.i = 4;',
  5730. ' p = function () {',
  5731. ' function SubSub() {',
  5732. ' $Self.i = 13;',
  5733. ' $Self.i = 14;',
  5734. ' };',
  5735. ' $Self.i = 13;',
  5736. ' $Self.i = 14;',
  5737. ' };',
  5738. ' };',
  5739. ' };',
  5740. ' };',
  5741. '});',
  5742. '']),
  5743. LinesToStr([
  5744. '']));
  5745. end;
  5746. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  5747. begin
  5748. StartProgram(false);
  5749. Add([
  5750. 'type',
  5751. ' TProc = reference to procedure;',
  5752. 'function DoIt: TProc;',
  5753. ' function Sub: TProc;',
  5754. ' begin',
  5755. ' Result:=procedure',
  5756. ' begin',
  5757. ' Sub:=procedure',
  5758. ' procedure SubSub;',
  5759. ' begin',
  5760. ' Result:=nil;',
  5761. ' Sub:=nil;',
  5762. ' DoIt:=nil;',
  5763. ' end;',
  5764. ' begin',
  5765. ' Result:=nil;',
  5766. ' Sub:=nil;',
  5767. ' DoIt:=nil;',
  5768. ' end;',
  5769. ' end;',
  5770. ' end;',
  5771. 'begin',
  5772. 'end;',
  5773. 'begin']);
  5774. ConvertProgram;
  5775. CheckSource('TestAnonymousProc_NestedAssignResult',
  5776. LinesToStr([ // statements
  5777. 'this.DoIt = function () {',
  5778. ' var Result = null;',
  5779. ' function Sub() {',
  5780. ' var Result$1 = null;',
  5781. ' Result$1 = function () {',
  5782. ' Result$1 = function () {',
  5783. ' function SubSub() {',
  5784. ' Result$1 = null;',
  5785. ' Result$1 = null;',
  5786. ' Result = null;',
  5787. ' };',
  5788. ' Result$1 = null;',
  5789. ' Result$1 = null;',
  5790. ' Result = null;',
  5791. ' };',
  5792. ' };',
  5793. ' return Result$1;',
  5794. ' };',
  5795. ' return Result;',
  5796. '};',
  5797. '']),
  5798. LinesToStr([
  5799. '']));
  5800. end;
  5801. procedure TTestModule.TestAnonymousProc_Class;
  5802. begin
  5803. StartProgram(false);
  5804. Add([
  5805. 'type',
  5806. ' TProc = reference to procedure;',
  5807. ' TEvent = procedure of object;',
  5808. ' TObject = class',
  5809. ' Size: word;',
  5810. ' function GetIt: TProc;',
  5811. ' procedure DoIt; virtual; abstract;',
  5812. ' end;',
  5813. 'function TObject.GetIt: TProc;',
  5814. 'begin',
  5815. ' Result:=procedure',
  5816. ' var p: TEvent;',
  5817. ' begin',
  5818. ' Size:=Size;',
  5819. ' Size:=Self.Size;',
  5820. ' p:=@DoIt;',
  5821. ' p:[email protected];',
  5822. ' end;',
  5823. 'end;',
  5824. 'begin']);
  5825. ConvertProgram;
  5826. CheckSource('TestAnonymousProc_Class',
  5827. LinesToStr([ // statements
  5828. 'rtl.createClass(this, "TObject", null, function () {',
  5829. ' this.$init = function () {',
  5830. ' this.Size = 0;',
  5831. ' };',
  5832. ' this.$final = function () {',
  5833. ' };',
  5834. ' this.GetIt = function () {',
  5835. ' var $Self = this;',
  5836. ' var Result = null;',
  5837. ' Result = function () {',
  5838. ' var p = null;',
  5839. ' $Self.Size = $Self.Size;',
  5840. ' $Self.Size = $Self.Size;',
  5841. ' p = rtl.createCallback($Self, "DoIt");',
  5842. ' p = rtl.createCallback($Self, "DoIt");',
  5843. ' };',
  5844. ' return Result;',
  5845. ' };',
  5846. '});',
  5847. '']),
  5848. LinesToStr([
  5849. '']));
  5850. end;
  5851. procedure TTestModule.TestAnonymousProc_ForLoop;
  5852. begin
  5853. StartProgram(false);
  5854. Add([
  5855. 'type TProc = reference to procedure;',
  5856. 'procedure Foo(p: TProc);',
  5857. 'begin',
  5858. 'end;',
  5859. 'procedure DoIt;',
  5860. 'var i: word;',
  5861. ' a: word;',
  5862. 'begin',
  5863. ' for i:=1 to 10 do begin',
  5864. ' Foo(procedure begin a:=3; end);',
  5865. ' end;',
  5866. 'end;',
  5867. 'begin',
  5868. ' DoIt;']);
  5869. ConvertProgram;
  5870. CheckSource('TestAnonymousProc_ForLoop',
  5871. LinesToStr([ // statements
  5872. 'this.Foo = function (p) {',
  5873. '};',
  5874. 'this.DoIt = function () {',
  5875. ' var i = 0;',
  5876. ' var a = 0;',
  5877. ' for (i = 1; i <= 10; i++) {',
  5878. ' $mod.Foo(function () {',
  5879. ' a = 3;',
  5880. ' });',
  5881. ' };',
  5882. '};',
  5883. '']),
  5884. LinesToStr([
  5885. '$mod.DoIt();'
  5886. ]));
  5887. end;
  5888. procedure TTestModule.TestAnonymousProc_AsmDelphi;
  5889. begin
  5890. StartProgram(false);
  5891. Add([
  5892. '{$mode delphi}',
  5893. 'type',
  5894. ' TProc = reference to procedure;',
  5895. ' TFunc = reference to function(x: word): word;',
  5896. 'procedure Run;',
  5897. 'asm',
  5898. 'end;',
  5899. 'procedure Walk(p: TProc; f: TFunc);',
  5900. 'begin',
  5901. ' Walk(procedure asm end, function(b:word): word asm return 1+b; end);',
  5902. 'end;',
  5903. 'begin',
  5904. ' Walk(procedure',
  5905. ' asm',
  5906. ' console.log("a");',
  5907. ' end,',
  5908. ' function(x: word): word asm',
  5909. ' console.log("c");',
  5910. ' end);',
  5911. '']);
  5912. ConvertProgram;
  5913. CheckSource('TestAnonymousProc_AsmDelphi',
  5914. LinesToStr([ // statements
  5915. 'this.Run = function () {',
  5916. '};',
  5917. 'this.Walk = function (p, f) {',
  5918. ' $mod.Walk(function () {',
  5919. ' }, function (b) {',
  5920. ' return 1+b;',
  5921. ' });',
  5922. '};',
  5923. '']),
  5924. LinesToStr([
  5925. '$mod.Walk(function () {',
  5926. ' console.log("a");',
  5927. '}, function (x) {',
  5928. ' console.log("c");',
  5929. '});',
  5930. '']));
  5931. end;
  5932. procedure TTestModule.TestEnum_Name;
  5933. begin
  5934. StartProgram(false);
  5935. Add('type TMyEnum = (Red, Green, Blue);');
  5936. Add('var e: TMyEnum;');
  5937. Add('var f: TMyEnum = Blue;');
  5938. Add('begin');
  5939. Add(' e:=green;');
  5940. Add(' e:=default(TMyEnum);');
  5941. ConvertProgram;
  5942. CheckSource('TestEnum_Name',
  5943. LinesToStr([ // statements
  5944. 'this.TMyEnum = {',
  5945. ' "0":"Red",',
  5946. ' Red:0,',
  5947. ' "1":"Green",',
  5948. ' Green:1,',
  5949. ' "2":"Blue",',
  5950. ' Blue:2',
  5951. ' };',
  5952. 'this.e = 0;',
  5953. 'this.f = this.TMyEnum.Blue;'
  5954. ]),
  5955. LinesToStr([
  5956. '$mod.e=$mod.TMyEnum.Green;',
  5957. '$mod.e=$mod.TMyEnum.Red;'
  5958. ]));
  5959. end;
  5960. procedure TTestModule.TestEnum_Number;
  5961. begin
  5962. Converter.Options:=Converter.Options+[coEnumNumbers];
  5963. StartProgram(false);
  5964. Add('type TMyEnum = (Red, Green);');
  5965. Add('var');
  5966. Add(' e: TMyEnum;');
  5967. Add(' f: TMyEnum = Green;');
  5968. Add(' i: longint;');
  5969. Add('begin');
  5970. Add(' e:=green;');
  5971. Add(' i:=longint(e);');
  5972. ConvertProgram;
  5973. CheckSource('TestEnumNumber',
  5974. LinesToStr([ // statements
  5975. 'this.TMyEnum = {',
  5976. ' "0":"Red",',
  5977. ' Red:0,',
  5978. ' "1":"Green",',
  5979. ' Green:1',
  5980. ' };',
  5981. 'this.e = 0;',
  5982. 'this.f = 1;',
  5983. 'this.i = 0;'
  5984. ]),
  5985. LinesToStr([
  5986. '$mod.e=1;',
  5987. '$mod.i=$mod.e;'
  5988. ]));
  5989. end;
  5990. procedure TTestModule.TestEnum_ConstFail;
  5991. begin
  5992. StartProgram(false);
  5993. Add([
  5994. 'type TMyEnum = (Red = 100, Green = 101);',
  5995. 'var',
  5996. ' e: TMyEnum;',
  5997. ' f: TMyEnum = Green;',
  5998. 'begin',
  5999. ' e:=green;']);
  6000. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  6001. ConvertProgram;
  6002. end;
  6003. procedure TTestModule.TestEnum_Functions;
  6004. begin
  6005. StartProgram(false);
  6006. Add([
  6007. 'type TMyEnum = (Red, Green);',
  6008. 'procedure DoIt(var e: TMyEnum; var i: word);',
  6009. 'var',
  6010. ' v: longint;',
  6011. ' s: string;',
  6012. 'begin',
  6013. ' val(s,e,v);',
  6014. ' val(s,e,i);',
  6015. 'end;',
  6016. 'var',
  6017. ' e: TMyEnum;',
  6018. ' i: longint;',
  6019. ' s: string;',
  6020. ' b: boolean;',
  6021. 'begin',
  6022. ' i:=ord(red);',
  6023. ' i:=ord(green);',
  6024. ' i:=ord(e);',
  6025. ' i:=ord(b);',
  6026. ' e:=low(tmyenum);',
  6027. ' e:=low(e);',
  6028. ' b:=low(boolean);',
  6029. ' e:=high(tmyenum);',
  6030. ' e:=high(e);',
  6031. ' b:=high(boolean);',
  6032. ' e:=pred(green);',
  6033. ' e:=pred(e);',
  6034. ' b:=pred(b);',
  6035. ' e:=succ(red);',
  6036. ' e:=succ(e);',
  6037. ' b:=succ(b);',
  6038. ' e:=tmyenum(1);',
  6039. ' e:=tmyenum(i);',
  6040. ' s:=str(e);',
  6041. ' str(e,s);',
  6042. ' str(red,s);',
  6043. ' s:=str(e:3);',
  6044. ' writestr(s,e:3,red);',
  6045. ' val(s,e,i);',
  6046. ' i:=longint(e);']);
  6047. ConvertProgram;
  6048. CheckSource('TestEnum_Functions',
  6049. LinesToStr([ // statements
  6050. 'this.TMyEnum = {',
  6051. ' "0":"Red",',
  6052. ' Red:0,',
  6053. ' "1":"Green",',
  6054. ' Green:1',
  6055. ' };',
  6056. 'this.DoIt = function (e, i) {',
  6057. ' var v = 0;',
  6058. ' var s = "";',
  6059. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  6060. ' v = w;',
  6061. ' }));',
  6062. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  6063. '};',
  6064. 'this.e = 0;',
  6065. 'this.i = 0;',
  6066. 'this.s = "";',
  6067. 'this.b = false;',
  6068. '']),
  6069. LinesToStr([
  6070. '$mod.i=$mod.TMyEnum.Red;',
  6071. '$mod.i=$mod.TMyEnum.Green;',
  6072. '$mod.i=$mod.e;',
  6073. '$mod.i=$mod.b+0;',
  6074. '$mod.e=$mod.TMyEnum.Red;',
  6075. '$mod.e=$mod.TMyEnum.Red;',
  6076. '$mod.b=false;',
  6077. '$mod.e=$mod.TMyEnum.Green;',
  6078. '$mod.e=$mod.TMyEnum.Green;',
  6079. '$mod.b=true;',
  6080. '$mod.e=$mod.TMyEnum.Green-1;',
  6081. '$mod.e=$mod.e-1;',
  6082. '$mod.b=false;',
  6083. '$mod.e=$mod.TMyEnum.Red+1;',
  6084. '$mod.e=$mod.e+1;',
  6085. '$mod.b=true;',
  6086. '$mod.e=1;',
  6087. '$mod.e=$mod.i;',
  6088. '$mod.s = $mod.TMyEnum[$mod.e];',
  6089. '$mod.s = $mod.TMyEnum[$mod.e];',
  6090. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  6091. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  6092. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  6093. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  6094. ' $mod.i = v;',
  6095. '});',
  6096. '$mod.i=$mod.e;',
  6097. '']));
  6098. end;
  6099. procedure TTestModule.TestEnumRg_Functions;
  6100. begin
  6101. StartProgram(false);
  6102. Add([
  6103. 'type',
  6104. ' TEnum = (Red, Green, Blue);',
  6105. ' TEnumRg = Green..Blue;',
  6106. 'procedure DoIt(var e: TEnumRg; var i: word);',
  6107. 'var',
  6108. ' v: longint;',
  6109. ' s: string;',
  6110. 'begin',
  6111. ' val(s,e,v);',
  6112. ' val(s,e,i);',
  6113. 'end;',
  6114. 'var',
  6115. ' e: TEnumRg;',
  6116. ' i: longint;',
  6117. ' s: string;',
  6118. 'begin',
  6119. ' i:=ord(green);',
  6120. ' i:=ord(e);',
  6121. ' e:=low(tenumrg);',
  6122. ' e:=low(e);',
  6123. ' e:=high(tenumrg);',
  6124. ' e:=high(e);',
  6125. ' e:=pred(blue);',
  6126. ' e:=pred(e);',
  6127. ' e:=succ(green);',
  6128. ' e:=succ(e);',
  6129. ' e:=tenumrg(1);',
  6130. ' e:=tenumrg(i);',
  6131. ' s:=str(e);',
  6132. ' str(e,s);',
  6133. ' str(red,s);',
  6134. ' s:=str(e:3);',
  6135. ' writestr(s,e:3,blue);',
  6136. ' val(s,e,i);',
  6137. ' i:=longint(e);']);
  6138. ConvertProgram;
  6139. CheckSource('TestEnumRg_Functions',
  6140. LinesToStr([ // statements
  6141. 'this.TEnum = {',
  6142. ' "0":"Red",',
  6143. ' Red:0,',
  6144. ' "1":"Green",',
  6145. ' Green:1,',
  6146. ' "2":"Blue",',
  6147. ' Blue:2',
  6148. ' };',
  6149. 'this.DoIt = function (e, i) {',
  6150. ' var v = 0;',
  6151. ' var s = "";',
  6152. ' e.set(rtl.valEnum(s, $mod.TEnum, function (w) {',
  6153. ' v = w;',
  6154. ' }));',
  6155. ' e.set(rtl.valEnum(s, $mod.TEnum, i.set));',
  6156. '};',
  6157. 'this.e = this.TEnum.Green;',
  6158. 'this.i = 0;',
  6159. 'this.s = "";',
  6160. '']),
  6161. LinesToStr([
  6162. '$mod.i=$mod.TEnum.Green;',
  6163. '$mod.i=$mod.e;',
  6164. '$mod.e=$mod.TEnum.Green;',
  6165. '$mod.e=$mod.TEnum.Green;',
  6166. '$mod.e=$mod.TEnum.Blue;',
  6167. '$mod.e=$mod.TEnum.Blue;',
  6168. '$mod.e=$mod.TEnum.Blue-1;',
  6169. '$mod.e=$mod.e-1;',
  6170. '$mod.e=$mod.TEnum.Green+1;',
  6171. '$mod.e=$mod.e+1;',
  6172. '$mod.e=1;',
  6173. '$mod.e=$mod.i;',
  6174. '$mod.s = $mod.TEnum[$mod.e];',
  6175. '$mod.s = $mod.TEnum[$mod.e];',
  6176. '$mod.s = $mod.TEnum[$mod.TEnum.Red];',
  6177. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3);',
  6178. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3)+$mod.TEnum[$mod.TEnum.Blue];',
  6179. '$mod.e = rtl.valEnum($mod.s, $mod.TEnum, function (v) {',
  6180. ' $mod.i = v;',
  6181. '});',
  6182. '$mod.i=$mod.e;',
  6183. '']));
  6184. end;
  6185. procedure TTestModule.TestEnum_AsParams;
  6186. begin
  6187. StartProgram(false);
  6188. Add('type TEnum = (Red,Blue);');
  6189. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  6190. Add('var vJ: TEnum;');
  6191. Add('begin');
  6192. Add(' vg:=vg;');
  6193. Add(' vj:=vh;');
  6194. Add(' vi:=vi;');
  6195. Add(' doit(vg,vg,vg);');
  6196. Add(' doit(vh,vh,vj);');
  6197. Add(' doit(vi,vi,vi);');
  6198. Add(' doit(vj,vj,vj);');
  6199. Add('end;');
  6200. Add('var i: TEnum;');
  6201. Add('begin');
  6202. Add(' doit(i,i,i);');
  6203. ConvertProgram;
  6204. CheckSource('TestEnum_AsParams',
  6205. LinesToStr([ // statements
  6206. 'this.TEnum = {',
  6207. ' "0": "Red",',
  6208. ' Red: 0,',
  6209. ' "1": "Blue",',
  6210. ' Blue: 1',
  6211. '};',
  6212. 'this.DoIt = function (vG,vH,vI) {',
  6213. ' var vJ = 0;',
  6214. ' vG = vG;',
  6215. ' vJ = vH;',
  6216. ' vI.set(vI.get());',
  6217. ' $mod.DoIt(vG, vG, {',
  6218. ' get: function () {',
  6219. ' return vG;',
  6220. ' },',
  6221. ' set: function (v) {',
  6222. ' vG = v;',
  6223. ' }',
  6224. ' });',
  6225. ' $mod.DoIt(vH, vH, {',
  6226. ' get: function () {',
  6227. ' return vJ;',
  6228. ' },',
  6229. ' set: function (v) {',
  6230. ' vJ = v;',
  6231. ' }',
  6232. ' });',
  6233. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  6234. ' $mod.DoIt(vJ, vJ, {',
  6235. ' get: function () {',
  6236. ' return vJ;',
  6237. ' },',
  6238. ' set: function (v) {',
  6239. ' vJ = v;',
  6240. ' }',
  6241. ' });',
  6242. '};',
  6243. 'this.i = 0;'
  6244. ]),
  6245. LinesToStr([
  6246. '$mod.DoIt($mod.i,$mod.i,{',
  6247. ' p: $mod,',
  6248. ' get: function () {',
  6249. ' return this.p.i;',
  6250. ' },',
  6251. ' set: function (v) {',
  6252. ' this.p.i = v;',
  6253. ' }',
  6254. '});'
  6255. ]));
  6256. end;
  6257. procedure TTestModule.TestEnumRange_Array;
  6258. begin
  6259. StartProgram(false);
  6260. Add([
  6261. 'type',
  6262. ' TEnum = (Red, Green, Blue);',
  6263. ' TEnumRg = green..blue;',
  6264. ' TArr = array[TEnumRg] of byte;',
  6265. ' TArr2 = array[green..blue] of byte;',
  6266. 'var',
  6267. ' a: TArr;',
  6268. ' b: TArr = (3,4);',
  6269. ' c: TArr2 = (5,6);',
  6270. 'begin',
  6271. ' a[green] := b[blue];',
  6272. ' c[green] := c[blue];',
  6273. '']);
  6274. ConvertProgram;
  6275. CheckSource('TestEnumRange_Array',
  6276. LinesToStr([ // statements
  6277. 'this.TEnum = {',
  6278. ' "0": "Red",',
  6279. ' Red: 0,',
  6280. ' "1": "Green",',
  6281. ' Green: 1,',
  6282. ' "2": "Blue",',
  6283. ' Blue: 2',
  6284. '};',
  6285. 'this.a = rtl.arraySetLength(null, 0, 2);',
  6286. 'this.b = [3, 4];',
  6287. 'this.c = [5, 6];',
  6288. '']),
  6289. LinesToStr([
  6290. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  6291. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  6292. '']));
  6293. end;
  6294. procedure TTestModule.TestEnum_ForIn;
  6295. begin
  6296. StartProgram(false);
  6297. Add([
  6298. 'type',
  6299. ' TEnum = (Red, Green, Blue);',
  6300. ' TEnumRg = green..blue;',
  6301. ' TArr = array[TEnum] of byte;',
  6302. ' TArrRg = array[TEnumRg] of byte;',
  6303. 'var',
  6304. ' e: TEnum;',
  6305. ' a1: TArr = (3,4,5);',
  6306. ' a2: TArrRg = (11,12);',
  6307. ' b: byte;',
  6308. 'begin',
  6309. ' for e in TEnum do ;',
  6310. ' for e in TEnumRg do ;',
  6311. ' for e in TArr do ;',
  6312. ' for e in TArrRg do ;',
  6313. ' for b in a1 do ;',
  6314. ' for b in a2 do ;',
  6315. '']);
  6316. ConvertProgram;
  6317. CheckSource('TestEnum_ForIn',
  6318. LinesToStr([ // statements
  6319. 'this.TEnum = {',
  6320. ' "0": "Red",',
  6321. ' Red: 0,',
  6322. ' "1": "Green",',
  6323. ' Green: 1,',
  6324. ' "2": "Blue",',
  6325. ' Blue: 2',
  6326. '};',
  6327. 'this.e = 0;',
  6328. 'this.a1 = [3, 4, 5];',
  6329. 'this.a2 = [11, 12];',
  6330. 'this.b = 0;',
  6331. '']),
  6332. LinesToStr([
  6333. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6334. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6335. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6336. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6337. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  6338. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  6339. '']));
  6340. end;
  6341. procedure TTestModule.TestEnum_ScopedNumber;
  6342. begin
  6343. Converter.Options:=Converter.Options+[coEnumNumbers];
  6344. StartProgram(false);
  6345. Add([
  6346. 'type',
  6347. ' TEnum = (Red, Green);',
  6348. 'var',
  6349. ' e: TEnum;',
  6350. 'begin',
  6351. ' e:=TEnum.Green;',
  6352. '']);
  6353. ConvertProgram;
  6354. CheckSource('TestEnum_ScopedNumber',
  6355. LinesToStr([ // statements
  6356. 'this.TEnum = {',
  6357. ' "0": "Red",',
  6358. ' Red: 0,',
  6359. ' "1": "Green",',
  6360. ' Green: 1',
  6361. '};',
  6362. 'this.e = 0;',
  6363. '']),
  6364. LinesToStr([
  6365. '$mod.e = 1;']));
  6366. end;
  6367. procedure TTestModule.TestEnum_InFunction;
  6368. begin
  6369. StartProgram(false);
  6370. Add([
  6371. 'const TEnum = 3;',
  6372. 'procedure DoIt;',
  6373. 'type',
  6374. ' TEnum = (Red, Green, Blue);',
  6375. ' procedure Sub;',
  6376. ' type',
  6377. ' TEnumSub = (Left, Right);',
  6378. ' var',
  6379. ' es: TEnumSub;',
  6380. ' begin',
  6381. ' es:=Left;',
  6382. ' end;',
  6383. 'var',
  6384. ' e, e2: TEnum;',
  6385. 'begin',
  6386. ' if e in [red,blue] then e2:=e;',
  6387. 'end;',
  6388. 'begin']);
  6389. ConvertProgram;
  6390. CheckSource('TestEnum_InFunction',
  6391. LinesToStr([ // statements
  6392. 'this.TEnum = 3;',
  6393. 'var TEnum$1 = {',
  6394. ' "0":"Red",',
  6395. ' Red:0,',
  6396. ' "1":"Green",',
  6397. ' Green:1,',
  6398. ' "2":"Blue",',
  6399. ' Blue:2',
  6400. ' };',
  6401. 'var TEnumSub = {',
  6402. ' "0": "Left",',
  6403. ' Left: 0,',
  6404. ' "1": "Right",',
  6405. ' Right: 1',
  6406. '};',
  6407. 'this.DoIt = function () {',
  6408. ' function Sub() {',
  6409. ' var es = 0;',
  6410. ' es = TEnumSub.Left;',
  6411. ' };',
  6412. ' var e = 0;',
  6413. ' var e2 = 0;',
  6414. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  6415. '};',
  6416. '']),
  6417. LinesToStr([
  6418. '']));
  6419. end;
  6420. procedure TTestModule.TestEnum_Name_Anonymous_Unit;
  6421. begin
  6422. StartUnit(true);
  6423. Add([
  6424. 'interface',
  6425. 'var color: (red, green);',
  6426. 'implementation',
  6427. 'initialization',
  6428. ' color:=green;',
  6429. '']);
  6430. ConvertUnit;
  6431. CheckSource('TestEnum_Name_Anonymous_Unit',
  6432. LinesToStr([
  6433. 'this.color$a = {',
  6434. ' "0": "red",',
  6435. ' red: 0,',
  6436. ' "1": "green",',
  6437. ' green: 1',
  6438. '};',
  6439. 'this.color = 0;',
  6440. '']),
  6441. LinesToStr([ // this.$init
  6442. '$mod.color = $mod.color$a.green;',
  6443. '']),
  6444. LinesToStr([ // implementation
  6445. '']) );
  6446. end;
  6447. procedure TTestModule.TestSet_Enum;
  6448. begin
  6449. StartProgram(false);
  6450. Add([
  6451. 'type',
  6452. ' TColor = (Red, Green, Blue);',
  6453. ' TColors = set of TColor;',
  6454. 'var',
  6455. ' c: TColor;',
  6456. ' s: TColors;',
  6457. ' t: TColors = [];',
  6458. ' u: TColors = [Red];',
  6459. 'begin',
  6460. ' s:=[];',
  6461. ' s:=[Green];',
  6462. ' s:=[Green,Blue];',
  6463. ' s:=[Red..Blue];',
  6464. ' s:=[Red,Green..Blue];',
  6465. ' s:=[Red,c];',
  6466. ' s:=t;',
  6467. ' s:=default(TColors);',
  6468. '']);
  6469. ConvertProgram;
  6470. CheckSource('TestSet',
  6471. LinesToStr([ // statements
  6472. 'this.TColor = {',
  6473. ' "0":"Red",',
  6474. ' Red:0,',
  6475. ' "1":"Green",',
  6476. ' Green:1,',
  6477. ' "2":"Blue",',
  6478. ' Blue:2',
  6479. ' };',
  6480. 'this.c = 0;',
  6481. 'this.s = {};',
  6482. 'this.t = {};',
  6483. 'this.u = rtl.createSet(this.TColor.Red);'
  6484. ]),
  6485. LinesToStr([
  6486. '$mod.s={};',
  6487. '$mod.s=rtl.createSet($mod.TColor.Green);',
  6488. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  6489. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  6490. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  6491. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  6492. '$mod.s=rtl.refSet($mod.t);',
  6493. '$mod.s={};',
  6494. '']));
  6495. end;
  6496. procedure TTestModule.TestSet_Operators;
  6497. begin
  6498. StartProgram(false);
  6499. Add('type');
  6500. Add(' TColor = (Red, Green, Blue);');
  6501. Add(' TColors = set of tcolor;');
  6502. Add('var');
  6503. Add(' vC: TColor;');
  6504. Add(' vS: TColors;');
  6505. Add(' vT: TColors;');
  6506. Add(' vU: TColors;');
  6507. Add(' B: boolean;');
  6508. Add('begin');
  6509. Add(' include(vs,green);');
  6510. Add(' exclude(vs,vc);');
  6511. Add(' vs:=vt+vu;');
  6512. Add(' vs:=vt+[red];');
  6513. Add(' vs:=[red]+vt;');
  6514. Add(' vs:=[red]+[green];');
  6515. Add(' vs:=vt-vu;');
  6516. Add(' vs:=vt-[red];');
  6517. Add(' vs:=[red]-vt;');
  6518. Add(' vs:=[red]-[green];');
  6519. Add(' vs:=vt*vu;');
  6520. Add(' vs:=vt*[red];');
  6521. Add(' vs:=[red]*vt;');
  6522. Add(' vs:=[red]*[green];');
  6523. Add(' vs:=vt><vu;');
  6524. Add(' vs:=vt><[red];');
  6525. Add(' vs:=[red]><vt;');
  6526. Add(' vs:=[red]><[green];');
  6527. Add(' b:=vt=vu;');
  6528. Add(' b:=vt=[red];');
  6529. Add(' b:=[red]=vt;');
  6530. Add(' b:=[red]=[green];');
  6531. Add(' b:=vt<>vu;');
  6532. Add(' b:=vt<>[red];');
  6533. Add(' b:=[red]<>vt;');
  6534. Add(' b:=[red]<>[green];');
  6535. Add(' b:=vt<=vu;');
  6536. Add(' b:=vt<=[red];');
  6537. Add(' b:=[red]<=vt;');
  6538. Add(' b:=[red]<=[green];');
  6539. Add(' b:=vt>=vu;');
  6540. Add(' b:=vt>=[red];');
  6541. Add(' b:=[red]>=vt;');
  6542. Add(' b:=[red]>=[green];');
  6543. ConvertProgram;
  6544. CheckSource('TestSet_Operators',
  6545. LinesToStr([ // statements
  6546. 'this.TColor = {',
  6547. ' "0":"Red",',
  6548. ' Red:0,',
  6549. ' "1":"Green",',
  6550. ' Green:1,',
  6551. ' "2":"Blue",',
  6552. ' Blue:2',
  6553. ' };',
  6554. 'this.vC = 0;',
  6555. 'this.vS = {};',
  6556. 'this.vT = {};',
  6557. 'this.vU = {};',
  6558. 'this.B = false;'
  6559. ]),
  6560. LinesToStr([
  6561. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  6562. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  6563. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  6564. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6565. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6566. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6567. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  6568. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6569. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6570. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6571. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  6572. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6573. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6574. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6575. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  6576. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6577. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6578. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6579. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  6580. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6581. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6582. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6583. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  6584. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6585. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6586. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6587. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  6588. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6589. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6590. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6591. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  6592. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6593. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6594. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6595. '']));
  6596. end;
  6597. procedure TTestModule.TestSet_Operator_In;
  6598. begin
  6599. StartProgram(false);
  6600. Add([
  6601. 'type',
  6602. ' TColor = (Red, Green, Blue);',
  6603. ' TColors = set of tcolor;',
  6604. ' TColorRg = green..blue;',
  6605. 'var',
  6606. ' vC: tcolor;',
  6607. ' vT: tcolors;',
  6608. ' B: boolean;',
  6609. ' rg: TColorRg;',
  6610. 'begin',
  6611. ' b:=red in vt;',
  6612. ' b:=vc in vt;',
  6613. ' b:=green in [red..blue];',
  6614. ' b:=vc in [red..blue];',
  6615. ' ',
  6616. ' if red in vt then ;',
  6617. ' while vC in vt do ;',
  6618. ' repeat',
  6619. ' until vC in vt;',
  6620. ' if rg in [green..blue] then ;',
  6621. '']);
  6622. ConvertProgram;
  6623. CheckSource('TestSet_Operator_In',
  6624. LinesToStr([ // statements
  6625. 'this.TColor = {',
  6626. ' "0":"Red",',
  6627. ' Red:0,',
  6628. ' "1":"Green",',
  6629. ' Green:1,',
  6630. ' "2":"Blue",',
  6631. ' Blue:2',
  6632. ' };',
  6633. 'this.vC = 0;',
  6634. 'this.vT = {};',
  6635. 'this.B = false;',
  6636. 'this.rg = this.TColor.Green;',
  6637. '']),
  6638. LinesToStr([
  6639. '$mod.B = $mod.TColor.Red in $mod.vT;',
  6640. '$mod.B = $mod.vC in $mod.vT;',
  6641. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  6642. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  6643. 'if ($mod.TColor.Red in $mod.vT) ;',
  6644. 'while ($mod.vC in $mod.vT) {',
  6645. '};',
  6646. 'do {',
  6647. '} while (!($mod.vC in $mod.vT));',
  6648. 'if ($mod.rg in rtl.createSet(null, $mod.TColor.Green, $mod.TColor.Blue)) ;',
  6649. '']));
  6650. end;
  6651. procedure TTestModule.TestSet_Functions;
  6652. begin
  6653. StartProgram(false);
  6654. Add('type');
  6655. Add(' TMyEnum = (Red, Green);');
  6656. Add(' TMyEnums = set of TMyEnum;');
  6657. Add('var');
  6658. Add(' e: TMyEnum;');
  6659. Add(' s: TMyEnums;');
  6660. Add('begin');
  6661. Add(' e:=Low(TMyEnums);');
  6662. Add(' e:=Low(s);');
  6663. Add(' e:=High(TMyEnums);');
  6664. Add(' e:=High(s);');
  6665. ConvertProgram;
  6666. CheckSource('TestSetFunctions',
  6667. LinesToStr([ // statements
  6668. 'this.TMyEnum = {',
  6669. ' "0":"Red",',
  6670. ' Red:0,',
  6671. ' "1":"Green",',
  6672. ' Green:1',
  6673. ' };',
  6674. 'this.e = 0;',
  6675. 'this.s = {};'
  6676. ]),
  6677. LinesToStr([
  6678. '$mod.e=$mod.TMyEnum.Red;',
  6679. '$mod.e=$mod.TMyEnum.Red;',
  6680. '$mod.e=$mod.TMyEnum.Green;',
  6681. '$mod.e=$mod.TMyEnum.Green;',
  6682. '']));
  6683. end;
  6684. procedure TTestModule.TestSet_PassAsArgClone;
  6685. begin
  6686. StartProgram(false);
  6687. Add('type');
  6688. Add(' TMyEnum = (Red, Green);');
  6689. Add(' TMyEnums = set of TMyEnum;');
  6690. Add('procedure DoDefault(s: tmyenums); begin end;');
  6691. Add('procedure DoConst(const s: tmyenums); begin end;');
  6692. Add('var');
  6693. Add(' aSet: tmyenums;');
  6694. Add('begin');
  6695. Add(' dodefault(aset);');
  6696. Add(' doconst(aset);');
  6697. ConvertProgram;
  6698. CheckSource('TestSetFunctions',
  6699. LinesToStr([ // statements
  6700. 'this.TMyEnum = {',
  6701. ' "0":"Red",',
  6702. ' Red:0,',
  6703. ' "1":"Green",',
  6704. ' Green:1',
  6705. ' };',
  6706. 'this.DoDefault = function (s) {',
  6707. '};',
  6708. 'this.DoConst = function (s) {',
  6709. '};',
  6710. 'this.aSet = {};'
  6711. ]),
  6712. LinesToStr([
  6713. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  6714. '$mod.DoConst($mod.aSet);',
  6715. '']));
  6716. end;
  6717. procedure TTestModule.TestSet_AsParams;
  6718. begin
  6719. StartProgram(false);
  6720. Add([
  6721. 'type TEnum = (Red,Blue);',
  6722. 'type TEnums = set of TEnum;',
  6723. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  6724. 'var vJ: TEnums;',
  6725. 'begin',
  6726. ' Include(vg,red);',
  6727. ' Include(result,blue);',
  6728. ' vg:=vg;',
  6729. ' vj:=vh;',
  6730. ' vi:=vi;',
  6731. ' doit(vg,vg,vg);',
  6732. ' doit(vh,vh,vj);',
  6733. ' doit(vi,vi,vi);',
  6734. ' doit(vj,vj,vj);',
  6735. 'end;',
  6736. 'var i: TEnums;',
  6737. 'begin',
  6738. ' doit(i,i,i);']);
  6739. ConvertProgram;
  6740. CheckSource('TestSet_AsParams',
  6741. LinesToStr([ // statements
  6742. 'this.TEnum = {',
  6743. ' "0": "Red",',
  6744. ' Red: 0,',
  6745. ' "1": "Blue",',
  6746. ' Blue: 1',
  6747. '};',
  6748. 'this.DoIt = function (vG,vH,vI) {',
  6749. ' var Result = {};',
  6750. ' var vJ = {};',
  6751. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  6752. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  6753. ' vG = rtl.refSet(vG);',
  6754. ' vJ = rtl.refSet(vH);',
  6755. ' vI.set(rtl.refSet(vI.get()));',
  6756. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  6757. ' get: function () {',
  6758. ' return vG;',
  6759. ' },',
  6760. ' set: function (v) {',
  6761. ' vG = v;',
  6762. ' }',
  6763. ' });',
  6764. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  6765. ' get: function () {',
  6766. ' return vJ;',
  6767. ' },',
  6768. ' set: function (v) {',
  6769. ' vJ = v;',
  6770. ' }',
  6771. ' });',
  6772. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  6773. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  6774. ' get: function () {',
  6775. ' return vJ;',
  6776. ' },',
  6777. ' set: function (v) {',
  6778. ' vJ = v;',
  6779. ' }',
  6780. ' });',
  6781. ' return Result;',
  6782. '};',
  6783. 'this.i = {};'
  6784. ]),
  6785. LinesToStr([
  6786. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  6787. ' p: $mod,',
  6788. ' get: function () {',
  6789. ' return this.p.i;',
  6790. ' },',
  6791. ' set: function (v) {',
  6792. ' this.p.i = v;',
  6793. ' }',
  6794. '});'
  6795. ]));
  6796. end;
  6797. procedure TTestModule.TestSet_Property;
  6798. begin
  6799. StartProgram(false);
  6800. Add('type');
  6801. Add(' TEnum = (Red,Blue);');
  6802. Add(' TEnums = set of TEnum;');
  6803. Add(' TObject = class');
  6804. Add(' function GetColors: TEnums; external name ''GetColors'';');
  6805. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  6806. Add(' property Colors: TEnums read GetColors write SetColors;');
  6807. Add(' end;');
  6808. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  6809. Add('begin end;');
  6810. Add('var Obj: TObject;');
  6811. Add('begin');
  6812. Add(' Include(Obj.Colors,Red);');
  6813. Add(' Exclude(Obj.Colors,Red);');
  6814. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  6815. ConvertProgram;
  6816. CheckSource('TestSet_Property',
  6817. LinesToStr([ // statements
  6818. 'this.TEnum = {',
  6819. ' "0": "Red",',
  6820. ' Red: 0,',
  6821. ' "1": "Blue",',
  6822. ' Blue: 1',
  6823. '};',
  6824. 'rtl.createClass(this, "TObject", null, function () {',
  6825. ' this.$init = function () {',
  6826. ' };',
  6827. ' this.$final = function () {',
  6828. ' };',
  6829. '});',
  6830. 'this.DoIt = function (i, j, k, l) {',
  6831. '};',
  6832. 'this.Obj = null;',
  6833. '']),
  6834. LinesToStr([
  6835. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6836. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6837. '']));
  6838. end;
  6839. procedure TTestModule.TestSet_EnumConst;
  6840. begin
  6841. StartProgram(false);
  6842. Add([
  6843. 'type',
  6844. ' TEnum = (Red,Blue);',
  6845. ' TEnums = set of TEnum;',
  6846. 'const',
  6847. ' Orange = red;',
  6848. 'var',
  6849. ' Enum: tenum;',
  6850. ' Enums: tenums;',
  6851. 'begin',
  6852. ' Include(enums,orange);',
  6853. ' Exclude(enums,orange);',
  6854. ' if orange in enums then;',
  6855. ' if orange in [orange,red] then;']);
  6856. ConvertProgram;
  6857. CheckSource('TestSet_EnumConst',
  6858. LinesToStr([ // statements
  6859. 'this.TEnum = {',
  6860. ' "0": "Red",',
  6861. ' Red: 0,',
  6862. ' "1": "Blue",',
  6863. ' Blue: 1',
  6864. '};',
  6865. 'this.Orange = this.TEnum.Red;',
  6866. 'this.Enum = 0;',
  6867. 'this.Enums = {};',
  6868. '']),
  6869. LinesToStr([
  6870. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  6871. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  6872. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  6873. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  6874. '']));
  6875. end;
  6876. procedure TTestModule.TestSet_IntConst;
  6877. begin
  6878. StartProgram(false);
  6879. Add([
  6880. 'type',
  6881. ' TEnums = set of Byte;',
  6882. 'const',
  6883. ' Orange = 0;',
  6884. 'var',
  6885. ' Enum: byte;',
  6886. ' Enums: tenums;',
  6887. 'begin',
  6888. ' Enum:=ord(1);',
  6889. ' Enums:=[];',
  6890. ' Enums:=[0];',
  6891. ' Enums:=[1..2];',
  6892. //' Include(enums,orange);',
  6893. //' Exclude(enums,orange);',
  6894. ' if orange in enums then;',
  6895. ' if orange in [orange,1] then;']);
  6896. ConvertProgram;
  6897. CheckSource('TestSet_IntConst',
  6898. LinesToStr([ // statements
  6899. 'this.Orange = 0;',
  6900. 'this.Enum = 0;',
  6901. 'this.Enums = {};',
  6902. '']),
  6903. LinesToStr([
  6904. '$mod.Enum = 1;',
  6905. '$mod.Enums = {};',
  6906. '$mod.Enums = rtl.createSet(0);',
  6907. '$mod.Enums = rtl.createSet(null, 1, 2);',
  6908. 'if (0 in $mod.Enums) ;',
  6909. 'if (0 in rtl.createSet(0, 1)) ;',
  6910. '']));
  6911. end;
  6912. procedure TTestModule.TestSet_IntRange;
  6913. begin
  6914. StartProgram(false);
  6915. Add([
  6916. 'type',
  6917. ' TRange = 1..3;',
  6918. ' TEnums = set of TRange;',
  6919. 'const',
  6920. ' Orange = 2;',
  6921. 'var',
  6922. ' Enum: byte;',
  6923. ' Enums: TEnums;',
  6924. 'begin',
  6925. ' Enums:=[];',
  6926. ' Enums:=[1];',
  6927. ' Enums:=[2..3];',
  6928. ' Include(enums,orange);',
  6929. ' Exclude(enums,orange);',
  6930. ' if orange in enums then;',
  6931. ' if orange in [orange,1] then;']);
  6932. ConvertProgram;
  6933. CheckSource('TestSet_IntRange',
  6934. LinesToStr([ // statements
  6935. 'this.Orange = 2;',
  6936. 'this.Enum = 0;',
  6937. 'this.Enums = {};',
  6938. '']),
  6939. LinesToStr([
  6940. '$mod.Enums = {};',
  6941. '$mod.Enums = rtl.createSet(1);',
  6942. '$mod.Enums = rtl.createSet(null, 2, 3);',
  6943. '$mod.Enums = rtl.includeSet($mod.Enums, 2);',
  6944. '$mod.Enums = rtl.excludeSet($mod.Enums, 2);',
  6945. 'if (2 in $mod.Enums) ;',
  6946. 'if (2 in rtl.createSet(2, 1)) ;',
  6947. '']));
  6948. end;
  6949. procedure TTestModule.TestSet_AnonymousEnumType;
  6950. begin
  6951. StartProgram(false);
  6952. Add('type');
  6953. Add(' TFlags = set of (red, green);');
  6954. Add('const');
  6955. Add(' favorite = red;');
  6956. Add('var');
  6957. Add(' f: TFlags;');
  6958. Add(' i: longint;');
  6959. Add('begin');
  6960. Add(' Include(f,red);');
  6961. Add(' Include(f,favorite);');
  6962. Add(' i:=ord(red);');
  6963. Add(' i:=ord(favorite);');
  6964. Add(' i:=ord(low(TFlags));');
  6965. Add(' i:=ord(low(f));');
  6966. Add(' i:=ord(low(favorite));');
  6967. Add(' i:=ord(high(TFlags));');
  6968. Add(' i:=ord(high(f));');
  6969. Add(' i:=ord(high(favorite));');
  6970. Add(' f:=[green,favorite];');
  6971. ConvertProgram;
  6972. CheckSource('TestSet_AnonymousEnumType',
  6973. LinesToStr([ // statements
  6974. 'this.TFlags$a = {',
  6975. ' "0": "red",',
  6976. ' red: 0,',
  6977. ' "1": "green",',
  6978. ' green: 1',
  6979. '};',
  6980. 'this.favorite = this.TFlags$a.red;',
  6981. 'this.f = {};',
  6982. 'this.i = 0;',
  6983. '']),
  6984. LinesToStr([
  6985. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6986. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6987. '$mod.i = $mod.TFlags$a.red;',
  6988. '$mod.i = $mod.TFlags$a.red;',
  6989. '$mod.i = $mod.TFlags$a.red;',
  6990. '$mod.i = $mod.TFlags$a.red;',
  6991. '$mod.i = $mod.TFlags$a.red;',
  6992. '$mod.i = $mod.TFlags$a.green;',
  6993. '$mod.i = $mod.TFlags$a.green;',
  6994. '$mod.i = $mod.TFlags$a.green;',
  6995. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  6996. '']));
  6997. end;
  6998. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  6999. begin
  7000. exit;
  7001. StartProgram(false);
  7002. Add([
  7003. 'type',
  7004. ' TAtoZ = ''A''..''Z'';',
  7005. ' TSetOfAZ = set of TAtoZ;',
  7006. 'var',
  7007. ' c: char;',
  7008. ' a: TAtoZ;',
  7009. ' s: TSetOfAZ = [''P'',''A''];',
  7010. ' i: longint;',
  7011. 'begin',
  7012. ' Include(s,''S'');',
  7013. ' Include(s,c);',
  7014. ' Include(s,a);',
  7015. ' c:=low(TAtoZ);',
  7016. ' i:=ord(low(TAtoZ));',
  7017. ' a:=high(TAtoZ);',
  7018. ' a:=high(TSetOfAtoZ);',
  7019. ' s:=[a,c,''M''];',
  7020. '']);
  7021. ConvertProgram;
  7022. CheckSource('TestSet_AnonymousEnumTypeChar',
  7023. LinesToStr([ // statements
  7024. '']),
  7025. LinesToStr([
  7026. '']));
  7027. end;
  7028. procedure TTestModule.TestSet_ConstEnum;
  7029. begin
  7030. StartProgram(false);
  7031. Add([
  7032. 'type',
  7033. ' TEnum = (red,blue,green);',
  7034. ' TEnums = set of TEnum;',
  7035. 'const',
  7036. ' teAny = [low(TEnum)..high(TEnum)];',
  7037. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  7038. 'var',
  7039. ' e: TEnum;',
  7040. ' s: TEnums;',
  7041. 'begin',
  7042. ' if blue in teAny then;',
  7043. ' if blue in teAny+[e] then;',
  7044. ' if blue in teAny+teRedBlue then;',
  7045. ' if e in [red,blue] then;',
  7046. ' s:=teAny;',
  7047. ' s:=teAny+[e];',
  7048. ' s:=[e]+teAny;',
  7049. ' s:=teAny+teRedBlue;',
  7050. ' s:=teAny+teRedBlue+[e];',
  7051. '']);
  7052. ConvertProgram;
  7053. CheckSource('TestSet_ConstEnum',
  7054. LinesToStr([ // statements
  7055. 'this.TEnum = {',
  7056. ' "0": "red",',
  7057. ' red: 0,',
  7058. ' "1": "blue",',
  7059. ' blue: 1,',
  7060. ' "2": "green",',
  7061. ' green: 2',
  7062. '};',
  7063. 'this.teAny = rtl.createSet(null, this.TEnum.red, this.TEnum.green);',
  7064. 'this.teRedBlue = rtl.createSet(null, this.TEnum.red, this.TEnum.green - 1);',
  7065. 'this.e = 0;',
  7066. 'this.s = {};',
  7067. '']),
  7068. LinesToStr([
  7069. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  7070. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  7071. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  7072. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  7073. '$mod.s = rtl.refSet($mod.teAny);',
  7074. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  7075. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  7076. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  7077. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  7078. '']));
  7079. end;
  7080. procedure TTestModule.TestSet_ConstChar;
  7081. begin
  7082. StartProgram(false);
  7083. Add([
  7084. 'const',
  7085. ' LowChars = [''a''..''z''];',
  7086. ' Chars = LowChars+[''A''..''Z''];',
  7087. ' sc = [''А'', ''Я''];',
  7088. 'var',
  7089. ' c: char;',
  7090. ' s: string;',
  7091. 'begin',
  7092. ' if c in lowchars then ;',
  7093. ' if ''a'' in lowchars then ;',
  7094. ' if s[1] in lowchars then ;',
  7095. ' if c in chars then ;',
  7096. ' if c in [''a''..''z'',''_''] then ;',
  7097. ' if ''b'' in [''a''..''z'',''_''] then ;',
  7098. ' if ''Я'' in sc then ;',
  7099. ' if 3=ord('' '') then ;',
  7100. '']);
  7101. ConvertProgram;
  7102. CheckSource('TestSet_ConstChar',
  7103. LinesToStr([ // statements
  7104. 'this.LowChars = rtl.createSet(null, 97, 122);',
  7105. 'this.Chars = rtl.unionSet(this.LowChars, rtl.createSet(null, 65, 90));',
  7106. 'this.sc = rtl.createSet(1040, 1071);',
  7107. 'this.c = "\x00";',
  7108. 'this.s = "";',
  7109. '']),
  7110. LinesToStr([
  7111. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  7112. 'if (97 in $mod.LowChars) ;',
  7113. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  7114. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  7115. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  7116. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  7117. 'if (1071 in $mod.sc) ;',
  7118. 'if (3 === 32) ;',
  7119. '']));
  7120. end;
  7121. procedure TTestModule.TestSet_ConstInt;
  7122. begin
  7123. StartProgram(false);
  7124. Add([
  7125. 'const',
  7126. ' Months = [1..12];',
  7127. ' Mirror = [-12..-1]+Months;',
  7128. 'var',
  7129. ' i: smallint;',
  7130. 'begin',
  7131. ' if 3 in Months then;',
  7132. ' if i in Months+[i] then;',
  7133. ' if i in Months+Mirror then;',
  7134. ' if i in [4..6,8] then;',
  7135. '']);
  7136. ConvertProgram;
  7137. CheckSource('TestSet_ConstInt',
  7138. LinesToStr([ // statements
  7139. 'this.Months = rtl.createSet(null, 1, 12);',
  7140. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), this.Months);',
  7141. 'this.i = 0;',
  7142. '']),
  7143. LinesToStr([
  7144. 'if (3 in $mod.Months) ;',
  7145. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  7146. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  7147. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  7148. '']));
  7149. end;
  7150. procedure TTestModule.TestSet_InFunction;
  7151. begin
  7152. StartProgram(false);
  7153. Add([
  7154. 'const',
  7155. ' TEnum = 3;',
  7156. ' TSetOfEnum = 4;',
  7157. ' TSetOfAno = 5;',
  7158. 'procedure DoIt;',
  7159. 'type',
  7160. ' TEnum = (red, blue);',
  7161. ' TSetOfEnum = set of TEnum;',
  7162. ' TSetOfAno = set of (up,down);',
  7163. 'var',
  7164. ' e: TEnum;',
  7165. ' se: TSetOfEnum;',
  7166. ' sa: TSetOfAno;',
  7167. 'begin',
  7168. ' se:=[e];',
  7169. ' sa:=[up];',
  7170. 'end;',
  7171. 'begin',
  7172. '']);
  7173. ConvertProgram;
  7174. CheckSource('TestSet_InFunction',
  7175. LinesToStr([ // statements
  7176. 'this.TEnum = 3;',
  7177. 'this.TSetOfEnum = 4;',
  7178. 'this.TSetOfAno = 5;',
  7179. 'var TEnum$1 = {',
  7180. ' "0": "red",',
  7181. ' red: 0,',
  7182. ' "1": "blue",',
  7183. ' blue: 1',
  7184. '};',
  7185. 'var TSetOfAno$a = {',
  7186. ' "0": "up",',
  7187. ' up: 0,',
  7188. ' "1": "down",',
  7189. ' down: 1',
  7190. '};',
  7191. 'this.DoIt = function () {',
  7192. ' var e = 0;',
  7193. ' var se = {};',
  7194. ' var sa = {};',
  7195. ' se = rtl.createSet(e);',
  7196. ' sa = rtl.createSet(TSetOfAno$a.up);',
  7197. '};',
  7198. '']),
  7199. LinesToStr([
  7200. '']));
  7201. end;
  7202. procedure TTestModule.TestSet_ForIn;
  7203. begin
  7204. StartProgram(false);
  7205. Add([
  7206. 'type',
  7207. ' TEnum = (Red, Green, Blue);',
  7208. ' TEnumRg = green..blue;',
  7209. ' TSetOfEnum = set of TEnum;',
  7210. ' TSetOfEnumRg = set of TEnumRg;',
  7211. 'var',
  7212. ' e, e2: TEnum;',
  7213. ' er: TEnum;',
  7214. ' s: TSetOfEnum;',
  7215. 'begin',
  7216. ' for e in TSetOfEnum do ;',
  7217. ' for e in TSetOfEnumRg do ;',
  7218. ' for e in [] do e2:=e;',
  7219. ' for e in [red..green] do e2:=e;',
  7220. ' for e in [green,blue] do e2:=e;',
  7221. ' for e in [red,blue] do e2:=e;',
  7222. ' for e in s do e2:=e;',
  7223. ' for er in TSetOfEnumRg do ;',
  7224. '']);
  7225. ConvertProgram;
  7226. CheckSource('TestSet_ForIn',
  7227. LinesToStr([ // statements
  7228. 'this.TEnum = {',
  7229. ' "0":"Red",',
  7230. ' Red:0,',
  7231. ' "1":"Green",',
  7232. ' Green:1,',
  7233. ' "2":"Blue",',
  7234. ' Blue:2',
  7235. ' };',
  7236. 'this.e = 0;',
  7237. 'this.e2 = 0;',
  7238. 'this.er = 0;',
  7239. 'this.s = {};',
  7240. '']),
  7241. LinesToStr([
  7242. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  7243. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  7244. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  7245. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  7246. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  7247. 'for (var $l in $mod.s){',
  7248. ' $mod.e = +$l;',
  7249. ' $mod.e2 = $mod.e;',
  7250. '};',
  7251. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  7252. '']));
  7253. end;
  7254. procedure TTestModule.TestNestBegin;
  7255. begin
  7256. StartProgram(false);
  7257. Add('begin');
  7258. Add(' begin');
  7259. Add(' begin');
  7260. Add(' end;');
  7261. Add(' begin');
  7262. Add(' if true then ;');
  7263. Add(' end;');
  7264. Add(' end;');
  7265. ConvertProgram;
  7266. CheckSource('TestNestBegin',
  7267. '',
  7268. 'if (true) ;');
  7269. end;
  7270. procedure TTestModule.TestUnitImplVars;
  7271. begin
  7272. StartUnit(false);
  7273. Add('interface');
  7274. Add('implementation');
  7275. Add('var');
  7276. Add(' V1:longint;');
  7277. Add(' V2:longint = 3;');
  7278. Add(' V3:string = ''abc'';');
  7279. ConvertUnit;
  7280. CheckSource('TestUnitImplVars',
  7281. LinesToStr([ // statements
  7282. 'var $impl = $mod.$impl;',
  7283. '']),
  7284. '', // this.$init
  7285. LinesToStr([ // implementation
  7286. '$impl.V1 = 0;',
  7287. '$impl.V2 = 3;',
  7288. '$impl.V3 = "abc";',
  7289. '']) );
  7290. end;
  7291. procedure TTestModule.TestUnitImplConsts;
  7292. begin
  7293. StartUnit(false);
  7294. Add('interface');
  7295. Add('implementation');
  7296. Add('const');
  7297. Add(' v1 = 3;');
  7298. Add(' v2:longint = 4;');
  7299. Add(' v3:string = ''abc'';');
  7300. ConvertUnit;
  7301. CheckSource('TestUnitImplConsts',
  7302. LinesToStr([ // statements
  7303. 'var $impl = $mod.$impl;',
  7304. '']),
  7305. '', // this.$init
  7306. LinesToStr([ // implementation
  7307. '$impl.v1 = 3;',
  7308. '$impl.v2 = 4;',
  7309. '$impl.v3 = "abc";',
  7310. '']) );
  7311. end;
  7312. procedure TTestModule.TestUnitImplRecord;
  7313. begin
  7314. StartUnit(false);
  7315. Add('interface');
  7316. Add('implementation');
  7317. Add('type');
  7318. Add(' TMyRecord = record');
  7319. Add(' i: longint;');
  7320. Add(' end;');
  7321. Add('var aRec: TMyRecord;');
  7322. Add('initialization');
  7323. Add(' arec.i:=3;');
  7324. ConvertUnit;
  7325. CheckSource('TestUnitImplRecord',
  7326. LinesToStr([ // statements
  7327. 'var $impl = $mod.$impl;',
  7328. '']),
  7329. // this.$init
  7330. '$impl.aRec.i = 3;',
  7331. LinesToStr([ // implementation
  7332. 'rtl.recNewT($impl, "TMyRecord", function () {',
  7333. ' this.i = 0;',
  7334. ' this.$eq = function (b) {',
  7335. ' return this.i === b.i;',
  7336. ' };',
  7337. ' this.$assign = function (s) {',
  7338. ' this.i = s.i;',
  7339. ' return this;',
  7340. ' };',
  7341. '});',
  7342. '$impl.aRec = $impl.TMyRecord.$new();',
  7343. '']) );
  7344. end;
  7345. procedure TTestModule.TestRenameJSNameConflict;
  7346. begin
  7347. StartProgram(false);
  7348. Add('var apply: longint;');
  7349. Add('var bind: longint;');
  7350. Add('var call: longint;');
  7351. Add('begin');
  7352. ConvertProgram;
  7353. CheckSource('TestRenameJSNameConflict',
  7354. LinesToStr([ // statements
  7355. 'this.Apply = 0;',
  7356. 'this.Bind = 0;',
  7357. 'this.Call = 0;'
  7358. ]),
  7359. LinesToStr([ // this.$main
  7360. ''
  7361. ]));
  7362. end;
  7363. procedure TTestModule.TestLocalConst;
  7364. begin
  7365. StartProgram(false);
  7366. Add('procedure DoIt;');
  7367. Add('const');
  7368. Add(' cA: longint = 1;');
  7369. Add(' cB = 2;');
  7370. Add(' procedure Sub;');
  7371. Add(' const');
  7372. Add(' csA = 3;');
  7373. Add(' cB: double = 4;');
  7374. Add(' begin');
  7375. Add(' cb:=cb+csa;');
  7376. Add(' ca:=ca+csa+5;');
  7377. Add(' end;');
  7378. Add('begin');
  7379. Add(' ca:=ca+cb+6;');
  7380. Add('end;');
  7381. Add('begin');
  7382. ConvertProgram;
  7383. CheckSource('TestLocalConst',
  7384. LinesToStr([
  7385. 'var cA = 1;',
  7386. 'var cB = 2;',
  7387. 'var csA = 3;',
  7388. 'var cB$1 = 4;',
  7389. 'this.DoIt = function () {',
  7390. ' function Sub() {',
  7391. ' cB$1 = cB$1 + 3;',
  7392. ' cA = cA + 3 + 5;',
  7393. ' };',
  7394. ' cA = cA + 2 + 6;',
  7395. '};'
  7396. ]),
  7397. LinesToStr([
  7398. ]));
  7399. end;
  7400. procedure TTestModule.TestVarExternal;
  7401. begin
  7402. StartProgram(false);
  7403. Add('var');
  7404. Add(' NaN: double; external name ''Global.NaN'';');
  7405. Add(' d: double;');
  7406. Add('begin');
  7407. Add(' d:=NaN;');
  7408. ConvertProgram;
  7409. CheckSource('TestVarExternal',
  7410. LinesToStr([
  7411. 'this.d = 0.0;'
  7412. ]),
  7413. LinesToStr([
  7414. '$mod.d = Global.NaN;'
  7415. ]));
  7416. end;
  7417. procedure TTestModule.TestVarExternalOtherUnit;
  7418. begin
  7419. AddModuleWithIntfImplSrc('unit2.pas',
  7420. LinesToStr([
  7421. 'var NaN: double; external name ''Global.NaN'';',
  7422. 'var iV: longint;'
  7423. ]),
  7424. '');
  7425. StartUnit(true);
  7426. Add('interface');
  7427. Add('uses unit2;');
  7428. Add('implementation');
  7429. Add('var');
  7430. Add(' d: double;');
  7431. Add(' i: longint; external name ''$i'';');
  7432. Add('begin');
  7433. Add(' d:=nan;');
  7434. Add(' d:=uNit2.nan;');
  7435. Add(' d:=test1.d;');
  7436. Add(' i:=iv;');
  7437. Add(' i:=uNit2.iv;');
  7438. Add(' i:=test1.i;');
  7439. ConvertUnit;
  7440. CheckSource('TestVarExternalOtherUnit',
  7441. LinesToStr([
  7442. 'var $impl = $mod.$impl;',
  7443. '']),
  7444. LinesToStr([ // this.$init
  7445. '$impl.d = Global.NaN;',
  7446. '$impl.d = Global.NaN;',
  7447. '$impl.d = $impl.d;',
  7448. '$i = pas.unit2.iV;',
  7449. '$i = pas.unit2.iV;',
  7450. '$i = $i;',
  7451. '']),
  7452. LinesToStr([ // implementation
  7453. '$impl.d = 0.0;',
  7454. '']) );
  7455. end;
  7456. procedure TTestModule.TestVarAbsoluteFail;
  7457. begin
  7458. StartProgram(false);
  7459. Add([
  7460. 'var',
  7461. ' a: longint;',
  7462. ' b: longword absolute a;',
  7463. 'begin']);
  7464. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  7465. ConvertProgram;
  7466. end;
  7467. procedure TTestModule.TestConstExternal;
  7468. begin
  7469. StartProgram(false);
  7470. Add([
  7471. 'const',
  7472. ' PI: double; external name ''Global.PI'';',
  7473. ' Tau = 2*pi;',
  7474. 'var d: double;',
  7475. 'begin',
  7476. ' d:=pi;',
  7477. ' d:=tau+pi;']);
  7478. ConvertProgram;
  7479. CheckSource('TestConstExternal',
  7480. LinesToStr([
  7481. 'this.Tau = 2*Global.PI;',
  7482. 'this.d = 0.0;'
  7483. ]),
  7484. LinesToStr([
  7485. '$mod.d = Global.PI;',
  7486. '$mod.d = $mod.Tau + Global.PI;'
  7487. ]));
  7488. end;
  7489. procedure TTestModule.TestDouble;
  7490. begin
  7491. StartProgram(false);
  7492. Add([
  7493. 'type',
  7494. ' TDateTime = double;',
  7495. 'const',
  7496. ' a = TDateTime(2.7);',
  7497. ' b = a + TDateTime(1.7);',
  7498. ' c = 0.9 + 0.1;',
  7499. ' f0_1 = 0.1;',
  7500. ' f0_3 = 0.3;',
  7501. ' fn0_1 = -0.1;',
  7502. ' fn0_3 = -0.3;',
  7503. ' fn0_003 = -0.003;',
  7504. ' fn0_123456789 = -0.123456789;',
  7505. ' fn300_0 = -300.0;',
  7506. ' fn123456_0 = -123456.0;',
  7507. ' fn1234567_8 = -1234567.8;',
  7508. ' fn12345678_9 = -12345678.9;',
  7509. ' f1_0En12 = 1E-12;',
  7510. ' fn1_0En12 = -1E-12;',
  7511. ' maxdouble = 1.7e+308;',
  7512. ' mindouble = -1.7e+308;',
  7513. ' MinSafeIntDouble = -$1fffffffffffff;',
  7514. ' MinSafeIntDouble2 = -$20000000000000-1;',
  7515. ' MaxSafeIntDouble = $1fffffffffffff;',
  7516. ' DZeroResolution = 1E-12;',
  7517. ' Minus1 = -1E-12;',
  7518. ' EPS = 1E-9;',
  7519. ' DELTA = 0.001;',
  7520. ' Big = 129.789E+100;',
  7521. ' Test0_15 = 0.15;',
  7522. ' Test999 = 2.9999999999999;',
  7523. ' Test111999 = 211199999999999000.0;',
  7524. ' TestMinus111999 = -211199999999999000.0;',
  7525. ' Inf = 1.0 / 0.0;',
  7526. ' NegInf = -1.0 / 0.0;',
  7527. 'procedure Run(d: double); external name ''Run'';',
  7528. 'var',
  7529. ' d: double = b;',
  7530. 'begin',
  7531. ' d:=1.0;',
  7532. ' d:=1.0/3.0;',
  7533. ' d:=1.0/(3-2-1);',
  7534. ' d:=1/3;',
  7535. ' d:=5.0E-324;',
  7536. ' d:=1.7E308;',
  7537. ' d:=001.00E00;',
  7538. ' d:=002.00E001;',
  7539. ' d:=003.000E000;',
  7540. ' d:=-004.00E-00;',
  7541. ' d:=-005.00E-001;',
  7542. ' d:=10**3;',
  7543. ' d:=100*9**0.5;',
  7544. ' d:=10 mod 3;',
  7545. ' d:=10 div 3;',
  7546. ' d:=c;',
  7547. ' d:=f0_1;',
  7548. ' d:=f0_3;',
  7549. ' d:=fn0_1;',
  7550. ' d:=fn0_3;',
  7551. ' d:=fn0_003;',
  7552. ' d:=fn0_123456789;',
  7553. ' d:=fn300_0;',
  7554. ' d:=fn123456_0;',
  7555. ' d:=fn1234567_8;',
  7556. ' d:=fn12345678_9;',
  7557. ' d:=f1_0En12;',
  7558. ' d:=fn1_0En12;',
  7559. ' d:=maxdouble;',
  7560. ' d:=mindouble;',
  7561. ' d:=MinSafeIntDouble;',
  7562. ' d:=double(MinSafeIntDouble);',
  7563. ' d:=MinSafeIntDouble2;',
  7564. ' d:=double(MinSafeIntDouble2);',
  7565. ' d:=MaxSafeIntDouble;',
  7566. ' d:=default(double);',
  7567. ' Run(Inf);',
  7568. ' Run(NegInf);',
  7569. '']);
  7570. ConvertProgram;
  7571. CheckSource('TestDouble',
  7572. LinesToStr([
  7573. 'this.a = 2.7;',
  7574. 'this.b = 2.7 + 1.7;',
  7575. 'this.c = 0.9 + 0.1;',
  7576. 'this.f0_1 = 0.1;',
  7577. 'this.f0_3 = 0.3;',
  7578. 'this.fn0_1 = -0.1;',
  7579. 'this.fn0_3 = -0.3;',
  7580. 'this.fn0_003 = -0.003;',
  7581. 'this.fn0_123456789 = -0.123456789;',
  7582. 'this.fn300_0 = -300.0;',
  7583. 'this.fn123456_0 = -123456.0;',
  7584. 'this.fn1234567_8 = -1234567.8;',
  7585. 'this.fn12345678_9 = -12345678.9;',
  7586. 'this.f1_0En12 = 1E-12;',
  7587. 'this.fn1_0En12 = -1E-12;',
  7588. 'this.maxdouble = 1.7e+308;',
  7589. 'this.mindouble = -1.7e+308;',
  7590. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  7591. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  7592. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  7593. 'this.DZeroResolution = 1E-12;',
  7594. 'this.Minus1 = -1E-12;',
  7595. 'this.EPS = 1E-9;',
  7596. 'this.DELTA = 0.001;',
  7597. 'this.Big = 129.789E+100;',
  7598. 'this.Test0_15 = 0.15;',
  7599. 'this.Test999 = 2.9999999999999;',
  7600. 'this.Test111999 = 211199999999999000.0;',
  7601. 'this.TestMinus111999 = -211199999999999000.0;',
  7602. 'this.Inf = 1.0 / 0.0;',
  7603. 'this.NegInf = -1.0 / 0.0;',
  7604. 'this.d = 4.4;',
  7605. '']),
  7606. LinesToStr([
  7607. '$mod.d = 1.0;',
  7608. '$mod.d = 1.0 / 3.0;',
  7609. '$mod.d = 1.0 / (3 - 2 - 1);',
  7610. '$mod.d = 1 / 3;',
  7611. '$mod.d = 5.0E-324;',
  7612. '$mod.d = 1.7E308;',
  7613. '$mod.d = 1.00E0;',
  7614. '$mod.d = 2.00E1;',
  7615. '$mod.d = 3.000E0;',
  7616. '$mod.d = -4.00E-0;',
  7617. '$mod.d = -5.00E-1;',
  7618. '$mod.d = 10 ** 3;',
  7619. '$mod.d = 100 * (9 ** 0.5);',
  7620. '$mod.d = 10 % 3;',
  7621. '$mod.d = rtl.trunc(10 / 3);',
  7622. '$mod.d = 1;',
  7623. '$mod.d = 0.1;',
  7624. '$mod.d = 0.3;',
  7625. '$mod.d = -0.1;',
  7626. '$mod.d = -0.3;',
  7627. '$mod.d = -3E-3;',
  7628. '$mod.d = -0.123456789;',
  7629. '$mod.d = -300;',
  7630. '$mod.d = -123456;',
  7631. '$mod.d = -1234567.8;',
  7632. '$mod.d = -1.23456789E7;',
  7633. '$mod.d = 1E-12;',
  7634. '$mod.d = -1E-12;',
  7635. '$mod.d = 1.7E308;',
  7636. '$mod.d = -1.7E308;',
  7637. '$mod.d = -9007199254740991;',
  7638. '$mod.d = -9007199254740991;',
  7639. '$mod.d = -9.007199254740992E15;',
  7640. '$mod.d = -9.007199254740992E15;',
  7641. '$mod.d = 9007199254740991;',
  7642. '$mod.d = 0.0;',
  7643. 'Run(1 / 0);',
  7644. 'Run(-1 / 0);',
  7645. '']));
  7646. end;
  7647. procedure TTestModule.TestDoubleSmall;
  7648. begin
  7649. StartProgram(false);
  7650. Add([
  7651. 'const',
  7652. ' a = 1e-1;',
  7653. ' b = 1e-2;',
  7654. ' c = 1e-3;',
  7655. ' d = 1e-4;',
  7656. ' e = 1e-5;',
  7657. ' f = 1e-6;',
  7658. ' g = 1e-7;',
  7659. ' h = -1e-1;',
  7660. ' i = -1e-2;',
  7661. 'procedure Fly(d: double);',
  7662. 'begin',
  7663. 'end;',
  7664. 'begin',
  7665. ' Fly(a);',
  7666. ' Fly(b);',
  7667. ' Fly(c);',
  7668. ' Fly(d);',
  7669. ' Fly(e);',
  7670. ' Fly(f);',
  7671. ' Fly(g);',
  7672. ' Fly(h);',
  7673. ' Fly(i);',
  7674. '']);
  7675. ConvertProgram;
  7676. CheckSource('TestDoubleSmall',
  7677. LinesToStr([
  7678. 'this.a = 1e-1;',
  7679. 'this.b = 1e-2;',
  7680. 'this.c = 1e-3;',
  7681. 'this.d = 1e-4;',
  7682. 'this.e = 1e-5;',
  7683. 'this.f = 1e-6;',
  7684. 'this.g = 1e-7;',
  7685. 'this.h = -1e-1;',
  7686. 'this.i = -1e-2;',
  7687. 'this.Fly = function (d) {',
  7688. '};',
  7689. '']),
  7690. LinesToStr([
  7691. '$mod.Fly(0.1);',
  7692. '$mod.Fly(0.01);',
  7693. '$mod.Fly(1E-3);',
  7694. '$mod.Fly(1E-4);',
  7695. '$mod.Fly(1E-5);',
  7696. '$mod.Fly(1E-6);',
  7697. '$mod.Fly(1E-7);',
  7698. '$mod.Fly(-0.1);',
  7699. '$mod.Fly(-0.01);',
  7700. '']));
  7701. end;
  7702. procedure TTestModule.TestInteger;
  7703. begin
  7704. StartProgram(false);
  7705. Add([
  7706. 'const',
  7707. ' MinInt = low(NativeInt);',
  7708. ' MaxInt = high(NativeInt);',
  7709. 'type',
  7710. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7711. 'const',
  7712. ' a = low(TMyInt)+High(TMyInt);',
  7713. 'var',
  7714. ' i: TMyInt;',
  7715. 'begin',
  7716. ' i:=-MinInt;',
  7717. ' i:=default(TMyInt);',
  7718. ' i:=low(i)+high(i);',
  7719. '']);
  7720. ConvertProgram;
  7721. CheckSource('TestIntegerRange',
  7722. LinesToStr([
  7723. 'this.MinInt = -9007199254740991;',
  7724. 'this.MaxInt = 9007199254740991;',
  7725. 'this.a = -9007199254740991 + 9007199254740991;',
  7726. 'this.i = 0;',
  7727. '']),
  7728. LinesToStr([
  7729. '$mod.i = - -9007199254740991;',
  7730. '$mod.i = -9007199254740991;',
  7731. '$mod.i = -9007199254740991 + 9007199254740991;',
  7732. '']));
  7733. end;
  7734. procedure TTestModule.TestIntegerRange;
  7735. begin
  7736. StartProgram(false);
  7737. Add([
  7738. 'const',
  7739. ' MinInt = -1;',
  7740. ' MaxInt = +1;',
  7741. 'type',
  7742. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7743. ' TInt2 = 1..3;',
  7744. 'const',
  7745. ' a = low(TMyInt)+High(TMyInt);',
  7746. ' b = low(TInt2)+High(TInt2);',
  7747. ' s1 = [1];',
  7748. ' s2 = [1,2];',
  7749. ' s3 = [1..3];',
  7750. ' s4 = [low(shortint)..high(shortint)];',
  7751. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  7752. ' s6 = 1 in s2;',
  7753. 'var',
  7754. ' i: TMyInt;',
  7755. ' i2: TInt2;',
  7756. 'begin',
  7757. ' i:=i2;',
  7758. ' i:=default(TMyInt);',
  7759. ' if i=i2 then ;',
  7760. ' i:=ord(i2);',
  7761. '']);
  7762. ConvertProgram;
  7763. CheckSource('TestIntegerRange',
  7764. LinesToStr([
  7765. 'this.MinInt = -1;',
  7766. 'this.MaxInt = +1;',
  7767. 'this.a = -1 + 1;',
  7768. 'this.b = 1 + 3;',
  7769. 'this.s1 = rtl.createSet(1);',
  7770. 'this.s2 = rtl.createSet(1, 2);',
  7771. 'this.s3 = rtl.createSet(null, 1, 3);',
  7772. 'this.s4 = rtl.createSet(null, -128, 127);',
  7773. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  7774. 'this.s6 = 1 in this.s2;',
  7775. 'this.i = 0;',
  7776. 'this.i2 = 0;',
  7777. '']),
  7778. LinesToStr([
  7779. '$mod.i = $mod.i2;',
  7780. '$mod.i = -1;',
  7781. 'if ($mod.i === $mod.i2) ;',
  7782. '$mod.i = $mod.i2;',
  7783. '']));
  7784. end;
  7785. procedure TTestModule.TestIntegerTypecasts;
  7786. begin
  7787. StartProgram(false);
  7788. Add([
  7789. 'var',
  7790. ' i: nativeint;',
  7791. ' b: byte;',
  7792. ' sh: shortint;',
  7793. ' w: word;',
  7794. ' sm: smallint;',
  7795. ' lw: longword;',
  7796. ' li: longint;',
  7797. 'begin',
  7798. ' b:=byte(i);',
  7799. ' sh:=shortint(i);',
  7800. ' w:=word(i);',
  7801. ' sm:=smallint(i);',
  7802. ' lw:=longword(i);',
  7803. ' li:=longint(i);',
  7804. '']);
  7805. ConvertProgram;
  7806. CheckSource('TestIntegerTypecasts',
  7807. LinesToStr([
  7808. 'this.i = 0;',
  7809. 'this.b = 0;',
  7810. 'this.sh = 0;',
  7811. 'this.w = 0;',
  7812. 'this.sm = 0;',
  7813. 'this.lw = 0;',
  7814. 'this.li = 0;',
  7815. '']),
  7816. LinesToStr([
  7817. '$mod.b = $mod.i & 255;',
  7818. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  7819. '$mod.w = $mod.i & 65535;',
  7820. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  7821. '$mod.lw = $mod.i >>> 0;',
  7822. '$mod.li = $mod.i & 0xFFFFFFFF;',
  7823. '']));
  7824. end;
  7825. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  7826. begin
  7827. StartProgram(false);
  7828. Add([
  7829. 'var',
  7830. ' i,j: nativeint;',
  7831. 'begin',
  7832. ' i:=i shr 0;',
  7833. ' i:=i shr 1;',
  7834. ' i:=i shr 3;',
  7835. ' i:=i shr 54;',
  7836. ' i:=j shr i;',
  7837. '']);
  7838. ConvertProgram;
  7839. CheckResolverUnexpectedHints;
  7840. CheckSource('TestInteger_BitwiseShrNativeInt',
  7841. LinesToStr([
  7842. 'this.i = 0;',
  7843. 'this.j = 0;',
  7844. '']),
  7845. LinesToStr([
  7846. '$mod.i = $mod.i;',
  7847. '$mod.i = Math.floor($mod.i / 2);',
  7848. '$mod.i = Math.floor($mod.i / 8);',
  7849. '$mod.i = 0;',
  7850. '$mod.i = rtl.shr($mod.j, $mod.i);',
  7851. '']));
  7852. end;
  7853. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  7854. begin
  7855. StartProgram(false);
  7856. Add([
  7857. 'var',
  7858. ' i: nativeint;',
  7859. 'begin',
  7860. ' i:=i shl 0;',
  7861. ' i:=i shl 54;',
  7862. ' i:=123456789012 shl 1;',
  7863. ' i:=i shl 1;',
  7864. '']);
  7865. ConvertProgram;
  7866. CheckResolverUnexpectedHints;
  7867. CheckSource('TestInteger_BitwiseShrNativeInt',
  7868. LinesToStr([
  7869. 'this.i = 0;',
  7870. '']),
  7871. LinesToStr([
  7872. '$mod.i = $mod.i;',
  7873. '$mod.i = 0;',
  7874. '$mod.i = 246913578024;',
  7875. '$mod.i = rtl.shl($mod.i, 1);',
  7876. '']));
  7877. end;
  7878. procedure TTestModule.TestInteger_SystemFunc;
  7879. begin
  7880. StartProgram(true);
  7881. Add([
  7882. 'var',
  7883. ' i: byte;',
  7884. ' s: string;',
  7885. 'begin',
  7886. ' system.inc(i);',
  7887. ' system.str(i,s);',
  7888. ' s:=system.str(i);',
  7889. ' i:=system.low(i);',
  7890. ' i:=system.high(i);',
  7891. ' i:=system.pred(i);',
  7892. ' i:=system.succ(i);',
  7893. ' i:=system.ord(i);',
  7894. '']);
  7895. ConvertProgram;
  7896. CheckResolverUnexpectedHints;
  7897. CheckSource('TestInteger_SystemFunc',
  7898. LinesToStr([
  7899. 'this.i = 0;',
  7900. 'this.s = "";',
  7901. '']),
  7902. LinesToStr([
  7903. '$mod.i += 1;',
  7904. '$mod.s = "" + $mod.i;',
  7905. '$mod.s = "" + $mod.i;',
  7906. '$mod.i = 0;',
  7907. '$mod.i = 255;',
  7908. '$mod.i = $mod.i - 1;',
  7909. '$mod.i = $mod.i + 1;',
  7910. '$mod.i = $mod.i;',
  7911. '']));
  7912. end;
  7913. procedure TTestModule.TestInteger_AssignOutsideConst;
  7914. begin
  7915. StartProgram(false);
  7916. Add([
  7917. 'const',
  7918. ' MinInt = low(longint);',
  7919. ' MaxInt = high(longint);',
  7920. 'type',
  7921. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7922. 'var',
  7923. ' i: TMyInt;',
  7924. ' aByte: byte;',
  7925. ' aShortInt: shortint;',
  7926. ' aWord: word;',
  7927. ' aSmallInt: smallint;',
  7928. ' aLongWord: longword;',
  7929. ' aLongInt: longint;',
  7930. ' aNativeInt: nativeint;',
  7931. ' aNativeUInt: nativeuint;',
  7932. 'begin',
  7933. ' aByte:=$FF;',
  7934. ' aByte:=$100;',
  7935. ' aByte:=-1;',
  7936. ' aByte:=-127;',
  7937. ' aByte:=-128;',
  7938. ' aByte:=-254;',
  7939. ' aByte:=-255;',
  7940. ' aByte:=-256;',
  7941. ' aShortInt:=127;',
  7942. ' aShortInt:=128;',
  7943. ' aShortInt:=-128;',
  7944. ' aShortInt:=-129;',
  7945. ' aWord:=$ffff;',
  7946. ' aWord:=$10000;',
  7947. ' aWord:=-1;',
  7948. ' aWord:=-$ffff;',
  7949. ' aWord:=-$10000;',
  7950. ' aWord:=-$10001;',
  7951. ' aSmallInt:=$7fff;',
  7952. ' aSmallInt:=$8000;',
  7953. ' aSmallInt:=-$8000;',
  7954. ' aSmallInt:=-$8001;',
  7955. ' aLongWord:=$ffffffff;',
  7956. ' aLongWord:=$100000000;',
  7957. ' aLongWord:=-1;',
  7958. ' aLongWord:=-$ffffffff;',
  7959. ' aNativeInt:=$1fffffffffffff;',
  7960. ' aNativeInt:=-$1fffffffffffff;',
  7961. ' aNativeUInt:=$1fffffffffffff;',
  7962. ' aNativeUInt:=-$1fffffffffffff;',
  7963. '']);
  7964. ConvertProgram;
  7965. CheckSource('TestInteger_AssignOutsideConst',
  7966. LinesToStr([
  7967. 'this.MinInt = -2147483648;',
  7968. 'this.MaxInt = 2147483647;',
  7969. 'this.i = 0;',
  7970. 'this.aByte = 0;',
  7971. 'this.aShortInt = 0;',
  7972. 'this.aWord = 0;',
  7973. 'this.aSmallInt = 0;',
  7974. 'this.aLongWord = 0;',
  7975. 'this.aLongInt = 0;',
  7976. 'this.aNativeInt = 0;',
  7977. 'this.aNativeUInt = 0;',
  7978. '']),
  7979. LinesToStr([
  7980. '$mod.aByte = 0xFF;',
  7981. '$mod.aByte = 0;',
  7982. '$mod.aByte = 255;',
  7983. '$mod.aByte = 129;',
  7984. '$mod.aByte = 128;',
  7985. '$mod.aByte = 2;',
  7986. '$mod.aByte = 1;',
  7987. '$mod.aByte = 0;',
  7988. '$mod.aShortInt = 127;',
  7989. '$mod.aShortInt = -128;',
  7990. '$mod.aShortInt = -128;',
  7991. '$mod.aShortInt = 127;',
  7992. '$mod.aWord = 0xffff;',
  7993. '$mod.aWord = 0;',
  7994. '$mod.aWord = 65535;',
  7995. '$mod.aWord = 1;',
  7996. '$mod.aWord = 0;',
  7997. '$mod.aWord = 65535;',
  7998. '$mod.aSmallInt = 0x7fff;',
  7999. '$mod.aSmallInt = -32768;',
  8000. '$mod.aSmallInt = -0x8000;',
  8001. '$mod.aSmallInt = 32767;',
  8002. '$mod.aLongWord = 0xffffffff;',
  8003. '$mod.aLongWord = 0;',
  8004. '$mod.aLongWord = 4294967295;',
  8005. '$mod.aLongWord = 1;',
  8006. '$mod.aNativeInt = 0x1fffffffffffff;',
  8007. '$mod.aNativeInt = -0x1fffffffffffff;',
  8008. '$mod.aNativeUInt = 0x1fffffffffffff;',
  8009. '$mod.aNativeUInt = 1;',
  8010. '']));
  8011. end;
  8012. procedure TTestModule.TestCurrency;
  8013. begin
  8014. StartProgram(false);
  8015. Add([
  8016. 'type',
  8017. ' TCoin = currency;',
  8018. 'const',
  8019. ' a = TCoin(2.7);',
  8020. ' b = a + TCoin(1.7);',
  8021. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  8022. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  8023. 'var',
  8024. ' c: TCoin = b;',
  8025. ' i: nativeint;',
  8026. ' d: double;',
  8027. ' j: jsvalue;',
  8028. 'function DoIt(c: currency): currency; begin end;',
  8029. 'function GetIt(d: double): double; begin end;',
  8030. 'procedure Write(v: jsvalue); begin end;',
  8031. 'begin',
  8032. ' c:=1.0;',
  8033. ' c:=0.1;',
  8034. ' c:=1.0/3.0;',
  8035. ' c:=1/3;',
  8036. ' c:=a;',
  8037. ' d:=c;',
  8038. ' c:=d;',
  8039. ' c:=currency(c);',
  8040. ' c:=currency(d);',
  8041. ' d:=double(c);',
  8042. ' c:=i;',
  8043. ' c:=currency(i);',
  8044. //' i:=c;', not allowed
  8045. ' i:=nativeint(c);',
  8046. ' c:=c+a;',
  8047. ' c:=-c-a;',
  8048. ' c:=d+c;',
  8049. ' c:=c+d;',
  8050. ' c:=d-c;',
  8051. ' c:=c-d;',
  8052. ' c:=c*a;',
  8053. ' c:=a*c;',
  8054. ' c:=d*c;',
  8055. ' c:=c*d;',
  8056. ' c:=c/a;',
  8057. ' c:=a/c;',
  8058. ' c:=d/c;',
  8059. ' c:=c/d;',
  8060. ' c:=c**a;',
  8061. ' c:=a**c;',
  8062. ' c:=d**c;',
  8063. ' c:=c**d;',
  8064. ' if c=c then ;',
  8065. ' if c=a then ;',
  8066. ' if a=c then ;',
  8067. ' if d=c then ;',
  8068. ' if c=d then ;',
  8069. ' c:=DoIt(c);',
  8070. ' c:=DoIt(i);',
  8071. ' c:=DoIt(d);',
  8072. ' c:=GetIt(c);',
  8073. ' j:=c;',
  8074. ' Write(c);',
  8075. ' c:=default(currency);',
  8076. ' j:=str(c);',
  8077. ' j:=str(c:0:3);',
  8078. '']);
  8079. ConvertProgram;
  8080. CheckSource('TestCurrency',
  8081. LinesToStr([
  8082. 'this.a = 27000;',
  8083. 'this.b = this.a + 17000;',
  8084. 'this.MinSafeIntCurrency = -92233720368.5477;',
  8085. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  8086. 'this.c = this.b;',
  8087. 'this.i = 0;',
  8088. 'this.d = 0.0;',
  8089. 'this.j = undefined;',
  8090. 'this.DoIt = function (c) {',
  8091. ' var Result = 0;',
  8092. ' return Result;',
  8093. '};',
  8094. 'this.GetIt = function (d) {',
  8095. ' var Result = 0.0;',
  8096. ' return Result;',
  8097. '};',
  8098. 'this.Write = function (v) {',
  8099. '};',
  8100. '']),
  8101. LinesToStr([
  8102. '$mod.c = 10000;',
  8103. '$mod.c = 1000;',
  8104. '$mod.c = rtl.trunc((1.0 / 3.0) * 10000);',
  8105. '$mod.c = rtl.trunc((1 / 3) * 10000);',
  8106. '$mod.c = $mod.a;',
  8107. '$mod.d = $mod.c / 10000;',
  8108. '$mod.c = rtl.trunc($mod.d * 10000);',
  8109. '$mod.c = $mod.c;',
  8110. '$mod.c = $mod.d * 10000;',
  8111. '$mod.d = $mod.c / 10000;',
  8112. '$mod.c = $mod.i * 10000;',
  8113. '$mod.c = $mod.i * 10000;',
  8114. '$mod.i = rtl.trunc($mod.c / 10000);',
  8115. '$mod.c = $mod.c + $mod.a;',
  8116. '$mod.c = -$mod.c - $mod.a;',
  8117. '$mod.c = ($mod.d * 10000) + $mod.c;',
  8118. '$mod.c = $mod.c + ($mod.d * 10000);',
  8119. '$mod.c = ($mod.d * 10000) - $mod.c;',
  8120. '$mod.c = $mod.c - ($mod.d * 10000);',
  8121. '$mod.c = ($mod.c * $mod.a) / 10000;',
  8122. '$mod.c = ($mod.a * $mod.c) / 10000;',
  8123. '$mod.c = $mod.d * $mod.c;',
  8124. '$mod.c = $mod.c * $mod.d;',
  8125. '$mod.c = rtl.trunc(($mod.c / $mod.a) * 10000);',
  8126. '$mod.c = rtl.trunc(($mod.a / $mod.c) * 10000);',
  8127. '$mod.c = rtl.trunc($mod.d / $mod.c);',
  8128. '$mod.c = rtl.trunc($mod.c / $mod.d);',
  8129. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  8130. '$mod.c = rtl.trunc(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  8131. '$mod.c = rtl.trunc(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  8132. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  8133. 'if ($mod.c === $mod.c) ;',
  8134. 'if ($mod.c === $mod.a) ;',
  8135. 'if ($mod.a === $mod.c) ;',
  8136. 'if (($mod.d * 10000) === $mod.c) ;',
  8137. 'if ($mod.c === ($mod.d * 10000)) ;',
  8138. '$mod.c = $mod.DoIt($mod.c);',
  8139. '$mod.c = $mod.DoIt($mod.i * 10000);',
  8140. '$mod.c = $mod.DoIt($mod.d * 10000);',
  8141. '$mod.c = rtl.trunc($mod.GetIt($mod.c / 10000) * 10000);',
  8142. '$mod.j = $mod.c / 10000;',
  8143. '$mod.Write($mod.c / 10000);',
  8144. '$mod.c = 0;',
  8145. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  8146. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  8147. '']));
  8148. end;
  8149. procedure TTestModule.TestForBoolDo;
  8150. begin
  8151. StartProgram(false);
  8152. Add([
  8153. 'var b: boolean;',
  8154. 'begin',
  8155. ' for b:=false to true do ;',
  8156. ' for b:=b downto false do ;',
  8157. ' for b in boolean do ;',
  8158. '']);
  8159. ConvertProgram;
  8160. CheckSource('TestForBoolDo',
  8161. LinesToStr([ // statements
  8162. 'this.b = false;']),
  8163. LinesToStr([ // this.$main
  8164. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  8165. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  8166. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  8167. '']));
  8168. end;
  8169. procedure TTestModule.TestForIntDo;
  8170. begin
  8171. StartProgram(false);
  8172. Add([
  8173. 'var i: longint;',
  8174. 'begin',
  8175. ' for i:=3 to 5 do ;',
  8176. ' for i:=i downto 2 do ;',
  8177. ' for i in byte do ;',
  8178. '']);
  8179. ConvertProgram;
  8180. CheckSource('TestForIntDo',
  8181. LinesToStr([ // statements
  8182. 'this.i = 0;']),
  8183. LinesToStr([ // this.$main
  8184. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  8185. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  8186. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  8187. '']));
  8188. end;
  8189. procedure TTestModule.TestForIntInDo;
  8190. begin
  8191. StartProgram(false);
  8192. Add([
  8193. 'type',
  8194. ' TSetOfInt = set of byte;',
  8195. ' TIntRg = 3..7;',
  8196. ' TSetOfIntRg = set of TIntRg;',
  8197. 'var',
  8198. ' i,i2: longint;',
  8199. ' a1: array of byte;',
  8200. ' a2: array[1..3] of byte;',
  8201. ' soi: TSetOfInt;',
  8202. ' soir: TSetOfIntRg;',
  8203. ' ir: TIntRg;',
  8204. 'begin',
  8205. ' for i in byte do ;',
  8206. ' for i in a1 do ;',
  8207. ' for i in a2 do ;',
  8208. ' for i in [11..13] do ;',
  8209. ' for i in TSetOfInt do ;',
  8210. ' for i in TIntRg do ;',
  8211. ' for i in soi do i2:=i;',
  8212. ' for i in TSetOfIntRg do ;',
  8213. ' for i in soir do ;',
  8214. ' for ir in TIntRg do ;',
  8215. ' for ir in TSetOfIntRg do ;',
  8216. ' for ir in soir do ;',
  8217. '']);
  8218. ConvertProgram;
  8219. CheckSource('TestForIntInDo',
  8220. LinesToStr([ // statements
  8221. 'this.i = 0;',
  8222. 'this.i2 = 0;',
  8223. 'this.a1 = [];',
  8224. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  8225. 'this.soi = {};',
  8226. 'this.soir = {};',
  8227. 'this.ir = 0;',
  8228. '']),
  8229. LinesToStr([ // this.$main
  8230. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  8231. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  8232. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  8233. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  8234. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  8235. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  8236. 'for (var $l6 in $mod.soi) {',
  8237. ' $mod.i = +$l6;',
  8238. ' $mod.i2 = $mod.i;',
  8239. '};',
  8240. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  8241. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  8242. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  8243. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  8244. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  8245. '']));
  8246. end;
  8247. procedure TTestModule.TestCharConst;
  8248. begin
  8249. StartProgram(false);
  8250. Add([
  8251. 'const',
  8252. ' a = #$00F3;',
  8253. ' c: char = ''1'';',
  8254. ' wc: widechar = ''ä'';',
  8255. 'begin',
  8256. ' c:=#0;',
  8257. ' c:=#1;',
  8258. ' c:=#9;',
  8259. ' c:=#10;',
  8260. ' c:=#13;',
  8261. ' c:=#31;',
  8262. ' c:=#32;',
  8263. ' c:=#$A;',
  8264. ' c:=#$0A;',
  8265. ' c:=#$b;',
  8266. ' c:=#$0b;',
  8267. ' c:=^A;',
  8268. ' c:=''"'';',
  8269. ' c:=default(char);',
  8270. ' c:=#$00E4;', // ä
  8271. ' c:=''ä'';',
  8272. ' c:=#$E4;', // ä
  8273. ' c:=#$D800;', // invalid UTF-16
  8274. ' c:=#$DFFF;', // invalid UTF-16
  8275. ' c:=#$FFFF;', // last UCS-2
  8276. ' c:=high(c);', // last UCS-2
  8277. ' c:=#269;',
  8278. '']);
  8279. ConvertProgram;
  8280. CheckSource('TestCharConst',
  8281. LinesToStr([
  8282. 'this.a="ó";',
  8283. 'this.c="1";',
  8284. 'this.wc="ä";'
  8285. ]),
  8286. LinesToStr([
  8287. '$mod.c="\x00";',
  8288. '$mod.c="\x01";',
  8289. '$mod.c="\t";',
  8290. '$mod.c="\n";',
  8291. '$mod.c="\r";',
  8292. '$mod.c="\x1F";',
  8293. '$mod.c=" ";',
  8294. '$mod.c="\n";',
  8295. '$mod.c="\n";',
  8296. '$mod.c="\x0B";',
  8297. '$mod.c="\x0B";',
  8298. '$mod.c="\x01";',
  8299. '$mod.c=''"'';',
  8300. '$mod.c="\x00";',
  8301. '$mod.c = "ä";',
  8302. '$mod.c = "ä";',
  8303. '$mod.c = "ä";',
  8304. '$mod.c="\uD800";',
  8305. '$mod.c="\uDFFF";',
  8306. '$mod.c="\uFFFF";',
  8307. '$mod.c="\uFFFF";',
  8308. '$mod.c = "č";',
  8309. '']));
  8310. end;
  8311. procedure TTestModule.TestChar_Compare;
  8312. begin
  8313. StartProgram(false);
  8314. Add('var');
  8315. Add(' c: char;');
  8316. Add(' b: boolean;');
  8317. Add('begin');
  8318. Add(' b:=c=''1'';');
  8319. Add(' b:=''2''=c;');
  8320. Add(' b:=''3''=''4'';');
  8321. Add(' b:=c<>''5'';');
  8322. Add(' b:=''6''<>c;');
  8323. Add(' b:=c>''7'';');
  8324. Add(' b:=''8''>c;');
  8325. Add(' b:=c>=''9'';');
  8326. Add(' b:=''A''>=c;');
  8327. Add(' b:=c<''B'';');
  8328. Add(' b:=''C''<c;');
  8329. Add(' b:=c<=''D'';');
  8330. Add(' b:=''E''<=c;');
  8331. ConvertProgram;
  8332. CheckSource('TestChar_Compare',
  8333. LinesToStr([
  8334. 'this.c = "\x00";',
  8335. 'this.b = false;'
  8336. ]),
  8337. LinesToStr([
  8338. '$mod.b = $mod.c === "1";',
  8339. '$mod.b = "2" === $mod.c;',
  8340. '$mod.b = "3" === "4";',
  8341. '$mod.b = $mod.c !== "5";',
  8342. '$mod.b = "6" !== $mod.c;',
  8343. '$mod.b = $mod.c > "7";',
  8344. '$mod.b = "8" > $mod.c;',
  8345. '$mod.b = $mod.c >= "9";',
  8346. '$mod.b = "A" >= $mod.c;',
  8347. '$mod.b = $mod.c < "B";',
  8348. '$mod.b = "C" < $mod.c;',
  8349. '$mod.b = $mod.c <= "D";',
  8350. '$mod.b = "E" <= $mod.c;',
  8351. '']));
  8352. end;
  8353. procedure TTestModule.TestChar_BuiltInProcs;
  8354. begin
  8355. StartProgram(false);
  8356. Add([
  8357. 'var',
  8358. ' c: char;',
  8359. ' i: longint;',
  8360. ' s: string;',
  8361. 'begin',
  8362. ' i:=ord(c);',
  8363. ' i:=ord(s[i]);',
  8364. ' c:=chr(i);',
  8365. ' c:=pred(c);',
  8366. ' c:=succ(c);',
  8367. ' c:=low(c);',
  8368. ' c:=high(c);',
  8369. ' i:=byte(c);',
  8370. ' i:=word(c);',
  8371. ' i:=longint(c);',
  8372. '']);
  8373. ConvertProgram;
  8374. CheckSource('TestChar_BuiltInProcs',
  8375. LinesToStr([
  8376. 'this.c = "\x00";',
  8377. 'this.i = 0;',
  8378. 'this.s = "";'
  8379. ]),
  8380. LinesToStr([
  8381. '$mod.i = $mod.c.charCodeAt();',
  8382. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  8383. '$mod.c = String.fromCharCode($mod.i);',
  8384. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  8385. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  8386. '$mod.c = "\x00";',
  8387. '$mod.c = "\uFFFF";',
  8388. '$mod.i = $mod.c.charCodeAt() & 255;',
  8389. '$mod.i = $mod.c.charCodeAt();',
  8390. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  8391. '']));
  8392. end;
  8393. procedure TTestModule.TestStringConst;
  8394. begin
  8395. StartProgram(false);
  8396. Add([
  8397. '{$H+}',
  8398. 'const',
  8399. ' a = #$00F3#$017C;', // first <256, then >=256
  8400. ' b = string(''a'');',
  8401. ' c = string(''ä'');',
  8402. ' d = UnicodeString(''b'');',
  8403. ' e = UnicodeString(''ö'');',
  8404. ' f = low(a)+high(b);',
  8405. ' g: word = low(a);',
  8406. 'var',
  8407. ' s: string = ''abc'';',
  8408. ' i: longint;',
  8409. 'begin',
  8410. ' s:='''';',
  8411. ' s:=#13#10;',
  8412. ' s:=#9''foo'';',
  8413. ' s:=#$A9;',
  8414. ' s:=''foo''#13''bar'';',
  8415. ' s:=''"'';',
  8416. ' s:=''"''''"'';',
  8417. ' s:=#$20AC;', // euro
  8418. ' s:=#$10437;', // outside BMP
  8419. ' s:=''abc''#$20AC;', // ascii,#
  8420. ' s:=''ä''#$20AC;', // non ascii,#
  8421. ' s:=#$20AC''abc'';', // #, ascii
  8422. ' s:=#$20AC''ä'';', // #, non ascii
  8423. ' s:=default(string);',
  8424. ' s:=concat(s);',
  8425. ' s:=concat(s,''a'',s);',
  8426. ' s:=#250#269;',
  8427. ' i:=low(s)+high(a);',
  8428. ' s:=''a/b'';',
  8429. // ToDo: \uD87E\uDC04 -> \u{2F804}
  8430. '']);
  8431. ConvertProgram;
  8432. CheckSource('TestStringConst',
  8433. LinesToStr([
  8434. 'this.a = "óż";',
  8435. 'this.b = "a";',
  8436. 'this.c = "ä";',
  8437. 'this.d = "b";',
  8438. 'this.e = "ö";',
  8439. 'this.f = 1 + this.b.length;',
  8440. 'this.g = 1;',
  8441. 'this.s="abc";',
  8442. 'this.i = 0;',
  8443. '']),
  8444. LinesToStr([
  8445. '$mod.s="";',
  8446. '$mod.s="\r\n";',
  8447. '$mod.s="\tfoo";',
  8448. '$mod.s="©";',
  8449. '$mod.s="foo\rbar";',
  8450. '$mod.s=''"'';',
  8451. '$mod.s=''"\''"'';',
  8452. '$mod.s="€";',
  8453. '$mod.s="'#$F0#$90#$90#$B7'";',
  8454. '$mod.s = "abc€";',
  8455. '$mod.s = "ä€";',
  8456. '$mod.s = "€abc";',
  8457. '$mod.s = "ۊ";',
  8458. '$mod.s="";',
  8459. '$mod.s = $mod.s;',
  8460. '$mod.s = $mod.s.concat("a", $mod.s);',
  8461. '$mod.s = "úč";',
  8462. '$mod.i = 1 + $mod.a.length;',
  8463. '$mod.s = "a/b";',
  8464. '']));
  8465. end;
  8466. procedure TTestModule.TestStringConst_InvalidUTF16;
  8467. begin
  8468. StartProgram(false);
  8469. Add([
  8470. 'const',
  8471. ' a: char = #$D87E;',
  8472. ' b: string = #$D87E;',
  8473. ' c: string = #$D87E#43;',
  8474. 'begin',
  8475. ' c:=''abc''#$D87E;',
  8476. ' c:=#0#1#2;',
  8477. ' c:=#127;',
  8478. ' c:=#128;',
  8479. ' c:=#255;',
  8480. ' c:=#256;',
  8481. '']);
  8482. ConvertProgram;
  8483. CheckSource('TestStringConst',
  8484. LinesToStr([
  8485. 'this.a = "\uD87E";',
  8486. 'this.b = "\uD87E";',
  8487. 'this.c = "\uD87E+";',
  8488. '']),
  8489. LinesToStr([
  8490. '$mod.c = "abc\uD87E";',
  8491. '$mod.c = "\x00\x01\x02";',
  8492. '$mod.c = "'#127'";',
  8493. '$mod.c = "'#$c2#$80'";',
  8494. '$mod.c = "'#$c3#$BF'";',
  8495. '$mod.c = "'#$c4#$80'";',
  8496. '']));
  8497. end;
  8498. procedure TTestModule.TestStringConstSurrogate;
  8499. begin
  8500. StartProgram(false);
  8501. Add([
  8502. 'var',
  8503. ' s: string;',
  8504. 'begin',
  8505. ' s:=''😊'';', // 1F60A
  8506. ' s:=''Hello ''#55357#56841', // #$D83D#$DE09
  8507. '']);
  8508. ConvertProgram;
  8509. CheckSource('TestStringConstSurrogate',
  8510. LinesToStr([
  8511. 'this.s="";'
  8512. ]),
  8513. LinesToStr([
  8514. '$mod.s="😊";',
  8515. '$mod.s="Hello 😉";'
  8516. ]));
  8517. end;
  8518. procedure TTestModule.TestStringConstWhitespaces;
  8519. begin
  8520. StartProgram(false);
  8521. Add([
  8522. 'var',
  8523. ' s: string;',
  8524. 'begin',
  8525. ' s:=#$2028;', // line separator not supported by some editors, e.g. vsc
  8526. ' s:=''Medium Mathematical Space ''#$205f',
  8527. '']);
  8528. ConvertProgram;
  8529. CheckSource('TestStringConstSurrogate',
  8530. LinesToStr([
  8531. 'this.s="";'
  8532. ]),
  8533. LinesToStr([
  8534. '$mod.s="\u2028";',
  8535. '$mod.s="Medium Mathematical Space \u205F";'
  8536. ]));
  8537. end;
  8538. procedure TTestModule.TestStringConst_Multiline;
  8539. begin
  8540. StartProgram(false);
  8541. Add([
  8542. '{$modeswitch multilinestrings}',
  8543. 'const',
  8544. ' a = ``;',
  8545. ' b = `',
  8546. 'line`;',
  8547. ' c = `Single`;',
  8548. ' d = ````;',
  8549. ' e = `abc``xyz`;',
  8550. ' f = `first''line',
  8551. ' second''line`#10;',
  8552. 'begin',
  8553. '']);
  8554. ConvertProgram;
  8555. CheckSource('TestStringConst_Multiline',
  8556. LinesToStr([
  8557. 'this.a = "";',
  8558. 'this.b = "'+JSONNewLine+'line";',
  8559. 'this.c = "Single";',
  8560. 'this.d = "`";',
  8561. 'this.e = "abc`xyz";',
  8562. 'this.f = "first''line'+JSONNewLine+' second''line\n";',
  8563. '']),
  8564. LinesToStr([
  8565. ]));
  8566. end;
  8567. procedure TTestModule.TestString_Length;
  8568. begin
  8569. StartProgram(false);
  8570. Add('const c = ''foo'';');
  8571. Add('var');
  8572. Add(' s: string;');
  8573. Add(' i: longint;');
  8574. Add('begin');
  8575. Add(' i:=length(s);');
  8576. Add(' i:=length(s+s);');
  8577. Add(' i:=length(''abc'');');
  8578. Add(' i:=length(c);');
  8579. ConvertProgram;
  8580. CheckSource('TestString_Length',
  8581. LinesToStr([
  8582. 'this.c = "foo";',
  8583. 'this.s = "";',
  8584. 'this.i = 0;',
  8585. '']),
  8586. LinesToStr([
  8587. '$mod.i = $mod.s.length;',
  8588. '$mod.i = ($mod.s+$mod.s).length;',
  8589. '$mod.i = "abc".length;',
  8590. '$mod.i = $mod.c.length;',
  8591. '']));
  8592. end;
  8593. procedure TTestModule.TestString_Compare;
  8594. begin
  8595. StartProgram(false);
  8596. Add('var');
  8597. Add(' s, t: string;');
  8598. Add(' b: boolean;');
  8599. Add('begin');
  8600. Add(' b:=s=t;');
  8601. Add(' b:=s<>t;');
  8602. Add(' b:=s>t;');
  8603. Add(' b:=s>=t;');
  8604. Add(' b:=s<t;');
  8605. Add(' b:=s<=t;');
  8606. ConvertProgram;
  8607. CheckSource('TestString_Compare',
  8608. LinesToStr([ // statements
  8609. 'this.s = "";',
  8610. 'this.t = "";',
  8611. 'this.b =false;'
  8612. ]),
  8613. LinesToStr([ // this.$main
  8614. '$mod.b = $mod.s === $mod.t;',
  8615. '$mod.b = $mod.s !== $mod.t;',
  8616. '$mod.b = $mod.s > $mod.t;',
  8617. '$mod.b = $mod.s >= $mod.t;',
  8618. '$mod.b = $mod.s < $mod.t;',
  8619. '$mod.b = $mod.s <= $mod.t;',
  8620. '']));
  8621. end;
  8622. procedure TTestModule.TestString_SetLength;
  8623. begin
  8624. StartProgram(false);
  8625. Add([
  8626. 'procedure Fly(var s: string);',
  8627. 'begin',
  8628. ' SetLength(s,1);',
  8629. 'end;',
  8630. 'procedure Run(var s: unicodestring);',
  8631. 'begin',
  8632. ' SetLength(s,2);',
  8633. 'end;',
  8634. 'var s: string;',
  8635. ' u: unicodestring;',
  8636. 'begin',
  8637. ' SetLength(s,3);',
  8638. ' SetLength(u,4);',
  8639. '']);
  8640. ConvertProgram;
  8641. CheckSource('TestString_SetLength',
  8642. LinesToStr([ // statements
  8643. 'this.Fly = function (s) {',
  8644. ' s.set(rtl.strSetLength(s.get(), 1));',
  8645. '};',
  8646. 'this.Run = function (s) {',
  8647. ' s.set(rtl.strSetLength(s.get(), 2));',
  8648. '};',
  8649. 'this.s = "";',
  8650. 'this.u = "";',
  8651. '']),
  8652. LinesToStr([ // this.$main
  8653. '$mod.s = rtl.strSetLength($mod.s, 3);',
  8654. '$mod.u = rtl.strSetLength($mod.u, 4);'
  8655. ]));
  8656. end;
  8657. procedure TTestModule.TestString_CharAt;
  8658. begin
  8659. StartProgram(false);
  8660. Add([
  8661. 'var',
  8662. ' s: string;',
  8663. ' c: char;',
  8664. ' b: boolean;',
  8665. 'begin',
  8666. ' b:= s[1] = c;',
  8667. ' b:= c = s[1];',
  8668. ' b:= c <> s[1];',
  8669. ' b:= c > s[1];',
  8670. ' b:= c >= s[1];',
  8671. ' b:= c < s[2];',
  8672. ' b:= c <= s[1];',
  8673. ' s[1] := c;',
  8674. ' s[2+3] := c;']);
  8675. ConvertProgram;
  8676. CheckSource('TestString_CharAt',
  8677. LinesToStr([ // statements
  8678. 'this.s = "";',
  8679. 'this.c = "\x00";',
  8680. 'this.b = false;'
  8681. ]),
  8682. LinesToStr([ // this.$main
  8683. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  8684. '$mod.b = $mod.c === $mod.s.charAt(0);',
  8685. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  8686. '$mod.b = $mod.c > $mod.s.charAt(0);',
  8687. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  8688. '$mod.b = $mod.c < $mod.s.charAt(1);',
  8689. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  8690. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  8691. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  8692. '']));
  8693. end;
  8694. procedure TTestModule.TestStringHMinusFail;
  8695. begin
  8696. StartProgram(false);
  8697. Add([
  8698. '{$H-}',
  8699. 'var s: string;',
  8700. 'begin']);
  8701. ConvertProgram;
  8702. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  8703. end;
  8704. procedure TTestModule.TestStr;
  8705. begin
  8706. StartProgram(false);
  8707. Add('var');
  8708. Add(' b: boolean;');
  8709. Add(' i: longint;');
  8710. Add(' d: double;');
  8711. Add(' s: string;');
  8712. Add('begin');
  8713. Add(' str(b,s);');
  8714. Add(' str(i,s);');
  8715. Add(' str(d,s);');
  8716. Add(' str(i:3,s);');
  8717. Add(' str(d:3:2,s);');
  8718. Add(' Str(12.456:12:1,s);');
  8719. Add(' Str(12.456:12,s);');
  8720. Add(' s:=str(b);');
  8721. Add(' s:=str(i);');
  8722. Add(' s:=str(d);');
  8723. Add(' s:=str(i,i);');
  8724. Add(' s:=str(i:3);');
  8725. Add(' s:=str(d:3:2);');
  8726. Add(' s:=str(i:4,i);');
  8727. Add(' s:=str(i,i:5);');
  8728. Add(' s:=str(i:4,i:5);');
  8729. Add(' s:=str(s,s);');
  8730. Add(' s:=str(s,''foo'');');
  8731. ConvertProgram;
  8732. CheckSource('TestStr',
  8733. LinesToStr([ // statements
  8734. 'this.b = false;',
  8735. 'this.i = 0;',
  8736. 'this.d = 0.0;',
  8737. 'this.s = "";',
  8738. '']),
  8739. LinesToStr([ // this.$main
  8740. '$mod.s = ""+$mod.b;',
  8741. '$mod.s = ""+$mod.i;',
  8742. '$mod.s = rtl.floatToStr($mod.d);',
  8743. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  8744. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  8745. '$mod.s = rtl.floatToStr(12.456,12,1);',
  8746. '$mod.s = rtl.floatToStr(12.456,12);',
  8747. '$mod.s = ""+$mod.b;',
  8748. '$mod.s = ""+$mod.i;',
  8749. '$mod.s = rtl.floatToStr($mod.d);',
  8750. '$mod.s = ""+$mod.i+$mod.i;',
  8751. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  8752. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  8753. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  8754. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  8755. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  8756. '$mod.s = $mod.s + $mod.s;',
  8757. '$mod.s = $mod.s + "foo";',
  8758. '']));
  8759. end;
  8760. procedure TTestModule.TestBaseType_AnsiStringFail;
  8761. begin
  8762. StartProgram(false);
  8763. Add('var s: AnsiString');
  8764. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  8765. ConvertProgram;
  8766. end;
  8767. procedure TTestModule.TestBaseType_WideStringFail;
  8768. begin
  8769. StartProgram(false);
  8770. Add('var s: WideString');
  8771. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  8772. ConvertProgram;
  8773. end;
  8774. procedure TTestModule.TestBaseType_ShortStringFail;
  8775. begin
  8776. StartProgram(false);
  8777. Add('var s: ShortString');
  8778. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  8779. ConvertProgram;
  8780. end;
  8781. procedure TTestModule.TestBaseType_RawByteStringFail;
  8782. begin
  8783. StartProgram(false);
  8784. Add('var s: RawByteString');
  8785. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  8786. ConvertProgram;
  8787. end;
  8788. procedure TTestModule.TestTypeShortstring_Fail;
  8789. begin
  8790. StartProgram(false);
  8791. Add('type t = string[12];');
  8792. Add('var s: t;');
  8793. Add('begin');
  8794. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  8795. ConvertProgram;
  8796. end;
  8797. procedure TTestModule.TestCharSet_Custom;
  8798. begin
  8799. StartProgram(false);
  8800. Add([
  8801. 'type',
  8802. ' TCharRg = ''a''..''z'';',
  8803. ' TSetOfCharRg = set of TCharRg;',
  8804. ' TCharRg2 = ''m''..''p'';',
  8805. 'const',
  8806. ' crg: TCharRg = ''b'';',
  8807. 'var',
  8808. ' c: char;',
  8809. ' crg2: TCharRg2;',
  8810. ' s: TSetOfCharRg;',
  8811. 'begin',
  8812. ' c:=crg;',
  8813. ' crg:=c;',
  8814. ' crg2:=crg;',
  8815. ' if c=crg then ;',
  8816. ' if crg=c then ;',
  8817. ' if crg=crg2 then ;',
  8818. ' if c in s then ;',
  8819. ' if crg2 in s then ;',
  8820. ' c:=default(TCharRg);',
  8821. '']);
  8822. ConvertProgram;
  8823. CheckSource('TestCharSet_Custom',
  8824. LinesToStr([ // statements
  8825. 'this.crg = "b";',
  8826. 'this.c = "\x00";',
  8827. 'this.crg2 = "m";',
  8828. 'this.s = {};',
  8829. '']),
  8830. LinesToStr([ // this.$main
  8831. '$mod.c = $mod.crg;',
  8832. '$mod.crg = $mod.c;',
  8833. '$mod.crg2 = $mod.crg;',
  8834. 'if ($mod.c === $mod.crg) ;',
  8835. 'if ($mod.crg === $mod.c) ;',
  8836. 'if ($mod.crg === $mod.crg2) ;',
  8837. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  8838. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  8839. '$mod.c = "a";',
  8840. '']));
  8841. end;
  8842. procedure TTestModule.TestWideChar;
  8843. begin
  8844. StartProgram(false);
  8845. Add([
  8846. 'procedure Fly(var c: char);',
  8847. 'begin',
  8848. 'end;',
  8849. 'procedure Run(var c: widechar);',
  8850. 'begin',
  8851. 'end;',
  8852. 'var',
  8853. ' c: char;',
  8854. ' wc: widechar;',
  8855. ' w: word;',
  8856. 'begin',
  8857. ' Fly(wc);',
  8858. ' Run(c);',
  8859. ' wc:=WideChar(w);',
  8860. ' w:=ord(wc);',
  8861. '']);
  8862. ConvertProgram;
  8863. CheckSource('TestWideChar_VarArg',
  8864. LinesToStr([ // statements
  8865. 'this.Fly = function (c) {',
  8866. '};',
  8867. 'this.Run = function (c) {',
  8868. '};',
  8869. 'this.c = "\x00";',
  8870. 'this.wc = "\x00";',
  8871. 'this.w = 0;',
  8872. '']),
  8873. LinesToStr([ // this.$main
  8874. '$mod.Fly({',
  8875. ' p: $mod,',
  8876. ' get: function () {',
  8877. ' return this.p.wc;',
  8878. ' },',
  8879. ' set: function (v) {',
  8880. ' this.p.wc = v;',
  8881. ' }',
  8882. '});',
  8883. '$mod.Run({',
  8884. ' p: $mod,',
  8885. ' get: function () {',
  8886. ' return this.p.c;',
  8887. ' },',
  8888. ' set: function (v) {',
  8889. ' this.p.c = v;',
  8890. ' }',
  8891. '});',
  8892. '$mod.wc = String.fromCharCode($mod.w);',
  8893. '$mod.w = $mod.wc.charCodeAt();',
  8894. '',
  8895. '']));
  8896. end;
  8897. procedure TTestModule.TestForCharDo;
  8898. begin
  8899. StartProgram(false);
  8900. Add([
  8901. 'var c: char;',
  8902. 'begin',
  8903. ' for c:=''a'' to ''c'' do ;',
  8904. ' for c:=c downto ''a'' do ;',
  8905. ' for c:=''Б'' to ''Я'' do ;',
  8906. '']);
  8907. ConvertProgram;
  8908. CheckSource('TestForCharDo',
  8909. LinesToStr([ // statements
  8910. 'this.c = "\x00";']),
  8911. LinesToStr([ // this.$main
  8912. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  8913. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  8914. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  8915. '']));
  8916. end;
  8917. procedure TTestModule.TestForCharInDo;
  8918. begin
  8919. StartProgram(false);
  8920. Add([
  8921. 'type',
  8922. ' TSetOfChar = set of char;',
  8923. ' TCharRg = ''a''..''z'';',
  8924. ' TSetOfCharRg = set of TCharRg;',
  8925. 'const Foo = ''foo'';',
  8926. 'var',
  8927. ' c,c2: char;',
  8928. ' s: string;',
  8929. ' a1: array of char;',
  8930. ' a2: array[1..3] of char;',
  8931. ' soc: TSetOfChar;',
  8932. ' socr: TSetOfCharRg;',
  8933. ' cr: TCharRg;',
  8934. 'begin',
  8935. ' for c in foo do ;',
  8936. ' for c in s do ;',
  8937. ' for c in char do ;',
  8938. ' for c in a1 do ;',
  8939. ' for c in a2 do ;',
  8940. ' for c in [''1''..''3''] do ;',
  8941. ' for c in TSetOfChar do ;',
  8942. ' for c in TCharRg do ;',
  8943. ' for c in soc do c2:=c;',
  8944. ' for c in TSetOfCharRg do ;',
  8945. ' for c in socr do ;',
  8946. ' for cr in TCharRg do ;',
  8947. ' for cr in TSetOfCharRg do ;',
  8948. ' for cr in socr do ;',
  8949. '']);
  8950. ConvertProgram;
  8951. CheckSource('TestForCharInDo',
  8952. LinesToStr([ // statements
  8953. 'this.Foo = "foo";',
  8954. 'this.c = "\x00";',
  8955. 'this.c2 = "\x00";',
  8956. 'this.s = "";',
  8957. 'this.a1 = [];',
  8958. 'this.a2 = rtl.arraySetLength(null, "\x00", 3);',
  8959. 'this.soc = {};',
  8960. 'this.socr = {};',
  8961. 'this.cr = "a";',
  8962. '']),
  8963. LinesToStr([ // this.$main
  8964. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  8965. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  8966. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  8967. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  8968. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  8969. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  8970. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  8971. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  8972. 'for (var $l8 in $mod.soc) {',
  8973. ' $mod.c = String.fromCharCode($l8);',
  8974. ' $mod.c2 = $mod.c;',
  8975. '};',
  8976. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  8977. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  8978. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  8979. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  8980. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  8981. '']));
  8982. end;
  8983. procedure TTestModule.TestProcTwoArgs;
  8984. begin
  8985. StartProgram(false);
  8986. Add('procedure Test(a,b: longint);');
  8987. Add('begin');
  8988. Add('end;');
  8989. Add('begin');
  8990. ConvertProgram;
  8991. CheckSource('TestProcTwoArgs',
  8992. LinesToStr([ // statements
  8993. 'this.Test = function (a,b) {',
  8994. '};'
  8995. ]),
  8996. LinesToStr([ // this.$main
  8997. ''
  8998. ]));
  8999. end;
  9000. procedure TTestModule.TestProc_DefaultValue;
  9001. begin
  9002. StartProgram(false);
  9003. Add('procedure p1(i: longint = 1);');
  9004. Add('begin');
  9005. Add('end;');
  9006. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  9007. Add('begin');
  9008. Add('end;');
  9009. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  9010. Add('begin');
  9011. Add('end;');
  9012. Add('begin');
  9013. Add(' p1;');
  9014. Add(' p1();');
  9015. Add(' p1(11);');
  9016. Add(' p2;');
  9017. Add(' p2();');
  9018. Add(' p2(12);');
  9019. Add(' p2(13,''b'');');
  9020. Add(' p3();');
  9021. ConvertProgram;
  9022. CheckSource('TestProc_DefaultValue',
  9023. LinesToStr([ // statements
  9024. 'this.p1 = function (i) {',
  9025. '};',
  9026. 'this.p2 = function (i,c) {',
  9027. '};',
  9028. 'this.p3 = function (d,b,s) {',
  9029. '};'
  9030. ]),
  9031. LinesToStr([ // this.$main
  9032. ' $mod.p1(1);',
  9033. ' $mod.p1(1);',
  9034. ' $mod.p1(11);',
  9035. ' $mod.p2(1,"a");',
  9036. ' $mod.p2(1,"a");',
  9037. ' $mod.p2(12,"a");',
  9038. ' $mod.p2(13,"b");',
  9039. ' $mod.p3(1.0,false,"abc");'
  9040. ]));
  9041. end;
  9042. procedure TTestModule.TestFunctionInt;
  9043. begin
  9044. StartProgram(false);
  9045. Add('function MyTest(Bar: longint): longint;');
  9046. Add('begin');
  9047. Add(' Result:=2*bar');
  9048. Add('end;');
  9049. Add('begin');
  9050. ConvertProgram;
  9051. CheckSource('TestFunctionInt',
  9052. LinesToStr([ // statements
  9053. 'this.MyTest = function (Bar) {',
  9054. ' var Result = 0;',
  9055. ' Result = 2*Bar;',
  9056. ' return Result;',
  9057. '};'
  9058. ]),
  9059. LinesToStr([ // this.$main
  9060. ''
  9061. ]));
  9062. end;
  9063. procedure TTestModule.TestFunctionString;
  9064. begin
  9065. StartProgram(false);
  9066. Add('function Test(Bar: string): string;');
  9067. Add('begin');
  9068. Add(' Result:=bar+BAR');
  9069. Add('end;');
  9070. Add('begin');
  9071. ConvertProgram;
  9072. CheckSource('TestFunctionString',
  9073. LinesToStr([ // statements
  9074. 'this.Test = function (Bar) {',
  9075. ' var Result = "";',
  9076. ' Result = Bar+Bar;',
  9077. ' return Result;',
  9078. '};'
  9079. ]),
  9080. LinesToStr([ // this.$main
  9081. ''
  9082. ]));
  9083. end;
  9084. procedure TTestModule.TestIfThen;
  9085. begin
  9086. StartProgram(false);
  9087. Add([
  9088. 'var b: boolean;',
  9089. 'begin',
  9090. ' if b then ;',
  9091. ' if b then else ;']);
  9092. ConvertProgram;
  9093. CheckSource('TestIfThen',
  9094. LinesToStr([ // statements
  9095. 'this.b = false;',
  9096. '']),
  9097. LinesToStr([ // this.$main
  9098. 'if ($mod.b) ;',
  9099. 'if ($mod.b) ;',
  9100. '']));
  9101. end;
  9102. procedure TTestModule.TestForLoop;
  9103. begin
  9104. StartProgram(false);
  9105. Add('var');
  9106. Add(' vI, vJ, vN: longint;');
  9107. Add('begin');
  9108. Add(' VJ:=0;');
  9109. Add(' VN:=3;');
  9110. Add(' for VI:=1 to VN do');
  9111. Add(' begin');
  9112. Add(' VJ:=VJ+VI;');
  9113. Add(' end;');
  9114. ConvertProgram;
  9115. CheckSource('TestForLoop',
  9116. LinesToStr([ // statements
  9117. 'this.vI = 0;',
  9118. 'this.vJ = 0;',
  9119. 'this.vN = 0;'
  9120. ]),
  9121. LinesToStr([ // this.$main
  9122. ' $mod.vJ = 0;',
  9123. ' $mod.vN = 3;',
  9124. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  9125. ' $mod.vI = $l;',
  9126. ' $mod.vJ = $mod.vJ + $mod.vI;',
  9127. ' };',
  9128. '']));
  9129. end;
  9130. procedure TTestModule.TestForLoopInsideFunction;
  9131. begin
  9132. StartProgram(false);
  9133. Add('function SumNumbers(Count: longint): longint;');
  9134. Add('var');
  9135. Add(' vI, vJ: longint;');
  9136. Add('begin');
  9137. Add(' vj:=0;');
  9138. Add(' for vi:=1 to count do');
  9139. Add(' begin');
  9140. Add(' vj:=vj+vi;');
  9141. Add(' end;');
  9142. Add('end;');
  9143. Add('begin');
  9144. Add(' sumnumbers(3);');
  9145. ConvertProgram;
  9146. CheckSource('TestForLoopInsideFunction',
  9147. LinesToStr([ // statements
  9148. 'this.SumNumbers = function (Count) {',
  9149. ' var Result = 0;',
  9150. ' var vI = 0;',
  9151. ' var vJ = 0;',
  9152. ' vJ = 0;',
  9153. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  9154. ' vI = $l;',
  9155. ' vJ = vJ + vI;',
  9156. ' };',
  9157. ' return Result;',
  9158. '};'
  9159. ]),
  9160. LinesToStr([ // $mod.$main
  9161. ' $mod.SumNumbers(3);'
  9162. ]));
  9163. end;
  9164. procedure TTestModule.TestForLoop_ReadVarAfter;
  9165. begin
  9166. StartProgram(false);
  9167. Add('var');
  9168. Add(' vI: longint;');
  9169. Add('begin');
  9170. Add(' for vi:=1 to 2 do ;');
  9171. Add(' if vi=3 then ;');
  9172. ConvertProgram;
  9173. CheckSource('TestForLoop',
  9174. LinesToStr([ // statements
  9175. 'this.vI = 0;'
  9176. ]),
  9177. LinesToStr([ // this.$main
  9178. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  9179. ' if ($mod.vI===3) ;'
  9180. ]));
  9181. end;
  9182. procedure TTestModule.TestForLoop_Nested;
  9183. begin
  9184. StartProgram(false);
  9185. Add('function SumNumbers(Count: longint): longint;');
  9186. Add('var');
  9187. Add(' vI, vJ, vK: longint;');
  9188. Add('begin');
  9189. Add(' VK:=0;');
  9190. Add(' for VI:=1 to count do');
  9191. Add(' begin');
  9192. Add(' for vj:=1 to vi do');
  9193. Add(' begin');
  9194. Add(' vk:=VK+VI;');
  9195. Add(' end;');
  9196. Add(' end;');
  9197. Add('end;');
  9198. Add('begin');
  9199. Add(' sumnumbers(3);');
  9200. ConvertProgram;
  9201. CheckSource('TestForLoopInFunction',
  9202. LinesToStr([ // statements
  9203. 'this.SumNumbers = function (Count) {',
  9204. ' var Result = 0;',
  9205. ' var vI = 0;',
  9206. ' var vJ = 0;',
  9207. ' var vK = 0;',
  9208. ' vK = 0;',
  9209. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  9210. ' vI = $l;',
  9211. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  9212. ' vJ = $l1;',
  9213. ' vK = vK + vI;',
  9214. ' };',
  9215. ' };',
  9216. ' return Result;',
  9217. '};'
  9218. ]),
  9219. LinesToStr([ // $mod.$main
  9220. ' $mod.SumNumbers(3);'
  9221. ]));
  9222. end;
  9223. procedure TTestModule.TestRepeatUntil;
  9224. begin
  9225. StartProgram(false);
  9226. Add('var');
  9227. Add(' vI, vJ, vN: longint;');
  9228. Add('begin');
  9229. Add(' vn:=3;');
  9230. Add(' vj:=0;');
  9231. Add(' VI:=0;');
  9232. Add(' repeat');
  9233. Add(' VI:=vi+1;');
  9234. Add(' vj:=VJ+vI;');
  9235. Add(' until vi>=vn');
  9236. ConvertProgram;
  9237. CheckSource('TestRepeatUntil',
  9238. LinesToStr([ // statements
  9239. 'this.vI = 0;',
  9240. 'this.vJ = 0;',
  9241. 'this.vN = 0;'
  9242. ]),
  9243. LinesToStr([ // $mod.$main
  9244. ' $mod.vN = 3;',
  9245. ' $mod.vJ = 0;',
  9246. ' $mod.vI = 0;',
  9247. ' do{',
  9248. ' $mod.vI = $mod.vI + 1;',
  9249. ' $mod.vJ = $mod.vJ + $mod.vI;',
  9250. ' }while(!($mod.vI>=$mod.vN));'
  9251. ]));
  9252. end;
  9253. procedure TTestModule.TestAsmBlock;
  9254. begin
  9255. StartProgram(false);
  9256. Add([
  9257. 'var',
  9258. ' vI: longint;',
  9259. 'begin',
  9260. ' vi:=1;',
  9261. ' asm',
  9262. ' if (vI===1) {',
  9263. ' vI=2;',
  9264. //' console.log(''end;'');', ToDo
  9265. ' }',
  9266. ' if (vI===2){ vI=3; }',
  9267. ' end;',
  9268. ' VI:=4;']);
  9269. ConvertProgram;
  9270. CheckSource('TestAsmBlock',
  9271. LinesToStr([ // statements
  9272. 'this.vI = 0;'
  9273. ]),
  9274. LinesToStr([ // $mod.$main
  9275. '$mod.vI = 1;',
  9276. 'if (vI===1) {',
  9277. ' vI=2;',
  9278. '}',
  9279. 'if (vI===2){ vI=3; }',
  9280. ';',
  9281. '$mod.vI = 4;'
  9282. ]));
  9283. end;
  9284. procedure TTestModule.TestAsmPas_Impl;
  9285. begin
  9286. StartUnit(false);
  9287. Add('interface');
  9288. Add('const cIntf: longint = 1;');
  9289. Add('var vIntf: longint;');
  9290. Add('implementation');
  9291. Add('const cImpl: longint = 2;');
  9292. Add('var vImpl: longint;');
  9293. Add('procedure DoIt;');
  9294. Add('const cLoc: longint = 3;');
  9295. Add('var vLoc: longint;');
  9296. Add('begin;');
  9297. Add(' asm');
  9298. //Add(' pas(vIntf)=pas(cIntf);');
  9299. //Add(' pas(vImpl)=pas(cImpl);');
  9300. //Add(' pas(vLoc)=pas(cLoc);');
  9301. Add(' end;');
  9302. Add('end;');
  9303. ConvertUnit;
  9304. CheckSource('TestAsmPas_Impl',
  9305. LinesToStr([
  9306. 'var $impl = $mod.$impl;',
  9307. 'this.cIntf = 1;',
  9308. 'this.vIntf = 0;',
  9309. '']),
  9310. '', // this.$init
  9311. LinesToStr([ // implementation
  9312. '$impl.cImpl = 2;',
  9313. '$impl.vImpl = 0;',
  9314. 'var cLoc = 3;',
  9315. '$impl.DoIt = function () {',
  9316. ' var vLoc = 0;',
  9317. '};',
  9318. '']) );
  9319. end;
  9320. procedure TTestModule.TestTryFinally;
  9321. begin
  9322. StartProgram(false);
  9323. Add('var i: longint;');
  9324. Add('begin');
  9325. Add(' try');
  9326. Add(' i:=0; i:=2 div i;');
  9327. Add(' finally');
  9328. Add(' i:=3');
  9329. Add(' end;');
  9330. ConvertProgram;
  9331. CheckSource('TestTryFinally',
  9332. LinesToStr([ // statements
  9333. 'this.i = 0;'
  9334. ]),
  9335. LinesToStr([ // $mod.$main
  9336. 'try {',
  9337. ' $mod.i = 0;',
  9338. ' $mod.i = rtl.trunc(2 / $mod.i);',
  9339. '} finally {',
  9340. ' $mod.i = 3;',
  9341. '};'
  9342. ]));
  9343. end;
  9344. procedure TTestModule.TestTryExcept;
  9345. begin
  9346. StartProgram(false);
  9347. Add([
  9348. 'type',
  9349. ' TObject = class end;',
  9350. ' Exception = class Msg: string; end;',
  9351. ' EInvalidCast = class(Exception) end;',
  9352. 'var vI: longint;',
  9353. 'begin',
  9354. ' try',
  9355. ' vi:=1;',
  9356. ' except',
  9357. ' vi:=2',
  9358. ' end;',
  9359. ' try',
  9360. ' vi:=3;',
  9361. ' except',
  9362. ' raise;',
  9363. ' end;',
  9364. ' try',
  9365. ' VI:=4;',
  9366. ' except',
  9367. ' on einvalidcast do',
  9368. ' raise;',
  9369. ' on E: exception do',
  9370. ' if e.msg='''' then',
  9371. ' raise e;',
  9372. ' else',
  9373. ' vi:=5',
  9374. ' end;',
  9375. ' try',
  9376. ' VI:=6;',
  9377. ' except',
  9378. ' on einvalidcast do ;',
  9379. ' end;',
  9380. '']);
  9381. ConvertProgram;
  9382. CheckSource('TestTryExcept',
  9383. LinesToStr([ // statements
  9384. 'rtl.createClass(this, "TObject", null, function () {',
  9385. ' this.$init = function () {',
  9386. ' };',
  9387. ' this.$final = function () {',
  9388. ' };',
  9389. '});',
  9390. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  9391. ' this.$init = function () {',
  9392. ' $mod.TObject.$init.call(this);',
  9393. ' this.Msg = "";',
  9394. ' };',
  9395. '});',
  9396. 'rtl.createClass(this, "EInvalidCast", this.Exception, function () {',
  9397. '});',
  9398. 'this.vI = 0;'
  9399. ]),
  9400. LinesToStr([ // $mod.$main
  9401. 'try {',
  9402. ' $mod.vI = 1;',
  9403. '} catch ($e) {',
  9404. ' $mod.vI = 2;',
  9405. '};',
  9406. 'try {',
  9407. ' $mod.vI = 3;',
  9408. '} catch ($e) {',
  9409. ' throw $e;',
  9410. '};',
  9411. 'try {',
  9412. ' $mod.vI = 4;',
  9413. '} catch ($e) {',
  9414. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  9415. ' throw $e',
  9416. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  9417. ' var E = $e;',
  9418. ' if (E.Msg === "") throw E;',
  9419. ' } else {',
  9420. ' $mod.vI = 5;',
  9421. ' }',
  9422. '};',
  9423. 'try {',
  9424. ' $mod.vI = 6;',
  9425. '} catch ($e) {',
  9426. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  9427. ' } else throw $e',
  9428. '};',
  9429. '']));
  9430. end;
  9431. procedure TTestModule.TestTryExcept_ReservedWords;
  9432. begin
  9433. StartProgram(false);
  9434. Add([
  9435. 'type',
  9436. ' TObject = class end;',
  9437. ' Exception = class',
  9438. ' Symbol: string;',
  9439. ' end;',
  9440. 'var &try: longint;',
  9441. 'begin',
  9442. ' try',
  9443. ' &try:=4;',
  9444. ' except',
  9445. ' on Error: exception do',
  9446. ' if errOR.symBol='''' then',
  9447. ' raise ERRor;',
  9448. ' end;',
  9449. '']);
  9450. ConvertProgram;
  9451. CheckSource('TestTryExcept_ReservedWords',
  9452. LinesToStr([ // statements
  9453. 'rtl.createClass(this, "TObject", null, function () {',
  9454. ' this.$init = function () {',
  9455. ' };',
  9456. ' this.$final = function () {',
  9457. ' };',
  9458. '});',
  9459. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  9460. ' this.$init = function () {',
  9461. ' $mod.TObject.$init.call(this);',
  9462. ' this.Symbol = "";',
  9463. ' };',
  9464. '});',
  9465. 'this.Try = 0;',
  9466. '']),
  9467. LinesToStr([ // $mod.$main
  9468. 'try {',
  9469. ' $mod.Try = 4;',
  9470. '} catch ($e) {',
  9471. ' if ($mod.Exception.isPrototypeOf($e)) {',
  9472. ' var error = $e;',
  9473. ' if (error.Symbol === "") throw error;',
  9474. ' } else throw $e',
  9475. '};',
  9476. '']));
  9477. end;
  9478. procedure TTestModule.TestIfThenRaiseElse;
  9479. begin
  9480. StartProgram(false);
  9481. Add([
  9482. 'type',
  9483. ' TObject = class',
  9484. ' constructor Create;',
  9485. ' end;',
  9486. 'constructor TObject.Create;',
  9487. 'begin',
  9488. 'end;',
  9489. 'var b: boolean;',
  9490. 'begin',
  9491. ' if b then',
  9492. ' raise TObject.Create',
  9493. ' else',
  9494. ' b:=false;',
  9495. '']);
  9496. ConvertProgram;
  9497. CheckSource('TestIfThenRaiseElse',
  9498. LinesToStr([ // statements
  9499. 'rtl.createClass(this, "TObject", null, function () {',
  9500. ' this.$init = function () {',
  9501. ' };',
  9502. ' this.$final = function () {',
  9503. ' };',
  9504. ' this.Create = function () {',
  9505. ' return this;',
  9506. ' };',
  9507. '});',
  9508. 'this.b = false;',
  9509. '']),
  9510. LinesToStr([ // $mod.$main
  9511. 'if ($mod.b) {',
  9512. ' throw $mod.TObject.$create("Create")}',
  9513. ' else $mod.b = false;',
  9514. '']));
  9515. end;
  9516. procedure TTestModule.TestCaseOf;
  9517. begin
  9518. StartProgram(false);
  9519. Add([
  9520. 'const e: longint; external name ''$e'';',
  9521. 'var vI: longint;',
  9522. 'begin',
  9523. ' case vi of',
  9524. ' 1: ;',
  9525. ' 2: vi:=3;',
  9526. ' e: ;',
  9527. ' else',
  9528. ' VI:=4',
  9529. ' end;']);
  9530. ConvertProgram;
  9531. CheckSource('TestCaseOf',
  9532. LinesToStr([ // statements
  9533. 'this.vI = 0;'
  9534. ]),
  9535. LinesToStr([ // $mod.$main
  9536. 'var $tmp = $mod.vI;',
  9537. 'if ($tmp === 1) {}',
  9538. 'else if ($tmp === 2) {',
  9539. ' $mod.vI = 3}',
  9540. ' else if ($tmp === $e) {}',
  9541. 'else {',
  9542. ' $mod.vI = 4;',
  9543. '};'
  9544. ]));
  9545. end;
  9546. procedure TTestModule.TestCaseOf_UseSwitch;
  9547. begin
  9548. StartProgram(false);
  9549. Converter.UseSwitchStatement:=true;
  9550. Add('var Vi: longint;');
  9551. Add('begin');
  9552. Add(' case vi of');
  9553. Add(' 1: ;');
  9554. Add(' 2: VI:=3;');
  9555. Add(' else');
  9556. Add(' vi:=4');
  9557. Add(' end;');
  9558. ConvertProgram;
  9559. CheckSource('TestCaseOf_UseSwitch',
  9560. LinesToStr([ // statements
  9561. 'this.Vi = 0;'
  9562. ]),
  9563. LinesToStr([ // $mod.$main
  9564. 'switch ($mod.Vi) {',
  9565. 'case 1:',
  9566. ' break;',
  9567. 'case 2:',
  9568. ' $mod.Vi = 3;',
  9569. ' break;',
  9570. 'default:',
  9571. ' $mod.Vi = 4;',
  9572. '};'
  9573. ]));
  9574. end;
  9575. procedure TTestModule.TestCaseOfNoElse;
  9576. begin
  9577. StartProgram(false);
  9578. Add('var Vi: longint;');
  9579. Add('begin');
  9580. Add(' case vi of');
  9581. Add(' 1: begin vi:=2; VI:=3; end;');
  9582. Add(' end;');
  9583. ConvertProgram;
  9584. CheckSource('TestCaseOfNoElse',
  9585. LinesToStr([ // statements
  9586. 'this.Vi = 0;'
  9587. ]),
  9588. LinesToStr([ // $mod.$main
  9589. 'var $tmp = $mod.Vi;',
  9590. 'if ($tmp === 1) {',
  9591. ' $mod.Vi = 2;',
  9592. ' $mod.Vi = 3;',
  9593. '};'
  9594. ]));
  9595. end;
  9596. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  9597. begin
  9598. StartProgram(false);
  9599. Converter.UseSwitchStatement:=true;
  9600. Add('var vI: longint;');
  9601. Add('begin');
  9602. Add(' case vi of');
  9603. Add(' 1: begin VI:=2; vi:=3; end;');
  9604. Add(' end;');
  9605. ConvertProgram;
  9606. CheckSource('TestCaseOfNoElse_UseSwitch',
  9607. LinesToStr([ // statements
  9608. 'this.vI = 0;'
  9609. ]),
  9610. LinesToStr([ // $mod.$main
  9611. 'switch ($mod.vI) {',
  9612. 'case 1:',
  9613. ' $mod.vI = 2;',
  9614. ' $mod.vI = 3;',
  9615. ' break;',
  9616. '};'
  9617. ]));
  9618. end;
  9619. procedure TTestModule.TestCaseOfRange;
  9620. begin
  9621. StartProgram(false);
  9622. Add('var vI: longint;');
  9623. Add('begin');
  9624. Add(' case vi of');
  9625. Add(' 1..3: vi:=14;');
  9626. Add(' 4,5: vi:=16;');
  9627. Add(' 6..7,9..10: ;');
  9628. Add(' else ;');
  9629. Add(' end;');
  9630. ConvertProgram;
  9631. CheckSource('TestCaseOfRange',
  9632. LinesToStr([ // statements
  9633. 'this.vI = 0;'
  9634. ]),
  9635. LinesToStr([ // $mod.$main
  9636. 'var $tmp = $mod.vI;',
  9637. 'if (($tmp >= 1) && ($tmp <= 3)){',
  9638. ' $mod.vI = 14',
  9639. '} else if (($tmp === 4) || ($tmp === 5)){',
  9640. ' $mod.vI = 16',
  9641. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  9642. ]));
  9643. end;
  9644. procedure TTestModule.TestCaseOfString;
  9645. begin
  9646. StartProgram(false);
  9647. Add([
  9648. 'var s,h: string;',
  9649. 'begin',
  9650. ' case s of',
  9651. ' ''foo'': s:=h;',
  9652. ' ''a''..''z'': h:=s;',
  9653. ' ''ў'', ''ё'': ;',
  9654. ' ''Б''..''Я'': ;',
  9655. ' end;',
  9656. '']);
  9657. ConvertProgram;
  9658. CheckSource('TestCaseOfString',
  9659. LinesToStr([ // statements
  9660. 'this.s = "";',
  9661. 'this.h = "";',
  9662. '']),
  9663. LinesToStr([ // $mod.$main
  9664. 'var $tmp = $mod.s;',
  9665. 'if ($tmp === "foo") {',
  9666. ' $mod.s = $mod.h}',
  9667. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  9668. ' $mod.h = $mod.s}',
  9669. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  9670. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  9671. '']));
  9672. end;
  9673. procedure TTestModule.TestCaseOfChar;
  9674. begin
  9675. StartProgram(false);
  9676. Add([
  9677. 'var s,h: char;',
  9678. 'begin',
  9679. ' case s of',
  9680. ' ''a''..''z'': h:=s;',
  9681. ' ''ä'': ;',
  9682. ' ''ў'', ''ё'': ;',
  9683. ' ''Б''..''Я'': ;',
  9684. ' end;',
  9685. '']);
  9686. ConvertProgram;
  9687. CheckSource('TestCaseOfString',
  9688. LinesToStr([ // statements
  9689. 'this.s = "\x00";',
  9690. 'this.h = "\x00";',
  9691. '']),
  9692. LinesToStr([ // $mod.$main
  9693. 'var $tmp = $mod.s;',
  9694. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  9695. ' $mod.h = $mod.s}',
  9696. ' else if ($tmp === "ä") {}',
  9697. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  9698. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  9699. '']));
  9700. end;
  9701. procedure TTestModule.TestCaseOfExternalClassConst;
  9702. begin
  9703. StartProgram(false);
  9704. Add([
  9705. '{$modeswitch externalclass}',
  9706. 'type',
  9707. ' TBird = class external name ''Bird''',
  9708. ' const e: longint;',
  9709. ' end;',
  9710. 'var vI: longint;',
  9711. 'begin',
  9712. ' case vi of',
  9713. ' 1: vi:=3;',
  9714. ' TBird.e: ;',
  9715. ' end;']);
  9716. ConvertProgram;
  9717. CheckSource('TestCaseOfExternalClassConst',
  9718. LinesToStr([ // statements
  9719. 'this.vI = 0;'
  9720. ]),
  9721. LinesToStr([ // $mod.$main
  9722. 'var $tmp = $mod.vI;',
  9723. 'if ($tmp === 1) {',
  9724. ' $mod.vI = 3}',
  9725. ' else if ($tmp === Bird.e) ;'
  9726. ]));
  9727. end;
  9728. procedure TTestModule.TestDebugger;
  9729. begin
  9730. StartProgram(false);
  9731. Add([
  9732. 'procedure DoIt;',
  9733. 'begin',
  9734. ' deBugger;',
  9735. ' DeBugger();',
  9736. 'end;',
  9737. 'begin',
  9738. ' Debugger;']);
  9739. ConvertProgram;
  9740. CheckSource('TestDebugger',
  9741. LinesToStr([ // statements
  9742. 'this.DoIt = function () {',
  9743. ' debugger;',
  9744. ' debugger;',
  9745. '};',
  9746. '']),
  9747. LinesToStr([ // $mod.$main
  9748. 'debugger;',
  9749. '']));
  9750. end;
  9751. procedure TTestModule.TestArray_Dynamic;
  9752. begin
  9753. StartProgram(false);
  9754. Add([
  9755. 'type',
  9756. ' TArrayInt = array of longint;',
  9757. 'var',
  9758. ' Arr: TArrayInt;',
  9759. ' i: longint;',
  9760. ' b: boolean;',
  9761. 'begin',
  9762. ' SetLength(arr,3);',
  9763. ' arr[0]:=4;',
  9764. ' arr[1]:=length(arr)+arr[0];',
  9765. ' arr[i]:=5;',
  9766. ' arr[arr[i]]:=arr[6];',
  9767. ' i:=low(arr);',
  9768. ' i:=high(arr);',
  9769. ' b:=Assigned(arr);',
  9770. ' Arr:=default(TArrayInt);']);
  9771. ConvertProgram;
  9772. CheckSource('TestArray_Dynamic',
  9773. LinesToStr([ // statements
  9774. 'this.Arr = [];',
  9775. 'this.i = 0;',
  9776. 'this.b = false;'
  9777. ]),
  9778. LinesToStr([ // $mod.$main
  9779. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  9780. '$mod.Arr[0] = 4;',
  9781. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  9782. '$mod.Arr[$mod.i] = 5;',
  9783. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  9784. '$mod.i = 0;',
  9785. '$mod.i = rtl.length($mod.Arr) - 1;',
  9786. '$mod.b = rtl.length($mod.Arr) > 0;',
  9787. '$mod.Arr = [];',
  9788. '']));
  9789. end;
  9790. procedure TTestModule.TestArray_Dynamic_Nil;
  9791. begin
  9792. StartProgram(false);
  9793. Add('type');
  9794. Add(' TArrayInt = array of longint;');
  9795. Add('var');
  9796. Add(' Arr: TArrayInt;');
  9797. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  9798. Add('begin');
  9799. Add(' arr:=nil;');
  9800. Add(' if arr=nil then;');
  9801. Add(' if nil=arr then;');
  9802. Add(' if arr<>nil then;');
  9803. Add(' if nil<>arr then;');
  9804. Add(' DoIt(nil,nil);');
  9805. ConvertProgram;
  9806. CheckSource('TestArray_Dynamic',
  9807. LinesToStr([ // statements
  9808. 'this.Arr = [];',
  9809. 'this.DoIt = function(i,j){',
  9810. '};'
  9811. ]),
  9812. LinesToStr([ // $mod.$main
  9813. '$mod.Arr = [];',
  9814. 'if (rtl.length($mod.Arr) === 0) ;',
  9815. 'if (rtl.length($mod.Arr) === 0) ;',
  9816. 'if (rtl.length($mod.Arr) > 0) ;',
  9817. 'if (rtl.length($mod.Arr) > 0) ;',
  9818. '$mod.DoIt([],[]);',
  9819. '']));
  9820. end;
  9821. procedure TTestModule.TestArray_DynMultiDimensional;
  9822. begin
  9823. StartProgram(false);
  9824. Add([
  9825. 'type',
  9826. ' TArrayInt = array of longint;',
  9827. ' TArrayArrayInt = array of TArrayInt;',
  9828. 'var',
  9829. ' Arr: TArrayInt;',
  9830. ' Arr2: TArrayArrayInt;',
  9831. ' i: longint;',
  9832. 'begin',
  9833. ' arr2:=nil;',
  9834. ' if arr2=nil then;',
  9835. ' if nil=arr2 then;',
  9836. ' i:=low(arr2);',
  9837. ' i:=low(arr2[1]);',
  9838. ' i:=high(arr2);',
  9839. ' i:=high(arr2[2]);',
  9840. ' arr2[3]:=arr;',
  9841. ' arr2[4][5]:=i;',
  9842. ' i:=arr2[6][7];',
  9843. ' arr2[8,9]:=i;',
  9844. ' i:=arr2[10,11];',
  9845. ' SetLength(arr2,14);',
  9846. ' SetLength(arr2[15],16);']);
  9847. ConvertProgram;
  9848. CheckSource('TestArray_Dynamic',
  9849. LinesToStr([ // statements
  9850. 'this.Arr = [];',
  9851. 'this.Arr2 = [];',
  9852. 'this.i = 0;'
  9853. ]),
  9854. LinesToStr([ // $mod.$main
  9855. '$mod.Arr2 = [];',
  9856. 'if (rtl.length($mod.Arr2) === 0) ;',
  9857. 'if (rtl.length($mod.Arr2) === 0) ;',
  9858. '$mod.i = 0;',
  9859. '$mod.i = 0;',
  9860. '$mod.i = rtl.length($mod.Arr2) - 1;',
  9861. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  9862. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  9863. '$mod.Arr2[4][5] = $mod.i;',
  9864. '$mod.i = $mod.Arr2[6][7];',
  9865. '$mod.Arr2[8][9] = $mod.i;',
  9866. '$mod.i = $mod.Arr2[10][11];',
  9867. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  9868. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  9869. '']));
  9870. end;
  9871. procedure TTestModule.TestArray_DynamicAssign;
  9872. begin
  9873. StartProgram(false);
  9874. Add([
  9875. 'type',
  9876. ' TArrayInt = array of longint;',
  9877. ' TArrayArrayInt = array of TArrayInt;',
  9878. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  9879. 'begin',
  9880. 'end;',
  9881. 'procedure Fly(var a: TArrayInt);',
  9882. 'begin',
  9883. 'end;',
  9884. 'var',
  9885. ' Arr: TArrayInt;',
  9886. ' Arr2: TArrayArrayInt;',
  9887. 'begin',
  9888. ' arr:=nil;',
  9889. ' arr2:=nil;',
  9890. ' arr2[1]:=nil;',
  9891. ' arr2[2]:=arr;',
  9892. ' Run(arr,arr,arr);',
  9893. ' Fly(arr);',
  9894. ' Run(arr2[4],arr2[5],arr2[6]);',
  9895. ' Fly(arr2[7]);',
  9896. '']);
  9897. ConvertProgram;
  9898. CheckSource('TestArray_DynamicAssign',
  9899. LinesToStr([ // statements
  9900. 'this.Run = function (a, b, c) {',
  9901. '};',
  9902. 'this.Fly = function (a) {',
  9903. '};',
  9904. 'this.Arr = [];',
  9905. 'this.Arr2 = [];',
  9906. '']),
  9907. LinesToStr([ // $mod.$main
  9908. '$mod.Arr = [];',
  9909. '$mod.Arr2 = [];',
  9910. '$mod.Arr2[1] = [];',
  9911. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  9912. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  9913. '$mod.Fly({',
  9914. ' p: $mod,',
  9915. ' get: function () {',
  9916. ' return this.p.Arr;',
  9917. ' },',
  9918. ' set: function (v) {',
  9919. ' this.p.Arr = v;',
  9920. ' }',
  9921. '});',
  9922. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  9923. '$mod.Fly({',
  9924. ' a: 7,',
  9925. ' p: $mod.Arr2,',
  9926. ' get: function () {',
  9927. ' return this.p[this.a];',
  9928. ' },',
  9929. ' set: function (v) {',
  9930. ' this.p[this.a] = v;',
  9931. ' }',
  9932. '});',
  9933. '']));
  9934. end;
  9935. procedure TTestModule.TestArray_StaticInt;
  9936. begin
  9937. StartProgram(false);
  9938. Add('type');
  9939. Add(' TArrayInt = array[2..4] of longint;');
  9940. Add('var');
  9941. Add(' Arr: TArrayInt;');
  9942. Add(' Arr2: TArrayInt = (5,6,7);');
  9943. Add(' i: longint;');
  9944. Add(' b: boolean;');
  9945. Add('begin');
  9946. Add(' arr[2]:=4;');
  9947. Add(' arr[3]:=arr[2]+arr[3];');
  9948. Add(' arr[i]:=5;');
  9949. Add(' arr[arr[i]]:=arr[high(arr)];');
  9950. Add(' i:=low(arr);');
  9951. Add(' i:=high(arr);');
  9952. Add(' b:=arr[2]=arr[3];');
  9953. Add(' arr:=default(TArrayInt);');
  9954. ConvertProgram;
  9955. CheckSource('TestArray_StaticInt',
  9956. LinesToStr([ // statements
  9957. 'this.Arr = rtl.arraySetLength(null,0,3);',
  9958. 'this.Arr2 = [5, 6, 7];',
  9959. 'this.i = 0;',
  9960. 'this.b = false;'
  9961. ]),
  9962. LinesToStr([ // $mod.$main
  9963. '$mod.Arr[0] = 4;',
  9964. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  9965. '$mod.Arr[$mod.i-2] = 5;',
  9966. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  9967. '$mod.i = 2;',
  9968. '$mod.i = 4;',
  9969. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9970. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  9971. '']));
  9972. end;
  9973. procedure TTestModule.TestArray_StaticBool;
  9974. begin
  9975. StartProgram(false);
  9976. Add('type');
  9977. Add(' TBools = array[boolean] of boolean;');
  9978. Add(' TBool2 = array[true..true] of boolean;');
  9979. Add('var');
  9980. Add(' Arr: TBools;');
  9981. Add(' Arr2: TBool2;');
  9982. Add(' Arr3: TBools = (true,false);');
  9983. Add(' b: boolean;');
  9984. Add('begin');
  9985. Add(' b:=low(arr);');
  9986. Add(' b:=high(arr);');
  9987. Add(' arr[true]:=false;');
  9988. Add(' arr[false]:=arr[b] or arr[true];');
  9989. Add(' arr[b]:=true;');
  9990. Add(' arr[arr[b]]:=arr[high(arr)];');
  9991. Add(' b:=arr[false]=arr[true];');
  9992. Add(' b:=low(arr2);');
  9993. Add(' b:=high(arr2);');
  9994. Add(' arr2[true]:=true;');
  9995. Add(' arr2[true]:=arr2[true] and arr2[b];');
  9996. Add(' arr2[b]:=false;');
  9997. ConvertProgram;
  9998. CheckSource('TestArray_StaticBool',
  9999. LinesToStr([ // statements
  10000. 'this.Arr = rtl.arraySetLength(null,false,2);',
  10001. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  10002. 'this.Arr3 = [true, false];',
  10003. 'this.b = false;'
  10004. ]),
  10005. LinesToStr([ // $mod.$main
  10006. '$mod.b = false;',
  10007. '$mod.b = true;',
  10008. '$mod.Arr[1] = false;',
  10009. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  10010. '$mod.Arr[+$mod.b] = true;',
  10011. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  10012. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  10013. '$mod.b = true;',
  10014. '$mod.b = true;',
  10015. '$mod.Arr2[0] = true;',
  10016. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  10017. '$mod.Arr2[1-$mod.b] = false;',
  10018. '']));
  10019. end;
  10020. procedure TTestModule.TestArray_StaticChar;
  10021. begin
  10022. StartProgram(false);
  10023. Add([
  10024. 'type',
  10025. ' TChars = array[char] of char;',
  10026. ' TChars2 = array[''a''..''z''] of char;',
  10027. 'var',
  10028. ' Arr: TChars;',
  10029. ' Arr2: TChars2;',
  10030. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  10031. ' Arr4: array[11..13] of char = ''pas'';',
  10032. ' Arr5: array[21..22] of char = ''äö'';',
  10033. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  10034. ' c: char;',
  10035. ' b: boolean;',
  10036. 'begin',
  10037. ' c:=low(arr);',
  10038. ' c:=high(arr);',
  10039. ' arr[''B'']:=''a'';',
  10040. ' arr[''D'']:=arr[c];',
  10041. ' arr[c]:=arr[''d''];',
  10042. ' arr[arr[c]]:=arr[high(arr)];',
  10043. ' b:=arr[low(arr)]=arr[''e''];',
  10044. ' c:=low(arr2);',
  10045. ' c:=high(arr2);',
  10046. ' arr2[''b'']:=''f'';',
  10047. ' arr2[''a'']:=arr2[c];',
  10048. ' arr2[c]:=arr2[''g''];']);
  10049. ConvertProgram;
  10050. CheckSource('TestArray_StaticChar',
  10051. LinesToStr([ // statements
  10052. 'this.Arr = rtl.arraySetLength(null, "\x00", 65536);',
  10053. 'this.Arr2 = rtl.arraySetLength(null, "\x00", 26);',
  10054. 'this.Arr3 = ["p", "a", "s"];',
  10055. 'this.Arr4 = ["p", "a", "s"];',
  10056. 'this.Arr5 = ["ä", "ö"];',
  10057. 'this.Arr6 = ["ä", "ö"];',
  10058. 'this.c = "\x00";',
  10059. 'this.b = false;',
  10060. '']),
  10061. LinesToStr([ // $mod.$main
  10062. '$mod.c = "\x00";',
  10063. '$mod.c = "\uFFFF";',
  10064. '$mod.Arr[66] = "a";',
  10065. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  10066. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  10067. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  10068. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  10069. '$mod.c = "a";',
  10070. '$mod.c = "z";',
  10071. '$mod.Arr2[1] = "f";',
  10072. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  10073. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  10074. '']));
  10075. end;
  10076. procedure TTestModule.TestArray_StaticMultiDim;
  10077. begin
  10078. StartProgram(false);
  10079. Add([
  10080. 'type',
  10081. ' TArrayInt = array[1..3] of longint;',
  10082. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  10083. ' TArrayArrayArrayInt = array[7..8] of TArrayArrayInt;',
  10084. ' TArrayDim2Int = array[1..2,1..3] of longint;',
  10085. ' TArrayDim3Int = array[1..2,1..3,1..4] of longint;',
  10086. ' TArrayDim4Int = array[1..2,1..3,1..4,1..5] of longint;',
  10087. 'var',
  10088. ' Arr: TArrayInt;',
  10089. ' Arr2: TArrayArrayInt;',
  10090. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  10091. ' Arr4: TArrayArrayInt;',
  10092. ' ArrDim2: TArrayDim2Int;',
  10093. ' ArrDim3: TArrayDim3Int;',
  10094. ' ArrDim4: TArrayDim4Int;',
  10095. ' i: longint;',
  10096. 'begin',
  10097. ' i:=low(arr);',
  10098. ' i:=low(arr2);',
  10099. ' i:=low(arr2[5]);',
  10100. ' i:=high(arr);',
  10101. ' i:=high(arr2);',
  10102. ' i:=high(arr2[6]);',
  10103. ' arr2[5]:=arr;',
  10104. ' arr2[6][2]:=i;',
  10105. ' i:=arr2[6][3];',
  10106. ' arr2[6,3]:=i;',
  10107. ' i:=arr2[5,2];',
  10108. ' arr2:=arr2;',// clone multi dim static array
  10109. ' arr3:=arr3;',// clone anonymous multi dim static array
  10110. ' arr4:=arr4;',
  10111. ' Arr:=Arr;',
  10112. ' ArrDim2:=ArrDim2;',
  10113. ' ArrDim3:=ArrDim3;',
  10114. ' ArrDim4:=ArrDim4;',
  10115. '']);
  10116. ConvertProgram;
  10117. CheckSource('TestArray_StaticMultiDim',
  10118. LinesToStr([ // statements
  10119. 'this.TArrayArrayInt$clone = function (a) {',
  10120. ' var b = [];',
  10121. ' b.length = 2;',
  10122. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10123. ' return b;',
  10124. '};',
  10125. 'this.TArrayArrayArrayInt$clone = function (a) {',
  10126. ' var b = [];',
  10127. ' b.length = 2;',
  10128. ' for (var c = 0; c < 2; c++) b[c] = $mod.TArrayArrayInt$clone(a[c]);',
  10129. ' return b;',
  10130. '};',
  10131. 'this.TArrayDim2Int$clone = function (a) {',
  10132. ' var b = [];',
  10133. ' b.length = 2;',
  10134. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10135. ' return b;',
  10136. '};',
  10137. 'this.TArrayDim3Int$clone = function (a) {',
  10138. ' var b = [];',
  10139. ' b.length = 2;',
  10140. ' for (var c = 0; c < 2; c++) {',
  10141. ' var d = b[c] = [];',
  10142. ' d.length = 3;',
  10143. ' var e = a[c];',
  10144. ' for (var f = 0; f < 3; f++) d[f] = e[f].slice(0);',
  10145. ' };',
  10146. ' return b;',
  10147. '};',
  10148. 'this.TArrayDim4Int$clone = function (a) {',
  10149. ' var b = [];',
  10150. ' b.length = 2;',
  10151. ' for (var c = 0; c < 2; c++) {',
  10152. ' var d = b[c] = [];',
  10153. ' d.length = 3;',
  10154. ' var e = a[c];',
  10155. ' for (var f = 0; f < 3; f++) {',
  10156. ' var g = d[f] = [];',
  10157. ' g.length = 4;',
  10158. ' var h = e[f];',
  10159. ' for (var i = 0; i < 4; i++) g[i] = h[i].slice(0);',
  10160. ' };',
  10161. ' };',
  10162. ' return b;',
  10163. '};',
  10164. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  10165. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  10166. 'this.Arr3$a$clone = function (a) {',
  10167. ' var b = [];',
  10168. ' b.length = 2;',
  10169. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10170. ' return b;',
  10171. '};',
  10172. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  10173. 'this.Arr4 = rtl.arraySetLength(null, 0, 2, 3);',
  10174. 'this.ArrDim2 = rtl.arraySetLength(null, 0, 2, 3);',
  10175. 'this.ArrDim3 = rtl.arraySetLength(null, 0, 2, 3, 4);',
  10176. 'this.ArrDim4 = rtl.arraySetLength(',
  10177. ' null,',
  10178. ' 0,',
  10179. ' 2,',
  10180. ' 3,',
  10181. ' 4,',
  10182. ' 5',
  10183. ');',
  10184. 'this.i = 0;'
  10185. ]),
  10186. LinesToStr([ // $mod.$main
  10187. '$mod.i = 1;',
  10188. '$mod.i = 5;',
  10189. '$mod.i = 1;',
  10190. '$mod.i = 3;',
  10191. '$mod.i = 6;',
  10192. '$mod.i = 3;',
  10193. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  10194. '$mod.Arr2[1][1] = $mod.i;',
  10195. '$mod.i = $mod.Arr2[1][2];',
  10196. '$mod.Arr2[1][2] = $mod.i;',
  10197. '$mod.i = $mod.Arr2[0][1];',
  10198. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  10199. '$mod.Arr3 = $mod.Arr3$a$clone($mod.Arr3);',
  10200. '$mod.Arr4 = $mod.TArrayArrayInt$clone($mod.Arr4);',
  10201. '$mod.Arr = $mod.Arr.slice(0);',
  10202. '$mod.ArrDim2 = $mod.TArrayDim2Int$clone($mod.ArrDim2);',
  10203. '$mod.ArrDim3 = $mod.TArrayDim3Int$clone($mod.ArrDim3);',
  10204. '$mod.ArrDim4 = $mod.TArrayDim4Int$clone($mod.ArrDim4);',
  10205. '']));
  10206. end;
  10207. procedure TTestModule.TestArray_StaticInFunction;
  10208. begin
  10209. StartProgram(false);
  10210. Add([
  10211. 'const TArrayInt = 3;',
  10212. 'const TArrayArrayInt = 4;',
  10213. 'procedure DoIt;',
  10214. 'type',
  10215. ' TArrayInt = array[1..3] of longint;',
  10216. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  10217. 'var',
  10218. ' Arr: TArrayInt;',
  10219. ' Arr2: TArrayArrayInt;',
  10220. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  10221. ' i: longint;',
  10222. 'begin',
  10223. ' arr2[5]:=arr;',
  10224. ' arr2:=arr2;',// clone multi dim static array
  10225. ' arr3:=arr3;',// clone multi dim anonymous static array
  10226. 'end;',
  10227. 'begin',
  10228. '']);
  10229. ConvertProgram;
  10230. CheckSource('TestArray_StaticInFunction',
  10231. LinesToStr([ // statements
  10232. 'this.TArrayInt = 3;',
  10233. 'this.TArrayArrayInt = 4;',
  10234. 'var TArrayArrayInt$1$clone = function (a) {',
  10235. ' var b = [];',
  10236. ' b.length = 2;',
  10237. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10238. ' return b;',
  10239. '};',
  10240. 'var Arr3$a$clone = function (a) {',
  10241. ' var b = [];',
  10242. ' b.length = 2;',
  10243. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10244. ' return b;',
  10245. '};',
  10246. 'this.DoIt = function () {',
  10247. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  10248. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  10249. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  10250. ' var i = 0;',
  10251. ' Arr2[0] = Arr.slice(0);',
  10252. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  10253. ' Arr3 = Arr3$a$clone(Arr3);',
  10254. '};',
  10255. '']),
  10256. LinesToStr([ // $mod.$main
  10257. '']));
  10258. end;
  10259. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  10260. begin
  10261. StartProgram(false);
  10262. Add([
  10263. 'type',
  10264. ' TArrayInt = array[1..3,1..2] of longint;',
  10265. 'var',
  10266. ' a,b: TArrayInt;',
  10267. 'begin',
  10268. ' if a=b then ;',
  10269. '']);
  10270. SetExpectedPasResolverError('compare static array is not supported',
  10271. nXIsNotSupported);
  10272. ConvertProgram;
  10273. end;
  10274. procedure TTestModule.TestArrayOfRecord;
  10275. begin
  10276. StartProgram(false);
  10277. Add([
  10278. 'type',
  10279. ' TRec = record',
  10280. ' Int: longint;',
  10281. ' end;',
  10282. ' TArrayRec = array of TRec;',
  10283. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  10284. 'begin',
  10285. 'end;',
  10286. 'var',
  10287. ' Arr: TArrayRec;',
  10288. ' r: TRec;',
  10289. ' i: longint;',
  10290. 'begin',
  10291. ' SetLength(arr,3);',
  10292. ' arr[0].int:=4;',
  10293. ' arr[1].int:=length(arr)+arr[2].int;',
  10294. ' arr[arr[i].int].int:=arr[5].int;',
  10295. ' arr[7]:=r;',
  10296. ' r:=arr[8];',
  10297. ' i:=low(arr);',
  10298. ' i:=high(arr);',
  10299. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  10300. ConvertProgram;
  10301. CheckSource('TestArrayOfRecord',
  10302. LinesToStr([ // statements
  10303. 'rtl.recNewT(this, "TRec", function () {',
  10304. ' this.Int = 0;',
  10305. ' this.$eq = function (b) {',
  10306. ' return this.Int === b.Int;',
  10307. ' };',
  10308. ' this.$assign = function (s) {',
  10309. ' this.Int = s.Int;',
  10310. ' return this;',
  10311. ' };',
  10312. '});',
  10313. 'this.DoIt = function (vd, vc, vv) {',
  10314. '};',
  10315. 'this.Arr = [];',
  10316. 'this.r = this.TRec.$new();',
  10317. 'this.i = 0;'
  10318. ]),
  10319. LinesToStr([ // $mod.$main
  10320. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  10321. '$mod.Arr[0].Int = 4;',
  10322. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  10323. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  10324. '$mod.Arr[7].$assign($mod.r);',
  10325. '$mod.r.$assign($mod.Arr[8]);',
  10326. '$mod.i = 0;',
  10327. '$mod.i = rtl.length($mod.Arr)-1;',
  10328. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  10329. '']));
  10330. end;
  10331. procedure TTestModule.TestArray_StaticRecord;
  10332. begin
  10333. StartProgram(false);
  10334. Add([
  10335. 'type',
  10336. ' TRec = record',
  10337. ' Int: longint;',
  10338. ' end;',
  10339. ' TArrayRec = array[1..2] of TRec;',
  10340. 'var',
  10341. ' Arr: TArrayRec;',
  10342. 'begin',
  10343. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  10344. '']);
  10345. ConvertProgram;
  10346. CheckSource('TestArray_StaticRecord',
  10347. LinesToStr([ // statements
  10348. 'rtl.recNewT(this, "TRec", function () {',
  10349. ' this.Int = 0;',
  10350. ' this.$eq = function (b) {',
  10351. ' return this.Int === b.Int;',
  10352. ' };',
  10353. ' this.$assign = function (s) {',
  10354. ' this.Int = s.Int;',
  10355. ' return this;',
  10356. ' };',
  10357. '});',
  10358. 'this.TArrayRec$clone = function (a) {',
  10359. ' var b = [];',
  10360. ' b.length = 2;',
  10361. ' for (var c = 0; c < 2; c++) b[c] = $mod.TRec.$clone(a[c]);',
  10362. ' return b;',
  10363. '};',
  10364. 'this.Arr = rtl.arraySetLength(null, this.TRec, 2);',
  10365. '']),
  10366. LinesToStr([ // $mod.$main
  10367. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  10368. end;
  10369. procedure TTestModule.TestArrayOfSet;
  10370. begin
  10371. StartProgram(false);
  10372. Add([
  10373. 'type',
  10374. ' TFlag = (big,small);',
  10375. ' TSetOfFlag = set of tflag;',
  10376. ' TArrayFlag = array of TSetOfFlag;',
  10377. 'procedure DoIt(const a: Tarrayflag);',
  10378. 'begin',
  10379. 'end;',
  10380. 'var',
  10381. ' f: TFlag;',
  10382. ' s: TSetOfFlag;',
  10383. ' Arr: TArrayFlag;',
  10384. ' i: longint;',
  10385. 'begin',
  10386. ' SetLength(arr,3);',
  10387. ' arr[0]:=s;',
  10388. ' arr[1]:=[big];',
  10389. ' arr[2]:=[big]+s;',
  10390. ' arr[3]:=s+[big];',
  10391. ' arr[4]:=arr[5];',
  10392. ' s:=arr[6];',
  10393. ' i:=low(arr);',
  10394. ' i:=high(arr);',
  10395. ' DoIt(arr);',
  10396. ' DoIt([s]);',
  10397. ' DoIt([[],s]);',
  10398. ' DoIt([s,[]]);',
  10399. '']);
  10400. ConvertProgram;
  10401. CheckSource('TestArrayOfSet',
  10402. LinesToStr([ // statements
  10403. 'this.TFlag = {',
  10404. ' "0": "big",',
  10405. ' big: 0,',
  10406. ' "1": "small",',
  10407. ' small: 1',
  10408. '};',
  10409. 'this.DoIt = function (a) {',
  10410. '};',
  10411. 'this.f = 0;',
  10412. 'this.s = {};',
  10413. 'this.Arr = [];',
  10414. 'this.i = 0;',
  10415. '']),
  10416. LinesToStr([ // $mod.$main
  10417. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  10418. '$mod.Arr[0] = rtl.refSet($mod.s);',
  10419. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  10420. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  10421. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  10422. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  10423. '$mod.s = rtl.refSet($mod.Arr[6]);',
  10424. '$mod.i = 0;',
  10425. '$mod.i = rtl.length($mod.Arr) - 1;',
  10426. '$mod.DoIt($mod.Arr);',
  10427. '$mod.DoIt([rtl.refSet($mod.s)]);',
  10428. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  10429. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  10430. '']));
  10431. end;
  10432. procedure TTestModule.TestArray_DynAsParam;
  10433. begin
  10434. StartProgram(false);
  10435. Add([
  10436. 'type integer = longint;',
  10437. 'type TArrInt = array of integer;',
  10438. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  10439. 'var vJ: TArrInt;',
  10440. 'begin',
  10441. ' vg:=vg;',
  10442. ' vj:=vh;',
  10443. ' vi:=vi;',
  10444. ' doit(vg,vg,vg);',
  10445. ' doit(vh,vh,vj);',
  10446. ' doit(vi,vi,vi);',
  10447. ' doit(vj,vj,vj);',
  10448. 'end;',
  10449. 'var i: TArrInt;',
  10450. 'begin',
  10451. ' doit(i,i,i);']);
  10452. ConvertProgram;
  10453. CheckSource('TestArray_DynAsParams',
  10454. LinesToStr([ // statements
  10455. 'this.DoIt = function (vG,vH,vI) {',
  10456. ' var vJ = [];',
  10457. ' vG = rtl.arrayRef(vG);',
  10458. ' vJ = rtl.arrayRef(vH);',
  10459. ' vI.set(rtl.arrayRef(vI.get()));',
  10460. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  10461. ' get: function () {',
  10462. ' return vG;',
  10463. ' },',
  10464. ' set: function (v) {',
  10465. ' vG = v;',
  10466. ' }',
  10467. ' });',
  10468. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  10469. ' get: function () {',
  10470. ' return vJ;',
  10471. ' },',
  10472. ' set: function (v) {',
  10473. ' vJ = v;',
  10474. ' }',
  10475. ' });',
  10476. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  10477. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  10478. ' get: function () {',
  10479. ' return vJ;',
  10480. ' },',
  10481. ' set: function (v) {',
  10482. ' vJ = v;',
  10483. ' }',
  10484. ' });',
  10485. '};',
  10486. 'this.i = [];'
  10487. ]),
  10488. LinesToStr([
  10489. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  10490. ' p: $mod,',
  10491. ' get: function () {',
  10492. ' return this.p.i;',
  10493. ' },',
  10494. ' set: function (v) {',
  10495. ' this.p.i = v;',
  10496. ' }',
  10497. '});'
  10498. ]));
  10499. end;
  10500. procedure TTestModule.TestArray_StaticAsParam;
  10501. begin
  10502. StartProgram(false);
  10503. Add([
  10504. 'type integer = longint;',
  10505. 'type TArrInt = array[1..2] of integer;',
  10506. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  10507. 'var vJ: TArrInt;',
  10508. 'begin',
  10509. ' vg:=vg;',
  10510. ' vj:=vh;',
  10511. ' vi:=vi;',
  10512. ' doit(vg,vg,vg);',
  10513. ' doit(vh,vh,vj);',
  10514. ' doit(vi,vi,vi);',
  10515. ' doit(vj,vj,vj);',
  10516. 'end;',
  10517. 'var i: TArrInt;',
  10518. 'begin',
  10519. ' doit(i,i,i);']);
  10520. ConvertProgram;
  10521. CheckSource('TestArray_StaticAsParams',
  10522. LinesToStr([ // statements
  10523. 'this.DoIt = function (vG,vH,vI) {',
  10524. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  10525. ' vG = vG.slice(0);',
  10526. ' vJ = vH.slice(0);',
  10527. ' vI.set(vI.get().slice(0));',
  10528. ' $mod.DoIt(vG.slice(0), vG, {',
  10529. ' get: function () {',
  10530. ' return vG;',
  10531. ' },',
  10532. ' set: function (v) {',
  10533. ' vG = v;',
  10534. ' }',
  10535. ' });',
  10536. ' $mod.DoIt(vH.slice(0), vH, {',
  10537. ' get: function () {',
  10538. ' return vJ;',
  10539. ' },',
  10540. ' set: function (v) {',
  10541. ' vJ = v;',
  10542. ' }',
  10543. ' });',
  10544. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  10545. ' $mod.DoIt(vJ.slice(0), vJ, {',
  10546. ' get: function () {',
  10547. ' return vJ;',
  10548. ' },',
  10549. ' set: function (v) {',
  10550. ' vJ = v;',
  10551. ' }',
  10552. ' });',
  10553. '};',
  10554. 'this.i = rtl.arraySetLength(null, 0, 2);'
  10555. ]),
  10556. LinesToStr([
  10557. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  10558. ' p: $mod,',
  10559. ' get: function () {',
  10560. ' return this.p.i;',
  10561. ' },',
  10562. ' set: function (v) {',
  10563. ' this.p.i = v;',
  10564. ' }',
  10565. '});'
  10566. ]));
  10567. end;
  10568. procedure TTestModule.TestArrayElement_AsParams;
  10569. begin
  10570. StartProgram(false);
  10571. Add('type integer = longint;');
  10572. Add('type TArrayInt = array of integer;');
  10573. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  10574. Add('var vJ: tarrayint;');
  10575. Add('begin');
  10576. Add(' vi:=vi;');
  10577. Add(' doit(vi,vi,vi);');
  10578. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  10579. Add('end;');
  10580. Add('var a: TArrayInt;');
  10581. Add('begin');
  10582. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  10583. ConvertProgram;
  10584. CheckSource('TestArrayElement_AsParams',
  10585. LinesToStr([ // statements
  10586. 'this.DoIt = function (vG,vH,vI) {',
  10587. ' var vJ = [];',
  10588. ' vI.set(vI.get());',
  10589. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  10590. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  10591. ' a:1+3,',
  10592. ' p:vJ,',
  10593. ' get: function () {',
  10594. ' return this.p[this.a];',
  10595. ' },',
  10596. ' set: function (v) {',
  10597. ' this.p[this.a] = v;',
  10598. ' }',
  10599. ' });',
  10600. '};',
  10601. 'this.a = [];'
  10602. ]),
  10603. LinesToStr([
  10604. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  10605. ' a: 1+6,',
  10606. ' p: $mod.a,',
  10607. ' get: function () {',
  10608. ' return this.p[this.a];',
  10609. ' },',
  10610. ' set: function (v) {',
  10611. ' this.p[this.a] = v;',
  10612. ' }',
  10613. '});'
  10614. ]));
  10615. end;
  10616. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  10617. begin
  10618. StartProgram(false);
  10619. Add('type Integer = longint;');
  10620. Add('type TArrayInt = array of integer;');
  10621. Add('function GetArr(vB: integer = 0): tarrayint;');
  10622. Add('begin');
  10623. Add('end;');
  10624. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10625. Add('begin');
  10626. Add('end;');
  10627. Add('begin');
  10628. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  10629. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  10630. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  10631. ConvertProgram;
  10632. CheckSource('TestArrayElementFromFuncResult_AsParams',
  10633. LinesToStr([ // statements
  10634. 'this.GetArr = function (vB) {',
  10635. ' var Result = [];',
  10636. ' return Result;',
  10637. '};',
  10638. 'this.DoIt = function (vG,vH,vI) {',
  10639. '};'
  10640. ]),
  10641. LinesToStr([
  10642. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  10643. ' a: 1+3,',
  10644. ' p: $mod.GetArr(0),',
  10645. ' get: function () {',
  10646. ' return this.p[this.a];',
  10647. ' },',
  10648. ' set: function (v) {',
  10649. ' this.p[this.a] = v;',
  10650. ' }',
  10651. '});',
  10652. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  10653. ' a: 2+3,',
  10654. ' p: $mod.GetArr(0),',
  10655. ' get: function () {',
  10656. ' return this.p[this.a];',
  10657. ' },',
  10658. ' set: function (v) {',
  10659. ' this.p[this.a] = v;',
  10660. ' }',
  10661. '});',
  10662. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  10663. ' a: 3+3,',
  10664. ' p: $mod.GetArr(9),',
  10665. ' get: function () {',
  10666. ' return this.p[this.a];',
  10667. ' },',
  10668. ' set: function (v) {',
  10669. ' this.p[this.a] = v;',
  10670. ' }',
  10671. '});',
  10672. '']));
  10673. end;
  10674. procedure TTestModule.TestArrayEnumTypeRange;
  10675. begin
  10676. StartProgram(false);
  10677. Add([
  10678. 'type',
  10679. ' TEnum = (red,blue);',
  10680. ' TEnumArray = array[TEnum] of longint;',
  10681. 'var',
  10682. ' e: TEnum;',
  10683. ' i: longint;',
  10684. ' a: TEnumArray;',
  10685. ' numbers: TEnumArray = (1,2);',
  10686. ' names: array[TEnum] of string = (''red'',''blue'');',
  10687. 'begin',
  10688. ' e:=low(a);',
  10689. ' e:=high(a);',
  10690. ' i:=a[red];',
  10691. ' a[e]:=a[e];']);
  10692. ConvertProgram;
  10693. CheckSource('TestArrayEnumTypeRange',
  10694. LinesToStr([ // statements
  10695. ' this.TEnum = {',
  10696. ' "0": "red",',
  10697. ' red: 0,',
  10698. ' "1": "blue",',
  10699. ' blue: 1',
  10700. '};',
  10701. 'this.e = 0;',
  10702. 'this.i = 0;',
  10703. 'this.a = rtl.arraySetLength(null,0,2);',
  10704. 'this.numbers = [1, 2];',
  10705. 'this.names = ["red", "blue"];',
  10706. '']),
  10707. LinesToStr([ // $mod.$main
  10708. '$mod.e = $mod.TEnum.red;',
  10709. '$mod.e = $mod.TEnum.blue;',
  10710. '$mod.i = $mod.a[$mod.TEnum.red];',
  10711. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  10712. '']));
  10713. end;
  10714. procedure TTestModule.TestArray_SetLengthOutArg;
  10715. begin
  10716. StartProgram(false);
  10717. Add([
  10718. 'type TArrInt = array of longint;',
  10719. 'procedure DoIt(out a: TArrInt);',
  10720. 'begin',
  10721. ' SetLength(a,2);',
  10722. 'end;',
  10723. 'begin',
  10724. '']);
  10725. ConvertProgram;
  10726. CheckSource('TestArray_SetLengthOutArg',
  10727. LinesToStr([ // statements
  10728. 'this.DoIt = function (a) {',
  10729. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  10730. '};',
  10731. '']),
  10732. LinesToStr([
  10733. '']));
  10734. end;
  10735. procedure TTestModule.TestArray_SetLengthProperty;
  10736. begin
  10737. StartProgram(false);
  10738. Add('type');
  10739. Add(' TArrInt = array of longint;');
  10740. Add(' TObject = class');
  10741. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  10742. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  10743. Add(' property Colors: TArrInt read GetColors write SetColors;');
  10744. Add(' end;');
  10745. Add('var Obj: TObject;');
  10746. Add('begin');
  10747. Add(' SetLength(Obj.Colors,2);');
  10748. ConvertProgram;
  10749. CheckSource('TestArray_SetLengthProperty',
  10750. LinesToStr([ // statements
  10751. 'rtl.createClass(this, "TObject", null, function () {',
  10752. ' this.$init = function () {',
  10753. ' };',
  10754. ' this.$final = function () {',
  10755. ' };',
  10756. '});',
  10757. 'this.Obj = null;',
  10758. '']),
  10759. LinesToStr([
  10760. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  10761. '']));
  10762. end;
  10763. procedure TTestModule.TestArray_SetLengthMultiDim;
  10764. begin
  10765. StartProgram(false);
  10766. Add([
  10767. 'type',
  10768. ' TArrArrInt = array of array of longint;',
  10769. ' TArrStaInt = array of array[1..2] of longint;',
  10770. 'var',
  10771. ' a: TArrArrInt;',
  10772. ' b: TArrStaInt;',
  10773. 'begin',
  10774. ' SetLength(a,2);',
  10775. ' SetLength(a,3,4);',
  10776. ' SetLength(b,5);',
  10777. '']);
  10778. ConvertProgram;
  10779. CheckSource('TestArray_SetLengthMultiDim',
  10780. LinesToStr([ // statements
  10781. 'this.a = [];',
  10782. 'this.b = [];',
  10783. '']),
  10784. LinesToStr([
  10785. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  10786. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  10787. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  10788. '']));
  10789. end;
  10790. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  10791. begin
  10792. StartProgram(false);
  10793. Add([
  10794. 'type',
  10795. ' TStaArr1 = array[1..3] of boolean;',
  10796. //' TStaArr2 = array[5..6] of TStaArr1;',
  10797. ' TDynArr1StaArr1 = array of TStaArr1;',
  10798. //' TDynArr1StaArr2 = array of TStaArr2;',
  10799. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  10800. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  10801. 'var',
  10802. ' DynArr1StaArr1: TDynArr1StaArr1;',
  10803. //' DynArr1StaArr2: TDynArr1StaArr1;',
  10804. ' DynArr2StaArr1: TDynArr2StaArr1;',
  10805. //' DynArr2StaArr2: TDynArr2StaArr2;',
  10806. 'begin',
  10807. ' SetLength(DynArr1StaArr1,11);',
  10808. ' SetLength(DynArr2StaArr1,12);',
  10809. ' SetLength(DynArr2StaArr1[13],14);',
  10810. ' SetLength(DynArr2StaArr1,15,16);',
  10811. //' SetLength(DynArr1StaArr2,21);',
  10812. //' SetLength(DynArr2StaArr2,22);',
  10813. //' SetLength(DynArr2StaArr2[23],24);',
  10814. //' SetLength(DynArr2StaArr2,25,26);',
  10815. '']);
  10816. ConvertProgram;
  10817. CheckSource('TestArray_DynOfStatic',
  10818. LinesToStr([ // statements
  10819. 'this.DynArr1StaArr1 = [];',
  10820. 'this.DynArr2StaArr1 = [];',
  10821. '']),
  10822. LinesToStr([ // $mod.$main
  10823. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  10824. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  10825. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  10826. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  10827. ' $mod.DynArr2StaArr1,',
  10828. ' false,',
  10829. ' 15,',
  10830. ' 16,',
  10831. ' "s",',
  10832. ' 3',
  10833. ');',
  10834. '']));
  10835. end;
  10836. procedure TTestModule.TestArray_OpenArrayOfString;
  10837. begin
  10838. StartProgram(false);
  10839. Add('procedure DoIt(const a: array of String);');
  10840. Add('var');
  10841. Add(' i: longint;');
  10842. Add(' s: string;');
  10843. Add('begin');
  10844. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  10845. Add('end;');
  10846. Add('var s: string;');
  10847. Add('begin');
  10848. Add(' DoIt([]);');
  10849. Add(' DoIt([s,''foo'','''',s+s]);');
  10850. ConvertProgram;
  10851. CheckSource('TestArray_OpenArrayOfString',
  10852. LinesToStr([ // statements
  10853. 'this.DoIt = function (a) {',
  10854. ' var i = 0;',
  10855. ' var s = "";',
  10856. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  10857. ' i = $l;',
  10858. ' s = a[rtl.length(a) - i - 1];',
  10859. ' };',
  10860. '};',
  10861. 'this.s = "";',
  10862. '']),
  10863. LinesToStr([
  10864. '$mod.DoIt([]);',
  10865. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  10866. '']));
  10867. end;
  10868. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  10869. begin
  10870. StartProgram(false);
  10871. Add([
  10872. 'type TArr = array of char;',
  10873. 'var',
  10874. ' c: char;',
  10875. ' s: string;',
  10876. ' a: TArr;',
  10877. 'procedure Run(const a: array of char);',
  10878. 'begin',
  10879. ' Run(c);',
  10880. ' Run(s);',
  10881. 'end;',
  10882. 'begin',
  10883. ' a:=c;',
  10884. ' a:=s;',
  10885. ' a:=#13;',
  10886. ' a:=''Foo'';',
  10887. ' Run(c);',
  10888. ' Run(s);',
  10889. '']);
  10890. ConvertProgram;
  10891. CheckSource('TestArray_ArrayOfCharAssignString',
  10892. LinesToStr([ // statements
  10893. 'this.c = "\x00";',
  10894. 'this.s = "";',
  10895. 'this.a = [];',
  10896. 'this.Run = function (a) {',
  10897. ' $mod.Run($mod.c.split(""));',
  10898. ' $mod.Run($mod.s.split(""));',
  10899. '};',
  10900. '']),
  10901. LinesToStr([
  10902. '$mod.a = $mod.c.split("");',
  10903. '$mod.a = $mod.s.split("");',
  10904. '$mod.a = "\r".split("");',
  10905. '$mod.a = "Foo".split("");',
  10906. '$mod.Run($mod.c.split(""));',
  10907. '$mod.Run($mod.s.split(""));',
  10908. '']));
  10909. end;
  10910. procedure TTestModule.TestArray_ConstRef;
  10911. begin
  10912. StartProgram(false);
  10913. Add([
  10914. 'type TArr = array of word;',
  10915. 'procedure Run(constref a: TArr);',
  10916. 'begin',
  10917. 'end;',
  10918. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  10919. 'var l: TArr;',
  10920. 'begin',
  10921. ' Run(l);',
  10922. ' Run(a);',
  10923. ' Run(b);',
  10924. ' Run(c);',
  10925. ' Run(d);',
  10926. ' Run(e);',
  10927. 'end;',
  10928. 'begin',
  10929. '']);
  10930. ConvertProgram;
  10931. CheckResolverUnexpectedHints();
  10932. CheckSource('TestArray_ConstRef',
  10933. LinesToStr([ // statements
  10934. 'this.Run = function (a) {',
  10935. '};',
  10936. 'this.Fly = function (a, b, c, d, e) {',
  10937. ' var l = [];',
  10938. ' $mod.Run(l);',
  10939. ' $mod.Run(a);',
  10940. ' $mod.Run(b.get());',
  10941. ' $mod.Run(c.get());',
  10942. ' $mod.Run(d);',
  10943. ' $mod.Run(e);',
  10944. '};',
  10945. '']),
  10946. LinesToStr([
  10947. '']));
  10948. end;
  10949. procedure TTestModule.TestArray_Concat;
  10950. begin
  10951. StartProgram(false);
  10952. Add([
  10953. 'type',
  10954. ' integer = longint;',
  10955. ' TFlag = (big,small);',
  10956. ' TFlags = set of TFlag;',
  10957. ' TRec = record',
  10958. ' i: integer;',
  10959. ' end;',
  10960. ' TArrInt = array of integer;',
  10961. ' TArrRec = array of TRec;',
  10962. ' TArrFlag = array of TFlag;',
  10963. ' TArrSet = array of TFlags;',
  10964. ' TArrJSValue = array of jsvalue;',
  10965. 'var',
  10966. ' ArrInt1, ArrInt2: tarrint;',
  10967. ' ArrRec1, ArrRec2: tarrrec;',
  10968. ' ArrFlag1, ArrFlag2: tarrflag;',
  10969. ' ArrSet1, ArrSet2: tarrset;',
  10970. ' ArrJSValue1, ArrJSValue2: tarrjsvalue;',
  10971. 'begin',
  10972. ' arrint1:=concat(arrint2);',
  10973. ' arrint1:=concat(arrint2,arrint2);',
  10974. ' arrint1:=concat(arrint2,arrint2,arrint2);',
  10975. ' arrrec1:=concat(arrrec2);',
  10976. ' arrrec1:=concat(arrrec2,arrrec2);',
  10977. ' arrrec1:=concat(arrrec2,arrrec2,arrrec2);',
  10978. ' arrset1:=concat(arrset2);',
  10979. ' arrset1:=concat(arrset2,arrset2);',
  10980. ' arrset1:=concat(arrset2,arrset2,arrset2);',
  10981. ' arrjsvalue1:=concat(arrjsvalue2);',
  10982. ' arrjsvalue1:=concat(arrjsvalue2,arrjsvalue2);',
  10983. ' arrjsvalue1:=concat(arrjsvalue2,arrjsvalue2,arrjsvalue2);',
  10984. ' arrint1:=concat([1],arrint2);',
  10985. ' arrflag1:=concat([big]);',
  10986. ' arrflag1:=concat([big],arrflag2);',
  10987. ' arrflag1:=concat(arrflag2,[small]);',
  10988. '']);
  10989. ConvertProgram;
  10990. CheckSource('TestArray_Concat',
  10991. LinesToStr([ // statements
  10992. 'this.TFlag = {',
  10993. ' "0": "big",',
  10994. ' big: 0,',
  10995. ' "1": "small",',
  10996. ' small: 1',
  10997. '};',
  10998. 'rtl.recNewT(this, "TRec", function () {',
  10999. ' this.i = 0;',
  11000. ' this.$eq = function (b) {',
  11001. ' return this.i === b.i;',
  11002. ' };',
  11003. ' this.$assign = function (s) {',
  11004. ' this.i = s.i;',
  11005. ' return this;',
  11006. ' };',
  11007. '});',
  11008. 'this.ArrInt1 = [];',
  11009. 'this.ArrInt2 = [];',
  11010. 'this.ArrRec1 = [];',
  11011. 'this.ArrRec2 = [];',
  11012. 'this.ArrFlag1 = [];',
  11013. 'this.ArrFlag2 = [];',
  11014. 'this.ArrSet1 = [];',
  11015. 'this.ArrSet2 = [];',
  11016. 'this.ArrJSValue1 = [];',
  11017. 'this.ArrJSValue2 = [];',
  11018. '']),
  11019. LinesToStr([ // $mod.$main
  11020. '$mod.ArrInt1 = rtl.arrayRef($mod.ArrInt2);',
  11021. '$mod.ArrInt1 = rtl.arrayConcatN($mod.ArrInt2, $mod.ArrInt2);',
  11022. '$mod.ArrInt1 = rtl.arrayConcatN($mod.ArrInt2, $mod.ArrInt2, $mod.ArrInt2);',
  11023. '$mod.ArrRec1 = rtl.arrayRef($mod.ArrRec2);',
  11024. '$mod.ArrRec1 = rtl.arrayConcat($mod.TRec, $mod.ArrRec2, $mod.ArrRec2);',
  11025. '$mod.ArrRec1 = rtl.arrayConcat($mod.TRec, $mod.ArrRec2, $mod.ArrRec2, $mod.ArrRec2);',
  11026. '$mod.ArrSet1 = rtl.arrayRef($mod.ArrSet2);',
  11027. '$mod.ArrSet1 = rtl.arrayConcat("refSet", $mod.ArrSet2, $mod.ArrSet2);',
  11028. '$mod.ArrSet1 = rtl.arrayConcat("refSet", $mod.ArrSet2, $mod.ArrSet2, $mod.ArrSet2);',
  11029. '$mod.ArrJSValue1 = rtl.arrayRef($mod.ArrJSValue2);',
  11030. '$mod.ArrJSValue1 = rtl.arrayConcatN($mod.ArrJSValue2, $mod.ArrJSValue2);',
  11031. '$mod.ArrJSValue1 = rtl.arrayConcatN($mod.ArrJSValue2, $mod.ArrJSValue2, $mod.ArrJSValue2);',
  11032. '$mod.ArrInt1 = rtl.arrayConcatN([1], $mod.ArrInt2);',
  11033. '$mod.ArrFlag1 = [$mod.TFlag.big];',
  11034. '$mod.ArrFlag1 = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag2);',
  11035. '$mod.ArrFlag1 = rtl.arrayConcatN($mod.ArrFlag2, [$mod.TFlag.small]);',
  11036. '']));
  11037. end;
  11038. procedure TTestModule.TestArray_Concat_Append;
  11039. begin
  11040. StartProgram(false);
  11041. Add([
  11042. 'type',
  11043. ' integer = longint;',
  11044. ' TFlag = (big,small);',
  11045. ' TFlags = set of TFlag;',
  11046. ' TRec = record',
  11047. ' i: integer;',
  11048. ' end;',
  11049. ' TArrInt = array of integer;',
  11050. ' TArrRec = array of TRec;',
  11051. ' TArrFlag = array of TFlag;',
  11052. ' TArrSet = array of TFlags;',
  11053. ' TArrJSValue = array of jsvalue;',
  11054. 'var',
  11055. ' ArrInt: tarrint;',
  11056. ' ArrRec: tarrrec;',
  11057. ' ArrFlag: tarrflag;',
  11058. ' ArrSet: tarrset;',
  11059. ' ArrJSValue: tarrjsvalue;',
  11060. ' r: TRec;',
  11061. ' f: TFlags;',
  11062. 'begin',
  11063. ' // append',
  11064. ' arrint:=concat(arrint);',
  11065. ' arrint:=concat(arrint,[2]);',
  11066. ' arrint:=concat(arrint,[3,4]);',
  11067. ' arrrec:=concat(arrrec);',
  11068. ' arrrec:=concat(arrrec,[r]);',
  11069. ' arrrec:=concat(arrrec,[r,r]);',
  11070. ' arrset:=concat(arrset);',
  11071. ' arrset:=concat(arrset,[f]);',
  11072. ' arrset:=concat(arrset,[f,f]);',
  11073. ' arrjsvalue:=concat(arrjsvalue);',
  11074. ' arrjsvalue:=concat(arrjsvalue,[11]);',
  11075. ' arrjsvalue:=concat(arrjsvalue,[12,13]);',
  11076. ' arrflag:=concat(arrflag);',
  11077. ' arrflag:=concat(arrflag,[small]);',
  11078. ' arrflag:=concat(arrflag,[small,big]);',
  11079. '']);
  11080. ConvertProgram;
  11081. CheckSource('TestArray_Concat_Append',
  11082. LinesToStr([ // statements
  11083. 'this.TFlag = {',
  11084. ' "0": "big",',
  11085. ' big: 0,',
  11086. ' "1": "small",',
  11087. ' small: 1',
  11088. '};',
  11089. 'rtl.recNewT(this, "TRec", function () {',
  11090. ' this.i = 0;',
  11091. ' this.$eq = function (b) {',
  11092. ' return this.i === b.i;',
  11093. ' };',
  11094. ' this.$assign = function (s) {',
  11095. ' this.i = s.i;',
  11096. ' return this;',
  11097. ' };',
  11098. '});',
  11099. 'this.ArrInt = [];',
  11100. 'this.ArrRec = [];',
  11101. 'this.ArrFlag = [];',
  11102. 'this.ArrSet = [];',
  11103. 'this.ArrJSValue = [];',
  11104. 'this.r = this.TRec.$new();',
  11105. 'this.f = {};',
  11106. '']),
  11107. LinesToStr([ // $mod.$main
  11108. '$mod.ArrInt = $mod.ArrInt;',
  11109. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 2);',
  11110. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 3, 4);',
  11111. '$mod.ArrRec = $mod.ArrRec;',
  11112. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r);',
  11113. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r, $mod.r);',
  11114. '$mod.ArrSet = $mod.ArrSet;',
  11115. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f);',
  11116. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f, $mod.f);',
  11117. '$mod.ArrJSValue = $mod.ArrJSValue;',
  11118. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 11);',
  11119. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 12, 13);',
  11120. '$mod.ArrFlag = $mod.ArrFlag;',
  11121. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small);',
  11122. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small, $mod.TFlag.big);',
  11123. '']));
  11124. end;
  11125. procedure TTestModule.TestArray_Concat_Append_Var;
  11126. begin
  11127. StartProgram(false);
  11128. Add([
  11129. 'type',
  11130. ' TArrInt = array of word;',
  11131. '',
  11132. 'procedure Fly(a: TArrInt; var b: TArrInt);',
  11133. 'begin',
  11134. ' a:=concat(a,[2]);',
  11135. ' b:=concat(b,[2]);',
  11136. 'end;',
  11137. 'var',
  11138. ' ArrInt: tarrint;',
  11139. 'begin',
  11140. ' Fly(ArrInt,ArrInt);',
  11141. '']);
  11142. ConvertProgram;
  11143. CheckSource('TestArray_Concat_Append_Var',
  11144. LinesToStr([ // statements
  11145. 'this.Fly = function (a, b) {',
  11146. ' a = rtl.arrayPushN(a, 2);',
  11147. ' b.set(rtl.arrayPushN(b.get(), 2));',
  11148. '};',
  11149. 'this.ArrInt = [];',
  11150. '']),
  11151. LinesToStr([ // $mod.$main
  11152. '$mod.Fly(rtl.arrayRef($mod.ArrInt), {',
  11153. ' p: $mod,',
  11154. ' get: function () {',
  11155. ' return this.p.ArrInt;',
  11156. ' },',
  11157. ' set: function (v) {',
  11158. ' this.p.ArrInt = v;',
  11159. ' }',
  11160. '});',
  11161. '']));
  11162. end;
  11163. procedure TTestModule.TestArray_Copy;
  11164. begin
  11165. StartProgram(false);
  11166. Add([
  11167. 'type',
  11168. ' integer = longint;',
  11169. ' TFlag = (big,small);',
  11170. ' TFlags = set of TFlag;',
  11171. ' TRec = record',
  11172. ' i: integer;',
  11173. ' end;',
  11174. ' TArrInt = array of integer;',
  11175. ' TArrRec = array of TRec;',
  11176. ' TArrSet = array of TFlags;',
  11177. ' TArrJSValue = array of jsvalue;',
  11178. 'var',
  11179. ' ArrInt: tarrint;',
  11180. ' ArrRec: tarrrec;',
  11181. ' ArrSet: tarrset;',
  11182. ' ArrJSValue: tarrjsvalue;',
  11183. 'begin',
  11184. ' arrint:=copy(arrint);',
  11185. ' arrint:=copy(arrint,2);',
  11186. ' arrint:=copy(arrint,3,4);',
  11187. ' arrint:=copy([1,1],1,2);',
  11188. ' arrrec:=copy(arrrec);',
  11189. ' arrrec:=copy(arrrec,5);',
  11190. ' arrrec:=copy(arrrec,6,7);',
  11191. ' arrset:=copy(arrset);',
  11192. ' arrset:=copy(arrset,8);',
  11193. ' arrset:=copy(arrset,9,10);',
  11194. ' arrjsvalue:=copy(arrjsvalue);',
  11195. ' arrjsvalue:=copy(arrjsvalue,11);',
  11196. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  11197. ' ']);
  11198. ConvertProgram;
  11199. CheckSource('TestArray_Copy',
  11200. LinesToStr([ // statements
  11201. 'this.TFlag = {',
  11202. ' "0": "big",',
  11203. ' big: 0,',
  11204. ' "1": "small",',
  11205. ' small: 1',
  11206. '};',
  11207. 'rtl.recNewT(this, "TRec", function () {',
  11208. ' this.i = 0;',
  11209. ' this.$eq = function (b) {',
  11210. ' return this.i === b.i;',
  11211. ' };',
  11212. ' this.$assign = function (s) {',
  11213. ' this.i = s.i;',
  11214. ' return this;',
  11215. ' };',
  11216. '});',
  11217. 'this.ArrInt = [];',
  11218. 'this.ArrRec = [];',
  11219. 'this.ArrSet = [];',
  11220. 'this.ArrJSValue = [];',
  11221. '']),
  11222. LinesToStr([ // $mod.$main
  11223. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  11224. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  11225. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  11226. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  11227. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  11228. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  11229. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  11230. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  11231. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  11232. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  11233. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  11234. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  11235. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  11236. '']));
  11237. end;
  11238. procedure TTestModule.TestArray_InsertDelete;
  11239. begin
  11240. StartProgram(false);
  11241. Add([
  11242. 'type',
  11243. ' integer = longint;',
  11244. ' TFlag = (big,small);',
  11245. ' TFlags = set of TFlag;',
  11246. ' TRec = record',
  11247. ' i: integer;',
  11248. ' end;',
  11249. ' TArrInt = array of integer;',
  11250. ' TArrRec = array of TRec;',
  11251. ' TArrSet = array of TFlags;',
  11252. ' TArrJSValue = array of jsvalue;',
  11253. ' TArrArrInt = array of TArrInt;',
  11254. 'var',
  11255. ' ArrInt: tarrint;',
  11256. ' ArrRec: tarrrec;',
  11257. ' ArrSet: tarrset;',
  11258. ' ArrJSValue: tarrjsvalue;',
  11259. ' ArrArrInt: TArrArrInt;',
  11260. 'begin',
  11261. ' Insert(1,arrint,2);',
  11262. ' Insert(arrint[3],arrint,4);',
  11263. ' Insert(arrrec[5],arrrec,6);',
  11264. ' Insert(arrset[7],arrset,7);',
  11265. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  11266. ' Insert(10,arrjsvalue,11);',
  11267. ' Insert([23],arrarrint,22);',
  11268. ' Delete(arrint,12,13);',
  11269. ' Delete(arrrec,14,15);',
  11270. ' Delete(arrset,17,18);',
  11271. ' Delete(arrjsvalue,19,10);']);
  11272. ConvertProgram;
  11273. CheckSource('TestArray_InsertDelete',
  11274. LinesToStr([ // statements
  11275. 'this.TFlag = {',
  11276. ' "0": "big",',
  11277. ' big: 0,',
  11278. ' "1": "small",',
  11279. ' small: 1',
  11280. '};',
  11281. 'rtl.recNewT(this, "TRec", function () {',
  11282. ' this.i = 0;',
  11283. ' this.$eq = function (b) {',
  11284. ' return this.i === b.i;',
  11285. ' };',
  11286. ' this.$assign = function (s) {',
  11287. ' this.i = s.i;',
  11288. ' return this;',
  11289. ' };',
  11290. '});',
  11291. 'this.ArrInt = [];',
  11292. 'this.ArrRec = [];',
  11293. 'this.ArrSet = [];',
  11294. 'this.ArrJSValue = [];',
  11295. 'this.ArrArrInt = [];',
  11296. '']),
  11297. LinesToStr([ // $mod.$main
  11298. '$mod.ArrInt = rtl.arrayInsert(1, $mod.ArrInt, 2);',
  11299. '$mod.ArrInt = rtl.arrayInsert($mod.ArrInt[3], $mod.ArrInt, 4);',
  11300. '$mod.ArrRec = rtl.arrayInsert($mod.ArrRec[5], $mod.ArrRec, 6);',
  11301. '$mod.ArrSet = rtl.arrayInsert($mod.ArrSet[7], $mod.ArrSet, 7);',
  11302. '$mod.ArrJSValue = rtl.arrayInsert($mod.ArrJSValue[8], $mod.ArrJSValue, 9);',
  11303. '$mod.ArrJSValue = rtl.arrayInsert(10, $mod.ArrJSValue, 11);',
  11304. '$mod.ArrArrInt = rtl.arrayInsert([23], $mod.ArrArrInt, 22);',
  11305. '$mod.ArrInt.splice(12, 13);',
  11306. '$mod.ArrRec.splice(14, 15);',
  11307. '$mod.ArrSet.splice(17, 18);',
  11308. '$mod.ArrJSValue.splice(19, 10);',
  11309. '']));
  11310. end;
  11311. procedure TTestModule.TestArray_Add_Append;
  11312. begin
  11313. StartProgram(false);
  11314. Add([
  11315. '{$modeswitch arrayoperators}',
  11316. 'type',
  11317. ' integer = longint;',
  11318. ' TFlag = (big,small);',
  11319. ' TFlags = set of TFlag;',
  11320. ' TRec = record',
  11321. ' i: integer;',
  11322. ' end;',
  11323. ' TArrInt = array of integer;',
  11324. ' TArrRec = array of TRec;',
  11325. ' TArrFlag = array of TFlag;',
  11326. ' TArrSet = array of TFlags;',
  11327. ' TArrJSValue = array of jsvalue;',
  11328. 'var',
  11329. ' ArrInt: tarrint;',
  11330. ' ArrRec: tarrrec;',
  11331. ' ArrFlag: tarrflag;',
  11332. ' ArrSet: tarrset;',
  11333. ' ArrJSValue: tarrjsvalue;',
  11334. ' r: TRec;',
  11335. ' f: TFlags;',
  11336. 'begin',
  11337. ' // append',
  11338. ' arrint:=arrint+[2];',
  11339. ' arrint:=arrint+[3,4];',
  11340. ' arrrec:=arrrec+[r];',
  11341. ' arrrec:=arrrec+[r,r];',
  11342. ' arrset:=arrset+[f];',
  11343. ' arrset:=arrset+[f,f];',
  11344. ' arrjsvalue:=arrjsvalue+[11];',
  11345. ' arrjsvalue:=arrjsvalue+[12,13];',
  11346. ' arrflag:=arrflag+[small];',
  11347. ' arrflag:=arrflag+[small,big];',
  11348. '']);
  11349. ConvertProgram;
  11350. CheckSource('TestArray_Add_Append',
  11351. LinesToStr([ // statements
  11352. 'this.TFlag = {',
  11353. ' "0": "big",',
  11354. ' big: 0,',
  11355. ' "1": "small",',
  11356. ' small: 1',
  11357. '};',
  11358. 'rtl.recNewT(this, "TRec", function () {',
  11359. ' this.i = 0;',
  11360. ' this.$eq = function (b) {',
  11361. ' return this.i === b.i;',
  11362. ' };',
  11363. ' this.$assign = function (s) {',
  11364. ' this.i = s.i;',
  11365. ' return this;',
  11366. ' };',
  11367. '});',
  11368. 'this.ArrInt = [];',
  11369. 'this.ArrRec = [];',
  11370. 'this.ArrFlag = [];',
  11371. 'this.ArrSet = [];',
  11372. 'this.ArrJSValue = [];',
  11373. 'this.r = this.TRec.$new();',
  11374. 'this.f = {};',
  11375. '']),
  11376. LinesToStr([ // $mod.$main
  11377. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 2);',
  11378. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 3, 4);',
  11379. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r);',
  11380. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r, $mod.r);',
  11381. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f);',
  11382. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f, $mod.f);',
  11383. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 11);',
  11384. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 12, 13);',
  11385. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small);',
  11386. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small, $mod.TFlag.big);',
  11387. '']));
  11388. end;
  11389. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  11390. begin
  11391. Parser.Options:=Parser.Options+[po_cassignments];
  11392. StartProgram(false);
  11393. Add([
  11394. '{$modeswitch arrayoperators}',
  11395. 'type',
  11396. ' integer = longint;',
  11397. ' TArrInt = array of integer;',
  11398. ' TArrStr = array of string;',
  11399. 'const',
  11400. ' Ints: TArrInt = (1,2,3);',
  11401. ' Aliases: TarrStr = (''foo'',''b'');',
  11402. ' OneInt: TArrInt = (7);',
  11403. ' OneStr: array of integer = (7);',
  11404. ' Chars: array of char = ''aoc'';',
  11405. ' Names: array of string = (''a'',''foo'');',
  11406. ' NameCount = low(Names)+high(Names)+length(Names);',
  11407. 'var i: integer;',
  11408. 'begin',
  11409. ' Ints:=[];',
  11410. ' Ints:=[1,1];',
  11411. ' Ints:=[1]+[2];',
  11412. ' Ints:=[2];',
  11413. ' Ints:=[]+ints;',
  11414. ' Ints:=Ints+[];',
  11415. ' Ints:=Ints+OneInt;',
  11416. ' Ints:=Ints+[1,1];',
  11417. ' Ints:=[i,i]+Ints;',
  11418. ' Ints:=[1]+[i]+[3];',
  11419. '']);
  11420. ConvertProgram;
  11421. CheckSource('TestArray_DynArrayConstObjFPC',
  11422. LinesToStr([ // statements
  11423. 'this.Ints = [1, 2, 3];',
  11424. 'this.Aliases = ["foo", "b"];',
  11425. 'this.OneInt = [7];',
  11426. 'this.OneStr = [7];',
  11427. 'this.Chars = ["a", "o", "c"];',
  11428. 'this.Names = ["a", "foo"];',
  11429. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  11430. 'this.i = 0;',
  11431. '']),
  11432. LinesToStr([ // $mod.$main
  11433. '$mod.Ints = [];',
  11434. '$mod.Ints = [1, 1];',
  11435. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  11436. '$mod.Ints = [2];',
  11437. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  11438. '$mod.Ints = $mod.Ints;',
  11439. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  11440. '$mod.Ints = rtl.arrayPushN($mod.Ints, 1, 1);',
  11441. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  11442. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  11443. '']));
  11444. end;
  11445. procedure TTestModule.TestArray_DynArrayConstDelphi;
  11446. begin
  11447. StartProgram(false);
  11448. // Note: const c = [1,1]; defines a set!
  11449. Add([
  11450. '{$mode delphi}',
  11451. 'type',
  11452. ' integer = longint;',
  11453. ' TArrInt = array of integer;',
  11454. ' TArrStr = array of string;',
  11455. 'const',
  11456. ' Ints: TArrInt = [1,1,2];',
  11457. ' Aliases: TarrStr = [''foo'',''b''];',
  11458. ' OneInt: TArrInt = [7];',
  11459. ' OneStr: array of integer = [7]+[8];',
  11460. ' Chars: array of char = ''aoc'';',
  11461. ' Names: array of string = [''a'',''a''];',
  11462. ' NameCount = low(Names)+high(Names)+length(Names);',
  11463. 'begin',
  11464. '']);
  11465. ConvertProgram;
  11466. CheckSource('TestArray_DynArrayConstDelphi',
  11467. LinesToStr([ // statements
  11468. 'this.Ints = [1, 1, 2];',
  11469. 'this.Aliases = ["foo", "b"];',
  11470. 'this.OneInt = [7];',
  11471. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  11472. 'this.Chars = ["a", "o", "c"];',
  11473. 'this.Names = ["a", "a"];',
  11474. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  11475. '']),
  11476. LinesToStr([ // $mod.$main
  11477. '']));
  11478. end;
  11479. procedure TTestModule.TestArray_ArrayLitAsParam;
  11480. begin
  11481. StartProgram(false);
  11482. Add([
  11483. '{$modeswitch arrayoperators}',
  11484. 'type',
  11485. ' integer = longint;',
  11486. ' TArrInt = array of integer;',
  11487. ' TArrSet = array of (red,green,blue);',
  11488. 'procedure DoOpenInt(const a: array of integer); forward;',
  11489. 'procedure DoInt(const a: TArrInt);',
  11490. 'begin',
  11491. ' DoInt(a+[1]);',
  11492. ' DoInt([1]+a);',
  11493. ' DoOpenInt(a);',
  11494. ' DoOpenInt(a+[1]);',
  11495. ' DoOpenInt([1]+a);',
  11496. 'end;',
  11497. 'procedure DoOpenInt(const a: array of integer);',
  11498. 'begin',
  11499. ' DoOpenInt(a+[1]);',
  11500. ' DoOpenInt([1]+a);',
  11501. ' DoInt(a);',
  11502. ' DoInt(a+[1]);',
  11503. ' DoInt([1]+a);',
  11504. 'end;',
  11505. 'procedure DoSet(const a: TArrSet);',
  11506. 'begin',
  11507. ' DoSet(a+[red]);',
  11508. ' DoSet([blue]+a);',
  11509. 'end;',
  11510. 'var',
  11511. ' i: TArrInt;',
  11512. ' s: TArrSet;',
  11513. 'begin',
  11514. ' DoInt([1]);',
  11515. ' DoInt([1]+[2]);',
  11516. ' DoInt(i+[1]);',
  11517. ' DoInt([1]+i);',
  11518. ' DoOpenInt([1]);',
  11519. ' DoOpenInt([1]+[2]);',
  11520. ' DoOpenInt(i+[1]);',
  11521. ' DoOpenInt([1]+i);',
  11522. ' DoSet([red]);',
  11523. ' DoSet([blue]+[green]);',
  11524. ' DoSet(s+[blue]);',
  11525. ' DoSet([red]+s);',
  11526. '']);
  11527. ConvertProgram;
  11528. CheckSource('TestArray_ArrayLitAsParam',
  11529. LinesToStr([ // statements
  11530. 'this.TArrSet$a = {',
  11531. ' "0": "red",',
  11532. ' red: 0,',
  11533. ' "1": "green",',
  11534. ' green: 1,',
  11535. ' "2": "blue",',
  11536. ' blue: 2',
  11537. '};',
  11538. 'this.DoInt = function (a) {',
  11539. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  11540. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  11541. ' $mod.DoOpenInt(a);',
  11542. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  11543. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  11544. '};',
  11545. 'this.DoOpenInt = function (a) {',
  11546. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  11547. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  11548. ' $mod.DoInt(a);',
  11549. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  11550. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  11551. '};',
  11552. 'this.DoSet = function (a) {',
  11553. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  11554. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  11555. '};',
  11556. 'this.i = [];',
  11557. 'this.s = [];',
  11558. '']),
  11559. LinesToStr([ // $mod.$main
  11560. '$mod.DoInt([1]);',
  11561. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  11562. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  11563. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  11564. '$mod.DoOpenInt([1]);',
  11565. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  11566. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  11567. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  11568. '$mod.DoSet([$mod.TArrSet$a.red]);',
  11569. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  11570. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  11571. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  11572. '']));
  11573. end;
  11574. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  11575. begin
  11576. StartProgram(false);
  11577. Add([
  11578. '{$modeswitch arrayoperators}',
  11579. 'type',
  11580. ' integer = longint;',
  11581. ' TArrInt = array of integer;',
  11582. ' TArrArrInt = array of TArrInt;',
  11583. 'procedure DoInt(const a: TArrArrInt);',
  11584. 'begin',
  11585. ' DoInt(a+[[1]]);',
  11586. ' DoInt([[1]]+a);',
  11587. ' DoInt(a);',
  11588. 'end;',
  11589. 'var',
  11590. ' i: TArrInt;',
  11591. ' a: TArrArrInt;',
  11592. 'begin',
  11593. ' a:=[[1]];',
  11594. ' a:=[i];',
  11595. ' a:=a+[i];',
  11596. ' a:=[i]+a;',
  11597. ' a:=[[1]+i];',
  11598. ' a:=[[1]+[2]];',
  11599. ' a:=[i+[2]];',
  11600. ' DoInt([[1]]);',
  11601. ' DoInt([[1]+[2],[3,4],[5]]);',
  11602. ' DoInt([i+[1]]+a);',
  11603. ' DoInt([i]+a);',
  11604. '']);
  11605. ConvertProgram;
  11606. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  11607. LinesToStr([ // statements
  11608. 'this.DoInt = function (a) {',
  11609. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  11610. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  11611. ' $mod.DoInt(a);',
  11612. '};',
  11613. 'this.i = [];',
  11614. 'this.a = [];',
  11615. '']),
  11616. LinesToStr([ // $mod.$main
  11617. '$mod.a = [[1]];',
  11618. '$mod.a = [$mod.i];',
  11619. '$mod.a = rtl.arrayPushN($mod.a, $mod.i);',
  11620. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  11621. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  11622. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  11623. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  11624. '$mod.DoInt([[1]]);',
  11625. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  11626. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  11627. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  11628. '']));
  11629. end;
  11630. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  11631. begin
  11632. StartProgram(false);
  11633. Add([
  11634. '{$modeswitch arrayoperators}',
  11635. 'type',
  11636. ' integer = longint;',
  11637. ' TArrInt = array[1..2] of integer;',
  11638. ' TArrArrInt = array of TArrInt;',
  11639. 'procedure DoInt(const a: TArrArrInt);',
  11640. 'begin',
  11641. ' DoInt(a+[[1,2]]);',
  11642. ' DoInt([[1,2]]+a);',
  11643. ' DoInt(a);',
  11644. 'end;',
  11645. 'var',
  11646. ' i: TArrInt;',
  11647. ' a: TArrArrInt;',
  11648. 'begin',
  11649. ' a:=[[1,1]];',
  11650. ' a:=[i];',
  11651. ' a:=a+[i];',
  11652. ' a:=[i]+a;',
  11653. ' DoInt([[1,1]]);',
  11654. ' DoInt([[1,2],[3,4]]);',
  11655. '']);
  11656. ConvertProgram;
  11657. CheckSource('TestArray_ArrayLitStaticAsParam',
  11658. LinesToStr([ // statements
  11659. 'this.DoInt = function (a) {',
  11660. ' $mod.DoInt(rtl.arrayConcat("slice", a, [[1, 2]]));',
  11661. ' $mod.DoInt(rtl.arrayConcat("slice", [[1, 2]], a));',
  11662. ' $mod.DoInt(a);',
  11663. '};',
  11664. 'this.i = rtl.arraySetLength(null, 0, 2);',
  11665. 'this.a = [];',
  11666. '']),
  11667. LinesToStr([ // $mod.$main
  11668. '$mod.a = [[1, 1]];',
  11669. '$mod.a = [$mod.i.slice(0)];',
  11670. '$mod.a = rtl.arrayPush("slice", $mod.a, $mod.i);',
  11671. '$mod.a = rtl.arrayConcat("slice", [$mod.i.slice(0)], $mod.a);',
  11672. '$mod.DoInt([[1, 1]]);',
  11673. '$mod.DoInt([[1, 2], [3, 4]]);',
  11674. '']));
  11675. end;
  11676. procedure TTestModule.TestArray_ForInArrOfString;
  11677. begin
  11678. StartProgram(false);
  11679. Add([
  11680. 'type',
  11681. 'type',
  11682. ' TMonthNameArray = array [1..12] of string;',
  11683. ' TMonthNames = TMonthNameArray;',
  11684. ' TObject = class',
  11685. ' private',
  11686. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  11687. ' public',
  11688. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  11689. ' end;',
  11690. 'var',
  11691. ' f: TObject;',
  11692. ' Month: string;',
  11693. ' Names: array of string = (''a'',''foo'',''bar'');',
  11694. ' i: longint;',
  11695. 'begin',
  11696. ' for Month in f.LongMonthNames do ;',
  11697. ' for Month in Names do ;',
  11698. ' for i:=low(Names) to high(Names) do ;',
  11699. '']);
  11700. ConvertProgram;
  11701. CheckSource('TestArray_ForInArrOfString',
  11702. LinesToStr([ // statements
  11703. 'rtl.createClass(this, "TObject", null, function () {',
  11704. ' this.$init = function () {',
  11705. ' };',
  11706. ' this.$final = function () {',
  11707. ' };',
  11708. '});',
  11709. 'this.f = null;',
  11710. 'this.Month = "";',
  11711. 'this.Names = ["a", "foo", "bar"];',
  11712. 'this.i = 0;',
  11713. '']),
  11714. LinesToStr([ // $mod.$main
  11715. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  11716. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  11717. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  11718. '']));
  11719. end;
  11720. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  11721. begin
  11722. StartProgram(false);
  11723. Add([
  11724. '{$modeswitch externalclass}',
  11725. 'type',
  11726. ' TJSObject = class external name ''Object''',
  11727. ' end;',
  11728. ' TJSArray = class external name ''Array''',
  11729. ' class function isArray(Value: JSValue) : boolean;',
  11730. ' function concat() : TJSArray; varargs;',
  11731. ' end;',
  11732. 'var',
  11733. ' aObj: TJSArray;',
  11734. ' a: array of longint;',
  11735. ' o: TJSObject;',
  11736. 'begin',
  11737. ' if TJSArray.isArray(65) then ;',
  11738. ' aObj:=TJSArray(a).concat(a);',
  11739. ' o:=TJSObject(a);',
  11740. ' aObj:=TJSArray([''bird'',''ant'']);',
  11741. '']);
  11742. ConvertProgram;
  11743. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  11744. LinesToStr([ // statements
  11745. 'this.aObj = null;',
  11746. 'this.a = [];',
  11747. 'this.o = null;',
  11748. '']),
  11749. LinesToStr([ // $mod.$main
  11750. 'if (Array.isArray(65)) ;',
  11751. '$mod.aObj = $mod.a.concat($mod.a);',
  11752. '$mod.o = $mod.a;',
  11753. '$mod.aObj = ["bird", "ant"];',
  11754. '']));
  11755. end;
  11756. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  11757. begin
  11758. StartProgram(false);
  11759. Add([
  11760. '{$modeswitch externalclass}',
  11761. 'type',
  11762. ' TArrStr = array of string;',
  11763. ' TJSArray = class external name ''Array''',
  11764. ' end;',
  11765. ' TJSObject = class external name ''Object''',
  11766. ' end;',
  11767. 'var',
  11768. ' aObj: TJSArray;',
  11769. ' a: TArrStr;',
  11770. ' jo: TJSObject;',
  11771. 'begin',
  11772. ' a:=TArrStr(aObj);',
  11773. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  11774. ' a:=TarrStr(jo);',
  11775. '']);
  11776. ConvertProgram;
  11777. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  11778. LinesToStr([ // statements
  11779. 'this.aObj = null;',
  11780. 'this.a = [];',
  11781. 'this.jo = null;',
  11782. '']),
  11783. LinesToStr([ // $mod.$main
  11784. '$mod.a = $mod.aObj;',
  11785. '$mod.aObj[1] = $mod.aObj[2];',
  11786. '$mod.a = $mod.jo;',
  11787. '']));
  11788. end;
  11789. procedure TTestModule.TestArrayOfConst_TVarRec;
  11790. begin
  11791. StartProgram(true,[supTVarRec]);
  11792. Add([
  11793. 'procedure Say(args: array of const);',
  11794. 'var',
  11795. ' i: longint;',
  11796. ' v: TVarRec;',
  11797. 'begin',
  11798. ' for i:=low(args) to high(args) do begin',
  11799. ' v:=args[i];',
  11800. ' case v.vtype of',
  11801. ' vtInteger: if length(args)=args[i].vInteger then ;',
  11802. ' end;',
  11803. ' end;',
  11804. ' for v in args do ;',
  11805. ' args:=nil;',
  11806. ' SetLength(args,2);',
  11807. 'end;',
  11808. 'begin']);
  11809. ConvertProgram;
  11810. CheckSource('TestArrayOfConst_TVarRec',
  11811. LinesToStr([ // statements
  11812. 'this.Say = function (args) {',
  11813. ' var i = 0;',
  11814. ' var v = pas.system.TVarRec.$new();',
  11815. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  11816. ' i = $l;',
  11817. ' v.$assign(args[i]);',
  11818. ' var $tmp = v.VType;',
  11819. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  11820. ' };',
  11821. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  11822. ' args = [];',
  11823. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  11824. '};',
  11825. '']),
  11826. LinesToStr([ // $mod.$main
  11827. ]));
  11828. end;
  11829. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  11830. begin
  11831. StartProgram(true,[supTVarRec]);
  11832. Add([
  11833. 'procedure Say(args: array of const);',
  11834. 'begin',
  11835. ' Say(args);',
  11836. 'end;',
  11837. 'var',
  11838. ' p: Pointer;',
  11839. ' j: jsvalue;',
  11840. ' c: currency;',
  11841. 'begin',
  11842. ' Say([]);',
  11843. ' Say([1]);',
  11844. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  11845. '']);
  11846. ConvertProgram;
  11847. CheckSource('TestArrayOfConst_PassBaseTypes',
  11848. LinesToStr([ // statements
  11849. 'this.Say = function (args) {',
  11850. ' $mod.Say(args);',
  11851. '};',
  11852. 'this.p = null;',
  11853. 'this.j = undefined;',
  11854. 'this.c = 0;',
  11855. '']),
  11856. LinesToStr([ // $mod.$main
  11857. '$mod.Say([]);',
  11858. '$mod.Say(pas.system.VarRecs(0, 1));',
  11859. '$mod.Say(pas.system.VarRecs(',
  11860. ' 9,',
  11861. ' "c",',
  11862. ' 18,',
  11863. ' "foo",',
  11864. ' 5,',
  11865. ' null,',
  11866. ' 1,',
  11867. ' true,',
  11868. ' 3,',
  11869. ' 1.3,',
  11870. ' 5,',
  11871. ' $mod.p,',
  11872. ' 20,',
  11873. ' $mod.j,',
  11874. ' 12,',
  11875. ' $mod.c',
  11876. ' ));',
  11877. '']));
  11878. end;
  11879. procedure TTestModule.TestArrayOfConst_PassObj;
  11880. begin
  11881. StartProgram(true,[supTVarRec]);
  11882. Add([
  11883. '{$interfaces corba}',
  11884. 'type',
  11885. ' TObject = class',
  11886. ' end;',
  11887. ' TClass = class of TObject;',
  11888. ' IUnknown = interface',
  11889. ' end;',
  11890. 'procedure Say(args: array of const);',
  11891. 'begin',
  11892. 'end;',
  11893. 'var',
  11894. ' o: TObject;',
  11895. ' c: TClass;',
  11896. ' i: IUnknown;',
  11897. 'begin',
  11898. ' Say([o,c,TObject]);',
  11899. ' Say([nil,i]);',
  11900. '']);
  11901. ConvertProgram;
  11902. CheckSource('TestArrayOfConst_PassObj',
  11903. LinesToStr([ // statements
  11904. 'rtl.createClass(this, "TObject", null, function () {',
  11905. ' this.$init = function () {',
  11906. ' };',
  11907. ' this.$final = function () {',
  11908. ' };',
  11909. '});',
  11910. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  11911. 'this.Say = function (args) {',
  11912. '};',
  11913. 'this.o = null;',
  11914. 'this.c = null;',
  11915. 'this.i = null;',
  11916. '']),
  11917. LinesToStr([ // $mod.$main
  11918. '$mod.Say(pas.system.VarRecs(',
  11919. ' 7,',
  11920. ' $mod.o,',
  11921. ' 8,',
  11922. ' $mod.c,',
  11923. ' 8,',
  11924. ' $mod.TObject',
  11925. '));',
  11926. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  11927. '']));
  11928. end;
  11929. procedure TTestModule.TestRecord_Empty;
  11930. begin
  11931. StartProgram(false);
  11932. Add([
  11933. 'type',
  11934. ' TRecA = record',
  11935. ' end;',
  11936. 'var a,b: TRecA;',
  11937. 'begin',
  11938. ' if a=b then ;']);
  11939. ConvertProgram;
  11940. CheckSource('TestRecord_Empty',
  11941. LinesToStr([ // statements
  11942. 'rtl.recNewT(this, "TRecA", function () {',
  11943. ' this.$eq = function (b) {',
  11944. ' return true;',
  11945. ' };',
  11946. ' this.$assign = function (s) {',
  11947. ' return this;',
  11948. ' };',
  11949. '});',
  11950. 'this.a = this.TRecA.$new();',
  11951. 'this.b = this.TRecA.$new();',
  11952. '']),
  11953. LinesToStr([ // $mod.$main
  11954. 'if ($mod.a.$eq($mod.b)) ;'
  11955. ]));
  11956. end;
  11957. procedure TTestModule.TestRecord_Var;
  11958. begin
  11959. StartProgram(false);
  11960. Add('type');
  11961. Add(' TRecA = record');
  11962. Add(' Bold: longint;');
  11963. Add(' end;');
  11964. Add('var Rec: TRecA;');
  11965. Add('begin');
  11966. Add(' rec.bold:=123');
  11967. ConvertProgram;
  11968. CheckSource('TestRecord_Var',
  11969. LinesToStr([ // statements
  11970. 'rtl.recNewT(this, "TRecA", function () {',
  11971. ' this.Bold = 0;',
  11972. ' this.$eq = function (b) {',
  11973. ' return this.Bold === b.Bold;',
  11974. ' };',
  11975. ' this.$assign = function (s) {',
  11976. ' this.Bold = s.Bold;',
  11977. ' return this;',
  11978. ' };',
  11979. '});',
  11980. 'this.Rec = this.TRecA.$new();',
  11981. '']),
  11982. LinesToStr([ // $mod.$main
  11983. '$mod.Rec.Bold = 123;'
  11984. ]));
  11985. end;
  11986. procedure TTestModule.TestRecord_VarExternal;
  11987. begin
  11988. StartProgram(false);
  11989. Add([
  11990. '{$modeswitch externalclass}',
  11991. 'type',
  11992. ' TRecA = record',
  11993. ' i: byte;',
  11994. ' length_: longint external name ''length'';',
  11995. ' end;',
  11996. 'var Rec: TRecA;',
  11997. 'begin',
  11998. ' rec.length_ := rec.length_',
  11999. '']);
  12000. ConvertProgram;
  12001. CheckSource('TestRecord_VarExternal',
  12002. LinesToStr([ // statements
  12003. 'rtl.recNewT(this, "TRecA", function () {',
  12004. ' this.i = 0;',
  12005. ' this.$eq = function (b) {',
  12006. ' return (this.i === b.i) && (this.length === b.length);',
  12007. ' };',
  12008. ' this.$assign = function (s) {',
  12009. ' this.i = s.i;',
  12010. ' this.length = s.length;',
  12011. ' return this;',
  12012. ' };',
  12013. '});',
  12014. 'this.Rec = this.TRecA.$new();',
  12015. '']),
  12016. LinesToStr([ // $mod.$main
  12017. '$mod.Rec.length = $mod.Rec.length;'
  12018. ]));
  12019. end;
  12020. procedure TTestModule.TestRecord_WithDo;
  12021. begin
  12022. StartProgram(false);
  12023. Add('type');
  12024. Add(' TRec = record');
  12025. Add(' vI: longint;');
  12026. Add(' end;');
  12027. Add('var');
  12028. Add(' Int: longint;');
  12029. Add(' r: TRec;');
  12030. Add('begin');
  12031. Add(' with r do');
  12032. Add(' int:=vi;');
  12033. Add(' with r do begin');
  12034. Add(' int:=vi;');
  12035. Add(' vi:=int;');
  12036. Add(' end;');
  12037. ConvertProgram;
  12038. CheckSource('TestWithRecordDo',
  12039. LinesToStr([ // statements
  12040. 'rtl.recNewT(this, "TRec", function () {',
  12041. ' this.vI = 0;',
  12042. ' this.$eq = function (b) {',
  12043. ' return this.vI === b.vI;',
  12044. ' };',
  12045. ' this.$assign = function (s) {',
  12046. ' this.vI = s.vI;',
  12047. ' return this;',
  12048. ' };',
  12049. '});',
  12050. 'this.Int = 0;',
  12051. 'this.r = this.TRec.$new();',
  12052. '']),
  12053. LinesToStr([ // $mod.$main
  12054. 'var $with = $mod.r;',
  12055. '$mod.Int = $with.vI;',
  12056. 'var $with1 = $mod.r;',
  12057. '$mod.Int = $with1.vI;',
  12058. '$with1.vI = $mod.Int;'
  12059. ]));
  12060. end;
  12061. procedure TTestModule.TestRecord_Assign;
  12062. begin
  12063. StartProgram(false);
  12064. Add([
  12065. 'type',
  12066. ' TEnum = (red,green);',
  12067. ' TEnums = set of TEnum;',
  12068. ' TSmallRec = record',
  12069. ' N: longint;',
  12070. ' end;',
  12071. ' TBigRec = record',
  12072. ' Int: longint;',
  12073. ' D: double;',
  12074. ' Arr: array of longint;',
  12075. ' Arr2: array[1..2] of longint;',
  12076. ' Small: TSmallRec;',
  12077. ' Enums: TEnums;',
  12078. ' end;',
  12079. 'var',
  12080. ' r, s: TBigRec;',
  12081. 'begin',
  12082. ' r:=s;',
  12083. ' r:=default(TBigRec);',
  12084. ' r:=default(s);',
  12085. '']);
  12086. ConvertProgram;
  12087. CheckSource('TestRecord_Assign',
  12088. LinesToStr([ // statements
  12089. 'this.TEnum = {',
  12090. ' "0": "red",',
  12091. ' red: 0,',
  12092. ' "1": "green",',
  12093. ' green: 1',
  12094. '};',
  12095. 'rtl.recNewT(this, "TSmallRec", function () {',
  12096. ' this.N = 0;',
  12097. ' this.$eq = function (b) {',
  12098. ' return this.N === b.N;',
  12099. ' };',
  12100. ' this.$assign = function (s) {',
  12101. ' this.N = s.N;',
  12102. ' return this;',
  12103. ' };',
  12104. '});',
  12105. 'rtl.recNewT(this, "TBigRec", function () {',
  12106. ' this.Int = 0;',
  12107. ' this.D = 0.0;',
  12108. ' this.$new = function () {',
  12109. ' var r = Object.create(this);',
  12110. ' r.Arr = [];',
  12111. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  12112. ' r.Small = $mod.TSmallRec.$new();',
  12113. ' r.Enums = {};',
  12114. ' return r;',
  12115. ' };',
  12116. ' this.$eq = function (b) {',
  12117. ' 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);',
  12118. ' };',
  12119. ' this.$assign = function (s) {',
  12120. ' this.Int = s.Int;',
  12121. ' this.D = s.D;',
  12122. ' this.Arr = rtl.arrayRef(s.Arr);',
  12123. ' this.Arr2 = s.Arr2.slice(0);',
  12124. ' this.Small.$assign(s.Small);',
  12125. ' this.Enums = rtl.refSet(s.Enums);',
  12126. ' return this;',
  12127. ' };',
  12128. '});',
  12129. 'this.r = this.TBigRec.$new();',
  12130. 'this.s = this.TBigRec.$new();',
  12131. '']),
  12132. LinesToStr([ // $mod.$main
  12133. '$mod.r.$assign($mod.s);',
  12134. '$mod.r.$assign($mod.TBigRec.$new());',
  12135. '$mod.r.$assign($mod.TBigRec.$new());',
  12136. '']));
  12137. end;
  12138. procedure TTestModule.TestRecord_AsParams;
  12139. begin
  12140. StartProgram(false);
  12141. Add([
  12142. 'type',
  12143. ' integer = longint;',
  12144. ' TRecord = record',
  12145. ' i: integer;',
  12146. ' end;',
  12147. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  12148. 'var vL: TRecord;',
  12149. 'begin',
  12150. ' vd:=vd;',
  12151. ' vd.i:=vd.i;',
  12152. ' vl:=vc;',
  12153. ' vv:=vv;',
  12154. ' vv.i:=vv.i;',
  12155. ' U:=vl;',
  12156. ' U:=vd;',
  12157. ' U:=vc;',
  12158. ' U:=vv;',
  12159. ' vl:=TRecord(U);',
  12160. ' vd:=TRecord(U);',
  12161. ' vv:=TRecord(U);',
  12162. ' doit(vd,vd,vd,vd);',
  12163. ' doit(vc,vc,vl,vl);',
  12164. ' doit(vv,vv,vv,vv);',
  12165. ' doit(vl,vl,vl,vl);',
  12166. ' TRecord(U).i:=3;',
  12167. 'end;',
  12168. 'var i: TRecord;',
  12169. 'begin',
  12170. ' doit(i,i,i,i);',
  12171. '']);
  12172. ConvertProgram;
  12173. CheckSource('TestRecord_AsParams',
  12174. LinesToStr([ // statements
  12175. 'rtl.recNewT(this, "TRecord", function () {',
  12176. ' this.i = 0;',
  12177. ' this.$eq = function (b) {',
  12178. ' return this.i === b.i;',
  12179. ' };',
  12180. ' this.$assign = function (s) {',
  12181. ' this.i = s.i;',
  12182. ' return this;',
  12183. ' };',
  12184. '});',
  12185. 'this.DoIt = function (vD, vC, vV, U) {',
  12186. ' var vL = $mod.TRecord.$new();',
  12187. ' vD.$assign(vD);',
  12188. ' vD.i = vD.i;',
  12189. ' vL.$assign(vC);',
  12190. ' vV.$assign(vV);',
  12191. ' vV.i = vV.i;',
  12192. ' U.$assign(vL);',
  12193. ' U.$assign(vD);',
  12194. ' U.$assign(vC);',
  12195. ' U.$assign(vV);',
  12196. ' vL.$assign(U);',
  12197. ' vD.$assign(U);',
  12198. ' vV.$assign(U);',
  12199. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  12200. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  12201. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  12202. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  12203. ' U.i = 3;',
  12204. '};',
  12205. 'this.i = this.TRecord.$new();'
  12206. ]),
  12207. LinesToStr([
  12208. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  12209. '']));
  12210. end;
  12211. procedure TTestModule.TestRecord_ConstRef;
  12212. begin
  12213. StartProgram(false);
  12214. Add([
  12215. 'type TRec = record i: word; end;',
  12216. 'procedure Run(constref a: TRec);',
  12217. 'begin',
  12218. 'end;',
  12219. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  12220. 'var l: TRec;',
  12221. 'begin',
  12222. ' Run(l);',
  12223. ' Run(a);',
  12224. ' Run(b);',
  12225. ' Run(c);',
  12226. ' Run(d);',
  12227. ' Run(e);',
  12228. 'end;',
  12229. 'begin',
  12230. '']);
  12231. ConvertProgram;
  12232. CheckResolverUnexpectedHints();
  12233. CheckSource('TestRecord_ConstRef',
  12234. LinesToStr([ // statements
  12235. 'rtl.recNewT(this, "TRec", function () {',
  12236. ' this.i = 0;',
  12237. ' this.$eq = function (b) {',
  12238. ' return this.i === b.i;',
  12239. ' };',
  12240. ' this.$assign = function (s) {',
  12241. ' this.i = s.i;',
  12242. ' return this;',
  12243. ' };',
  12244. '});',
  12245. 'this.Run = function (a) {',
  12246. '};',
  12247. 'this.Fly = function (a, b, c, d, e) {',
  12248. ' var l = $mod.TRec.$new();',
  12249. ' $mod.Run(l);',
  12250. ' $mod.Run(a);',
  12251. ' $mod.Run(b);',
  12252. ' $mod.Run(c);',
  12253. ' $mod.Run(d);',
  12254. ' $mod.Run(e);',
  12255. '};',
  12256. '']),
  12257. LinesToStr([
  12258. '']));
  12259. end;
  12260. procedure TTestModule.TestRecordElement_AsParams;
  12261. begin
  12262. StartProgram(false);
  12263. Add('type');
  12264. Add(' integer = longint;');
  12265. Add(' TRecord = record');
  12266. Add(' i: integer;');
  12267. Add(' end;');
  12268. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  12269. Add('var vJ: TRecord;');
  12270. Add('begin');
  12271. Add(' doit(vj.i,vj.i,vj.i);');
  12272. Add('end;');
  12273. Add('var r: TRecord;');
  12274. Add('begin');
  12275. Add(' doit(r.i,r.i,r.i);');
  12276. ConvertProgram;
  12277. CheckSource('TestRecordElement_AsParams',
  12278. LinesToStr([ // statements
  12279. 'rtl.recNewT(this, "TRecord", function () {',
  12280. ' this.i = 0;',
  12281. ' this.$eq = function (b) {',
  12282. ' return this.i === b.i;',
  12283. ' };',
  12284. ' this.$assign = function (s) {',
  12285. ' this.i = s.i;',
  12286. ' return this;',
  12287. ' };',
  12288. '});',
  12289. 'this.DoIt = function (vG,vH,vI) {',
  12290. ' var vJ = $mod.TRecord.$new();',
  12291. ' $mod.DoIt(vJ.i, vJ.i, {',
  12292. ' p: vJ,',
  12293. ' get: function () {',
  12294. ' return this.p.i;',
  12295. ' },',
  12296. ' set: function (v) {',
  12297. ' this.p.i = v;',
  12298. ' }',
  12299. ' });',
  12300. '};',
  12301. 'this.r = this.TRecord.$new();'
  12302. ]),
  12303. LinesToStr([
  12304. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  12305. ' p: $mod.r,',
  12306. ' get: function () {',
  12307. ' return this.p.i;',
  12308. ' },',
  12309. ' set: function (v) {',
  12310. ' this.p.i = v;',
  12311. ' }',
  12312. '});'
  12313. ]));
  12314. end;
  12315. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  12316. begin
  12317. StartProgram(false);
  12318. Add('type');
  12319. Add(' integer = longint;');
  12320. Add(' TRecord = record');
  12321. Add(' i: integer;');
  12322. Add(' end;');
  12323. Add('function GetRec(vB: integer = 0): TRecord;');
  12324. Add('begin');
  12325. Add('end;');
  12326. Add('procedure DoIt(vG: integer; const vH: integer);');
  12327. Add('begin');
  12328. Add('end;');
  12329. Add('begin');
  12330. Add(' doit(getrec.i,getrec.i);');
  12331. Add(' doit(getrec().i,getrec().i);');
  12332. Add(' doit(getrec(1).i,getrec(2).i);');
  12333. ConvertProgram;
  12334. CheckSource('TestRecordElementFromFuncResult_AsParams',
  12335. LinesToStr([ // statements
  12336. 'rtl.recNewT(this, "TRecord", function () {',
  12337. ' this.i = 0;',
  12338. ' this.$eq = function (b) {',
  12339. ' return this.i === b.i;',
  12340. ' };',
  12341. ' this.$assign = function (s) {',
  12342. ' this.i = s.i;',
  12343. ' return this;',
  12344. ' };',
  12345. '});',
  12346. 'this.GetRec = function (vB) {',
  12347. ' var Result = $mod.TRecord.$new();',
  12348. ' return Result;',
  12349. '};',
  12350. 'this.DoIt = function (vG, vH) {',
  12351. '};',
  12352. '']),
  12353. LinesToStr([
  12354. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  12355. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  12356. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  12357. '']));
  12358. end;
  12359. procedure TTestModule.TestRecordElementFromWith_AsParams;
  12360. begin
  12361. StartProgram(false);
  12362. Add('type');
  12363. Add(' integer = longint;');
  12364. Add(' TRecord = record');
  12365. Add(' i: integer;');
  12366. Add(' end;');
  12367. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  12368. Add('begin');
  12369. Add('end;');
  12370. Add('var r: trecord;');
  12371. Add('begin');
  12372. Add(' with r do ');
  12373. Add(' doit(i,i,i);');
  12374. ConvertProgram;
  12375. CheckSource('TestRecordElementFromWith_AsParams',
  12376. LinesToStr([ // statements
  12377. 'rtl.recNewT(this, "TRecord", function () {',
  12378. ' this.i = 0;',
  12379. ' this.$eq = function (b) {',
  12380. ' return this.i === b.i;',
  12381. ' };',
  12382. ' this.$assign = function (s) {',
  12383. ' this.i = s.i;',
  12384. ' return this;',
  12385. ' };',
  12386. '});',
  12387. 'this.DoIt = function (vG,vH,vI) {',
  12388. '};',
  12389. 'this.r = this.TRecord.$new();'
  12390. ]),
  12391. LinesToStr([
  12392. 'var $with = $mod.r;',
  12393. '$mod.DoIt($with.i,$with.i,{',
  12394. ' p: $with,',
  12395. ' get: function () {',
  12396. ' return this.p.i;',
  12397. ' },',
  12398. ' set: function (v) {',
  12399. ' this.p.i = v;',
  12400. ' }',
  12401. '});',
  12402. '']));
  12403. end;
  12404. procedure TTestModule.TestRecord_Equal;
  12405. begin
  12406. StartProgram(false);
  12407. Add('type');
  12408. Add(' integer = longint;');
  12409. Add(' TFlag = (red,blue);');
  12410. Add(' TFlags = set of TFlag;');
  12411. Add(' TProc = procedure;');
  12412. Add(' TRecord = record');
  12413. Add(' i: integer;');
  12414. Add(' Event: TProc;');
  12415. Add(' f: TFlags;');
  12416. Add(' end;');
  12417. Add(' TNested = record');
  12418. Add(' r: TRecord;');
  12419. Add(' end;');
  12420. Add('var');
  12421. Add(' b: boolean;');
  12422. Add(' r,s: trecord;');
  12423. Add('begin');
  12424. Add(' b:=r=s;');
  12425. Add(' b:=r<>s;');
  12426. ConvertProgram;
  12427. CheckSource('TestRecord_Equal',
  12428. LinesToStr([ // statements
  12429. 'this.TFlag = {',
  12430. ' "0": "red",',
  12431. ' red: 0,',
  12432. ' "1": "blue",',
  12433. ' blue: 1',
  12434. '};',
  12435. 'rtl.recNewT(this, "TRecord", function () {',
  12436. ' this.i = 0;',
  12437. ' this.Event = null;',
  12438. ' this.$new = function () {',
  12439. ' var r = Object.create(this);',
  12440. ' r.f = {};',
  12441. ' return r;',
  12442. ' };',
  12443. ' this.$eq = function (b) {',
  12444. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  12445. ' };',
  12446. ' this.$assign = function (s) {',
  12447. ' this.i = s.i;',
  12448. ' this.Event = s.Event;',
  12449. ' this.f = rtl.refSet(s.f);',
  12450. ' return this;',
  12451. ' };',
  12452. '});',
  12453. 'rtl.recNewT(this, "TNested", function () {',
  12454. ' this.$new = function () {',
  12455. ' var r = Object.create(this);',
  12456. ' r.r = $mod.TRecord.$new();',
  12457. ' return r;',
  12458. ' };',
  12459. ' this.$eq = function (b) {',
  12460. ' return this.r.$eq(b.r);',
  12461. ' };',
  12462. ' this.$assign = function (s) {',
  12463. ' this.r.$assign(s.r);',
  12464. ' return this;',
  12465. ' };',
  12466. '});',
  12467. 'this.b = false;',
  12468. 'this.r = this.TRecord.$new();',
  12469. 'this.s = this.TRecord.$new();',
  12470. '']),
  12471. LinesToStr([
  12472. '$mod.b = $mod.r.$eq($mod.s);',
  12473. '$mod.b = !$mod.r.$eq($mod.s);',
  12474. '']));
  12475. end;
  12476. procedure TTestModule.TestRecord_JSValue;
  12477. begin
  12478. StartProgram(false);
  12479. Add([
  12480. 'type',
  12481. ' TRecord = record',
  12482. ' i: longint;',
  12483. ' end;',
  12484. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  12485. 'begin',
  12486. 'end;',
  12487. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  12488. 'begin',
  12489. ' if jsvalue(d) then ;',
  12490. ' if jsvalue(c) then ;',
  12491. ' if jsvalue(v) then ;',
  12492. 'end;',
  12493. 'var',
  12494. ' Jv: jsvalue;',
  12495. ' Rec: trecord;',
  12496. 'begin',
  12497. ' rec:=trecord(jv);',
  12498. ' jv:=rec;',
  12499. ' Fly(rec,rec);',
  12500. ' Fly(@rec,@rec);',
  12501. ' if jsvalue(Rec) then ;',
  12502. ' Run(trecord(jv),trecord(jv),rec);',
  12503. '']);
  12504. ConvertProgram;
  12505. CheckSource('TestRecord_JSValue',
  12506. LinesToStr([ // statements
  12507. 'rtl.recNewT(this, "TRecord", function () {',
  12508. ' this.i = 0;',
  12509. ' this.$eq = function (b) {',
  12510. ' return this.i === b.i;',
  12511. ' };',
  12512. ' this.$assign = function (s) {',
  12513. ' this.i = s.i;',
  12514. ' return this;',
  12515. ' };',
  12516. '});',
  12517. 'this.Fly = function (d, c) {',
  12518. '};',
  12519. 'this.Run = function (d, c, v) {',
  12520. ' if (d) ;',
  12521. ' if (c) ;',
  12522. ' if (v) ;',
  12523. '};',
  12524. 'this.Jv = undefined;',
  12525. 'this.Rec = this.TRecord.$new();',
  12526. '']),
  12527. LinesToStr([
  12528. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  12529. '$mod.Jv = $mod.Rec;',
  12530. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  12531. '$mod.Fly($mod.Rec, $mod.Rec);',
  12532. 'if ($mod.Rec) ;',
  12533. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  12534. '']));
  12535. end;
  12536. procedure TTestModule.TestRecord_VariantFail;
  12537. begin
  12538. StartProgram(false);
  12539. Add([
  12540. 'type',
  12541. ' TRec = record',
  12542. ' case word of',
  12543. ' 0: (b0, b1: Byte);',
  12544. ' 1: (i: word);',
  12545. ' end;',
  12546. 'begin']);
  12547. SetExpectedPasResolverError('Not supported: variant record',
  12548. nNotSupportedX);
  12549. ConvertProgram;
  12550. end;
  12551. procedure TTestModule.TestRecord_FieldArray;
  12552. begin
  12553. StartProgram(false);
  12554. Add([
  12555. 'type',
  12556. ' TArrInt = array[3..4] of longint;',
  12557. ' TArrArrInt = array[3..4] of longint;',
  12558. ' TRec = record',
  12559. ' a: array of longint;',
  12560. ' s: array[1..2] of longint;',
  12561. ' m: array[1..2,3..4] of longint;',
  12562. ' o: TArrArrInt;',
  12563. ' end;',
  12564. 'begin']);
  12565. ConvertProgram;
  12566. CheckSource('TestRecord_FieldArray',
  12567. LinesToStr([ // statements
  12568. 'rtl.recNewT(this, "TRec", function () {',
  12569. ' this.m$a$clone = function (a) {',
  12570. ' var b = [];',
  12571. ' b.length = 2;',
  12572. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12573. ' return b;',
  12574. ' };',
  12575. ' this.$new = function () {',
  12576. ' var r = Object.create(this);',
  12577. ' r.a = [];',
  12578. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12579. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12580. ' r.o = rtl.arraySetLength(null, 0, 2);',
  12581. ' return r;',
  12582. ' };',
  12583. ' this.$eq = function (b) {',
  12584. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  12585. ' };',
  12586. ' this.$assign = function (s) {',
  12587. ' this.a = rtl.arrayRef(s.a);',
  12588. ' this.s = s.s.slice(0);',
  12589. ' this.m = this.m$a$clone(s.m);',
  12590. ' this.o = s.o.slice(0);',
  12591. ' return this;',
  12592. ' };',
  12593. '});',
  12594. '']),
  12595. LinesToStr([ // $mod.$main
  12596. '']));
  12597. end;
  12598. procedure TTestModule.TestRecord_Const;
  12599. begin
  12600. StartProgram(false);
  12601. Add([
  12602. 'type',
  12603. ' TArrInt = array[3..4] of longint;',
  12604. ' TPoint = record x,y: longint; end;',
  12605. ' TRec = record',
  12606. ' i: longint;',
  12607. ' a: array of longint;',
  12608. ' s: array[1..2] of longint;',
  12609. ' m: array[1..2,3..4] of longint;',
  12610. ' p: TPoint;',
  12611. ' end;',
  12612. ' TPoints = array of TPoint;',
  12613. 'const',
  12614. ' r: TRec = (',
  12615. ' i:1;',
  12616. ' a:(2,3);',
  12617. ' s:(4,5);',
  12618. ' m:( (11,12), (13,14) );',
  12619. ' p: (x:21; y:22)',
  12620. ' );',
  12621. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  12622. 'begin']);
  12623. ConvertProgram;
  12624. CheckSource('TestRecord_Const',
  12625. LinesToStr([ // statements
  12626. 'rtl.recNewT(this, "TPoint", function () {',
  12627. ' this.x = 0;',
  12628. ' this.y = 0;',
  12629. ' this.$eq = function (b) {',
  12630. ' return (this.x === b.x) && (this.y === b.y);',
  12631. ' };',
  12632. ' this.$assign = function (s) {',
  12633. ' this.x = s.x;',
  12634. ' this.y = s.y;',
  12635. ' return this;',
  12636. ' };',
  12637. '});',
  12638. 'rtl.recNewT(this, "TRec", function () {',
  12639. ' this.i = 0;',
  12640. ' this.m$a$clone = function (a) {',
  12641. ' var b = [];',
  12642. ' b.length = 2;',
  12643. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12644. ' return b;',
  12645. ' };',
  12646. ' this.$new = function () {',
  12647. ' var r = Object.create(this);',
  12648. ' r.a = [];',
  12649. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12650. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12651. ' r.p = $mod.TPoint.$new();',
  12652. ' return r;',
  12653. ' };',
  12654. ' this.$eq = function (b) {',
  12655. ' 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);',
  12656. ' };',
  12657. ' this.$assign = function (s) {',
  12658. ' this.i = s.i;',
  12659. ' this.a = rtl.arrayRef(s.a);',
  12660. ' this.s = s.s.slice(0);',
  12661. ' this.m = this.m$a$clone(s.m);',
  12662. ' this.p.$assign(s.p);',
  12663. ' return this;',
  12664. ' };',
  12665. '});',
  12666. 'this.r = this.TRec.$clone({',
  12667. ' i: 1,',
  12668. ' a: [2, 3],',
  12669. ' s: [4, 5],',
  12670. ' m: [[11, 12], [13, 14]],',
  12671. ' p: this.TPoint.$clone({',
  12672. ' x: 21,',
  12673. ' y: 22',
  12674. ' })',
  12675. '});',
  12676. 'this.p = [this.TPoint.$clone({',
  12677. ' x: 1,',
  12678. ' y: 2',
  12679. '}), this.TPoint.$clone({',
  12680. ' x: 3,',
  12681. ' y: 4',
  12682. '})];',
  12683. '']),
  12684. LinesToStr([ // $mod.$main
  12685. '']));
  12686. end;
  12687. procedure TTestModule.TestRecord_TypecastFail;
  12688. begin
  12689. StartProgram(false);
  12690. Add([
  12691. 'type',
  12692. ' TPoint = record x,y: longint; end;',
  12693. ' TRec = record l: longint end;',
  12694. 'var p: TPoint;',
  12695. 'begin',
  12696. ' if TRec(p).l=2 then ;']);
  12697. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  12698. nIllegalTypeConversionTo);
  12699. ConvertProgram;
  12700. end;
  12701. procedure TTestModule.TestRecord_InFunction;
  12702. begin
  12703. StartProgram(false);
  12704. Add([
  12705. 'var TPoint: longint = 3;',
  12706. 'procedure DoIt;',
  12707. 'type',
  12708. ' TPoint = record x,y: longint; end;',
  12709. ' TPoints = array of TPoint;',
  12710. 'var',
  12711. ' r: TPoint;',
  12712. ' p: TPoints;',
  12713. 'begin',
  12714. ' SetLength(p,2);',
  12715. 'end;',
  12716. 'begin']);
  12717. ConvertProgram;
  12718. CheckSource('TestRecord_InFunction',
  12719. LinesToStr([ // statements
  12720. 'this.TPoint = 3;',
  12721. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  12722. ' this.x = 0;',
  12723. ' this.y = 0;',
  12724. ' this.$eq = function (b) {',
  12725. ' return (this.x === b.x) && (this.y === b.y);',
  12726. ' };',
  12727. ' this.$assign = function (s) {',
  12728. ' this.x = s.x;',
  12729. ' this.y = s.y;',
  12730. ' return this;',
  12731. ' };',
  12732. '});',
  12733. 'this.DoIt = function () {',
  12734. ' var r = TPoint$1.$new();',
  12735. ' var p = [];',
  12736. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  12737. '};',
  12738. '']),
  12739. LinesToStr([ // $mod.$main
  12740. '']));
  12741. end;
  12742. procedure TTestModule.TestRecord_ArrayConstMultiline;
  12743. begin
  12744. StartProgram(false);
  12745. Add([
  12746. '{$mode delphi}',
  12747. 'type',
  12748. ' TBird = record Wing: string; end;',
  12749. 'const',
  12750. ' Birds: array[1..2] of TBird = (',
  12751. ' (Wing: ''''''',
  12752. ' First',
  12753. ' Second',
  12754. ' Third',
  12755. ' ''''''),',
  12756. ' (Wing: ''''''',
  12757. ' Value:=''Im in quotes''; ',
  12758. ' '''''')',
  12759. ' );',
  12760. 'begin']);
  12761. ConvertProgram;
  12762. CheckSource('TestRecord_ArrayConstMultiline',
  12763. LinesToStr([ // statements
  12764. 'rtl.recNewT(this, "TBird", function () {',
  12765. ' this.Wing = "";',
  12766. ' this.$eq = function (b) {',
  12767. ' return this.Wing === b.Wing;',
  12768. ' };',
  12769. ' this.$assign = function (s) {',
  12770. ' this.Wing = s.Wing;',
  12771. ' return this;',
  12772. ' };',
  12773. '});',
  12774. 'this.Birds$a$clone = function (a) {',
  12775. ' var b = [];',
  12776. ' b.length = 2;',
  12777. ' for (var c = 0; c < 2; c++) b[c] = $mod.TBird.$clone(a[c]);',
  12778. ' return b;',
  12779. '};',
  12780. 'this.Birds = [this.TBird.$clone({',
  12781. ' Wing: " First\n Second\n Third"',
  12782. '}), this.TBird.$clone({',
  12783. ' Wing: " Value:=''Im in quotes''; "',
  12784. '})];',
  12785. '']),
  12786. LinesToStr([ // $mod.$main
  12787. '']));
  12788. end;
  12789. procedure TTestModule.TestRecordAnonym_Field;
  12790. begin
  12791. StartProgram(false);
  12792. Add(['',
  12793. 'var Rec: record',
  12794. ' Bold: longint;',
  12795. ' end;',
  12796. 'begin',
  12797. ' rec.bold:=123;',
  12798. ' rec.bold:=rec.bold+7;',
  12799. '']);
  12800. ConvertProgram;
  12801. CheckSource('TestRecordAnonym_Field',
  12802. LinesToStr([ // statements
  12803. 'rtl.recNewT(this, "Rec$a", function () {',
  12804. ' this.Bold = 0;',
  12805. ' this.$eq = function (b) {',
  12806. ' return this.Bold === b.Bold;',
  12807. ' };',
  12808. ' this.$assign = function (s) {',
  12809. ' this.Bold = s.Bold;',
  12810. ' return this;',
  12811. ' };',
  12812. '});',
  12813. 'this.Rec = this.Rec$a.$new();',
  12814. '']),
  12815. LinesToStr([ // $mod.$main
  12816. '$mod.Rec.Bold = 123;',
  12817. '$mod.Rec.Bold = $mod.Rec.Bold + 7;',
  12818. '']));
  12819. end;
  12820. procedure TTestModule.TestRecordAnonym_Assign;
  12821. begin
  12822. StartProgram(false);
  12823. Add(['',
  12824. 'var S,T: record',
  12825. ' Bold: longint;',
  12826. ' end;',
  12827. ' b: boolean;',
  12828. 'begin',
  12829. ' S:=T;',
  12830. ' b:=s=t;',
  12831. '']);
  12832. ConvertProgram;
  12833. CheckSource('TestRecordAnonym_Assign',
  12834. LinesToStr([ // statements
  12835. 'rtl.recNewT(this, "T$a", function () {',
  12836. ' this.Bold = 0;',
  12837. ' this.$eq = function (b) {',
  12838. ' return this.Bold === b.Bold;',
  12839. ' };',
  12840. ' this.$assign = function (s) {',
  12841. ' this.Bold = s.Bold;',
  12842. ' return this;',
  12843. ' };',
  12844. '});',
  12845. 'this.S = this.T$a.$new();',
  12846. 'this.T = this.T$a.$new();',
  12847. 'this.b = false;',
  12848. '']),
  12849. LinesToStr([ // $mod.$main
  12850. '$mod.S.$assign($mod.T);',
  12851. '$mod.b = $mod.S.$eq($mod.T);',
  12852. '']));
  12853. end;
  12854. procedure TTestModule.TestRecordAnonym_Nested;
  12855. begin
  12856. StartProgram(false);
  12857. Add(['',
  12858. 'var S,T: record',
  12859. ' Bold: longint;',
  12860. ' Sub: record',
  12861. ' Color: word;',
  12862. ' end;',
  12863. ' end;',
  12864. ' b: boolean;',
  12865. 'begin',
  12866. ' S:=T;',
  12867. ' S.Sub:=T.Sub;',
  12868. ' S.Sub.Color:=T.Sub.Color+3;',
  12869. ' b:=s=t;',
  12870. ' b:=s.Sub=t.Sub;',
  12871. '']);
  12872. ConvertProgram;
  12873. CheckSource('TestRecordAnonym_Nested',
  12874. LinesToStr([ // statements
  12875. 'rtl.recNewT(this, "T$a", function () {',
  12876. ' this.Bold = 0;',
  12877. ' rtl.recNewT(this, "Sub$a", function () {',
  12878. ' this.Color = 0;',
  12879. ' this.$eq = function (b) {',
  12880. ' return this.Color === b.Color;',
  12881. ' };',
  12882. ' this.$assign = function (s) {',
  12883. ' this.Color = s.Color;',
  12884. ' return this;',
  12885. ' };',
  12886. ' });',
  12887. ' this.$new = function () {',
  12888. ' var r = Object.create(this);',
  12889. ' r.Sub = this.Sub$a.$new();',
  12890. ' return r;',
  12891. ' };',
  12892. ' this.$eq = function (b) {',
  12893. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12894. ' };',
  12895. ' this.$assign = function (s) {',
  12896. ' this.Bold = s.Bold;',
  12897. ' this.Sub.$assign(s.Sub);',
  12898. ' return this;',
  12899. ' };',
  12900. '}, true);',
  12901. 'this.S = this.T$a.$new();',
  12902. 'this.T = this.T$a.$new();',
  12903. 'this.b = false;',
  12904. '']),
  12905. LinesToStr([ // $mod.$main
  12906. '$mod.S.$assign($mod.T);',
  12907. '$mod.S.Sub.$assign($mod.T.Sub);',
  12908. '$mod.S.Sub.Color = $mod.T.Sub.Color + 3;',
  12909. '$mod.b = $mod.S.$eq($mod.T);',
  12910. '$mod.b = $mod.S.Sub.$eq($mod.T.Sub);',
  12911. '']));
  12912. end;
  12913. procedure TTestModule.TestRecordAnonym_Const;
  12914. begin
  12915. StartProgram(false);
  12916. Add(['',
  12917. 'var T: record',
  12918. ' Bold: longint;',
  12919. ' Sub: record',
  12920. ' Color: word;',
  12921. ' end;',
  12922. ' end = (Bold: 2; Sub: (Color: 3));',
  12923. 'begin',
  12924. '']);
  12925. ConvertProgram;
  12926. CheckSource('TestRecordAnonym_Const',
  12927. LinesToStr([ // statements
  12928. 'rtl.recNewT(this, "T$a", function () {',
  12929. ' this.Bold = 0;',
  12930. ' rtl.recNewT(this, "Sub$a", function () {',
  12931. ' this.Color = 0;',
  12932. ' this.$eq = function (b) {',
  12933. ' return this.Color === b.Color;',
  12934. ' };',
  12935. ' this.$assign = function (s) {',
  12936. ' this.Color = s.Color;',
  12937. ' return this;',
  12938. ' };',
  12939. ' });',
  12940. ' this.$new = function () {',
  12941. ' var r = Object.create(this);',
  12942. ' r.Sub = this.Sub$a.$new();',
  12943. ' return r;',
  12944. ' };',
  12945. ' this.$eq = function (b) {',
  12946. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12947. ' };',
  12948. ' this.$assign = function (s) {',
  12949. ' this.Bold = s.Bold;',
  12950. ' this.Sub.$assign(s.Sub);',
  12951. ' return this;',
  12952. ' };',
  12953. '}, true);',
  12954. 'this.T = this.T$a.$clone({',
  12955. ' Bold: 2,',
  12956. ' Sub: this.T$a.Sub$a.$clone({',
  12957. ' Color: 3',
  12958. ' })',
  12959. '});',
  12960. '']),
  12961. LinesToStr([ // $mod.$main
  12962. '']));
  12963. end;
  12964. procedure TTestModule.TestRecordAnonym_InFunction;
  12965. begin
  12966. StartProgram(false);
  12967. Add(['',
  12968. 'procedure Fly;',
  12969. 'var T: record',
  12970. ' Bold: longint;',
  12971. ' Sub: record',
  12972. ' Color: word;',
  12973. ' end;',
  12974. ' end = (Bold: 2; Sub: (Color: 3));',
  12975. 'begin',
  12976. 'end;',
  12977. 'begin',
  12978. '']);
  12979. ConvertProgram;
  12980. CheckSource('TestRecordAnonym_InFunction',
  12981. LinesToStr([ // statements
  12982. 'var T$a = rtl.recNewT(null, "", function () {',
  12983. ' this.Bold = 0;',
  12984. ' rtl.recNewT(this, "Sub$a", function () {',
  12985. ' this.Color = 0;',
  12986. ' this.$eq = function (b) {',
  12987. ' return this.Color === b.Color;',
  12988. ' };',
  12989. ' this.$assign = function (s) {',
  12990. ' this.Color = s.Color;',
  12991. ' return this;',
  12992. ' };',
  12993. ' });',
  12994. ' this.$new = function () {',
  12995. ' var r = Object.create(this);',
  12996. ' r.Sub = this.Sub$a.$new();',
  12997. ' return r;',
  12998. ' };',
  12999. ' this.$eq = function (b) {',
  13000. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  13001. ' };',
  13002. ' this.$assign = function (s) {',
  13003. ' this.Bold = s.Bold;',
  13004. ' this.Sub.$assign(s.Sub);',
  13005. ' return this;',
  13006. ' };',
  13007. '}, true);',
  13008. 'this.Fly = function () {',
  13009. ' var T = T$a.$clone({',
  13010. ' Bold: 2,',
  13011. ' Sub: T$a.Sub$a.$clone({',
  13012. ' Color: 3',
  13013. ' })',
  13014. ' });',
  13015. '};',
  13016. '']),
  13017. LinesToStr([ // $mod.$main
  13018. '']));
  13019. end;
  13020. procedure TTestModule.TestAdvRecord_Function;
  13021. begin
  13022. StartProgram(false);
  13023. Parser.Options:=Parser.Options+[po_cassignments];
  13024. Add([
  13025. '{$modeswitch AdvancedRecords}',
  13026. 'type',
  13027. ' TPoint = record',
  13028. ' x,y: word;',
  13029. ' function Add(const apt: TPoint): TPoint;',
  13030. ' end;',
  13031. 'function TPoint.Add(const apt: TPoint): TPoint;',
  13032. 'begin',
  13033. ' Result:=Self;',
  13034. ' Result.x+=apt.x;',
  13035. ' Result.y:=Result.y+apt.y;',
  13036. ' Self:=apt;',
  13037. 'end;',
  13038. 'var p,q: TPoint;',
  13039. 'begin',
  13040. ' p.add(q);',
  13041. ' p:=default(TPoint);',
  13042. ' p:=q;',
  13043. '']);
  13044. ConvertProgram;
  13045. CheckSource('TestAdvRecord_Function',
  13046. LinesToStr([ // statements
  13047. 'rtl.recNewT(this, "TPoint", function () {',
  13048. ' this.x = 0;',
  13049. ' this.y = 0;',
  13050. ' this.$eq = function (b) {',
  13051. ' return (this.x === b.x) && (this.y === b.y);',
  13052. ' };',
  13053. ' this.$assign = function (s) {',
  13054. ' this.x = s.x;',
  13055. ' this.y = s.y;',
  13056. ' return this;',
  13057. ' };',
  13058. ' this.Add = function (apt) {',
  13059. ' var Result = $mod.TPoint.$new();',
  13060. ' Result.$assign(this);',
  13061. ' Result.x += apt.x;',
  13062. ' Result.y = Result.y + apt.y;',
  13063. ' this.$assign(apt);',
  13064. ' return Result;',
  13065. ' };',
  13066. '});',
  13067. 'this.p = this.TPoint.$new();',
  13068. 'this.q = this.TPoint.$new();',
  13069. '']),
  13070. LinesToStr([ // $mod.$main
  13071. '$mod.p.Add($mod.q);',
  13072. '$mod.p.$assign($mod.TPoint.$new());',
  13073. '$mod.p.$assign($mod.q);',
  13074. '']));
  13075. end;
  13076. procedure TTestModule.TestAdvRecord_Property;
  13077. begin
  13078. StartProgram(false);
  13079. Add([
  13080. '{$modeswitch AdvancedRecords}',
  13081. 'type',
  13082. ' TPoint = record',
  13083. ' x,y: word;',
  13084. ' strict private',
  13085. ' function GetSize: longword;',
  13086. ' procedure SetSize(Value: longword);',
  13087. ' public',
  13088. ' property Size: longword read GetSize write SetSize;',
  13089. ' property Left: word read x write y;',
  13090. ' end;',
  13091. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  13092. 'function TPoint.GetSize: longword;',
  13093. 'begin',
  13094. ' x:=y;',
  13095. ' Size:=Size;',
  13096. ' Left:=Left;',
  13097. 'end;',
  13098. 'procedure TPoint.SetSize(Value: longword);',
  13099. 'begin',
  13100. 'end;',
  13101. 'var p,q: TPoint;',
  13102. 'begin',
  13103. ' p.Size:=q.Size;',
  13104. ' p.Left:=q.Left;',
  13105. '']);
  13106. ConvertProgram;
  13107. CheckSource('TestAdvRecord_Property',
  13108. LinesToStr([ // statements
  13109. 'rtl.recNewT(this, "TPoint", function () {',
  13110. ' this.x = 0;',
  13111. ' this.y = 0;',
  13112. ' this.$eq = function (b) {',
  13113. ' return (this.x === b.x) && (this.y === b.y);',
  13114. ' };',
  13115. ' this.$assign = function (s) {',
  13116. ' this.x = s.x;',
  13117. ' this.y = s.y;',
  13118. ' return this;',
  13119. ' };',
  13120. ' this.GetSize = function () {',
  13121. ' var Result = 0;',
  13122. ' this.x = this.y;',
  13123. ' this.SetSize(this.GetSize());',
  13124. ' this.y = this.x;',
  13125. ' return Result;',
  13126. ' };',
  13127. ' this.SetSize = function (Value) {',
  13128. ' };',
  13129. '});',
  13130. 'this.SetSize = function (Value) {',
  13131. '};',
  13132. 'this.p = this.TPoint.$new();',
  13133. 'this.q = this.TPoint.$new();',
  13134. '']),
  13135. LinesToStr([ // $mod.$main
  13136. '$mod.p.SetSize($mod.q.GetSize());',
  13137. '$mod.p.y = $mod.q.x;',
  13138. '']));
  13139. end;
  13140. procedure TTestModule.TestAdvRecord_PropertyDefault;
  13141. begin
  13142. StartProgram(false);
  13143. Add([
  13144. '{$modeswitch AdvancedRecords}',
  13145. 'type',
  13146. ' TPoint = record',
  13147. ' strict private',
  13148. ' function GetItems(Index: word): word;',
  13149. ' procedure SetItems(Index: word; Value: word);',
  13150. ' public',
  13151. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  13152. ' end;',
  13153. 'function TPoint.GetItems(Index: word): word;',
  13154. 'begin',
  13155. ' Items[index]:=Items[index];',
  13156. ' self.Items[index]:=self.Items[index];',
  13157. 'end;',
  13158. 'procedure TPoint.SetItems(Index: word; Value: word);',
  13159. 'begin',
  13160. 'end;',
  13161. 'var p: TPoint;',
  13162. 'begin',
  13163. ' p[1]:=p[2];',
  13164. ' p.Items[3]:=p.Items[4];',
  13165. '']);
  13166. ConvertProgram;
  13167. CheckSource('TestAdvRecord_PropertyDefault',
  13168. LinesToStr([ // statements
  13169. 'rtl.recNewT(this, "TPoint", function () {',
  13170. ' this.$eq = function (b) {',
  13171. ' return true;',
  13172. ' };',
  13173. ' this.$assign = function (s) {',
  13174. ' return this;',
  13175. ' };',
  13176. ' this.GetItems = function (Index) {',
  13177. ' var Result = 0;',
  13178. ' this.SetItems(Index, this.GetItems(Index));',
  13179. ' this.SetItems(Index, this.GetItems(Index));',
  13180. ' return Result;',
  13181. ' };',
  13182. ' this.SetItems = function (Index, Value) {',
  13183. ' };',
  13184. '});',
  13185. 'this.p = this.TPoint.$new();',
  13186. '']),
  13187. LinesToStr([ // $mod.$main
  13188. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  13189. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  13190. '']));
  13191. end;
  13192. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  13193. begin
  13194. StartProgram(false);
  13195. Add([
  13196. '{$modeswitch AdvancedRecords}',
  13197. 'type',
  13198. ' TRec = record',
  13199. ' class var',
  13200. ' Fx: longint;',
  13201. ' Fy: longint;',
  13202. ' class function GetInt: longint; static;',
  13203. ' class procedure SetInt(Value: longint); static;',
  13204. ' class procedure DoIt; static;',
  13205. ' class property IntA: longint read Fx write Fy;',
  13206. ' class property IntB: longint read GetInt write SetInt;',
  13207. ' end;',
  13208. 'class function trec.getint: longint;',
  13209. 'begin',
  13210. ' result:=fx;',
  13211. 'end;',
  13212. 'class procedure trec.setint(value: longint);',
  13213. 'begin',
  13214. 'end;',
  13215. 'class procedure trec.doit;',
  13216. 'begin',
  13217. ' IntA:=IntA+1;',
  13218. ' IntB:=IntB+1;',
  13219. 'end;',
  13220. 'var r: trec;',
  13221. 'begin',
  13222. ' trec.inta:=trec.inta+1;',
  13223. ' if trec.intb=2 then;',
  13224. ' trec.intb:=trec.intb+2;',
  13225. ' trec.setint(trec.inta);',
  13226. ' r.inta:=r.inta+1;',
  13227. ' if r.intb=2 then;',
  13228. ' r.intb:=r.intb+2;',
  13229. ' r.setint(r.inta);']);
  13230. ConvertProgram;
  13231. CheckSource('TestAdvRecord_Property_ClassMethod',
  13232. LinesToStr([ // statements
  13233. 'rtl.recNewT(this, "TRec", function () {',
  13234. ' this.Fx = 0;',
  13235. ' this.Fy = 0;',
  13236. ' this.$eq = function (b) {',
  13237. ' return true;',
  13238. ' };',
  13239. ' this.$assign = function (s) {',
  13240. ' return this;',
  13241. ' };',
  13242. ' this.GetInt = function () {',
  13243. ' var Result = 0;',
  13244. ' Result = $mod.TRec.Fx;',
  13245. ' return Result;',
  13246. ' };',
  13247. ' this.SetInt = function (Value) {',
  13248. ' };',
  13249. ' this.DoIt = function () {',
  13250. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  13251. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  13252. ' };',
  13253. '}, true);',
  13254. 'this.r = this.TRec.$new();',
  13255. '']),
  13256. LinesToStr([ // $mod.$main
  13257. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  13258. 'if ($mod.TRec.GetInt() === 2) ;',
  13259. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  13260. '$mod.TRec.SetInt($mod.TRec.Fx);',
  13261. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  13262. 'if ($mod.TRec.GetInt() === 2) ;',
  13263. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  13264. '$mod.TRec.SetInt($mod.r.Fx);',
  13265. '']));
  13266. end;
  13267. procedure TTestModule.TestAdvRecord_Const;
  13268. begin
  13269. StartProgram(false);
  13270. Add([
  13271. '{$modeswitch AdvancedRecords}',
  13272. 'type',
  13273. ' TArrInt = array[3..4] of longint;',
  13274. ' TPoint = record',
  13275. ' x,y: longint;',
  13276. ' class var Count: nativeint;',
  13277. ' end;',
  13278. ' TRec = record',
  13279. ' i: longint;',
  13280. ' a: array of longint;',
  13281. ' s: array[1..2] of longint;',
  13282. ' m: array[1..2,3..4] of longint;',
  13283. ' p: TPoint;',
  13284. ' end;',
  13285. ' TPoints = array of TPoint;',
  13286. 'const',
  13287. ' r: TRec = (',
  13288. ' i:1;',
  13289. ' a:(2,3);',
  13290. ' s:(4,5);',
  13291. ' m:( (11,12), (13,14) );',
  13292. ' p: (x:21)',
  13293. ' );',
  13294. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  13295. 'begin']);
  13296. ConvertProgram;
  13297. CheckSource('TestAdvRecord_Const',
  13298. LinesToStr([ // statements
  13299. 'rtl.recNewT(this, "TPoint", function () {',
  13300. ' this.x = 0;',
  13301. ' this.y = 0;',
  13302. ' this.Count = 0;',
  13303. ' this.$eq = function (b) {',
  13304. ' return (this.x === b.x) && (this.y === b.y);',
  13305. ' };',
  13306. ' this.$assign = function (s) {',
  13307. ' this.x = s.x;',
  13308. ' this.y = s.y;',
  13309. ' return this;',
  13310. ' };',
  13311. '}, true);',
  13312. 'rtl.recNewT(this, "TRec", function () {',
  13313. ' this.i = 0;',
  13314. ' this.m$a$clone = function (a) {',
  13315. ' var b = [];',
  13316. ' b.length = 2;',
  13317. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  13318. ' return b;',
  13319. ' };',
  13320. ' this.$new = function () {',
  13321. ' var r = Object.create(this);',
  13322. ' r.a = [];',
  13323. ' r.s = rtl.arraySetLength(null, 0, 2);',
  13324. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  13325. ' r.p = $mod.TPoint.$new();',
  13326. ' return r;',
  13327. ' };',
  13328. ' this.$eq = function (b) {',
  13329. ' 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);',
  13330. ' };',
  13331. ' this.$assign = function (s) {',
  13332. ' this.i = s.i;',
  13333. ' this.a = rtl.arrayRef(s.a);',
  13334. ' this.s = s.s.slice(0);',
  13335. ' this.m = this.m$a$clone(s.m);',
  13336. ' this.p.$assign(s.p);',
  13337. ' return this;',
  13338. ' };',
  13339. '});',
  13340. 'this.r = this.TRec.$clone({',
  13341. ' i: 1,',
  13342. ' a: [2, 3],',
  13343. ' s: [4, 5],',
  13344. ' m: [[11, 12], [13, 14]],',
  13345. ' p: this.TPoint.$clone({',
  13346. ' x: 21,',
  13347. ' y: 0',
  13348. ' })',
  13349. '});',
  13350. 'this.p = [this.TPoint.$clone({',
  13351. ' x: 1,',
  13352. ' y: 2',
  13353. '}), this.TPoint.$clone({',
  13354. ' x: 3,',
  13355. ' y: 4',
  13356. '})];',
  13357. '']),
  13358. LinesToStr([ // $mod.$main
  13359. '']));
  13360. end;
  13361. procedure TTestModule.TestAdvRecord_ExternalField;
  13362. begin
  13363. StartProgram(false);
  13364. Add([
  13365. '{$modeswitch AdvancedRecords}',
  13366. '{$modeswitch externalclass}',
  13367. 'type',
  13368. ' TCar = record',
  13369. ' public',
  13370. ' Intern: longint external name ''$Intern'';',
  13371. ' Intern2: longint external name ''$Intern2'';',
  13372. ' Bracket: longint external name ''["A B"]'';',
  13373. ' procedure DoIt;',
  13374. ' end;',
  13375. 'procedure tcar.doit;',
  13376. 'begin',
  13377. ' Intern:=Intern+1;',
  13378. ' Intern2:=Intern2+2;',
  13379. ' Bracket:=Bracket+3;',
  13380. 'end;',
  13381. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  13382. 'begin',
  13383. ' Rec.intern:=Rec.intern+1;',
  13384. ' Rec.intern2:=Rec.intern2+2;',
  13385. ' Rec.Bracket:=Rec.Bracket+3;',
  13386. ' with Rec do begin',
  13387. ' intern:=intern+1;',
  13388. ' intern2:=intern2+2;',
  13389. ' Bracket:=Bracket+3;',
  13390. ' end;']);
  13391. ConvertProgram;
  13392. CheckSource('TestAdvRecord_ExternalField',
  13393. LinesToStr([ // statements
  13394. 'rtl.recNewT(this, "TCar", function () {',
  13395. ' this.$eq = function (b) {',
  13396. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  13397. ' };',
  13398. ' this.$assign = function (s) {',
  13399. ' this.$Intern = s.$Intern;',
  13400. ' this.$Intern2 = s.$Intern2;',
  13401. ' this["A B"] = s["A B"];',
  13402. ' return this;',
  13403. ' };',
  13404. ' this.DoIt = function () {',
  13405. ' this.$Intern = this.$Intern + 1;',
  13406. ' this.$Intern2 = this.$Intern2 + 2;',
  13407. ' this["A B"] = this["A B"] + 3;',
  13408. ' };',
  13409. '});',
  13410. 'this.Rec = this.TCar.$clone({',
  13411. ' $Intern: 11,',
  13412. ' $Intern2: 12,',
  13413. ' "A B": 13',
  13414. '});',
  13415. '']),
  13416. LinesToStr([ // $mod.$main
  13417. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  13418. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  13419. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  13420. 'var $with = $mod.Rec;',
  13421. '$with.$Intern = $with.$Intern + 1;',
  13422. '$with.$Intern2 = $with.$Intern2 + 2;',
  13423. '$with["A B"] = $with["A B"] + 3;',
  13424. '']));
  13425. end;
  13426. procedure TTestModule.TestAdvRecord_SubRecord;
  13427. begin
  13428. StartProgram(false);
  13429. Add([
  13430. '{$modeswitch AdvancedRecords}',
  13431. 'type',
  13432. ' TRec = record',
  13433. ' type',
  13434. ' TPoint = record',
  13435. ' x,y: longint;',
  13436. ' class var Count: nativeint;',
  13437. ' procedure DoIt;',
  13438. ' class procedure DoThat; static;',
  13439. ' end;',
  13440. ' var',
  13441. ' i: longint;',
  13442. ' p: TPoint;',
  13443. ' procedure DoSome;',
  13444. ' end;',
  13445. 'const',
  13446. ' r: TRec = (',
  13447. ' i:1;',
  13448. ' p: (x:21;y:22)',
  13449. ' );',
  13450. 'procedure TRec.DoSome;',
  13451. 'begin',
  13452. ' p.x:=p.y+1;',
  13453. ' p.Count:=p.Count+2;',
  13454. 'end;',
  13455. 'procedure TRec.TPoint.DoIt;',
  13456. 'begin',
  13457. ' Count:=Count+3;',
  13458. 'end;',
  13459. 'class procedure TRec.TPoint.DoThat;',
  13460. 'begin',
  13461. ' Count:=Count+4;',
  13462. 'end;',
  13463. 'begin']);
  13464. ConvertProgram;
  13465. CheckSource('TestAdvRecord_SubRecord',
  13466. LinesToStr([ // statements
  13467. 'rtl.recNewT(this, "TRec", function () {',
  13468. ' rtl.recNewT(this, "TPoint", function () {',
  13469. ' this.x = 0;',
  13470. ' this.y = 0;',
  13471. ' this.Count = 0;',
  13472. ' this.$eq = function (b) {',
  13473. ' return (this.x === b.x) && (this.y === b.y);',
  13474. ' };',
  13475. ' this.$assign = function (s) {',
  13476. ' this.x = s.x;',
  13477. ' this.y = s.y;',
  13478. ' return this;',
  13479. ' };',
  13480. ' this.DoIt = function () {',
  13481. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  13482. ' };',
  13483. ' this.DoThat = function () {',
  13484. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  13485. ' };',
  13486. ' }, true);',
  13487. ' this.i = 0;',
  13488. ' this.$new = function () {',
  13489. ' var r = Object.create(this);',
  13490. ' r.p = this.TPoint.$new();',
  13491. ' return r;',
  13492. ' };',
  13493. ' this.$eq = function (b) {',
  13494. ' return (this.i === b.i) && this.p.$eq(b.p);',
  13495. ' };',
  13496. ' this.$assign = function (s) {',
  13497. ' this.i = s.i;',
  13498. ' this.p.$assign(s.p);',
  13499. ' return this;',
  13500. ' };',
  13501. ' this.DoSome = function () {',
  13502. ' this.p.x = this.p.y + 1;',
  13503. ' this.TPoint.Count = this.p.Count + 2;',
  13504. ' };',
  13505. '}, true);',
  13506. 'this.r = this.TRec.$clone({',
  13507. ' i: 1,',
  13508. ' p: this.TRec.TPoint.$clone({',
  13509. ' x: 21,',
  13510. ' y: 22',
  13511. ' })',
  13512. '});',
  13513. '']),
  13514. LinesToStr([ // $mod.$main
  13515. '']));
  13516. end;
  13517. procedure TTestModule.TestAdvRecord_SubClass;
  13518. begin
  13519. StartProgram(false);
  13520. Add([
  13521. '{$modeswitch AdvancedRecords}',
  13522. 'type',
  13523. ' TObject = class end;',
  13524. ' TPoint = record',
  13525. ' type',
  13526. ' TBird = class',
  13527. ' procedure DoIt;',
  13528. ' class procedure Glob;',
  13529. ' end;',
  13530. ' procedure DoIt(b: TBird);',
  13531. ' end;',
  13532. 'procedure TPoint.TBird.DoIt;',
  13533. 'begin',
  13534. ' doit;',
  13535. ' self.doit;',
  13536. ' glob;',
  13537. ' self.glob;',
  13538. 'end;',
  13539. 'class procedure TPoint.TBird.Glob;',
  13540. 'begin',
  13541. ' glob;',
  13542. ' self.glob;',
  13543. 'end;',
  13544. 'procedure TPoint.DoIt(b: TBird);',
  13545. 'begin',
  13546. ' b.doit;',
  13547. ' b.glob;',
  13548. ' TBird.glob;',
  13549. 'end;',
  13550. 'begin',
  13551. '']);
  13552. ConvertProgram;
  13553. CheckSource('TestAdvRecord_SubClass',
  13554. LinesToStr([ // statements
  13555. 'rtl.createClass(this, "TObject", null, function () {',
  13556. ' this.$init = function () {',
  13557. ' };',
  13558. ' this.$final = function () {',
  13559. ' };',
  13560. '});',
  13561. 'rtl.recNewT(this, "TPoint", function () {',
  13562. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  13563. ' this.DoIt = function () {',
  13564. ' this.DoIt();',
  13565. ' this.DoIt();',
  13566. ' this.$class.Glob();',
  13567. ' this.$class.Glob();',
  13568. ' };',
  13569. ' this.Glob = function () {',
  13570. ' this.Glob();',
  13571. ' this.Glob();',
  13572. ' };',
  13573. ' }, "TPoint.TBird");',
  13574. ' this.$eq = function (b) {',
  13575. ' return true;',
  13576. ' };',
  13577. ' this.$assign = function (s) {',
  13578. ' return this;',
  13579. ' };',
  13580. ' this.DoIt = function (b) {',
  13581. ' b.DoIt();',
  13582. ' b.$class.Glob();',
  13583. ' this.TBird.Glob();',
  13584. ' };',
  13585. '}, true);',
  13586. '']),
  13587. LinesToStr([ // $mod.$main
  13588. '']));
  13589. end;
  13590. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  13591. begin
  13592. StartProgram(false);
  13593. Add([
  13594. '{$modeswitch AdvancedRecords}',
  13595. 'type',
  13596. ' IUnknown = interface end;',
  13597. ' TPoint = record',
  13598. ' type IBird = interface end;',
  13599. ' end;',
  13600. 'begin',
  13601. '']);
  13602. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  13603. nNotYetImplemented);
  13604. ParseProgram;
  13605. end;
  13606. procedure TTestModule.TestAdvRecord_Constructor;
  13607. begin
  13608. StartProgram(false);
  13609. Add([
  13610. '{$modeswitch AdvancedRecords}',
  13611. 'type',
  13612. ' TPoint = record',
  13613. ' x,y: longint;',
  13614. ' class procedure Run(w: longint = 13); static;',
  13615. ' constructor Create(ax: longint; ay: longint = -1);',
  13616. ' end;',
  13617. 'class procedure tpoint.run(w: longint);',
  13618. 'begin',
  13619. ' run;',
  13620. ' run();',
  13621. 'end;',
  13622. 'constructor tpoint.create(ax,ay: longint);',
  13623. 'begin',
  13624. ' x:=ax;',
  13625. ' self.y:=ay;',
  13626. ' run;',
  13627. ' run(ax);',
  13628. 'end;',
  13629. 'var r: TPoint;',
  13630. 'begin',
  13631. ' r:=TPoint.Create(1,2);',
  13632. ' with TPoint do r:=Create(1,2);',
  13633. ' r.Create(3);',
  13634. ' r:=r.Create(4);',
  13635. '']);
  13636. ConvertProgram;
  13637. CheckSource('TestAdvRecord_Constructor',
  13638. LinesToStr([ // statements
  13639. 'rtl.recNewT(this, "TPoint", function () {',
  13640. ' this.x = 0;',
  13641. ' this.y = 0;',
  13642. ' this.$eq = function (b) {',
  13643. ' return (this.x === b.x) && (this.y === b.y);',
  13644. ' };',
  13645. ' this.$assign = function (s) {',
  13646. ' this.x = s.x;',
  13647. ' this.y = s.y;',
  13648. ' return this;',
  13649. ' };',
  13650. ' this.Run = function (w) {',
  13651. ' $mod.TPoint.Run(13);',
  13652. ' $mod.TPoint.Run(13);',
  13653. ' };',
  13654. ' this.Create = function (ax, ay) {',
  13655. ' this.x = ax;',
  13656. ' this.y = ay;',
  13657. ' this.Run(13);',
  13658. ' this.Run(ax);',
  13659. ' return this;',
  13660. ' };',
  13661. '});',
  13662. 'this.r = this.TPoint.$new();',
  13663. '']),
  13664. LinesToStr([ // $mod.$main
  13665. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  13666. 'var $with = $mod.TPoint;',
  13667. '$mod.r.$assign($with.$new().Create(1, 2));',
  13668. '$mod.r.Create(3, -1);',
  13669. '$mod.r.$assign($mod.r.Create(4, -1));',
  13670. '']));
  13671. end;
  13672. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  13673. begin
  13674. StartProgram(false);
  13675. Add([
  13676. '{$modeswitch AdvancedRecords}',
  13677. 'type',
  13678. ' TPoint = record',
  13679. ' class var x: longint;',
  13680. ' class procedure Fly; static;',
  13681. ' class constructor Init;',
  13682. ' end;',
  13683. 'var count: word;',
  13684. 'class procedure Tpoint.Fly;',
  13685. 'begin',
  13686. 'end;',
  13687. 'class constructor tpoint.init;',
  13688. 'begin',
  13689. ' count:=count+1;',
  13690. ' x:=x+3;',
  13691. ' tpoint.x:=tpoint.x+4;',
  13692. ' fly;',
  13693. ' tpoint.fly;',
  13694. 'end;',
  13695. 'var r: TPoint;',
  13696. 'begin',
  13697. ' r.x:=r.x+10;',
  13698. ' r.Fly;',
  13699. ' r.Fly();',
  13700. '']);
  13701. ConvertProgram;
  13702. CheckSource('TestAdvRecord_ClassConstructor_Program',
  13703. LinesToStr([ // statements
  13704. 'rtl.recNewT(this, "TPoint", function () {',
  13705. ' this.x = 0;',
  13706. ' this.$eq = function (b) {',
  13707. ' return true;',
  13708. ' };',
  13709. ' this.$assign = function (s) {',
  13710. ' return this;',
  13711. ' };',
  13712. ' this.Fly = function () {',
  13713. ' };',
  13714. '}, true);',
  13715. 'this.count = 0;',
  13716. 'this.r = this.TPoint.$new();',
  13717. '']),
  13718. LinesToStr([ // $mod.$main
  13719. '(function () {',
  13720. ' $mod.count = $mod.count + 1;',
  13721. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  13722. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  13723. ' $mod.TPoint.Fly();',
  13724. ' $mod.TPoint.Fly();',
  13725. '})();',
  13726. '$mod.TPoint.x = $mod.r.x + 10;',
  13727. '$mod.TPoint.Fly();',
  13728. '$mod.TPoint.Fly();',
  13729. '']));
  13730. end;
  13731. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  13732. begin
  13733. StartUnit(false);
  13734. Add([
  13735. 'interface',
  13736. '{$modeswitch AdvancedRecords}',
  13737. 'type',
  13738. ' TPoint = record',
  13739. ' class var x: longint;',
  13740. ' class procedure Fly; static;',
  13741. ' class constructor Init;',
  13742. ' end;',
  13743. 'implementation',
  13744. 'var count: word;',
  13745. 'class procedure Tpoint.Fly;',
  13746. 'begin',
  13747. 'end;',
  13748. 'class constructor tpoint.init;',
  13749. 'begin',
  13750. ' count:=count+1;',
  13751. ' x:=3;',
  13752. ' tpoint.x:=4;',
  13753. ' fly;',
  13754. ' tpoint.fly;',
  13755. 'end;',
  13756. '']);
  13757. ConvertUnit;
  13758. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  13759. LinesToStr([ // statements
  13760. 'var $impl = $mod.$impl;',
  13761. 'rtl.recNewT(this, "TPoint", function () {',
  13762. ' this.x = 0;',
  13763. ' this.$eq = function (b) {',
  13764. ' return true;',
  13765. ' };',
  13766. ' this.$assign = function (s) {',
  13767. ' return this;',
  13768. ' };',
  13769. ' this.Fly = function () {',
  13770. ' };',
  13771. '}, true);',
  13772. '']),
  13773. LinesToStr([ // $mod.$init
  13774. '(function () {',
  13775. ' $impl.count = $impl.count + 1;',
  13776. ' $mod.TPoint.x = 3;',
  13777. ' $mod.TPoint.x = 4;',
  13778. ' $mod.TPoint.Fly();',
  13779. ' $mod.TPoint.Fly();',
  13780. '})();',
  13781. '']),
  13782. LinesToStr([ // $mod.$main
  13783. '$impl.count = 0;',
  13784. '']));
  13785. end;
  13786. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  13787. begin
  13788. StartProgram(false);
  13789. Add(['type',
  13790. ' TObject = class',
  13791. ' public',
  13792. ' constructor Create;',
  13793. ' destructor Destroy;',
  13794. ' end;',
  13795. ' TBird = TObject;',
  13796. 'constructor tobject.create;',
  13797. 'begin end;',
  13798. 'destructor tobject.destroy;',
  13799. 'begin end;',
  13800. 'var Obj: tobject;',
  13801. 'begin',
  13802. ' obj:=tobject.create;',
  13803. ' obj:=tobject.create();',
  13804. ' obj:=tbird.create;',
  13805. ' obj:=tbird.create();',
  13806. ' obj:=obj.create();',
  13807. ' obj.destroy;',
  13808. '']);
  13809. ConvertProgram;
  13810. CheckSource('TestClass_TObjectDefaultConstructor',
  13811. LinesToStr([ // statements
  13812. 'rtl.createClass(this,"TObject",null,function(){',
  13813. ' this.$init = function () {',
  13814. ' };',
  13815. ' this.$final = function () {',
  13816. ' };',
  13817. ' this.Create = function(){',
  13818. ' return this;',
  13819. ' };',
  13820. ' this.Destroy = function(){',
  13821. ' };',
  13822. '});',
  13823. 'this.Obj = null;'
  13824. ]),
  13825. LinesToStr([ // $mod.$main
  13826. '$mod.Obj = $mod.TObject.$create("Create");',
  13827. '$mod.Obj = $mod.TObject.$create("Create");',
  13828. '$mod.Obj = $mod.TObject.$create("Create");',
  13829. '$mod.Obj = $mod.TObject.$create("Create");',
  13830. '$mod.Obj = $mod.Obj.Create();',
  13831. '$mod.Obj.$destroy("Destroy");',
  13832. '']));
  13833. end;
  13834. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  13835. begin
  13836. StartProgram(false);
  13837. Add('type');
  13838. Add(' TObject = class');
  13839. Add(' public');
  13840. Add(' constructor Create(Par: longint);');
  13841. Add(' end;');
  13842. Add('constructor tobject.create(par: longint);');
  13843. Add('begin end;');
  13844. Add('var Obj: tobject;');
  13845. Add('begin');
  13846. Add(' obj:=tobject.create(3);');
  13847. ConvertProgram;
  13848. CheckSource('TestClass_TObjectConstructorWithParams',
  13849. LinesToStr([ // statements
  13850. 'rtl.createClass(this,"TObject",null,function(){',
  13851. ' this.$init = function () {',
  13852. ' };',
  13853. ' this.$final = function () {',
  13854. ' };',
  13855. ' this.Create = function(Par){',
  13856. ' return this;',
  13857. ' };',
  13858. '});',
  13859. 'this.Obj = null;'
  13860. ]),
  13861. LinesToStr([ // $mod.$main
  13862. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  13863. ]));
  13864. end;
  13865. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  13866. begin
  13867. StartProgram(false);
  13868. Add('type');
  13869. Add(' TObject = class');
  13870. Add(' public');
  13871. Add(' constructor Create;');
  13872. Add(' end;');
  13873. Add(' TTest = class(TObject)');
  13874. Add(' public');
  13875. Add(' constructor Create(const Par: longint = 1);');
  13876. Add(' end;');
  13877. Add('constructor tobject.create;');
  13878. Add('begin end;');
  13879. Add('constructor ttest.create(const par: longint);');
  13880. Add('begin end;');
  13881. Add('var t: ttest;');
  13882. Add('begin');
  13883. Add(' t:=ttest.create;');
  13884. Add(' t:=ttest.create(2);');
  13885. ConvertProgram;
  13886. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  13887. LinesToStr([ // statements
  13888. 'rtl.createClass(this,"TObject",null,function(){',
  13889. ' this.$init = function () {',
  13890. ' };',
  13891. ' this.$final = function () {',
  13892. ' };',
  13893. ' this.Create = function(){',
  13894. ' return this;',
  13895. ' };',
  13896. '});',
  13897. 'rtl.createClass(this, "TTest", this.TObject, function () {',
  13898. ' this.Create$1 = function (Par) {',
  13899. ' return this;',
  13900. ' };',
  13901. '});',
  13902. 'this.t = null;'
  13903. ]),
  13904. LinesToStr([ // $mod.$main
  13905. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  13906. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  13907. ]));
  13908. end;
  13909. procedure TTestModule.TestClass_Var;
  13910. begin
  13911. StartProgram(false);
  13912. Add([
  13913. 'type',
  13914. ' TObject = class',
  13915. ' public',
  13916. ' vI: longint;',
  13917. ' constructor Create(Par: longint);',
  13918. ' end;',
  13919. 'constructor tobject.create(par: longint);',
  13920. 'begin',
  13921. ' vi:=par+3',
  13922. 'end;',
  13923. 'var Obj: tobject;',
  13924. 'begin',
  13925. ' obj:=tobject.create(4);',
  13926. ' obj.vi:=obj.VI+5;']);
  13927. ConvertProgram;
  13928. CheckSource('TestClass_Var',
  13929. LinesToStr([ // statements
  13930. 'rtl.createClass(this,"TObject",null,function(){',
  13931. ' this.$init = function () {',
  13932. ' this.vI = 0;',
  13933. ' };',
  13934. ' this.$final = function () {',
  13935. ' };',
  13936. ' this.Create = function(Par){',
  13937. ' this.vI = Par+3;',
  13938. ' return this;',
  13939. ' };',
  13940. '});',
  13941. 'this.Obj = null;'
  13942. ]),
  13943. LinesToStr([ // $mod.$main
  13944. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  13945. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  13946. ]));
  13947. end;
  13948. procedure TTestModule.TestClass_Method;
  13949. begin
  13950. StartProgram(false);
  13951. Add('type');
  13952. Add(' TObject = class');
  13953. Add(' public');
  13954. Add(' vI: longint;');
  13955. Add(' Sub: TObject;');
  13956. Add(' constructor Create;');
  13957. Add(' function GetIt(Par: longint): tobject;');
  13958. Add(' end;');
  13959. Add('constructor tobject.create; begin end;');
  13960. Add('function tobject.getit(par: longint): tobject;');
  13961. Add('begin');
  13962. Add(' Self.vi:=par+3;');
  13963. Add(' Result:=self.sub;');
  13964. Add('end;');
  13965. Add('var Obj: tobject;');
  13966. Add('begin');
  13967. Add(' obj:=tobject.create;');
  13968. Add(' obj.getit(4);');
  13969. Add(' obj.sub.sub:=nil;');
  13970. Add(' obj.sub.getit(5);');
  13971. Add(' obj.sub.getit(6).SUB:=nil;');
  13972. Add(' obj.sub.getit(7).GETIT(8);');
  13973. Add(' obj.sub.getit(9).SuB.getit(10);');
  13974. ConvertProgram;
  13975. CheckSource('TestClass_Method',
  13976. LinesToStr([ // statements
  13977. 'rtl.createClass(this,"TObject",null,function(){',
  13978. ' this.$init = function () {',
  13979. ' this.vI = 0;',
  13980. ' this.Sub = null;',
  13981. ' };',
  13982. ' this.$final = function () {',
  13983. ' this.Sub = undefined;',
  13984. ' };',
  13985. ' this.Create = function(){',
  13986. ' return this;',
  13987. ' };',
  13988. ' this.GetIt = function(Par){',
  13989. ' var Result = null;',
  13990. ' this.vI = Par + 3;',
  13991. ' Result = this.Sub;',
  13992. ' return Result;',
  13993. ' };',
  13994. '});',
  13995. 'this.Obj = null;'
  13996. ]),
  13997. LinesToStr([ // $mod.$main
  13998. '$mod.Obj = $mod.TObject.$create("Create");',
  13999. '$mod.Obj.GetIt(4);',
  14000. '$mod.Obj.Sub.Sub=null;',
  14001. '$mod.Obj.Sub.GetIt(5);',
  14002. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  14003. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  14004. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  14005. ]));
  14006. end;
  14007. procedure TTestModule.TestClass_Implementation;
  14008. begin
  14009. StartUnit(false);
  14010. Add([
  14011. 'interface',
  14012. 'type',
  14013. ' TObject = class',
  14014. ' constructor Create;',
  14015. ' end;',
  14016. 'implementation',
  14017. 'type',
  14018. ' TIntClass = class',
  14019. ' constructor Create; reintroduce;',
  14020. ' class procedure DoGlob;',
  14021. ' end;',
  14022. 'constructor tintclass.create;',
  14023. 'begin',
  14024. ' inherited;',
  14025. ' inherited create;',
  14026. ' doglob;',
  14027. 'end;',
  14028. 'class procedure tintclass.doglob;',
  14029. 'begin',
  14030. 'end;',
  14031. 'constructor tobject.create;',
  14032. 'var',
  14033. ' iC: tintclass;',
  14034. 'begin',
  14035. ' ic:=tintclass.create;',
  14036. ' tintclass.doglob;',
  14037. ' ic.doglob;',
  14038. 'end;',
  14039. 'initialization',
  14040. ' tintclass.doglob;',
  14041. '']);
  14042. ConvertUnit;
  14043. CheckSource('TestClass_Implementation',
  14044. LinesToStr([ // statements
  14045. 'var $impl = $mod.$impl;',
  14046. 'rtl.createClass(this, "TObject", null, function () {',
  14047. ' this.$init = function () {',
  14048. ' };',
  14049. ' this.$final = function () {',
  14050. ' };',
  14051. ' this.Create = function () {',
  14052. ' var iC = null;',
  14053. ' iC = $impl.TIntClass.$create("Create$1");',
  14054. ' $impl.TIntClass.DoGlob();',
  14055. ' iC.$class.DoGlob();',
  14056. ' return this;',
  14057. ' };',
  14058. '});',
  14059. '']),
  14060. LinesToStr([ // $mod.$main
  14061. '$impl.TIntClass.DoGlob();',
  14062. '']),
  14063. LinesToStr([
  14064. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  14065. ' this.Create$1 = function () {',
  14066. ' $mod.TObject.Create.call(this);',
  14067. ' $mod.TObject.Create.call(this);',
  14068. ' this.$class.DoGlob();',
  14069. ' return this;',
  14070. ' };',
  14071. ' this.DoGlob = function () {',
  14072. ' };',
  14073. '});',
  14074. '']));
  14075. end;
  14076. procedure TTestModule.TestClass_Inheritance;
  14077. begin
  14078. StartProgram(false);
  14079. Add('type');
  14080. Add(' TObject = class');
  14081. Add(' public');
  14082. Add(' constructor Create;');
  14083. Add(' end;');
  14084. Add(' TClassA = class');
  14085. Add(' end;');
  14086. Add(' TClassB = class(TObject)');
  14087. Add(' procedure ProcB;');
  14088. Add(' end;');
  14089. Add('constructor tobject.create; begin end;');
  14090. Add('procedure tclassb.procb; begin end;');
  14091. Add('var');
  14092. Add(' oO: TObject;');
  14093. Add(' oA: TClassA;');
  14094. Add(' oB: TClassB;');
  14095. Add('begin');
  14096. Add(' oO:=tobject.Create;');
  14097. Add(' oA:=tclassa.Create;');
  14098. Add(' ob:=tclassb.Create;');
  14099. Add(' if oo is tclassa then ;');
  14100. Add(' ob:=oo as tclassb;');
  14101. Add(' (oo as tclassb).procb;');
  14102. ConvertProgram;
  14103. CheckSource('TestClass_Inheritance',
  14104. LinesToStr([ // statements
  14105. 'rtl.createClass(this,"TObject",null,function(){',
  14106. ' this.$init = function () {',
  14107. ' };',
  14108. ' this.$final = function () {',
  14109. ' };',
  14110. ' this.Create = function () {',
  14111. ' return this;',
  14112. ' };',
  14113. '});',
  14114. 'rtl.createClass(this,"TClassA",this.TObject,function(){',
  14115. '});',
  14116. 'rtl.createClass(this,"TClassB",this.TObject,function(){',
  14117. ' this.ProcB = function () {',
  14118. ' };',
  14119. '});',
  14120. 'this.oO = null;',
  14121. 'this.oA = null;',
  14122. 'this.oB = null;'
  14123. ]),
  14124. LinesToStr([ // $mod.$main
  14125. '$mod.oO = $mod.TObject.$create("Create");',
  14126. '$mod.oA = $mod.TClassA.$create("Create");',
  14127. '$mod.oB = $mod.TClassB.$create("Create");',
  14128. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  14129. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  14130. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  14131. ]));
  14132. end;
  14133. procedure TTestModule.TestClass_TypeAlias;
  14134. begin
  14135. StartProgram(false);
  14136. Add([
  14137. '{$interfaces corba}',
  14138. 'type',
  14139. ' IObject = interface',
  14140. ' end;',
  14141. ' IBird = type IObject;',
  14142. ' TObject = class',
  14143. ' end;',
  14144. ' TBird = type TObject;',
  14145. 'var',
  14146. ' oObj: TObject;',
  14147. ' oBird: TBird;',
  14148. ' IntfObj: IObject;',
  14149. ' IntfBird: IBird;',
  14150. 'begin',
  14151. ' oObj:=oBird;',
  14152. '']);
  14153. ConvertProgram;
  14154. CheckSource('TestClass_TypeAlias',
  14155. LinesToStr([ // statements
  14156. 'rtl.createInterface(this, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  14157. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], this.IObject);',
  14158. 'rtl.createClass(this, "TObject", null, function () {',
  14159. ' this.$init = function () {',
  14160. ' };',
  14161. ' this.$final = function () {',
  14162. ' };',
  14163. '});',
  14164. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14165. '});',
  14166. 'this.oObj = null;',
  14167. 'this.oBird = null;',
  14168. 'this.IntfObj = null;',
  14169. 'this.IntfBird = null;',
  14170. '']),
  14171. LinesToStr([ // $mod.$main
  14172. '$mod.oObj = $mod.oBird;',
  14173. '']));
  14174. end;
  14175. procedure TTestModule.TestClass_AbstractMethod;
  14176. begin
  14177. StartProgram(false);
  14178. Add('type');
  14179. Add(' TObject = class');
  14180. Add(' public');
  14181. Add(' procedure DoIt; virtual; abstract;');
  14182. Add(' end;');
  14183. Add('begin');
  14184. ConvertProgram;
  14185. CheckSource('TestClass_AbstractMethod',
  14186. LinesToStr([ // statements
  14187. 'rtl.createClass(this,"TObject",null,function(){',
  14188. ' this.$init = function () {',
  14189. ' };',
  14190. ' this.$final = function () {',
  14191. ' };',
  14192. '});'
  14193. ]),
  14194. LinesToStr([ // this.$main
  14195. ''
  14196. ]));
  14197. end;
  14198. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  14199. begin
  14200. StartProgram(false);
  14201. Add([
  14202. 'type',
  14203. ' TObject = class',
  14204. ' procedure DoAbstract; virtual; abstract;',
  14205. ' procedure DoVirtual; virtual;',
  14206. ' procedure DoIt;',
  14207. ' end;',
  14208. ' TA = class',
  14209. ' procedure doabstract; override;',
  14210. ' procedure dovirtual; override;',
  14211. ' procedure DoSome;',
  14212. ' end;',
  14213. 'procedure tobject.dovirtual;',
  14214. 'begin',
  14215. ' inherited; // call non existing ancestor -> ignore silently',
  14216. 'end;',
  14217. 'procedure tobject.doit;',
  14218. 'begin',
  14219. 'end;',
  14220. 'procedure ta.doabstract;',
  14221. 'begin',
  14222. ' inherited dovirtual; // call TObject.DoVirtual',
  14223. 'end;',
  14224. 'procedure ta.dovirtual;',
  14225. 'begin',
  14226. ' inherited; // call TObject.DoVirtual',
  14227. ' inherited dovirtual; // call TObject.DoVirtual',
  14228. ' inherited dovirtual(); // call TObject.DoVirtual',
  14229. ' doit;',
  14230. ' doit();',
  14231. 'end;',
  14232. 'procedure ta.dosome;',
  14233. 'begin',
  14234. ' inherited; // call non existing ancestor method -> silently ignore',
  14235. 'end;',
  14236. 'begin']);
  14237. ConvertProgram;
  14238. CheckSource('TestClass_CallInherited_ProcNoParams',
  14239. LinesToStr([ // statements
  14240. 'rtl.createClass(this,"TObject",null,function(){',
  14241. ' this.$init = function () {',
  14242. ' };',
  14243. ' this.$final = function () {',
  14244. ' };',
  14245. ' this.DoVirtual = function () {',
  14246. ' };',
  14247. ' this.DoIt = function () {',
  14248. ' };',
  14249. '});',
  14250. 'rtl.createClass(this, "TA", this.TObject, function () {',
  14251. ' this.DoAbstract = function () {',
  14252. ' $mod.TObject.DoVirtual.call(this);',
  14253. ' };',
  14254. ' this.DoVirtual = function () {',
  14255. ' $mod.TObject.DoVirtual.call(this);',
  14256. ' $mod.TObject.DoVirtual.call(this);',
  14257. ' $mod.TObject.DoVirtual.call(this);',
  14258. ' this.DoIt();',
  14259. ' this.DoIt();',
  14260. ' };',
  14261. ' this.DoSome = function () {',
  14262. ' };',
  14263. '});'
  14264. ]),
  14265. LinesToStr([ // this.$main
  14266. ''
  14267. ]));
  14268. end;
  14269. procedure TTestModule.TestClass_CallInherited_WithParams;
  14270. begin
  14271. StartProgram(false);
  14272. Add([
  14273. 'type',
  14274. ' TObject = class',
  14275. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  14276. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  14277. ' procedure DoIt(pA: longint; pB: longint = 0);',
  14278. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  14279. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  14280. ' end;',
  14281. ' TClassA = class',
  14282. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  14283. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  14284. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  14285. ' end;',
  14286. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  14287. 'begin',
  14288. 'end;',
  14289. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  14290. 'begin',
  14291. 'end;',
  14292. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  14293. 'begin',
  14294. 'end;',
  14295. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  14296. 'begin',
  14297. 'end;',
  14298. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  14299. 'begin',
  14300. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  14301. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  14302. 'end;',
  14303. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  14304. 'begin',
  14305. ' inherited; // call TObject.DoVirtual(pA,pB)',
  14306. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  14307. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  14308. ' doit(pa,pb);',
  14309. ' doit(pa);',
  14310. ' doit2(pa);',
  14311. ' doit2;',
  14312. 'end;',
  14313. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  14314. 'begin',
  14315. ' pa:=inherited;',
  14316. 'end;',
  14317. 'begin']);
  14318. ConvertProgram;
  14319. CheckSource('TestClass_CallInherited_WithParams',
  14320. LinesToStr([ // statements
  14321. 'rtl.createClass(this,"TObject",null,function(){',
  14322. ' this.$init = function () {',
  14323. ' };',
  14324. ' this.$final = function () {',
  14325. ' };',
  14326. ' this.DoVirtual = function (pA,pB) {',
  14327. ' };',
  14328. ' this.DoIt = function (pA,pB) {',
  14329. ' };',
  14330. ' this.DoIt2 = function (pA,pB) {',
  14331. ' };',
  14332. ' this.GetIt = function (pA, pB) {',
  14333. ' var Result = 0;',
  14334. ' return Result;',
  14335. ' };',
  14336. '});',
  14337. 'rtl.createClass(this, "TClassA", this.TObject, function () {',
  14338. ' this.DoAbstract = function (pA,pB) {',
  14339. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  14340. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  14341. ' };',
  14342. ' this.DoVirtual = function (pA,pB) {',
  14343. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  14344. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  14345. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  14346. ' this.DoIt(pA,pB);',
  14347. ' this.DoIt(pA,0);',
  14348. ' this.DoIt2(pA,2);',
  14349. ' this.DoIt2(1,2);',
  14350. ' };',
  14351. ' this.GetIt$1 = function (pA, pB) {',
  14352. ' var Result = 0;',
  14353. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  14354. ' return Result;',
  14355. ' };',
  14356. '});'
  14357. ]),
  14358. LinesToStr([ // this.$main
  14359. ''
  14360. ]));
  14361. end;
  14362. procedure TTestModule.TestClasS_CallInheritedConstructor;
  14363. begin
  14364. StartProgram(false);
  14365. Add('type');
  14366. Add(' TObject = class');
  14367. Add(' constructor Create; virtual;');
  14368. Add(' constructor CreateWithB(b: boolean);');
  14369. Add(' end;');
  14370. Add(' TA = class');
  14371. Add(' constructor Create; override;');
  14372. Add(' constructor CreateWithC(c: char);');
  14373. Add(' procedure DoIt;');
  14374. Add(' class function DoSome: TObject;');
  14375. Add(' end;');
  14376. Add('constructor tobject.create;');
  14377. Add('begin');
  14378. Add(' inherited; // call non existing ancestor -> ignore silently');
  14379. Add('end;');
  14380. Add('constructor tobject.createwithb(b: boolean);');
  14381. Add('begin');
  14382. Add(' inherited; // call non existing ancestor -> ignore silently');
  14383. Add(' create; // normal call');
  14384. Add('end;');
  14385. Add('constructor ta.create;');
  14386. Add('begin');
  14387. Add(' inherited; // normal call TObject.Create');
  14388. Add(' inherited create; // normal call TObject.Create');
  14389. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  14390. Add('end;');
  14391. Add('constructor ta.createwithc(c: char);');
  14392. Add('begin');
  14393. Add(' inherited create; // call TObject.Create');
  14394. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  14395. Add(' doit;');
  14396. Add(' doit();');
  14397. Add(' dosome;');
  14398. Add('end;');
  14399. Add('procedure ta.doit;');
  14400. Add('begin');
  14401. Add(' create; // normal call');
  14402. Add(' createwithb(false); // normal call');
  14403. Add(' createwithc(''c''); // normal call');
  14404. Add('end;');
  14405. Add('class function ta.dosome: TObject;');
  14406. Add('begin');
  14407. Add(' Result:=create; // constructor');
  14408. Add(' Result:=createwithb(true); // constructor');
  14409. Add(' Result:=createwithc(''c''); // constructor');
  14410. Add('end;');
  14411. Add('begin');
  14412. ConvertProgram;
  14413. CheckSource('TestClass_CallInheritedConstructor',
  14414. LinesToStr([ // statements
  14415. 'rtl.createClass(this,"TObject",null,function(){',
  14416. ' this.$init = function () {',
  14417. ' };',
  14418. ' this.$final = function () {',
  14419. ' };',
  14420. ' this.Create = function () {',
  14421. ' return this;',
  14422. ' };',
  14423. ' this.CreateWithB = function (b) {',
  14424. ' this.Create();',
  14425. ' return this;',
  14426. ' };',
  14427. '});',
  14428. 'rtl.createClass(this, "TA", this.TObject, function () {',
  14429. ' this.Create = function () {',
  14430. ' $mod.TObject.Create.call(this);',
  14431. ' $mod.TObject.Create.call(this);',
  14432. ' $mod.TObject.CreateWithB.call(this, false);',
  14433. ' return this;',
  14434. ' };',
  14435. ' this.CreateWithC = function (c) {',
  14436. ' $mod.TObject.Create.call(this);',
  14437. ' $mod.TObject.CreateWithB.call(this, true);',
  14438. ' this.DoIt();',
  14439. ' this.DoIt();',
  14440. ' this.$class.DoSome();',
  14441. ' return this;',
  14442. ' };',
  14443. ' this.DoIt = function () {',
  14444. ' this.Create();',
  14445. ' this.CreateWithB(false);',
  14446. ' this.CreateWithC("c");',
  14447. ' };',
  14448. ' this.DoSome = function () {',
  14449. ' var Result = null;',
  14450. ' Result = this.$create("Create");',
  14451. ' Result = this.$create("CreateWithB", [true]);',
  14452. ' Result = this.$create("CreateWithC", ["c"]);',
  14453. ' return Result;',
  14454. ' };',
  14455. '});'
  14456. ]),
  14457. LinesToStr([ // this.$main
  14458. ''
  14459. ]));
  14460. end;
  14461. procedure TTestModule.TestClass_ClassVar_Assign;
  14462. begin
  14463. StartProgram(false);
  14464. Add([
  14465. 'type',
  14466. ' TObject = class',
  14467. ' public',
  14468. ' class var vI: longint;',
  14469. ' class var Sub: TObject;',
  14470. ' constructor Create;',
  14471. ' class function GetIt(var Par: longint): tobject;',
  14472. ' end;',
  14473. 'constructor tobject.create;',
  14474. 'begin',
  14475. ' vi:=vi+1;',
  14476. ' Self.vi:=Self.vi+1;',
  14477. ' inc(vi);',
  14478. 'end;',
  14479. 'class function tobject.getit(var par: longint): tobject;',
  14480. 'begin',
  14481. ' vi:=vi+3;',
  14482. ' Self.vi:=Self.vi+4;',
  14483. ' inc(vi);',
  14484. ' Result:=self.sub;',
  14485. ' GetIt(vi);',
  14486. 'end;',
  14487. 'var Obj: tobject;',
  14488. 'begin',
  14489. ' obj:=tobject.create;',
  14490. ' tobject.vi:=3;',
  14491. ' if tobject.vi=4 then ;',
  14492. ' tobject.sub:=nil;',
  14493. ' obj.sub:=nil;',
  14494. ' obj.sub.sub:=nil;']);
  14495. ConvertProgram;
  14496. CheckSource('TestClass_ClassVar_Assign',
  14497. LinesToStr([ // statements
  14498. 'rtl.createClass(this,"TObject",null,function(){',
  14499. ' this.vI = 0;',
  14500. ' this.Sub = null;',
  14501. ' this.$init = function () {',
  14502. ' };',
  14503. ' this.$final = function () {',
  14504. ' };',
  14505. ' this.Create = function(){',
  14506. ' $mod.TObject.vI = this.vI+1;',
  14507. ' $mod.TObject.vI = this.vI+1;',
  14508. ' $mod.TObject.vI += 1;',
  14509. ' return this;',
  14510. ' };',
  14511. ' this.GetIt = function(Par){',
  14512. ' var Result = null;',
  14513. ' $mod.TObject.vI = this.vI + 3;',
  14514. ' $mod.TObject.vI = this.vI + 4;',
  14515. ' $mod.TObject.vI += 1;',
  14516. ' Result = this.Sub;',
  14517. ' this.GetIt({',
  14518. ' p: $mod.TObject,',
  14519. ' get: function () {',
  14520. ' return this.p.vI;',
  14521. ' },',
  14522. ' set: function (v) {',
  14523. ' this.p.vI = v;',
  14524. ' }',
  14525. ' });',
  14526. ' return Result;',
  14527. ' };',
  14528. '});',
  14529. 'this.Obj = null;'
  14530. ]),
  14531. LinesToStr([ // $mod.$main
  14532. '$mod.Obj = $mod.TObject.$create("Create");',
  14533. '$mod.TObject.vI = 3;',
  14534. 'if ($mod.TObject.vI === 4);',
  14535. '$mod.TObject.Sub=null;',
  14536. '$mod.TObject.Sub=null;',
  14537. '$mod.TObject.Sub=null;',
  14538. '']));
  14539. end;
  14540. procedure TTestModule.TestClass_CallClassMethod;
  14541. begin
  14542. StartProgram(false);
  14543. Add('type');
  14544. Add(' TObject = class');
  14545. Add(' public');
  14546. Add(' class var vI: longint;');
  14547. Add(' class var Sub: TObject;');
  14548. Add(' constructor Create;');
  14549. Add(' function GetMore(Par: longint): longint;');
  14550. Add(' class function GetIt(Par: longint): tobject;');
  14551. Add(' end;');
  14552. Add('constructor tobject.create;');
  14553. Add('begin');
  14554. Add(' sub:=getit(3);');
  14555. Add(' vi:=getmore(4);');
  14556. Add(' sub:=Self.getit(5);');
  14557. Add(' vi:=Self.getmore(6);');
  14558. Add('end;');
  14559. Add('function tobject.getmore(par: longint): longint;');
  14560. Add('begin');
  14561. Add(' sub:=getit(11);');
  14562. Add(' vi:=getmore(12);');
  14563. Add(' sub:=self.getit(13);');
  14564. Add(' vi:=self.getmore(14);');
  14565. Add('end;');
  14566. Add('class function tobject.getit(par: longint): tobject;');
  14567. Add('begin');
  14568. Add(' sub:=getit(21);');
  14569. Add(' vi:=sub.getmore(22);');
  14570. Add(' sub:=self.getit(23);');
  14571. Add(' vi:=self.sub.getmore(24);');
  14572. Add('end;');
  14573. Add('var Obj: tobject;');
  14574. Add('begin');
  14575. Add(' obj:=tobject.create;');
  14576. Add(' tobject.getit(5);');
  14577. Add(' obj.getit(6);');
  14578. Add(' obj.sub.getit(7);');
  14579. Add(' obj.sub.getit(8).SUB:=nil;');
  14580. Add(' obj.sub.getit(9).GETIT(10);');
  14581. Add(' obj.sub.getit(11).SuB.getit(12);');
  14582. ConvertProgram;
  14583. CheckSource('TestClass_CallClassMethod',
  14584. LinesToStr([ // statements
  14585. 'rtl.createClass(this,"TObject",null,function(){',
  14586. ' this.vI = 0;',
  14587. ' this.Sub = null;',
  14588. ' this.$init = function () {',
  14589. ' };',
  14590. ' this.$final = function () {',
  14591. ' };',
  14592. ' this.Create = function(){',
  14593. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  14594. ' $mod.TObject.vI = this.GetMore(4);',
  14595. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  14596. ' $mod.TObject.vI = this.GetMore(6);',
  14597. ' return this;',
  14598. ' };',
  14599. ' this.GetMore = function(Par){',
  14600. ' var Result = 0;',
  14601. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  14602. ' $mod.TObject.vI = this.GetMore(12);',
  14603. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  14604. ' $mod.TObject.vI = this.GetMore(14);',
  14605. ' return Result;',
  14606. ' };',
  14607. ' this.GetIt = function(Par){',
  14608. ' var Result = null;',
  14609. ' $mod.TObject.Sub = this.GetIt(21);',
  14610. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  14611. ' $mod.TObject.Sub = this.GetIt(23);',
  14612. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  14613. ' return Result;',
  14614. ' };',
  14615. '});',
  14616. 'this.Obj = null;'
  14617. ]),
  14618. LinesToStr([ // $mod.$main
  14619. '$mod.Obj = $mod.TObject.$create("Create");',
  14620. '$mod.TObject.GetIt(5);',
  14621. '$mod.Obj.$class.GetIt(6);',
  14622. '$mod.Obj.Sub.$class.GetIt(7);',
  14623. '$mod.TObject.Sub=null;',
  14624. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  14625. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  14626. '']));
  14627. end;
  14628. procedure TTestModule.TestClass_CallClassMethodStatic;
  14629. begin
  14630. StartProgram(false);
  14631. Add([
  14632. 'type',
  14633. ' TObject = class',
  14634. ' public',
  14635. ' class function Fly: tobject; static;',
  14636. ' end;',
  14637. 'class function tobject.Fly: tobject;',
  14638. 'begin',
  14639. ' Result.Fly;',
  14640. ' Result.Fly();',
  14641. ' Fly;',
  14642. ' Fly();',
  14643. ' Fly.Fly;',
  14644. ' Fly.Fly();',
  14645. 'end;',
  14646. 'var Obj: tobject;',
  14647. 'begin',
  14648. ' obj.Fly;',
  14649. ' obj.Fly();',
  14650. ' with obj do begin',
  14651. ' Fly;',
  14652. ' Fly();',
  14653. ' end;',
  14654. '']);
  14655. ConvertProgram;
  14656. CheckSource('TestClass_CallClassMethodStatic',
  14657. LinesToStr([ // statements
  14658. 'rtl.createClass(this, "TObject", null, function () {',
  14659. ' this.$init = function () {',
  14660. ' };',
  14661. ' this.$final = function () {',
  14662. ' };',
  14663. ' this.Fly = function () {',
  14664. ' var Result = null;',
  14665. ' $mod.TObject.Fly();',
  14666. ' $mod.TObject.Fly();',
  14667. ' $mod.TObject.Fly();',
  14668. ' $mod.TObject.Fly();',
  14669. ' $mod.TObject.Fly();',
  14670. ' $mod.TObject.Fly();',
  14671. ' return Result;',
  14672. ' };',
  14673. '});',
  14674. 'this.Obj = null;'
  14675. ]),
  14676. LinesToStr([ // $mod.$main
  14677. '$mod.TObject.Fly();',
  14678. '$mod.TObject.Fly();',
  14679. 'var $with = $mod.Obj;',
  14680. '$with.Fly();',
  14681. '$with.Fly();',
  14682. '']));
  14683. end;
  14684. procedure TTestModule.TestClass_Property;
  14685. begin
  14686. StartProgram(false);
  14687. Add('type');
  14688. Add(' TObject = class');
  14689. Add(' Fx: longint;');
  14690. Add(' Fy: longint;');
  14691. Add(' function GetInt: longint;');
  14692. Add(' procedure SetInt(Value: longint);');
  14693. Add(' procedure DoIt;');
  14694. Add(' property IntA: longint read Fx write Fy;');
  14695. Add(' property IntB: longint read GetInt write SetInt;');
  14696. Add(' end;');
  14697. Add('function tobject.getint: longint;');
  14698. Add('begin');
  14699. Add(' result:=fx;');
  14700. Add('end;');
  14701. Add('procedure tobject.setint(value: longint);');
  14702. Add('begin');
  14703. Add(' if value=fy then exit;');
  14704. Add(' fy:=value;');
  14705. Add('end;');
  14706. Add('procedure tobject.doit;');
  14707. Add('begin');
  14708. Add(' IntA:=IntA+1;');
  14709. Add(' Self.IntA:=Self.IntA+1;');
  14710. Add(' IntB:=IntB+1;');
  14711. Add(' Self.IntB:=Self.IntB+1;');
  14712. Add('end;');
  14713. Add('var Obj: tobject;');
  14714. Add('begin');
  14715. Add(' obj.inta:=obj.inta+1;');
  14716. Add(' if obj.intb=2 then;');
  14717. Add(' obj.intb:=obj.intb+2;');
  14718. Add(' obj.setint(obj.inta);');
  14719. ConvertProgram;
  14720. CheckSource('TestClass_Property',
  14721. LinesToStr([ // statements
  14722. 'rtl.createClass(this, "TObject", null, function () {',
  14723. ' this.$init = function () {',
  14724. ' this.Fx = 0;',
  14725. ' this.Fy = 0;',
  14726. ' };',
  14727. ' this.$final = function () {',
  14728. ' };',
  14729. ' this.GetInt = function () {',
  14730. ' var Result = 0;',
  14731. ' Result = this.Fx;',
  14732. ' return Result;',
  14733. ' };',
  14734. ' this.SetInt = function (Value) {',
  14735. ' if (Value === this.Fy) return;',
  14736. ' this.Fy = Value;',
  14737. ' };',
  14738. ' this.DoIt = function () {',
  14739. ' this.Fy = this.Fx + 1;',
  14740. ' this.Fy = this.Fx + 1;',
  14741. ' this.SetInt(this.GetInt() + 1);',
  14742. ' this.SetInt(this.GetInt() + 1);',
  14743. ' };',
  14744. '});',
  14745. 'this.Obj = null;'
  14746. ]),
  14747. LinesToStr([ // $mod.$main
  14748. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  14749. 'if ($mod.Obj.GetInt() === 2);',
  14750. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  14751. '$mod.Obj.SetInt($mod.Obj.Fx);'
  14752. ]));
  14753. end;
  14754. procedure TTestModule.TestClass_Property_ClassMethod;
  14755. begin
  14756. StartProgram(false);
  14757. Add([
  14758. 'type',
  14759. ' TObject = class',
  14760. ' class var Fx: longint;',
  14761. ' class var Fy: longint;',
  14762. ' class function GetInt: longint;',
  14763. ' class procedure SetInt(Value: longint);',
  14764. ' end;',
  14765. ' TBird = class',
  14766. ' class procedure DoIt;',
  14767. ' class property IntA: longint read Fx write Fy;',
  14768. ' class property IntB: longint read GetInt write SetInt;',
  14769. ' end;',
  14770. 'class function tobject.getint: longint;',
  14771. 'begin',
  14772. ' result:=fx;',
  14773. 'end;',
  14774. 'class procedure tobject.setint(value: longint);',
  14775. 'begin',
  14776. 'end;',
  14777. 'class procedure tbird.doit;',
  14778. 'begin',
  14779. ' FX:=3;',
  14780. ' IntA:=IntA+1;',
  14781. ' Self.IntA:=Self.IntA+1;',
  14782. ' IntB:=IntB+1;',
  14783. ' Self.IntB:=Self.IntB+1;',
  14784. ' with Self do begin',
  14785. ' FX:=11;',
  14786. ' IntA:=IntA+12;',
  14787. ' IntB:=IntB+13;',
  14788. ' end;',
  14789. 'end;',
  14790. 'var Obj: tbird;',
  14791. 'begin',
  14792. ' tbird.fx:=tbird.fx+1;',
  14793. ' tbird.inta:=tbird.inta+1;',
  14794. ' if tbird.intb=2 then;',
  14795. ' tbird.intb:=tbird.intb+2;',
  14796. ' tbird.setint(tbird.inta);',
  14797. ' obj.inta:=obj.inta+1;',
  14798. ' if obj.intb=2 then;',
  14799. ' obj.intb:=obj.intb+2;',
  14800. ' obj.setint(obj.inta);',
  14801. ' with Tbird do begin',
  14802. ' FX:=FY+1;',
  14803. ' inta:=inta+2;',
  14804. ' intb:=intb+3;',
  14805. ' end;',
  14806. ' with Obj do begin',
  14807. ' FX:=FY+1;',
  14808. ' inta:=inta+2;',
  14809. ' intb:=intb+3;',
  14810. ' end;',
  14811. '']);
  14812. ConvertProgram;
  14813. CheckSource('TestClass_Property_ClassMethod',
  14814. LinesToStr([ // statements
  14815. 'rtl.createClass(this, "TObject", null, function () {',
  14816. ' this.Fx = 0;',
  14817. ' this.Fy = 0;',
  14818. ' this.$init = function () {',
  14819. ' };',
  14820. ' this.$final = function () {',
  14821. ' };',
  14822. ' this.GetInt = function () {',
  14823. ' var Result = 0;',
  14824. ' Result = this.Fx;',
  14825. ' return Result;',
  14826. ' };',
  14827. ' this.SetInt = function (Value) {',
  14828. ' };',
  14829. '});',
  14830. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14831. ' this.DoIt = function () {',
  14832. ' $mod.TObject.Fx = 3;',
  14833. ' $mod.TObject.Fy = this.Fx + 1;',
  14834. ' $mod.TObject.Fy = this.Fx + 1;',
  14835. ' this.SetInt(this.GetInt() + 1);',
  14836. ' this.SetInt(this.GetInt() + 1);',
  14837. ' $mod.TObject.Fx = 11;',
  14838. ' $mod.TObject.Fy = this.Fx + 12;',
  14839. ' this.SetInt(this.GetInt() + 13);',
  14840. ' };',
  14841. '});',
  14842. 'this.Obj = null;'
  14843. ]),
  14844. LinesToStr([ // $mod.$main
  14845. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  14846. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  14847. 'if ($mod.TBird.GetInt() === 2);',
  14848. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  14849. '$mod.TBird.SetInt($mod.TBird.Fx);',
  14850. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  14851. 'if ($mod.Obj.$class.GetInt() === 2);',
  14852. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  14853. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  14854. 'var $with = $mod.TBird;',
  14855. '$mod.TObject.Fx = $with.Fy + 1;',
  14856. '$mod.TObject.Fy = $with.Fx + 2;',
  14857. '$with.SetInt($with.GetInt() + 3);',
  14858. 'var $with1 = $mod.Obj;',
  14859. '$mod.TObject.Fx = $with1.Fy + 1;',
  14860. '$mod.TObject.Fy = $with1.Fx + 2;',
  14861. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  14862. '']));
  14863. end;
  14864. procedure TTestModule.TestClass_Property_ClassMethodStatic;
  14865. begin
  14866. StartProgram(false);
  14867. Add([
  14868. 'type',
  14869. ' TObject = class',
  14870. ' class function GetInt: longint; static;',
  14871. ' class procedure SetInt(Value: longint); static;',
  14872. ' class function GetItems(Index: word): longint; static;',
  14873. ' class procedure SetItems(Index: word; const Value: longint); static;',
  14874. ' end;',
  14875. ' TBird = class',
  14876. ' class procedure Fly;',
  14877. ' class property IntA: longint read GetInt write SetInt;',
  14878. ' class property Items[Index: word]: longint read GetItems write SetItems;',
  14879. ' end;',
  14880. 'class function tobject.getint: longint;',
  14881. 'begin',
  14882. 'end;',
  14883. 'class procedure tobject.setint(value: longint);',
  14884. 'begin',
  14885. 'end;',
  14886. 'class function tobject.GetItems(Index: word): longint;',
  14887. 'begin',
  14888. 'end;',
  14889. 'class procedure TObject.SetItems(Index: word; const Value: longint);',
  14890. 'begin',
  14891. 'end;',
  14892. 'class procedure tbird.fly;',
  14893. 'var w: longint;',
  14894. 'begin',
  14895. ' inta:=inta+51;',
  14896. ' w:=items[52];',
  14897. ' items[53]:=54;',
  14898. 'end;',
  14899. 'var Obj: tbird;',
  14900. ' i: longint;',
  14901. 'begin',
  14902. ' tbird.inta:=tbird.inta+1;',
  14903. ' i:=tbird.items[2];',
  14904. ' tbird.items[3]:=4;',
  14905. ' obj.inta:=obj.inta+11;',
  14906. ' i:=obj.items[12];',
  14907. ' obj.items[13]:=14;',
  14908. ' with Tbird do begin',
  14909. ' inta:=inta+21;',
  14910. ' i:=items[22];',
  14911. ' items[23]:=24;',
  14912. ' end;',
  14913. ' with Obj do begin',
  14914. ' inta:=inta+31;',
  14915. ' i:=items[32];',
  14916. ' items[33]:=34;',
  14917. ' end;',
  14918. '']);
  14919. ConvertProgram;
  14920. CheckSource('TestClass_Property_ClassMethod',
  14921. LinesToStr([ // statements
  14922. 'rtl.createClass(this, "TObject", null, function () {',
  14923. ' this.$init = function () {',
  14924. ' };',
  14925. ' this.$final = function () {',
  14926. ' };',
  14927. ' this.GetInt = function () {',
  14928. ' var Result = 0;',
  14929. ' return Result;',
  14930. ' };',
  14931. ' this.SetInt = function (Value) {',
  14932. ' };',
  14933. ' this.GetItems = function (Index) {',
  14934. ' var Result = 0;',
  14935. ' return Result;',
  14936. ' };',
  14937. ' this.SetItems = function (Index, Value) {',
  14938. ' };',
  14939. '});',
  14940. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14941. ' this.Fly = function () {',
  14942. ' var w = 0;',
  14943. ' this.SetInt(this.GetInt() + 51);',
  14944. ' w = this.GetItems(52);',
  14945. ' this.SetItems(53, 54);',
  14946. ' };',
  14947. '});',
  14948. 'this.Obj = null;',
  14949. 'this.i = 0;',
  14950. '']),
  14951. LinesToStr([ // $mod.$main
  14952. '$mod.TObject.SetInt($mod.TObject.GetInt() + 1);',
  14953. '$mod.i = $mod.TObject.GetItems(2);',
  14954. '$mod.TObject.SetItems(3, 4);',
  14955. '$mod.TObject.SetInt($mod.TObject.GetInt() + 11);',
  14956. '$mod.i = $mod.TObject.GetItems(12);',
  14957. '$mod.TObject.SetItems(13, 14);',
  14958. 'var $with = $mod.TBird;',
  14959. '$with.SetInt($with.GetInt() + 21);',
  14960. '$mod.i = $with.GetItems(22);',
  14961. '$with.SetItems(23, 24);',
  14962. 'var $with1 = $mod.Obj;',
  14963. '$with1.SetInt($with1.GetInt() + 31);',
  14964. '$mod.i = $with1.GetItems(32);',
  14965. '$with1.SetItems(33, 34);',
  14966. '']));
  14967. end;
  14968. procedure TTestModule.TestClass_Property_Indexed;
  14969. begin
  14970. StartProgram(false);
  14971. Add([
  14972. 'type',
  14973. ' TObject = class',
  14974. ' FItems: array of longint;',
  14975. ' function GetItems(Index: longint): longint;',
  14976. ' procedure SetItems(Index: longint; Value: longint);',
  14977. ' procedure DoIt;',
  14978. ' property Items[Index: longint]: longint read getitems write setitems;',
  14979. ' end;',
  14980. 'function tobject.getitems(index: longint): longint;',
  14981. 'begin',
  14982. ' Result:=fitems[index];',
  14983. 'end;',
  14984. 'procedure tobject.setitems(index: longint; value: longint);',
  14985. 'begin',
  14986. ' fitems[index]:=value;',
  14987. 'end;',
  14988. 'procedure tobject.doit;',
  14989. 'begin',
  14990. ' items[1]:=2;',
  14991. ' items[3]:=items[4];',
  14992. ' self.items[5]:=self.items[6];',
  14993. ' items[items[7]]:=items[items[8]];',
  14994. 'end;',
  14995. 'var Obj: tobject;',
  14996. 'begin',
  14997. ' obj.Items[11]:=obj.Items[12];',
  14998. '']);
  14999. ConvertProgram;
  15000. CheckSource('TestClass_Property_Indexed',
  15001. LinesToStr([ // statements
  15002. 'rtl.createClass(this, "TObject", null, function () {',
  15003. ' this.$init = function () {',
  15004. ' this.FItems = [];',
  15005. ' };',
  15006. ' this.$final = function () {',
  15007. ' this.FItems = undefined;',
  15008. ' };',
  15009. ' this.GetItems = function (Index) {',
  15010. ' var Result = 0;',
  15011. ' Result = this.FItems[Index];',
  15012. ' return Result;',
  15013. ' };',
  15014. ' this.SetItems = function (Index, Value) {',
  15015. ' this.FItems[Index] = Value;',
  15016. ' };',
  15017. ' this.DoIt = function () {',
  15018. ' this.SetItems(1, 2);',
  15019. ' this.SetItems(3,this.GetItems(4));',
  15020. ' this.SetItems(5,this.GetItems(6));',
  15021. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  15022. ' };',
  15023. '});',
  15024. 'this.Obj = null;'
  15025. ]),
  15026. LinesToStr([ // $mod.$main
  15027. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  15028. ]));
  15029. end;
  15030. procedure TTestModule.TestClass_Property_IndexSpec;
  15031. begin
  15032. StartProgram(false);
  15033. Add([
  15034. 'type',
  15035. ' TEnum = (red, blue);',
  15036. ' TObject = class',
  15037. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  15038. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  15039. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  15040. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  15041. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  15042. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  15043. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  15044. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  15045. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  15046. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  15047. ' end;',
  15048. 'procedure DoIt(b: boolean); begin end;',
  15049. 'var',
  15050. ' o: TObject;',
  15051. 'begin',
  15052. ' o.B1:=o.B1;',
  15053. ' o.B2:=o.B2;',
  15054. ' o.B3:=o.B3;',
  15055. ' o.I1[''a'']:=o.I1[''b''];',
  15056. ' doit(o.b1);',
  15057. ' doit(o.b2);',
  15058. ' doit(o.i1[''c'']);',
  15059. '']);
  15060. ConvertProgram;
  15061. CheckSource('TestClass_Property_IndexSpec',
  15062. LinesToStr([ // statements
  15063. 'this.TEnum = {',
  15064. ' "0": "red",',
  15065. ' red: 0,',
  15066. ' "1": "blue",',
  15067. ' blue: 1',
  15068. '};',
  15069. 'rtl.createClass(this, "TObject", null, function () {',
  15070. ' this.$init = function () {',
  15071. ' };',
  15072. ' this.$final = function () {',
  15073. ' };',
  15074. '});',
  15075. 'this.DoIt = function (b) {',
  15076. '};',
  15077. 'this.o = null;',
  15078. '']),
  15079. LinesToStr([ // $mod.$main
  15080. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  15081. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  15082. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  15083. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  15084. '$mod.DoIt($mod.o.GetIntBool(1));',
  15085. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  15086. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  15087. '']));
  15088. end;
  15089. procedure TTestModule.TestClass_PropertyOfTypeArray;
  15090. begin
  15091. StartProgram(false);
  15092. Add('type');
  15093. Add(' TArray = array of longint;');
  15094. Add(' TObject = class');
  15095. Add(' FItems: TArray;');
  15096. Add(' function GetItems: tarray;');
  15097. Add(' procedure SetItems(Value: tarray);');
  15098. Add(' property Items: tarray read getitems write setitems;');
  15099. Add(' procedure SetNumbers(const Value: tarray);');
  15100. Add(' property Numbers: tarray write setnumbers;');
  15101. Add(' end;');
  15102. Add('function tobject.getitems: tarray;');
  15103. Add('begin');
  15104. Add(' Result:=fitems;');
  15105. Add('end;');
  15106. Add('procedure tobject.setitems(value: tarray);');
  15107. Add('begin');
  15108. Add(' fitems:=value;');
  15109. Add(' fitems:=nil;');
  15110. Add(' Items:=nil;');
  15111. Add(' Items:=Items;');
  15112. Add(' Items[1]:=2;');
  15113. Add(' fitems[3]:=Items[4];');
  15114. Add(' Items[5]:=Items[6];');
  15115. Add(' Self.Items[7]:=8;');
  15116. Add(' Self.Items[9]:=Self.Items[10];');
  15117. Add(' Items[Items[11]]:=Items[Items[12]];');
  15118. Add('end;');
  15119. Add('procedure tobject.SetNumbers(const Value: tarray);');
  15120. Add('begin;');
  15121. Add(' Numbers:=nil;');
  15122. Add(' Numbers:=Value;');
  15123. Add(' Self.Numbers:=Value;');
  15124. Add('end;');
  15125. Add('var Obj: tobject;');
  15126. Add('begin');
  15127. Add(' obj.items:=nil;');
  15128. Add(' obj.items:=obj.items;');
  15129. Add(' obj.items[11]:=obj.items[12];');
  15130. ConvertProgram;
  15131. CheckSource('TestClass_PropertyOfTypeArray',
  15132. LinesToStr([ // statements
  15133. 'rtl.createClass(this, "TObject", null, function () {',
  15134. ' this.$init = function () {',
  15135. ' this.FItems = [];',
  15136. ' };',
  15137. ' this.$final = function () {',
  15138. ' this.FItems = undefined;',
  15139. ' };',
  15140. ' this.GetItems = function () {',
  15141. ' var Result = [];',
  15142. ' Result = rtl.arrayRef(this.FItems);',
  15143. ' return Result;',
  15144. ' };',
  15145. ' this.SetItems = function (Value) {',
  15146. ' this.FItems = rtl.arrayRef(Value);',
  15147. ' this.FItems = [];',
  15148. ' this.SetItems([]);',
  15149. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  15150. ' this.GetItems()[1] = 2;',
  15151. ' this.FItems[3] = this.GetItems()[4];',
  15152. ' this.GetItems()[5] = this.GetItems()[6];',
  15153. ' this.GetItems()[7] = 8;',
  15154. ' this.GetItems()[9] = this.GetItems()[10];',
  15155. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  15156. ' };',
  15157. ' this.SetNumbers = function (Value) {',
  15158. ' this.SetNumbers([]);',
  15159. ' this.SetNumbers(Value);',
  15160. ' this.SetNumbers(Value);',
  15161. ' };',
  15162. '});',
  15163. 'this.Obj = null;'
  15164. ]),
  15165. LinesToStr([ // $mod.$main
  15166. '$mod.Obj.SetItems([]);',
  15167. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  15168. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  15169. ]));
  15170. end;
  15171. procedure TTestModule.TestClass_PropertyDefault;
  15172. begin
  15173. StartProgram(false);
  15174. Add([
  15175. 'type',
  15176. ' TArray = array of longint;',
  15177. ' TObject = class',
  15178. ' end;',
  15179. ' TBird = class',
  15180. ' FItems: TArray;',
  15181. ' function GetItems(Index: longint): longint;',
  15182. ' procedure SetItems(Index, Value: longint);',
  15183. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  15184. ' end;',
  15185. 'function TBird.getitems(index: longint): longint;',
  15186. 'begin',
  15187. 'end;',
  15188. 'procedure TBird.setitems(index, value: longint);',
  15189. 'begin',
  15190. ' Self[1]:=2;',
  15191. ' Self[3]:=Self[index];',
  15192. ' Self[index]:=Self[Self[value]];',
  15193. ' Self[Self[4]]:=value;',
  15194. 'end;',
  15195. 'var',
  15196. ' Bird: TBird;',
  15197. ' Obj: TObject;',
  15198. 'begin',
  15199. ' bird[11]:=12;',
  15200. ' bird[13]:=bird[14];',
  15201. ' bird[Bird[15]]:=bird[Bird[15]];',
  15202. ' TBird(obj)[16]:=TBird(obj)[17];',
  15203. ' (obj as tbird)[18]:=19;',
  15204. '']);
  15205. ConvertProgram;
  15206. CheckSource('TestClass_PropertyDefault',
  15207. LinesToStr([ // statements
  15208. 'rtl.createClass(this, "TObject", null, function () {',
  15209. ' this.$init = function () {',
  15210. ' };',
  15211. ' this.$final = function () {',
  15212. ' };',
  15213. '});',
  15214. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15215. ' this.$init = function () {',
  15216. ' $mod.TObject.$init.call(this);',
  15217. ' this.FItems = [];',
  15218. ' };',
  15219. ' this.$final = function () {',
  15220. ' this.FItems = undefined;',
  15221. ' $mod.TObject.$final.call(this);',
  15222. ' };',
  15223. ' this.GetItems = function (Index) {',
  15224. ' var Result = 0;',
  15225. ' return Result;',
  15226. ' };',
  15227. ' this.SetItems = function (Index, Value) {',
  15228. ' this.SetItems(1, 2);',
  15229. ' this.SetItems(3, this.GetItems(Index));',
  15230. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  15231. ' this.SetItems(this.GetItems(4), Value);',
  15232. ' };',
  15233. '});',
  15234. 'this.Bird = null;',
  15235. 'this.Obj = null;',
  15236. '']),
  15237. LinesToStr([ // $mod.$main
  15238. '$mod.Bird.SetItems(11, 12);',
  15239. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  15240. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  15241. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  15242. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  15243. '']));
  15244. end;
  15245. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  15246. begin
  15247. StartProgram(false);
  15248. Add([
  15249. 'type',
  15250. ' TObject = class end;',
  15251. ' TAlphaList = class',
  15252. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  15253. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  15254. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  15255. ' end;',
  15256. ' TBetaList = class',
  15257. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  15258. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  15259. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  15260. ' end;',
  15261. ' TBird = class',
  15262. ' procedure DoIt;',
  15263. ' end;',
  15264. 'procedure TBird.DoIt;',
  15265. 'var',
  15266. ' List: TAlphaList;',
  15267. 'begin',
  15268. ' if TBetaList(List[true])[3]=nil then ;',
  15269. ' TBetaList(List[false])[5]:=nil;',
  15270. 'end;',
  15271. 'var',
  15272. ' List: TAlphaList;',
  15273. 'begin',
  15274. ' if TBetaList(List[true])[3]=nil then ;',
  15275. ' TBetaList(List[false])[5]:=nil;',
  15276. '']);
  15277. ConvertProgram;
  15278. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  15279. LinesToStr([ // statements
  15280. 'rtl.createClass(this, "TObject", null, function () {',
  15281. ' this.$init = function () {',
  15282. ' };',
  15283. ' this.$final = function () {',
  15284. ' };',
  15285. '});',
  15286. 'rtl.createClass(this, "TAlphaList", this.TObject, function () {',
  15287. '});',
  15288. 'rtl.createClass(this, "TBetaList", this.TObject, function () {',
  15289. '});',
  15290. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15291. ' this.DoIt = function () {',
  15292. ' var List = null;',
  15293. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  15294. ' List.GetAlphas(false).SetBetas(5, null);',
  15295. ' };',
  15296. '});',
  15297. 'this.List = null;',
  15298. '']),
  15299. LinesToStr([ // $mod.$main
  15300. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  15301. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  15302. '']));
  15303. end;
  15304. procedure TTestModule.TestClass_PropertyOverride;
  15305. begin
  15306. StartProgram(false);
  15307. Add('type');
  15308. Add(' integer = longint;');
  15309. Add(' TObject = class');
  15310. Add(' FItem: integer;');
  15311. Add(' function GetItem: integer; external name ''GetItem'';');
  15312. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  15313. Add(' property Item: integer read getitem write setitem;');
  15314. Add(' end;');
  15315. Add(' TCar = class');
  15316. Add(' FBag: integer;');
  15317. Add(' function GetBag: integer; external name ''GetBag'';');
  15318. Add(' property Item read getbag;');
  15319. Add(' end;');
  15320. Add('var');
  15321. Add(' Obj: tobject;');
  15322. Add(' Car: tcar;');
  15323. Add('begin');
  15324. Add(' Obj.Item:=Obj.Item;');
  15325. Add(' Car.Item:=Car.Item;');
  15326. ConvertProgram;
  15327. CheckSource('TestClass_PropertyOverride',
  15328. LinesToStr([ // statements
  15329. 'rtl.createClass(this, "TObject", null, function () {',
  15330. ' this.$init = function () {',
  15331. ' this.FItem = 0;',
  15332. ' };',
  15333. ' this.$final = function () {',
  15334. ' };',
  15335. '});',
  15336. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15337. ' this.$init = function () {',
  15338. ' $mod.TObject.$init.call(this);',
  15339. ' this.FBag = 0;',
  15340. ' };',
  15341. '});',
  15342. 'this.Obj = null;',
  15343. 'this.Car = null;',
  15344. '']),
  15345. LinesToStr([ // $mod.$main
  15346. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  15347. '$mod.Car.SetItem($mod.Car.GetBag());',
  15348. '']));
  15349. end;
  15350. procedure TTestModule.TestClass_PropertyIncVisibility;
  15351. begin
  15352. AddModuleWithIntfImplSrc('unit1.pp',
  15353. LinesToStr([
  15354. 'type',
  15355. ' TNumber = longint;',
  15356. ' TInteger = longint;',
  15357. ' TObject = class',
  15358. ' private',
  15359. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  15360. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  15361. ' protected',
  15362. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  15363. ' end;']),
  15364. LinesToStr([
  15365. '']));
  15366. StartProgram(true);
  15367. Add([
  15368. 'uses unit1;',
  15369. 'type',
  15370. ' TBird = class',
  15371. ' public',
  15372. ' property Items;',
  15373. ' end;',
  15374. 'procedure DoIt(i: TInteger);',
  15375. 'begin',
  15376. 'end;',
  15377. 'var b: TBird;',
  15378. 'begin',
  15379. ' b.Items[1]:=2;',
  15380. ' b.Items[3]:=b.Items[4];',
  15381. ' DoIt(b.Items[5]);',
  15382. '']);
  15383. ConvertProgram;
  15384. CheckSource('TestClass_PropertyIncVisibility',
  15385. LinesToStr([ // statements
  15386. 'rtl.createClass(this, "TBird", pas.unit1.TObject, function () {',
  15387. '});',
  15388. 'this.DoIt = function (i) {',
  15389. '};',
  15390. 'this.b = null;'
  15391. ]),
  15392. LinesToStr([ // $mod.$main
  15393. '$mod.b.SetItems(1, 2);',
  15394. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  15395. '$mod.DoIt($mod.b.GetItems(5));'
  15396. ]));
  15397. end;
  15398. procedure TTestModule.TestClass_Assigned;
  15399. begin
  15400. StartProgram(false);
  15401. Add('type');
  15402. Add(' TObject = class');
  15403. Add(' end;');
  15404. Add('var');
  15405. Add(' Obj: tobject;');
  15406. Add(' b: boolean;');
  15407. Add('begin');
  15408. Add(' if Assigned(obj) then ;');
  15409. Add(' b:=Assigned(obj) or false;');
  15410. ConvertProgram;
  15411. CheckSource('TestClass_Assigned',
  15412. LinesToStr([ // statements
  15413. 'rtl.createClass(this, "TObject", null, function () {',
  15414. ' this.$init = function () {',
  15415. ' };',
  15416. ' this.$final = function () {',
  15417. ' };',
  15418. '});',
  15419. 'this.Obj = null;',
  15420. 'this.b = false;'
  15421. ]),
  15422. LinesToStr([ // $mod.$main
  15423. 'if ($mod.Obj != null);',
  15424. '$mod.b = ($mod.Obj != null) || false;'
  15425. ]));
  15426. end;
  15427. procedure TTestModule.TestClass_WithClassDoCreate;
  15428. begin
  15429. StartProgram(false);
  15430. Add('type');
  15431. Add(' TObject = class');
  15432. Add(' aBool: boolean;');
  15433. Add(' Arr: array of boolean;');
  15434. Add(' constructor Create;');
  15435. Add(' end;');
  15436. Add('constructor TObject.Create; begin end;');
  15437. Add('var');
  15438. Add(' Obj: tobject;');
  15439. Add(' b: boolean;');
  15440. Add('begin');
  15441. Add(' with tobject.create do begin');
  15442. Add(' b:=abool;');
  15443. Add(' abool:=b;');
  15444. Add(' b:=arr[1];');
  15445. Add(' arr[2]:=b;');
  15446. Add(' end;');
  15447. Add(' with tobject do');
  15448. Add(' obj:=create;');
  15449. Add(' with obj do begin');
  15450. Add(' create;');
  15451. Add(' b:=abool;');
  15452. Add(' abool:=b;');
  15453. Add(' b:=arr[3];');
  15454. Add(' arr[4]:=b;');
  15455. Add(' end;');
  15456. ConvertProgram;
  15457. CheckSource('TestClass_WithClassDoCreate',
  15458. LinesToStr([ // statements
  15459. 'rtl.createClass(this, "TObject", null, function () {',
  15460. ' this.$init = function () {',
  15461. ' this.aBool = false;',
  15462. ' this.Arr = [];',
  15463. ' };',
  15464. ' this.$final = function () {',
  15465. ' this.Arr = undefined;',
  15466. ' };',
  15467. ' this.Create = function () {',
  15468. ' return this;',
  15469. ' };',
  15470. '});',
  15471. 'this.Obj = null;',
  15472. 'this.b = false;'
  15473. ]),
  15474. LinesToStr([ // $mod.$main
  15475. 'var $with = $mod.TObject.$create("Create");',
  15476. '$mod.b = $with.aBool;',
  15477. '$with.aBool = $mod.b;',
  15478. '$mod.b = $with.Arr[1];',
  15479. '$with.Arr[2] = $mod.b;',
  15480. 'var $with1 = $mod.TObject;',
  15481. '$mod.Obj = $with1.$create("Create");',
  15482. 'var $with2 = $mod.Obj;',
  15483. '$with2.Create();',
  15484. '$mod.b = $with2.aBool;',
  15485. '$with2.aBool = $mod.b;',
  15486. '$mod.b = $with2.Arr[3];',
  15487. '$with2.Arr[4] = $mod.b;',
  15488. '']));
  15489. end;
  15490. procedure TTestModule.TestClass_WithClassInstDoProperty;
  15491. begin
  15492. StartProgram(false);
  15493. Add('type');
  15494. Add(' TObject = class');
  15495. Add(' FInt: longint;');
  15496. Add(' constructor Create;');
  15497. Add(' function GetSize: longint;');
  15498. Add(' procedure SetSize(Value: longint);');
  15499. Add(' property Int: longint read FInt write FInt;');
  15500. Add(' property Size: longint read GetSize write SetSize;');
  15501. Add(' end;');
  15502. Add('constructor TObject.Create; begin end;');
  15503. Add('function TObject.GetSize: longint; begin; end;');
  15504. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  15505. Add('var');
  15506. Add(' Obj: tobject;');
  15507. Add(' i: longint;');
  15508. Add('begin');
  15509. Add(' with TObject.Create do begin');
  15510. Add(' i:=int;');
  15511. Add(' int:=i;');
  15512. Add(' i:=size;');
  15513. Add(' size:=i;');
  15514. Add(' end;');
  15515. Add(' with obj do begin');
  15516. Add(' i:=int;');
  15517. Add(' int:=i;');
  15518. Add(' i:=size;');
  15519. Add(' size:=i;');
  15520. Add(' end;');
  15521. ConvertProgram;
  15522. CheckSource('TestClass_WithClassInstDoProperty',
  15523. LinesToStr([ // statements
  15524. 'rtl.createClass(this, "TObject", null, function () {',
  15525. ' this.$init = function () {',
  15526. ' this.FInt = 0;',
  15527. ' };',
  15528. ' this.$final = function () {',
  15529. ' };',
  15530. ' this.Create = function () {',
  15531. ' return this;',
  15532. ' };',
  15533. ' this.GetSize = function () {',
  15534. ' var Result = 0;',
  15535. ' return Result;',
  15536. ' };',
  15537. ' this.SetSize = function (Value) {',
  15538. ' };',
  15539. '});',
  15540. 'this.Obj = null;',
  15541. 'this.i = 0;'
  15542. ]),
  15543. LinesToStr([ // $mod.$main
  15544. 'var $with = $mod.TObject.$create("Create");',
  15545. '$mod.i = $with.FInt;',
  15546. '$with.FInt = $mod.i;',
  15547. '$mod.i = $with.GetSize();',
  15548. '$with.SetSize($mod.i);',
  15549. 'var $with1 = $mod.Obj;',
  15550. '$mod.i = $with1.FInt;',
  15551. '$with1.FInt = $mod.i;',
  15552. '$mod.i = $with1.GetSize();',
  15553. '$with1.SetSize($mod.i);',
  15554. '']));
  15555. end;
  15556. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  15557. begin
  15558. StartProgram(false);
  15559. Add('type');
  15560. Add(' TObject = class');
  15561. Add(' constructor Create;');
  15562. Add(' function GetItems(Index: longint): longint;');
  15563. Add(' procedure SetItems(Index, Value: longint);');
  15564. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  15565. Add(' end;');
  15566. Add('constructor TObject.Create; begin end;');
  15567. Add('function tobject.getitems(index: longint): longint; begin; end;');
  15568. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  15569. Add('var');
  15570. Add(' Obj: tobject;');
  15571. Add(' i: longint;');
  15572. Add('begin');
  15573. Add(' with TObject.Create do begin');
  15574. Add(' i:=Items[1];');
  15575. Add(' Items[2]:=i;');
  15576. Add(' end;');
  15577. Add(' with obj do begin');
  15578. Add(' i:=Items[3];');
  15579. Add(' Items[4]:=i;');
  15580. Add(' end;');
  15581. ConvertProgram;
  15582. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  15583. LinesToStr([ // statements
  15584. 'rtl.createClass(this, "TObject", null, function () {',
  15585. ' this.$init = function () {',
  15586. ' };',
  15587. ' this.$final = function () {',
  15588. ' };',
  15589. ' this.Create = function () {',
  15590. ' return this;',
  15591. ' };',
  15592. ' this.GetItems = function (Index) {',
  15593. ' var Result = 0;',
  15594. ' return Result;',
  15595. ' };',
  15596. ' this.SetItems = function (Index, Value) {',
  15597. ' };',
  15598. '});',
  15599. 'this.Obj = null;',
  15600. 'this.i = 0;'
  15601. ]),
  15602. LinesToStr([ // $mod.$main
  15603. 'var $with = $mod.TObject.$create("Create");',
  15604. '$mod.i = $with.GetItems(1);',
  15605. '$with.SetItems(2, $mod.i);',
  15606. 'var $with1 = $mod.Obj;',
  15607. '$mod.i = $with1.GetItems(3);',
  15608. '$with1.SetItems(4, $mod.i);',
  15609. '']));
  15610. end;
  15611. procedure TTestModule.TestClass_WithClassInstDoFunc;
  15612. begin
  15613. StartProgram(false);
  15614. Add('type');
  15615. Add(' TObject = class');
  15616. Add(' constructor Create;');
  15617. Add(' function GetSize: longint;');
  15618. Add(' procedure SetSize(Value: longint);');
  15619. Add(' end;');
  15620. Add('constructor TObject.Create; begin end;');
  15621. Add('function TObject.GetSize: longint; begin; end;');
  15622. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  15623. Add('var');
  15624. Add(' Obj: tobject;');
  15625. Add(' i: longint;');
  15626. Add('begin');
  15627. Add(' with TObject.Create do begin');
  15628. Add(' i:=GetSize;');
  15629. Add(' i:=GetSize();');
  15630. Add(' SetSize(i);');
  15631. Add(' end;');
  15632. Add(' with obj do begin');
  15633. Add(' i:=GetSize;');
  15634. Add(' i:=GetSize();');
  15635. Add(' SetSize(i);');
  15636. Add(' end;');
  15637. ConvertProgram;
  15638. CheckSource('TestClass_WithClassInstDoFunc',
  15639. LinesToStr([ // statements
  15640. 'rtl.createClass(this, "TObject", null, function () {',
  15641. ' this.$init = function () {',
  15642. ' };',
  15643. ' this.$final = function () {',
  15644. ' };',
  15645. ' this.Create = function () {',
  15646. ' return this;',
  15647. ' };',
  15648. ' this.GetSize = function () {',
  15649. ' var Result = 0;',
  15650. ' return Result;',
  15651. ' };',
  15652. ' this.SetSize = function (Value) {',
  15653. ' };',
  15654. '});',
  15655. 'this.Obj = null;',
  15656. 'this.i = 0;'
  15657. ]),
  15658. LinesToStr([ // $mod.$main
  15659. 'var $with = $mod.TObject.$create("Create");',
  15660. '$mod.i = $with.GetSize();',
  15661. '$mod.i = $with.GetSize();',
  15662. '$with.SetSize($mod.i);',
  15663. 'var $with1 = $mod.Obj;',
  15664. '$mod.i = $with1.GetSize();',
  15665. '$mod.i = $with1.GetSize();',
  15666. '$with1.SetSize($mod.i);',
  15667. '']));
  15668. end;
  15669. procedure TTestModule.TestClass_TypeCast;
  15670. begin
  15671. StartProgram(false);
  15672. Add('type');
  15673. Add(' TObject = class');
  15674. Add(' Next: TObject;');
  15675. Add(' constructor Create;');
  15676. Add(' end;');
  15677. Add(' TControl = class(TObject)');
  15678. Add(' Arr: array of TObject;');
  15679. Add(' function GetIt(vI: longint = 0): TObject;');
  15680. Add(' end;');
  15681. Add('constructor tobject.create; begin end;');
  15682. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  15683. Add('var');
  15684. Add(' Obj: tobject;');
  15685. Add('begin');
  15686. Add(' obj:=tcontrol(obj).next;');
  15687. Add(' tcontrol(obj):=nil;');
  15688. Add(' obj:=tcontrol(obj);');
  15689. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  15690. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  15691. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  15692. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  15693. Add(' obj:=tcontrol(nil);');
  15694. ConvertProgram;
  15695. CheckSource('TestClass_TypeCast',
  15696. LinesToStr([ // statements
  15697. 'rtl.createClass(this, "TObject", null, function () {',
  15698. ' this.$init = function () {',
  15699. ' this.Next = null;',
  15700. ' };',
  15701. ' this.$final = function () {',
  15702. ' this.Next = undefined;',
  15703. ' };',
  15704. ' this.Create = function () {',
  15705. ' return this;',
  15706. ' };',
  15707. '});',
  15708. 'rtl.createClass(this, "TControl", this.TObject, function () {',
  15709. ' this.$init = function () {',
  15710. ' $mod.TObject.$init.call(this);',
  15711. ' this.Arr = [];',
  15712. ' };',
  15713. ' this.$final = function () {',
  15714. ' this.Arr = undefined;',
  15715. ' $mod.TObject.$final.call(this);',
  15716. ' };',
  15717. ' this.GetIt = function (vI) {',
  15718. ' var Result = null;',
  15719. ' return Result;',
  15720. ' };',
  15721. '});',
  15722. 'this.Obj = null;'
  15723. ]),
  15724. LinesToStr([ // $mod.$main
  15725. '$mod.Obj = $mod.Obj.Next;',
  15726. '$mod.Obj = null;',
  15727. '$mod.Obj = $mod.Obj;',
  15728. '$mod.Obj = $mod.Obj.GetIt(0);',
  15729. '$mod.Obj = $mod.Obj.GetIt(0);',
  15730. '$mod.Obj = $mod.Obj.GetIt(1);',
  15731. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  15732. '$mod.Obj = null;',
  15733. '']));
  15734. end;
  15735. procedure TTestModule.TestClass_TypeCastUntypedParam;
  15736. begin
  15737. StartProgram(false);
  15738. Add('type');
  15739. Add(' TObject = class end;');
  15740. Add('procedure ProcA(var A);');
  15741. Add('begin');
  15742. Add(' TObject(A):=nil;');
  15743. Add(' TObject(A):=TObject(A);');
  15744. Add(' if TObject(A)=nil then ;');
  15745. Add(' if nil=TObject(A) then ;');
  15746. Add('end;');
  15747. Add('procedure ProcB(out A);');
  15748. Add('begin');
  15749. Add(' TObject(A):=nil;');
  15750. Add(' TObject(A):=TObject(A);');
  15751. Add(' if TObject(A)=nil then ;');
  15752. Add(' if nil=TObject(A) then ;');
  15753. Add('end;');
  15754. Add('procedure ProcC(const A);');
  15755. Add('begin');
  15756. Add(' if TObject(A)=nil then ;');
  15757. Add(' if nil=TObject(A) then ;');
  15758. Add('end;');
  15759. Add('var o: TObject;');
  15760. Add('begin');
  15761. Add(' ProcA(o);');
  15762. Add(' ProcB(o);');
  15763. Add(' ProcC(o);');
  15764. ConvertProgram;
  15765. CheckSource('TestClass_TypeCastUntypedParam',
  15766. LinesToStr([ // statements
  15767. 'rtl.createClass(this, "TObject", null, function () {',
  15768. ' this.$init = function () {',
  15769. ' };',
  15770. ' this.$final = function () {',
  15771. ' };',
  15772. '});',
  15773. 'this.ProcA = function (A) {',
  15774. ' A.set(null);',
  15775. ' A.set(A.get());',
  15776. ' if (A.get() === null);',
  15777. ' if (null === A.get());',
  15778. '};',
  15779. 'this.ProcB = function (A) {',
  15780. ' A.set(null);',
  15781. ' A.set(A.get());',
  15782. ' if (A.get() === null);',
  15783. ' if (null === A.get());',
  15784. '};',
  15785. 'this.ProcC = function (A) {',
  15786. ' if (A === null);',
  15787. ' if (null === A);',
  15788. '};',
  15789. 'this.o = null;',
  15790. '']),
  15791. LinesToStr([ // $mod.$main
  15792. '$mod.ProcA({',
  15793. ' p: $mod,',
  15794. ' get: function () {',
  15795. ' return this.p.o;',
  15796. ' },',
  15797. ' set: function (v) {',
  15798. ' this.p.o = v;',
  15799. ' }',
  15800. '});',
  15801. '$mod.ProcB({',
  15802. ' p: $mod,',
  15803. ' get: function () {',
  15804. ' return this.p.o;',
  15805. ' },',
  15806. ' set: function (v) {',
  15807. ' this.p.o = v;',
  15808. ' }',
  15809. '});',
  15810. '$mod.ProcC($mod.o);',
  15811. '']));
  15812. end;
  15813. procedure TTestModule.TestClass_Overloads;
  15814. begin
  15815. StartProgram(false);
  15816. Add('type');
  15817. Add(' TObject = class');
  15818. Add(' procedure DoIt;');
  15819. Add(' procedure DoIt(vI: longint);');
  15820. Add(' end;');
  15821. Add('procedure TObject.DoIt;');
  15822. Add('begin');
  15823. Add(' DoIt;');
  15824. Add(' DoIt(1);');
  15825. Add('end;');
  15826. Add('procedure TObject.DoIt(vI: longint); begin end;');
  15827. Add('begin');
  15828. ConvertProgram;
  15829. CheckSource('TestClass_Overloads',
  15830. LinesToStr([ // statements
  15831. 'rtl.createClass(this, "TObject", null, function () {',
  15832. ' this.$init = function () {',
  15833. ' };',
  15834. ' this.$final = function () {',
  15835. ' };',
  15836. ' this.DoIt = function () {',
  15837. ' this.DoIt();',
  15838. ' this.DoIt$1(1);',
  15839. ' };',
  15840. ' this.DoIt$1 = function (vI) {',
  15841. ' };',
  15842. '});',
  15843. '']),
  15844. LinesToStr([ // $mod.$main
  15845. '']));
  15846. end;
  15847. procedure TTestModule.TestClass_OverloadsAncestor;
  15848. begin
  15849. StartProgram(false);
  15850. Add('type');
  15851. Add(' TObject = class;');
  15852. Add(' TObject = class');
  15853. Add(' procedure DoIt(vA: longint);');
  15854. Add(' procedure DoIt(vA, vB: longint);');
  15855. Add(' end;');
  15856. Add(' TCar = class;');
  15857. Add(' TCar = class');
  15858. Add(' procedure DoIt(vA: longint);');
  15859. Add(' procedure DoIt(vA, vB: longint);');
  15860. Add(' end;');
  15861. Add('procedure tobject.doit(va: longint);');
  15862. Add('begin');
  15863. Add(' doit(1);');
  15864. Add(' doit(1,2);');
  15865. Add('end;');
  15866. Add('procedure tobject.doit(va, vb: longint); begin end;');
  15867. Add('procedure tcar.doit(va: longint);');
  15868. Add('begin');
  15869. Add(' doit(1);');
  15870. Add(' doit(1,2);');
  15871. Add(' inherited doit(1);');
  15872. Add(' inherited doit(1,2);');
  15873. Add('end;');
  15874. Add('procedure tcar.doit(va, vb: longint); begin end;');
  15875. Add('begin');
  15876. ConvertProgram;
  15877. CheckSource('TestClass_OverloadsAncestor',
  15878. LinesToStr([ // statements
  15879. 'rtl.createClass(this, "TObject", null, function () {',
  15880. ' this.$init = function () {',
  15881. ' };',
  15882. ' this.$final = function () {',
  15883. ' };',
  15884. ' this.DoIt = function (vA) {',
  15885. ' this.DoIt(1);',
  15886. ' this.DoIt$1(1,2);',
  15887. ' };',
  15888. ' this.DoIt$1 = function (vA, vB) {',
  15889. ' };',
  15890. '});',
  15891. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15892. ' this.DoIt$2 = function (vA) {',
  15893. ' this.DoIt$2(1);',
  15894. ' this.DoIt$3(1, 2);',
  15895. ' $mod.TObject.DoIt.call(this, 1);',
  15896. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  15897. ' };',
  15898. ' this.DoIt$3 = function (vA, vB) {',
  15899. ' };',
  15900. '});',
  15901. '']),
  15902. LinesToStr([ // $mod.$main
  15903. '']));
  15904. end;
  15905. procedure TTestModule.TestClass_OverloadConstructor;
  15906. begin
  15907. StartProgram(false);
  15908. Add('type');
  15909. Add(' TObject = class');
  15910. Add(' constructor Create(vA: longint);');
  15911. Add(' constructor Create(vA, vB: longint);');
  15912. Add(' end;');
  15913. Add(' TCar = class');
  15914. Add(' constructor Create(vA: longint);');
  15915. Add(' constructor Create(vA, vB: longint);');
  15916. Add(' end;');
  15917. Add('constructor tobject.create(va: longint);');
  15918. Add('begin');
  15919. Add(' create(1);');
  15920. Add(' create(1,2);');
  15921. Add('end;');
  15922. Add('constructor tobject.create(va, vb: longint); begin end;');
  15923. Add('constructor tcar.create(va: longint);');
  15924. Add('begin');
  15925. Add(' create(1);');
  15926. Add(' create(1,2);');
  15927. Add(' inherited create(1);');
  15928. Add(' inherited create(1,2);');
  15929. Add('end;');
  15930. Add('constructor tcar.create(va, vb: longint); begin end;');
  15931. Add('begin');
  15932. Add(' tobject.create(1);');
  15933. Add(' tobject.create(1,2);');
  15934. Add(' tcar.create(1);');
  15935. Add(' tcar.create(1,2);');
  15936. ConvertProgram;
  15937. CheckSource('TestClass_OverloadConstructor',
  15938. LinesToStr([ // statements
  15939. 'rtl.createClass(this, "TObject", null, function () {',
  15940. ' this.$init = function () {',
  15941. ' };',
  15942. ' this.$final = function () {',
  15943. ' };',
  15944. ' this.Create = function (vA) {',
  15945. ' this.Create(1);',
  15946. ' this.Create$1(1,2);',
  15947. ' return this;',
  15948. ' };',
  15949. ' this.Create$1 = function (vA, vB) {',
  15950. ' return this;',
  15951. ' };',
  15952. '});',
  15953. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15954. ' this.Create$2 = function (vA) {',
  15955. ' this.Create$2(1);',
  15956. ' this.Create$3(1, 2);',
  15957. ' $mod.TObject.Create.call(this, 1);',
  15958. ' $mod.TObject.Create$1.call(this, 1, 2);',
  15959. ' return this;',
  15960. ' };',
  15961. ' this.Create$3 = function (vA, vB) {',
  15962. ' return this;',
  15963. ' };',
  15964. '});',
  15965. '']),
  15966. LinesToStr([ // $mod.$main
  15967. '$mod.TObject.$create("Create", [1]);',
  15968. '$mod.TObject.$create("Create$1", [1, 2]);',
  15969. '$mod.TCar.$create("Create$2", [1]);',
  15970. '$mod.TCar.$create("Create$3", [1, 2]);',
  15971. '']));
  15972. end;
  15973. procedure TTestModule.TestClass_OverloadDelphiOverride;
  15974. begin
  15975. StartProgram(false);
  15976. Add([
  15977. '{$mode delphi}',
  15978. 'type',
  15979. ' TObject = class end;',
  15980. ' TBird = class',
  15981. ' function {#a}GetValue: longint; overload; virtual;',
  15982. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  15983. ' end;',
  15984. ' TEagle = class(TBird)',
  15985. ' function {#c}GetValue: longint; overload; override;',
  15986. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  15987. ' end;',
  15988. 'function TBird.GetValue: longint;',
  15989. 'begin',
  15990. ' if 3={@a}GetValue then ;',
  15991. ' if 4={@b}GetValue(5) then ;',
  15992. 'end;',
  15993. 'function TBird.GetValue(AValue: longint): longint;',
  15994. 'begin',
  15995. 'end;',
  15996. 'function TEagle.GetValue: longint;',
  15997. 'begin',
  15998. ' if 13={@c}GetValue then ;',
  15999. ' if 14={@d}GetValue(15) then ;',
  16000. ' if 15=inherited {@a}GetValue then ;',
  16001. ' if 16=inherited {@b}GetValue(17) then ;',
  16002. 'end;',
  16003. 'function TEagle.GetValue(AValue: longint): longint;',
  16004. 'begin',
  16005. 'end;',
  16006. 'var',
  16007. ' e: TEagle;',
  16008. 'begin',
  16009. ' if 23=e.{@c}GetValue then ;',
  16010. ' if 24=e.{@d}GetValue(25) then ;']);
  16011. ConvertProgram;
  16012. CheckSource('TestClass_OverloadDelphiOverride',
  16013. LinesToStr([ // statements
  16014. 'rtl.createClass(this, "TObject", null, function () {',
  16015. ' this.$init = function () {',
  16016. ' };',
  16017. ' this.$final = function () {',
  16018. ' };',
  16019. '});',
  16020. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16021. ' this.GetValue = function () {',
  16022. ' var Result = 0;',
  16023. ' if (3 === this.GetValue()) ;',
  16024. ' if (4 === this.GetValue$1(5)) ;',
  16025. ' return Result;',
  16026. ' };',
  16027. ' this.GetValue$1 = function (AValue) {',
  16028. ' var Result = 0;',
  16029. ' return Result;',
  16030. ' };',
  16031. '});',
  16032. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  16033. ' this.GetValue = function () {',
  16034. ' var Result = 0;',
  16035. ' if (13 === this.GetValue()) ;',
  16036. ' if (14 === this.GetValue$1(15)) ;',
  16037. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  16038. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  16039. ' return Result;',
  16040. ' };',
  16041. ' this.GetValue$1 = function (AValue) {',
  16042. ' var Result = 0;',
  16043. ' return Result;',
  16044. ' };',
  16045. '});',
  16046. 'this.e = null;',
  16047. '']),
  16048. LinesToStr([ // $mod.$main
  16049. 'if (23 === $mod.e.GetValue()) ;',
  16050. 'if (24 === $mod.e.GetValue$1(25)) ;',
  16051. '']));
  16052. end;
  16053. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  16054. begin
  16055. StartProgram(false);
  16056. Add([
  16057. '{$mode delphi}',
  16058. 'type',
  16059. ' TObject = class end;',
  16060. ' TAnimal = class',
  16061. ' public',
  16062. ' {#animal_a}A: longint;',
  16063. ' function {#animal_b}B: longint;',
  16064. ' end;',
  16065. ' TBird = class(TAnimal)',
  16066. ' public',
  16067. ' {#bird_a}A: double;',
  16068. ' {#bird_b}B: boolean;',
  16069. ' end;',
  16070. ' TEagle = class(TBird)',
  16071. ' public',
  16072. ' function {#eagle_a}A: boolean;',
  16073. ' {#eagle_b}B: double;',
  16074. ' end;',
  16075. 'function TAnimal.B: longint;',
  16076. 'begin',
  16077. 'end;',
  16078. 'function TEagle.A: boolean;',
  16079. 'begin',
  16080. ' {@eagle_b}B:=3.3;',
  16081. ' {@eagle_a}A();',
  16082. ' TBird(Self).{@bird_b}B:=true;',
  16083. ' TAnimal(Self).{@animal_a}A:=17;',
  16084. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  16085. 'end;',
  16086. 'var',
  16087. ' e: TEagle;',
  16088. 'begin',
  16089. ' e.{@eagle_b}B:=5.3;',
  16090. ' if e.{@eagle_a}A then ;',
  16091. '']);
  16092. ConvertProgram;
  16093. CheckSource('TestClass_ReintroduceVarDelphi',
  16094. LinesToStr([ // statements
  16095. 'rtl.createClass(this, "TObject", null, function () {',
  16096. ' this.$init = function () {',
  16097. ' };',
  16098. ' this.$final = function () {',
  16099. ' };',
  16100. '});',
  16101. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  16102. ' this.$init = function () {',
  16103. ' $mod.TObject.$init.call(this);',
  16104. ' this.A = 0;',
  16105. ' };',
  16106. ' this.B = function () {',
  16107. ' var Result = 0;',
  16108. ' return Result;',
  16109. ' };',
  16110. '});',
  16111. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  16112. ' this.$init = function () {',
  16113. ' $mod.TAnimal.$init.call(this);',
  16114. ' this.A$1 = 0.0;',
  16115. ' this.B$1 = false;',
  16116. ' };',
  16117. '});',
  16118. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  16119. ' this.$init = function () {',
  16120. ' $mod.TBird.$init.call(this);',
  16121. ' this.B$2 = 0.0;',
  16122. ' };',
  16123. ' this.A$2 = function () {',
  16124. ' var Result = false;',
  16125. ' this.B$2 = 3.3;',
  16126. ' this.A$2();',
  16127. ' this.B$1 = true;',
  16128. ' this.A = 17;',
  16129. ' this.B$1 = this.A$1 > 1;',
  16130. ' return Result;',
  16131. ' };',
  16132. '});',
  16133. 'this.e = null;',
  16134. '']),
  16135. LinesToStr([ // $mod.$main
  16136. '$mod.e.B$2 = 5.3;',
  16137. 'if ($mod.e.A$2()) ;',
  16138. '']));
  16139. end;
  16140. procedure TTestModule.TestClass_ReintroducedVar;
  16141. begin
  16142. StartProgram(false);
  16143. Add('type');
  16144. Add(' TObject = class');
  16145. Add(' strict private');
  16146. Add(' Some: longint;');
  16147. Add(' end;');
  16148. Add(' TMobile = class');
  16149. Add(' strict private');
  16150. Add(' Some: string;');
  16151. Add(' end;');
  16152. Add(' TCar = class(tmobile)');
  16153. Add(' procedure Some;');
  16154. Add(' procedure Some(vA: longint);');
  16155. Add(' end;');
  16156. Add('procedure tcar.some;');
  16157. Add('begin');
  16158. Add(' Some;');
  16159. Add(' Some(1);');
  16160. Add('end;');
  16161. Add('procedure tcar.some(va: longint); begin end;');
  16162. Add('begin');
  16163. ConvertProgram;
  16164. CheckSource('TestClass_ReintroducedVar',
  16165. LinesToStr([ // statements
  16166. 'rtl.createClass(this, "TObject", null, function () {',
  16167. ' this.$init = function () {',
  16168. ' this.Some = 0;',
  16169. ' };',
  16170. ' this.$final = function () {',
  16171. ' };',
  16172. '});',
  16173. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  16174. ' this.$init = function () {',
  16175. ' $mod.TObject.$init.call(this);',
  16176. ' this.Some$1 = "";',
  16177. ' };',
  16178. '});',
  16179. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  16180. ' this.Some$2 = function () {',
  16181. ' this.Some$2();',
  16182. ' this.Some$3(1);',
  16183. ' };',
  16184. ' this.Some$3 = function (vA) {',
  16185. ' };',
  16186. '});',
  16187. '']),
  16188. LinesToStr([ // $mod.$main
  16189. '']));
  16190. end;
  16191. procedure TTestModule.TestClass_RaiseDescendant;
  16192. begin
  16193. StartProgram(false);
  16194. Add([
  16195. 'type',
  16196. ' TObject = class',
  16197. ' constructor Create(Msg: string);',
  16198. ' end;',
  16199. ' Exception = class',
  16200. ' end;',
  16201. ' EConvertError = class(Exception)',
  16202. ' end;',
  16203. 'constructor TObject.Create(Msg: string); begin end;',
  16204. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  16205. 'begin',
  16206. ' raise Exception.Create(''Bar1'');',
  16207. ' raise EConvertError.Create(''Bar2'');',
  16208. ' raise AssertConv(''Bar2'');',
  16209. ' raise AssertConv;',
  16210. '']);
  16211. ConvertProgram;
  16212. CheckSource('TestClass_RaiseDescendant',
  16213. LinesToStr([ // statements
  16214. 'rtl.createClass(this, "TObject", null, function () {',
  16215. ' this.$init = function () {',
  16216. ' };',
  16217. ' this.$final = function () {',
  16218. ' };',
  16219. ' this.Create = function (Msg) {',
  16220. ' return this;',
  16221. ' };',
  16222. '});',
  16223. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  16224. '});',
  16225. 'rtl.createClass(this, "EConvertError", this.Exception, function () {',
  16226. '});',
  16227. 'this.AssertConv = function (Msg) {',
  16228. ' var Result = null;',
  16229. ' return Result;',
  16230. '};',
  16231. '']),
  16232. LinesToStr([ // $mod.$main
  16233. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  16234. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  16235. 'throw $mod.AssertConv("Bar2");',
  16236. 'throw $mod.AssertConv("def");',
  16237. '']));
  16238. end;
  16239. procedure TTestModule.TestClass_ExternalMethod;
  16240. begin
  16241. AddModuleWithIntfImplSrc('unit2.pas',
  16242. LinesToStr([
  16243. 'type',
  16244. ' TObject = class',
  16245. ' public',
  16246. ' procedure Intern; external name ''$DoIntern'';',
  16247. ' end;',
  16248. '']),
  16249. LinesToStr([
  16250. '']));
  16251. StartUnit(true);
  16252. Add('interface');
  16253. Add('uses unit2;');
  16254. Add('type');
  16255. Add(' TCar = class(TObject)');
  16256. Add(' public');
  16257. Add(' procedure Intern2; external name ''$DoIntern2'';');
  16258. Add(' procedure DoIt;');
  16259. Add(' end;');
  16260. Add('implementation');
  16261. Add('procedure tcar.doit;');
  16262. Add('begin');
  16263. Add(' Intern;');
  16264. Add(' Intern();');
  16265. Add(' Intern2;');
  16266. Add(' Intern2();');
  16267. Add('end;');
  16268. Add('var Obj: TCar;');
  16269. Add('begin');
  16270. Add(' obj.intern;');
  16271. Add(' obj.intern();');
  16272. Add(' obj.intern2;');
  16273. Add(' obj.intern2();');
  16274. Add(' obj.doit;');
  16275. Add(' obj.doit();');
  16276. Add(' with obj do begin');
  16277. Add(' Intern;');
  16278. Add(' Intern();');
  16279. Add(' Intern2;');
  16280. Add(' Intern2();');
  16281. Add(' end;');
  16282. ConvertUnit;
  16283. CheckSource('TestClass_ExternalMethod',
  16284. LinesToStr([
  16285. 'var $impl = $mod.$impl;',
  16286. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  16287. ' this.DoIt = function () {',
  16288. ' this.$DoIntern();',
  16289. ' this.$DoIntern();',
  16290. ' this.$DoIntern2();',
  16291. ' this.$DoIntern2();',
  16292. ' };',
  16293. ' });',
  16294. '']),
  16295. LinesToStr([ // this.$init
  16296. '$impl.Obj.$DoIntern();',
  16297. '$impl.Obj.$DoIntern();',
  16298. '$impl.Obj.$DoIntern2();',
  16299. '$impl.Obj.$DoIntern2();',
  16300. '$impl.Obj.DoIt();',
  16301. '$impl.Obj.DoIt();',
  16302. 'var $with = $impl.Obj;',
  16303. '$with.$DoIntern();',
  16304. '$with.$DoIntern();',
  16305. '$with.$DoIntern2();',
  16306. '$with.$DoIntern2();',
  16307. '']),
  16308. LinesToStr([ // implementation
  16309. '$impl.Obj = null;',
  16310. '']) );
  16311. end;
  16312. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  16313. begin
  16314. StartProgram(false);
  16315. Add('type');
  16316. Add(' TObject = class');
  16317. Add(' procedure DoIt; virtual; external name ''Foo'';');
  16318. Add(' end;');
  16319. Add('begin');
  16320. SetExpectedPasResolverError('Virtual method name must match external',
  16321. nVirtualMethodNameMustMatchExternal);
  16322. ConvertProgram;
  16323. end;
  16324. procedure TTestModule.TestClass_ExternalOverrideFail;
  16325. begin
  16326. StartProgram(false);
  16327. Add('type');
  16328. Add(' TObject = class');
  16329. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  16330. Add(' end;');
  16331. Add(' TCar = class');
  16332. Add(' procedure DoIt; override; external name ''DoIt'';');
  16333. Add(' end;');
  16334. Add('begin');
  16335. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  16336. nInvalidXModifierY);
  16337. ConvertProgram;
  16338. end;
  16339. procedure TTestModule.TestClass_ExternalVar;
  16340. begin
  16341. AddModuleWithIntfImplSrc('unit2.pas',
  16342. LinesToStr([
  16343. '{$modeswitch externalclass}',
  16344. 'type',
  16345. ' TObject = class',
  16346. ' public',
  16347. ' Intern: longint external name ''$Intern'';',
  16348. ' Bracket: longint external name ''["A B"]'';',
  16349. ' end;',
  16350. '']),
  16351. LinesToStr([
  16352. '']));
  16353. StartUnit(true);
  16354. Add([
  16355. 'interface',
  16356. 'uses unit2;',
  16357. '{$modeswitch externalclass}',
  16358. 'type',
  16359. ' TCar = class(tobject)',
  16360. ' public',
  16361. ' Intern2: longint external name ''$Intern2'';',
  16362. ' procedure DoIt;',
  16363. ' end;',
  16364. 'implementation',
  16365. 'procedure tcar.doit;',
  16366. 'begin',
  16367. ' Intern:=Intern+1;',
  16368. ' Intern2:=Intern2+2;',
  16369. ' Bracket:=Bracket+3;',
  16370. 'end;',
  16371. 'var Obj: TCar;',
  16372. 'begin',
  16373. ' obj.intern:=obj.intern+1;',
  16374. ' obj.intern2:=obj.intern2+2;',
  16375. ' obj.Bracket:=obj.Bracket+3;',
  16376. ' with obj do begin',
  16377. ' intern:=intern+1;',
  16378. ' intern2:=intern2+2;',
  16379. ' Bracket:=Bracket+3;',
  16380. ' end;']);
  16381. ConvertUnit;
  16382. CheckSource('TestClass_ExternalVar',
  16383. LinesToStr([
  16384. 'var $impl = $mod.$impl;',
  16385. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  16386. ' this.DoIt = function () {',
  16387. ' this.$Intern = this.$Intern + 1;',
  16388. ' this.$Intern2 = this.$Intern2 + 2;',
  16389. ' this["A B"] = this["A B"] + 3;',
  16390. ' };',
  16391. ' });',
  16392. '']),
  16393. LinesToStr([
  16394. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  16395. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  16396. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  16397. 'var $with = $impl.Obj;',
  16398. '$with.$Intern = $with.$Intern + 1;',
  16399. '$with.$Intern2 = $with.$Intern2 + 2;',
  16400. '$with["A B"] = $with["A B"] + 3;',
  16401. '']),
  16402. LinesToStr([ // implementation
  16403. '$impl.Obj = null;',
  16404. '']));
  16405. end;
  16406. procedure TTestModule.TestClass_Const;
  16407. begin
  16408. StartProgram(false);
  16409. Add([
  16410. 'type',
  16411. ' integer = longint;',
  16412. ' TClass = class of TObject;',
  16413. ' TObject = class',
  16414. ' public',
  16415. ' const cI: integer = 3;',
  16416. ' procedure DoIt;',
  16417. ' class procedure DoMore;',
  16418. ' end;',
  16419. 'procedure tobject.doit;',
  16420. 'begin',
  16421. ' if cI=4 then;',
  16422. ' if 5=cI then;',
  16423. ' if Self.cI=6 then;',
  16424. ' if 7=Self.cI then;',
  16425. ' with Self do begin',
  16426. ' if cI=11 then;',
  16427. ' if 12=cI then;',
  16428. ' end;',
  16429. 'end;',
  16430. 'class procedure tobject.domore;',
  16431. 'begin',
  16432. ' if cI=8 then;',
  16433. ' if Self.cI=9 then;',
  16434. ' if 10=cI then;',
  16435. ' if 11=Self.cI then;',
  16436. ' with Self do begin',
  16437. ' if cI=13 then;',
  16438. ' if 14=cI then;',
  16439. ' end;',
  16440. 'end;',
  16441. 'var',
  16442. ' Obj: TObject;',
  16443. ' Cla: TClass;',
  16444. 'begin',
  16445. ' if TObject.cI=21 then ;',
  16446. ' if Obj.cI=22 then ;',
  16447. ' if Cla.cI=23 then ;',
  16448. ' with obj do if ci=24 then;',
  16449. ' with TObject do if ci=25 then;',
  16450. ' with Cla do if ci=26 then;']);
  16451. ConvertProgram;
  16452. CheckSource('TestClass_Const',
  16453. LinesToStr([
  16454. 'rtl.createClass(this, "TObject", null, function () {',
  16455. ' this.cI = 3;',
  16456. ' this.$init = function () {',
  16457. ' };',
  16458. ' this.$final = function () {',
  16459. ' };',
  16460. ' this.DoIt = function () {',
  16461. ' if (this.cI === 4) ;',
  16462. ' if (5 === this.cI) ;',
  16463. ' if (this.cI === 6) ;',
  16464. ' if (7 === this.cI) ;',
  16465. ' if (this.cI === 11) ;',
  16466. ' if (12 === this.cI) ;',
  16467. ' };',
  16468. ' this.DoMore = function () {',
  16469. ' if (this.cI === 8) ;',
  16470. ' if (this.cI === 9) ;',
  16471. ' if (10 === this.cI) ;',
  16472. ' if (11 === this.cI) ;',
  16473. ' if (this.cI === 13) ;',
  16474. ' if (14 === this.cI) ;',
  16475. ' };',
  16476. '});',
  16477. 'this.Obj = null;',
  16478. 'this.Cla = null;',
  16479. '']),
  16480. LinesToStr([
  16481. 'if ($mod.TObject.cI === 21) ;',
  16482. 'if ($mod.Obj.cI === 22) ;',
  16483. 'if ($mod.Cla.cI === 23) ;',
  16484. 'var $with = $mod.Obj;',
  16485. 'if ($with.cI === 24) ;',
  16486. 'var $with1 = $mod.TObject;',
  16487. 'if ($with1.cI === 25) ;',
  16488. 'var $with2 = $mod.Cla;',
  16489. 'if ($with2.cI === 26) ;',
  16490. '']));
  16491. end;
  16492. procedure TTestModule.TestClass_ConstEnum;
  16493. begin
  16494. StartProgram(false);
  16495. Add([
  16496. 'type',
  16497. ' TEnum = (red,blue);',
  16498. ' TObject = class',
  16499. ' end;',
  16500. ' TAnimal = class',
  16501. ' public',
  16502. ' type TSubEnum = (light,dark);',
  16503. ' const a = high(TEnum);',
  16504. ' const b = high(TSubEnum);',
  16505. ' end;',
  16506. ' TBird = class(TAnimal)',
  16507. ' public',
  16508. ' const c = high(TEnum);',
  16509. ' const d = high(TSubEnum);',
  16510. ' end;',
  16511. ' TAnt = class',
  16512. ' public',
  16513. ' const e = high(TEnum);',
  16514. ' const f = high(TBird.TSubEnum);',
  16515. ' end;',
  16516. 'begin',
  16517. '']);
  16518. ConvertProgram;
  16519. CheckSource('TestClass_ConstEnum',
  16520. LinesToStr([
  16521. 'this.TEnum = {',
  16522. ' "0": "red",',
  16523. ' red: 0,',
  16524. ' "1": "blue",',
  16525. ' blue: 1',
  16526. '};',
  16527. 'rtl.createClass(this, "TObject", null, function () {',
  16528. ' this.$init = function () {',
  16529. ' };',
  16530. ' this.$final = function () {',
  16531. ' };',
  16532. '});',
  16533. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  16534. ' this.TSubEnum = {',
  16535. ' "0": "light",',
  16536. ' light: 0,',
  16537. ' "1": "dark",',
  16538. ' dark: 1',
  16539. ' };',
  16540. ' this.a = $mod.TEnum.blue;',
  16541. ' this.b = this.TSubEnum.dark;',
  16542. '});',
  16543. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  16544. ' this.c = $mod.TEnum.blue;',
  16545. ' this.d = this.TSubEnum.dark;',
  16546. '});',
  16547. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  16548. ' this.e = $mod.TEnum.blue;',
  16549. ' this.f = $mod.TAnimal.TSubEnum.dark;',
  16550. '});',
  16551. '']),
  16552. LinesToStr([
  16553. '']));
  16554. end;
  16555. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  16556. begin
  16557. StartProgram(false);
  16558. Add([
  16559. 'type',
  16560. ' TObject = class',
  16561. ' const cI: longint = 3;',
  16562. ' procedure Fly;',
  16563. ' procedure Run;',
  16564. ' end;',
  16565. ' TBird = class',
  16566. ' procedure Go;',
  16567. ' end;',
  16568. 'procedure tobject.fly;',
  16569. 'const cI: word = 4;',
  16570. 'begin',
  16571. ' if cI=Self.cI then ;',
  16572. 'end;',
  16573. 'procedure tobject.run;',
  16574. 'const cI: word = 5;',
  16575. 'begin',
  16576. ' if cI=Self.cI then ;',
  16577. 'end;',
  16578. 'procedure tbird.go;',
  16579. 'const cI: word = 6;',
  16580. 'begin',
  16581. ' if cI=Self.cI then ;',
  16582. 'end;',
  16583. 'begin',
  16584. '']);
  16585. ConvertProgram;
  16586. CheckSource('TestClass_LocalConstDuplicate_Prg',
  16587. LinesToStr([
  16588. 'rtl.createClass(this, "TObject", null, function () {',
  16589. ' this.cI = 3;',
  16590. ' this.$init = function () {',
  16591. ' };',
  16592. ' this.$final = function () {',
  16593. ' };',
  16594. ' var cI$1 = 4;',
  16595. ' this.Fly = function () {',
  16596. ' if (cI$1 === this.cI) ;',
  16597. ' };',
  16598. ' var cI$2 = 5;',
  16599. ' this.Run = function () {',
  16600. ' if (cI$2 === this.cI) ;',
  16601. ' };',
  16602. '});',
  16603. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16604. ' var cI$3 = 6;',
  16605. ' this.Go = function () {',
  16606. ' if (cI$3 === this.cI) ;',
  16607. ' };',
  16608. '});',
  16609. '']),
  16610. LinesToStr([
  16611. '']));
  16612. end;
  16613. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  16614. begin
  16615. StartUnit(false);
  16616. Add([
  16617. 'interface',
  16618. 'type',
  16619. ' TObject = class',
  16620. ' const cI: longint = 3;',
  16621. ' procedure Fly;',
  16622. ' procedure Run;',
  16623. ' end;',
  16624. ' TBird = class',
  16625. ' procedure Go;',
  16626. ' end;',
  16627. 'implementation',
  16628. 'procedure tobject.fly;',
  16629. 'const cI: word = 4;',
  16630. 'begin',
  16631. ' if cI=Self.cI then ;',
  16632. 'end;',
  16633. 'procedure tobject.run;',
  16634. 'const cI: word = 5;',
  16635. 'begin',
  16636. ' if cI=Self.cI then ;',
  16637. 'end;',
  16638. 'procedure tbird.go;',
  16639. 'const cI: word = 6;',
  16640. 'begin',
  16641. ' if cI=Self.cI then ;',
  16642. 'end;',
  16643. '']);
  16644. ConvertUnit;
  16645. CheckSource('TestClass_LocalConstDuplicate_Unit',
  16646. LinesToStr([
  16647. 'rtl.createClass(this, "TObject", null, function () {',
  16648. ' this.cI = 3;',
  16649. ' this.$init = function () {',
  16650. ' };',
  16651. ' this.$final = function () {',
  16652. ' };',
  16653. ' var cI$1 = 4;',
  16654. ' this.Fly = function () {',
  16655. ' if (cI$1 === this.cI) ;',
  16656. ' };',
  16657. ' var cI$2 = 5;',
  16658. ' this.Run = function () {',
  16659. ' if (cI$2 === this.cI) ;',
  16660. ' };',
  16661. '});',
  16662. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16663. ' var cI$3 = 6;',
  16664. ' this.Go = function () {',
  16665. ' if (cI$3 === this.cI) ;',
  16666. ' };',
  16667. '});',
  16668. '']),
  16669. '',
  16670. '');
  16671. end;
  16672. procedure TTestModule.TestClass_LocalVarSelfFail;
  16673. begin
  16674. StartProgram(false);
  16675. Add([
  16676. 'type',
  16677. ' TObject = class',
  16678. ' constructor Create;',
  16679. ' end;',
  16680. 'constructor tobject.create;',
  16681. 'var self: longint;',
  16682. 'begin',
  16683. 'end',
  16684. 'begin',
  16685. '']);
  16686. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  16687. ConvertProgram;
  16688. end;
  16689. procedure TTestModule.TestClass_ArgSelfFail;
  16690. begin
  16691. StartProgram(false);
  16692. Add([
  16693. 'type',
  16694. ' TObject = class',
  16695. ' procedure DoIt(Self: longint);',
  16696. ' end;',
  16697. 'procedure tobject.doit(self: longint);',
  16698. 'begin',
  16699. 'end',
  16700. 'begin',
  16701. '']);
  16702. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  16703. ConvertProgram;
  16704. end;
  16705. procedure TTestModule.TestClass_NestedProcSelf;
  16706. begin
  16707. StartProgram(false);
  16708. Add([
  16709. 'type',
  16710. ' TObject = class',
  16711. ' Key: longint;',
  16712. ' class var State: longint;',
  16713. ' procedure DoIt;',
  16714. ' function GetSize: longint; virtual; abstract;',
  16715. ' procedure SetSize(Value: longint); virtual; abstract;',
  16716. ' property Size: longint read GetSize write SetSize;',
  16717. ' end;',
  16718. 'procedure tobject.doit;',
  16719. ' procedure Sub;',
  16720. ' begin',
  16721. ' key:=key+2;',
  16722. ' self.key:=self.key+3;',
  16723. ' state:=state+4;',
  16724. ' self.state:=self.state+5;',
  16725. ' tobject.state:=tobject.state+6;',
  16726. ' size:=size+7;',
  16727. ' self.size:=self.size+8;',
  16728. ' end;',
  16729. 'begin',
  16730. ' sub;',
  16731. ' key:=key+12;',
  16732. ' self.key:=self.key+13;',
  16733. ' state:=state+14;',
  16734. ' self.state:=self.state+15;',
  16735. ' tobject.state:=tobject.state+16;',
  16736. ' size:=size+17;',
  16737. ' self.size:=self.size+18;',
  16738. 'end;',
  16739. 'begin',
  16740. '']);
  16741. ConvertProgram;
  16742. CheckSource('TestClass_NestedProcSelf',
  16743. LinesToStr([ // statements
  16744. 'rtl.createClass(this, "TObject", null, function () {',
  16745. ' this.State = 0;',
  16746. ' this.$init = function () {',
  16747. ' this.Key = 0;',
  16748. ' };',
  16749. ' this.$final = function () {',
  16750. ' };',
  16751. ' this.DoIt = function () {',
  16752. ' var $Self = this;',
  16753. ' function Sub() {',
  16754. ' $Self.Key = $Self.Key + 2;',
  16755. ' $Self.Key = $Self.Key + 3;',
  16756. ' $mod.TObject.State = $Self.State + 4;',
  16757. ' $mod.TObject.State = $Self.State + 5;',
  16758. ' $mod.TObject.State = $mod.TObject.State + 6;',
  16759. ' $Self.SetSize($Self.GetSize() + 7);',
  16760. ' $Self.SetSize($Self.GetSize() + 8);',
  16761. ' };',
  16762. ' Sub();',
  16763. ' this.Key = this.Key + 12;',
  16764. ' $Self.Key = $Self.Key + 13;',
  16765. ' $mod.TObject.State = this.State + 14;',
  16766. ' $mod.TObject.State = $Self.State + 15;',
  16767. ' $mod.TObject.State = $mod.TObject.State + 16;',
  16768. ' this.SetSize(this.GetSize() + 17);',
  16769. ' $Self.SetSize($Self.GetSize() + 18);',
  16770. ' };',
  16771. '});',
  16772. '']),
  16773. LinesToStr([ // $mod.$main
  16774. '']));
  16775. end;
  16776. procedure TTestModule.TestClass_NestedProcSelf2;
  16777. begin
  16778. StartProgram(false);
  16779. Add([
  16780. 'type',
  16781. ' TObject = class',
  16782. ' Key: longint;',
  16783. ' class var State: longint;',
  16784. ' function GetSize: longint; virtual; abstract;',
  16785. ' procedure SetSize(Value: longint); virtual; abstract;',
  16786. ' property Size: longint read GetSize write SetSize;',
  16787. ' end;',
  16788. ' TBird = class',
  16789. ' procedure DoIt;',
  16790. ' end;',
  16791. 'procedure tbird.doit;',
  16792. ' procedure Sub;',
  16793. ' begin',
  16794. ' key:=key+2;',
  16795. ' self.key:=self.key+3;',
  16796. ' state:=state+4;',
  16797. ' self.state:=self.state+5;',
  16798. ' tobject.state:=tobject.state+6;',
  16799. ' size:=size+7;',
  16800. ' self.size:=self.size+8;',
  16801. ' end;',
  16802. 'begin',
  16803. ' sub;',
  16804. ' key:=key+12;',
  16805. ' self.key:=self.key+13;',
  16806. ' state:=state+14;',
  16807. ' self.state:=self.state+15;',
  16808. ' tobject.state:=tobject.state+16;',
  16809. ' size:=size+17;',
  16810. ' self.size:=self.size+18;',
  16811. 'end;',
  16812. 'begin',
  16813. '']);
  16814. ConvertProgram;
  16815. CheckSource('TestClass_NestedProcSelf2',
  16816. LinesToStr([ // statements
  16817. 'rtl.createClass(this, "TObject", null, function () {',
  16818. ' this.State = 0;',
  16819. ' this.$init = function () {',
  16820. ' this.Key = 0;',
  16821. ' };',
  16822. ' this.$final = function () {',
  16823. ' };',
  16824. '});',
  16825. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16826. ' this.DoIt = function () {',
  16827. ' var $Self = this;',
  16828. ' function Sub() {',
  16829. ' $Self.Key = $Self.Key + 2;',
  16830. ' $Self.Key = $Self.Key + 3;',
  16831. ' $mod.TObject.State = $Self.State + 4;',
  16832. ' $mod.TObject.State = $Self.State + 5;',
  16833. ' $mod.TObject.State = $mod.TObject.State + 6;',
  16834. ' $Self.SetSize($Self.GetSize() + 7);',
  16835. ' $Self.SetSize($Self.GetSize() + 8);',
  16836. ' };',
  16837. ' Sub();',
  16838. ' this.Key = this.Key + 12;',
  16839. ' $Self.Key = $Self.Key + 13;',
  16840. ' $mod.TObject.State = this.State + 14;',
  16841. ' $mod.TObject.State = $Self.State + 15;',
  16842. ' $mod.TObject.State = $mod.TObject.State + 16;',
  16843. ' this.SetSize(this.GetSize() + 17);',
  16844. ' $Self.SetSize($Self.GetSize() + 18);',
  16845. ' };',
  16846. '});',
  16847. '']),
  16848. LinesToStr([ // $mod.$main
  16849. '']));
  16850. end;
  16851. procedure TTestModule.TestClass_NestedProcClassSelf;
  16852. begin
  16853. StartProgram(false);
  16854. Add([
  16855. 'type',
  16856. ' TObject = class',
  16857. ' class var State: longint;',
  16858. ' class procedure DoIt;',
  16859. ' class function GetSize: longint; virtual; abstract;',
  16860. ' class procedure SetSize(Value: longint); virtual; abstract;',
  16861. ' class property Size: longint read GetSize write SetSize;',
  16862. ' end;',
  16863. 'class procedure tobject.doit;',
  16864. ' procedure Sub;',
  16865. ' begin',
  16866. ' state:=state+2;',
  16867. ' self.state:=self.state+3;',
  16868. ' tobject.state:=tobject.state+4;',
  16869. ' size:=size+5;',
  16870. ' self.size:=self.size+6;',
  16871. ' tobject.size:=tobject.size+7;',
  16872. ' end;',
  16873. 'begin',
  16874. ' sub;',
  16875. ' state:=state+12;',
  16876. ' self.state:=self.state+13;',
  16877. ' tobject.state:=tobject.state+14;',
  16878. ' size:=size+15;',
  16879. ' self.size:=self.size+16;',
  16880. ' tobject.size:=tobject.size+17;',
  16881. 'end;',
  16882. 'begin',
  16883. '']);
  16884. ConvertProgram;
  16885. CheckSource('TestClass_NestedProcClassSelf',
  16886. LinesToStr([ // statements
  16887. 'rtl.createClass(this, "TObject", null, function () {',
  16888. ' this.State = 0;',
  16889. ' this.$init = function () {',
  16890. ' };',
  16891. ' this.$final = function () {',
  16892. ' };',
  16893. ' this.DoIt = function () {',
  16894. ' var $Self = this;',
  16895. ' function Sub() {',
  16896. ' $mod.TObject.State = $Self.State + 2;',
  16897. ' $mod.TObject.State = $Self.State + 3;',
  16898. ' $mod.TObject.State = $mod.TObject.State + 4;',
  16899. ' $Self.SetSize($Self.GetSize() + 5);',
  16900. ' $Self.SetSize($Self.GetSize() + 6);',
  16901. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  16902. ' };',
  16903. ' Sub();',
  16904. ' $mod.TObject.State = this.State + 12;',
  16905. ' $mod.TObject.State = $Self.State + 13;',
  16906. ' $mod.TObject.State = $mod.TObject.State + 14;',
  16907. ' this.SetSize(this.GetSize() + 15);',
  16908. ' $Self.SetSize($Self.GetSize() + 16);',
  16909. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  16910. ' };',
  16911. '});',
  16912. '']),
  16913. LinesToStr([ // $mod.$main
  16914. '']));
  16915. end;
  16916. procedure TTestModule.TestClass_NestedProcCallInherited;
  16917. begin
  16918. StartProgram(false);
  16919. Add([
  16920. 'type',
  16921. ' TObject = class',
  16922. ' function DoIt(k: boolean): longint; virtual;',
  16923. ' end;',
  16924. ' TBird = class',
  16925. ' function DoIt(k: boolean): longint; override;',
  16926. ' end;',
  16927. 'function tobject.doit(k: boolean): longint;',
  16928. 'begin',
  16929. 'end;',
  16930. 'function tbird.doit(k: boolean): longint;',
  16931. ' procedure Sub;',
  16932. ' begin',
  16933. ' inherited DoIt(true);',
  16934. //' if inherited DoIt(false)=4 then ;',
  16935. ' end;',
  16936. 'begin',
  16937. ' Sub;',
  16938. ' inherited;',
  16939. ' inherited DoIt(true);',
  16940. //' if inherited DoIt(false)=14 then ;',
  16941. 'end;',
  16942. 'begin',
  16943. '']);
  16944. ConvertProgram;
  16945. CheckSource('TestClass_NestedProcCallInherited',
  16946. LinesToStr([ // statements
  16947. 'rtl.createClass(this, "TObject", null, function () {',
  16948. ' this.$init = function () {',
  16949. ' };',
  16950. ' this.$final = function () {',
  16951. ' };',
  16952. ' this.DoIt = function (k) {',
  16953. ' var Result = 0;',
  16954. ' return Result;',
  16955. ' };',
  16956. '});',
  16957. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16958. ' this.DoIt = function (k) {',
  16959. ' var $Self = this;',
  16960. ' var Result = 0;',
  16961. ' function Sub() {',
  16962. ' $mod.TObject.DoIt.call($Self, true);',
  16963. ' };',
  16964. ' Sub();',
  16965. ' $mod.TObject.DoIt.apply(this, arguments);',
  16966. ' $mod.TObject.DoIt.call(this, true);',
  16967. ' return Result;',
  16968. ' };',
  16969. '});',
  16970. '']),
  16971. LinesToStr([ // $mod.$main
  16972. '']));
  16973. end;
  16974. procedure TTestModule.TestClass_TObjectFree;
  16975. begin
  16976. StartProgram(false);
  16977. Add([
  16978. 'type',
  16979. ' TObject = class',
  16980. ' Obj: tobject;',
  16981. ' procedure Free;',
  16982. ' procedure Release;',
  16983. ' end;',
  16984. 'procedure tobject.free;',
  16985. 'begin',
  16986. 'end;',
  16987. 'procedure tobject.release;',
  16988. 'begin',
  16989. ' free;',
  16990. ' if true then free;',
  16991. 'end;',
  16992. 'function DoIt(o: tobject): tobject;',
  16993. 'var l: tobject;',
  16994. 'begin',
  16995. ' o.free;',
  16996. ' o.free();',
  16997. ' l.free;',
  16998. ' l.free();',
  16999. ' o.obj.free;',
  17000. ' o.obj.free();',
  17001. ' with o do obj.free;',
  17002. ' with o do obj.free();',
  17003. ' result.Free;',
  17004. ' result.Free();',
  17005. 'end;',
  17006. 'var o: tobject;',
  17007. ' a: array of tobject;',
  17008. 'begin',
  17009. ' o.free;',
  17010. ' o.obj.free;',
  17011. ' a[1+2].free;',
  17012. '']);
  17013. ConvertProgram;
  17014. CheckSource('TestClass_TObjectFree',
  17015. LinesToStr([ // statements
  17016. 'rtl.createClass(this, "TObject", null, function () {',
  17017. ' this.$init = function () {',
  17018. ' this.Obj = null;',
  17019. ' };',
  17020. ' this.$final = function () {',
  17021. ' this.Obj = undefined;',
  17022. ' };',
  17023. ' this.Free = function () {',
  17024. ' };',
  17025. ' this.Release = function () {',
  17026. ' this.Free();',
  17027. ' if (true) this.Free();',
  17028. ' };',
  17029. '});',
  17030. 'this.DoIt = function (o) {',
  17031. ' var Result = null;',
  17032. ' var l = null;',
  17033. ' o = rtl.freeLoc(o);',
  17034. ' o = rtl.freeLoc(o);',
  17035. ' l = rtl.freeLoc(l);',
  17036. ' l = rtl.freeLoc(l);',
  17037. ' rtl.free(o, "Obj");',
  17038. ' rtl.free(o, "Obj");',
  17039. ' rtl.free(o, "Obj");',
  17040. ' rtl.free(o, "Obj");',
  17041. ' Result = rtl.freeLoc(Result);',
  17042. ' Result = rtl.freeLoc(Result);',
  17043. ' return Result;',
  17044. '};',
  17045. 'this.o = null;',
  17046. 'this.a = [];',
  17047. '']),
  17048. LinesToStr([ // $mod.$main
  17049. 'rtl.free($mod, "o");',
  17050. 'rtl.free($mod.o, "Obj");',
  17051. 'rtl.free($mod.a, 1 + 2);',
  17052. '']));
  17053. end;
  17054. procedure TTestModule.TestClass_TObjectFree_VarArg;
  17055. begin
  17056. StartProgram(false);
  17057. Add([
  17058. 'type',
  17059. ' TObject = class',
  17060. ' Obj: tobject;',
  17061. ' procedure Free;',
  17062. ' end;',
  17063. 'procedure tobject.free;',
  17064. 'begin',
  17065. 'end;',
  17066. 'procedure DoIt(var o: tobject);',
  17067. 'begin',
  17068. ' o.free;',
  17069. ' o.free();',
  17070. 'end;',
  17071. 'begin',
  17072. '']);
  17073. ConvertProgram;
  17074. CheckSource('TestClass_TObjectFree_VarArg',
  17075. LinesToStr([ // statements
  17076. 'rtl.createClass(this, "TObject", null, function () {',
  17077. ' this.$init = function () {',
  17078. ' this.Obj = null;',
  17079. ' };',
  17080. ' this.$final = function () {',
  17081. ' this.Obj = undefined;',
  17082. ' };',
  17083. ' this.Free = function () {',
  17084. ' };',
  17085. '});',
  17086. 'this.DoIt = function (o) {',
  17087. ' o.set(rtl.freeLoc(o.get()));',
  17088. ' o.set(rtl.freeLoc(o.get()));',
  17089. '};',
  17090. '']),
  17091. LinesToStr([ // $mod.$main
  17092. '']));
  17093. end;
  17094. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  17095. begin
  17096. StartProgram(false);
  17097. Add([
  17098. 'type',
  17099. ' TObject = class',
  17100. ' constructor Create;',
  17101. ' procedure Free;',
  17102. ' end;',
  17103. 'constructor TObject.Create; begin end;',
  17104. 'procedure tobject.free; begin end;',
  17105. 'begin',
  17106. ' with tobject.create do free;',
  17107. '']);
  17108. ConvertProgram;
  17109. CheckSource('TestClass_TObjectFreeNewInstance',
  17110. LinesToStr([ // statements
  17111. 'rtl.createClass(this, "TObject", null, function () {',
  17112. ' this.$init = function () {',
  17113. ' };',
  17114. ' this.$final = function () {',
  17115. ' };',
  17116. ' this.Create = function () {',
  17117. ' return this;',
  17118. ' };',
  17119. ' this.Free = function () {',
  17120. ' };',
  17121. '});',
  17122. '']),
  17123. LinesToStr([ // $mod.$main
  17124. 'var $with = $mod.TObject.$create("Create");',
  17125. '$with=rtl.freeLoc($with);',
  17126. '']));
  17127. end;
  17128. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  17129. begin
  17130. StartProgram(false);
  17131. Add([
  17132. 'type',
  17133. ' TObject = class',
  17134. ' destructor Destroy;',
  17135. ' procedure Free;',
  17136. ' end;',
  17137. 'destructor TObject.Destroy; begin end;',
  17138. 'procedure tobject.free; begin end;',
  17139. 'var o: tobject;',
  17140. 'begin',
  17141. ' o.free;',
  17142. '']);
  17143. Converter.UseLowerCase:=true;
  17144. ConvertProgram;
  17145. CheckSource('TestClass_TObjectFreeLowerCase',
  17146. LinesToStr([ // statements
  17147. 'rtl.createClass(this, "tobject", null, function () {',
  17148. ' this.$init = function () {',
  17149. ' };',
  17150. ' this.$final = function () {',
  17151. ' };',
  17152. ' rtl.tObjectDestroy = "destroy";',
  17153. ' this.destroy = function () {',
  17154. ' };',
  17155. ' this.free = function () {',
  17156. ' };',
  17157. '});',
  17158. 'this.o = null;',
  17159. '']),
  17160. LinesToStr([ // $mod.$main
  17161. 'rtl.free($mod, "o");',
  17162. '']));
  17163. end;
  17164. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  17165. begin
  17166. StartProgram(false);
  17167. Add([
  17168. 'type',
  17169. ' TObject = class',
  17170. ' procedure Free;',
  17171. ' function GetObj: tobject; virtual; abstract;',
  17172. ' end;',
  17173. 'procedure tobject.free;',
  17174. 'begin',
  17175. 'end;',
  17176. 'var o: tobject;',
  17177. 'begin',
  17178. ' o.getobj.free;',
  17179. '']);
  17180. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  17181. ConvertProgram;
  17182. end;
  17183. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  17184. begin
  17185. StartProgram(false);
  17186. Add([
  17187. 'type',
  17188. ' TObject = class',
  17189. ' procedure Free;',
  17190. ' FObj: TObject;',
  17191. ' property Obj: tobject read FObj write FObj;',
  17192. ' end;',
  17193. 'procedure tobject.free;',
  17194. 'begin',
  17195. 'end;',
  17196. 'var o: tobject;',
  17197. 'begin',
  17198. ' o.obj.free;',
  17199. '']);
  17200. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  17201. ConvertProgram;
  17202. end;
  17203. procedure TTestModule.TestClass_ForIn;
  17204. begin
  17205. StartProgram(false);
  17206. Add([
  17207. 'type',
  17208. ' TObject = class end;',
  17209. ' TItem = TObject;',
  17210. ' TEnumerator = class',
  17211. ' FCurrent: TItem;',
  17212. ' property Current: TItem read FCurrent;',
  17213. ' function MoveNext: boolean;',
  17214. ' end;',
  17215. ' TBird = class',
  17216. ' function GetEnumerator: TEnumerator;',
  17217. ' end;',
  17218. 'function TEnumerator.MoveNext: boolean;',
  17219. 'begin',
  17220. 'end;',
  17221. 'function TBird.GetEnumerator: TEnumerator;',
  17222. 'begin',
  17223. 'end;',
  17224. 'var',
  17225. ' b: TBird;',
  17226. ' i, i2: TItem;',
  17227. 'begin',
  17228. ' for i in b do i2:=i;']);
  17229. ConvertProgram;
  17230. CheckSource('TestClass_ForIn',
  17231. LinesToStr([ // statements
  17232. 'rtl.createClass(this, "TObject", null, function () {',
  17233. ' this.$init = function () {',
  17234. ' };',
  17235. ' this.$final = function () {',
  17236. ' };',
  17237. '});',
  17238. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  17239. ' this.$init = function () {',
  17240. ' $mod.TObject.$init.call(this);',
  17241. ' this.FCurrent = null;',
  17242. ' };',
  17243. ' this.$final = function () {',
  17244. ' this.FCurrent = undefined;',
  17245. ' $mod.TObject.$final.call(this);',
  17246. ' };',
  17247. ' this.MoveNext = function () {',
  17248. ' var Result = false;',
  17249. ' return Result;',
  17250. ' };',
  17251. '});',
  17252. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17253. ' this.GetEnumerator = function () {',
  17254. ' var Result = null;',
  17255. ' return Result;',
  17256. ' };',
  17257. '});',
  17258. 'this.b = null;',
  17259. 'this.i = null;',
  17260. 'this.i2 = null;'
  17261. ]),
  17262. LinesToStr([ // $mod.$main
  17263. 'var $in = $mod.b.GetEnumerator();',
  17264. 'try {',
  17265. ' while ($in.MoveNext()){',
  17266. ' $mod.i = $in.FCurrent;',
  17267. ' $mod.i2 = $mod.i;',
  17268. ' }',
  17269. '} finally {',
  17270. ' $in = rtl.freeLoc($in)',
  17271. '};',
  17272. '']));
  17273. end;
  17274. procedure TTestModule.TestClass_DispatchMessage;
  17275. begin
  17276. StartProgram(false);
  17277. Add([
  17278. 'type',
  17279. ' {$DispatchField DispInt}',
  17280. ' {$DispatchStrField DispStr}',
  17281. ' TObject = class',
  17282. ' procedure Dispatch(var Msg); virtual; abstract;',
  17283. ' procedure DispatchStr(var Msg); virtual; abstract;',
  17284. ' end;',
  17285. ' THopMsg = record',
  17286. ' DispInt: longint;',
  17287. ' end;',
  17288. ' TPutMsg = record',
  17289. ' DispStr: string;',
  17290. ' end;',
  17291. ' TBird = class',
  17292. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  17293. ' procedure Run; overload; virtual; abstract;',
  17294. ' procedure Run(var Msg); overload; message ''Fast'';',
  17295. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  17296. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  17297. ' end;',
  17298. 'procedure TBird.Run(var Msg);',
  17299. 'begin',
  17300. 'end;',
  17301. 'begin',
  17302. '']);
  17303. ConvertProgram;
  17304. CheckResolverUnexpectedHints(true);
  17305. CheckSource('TestClass_Message',
  17306. LinesToStr([ // statements
  17307. 'rtl.createClass(this, "TObject", null, function () {',
  17308. ' this.$init = function () {',
  17309. ' };',
  17310. ' this.$final = function () {',
  17311. ' };',
  17312. '});',
  17313. 'rtl.recNewT(this, "THopMsg", function () {',
  17314. ' this.DispInt = 0;',
  17315. ' this.$eq = function (b) {',
  17316. ' return this.DispInt === b.DispInt;',
  17317. ' };',
  17318. ' this.$assign = function (s) {',
  17319. ' this.DispInt = s.DispInt;',
  17320. ' return this;',
  17321. ' };',
  17322. '});',
  17323. 'rtl.recNewT(this, "TPutMsg", function () {',
  17324. ' this.DispStr = "";',
  17325. ' this.$eq = function (b) {',
  17326. ' return this.DispStr === b.DispStr;',
  17327. ' };',
  17328. ' this.$assign = function (s) {',
  17329. ' this.DispStr = s.DispStr;',
  17330. ' return this;',
  17331. ' };',
  17332. '});',
  17333. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17334. ' this.Run$1 = function (Msg) {',
  17335. ' };',
  17336. ' this.$msgint = {',
  17337. ' "2": "Fly",',
  17338. ' "3": "Hop"',
  17339. ' };',
  17340. ' this.$msgstr = {',
  17341. ' Fast: "Run$1",',
  17342. ' foo: "Put"',
  17343. ' };',
  17344. '});',
  17345. '']),
  17346. LinesToStr([ // $mod.$main
  17347. '']));
  17348. end;
  17349. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  17350. begin
  17351. StartProgram(false);
  17352. Add([
  17353. 'type',
  17354. ' TObject = class',
  17355. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  17356. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  17357. ' end;',
  17358. 'begin',
  17359. '']);
  17360. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  17361. ConvertProgram;
  17362. end;
  17363. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  17364. begin
  17365. StartProgram(false);
  17366. Add([
  17367. 'type',
  17368. ' TObject = class',
  17369. ' procedure Dispatch(var Msg); virtual; abstract;',
  17370. ' end;',
  17371. ' TFlyMsg = record',
  17372. ' FlyId: longint;',
  17373. ' end;',
  17374. ' TBird = class',
  17375. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  17376. ' end;',
  17377. 'begin',
  17378. '']);
  17379. ConvertProgram;
  17380. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  17381. end;
  17382. procedure TTestModule.TestClassOf_Create;
  17383. begin
  17384. StartProgram(false);
  17385. Add('type');
  17386. Add(' TObject = class');
  17387. Add(' constructor Create;');
  17388. Add(' end;');
  17389. Add(' TClass = class of TObject;');
  17390. Add('constructor tobject.create; begin end;');
  17391. Add('var');
  17392. Add(' Obj: tobject;');
  17393. Add(' C: tclass;');
  17394. Add('begin');
  17395. Add(' obj:=C.create;');
  17396. Add(' with c do obj:=create;');
  17397. ConvertProgram;
  17398. CheckSource('TestClassOf_Create',
  17399. LinesToStr([ // statements
  17400. 'rtl.createClass(this, "TObject", null, function () {',
  17401. ' this.$init = function () {',
  17402. ' };',
  17403. ' this.$final = function () {',
  17404. ' };',
  17405. ' this.Create = function () {',
  17406. ' return this;',
  17407. ' };',
  17408. '});',
  17409. 'this.Obj = null;',
  17410. 'this.C = null;'
  17411. ]),
  17412. LinesToStr([ // $mod.$main
  17413. '$mod.Obj = $mod.C.$create("Create");',
  17414. 'var $with = $mod.C;',
  17415. '$mod.Obj = $with.$create("Create");',
  17416. '']));
  17417. end;
  17418. procedure TTestModule.TestClassOf_Call;
  17419. begin
  17420. StartProgram(false);
  17421. Add('type');
  17422. Add(' TObject = class');
  17423. Add(' class procedure DoIt;');
  17424. Add(' end;');
  17425. Add(' TClass = class of TObject;');
  17426. Add('class procedure tobject.doit; begin end;');
  17427. Add('var');
  17428. Add(' C: tclass;');
  17429. Add('begin');
  17430. Add(' c.doit;');
  17431. Add(' with c do doit;');
  17432. ConvertProgram;
  17433. CheckSource('TestClassOf_Call',
  17434. LinesToStr([ // statements
  17435. 'rtl.createClass(this, "TObject", null, function () {',
  17436. ' this.$init = function () {',
  17437. ' };',
  17438. ' this.$final = function () {',
  17439. ' };',
  17440. ' this.DoIt = function () {',
  17441. ' };',
  17442. '});',
  17443. 'this.C = null;'
  17444. ]),
  17445. LinesToStr([ // $mod.$main
  17446. '$mod.C.DoIt();',
  17447. 'var $with = $mod.C;',
  17448. '$with.DoIt();',
  17449. '']));
  17450. end;
  17451. procedure TTestModule.TestClassOf_Assign;
  17452. begin
  17453. StartProgram(false);
  17454. Add('type');
  17455. Add(' TClass = class of TObject;');
  17456. Add(' TObject = class');
  17457. Add(' ClassType: TClass; ');
  17458. Add(' end;');
  17459. Add('var');
  17460. Add(' Obj: tobject;');
  17461. Add(' C: tclass;');
  17462. Add('begin');
  17463. Add(' c:=nil;');
  17464. Add(' c:=obj.classtype;');
  17465. ConvertProgram;
  17466. CheckSource('TestClassOf_Assign',
  17467. LinesToStr([ // statements
  17468. 'rtl.createClass(this, "TObject", null, function () {',
  17469. ' this.$init = function () {',
  17470. ' this.ClassType = null;',
  17471. ' };',
  17472. ' this.$final = function () {',
  17473. ' this.ClassType = undefined;',
  17474. ' };',
  17475. '});',
  17476. 'this.Obj = null;',
  17477. 'this.C = null;'
  17478. ]),
  17479. LinesToStr([ // $mod.$main
  17480. '$mod.C = null;',
  17481. '$mod.C = $mod.Obj.ClassType;',
  17482. '']));
  17483. end;
  17484. procedure TTestModule.TestClassOf_Is;
  17485. begin
  17486. StartProgram(false);
  17487. Add('type');
  17488. Add(' TClass = class of TObject;');
  17489. Add(' TObject = class');
  17490. Add(' end;');
  17491. Add(' TCar = class');
  17492. Add(' end;');
  17493. Add(' TCars = class of TCar;');
  17494. Add('var');
  17495. Add(' Obj: tobject;');
  17496. Add(' C: tclass;');
  17497. Add(' Cars: tcars;');
  17498. Add('begin');
  17499. Add(' if c is tcar then ;');
  17500. Add(' if c is tcars then ;');
  17501. ConvertProgram;
  17502. CheckSource('TestClassOf_Is',
  17503. LinesToStr([ // statements
  17504. 'rtl.createClass(this, "TObject", null, function () {',
  17505. ' this.$init = function () {',
  17506. ' };',
  17507. ' this.$final = function () {',
  17508. ' };',
  17509. '});',
  17510. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  17511. '});',
  17512. 'this.Obj = null;',
  17513. 'this.C = null;',
  17514. 'this.Cars = null;'
  17515. ]),
  17516. LinesToStr([ // $mod.$main
  17517. 'if(rtl.is($mod.C,$mod.TCar));',
  17518. 'if(rtl.is($mod.C,$mod.TCar));',
  17519. '']));
  17520. end;
  17521. procedure TTestModule.TestClassOf_Compare;
  17522. begin
  17523. StartProgram(false);
  17524. Add('type');
  17525. Add(' TClass = class of TObject;');
  17526. Add(' TObject = class');
  17527. Add(' ClassType: TClass; ');
  17528. Add(' end;');
  17529. Add('var');
  17530. Add(' b: boolean;');
  17531. Add(' Obj: tobject;');
  17532. Add(' C: tclass;');
  17533. Add('begin');
  17534. Add(' b:=c=nil;');
  17535. Add(' b:=nil=c;');
  17536. Add(' b:=c=obj.classtype;');
  17537. Add(' b:=obj.classtype=c;');
  17538. Add(' b:=c=TObject;');
  17539. Add(' b:=TObject=c;');
  17540. Add(' b:=c<>nil;');
  17541. Add(' b:=nil<>c;');
  17542. Add(' b:=c<>obj.classtype;');
  17543. Add(' b:=obj.classtype<>c;');
  17544. Add(' b:=c<>TObject;');
  17545. Add(' b:=TObject<>c;');
  17546. ConvertProgram;
  17547. CheckSource('TestClassOf_Compare',
  17548. LinesToStr([ // statements
  17549. 'rtl.createClass(this, "TObject", null, function () {',
  17550. ' this.$init = function () {',
  17551. ' this.ClassType = null;',
  17552. ' };',
  17553. ' this.$final = function () {',
  17554. ' this.ClassType = undefined;',
  17555. ' };',
  17556. '});',
  17557. 'this.b = false;',
  17558. 'this.Obj = null;',
  17559. 'this.C = null;'
  17560. ]),
  17561. LinesToStr([ // $mod.$main
  17562. '$mod.b = $mod.C === null;',
  17563. '$mod.b = null === $mod.C;',
  17564. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  17565. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  17566. '$mod.b = $mod.C === $mod.TObject;',
  17567. '$mod.b = $mod.TObject === $mod.C;',
  17568. '$mod.b = $mod.C !== null;',
  17569. '$mod.b = null !== $mod.C;',
  17570. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  17571. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  17572. '$mod.b = $mod.C !== $mod.TObject;',
  17573. '$mod.b = $mod.TObject !== $mod.C;',
  17574. '']));
  17575. end;
  17576. procedure TTestModule.TestClassOf_ClassVar;
  17577. begin
  17578. StartProgram(false);
  17579. Add('type');
  17580. Add(' TObject = class');
  17581. Add(' class var id: longint;');
  17582. Add(' end;');
  17583. Add(' TClass = class of TObject;');
  17584. Add('var');
  17585. Add(' C: tclass;');
  17586. Add('begin');
  17587. Add(' C.id:=C.id;');
  17588. ConvertProgram;
  17589. CheckSource('TestClassOf_ClassVar',
  17590. LinesToStr([ // statements
  17591. 'rtl.createClass(this, "TObject", null, function () {',
  17592. ' this.id = 0;',
  17593. ' this.$init = function () {',
  17594. ' };',
  17595. ' this.$final = function () {',
  17596. ' };',
  17597. '});',
  17598. 'this.C = null;'
  17599. ]),
  17600. LinesToStr([ // $mod.$main
  17601. '$mod.TObject.id = $mod.C.id;',
  17602. '']));
  17603. end;
  17604. procedure TTestModule.TestClassOf_ClassMethod;
  17605. begin
  17606. StartProgram(false);
  17607. Add('type');
  17608. Add(' TObject = class');
  17609. Add(' class function DoIt(i: longint = 0): longint;');
  17610. Add(' end;');
  17611. Add(' TClass = class of TObject;');
  17612. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  17613. Add('var');
  17614. Add(' i: longint;');
  17615. Add(' C: tclass;');
  17616. Add('begin');
  17617. Add(' C.DoIt;');
  17618. Add(' C.DoIt();');
  17619. Add(' i:=C.DoIt;');
  17620. Add(' i:=C.DoIt();');
  17621. ConvertProgram;
  17622. CheckSource('TestClassOf_ClassMethod',
  17623. LinesToStr([ // statements
  17624. 'rtl.createClass(this, "TObject", null, function () {',
  17625. ' this.$init = function () {',
  17626. ' };',
  17627. ' this.$final = function () {',
  17628. ' };',
  17629. ' this.DoIt = function (i) {',
  17630. ' var Result = 0;',
  17631. ' return Result;',
  17632. ' };',
  17633. '});',
  17634. 'this.i = 0;',
  17635. 'this.C = null;'
  17636. ]),
  17637. LinesToStr([ // $mod.$main
  17638. '$mod.C.DoIt(0);',
  17639. '$mod.C.DoIt(0);',
  17640. '$mod.i = $mod.C.DoIt(0);',
  17641. '$mod.i = $mod.C.DoIt(0);',
  17642. '']));
  17643. end;
  17644. procedure TTestModule.TestClassOf_ClassProperty;
  17645. begin
  17646. StartProgram(false);
  17647. Add([
  17648. 'type',
  17649. ' TObject = class',
  17650. ' class var FA: longint;',
  17651. ' class function GetA: longint;',
  17652. ' class procedure SetA(Value: longint);',
  17653. ' class property pA: longint read fa write fa;',
  17654. ' class property pB: longint read geta write seta;',
  17655. ' end;',
  17656. ' TObjectClass = class of tobject;',
  17657. 'class function tobject.geta: longint; begin end;',
  17658. 'class procedure tobject.seta(value: longint); begin end;',
  17659. 'var',
  17660. ' b: boolean;',
  17661. ' Obj: tobject;',
  17662. ' Cla: tobjectclass;',
  17663. 'begin',
  17664. ' obj.pa:=obj.pa;',
  17665. ' obj.pb:=obj.pb;',
  17666. ' b:=obj.pa=4;',
  17667. ' b:=obj.pb=obj.pb;',
  17668. ' b:=5=obj.pa;',
  17669. ' cla.pa:=6;',
  17670. ' cla.pa:=cla.pa;',
  17671. ' cla.pb:=cla.pb;',
  17672. ' b:=cla.pa=7;',
  17673. ' b:=cla.pb=cla.pb;',
  17674. ' b:=8=cla.pa;',
  17675. ' tobject.pa:=9;',
  17676. ' tobject.pb:=tobject.pb;',
  17677. ' b:=tobject.pa=10;',
  17678. ' b:=11=tobject.pa;',
  17679. '']);
  17680. ConvertProgram;
  17681. CheckSource('TestClassOf_ClassProperty',
  17682. LinesToStr([ // statements
  17683. 'rtl.createClass(this, "TObject", null, function () {',
  17684. ' this.FA = 0;',
  17685. ' this.$init = function () {',
  17686. ' };',
  17687. ' this.$final = function () {',
  17688. ' };',
  17689. ' this.GetA = function () {',
  17690. ' var Result = 0;',
  17691. ' return Result;',
  17692. ' };',
  17693. ' this.SetA = function (Value) {',
  17694. ' };',
  17695. '});',
  17696. 'this.b = false;',
  17697. 'this.Obj = null;',
  17698. 'this.Cla = null;'
  17699. ]),
  17700. LinesToStr([ // $mod.$main
  17701. '$mod.TObject.FA = $mod.Obj.FA;',
  17702. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  17703. '$mod.b = $mod.Obj.FA === 4;',
  17704. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  17705. '$mod.b = 5 === $mod.Obj.FA;',
  17706. '$mod.TObject.FA = 6;',
  17707. '$mod.TObject.FA = $mod.Cla.FA;',
  17708. '$mod.Cla.SetA($mod.Cla.GetA());',
  17709. '$mod.b = $mod.Cla.FA === 7;',
  17710. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  17711. '$mod.b = 8 === $mod.Cla.FA;',
  17712. '$mod.TObject.FA = 9;',
  17713. '$mod.TObject.SetA($mod.TObject.GetA());',
  17714. '$mod.b = $mod.TObject.FA === 10;',
  17715. '$mod.b = 11 === $mod.TObject.FA;',
  17716. '']));
  17717. end;
  17718. procedure TTestModule.TestClassOf_ClassMethodSelf;
  17719. begin
  17720. StartProgram(false);
  17721. Add('type');
  17722. Add(' TObject = class');
  17723. Add(' class var GlobalId: longint;');
  17724. Add(' class procedure ProcA;');
  17725. Add(' end;');
  17726. Add('class procedure tobject.proca;');
  17727. Add('var b: boolean;');
  17728. Add('begin');
  17729. Add(' b:=self=nil;');
  17730. Add(' b:=self.globalid=3;');
  17731. Add(' b:=4=self.globalid;');
  17732. Add(' self.globalid:=5;');
  17733. Add(' self.proca;');
  17734. Add('end;');
  17735. Add('begin');
  17736. ConvertProgram;
  17737. CheckSource('TestClassOf_ClassMethodSelf',
  17738. LinesToStr([ // statements
  17739. 'rtl.createClass(this, "TObject", null, function () {',
  17740. ' this.GlobalId = 0;',
  17741. ' this.$init = function () {',
  17742. ' };',
  17743. ' this.$final = function () {',
  17744. ' };',
  17745. ' this.ProcA = function () {',
  17746. ' var b = false;',
  17747. ' b = this === null;',
  17748. ' b = this.GlobalId === 3;',
  17749. ' b = 4 === this.GlobalId;',
  17750. ' $mod.TObject.GlobalId = 5;',
  17751. ' this.ProcA();',
  17752. ' };',
  17753. '});'
  17754. ]),
  17755. LinesToStr([ // $mod.$main
  17756. '']));
  17757. end;
  17758. procedure TTestModule.TestClassOf_TypeCast;
  17759. begin
  17760. StartProgram(false);
  17761. Add('type');
  17762. Add(' TObject = class');
  17763. Add(' class procedure {#TObject_DoIt}DoIt;');
  17764. Add(' end;');
  17765. Add(' TClass = class of TObject;');
  17766. Add(' TMobile = class');
  17767. Add(' class procedure {#TMobile_DoIt}DoIt;');
  17768. Add(' end;');
  17769. Add(' TMobileClass = class of TMobile;');
  17770. Add(' TCar = class(TMobile)');
  17771. Add(' class procedure {#TCar_DoIt}DoIt;');
  17772. Add(' end;');
  17773. Add(' TCarClass = class of TCar;');
  17774. Add('class procedure TObject.DoIt;');
  17775. Add('begin');
  17776. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  17777. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  17778. Add('end;');
  17779. Add('class procedure TMobile.DoIt;');
  17780. Add('begin');
  17781. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  17782. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  17783. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  17784. Add('end;');
  17785. Add('class procedure TCar.DoIt; begin end;');
  17786. Add('var');
  17787. Add(' ObjC: TClass;');
  17788. Add(' MobileC: TMobileClass;');
  17789. Add(' CarC: TCarClass;');
  17790. Add('begin');
  17791. Add(' ObjC.{@TObject_DoIt}DoIt;');
  17792. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  17793. Add(' CarC.{@TCar_DoIt}DoIt;');
  17794. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  17795. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  17796. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  17797. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  17798. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  17799. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  17800. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  17801. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  17802. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  17803. ConvertProgram;
  17804. CheckSource('TestClassOf_TypeCast',
  17805. LinesToStr([ // statements
  17806. 'rtl.createClass(this, "TObject", null, function () {',
  17807. ' this.$init = function () {',
  17808. ' };',
  17809. ' this.$final = function () {',
  17810. ' };',
  17811. ' this.DoIt = function () {',
  17812. ' this.DoIt();',
  17813. ' this.DoIt$1();',
  17814. ' };',
  17815. '});',
  17816. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  17817. ' this.DoIt$1 = function () {',
  17818. ' this.DoIt();',
  17819. ' this.DoIt$1();',
  17820. ' this.DoIt$2();',
  17821. ' };',
  17822. '});',
  17823. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  17824. ' this.DoIt$2 = function () {',
  17825. ' };',
  17826. '});',
  17827. 'this.ObjC = null;',
  17828. 'this.MobileC = null;',
  17829. 'this.CarC = null;',
  17830. '']),
  17831. LinesToStr([ // $mod.$main
  17832. '$mod.ObjC.DoIt();',
  17833. '$mod.MobileC.DoIt$1();',
  17834. '$mod.CarC.DoIt$2();',
  17835. '$mod.ObjC.DoIt();',
  17836. '$mod.ObjC.DoIt$1();',
  17837. '$mod.ObjC.DoIt$2();',
  17838. '$mod.MobileC.DoIt();',
  17839. '$mod.MobileC.DoIt$1();',
  17840. '$mod.MobileC.DoIt$2();',
  17841. '$mod.CarC.DoIt();',
  17842. '$mod.CarC.DoIt$1();',
  17843. '$mod.CarC.DoIt$2();',
  17844. '']));
  17845. end;
  17846. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  17847. begin
  17848. StartProgram(false);
  17849. Add('type');
  17850. Add(' TObject = class');
  17851. Add(' function CurNow: longint; ');
  17852. Add(' class function Now: longint; ');
  17853. Add(' end;');
  17854. Add('function TObject.CurNow: longint; begin end;');
  17855. Add('class function TObject.Now: longint; begin end;');
  17856. Add('var');
  17857. Add(' Obj: tobject;');
  17858. Add(' vI: longint;');
  17859. Add('begin');
  17860. Add(' obj.curnow;');
  17861. Add(' vi:=obj.curnow;');
  17862. Add(' tobject.now;');
  17863. Add(' vi:=tobject.now;');
  17864. ConvertProgram;
  17865. CheckSource('TestClassOf_ImplicitFunctionCall',
  17866. LinesToStr([ // statements
  17867. 'rtl.createClass(this, "TObject", null, function () {',
  17868. ' this.$init = function () {',
  17869. ' };',
  17870. ' this.$final = function () {',
  17871. ' };',
  17872. ' this.CurNow = function () {',
  17873. ' var Result = 0;',
  17874. ' return Result;',
  17875. ' };',
  17876. ' this.Now = function () {',
  17877. ' var Result = 0;',
  17878. ' return Result;',
  17879. ' };',
  17880. '});',
  17881. 'this.Obj = null;',
  17882. 'this.vI = 0;',
  17883. '']),
  17884. LinesToStr([ // $mod.$main
  17885. '$mod.Obj.CurNow();',
  17886. '$mod.vI = $mod.Obj.CurNow();',
  17887. '$mod.TObject.Now();',
  17888. '$mod.vI = $mod.TObject.Now();',
  17889. '']));
  17890. end;
  17891. procedure TTestModule.TestClassOf_Const;
  17892. begin
  17893. StartProgram(false);
  17894. Add([
  17895. 'type',
  17896. ' TObject = class',
  17897. ' end;',
  17898. ' TBird = TObject;',
  17899. ' TBirds = class of TBird;',
  17900. ' TEagles = TBirds;',
  17901. ' THawk = class(TBird);',
  17902. 'const',
  17903. ' Hawk: TEagles = THawk;',
  17904. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  17905. ' TBird,',
  17906. ' THawk',
  17907. ' );',
  17908. 'begin']);
  17909. ConvertProgram;
  17910. CheckSource('TestClassOf_Const',
  17911. LinesToStr([ // statements
  17912. 'rtl.createClass(this, "TObject", null, function () {',
  17913. ' this.$init = function () {',
  17914. ' };',
  17915. ' this.$final = function () {',
  17916. ' };',
  17917. '});',
  17918. 'rtl.createClass(this, "THawk", this.TObject, function () {',
  17919. '});',
  17920. 'this.Hawk = this.THawk;',
  17921. 'this.DefaultBirdClasses = [this.TObject, this.THawk];',
  17922. '']),
  17923. LinesToStr([ // $mod.$main
  17924. '']));
  17925. end;
  17926. procedure TTestModule.TestNestedClass_Alias;
  17927. begin
  17928. WithTypeInfo:=true;
  17929. StartProgram(false);
  17930. Add([
  17931. 'type',
  17932. ' TObject = class',
  17933. ' type TNested = type longint;',
  17934. ' end;',
  17935. 'type TAlias = type tobject.tnested;',
  17936. 'var i: tobject.tnested = 3;',
  17937. 'var j: TAlias = 4;',
  17938. 'begin',
  17939. ' if typeinfo(TAlias)=nil then ;',
  17940. ' if typeinfo(tobject.tnested)=nil then ;',
  17941. '']);
  17942. ConvertProgram;
  17943. CheckSource('TestNestedClass_Alias',
  17944. LinesToStr([ // statements
  17945. 'rtl.createClass(this, "TObject", null, function () {',
  17946. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  17947. ' this.$init = function () {',
  17948. ' };',
  17949. ' this.$final = function () {',
  17950. ' };',
  17951. '});',
  17952. 'this.$rtti.$inherited("TAlias", this.$rtti["TObject.TNested"], {});',
  17953. 'this.i = 3;',
  17954. 'this.j = 4;',
  17955. '']),
  17956. LinesToStr([ // $mod.$main
  17957. 'if ($mod.$rtti["TAlias"] === null) ;',
  17958. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  17959. '']));
  17960. end;
  17961. procedure TTestModule.TestNestedClass_Record;
  17962. begin
  17963. WithTypeInfo:=true;
  17964. StartProgram(false);
  17965. Add([
  17966. 'type',
  17967. ' TObject = class',
  17968. ' type TPoint = record',
  17969. ' x,y: byte;',
  17970. ' end;',
  17971. ' procedure DoIt(t: TPoint);',
  17972. ' end;',
  17973. 'procedure tobject.DoIt(t: TPoint);',
  17974. 'var p: TPoint;',
  17975. 'begin',
  17976. ' t.x:=t.y;',
  17977. ' p:=t;',
  17978. 'end;',
  17979. 'var',
  17980. ' p: tobject.tpoint = (x:2; y:4);',
  17981. ' o: TObject;',
  17982. 'begin',
  17983. ' p:=p;',
  17984. ' o.doit(p);',
  17985. '']);
  17986. ConvertProgram;
  17987. CheckSource('TestNestedClass_Record',
  17988. LinesToStr([ // statements
  17989. 'rtl.createClass(this, "TObject", null, function () {',
  17990. ' rtl.recNewT(this, "TPoint", function () {',
  17991. ' this.x = 0;',
  17992. ' this.y = 0;',
  17993. ' this.$eq = function (b) {',
  17994. ' return (this.x === b.x) && (this.y === b.y);',
  17995. ' };',
  17996. ' this.$assign = function (s) {',
  17997. ' this.x = s.x;',
  17998. ' this.y = s.y;',
  17999. ' return this;',
  18000. ' };',
  18001. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  18002. ' $r.addField("x", rtl.byte);',
  18003. ' $r.addField("y", rtl.byte);',
  18004. ' });',
  18005. ' this.$init = function () {',
  18006. ' };',
  18007. ' this.$final = function () {',
  18008. ' };',
  18009. ' this.DoIt = function (t) {',
  18010. ' var p = this.TPoint.$new();',
  18011. ' t.x = t.y;',
  18012. ' p.$assign(t);',
  18013. ' };',
  18014. '});',
  18015. 'this.p = this.TObject.TPoint.$clone({',
  18016. ' x: 2,',
  18017. ' y: 4',
  18018. '});',
  18019. 'this.o = null;',
  18020. '']),
  18021. LinesToStr([ // $mod.$main
  18022. '$mod.p.$assign($mod.p);',
  18023. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  18024. '']));
  18025. end;
  18026. procedure TTestModule.TestNestedClass_Class;
  18027. begin
  18028. StartProgram(false);
  18029. Add([
  18030. 'type',
  18031. ' TObject = class end;',
  18032. ' TBird = class',
  18033. ' type TLeg = class',
  18034. ' FId: longint;',
  18035. ' constructor Create;',
  18036. ' function Create(i: longint): TLeg;',
  18037. ' end;',
  18038. ' function DoIt(b: TBird): Tleg;',
  18039. ' end;',
  18040. 'constructor tbird.tleg.create;',
  18041. 'begin',
  18042. ' FId:=3;',
  18043. 'end;',
  18044. 'function tbird.tleg.Create(i: longint): TLeg;',
  18045. 'begin',
  18046. ' Create;',
  18047. ' Result:=TLeg.Create;',
  18048. ' Result:=TBird.TLeg.Create;',
  18049. ' Result:=Create(3);',
  18050. ' FId:=i;',
  18051. 'end;',
  18052. 'function tbird.DoIt(b: tbird): tleg;',
  18053. 'begin',
  18054. ' Result.Create;',
  18055. ' Result:=TLeg.Create;',
  18056. ' Result:=TBird.TLeg.Create;',
  18057. ' Result:=Result.Create(3);',
  18058. 'end;',
  18059. 'var',
  18060. ' b: Tbird.tleg;',
  18061. 'begin',
  18062. ' b.Create;',
  18063. ' b:=TBird.TLeg.Create;',
  18064. ' b:=b.Create(3);',
  18065. '']);
  18066. ConvertProgram;
  18067. CheckSource('TestNestedClass_Class',
  18068. LinesToStr([ // statements
  18069. 'rtl.createClass(this, "TObject", null, function () {',
  18070. ' this.$init = function () {',
  18071. ' };',
  18072. ' this.$final = function () {',
  18073. ' };',
  18074. '});',
  18075. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18076. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  18077. ' this.$init = function () {',
  18078. ' $mod.TObject.$init.call(this);',
  18079. ' this.FId = 0;',
  18080. ' };',
  18081. ' this.Create = function () {',
  18082. ' this.FId = 3;',
  18083. ' return this;',
  18084. ' };',
  18085. ' this.Create$1 = function (i) {',
  18086. ' var Result = null;',
  18087. ' this.Create();',
  18088. ' Result = $mod.TBird.TLeg.$create("Create");',
  18089. ' Result = $mod.TBird.TLeg.$create("Create");',
  18090. ' Result = this.Create$1(3);',
  18091. ' this.FId = i;',
  18092. ' return Result;',
  18093. ' };',
  18094. ' }, "TBird.TLeg");',
  18095. ' this.DoIt = function (b) {',
  18096. ' var Result = null;',
  18097. ' Result.Create();',
  18098. ' Result = this.TLeg.$create("Create");',
  18099. ' Result = $mod.TBird.TLeg.$create("Create");',
  18100. ' Result = Result.Create$1(3);',
  18101. ' return Result;',
  18102. ' };',
  18103. '});',
  18104. 'this.b = null;',
  18105. '']),
  18106. LinesToStr([ // $mod.$main
  18107. '$mod.b.Create();',
  18108. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  18109. '$mod.b = $mod.b.Create$1(3);',
  18110. '']));
  18111. end;
  18112. procedure TTestModule.TestNestedClass_CallInherited;
  18113. begin
  18114. StartProgram(false);
  18115. Add([
  18116. 'type',
  18117. ' TObject = class end;',
  18118. ' TBird = class',
  18119. ' type',
  18120. ' TWing = class',
  18121. ' function Fly(w: word = 17): word; virtual;',
  18122. ' end;',
  18123. ' end;',
  18124. ' TEagle = class(TBird)',
  18125. ' type',
  18126. ' TEagleWing = class(TWing)',
  18127. ' function Fly(w: word): word; override;',
  18128. ' end;',
  18129. ' end;',
  18130. 'function TBird.TWing.Fly(w: word): word;',
  18131. 'begin',
  18132. 'end;',
  18133. 'function TEagle.TEagleWing.Fly(w: word): word;',
  18134. 'begin',
  18135. ' inherited;',
  18136. ' inherited Fly;',
  18137. ' inherited Fly(3);',
  18138. ' Result:=inherited Fly;',
  18139. ' Result:=inherited Fly(4);',
  18140. 'end;',
  18141. 'begin',
  18142. '']);
  18143. ConvertProgram;
  18144. CheckSource('TestNestedClass_CallInherited',
  18145. LinesToStr([ // statements
  18146. 'rtl.createClass(this, "TObject", null, function () {',
  18147. ' this.$init = function () {',
  18148. ' };',
  18149. ' this.$final = function () {',
  18150. ' };',
  18151. '});',
  18152. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18153. ' rtl.createClass(this, "TWing", $mod.TObject, function () {',
  18154. ' this.Fly = function (w) {',
  18155. ' var Result = 0;',
  18156. ' return Result;',
  18157. ' };',
  18158. ' }, "TBird.TWing");',
  18159. '});',
  18160. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  18161. ' rtl.createClass(this, "TEagleWing", this.TWing, function () {',
  18162. ' this.Fly = function (w) {',
  18163. ' var Result = 0;',
  18164. ' $mod.TBird.TWing.Fly.apply(this, arguments);',
  18165. ' $mod.TBird.TWing.Fly.call(this, 17);',
  18166. ' $mod.TBird.TWing.Fly.call(this, 3);',
  18167. ' Result = $mod.TBird.TWing.Fly.call(this, 17);',
  18168. ' Result = $mod.TBird.TWing.Fly.call(this, 4);',
  18169. ' return Result;',
  18170. ' };',
  18171. ' }, "TEagle.TEagleWing");',
  18172. '});',
  18173. '']),
  18174. LinesToStr([ // $mod.$main
  18175. '']));
  18176. end;
  18177. procedure TTestModule.TestExternalClass_Var;
  18178. begin
  18179. StartProgram(false);
  18180. Add([
  18181. '{$modeswitch externalclass}',
  18182. 'type',
  18183. ' TExtA = class external name ''ExtObj''',
  18184. ' Id: longint external name ''$Id'';',
  18185. ' B: longint;',
  18186. ' end;',
  18187. 'var Obj: TExtA;',
  18188. 'begin',
  18189. ' obj.id:=obj.id+1;',
  18190. ' obj.B:=obj.B+1;']);
  18191. ConvertProgram;
  18192. CheckSource('TestExternalClass_Var',
  18193. LinesToStr([ // statements
  18194. 'this.Obj = null;',
  18195. '']),
  18196. LinesToStr([ // $mod.$main
  18197. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  18198. '$mod.Obj.B = $mod.Obj.B + 1;',
  18199. '']));
  18200. end;
  18201. procedure TTestModule.TestExternalClass_Const;
  18202. begin
  18203. StartProgram(false);
  18204. Add([
  18205. '{$modeswitch externalclass}',
  18206. 'type',
  18207. ' TExtA = class external name ''ExtObj''',
  18208. ' const Two: longint = 2;',
  18209. ' const Three = 3;',
  18210. ' const Id: longint;',
  18211. ' end;',
  18212. ' TExtB = class external name ''ExtB''',
  18213. ' A: TExtA;',
  18214. ' end;',
  18215. 'var',
  18216. ' A: texta;',
  18217. ' B: textb;',
  18218. ' i: longint;',
  18219. 'begin',
  18220. ' i:=a.two;',
  18221. ' i:=texta.two;',
  18222. ' i:=a.three;',
  18223. ' i:=texta.three;',
  18224. ' i:=a.id;',
  18225. ' i:=texta.id;',
  18226. '']);
  18227. ConvertProgram;
  18228. CheckSource('TestExternalClass_Const',
  18229. LinesToStr([ // statements
  18230. 'this.A = null;',
  18231. 'this.B = null;',
  18232. 'this.i = 0;',
  18233. '']),
  18234. LinesToStr([ // $mod.$main
  18235. '$mod.i = 2;',
  18236. '$mod.i = 2;',
  18237. '$mod.i = 3;',
  18238. '$mod.i = 3;',
  18239. '$mod.i = $mod.A.Id;',
  18240. '$mod.i = ExtObj.Id;',
  18241. '']));
  18242. end;
  18243. procedure TTestModule.TestExternalClass_Dollar;
  18244. begin
  18245. StartProgram(false);
  18246. Add([
  18247. '{$modeswitch externalclass}',
  18248. 'type',
  18249. ' TExtA = class external name ''$''',
  18250. ' Id: longint external name ''$'';',
  18251. ' function Bla(i: longint): longint; external name ''$'';',
  18252. ' end;',
  18253. 'function dollar(k: longint): longint; external name ''$'';',
  18254. 'var Obj: TExtA;',
  18255. 'begin',
  18256. ' dollar(1);',
  18257. ' obj.id:=obj.id+2;',
  18258. ' obj.Bla(3);',
  18259. '']);
  18260. ConvertProgram;
  18261. CheckSource('TestExternalClass_Dollar',
  18262. LinesToStr([ // statements
  18263. 'this.Obj = null;',
  18264. '']),
  18265. LinesToStr([ // $mod.$main
  18266. '$(1);',
  18267. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  18268. '$mod.Obj.$(3);',
  18269. '']));
  18270. end;
  18271. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  18272. begin
  18273. StartProgram(false);
  18274. Add('{$modeswitch externalclass}');
  18275. Add('type');
  18276. Add(' TExtA = class external name ''ExtA''');
  18277. Add(' Id: longint external name ''$Id'';');
  18278. Add(' end;');
  18279. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  18280. Add(' Id: longint;');
  18281. Add(' end;');
  18282. Add('begin');
  18283. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  18284. ConvertProgram;
  18285. end;
  18286. procedure TTestModule.TestExternalClass_Method;
  18287. begin
  18288. StartProgram(false);
  18289. Add(['{$modeswitch externalclass}',
  18290. 'type',
  18291. ' TExtA = class external name ''ExtObj''',
  18292. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  18293. ' procedure DoSome(Id: longint = 1);',
  18294. ' end;',
  18295. 'var Obj: texta;',
  18296. 'begin',
  18297. ' obj.doit;',
  18298. ' obj.doit();',
  18299. ' obj.doit(2);',
  18300. ' with obj do begin',
  18301. ' doit;',
  18302. ' doit();',
  18303. ' doit(3);',
  18304. ' end;']);
  18305. ConvertProgram;
  18306. CheckSource('TestExternalClass_Method',
  18307. LinesToStr([ // statements
  18308. 'this.Obj = null;',
  18309. '']),
  18310. LinesToStr([ // $mod.$main
  18311. '$mod.Obj.$Execute(1);',
  18312. '$mod.Obj.$Execute(1);',
  18313. '$mod.Obj.$Execute(2);',
  18314. 'var $with = $mod.Obj;',
  18315. '$with.$Execute(1);',
  18316. '$with.$Execute(1);',
  18317. '$with.$Execute(3);',
  18318. '']));
  18319. end;
  18320. procedure TTestModule.TestExternalClass_ClassMethod;
  18321. begin
  18322. StartProgram(false);
  18323. Add([
  18324. '{$modeswitch externalclass}',
  18325. 'type',
  18326. ' TExtA = class external name ''ExtObj''',
  18327. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  18328. ' end;',
  18329. ' TExtB = TExtA;',
  18330. 'var p: Pointer;',
  18331. 'begin',
  18332. ' texta.doit;',
  18333. ' texta.doit();',
  18334. ' texta.doit(2);',
  18335. ' p:[email protected];',
  18336. ' with texta do begin',
  18337. ' doit;',
  18338. ' doit();',
  18339. ' doit(3);',
  18340. ' p:=@DoIt;',
  18341. ' end;',
  18342. ' textb.doit;',
  18343. ' textb.doit();',
  18344. ' textb.doit(4);',
  18345. ' with textb do begin',
  18346. ' doit;',
  18347. ' doit();',
  18348. ' doit(5);',
  18349. ' end;',
  18350. '']);
  18351. ConvertProgram;
  18352. CheckSource('TestExternalClass_ClassMethod',
  18353. LinesToStr([ // statements
  18354. 'this.p = null;',
  18355. '']),
  18356. LinesToStr([ // $mod.$main
  18357. 'ExtObj.$Execute(1);',
  18358. 'ExtObj.$Execute(1);',
  18359. 'ExtObj.$Execute(2);',
  18360. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  18361. 'ExtObj.$Execute(1);',
  18362. 'ExtObj.$Execute(1);',
  18363. 'ExtObj.$Execute(3);',
  18364. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  18365. 'ExtObj.$Execute(1);',
  18366. 'ExtObj.$Execute(1);',
  18367. 'ExtObj.$Execute(4);',
  18368. 'ExtObj.$Execute(1);',
  18369. 'ExtObj.$Execute(1);',
  18370. 'ExtObj.$Execute(5);',
  18371. '']));
  18372. end;
  18373. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  18374. begin
  18375. StartProgram(false);
  18376. Add([
  18377. '{$modeswitch externalclass}',
  18378. 'type',
  18379. ' TExtA = class external name ''ExtObj''',
  18380. ' class procedure DoIt(Id: longint = 1); static;',
  18381. ' end;',
  18382. 'var p: Pointer;',
  18383. 'begin',
  18384. ' texta.doit;',
  18385. ' texta.doit();',
  18386. ' texta.doit(2);',
  18387. ' p:[email protected];',
  18388. ' with texta do begin',
  18389. ' doit;',
  18390. ' doit();',
  18391. ' doit(3);',
  18392. ' p:=@DoIt;',
  18393. ' end;',
  18394. '']);
  18395. ConvertProgram;
  18396. CheckSource('TestExternalClass_ClassMethodStatic',
  18397. LinesToStr([ // statements
  18398. 'this.p = null;',
  18399. '']),
  18400. LinesToStr([ // $mod.$main
  18401. 'ExtObj.DoIt(1);',
  18402. 'ExtObj.DoIt(1);',
  18403. 'ExtObj.DoIt(2);',
  18404. '$mod.p = ExtObj.DoIt;',
  18405. 'ExtObj.DoIt(1);',
  18406. 'ExtObj.DoIt(1);',
  18407. 'ExtObj.DoIt(3);',
  18408. '$mod.p = ExtObj.DoIt;',
  18409. '']));
  18410. end;
  18411. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  18412. begin
  18413. StartProgram(false);
  18414. Add([
  18415. '{$modeswitch externalclass}',
  18416. 'type',
  18417. ' TBird = class external name ''Array''',
  18418. ' end;',
  18419. 'function GetPtr: Pointer;',
  18420. 'begin',
  18421. 'end;',
  18422. 'procedure Write(const p);',
  18423. 'begin',
  18424. 'end;',
  18425. 'procedure WriteLn; varargs;',
  18426. 'begin',
  18427. 'end;',
  18428. 'begin',
  18429. ' if TBird(GetPtr)=nil then ;',
  18430. ' Write(GetPtr);',
  18431. ' WriteLn(GetPtr);',
  18432. ' Write(TBird(GetPtr));',
  18433. ' WriteLn(TBird(GetPtr));',
  18434. '']);
  18435. ConvertProgram;
  18436. CheckSource('TestFunctionResultInTypeCast',
  18437. LinesToStr([ // statements
  18438. 'this.GetPtr = function () {',
  18439. ' var Result = null;',
  18440. ' return Result;',
  18441. '};',
  18442. 'this.Write = function (p) {',
  18443. '};',
  18444. 'this.WriteLn = function () {',
  18445. '};',
  18446. '']),
  18447. LinesToStr([
  18448. 'if ($mod.GetPtr() === null) ;',
  18449. '$mod.Write($mod.GetPtr());',
  18450. '$mod.WriteLn($mod.GetPtr());',
  18451. '$mod.Write($mod.GetPtr());',
  18452. '$mod.WriteLn($mod.GetPtr());',
  18453. '']));
  18454. end;
  18455. procedure TTestModule.TestExternalClass_NonExternalOverride;
  18456. begin
  18457. StartProgram(false);
  18458. Add([
  18459. '{$modeswitch externalclass}',
  18460. 'type',
  18461. ' TExtA = class external name ''ExtObjA''',
  18462. ' procedure ProcA; virtual;',
  18463. ' procedure ProcB; virtual;',
  18464. ' end;',
  18465. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  18466. ' end;',
  18467. ' TExtC = class (TExtB)',
  18468. ' procedure ProcA; override;',
  18469. ' end;',
  18470. 'procedure TExtC.ProcA;',
  18471. 'begin',
  18472. ' ProcA;',
  18473. ' Self.ProcA;',
  18474. ' ProcB;',
  18475. ' Self.ProcB;',
  18476. 'end;',
  18477. 'var',
  18478. ' A: texta;',
  18479. ' B: textb;',
  18480. ' C: textc;',
  18481. 'begin',
  18482. ' a.proca;',
  18483. ' b.proca;',
  18484. ' c.proca;']);
  18485. ConvertProgram;
  18486. CheckSource('TestExternalClass_NonExternalOverride',
  18487. LinesToStr([ // statements
  18488. 'rtl.createClassExt(this, "TExtC", ExtObjB, "", function () {',
  18489. ' this.$init = function () {',
  18490. ' };',
  18491. ' this.$final = function () {',
  18492. ' };',
  18493. ' this.ProcA = function () {',
  18494. ' this.ProcA();',
  18495. ' this.ProcA();',
  18496. ' this.ProcB();',
  18497. ' this.ProcB();',
  18498. ' };',
  18499. '});',
  18500. 'this.A = null;',
  18501. 'this.B = null;',
  18502. 'this.C = null;',
  18503. '']),
  18504. LinesToStr([ // $mod.$main
  18505. '$mod.A.ProcA();',
  18506. '$mod.B.ProcA();',
  18507. '$mod.C.ProcA();',
  18508. '']));
  18509. end;
  18510. procedure TTestModule.TestExternalClass_OverloadHint;
  18511. begin
  18512. StartProgram(false);
  18513. Add([
  18514. '{$modeswitch externalclass}',
  18515. 'type',
  18516. ' TExtA = class external name ''ExtObjA''',
  18517. ' procedure DoIt;',
  18518. ' procedure DoIt(i: longint);',
  18519. ' end;',
  18520. 'begin',
  18521. '']);
  18522. ConvertProgram;
  18523. CheckResolverUnexpectedHints(true);
  18524. CheckSource('TestExternalClass_OverloadHint',
  18525. LinesToStr([ // statements
  18526. '']),
  18527. LinesToStr([ // $mod.$main
  18528. '']));
  18529. end;
  18530. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  18531. begin
  18532. WithTypeInfo:=true;
  18533. StartProgram(false);
  18534. Add([
  18535. '{$modeswitch externalclass}',
  18536. 'type',
  18537. ' JSwiper = class external name ''Swiper''',
  18538. ' constructor New;',
  18539. ' end;',
  18540. ' TObject = class',
  18541. ' private',
  18542. ' FSwiper: JSwiper;',
  18543. ' published',
  18544. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  18545. ' end;',
  18546. 'begin',
  18547. ' JSwiper.new;',
  18548. '']);
  18549. ConvertProgram;
  18550. CheckSource('TestExternalClass_SameNamePublishedProperty',
  18551. LinesToStr([ // statements
  18552. 'this.$rtti.$ExtClass("JSwiper", {',
  18553. ' jsclass: "Swiper"',
  18554. '});',
  18555. 'rtl.createClass(this, "TObject", null, function () {',
  18556. ' this.$init = function () {',
  18557. ' this.FSwiper = null;',
  18558. ' };',
  18559. ' this.$final = function () {',
  18560. ' this.FSwiper = undefined;',
  18561. ' };',
  18562. ' var $r = this.$rtti;',
  18563. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  18564. '});',
  18565. '']),
  18566. LinesToStr([ // $mod.$main
  18567. 'new Swiper();',
  18568. '']));
  18569. end;
  18570. procedure TTestModule.TestExternalClass_Property;
  18571. begin
  18572. StartProgram(false);
  18573. Add([
  18574. '{$modeswitch externalclass}',
  18575. 'type',
  18576. ' TExtA = class external name ''ExtA''',
  18577. ' function getYear: longint;',
  18578. ' procedure setYear(Value: longint);',
  18579. ' property Year: longint read getyear write setyear;',
  18580. ' end;',
  18581. ' TExtB = class (TExtA)',
  18582. ' procedure OtherSetYear(Value: longint);',
  18583. ' property year write othersetyear;',
  18584. ' end;',
  18585. 'procedure textb.othersetyear(value: longint);',
  18586. 'begin',
  18587. ' setYear(Value+4);',
  18588. 'end;',
  18589. 'var',
  18590. ' A: texta;',
  18591. ' B: textb;',
  18592. 'begin',
  18593. ' a.year:=a.year+1;',
  18594. ' b.year:=b.year+2;']);
  18595. ConvertProgram;
  18596. CheckSource('TestExternalClass_NonExternalOverride',
  18597. LinesToStr([ // statements
  18598. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18599. ' this.$init = function () {',
  18600. ' };',
  18601. ' this.$final = function () {',
  18602. ' };',
  18603. ' this.OtherSetYear = function (Value) {',
  18604. ' this.setYear(Value+4);',
  18605. ' };',
  18606. '});',
  18607. 'this.A = null;',
  18608. 'this.B = null;',
  18609. '']),
  18610. LinesToStr([ // $mod.$main
  18611. '$mod.A.setYear($mod.A.getYear()+1);',
  18612. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  18613. '']));
  18614. end;
  18615. procedure TTestModule.TestExternalClass_PropertyDate;
  18616. begin
  18617. StartProgram(false);
  18618. Add([
  18619. '{$modeswitch externalclass}',
  18620. 'type',
  18621. ' TExtA = class external name ''ExtA''',
  18622. ' end;',
  18623. ' TExtB = class (TExtA)',
  18624. ' FDate: string;',
  18625. ' property Date: string read FDate write FDate;',
  18626. ' property ExtA: string read FDate write FDate;',
  18627. ' end;',
  18628. ' {$M+}',
  18629. ' TObject = class',
  18630. ' FDate: string;',
  18631. ' published',
  18632. ' property Date: string read FDate write FDate;',
  18633. ' property ExtA: string read FDate write FDate;',
  18634. ' end;',
  18635. 'var',
  18636. ' B: textb;',
  18637. ' o: TObject;',
  18638. 'begin',
  18639. ' b.date:=b.exta;',
  18640. ' o.date:=o.exta;']);
  18641. ConvertProgram;
  18642. CheckSource('TestExternalClass_PropertyDate',
  18643. LinesToStr([ // statements
  18644. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18645. ' this.$init = function () {',
  18646. ' this.FDate = "";',
  18647. ' };',
  18648. ' this.$final = function () {',
  18649. ' };',
  18650. '});',
  18651. 'rtl.createClass(this, "TObject", null, function () {',
  18652. ' this.$init = function () {',
  18653. ' this.FDate = "";',
  18654. ' };',
  18655. ' this.$final = function () {',
  18656. ' };',
  18657. ' var $r = this.$rtti;',
  18658. ' $r.addField("FDate", rtl.string, 4);',
  18659. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  18660. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  18661. '});',
  18662. 'this.B = null;',
  18663. 'this.o = null;',
  18664. '']),
  18665. LinesToStr([ // $mod.$main
  18666. '$mod.B.FDate = $mod.B.FDate;',
  18667. '$mod.o.FDate = $mod.o.FDate;',
  18668. '']));
  18669. end;
  18670. procedure TTestModule.TestExternalClass_ClassProperty;
  18671. begin
  18672. StartProgram(false);
  18673. Add('{$modeswitch externalclass}');
  18674. Add('type');
  18675. Add(' TExtA = class external name ''ExtA''');
  18676. Add(' class function getYear: longint;');
  18677. Add(' class procedure setYear(Value: longint);');
  18678. Add(' class property Year: longint read getyear write setyear;');
  18679. Add(' end;');
  18680. Add(' TExtB = class (TExtA)');
  18681. Add(' class function GetCentury: longint;');
  18682. Add(' class procedure SetCentury(Value: longint);');
  18683. Add(' class property Century: longint read getcentury write setcentury;');
  18684. Add(' end;');
  18685. Add('class function textb.getcentury: longint;');
  18686. Add('begin');
  18687. Add('end;');
  18688. Add('class procedure textb.setcentury(value: longint);');
  18689. Add('begin');
  18690. Add(' setyear(value+11);');
  18691. Add(' texta.year:=texta.year+12;');
  18692. Add(' year:=year+13;');
  18693. Add(' textb.century:=textb.century+14;');
  18694. Add(' century:=century+15;');
  18695. Add('end;');
  18696. Add('var');
  18697. Add(' A: texta;');
  18698. Add(' B: textb;');
  18699. Add('begin');
  18700. Add(' texta.year:=texta.year+1;');
  18701. Add(' textb.year:=textb.year+2;');
  18702. Add(' TextA.year:=TextA.year+3;');
  18703. Add(' b.year:=b.year+4;');
  18704. Add(' textb.century:=textb.century+5;');
  18705. Add(' b.century:=b.century+6;');
  18706. ConvertProgram;
  18707. CheckSource('TestExternalClass_ClassProperty',
  18708. LinesToStr([ // statements
  18709. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18710. ' this.$init = function () {',
  18711. ' };',
  18712. ' this.$final = function () {',
  18713. ' };',
  18714. ' this.GetCentury = function () {',
  18715. ' var Result = 0;',
  18716. ' return Result;',
  18717. ' };',
  18718. ' this.SetCentury = function (Value) {',
  18719. ' this.setYear(Value + 11);',
  18720. ' ExtA.setYear(ExtA.getYear() + 12);',
  18721. ' this.setYear(this.getYear() + 13);',
  18722. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  18723. ' this.SetCentury(this.GetCentury() + 15);',
  18724. ' };',
  18725. '});',
  18726. 'this.A = null;',
  18727. 'this.B = null;',
  18728. '']),
  18729. LinesToStr([ // $mod.$main
  18730. 'ExtA.setYear(ExtA.getYear() + 1);',
  18731. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  18732. 'ExtA.setYear(ExtA.getYear() + 3);',
  18733. '$mod.B.setYear($mod.B.getYear() + 4);',
  18734. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  18735. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  18736. '']));
  18737. end;
  18738. procedure TTestModule.TestExternalClass_ClassOf;
  18739. begin
  18740. StartProgram(false);
  18741. Add('{$modeswitch externalclass}');
  18742. Add('type');
  18743. Add(' TExtA = class external name ''ExtA''');
  18744. Add(' procedure ProcA; virtual;');
  18745. Add(' procedure ProcB; virtual;');
  18746. Add(' end;');
  18747. Add(' TExtAClass = class of TExtA;');
  18748. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  18749. Add(' end;');
  18750. Add(' TExtBClass = class of TExtB;');
  18751. Add(' TExtC = class (TExtB)');
  18752. Add(' procedure ProcA; override;');
  18753. Add(' end;');
  18754. Add(' TExtCClass = class of TExtC;');
  18755. Add('procedure TExtC.ProcA; begin end;');
  18756. Add('var');
  18757. Add(' A: texta; ClA: TExtAClass;');
  18758. Add(' B: textb; ClB: TExtBClass;');
  18759. Add(' C: textc; ClC: TExtCClass;');
  18760. Add('begin');
  18761. Add(' ClA:=texta;');
  18762. Add(' ClA:=textb;');
  18763. Add(' ClA:=textc;');
  18764. Add(' ClB:=textb;');
  18765. Add(' ClB:=textc;');
  18766. Add(' ClC:=textc;');
  18767. ConvertProgram;
  18768. CheckSource('TestExternalClass_ClassOf',
  18769. LinesToStr([ // statements
  18770. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18771. ' this.$init = function () {',
  18772. ' };',
  18773. ' this.$final = function () {',
  18774. ' };',
  18775. ' this.ProcA = function () {',
  18776. ' };',
  18777. '});',
  18778. 'this.A = null;',
  18779. 'this.ClA = null;',
  18780. 'this.B = null;',
  18781. 'this.ClB = null;',
  18782. 'this.C = null;',
  18783. 'this.ClC = null;',
  18784. '']),
  18785. LinesToStr([ // $mod.$main
  18786. '$mod.ClA = ExtA;',
  18787. '$mod.ClA = ExtB;',
  18788. '$mod.ClA = $mod.TExtC;',
  18789. '$mod.ClB = ExtB;',
  18790. '$mod.ClB = $mod.TExtC;',
  18791. '$mod.ClC = $mod.TExtC;',
  18792. '']));
  18793. end;
  18794. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  18795. begin
  18796. AddModuleWithIntfImplSrc('unit2.pas',
  18797. LinesToStr([
  18798. '{$modeswitch externalclass}',
  18799. 'type',
  18800. ' TExtA = class external name ''ExtA''',
  18801. ' class var Id: longint;',
  18802. ' end;',
  18803. '']),
  18804. '');
  18805. StartUnit(true);
  18806. Add('interface');
  18807. Add('uses unit2;');
  18808. Add('implementation');
  18809. Add('begin');
  18810. Add(' unit2.texta.id:=unit2.texta.id+1;');
  18811. ConvertUnit;
  18812. CheckSource('TestExternalClass_ClassOtherUnit',
  18813. LinesToStr([
  18814. '']),
  18815. LinesToStr([
  18816. 'ExtA.Id = ExtA.Id + 1;',
  18817. '']));
  18818. end;
  18819. procedure TTestModule.TestExternalClass_Is;
  18820. begin
  18821. StartProgram(false);
  18822. Add([
  18823. '{$modeswitch externalclass}',
  18824. 'type',
  18825. ' TExtA = class external name ''ExtA''',
  18826. ' end;',
  18827. ' TExtAClass = class of TExtA;',
  18828. ' TExtB = class external name ''ExtB'' (TExtA)',
  18829. ' end;',
  18830. ' TExtBClass = class of TExtB;',
  18831. ' TExtC = class (TExtB)',
  18832. ' end;',
  18833. ' TExtCClass = class of TExtC;',
  18834. 'var',
  18835. ' A: texta; ClA: TExtAClass;',
  18836. ' B: textb; ClB: TExtBClass;',
  18837. ' C: textc; ClC: TExtCClass;',
  18838. 'begin',
  18839. ' if a is textb then ;',
  18840. ' if a is textc then ;',
  18841. ' if b is textc then ;',
  18842. ' if cla is textb then ;',
  18843. ' if cla is textc then ;',
  18844. ' if clb is textc then ;',
  18845. ' try',
  18846. ' except',
  18847. ' on TExtA do ;',
  18848. ' on e: TExtB do ;',
  18849. ' end;',
  18850. '']);
  18851. ConvertProgram;
  18852. CheckSource('TestExternalClass_Is',
  18853. LinesToStr([ // statements
  18854. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18855. ' this.$init = function () {',
  18856. ' };',
  18857. ' this.$final = function () {',
  18858. ' };',
  18859. '});',
  18860. 'this.A = null;',
  18861. 'this.ClA = null;',
  18862. 'this.B = null;',
  18863. 'this.ClB = null;',
  18864. 'this.C = null;',
  18865. 'this.ClC = null;',
  18866. '']),
  18867. LinesToStr([ // $mod.$main
  18868. 'if (rtl.isExt($mod.A, ExtB)) ;',
  18869. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  18870. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  18871. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  18872. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  18873. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  18874. 'try {} catch ($e) {',
  18875. ' if (rtl.isExt($e,ExtA)) {}',
  18876. ' else if (rtl.isExt($e,ExtB)) {',
  18877. ' var e = $e;',
  18878. ' } else throw $e',
  18879. '};',
  18880. '']));
  18881. end;
  18882. procedure TTestModule.TestExternalClass_As;
  18883. begin
  18884. StartProgram(false);
  18885. Add('{$modeswitch externalclass}');
  18886. Add('type');
  18887. Add(' TExtA = class external name ''ExtA''');
  18888. Add(' end;');
  18889. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  18890. Add(' end;');
  18891. Add(' TExtC = class (TExtB)');
  18892. Add(' end;');
  18893. Add('var');
  18894. Add(' A: texta;');
  18895. Add(' B: textb;');
  18896. Add(' C: textc;');
  18897. Add('begin');
  18898. Add(' b:=a as textb;');
  18899. Add(' c:=a as textc;');
  18900. Add(' c:=b as textc;');
  18901. ConvertProgram;
  18902. CheckSource('TestExternalClass_Is',
  18903. LinesToStr([ // statements
  18904. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18905. ' this.$init = function () {',
  18906. ' };',
  18907. ' this.$final = function () {',
  18908. ' };',
  18909. '});',
  18910. 'this.A = null;',
  18911. 'this.B = null;',
  18912. 'this.C = null;',
  18913. '']),
  18914. LinesToStr([ // $mod.$main
  18915. '$mod.B = rtl.asExt($mod.A, ExtB);',
  18916. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  18917. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  18918. '']));
  18919. end;
  18920. procedure TTestModule.TestExternalClass_DestructorFail;
  18921. begin
  18922. StartProgram(false);
  18923. Add('{$modeswitch externalclass}');
  18924. Add('type');
  18925. Add(' TExtA = class external name ''ExtA''');
  18926. Add(' destructor Free;');
  18927. Add(' end;');
  18928. SetExpectedPasResolverError('Pascal element not supported: destructor',
  18929. nPasElementNotSupported);
  18930. ConvertProgram;
  18931. end;
  18932. procedure TTestModule.TestExternalClass_New;
  18933. begin
  18934. StartProgram(false);
  18935. Add([
  18936. '{$modeswitch externalclass}',
  18937. 'type',
  18938. ' TExtA = class external name ''ExtA''',
  18939. ' constructor New;',
  18940. ' constructor New(i: longint; j: longint = 2);',
  18941. ' end;',
  18942. 'var',
  18943. ' A: texta;',
  18944. 'begin',
  18945. ' a:=texta.new;',
  18946. ' a:=texta(texta.new);',
  18947. ' a:=texta.new();',
  18948. ' a:=texta.new(1);',
  18949. ' with texta do begin',
  18950. ' a:=new;',
  18951. ' a:=new();',
  18952. ' a:=new(2);',
  18953. ' end;',
  18954. ' a:=test1.texta.new;',
  18955. ' a:=test1.texta.new();',
  18956. ' a:=test1.texta.new(3);',
  18957. '']);
  18958. ConvertProgram;
  18959. CheckSource('TestExternalClass_New',
  18960. LinesToStr([ // statements
  18961. 'this.A = null;',
  18962. '']),
  18963. LinesToStr([ // $mod.$main
  18964. '$mod.A = new ExtA();',
  18965. '$mod.A = new ExtA();',
  18966. '$mod.A = new ExtA();',
  18967. '$mod.A = new ExtA(1,2);',
  18968. '$mod.A = new ExtA();',
  18969. '$mod.A = new ExtA();',
  18970. '$mod.A = new ExtA(2,2);',
  18971. '$mod.A = new ExtA();',
  18972. '$mod.A = new ExtA();',
  18973. '$mod.A = new ExtA(3,2);',
  18974. '']));
  18975. end;
  18976. procedure TTestModule.TestExternalClass_ClassOf_New;
  18977. begin
  18978. StartProgram(false);
  18979. Add('{$modeswitch externalclass}');
  18980. Add('type');
  18981. Add(' TExtAClass = class of TExtA;');
  18982. Add(' TExtA = class external name ''ExtA''');
  18983. Add(' C: TExtAClass;');
  18984. Add(' constructor New;');
  18985. Add(' end;');
  18986. Add('var');
  18987. Add(' A: texta;');
  18988. Add(' C: textaclass;');
  18989. Add('begin');
  18990. Add(' a:=c.new;');
  18991. Add(' a:=c.new();');
  18992. Add(' with C do begin');
  18993. Add(' a:=new;');
  18994. Add(' a:=new();');
  18995. Add(' end;');
  18996. Add(' a:=test1.c.new;');
  18997. Add(' a:=test1.c.new();');
  18998. Add(' a:=A.c.new();');
  18999. ConvertProgram;
  19000. CheckSource('TestExternalClass_ClassOf_New',
  19001. LinesToStr([ // statements
  19002. 'this.A = null;',
  19003. 'this.C = null;',
  19004. '']),
  19005. LinesToStr([ // $mod.$main
  19006. '$mod.A = new $mod.C();',
  19007. '$mod.A = new $mod.C();',
  19008. 'var $with = $mod.C;',
  19009. '$mod.A = new $with();',
  19010. '$mod.A = new $with();',
  19011. '$mod.A = new $mod.C();',
  19012. '$mod.A = new $mod.C();',
  19013. '$mod.A = new $mod.A.C();',
  19014. '']));
  19015. end;
  19016. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  19017. begin
  19018. StartProgram(false);
  19019. Add([
  19020. '{$modeswitch externalclass}',
  19021. 'type',
  19022. ' TExtAClass = class of TExtA;',
  19023. ' TExtA = class external name ''ExtA''',
  19024. ' constructor New;',
  19025. ' end;',
  19026. 'function GetCreator: TExtAClass;',
  19027. 'begin',
  19028. ' Result:=TExtA;',
  19029. 'end;',
  19030. 'var',
  19031. ' A: texta;',
  19032. 'begin',
  19033. ' a:=getcreator.new;',
  19034. ' a:=getcreator().new;',
  19035. ' a:=getcreator().new();',
  19036. ' a:=getcreator.new();',
  19037. ' with getcreator do begin',
  19038. ' a:=new;',
  19039. ' a:=new();',
  19040. ' end;']);
  19041. ConvertProgram;
  19042. CheckSource('TestExternalClass_FuncClassOf_New',
  19043. LinesToStr([ // statements
  19044. 'this.GetCreator = function () {',
  19045. ' var Result = null;',
  19046. ' Result = ExtA;',
  19047. ' return Result;',
  19048. '};',
  19049. 'this.A = null;',
  19050. '']),
  19051. LinesToStr([ // $mod.$main
  19052. '$mod.A = new ($mod.GetCreator())();',
  19053. '$mod.A = new ($mod.GetCreator())();',
  19054. '$mod.A = new ($mod.GetCreator())();',
  19055. '$mod.A = new ($mod.GetCreator())();',
  19056. 'var $with = $mod.GetCreator();',
  19057. '$mod.A = new $with();',
  19058. '$mod.A = new $with();',
  19059. '']));
  19060. end;
  19061. procedure TTestModule.TestExternalClass_New_PasClassFail;
  19062. begin
  19063. StartProgram(false);
  19064. Add([
  19065. '{$modeswitch externalclass}',
  19066. 'type',
  19067. ' TExtA = class external name ''ExtA''',
  19068. ' constructor New;',
  19069. ' end;',
  19070. ' TBird = class(TExtA)',
  19071. ' end;',
  19072. 'begin',
  19073. ' TBird.new;',
  19074. '']);
  19075. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  19076. ConvertProgram;
  19077. end;
  19078. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  19079. begin
  19080. StartProgram(false);
  19081. Add([
  19082. '{$modeswitch externalclass}',
  19083. 'type',
  19084. ' TExtA = class external name ''ExtA''',
  19085. ' constructor New;',
  19086. ' end;',
  19087. ' TBird = class(TExtA)',
  19088. ' end;',
  19089. 'begin',
  19090. ' TBird.new();',
  19091. '']);
  19092. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  19093. ConvertProgram;
  19094. end;
  19095. procedure TTestModule.TestExternalClass_NewExtName;
  19096. begin
  19097. StartProgram(false);
  19098. Add([
  19099. '{$modeswitch externalclass}',
  19100. 'type',
  19101. ' TExtA = class external name ''ExtA''',
  19102. ' constructor New; external name ''Other'';',
  19103. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  19104. ' end;',
  19105. 'var',
  19106. ' A: texta;',
  19107. 'begin',
  19108. ' a:=texta.new;',
  19109. ' a:=texta(texta.new);',
  19110. ' a:=texta.new();',
  19111. ' a:=texta.new(1);',
  19112. ' with texta do begin',
  19113. ' a:=new;',
  19114. ' a:=new();',
  19115. ' a:=new(2);',
  19116. ' end;',
  19117. ' a:=test1.texta.new;',
  19118. ' a:=test1.texta.new();',
  19119. ' a:=test1.texta.new(3);',
  19120. '']);
  19121. ConvertProgram;
  19122. CheckSource('TestExternalClass_NewExtName',
  19123. LinesToStr([ // statements
  19124. 'this.A = null;',
  19125. '']),
  19126. LinesToStr([ // $mod.$main
  19127. '$mod.A = new Other();',
  19128. '$mod.A = new Other();',
  19129. '$mod.A = new Other();',
  19130. '$mod.A = new A.B(1,2);',
  19131. '$mod.A = new Other();',
  19132. '$mod.A = new Other();',
  19133. '$mod.A = new A.B(2,2);',
  19134. '$mod.A = new Other();',
  19135. '$mod.A = new Other();',
  19136. '$mod.A = new A.B(3,2);',
  19137. '']));
  19138. end;
  19139. procedure TTestModule.TestExternalClass_Constructor;
  19140. begin
  19141. StartProgram(false);
  19142. Add([
  19143. '{$modeswitch externalclass}',
  19144. 'type',
  19145. ' TExtA = class external name ''ExtA''',
  19146. ' public type',
  19147. ' TExtB = class external name ''ExtB''',
  19148. ' public type',
  19149. ' TExtC = class external name ''ExtC''',
  19150. ' constructor New;',
  19151. ' constructor New(i: word);',
  19152. ' end;',
  19153. ' end;',
  19154. ' constructor Create;',
  19155. ' constructor Create(i: longint; j: longint = 2);',
  19156. ' end;',
  19157. 'var',
  19158. ' A: texta;',
  19159. ' C: texta.textb.textc;',
  19160. 'begin',
  19161. ' a:=texta.create;',
  19162. ' a:=texta(texta.create);',
  19163. ' a:=texta.create();',
  19164. ' a:=texta.create(1);',
  19165. ' with texta do begin',
  19166. ' a:=create;',
  19167. ' a:=create();',
  19168. ' a:=create(2);',
  19169. ' end;',
  19170. ' a:=test1.texta.create;',
  19171. ' a:=test1.texta.create();',
  19172. ' a:=test1.texta.create(3);',
  19173. ' c:=texta.textb.textc.new;',
  19174. ' c:=texta.textb.textc.new();',
  19175. ' c:=texta.textb.textc.new(4);',
  19176. '']);
  19177. ConvertProgram;
  19178. CheckSource('TestExternalClass_Constructor',
  19179. LinesToStr([ // statements
  19180. 'this.A = null;',
  19181. 'this.C = null;',
  19182. '']),
  19183. LinesToStr([ // $mod.$main
  19184. '$mod.A = new ExtA.Create();',
  19185. '$mod.A = new ExtA.Create();',
  19186. '$mod.A = new ExtA.Create();',
  19187. '$mod.A = new ExtA.Create(1,2);',
  19188. '$mod.A = new ExtA.Create();',
  19189. '$mod.A = new ExtA.Create();',
  19190. '$mod.A = new ExtA.Create(2,2);',
  19191. '$mod.A = new ExtA.Create();',
  19192. '$mod.A = new ExtA.Create();',
  19193. '$mod.A = new ExtA.Create(3,2);',
  19194. '$mod.C = new ExtA.ExtB.ExtC();',
  19195. '$mod.C = new ExtA.ExtB.ExtC();',
  19196. '$mod.C = new ExtA.ExtB.ExtC(4);',
  19197. '']));
  19198. end;
  19199. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  19200. begin
  19201. StartProgram(false);
  19202. Add([
  19203. '{$modeswitch externalclass}',
  19204. 'type',
  19205. ' TExtA = class external name ''ExtA''',
  19206. ' constructor Create; external name ''{}'';',
  19207. ' end;',
  19208. 'var',
  19209. ' A: texta;',
  19210. 'begin',
  19211. ' a:=texta.create;',
  19212. ' a:=texta(texta.create);',
  19213. ' a:=texta.create();',
  19214. ' with texta do begin',
  19215. ' a:=create;',
  19216. ' a:=create();',
  19217. ' end;',
  19218. ' a:=test1.texta.create;',
  19219. ' a:=test1.texta.create();',
  19220. '']);
  19221. ConvertProgram;
  19222. CheckSource('TestExternalClass_ConstructorBrackets',
  19223. LinesToStr([ // statements
  19224. 'this.A = null;',
  19225. '']),
  19226. LinesToStr([ // $mod.$main
  19227. '$mod.A = {};',
  19228. '$mod.A = {};',
  19229. '$mod.A = {};',
  19230. '$mod.A = {};',
  19231. '$mod.A = {};',
  19232. '$mod.A = {};',
  19233. '$mod.A = {};',
  19234. '']));
  19235. end;
  19236. procedure TTestModule.TestExternalClass_LocalConstSameName;
  19237. begin
  19238. StartProgram(false);
  19239. Add('{$modeswitch externalclass}');
  19240. Add('type');
  19241. Add(' TExtA = class external name ''ExtA''');
  19242. Add(' constructor New;');
  19243. Add(' end;');
  19244. Add('function DoIt: longint;');
  19245. Add('const ExtA: longint = 3;');
  19246. Add('begin');
  19247. Add(' Result:=ExtA;');
  19248. Add('end;');
  19249. Add('var');
  19250. Add(' A: texta;');
  19251. Add('begin');
  19252. Add(' a:=texta.new;');
  19253. ConvertProgram;
  19254. CheckSource('TestExternalClass_LocalConstSameName',
  19255. LinesToStr([ // statements
  19256. 'var ExtA$1 = 3;',
  19257. 'this.DoIt = function () {',
  19258. ' var Result = 0;',
  19259. ' Result = ExtA$1;',
  19260. ' return Result;',
  19261. '};',
  19262. 'this.A = null;',
  19263. '']),
  19264. LinesToStr([ // $mod.$main
  19265. '$mod.A = new ExtA();',
  19266. '']));
  19267. end;
  19268. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  19269. begin
  19270. StartProgram(false);
  19271. Add('{$modeswitch externalclass}');
  19272. Add('type');
  19273. Add(' TExtA = class external name ''ExtA''');
  19274. Add(' procedure DoIt;');
  19275. Add(' end;');
  19276. Add(' TMyA = class(TExtA)');
  19277. Add(' procedure DoIt;');
  19278. Add(' end;');
  19279. Add('procedure TMyA.DoIt; begin end;');
  19280. Add('begin');
  19281. ConvertProgram;
  19282. CheckSource('TestExternalClass_ReintroduceOverload',
  19283. LinesToStr([ // statements
  19284. 'rtl.createClassExt(this, "TMyA", ExtA, "", function () {',
  19285. ' this.$init = function () {',
  19286. ' };',
  19287. ' this.$final = function () {',
  19288. ' };',
  19289. ' this.DoIt$1 = function () {',
  19290. ' };',
  19291. '});',
  19292. '']),
  19293. LinesToStr([ // $mod.$main
  19294. '']));
  19295. end;
  19296. procedure TTestModule.TestExternalClass_Inherited;
  19297. begin
  19298. StartProgram(false);
  19299. Add('{$modeswitch externalclass}');
  19300. Add('type');
  19301. Add(' TExtA = class external name ''ExtA''');
  19302. Add(' procedure DoIt(i: longint = 1); virtual;');
  19303. Add(' procedure DoSome(j: longint = 2);');
  19304. Add(' end;');
  19305. Add(' TExtB = class external name ''ExtB''(TExtA)');
  19306. Add(' end;');
  19307. Add(' TMyC = class(TExtB)');
  19308. Add(' procedure DoIt(i: longint = 1); override;');
  19309. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  19310. Add(' end;');
  19311. Add('procedure TMyC.DoIt(i: longint);');
  19312. Add('begin');
  19313. Add(' inherited;');
  19314. Add(' inherited DoIt;');
  19315. Add(' inherited DoIt();');
  19316. Add(' inherited DoIt(3);');
  19317. Add(' inherited DoSome;');
  19318. Add(' inherited DoSome();');
  19319. Add(' inherited DoSome(4);');
  19320. Add('end;');
  19321. Add('procedure TMyC.DoSome(j: longint);');
  19322. Add('begin');
  19323. Add(' inherited;');
  19324. Add('end;');
  19325. Add('begin');
  19326. ConvertProgram;
  19327. CheckSource('TestExternalClass_ReintroduceOverload',
  19328. LinesToStr([ // statements
  19329. 'rtl.createClassExt(this, "TMyC", ExtB, "", function () {',
  19330. ' this.$init = function () {',
  19331. ' };',
  19332. ' this.$final = function () {',
  19333. ' };',
  19334. ' this.DoIt = function (i) {',
  19335. ' ExtB.DoIt.apply(this, arguments);',
  19336. ' ExtB.DoIt.call(this, 1);',
  19337. ' ExtB.DoIt.call(this, 1);',
  19338. ' ExtB.DoIt.call(this, 3);',
  19339. ' ExtB.DoSome.call(this, 2);',
  19340. ' ExtB.DoSome.call(this, 2);',
  19341. ' ExtB.DoSome.call(this, 4);',
  19342. ' };',
  19343. ' this.DoSome$1 = function (j) {',
  19344. ' ExtB.DoSome.apply(this, arguments);',
  19345. ' };',
  19346. '});',
  19347. '']),
  19348. LinesToStr([ // $mod.$main
  19349. '']));
  19350. end;
  19351. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  19352. begin
  19353. StartProgram(false);
  19354. Add('{$modeswitch externalclass}');
  19355. Add('type');
  19356. Add(' TObject = class');
  19357. Add(' end;');
  19358. Add(' TExtA = class external name ''ExtA''(TObject)');
  19359. Add(' end;');
  19360. Add('begin');
  19361. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  19362. ConvertProgram;
  19363. end;
  19364. procedure TTestModule.TestExternalClass_NewInstance;
  19365. begin
  19366. StartProgram(false);
  19367. Add('{$modeswitch externalclass}');
  19368. Add('type');
  19369. Add(' TExtA = class external name ''ExtA''');
  19370. Add(' end;');
  19371. Add(' TMyB = class(TExtA)');
  19372. Add(' protected');
  19373. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  19374. Add(' end;');
  19375. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  19376. Add('begin end;');
  19377. Add('begin');
  19378. ConvertProgram;
  19379. CheckSource('TestExternalClass_NewInstance',
  19380. LinesToStr([ // statements
  19381. 'rtl.createClassExt(this, "TMyB", ExtA, "NewInstance", function () {',
  19382. ' this.$init = function () {',
  19383. ' };',
  19384. ' this.$final = function () {',
  19385. ' };',
  19386. ' this.NewInstance = function (fnname, paramarray) {',
  19387. ' var Result = null;',
  19388. ' return Result;',
  19389. ' };',
  19390. '});',
  19391. '']),
  19392. LinesToStr([ // $mod.$main
  19393. '']));
  19394. end;
  19395. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  19396. begin
  19397. StartProgram(false);
  19398. Add('{$modeswitch externalclass}');
  19399. Add('type');
  19400. Add(' TExtA = class external name ''ExtA''');
  19401. Add(' end;');
  19402. Add(' TMyB = class(TExtA)');
  19403. Add(' protected');
  19404. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  19405. Add(' end;');
  19406. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  19407. Add('begin end;');
  19408. Add('begin');
  19409. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  19410. ConvertProgram;
  19411. end;
  19412. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  19413. begin
  19414. StartProgram(false);
  19415. Add('{$modeswitch externalclass}');
  19416. Add('type');
  19417. Add(' TExtA = class external name ''ExtA''');
  19418. Add(' end;');
  19419. Add(' TMyB = class(TExtA)');
  19420. Add(' protected');
  19421. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  19422. Add(' end;');
  19423. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  19424. Add('begin end;');
  19425. Add('begin');
  19426. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "Longint", expected "String"',
  19427. nIncompatibleTypeArgNo);
  19428. ConvertProgram;
  19429. end;
  19430. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  19431. begin
  19432. StartProgram(false);
  19433. Add('{$modeswitch externalclass}');
  19434. Add('type');
  19435. Add(' TExtA = class external name ''ExtA''');
  19436. Add(' end;');
  19437. Add(' TMyB = class(TExtA)');
  19438. Add(' protected');
  19439. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  19440. Add(' end;');
  19441. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  19442. Add('begin end;');
  19443. Add('begin');
  19444. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "type", expected "untyped"',
  19445. nIncompatibleTypeArgNo);
  19446. ConvertProgram;
  19447. end;
  19448. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  19449. begin
  19450. StartProgram(false);
  19451. Add([
  19452. '{$modeswitch externalclass}',
  19453. 'type',
  19454. ' TJSFunction = class external name ''Function''',
  19455. ' end;',
  19456. ' TExtA = class external name ''ExtA''(TJSFunction)',
  19457. ' constructor New(w: word);',
  19458. ' end;',
  19459. ' TBird = class (TExtA)',
  19460. ' public',
  19461. ' Size: word;',
  19462. ' class var Legs: word;',
  19463. ' constructor Create(a: word);',
  19464. ' end;',
  19465. ' TEagle = class (TBird)',
  19466. ' public',
  19467. ' constructor Create(b: word); reintroduce;',
  19468. ' end;',
  19469. 'constructor TBird.Create(a: word);',
  19470. 'begin',
  19471. ' inherited;', // silently ignored
  19472. ' inherited New(a);', // this.$func(a)
  19473. 'end;',
  19474. 'constructor TEagle.Create(b: word);',
  19475. 'begin',
  19476. ' inherited Create(b);',
  19477. 'end;',
  19478. 'var',
  19479. ' Bird: TBird;',
  19480. ' Eagle: TEagle;',
  19481. 'begin',
  19482. ' Bird:=TBird.Create(3);',
  19483. ' Eagle:=TEagle.Create(4);',
  19484. ' Bird.Size:=Bird.Size+5;',
  19485. ' Bird.Legs:=Bird.Legs+6;',
  19486. ' Eagle.Size:=Eagle.Size+5;',
  19487. ' Eagle.Legs:=Eagle.Legs+6;',
  19488. '']);
  19489. ConvertProgram;
  19490. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  19491. LinesToStr([ // statements
  19492. 'rtl.createClassExt(this, "TBird", ExtA, "", function () {',
  19493. ' this.Legs = 0;',
  19494. ' this.$init = function () {',
  19495. ' this.Size = 0;',
  19496. ' };',
  19497. ' this.$final = function () {',
  19498. ' };',
  19499. ' this.Create = function (a) {',
  19500. ' this.$ancestorfunc(a);',
  19501. ' return this;',
  19502. ' };',
  19503. '});',
  19504. 'rtl.createClassExt(this, "TEagle", this.TBird, "", function () {',
  19505. ' this.Create$1 = function (b) {',
  19506. ' $mod.TBird.Create.call(this, b);',
  19507. ' return this;',
  19508. ' };',
  19509. '});',
  19510. 'this.Bird = null;',
  19511. 'this.Eagle = null;',
  19512. '']),
  19513. LinesToStr([ // $mod.$main
  19514. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  19515. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  19516. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  19517. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  19518. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  19519. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  19520. '']));
  19521. end;
  19522. procedure TTestModule.TestExternalClass_PascalProperty;
  19523. begin
  19524. StartProgram(false);
  19525. Add('{$modeswitch externalclass}');
  19526. Add('type');
  19527. Add(' TJSElement = class;');
  19528. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  19529. Add(' TJSElement = class external name ''ExtA''');
  19530. Add(' end;');
  19531. Add(' TControl = class(TJSElement)');
  19532. Add(' private');
  19533. Add(' FOnClick: TJSNotifyEvent;');
  19534. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  19535. Add(' procedure Click(Sender: TJSElement);');
  19536. Add(' end;');
  19537. Add('procedure TControl.Click(Sender: TJSElement);');
  19538. Add('begin');
  19539. Add(' OnClick(Self);');
  19540. Add('end;');
  19541. Add('var');
  19542. Add(' Ctrl: TControl;');
  19543. Add('begin');
  19544. Add(' Ctrl.OnClick:[email protected];');
  19545. Add(' Ctrl.OnClick(Ctrl);');
  19546. ConvertProgram;
  19547. CheckSource('TestExternalClass_PascalProperty',
  19548. LinesToStr([ // statements
  19549. 'rtl.createClassExt(this, "TControl", ExtA, "", function () {',
  19550. ' this.$init = function () {',
  19551. ' this.FOnClick = null;',
  19552. ' };',
  19553. ' this.$final = function () {',
  19554. ' this.FOnClick = undefined;',
  19555. ' };',
  19556. ' this.Click = function (Sender) {',
  19557. ' this.FOnClick(this);',
  19558. ' };',
  19559. '});',
  19560. 'this.Ctrl = null;',
  19561. '']),
  19562. LinesToStr([ // $mod.$main
  19563. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  19564. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  19565. '']));
  19566. end;
  19567. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  19568. begin
  19569. StartProgram(false);
  19570. Add([
  19571. '{$modeswitch externalclass}',
  19572. 'type',
  19573. ' IUnknown = interface end;',
  19574. ' TObject = class',
  19575. ' end;',
  19576. ' TChild = class',
  19577. ' end;',
  19578. ' TExtRootA = class external name ''ExtRootA''',
  19579. ' end;',
  19580. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  19581. ' end;',
  19582. ' TExtRootB = class external name ''ExtRootB''',
  19583. ' end;',
  19584. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  19585. ' end;',
  19586. ' TExtString = class external name ''String''',
  19587. ' function charAt(aIndex : NativeInt) : string;',
  19588. ' end;',
  19589. 'var',
  19590. ' Obj: TObject;',
  19591. ' Child: TChild;',
  19592. ' RootA: TExtRootA;',
  19593. ' ChildA: TExtChildA;',
  19594. ' RootB: TExtRootB;',
  19595. ' ChildB: TExtChildB;',
  19596. ' i: IUnknown;',
  19597. ' s: string;',
  19598. ' v: jsvalue;',
  19599. 'begin',
  19600. ' obj:=tobject(roota);',
  19601. ' obj:=tobject(childa);',
  19602. ' child:=tchild(tobject(roota));',
  19603. ' roota:=textroota(obj);',
  19604. ' roota:=textroota(child);',
  19605. ' roota:=textroota(rootb);',
  19606. ' roota:=textroota(childb);',
  19607. ' childa:=textchilda(textroota(obj));',
  19608. ' roota:=TExtRootA(i);',
  19609. ' s:=TExtString(s).charAt(7);',
  19610. ' s:=TExtString(v).charAt(8);',
  19611. '']);
  19612. ConvertProgram;
  19613. CheckSource('TestExternalClass_TypeCastToRootClass',
  19614. LinesToStr([ // statements
  19615. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19616. 'rtl.createClass(this, "TObject", null, function () {',
  19617. ' this.$init = function () {',
  19618. ' };',
  19619. ' this.$final = function () {',
  19620. ' };',
  19621. '});',
  19622. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  19623. '});',
  19624. 'this.Obj = null;',
  19625. 'this.Child = null;',
  19626. 'this.RootA = null;',
  19627. 'this.ChildA = null;',
  19628. 'this.RootB = null;',
  19629. 'this.ChildB = null;',
  19630. 'this.i = null;',
  19631. 'this.s = "";',
  19632. 'this.v = undefined;',
  19633. '']),
  19634. LinesToStr([ // $mod.$main
  19635. '$mod.Obj = $mod.RootA;',
  19636. '$mod.Obj = $mod.ChildA;',
  19637. '$mod.Child = $mod.RootA;',
  19638. '$mod.RootA = $mod.Obj;',
  19639. '$mod.RootA = $mod.Child;',
  19640. '$mod.RootA = $mod.RootB;',
  19641. '$mod.RootA = $mod.ChildB;',
  19642. '$mod.ChildA = $mod.Obj;',
  19643. '$mod.RootA = $mod.i;',
  19644. '$mod.s = $mod.s.charAt(7);',
  19645. '$mod.s = $mod.v.charAt(8);',
  19646. '']));
  19647. end;
  19648. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  19649. begin
  19650. StartProgram(false);
  19651. Add([
  19652. '{$modeswitch externalclass}',
  19653. 'type',
  19654. ' IUnknown = interface end;',
  19655. ' IBird = interface(IUnknown) end;',
  19656. ' TClass = class of TObject;',
  19657. ' TObject = class',
  19658. ' end;',
  19659. ' TChild = class',
  19660. ' end;',
  19661. ' TJSObject = class external name ''Object''',
  19662. ' end;',
  19663. ' TRec = record end;',
  19664. 'var',
  19665. ' Obj: TObject;',
  19666. ' Child: TChild;',
  19667. ' i: IUnknown;',
  19668. ' Bird: IBird;',
  19669. ' j: TJSObject;',
  19670. ' r: TRec;',
  19671. ' c: TClass;',
  19672. 'begin',
  19673. ' j:=tjsobject(IUnknown);',
  19674. ' j:=tjsobject(IBird);',
  19675. ' j:=tjsobject(TObject);',
  19676. ' j:=tjsobject(TChild);',
  19677. ' j:=tjsobject(TRec);',
  19678. ' j:=tjsobject(Obj);',
  19679. ' j:=tjsobject(Child);',
  19680. ' j:=tjsobject(i);',
  19681. ' j:=tjsobject(Bird);',
  19682. ' j:=tjsobject(r);',
  19683. ' j:=tjsobject(c);',
  19684. '']);
  19685. ConvertProgram;
  19686. CheckSource('TestExternalClass_TypeCastToJSObject',
  19687. LinesToStr([ // statements
  19688. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19689. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  19690. 'rtl.createClass(this, "TObject", null, function () {',
  19691. ' this.$init = function () {',
  19692. ' };',
  19693. ' this.$final = function () {',
  19694. ' };',
  19695. '});',
  19696. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  19697. '});',
  19698. 'rtl.recNewT(this, "TRec", function () {',
  19699. ' this.$eq = function (b) {',
  19700. ' return true;',
  19701. ' };',
  19702. ' this.$assign = function (s) {',
  19703. ' return this;',
  19704. ' };',
  19705. '});',
  19706. 'this.Obj = null;',
  19707. 'this.Child = null;',
  19708. 'this.i = null;',
  19709. 'this.Bird = null;',
  19710. 'this.j = null;',
  19711. 'this.r = this.TRec.$new();',
  19712. 'this.c = null;',
  19713. '']),
  19714. LinesToStr([ // $mod.$main
  19715. '$mod.j = $mod.IUnknown;',
  19716. '$mod.j = $mod.IBird;',
  19717. '$mod.j = $mod.TObject;',
  19718. '$mod.j = $mod.TChild;',
  19719. '$mod.j = $mod.TRec;',
  19720. '$mod.j = $mod.Obj;',
  19721. '$mod.j = $mod.Child;',
  19722. '$mod.j = $mod.i;',
  19723. '$mod.j = $mod.Bird;',
  19724. '$mod.j = $mod.r;',
  19725. '$mod.j = $mod.c;',
  19726. '']));
  19727. end;
  19728. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  19729. begin
  19730. StartProgram(false);
  19731. Add('{$modeswitch externalclass}');
  19732. Add('type');
  19733. Add(' TJSString = class external name ''String''');
  19734. Add(' class function fromCharCode() : string; varargs;');
  19735. Add(' function anchor(const aName : string) : string;');
  19736. Add(' end;');
  19737. Add('var');
  19738. Add(' s: string;');
  19739. Add('begin');
  19740. Add(' s:=TJSString.fromCharCode(65,66);');
  19741. Add(' s:=TJSString(s).anchor(s);');
  19742. Add(' s:=TJSString(''foo'').anchor(s);');
  19743. ConvertProgram;
  19744. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  19745. LinesToStr([ // statements
  19746. 'this.s = "";',
  19747. '']),
  19748. LinesToStr([ // $mod.$main
  19749. '$mod.s = String.fromCharCode(65, 66);',
  19750. '$mod.s = $mod.s.anchor($mod.s);',
  19751. '$mod.s = "foo".anchor($mod.s);',
  19752. '']));
  19753. end;
  19754. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  19755. begin
  19756. StartProgram(false);
  19757. Add([
  19758. '{$modeswitch externalclass}',
  19759. 'type',
  19760. ' TJSObject = class external name ''Object'' end;',
  19761. ' TJSFunction = class external name ''Function''',
  19762. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  19763. ' function call(thisArg: TJSObject): JSValue; varargs;',
  19764. ' end;',
  19765. ' TObject = class',
  19766. ' procedure DoIt(i: longint);',
  19767. ' end;',
  19768. ' TFuncInt = function(o: TObject): longint;',
  19769. 'function GetIt(o: TObject): longint;',
  19770. ' procedure Sub; begin end;',
  19771. 'var',
  19772. ' f: TJSFunction;',
  19773. ' fi: TFuncInt;',
  19774. 'begin',
  19775. ' fi:=TFuncInt(f);',
  19776. ' f:=TJSFunction(fi);',
  19777. ' f:=TJSFunction(@GetIt);',
  19778. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  19779. ' f:=TJSFunction(@Sub);',
  19780. ' f:=TJSFunction(@o.doit);',
  19781. ' f:=TJSFunction(fi).bind(nil,4)',
  19782. 'end;',
  19783. 'procedure TObject.DoIt(i: longint);',
  19784. ' procedure Sub; begin end;',
  19785. 'var f: TJSFunction;',
  19786. 'begin',
  19787. ' f:=TJSFunction(@DoIt);',
  19788. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  19789. ' f:=TJSFunction(@Sub);',
  19790. ' f:=TJSFunction(@GetIt);',
  19791. 'end;',
  19792. 'begin']);
  19793. ConvertProgram;
  19794. CheckSource('TestExternalClass_TypeCastToJSFunction',
  19795. LinesToStr([ // statements
  19796. 'rtl.createClass(this, "TObject", null, function () {',
  19797. ' this.$init = function () {',
  19798. ' };',
  19799. ' this.$final = function () {',
  19800. ' };',
  19801. ' this.DoIt = function (i) {',
  19802. ' var $Self = this;',
  19803. ' function Sub() {',
  19804. ' };',
  19805. ' var f = null;',
  19806. ' f = this.DoIt;',
  19807. ' f = this.DoIt.bind(null, 13);',
  19808. ' f = Sub;',
  19809. ' f = $mod.GetIt;',
  19810. ' };',
  19811. '});',
  19812. 'this.GetIt = function (o) {',
  19813. ' var Result = 0;',
  19814. ' function Sub() {',
  19815. ' };',
  19816. ' var f = null;',
  19817. ' var fi = null;',
  19818. ' fi = f;',
  19819. ' f = fi;',
  19820. ' f = $mod.GetIt;',
  19821. ' f = $mod.GetIt.bind(null, 3);',
  19822. ' f = Sub;',
  19823. ' f = $mod.TObject.DoIt;',
  19824. ' f = fi.bind(null, 4);',
  19825. ' return Result;',
  19826. '};',
  19827. '']),
  19828. LinesToStr([ // $mod.$main
  19829. '']));
  19830. end;
  19831. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  19832. begin
  19833. StartProgram(false);
  19834. Add([
  19835. '{$mode delphi}',
  19836. '{$modeswitch externalclass}',
  19837. 'type',
  19838. ' TJSObject = class external name ''Object'' end;',
  19839. ' TJSWindow = class external name ''Window''(TJSObject)',
  19840. ' procedure Open;',
  19841. ' end;',
  19842. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  19843. ' procedure Execute;',
  19844. ' end;',
  19845. 'procedure Fly;',
  19846. 'var',
  19847. ' w: TJSWindow;',
  19848. ' e: TJSEventTarget;',
  19849. 'begin',
  19850. ' w:=TJSWindow(e);',
  19851. ' e:=TJSEventTarget(w);',
  19852. 'end;',
  19853. 'begin']);
  19854. ConvertProgram;
  19855. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  19856. LinesToStr([ // statements
  19857. 'this.Fly = function () {',
  19858. ' var w = null;',
  19859. ' var e = null;',
  19860. ' w = e;',
  19861. ' e = w;',
  19862. '};',
  19863. '']),
  19864. LinesToStr([ // $mod.$main
  19865. '']));
  19866. end;
  19867. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  19868. begin
  19869. StartProgram(false);
  19870. Add('{$modeswitch externalclass}');
  19871. Add('type');
  19872. Add(' TJSString = class external name ''String''');
  19873. Add(' class function fromCharCode() : string; varargs;');
  19874. Add(' end;');
  19875. Add('var');
  19876. Add(' s: string;');
  19877. Add(' sObj: TJSString;');
  19878. Add('begin');
  19879. Add(' s:=sObj.fromCharCode(65,66);');
  19880. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  19881. nExternalClassInstanceCannotAccessStaticX);
  19882. ConvertProgram;
  19883. end;
  19884. procedure TTestModule.TestExternalClass_BracketAccessor;
  19885. begin
  19886. StartProgram(false);
  19887. Add([
  19888. '{$modeswitch externalclass}',
  19889. 'type',
  19890. ' TJSArray = class external name ''Array2''',
  19891. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  19892. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  19893. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  19894. ' end;',
  19895. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  19896. 'begin end;',
  19897. 'var',
  19898. ' Arr: tjsarray;',
  19899. ' s: string;',
  19900. ' i: longint;',
  19901. ' v: jsvalue;',
  19902. 'begin',
  19903. ' v:=arr[0];',
  19904. ' v:=arr.items[1];',
  19905. ' arr[2]:=s;',
  19906. ' arr.items[3]:=s;',
  19907. ' arr[4]:=i;',
  19908. ' arr[5]:=arr[6];',
  19909. ' arr.items[7]:=arr.items[8];',
  19910. ' with arr do items[9]:=items[10];',
  19911. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  19912. ' with arr do begin',
  19913. ' v:=GetItems(14);',
  19914. ' setitems(15,16);',
  19915. ' end;',
  19916. ' v:=test1.arr.items[17];',
  19917. ' test1.arr.items[18]:=v;',
  19918. '']);
  19919. ConvertProgram;
  19920. CheckSource('TestExternalClass_BracketAccessor',
  19921. LinesToStr([ // statements
  19922. 'this.DoIt = function (vI, vJ, vK, vL) {',
  19923. '};',
  19924. 'this.Arr = null;',
  19925. 'this.s = "";',
  19926. 'this.i = 0;',
  19927. 'this.v = undefined;',
  19928. '']),
  19929. LinesToStr([ // $mod.$main
  19930. '$mod.v = $mod.Arr[0];',
  19931. '$mod.v = $mod.Arr[1];',
  19932. '$mod.Arr[2] = $mod.s;',
  19933. '$mod.Arr[3] = $mod.s;',
  19934. '$mod.Arr[4] = $mod.i;',
  19935. '$mod.Arr[5] = $mod.Arr[6];',
  19936. '$mod.Arr[7] = $mod.Arr[8];',
  19937. 'var $with = $mod.Arr;',
  19938. '$with[9] = $with[10];',
  19939. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  19940. ' a: 9,',
  19941. ' p: $mod.Arr,',
  19942. ' get: function () {',
  19943. ' return this.p[this.a];',
  19944. ' },',
  19945. ' set: function (v) {',
  19946. ' this.p[this.a] = v;',
  19947. ' }',
  19948. '}, {',
  19949. ' a: 10,',
  19950. ' p: $mod.Arr,',
  19951. ' get: function () {',
  19952. ' return this.p[this.a];',
  19953. ' },',
  19954. ' set: function (v) {',
  19955. ' this.p[this.a] = v;',
  19956. ' }',
  19957. '});',
  19958. 'var $with1 = $mod.Arr;',
  19959. '$mod.v = $with1[14];',
  19960. '$with1[15] = 16;',
  19961. '$mod.v = $mod.Arr[17];',
  19962. '$mod.Arr[18] = $mod.v;',
  19963. '']));
  19964. end;
  19965. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  19966. begin
  19967. StartProgram(false);
  19968. Add([
  19969. '{$modeswitch externalclass}',
  19970. 'type',
  19971. ' TJSArray = class external name ''Array2''',
  19972. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  19973. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  19974. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  19975. ' end;',
  19976. ' TMyArr = class(TJSArray)',
  19977. ' procedure DoIt;',
  19978. ' end;',
  19979. 'procedure tmyarr.DoIt;',
  19980. 'begin',
  19981. ' Items[1]:=Items[2];',
  19982. ' SetItems(3,getItems(4));',
  19983. 'end;',
  19984. 'var',
  19985. ' Arr: tmyarr;',
  19986. ' s: string;',
  19987. ' i: longint;',
  19988. ' v: jsvalue;',
  19989. 'begin',
  19990. ' v:=arr[0];',
  19991. ' v:=arr.items[1];',
  19992. ' arr[2]:=s;',
  19993. ' arr.items[3]:=s;',
  19994. ' arr[4]:=i;',
  19995. ' arr[5]:=arr[6];',
  19996. ' arr.items[7]:=arr.items[8];',
  19997. ' with arr do items[9]:=items[10];',
  19998. ' with arr do begin',
  19999. ' v:=GetItems(14);',
  20000. ' setitems(15,16);',
  20001. ' end;',
  20002. '']);
  20003. ConvertProgram;
  20004. CheckSource('TestExternalClass_BracketAccessor_Call',
  20005. LinesToStr([ // statements
  20006. 'rtl.createClassExt(this, "TMyArr", Array2, "", function () {',
  20007. ' this.$init = function () {',
  20008. ' };',
  20009. ' this.$final = function () {',
  20010. ' };',
  20011. ' this.DoIt = function () {',
  20012. ' this[1] = this[2];',
  20013. ' this[3] = this[4];',
  20014. ' };',
  20015. '});',
  20016. 'this.Arr = null;',
  20017. 'this.s = "";',
  20018. 'this.i = 0;',
  20019. 'this.v = undefined;',
  20020. '']),
  20021. LinesToStr([ // $mod.$main
  20022. '$mod.v = $mod.Arr[0];',
  20023. '$mod.v = $mod.Arr[1];',
  20024. '$mod.Arr[2] = $mod.s;',
  20025. '$mod.Arr[3] = $mod.s;',
  20026. '$mod.Arr[4] = $mod.i;',
  20027. '$mod.Arr[5] = $mod.Arr[6];',
  20028. '$mod.Arr[7] = $mod.Arr[8];',
  20029. 'var $with = $mod.Arr;',
  20030. '$with[9] = $with[10];',
  20031. 'var $with1 = $mod.Arr;',
  20032. '$mod.v = $with1[14];',
  20033. '$with1[15] = 16;',
  20034. '']));
  20035. end;
  20036. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  20037. begin
  20038. StartProgram(false);
  20039. Add('{$modeswitch externalclass}');
  20040. Add('type');
  20041. Add(' TJSArray = class external name ''Array2''');
  20042. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  20043. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  20044. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  20045. Add(' end;');
  20046. Add('begin');
  20047. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  20048. nBracketAccessorOfExternalClassMustHaveOneParameter);
  20049. ConvertProgram;
  20050. end;
  20051. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  20052. begin
  20053. StartProgram(false);
  20054. Add('{$modeswitch externalclass}');
  20055. Add('type');
  20056. Add(' TJSArray = class external name ''Array2''');
  20057. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  20058. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  20059. Add(' end;');
  20060. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  20061. Add('begin end;');
  20062. Add('var');
  20063. Add(' Arr: tjsarray;');
  20064. Add(' v: jsvalue;');
  20065. Add('begin');
  20066. Add(' v:=arr[0];');
  20067. Add(' v:=arr.items[1];');
  20068. Add(' with arr do v:=items[2];');
  20069. Add(' doit(arr[3],arr[4]);');
  20070. ConvertProgram;
  20071. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  20072. LinesToStr([ // statements
  20073. 'this.DoIt = function (vI, vJ) {',
  20074. '};',
  20075. 'this.Arr = null;',
  20076. 'this.v = undefined;',
  20077. '']),
  20078. LinesToStr([ // $mod.$main
  20079. '$mod.v = $mod.Arr[0];',
  20080. '$mod.v = $mod.Arr[1];',
  20081. 'var $with = $mod.Arr;',
  20082. '$mod.v = $with[2];',
  20083. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  20084. '']));
  20085. end;
  20086. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  20087. begin
  20088. StartProgram(false);
  20089. Add('{$modeswitch externalclass}');
  20090. Add('type');
  20091. Add(' TJSArray = class external name ''Array2''');
  20092. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20093. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  20094. Add(' end;');
  20095. Add('var');
  20096. Add(' Arr: tjsarray;');
  20097. Add(' s: string;');
  20098. Add(' i: longint;');
  20099. Add(' v: jsvalue;');
  20100. Add('begin');
  20101. Add(' arr[2]:=s;');
  20102. Add(' arr.items[3]:=s;');
  20103. Add(' arr[4]:=i;');
  20104. Add(' with arr do items[5]:=i;');
  20105. ConvertProgram;
  20106. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  20107. LinesToStr([ // statements
  20108. 'this.Arr = null;',
  20109. 'this.s = "";',
  20110. 'this.i = 0;',
  20111. 'this.v = undefined;',
  20112. '']),
  20113. LinesToStr([ // $mod.$main
  20114. '$mod.Arr[2] = $mod.s;',
  20115. '$mod.Arr[3] = $mod.s;',
  20116. '$mod.Arr[4] = $mod.i;',
  20117. 'var $with = $mod.Arr;',
  20118. '$with[5] = $mod.i;',
  20119. '']));
  20120. end;
  20121. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  20122. begin
  20123. StartProgram(false);
  20124. Add('{$modeswitch externalclass}');
  20125. Add('type');
  20126. Add(' TJSArray = class external name ''Array2''');
  20127. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20128. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  20129. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  20130. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  20131. Add(' end;');
  20132. Add('var');
  20133. Add(' Arr: tjsarray;');
  20134. Add(' s: string;');
  20135. Add(' i: longint;');
  20136. Add(' v: jsvalue;');
  20137. Add('begin');
  20138. Add(' arr[2]:=s;');
  20139. Add(' arr.items[3]:=s;');
  20140. Add(' arr.numbers[4]:=i;');
  20141. Add(' with arr do items[5]:=i;');
  20142. Add(' with arr do numbers[6]:=i;');
  20143. ConvertProgram;
  20144. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  20145. LinesToStr([ // statements
  20146. 'this.Arr = null;',
  20147. 'this.s = "";',
  20148. 'this.i = 0;',
  20149. 'this.v = undefined;',
  20150. '']),
  20151. LinesToStr([ // $mod.$main
  20152. '$mod.Arr[2] = $mod.s;',
  20153. '$mod.Arr[3] = $mod.s;',
  20154. '$mod.Arr[4] = $mod.i;',
  20155. 'var $with = $mod.Arr;',
  20156. '$with[5] = $mod.i;',
  20157. 'var $with1 = $mod.Arr;',
  20158. '$with1[6] = $mod.i;',
  20159. '']));
  20160. end;
  20161. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  20162. begin
  20163. StartProgram(false);
  20164. Add('{$modeswitch externalclass}');
  20165. Add('type');
  20166. Add(' TJSArray = class external name ''Array2''');
  20167. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  20168. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20169. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  20170. Add(' end;');
  20171. Add('var');
  20172. Add(' Arr: tjsarray;');
  20173. Add(' i: longint;');
  20174. Add(' IntArr: array of longint;');
  20175. Add(' v: jsvalue;');
  20176. Add('begin');
  20177. Add(' v:=arr.items[i];');
  20178. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  20179. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  20180. ConvertProgram;
  20181. CheckSource('TestExternalClass_BracketAccessor_Index',
  20182. LinesToStr([ // statements
  20183. 'this.Arr = null;',
  20184. 'this.i = 0;',
  20185. 'this.IntArr = [];',
  20186. 'this.v = undefined;',
  20187. '']),
  20188. LinesToStr([ // $mod.$main
  20189. '$mod.v = $mod.Arr[$mod.i];',
  20190. '$mod.Arr[rtl.trunc($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  20191. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  20192. '']));
  20193. end;
  20194. procedure TTestModule.TestExternalClass_ForInJSObject;
  20195. begin
  20196. StartProgram(false);
  20197. Add([
  20198. '{$modeswitch externalclass}',
  20199. 'type',
  20200. ' TJSObject = class external name ''Object''',
  20201. ' end;',
  20202. 'var',
  20203. ' o: TJSObject;',
  20204. ' key: string;',
  20205. 'begin',
  20206. ' for key in o do',
  20207. ' if key=''abc'' then ;',
  20208. '']);
  20209. ConvertProgram;
  20210. CheckSource('TestExternalClass_ForInJSObject',
  20211. LinesToStr([ // statements
  20212. 'this.o = null;',
  20213. 'this.key = "";',
  20214. '']),
  20215. LinesToStr([ // $mod.$main
  20216. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  20217. '']));
  20218. end;
  20219. procedure TTestModule.TestExternalClass_ForInJSArray;
  20220. begin
  20221. StartProgram(false);
  20222. Add([
  20223. '{$modeswitch externalclass}',
  20224. 'type',
  20225. ' TJSInt8Array = class external name ''Int8Array''',
  20226. ' private',
  20227. ' flength: NativeInt external name ''length'';',
  20228. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  20229. ' public',
  20230. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  20231. ' property Length: NativeInt read flength;',
  20232. ' end;',
  20233. 'var',
  20234. ' a: TJSInt8Array;',
  20235. ' value: shortint;',
  20236. 'begin',
  20237. ' for value in a do',
  20238. ' if value=3 then ;',
  20239. '']);
  20240. ConvertProgram;
  20241. CheckSource('TestExternalClass_ForInJSArray',
  20242. LinesToStr([ // statements
  20243. 'this.a = null;',
  20244. 'this.value = 0;',
  20245. '']),
  20246. LinesToStr([ // $mod.$main
  20247. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  20248. ' $mod.value = $in[$l];',
  20249. ' if ($mod.value === 3) ;',
  20250. '};',
  20251. '']));
  20252. end;
  20253. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  20254. begin
  20255. AddModuleWithIntfImplSrc('unit2.pas',
  20256. LinesToStr([
  20257. '{$modeswitch externalclass}',
  20258. 'type',
  20259. ' TJSBufferSource = class external name ''BufferSource''',
  20260. ' end;',
  20261. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  20262. '']),
  20263. '');
  20264. AddModuleWithIntfImplSrc('unit3.pas',
  20265. LinesToStr([
  20266. '{$modeswitch externalclass}',
  20267. 'type',
  20268. ' TJSBufferSource = class external name ''BufferSource''',
  20269. ' end;',
  20270. '']),
  20271. '');
  20272. StartUnit(true);
  20273. Add([
  20274. 'interface',
  20275. 'uses unit2, unit3;',
  20276. 'procedure DoSome(s: TJSBufferSource);',
  20277. 'implementation',
  20278. 'procedure DoSome(s: TJSBufferSource);',
  20279. 'begin',
  20280. ' DoIt(s);',
  20281. 'end;',
  20282. '']);
  20283. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  20284. nIncompatibleTypeArgNo);
  20285. ConvertUnit;
  20286. end;
  20287. procedure TTestModule.TestExternalClass_NestedConstructor;
  20288. begin
  20289. StartProgram(false);
  20290. Add([
  20291. '{$modeswitch externalclass}',
  20292. 'type',
  20293. ' TJSObject = class external name ''Object''',
  20294. ' type TBird = class external name ''Bird''',
  20295. ' type TWing = class external name ''Wing''',
  20296. ' constructor New;',
  20297. ' constructor Create(w: word = 3);',
  20298. ' end;',
  20299. ' end;',
  20300. ' end;',
  20301. 'var',
  20302. ' w: TJSObject.TBird.TWing;',
  20303. 'begin',
  20304. ' w:=tjsobject.tbird.twing.new;',
  20305. ' w:=tjsobject.tbird.twing.new();',
  20306. ' w:=tjsobject.tbird.twing.create;',
  20307. ' w:=tjsobject.tbird.twing.create(4);',
  20308. ' with tjsobject do begin',
  20309. ' w:=tbird.twing.new;',
  20310. ' w:=tbird.twing.new();',
  20311. ' w:=tbird.twing.create;',
  20312. ' w:=tbird.twing.create(11);',
  20313. ' end;',
  20314. ' with tjsobject.tbird do begin',
  20315. ' w:=twing.new;',
  20316. ' w:=twing.new();',
  20317. ' w:=twing.create;',
  20318. ' w:=twing.create(21);',
  20319. ' end;',
  20320. ' with tjsobject.tbird.twing do begin',
  20321. ' w:=new;',
  20322. ' w:=new();',
  20323. ' w:=create;',
  20324. ' w:=create(31);',
  20325. ' end;',
  20326. '']);
  20327. ConvertProgram;
  20328. CheckSource('TestExternalClass_NestedConstructor',
  20329. LinesToStr([ // statements
  20330. 'this.w = null;',
  20331. '']),
  20332. LinesToStr([ // $mod.$main
  20333. '$mod.w = new Object.Bird.Wing();',
  20334. '$mod.w = new Object.Bird.Wing();',
  20335. '$mod.w = new Object.Bird.Wing.Create();',
  20336. '$mod.w = new Object.Bird.Wing.Create(4);',
  20337. '$mod.w = new Object.Bird.Wing();',
  20338. '$mod.w = new Object.Bird.Wing();',
  20339. '$mod.w = new Object.Bird.Wing.Create();',
  20340. '$mod.w = new Object.Bird.Wing.Create(11);',
  20341. 'var $with = Object.Bird;',
  20342. '$mod.w = new Object.Bird.Wing();',
  20343. '$mod.w = new Object.Bird.Wing();',
  20344. '$mod.w = new Object.Bird.Wing.Create();',
  20345. '$mod.w = new Object.Bird.Wing.Create(21);',
  20346. 'var $with1 = Object.Bird.Wing;',
  20347. '$mod.w = new $with1();',
  20348. '$mod.w = new $with1();',
  20349. '$mod.w = new Object.Bird.Wing.Create();',
  20350. '$mod.w = new Object.Bird.Wing.Create(31);',
  20351. '']));
  20352. end;
  20353. procedure TTestModule.TestClassInterface_Corba;
  20354. begin
  20355. StartProgram(false);
  20356. Add([
  20357. '{$interfaces corba}',
  20358. 'type',
  20359. ' IUnknown = interface;',
  20360. ' IUnknown = interface',
  20361. ' [''{00000000-0000-0000-C000-000000000046}'']',
  20362. ' end;',
  20363. ' IInterface = IUnknown;',
  20364. ' IBird = interface(IInterface)',
  20365. ' function GetSize: longint;',
  20366. ' procedure SetSize(i: longint);',
  20367. ' property Size: longint read GetSize write SetSize;',
  20368. ' procedure DoIt(i: longint);',
  20369. ' end;',
  20370. ' TObject = class',
  20371. ' end;',
  20372. ' TBird = class(TObject,IBird)',
  20373. ' function GetSize: longint; virtual; abstract;',
  20374. ' procedure SetSize(i: longint); virtual; abstract;',
  20375. ' procedure DoIt(i: longint); virtual; abstract;',
  20376. ' end;',
  20377. 'var',
  20378. ' BirdIntf: IBird;',
  20379. 'begin',
  20380. ' BirdIntf.Size:=BirdIntf.Size;',
  20381. '']);
  20382. ConvertProgram;
  20383. CheckSource('TestClassInterface_Corba',
  20384. LinesToStr([ // statements
  20385. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  20386. 'rtl.createInterface(this, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], this.IUnknown);',
  20387. 'rtl.createClass(this, "TObject", null, function () {',
  20388. ' this.$init = function () {',
  20389. ' };',
  20390. ' this.$final = function () {',
  20391. ' };',
  20392. '});',
  20393. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20394. ' rtl.addIntf(this, $mod.IBird);',
  20395. '});',
  20396. 'this.BirdIntf = null;',
  20397. '']),
  20398. LinesToStr([ // $mod.$main
  20399. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  20400. '']));
  20401. end;
  20402. procedure TTestModule.TestClassInterface_Corba_ProcExternalFail;
  20403. begin
  20404. StartProgram(false);
  20405. Add([
  20406. '{$interfaces corba}',
  20407. 'type',
  20408. ' IUnknown = interface',
  20409. ' procedure DoIt; external name ''foo'';',
  20410. ' end;',
  20411. 'begin']);
  20412. SetExpectedParserError(
  20413. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  20414. nParserNoFieldsAllowed);
  20415. ConvertProgram;
  20416. end;
  20417. procedure TTestModule.TestClassInterface_Corba_Overloads;
  20418. begin
  20419. StartProgram(false);
  20420. Add([
  20421. '{$interfaces corba}',
  20422. 'type',
  20423. ' integer = longint;',
  20424. ' IUnknown = interface',
  20425. ' procedure DoIt(i: integer);',
  20426. ' procedure DoIt(s: string);',
  20427. ' end;',
  20428. ' IBird = interface(IUnknown)',
  20429. ' procedure DoIt(b: boolean); overload;',
  20430. ' end;',
  20431. ' TObject = class',
  20432. ' end;',
  20433. ' TBird = class(TObject,IBird)',
  20434. ' procedure DoIt(o: TObject);',
  20435. ' procedure DoIt(s: string);',
  20436. ' procedure DoIt(i: integer);',
  20437. ' procedure DoIt(b: boolean);',
  20438. ' end;',
  20439. 'procedure TBird.DoIt(o: TObject); begin end;',
  20440. 'procedure TBird.DoIt(s: string); begin end;',
  20441. 'procedure TBird.DoIt(i: integer); begin end;',
  20442. 'procedure TBird.DoIt(b: boolean); begin end;',
  20443. 'var',
  20444. ' BirdIntf: IBird;',
  20445. 'begin',
  20446. ' BirdIntf.DoIt(3);',
  20447. ' BirdIntf.DoIt(''abc'');',
  20448. ' BirdIntf.DoIt(true);',
  20449. '']);
  20450. ConvertProgram;
  20451. CheckSource('TestClassInterface_Overloads',
  20452. LinesToStr([ // statements
  20453. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  20454. 'rtl.createInterface(this, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], this.IUnknown);',
  20455. 'rtl.createClass(this, "TObject", null, function () {',
  20456. ' this.$init = function () {',
  20457. ' };',
  20458. ' this.$final = function () {',
  20459. ' };',
  20460. '});',
  20461. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20462. ' this.DoIt = function (o) {',
  20463. ' };',
  20464. ' this.DoIt$1 = function (s) {',
  20465. ' };',
  20466. ' this.DoIt$2 = function (i) {',
  20467. ' };',
  20468. ' this.DoIt$3 = function (b) {',
  20469. ' };',
  20470. ' rtl.addIntf(this, $mod.IBird, {',
  20471. ' DoIt$2: "DoIt$3",',
  20472. ' DoIt: "DoIt$2"',
  20473. ' });',
  20474. '});',
  20475. 'this.BirdIntf = null;',
  20476. '']),
  20477. LinesToStr([ // $mod.$main
  20478. '$mod.BirdIntf.DoIt(3);',
  20479. '$mod.BirdIntf.DoIt$1("abc");',
  20480. '$mod.BirdIntf.DoIt$2(true);',
  20481. '']));
  20482. end;
  20483. procedure TTestModule.TestClassInterface_Corba_DuplicateGUIInIntfListFail;
  20484. begin
  20485. StartProgram(false);
  20486. Add([
  20487. '{$interfaces corba}',
  20488. 'type',
  20489. ' IBird = interface',
  20490. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20491. ' end;',
  20492. ' IDog = interface',
  20493. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20494. ' end;',
  20495. ' TObject = class(IBird,IDog)',
  20496. ' end;',
  20497. 'begin']);
  20498. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  20499. nDuplicateGUIDXInYZ);
  20500. ConvertProgram;
  20501. end;
  20502. procedure TTestModule.TestClassInterface_Corba_DuplicateGUIInAncestorFail;
  20503. begin
  20504. StartProgram(false);
  20505. Add([
  20506. '{$interfaces corba}',
  20507. 'type',
  20508. ' IAnimal = interface',
  20509. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20510. ' end;',
  20511. ' IBird = interface(IAnimal)',
  20512. ' end;',
  20513. ' IHawk = interface(IBird)',
  20514. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20515. ' end;',
  20516. 'begin']);
  20517. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  20518. nDuplicateGUIDXInYZ);
  20519. ConvertProgram;
  20520. end;
  20521. procedure TTestModule.TestClassInterface_Corba_AncestorImpl;
  20522. begin
  20523. StartProgram(false);
  20524. Add([
  20525. '{$interfaces corba}',
  20526. 'type',
  20527. ' integer = longint;',
  20528. ' IUnknown = interface',
  20529. ' procedure DoIt(i: integer);',
  20530. ' end;',
  20531. ' IBird = interface',
  20532. ' procedure Fly(i: integer);',
  20533. ' end;',
  20534. ' TObject = class(IUnknown)',
  20535. ' procedure DoIt(i: integer);',
  20536. ' end;',
  20537. ' TBird = class(IBird)',
  20538. ' procedure Fly(i: integer);',
  20539. ' end;',
  20540. 'procedure TObject.DoIt(i: integer); begin end;',
  20541. 'procedure TBird.Fly(i: integer); begin end;',
  20542. 'begin',
  20543. '']);
  20544. ConvertProgram;
  20545. CheckSource('TestClassInterface_Corba_AncestorImpl',
  20546. LinesToStr([ // statements
  20547. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  20548. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  20549. 'rtl.createClass(this, "TObject", null, function () {',
  20550. ' this.$init = function () {',
  20551. ' };',
  20552. ' this.$final = function () {',
  20553. ' };',
  20554. ' this.DoIt = function (i) {',
  20555. ' };',
  20556. ' rtl.addIntf(this, $mod.IUnknown);',
  20557. '});',
  20558. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20559. ' this.Fly = function (i) {',
  20560. ' };',
  20561. ' rtl.addIntf(this, $mod.IBird);',
  20562. ' rtl.addIntf(this, $mod.IUnknown);',
  20563. '});',
  20564. '']),
  20565. LinesToStr([ // $mod.$main
  20566. '']));
  20567. end;
  20568. procedure TTestModule.TestClassInterface_Corba_ImplReintroduce;
  20569. begin
  20570. StartProgram(false);
  20571. Add([
  20572. '{$interfaces corba}',
  20573. 'type',
  20574. ' integer = longint;',
  20575. ' IBird = interface',
  20576. ' procedure DoIt(i: integer);',
  20577. ' end;',
  20578. ' TObject = class',
  20579. ' procedure DoIt(i: integer);',
  20580. ' end;',
  20581. ' TBird = class(IBird)',
  20582. ' procedure DoIt(i: integer); virtual; reintroduce;',
  20583. ' end;',
  20584. 'procedure TObject.DoIt(i: integer); begin end;',
  20585. 'procedure TBird.DoIt(i: integer); begin end;',
  20586. 'begin',
  20587. '']);
  20588. ConvertProgram;
  20589. CheckSource('TestClassInterface_Corba_ImplReintroduce',
  20590. LinesToStr([ // statements
  20591. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  20592. 'rtl.createClass(this, "TObject", null, function () {',
  20593. ' this.$init = function () {',
  20594. ' };',
  20595. ' this.$final = function () {',
  20596. ' };',
  20597. ' this.DoIt = function (i) {',
  20598. ' };',
  20599. '});',
  20600. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20601. ' this.DoIt$1 = function (i) {',
  20602. ' };',
  20603. ' rtl.addIntf(this, $mod.IBird, {',
  20604. ' DoIt: "DoIt$1"',
  20605. ' });',
  20606. '});',
  20607. '']),
  20608. LinesToStr([ // $mod.$main
  20609. '']));
  20610. end;
  20611. procedure TTestModule.TestClassInterface_Corba_MethodResolution;
  20612. begin
  20613. StartProgram(false);
  20614. Add([
  20615. '{$interfaces corba}',
  20616. 'type',
  20617. ' IUnknown = interface',
  20618. ' procedure Walk(i: longint);',
  20619. ' end;',
  20620. ' IBird = interface(IUnknown)',
  20621. ' procedure Walk(b: boolean); overload;',
  20622. ' procedure Fly(s: string);',
  20623. ' end;',
  20624. ' TObject = class',
  20625. ' end;',
  20626. ' TBird = class(TObject,IBird)',
  20627. ' procedure IBird.Fly = Move;',
  20628. ' procedure IBird.Walk = Hop;',
  20629. ' procedure Hop(i: longint);',
  20630. ' procedure Move(s: string);',
  20631. ' procedure Hop(b: boolean);',
  20632. ' end;',
  20633. 'procedure TBird.Move(s: string); begin end;',
  20634. 'procedure TBird.Hop(i: longint); begin end;',
  20635. 'procedure TBird.Hop(b: boolean); begin end;',
  20636. 'var',
  20637. ' BirdIntf: IBird;',
  20638. 'begin',
  20639. ' BirdIntf.Walk(3);',
  20640. ' BirdIntf.Walk(true);',
  20641. ' BirdIntf.Fly(''abc'');',
  20642. '']);
  20643. ConvertProgram;
  20644. CheckSource('TestClassInterface_Corba_MethodResolution',
  20645. LinesToStr([ // statements
  20646. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  20647. 'rtl.createInterface(this, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], this.IUnknown);',
  20648. 'rtl.createClass(this, "TObject", null, function () {',
  20649. ' this.$init = function () {',
  20650. ' };',
  20651. ' this.$final = function () {',
  20652. ' };',
  20653. '});',
  20654. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20655. ' this.Hop = function (i) {',
  20656. ' };',
  20657. ' this.Move = function (s) {',
  20658. ' };',
  20659. ' this.Hop$1 = function (b) {',
  20660. ' };',
  20661. ' rtl.addIntf(this, $mod.IBird, {',
  20662. ' Walk$1: "Hop$1",',
  20663. ' Fly: "Move",',
  20664. ' Walk: "Hop"',
  20665. ' });',
  20666. '});',
  20667. 'this.BirdIntf = null;',
  20668. '']),
  20669. LinesToStr([ // $mod.$main
  20670. '$mod.BirdIntf.Walk(3);',
  20671. '$mod.BirdIntf.Walk$1(true);',
  20672. '$mod.BirdIntf.Fly("abc");',
  20673. '']));
  20674. end;
  20675. procedure TTestModule.TestClassInterface_COM_AncestorMoreInterfaces;
  20676. begin
  20677. StartProgram(false);
  20678. Add([
  20679. '{$interfaces com}',
  20680. 'type',
  20681. ' IUnknown = interface',
  20682. ' function _AddRef: longint;',
  20683. ' procedure Walk;',
  20684. ' end;',
  20685. ' IBird = interface end;',
  20686. ' IDog = interface end;',
  20687. ' TObject = class(IBird,IDog)',
  20688. ' function _AddRef: longint; virtual; abstract;',
  20689. ' procedure Walk; virtual; abstract;',
  20690. ' end;',
  20691. ' TBird = class(IUnknown)',
  20692. ' end;',
  20693. 'begin',
  20694. '']);
  20695. ConvertProgram;
  20696. CheckSource('TestClassInterface_COM_AncestorMoreInterfaces',
  20697. LinesToStr([ // statements
  20698. 'rtl.createInterface(this, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  20699. 'rtl.createInterface(this, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], this.IUnknown);',
  20700. 'rtl.createInterface(this, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], this.IUnknown);',
  20701. 'rtl.createClass(this, "TObject", null, function () {',
  20702. ' this.$init = function () {',
  20703. ' };',
  20704. ' this.$final = function () {',
  20705. ' };',
  20706. ' rtl.addIntf(this, $mod.IBird);',
  20707. ' rtl.addIntf(this, $mod.IDog);',
  20708. '});',
  20709. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20710. ' rtl.addIntf(this, $mod.IUnknown);',
  20711. ' rtl.addIntf(this, $mod.IBird);',
  20712. ' rtl.addIntf(this, $mod.IDog);',
  20713. '});',
  20714. '']),
  20715. LinesToStr([ // $mod.$main
  20716. '']));
  20717. end;
  20718. procedure TTestModule.TestClassInterface_Corba_MethodOverride;
  20719. begin
  20720. StartProgram(false);
  20721. Add([
  20722. '{$interfaces corba}',
  20723. 'type',
  20724. ' IUnknown = interface',
  20725. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  20726. ' procedure Go;',
  20727. ' end;',
  20728. ' TObject = class(IUnknown)',
  20729. ' procedure Go; virtual; abstract;',
  20730. ' end;',
  20731. ' TBird = class',
  20732. ' procedure Go; override;',
  20733. ' end;',
  20734. ' TCat = class(TObject)',
  20735. ' procedure Go; override;',
  20736. ' end;',
  20737. ' TDog = class(TObject, IUnknown)',
  20738. ' procedure Go; override;',
  20739. ' end;',
  20740. 'procedure TBird.Go; begin end;',
  20741. 'procedure TCat.Go; begin end;',
  20742. 'procedure TDog.Go; begin end;',
  20743. 'begin',
  20744. '']);
  20745. ConvertProgram;
  20746. CheckSource('TestClassInterface_Corba_MethodOverride',
  20747. LinesToStr([ // statements
  20748. 'rtl.createInterface(this, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  20749. 'rtl.createClass(this, "TObject", null, function () {',
  20750. ' this.$init = function () {',
  20751. ' };',
  20752. ' this.$final = function () {',
  20753. ' };',
  20754. ' rtl.addIntf(this, $mod.IUnknown);',
  20755. '});',
  20756. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20757. ' this.Go = function () {',
  20758. ' };',
  20759. ' rtl.addIntf(this, $mod.IUnknown);',
  20760. '});',
  20761. 'rtl.createClass(this, "TCat", this.TObject, function () {',
  20762. ' this.Go = function () {',
  20763. ' };',
  20764. ' rtl.addIntf(this, $mod.IUnknown);',
  20765. '});',
  20766. 'rtl.createClass(this, "TDog", this.TObject, function () {',
  20767. ' this.Go = function () {',
  20768. ' };',
  20769. ' rtl.addIntf(this, $mod.IUnknown);',
  20770. '});',
  20771. '']),
  20772. LinesToStr([ // $mod.$main
  20773. '']));
  20774. end;
  20775. procedure TTestModule.TestClassInterface_Corba_Delegation;
  20776. begin
  20777. StartProgram(false);
  20778. Add([
  20779. '{$interfaces corba}',
  20780. 'type',
  20781. ' IUnknown = interface',
  20782. ' end;',
  20783. ' IBird = interface(IUnknown)',
  20784. ' procedure Fly(s: string);',
  20785. ' end;',
  20786. ' IEagle = interface(IBird)',
  20787. ' end;',
  20788. ' IDove = interface(IBird)',
  20789. ' end;',
  20790. ' ISwallow = interface(IBird)',
  20791. ' end;',
  20792. ' TObject = class',
  20793. ' end;',
  20794. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20795. ' procedure Fly(s: string); virtual; abstract;',
  20796. ' end;',
  20797. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20798. ' FBirdIntf: IBird;',
  20799. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  20800. ' function GetEagleIntf: IEagle; virtual; abstract;',
  20801. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20802. ' FDoveObj: TBird;',
  20803. ' property DoveObj: TBird read FDoveObj implements IDove;',
  20804. ' function GetSwallowObj: TBird; virtual; abstract;',
  20805. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20806. ' end;',
  20807. 'begin',
  20808. '']);
  20809. ConvertProgram;
  20810. CheckSource('TestClassInterface_Corba_Delegation',
  20811. LinesToStr([ // statements
  20812. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20813. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  20814. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  20815. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  20816. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  20817. 'rtl.createClass(this, "TObject", null, function () {',
  20818. ' this.$init = function () {',
  20819. ' };',
  20820. ' this.$final = function () {',
  20821. ' };',
  20822. '});',
  20823. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20824. ' rtl.addIntf(this, $mod.IBird);',
  20825. ' rtl.addIntf(this, $mod.IEagle);',
  20826. ' rtl.addIntf(this, $mod.IDove);',
  20827. ' rtl.addIntf(this, $mod.ISwallow);',
  20828. '});',
  20829. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20830. ' this.$init = function () {',
  20831. ' $mod.TObject.$init.call(this);',
  20832. ' this.FBirdIntf = null;',
  20833. ' this.FDoveObj = null;',
  20834. ' };',
  20835. ' this.$final = function () {',
  20836. ' this.FBirdIntf = undefined;',
  20837. ' this.FDoveObj = undefined;',
  20838. ' $mod.TObject.$final.call(this);',
  20839. ' };',
  20840. ' this.$intfmaps = {',
  20841. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  20842. ' return this.FBirdIntf;',
  20843. ' },',
  20844. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  20845. ' return this.GetEagleIntf();',
  20846. ' },',
  20847. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  20848. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  20849. ' },',
  20850. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  20851. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20852. ' }',
  20853. ' };',
  20854. '});',
  20855. '']),
  20856. LinesToStr([ // $mod.$main
  20857. '']));
  20858. end;
  20859. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  20860. begin
  20861. StartProgram(false);
  20862. Add([
  20863. '{$interfaces corba}',
  20864. 'type',
  20865. ' IUnknown = interface',
  20866. ' end;',
  20867. ' IBird = interface(IUnknown)',
  20868. ' procedure Fly(s: string);',
  20869. ' end;',
  20870. ' IEagle = interface(IBird)',
  20871. ' end;',
  20872. ' IDove = interface(IBird)',
  20873. ' end;',
  20874. ' ISwallow = interface(IBird)',
  20875. ' end;',
  20876. ' TObject = class',
  20877. ' end;',
  20878. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20879. ' procedure Fly(s: string); virtual; abstract;',
  20880. ' end;',
  20881. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20882. ' private',
  20883. ' class var FBirdIntf: IBird;',
  20884. ' class var FDoveObj: TBird;',
  20885. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  20886. ' class function GetSwallowObj: TBird; virtual; abstract;',
  20887. ' protected',
  20888. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  20889. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20890. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  20891. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20892. ' end;',
  20893. 'begin',
  20894. '']);
  20895. ConvertProgram;
  20896. CheckSource('TestClassInterface_Corba_DelegationStatic',
  20897. LinesToStr([ // statements
  20898. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20899. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  20900. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  20901. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  20902. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  20903. 'rtl.createClass(this, "TObject", null, function () {',
  20904. ' this.$init = function () {',
  20905. ' };',
  20906. ' this.$final = function () {',
  20907. ' };',
  20908. '});',
  20909. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20910. ' rtl.addIntf(this, $mod.IBird);',
  20911. ' rtl.addIntf(this, $mod.IEagle);',
  20912. ' rtl.addIntf(this, $mod.IDove);',
  20913. ' rtl.addIntf(this, $mod.ISwallow);',
  20914. '});',
  20915. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20916. ' this.FBirdIntf = null;',
  20917. ' this.FDoveObj = null;',
  20918. ' this.$intfmaps = {',
  20919. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  20920. ' return this.FBirdIntf;',
  20921. ' },',
  20922. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  20923. ' return this.GetEagleIntf();',
  20924. ' },',
  20925. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  20926. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  20927. ' },',
  20928. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  20929. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20930. ' }',
  20931. ' };',
  20932. '});',
  20933. '']),
  20934. LinesToStr([ // $mod.$main
  20935. '']));
  20936. end;
  20937. procedure TTestModule.TestClassInterface_Corba_Operators;
  20938. begin
  20939. StartProgram(false);
  20940. Add([
  20941. '{$interfaces corba}',
  20942. 'type',
  20943. ' IUnknown = interface',
  20944. ' end;',
  20945. ' IBird = interface(IUnknown)',
  20946. ' function GetItems(Index: longint): longint;',
  20947. ' procedure SetItems(Index: longint; Value: longint);',
  20948. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  20949. ' end;',
  20950. ' TObject = class',
  20951. ' end;',
  20952. ' TBird = class(TObject,IBird)',
  20953. ' function GetItems(Index: longint): longint; virtual; abstract;',
  20954. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  20955. ' end;',
  20956. 'var',
  20957. ' IntfVar: IBird = nil;',
  20958. ' IntfVar2: IBird;',
  20959. ' ObjVar: TBird;',
  20960. ' v: JSValue;',
  20961. 'begin',
  20962. ' IntfVar:=nil;',
  20963. ' IntfVar[3]:=IntfVar[4];',
  20964. ' if Assigned(IntfVar) then ;',
  20965. ' IntfVar:=IntfVar2;',
  20966. ' IntfVar:=ObjVar;',
  20967. ' if IntfVar=IntfVar2 then ;',
  20968. ' if IntfVar<>IntfVar2 then ;',
  20969. ' if IntfVar is IBird then ;',
  20970. ' if IntfVar is TBird then ;',
  20971. ' if ObjVar is IBird then ;',
  20972. ' IntfVar:=IntfVar2 as IBird;',
  20973. ' ObjVar:=IntfVar2 as TBird;',
  20974. ' IntfVar:=ObjVar as IBird;',
  20975. ' IntfVar:=IBird(IntfVar2);',
  20976. ' ObjVar:=TBird(IntfVar);',
  20977. ' IntfVar:=IBird(ObjVar);',
  20978. ' v:=IntfVar;',
  20979. ' IntfVar:=IBird(v);',
  20980. ' if v is IBird then ;',
  20981. ' v:=JSValue(IntfVar);',
  20982. ' v:=IBird;',
  20983. '']);
  20984. ConvertProgram;
  20985. CheckSource('TestClassInterface_Corba_Operators',
  20986. LinesToStr([ // statements
  20987. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20988. 'rtl.createInterface(this, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], this.IUnknown);',
  20989. 'rtl.createClass(this, "TObject", null, function () {',
  20990. ' this.$init = function () {',
  20991. ' };',
  20992. ' this.$final = function () {',
  20993. ' };',
  20994. '});',
  20995. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20996. ' rtl.addIntf(this, $mod.IBird);',
  20997. '});',
  20998. 'this.IntfVar = null;',
  20999. 'this.IntfVar2 = null;',
  21000. 'this.ObjVar = null;',
  21001. 'this.v = undefined;',
  21002. '']),
  21003. LinesToStr([ // $mod.$main
  21004. '$mod.IntfVar = null;',
  21005. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  21006. 'if ($mod.IntfVar != null) ;',
  21007. '$mod.IntfVar = $mod.IntfVar2;',
  21008. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  21009. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  21010. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  21011. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  21012. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  21013. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  21014. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  21015. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  21016. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  21017. '$mod.IntfVar = $mod.IntfVar2;',
  21018. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  21019. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  21020. '$mod.v = $mod.IntfVar;',
  21021. '$mod.IntfVar = rtl.getObject($mod.v);',
  21022. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  21023. '$mod.v = $mod.IntfVar;',
  21024. '$mod.v = $mod.IBird;',
  21025. '']));
  21026. end;
  21027. procedure TTestModule.TestClassInterface_Corba_Args;
  21028. begin
  21029. StartProgram(false);
  21030. Add([
  21031. '{$interfaces corba}',
  21032. 'type',
  21033. ' IUnknown = interface',
  21034. ' end;',
  21035. ' IBird = interface(IUnknown)',
  21036. ' end;',
  21037. ' TObject = class',
  21038. ' end;',
  21039. ' TBird = class(TObject,IBird)',
  21040. ' end;',
  21041. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  21042. 'begin',
  21043. ' DoIt(i,i,i);',
  21044. 'end;',
  21045. 'procedure Change(var i: IBird; out j: IBird);',
  21046. 'begin',
  21047. ' DoIt(i,i,i);',
  21048. ' Change(i,i);',
  21049. 'end;',
  21050. 'var',
  21051. ' i: IBird;',
  21052. ' o: TBird;',
  21053. 'begin',
  21054. ' DoIt(i,i,i);',
  21055. ' Change(i,i);',
  21056. ' DoIt(o,o,o);',
  21057. '']);
  21058. ConvertProgram;
  21059. CheckSource('TestClassInterface_Corba_Args',
  21060. LinesToStr([ // statements
  21061. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21062. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  21063. 'rtl.createClass(this, "TObject", null, function () {',
  21064. ' this.$init = function () {',
  21065. ' };',
  21066. ' this.$final = function () {',
  21067. ' };',
  21068. '});',
  21069. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21070. ' rtl.addIntf(this, $mod.IBird);',
  21071. '});',
  21072. 'this.DoIt = function (u, i, j) {',
  21073. ' $mod.DoIt({',
  21074. ' get: function () {',
  21075. ' return i;',
  21076. ' },',
  21077. ' set: function (v) {',
  21078. ' i = v;',
  21079. ' }',
  21080. ' }, i, i);',
  21081. '};',
  21082. 'this.Change = function (i, j) {',
  21083. ' $mod.DoIt(i, i.get(), i.get());',
  21084. ' $mod.Change(i, i);',
  21085. '};',
  21086. 'this.i = null;',
  21087. 'this.o = null;',
  21088. '']),
  21089. LinesToStr([ // $mod.$main
  21090. '$mod.DoIt({',
  21091. ' p: $mod,',
  21092. ' get: function () {',
  21093. ' return this.p.i;',
  21094. ' },',
  21095. ' set: function (v) {',
  21096. ' this.p.i = v;',
  21097. ' }',
  21098. '}, $mod.i, $mod.i);',
  21099. '$mod.Change({',
  21100. ' p: $mod,',
  21101. ' get: function () {',
  21102. ' return this.p.i;',
  21103. ' },',
  21104. ' set: function (v) {',
  21105. ' this.p.i = v;',
  21106. ' }',
  21107. '}, {',
  21108. ' p: $mod,',
  21109. ' get: function () {',
  21110. ' return this.p.i;',
  21111. ' },',
  21112. ' set: function (v) {',
  21113. ' this.p.i = v;',
  21114. ' }',
  21115. '});',
  21116. '$mod.DoIt({',
  21117. ' p: $mod,',
  21118. ' get: function () {',
  21119. ' return this.p.o;',
  21120. ' },',
  21121. ' set: function (v) {',
  21122. ' this.p.o = v;',
  21123. ' }',
  21124. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  21125. '']));
  21126. end;
  21127. procedure TTestModule.TestClassInterface_Corba_ForIn;
  21128. begin
  21129. StartProgram(false);
  21130. Add([
  21131. '{$interfaces corba}',
  21132. 'type',
  21133. ' IUnknown = interface end;',
  21134. ' TObject = class',
  21135. ' Id: longint;',
  21136. ' end;',
  21137. ' IEnumerator = interface(IUnknown)',
  21138. ' function GetCurrent: TObject;',
  21139. ' function MoveNext: Boolean;',
  21140. ' property Current: TObject read GetCurrent;',
  21141. ' end;',
  21142. ' IEnumerable = interface(IUnknown)',
  21143. ' function GetEnumerator: IEnumerator;',
  21144. ' end;',
  21145. 'var',
  21146. ' o: TObject;',
  21147. ' i: IEnumerable;',
  21148. 'begin',
  21149. ' for o in i do o.Id:=3;',
  21150. '']);
  21151. ConvertProgram;
  21152. CheckSource('TestClassInterface_Corba_ForIn',
  21153. LinesToStr([ // statements
  21154. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21155. 'rtl.createClass(this, "TObject", null, function () {',
  21156. ' this.$init = function () {',
  21157. ' this.Id = 0;',
  21158. ' };',
  21159. ' this.$final = function () {',
  21160. ' };',
  21161. '});',
  21162. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  21163. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  21164. 'this.o = null;',
  21165. 'this.i = null;',
  21166. '']),
  21167. LinesToStr([ // $mod.$main
  21168. 'var $in = $mod.i.GetEnumerator();',
  21169. 'while ($in.MoveNext()) {',
  21170. ' $mod.o = $in.GetCurrent();',
  21171. ' $mod.o.Id = 3;',
  21172. '};',
  21173. '']));
  21174. end;
  21175. procedure TTestModule.TestClassInterface_Corba_ArrayOfIntf;
  21176. begin
  21177. StartProgram(false);
  21178. Add([
  21179. '{$interfaces corba}',
  21180. 'type',
  21181. ' IUnknown = interface end;',
  21182. ' IBird = interface(IUnknown)',
  21183. ' function Fly(w: word): word;',
  21184. ' end;',
  21185. ' TBirdArray = array of IBird;',
  21186. 'var',
  21187. ' i: IBird;',
  21188. ' a: TBirdArray;',
  21189. 'begin',
  21190. ' SetLength(a,3);',
  21191. ' i:=a[1];',
  21192. ' a[2]:=i;',
  21193. ' for i in a do i.fly(3);',
  21194. '']);
  21195. ConvertProgram;
  21196. CheckSource('TestClassInterface_Corba_ArrayOfIntf',
  21197. LinesToStr([ // statements
  21198. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21199. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  21200. 'this.i = null;',
  21201. 'this.a = [];',
  21202. '']),
  21203. LinesToStr([ // $mod.$main
  21204. '$mod.a = rtl.arraySetLength($mod.a, null, 3);',
  21205. '$mod.i = $mod.a[1];',
  21206. '$mod.a[2] = $mod.i;',
  21207. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  21208. ' $mod.i = $in[$l];',
  21209. ' $mod.i.Fly(3);',
  21210. '};',
  21211. '']));
  21212. end;
  21213. procedure TTestModule.TestClassInterface_COM_AssignVar;
  21214. begin
  21215. StartProgram(false);
  21216. Add([
  21217. '{$interfaces com}',
  21218. 'type',
  21219. ' IUnknown = interface',
  21220. ' function _AddRef: longint;',
  21221. ' function _Release: longint;',
  21222. ' end;',
  21223. ' TObject = class(IUnknown)',
  21224. ' function _AddRef: longint; virtual; abstract;',
  21225. ' function _Release: longint; virtual; abstract;',
  21226. ' end;',
  21227. 'var',
  21228. ' i: IUnknown;',
  21229. 'procedure DoGlobal(o: TObject);',
  21230. 'begin',
  21231. ' i:=nil;',
  21232. ' i:=o;',
  21233. ' i:=i;',
  21234. 'end;',
  21235. 'procedure DoLocal(o: TObject);',
  21236. 'const k: IUnknown = nil;',
  21237. 'var j: IUnknown;',
  21238. 'begin',
  21239. ' k:=o;',
  21240. ' k:=i;',
  21241. ' j:=o;',
  21242. ' j:=i;',
  21243. 'end;',
  21244. 'var o: TObject;',
  21245. 'begin',
  21246. ' i:=nil;',
  21247. ' i:=o;',
  21248. '']);
  21249. ConvertProgram;
  21250. CheckSource('TestClassInterface_COM_AssignVar',
  21251. LinesToStr([ // statements
  21252. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21253. 'rtl.createClass(this, "TObject", null, function () {',
  21254. ' this.$init = function () {',
  21255. ' };',
  21256. ' this.$final = function () {',
  21257. ' };',
  21258. ' rtl.addIntf(this, $mod.IUnknown);',
  21259. '});',
  21260. 'this.i = null;',
  21261. 'this.DoGlobal = function (o) {',
  21262. ' rtl.setIntfP($mod, "i", null);',
  21263. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  21264. ' rtl.setIntfP($mod, "i", $mod.i);',
  21265. '};',
  21266. 'var k = null;',
  21267. 'this.DoLocal = function (o) {',
  21268. ' var j = null;',
  21269. ' try{',
  21270. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21271. ' k = rtl.setIntfL(k, $mod.i);',
  21272. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21273. ' j = rtl.setIntfL(j, $mod.i);',
  21274. ' }finally{',
  21275. ' rtl._Release(j);',
  21276. ' };',
  21277. '};',
  21278. 'this.o = null;',
  21279. '']),
  21280. LinesToStr([ // $mod.$main
  21281. 'rtl.setIntfP($mod, "i", null);',
  21282. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  21283. '']));
  21284. end;
  21285. procedure TTestModule.TestClassInterface_COM_AssignArg;
  21286. begin
  21287. StartProgram(false);
  21288. Add([
  21289. '{$interfaces com}',
  21290. 'type',
  21291. ' IUnknown = interface',
  21292. ' function _AddRef: longint;',
  21293. ' function _Release: longint;',
  21294. ' end;',
  21295. ' TObject = class(IUnknown)',
  21296. ' function _AddRef: longint; virtual; abstract;',
  21297. ' function _Release: longint; virtual; abstract;',
  21298. ' end;',
  21299. 'procedure DoDefault(i, j: IUnknown);',
  21300. 'begin',
  21301. ' i:=nil;',
  21302. ' i:=j;',
  21303. 'end;',
  21304. 'begin',
  21305. '']);
  21306. ConvertProgram;
  21307. CheckSource('TestClassInterface_COM_AssignArg',
  21308. LinesToStr([ // statements
  21309. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21310. 'rtl.createClass(this, "TObject", null, function () {',
  21311. ' this.$init = function () {',
  21312. ' };',
  21313. ' this.$final = function () {',
  21314. ' };',
  21315. ' rtl.addIntf(this, $mod.IUnknown);',
  21316. '});',
  21317. 'this.DoDefault = function (i, j) {',
  21318. ' rtl._AddRef(i);',
  21319. ' try {',
  21320. ' i = rtl.setIntfL(i, null);',
  21321. ' i = rtl.setIntfL(i, j);',
  21322. ' } finally {',
  21323. ' rtl._Release(i);',
  21324. ' };',
  21325. '};',
  21326. '']),
  21327. LinesToStr([ // $mod.$main
  21328. '']));
  21329. end;
  21330. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  21331. begin
  21332. StartProgram(false);
  21333. Add([
  21334. '{$interfaces com}',
  21335. 'type',
  21336. ' IUnknown = interface',
  21337. ' function _AddRef: longint;',
  21338. ' function _Release: longint;',
  21339. ' end;',
  21340. ' TObject = class(IUnknown)',
  21341. ' function _AddRef: longint; virtual; abstract;',
  21342. ' function _Release: longint; virtual; abstract;',
  21343. ' end;',
  21344. 'function DoDefault(i: IUnknown): IUnknown;',
  21345. 'begin',
  21346. ' Result:=i;',
  21347. ' if Result<>nil then exit;',
  21348. 'end;',
  21349. 'begin',
  21350. '']);
  21351. ConvertProgram;
  21352. CheckSource('TestClassInterface_COM_FunctionResult',
  21353. LinesToStr([ // statements
  21354. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21355. 'rtl.createClass(this, "TObject", null, function () {',
  21356. ' this.$init = function () {',
  21357. ' };',
  21358. ' this.$final = function () {',
  21359. ' };',
  21360. ' rtl.addIntf(this, $mod.IUnknown);',
  21361. '});',
  21362. 'this.DoDefault = function (i) {',
  21363. ' var Result = null;',
  21364. ' var $ok = false;',
  21365. ' try {',
  21366. ' Result = rtl.setIntfL(Result, i);',
  21367. ' if(Result !== null){',
  21368. ' $ok = true;',
  21369. ' return Result;',
  21370. ' };',
  21371. ' $ok = true;',
  21372. ' } finally {',
  21373. ' if(!$ok) rtl._Release(Result);',
  21374. ' };',
  21375. ' return Result;',
  21376. '};',
  21377. '']),
  21378. LinesToStr([ // $mod.$main
  21379. '']));
  21380. end;
  21381. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  21382. begin
  21383. StartProgram(false);
  21384. Add([
  21385. '{$interfaces com}',
  21386. 'type',
  21387. ' IUnknown = interface',
  21388. ' function _AddRef: longint;',
  21389. ' function _Release: longint;',
  21390. ' end;',
  21391. ' TObject = class(IUnknown)',
  21392. ' function _AddRef: longint; virtual; abstract;',
  21393. ' function _Release: longint; virtual; abstract;',
  21394. ' function GetIntf: IUnknown; virtual;',
  21395. ' end;',
  21396. ' TMouse = class',
  21397. ' function GetIntf: IUnknown; override;',
  21398. ' end;',
  21399. 'function TObject.GetIntf: IUnknown; begin end;',
  21400. 'function TMouse.GetIntf: IUnknown;',
  21401. 'var i: IUnknown;',
  21402. 'begin',
  21403. ' inherited;',
  21404. ' inherited GetIntf;',
  21405. ' inherited GetIntf();',
  21406. ' Result:=inherited GetIntf;',
  21407. ' Result:=inherited GetIntf();',
  21408. ' i:=inherited GetIntf;',
  21409. ' i:=inherited GetIntf();',
  21410. 'end;',
  21411. 'begin',
  21412. '']);
  21413. ConvertProgram;
  21414. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  21415. LinesToStr([ // statements
  21416. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21417. 'rtl.createClass(this, "TObject", null, function () {',
  21418. ' this.$init = function () {',
  21419. ' };',
  21420. ' this.$final = function () {',
  21421. ' };',
  21422. ' this.GetIntf = function () {',
  21423. ' var Result = null;',
  21424. ' return Result;',
  21425. ' };',
  21426. ' rtl.addIntf(this, $mod.IUnknown);',
  21427. '});',
  21428. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  21429. ' this.GetIntf = function () {',
  21430. ' var Result = null;',
  21431. ' var i = null;',
  21432. ' var $ir = rtl.createIntfRefs();',
  21433. ' var $ok = false;',
  21434. ' try {',
  21435. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  21436. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  21437. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  21438. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  21439. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  21440. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  21441. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  21442. ' $ok = true;',
  21443. ' } finally {',
  21444. ' $ir.free();',
  21445. ' rtl._Release(i);',
  21446. ' if (!$ok) rtl._Release(Result);',
  21447. ' };',
  21448. ' return Result;',
  21449. ' };',
  21450. ' rtl.addIntf(this, $mod.IUnknown);',
  21451. '});',
  21452. '']),
  21453. LinesToStr([ // $mod.$main
  21454. '']));
  21455. end;
  21456. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  21457. begin
  21458. StartProgram(false);
  21459. Add([
  21460. '{$interfaces com}',
  21461. 'type',
  21462. ' IUnknown = interface',
  21463. ' function _AddRef: longint;',
  21464. ' function _Release: longint;',
  21465. ' end;',
  21466. ' TObject = class(IUnknown)',
  21467. ' function _AddRef: longint; virtual; abstract;',
  21468. ' function _Release: longint; virtual; abstract;',
  21469. ' end;',
  21470. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  21471. 'begin',
  21472. ' if i is IUnknown then ;',
  21473. ' if o is IUnknown then ;',
  21474. ' if i is TObject then ;',
  21475. ' i:=j as IUnknown;',
  21476. ' i:=o as IUnknown;',
  21477. ' o:=j as TObject;',
  21478. ' i:=IUnknown(j);',
  21479. ' i:=IUnknown(o);',
  21480. ' o:=TObject(i);',
  21481. 'end;',
  21482. 'begin',
  21483. '']);
  21484. ConvertProgram;
  21485. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  21486. LinesToStr([ // statements
  21487. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21488. 'rtl.createClass(this, "TObject", null, function () {',
  21489. ' this.$init = function () {',
  21490. ' };',
  21491. ' this.$final = function () {',
  21492. ' };',
  21493. ' rtl.addIntf(this, $mod.IUnknown);',
  21494. '});',
  21495. 'this.DoDefault = function (i, j, o) {',
  21496. ' rtl._AddRef(i);',
  21497. ' try {',
  21498. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  21499. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  21500. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  21501. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  21502. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21503. ' o = rtl.intfAsClass(j, $mod.TObject);',
  21504. ' i = rtl.setIntfL(i, j);',
  21505. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21506. ' o = rtl.intfToClass(i, $mod.TObject);',
  21507. ' } finally {',
  21508. ' rtl._Release(i);',
  21509. ' };',
  21510. '};',
  21511. '']),
  21512. LinesToStr([ // $mod.$main
  21513. '']));
  21514. end;
  21515. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  21516. begin
  21517. StartProgram(false);
  21518. Add([
  21519. '{$interfaces com}',
  21520. 'type',
  21521. ' IUnknown = interface',
  21522. ' function _AddRef: longint;',
  21523. ' function _Release: longint;',
  21524. ' end;',
  21525. ' TObject = class(IUnknown)',
  21526. ' function _AddRef: longint; virtual; abstract;',
  21527. ' function _Release: longint; virtual; abstract;',
  21528. ' end;',
  21529. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  21530. 'var o: TObject;',
  21531. 'begin',
  21532. ' DoIt(v,v,v,v);',
  21533. ' DoIt(o,o,k,k);',
  21534. 'end;',
  21535. 'procedure DoSome;',
  21536. 'var v: IUnknown;',
  21537. 'begin',
  21538. ' DoIt(v,v,v,v);',
  21539. 'end;',
  21540. 'var i: IUnknown;',
  21541. 'begin',
  21542. ' DoIt(i,i,i,i);',
  21543. '']);
  21544. ConvertProgram;
  21545. CheckSource('TestClassInterface_COM_PassAsArg',
  21546. LinesToStr([ // statements
  21547. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21548. 'rtl.createClass(this, "TObject", null, function () {',
  21549. ' this.$init = function () {',
  21550. ' };',
  21551. ' this.$final = function () {',
  21552. ' };',
  21553. ' rtl.addIntf(this, $mod.IUnknown);',
  21554. '});',
  21555. 'this.DoIt = function (v, j, k, l) {',
  21556. ' var o = null;',
  21557. ' var $ir = rtl.createIntfRefs();',
  21558. ' rtl._AddRef(v);',
  21559. ' try {',
  21560. ' $mod.DoIt(v, v, {',
  21561. ' get: function () {',
  21562. ' return v;',
  21563. ' },',
  21564. ' set: function (w) {',
  21565. ' v = rtl.setIntfL(v, w);',
  21566. ' }',
  21567. ' }, {',
  21568. ' get: function () {',
  21569. ' return v;',
  21570. ' },',
  21571. ' set: function (w) {',
  21572. ' v = rtl.setIntfL(v, w);',
  21573. ' }',
  21574. ' });',
  21575. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  21576. ' } finally {',
  21577. ' $ir.free();',
  21578. ' rtl._Release(v);',
  21579. ' };',
  21580. '};',
  21581. 'this.DoSome = function () {',
  21582. ' var v = null;',
  21583. ' try {',
  21584. ' $mod.DoIt(v, v, {',
  21585. ' get: function () {',
  21586. ' return v;',
  21587. ' },',
  21588. ' set: function (w) {',
  21589. ' v = rtl.setIntfL(v, w);',
  21590. ' }',
  21591. ' }, {',
  21592. ' get: function () {',
  21593. ' return v;',
  21594. ' },',
  21595. ' set: function (w) {',
  21596. ' v = rtl.setIntfL(v, w);',
  21597. ' }',
  21598. ' });',
  21599. ' } finally {',
  21600. ' rtl._Release(v);',
  21601. ' };',
  21602. '};',
  21603. 'this.i = null;',
  21604. '']),
  21605. LinesToStr([ // $mod.$main
  21606. '$mod.DoIt($mod.i, $mod.i, {',
  21607. ' p: $mod,',
  21608. ' get: function () {',
  21609. ' return this.p.i;',
  21610. ' },',
  21611. ' set: function (v) {',
  21612. ' rtl.setIntfP(this.p, "i", v);',
  21613. ' }',
  21614. '}, {',
  21615. ' p: $mod,',
  21616. ' get: function () {',
  21617. ' return this.p.i;',
  21618. ' },',
  21619. ' set: function (v) {',
  21620. ' rtl.setIntfP(this.p, "i", v);',
  21621. ' }',
  21622. '});',
  21623. '']));
  21624. end;
  21625. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  21626. begin
  21627. StartProgram(false);
  21628. Add([
  21629. '{$interfaces com}',
  21630. 'type',
  21631. ' IUnknown = interface',
  21632. ' function _AddRef: longint;',
  21633. ' function _Release: longint;',
  21634. ' end;',
  21635. ' TObject = class(IUnknown)',
  21636. ' function _AddRef: longint; virtual; abstract;',
  21637. ' function _Release: longint; virtual; abstract;',
  21638. ' end;',
  21639. 'procedure DoIt(out i);',
  21640. 'begin end;',
  21641. 'procedure DoSome;',
  21642. 'var v: IUnknown;',
  21643. 'begin',
  21644. ' DoIt(v);',
  21645. 'end;',
  21646. 'function GetIt: IUnknown;',
  21647. 'begin',
  21648. ' DoIt(Result);',
  21649. 'end;',
  21650. 'var i: IUnknown;',
  21651. 'begin',
  21652. ' DoIt(i);',
  21653. '']);
  21654. ConvertProgram;
  21655. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  21656. LinesToStr([ // statements
  21657. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21658. 'rtl.createClass(this, "TObject", null, function () {',
  21659. ' this.$init = function () {',
  21660. ' };',
  21661. ' this.$final = function () {',
  21662. ' };',
  21663. ' rtl.addIntf(this, $mod.IUnknown);',
  21664. '});',
  21665. 'this.DoIt = function (i) {',
  21666. '};',
  21667. 'this.DoSome = function () {',
  21668. ' var v = null;',
  21669. ' try {',
  21670. ' $mod.DoIt({',
  21671. ' get: function () {',
  21672. ' return v;',
  21673. ' },',
  21674. ' set: function (w) {',
  21675. ' v = w;',
  21676. ' }',
  21677. ' });',
  21678. ' } finally {',
  21679. ' rtl._Release(v);',
  21680. ' };',
  21681. '};',
  21682. 'this.GetIt = function () {',
  21683. ' var Result = null;',
  21684. ' var $ok = false;',
  21685. ' try {',
  21686. ' $mod.DoIt({',
  21687. ' get: function () {',
  21688. ' return Result;',
  21689. ' },',
  21690. ' set: function (v) {',
  21691. ' Result = v;',
  21692. ' }',
  21693. ' });',
  21694. ' $ok = true;',
  21695. ' } finally {',
  21696. ' if (!$ok) rtl._Release(Result);',
  21697. ' };',
  21698. ' return Result;',
  21699. '};',
  21700. 'this.i = null;',
  21701. '']),
  21702. LinesToStr([ // $mod.$main
  21703. 'try {',
  21704. ' $mod.DoIt({',
  21705. ' p: $mod,',
  21706. ' get: function () {',
  21707. ' return this.p.i;',
  21708. ' },',
  21709. ' set: function (v) {',
  21710. ' this.p.i = v;',
  21711. ' }',
  21712. ' });',
  21713. '} finally {',
  21714. ' rtl._Release($mod.i);',
  21715. '};',
  21716. '']));
  21717. end;
  21718. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  21719. begin
  21720. StartProgram(false);
  21721. Add([
  21722. '{$interfaces com}',
  21723. 'type',
  21724. ' IUnknown = interface',
  21725. ' function _AddRef: longint;',
  21726. ' function _Release: longint;',
  21727. ' end;',
  21728. ' TObject = class(IUnknown)',
  21729. ' function _AddRef: longint; virtual; abstract;',
  21730. ' function _Release: longint; virtual; abstract;',
  21731. ' end;',
  21732. 'function GetIt: IUnknown;',
  21733. 'begin',
  21734. 'end;',
  21735. 'procedure DoSome;',
  21736. 'var v: IUnknown;',
  21737. ' i: longint;',
  21738. 'begin',
  21739. ' v:=GetIt;',
  21740. ' v:=GetIt();',
  21741. ' GetIt()._AddRef;',
  21742. ' i:=GetIt()._AddRef;',
  21743. 'end;',
  21744. 'var v: IUnknown;',
  21745. ' i: longint;',
  21746. 'begin',
  21747. ' v:=GetIt;',
  21748. ' v:=GetIt();',
  21749. ' GetIt()._AddRef;',
  21750. ' i:=GetIt()._AddRef;',
  21751. '']);
  21752. ConvertProgram;
  21753. CheckSource('TestClassInterface_COM_FunctionInExpr',
  21754. LinesToStr([ // statements
  21755. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21756. 'rtl.createClass(this, "TObject", null, function () {',
  21757. ' this.$init = function () {',
  21758. ' };',
  21759. ' this.$final = function () {',
  21760. ' };',
  21761. ' rtl.addIntf(this, $mod.IUnknown);',
  21762. '});',
  21763. 'this.GetIt = function () {',
  21764. ' var Result = null;',
  21765. ' return Result;',
  21766. '};',
  21767. 'this.DoSome = function () {',
  21768. ' var v = null;',
  21769. ' var i = 0;',
  21770. ' var $ir = rtl.createIntfRefs();',
  21771. ' try {',
  21772. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  21773. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  21774. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  21775. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  21776. ' } finally {',
  21777. ' $ir.free();',
  21778. ' rtl._Release(v);',
  21779. ' };',
  21780. '};',
  21781. 'this.v = null;',
  21782. 'this.i = 0;',
  21783. '']),
  21784. LinesToStr([ // $mod.$main
  21785. 'var $ir = rtl.createIntfRefs();',
  21786. 'try {',
  21787. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  21788. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  21789. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  21790. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  21791. '} finally {',
  21792. ' $ir.free();',
  21793. '};',
  21794. '']));
  21795. end;
  21796. procedure TTestModule.TestClassInterface_COM_Property;
  21797. begin
  21798. StartProgram(false);
  21799. Add([
  21800. '{$interfaces com}',
  21801. 'type',
  21802. ' IUnknown = interface',
  21803. ' function _AddRef: longint;',
  21804. ' function _Release: longint;',
  21805. ' end;',
  21806. ' TObject = class(IUnknown)',
  21807. ' FAnt: IUnknown;',
  21808. ' function _AddRef: longint; virtual; abstract;',
  21809. ' function _Release: longint; virtual; abstract;',
  21810. ' function GetBird: IUnknown; virtual; abstract;',
  21811. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  21812. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  21813. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  21814. ' property Ant: IUnknown read FAnt write FAnt;',
  21815. ' property Bird: IUnknown read GetBird write SetBird;',
  21816. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  21817. ' end;',
  21818. 'procedure DoIt;',
  21819. 'var',
  21820. ' o: TObject;',
  21821. ' v: IUnknown;',
  21822. 'begin',
  21823. ' v:=o.Ant;',
  21824. ' o.Ant:=v;',
  21825. ' o.Ant:=o.Ant;',
  21826. ' v:=o.Bird;',
  21827. ' o.Bird:=v;',
  21828. ' o.Bird:=o.Bird;',
  21829. ' v:=o.Items[1];',
  21830. ' o.Items[2]:=v;',
  21831. ' o.Items[3]:=o.Items[4];',
  21832. ' v:=o[5];',
  21833. ' o[6]:=v;',
  21834. ' o[7]:=o[8];',
  21835. 'end;',
  21836. 'begin',
  21837. '']);
  21838. ConvertProgram;
  21839. CheckSource('TestClassInterface_COM_Property',
  21840. LinesToStr([ // statements
  21841. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21842. 'rtl.createClass(this, "TObject", null, function () {',
  21843. ' this.$init = function () {',
  21844. ' this.FAnt = null;',
  21845. ' };',
  21846. ' this.$final = function () {',
  21847. ' rtl.setIntfP(this, "FAnt", null);',
  21848. ' };',
  21849. ' rtl.addIntf(this, $mod.IUnknown);',
  21850. '});',
  21851. 'this.DoIt = function () {',
  21852. ' var o = null;',
  21853. ' var v = null;',
  21854. ' var $ir = rtl.createIntfRefs();',
  21855. ' try {',
  21856. ' v = rtl.setIntfL(v, o.FAnt);',
  21857. ' rtl.setIntfP(o, "FAnt", v);',
  21858. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  21859. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  21860. ' o.SetBird(v);',
  21861. ' o.SetBird($ir.ref(1, o.GetBird()));',
  21862. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  21863. ' o.SetItems(2, v);',
  21864. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  21865. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  21866. ' o.SetItems(6, v);',
  21867. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  21868. ' } finally {',
  21869. ' $ir.free();',
  21870. ' rtl._Release(v);',
  21871. ' };',
  21872. '};',
  21873. '']),
  21874. LinesToStr([ // $mod.$main
  21875. '']));
  21876. end;
  21877. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  21878. begin
  21879. StartProgram(false);
  21880. Add([
  21881. '{$interfaces com}',
  21882. 'type',
  21883. ' IUnknown = interface',
  21884. ' function _AddRef: longint;',
  21885. ' function _Release: longint;',
  21886. ' function GetBird: IUnknown;',
  21887. ' procedure SetBird(Value: IUnknown);',
  21888. ' function GetItems(Index: longint): IUnknown;',
  21889. ' procedure SetItems(Index: longint; Value: IUnknown);',
  21890. ' property Bird: IUnknown read GetBird write SetBird;',
  21891. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  21892. ' end;',
  21893. ' TObject = class(IUnknown)',
  21894. ' function _AddRef: longint; virtual; abstract;',
  21895. ' function _Release: longint; virtual; abstract;',
  21896. ' function GetBird: IUnknown; virtual; abstract;',
  21897. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  21898. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  21899. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  21900. ' end;',
  21901. 'procedure DoIt;',
  21902. 'var',
  21903. ' o: TObject;',
  21904. ' v: IUnknown;',
  21905. 'begin',
  21906. ' v:=v.Items[1];',
  21907. ' v.Items[2]:=v;',
  21908. ' v.Items[3]:=v.Items[4];',
  21909. ' v:=v[5];',
  21910. ' v[6]:=v;',
  21911. ' v[7]:=v[8];',
  21912. ' v[9].Bird.Bird:=v;',
  21913. ' v:=v.Bird[10].Bird',
  21914. 'end;',
  21915. 'begin',
  21916. '']);
  21917. ConvertProgram;
  21918. CheckSource('TestClassInterface_COM_IntfProperty',
  21919. LinesToStr([ // statements
  21920. 'rtl.createInterface(this, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  21921. ' "_AddRef",',
  21922. ' "_Release",',
  21923. ' "GetBird",',
  21924. ' "SetBird",',
  21925. ' "GetItems",',
  21926. ' "SetItems"',
  21927. '], null);',
  21928. 'rtl.createClass(this, "TObject", null, function () {',
  21929. ' this.$init = function () {',
  21930. ' };',
  21931. ' this.$final = function () {',
  21932. ' };',
  21933. ' rtl.addIntf(this, $mod.IUnknown);',
  21934. '});',
  21935. 'this.DoIt = function () {',
  21936. ' var o = null;',
  21937. ' var v = null;',
  21938. ' var $ir = rtl.createIntfRefs();',
  21939. ' try {',
  21940. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  21941. ' v.SetItems(2, v);',
  21942. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  21943. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  21944. ' v.SetItems(6, v);',
  21945. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  21946. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  21947. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  21948. ' } finally {',
  21949. ' $ir.free();',
  21950. ' rtl._Release(v);',
  21951. ' };',
  21952. '};',
  21953. '']),
  21954. LinesToStr([ // $mod.$main
  21955. '']));
  21956. end;
  21957. procedure TTestModule.TestClassInterface_COM_Delegation;
  21958. begin
  21959. StartProgram(false);
  21960. Add([
  21961. '{$interfaces com}',
  21962. 'type',
  21963. ' IUnknown = interface',
  21964. ' function _AddRef: longint;',
  21965. ' function _Release: longint;',
  21966. ' end;',
  21967. ' IBird = interface(IUnknown)',
  21968. ' procedure Fly(s: string);',
  21969. ' end;',
  21970. ' IEagle = interface(IBird) end;',
  21971. ' IDove = interface(IBird) end;',
  21972. ' ISwallow = interface(IBird) end;',
  21973. ' TObject = class',
  21974. ' end;',
  21975. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  21976. ' function _AddRef: longint; virtual; abstract;',
  21977. ' function _Release: longint; virtual; abstract;',
  21978. ' procedure Fly(s: string); virtual; abstract;',
  21979. ' end;',
  21980. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  21981. ' function _AddRef: longint; virtual; abstract;',
  21982. ' function _Release: longint; virtual; abstract;',
  21983. ' FBirdIntf: IBird;',
  21984. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  21985. ' function GetEagleIntf: IEagle; virtual; abstract;',
  21986. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  21987. ' FDoveObj: TBird;',
  21988. ' property DoveObj: TBird read FDoveObj implements IDove;',
  21989. ' function GetSwallowObj: TBird; virtual; abstract;',
  21990. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  21991. ' end;',
  21992. 'begin',
  21993. '']);
  21994. ConvertProgram;
  21995. CheckSource('TestClassInterface_COM_Delegation',
  21996. LinesToStr([ // statements
  21997. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21998. 'rtl.createInterface(this, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], this.IUnknown);',
  21999. 'rtl.createInterface(this, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], this.IBird);',
  22000. 'rtl.createInterface(this, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], this.IBird);',
  22001. 'rtl.createInterface(this, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], this.IBird);',
  22002. 'rtl.createClass(this, "TObject", null, function () {',
  22003. ' this.$init = function () {',
  22004. ' };',
  22005. ' this.$final = function () {',
  22006. ' };',
  22007. '});',
  22008. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22009. ' rtl.addIntf(this, $mod.IBird);',
  22010. ' rtl.addIntf(this, $mod.IEagle);',
  22011. ' rtl.addIntf(this, $mod.IDove);',
  22012. ' rtl.addIntf(this, $mod.ISwallow);',
  22013. '});',
  22014. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  22015. ' this.$init = function () {',
  22016. ' $mod.TObject.$init.call(this);',
  22017. ' this.FBirdIntf = null;',
  22018. ' this.FDoveObj = null;',
  22019. ' };',
  22020. ' this.$final = function () {',
  22021. ' rtl.setIntfP(this, "FBirdIntf", null);',
  22022. ' this.FDoveObj = undefined;',
  22023. ' $mod.TObject.$final.call(this);',
  22024. ' };',
  22025. ' this.$intfmaps = {',
  22026. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  22027. ' return rtl._AddRef(this.FBirdIntf);',
  22028. ' },',
  22029. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  22030. ' return this.GetEagleIntf();',
  22031. ' },',
  22032. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  22033. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  22034. ' },',
  22035. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  22036. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  22037. ' }',
  22038. ' };',
  22039. '});',
  22040. '']),
  22041. LinesToStr([ // $mod.$main
  22042. '']));
  22043. end;
  22044. procedure TTestModule.TestClassInterface_COM_With;
  22045. begin
  22046. StartProgram(false);
  22047. Add([
  22048. '{$interfaces com}',
  22049. 'type',
  22050. ' IUnknown = interface',
  22051. ' function _AddRef: longint;',
  22052. ' function _Release: longint;',
  22053. ' function GetAnt: IUnknown;',
  22054. ' property Ant: IUnknown read GetAnt;',
  22055. ' end;',
  22056. ' TObject = class(IUnknown)',
  22057. ' function _AddRef: longint; virtual; abstract;',
  22058. ' function _Release: longint; virtual; abstract;',
  22059. ' function GetAnt: IUnknown; virtual; abstract;',
  22060. ' property Ant: IUnknown read GetAnt;',
  22061. ' end;',
  22062. 'procedure DoIt;',
  22063. 'var',
  22064. ' i: IUnknown;',
  22065. 'begin',
  22066. ' with i do ',
  22067. ' GetAnt;',
  22068. ' with i.Ant, Ant do ',
  22069. ' GetAnt;',
  22070. 'end;',
  22071. 'begin',
  22072. '']);
  22073. ConvertProgram;
  22074. CheckSource('TestClassInterface_COM_With',
  22075. LinesToStr([ // statements
  22076. 'rtl.createInterface(this, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  22077. 'rtl.createClass(this, "TObject", null, function () {',
  22078. ' this.$init = function () {',
  22079. ' };',
  22080. ' this.$final = function () {',
  22081. ' };',
  22082. ' rtl.addIntf(this, $mod.IUnknown);',
  22083. '});',
  22084. 'this.DoIt = function () {',
  22085. ' var i = null;',
  22086. ' var $ir = rtl.createIntfRefs();',
  22087. ' try {',
  22088. ' $ir.ref(1, i.GetAnt());',
  22089. ' var $with = $ir.ref(2, i.GetAnt());',
  22090. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  22091. ' $ir.ref(4, $with1.GetAnt());',
  22092. ' } finally {',
  22093. ' $ir.free();',
  22094. ' };',
  22095. '};',
  22096. '']),
  22097. LinesToStr([ // $mod.$main
  22098. '']));
  22099. end;
  22100. procedure TTestModule.TestClassInterface_COM_ForIn;
  22101. begin
  22102. StartProgram(false);
  22103. Add([
  22104. '{$interfaces com}',
  22105. 'type',
  22106. ' IUnknown = interface end;',
  22107. ' TObject = class',
  22108. ' Id: longint;',
  22109. ' end;',
  22110. ' IEnumerator = interface(IUnknown)',
  22111. ' function GetCurrent: TObject;',
  22112. ' function MoveNext: Boolean;',
  22113. ' property Current: TObject read GetCurrent;',
  22114. ' end;',
  22115. ' IEnumerable = interface(IUnknown)',
  22116. ' function GetEnumerator: IEnumerator;',
  22117. ' end;',
  22118. 'var',
  22119. ' o: TObject;',
  22120. ' i: IEnumerable;',
  22121. 'begin',
  22122. ' for o in i do o.Id:=3;',
  22123. '']);
  22124. ConvertProgram;
  22125. CheckSource('TestClassInterface_COM_ForIn',
  22126. LinesToStr([ // statements
  22127. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22128. 'rtl.createClass(this, "TObject", null, function () {',
  22129. ' this.$init = function () {',
  22130. ' this.Id = 0;',
  22131. ' };',
  22132. ' this.$final = function () {',
  22133. ' };',
  22134. '});',
  22135. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  22136. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  22137. 'this.o = null;',
  22138. 'this.i = null;',
  22139. '']),
  22140. LinesToStr([ // $mod.$main
  22141. 'var $in = $mod.i.GetEnumerator();',
  22142. 'try {',
  22143. ' while ($in.MoveNext()) {',
  22144. ' $mod.o = $in.GetCurrent();',
  22145. ' $mod.o.Id = 3;',
  22146. ' }',
  22147. '} finally {',
  22148. ' rtl._Release($in)',
  22149. '};',
  22150. '']));
  22151. end;
  22152. procedure TTestModule.TestClassInterface_COM_ArrayOfIntf;
  22153. begin
  22154. {$IFNDEF EnableCOMArrayOfIntf}
  22155. exit;
  22156. {$ENDIF}
  22157. StartProgram(false);
  22158. Add([
  22159. '{$interfaces com}',
  22160. 'type',
  22161. ' IUnknown = interface end;',
  22162. ' IBird = interface(IUnknown)',
  22163. ' function Fly(w: word): word;',
  22164. ' end;',
  22165. ' TBirdArray = array of IBird;',
  22166. 'procedure Run;',
  22167. 'var',
  22168. ' i: IBird;',
  22169. ' a,b: TBirdArray;',
  22170. 'begin',
  22171. //' SetLength(a,3);',
  22172. ' a:=b;',
  22173. ' i:=a[1];',
  22174. ' a[2]:=i;',
  22175. //' for i in a do i.fly(3);',
  22176. // a:=copy(b,1,2);
  22177. // a:=concat(b,a);
  22178. // insert(i,b,1);
  22179. // a:=[i,i];
  22180. 'end;',
  22181. // ToDo: pass TBirdArray as arg
  22182. 'begin',
  22183. '']);
  22184. ConvertProgram;
  22185. CheckSource('TestClassInterface_COM_ArrayOfIntf',
  22186. LinesToStr([ // statements
  22187. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22188. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  22189. 'this.Run = function () {',
  22190. ' var i = null;',
  22191. ' var a = [];',
  22192. ' var b = [];',
  22193. ' try {',
  22194. ' a = rtl.arrayRef(b);',
  22195. ' i = rtl.setIntfL(i, a[1]);',
  22196. ' rtl.setIntfP(a, 2, i);',
  22197. ' } finally {',
  22198. ' rtl._Release(i);',
  22199. ' rtl._ReleaseArray(a,1);',
  22200. ' };',
  22201. '};',
  22202. '']),
  22203. LinesToStr([ // $mod.$main
  22204. '']));
  22205. end;
  22206. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  22207. begin
  22208. StartProgram(false);
  22209. Add([
  22210. '{$interfaces com}',
  22211. 'type',
  22212. ' IUnknown = interface',
  22213. ' function _AddRef: longint;',
  22214. ' function _Release: longint;',
  22215. ' end;',
  22216. ' TObject = class',
  22217. ' end;',
  22218. ' TArrOfIntf = array of IUnknown;',
  22219. 'begin',
  22220. '']);
  22221. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  22222. ConvertProgram;
  22223. end;
  22224. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  22225. begin
  22226. StartProgram(false);
  22227. Add([
  22228. '{$interfaces com}',
  22229. 'type',
  22230. ' IUnknown = interface',
  22231. ' function _AddRef: longint;',
  22232. ' function _Release: longint;',
  22233. ' end;',
  22234. ' TRec = record',
  22235. ' i: IUnknown;',
  22236. ' end;',
  22237. 'begin',
  22238. '']);
  22239. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  22240. ConvertProgram;
  22241. end;
  22242. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  22243. begin
  22244. StartUnit(false);
  22245. Add([
  22246. '{$interfaces com}',
  22247. 'interface',
  22248. 'implementation',
  22249. 'type',
  22250. ' IUnknown = interface',
  22251. ' function _AddRef: longint;',
  22252. ' end;',
  22253. ' TObject = class(IUnknown)',
  22254. ' function _AddRef: longint;',
  22255. ' end;',
  22256. 'function TObject._AddRef: longint; begin end;',
  22257. 'var i: IUnknown;',
  22258. ' o: TObject;',
  22259. 'initialization',
  22260. ' i:=nil;',
  22261. ' i:=i;',
  22262. ' i:=o;',
  22263. ' if (o as IUnknown)=nil then ;',
  22264. '']);
  22265. ConvertUnit;
  22266. CheckSource('TestClassInterface_COM_UnitInitialization',
  22267. LinesToStr([ // statements
  22268. 'var $impl = $mod.$impl;',
  22269. '']),
  22270. LinesToStr([ // this.$init
  22271. 'var $ir = rtl.createIntfRefs();',
  22272. 'try {',
  22273. ' rtl.setIntfP($impl, "i", null);',
  22274. ' rtl.setIntfP($impl, "i", $impl.i);',
  22275. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  22276. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  22277. '} finally {',
  22278. ' $ir.free();',
  22279. '};',
  22280. '']),
  22281. LinesToStr([ // implementation
  22282. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  22283. 'rtl.createClass($impl, "TObject", null, function () {',
  22284. ' this.$init = function () {',
  22285. ' };',
  22286. ' this.$final = function () {',
  22287. ' };',
  22288. ' this._AddRef = function () {',
  22289. ' var Result = 0;',
  22290. ' return Result;',
  22291. ' };',
  22292. ' rtl.addIntf(this, $impl.IUnknown);',
  22293. '});',
  22294. '$impl.i = null;',
  22295. '$impl.o = null;',
  22296. ''])
  22297. );
  22298. end;
  22299. procedure TTestModule.TestClassInterface_Corba_GUID;
  22300. begin
  22301. StartProgram(false);
  22302. Add([
  22303. '{$interfaces corba}',
  22304. 'type',
  22305. ' IUnknown = interface',
  22306. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  22307. ' end;',
  22308. ' TObject = class end;',
  22309. ' TGUID = record D1, D2, D3, D4: word; end;',
  22310. ' TAliasGUID = TGUID;',
  22311. ' TGUIDString = type string;',
  22312. ' TAliasGUIDString = TGUIDString;',
  22313. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  22314. 'begin end;',
  22315. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  22316. 'begin end;',
  22317. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  22318. 'begin end;',
  22319. 'var',
  22320. ' i: IUnknown;',
  22321. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  22322. ' s: TAliasGUIDString;',
  22323. 'begin',
  22324. ' DoConstGUIDIt(IUnknown);',
  22325. ' DoDefGUID(IUnknown);',
  22326. ' DoStr(IUnknown);',
  22327. ' DoConstGUIDIt(i);',
  22328. ' DoDefGUID(i);',
  22329. ' DoStr(i);',
  22330. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  22331. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  22332. ' DoStr(g);',
  22333. ' g:=i;',
  22334. ' g:=IUnknown;',
  22335. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  22336. ' s:=i;',
  22337. ' s:=IUnknown;',
  22338. ' s:=g;',
  22339. ' if g=i then ;',
  22340. ' if i=g then ;',
  22341. ' if g=IUnknown then ;',
  22342. ' if IUnknown=g then ;',
  22343. ' if s=i then ;',
  22344. ' if i=s then ;',
  22345. ' if s=IUnknown then ;',
  22346. ' if IUnknown=s then ;',
  22347. ' if s=g then ;',
  22348. ' if g=s then ;',
  22349. '']);
  22350. ConvertProgram;
  22351. CheckSource('TestClassInterface_Corba_GUID',
  22352. LinesToStr([ // statements
  22353. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  22354. 'rtl.createClass(this, "TObject", null, function () {',
  22355. ' this.$init = function () {',
  22356. ' };',
  22357. ' this.$final = function () {',
  22358. ' };',
  22359. '});',
  22360. 'rtl.recNewT(this, "TGUID", function () {',
  22361. ' this.D1 = 0;',
  22362. ' this.D2 = 0;',
  22363. ' this.D3 = 0;',
  22364. ' this.D4 = 0;',
  22365. ' this.$eq = function (b) {',
  22366. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  22367. ' };',
  22368. ' this.$assign = function (s) {',
  22369. ' this.D1 = s.D1;',
  22370. ' this.D2 = s.D2;',
  22371. ' this.D3 = s.D3;',
  22372. ' this.D4 = s.D4;',
  22373. ' return this;',
  22374. ' };',
  22375. '});',
  22376. 'this.DoConstGUIDIt = function (g) {',
  22377. '};',
  22378. 'this.DoDefGUID = function (g) {',
  22379. '};',
  22380. 'this.DoStr = function (s) {',
  22381. '};',
  22382. 'this.i = null;',
  22383. 'this.g = this.TGUID.$clone({',
  22384. ' D1: 0xD91C9AF4,',
  22385. ' D2: 0x3C93,',
  22386. ' D3: 0x420F,',
  22387. ' D4: [',
  22388. ' 0xA3,',
  22389. ' 0x03,',
  22390. ' 0xBF,',
  22391. ' 0x5B,',
  22392. ' 0xA8,',
  22393. ' 0x2B,',
  22394. ' 0xFD,',
  22395. ' 0x23',
  22396. ' ]',
  22397. '});',
  22398. 'this.s = "";',
  22399. '']),
  22400. LinesToStr([ // $mod.$main
  22401. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  22402. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  22403. '$mod.DoStr($mod.IUnknown.$guid);',
  22404. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  22405. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  22406. '$mod.DoStr($mod.i.$guid);',
  22407. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  22408. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  22409. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  22410. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  22411. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  22412. '$mod.g.$assign({',
  22413. ' D1: 0xD91C9AF4,',
  22414. ' D2: 0x3C93,',
  22415. ' D3: 0x420F,',
  22416. ' D4: [',
  22417. ' 0xA3,',
  22418. ' 0x03,',
  22419. ' 0xBF,',
  22420. ' 0x5B,',
  22421. ' 0xA8,',
  22422. ' 0x2B,',
  22423. ' 0xFD,',
  22424. ' 0x23',
  22425. ' ]',
  22426. '});',
  22427. '$mod.s = $mod.i.$guid;',
  22428. '$mod.s = $mod.IUnknown.$guid;',
  22429. '$mod.s = rtl.guidrToStr($mod.g);',
  22430. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  22431. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  22432. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  22433. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  22434. 'if ($mod.s === $mod.i.$guid) ;',
  22435. 'if ($mod.i.$guid === $mod.s) ;',
  22436. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  22437. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  22438. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  22439. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  22440. '']));
  22441. end;
  22442. procedure TTestModule.TestClassInterface_Corba_GUIDProperty;
  22443. begin
  22444. StartProgram(false);
  22445. Add([
  22446. '{$interfaces corba}',
  22447. 'type',
  22448. ' IUnknown = interface',
  22449. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  22450. ' end;',
  22451. ' TGUID = record D1, D2, D3, D4: word; end;',
  22452. ' TAliasGUID = TGUID;',
  22453. ' TGUIDString = type string;',
  22454. ' TAliasGUIDString = TGUIDString;',
  22455. ' TObject = class',
  22456. ' function GetG: TAliasGUID; virtual; abstract;',
  22457. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  22458. ' function GetS: TAliasGUIDString; virtual; abstract;',
  22459. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  22460. ' property g: TAliasGUID read GetG write SetG;',
  22461. ' property s: TAliasGUIDString read GetS write SetS;',
  22462. ' end;',
  22463. 'var o: TObject;',
  22464. 'begin',
  22465. ' o.g:=IUnknown;',
  22466. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  22467. ' o.s:=IUnknown;',
  22468. ' o.s:=o.g;',
  22469. '']);
  22470. ConvertProgram;
  22471. CheckSource('TestClassInterface_GUIDProperty',
  22472. LinesToStr([ // statements
  22473. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  22474. 'rtl.recNewT(this, "TGUID", function () {',
  22475. ' this.D1 = 0;',
  22476. ' this.D2 = 0;',
  22477. ' this.D3 = 0;',
  22478. ' this.D4 = 0;',
  22479. ' this.$eq = function (b) {',
  22480. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  22481. ' };',
  22482. ' this.$assign = function (s) {',
  22483. ' this.D1 = s.D1;',
  22484. ' this.D2 = s.D2;',
  22485. ' this.D3 = s.D3;',
  22486. ' this.D4 = s.D4;',
  22487. ' return this;',
  22488. ' };',
  22489. '});',
  22490. 'rtl.createClass(this, "TObject", null, function () {',
  22491. ' this.$init = function () {',
  22492. ' };',
  22493. ' this.$final = function () {',
  22494. ' };',
  22495. '});',
  22496. 'this.o = null;',
  22497. '']),
  22498. LinesToStr([ // $mod.$main
  22499. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  22500. '$mod.o.SetG({',
  22501. ' D1: 0xD91C9AF4,',
  22502. ' D2: 0x3C93,',
  22503. ' D3: 0x420F,',
  22504. ' D4: [',
  22505. ' 0xA3,',
  22506. ' 0x03,',
  22507. ' 0xBF,',
  22508. ' 0x5B,',
  22509. ' 0xA8,',
  22510. ' 0x2B,',
  22511. ' 0xFD,',
  22512. ' 0x23',
  22513. ' ]',
  22514. '});',
  22515. '$mod.o.SetS($mod.IUnknown.$guid);',
  22516. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  22517. '']));
  22518. end;
  22519. procedure TTestModule.TestClassHelper_ClassVar;
  22520. begin
  22521. StartProgram(false);
  22522. Add([
  22523. 'type',
  22524. ' TObject = class',
  22525. ' end;',
  22526. ' THelper = class helper for TObject',
  22527. ' const',
  22528. ' One = 1;',
  22529. ' Two: word = 2;',
  22530. ' class var',
  22531. ' Glob: word;',
  22532. ' function Foo(w: word): word;',
  22533. ' class function Bar(w: word): word;',
  22534. ' end;',
  22535. 'function THelper.foo(w: word): word;',
  22536. 'begin',
  22537. ' Result:=w;',
  22538. ' Two:=One+w;',
  22539. ' Glob:=Glob;',
  22540. ' Result:=Self.Glob;',
  22541. ' Self.Glob:=Self.Glob;',
  22542. ' with Self do Glob:=Glob;',
  22543. 'end;',
  22544. 'class function THelper.bar(w: word): word;',
  22545. 'begin',
  22546. ' Result:=w;',
  22547. ' Two:=One;',
  22548. ' Glob:=Glob;',
  22549. ' Self.Glob:=Self.Glob;',
  22550. ' with Self do Glob:=Glob;',
  22551. 'end;',
  22552. 'var o: TObject;',
  22553. 'begin',
  22554. ' tobject.two:=tobject.one;',
  22555. ' tobject.Glob:=tobject.Glob;',
  22556. ' with tobject do begin',
  22557. ' two:=one;',
  22558. ' Glob:=Glob;',
  22559. ' end;',
  22560. ' o.two:=o.one;',
  22561. ' o.Glob:=o.Glob;',
  22562. ' with o do begin',
  22563. ' two:=one;',
  22564. ' Glob:=Glob;',
  22565. ' end;',
  22566. '']);
  22567. ConvertProgram;
  22568. CheckSource('TestClassHelper_ClassVar',
  22569. LinesToStr([ // statements
  22570. 'rtl.createClass(this, "TObject", null, function () {',
  22571. ' this.$init = function () {',
  22572. ' };',
  22573. ' this.$final = function () {',
  22574. ' };',
  22575. '});',
  22576. 'rtl.createHelper(this, "THelper", null, function () {',
  22577. ' this.One = 1;',
  22578. ' this.Two = 2;',
  22579. ' this.Glob = 0;',
  22580. ' this.Foo = function (w) {',
  22581. ' var Result = 0;',
  22582. ' Result = w;',
  22583. ' $mod.THelper.Two = 1 + w;',
  22584. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22585. ' Result = $mod.THelper.Glob;',
  22586. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22587. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22588. ' return Result;',
  22589. ' };',
  22590. ' this.Bar = function (w) {',
  22591. ' var Result = 0;',
  22592. ' Result = w;',
  22593. ' $mod.THelper.Two = 1;',
  22594. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22595. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22596. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22597. ' return Result;',
  22598. ' };',
  22599. '});',
  22600. 'this.o = null;',
  22601. '']),
  22602. LinesToStr([ // $mod.$main
  22603. '$mod.THelper.Two = 1;',
  22604. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22605. 'var $with = $mod.TObject;',
  22606. '$mod.THelper.Two = 1;',
  22607. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22608. '$mod.THelper.Two = 1;',
  22609. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22610. 'var $with1 = $mod.o;',
  22611. '$mod.THelper.Two = 1;',
  22612. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22613. '']));
  22614. end;
  22615. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  22616. begin
  22617. StartProgram(false);
  22618. Add([
  22619. 'type',
  22620. ' TObject = class',
  22621. ' FSize: word;',
  22622. ' property Size: word read FSize write FSize;',
  22623. ' end;',
  22624. ' THelper = class helper for TObject',
  22625. ' function Foo(w: word = 1): word;',
  22626. ' end;',
  22627. 'function THelper.foo(w: word): word;',
  22628. 'begin',
  22629. ' Result:=Size;',
  22630. ' Size:=Size+2;',
  22631. ' Self.Size:=Self.Size+3;',
  22632. ' FSize:=FSize+4;',
  22633. ' Self.FSize:=Self.FSize+5;',
  22634. ' with Self do begin',
  22635. ' Size:=Size+6;',
  22636. ' FSize:=FSize+7;',
  22637. ' FSize:=FSize+8;',
  22638. ' end;',
  22639. 'end;',
  22640. 'begin',
  22641. '']);
  22642. ConvertProgram;
  22643. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  22644. LinesToStr([ // statements
  22645. 'rtl.createClass(this, "TObject", null, function () {',
  22646. ' this.$init = function () {',
  22647. ' this.FSize = 0;',
  22648. ' };',
  22649. ' this.$final = function () {',
  22650. ' };',
  22651. '});',
  22652. 'rtl.createHelper(this, "THelper", null, function () {',
  22653. ' this.Foo = function (w) {',
  22654. ' var Result = 0;',
  22655. ' Result = this.FSize;',
  22656. ' this.FSize = this.FSize + 2;',
  22657. ' this.FSize = this.FSize + 3;',
  22658. ' this.FSize = this.FSize + 4;',
  22659. ' this.FSize = this.FSize + 5;',
  22660. ' this.FSize = this.FSize + 6;',
  22661. ' this.FSize = this.FSize + 7;',
  22662. ' this.FSize = this.FSize + 8;',
  22663. ' return Result;',
  22664. ' };',
  22665. '});',
  22666. '']),
  22667. LinesToStr([ // $mod.$main
  22668. '']));
  22669. end;
  22670. procedure TTestModule.TestClassHelper_Method_Call;
  22671. begin
  22672. StartProgram(false);
  22673. Add([
  22674. 'type',
  22675. ' TObject = class',
  22676. ' procedure Run(w: word = 10);',
  22677. ' end;',
  22678. ' THelper = class helper for TObject',
  22679. ' function Foo(w: word = 1): word;',
  22680. ' end;',
  22681. 'procedure TObject.Run(w: word);',
  22682. 'var o: TObject;',
  22683. 'begin',
  22684. ' Foo;',
  22685. ' Foo();',
  22686. ' Foo(2);',
  22687. ' Self.Foo;',
  22688. ' Self.Foo();',
  22689. ' Self.Foo(3);',
  22690. ' with Self do begin',
  22691. ' Foo;',
  22692. ' Foo();',
  22693. ' Foo(4);',
  22694. ' end;',
  22695. ' with o do Foo(5);',
  22696. 'end;',
  22697. 'function THelper.foo(w: word): word;',
  22698. 'begin',
  22699. ' Run;',
  22700. ' Run();',
  22701. ' Run(11);',
  22702. ' Foo;',
  22703. ' Foo();',
  22704. ' Foo(12);',
  22705. ' Self.Foo;',
  22706. ' Self.Foo();',
  22707. ' Self.Foo(13);',
  22708. ' with Self do begin',
  22709. ' Foo;',
  22710. ' Foo();',
  22711. ' Foo(14);',
  22712. ' end;',
  22713. 'end;',
  22714. 'var Obj: TObject;',
  22715. 'begin',
  22716. ' obj.Foo;',
  22717. ' obj.Foo();',
  22718. ' obj.Foo(21);',
  22719. ' with obj do begin',
  22720. ' Foo;',
  22721. ' Foo();',
  22722. ' Foo(22);',
  22723. ' end;',
  22724. '']);
  22725. ConvertProgram;
  22726. CheckSource('TestClassHelper_Method_Call',
  22727. LinesToStr([ // statements
  22728. 'rtl.createClass(this, "TObject", null, function () {',
  22729. ' this.$init = function () {',
  22730. ' };',
  22731. ' this.$final = function () {',
  22732. ' };',
  22733. ' this.Run = function (w) {',
  22734. ' var o = null;',
  22735. ' $mod.THelper.Foo.call(this, 1);',
  22736. ' $mod.THelper.Foo.call(this, 1);',
  22737. ' $mod.THelper.Foo.call(this, 2);',
  22738. ' $mod.THelper.Foo.call(this, 1);',
  22739. ' $mod.THelper.Foo.call(this, 1);',
  22740. ' $mod.THelper.Foo.call(this, 3);',
  22741. ' $mod.THelper.Foo.call(this, 1);',
  22742. ' $mod.THelper.Foo.call(this, 1);',
  22743. ' $mod.THelper.Foo.call(this, 4);',
  22744. ' $mod.THelper.Foo.call(o, 5);',
  22745. ' };',
  22746. '});',
  22747. 'rtl.createHelper(this, "THelper", null, function () {',
  22748. ' this.Foo = function (w) {',
  22749. ' var Result = 0;',
  22750. ' this.Run(10);',
  22751. ' this.Run(10);',
  22752. ' this.Run(11);',
  22753. ' $mod.THelper.Foo.call(this, 1);',
  22754. ' $mod.THelper.Foo.call(this, 1);',
  22755. ' $mod.THelper.Foo.call(this, 12);',
  22756. ' $mod.THelper.Foo.call(this, 1);',
  22757. ' $mod.THelper.Foo.call(this, 1);',
  22758. ' $mod.THelper.Foo.call(this, 13);',
  22759. ' $mod.THelper.Foo.call(this, 1);',
  22760. ' $mod.THelper.Foo.call(this, 1);',
  22761. ' $mod.THelper.Foo.call(this, 14);',
  22762. ' return Result;',
  22763. ' };',
  22764. '});',
  22765. 'this.Obj = null;',
  22766. '']),
  22767. LinesToStr([ // $mod.$main
  22768. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22769. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22770. '$mod.THelper.Foo.call($mod.Obj, 21);',
  22771. 'var $with = $mod.Obj;',
  22772. '$mod.THelper.Foo.call($with, 1);',
  22773. '$mod.THelper.Foo.call($with, 1);',
  22774. '$mod.THelper.Foo.call($with, 22);',
  22775. '']));
  22776. end;
  22777. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  22778. begin
  22779. StartProgram(false);
  22780. Add([
  22781. 'type',
  22782. ' TObject = class',
  22783. ' procedure Run(w: word = 10);',
  22784. ' end;',
  22785. ' THelper = class helper for TObject',
  22786. ' function Foo(w: word = 1): word;',
  22787. ' end;',
  22788. 'procedure TObject.Run(w: word);',
  22789. ' procedure Sub(Self: TObject);',
  22790. ' begin',
  22791. ' Foo;',
  22792. ' Foo();',
  22793. ' Self.Foo;',
  22794. ' Self.Foo();',
  22795. ' with Self do begin',
  22796. ' Foo;',
  22797. ' Foo();',
  22798. ' end;',
  22799. ' end;',
  22800. 'begin',
  22801. 'end;',
  22802. 'function THelper.foo(w: word): word;',
  22803. ' procedure Sub(Self: TObject);',
  22804. ' begin',
  22805. ' Run;',
  22806. ' Run();',
  22807. ' Foo;',
  22808. ' Foo();',
  22809. ' Self.Foo;',
  22810. ' Self.Foo();',
  22811. ' with Self do begin',
  22812. ' Foo;',
  22813. ' Foo();',
  22814. ' end;',
  22815. ' end;',
  22816. 'begin',
  22817. 'end;',
  22818. 'begin',
  22819. '']);
  22820. ConvertProgram;
  22821. CheckSource('TestClassHelper_Method_Nested_Call',
  22822. LinesToStr([ // statements
  22823. 'rtl.createClass(this, "TObject", null, function () {',
  22824. ' this.$init = function () {',
  22825. ' };',
  22826. ' this.$final = function () {',
  22827. ' };',
  22828. ' this.Run = function (w) {',
  22829. ' var $Self = this;',
  22830. ' function Sub(Self) {',
  22831. ' $mod.THelper.Foo.call($Self, 1);',
  22832. ' $mod.THelper.Foo.call($Self, 1);',
  22833. ' $mod.THelper.Foo.call(Self, 1);',
  22834. ' $mod.THelper.Foo.call(Self, 1);',
  22835. ' $mod.THelper.Foo.call(Self, 1);',
  22836. ' $mod.THelper.Foo.call(Self, 1);',
  22837. ' };',
  22838. ' };',
  22839. '});',
  22840. 'rtl.createHelper(this, "THelper", null, function () {',
  22841. ' this.Foo = function (w) {',
  22842. ' var $Self = this;',
  22843. ' var Result = 0;',
  22844. ' function Sub(Self) {',
  22845. ' $Self.Run(10);',
  22846. ' $Self.Run(10);',
  22847. ' $mod.THelper.Foo.call($Self, 1);',
  22848. ' $mod.THelper.Foo.call($Self, 1);',
  22849. ' $mod.THelper.Foo.call(Self, 1);',
  22850. ' $mod.THelper.Foo.call(Self, 1);',
  22851. ' $mod.THelper.Foo.call(Self, 1);',
  22852. ' $mod.THelper.Foo.call(Self, 1);',
  22853. ' };',
  22854. ' return Result;',
  22855. ' };',
  22856. '});',
  22857. '']),
  22858. LinesToStr([ // $mod.$main
  22859. '']));
  22860. end;
  22861. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  22862. begin
  22863. StartProgram(false);
  22864. Add([
  22865. 'type',
  22866. ' TObject = class',
  22867. ' class procedure Run(w: word = 10);',
  22868. ' end;',
  22869. ' THelper = class helper for TObject',
  22870. ' class function Foo(w: word = 1): word;',
  22871. ' end;',
  22872. 'class procedure TObject.Run(w: word);',
  22873. 'begin',
  22874. ' Foo;',
  22875. ' Foo();',
  22876. ' Self.Foo;',
  22877. ' Self.Foo();',
  22878. ' with Self do begin',
  22879. ' Foo;',
  22880. ' Foo();',
  22881. ' end;',
  22882. 'end;',
  22883. 'class function THelper.foo(w: word): word;',
  22884. 'begin',
  22885. ' Run;',
  22886. ' Run();',
  22887. ' Foo;',
  22888. ' Foo();',
  22889. ' Self.Foo;',
  22890. ' Self.Foo();',
  22891. ' with Self do begin',
  22892. ' Foo;',
  22893. ' Foo();',
  22894. ' end;',
  22895. 'end;',
  22896. 'var',
  22897. ' Obj: TObject;',
  22898. 'begin',
  22899. ' obj.Foo;',
  22900. ' obj.Foo();',
  22901. ' with obj do begin',
  22902. ' Foo;',
  22903. ' Foo();',
  22904. ' end;',
  22905. ' tobject.Foo;',
  22906. ' tobject.Foo();',
  22907. ' with tobject do begin',
  22908. ' Foo;',
  22909. ' Foo();',
  22910. ' end;',
  22911. '']);
  22912. ConvertProgram;
  22913. CheckSource('TestClassHelper_ClassMethod_Call',
  22914. LinesToStr([ // statements
  22915. 'rtl.createClass(this, "TObject", null, function () {',
  22916. ' this.$init = function () {',
  22917. ' };',
  22918. ' this.$final = function () {',
  22919. ' };',
  22920. ' this.Run = function (w) {',
  22921. ' $mod.THelper.Foo.call(this, 1);',
  22922. ' $mod.THelper.Foo.call(this, 1);',
  22923. ' $mod.THelper.Foo.call(this, 1);',
  22924. ' $mod.THelper.Foo.call(this, 1);',
  22925. ' $mod.THelper.Foo.call(this, 1);',
  22926. ' $mod.THelper.Foo.call(this, 1);',
  22927. ' };',
  22928. '});',
  22929. 'rtl.createHelper(this, "THelper", null, function () {',
  22930. ' this.Foo = function (w) {',
  22931. ' var Result = 0;',
  22932. ' this.Run(10);',
  22933. ' this.Run(10);',
  22934. ' $mod.THelper.Foo.call(this, 1);',
  22935. ' $mod.THelper.Foo.call(this, 1);',
  22936. ' $mod.THelper.Foo.call(this, 1);',
  22937. ' $mod.THelper.Foo.call(this, 1);',
  22938. ' $mod.THelper.Foo.call(this, 1);',
  22939. ' $mod.THelper.Foo.call(this, 1);',
  22940. ' return Result;',
  22941. ' };',
  22942. '});',
  22943. 'this.Obj = null;',
  22944. '']),
  22945. LinesToStr([ // $mod.$main
  22946. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  22947. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  22948. 'var $with = $mod.Obj;',
  22949. '$mod.THelper.Foo.call($with.$class, 1);',
  22950. '$mod.THelper.Foo.call($with.$class, 1);',
  22951. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22952. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22953. 'var $with1 = $mod.TObject;',
  22954. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22955. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22956. '']));
  22957. end;
  22958. procedure TTestModule.TestClassHelper_ClassOf;
  22959. begin
  22960. StartProgram(false);
  22961. Add([
  22962. 'type',
  22963. ' TObject = class',
  22964. ' end;',
  22965. ' TClass = class of TObject;',
  22966. ' THelper = class helper for TObject',
  22967. ' class function Foo(w: word = 1): word;',
  22968. ' end;',
  22969. 'class function THelper.foo(w: word): word;',
  22970. 'begin',
  22971. 'end;',
  22972. 'var',
  22973. ' c: TClass;',
  22974. 'begin',
  22975. ' c.Foo;',
  22976. ' c.Foo();',
  22977. ' with c do begin',
  22978. ' Foo;',
  22979. ' Foo();',
  22980. ' end;',
  22981. '']);
  22982. ConvertProgram;
  22983. CheckSource('TestClassHelper_ClassOf',
  22984. LinesToStr([ // statements
  22985. 'rtl.createClass(this, "TObject", null, function () {',
  22986. ' this.$init = function () {',
  22987. ' };',
  22988. ' this.$final = function () {',
  22989. ' };',
  22990. '});',
  22991. 'rtl.createHelper(this, "THelper", null, function () {',
  22992. ' this.Foo = function (w) {',
  22993. ' var Result = 0;',
  22994. ' return Result;',
  22995. ' };',
  22996. '});',
  22997. 'this.c = null;',
  22998. '']),
  22999. LinesToStr([ // $mod.$main
  23000. '$mod.THelper.Foo.call($mod.c, 1);',
  23001. '$mod.THelper.Foo.call($mod.c, 1);',
  23002. 'var $with = $mod.c;',
  23003. '$mod.THelper.Foo.call($with, 1);',
  23004. '$mod.THelper.Foo.call($with, 1);',
  23005. '']));
  23006. end;
  23007. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  23008. begin
  23009. StartProgram(false);
  23010. Add([
  23011. '{$mode objfpc}',
  23012. 'type',
  23013. ' TObject = class',
  23014. ' procedure DoIt;',
  23015. ' end;',
  23016. ' THelper = class helper for TObject',
  23017. ' procedure Fly(w: word = 1);',
  23018. ' class procedure Glide(w: word = 1);',
  23019. ' class procedure Run(w: word = 1); static;',
  23020. ' end;',
  23021. ' TFly = procedure(w: word) of object;',
  23022. ' TGlide = TFly;',
  23023. ' TRun = procedure(w: word);',
  23024. 'var',
  23025. ' f: TFly;',
  23026. ' g: TGlide;',
  23027. ' r: TRun;',
  23028. 'procedure TObject.DoIt;',
  23029. 'begin',
  23030. ' f:=@fly;',
  23031. ' g:=@glide;',
  23032. ' r:=@run;',
  23033. ' f:[email protected];',
  23034. ' g:[email protected];',
  23035. ' r:[email protected];',
  23036. ' with self do begin',
  23037. ' f:=@fly;',
  23038. ' g:=@glide;',
  23039. ' r:=@run;',
  23040. ' end;',
  23041. 'end;',
  23042. 'procedure THelper.fly(w: word);',
  23043. 'begin',
  23044. ' f:=@fly;',
  23045. ' g:=@glide;',
  23046. ' r:=@run;',
  23047. 'end;',
  23048. 'class procedure THelper.glide(w: word);',
  23049. 'begin',
  23050. ' g:=@glide;',
  23051. ' r:=@run;',
  23052. 'end;',
  23053. 'class procedure THelper.run(w: word);',
  23054. 'begin',
  23055. ' g:=@glide;',
  23056. ' r:=@run;',
  23057. 'end;',
  23058. 'var',
  23059. ' Obj: TObject;',
  23060. 'begin',
  23061. ' f:[email protected];',
  23062. ' g:[email protected];',
  23063. ' r:[email protected];',
  23064. ' with obj do begin',
  23065. ' f:=@fly;',
  23066. ' g:=@glide;',
  23067. ' r:=@run;',
  23068. ' end;',
  23069. ' g:[email protected];',
  23070. ' r:[email protected];',
  23071. ' with tobject do begin',
  23072. ' g:=@glide;',
  23073. ' r:=@run;',
  23074. ' end;',
  23075. '']);
  23076. ConvertProgram;
  23077. CheckSource('TestClassHelper_MethodRefObjFPC',
  23078. LinesToStr([ // statements
  23079. 'rtl.createClass(this, "TObject", null, function () {',
  23080. ' this.$init = function () {',
  23081. ' };',
  23082. ' this.$final = function () {',
  23083. ' };',
  23084. ' this.DoIt = function () {',
  23085. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23086. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23087. ' $mod.r = $mod.THelper.Run;',
  23088. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23089. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23090. ' $mod.r = $mod.THelper.Run;',
  23091. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23092. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23093. ' $mod.r = $mod.THelper.Run;',
  23094. ' };',
  23095. '});',
  23096. 'rtl.createHelper(this, "THelper", null, function () {',
  23097. ' this.Fly = function (w) {',
  23098. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23099. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23100. ' $mod.r = $mod.THelper.Run;',
  23101. ' };',
  23102. ' this.Glide = function (w) {',
  23103. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  23104. ' $mod.r = $mod.THelper.Run;',
  23105. ' };',
  23106. ' this.Run = function (w) {',
  23107. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  23108. ' $mod.r = $mod.THelper.Run;',
  23109. ' };',
  23110. '});',
  23111. 'this.f = null;',
  23112. 'this.g = null;',
  23113. 'this.r = null;',
  23114. 'this.Obj = null;',
  23115. '']),
  23116. LinesToStr([ // $mod.$main
  23117. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  23118. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  23119. '$mod.r = $mod.THelper.Run;',
  23120. 'var $with = $mod.Obj;',
  23121. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  23122. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  23123. '$mod.r = $mod.THelper.Run;',
  23124. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  23125. '$mod.r = $mod.THelper.Run;',
  23126. 'var $with1 = $mod.TObject;',
  23127. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  23128. '$mod.r = $mod.THelper.Run;',
  23129. '']));
  23130. end;
  23131. procedure TTestModule.TestClassHelper_Constructor;
  23132. begin
  23133. StartProgram(false);
  23134. Add([
  23135. 'type',
  23136. ' TObject = class',
  23137. ' constructor Create;',
  23138. ' end;',
  23139. ' TClass = class of TObject;',
  23140. ' THelper = class helper for TObject',
  23141. ' constructor NewHlp(w: word);',
  23142. ' end;',
  23143. 'var',
  23144. ' obj: TObject;',
  23145. ' c: TClass;',
  23146. 'constructor TObject.Create;',
  23147. 'begin',
  23148. ' NewHlp(2);', // normal call
  23149. ' tobject.NewHlp(3);', // new instance
  23150. ' c.newhlp(4);', // new instance
  23151. 'end;',
  23152. 'constructor THelper.NewHlp(w: word);',
  23153. 'begin',
  23154. ' create;', // normal call
  23155. ' tobject.create;', // new instance
  23156. ' NewHlp(2);', // normal call
  23157. ' tobject.NewHlp(3);', // new instance
  23158. ' c.newhlp(4);', // new instance
  23159. 'end;',
  23160. 'begin',
  23161. ' obj.newhlp(2);', // normal call
  23162. ' with Obj do newhlp(12);', // normal call
  23163. ' tobject.newhlp(3);', // new instance
  23164. ' with tobject do newhlp(13);', // new instance
  23165. ' c.newhlp(4);', // new instance
  23166. ' with c do newhlp(14);', // new instance
  23167. '']);
  23168. ConvertProgram;
  23169. CheckSource('TestClassHelper_Constructor',
  23170. LinesToStr([ // statements
  23171. 'rtl.createClass(this, "TObject", null, function () {',
  23172. ' this.$init = function () {',
  23173. ' };',
  23174. ' this.$final = function () {',
  23175. ' };',
  23176. ' this.Create = function () {',
  23177. ' $mod.THelper.NewHlp.call(this, 2);',
  23178. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23179. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  23180. ' return this;',
  23181. ' };',
  23182. '});',
  23183. 'rtl.createHelper(this, "THelper", null, function () {',
  23184. ' this.NewHlp = function (w) {',
  23185. ' this.Create();',
  23186. ' $mod.TObject.$create("Create");',
  23187. ' $mod.THelper.NewHlp.call(this, 2);',
  23188. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23189. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  23190. ' return this;',
  23191. ' };',
  23192. '});',
  23193. 'this.obj = null;',
  23194. 'this.c = null;',
  23195. '']),
  23196. LinesToStr([ // $mod.$main
  23197. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  23198. 'var $with = $mod.obj;',
  23199. '$mod.THelper.NewHlp.call($with, 12);',
  23200. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23201. 'var $with1 = $mod.TObject;',
  23202. '$with1.$create($mod.THelper.NewHlp, [13]);',
  23203. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  23204. 'var $with2 = $mod.c;',
  23205. '$with2.$create($mod.THelper.NewHlp, [14]);',
  23206. '']));
  23207. end;
  23208. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  23209. begin
  23210. StartProgram(false);
  23211. Add([
  23212. 'type',
  23213. ' TObject = class',
  23214. ' procedure Fly;',
  23215. ' end;',
  23216. ' TObjHelper = class helper for TObject',
  23217. ' procedure Fly;',
  23218. ' end;',
  23219. ' TBird = class',
  23220. ' procedure Fly;',
  23221. ' end;',
  23222. ' TBirdHelper = class helper for TBird',
  23223. ' procedure Fly;',
  23224. ' procedure Walk(w: word);',
  23225. ' end;',
  23226. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  23227. ' procedure Fly;',
  23228. ' procedure Walk(w: word);',
  23229. ' end;',
  23230. 'procedure Tobject.fly;',
  23231. 'begin',
  23232. ' inherited;', // ignore
  23233. 'end;',
  23234. 'procedure Tobjhelper.fly;',
  23235. 'begin',
  23236. ' {@TObject_Fly}inherited;',
  23237. ' inherited {@TObject_Fly}Fly;',
  23238. 'end;',
  23239. 'procedure Tbird.fly;',
  23240. 'begin',
  23241. ' {@TObjHelper_Fly}inherited;',
  23242. ' inherited {@TObjHelper_Fly}Fly;',
  23243. 'end;',
  23244. 'procedure Tbirdhelper.fly;',
  23245. 'begin',
  23246. ' {@TBird_Fly}inherited;',
  23247. ' inherited {@TBird_Fly}Fly;',
  23248. 'end;',
  23249. 'procedure Tbirdhelper.walk(w: word);',
  23250. 'begin',
  23251. 'end;',
  23252. 'procedure teagleHelper.fly;',
  23253. 'begin',
  23254. ' {@TBird_Fly}inherited;',
  23255. ' inherited {@TBird_Fly}Fly;',
  23256. 'end;',
  23257. 'procedure teagleHelper.walk(w: word);',
  23258. 'begin',
  23259. ' {@TBirdHelper_Walk}inherited;',
  23260. ' inherited {@TBirdHelper_Walk}Walk(3);',
  23261. 'end;',
  23262. 'begin',
  23263. '']);
  23264. ConvertProgram;
  23265. CheckSource('TestClassHelper_InheritedObjFPC',
  23266. LinesToStr([ // statements
  23267. 'rtl.createClass(this, "TObject", null, function () {',
  23268. ' this.$init = function () {',
  23269. ' };',
  23270. ' this.$final = function () {',
  23271. ' };',
  23272. ' this.Fly = function () {',
  23273. ' };',
  23274. '});',
  23275. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23276. ' this.Fly = function () {',
  23277. ' $mod.TObject.Fly.call(this);',
  23278. ' $mod.TObject.Fly.call(this);',
  23279. ' };',
  23280. '});',
  23281. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23282. ' this.Fly$1 = function () {',
  23283. ' $mod.TObjHelper.Fly.call(this);',
  23284. ' $mod.TObjHelper.Fly.call(this);',
  23285. ' };',
  23286. '});',
  23287. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  23288. ' this.Fly = function () {',
  23289. ' $mod.TBird.Fly$1.call(this);',
  23290. ' $mod.TBird.Fly$1.call(this);',
  23291. ' };',
  23292. ' this.Walk = function (w) {',
  23293. ' };',
  23294. '});',
  23295. 'rtl.createHelper(this, "TEagleHelper", this.TBirdHelper, function () {',
  23296. ' this.Fly$1 = function () {',
  23297. ' $mod.TBird.Fly$1.call(this);',
  23298. ' $mod.TBird.Fly$1.call(this);',
  23299. ' };',
  23300. ' this.Walk$1 = function (w) {',
  23301. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  23302. ' $mod.TBirdHelper.Walk.call(this, 3);',
  23303. ' };',
  23304. '});',
  23305. '']),
  23306. LinesToStr([ // $mod.$main
  23307. '']));
  23308. end;
  23309. procedure TTestModule.TestClassHelper_Property;
  23310. begin
  23311. StartProgram(false);
  23312. Add([
  23313. 'type',
  23314. ' TObject = class',
  23315. ' FSize: word;',
  23316. ' function GetSpeed: word;',
  23317. ' procedure SetSpeed(Value: word);',
  23318. ' end;',
  23319. ' TObjHelper = class helper for TObject',
  23320. ' function GetLeft: word;',
  23321. ' procedure SetLeft(Value: word);',
  23322. ' property Size: word read FSize write FSize;',
  23323. ' property Speed: word read GetSpeed write SetSpeed;',
  23324. ' property Left: word read GetLeft write SetLeft;',
  23325. ' end;',
  23326. ' TBird = class',
  23327. ' property NotRight: word read GetLeft write SetLeft;',
  23328. ' procedure DoIt;',
  23329. ' end;',
  23330. 'var',
  23331. ' b: TBird;',
  23332. 'function Tobject.GetSpeed: word;',
  23333. 'begin',
  23334. ' Size:=Size+11;',
  23335. ' Speed:=Speed+12;',
  23336. ' Result:=Left+13;',
  23337. ' Left:=13;',
  23338. ' Left:=Left+13;',
  23339. ' Self.Size:=Self.Size+21;',
  23340. ' Self.Speed:=Self.Speed+22;',
  23341. ' Self.Left:=Self.Left+23;',
  23342. ' with Self do begin',
  23343. ' Size:=Size+31;',
  23344. ' Speed:=Speed+32;',
  23345. ' Left:=Left+33;',
  23346. ' end;',
  23347. 'end;',
  23348. 'procedure Tobject.SetSpeed(Value: word);',
  23349. 'begin',
  23350. 'end;',
  23351. 'function TObjHelper.GetLeft: word;',
  23352. 'begin',
  23353. ' Size:=Size+11;',
  23354. ' Speed:=Speed+12;',
  23355. ' Left:=Left+13;',
  23356. ' Self.Size:=Self.Size+21;',
  23357. ' Self.Speed:=Self.Speed+22;',
  23358. ' Self.Left:=Self.Left+23;',
  23359. ' with Self do begin',
  23360. ' Size:=Size+31;',
  23361. ' Speed:=Speed+32;',
  23362. ' Left:=Left+33;',
  23363. ' end;',
  23364. 'end;',
  23365. 'procedure TObjHelper.SetLeft(Value: word);',
  23366. 'begin',
  23367. 'end;',
  23368. 'procedure TBird.DoIt;',
  23369. 'begin',
  23370. ' NotRight:=NotRight+11;',
  23371. ' Self.NotRight:=Self.NotRight+21;',
  23372. ' with Self do begin',
  23373. ' NotRight:=NotRight+31;',
  23374. ' end;',
  23375. 'end;',
  23376. 'begin',
  23377. ' b.Size:=b.Size+11;',
  23378. ' b.Speed:=b.Speed+12;',
  23379. ' b.Left:=b.Left+13;',
  23380. ' b.NotRight:=b.NotRight+14;',
  23381. ' with b do begin',
  23382. ' Size:=Size+31;',
  23383. ' Speed:=Speed+32;',
  23384. ' Left:=Left+33;',
  23385. ' NotRight:=NotRight+34;',
  23386. ' end;',
  23387. '']);
  23388. ConvertProgram;
  23389. CheckSource('TestClassHelper_Property',
  23390. LinesToStr([ // statements
  23391. 'rtl.createClass(this, "TObject", null, function () {',
  23392. ' this.$init = function () {',
  23393. ' this.FSize = 0;',
  23394. ' };',
  23395. ' this.$final = function () {',
  23396. ' };',
  23397. ' this.GetSpeed = function () {',
  23398. ' var Result = 0;',
  23399. ' this.FSize = this.FSize + 11;',
  23400. ' this.SetSpeed(this.GetSpeed() + 12);',
  23401. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  23402. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  23403. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23404. ' this.FSize = this.FSize + 21;',
  23405. ' this.SetSpeed(this.GetSpeed() + 22);',
  23406. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23407. ' this.FSize = this.FSize + 31;',
  23408. ' this.SetSpeed(this.GetSpeed() + 32);',
  23409. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23410. ' return Result;',
  23411. ' };',
  23412. ' this.SetSpeed = function (Value) {',
  23413. ' };',
  23414. '});',
  23415. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23416. ' this.GetLeft = function () {',
  23417. ' var Result = 0;',
  23418. ' this.FSize = this.FSize + 11;',
  23419. ' this.SetSpeed(this.GetSpeed() + 12);',
  23420. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23421. ' this.FSize = this.FSize + 21;',
  23422. ' this.SetSpeed(this.GetSpeed() + 22);',
  23423. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23424. ' this.FSize = this.FSize + 31;',
  23425. ' this.SetSpeed(this.GetSpeed() + 32);',
  23426. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23427. ' return Result;',
  23428. ' };',
  23429. ' this.SetLeft = function (Value) {',
  23430. ' };',
  23431. '});',
  23432. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23433. ' this.DoIt = function () {',
  23434. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  23435. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  23436. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  23437. ' };',
  23438. '});',
  23439. 'this.b = null;',
  23440. '']),
  23441. LinesToStr([ // $mod.$main
  23442. '$mod.b.FSize = $mod.b.FSize + 11;',
  23443. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  23444. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  23445. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  23446. 'var $with = $mod.b;',
  23447. '$with.FSize = $with.FSize + 31;',
  23448. '$with.SetSpeed($with.GetSpeed() + 32);',
  23449. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  23450. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  23451. '']));
  23452. end;
  23453. procedure TTestModule.TestClassHelper_Property_Array;
  23454. begin
  23455. StartProgram(false);
  23456. Add([
  23457. 'type',
  23458. ' TObject = class',
  23459. ' function GetSpeed(Index: boolean): word;',
  23460. ' procedure SetSpeed(Index: boolean; Value: word);',
  23461. ' end;',
  23462. ' TObjHelper = class helper for TObject',
  23463. ' function GetSize(Index: boolean): word;',
  23464. ' procedure SetSize(Index: boolean; Value: word);',
  23465. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  23466. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  23467. ' end;',
  23468. ' TBird = class',
  23469. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  23470. ' procedure DoIt;',
  23471. ' end;',
  23472. 'var',
  23473. ' b: TBird;',
  23474. 'function Tobject.GetSpeed(Index: boolean): word;',
  23475. 'begin',
  23476. ' Result:=Size[false];',
  23477. ' Size[true]:=Size[false]+11;',
  23478. ' Speed[true]:=Speed[false]+12;',
  23479. ' Self.Size[true]:=Self.Size[false]+21;',
  23480. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23481. ' with Self do begin',
  23482. ' Size[true]:=Size[false]+31;',
  23483. ' Speed[true]:=Speed[false]+32;',
  23484. ' end;',
  23485. 'end;',
  23486. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  23487. 'begin',
  23488. 'end;',
  23489. 'function TObjHelper.GetSize(Index: boolean): word;',
  23490. 'begin',
  23491. ' Size[true]:=Size[false]+11;',
  23492. ' Speed[true]:=Speed[false]+12;',
  23493. ' Self.Size[true]:=Self.Size[false]+21;',
  23494. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23495. ' with Self do begin',
  23496. ' Size[true]:=Size[false]+31;',
  23497. ' Speed[true]:=Speed[false]+32;',
  23498. ' end;',
  23499. 'end;',
  23500. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  23501. 'begin',
  23502. 'end;',
  23503. 'procedure TBird.DoIt;',
  23504. 'begin',
  23505. ' Items[true]:=Items[false]+11;',
  23506. ' Self.Items[true]:=Self.Items[false]+21;',
  23507. ' with Self do Items[true]:=Items[false]+31;',
  23508. 'end;',
  23509. 'begin',
  23510. ' b.Size[true]:=b.Size[false]+11;',
  23511. ' b.Speed[true]:=b.Speed[false]+12;',
  23512. ' b.Items[true]:=b.Items[false]+13;',
  23513. ' with b do begin',
  23514. ' Size[true]:=Size[false]+21;',
  23515. ' Speed[true]:=Speed[false]+22;',
  23516. ' Items[true]:=Items[false]+23;',
  23517. ' end;',
  23518. '']);
  23519. ConvertProgram;
  23520. CheckSource('TestClassHelper_Property_Array',
  23521. LinesToStr([ // statements
  23522. 'rtl.createClass(this, "TObject", null, function () {',
  23523. ' this.$init = function () {',
  23524. ' };',
  23525. ' this.$final = function () {',
  23526. ' };',
  23527. ' this.GetSpeed = function (Index) {',
  23528. ' var Result = 0;',
  23529. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  23530. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23531. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  23532. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23533. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  23534. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23535. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  23536. ' return Result;',
  23537. ' };',
  23538. ' this.SetSpeed = function (Index, Value) {',
  23539. ' };',
  23540. '});',
  23541. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23542. ' this.GetSize = function (Index) {',
  23543. ' var Result = 0;',
  23544. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23545. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  23546. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23547. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  23548. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23549. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  23550. ' return Result;',
  23551. ' };',
  23552. ' this.SetSize = function (Index, Value) {',
  23553. ' };',
  23554. '});',
  23555. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23556. ' this.DoIt = function () {',
  23557. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23558. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23559. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23560. ' };',
  23561. '});',
  23562. 'this.b = null;',
  23563. '']),
  23564. LinesToStr([ // $mod.$main
  23565. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  23566. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  23567. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  23568. 'var $with = $mod.b;',
  23569. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  23570. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  23571. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  23572. '']));
  23573. end;
  23574. procedure TTestModule.TestClassHelper_Property_Array_Default;
  23575. begin
  23576. StartProgram(false);
  23577. Add([
  23578. 'type',
  23579. ' TObject = class',
  23580. ' function GetSpeed(Index: boolean): word;',
  23581. ' procedure SetSpeed(Index: boolean; Value: word);',
  23582. ' end;',
  23583. ' TObjHelper = class helper for TObject',
  23584. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  23585. ' end;',
  23586. ' TBird = class',
  23587. ' end;',
  23588. ' TBirdHelper = class helper for TBird',
  23589. ' function GetSize(Index: word): boolean;',
  23590. ' procedure SetSize(Index: word; Value: boolean);',
  23591. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  23592. ' end;',
  23593. 'function Tobject.GetSpeed(Index: boolean): word;',
  23594. 'begin',
  23595. ' Self[true]:=Self[false]+1;',
  23596. 'end;',
  23597. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  23598. 'begin',
  23599. 'end;',
  23600. 'function TBirdHelper.GetSize(Index: word): boolean;',
  23601. 'begin',
  23602. ' Self[1]:=not Self[2];',
  23603. 'end;',
  23604. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  23605. 'begin',
  23606. 'end;',
  23607. 'var',
  23608. ' o: TObject;',
  23609. ' b: TBird;',
  23610. 'begin',
  23611. ' o[true]:=o[false]+1;',
  23612. ' b[3]:=not b[4];',
  23613. '']);
  23614. ConvertProgram;
  23615. CheckSource('TestClassHelper_Property_Array_Default',
  23616. LinesToStr([ // statements
  23617. 'rtl.createClass(this, "TObject", null, function () {',
  23618. ' this.$init = function () {',
  23619. ' };',
  23620. ' this.$final = function () {',
  23621. ' };',
  23622. ' this.GetSpeed = function (Index) {',
  23623. ' var Result = 0;',
  23624. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  23625. ' return Result;',
  23626. ' };',
  23627. ' this.SetSpeed = function (Index, Value) {',
  23628. ' };',
  23629. '});',
  23630. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23631. '});',
  23632. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23633. '});',
  23634. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  23635. ' this.GetSize = function (Index) {',
  23636. ' var Result = false;',
  23637. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  23638. ' return Result;',
  23639. ' };',
  23640. ' this.SetSize = function (Index, Value) {',
  23641. ' };',
  23642. '});',
  23643. 'this.o = null;',
  23644. 'this.b = null;',
  23645. '']),
  23646. LinesToStr([ // $mod.$main
  23647. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  23648. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  23649. '']));
  23650. end;
  23651. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  23652. begin
  23653. StartProgram(false);
  23654. Add([
  23655. 'type',
  23656. ' TObject = class',
  23657. ' end;',
  23658. ' TObjHelper = class helper for TObject',
  23659. ' function GetItems(Index: word): TObject;',
  23660. ' procedure SetItems(Index: word; Value: TObject);',
  23661. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  23662. ' end;',
  23663. 'function Tobjhelper.GetItems(Index: word): TObject;',
  23664. 'begin',
  23665. ' Self[1][2]:=Self[3][4];',
  23666. 'end;',
  23667. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  23668. 'begin',
  23669. 'end;',
  23670. 'var',
  23671. ' o: TObject;',
  23672. 'begin',
  23673. ' o[1][2]:=o[3][4];',
  23674. '']);
  23675. ConvertProgram;
  23676. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  23677. LinesToStr([ // statements
  23678. 'rtl.createClass(this, "TObject", null, function () {',
  23679. ' this.$init = function () {',
  23680. ' };',
  23681. ' this.$final = function () {',
  23682. ' };',
  23683. '});',
  23684. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23685. ' this.GetItems = function (Index) {',
  23686. ' var Result = null;',
  23687. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  23688. ' return Result;',
  23689. ' };',
  23690. ' this.SetItems = function (Index, Value) {',
  23691. ' };',
  23692. '});',
  23693. 'this.o = null;',
  23694. '']),
  23695. LinesToStr([ // $mod.$main
  23696. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  23697. '']));
  23698. end;
  23699. procedure TTestModule.TestClassHelper_ClassProperty;
  23700. begin
  23701. StartProgram(false);
  23702. Add([
  23703. 'type',
  23704. ' TObject = class',
  23705. ' class var FSize: word;',
  23706. ' class function GetSpeed: word;',
  23707. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  23708. ' end;',
  23709. ' TObjHelper = class helper for TObject',
  23710. ' class function GetLeft: word;',
  23711. ' class procedure SetLeft(Value: word);',
  23712. ' class property Size: word read FSize write FSize;',
  23713. ' class property Speed: word read GetSpeed write SetSpeed;',
  23714. ' class property Left: word read GetLeft write SetLeft;',
  23715. ' end;',
  23716. ' TBird = class',
  23717. ' class property NotRight: word read GetLeft write SetLeft;',
  23718. ' class procedure DoIt;',
  23719. ' end;',
  23720. ' TBirdClass = class of TBird;',
  23721. 'class function Tobject.GetSpeed: word;',
  23722. 'begin',
  23723. ' Size:=Size+11;',
  23724. ' Speed:=Speed+12;',
  23725. ' Left:=Left+13;',
  23726. ' Self.Size:=Self.Size+21;',
  23727. ' Self.Speed:=Self.Speed+22;',
  23728. ' Self.Left:=Self.Left+23;',
  23729. ' with Self do begin',
  23730. ' Size:=Size+31;',
  23731. ' Speed:=Speed+32;',
  23732. ' Left:=Left+33;',
  23733. ' end;',
  23734. 'end;',
  23735. 'class function TObjHelper.GetLeft: word;',
  23736. 'begin',
  23737. ' Size:=Size+11;',
  23738. ' Speed:=Speed+12;',
  23739. ' Left:=Left+13;',
  23740. ' Self.Size:=Self.Size+21;',
  23741. ' Self.Speed:=Self.Speed+22;',
  23742. ' Self.Left:=Self.Left+23;',
  23743. ' with Self do begin',
  23744. ' Size:=Size+31;',
  23745. ' Speed:=Speed+32;',
  23746. ' Left:=Left+33;',
  23747. ' end;',
  23748. 'end;',
  23749. 'class procedure TObjHelper.SetLeft(Value: word);',
  23750. 'begin',
  23751. 'end;',
  23752. 'class procedure TBird.DoIt;',
  23753. 'begin',
  23754. ' NotRight:=NotRight+11;',
  23755. ' Self.NotRight:=Self.NotRight+21;',
  23756. ' with Self do NotRight:=NotRight+31;',
  23757. 'end;',
  23758. 'var',
  23759. ' b: TBird;',
  23760. ' c: TBirdClass;',
  23761. 'begin',
  23762. ' b.Size:=b.Size+11;',
  23763. ' b.Speed:=b.Speed+12;',
  23764. ' b.Left:=b.Left+13;',
  23765. ' b.NotRight:=b.NotRight+14;',
  23766. ' with b do begin',
  23767. ' Size:=Size+31;',
  23768. ' Speed:=Speed+32;',
  23769. ' Left:=Left+33;',
  23770. ' NotRight:=NotRight+34;',
  23771. ' end;',
  23772. ' c.Size:=c.Size+11;',
  23773. ' c.Speed:=c.Speed+12;',
  23774. ' c.Left:=c.Left+13;',
  23775. ' c.NotRight:=c.NotRight+14;',
  23776. ' with c do begin',
  23777. ' Size:=Size+31;',
  23778. ' Speed:=Speed+32;',
  23779. ' Left:=Left+33;',
  23780. ' NotRight:=NotRight+34;',
  23781. ' end;',
  23782. ' tbird.Size:=tbird.Size+11;',
  23783. ' tbird.Speed:=tbird.Speed+12;',
  23784. ' tbird.Left:=tbird.Left+13;',
  23785. ' tbird.NotRight:=tbird.NotRight+14;',
  23786. ' with tbird do begin',
  23787. ' Size:=Size+31;',
  23788. ' Speed:=Speed+32;',
  23789. ' Left:=Left+33;',
  23790. ' NotRight:=NotRight+34;',
  23791. ' end;',
  23792. '']);
  23793. ConvertProgram;
  23794. CheckSource('TestClassHelper_ClassProperty',
  23795. LinesToStr([ // statements
  23796. 'rtl.createClass(this, "TObject", null, function () {',
  23797. ' this.FSize = 0;',
  23798. ' this.$init = function () {',
  23799. ' };',
  23800. ' this.$final = function () {',
  23801. ' };',
  23802. ' this.GetSpeed = function () {',
  23803. ' var Result = 0;',
  23804. ' $mod.TObject.FSize = this.FSize + 11;',
  23805. ' this.SetSpeed(this.GetSpeed() + 12);',
  23806. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23807. ' $mod.TObject.FSize = this.FSize + 21;',
  23808. ' this.SetSpeed(this.GetSpeed() + 22);',
  23809. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23810. ' $mod.TObject.FSize = this.FSize + 31;',
  23811. ' this.SetSpeed(this.GetSpeed() + 32);',
  23812. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23813. ' return Result;',
  23814. ' };',
  23815. '});',
  23816. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23817. ' this.GetLeft = function () {',
  23818. ' var Result = 0;',
  23819. ' $mod.TObject.FSize = this.FSize + 11;',
  23820. ' this.SetSpeed(this.GetSpeed() + 12);',
  23821. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23822. ' $mod.TObject.FSize = this.FSize + 21;',
  23823. ' this.SetSpeed(this.GetSpeed() + 22);',
  23824. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23825. ' $mod.TObject.FSize = this.FSize + 31;',
  23826. ' this.SetSpeed(this.GetSpeed() + 32);',
  23827. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23828. ' return Result;',
  23829. ' };',
  23830. ' this.SetLeft = function (Value) {',
  23831. ' };',
  23832. '});',
  23833. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23834. ' this.DoIt = function () {',
  23835. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  23836. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  23837. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  23838. ' };',
  23839. '});',
  23840. 'this.b = null;',
  23841. 'this.c = null;',
  23842. '']),
  23843. LinesToStr([ // $mod.$main
  23844. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  23845. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  23846. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  23847. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  23848. 'var $with = $mod.b;',
  23849. '$mod.TObject.FSize = $with.FSize + 31;',
  23850. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  23851. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  23852. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  23853. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  23854. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  23855. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  23856. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  23857. 'var $with1 = $mod.c;',
  23858. '$mod.TObject.FSize = $with1.FSize + 31;',
  23859. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  23860. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  23861. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  23862. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  23863. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  23864. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  23865. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  23866. 'var $with2 = $mod.TBird;',
  23867. '$mod.TObject.FSize = $with2.FSize + 31;',
  23868. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  23869. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  23870. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  23871. '']));
  23872. end;
  23873. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  23874. begin
  23875. StartProgram(false);
  23876. Add([
  23877. 'type',
  23878. ' TObject = class',
  23879. ' class function GetSpeed: word; static;',
  23880. ' class procedure SetSpeed(Value: word); static;',
  23881. ' end;',
  23882. ' TObjHelper = class helper for TObject',
  23883. ' class function GetLeft: word; static;',
  23884. ' class procedure SetLeft(Value: word); static;',
  23885. ' class property Speed: word read GetSpeed write SetSpeed;',
  23886. ' class property Left: word read GetLeft write SetLeft;',
  23887. ' end;',
  23888. ' TBird = class',
  23889. ' class property NotRight: word read GetLeft write SetLeft;',
  23890. ' class procedure DoIt; static;',
  23891. ' class procedure DoSome;',
  23892. ' end;',
  23893. ' TBirdClass = class of TBird;',
  23894. 'class function Tobject.GetSpeed: word;',
  23895. 'begin',
  23896. ' Speed:=Speed+12;',
  23897. ' Left:=Left+13;',
  23898. 'end;',
  23899. 'class procedure TObject.SetSpeed(Value: word);',
  23900. 'begin',
  23901. 'end;',
  23902. 'class function TObjHelper.GetLeft: word;',
  23903. 'begin',
  23904. ' Speed:=Speed+12;',
  23905. ' Left:=Left+13;',
  23906. 'end;',
  23907. 'class procedure TObjHelper.SetLeft(Value: word);',
  23908. 'begin',
  23909. 'end;',
  23910. 'class procedure TBird.DoIt;',
  23911. 'begin',
  23912. ' NotRight:=NotRight+11;',
  23913. 'end;',
  23914. 'class procedure TBird.DoSome;',
  23915. 'begin',
  23916. ' Speed:=Speed+12;',
  23917. ' Left:=Left+13;',
  23918. ' Self.Speed:=Self.Speed+22;',
  23919. ' Self.Left:=Self.Left+23;',
  23920. ' with Self do begin',
  23921. ' Speed:=Speed+32;',
  23922. ' Left:=Left+33;',
  23923. ' end;',
  23924. ' NotRight:=NotRight+11;',
  23925. ' Self.NotRight:=Self.NotRight+21;',
  23926. ' with Self do NotRight:=NotRight+31;',
  23927. 'end;',
  23928. 'var',
  23929. ' b: TBird;',
  23930. ' c: TBirdClass;',
  23931. 'begin',
  23932. ' b.Speed:=b.Speed+12;',
  23933. ' b.Left:=b.Left+13;',
  23934. ' b.NotRight:=b.NotRight+14;',
  23935. ' with b do begin',
  23936. ' Speed:=Speed+32;',
  23937. ' Left:=Left+33;',
  23938. ' NotRight:=NotRight+34;',
  23939. ' end;',
  23940. ' c.Speed:=c.Speed+12;',
  23941. ' c.Left:=c.Left+13;',
  23942. ' c.NotRight:=c.NotRight+14;',
  23943. ' with c do begin',
  23944. ' Speed:=Speed+32;',
  23945. ' Left:=Left+33;',
  23946. ' NotRight:=NotRight+34;',
  23947. ' end;',
  23948. ' tbird.Speed:=tbird.Speed+12;',
  23949. ' tbird.Left:=tbird.Left+13;',
  23950. ' tbird.NotRight:=tbird.NotRight+14;',
  23951. ' with tbird do begin',
  23952. ' Speed:=Speed+32;',
  23953. ' Left:=Left+33;',
  23954. ' NotRight:=NotRight+34;',
  23955. ' end;',
  23956. '']);
  23957. ConvertProgram;
  23958. CheckSource('TestClassHelper_ClassPropertyStatic',
  23959. LinesToStr([ // statements
  23960. 'rtl.createClass(this, "TObject", null, function () {',
  23961. ' this.$init = function () {',
  23962. ' };',
  23963. ' this.$final = function () {',
  23964. ' };',
  23965. ' this.GetSpeed = function () {',
  23966. ' var Result = 0;',
  23967. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23968. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23969. ' return Result;',
  23970. ' };',
  23971. ' this.SetSpeed = function (Value) {',
  23972. ' };',
  23973. '});',
  23974. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23975. ' this.GetLeft = function () {',
  23976. ' var Result = 0;',
  23977. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23978. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23979. ' return Result;',
  23980. ' };',
  23981. ' this.SetLeft = function (Value) {',
  23982. ' };',
  23983. '});',
  23984. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23985. ' this.DoIt = function () {',
  23986. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  23987. ' };',
  23988. ' this.DoSome = function () {',
  23989. ' this.SetSpeed(this.GetSpeed() + 12);',
  23990. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23991. ' this.SetSpeed(this.GetSpeed() + 22);',
  23992. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  23993. ' this.SetSpeed(this.GetSpeed() + 32);',
  23994. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23995. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  23996. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  23997. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  23998. ' };',
  23999. '});',
  24000. 'this.b = null;',
  24001. 'this.c = null;',
  24002. '']),
  24003. LinesToStr([ // $mod.$main
  24004. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  24005. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  24006. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  24007. 'var $with = $mod.b;',
  24008. '$with.SetSpeed($with.GetSpeed() + 32);',
  24009. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  24010. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  24011. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  24012. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  24013. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  24014. 'var $with1 = $mod.c;',
  24015. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  24016. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  24017. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  24018. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  24019. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  24020. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  24021. 'var $with2 = $mod.TBird;',
  24022. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  24023. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  24024. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  24025. '']));
  24026. end;
  24027. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  24028. begin
  24029. StartProgram(false);
  24030. Add([
  24031. 'type',
  24032. ' TObject = class',
  24033. ' class function GetSpeed(Index: boolean): word;',
  24034. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  24035. ' end;',
  24036. ' TObjHelper = class helper for TObject',
  24037. ' class function GetSize(Index: boolean): word;',
  24038. ' class procedure SetSize(Index: boolean; Value: word);',
  24039. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  24040. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  24041. ' end;',
  24042. ' TBird = class',
  24043. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  24044. ' class procedure DoIt;',
  24045. ' end;',
  24046. ' TBirdClass = class of TBird;',
  24047. 'class function Tobject.GetSpeed(Index: boolean): word;',
  24048. 'begin',
  24049. ' Size[true]:=Size[false]+11;',
  24050. ' Speed[true]:=Speed[false]+12;',
  24051. ' Self.Size[true]:=Self.Size[false]+21;',
  24052. ' Self.Speed[true]:=Self.Speed[false]+22;',
  24053. ' with Self do begin',
  24054. ' Size[true]:=Size[false]+31;',
  24055. ' Speed[true]:=Speed[false]+32;',
  24056. ' end;',
  24057. 'end;',
  24058. 'class function TObjHelper.GetSize(Index: boolean): word;',
  24059. 'begin',
  24060. ' Size[true]:=Size[false]+11;',
  24061. ' Speed[true]:=Speed[false]+12;',
  24062. ' Self.Size[true]:=Self.Size[false]+21;',
  24063. ' Self.Speed[true]:=Self.Speed[false]+22;',
  24064. ' with Self do begin',
  24065. ' Size[true]:=Size[false]+31;',
  24066. ' Speed[true]:=Speed[false]+32;',
  24067. ' end;',
  24068. 'end;',
  24069. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  24070. 'begin',
  24071. 'end;',
  24072. 'class procedure TBird.DoIt;',
  24073. 'begin',
  24074. ' Items[true]:=Items[false]+11;',
  24075. ' Self.Items[true]:=Self.Items[false]+21;',
  24076. ' with Self do Items[true]:=Items[false]+31;',
  24077. 'end;',
  24078. 'var',
  24079. ' b: TBird;',
  24080. ' c: TBirdClass;',
  24081. 'begin',
  24082. ' b.Size[true]:=b.Size[false]+11;',
  24083. ' b.Speed[true]:=b.Speed[false]+12;',
  24084. ' b.Items[true]:=b.Items[false]+13;',
  24085. ' with b do begin',
  24086. ' Size[true]:=Size[false]+21;',
  24087. ' Speed[true]:=Speed[false]+22;',
  24088. ' Items[true]:=Items[false]+23;',
  24089. ' end;',
  24090. ' c.Size[true]:=c.Size[false]+11;',
  24091. ' c.Speed[true]:=c.Speed[false]+12;',
  24092. ' c.Items[true]:=c.Items[false]+13;',
  24093. ' with c do begin',
  24094. ' Size[true]:=Size[false]+21;',
  24095. ' Speed[true]:=Speed[false]+22;',
  24096. ' Items[true]:=Items[false]+23;',
  24097. ' end;',
  24098. ' TBird.Size[true]:=TBird.Size[false]+11;',
  24099. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  24100. ' TBird.Items[true]:=TBird.Items[false]+13;',
  24101. ' with TBird do begin',
  24102. ' Size[true]:=Size[false]+21;',
  24103. ' Speed[true]:=Speed[false]+22;',
  24104. ' Items[true]:=Items[false]+23;',
  24105. ' end;',
  24106. '']);
  24107. ConvertProgram;
  24108. CheckSource('TestClassHelper_ClassProperty_Array',
  24109. LinesToStr([ // statements
  24110. 'rtl.createClass(this, "TObject", null, function () {',
  24111. ' this.$init = function () {',
  24112. ' };',
  24113. ' this.$final = function () {',
  24114. ' };',
  24115. ' this.GetSpeed = function (Index) {',
  24116. ' var Result = 0;',
  24117. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24118. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  24119. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24120. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  24121. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24122. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  24123. ' return Result;',
  24124. ' };',
  24125. '});',
  24126. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  24127. ' this.GetSize = function (Index) {',
  24128. ' var Result = 0;',
  24129. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24130. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  24131. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24132. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  24133. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24134. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  24135. ' return Result;',
  24136. ' };',
  24137. ' this.SetSize = function (Index, Value) {',
  24138. ' };',
  24139. '});',
  24140. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24141. ' this.DoIt = function () {',
  24142. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24143. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24144. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24145. ' };',
  24146. '});',
  24147. 'this.b = null;',
  24148. 'this.c = null;',
  24149. '']),
  24150. LinesToStr([ // $mod.$main
  24151. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  24152. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  24153. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  24154. 'var $with = $mod.b;',
  24155. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  24156. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  24157. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  24158. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  24159. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  24160. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  24161. 'var $with1 = $mod.c;',
  24162. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  24163. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  24164. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  24165. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  24166. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  24167. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  24168. 'var $with2 = $mod.TBird;',
  24169. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  24170. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  24171. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  24172. '']));
  24173. end;
  24174. procedure TTestModule.TestClassHelper_ForIn;
  24175. begin
  24176. StartProgram(false);
  24177. Add([
  24178. 'type',
  24179. ' TObject = class end;',
  24180. ' TItem = TObject;',
  24181. ' TEnumerator = class',
  24182. ' FCurrent: TItem;',
  24183. ' property Current: TItem read FCurrent;',
  24184. ' function MoveNext: boolean;',
  24185. ' end;',
  24186. ' TBird = class',
  24187. ' end;',
  24188. ' TBirdHelper = class helper for TBird',
  24189. ' function GetEnumerator: TEnumerator;',
  24190. ' end;',
  24191. 'function TEnumerator.MoveNext: boolean;',
  24192. 'begin',
  24193. 'end;',
  24194. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  24195. 'begin',
  24196. 'end;',
  24197. 'var',
  24198. ' b: TBird;',
  24199. ' i, i2: TItem;',
  24200. 'begin',
  24201. ' for i in b do i2:=i;']);
  24202. ConvertProgram;
  24203. CheckSource('TestClassHelper_ForIn',
  24204. LinesToStr([ // statements
  24205. 'rtl.createClass(this, "TObject", null, function () {',
  24206. ' this.$init = function () {',
  24207. ' };',
  24208. ' this.$final = function () {',
  24209. ' };',
  24210. '});',
  24211. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  24212. ' this.$init = function () {',
  24213. ' $mod.TObject.$init.call(this);',
  24214. ' this.FCurrent = null;',
  24215. ' };',
  24216. ' this.$final = function () {',
  24217. ' this.FCurrent = undefined;',
  24218. ' $mod.TObject.$final.call(this);',
  24219. ' };',
  24220. ' this.MoveNext = function () {',
  24221. ' var Result = false;',
  24222. ' return Result;',
  24223. ' };',
  24224. '});',
  24225. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24226. '});',
  24227. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  24228. ' this.GetEnumerator = function () {',
  24229. ' var Result = null;',
  24230. ' return Result;',
  24231. ' };',
  24232. '});',
  24233. 'this.b = null;',
  24234. 'this.i = null;',
  24235. 'this.i2 = null;'
  24236. ]),
  24237. LinesToStr([ // $mod.$main
  24238. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  24239. 'try {',
  24240. ' while ($in.MoveNext()){',
  24241. ' $mod.i = $in.FCurrent;',
  24242. ' $mod.i2 = $mod.i;',
  24243. ' }',
  24244. '} finally {',
  24245. ' $in = rtl.freeLoc($in)',
  24246. '};',
  24247. '']));
  24248. end;
  24249. procedure TTestModule.TestClassHelper_PassProperty;
  24250. begin
  24251. StartProgram(false);
  24252. Add([
  24253. 'type',
  24254. ' TObject = class',
  24255. ' FField: TObject;',
  24256. ' property Field: TObject read FField write FField;',
  24257. ' end;',
  24258. ' THelper = class helper for TObject',
  24259. ' procedure Fly;',
  24260. ' class procedure Run;',
  24261. ' class procedure Jump; static;',
  24262. ' end;',
  24263. 'procedure THelper.Fly;',
  24264. 'begin',
  24265. ' Field.Fly;',
  24266. ' Field.Run;',
  24267. ' Field.Jump;',
  24268. ' with Field do begin',
  24269. ' Fly;',
  24270. ' Run;',
  24271. ' Jump;',
  24272. ' end;',
  24273. 'end;',
  24274. 'class procedure THelper.Run;',
  24275. 'begin',
  24276. 'end;',
  24277. 'class procedure THelper.Jump;',
  24278. 'begin',
  24279. 'end;',
  24280. 'var',
  24281. ' b: TObject;',
  24282. 'begin',
  24283. ' b.Field.Fly;',
  24284. ' b.Field.Run;',
  24285. ' b.Field.Jump;',
  24286. ' with b do begin',
  24287. ' Field.Run;',
  24288. ' Field.Fly;',
  24289. ' Field.Jump;',
  24290. ' end;',
  24291. ' with b.Field do begin',
  24292. ' Run;',
  24293. ' Fly;',
  24294. ' Jump;',
  24295. ' end;',
  24296. '']);
  24297. ConvertProgram;
  24298. CheckSource('TestClassHelper_PassProperty',
  24299. LinesToStr([ // statements
  24300. 'rtl.createClass(this, "TObject", null, function () {',
  24301. ' this.$init = function () {',
  24302. ' this.FField = null;',
  24303. ' };',
  24304. ' this.$final = function () {',
  24305. ' this.FField = undefined;',
  24306. ' };',
  24307. '});',
  24308. 'rtl.createHelper(this, "THelper", null, function () {',
  24309. ' this.Fly = function () {',
  24310. ' $mod.THelper.Fly.call(this.FField);',
  24311. ' $mod.THelper.Run.call(this.FField.$class);',
  24312. ' $mod.THelper.Jump();',
  24313. ' var $with = this.FField;',
  24314. ' $mod.THelper.Fly.call($with);',
  24315. ' $mod.THelper.Run.call($with.$class);',
  24316. ' $mod.THelper.Jump();',
  24317. ' };',
  24318. ' this.Run = function () {',
  24319. ' };',
  24320. ' this.Jump = function () {',
  24321. ' };',
  24322. '});',
  24323. 'this.b = null;',
  24324. '']),
  24325. LinesToStr([ // $mod.$main
  24326. '$mod.THelper.Fly.call($mod.b.FField);',
  24327. '$mod.THelper.Run.call($mod.b.FField.$class);',
  24328. '$mod.THelper.Jump();',
  24329. 'var $with = $mod.b;',
  24330. '$mod.THelper.Run.call($with.FField.$class);',
  24331. '$mod.THelper.Fly.call($with.FField);',
  24332. '$mod.THelper.Jump();',
  24333. 'var $with1 = $mod.b.FField;',
  24334. '$mod.THelper.Run.call($with1.$class);',
  24335. '$mod.THelper.Fly.call($with1);',
  24336. '$mod.THelper.Jump();',
  24337. '']));
  24338. end;
  24339. procedure TTestModule.TestExtClassHelper_ClassVar;
  24340. begin
  24341. StartProgram(false);
  24342. Add([
  24343. '{$modeswitch externalclass}',
  24344. 'type',
  24345. ' TExtA = class external name ''ExtObj''',
  24346. ' end;',
  24347. ' THelper = class helper for TExtA',
  24348. ' const',
  24349. ' One = 1;',
  24350. ' Two: word = 2;',
  24351. ' class var',
  24352. ' Glob: word;',
  24353. ' function Foo(w: word): word;',
  24354. ' class function Bar(w: word): word; static;',
  24355. ' end;',
  24356. 'function THelper.foo(w: word): word;',
  24357. 'begin',
  24358. ' Result:=w;',
  24359. ' Two:=One+w;',
  24360. ' Glob:=Glob;',
  24361. ' Result:=Self.Glob;',
  24362. ' Self.Glob:=Self.Glob;',
  24363. ' with Self do Glob:=Glob;',
  24364. 'end;',
  24365. 'class function THelper.bar(w: word): word;',
  24366. 'begin',
  24367. ' Result:=w;',
  24368. ' Two:=One;',
  24369. ' Glob:=Glob;',
  24370. 'end;',
  24371. 'var o: TExtA;',
  24372. 'begin',
  24373. ' texta.two:=texta.one;',
  24374. ' texta.Glob:=texta.Glob;',
  24375. ' with texta do begin',
  24376. ' two:=one;',
  24377. ' Glob:=Glob;',
  24378. ' end;',
  24379. ' o.two:=o.one;',
  24380. ' o.Glob:=o.Glob;',
  24381. ' with o do begin',
  24382. ' two:=one;',
  24383. ' Glob:=Glob;',
  24384. ' end;',
  24385. '']);
  24386. ConvertProgram;
  24387. CheckSource('TestExtClassHelper_ClassVar',
  24388. LinesToStr([ // statements
  24389. 'rtl.createHelper(this, "THelper", null, function () {',
  24390. ' this.One = 1;',
  24391. ' this.Two = 2;',
  24392. ' this.Glob = 0;',
  24393. ' this.Foo = function (w) {',
  24394. ' var Result = 0;',
  24395. ' Result = w;',
  24396. ' $mod.THelper.Two = 1 + w;',
  24397. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24398. ' Result = $mod.THelper.Glob;',
  24399. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24400. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24401. ' return Result;',
  24402. ' };',
  24403. ' this.Bar = function (w) {',
  24404. ' var Result = 0;',
  24405. ' Result = w;',
  24406. ' $mod.THelper.Two = 1;',
  24407. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24408. ' return Result;',
  24409. ' };',
  24410. '});',
  24411. 'this.o = null;',
  24412. '']),
  24413. LinesToStr([ // $mod.$main
  24414. '$mod.THelper.Two = 1;',
  24415. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24416. '$mod.THelper.Two = 1;',
  24417. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24418. '$mod.THelper.Two = 1;',
  24419. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24420. 'var $with = $mod.o;',
  24421. '$mod.THelper.Two = 1;',
  24422. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24423. '']));
  24424. end;
  24425. procedure TTestModule.TestExtClassHelper_Method_Call;
  24426. begin
  24427. StartProgram(false);
  24428. Add([
  24429. '{$modeswitch externalclass}',
  24430. 'type',
  24431. ' TFly = function(w: word): word of object;',
  24432. ' TExtA = class external name ''ExtObj''',
  24433. ' procedure Run(w: word = 10);',
  24434. ' end;',
  24435. ' THelper = class helper for TExtA',
  24436. ' function Foo(w: word = 1): word;',
  24437. ' function Fly(w: word = 2): word; external name ''Fly'';',
  24438. ' end;',
  24439. 'var p: TFly;',
  24440. 'function THelper.foo(w: word): word;',
  24441. 'begin',
  24442. ' Run;',
  24443. ' Run();',
  24444. ' Run(11);',
  24445. ' Foo;',
  24446. ' Foo();',
  24447. ' Foo(12);',
  24448. ' Self.Foo;',
  24449. ' Self.Foo();',
  24450. ' Self.Foo(13);',
  24451. ' Fly;',
  24452. ' Fly();',
  24453. ' with Self do begin',
  24454. ' Foo;',
  24455. ' Foo();',
  24456. ' Foo(14);',
  24457. ' Fly;',
  24458. ' Fly();',
  24459. ' end;',
  24460. ' p:=@Fly;',
  24461. 'end;',
  24462. 'var Obj: TExtA;',
  24463. 'begin',
  24464. ' obj.Foo;',
  24465. ' obj.Foo();',
  24466. ' obj.Foo(21);',
  24467. ' obj.Fly;',
  24468. ' obj.Fly();',
  24469. ' with obj do begin',
  24470. ' Foo;',
  24471. ' Foo();',
  24472. ' Foo(22);',
  24473. ' Fly;',
  24474. ' Fly();',
  24475. ' end;',
  24476. ' p:[email protected];',
  24477. '']);
  24478. ConvertProgram;
  24479. CheckSource('TestExtClassHelper_Method_Call',
  24480. LinesToStr([ // statements
  24481. 'rtl.createHelper(this, "THelper", null, function () {',
  24482. ' this.Foo = function (w) {',
  24483. ' var Result = 0;',
  24484. ' this.Run(10);',
  24485. ' this.Run(10);',
  24486. ' this.Run(11);',
  24487. ' $mod.THelper.Foo.call(this, 1);',
  24488. ' $mod.THelper.Foo.call(this, 1);',
  24489. ' $mod.THelper.Foo.call(this, 12);',
  24490. ' $mod.THelper.Foo.call(this, 1);',
  24491. ' $mod.THelper.Foo.call(this, 1);',
  24492. ' $mod.THelper.Foo.call(this, 13);',
  24493. ' this.Fly(2);',
  24494. ' this.Fly(2);',
  24495. ' $mod.THelper.Foo.call(this, 1);',
  24496. ' $mod.THelper.Foo.call(this, 1);',
  24497. ' $mod.THelper.Foo.call(this, 14);',
  24498. ' this.Fly(2);',
  24499. ' this.Fly(2);',
  24500. ' $mod.p = rtl.createCallback(this, "Fly");',
  24501. ' return Result;',
  24502. ' };',
  24503. '});',
  24504. 'this.p = null;',
  24505. 'this.Obj = null;',
  24506. '']),
  24507. LinesToStr([ // $mod.$main
  24508. '$mod.THelper.Foo.call($mod.Obj, 1);',
  24509. '$mod.THelper.Foo.call($mod.Obj, 1);',
  24510. '$mod.THelper.Foo.call($mod.Obj, 21);',
  24511. '$mod.Obj.Fly(2);',
  24512. '$mod.Obj.Fly(2);',
  24513. 'var $with = $mod.Obj;',
  24514. '$mod.THelper.Foo.call($with, 1);',
  24515. '$mod.THelper.Foo.call($with, 1);',
  24516. '$mod.THelper.Foo.call($with, 22);',
  24517. '$with.Fly(2);',
  24518. '$with.Fly(2);',
  24519. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  24520. '']));
  24521. end;
  24522. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  24523. begin
  24524. StartProgram(false);
  24525. Add([
  24526. '{$modeswitch externalclass}',
  24527. 'type',
  24528. ' TExtA = class external name ''ExtObj''',
  24529. ' procedure Run(w: word = 10);',
  24530. ' end;',
  24531. ' THelper = class helper for TExtA',
  24532. ' class procedure Fly;',
  24533. ' end;',
  24534. 'class procedure THelper.Fly;',
  24535. 'begin end;',
  24536. 'begin',
  24537. '']);
  24538. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  24539. nHelperClassMethodForExtClassMustBeStatic);
  24540. ConvertProgram;
  24541. end;
  24542. procedure TTestModule.TestRecordHelper_ClassVar;
  24543. begin
  24544. StartProgram(false);
  24545. Add([
  24546. 'type',
  24547. ' TRec = record',
  24548. ' end;',
  24549. ' THelper = record helper for TRec',
  24550. ' const',
  24551. ' One = 1;',
  24552. ' Two: word = 2;',
  24553. ' class var',
  24554. ' Glob: word;',
  24555. ' function Foo(w: word): word;',
  24556. ' class function Bar(w: word): word; static;',
  24557. ' end;',
  24558. 'function THelper.foo(w: word): word;',
  24559. 'begin',
  24560. ' Result:=w;',
  24561. ' Two:=One+w;',
  24562. ' Glob:=Glob;',
  24563. ' Result:=Self.Glob;',
  24564. ' Self.Glob:=Self.Glob;',
  24565. ' with Self do Glob:=Glob;',
  24566. ' Self:=Self;',
  24567. 'end;',
  24568. 'class function THelper.bar(w: word): word;',
  24569. 'begin',
  24570. ' Result:=w;',
  24571. ' Two:=One;',
  24572. ' Glob:=Glob;',
  24573. 'end;',
  24574. 'var r: TRec;',
  24575. 'begin',
  24576. ' trec.two:=trec.one;',
  24577. ' trec.Glob:=trec.Glob;',
  24578. ' with trec do begin',
  24579. ' two:=one;',
  24580. ' Glob:=Glob;',
  24581. ' end;',
  24582. ' r.two:=r.one;',
  24583. ' r.Glob:=r.Glob;',
  24584. ' with r do begin',
  24585. ' two:=one;',
  24586. ' Glob:=Glob;',
  24587. ' end;',
  24588. '']);
  24589. ConvertProgram;
  24590. CheckSource('TestRecordHelper_ClassVar',
  24591. LinesToStr([ // statements
  24592. 'rtl.recNewT(this, "TRec", function () {',
  24593. ' this.$eq = function (b) {',
  24594. ' return true;',
  24595. ' };',
  24596. ' this.$assign = function (s) {',
  24597. ' return this;',
  24598. ' };',
  24599. '});',
  24600. 'rtl.createHelper(this, "THelper", null, function () {',
  24601. ' this.One = 1;',
  24602. ' this.Two = 2;',
  24603. ' this.Glob = 0;',
  24604. ' this.Foo = function (w) {',
  24605. ' var Result = 0;',
  24606. ' Result = w;',
  24607. ' $mod.THelper.Two = 1 + w;',
  24608. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24609. ' Result = $mod.THelper.Glob;',
  24610. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24611. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24612. ' this.$assign(this);',
  24613. ' return Result;',
  24614. ' };',
  24615. ' this.Bar = function (w) {',
  24616. ' var Result = 0;',
  24617. ' Result = w;',
  24618. ' $mod.THelper.Two = 1;',
  24619. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24620. ' return Result;',
  24621. ' };',
  24622. '});',
  24623. 'this.r = this.TRec.$new();',
  24624. '']),
  24625. LinesToStr([ // $mod.$main
  24626. '$mod.THelper.Two = 1;',
  24627. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24628. 'var $with = $mod.TRec;',
  24629. '$mod.THelper.Two = 1;',
  24630. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24631. '$mod.THelper.Two = 1;',
  24632. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24633. 'var $with1 = $mod.r;',
  24634. '$mod.THelper.Two = 1;',
  24635. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24636. '']));
  24637. end;
  24638. procedure TTestModule.TestRecordHelper_Method_Call;
  24639. begin
  24640. StartProgram(false);
  24641. Add([
  24642. '{$modeswitch AdvancedRecords}',
  24643. 'type',
  24644. ' TRec = record',
  24645. ' procedure Run(w: word = 10);',
  24646. ' end;',
  24647. ' THelper = record helper for TRec',
  24648. ' function Foo(w: word = 1): word;',
  24649. ' end;',
  24650. 'procedure TRec.Run(w: word);',
  24651. 'begin',
  24652. ' Foo;',
  24653. ' Foo();',
  24654. ' Foo(2);',
  24655. ' Self.Foo;',
  24656. ' Self.Foo();',
  24657. ' Self.Foo(3);',
  24658. ' with Self do begin',
  24659. ' Foo;',
  24660. ' Foo();',
  24661. ' Foo(4);',
  24662. ' end;',
  24663. 'end;',
  24664. 'function THelper.foo(w: word): word;',
  24665. 'begin',
  24666. ' Run;',
  24667. ' Run();',
  24668. ' Run(11);',
  24669. ' Foo;',
  24670. ' Foo();',
  24671. ' Foo(12);',
  24672. ' Self.Foo;',
  24673. ' Self.Foo();',
  24674. ' Self.Foo(13);',
  24675. ' with Self do begin',
  24676. ' Foo;',
  24677. ' Foo();',
  24678. ' Foo(14);',
  24679. ' end;',
  24680. 'end;',
  24681. 'var Rec: TRec;',
  24682. 'begin',
  24683. ' Rec.Foo;',
  24684. ' Rec.Foo();',
  24685. ' Rec.Foo(21);',
  24686. ' with Rec do begin',
  24687. ' Foo;',
  24688. ' Foo();',
  24689. ' Foo(22);',
  24690. ' end;',
  24691. '']);
  24692. ConvertProgram;
  24693. CheckSource('TestRecordHelper_Method_Call',
  24694. LinesToStr([ // statements
  24695. 'rtl.recNewT(this, "TRec", function () {',
  24696. ' this.$eq = function (b) {',
  24697. ' return true;',
  24698. ' };',
  24699. ' this.$assign = function (s) {',
  24700. ' return this;',
  24701. ' };',
  24702. ' this.Run = function (w) {',
  24703. ' $mod.THelper.Foo.call(this, 1);',
  24704. ' $mod.THelper.Foo.call(this, 1);',
  24705. ' $mod.THelper.Foo.call(this, 2);',
  24706. ' $mod.THelper.Foo.call(this, 1);',
  24707. ' $mod.THelper.Foo.call(this, 1);',
  24708. ' $mod.THelper.Foo.call(this, 3);',
  24709. ' $mod.THelper.Foo.call(this, 1);',
  24710. ' $mod.THelper.Foo.call(this, 1);',
  24711. ' $mod.THelper.Foo.call(this, 4);',
  24712. ' };',
  24713. '});',
  24714. 'rtl.createHelper(this, "THelper", null, function () {',
  24715. ' this.Foo = function (w) {',
  24716. ' var Result = 0;',
  24717. ' this.Run(10);',
  24718. ' this.Run(10);',
  24719. ' this.Run(11);',
  24720. ' $mod.THelper.Foo.call(this, 1);',
  24721. ' $mod.THelper.Foo.call(this, 1);',
  24722. ' $mod.THelper.Foo.call(this, 12);',
  24723. ' $mod.THelper.Foo.call(this, 1);',
  24724. ' $mod.THelper.Foo.call(this, 1);',
  24725. ' $mod.THelper.Foo.call(this, 13);',
  24726. ' $mod.THelper.Foo.call(this, 1);',
  24727. ' $mod.THelper.Foo.call(this, 1);',
  24728. ' $mod.THelper.Foo.call(this, 14);',
  24729. ' return Result;',
  24730. ' };',
  24731. '});',
  24732. 'this.Rec = this.TRec.$new();',
  24733. '']),
  24734. LinesToStr([ // $mod.$main
  24735. '$mod.THelper.Foo.call($mod.Rec, 1);',
  24736. '$mod.THelper.Foo.call($mod.Rec, 1);',
  24737. '$mod.THelper.Foo.call($mod.Rec, 21);',
  24738. 'var $with = $mod.Rec;',
  24739. '$mod.THelper.Foo.call($with, 1);',
  24740. '$mod.THelper.Foo.call($with, 1);',
  24741. '$mod.THelper.Foo.call($with, 22);',
  24742. '']));
  24743. end;
  24744. procedure TTestModule.TestRecordHelper_Constructor;
  24745. begin
  24746. StartProgram(false);
  24747. Add([
  24748. '{$modeswitch AdvancedRecords}',
  24749. 'type',
  24750. ' TRec = record',
  24751. ' constructor Create(w: word);',
  24752. ' end;',
  24753. ' THelper = record helper for TRec',
  24754. ' constructor NewHlp(w: word);',
  24755. ' end;',
  24756. 'var',
  24757. ' Rec: TRec;',
  24758. 'constructor TRec.Create(w: word);',
  24759. 'begin',
  24760. ' NewHlp(2);', // normal call
  24761. ' trec.NewHlp(3);', // new instance
  24762. 'end;',
  24763. 'constructor THelper.NewHlp(w: word);',
  24764. 'begin',
  24765. ' create(2);', // normal call
  24766. ' trec.create(3);', // new instance
  24767. ' NewHlp(4);', // normal call
  24768. ' trec.NewHlp(5);', // new instance
  24769. 'end;',
  24770. 'begin',
  24771. ' rec.newhlp(2);', // normal call
  24772. ' with rec do newhlp(12);', // normal call
  24773. ' trec.newhlp(3);', // new instance
  24774. ' with trec do newhlp(13);', // new instance
  24775. '']);
  24776. ConvertProgram;
  24777. CheckSource('TestRecordHelper_Constructor',
  24778. LinesToStr([ // statements
  24779. 'rtl.recNewT(this, "TRec", function () {',
  24780. ' this.$eq = function (b) {',
  24781. ' return true;',
  24782. ' };',
  24783. ' this.$assign = function (s) {',
  24784. ' return this;',
  24785. ' };',
  24786. ' this.Create = function (w) {',
  24787. ' $mod.THelper.NewHlp.call(this, 2);',
  24788. ' $mod.THelper.$new("NewHlp", [3]);',
  24789. ' return this;',
  24790. ' };',
  24791. '});',
  24792. 'rtl.createHelper(this, "THelper", null, function () {',
  24793. ' this.NewHlp = function (w) {',
  24794. ' this.Create(2);',
  24795. ' $mod.TRec.$new().Create(3);',
  24796. ' $mod.THelper.NewHlp.call(this, 4);',
  24797. ' $mod.THelper.$new("NewHlp", [5]);',
  24798. ' return this;',
  24799. ' };',
  24800. ' this.$new = function (fn, args) {',
  24801. ' return this[fn].apply($mod.TRec.$new(), args);',
  24802. ' };',
  24803. '});',
  24804. 'this.Rec = this.TRec.$new();',
  24805. '']),
  24806. LinesToStr([ // $mod.$main
  24807. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  24808. 'var $with = $mod.Rec;',
  24809. '$mod.THelper.NewHlp.call($with, 12);',
  24810. '$mod.THelper.$new("NewHlp", [3]);',
  24811. 'var $with1 = $mod.TRec;',
  24812. '$mod.THelper.$new("NewHlp", [13]);',
  24813. '']));
  24814. end;
  24815. procedure TTestModule.TestTypeHelper_ClassVar;
  24816. begin
  24817. StartProgram(false);
  24818. Add([
  24819. '{$modeswitch typehelpers}',
  24820. 'type',
  24821. ' THelper = type helper for byte',
  24822. ' const',
  24823. ' One = 1;',
  24824. ' Two: word = 2;',
  24825. ' class var',
  24826. ' Glob: word;',
  24827. ' function Foo(w: word): word;',
  24828. ' class function Bar(w: word): word; static;',
  24829. ' end;',
  24830. 'function THelper.foo(w: word): word;',
  24831. 'begin',
  24832. ' Result:=w;',
  24833. ' Two:=One+w;',
  24834. ' Glob:=Glob;',
  24835. ' Result:=Self.Glob;',
  24836. ' Self.Glob:=Self.Glob;',
  24837. ' with Self do Glob:=Glob;',
  24838. 'end;',
  24839. 'class function THelper.bar(w: word): word;',
  24840. 'begin',
  24841. ' Result:=w;',
  24842. ' Two:=One;',
  24843. ' Glob:=Glob;',
  24844. 'end;',
  24845. 'var b: byte;',
  24846. 'begin',
  24847. ' byte.two:=byte.one;',
  24848. ' byte.Glob:=byte.Glob;',
  24849. ' with byte do begin',
  24850. ' two:=one;',
  24851. ' Glob:=Glob;',
  24852. ' end;',
  24853. ' b.two:=b.one;',
  24854. ' b.Glob:=b.Glob;',
  24855. ' with b do begin',
  24856. ' two:=one;',
  24857. ' Glob:=Glob;',
  24858. ' end;',
  24859. '']);
  24860. ConvertProgram;
  24861. CheckSource('TestTypeHelper_ClassVar',
  24862. LinesToStr([ // statements
  24863. 'rtl.createHelper(this, "THelper", null, function () {',
  24864. ' this.One = 1;',
  24865. ' this.Two = 2;',
  24866. ' this.Glob = 0;',
  24867. ' this.Foo = function (w) {',
  24868. ' var Result = 0;',
  24869. ' Result = w;',
  24870. ' $mod.THelper.Two = 1 + w;',
  24871. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24872. ' Result = $mod.THelper.Glob;',
  24873. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24874. ' var $with = this.get();',
  24875. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24876. ' return Result;',
  24877. ' };',
  24878. ' this.Bar = function (w) {',
  24879. ' var Result = 0;',
  24880. ' Result = w;',
  24881. ' $mod.THelper.Two = 1;',
  24882. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24883. ' return Result;',
  24884. ' };',
  24885. '});',
  24886. 'this.b = 0;',
  24887. '']),
  24888. LinesToStr([ // $mod.$main
  24889. '$mod.THelper.Two = 1;',
  24890. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24891. '$mod.THelper.Two = 1;',
  24892. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24893. '$mod.THelper.Two = 1;',
  24894. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24895. 'var $with = $mod.b;',
  24896. '$mod.THelper.Two = 1;',
  24897. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24898. '']));
  24899. end;
  24900. procedure TTestModule.TestTypeHelper_PassResultElement;
  24901. begin
  24902. StartProgram(false);
  24903. Add([
  24904. '{$modeswitch typehelpers}',
  24905. 'type',
  24906. ' THelper = type helper for word',
  24907. ' procedure DoIt(e: byte = 123);',
  24908. ' class procedure DoSome(e: byte = 456); static;',
  24909. ' end;',
  24910. 'procedure THelper.DoIt(e: byte);',
  24911. 'begin',
  24912. 'end;',
  24913. 'class procedure THelper.DoSome(e: byte);',
  24914. 'begin',
  24915. 'end;',
  24916. 'function Foo(w: word): word;',
  24917. 'begin',
  24918. ' Result.DoIt;',
  24919. ' Result.DoIt();',
  24920. ' Result.DoSome;',
  24921. ' Result.DoSome();',
  24922. ' with Result do begin',
  24923. ' DoIt;',
  24924. ' DoIt();',
  24925. ' DoSome;',
  24926. ' DoSome();',
  24927. ' end;',
  24928. 'end;',
  24929. 'begin',
  24930. '']);
  24931. ConvertProgram;
  24932. CheckSource('TestTypeHelper_PassResultElement',
  24933. LinesToStr([ // statements
  24934. 'rtl.createHelper(this, "THelper", null, function () {',
  24935. ' this.DoIt = function (e) {',
  24936. ' };',
  24937. ' this.DoSome = function (e) {',
  24938. ' };',
  24939. '});',
  24940. 'this.Foo = function (w) {',
  24941. ' var Result = 0;',
  24942. ' $mod.THelper.DoIt.call({',
  24943. ' get: function () {',
  24944. ' return Result;',
  24945. ' },',
  24946. ' set: function (v) {',
  24947. ' Result = v;',
  24948. ' }',
  24949. ' }, 123);',
  24950. ' $mod.THelper.DoIt.call({',
  24951. ' get: function () {',
  24952. ' return Result;',
  24953. ' },',
  24954. ' set: function (v) {',
  24955. ' Result = v;',
  24956. ' }',
  24957. ' }, 123);',
  24958. ' $mod.THelper.DoSome(456);',
  24959. ' $mod.THelper.DoSome(456);',
  24960. ' $mod.THelper.DoIt.call({',
  24961. ' get: function () {',
  24962. ' return Result;',
  24963. ' },',
  24964. ' set: function (v) {',
  24965. ' Result = v;',
  24966. ' }',
  24967. ' }, 123);',
  24968. ' $mod.THelper.DoIt.call({',
  24969. ' get: function () {',
  24970. ' return Result;',
  24971. ' },',
  24972. ' set: function (v) {',
  24973. ' Result = v;',
  24974. ' }',
  24975. ' }, 123);',
  24976. ' $mod.THelper.DoSome(456);',
  24977. ' $mod.THelper.DoSome(456);',
  24978. ' return Result;',
  24979. '};',
  24980. '']),
  24981. LinesToStr([ // $mod.$main
  24982. '']));
  24983. end;
  24984. procedure TTestModule.TestTypeHelper_PassArgs;
  24985. begin
  24986. StartProgram(false);
  24987. Add([
  24988. '{$modeswitch typehelpers}',
  24989. 'type',
  24990. ' THelper = type helper for word',
  24991. ' procedure DoIt(e: byte = 123);',
  24992. ' end;',
  24993. 'procedure THelper.DoIt(e: byte);',
  24994. 'begin',
  24995. 'end;',
  24996. 'procedure FooDefault(a: word);',
  24997. 'begin',
  24998. ' a.DoIt;',
  24999. ' with a do DoIt;',
  25000. 'end;',
  25001. 'procedure FooConst(const a: word);',
  25002. 'begin',
  25003. ' a.DoIt;',
  25004. ' with a do DoIt;',
  25005. 'end;',
  25006. 'procedure FooVar(var a: word);',
  25007. 'begin',
  25008. ' a.DoIt;',
  25009. ' with a do DoIt;',
  25010. 'end;',
  25011. 'begin',
  25012. '']);
  25013. ConvertProgram;
  25014. CheckSource('TestTypeHelper_PassArgs',
  25015. LinesToStr([ // statements
  25016. 'rtl.createHelper(this, "THelper", null, function () {',
  25017. ' this.DoIt = function (e) {',
  25018. ' };',
  25019. '});',
  25020. 'this.FooDefault = function (a) {',
  25021. ' $mod.THelper.DoIt.call({',
  25022. ' get: function () {',
  25023. ' return a;',
  25024. ' },',
  25025. ' set: function (v) {',
  25026. ' a = v;',
  25027. ' }',
  25028. ' }, 123);',
  25029. ' $mod.THelper.DoIt.call({',
  25030. ' get: function () {',
  25031. ' return a;',
  25032. ' },',
  25033. ' set: function (v) {',
  25034. ' a = v;',
  25035. ' }',
  25036. ' }, 123);',
  25037. '};',
  25038. 'this.FooConst = function (a) {',
  25039. ' $mod.THelper.DoIt.call({',
  25040. ' get: function () {',
  25041. ' return a;',
  25042. ' },',
  25043. ' set: function (v) {',
  25044. ' rtl.raiseE("EPropReadOnly");',
  25045. ' }',
  25046. ' }, 123);',
  25047. ' $mod.THelper.DoIt.call({',
  25048. ' get: function () {',
  25049. ' return a;',
  25050. ' },',
  25051. ' set: function () {',
  25052. ' rtl.raiseE("EPropReadOnly");',
  25053. ' }',
  25054. ' }, 123);',
  25055. '};',
  25056. 'this.FooVar = function (a) {',
  25057. ' $mod.THelper.DoIt.call(a, 123);',
  25058. ' var $with = a.get();',
  25059. ' $mod.THelper.DoIt.call(a, 123);',
  25060. '};',
  25061. '']),
  25062. LinesToStr([ // $mod.$main
  25063. '']));
  25064. end;
  25065. procedure TTestModule.TestTypeHelper_PassVarConst;
  25066. begin
  25067. StartProgram(false);
  25068. Add([
  25069. '{$modeswitch typehelpers}',
  25070. 'type',
  25071. ' THelper = type helper for word',
  25072. ' procedure DoIt(e: byte = 123);',
  25073. ' end;',
  25074. 'procedure THelper.DoIt(e: byte);',
  25075. 'begin',
  25076. 'end;',
  25077. 'var a: word;',
  25078. 'const c: word = 2;',
  25079. '{$writeableconst off}',
  25080. 'const r: word = 3;',
  25081. 'begin',
  25082. ' a.DoIt;',
  25083. ' with a do DoIt;',
  25084. ' c.DoIt;',
  25085. ' with c do DoIt;',
  25086. ' r.DoIt;',
  25087. ' with r do DoIt;',
  25088. '']);
  25089. ConvertProgram;
  25090. CheckSource('TestTypeHelper_PassVarConst',
  25091. LinesToStr([ // statements
  25092. 'rtl.createHelper(this, "THelper", null, function () {',
  25093. ' this.DoIt = function (e) {',
  25094. ' };',
  25095. '});',
  25096. 'this.a = 0;',
  25097. 'this.c = 2;',
  25098. 'this.r = 3;',
  25099. '']),
  25100. LinesToStr([ // $mod.$main
  25101. '$mod.THelper.DoIt.call({',
  25102. ' p: $mod,',
  25103. ' get: function () {',
  25104. ' return this.p.a;',
  25105. ' },',
  25106. ' set: function (v) {',
  25107. ' this.p.a = v;',
  25108. ' }',
  25109. '}, 123);',
  25110. 'var $with = $mod.a;',
  25111. '$mod.THelper.DoIt.call({',
  25112. ' get: function () {',
  25113. ' return $with;',
  25114. ' },',
  25115. ' set: function (v) {',
  25116. ' $with = v;',
  25117. ' }',
  25118. '}, 123);',
  25119. '$mod.THelper.DoIt.call({',
  25120. ' p: $mod,',
  25121. ' get: function () {',
  25122. ' return this.p.c;',
  25123. ' },',
  25124. ' set: function (v) {',
  25125. ' this.p.c = v;',
  25126. ' }',
  25127. '}, 123);',
  25128. 'var $with1 = $mod.c;',
  25129. '$mod.THelper.DoIt.call({',
  25130. ' get: function () {',
  25131. ' return $with1;',
  25132. ' },',
  25133. ' set: function (v) {',
  25134. ' $with1 = v;',
  25135. ' }',
  25136. '}, 123);',
  25137. '$mod.THelper.DoIt.call({',
  25138. ' get: function () {',
  25139. ' return 3;',
  25140. ' },',
  25141. ' set: function (v) {',
  25142. ' rtl.raiseE("EPropReadOnly");',
  25143. ' }',
  25144. '}, 123);',
  25145. 'var $with2 = 3;',
  25146. ' $mod.THelper.DoIt.call({',
  25147. ' get: function () {',
  25148. ' return $with2;',
  25149. ' },',
  25150. ' set: function () {',
  25151. ' rtl.raiseE("EPropReadOnly");',
  25152. ' }',
  25153. ' }, 123);',
  25154. '']));
  25155. end;
  25156. procedure TTestModule.TestTypeHelper_PassFuncResult;
  25157. begin
  25158. StartProgram(false);
  25159. Add([
  25160. '{$modeswitch typehelpers}',
  25161. 'type',
  25162. ' THelper = type helper for word',
  25163. ' procedure DoIt(e: byte = 123);',
  25164. ' end;',
  25165. 'procedure THelper.DoIt(e: byte);',
  25166. 'begin',
  25167. 'end;',
  25168. 'function Foo(b: byte = 1): word;',
  25169. 'begin',
  25170. 'end;',
  25171. 'begin',
  25172. ' Foo.DoIt;',
  25173. ' Foo().DoIt;',
  25174. ' with Foo do DoIt;',
  25175. ' with Foo() do DoIt;',
  25176. '']);
  25177. ConvertProgram;
  25178. CheckSource('TestTypeHelper_PassFuncResult',
  25179. LinesToStr([ // statements
  25180. 'rtl.createHelper(this, "THelper", null, function () {',
  25181. ' this.DoIt = function (e) {',
  25182. ' };',
  25183. '});',
  25184. 'this.Foo = function (b) {',
  25185. ' var Result = 0;',
  25186. ' return Result;',
  25187. '};',
  25188. '']),
  25189. LinesToStr([ // $mod.$main
  25190. '$mod.THelper.DoIt.call({',
  25191. ' a: $mod.Foo(1),',
  25192. ' get: function () {',
  25193. ' return this.a;',
  25194. ' },',
  25195. ' set: function (v) {',
  25196. ' this.a = v;',
  25197. ' }',
  25198. '}, 123);',
  25199. '$mod.THelper.DoIt.call({',
  25200. ' a: $mod.Foo(1),',
  25201. ' get: function () {',
  25202. ' return this.a;',
  25203. ' },',
  25204. ' set: function (v) {',
  25205. ' this.a = v;',
  25206. ' }',
  25207. '}, 123);',
  25208. 'var $with = $mod.Foo(1);',
  25209. '$mod.THelper.DoIt.call({',
  25210. ' get: function () {',
  25211. ' return $with;',
  25212. ' },',
  25213. ' set: function (v) {',
  25214. ' $with = v;',
  25215. ' }',
  25216. '}, 123);',
  25217. 'var $with1 = $mod.Foo(1);',
  25218. '$mod.THelper.DoIt.call({',
  25219. ' get: function () {',
  25220. ' return $with1;',
  25221. ' },',
  25222. ' set: function (v) {',
  25223. ' $with1 = v;',
  25224. ' }',
  25225. '}, 123);',
  25226. '']));
  25227. end;
  25228. procedure TTestModule.TestTypeHelper_PassPropertyField;
  25229. begin
  25230. StartProgram(false);
  25231. Add([
  25232. '{$modeswitch typehelpers}',
  25233. 'type',
  25234. ' TObject = class',
  25235. ' FField: word;',
  25236. ' procedure SetField(Value: word);',
  25237. ' property Field: word read FField write SetField;',
  25238. ' end;',
  25239. ' THelper = type helper for word',
  25240. ' procedure Fly;',
  25241. ' class procedure Run; static;',
  25242. ' end;',
  25243. 'procedure TObject.SetField(Value: word);',
  25244. 'begin',
  25245. ' Field.Fly;',
  25246. ' Field.Run;',
  25247. ' Self.Field.Fly;',
  25248. ' Self.Field.Run;',
  25249. ' with Self do begin',
  25250. ' Field.Fly;',
  25251. ' Field.Run;',
  25252. ' end;',
  25253. ' with Self.Field do begin',
  25254. ' Fly;',
  25255. ' Run;',
  25256. ' end;',
  25257. 'end;',
  25258. 'procedure THelper.Fly;',
  25259. 'begin',
  25260. 'end;',
  25261. 'class procedure THelper.Run;',
  25262. 'begin',
  25263. 'end;',
  25264. 'var',
  25265. ' o: TObject;',
  25266. 'begin',
  25267. ' o.Field.Fly;',
  25268. ' o.Field.Run;',
  25269. ' with o do begin',
  25270. ' Field.Fly;',
  25271. ' Field.Run;',
  25272. ' end;',
  25273. ' with o.Field do begin',
  25274. ' Fly;',
  25275. ' Run;',
  25276. ' end;',
  25277. '']);
  25278. ConvertProgram;
  25279. CheckSource('TestTypeHelper_PassPropertyField',
  25280. LinesToStr([ // statements
  25281. 'rtl.createClass(this, "TObject", null, function () {',
  25282. ' this.$init = function () {',
  25283. ' this.FField = 0;',
  25284. ' };',
  25285. ' this.$final = function () {',
  25286. ' };',
  25287. ' this.SetField = function (Value) {',
  25288. ' $mod.THelper.Fly.call({',
  25289. ' p: this,',
  25290. ' get: function () {',
  25291. ' return this.p.FField;',
  25292. ' },',
  25293. ' set: function (v) {',
  25294. ' this.p.FField = v;',
  25295. ' }',
  25296. ' });',
  25297. ' $mod.THelper.Run();',
  25298. ' $mod.THelper.Fly.call({',
  25299. ' p: this,',
  25300. ' get: function () {',
  25301. ' return this.p.FField;',
  25302. ' },',
  25303. ' set: function (v) {',
  25304. ' this.p.FField = v;',
  25305. ' }',
  25306. ' });',
  25307. ' $mod.THelper.Run();',
  25308. ' $mod.THelper.Fly.call({',
  25309. ' p: this,',
  25310. ' get: function () {',
  25311. ' return this.p.FField;',
  25312. ' },',
  25313. ' set: function (v) {',
  25314. ' this.p.FField = v;',
  25315. ' }',
  25316. ' });',
  25317. ' $mod.THelper.Run();',
  25318. ' var $with = this.FField;',
  25319. ' $mod.THelper.Fly.call({',
  25320. ' get: function () {',
  25321. ' return $with;',
  25322. ' },',
  25323. ' set: function (v) {',
  25324. ' $with = v;',
  25325. ' }',
  25326. ' });',
  25327. ' $mod.THelper.Run();',
  25328. ' };',
  25329. '});',
  25330. 'rtl.createHelper(this, "THelper", null, function () {',
  25331. ' this.Fly = function () {',
  25332. ' };',
  25333. ' this.Run = function () {',
  25334. ' };',
  25335. '});',
  25336. 'this.o = null;',
  25337. '']),
  25338. LinesToStr([ // $mod.$main
  25339. '$mod.THelper.Fly.call({',
  25340. ' p: $mod.o,',
  25341. ' get: function () {',
  25342. ' return this.p.FField;',
  25343. ' },',
  25344. ' set: function (v) {',
  25345. ' this.p.FField = v;',
  25346. ' }',
  25347. '});',
  25348. '$mod.THelper.Run();',
  25349. 'var $with = $mod.o;',
  25350. '$mod.THelper.Fly.call({',
  25351. ' p: $with,',
  25352. ' get: function () {',
  25353. ' return this.p.FField;',
  25354. ' },',
  25355. ' set: function (v) {',
  25356. ' this.p.FField = v;',
  25357. ' }',
  25358. '});',
  25359. '$mod.THelper.Run();',
  25360. 'var $with1 = $mod.o.FField;',
  25361. '$mod.THelper.Fly.call({',
  25362. ' get: function () {',
  25363. ' return $with1;',
  25364. ' },',
  25365. ' set: function (v) {',
  25366. ' $with1 = v;',
  25367. ' }',
  25368. '});',
  25369. '$mod.THelper.Run();',
  25370. '']));
  25371. end;
  25372. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  25373. begin
  25374. StartProgram(false);
  25375. Add([
  25376. '{$modeswitch typehelpers}',
  25377. 'type',
  25378. ' TObject = class',
  25379. ' FField: word;',
  25380. ' function GetField: word;',
  25381. ' property Field: word read GetField write FField;',
  25382. ' end;',
  25383. ' THelper = type helper for word',
  25384. ' procedure Fly;',
  25385. ' class procedure Run; static;',
  25386. ' end;',
  25387. 'function TObject.GetField: word;',
  25388. 'begin',
  25389. ' Field.Fly;',
  25390. ' Field.Run;',
  25391. ' Self.Field.Fly;',
  25392. ' Self.Field.Run;',
  25393. ' with Self do begin',
  25394. ' Field.Fly;',
  25395. ' Field.Run;',
  25396. ' end;',
  25397. ' with Self.Field do begin',
  25398. ' Fly;',
  25399. ' Run;',
  25400. ' end;',
  25401. 'end;',
  25402. 'procedure THelper.Fly;',
  25403. 'begin',
  25404. 'end;',
  25405. 'class procedure THelper.Run;',
  25406. 'begin',
  25407. 'end;',
  25408. 'var',
  25409. ' o: TObject;',
  25410. 'begin',
  25411. ' o.Field.Fly;',
  25412. ' o.Field.Run;',
  25413. ' with o do begin',
  25414. ' Field.Fly;',
  25415. ' Field.Run;',
  25416. ' end;',
  25417. ' with o.Field do begin',
  25418. ' Fly;',
  25419. ' Run;',
  25420. ' end;',
  25421. '']);
  25422. ConvertProgram;
  25423. CheckSource('TestTypeHelper_PassPropertyGetter',
  25424. LinesToStr([ // statements
  25425. 'rtl.createClass(this, "TObject", null, function () {',
  25426. ' this.$init = function () {',
  25427. ' this.FField = 0;',
  25428. ' };',
  25429. ' this.$final = function () {',
  25430. ' };',
  25431. ' this.GetField = function () {',
  25432. ' var Result = 0;',
  25433. ' $mod.THelper.Fly.call({',
  25434. ' p: this.GetField(),',
  25435. ' get: function () {',
  25436. ' return this.p;',
  25437. ' },',
  25438. ' set: function (v) {',
  25439. ' this.p = v;',
  25440. ' }',
  25441. ' });',
  25442. ' $mod.THelper.Run();',
  25443. ' $mod.THelper.Fly.call({',
  25444. ' p: this.GetField(),',
  25445. ' get: function () {',
  25446. ' return this.p;',
  25447. ' },',
  25448. ' set: function (v) {',
  25449. ' this.p = v;',
  25450. ' }',
  25451. ' });',
  25452. ' $mod.THelper.Run();',
  25453. ' $mod.THelper.Fly.call({',
  25454. ' p: this.GetField(),',
  25455. ' get: function () {',
  25456. ' return this.p;',
  25457. ' },',
  25458. ' set: function (v) {',
  25459. ' this.p = v;',
  25460. ' }',
  25461. ' });',
  25462. ' $mod.THelper.Run();',
  25463. ' var $with = this.GetField();',
  25464. ' $mod.THelper.Fly.call({',
  25465. ' get: function () {',
  25466. ' return $with;',
  25467. ' },',
  25468. ' set: function (v) {',
  25469. ' $with = v;',
  25470. ' }',
  25471. ' });',
  25472. ' $mod.THelper.Run();',
  25473. ' return Result;',
  25474. ' };',
  25475. '});',
  25476. 'rtl.createHelper(this, "THelper", null, function () {',
  25477. ' this.Fly = function () {',
  25478. ' };',
  25479. ' this.Run = function () {',
  25480. ' };',
  25481. '});',
  25482. 'this.o = null;',
  25483. '']),
  25484. LinesToStr([ // $mod.$main
  25485. '$mod.THelper.Fly.call({',
  25486. ' p: $mod.o.GetField(),',
  25487. ' get: function () {',
  25488. ' return this.p;',
  25489. ' },',
  25490. ' set: function (v) {',
  25491. ' this.p = v;',
  25492. ' }',
  25493. '});',
  25494. '$mod.THelper.Run();',
  25495. 'var $with = $mod.o;',
  25496. '$mod.THelper.Fly.call({',
  25497. ' p: $with.GetField(),',
  25498. ' get: function () {',
  25499. ' return this.p;',
  25500. ' },',
  25501. ' set: function (v) {',
  25502. ' this.p = v;',
  25503. ' }',
  25504. '});',
  25505. '$mod.THelper.Run();',
  25506. 'var $with1 = $mod.o.GetField();',
  25507. '$mod.THelper.Fly.call({',
  25508. ' get: function () {',
  25509. ' return $with1;',
  25510. ' },',
  25511. ' set: function (v) {',
  25512. ' $with1 = v;',
  25513. ' }',
  25514. '});',
  25515. '$mod.THelper.Run();',
  25516. '']));
  25517. end;
  25518. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  25519. begin
  25520. StartProgram(false);
  25521. Add([
  25522. '{$modeswitch typehelpers}',
  25523. 'type',
  25524. ' TObject = class',
  25525. ' class var FField: word;',
  25526. ' class procedure SetField(Value: word);',
  25527. ' class property Field: word read FField write SetField;',
  25528. ' end;',
  25529. ' THelper = type helper for word',
  25530. ' procedure Fly(n: byte);',
  25531. ' end;',
  25532. 'class procedure TObject.SetField(Value: word);',
  25533. 'begin',
  25534. ' Field.Fly(1);',
  25535. ' Self.Field.Fly(2);',
  25536. ' with Self do Field.Fly(3);',
  25537. ' with Self.Field do Fly(4);',
  25538. ' TObject.Field.Fly(5);',
  25539. ' with TObject do Field.Fly(6);',
  25540. ' with TObject.Field do Fly(7);',
  25541. 'end;',
  25542. 'procedure THelper.Fly(n: byte);',
  25543. 'begin',
  25544. 'end;',
  25545. 'var',
  25546. ' o: TObject;',
  25547. 'begin',
  25548. ' o.Field.Fly(11);',
  25549. ' with o do Field.Fly(12);',
  25550. ' with o.Field do Fly(13);',
  25551. ' TObject.Field.Fly(14);',
  25552. ' with TObject do Field.Fly(15);',
  25553. ' with TObject.Field do Fly(16);',
  25554. '']);
  25555. ConvertProgram;
  25556. CheckSource('TestTypeHelper_PassClassPropertyField',
  25557. LinesToStr([ // statements
  25558. 'rtl.createClass(this, "TObject", null, function () {',
  25559. ' this.FField = 0;',
  25560. ' this.$init = function () {',
  25561. ' };',
  25562. ' this.$final = function () {',
  25563. ' };',
  25564. ' this.SetField = function (Value) {',
  25565. ' $mod.THelper.Fly.call({',
  25566. ' p: this,',
  25567. ' get: function () {',
  25568. ' return this.p.FField;',
  25569. ' },',
  25570. ' set: function (v) {',
  25571. ' $mod.TObject.FField = v;',
  25572. ' }',
  25573. ' }, 1);',
  25574. ' $mod.THelper.Fly.call({',
  25575. ' p: this,',
  25576. ' get: function () {',
  25577. ' return this.p.FField;',
  25578. ' },',
  25579. ' set: function (v) {',
  25580. ' $mod.TObject.FField = v;',
  25581. ' }',
  25582. ' }, 2);',
  25583. ' $mod.THelper.Fly.call({',
  25584. ' p: this,',
  25585. ' get: function () {',
  25586. ' return this.p.FField;',
  25587. ' },',
  25588. ' set: function (v) {',
  25589. ' $mod.TObject.FField = v;',
  25590. ' }',
  25591. ' }, 3);',
  25592. ' var $with = this.FField;',
  25593. ' $mod.THelper.Fly.call({',
  25594. ' get: function () {',
  25595. ' return $with;',
  25596. ' },',
  25597. ' set: function (v) {',
  25598. ' $with = v;',
  25599. ' }',
  25600. ' }, 4);',
  25601. ' $mod.THelper.Fly.call({',
  25602. ' p: $mod.TObject,',
  25603. ' get: function () {',
  25604. ' return this.p.FField;',
  25605. ' },',
  25606. ' set: function (v) {',
  25607. ' $mod.TObject.FField = v;',
  25608. ' }',
  25609. ' }, 5);',
  25610. ' var $with1 = $mod.TObject;',
  25611. ' $mod.THelper.Fly.call({',
  25612. ' p: $with1,',
  25613. ' get: function () {',
  25614. ' return this.p.FField;',
  25615. ' },',
  25616. ' set: function (v) {',
  25617. ' $mod.TObject.FField = v;',
  25618. ' }',
  25619. ' }, 6);',
  25620. ' var $with2 = $mod.TObject.FField;',
  25621. ' $mod.THelper.Fly.call({',
  25622. ' get: function () {',
  25623. ' return $with2;',
  25624. ' },',
  25625. ' set: function (v) {',
  25626. ' $with2 = v;',
  25627. ' }',
  25628. ' }, 7);',
  25629. ' };',
  25630. '});',
  25631. 'rtl.createHelper(this, "THelper", null, function () {',
  25632. ' this.Fly = function (n) {',
  25633. ' };',
  25634. '});',
  25635. 'this.o = null;',
  25636. '']),
  25637. LinesToStr([ // $mod.$main
  25638. '$mod.THelper.Fly.call({',
  25639. ' p: $mod.o,',
  25640. ' get: function () {',
  25641. ' return this.p.FField;',
  25642. ' },',
  25643. ' set: function (v) {',
  25644. ' $mod.TObject.FField = v;',
  25645. ' }',
  25646. '}, 11);',
  25647. 'var $with = $mod.o;',
  25648. '$mod.THelper.Fly.call({',
  25649. ' p: $with,',
  25650. ' get: function () {',
  25651. ' return this.p.FField;',
  25652. ' },',
  25653. ' set: function (v) {',
  25654. ' $mod.TObject.FField = v;',
  25655. ' }',
  25656. '}, 12);',
  25657. 'var $with1 = $mod.o.FField;',
  25658. '$mod.THelper.Fly.call({',
  25659. ' get: function () {',
  25660. ' return $with1;',
  25661. ' },',
  25662. ' set: function (v) {',
  25663. ' $with1 = v;',
  25664. ' }',
  25665. '}, 13);',
  25666. '$mod.THelper.Fly.call({',
  25667. ' p: $mod.TObject,',
  25668. ' get: function () {',
  25669. ' return this.p.FField;',
  25670. ' },',
  25671. ' set: function (v) {',
  25672. ' $mod.TObject.FField = v;',
  25673. ' }',
  25674. '}, 14);',
  25675. 'var $with2 = $mod.TObject;',
  25676. '$mod.THelper.Fly.call({',
  25677. ' p: $with2,',
  25678. ' get: function () {',
  25679. ' return this.p.FField;',
  25680. ' },',
  25681. ' set: function (v) {',
  25682. ' $mod.TObject.FField = v;',
  25683. ' }',
  25684. '}, 15);',
  25685. 'var $with3 = $mod.TObject.FField;',
  25686. '$mod.THelper.Fly.call({',
  25687. ' get: function () {',
  25688. ' return $with3;',
  25689. ' },',
  25690. ' set: function (v) {',
  25691. ' $with3 = v;',
  25692. ' }',
  25693. '}, 16);',
  25694. '']));
  25695. end;
  25696. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  25697. begin
  25698. StartProgram(false);
  25699. Add([
  25700. '{$modeswitch typehelpers}',
  25701. 'type',
  25702. ' TObject = class',
  25703. ' class var FField: word;',
  25704. ' class function GetField: word; static;',
  25705. ' class property Field: word read GetField write FField;',
  25706. ' end;',
  25707. ' THelper = type helper for word',
  25708. ' procedure Fly(n: byte);',
  25709. ' end;',
  25710. 'class function TObject.GetField: word;',
  25711. 'begin',
  25712. ' Field.Fly(1);',
  25713. ' TObject.Field.Fly(5);',
  25714. ' with TObject do Field.Fly(6);',
  25715. ' with TObject.Field do Fly(7);',
  25716. 'end;',
  25717. 'procedure THelper.Fly(n: byte);',
  25718. 'begin',
  25719. 'end;',
  25720. 'var',
  25721. ' o: TObject;',
  25722. 'begin',
  25723. ' o.Field.Fly(11);',
  25724. ' with o do Field.Fly(12);',
  25725. ' with o.Field do Fly(13);',
  25726. '']);
  25727. ConvertProgram;
  25728. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  25729. LinesToStr([ // statements
  25730. 'rtl.createClass(this, "TObject", null, function () {',
  25731. ' this.FField = 0;',
  25732. ' this.$init = function () {',
  25733. ' };',
  25734. ' this.$final = function () {',
  25735. ' };',
  25736. ' this.GetField = function () {',
  25737. ' var Result = 0;',
  25738. ' $mod.THelper.Fly.call({',
  25739. ' p: $mod.TObject.GetField(),',
  25740. ' get: function () {',
  25741. ' return this.p;',
  25742. ' },',
  25743. ' set: function (v) {',
  25744. ' this.p = v;',
  25745. ' }',
  25746. ' }, 1);',
  25747. ' $mod.THelper.Fly.call({',
  25748. ' p: $mod.TObject.GetField(),',
  25749. ' get: function () {',
  25750. ' return this.p;',
  25751. ' },',
  25752. ' set: function (v) {',
  25753. ' this.p = v;',
  25754. ' }',
  25755. ' }, 5);',
  25756. ' var $with = $mod.TObject;',
  25757. ' $mod.THelper.Fly.call({',
  25758. ' p: $with.GetField(),',
  25759. ' get: function () {',
  25760. ' return this.p;',
  25761. ' },',
  25762. ' set: function (v) {',
  25763. ' this.p = v;',
  25764. ' }',
  25765. ' }, 6);',
  25766. ' var $with1 = $mod.TObject.GetField();',
  25767. ' $mod.THelper.Fly.call({',
  25768. ' get: function () {',
  25769. ' return $with1;',
  25770. ' },',
  25771. ' set: function (v) {',
  25772. ' $with1 = v;',
  25773. ' }',
  25774. ' }, 7);',
  25775. ' return Result;',
  25776. ' };',
  25777. '});',
  25778. 'rtl.createHelper(this, "THelper", null, function () {',
  25779. ' this.Fly = function (n) {',
  25780. ' };',
  25781. '});',
  25782. 'this.o = null;',
  25783. '']),
  25784. LinesToStr([ // $mod.$main
  25785. '$mod.THelper.Fly.call({',
  25786. ' p: $mod.TObject.GetField(),',
  25787. ' get: function () {',
  25788. ' return this.p;',
  25789. ' },',
  25790. ' set: function (v) {',
  25791. ' this.p = v;',
  25792. ' }',
  25793. '}, 11);',
  25794. 'var $with = $mod.o;',
  25795. '$mod.THelper.Fly.call({',
  25796. ' p: $with.GetField(),',
  25797. ' get: function () {',
  25798. ' return this.p;',
  25799. ' },',
  25800. ' set: function (v) {',
  25801. ' this.p = v;',
  25802. ' }',
  25803. '}, 12);',
  25804. 'var $with1 = $mod.TObject.GetField();',
  25805. '$mod.THelper.Fly.call({',
  25806. ' get: function () {',
  25807. ' return $with1;',
  25808. ' },',
  25809. ' set: function (v) {',
  25810. ' $with1 = v;',
  25811. ' }',
  25812. '}, 13);',
  25813. '']));
  25814. end;
  25815. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  25816. begin
  25817. StartProgram(false);
  25818. Add([
  25819. '{$modeswitch typehelpers}',
  25820. 'type',
  25821. ' TObject = class',
  25822. ' class var FField: word;',
  25823. ' class function GetField: word;',
  25824. ' class property Field: word read GetField write FField;',
  25825. ' end;',
  25826. ' TClass = class of TObject;',
  25827. ' THelper = type helper for word',
  25828. ' procedure Fly(n: byte);',
  25829. ' end;',
  25830. 'class function TObject.GetField: word;',
  25831. 'begin',
  25832. ' Field.Fly(1);',
  25833. ' Self.Field.Fly(5);',
  25834. ' with Self do Field.Fly(6);',
  25835. ' with Self.Field do Fly(7);',
  25836. 'end;',
  25837. 'procedure THelper.Fly(n: byte);',
  25838. 'begin',
  25839. 'end;',
  25840. 'var',
  25841. ' o: TObject;',
  25842. ' c: TClass;',
  25843. 'begin',
  25844. ' o.Field.Fly(11);',
  25845. ' with o do Field.Fly(12);',
  25846. ' with o.Field do Fly(13);',
  25847. ' c.Field.Fly(14);',
  25848. ' with c do Field.Fly(15);',
  25849. ' with c.Field do Fly(16);',
  25850. '']);
  25851. ConvertProgram;
  25852. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  25853. LinesToStr([ // statements
  25854. 'rtl.createClass(this, "TObject", null, function () {',
  25855. ' this.FField = 0;',
  25856. ' this.$init = function () {',
  25857. ' };',
  25858. ' this.$final = function () {',
  25859. ' };',
  25860. ' this.GetField = function () {',
  25861. ' var Result = 0;',
  25862. ' $mod.THelper.Fly.call({',
  25863. ' p: this.GetField(),',
  25864. ' get: function () {',
  25865. ' return this.p;',
  25866. ' },',
  25867. ' set: function (v) {',
  25868. ' this.p = v;',
  25869. ' }',
  25870. ' }, 1);',
  25871. ' $mod.THelper.Fly.call({',
  25872. ' p: this.GetField(),',
  25873. ' get: function () {',
  25874. ' return this.p;',
  25875. ' },',
  25876. ' set: function (v) {',
  25877. ' this.p = v;',
  25878. ' }',
  25879. ' }, 5);',
  25880. ' $mod.THelper.Fly.call({',
  25881. ' p: this.GetField(),',
  25882. ' get: function () {',
  25883. ' return this.p;',
  25884. ' },',
  25885. ' set: function (v) {',
  25886. ' this.p = v;',
  25887. ' }',
  25888. ' }, 6);',
  25889. ' var $with = this.GetField();',
  25890. ' $mod.THelper.Fly.call({',
  25891. ' get: function () {',
  25892. ' return $with;',
  25893. ' },',
  25894. ' set: function (v) {',
  25895. ' $with = v;',
  25896. ' }',
  25897. ' }, 7);',
  25898. ' return Result;',
  25899. ' };',
  25900. '});',
  25901. 'rtl.createHelper(this, "THelper", null, function () {',
  25902. ' this.Fly = function (n) {',
  25903. ' };',
  25904. '});',
  25905. 'this.o = null;',
  25906. 'this.c = null;',
  25907. '']),
  25908. LinesToStr([ // $mod.$main
  25909. '$mod.THelper.Fly.call({',
  25910. ' p: $mod.o.$class.GetField(),',
  25911. ' get: function () {',
  25912. ' return this.p;',
  25913. ' },',
  25914. ' set: function (v) {',
  25915. ' this.p = v;',
  25916. ' }',
  25917. '}, 11);',
  25918. 'var $with = $mod.o;',
  25919. '$mod.THelper.Fly.call({',
  25920. ' p: $with.$class.GetField(),',
  25921. ' get: function () {',
  25922. ' return this.p;',
  25923. ' },',
  25924. ' set: function (v) {',
  25925. ' this.p = v;',
  25926. ' }',
  25927. '}, 12);',
  25928. 'var $with1 = $mod.o.$class.GetField();',
  25929. '$mod.THelper.Fly.call({',
  25930. ' get: function () {',
  25931. ' return $with1;',
  25932. ' },',
  25933. ' set: function (v) {',
  25934. ' $with1 = v;',
  25935. ' }',
  25936. '}, 13);',
  25937. '$mod.THelper.Fly.call({',
  25938. ' p: $mod.c.GetField(),',
  25939. ' get: function () {',
  25940. ' return this.p;',
  25941. ' },',
  25942. ' set: function (v) {',
  25943. ' this.p = v;',
  25944. ' }',
  25945. '}, 14);',
  25946. 'var $with2 = $mod.c;',
  25947. '$mod.THelper.Fly.call({',
  25948. ' p: $with2.GetField(),',
  25949. ' get: function () {',
  25950. ' return this.p;',
  25951. ' },',
  25952. ' set: function (v) {',
  25953. ' this.p = v;',
  25954. ' }',
  25955. '}, 15);',
  25956. 'var $with3 = $mod.c.GetField();',
  25957. '$mod.THelper.Fly.call({',
  25958. ' get: function () {',
  25959. ' return $with3;',
  25960. ' },',
  25961. ' set: function (v) {',
  25962. ' $with3 = v;',
  25963. ' }',
  25964. '}, 16);',
  25965. '']));
  25966. end;
  25967. procedure TTestModule.TestTypeHelper_Property;
  25968. begin
  25969. StartProgram(false);
  25970. Add([
  25971. '{$modeswitch typehelpers}',
  25972. 'type',
  25973. ' THelper = type helper for word',
  25974. ' function GetSize: longint;',
  25975. ' procedure SetSize(Value: longint);',
  25976. ' property Size: longint read GetSize write SetSize;',
  25977. ' end;',
  25978. 'function THelper.GetSize: longint;',
  25979. 'begin',
  25980. ' Result:=Size+1;',
  25981. ' Size:=2;',
  25982. ' Result:=Self.Size+3;',
  25983. ' Self.Size:=4;',
  25984. ' with Self do begin',
  25985. ' Result:=Size+5;',
  25986. ' Size:=6;',
  25987. ' end;',
  25988. 'end;',
  25989. 'procedure THelper.SetSize(Value: longint);',
  25990. 'begin',
  25991. 'end;',
  25992. 'var w: word;',
  25993. 'begin',
  25994. ' w:=w.Size+7;',
  25995. ' w.Size:=w+8;',
  25996. ' with w do begin',
  25997. ' w:=Size+9;',
  25998. ' Size:=w+10;',
  25999. ' end;',
  26000. '']);
  26001. ConvertProgram;
  26002. CheckSource('TestTypeHelper_Property',
  26003. LinesToStr([ // statements
  26004. 'rtl.createHelper(this, "THelper", null, function () {',
  26005. ' this.GetSize = function () {',
  26006. ' var Result = 0;',
  26007. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  26008. ' $mod.THelper.SetSize.call(this, 2);',
  26009. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  26010. ' $mod.THelper.SetSize.call(this, 4);',
  26011. ' var $with = this.get();',
  26012. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  26013. ' $mod.THelper.SetSize.call(this, 6);',
  26014. ' return Result;',
  26015. ' };',
  26016. ' this.SetSize = function (Value) {',
  26017. ' };',
  26018. '});',
  26019. 'this.w = 0;',
  26020. '']),
  26021. LinesToStr([ // $mod.$main
  26022. '$mod.w = $mod.THelper.GetSize.call({',
  26023. ' p: $mod,',
  26024. ' get: function () {',
  26025. ' return this.p.w;',
  26026. ' },',
  26027. ' set: function (v) {',
  26028. ' this.p.w = v;',
  26029. ' }',
  26030. '}) + 7;',
  26031. '$mod.THelper.SetSize.call({',
  26032. ' p: $mod,',
  26033. ' get: function () {',
  26034. ' return this.p.w;',
  26035. ' },',
  26036. ' set: function (v) {',
  26037. ' this.p.w = v;',
  26038. ' }',
  26039. '}, $mod.w + 8);',
  26040. 'var $with = $mod.w;',
  26041. '$mod.w = $mod.THelper.GetSize.call({',
  26042. ' get: function () {',
  26043. ' return $with;',
  26044. ' },',
  26045. ' set: function (v) {',
  26046. ' $with = v;',
  26047. ' }',
  26048. '}) + 9;',
  26049. '$mod.THelper.SetSize.call({',
  26050. ' get: function () {',
  26051. ' return $with;',
  26052. ' },',
  26053. ' set: function (v) {',
  26054. ' $with = v;',
  26055. ' }',
  26056. '}, $mod.w + 10);',
  26057. '']));
  26058. end;
  26059. procedure TTestModule.TestTypeHelper_Property_Array;
  26060. begin
  26061. StartProgram(false);
  26062. Add([
  26063. '{$modeswitch typehelpers}',
  26064. 'type',
  26065. ' THelper = type helper for word',
  26066. ' function GetItems(Index: byte): boolean;',
  26067. ' procedure SetItems(Index: byte; Value: boolean);',
  26068. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  26069. ' end;',
  26070. 'function THelper.GetItems(Index: byte): boolean;',
  26071. 'begin',
  26072. ' Result:=Items[1];',
  26073. ' Items[2]:=false;',
  26074. ' Result:=Self.Items[3];',
  26075. ' Self.Items[4]:=true;',
  26076. ' with Self do begin',
  26077. ' Result:=Items[5];',
  26078. ' Items[6]:=false;',
  26079. ' end;',
  26080. 'end;',
  26081. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  26082. 'begin',
  26083. 'end;',
  26084. 'var',
  26085. ' w: word;',
  26086. ' b: boolean;',
  26087. 'begin',
  26088. ' b:=w.Items[1];',
  26089. ' w.Items[2]:=b;',
  26090. ' with w do begin',
  26091. ' b:=Items[3];',
  26092. ' Items[4]:=b;',
  26093. ' end;',
  26094. '']);
  26095. ConvertProgram;
  26096. CheckSource('TestTypeHelper_Property_Array',
  26097. LinesToStr([ // statements
  26098. 'rtl.createHelper(this, "THelper", null, function () {',
  26099. ' this.GetItems = function (Index) {',
  26100. ' var Result = false;',
  26101. ' Result = $mod.THelper.GetItems.call(this, 1);',
  26102. ' $mod.THelper.SetItems.call(this, 2, false);',
  26103. ' Result = $mod.THelper.GetItems.call(this, 3);',
  26104. ' $mod.THelper.SetItems.call(this, 4, true);',
  26105. ' var $with = this.get();',
  26106. ' Result = $mod.THelper.GetItems.call(this, 5);',
  26107. ' $mod.THelper.SetItems.call(this, 6, false);',
  26108. ' return Result;',
  26109. ' };',
  26110. ' this.SetItems = function (Index, Value) {',
  26111. ' };',
  26112. '});',
  26113. 'this.w = 0;',
  26114. 'this.b = false;',
  26115. '']),
  26116. LinesToStr([ // $mod.$main
  26117. '$mod.b = $mod.THelper.GetItems.call({',
  26118. ' p: $mod,',
  26119. ' get: function () {',
  26120. ' return this.p.w;',
  26121. ' },',
  26122. ' set: function (v) {',
  26123. ' this.p.w = v;',
  26124. ' }',
  26125. '}, 1);',
  26126. '$mod.THelper.SetItems.call({',
  26127. ' p: $mod,',
  26128. ' get: function () {',
  26129. ' return this.p.w;',
  26130. ' },',
  26131. ' set: function (v) {',
  26132. ' this.p.w = v;',
  26133. ' }',
  26134. '}, 2, $mod.b);',
  26135. 'var $with = $mod.w;',
  26136. '$mod.b = $mod.THelper.GetItems.call({',
  26137. ' get: function () {',
  26138. ' return $with;',
  26139. ' },',
  26140. ' set: function (v) {',
  26141. ' $with = v;',
  26142. ' }',
  26143. '}, 3);',
  26144. '$mod.THelper.SetItems.call({',
  26145. ' get: function () {',
  26146. ' return $with;',
  26147. ' },',
  26148. ' set: function (v) {',
  26149. ' $with = v;',
  26150. ' }',
  26151. '}, 4, $mod.b);',
  26152. '']));
  26153. end;
  26154. procedure TTestModule.TestTypeHelper_ClassProperty;
  26155. begin
  26156. StartProgram(false);
  26157. Add([
  26158. '{$modeswitch typehelpers}',
  26159. 'type',
  26160. ' THelper = type helper for word',
  26161. ' class function GetSize: longint; static;',
  26162. ' class procedure SetSize(Value: longint); static;',
  26163. ' class property Size: longint read GetSize write SetSize;',
  26164. ' end;',
  26165. 'class function THelper.GetSize: longint;',
  26166. 'begin',
  26167. ' Result:=Size+1;',
  26168. ' Size:=2;',
  26169. 'end;',
  26170. 'class procedure THelper.SetSize(Value: longint);',
  26171. 'begin',
  26172. 'end;',
  26173. 'begin',
  26174. '']);
  26175. ConvertProgram;
  26176. CheckSource('TestTypeHelper_ClassProperty',
  26177. LinesToStr([ // statements
  26178. 'rtl.createHelper(this, "THelper", null, function () {',
  26179. ' this.GetSize = function () {',
  26180. ' var Result = 0;',
  26181. ' Result = $mod.THelper.GetSize() + 1;',
  26182. ' $mod.THelper.SetSize(2);',
  26183. ' return Result;',
  26184. ' };',
  26185. ' this.SetSize = function (Value) {',
  26186. ' };',
  26187. '});',
  26188. '']),
  26189. LinesToStr([ // $mod.$main
  26190. '']));
  26191. end;
  26192. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  26193. begin
  26194. StartProgram(false);
  26195. Add([
  26196. '{$modeswitch typehelpers}',
  26197. 'type',
  26198. ' THelper = type helper for word',
  26199. ' class function GetItems(Index: byte): boolean; static;',
  26200. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  26201. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  26202. ' end;',
  26203. 'class function THelper.GetItems(Index: byte): boolean;',
  26204. 'begin',
  26205. ' Result:=Items[1];',
  26206. ' Items[2]:=false;',
  26207. 'end;',
  26208. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  26209. 'begin',
  26210. 'end;',
  26211. 'var',
  26212. ' w: word;',
  26213. ' b: boolean;',
  26214. 'begin',
  26215. ' b:=w.Items[1];',
  26216. ' w.Items[2]:=b;',
  26217. ' with w do begin',
  26218. ' b:=Items[3];',
  26219. ' Items[4]:=b;',
  26220. ' end;',
  26221. '']);
  26222. ConvertProgram;
  26223. CheckSource('TestTypeHelper_ClassProperty_Array',
  26224. LinesToStr([ // statements
  26225. 'rtl.createHelper(this, "THelper", null, function () {',
  26226. ' this.GetItems = function (Index) {',
  26227. ' var Result = false;',
  26228. ' Result = $mod.THelper.GetItems(1);',
  26229. ' $mod.THelper.SetItems(2, false);',
  26230. ' return Result;',
  26231. ' };',
  26232. ' this.SetItems = function (Index, Value) {',
  26233. ' };',
  26234. '});',
  26235. 'this.w = 0;',
  26236. 'this.b = false;',
  26237. '']),
  26238. LinesToStr([ // $mod.$main
  26239. '$mod.b = $mod.THelper.GetItems(1);',
  26240. '$mod.THelper.SetItems(2, $mod.b);',
  26241. 'var $with = $mod.w;',
  26242. '$mod.b = $mod.THelper.GetItems(3);',
  26243. '$mod.THelper.SetItems(4, $mod.b);',
  26244. '']));
  26245. end;
  26246. procedure TTestModule.TestTypeHelper_ClassMethod;
  26247. begin
  26248. StartProgram(false);
  26249. Add([
  26250. '{$modeswitch typehelpers}',
  26251. 'type',
  26252. ' THelper = type helper for word',
  26253. ' class procedure DoStatic; static;',
  26254. ' end;',
  26255. 'class procedure THelper.DoStatic;',
  26256. 'begin',
  26257. ' DoStatic;',
  26258. ' DoStatic();',
  26259. 'end;',
  26260. 'var w: word;',
  26261. 'begin',
  26262. ' w.DoStatic;',
  26263. ' w.DoStatic();',
  26264. '']);
  26265. ConvertProgram;
  26266. CheckSource('TestTypeHelper_ClassMethod',
  26267. LinesToStr([ // statements
  26268. 'rtl.createHelper(this, "THelper", null, function () {',
  26269. ' this.DoStatic = function () {',
  26270. ' $mod.THelper.DoStatic();',
  26271. ' $mod.THelper.DoStatic();',
  26272. ' };',
  26273. '});',
  26274. 'this.w = 0;',
  26275. '']),
  26276. LinesToStr([ // $mod.$main
  26277. '$mod.THelper.DoStatic();',
  26278. '$mod.THelper.DoStatic();',
  26279. '']));
  26280. end;
  26281. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  26282. begin
  26283. StartProgram(false);
  26284. Add([
  26285. '{$modeswitch typehelpers}',
  26286. 'type',
  26287. ' THelper = type helper for word',
  26288. ' procedure Run; external name ''Run'';',
  26289. ' end;',
  26290. 'var w: word;',
  26291. 'begin',
  26292. ' w.Run;',
  26293. '']);
  26294. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  26295. ConvertProgram;
  26296. end;
  26297. procedure TTestModule.TestTypeHelper_Constructor;
  26298. begin
  26299. StartProgram(false);
  26300. Add([
  26301. '{$modeswitch typehelpers}',
  26302. 'type',
  26303. ' THelper = type helper for word',
  26304. ' constructor Init(e: longint);',
  26305. ' end;',
  26306. 'constructor THelper.Init(e: longint);',
  26307. 'begin',
  26308. ' Self:=e;',
  26309. ' Init(e+1);',
  26310. 'end;',
  26311. 'var w: word;',
  26312. 'begin',
  26313. ' w:=word.Init(2);',
  26314. ' w:=w.Init(3);',
  26315. ' with word do w:=Init(4);',
  26316. ' with w do w:=Init(5);',
  26317. '']);
  26318. ConvertProgram;
  26319. CheckSource('TestTypeHelper_Constructor',
  26320. LinesToStr([ // statements
  26321. 'rtl.createHelper(this, "THelper", null, function () {',
  26322. ' this.Init = function (e) {',
  26323. ' this.set(e);',
  26324. ' $mod.THelper.Init.call(this, e + 1);',
  26325. ' return this.get();',
  26326. ' };',
  26327. ' this.$new = function (fn, args) {',
  26328. ' return this[fn].apply({',
  26329. ' p: 0,',
  26330. ' get: function () {',
  26331. ' return this.p;',
  26332. ' },',
  26333. ' set: function (v) {',
  26334. ' this.p = v;',
  26335. ' }',
  26336. ' }, args);',
  26337. ' };',
  26338. '});',
  26339. 'this.w = 0;',
  26340. '']),
  26341. LinesToStr([ // $mod.$main
  26342. '$mod.w = $mod.THelper.$new("Init", [2]);',
  26343. '$mod.w = $mod.THelper.Init.call({',
  26344. ' p: $mod,',
  26345. ' get: function () {',
  26346. ' return this.p.w;',
  26347. ' },',
  26348. ' set: function (v) {',
  26349. ' this.p.w = v;',
  26350. ' }',
  26351. '}, 3);',
  26352. '$mod.w = $mod.THelper.$new("Init", [4]);',
  26353. 'var $with = $mod.w;',
  26354. '$mod.w = $mod.THelper.Init.call({',
  26355. ' get: function () {',
  26356. ' return $with;',
  26357. ' },',
  26358. ' set: function (v) {',
  26359. ' $with = v;',
  26360. ' }',
  26361. '}, 5);',
  26362. '']));
  26363. end;
  26364. procedure TTestModule.TestTypeHelper_Word;
  26365. begin
  26366. StartProgram(false);
  26367. Add([
  26368. '{$modeswitch typehelpers}',
  26369. 'type',
  26370. ' THelper = type helper for word',
  26371. ' procedure DoIt(e: byte = 123);',
  26372. ' end;',
  26373. 'procedure THelper.DoIt(e: byte);',
  26374. 'begin',
  26375. ' Self:=e;',
  26376. ' Self:=Self+1;',
  26377. ' with Self do Doit;',
  26378. 'end;',
  26379. 'begin',
  26380. ' word(3).DoIt;',
  26381. '']);
  26382. ConvertProgram;
  26383. CheckSource('TestTypeHelper_Word',
  26384. LinesToStr([ // statements
  26385. 'rtl.createHelper(this, "THelper", null, function () {',
  26386. ' this.DoIt = function (e) {',
  26387. ' this.set(e);',
  26388. ' this.set(this.get() + 1);',
  26389. ' var $with = this.get();',
  26390. ' $mod.THelper.DoIt.call(this, 123);',
  26391. ' };',
  26392. '});',
  26393. '']),
  26394. LinesToStr([ // $mod.$main
  26395. '$mod.THelper.DoIt.call({',
  26396. ' get: function () {',
  26397. ' return 3;',
  26398. ' },',
  26399. ' set: function (v) {',
  26400. ' rtl.raiseE("EPropReadOnly");',
  26401. ' }',
  26402. '}, 123);',
  26403. '']));
  26404. end;
  26405. procedure TTestModule.TestTypeHelper_Boolean;
  26406. begin
  26407. StartProgram(false);
  26408. Add([
  26409. '{$modeswitch typehelpers}',
  26410. 'type',
  26411. ' Integer = longint;',
  26412. ' THelper = type helper for boolean',
  26413. ' procedure Run(e: wordbool = true);',
  26414. ' end;',
  26415. 'procedure THelper.Run(e: wordbool);',
  26416. 'begin',
  26417. ' Self:=e;',
  26418. ' Self:=not Self;',
  26419. ' with Self do Run;',
  26420. ' if Integer(Self)=0 then ;',
  26421. 'end;',
  26422. 'begin',
  26423. ' boolean(3).Run;',
  26424. '']);
  26425. ConvertProgram;
  26426. CheckSource('TestTypeHelper_Boolean',
  26427. LinesToStr([ // statements
  26428. 'rtl.createHelper(this, "THelper", null, function () {',
  26429. ' this.Run = function (e) {',
  26430. ' this.set(e);',
  26431. ' this.set(!this.get());',
  26432. ' var $with = this.get();',
  26433. ' $mod.THelper.Run.call(this, true);',
  26434. ' if ((this.get() ? 1 : 0) === 0) ;',
  26435. ' };',
  26436. '});',
  26437. '']),
  26438. LinesToStr([ // $mod.$main
  26439. '$mod.THelper.Run.call({',
  26440. ' a: 3 != 0,',
  26441. ' get: function () {',
  26442. ' return this.a;',
  26443. ' },',
  26444. ' set: function (v) {',
  26445. ' rtl.raiseE("EPropReadOnly");',
  26446. ' }',
  26447. '}, true);',
  26448. '']));
  26449. end;
  26450. procedure TTestModule.TestTypeHelper_WordBool;
  26451. begin
  26452. StartProgram(false);
  26453. Add([
  26454. '{$modeswitch typehelpers}',
  26455. 'type',
  26456. ' Integer = longint;',
  26457. ' THelper = type helper for WordBool',
  26458. ' procedure Run(e: wordbool = true);',
  26459. ' end;',
  26460. 'procedure THelper.Run(e: wordbool);',
  26461. 'var i: integer;',
  26462. 'begin',
  26463. ' i:=Integer(Self);',
  26464. 'end;',
  26465. 'var w: wordbool;',
  26466. 'begin',
  26467. ' w.Run;',
  26468. ' wordbool(3).Run;',
  26469. '']);
  26470. ConvertProgram;
  26471. CheckSource('TestTypeHelper_WordBool',
  26472. LinesToStr([ // statements
  26473. 'rtl.createHelper(this, "THelper", null, function () {',
  26474. ' this.Run = function (e) {',
  26475. ' var i = 0;',
  26476. ' i = (this.get() ? 1 : 0);',
  26477. ' };',
  26478. '});',
  26479. 'this.w = false;',
  26480. '']),
  26481. LinesToStr([ // $mod.$main
  26482. '$mod.THelper.Run.call({',
  26483. ' p: $mod,',
  26484. ' get: function () {',
  26485. ' return this.p.w;',
  26486. ' },',
  26487. ' set: function (v) {',
  26488. ' this.p.w = v;',
  26489. ' }',
  26490. '}, true);',
  26491. '$mod.THelper.Run.call({',
  26492. ' a: 3 != 0,',
  26493. ' get: function () {',
  26494. ' return this.a;',
  26495. ' },',
  26496. ' set: function (v) {',
  26497. ' rtl.raiseE("EPropReadOnly");',
  26498. ' }',
  26499. '}, true);',
  26500. '']));
  26501. end;
  26502. procedure TTestModule.TestTypeHelper_Double;
  26503. begin
  26504. StartProgram(false);
  26505. Add([
  26506. '{$modeswitch typehelpers}',
  26507. 'type',
  26508. ' Float = type double;',
  26509. ' THelper = type helper for Float',
  26510. ' const NPI = 3.141592;',
  26511. ' function ToStr: String;',
  26512. ' end;',
  26513. 'function THelper.ToStr: String;',
  26514. 'begin',
  26515. 'end;',
  26516. 'procedure DoIt(s: string);',
  26517. 'begin',
  26518. 'end;',
  26519. 'var f: Float;',
  26520. 'begin',
  26521. ' DoIt(f.toStr);',
  26522. ' DoIt(f.toStr());',
  26523. ' (f*f).toStr;',
  26524. ' DoIt((f*f).toStr);',
  26525. '']);
  26526. ConvertProgram;
  26527. CheckSource('TestTypeHelper_Double',
  26528. LinesToStr([ // statements
  26529. 'rtl.createHelper(this, "THelper", null, function () {',
  26530. ' this.NPI = 3.141592;',
  26531. ' this.ToStr = function () {',
  26532. ' var Result = "";',
  26533. ' return Result;',
  26534. ' };',
  26535. '});',
  26536. 'this.DoIt = function (s) {',
  26537. '};',
  26538. 'this.f = 0.0;',
  26539. '']),
  26540. LinesToStr([ // $mod.$main
  26541. '$mod.DoIt($mod.THelper.ToStr.call({',
  26542. ' p: $mod,',
  26543. ' get: function () {',
  26544. ' return this.p.f;',
  26545. ' },',
  26546. ' set: function (v) {',
  26547. ' this.p.f = v;',
  26548. ' }',
  26549. '}));',
  26550. '$mod.DoIt($mod.THelper.ToStr.call({',
  26551. ' p: $mod,',
  26552. ' get: function () {',
  26553. ' return this.p.f;',
  26554. ' },',
  26555. ' set: function (v) {',
  26556. ' this.p.f = v;',
  26557. ' }',
  26558. '}));',
  26559. '$mod.THelper.ToStr.call({',
  26560. ' a: $mod.f * $mod.f,',
  26561. ' get: function () {',
  26562. ' return this.a;',
  26563. ' },',
  26564. ' set: function (v) {',
  26565. ' rtl.raiseE("EPropReadOnly");',
  26566. ' }',
  26567. '});',
  26568. '$mod.DoIt($mod.THelper.ToStr.call({',
  26569. ' a: $mod.f * $mod.f,',
  26570. ' get: function () {',
  26571. ' return this.a;',
  26572. ' },',
  26573. ' set: function (v) {',
  26574. ' rtl.raiseE("EPropReadOnly");',
  26575. ' }',
  26576. '}));',
  26577. '']));
  26578. end;
  26579. procedure TTestModule.TestTypeHelper_NativeInt;
  26580. begin
  26581. StartProgram(false);
  26582. Add([
  26583. '{$modeswitch typehelpers}',
  26584. 'type',
  26585. ' MaxInt = type nativeint;',
  26586. ' THelperI = type helper for MaxInt',
  26587. ' function ToStr: String;',
  26588. ' end;',
  26589. ' MaxUInt = type nativeuint;',
  26590. ' THelperU = type helper for MaxUInt',
  26591. ' function ToStr: String;',
  26592. ' end;',
  26593. 'function THelperI.ToStr: String;',
  26594. 'begin',
  26595. ' Result:=str(Self);',
  26596. 'end;',
  26597. 'function THelperU.ToStr: String;',
  26598. 'begin',
  26599. ' Result:=str(Self);',
  26600. 'end;',
  26601. 'procedure DoIt(s: string);',
  26602. 'begin',
  26603. 'end;',
  26604. 'var i: MaxInt;',
  26605. 'begin',
  26606. ' DoIt(i.toStr);',
  26607. ' DoIt(i.toStr());',
  26608. ' (i*i).toStr;',
  26609. ' DoIt((i*i).toStr);',
  26610. '']);
  26611. ConvertProgram;
  26612. CheckSource('TestTypeHelper_NativeInt',
  26613. LinesToStr([ // statements
  26614. 'rtl.createHelper(this, "THelperI", null, function () {',
  26615. ' this.ToStr = function () {',
  26616. ' var Result = "";',
  26617. ' Result = "" + this.get();',
  26618. ' return Result;',
  26619. ' };',
  26620. '});',
  26621. 'rtl.createHelper(this, "THelperU", null, function () {',
  26622. ' this.ToStr = function () {',
  26623. ' var Result = "";',
  26624. ' Result = "" + this.get();',
  26625. ' return Result;',
  26626. ' };',
  26627. '});',
  26628. 'this.DoIt = function (s) {',
  26629. '};',
  26630. 'this.i = 0;',
  26631. '']),
  26632. LinesToStr([ // $mod.$main
  26633. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26634. ' p: $mod,',
  26635. ' get: function () {',
  26636. ' return this.p.i;',
  26637. ' },',
  26638. ' set: function (v) {',
  26639. ' this.p.i = v;',
  26640. ' }',
  26641. '}));',
  26642. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26643. ' p: $mod,',
  26644. ' get: function () {',
  26645. ' return this.p.i;',
  26646. ' },',
  26647. ' set: function (v) {',
  26648. ' this.p.i = v;',
  26649. ' }',
  26650. '}));',
  26651. '$mod.THelperI.ToStr.call({',
  26652. ' a: $mod.i * $mod.i,',
  26653. ' get: function () {',
  26654. ' return this.a;',
  26655. ' },',
  26656. ' set: function (v) {',
  26657. ' rtl.raiseE("EPropReadOnly");',
  26658. ' }',
  26659. '});',
  26660. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26661. ' a: $mod.i * $mod.i,',
  26662. ' get: function () {',
  26663. ' return this.a;',
  26664. ' },',
  26665. ' set: function (v) {',
  26666. ' rtl.raiseE("EPropReadOnly");',
  26667. ' }',
  26668. '}));',
  26669. '']));
  26670. end;
  26671. procedure TTestModule.TestTypeHelper_StringChar;
  26672. begin
  26673. StartProgram(false);
  26674. Add([
  26675. '{$modeswitch typehelpers}',
  26676. 'type',
  26677. ' TStringHelper = type helper for string',
  26678. ' procedure DoIt(e: byte = 123);',
  26679. ' end;',
  26680. ' TCharHelper = type helper for char',
  26681. ' procedure Fly;',
  26682. ' end;',
  26683. 'procedure TStringHelper.DoIt(e: byte);',
  26684. 'begin',
  26685. ' Self[1]:=''c'';',
  26686. ' Self[2]:=Self[3];',
  26687. 'end;',
  26688. 'procedure TCharHelper.Fly;',
  26689. 'begin',
  26690. ' Self:=''c'';',
  26691. 'end;',
  26692. 'begin',
  26693. ' ''abc''.DoIt;',
  26694. ' ''xyz''.DoIt();',
  26695. ' ''c''.Fly();',
  26696. '']);
  26697. ConvertProgram;
  26698. CheckSource('TestTypeHelper_StringChar',
  26699. LinesToStr([ // statements
  26700. 'rtl.createHelper(this, "TStringHelper", null, function () {',
  26701. ' this.DoIt = function (e) {',
  26702. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  26703. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  26704. ' };',
  26705. '});',
  26706. 'rtl.createHelper(this, "TCharHelper", null, function () {',
  26707. ' this.Fly = function () {',
  26708. ' this.set("c");',
  26709. ' };',
  26710. '});',
  26711. '']),
  26712. LinesToStr([ // $mod.$main
  26713. '$mod.TStringHelper.DoIt.call({',
  26714. ' get: function () {',
  26715. ' return "abc";',
  26716. ' },',
  26717. ' set: function (v) {',
  26718. ' rtl.raiseE("EPropReadOnly");',
  26719. ' }',
  26720. '}, 123);',
  26721. '$mod.TStringHelper.DoIt.call({',
  26722. ' get: function () {',
  26723. ' return "xyz";',
  26724. ' },',
  26725. ' set: function (v) {',
  26726. ' rtl.raiseE("EPropReadOnly");',
  26727. ' }',
  26728. '}, 123);',
  26729. '$mod.TCharHelper.Fly.call({',
  26730. ' get: function () {',
  26731. ' return "c";',
  26732. ' },',
  26733. ' set: function (v) {',
  26734. ' rtl.raiseE("EPropReadOnly");',
  26735. ' }',
  26736. '});',
  26737. '']));
  26738. end;
  26739. procedure TTestModule.TestTypeHelper_JSValue;
  26740. begin
  26741. StartProgram(false);
  26742. Add([
  26743. '{$modeswitch typehelpers}',
  26744. 'type',
  26745. ' TExtValue = type jsvalue;',
  26746. ' THelper = type helper for TExtValue',
  26747. ' function ToStr: String;',
  26748. ' end;',
  26749. 'function THelper.ToStr: String;',
  26750. 'begin',
  26751. 'end;',
  26752. 'var',
  26753. ' s: string;',
  26754. ' v: TExtValue;',
  26755. 'begin',
  26756. ' s:=v.toStr;',
  26757. ' s:=v.toStr();',
  26758. ' TExtValue(s).toStr;',
  26759. '']);
  26760. ConvertProgram;
  26761. CheckSource('TestTypeHelper_JSValue',
  26762. LinesToStr([ // statements
  26763. 'rtl.createHelper(this, "THelper", null, function () {',
  26764. ' this.ToStr = function () {',
  26765. ' var Result = "";',
  26766. ' return Result;',
  26767. ' };',
  26768. '});',
  26769. 'this.s = "";',
  26770. 'this.v = undefined;',
  26771. '']),
  26772. LinesToStr([ // $mod.$main
  26773. '$mod.s = $mod.THelper.ToStr.call({',
  26774. ' p: $mod,',
  26775. ' get: function () {',
  26776. ' return this.p.v;',
  26777. ' },',
  26778. ' set: function (v) {',
  26779. ' this.p.v = v;',
  26780. ' }',
  26781. '});',
  26782. '$mod.s = $mod.THelper.ToStr.call({',
  26783. ' p: $mod,',
  26784. ' get: function () {',
  26785. ' return this.p.v;',
  26786. ' },',
  26787. ' set: function (v) {',
  26788. ' this.p.v = v;',
  26789. ' }',
  26790. '});',
  26791. '$mod.THelper.ToStr.call({',
  26792. ' p: $mod,',
  26793. ' get: function () {',
  26794. ' return this.p.s;',
  26795. ' },',
  26796. ' set: function (v) {',
  26797. ' rtl.raiseE("EPropReadOnly");',
  26798. ' }',
  26799. '});',
  26800. '']));
  26801. end;
  26802. procedure TTestModule.TestTypeHelper_Array;
  26803. begin
  26804. StartProgram(false);
  26805. Add([
  26806. '{$modeswitch typehelpers}',
  26807. 'type',
  26808. ' TArrOfBool = array of boolean;',
  26809. ' TArrOfJS = array of jsvalue;',
  26810. ' THelper = type helper for TArrOfBool',
  26811. ' procedure DoIt(e: byte = 123);',
  26812. ' end;',
  26813. 'procedure THelper.DoIt(e: byte);',
  26814. 'begin',
  26815. ' Self[1]:=true;',
  26816. ' Self[2]:=not Self[3];',
  26817. ' SetLength(Self,4);',
  26818. ' Self:=Concat(Self,[true]);',
  26819. 'end;',
  26820. 'var',
  26821. ' b: TArrOfBool;',
  26822. ' j: TArrOfJS;',
  26823. 'begin',
  26824. ' b.DoIt;',
  26825. ' TArrOfBool(j).DoIt();',
  26826. '']);
  26827. ConvertProgram;
  26828. CheckSource('TestTypeHelper_Array',
  26829. LinesToStr([ // statements
  26830. 'rtl.createHelper(this, "THelper", null, function () {',
  26831. ' this.DoIt = function (e) {',
  26832. ' this.get()[1] = true;',
  26833. ' this.get()[2] = !this.get()[3];',
  26834. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  26835. ' this.set(rtl.arrayPushN(this.get(), true));',
  26836. ' };',
  26837. '});',
  26838. 'this.b = [];',
  26839. 'this.j = [];',
  26840. '']),
  26841. LinesToStr([ // $mod.$main
  26842. '$mod.THelper.DoIt.call({',
  26843. ' p: $mod,',
  26844. ' get: function () {',
  26845. ' return this.p.b;',
  26846. ' },',
  26847. ' set: function (v) {',
  26848. ' this.p.b = v;',
  26849. ' }',
  26850. '}, 123);',
  26851. '$mod.THelper.DoIt.call({',
  26852. ' p: $mod,',
  26853. ' get: function () {',
  26854. ' return this.p.j;',
  26855. ' },',
  26856. ' set: function (v) {',
  26857. ' this.p.j = v;',
  26858. ' }',
  26859. '}, 123);',
  26860. '']));
  26861. end;
  26862. procedure TTestModule.TestTypeHelper_EnumType;
  26863. begin
  26864. StartProgram(false);
  26865. Add([
  26866. '{$modeswitch typehelpers}',
  26867. 'type',
  26868. ' TEnum = (red,blue);',
  26869. ' THelper = type helper for TEnum',
  26870. ' procedure DoIt(e: byte = 123);',
  26871. ' class procedure Swing(w: word); static;',
  26872. ' end;',
  26873. 'procedure THelper.DoIt(e: byte);',
  26874. 'begin',
  26875. ' Self:=red;',
  26876. ' Self:=succ(Self);',
  26877. ' with Self do Doit;',
  26878. 'end;',
  26879. 'class procedure THelper.Swing(w: word);',
  26880. 'begin',
  26881. 'end;',
  26882. 'var e: TEnum;',
  26883. 'begin',
  26884. ' e.DoIt;',
  26885. ' red.DoIt;',
  26886. ' TEnum.blue.DoIt;',
  26887. ' TEnum(1).DoIt;',
  26888. ' TEnum.Swing(3);',
  26889. '']);
  26890. ConvertProgram;
  26891. CheckSource('TestTypeHelper_EnumType',
  26892. LinesToStr([ // statements
  26893. 'this.TEnum = {',
  26894. ' "0": "red",',
  26895. ' red: 0,',
  26896. ' "1": "blue",',
  26897. ' blue: 1',
  26898. '};',
  26899. 'rtl.createHelper(this, "THelper", null, function () {',
  26900. ' this.DoIt = function (e) {',
  26901. ' this.set($mod.TEnum.red);',
  26902. ' this.set(this.get() + 1);',
  26903. ' var $with = this.get();',
  26904. ' $mod.THelper.DoIt.call(this, 123);',
  26905. ' };',
  26906. ' this.Swing = function (w) {',
  26907. ' };',
  26908. '});',
  26909. 'this.e = 0;',
  26910. '']),
  26911. LinesToStr([ // $mod.$main
  26912. '$mod.THelper.DoIt.call({',
  26913. ' p: $mod,',
  26914. ' get: function () {',
  26915. ' return this.p.e;',
  26916. ' },',
  26917. ' set: function (v) {',
  26918. ' this.p.e = v;',
  26919. ' }',
  26920. '}, 123);',
  26921. '$mod.THelper.DoIt.call({',
  26922. ' p: $mod.TEnum,',
  26923. ' get: function () {',
  26924. ' return this.p.red;',
  26925. ' },',
  26926. ' set: function (v) {',
  26927. ' rtl.raiseE("EPropReadOnly");',
  26928. ' }',
  26929. '}, 123);',
  26930. '$mod.THelper.DoIt.call({',
  26931. ' p: $mod.TEnum,',
  26932. ' get: function () {',
  26933. ' return this.p.blue;',
  26934. ' },',
  26935. ' set: function (v) {',
  26936. ' rtl.raiseE("EPropReadOnly");',
  26937. ' }',
  26938. '}, 123);',
  26939. '$mod.THelper.DoIt.call({',
  26940. ' get: function () {',
  26941. ' return 1;',
  26942. ' },',
  26943. ' set: function (v) {',
  26944. ' rtl.raiseE("EPropReadOnly");',
  26945. ' }',
  26946. '}, 123);',
  26947. '$mod.THelper.Swing(3);',
  26948. '']));
  26949. end;
  26950. procedure TTestModule.TestTypeHelper_SetType;
  26951. begin
  26952. StartProgram(false);
  26953. Add([
  26954. '{$modeswitch typehelpers}',
  26955. 'type',
  26956. ' TEnum = (red,blue);',
  26957. ' TSetOfEnum = set of TEnum;',
  26958. ' THelper = type helper for TSetOfEnum',
  26959. ' procedure DoIt(e: byte = 123);',
  26960. ' constructor Init(e: TEnum);',
  26961. ' constructor InitEmpty;',
  26962. ' end;',
  26963. 'procedure THelper.DoIt(e: byte);',
  26964. 'begin',
  26965. ' Self:=[];',
  26966. ' Self:=[red];',
  26967. ' Include(Self,blue);',
  26968. 'end;',
  26969. 'constructor THelper.Init(e: TEnum);',
  26970. 'begin',
  26971. ' Self:=[];',
  26972. ' Self:=[e];',
  26973. ' Include(Self,blue);',
  26974. 'end;',
  26975. 'constructor THelper.InitEmpty;',
  26976. 'begin',
  26977. 'end;',
  26978. 'var s: TSetOfEnum;',
  26979. 'begin',
  26980. ' s.DoIt;',
  26981. //' [red].DoIt;',
  26982. //' with s do DoIt;',
  26983. //' with [red,blue] do DoIt;',
  26984. ' s:=TSetOfEnum.Init(blue);',
  26985. ' s:=s.Init(blue);',
  26986. '']);
  26987. ConvertProgram;
  26988. CheckSource('TestTypeHelper_SetType',
  26989. LinesToStr([ // statements
  26990. 'this.TEnum = {',
  26991. ' "0": "red",',
  26992. ' red: 0,',
  26993. ' "1": "blue",',
  26994. ' blue: 1',
  26995. '};',
  26996. 'rtl.createHelper(this, "THelper", null, function () {',
  26997. ' this.DoIt = function (e) {',
  26998. ' this.set({});',
  26999. ' this.set(rtl.createSet($mod.TEnum.red));',
  27000. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  27001. ' };',
  27002. ' this.Init = function (e) {',
  27003. ' this.set({});',
  27004. ' this.set(rtl.createSet(e));',
  27005. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  27006. ' return this.get();',
  27007. ' };',
  27008. ' this.InitEmpty = function () {',
  27009. ' return this.get();',
  27010. ' };',
  27011. ' this.$new = function (fn, args) {',
  27012. ' return this[fn].apply({',
  27013. ' p: {},',
  27014. ' get: function () {',
  27015. ' return this.p;',
  27016. ' },',
  27017. ' set: function (v) {',
  27018. ' this.p = v;',
  27019. ' }',
  27020. ' }, args);',
  27021. ' };',
  27022. '});',
  27023. 'this.s = {};',
  27024. '']),
  27025. LinesToStr([ // $mod.$main
  27026. '$mod.THelper.DoIt.call({',
  27027. ' p: $mod,',
  27028. ' get: function () {',
  27029. ' return this.p.s;',
  27030. ' },',
  27031. ' set: function (v) {',
  27032. ' this.p.s = v;',
  27033. ' }',
  27034. '}, 123);',
  27035. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  27036. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  27037. ' p: $mod,',
  27038. ' get: function () {',
  27039. ' return this.p.s;',
  27040. ' },',
  27041. ' set: function (v) {',
  27042. ' this.p.s = v;',
  27043. ' }',
  27044. '}, $mod.TEnum.blue));',
  27045. '']));
  27046. end;
  27047. procedure TTestModule.TestTypeHelper_InterfaceType;
  27048. begin
  27049. StartProgram(false);
  27050. Add([
  27051. '{$interfaces com}',
  27052. '{$modeswitch typehelpers}',
  27053. 'type',
  27054. ' IUnknown = interface',
  27055. ' function _AddRef: longint;',
  27056. ' function _Release: longint;',
  27057. ' end;',
  27058. ' TObject = class(IUnknown)',
  27059. ' function _AddRef: longint; virtual; abstract;',
  27060. ' function _Release: longint; virtual; abstract;',
  27061. ' end;',
  27062. ' THelper = type helper for IUnknown',
  27063. ' procedure Fly(e: byte = 123);',
  27064. ' class procedure Run; static;',
  27065. ' end;',
  27066. 'var',
  27067. ' i: IUnknown;',
  27068. ' o: TObject;',
  27069. 'procedure THelper.Fly(e: byte);',
  27070. 'begin',
  27071. ' i:=Self;',
  27072. ' o:=Self as TObject;',
  27073. ' Self:=nil;',
  27074. ' Self:=i;',
  27075. ' Self:=o;',
  27076. ' with Self do begin',
  27077. ' Fly;',
  27078. ' Fly();',
  27079. ' end;',
  27080. 'end;',
  27081. 'class procedure THelper.Run;',
  27082. 'var l: IUnknown;',
  27083. 'begin',
  27084. ' l.Fly;',
  27085. ' l.Fly();',
  27086. 'end;',
  27087. 'begin',
  27088. ' i.Fly;',
  27089. ' i.Fly();',
  27090. ' i.Run;',
  27091. ' i.Run();',
  27092. ' IUnknown.Run;',
  27093. ' IUnknown.Run();',
  27094. '']);
  27095. ConvertProgram;
  27096. CheckSource('TestTypeHelper_InterfaceType',
  27097. LinesToStr([ // statements
  27098. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  27099. 'rtl.createClass(this, "TObject", null, function () {',
  27100. ' this.$init = function () {',
  27101. ' };',
  27102. ' this.$final = function () {',
  27103. ' };',
  27104. ' rtl.addIntf(this, $mod.IUnknown);',
  27105. '});',
  27106. 'rtl.createHelper(this, "THelper", null, function () {',
  27107. ' this.Fly = function (e) {',
  27108. ' var $ir = rtl.createIntfRefs();',
  27109. ' try {',
  27110. ' rtl.setIntfP($mod, "i", this.get());',
  27111. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  27112. ' this.set(null);',
  27113. ' this.set($mod.i);',
  27114. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  27115. ' var $with = this.get();',
  27116. ' $mod.THelper.Fly.call(this, 123);',
  27117. ' $mod.THelper.Fly.call(this, 123);',
  27118. ' } finally {',
  27119. ' $ir.free();',
  27120. ' };',
  27121. ' };',
  27122. ' this.Run = function () {',
  27123. ' var l = null;',
  27124. ' try {',
  27125. ' $mod.THelper.Fly.call({',
  27126. ' get: function () {',
  27127. ' return l;',
  27128. ' },',
  27129. ' set: function (v) {',
  27130. ' l = rtl.setIntfL(l, v);',
  27131. ' }',
  27132. ' }, 123);',
  27133. ' $mod.THelper.Fly.call({',
  27134. ' get: function () {',
  27135. ' return l;',
  27136. ' },',
  27137. ' set: function (v) {',
  27138. ' l = rtl.setIntfL(l, v);',
  27139. ' }',
  27140. ' }, 123);',
  27141. ' } finally {',
  27142. ' rtl._Release(l);',
  27143. ' };',
  27144. ' };',
  27145. '});',
  27146. 'this.i = null;',
  27147. 'this.o = null;',
  27148. '']),
  27149. LinesToStr([ // $mod.$main
  27150. '$mod.THelper.Fly.call({',
  27151. ' p: $mod,',
  27152. ' get: function () {',
  27153. ' return this.p.i;',
  27154. ' },',
  27155. ' set: function (v) {',
  27156. ' rtl.setIntfP(this.p, "i", v);',
  27157. ' }',
  27158. '}, 123);',
  27159. '$mod.THelper.Fly.call({',
  27160. ' p: $mod,',
  27161. ' get: function () {',
  27162. ' return this.p.i;',
  27163. ' },',
  27164. ' set: function (v) {',
  27165. ' rtl.setIntfP(this.p, "i", v);',
  27166. ' }',
  27167. '}, 123);',
  27168. '$mod.THelper.Run();',
  27169. '$mod.THelper.Run();',
  27170. '$mod.THelper.Run();',
  27171. '$mod.THelper.Run();',
  27172. '']));
  27173. end;
  27174. procedure TTestModule.TestTypeHelper_NestedSelf;
  27175. begin
  27176. StartProgram(false);
  27177. Add([
  27178. '{$modeswitch typehelpers}',
  27179. 'type',
  27180. ' THelper = type helper for string',
  27181. ' procedure Run(Value: string);',
  27182. ' end;',
  27183. 'procedure THelper.Run(Value: string);',
  27184. ' function Sub(i: nativeint): boolean;',
  27185. ' begin',
  27186. ' Result:=Self[i+1]=Value[i];',
  27187. ' end;',
  27188. 'begin',
  27189. ' if Self[3]=Value[4] then ;',
  27190. 'end;',
  27191. 'begin',
  27192. '']);
  27193. ConvertProgram;
  27194. CheckSource('TestTypeHelper_NestedSelf',
  27195. LinesToStr([ // statements
  27196. 'rtl.createHelper(this, "THelper", null, function () {',
  27197. ' this.Run = function (Value) {',
  27198. ' var $Self = this;',
  27199. ' function Sub(i) {',
  27200. ' var Result = false;',
  27201. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  27202. ' return Result;',
  27203. ' };',
  27204. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  27205. ' };',
  27206. '});',
  27207. '']),
  27208. LinesToStr([ // $mod.$main
  27209. '']));
  27210. end;
  27211. procedure TTestModule.TestProcType;
  27212. begin
  27213. StartProgram(false);
  27214. Add([
  27215. 'type',
  27216. ' TProcInt = procedure(vI: longint = 1);',
  27217. 'procedure DoIt(vJ: longint);',
  27218. 'begin end;',
  27219. 'var',
  27220. ' b: boolean;',
  27221. ' vP, vQ: tprocint;',
  27222. 'begin',
  27223. ' vp:=nil;',
  27224. ' vp:=vp;',
  27225. ' vp:=@doit;',
  27226. ' vp;',
  27227. ' vp();',
  27228. ' vp(2);',
  27229. ' b:=vp=nil;',
  27230. ' b:=nil=vp;',
  27231. ' b:=vp=vq;',
  27232. ' b:=vp=@doit;',
  27233. ' b:=@doit=vp;',
  27234. ' b:=vp<>nil;',
  27235. ' b:=nil<>vp;',
  27236. ' b:=vp<>vq;',
  27237. ' b:=vp<>@doit;',
  27238. ' b:=@doit<>vp;',
  27239. ' b:=Assigned(vp);',
  27240. ' if Assigned(vp) then ;']);
  27241. ConvertProgram;
  27242. CheckSource('TestProcType',
  27243. LinesToStr([ // statements
  27244. 'this.DoIt = function(vJ) {',
  27245. '};',
  27246. 'this.b = false;',
  27247. 'this.vP = null;',
  27248. 'this.vQ = null;'
  27249. ]),
  27250. LinesToStr([ // $mod.$main
  27251. '$mod.vP = null;',
  27252. '$mod.vP = $mod.vP;',
  27253. '$mod.vP = $mod.DoIt;',
  27254. '$mod.vP(1);',
  27255. '$mod.vP(1);',
  27256. '$mod.vP(2);',
  27257. '$mod.b = $mod.vP === null;',
  27258. '$mod.b = null === $mod.vP;',
  27259. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  27260. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27261. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27262. '$mod.b = $mod.vP !== null;',
  27263. '$mod.b = null !== $mod.vP;',
  27264. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  27265. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27266. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27267. '$mod.b = $mod.vP != null;',
  27268. 'if ($mod.vP != null) ;',
  27269. '']));
  27270. end;
  27271. procedure TTestModule.TestProcType_Arg;
  27272. begin
  27273. StartProgram(false);
  27274. Add([
  27275. 'type',
  27276. ' TProcInt = procedure(vI: longint = 1);',
  27277. 'procedure DoIt(vJ: longint); begin end;',
  27278. 'procedure DoSome(vP, vQ: TProcInt);',
  27279. 'var',
  27280. ' b: boolean;',
  27281. 'begin',
  27282. ' vp:=nil;',
  27283. ' vp:=vp;',
  27284. ' vp:=@doit;',
  27285. ' vp;',
  27286. ' vp();',
  27287. ' vp(2);',
  27288. ' b:=vp=nil;',
  27289. ' b:=nil=vp;',
  27290. ' b:=vp=vq;',
  27291. ' b:=vp=@doit;',
  27292. ' b:=@doit=vp;',
  27293. ' b:=vp<>nil;',
  27294. ' b:=nil<>vp;',
  27295. ' b:=vp<>vq;',
  27296. ' b:=vp<>@doit;',
  27297. ' b:=@doit<>vp;',
  27298. ' b:=Assigned(vp);',
  27299. ' if Assigned(vp) then ;',
  27300. 'end;',
  27301. 'begin',
  27302. ' DoSome(@DoIt,nil);']);
  27303. ConvertProgram;
  27304. CheckSource('TestProcType_Arg',
  27305. LinesToStr([ // statements
  27306. 'this.DoIt = function(vJ) {',
  27307. '};',
  27308. 'this.DoSome = function(vP, vQ) {',
  27309. ' var b = false;',
  27310. ' vP = null;',
  27311. ' vP = vP;',
  27312. ' vP = $mod.DoIt;',
  27313. ' vP(1);',
  27314. ' vP(1);',
  27315. ' vP(2);',
  27316. ' b = vP === null;',
  27317. ' b = null === vP;',
  27318. ' b = rtl.eqCallback(vP,vQ);',
  27319. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  27320. ' b = rtl.eqCallback($mod.DoIt, vP);',
  27321. ' b = vP !== null;',
  27322. ' b = null !== vP;',
  27323. ' b = !rtl.eqCallback(vP, vQ);',
  27324. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  27325. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  27326. ' b = vP != null;',
  27327. ' if (vP != null) ;',
  27328. '};',
  27329. '']),
  27330. LinesToStr([ // $mod.$main
  27331. '$mod.DoSome($mod.DoIt,null);',
  27332. '']));
  27333. end;
  27334. procedure TTestModule.TestProcType_FunctionFPC;
  27335. begin
  27336. StartProgram(false);
  27337. Add('type');
  27338. Add(' TFuncInt = function(vA: longint = 1): longint;');
  27339. Add('function DoIt(vI: longint): longint;');
  27340. Add('begin end;');
  27341. Add('var');
  27342. Add(' b: boolean;');
  27343. Add(' vP, vQ: tfuncint;');
  27344. Add('begin');
  27345. Add(' vp:=nil;');
  27346. Add(' vp:=vp;');
  27347. Add(' vp:=@doit;'); // ok in fpc and delphi
  27348. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  27349. Add(' vp;'); // ok in fpc and delphi
  27350. Add(' vp();');
  27351. Add(' vp(2);');
  27352. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  27353. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  27354. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  27355. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  27356. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  27357. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  27358. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  27359. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  27360. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  27361. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27362. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27363. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27364. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  27365. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  27366. Add(' b:=Assigned(vp);');
  27367. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  27368. Add(' doit(vp());'); // ok in fpc and delphi
  27369. Add(' doit(vp(2));'); // ok in fpc and delphi
  27370. ConvertProgram;
  27371. CheckSource('TestProcType_FunctionFPC',
  27372. LinesToStr([ // statements
  27373. 'this.DoIt = function(vI) {',
  27374. ' var Result = 0;',
  27375. ' return Result;',
  27376. '};',
  27377. 'this.b = false;',
  27378. 'this.vP = null;',
  27379. 'this.vQ = null;'
  27380. ]),
  27381. LinesToStr([ // $mod.$main
  27382. '$mod.vP = null;',
  27383. '$mod.vP = $mod.vP;',
  27384. '$mod.vP = $mod.DoIt;',
  27385. '$mod.vP(1);',
  27386. '$mod.vP(1);',
  27387. '$mod.vP(2);',
  27388. '$mod.b = $mod.vP === null;',
  27389. '$mod.b = null === $mod.vP;',
  27390. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  27391. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27392. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27393. '$mod.b = 4 === $mod.vP(1);',
  27394. '$mod.b = $mod.vP !== null;',
  27395. '$mod.b = null !== $mod.vP;',
  27396. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  27397. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27398. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27399. '$mod.b = 6 !== $mod.vP(1);',
  27400. '$mod.b = $mod.vP != null;',
  27401. '$mod.DoIt($mod.vP(1));',
  27402. '$mod.DoIt($mod.vP(2));',
  27403. '']));
  27404. end;
  27405. procedure TTestModule.TestProcType_FunctionDelphi;
  27406. begin
  27407. StartProgram(false);
  27408. Add('{$mode Delphi}');
  27409. Add('type');
  27410. Add(' TFuncInt = function(vA: longint = 1): longint;');
  27411. Add('function DoIt(vI: longint): longint;');
  27412. Add('begin end;');
  27413. Add('var');
  27414. Add(' b: boolean;');
  27415. Add(' vP, vQ: tfuncint;');
  27416. Add('begin');
  27417. Add(' vp:=nil;');
  27418. Add(' vp:=vp;');
  27419. Add(' vp:=@doit;'); // ok in fpc and delphi
  27420. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  27421. Add(' vp;'); // ok in fpc and delphi
  27422. Add(' vp();');
  27423. Add(' vp(2);');
  27424. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  27425. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  27426. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  27427. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  27428. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  27429. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  27430. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  27431. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  27432. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  27433. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27434. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27435. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27436. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  27437. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  27438. Add(' b:=Assigned(vp);');
  27439. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  27440. Add(' doit(vp());'); // ok in fpc and delphi
  27441. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  27442. ConvertProgram;
  27443. CheckSource('TestProcType_FunctionDelphi',
  27444. LinesToStr([ // statements
  27445. 'this.DoIt = function(vI) {',
  27446. ' var Result = 0;',
  27447. ' return Result;',
  27448. '};',
  27449. 'this.b = false;',
  27450. 'this.vP = null;',
  27451. 'this.vQ = null;'
  27452. ]),
  27453. LinesToStr([ // $mod.$main
  27454. '$mod.vP = null;',
  27455. '$mod.vP = $mod.vP;',
  27456. '$mod.vP = $mod.DoIt;',
  27457. '$mod.vP = $mod.DoIt;',
  27458. '$mod.vP(1);',
  27459. '$mod.vP(1);',
  27460. '$mod.vP(2);',
  27461. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  27462. '$mod.b = $mod.vP(1) === 3;',
  27463. '$mod.b = 4 === $mod.vP(1);',
  27464. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  27465. '$mod.b = $mod.vP(1) !== 5;',
  27466. '$mod.b = 6 !== $mod.vP(1);',
  27467. '$mod.b = $mod.vP != null;',
  27468. '$mod.DoIt($mod.vP(1));',
  27469. '$mod.DoIt($mod.vP(1));',
  27470. '$mod.DoIt($mod.vP(2));',
  27471. '']));
  27472. end;
  27473. procedure TTestModule.TestProcType_ProcedureDelphi;
  27474. begin
  27475. StartProgram(false);
  27476. Add('{$mode Delphi}');
  27477. Add('type');
  27478. Add(' TProc = procedure;');
  27479. Add('procedure DoIt;');
  27480. Add('begin end;');
  27481. Add('var');
  27482. Add(' b: boolean;');
  27483. Add(' vP, vQ: tproc;');
  27484. Add('begin');
  27485. Add(' vp:=nil;');
  27486. Add(' vp:=vp;');
  27487. Add(' vp:=vq;');
  27488. 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
  27489. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  27490. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  27491. Add(' vp;'); // ok in fpc and delphi
  27492. Add(' vp();');
  27493. // equal
  27494. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  27495. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  27496. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  27497. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  27498. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  27499. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  27500. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  27501. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  27502. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  27503. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  27504. // unequal
  27505. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  27506. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  27507. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  27508. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  27509. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27510. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  27511. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27512. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  27513. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27514. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  27515. Add(' b:=Assigned(vp);');
  27516. ConvertProgram;
  27517. CheckSource('TestProcType_ProcedureDelphi',
  27518. LinesToStr([ // statements
  27519. 'this.DoIt = function() {',
  27520. '};',
  27521. 'this.b = false;',
  27522. 'this.vP = null;',
  27523. 'this.vQ = null;'
  27524. ]),
  27525. LinesToStr([ // $mod.$main
  27526. '$mod.vP = null;',
  27527. '$mod.vP = $mod.vP;',
  27528. '$mod.vP = $mod.vQ;',
  27529. '$mod.vP = $mod.DoIt;',
  27530. '$mod.vP = $mod.DoIt;',
  27531. '$mod.vP();',
  27532. '$mod.vP();',
  27533. '$mod.b = $mod.vP === null;',
  27534. '$mod.b = null === $mod.vP;',
  27535. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  27536. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27537. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27538. '$mod.b = $mod.vP !== null;',
  27539. '$mod.b = null !== $mod.vP;',
  27540. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  27541. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27542. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27543. '$mod.b = $mod.vP != null;',
  27544. '']));
  27545. end;
  27546. procedure TTestModule.TestProcType_AsParam;
  27547. begin
  27548. StartProgram(false);
  27549. Add('type');
  27550. Add(' TFuncInt = function(vA: longint = 1): longint;');
  27551. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  27552. Add('var vJ: tfuncint;');
  27553. Add('begin');
  27554. Add(' vg:=vg;');
  27555. Add(' vj:=vh;');
  27556. Add(' vi:=vi;');
  27557. Add(' doit(vg,vg,vg);');
  27558. Add(' doit(vh,vh,vj);');
  27559. Add(' doit(vi,vi,vi);');
  27560. Add(' doit(vj,vj,vj);');
  27561. Add('end;');
  27562. Add('var i: tfuncint;');
  27563. Add('begin');
  27564. Add(' doit(i,i,i);');
  27565. ConvertProgram;
  27566. CheckSource('TestProcType_AsParam',
  27567. LinesToStr([ // statements
  27568. 'this.DoIt = function (vG,vH,vI) {',
  27569. ' var vJ = null;',
  27570. ' vG = vG;',
  27571. ' vJ = vH;',
  27572. ' vI.set(vI.get());',
  27573. ' $mod.DoIt(vG, vG, {',
  27574. ' get: function () {',
  27575. ' return vG;',
  27576. ' },',
  27577. ' set: function (v) {',
  27578. ' vG = v;',
  27579. ' }',
  27580. ' });',
  27581. ' $mod.DoIt(vH, vH, {',
  27582. ' get: function () {',
  27583. ' return vJ;',
  27584. ' },',
  27585. ' set: function (v) {',
  27586. ' vJ = v;',
  27587. ' }',
  27588. ' });',
  27589. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  27590. ' $mod.DoIt(vJ, vJ, {',
  27591. ' get: function () {',
  27592. ' return vJ;',
  27593. ' },',
  27594. ' set: function (v) {',
  27595. ' vJ = v;',
  27596. ' }',
  27597. ' });',
  27598. '};',
  27599. 'this.i = null;'
  27600. ]),
  27601. LinesToStr([
  27602. '$mod.DoIt($mod.i,$mod.i,{',
  27603. ' p: $mod,',
  27604. ' get: function () {',
  27605. ' return this.p.i;',
  27606. ' },',
  27607. ' set: function (v) {',
  27608. ' this.p.i = v;',
  27609. ' }',
  27610. '});'
  27611. ]));
  27612. end;
  27613. procedure TTestModule.TestProcType_MethodFPC;
  27614. begin
  27615. StartProgram(false);
  27616. Add('type');
  27617. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27618. Add(' TObject = class');
  27619. Add(' function DoIt(vA: longint = 1): longint;');
  27620. Add(' end;');
  27621. Add('function TObject.DoIt(vA: longint = 1): longint;');
  27622. Add('begin');
  27623. Add('end;');
  27624. Add('var');
  27625. Add(' Obj: TObject;');
  27626. Add(' vP: tfuncint;');
  27627. Add(' b: boolean;');
  27628. Add('begin');
  27629. Add(' vp:[email protected];'); // ok in fpc and delphi
  27630. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  27631. Add(' vp;'); // ok in fpc and delphi
  27632. Add(' vp();');
  27633. Add(' vp(2);');
  27634. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  27635. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  27636. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  27637. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  27638. ConvertProgram;
  27639. CheckSource('TestProcType_MethodFPC',
  27640. LinesToStr([ // statements
  27641. 'rtl.createClass(this, "TObject", null, function () {',
  27642. ' this.$init = function () {',
  27643. ' };',
  27644. ' this.$final = function () {',
  27645. ' };',
  27646. ' this.DoIt = function (vA) {',
  27647. ' var Result = 0;',
  27648. ' return Result;',
  27649. ' };',
  27650. '});',
  27651. 'this.Obj = null;',
  27652. 'this.vP = null;',
  27653. 'this.b = false;'
  27654. ]),
  27655. LinesToStr([
  27656. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27657. '$mod.vP(1);',
  27658. '$mod.vP(1);',
  27659. '$mod.vP(2);',
  27660. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  27661. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  27662. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  27663. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  27664. '']));
  27665. end;
  27666. procedure TTestModule.TestProcType_MethodDelphi;
  27667. begin
  27668. StartProgram(false);
  27669. Add([
  27670. '{$mode delphi}',
  27671. 'type',
  27672. ' TFuncInt = function(vA: longint = 1): longint of object;',
  27673. ' TObject = class',
  27674. ' function DoIt(vA: longint = 1): longint;',
  27675. ' end;',
  27676. 'function TObject.DoIt(vA: longint = 1): longint;',
  27677. 'begin',
  27678. 'end;',
  27679. 'var',
  27680. ' Obj: TObject;',
  27681. ' vP: tfuncint;',
  27682. ' b: boolean;',
  27683. 'begin',
  27684. ' vp:[email protected];', // ok in fpc and delphi
  27685. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  27686. ' vp;', // ok in fpc and delphi
  27687. ' vp();',
  27688. ' vp(2);',
  27689. //' b:[email protected];', // ok in fpc, illegal in delphi
  27690. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  27691. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  27692. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  27693. '']);
  27694. ConvertProgram;
  27695. CheckSource('TestProcType_MethodDelphi',
  27696. LinesToStr([ // statements
  27697. 'rtl.createClass(this, "TObject", null, function () {',
  27698. ' this.$init = function () {',
  27699. ' };',
  27700. ' this.$final = function () {',
  27701. ' };',
  27702. ' this.DoIt = function (vA) {',
  27703. ' var Result = 0;',
  27704. ' return Result;',
  27705. ' };',
  27706. '});',
  27707. 'this.Obj = null;',
  27708. 'this.vP = null;',
  27709. 'this.b = false;'
  27710. ]),
  27711. LinesToStr([
  27712. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27713. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27714. '$mod.vP(1);',
  27715. '$mod.vP(1);',
  27716. '$mod.vP(2);',
  27717. '']));
  27718. end;
  27719. procedure TTestModule.TestProcType_PropertyFPC;
  27720. begin
  27721. StartProgram(false);
  27722. Add('type');
  27723. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27724. Add(' TObject = class');
  27725. Add(' FOnFoo: TFuncInt;');
  27726. Add(' function DoIt(vA: longint = 1): longint;');
  27727. Add(' function GetFoo: TFuncInt;');
  27728. Add(' procedure SetFoo(const Value: TFuncInt);');
  27729. Add(' function GetEvents(Index: longint): TFuncInt;');
  27730. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  27731. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27732. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27733. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  27734. Add(' end;');
  27735. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27736. Add('function tobject.getfoo: tfuncint; begin end;');
  27737. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27738. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  27739. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  27740. Add('var');
  27741. Add(' Obj: TObject;');
  27742. Add(' vP: tfuncint;');
  27743. Add(' b: boolean;');
  27744. Add('begin');
  27745. Add(' obj.onfoo:=nil;');
  27746. Add(' obj.onbar:=nil;');
  27747. Add(' obj.events[1]:=nil;');
  27748. Add(' obj.onfoo:=obj.onfoo;');
  27749. Add(' obj.onbar:=obj.onbar;');
  27750. Add(' obj.events[2]:=obj.events[3];');
  27751. Add(' obj.onfoo:[email protected];');
  27752. Add(' obj.onbar:[email protected];');
  27753. Add(' obj.events[4]:[email protected];');
  27754. //Add(' obj.onfoo:=obj.doit;'); // delphi
  27755. //Add(' obj.onbar:=obj.doit;'); // delphi
  27756. //Add(' obj.events[4]:=obj.doit;'); // delphi
  27757. Add(' obj.onfoo;');
  27758. Add(' obj.onbar;');
  27759. //Add(' obj.events[5];'); ToDo in pasresolver
  27760. Add(' obj.onfoo();');
  27761. Add(' obj.onbar();');
  27762. Add(' obj.events[6]();');
  27763. Add(' b:=obj.onfoo=nil;');
  27764. Add(' b:=obj.onbar=nil;');
  27765. Add(' b:=obj.events[7]=nil;');
  27766. Add(' b:=obj.onfoo<>nil;');
  27767. Add(' b:=obj.onbar<>nil;');
  27768. Add(' b:=obj.events[8]<>nil;');
  27769. Add(' b:=obj.onfoo=vp;');
  27770. Add(' b:=obj.onbar=vp;');
  27771. Add(' b:=obj.events[9]=vp;');
  27772. Add(' b:=obj.onfoo=obj.onfoo;');
  27773. Add(' b:=obj.onbar=obj.onfoo;');
  27774. Add(' b:=obj.events[10]=obj.onfoo;');
  27775. Add(' b:=obj.onfoo<>obj.onfoo;');
  27776. Add(' b:=obj.onbar<>obj.onfoo;');
  27777. Add(' b:=obj.events[11]<>obj.onfoo;');
  27778. Add(' b:[email protected];');
  27779. Add(' b:[email protected];');
  27780. Add(' b:=obj.events[12][email protected];');
  27781. Add(' b:=obj.onfoo<>@obj.doit;');
  27782. Add(' b:=obj.onbar<>@obj.doit;');
  27783. Add(' b:=obj.events[12]<>@obj.doit;');
  27784. Add(' b:=Assigned(obj.onfoo);');
  27785. Add(' b:=Assigned(obj.onbar);');
  27786. Add(' b:=Assigned(obj.events[13]);');
  27787. ConvertProgram;
  27788. CheckSource('TestProcType_PropertyFPC',
  27789. LinesToStr([ // statements
  27790. 'rtl.createClass(this, "TObject", null, function () {',
  27791. ' this.$init = function () {',
  27792. ' this.FOnFoo = null;',
  27793. ' };',
  27794. ' this.$final = function () {',
  27795. ' this.FOnFoo = undefined;',
  27796. ' };',
  27797. ' this.DoIt = function (vA) {',
  27798. ' var Result = 0;',
  27799. ' return Result;',
  27800. ' };',
  27801. 'this.GetFoo = function () {',
  27802. ' var Result = null;',
  27803. ' return Result;',
  27804. '};',
  27805. 'this.SetFoo = function (Value) {',
  27806. '};',
  27807. 'this.GetEvents = function (Index) {',
  27808. ' var Result = null;',
  27809. ' return Result;',
  27810. '};',
  27811. 'this.SetEvents = function (Index, Value) {',
  27812. '};',
  27813. '});',
  27814. 'this.Obj = null;',
  27815. 'this.vP = null;',
  27816. 'this.b = false;'
  27817. ]),
  27818. LinesToStr([
  27819. '$mod.Obj.FOnFoo = null;',
  27820. '$mod.Obj.SetFoo(null);',
  27821. '$mod.Obj.SetEvents(1, null);',
  27822. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  27823. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  27824. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  27825. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27826. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27827. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27828. '$mod.Obj.FOnFoo(1);',
  27829. '$mod.Obj.GetFoo();',
  27830. '$mod.Obj.FOnFoo(1);',
  27831. '$mod.Obj.GetFoo()(1);',
  27832. '$mod.Obj.GetEvents(6)(1);',
  27833. '$mod.b = $mod.Obj.FOnFoo === null;',
  27834. '$mod.b = $mod.Obj.GetFoo() === null;',
  27835. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  27836. '$mod.b = $mod.Obj.FOnFoo !== null;',
  27837. '$mod.b = $mod.Obj.GetFoo() !== null;',
  27838. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  27839. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  27840. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  27841. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  27842. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  27843. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  27844. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  27845. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  27846. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  27847. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  27848. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  27849. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  27850. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  27851. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  27852. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  27853. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  27854. '$mod.b = $mod.Obj.FOnFoo != null;',
  27855. '$mod.b = $mod.Obj.GetFoo() != null;',
  27856. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  27857. '']));
  27858. end;
  27859. procedure TTestModule.TestProcType_PropertyDelphi;
  27860. begin
  27861. StartProgram(false);
  27862. Add('{$mode delphi}');
  27863. Add('type');
  27864. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27865. Add(' TObject = class');
  27866. Add(' FOnFoo: TFuncInt;');
  27867. Add(' function DoIt(vA: longint = 1): longint;');
  27868. Add(' function GetFoo: TFuncInt;');
  27869. Add(' procedure SetFoo(const Value: TFuncInt);');
  27870. Add(' function GetEvents(Index: longint): TFuncInt;');
  27871. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  27872. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27873. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27874. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  27875. Add(' end;');
  27876. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27877. Add('function tobject.getfoo: tfuncint; begin end;');
  27878. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27879. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  27880. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  27881. Add('var');
  27882. Add(' Obj: TObject;');
  27883. Add(' vP: tfuncint;');
  27884. Add(' b: boolean;');
  27885. Add('begin');
  27886. Add(' obj.onfoo:=nil;');
  27887. Add(' obj.onbar:=nil;');
  27888. Add(' obj.events[1]:=nil;');
  27889. Add(' obj.onfoo:=obj.onfoo;');
  27890. Add(' obj.onbar:=obj.onbar;');
  27891. Add(' obj.events[2]:=obj.events[3];');
  27892. Add(' obj.onfoo:[email protected];');
  27893. Add(' obj.onbar:[email protected];');
  27894. Add(' obj.events[4]:[email protected];');
  27895. Add(' obj.onfoo:=obj.doit;'); // delphi
  27896. Add(' obj.onbar:=obj.doit;'); // delphi
  27897. Add(' obj.events[4]:=obj.doit;'); // delphi
  27898. Add(' obj.onfoo;');
  27899. Add(' obj.onbar;');
  27900. //Add(' obj.events[5];'); ToDo in pasresolver
  27901. Add(' obj.onfoo();');
  27902. Add(' obj.onbar();');
  27903. Add(' obj.events[6]();');
  27904. //Add(' b:=obj.onfoo=nil;'); // fpc
  27905. //Add(' b:=obj.onbar=nil;'); // fpc
  27906. //Add(' b:=obj.events[7]=nil;'); // fpc
  27907. //Add(' b:=obj.onfoo<>nil;'); // fpc
  27908. //Add(' b:=obj.onbar<>nil;'); // fpc
  27909. //Add(' b:=obj.events[8]<>nil;'); // fpc
  27910. Add(' b:=obj.onfoo=vp;');
  27911. Add(' b:=obj.onbar=vp;');
  27912. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  27913. Add(' b:=obj.onfoo=obj.onfoo;');
  27914. Add(' b:=obj.onbar=obj.onfoo;');
  27915. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  27916. Add(' b:=obj.onfoo<>obj.onfoo;');
  27917. Add(' b:=obj.onbar<>obj.onfoo;');
  27918. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  27919. //Add(' b:[email protected];'); // fpc
  27920. //Add(' b:[email protected];'); // fpc
  27921. //Add(' b:=obj.events[12][email protected];'); // fpc
  27922. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  27923. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  27924. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  27925. Add(' b:=Assigned(obj.onfoo);');
  27926. Add(' b:=Assigned(obj.onbar);');
  27927. Add(' b:=Assigned(obj.events[13]);');
  27928. ConvertProgram;
  27929. CheckSource('TestProcType_PropertyDelphi',
  27930. LinesToStr([ // statements
  27931. 'rtl.createClass(this, "TObject", null, function () {',
  27932. ' this.$init = function () {',
  27933. ' this.FOnFoo = null;',
  27934. ' };',
  27935. ' this.$final = function () {',
  27936. ' this.FOnFoo = undefined;',
  27937. ' };',
  27938. ' this.DoIt = function (vA) {',
  27939. ' var Result = 0;',
  27940. ' return Result;',
  27941. ' };',
  27942. 'this.GetFoo = function () {',
  27943. ' var Result = null;',
  27944. ' return Result;',
  27945. '};',
  27946. 'this.SetFoo = function (Value) {',
  27947. '};',
  27948. 'this.GetEvents = function (Index) {',
  27949. ' var Result = null;',
  27950. ' return Result;',
  27951. '};',
  27952. 'this.SetEvents = function (Index, Value) {',
  27953. '};',
  27954. '});',
  27955. 'this.Obj = null;',
  27956. 'this.vP = null;',
  27957. 'this.b = false;'
  27958. ]),
  27959. LinesToStr([
  27960. '$mod.Obj.FOnFoo = null;',
  27961. '$mod.Obj.SetFoo(null);',
  27962. '$mod.Obj.SetEvents(1, null);',
  27963. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  27964. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  27965. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  27966. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27967. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27968. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27969. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27970. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27971. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27972. '$mod.Obj.FOnFoo(1);',
  27973. '$mod.Obj.GetFoo();',
  27974. '$mod.Obj.FOnFoo(1);',
  27975. '$mod.Obj.GetFoo()(1);',
  27976. '$mod.Obj.GetEvents(6)(1);',
  27977. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  27978. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  27979. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  27980. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  27981. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  27982. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  27983. '$mod.b = $mod.Obj.FOnFoo != null;',
  27984. '$mod.b = $mod.Obj.GetFoo() != null;',
  27985. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  27986. '']));
  27987. end;
  27988. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  27989. begin
  27990. StartProgram(false);
  27991. Add('type');
  27992. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27993. Add(' TObject = class');
  27994. Add(' FOnFoo: TFuncInt;');
  27995. Add(' function DoIt(vA: longint = 1): longint;');
  27996. Add(' function GetFoo: TFuncInt;');
  27997. Add(' procedure SetFoo(const Value: TFuncInt);');
  27998. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27999. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  28000. Add(' end;');
  28001. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  28002. Add('function tobject.getfoo: tfuncint; begin end;');
  28003. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  28004. Add('var');
  28005. Add(' Obj: TObject;');
  28006. Add(' vP: tfuncint;');
  28007. Add(' b: boolean;');
  28008. Add('begin');
  28009. Add('with obj do begin');
  28010. Add(' fonfoo:=nil;');
  28011. Add(' onfoo:=nil;');
  28012. Add(' onbar:=nil;');
  28013. Add(' fonfoo:=fonfoo;');
  28014. Add(' onfoo:=onfoo;');
  28015. Add(' onbar:=onbar;');
  28016. Add(' fonfoo:=@doit;');
  28017. Add(' onfoo:=@doit;');
  28018. Add(' onbar:=@doit;');
  28019. //Add(' fonfoo:=doit;'); // delphi
  28020. //Add(' onfoo:=doit;'); // delphi
  28021. //Add(' onbar:=doit;'); // delphi
  28022. Add(' fonfoo;');
  28023. Add(' onfoo;');
  28024. Add(' onbar;');
  28025. Add(' fonfoo();');
  28026. Add(' onfoo();');
  28027. Add(' onbar();');
  28028. Add(' b:=fonfoo=nil;');
  28029. Add(' b:=onfoo=nil;');
  28030. Add(' b:=onbar=nil;');
  28031. Add(' b:=fonfoo<>nil;');
  28032. Add(' b:=onfoo<>nil;');
  28033. Add(' b:=onbar<>nil;');
  28034. Add(' b:=fonfoo=vp;');
  28035. Add(' b:=onfoo=vp;');
  28036. Add(' b:=onbar=vp;');
  28037. Add(' b:=fonfoo=fonfoo;');
  28038. Add(' b:=onfoo=onfoo;');
  28039. Add(' b:=onbar=onfoo;');
  28040. Add(' b:=fonfoo<>fonfoo;');
  28041. Add(' b:=onfoo<>onfoo;');
  28042. Add(' b:=onbar<>onfoo;');
  28043. Add(' b:=fonfoo=@doit;');
  28044. Add(' b:=onfoo=@doit;');
  28045. Add(' b:=onbar=@doit;');
  28046. Add(' b:=fonfoo<>@doit;');
  28047. Add(' b:=onfoo<>@doit;');
  28048. Add(' b:=onbar<>@doit;');
  28049. Add(' b:=Assigned(fonfoo);');
  28050. Add(' b:=Assigned(onfoo);');
  28051. Add(' b:=Assigned(onbar);');
  28052. Add('end;');
  28053. ConvertProgram;
  28054. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  28055. LinesToStr([ // statements
  28056. 'rtl.createClass(this, "TObject", null, function () {',
  28057. ' this.$init = function () {',
  28058. ' this.FOnFoo = null;',
  28059. ' };',
  28060. ' this.$final = function () {',
  28061. ' this.FOnFoo = undefined;',
  28062. ' };',
  28063. ' this.DoIt = function (vA) {',
  28064. ' var Result = 0;',
  28065. ' return Result;',
  28066. ' };',
  28067. ' this.GetFoo = function () {',
  28068. ' var Result = null;',
  28069. ' return Result;',
  28070. ' };',
  28071. ' this.SetFoo = function (Value) {',
  28072. ' };',
  28073. '});',
  28074. 'this.Obj = null;',
  28075. 'this.vP = null;',
  28076. 'this.b = false;'
  28077. ]),
  28078. LinesToStr([
  28079. 'var $with = $mod.Obj;',
  28080. '$with.FOnFoo = null;',
  28081. '$with.FOnFoo = null;',
  28082. '$with.SetFoo(null);',
  28083. '$with.FOnFoo = $with.FOnFoo;',
  28084. '$with.FOnFoo = $with.FOnFoo;',
  28085. '$with.SetFoo($with.GetFoo());',
  28086. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  28087. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  28088. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  28089. '$with.FOnFoo(1);',
  28090. '$with.FOnFoo(1);',
  28091. '$with.GetFoo();',
  28092. '$with.FOnFoo(1);',
  28093. '$with.FOnFoo(1);',
  28094. '$with.GetFoo()(1);',
  28095. '$mod.b = $with.FOnFoo === null;',
  28096. '$mod.b = $with.FOnFoo === null;',
  28097. '$mod.b = $with.GetFoo() === null;',
  28098. '$mod.b = $with.FOnFoo !== null;',
  28099. '$mod.b = $with.FOnFoo !== null;',
  28100. '$mod.b = $with.GetFoo() !== null;',
  28101. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  28102. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  28103. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  28104. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28105. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28106. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  28107. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28108. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28109. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  28110. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28111. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28112. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  28113. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28114. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28115. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  28116. '$mod.b = $with.FOnFoo != null;',
  28117. '$mod.b = $with.FOnFoo != null;',
  28118. '$mod.b = $with.GetFoo() != null;',
  28119. '']));
  28120. end;
  28121. procedure TTestModule.TestProcType_Nested;
  28122. begin
  28123. StartProgram(false);
  28124. Add([
  28125. 'type',
  28126. ' TProcInt = procedure(vI: longint = 1);',
  28127. 'procedure DoIt(vJ: longint);',
  28128. 'var aProc: TProcInt;',
  28129. ' b: boolean;',
  28130. ' procedure Sub(vK: longint);',
  28131. ' var aSub: TProcInt;',
  28132. ' procedure SubSub(vK: longint);',
  28133. ' var aSubSub: TProcInt;',
  28134. ' begin;',
  28135. ' aProc:=@DoIt;',
  28136. ' aSub:=@DoIt;',
  28137. ' aSubSub:=@DoIt;',
  28138. ' aProc:=@Sub;',
  28139. ' aSub:=@Sub;',
  28140. ' aSubSub:=@Sub;',
  28141. ' aProc:=@SubSub;',
  28142. ' aSub:=@SubSub;',
  28143. ' aSubSub:=@SubSub;',
  28144. ' end;',
  28145. ' begin;',
  28146. ' end;',
  28147. 'begin;',
  28148. ' aProc:=@Sub;',
  28149. ' b:=aProc=@Sub;',
  28150. ' b:=@Sub=aProc;',
  28151. 'end;',
  28152. 'begin',
  28153. '']);
  28154. ConvertProgram;
  28155. CheckSource('TestProcType_Nested',
  28156. LinesToStr([ // statements
  28157. 'this.DoIt = function (vJ) {',
  28158. ' var aProc = null;',
  28159. ' var b = false;',
  28160. ' function Sub(vK) {',
  28161. ' var aSub = null;',
  28162. ' function SubSub(vK) {',
  28163. ' var aSubSub = null;',
  28164. ' aProc = $mod.DoIt;',
  28165. ' aSub = $mod.DoIt;',
  28166. ' aSubSub = $mod.DoIt;',
  28167. ' aProc = Sub;',
  28168. ' aSub = Sub;',
  28169. ' aSubSub = Sub;',
  28170. ' aProc = SubSub;',
  28171. ' aSub = SubSub;',
  28172. ' aSubSub = SubSub;',
  28173. ' };',
  28174. ' };',
  28175. ' aProc = Sub;',
  28176. ' b = rtl.eqCallback(aProc, Sub);',
  28177. ' b = rtl.eqCallback(Sub, aProc);',
  28178. '};',
  28179. '']),
  28180. LinesToStr([ // $mod.$main
  28181. '']));
  28182. end;
  28183. procedure TTestModule.TestProcType_NestedOfObject;
  28184. begin
  28185. StartProgram(false);
  28186. Add([
  28187. 'type',
  28188. ' TProcInt = procedure(vI: longint = 1) of object;',
  28189. ' TObject = class',
  28190. ' procedure DoIt(vJ: longint);',
  28191. ' end;',
  28192. 'procedure TObject.DoIt(vJ: longint);',
  28193. 'var aProc: TProcInt;',
  28194. ' b: boolean;',
  28195. ' procedure Sub(vK: longint);',
  28196. ' var aSub: TProcInt;',
  28197. ' procedure SubSub(vK: longint);',
  28198. ' var aSubSub: TProcInt;',
  28199. ' begin;',
  28200. ' aProc:=@DoIt;',
  28201. ' aSub:=@DoIt;',
  28202. ' aSubSub:=@DoIt;',
  28203. ' aProc:=@Sub;',
  28204. ' aSub:=@Sub;',
  28205. ' aSubSub:=@Sub;',
  28206. ' aProc:=@SubSub;',
  28207. ' aSub:=@SubSub;',
  28208. ' aSubSub:=@SubSub;',
  28209. ' end;',
  28210. ' begin;',
  28211. ' end;',
  28212. 'begin;',
  28213. ' aProc:=@Sub;',
  28214. ' b:=aProc=@Sub;',
  28215. ' b:=@Sub=aProc;',
  28216. 'end;',
  28217. 'begin',
  28218. '']);
  28219. ConvertProgram;
  28220. CheckSource('TestProcType_Nested',
  28221. LinesToStr([ // statements
  28222. 'rtl.createClass(this, "TObject", null, function () {',
  28223. ' this.$init = function () {',
  28224. ' };',
  28225. ' this.$final = function () {',
  28226. ' };',
  28227. ' this.DoIt = function (vJ) {',
  28228. ' var $Self = this;',
  28229. ' var aProc = null;',
  28230. ' var b = false;',
  28231. ' function Sub(vK) {',
  28232. ' var aSub = null;',
  28233. ' function SubSub(vK) {',
  28234. ' var aSubSub = null;',
  28235. ' aProc = rtl.createCallback($Self, "DoIt");',
  28236. ' aSub = rtl.createCallback($Self, "DoIt");',
  28237. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  28238. ' aProc = Sub;',
  28239. ' aSub = Sub;',
  28240. ' aSubSub = Sub;',
  28241. ' aProc = SubSub;',
  28242. ' aSub = SubSub;',
  28243. ' aSubSub = SubSub;',
  28244. ' };',
  28245. ' };',
  28246. ' aProc = Sub;',
  28247. ' b = rtl.eqCallback(aProc, Sub);',
  28248. ' b = rtl.eqCallback(Sub, aProc);',
  28249. ' };',
  28250. '});',
  28251. '']),
  28252. LinesToStr([ // $mod.$main
  28253. '']));
  28254. end;
  28255. procedure TTestModule.TestProcType_ReferenceToProc;
  28256. begin
  28257. StartProgram(false);
  28258. Add([
  28259. 'type',
  28260. ' TProcRef = reference to procedure(i: longint = 0);',
  28261. ' TFuncRef = reference to function(i: longint = 0): longint;',
  28262. 'var',
  28263. ' p: TProcRef;',
  28264. ' f: TFuncRef;',
  28265. 'procedure DoIt(i: longint);',
  28266. 'begin',
  28267. 'end;',
  28268. 'function GetIt(i: longint): longint;',
  28269. 'begin',
  28270. ' p:=@DoIt;',
  28271. ' f:=@GetIt;',
  28272. ' f;',
  28273. ' f();',
  28274. ' f(1);',
  28275. 'end;',
  28276. 'begin',
  28277. ' p:=@DoIt;',
  28278. ' f:=@GetIt;',
  28279. ' f;',
  28280. ' f();',
  28281. ' f(1);',
  28282. ' p:=TProcRef(f);',
  28283. '']);
  28284. ConvertProgram;
  28285. CheckSource('TestProcType_ReferenceToProc',
  28286. LinesToStr([ // statements
  28287. 'this.p = null;',
  28288. 'this.f = null;',
  28289. 'this.DoIt = function (i) {',
  28290. '};',
  28291. 'this.GetIt = function (i) {',
  28292. ' var Result = 0;',
  28293. ' $mod.p = $mod.DoIt;',
  28294. ' $mod.f = $mod.GetIt;',
  28295. ' $mod.f(0);',
  28296. ' $mod.f(0);',
  28297. ' $mod.f(1);',
  28298. ' return Result;',
  28299. '};',
  28300. '']),
  28301. LinesToStr([ // $mod.$main
  28302. '$mod.p = $mod.DoIt;',
  28303. '$mod.f = $mod.GetIt;',
  28304. '$mod.f(0);',
  28305. '$mod.f(0);',
  28306. '$mod.f(1);',
  28307. '$mod.p = $mod.f;',
  28308. '']));
  28309. end;
  28310. procedure TTestModule.TestProcType_ReferenceToMethod;
  28311. begin
  28312. StartProgram(false);
  28313. Add([
  28314. 'type',
  28315. ' TFuncRef = reference to function(i: longint = 5): longint;',
  28316. ' TObject = class',
  28317. ' function Grow(s: longint): longint;',
  28318. ' end;',
  28319. 'var',
  28320. ' f: tfuncref;',
  28321. 'function tobject.grow(s: longint): longint;',
  28322. ' function GrowSub(i: longint): longint;',
  28323. ' begin',
  28324. ' f:=@grow;',
  28325. ' f:=@growsub;',
  28326. ' end;',
  28327. 'begin',
  28328. ' f:=@grow;',
  28329. ' f:=@growsub;',
  28330. 'end;',
  28331. 'begin',
  28332. '']);
  28333. ConvertProgram;
  28334. CheckSource('TestProcType_ReferenceToMethod',
  28335. LinesToStr([ // statements
  28336. 'rtl.createClass(this, "TObject", null, function () {',
  28337. ' this.$init = function () {',
  28338. ' };',
  28339. ' this.$final = function () {',
  28340. ' };',
  28341. ' this.Grow = function (s) {',
  28342. ' var $Self = this;',
  28343. ' var Result = 0;',
  28344. ' function GrowSub(i) {',
  28345. ' var Result = 0;',
  28346. ' $mod.f = rtl.createCallback($Self, "Grow");',
  28347. ' $mod.f = GrowSub;',
  28348. ' return Result;',
  28349. ' };',
  28350. ' $mod.f = rtl.createCallback($Self, "Grow");',
  28351. ' $mod.f = GrowSub;',
  28352. ' return Result;',
  28353. ' };',
  28354. '});',
  28355. 'this.f = null;',
  28356. '']),
  28357. LinesToStr([ // $mod.$main
  28358. '']));
  28359. end;
  28360. procedure TTestModule.TestProcType_Typecast;
  28361. begin
  28362. StartProgram(false);
  28363. Add([
  28364. 'type',
  28365. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  28366. ' TEvent = procedure of object;',
  28367. ' TGetter = function:longint of object;',
  28368. ' TProcA = procedure(i: longint);',
  28369. ' TFuncB = function(i, j: longint): longint;',
  28370. 'procedure DoIt(); varargs; begin end;',
  28371. 'var',
  28372. ' Notify: tnotifyevent;',
  28373. ' Event: tevent;',
  28374. ' Getter: tgetter;',
  28375. ' ProcA: tproca;',
  28376. ' FuncB: tfuncb;',
  28377. ' p: pointer;',
  28378. 'begin',
  28379. ' notify:=tnotifyevent(event);',
  28380. ' event:=tevent(event);',
  28381. ' event:=tevent(notify);',
  28382. ' event:=tevent(getter);',
  28383. ' event:=tevent(proca);',
  28384. ' proca:=tproca(funcb);',
  28385. ' funcb:=tfuncb(funcb);',
  28386. ' funcb:=tfuncb(proca);',
  28387. ' funcb:=tfuncb(getter);',
  28388. ' proca:=tproca(p);',
  28389. ' funcb:=tfuncb(p);',
  28390. ' getter:=tgetter(p);',
  28391. ' p:=pointer(notify);',
  28392. ' p:=notify;',
  28393. ' p:=pointer(proca);',
  28394. ' p:=proca;',
  28395. ' p:=pointer(funcb);',
  28396. ' p:=funcb;',
  28397. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  28398. '']);
  28399. ConvertProgram;
  28400. CheckSource('TestProcType_Typecast',
  28401. LinesToStr([ // statements
  28402. 'this.DoIt = function () {',
  28403. '};',
  28404. 'this.Notify = null;',
  28405. 'this.Event = null;',
  28406. 'this.Getter = null;',
  28407. 'this.ProcA = null;',
  28408. 'this.FuncB = null;',
  28409. 'this.p = null;',
  28410. '']),
  28411. LinesToStr([ // $mod.$main
  28412. '$mod.Notify = $mod.Event;',
  28413. '$mod.Event = $mod.Event;',
  28414. '$mod.Event = $mod.Notify;',
  28415. '$mod.Event = $mod.Getter;',
  28416. '$mod.Event = $mod.ProcA;',
  28417. '$mod.ProcA = $mod.FuncB;',
  28418. '$mod.FuncB = $mod.FuncB;',
  28419. '$mod.FuncB = $mod.ProcA;',
  28420. '$mod.FuncB = $mod.Getter;',
  28421. '$mod.ProcA = $mod.p;',
  28422. '$mod.FuncB = $mod.p;',
  28423. '$mod.Getter = $mod.p;',
  28424. '$mod.p = $mod.Notify;',
  28425. '$mod.p = $mod.Notify;',
  28426. '$mod.p = $mod.ProcA;',
  28427. '$mod.p = $mod.ProcA;',
  28428. '$mod.p = $mod.FuncB;',
  28429. '$mod.p = $mod.FuncB;',
  28430. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  28431. '']));
  28432. end;
  28433. procedure TTestModule.TestProcType_PassProcToUntyped;
  28434. begin
  28435. StartProgram(false);
  28436. Add([
  28437. 'type',
  28438. ' TEvent = procedure of object;',
  28439. ' TFunc = function: longint;',
  28440. 'procedure DoIt(); varargs; begin end;',
  28441. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  28442. 'var',
  28443. ' Event: tevent;',
  28444. ' Func: TFunc;',
  28445. 'begin',
  28446. ' doit(event,func);',
  28447. ' dosome(event,event,event);',
  28448. ' dosome(func,func,func);',
  28449. '']);
  28450. ConvertProgram;
  28451. CheckSource('TestProcType_PassProcToUntyped',
  28452. LinesToStr([ // statements
  28453. 'this.DoIt = function () {',
  28454. '};',
  28455. 'this.DoSome = function (a, b, p) {',
  28456. '};',
  28457. 'this.Event = null;',
  28458. 'this.Func = null;',
  28459. '']),
  28460. LinesToStr([ // $mod.$main
  28461. '$mod.DoIt($mod.Event, $mod.Func);',
  28462. '$mod.DoSome($mod.Event, {',
  28463. ' p: $mod,',
  28464. ' get: function () {',
  28465. ' return this.p.Event;',
  28466. ' },',
  28467. ' set: function (v) {',
  28468. ' this.p.Event = v;',
  28469. ' }',
  28470. '}, $mod.Event);',
  28471. '$mod.DoSome($mod.Func, {',
  28472. ' p: $mod,',
  28473. ' get: function () {',
  28474. ' return this.p.Func;',
  28475. ' },',
  28476. ' set: function (v) {',
  28477. ' this.p.Func = v;',
  28478. ' }',
  28479. '}, $mod.Func);',
  28480. '']));
  28481. end;
  28482. procedure TTestModule.TestProcType_PassProcToArray;
  28483. begin
  28484. StartProgram(false);
  28485. Add([
  28486. 'type',
  28487. ' TFunc = function: longint;',
  28488. ' TArrFunc = array of TFunc;',
  28489. 'procedure DoIt(Arr: TArrFunc); begin end;',
  28490. 'function GetIt: longint; begin end;',
  28491. 'var',
  28492. ' Func: tfunc;',
  28493. 'begin',
  28494. ' doit([]);',
  28495. ' doit([@GetIt]);',
  28496. ' doit([Func]);',
  28497. '']);
  28498. ConvertProgram;
  28499. CheckSource('TestProcType_PassProcToArray',
  28500. LinesToStr([ // statements
  28501. 'this.DoIt = function (Arr) {',
  28502. '};',
  28503. 'this.GetIt = function () {',
  28504. ' var Result = 0;',
  28505. ' return Result;',
  28506. '};',
  28507. 'this.Func = null;',
  28508. '']),
  28509. LinesToStr([ // $mod.$main
  28510. '$mod.DoIt([]);',
  28511. '$mod.DoIt([$mod.GetIt]);',
  28512. '$mod.DoIt([$mod.Func]);',
  28513. '']));
  28514. end;
  28515. procedure TTestModule.TestProcType_SafeCallObjFPC;
  28516. begin
  28517. StartProgram(false);
  28518. Add([
  28519. '{$modeswitch externalclass}',
  28520. 'type',
  28521. ' TProc = reference to procedure(i: longint); safecall;',
  28522. ' TEvent = procedure(i: longint) of object; safecall;',
  28523. ' TExtA = class external name ''ExtObj''',
  28524. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  28525. ' procedure DoSome(Id: longint = 1);',
  28526. ' procedure SetOnClick(const e: TEvent);',
  28527. ' property OnClick: TEvent write SetOnClick;',
  28528. ' class procedure Fly(Id: longint = 1); static;',
  28529. ' procedure SetOnShow(const p: TProc);',
  28530. ' property OnShow: TProc write SetOnShow;',
  28531. ' end;',
  28532. 'procedure Run(i: longint = 1);',
  28533. 'begin',
  28534. 'end;',
  28535. 'var',
  28536. ' Obj: texta;',
  28537. ' e: TEvent;',
  28538. ' p: TProc;',
  28539. 'begin',
  28540. ' e:=e;',
  28541. ' e:[email protected];',
  28542. ' e:[email protected];',
  28543. ' e:=TEvent(@obj.dosome);', // no safecall
  28544. ' obj.OnClick:[email protected];',
  28545. ' obj.OnClick:[email protected];',
  28546. ' obj.setonclick(@obj.doit);',
  28547. ' obj.setonclick(@obj.dosome);',
  28548. ' p:=@Run;',
  28549. ' p:[email protected];',
  28550. ' obj.OnShow:=@Run;',
  28551. ' obj.OnShow:[email protected];',
  28552. ' obj.setOnShow(@Run);',
  28553. ' obj.setOnShow(@TExtA.Fly);',
  28554. ' with obj do begin',
  28555. ' e:=@doit;',
  28556. ' e:=@dosome;',
  28557. ' OnClick:=@doit;',
  28558. ' OnClick:=@dosome;',
  28559. ' setonclick(@doit);',
  28560. ' setonclick(@dosome);',
  28561. ' OnShow:=@Run;',
  28562. ' setOnShow(@Run);',
  28563. ' end;']);
  28564. ConvertProgram;
  28565. CheckSource('TestProcType_SafeCallObjFPC',
  28566. LinesToStr([ // statements
  28567. 'this.Run = function (i) {',
  28568. '};',
  28569. 'this.Obj = null;',
  28570. 'this.e = null;',
  28571. 'this.p = null;',
  28572. '']),
  28573. LinesToStr([ // $mod.$main
  28574. '$mod.e = $mod.e;',
  28575. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  28576. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  28577. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  28578. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28579. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28580. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28581. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28582. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  28583. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  28584. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28585. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28586. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28587. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28588. 'var $with = $mod.Obj;',
  28589. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  28590. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  28591. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28592. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28593. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28594. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28595. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28596. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28597. '']));
  28598. end;
  28599. procedure TTestModule.TestProcType_SafeCallDelphi;
  28600. begin
  28601. StartProgram(false);
  28602. Add([
  28603. '{$mode delphi}',
  28604. '{$modeswitch externalclass}',
  28605. 'type',
  28606. ' TProc = reference to procedure(i: longint); safecall;',
  28607. ' TEvent = procedure(i: longint) of object; safecall;',
  28608. ' TExtA = class external name ''ExtObj''',
  28609. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  28610. ' procedure DoSome(Id: longint = 1);',
  28611. ' procedure SetOnClick(const e: TEvent);',
  28612. ' property OnClick: TEvent write SetOnClick;',
  28613. ' class procedure Fly(Id: longint = 1); static;',
  28614. ' procedure SetOnShow(const p: TProc);',
  28615. ' property OnShow: TProc write SetOnShow;',
  28616. ' end;',
  28617. 'procedure Run(i: longint = 1);',
  28618. 'begin',
  28619. 'end;',
  28620. 'var',
  28621. ' Obj: texta;',
  28622. ' e: TEvent;',
  28623. ' p: TProc;',
  28624. 'begin',
  28625. ' e:=e;',
  28626. ' e:=obj.doit;',
  28627. ' e:=obj.dosome;',
  28628. ' e:=TEvent(@obj.dosome);', // no safecall
  28629. ' obj.OnClick:=obj.doit;',
  28630. ' obj.OnClick:=obj.dosome;',
  28631. ' obj.setonclick(obj.doit);',
  28632. ' obj.setonclick(obj.dosome);',
  28633. ' p:=Run;',
  28634. ' p:=TExtA.Fly;',
  28635. ' obj.OnShow:=Run;',
  28636. ' obj.OnShow:=TExtA.Fly;',
  28637. ' obj.setOnShow(Run);',
  28638. ' obj.setOnShow(TExtA.Fly);',
  28639. ' with obj do begin',
  28640. ' e:=doit;',
  28641. ' e:=dosome;',
  28642. ' OnClick:=doit;',
  28643. ' OnClick:=dosome;',
  28644. ' setonclick(doit);',
  28645. ' setonclick(dosome);',
  28646. ' OnShow:=@Run;',
  28647. ' setOnShow(@Run);',
  28648. ' end;']);
  28649. ConvertProgram;
  28650. CheckSource('TestProcType_SafeCallDelphi',
  28651. LinesToStr([ // statements
  28652. 'this.Run = function (i) {',
  28653. '};',
  28654. 'this.Obj = null;',
  28655. 'this.e = null;',
  28656. 'this.p = null;',
  28657. '']),
  28658. LinesToStr([ // $mod.$main
  28659. '$mod.e = $mod.e;',
  28660. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  28661. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  28662. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  28663. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28664. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28665. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28666. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28667. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  28668. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  28669. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28670. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28671. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28672. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28673. 'var $with = $mod.Obj;',
  28674. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  28675. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  28676. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28677. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28678. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28679. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28680. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28681. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28682. '']));
  28683. end;
  28684. procedure TTestModule.TestProcType_SafeCall_Arg;
  28685. begin
  28686. StartProgram(false);
  28687. Add([
  28688. 'type',
  28689. ' TSafecallProc = reference to procedure; safecall;',
  28690. 'procedure Fly(const aHandler: TSafecallProc);',
  28691. 'var',
  28692. ' P: TSafecallProc;',
  28693. 'begin',
  28694. ' P := aHandler;',
  28695. ' Fly(P);',
  28696. ' Fly(aHandler);',
  28697. 'end;',
  28698. 'begin',
  28699. ' Fly(nil);',
  28700. '']);
  28701. ConvertProgram;
  28702. CheckSource('TestProcType_SafeCall_Arg',
  28703. LinesToStr([ // statements
  28704. 'this.Fly = function (aHandler) {',
  28705. ' var P = null;',
  28706. ' P = aHandler;',
  28707. ' $mod.Fly(P);',
  28708. ' $mod.Fly(aHandler);',
  28709. '};',
  28710. '']),
  28711. LinesToStr([ // $mod.$main
  28712. '$mod.Fly(null);',
  28713. '']));
  28714. end;
  28715. procedure TTestModule.TestPointer;
  28716. begin
  28717. StartProgram(false);
  28718. Add(['type',
  28719. ' TObject = class end;',
  28720. ' TClass = class of TObject;',
  28721. ' TArrInt = array of longint;',
  28722. 'const',
  28723. ' n = nil;',
  28724. 'var',
  28725. ' v: jsvalue;',
  28726. ' Obj: tobject;',
  28727. ' C: tclass;',
  28728. ' a: tarrint;',
  28729. ' p: Pointer = nil;',
  28730. ' s: string;',
  28731. 'begin',
  28732. ' p:=p;',
  28733. ' p:=nil;',
  28734. ' if p=nil then;',
  28735. ' if nil=p then;',
  28736. ' if Assigned(p) then;',
  28737. ' p:=Pointer(v);',
  28738. ' p:=obj;',
  28739. ' p:=c;',
  28740. ' p:=a;',
  28741. ' p:=tobject;',
  28742. ' obj:=TObject(p);',
  28743. ' c:=TClass(p);',
  28744. ' a:=TArrInt(p);',
  28745. ' p:=n;',
  28746. ' p:=Pointer(a);',
  28747. ' p:=pointer(s);',
  28748. ' s:=string(p);',
  28749. '']);
  28750. ConvertProgram;
  28751. CheckSource('TestPointer',
  28752. LinesToStr([ // statements
  28753. 'rtl.createClass(this, "TObject", null, function () {',
  28754. ' this.$init = function () {',
  28755. ' };',
  28756. ' this.$final = function () {',
  28757. ' };',
  28758. '});',
  28759. 'this.n = null;',
  28760. 'this.v = undefined;',
  28761. 'this.Obj = null;',
  28762. 'this.C = null;',
  28763. 'this.a = [];',
  28764. 'this.p = null;',
  28765. 'this.s = "";',
  28766. '']),
  28767. LinesToStr([ // $mod.$main
  28768. '$mod.p = $mod.p;',
  28769. '$mod.p = null;',
  28770. 'if ($mod.p === null) ;',
  28771. 'if (null === $mod.p) ;',
  28772. 'if ($mod.p != null) ;',
  28773. '$mod.p = $mod.v;',
  28774. '$mod.p = $mod.Obj;',
  28775. '$mod.p = $mod.C;',
  28776. '$mod.p = $mod.a;',
  28777. '$mod.p = $mod.TObject;',
  28778. '$mod.Obj = $mod.p;',
  28779. '$mod.C = $mod.p;',
  28780. '$mod.a = $mod.p;',
  28781. '$mod.p = null;',
  28782. '$mod.p = $mod.a;',
  28783. '$mod.p = $mod.s;',
  28784. '$mod.s = $mod.p;',
  28785. '']));
  28786. end;
  28787. procedure TTestModule.TestPointer_Proc;
  28788. begin
  28789. StartProgram(false);
  28790. Add('type');
  28791. Add(' TObject = class');
  28792. Add(' procedure DoIt; virtual; abstract;');
  28793. Add(' end;');
  28794. Add('procedure DoSome; begin end;');
  28795. Add('var');
  28796. Add(' o: TObject;');
  28797. Add(' p: Pointer;');
  28798. Add('begin');
  28799. Add(' p:=@DoSome;');
  28800. Add(' p:[email protected];');
  28801. ConvertProgram;
  28802. CheckSource('TestPointer_Proc',
  28803. LinesToStr([ // statements
  28804. 'rtl.createClass(this, "TObject", null, function () {',
  28805. ' this.$init = function () {',
  28806. ' };',
  28807. ' this.$final = function () {',
  28808. ' };',
  28809. '});',
  28810. 'this.DoSome = function () {',
  28811. '};',
  28812. 'this.o = null;',
  28813. 'this.p = null;',
  28814. '']),
  28815. LinesToStr([ // $mod.$main
  28816. '$mod.p = $mod.DoSome;',
  28817. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  28818. '']));
  28819. end;
  28820. procedure TTestModule.TestPointer_AssignRecordFail;
  28821. begin
  28822. StartProgram(false);
  28823. Add('type');
  28824. Add(' TRec = record end;');
  28825. Add('var');
  28826. Add(' p: Pointer;');
  28827. Add(' r: TRec;');
  28828. Add('begin');
  28829. Add(' p:=r;');
  28830. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  28831. nIncompatibleTypesGotExpected);
  28832. ConvertProgram;
  28833. end;
  28834. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  28835. begin
  28836. StartProgram(false);
  28837. Add('type');
  28838. Add(' TArr = array[boolean] of longint;');
  28839. Add('var');
  28840. Add(' p: Pointer;');
  28841. Add(' a: TArr;');
  28842. Add('begin');
  28843. Add(' p:=a;');
  28844. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  28845. nIncompatibleTypesGotExpected);
  28846. ConvertProgram;
  28847. end;
  28848. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  28849. begin
  28850. StartProgram(false);
  28851. Add([
  28852. 'procedure DoIt(args: array of jsvalue); begin end;',
  28853. 'procedure DoAll; varargs; begin end;',
  28854. 'var',
  28855. ' v: jsvalue;',
  28856. 'begin',
  28857. ' DoIt([pointer(v)]);',
  28858. ' DoAll(pointer(v));',
  28859. '']);
  28860. ConvertProgram;
  28861. CheckSource('TestPointer_TypeCastJSValueToPointer',
  28862. LinesToStr([ // statements
  28863. 'this.DoIt = function (args) {',
  28864. '};',
  28865. 'this.DoAll = function () {',
  28866. '};',
  28867. 'this.v = undefined;',
  28868. '']),
  28869. LinesToStr([ // $mod.$main
  28870. '$mod.DoIt([$mod.v]);',
  28871. '$mod.DoAll($mod.v);',
  28872. '']));
  28873. end;
  28874. procedure TTestModule.TestPointer_NonRecordFail;
  28875. begin
  28876. StartProgram(false);
  28877. Add([
  28878. 'type',
  28879. ' p = ^longint;',
  28880. 'begin',
  28881. '']);
  28882. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  28883. ConvertProgram;
  28884. end;
  28885. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  28886. begin
  28887. StartProgram(false);
  28888. Add([
  28889. 'procedure DoIt(p: ^longint); begin end;',
  28890. 'begin',
  28891. '']);
  28892. SetExpectedParserError('Parameters or result types cannot contain local type definitions. Use a separate type definition in a type block. at token "^" in file test1.pp at line 3 column 19',nParserParamsOrResultTypesNoLocalTypeDefs);
  28893. ConvertProgram;
  28894. end;
  28895. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  28896. begin
  28897. StartProgram(false);
  28898. Add([
  28899. 'var p: ^longint;',
  28900. 'begin',
  28901. '']);
  28902. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28903. ConvertProgram;
  28904. end;
  28905. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  28906. begin
  28907. StartProgram(false);
  28908. Add([
  28909. 'function DoIt: ^longint; begin end;',
  28910. 'begin',
  28911. '']);
  28912. SetExpectedParserError('Parameters or result types cannot contain local type definitions. Use a separate type definition in a type block. at token "^" in file test1.pp at line 3 column 16',nParserParamsOrResultTypesNoLocalTypeDefs);
  28913. ConvertProgram;
  28914. end;
  28915. procedure TTestModule.TestPointer_AddrOperatorFail;
  28916. begin
  28917. StartProgram(false);
  28918. Add([
  28919. 'var i: longint;',
  28920. 'begin',
  28921. ' if @i=nil then ;',
  28922. '']);
  28923. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  28924. ConvertProgram;
  28925. end;
  28926. procedure TTestModule.TestPointer_ArrayParamsFail;
  28927. begin
  28928. StartProgram(false);
  28929. Add([
  28930. 'var',
  28931. ' p: Pointer;',
  28932. 'begin',
  28933. ' p:=p[1];',
  28934. '']);
  28935. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  28936. ConvertProgram;
  28937. end;
  28938. procedure TTestModule.TestPointer_PointerAddFail;
  28939. begin
  28940. StartProgram(false);
  28941. Add([
  28942. 'var',
  28943. ' p: Pointer;',
  28944. 'begin',
  28945. ' p:=p+1;',
  28946. '']);
  28947. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  28948. ConvertProgram;
  28949. end;
  28950. procedure TTestModule.TestPointer_IncPointerFail;
  28951. begin
  28952. StartProgram(false);
  28953. Add([
  28954. 'var',
  28955. ' p: Pointer;',
  28956. 'begin',
  28957. ' inc(p,1);',
  28958. '']);
  28959. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "Pointer", expected "integer"',
  28960. nIncompatibleTypeArgNo);
  28961. ConvertProgram;
  28962. end;
  28963. procedure TTestModule.TestPointer_Record;
  28964. begin
  28965. StartProgram(false);
  28966. Add([
  28967. 'type',
  28968. ' TRec = record x: longint; end;',
  28969. ' PRec = ^TRec;',
  28970. 'var',
  28971. ' r: TRec;',
  28972. ' p: PRec;',
  28973. ' q: ^TRec;',
  28974. ' Ptr: pointer;',
  28975. 'begin',
  28976. ' new(p);',
  28977. ' p:=@r;',
  28978. ' r:=p^;',
  28979. ' r.x:=p^.x;',
  28980. ' p^.x:=r.x;',
  28981. ' if p^.x=3 then ;',
  28982. ' if 4=p^.x then ;',
  28983. ' dispose(p);',
  28984. ' new(q);',
  28985. ' dispose(q);',
  28986. ' Ptr:=p;',
  28987. ' p:=PRec(ptr);',
  28988. '']);
  28989. ConvertProgram;
  28990. CheckSource('TestPointer_Record',
  28991. LinesToStr([ // statements
  28992. 'rtl.recNewT(this, "TRec", function () {',
  28993. ' this.x = 0;',
  28994. ' this.$eq = function (b) {',
  28995. ' return this.x === b.x;',
  28996. ' };',
  28997. ' this.$assign = function (s) {',
  28998. ' this.x = s.x;',
  28999. ' return this;',
  29000. ' };',
  29001. '});',
  29002. 'this.r = this.TRec.$new();',
  29003. 'this.p = null;',
  29004. 'this.q = null;',
  29005. 'this.Ptr = null;',
  29006. '']),
  29007. LinesToStr([ // $mod.$main
  29008. '$mod.p = $mod.TRec.$new();',
  29009. '$mod.p = $mod.r;',
  29010. '$mod.r.$assign($mod.p);',
  29011. '$mod.r.x = $mod.p.x;',
  29012. '$mod.p.x = $mod.r.x;',
  29013. 'if ($mod.p.x === 3) ;',
  29014. 'if (4 === $mod.p.x) ;',
  29015. '$mod.p = null;',
  29016. '$mod.q = $mod.TRec.$new();',
  29017. '$mod.q = null;',
  29018. '$mod.Ptr = $mod.p;',
  29019. '$mod.p = $mod.Ptr;',
  29020. '']));
  29021. end;
  29022. procedure TTestModule.TestPointer_RecordArg;
  29023. begin
  29024. StartProgram(false);
  29025. Add([
  29026. '{$modeswitch autoderef}',
  29027. 'type',
  29028. ' TRec = record x: longint; end;',
  29029. ' PRec = ^TRec;',
  29030. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  29031. 'begin',
  29032. ' a.x:=a.x;',
  29033. ' a^.x:=a^.x;',
  29034. ' with a^ do',
  29035. ' x:=x;',
  29036. 'end;',
  29037. 'function GetIt(p: PRec): PRec;',
  29038. 'begin',
  29039. ' p.x:=p.x;',
  29040. ' p^.x:=p^.x;',
  29041. ' with p^ do',
  29042. ' x:=x;',
  29043. 'end;',
  29044. 'var',
  29045. ' r: TRec;',
  29046. ' p: PRec;',
  29047. 'begin',
  29048. ' p:=GetIt(p);',
  29049. ' p^:=GetIt(@r)^;',
  29050. ' DoIt(p,p,p);',
  29051. ' DoIt(@r,p,p);',
  29052. '']);
  29053. ConvertProgram;
  29054. CheckSource('TestPointer_RecordArg',
  29055. LinesToStr([ // statements
  29056. 'rtl.recNewT(this, "TRec", function () {',
  29057. ' this.x = 0;',
  29058. ' this.$eq = function (b) {',
  29059. ' return this.x === b.x;',
  29060. ' };',
  29061. ' this.$assign = function (s) {',
  29062. ' this.x = s.x;',
  29063. ' return this;',
  29064. ' };',
  29065. '});',
  29066. 'this.DoIt = function (a, b, c) {',
  29067. ' var Result = $mod.TRec.$new();',
  29068. ' a.x = a.x;',
  29069. ' a.x = a.x;',
  29070. ' a.x = a.x;',
  29071. ' return Result;',
  29072. '};',
  29073. 'this.GetIt = function (p) {',
  29074. ' var Result = null;',
  29075. ' p.x = p.x;',
  29076. ' p.x = p.x;',
  29077. ' p.x = p.x;',
  29078. ' return Result;',
  29079. '};',
  29080. 'this.r = this.TRec.$new();',
  29081. 'this.p = null;',
  29082. '']),
  29083. LinesToStr([ // $mod.$main
  29084. '$mod.p = $mod.GetIt($mod.p);',
  29085. '$mod.p.$assign($mod.GetIt($mod.r));',
  29086. '$mod.DoIt($mod.p, {',
  29087. ' p: $mod,',
  29088. ' get: function () {',
  29089. ' return this.p.p;',
  29090. ' },',
  29091. ' set: function (v) {',
  29092. ' this.p.p = v;',
  29093. ' }',
  29094. '}, {',
  29095. ' p: $mod,',
  29096. ' get: function () {',
  29097. ' return this.p.p;',
  29098. ' },',
  29099. ' set: function (v) {',
  29100. ' this.p.p = v;',
  29101. ' }',
  29102. '});',
  29103. '$mod.DoIt($mod.r, {',
  29104. ' p: $mod,',
  29105. ' get: function () {',
  29106. ' return this.p.p;',
  29107. ' },',
  29108. ' set: function (v) {',
  29109. ' this.p.p = v;',
  29110. ' }',
  29111. '}, {',
  29112. ' p: $mod,',
  29113. ' get: function () {',
  29114. ' return this.p.p;',
  29115. ' },',
  29116. ' set: function (v) {',
  29117. ' this.p.p = v;',
  29118. ' }',
  29119. '});',
  29120. '']));
  29121. end;
  29122. procedure TTestModule.TestJSValue_AssignToJSValue;
  29123. begin
  29124. StartProgram(false);
  29125. Add('var');
  29126. Add(' v: jsvalue;');
  29127. Add(' i: longint;');
  29128. Add(' s: string;');
  29129. Add(' b: boolean;');
  29130. Add(' d: double;');
  29131. Add(' p: pointer;');
  29132. Add('begin');
  29133. Add(' v:=v;');
  29134. Add(' v:=1;');
  29135. Add(' v:=i;');
  29136. Add(' v:='''';');
  29137. Add(' v:=''c'';');
  29138. Add(' v:=''foo'';');
  29139. Add(' v:=s;');
  29140. Add(' v:=false;');
  29141. Add(' v:=true;');
  29142. Add(' v:=b;');
  29143. Add(' v:=0.1;');
  29144. Add(' v:=d;');
  29145. Add(' v:=nil;');
  29146. Add(' v:=p;');
  29147. ConvertProgram;
  29148. CheckSource('TestJSValue_AssignToJSValue',
  29149. LinesToStr([ // statements
  29150. 'this.v = undefined;',
  29151. 'this.i = 0;',
  29152. 'this.s = "";',
  29153. 'this.b = false;',
  29154. 'this.d = 0.0;',
  29155. 'this.p = null;',
  29156. '']),
  29157. LinesToStr([ // $mod.$main
  29158. '$mod.v = $mod.v;',
  29159. '$mod.v = 1;',
  29160. '$mod.v = $mod.i;',
  29161. '$mod.v = "";',
  29162. '$mod.v = "c";',
  29163. '$mod.v = "foo";',
  29164. '$mod.v = $mod.s;',
  29165. '$mod.v = false;',
  29166. '$mod.v = true;',
  29167. '$mod.v = $mod.b;',
  29168. '$mod.v = 0.1;',
  29169. '$mod.v = $mod.d;',
  29170. '$mod.v = null;',
  29171. '$mod.v = $mod.p;',
  29172. '']));
  29173. end;
  29174. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  29175. begin
  29176. StartProgram(false);
  29177. Add('type');
  29178. Add(' integer = longint;');
  29179. Add(' TYesNo = boolean;');
  29180. Add(' TFloat = double;');
  29181. Add(' TCaption = string;');
  29182. Add(' TChar = char;');
  29183. Add('var');
  29184. Add(' v: jsvalue;');
  29185. Add(' i: integer;');
  29186. Add(' s: TCaption;');
  29187. Add(' b: TYesNo;');
  29188. Add(' d: TFloat;');
  29189. Add(' c: char;');
  29190. Add('begin');
  29191. Add(' i:=longint(v);');
  29192. Add(' i:=integer(v);');
  29193. Add(' s:=string(v);');
  29194. Add(' s:=TCaption(v);');
  29195. Add(' b:=boolean(v);');
  29196. Add(' b:=TYesNo(v);');
  29197. Add(' d:=double(v);');
  29198. Add(' d:=TFloat(v);');
  29199. Add(' c:=char(v);');
  29200. Add(' c:=TChar(v);');
  29201. ConvertProgram;
  29202. CheckSource('TestJSValue_TypeCastToBaseType',
  29203. LinesToStr([ // statements
  29204. 'this.v = undefined;',
  29205. 'this.i = 0;',
  29206. 'this.s = "";',
  29207. 'this.b = false;',
  29208. 'this.d = 0.0;',
  29209. 'this.c = "\x00";',
  29210. '']),
  29211. LinesToStr([ // $mod.$main
  29212. '$mod.i = rtl.trunc($mod.v);',
  29213. '$mod.i = rtl.trunc($mod.v);',
  29214. '$mod.s = "" + $mod.v;',
  29215. '$mod.s = "" + $mod.v;',
  29216. '$mod.b = !($mod.v == false);',
  29217. '$mod.b = !($mod.v == false);',
  29218. '$mod.d = rtl.getNumber($mod.v);',
  29219. '$mod.d = rtl.getNumber($mod.v);',
  29220. '$mod.c = rtl.getChar($mod.v);',
  29221. '$mod.c = rtl.getChar($mod.v);',
  29222. '']));
  29223. end;
  29224. procedure TTestModule.TestJSValue_TypecastToJSValue;
  29225. begin
  29226. StartProgram(false);
  29227. Add([
  29228. 'type',
  29229. ' TArr = array of word;',
  29230. ' TRec = record end;',
  29231. ' TSet = set of boolean;',
  29232. 'procedure Fly(v: jsvalue);',
  29233. 'begin',
  29234. 'end;',
  29235. 'var',
  29236. ' a: TArr;',
  29237. ' r: TRec;',
  29238. ' s: TSet;',
  29239. 'begin',
  29240. ' Fly(jsvalue(a));',
  29241. ' Fly(jsvalue(r));',
  29242. ' Fly(jsvalue(s));',
  29243. '']);
  29244. ConvertProgram;
  29245. CheckSource('TestJSValue_TypecastToJSValue',
  29246. LinesToStr([ // statements
  29247. 'rtl.recNewT(this, "TRec", function () {',
  29248. ' this.$eq = function (b) {',
  29249. ' return true;',
  29250. ' };',
  29251. ' this.$assign = function (s) {',
  29252. ' return this;',
  29253. ' };',
  29254. '});',
  29255. 'this.Fly = function (v) {',
  29256. '};',
  29257. 'this.a = [];',
  29258. 'this.r = this.TRec.$new();',
  29259. 'this.s = {};',
  29260. '']),
  29261. LinesToStr([ // $mod.$main
  29262. '$mod.Fly($mod.a);',
  29263. '$mod.Fly($mod.r);',
  29264. '$mod.Fly($mod.s);',
  29265. '']));
  29266. end;
  29267. procedure TTestModule.TestJSValue_Equal;
  29268. begin
  29269. StartProgram(false);
  29270. Add('type');
  29271. Add(' integer = longint;');
  29272. Add(' TYesNo = boolean;');
  29273. Add(' TFloat = double;');
  29274. Add(' TCaption = string;');
  29275. Add(' TChar = char;');
  29276. Add(' TMulti = JSValue;');
  29277. Add('var');
  29278. Add(' v: jsvalue;');
  29279. Add(' i: integer;');
  29280. Add(' s: TCaption;');
  29281. Add(' b: TYesNo;');
  29282. Add(' d: TFloat;');
  29283. Add(' c: char;');
  29284. Add(' m: TMulti;');
  29285. Add('begin');
  29286. Add(' b:=v=v;');
  29287. Add(' b:=v<>v;');
  29288. Add(' b:=v=1;');
  29289. Add(' b:=v<>1;');
  29290. Add(' b:=2=v;');
  29291. Add(' b:=2<>v;');
  29292. Add(' b:=v=i;');
  29293. Add(' b:=i=v;');
  29294. Add(' b:=v=nil;');
  29295. Add(' b:=nil=v;');
  29296. Add(' b:=v=false;');
  29297. Add(' b:=true=v;');
  29298. Add(' b:=v=b;');
  29299. Add(' b:=b=v;');
  29300. Add(' b:=v=s;');
  29301. Add(' b:=s=v;');
  29302. Add(' b:=v=''foo'';');
  29303. Add(' b:=''''=v;');
  29304. Add(' b:=v=d;');
  29305. Add(' b:=d=v;');
  29306. Add(' b:=v=3.4;');
  29307. Add(' b:=5.6=v;');
  29308. Add(' b:=v=c;');
  29309. Add(' b:=c=v;');
  29310. Add(' b:=m=m;');
  29311. Add(' b:=v=m;');
  29312. Add(' b:=m=v;');
  29313. ConvertProgram;
  29314. CheckSource('TestJSValue_Equal',
  29315. LinesToStr([ // statements
  29316. 'this.v = undefined;',
  29317. 'this.i = 0;',
  29318. 'this.s = "";',
  29319. 'this.b = false;',
  29320. 'this.d = 0.0;',
  29321. 'this.c = "\x00";',
  29322. 'this.m = undefined;',
  29323. '']),
  29324. LinesToStr([ // $mod.$main
  29325. '$mod.b = $mod.v == $mod.v;',
  29326. '$mod.b = $mod.v != $mod.v;',
  29327. '$mod.b = $mod.v == 1;',
  29328. '$mod.b = $mod.v != 1;',
  29329. '$mod.b = 2 == $mod.v;',
  29330. '$mod.b = 2 != $mod.v;',
  29331. '$mod.b = $mod.v == $mod.i;',
  29332. '$mod.b = $mod.i == $mod.v;',
  29333. '$mod.b = $mod.v == null;',
  29334. '$mod.b = null == $mod.v;',
  29335. '$mod.b = $mod.v == false;',
  29336. '$mod.b = true == $mod.v;',
  29337. '$mod.b = $mod.v == $mod.b;',
  29338. '$mod.b = $mod.b == $mod.v;',
  29339. '$mod.b = $mod.v == $mod.s;',
  29340. '$mod.b = $mod.s == $mod.v;',
  29341. '$mod.b = $mod.v == "foo";',
  29342. '$mod.b = "" == $mod.v;',
  29343. '$mod.b = $mod.v == $mod.d;',
  29344. '$mod.b = $mod.d == $mod.v;',
  29345. '$mod.b = $mod.v == 3.4;',
  29346. '$mod.b = 5.6 == $mod.v;',
  29347. '$mod.b = $mod.v == $mod.c;',
  29348. '$mod.b = $mod.c == $mod.v;',
  29349. '$mod.b = $mod.m == $mod.m;',
  29350. '$mod.b = $mod.v == $mod.m;',
  29351. '$mod.b = $mod.m == $mod.v;',
  29352. '']));
  29353. end;
  29354. procedure TTestModule.TestJSValue_If;
  29355. begin
  29356. StartProgram(false);
  29357. Add([
  29358. 'procedure Fly(var u);',
  29359. 'begin',
  29360. ' if jsvalue(u) then ;',
  29361. 'end;',
  29362. 'var',
  29363. ' v: jsvalue;',
  29364. 'begin',
  29365. ' if v then ;',
  29366. ' while v do ;',
  29367. ' repeat until v;',
  29368. '']);
  29369. ConvertProgram;
  29370. CheckSource('TestJSValue_If',
  29371. LinesToStr([ // statements
  29372. 'this.Fly = function (u) {',
  29373. ' if (u.get()) ;',
  29374. '};',
  29375. 'this.v = undefined;',
  29376. '']),
  29377. LinesToStr([ // $mod.$main
  29378. 'if ($mod.v) ;',
  29379. 'while($mod.v){',
  29380. '};',
  29381. 'do{',
  29382. '} while(!$mod.v);',
  29383. '']));
  29384. end;
  29385. procedure TTestModule.TestJSValue_Not;
  29386. begin
  29387. StartProgram(false);
  29388. Add([
  29389. 'var',
  29390. ' v: jsvalue;',
  29391. ' b: boolean;',
  29392. 'begin',
  29393. ' b:=not v;',
  29394. ' if not v then ;',
  29395. ' while not v do ;',
  29396. ' repeat until not v;',
  29397. '']);
  29398. ConvertProgram;
  29399. CheckSource('TestJSValue_If',
  29400. LinesToStr([ // statements
  29401. 'this.v = undefined;',
  29402. 'this.b = false;',
  29403. '']),
  29404. LinesToStr([ // $mod.$main
  29405. '$mod.b=!$mod.v;',
  29406. 'if (!$mod.v) ;',
  29407. 'while(!$mod.v){',
  29408. '};',
  29409. 'do{',
  29410. '} while($mod.v);',
  29411. '']));
  29412. end;
  29413. procedure TTestModule.TestJSValue_Enum;
  29414. begin
  29415. StartProgram(false);
  29416. Add('type');
  29417. Add(' TColor = (red, blue);');
  29418. Add(' TRedBlue = TColor;');
  29419. Add('var');
  29420. Add(' v: jsvalue;');
  29421. Add(' e: TColor;');
  29422. Add('begin');
  29423. Add(' v:=e;');
  29424. Add(' v:=TColor(e);');
  29425. Add(' v:=TRedBlue(e);');
  29426. Add(' e:=TColor(v);');
  29427. Add(' e:=TRedBlue(v);');
  29428. ConvertProgram;
  29429. CheckSource('TestJSValue_Enum',
  29430. LinesToStr([ // statements
  29431. 'this.TColor = {',
  29432. ' "0": "red",',
  29433. ' red: 0,',
  29434. ' "1": "blue",',
  29435. ' blue: 1',
  29436. '};',
  29437. 'this.v = undefined;',
  29438. 'this.e = 0;',
  29439. '']),
  29440. LinesToStr([ // $mod.$main
  29441. '$mod.v = $mod.e;',
  29442. '$mod.v = $mod.e;',
  29443. '$mod.v = $mod.e;',
  29444. '$mod.e = $mod.v;',
  29445. '$mod.e = $mod.v;',
  29446. '']));
  29447. end;
  29448. procedure TTestModule.TestJSValue_ClassInstance;
  29449. begin
  29450. StartProgram(false);
  29451. Add([
  29452. 'type',
  29453. ' TObject = class',
  29454. ' end;',
  29455. ' TBirdObject = TObject;',
  29456. 'var',
  29457. ' v: jsvalue;',
  29458. ' o: TObject;',
  29459. 'begin',
  29460. ' v:=o;',
  29461. ' v:=TObject(o);',
  29462. ' v:=TBirdObject(o);',
  29463. ' o:=TObject(v);',
  29464. ' o:=TBirdObject(v);',
  29465. ' if v is TObject then ;',
  29466. '']);
  29467. ConvertProgram;
  29468. CheckSource('TestJSValue_ClassInstance',
  29469. LinesToStr([ // statements
  29470. 'rtl.createClass(this, "TObject", null, function () {',
  29471. ' this.$init = function () {',
  29472. ' };',
  29473. ' this.$final = function () {',
  29474. ' };',
  29475. '});',
  29476. 'this.v = undefined;',
  29477. 'this.o = null;',
  29478. '']),
  29479. LinesToStr([ // $mod.$main
  29480. '$mod.v = $mod.o;',
  29481. '$mod.v = $mod.o;',
  29482. '$mod.v = $mod.o;',
  29483. '$mod.o = rtl.getObject($mod.v);',
  29484. '$mod.o = rtl.getObject($mod.v);',
  29485. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  29486. '']));
  29487. end;
  29488. procedure TTestModule.TestJSValue_ClassOf;
  29489. begin
  29490. StartProgram(false);
  29491. Add([
  29492. 'type',
  29493. ' TClass = class of TObject;',
  29494. ' TObject = class',
  29495. ' end;',
  29496. ' TBirds = class of TBird;',
  29497. ' TBird = class(TObject) end;',
  29498. 'var',
  29499. ' v: jsvalue;',
  29500. ' c: TClass;',
  29501. 'begin',
  29502. ' v:=c;',
  29503. ' v:=TObject;',
  29504. ' v:=TClass(c);',
  29505. ' v:=TBirds(c);',
  29506. ' c:=TClass(v);',
  29507. ' c:=TBirds(v);',
  29508. ' if v is TClass then ;',
  29509. '']);
  29510. ConvertProgram;
  29511. CheckSource('TestJSValue_ClassOf',
  29512. LinesToStr([ // statements
  29513. 'rtl.createClass(this, "TObject", null, function () {',
  29514. ' this.$init = function () {',
  29515. ' };',
  29516. ' this.$final = function () {',
  29517. ' };',
  29518. '});',
  29519. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29520. '});',
  29521. 'this.v = undefined;',
  29522. 'this.c = null;',
  29523. '']),
  29524. LinesToStr([ // $mod.$main
  29525. '$mod.v = $mod.c;',
  29526. '$mod.v = $mod.TObject;',
  29527. '$mod.v = $mod.c;',
  29528. '$mod.v = $mod.c;',
  29529. '$mod.c = rtl.getObject($mod.v);',
  29530. '$mod.c = rtl.getObject($mod.v);',
  29531. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  29532. '']));
  29533. end;
  29534. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  29535. begin
  29536. StartProgram(false);
  29537. Add([
  29538. 'type',
  29539. ' integer = longint;',
  29540. ' TArray = array of JSValue;',
  29541. ' TArrgh = tarray;',
  29542. ' TArrInt = array of integer;',
  29543. 'var',
  29544. ' v: jsvalue;',
  29545. ' TheArray: tarray = (1,''2'');',
  29546. ' Arr: tarrgh;',
  29547. ' i: integer;',
  29548. ' ArrInt: tarrint;',
  29549. 'begin',
  29550. ' arr:=thearray;',
  29551. ' thearray:=arr;',
  29552. ' setlength(arr,2);',
  29553. ' setlength(thearray,3);',
  29554. ' arr[4]:=v;',
  29555. ' arr[5]:=length(thearray);',
  29556. ' arr[6]:=nil;',
  29557. ' arr[7]:=thearray[8];',
  29558. ' arr[low(arr)]:=high(thearray);',
  29559. ' arr:=arrint;',
  29560. ' arrInt:=tarrint(arr);',
  29561. ' if TheArray = nil then ;',
  29562. ' if nil = TheArray then ;',
  29563. ' if TheArray <> nil then ;',
  29564. ' if nil <> TheArray then ;',
  29565. '']);
  29566. ConvertProgram;
  29567. CheckSource('TestJSValue_ArrayOfJSValue',
  29568. LinesToStr([ // statements
  29569. 'this.v = undefined;',
  29570. 'this.TheArray = [1, "2"];',
  29571. 'this.Arr = [];',
  29572. 'this.i = 0;',
  29573. 'this.ArrInt = [];',
  29574. '']),
  29575. LinesToStr([ // $mod.$main
  29576. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  29577. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  29578. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  29579. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  29580. '$mod.Arr[4] = $mod.v;',
  29581. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  29582. '$mod.Arr[6] = null;',
  29583. '$mod.Arr[7] = $mod.TheArray[8];',
  29584. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  29585. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  29586. '$mod.ArrInt = $mod.Arr;',
  29587. 'if (rtl.length($mod.TheArray) === 0) ;',
  29588. 'if (rtl.length($mod.TheArray) === 0) ;',
  29589. 'if (rtl.length($mod.TheArray) > 0) ;',
  29590. 'if (rtl.length($mod.TheArray) > 0) ;',
  29591. '']));
  29592. end;
  29593. procedure TTestModule.TestJSValue_ArrayLit;
  29594. begin
  29595. StartProgram(false);
  29596. Add([
  29597. 'type',
  29598. ' TFlag = (big,small);',
  29599. ' TArray = array of JSValue;',
  29600. ' TObject = class end;',
  29601. ' TClass = class of TObject;',
  29602. 'var',
  29603. ' v: jsvalue;',
  29604. ' a: TArray;',
  29605. ' o: TObject;',
  29606. 'begin',
  29607. ' a:=[];',
  29608. ' a:=[1];',
  29609. ' a:=[1,2];',
  29610. ' a:=[big];',
  29611. ' a:=[1,big];',
  29612. ' a:=[o,nil];',
  29613. '']);
  29614. ConvertProgram;
  29615. CheckSource('TestJSValue_ArrayLit',
  29616. LinesToStr([ // statements
  29617. 'this.TFlag = {',
  29618. ' "0": "big",',
  29619. ' big: 0,',
  29620. ' "1": "small",',
  29621. ' small: 1',
  29622. '};',
  29623. 'rtl.createClass(this, "TObject", null, function () {',
  29624. ' this.$init = function () {',
  29625. ' };',
  29626. ' this.$final = function () {',
  29627. ' };',
  29628. '});',
  29629. 'this.v = undefined;',
  29630. 'this.a = [];',
  29631. 'this.o = null;',
  29632. '']),
  29633. LinesToStr([ // $mod.$main
  29634. '$mod.a = [];',
  29635. '$mod.a = [1];',
  29636. '$mod.a = [1, 2];',
  29637. '$mod.a = [$mod.TFlag.big];',
  29638. '$mod.a = [1, $mod.TFlag.big];',
  29639. '$mod.a = [$mod.o, null];',
  29640. '']));
  29641. end;
  29642. procedure TTestModule.TestJSValue_Params;
  29643. begin
  29644. StartProgram(false);
  29645. Add('type');
  29646. Add(' integer = longint;');
  29647. Add(' TYesNo = boolean;');
  29648. Add(' TFloat = double;');
  29649. Add(' TCaption = string;');
  29650. Add(' TChar = char;');
  29651. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  29652. Add('var');
  29653. Add(' l: jsvalue;');
  29654. Add('begin');
  29655. Add(' a:=a;');
  29656. Add(' l:=b;');
  29657. Add(' c:=c;');
  29658. Add(' d:=d;');
  29659. Add(' Result:=l;');
  29660. Add('end;');
  29661. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  29662. Add('var');
  29663. Add(' v: jsvalue;');
  29664. Add(' i: integer;');
  29665. Add(' b: TYesNo;');
  29666. Add(' d: TFloat;');
  29667. Add(' s: TCaption;');
  29668. Add(' c: TChar;');
  29669. Add('begin');
  29670. Add(' v:=doit(v,v,v,v);');
  29671. Add(' i:=integer(dosome(i,i));');
  29672. Add(' b:=TYesNo(dosome(b,b));');
  29673. Add(' d:=TFloat(dosome(d,d));');
  29674. Add(' s:=TCaption(dosome(s,s));');
  29675. Add(' c:=TChar(dosome(c,c));');
  29676. ConvertProgram;
  29677. CheckSource('TestJSValue_Params',
  29678. LinesToStr([ // statements
  29679. 'this.DoIt = function (a, b, c, d) {',
  29680. ' var Result = undefined;',
  29681. ' var l = undefined;',
  29682. ' a = a;',
  29683. ' l = b;',
  29684. ' c.set(c.get());',
  29685. ' d.set(d.get());',
  29686. ' Result = l;',
  29687. ' return Result;',
  29688. '};',
  29689. 'this.DoSome = function (a, b) {',
  29690. ' var Result = undefined;',
  29691. ' return Result;',
  29692. '};',
  29693. 'this.v = undefined;',
  29694. 'this.i = 0;',
  29695. 'this.b = false;',
  29696. 'this.d = 0.0;',
  29697. 'this.s = "";',
  29698. 'this.c = "\x00";',
  29699. '']),
  29700. LinesToStr([ // $mod.$main
  29701. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  29702. ' p: $mod,',
  29703. ' get: function () {',
  29704. ' return this.p.v;',
  29705. ' },',
  29706. ' set: function (v) {',
  29707. ' this.p.v = v;',
  29708. ' }',
  29709. '}, {',
  29710. ' p: $mod,',
  29711. ' get: function () {',
  29712. ' return this.p.v;',
  29713. ' },',
  29714. ' set: function (v) {',
  29715. ' this.p.v = v;',
  29716. ' }',
  29717. '});',
  29718. '$mod.i = rtl.trunc($mod.DoSome($mod.i, $mod.i));',
  29719. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  29720. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  29721. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  29722. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  29723. '']));
  29724. end;
  29725. procedure TTestModule.TestJSValue_UntypedParam;
  29726. begin
  29727. StartProgram(false);
  29728. Add('function DoIt(const a; var b; out c): jsvalue;');
  29729. Add('begin');
  29730. Add(' Result:=a;');
  29731. Add(' Result:=b;');
  29732. Add(' Result:=c;');
  29733. Add(' b:=Result;');
  29734. Add(' c:=Result;');
  29735. Add('end;');
  29736. Add('var i: longint;');
  29737. Add('begin');
  29738. Add(' doit(i,i,i);');
  29739. ConvertProgram;
  29740. CheckSource('TestJSValue_UntypedParam',
  29741. LinesToStr([ // statements
  29742. 'this.DoIt = function (a, b, c) {',
  29743. ' var Result = undefined;',
  29744. ' Result = a;',
  29745. ' Result = b.get();',
  29746. ' Result = c.get();',
  29747. ' b.set(Result);',
  29748. ' c.set(Result);',
  29749. ' return Result;',
  29750. '};',
  29751. 'this.i = 0;',
  29752. '']),
  29753. LinesToStr([ // $mod.$main
  29754. '$mod.DoIt($mod.i, {',
  29755. ' p: $mod,',
  29756. ' get: function () {',
  29757. ' return this.p.i;',
  29758. ' },',
  29759. ' set: function (v) {',
  29760. ' this.p.i = v;',
  29761. ' }',
  29762. '}, {',
  29763. ' p: $mod,',
  29764. ' get: function () {',
  29765. ' return this.p.i;',
  29766. ' },',
  29767. ' set: function (v) {',
  29768. ' this.p.i = v;',
  29769. ' }',
  29770. '});',
  29771. '']));
  29772. end;
  29773. procedure TTestModule.TestJSValue_FuncResultType;
  29774. begin
  29775. StartProgram(false);
  29776. Add('type');
  29777. Add(' integer = longint;');
  29778. Add(' TJSValueArray = array of JSValue;');
  29779. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  29780. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  29781. Add('begin');
  29782. Add(' while Compare(P,aList[0])>0 do ;');
  29783. Add('end;');
  29784. Add('var');
  29785. Add(' Compare: TListSortCompare;');
  29786. Add(' V: JSValue;');
  29787. Add(' i: integer;');
  29788. Add('begin');
  29789. Add(' if Compare(V,V)>0 then ;');
  29790. Add(' if Compare(i,i)>1 then ;');
  29791. Add(' if Compare(nil,false)>2 then ;');
  29792. Add(' if Compare(1,true)>3 then ;');
  29793. ConvertProgram;
  29794. CheckSource('TestJSValue_UntypedParam',
  29795. LinesToStr([ // statements
  29796. 'this.Sort = function (P, aList, Compare) {',
  29797. ' while (Compare(P, aList[0]) > 0) {',
  29798. ' };',
  29799. '};',
  29800. 'this.Compare = null;',
  29801. 'this.V = undefined;',
  29802. 'this.i = 0;',
  29803. '']),
  29804. LinesToStr([ // $mod.$main
  29805. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  29806. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  29807. 'if ($mod.Compare(null, false) > 2) ;',
  29808. 'if ($mod.Compare(1, true) > 3) ;',
  29809. '']));
  29810. end;
  29811. procedure TTestModule.TestJSValue_ProcType_Assign;
  29812. begin
  29813. StartProgram(false);
  29814. Add('type');
  29815. Add(' integer = longint;');
  29816. Add(' TObject = class');
  29817. Add(' class function GetGlob: integer;');
  29818. Add(' function Getter: integer;');
  29819. Add(' end;');
  29820. Add('class function TObject.GetGlob: integer;');
  29821. Add('var v1: jsvalue;');
  29822. Add('begin');
  29823. Add(' v1:=@GetGlob;');
  29824. Add(' v1:[email protected];');
  29825. Add('end;');
  29826. Add('function TObject.Getter: integer;');
  29827. Add('var v2: jsvalue;');
  29828. Add('begin');
  29829. Add(' v2:=@Getter;');
  29830. Add(' v2:[email protected];');
  29831. Add(' v2:=@GetGlob;');
  29832. Add(' v2:[email protected];');
  29833. Add('end;');
  29834. Add('function GetIt(i: integer): integer;');
  29835. Add('var v3: jsvalue;');
  29836. Add('begin');
  29837. Add(' v3:=@GetIt;');
  29838. Add('end;');
  29839. Add('var');
  29840. Add(' V: JSValue;');
  29841. Add(' o: TObject;');
  29842. Add('begin');
  29843. Add(' v:=@GetIt;');
  29844. Add(' v:[email protected];');
  29845. Add(' v:[email protected];');
  29846. ConvertProgram;
  29847. CheckSource('TestJSValue_ProcType_Assign',
  29848. LinesToStr([ // statements
  29849. 'rtl.createClass(this, "TObject", null, function () {',
  29850. ' this.$init = function () {',
  29851. ' };',
  29852. ' this.$final = function () {',
  29853. ' };',
  29854. ' this.GetGlob = function () {',
  29855. ' var Result = 0;',
  29856. ' var v1 = undefined;',
  29857. ' v1 = rtl.createCallback(this, "GetGlob");',
  29858. ' v1 = rtl.createCallback(this, "GetGlob");',
  29859. ' return Result;',
  29860. ' };',
  29861. ' this.Getter = function () {',
  29862. ' var Result = 0;',
  29863. ' var v2 = undefined;',
  29864. ' v2 = rtl.createCallback(this, "Getter");',
  29865. ' v2 = rtl.createCallback(this, "Getter");',
  29866. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  29867. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  29868. ' return Result;',
  29869. ' };',
  29870. '});',
  29871. 'this.GetIt = function (i) {',
  29872. ' var Result = 0;',
  29873. ' var v3 = undefined;',
  29874. ' v3 = $mod.GetIt;',
  29875. ' return Result;',
  29876. '};',
  29877. 'this.V = undefined;',
  29878. 'this.o = null;',
  29879. '']),
  29880. LinesToStr([ // $mod.$main
  29881. '$mod.V = $mod.GetIt;',
  29882. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  29883. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  29884. '']));
  29885. end;
  29886. procedure TTestModule.TestJSValue_ProcType_Equal;
  29887. begin
  29888. StartProgram(false);
  29889. Add('type');
  29890. Add(' integer = longint;');
  29891. Add(' TObject = class');
  29892. Add(' class function GetGlob: integer;');
  29893. Add(' function Getter: integer;');
  29894. Add(' end;');
  29895. Add('class function TObject.GetGlob: integer;');
  29896. Add('var v1: jsvalue;');
  29897. Add('begin');
  29898. Add(' if v1=@GetGlob then;');
  29899. Add(' if [email protected] then ;');
  29900. Add('end;');
  29901. Add('function TObject.Getter: integer;');
  29902. Add('var v2: jsvalue;');
  29903. Add('begin');
  29904. Add(' if v2=@Getter then;');
  29905. Add(' if [email protected] then ;');
  29906. Add(' if v2=@GetGlob then;');
  29907. Add(' if [email protected] then;');
  29908. Add('end;');
  29909. Add('function GetIt(i: integer): integer;');
  29910. Add('var v3: jsvalue;');
  29911. Add('begin');
  29912. Add(' if v3=@GetIt then;');
  29913. Add('end;');
  29914. Add('var');
  29915. Add(' V: JSValue;');
  29916. Add(' o: TObject;');
  29917. Add('begin');
  29918. Add(' if v=@GetIt then;');
  29919. Add(' if [email protected] then;');
  29920. Add(' if [email protected] then;');
  29921. Add(' if @GetIt=v then;');
  29922. Add(' if @o.Getter=v then;');
  29923. Add(' if @o.GetGlob=v then;');
  29924. ConvertProgram;
  29925. CheckSource('TestJSValue_ProcType_Equal',
  29926. LinesToStr([ // statements
  29927. 'rtl.createClass(this, "TObject", null, function () {',
  29928. ' this.$init = function () {',
  29929. ' };',
  29930. ' this.$final = function () {',
  29931. ' };',
  29932. ' this.GetGlob = function () {',
  29933. ' var Result = 0;',
  29934. ' var v1 = undefined;',
  29935. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  29936. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  29937. ' return Result;',
  29938. ' };',
  29939. ' this.Getter = function () {',
  29940. ' var Result = 0;',
  29941. ' var v2 = undefined;',
  29942. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  29943. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  29944. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  29945. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  29946. ' return Result;',
  29947. ' };',
  29948. '});',
  29949. 'this.GetIt = function (i) {',
  29950. ' var Result = 0;',
  29951. ' var v3 = undefined;',
  29952. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  29953. ' return Result;',
  29954. '};',
  29955. 'this.V = undefined;',
  29956. 'this.o = null;',
  29957. '']),
  29958. LinesToStr([ // $mod.$main
  29959. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  29960. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  29961. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  29962. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  29963. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  29964. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  29965. '']));
  29966. end;
  29967. procedure TTestModule.TestJSValue_ProcType_Param;
  29968. begin
  29969. StartProgram(false);
  29970. Add([
  29971. 'type',
  29972. ' variant = jsvalue;',
  29973. ' TArrVariant = array of variant;',
  29974. ' TArrVar2 = TArrVariant;',
  29975. ' TFuncInt = function: longint;',
  29976. 'function GetIt: longint;',
  29977. 'begin',
  29978. 'end;',
  29979. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  29980. 'var v: variant;',
  29981. 'begin',
  29982. ' v:=arr[1];',
  29983. 'end;',
  29984. 'var s: string;',
  29985. 'begin',
  29986. ' DoIt(GetIt,[]);',
  29987. ' DoIt(@GetIt,[]);',
  29988. ' DoIt(1,[s,GetIt]);',
  29989. ' DoIt(1,[s,@GetIt]);',
  29990. '']);
  29991. ConvertProgram;
  29992. CheckSource('TestJSValue_ProcType_Param',
  29993. LinesToStr([ // statements
  29994. 'this.GetIt = function () {',
  29995. ' var Result = 0;',
  29996. ' return Result;',
  29997. '};',
  29998. 'this.DoIt = function (p, Arr) {',
  29999. ' var v = undefined;',
  30000. ' v = Arr[1];',
  30001. '};',
  30002. 'this.s = "";',
  30003. '']),
  30004. LinesToStr([ // $mod.$main
  30005. '$mod.DoIt($mod.GetIt(), []);',
  30006. '$mod.DoIt($mod.GetIt, []);',
  30007. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  30008. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  30009. '']));
  30010. end;
  30011. procedure TTestModule.TestJSValue_AssignToPointerFail;
  30012. begin
  30013. StartProgram(false);
  30014. Add([
  30015. 'var',
  30016. ' v: JSValue;',
  30017. ' p: Pointer;',
  30018. 'begin',
  30019. ' p:=v;',
  30020. '']);
  30021. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  30022. nIncompatibleTypesGotExpected);
  30023. ConvertProgram;
  30024. end;
  30025. procedure TTestModule.TestJSValue_OverloadDouble;
  30026. begin
  30027. StartProgram(false);
  30028. Add([
  30029. 'type',
  30030. ' integer = longint;',
  30031. ' tdatetime = double;',
  30032. 'procedure DoIt(d: double); begin end;',
  30033. 'procedure DoIt(v: jsvalue); begin end;',
  30034. 'var',
  30035. ' d: double;',
  30036. ' dt: tdatetime;',
  30037. ' i: integer;',
  30038. ' b: byte;',
  30039. ' shi: shortint;',
  30040. ' w: word;',
  30041. ' smi: smallint;',
  30042. ' lw: longword;',
  30043. ' li: longint;',
  30044. ' ni: nativeint;',
  30045. ' nu: nativeuint;',
  30046. 'begin',
  30047. ' DoIt(d);',
  30048. ' DoIt(dt);',
  30049. ' DoIt(i);',
  30050. ' DoIt(b);',
  30051. ' DoIt(shi);',
  30052. ' DoIt(w);',
  30053. ' DoIt(smi);',
  30054. ' DoIt(lw);',
  30055. ' DoIt(li);',
  30056. ' DoIt(ni);',
  30057. ' DoIt(nu);',
  30058. '']);
  30059. ConvertProgram;
  30060. CheckSource('TestJSValue_OverloadDouble',
  30061. LinesToStr([ // statements
  30062. 'this.DoIt = function (d) {',
  30063. '};',
  30064. 'this.DoIt$1 = function (v) {',
  30065. '};',
  30066. 'this.d = 0.0;',
  30067. 'this.dt = 0.0;',
  30068. 'this.i = 0;',
  30069. 'this.b = 0;',
  30070. 'this.shi = 0;',
  30071. 'this.w = 0;',
  30072. 'this.smi = 0;',
  30073. 'this.lw = 0;',
  30074. 'this.li = 0;',
  30075. 'this.ni = 0;',
  30076. 'this.nu = 0;',
  30077. '']),
  30078. LinesToStr([ // $mod.$main
  30079. '$mod.DoIt($mod.d);',
  30080. '$mod.DoIt($mod.dt);',
  30081. '$mod.DoIt$1($mod.i);',
  30082. '$mod.DoIt$1($mod.b);',
  30083. '$mod.DoIt$1($mod.shi);',
  30084. '$mod.DoIt$1($mod.w);',
  30085. '$mod.DoIt$1($mod.smi);',
  30086. '$mod.DoIt$1($mod.lw);',
  30087. '$mod.DoIt$1($mod.li);',
  30088. '$mod.DoIt$1($mod.ni);',
  30089. '$mod.DoIt$1($mod.nu);',
  30090. '']));
  30091. end;
  30092. procedure TTestModule.TestJSValue_OverloadNativeInt;
  30093. begin
  30094. StartProgram(false);
  30095. Add([
  30096. 'type',
  30097. ' integer = longint;',
  30098. ' int53 = nativeint;',
  30099. ' tdatetime = double;',
  30100. 'procedure DoIt(n: nativeint); begin end;',
  30101. 'procedure DoIt(v: jsvalue); begin end;',
  30102. 'var',
  30103. ' d: double;',
  30104. ' dt: tdatetime;',
  30105. ' i: integer;',
  30106. ' b: byte;',
  30107. ' shi: shortint;',
  30108. ' w: word;',
  30109. ' smi: smallint;',
  30110. ' lw: longword;',
  30111. ' li: longint;',
  30112. ' ni: nativeint;',
  30113. ' nu: nativeuint;',
  30114. 'begin',
  30115. ' DoIt(d);',
  30116. ' DoIt(dt);',
  30117. ' DoIt(i);',
  30118. ' DoIt(b);',
  30119. ' DoIt(shi);',
  30120. ' DoIt(w);',
  30121. ' DoIt(smi);',
  30122. ' DoIt(lw);',
  30123. ' DoIt(li);',
  30124. ' DoIt(ni);',
  30125. ' DoIt(nu);',
  30126. '']);
  30127. ConvertProgram;
  30128. CheckSource('TestJSValue_OverloadNativeInt',
  30129. LinesToStr([ // statements
  30130. 'this.DoIt = function (n) {',
  30131. '};',
  30132. 'this.DoIt$1 = function (v) {',
  30133. '};',
  30134. 'this.d = 0.0;',
  30135. 'this.dt = 0.0;',
  30136. 'this.i = 0;',
  30137. 'this.b = 0;',
  30138. 'this.shi = 0;',
  30139. 'this.w = 0;',
  30140. 'this.smi = 0;',
  30141. 'this.lw = 0;',
  30142. 'this.li = 0;',
  30143. 'this.ni = 0;',
  30144. 'this.nu = 0;',
  30145. '']),
  30146. LinesToStr([ // $mod.$main
  30147. '$mod.DoIt$1($mod.d);',
  30148. '$mod.DoIt$1($mod.dt);',
  30149. '$mod.DoIt($mod.i);',
  30150. '$mod.DoIt($mod.b);',
  30151. '$mod.DoIt($mod.shi);',
  30152. '$mod.DoIt($mod.w);',
  30153. '$mod.DoIt($mod.smi);',
  30154. '$mod.DoIt($mod.lw);',
  30155. '$mod.DoIt($mod.li);',
  30156. '$mod.DoIt($mod.ni);',
  30157. '$mod.DoIt($mod.nu);',
  30158. '']));
  30159. end;
  30160. procedure TTestModule.TestJSValue_OverloadWord;
  30161. begin
  30162. StartProgram(false);
  30163. Add([
  30164. 'type',
  30165. ' integer = longint;',
  30166. ' int53 = nativeint;',
  30167. ' tdatetime = double;',
  30168. 'procedure DoIt(w: word); begin end;',
  30169. 'procedure DoIt(v: jsvalue); begin end;',
  30170. 'var',
  30171. ' d: double;',
  30172. ' dt: tdatetime;',
  30173. ' i: integer;',
  30174. ' b: byte;',
  30175. ' shi: shortint;',
  30176. ' w: word;',
  30177. ' smi: smallint;',
  30178. ' lw: longword;',
  30179. ' li: longint;',
  30180. ' ni: nativeint;',
  30181. ' nu: nativeuint;',
  30182. 'begin',
  30183. ' DoIt(d);',
  30184. ' DoIt(dt);',
  30185. ' DoIt(i);',
  30186. ' DoIt(b);',
  30187. ' DoIt(shi);',
  30188. ' DoIt(w);',
  30189. ' DoIt(smi);',
  30190. ' DoIt(lw);',
  30191. ' DoIt(li);',
  30192. ' DoIt(ni);',
  30193. ' DoIt(nu);',
  30194. '']);
  30195. ConvertProgram;
  30196. CheckSource('TestJSValue_OverloadWord',
  30197. LinesToStr([ // statements
  30198. 'this.DoIt = function (w) {',
  30199. '};',
  30200. 'this.DoIt$1 = function (v) {',
  30201. '};',
  30202. 'this.d = 0.0;',
  30203. 'this.dt = 0.0;',
  30204. 'this.i = 0;',
  30205. 'this.b = 0;',
  30206. 'this.shi = 0;',
  30207. 'this.w = 0;',
  30208. 'this.smi = 0;',
  30209. 'this.lw = 0;',
  30210. 'this.li = 0;',
  30211. 'this.ni = 0;',
  30212. 'this.nu = 0;',
  30213. '']),
  30214. LinesToStr([ // $mod.$main
  30215. '$mod.DoIt$1($mod.d);',
  30216. '$mod.DoIt$1($mod.dt);',
  30217. '$mod.DoIt$1($mod.i);',
  30218. '$mod.DoIt($mod.b);',
  30219. '$mod.DoIt($mod.shi);',
  30220. '$mod.DoIt($mod.w);',
  30221. '$mod.DoIt$1($mod.smi);',
  30222. '$mod.DoIt$1($mod.lw);',
  30223. '$mod.DoIt$1($mod.li);',
  30224. '$mod.DoIt$1($mod.ni);',
  30225. '$mod.DoIt$1($mod.nu);',
  30226. '']));
  30227. end;
  30228. procedure TTestModule.TestJSValue_OverloadString;
  30229. begin
  30230. StartProgram(false);
  30231. Add([
  30232. 'type',
  30233. ' uni = string;',
  30234. ' WChar = char;',
  30235. 'procedure DoIt(s: string); begin end;',
  30236. 'procedure DoIt(v: jsvalue); begin end;',
  30237. 'var',
  30238. ' s: string;',
  30239. ' c: char;',
  30240. ' u: uni;',
  30241. 'begin',
  30242. ' DoIt(s);',
  30243. ' DoIt(c);',
  30244. ' DoIt(u);',
  30245. '']);
  30246. ConvertProgram;
  30247. CheckSource('TestJSValue_OverloadString',
  30248. LinesToStr([ // statements
  30249. 'this.DoIt = function (s) {',
  30250. '};',
  30251. 'this.DoIt$1 = function (v) {',
  30252. '};',
  30253. 'this.s = "";',
  30254. 'this.c = "\x00";',
  30255. 'this.u = "";',
  30256. '']),
  30257. LinesToStr([ // $mod.$main
  30258. '$mod.DoIt($mod.s);',
  30259. '$mod.DoIt($mod.c);',
  30260. '$mod.DoIt($mod.u);',
  30261. '']));
  30262. end;
  30263. procedure TTestModule.TestJSValue_OverloadChar;
  30264. begin
  30265. StartProgram(false);
  30266. Add([
  30267. 'type',
  30268. ' uni = string;',
  30269. ' WChar = char;',
  30270. 'procedure DoIt(c: char); begin end;',
  30271. 'procedure DoIt(v: jsvalue); begin end;',
  30272. 'var',
  30273. ' s: string;',
  30274. ' c: char;',
  30275. ' u: uni;',
  30276. 'begin',
  30277. ' DoIt(s);',
  30278. ' DoIt(c);',
  30279. ' DoIt(u);',
  30280. '']);
  30281. ConvertProgram;
  30282. CheckSource('TestJSValue_OverloadChar',
  30283. LinesToStr([ // statements
  30284. 'this.DoIt = function (c) {',
  30285. '};',
  30286. 'this.DoIt$1 = function (v) {',
  30287. '};',
  30288. 'this.s = "";',
  30289. 'this.c = "\x00";',
  30290. 'this.u = "";',
  30291. '']),
  30292. LinesToStr([ // $mod.$main
  30293. '$mod.DoIt$1($mod.s);',
  30294. '$mod.DoIt($mod.c);',
  30295. '$mod.DoIt$1($mod.u);',
  30296. '']));
  30297. end;
  30298. procedure TTestModule.TestJSValue_OverloadPointer;
  30299. begin
  30300. StartProgram(false);
  30301. Add([
  30302. 'type',
  30303. ' TObject = class end;',
  30304. 'procedure DoIt(p: pointer); begin end;',
  30305. 'procedure DoIt(v: jsvalue); begin end;',
  30306. 'var',
  30307. ' o: TObject;',
  30308. 'begin',
  30309. ' DoIt(o);',
  30310. '']);
  30311. ConvertProgram;
  30312. CheckSource('TestJSValue_OverloadPointer',
  30313. LinesToStr([ // statements
  30314. 'rtl.createClass(this, "TObject", null, function () {',
  30315. ' this.$init = function () {',
  30316. ' };',
  30317. ' this.$final = function () {',
  30318. ' };',
  30319. '});',
  30320. 'this.DoIt = function (p) {',
  30321. '};',
  30322. 'this.DoIt$1 = function (v) {',
  30323. '};',
  30324. 'this.o = null;',
  30325. '']),
  30326. LinesToStr([ // $mod.$main
  30327. '$mod.DoIt($mod.o);',
  30328. '']));
  30329. end;
  30330. procedure TTestModule.TestJSValue_ForIn;
  30331. begin
  30332. StartProgram(false);
  30333. Add([
  30334. 'var',
  30335. ' v: JSValue;',
  30336. ' key: string;',
  30337. 'begin',
  30338. ' for key in v do begin',
  30339. ' if key=''abc'' then ;',
  30340. ' end;',
  30341. '']);
  30342. ConvertProgram;
  30343. CheckSource('TestJSValue_ForIn',
  30344. LinesToStr([ // statements
  30345. 'this.v = undefined;',
  30346. 'this.key = "";',
  30347. '']),
  30348. LinesToStr([ // $mod.$main
  30349. 'for ($mod.key in $mod.v) {',
  30350. ' if ($mod.key === "abc") ;',
  30351. '};',
  30352. '']));
  30353. end;
  30354. procedure TTestModule.TestRTTI_IntRange;
  30355. begin
  30356. WithTypeInfo:=true;
  30357. StartProgram(true,[supTypeInfo]);
  30358. Add([
  30359. '{$modeswitch externalclass}',
  30360. 'type',
  30361. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  30362. ' TColor = type TGraphicsColor;',
  30363. 'var',
  30364. ' p: TTypeInfo;',
  30365. ' k: TTypeKind;',
  30366. 'begin',
  30367. ' p:=typeinfo(TGraphicsColor);',
  30368. ' p:=typeinfo(TColor);',
  30369. ' k:=GetTypeKind(TGraphicsColor);',
  30370. ' k:=GetTypeKind(TColor);',
  30371. '']);
  30372. ConvertProgram;
  30373. CheckSource('TestRTTI_IntRange',
  30374. LinesToStr([ // statements
  30375. 'this.$rtti.$Int("TGraphicsColor", {',
  30376. ' minvalue: -2147483648,',
  30377. ' maxvalue: 2147483647,',
  30378. ' ordtype: 4',
  30379. '});',
  30380. 'this.$rtti.$inherited("TColor", this.$rtti["TGraphicsColor"], {});',
  30381. 'this.p = null;',
  30382. 'this.k = 0;',
  30383. '']),
  30384. LinesToStr([ // $mod.$main
  30385. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  30386. '$mod.p = $mod.$rtti["TColor"];',
  30387. '$mod.k = 1;',
  30388. '$mod.k = 1;',
  30389. '']));
  30390. end;
  30391. procedure TTestModule.TestRTTI_Double;
  30392. begin
  30393. WithTypeInfo:=true;
  30394. StartProgram(true,[supTypeInfo]);
  30395. Add([
  30396. '{$modeswitch externalclass}',
  30397. 'type',
  30398. ' TFloat = type double;',
  30399. 'var',
  30400. ' p: TTypeInfo;',
  30401. 'begin',
  30402. ' p:=typeinfo(double);',
  30403. ' p:=typeinfo(TFloat);',
  30404. '']);
  30405. ConvertProgram;
  30406. CheckSource('TestRTTI_Double',
  30407. LinesToStr([ // statements
  30408. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  30409. 'this.p = null;',
  30410. '']),
  30411. LinesToStr([ // $mod.$main
  30412. '$mod.p = rtl.double;',
  30413. '$mod.p = $mod.$rtti["TFloat"];',
  30414. '']));
  30415. end;
  30416. procedure TTestModule.TestRTTI_ProcType;
  30417. begin
  30418. WithTypeInfo:=true;
  30419. StartProgram(false);
  30420. Add('type');
  30421. Add(' TProcA = procedure;');
  30422. Add(' TMethodB = procedure of object;');
  30423. Add(' TProcC = procedure; varargs;');
  30424. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  30425. Add(' TProcE = function: nativeint;');
  30426. Add(' TProcF = function(const p: TProcA): nativeuint;');
  30427. Add('var p: pointer;');
  30428. Add('begin');
  30429. Add(' p:=typeinfo(tproca);');
  30430. ConvertProgram;
  30431. CheckSource('TestRTTI_ProcType',
  30432. LinesToStr([ // statements
  30433. 'this.$rtti.$ProcVar("TProcA", {',
  30434. ' procsig: rtl.newTIProcSig([])',
  30435. '});',
  30436. 'this.$rtti.$MethodVar("TMethodB", {',
  30437. ' procsig: rtl.newTIProcSig([]),',
  30438. ' methodkind: 0',
  30439. '});',
  30440. 'this.$rtti.$ProcVar("TProcC", {',
  30441. ' procsig: rtl.newTIProcSig([], null, 2)',
  30442. '});',
  30443. 'this.$rtti.$ProcVar("TProcD", {',
  30444. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  30445. '});',
  30446. 'this.$rtti.$ProcVar("TProcE", {',
  30447. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  30448. '});',
  30449. 'this.$rtti.$ProcVar("TProcF", {',
  30450. ' procsig: rtl.newTIProcSig([["p", this.$rtti["TProcA"], 2]], rtl.nativeuint)',
  30451. '});',
  30452. 'this.p = null;',
  30453. '']),
  30454. LinesToStr([ // $mod.$main
  30455. '$mod.p = $mod.$rtti["TProcA"];',
  30456. '']));
  30457. end;
  30458. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  30459. begin
  30460. WithTypeInfo:=true;
  30461. AddModuleWithIntfImplSrc('unit2.pas',
  30462. LinesToStr([
  30463. 'type',
  30464. ' TObject = class end;'
  30465. ]),
  30466. '');
  30467. StartUnit(true);
  30468. Add('interface');
  30469. Add('uses unit2;');
  30470. Add('type');
  30471. Add(' TProcA = function(o: tobject): tobject;');
  30472. Add('implementation');
  30473. Add('type');
  30474. Add(' TProcB = function(o: tobject): tobject;');
  30475. Add('var p: Pointer;');
  30476. Add('initialization');
  30477. Add(' p:=typeinfo(tproca);');
  30478. Add(' p:=typeinfo(tprocb);');
  30479. ConvertUnit;
  30480. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  30481. LinesToStr([ // statements
  30482. 'var $impl = $mod.$impl;',
  30483. 'this.$rtti.$ProcVar("TProcA", {',
  30484. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  30485. '});',
  30486. '']),
  30487. LinesToStr([ // this.$init
  30488. '$impl.p = $mod.$rtti["TProcA"];',
  30489. '$impl.p = $mod.$rtti["TProcB"];',
  30490. '']),
  30491. LinesToStr([ // implementation
  30492. '$mod.$rtti.$ProcVar("TProcB", {',
  30493. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  30494. '});',
  30495. '$impl.p = null;',
  30496. '']) );
  30497. end;
  30498. procedure TTestModule.TestRTTI_ProcTypeAnonymous;
  30499. begin
  30500. WithTypeInfo:=true;
  30501. StartProgram(false);
  30502. Add(['var',
  30503. ' ProcA: procedure;',
  30504. ' MethodB: procedure of object;',
  30505. ' ProcC: procedure; varargs;',
  30506. ' ProcD: procedure(i: longint; const j: string; var c: char; out d: double);',
  30507. ' ProcE: function: nativeint;',
  30508. ' p: pointer;',
  30509. 'begin',
  30510. ' p:=typeinfo(proca);']);
  30511. ConvertProgram;
  30512. CheckSource('TestRTTI_ProcTypeAnonymous',
  30513. LinesToStr([ // statements
  30514. 'this.$rtti.$ProcVar("ProcA$a", {',
  30515. ' procsig: rtl.newTIProcSig([])',
  30516. '});',
  30517. 'this.ProcA = null;',
  30518. 'this.$rtti.$MethodVar("MethodB$a", {',
  30519. ' procsig: rtl.newTIProcSig([]),',
  30520. ' methodkind: 0',
  30521. '});',
  30522. 'this.MethodB = null;',
  30523. 'this.$rtti.$ProcVar("ProcC$a", {',
  30524. ' procsig: rtl.newTIProcSig([], null, 2)',
  30525. '});',
  30526. 'this.ProcC = null;',
  30527. 'this.$rtti.$ProcVar("ProcD$a", {',
  30528. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  30529. '});',
  30530. 'this.ProcD = null;',
  30531. 'this.$rtti.$ProcVar("ProcE$a", {',
  30532. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  30533. '});',
  30534. 'this.ProcE = null;',
  30535. 'this.p = null;',
  30536. '']),
  30537. LinesToStr([ // $mod.$main
  30538. '$mod.p = $mod.$rtti["ProcA$a"];',
  30539. '']));
  30540. end;
  30541. procedure TTestModule.TestRTTI_EnumAndSetType;
  30542. begin
  30543. WithTypeInfo:=true;
  30544. StartProgram(false);
  30545. Add('type');
  30546. Add(' TFlag = (light,dark);');
  30547. Add(' TFlags = set of TFlag;');
  30548. Add(' TProc = function(f: TFlags): TFlag;');
  30549. Add('var p: pointer;');
  30550. Add('begin');
  30551. Add(' p:=typeinfo(tflag);');
  30552. Add(' p:=typeinfo(tflags);');
  30553. ConvertProgram;
  30554. CheckSource('TestRTTI_EnumAndType',
  30555. LinesToStr([ // statements
  30556. 'this.TFlag = {',
  30557. ' "0": "light",',
  30558. ' light: 0,',
  30559. ' "1": "dark",',
  30560. ' dark: 1',
  30561. '};',
  30562. 'this.$rtti.$Enum("TFlag", {',
  30563. ' minvalue: 0,',
  30564. ' maxvalue: 1,',
  30565. ' ordtype: 1,',
  30566. ' enumtype: this.TFlag',
  30567. '});',
  30568. 'this.$rtti.$Set("TFlags", {',
  30569. ' comptype: this.$rtti["TFlag"]',
  30570. '});',
  30571. 'this.$rtti.$ProcVar("TProc", {',
  30572. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TFlags"]]], this.$rtti["TFlag"])',
  30573. '});',
  30574. 'this.p = null;',
  30575. '']),
  30576. LinesToStr([ // $mod.$main
  30577. '$mod.p = $mod.$rtti["TFlag"];',
  30578. '$mod.p = $mod.$rtti["TFlags"];',
  30579. '']));
  30580. end;
  30581. procedure TTestModule.TestRTTI_EnumRange;
  30582. begin
  30583. WithTypeInfo:=true;
  30584. StartProgram(false);
  30585. Add([
  30586. 'type',
  30587. ' TCol = (red,green,blue);',
  30588. ' TColRg = green..blue;',
  30589. ' TSetOfColRg = set of TColRg;',
  30590. 'var p: pointer;',
  30591. 'begin',
  30592. ' p:=typeinfo(tcolrg);',
  30593. ' p:=typeinfo(tsetofcolrg);',
  30594. '']);
  30595. ConvertProgram;
  30596. end;
  30597. procedure TTestModule.TestRTTI_AnonymousEnumType;
  30598. begin
  30599. WithTypeInfo:=true;
  30600. StartProgram(false);
  30601. Add('type');
  30602. Add(' TFlags = set of (red, green);');
  30603. Add('var');
  30604. Add(' f: TFlags;');
  30605. Add('begin');
  30606. Add(' Include(f,red);');
  30607. ConvertProgram;
  30608. CheckSource('TestRTTI_AnonymousEnumType',
  30609. LinesToStr([ // statements
  30610. 'this.TFlags$a = {',
  30611. ' "0": "red",',
  30612. ' red: 0,',
  30613. ' "1": "green",',
  30614. ' green: 1',
  30615. '};',
  30616. 'this.$rtti.$Enum("TFlags$a", {',
  30617. ' minvalue: 0,',
  30618. ' maxvalue: 1,',
  30619. ' ordtype: 1,',
  30620. ' enumtype: this.TFlags$a',
  30621. '});',
  30622. 'this.$rtti.$Set("TFlags", {',
  30623. ' comptype: this.$rtti["TFlags$a"]',
  30624. '});',
  30625. 'this.f = {};',
  30626. '']),
  30627. LinesToStr([
  30628. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  30629. '']));
  30630. end;
  30631. procedure TTestModule.TestRTTI_StaticArray;
  30632. begin
  30633. WithTypeInfo:=true;
  30634. StartProgram(false);
  30635. Add('type');
  30636. Add(' TFlag = (light,dark);');
  30637. Add(' TFlagNames = array[TFlag] of string;');
  30638. Add(' TBoolNames = array[boolean] of string;');
  30639. Add(' TByteArray = array[1..32768] of byte;');
  30640. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  30641. Add('var p: pointer;');
  30642. Add('begin');
  30643. Add(' p:=typeinfo(TFlagNames);');
  30644. Add(' p:=typeinfo(TBoolNames);');
  30645. ConvertProgram;
  30646. CheckSource('TestRTTI_StaticArray',
  30647. LinesToStr([ // statements
  30648. 'this.TFlag = {',
  30649. ' "0": "light",',
  30650. ' light: 0,',
  30651. ' "1": "dark",',
  30652. ' dark: 1',
  30653. '};',
  30654. 'this.$rtti.$Enum("TFlag", {',
  30655. ' minvalue: 0,',
  30656. ' maxvalue: 1,',
  30657. ' ordtype: 1,',
  30658. ' enumtype: this.TFlag',
  30659. '});',
  30660. 'this.$rtti.$StaticArray("TFlagNames", {',
  30661. ' dims: [2],',
  30662. ' eltype: rtl.string',
  30663. '});',
  30664. 'this.$rtti.$StaticArray("TBoolNames", {',
  30665. ' dims: [2],',
  30666. ' eltype: rtl.string',
  30667. '});',
  30668. 'this.$rtti.$StaticArray("TByteArray", {',
  30669. ' dims: [32768],',
  30670. ' eltype: rtl.byte',
  30671. '});',
  30672. 'this.$rtti.$ProcVar("TProc", {',
  30673. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TBoolNames"]]], this.$rtti["TFlagNames"])',
  30674. '});',
  30675. 'this.p = null;',
  30676. '']),
  30677. LinesToStr([ // $mod.$main
  30678. '$mod.p = $mod.$rtti["TFlagNames"];',
  30679. '$mod.p = $mod.$rtti["TBoolNames"];',
  30680. '']));
  30681. end;
  30682. procedure TTestModule.TestRTTI_DynArray;
  30683. begin
  30684. WithTypeInfo:=true;
  30685. StartProgram(false);
  30686. Add('type');
  30687. Add(' TArrStr = array of string;');
  30688. Add(' TArr2Dim = array of tarrstr;');
  30689. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  30690. Add('var p: pointer;');
  30691. Add('begin');
  30692. Add(' p:=typeinfo(tarrstr);');
  30693. Add(' p:=typeinfo(tarr2dim);');
  30694. ConvertProgram;
  30695. CheckSource('TestRTTI_DynArray',
  30696. LinesToStr([ // statements
  30697. 'this.$rtti.$DynArray("TArrStr", {',
  30698. ' eltype: rtl.string',
  30699. '});',
  30700. 'this.$rtti.$DynArray("TArr2Dim", {',
  30701. ' eltype: this.$rtti["TArrStr"]',
  30702. '});',
  30703. 'this.$rtti.$ProcVar("TProc", {',
  30704. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TArrStr"]]], this.$rtti["TArr2Dim"])',
  30705. '});',
  30706. 'this.p = null;',
  30707. '']),
  30708. LinesToStr([ // $mod.$main
  30709. '$mod.p = $mod.$rtti["TArrStr"];',
  30710. '$mod.p = $mod.$rtti["TArr2Dim"];',
  30711. '']));
  30712. end;
  30713. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  30714. begin
  30715. WithTypeInfo:=true;
  30716. StartProgram(false);
  30717. Add('type');
  30718. Add(' TArr = array of array of longint;');
  30719. Add('var a: TArr;');
  30720. Add('begin');
  30721. ConvertProgram;
  30722. CheckSource('TestRTTI_ArrayNestedAnonymous',
  30723. LinesToStr([ // statements
  30724. 'this.$rtti.$DynArray("TArr$a", {',
  30725. ' eltype: rtl.longint',
  30726. '});',
  30727. 'this.$rtti.$DynArray("TArr", {',
  30728. ' eltype: this.$rtti["TArr$a"]',
  30729. '});',
  30730. 'this.a = [];',
  30731. '']),
  30732. LinesToStr([ // $mod.$main
  30733. ]));
  30734. end;
  30735. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  30736. begin
  30737. WithTypeInfo:=true;
  30738. StartProgram(false);
  30739. Add('type');
  30740. Add(' TObject = class');
  30741. Add(' published');
  30742. Add(' procedure Proc; virtual; abstract;');
  30743. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  30744. Add(' end;');
  30745. Add('begin');
  30746. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  30747. nDuplicatePublishedMethodXAtY);
  30748. ConvertProgram;
  30749. end;
  30750. procedure TTestModule.TestRTTI_PublishedMethodHideNoHint;
  30751. begin
  30752. WithTypeInfo:=true;
  30753. StartUnit(false);
  30754. Add([
  30755. 'interface',
  30756. 'type',
  30757. ' TObject = class',
  30758. ' end;',
  30759. ' {$M+}',
  30760. ' TBird = class',
  30761. ' procedure Fly;',
  30762. ' end;',
  30763. ' {$M-}',
  30764. 'type',
  30765. ' TEagle = class(TBird)',
  30766. ' procedure Fly;',
  30767. ' end;',
  30768. 'implementation',
  30769. 'procedure TBird.Fly;',
  30770. 'begin',
  30771. 'end;',
  30772. 'procedure TEagle.Fly;',
  30773. 'begin',
  30774. 'end;',
  30775. '']);
  30776. ConvertUnit;
  30777. CheckSource('TestRTTI_PublishedMethodHideNoHint',
  30778. LinesToStr([ // statements
  30779. 'rtl.createClass(this, "TObject", null, function () {',
  30780. ' this.$init = function () {',
  30781. ' };',
  30782. ' this.$final = function () {',
  30783. ' };',
  30784. '});',
  30785. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30786. ' this.Fly = function () {',
  30787. ' };',
  30788. ' var $r = this.$rtti;',
  30789. ' $r.addMethod("Fly", 0, [], 4);',
  30790. '});',
  30791. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  30792. ' this.Fly = function () {',
  30793. ' };',
  30794. ' var $r = this.$rtti;',
  30795. ' $r.addMethod("Fly", 0, [], 4);',
  30796. '});',
  30797. '']),
  30798. LinesToStr([ // $mod.$main
  30799. ]));
  30800. CheckResolverUnexpectedHints(true);
  30801. end;
  30802. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  30803. begin
  30804. WithTypeInfo:=true;
  30805. StartProgram(false);
  30806. Add('type');
  30807. Add(' TObject = class');
  30808. Add(' published');
  30809. Add(' procedure Proc; external name ''foo'';');
  30810. Add(' end;');
  30811. Add('begin');
  30812. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  30813. nPublishedNameMustMatchExternal);
  30814. ConvertProgram;
  30815. end;
  30816. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  30817. begin
  30818. WithTypeInfo:=true;
  30819. StartProgram(false);
  30820. Add('type');
  30821. Add(' TObject = class');
  30822. Add(' class var FA: longint;');
  30823. Add(' published');
  30824. Add(' class property A: longint read FA;');
  30825. Add(' end;');
  30826. Add('begin');
  30827. SetExpectedPasResolverError('Invalid published property modifier "class"',
  30828. nInvalidXModifierY);
  30829. ConvertProgram;
  30830. end;
  30831. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  30832. begin
  30833. WithTypeInfo:=true;
  30834. StartProgram(false);
  30835. Add('type');
  30836. Add(' TObject = class');
  30837. Add(' published');
  30838. Add(' class var FA: longint;');
  30839. Add(' end;');
  30840. Add('begin');
  30841. SetExpectedPasResolverError(sSymbolCannotBePublished,
  30842. nSymbolCannotBePublished);
  30843. ConvertProgram;
  30844. end;
  30845. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  30846. begin
  30847. WithTypeInfo:=true;
  30848. StartProgram(false);
  30849. Add('{$modeswitch externalclass}');
  30850. Add('type');
  30851. Add(' TObject = class');
  30852. Add(' published');
  30853. Add(' V: longint; external name ''foo'';');
  30854. Add(' end;');
  30855. Add('begin');
  30856. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  30857. nPublishedNameMustMatchExternal);
  30858. ConvertProgram;
  30859. end;
  30860. procedure TTestModule.TestRTTI_Class_Field;
  30861. begin
  30862. WithTypeInfo:=true;
  30863. StartProgram(false);
  30864. Add('{$modeswitch externalclass}');
  30865. Add('type');
  30866. Add(' TObject = class');
  30867. Add(' private');
  30868. Add(' FPropA: string;');
  30869. Add(' published');
  30870. Add(' VarLI: longint;');
  30871. Add(' VarC: char;');
  30872. Add(' VarS: string;');
  30873. Add(' VarD: double;');
  30874. Add(' VarB: boolean;');
  30875. Add(' VarLW: longword;');
  30876. Add(' VarSmI: smallint;');
  30877. Add(' VarW: word;');
  30878. Add(' VarShI: shortint;');
  30879. Add(' VarBy: byte;');
  30880. Add(' VarExt: longint external name ''VarExt'';');
  30881. Add(' ArrA, ArrB: array of byte;');
  30882. Add(' end;');
  30883. Add('var p: pointer;');
  30884. Add(' Obj: tobject;');
  30885. Add('begin');
  30886. Add(' p:=typeinfo(tobject);');
  30887. Add(' p:=typeinfo(p);');
  30888. Add(' p:=typeinfo(obj);');
  30889. ConvertProgram;
  30890. CheckSource('TestRTTI_Class_Field',
  30891. LinesToStr([ // statements
  30892. 'rtl.createClass(this, "TObject", null, function () {',
  30893. ' this.$init = function () {',
  30894. ' this.FPropA = "";',
  30895. ' this.VarLI = 0;',
  30896. ' this.VarC = "\x00";',
  30897. ' this.VarS = "";',
  30898. ' this.VarD = 0.0;',
  30899. ' this.VarB = false;',
  30900. ' this.VarLW = 0;',
  30901. ' this.VarSmI = 0;',
  30902. ' this.VarW = 0;',
  30903. ' this.VarShI = 0;',
  30904. ' this.VarBy = 0;',
  30905. ' this.ArrA = [];',
  30906. ' this.ArrB = [];',
  30907. ' };',
  30908. ' this.$final = function () {',
  30909. ' this.ArrA = undefined;',
  30910. ' this.ArrB = undefined;',
  30911. ' };',
  30912. ' var $r = this.$rtti;',
  30913. ' $r.addField("VarLI", rtl.longint, 4);',
  30914. ' $r.addField("VarC", rtl.char, 4);',
  30915. ' $r.addField("VarS", rtl.string, 4);',
  30916. ' $r.addField("VarD", rtl.double, 4);',
  30917. ' $r.addField("VarB", rtl.boolean, 4);',
  30918. ' $r.addField("VarLW", rtl.longword, 4);',
  30919. ' $r.addField("VarSmI", rtl.smallint, 4);',
  30920. ' $r.addField("VarW", rtl.word, 4);',
  30921. ' $r.addField("VarShI", rtl.shortint, 4);',
  30922. ' $r.addField("VarBy", rtl.byte, 4);',
  30923. ' $r.addField("VarExt", rtl.longint, 4);',
  30924. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  30925. ' eltype: rtl.byte',
  30926. ' });',
  30927. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"], 4);',
  30928. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"], 4);',
  30929. '});',
  30930. 'this.p = null;',
  30931. 'this.Obj = null;',
  30932. '']),
  30933. LinesToStr([ // $mod.$main
  30934. '$mod.p = $mod.$rtti["TObject"];',
  30935. '$mod.p = rtl.pointer;',
  30936. '$mod.p = $mod.Obj.$rtti;',
  30937. '']));
  30938. end;
  30939. procedure TTestModule.TestRTTI_Class_FieldPrivate;
  30940. begin
  30941. WithTypeInfo:=true;
  30942. StartProgram(false);
  30943. Add('type');
  30944. Add('{$RTTI explicit fields([vcPrivate,vcProtected,vcPublic,vcPublished])}');
  30945. Add(' TObject = class');
  30946. Add(' strict private');
  30947. Add(' A1: word;');
  30948. Add(' private');
  30949. Add(' A2: word;');
  30950. Add(' strict protected');
  30951. Add(' B1: word;');
  30952. Add(' protected');
  30953. Add(' B2, B3: word;');
  30954. Add(' public');
  30955. Add(' C: word;');
  30956. Add(' published');
  30957. Add(' D: word;');
  30958. Add(' end;');
  30959. Add('begin');
  30960. ConvertProgram;
  30961. CheckSource('TestRTTI_Class_FieldPrivate',
  30962. LinesToStr([ // statements
  30963. 'rtl.createClass(this, "TObject", null, function () {',
  30964. ' this.$init = function () {',
  30965. ' this.A1 = 0;',
  30966. ' this.A2 = 0;',
  30967. ' this.B1 = 0;',
  30968. ' this.B2 = 0;',
  30969. ' this.B3 = 0;',
  30970. ' this.C = 0;',
  30971. ' this.D = 0;',
  30972. ' };',
  30973. ' this.$final = function () {',
  30974. ' };',
  30975. ' var $r = this.$rtti;',
  30976. ' $r.addField("A1", rtl.word, 5);',
  30977. ' $r.addField("A2", rtl.word, 0);',
  30978. ' $r.addField("B1", rtl.word, 6);',
  30979. ' $r.addField("B2", rtl.word, 1);',
  30980. ' $r.addField("B3", rtl.word, 1);',
  30981. ' $r.addField("C", rtl.word);',
  30982. ' $r.addField("D", rtl.word, 3);',
  30983. '});',
  30984. '']),
  30985. LinesToStr([ // $mod.$main
  30986. '']));
  30987. end;
  30988. procedure TTestModule.TestRTTI_Class_Method;
  30989. begin
  30990. WithTypeInfo:=true;
  30991. StartProgram(false);
  30992. Add([
  30993. 'type',
  30994. ' TObject = class',
  30995. ' private',
  30996. ' procedure Internal; external name ''$intern'';',
  30997. ' published',
  30998. ' procedure Click; virtual; abstract;',
  30999. ' procedure Notify(Sender: TObject); virtual; abstract;',
  31000. ' function GetNotify: boolean; external name ''GetNotify'';',
  31001. ' procedure Println(a,b: longint); varargs; virtual; abstract;',
  31002. ' function Fetch(URL: string): word; async; external name ''Fetch'';',
  31003. ' end;',
  31004. 'begin']);
  31005. ConvertProgram;
  31006. CheckSource('TestRTTI_Class_Method',
  31007. LinesToStr([ // statements
  31008. 'rtl.createClass(this, "TObject", null, function () {',
  31009. ' this.$init = function () {',
  31010. ' };',
  31011. ' this.$final = function () {',
  31012. ' };',
  31013. ' var $r = this.$rtti;',
  31014. ' $r.addMethod("Click", 0, [], 4);',
  31015. ' $r.addMethod("Notify", 0, [["Sender", $r]], 4);',
  31016. ' $r.addMethod("GetNotify", 1, [], 4, rtl.boolean, 4);',
  31017. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], 4, null, 2);',
  31018. ' $r.addMethod("Fetch", 1, [["URL", rtl.string]], 4, rtl.word, 20);',
  31019. '});',
  31020. '']),
  31021. LinesToStr([ // $mod.$main
  31022. '']));
  31023. end;
  31024. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  31025. begin
  31026. WithTypeInfo:=true;
  31027. StartProgram(false);
  31028. Add('type');
  31029. Add(' TObject = class');
  31030. Add(' published');
  31031. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  31032. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  31033. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  31034. Add(' end;');
  31035. Add('begin');
  31036. ConvertProgram;
  31037. CheckSource('TestRTTI_Class_MethodOpenArray',
  31038. LinesToStr([ // statements
  31039. 'rtl.createClass(this, "TObject", null, function () {',
  31040. ' this.$init = function () {',
  31041. ' };',
  31042. ' this.$final = function () {',
  31043. ' };',
  31044. ' var $r = this.$rtti;',
  31045. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]], 4);',
  31046. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]], 4);',
  31047. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]], 4);',
  31048. '});',
  31049. '']),
  31050. LinesToStr([ // $mod.$main
  31051. '']));
  31052. end;
  31053. procedure TTestModule.TestRTTI_Class_MethodPrivate;
  31054. begin
  31055. WithTypeInfo:=true;
  31056. StartProgram(false);
  31057. Add('type');
  31058. Add('{$RTTI explicit methods([vcPrivate,vcProtected,vcPublic,vcPublished])}');
  31059. Add(' TObject = class');
  31060. Add(' private');
  31061. Add(' procedure PrivateProc(a: word); virtual; abstract;');
  31062. Add(' protected');
  31063. Add(' class function ProtectedFunc: word; virtual; abstract;');
  31064. Add(' public');
  31065. Add(' class procedure PublicProc; virtual; abstract;');
  31066. Add(' constructor Create;');
  31067. Add(' destructor Destroy;');
  31068. Add(' published');
  31069. Add(' function PublishedProc: word; virtual; abstract;');
  31070. Add(' end;');
  31071. Add('constructor TObject.Create;');
  31072. Add('begin');
  31073. Add('end;');
  31074. Add('destructor TObject.Destroy;');
  31075. Add('begin');
  31076. Add('end;');
  31077. Add('begin');
  31078. ConvertProgram;
  31079. CheckSource('TestRTTI_Class_MethodPrivate',
  31080. LinesToStr([ // statements
  31081. 'rtl.createClass(this, "TObject", null, function () {',
  31082. ' this.$init = function () {',
  31083. ' };',
  31084. ' this.$final = function () {',
  31085. ' };',
  31086. ' this.Create = function () {',
  31087. ' return this;',
  31088. ' };',
  31089. ' this.Destroy = function () {',
  31090. ' };',
  31091. ' var $r = this.$rtti;',
  31092. ' $r.addMethod("PrivateProc", 0, [["a", rtl.word]], 0);',
  31093. ' $r.addMethod("ProtectedFunc", 5, [], 1, rtl.word);',
  31094. ' $r.addMethod("PublicProc", 4, []);',
  31095. ' $r.addMethod("Create", 2, []);',
  31096. ' $r.addMethod("Destroy", 3, []);',
  31097. ' $r.addMethod("PublishedProc", 1, [], 4, rtl.word);',
  31098. '});',
  31099. '']),
  31100. LinesToStr([ // $mod.$main
  31101. '']));
  31102. end;
  31103. procedure TTestModule.TestRTTI_Class_Property;
  31104. begin
  31105. WithTypeInfo:=true;
  31106. StartProgram(false);
  31107. Add('{$modeswitch externalclass}');
  31108. Add('type');
  31109. Add(' TObject = class');
  31110. Add(' private');
  31111. Add(' FColor: longint;');
  31112. Add(' FColorStored: boolean;');
  31113. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  31114. Add(' function GetColor: longint; virtual; abstract;');
  31115. Add(' function GetColorStored: boolean; virtual; abstract;');
  31116. Add(' FExtSize: longint external name ''$extSize'';');
  31117. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  31118. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  31119. Add(' function GetExtSize: longint; external name ''$getSize'';');
  31120. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  31121. Add(' published');
  31122. Add(' property ColorA: longint read FColor;');
  31123. Add(' property ColorB: longint write FColor;');
  31124. Add(' property ColorC: longint read GetColor write SetColor;');
  31125. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  31126. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  31127. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  31128. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  31129. Add(' end;');
  31130. Add('begin');
  31131. ConvertProgram;
  31132. CheckSource('TestRTTI_Class_Property',
  31133. LinesToStr([ // statements
  31134. 'rtl.createClass(this, "TObject", null, function () {',
  31135. ' this.$init = function () {',
  31136. ' this.FColor = 0;',
  31137. ' this.FColorStored = false;',
  31138. ' };',
  31139. ' this.$final = function () {',
  31140. ' };',
  31141. ' var $r = this.$rtti;',
  31142. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  31143. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  31144. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  31145. ' $r.addProperty(',
  31146. ' "ColorD",',
  31147. ' 8,',
  31148. ' rtl.longint,',
  31149. ' "FColor",',
  31150. ' "FColor",',
  31151. ' 4,',
  31152. ' {',
  31153. ' stored: "FColorStored"',
  31154. ' }',
  31155. ' );',
  31156. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  31157. ' $r.addProperty(',
  31158. ' "ExtSizeB",',
  31159. ' 11,',
  31160. ' rtl.longint,',
  31161. ' "$getSize",',
  31162. ' "$setSize",',
  31163. ' 4,',
  31164. ' {',
  31165. ' stored: "$extSizeStored"',
  31166. ' }',
  31167. ' );',
  31168. ' $r.addProperty(',
  31169. ' "ExtSizeC",',
  31170. ' 12,',
  31171. ' rtl.longint,',
  31172. ' "$extSize",',
  31173. ' "$extSize",',
  31174. ' 4,',
  31175. ' {',
  31176. ' stored: "$getExtSizeStored"',
  31177. ' }',
  31178. ' );',
  31179. '});',
  31180. '']),
  31181. LinesToStr([ // $mod.$main
  31182. '']));
  31183. end;
  31184. procedure TTestModule.TestRTTI_Class_PropertyParams;
  31185. begin
  31186. WithTypeInfo:=true;
  31187. StartProgram(false);
  31188. Add('{$modeswitch externalclass}');
  31189. Add('type');
  31190. Add(' integer = longint;');
  31191. Add(' TObject = class');
  31192. Add(' private');
  31193. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  31194. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  31195. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  31196. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  31197. Add(' published');
  31198. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  31199. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  31200. Add(' end;');
  31201. Add('begin');
  31202. ConvertProgram;
  31203. CheckSource('TestRTTI_Class_PropertyParams',
  31204. LinesToStr([ // statements
  31205. 'rtl.createClass(this, "TObject", null, function () {',
  31206. ' this.$init = function () {',
  31207. ' };',
  31208. ' this.$final = function () {',
  31209. ' };',
  31210. ' var $r = this.$rtti;',
  31211. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  31212. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  31213. '});',
  31214. '']),
  31215. LinesToStr([ // $mod.$main
  31216. '']));
  31217. end;
  31218. procedure TTestModule.TestRTTI_Class_PropertyPrivate;
  31219. begin
  31220. WithTypeInfo:=true;
  31221. StartProgram(false);
  31222. Add('type');
  31223. Add('{$RTTI explicit properties([vcPrivate,vcProtected,vcPublic,vcPublished])}');
  31224. Add(' TObject = class');
  31225. Add(' private');
  31226. Add(' FWord: word;');
  31227. Add(' function GetWord: word; virtual; abstract;');
  31228. Add(' procedure SetWord(Value: word); virtual; abstract;');
  31229. Add(' property PrivateWord: word read FWord write FWord;');
  31230. Add(' protected');
  31231. Add(' property ProtectedWord: word read FWord write SetWord;');
  31232. Add(' public');
  31233. Add(' property PublicWord: word read GetWord;');
  31234. Add(' published');
  31235. Add(' property PublishedWord: word read FWord;');
  31236. Add(' end;');
  31237. Add('begin');
  31238. ConvertProgram;
  31239. CheckSource('TestRTTI_Class_PropertyPrivate',
  31240. LinesToStr([ // statements
  31241. 'rtl.createClass(this, "TObject", null, function () {',
  31242. ' this.$init = function () {',
  31243. ' this.FWord = 0;',
  31244. ' };',
  31245. ' this.$final = function () {',
  31246. ' };',
  31247. ' var $r = this.$rtti;',
  31248. ' $r.addProperty(',
  31249. ' "PrivateWord",',
  31250. ' 0,',
  31251. ' rtl.word,',
  31252. ' "FWord",',
  31253. ' "FWord",',
  31254. ' 0',
  31255. ' );',
  31256. ' $r.addProperty(',
  31257. ' "ProtectedWord",',
  31258. ' 2,',
  31259. ' rtl.word,',
  31260. ' "FWord",',
  31261. ' "SetWord",',
  31262. ' 1',
  31263. ' );',
  31264. ' $r.addProperty("PublicWord", 1, rtl.word, "GetWord", "", 2);',
  31265. ' $r.addProperty(',
  31266. ' "PublishedWord",',
  31267. ' 0,',
  31268. ' rtl.word,',
  31269. ' "FWord",',
  31270. ' ""',
  31271. ' );',
  31272. '});',
  31273. '']),
  31274. LinesToStr([ // $mod.$main
  31275. '']));
  31276. end;
  31277. procedure TTestModule.TestRTTI_Class_ClassProperty;
  31278. begin
  31279. WithTypeInfo:=true;
  31280. StartProgram(false);
  31281. Add('type');
  31282. Add('{$RTTI explicit properties([vcPrivate,vcProtected,vcPublic,vcPublished])}');
  31283. Add(' TObject = class');
  31284. Add(' private');
  31285. Add(' class var FWord: word;');
  31286. Add(' class function GetWord: word; virtual; abstract;');
  31287. Add(' class procedure SetWord(Value: word); virtual; abstract;');
  31288. Add(' class property PrivateWord: word read FWord write FWord;');
  31289. Add(' protected');
  31290. Add(' class property ProtectedWord: word read FWord write SetWord;');
  31291. Add(' public');
  31292. Add(' class property PublicWord: word read GetWord;');
  31293. Add(' end;');
  31294. Add('begin');
  31295. ConvertProgram;
  31296. CheckSource('TestRTTI_Class_ClassProperty',
  31297. LinesToStr([ // statements
  31298. 'rtl.createClass(this, "TObject", null, function () {',
  31299. ' this.FWord = 0;',
  31300. ' this.$init = function () {',
  31301. ' };',
  31302. ' this.$final = function () {',
  31303. ' };',
  31304. ' var $r = this.$rtti;',
  31305. ' $r.addProperty(',
  31306. ' "PrivateWord",',
  31307. ' 32,',
  31308. ' rtl.word,',
  31309. ' "FWord",',
  31310. ' "FWord",',
  31311. ' 0',
  31312. ' );',
  31313. ' $r.addProperty(',
  31314. ' "ProtectedWord",',
  31315. ' 34,',
  31316. ' rtl.word,',
  31317. ' "FWord",',
  31318. ' "SetWord",',
  31319. ' 1',
  31320. ' );',
  31321. ' $r.addProperty(',
  31322. ' "PublicWord",',
  31323. ' 33,',
  31324. ' rtl.word,',
  31325. ' "GetWord",',
  31326. ' "",',
  31327. ' 2',
  31328. ' );',
  31329. '});',
  31330. '']),
  31331. LinesToStr([ // $mod.$main
  31332. '']));
  31333. end;
  31334. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  31335. begin
  31336. WithTypeInfo:=true;
  31337. AddModuleWithIntfImplSrc('unit1.pas',
  31338. 'type TColor = -5..5;',
  31339. '');
  31340. StartProgram(true);
  31341. Add([
  31342. 'uses unit1;',
  31343. 'type',
  31344. ' TColorAlias = TColor;',
  31345. ' TColorTypeAlias = type TColor;',
  31346. ' TObject = class',
  31347. ' private',
  31348. ' fColor: TColor;',
  31349. ' fAlias: TColorAlias;',
  31350. ' fTypeAlias: TColorTypeAlias;',
  31351. ' published',
  31352. ' property Color: TColor read fcolor;',
  31353. ' property Alias: TColorAlias read falias;',
  31354. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  31355. ' end;',
  31356. 'begin',
  31357. '']);
  31358. ConvertProgram;
  31359. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  31360. LinesToStr([ // statements
  31361. 'this.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  31362. 'rtl.createClass(this, "TObject", null, function () {',
  31363. ' this.$init = function () {',
  31364. ' this.fColor = 0;',
  31365. ' this.fAlias = 0;',
  31366. ' this.fTypeAlias = 0;',
  31367. ' };',
  31368. ' this.$final = function () {',
  31369. ' };',
  31370. ' var $r = this.$rtti;',
  31371. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  31372. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  31373. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  31374. '});',
  31375. '']),
  31376. LinesToStr([ // $mod.$main
  31377. '']));
  31378. end;
  31379. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  31380. begin
  31381. WithTypeInfo:=true;
  31382. StartProgram(false);
  31383. Add([
  31384. '{$modeswitch omitrtti}',
  31385. 'type',
  31386. ' TObject = class',
  31387. ' private',
  31388. ' FA: byte;',
  31389. ' published',
  31390. ' property A: byte read FA write FA;',
  31391. ' end;',
  31392. 'begin']);
  31393. ConvertProgram;
  31394. CheckSource('TestRTTI_Class_OmitRTTI',
  31395. LinesToStr([ // statements
  31396. 'rtl.createClass(this, "TObject", null, function () {',
  31397. ' this.$init = function () {',
  31398. ' this.FA = 0;',
  31399. ' };',
  31400. ' this.$final = function () {',
  31401. ' };',
  31402. '});',
  31403. '']),
  31404. LinesToStr([ // $mod.$main
  31405. '']));
  31406. end;
  31407. procedure TTestModule.TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  31408. begin
  31409. WithTypeInfo:=true;
  31410. StartUnit(true,[supTObject]);
  31411. Add([
  31412. 'interface',
  31413. 'type',
  31414. ' {$M+}',
  31415. ' TBird = class',
  31416. ' published',
  31417. ' Swarm: array of TBird;',
  31418. ' end;',
  31419. 'implementation',
  31420. '']);
  31421. ConvertUnit;
  31422. CheckSource('TestRTTI_Class_Field_AnonymousArrayOfSelfClass',
  31423. LinesToStr([ // statements
  31424. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  31425. ' this.$init = function () {',
  31426. ' pas.system.TObject.$init.call(this);',
  31427. ' this.Swarm = [];',
  31428. ' };',
  31429. ' this.$final = function () {',
  31430. ' this.Swarm = undefined;',
  31431. ' pas.system.TObject.$final.call(this);',
  31432. ' };',
  31433. ' var $r = this.$rtti;',
  31434. ' $mod.$rtti.$DynArray("TBird.Swarm$a", {',
  31435. ' eltype: $r',
  31436. ' });',
  31437. ' $r.addField("Swarm", $mod.$rtti["TBird.Swarm$a"], 4);',
  31438. '});',
  31439. '']),
  31440. LinesToStr([ // $mod.$main
  31441. '']));
  31442. end;
  31443. procedure TTestModule.TestRTTI_IndexModifier;
  31444. begin
  31445. WithTypeInfo:=true;
  31446. StartProgram(false);
  31447. Add([
  31448. 'type',
  31449. ' TEnum = (red, blue);',
  31450. ' TObject = class',
  31451. ' FB: boolean;',
  31452. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  31453. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  31454. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  31455. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  31456. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  31457. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  31458. ' published',
  31459. ' property B1: boolean index 1 read FB write SetIntBool;',
  31460. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  31461. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  31462. ' end;',
  31463. 'begin']);
  31464. ConvertProgram;
  31465. CheckSource('TestRTTI_IndexModifier',
  31466. LinesToStr([ // statements
  31467. 'this.TEnum = {',
  31468. ' "0": "red",',
  31469. ' red: 0,',
  31470. ' "1": "blue",',
  31471. ' blue: 1',
  31472. '};',
  31473. 'this.$rtti.$Enum("TEnum", {',
  31474. ' minvalue: 0,',
  31475. ' maxvalue: 1,',
  31476. ' ordtype: 1,',
  31477. ' enumtype: this.TEnum',
  31478. '});',
  31479. 'rtl.createClass(this, "TObject", null, function () {',
  31480. ' this.$init = function () {',
  31481. ' this.FB = false;',
  31482. ' };',
  31483. ' this.$final = function () {',
  31484. ' };',
  31485. ' var $r = this.$rtti;',
  31486. ' $r.addProperty(',
  31487. ' "B1",',
  31488. ' 18,',
  31489. ' rtl.boolean,',
  31490. ' "FB",',
  31491. ' "SetIntBool",',
  31492. ' 4,',
  31493. ' {',
  31494. ' index: 1',
  31495. ' }',
  31496. ' );',
  31497. ' $r.addProperty(',
  31498. ' "B2",',
  31499. ' 17,',
  31500. ' rtl.boolean,',
  31501. ' "GetEnumBool",',
  31502. ' "FB",',
  31503. ' 4,',
  31504. ' {',
  31505. ' index: $mod.TEnum.blue',
  31506. ' }',
  31507. ' );',
  31508. ' $r.addProperty(',
  31509. ' "I1",',
  31510. ' 19,',
  31511. ' rtl.boolean,',
  31512. ' "GetStrIntBool",',
  31513. ' "SetStrIntBool",',
  31514. ' 4,',
  31515. ' {',
  31516. ' index: 2',
  31517. ' }',
  31518. ' );',
  31519. '});',
  31520. '']),
  31521. LinesToStr([ // $mod.$main
  31522. '']));
  31523. end;
  31524. procedure TTestModule.TestRTTI_StoredModifier;
  31525. begin
  31526. WithTypeInfo:=true;
  31527. StartProgram(false);
  31528. Add([
  31529. 'const',
  31530. ' ConstB = true;',
  31531. 'type',
  31532. ' TObject = class',
  31533. ' private',
  31534. ' FB: boolean;',
  31535. ' function IsBStored: boolean; virtual; abstract;',
  31536. ' published',
  31537. ' property BoolA: boolean read FB stored true;',
  31538. ' property BoolB: boolean read FB stored false;',
  31539. ' property BoolC: boolean read FB stored FB;',
  31540. ' property BoolD: boolean read FB stored ConstB;',
  31541. ' property BoolE: boolean read FB stored IsBStored;',
  31542. ' end;',
  31543. 'begin']);
  31544. ConvertProgram;
  31545. CheckSource('TestRTTI_StoredModifier',
  31546. LinesToStr([ // statements
  31547. 'this.ConstB = true;',
  31548. 'rtl.createClass(this, "TObject", null, function () {',
  31549. ' this.$init = function () {',
  31550. ' this.FB = false;',
  31551. ' };',
  31552. ' this.$final = function () {',
  31553. ' };',
  31554. ' var $r = this.$rtti;',
  31555. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  31556. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  31557. ' $r.addProperty(',
  31558. ' "BoolC",',
  31559. ' 8,',
  31560. ' rtl.boolean,',
  31561. ' "FB",',
  31562. ' "",',
  31563. ' 4,',
  31564. ' {',
  31565. ' stored: "FB"',
  31566. ' }',
  31567. ' );',
  31568. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  31569. ' $r.addProperty(',
  31570. ' "BoolE",',
  31571. ' 12,',
  31572. ' rtl.boolean,',
  31573. ' "FB",',
  31574. ' "",',
  31575. ' 4,',
  31576. ' {',
  31577. ' stored: "IsBStored"',
  31578. ' }',
  31579. ' );',
  31580. '});',
  31581. '']),
  31582. LinesToStr([ // $mod.$main
  31583. '']));
  31584. end;
  31585. procedure TTestModule.TestRTTI_DefaultValue;
  31586. begin
  31587. WithTypeInfo:=true;
  31588. StartProgram(false);
  31589. Add([
  31590. 'type',
  31591. ' TEnum = (red, blue);',
  31592. 'const',
  31593. ' CB = true or false;',
  31594. ' CI = 1+2;',
  31595. 'type',
  31596. ' TObject = class',
  31597. ' FB: boolean;',
  31598. ' FI: longint;',
  31599. ' FE: TEnum;',
  31600. ' published',
  31601. ' property B1: boolean read FB default true;',
  31602. ' property B2: boolean read FB default CB;',
  31603. ' property B3: boolean read FB default test1.cb;',
  31604. ' property I1: longint read FI default 2;',
  31605. ' property I2: longint read FI default CI;',
  31606. ' property E1: TEnum read FE default red;',
  31607. ' property E2: TEnum read FE default TEnum.blue;',
  31608. ' end;',
  31609. 'begin']);
  31610. ConvertProgram;
  31611. CheckSource('TestRTTI_DefaultValue',
  31612. LinesToStr([ // statements
  31613. 'this.TEnum = {',
  31614. ' "0": "red",',
  31615. ' red: 0,',
  31616. ' "1": "blue",',
  31617. ' blue: 1',
  31618. '};',
  31619. 'this.$rtti.$Enum("TEnum", {',
  31620. ' minvalue: 0,',
  31621. ' maxvalue: 1,',
  31622. ' ordtype: 1,',
  31623. ' enumtype: this.TEnum',
  31624. '});',
  31625. 'this.CB = true || false;',
  31626. 'this.CI = 1 + 2;',
  31627. 'rtl.createClass(this, "TObject", null, function () {',
  31628. ' this.$init = function () {',
  31629. ' this.FB = false;',
  31630. ' this.FI = 0;',
  31631. ' this.FE = 0;',
  31632. ' };',
  31633. ' this.$final = function () {',
  31634. ' };',
  31635. ' var $r = this.$rtti;',
  31636. ' $r.addProperty(',
  31637. ' "B1",',
  31638. ' 0,',
  31639. ' rtl.boolean,',
  31640. ' "FB",',
  31641. ' "",',
  31642. ' 4,',
  31643. ' {',
  31644. ' Default: true',
  31645. ' }',
  31646. ' );',
  31647. ' $r.addProperty(',
  31648. ' "B2",',
  31649. ' 0,',
  31650. ' rtl.boolean,',
  31651. ' "FB",',
  31652. ' "",',
  31653. ' 4,',
  31654. ' {',
  31655. ' Default: true',
  31656. ' }',
  31657. ' );',
  31658. ' $r.addProperty(',
  31659. ' "B3",',
  31660. ' 0,',
  31661. ' rtl.boolean,',
  31662. ' "FB",',
  31663. ' "",',
  31664. ' 4,',
  31665. ' {',
  31666. ' Default: true',
  31667. ' }',
  31668. ' );',
  31669. ' $r.addProperty(',
  31670. ' "I1",',
  31671. ' 0,',
  31672. ' rtl.longint,',
  31673. ' "FI",',
  31674. ' "",',
  31675. ' 4,',
  31676. ' {',
  31677. ' Default: 2',
  31678. ' }',
  31679. ' );',
  31680. ' $r.addProperty(',
  31681. ' "I2",',
  31682. ' 0,',
  31683. ' rtl.longint,',
  31684. ' "FI",',
  31685. ' "",',
  31686. ' 4,',
  31687. ' {',
  31688. ' Default: 3',
  31689. ' }',
  31690. ' );',
  31691. ' $r.addProperty(',
  31692. ' "E1",',
  31693. ' 0,',
  31694. ' $mod.$rtti["TEnum"],',
  31695. ' "FE",',
  31696. ' "",',
  31697. ' 4,',
  31698. ' {',
  31699. ' Default: $mod.TEnum.red',
  31700. ' }',
  31701. ' );',
  31702. ' $r.addProperty(',
  31703. ' "E2",',
  31704. ' 0,',
  31705. ' $mod.$rtti["TEnum"],',
  31706. ' "FE",',
  31707. ' "",',
  31708. ' 4,',
  31709. ' {',
  31710. ' Default: $mod.TEnum.blue',
  31711. ' }',
  31712. ' );',
  31713. '});',
  31714. '']),
  31715. LinesToStr([ // $mod.$main
  31716. '']));
  31717. end;
  31718. procedure TTestModule.TestRTTI_DefaultValueSet;
  31719. begin
  31720. WithTypeInfo:=true;
  31721. StartProgram(false);
  31722. Add([
  31723. 'type',
  31724. ' TEnum = (red, blue);',
  31725. ' TSet = set of TEnum;',
  31726. 'const',
  31727. ' CSet = [red,blue];',
  31728. 'type',
  31729. ' TObject = class',
  31730. ' FSet: TSet;',
  31731. ' published',
  31732. ' property Set1: TSet read FSet default [];',
  31733. ' property Set2: TSet read FSet default [red];',
  31734. ' property Set3: TSet read FSet default [red,blue];',
  31735. ' property Set4: TSet read FSet default CSet;',
  31736. ' end;',
  31737. 'begin']);
  31738. ConvertProgram;
  31739. CheckSource('TestRTTI_DefaultValueSet',
  31740. LinesToStr([ // statements
  31741. 'this.TEnum = {',
  31742. ' "0": "red",',
  31743. ' red: 0,',
  31744. ' "1": "blue",',
  31745. ' blue: 1',
  31746. '};',
  31747. 'this.$rtti.$Enum("TEnum", {',
  31748. ' minvalue: 0,',
  31749. ' maxvalue: 1,',
  31750. ' ordtype: 1,',
  31751. ' enumtype: this.TEnum',
  31752. '});',
  31753. 'this.$rtti.$Set("TSet", {',
  31754. ' comptype: this.$rtti["TEnum"]',
  31755. '});',
  31756. 'this.CSet = rtl.createSet(this.TEnum.red, this.TEnum.blue);',
  31757. 'rtl.createClass(this, "TObject", null, function () {',
  31758. ' this.$init = function () {',
  31759. ' this.FSet = {};',
  31760. ' };',
  31761. ' this.$final = function () {',
  31762. ' this.FSet = undefined;',
  31763. ' };',
  31764. ' var $r = this.$rtti;',
  31765. ' $r.addProperty(',
  31766. ' "Set1",',
  31767. ' 0,',
  31768. ' $mod.$rtti["TSet"],',
  31769. ' "FSet",',
  31770. ' "",',
  31771. ' 4,',
  31772. ' {',
  31773. ' Default: {}',
  31774. ' }',
  31775. ' );',
  31776. ' $r.addProperty(',
  31777. ' "Set2",',
  31778. ' 0,',
  31779. ' $mod.$rtti["TSet"],',
  31780. ' "FSet",',
  31781. ' "",',
  31782. ' 4,',
  31783. ' {',
  31784. ' Default: rtl.createSet($mod.TEnum.red)',
  31785. ' }',
  31786. ' );',
  31787. ' $r.addProperty(',
  31788. ' "Set3",',
  31789. ' 0,',
  31790. ' $mod.$rtti["TSet"],',
  31791. ' "FSet",',
  31792. ' "",',
  31793. ' 4,',
  31794. ' {',
  31795. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  31796. ' }',
  31797. ' );',
  31798. ' $r.addProperty(',
  31799. ' "Set4",',
  31800. ' 0,',
  31801. ' $mod.$rtti["TSet"],',
  31802. ' "FSet",',
  31803. ' "",',
  31804. ' 4,',
  31805. ' {',
  31806. ' Default: $mod.CSet',
  31807. ' }',
  31808. ' );',
  31809. '});',
  31810. '']),
  31811. LinesToStr([ // $mod.$main
  31812. '']));
  31813. end;
  31814. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  31815. begin
  31816. WithTypeInfo:=true;
  31817. StartProgram(false);
  31818. Add([
  31819. 'type',
  31820. ' TRg = -1..1;',
  31821. 'const',
  31822. ' l = low(TRg);',
  31823. ' h = high(TRg);',
  31824. 'type',
  31825. ' TObject = class',
  31826. ' FV: TRg;',
  31827. ' published',
  31828. ' property V1: TRg read FV default -1;',
  31829. ' end;',
  31830. 'begin']);
  31831. ConvertProgram;
  31832. CheckSource('TestRTTI_DefaultValueRangeType',
  31833. LinesToStr([ // statements
  31834. 'this.$rtti.$Int("TRg", {',
  31835. ' minvalue: -1,',
  31836. ' maxvalue: 1,',
  31837. ' ordtype: 0',
  31838. '});',
  31839. 'this.l = -1;',
  31840. 'this.h = 1;',
  31841. 'rtl.createClass(this, "TObject", null, function () {',
  31842. ' this.$init = function () {',
  31843. ' this.FV = 0;',
  31844. ' };',
  31845. ' this.$final = function () {',
  31846. ' };',
  31847. ' var $r = this.$rtti;',
  31848. ' $r.addProperty(',
  31849. ' "V1",',
  31850. ' 0,',
  31851. ' $mod.$rtti["TRg"],',
  31852. ' "FV",',
  31853. ' "",',
  31854. ' 4,',
  31855. ' {',
  31856. ' Default: -1',
  31857. ' }',
  31858. ' );',
  31859. '});',
  31860. '']),
  31861. LinesToStr([ // $mod.$main
  31862. '']));
  31863. end;
  31864. procedure TTestModule.TestRTTI_DefaultValueInherit;
  31865. begin
  31866. WithTypeInfo:=true;
  31867. StartProgram(false);
  31868. Add([
  31869. 'type',
  31870. ' TObject = class',
  31871. ' FA, FB: byte;',
  31872. ' property A: byte read FA default 1;',
  31873. ' property B: byte read FB default 2;',
  31874. ' end;',
  31875. ' TBird = class',
  31876. ' published',
  31877. ' property A;',
  31878. ' property B nodefault;',
  31879. ' end;',
  31880. 'begin']);
  31881. ConvertProgram;
  31882. CheckSource('TestRTTI_DefaultValueInherit',
  31883. LinesToStr([ // statements
  31884. 'rtl.createClass(this, "TObject", null, function () {',
  31885. ' this.$init = function () {',
  31886. ' this.FA = 0;',
  31887. ' this.FB = 0;',
  31888. ' };',
  31889. ' this.$final = function () {',
  31890. ' };',
  31891. '});',
  31892. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31893. ' var $r = this.$rtti;',
  31894. ' $r.addProperty(',
  31895. ' "A",',
  31896. ' 0,',
  31897. ' rtl.byte,',
  31898. ' "FA",',
  31899. ' "",',
  31900. ' 4,',
  31901. ' {',
  31902. ' Default: 1',
  31903. ' }',
  31904. ' );',
  31905. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  31906. '});',
  31907. '']),
  31908. LinesToStr([ // $mod.$main
  31909. '']));
  31910. end;
  31911. procedure TTestModule.TestRTTI_OverrideMethod;
  31912. begin
  31913. WithTypeInfo:=true;
  31914. StartProgram(false);
  31915. Add('type');
  31916. Add(' TObject = class');
  31917. Add(' published');
  31918. Add(' procedure DoIt; virtual; abstract;');
  31919. Add(' end;');
  31920. Add(' TSky = class');
  31921. Add(' published');
  31922. Add(' procedure DoIt; override;');
  31923. Add(' end;');
  31924. Add('procedure TSky.DoIt; begin end;');
  31925. Add('begin');
  31926. ConvertProgram;
  31927. CheckSource('TestRTTI_OverrideMethod',
  31928. LinesToStr([ // statements
  31929. 'rtl.createClass(this, "TObject", null, function () {',
  31930. ' this.$init = function () {',
  31931. ' };',
  31932. ' this.$final = function () {',
  31933. ' };',
  31934. ' var $r = this.$rtti;',
  31935. ' $r.addMethod("DoIt", 0, [], 4);',
  31936. '});',
  31937. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31938. ' this.DoIt = function () {',
  31939. ' };',
  31940. '});',
  31941. '']),
  31942. LinesToStr([ // $mod.$main
  31943. '']));
  31944. end;
  31945. procedure TTestModule.TestRTTI_ReintroduceMethod;
  31946. begin
  31947. WithTypeInfo:=true;
  31948. StartProgram(false);
  31949. Add([
  31950. 'type',
  31951. ' TObject = class',
  31952. ' published',
  31953. ' procedure DoIt;',
  31954. ' end;',
  31955. ' TSky = class',
  31956. ' published',
  31957. ' procedure DoIt; reintroduce;',
  31958. ' end;',
  31959. 'procedure TObject.DoIt; begin end;',
  31960. 'procedure TSky.DoIt;',
  31961. 'begin',
  31962. ' inherited DoIt;',
  31963. 'end;',
  31964. 'begin']);
  31965. ConvertProgram;
  31966. CheckSource('TestRTTI_ReintroduceMethod',
  31967. LinesToStr([ // statements
  31968. 'rtl.createClass(this, "TObject", null, function () {',
  31969. ' this.$init = function () {',
  31970. ' };',
  31971. ' this.$final = function () {',
  31972. ' };',
  31973. ' this.DoIt = function () {',
  31974. ' };',
  31975. ' var $r = this.$rtti;',
  31976. ' $r.addMethod("DoIt", 0, [], 4);',
  31977. '});',
  31978. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31979. ' this.DoIt = function () {',
  31980. ' $mod.TObject.DoIt.call(this);',
  31981. ' };',
  31982. ' var $r = this.$rtti;',
  31983. ' $r.addMethod("DoIt", 0, [], 4);',
  31984. '});',
  31985. '']),
  31986. LinesToStr([ // $mod.$main
  31987. '']));
  31988. end;
  31989. procedure TTestModule.TestRTTI_OverloadProperty;
  31990. begin
  31991. WithTypeInfo:=true;
  31992. StartProgram(false);
  31993. Add('type');
  31994. Add(' TObject = class');
  31995. Add(' protected');
  31996. Add(' FFlag: longint;');
  31997. Add(' published');
  31998. Add(' property Flag: longint read fflag;');
  31999. Add(' end;');
  32000. Add(' TSky = class');
  32001. Add(' published');
  32002. Add(' property FLAG: longint write fflag;');
  32003. Add(' end;');
  32004. Add('begin');
  32005. ConvertProgram;
  32006. CheckSource('TestRTTI_OverrideMethod',
  32007. LinesToStr([ // statements
  32008. 'rtl.createClass(this, "TObject", null, function () {',
  32009. ' this.$init = function () {',
  32010. ' this.FFlag = 0;',
  32011. ' };',
  32012. ' this.$final = function () {',
  32013. ' };',
  32014. ' var $r = this.$rtti;',
  32015. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  32016. '});',
  32017. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  32018. ' var $r = this.$rtti;',
  32019. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  32020. '});',
  32021. '']),
  32022. LinesToStr([ // $mod.$main
  32023. '']));
  32024. end;
  32025. procedure TTestModule.TestRTTI_ClassForward;
  32026. begin
  32027. WithTypeInfo:=true;
  32028. StartProgram(false);
  32029. Add('type');
  32030. Add(' TObject = class end;');
  32031. Add(' tbridge = class;');
  32032. Add(' TProc = function: tbridge;');
  32033. Add(' TOger = class');
  32034. Add(' published');
  32035. Add(' FBridge: tbridge;');
  32036. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  32037. Add(' property Bridge: tbridge read fbridge write setbridge;');
  32038. Add(' end;');
  32039. Add(' TBridge = class');
  32040. Add(' FOger: toger;');
  32041. Add(' end;');
  32042. Add('var p: Pointer;');
  32043. Add(' b: tbridge;');
  32044. Add('begin');
  32045. Add(' p:=typeinfo(tbridge);');
  32046. Add(' p:=typeinfo(b);');
  32047. ConvertProgram;
  32048. CheckSource('TestRTTI_ClassForward',
  32049. LinesToStr([ // statements
  32050. 'rtl.createClass(this, "TObject", null, function () {',
  32051. ' this.$init = function () {',
  32052. ' };',
  32053. ' this.$final = function () {',
  32054. ' };',
  32055. '});',
  32056. 'this.$rtti.$Class("TBridge");',
  32057. 'this.$rtti.$ProcVar("TProc", {',
  32058. ' procsig: rtl.newTIProcSig([], this.$rtti["TBridge"])',
  32059. '});',
  32060. 'rtl.createClass(this, "TOger", this.TObject, function () {',
  32061. ' this.$init = function () {',
  32062. ' $mod.TObject.$init.call(this);',
  32063. ' this.FBridge = null;',
  32064. ' };',
  32065. ' this.$final = function () {',
  32066. ' this.FBridge = undefined;',
  32067. ' $mod.TObject.$final.call(this);',
  32068. ' };',
  32069. ' var $r = this.$rtti;',
  32070. ' $r.addField("FBridge", $mod.$rtti["TBridge"], 4);',
  32071. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]], 4);',
  32072. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  32073. '});',
  32074. 'rtl.createClass(this, "TBridge", this.TObject, function () {',
  32075. ' this.$init = function () {',
  32076. ' $mod.TObject.$init.call(this);',
  32077. ' this.FOger = null;',
  32078. ' };',
  32079. ' this.$final = function () {',
  32080. ' this.FOger = undefined;',
  32081. ' $mod.TObject.$final.call(this);',
  32082. ' };',
  32083. '});',
  32084. 'this.p = null;',
  32085. 'this.b = null;',
  32086. '']),
  32087. LinesToStr([ // $mod.$main
  32088. '$mod.p = $mod.$rtti["TBridge"];',
  32089. '$mod.p = $mod.b.$rtti;',
  32090. '']));
  32091. end;
  32092. procedure TTestModule.TestRTTI_ClassOf;
  32093. begin
  32094. WithTypeInfo:=true;
  32095. StartProgram(false);
  32096. Add('type');
  32097. Add(' TClass = class of tobject;');
  32098. Add(' TProcA = function: TClass;');
  32099. Add(' TObject = class');
  32100. Add(' published');
  32101. Add(' C: tclass;');
  32102. Add(' end;');
  32103. Add(' tfox = class;');
  32104. Add(' TBird = class end;');
  32105. Add(' TBirds = class of tbird;');
  32106. Add(' TFox = class end;');
  32107. Add(' TFoxes = class of tfox;');
  32108. Add(' TCows = class of TCow;');
  32109. Add(' TCow = class;');
  32110. Add(' TCow = class end;');
  32111. Add('begin');
  32112. ConvertProgram;
  32113. CheckSource('TestRTTI_ClassOf',
  32114. LinesToStr([ // statements
  32115. 'this.$rtti.$Class("TObject");',
  32116. 'this.$rtti.$ClassRef("TClass", {',
  32117. ' instancetype: this.$rtti["TObject"]',
  32118. '});',
  32119. 'this.$rtti.$ProcVar("TProcA", {',
  32120. ' procsig: rtl.newTIProcSig([], this.$rtti["TClass"])',
  32121. '});',
  32122. 'rtl.createClass(this, "TObject", null, function () {',
  32123. ' this.$init = function () {',
  32124. ' this.C = null;',
  32125. ' };',
  32126. ' this.$final = function () {',
  32127. ' this.C = undefined;',
  32128. ' };',
  32129. ' var $r = this.$rtti;',
  32130. ' $r.addField("C", $mod.$rtti["TClass"], 4);',
  32131. '});',
  32132. 'this.$rtti.$Class("TFox");',
  32133. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32134. '});',
  32135. 'this.$rtti.$ClassRef("TBirds", {',
  32136. ' instancetype: this.$rtti["TBird"]',
  32137. '});',
  32138. 'rtl.createClass(this, "TFox", this.TObject, function () {',
  32139. '});',
  32140. 'this.$rtti.$ClassRef("TFoxes", {',
  32141. ' instancetype: this.$rtti["TFox"]',
  32142. '});',
  32143. 'this.$rtti.$Class("TCow");',
  32144. 'this.$rtti.$ClassRef("TCows", {',
  32145. ' instancetype: this.$rtti["TCow"]',
  32146. '});',
  32147. 'rtl.createClass(this, "TCow", this.TObject, function () {',
  32148. '});',
  32149. '']),
  32150. LinesToStr([ // $mod.$main
  32151. '']));
  32152. end;
  32153. procedure TTestModule.TestRTTI_Record;
  32154. begin
  32155. WithTypeInfo:=true;
  32156. StartProgram(false);
  32157. Add('type');
  32158. Add(' integer = longint;');
  32159. Add(' TPoint = record');
  32160. Add(' x,y: integer;');
  32161. Add(' end;');
  32162. Add('var p: pointer;');
  32163. Add(' r: tpoint;');
  32164. Add('begin');
  32165. Add(' p:=typeinfo(tpoint);');
  32166. Add(' p:=typeinfo(r);');
  32167. Add(' p:=typeinfo(r.x);');
  32168. ConvertProgram;
  32169. CheckSource('TestRTTI_Record',
  32170. LinesToStr([ // statements
  32171. 'rtl.recNewT(this, "TPoint", function () {',
  32172. ' this.x = 0;',
  32173. ' this.y = 0;',
  32174. ' this.$eq = function (b) {',
  32175. ' return (this.x === b.x) && (this.y === b.y);',
  32176. ' };',
  32177. ' this.$assign = function (s) {',
  32178. ' this.x = s.x;',
  32179. ' this.y = s.y;',
  32180. ' return this;',
  32181. ' };',
  32182. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  32183. ' $r.addField("x", rtl.longint);',
  32184. ' $r.addField("y", rtl.longint);',
  32185. '});',
  32186. 'this.p = null;',
  32187. 'this.r = this.TPoint.$new();',
  32188. '']),
  32189. LinesToStr([ // $mod.$main
  32190. '$mod.p = $mod.$rtti["TPoint"];',
  32191. '$mod.p = $mod.$rtti["TPoint"];',
  32192. '$mod.p = rtl.longint;',
  32193. '']));
  32194. end;
  32195. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  32196. begin
  32197. WithTypeInfo:=true;
  32198. StartProgram(false);
  32199. Add('type');
  32200. Add(' TFloatRec = record');
  32201. Add(' c,d: array of char;');
  32202. // Add(' i: array of array of longint;');
  32203. Add(' end;');
  32204. Add('var p: pointer;');
  32205. Add(' r: tfloatrec;');
  32206. Add('begin');
  32207. Add(' p:=typeinfo(tfloatrec);');
  32208. Add(' p:=typeinfo(r);');
  32209. Add(' p:=typeinfo(r.d);');
  32210. ConvertProgram;
  32211. CheckSource('TestRTTI_Record',
  32212. LinesToStr([ // statements
  32213. 'rtl.recNewT(this, "TFloatRec", function () {',
  32214. ' this.$new = function () {',
  32215. ' var r = Object.create(this);',
  32216. ' r.c = [];',
  32217. ' r.d = [];',
  32218. ' return r;',
  32219. ' };',
  32220. ' this.$eq = function (b) {',
  32221. ' return (this.c === b.c) && (this.d === b.d);',
  32222. ' };',
  32223. ' this.$assign = function (s) {',
  32224. ' this.c = rtl.arrayRef(s.c);',
  32225. ' this.d = rtl.arrayRef(s.d);',
  32226. ' return this;',
  32227. ' };',
  32228. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  32229. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  32230. ' eltype: rtl.char',
  32231. ' });',
  32232. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  32233. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  32234. '});',
  32235. 'this.p = null;',
  32236. 'this.r = this.TFloatRec.$new();',
  32237. '']),
  32238. LinesToStr([ // $mod.$main
  32239. '$mod.p = $mod.$rtti["TFloatRec"];',
  32240. '$mod.p = $mod.$rtti["TFloatRec"];',
  32241. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  32242. '']));
  32243. end;
  32244. procedure TTestModule.TestRTTI_Record_ClassVarType;
  32245. begin
  32246. WithTypeInfo:=true;
  32247. StartProgram(false);
  32248. Add([
  32249. '{$modeswitch AdvancedRecords}',
  32250. 'type',
  32251. ' TPoint = record',
  32252. ' type TProc = procedure(w: word);',
  32253. ' class var p: TProc;',
  32254. ' end;',
  32255. 'begin',
  32256. '']);
  32257. ConvertProgram;
  32258. CheckSource('TestRTTI_Record_ClassVarType',
  32259. LinesToStr([ // statements
  32260. 'rtl.recNewT(this, "TPoint", function () {',
  32261. ' $mod.$rtti.$ProcVar("TPoint.TProc", {',
  32262. ' procsig: rtl.newTIProcSig([["w", rtl.word]])',
  32263. ' });',
  32264. ' this.p = null;',
  32265. ' this.$eq = function (b) {',
  32266. ' return true;',
  32267. ' };',
  32268. ' this.$assign = function (s) {',
  32269. ' return this;',
  32270. ' };',
  32271. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  32272. ' $r.addField("p", $mod.$rtti["TPoint.TProc"]);',
  32273. '}, true);',
  32274. '']),
  32275. LinesToStr([ // $mod.$main
  32276. '']));
  32277. end;
  32278. procedure TTestModule.TestRTTI_LocalTypes;
  32279. begin
  32280. WithTypeInfo:=true;
  32281. StartProgram(false);
  32282. Add([
  32283. 'procedure DoIt;',
  32284. 'type',
  32285. ' integer = longint;',
  32286. ' TPoint = record',
  32287. ' x,y: integer;',
  32288. ' end;',
  32289. 'var p: TPoint;',
  32290. 'begin',
  32291. 'end;',
  32292. 'begin']);
  32293. ConvertProgram;
  32294. CheckSource('TestRTTI_LocalTypes',
  32295. LinesToStr([ // statements
  32296. 'var TPoint = rtl.recNewT(null, "", function () {',
  32297. ' this.x = 0;',
  32298. ' this.y = 0;',
  32299. ' this.$eq = function (b) {',
  32300. ' return (this.x === b.x) && (this.y === b.y);',
  32301. ' };',
  32302. ' this.$assign = function (s) {',
  32303. ' this.x = s.x;',
  32304. ' this.y = s.y;',
  32305. ' return this;',
  32306. ' };',
  32307. '});',
  32308. 'this.DoIt = function () {',
  32309. ' var p = TPoint.$new();',
  32310. '};',
  32311. '']),
  32312. LinesToStr([ // $mod.$main
  32313. '']));
  32314. end;
  32315. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  32316. begin
  32317. WithTypeInfo:=true;
  32318. StartProgram(false);
  32319. Add([
  32320. 'type',
  32321. ' TCaption = string;',
  32322. ' TYesNo = boolean;',
  32323. ' TLetter = char;',
  32324. ' TFloat = double;',
  32325. ' TPtr = pointer;',
  32326. ' TShortInt = shortint;',
  32327. ' TByte = byte;',
  32328. ' TSmallInt = smallint;',
  32329. ' TWord = word;',
  32330. ' TInt32 = longint;',
  32331. ' TDWord = longword;',
  32332. ' TValue = jsvalue;',
  32333. 'var p: TPtr;',
  32334. 'begin',
  32335. ' p:=typeinfo(string);',
  32336. ' p:=typeinfo(tcaption);',
  32337. ' p:=typeinfo(boolean);',
  32338. ' p:=typeinfo(tyesno);',
  32339. ' p:=typeinfo(char);',
  32340. ' p:=typeinfo(tletter);',
  32341. ' p:=typeinfo(double);',
  32342. ' p:=typeinfo(tfloat);',
  32343. ' p:=typeinfo(pointer);',
  32344. ' p:=typeinfo(tptr);',
  32345. ' p:=typeinfo(shortint);',
  32346. ' p:=typeinfo(tshortint);',
  32347. ' p:=typeinfo(byte);',
  32348. ' p:=typeinfo(tbyte);',
  32349. ' p:=typeinfo(smallint);',
  32350. ' p:=typeinfo(tsmallint);',
  32351. ' p:=typeinfo(word);',
  32352. ' p:=typeinfo(tword);',
  32353. ' p:=typeinfo(longword);',
  32354. ' p:=typeinfo(tdword);',
  32355. ' p:=typeinfo(jsvalue);',
  32356. ' p:=typeinfo(tvalue);',
  32357. '']);
  32358. ConvertProgram;
  32359. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  32360. LinesToStr([ // statements
  32361. 'this.p = null;',
  32362. '']),
  32363. LinesToStr([ // $mod.$main
  32364. '$mod.p = rtl.string;',
  32365. '$mod.p = rtl.string;',
  32366. '$mod.p = rtl.boolean;',
  32367. '$mod.p = rtl.boolean;',
  32368. '$mod.p = rtl.char;',
  32369. '$mod.p = rtl.char;',
  32370. '$mod.p = rtl.double;',
  32371. '$mod.p = rtl.double;',
  32372. '$mod.p = rtl.pointer;',
  32373. '$mod.p = rtl.pointer;',
  32374. '$mod.p = rtl.shortint;',
  32375. '$mod.p = rtl.shortint;',
  32376. '$mod.p = rtl.byte;',
  32377. '$mod.p = rtl.byte;',
  32378. '$mod.p = rtl.smallint;',
  32379. '$mod.p = rtl.smallint;',
  32380. '$mod.p = rtl.word;',
  32381. '$mod.p = rtl.word;',
  32382. '$mod.p = rtl.longword;',
  32383. '$mod.p = rtl.longword;',
  32384. '$mod.p = rtl.jsvalue;',
  32385. '$mod.p = rtl.jsvalue;',
  32386. '']));
  32387. end;
  32388. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  32389. begin
  32390. WithTypeInfo:=true;
  32391. StartProgram(false);
  32392. Add([
  32393. 'type',
  32394. ' TCaption = type string;',
  32395. ' TYesNo = type boolean;',
  32396. ' TLetter = type char;',
  32397. ' TFloat = type double;',
  32398. ' TPtr = type pointer;',
  32399. ' TShortInt = type shortint;',
  32400. ' TByte = type byte;',
  32401. ' TSmallInt = type smallint;',
  32402. ' TWord = type word;',
  32403. ' TInt32 = type longint;',
  32404. ' TDWord = type longword;',
  32405. ' TValue = type jsvalue;',
  32406. ' TAliasValue = type TValue;',
  32407. 'var',
  32408. ' p: TPtr;',
  32409. ' a: TAliasValue;',
  32410. 'begin',
  32411. ' p:=typeinfo(tcaption);',
  32412. ' p:=typeinfo(tyesno);',
  32413. ' p:=typeinfo(tletter);',
  32414. ' p:=typeinfo(tfloat);',
  32415. ' p:=typeinfo(tptr);',
  32416. ' p:=typeinfo(tshortint);',
  32417. ' p:=typeinfo(tbyte);',
  32418. ' p:=typeinfo(tsmallint);',
  32419. ' p:=typeinfo(tword);',
  32420. ' p:=typeinfo(tdword);',
  32421. ' p:=typeinfo(tvalue);',
  32422. ' p:=typeinfo(taliasvalue);',
  32423. ' p:=typeinfo(a);',
  32424. '']);
  32425. ConvertProgram;
  32426. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  32427. LinesToStr([ // statements
  32428. 'this.$rtti.$inherited("TCaption", rtl.string, {});',
  32429. 'this.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  32430. 'this.$rtti.$inherited("TLetter", rtl.char, {});',
  32431. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  32432. 'this.$rtti.$inherited("TPtr", rtl.pointer, {});',
  32433. 'this.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  32434. 'this.$rtti.$inherited("TByte", rtl.byte, {});',
  32435. 'this.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  32436. 'this.$rtti.$inherited("TWord", rtl.word, {});',
  32437. 'this.$rtti.$inherited("TInt32", rtl.longint, {});',
  32438. 'this.$rtti.$inherited("TDWord", rtl.longword, {});',
  32439. 'this.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  32440. 'this.$rtti.$inherited("TAliasValue", this.$rtti["TValue"], {});',
  32441. 'this.p = null;',
  32442. 'this.a = undefined;',
  32443. '']),
  32444. LinesToStr([ // $mod.$main
  32445. '$mod.p = $mod.$rtti["TCaption"];',
  32446. '$mod.p = $mod.$rtti["TYesNo"];',
  32447. '$mod.p = $mod.$rtti["TLetter"];',
  32448. '$mod.p = $mod.$rtti["TFloat"];',
  32449. '$mod.p = $mod.$rtti["TPtr"];',
  32450. '$mod.p = $mod.$rtti["TShortInt"];',
  32451. '$mod.p = $mod.$rtti["TByte"];',
  32452. '$mod.p = $mod.$rtti["TSmallInt"];',
  32453. '$mod.p = $mod.$rtti["TWord"];',
  32454. '$mod.p = $mod.$rtti["TDWord"];',
  32455. '$mod.p = $mod.$rtti["TValue"];',
  32456. '$mod.p = $mod.$rtti["TAliasValue"];',
  32457. '$mod.p = $mod.$rtti["TAliasValue"];',
  32458. '']));
  32459. end;
  32460. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  32461. begin
  32462. WithTypeInfo:=true;
  32463. StartProgram(false);
  32464. Add('procedure DoIt;');
  32465. Add('type');
  32466. Add(' integer = longint;');
  32467. Add(' TPoint = record');
  32468. Add(' x,y: integer;');
  32469. Add(' end;');
  32470. Add('var p: pointer;');
  32471. Add('begin');
  32472. Add(' p:=typeinfo(tpoint);');
  32473. Add('end;');
  32474. Add('begin');
  32475. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  32476. ConvertProgram;
  32477. end;
  32478. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  32479. begin
  32480. WithTypeInfo:=true;
  32481. StartProgram(true,[supTypeInfo]);
  32482. Add([
  32483. '{$modeswitch externalclass}',
  32484. 'type',
  32485. ' TFlag = (up,down);',
  32486. ' TFlags = set of TFlag;',
  32487. 'var',
  32488. ' ti: TTypeInfo;',
  32489. ' tiInt: TTypeInfoInteger;',
  32490. ' tiEnum: TTypeInfoEnum;',
  32491. ' tiSet: TTypeInfoSet;',
  32492. 'begin',
  32493. ' ti:=typeinfo(string);',
  32494. ' ti:=typeinfo(boolean);',
  32495. ' ti:=typeinfo(char);',
  32496. ' ti:=typeinfo(double);',
  32497. ' tiInt:=typeinfo(shortint);',
  32498. ' tiInt:=typeinfo(byte);',
  32499. ' tiInt:=typeinfo(smallint);',
  32500. ' tiInt:=typeinfo(word);',
  32501. ' tiInt:=typeinfo(longint);',
  32502. ' tiInt:=typeinfo(longword);',
  32503. ' ti:=typeinfo(jsvalue);',
  32504. ' tiEnum:=typeinfo(tflag);',
  32505. ' tiSet:=typeinfo(tflags);']);
  32506. ConvertProgram;
  32507. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  32508. LinesToStr([ // statements
  32509. 'this.TFlag = {',
  32510. ' "0": "up",',
  32511. ' up: 0,',
  32512. ' "1": "down",',
  32513. ' down: 1',
  32514. '};',
  32515. 'this.$rtti.$Enum("TFlag", {',
  32516. ' minvalue: 0,',
  32517. ' maxvalue: 1,',
  32518. ' ordtype: 1,',
  32519. ' enumtype: this.TFlag',
  32520. '});',
  32521. 'this.$rtti.$Set("TFlags", {',
  32522. ' comptype: this.$rtti["TFlag"]',
  32523. '});',
  32524. 'this.ti = null;',
  32525. 'this.tiInt = null;',
  32526. 'this.tiEnum = null;',
  32527. 'this.tiSet = null;',
  32528. '']),
  32529. LinesToStr([ // $mod.$main
  32530. '$mod.ti = rtl.string;',
  32531. '$mod.ti = rtl.boolean;',
  32532. '$mod.ti = rtl.char;',
  32533. '$mod.ti = rtl.double;',
  32534. '$mod.tiInt = rtl.shortint;',
  32535. '$mod.tiInt = rtl.byte;',
  32536. '$mod.tiInt = rtl.smallint;',
  32537. '$mod.tiInt = rtl.word;',
  32538. '$mod.tiInt = rtl.longint;',
  32539. '$mod.tiInt = rtl.longword;',
  32540. '$mod.ti = rtl.jsvalue;',
  32541. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  32542. '$mod.tiSet = $mod.$rtti["TFlags"];',
  32543. '']));
  32544. end;
  32545. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  32546. begin
  32547. WithTypeInfo:=true;
  32548. StartProgram(true,[supTypeInfo]);
  32549. Add('{$modeswitch externalclass}');
  32550. Add('type');
  32551. Add(' TStaticArr = array[boolean] of string;');
  32552. Add(' TDynArr = array of string;');
  32553. Add(' TProc = procedure;');
  32554. Add(' TMethod = procedure of object;');
  32555. Add('var');
  32556. Add(' StaticArray: TStaticArr;');
  32557. Add(' tiStaticArray: TTypeInfoStaticArray;');
  32558. Add(' DynArray: TDynArr;');
  32559. Add(' tiDynArray: TTypeInfoDynArray;');
  32560. Add(' ProcVar: TProc;');
  32561. Add(' tiProcVar: TTypeInfoProcVar;');
  32562. Add(' MethodVar: TMethod;');
  32563. Add(' tiMethodVar: TTypeInfoMethodVar;');
  32564. Add('begin');
  32565. Add(' tiStaticArray:=typeinfo(StaticArray);');
  32566. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  32567. Add(' tiDynArray:=typeinfo(DynArray);');
  32568. Add(' tiDynArray:=typeinfo(TDynArr);');
  32569. Add(' tiProcVar:=typeinfo(ProcVar);');
  32570. Add(' tiProcVar:=typeinfo(TProc);');
  32571. Add(' tiMethodVar:=typeinfo(MethodVar);');
  32572. Add(' tiMethodVar:=typeinfo(TMethod);');
  32573. ConvertProgram;
  32574. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  32575. LinesToStr([ // statements
  32576. 'this.$rtti.$StaticArray("TStaticArr", {',
  32577. ' dims: [2],',
  32578. ' eltype: rtl.string',
  32579. '});',
  32580. 'this.$rtti.$DynArray("TDynArr", {',
  32581. ' eltype: rtl.string',
  32582. '});',
  32583. 'this.$rtti.$ProcVar("TProc", {',
  32584. ' procsig: rtl.newTIProcSig([])',
  32585. '});',
  32586. 'this.$rtti.$MethodVar("TMethod", {',
  32587. ' procsig: rtl.newTIProcSig([]),',
  32588. ' methodkind: 0',
  32589. '});',
  32590. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  32591. 'this.tiStaticArray = null;',
  32592. 'this.DynArray = [];',
  32593. 'this.tiDynArray = null;',
  32594. 'this.ProcVar = null;',
  32595. 'this.tiProcVar = null;',
  32596. 'this.MethodVar = null;',
  32597. 'this.tiMethodVar = null;',
  32598. '']),
  32599. LinesToStr([ // $mod.$main
  32600. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  32601. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  32602. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  32603. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  32604. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  32605. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  32606. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  32607. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  32608. '']));
  32609. end;
  32610. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  32611. begin
  32612. WithTypeInfo:=true;
  32613. StartProgram(true,[supTypeInfo]);
  32614. Add('{$modeswitch externalclass}');
  32615. Add('type');
  32616. Add(' TRec = record end;');
  32617. // ToDo: ^TRec
  32618. Add(' TObject = class end;');
  32619. Add(' TClass = class of tobject;');
  32620. Add('var');
  32621. Add(' Rec: trec;');
  32622. Add(' tiRecord: ttypeinforecord;');
  32623. Add(' Obj: tobject;');
  32624. Add(' tiClass: ttypeinfoclass;');
  32625. Add(' aClass: tclass;');
  32626. Add(' tiClassRef: ttypeinfoclassref;');
  32627. // ToDo: ^TRec
  32628. Add(' tiPointer: ttypeinfopointer;');
  32629. Add('begin');
  32630. Add(' tirecord:=typeinfo(trec);');
  32631. Add(' tirecord:=typeinfo(trec);');
  32632. Add(' ticlass:=typeinfo(obj);');
  32633. Add(' ticlass:=typeinfo(tobject);');
  32634. Add(' ticlass:=typeinfo(aclass);');
  32635. Add(' ticlassref:=typeinfo(tclass);');
  32636. ConvertProgram;
  32637. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  32638. LinesToStr([ // statements
  32639. 'rtl.recNewT(this, "TRec", function () {',
  32640. ' this.$eq = function (b) {',
  32641. ' return true;',
  32642. ' };',
  32643. ' this.$assign = function (s) {',
  32644. ' return this;',
  32645. ' };',
  32646. ' $mod.$rtti.$Record("TRec", {});',
  32647. '});',
  32648. 'rtl.createClass(this, "TObject", null, function () {',
  32649. ' this.$init = function () {',
  32650. ' };',
  32651. ' this.$final = function () {',
  32652. ' };',
  32653. '});',
  32654. 'this.$rtti.$ClassRef("TClass", {',
  32655. ' instancetype: this.$rtti["TObject"]',
  32656. '});',
  32657. 'this.Rec = this.TRec.$new();',
  32658. 'this.tiRecord = null;',
  32659. 'this.Obj = null;',
  32660. 'this.tiClass = null;',
  32661. 'this.aClass = null;',
  32662. 'this.tiClassRef = null;',
  32663. 'this.tiPointer = null;',
  32664. '']),
  32665. LinesToStr([ // $mod.$main
  32666. '$mod.tiRecord = $mod.$rtti["TRec"];',
  32667. '$mod.tiRecord = $mod.$rtti["TRec"];',
  32668. '$mod.tiClass = $mod.Obj.$rtti;',
  32669. '$mod.tiClass = $mod.$rtti["TObject"];',
  32670. '$mod.tiClass = $mod.aClass.$rtti;',
  32671. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  32672. '']));
  32673. end;
  32674. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  32675. begin
  32676. WithTypeInfo:=true;
  32677. StartProgram(true,[supTypeInfo]);
  32678. Add([
  32679. '{$modeswitch externalclass}',
  32680. 'type',
  32681. ' TClass = class of tobject;',
  32682. ' TObject = class',
  32683. ' function MyClass: TClass;',
  32684. ' class function ClassType: TClass;',
  32685. ' end;',
  32686. 'function TObject.MyClass: TClass;',
  32687. 'var t: TTypeInfoClass;',
  32688. 'begin',
  32689. ' t:=TypeInfo(Self);',
  32690. ' t:=TypeInfo(Result);',
  32691. ' t:=TypeInfo(TObject);',
  32692. 'end;',
  32693. 'class function TObject.ClassType: TClass;',
  32694. 'var t: TTypeInfoClass;',
  32695. 'begin',
  32696. ' t:=TypeInfo(Self);',
  32697. ' t:=TypeInfo(Result);',
  32698. 'end;',
  32699. 'var',
  32700. ' Obj: TObject;',
  32701. ' t: TTypeInfoClass;',
  32702. 'begin',
  32703. ' t:=TypeInfo(TObject.ClassType);',
  32704. ' t:=TypeInfo(Obj.ClassType);',
  32705. ' t:=TypeInfo(Obj.MyClass);',
  32706. '']);
  32707. ConvertProgram;
  32708. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  32709. LinesToStr([ // statements
  32710. 'this.$rtti.$Class("TObject");',
  32711. 'this.$rtti.$ClassRef("TClass", {',
  32712. ' instancetype: this.$rtti["TObject"]',
  32713. '});',
  32714. 'rtl.createClass(this, "TObject", null, function () {',
  32715. ' this.$init = function () {',
  32716. ' };',
  32717. ' this.$final = function () {',
  32718. ' };',
  32719. ' this.MyClass = function () {',
  32720. ' var Result = null;',
  32721. ' var t = null;',
  32722. ' t = this.$rtti;',
  32723. ' t = Result.$rtti;',
  32724. ' t = $mod.$rtti["TObject"];',
  32725. ' return Result;',
  32726. ' };',
  32727. ' this.ClassType = function () {',
  32728. ' var Result = null;',
  32729. ' var t = null;',
  32730. ' t = this.$rtti;',
  32731. ' t = Result.$rtti;',
  32732. ' return Result;',
  32733. ' };',
  32734. '});',
  32735. 'this.Obj = null;',
  32736. 'this.t = null;',
  32737. '']),
  32738. LinesToStr([ // $mod.$main
  32739. '$mod.t = $mod.TObject.ClassType().$rtti;',
  32740. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  32741. '$mod.t = $mod.Obj.MyClass().$rtti;',
  32742. '']));
  32743. end;
  32744. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  32745. begin
  32746. WithTypeInfo:=true;
  32747. AddModuleWithIntfImplSrc('typinfo.pas',
  32748. LinesToStr([
  32749. '{$modeswitch externalclass}',
  32750. 'type',
  32751. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  32752. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  32753. '']),
  32754. '');
  32755. AddModuleWithIntfImplSrc('unit2.pas',
  32756. LinesToStr([
  32757. 'uses typinfo;',
  32758. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  32759. 'procedure DoPtr(p: PTypeInfo);',
  32760. 'procedure DoInfo(t: TTypeInfo);',
  32761. 'procedure DoInt(t: TTypeInfoInteger);',
  32762. '']),
  32763. LinesToStr([
  32764. 'procedure DoPtr(p: PTypeInfo);',
  32765. 'begin end;',
  32766. 'procedure DoInfo(t: TTypeInfo);',
  32767. 'begin end;',
  32768. 'procedure DoInt(t: TTypeInfoInteger);',
  32769. 'begin end;',
  32770. '']));
  32771. StartUnit(true);
  32772. Add([
  32773. 'interface',
  32774. 'uses unit2;', // does not use unit typinfo
  32775. 'implementation',
  32776. 'var',
  32777. ' i: byte;',
  32778. ' p: pointer;',
  32779. ' t: PTypeInfo;',
  32780. 'initialization',
  32781. ' p:=typeinfo(i);',
  32782. ' t:=typeinfo(i);',
  32783. ' if p=t then ;',
  32784. ' if p=typeinfo(i) then ;',
  32785. ' if typeinfo(i)=p then ;',
  32786. ' if t=typeinfo(i) then ;',
  32787. ' if typeinfo(i)=t then ;',
  32788. ' DoPtr(p);',
  32789. ' DoPtr(t);',
  32790. ' DoPtr(typeinfo(i));',
  32791. ' DoInfo(p);',
  32792. ' DoInfo(t);',
  32793. ' DoInfo(typeinfo(i));',
  32794. ' DoInt(typeinfo(i));',
  32795. '']);
  32796. ConvertUnit;
  32797. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  32798. LinesToStr([ // statements
  32799. 'var $impl = $mod.$impl;',
  32800. '']),
  32801. LinesToStr([ // this.$init
  32802. '$impl.p = rtl.byte;',
  32803. '$impl.t = rtl.byte;',
  32804. 'if ($impl.p === $impl.t) ;',
  32805. 'if ($impl.p === rtl.byte) ;',
  32806. 'if (rtl.byte === $impl.p) ;',
  32807. 'if ($impl.t === rtl.byte) ;',
  32808. 'if (rtl.byte === $impl.t) ;',
  32809. 'pas.unit2.DoPtr($impl.p);',
  32810. 'pas.unit2.DoPtr($impl.t);',
  32811. 'pas.unit2.DoPtr(rtl.byte);',
  32812. 'pas.unit2.DoInfo($impl.p);',
  32813. 'pas.unit2.DoInfo($impl.t);',
  32814. 'pas.unit2.DoInfo(rtl.byte);',
  32815. 'pas.unit2.DoInt(rtl.byte);',
  32816. '']),
  32817. LinesToStr([ // implementation
  32818. '$impl.i = 0;',
  32819. '$impl.p = null;',
  32820. '$impl.t = null;',
  32821. '']) );
  32822. end;
  32823. procedure TTestModule.TestRTTI_Interface_Corba;
  32824. begin
  32825. WithTypeInfo:=true;
  32826. StartProgram(true,[supTypeInfo]);
  32827. Add([
  32828. '{$interfaces corba}',
  32829. '{$modeswitch externalclass}',
  32830. 'type',
  32831. ' IUnknown = interface',
  32832. ' end;',
  32833. ' IBird = interface',
  32834. ' function GetItem: longint;',
  32835. ' procedure SetItem(Value: longint);',
  32836. ' property Item: longint read GetItem write SetItem;',
  32837. ' end;',
  32838. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  32839. 'var',
  32840. ' i: IBird;',
  32841. ' t: TTypeInfoInterface;',
  32842. 'begin',
  32843. ' t:=TypeInfo(IBird);',
  32844. ' t:=TypeInfo(i);',
  32845. ' DoIt(t);',
  32846. ' DoIt(TypeInfo(IBird));',
  32847. '']);
  32848. ConvertProgram;
  32849. CheckSource('TestRTTI_Interface_Corba',
  32850. LinesToStr([ // statements
  32851. 'rtl.createInterface(',
  32852. ' this,',
  32853. ' "IUnknown",',
  32854. ' "{B92D5841-758A-322B-B800-000000000000}",',
  32855. ' [],',
  32856. ' null,',
  32857. ' function () {',
  32858. ' }',
  32859. ');',
  32860. 'rtl.createInterface(',
  32861. ' this,',
  32862. ' "IBird",',
  32863. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  32864. ' ["GetItem", "SetItem"],',
  32865. ' null,',
  32866. ' function () {',
  32867. ' var $r = this.$rtti;',
  32868. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32869. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  32870. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem", 2);',
  32871. ' }',
  32872. ');',
  32873. 'this.DoIt = function (t) {',
  32874. '}; ',
  32875. 'this.i = null;',
  32876. 'this.t = null;',
  32877. '']),
  32878. LinesToStr([ // $mod.$main
  32879. '$mod.t = $mod.$rtti["IBird"];',
  32880. '$mod.t = $mod.i.$rtti;',
  32881. '$mod.DoIt($mod.t);',
  32882. '$mod.DoIt($mod.$rtti["IBird"]);',
  32883. '']));
  32884. end;
  32885. procedure TTestModule.TestRTTI_Interface_COM;
  32886. begin
  32887. WithTypeInfo:=true;
  32888. StartProgram(true,[supTypeInfo]);
  32889. Add([
  32890. '{$interfaces com}',
  32891. '{$modeswitch externalclass}',
  32892. 'type',
  32893. ' TGuid = record end;',
  32894. ' integer = longint;',
  32895. ' IUnknown = interface',
  32896. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  32897. ' function _AddRef: Integer;',
  32898. ' function _Release: Integer;',
  32899. ' end;',
  32900. ' IBird = interface',
  32901. ' function GetItem: longint;',
  32902. ' procedure SetItem(Value: longint);',
  32903. ' property Item: longint read GetItem write SetItem;',
  32904. ' end;',
  32905. 'var',
  32906. ' i: IBird;',
  32907. ' t: TTypeInfoInterface;',
  32908. 'begin',
  32909. ' t:=TypeInfo(IBird);',
  32910. ' t:=TypeInfo(i);',
  32911. '']);
  32912. ConvertProgram;
  32913. CheckSource('TestRTTI_Interface_COM',
  32914. LinesToStr([ // statements
  32915. 'rtl.recNewT(this, "TGuid", function () {',
  32916. ' this.$eq = function (b) {',
  32917. ' return true;',
  32918. ' };',
  32919. ' this.$assign = function (s) {',
  32920. ' return this;',
  32921. ' };',
  32922. ' $mod.$rtti.$Record("TGuid", {});',
  32923. '});',
  32924. 'rtl.createInterface(',
  32925. ' this,',
  32926. ' "IUnknown",',
  32927. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  32928. ' ["QueryInterface", "_AddRef", "_Release"],',
  32929. ' null,',
  32930. ' function () {',
  32931. ' this.$kind = "com";',
  32932. ' var $r = this.$rtti;',
  32933. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  32934. ' $r.addMethod("_AddRef", 1, [], rtl.longint);',
  32935. ' $r.addMethod("_Release", 1, [], rtl.longint);',
  32936. ' }',
  32937. ');',
  32938. 'rtl.createInterface(',
  32939. ' this,',
  32940. ' "IBird",',
  32941. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  32942. ' ["GetItem", "SetItem"],',
  32943. ' this.IUnknown,',
  32944. ' function () {',
  32945. ' var $r = this.$rtti;',
  32946. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32947. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  32948. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem", 2);',
  32949. ' }',
  32950. ');',
  32951. 'this.i = null;',
  32952. 'this.t = null;',
  32953. '']),
  32954. LinesToStr([ // $mod.$main
  32955. '$mod.t = $mod.$rtti["IBird"];',
  32956. '$mod.t = $mod.i.$rtti;',
  32957. '']));
  32958. end;
  32959. procedure TTestModule.TestRTTI_ClassHelper;
  32960. begin
  32961. WithTypeInfo:=true;
  32962. StartProgram(true,[supTypeInfo]);
  32963. Add([
  32964. '{$interfaces com}',
  32965. '{$modeswitch externalclass}',
  32966. 'type',
  32967. ' TObject = class',
  32968. ' end;',
  32969. ' THelper = class helper for TObject',
  32970. ' published',
  32971. ' function GetItem: longint;',
  32972. ' property Item: longint read GetItem;',
  32973. ' end;',
  32974. 'function THelper.GetItem: longint;',
  32975. 'begin',
  32976. 'end;',
  32977. 'var',
  32978. ' t: TTypeInfoHelper;',
  32979. 'begin',
  32980. ' t:=TypeInfo(THelper);',
  32981. '']);
  32982. ConvertProgram;
  32983. CheckSource('TestRTTI_ClassHelper',
  32984. LinesToStr([ // statements
  32985. 'rtl.createClass(this, "TObject", null, function () {',
  32986. ' this.$init = function () {',
  32987. ' };',
  32988. ' this.$final = function () {',
  32989. ' };',
  32990. '});',
  32991. 'rtl.createHelper(this, "THelper", null, function () {',
  32992. ' this.GetItem = function () {',
  32993. ' var Result = 0;',
  32994. ' return Result;',
  32995. ' };',
  32996. ' var $r = this.$rtti;',
  32997. ' $r.addMethod("GetItem", 1, [], 4, rtl.longint);',
  32998. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  32999. '});',
  33000. 'this.t = null;',
  33001. '']),
  33002. LinesToStr([ // $mod.$main
  33003. '$mod.t = $mod.$rtti["THelper"];',
  33004. '']));
  33005. end;
  33006. procedure TTestModule.TestRTTI_ExternalClass;
  33007. begin
  33008. WithTypeInfo:=true;
  33009. StartProgram(true,[supTypeInfo]);
  33010. Add([
  33011. '{$modeswitch externalclass}',
  33012. 'type',
  33013. ' TJSObject = class external name ''Object''',
  33014. ' end;',
  33015. ' TJSArray = class external name ''Array'' (TJSObject)',
  33016. ' end;',
  33017. 'var',
  33018. ' p: Pointer;',
  33019. ' tc: TTypeInfoExtClass;',
  33020. 'begin',
  33021. ' p:=typeinfo(TJSArray);']);
  33022. ConvertProgram;
  33023. CheckSource('TestRTTI_ExternalClass',
  33024. LinesToStr([ // statements
  33025. 'this.$rtti.$ExtClass("TJSObject", {',
  33026. ' jsclass: "Object"',
  33027. '});',
  33028. 'this.$rtti.$ExtClass("TJSArray", {',
  33029. ' ancestor: this.$rtti["TJSObject"],',
  33030. ' jsclass: "Array"',
  33031. '});',
  33032. 'this.p = null;',
  33033. 'this.tc = null;',
  33034. '']),
  33035. LinesToStr([ // $mod.$main
  33036. '$mod.p = $mod.$rtti["TJSArray"];',
  33037. '']));
  33038. end;
  33039. procedure TTestModule.TestRTTI_Unit;
  33040. begin
  33041. WithTypeInfo:=true;
  33042. AddModuleWithIntfImplSrc('unit2.pas',
  33043. LinesToStr([
  33044. '{$mode delphi}',
  33045. 'type',
  33046. ' TWordArray = array of word;',
  33047. ' TArray<T> = array of T;',
  33048. '']),
  33049. '');
  33050. StartUnit(true,[supTypeInfo,supTInterfacedObject]);
  33051. Add([
  33052. '{$mode delphi}',
  33053. 'interface',
  33054. 'uses unit2;',
  33055. 'type',
  33056. ' IBird = interface',
  33057. ' function Swoop: TWordArray;',
  33058. ' function Glide: TArray<word>;',
  33059. ' end;',
  33060. 'procedure Fly;',
  33061. 'implementation',
  33062. 'procedure Fly;',
  33063. 'var',
  33064. ' ta: tTypeInfoDynArray;',
  33065. ' ti: tTypeInfoInterface;',
  33066. 'begin',
  33067. ' ta:=typeinfo(TWordArray);',
  33068. ' ta:=typeinfo(TArray<word>);',
  33069. ' ti:=typeinfo(IBird);',
  33070. 'end;',
  33071. '']);
  33072. ConvertUnit;
  33073. CheckSource('TestRTTI_ExternalClass',
  33074. LinesToStr([ // statements
  33075. 'rtl.createInterface(',
  33076. ' this,',
  33077. ' "IBird",',
  33078. ' "{3B98AAAC-6116-3E17-AA85-F16786D85B09}",',
  33079. ' ["Swoop", "Glide"],',
  33080. ' pas.system.IUnknown,',
  33081. ' function () {',
  33082. ' var $r = this.$rtti;',
  33083. ' $r.addMethod("Swoop", 1, [], pas.unit2.$rtti["TWordArray"]);',
  33084. ' $r.addMethod("Glide", 1, [], pas.unit2.$rtti["TArray<System.Word>"]);',
  33085. ' }',
  33086. ');',
  33087. 'this.Fly = function () {',
  33088. ' var ta = null;',
  33089. ' var ti = null;',
  33090. ' ta = pas.unit2.$rtti["TWordArray"];',
  33091. ' ta = pas.unit2.$rtti["TArray<System.Word>"];',
  33092. ' ti = $mod.$rtti["IBird"];',
  33093. '};',
  33094. '']),
  33095. LinesToStr([ // $mod.$main
  33096. '']));
  33097. end;
  33098. procedure TTestModule.TestResourcestringProgram;
  33099. begin
  33100. AddModuleWithIntfImplSrc('unit2.pas',
  33101. LinesToStr([
  33102. 'resourcestring Title = ''Nice'';',
  33103. '']),
  33104. '');
  33105. StartProgram(true);
  33106. Add([
  33107. 'uses unit2;',
  33108. 'const Bar = ''bar'';',
  33109. 'resourcestring',
  33110. ' Red = ''red'';',
  33111. ' Foobar = ''fOo''+bar;',
  33112. 'var s: string;',
  33113. ' c: char;',
  33114. 'begin',
  33115. ' s:=red;',
  33116. ' s:=test1.red;',
  33117. ' s:=Title;',
  33118. ' c:=red[1];',
  33119. ' c:=test1.red[2];',
  33120. ' if red=foobar then ;',
  33121. ' if red[3]=red[4] then ;']);
  33122. ConvertProgram;
  33123. CheckSource('TestResourcestringProgram',
  33124. LinesToStr([ // statements
  33125. 'this.Bar = "bar";',
  33126. 'this.s = "";',
  33127. 'this.c = "\x00";',
  33128. '$mod.$resourcestrings = {',
  33129. ' Red: {',
  33130. ' org: "red"',
  33131. ' },',
  33132. ' Foobar: {',
  33133. ' org: "fOobar"',
  33134. ' }',
  33135. '};',
  33136. '']),
  33137. LinesToStr([ // $mod.$main
  33138. '$mod.s = rtl.getResStr($mod, "Red");',
  33139. '$mod.s = rtl.getResStr($mod, "Red");',
  33140. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  33141. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  33142. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  33143. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  33144. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  33145. '']));
  33146. end;
  33147. procedure TTestModule.TestResourcestringUnit;
  33148. begin
  33149. AddModuleWithIntfImplSrc('unit2.pas',
  33150. LinesToStr([
  33151. 'resourcestring Title = ''Nice'';',
  33152. '']),
  33153. '');
  33154. StartUnit(true);
  33155. Add([
  33156. 'interface',
  33157. 'uses unit2;',
  33158. 'const Red = ''rEd'';',
  33159. 'resourcestring',
  33160. ' Blue = ''blue'';',
  33161. ' NotRed = ''not''+Red;',
  33162. 'var s: string;',
  33163. 'implementation',
  33164. 'resourcestring',
  33165. ' ImplGreen = ''green'';',
  33166. 'initialization',
  33167. ' s:=blue+ImplGreen;',
  33168. ' s:=test1.blue+test1.implgreen;',
  33169. ' s:=blue[1]+implgreen[2];',
  33170. ' s:=Title;',
  33171. '']);
  33172. ConvertUnit;
  33173. CheckSource('TestResourcestringUnit',
  33174. LinesToStr([ // statements
  33175. 'this.Red = "rEd";',
  33176. 'this.s = "";',
  33177. '$mod.$resourcestrings = {',
  33178. ' Blue: {',
  33179. ' org: "blue"',
  33180. ' },',
  33181. ' NotRed: {',
  33182. ' org: "notrEd"',
  33183. ' },',
  33184. ' ImplGreen: {',
  33185. ' org: "green"',
  33186. ' }',
  33187. '};',
  33188. '']),
  33189. LinesToStr([ // $mod.$main
  33190. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  33191. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  33192. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  33193. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  33194. '']));
  33195. end;
  33196. procedure TTestModule.TestResourcestringImplementation;
  33197. begin
  33198. StartUnit(false);
  33199. Add([
  33200. 'interface',
  33201. 'implementation',
  33202. 'resourcestring',
  33203. ' ImplRed = ''red'';']);
  33204. ConvertUnit;
  33205. CheckSource('TestResourcestringImplementation',
  33206. LinesToStr([ // intf statements
  33207. 'var $impl = $mod.$impl;']),
  33208. LinesToStr([ // $mod.$init
  33209. '']),
  33210. LinesToStr([ // impl statements
  33211. '$mod.$resourcestrings = {',
  33212. ' ImplRed: {',
  33213. ' org: "red"',
  33214. ' }',
  33215. '};',
  33216. '']));
  33217. end;
  33218. procedure TTestModule.TestAttributes_Members;
  33219. begin
  33220. WithTypeInfo:=true;
  33221. StartProgram(false);
  33222. Add([
  33223. '{$modeswitch PrefixedAttributes}',
  33224. 'type',
  33225. ' TObject = class',
  33226. ' constructor Create;',
  33227. ' end;',
  33228. ' TCustomAttribute = class',
  33229. ' constructor Create(Id: word);',
  33230. ' end;',
  33231. ' [Missing]',
  33232. ' TBird = class',
  33233. ' published',
  33234. ' [Tcustom]',
  33235. ' FField: word;',
  33236. ' [tcustom(14)]',
  33237. ' property Size: word read FField;',
  33238. ' [Tcustom(15)]',
  33239. ' procedure Fly; virtual; abstract;',
  33240. ' end;',
  33241. ' TRec = record',
  33242. ' [Tcustom,tcustom(14)]',
  33243. ' Size: word;',
  33244. ' [Tcustom(15)]',
  33245. ' Width, Height: word;',
  33246. ' end;',
  33247. 'constructor TObject.Create; begin end;',
  33248. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  33249. 'begin',
  33250. '']);
  33251. ConvertProgram;
  33252. CheckSource('TestAttributes_Members',
  33253. LinesToStr([ // statements
  33254. 'rtl.createClass(this, "TObject", null, function () {',
  33255. ' this.$init = function () {',
  33256. ' };',
  33257. ' this.$final = function () {',
  33258. ' };',
  33259. ' this.Create = function () {',
  33260. ' return this;',
  33261. ' };',
  33262. '});',
  33263. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  33264. ' this.Create$1 = function (Id) {',
  33265. ' return this;',
  33266. ' };',
  33267. '});',
  33268. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  33269. ' this.$init = function () {',
  33270. ' $mod.TObject.$init.call(this);',
  33271. ' this.FField = 0;',
  33272. ' };',
  33273. ' var $r = this.$rtti;',
  33274. ' $r.addField("FField", rtl.word, 4, {',
  33275. ' attr: [$mod.TCustomAttribute, "Create"]',
  33276. ' });',
  33277. ' $r.addProperty(',
  33278. ' "Size",',
  33279. ' 0,',
  33280. ' rtl.word,',
  33281. ' "FField",',
  33282. ' "",',
  33283. ' 4,',
  33284. ' {',
  33285. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  33286. ' }',
  33287. ' );',
  33288. ' $r.addMethod(',
  33289. ' "Fly",',
  33290. ' 0,',
  33291. ' [],',
  33292. ' 4,',
  33293. ' null,',
  33294. ' 0,',
  33295. ' {',
  33296. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  33297. ' });',
  33298. '});',
  33299. 'rtl.recNewT(this, "TRec", function () {',
  33300. ' this.Size = 0;',
  33301. ' this.Width = 0;',
  33302. ' this.Height = 0;',
  33303. ' this.$eq = function (b) {',
  33304. ' return (this.Size === b.Size) && (this.Width === b.Width) && (this.Height === b.Height);',
  33305. ' };',
  33306. ' this.$assign = function (s) {',
  33307. ' this.Size = s.Size;',
  33308. ' this.Width = s.Width;',
  33309. ' this.Height = s.Height;',
  33310. ' return this;',
  33311. ' };',
  33312. ' var $r = $mod.$rtti.$Record("TRec", {});',
  33313. ' $r.addField("Size", rtl.word, 2, {',
  33314. ' attr: [',
  33315. ' $mod.TCustomAttribute,',
  33316. ' "Create",',
  33317. ' $mod.TCustomAttribute,',
  33318. ' "Create$1",',
  33319. ' [14]',
  33320. ' ]',
  33321. ' });',
  33322. ' $r.addField("Width", rtl.word, 2, {',
  33323. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  33324. ' });',
  33325. ' $r.addField("Height", rtl.word);',
  33326. '});',
  33327. '']),
  33328. LinesToStr([ // $mod.$main
  33329. '']));
  33330. end;
  33331. procedure TTestModule.TestAttributes_Types;
  33332. begin
  33333. WithTypeInfo:=true;
  33334. StartProgram(false);
  33335. Add([
  33336. '{$modeswitch PrefixedAttributes}',
  33337. 'type',
  33338. ' TObject = class',
  33339. ' constructor Create(Id: word);',
  33340. ' end;',
  33341. ' TCustomAttribute = class',
  33342. ' end;',
  33343. ' [TCustom(1)]',
  33344. ' TMyClass = class',
  33345. ' end;',
  33346. ' [TCustom(11)]',
  33347. ' TMyDescendant = class(TMyClass)',
  33348. ' end;',
  33349. ' [TCustom(2)]',
  33350. ' TRec = record',
  33351. ' end;',
  33352. ' [TCustom(3)]',
  33353. ' TInt = type word;',
  33354. 'constructor TObject.Create(Id: word);',
  33355. 'begin',
  33356. 'end;',
  33357. 'var p: pointer;',
  33358. 'begin',
  33359. ' p:=typeinfo(TMyClass);',
  33360. ' p:=typeinfo(TRec);',
  33361. ' p:=typeinfo(TInt);',
  33362. '']);
  33363. ConvertProgram;
  33364. CheckSource('TestAttributes_Types',
  33365. LinesToStr([ // statements
  33366. 'rtl.createClass(this, "TObject", null, function () {',
  33367. ' this.$init = function () {',
  33368. ' };',
  33369. ' this.$final = function () {',
  33370. ' };',
  33371. ' this.Create = function (Id) {',
  33372. ' return this;',
  33373. ' };',
  33374. '});',
  33375. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  33376. '});',
  33377. 'rtl.createClass(this, "TMyClass", this.TObject, function () {',
  33378. ' var $r = this.$rtti;',
  33379. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  33380. '});',
  33381. 'rtl.createClass(this, "TMyDescendant", this.TMyClass, function () {',
  33382. ' var $r = this.$rtti;',
  33383. ' $r.attr = [$mod.TCustomAttribute, "Create", [11]];',
  33384. '});',
  33385. 'rtl.recNewT(this, "TRec", function () {',
  33386. ' this.$eq = function (b) {',
  33387. ' return true;',
  33388. ' };',
  33389. ' this.$assign = function (s) {',
  33390. ' return this;',
  33391. ' };',
  33392. ' $mod.$rtti.$Record("TRec", {',
  33393. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  33394. ' });',
  33395. '});',
  33396. 'this.$rtti.$inherited("TInt", rtl.word, {',
  33397. ' attr: [this.TCustomAttribute, "Create", [3]]',
  33398. '});',
  33399. 'this.p = null;',
  33400. '']),
  33401. LinesToStr([ // $mod.$main
  33402. '$mod.p = $mod.$rtti["TMyClass"];',
  33403. '$mod.p = $mod.$rtti["TRec"];',
  33404. '$mod.p = $mod.$rtti["TInt"];',
  33405. '']));
  33406. end;
  33407. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  33408. begin
  33409. WithTypeInfo:=true;
  33410. StartProgram(false);
  33411. Add([
  33412. '{$modeswitch PrefixedAttributes}',
  33413. 'type',
  33414. ' TObject = class',
  33415. ' constructor Create;',
  33416. ' end;',
  33417. ' TCustomAttribute = class',
  33418. ' end;',
  33419. ' THelper = class helper for TCustomAttribute',
  33420. ' constructor Create(Id: word);',
  33421. ' end;',
  33422. ' [TCustom(3)]',
  33423. ' TMyInt = word;',
  33424. 'constructor TObject.Create; begin end;',
  33425. 'constructor THelper.Create(Id: word); begin end;',
  33426. 'begin',
  33427. ' if typeinfo(TMyInt)=nil then ;']);
  33428. ConvertProgram;
  33429. end;
  33430. procedure TTestModule.TestAttributes_InterfacesList;
  33431. begin
  33432. WithTypeInfo:=true;
  33433. StartProgram(false);
  33434. Add([
  33435. '{$mode Delphi}',
  33436. 'type',
  33437. ' TObject = class',
  33438. ' constructor Create;',
  33439. ' end;',
  33440. ' IInterface = interface end;',
  33441. ' TCustomAttribute = class',
  33442. ' end;',
  33443. ' Red = class(TCustomAttribute);',
  33444. ' Blue = class(TCustomAttribute);',
  33445. ' [Red]',
  33446. ' IBird<T> = interface',
  33447. ' procedure Fly;',
  33448. ' end;',
  33449. ' [Blue]',
  33450. ' IEagle = interface(IBird<Word>)',
  33451. ' procedure Dive;',
  33452. ' end;',
  33453. ' TAnt = class(TObject, IEagle)',
  33454. ' procedure Fly; virtual; abstract;',
  33455. ' procedure Dive; virtual; abstract;',
  33456. ' end;',
  33457. 'constructor TObject.Create;',
  33458. 'begin',
  33459. 'end;',
  33460. 'begin',
  33461. '']);
  33462. ConvertProgram;
  33463. CheckSource('TestAttributes_InterfacesList',
  33464. LinesToStr([ // statements
  33465. '$mod.$rtti.$Interface("IBird<System.Word>");',
  33466. 'rtl.createClass(this, "TObject", null, function () {',
  33467. ' this.$init = function () {',
  33468. ' };',
  33469. ' this.$final = function () {',
  33470. ' };',
  33471. ' this.Create = function () {',
  33472. ' return this;',
  33473. ' };',
  33474. '});',
  33475. 'rtl.createInterface(',
  33476. ' this,',
  33477. ' "IInterface",',
  33478. ' "{B92D5841-698D-3153-90C5-000000000000}",',
  33479. ' [],',
  33480. ' null,',
  33481. ' function () {',
  33482. ' this.$kind = "com";',
  33483. ' }',
  33484. ');',
  33485. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  33486. '});',
  33487. 'rtl.createClass(this, "Red", this.TCustomAttribute, function () {',
  33488. '});',
  33489. 'rtl.createClass(this, "Blue", this.TCustomAttribute, function () {',
  33490. '});',
  33491. 'rtl.createInterface(',
  33492. ' this,',
  33493. ' "IBird$G1",',
  33494. ' "{14691591-6648-3574-B8C8-FAAD81DAC421}",',
  33495. ' ["Fly"],',
  33496. ' this.IInterface,',
  33497. ' function () {',
  33498. ' var $r = this.$rtti;',
  33499. ' $r.addMethod("Fly", 0, []);',
  33500. ' $r.attr = [$mod.Red, "Create"];',
  33501. ' },',
  33502. ' "IBird<System.Word>"',
  33503. ');',
  33504. 'rtl.createInterface(',
  33505. ' this,',
  33506. ' "IEagle",',
  33507. ' "{5F4202AE-F2BE-37FD-8A88-1A2F926F1117}",',
  33508. ' ["Dive"],',
  33509. ' this.IBird$G1,',
  33510. ' function () {',
  33511. ' var $r = this.$rtti;',
  33512. ' $r.addMethod("Dive", 0, []);',
  33513. ' $r.attr = [$mod.Blue, "Create"];',
  33514. ' }',
  33515. ');',
  33516. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  33517. ' rtl.addIntf(this, $mod.IEagle);',
  33518. '});',
  33519. '']),
  33520. LinesToStr([ // $mod.$main
  33521. '']));
  33522. end;
  33523. procedure TTestModule.TestAssert;
  33524. begin
  33525. StartProgram(false);
  33526. Add([
  33527. 'procedure DoIt;',
  33528. 'var',
  33529. ' b: boolean;',
  33530. ' s: string;',
  33531. 'begin',
  33532. ' {$Assertions on}',
  33533. ' Assert(b);',
  33534. 'end;',
  33535. 'begin',
  33536. ' DoIt;',
  33537. '']);
  33538. ConvertProgram;
  33539. CheckSource('TestAssert',
  33540. LinesToStr([ // statements
  33541. 'this.DoIt = function () {',
  33542. ' var b = false;',
  33543. ' var s = "";',
  33544. ' if (!b) throw "assert failed";',
  33545. '};',
  33546. '']),
  33547. LinesToStr([ // $mod.$main
  33548. '$mod.DoIt();',
  33549. '']));
  33550. end;
  33551. procedure TTestModule.TestAssert_SysUtils;
  33552. begin
  33553. AddModuleWithIntfImplSrc('SysUtils.pas',
  33554. LinesToStr([
  33555. 'type',
  33556. ' TObject = class',
  33557. ' constructor Create;',
  33558. ' end;',
  33559. ' EAssertionFailed = class',
  33560. ' constructor Create(s: string);',
  33561. ' end;',
  33562. '']),
  33563. LinesToStr([
  33564. 'constructor TObject.Create;',
  33565. 'begin end;',
  33566. 'constructor EAssertionFailed.Create(s: string);',
  33567. 'begin end;',
  33568. '']) );
  33569. StartProgram(true);
  33570. Add([
  33571. 'uses sysutils;',
  33572. 'procedure DoIt;',
  33573. 'var',
  33574. ' b: boolean;',
  33575. ' s: string;',
  33576. 'begin',
  33577. ' {$Assertions on}',
  33578. ' Assert(b);',
  33579. ' Assert(b,''msg'');',
  33580. 'end;',
  33581. 'begin',
  33582. ' DoIt;',
  33583. '']);
  33584. ConvertProgram;
  33585. CheckSource('TestAssert_SysUtils',
  33586. LinesToStr([ // statements
  33587. 'this.DoIt = function () {',
  33588. ' var b = false;',
  33589. ' var s = "";',
  33590. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  33591. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  33592. '};',
  33593. '']),
  33594. LinesToStr([ // $mod.$main
  33595. '$mod.DoIt();',
  33596. '']));
  33597. end;
  33598. procedure TTestModule.TestObjectChecks;
  33599. begin
  33600. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  33601. StartProgram(false);
  33602. Add([
  33603. 'type',
  33604. ' TObject = class',
  33605. ' procedure DoIt;',
  33606. ' end;',
  33607. ' TClass = class of tobject;',
  33608. ' TBird = class',
  33609. ' end;',
  33610. ' TBirdClass = class of TBird;',
  33611. 'var',
  33612. ' o : TObject;',
  33613. ' c: TClass;',
  33614. ' b: TBird;',
  33615. ' bc: TBirdClass;',
  33616. 'procedure TObject.DoIt;',
  33617. 'begin',
  33618. ' b:=TBird(o);',
  33619. 'end;',
  33620. 'begin',
  33621. ' o.DoIt;',
  33622. ' b:=TBird(o);',
  33623. ' bc:=TBirdClass(c);',
  33624. '']);
  33625. ConvertProgram;
  33626. CheckSource('TestCheckMethodCall',
  33627. LinesToStr([ // statements
  33628. 'rtl.createClass(this, "TObject", null, function () {',
  33629. ' this.$init = function () {',
  33630. ' };',
  33631. ' this.$final = function () {',
  33632. ' };',
  33633. ' this.DoIt = function () {',
  33634. ' rtl.checkMethodCall(this,$mod.TObject);',
  33635. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  33636. ' };',
  33637. '});',
  33638. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  33639. '});',
  33640. 'this.o = null;',
  33641. 'this.c = null;',
  33642. 'this.b = null;',
  33643. 'this.bc = null;',
  33644. '']),
  33645. LinesToStr([ // $mod.$main
  33646. '$mod.o.DoIt();',
  33647. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  33648. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  33649. '']));
  33650. end;
  33651. procedure TTestModule.TestOverflowChecks_Int;
  33652. begin
  33653. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  33654. StartProgram(false);
  33655. Add([
  33656. 'procedure DoIt;',
  33657. 'var',
  33658. ' b: byte;',
  33659. ' n: nativeint;',
  33660. ' u: nativeuint;',
  33661. ' c: currency;',
  33662. 'begin',
  33663. ' n:=n+n;',
  33664. ' n:=n-n;',
  33665. ' n:=n+b;',
  33666. ' n:=b-n;',
  33667. ' n:=n*n;',
  33668. ' n:=n*u;',
  33669. ' c:=c+b;',
  33670. ' c:=b+c;',
  33671. ' c:=c*b;',
  33672. ' c:=b*c;',
  33673. 'end;',
  33674. 'begin',
  33675. '']);
  33676. ConvertProgram;
  33677. CheckSource('TestOverflowChecks_Int',
  33678. LinesToStr([ // statements
  33679. 'this.DoIt = function () {',
  33680. ' var b = 0;',
  33681. ' var n = 0;',
  33682. ' var u = 0;',
  33683. ' var c = 0;',
  33684. ' n = rtl.oc(n + n);',
  33685. ' n = rtl.oc(n - n);',
  33686. ' n = rtl.oc(n + b);',
  33687. ' n = rtl.oc(b - n);',
  33688. ' n = rtl.oc(n * n);',
  33689. ' n = rtl.oc(n * u);',
  33690. ' c = rtl.oc(c + (b * 10000));',
  33691. ' c = rtl.oc((b * 10000) + c);',
  33692. ' c = rtl.oc(c * b);',
  33693. ' c = rtl.oc(b * c);',
  33694. '};',
  33695. '']),
  33696. LinesToStr([ // $mod.$main
  33697. '']));
  33698. end;
  33699. procedure TTestModule.TestRangeChecks_AssignInt;
  33700. begin
  33701. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33702. StartProgram(false);
  33703. Add([
  33704. '{$R+}',
  33705. 'var',
  33706. ' b: byte = 2;',
  33707. ' w: word = 3;',
  33708. 'procedure DoIt(p: byte);',
  33709. 'begin',
  33710. ' b:=w;',
  33711. ' b+=w;',
  33712. ' b:=1;',
  33713. 'end;',
  33714. '{$R-}',
  33715. 'procedure DoSome;',
  33716. 'begin',
  33717. ' DoIt(w);',
  33718. ' b:=w;',
  33719. ' b:=2;',
  33720. 'end;',
  33721. 'begin',
  33722. '{$R+}',
  33723. '']);
  33724. ConvertProgram;
  33725. CheckSource('TestRangeChecks_AssignInt',
  33726. LinesToStr([ // statements
  33727. 'this.b = 2;',
  33728. 'this.w = 3;',
  33729. 'this.DoIt = function (p) {',
  33730. ' rtl.rc(p, 0, 255);',
  33731. ' $mod.b = rtl.rc($mod.w,0,255);',
  33732. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  33733. ' $mod.b = 1;',
  33734. '};',
  33735. 'this.DoSome = function () {',
  33736. ' $mod.DoIt($mod.w);',
  33737. ' $mod.b = $mod.w;',
  33738. ' $mod.b = 2;',
  33739. '};',
  33740. '']),
  33741. LinesToStr([ // $mod.$main
  33742. '']));
  33743. end;
  33744. procedure TTestModule.TestRangeChecks_AssignIntRange;
  33745. begin
  33746. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33747. StartProgram(false);
  33748. Add([
  33749. '{$R+}',
  33750. 'type Ten = 1..10;',
  33751. 'var',
  33752. ' b: Ten = 2;',
  33753. ' w: Ten = 3;',
  33754. 'procedure DoIt(p: Ten);',
  33755. 'begin',
  33756. ' b:=w;',
  33757. ' b+=w;',
  33758. ' b:=1;',
  33759. 'end;',
  33760. '{$R-}',
  33761. 'procedure DoSome;',
  33762. 'begin',
  33763. ' DoIt(w);',
  33764. ' b:=w;',
  33765. ' b:=2;',
  33766. 'end;',
  33767. 'begin',
  33768. '{$R+}',
  33769. '']);
  33770. ConvertProgram;
  33771. CheckSource('TestRangeChecks_AssignIntRange',
  33772. LinesToStr([ // statements
  33773. 'this.b = 2;',
  33774. 'this.w = 3;',
  33775. 'this.DoIt = function (p) {',
  33776. ' rtl.rc(p, 1, 10);',
  33777. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  33778. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  33779. ' $mod.b = 1;',
  33780. '};',
  33781. 'this.DoSome = function () {',
  33782. ' $mod.DoIt($mod.w);',
  33783. ' $mod.b = $mod.w;',
  33784. ' $mod.b = 2;',
  33785. '};',
  33786. '']),
  33787. LinesToStr([ // $mod.$main
  33788. '']));
  33789. end;
  33790. procedure TTestModule.TestRangeChecks_AssignEnum;
  33791. begin
  33792. StartProgram(false);
  33793. Add([
  33794. '{$R+}',
  33795. 'type TEnum = (red,green);',
  33796. 'var',
  33797. ' e: TEnum = red;',
  33798. 'procedure DoIt(p: TEnum);',
  33799. 'begin',
  33800. ' e:=p;',
  33801. ' p:=TEnum(0);',
  33802. ' p:=succ(e);',
  33803. 'end;',
  33804. '{$R-}',
  33805. 'procedure DoSome;',
  33806. 'begin',
  33807. ' DoIt(e);',
  33808. ' e:=TEnum(1);',
  33809. ' e:=pred(e);',
  33810. 'end;',
  33811. 'begin',
  33812. '{$R+}',
  33813. '']);
  33814. ConvertProgram;
  33815. CheckSource('TestRangeChecks_AssignEnum',
  33816. LinesToStr([ // statements
  33817. 'this.TEnum = {',
  33818. ' "0": "red",',
  33819. ' red: 0,',
  33820. ' "1": "green",',
  33821. ' green: 1',
  33822. '};',
  33823. 'this.e = this.TEnum.red;',
  33824. 'this.DoIt = function (p) {',
  33825. ' rtl.rc(p, 0, 1);',
  33826. ' $mod.e = rtl.rc(p, 0, 1);',
  33827. ' p = 0;',
  33828. ' p = rtl.rc($mod.e + 1, 0, 1);',
  33829. '};',
  33830. 'this.DoSome = function () {',
  33831. ' $mod.DoIt($mod.e);',
  33832. ' $mod.e = 1;',
  33833. ' $mod.e = $mod.e - 1;',
  33834. '};',
  33835. '']),
  33836. LinesToStr([ // $mod.$main
  33837. '']));
  33838. end;
  33839. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  33840. begin
  33841. StartProgram(false);
  33842. Add([
  33843. '{$R+}',
  33844. 'type',
  33845. ' TEnum = (red,green);',
  33846. ' TEnumRg = red..green;',
  33847. 'var',
  33848. ' e: TEnumRg = red;',
  33849. 'procedure DoIt(p: TEnumRg);',
  33850. 'begin',
  33851. ' e:=p;',
  33852. ' p:=TEnumRg(0);',
  33853. ' p:=succ(e);',
  33854. 'end;',
  33855. '{$R-}',
  33856. 'procedure DoSome;',
  33857. 'begin',
  33858. ' DoIt(e);',
  33859. ' e:=TEnum(1);',
  33860. ' e:=pred(e);',
  33861. 'end;',
  33862. 'begin',
  33863. '{$R+}',
  33864. '']);
  33865. ConvertProgram;
  33866. CheckSource('TestRangeChecks_AssignEnumRange',
  33867. LinesToStr([ // statements
  33868. 'this.TEnum = {',
  33869. ' "0": "red",',
  33870. ' red: 0,',
  33871. ' "1": "green",',
  33872. ' green: 1',
  33873. '};',
  33874. 'this.e = this.TEnum.red;',
  33875. 'this.DoIt = function (p) {',
  33876. ' rtl.rc(p, 0, 1);',
  33877. ' $mod.e = rtl.rc(p, 0, 1);',
  33878. ' p = 0;',
  33879. ' p = rtl.rc($mod.e + 1, 0, 1);',
  33880. '};',
  33881. 'this.DoSome = function () {',
  33882. ' $mod.DoIt($mod.e);',
  33883. ' $mod.e = 1;',
  33884. ' $mod.e = $mod.e - 1;',
  33885. '};',
  33886. '']),
  33887. LinesToStr([ // $mod.$main
  33888. '']));
  33889. end;
  33890. procedure TTestModule.TestRangeChecks_AssignChar;
  33891. begin
  33892. StartProgram(false);
  33893. Add([
  33894. '{$R+}',
  33895. 'type',
  33896. ' TLetter = char;',
  33897. 'var',
  33898. ' b: TLetter = ''2'';',
  33899. ' w: TLetter = ''3'';',
  33900. 'procedure DoIt(p: TLetter);',
  33901. 'begin',
  33902. ' b:=w;',
  33903. ' b:=''1'';',
  33904. 'end;',
  33905. '{$R-}',
  33906. 'procedure DoSome;',
  33907. 'begin',
  33908. ' DoIt(w);',
  33909. ' b:=w;',
  33910. ' b:=''2'';',
  33911. 'end;',
  33912. 'begin',
  33913. '{$R+}',
  33914. '']);
  33915. ConvertProgram;
  33916. CheckSource('TestRangeChecks_AssignChar',
  33917. LinesToStr([ // statements
  33918. 'this.b = "2";',
  33919. 'this.w = "3";',
  33920. 'this.DoIt = function (p) {',
  33921. ' rtl.rcc(p, 0, 65535);',
  33922. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  33923. ' $mod.b = "1";',
  33924. '};',
  33925. 'this.DoSome = function () {',
  33926. ' $mod.DoIt($mod.w);',
  33927. ' $mod.b = $mod.w;',
  33928. ' $mod.b = "2";',
  33929. '};',
  33930. '']),
  33931. LinesToStr([ // $mod.$main
  33932. '']));
  33933. end;
  33934. procedure TTestModule.TestRangeChecks_AssignCharRange;
  33935. begin
  33936. StartProgram(false);
  33937. Add([
  33938. '{$R+}',
  33939. 'type TDigit = ''0''..''9'';',
  33940. 'var',
  33941. ' b: TDigit = ''2'';',
  33942. ' w: TDigit = ''3'';',
  33943. 'procedure DoIt(p: TDigit);',
  33944. 'begin',
  33945. ' b:=w;',
  33946. ' b:=''1'';',
  33947. 'end;',
  33948. '{$R-}',
  33949. 'procedure DoSome;',
  33950. 'begin',
  33951. ' DoIt(w);',
  33952. ' b:=w;',
  33953. ' b:=''2'';',
  33954. 'end;',
  33955. 'begin',
  33956. '{$R+}',
  33957. '']);
  33958. ConvertProgram;
  33959. CheckSource('TestRangeChecks_AssignCharRange',
  33960. LinesToStr([ // statements
  33961. 'this.b = "2";',
  33962. 'this.w = "3";',
  33963. 'this.DoIt = function (p) {',
  33964. ' rtl.rcc(p, 48, 57);',
  33965. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  33966. ' $mod.b = "1";',
  33967. '};',
  33968. 'this.DoSome = function () {',
  33969. ' $mod.DoIt($mod.w);',
  33970. ' $mod.b = $mod.w;',
  33971. ' $mod.b = "2";',
  33972. '};',
  33973. '']),
  33974. LinesToStr([ // $mod.$main
  33975. '']));
  33976. end;
  33977. procedure TTestModule.TestRangeChecks_ArrayIndex;
  33978. begin
  33979. StartProgram(false);
  33980. Add([
  33981. '{$R+}',
  33982. 'type',
  33983. ' Ten = 1..10;',
  33984. ' TArr = array of Ten;',
  33985. ' TArrArr = array of TArr;',
  33986. ' TArrByte = array[byte] of Ten;',
  33987. ' TArrChar = array[''0''..''9''] of Ten;',
  33988. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  33989. ' TObject = class',
  33990. ' A: TArr;',
  33991. ' end;',
  33992. 'procedure DoIt;',
  33993. 'var',
  33994. ' Arr: TArr;',
  33995. ' ArrArr: TArrArr;',
  33996. ' ArrByte: TArrByte;',
  33997. ' ArrChar: TArrChar;',
  33998. ' ArrByteChar: TArrByteChar;',
  33999. ' i: Ten;',
  34000. ' c: char;',
  34001. ' o: tobject;',
  34002. 'begin',
  34003. ' i:=Arr[1];',
  34004. ' i:=ArrByteChar[1,''2''];',
  34005. ' Arr[1]:=Arr[1];',
  34006. ' Arr[i]:=Arr[i];',
  34007. ' ArrByte[3]:=ArrByte[3];',
  34008. ' ArrByte[i]:=ArrByte[i];',
  34009. ' ArrChar[''5'']:=ArrChar[''5''];',
  34010. ' ArrChar[c]:=ArrChar[c];',
  34011. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  34012. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  34013. ' o.a[i]:=o.a[i];',
  34014. 'end;',
  34015. 'begin',
  34016. '']);
  34017. ConvertProgram;
  34018. CheckSource('TestRangeChecks_ArrayIndex',
  34019. LinesToStr([ // statements
  34020. 'this.TArrByteChar$clone = function (a) {',
  34021. ' var b = [];',
  34022. ' b.length = 256;',
  34023. ' for (var c = 0; c < 256; c++) b[c] = a[c].slice(0);',
  34024. ' return b;',
  34025. '};',
  34026. 'rtl.createClass(this, "TObject", null, function () {',
  34027. ' this.$init = function () {',
  34028. ' this.A = [];',
  34029. ' };',
  34030. ' this.$final = function () {',
  34031. ' this.A = undefined;',
  34032. ' };',
  34033. '});',
  34034. 'this.DoIt = function () {',
  34035. ' var Arr = [];',
  34036. ' var ArrArr = [];',
  34037. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  34038. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  34039. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  34040. ' var i = 0;',
  34041. ' var c = "\x00";',
  34042. ' var o = null;',
  34043. ' i = rtl.rc(Arr[1], 1, 10);',
  34044. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  34045. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  34046. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  34047. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  34048. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  34049. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  34050. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  34051. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  34052. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  34053. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  34054. '};',
  34055. '']),
  34056. LinesToStr([ // $mod.$main
  34057. '']));
  34058. end;
  34059. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  34060. begin
  34061. StartProgram(false);
  34062. Add([
  34063. '{$R+}',
  34064. 'type',
  34065. ' Ten = 1..10;',
  34066. ' TRec = record x: Ten end;',
  34067. ' TArr = array of TRec;',
  34068. ' TArrArr = array of TArr;',
  34069. ' TObject = class',
  34070. ' A: TArr;',
  34071. ' end;',
  34072. 'procedure DoIt;',
  34073. 'var',
  34074. ' Arr: TArr;',
  34075. ' ArrArr: TArrArr;',
  34076. ' i: Ten;',
  34077. ' o: tobject;',
  34078. 'begin',
  34079. ' Arr[1]:=Arr[1];',
  34080. ' Arr[i]:=Arr[i+1];',
  34081. ' o.a[i]:=o.a[i+2];',
  34082. 'end;',
  34083. 'begin',
  34084. '']);
  34085. ConvertProgram;
  34086. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  34087. LinesToStr([ // statements
  34088. 'rtl.recNewT(this, "TRec", function () {',
  34089. ' this.x = 0;',
  34090. ' this.$eq = function (b) {',
  34091. ' return this.x === b.x;',
  34092. ' };',
  34093. ' this.$assign = function (s) {',
  34094. ' this.x = s.x;',
  34095. ' return this;',
  34096. ' };',
  34097. '});',
  34098. 'rtl.createClass(this, "TObject", null, function () {',
  34099. ' this.$init = function () {',
  34100. ' this.A = [];',
  34101. ' };',
  34102. ' this.$final = function () {',
  34103. ' this.A = undefined;',
  34104. ' };',
  34105. '});',
  34106. 'this.DoIt = function () {',
  34107. ' var Arr = [];',
  34108. ' var ArrArr = [];',
  34109. ' var i = 0;',
  34110. ' var o = null;',
  34111. ' Arr[1].$assign(Arr[1]);',
  34112. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  34113. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  34114. '};',
  34115. '']),
  34116. LinesToStr([ // $mod.$main
  34117. '']));
  34118. end;
  34119. procedure TTestModule.TestRangeChecks_StringIndex;
  34120. begin
  34121. StartProgram(false);
  34122. Add([
  34123. 'type',
  34124. ' TObject = class',
  34125. ' S: string;',
  34126. ' end;',
  34127. '{$R+}',
  34128. 'procedure DoIt(var h: string);',
  34129. 'var',
  34130. ' s: string;',
  34131. ' i: longint;',
  34132. ' c: char;',
  34133. ' o: tobject;',
  34134. 'begin',
  34135. ' c:=s[1];',
  34136. ' s[i]:=s[i];',
  34137. ' h[i]:=h[i];',
  34138. ' c:=o.s[i];',
  34139. ' o.s[i]:=c;',
  34140. 'end;',
  34141. 'begin',
  34142. '']);
  34143. ConvertProgram;
  34144. CheckSource('TestRangeChecks_StringIndex',
  34145. LinesToStr([ // statements
  34146. 'rtl.createClass(this, "TObject", null, function () {',
  34147. ' this.$init = function () {',
  34148. ' this.S = "";',
  34149. ' };',
  34150. ' this.$final = function () {',
  34151. ' };',
  34152. '});',
  34153. 'this.DoIt = function (h) {',
  34154. ' var s = "";',
  34155. ' var i = 0;',
  34156. ' var c = "\x00";',
  34157. ' var o = null;',
  34158. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  34159. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  34160. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  34161. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  34162. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  34163. '};',
  34164. '']),
  34165. LinesToStr([ // $mod.$main
  34166. '']));
  34167. end;
  34168. procedure TTestModule.TestRangeChecks_TypecastInt;
  34169. begin
  34170. StartProgram(false);
  34171. Add([
  34172. '{$R+}',
  34173. 'var',
  34174. ' i: nativeint;',
  34175. ' b: byte;',
  34176. ' sh: shortint;',
  34177. ' w: word;',
  34178. ' sm: smallint;',
  34179. ' lw: longword;',
  34180. ' li: longint;',
  34181. 'begin',
  34182. ' b:=12+byte(i);',
  34183. ' sh:=12+shortint(i);',
  34184. ' w:=12+word(i);',
  34185. ' sm:=12+smallint(i);',
  34186. ' lw:=12+longword(i);',
  34187. ' li:=12+longint(i);',
  34188. '']);
  34189. ConvertProgram;
  34190. CheckSource('TestRangeChecks_TypecastInt',
  34191. LinesToStr([
  34192. 'this.i = 0;',
  34193. 'this.b = 0;',
  34194. 'this.sh = 0;',
  34195. 'this.w = 0;',
  34196. 'this.sm = 0;',
  34197. 'this.lw = 0;',
  34198. 'this.li = 0;',
  34199. '']),
  34200. LinesToStr([
  34201. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  34202. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  34203. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  34204. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  34205. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  34206. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  34207. '']));
  34208. end;
  34209. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  34210. begin
  34211. Scanner.Options:=Scanner.Options+[po_CAssignments];
  34212. StartProgram(false);
  34213. Add([
  34214. '{$modeswitch typehelpers}',
  34215. '{$R+}',
  34216. 'type',
  34217. ' TObject = class',
  34218. ' FSize: byte;',
  34219. ' property Size: byte read FSize;',
  34220. ' end;',
  34221. ' THelper = type helper for byte',
  34222. ' procedure SetIt(w: word);',
  34223. ' end;',
  34224. 'procedure THelper.SetIt(w: word);',
  34225. 'begin',
  34226. ' Self:=w;',
  34227. 'end;',
  34228. 'function GetIt: byte;',
  34229. 'begin',
  34230. ' Result.SetIt(2);',
  34231. 'end;',
  34232. 'var',
  34233. ' b: byte = 3;',
  34234. ' o: TObject;',
  34235. 'begin',
  34236. ' b.SetIt(14);',
  34237. ' with b do SetIt(15);',
  34238. ' o.Size.SetIt(16);',
  34239. '']);
  34240. ConvertProgram;
  34241. CheckSource('TestRangeChecks_AssignInt',
  34242. LinesToStr([ // statements
  34243. 'rtl.createClass(this, "TObject", null, function () {',
  34244. ' this.$init = function () {',
  34245. ' this.FSize = 0;',
  34246. ' };',
  34247. ' this.$final = function () {',
  34248. ' };',
  34249. '});',
  34250. 'rtl.createHelper(this, "THelper", null, function () {',
  34251. ' this.SetIt = function (w) {',
  34252. ' rtl.rc(w, 0, 65535);',
  34253. ' this.set(w);',
  34254. ' };',
  34255. '});',
  34256. 'this.GetIt = function () {',
  34257. ' var Result = 0;',
  34258. ' $mod.THelper.SetIt.call({',
  34259. ' get: function () {',
  34260. ' return Result;',
  34261. ' },',
  34262. ' set: function (v) {',
  34263. ' rtl.rc(v, 0, 255);',
  34264. ' Result = v;',
  34265. ' }',
  34266. ' }, 2);',
  34267. ' return Result;',
  34268. '};',
  34269. 'this.b = 3;',
  34270. 'this.o = null;',
  34271. '']),
  34272. LinesToStr([ // $mod.$main
  34273. '$mod.THelper.SetIt.call({',
  34274. ' p: $mod,',
  34275. ' get: function () {',
  34276. ' return this.p.b;',
  34277. ' },',
  34278. ' set: function (v) {',
  34279. ' rtl.rc(v, 0, 255);',
  34280. ' this.p.b = v;',
  34281. ' }',
  34282. '}, 14);',
  34283. 'var $with = $mod.b;',
  34284. '$mod.THelper.SetIt.call({',
  34285. ' get: function () {',
  34286. ' return $with;',
  34287. ' },',
  34288. ' set: function (v) {',
  34289. ' rtl.rc(v, 0, 255);',
  34290. ' $with = v;',
  34291. ' }',
  34292. '}, 15);',
  34293. '$mod.THelper.SetIt.call({',
  34294. ' p: $mod.o,',
  34295. ' get: function () {',
  34296. ' return this.p.FSize;',
  34297. ' },',
  34298. ' set: function (v) {',
  34299. ' rtl.rc(v, 0, 255);',
  34300. ' this.p.FSize = v;',
  34301. ' }',
  34302. '}, 16);',
  34303. '']));
  34304. end;
  34305. procedure TTestModule.TestRangeChecks_AssignCurrency;
  34306. begin
  34307. Scanner.Options:=Scanner.Options+[po_CAssignments];
  34308. StartProgram(false);
  34309. Add([
  34310. '{$R+}',
  34311. 'var',
  34312. ' c: currency = 2.34;',
  34313. ' i: double;',
  34314. 'procedure DoIt(p: currency);',
  34315. 'begin',
  34316. ' c:=i;',
  34317. ' c+=i;',
  34318. ' c:=1;',
  34319. 'end;',
  34320. '{$R-}',
  34321. 'procedure DoSome;',
  34322. 'begin',
  34323. ' DoIt(i);',
  34324. ' c:=i;',
  34325. ' c:=2;',
  34326. 'end;',
  34327. 'begin',
  34328. '{$R+}',
  34329. '']);
  34330. ConvertProgram;
  34331. CheckSource('TestRangeChecks_AssignCurrency',
  34332. LinesToStr([ // statements
  34333. 'this.c = 2.34;',
  34334. 'this.i = 0.0;',
  34335. 'this.DoIt = function (p) {',
  34336. ' rtl.rc(p, -922337203685477, 922337203685477);',
  34337. ' $mod.c = rtl.rc(rtl.trunc($mod.i * 10000), -922337203685477, 922337203685477);',
  34338. ' rtl.rc($mod.c += rtl.trunc($mod.i * 10000), -922337203685477, 922337203685477);',
  34339. ' $mod.c = 10000;',
  34340. '};',
  34341. 'this.DoSome = function () {',
  34342. ' $mod.DoIt($mod.i * 10000);',
  34343. ' $mod.c = rtl.trunc($mod.i * 10000);',
  34344. ' $mod.c = 20000;',
  34345. '};',
  34346. '']),
  34347. LinesToStr([ // $mod.$main
  34348. '']));
  34349. end;
  34350. procedure TTestModule.TestAsync_Proc;
  34351. begin
  34352. StartProgram(false);
  34353. Add([
  34354. 'procedure Fly(w: word = 1); async; forward;',
  34355. 'procedure Run(w: word = 2); async;',
  34356. 'begin',
  34357. ' Fly(w);',
  34358. ' Fly;',
  34359. ' await(Fly(w));',
  34360. ' await(Fly);',
  34361. 'end;',
  34362. 'procedure Fly(w: word); ',
  34363. 'begin',
  34364. 'end;',
  34365. 'begin',
  34366. ' Run;',
  34367. ' Run(3);',
  34368. '']);
  34369. CheckResolverUnexpectedHints();
  34370. ConvertProgram;
  34371. CheckSource('TestAsync_Proc',
  34372. LinesToStr([ // statements
  34373. 'this.Run = async function (w) {',
  34374. ' $mod.Fly(w);',
  34375. ' $mod.Fly(1);',
  34376. ' await $mod.Fly(w);',
  34377. ' await $mod.Fly(1);',
  34378. '};',
  34379. 'this.Fly = async function (w) {',
  34380. '};',
  34381. '']),
  34382. LinesToStr([
  34383. '$mod.Run(2);',
  34384. '$mod.Run(3);',
  34385. '']));
  34386. end;
  34387. procedure TTestModule.TestAsync_CallResultIsPromise;
  34388. begin
  34389. StartProgram(false);
  34390. Add([
  34391. '{$modeswitch externalclass}',
  34392. 'type',
  34393. ' TObject = class',
  34394. ' end;',
  34395. ' TJSPromise = class external name ''Promise''',
  34396. ' end;',
  34397. ' TBird = class',
  34398. ' function Fly: word; async; ',
  34399. ' end;',
  34400. 'function TBird.Fly: word; async; ',
  34401. 'begin',
  34402. ' Result:=3;',
  34403. ' Fly:=4+Result;',
  34404. ' if Result=5 then ;',
  34405. ' exit(6);',
  34406. 'end;',
  34407. 'function Run: word; async;',
  34408. 'begin',
  34409. ' Result:=11+Result;',
  34410. ' inc(Result);',
  34411. 'end;',
  34412. 'var',
  34413. ' p: TJSPromise;',
  34414. ' o: TBird;',
  34415. 'begin',
  34416. ' p:=Run;',
  34417. ' p:=Run();',
  34418. ' if Run=p then ;',
  34419. ' if p=Run then ;',
  34420. ' if Run()=p then ;',
  34421. ' if p=Run() then ;',
  34422. ' p:=o.Fly;',
  34423. ' p:=o.Fly();',
  34424. ' if o.Fly=p then ;',
  34425. ' if o.Fly()=p then ;',
  34426. ' with o do begin',
  34427. ' p:=Fly;',
  34428. ' p:=Fly();',
  34429. ' if Fly=p then ;',
  34430. ' if Fly()=p then ;',
  34431. ' end;',
  34432. '']);
  34433. CheckResolverUnexpectedHints();
  34434. ConvertProgram;
  34435. CheckSource('TestAsync_CallResultIsPromise',
  34436. LinesToStr([ // statements
  34437. 'rtl.createClass(this, "TObject", null, function () {',
  34438. ' this.$init = function () {',
  34439. ' };',
  34440. ' this.$final = function () {',
  34441. ' };',
  34442. '});',
  34443. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34444. ' this.Fly = async function () {',
  34445. ' var Result = 0;',
  34446. ' Result = 3;',
  34447. ' Result = 4 + Result;',
  34448. ' if (Result === 5) ;',
  34449. ' return 6;',
  34450. ' return Result;',
  34451. ' };',
  34452. '});',
  34453. 'this.Run = async function () {',
  34454. ' var Result = 0;',
  34455. ' Result = 11 + Result;',
  34456. ' Result += 1;',
  34457. ' return Result;',
  34458. '};',
  34459. 'this.p = null;',
  34460. 'this.o = null;',
  34461. '']),
  34462. LinesToStr([
  34463. '$mod.p = $mod.Run();',
  34464. '$mod.p = $mod.Run();',
  34465. 'if ($mod.Run() === $mod.p) ;',
  34466. 'if ($mod.p === $mod.Run()) ;',
  34467. 'if ($mod.Run() === $mod.p) ;',
  34468. 'if ($mod.p === $mod.Run()) ;',
  34469. '$mod.p = $mod.o.Fly();',
  34470. '$mod.p = $mod.o.Fly();',
  34471. 'if ($mod.o.Fly() === $mod.p) ;',
  34472. 'if ($mod.o.Fly() === $mod.p) ;',
  34473. 'var $with = $mod.o;',
  34474. '$mod.p = $with.Fly();',
  34475. '$mod.p = $with.Fly();',
  34476. 'if ($with.Fly() === $mod.p) ;',
  34477. 'if ($with.Fly() === $mod.p) ;',
  34478. '']));
  34479. end;
  34480. procedure TTestModule.TestAsync_ConstructorFail;
  34481. begin
  34482. StartProgram(false);
  34483. Add([
  34484. 'type',
  34485. ' TObject = class',
  34486. ' end;',
  34487. ' TBird = class',
  34488. ' constructor Create; async;',
  34489. ' end;',
  34490. 'constructor TBird.Create; async;',
  34491. 'begin',
  34492. 'end;',
  34493. 'begin',
  34494. '']);
  34495. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  34496. ConvertProgram;
  34497. end;
  34498. procedure TTestModule.TestAsync_PropertyGetterFail;
  34499. begin
  34500. StartProgram(false);
  34501. Add([
  34502. 'type',
  34503. ' TObject = class',
  34504. ' end;',
  34505. ' TBird = class',
  34506. ' function GetSize: word; async;',
  34507. ' property Size: word read GetSize;',
  34508. ' end;',
  34509. 'function TBird.GetSize: word; async;',
  34510. 'begin',
  34511. 'end;',
  34512. 'begin',
  34513. '']);
  34514. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  34515. ConvertProgram;
  34516. end;
  34517. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  34518. begin
  34519. StartProgram(false);
  34520. Add([
  34521. 'procedure Run; async;',
  34522. 'begin',
  34523. ' await(word,1);',
  34524. 'end;',
  34525. 'begin',
  34526. '']);
  34527. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  34528. ConvertProgram;
  34529. end;
  34530. procedure TTestModule.TestAwait_AsyncCallTypeMismatch;
  34531. begin
  34532. StartProgram(false);
  34533. Add([
  34534. 'type',
  34535. ' TObject = class',
  34536. ' end;',
  34537. ' TBird = class',
  34538. ' end;',
  34539. 'function Fly: TObject; async;',
  34540. 'begin',
  34541. 'end;',
  34542. 'procedure Run; async;',
  34543. 'begin',
  34544. ' await(TBird,Fly);',
  34545. 'end;',
  34546. 'begin',
  34547. '']);
  34548. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "TObject", expected "TBird"',nIncompatibleTypeArgNo);
  34549. ConvertProgram;
  34550. end;
  34551. procedure TTestModule.TestAWait_OutsideAsyncFail;
  34552. begin
  34553. StartProgram(false);
  34554. Add([
  34555. 'procedure Crawl(w: double); ',
  34556. 'begin',
  34557. 'end;',
  34558. 'procedure Run(w: double);',
  34559. 'begin',
  34560. ' await(Crawl(w));',
  34561. 'end;',
  34562. 'begin',
  34563. ' Run(1);']);
  34564. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  34565. ConvertProgram;
  34566. end;
  34567. procedure TTestModule.TestAWait_IntegerFail;
  34568. begin
  34569. StartProgram(false);
  34570. Add([
  34571. 'function Run: word;',
  34572. 'begin',
  34573. 'end;',
  34574. 'procedure Fly(w: word); async;',
  34575. 'begin',
  34576. ' await(Run());',
  34577. 'end;',
  34578. 'begin',
  34579. ' Fly(1);']);
  34580. SetExpectedPasResolverError('async function or promise expected, but Result:Word found',nXExpectedButYFound);
  34581. ConvertProgram;
  34582. end;
  34583. procedure TTestModule.TestAWait_ExternalClassPromise;
  34584. begin
  34585. StartProgram(false);
  34586. Add([
  34587. '{$modeswitch externalclass}',
  34588. 'type',
  34589. ' TJSPromise = class external name ''Promise''',
  34590. ' end;',
  34591. ' TJSThenable = class external name ''Thenable''',
  34592. ' end;',
  34593. 'function Fly(w: word): TJSPromise;',
  34594. 'begin',
  34595. 'end;',
  34596. 'function Jump(w: word): word; async;',
  34597. 'begin',
  34598. 'end;',
  34599. 'function Eat(w: word): TJSPromise; async;',
  34600. 'begin',
  34601. 'end;',
  34602. 'function Run(d: double): word; async;',
  34603. 'var',
  34604. ' p: TJSPromise;',
  34605. 'begin',
  34606. ' Result:=await(word,p);', // promise needs type
  34607. ' Result:=await(word,Fly(3));', // promise needs type
  34608. ' Result:=await(Jump(4));', // async non promise must omit the type
  34609. ' Result:=await(word,Jump(5));', // async call can provide fitting type
  34610. ' Result:=await(word,Eat(6));', // promise needs type
  34611. 'end;',
  34612. 'begin',
  34613. '']);
  34614. ConvertProgram;
  34615. CheckSource('TestAWait_ExternalClassPromise',
  34616. LinesToStr([ // statements
  34617. 'this.Fly = function (w) {',
  34618. ' var Result = null;',
  34619. ' return Result;',
  34620. '};',
  34621. 'this.Jump = async function (w) {',
  34622. ' var Result = 0;',
  34623. ' return Result;',
  34624. '};',
  34625. 'this.Eat = async function (w) {',
  34626. ' var Result = null;',
  34627. ' return Result;',
  34628. '};',
  34629. 'this.Run = async function (d) {',
  34630. ' var Result = 0;',
  34631. ' var p = null;',
  34632. ' Result = await p;',
  34633. ' Result = await $mod.Fly(3);',
  34634. ' Result = await $mod.Jump(4);',
  34635. ' Result = await $mod.Jump(5);',
  34636. ' Result = await $mod.Eat(6);',
  34637. ' return Result;',
  34638. '};',
  34639. '']),
  34640. LinesToStr([
  34641. ]));
  34642. CheckResolverUnexpectedHints();
  34643. end;
  34644. procedure TTestModule.TestAWait_JSValue;
  34645. begin
  34646. StartProgram(false);
  34647. Add([
  34648. '{$modeswitch externalclass}',
  34649. 'type',
  34650. ' TJSPromise = class external name ''Promise''',
  34651. ' end;',
  34652. 'function Fly(w: word): jsvalue; async;',
  34653. 'begin',
  34654. 'end;',
  34655. 'function Run(d: jsvalue; var e): word; async;',
  34656. 'begin',
  34657. ' Result:=await(word,d);', // promise needs type
  34658. ' d:=await(Fly(4));', // async non promise must omit the type
  34659. ' Result:=await(word,e);', // promise needs type
  34660. 'end;',
  34661. 'begin',
  34662. '']);
  34663. ConvertProgram;
  34664. CheckSource('TestAWait_JSValue',
  34665. LinesToStr([ // statements
  34666. 'this.Fly = async function (w) {',
  34667. ' var Result = undefined;',
  34668. ' return Result;',
  34669. '};',
  34670. 'this.Run = async function (d, e) {',
  34671. ' var Result = 0;',
  34672. ' Result = await d;',
  34673. ' d = await $mod.Fly(4);',
  34674. ' Result = await e.get();',
  34675. ' return Result;',
  34676. '};',
  34677. '']),
  34678. LinesToStr([
  34679. ]));
  34680. CheckResolverUnexpectedHints();
  34681. end;
  34682. procedure TTestModule.TestAWait_Result;
  34683. begin
  34684. StartProgram(false);
  34685. Add([
  34686. '{$modeswitch externalclass}',
  34687. 'type',
  34688. ' TJSPromise = class external name ''Promise''',
  34689. ' end;',
  34690. 'function Crawl(d: double = 1.3): TJSPromise; ',
  34691. 'begin',
  34692. 'end;',
  34693. 'function Run(d: double = 1.6): word; async;',
  34694. 'begin',
  34695. ' Result:=await(word,Crawl);',
  34696. ' Result:=await(word,Crawl(4.5));',
  34697. ' Result:=await(Run);',
  34698. ' Result:=await(Run(6.7));',
  34699. 'end;',
  34700. 'begin',
  34701. ' Run(1);']);
  34702. ConvertProgram;
  34703. CheckSource('TestAWait_Result',
  34704. LinesToStr([ // statements
  34705. 'this.Crawl = function (d) {',
  34706. ' var Result = null;',
  34707. ' return Result;',
  34708. '};',
  34709. 'this.Run = async function (d) {',
  34710. ' var Result = 0;',
  34711. ' Result = await $mod.Crawl(1.3);',
  34712. ' Result = await $mod.Crawl(4.5);',
  34713. ' Result = await $mod.Run(1.6);',
  34714. ' Result = await $mod.Run(6.7);',
  34715. ' return Result;',
  34716. '};',
  34717. '']),
  34718. LinesToStr([
  34719. '$mod.Run(1);'
  34720. ]));
  34721. CheckResolverUnexpectedHints();
  34722. end;
  34723. procedure TTestModule.TestAWait_ResultPromiseMissingTypeFail;
  34724. begin
  34725. StartProgram(false);
  34726. Add([
  34727. '{$mode objfpc}',
  34728. '{$modeswitch externalclass}',
  34729. 'type',
  34730. ' TJSPromise = class external name ''Promise''',
  34731. ' end;',
  34732. 'function Run: TJSPromise; async;',
  34733. 'begin',
  34734. 'end;',
  34735. 'procedure Fly(w: word); async;',
  34736. 'begin',
  34737. ' await(Run());',
  34738. 'end;',
  34739. 'begin',
  34740. ' Fly(1);']);
  34741. SetExpectedPasResolverError('Wrong number of parameters specified for call to "function await(aType,TJSPromise):aType"',
  34742. nWrongNumberOfParametersForCallTo);
  34743. ConvertProgram;
  34744. end;
  34745. procedure TTestModule.TestAsync_AnonymousProc;
  34746. begin
  34747. StartProgram(false);
  34748. Add([
  34749. '{$mode objfpc}',
  34750. '{$modeswitch externalclass}',
  34751. 'type',
  34752. ' TJSPromise = class external name ''Promise''',
  34753. ' end;',
  34754. 'type',
  34755. ' TFunc = reference to function(x: double): word; async;',
  34756. 'function Crawl(d: double = 1.3): word; async;',
  34757. 'begin',
  34758. 'end;',
  34759. 'var Func: TFunc;',
  34760. 'begin',
  34761. ' Func:=function(c:double):word async begin',
  34762. ' Result:=await(Crawl(c));',
  34763. ' end;',
  34764. ' Func:=function(c:double):word async assembler asm',
  34765. ' end;',
  34766. '']);
  34767. ConvertProgram;
  34768. CheckSource('TestAsync_AnonymousProc',
  34769. LinesToStr([ // statements
  34770. 'this.Crawl = async function (d) {',
  34771. ' var Result = 0;',
  34772. ' return Result;',
  34773. '};',
  34774. 'this.Func = null;',
  34775. '']),
  34776. LinesToStr([
  34777. '$mod.Func = async function (c) {',
  34778. ' var Result = 0;',
  34779. ' Result = await $mod.Crawl(c);',
  34780. ' return Result;',
  34781. '};',
  34782. '$mod.Func = async function (c) {',
  34783. '};',
  34784. '']));
  34785. CheckResolverUnexpectedHints();
  34786. end;
  34787. procedure TTestModule.TestAsync_AnonymousProc_PromiseViaDotContext;
  34788. begin
  34789. StartProgram(false);
  34790. Add([
  34791. '{$mode objfpc}',
  34792. '{$modeswitch externalclass}',
  34793. 'type',
  34794. ' TJSPromise = class external name ''Promise''',
  34795. ' end;',
  34796. ' TObject = class',
  34797. ' public',
  34798. ' procedure Fly(Prom: TJSPromise);',
  34799. ' end;',
  34800. ' TFunc = reference to procedure(Bird: TObject);',
  34801. 'procedure TObject.Fly(Prom: TJSPromise);',
  34802. 'begin',
  34803. 'end;',
  34804. 'function Crawl: jsvalue; async;',
  34805. 'begin',
  34806. 'end;',
  34807. 'procedure Add(Func: TFunc);',
  34808. 'begin',
  34809. 'end;',
  34810. 'begin',
  34811. ' Add(procedure(Bird: TObject)',
  34812. ' begin',
  34813. ' Bird.Fly(Crawl());',
  34814. ' end);',
  34815. '']);
  34816. ConvertProgram;
  34817. CheckSource('TestAsync_AnonymousProc_PromiseViaDotContext',
  34818. LinesToStr([ // statements
  34819. 'rtl.createClass(this, "TObject", null, function () {',
  34820. ' this.$init = function () {',
  34821. ' };',
  34822. ' this.$final = function () {',
  34823. ' };',
  34824. ' this.Fly = function (Prom) {',
  34825. ' };',
  34826. '});',
  34827. 'this.Crawl = async function () {',
  34828. ' var Result = undefined;',
  34829. ' return Result;',
  34830. '};',
  34831. 'this.Add = function (Func) {',
  34832. '};',
  34833. '']),
  34834. LinesToStr([
  34835. '$mod.Add(function (Bird) {',
  34836. ' Bird.Fly($mod.Crawl());',
  34837. '});',
  34838. '']));
  34839. end;
  34840. procedure TTestModule.TestAsync_ProcType;
  34841. begin
  34842. StartProgram(false);
  34843. Add([
  34844. '{$mode objfpc}',
  34845. 'type',
  34846. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  34847. ' TFunc = function(x: double = 1.1): word; async;',
  34848. ' TProc = procedure(x: longint = 7); async;',
  34849. 'function Crawl(d: double): word; async;',
  34850. 'begin',
  34851. 'end;',
  34852. 'procedure Run(e:longint); async;',
  34853. 'begin',
  34854. 'end;',
  34855. 'procedure Fly(p: TProc); async;',
  34856. 'begin',
  34857. ' await(p);',
  34858. ' await(p());',
  34859. 'end;',
  34860. 'var',
  34861. ' RefFunc: TRefFunc;',
  34862. ' Func: TFunc;',
  34863. ' Proc, ProcB: TProc;',
  34864. 'begin',
  34865. ' Func:=@Crawl;',
  34866. ' RefFunc:=@Crawl;',
  34867. ' RefFunc:=function(c:double):word async begin',
  34868. ' Result:=await(RefFunc);',
  34869. ' Result:=await(RefFunc());',
  34870. ' Result:=await(Func);',
  34871. ' Result:=await(Func());',
  34872. ' await(Proc);',
  34873. ' await(Proc());',
  34874. ' await(Proc(13));',
  34875. ' end;',
  34876. ' Proc:=@Run;',
  34877. ' if Proc=ProcB then ;',
  34878. ' ']);
  34879. ConvertProgram;
  34880. CheckResolverUnexpectedHints();
  34881. CheckSource('TestAsync_ProcType',
  34882. LinesToStr([ // statements
  34883. 'this.Crawl = async function (d) {',
  34884. ' var Result = 0;',
  34885. ' return Result;',
  34886. '};',
  34887. 'this.Run = async function (e) {',
  34888. '};',
  34889. 'this.Fly = async function (p) {',
  34890. ' await p(7);',
  34891. ' await p(7);',
  34892. '};',
  34893. 'this.RefFunc = null;',
  34894. 'this.Func = null;',
  34895. 'this.Proc = null;',
  34896. 'this.ProcB = null;',
  34897. '']),
  34898. LinesToStr([
  34899. '$mod.Func = $mod.Crawl;',
  34900. '$mod.RefFunc = $mod.Crawl;',
  34901. '$mod.RefFunc = async function (c) {',
  34902. ' var Result = 0;',
  34903. ' Result = await $mod.RefFunc(1.3);',
  34904. ' Result = await $mod.RefFunc(1.3);',
  34905. ' Result = await $mod.Func(1.1);',
  34906. ' Result = await $mod.Func(1.1);',
  34907. ' await $mod.Proc(7);',
  34908. ' await $mod.Proc(7);',
  34909. ' await $mod.Proc(13);',
  34910. ' return Result;',
  34911. '};',
  34912. '$mod.Proc = $mod.Run;',
  34913. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  34914. '']));
  34915. end;
  34916. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  34917. begin
  34918. StartProgram(false);
  34919. Add([
  34920. '{$mode objfpc}',
  34921. 'type',
  34922. ' TRefFunc = reference to function(x: double = 1.3): word;',
  34923. 'function Crawl(d: double): word; async;',
  34924. 'begin',
  34925. 'end;',
  34926. 'var',
  34927. ' RefFunc: TRefFunc;',
  34928. 'begin',
  34929. ' RefFunc:=@Crawl;',
  34930. ' ']);
  34931. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  34932. ConvertProgram;
  34933. end;
  34934. procedure TTestModule.TestAsync_Inherited;
  34935. begin
  34936. StartProgram(false);
  34937. Add([
  34938. '{$mode objfpc}',
  34939. '{$modeswitch externalclass}',
  34940. 'type',
  34941. ' TJSPromise = class external name ''Promise''',
  34942. ' end;',
  34943. ' TObject = class',
  34944. ' function Run(w: word = 3): word; async; virtual;',
  34945. ' end;',
  34946. ' TBird = class',
  34947. ' function Run(w: word = 3): word; async; override;',
  34948. ' end;',
  34949. 'function TObject.Run(w: word = 3): word; async;',
  34950. 'begin',
  34951. 'end;',
  34952. 'function TBird.Run(w: word = 3): word;', // async modifier not needed in impl
  34953. 'var p: TJSPromise;',
  34954. 'begin',
  34955. ' p:=inherited;',
  34956. ' p:=inherited Run;',
  34957. ' p:=inherited Run();',
  34958. ' p:=inherited Run(4);',
  34959. ' exit(p);',
  34960. ' exit(inherited);',
  34961. ' exit(inherited Run);',
  34962. ' exit(inherited Run(5));',
  34963. ' exit(6);',
  34964. 'end;',
  34965. 'begin',
  34966. ' ']);
  34967. ConvertProgram;
  34968. CheckSource('TestAsync_Inherited',
  34969. LinesToStr([ // statements
  34970. 'rtl.createClass(this, "TObject", null, function () {',
  34971. ' this.$init = function () {',
  34972. ' };',
  34973. ' this.$final = function () {',
  34974. ' };',
  34975. ' this.Run = async function (w) {',
  34976. ' var Result = 0;',
  34977. ' return Result;',
  34978. ' };',
  34979. '});',
  34980. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34981. ' this.Run = async function (w) {',
  34982. ' var Result = 0;',
  34983. ' var p = null;',
  34984. ' p = $mod.TObject.Run.apply(this, arguments);',
  34985. ' p = $mod.TObject.Run.call(this, 3);',
  34986. ' p = $mod.TObject.Run.call(this, 3);',
  34987. ' p = $mod.TObject.Run.call(this, 4);',
  34988. ' return p;',
  34989. ' return $mod.TObject.Run.apply(this, arguments);',
  34990. ' return $mod.TObject.Run.call(this, 3);',
  34991. ' return $mod.TObject.Run.call(this, 5);',
  34992. ' return 6;',
  34993. ' return Result;',
  34994. ' };',
  34995. '});',
  34996. '']),
  34997. LinesToStr([
  34998. '']));
  34999. CheckResolverUnexpectedHints();
  35000. end;
  35001. procedure TTestModule.TestAsync_ClassInterface;
  35002. begin
  35003. StartProgram(false);
  35004. Add([
  35005. '{$mode objfpc}',
  35006. '{$modeswitch externalclass}',
  35007. 'type',
  35008. ' TJSPromise = class external name ''Promise''',
  35009. ' end;',
  35010. ' IUnknown = interface',
  35011. ' function _AddRef: longint;',
  35012. ' function _Release: longint;',
  35013. ' end;',
  35014. 'function Say(i: IUnknown): IUnknown; async;',
  35015. 'begin',
  35016. 'end;',
  35017. 'function Run: IUnknown; async;',
  35018. 'begin',
  35019. ' Result:=await(Run);',
  35020. ' Result:=await(Run());',
  35021. ' Result:=await(Run) as IUnknown;',
  35022. ' Result:=await(Say(nil));',
  35023. ' Result:=await(Say(await(Run())));',
  35024. ' Result:=await(Say(await(Run()) as IUnknown));',
  35025. ' Result:=await(Say(await(Run()) as IUnknown)) as IUnknown;',
  35026. 'end;',
  35027. 'procedure Fly;',
  35028. 'var p: TJSPromise;',
  35029. 'begin',
  35030. ' Run;',
  35031. ' Run();',
  35032. ' p:=Run;',
  35033. ' p:=Run();',
  35034. 'end;',
  35035. 'begin',
  35036. ' ']);
  35037. ConvertProgram;
  35038. CheckSource('TestAsync_ClassInterface',
  35039. LinesToStr([ // statements
  35040. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  35041. 'this.Say = async function (i) {',
  35042. ' var Result = null;',
  35043. ' return Result;',
  35044. '};',
  35045. 'this.Run = async function () {',
  35046. ' var Result = null;',
  35047. ' var $ok = false;',
  35048. ' try {',
  35049. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  35050. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  35051. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown));',
  35052. ' Result = rtl.setIntfL(Result, await $mod.Say(null));',
  35053. ' Result = rtl.setIntfL(Result, await $mod.Say(await $mod.Run()));',
  35054. ' Result = rtl.setIntfL(Result, await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)));',
  35055. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)), $mod.IUnknown));',
  35056. ' $ok = true;',
  35057. ' } finally {',
  35058. ' if (!$ok) rtl._Release(Result);',
  35059. ' };',
  35060. ' return Result;',
  35061. '};',
  35062. 'this.Fly = function () {',
  35063. ' var p = null;',
  35064. ' $mod.Run();',
  35065. ' $mod.Run();',
  35066. ' p = $mod.Run();',
  35067. ' p = $mod.Run();',
  35068. '};',
  35069. '']),
  35070. LinesToStr([
  35071. '']));
  35072. CheckResolverUnexpectedHints();
  35073. end;
  35074. procedure TTestModule.TestAsync_ClassInterface_AsyncMissmatchFail;
  35075. begin
  35076. StartProgram(true,[supTInterfacedObject]);
  35077. Add([
  35078. '{$mode objfpc}',
  35079. '{$modeswitch externalclass}',
  35080. 'type',
  35081. ' TJSPromise = class external name ''Promise''',
  35082. ' end;',
  35083. ' IBird = interface',
  35084. ' procedure Run;',
  35085. ' end;',
  35086. ' TBird = class(TInterfacedObject,IBird)',
  35087. ' procedure Run; async;',
  35088. ' end;',
  35089. 'procedure TBird.Run;',
  35090. 'begin',
  35091. 'end;',
  35092. 'begin',
  35093. ' ']);
  35094. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  35095. ConvertProgram;
  35096. end;
  35097. procedure TTestModule.TestAWait_ClassAs;
  35098. begin
  35099. StartProgram(false);
  35100. Add([
  35101. '{$mode objfpc}',
  35102. '{$modeswitch externalclass}',
  35103. 'type',
  35104. ' TJSPromise = class external name ''Promise''',
  35105. ' end;',
  35106. ' TObject = class',
  35107. ' function Run: TObject; async;',
  35108. ' end;',
  35109. ' TBird = class',
  35110. ' function Fly: TBird; async;',
  35111. ' end;',
  35112. 'function TObject.Run: TObject; async;',
  35113. 'begin',
  35114. 'end;',
  35115. 'function TBird.Fly: TBird;', // async modifier not needed in impl
  35116. 'var o: TObject;',
  35117. 'begin',
  35118. ' o:=await(TObject,Run);',
  35119. ' o:=await(TObject,Fly);',
  35120. ' o:=await(TBird,Fly);',
  35121. ' o:=await(TObject,inherited Run);',
  35122. ' o:=await(TObject,inherited Run) as TBird;',
  35123. 'end;',
  35124. 'begin',
  35125. ' ']);
  35126. ConvertProgram;
  35127. CheckSource('TestAWait_ClassAs',
  35128. LinesToStr([ // statements
  35129. 'rtl.createClass(this, "TObject", null, function () {',
  35130. ' this.$init = function () {',
  35131. ' };',
  35132. ' this.$final = function () {',
  35133. ' };',
  35134. ' this.Run = async function () {',
  35135. ' var Result = null;',
  35136. ' return Result;',
  35137. ' };',
  35138. '});',
  35139. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  35140. ' this.Fly = async function () {',
  35141. ' var Result = null;',
  35142. ' var o = null;',
  35143. ' o = await this.Run();',
  35144. ' o = await this.Fly();',
  35145. ' o = await this.Fly();',
  35146. ' o = await $mod.TObject.Run.call(this);',
  35147. ' o = rtl.as(await $mod.TObject.Run.call(this), $mod.TBird);',
  35148. ' return Result;',
  35149. ' };',
  35150. '});',
  35151. '']),
  35152. LinesToStr([
  35153. '']));
  35154. CheckResolverUnexpectedHints();
  35155. end;
  35156. procedure TTestModule.TestLibrary_Empty;
  35157. begin
  35158. StartLibrary(false);
  35159. Add([
  35160. '']);
  35161. ConvertLibrary;
  35162. CheckFullSource('TestLibrary_Empty',
  35163. LinesToStr([ // statements
  35164. 'rtl.module("library", [], function () {',
  35165. ' var $mod = this;',
  35166. ' $mod.$main = function () {',
  35167. ' };',
  35168. '});',
  35169. 'rtl.run("library");',
  35170. '']));
  35171. CheckResolverUnexpectedHints();
  35172. end;
  35173. procedure TTestModule.TestLibrary_ExportFunc;
  35174. begin
  35175. StartLibrary(false);
  35176. Add([
  35177. 'procedure Run(w: word);',
  35178. 'begin',
  35179. 'end;',
  35180. 'exports',
  35181. ' Run;',
  35182. ' run name ''Foo'';',
  35183. ' test1.run name ''Test1Run'';',
  35184. '']);
  35185. ConvertLibrary;
  35186. CheckFullSource('TestLibrary_ExportFunc',
  35187. LinesToStr([ // statements
  35188. 'rtl.module("library", [], function () {',
  35189. ' var $mod = this;',
  35190. ' this.Run = function (w) {',
  35191. ' };',
  35192. ' $mod.$main = function () {',
  35193. ' };',
  35194. '});',
  35195. 'rtl.run("library");',
  35196. 'export const Run = pas.library.Run;',
  35197. 'export const Foo = pas.library.Run;',
  35198. 'export const Test1Run = pas.library.Run;',
  35199. '']));
  35200. CheckResolverUnexpectedHints();
  35201. end;
  35202. procedure TTestModule.TestLibrary_ExportFuncOverloadedFail;
  35203. begin
  35204. StartLibrary(false);
  35205. Add([
  35206. 'procedure Run(w: word); overload;',
  35207. 'begin',
  35208. 'end;',
  35209. 'procedure Run(s: string); overload;',
  35210. 'begin',
  35211. 'end;',
  35212. 'exports',
  35213. ' Run;',
  35214. '']);
  35215. SetExpectedPasResolverError(sCantDetermineWhichOverloadedFunctionToCall,
  35216. nCantDetermineWhichOverloadedFunctionToCall);
  35217. ConvertLibrary;
  35218. end;
  35219. procedure TTestModule.TestLibrary_Export_Index_Fail;
  35220. begin
  35221. StartLibrary(false);
  35222. Add([
  35223. 'procedure Run(w: word);',
  35224. 'begin',
  35225. 'end;',
  35226. 'exports',
  35227. ' Run index 3;',
  35228. '']);
  35229. SetExpectedPasResolverError('Not supported: export index',nNotSupportedX);
  35230. ConvertLibrary;
  35231. end;
  35232. procedure TTestModule.TestLibrary_ExportVar;
  35233. begin
  35234. StartLibrary(false);
  35235. Add([
  35236. 'var Wing: word;',
  35237. 'exports',
  35238. ' Wing, wing name ''BirdArm'';',
  35239. '']);
  35240. ConvertLibrary;
  35241. CheckFullSource('TestLibrary_ExportVar',
  35242. LinesToStr([ // statements
  35243. 'rtl.module("library", [], function () {',
  35244. ' var $mod = this;',
  35245. ' this.Wing = 0;',
  35246. ' $mod.$main = function () {',
  35247. ' };',
  35248. '});',
  35249. 'rtl.run("library");',
  35250. 'export const vars = {};',
  35251. 'Object.defineProperties(vars, {',
  35252. ' Wing: {',
  35253. ' enumerable: true,',
  35254. ' get: function () {',
  35255. ' return pas.library.Wing;',
  35256. ' },',
  35257. ' set: function (v) {',
  35258. ' pas.library.Wing = v;',
  35259. ' }',
  35260. ' },',
  35261. ' BirdArm: {',
  35262. ' enumerable: true,',
  35263. ' get: function () {',
  35264. ' return pas.library.Wing;',
  35265. ' },',
  35266. ' set: function (v) {',
  35267. ' pas.library.Wing = v;',
  35268. ' }',
  35269. ' }',
  35270. '});',
  35271. '']));
  35272. CheckResolverUnexpectedHints();
  35273. end;
  35274. procedure TTestModule.TestLibrary_ExportUnitFunc;
  35275. begin
  35276. AddModuleWithIntfImplSrc('Unit1.pas',
  35277. LinesToStr([
  35278. 'type',
  35279. ' TAnt = class',
  35280. ' class function Crawl: word; static;',
  35281. ' end;',
  35282. 'function Fly: word;',
  35283. '']),
  35284. LinesToStr([
  35285. 'function Fly: word;',
  35286. 'begin',
  35287. 'end;',
  35288. 'class function TAnt.Crawl: word;',
  35289. 'begin',
  35290. 'end;',
  35291. '']));
  35292. StartLibrary(true,[supTObject]);
  35293. Add([
  35294. 'uses unit1;',
  35295. 'exports',
  35296. ' Fly;',
  35297. ' TAnt.Crawl;',
  35298. '']);
  35299. ConvertLibrary;
  35300. CheckFullSource('TestLibrary_ExportUnitFunc',
  35301. LinesToStr([ // statements
  35302. 'rtl.module("library", ["system", "Unit1"], function () {',
  35303. ' var $mod = this;',
  35304. ' $mod.$main = function () {',
  35305. ' };',
  35306. '});',
  35307. 'rtl.run("library");',
  35308. 'export const Fly = pas.Unit1.Fly;',
  35309. 'export const Crawl = pas.Unit1.TAnt.Crawl;',
  35310. '']));
  35311. CheckResolverUnexpectedHints();
  35312. end;
  35313. Initialization
  35314. RegisterTests([TTestModule]);
  35315. end.