tcmodules.pas 653 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2018 by Michael Van Canneyt
  4. Unit tests for Pascal-to-Javascript converter class.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************
  11. Examples:
  12. ./testpas2js --suite=TTestModule.TestEmptyProgram
  13. ./testpas2js --suite=TTestModule.TestEmptyUnit
  14. }
  15. unit TCModules;
  16. {$mode objfpc}{$H+}
  17. interface
  18. uses
  19. Classes, SysUtils, fpcunit, testregistry, contnrs,
  20. jstree, jswriter, jsbase,
  21. PasTree, PScanner, PasResolver, PParser, PasResolveEval,
  22. FPPas2Js;
  23. const
  24. // default parser+scanner options
  25. po_tcmodules = po_Pas2js+[po_KeepScannerError];
  26. co_tcmodules = [coNoTypeInfo];
  27. type
  28. TSrcMarkerKind = (
  29. mkLabel,
  30. mkResolverReference,
  31. mkDirectReference
  32. );
  33. PSrcMarker = ^TSrcMarker;
  34. TSrcMarker = record
  35. Kind: TSrcMarkerKind;
  36. Filename: string;
  37. Row: integer;
  38. StartCol, EndCol: integer; // token start, end column
  39. Identifier: string;
  40. Next: PSrcMarker;
  41. end;
  42. { TTestHintMessage }
  43. TTestHintMessage = class
  44. public
  45. Id: int64;
  46. MsgType: TMessageType;
  47. MsgNumber: integer;
  48. Msg: string;
  49. SourcePos: TPasSourcePos;
  50. end;
  51. { TTestPasParser }
  52. TTestPasParser = Class(TPasParser)
  53. end;
  54. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  55. { TTestEnginePasResolver }
  56. TTestEnginePasResolver = class(TPas2JsResolver)
  57. private
  58. FFilename: string;
  59. FModule: TPasModule;
  60. FOnFindUnit: TOnFindUnit;
  61. FParser: TTestPasParser;
  62. FStreamResolver: TStreamResolver;
  63. FScanner: TPas2jsPasScanner;
  64. FSource: string;
  65. public
  66. destructor Destroy; override;
  67. function FindUnit(const AName, InFilename: String; NameExpr,
  68. InFileExpr: TPasExpr): TPasModule; override;
  69. procedure UsedInterfacesFinished(Section: TPasSection); override;
  70. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  71. property Filename: string read FFilename write FFilename;
  72. property StreamResolver: TStreamResolver read FStreamResolver write FStreamResolver;
  73. property Scanner: TPas2jsPasScanner read FScanner write FScanner;
  74. property Parser: TTestPasParser read FParser write FParser;
  75. property Source: string read FSource write FSource;
  76. property Module: TPasModule read FModule;
  77. end;
  78. { TCustomTestModule }
  79. TCustomTestModule = Class(TTestCase)
  80. private
  81. FConverter: TPasToJSConverter;
  82. FEngine: TTestEnginePasResolver;
  83. FExpectedErrorClass: ExceptClass;
  84. FExpectedErrorMsg: string;
  85. FExpectedErrorNumber: integer;
  86. FFilename: string;
  87. FFileResolver: TStreamResolver;
  88. FJSImplementationSrc: TJSSourceElements;
  89. FJSImplementationUses: TJSArrayLiteral;
  90. FJSInitBody: TJSFunctionBody;
  91. FJSImplentationUses: TJSArrayLiteral;
  92. FJSInterfaceUses: TJSArrayLiteral;
  93. FJSModule: TJSSourceElements;
  94. FJSModuleSrc: TJSSourceElements;
  95. FJSSource: TStringList;
  96. FModule: TPasModule;
  97. FJSModuleCallArgs: TJSArguments;
  98. FModules: TObjectList;// list of TTestEnginePasResolver
  99. FParser: TTestPasParser;
  100. FPasProgram: TPasProgram;
  101. FHintMsgs: TObjectList; // list of TTestHintMessage
  102. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  103. FJSRegModuleCall: TJSCallExpression;
  104. FScanner: TPas2jsPasScanner;
  105. FSkipTests: boolean;
  106. FSource: TStringList;
  107. FFirstPasStatement: TPasImplBlock;
  108. {$IFDEF EnablePasTreeGlobalRefCount}
  109. FElementRefCountAtSetup: int64;
  110. {$ENDIF}
  111. function GetMsgCount: integer;
  112. function GetMsgs(Index: integer): TTestHintMessage;
  113. function GetResolverCount: integer;
  114. function GetResolvers(Index: integer): TTestEnginePasResolver;
  115. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  116. procedure OnParserLog(Sender: TObject; const Msg: String);
  117. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  118. procedure OnScannerLog(Sender: TObject; const Msg: String);
  119. protected
  120. procedure SetUp; override;
  121. function CreateConverter: TPasToJSConverter; virtual;
  122. function LoadUnit(const aUnitName: String): TPasModule;
  123. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  124. procedure TearDown; override;
  125. Procedure Add(Line: string); virtual;
  126. Procedure Add(const Lines: array of string);
  127. Procedure StartParsing; virtual;
  128. procedure ParseModuleQueue; virtual;
  129. procedure ParseModule; virtual;
  130. procedure ParseProgram; virtual;
  131. procedure ParseUnit; virtual;
  132. protected
  133. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  134. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  135. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  136. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  137. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  138. procedure AddSystemUnit; virtual;
  139. procedure StartProgram(NeedSystemUnit: boolean); virtual;
  140. procedure StartUnit(NeedSystemUnit: boolean); virtual;
  141. procedure ConvertModule; virtual;
  142. procedure ConvertProgram; virtual;
  143. procedure ConvertUnit; virtual;
  144. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  145. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  146. function GetDottedIdentifier(El: TJSElement): string;
  147. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  148. ImplStatements: string = ''); virtual;
  149. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  150. procedure CheckUnit(Filename, ExpectedSrc: string); virtual;
  151. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  152. Msg: string; Marker: PSrcMarker = nil); virtual;
  153. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  154. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  155. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  156. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  157. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  158. function IsErrorExpected(E: Exception): boolean;
  159. procedure HandleScannerError(E: EScannerError);
  160. procedure HandleParserError(E: EParserError);
  161. procedure HandlePasResolveError(E: EPasResolve);
  162. procedure HandlePas2JSError(E: EPas2JS);
  163. procedure HandleException(E: Exception);
  164. procedure FailException(E: Exception);
  165. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  166. function IndexOfResolver(const Filename: string): integer;
  167. function GetResolver(const Filename: string): TTestEnginePasResolver;
  168. function GetDefaultNamespace: string;
  169. property PasProgram: TPasProgram Read FPasProgram;
  170. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  171. property ResolverCount: integer read GetResolverCount;
  172. property Engine: TTestEnginePasResolver read FEngine;
  173. property Filename: string read FFilename;
  174. Property Module: TPasModule Read FModule;
  175. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  176. property Converter: TPasToJSConverter read FConverter;
  177. property JSSource: TStringList read FJSSource;
  178. property JSModule: TJSSourceElements read FJSModule;
  179. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  180. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  181. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  182. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  183. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  184. property JSInitBody: TJSFunctionBody read FJSInitBody;
  185. property JSImplementationSrc: TJSSourceElements read FJSImplementationSrc;
  186. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  187. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  188. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  189. property SkipTests: boolean read FSkipTests write FSkipTests;
  190. public
  191. constructor Create; override;
  192. destructor Destroy; override;
  193. property Source: TStringList read FSource;
  194. property FileResolver: TStreamResolver read FFileResolver;
  195. property Scanner: TPas2jsPasScanner read FScanner;
  196. property Parser: TTestPasParser read FParser;
  197. property MsgCount: integer read GetMsgCount;
  198. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  199. end;
  200. { TTestModule }
  201. TTestModule = class(TCustomTestModule)
  202. Published
  203. Procedure TestReservedWords;
  204. // program/units
  205. Procedure TestEmptyProgram;
  206. Procedure TestEmptyProgramUseStrict;
  207. Procedure TestEmptyUnit;
  208. Procedure TestEmptyUnitUseStrict;
  209. Procedure TestDottedUnitNames;
  210. Procedure TestDottedUnitNameImpl;
  211. Procedure TestDottedUnitExpr;
  212. Procedure Test_ModeFPCFail;
  213. Procedure Test_ModeSwitchCBlocksFail;
  214. Procedure TestUnit_UseSystem;
  215. Procedure TestUnit_Intf1Impl2Intf1;
  216. Procedure TestIncludeVersion;
  217. // vars/const
  218. Procedure TestVarInt;
  219. Procedure TestVarBaseTypes;
  220. Procedure TestBaseTypeSingleFail;
  221. Procedure TestBaseTypeExtendedFail;
  222. Procedure TestConstBaseTypes;
  223. Procedure TestUnitImplVars;
  224. Procedure TestUnitImplConsts;
  225. Procedure TestUnitImplRecord;
  226. Procedure TestRenameJSNameConflict;
  227. Procedure TestLocalConst;
  228. Procedure TestVarExternal;
  229. Procedure TestVarExternalOtherUnit;
  230. Procedure TestVarAbsoluteFail;
  231. Procedure TestConstExternal;
  232. // numbers
  233. Procedure TestDouble;
  234. Procedure TestInteger;
  235. Procedure TestIntegerRange;
  236. Procedure TestIntegerTypecasts;
  237. Procedure TestBitwiseAndNativeIntWarn;
  238. Procedure TestCurrency;
  239. Procedure TestForBoolDo;
  240. Procedure TestForIntDo;
  241. Procedure TestForIntInDo;
  242. // strings
  243. Procedure TestCharConst;
  244. Procedure TestChar_Compare;
  245. Procedure TestChar_BuiltInProcs;
  246. Procedure TestStringConst;
  247. Procedure TestStringConstSurrogate;
  248. Procedure TestString_Length;
  249. Procedure TestString_Compare;
  250. Procedure TestString_SetLength;
  251. Procedure TestString_CharAt;
  252. Procedure TestStringHMinusFail;
  253. Procedure TestStr;
  254. Procedure TestBaseType_AnsiStringFail;
  255. Procedure TestBaseType_WideStringFail;
  256. Procedure TestBaseType_ShortStringFail;
  257. Procedure TestBaseType_RawByteStringFail;
  258. Procedure TestTypeShortstring_Fail;
  259. Procedure TestCharSet_Custom;
  260. Procedure TestForCharDo;
  261. Procedure TestForCharInDo;
  262. // alias types
  263. Procedure TestAliasTypeRef;
  264. Procedure TestTypeCast_BaseTypes;
  265. Procedure TestTypeCast_AliasBaseTypes;
  266. // functions
  267. Procedure TestEmptyProc;
  268. Procedure TestProcOneParam;
  269. Procedure TestFunctionWithoutParams;
  270. Procedure TestProcedureWithoutParams;
  271. Procedure TestPrgProcVar;
  272. Procedure TestProcTwoArgs;
  273. Procedure TestProc_DefaultValue;
  274. Procedure TestUnitProcVar;
  275. Procedure TestImplProc;
  276. Procedure TestFunctionResult;
  277. Procedure TestNestedProc;
  278. Procedure TestNestedProc_ResultString;
  279. Procedure TestForwardProc;
  280. Procedure TestNestedForwardProc;
  281. Procedure TestAssignFunctionResult;
  282. Procedure TestFunctionResultInCondition;
  283. Procedure TestFunctionResultInForLoop;
  284. Procedure TestFunctionResultInTypeCast;
  285. Procedure TestExit;
  286. Procedure TestBreak;
  287. Procedure TestBreakAsVar;
  288. Procedure TestContinue;
  289. Procedure TestProc_External;
  290. Procedure TestProc_ExternalOtherUnit;
  291. Procedure TestProc_Asm;
  292. Procedure TestProc_Assembler;
  293. Procedure TestProc_VarParam;
  294. Procedure TestProc_VarParamString;
  295. Procedure TestProc_VarParamV;
  296. Procedure TestProc_Overload;
  297. Procedure TestProc_OverloadForward;
  298. Procedure TestProc_OverloadIntfImpl;
  299. Procedure TestProc_OverloadNested;
  300. Procedure TestProc_OverloadUnitCycle;
  301. Procedure TestProc_Varargs;
  302. Procedure TestProc_ConstOrder;
  303. Procedure TestProc_DuplicateConst;
  304. Procedure TestProc_LocalVarAbsolute;
  305. Procedure TestProc_ReservedWords;
  306. // anonymous functions
  307. Procedure TestAnonymousProc_Assign_ObjFPC;
  308. Procedure TestAnonymousProc_Assign_Delphi;
  309. Procedure TestAnonymousProc_Arg;
  310. Procedure TestAnonymousProc_Typecast;
  311. Procedure TestAnonymousProc_With;
  312. Procedure TestAnonymousProc_ExceptOn;
  313. Procedure TestAnonymousProc_Nested;
  314. Procedure TestAnonymousProc_NestedAssignResult;
  315. // enums, sets
  316. Procedure TestEnum_Name;
  317. Procedure TestEnum_Number;
  318. Procedure TestEnum_ConstFail;
  319. Procedure TestEnum_Functions;
  320. Procedure TestEnum_AsParams;
  321. Procedure TestEnumRange_Array;
  322. Procedure TestEnum_ForIn;
  323. Procedure TestEnum_ScopedNumber;
  324. Procedure TestEnum_InFunction;
  325. Procedure TestSet_Enum;
  326. Procedure TestSet_Operators;
  327. Procedure TestSet_Operator_In;
  328. Procedure TestSet_Functions;
  329. Procedure TestSet_PassAsArgClone;
  330. Procedure TestSet_AsParams;
  331. Procedure TestSet_Property;
  332. Procedure TestSet_EnumConst;
  333. Procedure TestSet_IntConst;
  334. Procedure TestSet_AnonymousEnumType;
  335. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  336. Procedure TestSet_ConstEnum;
  337. Procedure TestSet_ConstChar;
  338. Procedure TestSet_ConstInt;
  339. Procedure TestSet_InFunction;
  340. Procedure TestSet_ForIn;
  341. // statements
  342. Procedure TestNestBegin;
  343. Procedure TestIncDec;
  344. Procedure TestLoHiFpcMode;
  345. Procedure TestLoHiDelphiMode;
  346. Procedure TestAssignments;
  347. Procedure TestArithmeticOperators1;
  348. Procedure TestLogicalOperators;
  349. Procedure TestBitwiseOperators;
  350. Procedure TestFunctionInt;
  351. Procedure TestFunctionString;
  352. Procedure TestIfThen;
  353. Procedure TestForLoop;
  354. Procedure TestForLoopInsideFunction;
  355. Procedure TestForLoop_ReadVarAfter;
  356. Procedure TestForLoop_Nested;
  357. Procedure TestRepeatUntil;
  358. Procedure TestAsmBlock;
  359. Procedure TestAsmPas_Impl; // ToDo
  360. Procedure TestTryFinally;
  361. Procedure TestTryExcept;
  362. Procedure TestTryExcept_ReservedWords;
  363. Procedure TestIfThenRaiseElse;
  364. Procedure TestCaseOf;
  365. Procedure TestCaseOf_UseSwitch;
  366. Procedure TestCaseOfNoElse;
  367. Procedure TestCaseOfNoElse_UseSwitch;
  368. Procedure TestCaseOfRange;
  369. Procedure TestCaseOfString;
  370. Procedure TestCaseOfChar;
  371. Procedure TestCaseOfExternalClassConst;
  372. Procedure TestDebugger;
  373. // arrays
  374. Procedure TestArray_Dynamic;
  375. Procedure TestArray_Dynamic_Nil;
  376. Procedure TestArray_DynMultiDimensional;
  377. Procedure TestArray_StaticInt;
  378. Procedure TestArray_StaticBool;
  379. Procedure TestArray_StaticChar;
  380. Procedure TestArray_StaticMultiDim;
  381. Procedure TestArray_StaticInFunction;
  382. Procedure TestArrayOfRecord;
  383. Procedure TestArray_StaticRecord;
  384. Procedure TestArrayOfSet;
  385. // call(set) literal and clone var
  386. // call([set]) literal and clone var
  387. Procedure TestArray_DynAsParam;
  388. Procedure TestArray_StaticAsParam;
  389. Procedure TestArrayElement_AsParams;
  390. Procedure TestArrayElementFromFuncResult_AsParams;
  391. Procedure TestArrayEnumTypeRange;
  392. Procedure TestArray_SetLengthOutArg;
  393. Procedure TestArray_SetLengthProperty;
  394. Procedure TestArray_SetLengthMultiDim;
  395. Procedure TestArray_OpenArrayOfString;
  396. Procedure TestArray_Concat;
  397. Procedure TestArray_Copy;
  398. Procedure TestArray_InsertDelete;
  399. Procedure TestArray_DynArrayConstObjFPC;
  400. Procedure TestArray_DynArrayConstDelphi;
  401. Procedure TestArray_ArrayLitAsParam;
  402. Procedure TestArray_ArrayLitMultiDimAsParam;
  403. Procedure TestArray_ArrayLitStaticAsParam;
  404. Procedure TestArray_ForInArrOfString;
  405. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  406. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  407. // record
  408. Procedure TestRecord_Empty;
  409. Procedure TestRecord_Var;
  410. Procedure TestRecord_VarExternal;
  411. Procedure TestRecord_WithDo;
  412. Procedure TestRecord_Assign;
  413. Procedure TestRecord_AsParams;
  414. Procedure TestRecordElement_AsParams;
  415. Procedure TestRecordElementFromFuncResult_AsParams;
  416. Procedure TestRecordElementFromWith_AsParams;
  417. Procedure TestRecord_Equal;
  418. Procedure TestRecord_TypeCastJSValueToRecord;
  419. Procedure TestRecord_VariantFail;
  420. Procedure TestRecord_FieldArray;
  421. Procedure TestRecord_Const;
  422. Procedure TestRecord_TypecastFail;
  423. Procedure TestRecord_InFunction;
  424. // ToDo: Procedure TestRecord_ExternalField;
  425. // ToDo: RTTI of local record
  426. // ToDo: pcu local record, name clash and rtti
  427. // advanced record
  428. Procedure TestAdvRecord_Function;
  429. Procedure TestAdvRecord_Property;
  430. Procedure TestAdvRecord_PropertyDefault;
  431. Procedure TestAdvRecord_Property_ClassMethod;
  432. Procedure TestAdvRecord_Const;
  433. Procedure TestAdvRecord_ExternalField;
  434. Procedure TestAdvRecord_SubRecord;
  435. Procedure TestAdvRecord_SubClass;
  436. Procedure TestAdvRecord_SubInterfaceFail;
  437. Procedure TestAdvRecord_Constructor;
  438. // ToDo: class constructor
  439. // classes
  440. Procedure TestClass_TObjectDefaultConstructor;
  441. Procedure TestClass_TObjectConstructorWithParams;
  442. Procedure TestClass_TObjectConstructorWithDefaultParam;
  443. Procedure TestClass_Var;
  444. Procedure TestClass_Method;
  445. Procedure TestClass_Implementation;
  446. Procedure TestClass_Inheritance;
  447. Procedure TestClass_TypeAlias;
  448. Procedure TestClass_AbstractMethod;
  449. Procedure TestClass_CallInherited_ProcNoParams;
  450. Procedure TestClass_CallInherited_WithParams;
  451. Procedure TestClasS_CallInheritedConstructor;
  452. Procedure TestClass_ClassVar_Assign;
  453. Procedure TestClass_CallClassMethod;
  454. Procedure TestClass_Property;
  455. Procedure TestClass_Property_ClassMethod;
  456. Procedure TestClass_Property_Indexed;
  457. Procedure TestClass_Property_IndexSpec;
  458. Procedure TestClass_PropertyOfTypeArray;
  459. Procedure TestClass_PropertyDefault;
  460. Procedure TestClass_PropertyDefault2;
  461. Procedure TestClass_PropertyOverride;
  462. Procedure TestClass_PropertyIncVisibility;
  463. Procedure TestClass_Assigned;
  464. Procedure TestClass_WithClassDoCreate;
  465. Procedure TestClass_WithClassInstDoProperty;
  466. Procedure TestClass_WithClassInstDoPropertyWithParams;
  467. Procedure TestClass_WithClassInstDoFunc;
  468. Procedure TestClass_TypeCast;
  469. Procedure TestClass_TypeCastUntypedParam;
  470. Procedure TestClass_Overloads;
  471. Procedure TestClass_OverloadsAncestor;
  472. Procedure TestClass_OverloadConstructor;
  473. Procedure TestClass_OverloadDelphiOverride;
  474. Procedure TestClass_ReintroducedVar;
  475. Procedure TestClass_RaiseDescendant;
  476. Procedure TestClass_ExternalMethod;
  477. Procedure TestClass_ExternalVirtualNameMismatchFail;
  478. Procedure TestClass_ExternalOverrideFail;
  479. Procedure TestClass_ExternalVar;
  480. Procedure TestClass_Const;
  481. Procedure TestClass_LocalVarSelfFail;
  482. Procedure TestClass_ArgSelfFail;
  483. Procedure TestClass_NestedProcSelf;
  484. Procedure TestClass_NestedProcSelf2;
  485. Procedure TestClass_NestedProcClassSelf;
  486. Procedure TestClass_NestedProcCallInherited;
  487. Procedure TestClass_TObjectFree;
  488. Procedure TestClass_TObjectFreeNewInstance;
  489. Procedure TestClass_TObjectFreeLowerCase;
  490. Procedure TestClass_TObjectFreeFunctionFail;
  491. Procedure TestClass_TObjectFreePropertyFail;
  492. Procedure TestClass_ForIn;
  493. // class of
  494. Procedure TestClassOf_Create;
  495. Procedure TestClassOf_Call;
  496. Procedure TestClassOf_Assign;
  497. Procedure TestClassOf_Is;
  498. Procedure TestClassOf_Compare;
  499. Procedure TestClassOf_ClassVar;
  500. Procedure TestClassOf_ClassMethod;
  501. Procedure TestClassOf_ClassProperty;
  502. Procedure TestClassOf_ClassMethodSelf;
  503. Procedure TestClassOf_TypeCast;
  504. Procedure TestClassOf_ImplicitFunctionCall;
  505. Procedure TestClassOf_Const;
  506. // nested class
  507. Procedure TestNestedClass_Alias;
  508. Procedure TestNestedClass_Record;
  509. Procedure TestNestedClass_Class;
  510. // external class
  511. Procedure TestExternalClass_Var;
  512. Procedure TestExternalClass_Const;
  513. Procedure TestExternalClass_Dollar;
  514. Procedure TestExternalClass_DuplicateVarFail;
  515. Procedure TestExternalClass_Method;
  516. Procedure TestExternalClass_ClassMethod;
  517. Procedure TestExternalClass_FunctionResultInTypeCast;
  518. Procedure TestExternalClass_NonExternalOverride;
  519. Procedure TestExternalClass_OverloadHint;
  520. Procedure TestExternalClass_SameNamePublishedProperty;
  521. Procedure TestExternalClass_Property;
  522. Procedure TestExternalClass_PropertyDate;
  523. Procedure TestExternalClass_ClassProperty;
  524. Procedure TestExternalClass_ClassOf;
  525. Procedure TestExternalClass_ClassOtherUnit;
  526. Procedure TestExternalClass_Is;
  527. Procedure TestExternalClass_As;
  528. Procedure TestExternalClass_DestructorFail;
  529. Procedure TestExternalClass_New;
  530. Procedure TestExternalClass_ClassOf_New;
  531. Procedure TestExternalClass_FuncClassOf_New;
  532. Procedure TestExternalClass_New_PasClassFail;
  533. Procedure TestExternalClass_New_PasClassBracketsFail;
  534. Procedure TestExternalClass_LocalConstSameName;
  535. Procedure TestExternalClass_ReintroduceOverload;
  536. Procedure TestExternalClass_Inherited;
  537. Procedure TestExternalClass_PascalAncestorFail;
  538. Procedure TestExternalClass_NewInstance;
  539. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  540. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  541. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  542. Procedure TestExternalClass_PascalProperty;
  543. Procedure TestExternalClass_TypeCastToRootClass;
  544. Procedure TestExternalClass_TypeCastToJSObject;
  545. Procedure TestExternalClass_TypeCastStringToExternalString;
  546. Procedure TestExternalClass_TypeCastToJSFunction;
  547. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  548. Procedure TestExternalClass_BracketAccessor;
  549. Procedure TestExternalClass_BracketAccessor_Call;
  550. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  551. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  552. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  553. Procedure TestExternalClass_BracketAccessor_MultiType;
  554. Procedure TestExternalClass_BracketAccessor_Index;
  555. Procedure TestExternalClass_ForInJSObject;
  556. Procedure TestExternalClass_ForInJSArray;
  557. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  558. // class interfaces
  559. Procedure TestClassInterface_Corba;
  560. Procedure TestClassInterface_ProcExternalFail;
  561. Procedure TestClassInterface_Overloads;
  562. Procedure TestClassInterface_DuplicateGUIInIntfListFail;
  563. Procedure TestClassInterface_DuplicateGUIInAncestorFail;
  564. Procedure TestClassInterface_AncestorImpl;
  565. Procedure TestClassInterface_ImplReintroduce;
  566. Procedure TestClassInterface_MethodResolution;
  567. Procedure TestClassInterface_AncestorMoreInterfaces;
  568. Procedure TestClassInterface_MethodOverride;
  569. Procedure TestClassInterface_Corba_Delegation;
  570. Procedure TestClassInterface_Corba_DelegationStatic;
  571. Procedure TestClassInterface_Corba_Operators;
  572. Procedure TestClassInterface_Corba_Args;
  573. Procedure TestClassInterface_Corba_ForIn;
  574. Procedure TestClassInterface_COM_AssignVar;
  575. Procedure TestClassInterface_COM_AssignArg;
  576. Procedure TestClassInterface_COM_FunctionResult;
  577. Procedure TestClassInterface_COM_InheritedFuncResult;
  578. Procedure TestClassInterface_COM_IsAsTypeCasts;
  579. Procedure TestClassInterface_COM_PassAsArg;
  580. Procedure TestClassInterface_COM_PassToUntypedParam;
  581. Procedure TestClassInterface_COM_FunctionInExpr;
  582. Procedure TestClassInterface_COM_Property;
  583. Procedure TestClassInterface_COM_IntfProperty;
  584. Procedure TestClassInterface_COM_Delegation;
  585. Procedure TestClassInterface_COM_With;
  586. Procedure TestClassInterface_COM_ForIn;
  587. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  588. Procedure TestClassInterface_COM_RecordIntfFail;
  589. Procedure TestClassInterface_COM_UnitInitialization;
  590. Procedure TestClassInterface_GUID;
  591. Procedure TestClassInterface_GUIDProperty;
  592. // helpers
  593. Procedure TestClassHelper_ClassVar;
  594. Procedure TestClassHelper_Method_AccessInstanceFields;
  595. Procedure TestClassHelper_Method_Call;
  596. //Procedure TestClassHelper_Constructor;
  597. //Procedure TestClassHelper_InheritedObjFPC;
  598. //Procedure TestClassHelper_InheritedDelphi;
  599. // todo: TestClassHelper_Property
  600. // todo: TestClassHelper_ClassProperty
  601. // todo: TestClassHelper_Overload
  602. // todo: TestRecordHelper
  603. // todo: TestTypeHelper
  604. // proc types
  605. Procedure TestProcType;
  606. Procedure TestProcType_Arg;
  607. Procedure TestProcType_FunctionFPC;
  608. Procedure TestProcType_FunctionDelphi;
  609. Procedure TestProcType_ProcedureDelphi;
  610. Procedure TestProcType_AsParam;
  611. Procedure TestProcType_MethodFPC;
  612. Procedure TestProcType_MethodDelphi;
  613. Procedure TestProcType_PropertyFPC;
  614. Procedure TestProcType_PropertyDelphi;
  615. Procedure TestProcType_WithClassInstDoPropertyFPC;
  616. Procedure TestProcType_Nested;
  617. Procedure TestProcType_NestedOfObject;
  618. Procedure TestProcType_ReferenceToProc;
  619. Procedure TestProcType_ReferenceToMethod;
  620. Procedure TestProcType_Typecast;
  621. Procedure TestProcType_PassProcToUntyped;
  622. Procedure TestProcType_PassProcToArray;
  623. // pointer
  624. Procedure TestPointer;
  625. Procedure TestPointer_Proc;
  626. Procedure TestPointer_AssignRecordFail;
  627. Procedure TestPointer_AssignStaticArrayFail;
  628. Procedure TestPointer_TypeCastJSValueToPointer;
  629. Procedure TestPointer_NonRecordFail;
  630. Procedure TestPointer_AnonymousArgTypeFail;
  631. Procedure TestPointer_AnonymousVarTypeFail;
  632. Procedure TestPointer_AnonymousResultTypeFail;
  633. Procedure TestPointer_AddrOperatorFail;
  634. Procedure TestPointer_ArrayParamsFail;
  635. Procedure TestPointer_PointerAddFail;
  636. Procedure TestPointer_IncPointerFail;
  637. Procedure TestPointer_Record;
  638. Procedure TestPointer_RecordArg;
  639. // jsvalue
  640. Procedure TestJSValue_AssignToJSValue;
  641. Procedure TestJSValue_TypeCastToBaseType;
  642. Procedure TestJSValue_Equal;
  643. Procedure TestJSValue_If;
  644. Procedure TestJSValue_Not;
  645. Procedure TestJSValue_Enum;
  646. Procedure TestJSValue_ClassInstance;
  647. Procedure TestJSValue_ClassOf;
  648. Procedure TestJSValue_ArrayOfJSValue;
  649. Procedure TestJSValue_ArrayLit;
  650. Procedure TestJSValue_Params;
  651. Procedure TestJSValue_UntypedParam;
  652. Procedure TestJSValue_FuncResultType;
  653. Procedure TestJSValue_ProcType_Assign;
  654. Procedure TestJSValue_ProcType_Equal;
  655. Procedure TestJSValue_ProcType_Param;
  656. Procedure TestJSValue_AssignToPointerFail;
  657. Procedure TestJSValue_OverloadDouble;
  658. Procedure TestJSValue_OverloadNativeInt;
  659. Procedure TestJSValue_OverloadWord;
  660. Procedure TestJSValue_OverloadString;
  661. Procedure TestJSValue_OverloadChar;
  662. Procedure TestJSValue_OverloadPointer;
  663. Procedure TestJSValue_ForIn;
  664. // RTTI
  665. Procedure TestRTTI_IntRange;
  666. Procedure TestRTTI_Double;
  667. Procedure TestRTTI_ProcType;
  668. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  669. Procedure TestRTTI_EnumAndSetType;
  670. Procedure TestRTTI_EnumRange;
  671. Procedure TestRTTI_AnonymousEnumType;
  672. Procedure TestRTTI_StaticArray;
  673. Procedure TestRTTI_DynArray;
  674. Procedure TestRTTI_ArrayNestedAnonymous;
  675. Procedure TestRTTI_PublishedMethodOverloadFail;
  676. Procedure TestRTTI_PublishedMethodExternalFail;
  677. Procedure TestRTTI_PublishedClassPropertyFail;
  678. Procedure TestRTTI_PublishedClassFieldFail;
  679. Procedure TestRTTI_PublishedFieldExternalFail;
  680. Procedure TestRTTI_Class_Field;
  681. Procedure TestRTTI_Class_Method;
  682. Procedure TestRTTI_Class_MethodArgFlags;
  683. Procedure TestRTTI_Class_Property;
  684. Procedure TestRTTI_Class_PropertyParams;
  685. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  686. Procedure TestRTTI_Class_OmitRTTI;
  687. Procedure TestRTTI_IndexModifier;
  688. Procedure TestRTTI_StoredModifier;
  689. Procedure TestRTTI_DefaultValue;
  690. Procedure TestRTTI_DefaultValueSet;
  691. Procedure TestRTTI_DefaultValueRangeType;
  692. Procedure TestRTTI_DefaultValueInherit;
  693. Procedure TestRTTI_OverrideMethod;
  694. Procedure TestRTTI_OverloadProperty;
  695. // ToDo: array argument
  696. Procedure TestRTTI_ClassForward;
  697. Procedure TestRTTI_ClassOf;
  698. Procedure TestRTTI_Record;
  699. Procedure TestRTTI_RecordAnonymousArray;
  700. Procedure TestRTTI_LocalTypes;
  701. Procedure TestRTTI_TypeInfo_BaseTypes;
  702. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  703. Procedure TestRTTI_TypeInfo_LocalFail;
  704. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  705. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  706. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  707. Procedure TestRTTI_TypeInfo_FunctionClassType;
  708. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  709. Procedure TestRTTI_Interface_Corba;
  710. Procedure TestRTTI_Interface_COM;
  711. // Resourcestring
  712. Procedure TestResourcestringProgram;
  713. Procedure TestResourcestringUnit;
  714. Procedure TestResourcestringImplementation;
  715. // Attributes
  716. Procedure TestAtributes_Ignore;
  717. // Assertions, checks
  718. procedure TestAssert;
  719. procedure TestAssert_SysUtils;
  720. procedure TestObjectChecks;
  721. procedure TestRangeChecks_AssignInt;
  722. procedure TestRangeChecks_AssignIntRange;
  723. procedure TestRangeChecks_AssignEnum;
  724. procedure TestRangeChecks_AssignEnumRange;
  725. procedure TestRangeChecks_AssignChar;
  726. procedure TestRangeChecks_AssignCharRange;
  727. procedure TestRangeChecks_ArrayIndex;
  728. procedure TestRangeChecks_ArrayOfRecIndex;
  729. procedure TestRangeChecks_StringIndex;
  730. procedure TestRangeChecks_TypecastInt;
  731. end;
  732. function LinesToStr(Args: array of const): string;
  733. function ExtractFileUnitName(aFilename: string): string;
  734. function JSToStr(El: TJSElement): string;
  735. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  736. implementation
  737. function LinesToStr(Args: array of const): string;
  738. var
  739. s: String;
  740. i: Integer;
  741. begin
  742. s:='';
  743. for i:=Low(Args) to High(Args) do
  744. case Args[i].VType of
  745. vtChar: s += Args[i].VChar+LineEnding;
  746. vtString: s += Args[i].VString^+LineEnding;
  747. vtPChar: s += Args[i].VPChar+LineEnding;
  748. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  749. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  750. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  751. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  752. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  753. end;
  754. Result:=s;
  755. end;
  756. function ExtractFileUnitName(aFilename: string): string;
  757. var
  758. p: Integer;
  759. begin
  760. Result:=ExtractFileName(aFilename);
  761. if Result='' then exit;
  762. for p:=length(Result) downto 1 do
  763. case Result[p] of
  764. '/','\': exit;
  765. '.':
  766. begin
  767. Delete(Result,p,length(Result));
  768. exit;
  769. end;
  770. end;
  771. end;
  772. function JSToStr(El: TJSElement): string;
  773. var
  774. aWriter: TBufferWriter;
  775. aJSWriter: TJSWriter;
  776. begin
  777. aJSWriter:=nil;
  778. aWriter:=TBufferWriter.Create(1000);
  779. try
  780. aJSWriter:=TJSWriter.Create(aWriter);
  781. aJSWriter.IndentSize:=2;
  782. aJSWriter.WriteJS(El);
  783. Result:=aWriter.AsString;
  784. finally
  785. aJSWriter.Free;
  786. aWriter.Free;
  787. end;
  788. end;
  789. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  790. // search diff, ignore changes in spaces
  791. const
  792. SpaceChars = [#9,#10,#13,' '];
  793. var
  794. ExpectedP, ActualP: PChar;
  795. function FindLineEnd(p: PChar): PChar;
  796. begin
  797. Result:=p;
  798. while not (Result^ in [#0,#10,#13]) do inc(Result);
  799. end;
  800. function FindLineStart(p, MinP: PChar): PChar;
  801. begin
  802. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  803. Result:=p;
  804. end;
  805. procedure SkipLineEnd(var p: PChar);
  806. begin
  807. if p^ in [#10,#13] then
  808. begin
  809. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  810. inc(p,2)
  811. else
  812. inc(p);
  813. end;
  814. end;
  815. procedure DiffFound;
  816. var
  817. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  818. ExpLine, ActLine: String;
  819. i, LineNo, DiffLineNo: Integer;
  820. begin
  821. writeln('Diff found "',Msg,'". Lines:');
  822. // write correct lines
  823. p:=PChar(Expected);
  824. LineNo:=0;
  825. DiffLineNo:=0;
  826. repeat
  827. StartPos:=p;
  828. while not (p^ in [#0,#10,#13]) do inc(p);
  829. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  830. SkipLineEnd(p);
  831. inc(LineNo);
  832. if (p<=ExpectedP) and (p^<>#0) then
  833. begin
  834. writeln('= ',ExpLine);
  835. end else begin
  836. // diff line
  837. if DiffLineNo=0 then DiffLineNo:=LineNo;
  838. // write actual line
  839. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  840. ActLineEndP:=FindLineEnd(ActualP);
  841. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  842. writeln('- ',ActLine);
  843. // write expected line
  844. writeln('+ ',ExpLine);
  845. // write empty line with pointer ^
  846. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  847. writeln('^');
  848. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  849. CheckSrcDiff:=false;
  850. // write up to three following actual lines to get some context
  851. for i:=1 to 3 do begin
  852. ActLineStartP:=ActLineEndP;
  853. SkipLineEnd(ActLineStartP);
  854. if ActLineStartP^=#0 then break;
  855. ActLineEndP:=FindLineEnd(ActLineStartP);
  856. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  857. writeln('~ ',ActLine);
  858. end;
  859. exit;
  860. end;
  861. until p^=#0;
  862. writeln('DiffFound Actual:-----------------------');
  863. writeln(Actual);
  864. writeln('DiffFound Expected:---------------------');
  865. writeln(Expected);
  866. writeln('DiffFound ------------------------------');
  867. Msg:='diff found, but lines are the same, internal error';
  868. CheckSrcDiff:=false;
  869. end;
  870. var
  871. IsSpaceNeeded: Boolean;
  872. LastChar, Quote: Char;
  873. begin
  874. Result:=true;
  875. Msg:='';
  876. if Expected='' then Expected:=' ';
  877. if Actual='' then Actual:=' ';
  878. ExpectedP:=PChar(Expected);
  879. ActualP:=PChar(Actual);
  880. repeat
  881. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  882. case ExpectedP^ of
  883. #0:
  884. begin
  885. // check that rest of Actual has only spaces
  886. while ActualP^ in SpaceChars do inc(ActualP);
  887. if ActualP^<>#0 then
  888. begin
  889. DiffFound;
  890. exit;
  891. end;
  892. exit(true);
  893. end;
  894. ' ',#9,#10,#13:
  895. begin
  896. // skip space in Expected
  897. IsSpaceNeeded:=false;
  898. if ExpectedP>PChar(Expected) then
  899. LastChar:=ExpectedP[-1]
  900. else
  901. LastChar:=#0;
  902. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  903. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  904. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  905. IsSpaceNeeded:=true;
  906. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  907. begin
  908. DiffFound;
  909. exit;
  910. end;
  911. while ActualP^ in SpaceChars do inc(ActualP);
  912. end;
  913. '''','"':
  914. begin
  915. while ActualP^ in SpaceChars do inc(ActualP);
  916. if ExpectedP^<>ActualP^ then
  917. begin
  918. DiffFound;
  919. exit;
  920. end;
  921. Quote:=ExpectedP^;
  922. repeat
  923. inc(ExpectedP);
  924. inc(ActualP);
  925. if ExpectedP^<>ActualP^ then
  926. begin
  927. DiffFound;
  928. exit;
  929. end;
  930. if (ExpectedP^ in [#0,#10,#13]) then
  931. break
  932. else if (ExpectedP^=Quote) then
  933. begin
  934. inc(ExpectedP);
  935. inc(ActualP);
  936. break;
  937. end;
  938. until false;
  939. end;
  940. else
  941. while ActualP^ in SpaceChars do inc(ActualP);
  942. if ExpectedP^<>ActualP^ then
  943. begin
  944. DiffFound;
  945. exit;
  946. end;
  947. inc(ExpectedP);
  948. inc(ActualP);
  949. end;
  950. until false;
  951. end;
  952. { TTestEnginePasResolver }
  953. destructor TTestEnginePasResolver.Destroy;
  954. begin
  955. FreeAndNil(FStreamResolver);
  956. FreeAndNil(FParser);
  957. FreeAndNil(FScanner);
  958. FreeAndNil(FStreamResolver);
  959. if Module<>nil then
  960. begin
  961. Module.Release{$IFDEF CheckPasTreeRefCount}('CreateElement'){$ENDIF};
  962. FModule:=nil;
  963. end;
  964. inherited Destroy;
  965. end;
  966. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  967. NameExpr, InFileExpr: TPasExpr): TPasModule;
  968. begin
  969. Result:=nil;
  970. if InFilename<>'' then
  971. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  972. if Assigned(OnFindUnit) then
  973. Result:=OnFindUnit(AName);
  974. if NameExpr=nil then ;
  975. end;
  976. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  977. begin
  978. // do not parse recursively
  979. // parse via the queue
  980. if Section=nil then ;
  981. end;
  982. { TCustomTestModule }
  983. function TCustomTestModule.GetMsgCount: integer;
  984. begin
  985. Result:=FHintMsgs.Count;
  986. end;
  987. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  988. begin
  989. Result:=TTestHintMessage(FHintMsgs[Index]);
  990. end;
  991. function TCustomTestModule.GetResolverCount: integer;
  992. begin
  993. Result:=FModules.Count;
  994. end;
  995. function TCustomTestModule.GetResolvers(Index: integer
  996. ): TTestEnginePasResolver;
  997. begin
  998. Result:=TTestEnginePasResolver(FModules[Index]);
  999. end;
  1000. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1001. ): TPasModule;
  1002. var
  1003. DefNamespace: String;
  1004. begin
  1005. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1006. if (Pos('.',aUnitName)<1) then
  1007. begin
  1008. DefNamespace:=GetDefaultNamespace;
  1009. if DefNamespace<>'' then
  1010. begin
  1011. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1012. if Result<>nil then exit;
  1013. end;
  1014. end;
  1015. Result:=LoadUnit(aUnitName);
  1016. if Result<>nil then exit;
  1017. {$IFDEF VerbosePas2JS}
  1018. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1019. {$ENDIF}
  1020. Fail('can''t find unit "'+aUnitName+'"');
  1021. end;
  1022. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1023. var
  1024. aParser: TPasParser;
  1025. Item: TTestHintMessage;
  1026. begin
  1027. aParser:=Sender as TPasParser;
  1028. Item:=TTestHintMessage.Create;
  1029. Item.Id:=aParser.LastMsgNumber;
  1030. Item.MsgType:=aParser.LastMsgType;
  1031. Item.MsgNumber:=aParser.LastMsgNumber;
  1032. Item.Msg:=Msg;
  1033. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1034. {$IFDEF VerbosePas2JS}
  1035. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1036. {$ENDIF}
  1037. FHintMsgs.Add(Item);
  1038. end;
  1039. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1040. );
  1041. var
  1042. aResolver: TTestEnginePasResolver;
  1043. Item: TTestHintMessage;
  1044. begin
  1045. aResolver:=Sender as TTestEnginePasResolver;
  1046. Item:=TTestHintMessage.Create;
  1047. Item.Id:=aResolver.LastMsgId;
  1048. Item.MsgType:=aResolver.LastMsgType;
  1049. Item.MsgNumber:=aResolver.LastMsgNumber;
  1050. Item.Msg:=Msg;
  1051. Item.SourcePos:=aResolver.LastSourcePos;
  1052. {$IFDEF VerbosePas2JS}
  1053. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1054. {$ENDIF}
  1055. FHintMsgs.Add(Item);
  1056. end;
  1057. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1058. var
  1059. Item: TTestHintMessage;
  1060. aScanner: TPas2jsPasScanner;
  1061. begin
  1062. aScanner:=Sender as TPas2jsPasScanner;
  1063. Item:=TTestHintMessage.Create;
  1064. Item.Id:=aScanner.LastMsgNumber;
  1065. Item.MsgType:=aScanner.LastMsgType;
  1066. Item.MsgNumber:=aScanner.LastMsgNumber;
  1067. Item.Msg:=Msg;
  1068. Item.SourcePos:=aScanner.CurSourcePos;
  1069. {$IFDEF VerbosePas2JS}
  1070. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1071. {$ENDIF}
  1072. FHintMsgs.Add(Item);
  1073. end;
  1074. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1075. var
  1076. i: Integer;
  1077. CurEngine: TTestEnginePasResolver;
  1078. CurUnitName: String;
  1079. begin
  1080. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1081. Result:=nil;
  1082. if (Module.ClassType=TPasModule)
  1083. and (CompareText(Module.Name,aUnitName)=0) then
  1084. exit(Module);
  1085. for i:=0 to ResolverCount-1 do
  1086. begin
  1087. CurEngine:=Resolvers[i];
  1088. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1089. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1090. if CompareText(aUnitName,CurUnitName)=0 then
  1091. begin
  1092. Result:=CurEngine.Module;
  1093. if Result<>nil then exit;
  1094. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1095. FileResolver.FindSourceFile(aUnitName);
  1096. CurEngine.StreamResolver:=TStreamResolver.Create;
  1097. CurEngine.StreamResolver.OwnsStreams:=True;
  1098. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1099. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1100. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1101. InitScanner(CurEngine.Scanner);
  1102. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1103. CurEngine.Parser.Options:=po_tcmodules;
  1104. if CompareText(CurUnitName,'System')=0 then
  1105. CurEngine.Parser.ImplicitUses.Clear;
  1106. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1107. try
  1108. CurEngine.Parser.NextToken;
  1109. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1110. except
  1111. on E: Exception do
  1112. HandleException(E);
  1113. end;
  1114. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1115. Result:=CurEngine.Module;
  1116. exit;
  1117. end;
  1118. end;
  1119. end;
  1120. procedure TCustomTestModule.SetUp;
  1121. begin
  1122. {$IFDEF EnablePasTreeGlobalRefCount}
  1123. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1124. {$ENDIF}
  1125. if FModules<>nil then
  1126. begin
  1127. writeln('TCustomTestModule.SetUp FModules<>nil');
  1128. Halt;
  1129. end;
  1130. inherited SetUp;
  1131. FSkipTests:=false;
  1132. FSource:=TStringList.Create;
  1133. FModules:=TObjectList.Create(true);
  1134. FFilename:='test1.pp';
  1135. FFileResolver:=TStreamResolver.Create;
  1136. FFileResolver.OwnsStreams:=True;
  1137. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1138. InitScanner(FScanner);
  1139. FEngine:=AddModule(Filename);
  1140. FEngine.Scanner:=FScanner;
  1141. FScanner.Resolver:=FEngine;
  1142. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1143. FParser.OnLog:=@OnParserLog;
  1144. FEngine.Parser:=FParser;
  1145. Parser.Options:=po_tcmodules;
  1146. FModule:=Nil;
  1147. FConverter:=CreateConverter;
  1148. FExpectedErrorClass:=nil;
  1149. end;
  1150. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1151. begin
  1152. Result:=TPasToJSConverter.Create;
  1153. Result.Options:=co_tcmodules;
  1154. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1155. end;
  1156. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1157. begin
  1158. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1159. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1160. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1161. aScanner.AllowedBoolSwitches:=msAllPas2jsBoolSwitches;
  1162. aScanner.ReadOnlyBoolSwitches:=msAllPas2jsBoolSwitchesReadOnly;
  1163. aScanner.CurrentBoolSwitches:=msAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1164. aScanner.OnLog:=@OnScannerLog;
  1165. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1166. end;
  1167. procedure TCustomTestModule.TearDown;
  1168. {$IFDEF CheckPasTreeRefCount}
  1169. var
  1170. El: TPasElement;
  1171. {$ENDIF}
  1172. var
  1173. i: Integer;
  1174. CurModule: TPasModule;
  1175. begin
  1176. FHintMsgs.Clear;
  1177. FHintMsgsGood.Clear;
  1178. FSkipTests:=false;
  1179. FJSRegModuleCall:=nil;
  1180. FJSModuleCallArgs:=nil;
  1181. FJSImplentationUses:=nil;
  1182. FJSInterfaceUses:=nil;
  1183. FJSModuleSrc:=nil;
  1184. FJSInitBody:=nil;
  1185. FreeAndNil(FJSSource);
  1186. FreeAndNil(FJSModule);
  1187. FreeAndNil(FConverter);
  1188. Engine.Clear;
  1189. FreeAndNil(FSource);
  1190. FreeAndNil(FFileResolver);
  1191. if FModules<>nil then
  1192. begin
  1193. for i:=0 to FModules.Count-1 do
  1194. begin
  1195. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1196. if CurModule=nil then continue;
  1197. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1198. CurModule.ReleaseUsedUnits;
  1199. end;
  1200. if FModule<>nil then
  1201. FModule.ReleaseUsedUnits;
  1202. for i:=0 to FModules.Count-1 do
  1203. begin
  1204. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1205. if CurModule=nil then continue;
  1206. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1207. end;
  1208. FreeAndNil(FModules);
  1209. ReleaseAndNil(TPasElement(FModule){$IFDEF CheckPasTreeRefCount},'CreateElement'{$ENDIF});
  1210. FEngine:=nil;
  1211. end;
  1212. inherited TearDown;
  1213. {$IFDEF EnablePasTreeGlobalRefCount}
  1214. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1215. begin
  1216. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1217. {$IFDEF CheckPasTreeRefCount}
  1218. El:=TPasElement.FirstRefEl;
  1219. while El<>nil do
  1220. begin
  1221. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1222. for i:=0 to El.RefIds.Count-1 do
  1223. writeln(' ',El.RefIds[i]);
  1224. El:=El.NextRefEl;
  1225. end;
  1226. {$ENDIF}
  1227. Halt;
  1228. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1229. end;
  1230. {$ENDIF}
  1231. end;
  1232. procedure TCustomTestModule.Add(Line: string);
  1233. begin
  1234. Source.Add(Line);
  1235. end;
  1236. procedure TCustomTestModule.Add(const Lines: array of string);
  1237. var
  1238. i: Integer;
  1239. begin
  1240. for i:=low(Lines) to high(Lines) do
  1241. Add(Lines[i]);
  1242. end;
  1243. procedure TCustomTestModule.StartParsing;
  1244. var
  1245. Src: String;
  1246. begin
  1247. Src:=Source.Text;
  1248. FEngine.Source:=Src;
  1249. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1250. Scanner.OpenFile(FileName);
  1251. Writeln('// Test : ',Self.TestName);
  1252. Writeln(Src);
  1253. end;
  1254. procedure TCustomTestModule.ParseModuleQueue;
  1255. var
  1256. i: Integer;
  1257. CurResolver: TTestEnginePasResolver;
  1258. Found: Boolean;
  1259. Section: TPasSection;
  1260. begin
  1261. // parse til exception or all modules finished
  1262. while not SkipTests do
  1263. begin
  1264. Found:=false;
  1265. for i:=0 to ResolverCount-1 do
  1266. begin
  1267. CurResolver:=Resolvers[i];
  1268. if CurResolver.CurrentParser=nil then continue;
  1269. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1270. continue;
  1271. CurResolver.Parser.ParseContinue;
  1272. Found:=true;
  1273. break;
  1274. end;
  1275. if not Found then break;
  1276. end;
  1277. for i:=0 to ResolverCount-1 do
  1278. begin
  1279. CurResolver:=Resolvers[i];
  1280. if CurResolver.Parser=nil then
  1281. begin
  1282. if CurResolver.CurrentParser<>nil then
  1283. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1284. continue;
  1285. end;
  1286. if CurResolver.Parser.CurModule<>nil then
  1287. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1288. end;
  1289. end;
  1290. procedure TCustomTestModule.ParseModule;
  1291. begin
  1292. if SkipTests then exit;
  1293. FFirstPasStatement:=nil;
  1294. try
  1295. StartParsing;
  1296. Parser.ParseMain(FModule);
  1297. ParseModuleQueue;
  1298. except
  1299. on E: Exception do
  1300. HandleException(E);
  1301. end;
  1302. if SkipTests then exit;
  1303. AssertNotNull('Module resulted in Module',Module);
  1304. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1305. TAssert.AssertSame('Has resolver',Engine,Parser.Engine);
  1306. end;
  1307. procedure TCustomTestModule.ParseProgram;
  1308. begin
  1309. if SkipTests then exit;
  1310. ParseModule;
  1311. if SkipTests then exit;
  1312. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1313. FPasProgram:=TPasProgram(Module);
  1314. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1315. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1316. if (PasProgram.InitializationSection.Elements.Count>0) then
  1317. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1318. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1319. end;
  1320. procedure TCustomTestModule.ParseUnit;
  1321. begin
  1322. if SkipTests then exit;
  1323. ParseModule;
  1324. if SkipTests then exit;
  1325. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1326. AssertNotNull('Has interface section',Module.InterfaceSection);
  1327. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1328. if (Module.InitializationSection<>nil)
  1329. and (Module.InitializationSection.Elements.Count>0)
  1330. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1331. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1332. end;
  1333. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1334. ): TTestEnginePasResolver;
  1335. var
  1336. i: Integer;
  1337. begin
  1338. for i:=0 to ResolverCount-1 do
  1339. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1340. exit(Resolvers[i]);
  1341. Result:=nil;
  1342. end;
  1343. function TCustomTestModule.AddModule(aFilename: string
  1344. ): TTestEnginePasResolver;
  1345. begin
  1346. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1347. if FindModuleWithFilename(aFilename)<>nil then
  1348. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1349. Result:=TTestEnginePasResolver.Create;
  1350. Result.Filename:=aFilename;
  1351. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1352. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1353. Result.OnLog:=@OnPasResolverLog;
  1354. FModules.Add(Result);
  1355. end;
  1356. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1357. ): TTestEnginePasResolver;
  1358. begin
  1359. Result:=AddModule(aFilename);
  1360. Result.Source:=Src;
  1361. end;
  1362. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1363. ImplementationSrc: string): TTestEnginePasResolver;
  1364. var
  1365. Src: String;
  1366. begin
  1367. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1368. Src+=LineEnding;
  1369. Src+='interface'+LineEnding;
  1370. Src+=LineEnding;
  1371. Src+=InterfaceSrc;
  1372. Src+='implementation'+LineEnding;
  1373. Src+=LineEnding;
  1374. Src+=ImplementationSrc;
  1375. Src+='end.'+LineEnding;
  1376. Result:=AddModuleWithSrc(aFilename,Src);
  1377. end;
  1378. procedure TCustomTestModule.AddSystemUnit;
  1379. begin
  1380. AddModuleWithIntfImplSrc('system.pp',
  1381. // interface
  1382. LinesToStr([
  1383. 'type',
  1384. ' integer=longint;',
  1385. 'var',
  1386. ' ExitCode: Longint;',
  1387. ''
  1388. // implementation
  1389. ]),LinesToStr([
  1390. ''
  1391. ]));
  1392. end;
  1393. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean);
  1394. begin
  1395. if NeedSystemUnit then
  1396. AddSystemUnit
  1397. else
  1398. Parser.ImplicitUses.Clear;
  1399. Add('program '+ExtractFileUnitName(Filename)+';');
  1400. Add('');
  1401. end;
  1402. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean);
  1403. begin
  1404. if NeedSystemUnit then
  1405. AddSystemUnit
  1406. else
  1407. Parser.ImplicitUses.Clear;
  1408. Add('unit Test1;');
  1409. Add('');
  1410. end;
  1411. procedure TCustomTestModule.ConvertModule;
  1412. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1413. out UsesLit: TJSArrayLiteral);
  1414. var
  1415. i: Integer;
  1416. Item: TJSElement;
  1417. Lit: TJSLiteral;
  1418. begin
  1419. UsesLit:=nil;
  1420. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1421. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1422. exit; // null is ok
  1423. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1424. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1425. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1426. begin
  1427. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1428. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1429. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1430. Lit:=TJSLiteral(Item);
  1431. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1432. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1433. end;
  1434. end;
  1435. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  1436. out Src: TJSSourceElements);
  1437. var
  1438. FunDecl: TJSFunctionDeclarationStatement;
  1439. FunDef: TJSFuncDef;
  1440. FunBody: TJSFunctionBody;
  1441. begin
  1442. Src:=nil;
  1443. AssertNotNull(ParamName,Arg.Expr);
  1444. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  1445. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  1446. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  1447. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  1448. FunDef:=FunDecl.AFunction as TJSFuncDef;
  1449. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  1450. AssertNotNull(ParamName+' body',FunDef.Body);
  1451. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  1452. FunBody:=FunDef.Body as TJSFunctionBody;
  1453. AssertNotNull(ParamName+' body.A',FunBody.A);
  1454. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  1455. Src:=FunBody.A as TJSSourceElements;
  1456. end;
  1457. var
  1458. ModuleNameExpr: TJSLiteral;
  1459. InitFunction: TJSFunctionDeclarationStatement;
  1460. InitAssign: TJSSimpleAssignStatement;
  1461. InitName: String;
  1462. LastNode: TJSElement;
  1463. Arg: TJSArrayLiteralElement;
  1464. begin
  1465. if SkipTests then exit;
  1466. try
  1467. FJSModule:=FConverter.ConvertPasElement(Module,Engine) as TJSSourceElements;
  1468. except
  1469. on E: Exception do
  1470. HandleException(E);
  1471. end;
  1472. if SkipTests then exit;
  1473. if ExpectedErrorClass<>nil then
  1474. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  1475. FJSSource:=TStringList.Create;
  1476. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  1477. {$IFDEF VerbosePas2JS}
  1478. writeln('TTestModule.ConvertModule JS:');
  1479. write(FJSSource.Text);
  1480. {$ENDIF}
  1481. // rtl.module(...
  1482. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  1483. AssertNotNull('register module call',JSModule.Statements.Nodes[0].Node);
  1484. AssertEquals('register module call',TJSCallExpression,JSModule.Statements.Nodes[0].Node.ClassType);
  1485. FJSRegModuleCall:=JSModule.Statements.Nodes[0].Node as TJSCallExpression;
  1486. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  1487. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  1488. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  1489. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  1490. // parameter 'unitname'
  1491. if JSModuleCallArgs.Elements.Count<1 then
  1492. Fail('rtl.module first param unit missing');
  1493. Arg:=JSModuleCallArgs.Elements.Elements[0];
  1494. AssertNotNull('module name param',Arg.Expr);
  1495. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  1496. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  1497. if Module is TPasProgram then
  1498. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString))
  1499. else
  1500. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  1501. // main uses section
  1502. if JSModuleCallArgs.Elements.Count<2 then
  1503. Fail('rtl.module second param main uses missing');
  1504. Arg:=JSModuleCallArgs.Elements.Elements[1];
  1505. CheckUsesList('interface',Arg,FJSInterfaceUses);
  1506. // program/library/interface function()
  1507. if JSModuleCallArgs.Elements.Count<3 then
  1508. Fail('rtl.module third param intf-function missing');
  1509. Arg:=JSModuleCallArgs.Elements.Elements[2];
  1510. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  1511. // search for $mod.$init or $mod.$main - the last statement
  1512. if Module is TPasProgram then
  1513. begin
  1514. InitName:='$main';
  1515. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  1516. end
  1517. else
  1518. InitName:='$init';
  1519. FJSInitBody:=nil;
  1520. if JSModuleSrc.Statements.Count>0 then
  1521. begin
  1522. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  1523. if LastNode is TJSSimpleAssignStatement then
  1524. begin
  1525. InitAssign:=LastNode as TJSSimpleAssignStatement;
  1526. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  1527. begin
  1528. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  1529. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  1530. end
  1531. else if Module is TPasProgram then
  1532. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  1533. end;
  1534. end;
  1535. // optional: implementation uses section
  1536. if JSModuleCallArgs.Elements.Count<4 then
  1537. exit;
  1538. Arg:=JSModuleCallArgs.Elements.Elements[3];
  1539. CheckUsesList('implementation',Arg,FJSImplentationUses);
  1540. // optional: implementation function()
  1541. if JSModuleCallArgs.Elements.Count<5 then
  1542. exit;
  1543. Arg:=JSModuleCallArgs.Elements.Elements[4];
  1544. CheckFunctionParam('module impl-function',Arg,FJSImplementationSrc);
  1545. end;
  1546. procedure TCustomTestModule.ConvertProgram;
  1547. begin
  1548. Add('end.');
  1549. ParseProgram;
  1550. ConvertModule;
  1551. end;
  1552. procedure TCustomTestModule.ConvertUnit;
  1553. begin
  1554. Add('end.');
  1555. ParseUnit;
  1556. ConvertModule;
  1557. end;
  1558. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  1559. begin
  1560. Result:=tcmodules.JSToStr(El);
  1561. end;
  1562. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  1563. DottedName: string);
  1564. begin
  1565. if DottedName='' then
  1566. begin
  1567. AssertNull(Msg,El);
  1568. end
  1569. else
  1570. begin
  1571. AssertNotNull(Msg,El);
  1572. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  1573. end;
  1574. end;
  1575. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  1576. begin
  1577. if El=nil then
  1578. Result:=''
  1579. else if El is TJSPrimaryExpressionIdent then
  1580. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  1581. else if El is TJSDotMemberExpression then
  1582. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  1583. else
  1584. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  1585. end;
  1586. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  1587. InitStatements: string; ImplStatements: string);
  1588. var
  1589. ActualSrc, ExpectedSrc, InitName: String;
  1590. begin
  1591. ActualSrc:=JSToStr(JSModuleSrc);
  1592. ExpectedSrc:=
  1593. 'var $mod = this;'+LineEnding
  1594. +Statements;
  1595. if coUseStrict in Converter.Options then
  1596. ExpectedSrc:='"use strict";'+LineEnding+ExpectedSrc;
  1597. if Module is TPasProgram then
  1598. InitName:='$main'
  1599. else
  1600. InitName:='$init';
  1601. if (Module is TPasProgram) or (Trim(InitStatements)<>'') then
  1602. ExpectedSrc:=ExpectedSrc+LineEnding
  1603. +'$mod.'+InitName+' = function () {'+LineEnding
  1604. +InitStatements
  1605. +'};'+LineEnding;
  1606. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  1607. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  1608. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1609. if (JSImplementationSrc<>nil) then
  1610. begin
  1611. ActualSrc:=JSToStr(JSImplementationSrc);
  1612. ExpectedSrc:=
  1613. 'var $mod = this;'+LineEnding
  1614. +'var $impl = $mod.$impl;'+LineEnding
  1615. +ImplStatements;
  1616. end
  1617. else
  1618. begin
  1619. ActualSrc:='';
  1620. ExpectedSrc:=ImplStatements;
  1621. end;
  1622. //writeln('TTestModule.CheckSource InitStatements="',InitStatements,'"');
  1623. //writeln('TCustomTestModule.CheckSource Expected: ',ExpectedSrc);
  1624. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1625. end;
  1626. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  1627. // search diff, ignore changes in spaces
  1628. var
  1629. s: string;
  1630. begin
  1631. if CheckSrcDiff(Expected,Actual,s) then exit;
  1632. Fail(Msg+': '+s);
  1633. end;
  1634. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  1635. var
  1636. aResolver: TTestEnginePasResolver;
  1637. aConverter: TPasToJSConverter;
  1638. aJSModule: TJSSourceElements;
  1639. ActualSrc: String;
  1640. begin
  1641. aResolver:=GetResolver(Filename);
  1642. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  1643. {$IFDEF VerbosePas2JS}
  1644. writeln('CheckUnit '+Filename+' converting ...');
  1645. {$ENDIF}
  1646. aConverter:=CreateConverter;
  1647. aJSModule:=nil;
  1648. try
  1649. try
  1650. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  1651. except
  1652. on E: Exception do
  1653. HandleException(E);
  1654. end;
  1655. ActualSrc:=ConvertJSModuleToString(aJSModule);
  1656. {$IFDEF VerbosePas2JS}
  1657. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  1658. write(aResolver.Source);
  1659. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  1660. write(ActualSrc);
  1661. {$ENDIF}
  1662. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  1663. finally
  1664. aJSModule.Free;
  1665. aConverter.Free;
  1666. end;
  1667. end;
  1668. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  1669. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  1670. var
  1671. i: Integer;
  1672. Item: TTestHintMessage;
  1673. Expected,Actual: string;
  1674. begin
  1675. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  1676. for i:=0 to MsgCount-1 do
  1677. begin
  1678. Item:=Msgs[i];
  1679. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  1680. if (Marker<>nil) then
  1681. begin
  1682. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  1683. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  1684. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  1685. end;
  1686. // found
  1687. FHintMsgsGood.Add(Item);
  1688. str(Item.MsgType,Actual);
  1689. str(MsgType,Expected);
  1690. AssertEquals('MsgType',Expected,Actual);
  1691. exit;
  1692. end;
  1693. // needed message missing -> show emitted messages
  1694. WriteSources('',0,0);
  1695. for i:=0 to MsgCount-1 do
  1696. begin
  1697. Item:=Msgs[i];
  1698. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  1699. ' ('+IntToStr(Item.MsgNumber),')');
  1700. if Marker<>nil then
  1701. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  1702. writeln(' {',Item.Msg,'}');
  1703. end;
  1704. str(MsgType,Expected);
  1705. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  1706. if Marker<>nil then
  1707. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  1708. Actual:=Actual+' '+Msg;
  1709. Fail(Actual);
  1710. end;
  1711. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  1712. );
  1713. var
  1714. i: Integer;
  1715. s, Txt: String;
  1716. Msg: TTestHintMessage;
  1717. begin
  1718. for i:=0 to MsgCount-1 do
  1719. begin
  1720. Msg:=Msgs[i];
  1721. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  1722. s:='';
  1723. str(Msg.MsgType,s);
  1724. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  1725. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  1726. if WithSourcePos then
  1727. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  1728. Txt:=Txt+' {'+Msg.Msg+'}';
  1729. Fail(Txt);
  1730. end;
  1731. end;
  1732. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  1733. MsgNumber: integer);
  1734. begin
  1735. ExpectedErrorClass:=EScannerError;
  1736. ExpectedErrorMsg:=Msg;
  1737. ExpectedErrorNumber:=MsgNumber;
  1738. end;
  1739. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  1740. MsgNumber: integer);
  1741. begin
  1742. ExpectedErrorClass:=EParserError;
  1743. ExpectedErrorMsg:=Msg;
  1744. ExpectedErrorNumber:=MsgNumber;
  1745. end;
  1746. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  1747. MsgNumber: integer);
  1748. begin
  1749. ExpectedErrorClass:=EPasResolve;
  1750. ExpectedErrorMsg:=Msg;
  1751. ExpectedErrorNumber:=MsgNumber;
  1752. end;
  1753. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  1754. MsgNumber: integer);
  1755. begin
  1756. ExpectedErrorClass:=EPas2JS;
  1757. ExpectedErrorMsg:=Msg;
  1758. ExpectedErrorNumber:=MsgNumber;
  1759. end;
  1760. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  1761. var
  1762. MsgNumber: Integer;
  1763. Msg: String;
  1764. begin
  1765. Result:=false;
  1766. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  1767. Msg:=E.Message;
  1768. if E is EPas2JS then
  1769. MsgNumber:=EPas2JS(E).MsgNumber
  1770. else if E is EPasResolve then
  1771. MsgNumber:=EPasResolve(E).MsgNumber
  1772. else if E is EParserError then
  1773. MsgNumber:=Parser.LastMsgNumber
  1774. else if E is EScannerError then
  1775. begin
  1776. MsgNumber:=Scanner.LastMsgNumber;
  1777. Msg:=Scanner.LastMsg;
  1778. end
  1779. else
  1780. MsgNumber:=0;
  1781. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  1782. if Result then
  1783. SkipTests:=true;
  1784. end;
  1785. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  1786. begin
  1787. if IsErrorExpected(E) then exit;
  1788. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  1789. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  1790. +' '+Scanner.CurFilename
  1791. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  1792. FailException(E);
  1793. end;
  1794. procedure TCustomTestModule.HandleParserError(E: EParserError);
  1795. begin
  1796. if IsErrorExpected(E) then exit;
  1797. WriteSources(E.Filename,E.Row,E.Column);
  1798. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  1799. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  1800. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  1801. );
  1802. FailException(E);
  1803. end;
  1804. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  1805. var
  1806. P: TPasSourcePos;
  1807. begin
  1808. if IsErrorExpected(E) then exit;
  1809. P:=E.SourcePos;
  1810. WriteSources(P.FileName,P.Row,P.Column);
  1811. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  1812. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  1813. FailException(E);
  1814. end;
  1815. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  1816. var
  1817. Row, Col: integer;
  1818. begin
  1819. if IsErrorExpected(E) then exit;
  1820. Engine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  1821. WriteSources(E.PasElement.SourceFilename,Row,Col);
  1822. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  1823. +' '+E.PasElement.SourceFilename
  1824. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  1825. FailException(E);
  1826. end;
  1827. procedure TCustomTestModule.HandleException(E: Exception);
  1828. begin
  1829. if E is EScannerError then
  1830. HandleScannerError(EScannerError(E))
  1831. else if E is EParserError then
  1832. HandleParserError(EParserError(E))
  1833. else if E is EPasResolve then
  1834. HandlePasResolveError(EPasResolve(E))
  1835. else if E is EPas2JS then
  1836. HandlePas2JSError(EPas2JS(E))
  1837. else
  1838. begin
  1839. if IsErrorExpected(E) then exit;
  1840. if not (E is EAssertionFailedError) then
  1841. begin
  1842. WriteSources('',0,0);
  1843. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  1844. end;
  1845. FailException(E);
  1846. end;
  1847. end;
  1848. procedure TCustomTestModule.FailException(E: Exception);
  1849. var
  1850. MsgNumber: Integer;
  1851. begin
  1852. if ExpectedErrorClass<>nil then
  1853. begin
  1854. if FExpectedErrorClass=E.ClassType then
  1855. begin
  1856. if E is EPas2JS then
  1857. MsgNumber:=EPas2JS(E).MsgNumber
  1858. else if E is EPasResolve then
  1859. MsgNumber:=EPasResolve(E).MsgNumber
  1860. else if E is EParserError then
  1861. MsgNumber:=Parser.LastMsgNumber
  1862. else if E is EScannerError then
  1863. MsgNumber:=Scanner.LastMsgNumber
  1864. else
  1865. MsgNumber:=0;
  1866. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  1867. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  1868. ExpectedErrorNumber,MsgNumber);
  1869. end else begin
  1870. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  1871. end;
  1872. end;
  1873. Fail(E.Message);
  1874. end;
  1875. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  1876. aCol: integer);
  1877. var
  1878. IsSrc: Boolean;
  1879. i, j: Integer;
  1880. SrcLines: TStringList;
  1881. Line: string;
  1882. aModule: TTestEnginePasResolver;
  1883. begin
  1884. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  1885. for i:=0 to ResolverCount-1 do
  1886. begin
  1887. aModule:=Resolvers[i];
  1888. SrcLines:=TStringList.Create;
  1889. try
  1890. SrcLines.Text:=aModule.Source;
  1891. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  1892. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  1893. for j:=1 to SrcLines.Count do
  1894. begin
  1895. Line:=SrcLines[j-1];
  1896. if IsSrc and (j=aRow) then
  1897. begin
  1898. write('*');
  1899. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  1900. end;
  1901. writeln(Format('%:4d: ',[j]),Line);
  1902. end;
  1903. finally
  1904. SrcLines.Free;
  1905. end;
  1906. end;
  1907. end;
  1908. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  1909. var
  1910. i: Integer;
  1911. begin
  1912. for i:=0 to ResolverCount-1 do
  1913. if Filename=Resolvers[i].Filename then exit(i);
  1914. Result:=-1;
  1915. end;
  1916. function TCustomTestModule.GetResolver(const Filename: string
  1917. ): TTestEnginePasResolver;
  1918. var
  1919. i: Integer;
  1920. begin
  1921. i:=IndexOfResolver(Filename);
  1922. if i<0 then exit(nil);
  1923. Result:=Resolvers[i];
  1924. end;
  1925. function TCustomTestModule.GetDefaultNamespace: string;
  1926. var
  1927. C: TClass;
  1928. begin
  1929. Result:='';
  1930. if FModule=nil then exit;
  1931. C:=FModule.ClassType;
  1932. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  1933. Result:=Engine.DefaultNameSpace;
  1934. end;
  1935. constructor TCustomTestModule.Create;
  1936. begin
  1937. inherited Create;
  1938. FHintMsgs:=TObjectList.Create(true);
  1939. FHintMsgsGood:=TFPList.Create;
  1940. end;
  1941. destructor TCustomTestModule.Destroy;
  1942. begin
  1943. FreeAndNil(FHintMsgs);
  1944. FreeAndNil(FHintMsgsGood);
  1945. inherited Destroy;
  1946. end;
  1947. { TTestModule }
  1948. procedure TTestModule.TestReservedWords;
  1949. var
  1950. i: integer;
  1951. begin
  1952. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  1953. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  1954. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  1955. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  1956. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  1957. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  1958. end;
  1959. procedure TTestModule.TestEmptyProgram;
  1960. begin
  1961. StartProgram(false);
  1962. Add('begin');
  1963. ConvertProgram;
  1964. CheckSource('TestEmptyProgram','','');
  1965. end;
  1966. procedure TTestModule.TestEmptyProgramUseStrict;
  1967. begin
  1968. Converter.Options:=Converter.Options+[coUseStrict];
  1969. StartProgram(false);
  1970. Add('begin');
  1971. ConvertProgram;
  1972. CheckSource('TestEmptyProgramUseStrict','','');
  1973. end;
  1974. procedure TTestModule.TestEmptyUnit;
  1975. begin
  1976. StartUnit(false);
  1977. Add('interface');
  1978. Add('implementation');
  1979. ConvertUnit;
  1980. CheckSource('TestEmptyUnit',
  1981. LinesToStr([
  1982. ]),
  1983. '');
  1984. end;
  1985. procedure TTestModule.TestEmptyUnitUseStrict;
  1986. begin
  1987. Converter.Options:=Converter.Options+[coUseStrict];
  1988. StartUnit(false);
  1989. Add('interface');
  1990. Add('implementation');
  1991. ConvertUnit;
  1992. CheckSource('TestEmptyUnitUseStrict',
  1993. LinesToStr([
  1994. ''
  1995. ]),
  1996. '');
  1997. end;
  1998. procedure TTestModule.TestDottedUnitNames;
  1999. begin
  2000. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  2001. LinesToStr([
  2002. 'var iV: longint;'
  2003. ]),
  2004. '');
  2005. FFilename:='ns1.test1.pp';
  2006. StartProgram(true);
  2007. Add('uses unIt2;');
  2008. Add('implementation');
  2009. Add('var');
  2010. Add(' i: longint;');
  2011. Add('begin');
  2012. Add(' i:=iv;');
  2013. Add(' i:=uNit2.iv;');
  2014. Add(' i:=Ns1.TEst1.i;');
  2015. ConvertProgram;
  2016. CheckSource('TestDottedUnitNames',
  2017. LinesToStr([
  2018. 'this.i = 0;',
  2019. '']),
  2020. LinesToStr([ // this.$init
  2021. '$mod.i = pas["NS1.Unit2"].iV;',
  2022. '$mod.i = pas["NS1.Unit2"].iV;',
  2023. '$mod.i = $mod.i;',
  2024. '']) );
  2025. end;
  2026. procedure TTestModule.TestDottedUnitNameImpl;
  2027. begin
  2028. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  2029. LinesToStr([
  2030. 'type',
  2031. ' TObject = class end;',
  2032. ' TTestA = class',
  2033. ' end;'
  2034. ]),
  2035. LinesToStr(['uses TEST.UnitB;'])
  2036. );
  2037. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  2038. LinesToStr([
  2039. 'uses TEST.UnitA;',
  2040. 'type TTestB = class(TTestA);'
  2041. ]),
  2042. ''
  2043. );
  2044. StartProgram(true);
  2045. Add('uses TEST.UnitA;');
  2046. Add('begin');
  2047. ConvertProgram;
  2048. CheckSource('TestDottedUnitNameImpl',
  2049. LinesToStr([
  2050. '']),
  2051. LinesToStr([ // this.$init
  2052. '']) );
  2053. CheckUnit('TEST.UnitA.pas',
  2054. LinesToStr([
  2055. 'rtl.module("TEST.UnitA", ["system"], function () {',
  2056. ' var $mod = this;',
  2057. ' rtl.createClass($mod, "TObject", null, function () {',
  2058. ' this.$init = function () {',
  2059. ' };',
  2060. ' this.$final = function () {',
  2061. ' };',
  2062. ' });',
  2063. ' rtl.createClass($mod, "TTestA", $mod.TObject, function () {',
  2064. ' });',
  2065. '}, ["TEST.UnitB"]);'
  2066. ]));
  2067. CheckUnit('TEST.UnitB.pas',
  2068. LinesToStr([
  2069. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  2070. ' var $mod = this;',
  2071. ' rtl.createClass($mod, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  2072. ' });',
  2073. '});'
  2074. ]));
  2075. end;
  2076. procedure TTestModule.TestDottedUnitExpr;
  2077. begin
  2078. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  2079. LinesToStr([
  2080. 'procedure DoIt;'
  2081. ]),
  2082. 'procedure DoIt; begin end;');
  2083. FFilename:='Ns1.SubNs1.Test1.pp';
  2084. StartProgram(true);
  2085. Add('uses Ns2.sUbnS2.unIt2;');
  2086. Add('implementation');
  2087. Add('var');
  2088. Add(' i: longint;');
  2089. Add('begin');
  2090. Add(' ns2.subns2.unit2.doit;');
  2091. Add(' i:=Ns1.SubNS1.TEst1.i;');
  2092. ConvertProgram;
  2093. CheckSource('TestDottedUnitExpr',
  2094. LinesToStr([
  2095. 'this.i = 0;',
  2096. '']),
  2097. LinesToStr([ // this.$init
  2098. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  2099. '$mod.i = $mod.i;',
  2100. '']) );
  2101. end;
  2102. procedure TTestModule.Test_ModeFPCFail;
  2103. begin
  2104. StartProgram(false);
  2105. Add('{$mode FPC}');
  2106. Add('begin');
  2107. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  2108. ConvertProgram;
  2109. end;
  2110. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  2111. begin
  2112. StartProgram(false);
  2113. Add('{$modeswitch cblocks-}');
  2114. Add('begin');
  2115. SetExpectedScannerError('Invalid mode switch: "cblocks-"',nErrInvalidModeSwitch);
  2116. ConvertProgram;
  2117. end;
  2118. procedure TTestModule.TestUnit_UseSystem;
  2119. begin
  2120. StartUnit(true);
  2121. Add([
  2122. 'interface',
  2123. 'var i: integer;',
  2124. 'implementation']);
  2125. ConvertUnit;
  2126. CheckSource('TestUnit_UseSystem',
  2127. LinesToStr([
  2128. 'this.i = 0;',
  2129. '']),
  2130. LinesToStr([
  2131. '']) );
  2132. end;
  2133. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  2134. begin
  2135. AddModuleWithIntfImplSrc('unit1.pp',
  2136. LinesToStr([
  2137. 'type number = longint;']),
  2138. LinesToStr([
  2139. 'uses test1;',
  2140. 'procedure DoIt;',
  2141. 'begin',
  2142. ' i:=3;',
  2143. 'end;']));
  2144. StartUnit(true);
  2145. Add([
  2146. 'interface',
  2147. 'uses unit1;',
  2148. 'var i: number;',
  2149. 'implementation']);
  2150. ConvertUnit;
  2151. CheckSource('TestUnit_Intf1Impl2Intf1',
  2152. LinesToStr([
  2153. 'this.i = 0;',
  2154. '']),
  2155. LinesToStr([
  2156. '']) );
  2157. end;
  2158. procedure TTestModule.TestIncludeVersion;
  2159. begin
  2160. StartProgram(false);
  2161. Add([
  2162. 'var',
  2163. ' s: string;',
  2164. ' i: word;',
  2165. 'begin',
  2166. ' s:={$I %line%};',
  2167. ' i:={$I %linenum%};',
  2168. ' s:={$I %currentroutine%};',
  2169. ' s:={$I %pas2jsversion%};',
  2170. ' s:={$I %pas2jstarget%};',
  2171. ' s:={$I %pas2jstargetos%};',
  2172. ' s:={$I %pas2jstargetcpu%};',
  2173. ' s:={$I %file%};',
  2174. '']);
  2175. ConvertProgram;
  2176. CheckSource('TestIncludeVersion',
  2177. LinesToStr([
  2178. 'this.s="";',
  2179. 'this.i = 0;']),
  2180. LinesToStr([
  2181. '$mod.s = "7";',
  2182. '$mod.i = 8;',
  2183. '$mod.s = "<anonymous>";',
  2184. '$mod.s = "Comp.Ver.tcmodules";',
  2185. '$mod.s = "Browser";',
  2186. '$mod.s = "Browser";',
  2187. '$mod.s = "ECMAScript5";',
  2188. '$mod.s = "test1.pp";',
  2189. '']));
  2190. end;
  2191. procedure TTestModule.TestVarInt;
  2192. begin
  2193. StartProgram(false);
  2194. Add('var MyI: longint;');
  2195. Add('begin');
  2196. ConvertProgram;
  2197. CheckSource('TestVarInt','this.MyI=0;','');
  2198. end;
  2199. procedure TTestModule.TestVarBaseTypes;
  2200. begin
  2201. StartProgram(false);
  2202. Add('var');
  2203. Add(' i: longint;');
  2204. Add(' s: string;');
  2205. Add(' c: char;');
  2206. Add(' b: boolean;');
  2207. Add(' d: double;');
  2208. Add(' i2: longint = 3;');
  2209. Add(' s2: string = ''foo'';');
  2210. Add(' c2: char = ''4'';');
  2211. Add(' b2: boolean = true;');
  2212. Add(' d2: double = 5.6;');
  2213. Add(' i3: longint = $707;');
  2214. Add(' i4: nativeint = 4503599627370495;');
  2215. Add(' i5: nativeint = -4503599627370495-1;');
  2216. Add(' i6: nativeint = $fffffffffffff;');
  2217. Add(' i7: nativeint = -$fffffffffffff-1;');
  2218. Add(' i8: byte = 00;');
  2219. Add(' u8: nativeuint = $fffffffffffff;');
  2220. Add(' u9: nativeuint = $0000000000000;');
  2221. Add(' u10: nativeuint = $00ff00;');
  2222. Add('begin');
  2223. ConvertProgram;
  2224. CheckSource('TestVarBaseTypes',
  2225. LinesToStr([
  2226. 'this.i = 0;',
  2227. 'this.s = "";',
  2228. 'this.c = "";',
  2229. 'this.b = false;',
  2230. 'this.d = 0.0;',
  2231. 'this.i2 = 3;',
  2232. 'this.s2 = "foo";',
  2233. 'this.c2 = "4";',
  2234. 'this.b2 = true;',
  2235. 'this.d2 = 5.6;',
  2236. 'this.i3 = 0x707;',
  2237. 'this.i4 = 4503599627370495;',
  2238. 'this.i5 = -4503599627370495-1;',
  2239. 'this.i6 = 0xfffffffffffff;',
  2240. 'this.i7 =-0xfffffffffffff-1;',
  2241. 'this.i8 = 0;',
  2242. 'this.u8 = 0xfffffffffffff;',
  2243. 'this.u9 = 0x0;',
  2244. 'this.u10 = 0xff00;'
  2245. ]),
  2246. '');
  2247. end;
  2248. procedure TTestModule.TestBaseTypeSingleFail;
  2249. begin
  2250. StartProgram(false);
  2251. Add('var s: single;');
  2252. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  2253. ConvertProgram;
  2254. end;
  2255. procedure TTestModule.TestBaseTypeExtendedFail;
  2256. begin
  2257. StartProgram(false);
  2258. Add('var e: extended;');
  2259. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  2260. ConvertProgram;
  2261. end;
  2262. procedure TTestModule.TestConstBaseTypes;
  2263. begin
  2264. StartProgram(false);
  2265. Add('const');
  2266. Add(' i: longint = 3;');
  2267. Add(' s: string = ''foo'';');
  2268. Add(' c: char = ''4'';');
  2269. Add(' b: boolean = true;');
  2270. Add(' d: double = 5.6;');
  2271. Add(' e = low(word);');
  2272. Add(' f = high(word);');
  2273. Add('begin');
  2274. ConvertProgram;
  2275. CheckSource('TestVarBaseTypes',
  2276. LinesToStr([
  2277. 'this.i=3;',
  2278. 'this.s="foo";',
  2279. 'this.c="4";',
  2280. 'this.b=true;',
  2281. 'this.d=5.6;',
  2282. 'this.e = 0;',
  2283. 'this.f = 65535;'
  2284. ]),
  2285. '');
  2286. end;
  2287. procedure TTestModule.TestAliasTypeRef;
  2288. begin
  2289. StartProgram(false);
  2290. Add('type');
  2291. Add(' a=longint;');
  2292. Add(' b=a;');
  2293. Add('var');
  2294. Add(' c: A;');
  2295. Add(' d: B;');
  2296. Add('begin');
  2297. ConvertProgram;
  2298. CheckSource('TestAliasTypeRef',
  2299. LinesToStr([ // statements
  2300. 'this.c = 0;',
  2301. 'this.d = 0;'
  2302. ]),
  2303. LinesToStr([ // this.$main
  2304. ''
  2305. ]));
  2306. end;
  2307. procedure TTestModule.TestTypeCast_BaseTypes;
  2308. begin
  2309. StartProgram(false);
  2310. Add([
  2311. 'var',
  2312. ' i: longint;',
  2313. ' b: boolean;',
  2314. ' d: double;',
  2315. ' s: string;',
  2316. ' c: char;',
  2317. 'begin',
  2318. ' i:=longint(i);',
  2319. ' i:=longint(b);',
  2320. ' b:=boolean(b);',
  2321. ' b:=boolean(i);',
  2322. ' d:=double(d);',
  2323. ' d:=double(i);',
  2324. ' s:=string(s);',
  2325. ' s:=string(c);',
  2326. ' c:=char(c);',
  2327. ' c:=char(i);',
  2328. ' c:=char(65);',
  2329. ' c:=char(#10);',
  2330. ' c:=char(#$E000);',
  2331. '']);
  2332. ConvertProgram;
  2333. CheckSource('TestAliasTypeRef',
  2334. LinesToStr([ // statements
  2335. 'this.i = 0;',
  2336. 'this.b = false;',
  2337. 'this.d = 0.0;',
  2338. 'this.s = "";',
  2339. 'this.c = "";',
  2340. '']),
  2341. LinesToStr([ // this.$main
  2342. '$mod.i = $mod.i;',
  2343. '$mod.i = ($mod.b ? 1 : 0);',
  2344. '$mod.b = $mod.b;',
  2345. '$mod.b = $mod.i != 0;',
  2346. '$mod.d = $mod.d;',
  2347. '$mod.d = $mod.i;',
  2348. '$mod.s = $mod.s;',
  2349. '$mod.s = $mod.c;',
  2350. '$mod.c = $mod.c;',
  2351. '$mod.c = String.fromCharCode($mod.i);',
  2352. '$mod.c = "A";',
  2353. '$mod.c = "\n";',
  2354. '$mod.c = "";',
  2355. '']));
  2356. end;
  2357. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  2358. begin
  2359. StartProgram(false);
  2360. Add('type');
  2361. Add(' integer = longint;');
  2362. Add(' TYesNo = boolean;');
  2363. Add(' TFloat = double;');
  2364. Add(' TCaption = string;');
  2365. Add(' TChar = char;');
  2366. Add('var');
  2367. Add(' i: integer;');
  2368. Add(' b: TYesNo;');
  2369. Add(' d: TFloat;');
  2370. Add(' s: TCaption;');
  2371. Add(' c: TChar;');
  2372. Add('begin');
  2373. Add(' i:=integer(i);');
  2374. Add(' i:=integer(b);');
  2375. Add(' b:=TYesNo(b);');
  2376. Add(' b:=TYesNo(i);');
  2377. Add(' d:=TFloat(d);');
  2378. Add(' d:=TFloat(i);');
  2379. Add(' s:=TCaption(s);');
  2380. Add(' s:=TCaption(c);');
  2381. Add(' c:=TChar(c);');
  2382. ConvertProgram;
  2383. CheckSource('TestAliasTypeRef',
  2384. LinesToStr([ // statements
  2385. 'this.i = 0;',
  2386. 'this.b = false;',
  2387. 'this.d = 0.0;',
  2388. 'this.s = "";',
  2389. 'this.c = "";',
  2390. '']),
  2391. LinesToStr([ // this.$main
  2392. '$mod.i = $mod.i;',
  2393. '$mod.i = ($mod.b ? 1 : 0);',
  2394. '$mod.b = $mod.b;',
  2395. '$mod.b = $mod.i != 0;',
  2396. '$mod.d = $mod.d;',
  2397. '$mod.d = $mod.i;',
  2398. '$mod.s = $mod.s;',
  2399. '$mod.s = $mod.c;',
  2400. '$mod.c = $mod.c;',
  2401. '']));
  2402. end;
  2403. procedure TTestModule.TestEmptyProc;
  2404. begin
  2405. StartProgram(false);
  2406. Add('procedure Test;');
  2407. Add('begin');
  2408. Add('end;');
  2409. Add('begin');
  2410. ConvertProgram;
  2411. CheckSource('TestEmptyProc',
  2412. LinesToStr([ // statements
  2413. 'this.Test = function () {',
  2414. '};'
  2415. ]),
  2416. LinesToStr([ // this.$main
  2417. ''
  2418. ]));
  2419. end;
  2420. procedure TTestModule.TestProcOneParam;
  2421. begin
  2422. StartProgram(false);
  2423. Add('procedure ProcA(i: longint);');
  2424. Add('begin');
  2425. Add('end;');
  2426. Add('begin');
  2427. Add(' PROCA(3);');
  2428. ConvertProgram;
  2429. CheckSource('TestProcOneParam',
  2430. LinesToStr([ // statements
  2431. 'this.ProcA = function (i) {',
  2432. '};'
  2433. ]),
  2434. LinesToStr([ // this.$main
  2435. '$mod.ProcA(3);'
  2436. ]));
  2437. end;
  2438. procedure TTestModule.TestFunctionWithoutParams;
  2439. begin
  2440. StartProgram(false);
  2441. Add('function FuncA: longint;');
  2442. Add('begin');
  2443. Add('end;');
  2444. Add('var i: longint;');
  2445. Add('begin');
  2446. Add(' I:=FUNCA();');
  2447. Add(' I:=FUNCA;');
  2448. Add(' FUNCA();');
  2449. Add(' FUNCA;');
  2450. ConvertProgram;
  2451. CheckSource('TestProcWithoutParams',
  2452. LinesToStr([ // statements
  2453. 'this.FuncA = function () {',
  2454. ' var Result = 0;',
  2455. ' return Result;',
  2456. '};',
  2457. 'this.i=0;'
  2458. ]),
  2459. LinesToStr([ // this.$main
  2460. '$mod.i=$mod.FuncA();',
  2461. '$mod.i=$mod.FuncA();',
  2462. '$mod.FuncA();',
  2463. '$mod.FuncA();'
  2464. ]));
  2465. end;
  2466. procedure TTestModule.TestProcedureWithoutParams;
  2467. begin
  2468. StartProgram(false);
  2469. Add('procedure ProcA;');
  2470. Add('begin');
  2471. Add('end;');
  2472. Add('begin');
  2473. Add(' PROCA();');
  2474. Add(' PROCA;');
  2475. ConvertProgram;
  2476. CheckSource('TestProcWithoutParams',
  2477. LinesToStr([ // statements
  2478. 'this.ProcA = function () {',
  2479. '};'
  2480. ]),
  2481. LinesToStr([ // this.$main
  2482. '$mod.ProcA();',
  2483. '$mod.ProcA();'
  2484. ]));
  2485. end;
  2486. procedure TTestModule.TestIncDec;
  2487. begin
  2488. StartProgram(false);
  2489. Add([
  2490. 'procedure DoIt(var i: longint);',
  2491. 'begin',
  2492. ' inc(i);',
  2493. ' inc(i,2);',
  2494. 'end;',
  2495. 'var',
  2496. ' Bar: longint;',
  2497. 'begin',
  2498. ' inc(bar);',
  2499. ' inc(bar,2);',
  2500. ' dec(bar);',
  2501. ' dec(bar,3);',
  2502. '']);
  2503. ConvertProgram;
  2504. CheckSource('TestIncDec',
  2505. LinesToStr([ // statements
  2506. 'this.DoIt = function (i) {',
  2507. ' i.set(i.get()+1);',
  2508. ' i.set(i.get()+2);',
  2509. '};',
  2510. 'this.Bar = 0;'
  2511. ]),
  2512. LinesToStr([ // this.$main
  2513. '$mod.Bar+=1;',
  2514. '$mod.Bar+=2;',
  2515. '$mod.Bar-=1;',
  2516. '$mod.Bar-=3;'
  2517. ]));
  2518. end;
  2519. procedure TTestModule.TestLoHiFpcMode;
  2520. begin
  2521. StartProgram(false);
  2522. Add([
  2523. '{$mode objfpc}',
  2524. 'const',
  2525. ' LoByte1 = Lo(Word($1234));',
  2526. ' HiByte1 = Hi(Word($1234));',
  2527. ' LoByte2 = Lo(SmallInt($1234));',
  2528. ' HiByte2 = Hi(SmallInt($1234));',
  2529. ' LoWord1 = Lo($1234CDEF);',
  2530. ' HiWord1 = Hi($1234CDEF);',
  2531. ' LoWord2 = Lo(-$1234CDEF);',
  2532. ' HiWord2 = Hi(-$1234CDEF);',
  2533. ' lo4:byte=lo(byte($34));',
  2534. ' hi4:byte=hi(byte($34));',
  2535. ' lo5:byte=lo(shortint(-$34));',
  2536. ' hi5:byte=hi(shortint(-$34));',
  2537. ' lo6:longword=lo($123456789ABCD);',
  2538. ' hi6:longword=hi($123456789ABCD);',
  2539. ' lo7:longword=lo(-$123456789ABCD);',
  2540. ' hi7:longword=hi(-$123456789ABCD);',
  2541. 'var',
  2542. ' b: Byte;',
  2543. ' ss: shortint;',
  2544. ' w: Word;',
  2545. ' si: SmallInt;',
  2546. ' lw: LongWord;',
  2547. ' li: LongInt;',
  2548. ' b2: Byte;',
  2549. ' ni: nativeint;',
  2550. 'begin',
  2551. ' w := $1234;',
  2552. ' ss := -$12;',
  2553. ' b := lo(ss);',
  2554. ' b := HI(ss);',
  2555. ' b := lo(w);',
  2556. ' b := HI(w);',
  2557. ' b2 := lo(b);',
  2558. ' b2 := hi(b);',
  2559. ' lw := $1234CDEF;',
  2560. ' w := lo(lw);',
  2561. ' w := hi(lw);',
  2562. ' ni := $123456789ABCD;',
  2563. ' lw := lo(ni);',
  2564. ' lw := hi(ni);',
  2565. '']);
  2566. ConvertProgram;
  2567. CheckSource('TestLoHiFpcMode',
  2568. LinesToStr([ // statements
  2569. 'this.LoByte1 = 0x1234 & 0xFF;',
  2570. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2571. 'this.LoByte2 = 0x1234 & 0xFF;',
  2572. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2573. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  2574. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  2575. 'this.LoWord2 = -0x1234CDEF & 0xFFFF;',
  2576. 'this.HiWord2 = (-0x1234CDEF >> 16) & 0xFFFF;',
  2577. 'this.lo4 = 0x34 & 0xF;',
  2578. 'this.hi4 = (0x34 >> 4) & 0xF;',
  2579. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  2580. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  2581. 'this.lo6 = 0x123456789ABCD >>> 0;',
  2582. 'this.hi6 = 74565 >>> 0;',
  2583. 'this.lo7 = -0x123456789ABCD >>> 0;',
  2584. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  2585. 'this.b = 0;',
  2586. 'this.ss = 0;',
  2587. 'this.w = 0;',
  2588. 'this.si = 0;',
  2589. 'this.lw = 0;',
  2590. 'this.li = 0;',
  2591. 'this.b2 = 0;',
  2592. 'this.ni = 0;',
  2593. '']),
  2594. LinesToStr([ // this.$main
  2595. '$mod.w = 0x1234;',
  2596. '$mod.ss = -0x12;',
  2597. '$mod.b = $mod.ss & 0xFF;',
  2598. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  2599. '$mod.b = $mod.w & 0xFF;',
  2600. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2601. '$mod.b2 = $mod.b & 0xF;',
  2602. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  2603. '$mod.lw = 0x1234CDEF;',
  2604. '$mod.w = $mod.lw & 0xFFFF;',
  2605. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  2606. '$mod.ni = 0x123456789ABCD;',
  2607. '$mod.lw = $mod.ni >>> 0;',
  2608. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  2609. '']));
  2610. end;
  2611. procedure TTestModule.TestLoHiDelphiMode;
  2612. begin
  2613. StartProgram(false);
  2614. Add([
  2615. '{$mode delphi}',
  2616. 'const',
  2617. ' LoByte1 = Lo(Word($1234));',
  2618. ' HiByte1 = Hi(Word($1234));',
  2619. ' LoByte2 = Lo(SmallInt($1234));',
  2620. ' HiByte2 = Hi(SmallInt($1234));',
  2621. ' LoByte3 = Lo($1234CDEF);',
  2622. ' HiByte3 = Hi($1234CDEF);',
  2623. ' LoByte4 = Lo(-$1234CDEF);',
  2624. ' HiByte4 = Hi(-$1234CDEF);',
  2625. 'var',
  2626. ' b: Byte;',
  2627. ' w: Word;',
  2628. ' si: SmallInt;',
  2629. ' lw: LongWord;',
  2630. ' li: LongInt;',
  2631. 'begin',
  2632. ' w := $1234;',
  2633. ' b := lo(w);',
  2634. ' b := HI(w);',
  2635. ' lw := $1234CDEF;',
  2636. ' b := lo(lw);',
  2637. ' b := hi(lw);',
  2638. '']);
  2639. ConvertProgram;
  2640. CheckSource('TestLoHiDelphiMode',
  2641. LinesToStr([ // statements
  2642. 'this.LoByte1 = 0x1234 & 0xFF;',
  2643. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2644. 'this.LoByte2 = 0x1234 & 0xFF;',
  2645. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2646. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  2647. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  2648. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  2649. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  2650. 'this.b = 0;',
  2651. 'this.w = 0;',
  2652. 'this.si = 0;',
  2653. 'this.lw = 0;',
  2654. 'this.li = 0;'
  2655. ]),
  2656. LinesToStr([ // this.$main
  2657. '$mod.w = 0x1234;',
  2658. '$mod.b = $mod.w & 0xFF;',
  2659. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2660. '$mod.lw = 0x1234CDEF;',
  2661. '$mod.b = $mod.lw & 0xFF;',
  2662. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  2663. ]));
  2664. end;
  2665. procedure TTestModule.TestAssignments;
  2666. begin
  2667. StartProgram(false);
  2668. Parser.Options:=Parser.Options+[po_cassignments];
  2669. Add('var');
  2670. Add(' Bar:longint;');
  2671. Add('begin');
  2672. Add(' bar:=3;');
  2673. Add(' bar+=4;');
  2674. Add(' bar-=5;');
  2675. Add(' bar*=6;');
  2676. ConvertProgram;
  2677. CheckSource('TestAssignments',
  2678. LinesToStr([ // statements
  2679. 'this.Bar = 0;'
  2680. ]),
  2681. LinesToStr([ // this.$main
  2682. '$mod.Bar=3;',
  2683. '$mod.Bar+=4;',
  2684. '$mod.Bar-=5;',
  2685. '$mod.Bar*=6;'
  2686. ]));
  2687. end;
  2688. procedure TTestModule.TestArithmeticOperators1;
  2689. begin
  2690. StartProgram(false);
  2691. Add('var');
  2692. Add(' vA,vB,vC:longint;');
  2693. Add('begin');
  2694. Add(' va:=1;');
  2695. Add(' vb:=va+va;');
  2696. Add(' vb:=va div vb;');
  2697. Add(' vb:=va mod vb;');
  2698. Add(' vb:=va+va*vb+va div vb;');
  2699. Add(' vc:=-va;');
  2700. Add(' va:=va-vb;');
  2701. Add(' vb:=va;');
  2702. Add(' if va<vb then vc:=va else vc:=vb;');
  2703. ConvertProgram;
  2704. CheckSource('TestArithmeticOperators1',
  2705. LinesToStr([ // statements
  2706. 'this.vA = 0;',
  2707. 'this.vB = 0;',
  2708. 'this.vC = 0;'
  2709. ]),
  2710. LinesToStr([ // this.$main
  2711. '$mod.vA = 1;',
  2712. '$mod.vB = $mod.vA + $mod.vA;',
  2713. '$mod.vB = Math.floor($mod.vA / $mod.vB);',
  2714. '$mod.vB = $mod.vA % $mod.vB;',
  2715. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + Math.floor($mod.vA / $mod.vB);',
  2716. '$mod.vC = -$mod.vA;',
  2717. '$mod.vA = $mod.vA - $mod.vB;',
  2718. '$mod.vB = $mod.vA;',
  2719. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  2720. ]));
  2721. end;
  2722. procedure TTestModule.TestLogicalOperators;
  2723. begin
  2724. StartProgram(false);
  2725. Add('var');
  2726. Add(' vA,vB,vC:boolean;');
  2727. Add('begin');
  2728. Add(' va:=vb and vc;');
  2729. Add(' va:=vb or vc;');
  2730. Add(' va:=vb xor vc;');
  2731. Add(' va:=true and vc;');
  2732. Add(' va:=(vb and vc) or (va and vb);');
  2733. Add(' va:=not vb;');
  2734. ConvertProgram;
  2735. CheckSource('TestLogicalOperators',
  2736. LinesToStr([ // statements
  2737. 'this.vA = false;',
  2738. 'this.vB = false;',
  2739. 'this.vC = false;'
  2740. ]),
  2741. LinesToStr([ // this.$main
  2742. '$mod.vA = $mod.vB && $mod.vC;',
  2743. '$mod.vA = $mod.vB || $mod.vC;',
  2744. '$mod.vA = $mod.vB ^ $mod.vC;',
  2745. '$mod.vA = true && $mod.vC;',
  2746. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  2747. '$mod.vA = !$mod.vB;'
  2748. ]));
  2749. end;
  2750. procedure TTestModule.TestBitwiseOperators;
  2751. begin
  2752. StartProgram(false);
  2753. Add('var');
  2754. Add(' vA,vB,vC:longint;');
  2755. Add('begin');
  2756. Add(' va:=vb and vc;');
  2757. Add(' va:=vb or vc;');
  2758. Add(' va:=vb xor vc;');
  2759. Add(' va:=vb shl vc;');
  2760. Add(' va:=vb shr vc;');
  2761. Add(' va:=3 and vc;');
  2762. Add(' va:=(vb and vc) or (va and vb);');
  2763. Add(' va:=not vb;');
  2764. ConvertProgram;
  2765. CheckSource('TestBitwiseOperators',
  2766. LinesToStr([ // statements
  2767. 'this.vA = 0;',
  2768. 'this.vB = 0;',
  2769. 'this.vC = 0;'
  2770. ]),
  2771. LinesToStr([ // this.$main
  2772. '$mod.vA = $mod.vB & $mod.vC;',
  2773. '$mod.vA = $mod.vB | $mod.vC;',
  2774. '$mod.vA = $mod.vB ^ $mod.vC;',
  2775. '$mod.vA = $mod.vB << $mod.vC;',
  2776. '$mod.vA = $mod.vB >>> $mod.vC;',
  2777. '$mod.vA = 3 & $mod.vC;',
  2778. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  2779. '$mod.vA = ~$mod.vB;'
  2780. ]));
  2781. end;
  2782. procedure TTestModule.TestPrgProcVar;
  2783. begin
  2784. StartProgram(false);
  2785. Add('procedure Proc1;');
  2786. Add('type');
  2787. Add(' t1=longint;');
  2788. Add('var');
  2789. Add(' vA:t1;');
  2790. Add('begin');
  2791. Add('end;');
  2792. Add('begin');
  2793. ConvertProgram;
  2794. CheckSource('TestPrgProcVar',
  2795. LinesToStr([ // statements
  2796. 'this.Proc1 = function () {',
  2797. ' var vA=0;',
  2798. '};'
  2799. ]),
  2800. LinesToStr([ // this.$main
  2801. ''
  2802. ]));
  2803. end;
  2804. procedure TTestModule.TestUnitProcVar;
  2805. begin
  2806. StartUnit(false);
  2807. Add('interface');
  2808. Add('');
  2809. Add('type tA=string; // unit scope');
  2810. Add('procedure Proc1;');
  2811. Add('');
  2812. Add('implementation');
  2813. Add('');
  2814. Add('procedure Proc1;');
  2815. Add('type tA=longint; // local proc scope');
  2816. Add('var v1:tA; // using local tA');
  2817. Add('begin');
  2818. Add('end;');
  2819. Add('var v2:tA; // using interface tA');
  2820. ConvertUnit;
  2821. CheckSource('TestUnitProcVar',
  2822. LinesToStr([ // statements
  2823. 'var $impl = $mod.$impl;',
  2824. 'this.Proc1 = function () {',
  2825. ' var v1 = 0;',
  2826. '};',
  2827. '']),
  2828. // this.$init
  2829. '',
  2830. // implementation
  2831. LinesToStr([
  2832. '$impl.v2 = "";',
  2833. '']));
  2834. end;
  2835. procedure TTestModule.TestImplProc;
  2836. begin
  2837. StartUnit(false);
  2838. Add('interface');
  2839. Add('');
  2840. Add('procedure Proc1;');
  2841. Add('');
  2842. Add('implementation');
  2843. Add('');
  2844. Add('procedure Proc1; begin end;');
  2845. Add('procedure Proc2; begin end;');
  2846. Add('initialization');
  2847. Add(' Proc1;');
  2848. Add(' Proc2;');
  2849. ConvertUnit;
  2850. CheckSource('TestImplProc',
  2851. LinesToStr([ // statements
  2852. 'var $impl = $mod.$impl;',
  2853. 'this.Proc1 = function () {',
  2854. '};',
  2855. '']),
  2856. LinesToStr([ // this.$init
  2857. '$mod.Proc1();',
  2858. '$impl.Proc2();',
  2859. '']),
  2860. LinesToStr([ // implementation
  2861. '$impl.Proc2 = function () {',
  2862. '};',
  2863. ''])
  2864. );
  2865. end;
  2866. procedure TTestModule.TestFunctionResult;
  2867. begin
  2868. StartProgram(false);
  2869. Add('function Func1: longint;');
  2870. Add('begin');
  2871. Add(' Result:=3;');
  2872. Add(' Func1:=4;');
  2873. Add('end;');
  2874. Add('begin');
  2875. ConvertProgram;
  2876. CheckSource('TestFunctionResult',
  2877. LinesToStr([ // statements
  2878. 'this.Func1 = function () {',
  2879. ' var Result = 0;',
  2880. ' Result = 3;',
  2881. ' Result = 4;',
  2882. ' return Result;',
  2883. '};'
  2884. ]),
  2885. '');
  2886. end;
  2887. procedure TTestModule.TestNestedProc;
  2888. begin
  2889. StartProgram(false);
  2890. Add([
  2891. 'var vInUnit: longint;',
  2892. 'function DoIt(pA,pD: longint): longint;',
  2893. 'var',
  2894. ' vB: longint;',
  2895. ' vC: longint;',
  2896. ' function Nesty(pA: longint): longint; ',
  2897. ' var vB: longint;',
  2898. ' begin',
  2899. ' Result:=pa+vb+vc+pd+vInUnit;',
  2900. ' nesty:=3;',
  2901. ' doit:=4;',
  2902. ' exit;',
  2903. ' end;',
  2904. 'begin',
  2905. ' Result:=pa+vb+vc;',
  2906. ' doit:=6;',
  2907. ' exit;',
  2908. 'end;',
  2909. 'begin']);
  2910. ConvertProgram;
  2911. CheckSource('TestNestedProc',
  2912. LinesToStr([ // statements
  2913. 'this.vInUnit = 0;',
  2914. 'this.DoIt = function (pA, pD) {',
  2915. ' var Result = 0;',
  2916. ' var vB = 0;',
  2917. ' var vC = 0;',
  2918. ' function Nesty(pA) {',
  2919. ' var Result$1 = 0;',
  2920. ' var vB = 0;',
  2921. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  2922. ' Result$1 = 3;',
  2923. ' Result = 4;',
  2924. ' return Result$1;',
  2925. ' return Result$1;',
  2926. ' };',
  2927. ' Result = pA + vB + vC;',
  2928. ' Result = 6;',
  2929. ' return Result;',
  2930. ' return Result;',
  2931. '};'
  2932. ]),
  2933. '');
  2934. end;
  2935. procedure TTestModule.TestNestedProc_ResultString;
  2936. begin
  2937. StartProgram(false);
  2938. Add([
  2939. 'function DoIt: string;',
  2940. ' function Nesty: string; ',
  2941. ' begin',
  2942. ' nesty:=#65#66;',
  2943. ' nesty[1]:=#67;',
  2944. ' doit:=#68;',
  2945. ' doit[2]:=#69;',
  2946. ' end;',
  2947. 'begin',
  2948. ' doit:=#70;',
  2949. ' doit[3]:=#71;',
  2950. 'end;',
  2951. 'begin']);
  2952. ConvertProgram;
  2953. CheckSource('TestNestedProc_ResultString',
  2954. LinesToStr([ // statements
  2955. 'this.DoIt = function () {',
  2956. ' var Result = "";',
  2957. ' function Nesty() {',
  2958. ' var Result$1 = "";',
  2959. ' Result$1 = "AB";',
  2960. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  2961. ' Result = "D";',
  2962. ' Result = rtl.setCharAt(Result, 1, "E");',
  2963. ' return Result$1;',
  2964. ' };',
  2965. ' Result = "F";',
  2966. ' Result = rtl.setCharAt(Result, 2, "G");',
  2967. ' return Result;',
  2968. '};'
  2969. ]),
  2970. '');
  2971. end;
  2972. procedure TTestModule.TestForwardProc;
  2973. begin
  2974. StartProgram(false);
  2975. Add('procedure FuncA(Bar: longint); forward;');
  2976. Add('procedure FuncB(Bar: longint);');
  2977. Add('begin');
  2978. Add(' funca(bar);');
  2979. Add('end;');
  2980. Add('procedure funca(bar: longint);');
  2981. Add('begin');
  2982. Add(' if bar=3 then ;');
  2983. Add('end;');
  2984. Add('begin');
  2985. Add(' funca(4);');
  2986. Add(' funcb(5);');
  2987. ConvertProgram;
  2988. CheckSource('TestForwardProc',
  2989. LinesToStr([ // statements'
  2990. 'this.FuncB = function (Bar) {',
  2991. ' $mod.FuncA(Bar);',
  2992. '};',
  2993. 'this.FuncA = function (Bar) {',
  2994. ' if (Bar === 3);',
  2995. '};'
  2996. ]),
  2997. LinesToStr([
  2998. '$mod.FuncA(4);',
  2999. '$mod.FuncB(5);'
  3000. ])
  3001. );
  3002. end;
  3003. procedure TTestModule.TestNestedForwardProc;
  3004. begin
  3005. StartProgram(false);
  3006. Add('procedure FuncA;');
  3007. Add(' procedure FuncB(i: longint); forward;');
  3008. Add(' procedure FuncC(i: longint);');
  3009. Add(' begin');
  3010. Add(' funcb(i);');
  3011. Add(' end;');
  3012. Add(' procedure FuncB(i: longint);');
  3013. Add(' begin');
  3014. Add(' if i=3 then ;');
  3015. Add(' end;');
  3016. Add('begin');
  3017. Add(' funcc(4)');
  3018. Add('end;');
  3019. Add('begin');
  3020. Add(' funca;');
  3021. ConvertProgram;
  3022. CheckSource('TestNestedForwardProc',
  3023. LinesToStr([ // statements'
  3024. 'this.FuncA = function () {',
  3025. ' function FuncC(i) {',
  3026. ' FuncB(i);',
  3027. ' };',
  3028. ' function FuncB(i) {',
  3029. ' if (i === 3);',
  3030. ' };',
  3031. ' FuncC(4);',
  3032. '};'
  3033. ]),
  3034. LinesToStr([
  3035. '$mod.FuncA();'
  3036. ])
  3037. );
  3038. end;
  3039. procedure TTestModule.TestAssignFunctionResult;
  3040. begin
  3041. StartProgram(false);
  3042. Add('function Func1: longint;');
  3043. Add('begin');
  3044. Add('end;');
  3045. Add('var i: longint;');
  3046. Add('begin');
  3047. Add(' i:=func1();');
  3048. Add(' i:=func1()+func1();');
  3049. ConvertProgram;
  3050. CheckSource('TestAssignFunctionResult',
  3051. LinesToStr([ // statements
  3052. 'this.Func1 = function () {',
  3053. ' var Result = 0;',
  3054. ' return Result;',
  3055. '};',
  3056. 'this.i = 0;'
  3057. ]),
  3058. LinesToStr([
  3059. '$mod.i = $mod.Func1();',
  3060. '$mod.i = $mod.Func1() + $mod.Func1();'
  3061. ]));
  3062. end;
  3063. procedure TTestModule.TestFunctionResultInCondition;
  3064. begin
  3065. StartProgram(false);
  3066. Add('function Func1: longint;');
  3067. Add('begin');
  3068. Add('end;');
  3069. Add('function Func2: boolean;');
  3070. Add('begin');
  3071. Add('end;');
  3072. Add('var i: longint;');
  3073. Add('begin');
  3074. Add(' if func2 then ;');
  3075. Add(' if i=func1() then ;');
  3076. Add(' if i=func1 then ;');
  3077. ConvertProgram;
  3078. CheckSource('TestFunctionResultInCondition',
  3079. LinesToStr([ // statements
  3080. 'this.Func1 = function () {',
  3081. ' var Result = 0;',
  3082. ' return Result;',
  3083. '};',
  3084. 'this.Func2 = function () {',
  3085. ' var Result = false;',
  3086. ' return Result;',
  3087. '};',
  3088. 'this.i = 0;'
  3089. ]),
  3090. LinesToStr([
  3091. 'if ($mod.Func2());',
  3092. 'if ($mod.i === $mod.Func1());',
  3093. 'if ($mod.i === $mod.Func1());'
  3094. ]));
  3095. end;
  3096. procedure TTestModule.TestFunctionResultInForLoop;
  3097. begin
  3098. StartProgram(false);
  3099. Add([
  3100. 'function Func1(a: array of longint): longint;',
  3101. 'begin',
  3102. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  3103. ' for Result in a do if a[Result]=0 then exit;',
  3104. 'end;',
  3105. 'begin',
  3106. ' Func1([1,2,3])']);
  3107. ConvertProgram;
  3108. CheckSource('TestFunctionResultInForLoop',
  3109. LinesToStr([ // statements
  3110. 'this.Func1 = function (a) {',
  3111. ' var Result = 0;',
  3112. ' for (var $l1 = rtl.length(a) - 1; $l1 >= 0; $l1--) {',
  3113. ' Result = $l1;',
  3114. ' if (a[Result] === 0) return Result;',
  3115. ' };',
  3116. ' for (var $in2 = a, $l3 = 0, $end4 = rtl.length($in2) - 1; $l3 <= $end4; $l3++) {',
  3117. ' Result = $in2[$l3];',
  3118. ' if (a[Result] === 0) return Result;',
  3119. ' };',
  3120. ' return Result;',
  3121. '};',
  3122. '']),
  3123. LinesToStr([
  3124. '$mod.Func1([1, 2, 3]);'
  3125. ]));
  3126. end;
  3127. procedure TTestModule.TestFunctionResultInTypeCast;
  3128. begin
  3129. StartProgram(false);
  3130. Add([
  3131. 'function GetInt: longint;',
  3132. 'begin',
  3133. 'end;',
  3134. 'begin',
  3135. ' if Byte(GetInt)=0 then ;',
  3136. '']);
  3137. ConvertProgram;
  3138. CheckSource('TestFunctionResultInTypeCast',
  3139. LinesToStr([ // statements
  3140. 'this.GetInt = function () {',
  3141. ' var Result = 0;',
  3142. ' return Result;',
  3143. '};',
  3144. '']),
  3145. LinesToStr([
  3146. 'if (($mod.GetInt() & 255) === 0) ;'
  3147. ]));
  3148. end;
  3149. procedure TTestModule.TestExit;
  3150. begin
  3151. StartProgram(false);
  3152. Add('procedure ProcA;');
  3153. Add('begin');
  3154. Add(' exit;');
  3155. Add('end;');
  3156. Add('function FuncB: longint;');
  3157. Add('begin');
  3158. Add(' exit;');
  3159. Add(' exit(3);');
  3160. Add('end;');
  3161. Add('function FuncC: string;');
  3162. Add('begin');
  3163. Add(' exit;');
  3164. Add(' exit(''a'');');
  3165. Add(' exit(''abc'');');
  3166. Add('end;');
  3167. Add('begin');
  3168. Add(' exit;');
  3169. Add(' exit(1);');
  3170. ConvertProgram;
  3171. CheckSource('TestExit',
  3172. LinesToStr([ // statements
  3173. 'this.ProcA = function () {',
  3174. ' return;',
  3175. '};',
  3176. 'this.FuncB = function () {',
  3177. ' var Result = 0;',
  3178. ' return Result;',
  3179. ' return 3;',
  3180. ' return Result;',
  3181. '};',
  3182. 'this.FuncC = function () {',
  3183. ' var Result = "";',
  3184. ' return Result;',
  3185. ' return "a";',
  3186. ' return "abc";',
  3187. ' return Result;',
  3188. '};'
  3189. ]),
  3190. LinesToStr([
  3191. 'return;',
  3192. 'return 1;',
  3193. '']));
  3194. end;
  3195. procedure TTestModule.TestBreak;
  3196. begin
  3197. StartProgram(false);
  3198. Add([
  3199. 'var',
  3200. ' i: longint;',
  3201. 'begin',
  3202. ' repeat',
  3203. ' break;',
  3204. ' until true;',
  3205. ' while true do',
  3206. ' break;',
  3207. ' for i:=1 to 2 do',
  3208. ' break;']);
  3209. ConvertProgram;
  3210. CheckSource('TestBreak',
  3211. LinesToStr([ // statements
  3212. 'this.i = 0;'
  3213. ]),
  3214. LinesToStr([
  3215. 'do {',
  3216. ' break;',
  3217. '} while (!true);',
  3218. 'while (true) break;',
  3219. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  3220. '']));
  3221. end;
  3222. procedure TTestModule.TestBreakAsVar;
  3223. begin
  3224. StartProgram(false);
  3225. Add([
  3226. 'procedure DoIt(break: boolean);',
  3227. 'begin',
  3228. ' if break then ;',
  3229. 'end;',
  3230. 'var',
  3231. ' break: boolean;',
  3232. 'begin',
  3233. ' if break then ;']);
  3234. ConvertProgram;
  3235. CheckSource('TestBreakAsVar',
  3236. LinesToStr([ // statements
  3237. 'this.DoIt = function (Break) {',
  3238. ' if (Break) ;',
  3239. '};',
  3240. 'this.Break = false;',
  3241. '']),
  3242. LinesToStr([
  3243. 'if($mod.Break) ;',
  3244. '']));
  3245. end;
  3246. procedure TTestModule.TestContinue;
  3247. begin
  3248. StartProgram(false);
  3249. Add('var i: longint;');
  3250. Add('begin');
  3251. Add(' repeat');
  3252. Add(' continue;');
  3253. Add(' until true;');
  3254. Add(' while true do');
  3255. Add(' continue;');
  3256. Add(' for i:=1 to 2 do');
  3257. Add(' continue;');
  3258. ConvertProgram;
  3259. CheckSource('TestContinue',
  3260. LinesToStr([ // statements
  3261. 'this.i = 0;'
  3262. ]),
  3263. LinesToStr([
  3264. 'do {',
  3265. ' continue;',
  3266. '} while (!true);',
  3267. 'while (true) continue;',
  3268. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  3269. '']));
  3270. end;
  3271. procedure TTestModule.TestProc_External;
  3272. begin
  3273. StartProgram(false);
  3274. Add('procedure Foo; external name ''console.log'';');
  3275. Add('function Bar: longint; external name ''get.item'';');
  3276. Add('function Bla(s: string): longint; external name ''apply.something'';');
  3277. Add('var');
  3278. Add(' i: longint;');
  3279. Add('begin');
  3280. Add(' Foo;');
  3281. Add(' i:=Bar;');
  3282. Add(' i:=Bla(''abc'');');
  3283. ConvertProgram;
  3284. CheckSource('TestProc_External',
  3285. LinesToStr([ // statements
  3286. 'this.i = 0;'
  3287. ]),
  3288. LinesToStr([
  3289. 'console.log();',
  3290. '$mod.i = get.item();',
  3291. '$mod.i = apply.something("abc");'
  3292. ]));
  3293. end;
  3294. procedure TTestModule.TestProc_ExternalOtherUnit;
  3295. begin
  3296. AddModuleWithIntfImplSrc('unit2.pas',
  3297. LinesToStr([
  3298. 'procedure Now; external name ''Date.now'';',
  3299. 'procedure DoIt;'
  3300. ]),
  3301. 'procedure doit; begin end;');
  3302. StartUnit(true);
  3303. Add('interface');
  3304. Add('uses unit2;');
  3305. Add('implementation');
  3306. Add('begin');
  3307. Add(' now;');
  3308. Add(' now();');
  3309. Add(' uNit2.now;');
  3310. Add(' uNit2.now();');
  3311. Add(' doit;');
  3312. Add(' uNit2.doit;');
  3313. ConvertUnit;
  3314. CheckSource('TestProc_ExternalOtherUnit',
  3315. LinesToStr([
  3316. '']),
  3317. LinesToStr([
  3318. 'Date.now();',
  3319. 'Date.now();',
  3320. 'Date.now();',
  3321. 'Date.now();',
  3322. 'pas.unit2.DoIt();',
  3323. 'pas.unit2.DoIt();',
  3324. '']));
  3325. end;
  3326. procedure TTestModule.TestProc_Asm;
  3327. begin
  3328. StartProgram(false);
  3329. Add([
  3330. 'function DoIt: longint;',
  3331. 'begin;',
  3332. ' asm',
  3333. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3334. ' end;',
  3335. ' asm console.log(); end;',
  3336. ' asm',
  3337. ' s = "'' ";',
  3338. ' s = ''" '';',
  3339. ' s = s + "world" + "''";',
  3340. ' // end',
  3341. ' s = ''end'';',
  3342. ' s = "end";',
  3343. ' end;',
  3344. 'end;',
  3345. 'begin']);
  3346. ConvertProgram;
  3347. CheckSource('TestProc_Asm',
  3348. LinesToStr([ // statements
  3349. 'this.DoIt = function () {',
  3350. ' var Result = 0;',
  3351. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3352. ' console.log();',
  3353. ' s = "'' ";',
  3354. ' s = ''" '';',
  3355. ' s = s + "world" + "''";',
  3356. ' // end',
  3357. ' s = ''end'';',
  3358. ' s = "end";',
  3359. ' return Result;',
  3360. '};'
  3361. ]),
  3362. LinesToStr([
  3363. ''
  3364. ]));
  3365. end;
  3366. procedure TTestModule.TestProc_Assembler;
  3367. begin
  3368. StartProgram(false);
  3369. Add('function DoIt: longint; assembler;');
  3370. Add('asm');
  3371. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  3372. Add('end;');
  3373. Add('begin');
  3374. ConvertProgram;
  3375. CheckSource('TestProc_Assembler',
  3376. LinesToStr([ // statements
  3377. 'this.DoIt = function () {',
  3378. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3379. '};'
  3380. ]),
  3381. LinesToStr([
  3382. ''
  3383. ]));
  3384. end;
  3385. procedure TTestModule.TestProc_VarParam;
  3386. begin
  3387. StartProgram(false);
  3388. Add('type integer = longint;');
  3389. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  3390. Add('var vJ: integer;');
  3391. Add('begin');
  3392. Add(' vg:=vg+1;');
  3393. Add(' vj:=vh+2;');
  3394. Add(' vi:=vi+3;');
  3395. Add(' doit(vg,vg,vg);');
  3396. Add(' doit(vh,vh,vj);');
  3397. Add(' doit(vi,vi,vi);');
  3398. Add(' doit(vj,vj,vj);');
  3399. Add('end;');
  3400. Add('var i: integer;');
  3401. Add('begin');
  3402. Add(' doit(i,i,i);');
  3403. ConvertProgram;
  3404. CheckSource('TestProc_VarParam',
  3405. LinesToStr([ // statements
  3406. 'this.DoIt = function (vG,vH,vI) {',
  3407. ' var vJ = 0;',
  3408. ' vG = vG + 1;',
  3409. ' vJ = vH + 2;',
  3410. ' vI.set(vI.get()+3);',
  3411. ' $mod.DoIt(vG, vG, {',
  3412. ' get: function () {',
  3413. ' return vG;',
  3414. ' },',
  3415. ' set: function (v) {',
  3416. ' vG = v;',
  3417. ' }',
  3418. ' });',
  3419. ' $mod.DoIt(vH, vH, {',
  3420. ' get: function () {',
  3421. ' return vJ;',
  3422. ' },',
  3423. ' set: function (v) {',
  3424. ' vJ = v;',
  3425. ' }',
  3426. ' });',
  3427. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  3428. ' $mod.DoIt(vJ, vJ, {',
  3429. ' get: function () {',
  3430. ' return vJ;',
  3431. ' },',
  3432. ' set: function (v) {',
  3433. ' vJ = v;',
  3434. ' }',
  3435. ' });',
  3436. '};',
  3437. 'this.i = 0;'
  3438. ]),
  3439. LinesToStr([
  3440. '$mod.DoIt($mod.i,$mod.i,{',
  3441. ' p: $mod,',
  3442. ' get: function () {',
  3443. ' return this.p.i;',
  3444. ' },',
  3445. ' set: function (v) {',
  3446. ' this.p.i = v;',
  3447. ' }',
  3448. '});'
  3449. ]));
  3450. end;
  3451. procedure TTestModule.TestProc_VarParamString;
  3452. begin
  3453. StartProgram(false);
  3454. Add(['type TCaption = string;',
  3455. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  3456. 'var c: char;',
  3457. 'begin',
  3458. ' va[1]:=c;',
  3459. ' vb[2]:=c;',
  3460. ' vc[3]:=c;',
  3461. 'end;',
  3462. 'begin']);
  3463. ConvertProgram;
  3464. CheckSource('TestProc_VarParamString',
  3465. LinesToStr([ // statements
  3466. 'this.DoIt = function (vA,vB,vC) {',
  3467. ' var c = "";',
  3468. ' vA = rtl.setCharAt(vA, 0, c);',
  3469. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  3470. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  3471. '};',
  3472. '']),
  3473. LinesToStr([
  3474. ]));
  3475. end;
  3476. procedure TTestModule.TestProc_VarParamV;
  3477. begin
  3478. StartProgram(false);
  3479. Add([
  3480. 'procedure Inc2(var i: longint);',
  3481. 'begin',
  3482. ' i:=i+2;',
  3483. 'end;',
  3484. 'procedure DoIt(v: longint);',
  3485. 'var p: array of longint;',
  3486. 'begin',
  3487. ' Inc2(v);',
  3488. ' Inc2(p[v]);',
  3489. 'end;',
  3490. 'begin']);
  3491. ConvertProgram;
  3492. CheckSource('TestProc_VarParamV',
  3493. LinesToStr([ // statements
  3494. 'this.Inc2 = function (i) {',
  3495. ' i.set(i.get()+2);',
  3496. '};',
  3497. 'this.DoIt = function (v) {',
  3498. ' var p = [];',
  3499. ' $mod.Inc2({get: function () {',
  3500. ' return v;',
  3501. ' }, set: function (w) {',
  3502. ' v = w;',
  3503. ' }});',
  3504. ' $mod.Inc2({',
  3505. ' a: v,',
  3506. ' p: p,',
  3507. ' get: function () {',
  3508. ' return this.p[this.a];',
  3509. ' },',
  3510. ' set: function (v) {',
  3511. ' this.p[this.a] = v;',
  3512. ' }',
  3513. ' });',
  3514. '};',
  3515. '']),
  3516. LinesToStr([
  3517. '']));
  3518. end;
  3519. procedure TTestModule.TestProc_Overload;
  3520. begin
  3521. StartProgram(false);
  3522. Add('procedure DoIt(vI: longint); begin end;');
  3523. Add('procedure DoIt(vI, vJ: longint); begin end;');
  3524. Add('procedure DoIt(vD: double); begin end;');
  3525. Add('begin');
  3526. Add(' DoIt(1);');
  3527. Add(' DoIt(2,3);');
  3528. Add(' DoIt(4.5);');
  3529. ConvertProgram;
  3530. CheckSource('TestProcedureOverload',
  3531. LinesToStr([ // statements
  3532. 'this.DoIt = function (vI) {',
  3533. '};',
  3534. 'this.DoIt$1 = function (vI, vJ) {',
  3535. '};',
  3536. 'this.DoIt$2 = function (vD) {',
  3537. '};',
  3538. '']),
  3539. LinesToStr([
  3540. '$mod.DoIt(1);',
  3541. '$mod.DoIt$1(2, 3);',
  3542. '$mod.DoIt$2(4.5);',
  3543. '']));
  3544. end;
  3545. procedure TTestModule.TestProc_OverloadForward;
  3546. begin
  3547. StartProgram(false);
  3548. Add('procedure DoIt(vI: longint); forward;');
  3549. Add('procedure DoIt(vI, vJ: longint); begin end;');
  3550. Add('procedure doit(vi: longint); begin end;');
  3551. Add('begin');
  3552. Add(' doit(1);');
  3553. Add(' doit(2,3);');
  3554. ConvertProgram;
  3555. CheckSource('TestProcedureOverloadForward',
  3556. LinesToStr([ // statements
  3557. 'this.DoIt$1 = function (vI, vJ) {',
  3558. '};',
  3559. 'this.DoIt = function (vI) {',
  3560. '};',
  3561. '']),
  3562. LinesToStr([
  3563. '$mod.DoIt(1);',
  3564. '$mod.DoIt$1(2, 3);',
  3565. '']));
  3566. end;
  3567. procedure TTestModule.TestProc_OverloadIntfImpl;
  3568. begin
  3569. StartUnit(false);
  3570. Add('interface');
  3571. Add('procedure DoIt(vI: longint);');
  3572. Add('procedure DoIt(vI, vJ: longint);');
  3573. Add('implementation');
  3574. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  3575. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  3576. Add('procedure DoIt(vi: longint); begin end;');
  3577. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  3578. Add('procedure DoIt(vi, vj: longint); begin end;');
  3579. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  3580. Add('begin');
  3581. Add(' doit(1);');
  3582. Add(' doit(2,3);');
  3583. Add(' doit(4,5,6);');
  3584. Add(' doit(7,8,9,10);');
  3585. Add(' doit(11,12,13,14,15);');
  3586. ConvertUnit;
  3587. CheckSource('TestProcedureOverloadUnit',
  3588. LinesToStr([ // statements
  3589. 'var $impl = $mod.$impl;',
  3590. 'this.DoIt = function (vI) {',
  3591. '};',
  3592. 'this.DoIt$1 = function (vI, vJ) {',
  3593. '};',
  3594. '']),
  3595. LinesToStr([ // this.$init
  3596. '$mod.DoIt(1);',
  3597. '$mod.DoIt$1(2, 3);',
  3598. '$impl.DoIt$3(4,5,6);',
  3599. '$impl.DoIt$4(7,8,9,10);',
  3600. '$impl.DoIt$2(11,12,13,14,15);',
  3601. '']),
  3602. LinesToStr([ // implementation
  3603. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  3604. '};',
  3605. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  3606. '};',
  3607. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  3608. '};',
  3609. '']));
  3610. end;
  3611. procedure TTestModule.TestProc_OverloadNested;
  3612. begin
  3613. StartProgram(false);
  3614. Add([
  3615. 'procedure DoIt(vA: longint); overload; forward;',
  3616. 'procedure DoIt(vB, vC: longint); overload;',
  3617. 'begin // 2 param overload',
  3618. ' doit(1);',
  3619. ' doit(1,2);',
  3620. 'end;',
  3621. 'procedure doit(vA: longint);',
  3622. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  3623. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  3624. ' begin // 4 param overload',
  3625. ' doit(1);',
  3626. ' doit(1,2);',
  3627. ' doit(1,2,3);',
  3628. ' doit(1,2,3,4);',
  3629. ' end;',
  3630. ' procedure doit(vA, vB, vC: longint);',
  3631. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  3632. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  3633. ' begin // 6 param overload',
  3634. ' doit(1);',
  3635. ' doit(1,2);',
  3636. ' doit(1,2,3);',
  3637. ' doit(1,2,3,4);',
  3638. ' doit(1,2,3,4,5);',
  3639. ' doit(1,2,3,4,5,6);',
  3640. ' end;',
  3641. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  3642. ' begin // 5 param overload',
  3643. ' doit(1);',
  3644. ' doit(1,2);',
  3645. ' doit(1,2,3);',
  3646. ' doit(1,2,3,4);',
  3647. ' doit(1,2,3,4,5);',
  3648. ' doit(1,2,3,4,5,6);',
  3649. ' end;',
  3650. ' begin // 3 param overload',
  3651. ' doit(1);',
  3652. ' doit(1,2);',
  3653. ' doit(1,2,3);',
  3654. ' doit(1,2,3,4);',
  3655. ' doit(1,2,3,4,5);',
  3656. ' doit(1,2,3,4,5,6);',
  3657. ' end;',
  3658. 'begin // 1 param overload',
  3659. ' doit(1);',
  3660. ' doit(1,2);',
  3661. ' doit(1,2,3);',
  3662. ' doit(1,2,3,4);',
  3663. 'end;',
  3664. 'begin // main',
  3665. ' doit(1);',
  3666. ' doit(1,2);']);
  3667. ConvertProgram;
  3668. CheckSource('TestProcedureOverloadNested',
  3669. LinesToStr([ // statements
  3670. 'this.DoIt$1 = function (vB, vC) {',
  3671. ' $mod.DoIt(1);',
  3672. ' $mod.DoIt$1(1, 2);',
  3673. '};',
  3674. 'this.DoIt = function (vA) {',
  3675. ' function DoIt$3(vA, vB, vC, vD) {',
  3676. ' $mod.DoIt(1);',
  3677. ' $mod.DoIt$1(1, 2);',
  3678. ' DoIt$2(1, 2, 3);',
  3679. ' DoIt$3(1, 2, 3, 4);',
  3680. ' };',
  3681. ' function DoIt$2(vA, vB, vC) {',
  3682. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  3683. ' $mod.DoIt(1);',
  3684. ' $mod.DoIt$1(1, 2);',
  3685. ' DoIt$2(1, 2, 3);',
  3686. ' DoIt$3(1, 2, 3, 4);',
  3687. ' DoIt$4(1, 2, 3, 4, 5);',
  3688. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  3689. ' };',
  3690. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  3691. ' $mod.DoIt(1);',
  3692. ' $mod.DoIt$1(1, 2);',
  3693. ' DoIt$2(1, 2, 3);',
  3694. ' DoIt$3(1, 2, 3, 4);',
  3695. ' DoIt$4(1, 2, 3, 4, 5);',
  3696. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  3697. ' };',
  3698. ' $mod.DoIt(1);',
  3699. ' $mod.DoIt$1(1, 2);',
  3700. ' DoIt$2(1, 2, 3);',
  3701. ' DoIt$3(1, 2, 3, 4);',
  3702. ' DoIt$4(1, 2, 3, 4, 5);',
  3703. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  3704. ' };',
  3705. ' $mod.DoIt(1);',
  3706. ' $mod.DoIt$1(1, 2);',
  3707. ' DoIt$2(1, 2, 3);',
  3708. ' DoIt$3(1, 2, 3, 4);',
  3709. '};',
  3710. '']),
  3711. LinesToStr([
  3712. '$mod.DoIt(1);',
  3713. '$mod.DoIt$1(1, 2);',
  3714. '']));
  3715. end;
  3716. procedure TTestModule.TestProc_OverloadUnitCycle;
  3717. begin
  3718. AddModuleWithIntfImplSrc('Unit2.pas',
  3719. LinesToStr([
  3720. 'type',
  3721. ' TObject = class',
  3722. ' procedure DoIt(b: boolean); virtual; abstract;',
  3723. ' procedure DoIt(i: longint); virtual; abstract;',
  3724. ' end;',
  3725. '']),
  3726. 'uses test1;');
  3727. StartUnit(true);
  3728. Add([
  3729. 'interface',
  3730. 'uses unit2;',
  3731. 'type',
  3732. ' TEagle = class(TObject)',
  3733. ' procedure DoIt(b: boolean); override;',
  3734. ' procedure DoIt(i: longint); override;',
  3735. ' end;',
  3736. 'implementation',
  3737. 'procedure TEagle.DoIt(b: boolean); begin end;',
  3738. 'procedure TEagle.DoIt(i: longint); begin end;',
  3739. '']);
  3740. ConvertUnit;
  3741. CheckSource('TestProc_OverloadUnitCycle',
  3742. LinesToStr([ // statements
  3743. 'rtl.createClass($mod, "TEagle", pas.Unit2.TObject, function () {',
  3744. ' this.DoIt = function (b) {',
  3745. ' };',
  3746. ' this.DoIt$1 = function (i) {',
  3747. ' };',
  3748. '});',
  3749. '']),
  3750. '',
  3751. LinesToStr([
  3752. '']));
  3753. end;
  3754. procedure TTestModule.TestProc_Varargs;
  3755. begin
  3756. StartProgram(false);
  3757. Add([
  3758. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  3759. 'procedure ProcB; varargs; external name ''ProcB'';',
  3760. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  3761. 'function GetIt: longint; begin end;',
  3762. 'begin',
  3763. ' ProcA(1);',
  3764. ' ProcA(1,2);',
  3765. ' ProcA(1,2.0);',
  3766. ' ProcA(1,2,3);',
  3767. ' ProcA(1,''2'');',
  3768. ' ProcA(2,'''');',
  3769. ' ProcA(3,false);',
  3770. ' ProcB;',
  3771. ' ProcB();',
  3772. ' ProcB(4);',
  3773. ' ProcB(''foo'');',
  3774. ' ProcC;',
  3775. ' ProcC();',
  3776. ' ProcC(4);',
  3777. ' ProcC(5,''foo'');',
  3778. ' ProcB(GetIt);',
  3779. ' ProcB(GetIt());',
  3780. ' ProcB(GetIt,GetIt());']);
  3781. ConvertProgram;
  3782. CheckSource('TestProc_Varargs',
  3783. LinesToStr([ // statements
  3784. 'this.GetIt = function () {',
  3785. ' var Result = 0;',
  3786. ' return Result;',
  3787. '};',
  3788. '']),
  3789. LinesToStr([
  3790. 'ProcA(1);',
  3791. 'ProcA(1, 2);',
  3792. 'ProcA(1, 2.0);',
  3793. 'ProcA(1, 2, 3);',
  3794. 'ProcA(1, "2");',
  3795. 'ProcA(2, "");',
  3796. 'ProcA(3, false);',
  3797. 'ProcB();',
  3798. 'ProcB();',
  3799. 'ProcB(4);',
  3800. 'ProcB("foo");',
  3801. 'ProcC(17);',
  3802. 'ProcC(17);',
  3803. 'ProcC(4);',
  3804. 'ProcC(5, "foo");',
  3805. 'ProcB($mod.GetIt());',
  3806. 'ProcB($mod.GetIt());',
  3807. 'ProcB($mod.GetIt(), $mod.GetIt());',
  3808. '']));
  3809. end;
  3810. procedure TTestModule.TestProc_ConstOrder;
  3811. begin
  3812. StartProgram(false);
  3813. Add([
  3814. 'const A = 3;',
  3815. 'const B = A+1;',
  3816. 'procedure DoIt;',
  3817. 'const C = A+1;',
  3818. 'const D = B+1;',
  3819. 'const E = D+C+B+A;',
  3820. 'begin',
  3821. 'end;',
  3822. 'begin'
  3823. ]);
  3824. ConvertProgram;
  3825. CheckSource('TestProc_ConstOrder',
  3826. LinesToStr([ // statements
  3827. 'this.A = 3;',
  3828. 'this.B = 3 + 1;',
  3829. 'var C = 3 + 1;',
  3830. 'var D = 4 + 1;',
  3831. 'var E = 5 + 4 + 4 + 3;',
  3832. 'this.DoIt = function () {',
  3833. '};',
  3834. '']),
  3835. LinesToStr([
  3836. ''
  3837. ]));
  3838. end;
  3839. procedure TTestModule.TestProc_DuplicateConst;
  3840. begin
  3841. StartProgram(false);
  3842. Add([
  3843. 'const A = 1;',
  3844. 'procedure DoIt;',
  3845. 'const A = 2;',
  3846. ' procedure SubIt;',
  3847. ' const A = 21;',
  3848. ' begin',
  3849. ' end;',
  3850. 'begin',
  3851. 'end;',
  3852. 'procedure DoSome;',
  3853. 'const A = 3;',
  3854. 'begin',
  3855. 'end;',
  3856. 'begin'
  3857. ]);
  3858. ConvertProgram;
  3859. CheckSource('TestProc_DuplicateConst',
  3860. LinesToStr([ // statements
  3861. 'this.A = 1;',
  3862. 'var A$1 = 2;',
  3863. 'var A$2 = 21;',
  3864. 'this.DoIt = function () {',
  3865. ' function SubIt() {',
  3866. ' };',
  3867. '};',
  3868. 'var A$3 = 3;',
  3869. 'this.DoSome = function () {',
  3870. '};',
  3871. '']),
  3872. LinesToStr([
  3873. ''
  3874. ]));
  3875. end;
  3876. procedure TTestModule.TestProc_LocalVarAbsolute;
  3877. begin
  3878. StartProgram(false);
  3879. Add([
  3880. 'type',
  3881. ' TObject = class',
  3882. ' Index: longint;',
  3883. ' procedure DoAbs(Item: pointer);',
  3884. ' end;',
  3885. 'procedure TObject.DoAbs(Item: pointer);',
  3886. 'var',
  3887. ' o: TObject absolute Item;',
  3888. 'begin',
  3889. ' if o.Index<o.Index then o.Index:=o.Index;',
  3890. 'end;',
  3891. 'procedure DoIt(i: longint; p: pointer);',
  3892. 'var',
  3893. ' d: double absolute i;',
  3894. ' s: string absolute d;',
  3895. ' oi: TObject absolute i;',
  3896. ' op: TObject absolute p;',
  3897. 'begin',
  3898. ' if d=d then d:=d;',
  3899. ' if s=s then s:=s;',
  3900. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  3901. ' if op.Index=op.Index then op.Index:=op.Index;',
  3902. 'end;',
  3903. 'begin']);
  3904. ConvertProgram;
  3905. CheckSource('TestProc_LocalVarAbsolute',
  3906. LinesToStr([ // statements
  3907. 'rtl.createClass($mod, "TObject", null, function () {',
  3908. ' this.$init = function () {',
  3909. ' this.Index = 0;',
  3910. ' };',
  3911. ' this.$final = function () {',
  3912. ' };',
  3913. ' this.DoAbs = function (Item) {',
  3914. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  3915. ' };',
  3916. '});',
  3917. 'this.DoIt = function (i, p) {',
  3918. ' if (i === i) i = i;',
  3919. ' if (i === i) i = i;',
  3920. ' if (i.Index < i.Index) i.Index = i.Index;',
  3921. ' if (p.Index === p.Index) p.Index = p.Index;',
  3922. '};'
  3923. ]),
  3924. LinesToStr([
  3925. ]));
  3926. end;
  3927. procedure TTestModule.TestProc_ReservedWords;
  3928. begin
  3929. StartProgram(false);
  3930. Add([
  3931. 'procedure Date(ArrayBuffer: longint);',
  3932. 'const',
  3933. ' NaN: longint = 3;',
  3934. 'var',
  3935. ' &Boolean: longint;',
  3936. ' procedure Error(ArrayBuffer: longint);',
  3937. ' begin',
  3938. ' end;',
  3939. 'begin',
  3940. ' Nan:=&bOolean;',
  3941. 'end;',
  3942. 'begin',
  3943. ' Date(1);']);
  3944. ConvertProgram;
  3945. CheckSource('TestProc_ReservedWords',
  3946. LinesToStr([ // statements
  3947. 'var naN = 3;',
  3948. 'this.Date = function (arrayBuffer) {',
  3949. ' var boolean = 0;',
  3950. ' function error(arrayBuffer) {',
  3951. ' };',
  3952. ' naN = boolean;',
  3953. '};',
  3954. '']),
  3955. LinesToStr([
  3956. ' $mod.Date(1);'
  3957. ]));
  3958. end;
  3959. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  3960. begin
  3961. StartProgram(false);
  3962. Add([
  3963. '{$mode objfpc}',
  3964. 'type',
  3965. ' TFunc = reference to function(x: word): word;',
  3966. 'var Func: TFunc;',
  3967. 'procedure DoIt(a: word);',
  3968. 'begin',
  3969. ' Func:=function(b:word): word',
  3970. ' begin',
  3971. ' Result:=a+b;',
  3972. ' exit(b);',
  3973. ' exit(Result);',
  3974. ' end;',// test semicolon
  3975. ' a:=3;',
  3976. 'end;',
  3977. 'begin',
  3978. ' Func:=function(c:word):word begin',
  3979. ' Result:=3+c;',
  3980. ' exit(c);',
  3981. ' exit(Result);',
  3982. ' end;']);
  3983. ConvertProgram;
  3984. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  3985. LinesToStr([ // statements
  3986. 'this.Func = null;',
  3987. 'this.DoIt = function (a) {',
  3988. ' $mod.Func = function (b) {',
  3989. ' var Result = 0;',
  3990. ' Result = a + b;',
  3991. ' return b;',
  3992. ' return Result;',
  3993. ' return Result;',
  3994. ' };',
  3995. ' a = 3;',
  3996. '};',
  3997. '']),
  3998. LinesToStr([
  3999. '$mod.Func = function (c) {',
  4000. ' var Result = 0;',
  4001. ' Result = 3 + c;',
  4002. ' return c;',
  4003. ' return Result;',
  4004. ' return Result;',
  4005. '};',
  4006. '']));
  4007. end;
  4008. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  4009. begin
  4010. StartProgram(false);
  4011. Add([
  4012. '{$mode delphi}',
  4013. 'type',
  4014. ' TProc = reference to procedure(x: word);',
  4015. 'procedure DoIt(a: word);',
  4016. 'var Proc: TProc;',
  4017. 'begin',
  4018. ' Proc:=procedure(b:word) begin end;',
  4019. 'end;',
  4020. 'var Proc: TProc;',
  4021. 'begin',
  4022. ' Proc:=procedure(c:word) begin end;',
  4023. '']);
  4024. ConvertProgram;
  4025. CheckSource('TestAnonymousProc_Assign_Delphi',
  4026. LinesToStr([ // statements
  4027. 'this.DoIt = function (a) {',
  4028. ' var Proc = null;',
  4029. ' Proc = function (b) {',
  4030. ' };',
  4031. '};',
  4032. 'this.Proc = null;',
  4033. '']),
  4034. LinesToStr([
  4035. '$mod.Proc = function (c) {',
  4036. '};',
  4037. '']));
  4038. end;
  4039. procedure TTestModule.TestAnonymousProc_Arg;
  4040. begin
  4041. StartProgram(false);
  4042. Add([
  4043. 'type',
  4044. ' TProc = reference to procedure;',
  4045. ' TFunc = reference to function(x: word): word;',
  4046. 'procedure DoMore(f,g: TProc);',
  4047. 'begin',
  4048. 'end;',
  4049. 'procedure DoOdd(v: jsvalue);',
  4050. 'begin',
  4051. 'end;',
  4052. 'procedure DoIt(f: TFunc);',
  4053. 'begin',
  4054. ' DoIt(function(b:word): word',
  4055. ' begin',
  4056. ' Result:=1+b;',
  4057. ' end);',
  4058. ' DoMore(procedure begin end, procedure begin end);',
  4059. ' DoOdd(procedure begin end);',
  4060. 'end;',
  4061. 'begin',
  4062. ' DoMore(procedure begin end,',
  4063. ' procedure assembler asm',
  4064. ' console.log("c");',
  4065. ' end);',
  4066. '']);
  4067. ConvertProgram;
  4068. CheckSource('TestAnonymousProc_Arg',
  4069. LinesToStr([ // statements
  4070. 'this.DoMore = function (f, g) {',
  4071. '};',
  4072. 'this.DoOdd = function (v) {',
  4073. '};',
  4074. 'this.DoIt = function (f) {',
  4075. ' $mod.DoIt(function (b) {',
  4076. ' var Result = 0;',
  4077. ' Result = 1 + b;',
  4078. ' return Result;',
  4079. ' });',
  4080. ' $mod.DoMore(function () {',
  4081. ' }, function () {',
  4082. ' });',
  4083. ' $mod.DoOdd(function () {',
  4084. ' });',
  4085. '};',
  4086. '']),
  4087. LinesToStr([
  4088. '$mod.DoMore(function () {',
  4089. '}, function () {',
  4090. ' console.log("c");',
  4091. '});',
  4092. '']));
  4093. end;
  4094. procedure TTestModule.TestAnonymousProc_Typecast;
  4095. begin
  4096. StartProgram(false);
  4097. Add([
  4098. 'type',
  4099. ' TProc = reference to procedure(w: word);',
  4100. ' TArr = array of word;',
  4101. ' TFuncArr = reference to function: TArr;',
  4102. 'procedure DoIt(p: TProc);',
  4103. 'var',
  4104. ' w: word;',
  4105. ' a: TArr;',
  4106. 'begin',
  4107. ' p:=TProc(procedure(b: smallint) begin end);',
  4108. ' a:=TFuncArr(function: TArr begin end)();',
  4109. ' w:=TFuncArr(function: TArr begin end)()[3];',
  4110. 'end;',
  4111. 'begin']);
  4112. ConvertProgram;
  4113. CheckSource('TestAnonymousProc_Typecast',
  4114. LinesToStr([ // statements
  4115. 'this.DoIt = function (p) {',
  4116. ' var w = 0;',
  4117. ' var a = [];',
  4118. ' p = function (b) {',
  4119. ' };',
  4120. ' a = function () {',
  4121. ' var Result = [];',
  4122. ' return Result;',
  4123. ' }();',
  4124. ' w = function () {',
  4125. ' var Result = [];',
  4126. ' return Result;',
  4127. ' }()[3];',
  4128. '};',
  4129. '']),
  4130. LinesToStr([
  4131. '']));
  4132. end;
  4133. procedure TTestModule.TestAnonymousProc_With;
  4134. begin
  4135. StartProgram(false);
  4136. Add([
  4137. 'type',
  4138. ' TProc = reference to procedure(w: word);',
  4139. ' TObject = class',
  4140. ' b: boolean;',
  4141. ' end;',
  4142. 'var',
  4143. ' p: TProc;',
  4144. ' bird: TObject;',
  4145. 'begin',
  4146. ' with bird do',
  4147. ' p:=procedure(w: word)',
  4148. ' begin',
  4149. ' b:=w>2;',
  4150. ' end;',
  4151. '']);
  4152. ConvertProgram;
  4153. CheckSource('TestAnonymousProc_With',
  4154. LinesToStr([ // statements
  4155. 'rtl.createClass($mod, "TObject", null, function () {',
  4156. ' this.$init = function () {',
  4157. ' this.b = false;',
  4158. ' };',
  4159. ' this.$final = function () {',
  4160. ' };',
  4161. '});',
  4162. 'this.p = null;',
  4163. 'this.bird = null;',
  4164. '']),
  4165. LinesToStr([
  4166. 'var $with1 = $mod.bird;',
  4167. '$mod.p = function (w) {',
  4168. ' $with1.b = w > 2;',
  4169. '};',
  4170. '']));
  4171. end;
  4172. procedure TTestModule.TestAnonymousProc_ExceptOn;
  4173. begin
  4174. StartProgram(false);
  4175. Add([
  4176. 'type',
  4177. ' TProc = reference to procedure;',
  4178. ' TObject = class',
  4179. ' b: boolean;',
  4180. ' end;',
  4181. 'procedure DoIt;',
  4182. 'var',
  4183. ' p: TProc;',
  4184. 'begin',
  4185. ' try',
  4186. ' except',
  4187. ' on E: TObject do',
  4188. ' p:=procedure',
  4189. ' begin',
  4190. ' E.b:=true;',
  4191. ' end;',
  4192. ' end;',
  4193. 'end;',
  4194. 'begin']);
  4195. ConvertProgram;
  4196. CheckSource('TestAnonymousProc_ExceptOn',
  4197. LinesToStr([ // statements
  4198. 'rtl.createClass($mod, "TObject", null, function () {',
  4199. ' this.$init = function () {',
  4200. ' this.b = false;',
  4201. ' };',
  4202. ' this.$final = function () {',
  4203. ' };',
  4204. '});',
  4205. 'this.DoIt = function () {',
  4206. ' var p = null;',
  4207. ' try {} catch ($e) {',
  4208. ' if ($mod.TObject.isPrototypeOf($e)) {',
  4209. ' var E = $e;',
  4210. ' p = function () {',
  4211. ' E.b = true;',
  4212. ' };',
  4213. ' } else throw $e',
  4214. ' };',
  4215. '};',
  4216. '']),
  4217. LinesToStr([
  4218. '']));
  4219. end;
  4220. procedure TTestModule.TestAnonymousProc_Nested;
  4221. begin
  4222. StartProgram(false);
  4223. Add([
  4224. 'type',
  4225. ' TProc = reference to procedure;',
  4226. ' TObject = class',
  4227. ' i: byte;',
  4228. ' procedure DoIt;',
  4229. ' end;',
  4230. 'procedure TObject.DoIt;',
  4231. 'var',
  4232. ' p: TProc;',
  4233. ' procedure Sub;',
  4234. ' begin',
  4235. ' p:=procedure',
  4236. ' begin',
  4237. ' i:=3;',
  4238. ' Self.i:=4;',
  4239. ' p:=procedure',
  4240. ' procedure SubSub;',
  4241. ' begin',
  4242. ' i:=13;',
  4243. ' Self.i:=14;',
  4244. ' end;',
  4245. ' begin',
  4246. ' i:=13;',
  4247. ' Self.i:=14;',
  4248. ' end;',
  4249. ' end;',
  4250. ' end;',
  4251. 'begin',
  4252. 'end;',
  4253. 'begin']);
  4254. ConvertProgram;
  4255. CheckSource('TestAnonymousProc_Nested',
  4256. LinesToStr([ // statements
  4257. 'rtl.createClass($mod, "TObject", null, function () {',
  4258. ' this.$init = function () {',
  4259. ' this.i = 0;',
  4260. ' };',
  4261. ' this.$final = function () {',
  4262. ' };',
  4263. ' this.DoIt = function () {',
  4264. ' var Self = this;',
  4265. ' var p = null;',
  4266. ' function Sub() {',
  4267. ' p = function () {',
  4268. ' Self.i = 3;',
  4269. ' Self.i = 4;',
  4270. ' p = function () {',
  4271. ' function SubSub() {',
  4272. ' Self.i = 13;',
  4273. ' Self.i = 14;',
  4274. ' };',
  4275. ' Self.i = 13;',
  4276. ' Self.i = 14;',
  4277. ' };',
  4278. ' };',
  4279. ' };',
  4280. ' };',
  4281. '});',
  4282. '']),
  4283. LinesToStr([
  4284. '']));
  4285. end;
  4286. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  4287. begin
  4288. StartProgram(false);
  4289. Add([
  4290. 'type',
  4291. ' TProc = reference to procedure;',
  4292. 'function DoIt: TProc;',
  4293. ' function Sub: TProc;',
  4294. ' begin',
  4295. ' Result:=procedure',
  4296. ' begin',
  4297. ' Sub:=procedure',
  4298. ' procedure SubSub;',
  4299. ' begin',
  4300. ' Result:=nil;',
  4301. ' Sub:=nil;',
  4302. ' DoIt:=nil;',
  4303. ' end;',
  4304. ' begin',
  4305. ' Result:=nil;',
  4306. ' Sub:=nil;',
  4307. ' DoIt:=nil;',
  4308. ' end;',
  4309. ' end;',
  4310. ' end;',
  4311. 'begin',
  4312. 'end;',
  4313. 'begin']);
  4314. ConvertProgram;
  4315. CheckSource('TestAnonymousProc_NestedAssignResult',
  4316. LinesToStr([ // statements
  4317. 'this.DoIt = function () {',
  4318. ' var Result = null;',
  4319. ' function Sub() {',
  4320. ' var Result$1 = null;',
  4321. ' Result$1 = function () {',
  4322. ' Result$1 = function () {',
  4323. ' function SubSub() {',
  4324. ' Result$1 = null;',
  4325. ' Result$1 = null;',
  4326. ' Result = null;',
  4327. ' };',
  4328. ' Result$1 = null;',
  4329. ' Result$1 = null;',
  4330. ' Result = null;',
  4331. ' };',
  4332. ' };',
  4333. ' return Result$1;',
  4334. ' };',
  4335. ' return Result;',
  4336. '};',
  4337. '']),
  4338. LinesToStr([
  4339. '']));
  4340. end;
  4341. procedure TTestModule.TestEnum_Name;
  4342. begin
  4343. StartProgram(false);
  4344. Add('type TMyEnum = (Red, Green, Blue);');
  4345. Add('var e: TMyEnum;');
  4346. Add('var f: TMyEnum = Blue;');
  4347. Add('begin');
  4348. Add(' e:=green;');
  4349. Add(' e:=default(TMyEnum);');
  4350. ConvertProgram;
  4351. CheckSource('TestEnumName',
  4352. LinesToStr([ // statements
  4353. 'this.TMyEnum = {',
  4354. ' "0":"Red",',
  4355. ' Red:0,',
  4356. ' "1":"Green",',
  4357. ' Green:1,',
  4358. ' "2":"Blue",',
  4359. ' Blue:2',
  4360. ' };',
  4361. 'this.e = 0;',
  4362. 'this.f = $mod.TMyEnum.Blue;'
  4363. ]),
  4364. LinesToStr([
  4365. '$mod.e=$mod.TMyEnum.Green;',
  4366. '$mod.e=$mod.TMyEnum.Red;'
  4367. ]));
  4368. end;
  4369. procedure TTestModule.TestEnum_Number;
  4370. begin
  4371. Converter.Options:=Converter.Options+[coEnumNumbers];
  4372. StartProgram(false);
  4373. Add('type TMyEnum = (Red, Green);');
  4374. Add('var');
  4375. Add(' e: TMyEnum;');
  4376. Add(' f: TMyEnum = Green;');
  4377. Add(' i: longint;');
  4378. Add('begin');
  4379. Add(' e:=green;');
  4380. Add(' i:=longint(e);');
  4381. ConvertProgram;
  4382. CheckSource('TestEnumNumber',
  4383. LinesToStr([ // statements
  4384. 'this.TMyEnum = {',
  4385. ' "0":"Red",',
  4386. ' Red:0,',
  4387. ' "1":"Green",',
  4388. ' Green:1',
  4389. ' };',
  4390. 'this.e = 0;',
  4391. 'this.f = 1;',
  4392. 'this.i = 0;'
  4393. ]),
  4394. LinesToStr([
  4395. '$mod.e=1;',
  4396. '$mod.i=$mod.e;'
  4397. ]));
  4398. end;
  4399. procedure TTestModule.TestEnum_ConstFail;
  4400. begin
  4401. StartProgram(false);
  4402. Add([
  4403. 'type TMyEnum = (Red = 100, Green = 101);',
  4404. 'var',
  4405. ' e: TMyEnum;',
  4406. ' f: TMyEnum = Green;',
  4407. 'begin',
  4408. ' e:=green;']);
  4409. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] enum const',3002);
  4410. ConvertProgram;
  4411. end;
  4412. procedure TTestModule.TestEnum_Functions;
  4413. begin
  4414. StartProgram(false);
  4415. Add([
  4416. 'type TMyEnum = (Red, Green);',
  4417. 'procedure DoIt(var e: TMyEnum; var i: word);',
  4418. 'var',
  4419. ' v: longint;',
  4420. ' s: string;',
  4421. 'begin',
  4422. ' val(s,e,v);',
  4423. ' val(s,e,i);',
  4424. 'end;',
  4425. 'var',
  4426. ' e: TMyEnum;',
  4427. ' i: longint;',
  4428. ' s: string;',
  4429. ' b: boolean;',
  4430. 'begin',
  4431. ' i:=ord(red);',
  4432. ' i:=ord(green);',
  4433. ' i:=ord(e);',
  4434. ' i:=ord(b);',
  4435. ' e:=low(tmyenum);',
  4436. ' e:=low(e);',
  4437. ' b:=low(boolean);',
  4438. ' e:=high(tmyenum);',
  4439. ' e:=high(e);',
  4440. ' b:=high(boolean);',
  4441. ' e:=pred(green);',
  4442. ' e:=pred(e);',
  4443. ' b:=pred(b);',
  4444. ' e:=succ(red);',
  4445. ' e:=succ(e);',
  4446. ' b:=succ(b);',
  4447. ' e:=tmyenum(1);',
  4448. ' e:=tmyenum(i);',
  4449. ' s:=str(e);',
  4450. ' str(e,s);',
  4451. ' str(red,s);',
  4452. ' s:=str(e:3);',
  4453. ' writestr(s,e:3,red);',
  4454. ' val(s,e,i);',
  4455. ' e:=TMyEnum(i);',
  4456. ' i:=longint(e);']);
  4457. ConvertProgram;
  4458. CheckSource('TestEnum_Functions',
  4459. LinesToStr([ // statements
  4460. 'this.TMyEnum = {',
  4461. ' "0":"Red",',
  4462. ' Red:0,',
  4463. ' "1":"Green",',
  4464. ' Green:1',
  4465. ' };',
  4466. 'this.DoIt = function (e, i) {',
  4467. ' var v = 0;',
  4468. ' var s = "";',
  4469. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  4470. ' v = w;',
  4471. ' }));',
  4472. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  4473. '};',
  4474. 'this.e = 0;',
  4475. 'this.i = 0;',
  4476. 'this.s = "";',
  4477. 'this.b = false;',
  4478. '']),
  4479. LinesToStr([
  4480. '$mod.i=$mod.TMyEnum.Red;',
  4481. '$mod.i=$mod.TMyEnum.Green;',
  4482. '$mod.i=$mod.e;',
  4483. '$mod.i=$mod.b+0;',
  4484. '$mod.e=$mod.TMyEnum.Red;',
  4485. '$mod.e=$mod.TMyEnum.Red;',
  4486. '$mod.b=false;',
  4487. '$mod.e=$mod.TMyEnum.Green;',
  4488. '$mod.e=$mod.TMyEnum.Green;',
  4489. '$mod.b=true;',
  4490. '$mod.e=$mod.TMyEnum.Green-1;',
  4491. '$mod.e=$mod.e-1;',
  4492. '$mod.b=false;',
  4493. '$mod.e=$mod.TMyEnum.Red+1;',
  4494. '$mod.e=$mod.e+1;',
  4495. '$mod.b=true;',
  4496. '$mod.e=1;',
  4497. '$mod.e=$mod.i;',
  4498. '$mod.s = $mod.TMyEnum[$mod.e];',
  4499. '$mod.s = $mod.TMyEnum[$mod.e];',
  4500. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  4501. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  4502. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  4503. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  4504. ' $mod.i = v;',
  4505. '});',
  4506. '$mod.e=$mod.i;',
  4507. '$mod.i=$mod.e;',
  4508. '']));
  4509. end;
  4510. procedure TTestModule.TestEnum_AsParams;
  4511. begin
  4512. StartProgram(false);
  4513. Add('type TEnum = (Red,Blue);');
  4514. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  4515. Add('var vJ: TEnum;');
  4516. Add('begin');
  4517. Add(' vg:=vg;');
  4518. Add(' vj:=vh;');
  4519. Add(' vi:=vi;');
  4520. Add(' doit(vg,vg,vg);');
  4521. Add(' doit(vh,vh,vj);');
  4522. Add(' doit(vi,vi,vi);');
  4523. Add(' doit(vj,vj,vj);');
  4524. Add('end;');
  4525. Add('var i: TEnum;');
  4526. Add('begin');
  4527. Add(' doit(i,i,i);');
  4528. ConvertProgram;
  4529. CheckSource('TestEnum_AsParams',
  4530. LinesToStr([ // statements
  4531. 'this.TEnum = {',
  4532. ' "0": "Red",',
  4533. ' Red: 0,',
  4534. ' "1": "Blue",',
  4535. ' Blue: 1',
  4536. '};',
  4537. 'this.DoIt = function (vG,vH,vI) {',
  4538. ' var vJ = 0;',
  4539. ' vG = vG;',
  4540. ' vJ = vH;',
  4541. ' vI.set(vI.get());',
  4542. ' $mod.DoIt(vG, vG, {',
  4543. ' get: function () {',
  4544. ' return vG;',
  4545. ' },',
  4546. ' set: function (v) {',
  4547. ' vG = v;',
  4548. ' }',
  4549. ' });',
  4550. ' $mod.DoIt(vH, vH, {',
  4551. ' get: function () {',
  4552. ' return vJ;',
  4553. ' },',
  4554. ' set: function (v) {',
  4555. ' vJ = v;',
  4556. ' }',
  4557. ' });',
  4558. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4559. ' $mod.DoIt(vJ, vJ, {',
  4560. ' get: function () {',
  4561. ' return vJ;',
  4562. ' },',
  4563. ' set: function (v) {',
  4564. ' vJ = v;',
  4565. ' }',
  4566. ' });',
  4567. '};',
  4568. 'this.i = 0;'
  4569. ]),
  4570. LinesToStr([
  4571. '$mod.DoIt($mod.i,$mod.i,{',
  4572. ' p: $mod,',
  4573. ' get: function () {',
  4574. ' return this.p.i;',
  4575. ' },',
  4576. ' set: function (v) {',
  4577. ' this.p.i = v;',
  4578. ' }',
  4579. '});'
  4580. ]));
  4581. end;
  4582. procedure TTestModule.TestEnumRange_Array;
  4583. begin
  4584. StartProgram(false);
  4585. Add([
  4586. 'type',
  4587. ' TEnum = (Red, Green, Blue);',
  4588. ' TEnumRg = green..blue;',
  4589. ' TArr = array[TEnumRg] of byte;',
  4590. ' TArr2 = array[green..blue] of byte;',
  4591. 'var',
  4592. ' a: TArr;',
  4593. ' b: TArr = (3,4);',
  4594. ' c: TArr2 = (5,6);',
  4595. 'begin',
  4596. ' a[green] := b[blue];',
  4597. ' c[green] := c[blue];',
  4598. '']);
  4599. ConvertProgram;
  4600. CheckSource('TestEnumRange_Array',
  4601. LinesToStr([ // statements
  4602. 'this.TEnum = {',
  4603. ' "0": "Red",',
  4604. ' Red: 0,',
  4605. ' "1": "Green",',
  4606. ' Green: 1,',
  4607. ' "2": "Blue",',
  4608. ' Blue: 2',
  4609. '};',
  4610. 'this.a = rtl.arraySetLength(null, 0, 2);',
  4611. 'this.b = [3, 4];',
  4612. 'this.c = [5, 6];',
  4613. '']),
  4614. LinesToStr([
  4615. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  4616. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  4617. '']));
  4618. end;
  4619. procedure TTestModule.TestEnum_ForIn;
  4620. begin
  4621. StartProgram(false);
  4622. Add([
  4623. 'type',
  4624. ' TEnum = (Red, Green, Blue);',
  4625. ' TEnumRg = green..blue;',
  4626. ' TArr = array[TEnum] of byte;',
  4627. ' TArrRg = array[TEnumRg] of byte;',
  4628. 'var',
  4629. ' e: TEnum;',
  4630. ' a1: TArr = (3,4,5);',
  4631. ' a2: TArrRg = (11,12);',
  4632. ' b: byte;',
  4633. 'begin',
  4634. ' for e in TEnum do ;',
  4635. ' for e in TEnumRg do ;',
  4636. ' for e in TArr do ;',
  4637. ' for e in TArrRg do ;',
  4638. ' for b in a1 do ;',
  4639. ' for b in a2 do ;',
  4640. '']);
  4641. ConvertProgram;
  4642. CheckSource('TestEnum_ForIn',
  4643. LinesToStr([ // statements
  4644. 'this.TEnum = {',
  4645. ' "0": "Red",',
  4646. ' Red: 0,',
  4647. ' "1": "Green",',
  4648. ' Green: 1,',
  4649. ' "2": "Blue",',
  4650. ' Blue: 2',
  4651. '};',
  4652. 'this.e = 0;',
  4653. 'this.a1 = [3, 4, 5];',
  4654. 'this.a2 = [11, 12];',
  4655. 'this.b = 0;',
  4656. '']),
  4657. LinesToStr([
  4658. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  4659. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  4660. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  4661. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  4662. ' for (var $in1 = $mod.a1, $l2 = 0, $end3 = rtl.length($in1) - 1; $l2 <= $end3; $l2++) $mod.b = $in1[$l2];',
  4663. ' for (var $in4 = $mod.a2, $l5 = 0, $end6 = rtl.length($in4) - 1; $l5 <= $end6; $l5++) $mod.b = $in4[$l5];',
  4664. '']));
  4665. end;
  4666. procedure TTestModule.TestEnum_ScopedNumber;
  4667. begin
  4668. Converter.Options:=Converter.Options+[coEnumNumbers];
  4669. StartProgram(false);
  4670. Add([
  4671. 'type',
  4672. ' TEnum = (Red, Green);',
  4673. 'var',
  4674. ' e: TEnum;',
  4675. 'begin',
  4676. ' e:=TEnum.Green;',
  4677. '']);
  4678. ConvertProgram;
  4679. CheckSource('TestEnum_ScopedNumber',
  4680. LinesToStr([ // statements
  4681. 'this.TEnum = {',
  4682. ' "0": "Red",',
  4683. ' Red: 0,',
  4684. ' "1": "Green",',
  4685. ' Green: 1',
  4686. '};',
  4687. 'this.e = 0;',
  4688. '']),
  4689. LinesToStr([
  4690. '$mod.e = 1;']));
  4691. end;
  4692. procedure TTestModule.TestEnum_InFunction;
  4693. begin
  4694. StartProgram(false);
  4695. Add([
  4696. 'const TEnum = 3;',
  4697. 'procedure DoIt;',
  4698. 'type',
  4699. ' TEnum = (Red, Green, Blue);',
  4700. ' procedure Sub;',
  4701. ' type',
  4702. ' TEnumSub = (Left, Right);',
  4703. ' var',
  4704. ' es: TEnumSub;',
  4705. ' begin',
  4706. ' es:=Left;',
  4707. ' end;',
  4708. 'var',
  4709. ' e, e2: TEnum;',
  4710. 'begin',
  4711. ' if e in [red,blue] then e2:=e;',
  4712. 'end;',
  4713. 'begin']);
  4714. ConvertProgram;
  4715. CheckSource('TestEnum_InFunction',
  4716. LinesToStr([ // statements
  4717. 'this.TEnum = 3;',
  4718. 'var TEnum$1 = {',
  4719. ' "0":"Red",',
  4720. ' Red:0,',
  4721. ' "1":"Green",',
  4722. ' Green:1,',
  4723. ' "2":"Blue",',
  4724. ' Blue:2',
  4725. ' };',
  4726. 'var TEnumSub = {',
  4727. ' "0": "Left",',
  4728. ' Left: 0,',
  4729. ' "1": "Right",',
  4730. ' Right: 1',
  4731. '};',
  4732. 'this.DoIt = function () {',
  4733. ' function Sub() {',
  4734. ' var es = 0;',
  4735. ' es = TEnumSub.Left;',
  4736. ' };',
  4737. ' var e = 0;',
  4738. ' var e2 = 0;',
  4739. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  4740. '};',
  4741. '']),
  4742. LinesToStr([
  4743. '']));
  4744. end;
  4745. procedure TTestModule.TestSet_Enum;
  4746. begin
  4747. StartProgram(false);
  4748. Add([
  4749. 'type',
  4750. ' TColor = (Red, Green, Blue);',
  4751. ' TColors = set of TColor;',
  4752. 'var',
  4753. ' c: TColor;',
  4754. ' s: TColors;',
  4755. ' t: TColors = [];',
  4756. ' u: TColors = [Red];',
  4757. 'begin',
  4758. ' s:=[];',
  4759. ' s:=[Green];',
  4760. ' s:=[Green,Blue];',
  4761. ' s:=[Red..Blue];',
  4762. ' s:=[Red,Green..Blue];',
  4763. ' s:=[Red,c];',
  4764. ' s:=t;',
  4765. ' s:=default(TColors);',
  4766. '']);
  4767. ConvertProgram;
  4768. CheckSource('TestSet',
  4769. LinesToStr([ // statements
  4770. 'this.TColor = {',
  4771. ' "0":"Red",',
  4772. ' Red:0,',
  4773. ' "1":"Green",',
  4774. ' Green:1,',
  4775. ' "2":"Blue",',
  4776. ' Blue:2',
  4777. ' };',
  4778. 'this.c = 0;',
  4779. 'this.s = {};',
  4780. 'this.t = {};',
  4781. 'this.u = rtl.createSet($mod.TColor.Red);'
  4782. ]),
  4783. LinesToStr([
  4784. '$mod.s={};',
  4785. '$mod.s=rtl.createSet($mod.TColor.Green);',
  4786. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  4787. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  4788. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  4789. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  4790. '$mod.s=rtl.refSet($mod.t);',
  4791. '$mod.s={};',
  4792. '']));
  4793. end;
  4794. procedure TTestModule.TestSet_Operators;
  4795. begin
  4796. StartProgram(false);
  4797. Add('type');
  4798. Add(' TColor = (Red, Green, Blue);');
  4799. Add(' TColors = set of tcolor;');
  4800. Add('var');
  4801. Add(' vC: TColor;');
  4802. Add(' vS: TColors;');
  4803. Add(' vT: TColors;');
  4804. Add(' vU: TColors;');
  4805. Add(' B: boolean;');
  4806. Add('begin');
  4807. Add(' include(vs,green);');
  4808. Add(' exclude(vs,vc);');
  4809. Add(' vs:=vt+vu;');
  4810. Add(' vs:=vt+[red];');
  4811. Add(' vs:=[red]+vt;');
  4812. Add(' vs:=[red]+[green];');
  4813. Add(' vs:=vt-vu;');
  4814. Add(' vs:=vt-[red];');
  4815. Add(' vs:=[red]-vt;');
  4816. Add(' vs:=[red]-[green];');
  4817. Add(' vs:=vt*vu;');
  4818. Add(' vs:=vt*[red];');
  4819. Add(' vs:=[red]*vt;');
  4820. Add(' vs:=[red]*[green];');
  4821. Add(' vs:=vt><vu;');
  4822. Add(' vs:=vt><[red];');
  4823. Add(' vs:=[red]><vt;');
  4824. Add(' vs:=[red]><[green];');
  4825. Add(' b:=vt=vu;');
  4826. Add(' b:=vt=[red];');
  4827. Add(' b:=[red]=vt;');
  4828. Add(' b:=[red]=[green];');
  4829. Add(' b:=vt<>vu;');
  4830. Add(' b:=vt<>[red];');
  4831. Add(' b:=[red]<>vt;');
  4832. Add(' b:=[red]<>[green];');
  4833. Add(' b:=vt<=vu;');
  4834. Add(' b:=vt<=[red];');
  4835. Add(' b:=[red]<=vt;');
  4836. Add(' b:=[red]<=[green];');
  4837. Add(' b:=vt>=vu;');
  4838. Add(' b:=vt>=[red];');
  4839. Add(' b:=[red]>=vt;');
  4840. Add(' b:=[red]>=[green];');
  4841. ConvertProgram;
  4842. CheckSource('TestSet_Operators',
  4843. LinesToStr([ // statements
  4844. 'this.TColor = {',
  4845. ' "0":"Red",',
  4846. ' Red:0,',
  4847. ' "1":"Green",',
  4848. ' Green:1,',
  4849. ' "2":"Blue",',
  4850. ' Blue:2',
  4851. ' };',
  4852. 'this.vC = 0;',
  4853. 'this.vS = {};',
  4854. 'this.vT = {};',
  4855. 'this.vU = {};',
  4856. 'this.B = false;'
  4857. ]),
  4858. LinesToStr([
  4859. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  4860. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  4861. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  4862. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  4863. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  4864. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  4865. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  4866. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  4867. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  4868. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  4869. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  4870. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  4871. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  4872. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  4873. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  4874. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  4875. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  4876. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  4877. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  4878. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  4879. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  4880. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  4881. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  4882. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  4883. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  4884. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  4885. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  4886. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  4887. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  4888. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  4889. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  4890. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  4891. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  4892. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  4893. '']));
  4894. end;
  4895. procedure TTestModule.TestSet_Operator_In;
  4896. begin
  4897. StartProgram(false);
  4898. Add('type');
  4899. Add(' TColor = (Red, Green, Blue);');
  4900. Add(' TColors = set of tcolor;');
  4901. Add('var');
  4902. Add(' vC: tcolor;');
  4903. Add(' vT: tcolors;');
  4904. Add(' B: boolean;');
  4905. Add('begin');
  4906. Add(' b:=red in vt;');
  4907. Add(' b:=vc in vt;');
  4908. Add(' b:=green in [red..blue];');
  4909. Add(' b:=vc in [red..blue];');
  4910. Add(' ');
  4911. Add(' if red in vt then ;');
  4912. Add(' while vC in vt do ;');
  4913. Add(' repeat');
  4914. Add(' until vC in vt;');
  4915. ConvertProgram;
  4916. CheckSource('TestSet_Operator_In',
  4917. LinesToStr([ // statements
  4918. 'this.TColor = {',
  4919. ' "0":"Red",',
  4920. ' Red:0,',
  4921. ' "1":"Green",',
  4922. ' Green:1,',
  4923. ' "2":"Blue",',
  4924. ' Blue:2',
  4925. ' };',
  4926. 'this.vC = 0;',
  4927. 'this.vT = {};',
  4928. 'this.B = false;'
  4929. ]),
  4930. LinesToStr([
  4931. '$mod.B = $mod.TColor.Red in $mod.vT;',
  4932. '$mod.B = $mod.vC in $mod.vT;',
  4933. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  4934. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  4935. 'if ($mod.TColor.Red in $mod.vT) ;',
  4936. 'while ($mod.vC in $mod.vT) {',
  4937. '};',
  4938. 'do {',
  4939. '} while (!($mod.vC in $mod.vT));',
  4940. '']));
  4941. end;
  4942. procedure TTestModule.TestSet_Functions;
  4943. begin
  4944. StartProgram(false);
  4945. Add('type');
  4946. Add(' TMyEnum = (Red, Green);');
  4947. Add(' TMyEnums = set of TMyEnum;');
  4948. Add('var');
  4949. Add(' e: TMyEnum;');
  4950. Add(' s: TMyEnums;');
  4951. Add('begin');
  4952. Add(' e:=Low(TMyEnums);');
  4953. Add(' e:=Low(s);');
  4954. Add(' e:=High(TMyEnums);');
  4955. Add(' e:=High(s);');
  4956. ConvertProgram;
  4957. CheckSource('TestSetFunctions',
  4958. LinesToStr([ // statements
  4959. 'this.TMyEnum = {',
  4960. ' "0":"Red",',
  4961. ' Red:0,',
  4962. ' "1":"Green",',
  4963. ' Green:1',
  4964. ' };',
  4965. 'this.e = 0;',
  4966. 'this.s = {};'
  4967. ]),
  4968. LinesToStr([
  4969. '$mod.e=$mod.TMyEnum.Red;',
  4970. '$mod.e=$mod.TMyEnum.Red;',
  4971. '$mod.e=$mod.TMyEnum.Green;',
  4972. '$mod.e=$mod.TMyEnum.Green;',
  4973. '']));
  4974. end;
  4975. procedure TTestModule.TestSet_PassAsArgClone;
  4976. begin
  4977. StartProgram(false);
  4978. Add('type');
  4979. Add(' TMyEnum = (Red, Green);');
  4980. Add(' TMyEnums = set of TMyEnum;');
  4981. Add('procedure DoDefault(s: tmyenums); begin end;');
  4982. Add('procedure DoConst(const s: tmyenums); begin end;');
  4983. Add('var');
  4984. Add(' aSet: tmyenums;');
  4985. Add('begin');
  4986. Add(' dodefault(aset);');
  4987. Add(' doconst(aset);');
  4988. ConvertProgram;
  4989. CheckSource('TestSetFunctions',
  4990. LinesToStr([ // statements
  4991. 'this.TMyEnum = {',
  4992. ' "0":"Red",',
  4993. ' Red:0,',
  4994. ' "1":"Green",',
  4995. ' Green:1',
  4996. ' };',
  4997. 'this.DoDefault = function (s) {',
  4998. '};',
  4999. 'this.DoConst = function (s) {',
  5000. '};',
  5001. 'this.aSet = {};'
  5002. ]),
  5003. LinesToStr([
  5004. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  5005. '$mod.DoConst($mod.aSet);',
  5006. '']));
  5007. end;
  5008. procedure TTestModule.TestSet_AsParams;
  5009. begin
  5010. StartProgram(false);
  5011. Add([
  5012. 'type TEnum = (Red,Blue);',
  5013. 'type TEnums = set of TEnum;',
  5014. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  5015. 'var vJ: TEnums;',
  5016. 'begin',
  5017. ' Include(vg,red);',
  5018. ' Include(result,blue);',
  5019. ' vg:=vg;',
  5020. ' vj:=vh;',
  5021. ' vi:=vi;',
  5022. ' doit(vg,vg,vg);',
  5023. ' doit(vh,vh,vj);',
  5024. ' doit(vi,vi,vi);',
  5025. ' doit(vj,vj,vj);',
  5026. 'end;',
  5027. 'var i: TEnums;',
  5028. 'begin',
  5029. ' doit(i,i,i);']);
  5030. ConvertProgram;
  5031. CheckSource('TestSet_AsParams',
  5032. LinesToStr([ // statements
  5033. 'this.TEnum = {',
  5034. ' "0": "Red",',
  5035. ' Red: 0,',
  5036. ' "1": "Blue",',
  5037. ' Blue: 1',
  5038. '};',
  5039. 'this.DoIt = function (vG,vH,vI) {',
  5040. ' var Result = {};',
  5041. ' var vJ = {};',
  5042. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  5043. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  5044. ' vG = rtl.refSet(vG);',
  5045. ' vJ = rtl.refSet(vH);',
  5046. ' vI.set(rtl.refSet(vI.get()));',
  5047. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  5048. ' get: function () {',
  5049. ' return vG;',
  5050. ' },',
  5051. ' set: function (v) {',
  5052. ' vG = v;',
  5053. ' }',
  5054. ' });',
  5055. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  5056. ' get: function () {',
  5057. ' return vJ;',
  5058. ' },',
  5059. ' set: function (v) {',
  5060. ' vJ = v;',
  5061. ' }',
  5062. ' });',
  5063. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  5064. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  5065. ' get: function () {',
  5066. ' return vJ;',
  5067. ' },',
  5068. ' set: function (v) {',
  5069. ' vJ = v;',
  5070. ' }',
  5071. ' });',
  5072. ' return Result;',
  5073. '};',
  5074. 'this.i = {};'
  5075. ]),
  5076. LinesToStr([
  5077. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  5078. ' p: $mod,',
  5079. ' get: function () {',
  5080. ' return this.p.i;',
  5081. ' },',
  5082. ' set: function (v) {',
  5083. ' this.p.i = v;',
  5084. ' }',
  5085. '});'
  5086. ]));
  5087. end;
  5088. procedure TTestModule.TestSet_Property;
  5089. begin
  5090. StartProgram(false);
  5091. Add('type');
  5092. Add(' TEnum = (Red,Blue);');
  5093. Add(' TEnums = set of TEnum;');
  5094. Add(' TObject = class');
  5095. Add(' function GetColors: TEnums; external name ''GetColors'';');
  5096. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  5097. Add(' property Colors: TEnums read GetColors write SetColors;');
  5098. Add(' end;');
  5099. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  5100. Add('begin end;');
  5101. Add('var Obj: TObject;');
  5102. Add('begin');
  5103. Add(' Include(Obj.Colors,Red);');
  5104. Add(' Exclude(Obj.Colors,Red);');
  5105. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  5106. ConvertProgram;
  5107. CheckSource('TestSet_Property',
  5108. LinesToStr([ // statements
  5109. 'this.TEnum = {',
  5110. ' "0": "Red",',
  5111. ' Red: 0,',
  5112. ' "1": "Blue",',
  5113. ' Blue: 1',
  5114. '};',
  5115. 'rtl.createClass($mod, "TObject", null, function () {',
  5116. ' this.$init = function () {',
  5117. ' };',
  5118. ' this.$final = function () {',
  5119. ' };',
  5120. '});',
  5121. 'this.DoIt = function (i, j, k, l) {',
  5122. '};',
  5123. 'this.Obj = null;',
  5124. '']),
  5125. LinesToStr([
  5126. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  5127. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  5128. '']));
  5129. end;
  5130. procedure TTestModule.TestSet_EnumConst;
  5131. begin
  5132. StartProgram(false);
  5133. Add([
  5134. 'type',
  5135. ' TEnum = (Red,Blue);',
  5136. ' TEnums = set of TEnum;',
  5137. 'const',
  5138. ' Orange = red;',
  5139. 'var',
  5140. ' Enum: tenum;',
  5141. ' Enums: tenums;',
  5142. 'begin',
  5143. ' Include(enums,orange);',
  5144. ' Exclude(enums,orange);',
  5145. ' if orange in enums then;',
  5146. ' if orange in [orange,red] then;']);
  5147. ConvertProgram;
  5148. CheckSource('TestSet_EnumConst',
  5149. LinesToStr([ // statements
  5150. 'this.TEnum = {',
  5151. ' "0": "Red",',
  5152. ' Red: 0,',
  5153. ' "1": "Blue",',
  5154. ' Blue: 1',
  5155. '};',
  5156. 'this.Orange = $mod.TEnum.Red;',
  5157. 'this.Enum = 0;',
  5158. 'this.Enums = {};',
  5159. '']),
  5160. LinesToStr([
  5161. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  5162. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  5163. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  5164. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  5165. '']));
  5166. end;
  5167. procedure TTestModule.TestSet_IntConst;
  5168. begin
  5169. StartProgram(false);
  5170. Add([
  5171. 'type',
  5172. ' TEnums = set of Byte;',
  5173. 'const',
  5174. ' Orange = 0;',
  5175. 'var',
  5176. ' Enum: byte;',
  5177. ' Enums: tenums;',
  5178. 'begin',
  5179. ' Enums:=[];',
  5180. ' Enums:=[0];',
  5181. ' Enums:=[1..2];',
  5182. //' Include(enums,orange);',
  5183. //' Exclude(enums,orange);',
  5184. ' if orange in enums then;',
  5185. ' if orange in [orange,1] then;']);
  5186. ConvertProgram;
  5187. CheckSource('TestSet_IntConst',
  5188. LinesToStr([ // statements
  5189. 'this.Orange = 0;',
  5190. 'this.Enum = 0;',
  5191. 'this.Enums = {};',
  5192. '']),
  5193. LinesToStr([
  5194. '$mod.Enums = {};',
  5195. '$mod.Enums = rtl.createSet(0);',
  5196. '$mod.Enums = rtl.createSet(null, 1, 2);',
  5197. 'if (0 in $mod.Enums) ;',
  5198. 'if (0 in rtl.createSet(0, 1)) ;',
  5199. '']));
  5200. end;
  5201. procedure TTestModule.TestSet_AnonymousEnumType;
  5202. begin
  5203. StartProgram(false);
  5204. Add('type');
  5205. Add(' TFlags = set of (red, green);');
  5206. Add('const');
  5207. Add(' favorite = red;');
  5208. Add('var');
  5209. Add(' f: TFlags;');
  5210. Add(' i: longint;');
  5211. Add('begin');
  5212. Add(' Include(f,red);');
  5213. Add(' Include(f,favorite);');
  5214. Add(' i:=ord(red);');
  5215. Add(' i:=ord(favorite);');
  5216. Add(' i:=ord(low(TFlags));');
  5217. Add(' i:=ord(low(f));');
  5218. Add(' i:=ord(low(favorite));');
  5219. Add(' i:=ord(high(TFlags));');
  5220. Add(' i:=ord(high(f));');
  5221. Add(' i:=ord(high(favorite));');
  5222. Add(' f:=[green,favorite];');
  5223. ConvertProgram;
  5224. CheckSource('TestSet_AnonymousEnumType',
  5225. LinesToStr([ // statements
  5226. 'this.TFlags$a = {',
  5227. ' "0": "red",',
  5228. ' red: 0,',
  5229. ' "1": "green",',
  5230. ' green: 1',
  5231. '};',
  5232. 'this.favorite = $mod.TFlags$a.red;',
  5233. 'this.f = {};',
  5234. 'this.i = 0;',
  5235. '']),
  5236. LinesToStr([
  5237. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  5238. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  5239. '$mod.i = $mod.TFlags$a.red;',
  5240. '$mod.i = $mod.TFlags$a.red;',
  5241. '$mod.i = $mod.TFlags$a.red;',
  5242. '$mod.i = $mod.TFlags$a.red;',
  5243. '$mod.i = $mod.TFlags$a.red;',
  5244. '$mod.i = $mod.TFlags$a.green;',
  5245. '$mod.i = $mod.TFlags$a.green;',
  5246. '$mod.i = $mod.TFlags$a.green;',
  5247. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  5248. '']));
  5249. end;
  5250. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  5251. begin
  5252. exit;
  5253. StartProgram(false);
  5254. Add([
  5255. 'type',
  5256. ' TAtoZ = ''A''..''Z'';',
  5257. ' TSetOfAZ = set of TAtoZ;',
  5258. 'var',
  5259. ' c: char;',
  5260. ' a: TAtoZ;',
  5261. ' s: TSetOfAZ = [''P'',''A''];',
  5262. ' i: longint;',
  5263. 'begin',
  5264. ' Include(s,''S'');',
  5265. ' Include(s,c);',
  5266. ' Include(s,a);',
  5267. ' c:=low(TAtoZ);',
  5268. ' i:=ord(low(TAtoZ));',
  5269. ' a:=high(TAtoZ);',
  5270. ' a:=high(TSetOfAtoZ);',
  5271. ' s:=[a,c,''M''];',
  5272. '']);
  5273. ConvertProgram;
  5274. CheckSource('TestSet_AnonymousEnumTypeChar',
  5275. LinesToStr([ // statements
  5276. '']),
  5277. LinesToStr([
  5278. '']));
  5279. end;
  5280. procedure TTestModule.TestSet_ConstEnum;
  5281. begin
  5282. StartProgram(false);
  5283. Add([
  5284. 'type',
  5285. ' TEnum = (red,blue,green);',
  5286. ' TEnums = set of TEnum;',
  5287. 'const',
  5288. ' teAny = [low(TEnum)..high(TEnum)];',
  5289. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  5290. 'var',
  5291. ' e: TEnum;',
  5292. ' s: TEnums;',
  5293. 'begin',
  5294. ' if blue in teAny then;',
  5295. ' if blue in teAny+[e] then;',
  5296. ' if blue in teAny+teRedBlue then;',
  5297. ' if e in [red,blue] then;',
  5298. ' s:=teAny;',
  5299. ' s:=teAny+[e];',
  5300. ' s:=[e]+teAny;',
  5301. ' s:=teAny+teRedBlue;',
  5302. ' s:=teAny+teRedBlue+[e];',
  5303. '']);
  5304. ConvertProgram;
  5305. CheckSource('TestSet_ConstEnum',
  5306. LinesToStr([ // statements
  5307. 'this.TEnum = {',
  5308. ' "0": "red",',
  5309. ' red: 0,',
  5310. ' "1": "blue",',
  5311. ' blue: 1,',
  5312. ' "2": "green",',
  5313. ' green: 2',
  5314. '};',
  5315. 'this.teAny = rtl.createSet(null, $mod.TEnum.red, $mod.TEnum.green);',
  5316. 'this.teRedBlue = rtl.createSet(null, $mod.TEnum.red, $mod.TEnum.green - 1);',
  5317. 'this.e = 0;',
  5318. 'this.s = {};',
  5319. '']),
  5320. LinesToStr([
  5321. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  5322. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  5323. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  5324. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  5325. '$mod.s = rtl.refSet($mod.teAny);',
  5326. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  5327. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  5328. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  5329. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  5330. '']));
  5331. end;
  5332. procedure TTestModule.TestSet_ConstChar;
  5333. begin
  5334. StartProgram(false);
  5335. Add([
  5336. 'const',
  5337. ' LowChars = [''a''..''z''];',
  5338. ' Chars = LowChars+[''A''..''Z''];',
  5339. 'var',
  5340. ' c: char;',
  5341. ' s: string;',
  5342. 'begin',
  5343. ' if c in lowchars then ;',
  5344. ' if ''a'' in lowchars then ;',
  5345. ' if s[1] in lowchars then ;',
  5346. ' if c in chars then ;',
  5347. ' if c in [''a''..''z'',''_''] then ;',
  5348. ' if ''b'' in [''a''..''z'',''_''] then ;',
  5349. '']);
  5350. ConvertProgram;
  5351. CheckSource('TestSet_ConstChar',
  5352. LinesToStr([ // statements
  5353. 'this.LowChars = rtl.createSet(null, 97, 122);',
  5354. 'this.Chars = rtl.unionSet($mod.LowChars, rtl.createSet(null, 65, 90));',
  5355. 'this.c = "";',
  5356. 'this.s = "";',
  5357. '']),
  5358. LinesToStr([
  5359. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  5360. 'if (97 in $mod.LowChars) ;',
  5361. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  5362. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  5363. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  5364. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  5365. '']));
  5366. end;
  5367. procedure TTestModule.TestSet_ConstInt;
  5368. begin
  5369. StartProgram(false);
  5370. Add([
  5371. 'const',
  5372. ' Months = [1..12];',
  5373. ' Mirror = [-12..-1]+Months;',
  5374. 'var',
  5375. ' i: smallint;',
  5376. 'begin',
  5377. ' if 3 in Months then;',
  5378. ' if i in Months+[i] then;',
  5379. ' if i in Months+Mirror then;',
  5380. ' if i in [4..6,8] then;',
  5381. '']);
  5382. ConvertProgram;
  5383. CheckSource('TestSet_ConstInt',
  5384. LinesToStr([ // statements
  5385. 'this.Months = rtl.createSet(null, 1, 12);',
  5386. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), $mod.Months);',
  5387. 'this.i = 0;',
  5388. '']),
  5389. LinesToStr([
  5390. 'if (3 in $mod.Months) ;',
  5391. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  5392. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  5393. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  5394. '']));
  5395. end;
  5396. procedure TTestModule.TestSet_InFunction;
  5397. begin
  5398. StartProgram(false);
  5399. Add([
  5400. 'const',
  5401. ' TEnum = 3;',
  5402. ' TSetOfEnum = 4;',
  5403. ' TSetOfAno = 5;',
  5404. 'procedure DoIt;',
  5405. 'type',
  5406. ' TEnum = (red, blue);',
  5407. ' TSetOfEnum = set of TEnum;',
  5408. ' TSetOfAno = set of (up,down);',
  5409. 'var',
  5410. ' e: TEnum;',
  5411. ' se: TSetOfEnum;',
  5412. ' sa: TSetOfAno;',
  5413. 'begin',
  5414. ' se:=[e];',
  5415. ' sa:=[up];',
  5416. 'end;',
  5417. 'begin',
  5418. '']);
  5419. ConvertProgram;
  5420. CheckSource('TestSet_InFunction',
  5421. LinesToStr([ // statements
  5422. 'this.TEnum = 3;',
  5423. 'this.TSetOfEnum = 4;',
  5424. 'this.TSetOfAno = 5;',
  5425. 'var TEnum$1 = {',
  5426. ' "0": "red",',
  5427. ' red: 0,',
  5428. ' "1": "blue",',
  5429. ' blue: 1',
  5430. '};',
  5431. 'var TSetOfAno$a = {',
  5432. ' "0": "up",',
  5433. ' up: 0,',
  5434. ' "1": "down",',
  5435. ' down: 1',
  5436. '};',
  5437. 'this.DoIt = function () {',
  5438. ' var e = 0;',
  5439. ' var se = {};',
  5440. ' var sa = {};',
  5441. ' se = rtl.createSet(e);',
  5442. ' sa = rtl.createSet(TSetOfAno$a.up);',
  5443. '};',
  5444. '']),
  5445. LinesToStr([
  5446. '']));
  5447. end;
  5448. procedure TTestModule.TestSet_ForIn;
  5449. begin
  5450. StartProgram(false);
  5451. Add([
  5452. 'type',
  5453. ' TEnum = (Red, Green, Blue);',
  5454. ' TEnumRg = green..blue;',
  5455. ' TSetOfEnum = set of TEnum;',
  5456. ' TSetOfEnumRg = set of TEnumRg;',
  5457. 'var',
  5458. ' e, e2: TEnum;',
  5459. ' er: TEnum;',
  5460. ' s: TSetOfEnum;',
  5461. 'begin',
  5462. ' for e in TSetOfEnum do ;',
  5463. ' for e in TSetOfEnumRg do ;',
  5464. ' for e in [] do e2:=e;',
  5465. ' for e in [red..green] do e2:=e;',
  5466. ' for e in [green,blue] do e2:=e;',
  5467. ' for e in [red,blue] do e2:=e;',
  5468. ' for e in s do e2:=e;',
  5469. ' for er in TSetOfEnumRg do ;',
  5470. '']);
  5471. ConvertProgram;
  5472. CheckSource('TestSet_ForIn',
  5473. LinesToStr([ // statements
  5474. 'this.TEnum = {',
  5475. ' "0":"Red",',
  5476. ' Red:0,',
  5477. ' "1":"Green",',
  5478. ' Green:1,',
  5479. ' "2":"Blue",',
  5480. ' Blue:2',
  5481. ' };',
  5482. 'this.e = 0;',
  5483. 'this.e2 = 0;',
  5484. 'this.er = 0;',
  5485. 'this.s = {};',
  5486. '']),
  5487. LinesToStr([
  5488. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5489. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5490. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  5491. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  5492. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  5493. 'for (var $l1 in $mod.s){',
  5494. ' $mod.e = +$l1;',
  5495. ' $mod.e2 = $mod.e;',
  5496. '};',
  5497. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  5498. '']));
  5499. end;
  5500. procedure TTestModule.TestNestBegin;
  5501. begin
  5502. StartProgram(false);
  5503. Add('begin');
  5504. Add(' begin');
  5505. Add(' begin');
  5506. Add(' end;');
  5507. Add(' begin');
  5508. Add(' if true then ;');
  5509. Add(' end;');
  5510. Add(' end;');
  5511. ConvertProgram;
  5512. CheckSource('TestNestBegin',
  5513. '',
  5514. 'if (true) ;');
  5515. end;
  5516. procedure TTestModule.TestUnitImplVars;
  5517. begin
  5518. StartUnit(false);
  5519. Add('interface');
  5520. Add('implementation');
  5521. Add('var');
  5522. Add(' V1:longint;');
  5523. Add(' V2:longint = 3;');
  5524. Add(' V3:string = ''abc'';');
  5525. ConvertUnit;
  5526. CheckSource('TestUnitImplVars',
  5527. LinesToStr([ // statements
  5528. 'var $impl = $mod.$impl;',
  5529. '']),
  5530. '', // this.$init
  5531. LinesToStr([ // implementation
  5532. '$impl.V1 = 0;',
  5533. '$impl.V2 = 3;',
  5534. '$impl.V3 = "abc";',
  5535. '']) );
  5536. end;
  5537. procedure TTestModule.TestUnitImplConsts;
  5538. begin
  5539. StartUnit(false);
  5540. Add('interface');
  5541. Add('implementation');
  5542. Add('const');
  5543. Add(' v1 = 3;');
  5544. Add(' v2:longint = 4;');
  5545. Add(' v3:string = ''abc'';');
  5546. ConvertUnit;
  5547. CheckSource('TestUnitImplConsts',
  5548. LinesToStr([ // statements
  5549. 'var $impl = $mod.$impl;',
  5550. '']),
  5551. '', // this.$init
  5552. LinesToStr([ // implementation
  5553. '$impl.v1 = 3;',
  5554. '$impl.v2 = 4;',
  5555. '$impl.v3 = "abc";',
  5556. '']) );
  5557. end;
  5558. procedure TTestModule.TestUnitImplRecord;
  5559. begin
  5560. StartUnit(false);
  5561. Add('interface');
  5562. Add('implementation');
  5563. Add('type');
  5564. Add(' TMyRecord = record');
  5565. Add(' i: longint;');
  5566. Add(' end;');
  5567. Add('var aRec: TMyRecord;');
  5568. Add('initialization');
  5569. Add(' arec.i:=3;');
  5570. ConvertUnit;
  5571. CheckSource('TestUnitImplRecord',
  5572. LinesToStr([ // statements
  5573. 'var $impl = $mod.$impl;',
  5574. '']),
  5575. // this.$init
  5576. '$impl.aRec.i = 3;',
  5577. LinesToStr([ // implementation
  5578. 'rtl.recNewT($impl, "TMyRecord", function () {',
  5579. ' this.i = 0;',
  5580. ' this.$eq = function (b) {',
  5581. ' return this.i === b.i;',
  5582. ' };',
  5583. ' this.$assign = function (s) {',
  5584. ' this.i = s.i;',
  5585. ' return this;',
  5586. ' };',
  5587. '});',
  5588. '$impl.aRec = $impl.TMyRecord.$new();',
  5589. '']) );
  5590. end;
  5591. procedure TTestModule.TestRenameJSNameConflict;
  5592. begin
  5593. StartProgram(false);
  5594. Add('var apply: longint;');
  5595. Add('var bind: longint;');
  5596. Add('var call: longint;');
  5597. Add('begin');
  5598. ConvertProgram;
  5599. CheckSource('TestRenameJSNameConflict',
  5600. LinesToStr([ // statements
  5601. 'this.Apply = 0;',
  5602. 'this.Bind = 0;',
  5603. 'this.Call = 0;'
  5604. ]),
  5605. LinesToStr([ // this.$main
  5606. ''
  5607. ]));
  5608. end;
  5609. procedure TTestModule.TestLocalConst;
  5610. begin
  5611. StartProgram(false);
  5612. Add('procedure DoIt;');
  5613. Add('const');
  5614. Add(' cA: longint = 1;');
  5615. Add(' cB = 2;');
  5616. Add(' procedure Sub;');
  5617. Add(' const');
  5618. Add(' csA = 3;');
  5619. Add(' cB: double = 4;');
  5620. Add(' begin');
  5621. Add(' cb:=cb+csa;');
  5622. Add(' ca:=ca+csa+5;');
  5623. Add(' end;');
  5624. Add('begin');
  5625. Add(' ca:=ca+cb+6;');
  5626. Add('end;');
  5627. Add('begin');
  5628. ConvertProgram;
  5629. CheckSource('TestLocalConst',
  5630. LinesToStr([
  5631. 'var cA = 1;',
  5632. 'var cB = 2;',
  5633. 'var csA = 3;',
  5634. 'var cB$1 = 4;',
  5635. 'this.DoIt = function () {',
  5636. ' function Sub() {',
  5637. ' cB$1 = cB$1 + 3;',
  5638. ' cA = cA + 3 + 5;',
  5639. ' };',
  5640. ' cA = cA + 2 + 6;',
  5641. '};'
  5642. ]),
  5643. LinesToStr([
  5644. ]));
  5645. end;
  5646. procedure TTestModule.TestVarExternal;
  5647. begin
  5648. StartProgram(false);
  5649. Add('var');
  5650. Add(' NaN: double; external name ''Global.NaN'';');
  5651. Add(' d: double;');
  5652. Add('begin');
  5653. Add(' d:=NaN;');
  5654. ConvertProgram;
  5655. CheckSource('TestVarExternal',
  5656. LinesToStr([
  5657. 'this.d = 0.0;'
  5658. ]),
  5659. LinesToStr([
  5660. '$mod.d = Global.NaN;'
  5661. ]));
  5662. end;
  5663. procedure TTestModule.TestVarExternalOtherUnit;
  5664. begin
  5665. AddModuleWithIntfImplSrc('unit2.pas',
  5666. LinesToStr([
  5667. 'var NaN: double; external name ''Global.NaN'';',
  5668. 'var iV: longint;'
  5669. ]),
  5670. '');
  5671. StartUnit(true);
  5672. Add('interface');
  5673. Add('uses unit2;');
  5674. Add('implementation');
  5675. Add('var');
  5676. Add(' d: double;');
  5677. Add(' i: longint; external name ''$i'';');
  5678. Add('begin');
  5679. Add(' d:=nan;');
  5680. Add(' d:=uNit2.nan;');
  5681. Add(' d:=test1.d;');
  5682. Add(' i:=iv;');
  5683. Add(' i:=uNit2.iv;');
  5684. Add(' i:=test1.i;');
  5685. ConvertUnit;
  5686. CheckSource('TestVarExternalOtherUnit',
  5687. LinesToStr([
  5688. 'var $impl = $mod.$impl;',
  5689. '']),
  5690. LinesToStr([ // this.$init
  5691. '$impl.d = Global.NaN;',
  5692. '$impl.d = Global.NaN;',
  5693. '$impl.d = $impl.d;',
  5694. '$i = pas.unit2.iV;',
  5695. '$i = pas.unit2.iV;',
  5696. '$i = $i;',
  5697. '']),
  5698. LinesToStr([ // implementation
  5699. '$impl.d = 0.0;',
  5700. '']) );
  5701. end;
  5702. procedure TTestModule.TestVarAbsoluteFail;
  5703. begin
  5704. StartProgram(false);
  5705. Add([
  5706. 'var',
  5707. ' a: longint;',
  5708. ' b: longword absolute a;',
  5709. 'begin']);
  5710. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  5711. ConvertProgram;
  5712. end;
  5713. procedure TTestModule.TestConstExternal;
  5714. begin
  5715. StartProgram(false);
  5716. Add([
  5717. 'const',
  5718. ' PI: double; external name ''Global.PI'';',
  5719. ' Tau = 2*pi;',
  5720. 'var d: double;',
  5721. 'begin',
  5722. ' d:=pi;',
  5723. ' d:=tau+pi;']);
  5724. ConvertProgram;
  5725. CheckSource('TestConstExternal',
  5726. LinesToStr([
  5727. 'this.Tau = 2*Global.PI;',
  5728. 'this.d = 0.0;'
  5729. ]),
  5730. LinesToStr([
  5731. '$mod.d = Global.PI;',
  5732. '$mod.d = $mod.Tau + Global.PI;'
  5733. ]));
  5734. end;
  5735. procedure TTestModule.TestDouble;
  5736. begin
  5737. StartProgram(false);
  5738. Add([
  5739. 'type',
  5740. ' TDateTime = double;',
  5741. 'const',
  5742. ' a = TDateTime(2.7);',
  5743. ' b = a + TDateTime(1.7);',
  5744. ' c = 0.9 + 0.1;',
  5745. ' f0_1 = 0.1;',
  5746. ' f0_3 = 0.3;',
  5747. ' fn0_1 = -0.1;',
  5748. ' fn0_3 = -0.3;',
  5749. ' fn0_003 = -0.003;',
  5750. ' fn0_123456789 = -0.123456789;',
  5751. ' fn300_0 = -300.0;',
  5752. ' fn123456_0 = -123456.0;',
  5753. ' fn1234567_8 = -1234567.8;',
  5754. ' fn12345678_9 = -12345678.9;',
  5755. ' f1_0En12 = 1E-12;',
  5756. ' fn1_0En12 = -1E-12;',
  5757. ' maxdouble = 1.7e+308;',
  5758. ' mindouble = -1.7e+308;',
  5759. ' MinSafeIntDouble = -$10000000000000;',
  5760. ' MinSafeIntDouble2 = -$fffffffffffff-1;',
  5761. ' MaxSafeIntDouble = $fffffffffffff;',
  5762. ' DZeroResolution = 1E-12;',
  5763. ' Minus1 = -1E-12;',
  5764. ' EPS = 1E-9;',
  5765. ' DELTA = 0.001;',
  5766. ' Big = 129.789E+100;',
  5767. ' Test0_15 = 0.15;',
  5768. ' Test999 = 2.9999999999999;',
  5769. ' Test111999 = 211199999999999000.0;',
  5770. ' TestMinus111999 = -211199999999999000.0;',
  5771. 'var',
  5772. ' d: double = b;',
  5773. 'begin',
  5774. ' d:=1.0;',
  5775. ' d:=1.0/3.0;',
  5776. ' d:=1/3;',
  5777. ' d:=5.0E-324;',
  5778. ' d:=1.7E308;',
  5779. ' d:=001.00E00;',
  5780. ' d:=002.00E001;',
  5781. ' d:=003.000E000;',
  5782. ' d:=-004.00E-00;',
  5783. ' d:=-005.00E-001;',
  5784. ' d:=10**3;',
  5785. ' d:=10 mod 3;',
  5786. ' d:=10 div 3;',
  5787. ' d:=c;',
  5788. ' d:=f0_1;',
  5789. ' d:=f0_3;',
  5790. ' d:=fn0_1;',
  5791. ' d:=fn0_3;',
  5792. ' d:=fn0_003;',
  5793. ' d:=fn0_123456789;',
  5794. ' d:=fn300_0;',
  5795. ' d:=fn123456_0;',
  5796. ' d:=fn1234567_8;',
  5797. ' d:=fn12345678_9;',
  5798. ' d:=f1_0En12;',
  5799. ' d:=fn1_0En12;',
  5800. ' d:=maxdouble;',
  5801. ' d:=mindouble;',
  5802. ' d:=MinSafeIntDouble;',
  5803. ' d:=double(MinSafeIntDouble);',
  5804. ' d:=MinSafeIntDouble2;',
  5805. ' d:=double(MinSafeIntDouble2);',
  5806. ' d:=MaxSafeIntDouble;',
  5807. ' d:=default(double);',
  5808. '']);
  5809. ConvertProgram;
  5810. CheckSource('TestDouble',
  5811. LinesToStr([
  5812. 'this.a = 2.7;',
  5813. 'this.b = 2.7 + 1.7;',
  5814. 'this.c = 0.9 + 0.1;',
  5815. 'this.f0_1 = 0.1;',
  5816. 'this.f0_3 = 0.3;',
  5817. 'this.fn0_1 = -0.1;',
  5818. 'this.fn0_3 = -0.3;',
  5819. 'this.fn0_003 = -0.003;',
  5820. 'this.fn0_123456789 = -0.123456789;',
  5821. 'this.fn300_0 = -300.0;',
  5822. 'this.fn123456_0 = -123456.0;',
  5823. 'this.fn1234567_8 = -1234567.8;',
  5824. 'this.fn12345678_9 = -12345678.9;',
  5825. 'this.f1_0En12 = 1E-12;',
  5826. 'this.fn1_0En12 = -1E-12;',
  5827. 'this.maxdouble = 1.7e+308;',
  5828. 'this.mindouble = -1.7e+308;',
  5829. 'this.MinSafeIntDouble = -0x10000000000000;',
  5830. 'this.MinSafeIntDouble2 = -0xfffffffffffff - 1;',
  5831. 'this.MaxSafeIntDouble = 0xfffffffffffff;',
  5832. 'this.DZeroResolution = 1E-12;',
  5833. 'this.Minus1 = -1E-12;',
  5834. 'this.EPS = 1E-9;',
  5835. 'this.DELTA = 0.001;',
  5836. 'this.Big = 129.789E+100;',
  5837. 'this.Test0_15 = 0.15;',
  5838. 'this.Test999 = 2.9999999999999;',
  5839. 'this.Test111999 = 211199999999999000.0;',
  5840. 'this.TestMinus111999 = -211199999999999000.0;',
  5841. 'this.d = 4.4;'
  5842. ]),
  5843. LinesToStr([
  5844. '$mod.d = 1.0;',
  5845. '$mod.d = 1.0 / 3.0;',
  5846. '$mod.d = 1 / 3;',
  5847. '$mod.d = 5.0E-324;',
  5848. '$mod.d = 1.7E308;',
  5849. '$mod.d = 1.00E0;',
  5850. '$mod.d = 2.00E1;',
  5851. '$mod.d = 3.000E0;',
  5852. '$mod.d = -4.00E-0;',
  5853. '$mod.d = -5.00E-1;',
  5854. '$mod.d = Math.pow(10, 3);',
  5855. '$mod.d = 10 % 3;',
  5856. '$mod.d = Math.floor(10 / 3);',
  5857. '$mod.d = 1;',
  5858. '$mod.d = 0.1;',
  5859. '$mod.d = 0.3;',
  5860. '$mod.d = -0.1;',
  5861. '$mod.d = -0.3;',
  5862. '$mod.d = -0.003;',
  5863. '$mod.d = -0.123456789;',
  5864. '$mod.d = -300;',
  5865. '$mod.d = -123456;',
  5866. '$mod.d = -1234567.8;',
  5867. '$mod.d = -1.23456789E7;',
  5868. '$mod.d = 1E-12;',
  5869. '$mod.d = -1E-12;',
  5870. '$mod.d = 1.7E308;',
  5871. '$mod.d = -1.7E308;',
  5872. '$mod.d = -4503599627370496;',
  5873. '$mod.d = -4503599627370496;',
  5874. '$mod.d = -4503599627370496;',
  5875. '$mod.d = -4503599627370496;',
  5876. '$mod.d = 4503599627370495;',
  5877. '$mod.d = 0.0;',
  5878. '']));
  5879. end;
  5880. procedure TTestModule.TestInteger;
  5881. begin
  5882. StartProgram(false);
  5883. Add([
  5884. 'const',
  5885. ' MinInt = low(NativeInt);',
  5886. ' MaxInt = high(NativeInt);',
  5887. 'type',
  5888. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  5889. 'const',
  5890. ' a = low(TMyInt)+High(TMyInt);',
  5891. 'var',
  5892. ' i: TMyInt;',
  5893. 'begin',
  5894. ' i:=-MinInt;',
  5895. ' i:=default(TMyInt);',
  5896. ' i:=low(i)+high(i);',
  5897. '']);
  5898. ConvertProgram;
  5899. CheckSource('TestIntegerRange',
  5900. LinesToStr([
  5901. 'this.MinInt = -4503599627370496;',
  5902. 'this.MaxInt = 4503599627370495;',
  5903. 'this.a = -4503599627370496 + 4503599627370495;',
  5904. 'this.i = 0;',
  5905. '']),
  5906. LinesToStr([
  5907. '$mod.i = - -4503599627370496;',
  5908. '$mod.i = -4503599627370496;',
  5909. '$mod.i = -4503599627370496 + 4503599627370495;',
  5910. '']));
  5911. end;
  5912. procedure TTestModule.TestIntegerRange;
  5913. begin
  5914. StartProgram(false);
  5915. Add([
  5916. 'const',
  5917. ' MinInt = -1;',
  5918. ' MaxInt = +1;',
  5919. 'type',
  5920. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  5921. ' TInt2 = 1..3;',
  5922. 'const',
  5923. ' a = low(TMyInt)+High(TMyInt);',
  5924. ' b = low(TInt2)+High(TInt2);',
  5925. ' s1 = [1];',
  5926. ' s2 = [1,2];',
  5927. ' s3 = [1..3];',
  5928. ' s4 = [low(shortint)..high(shortint)];',
  5929. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  5930. ' s6 = 1 in s2;',
  5931. 'var',
  5932. ' i: TMyInt;',
  5933. ' i2: TInt2;',
  5934. 'begin',
  5935. ' i:=i2;',
  5936. ' i:=default(TMyInt);',
  5937. ' if i=i2 then ;']);
  5938. ConvertProgram;
  5939. CheckSource('TestIntegerRange',
  5940. LinesToStr([
  5941. 'this.MinInt = -1;',
  5942. 'this.MaxInt = +1;',
  5943. 'this.a = -1 + 1;',
  5944. 'this.b = 1 + 3;',
  5945. 'this.s1 = rtl.createSet(1);',
  5946. 'this.s2 = rtl.createSet(1, 2);',
  5947. 'this.s3 = rtl.createSet(null, 1, 3);',
  5948. 'this.s4 = rtl.createSet(null, -128, 127);',
  5949. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  5950. 'this.s6 = 1 in $mod.s2;',
  5951. 'this.i = 0;',
  5952. 'this.i2 = 0;',
  5953. '']),
  5954. LinesToStr([
  5955. '$mod.i = $mod.i2;',
  5956. '$mod.i = -1;',
  5957. 'if ($mod.i === $mod.i2) ;',
  5958. '']));
  5959. end;
  5960. procedure TTestModule.TestIntegerTypecasts;
  5961. begin
  5962. StartProgram(false);
  5963. Add([
  5964. 'var',
  5965. ' i: nativeint;',
  5966. ' b: byte;',
  5967. ' sh: shortint;',
  5968. ' w: word;',
  5969. ' sm: smallint;',
  5970. ' lw: longword;',
  5971. ' li: longint;',
  5972. 'begin',
  5973. ' b:=byte(i);',
  5974. ' sh:=shortint(i);',
  5975. ' w:=word(i);',
  5976. ' sm:=smallint(i);',
  5977. ' lw:=longword(i);',
  5978. ' li:=longint(i);',
  5979. '']);
  5980. ConvertProgram;
  5981. CheckSource('TestIntegerTypecasts',
  5982. LinesToStr([
  5983. 'this.i = 0;',
  5984. 'this.b = 0;',
  5985. 'this.sh = 0;',
  5986. 'this.w = 0;',
  5987. 'this.sm = 0;',
  5988. 'this.lw = 0;',
  5989. 'this.li = 0;',
  5990. '']),
  5991. LinesToStr([
  5992. '$mod.b = $mod.i & 255;',
  5993. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  5994. '$mod.w = $mod.i & 65535;',
  5995. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  5996. '$mod.lw = $mod.i >>> 0;',
  5997. '$mod.li = $mod.i & 0xFFFFFFFF;',
  5998. '']));
  5999. end;
  6000. procedure TTestModule.TestBitwiseAndNativeIntWarn;
  6001. begin
  6002. StartProgram(false);
  6003. Add([
  6004. 'var',
  6005. ' i,j: nativeint;',
  6006. 'begin',
  6007. ' i:=i and j;',
  6008. '']);
  6009. ConvertProgram;
  6010. CheckSource('TestBitwiseAndNativeIntWarn',
  6011. LinesToStr([
  6012. 'this.i = 0;',
  6013. 'this.j = 0;',
  6014. '']),
  6015. LinesToStr([
  6016. '$mod.i = $mod.i & $mod.j;',
  6017. '']));
  6018. CheckHint(mtWarning,nBitWiseOperationsAre32Bit,sBitWiseOperationsAre32Bit);
  6019. end;
  6020. procedure TTestModule.TestCurrency;
  6021. begin
  6022. StartProgram(false);
  6023. Add([
  6024. 'type',
  6025. ' TCoin = currency;',
  6026. 'const',
  6027. ' a = TCoin(2.7);',
  6028. ' b = a + TCoin(1.7);',
  6029. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  6030. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  6031. 'var',
  6032. ' c: TCoin = b;',
  6033. ' i: nativeint;',
  6034. ' d: double;',
  6035. ' j: jsvalue;',
  6036. 'function DoIt(c: currency): currency; begin end;',
  6037. 'function GetIt(d: double): double; begin end;',
  6038. 'procedure Write(v: jsvalue); begin end;',
  6039. 'begin',
  6040. ' c:=1.0;',
  6041. ' c:=0.1;',
  6042. ' c:=1.0/3.0;',
  6043. ' c:=1/3;',
  6044. ' c:=a;',
  6045. ' d:=c;',
  6046. ' c:=d;',
  6047. ' c:=currency(c);',
  6048. ' c:=currency(d);',
  6049. ' d:=double(c);',
  6050. ' c:=i;',
  6051. ' c:=currency(i);',
  6052. //' i:=c;', not allowed
  6053. ' i:=nativeint(c);',
  6054. ' c:=c+a;',
  6055. ' c:=-c-a;',
  6056. ' c:=d+c;',
  6057. ' c:=c+d;',
  6058. ' c:=d-c;',
  6059. ' c:=c-d;',
  6060. ' c:=c*a;',
  6061. ' c:=a*c;',
  6062. ' c:=d*c;',
  6063. ' c:=c*d;',
  6064. ' c:=c/a;',
  6065. ' c:=a/c;',
  6066. ' c:=d/c;',
  6067. ' c:=c/d;',
  6068. ' c:=c**a;',
  6069. ' c:=a**c;',
  6070. ' c:=d**c;',
  6071. ' c:=c**d;',
  6072. ' if c=c then ;',
  6073. ' if c=a then ;',
  6074. ' if a=c then ;',
  6075. ' if d=c then ;',
  6076. ' if c=d then ;',
  6077. ' c:=DoIt(c);',
  6078. ' c:=DoIt(i);',
  6079. ' c:=DoIt(d);',
  6080. ' c:=GetIt(c);',
  6081. ' j:=c;',
  6082. ' Write(c);',
  6083. ' c:=default(currency);',
  6084. ' j:=str(c);',
  6085. ' j:=str(c:0:3);',
  6086. '']);
  6087. ConvertProgram;
  6088. CheckSource('TestCurrency',
  6089. LinesToStr([
  6090. 'this.a = 27000;',
  6091. 'this.b = $mod.a + 17000;',
  6092. 'this.MinSafeIntCurrency = -92233720368.5477;',
  6093. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  6094. 'this.c = $mod.b;',
  6095. 'this.i = 0;',
  6096. 'this.d = 0.0;',
  6097. 'this.j = undefined;',
  6098. 'this.DoIt = function (c) {',
  6099. ' var Result = 0;',
  6100. ' return Result;',
  6101. '};',
  6102. 'this.GetIt = function (d) {',
  6103. ' var Result = 0.0;',
  6104. ' return Result;',
  6105. '};',
  6106. 'this.Write = function (v) {',
  6107. '};',
  6108. '']),
  6109. LinesToStr([
  6110. '$mod.c = 10000;',
  6111. '$mod.c = 1000;',
  6112. '$mod.c = Math.floor((1.0 / 3.0) * 10000);',
  6113. '$mod.c = Math.floor((1 / 3) * 10000);',
  6114. '$mod.c = $mod.a;',
  6115. '$mod.d = $mod.c / 10000;',
  6116. '$mod.c = Math.floor($mod.d * 10000);',
  6117. '$mod.c = $mod.c;',
  6118. '$mod.c = $mod.d * 10000;',
  6119. '$mod.d = $mod.c / 10000;',
  6120. '$mod.c = $mod.i * 10000;',
  6121. '$mod.c = $mod.i * 10000;',
  6122. '$mod.i = Math.floor($mod.c / 10000);',
  6123. '$mod.c = $mod.c + $mod.a;',
  6124. '$mod.c = -$mod.c - $mod.a;',
  6125. '$mod.c = ($mod.d * 10000) + $mod.c;',
  6126. '$mod.c = $mod.c + ($mod.d * 10000);',
  6127. '$mod.c = ($mod.d * 10000) - $mod.c;',
  6128. '$mod.c = $mod.c - ($mod.d * 10000);',
  6129. '$mod.c = ($mod.c * $mod.a) / 10000;',
  6130. '$mod.c = ($mod.a * $mod.c) / 10000;',
  6131. '$mod.c = $mod.d * $mod.c;',
  6132. '$mod.c = $mod.c * $mod.d;',
  6133. '$mod.c = Math.floor(($mod.c / $mod.a) * 10000);',
  6134. '$mod.c = Math.floor(($mod.a / $mod.c) * 10000);',
  6135. '$mod.c = Math.floor($mod.d / $mod.c);',
  6136. '$mod.c = Math.floor($mod.c / $mod.d);',
  6137. '$mod.c = Math.floor(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  6138. '$mod.c = Math.floor(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  6139. '$mod.c = Math.floor(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  6140. '$mod.c = Math.floor(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  6141. 'if ($mod.c === $mod.c) ;',
  6142. 'if ($mod.c === $mod.a) ;',
  6143. 'if ($mod.a === $mod.c) ;',
  6144. 'if (($mod.d * 10000) === $mod.c) ;',
  6145. 'if ($mod.c === ($mod.d * 10000)) ;',
  6146. '$mod.c = $mod.DoIt($mod.c);',
  6147. '$mod.c = $mod.DoIt($mod.i * 10000);',
  6148. '$mod.c = $mod.DoIt($mod.d * 10000);',
  6149. '$mod.c = Math.floor($mod.GetIt($mod.c / 10000) * 10000);',
  6150. '$mod.j = $mod.c / 10000;',
  6151. '$mod.Write($mod.c / 10000);',
  6152. '$mod.c = 0;',
  6153. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  6154. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  6155. '']));
  6156. end;
  6157. procedure TTestModule.TestForBoolDo;
  6158. begin
  6159. StartProgram(false);
  6160. Add([
  6161. 'var b: boolean;',
  6162. 'begin',
  6163. ' for b:=false to true do ;',
  6164. ' for b:=b downto false do ;',
  6165. ' for b in boolean do ;',
  6166. '']);
  6167. ConvertProgram;
  6168. CheckSource('TestForBoolDo',
  6169. LinesToStr([ // statements
  6170. 'this.b = false;']),
  6171. LinesToStr([ // this.$main
  6172. 'for (var $l1 = 0; $l1 <= 1; $l1++) $mod.b = $l1 !== 0;',
  6173. 'for (var $l2 = +$mod.b; $l2 >= 0; $l2--) $mod.b = $l2 !== 0;',
  6174. 'for (var $l3 = 0; $l3 <= 1; $l3++) $mod.b = $l3 !== 0;',
  6175. '']));
  6176. end;
  6177. procedure TTestModule.TestForIntDo;
  6178. begin
  6179. StartProgram(false);
  6180. Add([
  6181. 'var i: longint;',
  6182. 'begin',
  6183. ' for i:=3 to 5 do ;',
  6184. ' for i:=i downto 2 do ;',
  6185. ' for i in byte do ;',
  6186. '']);
  6187. ConvertProgram;
  6188. CheckSource('TestForIntDo',
  6189. LinesToStr([ // statements
  6190. 'this.i = 0;']),
  6191. LinesToStr([ // this.$main
  6192. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  6193. 'for (var $l1 = $mod.i; $l1 >= 2; $l1--) $mod.i = $l1;',
  6194. 'for (var $l2 = 0; $l2 <= 255; $l2++) $mod.i = $l2;',
  6195. '']));
  6196. end;
  6197. procedure TTestModule.TestForIntInDo;
  6198. begin
  6199. StartProgram(false);
  6200. Add([
  6201. 'type',
  6202. ' TSetOfInt = set of byte;',
  6203. ' TIntRg = 3..7;',
  6204. ' TSetOfIntRg = set of TIntRg;',
  6205. 'var',
  6206. ' i,i2: longint;',
  6207. ' a1: array of byte;',
  6208. ' a2: array[1..3] of byte;',
  6209. ' soi: TSetOfInt;',
  6210. ' soir: TSetOfIntRg;',
  6211. ' ir: TIntRg;',
  6212. 'begin',
  6213. ' for i in byte do ;',
  6214. ' for i in a1 do ;',
  6215. ' for i in a2 do ;',
  6216. ' for i in [11..13] do ;',
  6217. ' for i in TSetOfInt do ;',
  6218. ' for i in TIntRg do ;',
  6219. ' for i in soi do i2:=i;',
  6220. ' for i in TSetOfIntRg do ;',
  6221. ' for i in soir do ;',
  6222. ' for ir in TIntRg do ;',
  6223. ' for ir in TSetOfIntRg do ;',
  6224. ' for ir in soir do ;',
  6225. '']);
  6226. ConvertProgram;
  6227. CheckSource('TestForIntInDo',
  6228. LinesToStr([ // statements
  6229. 'this.i = 0;',
  6230. 'this.i2 = 0;',
  6231. 'this.a1 = [];',
  6232. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  6233. 'this.soi = {};',
  6234. 'this.soir = {};',
  6235. 'this.ir = 0;',
  6236. '']),
  6237. LinesToStr([ // this.$main
  6238. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  6239. 'for (var $in2 = $mod.a1, $l3 = 0, $end4 = rtl.length($in2) - 1; $l3 <= $end4; $l3++) $mod.i = $in2[$l3];',
  6240. 'for (var $in5 = $mod.a2, $l6 = 0, $end7 = rtl.length($in5) - 1; $l6 <= $end7; $l6++) $mod.i = $in5[$l6];',
  6241. 'for (var $l8 = 11; $l8 <= 13; $l8++) $mod.i = $l8;',
  6242. 'for (var $l9 = 0; $l9 <= 255; $l9++) $mod.i = $l9;',
  6243. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.i = $l10;',
  6244. 'for (var $l11 in $mod.soi) {',
  6245. ' $mod.i = +$l11;',
  6246. ' $mod.i2 = $mod.i;',
  6247. '};',
  6248. 'for (var $l12 = 3; $l12 <= 7; $l12++) $mod.i = $l12;',
  6249. 'for (var $l13 in $mod.soir) $mod.i = +$l13;',
  6250. 'for (var $l14 = 3; $l14 <= 7; $l14++) $mod.ir = $l14;',
  6251. 'for (var $l15 = 3; $l15 <= 7; $l15++) $mod.ir = $l15;',
  6252. 'for (var $l16 in $mod.soir) $mod.ir = +$l16;',
  6253. '']));
  6254. end;
  6255. procedure TTestModule.TestCharConst;
  6256. begin
  6257. StartProgram(false);
  6258. Add([
  6259. 'const',
  6260. ' a = #$00F3;',
  6261. ' c: char = ''1'';',
  6262. 'begin',
  6263. ' c:=#0;',
  6264. ' c:=#1;',
  6265. ' c:=#9;',
  6266. ' c:=#10;',
  6267. ' c:=#13;',
  6268. ' c:=#31;',
  6269. ' c:=#32;',
  6270. ' c:=#$A;',
  6271. ' c:=#$0A;',
  6272. ' c:=#$b;',
  6273. ' c:=#$0b;',
  6274. ' c:=^A;',
  6275. ' c:=''"'';',
  6276. ' c:=default(char);',
  6277. ' c:=#$00E4;', // ä
  6278. ' c:=''ä'';',
  6279. ' c:=#$E4;', // ä
  6280. ' c:=#$D800;', // invalid UTF-16
  6281. ' c:=#$DFFF;', // invalid UTF-16
  6282. ' c:=#$FFFF;', // last UCS-2
  6283. ' c:=high(c);', // last UCS-2
  6284. '']);
  6285. ConvertProgram;
  6286. CheckSource('TestCharConst',
  6287. LinesToStr([
  6288. 'this.a="ó";',
  6289. 'this.c="1";'
  6290. ]),
  6291. LinesToStr([
  6292. '$mod.c="\x00";',
  6293. '$mod.c="\x01";',
  6294. '$mod.c="\t";',
  6295. '$mod.c="\n";',
  6296. '$mod.c="\r";',
  6297. '$mod.c="\x1F";',
  6298. '$mod.c=" ";',
  6299. '$mod.c="\n";',
  6300. '$mod.c="\n";',
  6301. '$mod.c="\x0B";',
  6302. '$mod.c="\x0B";',
  6303. '$mod.c="\x01";',
  6304. '$mod.c=''"'';',
  6305. '$mod.c="\x00";',
  6306. '$mod.c = "ä";',
  6307. '$mod.c = "ä";',
  6308. '$mod.c = "ä";',
  6309. '$mod.c="\uD800";',
  6310. '$mod.c="\uDFFF";',
  6311. '$mod.c="\uFFFF";',
  6312. '$mod.c="\uFFFF";',
  6313. '']));
  6314. end;
  6315. procedure TTestModule.TestChar_Compare;
  6316. begin
  6317. StartProgram(false);
  6318. Add('var');
  6319. Add(' c: char;');
  6320. Add(' b: boolean;');
  6321. Add('begin');
  6322. Add(' b:=c=''1'';');
  6323. Add(' b:=''2''=c;');
  6324. Add(' b:=''3''=''4'';');
  6325. Add(' b:=c<>''5'';');
  6326. Add(' b:=''6''<>c;');
  6327. Add(' b:=c>''7'';');
  6328. Add(' b:=''8''>c;');
  6329. Add(' b:=c>=''9'';');
  6330. Add(' b:=''A''>=c;');
  6331. Add(' b:=c<''B'';');
  6332. Add(' b:=''C''<c;');
  6333. Add(' b:=c<=''D'';');
  6334. Add(' b:=''E''<=c;');
  6335. ConvertProgram;
  6336. CheckSource('TestChar_Compare',
  6337. LinesToStr([
  6338. 'this.c="";',
  6339. 'this.b = false;'
  6340. ]),
  6341. LinesToStr([
  6342. '$mod.b = $mod.c === "1";',
  6343. '$mod.b = "2" === $mod.c;',
  6344. '$mod.b = "3" === "4";',
  6345. '$mod.b = $mod.c !== "5";',
  6346. '$mod.b = "6" !== $mod.c;',
  6347. '$mod.b = $mod.c > "7";',
  6348. '$mod.b = "8" > $mod.c;',
  6349. '$mod.b = $mod.c >= "9";',
  6350. '$mod.b = "A" >= $mod.c;',
  6351. '$mod.b = $mod.c < "B";',
  6352. '$mod.b = "C" < $mod.c;',
  6353. '$mod.b = $mod.c <= "D";',
  6354. '$mod.b = "E" <= $mod.c;',
  6355. '']));
  6356. end;
  6357. procedure TTestModule.TestChar_BuiltInProcs;
  6358. begin
  6359. StartProgram(false);
  6360. Add([
  6361. 'var',
  6362. ' c: char;',
  6363. ' i: longint;',
  6364. ' s: string;',
  6365. 'begin',
  6366. ' i:=ord(c);',
  6367. ' i:=ord(s[i]);',
  6368. ' c:=chr(i);',
  6369. ' c:=pred(c);',
  6370. ' c:=succ(c);',
  6371. ' c:=low(c);',
  6372. ' c:=high(c);',
  6373. ' i:=byte(c);',
  6374. ' i:=word(c);',
  6375. ' i:=longint(c);',
  6376. '']);
  6377. ConvertProgram;
  6378. CheckSource('TestChar_BuiltInProcs',
  6379. LinesToStr([
  6380. 'this.c = "";',
  6381. 'this.i = 0;',
  6382. 'this.s = "";'
  6383. ]),
  6384. LinesToStr([
  6385. '$mod.i = $mod.c.charCodeAt();',
  6386. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  6387. '$mod.c = String.fromCharCode($mod.i);',
  6388. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  6389. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  6390. '$mod.c = "\x00";',
  6391. '$mod.c = "\uFFFF";',
  6392. '$mod.i = $mod.c.charCodeAt() & 255;',
  6393. '$mod.i = $mod.c.charCodeAt();',
  6394. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  6395. '']));
  6396. end;
  6397. procedure TTestModule.TestStringConst;
  6398. begin
  6399. StartProgram(false);
  6400. Add([
  6401. '{$H+}',
  6402. 'const',
  6403. ' a = #$00F3#$017C;', // first <256, then >=256
  6404. 'var',
  6405. ' s: string = ''abc'';',
  6406. 'begin',
  6407. ' s:='''';',
  6408. ' s:=#13#10;',
  6409. ' s:=#9''foo'';',
  6410. ' s:=#$A9;',
  6411. ' s:=''foo''#13''bar'';',
  6412. ' s:=''"'';',
  6413. ' s:=''"''''"'';',
  6414. ' s:=#$20AC;', // euro
  6415. ' s:=#$10437;', // outside BMP
  6416. ' s:=default(string);',
  6417. ' s:=concat(s);',
  6418. ' s:=concat(s,''a'',s)',
  6419. '']);
  6420. ConvertProgram;
  6421. CheckSource('TestStringConst',
  6422. LinesToStr([
  6423. 'this.a = "óż";',
  6424. 'this.s="abc";'
  6425. ]),
  6426. LinesToStr([
  6427. '$mod.s="";',
  6428. '$mod.s="\r\n";',
  6429. '$mod.s="\tfoo";',
  6430. '$mod.s="©";',
  6431. '$mod.s="foo\rbar";',
  6432. '$mod.s=''"'';',
  6433. '$mod.s=''"\''"'';',
  6434. '$mod.s="€";',
  6435. '$mod.s="'#$F0#$90#$90#$B7'";',
  6436. '$mod.s="";',
  6437. '$mod.s = $mod.s;',
  6438. '$mod.s = $mod.s.concat("a", $mod.s);',
  6439. '']));
  6440. end;
  6441. procedure TTestModule.TestStringConstSurrogate;
  6442. begin
  6443. StartProgram(false);
  6444. Add([
  6445. 'var',
  6446. ' s: string;',
  6447. 'begin',
  6448. ' s:=''😊'';', // 1F60A
  6449. '']);
  6450. ConvertProgram;
  6451. CheckSource('TestStringConstSurrogate',
  6452. LinesToStr([
  6453. 'this.s="";'
  6454. ]),
  6455. LinesToStr([
  6456. '$mod.s="😊";'
  6457. ]));
  6458. end;
  6459. procedure TTestModule.TestString_Length;
  6460. begin
  6461. StartProgram(false);
  6462. Add('const c = ''foo'';');
  6463. Add('var');
  6464. Add(' s: string;');
  6465. Add(' i: longint;');
  6466. Add('begin');
  6467. Add(' i:=length(s);');
  6468. Add(' i:=length(s+s);');
  6469. Add(' i:=length(''abc'');');
  6470. Add(' i:=length(c);');
  6471. ConvertProgram;
  6472. CheckSource('TestString_Length',
  6473. LinesToStr([
  6474. 'this.c = "foo";',
  6475. 'this.s = "";',
  6476. 'this.i = 0;',
  6477. '']),
  6478. LinesToStr([
  6479. '$mod.i = $mod.s.length;',
  6480. '$mod.i = ($mod.s+$mod.s).length;',
  6481. '$mod.i = "abc".length;',
  6482. '$mod.i = $mod.c.length;',
  6483. '']));
  6484. end;
  6485. procedure TTestModule.TestString_Compare;
  6486. begin
  6487. StartProgram(false);
  6488. Add('var');
  6489. Add(' s, t: string;');
  6490. Add(' b: boolean;');
  6491. Add('begin');
  6492. Add(' b:=s=t;');
  6493. Add(' b:=s<>t;');
  6494. Add(' b:=s>t;');
  6495. Add(' b:=s>=t;');
  6496. Add(' b:=s<t;');
  6497. Add(' b:=s<=t;');
  6498. ConvertProgram;
  6499. CheckSource('TestString_Compare',
  6500. LinesToStr([ // statements
  6501. 'this.s = "";',
  6502. 'this.t = "";',
  6503. 'this.b =false;'
  6504. ]),
  6505. LinesToStr([ // this.$main
  6506. '$mod.b = $mod.s === $mod.t;',
  6507. '$mod.b = $mod.s !== $mod.t;',
  6508. '$mod.b = $mod.s > $mod.t;',
  6509. '$mod.b = $mod.s >= $mod.t;',
  6510. '$mod.b = $mod.s < $mod.t;',
  6511. '$mod.b = $mod.s <= $mod.t;',
  6512. '']));
  6513. end;
  6514. procedure TTestModule.TestString_SetLength;
  6515. begin
  6516. StartProgram(false);
  6517. Add([
  6518. 'procedure DoIt(var s: string);',
  6519. 'begin',
  6520. ' SetLength(s,2);',
  6521. 'end;',
  6522. 'var s: string;',
  6523. 'begin',
  6524. ' SetLength(s,3);',
  6525. '']);
  6526. ConvertProgram;
  6527. CheckSource('TestString_SetLength',
  6528. LinesToStr([ // statements
  6529. 'this.DoIt = function (s) {',
  6530. ' s.set(rtl.strSetLength(s.get(), 2));',
  6531. '};',
  6532. 'this.s = "";',
  6533. '']),
  6534. LinesToStr([ // this.$main
  6535. '$mod.s = rtl.strSetLength($mod.s, 3);'
  6536. ]));
  6537. end;
  6538. procedure TTestModule.TestString_CharAt;
  6539. begin
  6540. StartProgram(false);
  6541. Add([
  6542. 'var',
  6543. ' s: string;',
  6544. ' c: char;',
  6545. ' b: boolean;',
  6546. 'begin',
  6547. ' b:= s[1] = c;',
  6548. ' b:= c = s[1];',
  6549. ' b:= c <> s[1];',
  6550. ' b:= c > s[1];',
  6551. ' b:= c >= s[1];',
  6552. ' b:= c < s[2];',
  6553. ' b:= c <= s[1];',
  6554. ' s[1] := c;',
  6555. ' s[2+3] := c;']);
  6556. ConvertProgram;
  6557. CheckSource('TestString_CharAt',
  6558. LinesToStr([ // statements
  6559. 'this.s = "";',
  6560. 'this.c = "";',
  6561. 'this.b = false;'
  6562. ]),
  6563. LinesToStr([ // this.$main
  6564. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  6565. '$mod.b = $mod.c === $mod.s.charAt(0);',
  6566. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  6567. '$mod.b = $mod.c > $mod.s.charAt(0);',
  6568. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  6569. '$mod.b = $mod.c < $mod.s.charAt(1);',
  6570. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  6571. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  6572. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  6573. '']));
  6574. end;
  6575. procedure TTestModule.TestStringHMinusFail;
  6576. begin
  6577. StartProgram(false);
  6578. Add([
  6579. '{$H-}',
  6580. 'var s: string;',
  6581. 'begin']);
  6582. ConvertProgram;
  6583. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  6584. end;
  6585. procedure TTestModule.TestStr;
  6586. begin
  6587. StartProgram(false);
  6588. Add('var');
  6589. Add(' b: boolean;');
  6590. Add(' i: longint;');
  6591. Add(' d: double;');
  6592. Add(' s: string;');
  6593. Add('begin');
  6594. Add(' str(b,s);');
  6595. Add(' str(i,s);');
  6596. Add(' str(d,s);');
  6597. Add(' str(i:3,s);');
  6598. Add(' str(d:3:2,s);');
  6599. Add(' Str(12.456:12:1,s);');
  6600. Add(' Str(12.456:12,s);');
  6601. Add(' s:=str(b);');
  6602. Add(' s:=str(i);');
  6603. Add(' s:=str(d);');
  6604. Add(' s:=str(i,i);');
  6605. Add(' s:=str(i:3);');
  6606. Add(' s:=str(d:3:2);');
  6607. Add(' s:=str(i:4,i);');
  6608. Add(' s:=str(i,i:5);');
  6609. Add(' s:=str(i:4,i:5);');
  6610. Add(' s:=str(s,s);');
  6611. Add(' s:=str(s,''foo'');');
  6612. ConvertProgram;
  6613. CheckSource('TestStr',
  6614. LinesToStr([ // statements
  6615. 'this.b = false;',
  6616. 'this.i = 0;',
  6617. 'this.d = 0.0;',
  6618. 'this.s = "";',
  6619. '']),
  6620. LinesToStr([ // this.$main
  6621. '$mod.s = ""+$mod.b;',
  6622. '$mod.s = ""+$mod.i;',
  6623. '$mod.s = rtl.floatToStr($mod.d);',
  6624. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  6625. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  6626. '$mod.s = rtl.floatToStr(12.456,12,1);',
  6627. '$mod.s = rtl.floatToStr(12.456,12);',
  6628. '$mod.s = ""+$mod.b;',
  6629. '$mod.s = ""+$mod.i;',
  6630. '$mod.s = rtl.floatToStr($mod.d);',
  6631. '$mod.s = ""+$mod.i+$mod.i;',
  6632. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  6633. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  6634. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  6635. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  6636. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  6637. '$mod.s = $mod.s + $mod.s;',
  6638. '$mod.s = $mod.s + "foo";',
  6639. '']));
  6640. end;
  6641. procedure TTestModule.TestBaseType_AnsiStringFail;
  6642. begin
  6643. StartProgram(false);
  6644. Add('var s: AnsiString');
  6645. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  6646. ConvertProgram;
  6647. end;
  6648. procedure TTestModule.TestBaseType_WideStringFail;
  6649. begin
  6650. StartProgram(false);
  6651. Add('var s: WideString');
  6652. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  6653. ConvertProgram;
  6654. end;
  6655. procedure TTestModule.TestBaseType_ShortStringFail;
  6656. begin
  6657. StartProgram(false);
  6658. Add('var s: ShortString');
  6659. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  6660. ConvertProgram;
  6661. end;
  6662. procedure TTestModule.TestBaseType_RawByteStringFail;
  6663. begin
  6664. StartProgram(false);
  6665. Add('var s: RawByteString');
  6666. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  6667. ConvertProgram;
  6668. end;
  6669. procedure TTestModule.TestTypeShortstring_Fail;
  6670. begin
  6671. StartProgram(false);
  6672. Add('type t = string[12];');
  6673. Add('var s: t;');
  6674. Add('begin');
  6675. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  6676. ConvertProgram;
  6677. end;
  6678. procedure TTestModule.TestCharSet_Custom;
  6679. begin
  6680. StartProgram(false);
  6681. Add([
  6682. 'type',
  6683. ' TCharRg = ''a''..''z'';',
  6684. ' TSetOfCharRg = set of TCharRg;',
  6685. ' TCharRg2 = ''m''..''p'';',
  6686. 'const',
  6687. ' crg: TCharRg = ''b'';',
  6688. 'var',
  6689. ' c: char;',
  6690. ' crg2: TCharRg2;',
  6691. ' s: TSetOfCharRg;',
  6692. 'begin',
  6693. ' c:=crg;',
  6694. ' crg:=c;',
  6695. ' crg2:=crg;',
  6696. ' if c=crg then ;',
  6697. ' if crg=c then ;',
  6698. ' if crg=crg2 then ;',
  6699. ' if c in s then ;',
  6700. ' if crg2 in s then ;',
  6701. ' c:=default(TCharRg);',
  6702. '']);
  6703. ConvertProgram;
  6704. CheckSource('TestCharSet_Custom',
  6705. LinesToStr([ // statements
  6706. 'this.crg = "b";',
  6707. 'this.c = "";',
  6708. 'this.crg2 = "m";',
  6709. 'this.s = {};',
  6710. '']),
  6711. LinesToStr([ // this.$main
  6712. '$mod.c = $mod.crg;',
  6713. '$mod.crg = $mod.c;',
  6714. '$mod.crg2 = $mod.crg;',
  6715. 'if ($mod.c === $mod.crg) ;',
  6716. 'if ($mod.crg === $mod.c) ;',
  6717. 'if ($mod.crg === $mod.crg2) ;',
  6718. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  6719. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  6720. '$mod.c = "a";',
  6721. '']));
  6722. end;
  6723. procedure TTestModule.TestForCharDo;
  6724. begin
  6725. StartProgram(false);
  6726. Add([
  6727. 'var c: char;',
  6728. 'begin',
  6729. ' for c:=''a'' to ''c'' do ;',
  6730. ' for c:=c downto ''a'' do ;',
  6731. ' for c:=''Б'' to ''Я'' do ;',
  6732. '']);
  6733. ConvertProgram;
  6734. CheckSource('TestForCharDo',
  6735. LinesToStr([ // statements
  6736. 'this.c = "";']),
  6737. LinesToStr([ // this.$main
  6738. 'for (var $l1 = 97; $l1 <= 99; $l1++) $mod.c = String.fromCharCode($l1);',
  6739. 'for (var $l2 = $mod.c.charCodeAt(); $l2 >= 97; $l2--) $mod.c = String.fromCharCode($l2);',
  6740. 'for (var $l3 = 1041; $l3 <= 1071; $l3++) $mod.c = String.fromCharCode($l3);',
  6741. '']));
  6742. end;
  6743. procedure TTestModule.TestForCharInDo;
  6744. begin
  6745. StartProgram(false);
  6746. Add([
  6747. 'type',
  6748. ' TSetOfChar = set of char;',
  6749. ' TCharRg = ''a''..''z'';',
  6750. ' TSetOfCharRg = set of TCharRg;',
  6751. 'const Foo = ''foo'';',
  6752. 'var',
  6753. ' c,c2: char;',
  6754. ' s: string;',
  6755. ' a1: array of char;',
  6756. ' a2: array[1..3] of char;',
  6757. ' soc: TSetOfChar;',
  6758. ' socr: TSetOfCharRg;',
  6759. ' cr: TCharRg;',
  6760. 'begin',
  6761. ' for c in foo do ;',
  6762. ' for c in s do ;',
  6763. ' for c in char do ;',
  6764. ' for c in a1 do ;',
  6765. ' for c in a2 do ;',
  6766. ' for c in [''1''..''3''] do ;',
  6767. ' for c in TSetOfChar do ;',
  6768. ' for c in TCharRg do ;',
  6769. ' for c in soc do c2:=c;',
  6770. ' for c in TSetOfCharRg do ;',
  6771. ' for c in socr do ;',
  6772. ' for cr in TCharRg do ;',
  6773. ' for cr in TSetOfCharRg do ;',
  6774. ' for cr in socr do ;',
  6775. '']);
  6776. ConvertProgram;
  6777. CheckSource('TestForCharInDo',
  6778. LinesToStr([ // statements
  6779. 'this.Foo = "foo";',
  6780. 'this.c = "";',
  6781. 'this.c2 = "";',
  6782. 'this.s = "";',
  6783. 'this.a1 = [];',
  6784. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  6785. 'this.soc = {};',
  6786. 'this.socr = {};',
  6787. 'this.cr = "a";',
  6788. '']),
  6789. LinesToStr([ // this.$main
  6790. 'for (var $in1 = $mod.Foo, $l2 = 0, $end3 = $in1.length - 1; $l2 <= $end3; $l2++) $mod.c = $in1.charAt($l2);',
  6791. 'for (var $in4 = $mod.s, $l5 = 0, $end6 = $in4.length - 1; $l5 <= $end6; $l5++) $mod.c = $in4.charAt($l5);',
  6792. 'for (var $l7 = 0; $l7 <= 65535; $l7++) $mod.c = String.fromCharCode($l7);',
  6793. 'for (var $in8 = $mod.a1, $l9 = 0, $end10 = rtl.length($in8) - 1; $l9 <= $end10; $l9++) $mod.c = $in8[$l9];',
  6794. 'for (var $in11 = $mod.a2, $l12 = 0, $end13 = rtl.length($in11) - 1; $l12 <= $end13; $l12++) $mod.c = $in11[$l12];',
  6795. 'for (var $l14 = 49; $l14 <= 51; $l14++) $mod.c = String.fromCharCode($l14);',
  6796. 'for (var $l15 = 0; $l15 <= 65535; $l15++) $mod.c = String.fromCharCode($l15);',
  6797. 'for (var $l16 = 97; $l16 <= 122; $l16++) $mod.c = String.fromCharCode($l16);',
  6798. 'for (var $l17 in $mod.soc) {',
  6799. ' $mod.c = String.fromCharCode($l17);',
  6800. ' $mod.c2 = $mod.c;',
  6801. '};',
  6802. 'for (var $l18 = 97; $l18 <= 122; $l18++) $mod.c = String.fromCharCode($l18);',
  6803. 'for (var $l19 in $mod.socr) $mod.c = String.fromCharCode($l19);',
  6804. 'for (var $l20 = 97; $l20 <= 122; $l20++) $mod.cr = String.fromCharCode($l20);',
  6805. 'for (var $l21 = 97; $l21 <= 122; $l21++) $mod.cr = String.fromCharCode($l21);',
  6806. 'for (var $l22 in $mod.socr) $mod.cr = String.fromCharCode($l22);',
  6807. '']));
  6808. end;
  6809. procedure TTestModule.TestProcTwoArgs;
  6810. begin
  6811. StartProgram(false);
  6812. Add('procedure Test(a,b: longint);');
  6813. Add('begin');
  6814. Add('end;');
  6815. Add('begin');
  6816. ConvertProgram;
  6817. CheckSource('TestProcTwoArgs',
  6818. LinesToStr([ // statements
  6819. 'this.Test = function (a,b) {',
  6820. '};'
  6821. ]),
  6822. LinesToStr([ // this.$main
  6823. ''
  6824. ]));
  6825. end;
  6826. procedure TTestModule.TestProc_DefaultValue;
  6827. begin
  6828. StartProgram(false);
  6829. Add('procedure p1(i: longint = 1);');
  6830. Add('begin');
  6831. Add('end;');
  6832. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  6833. Add('begin');
  6834. Add('end;');
  6835. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  6836. Add('begin');
  6837. Add('end;');
  6838. Add('begin');
  6839. Add(' p1;');
  6840. Add(' p1();');
  6841. Add(' p1(11);');
  6842. Add(' p2;');
  6843. Add(' p2();');
  6844. Add(' p2(12);');
  6845. Add(' p2(13,''b'');');
  6846. Add(' p3();');
  6847. ConvertProgram;
  6848. CheckSource('TestProc_DefaultValue',
  6849. LinesToStr([ // statements
  6850. 'this.p1 = function (i) {',
  6851. '};',
  6852. 'this.p2 = function (i,c) {',
  6853. '};',
  6854. 'this.p3 = function (d,b,s) {',
  6855. '};'
  6856. ]),
  6857. LinesToStr([ // this.$main
  6858. ' $mod.p1(1);',
  6859. ' $mod.p1(1);',
  6860. ' $mod.p1(11);',
  6861. ' $mod.p2(1,"a");',
  6862. ' $mod.p2(1,"a");',
  6863. ' $mod.p2(12,"a");',
  6864. ' $mod.p2(13,"b");',
  6865. ' $mod.p3(1.0,false,"abc");'
  6866. ]));
  6867. end;
  6868. procedure TTestModule.TestFunctionInt;
  6869. begin
  6870. StartProgram(false);
  6871. Add('function MyTest(Bar: longint): longint;');
  6872. Add('begin');
  6873. Add(' Result:=2*bar');
  6874. Add('end;');
  6875. Add('begin');
  6876. ConvertProgram;
  6877. CheckSource('TestFunctionInt',
  6878. LinesToStr([ // statements
  6879. 'this.MyTest = function (Bar) {',
  6880. ' var Result = 0;',
  6881. ' Result = 2*Bar;',
  6882. ' return Result;',
  6883. '};'
  6884. ]),
  6885. LinesToStr([ // this.$main
  6886. ''
  6887. ]));
  6888. end;
  6889. procedure TTestModule.TestFunctionString;
  6890. begin
  6891. StartProgram(false);
  6892. Add('function Test(Bar: string): string;');
  6893. Add('begin');
  6894. Add(' Result:=bar+BAR');
  6895. Add('end;');
  6896. Add('begin');
  6897. ConvertProgram;
  6898. CheckSource('TestFunctionString',
  6899. LinesToStr([ // statements
  6900. 'this.Test = function (Bar) {',
  6901. ' var Result = "";',
  6902. ' Result = Bar+Bar;',
  6903. ' return Result;',
  6904. '};'
  6905. ]),
  6906. LinesToStr([ // this.$main
  6907. ''
  6908. ]));
  6909. end;
  6910. procedure TTestModule.TestIfThen;
  6911. begin
  6912. StartProgram(false);
  6913. Add([
  6914. 'var b: boolean;',
  6915. 'begin',
  6916. ' if b then ;',
  6917. ' if b then else ;']);
  6918. ConvertProgram;
  6919. CheckSource('TestIfThen',
  6920. LinesToStr([ // statements
  6921. 'this.b = false;',
  6922. '']),
  6923. LinesToStr([ // this.$main
  6924. 'if ($mod.b) ;',
  6925. 'if ($mod.b) ;',
  6926. '']));
  6927. end;
  6928. procedure TTestModule.TestForLoop;
  6929. begin
  6930. StartProgram(false);
  6931. Add('var');
  6932. Add(' vI, vJ, vN: longint;');
  6933. Add('begin');
  6934. Add(' VJ:=0;');
  6935. Add(' VN:=3;');
  6936. Add(' for VI:=1 to VN do');
  6937. Add(' begin');
  6938. Add(' VJ:=VJ+VI;');
  6939. Add(' end;');
  6940. ConvertProgram;
  6941. CheckSource('TestForLoop',
  6942. LinesToStr([ // statements
  6943. 'this.vI = 0;',
  6944. 'this.vJ = 0;',
  6945. 'this.vN = 0;'
  6946. ]),
  6947. LinesToStr([ // this.$main
  6948. ' $mod.vJ = 0;',
  6949. ' $mod.vN = 3;',
  6950. ' for (var $l1 = 1, $end2 = $mod.vN; $l1 <= $end2; $l1++) {',
  6951. ' $mod.vI = $l1;',
  6952. ' $mod.vJ = $mod.vJ + $mod.vI;',
  6953. ' };',
  6954. '']));
  6955. end;
  6956. procedure TTestModule.TestForLoopInsideFunction;
  6957. begin
  6958. StartProgram(false);
  6959. Add('function SumNumbers(Count: longint): longint;');
  6960. Add('var');
  6961. Add(' vI, vJ: longint;');
  6962. Add('begin');
  6963. Add(' vj:=0;');
  6964. Add(' for vi:=1 to count do');
  6965. Add(' begin');
  6966. Add(' vj:=vj+vi;');
  6967. Add(' end;');
  6968. Add('end;');
  6969. Add('begin');
  6970. Add(' sumnumbers(3);');
  6971. ConvertProgram;
  6972. CheckSource('TestForLoopInsideFunction',
  6973. LinesToStr([ // statements
  6974. 'this.SumNumbers = function (Count) {',
  6975. ' var Result = 0;',
  6976. ' var vI = 0;',
  6977. ' var vJ = 0;',
  6978. ' vJ = 0;',
  6979. ' for (var $l1 = 1, $end2 = Count; $l1 <= $end2; $l1++) {',
  6980. ' vI = $l1;',
  6981. ' vJ = vJ + vI;',
  6982. ' };',
  6983. ' return Result;',
  6984. '};'
  6985. ]),
  6986. LinesToStr([ // $mod.$main
  6987. ' $mod.SumNumbers(3);'
  6988. ]));
  6989. end;
  6990. procedure TTestModule.TestForLoop_ReadVarAfter;
  6991. begin
  6992. StartProgram(false);
  6993. Add('var');
  6994. Add(' vI: longint;');
  6995. Add('begin');
  6996. Add(' for vi:=1 to 2 do ;');
  6997. Add(' if vi=3 then ;');
  6998. ConvertProgram;
  6999. CheckSource('TestForLoop',
  7000. LinesToStr([ // statements
  7001. 'this.vI = 0;'
  7002. ]),
  7003. LinesToStr([ // this.$main
  7004. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  7005. ' if ($mod.vI===3) ;'
  7006. ]));
  7007. end;
  7008. procedure TTestModule.TestForLoop_Nested;
  7009. begin
  7010. StartProgram(false);
  7011. Add('function SumNumbers(Count: longint): longint;');
  7012. Add('var');
  7013. Add(' vI, vJ, vK: longint;');
  7014. Add('begin');
  7015. Add(' VK:=0;');
  7016. Add(' for VI:=1 to count do');
  7017. Add(' begin');
  7018. Add(' for vj:=1 to vi do');
  7019. Add(' begin');
  7020. Add(' vk:=VK+VI;');
  7021. Add(' end;');
  7022. Add(' end;');
  7023. Add('end;');
  7024. Add('begin');
  7025. Add(' sumnumbers(3);');
  7026. ConvertProgram;
  7027. CheckSource('TestForLoopInFunction',
  7028. LinesToStr([ // statements
  7029. 'this.SumNumbers = function (Count) {',
  7030. ' var Result = 0;',
  7031. ' var vI = 0;',
  7032. ' var vJ = 0;',
  7033. ' var vK = 0;',
  7034. ' vK = 0;',
  7035. ' for (var $l1 = 1, $end2 = Count; $l1 <= $end2; $l1++) {',
  7036. ' vI = $l1;',
  7037. ' for (var $l3 = 1, $end4 = vI; $l3 <= $end4; $l3++) {',
  7038. ' vJ = $l3;',
  7039. ' vK = vK + vI;',
  7040. ' };',
  7041. ' };',
  7042. ' return Result;',
  7043. '};'
  7044. ]),
  7045. LinesToStr([ // $mod.$main
  7046. ' $mod.SumNumbers(3);'
  7047. ]));
  7048. end;
  7049. procedure TTestModule.TestRepeatUntil;
  7050. begin
  7051. StartProgram(false);
  7052. Add('var');
  7053. Add(' vI, vJ, vN: longint;');
  7054. Add('begin');
  7055. Add(' vn:=3;');
  7056. Add(' vj:=0;');
  7057. Add(' VI:=0;');
  7058. Add(' repeat');
  7059. Add(' VI:=vi+1;');
  7060. Add(' vj:=VJ+vI;');
  7061. Add(' until vi>=vn');
  7062. ConvertProgram;
  7063. CheckSource('TestRepeatUntil',
  7064. LinesToStr([ // statements
  7065. 'this.vI = 0;',
  7066. 'this.vJ = 0;',
  7067. 'this.vN = 0;'
  7068. ]),
  7069. LinesToStr([ // $mod.$main
  7070. ' $mod.vN = 3;',
  7071. ' $mod.vJ = 0;',
  7072. ' $mod.vI = 0;',
  7073. ' do{',
  7074. ' $mod.vI = $mod.vI + 1;',
  7075. ' $mod.vJ = $mod.vJ + $mod.vI;',
  7076. ' }while(!($mod.vI>=$mod.vN));'
  7077. ]));
  7078. end;
  7079. procedure TTestModule.TestAsmBlock;
  7080. begin
  7081. StartProgram(false);
  7082. Add([
  7083. 'var',
  7084. ' vI: longint;',
  7085. 'begin',
  7086. ' vi:=1;',
  7087. ' asm',
  7088. ' if (vI===1) {',
  7089. ' vI=2;',
  7090. //' console.log(''end;'');', ToDo
  7091. ' }',
  7092. ' if (vI===2){ vI=3; }',
  7093. ' end;',
  7094. ' VI:=4;']);
  7095. ConvertProgram;
  7096. CheckSource('TestAsmBlock',
  7097. LinesToStr([ // statements
  7098. 'this.vI = 0;'
  7099. ]),
  7100. LinesToStr([ // $mod.$main
  7101. '$mod.vI = 1;',
  7102. 'if (vI===1) {',
  7103. ' vI=2;',
  7104. '}',
  7105. 'if (vI===2){ vI=3; }',
  7106. ';',
  7107. '$mod.vI = 4;'
  7108. ]));
  7109. end;
  7110. procedure TTestModule.TestAsmPas_Impl;
  7111. begin
  7112. StartUnit(false);
  7113. Add('interface');
  7114. Add('const cIntf: longint = 1;');
  7115. Add('var vIntf: longint;');
  7116. Add('implementation');
  7117. Add('const cImpl: longint = 2;');
  7118. Add('var vImpl: longint;');
  7119. Add('procedure DoIt;');
  7120. Add('const cLoc: longint = 3;');
  7121. Add('var vLoc: longint;');
  7122. Add('begin;');
  7123. Add(' asm');
  7124. //Add(' pas(vIntf)=pas(cIntf);');
  7125. //Add(' pas(vImpl)=pas(cImpl);');
  7126. //Add(' pas(vLoc)=pas(cLoc);');
  7127. Add(' end;');
  7128. Add('end;');
  7129. ConvertUnit;
  7130. CheckSource('TestAsmPas_Impl',
  7131. LinesToStr([
  7132. 'var $impl = $mod.$impl;',
  7133. 'this.cIntf = 1;',
  7134. 'this.vIntf = 0;',
  7135. '']),
  7136. '', // this.$init
  7137. LinesToStr([ // implementation
  7138. '$impl.cImpl = 2;',
  7139. '$impl.vImpl = 0;',
  7140. 'var cLoc = 3;',
  7141. '$impl.DoIt = function () {',
  7142. ' var vLoc = 0;',
  7143. '};',
  7144. '']) );
  7145. end;
  7146. procedure TTestModule.TestTryFinally;
  7147. begin
  7148. StartProgram(false);
  7149. Add('var i: longint;');
  7150. Add('begin');
  7151. Add(' try');
  7152. Add(' i:=0; i:=2 div i;');
  7153. Add(' finally');
  7154. Add(' i:=3');
  7155. Add(' end;');
  7156. ConvertProgram;
  7157. CheckSource('TestTryFinally',
  7158. LinesToStr([ // statements
  7159. 'this.i = 0;'
  7160. ]),
  7161. LinesToStr([ // $mod.$main
  7162. 'try {',
  7163. ' $mod.i = 0;',
  7164. ' $mod.i = Math.floor(2 / $mod.i);',
  7165. '} finally {',
  7166. ' $mod.i = 3;',
  7167. '};'
  7168. ]));
  7169. end;
  7170. procedure TTestModule.TestTryExcept;
  7171. begin
  7172. StartProgram(false);
  7173. Add('type');
  7174. Add(' TObject = class end;');
  7175. Add(' Exception = class Msg: string; end;');
  7176. Add(' EInvalidCast = class(Exception) end;');
  7177. Add('var vI: longint;');
  7178. Add('begin');
  7179. Add(' try');
  7180. Add(' vi:=1;');
  7181. Add(' except');
  7182. Add(' vi:=2');
  7183. Add(' end;');
  7184. Add(' try');
  7185. Add(' vi:=3;');
  7186. Add(' except');
  7187. Add(' raise;');
  7188. Add(' end;');
  7189. Add(' try');
  7190. Add(' VI:=4;');
  7191. Add(' except');
  7192. Add(' on einvalidcast do');
  7193. Add(' raise;');
  7194. Add(' on E: exception do');
  7195. Add(' if e.msg='''' then');
  7196. Add(' raise e;');
  7197. Add(' else');
  7198. Add(' vi:=5');
  7199. Add(' end;');
  7200. Add(' try');
  7201. Add(' VI:=6;');
  7202. Add(' except');
  7203. Add(' on einvalidcast do ;');
  7204. Add(' end;');
  7205. ConvertProgram;
  7206. CheckSource('TestTryExcept',
  7207. LinesToStr([ // statements
  7208. 'rtl.createClass($mod, "TObject", null, function () {',
  7209. ' this.$init = function () {',
  7210. ' };',
  7211. ' this.$final = function () {',
  7212. ' };',
  7213. '});',
  7214. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  7215. ' this.$init = function () {',
  7216. ' $mod.TObject.$init.call(this);',
  7217. ' this.Msg = "";',
  7218. ' };',
  7219. '});',
  7220. 'rtl.createClass($mod, "EInvalidCast", $mod.Exception, function () {',
  7221. '});',
  7222. 'this.vI = 0;'
  7223. ]),
  7224. LinesToStr([ // $mod.$main
  7225. 'try {',
  7226. ' $mod.vI = 1;',
  7227. '} catch ($e) {',
  7228. ' $mod.vI = 2;',
  7229. '};',
  7230. 'try {',
  7231. ' $mod.vI = 3;',
  7232. '} catch ($e) {',
  7233. ' throw $e;',
  7234. '};',
  7235. 'try {',
  7236. ' $mod.vI = 4;',
  7237. '} catch ($e) {',
  7238. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  7239. ' throw $e',
  7240. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  7241. ' var E = $e;',
  7242. ' if (E.Msg === "") throw E;',
  7243. ' } else {',
  7244. ' $mod.vI = 5;',
  7245. ' }',
  7246. '};',
  7247. 'try {',
  7248. ' $mod.vI = 6;',
  7249. '} catch ($e) {',
  7250. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  7251. ' } else throw $e',
  7252. '};',
  7253. '']));
  7254. end;
  7255. procedure TTestModule.TestTryExcept_ReservedWords;
  7256. begin
  7257. StartProgram(false);
  7258. Add([
  7259. 'type',
  7260. ' TObject = class end;',
  7261. ' Exception = class',
  7262. ' Symbol: string;',
  7263. ' end;',
  7264. 'var &try: longint;',
  7265. 'begin',
  7266. ' try',
  7267. ' &try:=4;',
  7268. ' except',
  7269. ' on Error: exception do',
  7270. ' if errOR.symBol='''' then',
  7271. ' raise ERRor;',
  7272. ' end;',
  7273. '']);
  7274. ConvertProgram;
  7275. CheckSource('TestTryExcept_ReservedWords',
  7276. LinesToStr([ // statements
  7277. 'rtl.createClass($mod, "TObject", null, function () {',
  7278. ' this.$init = function () {',
  7279. ' };',
  7280. ' this.$final = function () {',
  7281. ' };',
  7282. '});',
  7283. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  7284. ' this.$init = function () {',
  7285. ' $mod.TObject.$init.call(this);',
  7286. ' this.Symbol = "";',
  7287. ' };',
  7288. '});',
  7289. 'this.Try = 0;',
  7290. '']),
  7291. LinesToStr([ // $mod.$main
  7292. 'try {',
  7293. ' $mod.Try = 4;',
  7294. '} catch ($e) {',
  7295. ' if ($mod.Exception.isPrototypeOf($e)) {',
  7296. ' var error = $e;',
  7297. ' if (error.Symbol === "") throw error;',
  7298. ' } else throw $e',
  7299. '};',
  7300. '']));
  7301. end;
  7302. procedure TTestModule.TestIfThenRaiseElse;
  7303. begin
  7304. StartProgram(false);
  7305. Add([
  7306. 'type',
  7307. ' TObject = class',
  7308. ' constructor Create;',
  7309. ' end;',
  7310. 'constructor TObject.Create;',
  7311. 'begin',
  7312. 'end;',
  7313. 'var b: boolean;',
  7314. 'begin',
  7315. ' if b then',
  7316. ' raise TObject.Create',
  7317. ' else',
  7318. ' b:=false;',
  7319. '']);
  7320. ConvertProgram;
  7321. CheckSource('TestIfThenRaiseElse',
  7322. LinesToStr([ // statements
  7323. 'rtl.createClass($mod, "TObject", null, function () {',
  7324. ' this.$init = function () {',
  7325. ' };',
  7326. ' this.$final = function () {',
  7327. ' };',
  7328. ' this.Create = function () {',
  7329. ' return this;',
  7330. ' };',
  7331. '});',
  7332. 'this.b = false;',
  7333. '']),
  7334. LinesToStr([ // $mod.$main
  7335. 'if ($mod.b) {',
  7336. ' throw $mod.TObject.$create("Create")}',
  7337. ' else $mod.b = false;',
  7338. '']));
  7339. end;
  7340. procedure TTestModule.TestCaseOf;
  7341. begin
  7342. StartProgram(false);
  7343. Add([
  7344. 'const e: longint; external name ''$e'';',
  7345. 'var vI: longint;',
  7346. 'begin',
  7347. ' case vi of',
  7348. ' 1: ;',
  7349. ' 2: vi:=3;',
  7350. ' e: ;',
  7351. ' else',
  7352. ' VI:=4',
  7353. ' end;']);
  7354. ConvertProgram;
  7355. CheckSource('TestCaseOf',
  7356. LinesToStr([ // statements
  7357. 'this.vI = 0;'
  7358. ]),
  7359. LinesToStr([ // $mod.$main
  7360. 'var $tmp1 = $mod.vI;',
  7361. 'if ($tmp1 === 1) {}',
  7362. 'else if ($tmp1 === 2) {',
  7363. ' $mod.vI = 3}',
  7364. ' else if ($tmp1 === $e) {}',
  7365. 'else {',
  7366. ' $mod.vI = 4;',
  7367. '};'
  7368. ]));
  7369. end;
  7370. procedure TTestModule.TestCaseOf_UseSwitch;
  7371. begin
  7372. StartProgram(false);
  7373. Converter.UseSwitchStatement:=true;
  7374. Add('var Vi: longint;');
  7375. Add('begin');
  7376. Add(' case vi of');
  7377. Add(' 1: ;');
  7378. Add(' 2: VI:=3;');
  7379. Add(' else');
  7380. Add(' vi:=4');
  7381. Add(' end;');
  7382. ConvertProgram;
  7383. CheckSource('TestCaseOf_UseSwitch',
  7384. LinesToStr([ // statements
  7385. 'this.Vi = 0;'
  7386. ]),
  7387. LinesToStr([ // $mod.$main
  7388. 'switch ($mod.Vi) {',
  7389. 'case 1:',
  7390. ' break;',
  7391. 'case 2:',
  7392. ' $mod.Vi = 3;',
  7393. ' break;',
  7394. 'default:',
  7395. ' $mod.Vi = 4;',
  7396. '};'
  7397. ]));
  7398. end;
  7399. procedure TTestModule.TestCaseOfNoElse;
  7400. begin
  7401. StartProgram(false);
  7402. Add('var Vi: longint;');
  7403. Add('begin');
  7404. Add(' case vi of');
  7405. Add(' 1: begin vi:=2; VI:=3; end;');
  7406. Add(' end;');
  7407. ConvertProgram;
  7408. CheckSource('TestCaseOfNoElse',
  7409. LinesToStr([ // statements
  7410. 'this.Vi = 0;'
  7411. ]),
  7412. LinesToStr([ // $mod.$main
  7413. 'var $tmp1 = $mod.Vi;',
  7414. 'if ($tmp1 === 1) {',
  7415. ' $mod.Vi = 2;',
  7416. ' $mod.Vi = 3;',
  7417. '};'
  7418. ]));
  7419. end;
  7420. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  7421. begin
  7422. StartProgram(false);
  7423. Converter.UseSwitchStatement:=true;
  7424. Add('var vI: longint;');
  7425. Add('begin');
  7426. Add(' case vi of');
  7427. Add(' 1: begin VI:=2; vi:=3; end;');
  7428. Add(' end;');
  7429. ConvertProgram;
  7430. CheckSource('TestCaseOfNoElse_UseSwitch',
  7431. LinesToStr([ // statements
  7432. 'this.vI = 0;'
  7433. ]),
  7434. LinesToStr([ // $mod.$main
  7435. 'switch ($mod.vI) {',
  7436. 'case 1:',
  7437. ' $mod.vI = 2;',
  7438. ' $mod.vI = 3;',
  7439. ' break;',
  7440. '};'
  7441. ]));
  7442. end;
  7443. procedure TTestModule.TestCaseOfRange;
  7444. begin
  7445. StartProgram(false);
  7446. Add('var vI: longint;');
  7447. Add('begin');
  7448. Add(' case vi of');
  7449. Add(' 1..3: vi:=14;');
  7450. Add(' 4,5: vi:=16;');
  7451. Add(' 6..7,9..10: ;');
  7452. Add(' else ;');
  7453. Add(' end;');
  7454. ConvertProgram;
  7455. CheckSource('TestCaseOfRange',
  7456. LinesToStr([ // statements
  7457. 'this.vI = 0;'
  7458. ]),
  7459. LinesToStr([ // $mod.$main
  7460. 'var $tmp1 = $mod.vI;',
  7461. 'if (($tmp1 >= 1) && ($tmp1 <= 3)){',
  7462. ' $mod.vI = 14',
  7463. '} else if (($tmp1 === 4) || ($tmp1 === 5)){',
  7464. ' $mod.vI = 16',
  7465. '} else if ((($tmp1 >= 6) && ($tmp1 <= 7)) || (($tmp1 >= 9) && ($tmp1 <= 10))) ;'
  7466. ]));
  7467. end;
  7468. procedure TTestModule.TestCaseOfString;
  7469. begin
  7470. StartProgram(false);
  7471. Add([
  7472. 'var s,h: string;',
  7473. 'begin',
  7474. ' case s of',
  7475. ' ''foo'': s:=h;',
  7476. ' ''a''..''z'': h:=s;',
  7477. ' ''Б''..''Я'': ;',
  7478. ' end;',
  7479. '']);
  7480. ConvertProgram;
  7481. CheckSource('TestCaseOfString',
  7482. LinesToStr([ // statements
  7483. 'this.s = "";',
  7484. 'this.h = "";',
  7485. '']),
  7486. LinesToStr([ // $mod.$main
  7487. 'var $tmp1 = $mod.s;',
  7488. 'if ($tmp1 === "foo") {',
  7489. ' $mod.s = $mod.h}',
  7490. ' else if (($tmp1.length === 1) && ($tmp1 >= "a") && ($tmp1 <= "z")) {',
  7491. ' $mod.h = $mod.s}',
  7492. ' else if (($tmp1.length === 1) && ($tmp1 >= "Б") && ($tmp1 <= "Я")) ;',
  7493. '']));
  7494. end;
  7495. procedure TTestModule.TestCaseOfChar;
  7496. begin
  7497. StartProgram(false);
  7498. Add([
  7499. 'var s,h: char;',
  7500. 'begin',
  7501. ' case s of',
  7502. ' ''a''..''z'': h:=s;',
  7503. ' ''ä'': ;',
  7504. ' ''Б''..''Я'': ;',
  7505. ' end;',
  7506. '']);
  7507. ConvertProgram;
  7508. CheckSource('TestCaseOfString',
  7509. LinesToStr([ // statements
  7510. 'this.s = "";',
  7511. 'this.h = "";',
  7512. '']),
  7513. LinesToStr([ // $mod.$main
  7514. 'var $tmp1 = $mod.s;',
  7515. 'if (($tmp1 >= "a") && ($tmp1 <= "z")) {',
  7516. ' $mod.h = $mod.s}',
  7517. ' else if ($tmp1 === "ä") {}',
  7518. ' else if (($tmp1 >= "Б") && ($tmp1 <= "Я")) ;',
  7519. '']));
  7520. end;
  7521. procedure TTestModule.TestCaseOfExternalClassConst;
  7522. begin
  7523. StartProgram(false);
  7524. Add([
  7525. '{$modeswitch externalclass}',
  7526. 'type',
  7527. ' TBird = class external name ''Bird''',
  7528. ' const e: longint;',
  7529. ' end;',
  7530. 'var vI: longint;',
  7531. 'begin',
  7532. ' case vi of',
  7533. ' 1: vi:=3;',
  7534. ' TBird.e: ;',
  7535. ' end;']);
  7536. ConvertProgram;
  7537. CheckSource('TestCaseOfExternalClassConst',
  7538. LinesToStr([ // statements
  7539. 'this.vI = 0;'
  7540. ]),
  7541. LinesToStr([ // $mod.$main
  7542. 'var $tmp1 = $mod.vI;',
  7543. 'if ($tmp1 === 1) {',
  7544. ' $mod.vI = 3}',
  7545. ' else if ($tmp1 === Bird.e) ;'
  7546. ]));
  7547. end;
  7548. procedure TTestModule.TestDebugger;
  7549. begin
  7550. StartProgram(false);
  7551. Add([
  7552. 'procedure DoIt;',
  7553. 'begin',
  7554. ' deBugger;',
  7555. ' DeBugger();',
  7556. 'end;',
  7557. 'begin',
  7558. ' Debugger;']);
  7559. ConvertProgram;
  7560. CheckSource('TestDebugger',
  7561. LinesToStr([ // statements
  7562. 'this.DoIt = function () {',
  7563. ' debugger;',
  7564. ' debugger;',
  7565. '};',
  7566. '']),
  7567. LinesToStr([ // $mod.$main
  7568. 'debugger;',
  7569. '']));
  7570. end;
  7571. procedure TTestModule.TestArray_Dynamic;
  7572. begin
  7573. StartProgram(false);
  7574. Add(['type',
  7575. ' TArrayInt = array of longint;',
  7576. 'var',
  7577. ' Arr: TArrayInt;',
  7578. ' i: longint;',
  7579. ' b: boolean;',
  7580. 'begin',
  7581. ' SetLength(arr,3);',
  7582. ' arr[0]:=4;',
  7583. ' arr[1]:=length(arr)+arr[0];',
  7584. ' arr[i]:=5;',
  7585. ' arr[arr[i]]:=arr[6];',
  7586. ' i:=low(arr);',
  7587. ' i:=high(arr);',
  7588. ' b:=Assigned(arr);',
  7589. ' Arr:=default(TArrayInt);']);
  7590. ConvertProgram;
  7591. CheckSource('TestArray_Dynamic',
  7592. LinesToStr([ // statements
  7593. 'this.Arr = [];',
  7594. 'this.i = 0;',
  7595. 'this.b = false;'
  7596. ]),
  7597. LinesToStr([ // $mod.$main
  7598. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  7599. '$mod.Arr[0] = 4;',
  7600. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  7601. '$mod.Arr[$mod.i] = 5;',
  7602. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  7603. '$mod.i = 0;',
  7604. '$mod.i = rtl.length($mod.Arr) - 1;',
  7605. '$mod.b = rtl.length($mod.Arr) > 0;',
  7606. '$mod.Arr = [];',
  7607. '']));
  7608. end;
  7609. procedure TTestModule.TestArray_Dynamic_Nil;
  7610. begin
  7611. StartProgram(false);
  7612. Add('type');
  7613. Add(' TArrayInt = array of longint;');
  7614. Add('var');
  7615. Add(' Arr: TArrayInt;');
  7616. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  7617. Add('begin');
  7618. Add(' arr:=nil;');
  7619. Add(' if arr=nil then;');
  7620. Add(' if nil=arr then;');
  7621. Add(' if arr<>nil then;');
  7622. Add(' if nil<>arr then;');
  7623. Add(' DoIt(nil,nil);');
  7624. ConvertProgram;
  7625. CheckSource('TestArray_Dynamic',
  7626. LinesToStr([ // statements
  7627. 'this.Arr = [];',
  7628. 'this.DoIt = function(i,j){',
  7629. '};'
  7630. ]),
  7631. LinesToStr([ // $mod.$main
  7632. '$mod.Arr = [];',
  7633. 'if (rtl.length($mod.Arr) === 0) ;',
  7634. 'if (rtl.length($mod.Arr) === 0) ;',
  7635. 'if (rtl.length($mod.Arr) > 0) ;',
  7636. 'if (rtl.length($mod.Arr) > 0) ;',
  7637. '$mod.DoIt([],[]);',
  7638. '']));
  7639. end;
  7640. procedure TTestModule.TestArray_DynMultiDimensional;
  7641. begin
  7642. StartProgram(false);
  7643. Add('type');
  7644. Add(' TArrayInt = array of longint;');
  7645. Add(' TArrayArrayInt = array of TArrayInt;');
  7646. Add('var');
  7647. Add(' Arr: TArrayInt;');
  7648. Add(' Arr2: TArrayArrayInt;');
  7649. Add(' i: longint;');
  7650. Add('begin');
  7651. Add(' arr2:=nil;');
  7652. Add(' if arr2=nil then;');
  7653. Add(' if nil=arr2 then;');
  7654. Add(' i:=low(arr2);');
  7655. Add(' i:=low(arr2[1]);');
  7656. Add(' i:=high(arr2);');
  7657. Add(' i:=high(arr2[2]);');
  7658. Add(' arr2[3]:=arr;');
  7659. Add(' arr2[4][5]:=i;');
  7660. Add(' i:=arr2[6][7];');
  7661. Add(' arr2[8,9]:=i;');
  7662. Add(' i:=arr2[10,11];');
  7663. Add(' SetLength(arr2,14);');
  7664. Add(' SetLength(arr2[15],16);');
  7665. ConvertProgram;
  7666. CheckSource('TestArray_Dynamic',
  7667. LinesToStr([ // statements
  7668. 'this.Arr = [];',
  7669. 'this.Arr2 = [];',
  7670. 'this.i = 0;'
  7671. ]),
  7672. LinesToStr([ // $mod.$main
  7673. '$mod.Arr2 = [];',
  7674. 'if (rtl.length($mod.Arr2) === 0) ;',
  7675. 'if (rtl.length($mod.Arr2) === 0) ;',
  7676. '$mod.i = 0;',
  7677. '$mod.i = 0;',
  7678. '$mod.i = rtl.length($mod.Arr2) - 1;',
  7679. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  7680. '$mod.Arr2[3] = $mod.Arr;',
  7681. '$mod.Arr2[4][5] = $mod.i;',
  7682. '$mod.i = $mod.Arr2[6][7];',
  7683. '$mod.Arr2[8][9] = $mod.i;',
  7684. '$mod.i = $mod.Arr2[10][11];',
  7685. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  7686. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  7687. '']));
  7688. end;
  7689. procedure TTestModule.TestArray_StaticInt;
  7690. begin
  7691. StartProgram(false);
  7692. Add('type');
  7693. Add(' TArrayInt = array[2..4] of longint;');
  7694. Add('var');
  7695. Add(' Arr: TArrayInt;');
  7696. Add(' Arr2: TArrayInt = (5,6,7);');
  7697. Add(' i: longint;');
  7698. Add(' b: boolean;');
  7699. Add('begin');
  7700. Add(' arr[2]:=4;');
  7701. Add(' arr[3]:=arr[2]+arr[3];');
  7702. Add(' arr[i]:=5;');
  7703. Add(' arr[arr[i]]:=arr[high(arr)];');
  7704. Add(' i:=low(arr);');
  7705. Add(' i:=high(arr);');
  7706. Add(' b:=arr[2]=arr[3];');
  7707. Add(' arr:=default(TArrayInt);');
  7708. ConvertProgram;
  7709. CheckSource('TestArray_StaticInt',
  7710. LinesToStr([ // statements
  7711. 'this.Arr = rtl.arraySetLength(null,0,3);',
  7712. 'this.Arr2 = [5, 6, 7];',
  7713. 'this.i = 0;',
  7714. 'this.b = false;'
  7715. ]),
  7716. LinesToStr([ // $mod.$main
  7717. '$mod.Arr[0] = 4;',
  7718. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  7719. '$mod.Arr[$mod.i-2] = 5;',
  7720. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  7721. '$mod.i = 2;',
  7722. '$mod.i = 4;',
  7723. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  7724. '$mod.Arr = rtl.arraySetLength(null,0,3).slice(0);',
  7725. '']));
  7726. end;
  7727. procedure TTestModule.TestArray_StaticBool;
  7728. begin
  7729. StartProgram(false);
  7730. Add('type');
  7731. Add(' TBools = array[boolean] of boolean;');
  7732. Add(' TBool2 = array[true..true] of boolean;');
  7733. Add('var');
  7734. Add(' Arr: TBools;');
  7735. Add(' Arr2: TBool2;');
  7736. Add(' Arr3: TBools = (true,false);');
  7737. Add(' b: boolean;');
  7738. Add('begin');
  7739. Add(' b:=low(arr);');
  7740. Add(' b:=high(arr);');
  7741. Add(' arr[true]:=false;');
  7742. Add(' arr[false]:=arr[b] or arr[true];');
  7743. Add(' arr[b]:=true;');
  7744. Add(' arr[arr[b]]:=arr[high(arr)];');
  7745. Add(' b:=arr[false]=arr[true];');
  7746. Add(' b:=low(arr2);');
  7747. Add(' b:=high(arr2);');
  7748. Add(' arr2[true]:=true;');
  7749. Add(' arr2[true]:=arr2[true] and arr2[b];');
  7750. Add(' arr2[b]:=false;');
  7751. ConvertProgram;
  7752. CheckSource('TestArray_StaticBool',
  7753. LinesToStr([ // statements
  7754. 'this.Arr = rtl.arraySetLength(null,false,2);',
  7755. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  7756. 'this.Arr3 = [true, false];',
  7757. 'this.b = false;'
  7758. ]),
  7759. LinesToStr([ // $mod.$main
  7760. '$mod.b = false;',
  7761. '$mod.b = true;',
  7762. '$mod.Arr[1] = false;',
  7763. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  7764. '$mod.Arr[+$mod.b] = true;',
  7765. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  7766. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  7767. '$mod.b = true;',
  7768. '$mod.b = true;',
  7769. '$mod.Arr2[0] = true;',
  7770. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  7771. '$mod.Arr2[1-$mod.b] = false;',
  7772. '']));
  7773. end;
  7774. procedure TTestModule.TestArray_StaticChar;
  7775. begin
  7776. StartProgram(false);
  7777. Add([
  7778. 'type',
  7779. ' TChars = array[char] of char;',
  7780. ' TChars2 = array[''a''..''z''] of char;',
  7781. 'var',
  7782. ' Arr: TChars;',
  7783. ' Arr2: TChars2;',
  7784. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  7785. ' Arr4: array[11..13] of char = ''pas'';',
  7786. ' Arr5: array[21..22] of char = ''äö'';',
  7787. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  7788. ' c: char;',
  7789. ' b: boolean;',
  7790. 'begin',
  7791. ' c:=low(arr);',
  7792. ' c:=high(arr);',
  7793. ' arr[''B'']:=''a'';',
  7794. ' arr[''D'']:=arr[c];',
  7795. ' arr[c]:=arr[''d''];',
  7796. ' arr[arr[c]]:=arr[high(arr)];',
  7797. ' b:=arr[low(arr)]=arr[''e''];',
  7798. ' c:=low(arr2);',
  7799. ' c:=high(arr2);',
  7800. ' arr2[''b'']:=''f'';',
  7801. ' arr2[''a'']:=arr2[c];',
  7802. ' arr2[c]:=arr2[''g''];']);
  7803. ConvertProgram;
  7804. CheckSource('TestArray_StaticChar',
  7805. LinesToStr([ // statements
  7806. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  7807. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  7808. 'this.Arr3 = ["p", "a", "s"];',
  7809. 'this.Arr4 = ["p", "a", "s"];',
  7810. 'this.Arr5 = ["ä", "ö"];',
  7811. 'this.Arr6 = ["ä", "ö"];',
  7812. 'this.c = "";',
  7813. 'this.b = false;',
  7814. '']),
  7815. LinesToStr([ // $mod.$main
  7816. '$mod.c = "\x00";',
  7817. '$mod.c = "\uFFFF";',
  7818. '$mod.Arr[66] = "a";',
  7819. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  7820. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  7821. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  7822. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  7823. '$mod.c = "a";',
  7824. '$mod.c = "z";',
  7825. '$mod.Arr2[1] = "f";',
  7826. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  7827. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  7828. '']));
  7829. end;
  7830. procedure TTestModule.TestArray_StaticMultiDim;
  7831. begin
  7832. StartProgram(false);
  7833. Add([
  7834. 'type',
  7835. ' TArrayInt = array[1..3] of longint;',
  7836. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  7837. 'var',
  7838. ' Arr: TArrayInt;',
  7839. ' Arr2: TArrayArrayInt;',
  7840. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  7841. ' i: longint;',
  7842. 'begin',
  7843. ' i:=low(arr);',
  7844. ' i:=low(arr2);',
  7845. ' i:=low(arr2[5]);',
  7846. ' i:=high(arr);',
  7847. ' i:=high(arr2);',
  7848. ' i:=high(arr2[6]);',
  7849. ' arr2[5]:=arr;',
  7850. ' arr2[6][2]:=i;',
  7851. ' i:=arr2[6][3];',
  7852. ' arr2[6,3]:=i;',
  7853. ' i:=arr2[5,2];',
  7854. ' arr2:=arr2;',// clone multi dim static array
  7855. //' arr3:=arr3;',// clone anonymous multi dim static array
  7856. '']);
  7857. ConvertProgram;
  7858. CheckSource('TestArray_StaticMultiDim',
  7859. LinesToStr([ // statements
  7860. 'this.TArrayArrayInt$clone = function (a) {',
  7861. ' var r = [];',
  7862. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  7863. ' return r;',
  7864. '};',
  7865. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  7866. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  7867. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  7868. 'this.i = 0;'
  7869. ]),
  7870. LinesToStr([ // $mod.$main
  7871. '$mod.i = 1;',
  7872. '$mod.i = 5;',
  7873. '$mod.i = 1;',
  7874. '$mod.i = 3;',
  7875. '$mod.i = 6;',
  7876. '$mod.i = 3;',
  7877. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  7878. '$mod.Arr2[1][1] = $mod.i;',
  7879. '$mod.i = $mod.Arr2[1][2];',
  7880. '$mod.Arr2[1][2] = $mod.i;',
  7881. '$mod.i = $mod.Arr2[0][1];',
  7882. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  7883. '']));
  7884. end;
  7885. procedure TTestModule.TestArray_StaticInFunction;
  7886. begin
  7887. StartProgram(false);
  7888. Add([
  7889. 'const TArrayInt = 3;',
  7890. 'const TArrayArrayInt = 4;',
  7891. 'procedure DoIt;',
  7892. 'type',
  7893. ' TArrayInt = array[1..3] of longint;',
  7894. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  7895. 'var',
  7896. ' Arr: TArrayInt;',
  7897. ' Arr2: TArrayArrayInt;',
  7898. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  7899. ' i: longint;',
  7900. 'begin',
  7901. ' arr2[5]:=arr;',
  7902. ' arr2:=arr2;',// clone multi dim static array
  7903. 'end;',
  7904. 'begin',
  7905. '']);
  7906. ConvertProgram;
  7907. CheckSource('TestArray_StaticInFunction',
  7908. LinesToStr([ // statements
  7909. 'this.TArrayInt = 3;',
  7910. 'this.TArrayArrayInt = 4;',
  7911. 'var TArrayArrayInt$1$clone = function (a) {',
  7912. ' var r = [];',
  7913. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  7914. ' return r;',
  7915. '};',
  7916. 'this.DoIt = function () {',
  7917. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  7918. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  7919. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  7920. ' var i = 0;',
  7921. ' Arr2[0] = Arr.slice(0);',
  7922. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  7923. '};',
  7924. '']),
  7925. LinesToStr([ // $mod.$main
  7926. '']));
  7927. end;
  7928. procedure TTestModule.TestArrayOfRecord;
  7929. begin
  7930. StartProgram(false);
  7931. Add([
  7932. 'type',
  7933. ' TRec = record',
  7934. ' Int: longint;',
  7935. ' end;',
  7936. ' TArrayRec = array of TRec;',
  7937. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  7938. 'begin',
  7939. 'end;',
  7940. 'var',
  7941. ' Arr: TArrayRec;',
  7942. ' r: TRec;',
  7943. ' i: longint;',
  7944. 'begin',
  7945. ' SetLength(arr,3);',
  7946. ' arr[0].int:=4;',
  7947. ' arr[1].int:=length(arr)+arr[2].int;',
  7948. ' arr[arr[i].int].int:=arr[5].int;',
  7949. ' arr[7]:=r;',
  7950. ' r:=arr[8];',
  7951. ' i:=low(arr);',
  7952. ' i:=high(arr);',
  7953. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  7954. ConvertProgram;
  7955. CheckSource('TestArrayOfRecord',
  7956. LinesToStr([ // statements
  7957. 'rtl.recNewT($mod, "TRec", function () {',
  7958. ' this.Int = 0;',
  7959. ' this.$eq = function (b) {',
  7960. ' return this.Int === b.Int;',
  7961. ' };',
  7962. ' this.$assign = function (s) {',
  7963. ' this.Int = s.Int;',
  7964. ' return this;',
  7965. ' };',
  7966. '});',
  7967. 'this.DoIt = function (vd, vc, vv) {',
  7968. '};',
  7969. 'this.Arr = [];',
  7970. 'this.r = $mod.TRec.$new();',
  7971. 'this.i = 0;'
  7972. ]),
  7973. LinesToStr([ // $mod.$main
  7974. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  7975. '$mod.Arr[0].Int = 4;',
  7976. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  7977. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  7978. '$mod.Arr[7].$assign($mod.r);',
  7979. '$mod.r.$assign($mod.Arr[8]);',
  7980. '$mod.i = 0;',
  7981. '$mod.i = rtl.length($mod.Arr)-1;',
  7982. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  7983. '']));
  7984. end;
  7985. procedure TTestModule.TestArray_StaticRecord;
  7986. begin
  7987. StartProgram(false);
  7988. Add([
  7989. 'type',
  7990. ' TRec = record',
  7991. ' Int: longint;',
  7992. ' end;',
  7993. ' TArrayRec = array[1..2] of TRec;',
  7994. 'var',
  7995. ' Arr: TArrayRec;',
  7996. 'begin',
  7997. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  7998. '']);
  7999. ConvertProgram;
  8000. CheckSource('TestArray_StaticRecord',
  8001. LinesToStr([ // statements
  8002. 'rtl.recNewT($mod, "TRec", function () {',
  8003. ' this.Int = 0;',
  8004. ' this.$eq = function (b) {',
  8005. ' return this.Int === b.Int;',
  8006. ' };',
  8007. ' this.$assign = function (s) {',
  8008. ' this.Int = s.Int;',
  8009. ' return this;',
  8010. ' };',
  8011. '});',
  8012. 'this.TArrayRec$clone = function (a) {',
  8013. ' var r = [];',
  8014. ' for (var i = 0; i < 2; i++) r.push($mod.TRec.$clone(a[i]));',
  8015. ' return r;',
  8016. '};',
  8017. 'this.Arr = rtl.arraySetLength(null, $mod.TRec, 2);',
  8018. '']),
  8019. LinesToStr([ // $mod.$main
  8020. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  8021. end;
  8022. procedure TTestModule.TestArrayOfSet;
  8023. begin
  8024. StartProgram(false);
  8025. Add([
  8026. 'type',
  8027. ' TFlag = (big,small);',
  8028. ' TSetOfFlag = set of tflag;',
  8029. ' TArrayFlag = array of TSetOfFlag;',
  8030. 'procedure DoIt(const a: Tarrayflag);',
  8031. 'begin',
  8032. 'end;',
  8033. 'var',
  8034. ' f: TFlag;',
  8035. ' s: TSetOfFlag;',
  8036. ' Arr: TArrayFlag;',
  8037. ' i: longint;',
  8038. 'begin',
  8039. ' SetLength(arr,3);',
  8040. ' arr[0]:=s;',
  8041. ' arr[1]:=[big];',
  8042. ' arr[2]:=[big]+s;',
  8043. ' arr[3]:=s+[big];',
  8044. ' arr[4]:=arr[5];',
  8045. ' s:=arr[6];',
  8046. ' i:=low(arr);',
  8047. ' i:=high(arr);',
  8048. ' DoIt(arr);',
  8049. ' DoIt([s]);',
  8050. ' DoIt([[],s]);',
  8051. ' DoIt([s,[]]);',
  8052. '']);
  8053. ConvertProgram;
  8054. CheckSource('TestArrayOfSet',
  8055. LinesToStr([ // statements
  8056. 'this.TFlag = {',
  8057. ' "0": "big",',
  8058. ' big: 0,',
  8059. ' "1": "small",',
  8060. ' small: 1',
  8061. '};',
  8062. 'this.DoIt = function (a) {',
  8063. '};',
  8064. 'this.f = 0;',
  8065. 'this.s = {};',
  8066. 'this.Arr = [];',
  8067. 'this.i = 0;',
  8068. '']),
  8069. LinesToStr([ // $mod.$main
  8070. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  8071. '$mod.Arr[0] = rtl.refSet($mod.s);',
  8072. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  8073. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  8074. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  8075. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  8076. '$mod.s = rtl.refSet($mod.Arr[6]);',
  8077. '$mod.i = 0;',
  8078. '$mod.i = rtl.length($mod.Arr) - 1;',
  8079. '$mod.DoIt($mod.Arr);',
  8080. '$mod.DoIt([rtl.refSet($mod.s)]);',
  8081. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  8082. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  8083. '']));
  8084. end;
  8085. procedure TTestModule.TestArray_DynAsParam;
  8086. begin
  8087. StartProgram(false);
  8088. Add([
  8089. 'type integer = longint;',
  8090. 'type TArrInt = array of integer;',
  8091. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  8092. 'var vJ: TArrInt;',
  8093. 'begin',
  8094. ' vg:=vg;',
  8095. ' vj:=vh;',
  8096. ' vi:=vi;',
  8097. ' doit(vg,vg,vg);',
  8098. ' doit(vh,vh,vj);',
  8099. ' doit(vi,vi,vi);',
  8100. ' doit(vj,vj,vj);',
  8101. 'end;',
  8102. 'var i: TArrInt;',
  8103. 'begin',
  8104. ' doit(i,i,i);']);
  8105. ConvertProgram;
  8106. CheckSource('TestArray_DynAsParams',
  8107. LinesToStr([ // statements
  8108. 'this.DoIt = function (vG,vH,vI) {',
  8109. ' var vJ = [];',
  8110. ' vG = vG;',
  8111. ' vJ = vH;',
  8112. ' vI.set(vI.get());',
  8113. ' $mod.DoIt(vG, vG, {',
  8114. ' get: function () {',
  8115. ' return vG;',
  8116. ' },',
  8117. ' set: function (v) {',
  8118. ' vG = v;',
  8119. ' }',
  8120. ' });',
  8121. ' $mod.DoIt(vH, vH, {',
  8122. ' get: function () {',
  8123. ' return vJ;',
  8124. ' },',
  8125. ' set: function (v) {',
  8126. ' vJ = v;',
  8127. ' }',
  8128. ' });',
  8129. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  8130. ' $mod.DoIt(vJ, vJ, {',
  8131. ' get: function () {',
  8132. ' return vJ;',
  8133. ' },',
  8134. ' set: function (v) {',
  8135. ' vJ = v;',
  8136. ' }',
  8137. ' });',
  8138. '};',
  8139. 'this.i = [];'
  8140. ]),
  8141. LinesToStr([
  8142. '$mod.DoIt($mod.i,$mod.i,{',
  8143. ' p: $mod,',
  8144. ' get: function () {',
  8145. ' return this.p.i;',
  8146. ' },',
  8147. ' set: function (v) {',
  8148. ' this.p.i = v;',
  8149. ' }',
  8150. '});'
  8151. ]));
  8152. end;
  8153. procedure TTestModule.TestArray_StaticAsParam;
  8154. begin
  8155. StartProgram(false);
  8156. Add([
  8157. 'type integer = longint;',
  8158. 'type TArrInt = array[1..2] of integer;',
  8159. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  8160. 'var vJ: TArrInt;',
  8161. 'begin',
  8162. ' vg:=vg;',
  8163. ' vj:=vh;',
  8164. ' vi:=vi;',
  8165. ' doit(vg,vg,vg);',
  8166. ' doit(vh,vh,vj);',
  8167. ' doit(vi,vi,vi);',
  8168. ' doit(vj,vj,vj);',
  8169. 'end;',
  8170. 'var i: TArrInt;',
  8171. 'begin',
  8172. ' doit(i,i,i);']);
  8173. ConvertProgram;
  8174. CheckSource('TestArray_StaticAsParams',
  8175. LinesToStr([ // statements
  8176. 'this.DoIt = function (vG,vH,vI) {',
  8177. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  8178. ' vG = vG.slice(0);',
  8179. ' vJ = vH.slice(0);',
  8180. ' vI.set(vI.get().slice(0));',
  8181. ' $mod.DoIt(vG.slice(0), vG, {',
  8182. ' get: function () {',
  8183. ' return vG;',
  8184. ' },',
  8185. ' set: function (v) {',
  8186. ' vG = v;',
  8187. ' }',
  8188. ' });',
  8189. ' $mod.DoIt(vH.slice(0), vH, {',
  8190. ' get: function () {',
  8191. ' return vJ;',
  8192. ' },',
  8193. ' set: function (v) {',
  8194. ' vJ = v;',
  8195. ' }',
  8196. ' });',
  8197. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  8198. ' $mod.DoIt(vJ.slice(0), vJ, {',
  8199. ' get: function () {',
  8200. ' return vJ;',
  8201. ' },',
  8202. ' set: function (v) {',
  8203. ' vJ = v;',
  8204. ' }',
  8205. ' });',
  8206. '};',
  8207. 'this.i = rtl.arraySetLength(null, 0, 2);'
  8208. ]),
  8209. LinesToStr([
  8210. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  8211. ' p: $mod,',
  8212. ' get: function () {',
  8213. ' return this.p.i;',
  8214. ' },',
  8215. ' set: function (v) {',
  8216. ' this.p.i = v;',
  8217. ' }',
  8218. '});'
  8219. ]));
  8220. end;
  8221. procedure TTestModule.TestArrayElement_AsParams;
  8222. begin
  8223. StartProgram(false);
  8224. Add('type integer = longint;');
  8225. Add('type TArrayInt = array of integer;');
  8226. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  8227. Add('var vJ: tarrayint;');
  8228. Add('begin');
  8229. Add(' vi:=vi;');
  8230. Add(' doit(vi,vi,vi);');
  8231. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  8232. Add('end;');
  8233. Add('var a: TArrayInt;');
  8234. Add('begin');
  8235. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  8236. ConvertProgram;
  8237. CheckSource('TestArrayElement_AsParams',
  8238. LinesToStr([ // statements
  8239. 'this.DoIt = function (vG,vH,vI) {',
  8240. ' var vJ = [];',
  8241. ' vI.set(vI.get());',
  8242. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  8243. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  8244. ' a:1+3,',
  8245. ' p:vJ,',
  8246. ' get: function () {',
  8247. ' return this.p[this.a];',
  8248. ' },',
  8249. ' set: function (v) {',
  8250. ' this.p[this.a] = v;',
  8251. ' }',
  8252. ' });',
  8253. '};',
  8254. 'this.a = [];'
  8255. ]),
  8256. LinesToStr([
  8257. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  8258. ' a: 1+6,',
  8259. ' p: $mod.a,',
  8260. ' get: function () {',
  8261. ' return this.p[this.a];',
  8262. ' },',
  8263. ' set: function (v) {',
  8264. ' this.p[this.a] = v;',
  8265. ' }',
  8266. '});'
  8267. ]));
  8268. end;
  8269. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  8270. begin
  8271. StartProgram(false);
  8272. Add('type Integer = longint;');
  8273. Add('type TArrayInt = array of integer;');
  8274. Add('function GetArr(vB: integer = 0): tarrayint;');
  8275. Add('begin');
  8276. Add('end;');
  8277. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  8278. Add('begin');
  8279. Add('end;');
  8280. Add('begin');
  8281. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  8282. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  8283. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  8284. ConvertProgram;
  8285. CheckSource('TestArrayElementFromFuncResult_AsParams',
  8286. LinesToStr([ // statements
  8287. 'this.GetArr = function (vB) {',
  8288. ' var Result = [];',
  8289. ' return Result;',
  8290. '};',
  8291. 'this.DoIt = function (vG,vH,vI) {',
  8292. '};'
  8293. ]),
  8294. LinesToStr([
  8295. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  8296. ' a: 1+3,',
  8297. ' p: $mod.GetArr(0),',
  8298. ' get: function () {',
  8299. ' return this.p[this.a];',
  8300. ' },',
  8301. ' set: function (v) {',
  8302. ' this.p[this.a] = v;',
  8303. ' }',
  8304. '});',
  8305. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  8306. ' a: 2+3,',
  8307. ' p: $mod.GetArr(0),',
  8308. ' get: function () {',
  8309. ' return this.p[this.a];',
  8310. ' },',
  8311. ' set: function (v) {',
  8312. ' this.p[this.a] = v;',
  8313. ' }',
  8314. '});',
  8315. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  8316. ' a: 3+3,',
  8317. ' p: $mod.GetArr(9),',
  8318. ' get: function () {',
  8319. ' return this.p[this.a];',
  8320. ' },',
  8321. ' set: function (v) {',
  8322. ' this.p[this.a] = v;',
  8323. ' }',
  8324. '});',
  8325. '']));
  8326. end;
  8327. procedure TTestModule.TestArrayEnumTypeRange;
  8328. begin
  8329. StartProgram(false);
  8330. Add([
  8331. 'type',
  8332. ' TEnum = (red,blue);',
  8333. ' TEnumArray = array[TEnum] of longint;',
  8334. 'var',
  8335. ' e: TEnum;',
  8336. ' i: longint;',
  8337. ' a: TEnumArray;',
  8338. ' numbers: TEnumArray = (1,2);',
  8339. ' names: array[TEnum] of string = (''red'',''blue'');',
  8340. 'begin',
  8341. ' e:=low(a);',
  8342. ' e:=high(a);',
  8343. ' i:=a[red];',
  8344. ' a[e]:=a[e];']);
  8345. ConvertProgram;
  8346. CheckSource('TestArrayEnumTypeRange',
  8347. LinesToStr([ // statements
  8348. ' this.TEnum = {',
  8349. ' "0": "red",',
  8350. ' red: 0,',
  8351. ' "1": "blue",',
  8352. ' blue: 1',
  8353. '};',
  8354. 'this.e = 0;',
  8355. 'this.i = 0;',
  8356. 'this.a = rtl.arraySetLength(null,0,2);',
  8357. 'this.numbers = [1, 2];',
  8358. 'this.names = ["red", "blue"];',
  8359. '']),
  8360. LinesToStr([ // $mod.$main
  8361. '$mod.e = $mod.TEnum.red;',
  8362. '$mod.e = $mod.TEnum.blue;',
  8363. '$mod.i = $mod.a[$mod.TEnum.red];',
  8364. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  8365. '']));
  8366. end;
  8367. procedure TTestModule.TestArray_SetLengthOutArg;
  8368. begin
  8369. StartProgram(false);
  8370. Add([
  8371. 'type TArrInt = array of longint;',
  8372. 'procedure DoIt(out a: TArrInt);',
  8373. 'begin',
  8374. ' SetLength(a,2);',
  8375. 'end;',
  8376. 'begin',
  8377. '']);
  8378. ConvertProgram;
  8379. CheckSource('TestArray_SetLengthOutArg',
  8380. LinesToStr([ // statements
  8381. 'this.DoIt = function (a) {',
  8382. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  8383. '};',
  8384. '']),
  8385. LinesToStr([
  8386. '']));
  8387. end;
  8388. procedure TTestModule.TestArray_SetLengthProperty;
  8389. begin
  8390. StartProgram(false);
  8391. Add('type');
  8392. Add(' TArrInt = array of longint;');
  8393. Add(' TObject = class');
  8394. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  8395. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  8396. Add(' property Colors: TArrInt read GetColors write SetColors;');
  8397. Add(' end;');
  8398. Add('var Obj: TObject;');
  8399. Add('begin');
  8400. Add(' SetLength(Obj.Colors,2);');
  8401. ConvertProgram;
  8402. CheckSource('TestArray_SetLengthProperty',
  8403. LinesToStr([ // statements
  8404. 'rtl.createClass($mod, "TObject", null, function () {',
  8405. ' this.$init = function () {',
  8406. ' };',
  8407. ' this.$final = function () {',
  8408. ' };',
  8409. '});',
  8410. 'this.Obj = null;',
  8411. '']),
  8412. LinesToStr([
  8413. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  8414. '']));
  8415. end;
  8416. procedure TTestModule.TestArray_SetLengthMultiDim;
  8417. begin
  8418. StartProgram(false);
  8419. Add([
  8420. 'type',
  8421. ' TArrArrInt = array of array of longint;',
  8422. 'var',
  8423. ' a: TArrArrInt;',
  8424. 'begin',
  8425. ' SetLength(a,2);',
  8426. ' SetLength(a,3,4);',
  8427. '']);
  8428. ConvertProgram;
  8429. CheckSource('TestArray_SetLengthMultiDim',
  8430. LinesToStr([ // statements
  8431. 'this.a = [];']),
  8432. LinesToStr([
  8433. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  8434. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  8435. '']));
  8436. end;
  8437. procedure TTestModule.TestArray_OpenArrayOfString;
  8438. begin
  8439. StartProgram(false);
  8440. Add('procedure DoIt(const a: array of String);');
  8441. Add('var');
  8442. Add(' i: longint;');
  8443. Add(' s: string;');
  8444. Add('begin');
  8445. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  8446. Add('end;');
  8447. Add('var s: string;');
  8448. Add('begin');
  8449. Add(' DoIt([]);');
  8450. Add(' DoIt([s,''foo'','''',s+s]);');
  8451. ConvertProgram;
  8452. CheckSource('TestArray_OpenArrayOfString',
  8453. LinesToStr([ // statements
  8454. 'this.DoIt = function (a) {',
  8455. ' var i = 0;',
  8456. ' var s = "";',
  8457. ' for (var $l1 = 0, $end2 = rtl.length(a) - 1; $l1 <= $end2; $l1++) {',
  8458. ' i = $l1;',
  8459. ' s = a[rtl.length(a) - i - 1];',
  8460. ' };',
  8461. '};',
  8462. 'this.s = "";',
  8463. '']),
  8464. LinesToStr([
  8465. '$mod.DoIt([]);',
  8466. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  8467. '']));
  8468. end;
  8469. procedure TTestModule.TestArray_Concat;
  8470. begin
  8471. StartProgram(false);
  8472. Add([
  8473. 'type',
  8474. ' integer = longint;',
  8475. ' TFlag = (big,small);',
  8476. ' TFlags = set of TFlag;',
  8477. ' TRec = record',
  8478. ' i: integer;',
  8479. ' end;',
  8480. ' TArrInt = array of integer;',
  8481. ' TArrRec = array of TRec;',
  8482. ' TArrFlag = array of TFlag;',
  8483. ' TArrSet = array of TFlags;',
  8484. ' TArrJSValue = array of jsvalue;',
  8485. 'var',
  8486. ' ArrInt: tarrint;',
  8487. ' ArrRec: tarrrec;',
  8488. ' ArrFlag: tarrflag;',
  8489. ' ArrSet: tarrset;',
  8490. ' ArrJSValue: tarrjsvalue;',
  8491. 'begin',
  8492. ' arrint:=concat(arrint);',
  8493. ' arrint:=concat(arrint,arrint);',
  8494. ' arrint:=concat(arrint,arrint,arrint);',
  8495. ' arrrec:=concat(arrrec);',
  8496. ' arrrec:=concat(arrrec,arrrec);',
  8497. ' arrrec:=concat(arrrec,arrrec,arrrec);',
  8498. ' arrset:=concat(arrset);',
  8499. ' arrset:=concat(arrset,arrset);',
  8500. ' arrset:=concat(arrset,arrset,arrset);',
  8501. ' arrjsvalue:=concat(arrjsvalue);',
  8502. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue);',
  8503. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);',
  8504. ' arrint:=concat([1],arrint);',
  8505. ' arrflag:=concat([big]);',
  8506. ' arrflag:=concat([big],arrflag);',
  8507. ' arrflag:=concat(arrflag,[small]);',
  8508. '']);
  8509. ConvertProgram;
  8510. CheckSource('TestArray_Concat',
  8511. LinesToStr([ // statements
  8512. 'this.TFlag = {',
  8513. ' "0": "big",',
  8514. ' big: 0,',
  8515. ' "1": "small",',
  8516. ' small: 1',
  8517. '};',
  8518. 'rtl.recNewT($mod, "TRec", function () {',
  8519. ' this.i = 0;',
  8520. ' this.$eq = function (b) {',
  8521. ' return this.i === b.i;',
  8522. ' };',
  8523. ' this.$assign = function (s) {',
  8524. ' this.i = s.i;',
  8525. ' return this;',
  8526. ' };',
  8527. '});',
  8528. 'this.ArrInt = [];',
  8529. 'this.ArrRec = [];',
  8530. 'this.ArrFlag = [];',
  8531. 'this.ArrSet = [];',
  8532. 'this.ArrJSValue = [];',
  8533. '']),
  8534. LinesToStr([ // $mod.$main
  8535. '$mod.ArrInt = $mod.ArrInt;',
  8536. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt);',
  8537. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt, $mod.ArrInt);',
  8538. '$mod.ArrRec = $mod.ArrRec;',
  8539. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  8540. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec, $mod.ArrRec);',
  8541. '$mod.ArrSet = $mod.ArrSet;',
  8542. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  8543. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet, $mod.ArrSet);',
  8544. '$mod.ArrJSValue = $mod.ArrJSValue;',
  8545. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue);',
  8546. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue, $mod.ArrJSValue);',
  8547. '$mod.ArrInt = rtl.arrayConcatN([1], $mod.ArrInt);',
  8548. '$mod.ArrFlag = [$mod.TFlag.big];',
  8549. '$mod.ArrFlag = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag);',
  8550. '$mod.ArrFlag = rtl.arrayConcatN($mod.ArrFlag, [$mod.TFlag.small]);',
  8551. '']));
  8552. end;
  8553. procedure TTestModule.TestArray_Copy;
  8554. begin
  8555. StartProgram(false);
  8556. Add([
  8557. 'type',
  8558. ' integer = longint;',
  8559. ' TFlag = (big,small);',
  8560. ' TFlags = set of TFlag;',
  8561. ' TRec = record',
  8562. ' i: integer;',
  8563. ' end;',
  8564. ' TArrInt = array of integer;',
  8565. ' TArrRec = array of TRec;',
  8566. ' TArrSet = array of TFlags;',
  8567. ' TArrJSValue = array of jsvalue;',
  8568. 'var',
  8569. ' ArrInt: tarrint;',
  8570. ' ArrRec: tarrrec;',
  8571. ' ArrSet: tarrset;',
  8572. ' ArrJSValue: tarrjsvalue;',
  8573. 'begin',
  8574. ' arrint:=copy(arrint);',
  8575. ' arrint:=copy(arrint,2);',
  8576. ' arrint:=copy(arrint,3,4);',
  8577. ' arrint:=copy([1,1],1,2);',
  8578. ' arrrec:=copy(arrrec);',
  8579. ' arrrec:=copy(arrrec,5);',
  8580. ' arrrec:=copy(arrrec,6,7);',
  8581. ' arrset:=copy(arrset);',
  8582. ' arrset:=copy(arrset,8);',
  8583. ' arrset:=copy(arrset,9,10);',
  8584. ' arrjsvalue:=copy(arrjsvalue);',
  8585. ' arrjsvalue:=copy(arrjsvalue,11);',
  8586. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  8587. ' ']);
  8588. ConvertProgram;
  8589. CheckSource('TestArray_Copy',
  8590. LinesToStr([ // statements
  8591. 'this.TFlag = {',
  8592. ' "0": "big",',
  8593. ' big: 0,',
  8594. ' "1": "small",',
  8595. ' small: 1',
  8596. '};',
  8597. 'rtl.recNewT($mod, "TRec", function () {',
  8598. ' this.i = 0;',
  8599. ' this.$eq = function (b) {',
  8600. ' return this.i === b.i;',
  8601. ' };',
  8602. ' this.$assign = function (s) {',
  8603. ' this.i = s.i;',
  8604. ' return this;',
  8605. ' };',
  8606. '});',
  8607. 'this.ArrInt = [];',
  8608. 'this.ArrRec = [];',
  8609. 'this.ArrSet = [];',
  8610. 'this.ArrJSValue = [];',
  8611. '']),
  8612. LinesToStr([ // $mod.$main
  8613. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  8614. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  8615. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  8616. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  8617. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  8618. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  8619. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  8620. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  8621. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  8622. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  8623. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  8624. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  8625. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  8626. '']));
  8627. end;
  8628. procedure TTestModule.TestArray_InsertDelete;
  8629. begin
  8630. StartProgram(false);
  8631. Add([
  8632. 'type',
  8633. ' integer = longint;',
  8634. ' TFlag = (big,small);',
  8635. ' TFlags = set of TFlag;',
  8636. ' TRec = record',
  8637. ' i: integer;',
  8638. ' end;',
  8639. ' TArrInt = array of integer;',
  8640. ' TArrRec = array of TRec;',
  8641. ' TArrSet = array of TFlags;',
  8642. ' TArrJSValue = array of jsvalue;',
  8643. ' TArrArrInt = array of TArrInt;',
  8644. 'var',
  8645. ' ArrInt: tarrint;',
  8646. ' ArrRec: tarrrec;',
  8647. ' ArrSet: tarrset;',
  8648. ' ArrJSValue: tarrjsvalue;',
  8649. ' ArrArrInt: TArrArrInt;',
  8650. 'begin',
  8651. ' Insert(1,arrint,2);',
  8652. ' Insert(arrint[3],arrint,4);',
  8653. ' Insert(arrrec[5],arrrec,6);',
  8654. ' Insert(arrset[7],arrset,7);',
  8655. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  8656. ' Insert(10,arrjsvalue,11);',
  8657. ' Insert([23],arrarrint,22);',
  8658. ' Delete(arrint,12,13);',
  8659. ' Delete(arrrec,14,15);',
  8660. ' Delete(arrset,17,18);',
  8661. ' Delete(arrjsvalue,19,10);']);
  8662. ConvertProgram;
  8663. CheckSource('TestArray_InsertDelete',
  8664. LinesToStr([ // statements
  8665. 'this.TFlag = {',
  8666. ' "0": "big",',
  8667. ' big: 0,',
  8668. ' "1": "small",',
  8669. ' small: 1',
  8670. '};',
  8671. 'rtl.recNewT($mod, "TRec", function () {',
  8672. ' this.i = 0;',
  8673. ' this.$eq = function (b) {',
  8674. ' return this.i === b.i;',
  8675. ' };',
  8676. ' this.$assign = function (s) {',
  8677. ' this.i = s.i;',
  8678. ' return this;',
  8679. ' };',
  8680. '});',
  8681. 'this.ArrInt = [];',
  8682. 'this.ArrRec = [];',
  8683. 'this.ArrSet = [];',
  8684. 'this.ArrJSValue = [];',
  8685. 'this.ArrArrInt = [];',
  8686. '']),
  8687. LinesToStr([ // $mod.$main
  8688. '$mod.ArrInt.splice(2, 0, 1);',
  8689. '$mod.ArrInt.splice(4, 0, $mod.ArrInt[3]);',
  8690. '$mod.ArrRec.splice(6, 0, $mod.ArrRec[5]);',
  8691. '$mod.ArrSet.splice(7, 0, $mod.ArrSet[7]);',
  8692. '$mod.ArrJSValue.splice(9, 0, $mod.ArrJSValue[8]);',
  8693. '$mod.ArrJSValue.splice(11, 0, 10);',
  8694. '$mod.ArrArrInt.splice(22, 0, [23]);',
  8695. '$mod.ArrInt.splice(12, 13);',
  8696. '$mod.ArrRec.splice(14, 15);',
  8697. '$mod.ArrSet.splice(17, 18);',
  8698. '$mod.ArrJSValue.splice(19, 10);',
  8699. '']));
  8700. end;
  8701. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  8702. begin
  8703. StartProgram(false);
  8704. Add([
  8705. '{$modeswitch arrayoperators}',
  8706. 'type',
  8707. ' integer = longint;',
  8708. ' TArrInt = array of integer;',
  8709. ' TArrStr = array of string;',
  8710. 'const',
  8711. ' Ints: TArrInt = (1,2,3);',
  8712. ' Aliases: TarrStr = (''foo'',''b'');',
  8713. ' OneInt: TArrInt = (7);',
  8714. ' OneStr: array of integer = (7);',
  8715. ' Chars: array of char = ''aoc'';',
  8716. ' Names: array of string = (''a'',''foo'');',
  8717. ' NameCount = low(Names)+high(Names)+length(Names);',
  8718. 'var i: integer;',
  8719. 'begin',
  8720. ' Ints:=[];',
  8721. ' Ints:=[1,1];',
  8722. ' Ints:=[1]+[2];',
  8723. ' Ints:=[2];',
  8724. ' Ints:=[]+ints;',
  8725. ' Ints:=Ints+[];',
  8726. ' Ints:=Ints+OneInt;',
  8727. ' Ints:=Ints+[1,1];',
  8728. ' Ints:=[i,i]+Ints;',
  8729. ' Ints:=[1]+[i]+[3];',
  8730. '']);
  8731. ConvertProgram;
  8732. CheckSource('TestArray_DynArrayConstObjFPC',
  8733. LinesToStr([ // statements
  8734. 'this.Ints = [1, 2, 3];',
  8735. 'this.Aliases = ["foo", "b"];',
  8736. 'this.OneInt = [7];',
  8737. 'this.OneStr = [7];',
  8738. 'this.Chars = ["a", "o", "c"];',
  8739. 'this.Names = ["a", "foo"];',
  8740. 'this.NameCount = 0 + (rtl.length($mod.Names) - 1) + rtl.length($mod.Names);',
  8741. 'this.i = 0;',
  8742. '']),
  8743. LinesToStr([ // $mod.$main
  8744. '$mod.Ints = [];',
  8745. '$mod.Ints = [1, 1];',
  8746. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  8747. '$mod.Ints = [2];',
  8748. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  8749. '$mod.Ints = rtl.arrayConcatN($mod.Ints, []);',
  8750. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  8751. '$mod.Ints = rtl.arrayConcatN($mod.Ints, [1, 1]);',
  8752. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  8753. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  8754. '']));
  8755. end;
  8756. procedure TTestModule.TestArray_DynArrayConstDelphi;
  8757. begin
  8758. StartProgram(false);
  8759. // Note: const c = [1,1]; defines a set!
  8760. Add([
  8761. '{$mode delphi}',
  8762. 'type',
  8763. ' integer = longint;',
  8764. ' TArrInt = array of integer;',
  8765. ' TArrStr = array of string;',
  8766. 'const',
  8767. ' Ints: TArrInt = [1,1,2];',
  8768. ' Aliases: TarrStr = [''foo'',''b''];',
  8769. ' OneInt: TArrInt = [7];',
  8770. ' OneStr: array of integer = [7]+[8];',
  8771. ' Chars: array of char = ''aoc'';',
  8772. ' Names: array of string = [''a'',''a''];',
  8773. ' NameCount = low(Names)+high(Names)+length(Names);',
  8774. 'begin',
  8775. '']);
  8776. ConvertProgram;
  8777. CheckSource('TestArray_DynArrayConstDelphi',
  8778. LinesToStr([ // statements
  8779. 'this.Ints = [1, 1, 2];',
  8780. 'this.Aliases = ["foo", "b"];',
  8781. 'this.OneInt = [7];',
  8782. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  8783. 'this.Chars = ["a", "o", "c"];',
  8784. 'this.Names = ["a", "a"];',
  8785. 'this.NameCount = 0 + (rtl.length($mod.Names) - 1) + rtl.length($mod.Names);',
  8786. '']),
  8787. LinesToStr([ // $mod.$main
  8788. '']));
  8789. end;
  8790. procedure TTestModule.TestArray_ArrayLitAsParam;
  8791. begin
  8792. StartProgram(false);
  8793. Add([
  8794. '{$modeswitch arrayoperators}',
  8795. 'type',
  8796. ' integer = longint;',
  8797. ' TArrInt = array of integer;',
  8798. ' TArrSet = array of (red,green,blue);',
  8799. 'procedure DoOpenInt(a: array of integer); forward;',
  8800. 'procedure DoInt(a: TArrInt);',
  8801. 'begin',
  8802. ' DoInt(a+[1]);',
  8803. ' DoInt([1]+a);',
  8804. ' DoOpenInt(a);',
  8805. ' DoOpenInt(a+[1]);',
  8806. ' DoOpenInt([1]+a);',
  8807. 'end;',
  8808. 'procedure DoOpenInt(a: array of integer);',
  8809. 'begin',
  8810. ' DoOpenInt(a+[1]);',
  8811. ' DoOpenInt([1]+a);',
  8812. ' DoInt(a);',
  8813. ' DoInt(a+[1]);',
  8814. ' DoInt([1]+a);',
  8815. 'end;',
  8816. 'procedure DoSet(a: TArrSet);',
  8817. 'begin',
  8818. ' DoSet(a+[red]);',
  8819. ' DoSet([blue]+a);',
  8820. 'end;',
  8821. 'var',
  8822. ' i: TArrInt;',
  8823. ' s: TArrSet;',
  8824. 'begin',
  8825. ' DoInt([1]);',
  8826. ' DoInt([1]+[2]);',
  8827. ' DoInt(i+[1]);',
  8828. ' DoInt([1]+i);',
  8829. ' DoOpenInt([1]);',
  8830. ' DoOpenInt([1]+[2]);',
  8831. ' DoOpenInt(i+[1]);',
  8832. ' DoOpenInt([1]+i);',
  8833. ' DoSet([red]);',
  8834. ' DoSet([blue]+[green]);',
  8835. ' DoSet(s+[blue]);',
  8836. ' DoSet([red]+s);',
  8837. '']);
  8838. ConvertProgram;
  8839. CheckSource('TestArray_ArrayLitAsParam',
  8840. LinesToStr([ // statements
  8841. 'this.TArrSet$a = {',
  8842. ' "0": "red",',
  8843. ' red: 0,',
  8844. ' "1": "green",',
  8845. ' green: 1,',
  8846. ' "2": "blue",',
  8847. ' blue: 2',
  8848. '};',
  8849. 'this.DoInt = function (a) {',
  8850. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  8851. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  8852. ' $mod.DoOpenInt(a);',
  8853. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  8854. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  8855. '};',
  8856. 'this.DoOpenInt = function (a) {',
  8857. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  8858. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  8859. ' $mod.DoInt(a);',
  8860. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  8861. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  8862. '};',
  8863. 'this.DoSet = function (a) {',
  8864. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  8865. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  8866. '};',
  8867. 'this.i = [];',
  8868. 'this.s = [];',
  8869. '']),
  8870. LinesToStr([ // $mod.$main
  8871. '$mod.DoInt([1]);',
  8872. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  8873. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  8874. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  8875. '$mod.DoOpenInt([1]);',
  8876. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  8877. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  8878. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  8879. '$mod.DoSet([$mod.TArrSet$a.red]);',
  8880. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  8881. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  8882. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  8883. '']));
  8884. end;
  8885. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  8886. begin
  8887. StartProgram(false);
  8888. Add([
  8889. '{$modeswitch arrayoperators}',
  8890. 'type',
  8891. ' integer = longint;',
  8892. ' TArrInt = array of integer;',
  8893. ' TArrArrInt = array of TArrInt;',
  8894. 'procedure DoInt(a: TArrArrInt);',
  8895. 'begin',
  8896. ' DoInt(a+[[1]]);',
  8897. ' DoInt([[1]]+a);',
  8898. ' DoInt(a);',
  8899. 'end;',
  8900. 'var',
  8901. ' i: TArrInt;',
  8902. ' a: TArrArrInt;',
  8903. 'begin',
  8904. ' a:=[[1]];',
  8905. ' a:=[i];',
  8906. ' a:=a+[i];',
  8907. ' a:=[i]+a;',
  8908. ' a:=[[1]+i];',
  8909. ' a:=[[1]+[2]];',
  8910. ' a:=[i+[2]];',
  8911. ' DoInt([[1]]);',
  8912. ' DoInt([[1]+[2],[3,4],[5]]);',
  8913. ' DoInt([i+[1]]+a);',
  8914. ' DoInt([i]+a);',
  8915. '']);
  8916. ConvertProgram;
  8917. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  8918. LinesToStr([ // statements
  8919. 'this.DoInt = function (a) {',
  8920. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  8921. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  8922. ' $mod.DoInt(a);',
  8923. '};',
  8924. 'this.i = [];',
  8925. 'this.a = [];',
  8926. '']),
  8927. LinesToStr([ // $mod.$main
  8928. '$mod.a = [[1]];',
  8929. '$mod.a = [$mod.i];',
  8930. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i]);',
  8931. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  8932. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  8933. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  8934. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  8935. '$mod.DoInt([[1]]);',
  8936. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  8937. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  8938. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  8939. '']));
  8940. end;
  8941. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  8942. begin
  8943. StartProgram(false);
  8944. Add([
  8945. '{$modeswitch arrayoperators}',
  8946. 'type',
  8947. ' integer = longint;',
  8948. ' TArrInt = array[1..2] of integer;',
  8949. ' TArrArrInt = array of TArrInt;',
  8950. 'procedure DoInt(a: TArrArrInt);',
  8951. 'begin',
  8952. ' DoInt(a+[[1,2]]);',
  8953. ' DoInt([[1,2]]+a);',
  8954. ' DoInt(a);',
  8955. 'end;',
  8956. 'var',
  8957. ' i: TArrInt;',
  8958. ' a: TArrArrInt;',
  8959. 'begin',
  8960. ' a:=[[1,1]];',
  8961. ' a:=[i];',
  8962. ' a:=a+[i];',
  8963. ' a:=[i]+a;',
  8964. ' DoInt([[1,1]]);',
  8965. ' DoInt([[1,2],[3,4]]);',
  8966. '']);
  8967. ConvertProgram;
  8968. CheckSource('TestArray_ArrayLitStaticAsParam',
  8969. LinesToStr([ // statements
  8970. 'this.DoInt = function (a) {',
  8971. ' $mod.DoInt(rtl.arrayConcatN(a, [[1, 2]]));',
  8972. ' $mod.DoInt(rtl.arrayConcatN([[1, 2]], a));',
  8973. ' $mod.DoInt(a);',
  8974. '};',
  8975. 'this.i = rtl.arraySetLength(null, 0, 2);',
  8976. 'this.a = [];',
  8977. '']),
  8978. LinesToStr([ // $mod.$main
  8979. '$mod.a = [[1, 1]];',
  8980. '$mod.a = [$mod.i.slice(0)];',
  8981. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i.slice(0)]);',
  8982. '$mod.a = rtl.arrayConcatN([$mod.i.slice(0)], $mod.a);',
  8983. '$mod.DoInt([[1, 1]]);',
  8984. '$mod.DoInt([[1, 2], [3, 4]]);',
  8985. '']));
  8986. end;
  8987. procedure TTestModule.TestArray_ForInArrOfString;
  8988. begin
  8989. StartProgram(false);
  8990. Add([
  8991. 'type',
  8992. 'type',
  8993. ' TMonthNameArray = array [1..12] of string;',
  8994. ' TMonthNames = TMonthNameArray;',
  8995. ' TObject = class',
  8996. ' private',
  8997. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  8998. ' public',
  8999. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  9000. ' end;',
  9001. 'var f: TObject;',
  9002. ' Month: string;',
  9003. 'begin',
  9004. ' for Month in f.LongMonthNames do ;',
  9005. '']);
  9006. ConvertProgram;
  9007. CheckSource('TestArray_ForInArrOfString',
  9008. LinesToStr([ // statements
  9009. 'rtl.createClass($mod, "TObject", null, function () {',
  9010. ' this.$init = function () {',
  9011. ' };',
  9012. ' this.$final = function () {',
  9013. ' };',
  9014. '});',
  9015. 'this.f = null;',
  9016. 'this.Month = "";',
  9017. '']),
  9018. LinesToStr([ // $mod.$main
  9019. 'for (var $in1 = $mod.f.GetLongMonthNames(), $l2 = 0, $end3 = rtl.length($in1) - 1; $l2 <= $end3; $l2++) $mod.Month = $in1[$l2];',
  9020. '']));
  9021. end;
  9022. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  9023. begin
  9024. StartProgram(false);
  9025. Add([
  9026. '{$modeswitch externalclass}',
  9027. 'type',
  9028. ' TJSObject = class external name ''Object''',
  9029. ' end;',
  9030. ' TJSArray = class external name ''Array''',
  9031. ' class function isArray(Value: JSValue) : boolean;',
  9032. ' function concat() : TJSArray; varargs;',
  9033. ' end;',
  9034. 'var',
  9035. ' aObj: TJSArray;',
  9036. ' a: array of longint;',
  9037. ' o: TJSObject;',
  9038. 'begin',
  9039. ' if TJSArray.isArray(65) then ;',
  9040. ' aObj:=TJSArray(a).concat(a);',
  9041. ' o:=TJSObject(a);']);
  9042. ConvertProgram;
  9043. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  9044. LinesToStr([ // statements
  9045. 'this.aObj = null;',
  9046. 'this.a = [];',
  9047. 'this.o = null;',
  9048. '']),
  9049. LinesToStr([ // $mod.$main
  9050. 'if (Array.isArray(65)) ;',
  9051. '$mod.aObj = $mod.a.concat($mod.a);',
  9052. '$mod.o = $mod.a;',
  9053. '']));
  9054. end;
  9055. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  9056. begin
  9057. StartProgram(false);
  9058. Add([
  9059. '{$modeswitch externalclass}',
  9060. 'type',
  9061. ' TArrStr = array of string;',
  9062. ' TJSArray = class external name ''Array''',
  9063. ' end;',
  9064. ' TJSObject = class external name ''Object''',
  9065. ' end;',
  9066. 'var',
  9067. ' aObj: TJSArray;',
  9068. ' a: TArrStr;',
  9069. ' jo: TJSObject;',
  9070. 'begin',
  9071. ' a:=TArrStr(aObj);',
  9072. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  9073. ' a:=TarrStr(jo);',
  9074. '']);
  9075. ConvertProgram;
  9076. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  9077. LinesToStr([ // statements
  9078. 'this.aObj = null;',
  9079. 'this.a = [];',
  9080. 'this.jo = null;',
  9081. '']),
  9082. LinesToStr([ // $mod.$main
  9083. '$mod.a = $mod.aObj;',
  9084. '$mod.aObj[1] = $mod.aObj[2];',
  9085. '$mod.a = $mod.jo;',
  9086. '']));
  9087. end;
  9088. procedure TTestModule.TestRecord_Empty;
  9089. begin
  9090. StartProgram(false);
  9091. Add(['type',
  9092. ' TRecA = record',
  9093. ' end;',
  9094. 'var a,b: TRecA;',
  9095. 'begin',
  9096. ' if a=b then ;']);
  9097. ConvertProgram;
  9098. CheckSource('TestRecord_Empty',
  9099. LinesToStr([ // statements
  9100. 'rtl.recNewT($mod, "TRecA", function () {',
  9101. ' this.$eq = function (b) {',
  9102. ' return true;',
  9103. ' };',
  9104. ' this.$assign = function (s) {',
  9105. ' return this;',
  9106. ' };',
  9107. '});',
  9108. 'this.a = $mod.TRecA.$new();',
  9109. 'this.b = $mod.TRecA.$new();',
  9110. '']),
  9111. LinesToStr([ // $mod.$main
  9112. 'if ($mod.a.$eq($mod.b)) ;'
  9113. ]));
  9114. end;
  9115. procedure TTestModule.TestRecord_Var;
  9116. begin
  9117. StartProgram(false);
  9118. Add('type');
  9119. Add(' TRecA = record');
  9120. Add(' Bold: longint;');
  9121. Add(' end;');
  9122. Add('var Rec: TRecA;');
  9123. Add('begin');
  9124. Add(' rec.bold:=123');
  9125. ConvertProgram;
  9126. CheckSource('TestRecord_Var',
  9127. LinesToStr([ // statements
  9128. 'rtl.recNewT($mod, "TRecA", function () {',
  9129. ' this.Bold = 0;',
  9130. ' this.$eq = function (b) {',
  9131. ' return this.Bold === b.Bold;',
  9132. ' };',
  9133. ' this.$assign = function (s) {',
  9134. ' this.Bold = s.Bold;',
  9135. ' return this;',
  9136. ' };',
  9137. '});',
  9138. 'this.Rec = $mod.TRecA.$new();',
  9139. '']),
  9140. LinesToStr([ // $mod.$main
  9141. '$mod.Rec.Bold = 123;'
  9142. ]));
  9143. end;
  9144. procedure TTestModule.TestRecord_VarExternal;
  9145. begin
  9146. StartProgram(false);
  9147. Add([
  9148. '{$modeswitch externalclass}',
  9149. 'type',
  9150. ' TRecA = record',
  9151. ' i: byte;',
  9152. ' length_: longint external name ''length'';',
  9153. ' end;',
  9154. 'var Rec: TRecA;',
  9155. 'begin',
  9156. ' rec.length_ := rec.length_',
  9157. '']);
  9158. ConvertProgram;
  9159. CheckSource('TestRecord_VarExternal',
  9160. LinesToStr([ // statements
  9161. 'rtl.recNewT($mod, "TRecA", function () {',
  9162. ' this.i = 0;',
  9163. ' this.$eq = function (b) {',
  9164. ' return (this.i === b.i) && (this.length === b.length);',
  9165. ' };',
  9166. ' this.$assign = function (s) {',
  9167. ' this.i = s.i;',
  9168. ' this.length = s.length;',
  9169. ' return this;',
  9170. ' };',
  9171. '});',
  9172. 'this.Rec = $mod.TRecA.$new();',
  9173. '']),
  9174. LinesToStr([ // $mod.$main
  9175. '$mod.Rec.length = $mod.Rec.length;'
  9176. ]));
  9177. end;
  9178. procedure TTestModule.TestRecord_WithDo;
  9179. begin
  9180. StartProgram(false);
  9181. Add('type');
  9182. Add(' TRec = record');
  9183. Add(' vI: longint;');
  9184. Add(' end;');
  9185. Add('var');
  9186. Add(' Int: longint;');
  9187. Add(' r: TRec;');
  9188. Add('begin');
  9189. Add(' with r do');
  9190. Add(' int:=vi;');
  9191. Add(' with r do begin');
  9192. Add(' int:=vi;');
  9193. Add(' vi:=int;');
  9194. Add(' end;');
  9195. ConvertProgram;
  9196. CheckSource('TestWithRecordDo',
  9197. LinesToStr([ // statements
  9198. 'rtl.recNewT($mod, "TRec", function () {',
  9199. ' this.vI = 0;',
  9200. ' this.$eq = function (b) {',
  9201. ' return this.vI === b.vI;',
  9202. ' };',
  9203. ' this.$assign = function (s) {',
  9204. ' this.vI = s.vI;',
  9205. ' return this;',
  9206. ' };',
  9207. '});',
  9208. 'this.Int = 0;',
  9209. 'this.r = $mod.TRec.$new();',
  9210. '']),
  9211. LinesToStr([ // $mod.$main
  9212. 'var $with1 = $mod.r;',
  9213. '$mod.Int = $with1.vI;',
  9214. 'var $with2 = $mod.r;',
  9215. '$mod.Int = $with2.vI;',
  9216. '$with2.vI = $mod.Int;'
  9217. ]));
  9218. end;
  9219. procedure TTestModule.TestRecord_Assign;
  9220. begin
  9221. StartProgram(false);
  9222. Add('type');
  9223. Add(' TEnum = (red,green);');
  9224. Add(' TEnums = set of TEnum;');
  9225. Add(' TSmallRec = record');
  9226. Add(' N: longint;');
  9227. Add(' end;');
  9228. Add(' TBigRec = record');
  9229. Add(' Int: longint;');
  9230. Add(' D: double;');
  9231. Add(' Arr: array of longint;');
  9232. Add(' Arr2: array[1..2] of longint;');
  9233. Add(' Small: TSmallRec;');
  9234. Add(' Enums: TEnums;');
  9235. Add(' end;');
  9236. Add('var');
  9237. Add(' r, s: TBigRec;');
  9238. Add('begin');
  9239. Add(' r:=s;');
  9240. Add(' r:=default(TBigRec);');
  9241. Add(' r:=default(s);');
  9242. ConvertProgram;
  9243. CheckSource('TestRecord_Assign',
  9244. LinesToStr([ // statements
  9245. 'this.TEnum = {',
  9246. ' "0": "red",',
  9247. ' red: 0,',
  9248. ' "1": "green",',
  9249. ' green: 1',
  9250. '};',
  9251. 'rtl.recNewT($mod, "TSmallRec", function () {',
  9252. ' this.N = 0;',
  9253. ' this.$eq = function (b) {',
  9254. ' return this.N === b.N;',
  9255. ' };',
  9256. ' this.$assign = function (s) {',
  9257. ' this.N = s.N;',
  9258. ' return this;',
  9259. ' };',
  9260. '});',
  9261. 'rtl.recNewT($mod, "TBigRec", function () {',
  9262. ' this.Int = 0;',
  9263. ' this.D = 0.0;',
  9264. ' this.Arr = [];',
  9265. ' this.$new = function () {',
  9266. ' var r = Object.create(this);',
  9267. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  9268. ' r.Small = $mod.TSmallRec.$new();',
  9269. ' r.Enums = {};',
  9270. ' return r;',
  9271. ' };',
  9272. ' this.$eq = function (b) {',
  9273. ' 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);',
  9274. ' };',
  9275. ' this.$assign = function (s) {',
  9276. ' this.Int = s.Int;',
  9277. ' this.D = s.D;',
  9278. ' this.Arr = s.Arr;',
  9279. ' this.Arr2 = s.Arr2.slice(0);',
  9280. ' this.Small.$assign(s.Small);',
  9281. ' this.Enums = rtl.refSet(s.Enums);',
  9282. ' return this;',
  9283. ' };',
  9284. '});',
  9285. 'this.r = $mod.TBigRec.$new();',
  9286. 'this.s = $mod.TBigRec.$new();',
  9287. '']),
  9288. LinesToStr([ // $mod.$main
  9289. '$mod.r.$assign($mod.s);',
  9290. '$mod.r.$assign($mod.TBigRec.$new());',
  9291. '$mod.r.$assign($mod.TBigRec.$new());',
  9292. '']));
  9293. end;
  9294. procedure TTestModule.TestRecord_AsParams;
  9295. begin
  9296. StartProgram(false);
  9297. Add([
  9298. 'type',
  9299. ' integer = longint;',
  9300. ' TRecord = record',
  9301. ' i: integer;',
  9302. ' end;',
  9303. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  9304. 'var vL: TRecord;',
  9305. 'begin',
  9306. ' vd:=vd;',
  9307. ' vd.i:=vd.i;',
  9308. ' vl:=vc;',
  9309. ' vv:=vv;',
  9310. ' vv.i:=vv.i;',
  9311. ' U:=vl;',
  9312. ' U:=vd;',
  9313. ' U:=vc;',
  9314. ' U:=vv;',
  9315. ' doit(vd,vd,vd,vd);',
  9316. ' doit(vc,vc,vl,vl);',
  9317. ' doit(vv,vv,vv,vv);',
  9318. ' doit(vl,vl,vl,vl);',
  9319. 'end;',
  9320. 'var i: TRecord;',
  9321. 'begin',
  9322. ' doit(i,i,i,i);']);
  9323. ConvertProgram;
  9324. CheckSource('TestRecord_AsParams',
  9325. LinesToStr([ // statements
  9326. 'rtl.recNewT($mod, "TRecord", function () {',
  9327. ' this.i = 0;',
  9328. ' this.$eq = function (b) {',
  9329. ' return this.i === b.i;',
  9330. ' };',
  9331. ' this.$assign = function (s) {',
  9332. ' this.i = s.i;',
  9333. ' return this;',
  9334. ' };',
  9335. '});',
  9336. 'this.DoIt = function (vD, vC, vV, U) {',
  9337. ' var vL = $mod.TRecord.$new();',
  9338. ' vD.$assign(vD);',
  9339. ' vD.i = vD.i;',
  9340. ' vL.$assign(vC);',
  9341. ' vV.$assign(vV);',
  9342. ' vV.i = vV.i;',
  9343. ' U.set(vL);',
  9344. ' U.set(vD);',
  9345. ' U.set(vC);',
  9346. ' U.set(vV);',
  9347. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, {',
  9348. ' get: function () {',
  9349. ' return vD;',
  9350. ' },',
  9351. ' set: function (v) {',
  9352. ' vD.$assign(v);',
  9353. ' }',
  9354. ' });',
  9355. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, {',
  9356. ' get: function () {',
  9357. ' return vL;',
  9358. ' },',
  9359. ' set: function (v) {',
  9360. ' vL.$assign(v);',
  9361. ' }',
  9362. ' });',
  9363. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, {',
  9364. ' get: function () {',
  9365. ' return vV;',
  9366. ' },',
  9367. ' set: function (v) {',
  9368. ' vV.$assign(v);',
  9369. ' }',
  9370. ' });',
  9371. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, {',
  9372. ' get: function () {',
  9373. ' return vL;',
  9374. ' },',
  9375. ' set: function (v) {',
  9376. ' vL.$assign(v);',
  9377. ' }',
  9378. ' });',
  9379. '};',
  9380. 'this.i = $mod.TRecord.$new();'
  9381. ]),
  9382. LinesToStr([
  9383. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, {',
  9384. ' p: $mod,',
  9385. ' get: function () {',
  9386. ' return this.p.i;',
  9387. ' },',
  9388. ' set: function (v) {',
  9389. ' this.p.i.$assign(v);',
  9390. ' }',
  9391. '});',
  9392. '']));
  9393. end;
  9394. procedure TTestModule.TestRecordElement_AsParams;
  9395. begin
  9396. StartProgram(false);
  9397. Add('type');
  9398. Add(' integer = longint;');
  9399. Add(' TRecord = record');
  9400. Add(' i: integer;');
  9401. Add(' end;');
  9402. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  9403. Add('var vJ: TRecord;');
  9404. Add('begin');
  9405. Add(' doit(vj.i,vj.i,vj.i);');
  9406. Add('end;');
  9407. Add('var r: TRecord;');
  9408. Add('begin');
  9409. Add(' doit(r.i,r.i,r.i);');
  9410. ConvertProgram;
  9411. CheckSource('TestRecordElement_AsParams',
  9412. LinesToStr([ // statements
  9413. 'rtl.recNewT($mod, "TRecord", function () {',
  9414. ' this.i = 0;',
  9415. ' this.$eq = function (b) {',
  9416. ' return this.i === b.i;',
  9417. ' };',
  9418. ' this.$assign = function (s) {',
  9419. ' this.i = s.i;',
  9420. ' return this;',
  9421. ' };',
  9422. '});',
  9423. 'this.DoIt = function (vG,vH,vI) {',
  9424. ' var vJ = $mod.TRecord.$new();',
  9425. ' $mod.DoIt(vJ.i, vJ.i, {',
  9426. ' p: vJ,',
  9427. ' get: function () {',
  9428. ' return this.p.i;',
  9429. ' },',
  9430. ' set: function (v) {',
  9431. ' this.p.i = v;',
  9432. ' }',
  9433. ' });',
  9434. '};',
  9435. 'this.r = $mod.TRecord.$new();'
  9436. ]),
  9437. LinesToStr([
  9438. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  9439. ' p: $mod.r,',
  9440. ' get: function () {',
  9441. ' return this.p.i;',
  9442. ' },',
  9443. ' set: function (v) {',
  9444. ' this.p.i = v;',
  9445. ' }',
  9446. '});'
  9447. ]));
  9448. end;
  9449. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  9450. begin
  9451. StartProgram(false);
  9452. Add('type');
  9453. Add(' integer = longint;');
  9454. Add(' TRecord = record');
  9455. Add(' i: integer;');
  9456. Add(' end;');
  9457. Add('function GetRec(vB: integer = 0): TRecord;');
  9458. Add('begin');
  9459. Add('end;');
  9460. Add('procedure DoIt(vG: integer; const vH: integer);');
  9461. Add('begin');
  9462. Add('end;');
  9463. Add('begin');
  9464. Add(' doit(getrec.i,getrec.i);');
  9465. Add(' doit(getrec().i,getrec().i);');
  9466. Add(' doit(getrec(1).i,getrec(2).i);');
  9467. ConvertProgram;
  9468. CheckSource('TestRecordElementFromFuncResult_AsParams',
  9469. LinesToStr([ // statements
  9470. 'rtl.recNewT($mod, "TRecord", function () {',
  9471. ' this.i = 0;',
  9472. ' this.$eq = function (b) {',
  9473. ' return this.i === b.i;',
  9474. ' };',
  9475. ' this.$assign = function (s) {',
  9476. ' this.i = s.i;',
  9477. ' return this;',
  9478. ' };',
  9479. '});',
  9480. 'this.GetRec = function (vB) {',
  9481. ' var Result = $mod.TRecord.$new();',
  9482. ' return Result;',
  9483. '};',
  9484. 'this.DoIt = function (vG, vH) {',
  9485. '};',
  9486. '']),
  9487. LinesToStr([
  9488. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  9489. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  9490. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  9491. '']));
  9492. end;
  9493. procedure TTestModule.TestRecordElementFromWith_AsParams;
  9494. begin
  9495. StartProgram(false);
  9496. Add('type');
  9497. Add(' integer = longint;');
  9498. Add(' TRecord = record');
  9499. Add(' i: integer;');
  9500. Add(' end;');
  9501. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  9502. Add('begin');
  9503. Add('end;');
  9504. Add('var r: trecord;');
  9505. Add('begin');
  9506. Add(' with r do ');
  9507. Add(' doit(i,i,i);');
  9508. ConvertProgram;
  9509. CheckSource('TestRecordElementFromWith_AsParams',
  9510. LinesToStr([ // statements
  9511. 'rtl.recNewT($mod, "TRecord", function () {',
  9512. ' this.i = 0;',
  9513. ' this.$eq = function (b) {',
  9514. ' return this.i === b.i;',
  9515. ' };',
  9516. ' this.$assign = function (s) {',
  9517. ' this.i = s.i;',
  9518. ' return this;',
  9519. ' };',
  9520. '});',
  9521. 'this.DoIt = function (vG,vH,vI) {',
  9522. '};',
  9523. 'this.r = $mod.TRecord.$new();'
  9524. ]),
  9525. LinesToStr([
  9526. 'var $with1 = $mod.r;',
  9527. '$mod.DoIt($with1.i,$with1.i,{',
  9528. ' p: $with1,',
  9529. ' get: function () {',
  9530. ' return this.p.i;',
  9531. ' },',
  9532. ' set: function (v) {',
  9533. ' this.p.i = v;',
  9534. ' }',
  9535. '});',
  9536. '']));
  9537. end;
  9538. procedure TTestModule.TestRecord_Equal;
  9539. begin
  9540. StartProgram(false);
  9541. Add('type');
  9542. Add(' integer = longint;');
  9543. Add(' TFlag = (red,blue);');
  9544. Add(' TFlags = set of TFlag;');
  9545. Add(' TProc = procedure;');
  9546. Add(' TRecord = record');
  9547. Add(' i: integer;');
  9548. Add(' Event: TProc;');
  9549. Add(' f: TFlags;');
  9550. Add(' end;');
  9551. Add(' TNested = record');
  9552. Add(' r: TRecord;');
  9553. Add(' end;');
  9554. Add('var');
  9555. Add(' b: boolean;');
  9556. Add(' r,s: trecord;');
  9557. Add('begin');
  9558. Add(' b:=r=s;');
  9559. Add(' b:=r<>s;');
  9560. ConvertProgram;
  9561. CheckSource('TestRecord_Equal',
  9562. LinesToStr([ // statements
  9563. 'this.TFlag = {',
  9564. ' "0": "red",',
  9565. ' red: 0,',
  9566. ' "1": "blue",',
  9567. ' blue: 1',
  9568. '};',
  9569. 'rtl.recNewT($mod, "TRecord", function () {',
  9570. ' this.i = 0;',
  9571. ' this.Event = null;',
  9572. ' this.$new = function () {',
  9573. ' var r = Object.create(this);',
  9574. ' r.f = {};',
  9575. ' return r;',
  9576. ' };',
  9577. ' this.$eq = function (b) {',
  9578. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  9579. ' };',
  9580. ' this.$assign = function (s) {',
  9581. ' this.i = s.i;',
  9582. ' this.Event = s.Event;',
  9583. ' this.f = rtl.refSet(s.f);',
  9584. ' return this;',
  9585. ' };',
  9586. '});',
  9587. 'rtl.recNewT($mod, "TNested", function () {',
  9588. ' this.$new = function () {',
  9589. ' var r = Object.create(this);',
  9590. ' r.r = $mod.TRecord.$new();',
  9591. ' return r;',
  9592. ' };',
  9593. ' this.$eq = function (b) {',
  9594. ' return this.r.$eq(b.r);',
  9595. ' };',
  9596. ' this.$assign = function (s) {',
  9597. ' this.r.$assign(s.r);',
  9598. ' return this;',
  9599. ' };',
  9600. '});',
  9601. 'this.b = false;',
  9602. 'this.r = $mod.TRecord.$new();',
  9603. 'this.s = $mod.TRecord.$new();',
  9604. '']),
  9605. LinesToStr([
  9606. '$mod.b = $mod.r.$eq($mod.s);',
  9607. '$mod.b = !$mod.r.$eq($mod.s);',
  9608. '']));
  9609. end;
  9610. procedure TTestModule.TestRecord_TypeCastJSValueToRecord;
  9611. begin
  9612. StartProgram(false);
  9613. Add('type');
  9614. Add(' TRecord = record');
  9615. Add(' i: longint;');
  9616. Add(' end;');
  9617. Add('var');
  9618. Add(' Jv: jsvalue;');
  9619. Add(' Rec: trecord;');
  9620. Add('begin');
  9621. Add(' rec:=trecord(jv);');
  9622. ConvertProgram;
  9623. CheckSource('TestRecord_TypeCastJSValueToRecord',
  9624. LinesToStr([ // statements
  9625. 'rtl.recNewT($mod, "TRecord", function () {',
  9626. ' this.i = 0;',
  9627. ' this.$eq = function (b) {',
  9628. ' return this.i === b.i;',
  9629. ' };',
  9630. ' this.$assign = function (s) {',
  9631. ' this.i = s.i;',
  9632. ' return this;',
  9633. ' };',
  9634. '});',
  9635. 'this.Jv = undefined;',
  9636. 'this.Rec = $mod.TRecord.$new();',
  9637. '']),
  9638. LinesToStr([
  9639. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  9640. '']));
  9641. end;
  9642. procedure TTestModule.TestRecord_VariantFail;
  9643. begin
  9644. StartProgram(false);
  9645. Add([
  9646. 'type',
  9647. ' TRec = record',
  9648. ' case word of',
  9649. ' 0: (b0, b1: Byte);',
  9650. ' 1: (i: word);',
  9651. ' end;',
  9652. 'begin']);
  9653. SetExpectedPasResolverError('variant record is not supported',
  9654. nXIsNotSupported);
  9655. ConvertProgram;
  9656. end;
  9657. procedure TTestModule.TestRecord_FieldArray;
  9658. begin
  9659. StartProgram(false);
  9660. Add([
  9661. 'type',
  9662. ' TArrInt = array[3..4] of longint;',
  9663. ' TArrArrInt = array[3..4] of longint;',
  9664. ' TRec = record',
  9665. ' a: array of longint;',
  9666. ' s: array[1..2] of longint;',
  9667. ' m: array[1..2,3..4] of longint;',
  9668. ' o: TArrArrInt;',
  9669. ' end;',
  9670. 'begin']);
  9671. ConvertProgram;
  9672. CheckSource('TestRecord_FieldArray',
  9673. LinesToStr([ // statements
  9674. 'rtl.recNewT($mod, "TRec", function () {',
  9675. ' this.a = [];',
  9676. ' this.$new = function () {',
  9677. ' var r = Object.create(this);',
  9678. ' r.s = rtl.arraySetLength(null, 0, 2);',
  9679. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  9680. ' r.o = rtl.arraySetLength(null, 0, 2);',
  9681. ' return r;',
  9682. ' };',
  9683. ' this.$eq = function (b) {',
  9684. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  9685. ' };',
  9686. ' this.$assign = function (s) {',
  9687. ' this.a = s.a;',
  9688. ' this.s = s.s.slice(0);',
  9689. ' this.m = s.m.slice(0);',
  9690. ' this.o = s.o.slice(0);',
  9691. ' return this;',
  9692. ' };',
  9693. '});',
  9694. '']),
  9695. LinesToStr([ // $mod.$main
  9696. '']));
  9697. end;
  9698. procedure TTestModule.TestRecord_Const;
  9699. begin
  9700. StartProgram(false);
  9701. Add([
  9702. 'type',
  9703. ' TArrInt = array[3..4] of longint;',
  9704. ' TPoint = record x,y: longint; end;',
  9705. ' TRec = record',
  9706. ' i: longint;',
  9707. ' a: array of longint;',
  9708. ' s: array[1..2] of longint;',
  9709. ' m: array[1..2,3..4] of longint;',
  9710. ' p: TPoint;',
  9711. ' end;',
  9712. ' TPoints = array of TPoint;',
  9713. 'const',
  9714. ' r: TRec = (',
  9715. ' i:1;',
  9716. ' a:(2,3);',
  9717. ' s:(4,5);',
  9718. ' m:( (11,12), (13,14) );',
  9719. ' p: (x:21; y:22)',
  9720. ' );',
  9721. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  9722. 'begin']);
  9723. ConvertProgram;
  9724. CheckSource('TestRecord_Const',
  9725. LinesToStr([ // statements
  9726. 'rtl.recNewT($mod, "TPoint", function () {',
  9727. ' this.x = 0;',
  9728. ' this.y = 0;',
  9729. ' this.$eq = function (b) {',
  9730. ' return (this.x === b.x) && (this.y === b.y);',
  9731. ' };',
  9732. ' this.$assign = function (s) {',
  9733. ' this.x = s.x;',
  9734. ' this.y = s.y;',
  9735. ' return this;',
  9736. ' };',
  9737. '});',
  9738. 'rtl.recNewT($mod, "TRec", function () {',
  9739. ' this.i = 0;',
  9740. ' this.a = [];',
  9741. ' this.$new = function () {',
  9742. ' var r = Object.create(this);',
  9743. ' r.s = rtl.arraySetLength(null, 0, 2);',
  9744. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  9745. ' r.p = $mod.TPoint.$new();',
  9746. ' return r;',
  9747. ' };',
  9748. ' this.$eq = function (b) {',
  9749. ' 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);',
  9750. ' };',
  9751. ' this.$assign = function (s) {',
  9752. ' this.i = s.i;',
  9753. ' this.a = s.a;',
  9754. ' this.s = s.s.slice(0);',
  9755. ' this.m = s.m.slice(0);',
  9756. ' this.p.$assign(s.p);',
  9757. ' return this;',
  9758. ' };',
  9759. '});',
  9760. 'this.r = $mod.TRec.$clone({',
  9761. ' i: 1,',
  9762. ' a: [2, 3],',
  9763. ' s: [4, 5],',
  9764. ' m: [[11, 12], [13, 14]],',
  9765. ' p: $mod.TPoint.$clone({',
  9766. ' x: 21,',
  9767. ' y: 22',
  9768. ' })',
  9769. '});',
  9770. 'this.p = [$mod.TPoint.$clone({',
  9771. ' x: 1,',
  9772. ' y: 2',
  9773. '}), $mod.TPoint.$clone({',
  9774. ' x: 3,',
  9775. ' y: 4',
  9776. '})];',
  9777. '']),
  9778. LinesToStr([ // $mod.$main
  9779. '']));
  9780. end;
  9781. procedure TTestModule.TestRecord_TypecastFail;
  9782. begin
  9783. StartProgram(false);
  9784. Add([
  9785. 'type',
  9786. ' TPoint = record x,y: longint; end;',
  9787. ' TRec = record l: longint end;',
  9788. 'var p: TPoint;',
  9789. 'begin',
  9790. ' if TRec(p).l=2 then ;']);
  9791. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  9792. nIllegalTypeConversionTo);
  9793. ConvertProgram;
  9794. end;
  9795. procedure TTestModule.TestRecord_InFunction;
  9796. begin
  9797. StartProgram(false);
  9798. Add([
  9799. 'var TPoint: longint = 3;',
  9800. 'procedure DoIt;',
  9801. 'type',
  9802. ' TPoint = record x,y: longint; end;',
  9803. ' TPoints = array of TPoint;',
  9804. 'var',
  9805. ' r: TPoint;',
  9806. ' p: TPoints;',
  9807. 'begin',
  9808. ' SetLength(p,2);',
  9809. 'end;',
  9810. 'begin']);
  9811. ConvertProgram;
  9812. CheckSource('TestRecord_InFunction',
  9813. LinesToStr([ // statements
  9814. 'this.TPoint = 3;',
  9815. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  9816. ' this.x = 0;',
  9817. ' this.y = 0;',
  9818. ' this.$eq = function (b) {',
  9819. ' return (this.x === b.x) && (this.y === b.y);',
  9820. ' };',
  9821. ' this.$assign = function (s) {',
  9822. ' this.x = s.x;',
  9823. ' this.y = s.y;',
  9824. ' return this;',
  9825. ' };',
  9826. '});',
  9827. 'this.DoIt = function () {',
  9828. ' var r = TPoint$1.$new();',
  9829. ' var p = [];',
  9830. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  9831. '};',
  9832. '']),
  9833. LinesToStr([ // $mod.$main
  9834. '']));
  9835. end;
  9836. procedure TTestModule.TestAdvRecord_Function;
  9837. begin
  9838. StartProgram(false);
  9839. Parser.Options:=Parser.Options+[po_cassignments];
  9840. Add([
  9841. '{$modeswitch AdvancedRecords}',
  9842. 'type',
  9843. ' TPoint = record',
  9844. ' x,y: word;',
  9845. ' function Add(const apt: TPoint): TPoint;',
  9846. ' end;',
  9847. 'function TPoint.Add(const apt: TPoint): TPoint;',
  9848. 'begin',
  9849. ' Result:=Self;',
  9850. ' Result.x+=apt.x;',
  9851. ' Result.y:=Result.y+apt.y;',
  9852. ' Self:=apt;',
  9853. 'end;',
  9854. 'var p,q: TPoint;',
  9855. 'begin',
  9856. ' p.add(q);',
  9857. ' p:=default(TPoint);',
  9858. ' p:=q;',
  9859. '']);
  9860. ConvertProgram;
  9861. CheckSource('TestAdvRecord_Function',
  9862. LinesToStr([ // statements
  9863. 'rtl.recNewT($mod, "TPoint", function () {',
  9864. ' this.x = 0;',
  9865. ' this.y = 0;',
  9866. ' this.$eq = function (b) {',
  9867. ' return (this.x === b.x) && (this.y === b.y);',
  9868. ' };',
  9869. ' this.$assign = function (s) {',
  9870. ' this.x = s.x;',
  9871. ' this.y = s.y;',
  9872. ' return this;',
  9873. ' };',
  9874. ' this.Add = function (apt) {',
  9875. ' var Result = $mod.TPoint.$new();',
  9876. ' Result.$assign(this);',
  9877. ' Result.x += apt.x;',
  9878. ' Result.y = Result.y + apt.y;',
  9879. ' this.$assign(apt);',
  9880. ' return Result;',
  9881. ' };',
  9882. '});',
  9883. 'this.p = $mod.TPoint.$new();',
  9884. 'this.q = $mod.TPoint.$new();',
  9885. '']),
  9886. LinesToStr([ // $mod.$main
  9887. '$mod.p.Add($mod.q);',
  9888. '$mod.p.$assign($mod.TPoint.$new());',
  9889. '$mod.p.$assign($mod.q);',
  9890. '']));
  9891. end;
  9892. procedure TTestModule.TestAdvRecord_Property;
  9893. begin
  9894. StartProgram(false);
  9895. Add([
  9896. '{$modeswitch AdvancedRecords}',
  9897. 'type',
  9898. ' TPoint = record',
  9899. ' x,y: word;',
  9900. ' strict private',
  9901. ' function GetSize: longword;',
  9902. ' procedure SetSize(Value: longword);',
  9903. ' public',
  9904. ' property Size: longword read GetSize write SetSize;',
  9905. ' property Left: word read x write y;',
  9906. ' end;',
  9907. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  9908. 'function TPoint.GetSize: longword;',
  9909. 'begin',
  9910. ' x:=y;',
  9911. ' Size:=Size;',
  9912. ' Left:=Left;',
  9913. 'end;',
  9914. 'procedure TPoint.SetSize(Value: longword);',
  9915. 'begin',
  9916. 'end;',
  9917. 'var p,q: TPoint;',
  9918. 'begin',
  9919. ' p.Size:=q.Size;',
  9920. ' p.Left:=q.Left;',
  9921. '']);
  9922. ConvertProgram;
  9923. CheckSource('TestAdvRecord_Property',
  9924. LinesToStr([ // statements
  9925. 'rtl.recNewT($mod, "TPoint", function () {',
  9926. ' this.x = 0;',
  9927. ' this.y = 0;',
  9928. ' this.$eq = function (b) {',
  9929. ' return (this.x === b.x) && (this.y === b.y);',
  9930. ' };',
  9931. ' this.$assign = function (s) {',
  9932. ' this.x = s.x;',
  9933. ' this.y = s.y;',
  9934. ' return this;',
  9935. ' };',
  9936. ' this.GetSize = function () {',
  9937. ' var Result = 0;',
  9938. ' this.x = this.y;',
  9939. ' this.SetSize(this.GetSize());',
  9940. ' this.y = this.x;',
  9941. ' return Result;',
  9942. ' };',
  9943. ' this.SetSize = function (Value) {',
  9944. ' };',
  9945. '});',
  9946. 'this.SetSize = function (Value) {',
  9947. '};',
  9948. 'this.p = $mod.TPoint.$new();',
  9949. 'this.q = $mod.TPoint.$new();',
  9950. '']),
  9951. LinesToStr([ // $mod.$main
  9952. '$mod.p.SetSize($mod.q.GetSize());',
  9953. '$mod.p.y = $mod.q.x;',
  9954. '']));
  9955. end;
  9956. procedure TTestModule.TestAdvRecord_PropertyDefault;
  9957. begin
  9958. StartProgram(false);
  9959. Add([
  9960. '{$modeswitch AdvancedRecords}',
  9961. 'type',
  9962. ' TPoint = record',
  9963. ' strict private',
  9964. ' function GetItems(Index: word): word;',
  9965. ' procedure SetItems(Index: word; Value: word);',
  9966. ' public',
  9967. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  9968. ' end;',
  9969. 'function TPoint.GetItems(Index: word): word;',
  9970. 'begin',
  9971. ' Items[index]:=Items[index];',
  9972. ' self.Items[index]:=self.Items[index];',
  9973. 'end;',
  9974. 'procedure TPoint.SetItems(Index: word; Value: word);',
  9975. 'begin',
  9976. 'end;',
  9977. 'var p: TPoint;',
  9978. 'begin',
  9979. ' p[1]:=p[2];',
  9980. ' p.Items[3]:=p.Items[4];',
  9981. '']);
  9982. ConvertProgram;
  9983. CheckSource('TestAdvRecord_PropertyDefault',
  9984. LinesToStr([ // statements
  9985. 'rtl.recNewT($mod, "TPoint", function () {',
  9986. ' this.$eq = function (b) {',
  9987. ' return true;',
  9988. ' };',
  9989. ' this.$assign = function (s) {',
  9990. ' return this;',
  9991. ' };',
  9992. ' this.GetItems = function (Index) {',
  9993. ' var Result = 0;',
  9994. ' this.SetItems(Index, this.GetItems(Index));',
  9995. ' this.SetItems(Index, this.GetItems(Index));',
  9996. ' return Result;',
  9997. ' };',
  9998. ' this.SetItems = function (Index, Value) {',
  9999. ' };',
  10000. '});',
  10001. 'this.p = $mod.TPoint.$new();',
  10002. '']),
  10003. LinesToStr([ // $mod.$main
  10004. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  10005. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  10006. '']));
  10007. end;
  10008. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  10009. begin
  10010. StartProgram(false);
  10011. Add([
  10012. '{$modeswitch AdvancedRecords}',
  10013. 'type',
  10014. ' TRec = record',
  10015. ' class var Fx: longint;',
  10016. ' class var Fy: longint;',
  10017. ' class function GetInt: longint; static;',
  10018. ' class procedure SetInt(Value: longint); static;',
  10019. ' class procedure DoIt; static;',
  10020. ' class property IntA: longint read Fx write Fy;',
  10021. ' class property IntB: longint read GetInt write SetInt;',
  10022. ' end;',
  10023. 'class function trec.getint: longint;',
  10024. 'begin',
  10025. ' result:=fx;',
  10026. 'end;',
  10027. 'class procedure trec.setint(value: longint);',
  10028. 'begin',
  10029. 'end;',
  10030. 'class procedure trec.doit;',
  10031. 'begin',
  10032. ' IntA:=IntA+1;',
  10033. ' IntB:=IntB+1;',
  10034. 'end;',
  10035. 'var r: trec;',
  10036. 'begin',
  10037. ' trec.inta:=trec.inta+1;',
  10038. ' if trec.intb=2 then;',
  10039. ' trec.intb:=trec.intb+2;',
  10040. ' trec.setint(trec.inta);',
  10041. ' r.inta:=r.inta+1;',
  10042. ' if r.intb=2 then;',
  10043. ' r.intb:=r.intb+2;',
  10044. ' r.setint(r.inta);']);
  10045. ConvertProgram;
  10046. CheckSource('TestAdvRecord_Property_ClassMethod',
  10047. LinesToStr([ // statements
  10048. 'rtl.recNewT($mod, "TRec", function () {',
  10049. ' this.Fx = 0;',
  10050. ' this.Fy = 0;',
  10051. ' this.$eq = function (b) {',
  10052. ' return true;',
  10053. ' };',
  10054. ' this.$assign = function (s) {',
  10055. ' return this;',
  10056. ' };',
  10057. ' this.GetInt = function () {',
  10058. ' var Result = 0;',
  10059. ' Result = this.Fx;',
  10060. ' return Result;',
  10061. ' };',
  10062. ' this.SetInt = function (Value) {',
  10063. ' };',
  10064. ' this.DoIt = function () {',
  10065. ' $mod.TRec.Fy = this.Fx + 1;',
  10066. ' this.SetInt(this.GetInt() + 1);',
  10067. ' };',
  10068. '}, true);',
  10069. 'this.r = $mod.TRec.$new();',
  10070. '']),
  10071. LinesToStr([ // $mod.$main
  10072. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  10073. 'if ($mod.TRec.GetInt() === 2) ;',
  10074. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  10075. '$mod.TRec.SetInt($mod.TRec.Fx);',
  10076. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  10077. 'if ($mod.r.$record.GetInt() === 2) ;',
  10078. '$mod.r.$record.SetInt($mod.r.$record.GetInt() + 2);',
  10079. '$mod.r.$record.SetInt($mod.r.Fx);',
  10080. '']));
  10081. end;
  10082. procedure TTestModule.TestAdvRecord_Const;
  10083. begin
  10084. StartProgram(false);
  10085. Add([
  10086. '{$modeswitch AdvancedRecords}',
  10087. 'type',
  10088. ' TArrInt = array[3..4] of longint;',
  10089. ' TPoint = record',
  10090. ' x,y: longint;',
  10091. ' class var Count: nativeint;',
  10092. ' end;',
  10093. ' TRec = record',
  10094. ' i: longint;',
  10095. ' a: array of longint;',
  10096. ' s: array[1..2] of longint;',
  10097. ' m: array[1..2,3..4] of longint;',
  10098. ' p: TPoint;',
  10099. ' end;',
  10100. ' TPoints = array of TPoint;',
  10101. 'const',
  10102. ' r: TRec = (',
  10103. ' i:1;',
  10104. ' a:(2,3);',
  10105. ' s:(4,5);',
  10106. ' m:( (11,12), (13,14) );',
  10107. ' p: (x:21)',
  10108. ' );',
  10109. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  10110. 'begin']);
  10111. ConvertProgram;
  10112. CheckSource('TestAdvRecord_Const',
  10113. LinesToStr([ // statements
  10114. 'rtl.recNewT($mod, "TPoint", function () {',
  10115. ' this.x = 0;',
  10116. ' this.y = 0;',
  10117. ' this.Count = 0;',
  10118. ' this.$eq = function (b) {',
  10119. ' return (this.x === b.x) && (this.y === b.y);',
  10120. ' };',
  10121. ' this.$assign = function (s) {',
  10122. ' this.x = s.x;',
  10123. ' this.y = s.y;',
  10124. ' return this;',
  10125. ' };',
  10126. '}, true);',
  10127. 'rtl.recNewT($mod, "TRec", function () {',
  10128. ' this.i = 0;',
  10129. ' this.a = [];',
  10130. ' this.$new = function () {',
  10131. ' var r = Object.create(this);',
  10132. ' r.s = rtl.arraySetLength(null, 0, 2);',
  10133. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  10134. ' r.p = $mod.TPoint.$new();',
  10135. ' return r;',
  10136. ' };',
  10137. ' this.$eq = function (b) {',
  10138. ' 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);',
  10139. ' };',
  10140. ' this.$assign = function (s) {',
  10141. ' this.i = s.i;',
  10142. ' this.a = s.a;',
  10143. ' this.s = s.s.slice(0);',
  10144. ' this.m = s.m.slice(0);',
  10145. ' this.p.$assign(s.p);',
  10146. ' return this;',
  10147. ' };',
  10148. '});',
  10149. 'this.r = $mod.TRec.$clone({',
  10150. ' i: 1,',
  10151. ' a: [2, 3],',
  10152. ' s: [4, 5],',
  10153. ' m: [[11, 12], [13, 14]],',
  10154. ' p: $mod.TPoint.$clone({',
  10155. ' x: 21,',
  10156. ' y: 0',
  10157. ' })',
  10158. '});',
  10159. 'this.p = [$mod.TPoint.$clone({',
  10160. ' x: 1,',
  10161. ' y: 2',
  10162. '}), $mod.TPoint.$clone({',
  10163. ' x: 3,',
  10164. ' y: 4',
  10165. '})];',
  10166. '']),
  10167. LinesToStr([ // $mod.$main
  10168. '']));
  10169. end;
  10170. procedure TTestModule.TestAdvRecord_ExternalField;
  10171. begin
  10172. StartProgram(false);
  10173. Add([
  10174. '{$modeswitch AdvancedRecords}',
  10175. '{$modeswitch externalclass}',
  10176. 'type',
  10177. ' TCar = record',
  10178. ' public',
  10179. ' Intern: longint external name ''$Intern'';',
  10180. ' Intern2: longint external name ''$Intern2'';',
  10181. ' Bracket: longint external name ''["A B"]'';',
  10182. ' procedure DoIt;',
  10183. ' end;',
  10184. 'implementation',
  10185. 'procedure tcar.doit;',
  10186. 'begin',
  10187. ' Intern:=Intern+1;',
  10188. ' Intern2:=Intern2+2;',
  10189. ' Bracket:=Bracket+3;',
  10190. 'end;',
  10191. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  10192. 'begin',
  10193. ' Rec.intern:=Rec.intern+1;',
  10194. ' Rec.intern2:=Rec.intern2+2;',
  10195. ' Rec.Bracket:=Rec.Bracket+3;',
  10196. ' with Rec do begin',
  10197. ' intern:=intern+1;',
  10198. ' intern2:=intern2+2;',
  10199. ' Bracket:=Bracket+3;',
  10200. ' end;']);
  10201. ConvertProgram;
  10202. CheckSource('TestAdvRecord_ExternalField',
  10203. LinesToStr([ // statements
  10204. 'rtl.recNewT($mod, "TCar", function () {',
  10205. ' this.$eq = function (b) {',
  10206. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  10207. ' };',
  10208. ' this.$assign = function (s) {',
  10209. ' this.$Intern = s.$Intern;',
  10210. ' this.$Intern2 = s.$Intern2;',
  10211. ' this["A B"] = s["A B"];',
  10212. ' return this;',
  10213. ' };',
  10214. ' this.DoIt = function () {',
  10215. ' this.$Intern = this.$Intern + 1;',
  10216. ' this.$Intern2 = this.$Intern2 + 2;',
  10217. ' this["A B"] = this["A B"] + 3;',
  10218. ' };',
  10219. '});',
  10220. 'this.Rec = $mod.TCar.$clone({',
  10221. ' $Intern: 11,',
  10222. ' $Intern2: 12,',
  10223. ' "A B": 13',
  10224. '});',
  10225. '']),
  10226. LinesToStr([ // $mod.$main
  10227. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  10228. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  10229. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  10230. 'var $with1 = $mod.Rec;',
  10231. '$with1.$Intern = $with1.$Intern + 1;',
  10232. '$with1.$Intern2 = $with1.$Intern2 + 2;',
  10233. '$with1["A B"] = $with1["A B"] + 3;',
  10234. '']));
  10235. end;
  10236. procedure TTestModule.TestAdvRecord_SubRecord;
  10237. begin
  10238. StartProgram(false);
  10239. Add([
  10240. '{$modeswitch AdvancedRecords}',
  10241. 'type',
  10242. ' TRec = record',
  10243. ' type',
  10244. ' TPoint = record',
  10245. ' x,y: longint;',
  10246. ' class var Count: nativeint;',
  10247. ' procedure DoIt;',
  10248. ' class procedure DoThat; static;',
  10249. ' end;',
  10250. ' var',
  10251. ' i: longint;',
  10252. ' p: TPoint;',
  10253. ' procedure DoSome;',
  10254. ' end;',
  10255. 'const',
  10256. ' r: TRec = (',
  10257. ' i:1;',
  10258. ' p: (x:21;y:22)',
  10259. ' );',
  10260. 'procedure TRec.DoSome;',
  10261. 'begin',
  10262. ' p.x:=p.y+1;',
  10263. ' p.Count:=p.Count+2;',
  10264. 'end;',
  10265. 'procedure TRec.TPoint.DoIt;',
  10266. 'begin',
  10267. ' Count:=Count+3;',
  10268. 'end;',
  10269. 'class procedure TRec.TPoint.DoThat;',
  10270. 'begin',
  10271. ' Count:=Count+4;',
  10272. 'end;',
  10273. 'begin']);
  10274. ConvertProgram;
  10275. CheckSource('TestAdvRecord_SubRecord',
  10276. LinesToStr([ // statements
  10277. 'rtl.recNewT($mod, "TRec", function () {',
  10278. ' rtl.recNewT(this, "TPoint", function () {',
  10279. ' this.x = 0;',
  10280. ' this.y = 0;',
  10281. ' this.Count = 0;',
  10282. ' this.$eq = function (b) {',
  10283. ' return (this.x === b.x) && (this.y === b.y);',
  10284. ' };',
  10285. ' this.$assign = function (s) {',
  10286. ' this.x = s.x;',
  10287. ' this.y = s.y;',
  10288. ' return this;',
  10289. ' };',
  10290. ' this.DoIt = function () {',
  10291. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  10292. ' };',
  10293. ' this.DoThat = function () {',
  10294. ' $mod.TRec.TPoint.Count = this.Count + 4;',
  10295. ' };',
  10296. ' }, true);',
  10297. ' this.i = 0;',
  10298. ' this.$new = function () {',
  10299. ' var r = Object.create(this);',
  10300. ' r.p = this.TPoint.$new();',
  10301. ' return r;',
  10302. ' };',
  10303. ' this.$eq = function (b) {',
  10304. ' return (this.i === b.i) && this.p.$eq(b.p);',
  10305. ' };',
  10306. ' this.$assign = function (s) {',
  10307. ' this.i = s.i;',
  10308. ' this.p.$assign(s.p);',
  10309. ' return this;',
  10310. ' };',
  10311. ' this.DoSome = function () {',
  10312. ' this.p.x = this.p.y + 1;',
  10313. ' this.TPoint.Count = this.p.Count + 2;',
  10314. ' };',
  10315. '}, true);',
  10316. 'this.r = $mod.TRec.$clone({',
  10317. ' i: 1,',
  10318. ' p: $mod.TRec.TPoint.$clone({',
  10319. ' x: 21,',
  10320. ' y: 22',
  10321. ' })',
  10322. '});',
  10323. '']),
  10324. LinesToStr([ // $mod.$main
  10325. '']));
  10326. end;
  10327. procedure TTestModule.TestAdvRecord_SubClass;
  10328. begin
  10329. StartProgram(false);
  10330. Add([
  10331. '{$modeswitch AdvancedRecords}',
  10332. 'type',
  10333. ' TObject = class end;',
  10334. ' TPoint = record',
  10335. ' type',
  10336. ' TBird = class',
  10337. ' procedure DoIt;',
  10338. ' class procedure Glob;',
  10339. ' end;',
  10340. ' procedure DoIt(b: TBird);',
  10341. ' end;',
  10342. 'procedure TPoint.TBird.DoIt;',
  10343. 'begin',
  10344. ' doit;',
  10345. ' self.doit;',
  10346. ' glob;',
  10347. ' self.glob;',
  10348. 'end;',
  10349. 'class procedure TPoint.TBird.Glob;',
  10350. 'begin',
  10351. ' glob;',
  10352. ' self.glob;',
  10353. 'end;',
  10354. 'procedure TPoint.DoIt(b: TBird);',
  10355. 'begin',
  10356. ' b.doit;',
  10357. ' b.glob;',
  10358. ' TBird.glob;',
  10359. 'end;',
  10360. 'begin',
  10361. '']);
  10362. ConvertProgram;
  10363. CheckSource('TestAdvRecord_SubClass',
  10364. LinesToStr([ // statements
  10365. 'rtl.createClass($mod, "TObject", null, function () {',
  10366. ' this.$init = function () {',
  10367. ' };',
  10368. ' this.$final = function () {',
  10369. ' };',
  10370. '});',
  10371. 'rtl.recNewT($mod, "TPoint", function () {',
  10372. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  10373. ' this.DoIt = function () {',
  10374. ' this.DoIt();',
  10375. ' this.DoIt();',
  10376. ' this.$class.Glob();',
  10377. ' this.$class.Glob();',
  10378. ' };',
  10379. ' this.Glob = function () {',
  10380. ' this.Glob();',
  10381. ' this.Glob();',
  10382. ' };',
  10383. ' });',
  10384. ' this.$eq = function (b) {',
  10385. ' return true;',
  10386. ' };',
  10387. ' this.$assign = function (s) {',
  10388. ' return this;',
  10389. ' };',
  10390. ' this.DoIt = function (b) {',
  10391. ' b.DoIt();',
  10392. ' b.$class.Glob();',
  10393. ' this.TBird.Glob();',
  10394. ' };',
  10395. '}, true);',
  10396. '']),
  10397. LinesToStr([ // $mod.$main
  10398. '']));
  10399. end;
  10400. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  10401. begin
  10402. StartProgram(false);
  10403. Add([
  10404. '{$modeswitch AdvancedRecords}',
  10405. 'type',
  10406. ' IUnknown = interface end;',
  10407. ' TPoint = record',
  10408. ' type IBird = interface end;',
  10409. ' end;',
  10410. 'begin',
  10411. '']);
  10412. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] interface inside record',
  10413. nNotYetImplemented);
  10414. ParseProgram;
  10415. end;
  10416. procedure TTestModule.TestAdvRecord_Constructor;
  10417. begin
  10418. StartProgram(false);
  10419. Add([
  10420. '{$modeswitch AdvancedRecords}',
  10421. 'type',
  10422. ' TPoint = record',
  10423. ' x,y: longint;',
  10424. ' constructor Create(ax: longint; ay: longint = -1);',
  10425. ' end;',
  10426. 'constructor tpoint.create(ax,ay: longint);',
  10427. 'begin',
  10428. ' x:=ax;',
  10429. ' self.y:=ay;',
  10430. 'end;',
  10431. 'var r: TPoint;',
  10432. 'begin',
  10433. ' r:=TPoint.Create(1,2);',
  10434. ' r.Create(3);',
  10435. ' r:=r.Create(4);',
  10436. '']);
  10437. ConvertProgram;
  10438. CheckSource('TestAdvRecord_Constructor',
  10439. LinesToStr([ // statements
  10440. 'rtl.recNewT($mod, "TPoint", function () {',
  10441. ' this.x = 0;',
  10442. ' this.y = 0;',
  10443. ' this.$eq = function (b) {',
  10444. ' return (this.x === b.x) && (this.y === b.y);',
  10445. ' };',
  10446. ' this.$assign = function (s) {',
  10447. ' this.x = s.x;',
  10448. ' this.y = s.y;',
  10449. ' return this;',
  10450. ' };',
  10451. ' this.Create = function (ax, ay) {',
  10452. ' this.x = ax;',
  10453. ' this.y = ay;',
  10454. ' return this;',
  10455. ' };',
  10456. '}, true);',
  10457. 'this.r = $mod.TPoint.$new();',
  10458. '']),
  10459. LinesToStr([ // $mod.$main
  10460. '$mod.r.$assign($mod.TPoint.$create("Create", [1, 2]));',
  10461. '$mod.r.Create(3, -1);',
  10462. '$mod.r.$assign($mod.r.Create(4, -1));',
  10463. '']));
  10464. end;
  10465. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  10466. begin
  10467. StartProgram(false);
  10468. Add(['type',
  10469. ' TObject = class',
  10470. ' public',
  10471. ' constructor Create;',
  10472. ' destructor Destroy;',
  10473. ' end;',
  10474. ' TBird = TObject;',
  10475. 'constructor tobject.create;',
  10476. 'begin end;',
  10477. 'destructor tobject.destroy;',
  10478. 'begin end;',
  10479. 'var Obj: tobject;',
  10480. 'begin',
  10481. ' obj:=tobject.create;',
  10482. ' obj:=tobject.create();',
  10483. ' obj:=tbird.create;',
  10484. ' obj:=tbird.create();',
  10485. ' obj:=obj.create();',
  10486. ' obj.destroy;',
  10487. '']);
  10488. ConvertProgram;
  10489. CheckSource('TestClass_TObjectDefaultConstructor',
  10490. LinesToStr([ // statements
  10491. 'rtl.createClass($mod,"TObject",null,function(){',
  10492. ' this.$init = function () {',
  10493. ' };',
  10494. ' this.$final = function () {',
  10495. ' };',
  10496. ' this.Create = function(){',
  10497. ' return this;',
  10498. ' };',
  10499. ' this.Destroy = function(){',
  10500. ' };',
  10501. '});',
  10502. 'this.Obj = null;'
  10503. ]),
  10504. LinesToStr([ // $mod.$main
  10505. '$mod.Obj = $mod.TObject.$create("Create");',
  10506. '$mod.Obj = $mod.TObject.$create("Create");',
  10507. '$mod.Obj = $mod.TObject.$create("Create");',
  10508. '$mod.Obj = $mod.TObject.$create("Create");',
  10509. '$mod.Obj = $mod.Obj.Create();',
  10510. '$mod.Obj.$destroy("Destroy");',
  10511. '']));
  10512. end;
  10513. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  10514. begin
  10515. StartProgram(false);
  10516. Add('type');
  10517. Add(' TObject = class');
  10518. Add(' public');
  10519. Add(' constructor Create(Par: longint);');
  10520. Add(' end;');
  10521. Add('constructor tobject.create(par: longint);');
  10522. Add('begin end;');
  10523. Add('var Obj: tobject;');
  10524. Add('begin');
  10525. Add(' obj:=tobject.create(3);');
  10526. ConvertProgram;
  10527. CheckSource('TestClass_TObjectConstructorWithParams',
  10528. LinesToStr([ // statements
  10529. 'rtl.createClass($mod,"TObject",null,function(){',
  10530. ' this.$init = function () {',
  10531. ' };',
  10532. ' this.$final = function () {',
  10533. ' };',
  10534. ' this.Create = function(Par){',
  10535. ' return this;',
  10536. ' };',
  10537. '});',
  10538. 'this.Obj = null;'
  10539. ]),
  10540. LinesToStr([ // $mod.$main
  10541. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  10542. ]));
  10543. end;
  10544. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  10545. begin
  10546. StartProgram(false);
  10547. Add('type');
  10548. Add(' TObject = class');
  10549. Add(' public');
  10550. Add(' constructor Create;');
  10551. Add(' end;');
  10552. Add(' TTest = class(TObject)');
  10553. Add(' public');
  10554. Add(' constructor Create(const Par: longint = 1);');
  10555. Add(' end;');
  10556. Add('constructor tobject.create;');
  10557. Add('begin end;');
  10558. Add('constructor ttest.create(const par: longint);');
  10559. Add('begin end;');
  10560. Add('var t: ttest;');
  10561. Add('begin');
  10562. Add(' t:=ttest.create;');
  10563. Add(' t:=ttest.create(2);');
  10564. ConvertProgram;
  10565. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  10566. LinesToStr([ // statements
  10567. 'rtl.createClass($mod,"TObject",null,function(){',
  10568. ' this.$init = function () {',
  10569. ' };',
  10570. ' this.$final = function () {',
  10571. ' };',
  10572. ' this.Create = function(){',
  10573. ' return this;',
  10574. ' };',
  10575. '});',
  10576. 'rtl.createClass($mod, "TTest", $mod.TObject, function () {',
  10577. ' this.Create$1 = function (Par) {',
  10578. ' return this;',
  10579. ' };',
  10580. '});',
  10581. 'this.t = null;'
  10582. ]),
  10583. LinesToStr([ // $mod.$main
  10584. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  10585. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  10586. ]));
  10587. end;
  10588. procedure TTestModule.TestClass_Var;
  10589. begin
  10590. StartProgram(false);
  10591. Add([
  10592. 'type',
  10593. ' TObject = class',
  10594. ' public',
  10595. ' vI: longint;',
  10596. ' constructor Create(Par: longint);',
  10597. ' end;',
  10598. 'constructor tobject.create(par: longint);',
  10599. 'begin',
  10600. ' vi:=par+3',
  10601. 'end;',
  10602. 'var Obj: tobject;',
  10603. 'begin',
  10604. ' obj:=tobject.create(4);',
  10605. ' obj.vi:=obj.VI+5;']);
  10606. ConvertProgram;
  10607. CheckSource('TestClass_Var',
  10608. LinesToStr([ // statements
  10609. 'rtl.createClass($mod,"TObject",null,function(){',
  10610. ' this.$init = function () {',
  10611. ' this.vI = 0;',
  10612. ' };',
  10613. ' this.$final = function () {',
  10614. ' };',
  10615. ' this.Create = function(Par){',
  10616. ' this.vI = Par+3;',
  10617. ' return this;',
  10618. ' };',
  10619. '});',
  10620. 'this.Obj = null;'
  10621. ]),
  10622. LinesToStr([ // $mod.$main
  10623. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  10624. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  10625. ]));
  10626. end;
  10627. procedure TTestModule.TestClass_Method;
  10628. begin
  10629. StartProgram(false);
  10630. Add('type');
  10631. Add(' TObject = class');
  10632. Add(' public');
  10633. Add(' vI: longint;');
  10634. Add(' Sub: TObject;');
  10635. Add(' constructor Create;');
  10636. Add(' function GetIt(Par: longint): tobject;');
  10637. Add(' end;');
  10638. Add('constructor tobject.create; begin end;');
  10639. Add('function tobject.getit(par: longint): tobject;');
  10640. Add('begin');
  10641. Add(' Self.vi:=par+3;');
  10642. Add(' Result:=self.sub;');
  10643. Add('end;');
  10644. Add('var Obj: tobject;');
  10645. Add('begin');
  10646. Add(' obj:=tobject.create;');
  10647. Add(' obj.getit(4);');
  10648. Add(' obj.sub.sub:=nil;');
  10649. Add(' obj.sub.getit(5);');
  10650. Add(' obj.sub.getit(6).SUB:=nil;');
  10651. Add(' obj.sub.getit(7).GETIT(8);');
  10652. Add(' obj.sub.getit(9).SuB.getit(10);');
  10653. ConvertProgram;
  10654. CheckSource('TestClass_Method',
  10655. LinesToStr([ // statements
  10656. 'rtl.createClass($mod,"TObject",null,function(){',
  10657. ' this.$init = function () {',
  10658. ' this.vI = 0;',
  10659. ' this.Sub = null;',
  10660. ' };',
  10661. ' this.$final = function () {',
  10662. ' this.Sub = undefined;',
  10663. ' };',
  10664. ' this.Create = function(){',
  10665. ' return this;',
  10666. ' };',
  10667. ' this.GetIt = function(Par){',
  10668. ' var Result = null;',
  10669. ' this.vI = Par + 3;',
  10670. ' Result = this.Sub;',
  10671. ' return Result;',
  10672. ' };',
  10673. '});',
  10674. 'this.Obj = null;'
  10675. ]),
  10676. LinesToStr([ // $mod.$main
  10677. '$mod.Obj = $mod.TObject.$create("Create");',
  10678. '$mod.Obj.GetIt(4);',
  10679. '$mod.Obj.Sub.Sub=null;',
  10680. '$mod.Obj.Sub.GetIt(5);',
  10681. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  10682. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  10683. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  10684. ]));
  10685. end;
  10686. procedure TTestModule.TestClass_Implementation;
  10687. begin
  10688. StartUnit(false);
  10689. Add([
  10690. 'interface',
  10691. 'type',
  10692. ' TObject = class',
  10693. ' constructor Create;',
  10694. ' end;',
  10695. 'implementation',
  10696. 'type',
  10697. ' TIntClass = class',
  10698. ' constructor Create; reintroduce;',
  10699. ' class procedure DoGlob;',
  10700. ' end;',
  10701. 'constructor tintclass.create;',
  10702. 'begin',
  10703. ' inherited;',
  10704. ' inherited create;',
  10705. ' doglob;',
  10706. 'end;',
  10707. 'class procedure tintclass.doglob;',
  10708. 'begin',
  10709. 'end;',
  10710. 'constructor tobject.create;',
  10711. 'var',
  10712. ' iC: tintclass;',
  10713. 'begin',
  10714. ' ic:=tintclass.create;',
  10715. ' tintclass.doglob;',
  10716. ' ic.doglob;',
  10717. 'end;',
  10718. 'initialization',
  10719. ' tintclass.doglob;',
  10720. '']);
  10721. ConvertUnit;
  10722. CheckSource('TestClass_Implementation',
  10723. LinesToStr([ // statements
  10724. 'var $impl = $mod.$impl;',
  10725. 'rtl.createClass($mod, "TObject", null, function () {',
  10726. ' this.$init = function () {',
  10727. ' };',
  10728. ' this.$final = function () {',
  10729. ' };',
  10730. ' this.Create = function () {',
  10731. ' var iC = null;',
  10732. ' iC = $impl.TIntClass.$create("Create$1");',
  10733. ' $impl.TIntClass.DoGlob();',
  10734. ' iC.$class.DoGlob();',
  10735. ' return this;',
  10736. ' };',
  10737. '});',
  10738. '']),
  10739. LinesToStr([ // $mod.$main
  10740. '$impl.TIntClass.DoGlob();',
  10741. '']),
  10742. LinesToStr([
  10743. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  10744. ' this.Create$1 = function () {',
  10745. ' $mod.TObject.Create.apply(this, arguments);',
  10746. ' $mod.TObject.Create.call(this);',
  10747. ' this.$class.DoGlob();',
  10748. ' return this;',
  10749. ' };',
  10750. ' this.DoGlob = function () {',
  10751. ' };',
  10752. '});',
  10753. '']));
  10754. end;
  10755. procedure TTestModule.TestClass_Inheritance;
  10756. begin
  10757. StartProgram(false);
  10758. Add('type');
  10759. Add(' TObject = class');
  10760. Add(' public');
  10761. Add(' constructor Create;');
  10762. Add(' end;');
  10763. Add(' TClassA = class');
  10764. Add(' end;');
  10765. Add(' TClassB = class(TObject)');
  10766. Add(' procedure ProcB;');
  10767. Add(' end;');
  10768. Add('constructor tobject.create; begin end;');
  10769. Add('procedure tclassb.procb; begin end;');
  10770. Add('var');
  10771. Add(' oO: TObject;');
  10772. Add(' oA: TClassA;');
  10773. Add(' oB: TClassB;');
  10774. Add('begin');
  10775. Add(' oO:=tobject.Create;');
  10776. Add(' oA:=tclassa.Create;');
  10777. Add(' ob:=tclassb.Create;');
  10778. Add(' if oo is tclassa then ;');
  10779. Add(' ob:=oo as tclassb;');
  10780. Add(' (oo as tclassb).procb;');
  10781. ConvertProgram;
  10782. CheckSource('TestClass_Inheritance',
  10783. LinesToStr([ // statements
  10784. 'rtl.createClass($mod,"TObject",null,function(){',
  10785. ' this.$init = function () {',
  10786. ' };',
  10787. ' this.$final = function () {',
  10788. ' };',
  10789. ' this.Create = function () {',
  10790. ' return this;',
  10791. ' };',
  10792. '});',
  10793. 'rtl.createClass($mod,"TClassA",$mod.TObject,function(){',
  10794. '});',
  10795. 'rtl.createClass($mod,"TClassB",$mod.TObject,function(){',
  10796. ' this.ProcB = function () {',
  10797. ' };',
  10798. '});',
  10799. 'this.oO = null;',
  10800. 'this.oA = null;',
  10801. 'this.oB = null;'
  10802. ]),
  10803. LinesToStr([ // $mod.$main
  10804. '$mod.oO = $mod.TObject.$create("Create");',
  10805. '$mod.oA = $mod.TClassA.$create("Create");',
  10806. '$mod.oB = $mod.TClassB.$create("Create");',
  10807. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  10808. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  10809. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  10810. ]));
  10811. end;
  10812. procedure TTestModule.TestClass_TypeAlias;
  10813. begin
  10814. StartProgram(false);
  10815. Add([
  10816. '{$interfaces corba}',
  10817. 'type',
  10818. ' IObject = interface',
  10819. ' end;',
  10820. ' IBird = type IObject;',
  10821. ' TObject = class',
  10822. ' end;',
  10823. ' TBird = type TObject;',
  10824. 'var',
  10825. ' oObj: TObject;',
  10826. ' oBird: TBird;',
  10827. ' IntfObj: IObject;',
  10828. ' IntfBird: IBird;',
  10829. 'begin',
  10830. ' oObj:=oBird;',
  10831. '']);
  10832. ConvertProgram;
  10833. CheckSource('TestClass_TypeAlias',
  10834. LinesToStr([ // statements
  10835. 'rtl.createInterface($mod, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  10836. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], $mod.IObject);',
  10837. 'rtl.createClass($mod, "TObject", null, function () {',
  10838. ' this.$init = function () {',
  10839. ' };',
  10840. ' this.$final = function () {',
  10841. ' };',
  10842. '});',
  10843. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  10844. '});',
  10845. 'this.oObj = null;',
  10846. 'this.oBird = null;',
  10847. 'this.IntfObj = null;',
  10848. 'this.IntfBird = null;',
  10849. '']),
  10850. LinesToStr([ // $mod.$main
  10851. '$mod.oObj = $mod.oBird;',
  10852. '']));
  10853. end;
  10854. procedure TTestModule.TestClass_AbstractMethod;
  10855. begin
  10856. StartProgram(false);
  10857. Add('type');
  10858. Add(' TObject = class');
  10859. Add(' public');
  10860. Add(' procedure DoIt; virtual; abstract;');
  10861. Add(' end;');
  10862. Add('begin');
  10863. ConvertProgram;
  10864. CheckSource('TestClass_AbstractMethod',
  10865. LinesToStr([ // statements
  10866. 'rtl.createClass($mod,"TObject",null,function(){',
  10867. ' this.$init = function () {',
  10868. ' };',
  10869. ' this.$final = function () {',
  10870. ' };',
  10871. '});'
  10872. ]),
  10873. LinesToStr([ // this.$main
  10874. ''
  10875. ]));
  10876. end;
  10877. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  10878. begin
  10879. StartProgram(false);
  10880. Add([
  10881. 'type',
  10882. ' TObject = class',
  10883. ' procedure DoAbstract; virtual; abstract;',
  10884. ' procedure DoVirtual; virtual;',
  10885. ' procedure DoIt;',
  10886. ' end;',
  10887. ' TA = class',
  10888. ' procedure doabstract; override;',
  10889. ' procedure dovirtual; override;',
  10890. ' procedure DoSome;',
  10891. ' end;',
  10892. 'procedure tobject.dovirtual;',
  10893. 'begin',
  10894. ' inherited; // call non existing ancestor -> ignore silently',
  10895. 'end;',
  10896. 'procedure tobject.doit;',
  10897. 'begin',
  10898. 'end;',
  10899. 'procedure ta.doabstract;',
  10900. 'begin',
  10901. ' inherited dovirtual; // call TObject.DoVirtual',
  10902. 'end;',
  10903. 'procedure ta.dovirtual;',
  10904. 'begin',
  10905. ' inherited; // call TObject.DoVirtual',
  10906. ' inherited dovirtual; // call TObject.DoVirtual',
  10907. ' inherited dovirtual(); // call TObject.DoVirtual',
  10908. ' doit;',
  10909. ' doit();',
  10910. 'end;',
  10911. 'procedure ta.dosome;',
  10912. 'begin',
  10913. ' inherited; // call non existing ancestor method -> silently ignore',
  10914. 'end;',
  10915. 'begin']);
  10916. ConvertProgram;
  10917. CheckSource('TestClass_CallInherited_ProcNoParams',
  10918. LinesToStr([ // statements
  10919. 'rtl.createClass($mod,"TObject",null,function(){',
  10920. ' this.$init = function () {',
  10921. ' };',
  10922. ' this.$final = function () {',
  10923. ' };',
  10924. ' this.DoVirtual = function () {',
  10925. ' };',
  10926. ' this.DoIt = function () {',
  10927. ' };',
  10928. '});',
  10929. 'rtl.createClass($mod, "TA", $mod.TObject, function () {',
  10930. ' this.DoAbstract = function () {',
  10931. ' $mod.TObject.DoVirtual.call(this);',
  10932. ' };',
  10933. ' this.DoVirtual = function () {',
  10934. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  10935. ' $mod.TObject.DoVirtual.call(this);',
  10936. ' $mod.TObject.DoVirtual.call(this);',
  10937. ' this.DoIt();',
  10938. ' this.DoIt();',
  10939. ' };',
  10940. ' this.DoSome = function () {',
  10941. ' };',
  10942. '});'
  10943. ]),
  10944. LinesToStr([ // this.$main
  10945. ''
  10946. ]));
  10947. end;
  10948. procedure TTestModule.TestClass_CallInherited_WithParams;
  10949. begin
  10950. StartProgram(false);
  10951. Add([
  10952. 'type',
  10953. ' TObject = class',
  10954. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  10955. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  10956. ' procedure DoIt(pA: longint; pB: longint = 0);',
  10957. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  10958. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  10959. ' end;',
  10960. ' TClassA = class',
  10961. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  10962. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  10963. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  10964. ' end;',
  10965. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  10966. 'begin',
  10967. 'end;',
  10968. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  10969. 'begin',
  10970. 'end;',
  10971. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  10972. 'begin',
  10973. 'end;',
  10974. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  10975. 'begin',
  10976. 'end;',
  10977. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  10978. 'begin',
  10979. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  10980. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  10981. 'end;',
  10982. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  10983. 'begin',
  10984. ' inherited; // call TObject.DoVirtual(pA,pB)',
  10985. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  10986. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  10987. ' doit(pa,pb);',
  10988. ' doit(pa);',
  10989. ' doit2(pa);',
  10990. ' doit2;',
  10991. 'end;',
  10992. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  10993. 'begin',
  10994. ' pa:=inherited;',
  10995. 'end;',
  10996. 'begin']);
  10997. ConvertProgram;
  10998. CheckSource('TestClass_CallInherited_WithParams',
  10999. LinesToStr([ // statements
  11000. 'rtl.createClass($mod,"TObject",null,function(){',
  11001. ' this.$init = function () {',
  11002. ' };',
  11003. ' this.$final = function () {',
  11004. ' };',
  11005. ' this.DoVirtual = function (pA,pB) {',
  11006. ' };',
  11007. ' this.DoIt = function (pA,pB) {',
  11008. ' };',
  11009. ' this.DoIt2 = function (pA,pB) {',
  11010. ' };',
  11011. ' this.GetIt = function (pA, pB) {',
  11012. ' var Result = 0;',
  11013. ' return Result;',
  11014. ' };',
  11015. '});',
  11016. 'rtl.createClass($mod, "TClassA", $mod.TObject, function () {',
  11017. ' this.DoAbstract = function (pA,pB) {',
  11018. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  11019. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  11020. ' };',
  11021. ' this.DoVirtual = function (pA,pB) {',
  11022. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  11023. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  11024. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  11025. ' this.DoIt(pA,pB);',
  11026. ' this.DoIt(pA,0);',
  11027. ' this.DoIt2(pA,2);',
  11028. ' this.DoIt2(1,2);',
  11029. ' };',
  11030. ' this.GetIt$1 = function (pA, pB) {',
  11031. ' var Result = 0;',
  11032. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  11033. ' return Result;',
  11034. ' };',
  11035. '});'
  11036. ]),
  11037. LinesToStr([ // this.$main
  11038. ''
  11039. ]));
  11040. end;
  11041. procedure TTestModule.TestClasS_CallInheritedConstructor;
  11042. begin
  11043. StartProgram(false);
  11044. Add('type');
  11045. Add(' TObject = class');
  11046. Add(' constructor Create; virtual;');
  11047. Add(' constructor CreateWithB(b: boolean);');
  11048. Add(' end;');
  11049. Add(' TA = class');
  11050. Add(' constructor Create; override;');
  11051. Add(' constructor CreateWithC(c: char);');
  11052. Add(' procedure DoIt;');
  11053. Add(' class function DoSome: TObject;');
  11054. Add(' end;');
  11055. Add('constructor tobject.create;');
  11056. Add('begin');
  11057. Add(' inherited; // call non existing ancestor -> ignore silently');
  11058. Add('end;');
  11059. Add('constructor tobject.createwithb(b: boolean);');
  11060. Add('begin');
  11061. Add(' inherited; // call non existing ancestor -> ignore silently');
  11062. Add(' create; // normal call');
  11063. Add('end;');
  11064. Add('constructor ta.create;');
  11065. Add('begin');
  11066. Add(' inherited; // normal call TObject.Create');
  11067. Add(' inherited create; // normal call TObject.Create');
  11068. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  11069. Add('end;');
  11070. Add('constructor ta.createwithc(c: char);');
  11071. Add('begin');
  11072. Add(' inherited create; // call TObject.Create');
  11073. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  11074. Add(' doit;');
  11075. Add(' doit();');
  11076. Add(' dosome;');
  11077. Add('end;');
  11078. Add('procedure ta.doit;');
  11079. Add('begin');
  11080. Add(' create; // normal call');
  11081. Add(' createwithb(false); // normal call');
  11082. Add(' createwithc(''c''); // normal call');
  11083. Add('end;');
  11084. Add('class function ta.dosome: TObject;');
  11085. Add('begin');
  11086. Add(' Result:=create; // constructor');
  11087. Add(' Result:=createwithb(true); // constructor');
  11088. Add(' Result:=createwithc(''c''); // constructor');
  11089. Add('end;');
  11090. Add('begin');
  11091. ConvertProgram;
  11092. CheckSource('TestClass_CallInheritedConstructor',
  11093. LinesToStr([ // statements
  11094. 'rtl.createClass($mod,"TObject",null,function(){',
  11095. ' this.$init = function () {',
  11096. ' };',
  11097. ' this.$final = function () {',
  11098. ' };',
  11099. ' this.Create = function () {',
  11100. ' return this;',
  11101. ' };',
  11102. ' this.CreateWithB = function (b) {',
  11103. ' this.Create();',
  11104. ' return this;',
  11105. ' };',
  11106. '});',
  11107. 'rtl.createClass($mod, "TA", $mod.TObject, function () {',
  11108. ' this.Create = function () {',
  11109. ' $mod.TObject.Create.apply(this, arguments);',
  11110. ' $mod.TObject.Create.call(this);',
  11111. ' $mod.TObject.CreateWithB.call(this, false);',
  11112. ' return this;',
  11113. ' };',
  11114. ' this.CreateWithC = function (c) {',
  11115. ' $mod.TObject.Create.call(this);',
  11116. ' $mod.TObject.CreateWithB.call(this, true);',
  11117. ' this.DoIt();',
  11118. ' this.DoIt();',
  11119. ' this.$class.DoSome();',
  11120. ' return this;',
  11121. ' };',
  11122. ' this.DoIt = function () {',
  11123. ' this.Create();',
  11124. ' this.CreateWithB(false);',
  11125. ' this.CreateWithC("c");',
  11126. ' };',
  11127. ' this.DoSome = function () {',
  11128. ' var Result = null;',
  11129. ' Result = this.$create("Create");',
  11130. ' Result = this.$create("CreateWithB", [true]);',
  11131. ' Result = this.$create("CreateWithC", ["c"]);',
  11132. ' return Result;',
  11133. ' };',
  11134. '});'
  11135. ]),
  11136. LinesToStr([ // this.$main
  11137. ''
  11138. ]));
  11139. end;
  11140. procedure TTestModule.TestClass_ClassVar_Assign;
  11141. begin
  11142. StartProgram(false);
  11143. Add([
  11144. 'type',
  11145. ' TObject = class',
  11146. ' public',
  11147. ' class var vI: longint;',
  11148. ' class var Sub: TObject;',
  11149. ' constructor Create;',
  11150. ' class function GetIt(Par: longint): tobject;',
  11151. ' end;',
  11152. 'constructor tobject.create;',
  11153. 'begin',
  11154. ' vi:=vi+1;',
  11155. ' Self.vi:=Self.vi+1;',
  11156. ' inc(vi);',
  11157. 'end;',
  11158. 'class function tobject.getit(par: longint): tobject;',
  11159. 'begin',
  11160. ' vi:=vi+par;',
  11161. ' Self.vi:=Self.vi+par;',
  11162. ' inc(vi);',
  11163. ' Result:=self.sub;',
  11164. 'end;',
  11165. 'var Obj: tobject;',
  11166. 'begin',
  11167. ' obj:=tobject.create;',
  11168. ' tobject.vi:=3;',
  11169. ' if tobject.vi=4 then ;',
  11170. ' tobject.sub:=nil;',
  11171. ' obj.sub:=nil;',
  11172. ' obj.sub.sub:=nil;']);
  11173. ConvertProgram;
  11174. CheckSource('TestClass_ClassVar_Assign',
  11175. LinesToStr([ // statements
  11176. 'rtl.createClass($mod,"TObject",null,function(){',
  11177. ' this.vI = 0;',
  11178. ' this.Sub = null;',
  11179. ' this.$init = function () {',
  11180. ' };',
  11181. ' this.$final = function () {',
  11182. ' };',
  11183. ' this.Create = function(){',
  11184. ' $mod.TObject.vI = this.vI+1;',
  11185. ' $mod.TObject.vI = this.vI+1;',
  11186. ' $mod.TObject.vI += 1;',
  11187. ' return this;',
  11188. ' };',
  11189. ' this.GetIt = function(Par){',
  11190. ' var Result = null;',
  11191. ' $mod.TObject.vI = this.vI + Par;',
  11192. ' $mod.TObject.vI = this.vI + Par;',
  11193. ' $mod.TObject.vI += 1;',
  11194. ' Result = this.Sub;',
  11195. ' return Result;',
  11196. ' };',
  11197. '});',
  11198. 'this.Obj = null;'
  11199. ]),
  11200. LinesToStr([ // $mod.$main
  11201. '$mod.Obj = $mod.TObject.$create("Create");',
  11202. '$mod.TObject.vI = 3;',
  11203. 'if ($mod.TObject.vI === 4);',
  11204. '$mod.TObject.Sub=null;',
  11205. '$mod.TObject.Sub=null;',
  11206. '$mod.TObject.Sub=null;',
  11207. '']));
  11208. end;
  11209. procedure TTestModule.TestClass_CallClassMethod;
  11210. begin
  11211. StartProgram(false);
  11212. Add('type');
  11213. Add(' TObject = class');
  11214. Add(' public');
  11215. Add(' class var vI: longint;');
  11216. Add(' class var Sub: TObject;');
  11217. Add(' constructor Create;');
  11218. Add(' function GetMore(Par: longint): longint;');
  11219. Add(' class function GetIt(Par: longint): tobject;');
  11220. Add(' end;');
  11221. Add('constructor tobject.create;');
  11222. Add('begin');
  11223. Add(' sub:=getit(3);');
  11224. Add(' vi:=getmore(4);');
  11225. Add(' sub:=Self.getit(5);');
  11226. Add(' vi:=Self.getmore(6);');
  11227. Add('end;');
  11228. Add('function tobject.getmore(par: longint): longint;');
  11229. Add('begin');
  11230. Add(' sub:=getit(11);');
  11231. Add(' vi:=getmore(12);');
  11232. Add(' sub:=self.getit(13);');
  11233. Add(' vi:=self.getmore(14);');
  11234. Add('end;');
  11235. Add('class function tobject.getit(par: longint): tobject;');
  11236. Add('begin');
  11237. Add(' sub:=getit(21);');
  11238. Add(' vi:=sub.getmore(22);');
  11239. Add(' sub:=self.getit(23);');
  11240. Add(' vi:=self.sub.getmore(24);');
  11241. Add('end;');
  11242. Add('var Obj: tobject;');
  11243. Add('begin');
  11244. Add(' obj:=tobject.create;');
  11245. Add(' tobject.getit(5);');
  11246. Add(' obj.getit(6);');
  11247. Add(' obj.sub.getit(7);');
  11248. Add(' obj.sub.getit(8).SUB:=nil;');
  11249. Add(' obj.sub.getit(9).GETIT(10);');
  11250. Add(' obj.sub.getit(11).SuB.getit(12);');
  11251. ConvertProgram;
  11252. CheckSource('TestClass_CallClassMethod',
  11253. LinesToStr([ // statements
  11254. 'rtl.createClass($mod,"TObject",null,function(){',
  11255. ' this.vI = 0;',
  11256. ' this.Sub = null;',
  11257. ' this.$init = function () {',
  11258. ' };',
  11259. ' this.$final = function () {',
  11260. ' };',
  11261. ' this.Create = function(){',
  11262. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  11263. ' $mod.TObject.vI = this.GetMore(4);',
  11264. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  11265. ' $mod.TObject.vI = this.GetMore(6);',
  11266. ' return this;',
  11267. ' };',
  11268. ' this.GetMore = function(Par){',
  11269. ' var Result = 0;',
  11270. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  11271. ' $mod.TObject.vI = this.GetMore(12);',
  11272. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  11273. ' $mod.TObject.vI = this.GetMore(14);',
  11274. ' return Result;',
  11275. ' };',
  11276. ' this.GetIt = function(Par){',
  11277. ' var Result = null;',
  11278. ' $mod.TObject.Sub = this.GetIt(21);',
  11279. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  11280. ' $mod.TObject.Sub = this.GetIt(23);',
  11281. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  11282. ' return Result;',
  11283. ' };',
  11284. '});',
  11285. 'this.Obj = null;'
  11286. ]),
  11287. LinesToStr([ // $mod.$main
  11288. '$mod.Obj = $mod.TObject.$create("Create");',
  11289. '$mod.TObject.GetIt(5);',
  11290. '$mod.Obj.$class.GetIt(6);',
  11291. '$mod.Obj.Sub.$class.GetIt(7);',
  11292. '$mod.TObject.Sub=null;',
  11293. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  11294. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  11295. '']));
  11296. end;
  11297. procedure TTestModule.TestClass_Property;
  11298. begin
  11299. StartProgram(false);
  11300. Add('type');
  11301. Add(' TObject = class');
  11302. Add(' Fx: longint;');
  11303. Add(' Fy: longint;');
  11304. Add(' function GetInt: longint;');
  11305. Add(' procedure SetInt(Value: longint);');
  11306. Add(' procedure DoIt;');
  11307. Add(' property IntA: longint read Fx write Fy;');
  11308. Add(' property IntB: longint read GetInt write SetInt;');
  11309. Add(' end;');
  11310. Add('function tobject.getint: longint;');
  11311. Add('begin');
  11312. Add(' result:=fx;');
  11313. Add('end;');
  11314. Add('procedure tobject.setint(value: longint);');
  11315. Add('begin');
  11316. Add(' if value=fy then exit;');
  11317. Add(' fy:=value;');
  11318. Add('end;');
  11319. Add('procedure tobject.doit;');
  11320. Add('begin');
  11321. Add(' IntA:=IntA+1;');
  11322. Add(' Self.IntA:=Self.IntA+1;');
  11323. Add(' IntB:=IntB+1;');
  11324. Add(' Self.IntB:=Self.IntB+1;');
  11325. Add('end;');
  11326. Add('var Obj: tobject;');
  11327. Add('begin');
  11328. Add(' obj.inta:=obj.inta+1;');
  11329. Add(' if obj.intb=2 then;');
  11330. Add(' obj.intb:=obj.intb+2;');
  11331. Add(' obj.setint(obj.inta);');
  11332. ConvertProgram;
  11333. CheckSource('TestClass_Property',
  11334. LinesToStr([ // statements
  11335. 'rtl.createClass($mod, "TObject", null, function () {',
  11336. ' this.$init = function () {',
  11337. ' this.Fx = 0;',
  11338. ' this.Fy = 0;',
  11339. ' };',
  11340. ' this.$final = function () {',
  11341. ' };',
  11342. ' this.GetInt = function () {',
  11343. ' var Result = 0;',
  11344. ' Result = this.Fx;',
  11345. ' return Result;',
  11346. ' };',
  11347. ' this.SetInt = function (Value) {',
  11348. ' if (Value === this.Fy) return;',
  11349. ' this.Fy = Value;',
  11350. ' };',
  11351. ' this.DoIt = function () {',
  11352. ' this.Fy = this.Fx + 1;',
  11353. ' this.Fy = this.Fx + 1;',
  11354. ' this.SetInt(this.GetInt() + 1);',
  11355. ' this.SetInt(this.GetInt() + 1);',
  11356. ' };',
  11357. '});',
  11358. 'this.Obj = null;'
  11359. ]),
  11360. LinesToStr([ // $mod.$main
  11361. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  11362. 'if ($mod.Obj.GetInt() === 2);',
  11363. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  11364. '$mod.Obj.SetInt($mod.Obj.Fx);'
  11365. ]));
  11366. end;
  11367. procedure TTestModule.TestClass_Property_ClassMethod;
  11368. begin
  11369. StartProgram(false);
  11370. Add([
  11371. 'type',
  11372. ' TObject = class',
  11373. ' class var Fx: longint;',
  11374. ' class var Fy: longint;',
  11375. ' class function GetInt: longint;',
  11376. ' class procedure SetInt(Value: longint);',
  11377. ' end;',
  11378. ' TBird = class',
  11379. ' class procedure DoIt;',
  11380. ' class property IntA: longint read Fx write Fy;',
  11381. ' class property IntB: longint read GetInt write SetInt;',
  11382. ' end;',
  11383. 'class function tobject.getint: longint;',
  11384. 'begin',
  11385. ' result:=fx;',
  11386. 'end;',
  11387. 'class procedure tobject.setint(value: longint);',
  11388. 'begin',
  11389. 'end;',
  11390. 'class procedure tbird.doit;',
  11391. 'begin',
  11392. ' FX:=3;',
  11393. ' IntA:=IntA+1;',
  11394. ' Self.IntA:=Self.IntA+1;',
  11395. ' IntB:=IntB+1;',
  11396. ' Self.IntB:=Self.IntB+1;',
  11397. ' with Self do begin',
  11398. ' FX:=11;',
  11399. ' IntA:=IntA+12;',
  11400. ' IntB:=IntB+13;',
  11401. ' end;',
  11402. 'end;',
  11403. 'var Obj: tbird;',
  11404. 'begin',
  11405. ' tbird.fx:=tbird.fx+1;',
  11406. ' tbird.inta:=tbird.inta+1;',
  11407. ' if tbird.intb=2 then;',
  11408. ' tbird.intb:=tbird.intb+2;',
  11409. ' tbird.setint(tbird.inta);',
  11410. ' obj.inta:=obj.inta+1;',
  11411. ' if obj.intb=2 then;',
  11412. ' obj.intb:=obj.intb+2;',
  11413. ' obj.setint(obj.inta);',
  11414. ' with Tbird do begin',
  11415. ' FX:=FY+1;',
  11416. ' inta:=inta+2;',
  11417. ' intb:=intb+3;',
  11418. ' end;',
  11419. ' with Obj do begin',
  11420. ' FX:=FY+1;',
  11421. ' inta:=inta+2;',
  11422. ' intb:=intb+3;',
  11423. ' end;',
  11424. '']);
  11425. ConvertProgram;
  11426. CheckSource('TestClass_Property_ClassMethod',
  11427. LinesToStr([ // statements
  11428. 'rtl.createClass($mod, "TObject", null, function () {',
  11429. ' this.Fx = 0;',
  11430. ' this.Fy = 0;',
  11431. ' this.$init = function () {',
  11432. ' };',
  11433. ' this.$final = function () {',
  11434. ' };',
  11435. ' this.GetInt = function () {',
  11436. ' var Result = 0;',
  11437. ' Result = this.Fx;',
  11438. ' return Result;',
  11439. ' };',
  11440. ' this.SetInt = function (Value) {',
  11441. ' };',
  11442. '});',
  11443. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  11444. ' this.DoIt = function () {',
  11445. ' $mod.TObject.Fx = 3;',
  11446. ' $mod.TObject.Fy = this.Fx + 1;',
  11447. ' $mod.TObject.Fy = this.Fx + 1;',
  11448. ' this.SetInt(this.GetInt() + 1);',
  11449. ' this.SetInt(this.GetInt() + 1);',
  11450. ' $mod.TObject.Fx = 11;',
  11451. ' $mod.TObject.Fy = this.Fx + 12;',
  11452. ' this.SetInt(this.GetInt() + 13);',
  11453. ' };',
  11454. '});',
  11455. 'this.Obj = null;'
  11456. ]),
  11457. LinesToStr([ // $mod.$main
  11458. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  11459. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  11460. 'if ($mod.TBird.GetInt() === 2);',
  11461. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  11462. '$mod.TBird.SetInt($mod.TBird.Fx);',
  11463. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  11464. 'if ($mod.Obj.$class.GetInt() === 2);',
  11465. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  11466. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  11467. 'var $with1 = $mod.TBird;',
  11468. '$mod.TObject.Fx = $with1.Fy + 1;',
  11469. '$mod.TObject.Fy = $with1.Fx + 2;',
  11470. '$with1.SetInt($with1.GetInt() + 3);',
  11471. 'var $with2 = $mod.Obj;',
  11472. '$mod.TObject.Fx = $with2.Fy + 1;',
  11473. '$mod.TObject.Fy = $with2.Fx + 2;',
  11474. '$with2.SetInt($with2.GetInt() + 3);',
  11475. '']));
  11476. end;
  11477. procedure TTestModule.TestClass_Property_Indexed;
  11478. begin
  11479. StartProgram(false);
  11480. Add('type');
  11481. Add(' TObject = class');
  11482. Add(' FItems: array of longint;');
  11483. Add(' function GetItems(Index: longint): longint;');
  11484. Add(' procedure SetItems(Index: longint; Value: longint);');
  11485. Add(' procedure DoIt;');
  11486. Add(' property Items[Index: longint]: longint read getitems write setitems;');
  11487. Add(' end;');
  11488. Add('function tobject.getitems(index: longint): longint;');
  11489. Add('begin');
  11490. Add(' Result:=fitems[index];');
  11491. Add('end;');
  11492. Add('procedure tobject.setitems(index: longint; value: longint);');
  11493. Add('begin');
  11494. Add(' fitems[index]:=value;');
  11495. Add('end;');
  11496. Add('procedure tobject.doit;');
  11497. Add('begin');
  11498. Add(' items[1]:=2;');
  11499. Add(' items[3]:=items[4];');
  11500. Add(' self.items[5]:=self.items[6];');
  11501. Add(' items[items[7]]:=items[items[8]];');
  11502. Add('end;');
  11503. Add('var Obj: tobject;');
  11504. Add('begin');
  11505. Add(' obj.Items[11]:=obj.Items[12];');
  11506. ConvertProgram;
  11507. CheckSource('TestClass_Property_Indexed',
  11508. LinesToStr([ // statements
  11509. 'rtl.createClass($mod, "TObject", null, function () {',
  11510. ' this.$init = function () {',
  11511. ' this.FItems = [];',
  11512. ' };',
  11513. ' this.$final = function () {',
  11514. ' this.FItems = undefined;',
  11515. ' };',
  11516. ' this.GetItems = function (Index) {',
  11517. ' var Result = 0;',
  11518. ' Result = this.FItems[Index];',
  11519. ' return Result;',
  11520. ' };',
  11521. ' this.SetItems = function (Index, Value) {',
  11522. ' this.FItems[Index] = Value;',
  11523. ' };',
  11524. ' this.DoIt = function () {',
  11525. ' this.SetItems(1, 2);',
  11526. ' this.SetItems(3,this.GetItems(4));',
  11527. ' this.SetItems(5,this.GetItems(6));',
  11528. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  11529. ' };',
  11530. '});',
  11531. 'this.Obj = null;'
  11532. ]),
  11533. LinesToStr([ // $mod.$main
  11534. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  11535. ]));
  11536. end;
  11537. procedure TTestModule.TestClass_Property_IndexSpec;
  11538. begin
  11539. StartProgram(false);
  11540. Add([
  11541. 'type',
  11542. ' TEnum = (red, blue);',
  11543. ' TObject = class',
  11544. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  11545. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  11546. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  11547. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  11548. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  11549. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  11550. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  11551. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  11552. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  11553. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  11554. ' end;',
  11555. 'procedure DoIt(b: boolean); begin end;',
  11556. 'var',
  11557. ' o: TObject;',
  11558. 'begin',
  11559. ' o.B1:=o.B1;',
  11560. ' o.B2:=o.B2;',
  11561. ' o.B3:=o.B3;',
  11562. ' o.I1[''a'']:=o.I1[''b''];',
  11563. ' doit(o.b1);',
  11564. ' doit(o.b2);',
  11565. ' doit(o.i1[''c'']);',
  11566. '']);
  11567. ConvertProgram;
  11568. CheckSource('TestClass_Property_IndexSpec',
  11569. LinesToStr([ // statements
  11570. 'this.TEnum = {',
  11571. ' "0": "red",',
  11572. ' red: 0,',
  11573. ' "1": "blue",',
  11574. ' blue: 1',
  11575. '};',
  11576. 'rtl.createClass($mod, "TObject", null, function () {',
  11577. ' this.$init = function () {',
  11578. ' };',
  11579. ' this.$final = function () {',
  11580. ' };',
  11581. '});',
  11582. 'this.DoIt = function (b) {',
  11583. '};',
  11584. 'this.o = null;',
  11585. '']),
  11586. LinesToStr([ // $mod.$main
  11587. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  11588. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  11589. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  11590. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  11591. '$mod.DoIt($mod.o.GetIntBool(1));',
  11592. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  11593. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  11594. '']));
  11595. end;
  11596. procedure TTestModule.TestClass_PropertyOfTypeArray;
  11597. begin
  11598. StartProgram(false);
  11599. Add('type');
  11600. Add(' TArray = array of longint;');
  11601. Add(' TObject = class');
  11602. Add(' FItems: TArray;');
  11603. Add(' function GetItems: tarray;');
  11604. Add(' procedure SetItems(Value: tarray);');
  11605. Add(' property Items: tarray read getitems write setitems;');
  11606. Add(' end;');
  11607. Add('function tobject.getitems: tarray;');
  11608. Add('begin');
  11609. Add(' Result:=fitems;');
  11610. Add('end;');
  11611. Add('procedure tobject.setitems(value: tarray);');
  11612. Add('begin');
  11613. Add(' fitems:=value;');
  11614. Add(' fitems:=nil;');
  11615. Add(' Items:=nil;');
  11616. Add(' Items:=Items;');
  11617. Add(' Items[1]:=2;');
  11618. Add(' fitems[3]:=Items[4];');
  11619. Add(' Items[5]:=Items[6];');
  11620. Add(' Self.Items[7]:=8;');
  11621. Add(' Self.Items[9]:=Self.Items[10];');
  11622. Add(' Items[Items[11]]:=Items[Items[12]];');
  11623. Add('end;');
  11624. Add('var Obj: tobject;');
  11625. Add('begin');
  11626. Add(' obj.items:=nil;');
  11627. Add(' obj.items:=obj.items;');
  11628. Add(' obj.items[11]:=obj.items[12];');
  11629. ConvertProgram;
  11630. CheckSource('TestClass_PropertyOfTypeArray',
  11631. LinesToStr([ // statements
  11632. 'rtl.createClass($mod, "TObject", null, function () {',
  11633. ' this.$init = function () {',
  11634. ' this.FItems = [];',
  11635. ' };',
  11636. ' this.$final = function () {',
  11637. ' this.FItems = undefined;',
  11638. ' };',
  11639. ' this.GetItems = function () {',
  11640. ' var Result = [];',
  11641. ' Result = this.FItems;',
  11642. ' return Result;',
  11643. ' };',
  11644. ' this.SetItems = function (Value) {',
  11645. ' this.FItems = Value;',
  11646. ' this.FItems = [];',
  11647. ' this.SetItems([]);',
  11648. ' this.SetItems(this.GetItems());',
  11649. ' this.GetItems()[1] = 2;',
  11650. ' this.FItems[3] = this.GetItems()[4];',
  11651. ' this.GetItems()[5] = this.GetItems()[6];',
  11652. ' this.GetItems()[7] = 8;',
  11653. ' this.GetItems()[9] = this.GetItems()[10];',
  11654. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  11655. ' };',
  11656. '});',
  11657. 'this.Obj = null;'
  11658. ]),
  11659. LinesToStr([ // $mod.$main
  11660. '$mod.Obj.SetItems([]);',
  11661. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  11662. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  11663. ]));
  11664. end;
  11665. procedure TTestModule.TestClass_PropertyDefault;
  11666. begin
  11667. StartProgram(false);
  11668. Add([
  11669. 'type',
  11670. ' TArray = array of longint;',
  11671. ' TObject = class',
  11672. ' FItems: TArray;',
  11673. ' function GetItems(Index: longint): longint;',
  11674. ' procedure SetItems(Index, Value: longint);',
  11675. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  11676. ' end;',
  11677. 'function tobject.getitems(index: longint): longint;',
  11678. 'begin',
  11679. 'end;',
  11680. 'procedure tobject.setitems(index, value: longint);',
  11681. 'begin',
  11682. ' Self[1]:=2;',
  11683. ' Self[3]:=Self[index];',
  11684. ' Self[index]:=Self[Self[value]];',
  11685. ' Self[Self[4]]:=value;',
  11686. 'end;',
  11687. 'var Obj: tobject;',
  11688. 'begin',
  11689. ' obj[11]:=12;',
  11690. ' obj[13]:=obj[14];',
  11691. ' obj[obj[15]]:=obj[obj[15]];',
  11692. ' TObject(obj)[16]:=TObject(obj)[17];']);
  11693. ConvertProgram;
  11694. CheckSource('TestClass_PropertyDefault',
  11695. LinesToStr([ // statements
  11696. 'rtl.createClass($mod, "TObject", null, function () {',
  11697. ' this.$init = function () {',
  11698. ' this.FItems = [];',
  11699. ' };',
  11700. ' this.$final = function () {',
  11701. ' this.FItems = undefined;',
  11702. ' };',
  11703. ' this.GetItems = function (Index) {',
  11704. ' var Result = 0;',
  11705. ' return Result;',
  11706. ' };',
  11707. ' this.SetItems = function (Index, Value) {',
  11708. ' this.SetItems(1, 2);',
  11709. ' this.SetItems(3, this.GetItems(Index));',
  11710. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  11711. ' this.SetItems(this.GetItems(4), Value);',
  11712. ' };',
  11713. '});',
  11714. 'this.Obj = null;'
  11715. ]),
  11716. LinesToStr([ // $mod.$main
  11717. '$mod.Obj.SetItems(11, 12);',
  11718. '$mod.Obj.SetItems(13, $mod.Obj.GetItems(14));',
  11719. '$mod.Obj.SetItems($mod.Obj.GetItems(15), $mod.Obj.GetItems($mod.Obj.GetItems(15)));',
  11720. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  11721. '']));
  11722. end;
  11723. procedure TTestModule.TestClass_PropertyDefault2;
  11724. begin
  11725. StartProgram(false);
  11726. Add([
  11727. 'type',
  11728. ' TObject = class end;',
  11729. ' TAlphaList = class',
  11730. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  11731. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  11732. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  11733. ' end;',
  11734. ' TBetaList = class',
  11735. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  11736. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  11737. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  11738. ' end;',
  11739. ' TBird = class',
  11740. ' procedure DoIt;',
  11741. ' end;',
  11742. 'procedure TBird.DoIt;',
  11743. 'var',
  11744. ' List: TAlphaList;',
  11745. 'begin',
  11746. ' if TBetaList(List[true])[3]=nil then ;',
  11747. ' TBetaList(List[false])[5]:=nil;',
  11748. 'end;',
  11749. 'var',
  11750. ' List: TAlphaList;',
  11751. 'begin',
  11752. ' if TBetaList(List[true])[3]=nil then ;',
  11753. ' TBetaList(List[false])[5]:=nil;',
  11754. '']);
  11755. ConvertProgram;
  11756. CheckSource('TestClass_PropertyDefault2',
  11757. LinesToStr([ // statements
  11758. 'rtl.createClass($mod, "TObject", null, function () {',
  11759. ' this.$init = function () {',
  11760. ' };',
  11761. ' this.$final = function () {',
  11762. ' };',
  11763. '});',
  11764. 'rtl.createClass($mod, "TAlphaList", $mod.TObject, function () {',
  11765. '});',
  11766. 'rtl.createClass($mod, "TBetaList", $mod.TObject, function () {',
  11767. '});',
  11768. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  11769. ' this.DoIt = function () {',
  11770. ' var List = null;',
  11771. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  11772. ' List.GetAlphas(false).SetBetas(5, null);',
  11773. ' };',
  11774. '});',
  11775. 'this.List = null;',
  11776. '']),
  11777. LinesToStr([ // $mod.$main
  11778. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  11779. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  11780. '']));
  11781. end;
  11782. procedure TTestModule.TestClass_PropertyOverride;
  11783. begin
  11784. StartProgram(false);
  11785. Add('type');
  11786. Add(' integer = longint;');
  11787. Add(' TObject = class');
  11788. Add(' FItem: integer;');
  11789. Add(' function GetItem: integer; external name ''GetItem'';');
  11790. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  11791. Add(' property Item: integer read getitem write setitem;');
  11792. Add(' end;');
  11793. Add(' TCar = class');
  11794. Add(' FBag: integer;');
  11795. Add(' function GetBag: integer; external name ''GetBag'';');
  11796. Add(' property Item read getbag;');
  11797. Add(' end;');
  11798. Add('var');
  11799. Add(' Obj: tobject;');
  11800. Add(' Car: tcar;');
  11801. Add('begin');
  11802. Add(' Obj.Item:=Obj.Item;');
  11803. Add(' Car.Item:=Car.Item;');
  11804. ConvertProgram;
  11805. CheckSource('TestClass_PropertyOverride',
  11806. LinesToStr([ // statements
  11807. 'rtl.createClass($mod, "TObject", null, function () {',
  11808. ' this.$init = function () {',
  11809. ' this.FItem = 0;',
  11810. ' };',
  11811. ' this.$final = function () {',
  11812. ' };',
  11813. '});',
  11814. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  11815. ' this.$init = function () {',
  11816. ' $mod.TObject.$init.call(this);',
  11817. ' this.FBag = 0;',
  11818. ' };',
  11819. '});',
  11820. 'this.Obj = null;',
  11821. 'this.Car = null;',
  11822. '']),
  11823. LinesToStr([ // $mod.$main
  11824. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  11825. '$mod.Car.SetItem($mod.Car.GetBag());',
  11826. '']));
  11827. end;
  11828. procedure TTestModule.TestClass_PropertyIncVisibility;
  11829. begin
  11830. AddModuleWithIntfImplSrc('unit1.pp',
  11831. LinesToStr([
  11832. 'type',
  11833. ' TNumber = longint;',
  11834. ' TInteger = longint;',
  11835. ' TObject = class',
  11836. ' private',
  11837. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  11838. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  11839. ' protected',
  11840. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  11841. ' end;']),
  11842. LinesToStr([
  11843. '']));
  11844. StartProgram(true);
  11845. Add([
  11846. 'uses unit1;',
  11847. 'type',
  11848. ' TBird = class',
  11849. ' public',
  11850. ' property Items;',
  11851. ' end;',
  11852. 'procedure DoIt(i: TInteger);',
  11853. 'begin',
  11854. 'end;',
  11855. 'var b: TBird;',
  11856. 'begin',
  11857. ' b.Items[1]:=2;',
  11858. ' b.Items[3]:=b.Items[4];',
  11859. ' DoIt(b.Items[5]);',
  11860. '']);
  11861. ConvertProgram;
  11862. CheckSource('TestClass_PropertyIncVisibility',
  11863. LinesToStr([ // statements
  11864. 'rtl.createClass($mod, "TBird", pas.unit1.TObject, function () {',
  11865. '});',
  11866. 'this.DoIt = function (i) {',
  11867. '};',
  11868. 'this.b = null;'
  11869. ]),
  11870. LinesToStr([ // $mod.$main
  11871. '$mod.b.SetItems(1, 2);',
  11872. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  11873. '$mod.DoIt($mod.b.GetItems(5));'
  11874. ]));
  11875. end;
  11876. procedure TTestModule.TestClass_Assigned;
  11877. begin
  11878. StartProgram(false);
  11879. Add('type');
  11880. Add(' TObject = class');
  11881. Add(' end;');
  11882. Add('var');
  11883. Add(' Obj: tobject;');
  11884. Add(' b: boolean;');
  11885. Add('begin');
  11886. Add(' if Assigned(obj) then ;');
  11887. Add(' b:=Assigned(obj) or false;');
  11888. ConvertProgram;
  11889. CheckSource('TestClass_Assigned',
  11890. LinesToStr([ // statements
  11891. 'rtl.createClass($mod, "TObject", null, function () {',
  11892. ' this.$init = function () {',
  11893. ' };',
  11894. ' this.$final = function () {',
  11895. ' };',
  11896. '});',
  11897. 'this.Obj = null;',
  11898. 'this.b = false;'
  11899. ]),
  11900. LinesToStr([ // $mod.$main
  11901. 'if ($mod.Obj != null);',
  11902. '$mod.b = ($mod.Obj != null) || false;'
  11903. ]));
  11904. end;
  11905. procedure TTestModule.TestClass_WithClassDoCreate;
  11906. begin
  11907. StartProgram(false);
  11908. Add('type');
  11909. Add(' TObject = class');
  11910. Add(' aBool: boolean;');
  11911. Add(' Arr: array of boolean;');
  11912. Add(' constructor Create;');
  11913. Add(' end;');
  11914. Add('constructor TObject.Create; begin end;');
  11915. Add('var');
  11916. Add(' Obj: tobject;');
  11917. Add(' b: boolean;');
  11918. Add('begin');
  11919. Add(' with tobject.create do begin');
  11920. Add(' b:=abool;');
  11921. Add(' abool:=b;');
  11922. Add(' b:=arr[1];');
  11923. Add(' arr[2]:=b;');
  11924. Add(' end;');
  11925. Add(' with tobject do');
  11926. Add(' obj:=create;');
  11927. Add(' with obj do begin');
  11928. Add(' create;');
  11929. Add(' b:=abool;');
  11930. Add(' abool:=b;');
  11931. Add(' b:=arr[3];');
  11932. Add(' arr[4]:=b;');
  11933. Add(' end;');
  11934. ConvertProgram;
  11935. CheckSource('TestClass_WithClassDoCreate',
  11936. LinesToStr([ // statements
  11937. 'rtl.createClass($mod, "TObject", null, function () {',
  11938. ' this.$init = function () {',
  11939. ' this.aBool = false;',
  11940. ' this.Arr = [];',
  11941. ' };',
  11942. ' this.$final = function () {',
  11943. ' this.Arr = undefined;',
  11944. ' };',
  11945. ' this.Create = function () {',
  11946. ' return this;',
  11947. ' };',
  11948. '});',
  11949. 'this.Obj = null;',
  11950. 'this.b = false;'
  11951. ]),
  11952. LinesToStr([ // $mod.$main
  11953. 'var $with1 = $mod.TObject.$create("Create");',
  11954. '$mod.b = $with1.aBool;',
  11955. '$with1.aBool = $mod.b;',
  11956. '$mod.b = $with1.Arr[1];',
  11957. '$with1.Arr[2] = $mod.b;',
  11958. 'var $with2 = $mod.TObject;',
  11959. '$mod.Obj = $with2.$create("Create");',
  11960. 'var $with3 = $mod.Obj;',
  11961. '$with3.Create();',
  11962. '$mod.b = $with3.aBool;',
  11963. '$with3.aBool = $mod.b;',
  11964. '$mod.b = $with3.Arr[3];',
  11965. '$with3.Arr[4] = $mod.b;',
  11966. '']));
  11967. end;
  11968. procedure TTestModule.TestClass_WithClassInstDoProperty;
  11969. begin
  11970. StartProgram(false);
  11971. Add('type');
  11972. Add(' TObject = class');
  11973. Add(' FInt: longint;');
  11974. Add(' constructor Create;');
  11975. Add(' function GetSize: longint;');
  11976. Add(' procedure SetSize(Value: longint);');
  11977. Add(' property Int: longint read FInt write FInt;');
  11978. Add(' property Size: longint read GetSize write SetSize;');
  11979. Add(' end;');
  11980. Add('constructor TObject.Create; begin end;');
  11981. Add('function TObject.GetSize: longint; begin; end;');
  11982. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  11983. Add('var');
  11984. Add(' Obj: tobject;');
  11985. Add(' i: longint;');
  11986. Add('begin');
  11987. Add(' with TObject.Create do begin');
  11988. Add(' i:=int;');
  11989. Add(' int:=i;');
  11990. Add(' i:=size;');
  11991. Add(' size:=i;');
  11992. Add(' end;');
  11993. Add(' with obj do begin');
  11994. Add(' i:=int;');
  11995. Add(' int:=i;');
  11996. Add(' i:=size;');
  11997. Add(' size:=i;');
  11998. Add(' end;');
  11999. ConvertProgram;
  12000. CheckSource('TestClass_WithClassInstDoProperty',
  12001. LinesToStr([ // statements
  12002. 'rtl.createClass($mod, "TObject", null, function () {',
  12003. ' this.$init = function () {',
  12004. ' this.FInt = 0;',
  12005. ' };',
  12006. ' this.$final = function () {',
  12007. ' };',
  12008. ' this.Create = function () {',
  12009. ' return this;',
  12010. ' };',
  12011. ' this.GetSize = function () {',
  12012. ' var Result = 0;',
  12013. ' return Result;',
  12014. ' };',
  12015. ' this.SetSize = function (Value) {',
  12016. ' };',
  12017. '});',
  12018. 'this.Obj = null;',
  12019. 'this.i = 0;'
  12020. ]),
  12021. LinesToStr([ // $mod.$main
  12022. 'var $with1 = $mod.TObject.$create("Create");',
  12023. '$mod.i = $with1.FInt;',
  12024. '$with1.FInt = $mod.i;',
  12025. '$mod.i = $with1.GetSize();',
  12026. '$with1.SetSize($mod.i);',
  12027. 'var $with2 = $mod.Obj;',
  12028. '$mod.i = $with2.FInt;',
  12029. '$with2.FInt = $mod.i;',
  12030. '$mod.i = $with2.GetSize();',
  12031. '$with2.SetSize($mod.i);',
  12032. '']));
  12033. end;
  12034. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  12035. begin
  12036. StartProgram(false);
  12037. Add('type');
  12038. Add(' TObject = class');
  12039. Add(' constructor Create;');
  12040. Add(' function GetItems(Index: longint): longint;');
  12041. Add(' procedure SetItems(Index, Value: longint);');
  12042. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  12043. Add(' end;');
  12044. Add('constructor TObject.Create; begin end;');
  12045. Add('function tobject.getitems(index: longint): longint; begin; end;');
  12046. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  12047. Add('var');
  12048. Add(' Obj: tobject;');
  12049. Add(' i: longint;');
  12050. Add('begin');
  12051. Add(' with TObject.Create do begin');
  12052. Add(' i:=Items[1];');
  12053. Add(' Items[2]:=i;');
  12054. Add(' end;');
  12055. Add(' with obj do begin');
  12056. Add(' i:=Items[3];');
  12057. Add(' Items[4]:=i;');
  12058. Add(' end;');
  12059. ConvertProgram;
  12060. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  12061. LinesToStr([ // statements
  12062. 'rtl.createClass($mod, "TObject", null, function () {',
  12063. ' this.$init = function () {',
  12064. ' };',
  12065. ' this.$final = function () {',
  12066. ' };',
  12067. ' this.Create = function () {',
  12068. ' return this;',
  12069. ' };',
  12070. ' this.GetItems = function (Index) {',
  12071. ' var Result = 0;',
  12072. ' return Result;',
  12073. ' };',
  12074. ' this.SetItems = function (Index, Value) {',
  12075. ' };',
  12076. '});',
  12077. 'this.Obj = null;',
  12078. 'this.i = 0;'
  12079. ]),
  12080. LinesToStr([ // $mod.$main
  12081. 'var $with1 = $mod.TObject.$create("Create");',
  12082. '$mod.i = $with1.GetItems(1);',
  12083. '$with1.SetItems(2, $mod.i);',
  12084. 'var $with2 = $mod.Obj;',
  12085. '$mod.i = $with2.GetItems(3);',
  12086. '$with2.SetItems(4, $mod.i);',
  12087. '']));
  12088. end;
  12089. procedure TTestModule.TestClass_WithClassInstDoFunc;
  12090. begin
  12091. StartProgram(false);
  12092. Add('type');
  12093. Add(' TObject = class');
  12094. Add(' constructor Create;');
  12095. Add(' function GetSize: longint;');
  12096. Add(' procedure SetSize(Value: longint);');
  12097. Add(' end;');
  12098. Add('constructor TObject.Create; begin end;');
  12099. Add('function TObject.GetSize: longint; begin; end;');
  12100. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  12101. Add('var');
  12102. Add(' Obj: tobject;');
  12103. Add(' i: longint;');
  12104. Add('begin');
  12105. Add(' with TObject.Create do begin');
  12106. Add(' i:=GetSize;');
  12107. Add(' i:=GetSize();');
  12108. Add(' SetSize(i);');
  12109. Add(' end;');
  12110. Add(' with obj do begin');
  12111. Add(' i:=GetSize;');
  12112. Add(' i:=GetSize();');
  12113. Add(' SetSize(i);');
  12114. Add(' end;');
  12115. ConvertProgram;
  12116. CheckSource('TestClass_WithClassInstDoFunc',
  12117. LinesToStr([ // statements
  12118. 'rtl.createClass($mod, "TObject", null, function () {',
  12119. ' this.$init = function () {',
  12120. ' };',
  12121. ' this.$final = function () {',
  12122. ' };',
  12123. ' this.Create = function () {',
  12124. ' return this;',
  12125. ' };',
  12126. ' this.GetSize = function () {',
  12127. ' var Result = 0;',
  12128. ' return Result;',
  12129. ' };',
  12130. ' this.SetSize = function (Value) {',
  12131. ' };',
  12132. '});',
  12133. 'this.Obj = null;',
  12134. 'this.i = 0;'
  12135. ]),
  12136. LinesToStr([ // $mod.$main
  12137. 'var $with1 = $mod.TObject.$create("Create");',
  12138. '$mod.i = $with1.GetSize();',
  12139. '$mod.i = $with1.GetSize();',
  12140. '$with1.SetSize($mod.i);',
  12141. 'var $with2 = $mod.Obj;',
  12142. '$mod.i = $with2.GetSize();',
  12143. '$mod.i = $with2.GetSize();',
  12144. '$with2.SetSize($mod.i);',
  12145. '']));
  12146. end;
  12147. procedure TTestModule.TestClass_TypeCast;
  12148. begin
  12149. StartProgram(false);
  12150. Add('type');
  12151. Add(' TObject = class');
  12152. Add(' Next: TObject;');
  12153. Add(' constructor Create;');
  12154. Add(' end;');
  12155. Add(' TControl = class(TObject)');
  12156. Add(' Arr: array of TObject;');
  12157. Add(' function GetIt(vI: longint = 0): TObject;');
  12158. Add(' end;');
  12159. Add('constructor tobject.create; begin end;');
  12160. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  12161. Add('var');
  12162. Add(' Obj: tobject;');
  12163. Add('begin');
  12164. Add(' obj:=tcontrol(obj).next;');
  12165. Add(' tcontrol(obj):=nil;');
  12166. Add(' obj:=tcontrol(obj);');
  12167. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  12168. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  12169. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  12170. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  12171. ConvertProgram;
  12172. CheckSource('TestClass_TypeCast',
  12173. LinesToStr([ // statements
  12174. 'rtl.createClass($mod, "TObject", null, function () {',
  12175. ' this.$init = function () {',
  12176. ' this.Next = null;',
  12177. ' };',
  12178. ' this.$final = function () {',
  12179. ' this.Next = undefined;',
  12180. ' };',
  12181. ' this.Create = function () {',
  12182. ' return this;',
  12183. ' };',
  12184. '});',
  12185. 'rtl.createClass($mod, "TControl", $mod.TObject, function () {',
  12186. ' this.$init = function () {',
  12187. ' $mod.TObject.$init.call(this);',
  12188. ' this.Arr = [];',
  12189. ' };',
  12190. ' this.$final = function () {',
  12191. ' this.Arr = undefined;',
  12192. ' $mod.TObject.$final.call(this);',
  12193. ' };',
  12194. ' this.GetIt = function (vI) {',
  12195. ' var Result = null;',
  12196. ' return Result;',
  12197. ' };',
  12198. '});',
  12199. 'this.Obj = null;'
  12200. ]),
  12201. LinesToStr([ // $mod.$main
  12202. '$mod.Obj = $mod.Obj.Next;',
  12203. '$mod.Obj = null;',
  12204. '$mod.Obj = $mod.Obj;',
  12205. '$mod.Obj = $mod.Obj.GetIt(0);',
  12206. '$mod.Obj = $mod.Obj.GetIt(0);',
  12207. '$mod.Obj = $mod.Obj.GetIt(1);',
  12208. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  12209. '']));
  12210. end;
  12211. procedure TTestModule.TestClass_TypeCastUntypedParam;
  12212. begin
  12213. StartProgram(false);
  12214. Add('type');
  12215. Add(' TObject = class end;');
  12216. Add('procedure ProcA(var A);');
  12217. Add('begin');
  12218. Add(' TObject(A):=nil;');
  12219. Add(' TObject(A):=TObject(A);');
  12220. Add(' if TObject(A)=nil then ;');
  12221. Add(' if nil=TObject(A) then ;');
  12222. Add('end;');
  12223. Add('procedure ProcB(out A);');
  12224. Add('begin');
  12225. Add(' TObject(A):=nil;');
  12226. Add(' TObject(A):=TObject(A);');
  12227. Add(' if TObject(A)=nil then ;');
  12228. Add(' if nil=TObject(A) then ;');
  12229. Add('end;');
  12230. Add('procedure ProcC(const A);');
  12231. Add('begin');
  12232. Add(' if TObject(A)=nil then ;');
  12233. Add(' if nil=TObject(A) then ;');
  12234. Add('end;');
  12235. Add('var o: TObject;');
  12236. Add('begin');
  12237. Add(' ProcA(o);');
  12238. Add(' ProcB(o);');
  12239. Add(' ProcC(o);');
  12240. ConvertProgram;
  12241. CheckSource('TestClass_TypeCastUntypedParam',
  12242. LinesToStr([ // statements
  12243. 'rtl.createClass($mod, "TObject", null, function () {',
  12244. ' this.$init = function () {',
  12245. ' };',
  12246. ' this.$final = function () {',
  12247. ' };',
  12248. '});',
  12249. 'this.ProcA = function (A) {',
  12250. ' A.set(null);',
  12251. ' A.set(A.get());',
  12252. ' if (A.get() === null);',
  12253. ' if (null === A.get());',
  12254. '};',
  12255. 'this.ProcB = function (A) {',
  12256. ' A.set(null);',
  12257. ' A.set(A.get());',
  12258. ' if (A.get() === null);',
  12259. ' if (null === A.get());',
  12260. '};',
  12261. 'this.ProcC = function (A) {',
  12262. ' if (A === null);',
  12263. ' if (null === A);',
  12264. '};',
  12265. 'this.o = null;',
  12266. '']),
  12267. LinesToStr([ // $mod.$main
  12268. '$mod.ProcA({',
  12269. ' p: $mod,',
  12270. ' get: function () {',
  12271. ' return this.p.o;',
  12272. ' },',
  12273. ' set: function (v) {',
  12274. ' this.p.o = v;',
  12275. ' }',
  12276. '});',
  12277. '$mod.ProcB({',
  12278. ' p: $mod,',
  12279. ' get: function () {',
  12280. ' return this.p.o;',
  12281. ' },',
  12282. ' set: function (v) {',
  12283. ' this.p.o = v;',
  12284. ' }',
  12285. '});',
  12286. '$mod.ProcC($mod.o);',
  12287. '']));
  12288. end;
  12289. procedure TTestModule.TestClass_Overloads;
  12290. begin
  12291. StartProgram(false);
  12292. Add('type');
  12293. Add(' TObject = class');
  12294. Add(' procedure DoIt;');
  12295. Add(' procedure DoIt(vI: longint);');
  12296. Add(' end;');
  12297. Add('procedure TObject.DoIt;');
  12298. Add('begin');
  12299. Add(' DoIt;');
  12300. Add(' DoIt(1);');
  12301. Add('end;');
  12302. Add('procedure TObject.DoIt(vI: longint); begin end;');
  12303. Add('begin');
  12304. ConvertProgram;
  12305. CheckSource('TestClass_Overloads',
  12306. LinesToStr([ // statements
  12307. 'rtl.createClass($mod, "TObject", null, function () {',
  12308. ' this.$init = function () {',
  12309. ' };',
  12310. ' this.$final = function () {',
  12311. ' };',
  12312. ' this.DoIt = function () {',
  12313. ' this.DoIt();',
  12314. ' this.DoIt$1(1);',
  12315. ' };',
  12316. ' this.DoIt$1 = function (vI) {',
  12317. ' };',
  12318. '});',
  12319. '']),
  12320. LinesToStr([ // $mod.$main
  12321. '']));
  12322. end;
  12323. procedure TTestModule.TestClass_OverloadsAncestor;
  12324. begin
  12325. StartProgram(false);
  12326. Add('type');
  12327. Add(' TObject = class;');
  12328. Add(' TObject = class');
  12329. Add(' procedure DoIt(vA: longint);');
  12330. Add(' procedure DoIt(vA, vB: longint);');
  12331. Add(' end;');
  12332. Add(' TCar = class;');
  12333. Add(' TCar = class');
  12334. Add(' procedure DoIt(vA: longint);');
  12335. Add(' procedure DoIt(vA, vB: longint);');
  12336. Add(' end;');
  12337. Add('procedure tobject.doit(va: longint);');
  12338. Add('begin');
  12339. Add(' doit(1);');
  12340. Add(' doit(1,2);');
  12341. Add('end;');
  12342. Add('procedure tobject.doit(va, vb: longint); begin end;');
  12343. Add('procedure tcar.doit(va: longint);');
  12344. Add('begin');
  12345. Add(' doit(1);');
  12346. Add(' doit(1,2);');
  12347. Add(' inherited doit(1);');
  12348. Add(' inherited doit(1,2);');
  12349. Add('end;');
  12350. Add('procedure tcar.doit(va, vb: longint); begin end;');
  12351. Add('begin');
  12352. ConvertProgram;
  12353. CheckSource('TestClass_OverloadsAncestor',
  12354. LinesToStr([ // statements
  12355. 'rtl.createClass($mod, "TObject", null, function () {',
  12356. ' this.$init = function () {',
  12357. ' };',
  12358. ' this.$final = function () {',
  12359. ' };',
  12360. ' this.DoIt = function (vA) {',
  12361. ' this.DoIt(1);',
  12362. ' this.DoIt$1(1,2);',
  12363. ' };',
  12364. ' this.DoIt$1 = function (vA, vB) {',
  12365. ' };',
  12366. '});',
  12367. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  12368. ' this.DoIt$2 = function (vA) {',
  12369. ' this.DoIt$2(1);',
  12370. ' this.DoIt$3(1, 2);',
  12371. ' $mod.TObject.DoIt.call(this, 1);',
  12372. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  12373. ' };',
  12374. ' this.DoIt$3 = function (vA, vB) {',
  12375. ' };',
  12376. '});',
  12377. '']),
  12378. LinesToStr([ // $mod.$main
  12379. '']));
  12380. end;
  12381. procedure TTestModule.TestClass_OverloadConstructor;
  12382. begin
  12383. StartProgram(false);
  12384. Add('type');
  12385. Add(' TObject = class');
  12386. Add(' constructor Create(vA: longint);');
  12387. Add(' constructor Create(vA, vB: longint);');
  12388. Add(' end;');
  12389. Add(' TCar = class');
  12390. Add(' constructor Create(vA: longint);');
  12391. Add(' constructor Create(vA, vB: longint);');
  12392. Add(' end;');
  12393. Add('constructor tobject.create(va: longint);');
  12394. Add('begin');
  12395. Add(' create(1);');
  12396. Add(' create(1,2);');
  12397. Add('end;');
  12398. Add('constructor tobject.create(va, vb: longint); begin end;');
  12399. Add('constructor tcar.create(va: longint);');
  12400. Add('begin');
  12401. Add(' create(1);');
  12402. Add(' create(1,2);');
  12403. Add(' inherited create(1);');
  12404. Add(' inherited create(1,2);');
  12405. Add('end;');
  12406. Add('constructor tcar.create(va, vb: longint); begin end;');
  12407. Add('begin');
  12408. Add(' tobject.create(1);');
  12409. Add(' tobject.create(1,2);');
  12410. Add(' tcar.create(1);');
  12411. Add(' tcar.create(1,2);');
  12412. ConvertProgram;
  12413. CheckSource('TestClass_OverloadConstructor',
  12414. LinesToStr([ // statements
  12415. 'rtl.createClass($mod, "TObject", null, function () {',
  12416. ' this.$init = function () {',
  12417. ' };',
  12418. ' this.$final = function () {',
  12419. ' };',
  12420. ' this.Create = function (vA) {',
  12421. ' this.Create(1);',
  12422. ' this.Create$1(1,2);',
  12423. ' return this;',
  12424. ' };',
  12425. ' this.Create$1 = function (vA, vB) {',
  12426. ' return this;',
  12427. ' };',
  12428. '});',
  12429. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  12430. ' this.Create$2 = function (vA) {',
  12431. ' this.Create$2(1);',
  12432. ' this.Create$3(1, 2);',
  12433. ' $mod.TObject.Create.call(this, 1);',
  12434. ' $mod.TObject.Create$1.call(this, 1, 2);',
  12435. ' return this;',
  12436. ' };',
  12437. ' this.Create$3 = function (vA, vB) {',
  12438. ' return this;',
  12439. ' };',
  12440. '});',
  12441. '']),
  12442. LinesToStr([ // $mod.$main
  12443. '$mod.TObject.$create("Create", [1]);',
  12444. '$mod.TObject.$create("Create$1", [1, 2]);',
  12445. '$mod.TCar.$create("Create$2", [1]);',
  12446. '$mod.TCar.$create("Create$3", [1, 2]);',
  12447. '']));
  12448. end;
  12449. procedure TTestModule.TestClass_OverloadDelphiOverride;
  12450. begin
  12451. StartProgram(false);
  12452. Add([
  12453. '{$mode delphi}',
  12454. 'type',
  12455. ' TObject = class end;',
  12456. ' TBird = class',
  12457. ' function {#a}GetValue: longint; overload; virtual;',
  12458. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  12459. ' end;',
  12460. ' TEagle = class(TBird)',
  12461. ' function {#c}GetValue: longint; overload; override;',
  12462. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  12463. ' end;',
  12464. 'function TBird.GetValue: longint;',
  12465. 'begin',
  12466. ' if 3={@a}GetValue then ;',
  12467. ' if 4={@b}GetValue(5) then ;',
  12468. 'end;',
  12469. 'function TBird.GetValue(AValue: longint): longint;',
  12470. 'begin',
  12471. 'end;',
  12472. 'function TEagle.GetValue: longint;',
  12473. 'begin',
  12474. ' if 13={@c}GetValue then ;',
  12475. ' if 14={@d}GetValue(15) then ;',
  12476. ' if 15=inherited {@a}GetValue then ;',
  12477. ' if 16=inherited {@b}GetValue(17) then ;',
  12478. 'end;',
  12479. 'function TEagle.GetValue(AValue: longint): longint;',
  12480. 'begin',
  12481. 'end;',
  12482. 'var',
  12483. ' e: TEagle;',
  12484. 'begin',
  12485. ' if 23=e.{@c}GetValue then ;',
  12486. ' if 24=e.{@d}GetValue(25) then ;']);
  12487. ConvertProgram;
  12488. CheckSource('TestClass_OverloadDelphiOverride',
  12489. LinesToStr([ // statements
  12490. 'rtl.createClass($mod, "TObject", null, function () {',
  12491. ' this.$init = function () {',
  12492. ' };',
  12493. ' this.$final = function () {',
  12494. ' };',
  12495. '});',
  12496. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  12497. ' this.GetValue = function () {',
  12498. ' var Result = 0;',
  12499. ' if (3 === this.GetValue()) ;',
  12500. ' if (4 === this.GetValue$1(5)) ;',
  12501. ' return Result;',
  12502. ' };',
  12503. ' this.GetValue$1 = function (AValue) {',
  12504. ' var Result = 0;',
  12505. ' return Result;',
  12506. ' };',
  12507. '});',
  12508. 'rtl.createClass($mod, "TEagle", $mod.TBird, function () {',
  12509. ' this.GetValue = function () {',
  12510. ' var Result = 0;',
  12511. ' if (13 === this.GetValue()) ;',
  12512. ' if (14 === this.GetValue$1(15)) ;',
  12513. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  12514. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  12515. ' return Result;',
  12516. ' };',
  12517. ' this.GetValue$1 = function (AValue) {',
  12518. ' var Result = 0;',
  12519. ' return Result;',
  12520. ' };',
  12521. '});',
  12522. 'this.e = null;',
  12523. '']),
  12524. LinesToStr([ // $mod.$main
  12525. 'if (23 === $mod.e.GetValue()) ;',
  12526. 'if (24 === $mod.e.GetValue$1(25)) ;',
  12527. '']));
  12528. end;
  12529. procedure TTestModule.TestClass_ReintroducedVar;
  12530. begin
  12531. StartProgram(false);
  12532. Add('type');
  12533. Add(' TObject = class');
  12534. Add(' strict private');
  12535. Add(' Some: longint;');
  12536. Add(' end;');
  12537. Add(' TMobile = class');
  12538. Add(' strict private');
  12539. Add(' Some: string;');
  12540. Add(' end;');
  12541. Add(' TCar = class(tmobile)');
  12542. Add(' procedure Some;');
  12543. Add(' procedure Some(vA: longint);');
  12544. Add(' end;');
  12545. Add('procedure tcar.some;');
  12546. Add('begin');
  12547. Add(' Some;');
  12548. Add(' Some(1);');
  12549. Add('end;');
  12550. Add('procedure tcar.some(va: longint); begin end;');
  12551. Add('begin');
  12552. ConvertProgram;
  12553. CheckSource('TestClass_ReintroducedVar',
  12554. LinesToStr([ // statements
  12555. 'rtl.createClass($mod, "TObject", null, function () {',
  12556. ' this.$init = function () {',
  12557. ' this.Some = 0;',
  12558. ' };',
  12559. ' this.$final = function () {',
  12560. ' };',
  12561. '});',
  12562. 'rtl.createClass($mod, "TMobile", $mod.TObject, function () {',
  12563. ' this.$init = function () {',
  12564. ' $mod.TObject.$init.call(this);',
  12565. ' this.Some$1 = "";',
  12566. ' };',
  12567. '});',
  12568. 'rtl.createClass($mod, "TCar", $mod.TMobile, function () {',
  12569. ' this.Some$2 = function () {',
  12570. ' this.Some$2();',
  12571. ' this.Some$3(1);',
  12572. ' };',
  12573. ' this.Some$3 = function (vA) {',
  12574. ' };',
  12575. '});',
  12576. '']),
  12577. LinesToStr([ // $mod.$main
  12578. '']));
  12579. end;
  12580. procedure TTestModule.TestClass_RaiseDescendant;
  12581. begin
  12582. StartProgram(false);
  12583. Add([
  12584. 'type',
  12585. ' TObject = class',
  12586. ' constructor Create(Msg: string);',
  12587. ' end;',
  12588. ' Exception = class',
  12589. ' end;',
  12590. ' EConvertError = class(Exception)',
  12591. ' end;',
  12592. 'constructor TObject.Create(Msg: string); begin end;',
  12593. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  12594. 'begin',
  12595. ' raise Exception.Create(''Bar1'');',
  12596. ' raise EConvertError.Create(''Bar2'');',
  12597. ' raise AssertConv(''Bar2'');',
  12598. ' raise AssertConv;',
  12599. '']);
  12600. ConvertProgram;
  12601. CheckSource('TestClass_RaiseDescendant',
  12602. LinesToStr([ // statements
  12603. 'rtl.createClass($mod, "TObject", null, function () {',
  12604. ' this.$init = function () {',
  12605. ' };',
  12606. ' this.$final = function () {',
  12607. ' };',
  12608. ' this.Create = function (Msg) {',
  12609. ' return this;',
  12610. ' };',
  12611. '});',
  12612. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  12613. '});',
  12614. 'rtl.createClass($mod, "EConvertError", $mod.Exception, function () {',
  12615. '});',
  12616. 'this.AssertConv = function (Msg) {',
  12617. ' var Result = null;',
  12618. ' return Result;',
  12619. '};',
  12620. '']),
  12621. LinesToStr([ // $mod.$main
  12622. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  12623. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  12624. 'throw $mod.AssertConv("Bar2");',
  12625. 'throw $mod.AssertConv("def");',
  12626. '']));
  12627. end;
  12628. procedure TTestModule.TestClass_ExternalMethod;
  12629. begin
  12630. AddModuleWithIntfImplSrc('unit2.pas',
  12631. LinesToStr([
  12632. 'type',
  12633. ' TObject = class',
  12634. ' public',
  12635. ' procedure Intern; external name ''$DoIntern'';',
  12636. ' end;',
  12637. '']),
  12638. LinesToStr([
  12639. '']));
  12640. StartUnit(true);
  12641. Add('interface');
  12642. Add('uses unit2;');
  12643. Add('type');
  12644. Add(' TCar = class(TObject)');
  12645. Add(' public');
  12646. Add(' procedure Intern2; external name ''$DoIntern2'';');
  12647. Add(' procedure DoIt;');
  12648. Add(' end;');
  12649. Add('implementation');
  12650. Add('procedure tcar.doit;');
  12651. Add('begin');
  12652. Add(' Intern;');
  12653. Add(' Intern();');
  12654. Add(' Intern2;');
  12655. Add(' Intern2();');
  12656. Add('end;');
  12657. Add('var Obj: TCar;');
  12658. Add('begin');
  12659. Add(' obj.intern;');
  12660. Add(' obj.intern();');
  12661. Add(' obj.intern2;');
  12662. Add(' obj.intern2();');
  12663. Add(' obj.doit;');
  12664. Add(' obj.doit();');
  12665. Add(' with obj do begin');
  12666. Add(' Intern;');
  12667. Add(' Intern();');
  12668. Add(' Intern2;');
  12669. Add(' Intern2();');
  12670. Add(' end;');
  12671. ConvertUnit;
  12672. CheckSource('TestClass_ExternalMethod',
  12673. LinesToStr([
  12674. 'var $impl = $mod.$impl;',
  12675. 'rtl.createClass($mod, "TCar", pas.unit2.TObject, function () {',
  12676. ' this.DoIt = function () {',
  12677. ' this.$DoIntern();',
  12678. ' this.$DoIntern();',
  12679. ' this.$DoIntern2();',
  12680. ' this.$DoIntern2();',
  12681. ' };',
  12682. ' });',
  12683. '']),
  12684. LinesToStr([ // this.$init
  12685. '$impl.Obj.$DoIntern();',
  12686. '$impl.Obj.$DoIntern();',
  12687. '$impl.Obj.$DoIntern2();',
  12688. '$impl.Obj.$DoIntern2();',
  12689. '$impl.Obj.DoIt();',
  12690. '$impl.Obj.DoIt();',
  12691. 'var $with1 = $impl.Obj;',
  12692. '$with1.$DoIntern();',
  12693. '$with1.$DoIntern();',
  12694. '$with1.$DoIntern2();',
  12695. '$with1.$DoIntern2();',
  12696. '']),
  12697. LinesToStr([ // implementation
  12698. '$impl.Obj = null;',
  12699. '']) );
  12700. end;
  12701. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  12702. begin
  12703. StartProgram(false);
  12704. Add('type');
  12705. Add(' TObject = class');
  12706. Add(' procedure DoIt; virtual; external name ''Foo'';');
  12707. Add(' end;');
  12708. Add('begin');
  12709. SetExpectedPasResolverError('Virtual method name must match external',
  12710. nVirtualMethodNameMustMatchExternal);
  12711. ConvertProgram;
  12712. end;
  12713. procedure TTestModule.TestClass_ExternalOverrideFail;
  12714. begin
  12715. StartProgram(false);
  12716. Add('type');
  12717. Add(' TObject = class');
  12718. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  12719. Add(' end;');
  12720. Add(' TCar = class');
  12721. Add(' procedure DoIt; override; external name ''DoIt'';');
  12722. Add(' end;');
  12723. Add('begin');
  12724. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  12725. nInvalidXModifierY);
  12726. ConvertProgram;
  12727. end;
  12728. procedure TTestModule.TestClass_ExternalVar;
  12729. begin
  12730. AddModuleWithIntfImplSrc('unit2.pas',
  12731. LinesToStr([
  12732. '{$modeswitch externalclass}',
  12733. 'type',
  12734. ' TObject = class',
  12735. ' public',
  12736. ' Intern: longint external name ''$Intern'';',
  12737. ' Bracket: longint external name ''["A B"]'';',
  12738. ' end;',
  12739. '']),
  12740. LinesToStr([
  12741. '']));
  12742. StartUnit(true);
  12743. Add([
  12744. 'interface',
  12745. 'uses unit2;',
  12746. '{$modeswitch externalclass}',
  12747. 'type',
  12748. ' TCar = class(tobject)',
  12749. ' public',
  12750. ' Intern2: longint external name ''$Intern2'';',
  12751. ' procedure DoIt;',
  12752. ' end;',
  12753. 'implementation',
  12754. 'procedure tcar.doit;',
  12755. 'begin',
  12756. ' Intern:=Intern+1;',
  12757. ' Intern2:=Intern2+2;',
  12758. ' Bracket:=Bracket+3;',
  12759. 'end;',
  12760. 'var Obj: TCar;',
  12761. 'begin',
  12762. ' obj.intern:=obj.intern+1;',
  12763. ' obj.intern2:=obj.intern2+2;',
  12764. ' obj.Bracket:=obj.Bracket+3;',
  12765. ' with obj do begin',
  12766. ' intern:=intern+1;',
  12767. ' intern2:=intern2+2;',
  12768. ' Bracket:=Bracket+3;',
  12769. ' end;']);
  12770. ConvertUnit;
  12771. CheckSource('TestClass_ExternalVar',
  12772. LinesToStr([
  12773. 'var $impl = $mod.$impl;',
  12774. 'rtl.createClass($mod, "TCar", pas.unit2.TObject, function () {',
  12775. ' this.DoIt = function () {',
  12776. ' this.$Intern = this.$Intern + 1;',
  12777. ' this.$Intern2 = this.$Intern2 + 2;',
  12778. ' this["A B"] = this["A B"] + 3;',
  12779. ' };',
  12780. ' });',
  12781. '']),
  12782. LinesToStr([
  12783. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  12784. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  12785. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  12786. 'var $with1 = $impl.Obj;',
  12787. '$with1.$Intern = $with1.$Intern + 1;',
  12788. '$with1.$Intern2 = $with1.$Intern2 + 2;',
  12789. '$with1["A B"] = $with1["A B"] + 3;',
  12790. '']),
  12791. LinesToStr([ // implementation
  12792. '$impl.Obj = null;',
  12793. '']));
  12794. end;
  12795. procedure TTestModule.TestClass_Const;
  12796. begin
  12797. StartProgram(false);
  12798. Add('type');
  12799. Add(' integer = longint;');
  12800. Add(' TClass = class of TObject;');
  12801. Add(' TObject = class');
  12802. Add(' public');
  12803. Add(' const cI: integer = 3;');
  12804. Add(' procedure DoIt;');
  12805. Add(' class procedure DoMore;');
  12806. Add(' end;');
  12807. Add('implementation');
  12808. Add('procedure tobject.doit;');
  12809. Add('begin');
  12810. Add(' if cI=4 then;');
  12811. Add(' if 5=cI then;');
  12812. Add(' if Self.cI=6 then;');
  12813. Add(' if 7=Self.cI then;');
  12814. Add(' with Self do begin');
  12815. Add(' if cI=11 then;');
  12816. Add(' if 12=cI then;');
  12817. Add(' end;');
  12818. Add('end;');
  12819. Add('class procedure tobject.domore;');
  12820. Add('begin');
  12821. Add(' if cI=8 then;');
  12822. Add(' if Self.cI=9 then;');
  12823. Add(' if 10=cI then;');
  12824. Add(' if 11=Self.cI then;');
  12825. Add(' with Self do begin');
  12826. Add(' if cI=13 then;');
  12827. Add(' if 14=cI then;');
  12828. Add(' end;');
  12829. Add('end;');
  12830. Add('var');
  12831. Add(' Obj: TObject;');
  12832. Add(' Cla: TClass;');
  12833. Add('begin');
  12834. Add(' if TObject.cI=21 then ;');
  12835. Add(' if Obj.cI=22 then ;');
  12836. Add(' if Cla.cI=23 then ;');
  12837. Add(' with obj do if ci=24 then;');
  12838. Add(' with TObject do if ci=25 then;');
  12839. Add(' with Cla do if ci=26 then;');
  12840. ConvertProgram;
  12841. CheckSource('TestClass_Const',
  12842. LinesToStr([
  12843. 'rtl.createClass($mod, "TObject", null, function () {',
  12844. ' this.cI = 3;',
  12845. ' this.$init = function () {',
  12846. ' };',
  12847. ' this.$final = function () {',
  12848. ' };',
  12849. ' this.DoIt = function () {',
  12850. ' if (this.cI === 4) ;',
  12851. ' if (5 === this.cI) ;',
  12852. ' if (this.cI === 6) ;',
  12853. ' if (7 === this.cI) ;',
  12854. ' if (this.cI === 11) ;',
  12855. ' if (12 === this.cI) ;',
  12856. ' };',
  12857. ' this.DoMore = function () {',
  12858. ' if (this.cI === 8) ;',
  12859. ' if (this.cI === 9) ;',
  12860. ' if (10 === this.cI) ;',
  12861. ' if (11 === this.cI) ;',
  12862. ' if (this.cI === 13) ;',
  12863. ' if (14 === this.cI) ;',
  12864. ' };',
  12865. '});',
  12866. 'this.Obj = null;',
  12867. 'this.Cla = null;',
  12868. '']),
  12869. LinesToStr([
  12870. 'if ($mod.TObject.cI === 21) ;',
  12871. 'if ($mod.Obj.cI === 22) ;',
  12872. 'if ($mod.Cla.cI === 23) ;',
  12873. 'var $with1 = $mod.Obj;',
  12874. 'if ($with1.cI === 24) ;',
  12875. 'var $with2 = $mod.TObject;',
  12876. 'if ($with2.cI === 25) ;',
  12877. 'var $with3 = $mod.Cla;',
  12878. 'if ($with3.cI === 26) ;',
  12879. '']));
  12880. end;
  12881. procedure TTestModule.TestClass_LocalVarSelfFail;
  12882. begin
  12883. StartProgram(false);
  12884. Add([
  12885. 'type',
  12886. ' TObject = class',
  12887. ' constructor Create;',
  12888. ' end;',
  12889. 'constructor tobject.create;',
  12890. 'var self: longint;',
  12891. 'begin',
  12892. 'end',
  12893. 'begin',
  12894. '']);
  12895. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  12896. ConvertProgram;
  12897. end;
  12898. procedure TTestModule.TestClass_ArgSelfFail;
  12899. begin
  12900. StartProgram(false);
  12901. Add([
  12902. 'type',
  12903. ' TObject = class',
  12904. ' procedure DoIt(Self: longint);',
  12905. ' end;',
  12906. 'procedure tobject.doit(self: longint);',
  12907. 'begin',
  12908. 'end',
  12909. 'begin',
  12910. '']);
  12911. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  12912. ConvertProgram;
  12913. end;
  12914. procedure TTestModule.TestClass_NestedProcSelf;
  12915. begin
  12916. StartProgram(false);
  12917. Add([
  12918. 'type',
  12919. ' TObject = class',
  12920. ' Key: longint;',
  12921. ' class var State: longint;',
  12922. ' procedure DoIt;',
  12923. ' function GetSize: longint; virtual; abstract;',
  12924. ' procedure SetSize(Value: longint); virtual; abstract;',
  12925. ' property Size: longint read GetSize write SetSize;',
  12926. ' end;',
  12927. 'procedure tobject.doit;',
  12928. ' procedure Sub;',
  12929. ' begin',
  12930. ' key:=key+2;',
  12931. ' self.key:=self.key+3;',
  12932. ' state:=state+4;',
  12933. ' self.state:=self.state+5;',
  12934. ' tobject.state:=tobject.state+6;',
  12935. ' size:=size+7;',
  12936. ' self.size:=self.size+8;',
  12937. ' end;',
  12938. 'begin',
  12939. ' sub;',
  12940. ' key:=key+12;',
  12941. ' self.key:=self.key+13;',
  12942. ' state:=state+14;',
  12943. ' self.state:=self.state+15;',
  12944. ' tobject.state:=tobject.state+16;',
  12945. ' size:=size+17;',
  12946. ' self.size:=self.size+18;',
  12947. 'end;',
  12948. 'begin',
  12949. '']);
  12950. ConvertProgram;
  12951. CheckSource('TestClass_NestedProcSelf',
  12952. LinesToStr([ // statements
  12953. 'rtl.createClass($mod, "TObject", null, function () {',
  12954. ' this.State = 0;',
  12955. ' this.$init = function () {',
  12956. ' this.Key = 0;',
  12957. ' };',
  12958. ' this.$final = function () {',
  12959. ' };',
  12960. ' this.DoIt = function () {',
  12961. ' var Self = this;',
  12962. ' function Sub() {',
  12963. ' Self.Key = Self.Key + 2;',
  12964. ' Self.Key = Self.Key + 3;',
  12965. ' $mod.TObject.State = Self.State + 4;',
  12966. ' $mod.TObject.State = Self.State + 5;',
  12967. ' $mod.TObject.State = $mod.TObject.State + 6;',
  12968. ' Self.SetSize(Self.GetSize() + 7);',
  12969. ' Self.SetSize(Self.GetSize() + 8);',
  12970. ' };',
  12971. ' Sub();',
  12972. ' Self.Key = Self.Key + 12;',
  12973. ' Self.Key = Self.Key + 13;',
  12974. ' $mod.TObject.State = Self.State + 14;',
  12975. ' $mod.TObject.State = Self.State + 15;',
  12976. ' $mod.TObject.State = $mod.TObject.State + 16;',
  12977. ' Self.SetSize(Self.GetSize() + 17);',
  12978. ' Self.SetSize(Self.GetSize() + 18);',
  12979. ' };',
  12980. '});',
  12981. '']),
  12982. LinesToStr([ // $mod.$main
  12983. '']));
  12984. end;
  12985. procedure TTestModule.TestClass_NestedProcSelf2;
  12986. begin
  12987. StartProgram(false);
  12988. Add([
  12989. 'type',
  12990. ' TObject = class',
  12991. ' Key: longint;',
  12992. ' class var State: longint;',
  12993. ' function GetSize: longint; virtual; abstract;',
  12994. ' procedure SetSize(Value: longint); virtual; abstract;',
  12995. ' property Size: longint read GetSize write SetSize;',
  12996. ' end;',
  12997. ' TBird = class',
  12998. ' procedure DoIt;',
  12999. ' end;',
  13000. 'procedure tbird.doit;',
  13001. ' procedure Sub;',
  13002. ' begin',
  13003. ' key:=key+2;',
  13004. ' self.key:=self.key+3;',
  13005. ' state:=state+4;',
  13006. ' self.state:=self.state+5;',
  13007. ' tobject.state:=tobject.state+6;',
  13008. ' size:=size+7;',
  13009. ' self.size:=self.size+8;',
  13010. ' end;',
  13011. 'begin',
  13012. ' sub;',
  13013. ' key:=key+12;',
  13014. ' self.key:=self.key+13;',
  13015. ' state:=state+14;',
  13016. ' self.state:=self.state+15;',
  13017. ' tobject.state:=tobject.state+16;',
  13018. ' size:=size+17;',
  13019. ' self.size:=self.size+18;',
  13020. 'end;',
  13021. 'begin',
  13022. '']);
  13023. ConvertProgram;
  13024. CheckSource('TestClass_NestedProcSelf2',
  13025. LinesToStr([ // statements
  13026. 'rtl.createClass($mod, "TObject", null, function () {',
  13027. ' this.State = 0;',
  13028. ' this.$init = function () {',
  13029. ' this.Key = 0;',
  13030. ' };',
  13031. ' this.$final = function () {',
  13032. ' };',
  13033. '});',
  13034. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  13035. ' this.DoIt = function () {',
  13036. ' var Self = this;',
  13037. ' function Sub() {',
  13038. ' Self.Key = Self.Key + 2;',
  13039. ' Self.Key = Self.Key + 3;',
  13040. ' $mod.TObject.State = Self.State + 4;',
  13041. ' $mod.TObject.State = Self.State + 5;',
  13042. ' $mod.TObject.State = $mod.TObject.State + 6;',
  13043. ' Self.SetSize(Self.GetSize() + 7);',
  13044. ' Self.SetSize(Self.GetSize() + 8);',
  13045. ' };',
  13046. ' Sub();',
  13047. ' Self.Key = Self.Key + 12;',
  13048. ' Self.Key = Self.Key + 13;',
  13049. ' $mod.TObject.State = Self.State + 14;',
  13050. ' $mod.TObject.State = Self.State + 15;',
  13051. ' $mod.TObject.State = $mod.TObject.State + 16;',
  13052. ' Self.SetSize(Self.GetSize() + 17);',
  13053. ' Self.SetSize(Self.GetSize() + 18);',
  13054. ' };',
  13055. '});',
  13056. '']),
  13057. LinesToStr([ // $mod.$main
  13058. '']));
  13059. end;
  13060. procedure TTestModule.TestClass_NestedProcClassSelf;
  13061. begin
  13062. StartProgram(false);
  13063. Add([
  13064. 'type',
  13065. ' TObject = class',
  13066. ' class var State: longint;',
  13067. ' class procedure DoIt;',
  13068. ' class function GetSize: longint; virtual; abstract;',
  13069. ' class procedure SetSize(Value: longint); virtual; abstract;',
  13070. ' class property Size: longint read GetSize write SetSize;',
  13071. ' end;',
  13072. 'class procedure tobject.doit;',
  13073. ' procedure Sub;',
  13074. ' begin',
  13075. ' state:=state+2;',
  13076. ' self.state:=self.state+3;',
  13077. ' tobject.state:=tobject.state+4;',
  13078. ' size:=size+5;',
  13079. ' self.size:=self.size+6;',
  13080. ' tobject.size:=tobject.size+7;',
  13081. ' end;',
  13082. 'begin',
  13083. ' sub;',
  13084. ' state:=state+12;',
  13085. ' self.state:=self.state+13;',
  13086. ' tobject.state:=tobject.state+14;',
  13087. ' size:=size+15;',
  13088. ' self.size:=self.size+16;',
  13089. ' tobject.size:=tobject.size+17;',
  13090. 'end;',
  13091. 'begin',
  13092. '']);
  13093. ConvertProgram;
  13094. CheckSource('TestClass_NestedProcClassSelf',
  13095. LinesToStr([ // statements
  13096. 'rtl.createClass($mod, "TObject", null, function () {',
  13097. ' this.State = 0;',
  13098. ' this.$init = function () {',
  13099. ' };',
  13100. ' this.$final = function () {',
  13101. ' };',
  13102. ' this.DoIt = function () {',
  13103. ' var Self = this;',
  13104. ' function Sub() {',
  13105. ' $mod.TObject.State = Self.State + 2;',
  13106. ' $mod.TObject.State = Self.State + 3;',
  13107. ' $mod.TObject.State = $mod.TObject.State + 4;',
  13108. ' Self.SetSize(Self.GetSize() + 5);',
  13109. ' Self.SetSize(Self.GetSize() + 6);',
  13110. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  13111. ' };',
  13112. ' Sub();',
  13113. ' $mod.TObject.State = Self.State + 12;',
  13114. ' $mod.TObject.State = Self.State + 13;',
  13115. ' $mod.TObject.State = $mod.TObject.State + 14;',
  13116. ' Self.SetSize(Self.GetSize() + 15);',
  13117. ' Self.SetSize(Self.GetSize() + 16);',
  13118. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  13119. ' };',
  13120. '});',
  13121. '']),
  13122. LinesToStr([ // $mod.$main
  13123. '']));
  13124. end;
  13125. procedure TTestModule.TestClass_NestedProcCallInherited;
  13126. begin
  13127. StartProgram(false);
  13128. Add([
  13129. 'type',
  13130. ' TObject = class',
  13131. ' function DoIt(k: boolean): longint; virtual;',
  13132. ' end;',
  13133. ' TBird = class',
  13134. ' function DoIt(k: boolean): longint; override;',
  13135. ' end;',
  13136. 'function tobject.doit(k: boolean): longint;',
  13137. 'begin',
  13138. 'end;',
  13139. 'function tbird.doit(k: boolean): longint;',
  13140. ' procedure Sub;',
  13141. ' begin',
  13142. ' inherited DoIt(true);',
  13143. //' if inherited DoIt(false)=4 then ;',
  13144. ' end;',
  13145. 'begin',
  13146. ' Sub;',
  13147. ' inherited;',
  13148. ' inherited DoIt(true);',
  13149. //' if inherited DoIt(false)=14 then ;',
  13150. 'end;',
  13151. 'begin',
  13152. '']);
  13153. ConvertProgram;
  13154. CheckSource('TestClass_NestedProcCallInherited',
  13155. LinesToStr([ // statements
  13156. 'rtl.createClass($mod, "TObject", null, function () {',
  13157. ' this.$init = function () {',
  13158. ' };',
  13159. ' this.$final = function () {',
  13160. ' };',
  13161. ' this.DoIt = function (k) {',
  13162. ' var Result = 0;',
  13163. ' return Result;',
  13164. ' };',
  13165. '});',
  13166. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  13167. ' this.DoIt = function (k) {',
  13168. ' var Self = this;',
  13169. ' var Result = 0;',
  13170. ' function Sub() {',
  13171. ' $mod.TObject.DoIt.call(Self, true);',
  13172. ' };',
  13173. ' Sub();',
  13174. ' $mod.TObject.DoIt.apply(Self, arguments);',
  13175. ' $mod.TObject.DoIt.call(Self, true);',
  13176. ' return Result;',
  13177. ' };',
  13178. '});',
  13179. '']),
  13180. LinesToStr([ // $mod.$main
  13181. '']));
  13182. end;
  13183. procedure TTestModule.TestClass_TObjectFree;
  13184. begin
  13185. StartProgram(false);
  13186. Add([
  13187. 'type',
  13188. ' TObject = class',
  13189. ' Obj: tobject;',
  13190. ' procedure Free;',
  13191. ' procedure Release;',
  13192. ' end;',
  13193. 'procedure tobject.free;',
  13194. 'begin',
  13195. 'end;',
  13196. 'procedure tobject.release;',
  13197. 'begin',
  13198. ' free;',
  13199. ' if true then free;',
  13200. 'end;',
  13201. 'function DoIt(o: tobject): tobject;',
  13202. 'var l: tobject;',
  13203. 'begin',
  13204. ' o.free;',
  13205. ' o.free();',
  13206. ' l.free;',
  13207. ' l.free();',
  13208. ' o.obj.free;',
  13209. ' o.obj.free();',
  13210. ' with o do obj.free;',
  13211. ' with o do obj.free();',
  13212. ' result.Free;',
  13213. ' result.Free();',
  13214. 'end;',
  13215. 'var o: tobject;',
  13216. ' a: array of tobject;',
  13217. 'begin',
  13218. ' o.free;',
  13219. ' o.obj.free;',
  13220. ' a[1+2].free;',
  13221. '']);
  13222. ConvertProgram;
  13223. CheckSource('TestClass_TObjectFree',
  13224. LinesToStr([ // statements
  13225. 'rtl.createClass($mod, "TObject", null, function () {',
  13226. ' this.$init = function () {',
  13227. ' this.Obj = null;',
  13228. ' };',
  13229. ' this.$final = function () {',
  13230. ' this.Obj = undefined;',
  13231. ' };',
  13232. ' this.Free = function () {',
  13233. ' };',
  13234. ' this.Release = function () {',
  13235. ' this.Free();',
  13236. ' if (true) this.Free();',
  13237. ' };',
  13238. '});',
  13239. 'this.DoIt = function (o) {',
  13240. ' var Result = null;',
  13241. ' var l = null;',
  13242. ' o = rtl.freeLoc(o);',
  13243. ' o = rtl.freeLoc(o);',
  13244. ' l = rtl.freeLoc(l);',
  13245. ' l = rtl.freeLoc(l);',
  13246. ' rtl.free(o, "Obj");',
  13247. ' rtl.free(o, "Obj");',
  13248. ' rtl.free(o, "Obj");',
  13249. ' rtl.free(o, "Obj");',
  13250. ' Result = rtl.freeLoc(Result);',
  13251. ' Result = rtl.freeLoc(Result);',
  13252. ' return Result;',
  13253. '};',
  13254. 'this.o = null;',
  13255. 'this.a = [];',
  13256. '']),
  13257. LinesToStr([ // $mod.$main
  13258. 'rtl.free($mod, "o");',
  13259. 'rtl.free($mod.o, "Obj");',
  13260. 'rtl.free($mod.a, 1 + 2);',
  13261. '']));
  13262. end;
  13263. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  13264. begin
  13265. StartProgram(false);
  13266. Add([
  13267. 'type',
  13268. ' TObject = class',
  13269. ' constructor Create;',
  13270. ' procedure Free;',
  13271. ' end;',
  13272. 'constructor TObject.Create; begin end;',
  13273. 'procedure tobject.free; begin end;',
  13274. 'begin',
  13275. ' with tobject.create do free;',
  13276. '']);
  13277. ConvertProgram;
  13278. CheckSource('TestClass_TObjectFreeNewInstance',
  13279. LinesToStr([ // statements
  13280. 'rtl.createClass($mod, "TObject", null, function () {',
  13281. ' this.$init = function () {',
  13282. ' };',
  13283. ' this.$final = function () {',
  13284. ' };',
  13285. ' this.Create = function () {',
  13286. ' return this;',
  13287. ' };',
  13288. ' this.Free = function () {',
  13289. ' };',
  13290. '});',
  13291. '']),
  13292. LinesToStr([ // $mod.$main
  13293. 'var $with1 = $mod.TObject.$create("Create");',
  13294. '$with1=rtl.freeLoc($with1);',
  13295. '']));
  13296. end;
  13297. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  13298. begin
  13299. StartProgram(false);
  13300. Add([
  13301. 'type',
  13302. ' TObject = class',
  13303. ' destructor Destroy;',
  13304. ' procedure Free;',
  13305. ' end;',
  13306. 'destructor TObject.Destroy; begin end;',
  13307. 'procedure tobject.free; begin end;',
  13308. 'var o: tobject;',
  13309. 'begin',
  13310. ' o.free;',
  13311. '']);
  13312. Converter.UseLowerCase:=true;
  13313. ConvertProgram;
  13314. CheckSource('TestClass_TObjectFreeLowerCase',
  13315. LinesToStr([ // statements
  13316. 'rtl.createClass($mod, "tobject", null, function () {',
  13317. ' this.$init = function () {',
  13318. ' };',
  13319. ' this.$final = function () {',
  13320. ' };',
  13321. ' rtl.tObjectDestroy = "destroy";',
  13322. ' this.destroy = function () {',
  13323. ' };',
  13324. ' this.free = function () {',
  13325. ' };',
  13326. '});',
  13327. 'this.o = null;',
  13328. '']),
  13329. LinesToStr([ // $mod.$main
  13330. 'rtl.free($mod, "o");',
  13331. '']));
  13332. end;
  13333. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  13334. begin
  13335. StartProgram(false);
  13336. Add([
  13337. 'type',
  13338. ' TObject = class',
  13339. ' procedure Free;',
  13340. ' function GetObj: tobject; virtual; abstract;',
  13341. ' end;',
  13342. 'procedure tobject.free;',
  13343. 'begin',
  13344. 'end;',
  13345. 'var o: tobject;',
  13346. 'begin',
  13347. ' o.getobj.free;',
  13348. '']);
  13349. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  13350. ConvertProgram;
  13351. end;
  13352. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  13353. begin
  13354. StartProgram(false);
  13355. Add([
  13356. 'type',
  13357. ' TObject = class',
  13358. ' procedure Free;',
  13359. ' FObj: TObject;',
  13360. ' property Obj: tobject read FObj write FObj;',
  13361. ' end;',
  13362. 'procedure tobject.free;',
  13363. 'begin',
  13364. 'end;',
  13365. 'var o: tobject;',
  13366. 'begin',
  13367. ' o.obj.free;',
  13368. '']);
  13369. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  13370. ConvertProgram;
  13371. end;
  13372. procedure TTestModule.TestClass_ForIn;
  13373. begin
  13374. StartProgram(false);
  13375. Add([
  13376. 'type',
  13377. ' TObject = class end;',
  13378. ' TItem = TObject;',
  13379. ' TEnumerator = class',
  13380. ' FCurrent: TItem;',
  13381. ' property Current: TItem read FCurrent;',
  13382. ' function MoveNext: boolean;',
  13383. ' end;',
  13384. ' TBird = class',
  13385. ' function GetEnumerator: TEnumerator;',
  13386. ' end;',
  13387. 'function TEnumerator.MoveNext: boolean;',
  13388. 'begin',
  13389. 'end;',
  13390. 'function TBird.GetEnumerator: TEnumerator;',
  13391. 'begin',
  13392. 'end;',
  13393. 'var',
  13394. ' b: TBird;',
  13395. ' i, i2: TItem;',
  13396. 'begin',
  13397. ' for i in b do i2:=i;']);
  13398. ConvertProgram;
  13399. CheckSource('TestClass_ForIn',
  13400. LinesToStr([ // statements
  13401. 'rtl.createClass($mod, "TObject", null, function () {',
  13402. ' this.$init = function () {',
  13403. ' };',
  13404. ' this.$final = function () {',
  13405. ' };',
  13406. '});',
  13407. 'rtl.createClass($mod, "TEnumerator", $mod.TObject, function () {',
  13408. ' this.$init = function () {',
  13409. ' $mod.TObject.$init.call(this);',
  13410. ' this.FCurrent = null;',
  13411. ' };',
  13412. ' this.$final = function () {',
  13413. ' this.FCurrent = undefined;',
  13414. ' $mod.TObject.$final.call(this);',
  13415. ' };',
  13416. ' this.MoveNext = function () {',
  13417. ' var Result = false;',
  13418. ' return Result;',
  13419. ' };',
  13420. '});',
  13421. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  13422. ' this.GetEnumerator = function () {',
  13423. ' var Result = null;',
  13424. ' return Result;',
  13425. ' };',
  13426. '});',
  13427. 'this.b = null;',
  13428. 'this.i = null;',
  13429. 'this.i2 = null;'
  13430. ]),
  13431. LinesToStr([ // $mod.$main
  13432. 'var $in1 = $mod.b.GetEnumerator();',
  13433. 'try {',
  13434. ' while ($in1.MoveNext()){',
  13435. ' $mod.i = $in1.FCurrent;',
  13436. ' $mod.i2 = $mod.i;',
  13437. ' }',
  13438. '} finally {',
  13439. ' $in1 = rtl.freeLoc($in1)',
  13440. '};',
  13441. '']));
  13442. end;
  13443. procedure TTestModule.TestClassOf_Create;
  13444. begin
  13445. StartProgram(false);
  13446. Add('type');
  13447. Add(' TObject = class');
  13448. Add(' constructor Create;');
  13449. Add(' end;');
  13450. Add(' TClass = class of TObject;');
  13451. Add('constructor tobject.create; begin end;');
  13452. Add('var');
  13453. Add(' Obj: tobject;');
  13454. Add(' C: tclass;');
  13455. Add('begin');
  13456. Add(' obj:=C.create;');
  13457. Add(' with c do obj:=create;');
  13458. ConvertProgram;
  13459. CheckSource('TestClassOf_Create',
  13460. LinesToStr([ // statements
  13461. 'rtl.createClass($mod, "TObject", null, function () {',
  13462. ' this.$init = function () {',
  13463. ' };',
  13464. ' this.$final = function () {',
  13465. ' };',
  13466. ' this.Create = function () {',
  13467. ' return this;',
  13468. ' };',
  13469. '});',
  13470. 'this.Obj = null;',
  13471. 'this.C = null;'
  13472. ]),
  13473. LinesToStr([ // $mod.$main
  13474. '$mod.Obj = $mod.C.$create("Create");',
  13475. 'var $with1 = $mod.C;',
  13476. '$mod.Obj = $with1.$create("Create");',
  13477. '']));
  13478. end;
  13479. procedure TTestModule.TestClassOf_Call;
  13480. begin
  13481. StartProgram(false);
  13482. Add('type');
  13483. Add(' TObject = class');
  13484. Add(' class procedure DoIt;');
  13485. Add(' end;');
  13486. Add(' TClass = class of TObject;');
  13487. Add('class procedure tobject.doit; begin end;');
  13488. Add('var');
  13489. Add(' C: tclass;');
  13490. Add('begin');
  13491. Add(' c.doit;');
  13492. Add(' with c do doit;');
  13493. ConvertProgram;
  13494. CheckSource('TestClassOf_Call',
  13495. LinesToStr([ // statements
  13496. 'rtl.createClass($mod, "TObject", null, function () {',
  13497. ' this.$init = function () {',
  13498. ' };',
  13499. ' this.$final = function () {',
  13500. ' };',
  13501. ' this.DoIt = function () {',
  13502. ' };',
  13503. '});',
  13504. 'this.C = null;'
  13505. ]),
  13506. LinesToStr([ // $mod.$main
  13507. '$mod.C.DoIt();',
  13508. 'var $with1 = $mod.C;',
  13509. '$with1.DoIt();',
  13510. '']));
  13511. end;
  13512. procedure TTestModule.TestClassOf_Assign;
  13513. begin
  13514. StartProgram(false);
  13515. Add('type');
  13516. Add(' TClass = class of TObject;');
  13517. Add(' TObject = class');
  13518. Add(' ClassType: TClass; ');
  13519. Add(' end;');
  13520. Add('var');
  13521. Add(' Obj: tobject;');
  13522. Add(' C: tclass;');
  13523. Add('begin');
  13524. Add(' c:=nil;');
  13525. Add(' c:=obj.classtype;');
  13526. ConvertProgram;
  13527. CheckSource('TestClassOf_Assign',
  13528. LinesToStr([ // statements
  13529. 'rtl.createClass($mod, "TObject", null, function () {',
  13530. ' this.$init = function () {',
  13531. ' this.ClassType = null;',
  13532. ' };',
  13533. ' this.$final = function () {',
  13534. ' this.ClassType = undefined;',
  13535. ' };',
  13536. '});',
  13537. 'this.Obj = null;',
  13538. 'this.C = null;'
  13539. ]),
  13540. LinesToStr([ // $mod.$main
  13541. '$mod.C = null;',
  13542. '$mod.C = $mod.Obj.ClassType;',
  13543. '']));
  13544. end;
  13545. procedure TTestModule.TestClassOf_Is;
  13546. begin
  13547. StartProgram(false);
  13548. Add('type');
  13549. Add(' TClass = class of TObject;');
  13550. Add(' TObject = class');
  13551. Add(' end;');
  13552. Add(' TCar = class');
  13553. Add(' end;');
  13554. Add(' TCars = class of TCar;');
  13555. Add('var');
  13556. Add(' Obj: tobject;');
  13557. Add(' C: tclass;');
  13558. Add(' Cars: tcars;');
  13559. Add('begin');
  13560. Add(' if c is tcar then ;');
  13561. Add(' if c is tcars then ;');
  13562. ConvertProgram;
  13563. CheckSource('TestClassOf_Is',
  13564. LinesToStr([ // statements
  13565. 'rtl.createClass($mod, "TObject", null, function () {',
  13566. ' this.$init = function () {',
  13567. ' };',
  13568. ' this.$final = function () {',
  13569. ' };',
  13570. '});',
  13571. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  13572. '});',
  13573. 'this.Obj = null;',
  13574. 'this.C = null;',
  13575. 'this.Cars = null;'
  13576. ]),
  13577. LinesToStr([ // $mod.$main
  13578. 'if(rtl.is($mod.C,$mod.TCar));',
  13579. 'if(rtl.is($mod.C,$mod.TCar));',
  13580. '']));
  13581. end;
  13582. procedure TTestModule.TestClassOf_Compare;
  13583. begin
  13584. StartProgram(false);
  13585. Add('type');
  13586. Add(' TClass = class of TObject;');
  13587. Add(' TObject = class');
  13588. Add(' ClassType: TClass; ');
  13589. Add(' end;');
  13590. Add('var');
  13591. Add(' b: boolean;');
  13592. Add(' Obj: tobject;');
  13593. Add(' C: tclass;');
  13594. Add('begin');
  13595. Add(' b:=c=nil;');
  13596. Add(' b:=nil=c;');
  13597. Add(' b:=c=obj.classtype;');
  13598. Add(' b:=obj.classtype=c;');
  13599. Add(' b:=c=TObject;');
  13600. Add(' b:=TObject=c;');
  13601. Add(' b:=c<>nil;');
  13602. Add(' b:=nil<>c;');
  13603. Add(' b:=c<>obj.classtype;');
  13604. Add(' b:=obj.classtype<>c;');
  13605. Add(' b:=c<>TObject;');
  13606. Add(' b:=TObject<>c;');
  13607. ConvertProgram;
  13608. CheckSource('TestClassOf_Compare',
  13609. LinesToStr([ // statements
  13610. 'rtl.createClass($mod, "TObject", null, function () {',
  13611. ' this.$init = function () {',
  13612. ' this.ClassType = null;',
  13613. ' };',
  13614. ' this.$final = function () {',
  13615. ' this.ClassType = undefined;',
  13616. ' };',
  13617. '});',
  13618. 'this.b = false;',
  13619. 'this.Obj = null;',
  13620. 'this.C = null;'
  13621. ]),
  13622. LinesToStr([ // $mod.$main
  13623. '$mod.b = $mod.C === null;',
  13624. '$mod.b = null === $mod.C;',
  13625. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  13626. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  13627. '$mod.b = $mod.C === $mod.TObject;',
  13628. '$mod.b = $mod.TObject === $mod.C;',
  13629. '$mod.b = $mod.C !== null;',
  13630. '$mod.b = null !== $mod.C;',
  13631. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  13632. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  13633. '$mod.b = $mod.C !== $mod.TObject;',
  13634. '$mod.b = $mod.TObject !== $mod.C;',
  13635. '']));
  13636. end;
  13637. procedure TTestModule.TestClassOf_ClassVar;
  13638. begin
  13639. StartProgram(false);
  13640. Add('type');
  13641. Add(' TObject = class');
  13642. Add(' class var id: longint;');
  13643. Add(' end;');
  13644. Add(' TClass = class of TObject;');
  13645. Add('var');
  13646. Add(' C: tclass;');
  13647. Add('begin');
  13648. Add(' C.id:=C.id;');
  13649. ConvertProgram;
  13650. CheckSource('TestClassOf_ClassVar',
  13651. LinesToStr([ // statements
  13652. 'rtl.createClass($mod, "TObject", null, function () {',
  13653. ' this.id = 0;',
  13654. ' this.$init = function () {',
  13655. ' };',
  13656. ' this.$final = function () {',
  13657. ' };',
  13658. '});',
  13659. 'this.C = null;'
  13660. ]),
  13661. LinesToStr([ // $mod.$main
  13662. '$mod.TObject.id = $mod.C.id;',
  13663. '']));
  13664. end;
  13665. procedure TTestModule.TestClassOf_ClassMethod;
  13666. begin
  13667. StartProgram(false);
  13668. Add('type');
  13669. Add(' TObject = class');
  13670. Add(' class function DoIt(i: longint = 0): longint;');
  13671. Add(' end;');
  13672. Add(' TClass = class of TObject;');
  13673. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  13674. Add('var');
  13675. Add(' i: longint;');
  13676. Add(' C: tclass;');
  13677. Add('begin');
  13678. Add(' C.DoIt;');
  13679. Add(' C.DoIt();');
  13680. Add(' i:=C.DoIt;');
  13681. Add(' i:=C.DoIt();');
  13682. ConvertProgram;
  13683. CheckSource('TestClassOf_ClassMethod',
  13684. LinesToStr([ // statements
  13685. 'rtl.createClass($mod, "TObject", null, function () {',
  13686. ' this.$init = function () {',
  13687. ' };',
  13688. ' this.$final = function () {',
  13689. ' };',
  13690. ' this.DoIt = function (i) {',
  13691. ' var Result = 0;',
  13692. ' return Result;',
  13693. ' };',
  13694. '});',
  13695. 'this.i = 0;',
  13696. 'this.C = null;'
  13697. ]),
  13698. LinesToStr([ // $mod.$main
  13699. '$mod.C.DoIt(0);',
  13700. '$mod.C.DoIt(0);',
  13701. '$mod.i = $mod.C.DoIt(0);',
  13702. '$mod.i = $mod.C.DoIt(0);',
  13703. '']));
  13704. end;
  13705. procedure TTestModule.TestClassOf_ClassProperty;
  13706. begin
  13707. StartProgram(false);
  13708. Add([
  13709. 'type',
  13710. ' TObject = class',
  13711. ' class var FA: longint;',
  13712. ' class function GetA: longint;',
  13713. ' class procedure SetA(Value: longint);',
  13714. ' class property pA: longint read fa write fa;',
  13715. ' class property pB: longint read geta write seta;',
  13716. ' end;',
  13717. ' TObjectClass = class of tobject;',
  13718. 'class function tobject.geta: longint; begin end;',
  13719. 'class procedure tobject.seta(value: longint); begin end;',
  13720. 'var',
  13721. ' b: boolean;',
  13722. ' Obj: tobject;',
  13723. ' Cla: tobjectclass;',
  13724. 'begin',
  13725. ' obj.pa:=obj.pa;',
  13726. ' obj.pb:=obj.pb;',
  13727. ' b:=obj.pa=4;',
  13728. ' b:=obj.pb=obj.pb;',
  13729. ' b:=5=obj.pa;',
  13730. ' cla.pa:=6;',
  13731. ' cla.pa:=cla.pa;',
  13732. ' cla.pb:=cla.pb;',
  13733. ' b:=cla.pa=7;',
  13734. ' b:=cla.pb=cla.pb;',
  13735. ' b:=8=cla.pa;',
  13736. ' tobject.pa:=9;',
  13737. ' tobject.pb:=tobject.pb;',
  13738. ' b:=tobject.pa=10;',
  13739. ' b:=11=tobject.pa;',
  13740. '']);
  13741. ConvertProgram;
  13742. CheckSource('TestClassOf_ClassProperty',
  13743. LinesToStr([ // statements
  13744. 'rtl.createClass($mod, "TObject", null, function () {',
  13745. ' this.FA = 0;',
  13746. ' this.$init = function () {',
  13747. ' };',
  13748. ' this.$final = function () {',
  13749. ' };',
  13750. ' this.GetA = function () {',
  13751. ' var Result = 0;',
  13752. ' return Result;',
  13753. ' };',
  13754. ' this.SetA = function (Value) {',
  13755. ' };',
  13756. '});',
  13757. 'this.b = false;',
  13758. 'this.Obj = null;',
  13759. 'this.Cla = null;'
  13760. ]),
  13761. LinesToStr([ // $mod.$main
  13762. '$mod.TObject.FA = $mod.Obj.FA;',
  13763. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  13764. '$mod.b = $mod.Obj.FA === 4;',
  13765. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  13766. '$mod.b = 5 === $mod.Obj.FA;',
  13767. '$mod.TObject.FA = 6;',
  13768. '$mod.TObject.FA = $mod.Cla.FA;',
  13769. '$mod.Cla.SetA($mod.Cla.GetA());',
  13770. '$mod.b = $mod.Cla.FA === 7;',
  13771. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  13772. '$mod.b = 8 === $mod.Cla.FA;',
  13773. '$mod.TObject.FA = 9;',
  13774. '$mod.TObject.SetA($mod.TObject.GetA());',
  13775. '$mod.b = $mod.TObject.FA === 10;',
  13776. '$mod.b = 11 === $mod.TObject.FA;',
  13777. '']));
  13778. end;
  13779. procedure TTestModule.TestClassOf_ClassMethodSelf;
  13780. begin
  13781. StartProgram(false);
  13782. Add('type');
  13783. Add(' TObject = class');
  13784. Add(' class var GlobalId: longint;');
  13785. Add(' class procedure ProcA;');
  13786. Add(' end;');
  13787. Add('class procedure tobject.proca;');
  13788. Add('var b: boolean;');
  13789. Add('begin');
  13790. Add(' b:=self=nil;');
  13791. Add(' b:=self.globalid=3;');
  13792. Add(' b:=4=self.globalid;');
  13793. Add(' self.globalid:=5;');
  13794. Add(' self.proca;');
  13795. Add('end;');
  13796. Add('begin');
  13797. ConvertProgram;
  13798. CheckSource('TestClassOf_ClassMethodSelf',
  13799. LinesToStr([ // statements
  13800. 'rtl.createClass($mod, "TObject", null, function () {',
  13801. ' this.GlobalId = 0;',
  13802. ' this.$init = function () {',
  13803. ' };',
  13804. ' this.$final = function () {',
  13805. ' };',
  13806. ' this.ProcA = function () {',
  13807. ' var b = false;',
  13808. ' b = this === null;',
  13809. ' b = this.GlobalId === 3;',
  13810. ' b = 4 === this.GlobalId;',
  13811. ' $mod.TObject.GlobalId = 5;',
  13812. ' this.ProcA();',
  13813. ' };',
  13814. '});'
  13815. ]),
  13816. LinesToStr([ // $mod.$main
  13817. '']));
  13818. end;
  13819. procedure TTestModule.TestClassOf_TypeCast;
  13820. begin
  13821. StartProgram(false);
  13822. Add('type');
  13823. Add(' TObject = class');
  13824. Add(' class procedure {#TObject_DoIt}DoIt;');
  13825. Add(' end;');
  13826. Add(' TClass = class of TObject;');
  13827. Add(' TMobile = class');
  13828. Add(' class procedure {#TMobile_DoIt}DoIt;');
  13829. Add(' end;');
  13830. Add(' TMobileClass = class of TMobile;');
  13831. Add(' TCar = class(TMobile)');
  13832. Add(' class procedure {#TCar_DoIt}DoIt;');
  13833. Add(' end;');
  13834. Add(' TCarClass = class of TCar;');
  13835. Add('class procedure TObject.DoIt;');
  13836. Add('begin');
  13837. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  13838. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  13839. Add('end;');
  13840. Add('class procedure TMobile.DoIt;');
  13841. Add('begin');
  13842. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  13843. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  13844. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  13845. Add('end;');
  13846. Add('class procedure TCar.DoIt; begin end;');
  13847. Add('var');
  13848. Add(' ObjC: TClass;');
  13849. Add(' MobileC: TMobileClass;');
  13850. Add(' CarC: TCarClass;');
  13851. Add('begin');
  13852. Add(' ObjC.{@TObject_DoIt}DoIt;');
  13853. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  13854. Add(' CarC.{@TCar_DoIt}DoIt;');
  13855. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  13856. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  13857. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  13858. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  13859. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  13860. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  13861. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  13862. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  13863. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  13864. ConvertProgram;
  13865. CheckSource('TestClassOf_TypeCast',
  13866. LinesToStr([ // statements
  13867. 'rtl.createClass($mod, "TObject", null, function () {',
  13868. ' this.$init = function () {',
  13869. ' };',
  13870. ' this.$final = function () {',
  13871. ' };',
  13872. ' this.DoIt = function () {',
  13873. ' this.DoIt();',
  13874. ' this.DoIt$1();',
  13875. ' };',
  13876. '});',
  13877. 'rtl.createClass($mod, "TMobile", $mod.TObject, function () {',
  13878. ' this.DoIt$1 = function () {',
  13879. ' this.DoIt();',
  13880. ' this.DoIt$1();',
  13881. ' this.DoIt$2();',
  13882. ' };',
  13883. '});',
  13884. 'rtl.createClass($mod, "TCar", $mod.TMobile, function () {',
  13885. ' this.DoIt$2 = function () {',
  13886. ' };',
  13887. '});',
  13888. 'this.ObjC = null;',
  13889. 'this.MobileC = null;',
  13890. 'this.CarC = null;',
  13891. '']),
  13892. LinesToStr([ // $mod.$main
  13893. '$mod.ObjC.DoIt();',
  13894. '$mod.MobileC.DoIt$1();',
  13895. '$mod.CarC.DoIt$2();',
  13896. '$mod.ObjC.DoIt();',
  13897. '$mod.ObjC.DoIt$1();',
  13898. '$mod.ObjC.DoIt$2();',
  13899. '$mod.MobileC.DoIt();',
  13900. '$mod.MobileC.DoIt$1();',
  13901. '$mod.MobileC.DoIt$2();',
  13902. '$mod.CarC.DoIt();',
  13903. '$mod.CarC.DoIt$1();',
  13904. '$mod.CarC.DoIt$2();',
  13905. '']));
  13906. end;
  13907. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  13908. begin
  13909. StartProgram(false);
  13910. Add('type');
  13911. Add(' TObject = class');
  13912. Add(' function CurNow: longint; ');
  13913. Add(' class function Now: longint; ');
  13914. Add(' end;');
  13915. Add('function TObject.CurNow: longint; begin end;');
  13916. Add('class function TObject.Now: longint; begin end;');
  13917. Add('var');
  13918. Add(' Obj: tobject;');
  13919. Add(' vI: longint;');
  13920. Add('begin');
  13921. Add(' obj.curnow;');
  13922. Add(' vi:=obj.curnow;');
  13923. Add(' tobject.now;');
  13924. Add(' vi:=tobject.now;');
  13925. ConvertProgram;
  13926. CheckSource('TestClassOf_ImplicitFunctionCall',
  13927. LinesToStr([ // statements
  13928. 'rtl.createClass($mod, "TObject", null, function () {',
  13929. ' this.$init = function () {',
  13930. ' };',
  13931. ' this.$final = function () {',
  13932. ' };',
  13933. ' this.CurNow = function () {',
  13934. ' var Result = 0;',
  13935. ' return Result;',
  13936. ' };',
  13937. ' this.Now = function () {',
  13938. ' var Result = 0;',
  13939. ' return Result;',
  13940. ' };',
  13941. '});',
  13942. 'this.Obj = null;',
  13943. 'this.vI = 0;',
  13944. '']),
  13945. LinesToStr([ // $mod.$main
  13946. '$mod.Obj.CurNow();',
  13947. '$mod.vI = $mod.Obj.CurNow();',
  13948. '$mod.TObject.Now();',
  13949. '$mod.vI = $mod.TObject.Now();',
  13950. '']));
  13951. end;
  13952. procedure TTestModule.TestClassOf_Const;
  13953. begin
  13954. StartProgram(false);
  13955. Add([
  13956. 'type',
  13957. ' TObject = class',
  13958. ' end;',
  13959. ' TBird = TObject;',
  13960. ' TBirds = class of TBird;',
  13961. ' TEagles = TBirds;',
  13962. ' THawk = class(TBird);',
  13963. 'const',
  13964. ' Hawk: TEagles = THawk;',
  13965. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  13966. ' TBird,',
  13967. ' THawk',
  13968. ' );',
  13969. 'begin']);
  13970. ConvertProgram;
  13971. CheckSource('TestClassOf_Const',
  13972. LinesToStr([ // statements
  13973. 'rtl.createClass($mod, "TObject", null, function () {',
  13974. ' this.$init = function () {',
  13975. ' };',
  13976. ' this.$final = function () {',
  13977. ' };',
  13978. '});',
  13979. 'rtl.createClass($mod, "THawk", $mod.TObject, function () {',
  13980. '});',
  13981. 'this.Hawk = $mod.THawk;',
  13982. 'this.DefaultBirdClasses = [$mod.TObject, $mod.THawk];',
  13983. '']),
  13984. LinesToStr([ // $mod.$main
  13985. '']));
  13986. end;
  13987. procedure TTestModule.TestNestedClass_Alias;
  13988. begin
  13989. Converter.Options:=Converter.Options-[coNoTypeInfo];
  13990. StartProgram(false);
  13991. Add([
  13992. 'type',
  13993. ' TObject = class',
  13994. ' type TNested = type longint;',
  13995. ' end;',
  13996. 'type TAlias = type tobject.tnested;',
  13997. 'var i: tobject.tnested = 3;',
  13998. 'var j: TAlias = 4;',
  13999. 'begin',
  14000. ' if typeinfo(TAlias)=nil then ;',
  14001. ' if typeinfo(tobject.tnested)=nil then ;',
  14002. '']);
  14003. ConvertProgram;
  14004. CheckSource('TestNestedClass_Alias',
  14005. LinesToStr([ // statements
  14006. 'rtl.createClass($mod, "TObject", null, function () {',
  14007. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  14008. ' this.$init = function () {',
  14009. ' };',
  14010. ' this.$final = function () {',
  14011. ' };',
  14012. '});',
  14013. '$mod.$rtti.$inherited("TAlias", $mod.$rtti["TObject.TNested"], {});',
  14014. 'this.i = 3;',
  14015. 'this.j = 4;',
  14016. '']),
  14017. LinesToStr([ // $mod.$main
  14018. 'if ($mod.$rtti["TAlias"] === null) ;',
  14019. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  14020. '']));
  14021. end;
  14022. procedure TTestModule.TestNestedClass_Record;
  14023. begin
  14024. Converter.Options:=Converter.Options-[coNoTypeInfo];
  14025. StartProgram(false);
  14026. Add([
  14027. 'type',
  14028. ' TObject = class',
  14029. ' type TPoint = record',
  14030. ' x,y: byte;',
  14031. ' end;',
  14032. ' procedure DoIt(t: TPoint);',
  14033. ' end;',
  14034. 'procedure tobject.DoIt(t: TPoint);',
  14035. 'var p: TPoint;',
  14036. 'begin',
  14037. ' t.x:=t.y;',
  14038. ' p:=t;',
  14039. 'end;',
  14040. 'var',
  14041. ' p: tobject.tpoint = (x:2; y:4);',
  14042. ' o: TObject;',
  14043. 'begin',
  14044. ' p:=p;',
  14045. ' o.doit(p);',
  14046. '']);
  14047. ConvertProgram;
  14048. CheckSource('TestNestedClass_Record',
  14049. LinesToStr([ // statements
  14050. 'rtl.createClass($mod, "TObject", null, function () {',
  14051. ' rtl.recNewT(this, "TPoint", function () {',
  14052. ' this.x = 0;',
  14053. ' this.y = 0;',
  14054. ' this.$eq = function (b) {',
  14055. ' return (this.x === b.x) && (this.y === b.y);',
  14056. ' };',
  14057. ' this.$assign = function (s) {',
  14058. ' this.x = s.x;',
  14059. ' this.y = s.y;',
  14060. ' return this;',
  14061. ' };',
  14062. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  14063. ' $r.addField("x", rtl.byte);',
  14064. ' $r.addField("y", rtl.byte);',
  14065. ' });',
  14066. ' this.$init = function () {',
  14067. ' };',
  14068. ' this.$final = function () {',
  14069. ' };',
  14070. ' this.DoIt = function (t) {',
  14071. ' var p = this.TPoint.$new();',
  14072. ' t.x = t.y;',
  14073. ' p.$assign(t);',
  14074. ' };',
  14075. '});',
  14076. 'this.p = $mod.TObject.TPoint.$clone({',
  14077. ' x: 2,',
  14078. ' y: 4',
  14079. '});',
  14080. 'this.o = null;',
  14081. '']),
  14082. LinesToStr([ // $mod.$main
  14083. '$mod.p.$assign($mod.p);',
  14084. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  14085. '']));
  14086. end;
  14087. procedure TTestModule.TestNestedClass_Class;
  14088. begin
  14089. Converter.Options:=Converter.Options-[coNoTypeInfo];
  14090. StartProgram(false);
  14091. Add([
  14092. 'type',
  14093. ' TObject = class end;',
  14094. ' TBird = class',
  14095. ' type TLeg = class',
  14096. ' FId: longint;',
  14097. ' constructor Create;',
  14098. ' function Create(i: longint): TLeg;',
  14099. ' end;',
  14100. ' function DoIt(b: TBird): Tleg;',
  14101. ' end;',
  14102. 'constructor tbird.tleg.create;',
  14103. 'begin',
  14104. ' FId:=3;',
  14105. 'end;',
  14106. 'function tbird.tleg.Create(i: longint): TLeg;',
  14107. 'begin',
  14108. ' Create;',
  14109. ' Result:=TLeg.Create;',
  14110. ' Result:=TBird.TLeg.Create;',
  14111. ' Result:=Create(3);',
  14112. ' FId:=i;',
  14113. 'end;',
  14114. 'function tbird.DoIt(b: tbird): tleg;',
  14115. 'begin',
  14116. ' Result.Create;',
  14117. ' Result:=TLeg.Create;',
  14118. ' Result:=TBird.TLeg.Create;',
  14119. ' Result:=Result.Create(3);',
  14120. 'end;',
  14121. 'var',
  14122. ' b: Tbird.tleg;',
  14123. 'begin',
  14124. ' b.Create;',
  14125. ' b:=TBird.TLeg.Create;',
  14126. ' b:=b.Create(3);',
  14127. '']);
  14128. ConvertProgram;
  14129. CheckSource('TestNestedClass_Class',
  14130. LinesToStr([ // statements
  14131. 'rtl.createClass($mod, "TObject", null, function () {',
  14132. ' this.$init = function () {',
  14133. ' };',
  14134. ' this.$final = function () {',
  14135. ' };',
  14136. '});',
  14137. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  14138. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  14139. ' this.$init = function () {',
  14140. ' $mod.TObject.$init.call(this);',
  14141. ' this.FId = 0;',
  14142. ' };',
  14143. ' this.Create = function () {',
  14144. ' this.FId = 3;',
  14145. ' return this;',
  14146. ' };',
  14147. ' this.Create$1 = function (i) {',
  14148. ' var Result = null;',
  14149. ' this.Create();',
  14150. ' Result = $mod.TBird.TLeg.$create("Create");',
  14151. ' Result = $mod.TBird.TLeg.$create("Create");',
  14152. ' Result = this.Create$1(3);',
  14153. ' this.FId = i;',
  14154. ' return Result;',
  14155. ' };',
  14156. ' });',
  14157. ' this.DoIt = function (b) {',
  14158. ' var Result = null;',
  14159. ' Result.Create();',
  14160. ' Result = this.TLeg.$create("Create");',
  14161. ' Result = $mod.TBird.TLeg.$create("Create");',
  14162. ' Result = Result.Create$1(3);',
  14163. ' return Result;',
  14164. ' };',
  14165. '});',
  14166. 'this.b = null;',
  14167. '']),
  14168. LinesToStr([ // $mod.$main
  14169. '$mod.b.Create();',
  14170. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  14171. '$mod.b = $mod.b.Create$1(3);',
  14172. '']));
  14173. end;
  14174. procedure TTestModule.TestExternalClass_Var;
  14175. begin
  14176. StartProgram(false);
  14177. Add([
  14178. '{$modeswitch externalclass}',
  14179. 'type',
  14180. ' TExtA = class external name ''ExtObj''',
  14181. ' Id: longint external name ''$Id'';',
  14182. ' B: longint;',
  14183. ' end;',
  14184. 'var Obj: TExtA;',
  14185. 'begin',
  14186. ' obj.id:=obj.id+1;',
  14187. ' obj.B:=obj.B+1;']);
  14188. ConvertProgram;
  14189. CheckSource('TestExternalClass_Var',
  14190. LinesToStr([ // statements
  14191. 'this.Obj = null;',
  14192. '']),
  14193. LinesToStr([ // $mod.$main
  14194. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  14195. '$mod.Obj.B = $mod.Obj.B + 1;',
  14196. '']));
  14197. end;
  14198. procedure TTestModule.TestExternalClass_Const;
  14199. begin
  14200. StartProgram(false);
  14201. Add([
  14202. '{$modeswitch externalclass}',
  14203. 'type',
  14204. ' TExtA = class external name ''ExtObj''',
  14205. ' const Two: longint = 2;',
  14206. ' const Three = 3;',
  14207. ' const Id: longint;',
  14208. ' end;',
  14209. ' TExtB = class external name ''ExtB''',
  14210. ' A: TExtA;',
  14211. ' end;',
  14212. 'var',
  14213. ' A: texta;',
  14214. ' B: textb;',
  14215. ' i: longint;',
  14216. 'begin',
  14217. ' i:=a.two;',
  14218. ' i:=texta.two;',
  14219. ' i:=a.three;',
  14220. ' i:=texta.three;',
  14221. ' i:=a.id;',
  14222. ' i:=texta.id;',
  14223. '']);
  14224. ConvertProgram;
  14225. CheckSource('TestExternalClass_Const',
  14226. LinesToStr([ // statements
  14227. 'this.A = null;',
  14228. 'this.B = null;',
  14229. 'this.i = 0;',
  14230. '']),
  14231. LinesToStr([ // $mod.$main
  14232. '$mod.i = 2;',
  14233. '$mod.i = 2;',
  14234. '$mod.i = 3;',
  14235. '$mod.i = 3;',
  14236. '$mod.i = $mod.A.Id;',
  14237. '$mod.i = ExtObj.Id;',
  14238. '']));
  14239. end;
  14240. procedure TTestModule.TestExternalClass_Dollar;
  14241. begin
  14242. StartProgram(false);
  14243. Add([
  14244. '{$modeswitch externalclass}',
  14245. 'type',
  14246. ' TExtA = class external name ''$''',
  14247. ' Id: longint external name ''$'';',
  14248. ' function Bla(i: longint): longint; external name ''$'';',
  14249. ' end;',
  14250. 'function dollar(k: longint): longint; external name ''$'';',
  14251. 'var Obj: TExtA;',
  14252. 'begin',
  14253. ' dollar(1);',
  14254. ' obj.id:=obj.id+2;',
  14255. ' obj.Bla(3);',
  14256. '']);
  14257. ConvertProgram;
  14258. CheckSource('TestExternalClass_Dollar',
  14259. LinesToStr([ // statements
  14260. 'this.Obj = null;',
  14261. '']),
  14262. LinesToStr([ // $mod.$main
  14263. '$(1);',
  14264. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  14265. '$mod.Obj.$(3);',
  14266. '']));
  14267. end;
  14268. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  14269. begin
  14270. StartProgram(false);
  14271. Add('{$modeswitch externalclass}');
  14272. Add('type');
  14273. Add(' TExtA = class external name ''ExtA''');
  14274. Add(' Id: longint external name ''$Id'';');
  14275. Add(' end;');
  14276. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  14277. Add(' Id: longint;');
  14278. Add(' end;');
  14279. Add('begin');
  14280. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  14281. ConvertProgram;
  14282. end;
  14283. procedure TTestModule.TestExternalClass_Method;
  14284. begin
  14285. StartProgram(false);
  14286. Add('{$modeswitch externalclass}');
  14287. Add('type');
  14288. Add(' TExtA = class external name ''ExtObj''');
  14289. Add(' procedure DoIt(Id: longint = 1); external name ''$Execute'';');
  14290. Add(' procedure DoSome(Id: longint = 1);');
  14291. Add(' end;');
  14292. Add('var Obj: texta;');
  14293. Add('begin');
  14294. Add(' obj.doit;');
  14295. Add(' obj.doit();');
  14296. Add(' obj.doit(2);');
  14297. Add(' with obj do begin');
  14298. Add(' doit;');
  14299. Add(' doit();');
  14300. Add(' doit(3);');
  14301. Add(' end;');
  14302. ConvertProgram;
  14303. CheckSource('TestExternalClass_Method',
  14304. LinesToStr([ // statements
  14305. 'this.Obj = null;',
  14306. '']),
  14307. LinesToStr([ // $mod.$main
  14308. '$mod.Obj.$Execute(1);',
  14309. '$mod.Obj.$Execute(1);',
  14310. '$mod.Obj.$Execute(2);',
  14311. 'var $with1 = $mod.Obj;',
  14312. '$with1.$Execute(1);',
  14313. '$with1.$Execute(1);',
  14314. '$with1.$Execute(3);',
  14315. '']));
  14316. end;
  14317. procedure TTestModule.TestExternalClass_ClassMethod;
  14318. begin
  14319. StartProgram(false);
  14320. Add([
  14321. '{$modeswitch externalclass}',
  14322. 'type',
  14323. ' TExtA = class external name ''ExtObj''',
  14324. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  14325. ' end;',
  14326. ' TExtB = TExtA;',
  14327. 'begin',
  14328. ' texta.doit;',
  14329. ' texta.doit();',
  14330. ' texta.doit(2);',
  14331. ' with texta do begin',
  14332. ' doit;',
  14333. ' doit();',
  14334. ' doit(3);',
  14335. ' end;',
  14336. ' textb.doit;',
  14337. ' textb.doit();',
  14338. ' textb.doit(4);',
  14339. ' with textb do begin',
  14340. ' doit;',
  14341. ' doit();',
  14342. ' doit(5);',
  14343. ' end;',
  14344. '']);
  14345. ConvertProgram;
  14346. CheckSource('TestExternalClass_ClassMethod',
  14347. LinesToStr([ // statements
  14348. '']),
  14349. LinesToStr([ // $mod.$main
  14350. 'ExtObj.$Execute(1);',
  14351. 'ExtObj.$Execute(1);',
  14352. 'ExtObj.$Execute(2);',
  14353. 'ExtObj.$Execute(1);',
  14354. 'ExtObj.$Execute(1);',
  14355. 'ExtObj.$Execute(3);',
  14356. 'ExtObj.$Execute(1);',
  14357. 'ExtObj.$Execute(1);',
  14358. 'ExtObj.$Execute(4);',
  14359. 'ExtObj.$Execute(1);',
  14360. 'ExtObj.$Execute(1);',
  14361. 'ExtObj.$Execute(5);',
  14362. '']));
  14363. end;
  14364. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  14365. begin
  14366. StartProgram(false);
  14367. Add([
  14368. '{$modeswitch externalclass}',
  14369. 'type',
  14370. ' TBird = class external name ''Array''',
  14371. ' end;',
  14372. 'function GetPtr: Pointer;',
  14373. 'begin',
  14374. 'end;',
  14375. 'procedure Write(const p);',
  14376. 'begin',
  14377. 'end;',
  14378. 'procedure WriteLn; varargs;',
  14379. 'begin',
  14380. 'end;',
  14381. 'begin',
  14382. ' if TBird(GetPtr)=nil then ;',
  14383. ' Write(GetPtr);',
  14384. ' WriteLn(GetPtr);',
  14385. ' Write(TBird(GetPtr));',
  14386. ' WriteLn(TBird(GetPtr));',
  14387. '']);
  14388. ConvertProgram;
  14389. CheckSource('TestFunctionResultInTypeCast',
  14390. LinesToStr([ // statements
  14391. 'this.GetPtr = function () {',
  14392. ' var Result = null;',
  14393. ' return Result;',
  14394. '};',
  14395. 'this.Write = function (p) {',
  14396. '};',
  14397. 'this.WriteLn = function () {',
  14398. '};',
  14399. '']),
  14400. LinesToStr([
  14401. 'if ($mod.GetPtr() === null) ;',
  14402. '$mod.Write($mod.GetPtr());',
  14403. '$mod.WriteLn($mod.GetPtr());',
  14404. '$mod.Write($mod.GetPtr());',
  14405. '$mod.WriteLn($mod.GetPtr());',
  14406. '']));
  14407. end;
  14408. procedure TTestModule.TestExternalClass_NonExternalOverride;
  14409. begin
  14410. StartProgram(false);
  14411. Add('{$modeswitch externalclass}');
  14412. Add('type');
  14413. Add(' TExtA = class external name ''ExtObjA''');
  14414. Add(' procedure ProcA; virtual;');
  14415. Add(' procedure ProcB; virtual;');
  14416. Add(' end;');
  14417. Add(' TExtB = class external name ''ExtObjB'' (TExtA)');
  14418. Add(' end;');
  14419. Add(' TExtC = class (TExtB)');
  14420. Add(' procedure ProcA; override;');
  14421. Add(' end;');
  14422. Add('procedure TExtC.ProcA;');
  14423. Add('begin');
  14424. Add(' ProcA;');
  14425. Add(' Self.ProcA;');
  14426. Add(' ProcB;');
  14427. Add(' Self.ProcB;');
  14428. Add('end;');
  14429. Add('var');
  14430. Add(' A: texta;');
  14431. Add(' B: textb;');
  14432. Add(' C: textc;');
  14433. Add('begin');
  14434. Add(' a.proca;');
  14435. Add(' b.proca;');
  14436. Add(' c.proca;');
  14437. ConvertProgram;
  14438. CheckSource('TestExternalClass_NonExternalOverride',
  14439. LinesToStr([ // statements
  14440. 'rtl.createClassExt($mod, "TExtC", ExtObjB, "", function () {',
  14441. ' this.$init = function () {',
  14442. ' };',
  14443. ' this.$final = function () {',
  14444. ' };',
  14445. ' this.ProcA = function () {',
  14446. ' this.ProcA();',
  14447. ' this.ProcA();',
  14448. ' this.ProcB();',
  14449. ' this.ProcB();',
  14450. ' };',
  14451. '});',
  14452. 'this.A = null;',
  14453. 'this.B = null;',
  14454. 'this.C = null;',
  14455. '']),
  14456. LinesToStr([ // $mod.$main
  14457. '$mod.A.ProcA();',
  14458. '$mod.B.ProcA();',
  14459. '$mod.C.ProcA();',
  14460. '']));
  14461. end;
  14462. procedure TTestModule.TestExternalClass_OverloadHint;
  14463. begin
  14464. StartProgram(false);
  14465. Add([
  14466. '{$modeswitch externalclass}',
  14467. 'type',
  14468. ' TExtA = class external name ''ExtObjA''',
  14469. ' procedure DoIt;',
  14470. ' procedure DoIt(i: longint);',
  14471. ' end;',
  14472. 'begin',
  14473. '']);
  14474. ConvertProgram;
  14475. CheckResolverUnexpectedHints(true);
  14476. CheckSource('TestExternalClass_OverloadHint',
  14477. LinesToStr([ // statements
  14478. '']),
  14479. LinesToStr([ // $mod.$main
  14480. '']));
  14481. end;
  14482. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  14483. begin
  14484. StartProgram(false);
  14485. Add([
  14486. '{$modeswitch externalclass}',
  14487. 'type',
  14488. ' JSwiper = class external name ''Swiper''',
  14489. ' constructor New;',
  14490. ' end;',
  14491. ' TObject = class',
  14492. ' private',
  14493. ' FSwiper: JSwiper;',
  14494. ' published',
  14495. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  14496. ' end;',
  14497. 'begin',
  14498. ' JSwiper.new;',
  14499. '']);
  14500. ConvertProgram;
  14501. CheckSource('TestExternalClass_SameNamePublishedProperty',
  14502. LinesToStr([ // statements
  14503. 'rtl.createClass($mod, "TObject", null, function () {',
  14504. ' this.$init = function () {',
  14505. ' this.FSwiper = null;',
  14506. ' };',
  14507. ' this.$final = function () {',
  14508. ' this.FSwiper = undefined;',
  14509. ' };',
  14510. ' var $r = this.$rtti;',
  14511. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  14512. '});',
  14513. '']),
  14514. LinesToStr([ // $mod.$main
  14515. 'new Swiper();',
  14516. '']));
  14517. end;
  14518. procedure TTestModule.TestExternalClass_Property;
  14519. begin
  14520. StartProgram(false);
  14521. Add([
  14522. '{$modeswitch externalclass}',
  14523. 'type',
  14524. ' TExtA = class external name ''ExtA''',
  14525. ' function getYear: longint;',
  14526. ' procedure setYear(Value: longint);',
  14527. ' property Year: longint read getyear write setyear;',
  14528. ' end;',
  14529. ' TExtB = class (TExtA)',
  14530. ' procedure OtherSetYear(Value: longint);',
  14531. ' property year write othersetyear;',
  14532. ' end;',
  14533. 'procedure textb.othersetyear(value: longint);',
  14534. 'begin',
  14535. ' setYear(Value+4);',
  14536. 'end;',
  14537. 'var',
  14538. ' A: texta;',
  14539. ' B: textb;',
  14540. 'begin',
  14541. ' a.year:=a.year+1;',
  14542. ' b.year:=b.year+2;']);
  14543. ConvertProgram;
  14544. CheckSource('TestExternalClass_NonExternalOverride',
  14545. LinesToStr([ // statements
  14546. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  14547. ' this.$init = function () {',
  14548. ' };',
  14549. ' this.$final = function () {',
  14550. ' };',
  14551. ' this.OtherSetYear = function (Value) {',
  14552. ' this.setYear(Value+4);',
  14553. ' };',
  14554. '});',
  14555. 'this.A = null;',
  14556. 'this.B = null;',
  14557. '']),
  14558. LinesToStr([ // $mod.$main
  14559. '$mod.A.setYear($mod.A.getYear()+1);',
  14560. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  14561. '']));
  14562. end;
  14563. procedure TTestModule.TestExternalClass_PropertyDate;
  14564. begin
  14565. StartProgram(false);
  14566. Add([
  14567. '{$modeswitch externalclass}',
  14568. 'type',
  14569. ' TExtA = class external name ''ExtA''',
  14570. ' end;',
  14571. ' TExtB = class (TExtA)',
  14572. ' FDate: string;',
  14573. ' property Date: string read FDate write FDate;',
  14574. ' property ExtA: string read FDate write FDate;',
  14575. ' end;',
  14576. ' {$M+}',
  14577. ' TObject = class',
  14578. ' FDate: string;',
  14579. ' published',
  14580. ' property Date: string read FDate write FDate;',
  14581. ' property ExtA: string read FDate write FDate;',
  14582. ' end;',
  14583. 'var',
  14584. ' B: textb;',
  14585. ' o: TObject;',
  14586. 'begin',
  14587. ' b.date:=b.exta;',
  14588. ' o.date:=o.exta;']);
  14589. ConvertProgram;
  14590. CheckSource('TestExternalClass_PropertyDate',
  14591. LinesToStr([ // statements
  14592. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  14593. ' this.$init = function () {',
  14594. ' this.FDate = "";',
  14595. ' };',
  14596. ' this.$final = function () {',
  14597. ' };',
  14598. '});',
  14599. 'rtl.createClass($mod, "TObject", null, function () {',
  14600. ' this.$init = function () {',
  14601. ' this.FDate = "";',
  14602. ' };',
  14603. ' this.$final = function () {',
  14604. ' };',
  14605. ' var $r = this.$rtti;',
  14606. ' $r.addField("FDate", rtl.string);',
  14607. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  14608. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  14609. '});',
  14610. 'this.B = null;',
  14611. 'this.o = null;',
  14612. '']),
  14613. LinesToStr([ // $mod.$main
  14614. '$mod.B.FDate = $mod.B.FDate;',
  14615. '$mod.o.FDate = $mod.o.FDate;',
  14616. '']));
  14617. end;
  14618. procedure TTestModule.TestExternalClass_ClassProperty;
  14619. begin
  14620. StartProgram(false);
  14621. Add('{$modeswitch externalclass}');
  14622. Add('type');
  14623. Add(' TExtA = class external name ''ExtA''');
  14624. Add(' class function getYear: longint;');
  14625. Add(' class procedure setYear(Value: longint);');
  14626. Add(' class property Year: longint read getyear write setyear;');
  14627. Add(' end;');
  14628. Add(' TExtB = class (TExtA)');
  14629. Add(' class function GetCentury: longint;');
  14630. Add(' class procedure SetCentury(Value: longint);');
  14631. Add(' class property Century: longint read getcentury write setcentury;');
  14632. Add(' end;');
  14633. Add('class function textb.getcentury: longint;');
  14634. Add('begin');
  14635. Add('end;');
  14636. Add('class procedure textb.setcentury(value: longint);');
  14637. Add('begin');
  14638. Add(' setyear(value+11);');
  14639. Add(' texta.year:=texta.year+12;');
  14640. Add(' year:=year+13;');
  14641. Add(' textb.century:=textb.century+14;');
  14642. Add(' century:=century+15;');
  14643. Add('end;');
  14644. Add('var');
  14645. Add(' A: texta;');
  14646. Add(' B: textb;');
  14647. Add('begin');
  14648. Add(' texta.year:=texta.year+1;');
  14649. Add(' textb.year:=textb.year+2;');
  14650. Add(' TextA.year:=TextA.year+3;');
  14651. Add(' b.year:=b.year+4;');
  14652. Add(' textb.century:=textb.century+5;');
  14653. Add(' b.century:=b.century+6;');
  14654. ConvertProgram;
  14655. CheckSource('TestExternalClass_ClassProperty',
  14656. LinesToStr([ // statements
  14657. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  14658. ' this.$init = function () {',
  14659. ' };',
  14660. ' this.$final = function () {',
  14661. ' };',
  14662. ' this.GetCentury = function () {',
  14663. ' var Result = 0;',
  14664. ' return Result;',
  14665. ' };',
  14666. ' this.SetCentury = function (Value) {',
  14667. ' this.setYear(Value + 11);',
  14668. ' ExtA.setYear(ExtA.getYear() + 12);',
  14669. ' this.setYear(this.getYear() + 13);',
  14670. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  14671. ' this.SetCentury(this.GetCentury() + 15);',
  14672. ' };',
  14673. '});',
  14674. 'this.A = null;',
  14675. 'this.B = null;',
  14676. '']),
  14677. LinesToStr([ // $mod.$main
  14678. 'ExtA.setYear(ExtA.getYear() + 1);',
  14679. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  14680. 'ExtA.setYear(ExtA.getYear() + 3);',
  14681. '$mod.B.setYear($mod.B.getYear() + 4);',
  14682. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  14683. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  14684. '']));
  14685. end;
  14686. procedure TTestModule.TestExternalClass_ClassOf;
  14687. begin
  14688. StartProgram(false);
  14689. Add('{$modeswitch externalclass}');
  14690. Add('type');
  14691. Add(' TExtA = class external name ''ExtA''');
  14692. Add(' procedure ProcA; virtual;');
  14693. Add(' procedure ProcB; virtual;');
  14694. Add(' end;');
  14695. Add(' TExtAClass = class of TExtA;');
  14696. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  14697. Add(' end;');
  14698. Add(' TExtBClass = class of TExtB;');
  14699. Add(' TExtC = class (TExtB)');
  14700. Add(' procedure ProcA; override;');
  14701. Add(' end;');
  14702. Add(' TExtCClass = class of TExtC;');
  14703. Add('procedure TExtC.ProcA; begin end;');
  14704. Add('var');
  14705. Add(' A: texta; ClA: TExtAClass;');
  14706. Add(' B: textb; ClB: TExtBClass;');
  14707. Add(' C: textc; ClC: TExtCClass;');
  14708. Add('begin');
  14709. Add(' ClA:=texta;');
  14710. Add(' ClA:=textb;');
  14711. Add(' ClA:=textc;');
  14712. Add(' ClB:=textb;');
  14713. Add(' ClB:=textc;');
  14714. Add(' ClC:=textc;');
  14715. ConvertProgram;
  14716. CheckSource('TestExternalClass_ClassOf',
  14717. LinesToStr([ // statements
  14718. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  14719. ' this.$init = function () {',
  14720. ' };',
  14721. ' this.$final = function () {',
  14722. ' };',
  14723. ' this.ProcA = function () {',
  14724. ' };',
  14725. '});',
  14726. 'this.A = null;',
  14727. 'this.ClA = null;',
  14728. 'this.B = null;',
  14729. 'this.ClB = null;',
  14730. 'this.C = null;',
  14731. 'this.ClC = null;',
  14732. '']),
  14733. LinesToStr([ // $mod.$main
  14734. '$mod.ClA = ExtA;',
  14735. '$mod.ClA = ExtB;',
  14736. '$mod.ClA = $mod.TExtC;',
  14737. '$mod.ClB = ExtB;',
  14738. '$mod.ClB = $mod.TExtC;',
  14739. '$mod.ClC = $mod.TExtC;',
  14740. '']));
  14741. end;
  14742. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  14743. begin
  14744. AddModuleWithIntfImplSrc('unit2.pas',
  14745. LinesToStr([
  14746. '{$modeswitch externalclass}',
  14747. 'type',
  14748. ' TExtA = class external name ''ExtA''',
  14749. ' class var Id: longint;',
  14750. ' end;',
  14751. '']),
  14752. '');
  14753. StartUnit(true);
  14754. Add('interface');
  14755. Add('uses unit2;');
  14756. Add('implementation');
  14757. Add('begin');
  14758. Add(' unit2.texta.id:=unit2.texta.id+1;');
  14759. ConvertUnit;
  14760. CheckSource('TestExternalClass_ClassOtherUnit',
  14761. LinesToStr([
  14762. '']),
  14763. LinesToStr([
  14764. 'ExtA.Id = ExtA.Id + 1;',
  14765. '']));
  14766. end;
  14767. procedure TTestModule.TestExternalClass_Is;
  14768. begin
  14769. StartProgram(false);
  14770. Add('{$modeswitch externalclass}');
  14771. Add('type');
  14772. Add(' TExtA = class external name ''ExtA''');
  14773. Add(' end;');
  14774. Add(' TExtAClass = class of TExtA;');
  14775. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  14776. Add(' end;');
  14777. Add(' TExtBClass = class of TExtB;');
  14778. Add(' TExtC = class (TExtB)');
  14779. Add(' end;');
  14780. Add(' TExtCClass = class of TExtC;');
  14781. Add('var');
  14782. Add(' A: texta; ClA: TExtAClass;');
  14783. Add(' B: textb; ClB: TExtBClass;');
  14784. Add(' C: textc; ClC: TExtCClass;');
  14785. Add('begin');
  14786. Add(' if a is textb then ;');
  14787. Add(' if a is textc then ;');
  14788. Add(' if b is textc then ;');
  14789. Add(' if cla is textb then ;');
  14790. Add(' if cla is textc then ;');
  14791. Add(' if clb is textc then ;');
  14792. ConvertProgram;
  14793. CheckSource('TestExternalClass_Is',
  14794. LinesToStr([ // statements
  14795. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  14796. ' this.$init = function () {',
  14797. ' };',
  14798. ' this.$final = function () {',
  14799. ' };',
  14800. '});',
  14801. 'this.A = null;',
  14802. 'this.ClA = null;',
  14803. 'this.B = null;',
  14804. 'this.ClB = null;',
  14805. 'this.C = null;',
  14806. 'this.ClC = null;',
  14807. '']),
  14808. LinesToStr([ // $mod.$main
  14809. 'if (rtl.isExt($mod.A, ExtB)) ;',
  14810. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  14811. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  14812. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  14813. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  14814. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  14815. '']));
  14816. end;
  14817. procedure TTestModule.TestExternalClass_As;
  14818. begin
  14819. StartProgram(false);
  14820. Add('{$modeswitch externalclass}');
  14821. Add('type');
  14822. Add(' TExtA = class external name ''ExtA''');
  14823. Add(' end;');
  14824. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  14825. Add(' end;');
  14826. Add(' TExtC = class (TExtB)');
  14827. Add(' end;');
  14828. Add('var');
  14829. Add(' A: texta;');
  14830. Add(' B: textb;');
  14831. Add(' C: textc;');
  14832. Add('begin');
  14833. Add(' b:=a as textb;');
  14834. Add(' c:=a as textc;');
  14835. Add(' c:=b as textc;');
  14836. ConvertProgram;
  14837. CheckSource('TestExternalClass_Is',
  14838. LinesToStr([ // statements
  14839. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  14840. ' this.$init = function () {',
  14841. ' };',
  14842. ' this.$final = function () {',
  14843. ' };',
  14844. '});',
  14845. 'this.A = null;',
  14846. 'this.B = null;',
  14847. 'this.C = null;',
  14848. '']),
  14849. LinesToStr([ // $mod.$main
  14850. '$mod.B = rtl.asExt($mod.A, ExtB);',
  14851. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  14852. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  14853. '']));
  14854. end;
  14855. procedure TTestModule.TestExternalClass_DestructorFail;
  14856. begin
  14857. StartProgram(false);
  14858. Add('{$modeswitch externalclass}');
  14859. Add('type');
  14860. Add(' TExtA = class external name ''ExtA''');
  14861. Add(' destructor Free;');
  14862. Add(' end;');
  14863. SetExpectedPasResolverError('Pascal element not supported: destructor',
  14864. nPasElementNotSupported);
  14865. ConvertProgram;
  14866. end;
  14867. procedure TTestModule.TestExternalClass_New;
  14868. begin
  14869. StartProgram(false);
  14870. Add('{$modeswitch externalclass}');
  14871. Add('type');
  14872. Add(' TExtA = class external name ''ExtA''');
  14873. Add(' constructor New;');
  14874. Add(' constructor New(i: longint; j: longint = 2);');
  14875. Add(' end;');
  14876. Add('var');
  14877. Add(' A: texta;');
  14878. Add('begin');
  14879. Add(' a:=texta.new;');
  14880. Add(' a:=texta.new();');
  14881. Add(' a:=texta.new(1);');
  14882. Add(' with texta do begin');
  14883. Add(' a:=new;');
  14884. Add(' a:=new();');
  14885. Add(' a:=new(2);');
  14886. Add(' end;');
  14887. Add(' a:=test1.texta.new;');
  14888. Add(' a:=test1.texta.new();');
  14889. Add(' a:=test1.texta.new(3);');
  14890. ConvertProgram;
  14891. CheckSource('TestExternalClass_New',
  14892. LinesToStr([ // statements
  14893. 'this.A = null;',
  14894. '']),
  14895. LinesToStr([ // $mod.$main
  14896. '$mod.A = new ExtA();',
  14897. '$mod.A = new ExtA();',
  14898. '$mod.A = new ExtA(1,2);',
  14899. '$mod.A = new ExtA();',
  14900. '$mod.A = new ExtA();',
  14901. '$mod.A = new ExtA(2,2);',
  14902. '$mod.A = new ExtA();',
  14903. '$mod.A = new ExtA();',
  14904. '$mod.A = new ExtA(3,2);',
  14905. '']));
  14906. end;
  14907. procedure TTestModule.TestExternalClass_ClassOf_New;
  14908. begin
  14909. StartProgram(false);
  14910. Add('{$modeswitch externalclass}');
  14911. Add('type');
  14912. Add(' TExtAClass = class of TExtA;');
  14913. Add(' TExtA = class external name ''ExtA''');
  14914. Add(' C: TExtAClass;');
  14915. Add(' constructor New;');
  14916. Add(' end;');
  14917. Add('var');
  14918. Add(' A: texta;');
  14919. Add(' C: textaclass;');
  14920. Add('begin');
  14921. Add(' a:=c.new;');
  14922. Add(' a:=c.new();');
  14923. Add(' with C do begin');
  14924. Add(' a:=new;');
  14925. Add(' a:=new();');
  14926. Add(' end;');
  14927. Add(' a:=test1.c.new;');
  14928. Add(' a:=test1.c.new();');
  14929. Add(' a:=A.c.new();');
  14930. ConvertProgram;
  14931. CheckSource('TestExternalClass_ClassOf_New',
  14932. LinesToStr([ // statements
  14933. 'this.A = null;',
  14934. 'this.C = null;',
  14935. '']),
  14936. LinesToStr([ // $mod.$main
  14937. '$mod.A = new $mod.C();',
  14938. '$mod.A = new $mod.C();',
  14939. 'var $with1 = $mod.C;',
  14940. '$mod.A = new $with1();',
  14941. '$mod.A = new $with1();',
  14942. '$mod.A = new $mod.C();',
  14943. '$mod.A = new $mod.C();',
  14944. '$mod.A = new $mod.A.C();',
  14945. '']));
  14946. end;
  14947. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  14948. begin
  14949. StartProgram(false);
  14950. Add([
  14951. '{$modeswitch externalclass}',
  14952. 'type',
  14953. ' TExtAClass = class of TExtA;',
  14954. ' TExtA = class external name ''ExtA''',
  14955. ' constructor New;',
  14956. ' end;',
  14957. 'function GetCreator: TExtAClass;',
  14958. 'begin',
  14959. ' Result:=TExtA;',
  14960. 'end;',
  14961. 'var',
  14962. ' A: texta;',
  14963. 'begin',
  14964. ' a:=getcreator.new;',
  14965. ' a:=getcreator().new;',
  14966. ' a:=getcreator().new();',
  14967. ' a:=getcreator.new();',
  14968. ' with getcreator do begin',
  14969. ' a:=new;',
  14970. ' a:=new();',
  14971. ' end;']);
  14972. ConvertProgram;
  14973. CheckSource('TestExternalClass_FuncClassOf_New',
  14974. LinesToStr([ // statements
  14975. 'this.GetCreator = function () {',
  14976. ' var Result = null;',
  14977. ' Result = ExtA;',
  14978. ' return Result;',
  14979. '};',
  14980. 'this.A = null;',
  14981. '']),
  14982. LinesToStr([ // $mod.$main
  14983. '$mod.A = new ($mod.GetCreator())();',
  14984. '$mod.A = new ($mod.GetCreator())();',
  14985. '$mod.A = new ($mod.GetCreator())();',
  14986. '$mod.A = new ($mod.GetCreator())();',
  14987. 'var $with1 = $mod.GetCreator();',
  14988. '$mod.A = new $with1();',
  14989. '$mod.A = new $with1();',
  14990. '']));
  14991. end;
  14992. procedure TTestModule.TestExternalClass_New_PasClassFail;
  14993. begin
  14994. StartProgram(false);
  14995. Add([
  14996. '{$modeswitch externalclass}',
  14997. 'type',
  14998. ' TExtA = class external name ''ExtA''',
  14999. ' constructor New;',
  15000. ' end;',
  15001. ' TBird = class(TExtA)',
  15002. ' end;',
  15003. 'begin',
  15004. ' TBird.new;',
  15005. '']);
  15006. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  15007. ConvertProgram;
  15008. end;
  15009. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  15010. begin
  15011. StartProgram(false);
  15012. Add([
  15013. '{$modeswitch externalclass}',
  15014. 'type',
  15015. ' TExtA = class external name ''ExtA''',
  15016. ' constructor New;',
  15017. ' end;',
  15018. ' TBird = class(TExtA)',
  15019. ' end;',
  15020. 'begin',
  15021. ' TBird.new();',
  15022. '']);
  15023. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  15024. ConvertProgram;
  15025. end;
  15026. procedure TTestModule.TestExternalClass_LocalConstSameName;
  15027. begin
  15028. StartProgram(false);
  15029. Add('{$modeswitch externalclass}');
  15030. Add('type');
  15031. Add(' TExtA = class external name ''ExtA''');
  15032. Add(' constructor New;');
  15033. Add(' end;');
  15034. Add('function DoIt: longint;');
  15035. Add('const ExtA: longint = 3;');
  15036. Add('begin');
  15037. Add(' Result:=ExtA;');
  15038. Add('end;');
  15039. Add('var');
  15040. Add(' A: texta;');
  15041. Add('begin');
  15042. Add(' a:=texta.new;');
  15043. ConvertProgram;
  15044. CheckSource('TestExternalClass_LocalConstSameName',
  15045. LinesToStr([ // statements
  15046. 'var ExtA$1 = 3;',
  15047. 'this.DoIt = function () {',
  15048. ' var Result = 0;',
  15049. ' Result = ExtA$1;',
  15050. ' return Result;',
  15051. '};',
  15052. 'this.A = null;',
  15053. '']),
  15054. LinesToStr([ // $mod.$main
  15055. '$mod.A = new ExtA();',
  15056. '']));
  15057. end;
  15058. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  15059. begin
  15060. StartProgram(false);
  15061. Add('{$modeswitch externalclass}');
  15062. Add('type');
  15063. Add(' TExtA = class external name ''ExtA''');
  15064. Add(' procedure DoIt;');
  15065. Add(' end;');
  15066. Add(' TMyA = class(TExtA)');
  15067. Add(' procedure DoIt;');
  15068. Add(' end;');
  15069. Add('procedure TMyA.DoIt; begin end;');
  15070. Add('begin');
  15071. ConvertProgram;
  15072. CheckSource('TestExternalClass_ReintroduceOverload',
  15073. LinesToStr([ // statements
  15074. 'rtl.createClassExt($mod, "TMyA", ExtA, "", function () {',
  15075. ' this.$init = function () {',
  15076. ' };',
  15077. ' this.$final = function () {',
  15078. ' };',
  15079. ' this.DoIt$1 = function () {',
  15080. ' };',
  15081. '});',
  15082. '']),
  15083. LinesToStr([ // $mod.$main
  15084. '']));
  15085. end;
  15086. procedure TTestModule.TestExternalClass_Inherited;
  15087. begin
  15088. StartProgram(false);
  15089. Add('{$modeswitch externalclass}');
  15090. Add('type');
  15091. Add(' TExtA = class external name ''ExtA''');
  15092. Add(' procedure DoIt(i: longint = 1); virtual;');
  15093. Add(' procedure DoSome(j: longint = 2);');
  15094. Add(' end;');
  15095. Add(' TExtB = class external name ''ExtB''(TExtA)');
  15096. Add(' end;');
  15097. Add(' TMyC = class(TExtB)');
  15098. Add(' procedure DoIt(i: longint = 1); override;');
  15099. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  15100. Add(' end;');
  15101. Add('procedure TMyC.DoIt(i: longint);');
  15102. Add('begin');
  15103. Add(' inherited;');
  15104. Add(' inherited DoIt;');
  15105. Add(' inherited DoIt();');
  15106. Add(' inherited DoIt(3);');
  15107. Add(' inherited DoSome;');
  15108. Add(' inherited DoSome();');
  15109. Add(' inherited DoSome(4);');
  15110. Add('end;');
  15111. Add('procedure TMyC.DoSome(j: longint);');
  15112. Add('begin');
  15113. Add(' inherited;');
  15114. Add('end;');
  15115. Add('begin');
  15116. ConvertProgram;
  15117. CheckSource('TestExternalClass_ReintroduceOverload',
  15118. LinesToStr([ // statements
  15119. 'rtl.createClassExt($mod, "TMyC", ExtB, "", function () {',
  15120. ' this.$init = function () {',
  15121. ' };',
  15122. ' this.$final = function () {',
  15123. ' };',
  15124. ' this.DoIt = function (i) {',
  15125. ' ExtB.DoIt.apply(this, arguments);',
  15126. ' ExtB.DoIt.call(this, 1);',
  15127. ' ExtB.DoIt.call(this, 1);',
  15128. ' ExtB.DoIt.call(this, 3);',
  15129. ' ExtB.DoSome.call(this, 2);',
  15130. ' ExtB.DoSome.call(this, 2);',
  15131. ' ExtB.DoSome.call(this, 4);',
  15132. ' };',
  15133. ' this.DoSome$1 = function (j) {',
  15134. ' ExtB.DoSome.apply(this, arguments);',
  15135. ' };',
  15136. '});',
  15137. '']),
  15138. LinesToStr([ // $mod.$main
  15139. '']));
  15140. end;
  15141. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  15142. begin
  15143. StartProgram(false);
  15144. Add('{$modeswitch externalclass}');
  15145. Add('type');
  15146. Add(' TObject = class');
  15147. Add(' end;');
  15148. Add(' TExtA = class external name ''ExtA''(TObject)');
  15149. Add(' end;');
  15150. Add('begin');
  15151. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  15152. ConvertProgram;
  15153. end;
  15154. procedure TTestModule.TestExternalClass_NewInstance;
  15155. begin
  15156. StartProgram(false);
  15157. Add('{$modeswitch externalclass}');
  15158. Add('type');
  15159. Add(' TExtA = class external name ''ExtA''');
  15160. Add(' end;');
  15161. Add(' TMyB = class(TExtA)');
  15162. Add(' protected');
  15163. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  15164. Add(' end;');
  15165. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  15166. Add('begin end;');
  15167. Add('begin');
  15168. ConvertProgram;
  15169. CheckSource('TestExternalClass_NewInstance',
  15170. LinesToStr([ // statements
  15171. 'rtl.createClassExt($mod, "TMyB", ExtA, "NewInstance", function () {',
  15172. ' this.$init = function () {',
  15173. ' };',
  15174. ' this.$final = function () {',
  15175. ' };',
  15176. ' this.NewInstance = function (fnname, paramarray) {',
  15177. ' var Result = null;',
  15178. ' return Result;',
  15179. ' };',
  15180. '});',
  15181. '']),
  15182. LinesToStr([ // $mod.$main
  15183. '']));
  15184. end;
  15185. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  15186. begin
  15187. StartProgram(false);
  15188. Add('{$modeswitch externalclass}');
  15189. Add('type');
  15190. Add(' TExtA = class external name ''ExtA''');
  15191. Add(' end;');
  15192. Add(' TMyB = class(TExtA)');
  15193. Add(' protected');
  15194. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  15195. Add(' end;');
  15196. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  15197. Add('begin end;');
  15198. Add('begin');
  15199. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  15200. ConvertProgram;
  15201. end;
  15202. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  15203. begin
  15204. StartProgram(false);
  15205. Add('{$modeswitch externalclass}');
  15206. Add('type');
  15207. Add(' TExtA = class external name ''ExtA''');
  15208. Add(' end;');
  15209. Add(' TMyB = class(TExtA)');
  15210. Add(' protected');
  15211. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  15212. Add(' end;');
  15213. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  15214. Add('begin end;');
  15215. Add('begin');
  15216. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Longint", expected "String"',
  15217. nIncompatibleTypeArgNo);
  15218. ConvertProgram;
  15219. end;
  15220. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  15221. begin
  15222. StartProgram(false);
  15223. Add('{$modeswitch externalclass}');
  15224. Add('type');
  15225. Add(' TExtA = class external name ''ExtA''');
  15226. Add(' end;');
  15227. Add(' TMyB = class(TExtA)');
  15228. Add(' protected');
  15229. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  15230. Add(' end;');
  15231. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  15232. Add('begin end;');
  15233. Add('begin');
  15234. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "type", expected "untyped"',
  15235. nIncompatibleTypeArgNo);
  15236. ConvertProgram;
  15237. end;
  15238. procedure TTestModule.TestExternalClass_PascalProperty;
  15239. begin
  15240. StartProgram(false);
  15241. Add('{$modeswitch externalclass}');
  15242. Add('type');
  15243. Add(' TJSElement = class;');
  15244. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  15245. Add(' TJSElement = class external name ''ExtA''');
  15246. Add(' end;');
  15247. Add(' TControl = class(TJSElement)');
  15248. Add(' private');
  15249. Add(' FOnClick: TJSNotifyEvent;');
  15250. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  15251. Add(' procedure Click(Sender: TJSElement);');
  15252. Add(' end;');
  15253. Add('procedure TControl.Click(Sender: TJSElement);');
  15254. Add('begin');
  15255. Add(' OnClick(Self);');
  15256. Add('end;');
  15257. Add('var');
  15258. Add(' Ctrl: TControl;');
  15259. Add('begin');
  15260. Add(' Ctrl.OnClick:[email protected];');
  15261. Add(' Ctrl.OnClick(Ctrl);');
  15262. ConvertProgram;
  15263. CheckSource('TestExternalClass_PascalProperty',
  15264. LinesToStr([ // statements
  15265. 'rtl.createClassExt($mod, "TControl", ExtA, "", function () {',
  15266. ' this.$init = function () {',
  15267. ' this.FOnClick = null;',
  15268. ' };',
  15269. ' this.$final = function () {',
  15270. ' this.FOnClick = undefined;',
  15271. ' };',
  15272. ' this.Click = function (Sender) {',
  15273. ' this.FOnClick(this);',
  15274. ' };',
  15275. '});',
  15276. 'this.Ctrl = null;',
  15277. '']),
  15278. LinesToStr([ // $mod.$main
  15279. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  15280. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  15281. '']));
  15282. end;
  15283. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  15284. begin
  15285. StartProgram(false);
  15286. Add([
  15287. '{$modeswitch externalclass}',
  15288. 'type',
  15289. ' IUnknown = interface end;',
  15290. ' TObject = class',
  15291. ' end;',
  15292. ' TChild = class',
  15293. ' end;',
  15294. ' TExtRootA = class external name ''ExtRootA''',
  15295. ' end;',
  15296. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  15297. ' end;',
  15298. ' TExtRootB = class external name ''ExtRootB''',
  15299. ' end;',
  15300. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  15301. ' end;',
  15302. 'var',
  15303. ' Obj: TObject;',
  15304. ' Child: TChild;',
  15305. ' RootA: TExtRootA;',
  15306. ' ChildA: TExtChildA;',
  15307. ' RootB: TExtRootB;',
  15308. ' ChildB: TExtChildB;',
  15309. ' i: IUnknown;',
  15310. 'begin',
  15311. ' obj:=tobject(roota);',
  15312. ' obj:=tobject(childa);',
  15313. ' child:=tchild(tobject(roota));',
  15314. ' roota:=textroota(obj);',
  15315. ' roota:=textroota(child);',
  15316. ' roota:=textroota(rootb);',
  15317. ' roota:=textroota(childb);',
  15318. ' childa:=textchilda(textroota(obj));',
  15319. ' roota:=TExtRootA(i)',
  15320. '']);
  15321. ConvertProgram;
  15322. CheckSource('TestExternalClass_TypeCastToRootClass',
  15323. LinesToStr([ // statements
  15324. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  15325. 'rtl.createClass($mod, "TObject", null, function () {',
  15326. ' this.$init = function () {',
  15327. ' };',
  15328. ' this.$final = function () {',
  15329. ' };',
  15330. '});',
  15331. 'rtl.createClass($mod, "TChild", $mod.TObject, function () {',
  15332. '});',
  15333. 'this.Obj = null;',
  15334. 'this.Child = null;',
  15335. 'this.RootA = null;',
  15336. 'this.ChildA = null;',
  15337. 'this.RootB = null;',
  15338. 'this.ChildB = null;',
  15339. 'this.i = null;',
  15340. '']),
  15341. LinesToStr([ // $mod.$main
  15342. '$mod.Obj = $mod.RootA;',
  15343. '$mod.Obj = $mod.ChildA;',
  15344. '$mod.Child = $mod.RootA;',
  15345. '$mod.RootA = $mod.Obj;',
  15346. '$mod.RootA = $mod.Child;',
  15347. '$mod.RootA = $mod.RootB;',
  15348. '$mod.RootA = $mod.ChildB;',
  15349. '$mod.ChildA = $mod.Obj;',
  15350. '$mod.RootA = $mod.i;',
  15351. '']));
  15352. end;
  15353. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  15354. begin
  15355. StartProgram(false);
  15356. Add([
  15357. '{$modeswitch externalclass}',
  15358. 'type',
  15359. ' IUnknown = interface end;',
  15360. ' IBird = interface(IUnknown) end;',
  15361. ' TClass = class of TObject;',
  15362. ' TObject = class',
  15363. ' end;',
  15364. ' TChild = class',
  15365. ' end;',
  15366. ' TJSObject = class external name ''Object''',
  15367. ' end;',
  15368. ' TRec = record end;',
  15369. 'var',
  15370. ' Obj: TObject;',
  15371. ' Child: TChild;',
  15372. ' i: IUnknown;',
  15373. ' Bird: IBird;',
  15374. ' j: TJSObject;',
  15375. ' r: TRec;',
  15376. ' c: TClass;',
  15377. 'begin',
  15378. ' j:=tjsobject(IUnknown);',
  15379. ' j:=tjsobject(IBird);',
  15380. ' j:=tjsobject(TObject);',
  15381. ' j:=tjsobject(TChild);',
  15382. ' j:=tjsobject(TRec);',
  15383. ' j:=tjsobject(Obj);',
  15384. ' j:=tjsobject(Child);',
  15385. ' j:=tjsobject(i);',
  15386. ' j:=tjsobject(Bird);',
  15387. ' j:=tjsobject(r);',
  15388. ' j:=tjsobject(c);',
  15389. '']);
  15390. ConvertProgram;
  15391. CheckSource('TestExternalClass_TypeCastToJSObject',
  15392. LinesToStr([ // statements
  15393. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  15394. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], $mod.IUnknown);',
  15395. 'rtl.createClass($mod, "TObject", null, function () {',
  15396. ' this.$init = function () {',
  15397. ' };',
  15398. ' this.$final = function () {',
  15399. ' };',
  15400. '});',
  15401. 'rtl.createClass($mod, "TChild", $mod.TObject, function () {',
  15402. '});',
  15403. 'rtl.recNewT($mod, "TRec", function () {',
  15404. ' this.$eq = function (b) {',
  15405. ' return true;',
  15406. ' };',
  15407. ' this.$assign = function (s) {',
  15408. ' return this;',
  15409. ' };',
  15410. '});',
  15411. 'this.Obj = null;',
  15412. 'this.Child = null;',
  15413. 'this.i = null;',
  15414. 'this.Bird = null;',
  15415. 'this.j = null;',
  15416. 'this.r = $mod.TRec.$new();',
  15417. 'this.c = null;',
  15418. '']),
  15419. LinesToStr([ // $mod.$main
  15420. '$mod.j = $mod.IUnknown;',
  15421. '$mod.j = $mod.IBird;',
  15422. '$mod.j = $mod.TObject;',
  15423. '$mod.j = $mod.TChild;',
  15424. '$mod.j = $mod.TRec;',
  15425. '$mod.j = $mod.Obj;',
  15426. '$mod.j = $mod.Child;',
  15427. '$mod.j = $mod.i;',
  15428. '$mod.j = $mod.Bird;',
  15429. '$mod.j = $mod.r;',
  15430. '$mod.j = $mod.c;',
  15431. '']));
  15432. end;
  15433. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  15434. begin
  15435. StartProgram(false);
  15436. Add('{$modeswitch externalclass}');
  15437. Add('type');
  15438. Add(' TJSString = class external name ''String''');
  15439. Add(' class function fromCharCode() : string; varargs;');
  15440. Add(' function anchor(const aName : string) : string;');
  15441. Add(' end;');
  15442. Add('var');
  15443. Add(' s: string;');
  15444. Add('begin');
  15445. Add(' s:=TJSString.fromCharCode(65,66);');
  15446. Add(' s:=TJSString(s).anchor(s);');
  15447. Add(' s:=TJSString(''foo'').anchor(s);');
  15448. ConvertProgram;
  15449. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  15450. LinesToStr([ // statements
  15451. 'this.s = "";',
  15452. '']),
  15453. LinesToStr([ // $mod.$main
  15454. '$mod.s = String.fromCharCode(65, 66);',
  15455. '$mod.s = $mod.s.anchor($mod.s);',
  15456. '$mod.s = "foo".anchor($mod.s);',
  15457. '']));
  15458. end;
  15459. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  15460. begin
  15461. StartProgram(false);
  15462. Add([
  15463. '{$modeswitch externalclass}',
  15464. 'type',
  15465. ' TJSObject = class external name ''Object'' end;',
  15466. ' TJSFunction = class external name ''Function''',
  15467. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  15468. ' function call(thisArg: TJSObject): JSValue; varargs;',
  15469. ' end;',
  15470. ' TObject = class',
  15471. ' procedure DoIt(i: longint);',
  15472. ' end;',
  15473. ' TFuncInt = function(o: TObject): longint;',
  15474. 'function GetIt(o: TObject): longint;',
  15475. ' procedure Sub; begin end;',
  15476. 'var',
  15477. ' f: TJSFunction;',
  15478. ' fi: TFuncInt;',
  15479. 'begin',
  15480. ' fi:=TFuncInt(f);',
  15481. ' f:=TJSFunction(fi);',
  15482. ' f:=TJSFunction(@GetIt);',
  15483. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  15484. ' f:=TJSFunction(@Sub);',
  15485. ' f:=TJSFunction(@o.doit);',
  15486. ' f:=TJSFunction(fi).bind(nil,4)',
  15487. 'end;',
  15488. 'procedure TObject.DoIt(i: longint);',
  15489. ' procedure Sub; begin end;',
  15490. 'var f: TJSFunction;',
  15491. 'begin',
  15492. ' f:=TJSFunction(@DoIt);',
  15493. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  15494. ' f:=TJSFunction(@Sub);',
  15495. ' f:=TJSFunction(@GetIt);',
  15496. 'end;',
  15497. 'begin']);
  15498. ConvertProgram;
  15499. CheckSource('TestExternalClass_TypeCastToJSFunction',
  15500. LinesToStr([ // statements
  15501. 'rtl.createClass($mod, "TObject", null, function () {',
  15502. ' this.$init = function () {',
  15503. ' };',
  15504. ' this.$final = function () {',
  15505. ' };',
  15506. ' this.DoIt = function (i) {',
  15507. ' var Self = this;',
  15508. ' function Sub() {',
  15509. ' };',
  15510. ' var f = null;',
  15511. ' f = rtl.createCallback(Self, "DoIt");',
  15512. ' f = rtl.createCallback(Self, "DoIt").bind(null, 13);',
  15513. ' f = Sub;',
  15514. ' f = $mod.GetIt;',
  15515. ' };',
  15516. '});',
  15517. 'this.GetIt = function (o) {',
  15518. ' var Result = 0;',
  15519. ' function Sub() {',
  15520. ' };',
  15521. ' var f = null;',
  15522. ' var fi = null;',
  15523. ' fi = f;',
  15524. ' f = fi;',
  15525. ' f = $mod.GetIt;',
  15526. ' f = $mod.GetIt.bind(null, 3);',
  15527. ' f = Sub;',
  15528. ' f = rtl.createCallback(o, "DoIt");',
  15529. ' f = fi.bind(null, 4);',
  15530. ' return Result;',
  15531. '};',
  15532. '']),
  15533. LinesToStr([ // $mod.$main
  15534. '']));
  15535. end;
  15536. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  15537. begin
  15538. StartProgram(false);
  15539. Add('{$modeswitch externalclass}');
  15540. Add('type');
  15541. Add(' TJSString = class external name ''String''');
  15542. Add(' class function fromCharCode() : string; varargs;');
  15543. Add(' end;');
  15544. Add('var');
  15545. Add(' s: string;');
  15546. Add(' sObj: TJSString;');
  15547. Add('begin');
  15548. Add(' s:=sObj.fromCharCode(65,66);');
  15549. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  15550. nExternalClassInstanceCannotAccessStaticX);
  15551. ConvertProgram;
  15552. end;
  15553. procedure TTestModule.TestExternalClass_BracketAccessor;
  15554. begin
  15555. StartProgram(false);
  15556. Add([
  15557. '{$modeswitch externalclass}',
  15558. 'type',
  15559. ' TJSArray = class external name ''Array2''',
  15560. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  15561. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  15562. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  15563. ' end;',
  15564. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  15565. 'begin end;',
  15566. 'var',
  15567. ' Arr: tjsarray;',
  15568. ' s: string;',
  15569. ' i: longint;',
  15570. ' v: jsvalue;',
  15571. 'begin',
  15572. ' v:=arr[0];',
  15573. ' v:=arr.items[1];',
  15574. ' arr[2]:=s;',
  15575. ' arr.items[3]:=s;',
  15576. ' arr[4]:=i;',
  15577. ' arr[5]:=arr[6];',
  15578. ' arr.items[7]:=arr.items[8];',
  15579. ' with arr do items[9]:=items[10];',
  15580. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  15581. ' with arr do begin',
  15582. ' v:=GetItems(14);',
  15583. ' setitems(15,16);',
  15584. ' end;',
  15585. ' v:=test1.arr.items[17];',
  15586. ' test1.arr.items[18]:=v;',
  15587. '']);
  15588. ConvertProgram;
  15589. CheckSource('TestExternalClass_BracketAccessor',
  15590. LinesToStr([ // statements
  15591. 'this.DoIt = function (vI, vJ, vK, vL) {',
  15592. '};',
  15593. 'this.Arr = null;',
  15594. 'this.s = "";',
  15595. 'this.i = 0;',
  15596. 'this.v = undefined;',
  15597. '']),
  15598. LinesToStr([ // $mod.$main
  15599. '$mod.v = $mod.Arr[0];',
  15600. '$mod.v = $mod.Arr[1];',
  15601. '$mod.Arr[2] = $mod.s;',
  15602. '$mod.Arr[3] = $mod.s;',
  15603. '$mod.Arr[4] = $mod.i;',
  15604. '$mod.Arr[5] = $mod.Arr[6];',
  15605. '$mod.Arr[7] = $mod.Arr[8];',
  15606. 'var $with1 = $mod.Arr;',
  15607. '$with1[9] = $with1[10];',
  15608. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  15609. ' a: 9,',
  15610. ' p: $mod.Arr,',
  15611. ' get: function () {',
  15612. ' return this.p[this.a];',
  15613. ' },',
  15614. ' set: function (v) {',
  15615. ' this.p[this.a] = v;',
  15616. ' }',
  15617. '}, {',
  15618. ' a: 10,',
  15619. ' p: $mod.Arr,',
  15620. ' get: function () {',
  15621. ' return this.p[this.a];',
  15622. ' },',
  15623. ' set: function (v) {',
  15624. ' this.p[this.a] = v;',
  15625. ' }',
  15626. '});',
  15627. 'var $with2 = $mod.Arr;',
  15628. '$mod.v = $with2[14];',
  15629. '$with2[15] = 16;',
  15630. '$mod.v = $mod.Arr[17];',
  15631. '$mod.Arr[18] = $mod.v;',
  15632. '']));
  15633. end;
  15634. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  15635. begin
  15636. StartProgram(false);
  15637. Add([
  15638. '{$modeswitch externalclass}',
  15639. 'type',
  15640. ' TJSArray = class external name ''Array2''',
  15641. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  15642. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  15643. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  15644. ' end;',
  15645. ' TMyArr = class(TJSArray)',
  15646. ' procedure DoIt;',
  15647. ' end;',
  15648. 'procedure tmyarr.DoIt;',
  15649. 'begin',
  15650. ' Items[1]:=Items[2];',
  15651. ' SetItems(3,getItems(4));',
  15652. 'end;',
  15653. 'var',
  15654. ' Arr: tmyarr;',
  15655. ' s: string;',
  15656. ' i: longint;',
  15657. ' v: jsvalue;',
  15658. 'begin',
  15659. ' v:=arr[0];',
  15660. ' v:=arr.items[1];',
  15661. ' arr[2]:=s;',
  15662. ' arr.items[3]:=s;',
  15663. ' arr[4]:=i;',
  15664. ' arr[5]:=arr[6];',
  15665. ' arr.items[7]:=arr.items[8];',
  15666. ' with arr do items[9]:=items[10];',
  15667. ' with arr do begin',
  15668. ' v:=GetItems(14);',
  15669. ' setitems(15,16);',
  15670. ' end;',
  15671. '']);
  15672. ConvertProgram;
  15673. CheckSource('TestExternalClass_BracketAccessor_Call',
  15674. LinesToStr([ // statements
  15675. 'rtl.createClassExt($mod, "TMyArr", Array2, "", function () {',
  15676. ' this.$init = function () {',
  15677. ' };',
  15678. ' this.$final = function () {',
  15679. ' };',
  15680. ' this.DoIt = function () {',
  15681. ' this[1] = this[2];',
  15682. ' this[3] = this[4];',
  15683. ' };',
  15684. '});',
  15685. 'this.Arr = null;',
  15686. 'this.s = "";',
  15687. 'this.i = 0;',
  15688. 'this.v = undefined;',
  15689. '']),
  15690. LinesToStr([ // $mod.$main
  15691. '$mod.v = $mod.Arr[0];',
  15692. '$mod.v = $mod.Arr[1];',
  15693. '$mod.Arr[2] = $mod.s;',
  15694. '$mod.Arr[3] = $mod.s;',
  15695. '$mod.Arr[4] = $mod.i;',
  15696. '$mod.Arr[5] = $mod.Arr[6];',
  15697. '$mod.Arr[7] = $mod.Arr[8];',
  15698. 'var $with1 = $mod.Arr;',
  15699. '$with1[9] = $with1[10];',
  15700. 'var $with2 = $mod.Arr;',
  15701. '$mod.v = $with2[14];',
  15702. '$with2[15] = 16;',
  15703. '']));
  15704. end;
  15705. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  15706. begin
  15707. StartProgram(false);
  15708. Add('{$modeswitch externalclass}');
  15709. Add('type');
  15710. Add(' TJSArray = class external name ''Array2''');
  15711. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  15712. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  15713. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  15714. Add(' end;');
  15715. Add('begin');
  15716. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  15717. nBracketAccessorOfExternalClassMustHaveOneParameter);
  15718. ConvertProgram;
  15719. end;
  15720. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  15721. begin
  15722. StartProgram(false);
  15723. Add('{$modeswitch externalclass}');
  15724. Add('type');
  15725. Add(' TJSArray = class external name ''Array2''');
  15726. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  15727. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  15728. Add(' end;');
  15729. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  15730. Add('begin end;');
  15731. Add('var');
  15732. Add(' Arr: tjsarray;');
  15733. Add(' v: jsvalue;');
  15734. Add('begin');
  15735. Add(' v:=arr[0];');
  15736. Add(' v:=arr.items[1];');
  15737. Add(' with arr do v:=items[2];');
  15738. Add(' doit(arr[3],arr[4]);');
  15739. ConvertProgram;
  15740. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  15741. LinesToStr([ // statements
  15742. 'this.DoIt = function (vI, vJ) {',
  15743. '};',
  15744. 'this.Arr = null;',
  15745. 'this.v = undefined;',
  15746. '']),
  15747. LinesToStr([ // $mod.$main
  15748. '$mod.v = $mod.Arr[0];',
  15749. '$mod.v = $mod.Arr[1];',
  15750. 'var $with1 = $mod.Arr;',
  15751. '$mod.v = $with1[2];',
  15752. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  15753. '']));
  15754. end;
  15755. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  15756. begin
  15757. StartProgram(false);
  15758. Add('{$modeswitch externalclass}');
  15759. Add('type');
  15760. Add(' TJSArray = class external name ''Array2''');
  15761. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  15762. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  15763. Add(' end;');
  15764. Add('var');
  15765. Add(' Arr: tjsarray;');
  15766. Add(' s: string;');
  15767. Add(' i: longint;');
  15768. Add(' v: jsvalue;');
  15769. Add('begin');
  15770. Add(' arr[2]:=s;');
  15771. Add(' arr.items[3]:=s;');
  15772. Add(' arr[4]:=i;');
  15773. Add(' with arr do items[5]:=i;');
  15774. ConvertProgram;
  15775. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  15776. LinesToStr([ // statements
  15777. 'this.Arr = null;',
  15778. 'this.s = "";',
  15779. 'this.i = 0;',
  15780. 'this.v = undefined;',
  15781. '']),
  15782. LinesToStr([ // $mod.$main
  15783. '$mod.Arr[2] = $mod.s;',
  15784. '$mod.Arr[3] = $mod.s;',
  15785. '$mod.Arr[4] = $mod.i;',
  15786. 'var $with1 = $mod.Arr;',
  15787. '$with1[5] = $mod.i;',
  15788. '']));
  15789. end;
  15790. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  15791. begin
  15792. StartProgram(false);
  15793. Add('{$modeswitch externalclass}');
  15794. Add('type');
  15795. Add(' TJSArray = class external name ''Array2''');
  15796. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  15797. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  15798. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  15799. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  15800. Add(' end;');
  15801. Add('var');
  15802. Add(' Arr: tjsarray;');
  15803. Add(' s: string;');
  15804. Add(' i: longint;');
  15805. Add(' v: jsvalue;');
  15806. Add('begin');
  15807. Add(' arr[2]:=s;');
  15808. Add(' arr.items[3]:=s;');
  15809. Add(' arr.numbers[4]:=i;');
  15810. Add(' with arr do items[5]:=i;');
  15811. Add(' with arr do numbers[6]:=i;');
  15812. ConvertProgram;
  15813. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  15814. LinesToStr([ // statements
  15815. 'this.Arr = null;',
  15816. 'this.s = "";',
  15817. 'this.i = 0;',
  15818. 'this.v = undefined;',
  15819. '']),
  15820. LinesToStr([ // $mod.$main
  15821. '$mod.Arr[2] = $mod.s;',
  15822. '$mod.Arr[3] = $mod.s;',
  15823. '$mod.Arr[4] = $mod.i;',
  15824. 'var $with1 = $mod.Arr;',
  15825. '$with1[5] = $mod.i;',
  15826. 'var $with2 = $mod.Arr;',
  15827. '$with2[6] = $mod.i;',
  15828. '']));
  15829. end;
  15830. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  15831. begin
  15832. StartProgram(false);
  15833. Add('{$modeswitch externalclass}');
  15834. Add('type');
  15835. Add(' TJSArray = class external name ''Array2''');
  15836. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  15837. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  15838. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  15839. Add(' end;');
  15840. Add('var');
  15841. Add(' Arr: tjsarray;');
  15842. Add(' i: longint;');
  15843. Add(' IntArr: array of longint;');
  15844. Add(' v: jsvalue;');
  15845. Add('begin');
  15846. Add(' v:=arr.items[i];');
  15847. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  15848. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  15849. ConvertProgram;
  15850. CheckSource('TestExternalClass_BracketAccessor_Index',
  15851. LinesToStr([ // statements
  15852. 'this.Arr = null;',
  15853. 'this.i = 0;',
  15854. 'this.IntArr = [];',
  15855. 'this.v = undefined;',
  15856. '']),
  15857. LinesToStr([ // $mod.$main
  15858. '$mod.v = $mod.Arr[$mod.i];',
  15859. '$mod.Arr[Math.floor($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  15860. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  15861. '']));
  15862. end;
  15863. procedure TTestModule.TestExternalClass_ForInJSObject;
  15864. begin
  15865. StartProgram(false);
  15866. Add([
  15867. '{$modeswitch externalclass}',
  15868. 'type',
  15869. ' TJSObject = class external name ''Object''',
  15870. ' end;',
  15871. 'var',
  15872. ' o: TJSObject;',
  15873. ' key: string;',
  15874. 'begin',
  15875. ' for key in o do',
  15876. ' if key=''abc'' then ;',
  15877. '']);
  15878. ConvertProgram;
  15879. CheckSource('TestExternalClass_ForInJSObject',
  15880. LinesToStr([ // statements
  15881. 'this.o = null;',
  15882. 'this.key = "";',
  15883. '']),
  15884. LinesToStr([ // $mod.$main
  15885. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  15886. '']));
  15887. end;
  15888. procedure TTestModule.TestExternalClass_ForInJSArray;
  15889. begin
  15890. StartProgram(false);
  15891. Add([
  15892. '{$modeswitch externalclass}',
  15893. 'type',
  15894. ' TJSInt8Array = class external name ''Int8Array''',
  15895. ' private',
  15896. ' flength: NativeInt external name ''length'';',
  15897. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  15898. ' public',
  15899. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  15900. ' property Length: NativeInt read flength;',
  15901. ' end;',
  15902. 'var',
  15903. ' a: TJSInt8Array;',
  15904. ' value: shortint;',
  15905. 'begin',
  15906. ' for value in a do',
  15907. ' if value=3 then ;',
  15908. '']);
  15909. ConvertProgram;
  15910. CheckSource('TestExternalClass_ForInJSArray',
  15911. LinesToStr([ // statements
  15912. 'this.a = null;',
  15913. 'this.value = 0;',
  15914. '']),
  15915. LinesToStr([ // $mod.$main
  15916. 'for (var $in1 = $mod.a, $l2 = 0, $end3 = rtl.length($in1) - 1; $l2 <= $end3; $l2++) {',
  15917. ' $mod.value = $in1[$l2];',
  15918. ' if ($mod.value === 3) ;',
  15919. '};',
  15920. '']));
  15921. end;
  15922. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  15923. begin
  15924. AddModuleWithIntfImplSrc('unit2.pas',
  15925. LinesToStr([
  15926. '{$modeswitch externalclass}',
  15927. 'type',
  15928. ' TJSBufferSource = class external name ''BufferSource''',
  15929. ' end;',
  15930. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  15931. '']),
  15932. '');
  15933. AddModuleWithIntfImplSrc('unit3.pas',
  15934. LinesToStr([
  15935. '{$modeswitch externalclass}',
  15936. 'type',
  15937. ' TJSBufferSource = class external name ''BufferSource''',
  15938. ' end;',
  15939. '']),
  15940. '');
  15941. StartUnit(true);
  15942. Add([
  15943. 'interface',
  15944. 'uses unit2, unit3;',
  15945. 'procedure DoSome(s: TJSBufferSource);',
  15946. 'implementation',
  15947. 'procedure DoSome(s: TJSBufferSource);',
  15948. 'begin',
  15949. ' DoIt(s);',
  15950. 'end;',
  15951. '']);
  15952. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  15953. nIncompatibleTypeArgNo);
  15954. ConvertUnit;
  15955. end;
  15956. procedure TTestModule.TestClassInterface_Corba;
  15957. begin
  15958. StartProgram(false);
  15959. Add([
  15960. '{$interfaces corba}',
  15961. 'type',
  15962. ' IUnknown = interface;',
  15963. ' IUnknown = interface',
  15964. ' [''{00000000-0000-0000-C000-000000000046}'']',
  15965. ' end;',
  15966. ' IInterface = IUnknown;',
  15967. ' IBird = interface(IInterface)',
  15968. ' function GetSize: longint;',
  15969. ' procedure SetSize(i: longint);',
  15970. ' property Size: longint read GetSize write SetSize;',
  15971. ' procedure DoIt(i: longint);',
  15972. ' end;',
  15973. ' TObject = class',
  15974. ' end;',
  15975. ' TBird = class(TObject,IBird)',
  15976. ' function GetSize: longint; virtual; abstract;',
  15977. ' procedure SetSize(i: longint); virtual; abstract;',
  15978. ' procedure DoIt(i: longint); virtual; abstract;',
  15979. ' end;',
  15980. 'var',
  15981. ' BirdIntf: IBird;',
  15982. 'begin',
  15983. ' BirdIntf.Size:=BirdIntf.Size;',
  15984. '']);
  15985. ConvertProgram;
  15986. CheckSource('TestClassInterface_Corba',
  15987. LinesToStr([ // statements
  15988. 'rtl.createInterface($mod, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  15989. 'rtl.createInterface($mod, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], $mod.IUnknown);',
  15990. 'rtl.createClass($mod, "TObject", null, function () {',
  15991. ' this.$init = function () {',
  15992. ' };',
  15993. ' this.$final = function () {',
  15994. ' };',
  15995. '});',
  15996. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  15997. ' rtl.addIntf(this, $mod.IBird);',
  15998. '});',
  15999. 'this.BirdIntf = null;',
  16000. '']),
  16001. LinesToStr([ // $mod.$main
  16002. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  16003. '']));
  16004. end;
  16005. procedure TTestModule.TestClassInterface_ProcExternalFail;
  16006. begin
  16007. StartProgram(false);
  16008. Add([
  16009. '{$interfaces corba}',
  16010. 'type',
  16011. ' IUnknown = interface',
  16012. ' procedure DoIt; external name ''foo'';',
  16013. ' end;',
  16014. 'begin']);
  16015. SetExpectedParserError(
  16016. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  16017. nParserNoFieldsAllowed);
  16018. ConvertProgram;
  16019. end;
  16020. procedure TTestModule.TestClassInterface_Overloads;
  16021. begin
  16022. StartProgram(false);
  16023. Add([
  16024. '{$interfaces corba}',
  16025. 'type',
  16026. ' integer = longint;',
  16027. ' IUnknown = interface',
  16028. ' procedure DoIt(i: integer);',
  16029. ' procedure DoIt(s: string);',
  16030. ' end;',
  16031. ' IBird = interface(IUnknown)',
  16032. ' procedure DoIt(b: boolean); overload;',
  16033. ' end;',
  16034. ' TObject = class',
  16035. ' end;',
  16036. ' TBird = class(TObject,IBird)',
  16037. ' procedure DoIt(o: TObject);',
  16038. ' procedure DoIt(s: string);',
  16039. ' procedure DoIt(i: integer);',
  16040. ' procedure DoIt(b: boolean);',
  16041. ' end;',
  16042. 'procedure TBird.DoIt(o: TObject); begin end;',
  16043. 'procedure TBird.DoIt(s: string); begin end;',
  16044. 'procedure TBird.DoIt(i: integer); begin end;',
  16045. 'procedure TBird.DoIt(b: boolean); begin end;',
  16046. 'var',
  16047. ' BirdIntf: IBird;',
  16048. 'begin',
  16049. ' BirdIntf.DoIt(3);',
  16050. ' BirdIntf.DoIt(''abc'');',
  16051. ' BirdIntf.DoIt(true);',
  16052. '']);
  16053. ConvertProgram;
  16054. CheckSource('TestClassInterface_Overloads',
  16055. LinesToStr([ // statements
  16056. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  16057. 'rtl.createInterface($mod, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], $mod.IUnknown);',
  16058. 'rtl.createClass($mod, "TObject", null, function () {',
  16059. ' this.$init = function () {',
  16060. ' };',
  16061. ' this.$final = function () {',
  16062. ' };',
  16063. '});',
  16064. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16065. ' this.DoIt = function (o) {',
  16066. ' };',
  16067. ' this.DoIt$1 = function (s) {',
  16068. ' };',
  16069. ' this.DoIt$2 = function (i) {',
  16070. ' };',
  16071. ' this.DoIt$3 = function (b) {',
  16072. ' };',
  16073. ' rtl.addIntf(this, $mod.IBird, {',
  16074. ' DoIt$2: "DoIt$3",',
  16075. ' DoIt: "DoIt$2"',
  16076. ' });',
  16077. '});',
  16078. 'this.BirdIntf = null;',
  16079. '']),
  16080. LinesToStr([ // $mod.$main
  16081. '$mod.BirdIntf.DoIt(3);',
  16082. '$mod.BirdIntf.DoIt$1("abc");',
  16083. '$mod.BirdIntf.DoIt$2(true);',
  16084. '']));
  16085. end;
  16086. procedure TTestModule.TestClassInterface_DuplicateGUIInIntfListFail;
  16087. begin
  16088. StartProgram(false);
  16089. Add([
  16090. '{$interfaces corba}',
  16091. 'type',
  16092. ' IBird = interface',
  16093. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  16094. ' end;',
  16095. ' IDog = interface',
  16096. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  16097. ' end;',
  16098. ' TObject = class(IBird,IDog)',
  16099. ' end;',
  16100. 'begin']);
  16101. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  16102. nDuplicateGUIDXInYZ);
  16103. ConvertProgram;
  16104. end;
  16105. procedure TTestModule.TestClassInterface_DuplicateGUIInAncestorFail;
  16106. begin
  16107. StartProgram(false);
  16108. Add([
  16109. '{$interfaces corba}',
  16110. 'type',
  16111. ' IAnimal = interface',
  16112. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  16113. ' end;',
  16114. ' IBird = interface(IAnimal)',
  16115. ' end;',
  16116. ' IHawk = interface(IBird)',
  16117. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  16118. ' end;',
  16119. 'begin']);
  16120. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  16121. nDuplicateGUIDXInYZ);
  16122. ConvertProgram;
  16123. end;
  16124. procedure TTestModule.TestClassInterface_AncestorImpl;
  16125. begin
  16126. StartProgram(false);
  16127. Add([
  16128. '{$interfaces corba}',
  16129. 'type',
  16130. ' integer = longint;',
  16131. ' IUnknown = interface',
  16132. ' procedure DoIt(i: integer);',
  16133. ' end;',
  16134. ' IBird = interface',
  16135. ' procedure Fly(i: integer);',
  16136. ' end;',
  16137. ' TObject = class(IUnknown)',
  16138. ' procedure DoIt(i: integer);',
  16139. ' end;',
  16140. ' TBird = class(IBird)',
  16141. ' procedure Fly(i: integer);',
  16142. ' end;',
  16143. 'procedure TObject.DoIt(i: integer); begin end;',
  16144. 'procedure TBird.Fly(i: integer); begin end;',
  16145. 'begin',
  16146. '']);
  16147. ConvertProgram;
  16148. CheckSource('TestClassInterface_AncestorIntf',
  16149. LinesToStr([ // statements
  16150. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  16151. 'rtl.createInterface($mod, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  16152. 'rtl.createClass($mod, "TObject", null, function () {',
  16153. ' this.$init = function () {',
  16154. ' };',
  16155. ' this.$final = function () {',
  16156. ' };',
  16157. ' this.DoIt = function (i) {',
  16158. ' };',
  16159. ' rtl.addIntf(this, $mod.IUnknown);',
  16160. '});',
  16161. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16162. ' this.Fly = function (i) {',
  16163. ' };',
  16164. ' rtl.addIntf(this, $mod.IBird);',
  16165. ' rtl.addIntf(this, $mod.IUnknown);',
  16166. '});',
  16167. '']),
  16168. LinesToStr([ // $mod.$main
  16169. '']));
  16170. end;
  16171. procedure TTestModule.TestClassInterface_ImplReintroduce;
  16172. begin
  16173. StartProgram(false);
  16174. Add([
  16175. '{$interfaces corba}',
  16176. 'type',
  16177. ' integer = longint;',
  16178. ' IBird = interface',
  16179. ' procedure DoIt(i: integer);',
  16180. ' end;',
  16181. ' TObject = class',
  16182. ' procedure DoIt(i: integer);',
  16183. ' end;',
  16184. ' TBird = class(IBird)',
  16185. ' procedure DoIt(i: integer); virtual; reintroduce;',
  16186. ' end;',
  16187. 'procedure TObject.DoIt(i: integer); begin end;',
  16188. 'procedure TBird.DoIt(i: integer); begin end;',
  16189. 'begin',
  16190. '']);
  16191. ConvertProgram;
  16192. CheckSource('TestClassInterface_ImplReintroduce',
  16193. LinesToStr([ // statements
  16194. 'rtl.createInterface($mod, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  16195. 'rtl.createClass($mod, "TObject", null, function () {',
  16196. ' this.$init = function () {',
  16197. ' };',
  16198. ' this.$final = function () {',
  16199. ' };',
  16200. ' this.DoIt = function (i) {',
  16201. ' };',
  16202. '});',
  16203. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16204. ' this.DoIt$1 = function (i) {',
  16205. ' };',
  16206. ' rtl.addIntf(this, $mod.IBird, {',
  16207. ' DoIt: "DoIt$1"',
  16208. ' });',
  16209. '});',
  16210. '']),
  16211. LinesToStr([ // $mod.$main
  16212. '']));
  16213. end;
  16214. procedure TTestModule.TestClassInterface_MethodResolution;
  16215. begin
  16216. StartProgram(false);
  16217. Add([
  16218. '{$interfaces corba}',
  16219. 'type',
  16220. ' IUnknown = interface',
  16221. ' procedure Walk(i: longint);',
  16222. ' end;',
  16223. ' IBird = interface(IUnknown)',
  16224. ' procedure Walk(b: boolean); overload;',
  16225. ' procedure Fly(s: string);',
  16226. ' end;',
  16227. ' TObject = class',
  16228. ' end;',
  16229. ' TBird = class(TObject,IBird)',
  16230. ' procedure IBird.Fly = Move;',
  16231. ' procedure IBird.Walk = Hop;',
  16232. ' procedure Hop(i: longint);',
  16233. ' procedure Move(s: string);',
  16234. ' procedure Hop(b: boolean);',
  16235. ' end;',
  16236. 'procedure TBird.Move(s: string); begin end;',
  16237. 'procedure TBird.Hop(i: longint); begin end;',
  16238. 'procedure TBird.Hop(b: boolean); begin end;',
  16239. 'var',
  16240. ' BirdIntf: IBird;',
  16241. 'begin',
  16242. ' BirdIntf.Walk(3);',
  16243. ' BirdIntf.Walk(true);',
  16244. ' BirdIntf.Fly(''abc'');',
  16245. '']);
  16246. ConvertProgram;
  16247. CheckSource('TestClassInterface_MethodResolution',
  16248. LinesToStr([ // statements
  16249. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  16250. 'rtl.createInterface($mod, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], $mod.IUnknown);',
  16251. 'rtl.createClass($mod, "TObject", null, function () {',
  16252. ' this.$init = function () {',
  16253. ' };',
  16254. ' this.$final = function () {',
  16255. ' };',
  16256. '});',
  16257. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16258. ' this.Hop = function (i) {',
  16259. ' };',
  16260. ' this.Move = function (s) {',
  16261. ' };',
  16262. ' this.Hop$1 = function (b) {',
  16263. ' };',
  16264. ' rtl.addIntf(this, $mod.IBird, {',
  16265. ' Walk$1: "Hop$1",',
  16266. ' Fly: "Move",',
  16267. ' Walk: "Hop"',
  16268. ' });',
  16269. '});',
  16270. 'this.BirdIntf = null;',
  16271. '']),
  16272. LinesToStr([ // $mod.$main
  16273. '$mod.BirdIntf.Walk(3);',
  16274. '$mod.BirdIntf.Walk$1(true);',
  16275. '$mod.BirdIntf.Fly("abc");',
  16276. '']));
  16277. end;
  16278. procedure TTestModule.TestClassInterface_AncestorMoreInterfaces;
  16279. begin
  16280. StartProgram(false);
  16281. Add([
  16282. '{$interfaces com}',
  16283. 'type',
  16284. ' IUnknown = interface',
  16285. ' function _AddRef: longint;',
  16286. ' procedure Walk;',
  16287. ' end;',
  16288. ' IBird = interface end;',
  16289. ' IDog = interface end;',
  16290. ' TObject = class(IBird,IDog)',
  16291. ' function _AddRef: longint; virtual; abstract;',
  16292. ' procedure Walk; virtual; abstract;',
  16293. ' end;',
  16294. ' TBird = class(IUnknown)',
  16295. ' end;',
  16296. 'begin',
  16297. '']);
  16298. ConvertProgram;
  16299. CheckSource('TestClassInterface_COM_AncestorLess',
  16300. LinesToStr([ // statements
  16301. 'rtl.createInterface($mod, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  16302. 'rtl.createInterface($mod, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], $mod.IUnknown);',
  16303. 'rtl.createInterface($mod, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], $mod.IUnknown);',
  16304. 'rtl.createClass($mod, "TObject", null, function () {',
  16305. ' this.$init = function () {',
  16306. ' };',
  16307. ' this.$final = function () {',
  16308. ' };',
  16309. ' rtl.addIntf(this, $mod.IBird);',
  16310. ' rtl.addIntf(this, $mod.IDog);',
  16311. '});',
  16312. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16313. ' rtl.addIntf(this, $mod.IUnknown);',
  16314. ' rtl.addIntf(this, $mod.IBird);',
  16315. ' rtl.addIntf(this, $mod.IDog);',
  16316. '});',
  16317. '']),
  16318. LinesToStr([ // $mod.$main
  16319. '']));
  16320. end;
  16321. procedure TTestModule.TestClassInterface_MethodOverride;
  16322. begin
  16323. StartProgram(false);
  16324. Add([
  16325. '{$interfaces corba}',
  16326. 'type',
  16327. ' IUnknown = interface',
  16328. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  16329. ' procedure Go;',
  16330. ' end;',
  16331. ' TObject = class(IUnknown)',
  16332. ' procedure Go; virtual; abstract;',
  16333. ' end;',
  16334. ' TBird = class',
  16335. ' procedure Go; override;',
  16336. ' end;',
  16337. ' TCat = class(TObject)',
  16338. ' procedure Go; override;',
  16339. ' end;',
  16340. ' TDog = class(TObject, IUnknown)',
  16341. ' procedure Go; override;',
  16342. ' end;',
  16343. 'procedure TBird.Go; begin end;',
  16344. 'procedure TCat.Go; begin end;',
  16345. 'procedure TDog.Go; begin end;',
  16346. 'begin',
  16347. '']);
  16348. ConvertProgram;
  16349. CheckSource('TestClassInterface_MethodOverride',
  16350. LinesToStr([ // statements
  16351. 'rtl.createInterface($mod, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  16352. 'rtl.createClass($mod, "TObject", null, function () {',
  16353. ' this.$init = function () {',
  16354. ' };',
  16355. ' this.$final = function () {',
  16356. ' };',
  16357. ' rtl.addIntf(this, $mod.IUnknown);',
  16358. '});',
  16359. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16360. ' this.Go = function () {',
  16361. ' };',
  16362. ' rtl.addIntf(this, $mod.IUnknown);',
  16363. '});',
  16364. 'rtl.createClass($mod, "TCat", $mod.TObject, function () {',
  16365. ' this.Go = function () {',
  16366. ' };',
  16367. ' rtl.addIntf(this, $mod.IUnknown);',
  16368. '});',
  16369. 'rtl.createClass($mod, "TDog", $mod.TObject, function () {',
  16370. ' this.Go = function () {',
  16371. ' };',
  16372. ' rtl.addIntf(this, $mod.IUnknown);',
  16373. '});',
  16374. '']),
  16375. LinesToStr([ // $mod.$main
  16376. '']));
  16377. end;
  16378. procedure TTestModule.TestClassInterface_Corba_Delegation;
  16379. begin
  16380. StartProgram(false);
  16381. Add([
  16382. '{$interfaces corba}',
  16383. 'type',
  16384. ' IUnknown = interface',
  16385. ' end;',
  16386. ' IBird = interface(IUnknown)',
  16387. ' procedure Fly(s: string);',
  16388. ' end;',
  16389. ' IEagle = interface(IBird)',
  16390. ' end;',
  16391. ' IDove = interface(IBird)',
  16392. ' end;',
  16393. ' ISwallow = interface(IBird)',
  16394. ' end;',
  16395. ' TObject = class',
  16396. ' end;',
  16397. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  16398. ' procedure Fly(s: string); virtual; abstract;',
  16399. ' end;',
  16400. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  16401. ' FBirdIntf: IBird;',
  16402. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  16403. ' function GetEagleIntf: IEagle; virtual; abstract;',
  16404. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  16405. ' FDoveObj: TBird;',
  16406. ' property DoveObj: TBird read FDoveObj implements IDove;',
  16407. ' function GetSwallowObj: TBird; virtual; abstract;',
  16408. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  16409. ' end;',
  16410. 'begin',
  16411. '']);
  16412. ConvertProgram;
  16413. CheckSource('TestClassInterface_Delegation',
  16414. LinesToStr([ // statements
  16415. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  16416. 'rtl.createInterface($mod, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], $mod.IUnknown);',
  16417. 'rtl.createInterface($mod, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], $mod.IBird);',
  16418. 'rtl.createInterface($mod, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], $mod.IBird);',
  16419. 'rtl.createInterface($mod, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], $mod.IBird);',
  16420. 'rtl.createClass($mod, "TObject", null, function () {',
  16421. ' this.$init = function () {',
  16422. ' };',
  16423. ' this.$final = function () {',
  16424. ' };',
  16425. '});',
  16426. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16427. ' rtl.addIntf(this, $mod.IBird);',
  16428. ' rtl.addIntf(this, $mod.IEagle);',
  16429. ' rtl.addIntf(this, $mod.IDove);',
  16430. ' rtl.addIntf(this, $mod.ISwallow);',
  16431. '});',
  16432. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  16433. ' this.$init = function () {',
  16434. ' $mod.TObject.$init.call(this);',
  16435. ' this.FBirdIntf = null;',
  16436. ' this.FDoveObj = null;',
  16437. ' };',
  16438. ' this.$final = function () {',
  16439. ' this.FBirdIntf = undefined;',
  16440. ' this.FDoveObj = undefined;',
  16441. ' $mod.TObject.$final.call(this);',
  16442. ' };',
  16443. ' this.$intfmaps = {',
  16444. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  16445. ' return this.FBirdIntf;',
  16446. ' },',
  16447. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  16448. ' return this.GetEagleIntf();',
  16449. ' },',
  16450. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  16451. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  16452. ' },',
  16453. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  16454. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  16455. ' }',
  16456. ' };',
  16457. '});',
  16458. '']),
  16459. LinesToStr([ // $mod.$main
  16460. '']));
  16461. end;
  16462. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  16463. begin
  16464. StartProgram(false);
  16465. Add([
  16466. '{$interfaces corba}',
  16467. 'type',
  16468. ' IUnknown = interface',
  16469. ' end;',
  16470. ' IBird = interface(IUnknown)',
  16471. ' procedure Fly(s: string);',
  16472. ' end;',
  16473. ' IEagle = interface(IBird)',
  16474. ' end;',
  16475. ' IDove = interface(IBird)',
  16476. ' end;',
  16477. ' ISwallow = interface(IBird)',
  16478. ' end;',
  16479. ' TObject = class',
  16480. ' end;',
  16481. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  16482. ' procedure Fly(s: string); virtual; abstract;',
  16483. ' end;',
  16484. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  16485. ' private',
  16486. ' class var FBirdIntf: IBird;',
  16487. ' class var FDoveObj: TBird;',
  16488. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  16489. ' class function GetSwallowObj: TBird; virtual; abstract;',
  16490. ' protected',
  16491. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  16492. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  16493. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  16494. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  16495. ' end;',
  16496. 'begin',
  16497. '']);
  16498. ConvertProgram;
  16499. CheckSource('TestClassInterface_DelegationStatic',
  16500. LinesToStr([ // statements
  16501. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  16502. 'rtl.createInterface($mod, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], $mod.IUnknown);',
  16503. 'rtl.createInterface($mod, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], $mod.IBird);',
  16504. 'rtl.createInterface($mod, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], $mod.IBird);',
  16505. 'rtl.createInterface($mod, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], $mod.IBird);',
  16506. 'rtl.createClass($mod, "TObject", null, function () {',
  16507. ' this.$init = function () {',
  16508. ' };',
  16509. ' this.$final = function () {',
  16510. ' };',
  16511. '});',
  16512. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16513. ' rtl.addIntf(this, $mod.IBird);',
  16514. ' rtl.addIntf(this, $mod.IEagle);',
  16515. ' rtl.addIntf(this, $mod.IDove);',
  16516. ' rtl.addIntf(this, $mod.ISwallow);',
  16517. '});',
  16518. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  16519. ' this.FBirdIntf = null;',
  16520. ' this.FDoveObj = null;',
  16521. ' this.$intfmaps = {',
  16522. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  16523. ' return this.FBirdIntf;',
  16524. ' },',
  16525. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  16526. ' return this.$class.GetEagleIntf();',
  16527. ' },',
  16528. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  16529. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  16530. ' },',
  16531. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  16532. ' return rtl.getIntfT(this.$class.GetSwallowObj(), $mod.ISwallow);',
  16533. ' }',
  16534. ' };',
  16535. '});',
  16536. '']),
  16537. LinesToStr([ // $mod.$main
  16538. '']));
  16539. end;
  16540. procedure TTestModule.TestClassInterface_Corba_Operators;
  16541. begin
  16542. StartProgram(false);
  16543. Add([
  16544. '{$interfaces corba}',
  16545. 'type',
  16546. ' IUnknown = interface',
  16547. ' end;',
  16548. ' IBird = interface(IUnknown)',
  16549. ' function GetItems(Index: longint): longint;',
  16550. ' procedure SetItems(Index: longint; Value: longint);',
  16551. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  16552. ' end;',
  16553. ' TObject = class',
  16554. ' end;',
  16555. ' TBird = class(TObject,IBird)',
  16556. ' function GetItems(Index: longint): longint; virtual; abstract;',
  16557. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  16558. ' end;',
  16559. 'var',
  16560. ' IntfVar: IBird = nil;',
  16561. ' IntfVar2: IBird;',
  16562. ' ObjVar: TBird;',
  16563. ' v: JSValue;',
  16564. 'begin',
  16565. ' IntfVar:=nil;',
  16566. ' IntfVar[3]:=IntfVar[4];',
  16567. ' if Assigned(IntfVar) then ;',
  16568. ' IntfVar:=IntfVar2;',
  16569. ' IntfVar:=ObjVar;',
  16570. ' if IntfVar=IntfVar2 then ;',
  16571. ' if IntfVar<>IntfVar2 then ;',
  16572. ' if IntfVar is IBird then ;',
  16573. ' if IntfVar is TBird then ;',
  16574. ' if ObjVar is IBird then ;',
  16575. ' IntfVar:=IntfVar2 as IBird;',
  16576. ' ObjVar:=IntfVar2 as TBird;',
  16577. ' IntfVar:=ObjVar as IBird;',
  16578. ' IntfVar:=IBird(IntfVar2);',
  16579. ' ObjVar:=TBird(IntfVar);',
  16580. ' IntfVar:=IBird(ObjVar);',
  16581. ' v:=IntfVar;',
  16582. ' IntfVar:=IBird(v);',
  16583. ' if v is IBird then ;',
  16584. ' v:=JSValue(IntfVar);',
  16585. ' v:=IBird;',
  16586. '']);
  16587. ConvertProgram;
  16588. CheckSource('TestClassInterface_Corba_Operators',
  16589. LinesToStr([ // statements
  16590. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  16591. 'rtl.createInterface($mod, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], $mod.IUnknown);',
  16592. 'rtl.createClass($mod, "TObject", null, function () {',
  16593. ' this.$init = function () {',
  16594. ' };',
  16595. ' this.$final = function () {',
  16596. ' };',
  16597. '});',
  16598. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16599. ' rtl.addIntf(this, $mod.IBird);',
  16600. '});',
  16601. 'this.IntfVar = null;',
  16602. 'this.IntfVar2 = null;',
  16603. 'this.ObjVar = null;',
  16604. 'this.v = undefined;',
  16605. '']),
  16606. LinesToStr([ // $mod.$main
  16607. '$mod.IntfVar = null;',
  16608. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  16609. 'if ($mod.IntfVar != null) ;',
  16610. '$mod.IntfVar = $mod.IntfVar2;',
  16611. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  16612. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  16613. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  16614. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  16615. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  16616. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  16617. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  16618. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  16619. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  16620. '$mod.IntfVar = $mod.IntfVar2;',
  16621. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  16622. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  16623. '$mod.v = $mod.IntfVar;',
  16624. '$mod.IntfVar = rtl.getObject($mod.v);',
  16625. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  16626. '$mod.v = rtl.getObject($mod.IntfVar);',
  16627. '$mod.v = $mod.IBird;',
  16628. '']));
  16629. end;
  16630. procedure TTestModule.TestClassInterface_Corba_Args;
  16631. begin
  16632. StartProgram(false);
  16633. Add([
  16634. '{$interfaces corba}',
  16635. 'type',
  16636. ' IUnknown = interface',
  16637. ' end;',
  16638. ' IBird = interface(IUnknown)',
  16639. ' end;',
  16640. ' TObject = class',
  16641. ' end;',
  16642. ' TBird = class(TObject,IBird)',
  16643. ' end;',
  16644. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  16645. 'begin',
  16646. ' DoIt(i,i,i);',
  16647. 'end;',
  16648. 'procedure Change(var i: IBird; out j: IBird);',
  16649. 'begin',
  16650. ' DoIt(i,i,i);',
  16651. ' Change(i,i);',
  16652. 'end;',
  16653. 'var',
  16654. ' i: IBird;',
  16655. ' o: TBird;',
  16656. 'begin',
  16657. ' DoIt(i,i,i);',
  16658. ' Change(i,i);',
  16659. ' DoIt(o,o,o);',
  16660. '']);
  16661. ConvertProgram;
  16662. CheckSource('TestClassInterface_Corba_Args',
  16663. LinesToStr([ // statements
  16664. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  16665. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], $mod.IUnknown);',
  16666. 'rtl.createClass($mod, "TObject", null, function () {',
  16667. ' this.$init = function () {',
  16668. ' };',
  16669. ' this.$final = function () {',
  16670. ' };',
  16671. '});',
  16672. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16673. ' rtl.addIntf(this, $mod.IBird);',
  16674. '});',
  16675. 'this.DoIt = function (u, i, j) {',
  16676. ' $mod.DoIt({',
  16677. ' get: function () {',
  16678. ' return i;',
  16679. ' },',
  16680. ' set: function (v) {',
  16681. ' i = v;',
  16682. ' }',
  16683. ' }, i, i);',
  16684. '};',
  16685. 'this.Change = function (i, j) {',
  16686. ' $mod.DoIt(i, i.get(), i.get());',
  16687. ' $mod.Change(i, i);',
  16688. '};',
  16689. 'this.i = null;',
  16690. 'this.o = null;',
  16691. '']),
  16692. LinesToStr([ // $mod.$main
  16693. '$mod.DoIt({',
  16694. ' p: $mod,',
  16695. ' get: function () {',
  16696. ' return this.p.i;',
  16697. ' },',
  16698. ' set: function (v) {',
  16699. ' this.p.i = v;',
  16700. ' }',
  16701. '}, $mod.i, $mod.i);',
  16702. '$mod.Change({',
  16703. ' p: $mod,',
  16704. ' get: function () {',
  16705. ' return this.p.i;',
  16706. ' },',
  16707. ' set: function (v) {',
  16708. ' this.p.i = v;',
  16709. ' }',
  16710. '}, {',
  16711. ' p: $mod,',
  16712. ' get: function () {',
  16713. ' return this.p.i;',
  16714. ' },',
  16715. ' set: function (v) {',
  16716. ' this.p.i = v;',
  16717. ' }',
  16718. '});',
  16719. '$mod.DoIt({',
  16720. ' p: $mod,',
  16721. ' get: function () {',
  16722. ' return this.p.o;',
  16723. ' },',
  16724. ' set: function (v) {',
  16725. ' this.p.o = v;',
  16726. ' }',
  16727. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  16728. '']));
  16729. end;
  16730. procedure TTestModule.TestClassInterface_Corba_ForIn;
  16731. begin
  16732. StartProgram(false);
  16733. Add([
  16734. '{$interfaces corba}',
  16735. 'type',
  16736. ' IUnknown = interface end;',
  16737. ' TObject = class',
  16738. ' Id: longint;',
  16739. ' end;',
  16740. ' IEnumerator = interface(IUnknown)',
  16741. ' function GetCurrent: TObject;',
  16742. ' function MoveNext: Boolean;',
  16743. ' property Current: TObject read GetCurrent;',
  16744. ' end;',
  16745. ' IEnumerable = interface(IUnknown)',
  16746. ' function GetEnumerator: IEnumerator;',
  16747. ' end;',
  16748. 'var',
  16749. ' o: TObject;',
  16750. ' i: IEnumerable;',
  16751. 'begin',
  16752. ' for o in i do o.Id:=3;',
  16753. '']);
  16754. ConvertProgram;
  16755. CheckSource('TestClassInterface_Corba_ForIn',
  16756. LinesToStr([ // statements
  16757. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  16758. 'rtl.createClass($mod, "TObject", null, function () {',
  16759. ' this.$init = function () {',
  16760. ' this.Id = 0;',
  16761. ' };',
  16762. ' this.$final = function () {',
  16763. ' };',
  16764. '});',
  16765. 'rtl.createInterface($mod, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], $mod.IUnknown);',
  16766. 'rtl.createInterface($mod, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], $mod.IUnknown);',
  16767. 'this.o = null;',
  16768. 'this.i = null;',
  16769. '']),
  16770. LinesToStr([ // $mod.$main
  16771. 'var $in1 = $mod.i.GetEnumerator();',
  16772. 'while ($in1.MoveNext()) {',
  16773. ' $mod.o = $in1.GetCurrent();',
  16774. ' $mod.o.Id = 3;',
  16775. '};',
  16776. '']));
  16777. end;
  16778. procedure TTestModule.TestClassInterface_COM_AssignVar;
  16779. begin
  16780. StartProgram(false);
  16781. Add([
  16782. '{$interfaces com}',
  16783. 'type',
  16784. ' IUnknown = interface',
  16785. ' function _AddRef: longint;',
  16786. ' function _Release: longint;',
  16787. ' end;',
  16788. ' TObject = class(IUnknown)',
  16789. ' function _AddRef: longint; virtual; abstract;',
  16790. ' function _Release: longint; virtual; abstract;',
  16791. ' end;',
  16792. 'var',
  16793. ' i: IUnknown;',
  16794. 'procedure DoGlobal(o: TObject);',
  16795. 'begin',
  16796. ' i:=nil;',
  16797. ' i:=o;',
  16798. ' i:=i;',
  16799. 'end;',
  16800. 'procedure DoLocal(o: TObject);',
  16801. 'const k: IUnknown = nil;',
  16802. 'var j: IUnknown;',
  16803. 'begin',
  16804. ' k:=o;',
  16805. ' k:=i;',
  16806. ' j:=o;',
  16807. ' j:=i;',
  16808. 'end;',
  16809. 'var o: TObject;',
  16810. 'begin',
  16811. ' i:=nil;',
  16812. ' i:=o;',
  16813. '']);
  16814. ConvertProgram;
  16815. CheckSource('TestClassInterface_COM_AssignVar',
  16816. LinesToStr([ // statements
  16817. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  16818. 'rtl.createClass($mod, "TObject", null, function () {',
  16819. ' this.$init = function () {',
  16820. ' };',
  16821. ' this.$final = function () {',
  16822. ' };',
  16823. ' rtl.addIntf(this, $mod.IUnknown);',
  16824. '});',
  16825. 'this.i = null;',
  16826. 'this.DoGlobal = function (o) {',
  16827. ' rtl.setIntfP($mod, "i", null);',
  16828. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  16829. ' rtl.setIntfP($mod, "i", $mod.i);',
  16830. '};',
  16831. 'var k = null;',
  16832. 'this.DoLocal = function (o) {',
  16833. ' var j = null;',
  16834. ' try{',
  16835. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  16836. ' k = rtl.setIntfL(k, $mod.i);',
  16837. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  16838. ' j = rtl.setIntfL(j, $mod.i);',
  16839. ' }finally{',
  16840. ' rtl._Release(j);',
  16841. ' };',
  16842. '};',
  16843. 'this.o = null;',
  16844. '']),
  16845. LinesToStr([ // $mod.$main
  16846. 'rtl.setIntfP($mod, "i", null);',
  16847. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  16848. '']));
  16849. end;
  16850. procedure TTestModule.TestClassInterface_COM_AssignArg;
  16851. begin
  16852. StartProgram(false);
  16853. Add([
  16854. '{$interfaces com}',
  16855. 'type',
  16856. ' IUnknown = interface',
  16857. ' function _AddRef: longint;',
  16858. ' function _Release: longint;',
  16859. ' end;',
  16860. ' TObject = class(IUnknown)',
  16861. ' function _AddRef: longint; virtual; abstract;',
  16862. ' function _Release: longint; virtual; abstract;',
  16863. ' end;',
  16864. 'procedure DoDefault(i, j: IUnknown);',
  16865. 'begin',
  16866. ' i:=nil;',
  16867. ' i:=j;',
  16868. 'end;',
  16869. 'begin',
  16870. '']);
  16871. ConvertProgram;
  16872. CheckSource('TestClassInterface_COM_AssignArg',
  16873. LinesToStr([ // statements
  16874. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  16875. 'rtl.createClass($mod, "TObject", null, function () {',
  16876. ' this.$init = function () {',
  16877. ' };',
  16878. ' this.$final = function () {',
  16879. ' };',
  16880. ' rtl.addIntf(this, $mod.IUnknown);',
  16881. '});',
  16882. 'this.DoDefault = function (i, j) {',
  16883. ' rtl._AddRef(i);',
  16884. ' try {',
  16885. ' i = rtl.setIntfL(i, null);',
  16886. ' i = rtl.setIntfL(i, j);',
  16887. ' } finally {',
  16888. ' rtl._Release(i);',
  16889. ' };',
  16890. '};',
  16891. '']),
  16892. LinesToStr([ // $mod.$main
  16893. '']));
  16894. end;
  16895. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  16896. begin
  16897. StartProgram(false);
  16898. Add([
  16899. '{$interfaces com}',
  16900. 'type',
  16901. ' IUnknown = interface',
  16902. ' function _AddRef: longint;',
  16903. ' function _Release: longint;',
  16904. ' end;',
  16905. ' TObject = class(IUnknown)',
  16906. ' function _AddRef: longint; virtual; abstract;',
  16907. ' function _Release: longint; virtual; abstract;',
  16908. ' end;',
  16909. 'function DoDefault(i: IUnknown): IUnknown;',
  16910. 'begin',
  16911. ' Result:=i;',
  16912. ' if Result<>nil then exit;',
  16913. 'end;',
  16914. 'begin',
  16915. '']);
  16916. ConvertProgram;
  16917. CheckSource('TestClassInterface_COM_FunctionResult',
  16918. LinesToStr([ // statements
  16919. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  16920. 'rtl.createClass($mod, "TObject", null, function () {',
  16921. ' this.$init = function () {',
  16922. ' };',
  16923. ' this.$final = function () {',
  16924. ' };',
  16925. ' rtl.addIntf(this, $mod.IUnknown);',
  16926. '});',
  16927. 'this.DoDefault = function (i) {',
  16928. ' var Result = null;',
  16929. ' var $ok = false;',
  16930. ' try {',
  16931. ' Result = rtl.setIntfL(Result, i);',
  16932. ' if(Result !== null){',
  16933. ' $ok = true;',
  16934. ' return Result;',
  16935. ' };',
  16936. ' $ok = true;',
  16937. ' } finally {',
  16938. ' if(!$ok) rtl._Release(Result);',
  16939. ' };',
  16940. ' return Result;',
  16941. '};',
  16942. '']),
  16943. LinesToStr([ // $mod.$main
  16944. '']));
  16945. end;
  16946. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  16947. begin
  16948. StartProgram(false);
  16949. Add([
  16950. '{$interfaces com}',
  16951. 'type',
  16952. ' IUnknown = interface',
  16953. ' function _AddRef: longint;',
  16954. ' function _Release: longint;',
  16955. ' end;',
  16956. ' TObject = class(IUnknown)',
  16957. ' function _AddRef: longint; virtual; abstract;',
  16958. ' function _Release: longint; virtual; abstract;',
  16959. ' function GetIntf: IUnknown; virtual;',
  16960. ' end;',
  16961. ' TMouse = class',
  16962. ' function GetIntf: IUnknown; override;',
  16963. ' end;',
  16964. 'function TObject.GetIntf: IUnknown; begin end;',
  16965. 'function TMouse.GetIntf: IUnknown;',
  16966. 'var i: IUnknown;',
  16967. 'begin',
  16968. ' inherited;',
  16969. ' inherited GetIntf;',
  16970. ' inherited GetIntf();',
  16971. ' Result:=inherited GetIntf;',
  16972. ' Result:=inherited GetIntf();',
  16973. ' i:=inherited GetIntf;',
  16974. ' i:=inherited GetIntf();',
  16975. 'end;',
  16976. 'begin',
  16977. '']);
  16978. ConvertProgram;
  16979. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  16980. LinesToStr([ // statements
  16981. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  16982. 'rtl.createClass($mod, "TObject", null, function () {',
  16983. ' this.$init = function () {',
  16984. ' };',
  16985. ' this.$final = function () {',
  16986. ' };',
  16987. ' this.GetIntf = function () {',
  16988. ' var Result = null;',
  16989. ' return Result;',
  16990. ' };',
  16991. ' rtl.addIntf(this, $mod.IUnknown);',
  16992. '});',
  16993. 'rtl.createClass($mod, "TMouse", $mod.TObject, function () {',
  16994. ' this.GetIntf = function () {',
  16995. ' var Result = null;',
  16996. ' var i = null;',
  16997. ' var $ir = rtl.createIntfRefs();',
  16998. ' var $ok = false;',
  16999. ' try {',
  17000. ' $ir.ref(1, $mod.TObject.GetIntf.apply(this, arguments));',
  17001. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  17002. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  17003. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  17004. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  17005. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  17006. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  17007. ' $ok = true;',
  17008. ' } finally {',
  17009. ' $ir.free();',
  17010. ' rtl._Release(i);',
  17011. ' if (!$ok) rtl._Release(Result);',
  17012. ' };',
  17013. ' return Result;',
  17014. ' };',
  17015. ' rtl.addIntf(this, $mod.IUnknown);',
  17016. '});',
  17017. '']),
  17018. LinesToStr([ // $mod.$main
  17019. '']));
  17020. end;
  17021. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  17022. begin
  17023. StartProgram(false);
  17024. Add([
  17025. '{$interfaces com}',
  17026. 'type',
  17027. ' IUnknown = interface',
  17028. ' function _AddRef: longint;',
  17029. ' function _Release: longint;',
  17030. ' end;',
  17031. ' TObject = class(IUnknown)',
  17032. ' function _AddRef: longint; virtual; abstract;',
  17033. ' function _Release: longint; virtual; abstract;',
  17034. ' end;',
  17035. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  17036. 'begin',
  17037. ' if i is IUnknown then ;',
  17038. ' if o is IUnknown then ;',
  17039. ' if i is TObject then ;',
  17040. ' i:=j as IUnknown;',
  17041. ' i:=o as IUnknown;',
  17042. ' o:=j as TObject;',
  17043. ' i:=IUnknown(j);',
  17044. ' i:=IUnknown(o);',
  17045. ' o:=TObject(i);',
  17046. 'end;',
  17047. 'begin',
  17048. '']);
  17049. ConvertProgram;
  17050. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  17051. LinesToStr([ // statements
  17052. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17053. 'rtl.createClass($mod, "TObject", null, function () {',
  17054. ' this.$init = function () {',
  17055. ' };',
  17056. ' this.$final = function () {',
  17057. ' };',
  17058. ' rtl.addIntf(this, $mod.IUnknown);',
  17059. '});',
  17060. 'this.DoDefault = function (i, j, o) {',
  17061. ' rtl._AddRef(i);',
  17062. ' try {',
  17063. ' if ($mod.IUnknown.isPrototypeOf(i)) ;',
  17064. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  17065. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  17066. ' i = rtl.setIntfL(i, rtl.as(j, $mod.IUnknown));',
  17067. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  17068. ' o = rtl.intfAsClass(j, $mod.TObject);',
  17069. ' i = rtl.setIntfL(i, j);',
  17070. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  17071. ' o = rtl.intfToClass(i, $mod.TObject);',
  17072. ' } finally {',
  17073. ' rtl._Release(i);',
  17074. ' };',
  17075. '};',
  17076. '']),
  17077. LinesToStr([ // $mod.$main
  17078. '']));
  17079. end;
  17080. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  17081. begin
  17082. StartProgram(false);
  17083. Add([
  17084. '{$interfaces com}',
  17085. 'type',
  17086. ' IUnknown = interface',
  17087. ' function _AddRef: longint;',
  17088. ' function _Release: longint;',
  17089. ' end;',
  17090. ' TObject = class(IUnknown)',
  17091. ' function _AddRef: longint; virtual; abstract;',
  17092. ' function _Release: longint; virtual; abstract;',
  17093. ' end;',
  17094. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  17095. 'var o: TObject;',
  17096. 'begin',
  17097. ' DoIt(v,v,v,v);',
  17098. ' DoIt(o,o,k,k);',
  17099. 'end;',
  17100. 'procedure DoSome;',
  17101. 'var v: IUnknown;',
  17102. 'begin',
  17103. ' DoIt(v,v,v,v);',
  17104. 'end;',
  17105. 'var i: IUnknown;',
  17106. 'begin',
  17107. ' DoIt(i,i,i,i);',
  17108. '']);
  17109. ConvertProgram;
  17110. CheckSource('TestClassInterface_COM_PassAsArg',
  17111. LinesToStr([ // statements
  17112. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17113. 'rtl.createClass($mod, "TObject", null, function () {',
  17114. ' this.$init = function () {',
  17115. ' };',
  17116. ' this.$final = function () {',
  17117. ' };',
  17118. ' rtl.addIntf(this, $mod.IUnknown);',
  17119. '});',
  17120. 'this.DoIt = function (v, j, k, l) {',
  17121. ' var o = null;',
  17122. ' var $ir = rtl.createIntfRefs();',
  17123. ' rtl._AddRef(v);',
  17124. ' try {',
  17125. ' $mod.DoIt(v, v, {',
  17126. ' get: function () {',
  17127. ' return v;',
  17128. ' },',
  17129. ' set: function (w) {',
  17130. ' v = rtl.setIntfL(v, w);',
  17131. ' }',
  17132. ' }, {',
  17133. ' get: function () {',
  17134. ' return v;',
  17135. ' },',
  17136. ' set: function (w) {',
  17137. ' v = rtl.setIntfL(v, w);',
  17138. ' }',
  17139. ' });',
  17140. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  17141. ' } finally {',
  17142. ' $ir.free();',
  17143. ' rtl._Release(v);',
  17144. ' };',
  17145. '};',
  17146. 'this.DoSome = function () {',
  17147. ' var v = null;',
  17148. ' try {',
  17149. ' $mod.DoIt(v, v, {',
  17150. ' get: function () {',
  17151. ' return v;',
  17152. ' },',
  17153. ' set: function (w) {',
  17154. ' v = rtl.setIntfL(v, w);',
  17155. ' }',
  17156. ' }, {',
  17157. ' get: function () {',
  17158. ' return v;',
  17159. ' },',
  17160. ' set: function (w) {',
  17161. ' v = rtl.setIntfL(v, w);',
  17162. ' }',
  17163. ' });',
  17164. ' } finally {',
  17165. ' rtl._Release(v);',
  17166. ' };',
  17167. '};',
  17168. 'this.i = null;',
  17169. '']),
  17170. LinesToStr([ // $mod.$main
  17171. '$mod.DoIt($mod.i, $mod.i, {',
  17172. ' p: $mod,',
  17173. ' get: function () {',
  17174. ' return this.p.i;',
  17175. ' },',
  17176. ' set: function (v) {',
  17177. ' rtl.setIntfP(this.p, "i", v);',
  17178. ' }',
  17179. '}, {',
  17180. ' p: $mod,',
  17181. ' get: function () {',
  17182. ' return this.p.i;',
  17183. ' },',
  17184. ' set: function (v) {',
  17185. ' rtl.setIntfP(this.p, "i", v);',
  17186. ' }',
  17187. '});',
  17188. '']));
  17189. end;
  17190. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  17191. begin
  17192. StartProgram(false);
  17193. Add([
  17194. '{$interfaces com}',
  17195. 'type',
  17196. ' IUnknown = interface',
  17197. ' function _AddRef: longint;',
  17198. ' function _Release: longint;',
  17199. ' end;',
  17200. ' TObject = class(IUnknown)',
  17201. ' function _AddRef: longint; virtual; abstract;',
  17202. ' function _Release: longint; virtual; abstract;',
  17203. ' end;',
  17204. 'procedure DoIt(out i);',
  17205. 'begin end;',
  17206. 'procedure DoSome;',
  17207. 'var v: IUnknown;',
  17208. 'begin',
  17209. ' DoIt(v);',
  17210. 'end;',
  17211. 'function GetIt: IUnknown;',
  17212. 'begin',
  17213. ' DoIt(Result);',
  17214. 'end;',
  17215. 'var i: IUnknown;',
  17216. 'begin',
  17217. ' DoIt(i);',
  17218. '']);
  17219. ConvertProgram;
  17220. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  17221. LinesToStr([ // statements
  17222. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17223. 'rtl.createClass($mod, "TObject", null, function () {',
  17224. ' this.$init = function () {',
  17225. ' };',
  17226. ' this.$final = function () {',
  17227. ' };',
  17228. ' rtl.addIntf(this, $mod.IUnknown);',
  17229. '});',
  17230. 'this.DoIt = function (i) {',
  17231. '};',
  17232. 'this.DoSome = function () {',
  17233. ' var v = null;',
  17234. ' try {',
  17235. ' $mod.DoIt({',
  17236. ' get: function () {',
  17237. ' return v;',
  17238. ' },',
  17239. ' set: function (w) {',
  17240. ' v = w;',
  17241. ' }',
  17242. ' });',
  17243. ' } finally {',
  17244. ' rtl._Release(v);',
  17245. ' };',
  17246. '};',
  17247. 'this.GetIt = function () {',
  17248. ' var Result = null;',
  17249. ' var $ok = false;',
  17250. ' try {',
  17251. ' $mod.DoIt({',
  17252. ' get: function () {',
  17253. ' return Result;',
  17254. ' },',
  17255. ' set: function (v) {',
  17256. ' Result = v;',
  17257. ' }',
  17258. ' });',
  17259. ' $ok = true;',
  17260. ' } finally {',
  17261. ' if (!$ok) rtl._Release(Result);',
  17262. ' };',
  17263. ' return Result;',
  17264. '};',
  17265. 'this.i = null;',
  17266. '']),
  17267. LinesToStr([ // $mod.$main
  17268. 'try {',
  17269. ' $mod.DoIt({',
  17270. ' p: $mod,',
  17271. ' get: function () {',
  17272. ' return this.p.i;',
  17273. ' },',
  17274. ' set: function (v) {',
  17275. ' this.p.i = v;',
  17276. ' }',
  17277. ' });',
  17278. '} finally {',
  17279. ' rtl._Release($mod.i);',
  17280. '};',
  17281. '']));
  17282. end;
  17283. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  17284. begin
  17285. StartProgram(false);
  17286. Add([
  17287. '{$interfaces com}',
  17288. 'type',
  17289. ' IUnknown = interface',
  17290. ' function _AddRef: longint;',
  17291. ' function _Release: longint;',
  17292. ' end;',
  17293. ' TObject = class(IUnknown)',
  17294. ' function _AddRef: longint; virtual; abstract;',
  17295. ' function _Release: longint; virtual; abstract;',
  17296. ' end;',
  17297. 'function GetIt: IUnknown;',
  17298. 'begin',
  17299. 'end;',
  17300. 'procedure DoSome;',
  17301. 'var v: IUnknown;',
  17302. ' i: longint;',
  17303. 'begin',
  17304. ' v:=GetIt;',
  17305. ' v:=GetIt();',
  17306. ' GetIt()._AddRef;',
  17307. ' i:=GetIt()._AddRef;',
  17308. 'end;',
  17309. 'var v: IUnknown;',
  17310. ' i: longint;',
  17311. 'begin',
  17312. ' v:=GetIt;',
  17313. ' v:=GetIt();',
  17314. ' GetIt()._AddRef;',
  17315. ' i:=GetIt()._AddRef;',
  17316. '']);
  17317. ConvertProgram;
  17318. CheckSource('TestClassInterface_COM_FunctionInExpr',
  17319. LinesToStr([ // statements
  17320. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17321. 'rtl.createClass($mod, "TObject", null, function () {',
  17322. ' this.$init = function () {',
  17323. ' };',
  17324. ' this.$final = function () {',
  17325. ' };',
  17326. ' rtl.addIntf(this, $mod.IUnknown);',
  17327. '});',
  17328. 'this.GetIt = function () {',
  17329. ' var Result = null;',
  17330. ' return Result;',
  17331. '};',
  17332. 'this.DoSome = function () {',
  17333. ' var v = null;',
  17334. ' var i = 0;',
  17335. ' var $ir = rtl.createIntfRefs();',
  17336. ' try {',
  17337. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  17338. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  17339. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  17340. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  17341. ' } finally {',
  17342. ' $ir.free();',
  17343. ' rtl._Release(v);',
  17344. ' };',
  17345. '};',
  17346. 'this.v = null;',
  17347. 'this.i = 0;',
  17348. '']),
  17349. LinesToStr([ // $mod.$main
  17350. 'var $ir = rtl.createIntfRefs();',
  17351. 'try {',
  17352. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  17353. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  17354. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  17355. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  17356. '} finally {',
  17357. ' $ir.free();',
  17358. '};',
  17359. '']));
  17360. end;
  17361. procedure TTestModule.TestClassInterface_COM_Property;
  17362. begin
  17363. StartProgram(false);
  17364. Add([
  17365. '{$interfaces com}',
  17366. 'type',
  17367. ' IUnknown = interface',
  17368. ' function _AddRef: longint;',
  17369. ' function _Release: longint;',
  17370. ' end;',
  17371. ' TObject = class(IUnknown)',
  17372. ' FAnt: IUnknown;',
  17373. ' function _AddRef: longint; virtual; abstract;',
  17374. ' function _Release: longint; virtual; abstract;',
  17375. ' function GetBird: IUnknown; virtual; abstract;',
  17376. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  17377. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  17378. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  17379. ' property Ant: IUnknown read FAnt write FAnt;',
  17380. ' property Bird: IUnknown read GetBird write SetBird;',
  17381. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  17382. ' end;',
  17383. 'procedure DoIt;',
  17384. 'var',
  17385. ' o: TObject;',
  17386. ' v: IUnknown;',
  17387. 'begin',
  17388. ' v:=o.Ant;',
  17389. ' o.Ant:=v;',
  17390. ' o.Ant:=o.Ant;',
  17391. ' v:=o.Bird;',
  17392. ' o.Bird:=v;',
  17393. ' o.Bird:=o.Bird;',
  17394. ' v:=o.Items[1];',
  17395. ' o.Items[2]:=v;',
  17396. ' o.Items[3]:=o.Items[4];',
  17397. ' v:=o[5];',
  17398. ' o[6]:=v;',
  17399. ' o[7]:=o[8];',
  17400. 'end;',
  17401. 'begin',
  17402. '']);
  17403. ConvertProgram;
  17404. CheckSource('TestClassInterface_COM_Property',
  17405. LinesToStr([ // statements
  17406. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17407. 'rtl.createClass($mod, "TObject", null, function () {',
  17408. ' this.$init = function () {',
  17409. ' this.FAnt = null;',
  17410. ' };',
  17411. ' this.$final = function () {',
  17412. ' this.FAnt = undefined;',
  17413. ' };',
  17414. ' rtl.addIntf(this, $mod.IUnknown);',
  17415. '});',
  17416. 'this.DoIt = function () {',
  17417. ' var o = null;',
  17418. ' var v = null;',
  17419. ' var $ir = rtl.createIntfRefs();',
  17420. ' try {',
  17421. ' v = rtl.setIntfL(v, o.FAnt);',
  17422. ' rtl.setIntfP(o, "FAnt", v);',
  17423. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  17424. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  17425. ' o.SetBird(v);',
  17426. ' o.SetBird($ir.ref(1, o.GetBird()));',
  17427. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  17428. ' o.SetItems(2, v);',
  17429. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  17430. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  17431. ' o.SetItems(6, v);',
  17432. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  17433. ' } finally {',
  17434. ' $ir.free();',
  17435. ' rtl._Release(v);',
  17436. ' };',
  17437. '};',
  17438. '']),
  17439. LinesToStr([ // $mod.$main
  17440. '']));
  17441. end;
  17442. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  17443. begin
  17444. StartProgram(false);
  17445. Add([
  17446. '{$interfaces com}',
  17447. 'type',
  17448. ' IUnknown = interface',
  17449. ' function _AddRef: longint;',
  17450. ' function _Release: longint;',
  17451. ' function GetBird: IUnknown;',
  17452. ' procedure SetBird(Value: IUnknown);',
  17453. ' function GetItems(Index: longint): IUnknown;',
  17454. ' procedure SetItems(Index: longint; Value: IUnknown);',
  17455. ' property Bird: IUnknown read GetBird write SetBird;',
  17456. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  17457. ' end;',
  17458. ' TObject = class(IUnknown)',
  17459. ' function _AddRef: longint; virtual; abstract;',
  17460. ' function _Release: longint; virtual; abstract;',
  17461. ' function GetBird: IUnknown; virtual; abstract;',
  17462. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  17463. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  17464. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  17465. ' end;',
  17466. 'procedure DoIt;',
  17467. 'var',
  17468. ' o: TObject;',
  17469. ' v: IUnknown;',
  17470. 'begin',
  17471. ' v:=v.Items[1];',
  17472. ' v.Items[2]:=v;',
  17473. ' v.Items[3]:=v.Items[4];',
  17474. ' v:=v[5];',
  17475. ' v[6]:=v;',
  17476. ' v[7]:=v[8];',
  17477. ' v[9].Bird.Bird:=v;',
  17478. ' v:=v.Bird[10].Bird',
  17479. 'end;',
  17480. 'begin',
  17481. '']);
  17482. ConvertProgram;
  17483. CheckSource('TestClassInterface_COM_IntfProperty',
  17484. LinesToStr([ // statements
  17485. 'rtl.createInterface($mod, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  17486. ' "_AddRef",',
  17487. ' "_Release",',
  17488. ' "GetBird",',
  17489. ' "SetBird",',
  17490. ' "GetItems",',
  17491. ' "SetItems"',
  17492. '], null);',
  17493. 'rtl.createClass($mod, "TObject", null, function () {',
  17494. ' this.$init = function () {',
  17495. ' };',
  17496. ' this.$final = function () {',
  17497. ' };',
  17498. ' rtl.addIntf(this, $mod.IUnknown);',
  17499. '});',
  17500. 'this.DoIt = function () {',
  17501. ' var o = null;',
  17502. ' var v = null;',
  17503. ' var $ir = rtl.createIntfRefs();',
  17504. ' try {',
  17505. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  17506. ' v.SetItems(2, v);',
  17507. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  17508. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  17509. ' v.SetItems(6, v);',
  17510. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  17511. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  17512. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  17513. ' } finally {',
  17514. ' $ir.free();',
  17515. ' rtl._Release(v);',
  17516. ' };',
  17517. '};',
  17518. '']),
  17519. LinesToStr([ // $mod.$main
  17520. '']));
  17521. end;
  17522. procedure TTestModule.TestClassInterface_COM_Delegation;
  17523. begin
  17524. StartProgram(false);
  17525. Add([
  17526. '{$interfaces com}',
  17527. 'type',
  17528. ' IUnknown = interface',
  17529. ' function _AddRef: longint;',
  17530. ' function _Release: longint;',
  17531. ' end;',
  17532. ' IBird = interface(IUnknown)',
  17533. ' procedure Fly(s: string);',
  17534. ' end;',
  17535. ' IEagle = interface(IBird) end;',
  17536. ' IDove = interface(IBird) end;',
  17537. ' ISwallow = interface(IBird) end;',
  17538. ' TObject = class',
  17539. ' end;',
  17540. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  17541. ' function _AddRef: longint; virtual; abstract;',
  17542. ' function _Release: longint; virtual; abstract;',
  17543. ' procedure Fly(s: string); virtual; abstract;',
  17544. ' end;',
  17545. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  17546. ' function _AddRef: longint; virtual; abstract;',
  17547. ' function _Release: longint; virtual; abstract;',
  17548. ' FBirdIntf: IBird;',
  17549. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  17550. ' function GetEagleIntf: IEagle; virtual; abstract;',
  17551. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  17552. ' FDoveObj: TBird;',
  17553. ' property DoveObj: TBird read FDoveObj implements IDove;',
  17554. ' function GetSwallowObj: TBird; virtual; abstract;',
  17555. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  17556. ' end;',
  17557. 'begin',
  17558. '']);
  17559. ConvertProgram;
  17560. CheckSource('TestClassInterface_COM_Delegation',
  17561. LinesToStr([ // statements
  17562. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17563. 'rtl.createInterface($mod, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], $mod.IUnknown);',
  17564. 'rtl.createInterface($mod, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], $mod.IBird);',
  17565. 'rtl.createInterface($mod, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], $mod.IBird);',
  17566. 'rtl.createInterface($mod, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], $mod.IBird);',
  17567. 'rtl.createClass($mod, "TObject", null, function () {',
  17568. ' this.$init = function () {',
  17569. ' };',
  17570. ' this.$final = function () {',
  17571. ' };',
  17572. '});',
  17573. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17574. ' rtl.addIntf(this, $mod.IBird);',
  17575. ' rtl.addIntf(this, $mod.IEagle);',
  17576. ' rtl.addIntf(this, $mod.IDove);',
  17577. ' rtl.addIntf(this, $mod.ISwallow);',
  17578. '});',
  17579. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  17580. ' this.$init = function () {',
  17581. ' $mod.TObject.$init.call(this);',
  17582. ' this.FBirdIntf = null;',
  17583. ' this.FDoveObj = null;',
  17584. ' };',
  17585. ' this.$final = function () {',
  17586. ' this.FBirdIntf = undefined;',
  17587. ' this.FDoveObj = undefined;',
  17588. ' $mod.TObject.$final.call(this);',
  17589. ' };',
  17590. ' this.$intfmaps = {',
  17591. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  17592. ' return rtl._AddRef(this.FBirdIntf);',
  17593. ' },',
  17594. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  17595. ' return this.GetEagleIntf();',
  17596. ' },',
  17597. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  17598. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  17599. ' },',
  17600. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  17601. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  17602. ' }',
  17603. ' };',
  17604. '});',
  17605. '']),
  17606. LinesToStr([ // $mod.$main
  17607. '']));
  17608. end;
  17609. procedure TTestModule.TestClassInterface_COM_With;
  17610. begin
  17611. StartProgram(false);
  17612. Add([
  17613. '{$interfaces com}',
  17614. 'type',
  17615. ' IUnknown = interface',
  17616. ' function _AddRef: longint;',
  17617. ' function _Release: longint;',
  17618. ' function GetAnt: IUnknown;',
  17619. ' property Ant: IUnknown read GetAnt;',
  17620. ' end;',
  17621. ' TObject = class(IUnknown)',
  17622. ' function _AddRef: longint; virtual; abstract;',
  17623. ' function _Release: longint; virtual; abstract;',
  17624. ' function GetAnt: IUnknown; virtual; abstract;',
  17625. ' property Ant: IUnknown read GetAnt;',
  17626. ' end;',
  17627. 'procedure DoIt;',
  17628. 'var',
  17629. ' i: IUnknown;',
  17630. 'begin',
  17631. ' with i do ',
  17632. ' GetAnt;',
  17633. ' with i.Ant, Ant do ',
  17634. ' GetAnt;',
  17635. 'end;',
  17636. 'begin',
  17637. '']);
  17638. ConvertProgram;
  17639. CheckSource('TestClassInterface_COM_With',
  17640. LinesToStr([ // statements
  17641. 'rtl.createInterface($mod, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  17642. 'rtl.createClass($mod, "TObject", null, function () {',
  17643. ' this.$init = function () {',
  17644. ' };',
  17645. ' this.$final = function () {',
  17646. ' };',
  17647. ' rtl.addIntf(this, $mod.IUnknown);',
  17648. '});',
  17649. 'this.DoIt = function () {',
  17650. ' var i = null;',
  17651. ' var $ir = rtl.createIntfRefs();',
  17652. ' try {',
  17653. ' $ir.ref(1, i.GetAnt());',
  17654. ' var $with1 = $ir.ref(2, i.GetAnt());',
  17655. ' var $with2 = $ir.ref(3, $with1.GetAnt());',
  17656. ' $ir.ref(4, $with2.GetAnt());',
  17657. ' } finally {',
  17658. ' $ir.free();',
  17659. ' };',
  17660. '};',
  17661. '']),
  17662. LinesToStr([ // $mod.$main
  17663. '']));
  17664. end;
  17665. procedure TTestModule.TestClassInterface_COM_ForIn;
  17666. begin
  17667. StartProgram(false);
  17668. Add([
  17669. '{$interfaces com}',
  17670. 'type',
  17671. ' IUnknown = interface end;',
  17672. ' TObject = class',
  17673. ' Id: longint;',
  17674. ' end;',
  17675. ' IEnumerator = interface(IUnknown)',
  17676. ' function GetCurrent: TObject;',
  17677. ' function MoveNext: Boolean;',
  17678. ' property Current: TObject read GetCurrent;',
  17679. ' end;',
  17680. ' IEnumerable = interface(IUnknown)',
  17681. ' function GetEnumerator: IEnumerator;',
  17682. ' end;',
  17683. 'var',
  17684. ' o: TObject;',
  17685. ' i: IEnumerable;',
  17686. 'begin',
  17687. ' for o in i do o.Id:=3;',
  17688. '']);
  17689. ConvertProgram;
  17690. CheckSource('TestClassInterface_COM_ForIn',
  17691. LinesToStr([ // statements
  17692. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17693. 'rtl.createClass($mod, "TObject", null, function () {',
  17694. ' this.$init = function () {',
  17695. ' this.Id = 0;',
  17696. ' };',
  17697. ' this.$final = function () {',
  17698. ' };',
  17699. '});',
  17700. 'rtl.createInterface($mod, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], $mod.IUnknown);',
  17701. 'rtl.createInterface($mod, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], $mod.IUnknown);',
  17702. 'this.o = null;',
  17703. 'this.i = null;',
  17704. '']),
  17705. LinesToStr([ // $mod.$main
  17706. 'var $in1 = $mod.i.GetEnumerator();',
  17707. 'try {',
  17708. ' while ($in1.MoveNext()) {',
  17709. ' $mod.o = $in1.GetCurrent();',
  17710. ' $mod.o.Id = 3;',
  17711. ' }',
  17712. '} finally {',
  17713. ' rtl._Release($in1)',
  17714. '};',
  17715. '']));
  17716. end;
  17717. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  17718. begin
  17719. StartProgram(false);
  17720. Add([
  17721. '{$interfaces com}',
  17722. 'type',
  17723. ' IUnknown = interface',
  17724. ' function _AddRef: longint;',
  17725. ' function _Release: longint;',
  17726. ' end;',
  17727. ' TObject = class',
  17728. ' end;',
  17729. ' TArrOfIntf = array of IUnknown;',
  17730. 'begin',
  17731. '']);
  17732. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  17733. ConvertProgram;
  17734. end;
  17735. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  17736. begin
  17737. StartProgram(false);
  17738. Add([
  17739. '{$interfaces com}',
  17740. 'type',
  17741. ' IUnknown = interface',
  17742. ' function _AddRef: longint;',
  17743. ' function _Release: longint;',
  17744. ' end;',
  17745. ' TRec = record',
  17746. ' i: IUnknown;',
  17747. ' end;',
  17748. 'begin',
  17749. '']);
  17750. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  17751. ConvertProgram;
  17752. end;
  17753. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  17754. begin
  17755. StartUnit(false);
  17756. Add([
  17757. '{$interfaces com}',
  17758. 'interface',
  17759. 'implementation',
  17760. 'type',
  17761. ' IUnknown = interface',
  17762. ' function _AddRef: longint;',
  17763. ' end;',
  17764. ' TObject = class(IUnknown)',
  17765. ' function _AddRef: longint;',
  17766. ' end;',
  17767. 'function TObject._AddRef: longint; begin end;',
  17768. 'var i: IUnknown;',
  17769. ' o: TObject;',
  17770. 'initialization',
  17771. ' i:=nil;',
  17772. ' i:=i;',
  17773. ' i:=o;',
  17774. ' if (o as IUnknown)=nil then ;',
  17775. '']);
  17776. ConvertUnit;
  17777. CheckSource('TestClassInterface_COM_UnitInitialization',
  17778. LinesToStr([ // statements
  17779. 'var $impl = $mod.$impl;',
  17780. '']),
  17781. LinesToStr([ // this.$init
  17782. 'var $ir = rtl.createIntfRefs();',
  17783. 'try {',
  17784. ' rtl.setIntfP($impl, "i", null);',
  17785. ' rtl.setIntfP($impl, "i", $impl.i);',
  17786. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  17787. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  17788. '} finally {',
  17789. ' $ir.free();',
  17790. '};',
  17791. '']),
  17792. LinesToStr([ // implementation
  17793. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  17794. 'rtl.createClass($impl, "TObject", null, function () {',
  17795. ' this.$init = function () {',
  17796. ' };',
  17797. ' this.$final = function () {',
  17798. ' };',
  17799. ' this._AddRef = function () {',
  17800. ' var Result = 0;',
  17801. ' return Result;',
  17802. ' };',
  17803. ' rtl.addIntf(this, $impl.IUnknown);',
  17804. '});',
  17805. '$impl.i = null;',
  17806. '$impl.o = null;',
  17807. ''])
  17808. );
  17809. end;
  17810. procedure TTestModule.TestClassInterface_GUID;
  17811. begin
  17812. StartProgram(false);
  17813. Add([
  17814. '{$interfaces corba}',
  17815. 'type',
  17816. ' IUnknown = interface',
  17817. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  17818. ' end;',
  17819. ' TObject = class end;',
  17820. ' TGUID = record D1, D2, D3, D4: word; end;',
  17821. ' TAliasGUID = TGUID;',
  17822. ' TGUIDString = type string;',
  17823. ' TAliasGUIDString = TGUIDString;',
  17824. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  17825. 'begin end;',
  17826. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  17827. 'begin end;',
  17828. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  17829. 'begin end;',
  17830. 'var',
  17831. ' i: IUnknown;',
  17832. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  17833. ' s: TAliasGUIDString;',
  17834. 'begin',
  17835. ' DoConstGUIDIt(IUnknown);',
  17836. ' DoDefGUID(IUnknown);',
  17837. ' DoStr(IUnknown);',
  17838. ' DoConstGUIDIt(i);',
  17839. ' DoDefGUID(i);',
  17840. ' DoStr(i);',
  17841. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  17842. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  17843. ' DoStr(g);',
  17844. ' g:=i;',
  17845. ' g:=IUnknown;',
  17846. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  17847. ' s:=i;',
  17848. ' s:=IUnknown;',
  17849. ' s:=g;',
  17850. ' if g=i then ;',
  17851. ' if i=g then ;',
  17852. ' if g=IUnknown then ;',
  17853. ' if IUnknown=g then ;',
  17854. ' if s=i then ;',
  17855. ' if i=s then ;',
  17856. ' if s=IUnknown then ;',
  17857. ' if IUnknown=s then ;',
  17858. ' if s=g then ;',
  17859. ' if g=s then ;',
  17860. '']);
  17861. ConvertProgram;
  17862. CheckSource('TestClassInterface_GUID',
  17863. LinesToStr([ // statements
  17864. 'rtl.createInterface($mod, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  17865. 'rtl.createClass($mod, "TObject", null, function () {',
  17866. ' this.$init = function () {',
  17867. ' };',
  17868. ' this.$final = function () {',
  17869. ' };',
  17870. '});',
  17871. 'rtl.recNewT($mod, "TGUID", function () {',
  17872. ' this.D1 = 0;',
  17873. ' this.D2 = 0;',
  17874. ' this.D3 = 0;',
  17875. ' this.D4 = 0;',
  17876. ' this.$eq = function (b) {',
  17877. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  17878. ' };',
  17879. ' this.$assign = function (s) {',
  17880. ' this.D1 = s.D1;',
  17881. ' this.D2 = s.D2;',
  17882. ' this.D3 = s.D3;',
  17883. ' this.D4 = s.D4;',
  17884. ' return this;',
  17885. ' };',
  17886. '});',
  17887. 'this.DoConstGUIDIt = function (g) {',
  17888. '};',
  17889. 'this.DoDefGUID = function (g) {',
  17890. '};',
  17891. 'this.DoStr = function (s) {',
  17892. '};',
  17893. 'this.i = null;',
  17894. 'this.g = $mod.TGUID.$clone({',
  17895. ' D1: 0xD91C9AF4,',
  17896. ' D2: 0x3C93,',
  17897. ' D3: 0x420F,',
  17898. ' D4: [',
  17899. ' 0xA3,',
  17900. ' 0x03,',
  17901. ' 0xBF,',
  17902. ' 0x5B,',
  17903. ' 0xA8,',
  17904. ' 0x2B,',
  17905. ' 0xFD,',
  17906. ' 0x23',
  17907. ' ]',
  17908. '});',
  17909. 'this.s = "";',
  17910. '']),
  17911. LinesToStr([ // $mod.$main
  17912. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  17913. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  17914. '$mod.DoStr($mod.IUnknown.$guid);',
  17915. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  17916. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  17917. '$mod.DoStr($mod.i.$guid);',
  17918. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  17919. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  17920. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  17921. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  17922. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  17923. '$mod.g.$assign({',
  17924. ' D1: 0xD91C9AF4,',
  17925. ' D2: 0x3C93,',
  17926. ' D3: 0x420F,',
  17927. ' D4: [',
  17928. ' 0xA3,',
  17929. ' 0x03,',
  17930. ' 0xBF,',
  17931. ' 0x5B,',
  17932. ' 0xA8,',
  17933. ' 0x2B,',
  17934. ' 0xFD,',
  17935. ' 0x23',
  17936. ' ]',
  17937. '});',
  17938. '$mod.s = $mod.i.$guid;',
  17939. '$mod.s = $mod.IUnknown.$guid;',
  17940. '$mod.s = rtl.guidrToStr($mod.g);',
  17941. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  17942. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  17943. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  17944. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  17945. 'if ($mod.s === $mod.i.$guid) ;',
  17946. 'if ($mod.i.$guid === $mod.s) ;',
  17947. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  17948. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  17949. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  17950. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  17951. '']));
  17952. end;
  17953. procedure TTestModule.TestClassInterface_GUIDProperty;
  17954. begin
  17955. StartProgram(false);
  17956. Add([
  17957. '{$interfaces corba}',
  17958. 'type',
  17959. ' IUnknown = interface',
  17960. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  17961. ' end;',
  17962. ' TGUID = record D1, D2, D3, D4: word; end;',
  17963. ' TAliasGUID = TGUID;',
  17964. ' TGUIDString = type string;',
  17965. ' TAliasGUIDString = TGUIDString;',
  17966. ' TObject = class',
  17967. ' function GetG: TAliasGUID; virtual; abstract;',
  17968. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  17969. ' function GetS: TAliasGUIDString; virtual; abstract;',
  17970. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  17971. ' property g: TAliasGUID read GetG write SetG;',
  17972. ' property s: TAliasGUIDString read GetS write SetS;',
  17973. ' end;',
  17974. 'var o: TObject;',
  17975. 'begin',
  17976. ' o.g:=IUnknown;',
  17977. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  17978. ' o.s:=IUnknown;',
  17979. ' o.s:=o.g;',
  17980. '']);
  17981. ConvertProgram;
  17982. CheckSource('TestClassInterface_GUIDProperty',
  17983. LinesToStr([ // statements
  17984. 'rtl.createInterface($mod, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  17985. 'rtl.recNewT($mod, "TGUID", function () {',
  17986. ' this.D1 = 0;',
  17987. ' this.D2 = 0;',
  17988. ' this.D3 = 0;',
  17989. ' this.D4 = 0;',
  17990. ' this.$eq = function (b) {',
  17991. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  17992. ' };',
  17993. ' this.$assign = function (s) {',
  17994. ' this.D1 = s.D1;',
  17995. ' this.D2 = s.D2;',
  17996. ' this.D3 = s.D3;',
  17997. ' this.D4 = s.D4;',
  17998. ' return this;',
  17999. ' };',
  18000. '});',
  18001. 'rtl.createClass($mod, "TObject", null, function () {',
  18002. ' this.$init = function () {',
  18003. ' };',
  18004. ' this.$final = function () {',
  18005. ' };',
  18006. '});',
  18007. 'this.o = null;',
  18008. '']),
  18009. LinesToStr([ // $mod.$main
  18010. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  18011. '$mod.o.SetG({',
  18012. ' D1: 0xD91C9AF4,',
  18013. ' D2: 0x3C93,',
  18014. ' D3: 0x420F,',
  18015. ' D4: [',
  18016. ' 0xA3,',
  18017. ' 0x03,',
  18018. ' 0xBF,',
  18019. ' 0x5B,',
  18020. ' 0xA8,',
  18021. ' 0x2B,',
  18022. ' 0xFD,',
  18023. ' 0x23',
  18024. ' ]',
  18025. '});',
  18026. '$mod.o.SetS($mod.IUnknown.$guid);',
  18027. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  18028. '']));
  18029. end;
  18030. procedure TTestModule.TestClassHelper_ClassVar;
  18031. begin
  18032. StartProgram(false);
  18033. Add([
  18034. 'type',
  18035. ' TObject = class',
  18036. ' end;',
  18037. ' THelper = class helper for TObject',
  18038. ' const',
  18039. ' One = 1;',
  18040. ' Two: word = 2;',
  18041. ' class var',
  18042. ' Glob: word;',
  18043. ' function Foo(w: word): word;',
  18044. ' class function Bar(w: word): word;',
  18045. ' end;',
  18046. 'function THelper.foo(w: word): word;',
  18047. 'begin',
  18048. ' Result:=w;',
  18049. ' Two:=One+w;',
  18050. ' Glob:=Glob;',
  18051. ' Result:=Self.Glob;',
  18052. ' Self.Glob:=Self.Glob;',
  18053. ' with Self do Glob:=Glob;',
  18054. 'end;',
  18055. 'class function THelper.bar(w: word): word;',
  18056. 'begin',
  18057. ' Result:=w;',
  18058. ' Two:=One;',
  18059. ' Glob:=Glob;',
  18060. ' Self.Glob:=Self.Glob;',
  18061. ' with Self do Glob:=Glob;',
  18062. 'end;',
  18063. 'var o: TObject;',
  18064. 'begin',
  18065. ' tobject.two:=tobject.one;',
  18066. ' tobject.Glob:=tobject.Glob;',
  18067. ' with tobject do begin',
  18068. ' two:=one;',
  18069. ' Glob:=Glob;',
  18070. ' end;',
  18071. ' o.two:=o.one;',
  18072. ' o.Glob:=o.Glob;',
  18073. ' with o do begin',
  18074. ' two:=one;',
  18075. ' Glob:=Glob;',
  18076. ' end;',
  18077. '']);
  18078. ConvertProgram;
  18079. CheckSource('TestClassHelper',
  18080. LinesToStr([ // statements
  18081. 'rtl.createClass($mod, "TObject", null, function () {',
  18082. ' this.$init = function () {',
  18083. ' };',
  18084. ' this.$final = function () {',
  18085. ' };',
  18086. '});',
  18087. 'rtl.createHelper($mod, "THelper", null, function () {',
  18088. ' this.One = 1;',
  18089. ' this.Two = 2;',
  18090. ' this.Glob = 0;',
  18091. ' this.Foo = function (w) {',
  18092. ' var Result = 0;',
  18093. ' Result = w;',
  18094. ' $mod.THelper.Two = 1 + w;',
  18095. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  18096. ' Result = $mod.THelper.Glob;',
  18097. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  18098. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  18099. ' return Result;',
  18100. ' };',
  18101. ' this.Bar = function (w) {',
  18102. ' var Result = 0;',
  18103. ' Result = w;',
  18104. ' $mod.THelper.Two = 1;',
  18105. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  18106. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  18107. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  18108. ' return Result;',
  18109. ' };',
  18110. '});',
  18111. 'this.o = null;',
  18112. '']),
  18113. LinesToStr([ // $mod.$main
  18114. '$mod.THelper.Two = 1;',
  18115. '$mod.THelper.Glob = $mod.THelper.Glob;',
  18116. 'var $with1 = $mod.TObject;',
  18117. '$mod.THelper.Two = 1;',
  18118. '$mod.THelper.Glob = $mod.THelper.Glob;',
  18119. '$mod.THelper.Two = 1;',
  18120. '$mod.THelper.Glob = $mod.THelper.Glob;',
  18121. 'var $with2 = $mod.o;',
  18122. '$mod.THelper.Two = 1;',
  18123. '$mod.THelper.Glob = $mod.THelper.Glob;',
  18124. '']));
  18125. end;
  18126. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  18127. begin
  18128. StartProgram(false);
  18129. Add([
  18130. 'type',
  18131. ' TObject = class',
  18132. ' FSize: word;',
  18133. ' property Size: word read FSize write FSize;',
  18134. ' end;',
  18135. ' THelper = class helper for TObject',
  18136. ' function Foo(w: word = 1): word;',
  18137. ' end;',
  18138. 'function THelper.foo(w: word): word;',
  18139. 'begin',
  18140. ' Result:=Size;',
  18141. ' Size:=Size+2;',
  18142. ' Self.Size:=Self.Size+3;',
  18143. ' FSize:=FSize+4;',
  18144. ' Self.FSize:=Self.FSize+5;',
  18145. ' with Self do begin',
  18146. ' Size:=Size+6;',
  18147. ' FSize:=FSize+7;',
  18148. ' FSize:=FSize+8;',
  18149. ' end;',
  18150. 'end;',
  18151. 'begin',
  18152. '']);
  18153. ConvertProgram;
  18154. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  18155. LinesToStr([ // statements
  18156. 'rtl.createClass($mod, "TObject", null, function () {',
  18157. ' this.$init = function () {',
  18158. ' this.FSize = 0;',
  18159. ' };',
  18160. ' this.$final = function () {',
  18161. ' };',
  18162. '});',
  18163. 'rtl.createHelper($mod, "THelper", null, function () {',
  18164. ' this.Foo = function (w) {',
  18165. ' var Result = 0;',
  18166. ' Result = this.FSize;',
  18167. ' this.FSize = this.FSize + 2;',
  18168. ' this.FSize = this.FSize + 3;',
  18169. ' this.FSize = this.FSize + 4;',
  18170. ' this.FSize = this.FSize + 5;',
  18171. ' this.FSize = this.FSize + 6;',
  18172. ' this.FSize = this.FSize + 7;',
  18173. ' this.FSize = this.FSize + 8;',
  18174. ' return Result;',
  18175. ' };',
  18176. '});',
  18177. '']),
  18178. LinesToStr([ // $mod.$main
  18179. '']));
  18180. end;
  18181. procedure TTestModule.TestClassHelper_Method_Call;
  18182. begin
  18183. exit;
  18184. StartProgram(false);
  18185. Add([
  18186. 'type',
  18187. ' TObject = class',
  18188. ' procedure Run(w: word = 10);',
  18189. ' end;',
  18190. ' THelper = class helper for TObject',
  18191. ' function Foo(w: word = 1): word;',
  18192. ' end;',
  18193. 'procedure TObject.Run(w: word);',
  18194. 'begin',
  18195. ' Foo;',
  18196. ' Foo();',
  18197. ' Foo(2);',
  18198. ' Self.Foo;',
  18199. ' Self.Foo();',
  18200. ' Self.Foo(3);',
  18201. ' with Self do begin',
  18202. ' Foo;',
  18203. ' Foo();',
  18204. ' Foo(4);',
  18205. ' end;',
  18206. 'end;',
  18207. 'function THelper.foo(w: word): word;',
  18208. 'begin',
  18209. ' Run;',
  18210. ' Run();',
  18211. ' Run(11);',
  18212. ' Foo;',
  18213. ' Foo();',
  18214. ' Foo(12);',
  18215. ' Self.Foo;',
  18216. ' Self.Foo();',
  18217. ' Self.Foo(13);',
  18218. ' with Self do begin',
  18219. ' Foo;',
  18220. ' Foo();',
  18221. ' Foo(14);',
  18222. ' end;',
  18223. 'end;',
  18224. 'var Obj: TObject;',
  18225. 'begin',
  18226. ' obj.Foo;',
  18227. ' obj.Foo();',
  18228. ' obj.Foo(21);',
  18229. ' with obj do begin',
  18230. ' Foo;',
  18231. ' Foo();',
  18232. ' Foo(22);',
  18233. ' end;',
  18234. '']);
  18235. ConvertProgram;
  18236. CheckSource('TestClassHelper_Method_Call',
  18237. LinesToStr([ // statements
  18238. 'rtl.createClass($mod, "TObject", null, function () {',
  18239. ' this.$init = function () {',
  18240. ' this.FSize = 0;',
  18241. ' };',
  18242. ' this.$final = function () {',
  18243. ' };',
  18244. '});',
  18245. 'rtl.createHelper($mod, "THelper", null, function () {',
  18246. ' this.Foo = function (w) {',
  18247. ' var Result = 0;',
  18248. ' Result = this.FSize;',
  18249. ' this.FSize = this.FSize + 2;',
  18250. ' this.FSize = this.FSize + 3;',
  18251. ' this.FSize = this.FSize + 4;',
  18252. ' this.FSize = this.FSize + 5;',
  18253. ' this.FSize = this.FSize + 6;',
  18254. ' this.FSize = this.FSize + 7;',
  18255. ' this.FSize = this.FSize + 8;',
  18256. ' return Result;',
  18257. ' };',
  18258. '});',
  18259. '']),
  18260. LinesToStr([ // $mod.$main
  18261. '']));
  18262. end;
  18263. procedure TTestModule.TestProcType;
  18264. begin
  18265. StartProgram(false);
  18266. Add([
  18267. 'type',
  18268. ' TProcInt = procedure(vI: longint = 1);',
  18269. 'procedure DoIt(vJ: longint);',
  18270. 'begin end;',
  18271. 'var',
  18272. ' b: boolean;',
  18273. ' vP, vQ: tprocint;',
  18274. 'begin',
  18275. ' vp:=nil;',
  18276. ' vp:=vp;',
  18277. ' vp:=@doit;',
  18278. ' vp;',
  18279. ' vp();',
  18280. ' vp(2);',
  18281. ' b:=vp=nil;',
  18282. ' b:=nil=vp;',
  18283. ' b:=vp=vq;',
  18284. ' b:=vp=@doit;',
  18285. ' b:=@doit=vp;',
  18286. ' b:=vp<>nil;',
  18287. ' b:=nil<>vp;',
  18288. ' b:=vp<>vq;',
  18289. ' b:=vp<>@doit;',
  18290. ' b:=@doit<>vp;',
  18291. ' b:=Assigned(vp);',
  18292. ' if Assigned(vp) then ;']);
  18293. ConvertProgram;
  18294. CheckSource('TestProcType',
  18295. LinesToStr([ // statements
  18296. 'this.DoIt = function(vJ) {',
  18297. '};',
  18298. 'this.b = false;',
  18299. 'this.vP = null;',
  18300. 'this.vQ = null;'
  18301. ]),
  18302. LinesToStr([ // $mod.$main
  18303. '$mod.vP = null;',
  18304. '$mod.vP = $mod.vP;',
  18305. '$mod.vP = $mod.DoIt;',
  18306. '$mod.vP(1);',
  18307. '$mod.vP(1);',
  18308. '$mod.vP(2);',
  18309. '$mod.b = $mod.vP === null;',
  18310. '$mod.b = null === $mod.vP;',
  18311. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  18312. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  18313. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  18314. '$mod.b = $mod.vP !== null;',
  18315. '$mod.b = null !== $mod.vP;',
  18316. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  18317. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  18318. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  18319. '$mod.b = $mod.vP != null;',
  18320. 'if ($mod.vP != null) ;',
  18321. '']));
  18322. end;
  18323. procedure TTestModule.TestProcType_Arg;
  18324. begin
  18325. StartProgram(false);
  18326. Add([
  18327. 'type',
  18328. ' TProcInt = procedure(vI: longint = 1);',
  18329. 'procedure DoIt(vJ: longint); begin end;',
  18330. 'procedure DoSome(vP, vQ: TProcInt);',
  18331. 'var',
  18332. ' b: boolean;',
  18333. 'begin',
  18334. ' vp:=nil;',
  18335. ' vp:=vp;',
  18336. ' vp:=@doit;',
  18337. ' vp;',
  18338. ' vp();',
  18339. ' vp(2);',
  18340. ' b:=vp=nil;',
  18341. ' b:=nil=vp;',
  18342. ' b:=vp=vq;',
  18343. ' b:=vp=@doit;',
  18344. ' b:=@doit=vp;',
  18345. ' b:=vp<>nil;',
  18346. ' b:=nil<>vp;',
  18347. ' b:=vp<>vq;',
  18348. ' b:=vp<>@doit;',
  18349. ' b:=@doit<>vp;',
  18350. ' b:=Assigned(vp);',
  18351. ' if Assigned(vp) then ;',
  18352. 'end;',
  18353. 'begin',
  18354. ' DoSome(@DoIt,nil);']);
  18355. ConvertProgram;
  18356. CheckSource('TestProcType_Arg',
  18357. LinesToStr([ // statements
  18358. 'this.DoIt = function(vJ) {',
  18359. '};',
  18360. 'this.DoSome = function(vP, vQ) {',
  18361. ' var b = false;',
  18362. ' vP = null;',
  18363. ' vP = vP;',
  18364. ' vP = $mod.DoIt;',
  18365. ' vP(1);',
  18366. ' vP(1);',
  18367. ' vP(2);',
  18368. ' b = vP === null;',
  18369. ' b = null === vP;',
  18370. ' b = rtl.eqCallback(vP,vQ);',
  18371. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  18372. ' b = rtl.eqCallback($mod.DoIt, vP);',
  18373. ' b = vP !== null;',
  18374. ' b = null !== vP;',
  18375. ' b = !rtl.eqCallback(vP, vQ);',
  18376. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  18377. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  18378. ' b = vP != null;',
  18379. ' if (vP != null) ;',
  18380. '};',
  18381. '']),
  18382. LinesToStr([ // $mod.$main
  18383. '$mod.DoSome($mod.DoIt,null);',
  18384. '']));
  18385. end;
  18386. procedure TTestModule.TestProcType_FunctionFPC;
  18387. begin
  18388. StartProgram(false);
  18389. Add('type');
  18390. Add(' TFuncInt = function(vA: longint = 1): longint;');
  18391. Add('function DoIt(vI: longint): longint;');
  18392. Add('begin end;');
  18393. Add('var');
  18394. Add(' b: boolean;');
  18395. Add(' vP, vQ: tfuncint;');
  18396. Add('begin');
  18397. Add(' vp:=nil;');
  18398. Add(' vp:=vp;');
  18399. Add(' vp:=@doit;'); // ok in fpc and delphi
  18400. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  18401. Add(' vp;'); // ok in fpc and delphi
  18402. Add(' vp();');
  18403. Add(' vp(2);');
  18404. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  18405. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  18406. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  18407. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  18408. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  18409. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  18410. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  18411. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  18412. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  18413. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  18414. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  18415. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  18416. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  18417. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  18418. Add(' b:=Assigned(vp);');
  18419. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  18420. Add(' doit(vp());'); // ok in fpc and delphi
  18421. Add(' doit(vp(2));'); // ok in fpc and delphi
  18422. ConvertProgram;
  18423. CheckSource('TestProcType_FunctionFPC',
  18424. LinesToStr([ // statements
  18425. 'this.DoIt = function(vI) {',
  18426. ' var Result = 0;',
  18427. ' return Result;',
  18428. '};',
  18429. 'this.b = false;',
  18430. 'this.vP = null;',
  18431. 'this.vQ = null;'
  18432. ]),
  18433. LinesToStr([ // $mod.$main
  18434. '$mod.vP = null;',
  18435. '$mod.vP = $mod.vP;',
  18436. '$mod.vP = $mod.DoIt;',
  18437. '$mod.vP(1);',
  18438. '$mod.vP(1);',
  18439. '$mod.vP(2);',
  18440. '$mod.b = $mod.vP === null;',
  18441. '$mod.b = null === $mod.vP;',
  18442. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  18443. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  18444. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  18445. '$mod.b = 4 === $mod.vP(1);',
  18446. '$mod.b = $mod.vP !== null;',
  18447. '$mod.b = null !== $mod.vP;',
  18448. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  18449. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  18450. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  18451. '$mod.b = 6 !== $mod.vP(1);',
  18452. '$mod.b = $mod.vP != null;',
  18453. '$mod.DoIt($mod.vP(1));',
  18454. '$mod.DoIt($mod.vP(2));',
  18455. '']));
  18456. end;
  18457. procedure TTestModule.TestProcType_FunctionDelphi;
  18458. begin
  18459. StartProgram(false);
  18460. Add('{$mode Delphi}');
  18461. Add('type');
  18462. Add(' TFuncInt = function(vA: longint = 1): longint;');
  18463. Add('function DoIt(vI: longint): longint;');
  18464. Add('begin end;');
  18465. Add('var');
  18466. Add(' b: boolean;');
  18467. Add(' vP, vQ: tfuncint;');
  18468. Add('begin');
  18469. Add(' vp:=nil;');
  18470. Add(' vp:=vp;');
  18471. Add(' vp:=@doit;'); // ok in fpc and delphi
  18472. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  18473. Add(' vp;'); // ok in fpc and delphi
  18474. Add(' vp();');
  18475. Add(' vp(2);');
  18476. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  18477. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  18478. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  18479. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  18480. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  18481. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  18482. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  18483. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  18484. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  18485. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  18486. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  18487. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  18488. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  18489. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  18490. Add(' b:=Assigned(vp);');
  18491. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  18492. Add(' doit(vp());'); // ok in fpc and delphi
  18493. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  18494. ConvertProgram;
  18495. CheckSource('TestProcType_FunctionDelphi',
  18496. LinesToStr([ // statements
  18497. 'this.DoIt = function(vI) {',
  18498. ' var Result = 0;',
  18499. ' return Result;',
  18500. '};',
  18501. 'this.b = false;',
  18502. 'this.vP = null;',
  18503. 'this.vQ = null;'
  18504. ]),
  18505. LinesToStr([ // $mod.$main
  18506. '$mod.vP = null;',
  18507. '$mod.vP = $mod.vP;',
  18508. '$mod.vP = $mod.DoIt;',
  18509. '$mod.vP = $mod.DoIt;',
  18510. '$mod.vP(1);',
  18511. '$mod.vP(1);',
  18512. '$mod.vP(2);',
  18513. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  18514. '$mod.b = $mod.vP(1) === 3;',
  18515. '$mod.b = 4 === $mod.vP(1);',
  18516. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  18517. '$mod.b = $mod.vP(1) !== 5;',
  18518. '$mod.b = 6 !== $mod.vP(1);',
  18519. '$mod.b = $mod.vP != null;',
  18520. '$mod.DoIt($mod.vP(1));',
  18521. '$mod.DoIt($mod.vP(1));',
  18522. '$mod.DoIt($mod.vP(2));',
  18523. '']));
  18524. end;
  18525. procedure TTestModule.TestProcType_ProcedureDelphi;
  18526. begin
  18527. StartProgram(false);
  18528. Add('{$mode Delphi}');
  18529. Add('type');
  18530. Add(' TProc = procedure;');
  18531. Add('procedure DoIt;');
  18532. Add('begin end;');
  18533. Add('var');
  18534. Add(' b: boolean;');
  18535. Add(' vP, vQ: tproc;');
  18536. Add('begin');
  18537. Add(' vp:=nil;');
  18538. Add(' vp:=vp;');
  18539. Add(' vp:=vq;');
  18540. 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
  18541. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  18542. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  18543. Add(' vp;'); // ok in fpc and delphi
  18544. Add(' vp();');
  18545. // equal
  18546. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  18547. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  18548. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  18549. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  18550. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  18551. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  18552. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  18553. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  18554. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  18555. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  18556. // unequal
  18557. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  18558. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  18559. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  18560. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  18561. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  18562. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  18563. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  18564. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  18565. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  18566. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  18567. Add(' b:=Assigned(vp);');
  18568. ConvertProgram;
  18569. CheckSource('TestProcType_ProcedureDelphi',
  18570. LinesToStr([ // statements
  18571. 'this.DoIt = function() {',
  18572. '};',
  18573. 'this.b = false;',
  18574. 'this.vP = null;',
  18575. 'this.vQ = null;'
  18576. ]),
  18577. LinesToStr([ // $mod.$main
  18578. '$mod.vP = null;',
  18579. '$mod.vP = $mod.vP;',
  18580. '$mod.vP = $mod.vQ;',
  18581. '$mod.vP = $mod.DoIt;',
  18582. '$mod.vP = $mod.DoIt;',
  18583. '$mod.vP();',
  18584. '$mod.vP();',
  18585. '$mod.b = $mod.vP === null;',
  18586. '$mod.b = null === $mod.vP;',
  18587. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  18588. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  18589. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  18590. '$mod.b = $mod.vP !== null;',
  18591. '$mod.b = null !== $mod.vP;',
  18592. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  18593. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  18594. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  18595. '$mod.b = $mod.vP != null;',
  18596. '']));
  18597. end;
  18598. procedure TTestModule.TestProcType_AsParam;
  18599. begin
  18600. StartProgram(false);
  18601. Add('type');
  18602. Add(' TFuncInt = function(vA: longint = 1): longint;');
  18603. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  18604. Add('var vJ: tfuncint;');
  18605. Add('begin');
  18606. Add(' vg:=vg;');
  18607. Add(' vj:=vh;');
  18608. Add(' vi:=vi;');
  18609. Add(' doit(vg,vg,vg);');
  18610. Add(' doit(vh,vh,vj);');
  18611. Add(' doit(vi,vi,vi);');
  18612. Add(' doit(vj,vj,vj);');
  18613. Add('end;');
  18614. Add('var i: tfuncint;');
  18615. Add('begin');
  18616. Add(' doit(i,i,i);');
  18617. ConvertProgram;
  18618. CheckSource('TestProcType_AsParam',
  18619. LinesToStr([ // statements
  18620. 'this.DoIt = function (vG,vH,vI) {',
  18621. ' var vJ = null;',
  18622. ' vG = vG;',
  18623. ' vJ = vH;',
  18624. ' vI.set(vI.get());',
  18625. ' $mod.DoIt(vG, vG, {',
  18626. ' get: function () {',
  18627. ' return vG;',
  18628. ' },',
  18629. ' set: function (v) {',
  18630. ' vG = v;',
  18631. ' }',
  18632. ' });',
  18633. ' $mod.DoIt(vH, vH, {',
  18634. ' get: function () {',
  18635. ' return vJ;',
  18636. ' },',
  18637. ' set: function (v) {',
  18638. ' vJ = v;',
  18639. ' }',
  18640. ' });',
  18641. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  18642. ' $mod.DoIt(vJ, vJ, {',
  18643. ' get: function () {',
  18644. ' return vJ;',
  18645. ' },',
  18646. ' set: function (v) {',
  18647. ' vJ = v;',
  18648. ' }',
  18649. ' });',
  18650. '};',
  18651. 'this.i = null;'
  18652. ]),
  18653. LinesToStr([
  18654. '$mod.DoIt($mod.i,$mod.i,{',
  18655. ' p: $mod,',
  18656. ' get: function () {',
  18657. ' return this.p.i;',
  18658. ' },',
  18659. ' set: function (v) {',
  18660. ' this.p.i = v;',
  18661. ' }',
  18662. '});'
  18663. ]));
  18664. end;
  18665. procedure TTestModule.TestProcType_MethodFPC;
  18666. begin
  18667. StartProgram(false);
  18668. Add('type');
  18669. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  18670. Add(' TObject = class');
  18671. Add(' function DoIt(vA: longint = 1): longint;');
  18672. Add(' end;');
  18673. Add('function TObject.DoIt(vA: longint = 1): longint;');
  18674. Add('begin');
  18675. Add('end;');
  18676. Add('var');
  18677. Add(' Obj: TObject;');
  18678. Add(' vP: tfuncint;');
  18679. Add(' b: boolean;');
  18680. Add('begin');
  18681. Add(' vp:[email protected];'); // ok in fpc and delphi
  18682. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  18683. Add(' vp;'); // ok in fpc and delphi
  18684. Add(' vp();');
  18685. Add(' vp(2);');
  18686. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  18687. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  18688. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  18689. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  18690. ConvertProgram;
  18691. CheckSource('TestProcType_MethodFPC',
  18692. LinesToStr([ // statements
  18693. 'rtl.createClass($mod, "TObject", null, function () {',
  18694. ' this.$init = function () {',
  18695. ' };',
  18696. ' this.$final = function () {',
  18697. ' };',
  18698. ' this.DoIt = function (vA) {',
  18699. ' var Result = 0;',
  18700. ' return Result;',
  18701. ' };',
  18702. '});',
  18703. 'this.Obj = null;',
  18704. 'this.vP = null;',
  18705. 'this.b = false;'
  18706. ]),
  18707. LinesToStr([
  18708. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  18709. '$mod.vP(1);',
  18710. '$mod.vP(1);',
  18711. '$mod.vP(2);',
  18712. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  18713. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  18714. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  18715. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  18716. '']));
  18717. end;
  18718. procedure TTestModule.TestProcType_MethodDelphi;
  18719. begin
  18720. StartProgram(false);
  18721. Add('{$mode delphi}');
  18722. Add('type');
  18723. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  18724. Add(' TObject = class');
  18725. Add(' function DoIt(vA: longint = 1): longint;');
  18726. Add(' end;');
  18727. Add('function TObject.DoIt(vA: longint = 1): longint;');
  18728. Add('begin');
  18729. Add('end;');
  18730. Add('var');
  18731. Add(' Obj: TObject;');
  18732. Add(' vP: tfuncint;');
  18733. Add(' b: boolean;');
  18734. Add('begin');
  18735. Add(' vp:[email protected];'); // ok in fpc and delphi
  18736. Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  18737. Add(' vp;'); // ok in fpc and delphi
  18738. Add(' vp();');
  18739. Add(' vp(2);');
  18740. //Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  18741. //Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  18742. //Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  18743. //Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  18744. ConvertProgram;
  18745. CheckSource('TestProcType_MethodDelphi',
  18746. LinesToStr([ // statements
  18747. 'rtl.createClass($mod, "TObject", null, function () {',
  18748. ' this.$init = function () {',
  18749. ' };',
  18750. ' this.$final = function () {',
  18751. ' };',
  18752. ' this.DoIt = function (vA) {',
  18753. ' var Result = 0;',
  18754. ' return Result;',
  18755. ' };',
  18756. '});',
  18757. 'this.Obj = null;',
  18758. 'this.vP = null;',
  18759. 'this.b = false;'
  18760. ]),
  18761. LinesToStr([
  18762. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  18763. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  18764. '$mod.vP(1);',
  18765. '$mod.vP(1);',
  18766. '$mod.vP(2);',
  18767. '']));
  18768. end;
  18769. procedure TTestModule.TestProcType_PropertyFPC;
  18770. begin
  18771. StartProgram(false);
  18772. Add('type');
  18773. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  18774. Add(' TObject = class');
  18775. Add(' FOnFoo: TFuncInt;');
  18776. Add(' function DoIt(vA: longint = 1): longint;');
  18777. Add(' function GetFoo: TFuncInt;');
  18778. Add(' procedure SetFoo(const Value: TFuncInt);');
  18779. Add(' function GetEvents(Index: longint): TFuncInt;');
  18780. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  18781. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  18782. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  18783. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  18784. Add(' end;');
  18785. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  18786. Add('function tobject.getfoo: tfuncint; begin end;');
  18787. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  18788. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  18789. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  18790. Add('var');
  18791. Add(' Obj: TObject;');
  18792. Add(' vP: tfuncint;');
  18793. Add(' b: boolean;');
  18794. Add('begin');
  18795. Add(' obj.onfoo:=nil;');
  18796. Add(' obj.onbar:=nil;');
  18797. Add(' obj.events[1]:=nil;');
  18798. Add(' obj.onfoo:=obj.onfoo;');
  18799. Add(' obj.onbar:=obj.onbar;');
  18800. Add(' obj.events[2]:=obj.events[3];');
  18801. Add(' obj.onfoo:[email protected];');
  18802. Add(' obj.onbar:[email protected];');
  18803. Add(' obj.events[4]:[email protected];');
  18804. //Add(' obj.onfoo:=obj.doit;'); // delphi
  18805. //Add(' obj.onbar:=obj.doit;'); // delphi
  18806. //Add(' obj.events[4]:=obj.doit;'); // delphi
  18807. Add(' obj.onfoo;');
  18808. Add(' obj.onbar;');
  18809. //Add(' obj.events[5];'); ToDo in pasresolver
  18810. Add(' obj.onfoo();');
  18811. Add(' obj.onbar();');
  18812. Add(' obj.events[6]();');
  18813. Add(' b:=obj.onfoo=nil;');
  18814. Add(' b:=obj.onbar=nil;');
  18815. Add(' b:=obj.events[7]=nil;');
  18816. Add(' b:=obj.onfoo<>nil;');
  18817. Add(' b:=obj.onbar<>nil;');
  18818. Add(' b:=obj.events[8]<>nil;');
  18819. Add(' b:=obj.onfoo=vp;');
  18820. Add(' b:=obj.onbar=vp;');
  18821. Add(' b:=obj.events[9]=vp;');
  18822. Add(' b:=obj.onfoo=obj.onfoo;');
  18823. Add(' b:=obj.onbar=obj.onfoo;');
  18824. Add(' b:=obj.events[10]=obj.onfoo;');
  18825. Add(' b:=obj.onfoo<>obj.onfoo;');
  18826. Add(' b:=obj.onbar<>obj.onfoo;');
  18827. Add(' b:=obj.events[11]<>obj.onfoo;');
  18828. Add(' b:[email protected];');
  18829. Add(' b:[email protected];');
  18830. Add(' b:=obj.events[12][email protected];');
  18831. Add(' b:=obj.onfoo<>@obj.doit;');
  18832. Add(' b:=obj.onbar<>@obj.doit;');
  18833. Add(' b:=obj.events[12]<>@obj.doit;');
  18834. Add(' b:=Assigned(obj.onfoo);');
  18835. Add(' b:=Assigned(obj.onbar);');
  18836. Add(' b:=Assigned(obj.events[13]);');
  18837. ConvertProgram;
  18838. CheckSource('TestProcType_PropertyFPC',
  18839. LinesToStr([ // statements
  18840. 'rtl.createClass($mod, "TObject", null, function () {',
  18841. ' this.$init = function () {',
  18842. ' this.FOnFoo = null;',
  18843. ' };',
  18844. ' this.$final = function () {',
  18845. ' this.FOnFoo = undefined;',
  18846. ' };',
  18847. ' this.DoIt = function (vA) {',
  18848. ' var Result = 0;',
  18849. ' return Result;',
  18850. ' };',
  18851. 'this.GetFoo = function () {',
  18852. ' var Result = null;',
  18853. ' return Result;',
  18854. '};',
  18855. 'this.SetFoo = function (Value) {',
  18856. '};',
  18857. 'this.GetEvents = function (Index) {',
  18858. ' var Result = null;',
  18859. ' return Result;',
  18860. '};',
  18861. 'this.SetEvents = function (Index, Value) {',
  18862. '};',
  18863. '});',
  18864. 'this.Obj = null;',
  18865. 'this.vP = null;',
  18866. 'this.b = false;'
  18867. ]),
  18868. LinesToStr([
  18869. '$mod.Obj.FOnFoo = null;',
  18870. '$mod.Obj.SetFoo(null);',
  18871. '$mod.Obj.SetEvents(1, null);',
  18872. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  18873. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  18874. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  18875. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  18876. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  18877. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  18878. '$mod.Obj.FOnFoo(1);',
  18879. '$mod.Obj.GetFoo();',
  18880. '$mod.Obj.FOnFoo(1);',
  18881. '$mod.Obj.GetFoo()(1);',
  18882. '$mod.Obj.GetEvents(6)(1);',
  18883. '$mod.b = $mod.Obj.FOnFoo === null;',
  18884. '$mod.b = $mod.Obj.GetFoo() === null;',
  18885. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  18886. '$mod.b = $mod.Obj.FOnFoo !== null;',
  18887. '$mod.b = $mod.Obj.GetFoo() !== null;',
  18888. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  18889. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  18890. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  18891. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  18892. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  18893. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  18894. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  18895. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  18896. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  18897. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  18898. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  18899. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  18900. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  18901. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  18902. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  18903. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  18904. '$mod.b = $mod.Obj.FOnFoo != null;',
  18905. '$mod.b = $mod.Obj.GetFoo() != null;',
  18906. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  18907. '']));
  18908. end;
  18909. procedure TTestModule.TestProcType_PropertyDelphi;
  18910. begin
  18911. StartProgram(false);
  18912. Add('{$mode delphi}');
  18913. Add('type');
  18914. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  18915. Add(' TObject = class');
  18916. Add(' FOnFoo: TFuncInt;');
  18917. Add(' function DoIt(vA: longint = 1): longint;');
  18918. Add(' function GetFoo: TFuncInt;');
  18919. Add(' procedure SetFoo(const Value: TFuncInt);');
  18920. Add(' function GetEvents(Index: longint): TFuncInt;');
  18921. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  18922. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  18923. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  18924. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  18925. Add(' end;');
  18926. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  18927. Add('function tobject.getfoo: tfuncint; begin end;');
  18928. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  18929. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  18930. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  18931. Add('var');
  18932. Add(' Obj: TObject;');
  18933. Add(' vP: tfuncint;');
  18934. Add(' b: boolean;');
  18935. Add('begin');
  18936. Add(' obj.onfoo:=nil;');
  18937. Add(' obj.onbar:=nil;');
  18938. Add(' obj.events[1]:=nil;');
  18939. Add(' obj.onfoo:=obj.onfoo;');
  18940. Add(' obj.onbar:=obj.onbar;');
  18941. Add(' obj.events[2]:=obj.events[3];');
  18942. Add(' obj.onfoo:[email protected];');
  18943. Add(' obj.onbar:[email protected];');
  18944. Add(' obj.events[4]:[email protected];');
  18945. Add(' obj.onfoo:=obj.doit;'); // delphi
  18946. Add(' obj.onbar:=obj.doit;'); // delphi
  18947. Add(' obj.events[4]:=obj.doit;'); // delphi
  18948. Add(' obj.onfoo;');
  18949. Add(' obj.onbar;');
  18950. //Add(' obj.events[5];'); ToDo in pasresolver
  18951. Add(' obj.onfoo();');
  18952. Add(' obj.onbar();');
  18953. Add(' obj.events[6]();');
  18954. //Add(' b:=obj.onfoo=nil;'); // fpc
  18955. //Add(' b:=obj.onbar=nil;'); // fpc
  18956. //Add(' b:=obj.events[7]=nil;'); // fpc
  18957. //Add(' b:=obj.onfoo<>nil;'); // fpc
  18958. //Add(' b:=obj.onbar<>nil;'); // fpc
  18959. //Add(' b:=obj.events[8]<>nil;'); // fpc
  18960. Add(' b:=obj.onfoo=vp;');
  18961. Add(' b:=obj.onbar=vp;');
  18962. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  18963. Add(' b:=obj.onfoo=obj.onfoo;');
  18964. Add(' b:=obj.onbar=obj.onfoo;');
  18965. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  18966. Add(' b:=obj.onfoo<>obj.onfoo;');
  18967. Add(' b:=obj.onbar<>obj.onfoo;');
  18968. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  18969. //Add(' b:[email protected];'); // fpc
  18970. //Add(' b:[email protected];'); // fpc
  18971. //Add(' b:=obj.events[12][email protected];'); // fpc
  18972. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  18973. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  18974. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  18975. Add(' b:=Assigned(obj.onfoo);');
  18976. Add(' b:=Assigned(obj.onbar);');
  18977. Add(' b:=Assigned(obj.events[13]);');
  18978. ConvertProgram;
  18979. CheckSource('TestProcType_PropertyDelphi',
  18980. LinesToStr([ // statements
  18981. 'rtl.createClass($mod, "TObject", null, function () {',
  18982. ' this.$init = function () {',
  18983. ' this.FOnFoo = null;',
  18984. ' };',
  18985. ' this.$final = function () {',
  18986. ' this.FOnFoo = undefined;',
  18987. ' };',
  18988. ' this.DoIt = function (vA) {',
  18989. ' var Result = 0;',
  18990. ' return Result;',
  18991. ' };',
  18992. 'this.GetFoo = function () {',
  18993. ' var Result = null;',
  18994. ' return Result;',
  18995. '};',
  18996. 'this.SetFoo = function (Value) {',
  18997. '};',
  18998. 'this.GetEvents = function (Index) {',
  18999. ' var Result = null;',
  19000. ' return Result;',
  19001. '};',
  19002. 'this.SetEvents = function (Index, Value) {',
  19003. '};',
  19004. '});',
  19005. 'this.Obj = null;',
  19006. 'this.vP = null;',
  19007. 'this.b = false;'
  19008. ]),
  19009. LinesToStr([
  19010. '$mod.Obj.FOnFoo = null;',
  19011. '$mod.Obj.SetFoo(null);',
  19012. '$mod.Obj.SetEvents(1, null);',
  19013. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  19014. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  19015. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  19016. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  19017. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  19018. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  19019. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  19020. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  19021. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  19022. '$mod.Obj.FOnFoo(1);',
  19023. '$mod.Obj.GetFoo();',
  19024. '$mod.Obj.FOnFoo(1);',
  19025. '$mod.Obj.GetFoo()(1);',
  19026. '$mod.Obj.GetEvents(6)(1);',
  19027. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  19028. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  19029. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  19030. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  19031. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  19032. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  19033. '$mod.b = $mod.Obj.FOnFoo != null;',
  19034. '$mod.b = $mod.Obj.GetFoo() != null;',
  19035. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  19036. '']));
  19037. end;
  19038. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  19039. begin
  19040. StartProgram(false);
  19041. Add('type');
  19042. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  19043. Add(' TObject = class');
  19044. Add(' FOnFoo: TFuncInt;');
  19045. Add(' function DoIt(vA: longint = 1): longint;');
  19046. Add(' function GetFoo: TFuncInt;');
  19047. Add(' procedure SetFoo(const Value: TFuncInt);');
  19048. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  19049. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  19050. Add(' end;');
  19051. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  19052. Add('function tobject.getfoo: tfuncint; begin end;');
  19053. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  19054. Add('var');
  19055. Add(' Obj: TObject;');
  19056. Add(' vP: tfuncint;');
  19057. Add(' b: boolean;');
  19058. Add('begin');
  19059. Add('with obj do begin');
  19060. Add(' fonfoo:=nil;');
  19061. Add(' onfoo:=nil;');
  19062. Add(' onbar:=nil;');
  19063. Add(' fonfoo:=fonfoo;');
  19064. Add(' onfoo:=onfoo;');
  19065. Add(' onbar:=onbar;');
  19066. Add(' fonfoo:=@doit;');
  19067. Add(' onfoo:=@doit;');
  19068. Add(' onbar:=@doit;');
  19069. //Add(' fonfoo:=doit;'); // delphi
  19070. //Add(' onfoo:=doit;'); // delphi
  19071. //Add(' onbar:=doit;'); // delphi
  19072. Add(' fonfoo;');
  19073. Add(' onfoo;');
  19074. Add(' onbar;');
  19075. Add(' fonfoo();');
  19076. Add(' onfoo();');
  19077. Add(' onbar();');
  19078. Add(' b:=fonfoo=nil;');
  19079. Add(' b:=onfoo=nil;');
  19080. Add(' b:=onbar=nil;');
  19081. Add(' b:=fonfoo<>nil;');
  19082. Add(' b:=onfoo<>nil;');
  19083. Add(' b:=onbar<>nil;');
  19084. Add(' b:=fonfoo=vp;');
  19085. Add(' b:=onfoo=vp;');
  19086. Add(' b:=onbar=vp;');
  19087. Add(' b:=fonfoo=fonfoo;');
  19088. Add(' b:=onfoo=onfoo;');
  19089. Add(' b:=onbar=onfoo;');
  19090. Add(' b:=fonfoo<>fonfoo;');
  19091. Add(' b:=onfoo<>onfoo;');
  19092. Add(' b:=onbar<>onfoo;');
  19093. Add(' b:=fonfoo=@doit;');
  19094. Add(' b:=onfoo=@doit;');
  19095. Add(' b:=onbar=@doit;');
  19096. Add(' b:=fonfoo<>@doit;');
  19097. Add(' b:=onfoo<>@doit;');
  19098. Add(' b:=onbar<>@doit;');
  19099. Add(' b:=Assigned(fonfoo);');
  19100. Add(' b:=Assigned(onfoo);');
  19101. Add(' b:=Assigned(onbar);');
  19102. Add('end;');
  19103. ConvertProgram;
  19104. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  19105. LinesToStr([ // statements
  19106. 'rtl.createClass($mod, "TObject", null, function () {',
  19107. ' this.$init = function () {',
  19108. ' this.FOnFoo = null;',
  19109. ' };',
  19110. ' this.$final = function () {',
  19111. ' this.FOnFoo = undefined;',
  19112. ' };',
  19113. ' this.DoIt = function (vA) {',
  19114. ' var Result = 0;',
  19115. ' return Result;',
  19116. ' };',
  19117. ' this.GetFoo = function () {',
  19118. ' var Result = null;',
  19119. ' return Result;',
  19120. ' };',
  19121. ' this.SetFoo = function (Value) {',
  19122. ' };',
  19123. '});',
  19124. 'this.Obj = null;',
  19125. 'this.vP = null;',
  19126. 'this.b = false;'
  19127. ]),
  19128. LinesToStr([
  19129. 'var $with1 = $mod.Obj;',
  19130. '$with1.FOnFoo = null;',
  19131. '$with1.FOnFoo = null;',
  19132. '$with1.SetFoo(null);',
  19133. '$with1.FOnFoo = $with1.FOnFoo;',
  19134. '$with1.FOnFoo = $with1.FOnFoo;',
  19135. '$with1.SetFoo($with1.GetFoo());',
  19136. '$with1.FOnFoo = rtl.createCallback($with1, "DoIt");',
  19137. '$with1.FOnFoo = rtl.createCallback($with1, "DoIt");',
  19138. '$with1.SetFoo(rtl.createCallback($with1, "DoIt"));',
  19139. '$with1.FOnFoo(1);',
  19140. '$with1.FOnFoo(1);',
  19141. '$with1.GetFoo();',
  19142. '$with1.FOnFoo(1);',
  19143. '$with1.FOnFoo(1);',
  19144. '$with1.GetFoo()(1);',
  19145. '$mod.b = $with1.FOnFoo === null;',
  19146. '$mod.b = $with1.FOnFoo === null;',
  19147. '$mod.b = $with1.GetFoo() === null;',
  19148. '$mod.b = $with1.FOnFoo !== null;',
  19149. '$mod.b = $with1.FOnFoo !== null;',
  19150. '$mod.b = $with1.GetFoo() !== null;',
  19151. '$mod.b = rtl.eqCallback($with1.FOnFoo, $mod.vP);',
  19152. '$mod.b = rtl.eqCallback($with1.FOnFoo, $mod.vP);',
  19153. '$mod.b = rtl.eqCallback($with1.GetFoo(), $mod.vP);',
  19154. '$mod.b = rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  19155. '$mod.b = rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  19156. '$mod.b = rtl.eqCallback($with1.GetFoo(), $with1.FOnFoo);',
  19157. '$mod.b = !rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  19158. '$mod.b = !rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  19159. '$mod.b = !rtl.eqCallback($with1.GetFoo(), $with1.FOnFoo);',
  19160. '$mod.b = rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  19161. '$mod.b = rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  19162. '$mod.b = rtl.eqCallback($with1.GetFoo(), rtl.createCallback($with1, "DoIt"));',
  19163. '$mod.b = !rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  19164. '$mod.b = !rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  19165. '$mod.b = !rtl.eqCallback($with1.GetFoo(), rtl.createCallback($with1, "DoIt"));',
  19166. '$mod.b = $with1.FOnFoo != null;',
  19167. '$mod.b = $with1.FOnFoo != null;',
  19168. '$mod.b = $with1.GetFoo() != null;',
  19169. '']));
  19170. end;
  19171. procedure TTestModule.TestProcType_Nested;
  19172. begin
  19173. StartProgram(false);
  19174. Add([
  19175. 'type',
  19176. ' TProcInt = procedure(vI: longint = 1);',
  19177. 'procedure DoIt(vJ: longint);',
  19178. 'var aProc: TProcInt;',
  19179. ' b: boolean;',
  19180. ' procedure Sub(vK: longint);',
  19181. ' var aSub: TProcInt;',
  19182. ' procedure SubSub(vK: longint);',
  19183. ' var aSubSub: TProcInt;',
  19184. ' begin;',
  19185. ' aProc:=@DoIt;',
  19186. ' aSub:=@DoIt;',
  19187. ' aSubSub:=@DoIt;',
  19188. ' aProc:=@Sub;',
  19189. ' aSub:=@Sub;',
  19190. ' aSubSub:=@Sub;',
  19191. ' aProc:=@SubSub;',
  19192. ' aSub:=@SubSub;',
  19193. ' aSubSub:=@SubSub;',
  19194. ' end;',
  19195. ' begin;',
  19196. ' end;',
  19197. 'begin;',
  19198. ' aProc:=@Sub;',
  19199. ' b:=aProc=@Sub;',
  19200. ' b:=@Sub=aProc;',
  19201. 'end;',
  19202. 'begin',
  19203. '']);
  19204. ConvertProgram;
  19205. CheckSource('TestProcType_Nested',
  19206. LinesToStr([ // statements
  19207. 'this.DoIt = function (vJ) {',
  19208. ' var aProc = null;',
  19209. ' var b = false;',
  19210. ' function Sub(vK) {',
  19211. ' var aSub = null;',
  19212. ' function SubSub(vK) {',
  19213. ' var aSubSub = null;',
  19214. ' aProc = $mod.DoIt;',
  19215. ' aSub = $mod.DoIt;',
  19216. ' aSubSub = $mod.DoIt;',
  19217. ' aProc = Sub;',
  19218. ' aSub = Sub;',
  19219. ' aSubSub = Sub;',
  19220. ' aProc = SubSub;',
  19221. ' aSub = SubSub;',
  19222. ' aSubSub = SubSub;',
  19223. ' };',
  19224. ' };',
  19225. ' aProc = Sub;',
  19226. ' b = rtl.eqCallback(aProc, Sub);',
  19227. ' b = rtl.eqCallback(Sub, aProc);',
  19228. '};',
  19229. '']),
  19230. LinesToStr([ // $mod.$main
  19231. '']));
  19232. end;
  19233. procedure TTestModule.TestProcType_NestedOfObject;
  19234. begin
  19235. StartProgram(false);
  19236. Add([
  19237. 'type',
  19238. ' TProcInt = procedure(vI: longint = 1) of object;',
  19239. ' TObject = class',
  19240. ' procedure DoIt(vJ: longint);',
  19241. ' end;',
  19242. 'procedure TObject.DoIt(vJ: longint);',
  19243. 'var aProc: TProcInt;',
  19244. ' b: boolean;',
  19245. ' procedure Sub(vK: longint);',
  19246. ' var aSub: TProcInt;',
  19247. ' procedure SubSub(vK: longint);',
  19248. ' var aSubSub: TProcInt;',
  19249. ' begin;',
  19250. ' aProc:=@DoIt;',
  19251. ' aSub:=@DoIt;',
  19252. ' aSubSub:=@DoIt;',
  19253. ' aProc:=@Sub;',
  19254. ' aSub:=@Sub;',
  19255. ' aSubSub:=@Sub;',
  19256. ' aProc:=@SubSub;',
  19257. ' aSub:=@SubSub;',
  19258. ' aSubSub:=@SubSub;',
  19259. ' end;',
  19260. ' begin;',
  19261. ' end;',
  19262. 'begin;',
  19263. ' aProc:=@Sub;',
  19264. ' b:=aProc=@Sub;',
  19265. ' b:=@Sub=aProc;',
  19266. 'end;',
  19267. 'begin',
  19268. '']);
  19269. ConvertProgram;
  19270. CheckSource('TestProcType_Nested',
  19271. LinesToStr([ // statements
  19272. 'rtl.createClass($mod, "TObject", null, function () {',
  19273. ' this.$init = function () {',
  19274. ' };',
  19275. ' this.$final = function () {',
  19276. ' };',
  19277. ' this.DoIt = function (vJ) {',
  19278. ' var Self = this;',
  19279. ' var aProc = null;',
  19280. ' var b = false;',
  19281. ' function Sub(vK) {',
  19282. ' var aSub = null;',
  19283. ' function SubSub(vK) {',
  19284. ' var aSubSub = null;',
  19285. ' aProc = rtl.createCallback(Self, "DoIt");',
  19286. ' aSub = rtl.createCallback(Self, "DoIt");',
  19287. ' aSubSub = rtl.createCallback(Self, "DoIt");',
  19288. ' aProc = Sub;',
  19289. ' aSub = Sub;',
  19290. ' aSubSub = Sub;',
  19291. ' aProc = SubSub;',
  19292. ' aSub = SubSub;',
  19293. ' aSubSub = SubSub;',
  19294. ' };',
  19295. ' };',
  19296. ' aProc = Sub;',
  19297. ' b = rtl.eqCallback(aProc, Sub);',
  19298. ' b = rtl.eqCallback(Sub, aProc);',
  19299. ' };',
  19300. '});',
  19301. '']),
  19302. LinesToStr([ // $mod.$main
  19303. '']));
  19304. end;
  19305. procedure TTestModule.TestProcType_ReferenceToProc;
  19306. begin
  19307. StartProgram(false);
  19308. Add([
  19309. 'type',
  19310. ' TProcRef = reference to procedure(i: longint = 0);',
  19311. ' TFuncRef = reference to function(i: longint = 0): longint;',
  19312. 'var',
  19313. ' p: TProcRef;',
  19314. ' f: TFuncRef;',
  19315. 'procedure DoIt(i: longint);',
  19316. 'begin',
  19317. 'end;',
  19318. 'function GetIt(i: longint): longint;',
  19319. 'begin',
  19320. ' p:=@DoIt;',
  19321. ' f:=@GetIt;',
  19322. ' f;',
  19323. ' f();',
  19324. ' f(1);',
  19325. 'end;',
  19326. 'begin',
  19327. ' p:=@DoIt;',
  19328. ' f:=@GetIt;',
  19329. ' f;',
  19330. ' f();',
  19331. ' f(1);',
  19332. ' p:=TProcRef(f);',
  19333. '']);
  19334. ConvertProgram;
  19335. CheckSource('TestProcType_ReferenceToProc',
  19336. LinesToStr([ // statements
  19337. 'this.p = null;',
  19338. 'this.f = null;',
  19339. 'this.DoIt = function (i) {',
  19340. '};',
  19341. 'this.GetIt = function (i) {',
  19342. ' var Result = 0;',
  19343. ' $mod.p = $mod.DoIt;',
  19344. ' $mod.f = $mod.GetIt;',
  19345. ' $mod.f(0);',
  19346. ' $mod.f(0);',
  19347. ' $mod.f(1);',
  19348. ' return Result;',
  19349. '};',
  19350. '']),
  19351. LinesToStr([ // $mod.$main
  19352. '$mod.p = $mod.DoIt;',
  19353. '$mod.f = $mod.GetIt;',
  19354. '$mod.f(0);',
  19355. '$mod.f(0);',
  19356. '$mod.f(1);',
  19357. '$mod.p = $mod.f;',
  19358. '']));
  19359. end;
  19360. procedure TTestModule.TestProcType_ReferenceToMethod;
  19361. begin
  19362. StartProgram(false);
  19363. Add([
  19364. 'type',
  19365. ' TFuncRef = reference to function(i: longint = 5): longint;',
  19366. ' TObject = class',
  19367. ' function Grow(s: longint): longint;',
  19368. ' end;',
  19369. 'var',
  19370. ' f: tfuncref;',
  19371. 'function tobject.grow(s: longint): longint;',
  19372. ' function GrowSub(i: longint): longint;',
  19373. ' begin',
  19374. ' f:=@grow;',
  19375. ' f:=@growsub;',
  19376. ' end;',
  19377. 'begin',
  19378. ' f:=@grow;',
  19379. ' f:=@growsub;',
  19380. 'end;',
  19381. 'begin',
  19382. '']);
  19383. ConvertProgram;
  19384. CheckSource('TestProcType_ReferenceToMethod',
  19385. LinesToStr([ // statements
  19386. 'rtl.createClass($mod, "TObject", null, function () {',
  19387. ' this.$init = function () {',
  19388. ' };',
  19389. ' this.$final = function () {',
  19390. ' };',
  19391. ' this.Grow = function (s) {',
  19392. ' var Self = this;',
  19393. ' var Result = 0;',
  19394. ' function GrowSub(i) {',
  19395. ' var Result = 0;',
  19396. ' $mod.f = rtl.createCallback(Self, "Grow");',
  19397. ' $mod.f = GrowSub;',
  19398. ' return Result;',
  19399. ' };',
  19400. ' $mod.f = rtl.createCallback(Self, "Grow");',
  19401. ' $mod.f = GrowSub;',
  19402. ' return Result;',
  19403. ' };',
  19404. '});',
  19405. 'this.f = null;',
  19406. '']),
  19407. LinesToStr([ // $mod.$main
  19408. '']));
  19409. end;
  19410. procedure TTestModule.TestProcType_Typecast;
  19411. begin
  19412. StartProgram(false);
  19413. Add([
  19414. 'type',
  19415. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  19416. ' TEvent = procedure of object;',
  19417. ' TGetter = function:longint of object;',
  19418. ' TProcA = procedure(i: longint);',
  19419. ' TFuncB = function(i, j: longint): longint;',
  19420. 'procedure DoIt(); varargs; begin end;',
  19421. 'var',
  19422. ' Notify: tnotifyevent;',
  19423. ' Event: tevent;',
  19424. ' Getter: tgetter;',
  19425. ' ProcA: tproca;',
  19426. ' FuncB: tfuncb;',
  19427. ' p: pointer;',
  19428. 'begin',
  19429. ' notify:=tnotifyevent(event);',
  19430. ' event:=tevent(event);',
  19431. ' event:=tevent(notify);',
  19432. ' event:=tevent(getter);',
  19433. ' event:=tevent(proca);',
  19434. ' proca:=tproca(funcb);',
  19435. ' funcb:=tfuncb(funcb);',
  19436. ' funcb:=tfuncb(proca);',
  19437. ' funcb:=tfuncb(getter);',
  19438. ' proca:=tproca(p);',
  19439. ' funcb:=tfuncb(p);',
  19440. ' getter:=tgetter(p);',
  19441. ' p:=pointer(notify);',
  19442. ' p:=notify;',
  19443. ' p:=pointer(proca);',
  19444. ' p:=proca;',
  19445. ' p:=pointer(funcb);',
  19446. ' p:=funcb;',
  19447. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  19448. '']);
  19449. ConvertProgram;
  19450. CheckSource('TestProcType_Typecast',
  19451. LinesToStr([ // statements
  19452. 'this.DoIt = function () {',
  19453. '};',
  19454. 'this.Notify = null;',
  19455. 'this.Event = null;',
  19456. 'this.Getter = null;',
  19457. 'this.ProcA = null;',
  19458. 'this.FuncB = null;',
  19459. 'this.p = null;',
  19460. '']),
  19461. LinesToStr([ // $mod.$main
  19462. '$mod.Notify = $mod.Event;',
  19463. '$mod.Event = $mod.Event;',
  19464. '$mod.Event = $mod.Notify;',
  19465. '$mod.Event = $mod.Getter;',
  19466. '$mod.Event = $mod.ProcA;',
  19467. '$mod.ProcA = $mod.FuncB;',
  19468. '$mod.FuncB = $mod.FuncB;',
  19469. '$mod.FuncB = $mod.ProcA;',
  19470. '$mod.FuncB = $mod.Getter;',
  19471. '$mod.ProcA = $mod.p;',
  19472. '$mod.FuncB = $mod.p;',
  19473. '$mod.Getter = $mod.p;',
  19474. '$mod.p = $mod.Notify;',
  19475. '$mod.p = $mod.Notify;',
  19476. '$mod.p = $mod.ProcA;',
  19477. '$mod.p = $mod.ProcA;',
  19478. '$mod.p = $mod.FuncB;',
  19479. '$mod.p = $mod.FuncB;',
  19480. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  19481. '']));
  19482. end;
  19483. procedure TTestModule.TestProcType_PassProcToUntyped;
  19484. begin
  19485. StartProgram(false);
  19486. Add([
  19487. 'type',
  19488. ' TEvent = procedure of object;',
  19489. ' TFunc = function: longint;',
  19490. 'procedure DoIt(); varargs; begin end;',
  19491. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  19492. 'var',
  19493. ' Event: tevent;',
  19494. ' Func: TFunc;',
  19495. 'begin',
  19496. ' doit(event,func);',
  19497. ' dosome(event,event,event);',
  19498. ' dosome(func,func,func);',
  19499. '']);
  19500. ConvertProgram;
  19501. CheckSource('TestProcType_PassProcToUntyped',
  19502. LinesToStr([ // statements
  19503. 'this.DoIt = function () {',
  19504. '};',
  19505. 'this.DoSome = function (a, b, p) {',
  19506. '};',
  19507. 'this.Event = null;',
  19508. 'this.Func = null;',
  19509. '']),
  19510. LinesToStr([ // $mod.$main
  19511. '$mod.DoIt($mod.Event, $mod.Func);',
  19512. '$mod.DoSome($mod.Event, {',
  19513. ' p: $mod,',
  19514. ' get: function () {',
  19515. ' return this.p.Event;',
  19516. ' },',
  19517. ' set: function (v) {',
  19518. ' this.p.Event = v;',
  19519. ' }',
  19520. '}, $mod.Event);',
  19521. '$mod.DoSome($mod.Func, {',
  19522. ' p: $mod,',
  19523. ' get: function () {',
  19524. ' return this.p.Func;',
  19525. ' },',
  19526. ' set: function (v) {',
  19527. ' this.p.Func = v;',
  19528. ' }',
  19529. '}, $mod.Func);',
  19530. '']));
  19531. end;
  19532. procedure TTestModule.TestProcType_PassProcToArray;
  19533. begin
  19534. StartProgram(false);
  19535. Add([
  19536. 'type',
  19537. ' TFunc = function: longint;',
  19538. ' TArrFunc = array of TFunc;',
  19539. 'procedure DoIt(Arr: TArrFunc); begin end;',
  19540. 'function GetIt: longint; begin end;',
  19541. 'var',
  19542. ' Func: tfunc;',
  19543. 'begin',
  19544. ' doit([]);',
  19545. ' doit([@GetIt]);',
  19546. ' doit([Func]);',
  19547. '']);
  19548. ConvertProgram;
  19549. CheckSource('TestProcType_PassProcToArray',
  19550. LinesToStr([ // statements
  19551. 'this.DoIt = function (Arr) {',
  19552. '};',
  19553. 'this.GetIt = function () {',
  19554. ' var Result = 0;',
  19555. ' return Result;',
  19556. '};',
  19557. 'this.Func = null;',
  19558. '']),
  19559. LinesToStr([ // $mod.$main
  19560. '$mod.DoIt([]);',
  19561. '$mod.DoIt([$mod.GetIt]);',
  19562. '$mod.DoIt([$mod.Func]);',
  19563. '']));
  19564. end;
  19565. procedure TTestModule.TestPointer;
  19566. begin
  19567. StartProgram(false);
  19568. Add(['type',
  19569. ' TObject = class end;',
  19570. ' TClass = class of TObject;',
  19571. ' TArrInt = array of longint;',
  19572. 'const',
  19573. ' n = nil;',
  19574. 'var',
  19575. ' v: jsvalue;',
  19576. ' Obj: tobject;',
  19577. ' C: tclass;',
  19578. ' a: tarrint;',
  19579. ' p: Pointer = nil;',
  19580. ' s: string;',
  19581. 'begin',
  19582. ' p:=p;',
  19583. ' p:=nil;',
  19584. ' if p=nil then;',
  19585. ' if nil=p then;',
  19586. ' if Assigned(p) then;',
  19587. ' p:=Pointer(v);',
  19588. ' p:=obj;',
  19589. ' p:=c;',
  19590. ' p:=a;',
  19591. ' p:=tobject;',
  19592. ' obj:=TObject(p);',
  19593. ' c:=TClass(p);',
  19594. ' a:=TArrInt(p);',
  19595. ' p:=n;',
  19596. ' p:=Pointer(a);',
  19597. ' p:=pointer(s);',
  19598. ' s:=string(p);',
  19599. '']);
  19600. ConvertProgram;
  19601. CheckSource('TestPointer',
  19602. LinesToStr([ // statements
  19603. 'rtl.createClass($mod, "TObject", null, function () {',
  19604. ' this.$init = function () {',
  19605. ' };',
  19606. ' this.$final = function () {',
  19607. ' };',
  19608. '});',
  19609. 'this.n = null;',
  19610. 'this.v = undefined;',
  19611. 'this.Obj = null;',
  19612. 'this.C = null;',
  19613. 'this.a = [];',
  19614. 'this.p = null;',
  19615. 'this.s = "";',
  19616. '']),
  19617. LinesToStr([ // $mod.$main
  19618. '$mod.p = $mod.p;',
  19619. '$mod.p = null;',
  19620. 'if ($mod.p === null) ;',
  19621. 'if (null === $mod.p) ;',
  19622. 'if ($mod.p != null) ;',
  19623. '$mod.p = $mod.v;',
  19624. '$mod.p = $mod.Obj;',
  19625. '$mod.p = $mod.C;',
  19626. '$mod.p = $mod.a;',
  19627. '$mod.p = $mod.TObject;',
  19628. '$mod.Obj = $mod.p;',
  19629. '$mod.C = $mod.p;',
  19630. '$mod.a = $mod.p;',
  19631. '$mod.p = null;',
  19632. '$mod.p = $mod.a;',
  19633. '$mod.p = $mod.s;',
  19634. '$mod.s = $mod.p;',
  19635. '']));
  19636. end;
  19637. procedure TTestModule.TestPointer_Proc;
  19638. begin
  19639. StartProgram(false);
  19640. Add('type');
  19641. Add(' TObject = class');
  19642. Add(' procedure DoIt; virtual; abstract;');
  19643. Add(' end;');
  19644. Add('procedure DoSome; begin end;');
  19645. Add('var');
  19646. Add(' o: TObject;');
  19647. Add(' p: Pointer;');
  19648. Add('begin');
  19649. Add(' p:=@DoSome;');
  19650. Add(' p:[email protected];');
  19651. ConvertProgram;
  19652. CheckSource('TestPointer_Proc',
  19653. LinesToStr([ // statements
  19654. 'rtl.createClass($mod, "TObject", null, function () {',
  19655. ' this.$init = function () {',
  19656. ' };',
  19657. ' this.$final = function () {',
  19658. ' };',
  19659. '});',
  19660. 'this.DoSome = function () {',
  19661. '};',
  19662. 'this.o = null;',
  19663. 'this.p = null;',
  19664. '']),
  19665. LinesToStr([ // $mod.$main
  19666. '$mod.p = $mod.DoSome;',
  19667. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  19668. '']));
  19669. end;
  19670. procedure TTestModule.TestPointer_AssignRecordFail;
  19671. begin
  19672. StartProgram(false);
  19673. Add('type');
  19674. Add(' TRec = record end;');
  19675. Add('var');
  19676. Add(' p: Pointer;');
  19677. Add(' r: TRec;');
  19678. Add('begin');
  19679. Add(' p:=r;');
  19680. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  19681. nIncompatibleTypesGotExpected);
  19682. ConvertProgram;
  19683. end;
  19684. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  19685. begin
  19686. StartProgram(false);
  19687. Add('type');
  19688. Add(' TArr = array[boolean] of longint;');
  19689. Add('var');
  19690. Add(' p: Pointer;');
  19691. Add(' a: TArr;');
  19692. Add('begin');
  19693. Add(' p:=a;');
  19694. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  19695. nIncompatibleTypesGotExpected);
  19696. ConvertProgram;
  19697. end;
  19698. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  19699. begin
  19700. StartProgram(false);
  19701. Add([
  19702. 'procedure DoIt(args: array of jsvalue); begin end;',
  19703. 'procedure DoAll; varargs; begin end;',
  19704. 'var',
  19705. ' v: jsvalue;',
  19706. 'begin',
  19707. ' DoIt([pointer(v)]);',
  19708. ' DoAll(pointer(v));',
  19709. '']);
  19710. ConvertProgram;
  19711. CheckSource('TestPointer_TypeCastJSValueToPointer',
  19712. LinesToStr([ // statements
  19713. 'this.DoIt = function (args) {',
  19714. '};',
  19715. 'this.DoAll = function () {',
  19716. '};',
  19717. 'this.v = undefined;',
  19718. '']),
  19719. LinesToStr([ // $mod.$main
  19720. '$mod.DoIt([$mod.v]);',
  19721. '$mod.DoAll($mod.v);',
  19722. '']));
  19723. end;
  19724. procedure TTestModule.TestPointer_NonRecordFail;
  19725. begin
  19726. StartProgram(false);
  19727. Add([
  19728. 'type',
  19729. ' p = ^longint;',
  19730. 'begin',
  19731. '']);
  19732. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  19733. ConvertProgram;
  19734. end;
  19735. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  19736. begin
  19737. StartProgram(false);
  19738. Add([
  19739. 'procedure DoIt(p: ^longint); begin end;',
  19740. 'begin',
  19741. '']);
  19742. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  19743. ConvertProgram;
  19744. end;
  19745. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  19746. begin
  19747. StartProgram(false);
  19748. Add([
  19749. 'var p: ^longint;',
  19750. 'begin',
  19751. '']);
  19752. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  19753. ConvertProgram;
  19754. end;
  19755. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  19756. begin
  19757. StartProgram(false);
  19758. Add([
  19759. 'function DoIt: ^longint; begin end;',
  19760. 'begin',
  19761. '']);
  19762. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  19763. ConvertProgram;
  19764. end;
  19765. procedure TTestModule.TestPointer_AddrOperatorFail;
  19766. begin
  19767. StartProgram(false);
  19768. Add([
  19769. 'var i: longint;',
  19770. 'begin',
  19771. ' if @i=nil then ;',
  19772. '']);
  19773. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  19774. ConvertProgram;
  19775. end;
  19776. procedure TTestModule.TestPointer_ArrayParamsFail;
  19777. begin
  19778. StartProgram(false);
  19779. Add([
  19780. 'var',
  19781. ' p: Pointer;',
  19782. 'begin',
  19783. ' p:=p[1];',
  19784. '']);
  19785. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  19786. ConvertProgram;
  19787. end;
  19788. procedure TTestModule.TestPointer_PointerAddFail;
  19789. begin
  19790. StartProgram(false);
  19791. Add([
  19792. 'var',
  19793. ' p: Pointer;',
  19794. 'begin',
  19795. ' p:=p+1;',
  19796. '']);
  19797. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  19798. ConvertProgram;
  19799. end;
  19800. procedure TTestModule.TestPointer_IncPointerFail;
  19801. begin
  19802. StartProgram(false);
  19803. Add([
  19804. 'var',
  19805. ' p: Pointer;',
  19806. 'begin',
  19807. ' inc(p,1);',
  19808. '']);
  19809. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Pointer", expected "integer"',
  19810. nIncompatibleTypeArgNo);
  19811. ConvertProgram;
  19812. end;
  19813. procedure TTestModule.TestPointer_Record;
  19814. begin
  19815. StartProgram(false);
  19816. Add([
  19817. 'type',
  19818. ' TRec = record x: longint; end;',
  19819. ' PRec = ^TRec;',
  19820. 'var',
  19821. ' r: TRec;',
  19822. ' p: PRec;',
  19823. ' q: ^TRec;',
  19824. ' Ptr: pointer;',
  19825. 'begin',
  19826. ' new(p);',
  19827. ' p:=@r;',
  19828. ' r:=p^;',
  19829. ' r.x:=p^.x;',
  19830. ' p^.x:=r.x;',
  19831. ' if p^.x=3 then ;',
  19832. ' if 4=p^.x then ;',
  19833. ' dispose(p);',
  19834. ' new(q);',
  19835. ' dispose(q);',
  19836. ' Ptr:=p;',
  19837. ' p:=PRec(ptr);',
  19838. '']);
  19839. ConvertProgram;
  19840. CheckSource('TestPointer_Record',
  19841. LinesToStr([ // statements
  19842. 'rtl.recNewT($mod, "TRec", function () {',
  19843. ' this.x = 0;',
  19844. ' this.$eq = function (b) {',
  19845. ' return this.x === b.x;',
  19846. ' };',
  19847. ' this.$assign = function (s) {',
  19848. ' this.x = s.x;',
  19849. ' return this;',
  19850. ' };',
  19851. '});',
  19852. 'this.r = $mod.TRec.$new();',
  19853. 'this.p = null;',
  19854. 'this.q = null;',
  19855. 'this.Ptr = null;',
  19856. '']),
  19857. LinesToStr([ // $mod.$main
  19858. '$mod.p = $mod.TRec.$new();',
  19859. '$mod.p = $mod.r;',
  19860. '$mod.r.$assign($mod.p);',
  19861. '$mod.r.x = $mod.p.x;',
  19862. '$mod.p.x = $mod.r.x;',
  19863. 'if ($mod.p.x === 3) ;',
  19864. 'if (4 === $mod.p.x) ;',
  19865. '$mod.p = null;',
  19866. '$mod.q = $mod.TRec.$new();',
  19867. '$mod.q = null;',
  19868. '$mod.Ptr = $mod.p;',
  19869. '$mod.p = $mod.Ptr;',
  19870. '']));
  19871. end;
  19872. procedure TTestModule.TestPointer_RecordArg;
  19873. begin
  19874. StartProgram(false);
  19875. Add([
  19876. '{$modeswitch autoderef}',
  19877. 'type',
  19878. ' TRec = record x: longint; end;',
  19879. ' PRec = ^TRec;',
  19880. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  19881. 'begin',
  19882. ' a.x:=a.x;',
  19883. ' a^.x:=a^.x;',
  19884. ' with a^ do',
  19885. ' x:=x;',
  19886. 'end;',
  19887. 'function GetIt(p: PRec): PRec;',
  19888. 'begin',
  19889. ' p.x:=p.x;',
  19890. ' p^.x:=p^.x;',
  19891. ' with p^ do',
  19892. ' x:=x;',
  19893. 'end;',
  19894. 'var',
  19895. ' r: TRec;',
  19896. ' p: PRec;',
  19897. 'begin',
  19898. ' p:=GetIt(p);',
  19899. ' p^:=GetIt(@r)^;',
  19900. ' DoIt(p,p,p);',
  19901. ' DoIt(@r,p,p);',
  19902. '']);
  19903. ConvertProgram;
  19904. CheckSource('TestPointer_Record',
  19905. LinesToStr([ // statements
  19906. 'rtl.recNewT($mod, "TRec", function () {',
  19907. ' this.x = 0;',
  19908. ' this.$eq = function (b) {',
  19909. ' return this.x === b.x;',
  19910. ' };',
  19911. ' this.$assign = function (s) {',
  19912. ' this.x = s.x;',
  19913. ' return this;',
  19914. ' };',
  19915. '});',
  19916. 'this.DoIt = function (a, b, c) {',
  19917. ' var Result = $mod.TRec.$new();',
  19918. ' a.x = a.x;',
  19919. ' a.x = a.x;',
  19920. ' a.x = a.x;',
  19921. ' return Result;',
  19922. '};',
  19923. 'this.GetIt = function (p) {',
  19924. ' var Result = null;',
  19925. ' p.x = p.x;',
  19926. ' p.x = p.x;',
  19927. ' p.x = p.x;',
  19928. ' return Result;',
  19929. '};',
  19930. 'this.r = $mod.TRec.$new();',
  19931. 'this.p = null;',
  19932. '']),
  19933. LinesToStr([ // $mod.$main
  19934. '$mod.p = $mod.GetIt($mod.p);',
  19935. '$mod.p.$assign($mod.GetIt($mod.r));',
  19936. '$mod.DoIt($mod.p, {',
  19937. ' p: $mod,',
  19938. ' get: function () {',
  19939. ' return this.p.p;',
  19940. ' },',
  19941. ' set: function (v) {',
  19942. ' this.p.p = v;',
  19943. ' }',
  19944. '}, {',
  19945. ' p: $mod,',
  19946. ' get: function () {',
  19947. ' return this.p.p;',
  19948. ' },',
  19949. ' set: function (v) {',
  19950. ' this.p.p = v;',
  19951. ' }',
  19952. '});',
  19953. '$mod.DoIt($mod.r, {',
  19954. ' p: $mod,',
  19955. ' get: function () {',
  19956. ' return this.p.p;',
  19957. ' },',
  19958. ' set: function (v) {',
  19959. ' this.p.p = v;',
  19960. ' }',
  19961. '}, {',
  19962. ' p: $mod,',
  19963. ' get: function () {',
  19964. ' return this.p.p;',
  19965. ' },',
  19966. ' set: function (v) {',
  19967. ' this.p.p = v;',
  19968. ' }',
  19969. '});',
  19970. '']));
  19971. end;
  19972. procedure TTestModule.TestJSValue_AssignToJSValue;
  19973. begin
  19974. StartProgram(false);
  19975. Add('var');
  19976. Add(' v: jsvalue;');
  19977. Add(' i: longint;');
  19978. Add(' s: string;');
  19979. Add(' b: boolean;');
  19980. Add(' d: double;');
  19981. Add(' p: pointer;');
  19982. Add('begin');
  19983. Add(' v:=v;');
  19984. Add(' v:=1;');
  19985. Add(' v:=i;');
  19986. Add(' v:='''';');
  19987. Add(' v:=''c'';');
  19988. Add(' v:=''foo'';');
  19989. Add(' v:=s;');
  19990. Add(' v:=false;');
  19991. Add(' v:=true;');
  19992. Add(' v:=b;');
  19993. Add(' v:=0.1;');
  19994. Add(' v:=d;');
  19995. Add(' v:=nil;');
  19996. Add(' v:=p;');
  19997. ConvertProgram;
  19998. CheckSource('TestJSValue_AssignToJSValue',
  19999. LinesToStr([ // statements
  20000. 'this.v = undefined;',
  20001. 'this.i = 0;',
  20002. 'this.s = "";',
  20003. 'this.b = false;',
  20004. 'this.d = 0.0;',
  20005. 'this.p = null;',
  20006. '']),
  20007. LinesToStr([ // $mod.$main
  20008. '$mod.v = $mod.v;',
  20009. '$mod.v = 1;',
  20010. '$mod.v = $mod.i;',
  20011. '$mod.v = "";',
  20012. '$mod.v = "c";',
  20013. '$mod.v = "foo";',
  20014. '$mod.v = $mod.s;',
  20015. '$mod.v = false;',
  20016. '$mod.v = true;',
  20017. '$mod.v = $mod.b;',
  20018. '$mod.v = 0.1;',
  20019. '$mod.v = $mod.d;',
  20020. '$mod.v = null;',
  20021. '$mod.v = $mod.p;',
  20022. '']));
  20023. end;
  20024. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  20025. begin
  20026. StartProgram(false);
  20027. Add('type');
  20028. Add(' integer = longint;');
  20029. Add(' TYesNo = boolean;');
  20030. Add(' TFloat = double;');
  20031. Add(' TCaption = string;');
  20032. Add(' TChar = char;');
  20033. Add('var');
  20034. Add(' v: jsvalue;');
  20035. Add(' i: integer;');
  20036. Add(' s: TCaption;');
  20037. Add(' b: TYesNo;');
  20038. Add(' d: TFloat;');
  20039. Add(' c: char;');
  20040. Add('begin');
  20041. Add(' i:=longint(v);');
  20042. Add(' i:=integer(v);');
  20043. Add(' s:=string(v);');
  20044. Add(' s:=TCaption(v);');
  20045. Add(' b:=boolean(v);');
  20046. Add(' b:=TYesNo(v);');
  20047. Add(' d:=double(v);');
  20048. Add(' d:=TFloat(v);');
  20049. Add(' c:=char(v);');
  20050. Add(' c:=TChar(v);');
  20051. ConvertProgram;
  20052. CheckSource('TestJSValue_TypeCastToBaseType',
  20053. LinesToStr([ // statements
  20054. 'this.v = undefined;',
  20055. 'this.i = 0;',
  20056. 'this.s = "";',
  20057. 'this.b = false;',
  20058. 'this.d = 0.0;',
  20059. 'this.c = "";',
  20060. '']),
  20061. LinesToStr([ // $mod.$main
  20062. '$mod.i = Math.floor($mod.v);',
  20063. '$mod.i = Math.floor($mod.v);',
  20064. '$mod.s = "" + $mod.v;',
  20065. '$mod.s = "" + $mod.v;',
  20066. '$mod.b = !($mod.v == false);',
  20067. '$mod.b = !($mod.v == false);',
  20068. '$mod.d = rtl.getNumber($mod.v);',
  20069. '$mod.d = rtl.getNumber($mod.v);',
  20070. '$mod.c = rtl.getChar($mod.v);',
  20071. '$mod.c = rtl.getChar($mod.v);',
  20072. '']));
  20073. end;
  20074. procedure TTestModule.TestJSValue_Equal;
  20075. begin
  20076. StartProgram(false);
  20077. Add('type');
  20078. Add(' integer = longint;');
  20079. Add(' TYesNo = boolean;');
  20080. Add(' TFloat = double;');
  20081. Add(' TCaption = string;');
  20082. Add(' TChar = char;');
  20083. Add(' TMulti = JSValue;');
  20084. Add('var');
  20085. Add(' v: jsvalue;');
  20086. Add(' i: integer;');
  20087. Add(' s: TCaption;');
  20088. Add(' b: TYesNo;');
  20089. Add(' d: TFloat;');
  20090. Add(' c: char;');
  20091. Add(' m: TMulti;');
  20092. Add('begin');
  20093. Add(' b:=v=v;');
  20094. Add(' b:=v<>v;');
  20095. Add(' b:=v=1;');
  20096. Add(' b:=v<>1;');
  20097. Add(' b:=2=v;');
  20098. Add(' b:=2<>v;');
  20099. Add(' b:=v=i;');
  20100. Add(' b:=i=v;');
  20101. Add(' b:=v=nil;');
  20102. Add(' b:=nil=v;');
  20103. Add(' b:=v=false;');
  20104. Add(' b:=true=v;');
  20105. Add(' b:=v=b;');
  20106. Add(' b:=b=v;');
  20107. Add(' b:=v=s;');
  20108. Add(' b:=s=v;');
  20109. Add(' b:=v=''foo'';');
  20110. Add(' b:=''''=v;');
  20111. Add(' b:=v=d;');
  20112. Add(' b:=d=v;');
  20113. Add(' b:=v=3.4;');
  20114. Add(' b:=5.6=v;');
  20115. Add(' b:=v=c;');
  20116. Add(' b:=c=v;');
  20117. Add(' b:=m=m;');
  20118. Add(' b:=v=m;');
  20119. Add(' b:=m=v;');
  20120. ConvertProgram;
  20121. CheckSource('TestJSValue_Equal',
  20122. LinesToStr([ // statements
  20123. 'this.v = undefined;',
  20124. 'this.i = 0;',
  20125. 'this.s = "";',
  20126. 'this.b = false;',
  20127. 'this.d = 0.0;',
  20128. 'this.c = "";',
  20129. 'this.m = undefined;',
  20130. '']),
  20131. LinesToStr([ // $mod.$main
  20132. '$mod.b = $mod.v == $mod.v;',
  20133. '$mod.b = $mod.v != $mod.v;',
  20134. '$mod.b = $mod.v == 1;',
  20135. '$mod.b = $mod.v != 1;',
  20136. '$mod.b = 2 == $mod.v;',
  20137. '$mod.b = 2 != $mod.v;',
  20138. '$mod.b = $mod.v == $mod.i;',
  20139. '$mod.b = $mod.i == $mod.v;',
  20140. '$mod.b = $mod.v == null;',
  20141. '$mod.b = null == $mod.v;',
  20142. '$mod.b = $mod.v == false;',
  20143. '$mod.b = true == $mod.v;',
  20144. '$mod.b = $mod.v == $mod.b;',
  20145. '$mod.b = $mod.b == $mod.v;',
  20146. '$mod.b = $mod.v == $mod.s;',
  20147. '$mod.b = $mod.s == $mod.v;',
  20148. '$mod.b = $mod.v == "foo";',
  20149. '$mod.b = "" == $mod.v;',
  20150. '$mod.b = $mod.v == $mod.d;',
  20151. '$mod.b = $mod.d == $mod.v;',
  20152. '$mod.b = $mod.v == 3.4;',
  20153. '$mod.b = 5.6 == $mod.v;',
  20154. '$mod.b = $mod.v == $mod.c;',
  20155. '$mod.b = $mod.c == $mod.v;',
  20156. '$mod.b = $mod.m == $mod.m;',
  20157. '$mod.b = $mod.v == $mod.m;',
  20158. '$mod.b = $mod.m == $mod.v;',
  20159. '']));
  20160. end;
  20161. procedure TTestModule.TestJSValue_If;
  20162. begin
  20163. StartProgram(false);
  20164. Add([
  20165. 'var',
  20166. ' v: jsvalue;',
  20167. 'begin',
  20168. ' if v then ;',
  20169. ' while v do ;',
  20170. ' repeat until v;',
  20171. '']);
  20172. ConvertProgram;
  20173. CheckSource('TestJSValue_If',
  20174. LinesToStr([ // statements
  20175. 'this.v = undefined;',
  20176. '']),
  20177. LinesToStr([ // $mod.$main
  20178. 'if ($mod.v) ;',
  20179. 'while($mod.v){',
  20180. '};',
  20181. 'do{',
  20182. '} while(!$mod.v);',
  20183. '']));
  20184. end;
  20185. procedure TTestModule.TestJSValue_Not;
  20186. begin
  20187. StartProgram(false);
  20188. Add([
  20189. 'var',
  20190. ' v: jsvalue;',
  20191. ' b: boolean;',
  20192. 'begin',
  20193. ' b:=not v;',
  20194. ' if not v then ;',
  20195. ' while not v do ;',
  20196. ' repeat until not v;',
  20197. '']);
  20198. ConvertProgram;
  20199. CheckSource('TestJSValue_If',
  20200. LinesToStr([ // statements
  20201. 'this.v = undefined;',
  20202. 'this.b = false;',
  20203. '']),
  20204. LinesToStr([ // $mod.$main
  20205. '$mod.b=!$mod.v;',
  20206. 'if (!$mod.v) ;',
  20207. 'while(!$mod.v){',
  20208. '};',
  20209. 'do{',
  20210. '} while($mod.v);',
  20211. '']));
  20212. end;
  20213. procedure TTestModule.TestJSValue_Enum;
  20214. begin
  20215. StartProgram(false);
  20216. Add('type');
  20217. Add(' TColor = (red, blue);');
  20218. Add(' TRedBlue = TColor;');
  20219. Add('var');
  20220. Add(' v: jsvalue;');
  20221. Add(' e: TColor;');
  20222. Add('begin');
  20223. Add(' v:=e;');
  20224. Add(' v:=TColor(e);');
  20225. Add(' v:=TRedBlue(e);');
  20226. Add(' e:=TColor(v);');
  20227. Add(' e:=TRedBlue(v);');
  20228. ConvertProgram;
  20229. CheckSource('TestJSValue_Enum',
  20230. LinesToStr([ // statements
  20231. 'this.TColor = {',
  20232. ' "0": "red",',
  20233. ' red: 0,',
  20234. ' "1": "blue",',
  20235. ' blue: 1',
  20236. '};',
  20237. 'this.v = undefined;',
  20238. 'this.e = 0;',
  20239. '']),
  20240. LinesToStr([ // $mod.$main
  20241. '$mod.v = $mod.e;',
  20242. '$mod.v = $mod.e;',
  20243. '$mod.v = $mod.e;',
  20244. '$mod.e = $mod.v;',
  20245. '$mod.e = $mod.v;',
  20246. '']));
  20247. end;
  20248. procedure TTestModule.TestJSValue_ClassInstance;
  20249. begin
  20250. StartProgram(false);
  20251. Add([
  20252. 'type',
  20253. ' TObject = class',
  20254. ' end;',
  20255. ' TBirdObject = TObject;',
  20256. 'var',
  20257. ' v: jsvalue;',
  20258. ' o: TObject;',
  20259. 'begin',
  20260. ' v:=o;',
  20261. ' v:=TObject(o);',
  20262. ' v:=TBirdObject(o);',
  20263. ' o:=TObject(v);',
  20264. ' o:=TBirdObject(v);',
  20265. ' if v is TObject then ;',
  20266. '']);
  20267. ConvertProgram;
  20268. CheckSource('TestJSValue_ClassInstance',
  20269. LinesToStr([ // statements
  20270. 'rtl.createClass($mod, "TObject", null, function () {',
  20271. ' this.$init = function () {',
  20272. ' };',
  20273. ' this.$final = function () {',
  20274. ' };',
  20275. '});',
  20276. 'this.v = undefined;',
  20277. 'this.o = null;',
  20278. '']),
  20279. LinesToStr([ // $mod.$main
  20280. '$mod.v = $mod.o;',
  20281. '$mod.v = $mod.o;',
  20282. '$mod.v = $mod.o;',
  20283. '$mod.o = rtl.getObject($mod.v);',
  20284. '$mod.o = rtl.getObject($mod.v);',
  20285. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  20286. '']));
  20287. end;
  20288. procedure TTestModule.TestJSValue_ClassOf;
  20289. begin
  20290. StartProgram(false);
  20291. Add([
  20292. 'type',
  20293. ' TClass = class of TObject;',
  20294. ' TObject = class',
  20295. ' end;',
  20296. ' TBirds = class of TBird;',
  20297. ' TBird = class(TObject) end;',
  20298. 'var',
  20299. ' v: jsvalue;',
  20300. ' c: TClass;',
  20301. 'begin',
  20302. ' v:=c;',
  20303. ' v:=TObject;',
  20304. ' v:=TClass(c);',
  20305. ' v:=TBirds(c);',
  20306. ' c:=TClass(v);',
  20307. ' c:=TBirds(v);',
  20308. ' if v is TClass then ;',
  20309. '']);
  20310. ConvertProgram;
  20311. CheckSource('TestJSValue_ClassOf',
  20312. LinesToStr([ // statements
  20313. 'rtl.createClass($mod, "TObject", null, function () {',
  20314. ' this.$init = function () {',
  20315. ' };',
  20316. ' this.$final = function () {',
  20317. ' };',
  20318. '});',
  20319. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20320. '});',
  20321. 'this.v = undefined;',
  20322. 'this.c = null;',
  20323. '']),
  20324. LinesToStr([ // $mod.$main
  20325. '$mod.v = $mod.c;',
  20326. '$mod.v = $mod.TObject;',
  20327. '$mod.v = $mod.c;',
  20328. '$mod.v = $mod.c;',
  20329. '$mod.c = rtl.getObject($mod.v);',
  20330. '$mod.c = rtl.getObject($mod.v);',
  20331. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  20332. '']));
  20333. end;
  20334. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  20335. begin
  20336. StartProgram(false);
  20337. Add([
  20338. 'type',
  20339. ' integer = longint;',
  20340. ' TArray = array of JSValue;',
  20341. ' TArrgh = tarray;',
  20342. ' TArrInt = array of integer;',
  20343. 'var',
  20344. ' v: jsvalue;',
  20345. ' TheArray: tarray = (1,''2'');',
  20346. ' Arr: tarrgh;',
  20347. ' i: integer;',
  20348. ' ArrInt: tarrint;',
  20349. 'begin',
  20350. ' arr:=thearray;',
  20351. ' thearray:=arr;',
  20352. ' setlength(arr,2);',
  20353. ' setlength(thearray,3);',
  20354. ' arr[4]:=v;',
  20355. ' arr[5]:=length(thearray);',
  20356. ' arr[6]:=nil;',
  20357. ' arr[7]:=thearray[8];',
  20358. ' arr[low(arr)]:=high(thearray);',
  20359. ' arr:=arrint;',
  20360. ' arrInt:=tarrint(arr);',
  20361. ' if TheArray = nil then ;',
  20362. ' if nil = TheArray then ;',
  20363. ' if TheArray <> nil then ;',
  20364. ' if nil <> TheArray then ;',
  20365. '']);
  20366. ConvertProgram;
  20367. CheckSource('TestJSValue_ArrayOfJSValue',
  20368. LinesToStr([ // statements
  20369. 'this.v = undefined;',
  20370. 'this.TheArray = [1, "2"];',
  20371. 'this.Arr = [];',
  20372. 'this.i = 0;',
  20373. 'this.ArrInt = [];',
  20374. '']),
  20375. LinesToStr([ // $mod.$main
  20376. '$mod.Arr = $mod.TheArray;',
  20377. '$mod.TheArray = $mod.Arr;',
  20378. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  20379. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  20380. '$mod.Arr[4] = $mod.v;',
  20381. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  20382. '$mod.Arr[6] = null;',
  20383. '$mod.Arr[7] = $mod.TheArray[8];',
  20384. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  20385. '$mod.Arr = $mod.ArrInt;',
  20386. '$mod.ArrInt = $mod.Arr;',
  20387. 'if (rtl.length($mod.TheArray) === 0) ;',
  20388. 'if (rtl.length($mod.TheArray) === 0) ;',
  20389. 'if (rtl.length($mod.TheArray) > 0) ;',
  20390. 'if (rtl.length($mod.TheArray) > 0) ;',
  20391. '']));
  20392. end;
  20393. procedure TTestModule.TestJSValue_ArrayLit;
  20394. begin
  20395. StartProgram(false);
  20396. Add([
  20397. 'type',
  20398. ' TFlag = (big,small);',
  20399. ' TArray = array of JSValue;',
  20400. ' TObject = class end;',
  20401. ' TClass = class of TObject;',
  20402. 'var',
  20403. ' v: jsvalue;',
  20404. ' a: TArray;',
  20405. ' o: TObject;',
  20406. 'begin',
  20407. ' a:=[];',
  20408. ' a:=[1];',
  20409. ' a:=[1,2];',
  20410. ' a:=[big];',
  20411. ' a:=[1,big];',
  20412. ' a:=[o,nil];',
  20413. '']);
  20414. ConvertProgram;
  20415. CheckSource('TestJSValue_ArrayLit',
  20416. LinesToStr([ // statements
  20417. 'this.TFlag = {',
  20418. ' "0": "big",',
  20419. ' big: 0,',
  20420. ' "1": "small",',
  20421. ' small: 1',
  20422. '};',
  20423. 'rtl.createClass($mod, "TObject", null, function () {',
  20424. ' this.$init = function () {',
  20425. ' };',
  20426. ' this.$final = function () {',
  20427. ' };',
  20428. '});',
  20429. 'this.v = undefined;',
  20430. 'this.a = [];',
  20431. 'this.o = null;',
  20432. '']),
  20433. LinesToStr([ // $mod.$main
  20434. '$mod.a = [];',
  20435. '$mod.a = [1];',
  20436. '$mod.a = [1, 2];',
  20437. '$mod.a = [$mod.TFlag.big];',
  20438. '$mod.a = [1, $mod.TFlag.big];',
  20439. '$mod.a = [$mod.o, null];',
  20440. '']));
  20441. end;
  20442. procedure TTestModule.TestJSValue_Params;
  20443. begin
  20444. StartProgram(false);
  20445. Add('type');
  20446. Add(' integer = longint;');
  20447. Add(' TYesNo = boolean;');
  20448. Add(' TFloat = double;');
  20449. Add(' TCaption = string;');
  20450. Add(' TChar = char;');
  20451. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  20452. Add('var');
  20453. Add(' l: jsvalue;');
  20454. Add('begin');
  20455. Add(' a:=a;');
  20456. Add(' l:=b;');
  20457. Add(' c:=c;');
  20458. Add(' d:=d;');
  20459. Add(' Result:=l;');
  20460. Add('end;');
  20461. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  20462. Add('var');
  20463. Add(' v: jsvalue;');
  20464. Add(' i: integer;');
  20465. Add(' b: TYesNo;');
  20466. Add(' d: TFloat;');
  20467. Add(' s: TCaption;');
  20468. Add(' c: TChar;');
  20469. Add('begin');
  20470. Add(' v:=doit(v,v,v,v);');
  20471. Add(' i:=integer(dosome(i,i));');
  20472. Add(' b:=TYesNo(dosome(b,b));');
  20473. Add(' d:=TFloat(dosome(d,d));');
  20474. Add(' s:=TCaption(dosome(s,s));');
  20475. Add(' c:=TChar(dosome(c,c));');
  20476. ConvertProgram;
  20477. CheckSource('TestJSValue_Params',
  20478. LinesToStr([ // statements
  20479. 'this.DoIt = function (a, b, c, d) {',
  20480. ' var Result = undefined;',
  20481. ' var l = undefined;',
  20482. ' a = a;',
  20483. ' l = b;',
  20484. ' c.set(c.get());',
  20485. ' d.set(d.get());',
  20486. ' Result = l;',
  20487. ' return Result;',
  20488. '};',
  20489. 'this.DoSome = function (a, b) {',
  20490. ' var Result = undefined;',
  20491. ' return Result;',
  20492. '};',
  20493. 'this.v = undefined;',
  20494. 'this.i = 0;',
  20495. 'this.b = false;',
  20496. 'this.d = 0.0;',
  20497. 'this.s = "";',
  20498. 'this.c = "";',
  20499. '']),
  20500. LinesToStr([ // $mod.$main
  20501. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  20502. ' p: $mod,',
  20503. ' get: function () {',
  20504. ' return this.p.v;',
  20505. ' },',
  20506. ' set: function (v) {',
  20507. ' this.p.v = v;',
  20508. ' }',
  20509. '}, {',
  20510. ' p: $mod,',
  20511. ' get: function () {',
  20512. ' return this.p.v;',
  20513. ' },',
  20514. ' set: function (v) {',
  20515. ' this.p.v = v;',
  20516. ' }',
  20517. '});',
  20518. '$mod.i = Math.floor($mod.DoSome($mod.i, $mod.i));',
  20519. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  20520. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  20521. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  20522. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  20523. '']));
  20524. end;
  20525. procedure TTestModule.TestJSValue_UntypedParam;
  20526. begin
  20527. StartProgram(false);
  20528. Add('function DoIt(const a; var b; out c): jsvalue;');
  20529. Add('begin');
  20530. Add(' Result:=a;');
  20531. Add(' Result:=b;');
  20532. Add(' Result:=c;');
  20533. Add(' b:=Result;');
  20534. Add(' c:=Result;');
  20535. Add('end;');
  20536. Add('var i: longint;');
  20537. Add('begin');
  20538. Add(' doit(i,i,i);');
  20539. ConvertProgram;
  20540. CheckSource('TestJSValue_UntypedParam',
  20541. LinesToStr([ // statements
  20542. 'this.DoIt = function (a, b, c) {',
  20543. ' var Result = undefined;',
  20544. ' Result = a;',
  20545. ' Result = b.get();',
  20546. ' Result = c.get();',
  20547. ' b.set(Result);',
  20548. ' c.set(Result);',
  20549. ' return Result;',
  20550. '};',
  20551. 'this.i = 0;',
  20552. '']),
  20553. LinesToStr([ // $mod.$main
  20554. '$mod.DoIt($mod.i, {',
  20555. ' p: $mod,',
  20556. ' get: function () {',
  20557. ' return this.p.i;',
  20558. ' },',
  20559. ' set: function (v) {',
  20560. ' this.p.i = v;',
  20561. ' }',
  20562. '}, {',
  20563. ' p: $mod,',
  20564. ' get: function () {',
  20565. ' return this.p.i;',
  20566. ' },',
  20567. ' set: function (v) {',
  20568. ' this.p.i = v;',
  20569. ' }',
  20570. '});',
  20571. '']));
  20572. end;
  20573. procedure TTestModule.TestJSValue_FuncResultType;
  20574. begin
  20575. StartProgram(false);
  20576. Add('type');
  20577. Add(' integer = longint;');
  20578. Add(' TJSValueArray = array of JSValue;');
  20579. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  20580. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  20581. Add('begin');
  20582. Add(' while Compare(P,aList[0])>0 do ;');
  20583. Add('end;');
  20584. Add('var');
  20585. Add(' Compare: TListSortCompare;');
  20586. Add(' V: JSValue;');
  20587. Add(' i: integer;');
  20588. Add('begin');
  20589. Add(' if Compare(V,V)>0 then ;');
  20590. Add(' if Compare(i,i)>1 then ;');
  20591. Add(' if Compare(nil,false)>2 then ;');
  20592. Add(' if Compare(1,true)>3 then ;');
  20593. ConvertProgram;
  20594. CheckSource('TestJSValue_UntypedParam',
  20595. LinesToStr([ // statements
  20596. 'this.Sort = function (P, aList, Compare) {',
  20597. ' while (Compare(P, aList[0]) > 0) {',
  20598. ' };',
  20599. '};',
  20600. 'this.Compare = null;',
  20601. 'this.V = undefined;',
  20602. 'this.i = 0;',
  20603. '']),
  20604. LinesToStr([ // $mod.$main
  20605. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  20606. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  20607. 'if ($mod.Compare(null, false) > 2) ;',
  20608. 'if ($mod.Compare(1, true) > 3) ;',
  20609. '']));
  20610. end;
  20611. procedure TTestModule.TestJSValue_ProcType_Assign;
  20612. begin
  20613. StartProgram(false);
  20614. Add('type');
  20615. Add(' integer = longint;');
  20616. Add(' TObject = class');
  20617. Add(' class function GetGlob: integer;');
  20618. Add(' function Getter: integer;');
  20619. Add(' end;');
  20620. Add('class function TObject.GetGlob: integer;');
  20621. Add('var v1: jsvalue;');
  20622. Add('begin');
  20623. Add(' v1:=@GetGlob;');
  20624. Add(' v1:[email protected];');
  20625. Add('end;');
  20626. Add('function TObject.Getter: integer;');
  20627. Add('var v2: jsvalue;');
  20628. Add('begin');
  20629. Add(' v2:=@Getter;');
  20630. Add(' v2:[email protected];');
  20631. Add(' v2:=@GetGlob;');
  20632. Add(' v2:[email protected];');
  20633. Add('end;');
  20634. Add('function GetIt(i: integer): integer;');
  20635. Add('var v3: jsvalue;');
  20636. Add('begin');
  20637. Add(' v3:=@GetIt;');
  20638. Add('end;');
  20639. Add('var');
  20640. Add(' V: JSValue;');
  20641. Add(' o: TObject;');
  20642. Add('begin');
  20643. Add(' v:=@GetIt;');
  20644. Add(' v:[email protected];');
  20645. Add(' v:[email protected];');
  20646. ConvertProgram;
  20647. CheckSource('TestJSValue_ProcType_Assign',
  20648. LinesToStr([ // statements
  20649. 'rtl.createClass($mod, "TObject", null, function () {',
  20650. ' this.$init = function () {',
  20651. ' };',
  20652. ' this.$final = function () {',
  20653. ' };',
  20654. ' this.GetGlob = function () {',
  20655. ' var Result = 0;',
  20656. ' var v1 = undefined;',
  20657. ' v1 = rtl.createCallback(this, "GetGlob");',
  20658. ' v1 = rtl.createCallback(this, "GetGlob");',
  20659. ' return Result;',
  20660. ' };',
  20661. ' this.Getter = function () {',
  20662. ' var Result = 0;',
  20663. ' var v2 = undefined;',
  20664. ' v2 = rtl.createCallback(this, "Getter");',
  20665. ' v2 = rtl.createCallback(this, "Getter");',
  20666. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  20667. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  20668. ' return Result;',
  20669. ' };',
  20670. '});',
  20671. 'this.GetIt = function (i) {',
  20672. ' var Result = 0;',
  20673. ' var v3 = undefined;',
  20674. ' v3 = $mod.GetIt;',
  20675. ' return Result;',
  20676. '};',
  20677. 'this.V = undefined;',
  20678. 'this.o = null;',
  20679. '']),
  20680. LinesToStr([ // $mod.$main
  20681. '$mod.V = $mod.GetIt;',
  20682. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  20683. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  20684. '']));
  20685. end;
  20686. procedure TTestModule.TestJSValue_ProcType_Equal;
  20687. begin
  20688. StartProgram(false);
  20689. Add('type');
  20690. Add(' integer = longint;');
  20691. Add(' TObject = class');
  20692. Add(' class function GetGlob: integer;');
  20693. Add(' function Getter: integer;');
  20694. Add(' end;');
  20695. Add('class function TObject.GetGlob: integer;');
  20696. Add('var v1: jsvalue;');
  20697. Add('begin');
  20698. Add(' if v1=@GetGlob then;');
  20699. Add(' if [email protected] then ;');
  20700. Add('end;');
  20701. Add('function TObject.Getter: integer;');
  20702. Add('var v2: jsvalue;');
  20703. Add('begin');
  20704. Add(' if v2=@Getter then;');
  20705. Add(' if [email protected] then ;');
  20706. Add(' if v2=@GetGlob then;');
  20707. Add(' if [email protected] then;');
  20708. Add('end;');
  20709. Add('function GetIt(i: integer): integer;');
  20710. Add('var v3: jsvalue;');
  20711. Add('begin');
  20712. Add(' if v3=@GetIt then;');
  20713. Add('end;');
  20714. Add('var');
  20715. Add(' V: JSValue;');
  20716. Add(' o: TObject;');
  20717. Add('begin');
  20718. Add(' if v=@GetIt then;');
  20719. Add(' if [email protected] then;');
  20720. Add(' if [email protected] then;');
  20721. Add(' if @GetIt=v then;');
  20722. Add(' if @o.Getter=v then;');
  20723. Add(' if @o.GetGlob=v then;');
  20724. ConvertProgram;
  20725. CheckSource('TestJSValue_ProcType_Equal',
  20726. LinesToStr([ // statements
  20727. 'rtl.createClass($mod, "TObject", null, function () {',
  20728. ' this.$init = function () {',
  20729. ' };',
  20730. ' this.$final = function () {',
  20731. ' };',
  20732. ' this.GetGlob = function () {',
  20733. ' var Result = 0;',
  20734. ' var v1 = undefined;',
  20735. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  20736. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  20737. ' return Result;',
  20738. ' };',
  20739. ' this.Getter = function () {',
  20740. ' var Result = 0;',
  20741. ' var v2 = undefined;',
  20742. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  20743. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  20744. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  20745. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  20746. ' return Result;',
  20747. ' };',
  20748. '});',
  20749. 'this.GetIt = function (i) {',
  20750. ' var Result = 0;',
  20751. ' var v3 = undefined;',
  20752. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  20753. ' return Result;',
  20754. '};',
  20755. 'this.V = undefined;',
  20756. 'this.o = null;',
  20757. '']),
  20758. LinesToStr([ // $mod.$main
  20759. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  20760. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  20761. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  20762. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  20763. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  20764. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  20765. '']));
  20766. end;
  20767. procedure TTestModule.TestJSValue_ProcType_Param;
  20768. begin
  20769. StartProgram(false);
  20770. Add([
  20771. 'type',
  20772. ' variant = jsvalue;',
  20773. ' TArrVariant = array of variant;',
  20774. ' TArrVar2 = TArrVariant;',
  20775. ' TFuncInt = function: longint;',
  20776. 'function GetIt: longint;',
  20777. 'begin',
  20778. 'end;',
  20779. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  20780. 'var v: variant;',
  20781. 'begin',
  20782. ' v:=arr[1];',
  20783. 'end;',
  20784. 'var s: string;',
  20785. 'begin',
  20786. ' DoIt(GetIt,[]);',
  20787. ' DoIt(@GetIt,[]);',
  20788. ' DoIt(1,[s,GetIt]);',
  20789. ' DoIt(1,[s,@GetIt]);',
  20790. '']);
  20791. ConvertProgram;
  20792. CheckSource('TestJSValue_ProcType_Param',
  20793. LinesToStr([ // statements
  20794. 'this.GetIt = function () {',
  20795. ' var Result = 0;',
  20796. ' return Result;',
  20797. '};',
  20798. 'this.DoIt = function (p, Arr) {',
  20799. ' var v = undefined;',
  20800. ' v = Arr[1];',
  20801. '};',
  20802. 'this.s = "";',
  20803. '']),
  20804. LinesToStr([ // $mod.$main
  20805. '$mod.DoIt($mod.GetIt(), []);',
  20806. '$mod.DoIt($mod.GetIt, []);',
  20807. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  20808. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  20809. '']));
  20810. end;
  20811. procedure TTestModule.TestJSValue_AssignToPointerFail;
  20812. begin
  20813. StartProgram(false);
  20814. Add([
  20815. 'var',
  20816. ' v: JSValue;',
  20817. ' p: Pointer;',
  20818. 'begin',
  20819. ' p:=v;',
  20820. '']);
  20821. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  20822. nIncompatibleTypesGotExpected);
  20823. ConvertProgram;
  20824. end;
  20825. procedure TTestModule.TestJSValue_OverloadDouble;
  20826. begin
  20827. StartProgram(false);
  20828. Add([
  20829. 'type',
  20830. ' integer = longint;',
  20831. ' tdatetime = double;',
  20832. 'procedure DoIt(d: double); begin end;',
  20833. 'procedure DoIt(v: jsvalue); begin end;',
  20834. 'var',
  20835. ' d: double;',
  20836. ' dt: tdatetime;',
  20837. ' i: integer;',
  20838. ' b: byte;',
  20839. ' shi: shortint;',
  20840. ' w: word;',
  20841. ' smi: smallint;',
  20842. ' lw: longword;',
  20843. ' li: longint;',
  20844. ' ni: nativeint;',
  20845. ' nu: nativeuint;',
  20846. 'begin',
  20847. ' DoIt(d);',
  20848. ' DoIt(dt);',
  20849. ' DoIt(i);',
  20850. ' DoIt(b);',
  20851. ' DoIt(shi);',
  20852. ' DoIt(w);',
  20853. ' DoIt(smi);',
  20854. ' DoIt(lw);',
  20855. ' DoIt(li);',
  20856. ' DoIt(ni);',
  20857. ' DoIt(nu);',
  20858. '']);
  20859. ConvertProgram;
  20860. CheckSource('TestJSValue_OverloadDouble',
  20861. LinesToStr([ // statements
  20862. 'this.DoIt = function (d) {',
  20863. '};',
  20864. 'this.DoIt$1 = function (v) {',
  20865. '};',
  20866. 'this.d = 0.0;',
  20867. 'this.dt = 0.0;',
  20868. 'this.i = 0;',
  20869. 'this.b = 0;',
  20870. 'this.shi = 0;',
  20871. 'this.w = 0;',
  20872. 'this.smi = 0;',
  20873. 'this.lw = 0;',
  20874. 'this.li = 0;',
  20875. 'this.ni = 0;',
  20876. 'this.nu = 0;',
  20877. '']),
  20878. LinesToStr([ // $mod.$main
  20879. '$mod.DoIt($mod.d);',
  20880. '$mod.DoIt($mod.dt);',
  20881. '$mod.DoIt$1($mod.i);',
  20882. '$mod.DoIt$1($mod.b);',
  20883. '$mod.DoIt$1($mod.shi);',
  20884. '$mod.DoIt$1($mod.w);',
  20885. '$mod.DoIt$1($mod.smi);',
  20886. '$mod.DoIt$1($mod.lw);',
  20887. '$mod.DoIt$1($mod.li);',
  20888. '$mod.DoIt$1($mod.ni);',
  20889. '$mod.DoIt$1($mod.nu);',
  20890. '']));
  20891. end;
  20892. procedure TTestModule.TestJSValue_OverloadNativeInt;
  20893. begin
  20894. StartProgram(false);
  20895. Add([
  20896. 'type',
  20897. ' integer = longint;',
  20898. ' int53 = nativeint;',
  20899. ' tdatetime = double;',
  20900. 'procedure DoIt(n: nativeint); begin end;',
  20901. 'procedure DoIt(v: jsvalue); begin end;',
  20902. 'var',
  20903. ' d: double;',
  20904. ' dt: tdatetime;',
  20905. ' i: integer;',
  20906. ' b: byte;',
  20907. ' shi: shortint;',
  20908. ' w: word;',
  20909. ' smi: smallint;',
  20910. ' lw: longword;',
  20911. ' li: longint;',
  20912. ' ni: nativeint;',
  20913. ' nu: nativeuint;',
  20914. 'begin',
  20915. ' DoIt(d);',
  20916. ' DoIt(dt);',
  20917. ' DoIt(i);',
  20918. ' DoIt(b);',
  20919. ' DoIt(shi);',
  20920. ' DoIt(w);',
  20921. ' DoIt(smi);',
  20922. ' DoIt(lw);',
  20923. ' DoIt(li);',
  20924. ' DoIt(ni);',
  20925. ' DoIt(nu);',
  20926. '']);
  20927. ConvertProgram;
  20928. CheckSource('TestJSValue_OverloadNativeInt',
  20929. LinesToStr([ // statements
  20930. 'this.DoIt = function (n) {',
  20931. '};',
  20932. 'this.DoIt$1 = function (v) {',
  20933. '};',
  20934. 'this.d = 0.0;',
  20935. 'this.dt = 0.0;',
  20936. 'this.i = 0;',
  20937. 'this.b = 0;',
  20938. 'this.shi = 0;',
  20939. 'this.w = 0;',
  20940. 'this.smi = 0;',
  20941. 'this.lw = 0;',
  20942. 'this.li = 0;',
  20943. 'this.ni = 0;',
  20944. 'this.nu = 0;',
  20945. '']),
  20946. LinesToStr([ // $mod.$main
  20947. '$mod.DoIt$1($mod.d);',
  20948. '$mod.DoIt$1($mod.dt);',
  20949. '$mod.DoIt($mod.i);',
  20950. '$mod.DoIt($mod.b);',
  20951. '$mod.DoIt($mod.shi);',
  20952. '$mod.DoIt($mod.w);',
  20953. '$mod.DoIt($mod.smi);',
  20954. '$mod.DoIt($mod.lw);',
  20955. '$mod.DoIt($mod.li);',
  20956. '$mod.DoIt($mod.ni);',
  20957. '$mod.DoIt($mod.nu);',
  20958. '']));
  20959. end;
  20960. procedure TTestModule.TestJSValue_OverloadWord;
  20961. begin
  20962. StartProgram(false);
  20963. Add([
  20964. 'type',
  20965. ' integer = longint;',
  20966. ' int53 = nativeint;',
  20967. ' tdatetime = double;',
  20968. 'procedure DoIt(w: word); begin end;',
  20969. 'procedure DoIt(v: jsvalue); begin end;',
  20970. 'var',
  20971. ' d: double;',
  20972. ' dt: tdatetime;',
  20973. ' i: integer;',
  20974. ' b: byte;',
  20975. ' shi: shortint;',
  20976. ' w: word;',
  20977. ' smi: smallint;',
  20978. ' lw: longword;',
  20979. ' li: longint;',
  20980. ' ni: nativeint;',
  20981. ' nu: nativeuint;',
  20982. 'begin',
  20983. ' DoIt(d);',
  20984. ' DoIt(dt);',
  20985. ' DoIt(i);',
  20986. ' DoIt(b);',
  20987. ' DoIt(shi);',
  20988. ' DoIt(w);',
  20989. ' DoIt(smi);',
  20990. ' DoIt(lw);',
  20991. ' DoIt(li);',
  20992. ' DoIt(ni);',
  20993. ' DoIt(nu);',
  20994. '']);
  20995. ConvertProgram;
  20996. CheckSource('TestJSValue_OverloadWord',
  20997. LinesToStr([ // statements
  20998. 'this.DoIt = function (w) {',
  20999. '};',
  21000. 'this.DoIt$1 = function (v) {',
  21001. '};',
  21002. 'this.d = 0.0;',
  21003. 'this.dt = 0.0;',
  21004. 'this.i = 0;',
  21005. 'this.b = 0;',
  21006. 'this.shi = 0;',
  21007. 'this.w = 0;',
  21008. 'this.smi = 0;',
  21009. 'this.lw = 0;',
  21010. 'this.li = 0;',
  21011. 'this.ni = 0;',
  21012. 'this.nu = 0;',
  21013. '']),
  21014. LinesToStr([ // $mod.$main
  21015. '$mod.DoIt$1($mod.d);',
  21016. '$mod.DoIt$1($mod.dt);',
  21017. '$mod.DoIt$1($mod.i);',
  21018. '$mod.DoIt($mod.b);',
  21019. '$mod.DoIt($mod.shi);',
  21020. '$mod.DoIt($mod.w);',
  21021. '$mod.DoIt$1($mod.smi);',
  21022. '$mod.DoIt$1($mod.lw);',
  21023. '$mod.DoIt$1($mod.li);',
  21024. '$mod.DoIt$1($mod.ni);',
  21025. '$mod.DoIt$1($mod.nu);',
  21026. '']));
  21027. end;
  21028. procedure TTestModule.TestJSValue_OverloadString;
  21029. begin
  21030. StartProgram(false);
  21031. Add([
  21032. 'type',
  21033. ' uni = string;',
  21034. ' WChar = char;',
  21035. 'procedure DoIt(s: string); begin end;',
  21036. 'procedure DoIt(v: jsvalue); begin end;',
  21037. 'var',
  21038. ' s: string;',
  21039. ' c: char;',
  21040. ' u: uni;',
  21041. 'begin',
  21042. ' DoIt(s);',
  21043. ' DoIt(c);',
  21044. ' DoIt(u);',
  21045. '']);
  21046. ConvertProgram;
  21047. CheckSource('TestJSValue_OverloadString',
  21048. LinesToStr([ // statements
  21049. 'this.DoIt = function (s) {',
  21050. '};',
  21051. 'this.DoIt$1 = function (v) {',
  21052. '};',
  21053. 'this.s = "";',
  21054. 'this.c = "";',
  21055. 'this.u = "";',
  21056. '']),
  21057. LinesToStr([ // $mod.$main
  21058. '$mod.DoIt($mod.s);',
  21059. '$mod.DoIt($mod.c);',
  21060. '$mod.DoIt($mod.u);',
  21061. '']));
  21062. end;
  21063. procedure TTestModule.TestJSValue_OverloadChar;
  21064. begin
  21065. StartProgram(false);
  21066. Add([
  21067. 'type',
  21068. ' uni = string;',
  21069. ' WChar = char;',
  21070. 'procedure DoIt(c: char); begin end;',
  21071. 'procedure DoIt(v: jsvalue); begin end;',
  21072. 'var',
  21073. ' s: string;',
  21074. ' c: char;',
  21075. ' u: uni;',
  21076. 'begin',
  21077. ' DoIt(s);',
  21078. ' DoIt(c);',
  21079. ' DoIt(u);',
  21080. '']);
  21081. ConvertProgram;
  21082. CheckSource('TestJSValue_OverloadChar',
  21083. LinesToStr([ // statements
  21084. 'this.DoIt = function (c) {',
  21085. '};',
  21086. 'this.DoIt$1 = function (v) {',
  21087. '};',
  21088. 'this.s = "";',
  21089. 'this.c = "";',
  21090. 'this.u = "";',
  21091. '']),
  21092. LinesToStr([ // $mod.$main
  21093. '$mod.DoIt$1($mod.s);',
  21094. '$mod.DoIt($mod.c);',
  21095. '$mod.DoIt$1($mod.u);',
  21096. '']));
  21097. end;
  21098. procedure TTestModule.TestJSValue_OverloadPointer;
  21099. begin
  21100. StartProgram(false);
  21101. Add([
  21102. 'type',
  21103. ' TObject = class end;',
  21104. 'procedure DoIt(p: pointer); begin end;',
  21105. 'procedure DoIt(v: jsvalue); begin end;',
  21106. 'var',
  21107. ' o: TObject;',
  21108. 'begin',
  21109. ' DoIt(o);',
  21110. '']);
  21111. ConvertProgram;
  21112. CheckSource('TestJSValue_OverloadPointer',
  21113. LinesToStr([ // statements
  21114. 'rtl.createClass($mod, "TObject", null, function () {',
  21115. ' this.$init = function () {',
  21116. ' };',
  21117. ' this.$final = function () {',
  21118. ' };',
  21119. '});',
  21120. 'this.DoIt = function (p) {',
  21121. '};',
  21122. 'this.DoIt$1 = function (v) {',
  21123. '};',
  21124. 'this.o = null;',
  21125. '']),
  21126. LinesToStr([ // $mod.$main
  21127. '$mod.DoIt($mod.o);',
  21128. '']));
  21129. end;
  21130. procedure TTestModule.TestJSValue_ForIn;
  21131. begin
  21132. StartProgram(false);
  21133. Add([
  21134. 'var',
  21135. ' v: JSValue;',
  21136. ' key: string;',
  21137. 'begin',
  21138. ' for key in v do begin',
  21139. ' if key=''abc'' then ;',
  21140. ' end;',
  21141. '']);
  21142. ConvertProgram;
  21143. CheckSource('TestJSValue_ForIn',
  21144. LinesToStr([ // statements
  21145. 'this.v = undefined;',
  21146. 'this.key = "";',
  21147. '']),
  21148. LinesToStr([ // $mod.$main
  21149. 'for ($mod.key in $mod.v) {',
  21150. ' if ($mod.key === "abc") ;',
  21151. '};',
  21152. '']));
  21153. end;
  21154. procedure TTestModule.TestRTTI_IntRange;
  21155. begin
  21156. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21157. StartProgram(false);
  21158. Add([
  21159. '{$modeswitch externalclass}',
  21160. 'type',
  21161. ' TTypeInfo = class external name ''rtl.tTypeInfo''',
  21162. ' end;',
  21163. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  21164. ' end;',
  21165. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  21166. ' TColor = type TGraphicsColor;',
  21167. 'var',
  21168. ' p: TTypeInfo;',
  21169. 'begin',
  21170. ' p:=typeinfo(TGraphicsColor);',
  21171. ' p:=typeinfo(TColor);',
  21172. '']);
  21173. ConvertProgram;
  21174. CheckSource('TestRTTI_IntRange',
  21175. LinesToStr([ // statements
  21176. '$mod.$rtti.$Int("TGraphicsColor", {',
  21177. ' minvalue: -2147483648,',
  21178. ' maxvalue: 2147483647,',
  21179. ' ordtype: 4',
  21180. '});',
  21181. '$mod.$rtti.$inherited("TColor", $mod.$rtti["TGraphicsColor"], {});',
  21182. 'this.p = null;',
  21183. '']),
  21184. LinesToStr([ // $mod.$main
  21185. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  21186. '$mod.p = $mod.$rtti["TColor"];',
  21187. '']));
  21188. end;
  21189. procedure TTestModule.TestRTTI_Double;
  21190. begin
  21191. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21192. StartProgram(false);
  21193. Add([
  21194. '{$modeswitch externalclass}',
  21195. 'type',
  21196. ' TTypeInfo = class external name ''rtl.tTypeInfo''',
  21197. ' end;',
  21198. ' TFloat = type double;',
  21199. 'var',
  21200. ' p: TTypeInfo;',
  21201. 'begin',
  21202. ' p:=typeinfo(double);',
  21203. ' p:=typeinfo(TFloat);',
  21204. '']);
  21205. ConvertProgram;
  21206. CheckSource('TestRTTI_Double',
  21207. LinesToStr([ // statements
  21208. '$mod.$rtti.$inherited("TFloat", rtl.double, {});',
  21209. 'this.p = null;',
  21210. '']),
  21211. LinesToStr([ // $mod.$main
  21212. '$mod.p = rtl.double;',
  21213. '$mod.p = $mod.$rtti["TFloat"];',
  21214. '']));
  21215. end;
  21216. procedure TTestModule.TestRTTI_ProcType;
  21217. begin
  21218. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21219. StartProgram(false);
  21220. Add('type');
  21221. Add(' TProcA = procedure;');
  21222. Add(' TMethodB = procedure of object;');
  21223. Add(' TProcC = procedure; varargs;');
  21224. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  21225. Add(' TProcE = function: nativeint;');
  21226. Add(' TProcF = function(const p: TProcA): nativeuint;');
  21227. Add('var p: pointer;');
  21228. Add('begin');
  21229. Add(' p:=typeinfo(tproca);');
  21230. ConvertProgram;
  21231. CheckSource('TestRTTI_ProcType',
  21232. LinesToStr([ // statements
  21233. '$mod.$rtti.$ProcVar("TProcA", {',
  21234. ' procsig: rtl.newTIProcSig(null)',
  21235. '});',
  21236. '$mod.$rtti.$MethodVar("TMethodB", {',
  21237. ' procsig: rtl.newTIProcSig(null),',
  21238. ' methodkind: 0',
  21239. '});',
  21240. '$mod.$rtti.$ProcVar("TProcC", {',
  21241. ' procsig: rtl.newTIProcSig(null, 2)',
  21242. '});',
  21243. '$mod.$rtti.$ProcVar("TProcD", {',
  21244. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  21245. '});',
  21246. '$mod.$rtti.$ProcVar("TProcE", {',
  21247. ' procsig: rtl.newTIProcSig(null, rtl.nativeint)',
  21248. '});',
  21249. '$mod.$rtti.$ProcVar("TProcF", {',
  21250. ' procsig: rtl.newTIProcSig([["p", $mod.$rtti["TProcA"], 2]], rtl.nativeuint)',
  21251. '});',
  21252. 'this.p = null;',
  21253. '']),
  21254. LinesToStr([ // $mod.$main
  21255. '$mod.p = $mod.$rtti["TProcA"];',
  21256. '']));
  21257. end;
  21258. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  21259. begin
  21260. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21261. AddModuleWithIntfImplSrc('unit2.pas',
  21262. LinesToStr([
  21263. 'type',
  21264. ' TObject = class end;'
  21265. ]),
  21266. '');
  21267. StartUnit(true);
  21268. Add('interface');
  21269. Add('uses unit2;');
  21270. Add('type');
  21271. Add(' TProcA = function(o: tobject): tobject;');
  21272. Add('implementation');
  21273. Add('type');
  21274. Add(' TProcB = function(o: tobject): tobject;');
  21275. Add('var p: Pointer;');
  21276. Add('initialization');
  21277. Add(' p:=typeinfo(tproca);');
  21278. Add(' p:=typeinfo(tprocb);');
  21279. ConvertUnit;
  21280. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  21281. LinesToStr([ // statements
  21282. 'var $impl = $mod.$impl;',
  21283. '$mod.$rtti.$ProcVar("TProcA", {',
  21284. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  21285. '});',
  21286. '']),
  21287. LinesToStr([ // this.$init
  21288. '$impl.p = $mod.$rtti["TProcA"];',
  21289. '$impl.p = $mod.$rtti["TProcB"];',
  21290. '']),
  21291. LinesToStr([ // implementation
  21292. '$mod.$rtti.$ProcVar("TProcB", {',
  21293. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  21294. '});',
  21295. '$impl.p = null;',
  21296. '']) );
  21297. end;
  21298. procedure TTestModule.TestRTTI_EnumAndSetType;
  21299. begin
  21300. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21301. StartProgram(false);
  21302. Add('type');
  21303. Add(' TFlag = (light,dark);');
  21304. Add(' TFlags = set of TFlag;');
  21305. Add(' TProc = function(f: TFlags): TFlag;');
  21306. Add('var p: pointer;');
  21307. Add('begin');
  21308. Add(' p:=typeinfo(tflag);');
  21309. Add(' p:=typeinfo(tflags);');
  21310. ConvertProgram;
  21311. CheckSource('TestRTTI_EnumAndType',
  21312. LinesToStr([ // statements
  21313. 'this.TFlag = {',
  21314. ' "0": "light",',
  21315. ' light: 0,',
  21316. ' "1": "dark",',
  21317. ' dark: 1',
  21318. '};',
  21319. '$mod.$rtti.$Enum("TFlag", {',
  21320. ' minvalue: 0,',
  21321. ' maxvalue: 1,',
  21322. ' ordtype: 1,',
  21323. ' enumtype: this.TFlag',
  21324. '});',
  21325. '$mod.$rtti.$Set("TFlags", {',
  21326. ' comptype: $mod.$rtti["TFlag"]',
  21327. '});',
  21328. '$mod.$rtti.$ProcVar("TProc", {',
  21329. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TFlags"]]], $mod.$rtti["TFlag"])',
  21330. '});',
  21331. 'this.p = null;',
  21332. '']),
  21333. LinesToStr([ // $mod.$main
  21334. '$mod.p = $mod.$rtti["TFlag"];',
  21335. '$mod.p = $mod.$rtti["TFlags"];',
  21336. '']));
  21337. end;
  21338. procedure TTestModule.TestRTTI_EnumRange;
  21339. begin
  21340. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21341. StartProgram(false);
  21342. Add([
  21343. 'type',
  21344. ' TCol = (red,green,blue);',
  21345. ' TColRg = green..blue;',
  21346. ' TSetOfColRg = set of TColRg;',
  21347. 'var p: pointer;',
  21348. 'begin',
  21349. ' p:=typeinfo(tcolrg);',
  21350. ' p:=typeinfo(tsetofcolrg);',
  21351. '']);
  21352. ConvertProgram;
  21353. end;
  21354. procedure TTestModule.TestRTTI_AnonymousEnumType;
  21355. begin
  21356. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21357. StartProgram(false);
  21358. Add('type');
  21359. Add(' TFlags = set of (red, green);');
  21360. Add('var');
  21361. Add(' f: TFlags;');
  21362. Add('begin');
  21363. Add(' Include(f,red);');
  21364. ConvertProgram;
  21365. CheckSource('TestRTTI_AnonymousEnumType',
  21366. LinesToStr([ // statements
  21367. 'this.TFlags$a = {',
  21368. ' "0": "red",',
  21369. ' red: 0,',
  21370. ' "1": "green",',
  21371. ' green: 1',
  21372. '};',
  21373. '$mod.$rtti.$Enum("TFlags$a", {',
  21374. ' minvalue: 0,',
  21375. ' maxvalue: 1,',
  21376. ' ordtype: 1,',
  21377. ' enumtype: this.TFlags$a',
  21378. '});',
  21379. '$mod.$rtti.$Set("TFlags", {',
  21380. ' comptype: $mod.$rtti["TFlags$a"]',
  21381. '});',
  21382. 'this.f = {};',
  21383. '']),
  21384. LinesToStr([
  21385. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  21386. '']));
  21387. end;
  21388. procedure TTestModule.TestRTTI_StaticArray;
  21389. begin
  21390. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21391. StartProgram(false);
  21392. Add('type');
  21393. Add(' TFlag = (light,dark);');
  21394. Add(' TFlagNames = array[TFlag] of string;');
  21395. Add(' TBoolNames = array[boolean] of string;');
  21396. Add(' TByteArray = array[1..32768] of byte;');
  21397. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  21398. Add('var p: pointer;');
  21399. Add('begin');
  21400. Add(' p:=typeinfo(TFlagNames);');
  21401. Add(' p:=typeinfo(TBoolNames);');
  21402. ConvertProgram;
  21403. CheckSource('TestRTTI_StaticArray',
  21404. LinesToStr([ // statements
  21405. 'this.TFlag = {',
  21406. ' "0": "light",',
  21407. ' light: 0,',
  21408. ' "1": "dark",',
  21409. ' dark: 1',
  21410. '};',
  21411. '$mod.$rtti.$Enum("TFlag", {',
  21412. ' minvalue: 0,',
  21413. ' maxvalue: 1,',
  21414. ' ordtype: 1,',
  21415. ' enumtype: this.TFlag',
  21416. '});',
  21417. '$mod.$rtti.$StaticArray("TFlagNames", {',
  21418. ' dims: [2],',
  21419. ' eltype: rtl.string',
  21420. '});',
  21421. '$mod.$rtti.$StaticArray("TBoolNames", {',
  21422. ' dims: [2],',
  21423. ' eltype: rtl.string',
  21424. '});',
  21425. '$mod.$rtti.$StaticArray("TByteArray", {',
  21426. ' dims: [32768],',
  21427. ' eltype: rtl.byte',
  21428. '});',
  21429. '$mod.$rtti.$ProcVar("TProc", {',
  21430. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TBoolNames"]]], $mod.$rtti["TFlagNames"])',
  21431. '});',
  21432. 'this.p = null;',
  21433. '']),
  21434. LinesToStr([ // $mod.$main
  21435. '$mod.p = $mod.$rtti["TFlagNames"];',
  21436. '$mod.p = $mod.$rtti["TBoolNames"];',
  21437. '']));
  21438. end;
  21439. procedure TTestModule.TestRTTI_DynArray;
  21440. begin
  21441. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21442. StartProgram(false);
  21443. Add('type');
  21444. Add(' TArrStr = array of string;');
  21445. Add(' TArr2Dim = array of tarrstr;');
  21446. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  21447. Add('var p: pointer;');
  21448. Add('begin');
  21449. Add(' p:=typeinfo(tarrstr);');
  21450. Add(' p:=typeinfo(tarr2dim);');
  21451. ConvertProgram;
  21452. CheckSource('TestRTTI_DynArray',
  21453. LinesToStr([ // statements
  21454. '$mod.$rtti.$DynArray("TArrStr", {',
  21455. ' eltype: rtl.string',
  21456. '});',
  21457. '$mod.$rtti.$DynArray("TArr2Dim", {',
  21458. ' eltype: $mod.$rtti["TArrStr"]',
  21459. '});',
  21460. '$mod.$rtti.$ProcVar("TProc", {',
  21461. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TArrStr"]]], $mod.$rtti["TArr2Dim"])',
  21462. '});',
  21463. 'this.p = null;',
  21464. '']),
  21465. LinesToStr([ // $mod.$main
  21466. '$mod.p = $mod.$rtti["TArrStr"];',
  21467. '$mod.p = $mod.$rtti["TArr2Dim"];',
  21468. '']));
  21469. end;
  21470. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  21471. begin
  21472. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21473. StartProgram(false);
  21474. Add('type');
  21475. Add(' TArr = array of array of longint;');
  21476. Add('var a: TArr;');
  21477. Add('begin');
  21478. ConvertProgram;
  21479. CheckSource('TestRTTI_ArrayNestedAnonymous',
  21480. LinesToStr([ // statements
  21481. '$mod.$rtti.$DynArray("TArr$a", {',
  21482. ' eltype: rtl.longint',
  21483. '});',
  21484. '$mod.$rtti.$DynArray("TArr", {',
  21485. ' eltype: $mod.$rtti["TArr$a"]',
  21486. '});',
  21487. 'this.a = [];',
  21488. '']),
  21489. LinesToStr([ // $mod.$main
  21490. ]));
  21491. end;
  21492. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  21493. begin
  21494. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21495. StartProgram(false);
  21496. Add('type');
  21497. Add(' TObject = class');
  21498. Add(' published');
  21499. Add(' procedure Proc; virtual; abstract;');
  21500. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  21501. Add(' end;');
  21502. Add('begin');
  21503. SetExpectedPasResolverError('Duplicate identifier "Proc" at test1.pp(6,19)',
  21504. nDuplicateIdentifier);
  21505. ConvertProgram;
  21506. end;
  21507. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  21508. begin
  21509. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21510. StartProgram(false);
  21511. Add('type');
  21512. Add(' TObject = class');
  21513. Add(' published');
  21514. Add(' procedure Proc; external name ''foo'';');
  21515. Add(' end;');
  21516. Add('begin');
  21517. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  21518. nPublishedNameMustMatchExternal);
  21519. ConvertProgram;
  21520. end;
  21521. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  21522. begin
  21523. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21524. StartProgram(false);
  21525. Add('type');
  21526. Add(' TObject = class');
  21527. Add(' class var FA: longint;');
  21528. Add(' published');
  21529. Add(' class property A: longint read FA;');
  21530. Add(' end;');
  21531. Add('begin');
  21532. SetExpectedPasResolverError('Invalid published property modifier "class"',
  21533. nInvalidXModifierY);
  21534. ConvertProgram;
  21535. end;
  21536. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  21537. begin
  21538. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21539. StartProgram(false);
  21540. Add('type');
  21541. Add(' TObject = class');
  21542. Add(' published');
  21543. Add(' class var FA: longint;');
  21544. Add(' end;');
  21545. Add('begin');
  21546. SetExpectedPasResolverError(sSymbolCannotBePublished,
  21547. nSymbolCannotBePublished);
  21548. ConvertProgram;
  21549. end;
  21550. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  21551. begin
  21552. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21553. StartProgram(false);
  21554. Add('{$modeswitch externalclass}');
  21555. Add('type');
  21556. Add(' TObject = class');
  21557. Add(' published');
  21558. Add(' V: longint; external name ''foo'';');
  21559. Add(' end;');
  21560. Add('begin');
  21561. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  21562. nPublishedNameMustMatchExternal);
  21563. ConvertProgram;
  21564. end;
  21565. procedure TTestModule.TestRTTI_Class_Field;
  21566. begin
  21567. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21568. StartProgram(false);
  21569. Add('{$modeswitch externalclass}');
  21570. Add('type');
  21571. Add(' TObject = class');
  21572. Add(' private');
  21573. Add(' FPropA: string;');
  21574. Add(' published');
  21575. Add(' VarLI: longint;');
  21576. Add(' VarC: char;');
  21577. Add(' VarS: string;');
  21578. Add(' VarD: double;');
  21579. Add(' VarB: boolean;');
  21580. Add(' VarLW: longword;');
  21581. Add(' VarSmI: smallint;');
  21582. Add(' VarW: word;');
  21583. Add(' VarShI: shortint;');
  21584. Add(' VarBy: byte;');
  21585. Add(' VarExt: longint external name ''VarExt'';');
  21586. Add(' end;');
  21587. Add('var p: pointer;');
  21588. Add(' Obj: tobject;');
  21589. Add('begin');
  21590. Add(' p:=typeinfo(tobject);');
  21591. Add(' p:=typeinfo(p);');
  21592. Add(' p:=typeinfo(obj);');
  21593. ConvertProgram;
  21594. CheckSource('TestRTTI_Class_Field',
  21595. LinesToStr([ // statements
  21596. 'rtl.createClass($mod, "TObject", null, function () {',
  21597. ' this.$init = function () {',
  21598. ' this.FPropA = "";',
  21599. ' this.VarLI = 0;',
  21600. ' this.VarC = "";',
  21601. ' this.VarS = "";',
  21602. ' this.VarD = 0.0;',
  21603. ' this.VarB = false;',
  21604. ' this.VarLW = 0;',
  21605. ' this.VarSmI = 0;',
  21606. ' this.VarW = 0;',
  21607. ' this.VarShI = 0;',
  21608. ' this.VarBy = 0;',
  21609. ' };',
  21610. ' this.$final = function () {',
  21611. ' };',
  21612. ' var $r = this.$rtti;',
  21613. ' $r.addField("VarLI", rtl.longint);',
  21614. ' $r.addField("VarC", rtl.char);',
  21615. ' $r.addField("VarS", rtl.string);',
  21616. ' $r.addField("VarD", rtl.double);',
  21617. ' $r.addField("VarB", rtl.boolean);',
  21618. ' $r.addField("VarLW", rtl.longword);',
  21619. ' $r.addField("VarSmI", rtl.smallint);',
  21620. ' $r.addField("VarW", rtl.word);',
  21621. ' $r.addField("VarShI", rtl.shortint);',
  21622. ' $r.addField("VarBy", rtl.byte);',
  21623. ' $r.addField("VarExt", rtl.longint);',
  21624. '});',
  21625. 'this.p = null;',
  21626. 'this.Obj = null;',
  21627. '']),
  21628. LinesToStr([ // $mod.$main
  21629. '$mod.p = $mod.$rtti["TObject"];',
  21630. '$mod.p = rtl.pointer;',
  21631. '$mod.p = $mod.Obj.$rtti;',
  21632. '']));
  21633. end;
  21634. procedure TTestModule.TestRTTI_Class_Method;
  21635. begin
  21636. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21637. StartProgram(false);
  21638. Add('type');
  21639. Add(' TObject = class');
  21640. Add(' private');
  21641. Add(' procedure Internal; external name ''$intern'';');
  21642. Add(' published');
  21643. Add(' procedure Click; virtual; abstract;');
  21644. Add(' procedure Notify(Sender: TObject); virtual; abstract;');
  21645. Add(' function GetNotify: boolean; external name ''GetNotify'';');
  21646. Add(' procedure Println(a,b: longint); varargs; virtual; abstract;');
  21647. Add(' end;');
  21648. Add('begin');
  21649. ConvertProgram;
  21650. CheckSource('TestRTTI_Class_Method',
  21651. LinesToStr([ // statements
  21652. 'rtl.createClass($mod, "TObject", null, function () {',
  21653. ' this.$init = function () {',
  21654. ' };',
  21655. ' this.$final = function () {',
  21656. ' };',
  21657. ' var $r = this.$rtti;',
  21658. ' $r.addMethod("Click", 0, null);',
  21659. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  21660. ' $r.addMethod("GetNotify", 1, null, rtl.boolean,{flags: 4});',
  21661. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, {',
  21662. ' flags: 2',
  21663. ' });',
  21664. '});',
  21665. '']),
  21666. LinesToStr([ // $mod.$main
  21667. '']));
  21668. end;
  21669. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  21670. begin
  21671. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21672. StartProgram(false);
  21673. Add('type');
  21674. Add(' TObject = class');
  21675. Add(' published');
  21676. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  21677. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  21678. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  21679. Add(' end;');
  21680. Add('begin');
  21681. ConvertProgram;
  21682. CheckSource('TestRTTI_Class_MethodOpenArray',
  21683. LinesToStr([ // statements
  21684. 'rtl.createClass($mod, "TObject", null, function () {',
  21685. ' this.$init = function () {',
  21686. ' };',
  21687. ' this.$final = function () {',
  21688. ' };',
  21689. ' var $r = this.$rtti;',
  21690. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  21691. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  21692. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  21693. '});',
  21694. '']),
  21695. LinesToStr([ // $mod.$main
  21696. '']));
  21697. end;
  21698. procedure TTestModule.TestRTTI_Class_Property;
  21699. begin
  21700. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21701. StartProgram(false);
  21702. Add('{$modeswitch externalclass}');
  21703. Add('type');
  21704. Add(' TObject = class');
  21705. Add(' private');
  21706. Add(' FColor: longint;');
  21707. Add(' FColorStored: boolean;');
  21708. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  21709. Add(' function GetColor: longint; virtual; abstract;');
  21710. Add(' function GetColorStored: boolean; virtual; abstract;');
  21711. Add(' FExtSize: longint external name ''$extSize'';');
  21712. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  21713. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  21714. Add(' function GetExtSize: longint; external name ''$getSize'';');
  21715. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  21716. Add(' published');
  21717. Add(' property ColorA: longint read FColor;');
  21718. Add(' property ColorB: longint write FColor;');
  21719. Add(' property ColorC: longint read GetColor write SetColor;');
  21720. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  21721. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  21722. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  21723. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  21724. Add(' end;');
  21725. Add('begin');
  21726. ConvertProgram;
  21727. CheckSource('TestRTTI_Class_Property',
  21728. LinesToStr([ // statements
  21729. 'rtl.createClass($mod, "TObject", null, function () {',
  21730. ' this.$init = function () {',
  21731. ' this.FColor = 0;',
  21732. ' this.FColorStored = false;',
  21733. ' };',
  21734. ' this.$final = function () {',
  21735. ' };',
  21736. ' var $r = this.$rtti;',
  21737. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  21738. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  21739. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  21740. ' $r.addProperty(',
  21741. ' "ColorD",',
  21742. ' 8,',
  21743. ' rtl.longint,',
  21744. ' "FColor",',
  21745. ' "FColor",',
  21746. ' {',
  21747. ' stored: "FColorStored"',
  21748. ' }',
  21749. ' );',
  21750. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  21751. ' $r.addProperty(',
  21752. ' "ExtSizeB",',
  21753. ' 11,',
  21754. ' rtl.longint,',
  21755. ' "$getSize",',
  21756. ' "$setSize",',
  21757. ' {',
  21758. ' stored: "$extSizeStored"',
  21759. ' }',
  21760. ' );',
  21761. ' $r.addProperty(',
  21762. ' "ExtSizeC",',
  21763. ' 12,',
  21764. ' rtl.longint,',
  21765. ' "$extSize",',
  21766. ' "$extSize",',
  21767. ' {',
  21768. ' stored: "$getExtSizeStored"',
  21769. ' }',
  21770. ' );',
  21771. '});',
  21772. '']),
  21773. LinesToStr([ // $mod.$main
  21774. '']));
  21775. end;
  21776. procedure TTestModule.TestRTTI_Class_PropertyParams;
  21777. begin
  21778. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21779. StartProgram(false);
  21780. Add('{$modeswitch externalclass}');
  21781. Add('type');
  21782. Add(' integer = longint;');
  21783. Add(' TObject = class');
  21784. Add(' private');
  21785. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  21786. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  21787. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  21788. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  21789. Add(' published');
  21790. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  21791. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  21792. Add(' end;');
  21793. Add('begin');
  21794. ConvertProgram;
  21795. CheckSource('TestRTTI_Class_PropertyParams',
  21796. LinesToStr([ // statements
  21797. 'rtl.createClass($mod, "TObject", null, function () {',
  21798. ' this.$init = function () {',
  21799. ' };',
  21800. ' this.$final = function () {',
  21801. ' };',
  21802. ' var $r = this.$rtti;',
  21803. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  21804. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  21805. '});',
  21806. '']),
  21807. LinesToStr([ // $mod.$main
  21808. '']));
  21809. end;
  21810. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  21811. begin
  21812. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21813. AddModuleWithIntfImplSrc('unit1.pas',
  21814. 'type TColor = -5..5;',
  21815. '');
  21816. StartProgram(true);
  21817. Add([
  21818. 'uses unit1;',
  21819. 'type',
  21820. ' TColorAlias = TColor;',
  21821. ' TColorTypeAlias = type TColor;',
  21822. ' TObject = class',
  21823. ' private',
  21824. ' fColor: TColor;',
  21825. ' fAlias: TColorAlias;',
  21826. ' fTypeAlias: TColorTypeAlias;',
  21827. ' published',
  21828. ' property Color: TColor read fcolor;',
  21829. ' property Alias: TColorAlias read falias;',
  21830. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  21831. ' end;',
  21832. 'begin',
  21833. '']);
  21834. ConvertProgram;
  21835. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  21836. LinesToStr([ // statements
  21837. '$mod.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  21838. 'rtl.createClass($mod, "TObject", null, function () {',
  21839. ' this.$init = function () {',
  21840. ' this.fColor = 0;',
  21841. ' this.fAlias = 0;',
  21842. ' this.fTypeAlias = 0;',
  21843. ' };',
  21844. ' this.$final = function () {',
  21845. ' };',
  21846. ' var $r = this.$rtti;',
  21847. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  21848. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  21849. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  21850. '});',
  21851. '']),
  21852. LinesToStr([ // $mod.$main
  21853. '']));
  21854. end;
  21855. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  21856. begin
  21857. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21858. StartProgram(false);
  21859. Add([
  21860. '{$modeswitch omitrtti}',
  21861. 'type',
  21862. ' TObject = class',
  21863. ' private',
  21864. ' FA: byte;',
  21865. ' published',
  21866. ' property A: byte read FA write FA;',
  21867. ' end;',
  21868. 'begin']);
  21869. ConvertProgram;
  21870. CheckSource('TestRTTI_Class_OmitRTTI',
  21871. LinesToStr([ // statements
  21872. 'rtl.createClass($mod, "TObject", null, function () {',
  21873. ' this.$init = function () {',
  21874. ' this.FA = 0;',
  21875. ' };',
  21876. ' this.$final = function () {',
  21877. ' };',
  21878. '});',
  21879. '']),
  21880. LinesToStr([ // $mod.$main
  21881. '']));
  21882. end;
  21883. procedure TTestModule.TestRTTI_IndexModifier;
  21884. begin
  21885. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21886. StartProgram(false);
  21887. Add([
  21888. 'type',
  21889. ' TEnum = (red, blue);',
  21890. ' TObject = class',
  21891. ' FB: boolean;',
  21892. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  21893. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  21894. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  21895. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  21896. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  21897. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  21898. ' published',
  21899. ' property B1: boolean index 1 read FB write SetIntBool;',
  21900. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  21901. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  21902. ' end;',
  21903. 'begin']);
  21904. ConvertProgram;
  21905. CheckSource('TestRTTI_IndexModifier',
  21906. LinesToStr([ // statements
  21907. 'this.TEnum = {',
  21908. ' "0": "red",',
  21909. ' red: 0,',
  21910. ' "1": "blue",',
  21911. ' blue: 1',
  21912. '};',
  21913. '$mod.$rtti.$Enum("TEnum", {',
  21914. ' minvalue: 0,',
  21915. ' maxvalue: 1,',
  21916. ' ordtype: 1,',
  21917. ' enumtype: this.TEnum',
  21918. '});',
  21919. 'rtl.createClass($mod, "TObject", null, function () {',
  21920. ' this.$init = function () {',
  21921. ' this.FB = false;',
  21922. ' };',
  21923. ' this.$final = function () {',
  21924. ' };',
  21925. ' var $r = this.$rtti;',
  21926. ' $r.addProperty(',
  21927. ' "B1",',
  21928. ' 18,',
  21929. ' rtl.boolean,',
  21930. ' "FB",',
  21931. ' "SetIntBool",',
  21932. ' {',
  21933. ' index: 1',
  21934. ' }',
  21935. ' );',
  21936. ' $r.addProperty(',
  21937. ' "B2",',
  21938. ' 17,',
  21939. ' rtl.boolean,',
  21940. ' "GetEnumBool",',
  21941. ' "FB",',
  21942. ' {',
  21943. ' index: $mod.TEnum.blue',
  21944. ' }',
  21945. ' );',
  21946. ' $r.addProperty(',
  21947. ' "I1",',
  21948. ' 19,',
  21949. ' rtl.boolean,',
  21950. ' "GetStrIntBool",',
  21951. ' "SetStrIntBool",',
  21952. ' {',
  21953. ' index: 2',
  21954. ' }',
  21955. ' );',
  21956. '});',
  21957. '']),
  21958. LinesToStr([ // $mod.$main
  21959. '']));
  21960. end;
  21961. procedure TTestModule.TestRTTI_StoredModifier;
  21962. begin
  21963. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21964. StartProgram(false);
  21965. Add([
  21966. 'const',
  21967. ' ConstB = true;',
  21968. 'type',
  21969. ' TObject = class',
  21970. ' private',
  21971. ' FB: boolean;',
  21972. ' function IsBStored: boolean; virtual; abstract;',
  21973. ' published',
  21974. ' property BoolA: boolean read FB stored true;',
  21975. ' property BoolB: boolean read FB stored false;',
  21976. ' property BoolC: boolean read FB stored FB;',
  21977. ' property BoolD: boolean read FB stored ConstB;',
  21978. ' property BoolE: boolean read FB stored IsBStored;',
  21979. ' end;',
  21980. 'begin']);
  21981. ConvertProgram;
  21982. CheckSource('TestRTTI_StoredModifier',
  21983. LinesToStr([ // statements
  21984. 'this.ConstB = true;',
  21985. 'rtl.createClass($mod, "TObject", null, function () {',
  21986. ' this.$init = function () {',
  21987. ' this.FB = false;',
  21988. ' };',
  21989. ' this.$final = function () {',
  21990. ' };',
  21991. ' var $r = this.$rtti;',
  21992. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  21993. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  21994. ' $r.addProperty(',
  21995. ' "BoolC",',
  21996. ' 8,',
  21997. ' rtl.boolean,',
  21998. ' "FB",',
  21999. ' "",',
  22000. ' {',
  22001. ' stored: "FB"',
  22002. ' }',
  22003. ' );',
  22004. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  22005. ' $r.addProperty(',
  22006. ' "BoolE",',
  22007. ' 12,',
  22008. ' rtl.boolean,',
  22009. ' "FB",',
  22010. ' "",',
  22011. ' {',
  22012. ' stored: "IsBStored"',
  22013. ' }',
  22014. ' );',
  22015. '});',
  22016. '']),
  22017. LinesToStr([ // $mod.$main
  22018. '']));
  22019. end;
  22020. procedure TTestModule.TestRTTI_DefaultValue;
  22021. begin
  22022. Converter.Options:=Converter.Options-[coNoTypeInfo];
  22023. StartProgram(false);
  22024. Add([
  22025. 'type',
  22026. ' TEnum = (red, blue);',
  22027. 'const',
  22028. ' CB = true or false;',
  22029. ' CI = 1+2;',
  22030. 'type',
  22031. ' TObject = class',
  22032. ' FB: boolean;',
  22033. ' FI: longint;',
  22034. ' FE: TEnum;',
  22035. ' published',
  22036. ' property B1: boolean read FB default true;',
  22037. ' property B2: boolean read FB default CB;',
  22038. ' property B3: boolean read FB default test1.cb;',
  22039. ' property I1: longint read FI default 2;',
  22040. ' property I2: longint read FI default CI;',
  22041. ' property E1: TEnum read FE default red;',
  22042. ' property E2: TEnum read FE default TEnum.blue;',
  22043. ' end;',
  22044. 'begin']);
  22045. ConvertProgram;
  22046. CheckSource('TestRTTI_DefaultValue',
  22047. LinesToStr([ // statements
  22048. 'this.TEnum = {',
  22049. ' "0": "red",',
  22050. ' red: 0,',
  22051. ' "1": "blue",',
  22052. ' blue: 1',
  22053. '};',
  22054. '$mod.$rtti.$Enum("TEnum", {',
  22055. ' minvalue: 0,',
  22056. ' maxvalue: 1,',
  22057. ' ordtype: 1,',
  22058. ' enumtype: this.TEnum',
  22059. '});',
  22060. 'this.CB = true || false;',
  22061. 'this.CI = 1 + 2;',
  22062. 'rtl.createClass($mod, "TObject", null, function () {',
  22063. ' this.$init = function () {',
  22064. ' this.FB = false;',
  22065. ' this.FI = 0;',
  22066. ' this.FE = 0;',
  22067. ' };',
  22068. ' this.$final = function () {',
  22069. ' };',
  22070. ' var $r = this.$rtti;',
  22071. ' $r.addProperty(',
  22072. ' "B1",',
  22073. ' 0,',
  22074. ' rtl.boolean,',
  22075. ' "FB",',
  22076. ' "",',
  22077. ' {',
  22078. ' Default: true',
  22079. ' }',
  22080. ' );',
  22081. ' $r.addProperty(',
  22082. ' "B2",',
  22083. ' 0,',
  22084. ' rtl.boolean,',
  22085. ' "FB",',
  22086. ' "",',
  22087. ' {',
  22088. ' Default: true',
  22089. ' }',
  22090. ' );',
  22091. ' $r.addProperty(',
  22092. ' "B3",',
  22093. ' 0,',
  22094. ' rtl.boolean,',
  22095. ' "FB",',
  22096. ' "",',
  22097. ' {',
  22098. ' Default: true',
  22099. ' }',
  22100. ' );',
  22101. ' $r.addProperty(',
  22102. ' "I1",',
  22103. ' 0,',
  22104. ' rtl.longint,',
  22105. ' "FI",',
  22106. ' "",',
  22107. ' {',
  22108. ' Default: 2',
  22109. ' }',
  22110. ' );',
  22111. ' $r.addProperty(',
  22112. ' "I2",',
  22113. ' 0,',
  22114. ' rtl.longint,',
  22115. ' "FI",',
  22116. ' "",',
  22117. ' {',
  22118. ' Default: 3',
  22119. ' }',
  22120. ' );',
  22121. ' $r.addProperty(',
  22122. ' "E1",',
  22123. ' 0,',
  22124. ' $mod.$rtti["TEnum"],',
  22125. ' "FE",',
  22126. ' "",',
  22127. ' {',
  22128. ' Default: $mod.TEnum.red',
  22129. ' }',
  22130. ' );',
  22131. ' $r.addProperty(',
  22132. ' "E2",',
  22133. ' 0,',
  22134. ' $mod.$rtti["TEnum"],',
  22135. ' "FE",',
  22136. ' "",',
  22137. ' {',
  22138. ' Default: $mod.TEnum.blue',
  22139. ' }',
  22140. ' );',
  22141. '});',
  22142. '']),
  22143. LinesToStr([ // $mod.$main
  22144. '']));
  22145. end;
  22146. procedure TTestModule.TestRTTI_DefaultValueSet;
  22147. begin
  22148. Converter.Options:=Converter.Options-[coNoTypeInfo];
  22149. StartProgram(false);
  22150. Add([
  22151. 'type',
  22152. ' TEnum = (red, blue);',
  22153. ' TSet = set of TEnum;',
  22154. 'const',
  22155. ' CSet = [red,blue];',
  22156. 'type',
  22157. ' TObject = class',
  22158. ' FSet: TSet;',
  22159. ' published',
  22160. ' property Set1: TSet read FSet default [];',
  22161. ' property Set2: TSet read FSet default [red];',
  22162. ' property Set3: TSet read FSet default [red,blue];',
  22163. ' property Set4: TSet read FSet default CSet;',
  22164. ' end;',
  22165. 'begin']);
  22166. ConvertProgram;
  22167. CheckSource('TestRTTI_DefaultValueSet',
  22168. LinesToStr([ // statements
  22169. 'this.TEnum = {',
  22170. ' "0": "red",',
  22171. ' red: 0,',
  22172. ' "1": "blue",',
  22173. ' blue: 1',
  22174. '};',
  22175. '$mod.$rtti.$Enum("TEnum", {',
  22176. ' minvalue: 0,',
  22177. ' maxvalue: 1,',
  22178. ' ordtype: 1,',
  22179. ' enumtype: this.TEnum',
  22180. '});',
  22181. '$mod.$rtti.$Set("TSet", {',
  22182. ' comptype: $mod.$rtti["TEnum"]',
  22183. '});',
  22184. 'this.CSet = rtl.createSet($mod.TEnum.red, $mod.TEnum.blue);',
  22185. 'rtl.createClass($mod, "TObject", null, function () {',
  22186. ' this.$init = function () {',
  22187. ' this.FSet = {};',
  22188. ' };',
  22189. ' this.$final = function () {',
  22190. ' this.FSet = undefined;',
  22191. ' };',
  22192. ' var $r = this.$rtti;',
  22193. ' $r.addProperty(',
  22194. ' "Set1",',
  22195. ' 0,',
  22196. ' $mod.$rtti["TSet"],',
  22197. ' "FSet",',
  22198. ' "",',
  22199. ' {',
  22200. ' Default: {}',
  22201. ' }',
  22202. ' );',
  22203. ' $r.addProperty(',
  22204. ' "Set2",',
  22205. ' 0,',
  22206. ' $mod.$rtti["TSet"],',
  22207. ' "FSet",',
  22208. ' "",',
  22209. ' {',
  22210. ' Default: rtl.createSet($mod.TEnum.red)',
  22211. ' }',
  22212. ' );',
  22213. ' $r.addProperty(',
  22214. ' "Set3",',
  22215. ' 0,',
  22216. ' $mod.$rtti["TSet"],',
  22217. ' "FSet",',
  22218. ' "",',
  22219. ' {',
  22220. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  22221. ' }',
  22222. ' );',
  22223. ' $r.addProperty(',
  22224. ' "Set4",',
  22225. ' 0,',
  22226. ' $mod.$rtti["TSet"],',
  22227. ' "FSet",',
  22228. ' "",',
  22229. ' {',
  22230. ' Default: $mod.CSet',
  22231. ' }',
  22232. ' );',
  22233. '});',
  22234. '']),
  22235. LinesToStr([ // $mod.$main
  22236. '']));
  22237. end;
  22238. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  22239. begin
  22240. Converter.Options:=Converter.Options-[coNoTypeInfo];
  22241. StartProgram(false);
  22242. Add([
  22243. 'type',
  22244. ' TRg = -1..1;',
  22245. 'const',
  22246. ' l = low(TRg);',
  22247. ' h = high(TRg);',
  22248. 'type',
  22249. ' TObject = class',
  22250. ' FV: TRg;',
  22251. ' published',
  22252. ' property V1: TRg read FV default -1;',
  22253. ' end;',
  22254. 'begin']);
  22255. ConvertProgram;
  22256. CheckSource('TestRTTI_DefaultValueRangeType',
  22257. LinesToStr([ // statements
  22258. '$mod.$rtti.$Int("TRg", {',
  22259. ' minvalue: -1,',
  22260. ' maxvalue: 1,',
  22261. ' ordtype: 0',
  22262. '});',
  22263. 'this.l = -1;',
  22264. 'this.h = 1;',
  22265. 'rtl.createClass($mod, "TObject", null, function () {',
  22266. ' this.$init = function () {',
  22267. ' this.FV = 0;',
  22268. ' };',
  22269. ' this.$final = function () {',
  22270. ' };',
  22271. ' var $r = this.$rtti;',
  22272. ' $r.addProperty(',
  22273. ' "V1",',
  22274. ' 0,',
  22275. ' $mod.$rtti["TRg"],',
  22276. ' "FV",',
  22277. ' "",',
  22278. ' {',
  22279. ' Default: -1',
  22280. ' }',
  22281. ' );',
  22282. '});',
  22283. '']),
  22284. LinesToStr([ // $mod.$main
  22285. '']));
  22286. end;
  22287. procedure TTestModule.TestRTTI_DefaultValueInherit;
  22288. begin
  22289. Converter.Options:=Converter.Options-[coNoTypeInfo];
  22290. StartProgram(false);
  22291. Add([
  22292. 'type',
  22293. ' TObject = class',
  22294. ' FA, FB: byte;',
  22295. ' property A: byte read FA default 1;',
  22296. ' property B: byte read FB default 2;',
  22297. ' end;',
  22298. ' TBird = class',
  22299. ' published',
  22300. ' property A;',
  22301. ' property B nodefault;',
  22302. ' end;',
  22303. 'begin']);
  22304. ConvertProgram;
  22305. CheckSource('TestRTTI_DefaultValueInherit',
  22306. LinesToStr([ // statements
  22307. 'rtl.createClass($mod, "TObject", null, function () {',
  22308. ' this.$init = function () {',
  22309. ' this.FA = 0;',
  22310. ' this.FB = 0;',
  22311. ' };',
  22312. ' this.$final = function () {',
  22313. ' };',
  22314. '});',
  22315. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  22316. ' var $r = this.$rtti;',
  22317. ' $r.addProperty(',
  22318. ' "A",',
  22319. ' 0,',
  22320. ' rtl.byte,',
  22321. ' "FA",',
  22322. ' "",',
  22323. ' {',
  22324. ' Default: 1',
  22325. ' }',
  22326. ' );',
  22327. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  22328. '});',
  22329. '']),
  22330. LinesToStr([ // $mod.$main
  22331. '']));
  22332. end;
  22333. procedure TTestModule.TestRTTI_OverrideMethod;
  22334. begin
  22335. Converter.Options:=Converter.Options-[coNoTypeInfo];
  22336. StartProgram(false);
  22337. Add('type');
  22338. Add(' TObject = class');
  22339. Add(' published');
  22340. Add(' procedure DoIt; virtual; abstract;');
  22341. Add(' end;');
  22342. Add(' TSky = class');
  22343. Add(' published');
  22344. Add(' procedure DoIt; override;');
  22345. Add(' end;');
  22346. Add('procedure TSky.DoIt; begin end;');
  22347. Add('begin');
  22348. ConvertProgram;
  22349. CheckSource('TestRTTI_OverrideMethod',
  22350. LinesToStr([ // statements
  22351. 'rtl.createClass($mod, "TObject", null, function () {',
  22352. ' this.$init = function () {',
  22353. ' };',
  22354. ' this.$final = function () {',
  22355. ' };',
  22356. ' var $r = this.$rtti;',
  22357. ' $r.addMethod("DoIt", 0, null);',
  22358. '});',
  22359. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  22360. ' this.DoIt = function () {',
  22361. ' };',
  22362. '});',
  22363. '']),
  22364. LinesToStr([ // $mod.$main
  22365. '']));
  22366. end;
  22367. procedure TTestModule.TestRTTI_OverloadProperty;
  22368. begin
  22369. Converter.Options:=Converter.Options-[coNoTypeInfo];
  22370. StartProgram(false);
  22371. Add('type');
  22372. Add(' TObject = class');
  22373. Add(' protected');
  22374. Add(' FFlag: longint;');
  22375. Add(' published');
  22376. Add(' property Flag: longint read fflag;');
  22377. Add(' end;');
  22378. Add(' TSky = class');
  22379. Add(' published');
  22380. Add(' property FLAG: longint write fflag;');
  22381. Add(' end;');
  22382. Add('begin');
  22383. ConvertProgram;
  22384. CheckSource('TestRTTI_OverrideMethod',
  22385. LinesToStr([ // statements
  22386. 'rtl.createClass($mod, "TObject", null, function () {',
  22387. ' this.$init = function () {',
  22388. ' this.FFlag = 0;',
  22389. ' };',
  22390. ' this.$final = function () {',
  22391. ' };',
  22392. ' var $r = this.$rtti;',
  22393. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  22394. '});',
  22395. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  22396. ' var $r = this.$rtti;',
  22397. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  22398. '});',
  22399. '']),
  22400. LinesToStr([ // $mod.$main
  22401. '']));
  22402. end;
  22403. procedure TTestModule.TestRTTI_ClassForward;
  22404. begin
  22405. Converter.Options:=Converter.Options-[coNoTypeInfo];
  22406. StartProgram(false);
  22407. Add('type');
  22408. Add(' TObject = class end;');
  22409. Add(' tbridge = class;');
  22410. Add(' TProc = function: tbridge;');
  22411. Add(' TOger = class');
  22412. Add(' published');
  22413. Add(' FBridge: tbridge;');
  22414. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  22415. Add(' property Bridge: tbridge read fbridge write setbridge;');
  22416. Add(' end;');
  22417. Add(' TBridge = class');
  22418. Add(' FOger: toger;');
  22419. Add(' end;');
  22420. Add('var p: Pointer;');
  22421. Add(' b: tbridge;');
  22422. Add('begin');
  22423. Add(' p:=typeinfo(tbridge);');
  22424. Add(' p:=typeinfo(b);');
  22425. ConvertProgram;
  22426. CheckSource('TestRTTI_ClassForward',
  22427. LinesToStr([ // statements
  22428. 'rtl.createClass($mod, "TObject", null, function () {',
  22429. ' this.$init = function () {',
  22430. ' };',
  22431. ' this.$final = function () {',
  22432. ' };',
  22433. '});',
  22434. '$mod.$rtti.$Class("TBridge");',
  22435. '$mod.$rtti.$ProcVar("TProc", {',
  22436. ' procsig: rtl.newTIProcSig(null, $mod.$rtti["TBridge"])',
  22437. '});',
  22438. 'rtl.createClass($mod, "TOger", $mod.TObject, function () {',
  22439. ' this.$init = function () {',
  22440. ' $mod.TObject.$init.call(this);',
  22441. ' this.FBridge = null;',
  22442. ' };',
  22443. ' this.$final = function () {',
  22444. ' this.FBridge = undefined;',
  22445. ' $mod.TObject.$final.call(this);',
  22446. ' };',
  22447. ' var $r = this.$rtti;',
  22448. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  22449. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  22450. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  22451. '});',
  22452. 'rtl.createClass($mod, "TBridge", $mod.TObject, function () {',
  22453. ' this.$init = function () {',
  22454. ' $mod.TObject.$init.call(this);',
  22455. ' this.FOger = null;',
  22456. ' };',
  22457. ' this.$final = function () {',
  22458. ' this.FOger = undefined;',
  22459. ' $mod.TObject.$final.call(this);',
  22460. ' };',
  22461. '});',
  22462. 'this.p = null;',
  22463. 'this.b = null;',
  22464. '']),
  22465. LinesToStr([ // $mod.$main
  22466. '$mod.p = $mod.$rtti["TBridge"];',
  22467. '$mod.p = $mod.b.$rtti;',
  22468. '']));
  22469. end;
  22470. procedure TTestModule.TestRTTI_ClassOf;
  22471. begin
  22472. Converter.Options:=Converter.Options-[coNoTypeInfo];
  22473. StartProgram(false);
  22474. Add('type');
  22475. Add(' TClass = class of tobject;');
  22476. Add(' TProcA = function: TClass;');
  22477. Add(' TObject = class');
  22478. Add(' published');
  22479. Add(' C: tclass;');
  22480. Add(' end;');
  22481. Add(' tfox = class;');
  22482. Add(' TBird = class end;');
  22483. Add(' TBirds = class of tbird;');
  22484. Add(' TFox = class end;');
  22485. Add(' TFoxes = class of tfox;');
  22486. Add(' TCows = class of TCow;');
  22487. Add(' TCow = class;');
  22488. Add(' TCow = class end;');
  22489. Add('begin');
  22490. ConvertProgram;
  22491. CheckSource('TestRTTI_ClassOf',
  22492. LinesToStr([ // statements
  22493. '$mod.$rtti.$Class("TObject");',
  22494. '$mod.$rtti.$ClassRef("TClass", {',
  22495. ' instancetype: $mod.$rtti["TObject"]',
  22496. '});',
  22497. '$mod.$rtti.$ProcVar("TProcA", {',
  22498. ' procsig: rtl.newTIProcSig(null, $mod.$rtti["TClass"])',
  22499. '});',
  22500. 'rtl.createClass($mod, "TObject", null, function () {',
  22501. ' this.$init = function () {',
  22502. ' this.C = null;',
  22503. ' };',
  22504. ' this.$final = function () {',
  22505. ' this.C = undefined;',
  22506. ' };',
  22507. ' var $r = this.$rtti;',
  22508. ' $r.addField("C", $mod.$rtti["TClass"]);',
  22509. '});',
  22510. '$mod.$rtti.$Class("TFox");',
  22511. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  22512. '});',
  22513. '$mod.$rtti.$ClassRef("TBirds", {',
  22514. ' instancetype: $mod.$rtti["TBird"]',
  22515. '});',
  22516. 'rtl.createClass($mod, "TFox", $mod.TObject, function () {',
  22517. '});',
  22518. '$mod.$rtti.$ClassRef("TFoxes", {',
  22519. ' instancetype: $mod.$rtti["TFox"]',
  22520. '});',
  22521. '$mod.$rtti.$Class("TCow");',
  22522. '$mod.$rtti.$ClassRef("TCows", {',
  22523. ' instancetype: $mod.$rtti["TCow"]',
  22524. '});',
  22525. 'rtl.createClass($mod, "TCow", $mod.TObject, function () {',
  22526. '});',
  22527. '']),
  22528. LinesToStr([ // $mod.$main
  22529. '']));
  22530. end;
  22531. procedure TTestModule.TestRTTI_Record;
  22532. begin
  22533. Converter.Options:=Converter.Options-[coNoTypeInfo];
  22534. StartProgram(false);
  22535. Add('type');
  22536. Add(' integer = longint;');
  22537. Add(' TPoint = record');
  22538. Add(' x,y: integer;');
  22539. Add(' end;');
  22540. Add('var p: pointer;');
  22541. Add(' r: tpoint;');
  22542. Add('begin');
  22543. Add(' p:=typeinfo(tpoint);');
  22544. Add(' p:=typeinfo(r);');
  22545. Add(' p:=typeinfo(r.x);');
  22546. ConvertProgram;
  22547. CheckSource('TestRTTI_Record',
  22548. LinesToStr([ // statements
  22549. 'rtl.recNewT($mod, "TPoint", function () {',
  22550. ' this.x = 0;',
  22551. ' this.y = 0;',
  22552. ' this.$eq = function (b) {',
  22553. ' return (this.x === b.x) && (this.y === b.y);',
  22554. ' };',
  22555. ' this.$assign = function (s) {',
  22556. ' this.x = s.x;',
  22557. ' this.y = s.y;',
  22558. ' return this;',
  22559. ' };',
  22560. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  22561. ' $r.addField("x", rtl.longint);',
  22562. ' $r.addField("y", rtl.longint);',
  22563. '});',
  22564. 'this.p = null;',
  22565. 'this.r = $mod.TPoint.$new();',
  22566. '']),
  22567. LinesToStr([ // $mod.$main
  22568. '$mod.p = $mod.$rtti["TPoint"];',
  22569. '$mod.p = $mod.$rtti["TPoint"];',
  22570. '$mod.p = rtl.longint;',
  22571. '']));
  22572. end;
  22573. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  22574. begin
  22575. Converter.Options:=Converter.Options-[coNoTypeInfo];
  22576. StartProgram(false);
  22577. Add('type');
  22578. Add(' TFloatRec = record');
  22579. Add(' d: array of char;');
  22580. // Add(' i: array of array of longint;');
  22581. Add(' end;');
  22582. Add('var p: pointer;');
  22583. Add(' r: tfloatrec;');
  22584. Add('begin');
  22585. Add(' p:=typeinfo(tfloatrec);');
  22586. Add(' p:=typeinfo(r);');
  22587. Add(' p:=typeinfo(r.d);');
  22588. ConvertProgram;
  22589. CheckSource('TestRTTI_Record',
  22590. LinesToStr([ // statements
  22591. 'rtl.recNewT($mod, "TFloatRec", function () {',
  22592. ' this.d = [];',
  22593. ' this.$eq = function (b) {',
  22594. ' return this.d === b.d;',
  22595. ' };',
  22596. ' this.$assign = function (s) {',
  22597. ' this.d = s.d;',
  22598. ' return this;',
  22599. ' };',
  22600. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  22601. ' eltype: rtl.char',
  22602. ' });',
  22603. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  22604. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  22605. '});',
  22606. 'this.p = null;',
  22607. 'this.r = $mod.TFloatRec.$new();',
  22608. '']),
  22609. LinesToStr([ // $mod.$main
  22610. '$mod.p = $mod.$rtti["TFloatRec"];',
  22611. '$mod.p = $mod.$rtti["TFloatRec"];',
  22612. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  22613. '']));
  22614. end;
  22615. procedure TTestModule.TestRTTI_LocalTypes;
  22616. begin
  22617. Converter.Options:=Converter.Options-[coNoTypeInfo];
  22618. StartProgram(false);
  22619. Add([
  22620. 'procedure DoIt;',
  22621. 'type',
  22622. ' integer = longint;',
  22623. ' TPoint = record',
  22624. ' x,y: integer;',
  22625. ' end;',
  22626. 'var p: TPoint;',
  22627. 'begin',
  22628. 'end;',
  22629. 'begin']);
  22630. ConvertProgram;
  22631. CheckSource('TestRTTI_LocalTypes',
  22632. LinesToStr([ // statements
  22633. 'var TPoint = rtl.recNewT(null, "", function () {',
  22634. ' this.x = 0;',
  22635. ' this.y = 0;',
  22636. ' this.$eq = function (b) {',
  22637. ' return (this.x === b.x) && (this.y === b.y);',
  22638. ' };',
  22639. ' this.$assign = function (s) {',
  22640. ' this.x = s.x;',
  22641. ' this.y = s.y;',
  22642. ' return this;',
  22643. ' };',
  22644. '});',
  22645. 'this.DoIt = function () {',
  22646. ' var p = TPoint.$new();',
  22647. '};',
  22648. '']),
  22649. LinesToStr([ // $mod.$main
  22650. '']));
  22651. end;
  22652. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  22653. begin
  22654. Converter.Options:=Converter.Options-[coNoTypeInfo];
  22655. StartProgram(false);
  22656. Add([
  22657. 'type',
  22658. ' TCaption = string;',
  22659. ' TYesNo = boolean;',
  22660. ' TLetter = char;',
  22661. ' TFloat = double;',
  22662. ' TPtr = pointer;',
  22663. ' TShortInt = shortint;',
  22664. ' TByte = byte;',
  22665. ' TSmallInt = smallint;',
  22666. ' TWord = word;',
  22667. ' TInt32 = longint;',
  22668. ' TDWord = longword;',
  22669. ' TValue = jsvalue;',
  22670. 'var p: TPtr;',
  22671. 'begin',
  22672. ' p:=typeinfo(string);',
  22673. ' p:=typeinfo(tcaption);',
  22674. ' p:=typeinfo(boolean);',
  22675. ' p:=typeinfo(tyesno);',
  22676. ' p:=typeinfo(char);',
  22677. ' p:=typeinfo(tletter);',
  22678. ' p:=typeinfo(double);',
  22679. ' p:=typeinfo(tfloat);',
  22680. ' p:=typeinfo(pointer);',
  22681. ' p:=typeinfo(tptr);',
  22682. ' p:=typeinfo(shortint);',
  22683. ' p:=typeinfo(tshortint);',
  22684. ' p:=typeinfo(byte);',
  22685. ' p:=typeinfo(tbyte);',
  22686. ' p:=typeinfo(smallint);',
  22687. ' p:=typeinfo(tsmallint);',
  22688. ' p:=typeinfo(word);',
  22689. ' p:=typeinfo(tword);',
  22690. ' p:=typeinfo(longword);',
  22691. ' p:=typeinfo(tdword);',
  22692. ' p:=typeinfo(jsvalue);',
  22693. ' p:=typeinfo(tvalue);',
  22694. '']);
  22695. ConvertProgram;
  22696. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  22697. LinesToStr([ // statements
  22698. 'this.p = null;',
  22699. '']),
  22700. LinesToStr([ // $mod.$main
  22701. '$mod.p = rtl.string;',
  22702. '$mod.p = rtl.string;',
  22703. '$mod.p = rtl.boolean;',
  22704. '$mod.p = rtl.boolean;',
  22705. '$mod.p = rtl.char;',
  22706. '$mod.p = rtl.char;',
  22707. '$mod.p = rtl.double;',
  22708. '$mod.p = rtl.double;',
  22709. '$mod.p = rtl.pointer;',
  22710. '$mod.p = rtl.pointer;',
  22711. '$mod.p = rtl.shortint;',
  22712. '$mod.p = rtl.shortint;',
  22713. '$mod.p = rtl.byte;',
  22714. '$mod.p = rtl.byte;',
  22715. '$mod.p = rtl.smallint;',
  22716. '$mod.p = rtl.smallint;',
  22717. '$mod.p = rtl.word;',
  22718. '$mod.p = rtl.word;',
  22719. '$mod.p = rtl.longword;',
  22720. '$mod.p = rtl.longword;',
  22721. '$mod.p = rtl.jsvalue;',
  22722. '$mod.p = rtl.jsvalue;',
  22723. '']));
  22724. end;
  22725. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  22726. begin
  22727. Converter.Options:=Converter.Options-[coNoTypeInfo];
  22728. StartProgram(false);
  22729. Add([
  22730. 'type',
  22731. ' TCaption = type string;',
  22732. ' TYesNo = type boolean;',
  22733. ' TLetter = type char;',
  22734. ' TFloat = type double;',
  22735. ' TPtr = type pointer;',
  22736. ' TShortInt = type shortint;',
  22737. ' TByte = type byte;',
  22738. ' TSmallInt = type smallint;',
  22739. ' TWord = type word;',
  22740. ' TInt32 = type longint;',
  22741. ' TDWord = type longword;',
  22742. ' TValue = type jsvalue;',
  22743. ' TAliasValue = type TValue;',
  22744. 'var',
  22745. ' p: TPtr;',
  22746. ' a: TAliasValue;',
  22747. 'begin',
  22748. ' p:=typeinfo(tcaption);',
  22749. ' p:=typeinfo(tyesno);',
  22750. ' p:=typeinfo(tletter);',
  22751. ' p:=typeinfo(tfloat);',
  22752. ' p:=typeinfo(tptr);',
  22753. ' p:=typeinfo(tshortint);',
  22754. ' p:=typeinfo(tbyte);',
  22755. ' p:=typeinfo(tsmallint);',
  22756. ' p:=typeinfo(tword);',
  22757. ' p:=typeinfo(tdword);',
  22758. ' p:=typeinfo(tvalue);',
  22759. ' p:=typeinfo(taliasvalue);',
  22760. ' p:=typeinfo(a);',
  22761. '']);
  22762. ConvertProgram;
  22763. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  22764. LinesToStr([ // statements
  22765. '$mod.$rtti.$inherited("TCaption", rtl.string, {});',
  22766. '$mod.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  22767. '$mod.$rtti.$inherited("TLetter", rtl.char, {});',
  22768. '$mod.$rtti.$inherited("TFloat", rtl.double, {});',
  22769. '$mod.$rtti.$inherited("TPtr", rtl.pointer, {});',
  22770. '$mod.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  22771. '$mod.$rtti.$inherited("TByte", rtl.byte, {});',
  22772. '$mod.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  22773. '$mod.$rtti.$inherited("TWord", rtl.word, {});',
  22774. '$mod.$rtti.$inherited("TInt32", rtl.longint, {});',
  22775. '$mod.$rtti.$inherited("TDWord", rtl.longword, {});',
  22776. '$mod.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  22777. '$mod.$rtti.$inherited("TAliasValue", $mod.$rtti["TValue"], {});',
  22778. 'this.p = null;',
  22779. 'this.a = undefined;',
  22780. '']),
  22781. LinesToStr([ // $mod.$main
  22782. '$mod.p = $mod.$rtti["TCaption"];',
  22783. '$mod.p = $mod.$rtti["TYesNo"];',
  22784. '$mod.p = $mod.$rtti["TLetter"];',
  22785. '$mod.p = $mod.$rtti["TFloat"];',
  22786. '$mod.p = $mod.$rtti["TPtr"];',
  22787. '$mod.p = $mod.$rtti["TShortInt"];',
  22788. '$mod.p = $mod.$rtti["TByte"];',
  22789. '$mod.p = $mod.$rtti["TSmallInt"];',
  22790. '$mod.p = $mod.$rtti["TWord"];',
  22791. '$mod.p = $mod.$rtti["TDWord"];',
  22792. '$mod.p = $mod.$rtti["TValue"];',
  22793. '$mod.p = $mod.$rtti["TAliasValue"];',
  22794. '$mod.p = $mod.$rtti["TAliasValue"];',
  22795. '']));
  22796. end;
  22797. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  22798. begin
  22799. Converter.Options:=Converter.Options-[coNoTypeInfo];
  22800. StartProgram(false);
  22801. Add('procedure DoIt;');
  22802. Add('type');
  22803. Add(' integer = longint;');
  22804. Add(' TPoint = record');
  22805. Add(' x,y: integer;');
  22806. Add(' end;');
  22807. Add('var p: pointer;');
  22808. Add('begin');
  22809. Add(' p:=typeinfo(tpoint);');
  22810. Add('end;');
  22811. Add('begin');
  22812. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  22813. ConvertProgram;
  22814. end;
  22815. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  22816. begin
  22817. Converter.Options:=Converter.Options-[coNoTypeInfo];
  22818. StartProgram(false);
  22819. Add([
  22820. '{$modeswitch externalclass}',
  22821. 'type',
  22822. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  22823. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  22824. ' TFlag = (up,down);',
  22825. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  22826. ' TFlags = set of TFlag;',
  22827. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  22828. 'var',
  22829. ' ti: TTypeInfo;',
  22830. ' tiInt: TTypeInfoInteger;',
  22831. ' tiEnum: TTypeInfoEnum;',
  22832. ' tiSet: TTypeInfoSet;',
  22833. 'begin',
  22834. ' ti:=typeinfo(string);',
  22835. ' ti:=typeinfo(boolean);',
  22836. ' ti:=typeinfo(char);',
  22837. ' ti:=typeinfo(double);',
  22838. ' tiInt:=typeinfo(shortint);',
  22839. ' tiInt:=typeinfo(byte);',
  22840. ' tiInt:=typeinfo(smallint);',
  22841. ' tiInt:=typeinfo(word);',
  22842. ' tiInt:=typeinfo(longint);',
  22843. ' tiInt:=typeinfo(longword);',
  22844. ' ti:=typeinfo(jsvalue);',
  22845. ' tiEnum:=typeinfo(tflag);',
  22846. ' tiSet:=typeinfo(tflags);']);
  22847. ConvertProgram;
  22848. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  22849. LinesToStr([ // statements
  22850. 'this.TFlag = {',
  22851. ' "0": "up",',
  22852. ' up: 0,',
  22853. ' "1": "down",',
  22854. ' down: 1',
  22855. '};',
  22856. '$mod.$rtti.$Enum("TFlag", {',
  22857. ' minvalue: 0,',
  22858. ' maxvalue: 1,',
  22859. ' ordtype: 1,',
  22860. ' enumtype: this.TFlag',
  22861. '});',
  22862. '$mod.$rtti.$Set("TFlags", {',
  22863. ' comptype: $mod.$rtti["TFlag"]',
  22864. '});',
  22865. 'this.ti = null;',
  22866. 'this.tiInt = null;',
  22867. 'this.tiEnum = null;',
  22868. 'this.tiSet = null;',
  22869. '']),
  22870. LinesToStr([ // $mod.$main
  22871. '$mod.ti = rtl.string;',
  22872. '$mod.ti = rtl.boolean;',
  22873. '$mod.ti = rtl.char;',
  22874. '$mod.ti = rtl.double;',
  22875. '$mod.tiInt = rtl.shortint;',
  22876. '$mod.tiInt = rtl.byte;',
  22877. '$mod.tiInt = rtl.smallint;',
  22878. '$mod.tiInt = rtl.word;',
  22879. '$mod.tiInt = rtl.longint;',
  22880. '$mod.tiInt = rtl.longword;',
  22881. '$mod.ti = rtl.jsvalue;',
  22882. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  22883. '$mod.tiSet = $mod.$rtti["TFlags"];',
  22884. '']));
  22885. end;
  22886. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  22887. begin
  22888. Converter.Options:=Converter.Options-[coNoTypeInfo];
  22889. StartProgram(false);
  22890. Add('{$modeswitch externalclass}');
  22891. Add('type');
  22892. Add(' TTypeInfo = class external name ''rtl.tTypeInfo'' end;');
  22893. Add(' TStaticArr = array[boolean] of string;');
  22894. Add(' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;');
  22895. Add(' TDynArr = array of string;');
  22896. Add(' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;');
  22897. Add(' TProc = procedure;');
  22898. Add(' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;');
  22899. Add(' TMethod = procedure of object;');
  22900. Add(' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;');
  22901. Add('var');
  22902. Add(' StaticArray: TStaticArr;');
  22903. Add(' tiStaticArray: TTypeInfoStaticArray;');
  22904. Add(' DynArray: TDynArr;');
  22905. Add(' tiDynArray: TTypeInfoDynArray;');
  22906. Add(' ProcVar: TProc;');
  22907. Add(' tiProcVar: TTypeInfoProcVar;');
  22908. Add(' MethodVar: TMethod;');
  22909. Add(' tiMethodVar: TTypeInfoMethodVar;');
  22910. Add('begin');
  22911. Add(' tiStaticArray:=typeinfo(StaticArray);');
  22912. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  22913. Add(' tiDynArray:=typeinfo(DynArray);');
  22914. Add(' tiDynArray:=typeinfo(TDynArr);');
  22915. Add(' tiProcVar:=typeinfo(ProcVar);');
  22916. Add(' tiProcVar:=typeinfo(TProc);');
  22917. Add(' tiMethodVar:=typeinfo(MethodVar);');
  22918. Add(' tiMethodVar:=typeinfo(TMethod);');
  22919. ConvertProgram;
  22920. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  22921. LinesToStr([ // statements
  22922. ' $mod.$rtti.$StaticArray("TStaticArr", {',
  22923. ' dims: [2],',
  22924. ' eltype: rtl.string',
  22925. '});',
  22926. '$mod.$rtti.$DynArray("TDynArr", {',
  22927. ' eltype: rtl.string',
  22928. '});',
  22929. '$mod.$rtti.$ProcVar("TProc", {',
  22930. ' procsig: rtl.newTIProcSig(null)',
  22931. '});',
  22932. '$mod.$rtti.$MethodVar("TMethod", {',
  22933. ' procsig: rtl.newTIProcSig(null),',
  22934. ' methodkind: 0',
  22935. '});',
  22936. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  22937. 'this.tiStaticArray = null;',
  22938. 'this.DynArray = [];',
  22939. 'this.tiDynArray = null;',
  22940. 'this.ProcVar = null;',
  22941. 'this.tiProcVar = null;',
  22942. 'this.MethodVar = null;',
  22943. 'this.tiMethodVar = null;',
  22944. '']),
  22945. LinesToStr([ // $mod.$main
  22946. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  22947. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  22948. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  22949. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  22950. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  22951. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  22952. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  22953. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  22954. '']));
  22955. end;
  22956. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  22957. begin
  22958. Converter.Options:=Converter.Options-[coNoTypeInfo];
  22959. StartProgram(false);
  22960. Add('{$modeswitch externalclass}');
  22961. Add('type');
  22962. Add(' TTypeInfo = class external name ''rtl.tTypeInfo'' end;');
  22963. Add(' TRec = record end;');
  22964. Add(' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;');
  22965. // ToDo: ^PRec
  22966. Add(' TObject = class end;');
  22967. Add(' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;');
  22968. Add(' TClass = class of tobject;');
  22969. Add(' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;');
  22970. Add(' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;');
  22971. Add('var');
  22972. Add(' Rec: trec;');
  22973. Add(' tiRecord: ttypeinforecord;');
  22974. Add(' Obj: tobject;');
  22975. Add(' tiClass: ttypeinfoclass;');
  22976. Add(' aClass: tclass;');
  22977. Add(' tiClassRef: ttypeinfoclassref;');
  22978. // ToDo: ^PRec
  22979. Add(' tiPointer: ttypeinfopointer;');
  22980. Add('begin');
  22981. Add(' tirecord:=typeinfo(trec);');
  22982. Add(' tirecord:=typeinfo(trec);');
  22983. Add(' ticlass:=typeinfo(obj);');
  22984. Add(' ticlass:=typeinfo(tobject);');
  22985. Add(' ticlass:=typeinfo(aclass);');
  22986. Add(' ticlassref:=typeinfo(tclass);');
  22987. ConvertProgram;
  22988. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  22989. LinesToStr([ // statements
  22990. 'rtl.recNewT($mod, "TRec", function () {',
  22991. ' this.$eq = function (b) {',
  22992. ' return true;',
  22993. ' };',
  22994. ' this.$assign = function (s) {',
  22995. ' return this;',
  22996. ' };',
  22997. ' $mod.$rtti.$Record("TRec", {});',
  22998. '});',
  22999. 'rtl.createClass($mod, "TObject", null, function () {',
  23000. ' this.$init = function () {',
  23001. ' };',
  23002. ' this.$final = function () {',
  23003. ' };',
  23004. '});',
  23005. '$mod.$rtti.$ClassRef("TClass", {',
  23006. ' instancetype: $mod.$rtti["TObject"]',
  23007. '});',
  23008. 'this.Rec = $mod.TRec.$new();',
  23009. 'this.tiRecord = null;',
  23010. 'this.Obj = null;',
  23011. 'this.tiClass = null;',
  23012. 'this.aClass = null;',
  23013. 'this.tiClassRef = null;',
  23014. 'this.tiPointer = null;',
  23015. '']),
  23016. LinesToStr([ // $mod.$main
  23017. '$mod.tiRecord = $mod.$rtti["TRec"];',
  23018. '$mod.tiRecord = $mod.$rtti["TRec"];',
  23019. '$mod.tiClass = $mod.Obj.$rtti;',
  23020. '$mod.tiClass = $mod.$rtti["TObject"];',
  23021. '$mod.tiClass = $mod.aClass.$rtti;',
  23022. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  23023. '']));
  23024. end;
  23025. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  23026. begin
  23027. Converter.Options:=Converter.Options-[coNoTypeInfo];
  23028. StartProgram(false);
  23029. Add([
  23030. '{$modeswitch externalclass}',
  23031. 'type',
  23032. ' TClass = class of tobject;',
  23033. ' TObject = class',
  23034. ' function MyClass: TClass;',
  23035. ' class function ClassType: TClass;',
  23036. ' end;',
  23037. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  23038. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  23039. 'function TObject.MyClass: TClass;',
  23040. 'var t: TTypeInfoClass;',
  23041. 'begin',
  23042. ' t:=TypeInfo(Self);',
  23043. ' t:=TypeInfo(Result);',
  23044. ' t:=TypeInfo(TObject);',
  23045. 'end;',
  23046. 'class function TObject.ClassType: TClass;',
  23047. 'var t: TTypeInfoClass;',
  23048. 'begin',
  23049. ' t:=TypeInfo(Self);',
  23050. ' t:=TypeInfo(Result);',
  23051. 'end;',
  23052. 'var',
  23053. ' Obj: TObject;',
  23054. ' t: TTypeInfoClass;',
  23055. 'begin',
  23056. ' t:=TypeInfo(TObject.ClassType);',
  23057. ' t:=TypeInfo(Obj.ClassType);',
  23058. ' t:=TypeInfo(Obj.MyClass);',
  23059. '']);
  23060. ConvertProgram;
  23061. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  23062. LinesToStr([ // statements
  23063. '$mod.$rtti.$Class("TObject");',
  23064. '$mod.$rtti.$ClassRef("TClass", {',
  23065. ' instancetype: $mod.$rtti["TObject"]',
  23066. '});',
  23067. 'rtl.createClass($mod, "TObject", null, function () {',
  23068. ' this.$init = function () {',
  23069. ' };',
  23070. ' this.$final = function () {',
  23071. ' };',
  23072. ' this.MyClass = function () {',
  23073. ' var Result = null;',
  23074. ' var t = null;',
  23075. ' t = this.$rtti;',
  23076. ' t = Result.$rtti;',
  23077. ' t = $mod.$rtti["TObject"];',
  23078. ' return Result;',
  23079. ' };',
  23080. ' this.ClassType = function () {',
  23081. ' var Result = null;',
  23082. ' var t = null;',
  23083. ' t = this.$rtti;',
  23084. ' t = Result.$rtti;',
  23085. ' return Result;',
  23086. ' };',
  23087. '});',
  23088. 'this.Obj = null;',
  23089. 'this.t = null;',
  23090. '']),
  23091. LinesToStr([ // $mod.$main
  23092. '$mod.t = $mod.TObject.ClassType().$rtti;',
  23093. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  23094. '$mod.t = $mod.Obj.MyClass().$rtti;',
  23095. '']));
  23096. end;
  23097. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  23098. begin
  23099. Converter.Options:=Converter.Options-[coNoTypeInfo];
  23100. AddModuleWithIntfImplSrc('typinfo.pas',
  23101. LinesToStr([
  23102. '{$modeswitch externalclass}',
  23103. 'type',
  23104. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  23105. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  23106. '']),
  23107. '');
  23108. AddModuleWithIntfImplSrc('unit2.pas',
  23109. LinesToStr([
  23110. 'uses typinfo;',
  23111. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  23112. 'procedure DoPtr(p: PTypeInfo);',
  23113. 'procedure DoInfo(t: TTypeInfo);',
  23114. 'procedure DoInt(t: TTypeInfoInteger);',
  23115. '']),
  23116. LinesToStr([
  23117. 'procedure DoPtr(p: PTypeInfo);',
  23118. 'begin end;',
  23119. 'procedure DoInfo(t: TTypeInfo);',
  23120. 'begin end;',
  23121. 'procedure DoInt(t: TTypeInfoInteger);',
  23122. 'begin end;',
  23123. '']));
  23124. StartUnit(true);
  23125. Add([
  23126. 'interface',
  23127. 'uses unit2;', // does not use unit typinfo
  23128. 'implementation',
  23129. 'var',
  23130. ' i: byte;',
  23131. ' p: pointer;',
  23132. ' t: PTypeInfo;',
  23133. 'initialization',
  23134. ' p:=typeinfo(i);',
  23135. ' t:=typeinfo(i);',
  23136. ' if p=t then ;',
  23137. ' if p=typeinfo(i) then ;',
  23138. ' if typeinfo(i)=p then ;',
  23139. ' if t=typeinfo(i) then ;',
  23140. ' if typeinfo(i)=t then ;',
  23141. ' DoPtr(p);',
  23142. ' DoPtr(t);',
  23143. ' DoPtr(typeinfo(i));',
  23144. ' DoInfo(p);',
  23145. ' DoInfo(t);',
  23146. ' DoInfo(typeinfo(i));',
  23147. ' DoInt(typeinfo(i));',
  23148. '']);
  23149. ConvertUnit;
  23150. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  23151. LinesToStr([ // statements
  23152. 'var $impl = $mod.$impl;',
  23153. '']),
  23154. LinesToStr([ // this.$init
  23155. '$impl.p = rtl.byte;',
  23156. '$impl.t = rtl.byte;',
  23157. 'if ($impl.p === $impl.t) ;',
  23158. 'if ($impl.p === rtl.byte) ;',
  23159. 'if (rtl.byte === $impl.p) ;',
  23160. 'if ($impl.t === rtl.byte) ;',
  23161. 'if (rtl.byte === $impl.t) ;',
  23162. 'pas.unit2.DoPtr($impl.p);',
  23163. 'pas.unit2.DoPtr($impl.t);',
  23164. 'pas.unit2.DoPtr(rtl.byte);',
  23165. 'pas.unit2.DoInfo($impl.p);',
  23166. 'pas.unit2.DoInfo($impl.t);',
  23167. 'pas.unit2.DoInfo(rtl.byte);',
  23168. 'pas.unit2.DoInt(rtl.byte);',
  23169. '']),
  23170. LinesToStr([ // implementation
  23171. '$impl.i = 0;',
  23172. '$impl.p = null;',
  23173. '$impl.t = null;',
  23174. '']) );
  23175. end;
  23176. procedure TTestModule.TestRTTI_Interface_Corba;
  23177. begin
  23178. Converter.Options:=Converter.Options-[coNoTypeInfo];
  23179. StartProgram(false);
  23180. Add([
  23181. '{$interfaces corba}',
  23182. '{$modeswitch externalclass}',
  23183. 'type',
  23184. ' IUnknown = interface',
  23185. ' end;',
  23186. ' IBird = interface',
  23187. ' function GetItem: longint;',
  23188. ' procedure SetItem(Value: longint);',
  23189. ' property Item: longint read GetItem write SetItem;',
  23190. ' end;',
  23191. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  23192. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  23193. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  23194. 'var',
  23195. ' i: IBird;',
  23196. ' t: TTypeInfoInterface;',
  23197. 'begin',
  23198. ' t:=TypeInfo(IBird);',
  23199. ' t:=TypeInfo(i);',
  23200. ' DoIt(t);',
  23201. ' DoIt(TypeInfo(IBird));',
  23202. '']);
  23203. ConvertProgram;
  23204. CheckSource('TestRTTI_Interface_Corba',
  23205. LinesToStr([ // statements
  23206. 'rtl.createInterface(',
  23207. ' $mod,',
  23208. ' "IUnknown",',
  23209. ' "{B92D5841-758A-322B-B800-000000000000}",',
  23210. ' [],',
  23211. ' null,',
  23212. ' function () {',
  23213. ' }',
  23214. ');',
  23215. 'rtl.createInterface(',
  23216. ' $mod,',
  23217. ' "IBird",',
  23218. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  23219. ' ["GetItem", "SetItem"],',
  23220. ' null,',
  23221. ' function () {',
  23222. ' var $r = this.$rtti;',
  23223. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  23224. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  23225. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  23226. ' }',
  23227. ');',
  23228. 'this.DoIt = function (t) {',
  23229. '}; ',
  23230. 'this.i = null;',
  23231. 'this.t = null;',
  23232. '']),
  23233. LinesToStr([ // $mod.$main
  23234. '$mod.t = $mod.$rtti["IBird"];',
  23235. '$mod.t = $mod.i.$rtti;',
  23236. '$mod.DoIt($mod.t);',
  23237. '$mod.DoIt($mod.$rtti["IBird"]);',
  23238. '']));
  23239. end;
  23240. procedure TTestModule.TestRTTI_Interface_COM;
  23241. begin
  23242. Converter.Options:=Converter.Options-[coNoTypeInfo];
  23243. StartProgram(false);
  23244. Add([
  23245. '{$interfaces com}',
  23246. '{$modeswitch externalclass}',
  23247. 'type',
  23248. ' TGuid = record end;',
  23249. ' integer = longint;',
  23250. ' IUnknown = interface',
  23251. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  23252. ' function _AddRef: Integer;',
  23253. ' function _Release: Integer;',
  23254. ' end;',
  23255. ' IBird = interface',
  23256. ' function GetItem: longint;',
  23257. ' procedure SetItem(Value: longint);',
  23258. ' property Item: longint read GetItem write SetItem;',
  23259. ' end;',
  23260. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  23261. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  23262. 'var',
  23263. ' i: IBird;',
  23264. ' t: TTypeInfoInterface;',
  23265. 'begin',
  23266. ' t:=TypeInfo(IBird);',
  23267. ' t:=TypeInfo(i);',
  23268. '']);
  23269. ConvertProgram;
  23270. CheckSource('TestRTTI_Interface_COM',
  23271. LinesToStr([ // statements
  23272. 'rtl.recNewT($mod, "TGuid", function () {',
  23273. ' this.$eq = function (b) {',
  23274. ' return true;',
  23275. ' };',
  23276. ' this.$assign = function (s) {',
  23277. ' return this;',
  23278. ' };',
  23279. ' $mod.$rtti.$Record("TGuid", {});',
  23280. '});',
  23281. 'rtl.createInterface(',
  23282. ' $mod,',
  23283. ' "IUnknown",',
  23284. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  23285. ' ["QueryInterface", "_AddRef", "_Release"],',
  23286. ' null,',
  23287. ' function () {',
  23288. ' this.$kind = "com";',
  23289. ' var $r = this.$rtti;',
  23290. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  23291. ' $r.addMethod("_AddRef", 1, null, rtl.longint);',
  23292. ' $r.addMethod("_Release", 1, null, rtl.longint);',
  23293. ' }',
  23294. ');',
  23295. 'rtl.createInterface(',
  23296. ' $mod,',
  23297. ' "IBird",',
  23298. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  23299. ' ["GetItem", "SetItem"],',
  23300. ' $mod.IUnknown,',
  23301. ' function () {',
  23302. ' var $r = this.$rtti;',
  23303. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  23304. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  23305. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  23306. ' }',
  23307. ');',
  23308. 'this.i = null;',
  23309. 'this.t = null;',
  23310. '']),
  23311. LinesToStr([ // $mod.$main
  23312. '$mod.t = $mod.$rtti["IBird"];',
  23313. '$mod.t = $mod.i.$rtti;',
  23314. '']));
  23315. end;
  23316. procedure TTestModule.TestResourcestringProgram;
  23317. begin
  23318. StartProgram(false);
  23319. Add([
  23320. 'const Bar = ''bar'';',
  23321. 'resourcestring',
  23322. ' Red = ''red'';',
  23323. ' Foobar = ''fOo''+bar;',
  23324. 'var s: string;',
  23325. ' c: char;',
  23326. 'begin',
  23327. ' s:=red;',
  23328. ' s:=test1.red;',
  23329. ' c:=red[1];',
  23330. ' c:=test1.red[2];',
  23331. ' if red=foobar then ;',
  23332. ' if red[3]=red[4] then ;']);
  23333. ConvertProgram;
  23334. CheckSource('TestResourcestringProgram',
  23335. LinesToStr([ // statements
  23336. 'this.Bar = "bar";',
  23337. 'this.s = "";',
  23338. 'this.c = "";',
  23339. '$mod.$resourcestrings = {',
  23340. ' Red: {',
  23341. ' org: "red"',
  23342. ' },',
  23343. ' Foobar: {',
  23344. ' org: "fOobar"',
  23345. ' }',
  23346. '};',
  23347. '']),
  23348. LinesToStr([ // $mod.$main
  23349. '$mod.s = rtl.getResStr(pas.program, "Red");',
  23350. '$mod.s = rtl.getResStr(pas.program, "Red");',
  23351. '$mod.c = rtl.getResStr(pas.program, "Red").charAt(0);',
  23352. '$mod.c = rtl.getResStr(pas.program, "Red").charAt(1);',
  23353. 'if (rtl.getResStr(pas.program, "Red") === rtl.getResStr(pas.program, "Foobar")) ;',
  23354. 'if (rtl.getResStr(pas.program, "Red").charAt(2) === rtl.getResStr(pas.program, "Red").charAt(3)) ;',
  23355. '']));
  23356. end;
  23357. procedure TTestModule.TestResourcestringUnit;
  23358. begin
  23359. StartUnit(false);
  23360. Add([
  23361. 'interface',
  23362. 'const Red = ''rEd'';',
  23363. 'resourcestring',
  23364. ' Blue = ''blue'';',
  23365. ' NotRed = ''not''+Red;',
  23366. 'var s: string;',
  23367. 'implementation',
  23368. 'resourcestring',
  23369. ' ImplGreen = ''green'';',
  23370. 'initialization',
  23371. ' s:=blue+ImplGreen;',
  23372. ' s:=test1.blue+test1.implgreen;',
  23373. ' s:=blue[1]+implgreen[2];']);
  23374. ConvertUnit;
  23375. CheckSource('TestResourcestringUnit',
  23376. LinesToStr([ // statements
  23377. 'this.Red = "rEd";',
  23378. 'this.s = "";',
  23379. '$mod.$resourcestrings = {',
  23380. ' Blue: {',
  23381. ' org: "blue"',
  23382. ' },',
  23383. ' NotRed: {',
  23384. ' org: "notrEd"',
  23385. ' },',
  23386. ' ImplGreen: {',
  23387. ' org: "green"',
  23388. ' }',
  23389. '};',
  23390. '']),
  23391. LinesToStr([ // $mod.$main
  23392. '$mod.s = rtl.getResStr(pas.Test1, "Blue") + rtl.getResStr(pas.Test1, "ImplGreen");',
  23393. '$mod.s = rtl.getResStr(pas.Test1, "Blue") + rtl.getResStr(pas.Test1, "ImplGreen");',
  23394. '$mod.s = rtl.getResStr(pas.Test1, "Blue").charAt(0) + rtl.getResStr(pas.Test1, "ImplGreen").charAt(1);',
  23395. '']));
  23396. end;
  23397. procedure TTestModule.TestResourcestringImplementation;
  23398. begin
  23399. StartUnit(false);
  23400. Add([
  23401. 'interface',
  23402. 'implementation',
  23403. 'resourcestring',
  23404. ' ImplRed = ''red'';']);
  23405. ConvertUnit;
  23406. CheckSource('TestResourcestringImplementation',
  23407. LinesToStr([ // intf statements
  23408. 'var $impl = $mod.$impl;']),
  23409. LinesToStr([ // $mod.$init
  23410. '']),
  23411. LinesToStr([ // impl statements
  23412. '$mod.$resourcestrings = {',
  23413. ' ImplRed: {',
  23414. ' org: "red"',
  23415. ' }',
  23416. '};',
  23417. '']));
  23418. end;
  23419. procedure TTestModule.TestAtributes_Ignore;
  23420. begin
  23421. StartProgram(false);
  23422. Add([
  23423. '{$modeswitch ignoreattributes}',
  23424. 'type',
  23425. ' [custom1, custom2(1+3,''foo'')] [mod1.custom3]',
  23426. ' TObject = class',
  23427. ' [custom5()] FS: string;',
  23428. ' [customProp] property S: string read FS;',
  23429. ' end;',
  23430. 'var',
  23431. ' [custom6]',
  23432. ' o: TObject;',
  23433. 'begin',
  23434. '']);
  23435. ConvertProgram;
  23436. CheckSource('TestAtributes_Ignore',
  23437. LinesToStr([ // statements
  23438. 'rtl.createClass($mod, "TObject", null, function () {',
  23439. ' this.$init = function () {',
  23440. ' this.FS = "";',
  23441. ' };',
  23442. ' this.$final = function () {',
  23443. ' };',
  23444. '});',
  23445. 'this.o = null;',
  23446. '']),
  23447. LinesToStr([ // $mod.$main
  23448. '']));
  23449. end;
  23450. procedure TTestModule.TestAssert;
  23451. begin
  23452. StartProgram(false);
  23453. Add([
  23454. 'procedure DoIt;',
  23455. 'var',
  23456. ' b: boolean;',
  23457. ' s: string;',
  23458. 'begin',
  23459. ' {$Assertions on}',
  23460. ' Assert(b);',
  23461. 'end;',
  23462. 'begin',
  23463. ' DoIt;',
  23464. '']);
  23465. ConvertProgram;
  23466. CheckSource('TestAssert',
  23467. LinesToStr([ // statements
  23468. 'this.DoIt = function () {',
  23469. ' var b = false;',
  23470. ' var s = "";',
  23471. ' if (!b) throw "assert failed";',
  23472. '};',
  23473. '']),
  23474. LinesToStr([ // $mod.$main
  23475. '$mod.DoIt();',
  23476. '']));
  23477. end;
  23478. procedure TTestModule.TestAssert_SysUtils;
  23479. begin
  23480. AddModuleWithIntfImplSrc('SysUtils.pas',
  23481. LinesToStr([
  23482. 'type',
  23483. ' TObject = class',
  23484. ' constructor Create;',
  23485. ' end;',
  23486. ' EAssertionFailed = class',
  23487. ' constructor Create(s: string);',
  23488. ' end;',
  23489. '']),
  23490. LinesToStr([
  23491. 'constructor TObject.Create;',
  23492. 'begin end;',
  23493. 'constructor EAssertionFailed.Create(s: string);',
  23494. 'begin end;',
  23495. '']) );
  23496. StartProgram(true);
  23497. Add([
  23498. 'uses sysutils;',
  23499. 'procedure DoIt;',
  23500. 'var',
  23501. ' b: boolean;',
  23502. ' s: string;',
  23503. 'begin',
  23504. ' {$Assertions on}',
  23505. ' Assert(b);',
  23506. ' Assert(b,''msg'');',
  23507. 'end;',
  23508. 'begin',
  23509. ' DoIt;',
  23510. '']);
  23511. ConvertProgram;
  23512. CheckSource('TestAssert_SysUtils',
  23513. LinesToStr([ // statements
  23514. 'this.DoIt = function () {',
  23515. ' var b = false;',
  23516. ' var s = "";',
  23517. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  23518. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  23519. '};',
  23520. '']),
  23521. LinesToStr([ // $mod.$main
  23522. '$mod.DoIt();',
  23523. '']));
  23524. end;
  23525. procedure TTestModule.TestObjectChecks;
  23526. begin
  23527. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  23528. StartProgram(false);
  23529. Add([
  23530. 'type',
  23531. ' TObject = class',
  23532. ' procedure DoIt;',
  23533. ' end;',
  23534. ' TClass = class of tobject;',
  23535. ' TBird = class',
  23536. ' end;',
  23537. ' TBirdClass = class of TBird;',
  23538. 'var',
  23539. ' o : TObject;',
  23540. ' c: TClass;',
  23541. ' b: TBird;',
  23542. ' bc: TBirdClass;',
  23543. 'procedure TObject.DoIt;',
  23544. 'begin',
  23545. ' b:=TBird(o);',
  23546. 'end;',
  23547. 'begin',
  23548. ' o.DoIt;',
  23549. ' b:=TBird(o);',
  23550. ' bc:=TBirdClass(c);',
  23551. '']);
  23552. ConvertProgram;
  23553. CheckSource('TestCheckMethodCall',
  23554. LinesToStr([ // statements
  23555. 'rtl.createClass($mod, "TObject", null, function () {',
  23556. ' this.$init = function () {',
  23557. ' };',
  23558. ' this.$final = function () {',
  23559. ' };',
  23560. ' this.DoIt = function () {',
  23561. ' rtl.checkMethodCall(this,$mod.TObject);',
  23562. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  23563. ' };',
  23564. '});',
  23565. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  23566. '});',
  23567. 'this.o = null;',
  23568. 'this.c = null;',
  23569. 'this.b = null;',
  23570. 'this.bc = null;',
  23571. '']),
  23572. LinesToStr([ // $mod.$main
  23573. '$mod.o.DoIt();',
  23574. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  23575. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  23576. '']));
  23577. end;
  23578. procedure TTestModule.TestRangeChecks_AssignInt;
  23579. begin
  23580. Scanner.Options:=Scanner.Options+[po_CAssignments];
  23581. StartProgram(false);
  23582. Add([
  23583. '{$R+}',
  23584. 'var',
  23585. ' b: byte = 2;',
  23586. ' w: word = 3;',
  23587. 'procedure DoIt(p: byte);',
  23588. 'begin',
  23589. ' b:=w;',
  23590. ' b+=w;',
  23591. ' b:=1;',
  23592. 'end;',
  23593. '{$R-}',
  23594. 'procedure DoSome;',
  23595. 'begin',
  23596. ' DoIt(w);',
  23597. ' b:=w;',
  23598. ' b:=2;',
  23599. 'end;',
  23600. 'begin',
  23601. '{$R+}',
  23602. '']);
  23603. ConvertProgram;
  23604. CheckSource('TestRangeChecks_AssignInt',
  23605. LinesToStr([ // statements
  23606. 'this.b = 2;',
  23607. 'this.w = 3;',
  23608. 'this.DoIt = function (p) {',
  23609. ' rtl.rc(p, 0, 255);',
  23610. ' $mod.b = rtl.rc($mod.w,0,255);',
  23611. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  23612. ' $mod.b = 1;',
  23613. '};',
  23614. 'this.DoSome = function () {',
  23615. ' $mod.DoIt($mod.w);',
  23616. ' $mod.b = $mod.w;',
  23617. ' $mod.b = 2;',
  23618. '};',
  23619. '']),
  23620. LinesToStr([ // $mod.$main
  23621. '']));
  23622. end;
  23623. procedure TTestModule.TestRangeChecks_AssignIntRange;
  23624. begin
  23625. Scanner.Options:=Scanner.Options+[po_CAssignments];
  23626. StartProgram(false);
  23627. Add([
  23628. '{$R+}',
  23629. 'type Ten = 1..10;',
  23630. 'var',
  23631. ' b: Ten = 2;',
  23632. ' w: Ten = 3;',
  23633. 'procedure DoIt(p: Ten);',
  23634. 'begin',
  23635. ' b:=w;',
  23636. ' b+=w;',
  23637. ' b:=1;',
  23638. 'end;',
  23639. '{$R-}',
  23640. 'procedure DoSome;',
  23641. 'begin',
  23642. ' DoIt(w);',
  23643. ' b:=w;',
  23644. ' b:=2;',
  23645. 'end;',
  23646. 'begin',
  23647. '{$R+}',
  23648. '']);
  23649. ConvertProgram;
  23650. CheckSource('TestRangeChecks_AssignIntRange',
  23651. LinesToStr([ // statements
  23652. 'this.b = 2;',
  23653. 'this.w = 3;',
  23654. 'this.DoIt = function (p) {',
  23655. ' rtl.rc(p, 1, 10);',
  23656. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  23657. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  23658. ' $mod.b = 1;',
  23659. '};',
  23660. 'this.DoSome = function () {',
  23661. ' $mod.DoIt($mod.w);',
  23662. ' $mod.b = $mod.w;',
  23663. ' $mod.b = 2;',
  23664. '};',
  23665. '']),
  23666. LinesToStr([ // $mod.$main
  23667. '']));
  23668. end;
  23669. procedure TTestModule.TestRangeChecks_AssignEnum;
  23670. begin
  23671. StartProgram(false);
  23672. Add([
  23673. '{$R+}',
  23674. 'type TEnum = (red,green);',
  23675. 'var',
  23676. ' e: TEnum = red;',
  23677. 'procedure DoIt(p: TEnum);',
  23678. 'begin',
  23679. ' e:=p;',
  23680. ' p:=TEnum(0);',
  23681. ' p:=succ(e);',
  23682. 'end;',
  23683. '{$R-}',
  23684. 'procedure DoSome;',
  23685. 'begin',
  23686. ' DoIt(e);',
  23687. ' e:=TEnum(1);',
  23688. ' e:=pred(e);',
  23689. 'end;',
  23690. 'begin',
  23691. '{$R+}',
  23692. '']);
  23693. ConvertProgram;
  23694. CheckSource('TestRangeChecks_AssignEnum',
  23695. LinesToStr([ // statements
  23696. 'this.TEnum = {',
  23697. ' "0": "red",',
  23698. ' red: 0,',
  23699. ' "1": "green",',
  23700. ' green: 1',
  23701. '};',
  23702. 'this.e = $mod.TEnum.red;',
  23703. 'this.DoIt = function (p) {',
  23704. ' rtl.rc(p, 0, 1);',
  23705. ' $mod.e = rtl.rc(p, 0, 1);',
  23706. ' p = 0;',
  23707. ' p = rtl.rc($mod.e + 1, 0, 1);',
  23708. '};',
  23709. 'this.DoSome = function () {',
  23710. ' $mod.DoIt($mod.e);',
  23711. ' $mod.e = 1;',
  23712. ' $mod.e = $mod.e - 1;',
  23713. '};',
  23714. '']),
  23715. LinesToStr([ // $mod.$main
  23716. '']));
  23717. end;
  23718. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  23719. begin
  23720. StartProgram(false);
  23721. Add([
  23722. '{$R+}',
  23723. 'type',
  23724. ' TEnum = (red,green);',
  23725. ' TEnumRg = red..green;',
  23726. 'var',
  23727. ' e: TEnumRg = red;',
  23728. 'procedure DoIt(p: TEnumRg);',
  23729. 'begin',
  23730. ' e:=p;',
  23731. ' p:=TEnumRg(0);',
  23732. ' p:=succ(e);',
  23733. 'end;',
  23734. '{$R-}',
  23735. 'procedure DoSome;',
  23736. 'begin',
  23737. ' DoIt(e);',
  23738. ' e:=TEnum(1);',
  23739. ' e:=pred(e);',
  23740. 'end;',
  23741. 'begin',
  23742. '{$R+}',
  23743. '']);
  23744. ConvertProgram;
  23745. CheckSource('TestRangeChecks_AssignEnumRange',
  23746. LinesToStr([ // statements
  23747. 'this.TEnum = {',
  23748. ' "0": "red",',
  23749. ' red: 0,',
  23750. ' "1": "green",',
  23751. ' green: 1',
  23752. '};',
  23753. 'this.e = $mod.TEnum.red;',
  23754. 'this.DoIt = function (p) {',
  23755. ' rtl.rc(p, 0, 1);',
  23756. ' $mod.e = rtl.rc(p, 0, 1);',
  23757. ' p = 0;',
  23758. ' p = rtl.rc($mod.e + 1, 0, 1);',
  23759. '};',
  23760. 'this.DoSome = function () {',
  23761. ' $mod.DoIt($mod.e);',
  23762. ' $mod.e = 1;',
  23763. ' $mod.e = $mod.e - 1;',
  23764. '};',
  23765. '']),
  23766. LinesToStr([ // $mod.$main
  23767. '']));
  23768. end;
  23769. procedure TTestModule.TestRangeChecks_AssignChar;
  23770. begin
  23771. StartProgram(false);
  23772. Add([
  23773. '{$R+}',
  23774. 'type',
  23775. ' TLetter = char;',
  23776. 'var',
  23777. ' b: TLetter = ''2'';',
  23778. ' w: TLetter = ''3'';',
  23779. 'procedure DoIt(p: TLetter);',
  23780. 'begin',
  23781. ' b:=w;',
  23782. ' b:=''1'';',
  23783. 'end;',
  23784. '{$R-}',
  23785. 'procedure DoSome;',
  23786. 'begin',
  23787. ' DoIt(w);',
  23788. ' b:=w;',
  23789. ' b:=''2'';',
  23790. 'end;',
  23791. 'begin',
  23792. '{$R+}',
  23793. '']);
  23794. ConvertProgram;
  23795. CheckSource('TestRangeChecks_AssignChar',
  23796. LinesToStr([ // statements
  23797. 'this.b = "2";',
  23798. 'this.w = "3";',
  23799. 'this.DoIt = function (p) {',
  23800. ' rtl.rcc(p, 0, 65535);',
  23801. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  23802. ' $mod.b = "1";',
  23803. '};',
  23804. 'this.DoSome = function () {',
  23805. ' $mod.DoIt($mod.w);',
  23806. ' $mod.b = $mod.w;',
  23807. ' $mod.b = "2";',
  23808. '};',
  23809. '']),
  23810. LinesToStr([ // $mod.$main
  23811. '']));
  23812. end;
  23813. procedure TTestModule.TestRangeChecks_AssignCharRange;
  23814. begin
  23815. StartProgram(false);
  23816. Add([
  23817. '{$R+}',
  23818. 'type TDigit = ''0''..''9'';',
  23819. 'var',
  23820. ' b: TDigit = ''2'';',
  23821. ' w: TDigit = ''3'';',
  23822. 'procedure DoIt(p: TDigit);',
  23823. 'begin',
  23824. ' b:=w;',
  23825. ' b:=''1'';',
  23826. 'end;',
  23827. '{$R-}',
  23828. 'procedure DoSome;',
  23829. 'begin',
  23830. ' DoIt(w);',
  23831. ' b:=w;',
  23832. ' b:=''2'';',
  23833. 'end;',
  23834. 'begin',
  23835. '{$R+}',
  23836. '']);
  23837. ConvertProgram;
  23838. CheckSource('TestRangeChecks_AssignCharRange',
  23839. LinesToStr([ // statements
  23840. 'this.b = "2";',
  23841. 'this.w = "3";',
  23842. 'this.DoIt = function (p) {',
  23843. ' rtl.rcc(p, 48, 57);',
  23844. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  23845. ' $mod.b = "1";',
  23846. '};',
  23847. 'this.DoSome = function () {',
  23848. ' $mod.DoIt($mod.w);',
  23849. ' $mod.b = $mod.w;',
  23850. ' $mod.b = "2";',
  23851. '};',
  23852. '']),
  23853. LinesToStr([ // $mod.$main
  23854. '']));
  23855. end;
  23856. procedure TTestModule.TestRangeChecks_ArrayIndex;
  23857. begin
  23858. StartProgram(false);
  23859. Add([
  23860. '{$R+}',
  23861. 'type',
  23862. ' Ten = 1..10;',
  23863. ' TArr = array of Ten;',
  23864. ' TArrArr = array of TArr;',
  23865. ' TArrByte = array[byte] of Ten;',
  23866. ' TArrChar = array[''0''..''9''] of Ten;',
  23867. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  23868. ' TObject = class',
  23869. ' A: TArr;',
  23870. ' end;',
  23871. 'procedure DoIt;',
  23872. 'var',
  23873. ' Arr: TArr;',
  23874. ' ArrArr: TArrArr;',
  23875. ' ArrByte: TArrByte;',
  23876. ' ArrChar: TArrChar;',
  23877. ' ArrByteChar: TArrByteChar;',
  23878. ' i: Ten;',
  23879. ' c: char;',
  23880. ' o: tobject;',
  23881. 'begin',
  23882. ' i:=Arr[1];',
  23883. ' i:=ArrByteChar[1,''2''];',
  23884. ' Arr[1]:=Arr[1];',
  23885. ' Arr[i]:=Arr[i];',
  23886. ' ArrByte[3]:=ArrByte[3];',
  23887. ' ArrByte[i]:=ArrByte[i];',
  23888. ' ArrChar[''5'']:=ArrChar[''5''];',
  23889. ' ArrChar[c]:=ArrChar[c];',
  23890. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  23891. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  23892. ' o.a[i]:=o.a[i];',
  23893. 'end;',
  23894. 'begin',
  23895. '']);
  23896. ConvertProgram;
  23897. CheckSource('TestRangeChecks_ArrayIndex',
  23898. LinesToStr([ // statements
  23899. 'rtl.createClass($mod, "TObject", null, function () {',
  23900. ' this.$init = function () {',
  23901. ' this.A = [];',
  23902. ' };',
  23903. ' this.$final = function () {',
  23904. ' this.A = undefined;',
  23905. ' };',
  23906. '});',
  23907. 'this.DoIt = function () {',
  23908. ' var Arr = [];',
  23909. ' var ArrArr = [];',
  23910. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  23911. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  23912. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  23913. ' var i = 0;',
  23914. ' var c = "";',
  23915. ' var o = null;',
  23916. ' i = rtl.rc(Arr[1], 1, 10);',
  23917. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  23918. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  23919. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  23920. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  23921. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  23922. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  23923. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  23924. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  23925. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  23926. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  23927. '};',
  23928. '']),
  23929. LinesToStr([ // $mod.$main
  23930. '']));
  23931. end;
  23932. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  23933. begin
  23934. StartProgram(false);
  23935. Add([
  23936. '{$R+}',
  23937. 'type',
  23938. ' Ten = 1..10;',
  23939. ' TRec = record x: Ten end;',
  23940. ' TArr = array of TRec;',
  23941. ' TArrArr = array of TArr;',
  23942. ' TObject = class',
  23943. ' A: TArr;',
  23944. ' end;',
  23945. 'procedure DoIt;',
  23946. 'var',
  23947. ' Arr: TArr;',
  23948. ' ArrArr: TArrArr;',
  23949. ' i: Ten;',
  23950. ' o: tobject;',
  23951. 'begin',
  23952. ' Arr[1]:=Arr[1];',
  23953. ' Arr[i]:=Arr[i+1];',
  23954. ' o.a[i]:=o.a[i+2];',
  23955. 'end;',
  23956. 'begin',
  23957. '']);
  23958. ConvertProgram;
  23959. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  23960. LinesToStr([ // statements
  23961. 'rtl.recNewT($mod, "TRec", function () {',
  23962. ' this.x = 0;',
  23963. ' this.$eq = function (b) {',
  23964. ' return this.x === b.x;',
  23965. ' };',
  23966. ' this.$assign = function (s) {',
  23967. ' this.x = s.x;',
  23968. ' return this;',
  23969. ' };',
  23970. '});',
  23971. 'rtl.createClass($mod, "TObject", null, function () {',
  23972. ' this.$init = function () {',
  23973. ' this.A = [];',
  23974. ' };',
  23975. ' this.$final = function () {',
  23976. ' this.A = undefined;',
  23977. ' };',
  23978. '});',
  23979. 'this.DoIt = function () {',
  23980. ' var Arr = [];',
  23981. ' var ArrArr = [];',
  23982. ' var i = 0;',
  23983. ' var o = null;',
  23984. ' Arr[1].$assign(Arr[1]);',
  23985. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  23986. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  23987. '};',
  23988. '']),
  23989. LinesToStr([ // $mod.$main
  23990. '']));
  23991. end;
  23992. procedure TTestModule.TestRangeChecks_StringIndex;
  23993. begin
  23994. StartProgram(false);
  23995. Add([
  23996. 'type',
  23997. ' TObject = class',
  23998. ' S: string;',
  23999. ' end;',
  24000. '{$R+}',
  24001. 'procedure DoIt(var h: string);',
  24002. 'var',
  24003. ' s: string;',
  24004. ' i: longint;',
  24005. ' c: char;',
  24006. ' o: tobject;',
  24007. 'begin',
  24008. ' c:=s[1];',
  24009. ' s[i]:=s[i];',
  24010. ' h[i]:=h[i];',
  24011. ' c:=o.s[i];',
  24012. ' o.s[i]:=c;',
  24013. 'end;',
  24014. 'begin',
  24015. '']);
  24016. ConvertProgram;
  24017. CheckSource('TestRangeChecks_StringIndex',
  24018. LinesToStr([ // statements
  24019. 'rtl.createClass($mod, "TObject", null, function () {',
  24020. ' this.$init = function () {',
  24021. ' this.S = "";',
  24022. ' };',
  24023. ' this.$final = function () {',
  24024. ' };',
  24025. '});',
  24026. 'this.DoIt = function (h) {',
  24027. ' var s = "";',
  24028. ' var i = 0;',
  24029. ' var c = "";',
  24030. ' var o = null;',
  24031. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  24032. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  24033. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  24034. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  24035. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  24036. '};',
  24037. '']),
  24038. LinesToStr([ // $mod.$main
  24039. '']));
  24040. end;
  24041. procedure TTestModule.TestRangeChecks_TypecastInt;
  24042. begin
  24043. StartProgram(false);
  24044. Add([
  24045. '{$R+}',
  24046. 'var',
  24047. ' i: nativeint;',
  24048. ' b: byte;',
  24049. ' sh: shortint;',
  24050. ' w: word;',
  24051. ' sm: smallint;',
  24052. ' lw: longword;',
  24053. ' li: longint;',
  24054. 'begin',
  24055. ' b:=12+byte(i);',
  24056. ' sh:=12+shortint(i);',
  24057. ' w:=12+word(i);',
  24058. ' sm:=12+smallint(i);',
  24059. ' lw:=12+longword(i);',
  24060. ' li:=12+longint(i);',
  24061. '']);
  24062. ConvertProgram;
  24063. CheckSource('TestRangeChecks_TypecastInt',
  24064. LinesToStr([
  24065. 'this.i = 0;',
  24066. 'this.b = 0;',
  24067. 'this.sh = 0;',
  24068. 'this.w = 0;',
  24069. 'this.sm = 0;',
  24070. 'this.lw = 0;',
  24071. 'this.li = 0;',
  24072. '']),
  24073. LinesToStr([
  24074. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  24075. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  24076. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  24077. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  24078. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  24079. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  24080. '']));
  24081. end;
  24082. Initialization
  24083. RegisterTests([TTestModule]);
  24084. end.