tcmodules.pas 637 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134
  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 TestCurrency;
  238. Procedure TestForBoolDo;
  239. Procedure TestForIntDo;
  240. Procedure TestForIntInDo;
  241. // strings
  242. Procedure TestCharConst;
  243. Procedure TestChar_Compare;
  244. Procedure TestChar_BuiltInProcs;
  245. Procedure TestStringConst;
  246. Procedure TestStringConstSurrogate;
  247. Procedure TestString_Length;
  248. Procedure TestString_Compare;
  249. Procedure TestString_SetLength;
  250. Procedure TestString_CharAt;
  251. Procedure TestStringHMinusFail;
  252. Procedure TestStr;
  253. Procedure TestBaseType_AnsiStringFail;
  254. Procedure TestBaseType_WideStringFail;
  255. Procedure TestBaseType_ShortStringFail;
  256. Procedure TestBaseType_RawByteStringFail;
  257. Procedure TestTypeShortstring_Fail;
  258. Procedure TestCharSet_Custom;
  259. Procedure TestForCharDo;
  260. Procedure TestForCharInDo;
  261. // alias types
  262. Procedure TestAliasTypeRef;
  263. Procedure TestTypeCast_BaseTypes;
  264. Procedure TestTypeCast_AliasBaseTypes;
  265. // functions
  266. Procedure TestEmptyProc;
  267. Procedure TestProcOneParam;
  268. Procedure TestFunctionWithoutParams;
  269. Procedure TestProcedureWithoutParams;
  270. Procedure TestPrgProcVar;
  271. Procedure TestProcTwoArgs;
  272. Procedure TestProc_DefaultValue;
  273. Procedure TestUnitProcVar;
  274. Procedure TestImplProc;
  275. Procedure TestFunctionResult;
  276. Procedure TestNestedProc;
  277. Procedure TestNestedProc_ResultString;
  278. Procedure TestForwardProc;
  279. Procedure TestNestedForwardProc;
  280. Procedure TestAssignFunctionResult;
  281. Procedure TestFunctionResultInCondition;
  282. Procedure TestFunctionResultInForLoop;
  283. Procedure TestFunctionResultInTypeCast;
  284. Procedure TestExit;
  285. Procedure TestBreak;
  286. Procedure TestBreakAsVar;
  287. Procedure TestContinue;
  288. Procedure TestProc_External;
  289. Procedure TestProc_ExternalOtherUnit;
  290. Procedure TestProc_Asm;
  291. Procedure TestProc_Assembler;
  292. Procedure TestProc_VarParam;
  293. Procedure TestProc_VarParamString;
  294. Procedure TestProc_VarParamV;
  295. Procedure TestProc_Overload;
  296. Procedure TestProc_OverloadForward;
  297. Procedure TestProc_OverloadIntfImpl;
  298. Procedure TestProc_OverloadNested;
  299. Procedure TestProc_OverloadUnitCycle;
  300. Procedure TestProc_Varargs;
  301. Procedure TestProc_ConstOrder;
  302. Procedure TestProc_DuplicateConst;
  303. Procedure TestProc_LocalVarAbsolute;
  304. Procedure TestProc_ReservedWords;
  305. // anonymous functions
  306. Procedure TestAnonymousProc_Assign_ObjFPC;
  307. Procedure TestAnonymousProc_Assign_Delphi;
  308. Procedure TestAnonymousProc_Arg;
  309. Procedure TestAnonymousProc_Typecast;
  310. Procedure TestAnonymousProc_With;
  311. Procedure TestAnonymousProc_ExceptOn;
  312. Procedure TestAnonymousProc_Nested;
  313. Procedure TestAnonymousProc_NestedAssignResult;
  314. // enums, sets
  315. Procedure TestEnum_Name;
  316. Procedure TestEnum_Number;
  317. Procedure TestEnum_ConstFail;
  318. Procedure TestEnum_Functions;
  319. Procedure TestEnum_AsParams;
  320. Procedure TestEnumRange_Array;
  321. Procedure TestEnum_ForIn;
  322. Procedure TestEnum_ScopedNumber;
  323. Procedure TestEnum_InFunction;
  324. Procedure TestSet_Enum;
  325. Procedure TestSet_Operators;
  326. Procedure TestSet_Operator_In;
  327. Procedure TestSet_Functions;
  328. Procedure TestSet_PassAsArgClone;
  329. Procedure TestSet_AsParams;
  330. Procedure TestSet_Property;
  331. Procedure TestSet_EnumConst;
  332. Procedure TestSet_IntConst;
  333. Procedure TestSet_AnonymousEnumType;
  334. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  335. Procedure TestSet_ConstEnum;
  336. Procedure TestSet_ConstChar;
  337. Procedure TestSet_ConstInt;
  338. Procedure TestSet_InFunction;
  339. Procedure TestSet_ForIn;
  340. // statements
  341. Procedure TestNestBegin;
  342. Procedure TestIncDec;
  343. Procedure TestAssignments;
  344. Procedure TestArithmeticOperators1;
  345. Procedure TestLogicalOperators;
  346. Procedure TestBitwiseOperators;
  347. Procedure TestFunctionInt;
  348. Procedure TestFunctionString;
  349. Procedure TestIfThen;
  350. Procedure TestForLoop;
  351. Procedure TestForLoopInsideFunction;
  352. Procedure TestForLoop_ReadVarAfter;
  353. Procedure TestForLoop_Nested;
  354. Procedure TestRepeatUntil;
  355. Procedure TestAsmBlock;
  356. Procedure TestAsmPas_Impl; // ToDo
  357. Procedure TestTryFinally;
  358. Procedure TestTryExcept;
  359. Procedure TestTryExcept_ReservedWords;
  360. Procedure TestIfThenRaiseElse;
  361. Procedure TestCaseOf;
  362. Procedure TestCaseOf_UseSwitch;
  363. Procedure TestCaseOfNoElse;
  364. Procedure TestCaseOfNoElse_UseSwitch;
  365. Procedure TestCaseOfRange;
  366. Procedure TestCaseOfString;
  367. Procedure TestCaseOfExternalClassConst;
  368. Procedure TestDebugger;
  369. // arrays
  370. Procedure TestArray_Dynamic;
  371. Procedure TestArray_Dynamic_Nil;
  372. Procedure TestArray_DynMultiDimensional;
  373. Procedure TestArray_StaticInt;
  374. Procedure TestArray_StaticBool;
  375. Procedure TestArray_StaticChar;
  376. Procedure TestArray_StaticMultiDim;
  377. Procedure TestArray_StaticInFunction;
  378. Procedure TestArrayOfRecord;
  379. Procedure TestArray_StaticRecord;
  380. Procedure TestArrayOfSet;
  381. // call(set) literal and clone var
  382. // call([set]) literal and clone var
  383. Procedure TestArray_DynAsParam;
  384. Procedure TestArray_StaticAsParam;
  385. Procedure TestArrayElement_AsParams;
  386. Procedure TestArrayElementFromFuncResult_AsParams;
  387. Procedure TestArrayEnumTypeRange;
  388. Procedure TestArray_SetLengthOutArg;
  389. Procedure TestArray_SetLengthProperty;
  390. Procedure TestArray_SetLengthMultiDim;
  391. Procedure TestArray_OpenArrayOfString;
  392. Procedure TestArray_Concat;
  393. Procedure TestArray_Copy;
  394. Procedure TestArray_InsertDelete;
  395. Procedure TestArray_DynArrayConstObjFPC;
  396. Procedure TestArray_DynArrayConstDelphi;
  397. Procedure TestArray_ArrayLitAsParam;
  398. Procedure TestArray_ArrayLitMultiDimAsParam;
  399. Procedure TestArray_ArrayLitStaticAsParam;
  400. Procedure TestArray_ForInArrOfString;
  401. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  402. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  403. // record
  404. Procedure TestRecord_Empty;
  405. Procedure TestRecord_Var;
  406. Procedure TestRecord_VarExternal;
  407. Procedure TestRecord_WithDo;
  408. Procedure TestRecord_Assign;
  409. Procedure TestRecord_AsParams;
  410. Procedure TestRecordElement_AsParams;
  411. Procedure TestRecordElementFromFuncResult_AsParams;
  412. Procedure TestRecordElementFromWith_AsParams;
  413. Procedure TestRecord_Equal;
  414. Procedure TestRecord_TypeCastJSValueToRecord;
  415. Procedure TestRecord_VariantFail;
  416. Procedure TestRecord_FieldArray;
  417. Procedure TestRecord_Const;
  418. Procedure TestRecord_TypecastFail;
  419. Procedure TestRecord_InFunction;
  420. // ToDo: RTTI of local record
  421. // ToDo: pcu local record, name clash and rtti
  422. // advanced record
  423. Procedure TestAdvRecord_Function;
  424. Procedure TestAdvRecord_Property;
  425. Procedure TestAdvRecord_PropertyDefault;
  426. Procedure TestAdvRecord_Property_ClassMethod;
  427. Procedure TestAdvRecord_Const;
  428. Procedure TestAdvRecord_SubRecord;
  429. Procedure TestAdvRecord_SubClass;
  430. Procedure TestAdvRecord_SubInterfaceFail;
  431. Procedure TestAdvRecord_Constructor;
  432. // ToDo: class constructor
  433. // classes
  434. Procedure TestClass_TObjectDefaultConstructor;
  435. Procedure TestClass_TObjectConstructorWithParams;
  436. Procedure TestClass_TObjectConstructorWithDefaultParam;
  437. Procedure TestClass_Var;
  438. Procedure TestClass_Method;
  439. Procedure TestClass_Implementation;
  440. Procedure TestClass_Inheritance;
  441. Procedure TestClass_TypeAlias;
  442. Procedure TestClass_AbstractMethod;
  443. Procedure TestClass_CallInherited_ProcNoParams;
  444. Procedure TestClass_CallInherited_WithParams;
  445. Procedure TestClasS_CallInheritedConstructor;
  446. Procedure TestClass_ClassVar_Assign;
  447. Procedure TestClass_CallClassMethod;
  448. Procedure TestClass_Property;
  449. Procedure TestClass_Property_ClassMethod;
  450. Procedure TestClass_Property_Indexed;
  451. Procedure TestClass_Property_IndexSpec;
  452. Procedure TestClass_PropertyOfTypeArray;
  453. Procedure TestClass_PropertyDefault;
  454. Procedure TestClass_PropertyDefault2;
  455. Procedure TestClass_PropertyOverride;
  456. Procedure TestClass_PropertyIncVisibility;
  457. Procedure TestClass_Assigned;
  458. Procedure TestClass_WithClassDoCreate;
  459. Procedure TestClass_WithClassInstDoProperty;
  460. Procedure TestClass_WithClassInstDoPropertyWithParams;
  461. Procedure TestClass_WithClassInstDoFunc;
  462. Procedure TestClass_TypeCast;
  463. Procedure TestClass_TypeCastUntypedParam;
  464. Procedure TestClass_Overloads;
  465. Procedure TestClass_OverloadsAncestor;
  466. Procedure TestClass_OverloadConstructor;
  467. Procedure TestClass_OverloadDelphiOverride;
  468. Procedure TestClass_ReintroducedVar;
  469. Procedure TestClass_RaiseDescendant;
  470. Procedure TestClass_ExternalMethod;
  471. Procedure TestClass_ExternalVirtualNameMismatchFail;
  472. Procedure TestClass_ExternalOverrideFail;
  473. Procedure TestClass_ExternalVar;
  474. Procedure TestClass_Const;
  475. Procedure TestClass_LocalVarSelfFail;
  476. Procedure TestClass_ArgSelfFail;
  477. Procedure TestClass_NestedProcSelf;
  478. Procedure TestClass_NestedProcSelf2;
  479. Procedure TestClass_NestedProcClassSelf;
  480. Procedure TestClass_NestedProcCallInherited;
  481. Procedure TestClass_TObjectFree;
  482. Procedure TestClass_TObjectFreeNewInstance;
  483. Procedure TestClass_TObjectFreeLowerCase;
  484. Procedure TestClass_TObjectFreeFunctionFail;
  485. Procedure TestClass_TObjectFreePropertyFail;
  486. Procedure TestClass_ForIn;
  487. // class of
  488. Procedure TestClassOf_Create;
  489. Procedure TestClassOf_Call;
  490. Procedure TestClassOf_Assign;
  491. Procedure TestClassOf_Is;
  492. Procedure TestClassOf_Compare;
  493. Procedure TestClassOf_ClassVar;
  494. Procedure TestClassOf_ClassMethod;
  495. Procedure TestClassOf_ClassProperty;
  496. Procedure TestClassOf_ClassMethodSelf;
  497. Procedure TestClassOf_TypeCast;
  498. Procedure TestClassOf_ImplicitFunctionCall;
  499. Procedure TestClassOf_Const;
  500. // nested class
  501. Procedure TestNestedClass_Alias;
  502. Procedure TestNestedClass_Record;
  503. Procedure TestNestedClass_Class;
  504. // external class
  505. Procedure TestExternalClass_Var;
  506. Procedure TestExternalClass_Const;
  507. Procedure TestExternalClass_Dollar;
  508. Procedure TestExternalClass_DuplicateVarFail;
  509. Procedure TestExternalClass_Method;
  510. Procedure TestExternalClass_ClassMethod;
  511. Procedure TestExternalClass_FunctionResultInTypeCast;
  512. Procedure TestExternalClass_NonExternalOverride;
  513. Procedure TestExternalClass_OverloadHint;
  514. Procedure TestExternalClass_SameNamePublishedProperty;
  515. Procedure TestExternalClass_Property;
  516. Procedure TestExternalClass_PropertyDate;
  517. Procedure TestExternalClass_ClassProperty;
  518. Procedure TestExternalClass_ClassOf;
  519. Procedure TestExternalClass_ClassOtherUnit;
  520. Procedure TestExternalClass_Is;
  521. Procedure TestExternalClass_As;
  522. Procedure TestExternalClass_DestructorFail;
  523. Procedure TestExternalClass_New;
  524. Procedure TestExternalClass_ClassOf_New;
  525. Procedure TestExternalClass_FuncClassOf_New;
  526. Procedure TestExternalClass_New_PasClassFail;
  527. Procedure TestExternalClass_New_PasClassBracketsFail;
  528. Procedure TestExternalClass_LocalConstSameName;
  529. Procedure TestExternalClass_ReintroduceOverload;
  530. Procedure TestExternalClass_Inherited;
  531. Procedure TestExternalClass_PascalAncestorFail;
  532. Procedure TestExternalClass_NewInstance;
  533. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  534. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  535. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  536. Procedure TestExternalClass_PascalProperty;
  537. Procedure TestExternalClass_TypeCastToRootClass;
  538. Procedure TestExternalClass_TypeCastToJSObject;
  539. Procedure TestExternalClass_TypeCastStringToExternalString;
  540. Procedure TestExternalClass_TypeCastToJSFunction;
  541. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  542. Procedure TestExternalClass_BracketAccessor;
  543. Procedure TestExternalClass_BracketAccessor_Call;
  544. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  545. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  546. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  547. Procedure TestExternalClass_BracketAccessor_MultiType;
  548. Procedure TestExternalClass_BracketAccessor_Index;
  549. Procedure TestExternalClass_ForInJSObject;
  550. Procedure TestExternalClass_ForInJSArray;
  551. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  552. // class interfaces
  553. Procedure TestClassInterface_Corba;
  554. Procedure TestClassInterface_ProcExternalFail;
  555. Procedure TestClassInterface_Overloads;
  556. Procedure TestClassInterface_DuplicateGUIInIntfListFail;
  557. Procedure TestClassInterface_DuplicateGUIInAncestorFail;
  558. Procedure TestClassInterface_AncestorImpl;
  559. Procedure TestClassInterface_ImplReintroduce;
  560. Procedure TestClassInterface_MethodResolution;
  561. Procedure TestClassInterface_AncestorMoreInterfaces;
  562. Procedure TestClassInterface_MethodOverride;
  563. Procedure TestClassInterface_Corba_Delegation;
  564. Procedure TestClassInterface_Corba_DelegationStatic;
  565. Procedure TestClassInterface_Corba_Operators;
  566. Procedure TestClassInterface_Corba_Args;
  567. Procedure TestClassInterface_Corba_ForIn;
  568. Procedure TestClassInterface_COM_AssignVar;
  569. Procedure TestClassInterface_COM_AssignArg;
  570. Procedure TestClassInterface_COM_FunctionResult;
  571. Procedure TestClassInterface_COM_InheritedFuncResult;
  572. Procedure TestClassInterface_COM_IsAsTypeCasts;
  573. Procedure TestClassInterface_COM_PassAsArg;
  574. Procedure TestClassInterface_COM_PassToUntypedParam;
  575. Procedure TestClassInterface_COM_FunctionInExpr;
  576. Procedure TestClassInterface_COM_Property;
  577. Procedure TestClassInterface_COM_IntfProperty;
  578. Procedure TestClassInterface_COM_Delegation;
  579. Procedure TestClassInterface_COM_With;
  580. Procedure TestClassInterface_COM_ForIn;
  581. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  582. Procedure TestClassInterface_COM_RecordIntfFail;
  583. Procedure TestClassInterface_COM_UnitInitialization;
  584. Procedure TestClassInterface_GUID;
  585. Procedure TestClassInterface_GUIDProperty;
  586. // proc types
  587. Procedure TestProcType;
  588. Procedure TestProcType_Arg;
  589. Procedure TestProcType_FunctionFPC;
  590. Procedure TestProcType_FunctionDelphi;
  591. Procedure TestProcType_ProcedureDelphi;
  592. Procedure TestProcType_AsParam;
  593. Procedure TestProcType_MethodFPC;
  594. Procedure TestProcType_MethodDelphi;
  595. Procedure TestProcType_PropertyFPC;
  596. Procedure TestProcType_PropertyDelphi;
  597. Procedure TestProcType_WithClassInstDoPropertyFPC;
  598. Procedure TestProcType_Nested;
  599. Procedure TestProcType_NestedOfObject;
  600. Procedure TestProcType_ReferenceToProc;
  601. Procedure TestProcType_ReferenceToMethod;
  602. Procedure TestProcType_Typecast;
  603. Procedure TestProcType_PassProcToUntyped;
  604. Procedure TestProcType_PassProcToArray;
  605. // pointer
  606. Procedure TestPointer;
  607. Procedure TestPointer_Proc;
  608. Procedure TestPointer_AssignRecordFail;
  609. Procedure TestPointer_AssignStaticArrayFail;
  610. Procedure TestPointer_TypeCastJSValueToPointer;
  611. Procedure TestPointer_NonRecordFail;
  612. Procedure TestPointer_AnonymousArgTypeFail;
  613. Procedure TestPointer_AnonymousVarTypeFail;
  614. Procedure TestPointer_AnonymousResultTypeFail;
  615. Procedure TestPointer_AddrOperatorFail;
  616. Procedure TestPointer_ArrayParamsFail;
  617. Procedure TestPointer_PointerAddFail;
  618. Procedure TestPointer_IncPointerFail;
  619. Procedure TestPointer_Record;
  620. Procedure TestPointer_RecordArg;
  621. // jsvalue
  622. Procedure TestJSValue_AssignToJSValue;
  623. Procedure TestJSValue_TypeCastToBaseType;
  624. Procedure TestJSValue_Equal;
  625. Procedure TestJSValue_If;
  626. Procedure TestJSValue_Not;
  627. Procedure TestJSValue_Enum;
  628. Procedure TestJSValue_ClassInstance;
  629. Procedure TestJSValue_ClassOf;
  630. Procedure TestJSValue_ArrayOfJSValue;
  631. Procedure TestJSValue_ArrayLit;
  632. Procedure TestJSValue_Params;
  633. Procedure TestJSValue_UntypedParam;
  634. Procedure TestJSValue_FuncResultType;
  635. Procedure TestJSValue_ProcType_Assign;
  636. Procedure TestJSValue_ProcType_Equal;
  637. Procedure TestJSValue_ProcType_Param;
  638. Procedure TestJSValue_AssignToPointerFail;
  639. Procedure TestJSValue_OverloadDouble;
  640. Procedure TestJSValue_OverloadNativeInt;
  641. Procedure TestJSValue_OverloadWord;
  642. Procedure TestJSValue_OverloadString;
  643. Procedure TestJSValue_OverloadChar;
  644. Procedure TestJSValue_OverloadPointer;
  645. Procedure TestJSValue_ForIn;
  646. // RTTI
  647. Procedure TestRTTI_IntRange;
  648. Procedure TestRTTI_Double;
  649. Procedure TestRTTI_ProcType;
  650. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  651. Procedure TestRTTI_EnumAndSetType;
  652. Procedure TestRTTI_EnumRange;
  653. Procedure TestRTTI_AnonymousEnumType;
  654. Procedure TestRTTI_StaticArray;
  655. Procedure TestRTTI_DynArray;
  656. Procedure TestRTTI_ArrayNestedAnonymous;
  657. Procedure TestRTTI_PublishedMethodOverloadFail;
  658. Procedure TestRTTI_PublishedMethodExternalFail;
  659. Procedure TestRTTI_PublishedClassPropertyFail;
  660. Procedure TestRTTI_PublishedClassFieldFail;
  661. Procedure TestRTTI_PublishedFieldExternalFail;
  662. Procedure TestRTTI_Class_Field;
  663. Procedure TestRTTI_Class_Method;
  664. Procedure TestRTTI_Class_MethodArgFlags;
  665. Procedure TestRTTI_Class_Property;
  666. Procedure TestRTTI_Class_PropertyParams;
  667. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  668. Procedure TestRTTI_Class_OmitRTTI;
  669. Procedure TestRTTI_IndexModifier;
  670. Procedure TestRTTI_StoredModifier;
  671. Procedure TestRTTI_DefaultValue;
  672. Procedure TestRTTI_DefaultValueSet;
  673. Procedure TestRTTI_DefaultValueRangeType;
  674. Procedure TestRTTI_DefaultValueInherit;
  675. Procedure TestRTTI_OverrideMethod;
  676. Procedure TestRTTI_OverloadProperty;
  677. // ToDo: array argument
  678. Procedure TestRTTI_ClassForward;
  679. Procedure TestRTTI_ClassOf;
  680. Procedure TestRTTI_Record;
  681. Procedure TestRTTI_RecordAnonymousArray;
  682. Procedure TestRTTI_LocalTypes;
  683. Procedure TestRTTI_TypeInfo_BaseTypes;
  684. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  685. Procedure TestRTTI_TypeInfo_LocalFail;
  686. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  687. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  688. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  689. Procedure TestRTTI_TypeInfo_FunctionClassType;
  690. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  691. Procedure TestRTTI_Interface_Corba;
  692. Procedure TestRTTI_Interface_COM;
  693. // Resourcestring
  694. Procedure TestResourcestringProgram;
  695. Procedure TestResourcestringUnit;
  696. Procedure TestResourcestringImplementation;
  697. // Attributes
  698. Procedure TestAtributes_Ignore;
  699. // Assertions, checks
  700. procedure TestAssert;
  701. procedure TestAssert_SysUtils;
  702. procedure TestObjectChecks;
  703. procedure TestRangeChecks_AssignInt;
  704. procedure TestRangeChecks_AssignIntRange;
  705. procedure TestRangeChecks_AssignEnum;
  706. procedure TestRangeChecks_AssignEnumRange;
  707. procedure TestRangeChecks_AssignChar;
  708. procedure TestRangeChecks_AssignCharRange;
  709. procedure TestRangeChecks_ArrayIndex;
  710. procedure TestRangeChecks_ArrayOfRecIndex;
  711. procedure TestRangeChecks_StringIndex;
  712. procedure TestRangeChecks_TypecastInt;
  713. end;
  714. function LinesToStr(Args: array of const): string;
  715. function ExtractFileUnitName(aFilename: string): string;
  716. function JSToStr(El: TJSElement): string;
  717. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  718. implementation
  719. function LinesToStr(Args: array of const): string;
  720. var
  721. s: String;
  722. i: Integer;
  723. begin
  724. s:='';
  725. for i:=Low(Args) to High(Args) do
  726. case Args[i].VType of
  727. vtChar: s += Args[i].VChar+LineEnding;
  728. vtString: s += Args[i].VString^+LineEnding;
  729. vtPChar: s += Args[i].VPChar+LineEnding;
  730. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  731. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  732. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  733. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  734. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  735. end;
  736. Result:=s;
  737. end;
  738. function ExtractFileUnitName(aFilename: string): string;
  739. var
  740. p: Integer;
  741. begin
  742. Result:=ExtractFileName(aFilename);
  743. if Result='' then exit;
  744. for p:=length(Result) downto 1 do
  745. case Result[p] of
  746. '/','\': exit;
  747. '.':
  748. begin
  749. Delete(Result,p,length(Result));
  750. exit;
  751. end;
  752. end;
  753. end;
  754. function JSToStr(El: TJSElement): string;
  755. var
  756. aWriter: TBufferWriter;
  757. aJSWriter: TJSWriter;
  758. begin
  759. aJSWriter:=nil;
  760. aWriter:=TBufferWriter.Create(1000);
  761. try
  762. aJSWriter:=TJSWriter.Create(aWriter);
  763. aJSWriter.IndentSize:=2;
  764. aJSWriter.WriteJS(El);
  765. Result:=aWriter.AsString;
  766. finally
  767. aJSWriter.Free;
  768. aWriter.Free;
  769. end;
  770. end;
  771. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  772. // search diff, ignore changes in spaces
  773. const
  774. SpaceChars = [#9,#10,#13,' '];
  775. var
  776. ExpectedP, ActualP: PChar;
  777. function FindLineEnd(p: PChar): PChar;
  778. begin
  779. Result:=p;
  780. while not (Result^ in [#0,#10,#13]) do inc(Result);
  781. end;
  782. function FindLineStart(p, MinP: PChar): PChar;
  783. begin
  784. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  785. Result:=p;
  786. end;
  787. procedure SkipLineEnd(var p: PChar);
  788. begin
  789. if p^ in [#10,#13] then
  790. begin
  791. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  792. inc(p,2)
  793. else
  794. inc(p);
  795. end;
  796. end;
  797. procedure DiffFound;
  798. var
  799. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  800. ExpLine, ActLine: String;
  801. i, LineNo, DiffLineNo: Integer;
  802. begin
  803. writeln('Diff found "',Msg,'". Lines:');
  804. // write correct lines
  805. p:=PChar(Expected);
  806. LineNo:=0;
  807. DiffLineNo:=0;
  808. repeat
  809. StartPos:=p;
  810. while not (p^ in [#0,#10,#13]) do inc(p);
  811. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  812. SkipLineEnd(p);
  813. inc(LineNo);
  814. if (p<=ExpectedP) and (p^<>#0) then
  815. begin
  816. writeln('= ',ExpLine);
  817. end else begin
  818. // diff line
  819. if DiffLineNo=0 then DiffLineNo:=LineNo;
  820. // write actual line
  821. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  822. ActLineEndP:=FindLineEnd(ActualP);
  823. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  824. writeln('- ',ActLine);
  825. // write expected line
  826. writeln('+ ',ExpLine);
  827. // write empty line with pointer ^
  828. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  829. writeln('^');
  830. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  831. CheckSrcDiff:=false;
  832. // write up to three following actual lines to get some context
  833. for i:=1 to 3 do begin
  834. ActLineStartP:=ActLineEndP;
  835. SkipLineEnd(ActLineStartP);
  836. if ActLineStartP^=#0 then break;
  837. ActLineEndP:=FindLineEnd(ActLineStartP);
  838. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  839. writeln('~ ',ActLine);
  840. end;
  841. exit;
  842. end;
  843. until p^=#0;
  844. writeln('DiffFound Actual:-----------------------');
  845. writeln(Actual);
  846. writeln('DiffFound Expected:---------------------');
  847. writeln(Expected);
  848. writeln('DiffFound ------------------------------');
  849. Msg:='diff found, but lines are the same, internal error';
  850. CheckSrcDiff:=false;
  851. end;
  852. var
  853. IsSpaceNeeded: Boolean;
  854. LastChar, Quote: Char;
  855. begin
  856. Result:=true;
  857. Msg:='';
  858. if Expected='' then Expected:=' ';
  859. if Actual='' then Actual:=' ';
  860. ExpectedP:=PChar(Expected);
  861. ActualP:=PChar(Actual);
  862. repeat
  863. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  864. case ExpectedP^ of
  865. #0:
  866. begin
  867. // check that rest of Actual has only spaces
  868. while ActualP^ in SpaceChars do inc(ActualP);
  869. if ActualP^<>#0 then
  870. begin
  871. DiffFound;
  872. exit;
  873. end;
  874. exit(true);
  875. end;
  876. ' ',#9,#10,#13:
  877. begin
  878. // skip space in Expected
  879. IsSpaceNeeded:=false;
  880. if ExpectedP>PChar(Expected) then
  881. LastChar:=ExpectedP[-1]
  882. else
  883. LastChar:=#0;
  884. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  885. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  886. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  887. IsSpaceNeeded:=true;
  888. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  889. begin
  890. DiffFound;
  891. exit;
  892. end;
  893. while ActualP^ in SpaceChars do inc(ActualP);
  894. end;
  895. '''','"':
  896. begin
  897. while ActualP^ in SpaceChars do inc(ActualP);
  898. if ExpectedP^<>ActualP^ then
  899. begin
  900. DiffFound;
  901. exit;
  902. end;
  903. Quote:=ExpectedP^;
  904. repeat
  905. inc(ExpectedP);
  906. inc(ActualP);
  907. if ExpectedP^<>ActualP^ then
  908. begin
  909. DiffFound;
  910. exit;
  911. end;
  912. if (ExpectedP^ in [#0,#10,#13]) then
  913. break
  914. else if (ExpectedP^=Quote) then
  915. begin
  916. inc(ExpectedP);
  917. inc(ActualP);
  918. break;
  919. end;
  920. until false;
  921. end;
  922. else
  923. while ActualP^ in SpaceChars do inc(ActualP);
  924. if ExpectedP^<>ActualP^ then
  925. begin
  926. DiffFound;
  927. exit;
  928. end;
  929. inc(ExpectedP);
  930. inc(ActualP);
  931. end;
  932. until false;
  933. end;
  934. { TTestEnginePasResolver }
  935. destructor TTestEnginePasResolver.Destroy;
  936. begin
  937. FreeAndNil(FStreamResolver);
  938. FreeAndNil(FParser);
  939. FreeAndNil(FScanner);
  940. FreeAndNil(FStreamResolver);
  941. if Module<>nil then
  942. begin
  943. Module.Release{$IFDEF CheckPasTreeRefCount}('CreateElement'){$ENDIF};
  944. FModule:=nil;
  945. end;
  946. inherited Destroy;
  947. end;
  948. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  949. NameExpr, InFileExpr: TPasExpr): TPasModule;
  950. begin
  951. Result:=nil;
  952. if InFilename<>'' then
  953. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  954. if Assigned(OnFindUnit) then
  955. Result:=OnFindUnit(AName);
  956. if NameExpr=nil then ;
  957. end;
  958. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  959. begin
  960. // do not parse recursively
  961. // parse via the queue
  962. if Section=nil then ;
  963. end;
  964. { TCustomTestModule }
  965. function TCustomTestModule.GetMsgCount: integer;
  966. begin
  967. Result:=FHintMsgs.Count;
  968. end;
  969. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  970. begin
  971. Result:=TTestHintMessage(FHintMsgs[Index]);
  972. end;
  973. function TCustomTestModule.GetResolverCount: integer;
  974. begin
  975. Result:=FModules.Count;
  976. end;
  977. function TCustomTestModule.GetResolvers(Index: integer
  978. ): TTestEnginePasResolver;
  979. begin
  980. Result:=TTestEnginePasResolver(FModules[Index]);
  981. end;
  982. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  983. ): TPasModule;
  984. var
  985. DefNamespace: String;
  986. begin
  987. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  988. if (Pos('.',aUnitName)<1) then
  989. begin
  990. DefNamespace:=GetDefaultNamespace;
  991. if DefNamespace<>'' then
  992. begin
  993. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  994. if Result<>nil then exit;
  995. end;
  996. end;
  997. Result:=LoadUnit(aUnitName);
  998. if Result<>nil then exit;
  999. {$IFDEF VerbosePas2JS}
  1000. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1001. {$ENDIF}
  1002. Fail('can''t find unit "'+aUnitName+'"');
  1003. end;
  1004. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1005. var
  1006. aParser: TPasParser;
  1007. Item: TTestHintMessage;
  1008. begin
  1009. aParser:=Sender as TPasParser;
  1010. Item:=TTestHintMessage.Create;
  1011. Item.Id:=aParser.LastMsgNumber;
  1012. Item.MsgType:=aParser.LastMsgType;
  1013. Item.MsgNumber:=aParser.LastMsgNumber;
  1014. Item.Msg:=Msg;
  1015. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1016. {$IFDEF VerbosePas2JS}
  1017. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1018. {$ENDIF}
  1019. FHintMsgs.Add(Item);
  1020. end;
  1021. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1022. );
  1023. var
  1024. aResolver: TTestEnginePasResolver;
  1025. Item: TTestHintMessage;
  1026. begin
  1027. aResolver:=Sender as TTestEnginePasResolver;
  1028. Item:=TTestHintMessage.Create;
  1029. Item.Id:=aResolver.LastMsgId;
  1030. Item.MsgType:=aResolver.LastMsgType;
  1031. Item.MsgNumber:=aResolver.LastMsgNumber;
  1032. Item.Msg:=Msg;
  1033. Item.SourcePos:=aResolver.LastSourcePos;
  1034. {$IFDEF VerbosePas2JS}
  1035. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1036. {$ENDIF}
  1037. FHintMsgs.Add(Item);
  1038. end;
  1039. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1040. var
  1041. Item: TTestHintMessage;
  1042. aScanner: TPas2jsPasScanner;
  1043. begin
  1044. aScanner:=Sender as TPas2jsPasScanner;
  1045. Item:=TTestHintMessage.Create;
  1046. Item.Id:=aScanner.LastMsgNumber;
  1047. Item.MsgType:=aScanner.LastMsgType;
  1048. Item.MsgNumber:=aScanner.LastMsgNumber;
  1049. Item.Msg:=Msg;
  1050. Item.SourcePos:=aScanner.CurSourcePos;
  1051. {$IFDEF VerbosePas2JS}
  1052. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1053. {$ENDIF}
  1054. FHintMsgs.Add(Item);
  1055. end;
  1056. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1057. var
  1058. i: Integer;
  1059. CurEngine: TTestEnginePasResolver;
  1060. CurUnitName: String;
  1061. begin
  1062. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1063. Result:=nil;
  1064. if (Module.ClassType=TPasModule)
  1065. and (CompareText(Module.Name,aUnitName)=0) then
  1066. exit(Module);
  1067. for i:=0 to ResolverCount-1 do
  1068. begin
  1069. CurEngine:=Resolvers[i];
  1070. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1071. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1072. if CompareText(aUnitName,CurUnitName)=0 then
  1073. begin
  1074. Result:=CurEngine.Module;
  1075. if Result<>nil then exit;
  1076. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1077. FileResolver.FindSourceFile(aUnitName);
  1078. CurEngine.StreamResolver:=TStreamResolver.Create;
  1079. CurEngine.StreamResolver.OwnsStreams:=True;
  1080. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1081. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1082. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1083. InitScanner(CurEngine.Scanner);
  1084. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1085. CurEngine.Parser.Options:=po_tcmodules;
  1086. if CompareText(CurUnitName,'System')=0 then
  1087. CurEngine.Parser.ImplicitUses.Clear;
  1088. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1089. try
  1090. CurEngine.Parser.NextToken;
  1091. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1092. except
  1093. on E: Exception do
  1094. HandleException(E);
  1095. end;
  1096. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1097. Result:=CurEngine.Module;
  1098. exit;
  1099. end;
  1100. end;
  1101. end;
  1102. procedure TCustomTestModule.SetUp;
  1103. begin
  1104. {$IFDEF EnablePasTreeGlobalRefCount}
  1105. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1106. {$ENDIF}
  1107. if FModules<>nil then
  1108. begin
  1109. writeln('TCustomTestModule.SetUp FModules<>nil');
  1110. Halt;
  1111. end;
  1112. inherited SetUp;
  1113. FSkipTests:=false;
  1114. FSource:=TStringList.Create;
  1115. FModules:=TObjectList.Create(true);
  1116. FFilename:='test1.pp';
  1117. FFileResolver:=TStreamResolver.Create;
  1118. FFileResolver.OwnsStreams:=True;
  1119. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1120. InitScanner(FScanner);
  1121. FEngine:=AddModule(Filename);
  1122. FEngine.Scanner:=FScanner;
  1123. FScanner.Resolver:=FEngine;
  1124. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1125. FParser.OnLog:=@OnParserLog;
  1126. FEngine.Parser:=FParser;
  1127. Parser.Options:=po_tcmodules;
  1128. FModule:=Nil;
  1129. FConverter:=CreateConverter;
  1130. FExpectedErrorClass:=nil;
  1131. end;
  1132. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1133. begin
  1134. Result:=TPasToJSConverter.Create;
  1135. Result.Options:=co_tcmodules;
  1136. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1137. end;
  1138. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1139. begin
  1140. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1141. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1142. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1143. aScanner.AllowedBoolSwitches:=msAllPas2jsBoolSwitches;
  1144. aScanner.ReadOnlyBoolSwitches:=msAllPas2jsBoolSwitchesReadOnly;
  1145. aScanner.CurrentBoolSwitches:=msAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1146. aScanner.OnLog:=@OnScannerLog;
  1147. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1148. end;
  1149. procedure TCustomTestModule.TearDown;
  1150. {$IFDEF CheckPasTreeRefCount}
  1151. var
  1152. El: TPasElement;
  1153. {$ENDIF}
  1154. var
  1155. i: Integer;
  1156. CurModule: TPasModule;
  1157. begin
  1158. FHintMsgs.Clear;
  1159. FHintMsgsGood.Clear;
  1160. FSkipTests:=false;
  1161. FJSRegModuleCall:=nil;
  1162. FJSModuleCallArgs:=nil;
  1163. FJSImplentationUses:=nil;
  1164. FJSInterfaceUses:=nil;
  1165. FJSModuleSrc:=nil;
  1166. FJSInitBody:=nil;
  1167. FreeAndNil(FJSSource);
  1168. FreeAndNil(FJSModule);
  1169. FreeAndNil(FConverter);
  1170. Engine.Clear;
  1171. FreeAndNil(FSource);
  1172. FreeAndNil(FFileResolver);
  1173. if FModules<>nil then
  1174. begin
  1175. for i:=0 to FModules.Count-1 do
  1176. begin
  1177. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1178. if CurModule=nil then continue;
  1179. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1180. CurModule.ReleaseUsedUnits;
  1181. end;
  1182. if FModule<>nil then
  1183. FModule.ReleaseUsedUnits;
  1184. for i:=0 to FModules.Count-1 do
  1185. begin
  1186. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1187. if CurModule=nil then continue;
  1188. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1189. end;
  1190. FreeAndNil(FModules);
  1191. ReleaseAndNil(TPasElement(FModule){$IFDEF CheckPasTreeRefCount},'CreateElement'{$ENDIF});
  1192. FEngine:=nil;
  1193. end;
  1194. inherited TearDown;
  1195. {$IFDEF EnablePasTreeGlobalRefCount}
  1196. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1197. begin
  1198. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1199. {$IFDEF CheckPasTreeRefCount}
  1200. El:=TPasElement.FirstRefEl;
  1201. while El<>nil do
  1202. begin
  1203. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1204. for i:=0 to El.RefIds.Count-1 do
  1205. writeln(' ',El.RefIds[i]);
  1206. El:=El.NextRefEl;
  1207. end;
  1208. {$ENDIF}
  1209. Halt;
  1210. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1211. end;
  1212. {$ENDIF}
  1213. end;
  1214. procedure TCustomTestModule.Add(Line: string);
  1215. begin
  1216. Source.Add(Line);
  1217. end;
  1218. procedure TCustomTestModule.Add(const Lines: array of string);
  1219. var
  1220. i: Integer;
  1221. begin
  1222. for i:=low(Lines) to high(Lines) do
  1223. Add(Lines[i]);
  1224. end;
  1225. procedure TCustomTestModule.StartParsing;
  1226. var
  1227. Src: String;
  1228. begin
  1229. Src:=Source.Text;
  1230. FEngine.Source:=Src;
  1231. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1232. Scanner.OpenFile(FileName);
  1233. Writeln('// Test : ',Self.TestName);
  1234. Writeln(Src);
  1235. end;
  1236. procedure TCustomTestModule.ParseModuleQueue;
  1237. var
  1238. i: Integer;
  1239. CurResolver: TTestEnginePasResolver;
  1240. Found: Boolean;
  1241. Section: TPasSection;
  1242. begin
  1243. // parse til exception or all modules finished
  1244. while not SkipTests do
  1245. begin
  1246. Found:=false;
  1247. for i:=0 to ResolverCount-1 do
  1248. begin
  1249. CurResolver:=Resolvers[i];
  1250. if CurResolver.CurrentParser=nil then continue;
  1251. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1252. continue;
  1253. CurResolver.Parser.ParseContinue;
  1254. Found:=true;
  1255. break;
  1256. end;
  1257. if not Found then break;
  1258. end;
  1259. for i:=0 to ResolverCount-1 do
  1260. begin
  1261. CurResolver:=Resolvers[i];
  1262. if CurResolver.Parser=nil then
  1263. begin
  1264. if CurResolver.CurrentParser<>nil then
  1265. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1266. continue;
  1267. end;
  1268. if CurResolver.Parser.CurModule<>nil then
  1269. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1270. end;
  1271. end;
  1272. procedure TCustomTestModule.ParseModule;
  1273. begin
  1274. if SkipTests then exit;
  1275. FFirstPasStatement:=nil;
  1276. try
  1277. StartParsing;
  1278. Parser.ParseMain(FModule);
  1279. ParseModuleQueue;
  1280. except
  1281. on E: Exception do
  1282. HandleException(E);
  1283. end;
  1284. if SkipTests then exit;
  1285. AssertNotNull('Module resulted in Module',Module);
  1286. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1287. TAssert.AssertSame('Has resolver',Engine,Parser.Engine);
  1288. end;
  1289. procedure TCustomTestModule.ParseProgram;
  1290. begin
  1291. if SkipTests then exit;
  1292. ParseModule;
  1293. if SkipTests then exit;
  1294. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1295. FPasProgram:=TPasProgram(Module);
  1296. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1297. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1298. if (PasProgram.InitializationSection.Elements.Count>0) then
  1299. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1300. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1301. end;
  1302. procedure TCustomTestModule.ParseUnit;
  1303. begin
  1304. if SkipTests then exit;
  1305. ParseModule;
  1306. if SkipTests then exit;
  1307. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1308. AssertNotNull('Has interface section',Module.InterfaceSection);
  1309. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1310. if (Module.InitializationSection<>nil)
  1311. and (Module.InitializationSection.Elements.Count>0)
  1312. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1313. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1314. end;
  1315. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1316. ): TTestEnginePasResolver;
  1317. var
  1318. i: Integer;
  1319. begin
  1320. for i:=0 to ResolverCount-1 do
  1321. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1322. exit(Resolvers[i]);
  1323. Result:=nil;
  1324. end;
  1325. function TCustomTestModule.AddModule(aFilename: string
  1326. ): TTestEnginePasResolver;
  1327. begin
  1328. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1329. if FindModuleWithFilename(aFilename)<>nil then
  1330. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1331. Result:=TTestEnginePasResolver.Create;
  1332. Result.Filename:=aFilename;
  1333. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1334. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1335. Result.OnLog:=@OnPasResolverLog;
  1336. FModules.Add(Result);
  1337. end;
  1338. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1339. ): TTestEnginePasResolver;
  1340. begin
  1341. Result:=AddModule(aFilename);
  1342. Result.Source:=Src;
  1343. end;
  1344. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1345. ImplementationSrc: string): TTestEnginePasResolver;
  1346. var
  1347. Src: String;
  1348. begin
  1349. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1350. Src+=LineEnding;
  1351. Src+='interface'+LineEnding;
  1352. Src+=LineEnding;
  1353. Src+=InterfaceSrc;
  1354. Src+='implementation'+LineEnding;
  1355. Src+=LineEnding;
  1356. Src+=ImplementationSrc;
  1357. Src+='end.'+LineEnding;
  1358. Result:=AddModuleWithSrc(aFilename,Src);
  1359. end;
  1360. procedure TCustomTestModule.AddSystemUnit;
  1361. begin
  1362. AddModuleWithIntfImplSrc('system.pp',
  1363. // interface
  1364. LinesToStr([
  1365. 'type',
  1366. ' integer=longint;',
  1367. 'var',
  1368. ' ExitCode: Longint;',
  1369. ''
  1370. // implementation
  1371. ]),LinesToStr([
  1372. ''
  1373. ]));
  1374. end;
  1375. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean);
  1376. begin
  1377. if NeedSystemUnit then
  1378. AddSystemUnit
  1379. else
  1380. Parser.ImplicitUses.Clear;
  1381. Add('program '+ExtractFileUnitName(Filename)+';');
  1382. Add('');
  1383. end;
  1384. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean);
  1385. begin
  1386. if NeedSystemUnit then
  1387. AddSystemUnit
  1388. else
  1389. Parser.ImplicitUses.Clear;
  1390. Add('unit Test1;');
  1391. Add('');
  1392. end;
  1393. procedure TCustomTestModule.ConvertModule;
  1394. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1395. out UsesLit: TJSArrayLiteral);
  1396. var
  1397. i: Integer;
  1398. Item: TJSElement;
  1399. Lit: TJSLiteral;
  1400. begin
  1401. UsesLit:=nil;
  1402. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1403. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1404. exit; // null is ok
  1405. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1406. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1407. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1408. begin
  1409. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1410. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1411. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1412. Lit:=TJSLiteral(Item);
  1413. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1414. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1415. end;
  1416. end;
  1417. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  1418. out Src: TJSSourceElements);
  1419. var
  1420. FunDecl: TJSFunctionDeclarationStatement;
  1421. FunDef: TJSFuncDef;
  1422. FunBody: TJSFunctionBody;
  1423. begin
  1424. Src:=nil;
  1425. AssertNotNull(ParamName,Arg.Expr);
  1426. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  1427. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  1428. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  1429. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  1430. FunDef:=FunDecl.AFunction as TJSFuncDef;
  1431. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  1432. AssertNotNull(ParamName+' body',FunDef.Body);
  1433. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  1434. FunBody:=FunDef.Body as TJSFunctionBody;
  1435. AssertNotNull(ParamName+' body.A',FunBody.A);
  1436. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  1437. Src:=FunBody.A as TJSSourceElements;
  1438. end;
  1439. var
  1440. ModuleNameExpr: TJSLiteral;
  1441. InitFunction: TJSFunctionDeclarationStatement;
  1442. InitAssign: TJSSimpleAssignStatement;
  1443. InitName: String;
  1444. LastNode: TJSElement;
  1445. Arg: TJSArrayLiteralElement;
  1446. begin
  1447. if SkipTests then exit;
  1448. try
  1449. FJSModule:=FConverter.ConvertPasElement(Module,Engine) as TJSSourceElements;
  1450. except
  1451. on E: Exception do
  1452. HandleException(E);
  1453. end;
  1454. if SkipTests then exit;
  1455. if ExpectedErrorClass<>nil then
  1456. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  1457. FJSSource:=TStringList.Create;
  1458. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  1459. {$IFDEF VerbosePas2JS}
  1460. writeln('TTestModule.ConvertModule JS:');
  1461. write(FJSSource.Text);
  1462. {$ENDIF}
  1463. // rtl.module(...
  1464. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  1465. AssertNotNull('register module call',JSModule.Statements.Nodes[0].Node);
  1466. AssertEquals('register module call',TJSCallExpression,JSModule.Statements.Nodes[0].Node.ClassType);
  1467. FJSRegModuleCall:=JSModule.Statements.Nodes[0].Node as TJSCallExpression;
  1468. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  1469. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  1470. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  1471. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  1472. // parameter 'unitname'
  1473. if JSModuleCallArgs.Elements.Count<1 then
  1474. Fail('rtl.module first param unit missing');
  1475. Arg:=JSModuleCallArgs.Elements.Elements[0];
  1476. AssertNotNull('module name param',Arg.Expr);
  1477. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  1478. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  1479. if Module is TPasProgram then
  1480. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString))
  1481. else
  1482. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  1483. // main uses section
  1484. if JSModuleCallArgs.Elements.Count<2 then
  1485. Fail('rtl.module second param main uses missing');
  1486. Arg:=JSModuleCallArgs.Elements.Elements[1];
  1487. CheckUsesList('interface',Arg,FJSInterfaceUses);
  1488. // program/library/interface function()
  1489. if JSModuleCallArgs.Elements.Count<3 then
  1490. Fail('rtl.module third param intf-function missing');
  1491. Arg:=JSModuleCallArgs.Elements.Elements[2];
  1492. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  1493. // search for $mod.$init or $mod.$main - the last statement
  1494. if Module is TPasProgram then
  1495. begin
  1496. InitName:='$main';
  1497. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  1498. end
  1499. else
  1500. InitName:='$init';
  1501. FJSInitBody:=nil;
  1502. if JSModuleSrc.Statements.Count>0 then
  1503. begin
  1504. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  1505. if LastNode is TJSSimpleAssignStatement then
  1506. begin
  1507. InitAssign:=LastNode as TJSSimpleAssignStatement;
  1508. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  1509. begin
  1510. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  1511. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  1512. end
  1513. else if Module is TPasProgram then
  1514. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  1515. end;
  1516. end;
  1517. // optional: implementation uses section
  1518. if JSModuleCallArgs.Elements.Count<4 then
  1519. exit;
  1520. Arg:=JSModuleCallArgs.Elements.Elements[3];
  1521. CheckUsesList('implementation',Arg,FJSImplentationUses);
  1522. // optional: implementation function()
  1523. if JSModuleCallArgs.Elements.Count<5 then
  1524. exit;
  1525. Arg:=JSModuleCallArgs.Elements.Elements[4];
  1526. CheckFunctionParam('module impl-function',Arg,FJSImplementationSrc);
  1527. end;
  1528. procedure TCustomTestModule.ConvertProgram;
  1529. begin
  1530. Add('end.');
  1531. ParseProgram;
  1532. ConvertModule;
  1533. end;
  1534. procedure TCustomTestModule.ConvertUnit;
  1535. begin
  1536. Add('end.');
  1537. ParseUnit;
  1538. ConvertModule;
  1539. end;
  1540. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  1541. begin
  1542. Result:=tcmodules.JSToStr(El);
  1543. end;
  1544. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  1545. DottedName: string);
  1546. begin
  1547. if DottedName='' then
  1548. begin
  1549. AssertNull(Msg,El);
  1550. end
  1551. else
  1552. begin
  1553. AssertNotNull(Msg,El);
  1554. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  1555. end;
  1556. end;
  1557. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  1558. begin
  1559. if El=nil then
  1560. Result:=''
  1561. else if El is TJSPrimaryExpressionIdent then
  1562. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  1563. else if El is TJSDotMemberExpression then
  1564. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  1565. else
  1566. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  1567. end;
  1568. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  1569. InitStatements: string; ImplStatements: string);
  1570. var
  1571. ActualSrc, ExpectedSrc, InitName: String;
  1572. begin
  1573. ActualSrc:=JSToStr(JSModuleSrc);
  1574. ExpectedSrc:=
  1575. 'var $mod = this;'+LineEnding
  1576. +Statements;
  1577. if coUseStrict in Converter.Options then
  1578. ExpectedSrc:='"use strict";'+LineEnding+ExpectedSrc;
  1579. if Module is TPasProgram then
  1580. InitName:='$main'
  1581. else
  1582. InitName:='$init';
  1583. if (Module is TPasProgram) or (Trim(InitStatements)<>'') then
  1584. ExpectedSrc:=ExpectedSrc+LineEnding
  1585. +'$mod.'+InitName+' = function () {'+LineEnding
  1586. +InitStatements
  1587. +'};'+LineEnding;
  1588. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  1589. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  1590. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1591. if (JSImplementationSrc<>nil) then
  1592. begin
  1593. ActualSrc:=JSToStr(JSImplementationSrc);
  1594. ExpectedSrc:=
  1595. 'var $mod = this;'+LineEnding
  1596. +'var $impl = $mod.$impl;'+LineEnding
  1597. +ImplStatements;
  1598. end
  1599. else
  1600. begin
  1601. ActualSrc:='';
  1602. ExpectedSrc:=ImplStatements;
  1603. end;
  1604. //writeln('TTestModule.CheckSource InitStatements="',InitStatements,'"');
  1605. //writeln('TCustomTestModule.CheckSource Expected: ',ExpectedSrc);
  1606. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1607. end;
  1608. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  1609. // search diff, ignore changes in spaces
  1610. var
  1611. s: string;
  1612. begin
  1613. if CheckSrcDiff(Expected,Actual,s) then exit;
  1614. Fail(Msg+': '+s);
  1615. end;
  1616. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  1617. var
  1618. aResolver: TTestEnginePasResolver;
  1619. aConverter: TPasToJSConverter;
  1620. aJSModule: TJSSourceElements;
  1621. ActualSrc: String;
  1622. begin
  1623. aResolver:=GetResolver(Filename);
  1624. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  1625. {$IFDEF VerbosePas2JS}
  1626. writeln('CheckUnit '+Filename+' converting ...');
  1627. {$ENDIF}
  1628. aConverter:=CreateConverter;
  1629. aJSModule:=nil;
  1630. try
  1631. try
  1632. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  1633. except
  1634. on E: Exception do
  1635. HandleException(E);
  1636. end;
  1637. ActualSrc:=ConvertJSModuleToString(aJSModule);
  1638. {$IFDEF VerbosePas2JS}
  1639. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  1640. write(aResolver.Source);
  1641. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  1642. write(ActualSrc);
  1643. {$ENDIF}
  1644. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  1645. finally
  1646. aJSModule.Free;
  1647. aConverter.Free;
  1648. end;
  1649. end;
  1650. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  1651. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  1652. var
  1653. i: Integer;
  1654. Item: TTestHintMessage;
  1655. Expected,Actual: string;
  1656. begin
  1657. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  1658. for i:=0 to MsgCount-1 do
  1659. begin
  1660. Item:=Msgs[i];
  1661. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  1662. if (Marker<>nil) then
  1663. begin
  1664. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  1665. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  1666. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  1667. end;
  1668. // found
  1669. FHintMsgsGood.Add(Item);
  1670. str(Item.MsgType,Actual);
  1671. str(MsgType,Expected);
  1672. AssertEquals('MsgType',Expected,Actual);
  1673. exit;
  1674. end;
  1675. // needed message missing -> show emitted messages
  1676. WriteSources('',0,0);
  1677. for i:=0 to MsgCount-1 do
  1678. begin
  1679. Item:=Msgs[i];
  1680. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  1681. ' ('+IntToStr(Item.MsgNumber),')');
  1682. if Marker<>nil then
  1683. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  1684. writeln(' {',Item.Msg,'}');
  1685. end;
  1686. str(MsgType,Expected);
  1687. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  1688. if Marker<>nil then
  1689. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  1690. Actual:=Actual+' '+Msg;
  1691. Fail(Actual);
  1692. end;
  1693. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  1694. );
  1695. var
  1696. i: Integer;
  1697. s, Txt: String;
  1698. Msg: TTestHintMessage;
  1699. begin
  1700. for i:=0 to MsgCount-1 do
  1701. begin
  1702. Msg:=Msgs[i];
  1703. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  1704. s:='';
  1705. str(Msg.MsgType,s);
  1706. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  1707. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  1708. if WithSourcePos then
  1709. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  1710. Txt:=Txt+' {'+Msg.Msg+'}';
  1711. Fail(Txt);
  1712. end;
  1713. end;
  1714. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  1715. MsgNumber: integer);
  1716. begin
  1717. ExpectedErrorClass:=EScannerError;
  1718. ExpectedErrorMsg:=Msg;
  1719. ExpectedErrorNumber:=MsgNumber;
  1720. end;
  1721. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  1722. MsgNumber: integer);
  1723. begin
  1724. ExpectedErrorClass:=EParserError;
  1725. ExpectedErrorMsg:=Msg;
  1726. ExpectedErrorNumber:=MsgNumber;
  1727. end;
  1728. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  1729. MsgNumber: integer);
  1730. begin
  1731. ExpectedErrorClass:=EPasResolve;
  1732. ExpectedErrorMsg:=Msg;
  1733. ExpectedErrorNumber:=MsgNumber;
  1734. end;
  1735. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  1736. MsgNumber: integer);
  1737. begin
  1738. ExpectedErrorClass:=EPas2JS;
  1739. ExpectedErrorMsg:=Msg;
  1740. ExpectedErrorNumber:=MsgNumber;
  1741. end;
  1742. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  1743. var
  1744. MsgNumber: Integer;
  1745. Msg: String;
  1746. begin
  1747. Result:=false;
  1748. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  1749. Msg:=E.Message;
  1750. if E is EPas2JS then
  1751. MsgNumber:=EPas2JS(E).MsgNumber
  1752. else if E is EPasResolve then
  1753. MsgNumber:=EPasResolve(E).MsgNumber
  1754. else if E is EParserError then
  1755. MsgNumber:=Parser.LastMsgNumber
  1756. else if E is EScannerError then
  1757. begin
  1758. MsgNumber:=Scanner.LastMsgNumber;
  1759. Msg:=Scanner.LastMsg;
  1760. end
  1761. else
  1762. MsgNumber:=0;
  1763. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  1764. if Result then
  1765. SkipTests:=true;
  1766. end;
  1767. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  1768. begin
  1769. if IsErrorExpected(E) then exit;
  1770. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  1771. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  1772. +' '+Scanner.CurFilename
  1773. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  1774. FailException(E);
  1775. end;
  1776. procedure TCustomTestModule.HandleParserError(E: EParserError);
  1777. begin
  1778. if IsErrorExpected(E) then exit;
  1779. WriteSources(E.Filename,E.Row,E.Column);
  1780. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  1781. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  1782. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  1783. );
  1784. FailException(E);
  1785. end;
  1786. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  1787. var
  1788. P: TPasSourcePos;
  1789. begin
  1790. if IsErrorExpected(E) then exit;
  1791. P:=E.SourcePos;
  1792. WriteSources(P.FileName,P.Row,P.Column);
  1793. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  1794. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  1795. FailException(E);
  1796. end;
  1797. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  1798. var
  1799. Row, Col: integer;
  1800. begin
  1801. if IsErrorExpected(E) then exit;
  1802. Engine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  1803. WriteSources(E.PasElement.SourceFilename,Row,Col);
  1804. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  1805. +' '+E.PasElement.SourceFilename
  1806. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  1807. FailException(E);
  1808. end;
  1809. procedure TCustomTestModule.HandleException(E: Exception);
  1810. begin
  1811. if E is EScannerError then
  1812. HandleScannerError(EScannerError(E))
  1813. else if E is EParserError then
  1814. HandleParserError(EParserError(E))
  1815. else if E is EPasResolve then
  1816. HandlePasResolveError(EPasResolve(E))
  1817. else if E is EPas2JS then
  1818. HandlePas2JSError(EPas2JS(E))
  1819. else
  1820. begin
  1821. if IsErrorExpected(E) then exit;
  1822. if not (E is EAssertionFailedError) then
  1823. begin
  1824. WriteSources('',0,0);
  1825. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  1826. end;
  1827. FailException(E);
  1828. end;
  1829. end;
  1830. procedure TCustomTestModule.FailException(E: Exception);
  1831. var
  1832. MsgNumber: Integer;
  1833. begin
  1834. if ExpectedErrorClass<>nil then
  1835. begin
  1836. if FExpectedErrorClass=E.ClassType then
  1837. begin
  1838. if E is EPas2JS then
  1839. MsgNumber:=EPas2JS(E).MsgNumber
  1840. else if E is EPasResolve then
  1841. MsgNumber:=EPasResolve(E).MsgNumber
  1842. else if E is EParserError then
  1843. MsgNumber:=Parser.LastMsgNumber
  1844. else if E is EScannerError then
  1845. MsgNumber:=Scanner.LastMsgNumber
  1846. else
  1847. MsgNumber:=0;
  1848. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  1849. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  1850. ExpectedErrorNumber,MsgNumber);
  1851. end else begin
  1852. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  1853. end;
  1854. end;
  1855. Fail(E.Message);
  1856. end;
  1857. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  1858. aCol: integer);
  1859. var
  1860. IsSrc: Boolean;
  1861. i, j: Integer;
  1862. SrcLines: TStringList;
  1863. Line: string;
  1864. aModule: TTestEnginePasResolver;
  1865. begin
  1866. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  1867. for i:=0 to ResolverCount-1 do
  1868. begin
  1869. aModule:=Resolvers[i];
  1870. SrcLines:=TStringList.Create;
  1871. try
  1872. SrcLines.Text:=aModule.Source;
  1873. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  1874. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  1875. for j:=1 to SrcLines.Count do
  1876. begin
  1877. Line:=SrcLines[j-1];
  1878. if IsSrc and (j=aRow) then
  1879. begin
  1880. write('*');
  1881. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  1882. end;
  1883. writeln(Format('%:4d: ',[j]),Line);
  1884. end;
  1885. finally
  1886. SrcLines.Free;
  1887. end;
  1888. end;
  1889. end;
  1890. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  1891. var
  1892. i: Integer;
  1893. begin
  1894. for i:=0 to ResolverCount-1 do
  1895. if Filename=Resolvers[i].Filename then exit(i);
  1896. Result:=-1;
  1897. end;
  1898. function TCustomTestModule.GetResolver(const Filename: string
  1899. ): TTestEnginePasResolver;
  1900. var
  1901. i: Integer;
  1902. begin
  1903. i:=IndexOfResolver(Filename);
  1904. if i<0 then exit(nil);
  1905. Result:=Resolvers[i];
  1906. end;
  1907. function TCustomTestModule.GetDefaultNamespace: string;
  1908. var
  1909. C: TClass;
  1910. begin
  1911. Result:='';
  1912. if FModule=nil then exit;
  1913. C:=FModule.ClassType;
  1914. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  1915. Result:=Engine.DefaultNameSpace;
  1916. end;
  1917. constructor TCustomTestModule.Create;
  1918. begin
  1919. inherited Create;
  1920. FHintMsgs:=TObjectList.Create(true);
  1921. FHintMsgsGood:=TFPList.Create;
  1922. end;
  1923. destructor TCustomTestModule.Destroy;
  1924. begin
  1925. FreeAndNil(FHintMsgs);
  1926. FreeAndNil(FHintMsgsGood);
  1927. inherited Destroy;
  1928. end;
  1929. { TTestModule }
  1930. procedure TTestModule.TestReservedWords;
  1931. var
  1932. i: integer;
  1933. begin
  1934. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  1935. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  1936. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  1937. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  1938. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  1939. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  1940. end;
  1941. procedure TTestModule.TestEmptyProgram;
  1942. begin
  1943. StartProgram(false);
  1944. Add('begin');
  1945. ConvertProgram;
  1946. CheckSource('TestEmptyProgram','','');
  1947. end;
  1948. procedure TTestModule.TestEmptyProgramUseStrict;
  1949. begin
  1950. Converter.Options:=Converter.Options+[coUseStrict];
  1951. StartProgram(false);
  1952. Add('begin');
  1953. ConvertProgram;
  1954. CheckSource('TestEmptyProgramUseStrict','','');
  1955. end;
  1956. procedure TTestModule.TestEmptyUnit;
  1957. begin
  1958. StartUnit(false);
  1959. Add('interface');
  1960. Add('implementation');
  1961. ConvertUnit;
  1962. CheckSource('TestEmptyUnit',
  1963. LinesToStr([
  1964. ]),
  1965. '');
  1966. end;
  1967. procedure TTestModule.TestEmptyUnitUseStrict;
  1968. begin
  1969. Converter.Options:=Converter.Options+[coUseStrict];
  1970. StartUnit(false);
  1971. Add('interface');
  1972. Add('implementation');
  1973. ConvertUnit;
  1974. CheckSource('TestEmptyUnitUseStrict',
  1975. LinesToStr([
  1976. ''
  1977. ]),
  1978. '');
  1979. end;
  1980. procedure TTestModule.TestDottedUnitNames;
  1981. begin
  1982. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  1983. LinesToStr([
  1984. 'var iV: longint;'
  1985. ]),
  1986. '');
  1987. FFilename:='ns1.test1.pp';
  1988. StartProgram(true);
  1989. Add('uses unIt2;');
  1990. Add('implementation');
  1991. Add('var');
  1992. Add(' i: longint;');
  1993. Add('begin');
  1994. Add(' i:=iv;');
  1995. Add(' i:=uNit2.iv;');
  1996. Add(' i:=Ns1.TEst1.i;');
  1997. ConvertProgram;
  1998. CheckSource('TestDottedUnitNames',
  1999. LinesToStr([
  2000. 'this.i = 0;',
  2001. '']),
  2002. LinesToStr([ // this.$init
  2003. '$mod.i = pas["NS1.Unit2"].iV;',
  2004. '$mod.i = pas["NS1.Unit2"].iV;',
  2005. '$mod.i = $mod.i;',
  2006. '']) );
  2007. end;
  2008. procedure TTestModule.TestDottedUnitNameImpl;
  2009. begin
  2010. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  2011. LinesToStr([
  2012. 'type',
  2013. ' TObject = class end;',
  2014. ' TTestA = class',
  2015. ' end;'
  2016. ]),
  2017. LinesToStr(['uses TEST.UnitB;'])
  2018. );
  2019. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  2020. LinesToStr([
  2021. 'uses TEST.UnitA;',
  2022. 'type TTestB = class(TTestA);'
  2023. ]),
  2024. ''
  2025. );
  2026. StartProgram(true);
  2027. Add('uses TEST.UnitA;');
  2028. Add('begin');
  2029. ConvertProgram;
  2030. CheckSource('TestDottedUnitNameImpl',
  2031. LinesToStr([
  2032. '']),
  2033. LinesToStr([ // this.$init
  2034. '']) );
  2035. CheckUnit('TEST.UnitA.pas',
  2036. LinesToStr([
  2037. 'rtl.module("TEST.UnitA", ["system"], function () {',
  2038. ' var $mod = this;',
  2039. ' rtl.createClass($mod, "TObject", null, function () {',
  2040. ' this.$init = function () {',
  2041. ' };',
  2042. ' this.$final = function () {',
  2043. ' };',
  2044. ' });',
  2045. ' rtl.createClass($mod, "TTestA", $mod.TObject, function () {',
  2046. ' });',
  2047. '}, ["TEST.UnitB"]);'
  2048. ]));
  2049. CheckUnit('TEST.UnitB.pas',
  2050. LinesToStr([
  2051. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  2052. ' var $mod = this;',
  2053. ' rtl.createClass($mod, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  2054. ' });',
  2055. '});'
  2056. ]));
  2057. end;
  2058. procedure TTestModule.TestDottedUnitExpr;
  2059. begin
  2060. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  2061. LinesToStr([
  2062. 'procedure DoIt;'
  2063. ]),
  2064. 'procedure DoIt; begin end;');
  2065. FFilename:='Ns1.SubNs1.Test1.pp';
  2066. StartProgram(true);
  2067. Add('uses Ns2.sUbnS2.unIt2;');
  2068. Add('implementation');
  2069. Add('var');
  2070. Add(' i: longint;');
  2071. Add('begin');
  2072. Add(' ns2.subns2.unit2.doit;');
  2073. Add(' i:=Ns1.SubNS1.TEst1.i;');
  2074. ConvertProgram;
  2075. CheckSource('TestDottedUnitExpr',
  2076. LinesToStr([
  2077. 'this.i = 0;',
  2078. '']),
  2079. LinesToStr([ // this.$init
  2080. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  2081. '$mod.i = $mod.i;',
  2082. '']) );
  2083. end;
  2084. procedure TTestModule.Test_ModeFPCFail;
  2085. begin
  2086. StartProgram(false);
  2087. Add('{$mode FPC}');
  2088. Add('begin');
  2089. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  2090. ConvertProgram;
  2091. end;
  2092. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  2093. begin
  2094. StartProgram(false);
  2095. Add('{$modeswitch cblocks-}');
  2096. Add('begin');
  2097. SetExpectedScannerError('Invalid mode switch: "cblocks-"',nErrInvalidModeSwitch);
  2098. ConvertProgram;
  2099. end;
  2100. procedure TTestModule.TestUnit_UseSystem;
  2101. begin
  2102. StartUnit(true);
  2103. Add([
  2104. 'interface',
  2105. 'var i: integer;',
  2106. 'implementation']);
  2107. ConvertUnit;
  2108. CheckSource('TestUnit_UseSystem',
  2109. LinesToStr([
  2110. 'this.i = 0;',
  2111. '']),
  2112. LinesToStr([
  2113. '']) );
  2114. end;
  2115. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  2116. begin
  2117. AddModuleWithIntfImplSrc('unit1.pp',
  2118. LinesToStr([
  2119. 'type number = longint;']),
  2120. LinesToStr([
  2121. 'uses test1;',
  2122. 'procedure DoIt;',
  2123. 'begin',
  2124. ' i:=3;',
  2125. 'end;']));
  2126. StartUnit(true);
  2127. Add([
  2128. 'interface',
  2129. 'uses unit1;',
  2130. 'var i: number;',
  2131. 'implementation']);
  2132. ConvertUnit;
  2133. CheckSource('TestUnit_Intf1Impl2Intf1',
  2134. LinesToStr([
  2135. 'this.i = 0;',
  2136. '']),
  2137. LinesToStr([
  2138. '']) );
  2139. end;
  2140. procedure TTestModule.TestIncludeVersion;
  2141. begin
  2142. StartProgram(false);
  2143. Add([
  2144. 'var',
  2145. ' s: string;',
  2146. ' i: word;',
  2147. 'begin',
  2148. ' s:={$I %line%};',
  2149. ' i:={$I %linenum%};',
  2150. ' s:={$I %currentroutine%};',
  2151. ' s:={$I %pas2jsversion%};',
  2152. ' s:={$I %pas2jstarget%};',
  2153. ' s:={$I %pas2jstargetos%};',
  2154. ' s:={$I %pas2jstargetcpu%};',
  2155. ' s:={$I %file%};',
  2156. '']);
  2157. ConvertProgram;
  2158. CheckSource('TestIncludeVersion',
  2159. LinesToStr([
  2160. 'this.s="";',
  2161. 'this.i = 0;']),
  2162. LinesToStr([
  2163. '$mod.s = "7";',
  2164. '$mod.i = 8;',
  2165. '$mod.s = "<anonymous>";',
  2166. '$mod.s = "Comp.Ver.tcmodules";',
  2167. '$mod.s = "Browser";',
  2168. '$mod.s = "Browser";',
  2169. '$mod.s = "ECMAScript5";',
  2170. '$mod.s = "test1.pp";',
  2171. '']));
  2172. end;
  2173. procedure TTestModule.TestVarInt;
  2174. begin
  2175. StartProgram(false);
  2176. Add('var MyI: longint;');
  2177. Add('begin');
  2178. ConvertProgram;
  2179. CheckSource('TestVarInt','this.MyI=0;','');
  2180. end;
  2181. procedure TTestModule.TestVarBaseTypes;
  2182. begin
  2183. StartProgram(false);
  2184. Add('var');
  2185. Add(' i: longint;');
  2186. Add(' s: string;');
  2187. Add(' c: char;');
  2188. Add(' b: boolean;');
  2189. Add(' d: double;');
  2190. Add(' i2: longint = 3;');
  2191. Add(' s2: string = ''foo'';');
  2192. Add(' c2: char = ''4'';');
  2193. Add(' b2: boolean = true;');
  2194. Add(' d2: double = 5.6;');
  2195. Add(' i3: longint = $707;');
  2196. Add(' i4: nativeint = 4503599627370495;');
  2197. Add(' i5: nativeint = -4503599627370496;');
  2198. Add(' i6: nativeint = $fffffffffffff;');
  2199. Add(' i7: nativeint = -$10000000000000;');
  2200. Add(' i8: byte = 00;');
  2201. Add(' u8: nativeuint = $fffffffffffff;');
  2202. Add(' u9: nativeuint = $0000000000000;');
  2203. Add(' u10: nativeuint = $00ff00;');
  2204. Add('begin');
  2205. ConvertProgram;
  2206. CheckSource('TestVarBaseTypes',
  2207. LinesToStr([
  2208. 'this.i = 0;',
  2209. 'this.s = "";',
  2210. 'this.c = "";',
  2211. 'this.b = false;',
  2212. 'this.d = 0.0;',
  2213. 'this.i2 = 3;',
  2214. 'this.s2 = "foo";',
  2215. 'this.c2 = "4";',
  2216. 'this.b2 = true;',
  2217. 'this.d2 = 5.6;',
  2218. 'this.i3 = 0x707;',
  2219. 'this.i4 = 4503599627370495;',
  2220. 'this.i5 = -4503599627370496;',
  2221. 'this.i6 = 0xfffffffffffff;',
  2222. 'this.i7 =-0x10000000000000;',
  2223. 'this.i8 = 0;',
  2224. 'this.u8 = 0xfffffffffffff;',
  2225. 'this.u9 = 0x0;',
  2226. 'this.u10 = 0xff00;'
  2227. ]),
  2228. '');
  2229. end;
  2230. procedure TTestModule.TestBaseTypeSingleFail;
  2231. begin
  2232. StartProgram(false);
  2233. Add('var s: single;');
  2234. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  2235. ConvertProgram;
  2236. end;
  2237. procedure TTestModule.TestBaseTypeExtendedFail;
  2238. begin
  2239. StartProgram(false);
  2240. Add('var e: extended;');
  2241. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  2242. ConvertProgram;
  2243. end;
  2244. procedure TTestModule.TestConstBaseTypes;
  2245. begin
  2246. StartProgram(false);
  2247. Add('const');
  2248. Add(' i: longint = 3;');
  2249. Add(' s: string = ''foo'';');
  2250. Add(' c: char = ''4'';');
  2251. Add(' b: boolean = true;');
  2252. Add(' d: double = 5.6;');
  2253. Add(' e = low(word);');
  2254. Add(' f = high(word);');
  2255. Add('begin');
  2256. ConvertProgram;
  2257. CheckSource('TestVarBaseTypes',
  2258. LinesToStr([
  2259. 'this.i=3;',
  2260. 'this.s="foo";',
  2261. 'this.c="4";',
  2262. 'this.b=true;',
  2263. 'this.d=5.6;',
  2264. 'this.e = 0;',
  2265. 'this.f = 65535;'
  2266. ]),
  2267. '');
  2268. end;
  2269. procedure TTestModule.TestAliasTypeRef;
  2270. begin
  2271. StartProgram(false);
  2272. Add('type');
  2273. Add(' a=longint;');
  2274. Add(' b=a;');
  2275. Add('var');
  2276. Add(' c: A;');
  2277. Add(' d: B;');
  2278. Add('begin');
  2279. ConvertProgram;
  2280. CheckSource('TestAliasTypeRef',
  2281. LinesToStr([ // statements
  2282. 'this.c = 0;',
  2283. 'this.d = 0;'
  2284. ]),
  2285. LinesToStr([ // this.$main
  2286. ''
  2287. ]));
  2288. end;
  2289. procedure TTestModule.TestTypeCast_BaseTypes;
  2290. begin
  2291. StartProgram(false);
  2292. Add([
  2293. 'var',
  2294. ' i: longint;',
  2295. ' b: boolean;',
  2296. ' d: double;',
  2297. ' s: string;',
  2298. ' c: char;',
  2299. 'begin',
  2300. ' i:=longint(i);',
  2301. ' i:=longint(b);',
  2302. ' b:=boolean(b);',
  2303. ' b:=boolean(i);',
  2304. ' d:=double(d);',
  2305. ' d:=double(i);',
  2306. ' s:=string(s);',
  2307. ' s:=string(c);',
  2308. ' c:=char(c);',
  2309. ' c:=char(i);',
  2310. ' c:=char(65);',
  2311. ' c:=char(#10);',
  2312. ' c:=char(#$E000);',
  2313. '']);
  2314. ConvertProgram;
  2315. CheckSource('TestAliasTypeRef',
  2316. LinesToStr([ // statements
  2317. 'this.i = 0;',
  2318. 'this.b = false;',
  2319. 'this.d = 0.0;',
  2320. 'this.s = "";',
  2321. 'this.c = "";',
  2322. '']),
  2323. LinesToStr([ // this.$main
  2324. '$mod.i = $mod.i;',
  2325. '$mod.i = ($mod.b ? 1 : 0);',
  2326. '$mod.b = $mod.b;',
  2327. '$mod.b = $mod.i != 0;',
  2328. '$mod.d = $mod.d;',
  2329. '$mod.d = $mod.i;',
  2330. '$mod.s = $mod.s;',
  2331. '$mod.s = $mod.c;',
  2332. '$mod.c = $mod.c;',
  2333. '$mod.c = String.fromCharCode($mod.i);',
  2334. '$mod.c = "A";',
  2335. '$mod.c = "\n";',
  2336. '$mod.c = "";',
  2337. '']));
  2338. end;
  2339. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  2340. begin
  2341. StartProgram(false);
  2342. Add('type');
  2343. Add(' integer = longint;');
  2344. Add(' TYesNo = boolean;');
  2345. Add(' TFloat = double;');
  2346. Add(' TCaption = string;');
  2347. Add(' TChar = char;');
  2348. Add('var');
  2349. Add(' i: integer;');
  2350. Add(' b: TYesNo;');
  2351. Add(' d: TFloat;');
  2352. Add(' s: TCaption;');
  2353. Add(' c: TChar;');
  2354. Add('begin');
  2355. Add(' i:=integer(i);');
  2356. Add(' i:=integer(b);');
  2357. Add(' b:=TYesNo(b);');
  2358. Add(' b:=TYesNo(i);');
  2359. Add(' d:=TFloat(d);');
  2360. Add(' d:=TFloat(i);');
  2361. Add(' s:=TCaption(s);');
  2362. Add(' s:=TCaption(c);');
  2363. Add(' c:=TChar(c);');
  2364. ConvertProgram;
  2365. CheckSource('TestAliasTypeRef',
  2366. LinesToStr([ // statements
  2367. 'this.i = 0;',
  2368. 'this.b = false;',
  2369. 'this.d = 0.0;',
  2370. 'this.s = "";',
  2371. 'this.c = "";',
  2372. '']),
  2373. LinesToStr([ // this.$main
  2374. '$mod.i = $mod.i;',
  2375. '$mod.i = ($mod.b ? 1 : 0);',
  2376. '$mod.b = $mod.b;',
  2377. '$mod.b = $mod.i != 0;',
  2378. '$mod.d = $mod.d;',
  2379. '$mod.d = $mod.i;',
  2380. '$mod.s = $mod.s;',
  2381. '$mod.s = $mod.c;',
  2382. '$mod.c = $mod.c;',
  2383. '']));
  2384. end;
  2385. procedure TTestModule.TestEmptyProc;
  2386. begin
  2387. StartProgram(false);
  2388. Add('procedure Test;');
  2389. Add('begin');
  2390. Add('end;');
  2391. Add('begin');
  2392. ConvertProgram;
  2393. CheckSource('TestEmptyProc',
  2394. LinesToStr([ // statements
  2395. 'this.Test = function () {',
  2396. '};'
  2397. ]),
  2398. LinesToStr([ // this.$main
  2399. ''
  2400. ]));
  2401. end;
  2402. procedure TTestModule.TestProcOneParam;
  2403. begin
  2404. StartProgram(false);
  2405. Add('procedure ProcA(i: longint);');
  2406. Add('begin');
  2407. Add('end;');
  2408. Add('begin');
  2409. Add(' PROCA(3);');
  2410. ConvertProgram;
  2411. CheckSource('TestProcOneParam',
  2412. LinesToStr([ // statements
  2413. 'this.ProcA = function (i) {',
  2414. '};'
  2415. ]),
  2416. LinesToStr([ // this.$main
  2417. '$mod.ProcA(3);'
  2418. ]));
  2419. end;
  2420. procedure TTestModule.TestFunctionWithoutParams;
  2421. begin
  2422. StartProgram(false);
  2423. Add('function FuncA: longint;');
  2424. Add('begin');
  2425. Add('end;');
  2426. Add('var i: longint;');
  2427. Add('begin');
  2428. Add(' I:=FUNCA();');
  2429. Add(' I:=FUNCA;');
  2430. Add(' FUNCA();');
  2431. Add(' FUNCA;');
  2432. ConvertProgram;
  2433. CheckSource('TestProcWithoutParams',
  2434. LinesToStr([ // statements
  2435. 'this.FuncA = function () {',
  2436. ' var Result = 0;',
  2437. ' return Result;',
  2438. '};',
  2439. 'this.i=0;'
  2440. ]),
  2441. LinesToStr([ // this.$main
  2442. '$mod.i=$mod.FuncA();',
  2443. '$mod.i=$mod.FuncA();',
  2444. '$mod.FuncA();',
  2445. '$mod.FuncA();'
  2446. ]));
  2447. end;
  2448. procedure TTestModule.TestProcedureWithoutParams;
  2449. begin
  2450. StartProgram(false);
  2451. Add('procedure ProcA;');
  2452. Add('begin');
  2453. Add('end;');
  2454. Add('begin');
  2455. Add(' PROCA();');
  2456. Add(' PROCA;');
  2457. ConvertProgram;
  2458. CheckSource('TestProcWithoutParams',
  2459. LinesToStr([ // statements
  2460. 'this.ProcA = function () {',
  2461. '};'
  2462. ]),
  2463. LinesToStr([ // this.$main
  2464. '$mod.ProcA();',
  2465. '$mod.ProcA();'
  2466. ]));
  2467. end;
  2468. procedure TTestModule.TestIncDec;
  2469. begin
  2470. StartProgram(false);
  2471. Add([
  2472. 'procedure DoIt(var i: longint);',
  2473. 'begin',
  2474. ' inc(i);',
  2475. ' inc(i,2);',
  2476. 'end;',
  2477. 'var',
  2478. ' Bar: longint;',
  2479. 'begin',
  2480. ' inc(bar);',
  2481. ' inc(bar,2);',
  2482. ' dec(bar);',
  2483. ' dec(bar,3);',
  2484. '']);
  2485. ConvertProgram;
  2486. CheckSource('TestIncDec',
  2487. LinesToStr([ // statements
  2488. 'this.DoIt = function (i) {',
  2489. ' i.set(i.get()+1);',
  2490. ' i.set(i.get()+2);',
  2491. '};',
  2492. 'this.Bar = 0;'
  2493. ]),
  2494. LinesToStr([ // this.$main
  2495. '$mod.Bar+=1;',
  2496. '$mod.Bar+=2;',
  2497. '$mod.Bar-=1;',
  2498. '$mod.Bar-=3;'
  2499. ]));
  2500. end;
  2501. procedure TTestModule.TestAssignments;
  2502. begin
  2503. StartProgram(false);
  2504. Parser.Options:=Parser.Options+[po_cassignments];
  2505. Add('var');
  2506. Add(' Bar:longint;');
  2507. Add('begin');
  2508. Add(' bar:=3;');
  2509. Add(' bar+=4;');
  2510. Add(' bar-=5;');
  2511. Add(' bar*=6;');
  2512. ConvertProgram;
  2513. CheckSource('TestAssignments',
  2514. LinesToStr([ // statements
  2515. 'this.Bar = 0;'
  2516. ]),
  2517. LinesToStr([ // this.$main
  2518. '$mod.Bar=3;',
  2519. '$mod.Bar+=4;',
  2520. '$mod.Bar-=5;',
  2521. '$mod.Bar*=6;'
  2522. ]));
  2523. end;
  2524. procedure TTestModule.TestArithmeticOperators1;
  2525. begin
  2526. StartProgram(false);
  2527. Add('var');
  2528. Add(' vA,vB,vC:longint;');
  2529. Add('begin');
  2530. Add(' va:=1;');
  2531. Add(' vb:=va+va;');
  2532. Add(' vb:=va div vb;');
  2533. Add(' vb:=va mod vb;');
  2534. Add(' vb:=va+va*vb+va div vb;');
  2535. Add(' vc:=-va;');
  2536. Add(' va:=va-vb;');
  2537. Add(' vb:=va;');
  2538. Add(' if va<vb then vc:=va else vc:=vb;');
  2539. ConvertProgram;
  2540. CheckSource('TestArithmeticOperators1',
  2541. LinesToStr([ // statements
  2542. 'this.vA = 0;',
  2543. 'this.vB = 0;',
  2544. 'this.vC = 0;'
  2545. ]),
  2546. LinesToStr([ // this.$main
  2547. '$mod.vA = 1;',
  2548. '$mod.vB = $mod.vA + $mod.vA;',
  2549. '$mod.vB = Math.floor($mod.vA / $mod.vB);',
  2550. '$mod.vB = $mod.vA % $mod.vB;',
  2551. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + Math.floor($mod.vA / $mod.vB);',
  2552. '$mod.vC = -$mod.vA;',
  2553. '$mod.vA = $mod.vA - $mod.vB;',
  2554. '$mod.vB = $mod.vA;',
  2555. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  2556. ]));
  2557. end;
  2558. procedure TTestModule.TestLogicalOperators;
  2559. begin
  2560. StartProgram(false);
  2561. Add('var');
  2562. Add(' vA,vB,vC:boolean;');
  2563. Add('begin');
  2564. Add(' va:=vb and vc;');
  2565. Add(' va:=vb or vc;');
  2566. Add(' va:=vb xor vc;');
  2567. Add(' va:=true and vc;');
  2568. Add(' va:=(vb and vc) or (va and vb);');
  2569. Add(' va:=not vb;');
  2570. ConvertProgram;
  2571. CheckSource('TestLogicalOperators',
  2572. LinesToStr([ // statements
  2573. 'this.vA = false;',
  2574. 'this.vB = false;',
  2575. 'this.vC = false;'
  2576. ]),
  2577. LinesToStr([ // this.$main
  2578. '$mod.vA = $mod.vB && $mod.vC;',
  2579. '$mod.vA = $mod.vB || $mod.vC;',
  2580. '$mod.vA = $mod.vB ^ $mod.vC;',
  2581. '$mod.vA = true && $mod.vC;',
  2582. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  2583. '$mod.vA = !$mod.vB;'
  2584. ]));
  2585. end;
  2586. procedure TTestModule.TestBitwiseOperators;
  2587. begin
  2588. StartProgram(false);
  2589. Add('var');
  2590. Add(' vA,vB,vC:longint;');
  2591. Add('begin');
  2592. Add(' va:=vb and vc;');
  2593. Add(' va:=vb or vc;');
  2594. Add(' va:=vb xor vc;');
  2595. Add(' va:=vb shl vc;');
  2596. Add(' va:=vb shr vc;');
  2597. Add(' va:=3 and vc;');
  2598. Add(' va:=(vb and vc) or (va and vb);');
  2599. Add(' va:=not vb;');
  2600. ConvertProgram;
  2601. CheckSource('TestBitwiseOperators',
  2602. LinesToStr([ // statements
  2603. 'this.vA = 0;',
  2604. 'this.vB = 0;',
  2605. 'this.vC = 0;'
  2606. ]),
  2607. LinesToStr([ // this.$main
  2608. '$mod.vA = $mod.vB & $mod.vC;',
  2609. '$mod.vA = $mod.vB | $mod.vC;',
  2610. '$mod.vA = $mod.vB ^ $mod.vC;',
  2611. '$mod.vA = $mod.vB << $mod.vC;',
  2612. '$mod.vA = $mod.vB >>> $mod.vC;',
  2613. '$mod.vA = 3 & $mod.vC;',
  2614. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  2615. '$mod.vA = ~$mod.vB;'
  2616. ]));
  2617. end;
  2618. procedure TTestModule.TestPrgProcVar;
  2619. begin
  2620. StartProgram(false);
  2621. Add('procedure Proc1;');
  2622. Add('type');
  2623. Add(' t1=longint;');
  2624. Add('var');
  2625. Add(' vA:t1;');
  2626. Add('begin');
  2627. Add('end;');
  2628. Add('begin');
  2629. ConvertProgram;
  2630. CheckSource('TestPrgProcVar',
  2631. LinesToStr([ // statements
  2632. 'this.Proc1 = function () {',
  2633. ' var vA=0;',
  2634. '};'
  2635. ]),
  2636. LinesToStr([ // this.$main
  2637. ''
  2638. ]));
  2639. end;
  2640. procedure TTestModule.TestUnitProcVar;
  2641. begin
  2642. StartUnit(false);
  2643. Add('interface');
  2644. Add('');
  2645. Add('type tA=string; // unit scope');
  2646. Add('procedure Proc1;');
  2647. Add('');
  2648. Add('implementation');
  2649. Add('');
  2650. Add('procedure Proc1;');
  2651. Add('type tA=longint; // local proc scope');
  2652. Add('var v1:tA; // using local tA');
  2653. Add('begin');
  2654. Add('end;');
  2655. Add('var v2:tA; // using interface tA');
  2656. ConvertUnit;
  2657. CheckSource('TestUnitProcVar',
  2658. LinesToStr([ // statements
  2659. 'var $impl = $mod.$impl;',
  2660. 'this.Proc1 = function () {',
  2661. ' var v1 = 0;',
  2662. '};',
  2663. '']),
  2664. // this.$init
  2665. '',
  2666. // implementation
  2667. LinesToStr([
  2668. '$impl.v2 = "";',
  2669. '']));
  2670. end;
  2671. procedure TTestModule.TestImplProc;
  2672. begin
  2673. StartUnit(false);
  2674. Add('interface');
  2675. Add('');
  2676. Add('procedure Proc1;');
  2677. Add('');
  2678. Add('implementation');
  2679. Add('');
  2680. Add('procedure Proc1; begin end;');
  2681. Add('procedure Proc2; begin end;');
  2682. Add('initialization');
  2683. Add(' Proc1;');
  2684. Add(' Proc2;');
  2685. ConvertUnit;
  2686. CheckSource('TestImplProc',
  2687. LinesToStr([ // statements
  2688. 'var $impl = $mod.$impl;',
  2689. 'this.Proc1 = function () {',
  2690. '};',
  2691. '']),
  2692. LinesToStr([ // this.$init
  2693. '$mod.Proc1();',
  2694. '$impl.Proc2();',
  2695. '']),
  2696. LinesToStr([ // implementation
  2697. '$impl.Proc2 = function () {',
  2698. '};',
  2699. ''])
  2700. );
  2701. end;
  2702. procedure TTestModule.TestFunctionResult;
  2703. begin
  2704. StartProgram(false);
  2705. Add('function Func1: longint;');
  2706. Add('begin');
  2707. Add(' Result:=3;');
  2708. Add(' Func1:=4;');
  2709. Add('end;');
  2710. Add('begin');
  2711. ConvertProgram;
  2712. CheckSource('TestFunctionResult',
  2713. LinesToStr([ // statements
  2714. 'this.Func1 = function () {',
  2715. ' var Result = 0;',
  2716. ' Result = 3;',
  2717. ' Result = 4;',
  2718. ' return Result;',
  2719. '};'
  2720. ]),
  2721. '');
  2722. end;
  2723. procedure TTestModule.TestNestedProc;
  2724. begin
  2725. StartProgram(false);
  2726. Add([
  2727. 'var vInUnit: longint;',
  2728. 'function DoIt(pA,pD: longint): longint;',
  2729. 'var',
  2730. ' vB: longint;',
  2731. ' vC: longint;',
  2732. ' function Nesty(pA: longint): longint; ',
  2733. ' var vB: longint;',
  2734. ' begin',
  2735. ' Result:=pa+vb+vc+pd+vInUnit;',
  2736. ' nesty:=3;',
  2737. ' doit:=4;',
  2738. ' exit;',
  2739. ' end;',
  2740. 'begin',
  2741. ' Result:=pa+vb+vc;',
  2742. ' doit:=6;',
  2743. ' exit;',
  2744. 'end;',
  2745. 'begin']);
  2746. ConvertProgram;
  2747. CheckSource('TestNestedProc',
  2748. LinesToStr([ // statements
  2749. 'this.vInUnit = 0;',
  2750. 'this.DoIt = function (pA, pD) {',
  2751. ' var Result = 0;',
  2752. ' var vB = 0;',
  2753. ' var vC = 0;',
  2754. ' function Nesty(pA) {',
  2755. ' var Result$1 = 0;',
  2756. ' var vB = 0;',
  2757. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  2758. ' Result$1 = 3;',
  2759. ' Result = 4;',
  2760. ' return Result$1;',
  2761. ' return Result$1;',
  2762. ' };',
  2763. ' Result = pA + vB + vC;',
  2764. ' Result = 6;',
  2765. ' return Result;',
  2766. ' return Result;',
  2767. '};'
  2768. ]),
  2769. '');
  2770. end;
  2771. procedure TTestModule.TestNestedProc_ResultString;
  2772. begin
  2773. StartProgram(false);
  2774. Add([
  2775. 'function DoIt: string;',
  2776. ' function Nesty: string; ',
  2777. ' begin',
  2778. ' nesty:=#65#66;',
  2779. ' nesty[1]:=#67;',
  2780. ' doit:=#68;',
  2781. ' doit[2]:=#69;',
  2782. ' end;',
  2783. 'begin',
  2784. ' doit:=#70;',
  2785. ' doit[3]:=#71;',
  2786. 'end;',
  2787. 'begin']);
  2788. ConvertProgram;
  2789. CheckSource('TestNestedProc_ResultString',
  2790. LinesToStr([ // statements
  2791. 'this.DoIt = function () {',
  2792. ' var Result = "";',
  2793. ' function Nesty() {',
  2794. ' var Result$1 = "";',
  2795. ' Result$1 = "AB";',
  2796. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  2797. ' Result = "D";',
  2798. ' Result = rtl.setCharAt(Result, 1, "E");',
  2799. ' return Result$1;',
  2800. ' };',
  2801. ' Result = "F";',
  2802. ' Result = rtl.setCharAt(Result, 2, "G");',
  2803. ' return Result;',
  2804. '};'
  2805. ]),
  2806. '');
  2807. end;
  2808. procedure TTestModule.TestForwardProc;
  2809. begin
  2810. StartProgram(false);
  2811. Add('procedure FuncA(Bar: longint); forward;');
  2812. Add('procedure FuncB(Bar: longint);');
  2813. Add('begin');
  2814. Add(' funca(bar);');
  2815. Add('end;');
  2816. Add('procedure funca(bar: longint);');
  2817. Add('begin');
  2818. Add(' if bar=3 then ;');
  2819. Add('end;');
  2820. Add('begin');
  2821. Add(' funca(4);');
  2822. Add(' funcb(5);');
  2823. ConvertProgram;
  2824. CheckSource('TestForwardProc',
  2825. LinesToStr([ // statements'
  2826. 'this.FuncB = function (Bar) {',
  2827. ' $mod.FuncA(Bar);',
  2828. '};',
  2829. 'this.FuncA = function (Bar) {',
  2830. ' if (Bar === 3);',
  2831. '};'
  2832. ]),
  2833. LinesToStr([
  2834. '$mod.FuncA(4);',
  2835. '$mod.FuncB(5);'
  2836. ])
  2837. );
  2838. end;
  2839. procedure TTestModule.TestNestedForwardProc;
  2840. begin
  2841. StartProgram(false);
  2842. Add('procedure FuncA;');
  2843. Add(' procedure FuncB(i: longint); forward;');
  2844. Add(' procedure FuncC(i: longint);');
  2845. Add(' begin');
  2846. Add(' funcb(i);');
  2847. Add(' end;');
  2848. Add(' procedure FuncB(i: longint);');
  2849. Add(' begin');
  2850. Add(' if i=3 then ;');
  2851. Add(' end;');
  2852. Add('begin');
  2853. Add(' funcc(4)');
  2854. Add('end;');
  2855. Add('begin');
  2856. Add(' funca;');
  2857. ConvertProgram;
  2858. CheckSource('TestNestedForwardProc',
  2859. LinesToStr([ // statements'
  2860. 'this.FuncA = function () {',
  2861. ' function FuncC(i) {',
  2862. ' FuncB(i);',
  2863. ' };',
  2864. ' function FuncB(i) {',
  2865. ' if (i === 3);',
  2866. ' };',
  2867. ' FuncC(4);',
  2868. '};'
  2869. ]),
  2870. LinesToStr([
  2871. '$mod.FuncA();'
  2872. ])
  2873. );
  2874. end;
  2875. procedure TTestModule.TestAssignFunctionResult;
  2876. begin
  2877. StartProgram(false);
  2878. Add('function Func1: longint;');
  2879. Add('begin');
  2880. Add('end;');
  2881. Add('var i: longint;');
  2882. Add('begin');
  2883. Add(' i:=func1();');
  2884. Add(' i:=func1()+func1();');
  2885. ConvertProgram;
  2886. CheckSource('TestAssignFunctionResult',
  2887. LinesToStr([ // statements
  2888. 'this.Func1 = function () {',
  2889. ' var Result = 0;',
  2890. ' return Result;',
  2891. '};',
  2892. 'this.i = 0;'
  2893. ]),
  2894. LinesToStr([
  2895. '$mod.i = $mod.Func1();',
  2896. '$mod.i = $mod.Func1() + $mod.Func1();'
  2897. ]));
  2898. end;
  2899. procedure TTestModule.TestFunctionResultInCondition;
  2900. begin
  2901. StartProgram(false);
  2902. Add('function Func1: longint;');
  2903. Add('begin');
  2904. Add('end;');
  2905. Add('function Func2: boolean;');
  2906. Add('begin');
  2907. Add('end;');
  2908. Add('var i: longint;');
  2909. Add('begin');
  2910. Add(' if func2 then ;');
  2911. Add(' if i=func1() then ;');
  2912. Add(' if i=func1 then ;');
  2913. ConvertProgram;
  2914. CheckSource('TestFunctionResultInCondition',
  2915. LinesToStr([ // statements
  2916. 'this.Func1 = function () {',
  2917. ' var Result = 0;',
  2918. ' return Result;',
  2919. '};',
  2920. 'this.Func2 = function () {',
  2921. ' var Result = false;',
  2922. ' return Result;',
  2923. '};',
  2924. 'this.i = 0;'
  2925. ]),
  2926. LinesToStr([
  2927. 'if ($mod.Func2());',
  2928. 'if ($mod.i === $mod.Func1());',
  2929. 'if ($mod.i === $mod.Func1());'
  2930. ]));
  2931. end;
  2932. procedure TTestModule.TestFunctionResultInForLoop;
  2933. begin
  2934. StartProgram(false);
  2935. Add([
  2936. 'function Func1(a: array of longint): longint;',
  2937. 'begin',
  2938. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  2939. ' for Result in a do if a[Result]=0 then exit;',
  2940. 'end;',
  2941. 'begin',
  2942. ' Func1([1,2,3])']);
  2943. ConvertProgram;
  2944. CheckSource('TestFunctionResultInForLoop',
  2945. LinesToStr([ // statements
  2946. 'this.Func1 = function (a) {',
  2947. ' var Result = 0;',
  2948. ' for (var $l1 = rtl.length(a) - 1; $l1 >= 0; $l1--) {',
  2949. ' Result = $l1;',
  2950. ' if (a[Result] === 0) return Result;',
  2951. ' };',
  2952. ' for (var $in2 = a, $l3 = 0, $end4 = rtl.length($in2) - 1; $l3 <= $end4; $l3++) {',
  2953. ' Result = $in2[$l3];',
  2954. ' if (a[Result] === 0) return Result;',
  2955. ' };',
  2956. ' return Result;',
  2957. '};',
  2958. '']),
  2959. LinesToStr([
  2960. '$mod.Func1([1, 2, 3]);'
  2961. ]));
  2962. end;
  2963. procedure TTestModule.TestFunctionResultInTypeCast;
  2964. begin
  2965. StartProgram(false);
  2966. Add([
  2967. 'function GetInt: longint;',
  2968. 'begin',
  2969. 'end;',
  2970. 'begin',
  2971. ' if Byte(GetInt)=0 then ;',
  2972. '']);
  2973. ConvertProgram;
  2974. CheckSource('TestFunctionResultInTypeCast',
  2975. LinesToStr([ // statements
  2976. 'this.GetInt = function () {',
  2977. ' var Result = 0;',
  2978. ' return Result;',
  2979. '};',
  2980. '']),
  2981. LinesToStr([
  2982. 'if (($mod.GetInt() & 255) === 0) ;'
  2983. ]));
  2984. end;
  2985. procedure TTestModule.TestExit;
  2986. begin
  2987. StartProgram(false);
  2988. Add('procedure ProcA;');
  2989. Add('begin');
  2990. Add(' exit;');
  2991. Add('end;');
  2992. Add('function FuncB: longint;');
  2993. Add('begin');
  2994. Add(' exit;');
  2995. Add(' exit(3);');
  2996. Add('end;');
  2997. Add('function FuncC: string;');
  2998. Add('begin');
  2999. Add(' exit;');
  3000. Add(' exit(''a'');');
  3001. Add(' exit(''abc'');');
  3002. Add('end;');
  3003. Add('begin');
  3004. Add(' exit;');
  3005. Add(' exit(1);');
  3006. ConvertProgram;
  3007. CheckSource('TestExit',
  3008. LinesToStr([ // statements
  3009. 'this.ProcA = function () {',
  3010. ' return;',
  3011. '};',
  3012. 'this.FuncB = function () {',
  3013. ' var Result = 0;',
  3014. ' return Result;',
  3015. ' return 3;',
  3016. ' return Result;',
  3017. '};',
  3018. 'this.FuncC = function () {',
  3019. ' var Result = "";',
  3020. ' return Result;',
  3021. ' return "a";',
  3022. ' return "abc";',
  3023. ' return Result;',
  3024. '};'
  3025. ]),
  3026. LinesToStr([
  3027. 'return;',
  3028. 'return 1;',
  3029. '']));
  3030. end;
  3031. procedure TTestModule.TestBreak;
  3032. begin
  3033. StartProgram(false);
  3034. Add([
  3035. 'var',
  3036. ' i: longint;',
  3037. 'begin',
  3038. ' repeat',
  3039. ' break;',
  3040. ' until true;',
  3041. ' while true do',
  3042. ' break;',
  3043. ' for i:=1 to 2 do',
  3044. ' break;']);
  3045. ConvertProgram;
  3046. CheckSource('TestBreak',
  3047. LinesToStr([ // statements
  3048. 'this.i = 0;'
  3049. ]),
  3050. LinesToStr([
  3051. 'do {',
  3052. ' break;',
  3053. '} while (!true);',
  3054. 'while (true) break;',
  3055. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  3056. '']));
  3057. end;
  3058. procedure TTestModule.TestBreakAsVar;
  3059. begin
  3060. StartProgram(false);
  3061. Add([
  3062. 'procedure DoIt(break: boolean);',
  3063. 'begin',
  3064. ' if break then ;',
  3065. 'end;',
  3066. 'var',
  3067. ' break: boolean;',
  3068. 'begin',
  3069. ' if break then ;']);
  3070. ConvertProgram;
  3071. CheckSource('TestBreakAsVar',
  3072. LinesToStr([ // statements
  3073. 'this.DoIt = function (Break) {',
  3074. ' if (Break) ;',
  3075. '};',
  3076. 'this.Break = false;',
  3077. '']),
  3078. LinesToStr([
  3079. 'if($mod.Break) ;',
  3080. '']));
  3081. end;
  3082. procedure TTestModule.TestContinue;
  3083. begin
  3084. StartProgram(false);
  3085. Add('var i: longint;');
  3086. Add('begin');
  3087. Add(' repeat');
  3088. Add(' continue;');
  3089. Add(' until true;');
  3090. Add(' while true do');
  3091. Add(' continue;');
  3092. Add(' for i:=1 to 2 do');
  3093. Add(' continue;');
  3094. ConvertProgram;
  3095. CheckSource('TestContinue',
  3096. LinesToStr([ // statements
  3097. 'this.i = 0;'
  3098. ]),
  3099. LinesToStr([
  3100. 'do {',
  3101. ' continue;',
  3102. '} while (!true);',
  3103. 'while (true) continue;',
  3104. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  3105. '']));
  3106. end;
  3107. procedure TTestModule.TestProc_External;
  3108. begin
  3109. StartProgram(false);
  3110. Add('procedure Foo; external name ''console.log'';');
  3111. Add('function Bar: longint; external name ''get.item'';');
  3112. Add('function Bla(s: string): longint; external name ''apply.something'';');
  3113. Add('var');
  3114. Add(' i: longint;');
  3115. Add('begin');
  3116. Add(' Foo;');
  3117. Add(' i:=Bar;');
  3118. Add(' i:=Bla(''abc'');');
  3119. ConvertProgram;
  3120. CheckSource('TestProc_External',
  3121. LinesToStr([ // statements
  3122. 'this.i = 0;'
  3123. ]),
  3124. LinesToStr([
  3125. 'console.log();',
  3126. '$mod.i = get.item();',
  3127. '$mod.i = apply.something("abc");'
  3128. ]));
  3129. end;
  3130. procedure TTestModule.TestProc_ExternalOtherUnit;
  3131. begin
  3132. AddModuleWithIntfImplSrc('unit2.pas',
  3133. LinesToStr([
  3134. 'procedure Now; external name ''Date.now'';',
  3135. 'procedure DoIt;'
  3136. ]),
  3137. 'procedure doit; begin end;');
  3138. StartUnit(true);
  3139. Add('interface');
  3140. Add('uses unit2;');
  3141. Add('implementation');
  3142. Add('begin');
  3143. Add(' now;');
  3144. Add(' now();');
  3145. Add(' uNit2.now;');
  3146. Add(' uNit2.now();');
  3147. Add(' doit;');
  3148. Add(' uNit2.doit;');
  3149. ConvertUnit;
  3150. CheckSource('TestProc_ExternalOtherUnit',
  3151. LinesToStr([
  3152. '']),
  3153. LinesToStr([
  3154. 'Date.now();',
  3155. 'Date.now();',
  3156. 'Date.now();',
  3157. 'Date.now();',
  3158. 'pas.unit2.DoIt();',
  3159. 'pas.unit2.DoIt();',
  3160. '']));
  3161. end;
  3162. procedure TTestModule.TestProc_Asm;
  3163. begin
  3164. StartProgram(false);
  3165. Add([
  3166. 'function DoIt: longint;',
  3167. 'begin;',
  3168. ' asm',
  3169. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3170. ' end;',
  3171. ' asm console.log(); end;',
  3172. ' asm',
  3173. ' s = "'' ";',
  3174. ' s = ''" '';',
  3175. ' s = s + "world" + "''";',
  3176. ' // end',
  3177. ' s = ''end'';',
  3178. ' s = "end";',
  3179. ' end;',
  3180. 'end;',
  3181. 'begin']);
  3182. ConvertProgram;
  3183. CheckSource('TestProc_Asm',
  3184. LinesToStr([ // statements
  3185. 'this.DoIt = function () {',
  3186. ' var Result = 0;',
  3187. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3188. ' console.log();',
  3189. ' s = "'' ";',
  3190. ' s = ''" '';',
  3191. ' s = s + "world" + "''";',
  3192. ' // end',
  3193. ' s = ''end'';',
  3194. ' s = "end";',
  3195. ' return Result;',
  3196. '};'
  3197. ]),
  3198. LinesToStr([
  3199. ''
  3200. ]));
  3201. end;
  3202. procedure TTestModule.TestProc_Assembler;
  3203. begin
  3204. StartProgram(false);
  3205. Add('function DoIt: longint; assembler;');
  3206. Add('asm');
  3207. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  3208. Add('end;');
  3209. Add('begin');
  3210. ConvertProgram;
  3211. CheckSource('TestProc_Assembler',
  3212. LinesToStr([ // statements
  3213. 'this.DoIt = function () {',
  3214. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3215. '};'
  3216. ]),
  3217. LinesToStr([
  3218. ''
  3219. ]));
  3220. end;
  3221. procedure TTestModule.TestProc_VarParam;
  3222. begin
  3223. StartProgram(false);
  3224. Add('type integer = longint;');
  3225. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  3226. Add('var vJ: integer;');
  3227. Add('begin');
  3228. Add(' vg:=vg+1;');
  3229. Add(' vj:=vh+2;');
  3230. Add(' vi:=vi+3;');
  3231. Add(' doit(vg,vg,vg);');
  3232. Add(' doit(vh,vh,vj);');
  3233. Add(' doit(vi,vi,vi);');
  3234. Add(' doit(vj,vj,vj);');
  3235. Add('end;');
  3236. Add('var i: integer;');
  3237. Add('begin');
  3238. Add(' doit(i,i,i);');
  3239. ConvertProgram;
  3240. CheckSource('TestProc_VarParam',
  3241. LinesToStr([ // statements
  3242. 'this.DoIt = function (vG,vH,vI) {',
  3243. ' var vJ = 0;',
  3244. ' vG = vG + 1;',
  3245. ' vJ = vH + 2;',
  3246. ' vI.set(vI.get()+3);',
  3247. ' $mod.DoIt(vG, vG, {',
  3248. ' get: function () {',
  3249. ' return vG;',
  3250. ' },',
  3251. ' set: function (v) {',
  3252. ' vG = v;',
  3253. ' }',
  3254. ' });',
  3255. ' $mod.DoIt(vH, vH, {',
  3256. ' get: function () {',
  3257. ' return vJ;',
  3258. ' },',
  3259. ' set: function (v) {',
  3260. ' vJ = v;',
  3261. ' }',
  3262. ' });',
  3263. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  3264. ' $mod.DoIt(vJ, vJ, {',
  3265. ' get: function () {',
  3266. ' return vJ;',
  3267. ' },',
  3268. ' set: function (v) {',
  3269. ' vJ = v;',
  3270. ' }',
  3271. ' });',
  3272. '};',
  3273. 'this.i = 0;'
  3274. ]),
  3275. LinesToStr([
  3276. '$mod.DoIt($mod.i,$mod.i,{',
  3277. ' p: $mod,',
  3278. ' get: function () {',
  3279. ' return this.p.i;',
  3280. ' },',
  3281. ' set: function (v) {',
  3282. ' this.p.i = v;',
  3283. ' }',
  3284. '});'
  3285. ]));
  3286. end;
  3287. procedure TTestModule.TestProc_VarParamString;
  3288. begin
  3289. StartProgram(false);
  3290. Add(['type TCaption = string;',
  3291. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  3292. 'var c: char;',
  3293. 'begin',
  3294. ' va[1]:=c;',
  3295. ' vb[2]:=c;',
  3296. ' vc[3]:=c;',
  3297. 'end;',
  3298. 'begin']);
  3299. ConvertProgram;
  3300. CheckSource('TestProc_VarParamString',
  3301. LinesToStr([ // statements
  3302. 'this.DoIt = function (vA,vB,vC) {',
  3303. ' var c = "";',
  3304. ' vA = rtl.setCharAt(vA, 0, c);',
  3305. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  3306. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  3307. '};',
  3308. '']),
  3309. LinesToStr([
  3310. ]));
  3311. end;
  3312. procedure TTestModule.TestProc_VarParamV;
  3313. begin
  3314. StartProgram(false);
  3315. Add([
  3316. 'procedure Inc2(var i: longint);',
  3317. 'begin',
  3318. ' i:=i+2;',
  3319. 'end;',
  3320. 'procedure DoIt(v: longint);',
  3321. 'var p: array of longint;',
  3322. 'begin',
  3323. ' Inc2(v);',
  3324. ' Inc2(p[v]);',
  3325. 'end;',
  3326. 'begin']);
  3327. ConvertProgram;
  3328. CheckSource('TestProc_VarParamV',
  3329. LinesToStr([ // statements
  3330. 'this.Inc2 = function (i) {',
  3331. ' i.set(i.get()+2);',
  3332. '};',
  3333. 'this.DoIt = function (v) {',
  3334. ' var p = [];',
  3335. ' $mod.Inc2({get: function () {',
  3336. ' return v;',
  3337. ' }, set: function (w) {',
  3338. ' v = w;',
  3339. ' }});',
  3340. ' $mod.Inc2({',
  3341. ' a: v,',
  3342. ' p: p,',
  3343. ' get: function () {',
  3344. ' return this.p[this.a];',
  3345. ' },',
  3346. ' set: function (v) {',
  3347. ' this.p[this.a] = v;',
  3348. ' }',
  3349. ' });',
  3350. '};',
  3351. '']),
  3352. LinesToStr([
  3353. '']));
  3354. end;
  3355. procedure TTestModule.TestProc_Overload;
  3356. begin
  3357. StartProgram(false);
  3358. Add('procedure DoIt(vI: longint); begin end;');
  3359. Add('procedure DoIt(vI, vJ: longint); begin end;');
  3360. Add('procedure DoIt(vD: double); begin end;');
  3361. Add('begin');
  3362. Add(' DoIt(1);');
  3363. Add(' DoIt(2,3);');
  3364. Add(' DoIt(4.5);');
  3365. ConvertProgram;
  3366. CheckSource('TestProcedureOverload',
  3367. LinesToStr([ // statements
  3368. 'this.DoIt = function (vI) {',
  3369. '};',
  3370. 'this.DoIt$1 = function (vI, vJ) {',
  3371. '};',
  3372. 'this.DoIt$2 = function (vD) {',
  3373. '};',
  3374. '']),
  3375. LinesToStr([
  3376. '$mod.DoIt(1);',
  3377. '$mod.DoIt$1(2, 3);',
  3378. '$mod.DoIt$2(4.5);',
  3379. '']));
  3380. end;
  3381. procedure TTestModule.TestProc_OverloadForward;
  3382. begin
  3383. StartProgram(false);
  3384. Add('procedure DoIt(vI: longint); forward;');
  3385. Add('procedure DoIt(vI, vJ: longint); begin end;');
  3386. Add('procedure doit(vi: longint); begin end;');
  3387. Add('begin');
  3388. Add(' doit(1);');
  3389. Add(' doit(2,3);');
  3390. ConvertProgram;
  3391. CheckSource('TestProcedureOverloadForward',
  3392. LinesToStr([ // statements
  3393. 'this.DoIt$1 = function (vI, vJ) {',
  3394. '};',
  3395. 'this.DoIt = function (vI) {',
  3396. '};',
  3397. '']),
  3398. LinesToStr([
  3399. '$mod.DoIt(1);',
  3400. '$mod.DoIt$1(2, 3);',
  3401. '']));
  3402. end;
  3403. procedure TTestModule.TestProc_OverloadIntfImpl;
  3404. begin
  3405. StartUnit(false);
  3406. Add('interface');
  3407. Add('procedure DoIt(vI: longint);');
  3408. Add('procedure DoIt(vI, vJ: longint);');
  3409. Add('implementation');
  3410. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  3411. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  3412. Add('procedure DoIt(vi: longint); begin end;');
  3413. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  3414. Add('procedure DoIt(vi, vj: longint); begin end;');
  3415. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  3416. Add('begin');
  3417. Add(' doit(1);');
  3418. Add(' doit(2,3);');
  3419. Add(' doit(4,5,6);');
  3420. Add(' doit(7,8,9,10);');
  3421. Add(' doit(11,12,13,14,15);');
  3422. ConvertUnit;
  3423. CheckSource('TestProcedureOverloadUnit',
  3424. LinesToStr([ // statements
  3425. 'var $impl = $mod.$impl;',
  3426. 'this.DoIt = function (vI) {',
  3427. '};',
  3428. 'this.DoIt$1 = function (vI, vJ) {',
  3429. '};',
  3430. '']),
  3431. LinesToStr([ // this.$init
  3432. '$mod.DoIt(1);',
  3433. '$mod.DoIt$1(2, 3);',
  3434. '$impl.DoIt$3(4,5,6);',
  3435. '$impl.DoIt$4(7,8,9,10);',
  3436. '$impl.DoIt$2(11,12,13,14,15);',
  3437. '']),
  3438. LinesToStr([ // implementation
  3439. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  3440. '};',
  3441. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  3442. '};',
  3443. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  3444. '};',
  3445. '']));
  3446. end;
  3447. procedure TTestModule.TestProc_OverloadNested;
  3448. begin
  3449. StartProgram(false);
  3450. Add([
  3451. 'procedure DoIt(vA: longint); overload; forward;',
  3452. 'procedure DoIt(vB, vC: longint); overload;',
  3453. 'begin // 2 param overload',
  3454. ' doit(1);',
  3455. ' doit(1,2);',
  3456. 'end;',
  3457. 'procedure doit(vA: longint);',
  3458. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  3459. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  3460. ' begin // 4 param overload',
  3461. ' doit(1);',
  3462. ' doit(1,2);',
  3463. ' doit(1,2,3);',
  3464. ' doit(1,2,3,4);',
  3465. ' end;',
  3466. ' procedure doit(vA, vB, vC: longint);',
  3467. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  3468. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  3469. ' begin // 6 param overload',
  3470. ' doit(1);',
  3471. ' doit(1,2);',
  3472. ' doit(1,2,3);',
  3473. ' doit(1,2,3,4);',
  3474. ' doit(1,2,3,4,5);',
  3475. ' doit(1,2,3,4,5,6);',
  3476. ' end;',
  3477. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  3478. ' begin // 5 param overload',
  3479. ' doit(1);',
  3480. ' doit(1,2);',
  3481. ' doit(1,2,3);',
  3482. ' doit(1,2,3,4);',
  3483. ' doit(1,2,3,4,5);',
  3484. ' doit(1,2,3,4,5,6);',
  3485. ' end;',
  3486. ' begin // 3 param overload',
  3487. ' doit(1);',
  3488. ' doit(1,2);',
  3489. ' doit(1,2,3);',
  3490. ' doit(1,2,3,4);',
  3491. ' doit(1,2,3,4,5);',
  3492. ' doit(1,2,3,4,5,6);',
  3493. ' end;',
  3494. 'begin // 1 param overload',
  3495. ' doit(1);',
  3496. ' doit(1,2);',
  3497. ' doit(1,2,3);',
  3498. ' doit(1,2,3,4);',
  3499. 'end;',
  3500. 'begin // main',
  3501. ' doit(1);',
  3502. ' doit(1,2);']);
  3503. ConvertProgram;
  3504. CheckSource('TestProcedureOverloadNested',
  3505. LinesToStr([ // statements
  3506. 'this.DoIt$1 = function (vB, vC) {',
  3507. ' $mod.DoIt(1);',
  3508. ' $mod.DoIt$1(1, 2);',
  3509. '};',
  3510. 'this.DoIt = function (vA) {',
  3511. ' function DoIt$3(vA, vB, vC, vD) {',
  3512. ' $mod.DoIt(1);',
  3513. ' $mod.DoIt$1(1, 2);',
  3514. ' DoIt$2(1, 2, 3);',
  3515. ' DoIt$3(1, 2, 3, 4);',
  3516. ' };',
  3517. ' function DoIt$2(vA, vB, vC) {',
  3518. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  3519. ' $mod.DoIt(1);',
  3520. ' $mod.DoIt$1(1, 2);',
  3521. ' DoIt$2(1, 2, 3);',
  3522. ' DoIt$3(1, 2, 3, 4);',
  3523. ' DoIt$4(1, 2, 3, 4, 5);',
  3524. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  3525. ' };',
  3526. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  3527. ' $mod.DoIt(1);',
  3528. ' $mod.DoIt$1(1, 2);',
  3529. ' DoIt$2(1, 2, 3);',
  3530. ' DoIt$3(1, 2, 3, 4);',
  3531. ' DoIt$4(1, 2, 3, 4, 5);',
  3532. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  3533. ' };',
  3534. ' $mod.DoIt(1);',
  3535. ' $mod.DoIt$1(1, 2);',
  3536. ' DoIt$2(1, 2, 3);',
  3537. ' DoIt$3(1, 2, 3, 4);',
  3538. ' DoIt$4(1, 2, 3, 4, 5);',
  3539. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  3540. ' };',
  3541. ' $mod.DoIt(1);',
  3542. ' $mod.DoIt$1(1, 2);',
  3543. ' DoIt$2(1, 2, 3);',
  3544. ' DoIt$3(1, 2, 3, 4);',
  3545. '};',
  3546. '']),
  3547. LinesToStr([
  3548. '$mod.DoIt(1);',
  3549. '$mod.DoIt$1(1, 2);',
  3550. '']));
  3551. end;
  3552. procedure TTestModule.TestProc_OverloadUnitCycle;
  3553. begin
  3554. AddModuleWithIntfImplSrc('Unit2.pas',
  3555. LinesToStr([
  3556. 'type',
  3557. ' TObject = class',
  3558. ' procedure DoIt(b: boolean); virtual; abstract;',
  3559. ' procedure DoIt(i: longint); virtual; abstract;',
  3560. ' end;',
  3561. '']),
  3562. 'uses test1;');
  3563. StartUnit(true);
  3564. Add([
  3565. 'interface',
  3566. 'uses unit2;',
  3567. 'type',
  3568. ' TEagle = class(TObject)',
  3569. ' procedure DoIt(b: boolean); override;',
  3570. ' procedure DoIt(i: longint); override;',
  3571. ' end;',
  3572. 'implementation',
  3573. 'procedure TEagle.DoIt(b: boolean); begin end;',
  3574. 'procedure TEagle.DoIt(i: longint); begin end;',
  3575. '']);
  3576. ConvertUnit;
  3577. CheckSource('TestProc_OverloadUnitCycle',
  3578. LinesToStr([ // statements
  3579. 'rtl.createClass($mod, "TEagle", pas.Unit2.TObject, function () {',
  3580. ' this.DoIt = function (b) {',
  3581. ' };',
  3582. ' this.DoIt$1 = function (i) {',
  3583. ' };',
  3584. '});',
  3585. '']),
  3586. '',
  3587. LinesToStr([
  3588. '']));
  3589. end;
  3590. procedure TTestModule.TestProc_Varargs;
  3591. begin
  3592. StartProgram(false);
  3593. Add([
  3594. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  3595. 'procedure ProcB; varargs; external name ''ProcB'';',
  3596. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  3597. 'function GetIt: longint; begin end;',
  3598. 'begin',
  3599. ' ProcA(1);',
  3600. ' ProcA(1,2);',
  3601. ' ProcA(1,2.0);',
  3602. ' ProcA(1,2,3);',
  3603. ' ProcA(1,''2'');',
  3604. ' ProcA(2,'''');',
  3605. ' ProcA(3,false);',
  3606. ' ProcB;',
  3607. ' ProcB();',
  3608. ' ProcB(4);',
  3609. ' ProcB(''foo'');',
  3610. ' ProcC;',
  3611. ' ProcC();',
  3612. ' ProcC(4);',
  3613. ' ProcC(5,''foo'');',
  3614. ' ProcB(GetIt);',
  3615. ' ProcB(GetIt());',
  3616. ' ProcB(GetIt,GetIt());']);
  3617. ConvertProgram;
  3618. CheckSource('TestProc_Varargs',
  3619. LinesToStr([ // statements
  3620. 'this.GetIt = function () {',
  3621. ' var Result = 0;',
  3622. ' return Result;',
  3623. '};',
  3624. '']),
  3625. LinesToStr([
  3626. 'ProcA(1);',
  3627. 'ProcA(1, 2);',
  3628. 'ProcA(1, 2.0);',
  3629. 'ProcA(1, 2, 3);',
  3630. 'ProcA(1, "2");',
  3631. 'ProcA(2, "");',
  3632. 'ProcA(3, false);',
  3633. 'ProcB();',
  3634. 'ProcB();',
  3635. 'ProcB(4);',
  3636. 'ProcB("foo");',
  3637. 'ProcC(17);',
  3638. 'ProcC(17);',
  3639. 'ProcC(4);',
  3640. 'ProcC(5, "foo");',
  3641. 'ProcB($mod.GetIt());',
  3642. 'ProcB($mod.GetIt());',
  3643. 'ProcB($mod.GetIt(), $mod.GetIt());',
  3644. '']));
  3645. end;
  3646. procedure TTestModule.TestProc_ConstOrder;
  3647. begin
  3648. StartProgram(false);
  3649. Add([
  3650. 'const A = 3;',
  3651. 'const B = A+1;',
  3652. 'procedure DoIt;',
  3653. 'const C = A+1;',
  3654. 'const D = B+1;',
  3655. 'const E = D+C+B+A;',
  3656. 'begin',
  3657. 'end;',
  3658. 'begin'
  3659. ]);
  3660. ConvertProgram;
  3661. CheckSource('TestProc_ConstOrder',
  3662. LinesToStr([ // statements
  3663. 'this.A = 3;',
  3664. 'this.B = 3 + 1;',
  3665. 'var C = 3 + 1;',
  3666. 'var D = 4 + 1;',
  3667. 'var E = 5 + 4 + 4 + 3;',
  3668. 'this.DoIt = function () {',
  3669. '};',
  3670. '']),
  3671. LinesToStr([
  3672. ''
  3673. ]));
  3674. end;
  3675. procedure TTestModule.TestProc_DuplicateConst;
  3676. begin
  3677. StartProgram(false);
  3678. Add([
  3679. 'const A = 1;',
  3680. 'procedure DoIt;',
  3681. 'const A = 2;',
  3682. ' procedure SubIt;',
  3683. ' const A = 21;',
  3684. ' begin',
  3685. ' end;',
  3686. 'begin',
  3687. 'end;',
  3688. 'procedure DoSome;',
  3689. 'const A = 3;',
  3690. 'begin',
  3691. 'end;',
  3692. 'begin'
  3693. ]);
  3694. ConvertProgram;
  3695. CheckSource('TestProc_DuplicateConst',
  3696. LinesToStr([ // statements
  3697. 'this.A = 1;',
  3698. 'var A$1 = 2;',
  3699. 'var A$2 = 21;',
  3700. 'this.DoIt = function () {',
  3701. ' function SubIt() {',
  3702. ' };',
  3703. '};',
  3704. 'var A$3 = 3;',
  3705. 'this.DoSome = function () {',
  3706. '};',
  3707. '']),
  3708. LinesToStr([
  3709. ''
  3710. ]));
  3711. end;
  3712. procedure TTestModule.TestProc_LocalVarAbsolute;
  3713. begin
  3714. StartProgram(false);
  3715. Add([
  3716. 'type',
  3717. ' TObject = class',
  3718. ' Index: longint;',
  3719. ' procedure DoAbs(Item: pointer);',
  3720. ' end;',
  3721. 'procedure TObject.DoAbs(Item: pointer);',
  3722. 'var',
  3723. ' o: TObject absolute Item;',
  3724. 'begin',
  3725. ' if o.Index<o.Index then o.Index:=o.Index;',
  3726. 'end;',
  3727. 'procedure DoIt(i: longint; p: pointer);',
  3728. 'var',
  3729. ' d: double absolute i;',
  3730. ' s: string absolute d;',
  3731. ' oi: TObject absolute i;',
  3732. ' op: TObject absolute p;',
  3733. 'begin',
  3734. ' if d=d then d:=d;',
  3735. ' if s=s then s:=s;',
  3736. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  3737. ' if op.Index=op.Index then op.Index:=op.Index;',
  3738. 'end;',
  3739. 'begin']);
  3740. ConvertProgram;
  3741. CheckSource('TestProc_LocalVarAbsolute',
  3742. LinesToStr([ // statements
  3743. 'rtl.createClass($mod, "TObject", null, function () {',
  3744. ' this.$init = function () {',
  3745. ' this.Index = 0;',
  3746. ' };',
  3747. ' this.$final = function () {',
  3748. ' };',
  3749. ' this.DoAbs = function (Item) {',
  3750. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  3751. ' };',
  3752. '});',
  3753. 'this.DoIt = function (i, p) {',
  3754. ' if (i === i) i = i;',
  3755. ' if (i === i) i = i;',
  3756. ' if (i.Index < i.Index) i.Index = i.Index;',
  3757. ' if (p.Index === p.Index) p.Index = p.Index;',
  3758. '};'
  3759. ]),
  3760. LinesToStr([
  3761. ]));
  3762. end;
  3763. procedure TTestModule.TestProc_ReservedWords;
  3764. begin
  3765. StartProgram(false);
  3766. Add([
  3767. 'procedure Date(ArrayBuffer: longint);',
  3768. 'const',
  3769. ' NaN: longint = 3;',
  3770. 'var',
  3771. ' &Boolean: longint;',
  3772. ' procedure Error(ArrayBuffer: longint);',
  3773. ' begin',
  3774. ' end;',
  3775. 'begin',
  3776. ' Nan:=&bOolean;',
  3777. 'end;',
  3778. 'begin',
  3779. ' Date(1);']);
  3780. ConvertProgram;
  3781. CheckSource('TestProc_ReservedWords',
  3782. LinesToStr([ // statements
  3783. 'var naN = 3;',
  3784. 'this.Date = function (arrayBuffer) {',
  3785. ' var boolean = 0;',
  3786. ' function error(arrayBuffer) {',
  3787. ' };',
  3788. ' naN = boolean;',
  3789. '};',
  3790. '']),
  3791. LinesToStr([
  3792. ' $mod.Date(1);'
  3793. ]));
  3794. end;
  3795. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  3796. begin
  3797. StartProgram(false);
  3798. Add([
  3799. '{$mode objfpc}',
  3800. 'type',
  3801. ' TFunc = reference to function(x: word): word;',
  3802. 'var Func: TFunc;',
  3803. 'procedure DoIt(a: word);',
  3804. 'begin',
  3805. ' Func:=function(b:word): word',
  3806. ' begin',
  3807. ' Result:=a+b;',
  3808. ' exit(b);',
  3809. ' exit(Result);',
  3810. ' end;',// test semicolon
  3811. ' a:=3;',
  3812. 'end;',
  3813. 'begin',
  3814. ' Func:=function(c:word):word begin',
  3815. ' Result:=3+c;',
  3816. ' exit(c);',
  3817. ' exit(Result);',
  3818. ' end;']);
  3819. ConvertProgram;
  3820. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  3821. LinesToStr([ // statements
  3822. 'this.Func = null;',
  3823. 'this.DoIt = function (a) {',
  3824. ' $mod.Func = function (b) {',
  3825. ' var Result = 0;',
  3826. ' Result = a + b;',
  3827. ' return b;',
  3828. ' return Result;',
  3829. ' return Result;',
  3830. ' };',
  3831. ' a = 3;',
  3832. '};',
  3833. '']),
  3834. LinesToStr([
  3835. '$mod.Func = function (c) {',
  3836. ' var Result = 0;',
  3837. ' Result = 3 + c;',
  3838. ' return c;',
  3839. ' return Result;',
  3840. ' return Result;',
  3841. '};',
  3842. '']));
  3843. end;
  3844. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  3845. begin
  3846. StartProgram(false);
  3847. Add([
  3848. '{$mode delphi}',
  3849. 'type',
  3850. ' TProc = reference to procedure(x: word);',
  3851. 'procedure DoIt(a: word);',
  3852. 'var Proc: TProc;',
  3853. 'begin',
  3854. ' Proc:=procedure(b:word) begin end;',
  3855. 'end;',
  3856. 'var Proc: TProc;',
  3857. 'begin',
  3858. ' Proc:=procedure(c:word) begin end;',
  3859. '']);
  3860. ConvertProgram;
  3861. CheckSource('TestAnonymousProc_Assign_Delphi',
  3862. LinesToStr([ // statements
  3863. 'this.DoIt = function (a) {',
  3864. ' var Proc = null;',
  3865. ' Proc = function (b) {',
  3866. ' };',
  3867. '};',
  3868. 'this.Proc = null;',
  3869. '']),
  3870. LinesToStr([
  3871. '$mod.Proc = function (c) {',
  3872. '};',
  3873. '']));
  3874. end;
  3875. procedure TTestModule.TestAnonymousProc_Arg;
  3876. begin
  3877. StartProgram(false);
  3878. Add([
  3879. 'type',
  3880. ' TProc = reference to procedure;',
  3881. ' TFunc = reference to function(x: word): word;',
  3882. 'procedure DoMore(f,g: TProc);',
  3883. 'begin',
  3884. 'end;',
  3885. 'procedure DoOdd(v: jsvalue);',
  3886. 'begin',
  3887. 'end;',
  3888. 'procedure DoIt(f: TFunc);',
  3889. 'begin',
  3890. ' DoIt(function(b:word): word',
  3891. ' begin',
  3892. ' Result:=1+b;',
  3893. ' end);',
  3894. ' DoMore(procedure begin end, procedure begin end);',
  3895. ' DoOdd(procedure begin end);',
  3896. 'end;',
  3897. 'begin',
  3898. ' DoMore(procedure begin end,',
  3899. ' procedure assembler asm',
  3900. ' console.log("c");',
  3901. ' end);',
  3902. '']);
  3903. ConvertProgram;
  3904. CheckSource('TestAnonymousProc_Arg',
  3905. LinesToStr([ // statements
  3906. 'this.DoMore = function (f, g) {',
  3907. '};',
  3908. 'this.DoOdd = function (v) {',
  3909. '};',
  3910. 'this.DoIt = function (f) {',
  3911. ' $mod.DoIt(function (b) {',
  3912. ' var Result = 0;',
  3913. ' Result = 1 + b;',
  3914. ' return Result;',
  3915. ' });',
  3916. ' $mod.DoMore(function () {',
  3917. ' }, function () {',
  3918. ' });',
  3919. ' $mod.DoOdd(function () {',
  3920. ' });',
  3921. '};',
  3922. '']),
  3923. LinesToStr([
  3924. '$mod.DoMore(function () {',
  3925. '}, function () {',
  3926. ' console.log("c");',
  3927. '});',
  3928. '']));
  3929. end;
  3930. procedure TTestModule.TestAnonymousProc_Typecast;
  3931. begin
  3932. StartProgram(false);
  3933. Add([
  3934. 'type',
  3935. ' TProc = reference to procedure(w: word);',
  3936. ' TArr = array of word;',
  3937. ' TFuncArr = reference to function: TArr;',
  3938. 'procedure DoIt(p: TProc);',
  3939. 'var',
  3940. ' w: word;',
  3941. ' a: TArr;',
  3942. 'begin',
  3943. ' p:=TProc(procedure(b: smallint) begin end);',
  3944. ' a:=TFuncArr(function: TArr begin end)();',
  3945. ' w:=TFuncArr(function: TArr begin end)()[3];',
  3946. 'end;',
  3947. 'begin']);
  3948. ConvertProgram;
  3949. CheckSource('TestAnonymousProc_Typecast',
  3950. LinesToStr([ // statements
  3951. 'this.DoIt = function (p) {',
  3952. ' var w = 0;',
  3953. ' var a = [];',
  3954. ' p = function (b) {',
  3955. ' };',
  3956. ' a = function () {',
  3957. ' var Result = [];',
  3958. ' return Result;',
  3959. ' }();',
  3960. ' w = function () {',
  3961. ' var Result = [];',
  3962. ' return Result;',
  3963. ' }()[3];',
  3964. '};',
  3965. '']),
  3966. LinesToStr([
  3967. '']));
  3968. end;
  3969. procedure TTestModule.TestAnonymousProc_With;
  3970. begin
  3971. StartProgram(false);
  3972. Add([
  3973. 'type',
  3974. ' TProc = reference to procedure(w: word);',
  3975. ' TObject = class',
  3976. ' b: boolean;',
  3977. ' end;',
  3978. 'var',
  3979. ' p: TProc;',
  3980. ' bird: TObject;',
  3981. 'begin',
  3982. ' with bird do',
  3983. ' p:=procedure(w: word)',
  3984. ' begin',
  3985. ' b:=w>2;',
  3986. ' end;',
  3987. '']);
  3988. ConvertProgram;
  3989. CheckSource('TestAnonymousProc_With',
  3990. LinesToStr([ // statements
  3991. 'rtl.createClass($mod, "TObject", null, function () {',
  3992. ' this.$init = function () {',
  3993. ' this.b = false;',
  3994. ' };',
  3995. ' this.$final = function () {',
  3996. ' };',
  3997. '});',
  3998. 'this.p = null;',
  3999. 'this.bird = null;',
  4000. '']),
  4001. LinesToStr([
  4002. 'var $with1 = $mod.bird;',
  4003. '$mod.p = function (w) {',
  4004. ' $with1.b = w > 2;',
  4005. '};',
  4006. '']));
  4007. end;
  4008. procedure TTestModule.TestAnonymousProc_ExceptOn;
  4009. begin
  4010. StartProgram(false);
  4011. Add([
  4012. 'type',
  4013. ' TProc = reference to procedure;',
  4014. ' TObject = class',
  4015. ' b: boolean;',
  4016. ' end;',
  4017. 'procedure DoIt;',
  4018. 'var',
  4019. ' p: TProc;',
  4020. 'begin',
  4021. ' try',
  4022. ' except',
  4023. ' on E: TObject do',
  4024. ' p:=procedure',
  4025. ' begin',
  4026. ' E.b:=true;',
  4027. ' end;',
  4028. ' end;',
  4029. 'end;',
  4030. 'begin']);
  4031. ConvertProgram;
  4032. CheckSource('TestAnonymousProc_ExceptOn',
  4033. LinesToStr([ // statements
  4034. 'rtl.createClass($mod, "TObject", null, function () {',
  4035. ' this.$init = function () {',
  4036. ' this.b = false;',
  4037. ' };',
  4038. ' this.$final = function () {',
  4039. ' };',
  4040. '});',
  4041. 'this.DoIt = function () {',
  4042. ' var p = null;',
  4043. ' try {} catch ($e) {',
  4044. ' if ($mod.TObject.isPrototypeOf($e)) {',
  4045. ' var E = $e;',
  4046. ' p = function () {',
  4047. ' E.b = true;',
  4048. ' };',
  4049. ' } else throw $e',
  4050. ' };',
  4051. '};',
  4052. '']),
  4053. LinesToStr([
  4054. '']));
  4055. end;
  4056. procedure TTestModule.TestAnonymousProc_Nested;
  4057. begin
  4058. StartProgram(false);
  4059. Add([
  4060. 'type',
  4061. ' TProc = reference to procedure;',
  4062. ' TObject = class',
  4063. ' i: byte;',
  4064. ' procedure DoIt;',
  4065. ' end;',
  4066. 'procedure TObject.DoIt;',
  4067. 'var',
  4068. ' p: TProc;',
  4069. ' procedure Sub;',
  4070. ' begin',
  4071. ' p:=procedure',
  4072. ' begin',
  4073. ' i:=3;',
  4074. ' Self.i:=4;',
  4075. ' p:=procedure',
  4076. ' procedure SubSub;',
  4077. ' begin',
  4078. ' i:=13;',
  4079. ' Self.i:=14;',
  4080. ' end;',
  4081. ' begin',
  4082. ' i:=13;',
  4083. ' Self.i:=14;',
  4084. ' end;',
  4085. ' end;',
  4086. ' end;',
  4087. 'begin',
  4088. 'end;',
  4089. 'begin']);
  4090. ConvertProgram;
  4091. CheckSource('TestAnonymousProc_Nested',
  4092. LinesToStr([ // statements
  4093. 'rtl.createClass($mod, "TObject", null, function () {',
  4094. ' this.$init = function () {',
  4095. ' this.i = 0;',
  4096. ' };',
  4097. ' this.$final = function () {',
  4098. ' };',
  4099. ' this.DoIt = function () {',
  4100. ' var Self = this;',
  4101. ' var p = null;',
  4102. ' function Sub() {',
  4103. ' p = function () {',
  4104. ' Self.i = 3;',
  4105. ' Self.i = 4;',
  4106. ' p = function () {',
  4107. ' function SubSub() {',
  4108. ' Self.i = 13;',
  4109. ' Self.i = 14;',
  4110. ' };',
  4111. ' Self.i = 13;',
  4112. ' Self.i = 14;',
  4113. ' };',
  4114. ' };',
  4115. ' };',
  4116. ' };',
  4117. '});',
  4118. '']),
  4119. LinesToStr([
  4120. '']));
  4121. end;
  4122. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  4123. begin
  4124. StartProgram(false);
  4125. Add([
  4126. 'type',
  4127. ' TProc = reference to procedure;',
  4128. 'function DoIt: TProc;',
  4129. ' function Sub: TProc;',
  4130. ' begin',
  4131. ' Result:=procedure',
  4132. ' begin',
  4133. ' Sub:=procedure',
  4134. ' procedure SubSub;',
  4135. ' begin',
  4136. ' Result:=nil;',
  4137. ' Sub:=nil;',
  4138. ' DoIt:=nil;',
  4139. ' end;',
  4140. ' begin',
  4141. ' Result:=nil;',
  4142. ' Sub:=nil;',
  4143. ' DoIt:=nil;',
  4144. ' end;',
  4145. ' end;',
  4146. ' end;',
  4147. 'begin',
  4148. 'end;',
  4149. 'begin']);
  4150. ConvertProgram;
  4151. CheckSource('TestAnonymousProc_NestedAssignResult',
  4152. LinesToStr([ // statements
  4153. 'this.DoIt = function () {',
  4154. ' var Result = null;',
  4155. ' function Sub() {',
  4156. ' var Result$1 = null;',
  4157. ' Result$1 = function () {',
  4158. ' Result$1 = function () {',
  4159. ' function SubSub() {',
  4160. ' Result$1 = null;',
  4161. ' Result$1 = null;',
  4162. ' Result = null;',
  4163. ' };',
  4164. ' Result$1 = null;',
  4165. ' Result$1 = null;',
  4166. ' Result = null;',
  4167. ' };',
  4168. ' };',
  4169. ' return Result$1;',
  4170. ' };',
  4171. ' return Result;',
  4172. '};',
  4173. '']),
  4174. LinesToStr([
  4175. '']));
  4176. end;
  4177. procedure TTestModule.TestEnum_Name;
  4178. begin
  4179. StartProgram(false);
  4180. Add('type TMyEnum = (Red, Green, Blue);');
  4181. Add('var e: TMyEnum;');
  4182. Add('var f: TMyEnum = Blue;');
  4183. Add('begin');
  4184. Add(' e:=green;');
  4185. Add(' e:=default(TMyEnum);');
  4186. ConvertProgram;
  4187. CheckSource('TestEnumName',
  4188. LinesToStr([ // statements
  4189. 'this.TMyEnum = {',
  4190. ' "0":"Red",',
  4191. ' Red:0,',
  4192. ' "1":"Green",',
  4193. ' Green:1,',
  4194. ' "2":"Blue",',
  4195. ' Blue:2',
  4196. ' };',
  4197. 'this.e = 0;',
  4198. 'this.f = $mod.TMyEnum.Blue;'
  4199. ]),
  4200. LinesToStr([
  4201. '$mod.e=$mod.TMyEnum.Green;',
  4202. '$mod.e=$mod.TMyEnum.Red;'
  4203. ]));
  4204. end;
  4205. procedure TTestModule.TestEnum_Number;
  4206. begin
  4207. Converter.Options:=Converter.Options+[coEnumNumbers];
  4208. StartProgram(false);
  4209. Add('type TMyEnum = (Red, Green);');
  4210. Add('var');
  4211. Add(' e: TMyEnum;');
  4212. Add(' f: TMyEnum = Green;');
  4213. Add(' i: longint;');
  4214. Add('begin');
  4215. Add(' e:=green;');
  4216. Add(' i:=longint(e);');
  4217. ConvertProgram;
  4218. CheckSource('TestEnumNumber',
  4219. LinesToStr([ // statements
  4220. 'this.TMyEnum = {',
  4221. ' "0":"Red",',
  4222. ' Red:0,',
  4223. ' "1":"Green",',
  4224. ' Green:1',
  4225. ' };',
  4226. 'this.e = 0;',
  4227. 'this.f = 1;',
  4228. 'this.i = 0;'
  4229. ]),
  4230. LinesToStr([
  4231. '$mod.e=1;',
  4232. '$mod.i=$mod.e;'
  4233. ]));
  4234. end;
  4235. procedure TTestModule.TestEnum_ConstFail;
  4236. begin
  4237. StartProgram(false);
  4238. Add([
  4239. 'type TMyEnum = (Red = 100, Green = 101);',
  4240. 'var',
  4241. ' e: TMyEnum;',
  4242. ' f: TMyEnum = Green;',
  4243. 'begin',
  4244. ' e:=green;']);
  4245. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] enum const',3002);
  4246. ConvertProgram;
  4247. end;
  4248. procedure TTestModule.TestEnum_Functions;
  4249. begin
  4250. StartProgram(false);
  4251. Add([
  4252. 'type TMyEnum = (Red, Green);',
  4253. 'procedure DoIt(var e: TMyEnum; var i: word);',
  4254. 'var',
  4255. ' v: longint;',
  4256. ' s: string;',
  4257. 'begin',
  4258. ' val(s,e,v);',
  4259. ' val(s,e,i);',
  4260. 'end;',
  4261. 'var',
  4262. ' e: TMyEnum;',
  4263. ' i: longint;',
  4264. ' s: string;',
  4265. ' b: boolean;',
  4266. 'begin',
  4267. ' i:=ord(red);',
  4268. ' i:=ord(green);',
  4269. ' i:=ord(e);',
  4270. ' i:=ord(b);',
  4271. ' e:=low(tmyenum);',
  4272. ' e:=low(e);',
  4273. ' b:=low(boolean);',
  4274. ' e:=high(tmyenum);',
  4275. ' e:=high(e);',
  4276. ' b:=high(boolean);',
  4277. ' e:=pred(green);',
  4278. ' e:=pred(e);',
  4279. ' b:=pred(b);',
  4280. ' e:=succ(red);',
  4281. ' e:=succ(e);',
  4282. ' b:=succ(b);',
  4283. ' e:=tmyenum(1);',
  4284. ' e:=tmyenum(i);',
  4285. ' s:=str(e);',
  4286. ' str(e,s);',
  4287. ' str(red,s);',
  4288. ' s:=str(e:3);',
  4289. ' writestr(s,e:3,red);',
  4290. ' val(s,e,i);',
  4291. ' e:=TMyEnum(i);',
  4292. ' i:=longint(e);']);
  4293. ConvertProgram;
  4294. CheckSource('TestEnum_Functions',
  4295. LinesToStr([ // statements
  4296. 'this.TMyEnum = {',
  4297. ' "0":"Red",',
  4298. ' Red:0,',
  4299. ' "1":"Green",',
  4300. ' Green:1',
  4301. ' };',
  4302. 'this.DoIt = function (e, i) {',
  4303. ' var v = 0;',
  4304. ' var s = "";',
  4305. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  4306. ' v = w;',
  4307. ' }));',
  4308. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  4309. '};',
  4310. 'this.e = 0;',
  4311. 'this.i = 0;',
  4312. 'this.s = "";',
  4313. 'this.b = false;',
  4314. '']),
  4315. LinesToStr([
  4316. '$mod.i=$mod.TMyEnum.Red;',
  4317. '$mod.i=$mod.TMyEnum.Green;',
  4318. '$mod.i=$mod.e;',
  4319. '$mod.i=$mod.b+0;',
  4320. '$mod.e=$mod.TMyEnum.Red;',
  4321. '$mod.e=$mod.TMyEnum.Red;',
  4322. '$mod.b=false;',
  4323. '$mod.e=$mod.TMyEnum.Green;',
  4324. '$mod.e=$mod.TMyEnum.Green;',
  4325. '$mod.b=true;',
  4326. '$mod.e=$mod.TMyEnum.Green-1;',
  4327. '$mod.e=$mod.e-1;',
  4328. '$mod.b=false;',
  4329. '$mod.e=$mod.TMyEnum.Red+1;',
  4330. '$mod.e=$mod.e+1;',
  4331. '$mod.b=true;',
  4332. '$mod.e=1;',
  4333. '$mod.e=$mod.i;',
  4334. '$mod.s = $mod.TMyEnum[$mod.e];',
  4335. '$mod.s = $mod.TMyEnum[$mod.e];',
  4336. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  4337. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  4338. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  4339. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  4340. ' $mod.i = v;',
  4341. '});',
  4342. '$mod.e=$mod.i;',
  4343. '$mod.i=$mod.e;',
  4344. '']));
  4345. end;
  4346. procedure TTestModule.TestEnum_AsParams;
  4347. begin
  4348. StartProgram(false);
  4349. Add('type TEnum = (Red,Blue);');
  4350. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  4351. Add('var vJ: TEnum;');
  4352. Add('begin');
  4353. Add(' vg:=vg;');
  4354. Add(' vj:=vh;');
  4355. Add(' vi:=vi;');
  4356. Add(' doit(vg,vg,vg);');
  4357. Add(' doit(vh,vh,vj);');
  4358. Add(' doit(vi,vi,vi);');
  4359. Add(' doit(vj,vj,vj);');
  4360. Add('end;');
  4361. Add('var i: TEnum;');
  4362. Add('begin');
  4363. Add(' doit(i,i,i);');
  4364. ConvertProgram;
  4365. CheckSource('TestEnum_AsParams',
  4366. LinesToStr([ // statements
  4367. 'this.TEnum = {',
  4368. ' "0": "Red",',
  4369. ' Red: 0,',
  4370. ' "1": "Blue",',
  4371. ' Blue: 1',
  4372. '};',
  4373. 'this.DoIt = function (vG,vH,vI) {',
  4374. ' var vJ = 0;',
  4375. ' vG = vG;',
  4376. ' vJ = vH;',
  4377. ' vI.set(vI.get());',
  4378. ' $mod.DoIt(vG, vG, {',
  4379. ' get: function () {',
  4380. ' return vG;',
  4381. ' },',
  4382. ' set: function (v) {',
  4383. ' vG = v;',
  4384. ' }',
  4385. ' });',
  4386. ' $mod.DoIt(vH, vH, {',
  4387. ' get: function () {',
  4388. ' return vJ;',
  4389. ' },',
  4390. ' set: function (v) {',
  4391. ' vJ = v;',
  4392. ' }',
  4393. ' });',
  4394. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4395. ' $mod.DoIt(vJ, vJ, {',
  4396. ' get: function () {',
  4397. ' return vJ;',
  4398. ' },',
  4399. ' set: function (v) {',
  4400. ' vJ = v;',
  4401. ' }',
  4402. ' });',
  4403. '};',
  4404. 'this.i = 0;'
  4405. ]),
  4406. LinesToStr([
  4407. '$mod.DoIt($mod.i,$mod.i,{',
  4408. ' p: $mod,',
  4409. ' get: function () {',
  4410. ' return this.p.i;',
  4411. ' },',
  4412. ' set: function (v) {',
  4413. ' this.p.i = v;',
  4414. ' }',
  4415. '});'
  4416. ]));
  4417. end;
  4418. procedure TTestModule.TestEnumRange_Array;
  4419. begin
  4420. StartProgram(false);
  4421. Add([
  4422. 'type',
  4423. ' TEnum = (Red, Green, Blue);',
  4424. ' TEnumRg = green..blue;',
  4425. ' TArr = array[TEnumRg] of byte;',
  4426. ' TArr2 = array[green..blue] of byte;',
  4427. 'var',
  4428. ' a: TArr;',
  4429. ' b: TArr = (3,4);',
  4430. ' c: TArr2 = (5,6);',
  4431. 'begin',
  4432. ' a[green] := b[blue];',
  4433. ' c[green] := c[blue];',
  4434. '']);
  4435. ConvertProgram;
  4436. CheckSource('TestEnumRange_Array',
  4437. LinesToStr([ // statements
  4438. 'this.TEnum = {',
  4439. ' "0": "Red",',
  4440. ' Red: 0,',
  4441. ' "1": "Green",',
  4442. ' Green: 1,',
  4443. ' "2": "Blue",',
  4444. ' Blue: 2',
  4445. '};',
  4446. 'this.a = rtl.arraySetLength(null, 0, 2);',
  4447. 'this.b = [3, 4];',
  4448. 'this.c = [5, 6];',
  4449. '']),
  4450. LinesToStr([
  4451. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  4452. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  4453. '']));
  4454. end;
  4455. procedure TTestModule.TestEnum_ForIn;
  4456. begin
  4457. StartProgram(false);
  4458. Add([
  4459. 'type',
  4460. ' TEnum = (Red, Green, Blue);',
  4461. ' TEnumRg = green..blue;',
  4462. ' TArr = array[TEnum] of byte;',
  4463. ' TArrRg = array[TEnumRg] of byte;',
  4464. 'var',
  4465. ' e: TEnum;',
  4466. ' a1: TArr = (3,4,5);',
  4467. ' a2: TArrRg = (11,12);',
  4468. ' b: byte;',
  4469. 'begin',
  4470. ' for e in TEnum do ;',
  4471. ' for e in TEnumRg do ;',
  4472. ' for e in TArr do ;',
  4473. ' for e in TArrRg do ;',
  4474. ' for b in a1 do ;',
  4475. ' for b in a2 do ;',
  4476. '']);
  4477. ConvertProgram;
  4478. CheckSource('TestEnum_ForIn',
  4479. LinesToStr([ // statements
  4480. 'this.TEnum = {',
  4481. ' "0": "Red",',
  4482. ' Red: 0,',
  4483. ' "1": "Green",',
  4484. ' Green: 1,',
  4485. ' "2": "Blue",',
  4486. ' Blue: 2',
  4487. '};',
  4488. 'this.e = 0;',
  4489. 'this.a1 = [3, 4, 5];',
  4490. 'this.a2 = [11, 12];',
  4491. 'this.b = 0;',
  4492. '']),
  4493. LinesToStr([
  4494. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  4495. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  4496. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  4497. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  4498. ' for (var $in1 = $mod.a1, $l2 = 0, $end3 = rtl.length($in1) - 1; $l2 <= $end3; $l2++) $mod.b = $in1[$l2];',
  4499. ' for (var $in4 = $mod.a2, $l5 = 0, $end6 = rtl.length($in4) - 1; $l5 <= $end6; $l5++) $mod.b = $in4[$l5];',
  4500. '']));
  4501. end;
  4502. procedure TTestModule.TestEnum_ScopedNumber;
  4503. begin
  4504. Converter.Options:=Converter.Options+[coEnumNumbers];
  4505. StartProgram(false);
  4506. Add([
  4507. 'type',
  4508. ' TEnum = (Red, Green);',
  4509. 'var',
  4510. ' e: TEnum;',
  4511. 'begin',
  4512. ' e:=TEnum.Green;',
  4513. '']);
  4514. ConvertProgram;
  4515. CheckSource('TestEnum_ScopedNumber',
  4516. LinesToStr([ // statements
  4517. 'this.TEnum = {',
  4518. ' "0": "Red",',
  4519. ' Red: 0,',
  4520. ' "1": "Green",',
  4521. ' Green: 1',
  4522. '};',
  4523. 'this.e = 0;',
  4524. '']),
  4525. LinesToStr([
  4526. '$mod.e = 1;']));
  4527. end;
  4528. procedure TTestModule.TestEnum_InFunction;
  4529. begin
  4530. StartProgram(false);
  4531. Add([
  4532. 'const TEnum = 3;',
  4533. 'procedure DoIt;',
  4534. 'type',
  4535. ' TEnum = (Red, Green, Blue);',
  4536. ' procedure Sub;',
  4537. ' type',
  4538. ' TEnumSub = (Left, Right);',
  4539. ' var',
  4540. ' es: TEnumSub;',
  4541. ' begin',
  4542. ' es:=Left;',
  4543. ' end;',
  4544. 'var',
  4545. ' e, e2: TEnum;',
  4546. 'begin',
  4547. ' if e in [red,blue] then e2:=e;',
  4548. 'end;',
  4549. 'begin']);
  4550. ConvertProgram;
  4551. CheckSource('TestEnum_InFunction',
  4552. LinesToStr([ // statements
  4553. 'this.TEnum = 3;',
  4554. 'var TEnum$1 = {',
  4555. ' "0":"Red",',
  4556. ' Red:0,',
  4557. ' "1":"Green",',
  4558. ' Green:1,',
  4559. ' "2":"Blue",',
  4560. ' Blue:2',
  4561. ' };',
  4562. 'var TEnumSub = {',
  4563. ' "0": "Left",',
  4564. ' Left: 0,',
  4565. ' "1": "Right",',
  4566. ' Right: 1',
  4567. '};',
  4568. 'this.DoIt = function () {',
  4569. ' function Sub() {',
  4570. ' var es = 0;',
  4571. ' es = TEnumSub.Left;',
  4572. ' };',
  4573. ' var e = 0;',
  4574. ' var e2 = 0;',
  4575. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  4576. '};',
  4577. '']),
  4578. LinesToStr([
  4579. '']));
  4580. end;
  4581. procedure TTestModule.TestSet_Enum;
  4582. begin
  4583. StartProgram(false);
  4584. Add([
  4585. 'type',
  4586. ' TColor = (Red, Green, Blue);',
  4587. ' TColors = set of TColor;',
  4588. 'var',
  4589. ' c: TColor;',
  4590. ' s: TColors;',
  4591. ' t: TColors = [];',
  4592. ' u: TColors = [Red];',
  4593. 'begin',
  4594. ' s:=[];',
  4595. ' s:=[Green];',
  4596. ' s:=[Green,Blue];',
  4597. ' s:=[Red..Blue];',
  4598. ' s:=[Red,Green..Blue];',
  4599. ' s:=[Red,c];',
  4600. ' s:=t;',
  4601. ' s:=default(TColors);',
  4602. '']);
  4603. ConvertProgram;
  4604. CheckSource('TestSet',
  4605. LinesToStr([ // statements
  4606. 'this.TColor = {',
  4607. ' "0":"Red",',
  4608. ' Red:0,',
  4609. ' "1":"Green",',
  4610. ' Green:1,',
  4611. ' "2":"Blue",',
  4612. ' Blue:2',
  4613. ' };',
  4614. 'this.c = 0;',
  4615. 'this.s = {};',
  4616. 'this.t = {};',
  4617. 'this.u = rtl.createSet($mod.TColor.Red);'
  4618. ]),
  4619. LinesToStr([
  4620. '$mod.s={};',
  4621. '$mod.s=rtl.createSet($mod.TColor.Green);',
  4622. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  4623. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  4624. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  4625. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  4626. '$mod.s=rtl.refSet($mod.t);',
  4627. '$mod.s={};',
  4628. '']));
  4629. end;
  4630. procedure TTestModule.TestSet_Operators;
  4631. begin
  4632. StartProgram(false);
  4633. Add('type');
  4634. Add(' TColor = (Red, Green, Blue);');
  4635. Add(' TColors = set of tcolor;');
  4636. Add('var');
  4637. Add(' vC: TColor;');
  4638. Add(' vS: TColors;');
  4639. Add(' vT: TColors;');
  4640. Add(' vU: TColors;');
  4641. Add(' B: boolean;');
  4642. Add('begin');
  4643. Add(' include(vs,green);');
  4644. Add(' exclude(vs,vc);');
  4645. Add(' vs:=vt+vu;');
  4646. Add(' vs:=vt+[red];');
  4647. Add(' vs:=[red]+vt;');
  4648. Add(' vs:=[red]+[green];');
  4649. Add(' vs:=vt-vu;');
  4650. Add(' vs:=vt-[red];');
  4651. Add(' vs:=[red]-vt;');
  4652. Add(' vs:=[red]-[green];');
  4653. Add(' vs:=vt*vu;');
  4654. Add(' vs:=vt*[red];');
  4655. Add(' vs:=[red]*vt;');
  4656. Add(' vs:=[red]*[green];');
  4657. Add(' vs:=vt><vu;');
  4658. Add(' vs:=vt><[red];');
  4659. Add(' vs:=[red]><vt;');
  4660. Add(' vs:=[red]><[green];');
  4661. Add(' b:=vt=vu;');
  4662. Add(' b:=vt=[red];');
  4663. Add(' b:=[red]=vt;');
  4664. Add(' b:=[red]=[green];');
  4665. Add(' b:=vt<>vu;');
  4666. Add(' b:=vt<>[red];');
  4667. Add(' b:=[red]<>vt;');
  4668. Add(' b:=[red]<>[green];');
  4669. Add(' b:=vt<=vu;');
  4670. Add(' b:=vt<=[red];');
  4671. Add(' b:=[red]<=vt;');
  4672. Add(' b:=[red]<=[green];');
  4673. Add(' b:=vt>=vu;');
  4674. Add(' b:=vt>=[red];');
  4675. Add(' b:=[red]>=vt;');
  4676. Add(' b:=[red]>=[green];');
  4677. ConvertProgram;
  4678. CheckSource('TestSet_Operators',
  4679. LinesToStr([ // statements
  4680. 'this.TColor = {',
  4681. ' "0":"Red",',
  4682. ' Red:0,',
  4683. ' "1":"Green",',
  4684. ' Green:1,',
  4685. ' "2":"Blue",',
  4686. ' Blue:2',
  4687. ' };',
  4688. 'this.vC = 0;',
  4689. 'this.vS = {};',
  4690. 'this.vT = {};',
  4691. 'this.vU = {};',
  4692. 'this.B = false;'
  4693. ]),
  4694. LinesToStr([
  4695. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  4696. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  4697. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  4698. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  4699. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  4700. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  4701. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  4702. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  4703. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  4704. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  4705. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  4706. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  4707. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  4708. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  4709. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  4710. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  4711. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  4712. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  4713. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  4714. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  4715. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  4716. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  4717. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  4718. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  4719. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  4720. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  4721. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  4722. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  4723. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  4724. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  4725. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  4726. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  4727. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  4728. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  4729. '']));
  4730. end;
  4731. procedure TTestModule.TestSet_Operator_In;
  4732. begin
  4733. StartProgram(false);
  4734. Add('type');
  4735. Add(' TColor = (Red, Green, Blue);');
  4736. Add(' TColors = set of tcolor;');
  4737. Add('var');
  4738. Add(' vC: tcolor;');
  4739. Add(' vT: tcolors;');
  4740. Add(' B: boolean;');
  4741. Add('begin');
  4742. Add(' b:=red in vt;');
  4743. Add(' b:=vc in vt;');
  4744. Add(' b:=green in [red..blue];');
  4745. Add(' b:=vc in [red..blue];');
  4746. Add(' ');
  4747. Add(' if red in vt then ;');
  4748. Add(' while vC in vt do ;');
  4749. Add(' repeat');
  4750. Add(' until vC in vt;');
  4751. ConvertProgram;
  4752. CheckSource('TestSet_Operator_In',
  4753. LinesToStr([ // statements
  4754. 'this.TColor = {',
  4755. ' "0":"Red",',
  4756. ' Red:0,',
  4757. ' "1":"Green",',
  4758. ' Green:1,',
  4759. ' "2":"Blue",',
  4760. ' Blue:2',
  4761. ' };',
  4762. 'this.vC = 0;',
  4763. 'this.vT = {};',
  4764. 'this.B = false;'
  4765. ]),
  4766. LinesToStr([
  4767. '$mod.B = $mod.TColor.Red in $mod.vT;',
  4768. '$mod.B = $mod.vC in $mod.vT;',
  4769. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  4770. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  4771. 'if ($mod.TColor.Red in $mod.vT) ;',
  4772. 'while ($mod.vC in $mod.vT) {',
  4773. '};',
  4774. 'do {',
  4775. '} while (!($mod.vC in $mod.vT));',
  4776. '']));
  4777. end;
  4778. procedure TTestModule.TestSet_Functions;
  4779. begin
  4780. StartProgram(false);
  4781. Add('type');
  4782. Add(' TMyEnum = (Red, Green);');
  4783. Add(' TMyEnums = set of TMyEnum;');
  4784. Add('var');
  4785. Add(' e: TMyEnum;');
  4786. Add(' s: TMyEnums;');
  4787. Add('begin');
  4788. Add(' e:=Low(TMyEnums);');
  4789. Add(' e:=Low(s);');
  4790. Add(' e:=High(TMyEnums);');
  4791. Add(' e:=High(s);');
  4792. ConvertProgram;
  4793. CheckSource('TestSetFunctions',
  4794. LinesToStr([ // statements
  4795. 'this.TMyEnum = {',
  4796. ' "0":"Red",',
  4797. ' Red:0,',
  4798. ' "1":"Green",',
  4799. ' Green:1',
  4800. ' };',
  4801. 'this.e = 0;',
  4802. 'this.s = {};'
  4803. ]),
  4804. LinesToStr([
  4805. '$mod.e=$mod.TMyEnum.Red;',
  4806. '$mod.e=$mod.TMyEnum.Red;',
  4807. '$mod.e=$mod.TMyEnum.Green;',
  4808. '$mod.e=$mod.TMyEnum.Green;',
  4809. '']));
  4810. end;
  4811. procedure TTestModule.TestSet_PassAsArgClone;
  4812. begin
  4813. StartProgram(false);
  4814. Add('type');
  4815. Add(' TMyEnum = (Red, Green);');
  4816. Add(' TMyEnums = set of TMyEnum;');
  4817. Add('procedure DoDefault(s: tmyenums); begin end;');
  4818. Add('procedure DoConst(const s: tmyenums); begin end;');
  4819. Add('var');
  4820. Add(' aSet: tmyenums;');
  4821. Add('begin');
  4822. Add(' dodefault(aset);');
  4823. Add(' doconst(aset);');
  4824. ConvertProgram;
  4825. CheckSource('TestSetFunctions',
  4826. LinesToStr([ // statements
  4827. 'this.TMyEnum = {',
  4828. ' "0":"Red",',
  4829. ' Red:0,',
  4830. ' "1":"Green",',
  4831. ' Green:1',
  4832. ' };',
  4833. 'this.DoDefault = function (s) {',
  4834. '};',
  4835. 'this.DoConst = function (s) {',
  4836. '};',
  4837. 'this.aSet = {};'
  4838. ]),
  4839. LinesToStr([
  4840. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  4841. '$mod.DoConst($mod.aSet);',
  4842. '']));
  4843. end;
  4844. procedure TTestModule.TestSet_AsParams;
  4845. begin
  4846. StartProgram(false);
  4847. Add([
  4848. 'type TEnum = (Red,Blue);',
  4849. 'type TEnums = set of TEnum;',
  4850. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  4851. 'var vJ: TEnums;',
  4852. 'begin',
  4853. ' Include(vg,red);',
  4854. ' Include(result,blue);',
  4855. ' vg:=vg;',
  4856. ' vj:=vh;',
  4857. ' vi:=vi;',
  4858. ' doit(vg,vg,vg);',
  4859. ' doit(vh,vh,vj);',
  4860. ' doit(vi,vi,vi);',
  4861. ' doit(vj,vj,vj);',
  4862. 'end;',
  4863. 'var i: TEnums;',
  4864. 'begin',
  4865. ' doit(i,i,i);']);
  4866. ConvertProgram;
  4867. CheckSource('TestSet_AsParams',
  4868. LinesToStr([ // statements
  4869. 'this.TEnum = {',
  4870. ' "0": "Red",',
  4871. ' Red: 0,',
  4872. ' "1": "Blue",',
  4873. ' Blue: 1',
  4874. '};',
  4875. 'this.DoIt = function (vG,vH,vI) {',
  4876. ' var Result = {};',
  4877. ' var vJ = {};',
  4878. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  4879. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  4880. ' vG = rtl.refSet(vG);',
  4881. ' vJ = rtl.refSet(vH);',
  4882. ' vI.set(rtl.refSet(vI.get()));',
  4883. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  4884. ' get: function () {',
  4885. ' return vG;',
  4886. ' },',
  4887. ' set: function (v) {',
  4888. ' vG = v;',
  4889. ' }',
  4890. ' });',
  4891. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  4892. ' get: function () {',
  4893. ' return vJ;',
  4894. ' },',
  4895. ' set: function (v) {',
  4896. ' vJ = v;',
  4897. ' }',
  4898. ' });',
  4899. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  4900. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  4901. ' get: function () {',
  4902. ' return vJ;',
  4903. ' },',
  4904. ' set: function (v) {',
  4905. ' vJ = v;',
  4906. ' }',
  4907. ' });',
  4908. ' return Result;',
  4909. '};',
  4910. 'this.i = {};'
  4911. ]),
  4912. LinesToStr([
  4913. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  4914. ' p: $mod,',
  4915. ' get: function () {',
  4916. ' return this.p.i;',
  4917. ' },',
  4918. ' set: function (v) {',
  4919. ' this.p.i = v;',
  4920. ' }',
  4921. '});'
  4922. ]));
  4923. end;
  4924. procedure TTestModule.TestSet_Property;
  4925. begin
  4926. StartProgram(false);
  4927. Add('type');
  4928. Add(' TEnum = (Red,Blue);');
  4929. Add(' TEnums = set of TEnum;');
  4930. Add(' TObject = class');
  4931. Add(' function GetColors: TEnums; external name ''GetColors'';');
  4932. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  4933. Add(' property Colors: TEnums read GetColors write SetColors;');
  4934. Add(' end;');
  4935. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  4936. Add('begin end;');
  4937. Add('var Obj: TObject;');
  4938. Add('begin');
  4939. Add(' Include(Obj.Colors,Red);');
  4940. Add(' Exclude(Obj.Colors,Red);');
  4941. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  4942. ConvertProgram;
  4943. CheckSource('TestSet_Property',
  4944. LinesToStr([ // statements
  4945. 'this.TEnum = {',
  4946. ' "0": "Red",',
  4947. ' Red: 0,',
  4948. ' "1": "Blue",',
  4949. ' Blue: 1',
  4950. '};',
  4951. 'rtl.createClass($mod, "TObject", null, function () {',
  4952. ' this.$init = function () {',
  4953. ' };',
  4954. ' this.$final = function () {',
  4955. ' };',
  4956. '});',
  4957. 'this.DoIt = function (i, j, k, l) {',
  4958. '};',
  4959. 'this.Obj = null;',
  4960. '']),
  4961. LinesToStr([
  4962. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  4963. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  4964. '']));
  4965. end;
  4966. procedure TTestModule.TestSet_EnumConst;
  4967. begin
  4968. StartProgram(false);
  4969. Add([
  4970. 'type',
  4971. ' TEnum = (Red,Blue);',
  4972. ' TEnums = set of TEnum;',
  4973. 'const',
  4974. ' Orange = red;',
  4975. 'var',
  4976. ' Enum: tenum;',
  4977. ' Enums: tenums;',
  4978. 'begin',
  4979. ' Include(enums,orange);',
  4980. ' Exclude(enums,orange);',
  4981. ' if orange in enums then;',
  4982. ' if orange in [orange,red] then;']);
  4983. ConvertProgram;
  4984. CheckSource('TestSet_EnumConst',
  4985. LinesToStr([ // statements
  4986. 'this.TEnum = {',
  4987. ' "0": "Red",',
  4988. ' Red: 0,',
  4989. ' "1": "Blue",',
  4990. ' Blue: 1',
  4991. '};',
  4992. 'this.Orange = $mod.TEnum.Red;',
  4993. 'this.Enum = 0;',
  4994. 'this.Enums = {};',
  4995. '']),
  4996. LinesToStr([
  4997. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  4998. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  4999. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  5000. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  5001. '']));
  5002. end;
  5003. procedure TTestModule.TestSet_IntConst;
  5004. begin
  5005. StartProgram(false);
  5006. Add([
  5007. 'type',
  5008. ' TEnums = set of Byte;',
  5009. 'const',
  5010. ' Orange = 0;',
  5011. 'var',
  5012. ' Enum: byte;',
  5013. ' Enums: tenums;',
  5014. 'begin',
  5015. ' Enums:=[];',
  5016. ' Enums:=[0];',
  5017. ' Enums:=[1..2];',
  5018. //' Include(enums,orange);',
  5019. //' Exclude(enums,orange);',
  5020. ' if orange in enums then;',
  5021. ' if orange in [orange,1] then;']);
  5022. ConvertProgram;
  5023. CheckSource('TestSet_IntConst',
  5024. LinesToStr([ // statements
  5025. 'this.Orange = 0;',
  5026. 'this.Enum = 0;',
  5027. 'this.Enums = {};',
  5028. '']),
  5029. LinesToStr([
  5030. '$mod.Enums = {};',
  5031. '$mod.Enums = rtl.createSet(0);',
  5032. '$mod.Enums = rtl.createSet(null, 1, 2);',
  5033. 'if (0 in $mod.Enums) ;',
  5034. 'if (0 in rtl.createSet(0, 1)) ;',
  5035. '']));
  5036. end;
  5037. procedure TTestModule.TestSet_AnonymousEnumType;
  5038. begin
  5039. StartProgram(false);
  5040. Add('type');
  5041. Add(' TFlags = set of (red, green);');
  5042. Add('const');
  5043. Add(' favorite = red;');
  5044. Add('var');
  5045. Add(' f: TFlags;');
  5046. Add(' i: longint;');
  5047. Add('begin');
  5048. Add(' Include(f,red);');
  5049. Add(' Include(f,favorite);');
  5050. Add(' i:=ord(red);');
  5051. Add(' i:=ord(favorite);');
  5052. Add(' i:=ord(low(TFlags));');
  5053. Add(' i:=ord(low(f));');
  5054. Add(' i:=ord(low(favorite));');
  5055. Add(' i:=ord(high(TFlags));');
  5056. Add(' i:=ord(high(f));');
  5057. Add(' i:=ord(high(favorite));');
  5058. Add(' f:=[green,favorite];');
  5059. ConvertProgram;
  5060. CheckSource('TestSet_AnonymousEnumType',
  5061. LinesToStr([ // statements
  5062. 'this.TFlags$a = {',
  5063. ' "0": "red",',
  5064. ' red: 0,',
  5065. ' "1": "green",',
  5066. ' green: 1',
  5067. '};',
  5068. 'this.favorite = $mod.TFlags$a.red;',
  5069. 'this.f = {};',
  5070. 'this.i = 0;',
  5071. '']),
  5072. LinesToStr([
  5073. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  5074. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  5075. '$mod.i = $mod.TFlags$a.red;',
  5076. '$mod.i = $mod.TFlags$a.red;',
  5077. '$mod.i = $mod.TFlags$a.red;',
  5078. '$mod.i = $mod.TFlags$a.red;',
  5079. '$mod.i = $mod.TFlags$a.red;',
  5080. '$mod.i = $mod.TFlags$a.green;',
  5081. '$mod.i = $mod.TFlags$a.green;',
  5082. '$mod.i = $mod.TFlags$a.green;',
  5083. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  5084. '']));
  5085. end;
  5086. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  5087. begin
  5088. exit;
  5089. StartProgram(false);
  5090. Add([
  5091. 'type',
  5092. ' TAtoZ = ''A''..''Z'';',
  5093. ' TSetOfAZ = set of TAtoZ;',
  5094. 'var',
  5095. ' c: char;',
  5096. ' a: TAtoZ;',
  5097. ' s: TSetOfAZ = [''P'',''A''];',
  5098. ' i: longint;',
  5099. 'begin',
  5100. ' Include(s,''S'');',
  5101. ' Include(s,c);',
  5102. ' Include(s,a);',
  5103. ' c:=low(TAtoZ);',
  5104. ' i:=ord(low(TAtoZ));',
  5105. ' a:=high(TAtoZ);',
  5106. ' a:=high(TSetOfAtoZ);',
  5107. ' s:=[a,c,''M''];',
  5108. '']);
  5109. ConvertProgram;
  5110. CheckSource('TestSet_AnonymousEnumTypeChar',
  5111. LinesToStr([ // statements
  5112. '']),
  5113. LinesToStr([
  5114. '']));
  5115. end;
  5116. procedure TTestModule.TestSet_ConstEnum;
  5117. begin
  5118. StartProgram(false);
  5119. Add([
  5120. 'type',
  5121. ' TEnum = (red,blue,green);',
  5122. ' TEnums = set of TEnum;',
  5123. 'const',
  5124. ' teAny = [low(TEnum)..high(TEnum)];',
  5125. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  5126. 'var',
  5127. ' e: TEnum;',
  5128. ' s: TEnums;',
  5129. 'begin',
  5130. ' if blue in teAny then;',
  5131. ' if blue in teAny+[e] then;',
  5132. ' if blue in teAny+teRedBlue then;',
  5133. ' if e in [red,blue] then;',
  5134. ' s:=teAny;',
  5135. ' s:=teAny+[e];',
  5136. ' s:=[e]+teAny;',
  5137. ' s:=teAny+teRedBlue;',
  5138. ' s:=teAny+teRedBlue+[e];',
  5139. '']);
  5140. ConvertProgram;
  5141. CheckSource('TestSet_ConstEnum',
  5142. LinesToStr([ // statements
  5143. 'this.TEnum = {',
  5144. ' "0": "red",',
  5145. ' red: 0,',
  5146. ' "1": "blue",',
  5147. ' blue: 1,',
  5148. ' "2": "green",',
  5149. ' green: 2',
  5150. '};',
  5151. 'this.teAny = rtl.createSet(null, $mod.TEnum.red, $mod.TEnum.green);',
  5152. 'this.teRedBlue = rtl.createSet(null, $mod.TEnum.red, $mod.TEnum.green - 1);',
  5153. 'this.e = 0;',
  5154. 'this.s = {};',
  5155. '']),
  5156. LinesToStr([
  5157. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  5158. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  5159. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  5160. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  5161. '$mod.s = rtl.refSet($mod.teAny);',
  5162. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  5163. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  5164. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  5165. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  5166. '']));
  5167. end;
  5168. procedure TTestModule.TestSet_ConstChar;
  5169. begin
  5170. StartProgram(false);
  5171. Add([
  5172. 'const',
  5173. ' LowChars = [''a''..''z''];',
  5174. ' Chars = LowChars+[''A''..''Z''];',
  5175. 'var',
  5176. ' c: char;',
  5177. ' s: string;',
  5178. 'begin',
  5179. ' if c in lowchars then ;',
  5180. ' if ''a'' in lowchars then ;',
  5181. ' if s[1] in lowchars then ;',
  5182. ' if c in chars then ;',
  5183. ' if c in [''a''..''z'',''_''] then ;',
  5184. ' if ''b'' in [''a''..''z'',''_''] then ;',
  5185. '']);
  5186. ConvertProgram;
  5187. CheckSource('TestSet_ConstChar',
  5188. LinesToStr([ // statements
  5189. 'this.LowChars = rtl.createSet(null, 97, 122);',
  5190. 'this.Chars = rtl.unionSet($mod.LowChars, rtl.createSet(null, 65, 90));',
  5191. 'this.c = "";',
  5192. 'this.s = "";',
  5193. '']),
  5194. LinesToStr([
  5195. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  5196. 'if (97 in $mod.LowChars) ;',
  5197. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  5198. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  5199. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  5200. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  5201. '']));
  5202. end;
  5203. procedure TTestModule.TestSet_ConstInt;
  5204. begin
  5205. StartProgram(false);
  5206. Add([
  5207. 'const',
  5208. ' Months = [1..12];',
  5209. ' Mirror = [-12..-1]+Months;',
  5210. 'var',
  5211. ' i: smallint;',
  5212. 'begin',
  5213. ' if 3 in Months then;',
  5214. ' if i in Months+[i] then;',
  5215. ' if i in Months+Mirror then;',
  5216. ' if i in [4..6,8] then;',
  5217. '']);
  5218. ConvertProgram;
  5219. CheckSource('TestSet_ConstInt',
  5220. LinesToStr([ // statements
  5221. 'this.Months = rtl.createSet(null, 1, 12);',
  5222. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), $mod.Months);',
  5223. 'this.i = 0;',
  5224. '']),
  5225. LinesToStr([
  5226. 'if (3 in $mod.Months) ;',
  5227. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  5228. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  5229. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  5230. '']));
  5231. end;
  5232. procedure TTestModule.TestSet_InFunction;
  5233. begin
  5234. StartProgram(false);
  5235. Add([
  5236. 'const',
  5237. ' TEnum = 3;',
  5238. ' TSetOfEnum = 4;',
  5239. ' TSetOfAno = 5;',
  5240. 'procedure DoIt;',
  5241. 'type',
  5242. ' TEnum = (red, blue);',
  5243. ' TSetOfEnum = set of TEnum;',
  5244. ' TSetOfAno = set of (up,down);',
  5245. 'var',
  5246. ' e: TEnum;',
  5247. ' se: TSetOfEnum;',
  5248. ' sa: TSetOfAno;',
  5249. 'begin',
  5250. ' se:=[e];',
  5251. ' sa:=[up];',
  5252. 'end;',
  5253. 'begin',
  5254. '']);
  5255. ConvertProgram;
  5256. CheckSource('TestSet_InFunction',
  5257. LinesToStr([ // statements
  5258. 'this.TEnum = 3;',
  5259. 'this.TSetOfEnum = 4;',
  5260. 'this.TSetOfAno = 5;',
  5261. 'var TEnum$1 = {',
  5262. ' "0": "red",',
  5263. ' red: 0,',
  5264. ' "1": "blue",',
  5265. ' blue: 1',
  5266. '};',
  5267. 'var TSetOfAno$a = {',
  5268. ' "0": "up",',
  5269. ' up: 0,',
  5270. ' "1": "down",',
  5271. ' down: 1',
  5272. '};',
  5273. 'this.DoIt = function () {',
  5274. ' var e = 0;',
  5275. ' var se = {};',
  5276. ' var sa = {};',
  5277. ' se = rtl.createSet(e);',
  5278. ' sa = rtl.createSet(TSetOfAno$a.up);',
  5279. '};',
  5280. '']),
  5281. LinesToStr([
  5282. '']));
  5283. end;
  5284. procedure TTestModule.TestSet_ForIn;
  5285. begin
  5286. StartProgram(false);
  5287. Add([
  5288. 'type',
  5289. ' TEnum = (Red, Green, Blue);',
  5290. ' TEnumRg = green..blue;',
  5291. ' TSetOfEnum = set of TEnum;',
  5292. ' TSetOfEnumRg = set of TEnumRg;',
  5293. 'var',
  5294. ' e, e2: TEnum;',
  5295. ' er: TEnum;',
  5296. ' s: TSetOfEnum;',
  5297. 'begin',
  5298. ' for e in TSetOfEnum do ;',
  5299. ' for e in TSetOfEnumRg do ;',
  5300. ' for e in [] do e2:=e;',
  5301. ' for e in [red..green] do e2:=e;',
  5302. ' for e in [green,blue] do e2:=e;',
  5303. ' for e in [red,blue] do e2:=e;',
  5304. ' for e in s do e2:=e;',
  5305. ' for er in TSetOfEnumRg do ;',
  5306. '']);
  5307. ConvertProgram;
  5308. CheckSource('TestSet_ForIn',
  5309. LinesToStr([ // statements
  5310. 'this.TEnum = {',
  5311. ' "0":"Red",',
  5312. ' Red:0,',
  5313. ' "1":"Green",',
  5314. ' Green:1,',
  5315. ' "2":"Blue",',
  5316. ' Blue:2',
  5317. ' };',
  5318. 'this.e = 0;',
  5319. 'this.e2 = 0;',
  5320. 'this.er = 0;',
  5321. 'this.s = {};',
  5322. '']),
  5323. LinesToStr([
  5324. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5325. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5326. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  5327. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  5328. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  5329. 'for (var $l1 in $mod.s){',
  5330. ' $mod.e = +$l1;',
  5331. ' $mod.e2 = $mod.e;',
  5332. '};',
  5333. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  5334. '']));
  5335. end;
  5336. procedure TTestModule.TestNestBegin;
  5337. begin
  5338. StartProgram(false);
  5339. Add('begin');
  5340. Add(' begin');
  5341. Add(' begin');
  5342. Add(' end;');
  5343. Add(' begin');
  5344. Add(' if true then ;');
  5345. Add(' end;');
  5346. Add(' end;');
  5347. ConvertProgram;
  5348. CheckSource('TestNestBegin',
  5349. '',
  5350. 'if (true) ;');
  5351. end;
  5352. procedure TTestModule.TestUnitImplVars;
  5353. begin
  5354. StartUnit(false);
  5355. Add('interface');
  5356. Add('implementation');
  5357. Add('var');
  5358. Add(' V1:longint;');
  5359. Add(' V2:longint = 3;');
  5360. Add(' V3:string = ''abc'';');
  5361. ConvertUnit;
  5362. CheckSource('TestUnitImplVars',
  5363. LinesToStr([ // statements
  5364. 'var $impl = $mod.$impl;',
  5365. '']),
  5366. '', // this.$init
  5367. LinesToStr([ // implementation
  5368. '$impl.V1 = 0;',
  5369. '$impl.V2 = 3;',
  5370. '$impl.V3 = "abc";',
  5371. '']) );
  5372. end;
  5373. procedure TTestModule.TestUnitImplConsts;
  5374. begin
  5375. StartUnit(false);
  5376. Add('interface');
  5377. Add('implementation');
  5378. Add('const');
  5379. Add(' v1 = 3;');
  5380. Add(' v2:longint = 4;');
  5381. Add(' v3:string = ''abc'';');
  5382. ConvertUnit;
  5383. CheckSource('TestUnitImplConsts',
  5384. LinesToStr([ // statements
  5385. 'var $impl = $mod.$impl;',
  5386. '']),
  5387. '', // this.$init
  5388. LinesToStr([ // implementation
  5389. '$impl.v1 = 3;',
  5390. '$impl.v2 = 4;',
  5391. '$impl.v3 = "abc";',
  5392. '']) );
  5393. end;
  5394. procedure TTestModule.TestUnitImplRecord;
  5395. begin
  5396. StartUnit(false);
  5397. Add('interface');
  5398. Add('implementation');
  5399. Add('type');
  5400. Add(' TMyRecord = record');
  5401. Add(' i: longint;');
  5402. Add(' end;');
  5403. Add('var aRec: TMyRecord;');
  5404. Add('initialization');
  5405. Add(' arec.i:=3;');
  5406. ConvertUnit;
  5407. CheckSource('TestUnitImplRecord',
  5408. LinesToStr([ // statements
  5409. 'var $impl = $mod.$impl;',
  5410. '']),
  5411. // this.$init
  5412. '$impl.aRec.i = 3;',
  5413. LinesToStr([ // implementation
  5414. 'rtl.recNewT($impl, "TMyRecord", function () {',
  5415. ' this.i = 0;',
  5416. ' this.$eq = function (b) {',
  5417. ' return this.i === b.i;',
  5418. ' };',
  5419. ' this.$assign = function (s) {',
  5420. ' this.i = s.i;',
  5421. ' return this;',
  5422. ' };',
  5423. '});',
  5424. '$impl.aRec = $impl.TMyRecord.$new();',
  5425. '']) );
  5426. end;
  5427. procedure TTestModule.TestRenameJSNameConflict;
  5428. begin
  5429. StartProgram(false);
  5430. Add('var apply: longint;');
  5431. Add('var bind: longint;');
  5432. Add('var call: longint;');
  5433. Add('begin');
  5434. ConvertProgram;
  5435. CheckSource('TestRenameJSNameConflict',
  5436. LinesToStr([ // statements
  5437. 'this.Apply = 0;',
  5438. 'this.Bind = 0;',
  5439. 'this.Call = 0;'
  5440. ]),
  5441. LinesToStr([ // this.$main
  5442. ''
  5443. ]));
  5444. end;
  5445. procedure TTestModule.TestLocalConst;
  5446. begin
  5447. StartProgram(false);
  5448. Add('procedure DoIt;');
  5449. Add('const');
  5450. Add(' cA: longint = 1;');
  5451. Add(' cB = 2;');
  5452. Add(' procedure Sub;');
  5453. Add(' const');
  5454. Add(' csA = 3;');
  5455. Add(' cB: double = 4;');
  5456. Add(' begin');
  5457. Add(' cb:=cb+csa;');
  5458. Add(' ca:=ca+csa+5;');
  5459. Add(' end;');
  5460. Add('begin');
  5461. Add(' ca:=ca+cb+6;');
  5462. Add('end;');
  5463. Add('begin');
  5464. ConvertProgram;
  5465. CheckSource('TestLocalConst',
  5466. LinesToStr([
  5467. 'var cA = 1;',
  5468. 'var cB = 2;',
  5469. 'var csA = 3;',
  5470. 'var cB$1 = 4;',
  5471. 'this.DoIt = function () {',
  5472. ' function Sub() {',
  5473. ' cB$1 = cB$1 + 3;',
  5474. ' cA = cA + 3 + 5;',
  5475. ' };',
  5476. ' cA = cA + 2 + 6;',
  5477. '};'
  5478. ]),
  5479. LinesToStr([
  5480. ]));
  5481. end;
  5482. procedure TTestModule.TestVarExternal;
  5483. begin
  5484. StartProgram(false);
  5485. Add('var');
  5486. Add(' NaN: double; external name ''Global.NaN'';');
  5487. Add(' d: double;');
  5488. Add('begin');
  5489. Add(' d:=NaN;');
  5490. ConvertProgram;
  5491. CheckSource('TestVarExternal',
  5492. LinesToStr([
  5493. 'this.d = 0.0;'
  5494. ]),
  5495. LinesToStr([
  5496. '$mod.d = Global.NaN;'
  5497. ]));
  5498. end;
  5499. procedure TTestModule.TestVarExternalOtherUnit;
  5500. begin
  5501. AddModuleWithIntfImplSrc('unit2.pas',
  5502. LinesToStr([
  5503. 'var NaN: double; external name ''Global.NaN'';',
  5504. 'var iV: longint;'
  5505. ]),
  5506. '');
  5507. StartUnit(true);
  5508. Add('interface');
  5509. Add('uses unit2;');
  5510. Add('implementation');
  5511. Add('var');
  5512. Add(' d: double;');
  5513. Add(' i: longint; external name ''$i'';');
  5514. Add('begin');
  5515. Add(' d:=nan;');
  5516. Add(' d:=uNit2.nan;');
  5517. Add(' d:=test1.d;');
  5518. Add(' i:=iv;');
  5519. Add(' i:=uNit2.iv;');
  5520. Add(' i:=test1.i;');
  5521. ConvertUnit;
  5522. CheckSource('TestVarExternalOtherUnit',
  5523. LinesToStr([
  5524. 'var $impl = $mod.$impl;',
  5525. '']),
  5526. LinesToStr([ // this.$init
  5527. '$impl.d = Global.NaN;',
  5528. '$impl.d = Global.NaN;',
  5529. '$impl.d = $impl.d;',
  5530. '$i = pas.unit2.iV;',
  5531. '$i = pas.unit2.iV;',
  5532. '$i = $i;',
  5533. '']),
  5534. LinesToStr([ // implementation
  5535. '$impl.d = 0.0;',
  5536. '']) );
  5537. end;
  5538. procedure TTestModule.TestVarAbsoluteFail;
  5539. begin
  5540. StartProgram(false);
  5541. Add([
  5542. 'var',
  5543. ' a: longint;',
  5544. ' b: longword absolute a;',
  5545. 'begin']);
  5546. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  5547. ConvertProgram;
  5548. end;
  5549. procedure TTestModule.TestConstExternal;
  5550. begin
  5551. StartProgram(false);
  5552. Add([
  5553. 'const',
  5554. ' PI: double; external name ''Global.PI'';',
  5555. ' Tau = 2*pi;',
  5556. 'var d: double;',
  5557. 'begin',
  5558. ' d:=pi;',
  5559. ' d:=tau+pi;']);
  5560. ConvertProgram;
  5561. CheckSource('TestConstExternal',
  5562. LinesToStr([
  5563. 'this.Tau = 2*Global.PI;',
  5564. 'this.d = 0.0;'
  5565. ]),
  5566. LinesToStr([
  5567. '$mod.d = Global.PI;',
  5568. '$mod.d = $mod.Tau + Global.PI;'
  5569. ]));
  5570. end;
  5571. procedure TTestModule.TestDouble;
  5572. begin
  5573. StartProgram(false);
  5574. Add([
  5575. 'type',
  5576. ' TDateTime = double;',
  5577. 'const',
  5578. ' a = TDateTime(2.7);',
  5579. ' b = a + TDateTime(1.7);',
  5580. ' c = 0.9 + 0.1;',
  5581. ' f0_1 = 0.1;',
  5582. ' f0_3 = 0.3;',
  5583. ' fn0_1 = -0.1;',
  5584. ' fn0_3 = -0.3;',
  5585. ' fn0_003 = -0.003;',
  5586. ' fn0_123456789 = -0.123456789;',
  5587. ' fn300_0 = -300.0;',
  5588. ' fn123456_0 = -123456.0;',
  5589. ' fn1234567_8 = -1234567.8;',
  5590. ' fn12345678_9 = -12345678.9;',
  5591. ' f1_0En12 = 1E-12;',
  5592. ' fn1_0En12 = -1E-12;',
  5593. ' maxdouble = 1.7e+308;',
  5594. ' mindouble = -1.7e+308;',
  5595. ' MinSafeIntDouble = -$10000000000000;',
  5596. ' MaxSafeIntDouble = $fffffffffffff;',
  5597. 'var',
  5598. ' d: double = b;',
  5599. 'begin',
  5600. ' d:=1.0;',
  5601. ' d:=1.0/3.0;',
  5602. ' d:=1/3;',
  5603. ' d:=5.0E-324;',
  5604. ' d:=1.7E308;',
  5605. ' d:=001.00E00;',
  5606. ' d:=002.00E001;',
  5607. ' d:=-003.00E-00;',
  5608. ' d:=-004.00E-001;',
  5609. ' d:=10**3;',
  5610. ' d:=10 mod 3;',
  5611. ' d:=10 div 3;',
  5612. ' d:=c;',
  5613. ' d:=f0_1;',
  5614. ' d:=f0_3;',
  5615. ' d:=fn0_1;',
  5616. ' d:=fn0_3;',
  5617. ' d:=fn0_003;',
  5618. ' d:=fn0_123456789;',
  5619. ' d:=fn300_0;',
  5620. ' d:=fn123456_0;',
  5621. ' d:=fn1234567_8;',
  5622. ' d:=fn12345678_9;',
  5623. ' d:=f1_0En12;',
  5624. ' d:=fn1_0En12;',
  5625. ' d:=maxdouble;',
  5626. ' d:=mindouble;',
  5627. ' d:=MinSafeIntDouble;',
  5628. ' d:=MaxSafeIntDouble;',
  5629. ' d:=default(double);',
  5630. '']);
  5631. ConvertProgram;
  5632. CheckSource('TestDouble',
  5633. LinesToStr([
  5634. 'this.a = 2.7;',
  5635. 'this.b = 2.7 + 1.7;',
  5636. 'this.c = 0.9 + 0.1;',
  5637. 'this.f0_1 = 0.1;',
  5638. 'this.f0_3 = 0.3;',
  5639. 'this.fn0_1 = -0.1;',
  5640. 'this.fn0_3 = -0.3;',
  5641. 'this.fn0_003 = -0.003;',
  5642. 'this.fn0_123456789 = -0.123456789;',
  5643. 'this.fn300_0 = -300.0;',
  5644. 'this.fn123456_0 = -123456.0;',
  5645. 'this.fn1234567_8 = -1234567.8;',
  5646. 'this.fn12345678_9 = -12345678.9;',
  5647. 'this.f1_0En12 = 1E-12;',
  5648. 'this.fn1_0En12 = -1E-12;',
  5649. 'this.maxdouble = 1.7e+308;',
  5650. 'this.mindouble = -1.7e+308;',
  5651. 'this.MinSafeIntDouble = -0x10000000000000;',
  5652. 'this.MaxSafeIntDouble = 0xfffffffffffff;',
  5653. 'this.d = 4.4;'
  5654. ]),
  5655. LinesToStr([
  5656. '$mod.d = 1.0;',
  5657. '$mod.d = 1.0 / 3.0;',
  5658. '$mod.d = 1 / 3;',
  5659. '$mod.d = 5.0E-324;',
  5660. '$mod.d = 1.7E308;',
  5661. '$mod.d = 1.00E0;',
  5662. '$mod.d = 2.00E1;',
  5663. '$mod.d = -3.00E-0;',
  5664. '$mod.d = -4.00E-1;',
  5665. '$mod.d = Math.pow(10, 3);',
  5666. '$mod.d = 10 % 3;',
  5667. '$mod.d = Math.floor(10 / 3);',
  5668. '$mod.d = 1;',
  5669. '$mod.d = 0.1;',
  5670. '$mod.d = 0.3;',
  5671. '$mod.d = -0.1;',
  5672. '$mod.d = -0.3;',
  5673. '$mod.d = -0.003;',
  5674. '$mod.d = -0.123456789;',
  5675. '$mod.d = -300;',
  5676. '$mod.d = -123456;',
  5677. '$mod.d = -1234567.8;',
  5678. '$mod.d = -1.23456789E7;',
  5679. '$mod.d = 1E-12;',
  5680. '$mod.d = -1E-12;',
  5681. '$mod.d = 1.7E308;',
  5682. '$mod.d = -1.7E308;',
  5683. '$mod.d = -4503599627370496;',
  5684. '$mod.d = 4503599627370495;',
  5685. '$mod.d = 0.0;',
  5686. '']));
  5687. end;
  5688. procedure TTestModule.TestInteger;
  5689. begin
  5690. StartProgram(false);
  5691. Add([
  5692. 'const',
  5693. ' MinInt = low(NativeInt);',
  5694. ' MaxInt = high(NativeInt);',
  5695. 'type',
  5696. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  5697. 'const',
  5698. ' a = low(TMyInt)+High(TMyInt);',
  5699. 'var',
  5700. ' i: TMyInt;',
  5701. 'begin',
  5702. ' i:=-MinInt;',
  5703. ' i:=default(TMyInt);',
  5704. ' i:=low(i)+high(i);',
  5705. '']);
  5706. ConvertProgram;
  5707. CheckSource('TestIntegerRange',
  5708. LinesToStr([
  5709. 'this.MinInt = -4503599627370496;',
  5710. 'this.MaxInt = 4503599627370495;',
  5711. 'this.a = -4503599627370496 + 4503599627370495;',
  5712. 'this.i = 0;',
  5713. '']),
  5714. LinesToStr([
  5715. '$mod.i = - -4503599627370496;',
  5716. '$mod.i = -4503599627370496;',
  5717. '$mod.i = -4503599627370496 + 4503599627370495;',
  5718. '']));
  5719. end;
  5720. procedure TTestModule.TestIntegerRange;
  5721. begin
  5722. StartProgram(false);
  5723. Add([
  5724. 'const',
  5725. ' MinInt = -1;',
  5726. ' MaxInt = +1;',
  5727. 'type',
  5728. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  5729. ' TInt2 = 1..3;',
  5730. 'const',
  5731. ' a = low(TMyInt)+High(TMyInt);',
  5732. ' b = low(TInt2)+High(TInt2);',
  5733. ' s1 = [1];',
  5734. ' s2 = [1,2];',
  5735. ' s3 = [1..3];',
  5736. ' s4 = [low(shortint)..high(shortint)];',
  5737. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  5738. ' s6 = 1 in s2;',
  5739. 'var',
  5740. ' i: TMyInt;',
  5741. ' i2: TInt2;',
  5742. 'begin',
  5743. ' i:=i2;',
  5744. ' i:=default(TMyInt);',
  5745. ' if i=i2 then ;']);
  5746. ConvertProgram;
  5747. CheckSource('TestIntegerRange',
  5748. LinesToStr([
  5749. 'this.MinInt = -1;',
  5750. 'this.MaxInt = +1;',
  5751. 'this.a = -1 + 1;',
  5752. 'this.b = 1 + 3;',
  5753. 'this.s1 = rtl.createSet(1);',
  5754. 'this.s2 = rtl.createSet(1, 2);',
  5755. 'this.s3 = rtl.createSet(null, 1, 3);',
  5756. 'this.s4 = rtl.createSet(null, -128, 127);',
  5757. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  5758. 'this.s6 = 1 in $mod.s2;',
  5759. 'this.i = 0;',
  5760. 'this.i2 = 0;',
  5761. '']),
  5762. LinesToStr([
  5763. '$mod.i = $mod.i2;',
  5764. '$mod.i = -1;',
  5765. 'if ($mod.i === $mod.i2) ;',
  5766. '']));
  5767. end;
  5768. procedure TTestModule.TestIntegerTypecasts;
  5769. begin
  5770. StartProgram(false);
  5771. Add([
  5772. 'var',
  5773. ' i: nativeint;',
  5774. ' b: byte;',
  5775. ' sh: shortint;',
  5776. ' w: word;',
  5777. ' sm: smallint;',
  5778. ' lw: longword;',
  5779. ' li: longint;',
  5780. 'begin',
  5781. ' b:=byte(i);',
  5782. ' sh:=shortint(i);',
  5783. ' w:=word(i);',
  5784. ' sm:=smallint(i);',
  5785. ' lw:=longword(i);',
  5786. ' li:=longint(i);',
  5787. '']);
  5788. ConvertProgram;
  5789. CheckSource('TestIntegerTypecasts',
  5790. LinesToStr([
  5791. 'this.i = 0;',
  5792. 'this.b = 0;',
  5793. 'this.sh = 0;',
  5794. 'this.w = 0;',
  5795. 'this.sm = 0;',
  5796. 'this.lw = 0;',
  5797. 'this.li = 0;',
  5798. '']),
  5799. LinesToStr([
  5800. '$mod.b = $mod.i & 255;',
  5801. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  5802. '$mod.w = $mod.i & 65535;',
  5803. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  5804. '$mod.lw = $mod.i >>> 0;',
  5805. '$mod.li = $mod.i & 0xFFFFFFFF;',
  5806. '']));
  5807. end;
  5808. procedure TTestModule.TestCurrency;
  5809. begin
  5810. StartProgram(false);
  5811. Add([
  5812. 'type',
  5813. ' TCoin = currency;',
  5814. 'const',
  5815. ' a = TCoin(2.7);',
  5816. ' b = a + TCoin(1.7);',
  5817. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  5818. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  5819. 'var',
  5820. ' c: TCoin = b;',
  5821. ' i: nativeint;',
  5822. ' d: double;',
  5823. ' j: jsvalue;',
  5824. 'function DoIt(c: currency): currency; begin end;',
  5825. 'function GetIt(d: double): double; begin end;',
  5826. 'procedure Write(v: jsvalue); begin end;',
  5827. 'begin',
  5828. ' c:=1.0;',
  5829. ' c:=0.1;',
  5830. ' c:=1.0/3.0;',
  5831. ' c:=1/3;',
  5832. ' c:=a;',
  5833. ' d:=c;',
  5834. ' c:=d;',
  5835. ' c:=currency(c);',
  5836. ' c:=currency(d);',
  5837. ' d:=double(c);',
  5838. ' c:=i;',
  5839. ' c:=currency(i);',
  5840. //' i:=c;', not allowed
  5841. ' i:=nativeint(c);',
  5842. ' c:=c+a;',
  5843. ' c:=-c-a;',
  5844. ' c:=d+c;',
  5845. ' c:=c+d;',
  5846. ' c:=d-c;',
  5847. ' c:=c-d;',
  5848. ' c:=c*a;',
  5849. ' c:=a*c;',
  5850. ' c:=d*c;',
  5851. ' c:=c*d;',
  5852. ' c:=c/a;',
  5853. ' c:=a/c;',
  5854. ' c:=d/c;',
  5855. ' c:=c/d;',
  5856. ' c:=c**a;',
  5857. ' c:=a**c;',
  5858. ' c:=d**c;',
  5859. ' c:=c**d;',
  5860. ' if c=c then ;',
  5861. ' if c=a then ;',
  5862. ' if a=c then ;',
  5863. ' if d=c then ;',
  5864. ' if c=d then ;',
  5865. ' c:=DoIt(c);',
  5866. ' c:=DoIt(i);',
  5867. ' c:=DoIt(d);',
  5868. ' c:=GetIt(c);',
  5869. ' j:=c;',
  5870. ' Write(c);',
  5871. ' c:=default(currency);',
  5872. ' j:=str(c);',
  5873. ' j:=str(c:0:3);',
  5874. '']);
  5875. ConvertProgram;
  5876. CheckSource('TestCurrency',
  5877. LinesToStr([
  5878. 'this.a = 27000;',
  5879. 'this.b = $mod.a + 17000;',
  5880. 'this.MinSafeIntCurrency = -92233720368.5477;',
  5881. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  5882. 'this.c = $mod.b;',
  5883. 'this.i = 0;',
  5884. 'this.d = 0.0;',
  5885. 'this.j = undefined;',
  5886. 'this.DoIt = function (c) {',
  5887. ' var Result = 0;',
  5888. ' return Result;',
  5889. '};',
  5890. 'this.GetIt = function (d) {',
  5891. ' var Result = 0.0;',
  5892. ' return Result;',
  5893. '};',
  5894. 'this.Write = function (v) {',
  5895. '};',
  5896. '']),
  5897. LinesToStr([
  5898. '$mod.c = 10000;',
  5899. '$mod.c = 1000;',
  5900. '$mod.c = Math.floor((1.0 / 3.0) * 10000);',
  5901. '$mod.c = Math.floor((1 / 3) * 10000);',
  5902. '$mod.c = $mod.a;',
  5903. '$mod.d = $mod.c / 10000;',
  5904. '$mod.c = Math.floor($mod.d * 10000);',
  5905. '$mod.c = $mod.c;',
  5906. '$mod.c = $mod.d * 10000;',
  5907. '$mod.d = $mod.c / 10000;',
  5908. '$mod.c = $mod.i * 10000;',
  5909. '$mod.c = $mod.i * 10000;',
  5910. '$mod.i = Math.floor($mod.c / 10000);',
  5911. '$mod.c = $mod.c + $mod.a;',
  5912. '$mod.c = -$mod.c - $mod.a;',
  5913. '$mod.c = ($mod.d * 10000) + $mod.c;',
  5914. '$mod.c = $mod.c + ($mod.d * 10000);',
  5915. '$mod.c = ($mod.d * 10000) - $mod.c;',
  5916. '$mod.c = $mod.c - ($mod.d * 10000);',
  5917. '$mod.c = ($mod.c * $mod.a) / 10000;',
  5918. '$mod.c = ($mod.a * $mod.c) / 10000;',
  5919. '$mod.c = $mod.d * $mod.c;',
  5920. '$mod.c = $mod.c * $mod.d;',
  5921. '$mod.c = Math.floor(($mod.c / $mod.a) * 10000);',
  5922. '$mod.c = Math.floor(($mod.a / $mod.c) * 10000);',
  5923. '$mod.c = Math.floor($mod.d / $mod.c);',
  5924. '$mod.c = Math.floor($mod.c / $mod.d);',
  5925. '$mod.c = Math.floor(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  5926. '$mod.c = Math.floor(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  5927. '$mod.c = Math.floor(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  5928. '$mod.c = Math.floor(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  5929. 'if ($mod.c === $mod.c) ;',
  5930. 'if ($mod.c === $mod.a) ;',
  5931. 'if ($mod.a === $mod.c) ;',
  5932. 'if (($mod.d * 10000) === $mod.c) ;',
  5933. 'if ($mod.c === ($mod.d * 10000)) ;',
  5934. '$mod.c = $mod.DoIt($mod.c);',
  5935. '$mod.c = $mod.DoIt($mod.i * 10000);',
  5936. '$mod.c = $mod.DoIt($mod.d * 10000);',
  5937. '$mod.c = Math.floor($mod.GetIt($mod.c / 10000) * 10000);',
  5938. '$mod.j = $mod.c / 10000;',
  5939. '$mod.Write($mod.c / 10000);',
  5940. '$mod.c = 0;',
  5941. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  5942. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  5943. '']));
  5944. end;
  5945. procedure TTestModule.TestForBoolDo;
  5946. begin
  5947. StartProgram(false);
  5948. Add([
  5949. 'var b: boolean;',
  5950. 'begin',
  5951. ' for b:=false to true do ;',
  5952. ' for b:=b downto false do ;',
  5953. ' for b in boolean do ;',
  5954. '']);
  5955. ConvertProgram;
  5956. CheckSource('TestForBoolDo',
  5957. LinesToStr([ // statements
  5958. 'this.b = false;']),
  5959. LinesToStr([ // this.$main
  5960. 'for (var $l1 = 0; $l1 <= 1; $l1++) $mod.b = $l1 !== 0;',
  5961. 'for (var $l2 = +$mod.b; $l2 >= 0; $l2--) $mod.b = $l2 !== 0;',
  5962. 'for (var $l3 = 0; $l3 <= 1; $l3++) $mod.b = $l3 !== 0;',
  5963. '']));
  5964. end;
  5965. procedure TTestModule.TestForIntDo;
  5966. begin
  5967. StartProgram(false);
  5968. Add([
  5969. 'var i: longint;',
  5970. 'begin',
  5971. ' for i:=3 to 5 do ;',
  5972. ' for i:=i downto 2 do ;',
  5973. ' for i in byte do ;',
  5974. '']);
  5975. ConvertProgram;
  5976. CheckSource('TestForIntDo',
  5977. LinesToStr([ // statements
  5978. 'this.i = 0;']),
  5979. LinesToStr([ // this.$main
  5980. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  5981. 'for (var $l1 = $mod.i; $l1 >= 2; $l1--) $mod.i = $l1;',
  5982. 'for (var $l2 = 0; $l2 <= 255; $l2++) $mod.i = $l2;',
  5983. '']));
  5984. end;
  5985. procedure TTestModule.TestForIntInDo;
  5986. begin
  5987. StartProgram(false);
  5988. Add([
  5989. 'type',
  5990. ' TSetOfInt = set of byte;',
  5991. ' TIntRg = 3..7;',
  5992. ' TSetOfIntRg = set of TIntRg;',
  5993. 'var',
  5994. ' i,i2: longint;',
  5995. ' a1: array of byte;',
  5996. ' a2: array[1..3] of byte;',
  5997. ' soi: TSetOfInt;',
  5998. ' soir: TSetOfIntRg;',
  5999. ' ir: TIntRg;',
  6000. 'begin',
  6001. ' for i in byte do ;',
  6002. ' for i in a1 do ;',
  6003. ' for i in a2 do ;',
  6004. ' for i in [11..13] do ;',
  6005. ' for i in TSetOfInt do ;',
  6006. ' for i in TIntRg do ;',
  6007. ' for i in soi do i2:=i;',
  6008. ' for i in TSetOfIntRg do ;',
  6009. ' for i in soir do ;',
  6010. ' for ir in TIntRg do ;',
  6011. ' for ir in TSetOfIntRg do ;',
  6012. ' for ir in soir do ;',
  6013. '']);
  6014. ConvertProgram;
  6015. CheckSource('TestForIntInDo',
  6016. LinesToStr([ // statements
  6017. 'this.i = 0;',
  6018. 'this.i2 = 0;',
  6019. 'this.a1 = [];',
  6020. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  6021. 'this.soi = {};',
  6022. 'this.soir = {};',
  6023. 'this.ir = 0;',
  6024. '']),
  6025. LinesToStr([ // this.$main
  6026. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  6027. 'for (var $in2 = $mod.a1, $l3 = 0, $end4 = rtl.length($in2) - 1; $l3 <= $end4; $l3++) $mod.i = $in2[$l3];',
  6028. 'for (var $in5 = $mod.a2, $l6 = 0, $end7 = rtl.length($in5) - 1; $l6 <= $end7; $l6++) $mod.i = $in5[$l6];',
  6029. 'for (var $l8 = 11; $l8 <= 13; $l8++) $mod.i = $l8;',
  6030. 'for (var $l9 = 0; $l9 <= 255; $l9++) $mod.i = $l9;',
  6031. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.i = $l10;',
  6032. 'for (var $l11 in $mod.soi) {',
  6033. ' $mod.i = +$l11;',
  6034. ' $mod.i2 = $mod.i;',
  6035. '};',
  6036. 'for (var $l12 = 3; $l12 <= 7; $l12++) $mod.i = $l12;',
  6037. 'for (var $l13 in $mod.soir) $mod.i = +$l13;',
  6038. 'for (var $l14 = 3; $l14 <= 7; $l14++) $mod.ir = $l14;',
  6039. 'for (var $l15 = 3; $l15 <= 7; $l15++) $mod.ir = $l15;',
  6040. 'for (var $l16 in $mod.soir) $mod.ir = +$l16;',
  6041. '']));
  6042. end;
  6043. procedure TTestModule.TestCharConst;
  6044. begin
  6045. StartProgram(false);
  6046. Add([
  6047. 'const',
  6048. ' c: char = ''1'';',
  6049. 'begin',
  6050. ' c:=#0;',
  6051. ' c:=#1;',
  6052. ' c:=#9;',
  6053. ' c:=#10;',
  6054. ' c:=#13;',
  6055. ' c:=#31;',
  6056. ' c:=#32;',
  6057. ' c:=#$A;',
  6058. ' c:=#$0A;',
  6059. ' c:=#$b;',
  6060. ' c:=#$0b;',
  6061. ' c:=^A;',
  6062. ' c:=''"'';',
  6063. ' c:=default(char);',
  6064. ' c:=#$00E4;', // ä
  6065. ' c:=''ä'';',
  6066. ' c:=#$E4;', // ä
  6067. ' c:=#$D800;', // invalid UTF-16
  6068. ' c:=#$DFFF;', // invalid UTF-16
  6069. ' c:=#$FFFF;', // last UCS-2
  6070. ' c:=high(c);', // last UCS-2
  6071. '']);
  6072. ConvertProgram;
  6073. CheckSource('TestCharConst',
  6074. LinesToStr([
  6075. 'this.c="1";'
  6076. ]),
  6077. LinesToStr([
  6078. '$mod.c="\x00";',
  6079. '$mod.c="\x01";',
  6080. '$mod.c="\t";',
  6081. '$mod.c="\n";',
  6082. '$mod.c="\r";',
  6083. '$mod.c="\x1F";',
  6084. '$mod.c=" ";',
  6085. '$mod.c="\n";',
  6086. '$mod.c="\n";',
  6087. '$mod.c="\x0B";',
  6088. '$mod.c="\x0B";',
  6089. '$mod.c="\x01";',
  6090. '$mod.c=''"'';',
  6091. '$mod.c="\x00";',
  6092. '$mod.c = "ä";',
  6093. '$mod.c = "ä";',
  6094. '$mod.c = "ä";',
  6095. '$mod.c="\uD800";',
  6096. '$mod.c="\uDFFF";',
  6097. '$mod.c="\uFFFF";',
  6098. '$mod.c="\uFFFF";',
  6099. '']));
  6100. end;
  6101. procedure TTestModule.TestChar_Compare;
  6102. begin
  6103. StartProgram(false);
  6104. Add('var');
  6105. Add(' c: char;');
  6106. Add(' b: boolean;');
  6107. Add('begin');
  6108. Add(' b:=c=''1'';');
  6109. Add(' b:=''2''=c;');
  6110. Add(' b:=''3''=''4'';');
  6111. Add(' b:=c<>''5'';');
  6112. Add(' b:=''6''<>c;');
  6113. Add(' b:=c>''7'';');
  6114. Add(' b:=''8''>c;');
  6115. Add(' b:=c>=''9'';');
  6116. Add(' b:=''A''>=c;');
  6117. Add(' b:=c<''B'';');
  6118. Add(' b:=''C''<c;');
  6119. Add(' b:=c<=''D'';');
  6120. Add(' b:=''E''<=c;');
  6121. ConvertProgram;
  6122. CheckSource('TestChar_Compare',
  6123. LinesToStr([
  6124. 'this.c="";',
  6125. 'this.b = false;'
  6126. ]),
  6127. LinesToStr([
  6128. '$mod.b = $mod.c === "1";',
  6129. '$mod.b = "2" === $mod.c;',
  6130. '$mod.b = "3" === "4";',
  6131. '$mod.b = $mod.c !== "5";',
  6132. '$mod.b = "6" !== $mod.c;',
  6133. '$mod.b = $mod.c > "7";',
  6134. '$mod.b = "8" > $mod.c;',
  6135. '$mod.b = $mod.c >= "9";',
  6136. '$mod.b = "A" >= $mod.c;',
  6137. '$mod.b = $mod.c < "B";',
  6138. '$mod.b = "C" < $mod.c;',
  6139. '$mod.b = $mod.c <= "D";',
  6140. '$mod.b = "E" <= $mod.c;',
  6141. '']));
  6142. end;
  6143. procedure TTestModule.TestChar_BuiltInProcs;
  6144. begin
  6145. StartProgram(false);
  6146. Add([
  6147. 'var',
  6148. ' c: char;',
  6149. ' i: longint;',
  6150. ' s: string;',
  6151. 'begin',
  6152. ' i:=ord(c);',
  6153. ' i:=ord(s[i]);',
  6154. ' c:=chr(i);',
  6155. ' c:=pred(c);',
  6156. ' c:=succ(c);',
  6157. ' c:=low(c);',
  6158. ' c:=high(c);',
  6159. '']);
  6160. ConvertProgram;
  6161. CheckSource('TestChar_BuiltInProcs',
  6162. LinesToStr([
  6163. 'this.c = "";',
  6164. 'this.i = 0;',
  6165. 'this.s = "";'
  6166. ]),
  6167. LinesToStr([
  6168. '$mod.i = $mod.c.charCodeAt();',
  6169. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  6170. '$mod.c = String.fromCharCode($mod.i);',
  6171. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  6172. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  6173. '$mod.c = "\x00";',
  6174. '$mod.c = "\uFFFF";',
  6175. '']));
  6176. end;
  6177. procedure TTestModule.TestStringConst;
  6178. begin
  6179. StartProgram(false);
  6180. Add([
  6181. '{$H+}',
  6182. 'var',
  6183. ' s: string = ''abc'';',
  6184. 'begin',
  6185. ' s:='''';',
  6186. ' s:=#13#10;',
  6187. ' s:=#9''foo'';',
  6188. ' s:=#$A9;',
  6189. ' s:=''foo''#13''bar'';',
  6190. ' s:=''"'';',
  6191. ' s:=''"''''"'';',
  6192. ' s:=#$20AC;', // euro
  6193. ' s:=#$10437;', // outside BMP
  6194. ' s:=default(string);',
  6195. ' s:=concat(s);',
  6196. ' s:=concat(s,''a'',s)',
  6197. '']);
  6198. ConvertProgram;
  6199. CheckSource('TestStringConst',
  6200. LinesToStr([
  6201. 'this.s="abc";'
  6202. ]),
  6203. LinesToStr([
  6204. '$mod.s="";',
  6205. '$mod.s="\r\n";',
  6206. '$mod.s="\tfoo";',
  6207. '$mod.s="©";',
  6208. '$mod.s="foo\rbar";',
  6209. '$mod.s=''"'';',
  6210. '$mod.s=''"\''"'';',
  6211. '$mod.s="€";',
  6212. '$mod.s="'#$F0#$90#$90#$B7'";',
  6213. '$mod.s="";',
  6214. '$mod.s = $mod.s;',
  6215. '$mod.s = $mod.s.concat("a", $mod.s);',
  6216. '']));
  6217. end;
  6218. procedure TTestModule.TestStringConstSurrogate;
  6219. begin
  6220. StartProgram(false);
  6221. Add([
  6222. 'var',
  6223. ' s: string;',
  6224. 'begin',
  6225. ' s:=''😊'';', // 1F60A
  6226. '']);
  6227. ConvertProgram;
  6228. CheckSource('TestStringConstSurrogate',
  6229. LinesToStr([
  6230. 'this.s="";'
  6231. ]),
  6232. LinesToStr([
  6233. '$mod.s="😊";'
  6234. ]));
  6235. end;
  6236. procedure TTestModule.TestString_Length;
  6237. begin
  6238. StartProgram(false);
  6239. Add('const c = ''foo'';');
  6240. Add('var');
  6241. Add(' s: string;');
  6242. Add(' i: longint;');
  6243. Add('begin');
  6244. Add(' i:=length(s);');
  6245. Add(' i:=length(s+s);');
  6246. Add(' i:=length(''abc'');');
  6247. Add(' i:=length(c);');
  6248. ConvertProgram;
  6249. CheckSource('TestString_Length',
  6250. LinesToStr([
  6251. 'this.c = "foo";',
  6252. 'this.s = "";',
  6253. 'this.i = 0;',
  6254. '']),
  6255. LinesToStr([
  6256. '$mod.i = $mod.s.length;',
  6257. '$mod.i = ($mod.s+$mod.s).length;',
  6258. '$mod.i = "abc".length;',
  6259. '$mod.i = $mod.c.length;',
  6260. '']));
  6261. end;
  6262. procedure TTestModule.TestString_Compare;
  6263. begin
  6264. StartProgram(false);
  6265. Add('var');
  6266. Add(' s, t: string;');
  6267. Add(' b: boolean;');
  6268. Add('begin');
  6269. Add(' b:=s=t;');
  6270. Add(' b:=s<>t;');
  6271. Add(' b:=s>t;');
  6272. Add(' b:=s>=t;');
  6273. Add(' b:=s<t;');
  6274. Add(' b:=s<=t;');
  6275. ConvertProgram;
  6276. CheckSource('TestString_Compare',
  6277. LinesToStr([ // statements
  6278. 'this.s = "";',
  6279. 'this.t = "";',
  6280. 'this.b =false;'
  6281. ]),
  6282. LinesToStr([ // this.$main
  6283. '$mod.b = $mod.s === $mod.t;',
  6284. '$mod.b = $mod.s !== $mod.t;',
  6285. '$mod.b = $mod.s > $mod.t;',
  6286. '$mod.b = $mod.s >= $mod.t;',
  6287. '$mod.b = $mod.s < $mod.t;',
  6288. '$mod.b = $mod.s <= $mod.t;',
  6289. '']));
  6290. end;
  6291. procedure TTestModule.TestString_SetLength;
  6292. begin
  6293. StartProgram(false);
  6294. Add([
  6295. 'procedure DoIt(var s: string);',
  6296. 'begin',
  6297. ' SetLength(s,2);',
  6298. 'end;',
  6299. 'var s: string;',
  6300. 'begin',
  6301. ' SetLength(s,3);',
  6302. '']);
  6303. ConvertProgram;
  6304. CheckSource('TestString_SetLength',
  6305. LinesToStr([ // statements
  6306. 'this.DoIt = function (s) {',
  6307. ' s.set(rtl.strSetLength(s.get(), 2));',
  6308. '};',
  6309. 'this.s = "";',
  6310. '']),
  6311. LinesToStr([ // this.$main
  6312. '$mod.s = rtl.strSetLength($mod.s, 3);'
  6313. ]));
  6314. end;
  6315. procedure TTestModule.TestString_CharAt;
  6316. begin
  6317. StartProgram(false);
  6318. Add([
  6319. 'var',
  6320. ' s: string;',
  6321. ' c: char;',
  6322. ' b: boolean;',
  6323. 'begin',
  6324. ' b:= s[1] = c;',
  6325. ' b:= c = s[1];',
  6326. ' b:= c <> s[1];',
  6327. ' b:= c > s[1];',
  6328. ' b:= c >= s[1];',
  6329. ' b:= c < s[2];',
  6330. ' b:= c <= s[1];',
  6331. ' s[1] := c;',
  6332. ' s[2+3] := c;']);
  6333. ConvertProgram;
  6334. CheckSource('TestString_CharAt',
  6335. LinesToStr([ // statements
  6336. 'this.s = "";',
  6337. 'this.c = "";',
  6338. 'this.b = false;'
  6339. ]),
  6340. LinesToStr([ // this.$main
  6341. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  6342. '$mod.b = $mod.c === $mod.s.charAt(0);',
  6343. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  6344. '$mod.b = $mod.c > $mod.s.charAt(0);',
  6345. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  6346. '$mod.b = $mod.c < $mod.s.charAt(1);',
  6347. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  6348. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  6349. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  6350. '']));
  6351. end;
  6352. procedure TTestModule.TestStringHMinusFail;
  6353. begin
  6354. StartProgram(false);
  6355. Add([
  6356. '{$H-}',
  6357. 'var s: string;',
  6358. 'begin']);
  6359. ConvertProgram;
  6360. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  6361. end;
  6362. procedure TTestModule.TestStr;
  6363. begin
  6364. StartProgram(false);
  6365. Add('var');
  6366. Add(' b: boolean;');
  6367. Add(' i: longint;');
  6368. Add(' d: double;');
  6369. Add(' s: string;');
  6370. Add('begin');
  6371. Add(' str(b,s);');
  6372. Add(' str(i,s);');
  6373. Add(' str(d,s);');
  6374. Add(' str(i:3,s);');
  6375. Add(' str(d:3:2,s);');
  6376. Add(' Str(12.456:12:1,s);');
  6377. Add(' Str(12.456:12,s);');
  6378. Add(' s:=str(b);');
  6379. Add(' s:=str(i);');
  6380. Add(' s:=str(d);');
  6381. Add(' s:=str(i,i);');
  6382. Add(' s:=str(i:3);');
  6383. Add(' s:=str(d:3:2);');
  6384. Add(' s:=str(i:4,i);');
  6385. Add(' s:=str(i,i:5);');
  6386. Add(' s:=str(i:4,i:5);');
  6387. Add(' s:=str(s,s);');
  6388. Add(' s:=str(s,''foo'');');
  6389. ConvertProgram;
  6390. CheckSource('TestStr',
  6391. LinesToStr([ // statements
  6392. 'this.b = false;',
  6393. 'this.i = 0;',
  6394. 'this.d = 0.0;',
  6395. 'this.s = "";',
  6396. '']),
  6397. LinesToStr([ // this.$main
  6398. '$mod.s = ""+$mod.b;',
  6399. '$mod.s = ""+$mod.i;',
  6400. '$mod.s = rtl.floatToStr($mod.d);',
  6401. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  6402. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  6403. '$mod.s = rtl.floatToStr(12.456,12,1);',
  6404. '$mod.s = rtl.floatToStr(12.456,12);',
  6405. '$mod.s = ""+$mod.b;',
  6406. '$mod.s = ""+$mod.i;',
  6407. '$mod.s = rtl.floatToStr($mod.d);',
  6408. '$mod.s = ""+$mod.i+$mod.i;',
  6409. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  6410. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  6411. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  6412. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  6413. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  6414. '$mod.s = $mod.s + $mod.s;',
  6415. '$mod.s = $mod.s + "foo";',
  6416. '']));
  6417. end;
  6418. procedure TTestModule.TestBaseType_AnsiStringFail;
  6419. begin
  6420. StartProgram(false);
  6421. Add('var s: AnsiString');
  6422. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  6423. ConvertProgram;
  6424. end;
  6425. procedure TTestModule.TestBaseType_WideStringFail;
  6426. begin
  6427. StartProgram(false);
  6428. Add('var s: WideString');
  6429. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  6430. ConvertProgram;
  6431. end;
  6432. procedure TTestModule.TestBaseType_ShortStringFail;
  6433. begin
  6434. StartProgram(false);
  6435. Add('var s: ShortString');
  6436. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  6437. ConvertProgram;
  6438. end;
  6439. procedure TTestModule.TestBaseType_RawByteStringFail;
  6440. begin
  6441. StartProgram(false);
  6442. Add('var s: RawByteString');
  6443. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  6444. ConvertProgram;
  6445. end;
  6446. procedure TTestModule.TestTypeShortstring_Fail;
  6447. begin
  6448. StartProgram(false);
  6449. Add('type t = string[12];');
  6450. Add('var s: t;');
  6451. Add('begin');
  6452. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  6453. ConvertProgram;
  6454. end;
  6455. procedure TTestModule.TestCharSet_Custom;
  6456. begin
  6457. StartProgram(false);
  6458. Add([
  6459. 'type',
  6460. ' TCharRg = ''a''..''z'';',
  6461. ' TSetOfCharRg = set of TCharRg;',
  6462. ' TCharRg2 = ''m''..''p'';',
  6463. 'const',
  6464. ' crg: TCharRg = ''b'';',
  6465. 'var',
  6466. ' c: char;',
  6467. ' crg2: TCharRg2;',
  6468. ' s: TSetOfCharRg;',
  6469. 'begin',
  6470. ' c:=crg;',
  6471. ' crg:=c;',
  6472. ' crg2:=crg;',
  6473. ' if c=crg then ;',
  6474. ' if crg=c then ;',
  6475. ' if crg=crg2 then ;',
  6476. ' if c in s then ;',
  6477. ' if crg2 in s then ;',
  6478. ' c:=default(TCharRg);',
  6479. '']);
  6480. ConvertProgram;
  6481. CheckSource('TestCharSet_Custom',
  6482. LinesToStr([ // statements
  6483. 'this.crg = "b";',
  6484. 'this.c = "";',
  6485. 'this.crg2 = "m";',
  6486. 'this.s = {};',
  6487. '']),
  6488. LinesToStr([ // this.$main
  6489. '$mod.c = $mod.crg;',
  6490. '$mod.crg = $mod.c;',
  6491. '$mod.crg2 = $mod.crg;',
  6492. 'if ($mod.c === $mod.crg) ;',
  6493. 'if ($mod.crg === $mod.c) ;',
  6494. 'if ($mod.crg === $mod.crg2) ;',
  6495. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  6496. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  6497. '$mod.c = "a";',
  6498. '']));
  6499. end;
  6500. procedure TTestModule.TestForCharDo;
  6501. begin
  6502. StartProgram(false);
  6503. Add([
  6504. 'var c: char;',
  6505. 'begin',
  6506. ' for c:=''a'' to ''c'' do ;',
  6507. ' for c:=c downto ''a'' do ;',
  6508. '']);
  6509. ConvertProgram;
  6510. CheckSource('TestForCharDo',
  6511. LinesToStr([ // statements
  6512. 'this.c = "";']),
  6513. LinesToStr([ // this.$main
  6514. 'for (var $l1 = 97; $l1 <= 99; $l1++) $mod.c = String.fromCharCode($l1);',
  6515. 'for (var $l2 = $mod.c.charCodeAt(); $l2 >= 97; $l2--) $mod.c = String.fromCharCode($l2);',
  6516. '']));
  6517. end;
  6518. procedure TTestModule.TestForCharInDo;
  6519. begin
  6520. StartProgram(false);
  6521. Add([
  6522. 'type',
  6523. ' TSetOfChar = set of char;',
  6524. ' TCharRg = ''a''..''z'';',
  6525. ' TSetOfCharRg = set of TCharRg;',
  6526. 'const Foo = ''foo'';',
  6527. 'var',
  6528. ' c,c2: char;',
  6529. ' s: string;',
  6530. ' a1: array of char;',
  6531. ' a2: array[1..3] of char;',
  6532. ' soc: TSetOfChar;',
  6533. ' socr: TSetOfCharRg;',
  6534. ' cr: TCharRg;',
  6535. 'begin',
  6536. ' for c in foo do ;',
  6537. ' for c in s do ;',
  6538. ' for c in char do ;',
  6539. ' for c in a1 do ;',
  6540. ' for c in a2 do ;',
  6541. ' for c in [''1''..''3''] do ;',
  6542. ' for c in TSetOfChar do ;',
  6543. ' for c in TCharRg do ;',
  6544. ' for c in soc do c2:=c;',
  6545. ' for c in TSetOfCharRg do ;',
  6546. ' for c in socr do ;',
  6547. ' for cr in TCharRg do ;',
  6548. ' for cr in TSetOfCharRg do ;',
  6549. ' for cr in socr do ;',
  6550. '']);
  6551. ConvertProgram;
  6552. CheckSource('TestForCharInDo',
  6553. LinesToStr([ // statements
  6554. 'this.Foo = "foo";',
  6555. 'this.c = "";',
  6556. 'this.c2 = "";',
  6557. 'this.s = "";',
  6558. 'this.a1 = [];',
  6559. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  6560. 'this.soc = {};',
  6561. 'this.socr = {};',
  6562. 'this.cr = "a";',
  6563. '']),
  6564. LinesToStr([ // this.$main
  6565. 'for (var $in1 = $mod.Foo, $l2 = 0, $end3 = $in1.length - 1; $l2 <= $end3; $l2++) $mod.c = $in1.charAt($l2);',
  6566. 'for (var $in4 = $mod.s, $l5 = 0, $end6 = $in4.length - 1; $l5 <= $end6; $l5++) $mod.c = $in4.charAt($l5);',
  6567. 'for (var $l7 = 0; $l7 <= 65535; $l7++) $mod.c = String.fromCharCode($l7);',
  6568. 'for (var $in8 = $mod.a1, $l9 = 0, $end10 = rtl.length($in8) - 1; $l9 <= $end10; $l9++) $mod.c = $in8[$l9];',
  6569. 'for (var $in11 = $mod.a2, $l12 = 0, $end13 = rtl.length($in11) - 1; $l12 <= $end13; $l12++) $mod.c = $in11[$l12];',
  6570. 'for (var $l14 = 49; $l14 <= 51; $l14++) $mod.c = String.fromCharCode($l14);',
  6571. 'for (var $l15 = 0; $l15 <= 65535; $l15++) $mod.c = String.fromCharCode($l15);',
  6572. 'for (var $l16 = 97; $l16 <= 122; $l16++) $mod.c = String.fromCharCode($l16);',
  6573. 'for (var $l17 in $mod.soc) {',
  6574. ' $mod.c = String.fromCharCode($l17);',
  6575. ' $mod.c2 = $mod.c;',
  6576. '};',
  6577. 'for (var $l18 = 97; $l18 <= 122; $l18++) $mod.c = String.fromCharCode($l18);',
  6578. 'for (var $l19 in $mod.socr) $mod.c = String.fromCharCode($l19);',
  6579. 'for (var $l20 = 97; $l20 <= 122; $l20++) $mod.cr = String.fromCharCode($l20);',
  6580. 'for (var $l21 = 97; $l21 <= 122; $l21++) $mod.cr = String.fromCharCode($l21);',
  6581. 'for (var $l22 in $mod.socr) $mod.cr = String.fromCharCode($l22);',
  6582. '']));
  6583. end;
  6584. procedure TTestModule.TestProcTwoArgs;
  6585. begin
  6586. StartProgram(false);
  6587. Add('procedure Test(a,b: longint);');
  6588. Add('begin');
  6589. Add('end;');
  6590. Add('begin');
  6591. ConvertProgram;
  6592. CheckSource('TestProcTwoArgs',
  6593. LinesToStr([ // statements
  6594. 'this.Test = function (a,b) {',
  6595. '};'
  6596. ]),
  6597. LinesToStr([ // this.$main
  6598. ''
  6599. ]));
  6600. end;
  6601. procedure TTestModule.TestProc_DefaultValue;
  6602. begin
  6603. StartProgram(false);
  6604. Add('procedure p1(i: longint = 1);');
  6605. Add('begin');
  6606. Add('end;');
  6607. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  6608. Add('begin');
  6609. Add('end;');
  6610. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  6611. Add('begin');
  6612. Add('end;');
  6613. Add('begin');
  6614. Add(' p1;');
  6615. Add(' p1();');
  6616. Add(' p1(11);');
  6617. Add(' p2;');
  6618. Add(' p2();');
  6619. Add(' p2(12);');
  6620. Add(' p2(13,''b'');');
  6621. Add(' p3();');
  6622. ConvertProgram;
  6623. CheckSource('TestProc_DefaultValue',
  6624. LinesToStr([ // statements
  6625. 'this.p1 = function (i) {',
  6626. '};',
  6627. 'this.p2 = function (i,c) {',
  6628. '};',
  6629. 'this.p3 = function (d,b,s) {',
  6630. '};'
  6631. ]),
  6632. LinesToStr([ // this.$main
  6633. ' $mod.p1(1);',
  6634. ' $mod.p1(1);',
  6635. ' $mod.p1(11);',
  6636. ' $mod.p2(1,"a");',
  6637. ' $mod.p2(1,"a");',
  6638. ' $mod.p2(12,"a");',
  6639. ' $mod.p2(13,"b");',
  6640. ' $mod.p3(1.0,false,"abc");'
  6641. ]));
  6642. end;
  6643. procedure TTestModule.TestFunctionInt;
  6644. begin
  6645. StartProgram(false);
  6646. Add('function MyTest(Bar: longint): longint;');
  6647. Add('begin');
  6648. Add(' Result:=2*bar');
  6649. Add('end;');
  6650. Add('begin');
  6651. ConvertProgram;
  6652. CheckSource('TestFunctionInt',
  6653. LinesToStr([ // statements
  6654. 'this.MyTest = function (Bar) {',
  6655. ' var Result = 0;',
  6656. ' Result = 2*Bar;',
  6657. ' return Result;',
  6658. '};'
  6659. ]),
  6660. LinesToStr([ // this.$main
  6661. ''
  6662. ]));
  6663. end;
  6664. procedure TTestModule.TestFunctionString;
  6665. begin
  6666. StartProgram(false);
  6667. Add('function Test(Bar: string): string;');
  6668. Add('begin');
  6669. Add(' Result:=bar+BAR');
  6670. Add('end;');
  6671. Add('begin');
  6672. ConvertProgram;
  6673. CheckSource('TestFunctionString',
  6674. LinesToStr([ // statements
  6675. 'this.Test = function (Bar) {',
  6676. ' var Result = "";',
  6677. ' Result = Bar+Bar;',
  6678. ' return Result;',
  6679. '};'
  6680. ]),
  6681. LinesToStr([ // this.$main
  6682. ''
  6683. ]));
  6684. end;
  6685. procedure TTestModule.TestIfThen;
  6686. begin
  6687. StartProgram(false);
  6688. Add([
  6689. 'var b: boolean;',
  6690. 'begin',
  6691. ' if b then ;',
  6692. ' if b then else ;']);
  6693. ConvertProgram;
  6694. CheckSource('TestIfThen',
  6695. LinesToStr([ // statements
  6696. 'this.b = false;',
  6697. '']),
  6698. LinesToStr([ // this.$main
  6699. 'if ($mod.b) ;',
  6700. 'if ($mod.b) ;',
  6701. '']));
  6702. end;
  6703. procedure TTestModule.TestForLoop;
  6704. begin
  6705. StartProgram(false);
  6706. Add('var');
  6707. Add(' vI, vJ, vN: longint;');
  6708. Add('begin');
  6709. Add(' VJ:=0;');
  6710. Add(' VN:=3;');
  6711. Add(' for VI:=1 to VN do');
  6712. Add(' begin');
  6713. Add(' VJ:=VJ+VI;');
  6714. Add(' end;');
  6715. ConvertProgram;
  6716. CheckSource('TestForLoop',
  6717. LinesToStr([ // statements
  6718. 'this.vI = 0;',
  6719. 'this.vJ = 0;',
  6720. 'this.vN = 0;'
  6721. ]),
  6722. LinesToStr([ // this.$main
  6723. ' $mod.vJ = 0;',
  6724. ' $mod.vN = 3;',
  6725. ' for (var $l1 = 1, $end2 = $mod.vN; $l1 <= $end2; $l1++) {',
  6726. ' $mod.vI = $l1;',
  6727. ' $mod.vJ = $mod.vJ + $mod.vI;',
  6728. ' };',
  6729. '']));
  6730. end;
  6731. procedure TTestModule.TestForLoopInsideFunction;
  6732. begin
  6733. StartProgram(false);
  6734. Add('function SumNumbers(Count: longint): longint;');
  6735. Add('var');
  6736. Add(' vI, vJ: longint;');
  6737. Add('begin');
  6738. Add(' vj:=0;');
  6739. Add(' for vi:=1 to count do');
  6740. Add(' begin');
  6741. Add(' vj:=vj+vi;');
  6742. Add(' end;');
  6743. Add('end;');
  6744. Add('begin');
  6745. Add(' sumnumbers(3);');
  6746. ConvertProgram;
  6747. CheckSource('TestForLoopInsideFunction',
  6748. LinesToStr([ // statements
  6749. 'this.SumNumbers = function (Count) {',
  6750. ' var Result = 0;',
  6751. ' var vI = 0;',
  6752. ' var vJ = 0;',
  6753. ' vJ = 0;',
  6754. ' for (var $l1 = 1, $end2 = Count; $l1 <= $end2; $l1++) {',
  6755. ' vI = $l1;',
  6756. ' vJ = vJ + vI;',
  6757. ' };',
  6758. ' return Result;',
  6759. '};'
  6760. ]),
  6761. LinesToStr([ // $mod.$main
  6762. ' $mod.SumNumbers(3);'
  6763. ]));
  6764. end;
  6765. procedure TTestModule.TestForLoop_ReadVarAfter;
  6766. begin
  6767. StartProgram(false);
  6768. Add('var');
  6769. Add(' vI: longint;');
  6770. Add('begin');
  6771. Add(' for vi:=1 to 2 do ;');
  6772. Add(' if vi=3 then ;');
  6773. ConvertProgram;
  6774. CheckSource('TestForLoop',
  6775. LinesToStr([ // statements
  6776. 'this.vI = 0;'
  6777. ]),
  6778. LinesToStr([ // this.$main
  6779. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  6780. ' if ($mod.vI===3) ;'
  6781. ]));
  6782. end;
  6783. procedure TTestModule.TestForLoop_Nested;
  6784. begin
  6785. StartProgram(false);
  6786. Add('function SumNumbers(Count: longint): longint;');
  6787. Add('var');
  6788. Add(' vI, vJ, vK: longint;');
  6789. Add('begin');
  6790. Add(' VK:=0;');
  6791. Add(' for VI:=1 to count do');
  6792. Add(' begin');
  6793. Add(' for vj:=1 to vi do');
  6794. Add(' begin');
  6795. Add(' vk:=VK+VI;');
  6796. Add(' end;');
  6797. Add(' end;');
  6798. Add('end;');
  6799. Add('begin');
  6800. Add(' sumnumbers(3);');
  6801. ConvertProgram;
  6802. CheckSource('TestForLoopInFunction',
  6803. LinesToStr([ // statements
  6804. 'this.SumNumbers = function (Count) {',
  6805. ' var Result = 0;',
  6806. ' var vI = 0;',
  6807. ' var vJ = 0;',
  6808. ' var vK = 0;',
  6809. ' vK = 0;',
  6810. ' for (var $l1 = 1, $end2 = Count; $l1 <= $end2; $l1++) {',
  6811. ' vI = $l1;',
  6812. ' for (var $l3 = 1, $end4 = vI; $l3 <= $end4; $l3++) {',
  6813. ' vJ = $l3;',
  6814. ' vK = vK + vI;',
  6815. ' };',
  6816. ' };',
  6817. ' return Result;',
  6818. '};'
  6819. ]),
  6820. LinesToStr([ // $mod.$main
  6821. ' $mod.SumNumbers(3);'
  6822. ]));
  6823. end;
  6824. procedure TTestModule.TestRepeatUntil;
  6825. begin
  6826. StartProgram(false);
  6827. Add('var');
  6828. Add(' vI, vJ, vN: longint;');
  6829. Add('begin');
  6830. Add(' vn:=3;');
  6831. Add(' vj:=0;');
  6832. Add(' VI:=0;');
  6833. Add(' repeat');
  6834. Add(' VI:=vi+1;');
  6835. Add(' vj:=VJ+vI;');
  6836. Add(' until vi>=vn');
  6837. ConvertProgram;
  6838. CheckSource('TestRepeatUntil',
  6839. LinesToStr([ // statements
  6840. 'this.vI = 0;',
  6841. 'this.vJ = 0;',
  6842. 'this.vN = 0;'
  6843. ]),
  6844. LinesToStr([ // $mod.$main
  6845. ' $mod.vN = 3;',
  6846. ' $mod.vJ = 0;',
  6847. ' $mod.vI = 0;',
  6848. ' do{',
  6849. ' $mod.vI = $mod.vI + 1;',
  6850. ' $mod.vJ = $mod.vJ + $mod.vI;',
  6851. ' }while(!($mod.vI>=$mod.vN));'
  6852. ]));
  6853. end;
  6854. procedure TTestModule.TestAsmBlock;
  6855. begin
  6856. StartProgram(false);
  6857. Add([
  6858. 'var',
  6859. ' vI: longint;',
  6860. 'begin',
  6861. ' vi:=1;',
  6862. ' asm',
  6863. ' if (vI===1) {',
  6864. ' vI=2;',
  6865. //' console.log(''end;'');', ToDo
  6866. ' }',
  6867. ' if (vI===2){ vI=3; }',
  6868. ' end;',
  6869. ' VI:=4;']);
  6870. ConvertProgram;
  6871. CheckSource('TestAsmBlock',
  6872. LinesToStr([ // statements
  6873. 'this.vI = 0;'
  6874. ]),
  6875. LinesToStr([ // $mod.$main
  6876. '$mod.vI = 1;',
  6877. 'if (vI===1) {',
  6878. ' vI=2;',
  6879. '}',
  6880. 'if (vI===2){ vI=3; }',
  6881. ';',
  6882. '$mod.vI = 4;'
  6883. ]));
  6884. end;
  6885. procedure TTestModule.TestAsmPas_Impl;
  6886. begin
  6887. StartUnit(false);
  6888. Add('interface');
  6889. Add('const cIntf: longint = 1;');
  6890. Add('var vIntf: longint;');
  6891. Add('implementation');
  6892. Add('const cImpl: longint = 2;');
  6893. Add('var vImpl: longint;');
  6894. Add('procedure DoIt;');
  6895. Add('const cLoc: longint = 3;');
  6896. Add('var vLoc: longint;');
  6897. Add('begin;');
  6898. Add(' asm');
  6899. //Add(' pas(vIntf)=pas(cIntf);');
  6900. //Add(' pas(vImpl)=pas(cImpl);');
  6901. //Add(' pas(vLoc)=pas(cLoc);');
  6902. Add(' end;');
  6903. Add('end;');
  6904. ConvertUnit;
  6905. CheckSource('TestAsmPas_Impl',
  6906. LinesToStr([
  6907. 'var $impl = $mod.$impl;',
  6908. 'this.cIntf = 1;',
  6909. 'this.vIntf = 0;',
  6910. '']),
  6911. '', // this.$init
  6912. LinesToStr([ // implementation
  6913. '$impl.cImpl = 2;',
  6914. '$impl.vImpl = 0;',
  6915. 'var cLoc = 3;',
  6916. '$impl.DoIt = function () {',
  6917. ' var vLoc = 0;',
  6918. '};',
  6919. '']) );
  6920. end;
  6921. procedure TTestModule.TestTryFinally;
  6922. begin
  6923. StartProgram(false);
  6924. Add('var i: longint;');
  6925. Add('begin');
  6926. Add(' try');
  6927. Add(' i:=0; i:=2 div i;');
  6928. Add(' finally');
  6929. Add(' i:=3');
  6930. Add(' end;');
  6931. ConvertProgram;
  6932. CheckSource('TestTryFinally',
  6933. LinesToStr([ // statements
  6934. 'this.i = 0;'
  6935. ]),
  6936. LinesToStr([ // $mod.$main
  6937. 'try {',
  6938. ' $mod.i = 0;',
  6939. ' $mod.i = Math.floor(2 / $mod.i);',
  6940. '} finally {',
  6941. ' $mod.i = 3;',
  6942. '};'
  6943. ]));
  6944. end;
  6945. procedure TTestModule.TestTryExcept;
  6946. begin
  6947. StartProgram(false);
  6948. Add('type');
  6949. Add(' TObject = class end;');
  6950. Add(' Exception = class Msg: string; end;');
  6951. Add(' EInvalidCast = class(Exception) end;');
  6952. Add('var vI: longint;');
  6953. Add('begin');
  6954. Add(' try');
  6955. Add(' vi:=1;');
  6956. Add(' except');
  6957. Add(' vi:=2');
  6958. Add(' end;');
  6959. Add(' try');
  6960. Add(' vi:=3;');
  6961. Add(' except');
  6962. Add(' raise;');
  6963. Add(' end;');
  6964. Add(' try');
  6965. Add(' VI:=4;');
  6966. Add(' except');
  6967. Add(' on einvalidcast do');
  6968. Add(' raise;');
  6969. Add(' on E: exception do');
  6970. Add(' if e.msg='''' then');
  6971. Add(' raise e;');
  6972. Add(' else');
  6973. Add(' vi:=5');
  6974. Add(' end;');
  6975. Add(' try');
  6976. Add(' VI:=6;');
  6977. Add(' except');
  6978. Add(' on einvalidcast do ;');
  6979. Add(' end;');
  6980. ConvertProgram;
  6981. CheckSource('TestTryExcept',
  6982. LinesToStr([ // statements
  6983. 'rtl.createClass($mod, "TObject", null, function () {',
  6984. ' this.$init = function () {',
  6985. ' };',
  6986. ' this.$final = function () {',
  6987. ' };',
  6988. '});',
  6989. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  6990. ' this.$init = function () {',
  6991. ' $mod.TObject.$init.call(this);',
  6992. ' this.Msg = "";',
  6993. ' };',
  6994. '});',
  6995. 'rtl.createClass($mod, "EInvalidCast", $mod.Exception, function () {',
  6996. '});',
  6997. 'this.vI = 0;'
  6998. ]),
  6999. LinesToStr([ // $mod.$main
  7000. 'try {',
  7001. ' $mod.vI = 1;',
  7002. '} catch ($e) {',
  7003. ' $mod.vI = 2;',
  7004. '};',
  7005. 'try {',
  7006. ' $mod.vI = 3;',
  7007. '} catch ($e) {',
  7008. ' throw $e;',
  7009. '};',
  7010. 'try {',
  7011. ' $mod.vI = 4;',
  7012. '} catch ($e) {',
  7013. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  7014. ' throw $e',
  7015. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  7016. ' var E = $e;',
  7017. ' if (E.Msg === "") throw E;',
  7018. ' } else {',
  7019. ' $mod.vI = 5;',
  7020. ' }',
  7021. '};',
  7022. 'try {',
  7023. ' $mod.vI = 6;',
  7024. '} catch ($e) {',
  7025. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  7026. ' } else throw $e',
  7027. '};',
  7028. '']));
  7029. end;
  7030. procedure TTestModule.TestTryExcept_ReservedWords;
  7031. begin
  7032. StartProgram(false);
  7033. Add([
  7034. 'type',
  7035. ' TObject = class end;',
  7036. ' Exception = class',
  7037. ' Symbol: string;',
  7038. ' end;',
  7039. 'var &try: longint;',
  7040. 'begin',
  7041. ' try',
  7042. ' &try:=4;',
  7043. ' except',
  7044. ' on Error: exception do',
  7045. ' if errOR.symBol='''' then',
  7046. ' raise ERRor;',
  7047. ' end;',
  7048. '']);
  7049. ConvertProgram;
  7050. CheckSource('TestTryExcept_ReservedWords',
  7051. LinesToStr([ // statements
  7052. 'rtl.createClass($mod, "TObject", null, function () {',
  7053. ' this.$init = function () {',
  7054. ' };',
  7055. ' this.$final = function () {',
  7056. ' };',
  7057. '});',
  7058. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  7059. ' this.$init = function () {',
  7060. ' $mod.TObject.$init.call(this);',
  7061. ' this.Symbol = "";',
  7062. ' };',
  7063. '});',
  7064. 'this.Try = 0;',
  7065. '']),
  7066. LinesToStr([ // $mod.$main
  7067. 'try {',
  7068. ' $mod.Try = 4;',
  7069. '} catch ($e) {',
  7070. ' if ($mod.Exception.isPrototypeOf($e)) {',
  7071. ' var error = $e;',
  7072. ' if (error.Symbol === "") throw error;',
  7073. ' } else throw $e',
  7074. '};',
  7075. '']));
  7076. end;
  7077. procedure TTestModule.TestIfThenRaiseElse;
  7078. begin
  7079. StartProgram(false);
  7080. Add([
  7081. 'type',
  7082. ' TObject = class',
  7083. ' constructor Create;',
  7084. ' end;',
  7085. 'constructor TObject.Create;',
  7086. 'begin',
  7087. 'end;',
  7088. 'var b: boolean;',
  7089. 'begin',
  7090. ' if b then',
  7091. ' raise TObject.Create',
  7092. ' else',
  7093. ' b:=false;',
  7094. '']);
  7095. ConvertProgram;
  7096. CheckSource('TestIfThenRaiseElse',
  7097. LinesToStr([ // statements
  7098. 'rtl.createClass($mod, "TObject", null, function () {',
  7099. ' this.$init = function () {',
  7100. ' };',
  7101. ' this.$final = function () {',
  7102. ' };',
  7103. ' this.Create = function () {',
  7104. ' return this;',
  7105. ' };',
  7106. '});',
  7107. 'this.b = false;',
  7108. '']),
  7109. LinesToStr([ // $mod.$main
  7110. 'if ($mod.b) {',
  7111. ' throw $mod.TObject.$create("Create")}',
  7112. ' else $mod.b = false;',
  7113. '']));
  7114. end;
  7115. procedure TTestModule.TestCaseOf;
  7116. begin
  7117. StartProgram(false);
  7118. Add([
  7119. 'const e: longint; external name ''$e'';',
  7120. 'var vI: longint;',
  7121. 'begin',
  7122. ' case vi of',
  7123. ' 1: ;',
  7124. ' 2: vi:=3;',
  7125. ' e: ;',
  7126. ' else',
  7127. ' VI:=4',
  7128. ' end;']);
  7129. ConvertProgram;
  7130. CheckSource('TestCaseOf',
  7131. LinesToStr([ // statements
  7132. 'this.vI = 0;'
  7133. ]),
  7134. LinesToStr([ // $mod.$main
  7135. 'var $tmp1 = $mod.vI;',
  7136. 'if ($tmp1 === 1) {}',
  7137. 'else if ($tmp1 === 2) {',
  7138. ' $mod.vI = 3}',
  7139. ' else if ($tmp1 === $e) {}',
  7140. 'else {',
  7141. ' $mod.vI = 4;',
  7142. '};'
  7143. ]));
  7144. end;
  7145. procedure TTestModule.TestCaseOf_UseSwitch;
  7146. begin
  7147. StartProgram(false);
  7148. Converter.UseSwitchStatement:=true;
  7149. Add('var Vi: longint;');
  7150. Add('begin');
  7151. Add(' case vi of');
  7152. Add(' 1: ;');
  7153. Add(' 2: VI:=3;');
  7154. Add(' else');
  7155. Add(' vi:=4');
  7156. Add(' end;');
  7157. ConvertProgram;
  7158. CheckSource('TestCaseOf_UseSwitch',
  7159. LinesToStr([ // statements
  7160. 'this.Vi = 0;'
  7161. ]),
  7162. LinesToStr([ // $mod.$main
  7163. 'switch ($mod.Vi) {',
  7164. 'case 1:',
  7165. ' break;',
  7166. 'case 2:',
  7167. ' $mod.Vi = 3;',
  7168. ' break;',
  7169. 'default:',
  7170. ' $mod.Vi = 4;',
  7171. '};'
  7172. ]));
  7173. end;
  7174. procedure TTestModule.TestCaseOfNoElse;
  7175. begin
  7176. StartProgram(false);
  7177. Add('var Vi: longint;');
  7178. Add('begin');
  7179. Add(' case vi of');
  7180. Add(' 1: begin vi:=2; VI:=3; end;');
  7181. Add(' end;');
  7182. ConvertProgram;
  7183. CheckSource('TestCaseOfNoElse',
  7184. LinesToStr([ // statements
  7185. 'this.Vi = 0;'
  7186. ]),
  7187. LinesToStr([ // $mod.$main
  7188. 'var $tmp1 = $mod.Vi;',
  7189. 'if ($tmp1 === 1) {',
  7190. ' $mod.Vi = 2;',
  7191. ' $mod.Vi = 3;',
  7192. '};'
  7193. ]));
  7194. end;
  7195. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  7196. begin
  7197. StartProgram(false);
  7198. Converter.UseSwitchStatement:=true;
  7199. Add('var vI: longint;');
  7200. Add('begin');
  7201. Add(' case vi of');
  7202. Add(' 1: begin VI:=2; vi:=3; end;');
  7203. Add(' end;');
  7204. ConvertProgram;
  7205. CheckSource('TestCaseOfNoElse_UseSwitch',
  7206. LinesToStr([ // statements
  7207. 'this.vI = 0;'
  7208. ]),
  7209. LinesToStr([ // $mod.$main
  7210. 'switch ($mod.vI) {',
  7211. 'case 1:',
  7212. ' $mod.vI = 2;',
  7213. ' $mod.vI = 3;',
  7214. ' break;',
  7215. '};'
  7216. ]));
  7217. end;
  7218. procedure TTestModule.TestCaseOfRange;
  7219. begin
  7220. StartProgram(false);
  7221. Add('var vI: longint;');
  7222. Add('begin');
  7223. Add(' case vi of');
  7224. Add(' 1..3: vi:=14;');
  7225. Add(' 4,5: vi:=16;');
  7226. Add(' 6..7,9..10: ;');
  7227. Add(' else ;');
  7228. Add(' end;');
  7229. ConvertProgram;
  7230. CheckSource('TestCaseOfRange',
  7231. LinesToStr([ // statements
  7232. 'this.vI = 0;'
  7233. ]),
  7234. LinesToStr([ // $mod.$main
  7235. 'var $tmp1 = $mod.vI;',
  7236. 'if (($tmp1 >= 1) && ($tmp1 <= 3)){',
  7237. ' $mod.vI = 14',
  7238. '} else if (($tmp1 === 4) || ($tmp1 === 5)){',
  7239. ' $mod.vI = 16',
  7240. '} else if ((($tmp1 >= 6) && ($tmp1 <= 7)) || (($tmp1 >= 9) && ($tmp1 <= 10))) ;'
  7241. ]));
  7242. end;
  7243. procedure TTestModule.TestCaseOfString;
  7244. begin
  7245. StartProgram(false);
  7246. Add([
  7247. 'var s,h: string;',
  7248. 'begin',
  7249. ' case s of',
  7250. ' ''foo'': s:=h;',
  7251. ' ''a''..''z'': h:=s;',
  7252. ' end;',
  7253. '']);
  7254. ConvertProgram;
  7255. CheckSource('TestCaseOfString',
  7256. LinesToStr([ // statements
  7257. 'this.s = "";',
  7258. 'this.h = "";',
  7259. '']),
  7260. LinesToStr([ // $mod.$main
  7261. 'var $tmp1 = $mod.s;',
  7262. 'if ($tmp1 === "foo") {',
  7263. ' $mod.s = $mod.h}',
  7264. ' else if (($tmp1.length === 1) && ($tmp1 >= "a") && ($tmp1 <= "z")) $mod.h = $mod.s;',
  7265. '']));
  7266. end;
  7267. procedure TTestModule.TestCaseOfExternalClassConst;
  7268. begin
  7269. StartProgram(false);
  7270. Add([
  7271. '{$modeswitch externalclass}',
  7272. 'type',
  7273. ' TBird = class external name ''Bird''',
  7274. ' const e: longint;',
  7275. ' end;',
  7276. 'var vI: longint;',
  7277. 'begin',
  7278. ' case vi of',
  7279. ' 1: vi:=3;',
  7280. ' TBird.e: ;',
  7281. ' end;']);
  7282. ConvertProgram;
  7283. CheckSource('TestCaseOfExternalClassConst',
  7284. LinesToStr([ // statements
  7285. 'this.vI = 0;'
  7286. ]),
  7287. LinesToStr([ // $mod.$main
  7288. 'var $tmp1 = $mod.vI;',
  7289. 'if ($tmp1 === 1) {',
  7290. ' $mod.vI = 3}',
  7291. ' else if ($tmp1 === Bird.e) ;'
  7292. ]));
  7293. end;
  7294. procedure TTestModule.TestDebugger;
  7295. begin
  7296. StartProgram(false);
  7297. Add([
  7298. 'procedure DoIt;',
  7299. 'begin',
  7300. ' deBugger;',
  7301. ' DeBugger();',
  7302. 'end;',
  7303. 'begin',
  7304. ' Debugger;']);
  7305. ConvertProgram;
  7306. CheckSource('TestDebugger',
  7307. LinesToStr([ // statements
  7308. 'this.DoIt = function () {',
  7309. ' debugger;',
  7310. ' debugger;',
  7311. '};',
  7312. '']),
  7313. LinesToStr([ // $mod.$main
  7314. 'debugger;',
  7315. '']));
  7316. end;
  7317. procedure TTestModule.TestArray_Dynamic;
  7318. begin
  7319. StartProgram(false);
  7320. Add(['type',
  7321. ' TArrayInt = array of longint;',
  7322. 'var',
  7323. ' Arr: TArrayInt;',
  7324. ' i: longint;',
  7325. ' b: boolean;',
  7326. 'begin',
  7327. ' SetLength(arr,3);',
  7328. ' arr[0]:=4;',
  7329. ' arr[1]:=length(arr)+arr[0];',
  7330. ' arr[i]:=5;',
  7331. ' arr[arr[i]]:=arr[6];',
  7332. ' i:=low(arr);',
  7333. ' i:=high(arr);',
  7334. ' b:=Assigned(arr);',
  7335. ' Arr:=default(TArrayInt);']);
  7336. ConvertProgram;
  7337. CheckSource('TestArray_Dynamic',
  7338. LinesToStr([ // statements
  7339. 'this.Arr = [];',
  7340. 'this.i = 0;',
  7341. 'this.b = false;'
  7342. ]),
  7343. LinesToStr([ // $mod.$main
  7344. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  7345. '$mod.Arr[0] = 4;',
  7346. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  7347. '$mod.Arr[$mod.i] = 5;',
  7348. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  7349. '$mod.i = 0;',
  7350. '$mod.i = rtl.length($mod.Arr) - 1;',
  7351. '$mod.b = rtl.length($mod.Arr) > 0;',
  7352. '$mod.Arr = [];',
  7353. '']));
  7354. end;
  7355. procedure TTestModule.TestArray_Dynamic_Nil;
  7356. begin
  7357. StartProgram(false);
  7358. Add('type');
  7359. Add(' TArrayInt = array of longint;');
  7360. Add('var');
  7361. Add(' Arr: TArrayInt;');
  7362. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  7363. Add('begin');
  7364. Add(' arr:=nil;');
  7365. Add(' if arr=nil then;');
  7366. Add(' if nil=arr then;');
  7367. Add(' if arr<>nil then;');
  7368. Add(' if nil<>arr then;');
  7369. Add(' DoIt(nil,nil);');
  7370. ConvertProgram;
  7371. CheckSource('TestArray_Dynamic',
  7372. LinesToStr([ // statements
  7373. 'this.Arr = [];',
  7374. 'this.DoIt = function(i,j){',
  7375. '};'
  7376. ]),
  7377. LinesToStr([ // $mod.$main
  7378. '$mod.Arr = [];',
  7379. 'if (rtl.length($mod.Arr) === 0) ;',
  7380. 'if (rtl.length($mod.Arr) === 0) ;',
  7381. 'if (rtl.length($mod.Arr) > 0) ;',
  7382. 'if (rtl.length($mod.Arr) > 0) ;',
  7383. '$mod.DoIt([],[]);',
  7384. '']));
  7385. end;
  7386. procedure TTestModule.TestArray_DynMultiDimensional;
  7387. begin
  7388. StartProgram(false);
  7389. Add('type');
  7390. Add(' TArrayInt = array of longint;');
  7391. Add(' TArrayArrayInt = array of TArrayInt;');
  7392. Add('var');
  7393. Add(' Arr: TArrayInt;');
  7394. Add(' Arr2: TArrayArrayInt;');
  7395. Add(' i: longint;');
  7396. Add('begin');
  7397. Add(' arr2:=nil;');
  7398. Add(' if arr2=nil then;');
  7399. Add(' if nil=arr2 then;');
  7400. Add(' i:=low(arr2);');
  7401. Add(' i:=low(arr2[1]);');
  7402. Add(' i:=high(arr2);');
  7403. Add(' i:=high(arr2[2]);');
  7404. Add(' arr2[3]:=arr;');
  7405. Add(' arr2[4][5]:=i;');
  7406. Add(' i:=arr2[6][7];');
  7407. Add(' arr2[8,9]:=i;');
  7408. Add(' i:=arr2[10,11];');
  7409. Add(' SetLength(arr2,14);');
  7410. Add(' SetLength(arr2[15],16);');
  7411. ConvertProgram;
  7412. CheckSource('TestArray_Dynamic',
  7413. LinesToStr([ // statements
  7414. 'this.Arr = [];',
  7415. 'this.Arr2 = [];',
  7416. 'this.i = 0;'
  7417. ]),
  7418. LinesToStr([ // $mod.$main
  7419. '$mod.Arr2 = [];',
  7420. 'if (rtl.length($mod.Arr2) === 0) ;',
  7421. 'if (rtl.length($mod.Arr2) === 0) ;',
  7422. '$mod.i = 0;',
  7423. '$mod.i = 0;',
  7424. '$mod.i = rtl.length($mod.Arr2) - 1;',
  7425. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  7426. '$mod.Arr2[3] = $mod.Arr;',
  7427. '$mod.Arr2[4][5] = $mod.i;',
  7428. '$mod.i = $mod.Arr2[6][7];',
  7429. '$mod.Arr2[8][9] = $mod.i;',
  7430. '$mod.i = $mod.Arr2[10][11];',
  7431. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  7432. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  7433. '']));
  7434. end;
  7435. procedure TTestModule.TestArray_StaticInt;
  7436. begin
  7437. StartProgram(false);
  7438. Add('type');
  7439. Add(' TArrayInt = array[2..4] of longint;');
  7440. Add('var');
  7441. Add(' Arr: TArrayInt;');
  7442. Add(' Arr2: TArrayInt = (5,6,7);');
  7443. Add(' i: longint;');
  7444. Add(' b: boolean;');
  7445. Add('begin');
  7446. Add(' arr[2]:=4;');
  7447. Add(' arr[3]:=arr[2]+arr[3];');
  7448. Add(' arr[i]:=5;');
  7449. Add(' arr[arr[i]]:=arr[high(arr)];');
  7450. Add(' i:=low(arr);');
  7451. Add(' i:=high(arr);');
  7452. Add(' b:=arr[2]=arr[3];');
  7453. Add(' arr:=default(TArrayInt);');
  7454. ConvertProgram;
  7455. CheckSource('TestArray_StaticInt',
  7456. LinesToStr([ // statements
  7457. 'this.Arr = rtl.arraySetLength(null,0,3);',
  7458. 'this.Arr2 = [5, 6, 7];',
  7459. 'this.i = 0;',
  7460. 'this.b = false;'
  7461. ]),
  7462. LinesToStr([ // $mod.$main
  7463. '$mod.Arr[0] = 4;',
  7464. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  7465. '$mod.Arr[$mod.i-2] = 5;',
  7466. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  7467. '$mod.i = 2;',
  7468. '$mod.i = 4;',
  7469. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  7470. '$mod.Arr = rtl.arraySetLength(null,0,3).slice(0);',
  7471. '']));
  7472. end;
  7473. procedure TTestModule.TestArray_StaticBool;
  7474. begin
  7475. StartProgram(false);
  7476. Add('type');
  7477. Add(' TBools = array[boolean] of boolean;');
  7478. Add(' TBool2 = array[true..true] of boolean;');
  7479. Add('var');
  7480. Add(' Arr: TBools;');
  7481. Add(' Arr2: TBool2;');
  7482. Add(' Arr3: TBools = (true,false);');
  7483. Add(' b: boolean;');
  7484. Add('begin');
  7485. Add(' b:=low(arr);');
  7486. Add(' b:=high(arr);');
  7487. Add(' arr[true]:=false;');
  7488. Add(' arr[false]:=arr[b] or arr[true];');
  7489. Add(' arr[b]:=true;');
  7490. Add(' arr[arr[b]]:=arr[high(arr)];');
  7491. Add(' b:=arr[false]=arr[true];');
  7492. Add(' b:=low(arr2);');
  7493. Add(' b:=high(arr2);');
  7494. Add(' arr2[true]:=true;');
  7495. Add(' arr2[true]:=arr2[true] and arr2[b];');
  7496. Add(' arr2[b]:=false;');
  7497. ConvertProgram;
  7498. CheckSource('TestArray_StaticBool',
  7499. LinesToStr([ // statements
  7500. 'this.Arr = rtl.arraySetLength(null,false,2);',
  7501. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  7502. 'this.Arr3 = [true, false];',
  7503. 'this.b = false;'
  7504. ]),
  7505. LinesToStr([ // $mod.$main
  7506. '$mod.b = false;',
  7507. '$mod.b = true;',
  7508. '$mod.Arr[1] = false;',
  7509. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  7510. '$mod.Arr[+$mod.b] = true;',
  7511. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  7512. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  7513. '$mod.b = true;',
  7514. '$mod.b = true;',
  7515. '$mod.Arr2[0] = true;',
  7516. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  7517. '$mod.Arr2[1-$mod.b] = false;',
  7518. '']));
  7519. end;
  7520. procedure TTestModule.TestArray_StaticChar;
  7521. begin
  7522. StartProgram(false);
  7523. Add([
  7524. 'type',
  7525. ' TChars = array[char] of char;',
  7526. ' TChars2 = array[''a''..''z''] of char;',
  7527. 'var',
  7528. ' Arr: TChars;',
  7529. ' Arr2: TChars2;',
  7530. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  7531. ' Arr4: array[11..13] of char = ''pas'';',
  7532. ' Arr5: array[21..22] of char = ''äö'';',
  7533. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  7534. ' c: char;',
  7535. ' b: boolean;',
  7536. 'begin',
  7537. ' c:=low(arr);',
  7538. ' c:=high(arr);',
  7539. ' arr[''B'']:=''a'';',
  7540. ' arr[''D'']:=arr[c];',
  7541. ' arr[c]:=arr[''d''];',
  7542. ' arr[arr[c]]:=arr[high(arr)];',
  7543. ' b:=arr[low(arr)]=arr[''e''];',
  7544. ' c:=low(arr2);',
  7545. ' c:=high(arr2);',
  7546. ' arr2[''b'']:=''f'';',
  7547. ' arr2[''a'']:=arr2[c];',
  7548. ' arr2[c]:=arr2[''g''];']);
  7549. ConvertProgram;
  7550. CheckSource('TestArray_StaticChar',
  7551. LinesToStr([ // statements
  7552. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  7553. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  7554. 'this.Arr3 = ["p", "a", "s"];',
  7555. 'this.Arr4 = ["p", "a", "s"];',
  7556. 'this.Arr5 = ["ä", "ö"];',
  7557. 'this.Arr6 = ["ä", "ö"];',
  7558. 'this.c = "";',
  7559. 'this.b = false;',
  7560. '']),
  7561. LinesToStr([ // $mod.$main
  7562. '$mod.c = "\x00";',
  7563. '$mod.c = "\uFFFF";',
  7564. '$mod.Arr[66] = "a";',
  7565. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  7566. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  7567. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  7568. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  7569. '$mod.c = "a";',
  7570. '$mod.c = "z";',
  7571. '$mod.Arr2[1] = "f";',
  7572. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  7573. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  7574. '']));
  7575. end;
  7576. procedure TTestModule.TestArray_StaticMultiDim;
  7577. begin
  7578. StartProgram(false);
  7579. Add([
  7580. 'type',
  7581. ' TArrayInt = array[1..3] of longint;',
  7582. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  7583. 'var',
  7584. ' Arr: TArrayInt;',
  7585. ' Arr2: TArrayArrayInt;',
  7586. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  7587. ' i: longint;',
  7588. 'begin',
  7589. ' i:=low(arr);',
  7590. ' i:=low(arr2);',
  7591. ' i:=low(arr2[5]);',
  7592. ' i:=high(arr);',
  7593. ' i:=high(arr2);',
  7594. ' i:=high(arr2[6]);',
  7595. ' arr2[5]:=arr;',
  7596. ' arr2[6][2]:=i;',
  7597. ' i:=arr2[6][3];',
  7598. ' arr2[6,3]:=i;',
  7599. ' i:=arr2[5,2];',
  7600. ' arr2:=arr2;',// clone multi dim static array
  7601. //' arr3:=arr3;',// clone anonymous multi dim static array
  7602. '']);
  7603. ConvertProgram;
  7604. CheckSource('TestArray_StaticMultiDim',
  7605. LinesToStr([ // statements
  7606. 'this.TArrayArrayInt$clone = function (a) {',
  7607. ' var r = [];',
  7608. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  7609. ' return r;',
  7610. '};',
  7611. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  7612. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  7613. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  7614. 'this.i = 0;'
  7615. ]),
  7616. LinesToStr([ // $mod.$main
  7617. '$mod.i = 1;',
  7618. '$mod.i = 5;',
  7619. '$mod.i = 1;',
  7620. '$mod.i = 3;',
  7621. '$mod.i = 6;',
  7622. '$mod.i = 3;',
  7623. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  7624. '$mod.Arr2[1][1] = $mod.i;',
  7625. '$mod.i = $mod.Arr2[1][2];',
  7626. '$mod.Arr2[1][2] = $mod.i;',
  7627. '$mod.i = $mod.Arr2[0][1];',
  7628. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  7629. '']));
  7630. end;
  7631. procedure TTestModule.TestArray_StaticInFunction;
  7632. begin
  7633. StartProgram(false);
  7634. Add([
  7635. 'const TArrayInt = 3;',
  7636. 'const TArrayArrayInt = 4;',
  7637. 'procedure DoIt;',
  7638. 'type',
  7639. ' TArrayInt = array[1..3] of longint;',
  7640. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  7641. 'var',
  7642. ' Arr: TArrayInt;',
  7643. ' Arr2: TArrayArrayInt;',
  7644. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  7645. ' i: longint;',
  7646. 'begin',
  7647. ' arr2[5]:=arr;',
  7648. ' arr2:=arr2;',// clone multi dim static array
  7649. 'end;',
  7650. 'begin',
  7651. '']);
  7652. ConvertProgram;
  7653. CheckSource('TestArray_StaticInFunction',
  7654. LinesToStr([ // statements
  7655. 'this.TArrayInt = 3;',
  7656. 'this.TArrayArrayInt = 4;',
  7657. 'var TArrayArrayInt$1$clone = function (a) {',
  7658. ' var r = [];',
  7659. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  7660. ' return r;',
  7661. '};',
  7662. 'this.DoIt = function () {',
  7663. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  7664. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  7665. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  7666. ' var i = 0;',
  7667. ' Arr2[0] = Arr.slice(0);',
  7668. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  7669. '};',
  7670. '']),
  7671. LinesToStr([ // $mod.$main
  7672. '']));
  7673. end;
  7674. procedure TTestModule.TestArrayOfRecord;
  7675. begin
  7676. StartProgram(false);
  7677. Add([
  7678. 'type',
  7679. ' TRec = record',
  7680. ' Int: longint;',
  7681. ' end;',
  7682. ' TArrayRec = array of TRec;',
  7683. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  7684. 'begin',
  7685. 'end;',
  7686. 'var',
  7687. ' Arr: TArrayRec;',
  7688. ' r: TRec;',
  7689. ' i: longint;',
  7690. 'begin',
  7691. ' SetLength(arr,3);',
  7692. ' arr[0].int:=4;',
  7693. ' arr[1].int:=length(arr)+arr[2].int;',
  7694. ' arr[arr[i].int].int:=arr[5].int;',
  7695. ' arr[7]:=r;',
  7696. ' r:=arr[8];',
  7697. ' i:=low(arr);',
  7698. ' i:=high(arr);',
  7699. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  7700. ConvertProgram;
  7701. CheckSource('TestArrayOfRecord',
  7702. LinesToStr([ // statements
  7703. 'rtl.recNewT($mod, "TRec", function () {',
  7704. ' this.Int = 0;',
  7705. ' this.$eq = function (b) {',
  7706. ' return this.Int === b.Int;',
  7707. ' };',
  7708. ' this.$assign = function (s) {',
  7709. ' this.Int = s.Int;',
  7710. ' return this;',
  7711. ' };',
  7712. '});',
  7713. 'this.DoIt = function (vd, vc, vv) {',
  7714. '};',
  7715. 'this.Arr = [];',
  7716. 'this.r = $mod.TRec.$new();',
  7717. 'this.i = 0;'
  7718. ]),
  7719. LinesToStr([ // $mod.$main
  7720. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  7721. '$mod.Arr[0].Int = 4;',
  7722. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  7723. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  7724. '$mod.Arr[7].$assign($mod.r);',
  7725. '$mod.r.$assign($mod.Arr[8]);',
  7726. '$mod.i = 0;',
  7727. '$mod.i = rtl.length($mod.Arr)-1;',
  7728. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  7729. '']));
  7730. end;
  7731. procedure TTestModule.TestArray_StaticRecord;
  7732. begin
  7733. StartProgram(false);
  7734. Add([
  7735. 'type',
  7736. ' TRec = record',
  7737. ' Int: longint;',
  7738. ' end;',
  7739. ' TArrayRec = array[1..2] of TRec;',
  7740. 'var',
  7741. ' Arr: TArrayRec;',
  7742. 'begin',
  7743. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  7744. '']);
  7745. ConvertProgram;
  7746. CheckSource('TestArray_StaticRecord',
  7747. LinesToStr([ // statements
  7748. 'rtl.recNewT($mod, "TRec", function () {',
  7749. ' this.Int = 0;',
  7750. ' this.$eq = function (b) {',
  7751. ' return this.Int === b.Int;',
  7752. ' };',
  7753. ' this.$assign = function (s) {',
  7754. ' this.Int = s.Int;',
  7755. ' return this;',
  7756. ' };',
  7757. '});',
  7758. 'this.TArrayRec$clone = function (a) {',
  7759. ' var r = [];',
  7760. ' for (var i = 0; i < 2; i++) r.push($mod.TRec.$clone(a[i]));',
  7761. ' return r;',
  7762. '};',
  7763. 'this.Arr = rtl.arraySetLength(null, $mod.TRec, 2);',
  7764. '']),
  7765. LinesToStr([ // $mod.$main
  7766. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  7767. end;
  7768. procedure TTestModule.TestArrayOfSet;
  7769. begin
  7770. StartProgram(false);
  7771. Add([
  7772. 'type',
  7773. ' TFlag = (big,small);',
  7774. ' TSetOfFlag = set of tflag;',
  7775. ' TArrayFlag = array of TSetOfFlag;',
  7776. 'procedure DoIt(const a: Tarrayflag);',
  7777. 'begin',
  7778. 'end;',
  7779. 'var',
  7780. ' f: TFlag;',
  7781. ' s: TSetOfFlag;',
  7782. ' Arr: TArrayFlag;',
  7783. ' i: longint;',
  7784. 'begin',
  7785. ' SetLength(arr,3);',
  7786. ' arr[0]:=s;',
  7787. ' arr[1]:=[big];',
  7788. ' arr[2]:=[big]+s;',
  7789. ' arr[3]:=s+[big];',
  7790. ' arr[4]:=arr[5];',
  7791. ' s:=arr[6];',
  7792. ' i:=low(arr);',
  7793. ' i:=high(arr);',
  7794. ' DoIt(arr);',
  7795. ' DoIt([s]);',
  7796. ' DoIt([[],s]);',
  7797. ' DoIt([s,[]]);',
  7798. '']);
  7799. ConvertProgram;
  7800. CheckSource('TestArrayOfSet',
  7801. LinesToStr([ // statements
  7802. 'this.TFlag = {',
  7803. ' "0": "big",',
  7804. ' big: 0,',
  7805. ' "1": "small",',
  7806. ' small: 1',
  7807. '};',
  7808. 'this.DoIt = function (a) {',
  7809. '};',
  7810. 'this.f = 0;',
  7811. 'this.s = {};',
  7812. 'this.Arr = [];',
  7813. 'this.i = 0;',
  7814. '']),
  7815. LinesToStr([ // $mod.$main
  7816. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  7817. '$mod.Arr[0] = rtl.refSet($mod.s);',
  7818. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  7819. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  7820. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  7821. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  7822. '$mod.s = rtl.refSet($mod.Arr[6]);',
  7823. '$mod.i = 0;',
  7824. '$mod.i = rtl.length($mod.Arr) - 1;',
  7825. '$mod.DoIt($mod.Arr);',
  7826. '$mod.DoIt([rtl.refSet($mod.s)]);',
  7827. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  7828. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  7829. '']));
  7830. end;
  7831. procedure TTestModule.TestArray_DynAsParam;
  7832. begin
  7833. StartProgram(false);
  7834. Add([
  7835. 'type integer = longint;',
  7836. 'type TArrInt = array of integer;',
  7837. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  7838. 'var vJ: TArrInt;',
  7839. 'begin',
  7840. ' vg:=vg;',
  7841. ' vj:=vh;',
  7842. ' vi:=vi;',
  7843. ' doit(vg,vg,vg);',
  7844. ' doit(vh,vh,vj);',
  7845. ' doit(vi,vi,vi);',
  7846. ' doit(vj,vj,vj);',
  7847. 'end;',
  7848. 'var i: TArrInt;',
  7849. 'begin',
  7850. ' doit(i,i,i);']);
  7851. ConvertProgram;
  7852. CheckSource('TestArray_DynAsParams',
  7853. LinesToStr([ // statements
  7854. 'this.DoIt = function (vG,vH,vI) {',
  7855. ' var vJ = [];',
  7856. ' vG = vG;',
  7857. ' vJ = vH;',
  7858. ' vI.set(vI.get());',
  7859. ' $mod.DoIt(vG, vG, {',
  7860. ' get: function () {',
  7861. ' return vG;',
  7862. ' },',
  7863. ' set: function (v) {',
  7864. ' vG = v;',
  7865. ' }',
  7866. ' });',
  7867. ' $mod.DoIt(vH, vH, {',
  7868. ' get: function () {',
  7869. ' return vJ;',
  7870. ' },',
  7871. ' set: function (v) {',
  7872. ' vJ = v;',
  7873. ' }',
  7874. ' });',
  7875. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  7876. ' $mod.DoIt(vJ, vJ, {',
  7877. ' get: function () {',
  7878. ' return vJ;',
  7879. ' },',
  7880. ' set: function (v) {',
  7881. ' vJ = v;',
  7882. ' }',
  7883. ' });',
  7884. '};',
  7885. 'this.i = [];'
  7886. ]),
  7887. LinesToStr([
  7888. '$mod.DoIt($mod.i,$mod.i,{',
  7889. ' p: $mod,',
  7890. ' get: function () {',
  7891. ' return this.p.i;',
  7892. ' },',
  7893. ' set: function (v) {',
  7894. ' this.p.i = v;',
  7895. ' }',
  7896. '});'
  7897. ]));
  7898. end;
  7899. procedure TTestModule.TestArray_StaticAsParam;
  7900. begin
  7901. StartProgram(false);
  7902. Add([
  7903. 'type integer = longint;',
  7904. 'type TArrInt = array[1..2] of integer;',
  7905. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  7906. 'var vJ: TArrInt;',
  7907. 'begin',
  7908. ' vg:=vg;',
  7909. ' vj:=vh;',
  7910. ' vi:=vi;',
  7911. ' doit(vg,vg,vg);',
  7912. ' doit(vh,vh,vj);',
  7913. ' doit(vi,vi,vi);',
  7914. ' doit(vj,vj,vj);',
  7915. 'end;',
  7916. 'var i: TArrInt;',
  7917. 'begin',
  7918. ' doit(i,i,i);']);
  7919. ConvertProgram;
  7920. CheckSource('TestArray_StaticAsParams',
  7921. LinesToStr([ // statements
  7922. 'this.DoIt = function (vG,vH,vI) {',
  7923. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  7924. ' vG = vG.slice(0);',
  7925. ' vJ = vH.slice(0);',
  7926. ' vI.set(vI.get().slice(0));',
  7927. ' $mod.DoIt(vG.slice(0), vG, {',
  7928. ' get: function () {',
  7929. ' return vG;',
  7930. ' },',
  7931. ' set: function (v) {',
  7932. ' vG = v;',
  7933. ' }',
  7934. ' });',
  7935. ' $mod.DoIt(vH.slice(0), vH, {',
  7936. ' get: function () {',
  7937. ' return vJ;',
  7938. ' },',
  7939. ' set: function (v) {',
  7940. ' vJ = v;',
  7941. ' }',
  7942. ' });',
  7943. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  7944. ' $mod.DoIt(vJ.slice(0), vJ, {',
  7945. ' get: function () {',
  7946. ' return vJ;',
  7947. ' },',
  7948. ' set: function (v) {',
  7949. ' vJ = v;',
  7950. ' }',
  7951. ' });',
  7952. '};',
  7953. 'this.i = rtl.arraySetLength(null, 0, 2);'
  7954. ]),
  7955. LinesToStr([
  7956. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  7957. ' p: $mod,',
  7958. ' get: function () {',
  7959. ' return this.p.i;',
  7960. ' },',
  7961. ' set: function (v) {',
  7962. ' this.p.i = v;',
  7963. ' }',
  7964. '});'
  7965. ]));
  7966. end;
  7967. procedure TTestModule.TestArrayElement_AsParams;
  7968. begin
  7969. StartProgram(false);
  7970. Add('type integer = longint;');
  7971. Add('type TArrayInt = array of integer;');
  7972. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  7973. Add('var vJ: tarrayint;');
  7974. Add('begin');
  7975. Add(' vi:=vi;');
  7976. Add(' doit(vi,vi,vi);');
  7977. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  7978. Add('end;');
  7979. Add('var a: TArrayInt;');
  7980. Add('begin');
  7981. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  7982. ConvertProgram;
  7983. CheckSource('TestArrayElement_AsParams',
  7984. LinesToStr([ // statements
  7985. 'this.DoIt = function (vG,vH,vI) {',
  7986. ' var vJ = [];',
  7987. ' vI.set(vI.get());',
  7988. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  7989. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  7990. ' a:1+3,',
  7991. ' p:vJ,',
  7992. ' get: function () {',
  7993. ' return this.p[this.a];',
  7994. ' },',
  7995. ' set: function (v) {',
  7996. ' this.p[this.a] = v;',
  7997. ' }',
  7998. ' });',
  7999. '};',
  8000. 'this.a = [];'
  8001. ]),
  8002. LinesToStr([
  8003. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  8004. ' a: 1+6,',
  8005. ' p: $mod.a,',
  8006. ' get: function () {',
  8007. ' return this.p[this.a];',
  8008. ' },',
  8009. ' set: function (v) {',
  8010. ' this.p[this.a] = v;',
  8011. ' }',
  8012. '});'
  8013. ]));
  8014. end;
  8015. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  8016. begin
  8017. StartProgram(false);
  8018. Add('type Integer = longint;');
  8019. Add('type TArrayInt = array of integer;');
  8020. Add('function GetArr(vB: integer = 0): tarrayint;');
  8021. Add('begin');
  8022. Add('end;');
  8023. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  8024. Add('begin');
  8025. Add('end;');
  8026. Add('begin');
  8027. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  8028. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  8029. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  8030. ConvertProgram;
  8031. CheckSource('TestArrayElementFromFuncResult_AsParams',
  8032. LinesToStr([ // statements
  8033. 'this.GetArr = function (vB) {',
  8034. ' var Result = [];',
  8035. ' return Result;',
  8036. '};',
  8037. 'this.DoIt = function (vG,vH,vI) {',
  8038. '};'
  8039. ]),
  8040. LinesToStr([
  8041. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  8042. ' a: 1+3,',
  8043. ' p: $mod.GetArr(0),',
  8044. ' get: function () {',
  8045. ' return this.p[this.a];',
  8046. ' },',
  8047. ' set: function (v) {',
  8048. ' this.p[this.a] = v;',
  8049. ' }',
  8050. '});',
  8051. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  8052. ' a: 2+3,',
  8053. ' p: $mod.GetArr(0),',
  8054. ' get: function () {',
  8055. ' return this.p[this.a];',
  8056. ' },',
  8057. ' set: function (v) {',
  8058. ' this.p[this.a] = v;',
  8059. ' }',
  8060. '});',
  8061. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  8062. ' a: 3+3,',
  8063. ' p: $mod.GetArr(9),',
  8064. ' get: function () {',
  8065. ' return this.p[this.a];',
  8066. ' },',
  8067. ' set: function (v) {',
  8068. ' this.p[this.a] = v;',
  8069. ' }',
  8070. '});',
  8071. '']));
  8072. end;
  8073. procedure TTestModule.TestArrayEnumTypeRange;
  8074. begin
  8075. StartProgram(false);
  8076. Add([
  8077. 'type',
  8078. ' TEnum = (red,blue);',
  8079. ' TEnumArray = array[TEnum] of longint;',
  8080. 'var',
  8081. ' e: TEnum;',
  8082. ' i: longint;',
  8083. ' a: TEnumArray;',
  8084. ' numbers: TEnumArray = (1,2);',
  8085. ' names: array[TEnum] of string = (''red'',''blue'');',
  8086. 'begin',
  8087. ' e:=low(a);',
  8088. ' e:=high(a);',
  8089. ' i:=a[red];',
  8090. ' a[e]:=a[e];']);
  8091. ConvertProgram;
  8092. CheckSource('TestArrayEnumTypeRange',
  8093. LinesToStr([ // statements
  8094. ' this.TEnum = {',
  8095. ' "0": "red",',
  8096. ' red: 0,',
  8097. ' "1": "blue",',
  8098. ' blue: 1',
  8099. '};',
  8100. 'this.e = 0;',
  8101. 'this.i = 0;',
  8102. 'this.a = rtl.arraySetLength(null,0,2);',
  8103. 'this.numbers = [1, 2];',
  8104. 'this.names = ["red", "blue"];',
  8105. '']),
  8106. LinesToStr([ // $mod.$main
  8107. '$mod.e = $mod.TEnum.red;',
  8108. '$mod.e = $mod.TEnum.blue;',
  8109. '$mod.i = $mod.a[$mod.TEnum.red];',
  8110. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  8111. '']));
  8112. end;
  8113. procedure TTestModule.TestArray_SetLengthOutArg;
  8114. begin
  8115. StartProgram(false);
  8116. Add([
  8117. 'type TArrInt = array of longint;',
  8118. 'procedure DoIt(out a: TArrInt);',
  8119. 'begin',
  8120. ' SetLength(a,2);',
  8121. 'end;',
  8122. 'begin',
  8123. '']);
  8124. ConvertProgram;
  8125. CheckSource('TestArray_SetLengthOutArg',
  8126. LinesToStr([ // statements
  8127. 'this.DoIt = function (a) {',
  8128. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  8129. '};',
  8130. '']),
  8131. LinesToStr([
  8132. '']));
  8133. end;
  8134. procedure TTestModule.TestArray_SetLengthProperty;
  8135. begin
  8136. StartProgram(false);
  8137. Add('type');
  8138. Add(' TArrInt = array of longint;');
  8139. Add(' TObject = class');
  8140. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  8141. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  8142. Add(' property Colors: TArrInt read GetColors write SetColors;');
  8143. Add(' end;');
  8144. Add('var Obj: TObject;');
  8145. Add('begin');
  8146. Add(' SetLength(Obj.Colors,2);');
  8147. ConvertProgram;
  8148. CheckSource('TestArray_SetLengthProperty',
  8149. LinesToStr([ // statements
  8150. 'rtl.createClass($mod, "TObject", null, function () {',
  8151. ' this.$init = function () {',
  8152. ' };',
  8153. ' this.$final = function () {',
  8154. ' };',
  8155. '});',
  8156. 'this.Obj = null;',
  8157. '']),
  8158. LinesToStr([
  8159. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  8160. '']));
  8161. end;
  8162. procedure TTestModule.TestArray_SetLengthMultiDim;
  8163. begin
  8164. StartProgram(false);
  8165. Add([
  8166. 'type',
  8167. ' TArrArrInt = array of array of longint;',
  8168. 'var',
  8169. ' a: TArrArrInt;',
  8170. 'begin',
  8171. ' SetLength(a,2);',
  8172. ' SetLength(a,3,4);',
  8173. '']);
  8174. ConvertProgram;
  8175. CheckSource('TestArray_SetLengthMultiDim',
  8176. LinesToStr([ // statements
  8177. 'this.a = [];']),
  8178. LinesToStr([
  8179. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  8180. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  8181. '']));
  8182. end;
  8183. procedure TTestModule.TestArray_OpenArrayOfString;
  8184. begin
  8185. StartProgram(false);
  8186. Add('procedure DoIt(const a: array of String);');
  8187. Add('var');
  8188. Add(' i: longint;');
  8189. Add(' s: string;');
  8190. Add('begin');
  8191. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  8192. Add('end;');
  8193. Add('var s: string;');
  8194. Add('begin');
  8195. Add(' DoIt([]);');
  8196. Add(' DoIt([s,''foo'','''',s+s]);');
  8197. ConvertProgram;
  8198. CheckSource('TestArray_OpenArrayOfString',
  8199. LinesToStr([ // statements
  8200. 'this.DoIt = function (a) {',
  8201. ' var i = 0;',
  8202. ' var s = "";',
  8203. ' for (var $l1 = 0, $end2 = rtl.length(a) - 1; $l1 <= $end2; $l1++) {',
  8204. ' i = $l1;',
  8205. ' s = a[rtl.length(a) - i - 1];',
  8206. ' };',
  8207. '};',
  8208. 'this.s = "";',
  8209. '']),
  8210. LinesToStr([
  8211. '$mod.DoIt([]);',
  8212. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  8213. '']));
  8214. end;
  8215. procedure TTestModule.TestArray_Concat;
  8216. begin
  8217. StartProgram(false);
  8218. Add([
  8219. 'type',
  8220. ' integer = longint;',
  8221. ' TFlag = (big,small);',
  8222. ' TFlags = set of TFlag;',
  8223. ' TRec = record',
  8224. ' i: integer;',
  8225. ' end;',
  8226. ' TArrInt = array of integer;',
  8227. ' TArrRec = array of TRec;',
  8228. ' TArrFlag = array of TFlag;',
  8229. ' TArrSet = array of TFlags;',
  8230. ' TArrJSValue = array of jsvalue;',
  8231. 'var',
  8232. ' ArrInt: tarrint;',
  8233. ' ArrRec: tarrrec;',
  8234. ' ArrFlag: tarrflag;',
  8235. ' ArrSet: tarrset;',
  8236. ' ArrJSValue: tarrjsvalue;',
  8237. 'begin',
  8238. ' arrint:=concat(arrint);',
  8239. ' arrint:=concat(arrint,arrint);',
  8240. ' arrint:=concat(arrint,arrint,arrint);',
  8241. ' arrrec:=concat(arrrec);',
  8242. ' arrrec:=concat(arrrec,arrrec);',
  8243. ' arrrec:=concat(arrrec,arrrec,arrrec);',
  8244. ' arrset:=concat(arrset);',
  8245. ' arrset:=concat(arrset,arrset);',
  8246. ' arrset:=concat(arrset,arrset,arrset);',
  8247. ' arrjsvalue:=concat(arrjsvalue);',
  8248. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue);',
  8249. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);',
  8250. ' arrint:=concat([1],arrint);',
  8251. ' arrflag:=concat([big]);',
  8252. ' arrflag:=concat([big],arrflag);',
  8253. ' arrflag:=concat(arrflag,[small]);',
  8254. '']);
  8255. ConvertProgram;
  8256. CheckSource('TestArray_Concat',
  8257. LinesToStr([ // statements
  8258. 'this.TFlag = {',
  8259. ' "0": "big",',
  8260. ' big: 0,',
  8261. ' "1": "small",',
  8262. ' small: 1',
  8263. '};',
  8264. 'rtl.recNewT($mod, "TRec", function () {',
  8265. ' this.i = 0;',
  8266. ' this.$eq = function (b) {',
  8267. ' return this.i === b.i;',
  8268. ' };',
  8269. ' this.$assign = function (s) {',
  8270. ' this.i = s.i;',
  8271. ' return this;',
  8272. ' };',
  8273. '});',
  8274. 'this.ArrInt = [];',
  8275. 'this.ArrRec = [];',
  8276. 'this.ArrFlag = [];',
  8277. 'this.ArrSet = [];',
  8278. 'this.ArrJSValue = [];',
  8279. '']),
  8280. LinesToStr([ // $mod.$main
  8281. '$mod.ArrInt = $mod.ArrInt;',
  8282. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt);',
  8283. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt, $mod.ArrInt);',
  8284. '$mod.ArrRec = $mod.ArrRec;',
  8285. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  8286. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec, $mod.ArrRec);',
  8287. '$mod.ArrSet = $mod.ArrSet;',
  8288. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  8289. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet, $mod.ArrSet);',
  8290. '$mod.ArrJSValue = $mod.ArrJSValue;',
  8291. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue);',
  8292. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue, $mod.ArrJSValue);',
  8293. '$mod.ArrInt = rtl.arrayConcatN([1], $mod.ArrInt);',
  8294. '$mod.ArrFlag = [$mod.TFlag.big];',
  8295. '$mod.ArrFlag = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag);',
  8296. '$mod.ArrFlag = rtl.arrayConcatN($mod.ArrFlag, [$mod.TFlag.small]);',
  8297. '']));
  8298. end;
  8299. procedure TTestModule.TestArray_Copy;
  8300. begin
  8301. StartProgram(false);
  8302. Add([
  8303. 'type',
  8304. ' integer = longint;',
  8305. ' TFlag = (big,small);',
  8306. ' TFlags = set of TFlag;',
  8307. ' TRec = record',
  8308. ' i: integer;',
  8309. ' end;',
  8310. ' TArrInt = array of integer;',
  8311. ' TArrRec = array of TRec;',
  8312. ' TArrSet = array of TFlags;',
  8313. ' TArrJSValue = array of jsvalue;',
  8314. 'var',
  8315. ' ArrInt: tarrint;',
  8316. ' ArrRec: tarrrec;',
  8317. ' ArrSet: tarrset;',
  8318. ' ArrJSValue: tarrjsvalue;',
  8319. 'begin',
  8320. ' arrint:=copy(arrint);',
  8321. ' arrint:=copy(arrint,2);',
  8322. ' arrint:=copy(arrint,3,4);',
  8323. ' arrint:=copy([1,1],1,2);',
  8324. ' arrrec:=copy(arrrec);',
  8325. ' arrrec:=copy(arrrec,5);',
  8326. ' arrrec:=copy(arrrec,6,7);',
  8327. ' arrset:=copy(arrset);',
  8328. ' arrset:=copy(arrset,8);',
  8329. ' arrset:=copy(arrset,9,10);',
  8330. ' arrjsvalue:=copy(arrjsvalue);',
  8331. ' arrjsvalue:=copy(arrjsvalue,11);',
  8332. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  8333. ' ']);
  8334. ConvertProgram;
  8335. CheckSource('TestArray_Copy',
  8336. LinesToStr([ // statements
  8337. 'this.TFlag = {',
  8338. ' "0": "big",',
  8339. ' big: 0,',
  8340. ' "1": "small",',
  8341. ' small: 1',
  8342. '};',
  8343. 'rtl.recNewT($mod, "TRec", function () {',
  8344. ' this.i = 0;',
  8345. ' this.$eq = function (b) {',
  8346. ' return this.i === b.i;',
  8347. ' };',
  8348. ' this.$assign = function (s) {',
  8349. ' this.i = s.i;',
  8350. ' return this;',
  8351. ' };',
  8352. '});',
  8353. 'this.ArrInt = [];',
  8354. 'this.ArrRec = [];',
  8355. 'this.ArrSet = [];',
  8356. 'this.ArrJSValue = [];',
  8357. '']),
  8358. LinesToStr([ // $mod.$main
  8359. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  8360. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  8361. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  8362. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  8363. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  8364. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  8365. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  8366. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  8367. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  8368. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  8369. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  8370. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  8371. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  8372. '']));
  8373. end;
  8374. procedure TTestModule.TestArray_InsertDelete;
  8375. begin
  8376. StartProgram(false);
  8377. Add([
  8378. 'type',
  8379. ' integer = longint;',
  8380. ' TFlag = (big,small);',
  8381. ' TFlags = set of TFlag;',
  8382. ' TRec = record',
  8383. ' i: integer;',
  8384. ' end;',
  8385. ' TArrInt = array of integer;',
  8386. ' TArrRec = array of TRec;',
  8387. ' TArrSet = array of TFlags;',
  8388. ' TArrJSValue = array of jsvalue;',
  8389. ' TArrArrInt = array of TArrInt;',
  8390. 'var',
  8391. ' ArrInt: tarrint;',
  8392. ' ArrRec: tarrrec;',
  8393. ' ArrSet: tarrset;',
  8394. ' ArrJSValue: tarrjsvalue;',
  8395. ' ArrArrInt: TArrArrInt;',
  8396. 'begin',
  8397. ' Insert(1,arrint,2);',
  8398. ' Insert(arrint[3],arrint,4);',
  8399. ' Insert(arrrec[5],arrrec,6);',
  8400. ' Insert(arrset[7],arrset,7);',
  8401. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  8402. ' Insert(10,arrjsvalue,11);',
  8403. ' Insert([23],arrarrint,22);',
  8404. ' Delete(arrint,12,13);',
  8405. ' Delete(arrrec,14,15);',
  8406. ' Delete(arrset,17,18);',
  8407. ' Delete(arrjsvalue,19,10);']);
  8408. ConvertProgram;
  8409. CheckSource('TestArray_InsertDelete',
  8410. LinesToStr([ // statements
  8411. 'this.TFlag = {',
  8412. ' "0": "big",',
  8413. ' big: 0,',
  8414. ' "1": "small",',
  8415. ' small: 1',
  8416. '};',
  8417. 'rtl.recNewT($mod, "TRec", function () {',
  8418. ' this.i = 0;',
  8419. ' this.$eq = function (b) {',
  8420. ' return this.i === b.i;',
  8421. ' };',
  8422. ' this.$assign = function (s) {',
  8423. ' this.i = s.i;',
  8424. ' return this;',
  8425. ' };',
  8426. '});',
  8427. 'this.ArrInt = [];',
  8428. 'this.ArrRec = [];',
  8429. 'this.ArrSet = [];',
  8430. 'this.ArrJSValue = [];',
  8431. 'this.ArrArrInt = [];',
  8432. '']),
  8433. LinesToStr([ // $mod.$main
  8434. '$mod.ArrInt.splice(2, 0, 1);',
  8435. '$mod.ArrInt.splice(4, 0, $mod.ArrInt[3]);',
  8436. '$mod.ArrRec.splice(6, 0, $mod.ArrRec[5]);',
  8437. '$mod.ArrSet.splice(7, 0, $mod.ArrSet[7]);',
  8438. '$mod.ArrJSValue.splice(9, 0, $mod.ArrJSValue[8]);',
  8439. '$mod.ArrJSValue.splice(11, 0, 10);',
  8440. '$mod.ArrArrInt.splice(22, 0, [23]);',
  8441. '$mod.ArrInt.splice(12, 13);',
  8442. '$mod.ArrRec.splice(14, 15);',
  8443. '$mod.ArrSet.splice(17, 18);',
  8444. '$mod.ArrJSValue.splice(19, 10);',
  8445. '']));
  8446. end;
  8447. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  8448. begin
  8449. StartProgram(false);
  8450. Add([
  8451. '{$modeswitch arrayoperators}',
  8452. 'type',
  8453. ' integer = longint;',
  8454. ' TArrInt = array of integer;',
  8455. ' TArrStr = array of string;',
  8456. 'const',
  8457. ' Ints: TArrInt = (1,2,3);',
  8458. ' Aliases: TarrStr = (''foo'',''b'');',
  8459. ' OneInt: TArrInt = (7);',
  8460. ' OneStr: array of integer = (7);',
  8461. ' Chars: array of char = ''aoc'';',
  8462. ' Names: array of string = (''a'',''foo'');',
  8463. ' NameCount = low(Names)+high(Names)+length(Names);',
  8464. 'var i: integer;',
  8465. 'begin',
  8466. ' Ints:=[];',
  8467. ' Ints:=[1,1];',
  8468. ' Ints:=[1]+[2];',
  8469. ' Ints:=[2];',
  8470. ' Ints:=[]+ints;',
  8471. ' Ints:=Ints+[];',
  8472. ' Ints:=Ints+OneInt;',
  8473. ' Ints:=Ints+[1,1];',
  8474. ' Ints:=[i,i]+Ints;',
  8475. ' Ints:=[1]+[i]+[3];',
  8476. '']);
  8477. ConvertProgram;
  8478. CheckSource('TestArray_DynArrayConstObjFPC',
  8479. LinesToStr([ // statements
  8480. 'this.Ints = [1, 2, 3];',
  8481. 'this.Aliases = ["foo", "b"];',
  8482. 'this.OneInt = [7];',
  8483. 'this.OneStr = [7];',
  8484. 'this.Chars = ["a", "o", "c"];',
  8485. 'this.Names = ["a", "foo"];',
  8486. 'this.NameCount = 0 + (rtl.length($mod.Names) - 1) + rtl.length($mod.Names);',
  8487. 'this.i = 0;',
  8488. '']),
  8489. LinesToStr([ // $mod.$main
  8490. '$mod.Ints = [];',
  8491. '$mod.Ints = [1, 1];',
  8492. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  8493. '$mod.Ints = [2];',
  8494. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  8495. '$mod.Ints = rtl.arrayConcatN($mod.Ints, []);',
  8496. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  8497. '$mod.Ints = rtl.arrayConcatN($mod.Ints, [1, 1]);',
  8498. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  8499. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  8500. '']));
  8501. end;
  8502. procedure TTestModule.TestArray_DynArrayConstDelphi;
  8503. begin
  8504. StartProgram(false);
  8505. // Note: const c = [1,1]; defines a set!
  8506. Add([
  8507. '{$mode delphi}',
  8508. 'type',
  8509. ' integer = longint;',
  8510. ' TArrInt = array of integer;',
  8511. ' TArrStr = array of string;',
  8512. 'const',
  8513. ' Ints: TArrInt = [1,1,2];',
  8514. ' Aliases: TarrStr = [''foo'',''b''];',
  8515. ' OneInt: TArrInt = [7];',
  8516. ' OneStr: array of integer = [7]+[8];',
  8517. ' Chars: array of char = ''aoc'';',
  8518. ' Names: array of string = [''a'',''a''];',
  8519. ' NameCount = low(Names)+high(Names)+length(Names);',
  8520. 'begin',
  8521. '']);
  8522. ConvertProgram;
  8523. CheckSource('TestArray_DynArrayConstDelphi',
  8524. LinesToStr([ // statements
  8525. 'this.Ints = [1, 1, 2];',
  8526. 'this.Aliases = ["foo", "b"];',
  8527. 'this.OneInt = [7];',
  8528. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  8529. 'this.Chars = ["a", "o", "c"];',
  8530. 'this.Names = ["a", "a"];',
  8531. 'this.NameCount = 0 + (rtl.length($mod.Names) - 1) + rtl.length($mod.Names);',
  8532. '']),
  8533. LinesToStr([ // $mod.$main
  8534. '']));
  8535. end;
  8536. procedure TTestModule.TestArray_ArrayLitAsParam;
  8537. begin
  8538. StartProgram(false);
  8539. Add([
  8540. '{$modeswitch arrayoperators}',
  8541. 'type',
  8542. ' integer = longint;',
  8543. ' TArrInt = array of integer;',
  8544. ' TArrSet = array of (red,green,blue);',
  8545. 'procedure DoOpenInt(a: array of integer); forward;',
  8546. 'procedure DoInt(a: TArrInt);',
  8547. 'begin',
  8548. ' DoInt(a+[1]);',
  8549. ' DoInt([1]+a);',
  8550. ' DoOpenInt(a);',
  8551. ' DoOpenInt(a+[1]);',
  8552. ' DoOpenInt([1]+a);',
  8553. 'end;',
  8554. 'procedure DoOpenInt(a: array of integer);',
  8555. 'begin',
  8556. ' DoOpenInt(a+[1]);',
  8557. ' DoOpenInt([1]+a);',
  8558. ' DoInt(a);',
  8559. ' DoInt(a+[1]);',
  8560. ' DoInt([1]+a);',
  8561. 'end;',
  8562. 'procedure DoSet(a: TArrSet);',
  8563. 'begin',
  8564. ' DoSet(a+[red]);',
  8565. ' DoSet([blue]+a);',
  8566. 'end;',
  8567. 'var',
  8568. ' i: TArrInt;',
  8569. ' s: TArrSet;',
  8570. 'begin',
  8571. ' DoInt([1]);',
  8572. ' DoInt([1]+[2]);',
  8573. ' DoInt(i+[1]);',
  8574. ' DoInt([1]+i);',
  8575. ' DoOpenInt([1]);',
  8576. ' DoOpenInt([1]+[2]);',
  8577. ' DoOpenInt(i+[1]);',
  8578. ' DoOpenInt([1]+i);',
  8579. ' DoSet([red]);',
  8580. ' DoSet([blue]+[green]);',
  8581. ' DoSet(s+[blue]);',
  8582. ' DoSet([red]+s);',
  8583. '']);
  8584. ConvertProgram;
  8585. CheckSource('TestArray_ArrayLitAsParam',
  8586. LinesToStr([ // statements
  8587. 'this.TArrSet$a = {',
  8588. ' "0": "red",',
  8589. ' red: 0,',
  8590. ' "1": "green",',
  8591. ' green: 1,',
  8592. ' "2": "blue",',
  8593. ' blue: 2',
  8594. '};',
  8595. 'this.DoInt = function (a) {',
  8596. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  8597. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  8598. ' $mod.DoOpenInt(a);',
  8599. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  8600. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  8601. '};',
  8602. 'this.DoOpenInt = function (a) {',
  8603. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  8604. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  8605. ' $mod.DoInt(a);',
  8606. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  8607. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  8608. '};',
  8609. 'this.DoSet = function (a) {',
  8610. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  8611. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  8612. '};',
  8613. 'this.i = [];',
  8614. 'this.s = [];',
  8615. '']),
  8616. LinesToStr([ // $mod.$main
  8617. '$mod.DoInt([1]);',
  8618. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  8619. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  8620. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  8621. '$mod.DoOpenInt([1]);',
  8622. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  8623. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  8624. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  8625. '$mod.DoSet([$mod.TArrSet$a.red]);',
  8626. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  8627. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  8628. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  8629. '']));
  8630. end;
  8631. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  8632. begin
  8633. StartProgram(false);
  8634. Add([
  8635. '{$modeswitch arrayoperators}',
  8636. 'type',
  8637. ' integer = longint;',
  8638. ' TArrInt = array of integer;',
  8639. ' TArrArrInt = array of TArrInt;',
  8640. 'procedure DoInt(a: TArrArrInt);',
  8641. 'begin',
  8642. ' DoInt(a+[[1]]);',
  8643. ' DoInt([[1]]+a);',
  8644. ' DoInt(a);',
  8645. 'end;',
  8646. 'var',
  8647. ' i: TArrInt;',
  8648. ' a: TArrArrInt;',
  8649. 'begin',
  8650. ' a:=[[1]];',
  8651. ' a:=[i];',
  8652. ' a:=a+[i];',
  8653. ' a:=[i]+a;',
  8654. ' a:=[[1]+i];',
  8655. ' a:=[[1]+[2]];',
  8656. ' a:=[i+[2]];',
  8657. ' DoInt([[1]]);',
  8658. ' DoInt([[1]+[2],[3,4],[5]]);',
  8659. ' DoInt([i+[1]]+a);',
  8660. ' DoInt([i]+a);',
  8661. '']);
  8662. ConvertProgram;
  8663. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  8664. LinesToStr([ // statements
  8665. 'this.DoInt = function (a) {',
  8666. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  8667. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  8668. ' $mod.DoInt(a);',
  8669. '};',
  8670. 'this.i = [];',
  8671. 'this.a = [];',
  8672. '']),
  8673. LinesToStr([ // $mod.$main
  8674. '$mod.a = [[1]];',
  8675. '$mod.a = [$mod.i];',
  8676. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i]);',
  8677. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  8678. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  8679. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  8680. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  8681. '$mod.DoInt([[1]]);',
  8682. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  8683. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  8684. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  8685. '']));
  8686. end;
  8687. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  8688. begin
  8689. StartProgram(false);
  8690. Add([
  8691. '{$modeswitch arrayoperators}',
  8692. 'type',
  8693. ' integer = longint;',
  8694. ' TArrInt = array[1..2] of integer;',
  8695. ' TArrArrInt = array of TArrInt;',
  8696. 'procedure DoInt(a: TArrArrInt);',
  8697. 'begin',
  8698. ' DoInt(a+[[1,2]]);',
  8699. ' DoInt([[1,2]]+a);',
  8700. ' DoInt(a);',
  8701. 'end;',
  8702. 'var',
  8703. ' i: TArrInt;',
  8704. ' a: TArrArrInt;',
  8705. 'begin',
  8706. ' a:=[[1,1]];',
  8707. ' a:=[i];',
  8708. ' a:=a+[i];',
  8709. ' a:=[i]+a;',
  8710. ' DoInt([[1,1]]);',
  8711. ' DoInt([[1,2],[3,4]]);',
  8712. '']);
  8713. ConvertProgram;
  8714. CheckSource('TestArray_ArrayLitStaticAsParam',
  8715. LinesToStr([ // statements
  8716. 'this.DoInt = function (a) {',
  8717. ' $mod.DoInt(rtl.arrayConcatN(a, [[1, 2]]));',
  8718. ' $mod.DoInt(rtl.arrayConcatN([[1, 2]], a));',
  8719. ' $mod.DoInt(a);',
  8720. '};',
  8721. 'this.i = rtl.arraySetLength(null, 0, 2);',
  8722. 'this.a = [];',
  8723. '']),
  8724. LinesToStr([ // $mod.$main
  8725. '$mod.a = [[1, 1]];',
  8726. '$mod.a = [$mod.i.slice(0)];',
  8727. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i.slice(0)]);',
  8728. '$mod.a = rtl.arrayConcatN([$mod.i.slice(0)], $mod.a);',
  8729. '$mod.DoInt([[1, 1]]);',
  8730. '$mod.DoInt([[1, 2], [3, 4]]);',
  8731. '']));
  8732. end;
  8733. procedure TTestModule.TestArray_ForInArrOfString;
  8734. begin
  8735. StartProgram(false);
  8736. Add([
  8737. 'type',
  8738. 'type',
  8739. ' TMonthNameArray = array [1..12] of string;',
  8740. ' TMonthNames = TMonthNameArray;',
  8741. ' TObject = class',
  8742. ' private',
  8743. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  8744. ' public',
  8745. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  8746. ' end;',
  8747. 'var f: TObject;',
  8748. ' Month: string;',
  8749. 'begin',
  8750. ' for Month in f.LongMonthNames do ;',
  8751. '']);
  8752. ConvertProgram;
  8753. CheckSource('TestArray_ForInArrOfString',
  8754. LinesToStr([ // statements
  8755. 'rtl.createClass($mod, "TObject", null, function () {',
  8756. ' this.$init = function () {',
  8757. ' };',
  8758. ' this.$final = function () {',
  8759. ' };',
  8760. '});',
  8761. 'this.f = null;',
  8762. 'this.Month = "";',
  8763. '']),
  8764. LinesToStr([ // $mod.$main
  8765. 'for (var $in1 = $mod.f.GetLongMonthNames(), $l2 = 0, $end3 = rtl.length($in1) - 1; $l2 <= $end3; $l2++) $mod.Month = $in1[$l2];',
  8766. '']));
  8767. end;
  8768. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  8769. begin
  8770. StartProgram(false);
  8771. Add([
  8772. '{$modeswitch externalclass}',
  8773. 'type',
  8774. ' TJSObject = class external name ''Object''',
  8775. ' end;',
  8776. ' TJSArray = class external name ''Array''',
  8777. ' class function isArray(Value: JSValue) : boolean;',
  8778. ' function concat() : TJSArray; varargs;',
  8779. ' end;',
  8780. 'var',
  8781. ' aObj: TJSArray;',
  8782. ' a: array of longint;',
  8783. ' o: TJSObject;',
  8784. 'begin',
  8785. ' if TJSArray.isArray(65) then ;',
  8786. ' aObj:=TJSArray(a).concat(a);',
  8787. ' o:=TJSObject(a);']);
  8788. ConvertProgram;
  8789. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  8790. LinesToStr([ // statements
  8791. 'this.aObj = null;',
  8792. 'this.a = [];',
  8793. 'this.o = null;',
  8794. '']),
  8795. LinesToStr([ // $mod.$main
  8796. 'if (Array.isArray(65)) ;',
  8797. '$mod.aObj = $mod.a.concat($mod.a);',
  8798. '$mod.o = $mod.a;',
  8799. '']));
  8800. end;
  8801. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  8802. begin
  8803. StartProgram(false);
  8804. Add([
  8805. '{$modeswitch externalclass}',
  8806. 'type',
  8807. ' TArrStr = array of string;',
  8808. ' TJSArray = class external name ''Array''',
  8809. ' end;',
  8810. ' TJSObject = class external name ''Object''',
  8811. ' end;',
  8812. 'var',
  8813. ' aObj: TJSArray;',
  8814. ' a: TArrStr;',
  8815. ' jo: TJSObject;',
  8816. 'begin',
  8817. ' a:=TArrStr(aObj);',
  8818. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  8819. ' a:=TarrStr(jo);',
  8820. '']);
  8821. ConvertProgram;
  8822. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  8823. LinesToStr([ // statements
  8824. 'this.aObj = null;',
  8825. 'this.a = [];',
  8826. 'this.jo = null;',
  8827. '']),
  8828. LinesToStr([ // $mod.$main
  8829. '$mod.a = $mod.aObj;',
  8830. '$mod.aObj[1] = $mod.aObj[2];',
  8831. '$mod.a = $mod.jo;',
  8832. '']));
  8833. end;
  8834. procedure TTestModule.TestRecord_Empty;
  8835. begin
  8836. StartProgram(false);
  8837. Add(['type',
  8838. ' TRecA = record',
  8839. ' end;',
  8840. 'var a,b: TRecA;',
  8841. 'begin',
  8842. ' if a=b then ;']);
  8843. ConvertProgram;
  8844. CheckSource('TestRecord_Empty',
  8845. LinesToStr([ // statements
  8846. 'rtl.recNewT($mod, "TRecA", function () {',
  8847. ' this.$eq = function (b) {',
  8848. ' return true;',
  8849. ' };',
  8850. ' this.$assign = function (s) {',
  8851. ' return this;',
  8852. ' };',
  8853. '});',
  8854. 'this.a = $mod.TRecA.$new();',
  8855. 'this.b = $mod.TRecA.$new();',
  8856. '']),
  8857. LinesToStr([ // $mod.$main
  8858. 'if ($mod.a.$eq($mod.b)) ;'
  8859. ]));
  8860. end;
  8861. procedure TTestModule.TestRecord_Var;
  8862. begin
  8863. StartProgram(false);
  8864. Add('type');
  8865. Add(' TRecA = record');
  8866. Add(' Bold: longint;');
  8867. Add(' end;');
  8868. Add('var Rec: TRecA;');
  8869. Add('begin');
  8870. Add(' rec.bold:=123');
  8871. ConvertProgram;
  8872. CheckSource('TestRecord_Var',
  8873. LinesToStr([ // statements
  8874. 'rtl.recNewT($mod, "TRecA", function () {',
  8875. ' this.Bold = 0;',
  8876. ' this.$eq = function (b) {',
  8877. ' return this.Bold === b.Bold;',
  8878. ' };',
  8879. ' this.$assign = function (s) {',
  8880. ' this.Bold = s.Bold;',
  8881. ' return this;',
  8882. ' };',
  8883. '});',
  8884. 'this.Rec = $mod.TRecA.$new();',
  8885. '']),
  8886. LinesToStr([ // $mod.$main
  8887. '$mod.Rec.Bold = 123;'
  8888. ]));
  8889. end;
  8890. procedure TTestModule.TestRecord_VarExternal;
  8891. begin
  8892. StartProgram(false);
  8893. Add([
  8894. '{$modeswitch externalclass}',
  8895. 'type',
  8896. ' TRecA = record',
  8897. ' i: byte;',
  8898. ' length_: longint external name ''length'';',
  8899. ' end;',
  8900. 'var Rec: TRecA;',
  8901. 'begin',
  8902. ' rec.length_ := rec.length_',
  8903. '']);
  8904. ConvertProgram;
  8905. CheckSource('TestRecord_VarExternal',
  8906. LinesToStr([ // statements
  8907. 'rtl.recNewT($mod, "TRecA", function () {',
  8908. ' this.i = 0;',
  8909. ' this.$eq = function (b) {',
  8910. ' return (this.i === b.i) && (this.length === b.length);',
  8911. ' };',
  8912. ' this.$assign = function (s) {',
  8913. ' this.i = s.i;',
  8914. ' this.length = s.length;',
  8915. ' return this;',
  8916. ' };',
  8917. '});',
  8918. 'this.Rec = $mod.TRecA.$new();',
  8919. '']),
  8920. LinesToStr([ // $mod.$main
  8921. '$mod.Rec.length = $mod.Rec.length;'
  8922. ]));
  8923. end;
  8924. procedure TTestModule.TestRecord_WithDo;
  8925. begin
  8926. StartProgram(false);
  8927. Add('type');
  8928. Add(' TRec = record');
  8929. Add(' vI: longint;');
  8930. Add(' end;');
  8931. Add('var');
  8932. Add(' Int: longint;');
  8933. Add(' r: TRec;');
  8934. Add('begin');
  8935. Add(' with r do');
  8936. Add(' int:=vi;');
  8937. Add(' with r do begin');
  8938. Add(' int:=vi;');
  8939. Add(' vi:=int;');
  8940. Add(' end;');
  8941. ConvertProgram;
  8942. CheckSource('TestWithRecordDo',
  8943. LinesToStr([ // statements
  8944. 'rtl.recNewT($mod, "TRec", function () {',
  8945. ' this.vI = 0;',
  8946. ' this.$eq = function (b) {',
  8947. ' return this.vI === b.vI;',
  8948. ' };',
  8949. ' this.$assign = function (s) {',
  8950. ' this.vI = s.vI;',
  8951. ' return this;',
  8952. ' };',
  8953. '});',
  8954. 'this.Int = 0;',
  8955. 'this.r = $mod.TRec.$new();',
  8956. '']),
  8957. LinesToStr([ // $mod.$main
  8958. 'var $with1 = $mod.r;',
  8959. '$mod.Int = $with1.vI;',
  8960. 'var $with2 = $mod.r;',
  8961. '$mod.Int = $with2.vI;',
  8962. '$with2.vI = $mod.Int;'
  8963. ]));
  8964. end;
  8965. procedure TTestModule.TestRecord_Assign;
  8966. begin
  8967. StartProgram(false);
  8968. Add('type');
  8969. Add(' TEnum = (red,green);');
  8970. Add(' TEnums = set of TEnum;');
  8971. Add(' TSmallRec = record');
  8972. Add(' N: longint;');
  8973. Add(' end;');
  8974. Add(' TBigRec = record');
  8975. Add(' Int: longint;');
  8976. Add(' D: double;');
  8977. Add(' Arr: array of longint;');
  8978. Add(' Arr2: array[1..2] of longint;');
  8979. Add(' Small: TSmallRec;');
  8980. Add(' Enums: TEnums;');
  8981. Add(' end;');
  8982. Add('var');
  8983. Add(' r, s: TBigRec;');
  8984. Add('begin');
  8985. Add(' r:=s;');
  8986. Add(' r:=default(TBigRec);');
  8987. Add(' r:=default(s);');
  8988. ConvertProgram;
  8989. CheckSource('TestRecord_Assign',
  8990. LinesToStr([ // statements
  8991. 'this.TEnum = {',
  8992. ' "0": "red",',
  8993. ' red: 0,',
  8994. ' "1": "green",',
  8995. ' green: 1',
  8996. '};',
  8997. 'rtl.recNewT($mod, "TSmallRec", function () {',
  8998. ' this.N = 0;',
  8999. ' this.$eq = function (b) {',
  9000. ' return this.N === b.N;',
  9001. ' };',
  9002. ' this.$assign = function (s) {',
  9003. ' this.N = s.N;',
  9004. ' return this;',
  9005. ' };',
  9006. '});',
  9007. 'rtl.recNewT($mod, "TBigRec", function () {',
  9008. ' this.Int = 0;',
  9009. ' this.D = 0.0;',
  9010. ' this.Arr = [];',
  9011. ' this.$new = function () {',
  9012. ' var r = Object.create(this);',
  9013. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  9014. ' r.Small = $mod.TSmallRec.$new();',
  9015. ' r.Enums = {};',
  9016. ' return r;',
  9017. ' };',
  9018. ' this.$eq = function (b) {',
  9019. ' 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);',
  9020. ' };',
  9021. ' this.$assign = function (s) {',
  9022. ' this.Int = s.Int;',
  9023. ' this.D = s.D;',
  9024. ' this.Arr = s.Arr;',
  9025. ' this.Arr2 = s.Arr2.slice(0);',
  9026. ' this.Small.$assign(s.Small);',
  9027. ' this.Enums = rtl.refSet(s.Enums);',
  9028. ' return this;',
  9029. ' };',
  9030. '});',
  9031. 'this.r = $mod.TBigRec.$new();',
  9032. 'this.s = $mod.TBigRec.$new();',
  9033. '']),
  9034. LinesToStr([ // $mod.$main
  9035. '$mod.r.$assign($mod.s);',
  9036. '$mod.r.$assign($mod.TBigRec.$new());',
  9037. '$mod.r.$assign($mod.TBigRec.$new());',
  9038. '']));
  9039. end;
  9040. procedure TTestModule.TestRecord_AsParams;
  9041. begin
  9042. StartProgram(false);
  9043. Add([
  9044. 'type',
  9045. ' integer = longint;',
  9046. ' TRecord = record',
  9047. ' i: integer;',
  9048. ' end;',
  9049. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  9050. 'var vL: TRecord;',
  9051. 'begin',
  9052. ' vd:=vd;',
  9053. ' vd.i:=vd.i;',
  9054. ' vl:=vc;',
  9055. ' vv:=vv;',
  9056. ' vv.i:=vv.i;',
  9057. ' U:=vl;',
  9058. ' U:=vd;',
  9059. ' U:=vc;',
  9060. ' U:=vv;',
  9061. ' doit(vd,vd,vd,vd);',
  9062. ' doit(vc,vc,vl,vl);',
  9063. ' doit(vv,vv,vv,vv);',
  9064. ' doit(vl,vl,vl,vl);',
  9065. 'end;',
  9066. 'var i: TRecord;',
  9067. 'begin',
  9068. ' doit(i,i,i,i);']);
  9069. ConvertProgram;
  9070. CheckSource('TestRecord_AsParams',
  9071. LinesToStr([ // statements
  9072. 'rtl.recNewT($mod, "TRecord", function () {',
  9073. ' this.i = 0;',
  9074. ' this.$eq = function (b) {',
  9075. ' return this.i === b.i;',
  9076. ' };',
  9077. ' this.$assign = function (s) {',
  9078. ' this.i = s.i;',
  9079. ' return this;',
  9080. ' };',
  9081. '});',
  9082. 'this.DoIt = function (vD, vC, vV, U) {',
  9083. ' var vL = $mod.TRecord.$new();',
  9084. ' vD.$assign(vD);',
  9085. ' vD.i = vD.i;',
  9086. ' vL.$assign(vC);',
  9087. ' vV.$assign(vV);',
  9088. ' vV.i = vV.i;',
  9089. ' U.set(vL);',
  9090. ' U.set(vD);',
  9091. ' U.set(vC);',
  9092. ' U.set(vV);',
  9093. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, {',
  9094. ' get: function () {',
  9095. ' return vD;',
  9096. ' },',
  9097. ' set: function (v) {',
  9098. ' vD.$assign(v);',
  9099. ' }',
  9100. ' });',
  9101. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, {',
  9102. ' get: function () {',
  9103. ' return vL;',
  9104. ' },',
  9105. ' set: function (v) {',
  9106. ' vL.$assign(v);',
  9107. ' }',
  9108. ' });',
  9109. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, {',
  9110. ' get: function () {',
  9111. ' return vV;',
  9112. ' },',
  9113. ' set: function (v) {',
  9114. ' vV.$assign(v);',
  9115. ' }',
  9116. ' });',
  9117. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, {',
  9118. ' get: function () {',
  9119. ' return vL;',
  9120. ' },',
  9121. ' set: function (v) {',
  9122. ' vL.$assign(v);',
  9123. ' }',
  9124. ' });',
  9125. '};',
  9126. 'this.i = $mod.TRecord.$new();'
  9127. ]),
  9128. LinesToStr([
  9129. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, {',
  9130. ' p: $mod,',
  9131. ' get: function () {',
  9132. ' return this.p.i;',
  9133. ' },',
  9134. ' set: function (v) {',
  9135. ' this.p.i.$assign(v);',
  9136. ' }',
  9137. '});',
  9138. '']));
  9139. end;
  9140. procedure TTestModule.TestRecordElement_AsParams;
  9141. begin
  9142. StartProgram(false);
  9143. Add('type');
  9144. Add(' integer = longint;');
  9145. Add(' TRecord = record');
  9146. Add(' i: integer;');
  9147. Add(' end;');
  9148. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  9149. Add('var vJ: TRecord;');
  9150. Add('begin');
  9151. Add(' doit(vj.i,vj.i,vj.i);');
  9152. Add('end;');
  9153. Add('var r: TRecord;');
  9154. Add('begin');
  9155. Add(' doit(r.i,r.i,r.i);');
  9156. ConvertProgram;
  9157. CheckSource('TestRecordElement_AsParams',
  9158. LinesToStr([ // statements
  9159. 'rtl.recNewT($mod, "TRecord", function () {',
  9160. ' this.i = 0;',
  9161. ' this.$eq = function (b) {',
  9162. ' return this.i === b.i;',
  9163. ' };',
  9164. ' this.$assign = function (s) {',
  9165. ' this.i = s.i;',
  9166. ' return this;',
  9167. ' };',
  9168. '});',
  9169. 'this.DoIt = function (vG,vH,vI) {',
  9170. ' var vJ = $mod.TRecord.$new();',
  9171. ' $mod.DoIt(vJ.i, vJ.i, {',
  9172. ' p: vJ,',
  9173. ' get: function () {',
  9174. ' return this.p.i;',
  9175. ' },',
  9176. ' set: function (v) {',
  9177. ' this.p.i = v;',
  9178. ' }',
  9179. ' });',
  9180. '};',
  9181. 'this.r = $mod.TRecord.$new();'
  9182. ]),
  9183. LinesToStr([
  9184. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  9185. ' p: $mod.r,',
  9186. ' get: function () {',
  9187. ' return this.p.i;',
  9188. ' },',
  9189. ' set: function (v) {',
  9190. ' this.p.i = v;',
  9191. ' }',
  9192. '});'
  9193. ]));
  9194. end;
  9195. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  9196. begin
  9197. StartProgram(false);
  9198. Add('type');
  9199. Add(' integer = longint;');
  9200. Add(' TRecord = record');
  9201. Add(' i: integer;');
  9202. Add(' end;');
  9203. Add('function GetRec(vB: integer = 0): TRecord;');
  9204. Add('begin');
  9205. Add('end;');
  9206. Add('procedure DoIt(vG: integer; const vH: integer);');
  9207. Add('begin');
  9208. Add('end;');
  9209. Add('begin');
  9210. Add(' doit(getrec.i,getrec.i);');
  9211. Add(' doit(getrec().i,getrec().i);');
  9212. Add(' doit(getrec(1).i,getrec(2).i);');
  9213. ConvertProgram;
  9214. CheckSource('TestRecordElementFromFuncResult_AsParams',
  9215. LinesToStr([ // statements
  9216. 'rtl.recNewT($mod, "TRecord", function () {',
  9217. ' this.i = 0;',
  9218. ' this.$eq = function (b) {',
  9219. ' return this.i === b.i;',
  9220. ' };',
  9221. ' this.$assign = function (s) {',
  9222. ' this.i = s.i;',
  9223. ' return this;',
  9224. ' };',
  9225. '});',
  9226. 'this.GetRec = function (vB) {',
  9227. ' var Result = $mod.TRecord.$new();',
  9228. ' return Result;',
  9229. '};',
  9230. 'this.DoIt = function (vG, vH) {',
  9231. '};',
  9232. '']),
  9233. LinesToStr([
  9234. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  9235. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  9236. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  9237. '']));
  9238. end;
  9239. procedure TTestModule.TestRecordElementFromWith_AsParams;
  9240. begin
  9241. StartProgram(false);
  9242. Add('type');
  9243. Add(' integer = longint;');
  9244. Add(' TRecord = record');
  9245. Add(' i: integer;');
  9246. Add(' end;');
  9247. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  9248. Add('begin');
  9249. Add('end;');
  9250. Add('var r: trecord;');
  9251. Add('begin');
  9252. Add(' with r do ');
  9253. Add(' doit(i,i,i);');
  9254. ConvertProgram;
  9255. CheckSource('TestRecordElementFromWith_AsParams',
  9256. LinesToStr([ // statements
  9257. 'rtl.recNewT($mod, "TRecord", function () {',
  9258. ' this.i = 0;',
  9259. ' this.$eq = function (b) {',
  9260. ' return this.i === b.i;',
  9261. ' };',
  9262. ' this.$assign = function (s) {',
  9263. ' this.i = s.i;',
  9264. ' return this;',
  9265. ' };',
  9266. '});',
  9267. 'this.DoIt = function (vG,vH,vI) {',
  9268. '};',
  9269. 'this.r = $mod.TRecord.$new();'
  9270. ]),
  9271. LinesToStr([
  9272. 'var $with1 = $mod.r;',
  9273. '$mod.DoIt($with1.i,$with1.i,{',
  9274. ' p: $with1,',
  9275. ' get: function () {',
  9276. ' return this.p.i;',
  9277. ' },',
  9278. ' set: function (v) {',
  9279. ' this.p.i = v;',
  9280. ' }',
  9281. '});',
  9282. '']));
  9283. end;
  9284. procedure TTestModule.TestRecord_Equal;
  9285. begin
  9286. StartProgram(false);
  9287. Add('type');
  9288. Add(' integer = longint;');
  9289. Add(' TFlag = (red,blue);');
  9290. Add(' TFlags = set of TFlag;');
  9291. Add(' TProc = procedure;');
  9292. Add(' TRecord = record');
  9293. Add(' i: integer;');
  9294. Add(' Event: TProc;');
  9295. Add(' f: TFlags;');
  9296. Add(' end;');
  9297. Add(' TNested = record');
  9298. Add(' r: TRecord;');
  9299. Add(' end;');
  9300. Add('var');
  9301. Add(' b: boolean;');
  9302. Add(' r,s: trecord;');
  9303. Add('begin');
  9304. Add(' b:=r=s;');
  9305. Add(' b:=r<>s;');
  9306. ConvertProgram;
  9307. CheckSource('TestRecord_Equal',
  9308. LinesToStr([ // statements
  9309. 'this.TFlag = {',
  9310. ' "0": "red",',
  9311. ' red: 0,',
  9312. ' "1": "blue",',
  9313. ' blue: 1',
  9314. '};',
  9315. 'rtl.recNewT($mod, "TRecord", function () {',
  9316. ' this.i = 0;',
  9317. ' this.Event = null;',
  9318. ' this.$new = function () {',
  9319. ' var r = Object.create(this);',
  9320. ' r.f = {};',
  9321. ' return r;',
  9322. ' };',
  9323. ' this.$eq = function (b) {',
  9324. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  9325. ' };',
  9326. ' this.$assign = function (s) {',
  9327. ' this.i = s.i;',
  9328. ' this.Event = s.Event;',
  9329. ' this.f = rtl.refSet(s.f);',
  9330. ' return this;',
  9331. ' };',
  9332. '});',
  9333. 'rtl.recNewT($mod, "TNested", function () {',
  9334. ' this.$new = function () {',
  9335. ' var r = Object.create(this);',
  9336. ' r.r = $mod.TRecord.$new();',
  9337. ' return r;',
  9338. ' };',
  9339. ' this.$eq = function (b) {',
  9340. ' return this.r.$eq(b.r);',
  9341. ' };',
  9342. ' this.$assign = function (s) {',
  9343. ' this.r.$assign(s.r);',
  9344. ' return this;',
  9345. ' };',
  9346. '});',
  9347. 'this.b = false;',
  9348. 'this.r = $mod.TRecord.$new();',
  9349. 'this.s = $mod.TRecord.$new();',
  9350. '']),
  9351. LinesToStr([
  9352. '$mod.b = $mod.r.$eq($mod.s);',
  9353. '$mod.b = !$mod.r.$eq($mod.s);',
  9354. '']));
  9355. end;
  9356. procedure TTestModule.TestRecord_TypeCastJSValueToRecord;
  9357. begin
  9358. StartProgram(false);
  9359. Add('type');
  9360. Add(' TRecord = record');
  9361. Add(' i: longint;');
  9362. Add(' end;');
  9363. Add('var');
  9364. Add(' Jv: jsvalue;');
  9365. Add(' Rec: trecord;');
  9366. Add('begin');
  9367. Add(' rec:=trecord(jv);');
  9368. ConvertProgram;
  9369. CheckSource('TestRecord_TypeCastJSValueToRecord',
  9370. LinesToStr([ // statements
  9371. 'rtl.recNewT($mod, "TRecord", function () {',
  9372. ' this.i = 0;',
  9373. ' this.$eq = function (b) {',
  9374. ' return this.i === b.i;',
  9375. ' };',
  9376. ' this.$assign = function (s) {',
  9377. ' this.i = s.i;',
  9378. ' return this;',
  9379. ' };',
  9380. '});',
  9381. 'this.Jv = undefined;',
  9382. 'this.Rec = $mod.TRecord.$new();',
  9383. '']),
  9384. LinesToStr([
  9385. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  9386. '']));
  9387. end;
  9388. procedure TTestModule.TestRecord_VariantFail;
  9389. begin
  9390. StartProgram(false);
  9391. Add([
  9392. 'type',
  9393. ' TRec = record',
  9394. ' case word of',
  9395. ' 0: (b0, b1: Byte);',
  9396. ' 1: (i: word);',
  9397. ' end;',
  9398. 'begin']);
  9399. SetExpectedPasResolverError('variant record is not supported',
  9400. nXIsNotSupported);
  9401. ConvertProgram;
  9402. end;
  9403. procedure TTestModule.TestRecord_FieldArray;
  9404. begin
  9405. StartProgram(false);
  9406. Add([
  9407. 'type',
  9408. ' TArrInt = array[3..4] of longint;',
  9409. ' TArrArrInt = array[3..4] of longint;',
  9410. ' TRec = record',
  9411. ' a: array of longint;',
  9412. ' s: array[1..2] of longint;',
  9413. ' m: array[1..2,3..4] of longint;',
  9414. ' o: TArrArrInt;',
  9415. ' end;',
  9416. 'begin']);
  9417. ConvertProgram;
  9418. CheckSource('TestRecord_FieldArray',
  9419. LinesToStr([ // statements
  9420. 'rtl.recNewT($mod, "TRec", function () {',
  9421. ' this.a = [];',
  9422. ' this.$new = function () {',
  9423. ' var r = Object.create(this);',
  9424. ' r.s = rtl.arraySetLength(null, 0, 2);',
  9425. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  9426. ' r.o = rtl.arraySetLength(null, 0, 2);',
  9427. ' return r;',
  9428. ' };',
  9429. ' this.$eq = function (b) {',
  9430. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  9431. ' };',
  9432. ' this.$assign = function (s) {',
  9433. ' this.a = s.a;',
  9434. ' this.s = s.s.slice(0);',
  9435. ' this.m = s.m.slice(0);',
  9436. ' this.o = s.o.slice(0);',
  9437. ' return this;',
  9438. ' };',
  9439. '});',
  9440. '']),
  9441. LinesToStr([ // $mod.$main
  9442. '']));
  9443. end;
  9444. procedure TTestModule.TestRecord_Const;
  9445. begin
  9446. StartProgram(false);
  9447. Add([
  9448. 'type',
  9449. ' TArrInt = array[3..4] of longint;',
  9450. ' TPoint = record x,y: longint; end;',
  9451. ' TRec = record',
  9452. ' i: longint;',
  9453. ' a: array of longint;',
  9454. ' s: array[1..2] of longint;',
  9455. ' m: array[1..2,3..4] of longint;',
  9456. ' p: TPoint;',
  9457. ' end;',
  9458. ' TPoints = array of TPoint;',
  9459. 'const',
  9460. ' r: TRec = (',
  9461. ' i:1;',
  9462. ' a:(2,3);',
  9463. ' s:(4,5);',
  9464. ' m:( (11,12), (13,14) );',
  9465. ' p: (x:21; y:22)',
  9466. ' );',
  9467. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  9468. 'begin']);
  9469. ConvertProgram;
  9470. CheckSource('TestRecord_Const',
  9471. LinesToStr([ // statements
  9472. 'rtl.recNewT($mod, "TPoint", function () {',
  9473. ' this.x = 0;',
  9474. ' this.y = 0;',
  9475. ' this.$eq = function (b) {',
  9476. ' return (this.x === b.x) && (this.y === b.y);',
  9477. ' };',
  9478. ' this.$assign = function (s) {',
  9479. ' this.x = s.x;',
  9480. ' this.y = s.y;',
  9481. ' return this;',
  9482. ' };',
  9483. '});',
  9484. 'rtl.recNewT($mod, "TRec", function () {',
  9485. ' this.i = 0;',
  9486. ' this.a = [];',
  9487. ' this.$new = function () {',
  9488. ' var r = Object.create(this);',
  9489. ' r.s = rtl.arraySetLength(null, 0, 2);',
  9490. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  9491. ' r.p = $mod.TPoint.$new();',
  9492. ' return r;',
  9493. ' };',
  9494. ' this.$eq = function (b) {',
  9495. ' 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);',
  9496. ' };',
  9497. ' this.$assign = function (s) {',
  9498. ' this.i = s.i;',
  9499. ' this.a = s.a;',
  9500. ' this.s = s.s.slice(0);',
  9501. ' this.m = s.m.slice(0);',
  9502. ' this.p.$assign(s.p);',
  9503. ' return this;',
  9504. ' };',
  9505. '});',
  9506. 'this.r = $mod.TRec.$clone({',
  9507. ' i: 1,',
  9508. ' a: [2, 3],',
  9509. ' s: [4, 5],',
  9510. ' m: [[11, 12], [13, 14]],',
  9511. ' p: $mod.TPoint.$clone({',
  9512. ' x: 21,',
  9513. ' y: 22',
  9514. ' })',
  9515. '});',
  9516. 'this.p = [$mod.TPoint.$clone({',
  9517. ' x: 1,',
  9518. ' y: 2',
  9519. '}), $mod.TPoint.$clone({',
  9520. ' x: 3,',
  9521. ' y: 4',
  9522. '})];',
  9523. '']),
  9524. LinesToStr([ // $mod.$main
  9525. '']));
  9526. end;
  9527. procedure TTestModule.TestRecord_TypecastFail;
  9528. begin
  9529. StartProgram(false);
  9530. Add([
  9531. 'type',
  9532. ' TPoint = record x,y: longint; end;',
  9533. ' TRec = record l: longint end;',
  9534. 'var p: TPoint;',
  9535. 'begin',
  9536. ' if TRec(p).l=2 then ;']);
  9537. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  9538. nIllegalTypeConversionTo);
  9539. ConvertProgram;
  9540. end;
  9541. procedure TTestModule.TestRecord_InFunction;
  9542. begin
  9543. StartProgram(false);
  9544. Add([
  9545. 'var TPoint: longint = 3;',
  9546. 'procedure DoIt;',
  9547. 'type',
  9548. ' TPoint = record x,y: longint; end;',
  9549. ' TPoints = array of TPoint;',
  9550. 'var',
  9551. ' r: TPoint;',
  9552. ' p: TPoints;',
  9553. 'begin',
  9554. ' SetLength(p,2);',
  9555. 'end;',
  9556. 'begin']);
  9557. ConvertProgram;
  9558. CheckSource('TestRecord_InFunction',
  9559. LinesToStr([ // statements
  9560. 'this.TPoint = 3;',
  9561. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  9562. ' this.x = 0;',
  9563. ' this.y = 0;',
  9564. ' this.$eq = function (b) {',
  9565. ' return (this.x === b.x) && (this.y === b.y);',
  9566. ' };',
  9567. ' this.$assign = function (s) {',
  9568. ' this.x = s.x;',
  9569. ' this.y = s.y;',
  9570. ' return this;',
  9571. ' };',
  9572. '});',
  9573. 'this.DoIt = function () {',
  9574. ' var r = TPoint$1.$new();',
  9575. ' var p = [];',
  9576. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  9577. '};',
  9578. '']),
  9579. LinesToStr([ // $mod.$main
  9580. '']));
  9581. end;
  9582. procedure TTestModule.TestAdvRecord_Function;
  9583. begin
  9584. StartProgram(false);
  9585. Parser.Options:=Parser.Options+[po_cassignments];
  9586. Add([
  9587. '{$modeswitch AdvancedRecords}',
  9588. 'type',
  9589. ' TPoint = record',
  9590. ' x,y: word;',
  9591. ' function Add(const apt: TPoint): TPoint;',
  9592. ' end;',
  9593. 'function TPoint.Add(const apt: TPoint): TPoint;',
  9594. 'begin',
  9595. ' Result:=Self;',
  9596. ' Result.x+=apt.x;',
  9597. ' Result.y:=Result.y+apt.y;',
  9598. ' Self:=apt;',
  9599. 'end;',
  9600. 'var p,q: TPoint;',
  9601. 'begin',
  9602. ' p.add(q);',
  9603. ' p:=default(TPoint);',
  9604. ' p:=q;',
  9605. '']);
  9606. ConvertProgram;
  9607. CheckSource('TestAdvRecord_Function',
  9608. LinesToStr([ // statements
  9609. 'rtl.recNewT($mod, "TPoint", function () {',
  9610. ' this.x = 0;',
  9611. ' this.y = 0;',
  9612. ' this.$eq = function (b) {',
  9613. ' return (this.x === b.x) && (this.y === b.y);',
  9614. ' };',
  9615. ' this.$assign = function (s) {',
  9616. ' this.x = s.x;',
  9617. ' this.y = s.y;',
  9618. ' return this;',
  9619. ' };',
  9620. ' this.Add = function (apt) {',
  9621. ' var Result = $mod.TPoint.$new();',
  9622. ' Result.$assign(this);',
  9623. ' Result.x += apt.x;',
  9624. ' Result.y = Result.y + apt.y;',
  9625. ' this.$assign(apt);',
  9626. ' return Result;',
  9627. ' };',
  9628. '});',
  9629. 'this.p = $mod.TPoint.$new();',
  9630. 'this.q = $mod.TPoint.$new();',
  9631. '']),
  9632. LinesToStr([ // $mod.$main
  9633. '$mod.p.Add($mod.q);',
  9634. '$mod.p.$assign($mod.TPoint.$new());',
  9635. '$mod.p.$assign($mod.q);',
  9636. '']));
  9637. end;
  9638. procedure TTestModule.TestAdvRecord_Property;
  9639. begin
  9640. StartProgram(false);
  9641. Add([
  9642. '{$modeswitch AdvancedRecords}',
  9643. 'type',
  9644. ' TPoint = record',
  9645. ' x,y: word;',
  9646. ' strict private',
  9647. ' function GetSize: longword;',
  9648. ' procedure SetSize(Value: longword);',
  9649. ' public',
  9650. ' property Size: longword read GetSize write SetSize;',
  9651. ' property Left: word read x write y;',
  9652. ' end;',
  9653. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  9654. 'function TPoint.GetSize: longword;',
  9655. 'begin',
  9656. ' x:=y;',
  9657. ' Size:=Size;',
  9658. ' Left:=Left;',
  9659. 'end;',
  9660. 'procedure TPoint.SetSize(Value: longword);',
  9661. 'begin',
  9662. 'end;',
  9663. 'var p,q: TPoint;',
  9664. 'begin',
  9665. ' p.Size:=q.Size;',
  9666. ' p.Left:=q.Left;',
  9667. '']);
  9668. ConvertProgram;
  9669. CheckSource('TestAdvRecord_Property',
  9670. LinesToStr([ // statements
  9671. 'rtl.recNewT($mod, "TPoint", function () {',
  9672. ' this.x = 0;',
  9673. ' this.y = 0;',
  9674. ' this.$eq = function (b) {',
  9675. ' return (this.x === b.x) && (this.y === b.y);',
  9676. ' };',
  9677. ' this.$assign = function (s) {',
  9678. ' this.x = s.x;',
  9679. ' this.y = s.y;',
  9680. ' return this;',
  9681. ' };',
  9682. ' this.GetSize = function () {',
  9683. ' var Result = 0;',
  9684. ' this.x = this.y;',
  9685. ' this.SetSize(this.GetSize());',
  9686. ' this.y = this.x;',
  9687. ' return Result;',
  9688. ' };',
  9689. ' this.SetSize = function (Value) {',
  9690. ' };',
  9691. '});',
  9692. 'this.SetSize = function (Value) {',
  9693. '};',
  9694. 'this.p = $mod.TPoint.$new();',
  9695. 'this.q = $mod.TPoint.$new();',
  9696. '']),
  9697. LinesToStr([ // $mod.$main
  9698. '$mod.p.SetSize($mod.q.GetSize());',
  9699. '$mod.p.y = $mod.q.x;',
  9700. '']));
  9701. end;
  9702. procedure TTestModule.TestAdvRecord_PropertyDefault;
  9703. begin
  9704. StartProgram(false);
  9705. Add([
  9706. '{$modeswitch AdvancedRecords}',
  9707. 'type',
  9708. ' TPoint = record',
  9709. ' strict private',
  9710. ' function GetItems(Index: word): word;',
  9711. ' procedure SetItems(Index: word; Value: word);',
  9712. ' public',
  9713. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  9714. ' end;',
  9715. 'function TPoint.GetItems(Index: word): word;',
  9716. 'begin',
  9717. ' Items[index]:=Items[index];',
  9718. ' self.Items[index]:=self.Items[index];',
  9719. 'end;',
  9720. 'procedure TPoint.SetItems(Index: word; Value: word);',
  9721. 'begin',
  9722. 'end;',
  9723. 'var p: TPoint;',
  9724. 'begin',
  9725. ' p[1]:=p[2];',
  9726. ' p.Items[3]:=p.Items[4];',
  9727. '']);
  9728. ConvertProgram;
  9729. CheckSource('TestAdvRecord_PropertyDefault',
  9730. LinesToStr([ // statements
  9731. 'rtl.recNewT($mod, "TPoint", function () {',
  9732. ' this.$eq = function (b) {',
  9733. ' return true;',
  9734. ' };',
  9735. ' this.$assign = function (s) {',
  9736. ' return this;',
  9737. ' };',
  9738. ' this.GetItems = function (Index) {',
  9739. ' var Result = 0;',
  9740. ' this.SetItems(Index, this.GetItems(Index));',
  9741. ' this.SetItems(Index, this.GetItems(Index));',
  9742. ' return Result;',
  9743. ' };',
  9744. ' this.SetItems = function (Index, Value) {',
  9745. ' };',
  9746. '});',
  9747. 'this.p = $mod.TPoint.$new();',
  9748. '']),
  9749. LinesToStr([ // $mod.$main
  9750. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  9751. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  9752. '']));
  9753. end;
  9754. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  9755. begin
  9756. StartProgram(false);
  9757. Add([
  9758. '{$modeswitch AdvancedRecords}',
  9759. 'type',
  9760. ' TRec = record',
  9761. ' class var Fx: longint;',
  9762. ' class var Fy: longint;',
  9763. ' class function GetInt: longint; static;',
  9764. ' class procedure SetInt(Value: longint); static;',
  9765. ' class procedure DoIt; static;',
  9766. ' class property IntA: longint read Fx write Fy;',
  9767. ' class property IntB: longint read GetInt write SetInt;',
  9768. ' end;',
  9769. 'class function trec.getint: longint;',
  9770. 'begin',
  9771. ' result:=fx;',
  9772. 'end;',
  9773. 'class procedure trec.setint(value: longint);',
  9774. 'begin',
  9775. 'end;',
  9776. 'class procedure trec.doit;',
  9777. 'begin',
  9778. ' IntA:=IntA+1;',
  9779. ' IntB:=IntB+1;',
  9780. 'end;',
  9781. 'var r: trec;',
  9782. 'begin',
  9783. ' trec.inta:=trec.inta+1;',
  9784. ' if trec.intb=2 then;',
  9785. ' trec.intb:=trec.intb+2;',
  9786. ' trec.setint(trec.inta);',
  9787. ' r.inta:=r.inta+1;',
  9788. ' if r.intb=2 then;',
  9789. ' r.intb:=r.intb+2;',
  9790. ' r.setint(r.inta);']);
  9791. ConvertProgram;
  9792. CheckSource('TestAdvRecord_Property_ClassMethod',
  9793. LinesToStr([ // statements
  9794. 'rtl.recNewT($mod, "TRec", function () {',
  9795. ' this.Fx = 0;',
  9796. ' this.Fy = 0;',
  9797. ' this.$eq = function (b) {',
  9798. ' return true;',
  9799. ' };',
  9800. ' this.$assign = function (s) {',
  9801. ' return this;',
  9802. ' };',
  9803. ' this.GetInt = function () {',
  9804. ' var Result = 0;',
  9805. ' Result = this.Fx;',
  9806. ' return Result;',
  9807. ' };',
  9808. ' this.SetInt = function (Value) {',
  9809. ' };',
  9810. ' this.DoIt = function () {',
  9811. ' this.Fy = this.Fx + 1;',
  9812. ' this.SetInt(this.GetInt() + 1);',
  9813. ' };',
  9814. '}, true);',
  9815. 'this.r = $mod.TRec.$new();',
  9816. '']),
  9817. LinesToStr([ // $mod.$main
  9818. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  9819. 'if ($mod.TRec.GetInt() === 2) ;',
  9820. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  9821. '$mod.TRec.SetInt($mod.TRec.Fx);',
  9822. '$mod.r.$record.Fy = $mod.r.Fx + 1;',
  9823. 'if ($mod.r.$record.GetInt() === 2) ;',
  9824. '$mod.r.$record.SetInt($mod.r.$record.GetInt() + 2);',
  9825. '$mod.r.$record.SetInt($mod.r.Fx);',
  9826. '']));
  9827. end;
  9828. procedure TTestModule.TestAdvRecord_Const;
  9829. begin
  9830. StartProgram(false);
  9831. Add([
  9832. '{$modeswitch AdvancedRecords}',
  9833. 'type',
  9834. ' TArrInt = array[3..4] of longint;',
  9835. ' TPoint = record',
  9836. ' x,y: longint;',
  9837. ' class var Count: nativeint;',
  9838. ' end;',
  9839. ' TRec = record',
  9840. ' i: longint;',
  9841. ' a: array of longint;',
  9842. ' s: array[1..2] of longint;',
  9843. ' m: array[1..2,3..4] of longint;',
  9844. ' p: TPoint;',
  9845. ' end;',
  9846. ' TPoints = array of TPoint;',
  9847. 'const',
  9848. ' r: TRec = (',
  9849. ' i:1;',
  9850. ' a:(2,3);',
  9851. ' s:(4,5);',
  9852. ' m:( (11,12), (13,14) );',
  9853. ' p: (x:21)',
  9854. ' );',
  9855. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  9856. 'begin']);
  9857. ConvertProgram;
  9858. CheckSource('TestAdvRecord_Const',
  9859. LinesToStr([ // statements
  9860. 'rtl.recNewT($mod, "TPoint", function () {',
  9861. ' this.x = 0;',
  9862. ' this.y = 0;',
  9863. ' this.Count = 0;',
  9864. ' this.$eq = function (b) {',
  9865. ' return (this.x === b.x) && (this.y === b.y);',
  9866. ' };',
  9867. ' this.$assign = function (s) {',
  9868. ' this.x = s.x;',
  9869. ' this.y = s.y;',
  9870. ' return this;',
  9871. ' };',
  9872. '}, true);',
  9873. 'rtl.recNewT($mod, "TRec", function () {',
  9874. ' this.i = 0;',
  9875. ' this.a = [];',
  9876. ' this.$new = function () {',
  9877. ' var r = Object.create(this);',
  9878. ' r.s = rtl.arraySetLength(null, 0, 2);',
  9879. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  9880. ' r.p = $mod.TPoint.$new();',
  9881. ' return r;',
  9882. ' };',
  9883. ' this.$eq = function (b) {',
  9884. ' 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);',
  9885. ' };',
  9886. ' this.$assign = function (s) {',
  9887. ' this.i = s.i;',
  9888. ' this.a = s.a;',
  9889. ' this.s = s.s.slice(0);',
  9890. ' this.m = s.m.slice(0);',
  9891. ' this.p.$assign(s.p);',
  9892. ' return this;',
  9893. ' };',
  9894. '});',
  9895. 'this.r = $mod.TRec.$clone({',
  9896. ' i: 1,',
  9897. ' a: [2, 3],',
  9898. ' s: [4, 5],',
  9899. ' m: [[11, 12], [13, 14]],',
  9900. ' p: $mod.TPoint.$clone({',
  9901. ' x: 21,',
  9902. ' y: 0',
  9903. ' })',
  9904. '});',
  9905. 'this.p = [$mod.TPoint.$clone({',
  9906. ' x: 1,',
  9907. ' y: 2',
  9908. '}), $mod.TPoint.$clone({',
  9909. ' x: 3,',
  9910. ' y: 4',
  9911. '})];',
  9912. '']),
  9913. LinesToStr([ // $mod.$main
  9914. '']));
  9915. end;
  9916. procedure TTestModule.TestAdvRecord_SubRecord;
  9917. begin
  9918. StartProgram(false);
  9919. Add([
  9920. '{$modeswitch AdvancedRecords}',
  9921. 'type',
  9922. ' TRec = record',
  9923. ' type',
  9924. ' TPoint = record',
  9925. ' x,y: longint;',
  9926. ' class var Count: nativeint;',
  9927. ' procedure DoIt;',
  9928. ' class procedure DoThat; static;',
  9929. ' end;',
  9930. ' var',
  9931. ' i: longint;',
  9932. ' p: TPoint;',
  9933. ' procedure DoSome;',
  9934. ' end;',
  9935. 'const',
  9936. ' r: TRec = (',
  9937. ' i:1;',
  9938. ' p: (x:21;y:22)',
  9939. ' );',
  9940. 'procedure TRec.DoSome;',
  9941. 'begin',
  9942. ' p.x:=p.y+1;',
  9943. ' p.Count:=p.Count+2;',
  9944. 'end;',
  9945. 'procedure TRec.TPoint.DoIt;',
  9946. 'begin',
  9947. ' Count:=Count+3;',
  9948. 'end;',
  9949. 'class procedure TRec.TPoint.DoThat;',
  9950. 'begin',
  9951. ' Count:=Count+4;',
  9952. 'end;',
  9953. 'begin']);
  9954. ConvertProgram;
  9955. CheckSource('TestAdvRecord_SubRecord',
  9956. LinesToStr([ // statements
  9957. 'rtl.recNewT($mod, "TRec", function () {',
  9958. ' rtl.recNewT(this, "TPoint", function () {',
  9959. ' this.x = 0;',
  9960. ' this.y = 0;',
  9961. ' this.Count = 0;',
  9962. ' this.$eq = function (b) {',
  9963. ' return (this.x === b.x) && (this.y === b.y);',
  9964. ' };',
  9965. ' this.$assign = function (s) {',
  9966. ' this.x = s.x;',
  9967. ' this.y = s.y;',
  9968. ' return this;',
  9969. ' };',
  9970. ' this.DoIt = function () {',
  9971. ' this.$record.Count = this.Count + 3;',
  9972. ' };',
  9973. ' this.DoThat = function () {',
  9974. ' this.Count = this.Count + 4;',
  9975. ' };',
  9976. ' }, true);',
  9977. ' this.i = 0;',
  9978. ' this.$new = function () {',
  9979. ' var r = Object.create(this);',
  9980. ' r.p = this.TPoint.$new();',
  9981. ' return r;',
  9982. ' };',
  9983. ' this.$eq = function (b) {',
  9984. ' return (this.i === b.i) && this.p.$eq(b.p);',
  9985. ' };',
  9986. ' this.$assign = function (s) {',
  9987. ' this.i = s.i;',
  9988. ' this.p.$assign(s.p);',
  9989. ' return this;',
  9990. ' };',
  9991. ' this.DoSome = function () {',
  9992. ' this.p.x = this.p.y + 1;',
  9993. ' this.p.$record.Count = this.p.Count + 2;',
  9994. ' };',
  9995. '}, true);',
  9996. 'this.r = $mod.TRec.$clone({',
  9997. ' i: 1,',
  9998. ' p: $mod.TRec.TPoint.$clone({',
  9999. ' x: 21,',
  10000. ' y: 22',
  10001. ' })',
  10002. '});',
  10003. '']),
  10004. LinesToStr([ // $mod.$main
  10005. '']));
  10006. end;
  10007. procedure TTestModule.TestAdvRecord_SubClass;
  10008. begin
  10009. StartProgram(false);
  10010. Add([
  10011. '{$modeswitch AdvancedRecords}',
  10012. 'type',
  10013. ' TObject = class end;',
  10014. ' TPoint = record',
  10015. ' type',
  10016. ' TBird = class',
  10017. ' procedure DoIt;',
  10018. ' class procedure Glob;',
  10019. ' end;',
  10020. ' procedure DoIt(b: TBird);',
  10021. ' end;',
  10022. 'procedure TPoint.TBird.DoIt;',
  10023. 'begin',
  10024. ' doit;',
  10025. ' self.doit;',
  10026. ' glob;',
  10027. ' self.glob;',
  10028. 'end;',
  10029. 'class procedure TPoint.TBird.Glob;',
  10030. 'begin',
  10031. ' glob;',
  10032. ' self.glob;',
  10033. 'end;',
  10034. 'procedure TPoint.DoIt(b: TBird);',
  10035. 'begin',
  10036. ' b.doit;',
  10037. ' b.glob;',
  10038. ' TBird.glob;',
  10039. 'end;',
  10040. 'begin',
  10041. '']);
  10042. ConvertProgram;
  10043. CheckSource('TestAdvRecord_SubClass',
  10044. LinesToStr([ // statements
  10045. 'rtl.createClass($mod, "TObject", null, function () {',
  10046. ' this.$init = function () {',
  10047. ' };',
  10048. ' this.$final = function () {',
  10049. ' };',
  10050. '});',
  10051. 'rtl.recNewT($mod, "TPoint", function () {',
  10052. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  10053. ' this.DoIt = function () {',
  10054. ' this.DoIt();',
  10055. ' this.DoIt();',
  10056. ' this.$class.Glob();',
  10057. ' this.$class.Glob();',
  10058. ' };',
  10059. ' this.Glob = function () {',
  10060. ' this.Glob();',
  10061. ' this.Glob();',
  10062. ' };',
  10063. ' });',
  10064. ' this.$eq = function (b) {',
  10065. ' return true;',
  10066. ' };',
  10067. ' this.$assign = function (s) {',
  10068. ' return this;',
  10069. ' };',
  10070. ' this.DoIt = function (b) {',
  10071. ' b.DoIt();',
  10072. ' b.$class.Glob();',
  10073. ' this.TBird.Glob();',
  10074. ' };',
  10075. '}, true);',
  10076. '']),
  10077. LinesToStr([ // $mod.$main
  10078. '']));
  10079. end;
  10080. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  10081. begin
  10082. StartProgram(false);
  10083. Add([
  10084. '{$modeswitch AdvancedRecords}',
  10085. 'type',
  10086. ' IUnknown = interface end;',
  10087. ' TPoint = record',
  10088. ' type IBird = interface end;',
  10089. ' end;',
  10090. 'begin',
  10091. '']);
  10092. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] interface inside record',
  10093. nNotYetImplemented);
  10094. ParseProgram;
  10095. end;
  10096. procedure TTestModule.TestAdvRecord_Constructor;
  10097. begin
  10098. StartProgram(false);
  10099. Add([
  10100. '{$modeswitch AdvancedRecords}',
  10101. 'type',
  10102. ' TPoint = record',
  10103. ' x,y: longint;',
  10104. ' constructor Create(ax: longint; ay: longint = -1);',
  10105. ' end;',
  10106. 'constructor tpoint.create(ax,ay: longint);',
  10107. 'begin',
  10108. ' x:=ax;',
  10109. ' self.y:=ay;',
  10110. 'end;',
  10111. 'var r: TPoint;',
  10112. 'begin',
  10113. ' r:=TPoint.Create(1,2);',
  10114. ' r.Create(3);',
  10115. ' r:=r.Create(4);',
  10116. '']);
  10117. ConvertProgram;
  10118. CheckSource('TestAdvRecord_Constructor',
  10119. LinesToStr([ // statements
  10120. 'rtl.recNewT($mod, "TPoint", function () {',
  10121. ' this.x = 0;',
  10122. ' this.y = 0;',
  10123. ' this.$eq = function (b) {',
  10124. ' return (this.x === b.x) && (this.y === b.y);',
  10125. ' };',
  10126. ' this.$assign = function (s) {',
  10127. ' this.x = s.x;',
  10128. ' this.y = s.y;',
  10129. ' return this;',
  10130. ' };',
  10131. ' this.Create = function (ax, ay) {',
  10132. ' this.x = ax;',
  10133. ' this.y = ay;',
  10134. ' return this;',
  10135. ' };',
  10136. '}, true);',
  10137. 'this.r = $mod.TPoint.$new();',
  10138. '']),
  10139. LinesToStr([ // $mod.$main
  10140. '$mod.r.$assign($mod.TPoint.$create("Create", [1, 2]));',
  10141. '$mod.r.Create(3, -1);',
  10142. '$mod.r.$assign($mod.r.Create(4, -1));',
  10143. '']));
  10144. end;
  10145. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  10146. begin
  10147. StartProgram(false);
  10148. Add(['type',
  10149. ' TObject = class',
  10150. ' public',
  10151. ' constructor Create;',
  10152. ' destructor Destroy;',
  10153. ' end;',
  10154. ' TBird = TObject;',
  10155. 'constructor tobject.create;',
  10156. 'begin end;',
  10157. 'destructor tobject.destroy;',
  10158. 'begin end;',
  10159. 'var Obj: tobject;',
  10160. 'begin',
  10161. ' obj:=tobject.create;',
  10162. ' obj:=tobject.create();',
  10163. ' obj:=tbird.create;',
  10164. ' obj:=tbird.create();',
  10165. ' obj:=obj.create();',
  10166. ' obj.destroy;',
  10167. '']);
  10168. ConvertProgram;
  10169. CheckSource('TestClass_TObjectDefaultConstructor',
  10170. LinesToStr([ // statements
  10171. 'rtl.createClass($mod,"TObject",null,function(){',
  10172. ' this.$init = function () {',
  10173. ' };',
  10174. ' this.$final = function () {',
  10175. ' };',
  10176. ' this.Create = function(){',
  10177. ' return this;',
  10178. ' };',
  10179. ' this.Destroy = function(){',
  10180. ' };',
  10181. '});',
  10182. 'this.Obj = null;'
  10183. ]),
  10184. LinesToStr([ // $mod.$main
  10185. '$mod.Obj = $mod.TObject.$create("Create");',
  10186. '$mod.Obj = $mod.TObject.$create("Create");',
  10187. '$mod.Obj = $mod.TObject.$create("Create");',
  10188. '$mod.Obj = $mod.TObject.$create("Create");',
  10189. '$mod.Obj = $mod.Obj.Create();',
  10190. '$mod.Obj.$destroy("Destroy");',
  10191. '']));
  10192. end;
  10193. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  10194. begin
  10195. StartProgram(false);
  10196. Add('type');
  10197. Add(' TObject = class');
  10198. Add(' public');
  10199. Add(' constructor Create(Par: longint);');
  10200. Add(' end;');
  10201. Add('constructor tobject.create(par: longint);');
  10202. Add('begin end;');
  10203. Add('var Obj: tobject;');
  10204. Add('begin');
  10205. Add(' obj:=tobject.create(3);');
  10206. ConvertProgram;
  10207. CheckSource('TestClass_TObjectConstructorWithParams',
  10208. LinesToStr([ // statements
  10209. 'rtl.createClass($mod,"TObject",null,function(){',
  10210. ' this.$init = function () {',
  10211. ' };',
  10212. ' this.$final = function () {',
  10213. ' };',
  10214. ' this.Create = function(Par){',
  10215. ' return this;',
  10216. ' };',
  10217. '});',
  10218. 'this.Obj = null;'
  10219. ]),
  10220. LinesToStr([ // $mod.$main
  10221. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  10222. ]));
  10223. end;
  10224. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  10225. begin
  10226. StartProgram(false);
  10227. Add('type');
  10228. Add(' TObject = class');
  10229. Add(' public');
  10230. Add(' constructor Create;');
  10231. Add(' end;');
  10232. Add(' TTest = class(TObject)');
  10233. Add(' public');
  10234. Add(' constructor Create(const Par: longint = 1);');
  10235. Add(' end;');
  10236. Add('constructor tobject.create;');
  10237. Add('begin end;');
  10238. Add('constructor ttest.create(const par: longint);');
  10239. Add('begin end;');
  10240. Add('var t: ttest;');
  10241. Add('begin');
  10242. Add(' t:=ttest.create;');
  10243. Add(' t:=ttest.create(2);');
  10244. ConvertProgram;
  10245. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  10246. LinesToStr([ // statements
  10247. 'rtl.createClass($mod,"TObject",null,function(){',
  10248. ' this.$init = function () {',
  10249. ' };',
  10250. ' this.$final = function () {',
  10251. ' };',
  10252. ' this.Create = function(){',
  10253. ' return this;',
  10254. ' };',
  10255. '});',
  10256. 'rtl.createClass($mod, "TTest", $mod.TObject, function () {',
  10257. ' this.Create$1 = function (Par) {',
  10258. ' return this;',
  10259. ' };',
  10260. '});',
  10261. 'this.t = null;'
  10262. ]),
  10263. LinesToStr([ // $mod.$main
  10264. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  10265. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  10266. ]));
  10267. end;
  10268. procedure TTestModule.TestClass_Var;
  10269. begin
  10270. StartProgram(false);
  10271. Add([
  10272. 'type',
  10273. ' TObject = class',
  10274. ' public',
  10275. ' vI: longint;',
  10276. ' constructor Create(Par: longint);',
  10277. ' end;',
  10278. 'constructor tobject.create(par: longint);',
  10279. 'begin',
  10280. ' vi:=par+3',
  10281. 'end;',
  10282. 'var Obj: tobject;',
  10283. 'begin',
  10284. ' obj:=tobject.create(4);',
  10285. ' obj.vi:=obj.VI+5;']);
  10286. ConvertProgram;
  10287. CheckSource('TestClass_Var',
  10288. LinesToStr([ // statements
  10289. 'rtl.createClass($mod,"TObject",null,function(){',
  10290. ' this.$init = function () {',
  10291. ' this.vI = 0;',
  10292. ' };',
  10293. ' this.$final = function () {',
  10294. ' };',
  10295. ' this.Create = function(Par){',
  10296. ' this.vI = Par+3;',
  10297. ' return this;',
  10298. ' };',
  10299. '});',
  10300. 'this.Obj = null;'
  10301. ]),
  10302. LinesToStr([ // $mod.$main
  10303. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  10304. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  10305. ]));
  10306. end;
  10307. procedure TTestModule.TestClass_Method;
  10308. begin
  10309. StartProgram(false);
  10310. Add('type');
  10311. Add(' TObject = class');
  10312. Add(' public');
  10313. Add(' vI: longint;');
  10314. Add(' Sub: TObject;');
  10315. Add(' constructor Create;');
  10316. Add(' function GetIt(Par: longint): tobject;');
  10317. Add(' end;');
  10318. Add('constructor tobject.create; begin end;');
  10319. Add('function tobject.getit(par: longint): tobject;');
  10320. Add('begin');
  10321. Add(' Self.vi:=par+3;');
  10322. Add(' Result:=self.sub;');
  10323. Add('end;');
  10324. Add('var Obj: tobject;');
  10325. Add('begin');
  10326. Add(' obj:=tobject.create;');
  10327. Add(' obj.getit(4);');
  10328. Add(' obj.sub.sub:=nil;');
  10329. Add(' obj.sub.getit(5);');
  10330. Add(' obj.sub.getit(6).SUB:=nil;');
  10331. Add(' obj.sub.getit(7).GETIT(8);');
  10332. Add(' obj.sub.getit(9).SuB.getit(10);');
  10333. ConvertProgram;
  10334. CheckSource('TestClass_Method',
  10335. LinesToStr([ // statements
  10336. 'rtl.createClass($mod,"TObject",null,function(){',
  10337. ' this.$init = function () {',
  10338. ' this.vI = 0;',
  10339. ' this.Sub = null;',
  10340. ' };',
  10341. ' this.$final = function () {',
  10342. ' this.Sub = undefined;',
  10343. ' };',
  10344. ' this.Create = function(){',
  10345. ' return this;',
  10346. ' };',
  10347. ' this.GetIt = function(Par){',
  10348. ' var Result = null;',
  10349. ' this.vI = Par + 3;',
  10350. ' Result = this.Sub;',
  10351. ' return Result;',
  10352. ' };',
  10353. '});',
  10354. 'this.Obj = null;'
  10355. ]),
  10356. LinesToStr([ // $mod.$main
  10357. '$mod.Obj = $mod.TObject.$create("Create");',
  10358. '$mod.Obj.GetIt(4);',
  10359. '$mod.Obj.Sub.Sub=null;',
  10360. '$mod.Obj.Sub.GetIt(5);',
  10361. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  10362. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  10363. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  10364. ]));
  10365. end;
  10366. procedure TTestModule.TestClass_Implementation;
  10367. begin
  10368. StartUnit(false);
  10369. Add([
  10370. 'interface',
  10371. 'type',
  10372. ' TObject = class',
  10373. ' constructor Create;',
  10374. ' end;',
  10375. 'implementation',
  10376. 'type',
  10377. ' TIntClass = class',
  10378. ' constructor Create; reintroduce;',
  10379. ' class procedure DoGlob;',
  10380. ' end;',
  10381. 'constructor tintclass.create;',
  10382. 'begin',
  10383. ' inherited;',
  10384. ' inherited create;',
  10385. ' doglob;',
  10386. 'end;',
  10387. 'class procedure tintclass.doglob;',
  10388. 'begin',
  10389. 'end;',
  10390. 'constructor tobject.create;',
  10391. 'var',
  10392. ' iC: tintclass;',
  10393. 'begin',
  10394. ' ic:=tintclass.create;',
  10395. ' tintclass.doglob;',
  10396. ' ic.doglob;',
  10397. 'end;',
  10398. 'initialization',
  10399. ' tintclass.doglob;',
  10400. '']);
  10401. ConvertUnit;
  10402. CheckSource('TestClass_Implementation',
  10403. LinesToStr([ // statements
  10404. 'var $impl = $mod.$impl;',
  10405. 'rtl.createClass($mod, "TObject", null, function () {',
  10406. ' this.$init = function () {',
  10407. ' };',
  10408. ' this.$final = function () {',
  10409. ' };',
  10410. ' this.Create = function () {',
  10411. ' var iC = null;',
  10412. ' iC = $impl.TIntClass.$create("Create$1");',
  10413. ' $impl.TIntClass.DoGlob();',
  10414. ' iC.$class.DoGlob();',
  10415. ' return this;',
  10416. ' };',
  10417. '});',
  10418. '']),
  10419. LinesToStr([ // $mod.$main
  10420. '$impl.TIntClass.DoGlob();',
  10421. '']),
  10422. LinesToStr([
  10423. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  10424. ' this.Create$1 = function () {',
  10425. ' $mod.TObject.Create.apply(this, arguments);',
  10426. ' $mod.TObject.Create.call(this);',
  10427. ' this.$class.DoGlob();',
  10428. ' return this;',
  10429. ' };',
  10430. ' this.DoGlob = function () {',
  10431. ' };',
  10432. '});',
  10433. '']));
  10434. end;
  10435. procedure TTestModule.TestClass_Inheritance;
  10436. begin
  10437. StartProgram(false);
  10438. Add('type');
  10439. Add(' TObject = class');
  10440. Add(' public');
  10441. Add(' constructor Create;');
  10442. Add(' end;');
  10443. Add(' TClassA = class');
  10444. Add(' end;');
  10445. Add(' TClassB = class(TObject)');
  10446. Add(' procedure ProcB;');
  10447. Add(' end;');
  10448. Add('constructor tobject.create; begin end;');
  10449. Add('procedure tclassb.procb; begin end;');
  10450. Add('var');
  10451. Add(' oO: TObject;');
  10452. Add(' oA: TClassA;');
  10453. Add(' oB: TClassB;');
  10454. Add('begin');
  10455. Add(' oO:=tobject.Create;');
  10456. Add(' oA:=tclassa.Create;');
  10457. Add(' ob:=tclassb.Create;');
  10458. Add(' if oo is tclassa then ;');
  10459. Add(' ob:=oo as tclassb;');
  10460. Add(' (oo as tclassb).procb;');
  10461. ConvertProgram;
  10462. CheckSource('TestClass_Inheritance',
  10463. LinesToStr([ // statements
  10464. 'rtl.createClass($mod,"TObject",null,function(){',
  10465. ' this.$init = function () {',
  10466. ' };',
  10467. ' this.$final = function () {',
  10468. ' };',
  10469. ' this.Create = function () {',
  10470. ' return this;',
  10471. ' };',
  10472. '});',
  10473. 'rtl.createClass($mod,"TClassA",$mod.TObject,function(){',
  10474. '});',
  10475. 'rtl.createClass($mod,"TClassB",$mod.TObject,function(){',
  10476. ' this.ProcB = function () {',
  10477. ' };',
  10478. '});',
  10479. 'this.oO = null;',
  10480. 'this.oA = null;',
  10481. 'this.oB = null;'
  10482. ]),
  10483. LinesToStr([ // $mod.$main
  10484. '$mod.oO = $mod.TObject.$create("Create");',
  10485. '$mod.oA = $mod.TClassA.$create("Create");',
  10486. '$mod.oB = $mod.TClassB.$create("Create");',
  10487. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  10488. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  10489. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  10490. ]));
  10491. end;
  10492. procedure TTestModule.TestClass_TypeAlias;
  10493. begin
  10494. StartProgram(false);
  10495. Add([
  10496. '{$interfaces corba}',
  10497. 'type',
  10498. ' IObject = interface',
  10499. ' end;',
  10500. ' IBird = type IObject;',
  10501. ' TObject = class',
  10502. ' end;',
  10503. ' TBird = type TObject;',
  10504. 'var',
  10505. ' oObj: TObject;',
  10506. ' oBird: TBird;',
  10507. ' IntfObj: IObject;',
  10508. ' IntfBird: IBird;',
  10509. 'begin',
  10510. ' oObj:=oBird;',
  10511. '']);
  10512. ConvertProgram;
  10513. CheckSource('TestClass_TypeAlias',
  10514. LinesToStr([ // statements
  10515. 'rtl.createInterface($mod, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  10516. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], $mod.IObject);',
  10517. 'rtl.createClass($mod, "TObject", null, function () {',
  10518. ' this.$init = function () {',
  10519. ' };',
  10520. ' this.$final = function () {',
  10521. ' };',
  10522. '});',
  10523. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  10524. '});',
  10525. 'this.oObj = null;',
  10526. 'this.oBird = null;',
  10527. 'this.IntfObj = null;',
  10528. 'this.IntfBird = null;',
  10529. '']),
  10530. LinesToStr([ // $mod.$main
  10531. '$mod.oObj = $mod.oBird;',
  10532. '']));
  10533. end;
  10534. procedure TTestModule.TestClass_AbstractMethod;
  10535. begin
  10536. StartProgram(false);
  10537. Add('type');
  10538. Add(' TObject = class');
  10539. Add(' public');
  10540. Add(' procedure DoIt; virtual; abstract;');
  10541. Add(' end;');
  10542. Add('begin');
  10543. ConvertProgram;
  10544. CheckSource('TestClass_AbstractMethod',
  10545. LinesToStr([ // statements
  10546. 'rtl.createClass($mod,"TObject",null,function(){',
  10547. ' this.$init = function () {',
  10548. ' };',
  10549. ' this.$final = function () {',
  10550. ' };',
  10551. '});'
  10552. ]),
  10553. LinesToStr([ // this.$main
  10554. ''
  10555. ]));
  10556. end;
  10557. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  10558. begin
  10559. StartProgram(false);
  10560. Add([
  10561. 'type',
  10562. ' TObject = class',
  10563. ' procedure DoAbstract; virtual; abstract;',
  10564. ' procedure DoVirtual; virtual;',
  10565. ' procedure DoIt;',
  10566. ' end;',
  10567. ' TA = class',
  10568. ' procedure doabstract; override;',
  10569. ' procedure dovirtual; override;',
  10570. ' procedure DoSome;',
  10571. ' end;',
  10572. 'procedure tobject.dovirtual;',
  10573. 'begin',
  10574. ' inherited; // call non existing ancestor -> ignore silently',
  10575. 'end;',
  10576. 'procedure tobject.doit;',
  10577. 'begin',
  10578. 'end;',
  10579. 'procedure ta.doabstract;',
  10580. 'begin',
  10581. ' inherited dovirtual; // call TObject.DoVirtual',
  10582. 'end;',
  10583. 'procedure ta.dovirtual;',
  10584. 'begin',
  10585. ' inherited; // call TObject.DoVirtual',
  10586. ' inherited dovirtual; // call TObject.DoVirtual',
  10587. ' inherited dovirtual(); // call TObject.DoVirtual',
  10588. ' doit;',
  10589. ' doit();',
  10590. 'end;',
  10591. 'procedure ta.dosome;',
  10592. 'begin',
  10593. ' inherited; // call non existing ancestor method -> silently ignore',
  10594. 'end;',
  10595. 'begin']);
  10596. ConvertProgram;
  10597. CheckSource('TestClass_CallInherited_ProcNoParams',
  10598. LinesToStr([ // statements
  10599. 'rtl.createClass($mod,"TObject",null,function(){',
  10600. ' this.$init = function () {',
  10601. ' };',
  10602. ' this.$final = function () {',
  10603. ' };',
  10604. ' this.DoVirtual = function () {',
  10605. ' };',
  10606. ' this.DoIt = function () {',
  10607. ' };',
  10608. '});',
  10609. 'rtl.createClass($mod, "TA", $mod.TObject, function () {',
  10610. ' this.DoAbstract = function () {',
  10611. ' $mod.TObject.DoVirtual.call(this);',
  10612. ' };',
  10613. ' this.DoVirtual = function () {',
  10614. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  10615. ' $mod.TObject.DoVirtual.call(this);',
  10616. ' $mod.TObject.DoVirtual.call(this);',
  10617. ' this.DoIt();',
  10618. ' this.DoIt();',
  10619. ' };',
  10620. ' this.DoSome = function () {',
  10621. ' };',
  10622. '});'
  10623. ]),
  10624. LinesToStr([ // this.$main
  10625. ''
  10626. ]));
  10627. end;
  10628. procedure TTestModule.TestClass_CallInherited_WithParams;
  10629. begin
  10630. StartProgram(false);
  10631. Add([
  10632. 'type',
  10633. ' TObject = class',
  10634. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  10635. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  10636. ' procedure DoIt(pA: longint; pB: longint = 0);',
  10637. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  10638. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  10639. ' end;',
  10640. ' TClassA = class',
  10641. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  10642. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  10643. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  10644. ' end;',
  10645. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  10646. 'begin',
  10647. 'end;',
  10648. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  10649. 'begin',
  10650. 'end;',
  10651. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  10652. 'begin',
  10653. 'end;',
  10654. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  10655. 'begin',
  10656. 'end;',
  10657. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  10658. 'begin',
  10659. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  10660. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  10661. 'end;',
  10662. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  10663. 'begin',
  10664. ' inherited; // call TObject.DoVirtual(pA,pB)',
  10665. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  10666. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  10667. ' doit(pa,pb);',
  10668. ' doit(pa);',
  10669. ' doit2(pa);',
  10670. ' doit2;',
  10671. 'end;',
  10672. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  10673. 'begin',
  10674. ' pa:=inherited;',
  10675. 'end;',
  10676. 'begin']);
  10677. ConvertProgram;
  10678. CheckSource('TestClass_CallInherited_WithParams',
  10679. LinesToStr([ // statements
  10680. 'rtl.createClass($mod,"TObject",null,function(){',
  10681. ' this.$init = function () {',
  10682. ' };',
  10683. ' this.$final = function () {',
  10684. ' };',
  10685. ' this.DoVirtual = function (pA,pB) {',
  10686. ' };',
  10687. ' this.DoIt = function (pA,pB) {',
  10688. ' };',
  10689. ' this.DoIt2 = function (pA,pB) {',
  10690. ' };',
  10691. ' this.GetIt = function (pA, pB) {',
  10692. ' var Result = 0;',
  10693. ' return Result;',
  10694. ' };',
  10695. '});',
  10696. 'rtl.createClass($mod, "TClassA", $mod.TObject, function () {',
  10697. ' this.DoAbstract = function (pA,pB) {',
  10698. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  10699. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  10700. ' };',
  10701. ' this.DoVirtual = function (pA,pB) {',
  10702. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  10703. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  10704. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  10705. ' this.DoIt(pA,pB);',
  10706. ' this.DoIt(pA,0);',
  10707. ' this.DoIt2(pA,2);',
  10708. ' this.DoIt2(1,2);',
  10709. ' };',
  10710. ' this.GetIt$1 = function (pA, pB) {',
  10711. ' var Result = 0;',
  10712. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  10713. ' return Result;',
  10714. ' };',
  10715. '});'
  10716. ]),
  10717. LinesToStr([ // this.$main
  10718. ''
  10719. ]));
  10720. end;
  10721. procedure TTestModule.TestClasS_CallInheritedConstructor;
  10722. begin
  10723. StartProgram(false);
  10724. Add('type');
  10725. Add(' TObject = class');
  10726. Add(' constructor Create; virtual;');
  10727. Add(' constructor CreateWithB(b: boolean);');
  10728. Add(' end;');
  10729. Add(' TA = class');
  10730. Add(' constructor Create; override;');
  10731. Add(' constructor CreateWithC(c: char);');
  10732. Add(' procedure DoIt;');
  10733. Add(' class function DoSome: TObject;');
  10734. Add(' end;');
  10735. Add('constructor tobject.create;');
  10736. Add('begin');
  10737. Add(' inherited; // call non existing ancestor -> ignore silently');
  10738. Add('end;');
  10739. Add('constructor tobject.createwithb(b: boolean);');
  10740. Add('begin');
  10741. Add(' inherited; // call non existing ancestor -> ignore silently');
  10742. Add(' create; // normal call');
  10743. Add('end;');
  10744. Add('constructor ta.create;');
  10745. Add('begin');
  10746. Add(' inherited; // normal call TObject.Create');
  10747. Add(' inherited create; // normal call TObject.Create');
  10748. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  10749. Add('end;');
  10750. Add('constructor ta.createwithc(c: char);');
  10751. Add('begin');
  10752. Add(' inherited create; // call TObject.Create');
  10753. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  10754. Add(' doit;');
  10755. Add(' doit();');
  10756. Add(' dosome;');
  10757. Add('end;');
  10758. Add('procedure ta.doit;');
  10759. Add('begin');
  10760. Add(' create; // normal call');
  10761. Add(' createwithb(false); // normal call');
  10762. Add(' createwithc(''c''); // normal call');
  10763. Add('end;');
  10764. Add('class function ta.dosome: TObject;');
  10765. Add('begin');
  10766. Add(' Result:=create; // constructor');
  10767. Add(' Result:=createwithb(true); // constructor');
  10768. Add(' Result:=createwithc(''c''); // constructor');
  10769. Add('end;');
  10770. Add('begin');
  10771. ConvertProgram;
  10772. CheckSource('TestClass_CallInheritedConstructor',
  10773. LinesToStr([ // statements
  10774. 'rtl.createClass($mod,"TObject",null,function(){',
  10775. ' this.$init = function () {',
  10776. ' };',
  10777. ' this.$final = function () {',
  10778. ' };',
  10779. ' this.Create = function () {',
  10780. ' return this;',
  10781. ' };',
  10782. ' this.CreateWithB = function (b) {',
  10783. ' this.Create();',
  10784. ' return this;',
  10785. ' };',
  10786. '});',
  10787. 'rtl.createClass($mod, "TA", $mod.TObject, function () {',
  10788. ' this.Create = function () {',
  10789. ' $mod.TObject.Create.apply(this, arguments);',
  10790. ' $mod.TObject.Create.call(this);',
  10791. ' $mod.TObject.CreateWithB.call(this, false);',
  10792. ' return this;',
  10793. ' };',
  10794. ' this.CreateWithC = function (c) {',
  10795. ' $mod.TObject.Create.call(this);',
  10796. ' $mod.TObject.CreateWithB.call(this, true);',
  10797. ' this.DoIt();',
  10798. ' this.DoIt();',
  10799. ' this.$class.DoSome();',
  10800. ' return this;',
  10801. ' };',
  10802. ' this.DoIt = function () {',
  10803. ' this.Create();',
  10804. ' this.CreateWithB(false);',
  10805. ' this.CreateWithC("c");',
  10806. ' };',
  10807. ' this.DoSome = function () {',
  10808. ' var Result = null;',
  10809. ' Result = this.$create("Create");',
  10810. ' Result = this.$create("CreateWithB", [true]);',
  10811. ' Result = this.$create("CreateWithC", ["c"]);',
  10812. ' return Result;',
  10813. ' };',
  10814. '});'
  10815. ]),
  10816. LinesToStr([ // this.$main
  10817. ''
  10818. ]));
  10819. end;
  10820. procedure TTestModule.TestClass_ClassVar_Assign;
  10821. begin
  10822. StartProgram(false);
  10823. Add([
  10824. 'type',
  10825. ' TObject = class',
  10826. ' public',
  10827. ' class var vI: longint;',
  10828. ' class var Sub: TObject;',
  10829. ' constructor Create;',
  10830. ' class function GetIt(Par: longint): tobject;',
  10831. ' end;',
  10832. 'constructor tobject.create;',
  10833. 'begin',
  10834. ' vi:=vi+1;',
  10835. ' Self.vi:=Self.vi+1;',
  10836. ' inc(vi);',
  10837. 'end;',
  10838. 'class function tobject.getit(par: longint): tobject;',
  10839. 'begin',
  10840. ' vi:=vi+par;',
  10841. ' Self.vi:=Self.vi+par;',
  10842. ' inc(vi);',
  10843. ' Result:=self.sub;',
  10844. 'end;',
  10845. 'var Obj: tobject;',
  10846. 'begin',
  10847. ' obj:=tobject.create;',
  10848. ' tobject.vi:=3;',
  10849. ' if tobject.vi=4 then ;',
  10850. ' tobject.sub:=nil;',
  10851. ' obj.sub:=nil;',
  10852. ' obj.sub.sub:=nil;']);
  10853. ConvertProgram;
  10854. CheckSource('TestClass_ClassVar_Assign',
  10855. LinesToStr([ // statements
  10856. 'rtl.createClass($mod,"TObject",null,function(){',
  10857. ' this.vI = 0;',
  10858. ' this.Sub = null;',
  10859. ' this.$init = function () {',
  10860. ' };',
  10861. ' this.$final = function () {',
  10862. ' };',
  10863. ' this.Create = function(){',
  10864. ' $mod.TObject.vI = this.vI+1;',
  10865. ' $mod.TObject.vI = this.vI+1;',
  10866. ' $mod.TObject.vI += 1;',
  10867. ' return this;',
  10868. ' };',
  10869. ' this.GetIt = function(Par){',
  10870. ' var Result = null;',
  10871. ' $mod.TObject.vI = this.vI + Par;',
  10872. ' $mod.TObject.vI = this.vI + Par;',
  10873. ' $mod.TObject.vI += 1;',
  10874. ' Result = this.Sub;',
  10875. ' return Result;',
  10876. ' };',
  10877. '});',
  10878. 'this.Obj = null;'
  10879. ]),
  10880. LinesToStr([ // $mod.$main
  10881. '$mod.Obj = $mod.TObject.$create("Create");',
  10882. '$mod.TObject.vI = 3;',
  10883. 'if ($mod.TObject.vI === 4);',
  10884. '$mod.TObject.Sub=null;',
  10885. '$mod.TObject.Sub=null;',
  10886. '$mod.TObject.Sub=null;',
  10887. '']));
  10888. end;
  10889. procedure TTestModule.TestClass_CallClassMethod;
  10890. begin
  10891. StartProgram(false);
  10892. Add('type');
  10893. Add(' TObject = class');
  10894. Add(' public');
  10895. Add(' class var vI: longint;');
  10896. Add(' class var Sub: TObject;');
  10897. Add(' constructor Create;');
  10898. Add(' function GetMore(Par: longint): longint;');
  10899. Add(' class function GetIt(Par: longint): tobject;');
  10900. Add(' end;');
  10901. Add('constructor tobject.create;');
  10902. Add('begin');
  10903. Add(' sub:=getit(3);');
  10904. Add(' vi:=getmore(4);');
  10905. Add(' sub:=Self.getit(5);');
  10906. Add(' vi:=Self.getmore(6);');
  10907. Add('end;');
  10908. Add('function tobject.getmore(par: longint): longint;');
  10909. Add('begin');
  10910. Add(' sub:=getit(11);');
  10911. Add(' vi:=getmore(12);');
  10912. Add(' sub:=self.getit(13);');
  10913. Add(' vi:=self.getmore(14);');
  10914. Add('end;');
  10915. Add('class function tobject.getit(par: longint): tobject;');
  10916. Add('begin');
  10917. Add(' sub:=getit(21);');
  10918. Add(' vi:=sub.getmore(22);');
  10919. Add(' sub:=self.getit(23);');
  10920. Add(' vi:=self.sub.getmore(24);');
  10921. Add('end;');
  10922. Add('var Obj: tobject;');
  10923. Add('begin');
  10924. Add(' obj:=tobject.create;');
  10925. Add(' tobject.getit(5);');
  10926. Add(' obj.getit(6);');
  10927. Add(' obj.sub.getit(7);');
  10928. Add(' obj.sub.getit(8).SUB:=nil;');
  10929. Add(' obj.sub.getit(9).GETIT(10);');
  10930. Add(' obj.sub.getit(11).SuB.getit(12);');
  10931. ConvertProgram;
  10932. CheckSource('TestClass_CallClassMethod',
  10933. LinesToStr([ // statements
  10934. 'rtl.createClass($mod,"TObject",null,function(){',
  10935. ' this.vI = 0;',
  10936. ' this.Sub = null;',
  10937. ' this.$init = function () {',
  10938. ' };',
  10939. ' this.$final = function () {',
  10940. ' };',
  10941. ' this.Create = function(){',
  10942. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  10943. ' $mod.TObject.vI = this.GetMore(4);',
  10944. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  10945. ' $mod.TObject.vI = this.GetMore(6);',
  10946. ' return this;',
  10947. ' };',
  10948. ' this.GetMore = function(Par){',
  10949. ' var Result = 0;',
  10950. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  10951. ' $mod.TObject.vI = this.GetMore(12);',
  10952. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  10953. ' $mod.TObject.vI = this.GetMore(14);',
  10954. ' return Result;',
  10955. ' };',
  10956. ' this.GetIt = function(Par){',
  10957. ' var Result = null;',
  10958. ' $mod.TObject.Sub = this.GetIt(21);',
  10959. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  10960. ' $mod.TObject.Sub = this.GetIt(23);',
  10961. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  10962. ' return Result;',
  10963. ' };',
  10964. '});',
  10965. 'this.Obj = null;'
  10966. ]),
  10967. LinesToStr([ // $mod.$main
  10968. '$mod.Obj = $mod.TObject.$create("Create");',
  10969. '$mod.TObject.GetIt(5);',
  10970. '$mod.Obj.$class.GetIt(6);',
  10971. '$mod.Obj.Sub.$class.GetIt(7);',
  10972. '$mod.TObject.Sub=null;',
  10973. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  10974. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  10975. '']));
  10976. end;
  10977. procedure TTestModule.TestClass_Property;
  10978. begin
  10979. StartProgram(false);
  10980. Add('type');
  10981. Add(' TObject = class');
  10982. Add(' Fx: longint;');
  10983. Add(' Fy: longint;');
  10984. Add(' function GetInt: longint;');
  10985. Add(' procedure SetInt(Value: longint);');
  10986. Add(' procedure DoIt;');
  10987. Add(' property IntA: longint read Fx write Fy;');
  10988. Add(' property IntB: longint read GetInt write SetInt;');
  10989. Add(' end;');
  10990. Add('function tobject.getint: longint;');
  10991. Add('begin');
  10992. Add(' result:=fx;');
  10993. Add('end;');
  10994. Add('procedure tobject.setint(value: longint);');
  10995. Add('begin');
  10996. Add(' if value=fy then exit;');
  10997. Add(' fy:=value;');
  10998. Add('end;');
  10999. Add('procedure tobject.doit;');
  11000. Add('begin');
  11001. Add(' IntA:=IntA+1;');
  11002. Add(' Self.IntA:=Self.IntA+1;');
  11003. Add(' IntB:=IntB+1;');
  11004. Add(' Self.IntB:=Self.IntB+1;');
  11005. Add('end;');
  11006. Add('var Obj: tobject;');
  11007. Add('begin');
  11008. Add(' obj.inta:=obj.inta+1;');
  11009. Add(' if obj.intb=2 then;');
  11010. Add(' obj.intb:=obj.intb+2;');
  11011. Add(' obj.setint(obj.inta);');
  11012. ConvertProgram;
  11013. CheckSource('TestClass_Property',
  11014. LinesToStr([ // statements
  11015. 'rtl.createClass($mod, "TObject", null, function () {',
  11016. ' this.$init = function () {',
  11017. ' this.Fx = 0;',
  11018. ' this.Fy = 0;',
  11019. ' };',
  11020. ' this.$final = function () {',
  11021. ' };',
  11022. ' this.GetInt = function () {',
  11023. ' var Result = 0;',
  11024. ' Result = this.Fx;',
  11025. ' return Result;',
  11026. ' };',
  11027. ' this.SetInt = function (Value) {',
  11028. ' if (Value === this.Fy) return;',
  11029. ' this.Fy = Value;',
  11030. ' };',
  11031. ' this.DoIt = function () {',
  11032. ' this.Fy = this.Fx + 1;',
  11033. ' this.Fy = this.Fx + 1;',
  11034. ' this.SetInt(this.GetInt() + 1);',
  11035. ' this.SetInt(this.GetInt() + 1);',
  11036. ' };',
  11037. '});',
  11038. 'this.Obj = null;'
  11039. ]),
  11040. LinesToStr([ // $mod.$main
  11041. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  11042. 'if ($mod.Obj.GetInt() === 2);',
  11043. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  11044. '$mod.Obj.SetInt($mod.Obj.Fx);'
  11045. ]));
  11046. end;
  11047. procedure TTestModule.TestClass_Property_ClassMethod;
  11048. begin
  11049. StartProgram(false);
  11050. Add([
  11051. 'type',
  11052. ' TObject = class',
  11053. ' class var Fx: longint;',
  11054. ' class var Fy: longint;',
  11055. ' class function GetInt: longint;',
  11056. ' class procedure SetInt(Value: longint);',
  11057. ' class procedure DoIt;',
  11058. ' class property IntA: longint read Fx write Fy;',
  11059. ' class property IntB: longint read GetInt write SetInt;',
  11060. ' end;',
  11061. 'class function tobject.getint: longint;',
  11062. 'begin',
  11063. ' result:=fx;',
  11064. 'end;',
  11065. 'class procedure tobject.setint(value: longint);',
  11066. 'begin',
  11067. 'end;',
  11068. 'class procedure tobject.doit;',
  11069. 'begin',
  11070. ' IntA:=IntA+1;',
  11071. ' Self.IntA:=Self.IntA+1;',
  11072. ' IntB:=IntB+1;',
  11073. ' Self.IntB:=Self.IntB+1;',
  11074. 'end;',
  11075. 'var Obj: tobject;',
  11076. 'begin',
  11077. ' tobject.inta:=tobject.inta+1;',
  11078. ' if tobject.intb=2 then;',
  11079. ' tobject.intb:=tobject.intb+2;',
  11080. ' tobject.setint(tobject.inta);',
  11081. ' obj.inta:=obj.inta+1;',
  11082. ' if obj.intb=2 then;',
  11083. ' obj.intb:=obj.intb+2;',
  11084. ' obj.setint(obj.inta);']);
  11085. ConvertProgram;
  11086. CheckSource('TestClass_Property_ClassMethod',
  11087. LinesToStr([ // statements
  11088. 'rtl.createClass($mod, "TObject", null, function () {',
  11089. ' this.Fx = 0;',
  11090. ' this.Fy = 0;',
  11091. ' this.$init = function () {',
  11092. ' };',
  11093. ' this.$final = function () {',
  11094. ' };',
  11095. ' this.GetInt = function () {',
  11096. ' var Result = 0;',
  11097. ' Result = this.Fx;',
  11098. ' return Result;',
  11099. ' };',
  11100. ' this.SetInt = function (Value) {',
  11101. ' };',
  11102. ' this.DoIt = function () {',
  11103. ' this.Fy = this.Fx + 1;',
  11104. ' this.Fy = this.Fx + 1;',
  11105. ' this.SetInt(this.GetInt() + 1);',
  11106. ' this.SetInt(this.GetInt() + 1);',
  11107. ' };',
  11108. '});',
  11109. 'this.Obj = null;'
  11110. ]),
  11111. LinesToStr([ // $mod.$main
  11112. '$mod.TObject.Fy = $mod.TObject.Fx + 1;',
  11113. 'if ($mod.TObject.GetInt() === 2);',
  11114. '$mod.TObject.SetInt($mod.TObject.GetInt() + 2);',
  11115. '$mod.TObject.SetInt($mod.TObject.Fx);',
  11116. '$mod.Obj.$class.Fy = $mod.Obj.Fx + 1;',
  11117. 'if ($mod.Obj.$class.GetInt() === 2);',
  11118. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  11119. '$mod.Obj.$class.SetInt($mod.Obj.Fx);'
  11120. ]));
  11121. end;
  11122. procedure TTestModule.TestClass_Property_Indexed;
  11123. begin
  11124. StartProgram(false);
  11125. Add('type');
  11126. Add(' TObject = class');
  11127. Add(' FItems: array of longint;');
  11128. Add(' function GetItems(Index: longint): longint;');
  11129. Add(' procedure SetItems(Index: longint; Value: longint);');
  11130. Add(' procedure DoIt;');
  11131. Add(' property Items[Index: longint]: longint read getitems write setitems;');
  11132. Add(' end;');
  11133. Add('function tobject.getitems(index: longint): longint;');
  11134. Add('begin');
  11135. Add(' Result:=fitems[index];');
  11136. Add('end;');
  11137. Add('procedure tobject.setitems(index: longint; value: longint);');
  11138. Add('begin');
  11139. Add(' fitems[index]:=value;');
  11140. Add('end;');
  11141. Add('procedure tobject.doit;');
  11142. Add('begin');
  11143. Add(' items[1]:=2;');
  11144. Add(' items[3]:=items[4];');
  11145. Add(' self.items[5]:=self.items[6];');
  11146. Add(' items[items[7]]:=items[items[8]];');
  11147. Add('end;');
  11148. Add('var Obj: tobject;');
  11149. Add('begin');
  11150. Add(' obj.Items[11]:=obj.Items[12];');
  11151. ConvertProgram;
  11152. CheckSource('TestClass_Property_Indexed',
  11153. LinesToStr([ // statements
  11154. 'rtl.createClass($mod, "TObject", null, function () {',
  11155. ' this.$init = function () {',
  11156. ' this.FItems = [];',
  11157. ' };',
  11158. ' this.$final = function () {',
  11159. ' this.FItems = undefined;',
  11160. ' };',
  11161. ' this.GetItems = function (Index) {',
  11162. ' var Result = 0;',
  11163. ' Result = this.FItems[Index];',
  11164. ' return Result;',
  11165. ' };',
  11166. ' this.SetItems = function (Index, Value) {',
  11167. ' this.FItems[Index] = Value;',
  11168. ' };',
  11169. ' this.DoIt = function () {',
  11170. ' this.SetItems(1, 2);',
  11171. ' this.SetItems(3,this.GetItems(4));',
  11172. ' this.SetItems(5,this.GetItems(6));',
  11173. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  11174. ' };',
  11175. '});',
  11176. 'this.Obj = null;'
  11177. ]),
  11178. LinesToStr([ // $mod.$main
  11179. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  11180. ]));
  11181. end;
  11182. procedure TTestModule.TestClass_Property_IndexSpec;
  11183. begin
  11184. StartProgram(false);
  11185. Add([
  11186. 'type',
  11187. ' TEnum = (red, blue);',
  11188. ' TObject = class',
  11189. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  11190. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  11191. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  11192. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  11193. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  11194. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  11195. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  11196. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  11197. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  11198. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  11199. ' end;',
  11200. 'procedure DoIt(b: boolean); begin end;',
  11201. 'var',
  11202. ' o: TObject;',
  11203. 'begin',
  11204. ' o.B1:=o.B1;',
  11205. ' o.B2:=o.B2;',
  11206. ' o.B3:=o.B3;',
  11207. ' o.I1[''a'']:=o.I1[''b''];',
  11208. ' doit(o.b1);',
  11209. ' doit(o.b2);',
  11210. ' doit(o.i1[''c'']);',
  11211. '']);
  11212. ConvertProgram;
  11213. CheckSource('TestClass_Property_IndexSpec',
  11214. LinesToStr([ // statements
  11215. 'this.TEnum = {',
  11216. ' "0": "red",',
  11217. ' red: 0,',
  11218. ' "1": "blue",',
  11219. ' blue: 1',
  11220. '};',
  11221. 'rtl.createClass($mod, "TObject", null, function () {',
  11222. ' this.$init = function () {',
  11223. ' };',
  11224. ' this.$final = function () {',
  11225. ' };',
  11226. '});',
  11227. 'this.DoIt = function (b) {',
  11228. '};',
  11229. 'this.o = null;',
  11230. '']),
  11231. LinesToStr([ // $mod.$main
  11232. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  11233. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  11234. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  11235. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  11236. '$mod.DoIt($mod.o.GetIntBool(1));',
  11237. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  11238. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  11239. '']));
  11240. end;
  11241. procedure TTestModule.TestClass_PropertyOfTypeArray;
  11242. begin
  11243. StartProgram(false);
  11244. Add('type');
  11245. Add(' TArray = array of longint;');
  11246. Add(' TObject = class');
  11247. Add(' FItems: TArray;');
  11248. Add(' function GetItems: tarray;');
  11249. Add(' procedure SetItems(Value: tarray);');
  11250. Add(' property Items: tarray read getitems write setitems;');
  11251. Add(' end;');
  11252. Add('function tobject.getitems: tarray;');
  11253. Add('begin');
  11254. Add(' Result:=fitems;');
  11255. Add('end;');
  11256. Add('procedure tobject.setitems(value: tarray);');
  11257. Add('begin');
  11258. Add(' fitems:=value;');
  11259. Add(' fitems:=nil;');
  11260. Add(' Items:=nil;');
  11261. Add(' Items:=Items;');
  11262. Add(' Items[1]:=2;');
  11263. Add(' fitems[3]:=Items[4];');
  11264. Add(' Items[5]:=Items[6];');
  11265. Add(' Self.Items[7]:=8;');
  11266. Add(' Self.Items[9]:=Self.Items[10];');
  11267. Add(' Items[Items[11]]:=Items[Items[12]];');
  11268. Add('end;');
  11269. Add('var Obj: tobject;');
  11270. Add('begin');
  11271. Add(' obj.items:=nil;');
  11272. Add(' obj.items:=obj.items;');
  11273. Add(' obj.items[11]:=obj.items[12];');
  11274. ConvertProgram;
  11275. CheckSource('TestClass_PropertyOfTypeArray',
  11276. LinesToStr([ // statements
  11277. 'rtl.createClass($mod, "TObject", null, function () {',
  11278. ' this.$init = function () {',
  11279. ' this.FItems = [];',
  11280. ' };',
  11281. ' this.$final = function () {',
  11282. ' this.FItems = undefined;',
  11283. ' };',
  11284. ' this.GetItems = function () {',
  11285. ' var Result = [];',
  11286. ' Result = this.FItems;',
  11287. ' return Result;',
  11288. ' };',
  11289. ' this.SetItems = function (Value) {',
  11290. ' this.FItems = Value;',
  11291. ' this.FItems = [];',
  11292. ' this.SetItems([]);',
  11293. ' this.SetItems(this.GetItems());',
  11294. ' this.GetItems()[1] = 2;',
  11295. ' this.FItems[3] = this.GetItems()[4];',
  11296. ' this.GetItems()[5] = this.GetItems()[6];',
  11297. ' this.GetItems()[7] = 8;',
  11298. ' this.GetItems()[9] = this.GetItems()[10];',
  11299. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  11300. ' };',
  11301. '});',
  11302. 'this.Obj = null;'
  11303. ]),
  11304. LinesToStr([ // $mod.$main
  11305. '$mod.Obj.SetItems([]);',
  11306. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  11307. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  11308. ]));
  11309. end;
  11310. procedure TTestModule.TestClass_PropertyDefault;
  11311. begin
  11312. StartProgram(false);
  11313. Add([
  11314. 'type',
  11315. ' TArray = array of longint;',
  11316. ' TObject = class',
  11317. ' FItems: TArray;',
  11318. ' function GetItems(Index: longint): longint;',
  11319. ' procedure SetItems(Index, Value: longint);',
  11320. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  11321. ' end;',
  11322. 'function tobject.getitems(index: longint): longint;',
  11323. 'begin',
  11324. 'end;',
  11325. 'procedure tobject.setitems(index, value: longint);',
  11326. 'begin',
  11327. ' Self[1]:=2;',
  11328. ' Self[3]:=Self[index];',
  11329. ' Self[index]:=Self[Self[value]];',
  11330. ' Self[Self[4]]:=value;',
  11331. 'end;',
  11332. 'var Obj: tobject;',
  11333. 'begin',
  11334. ' obj[11]:=12;',
  11335. ' obj[13]:=obj[14];',
  11336. ' obj[obj[15]]:=obj[obj[15]];',
  11337. ' TObject(obj)[16]:=TObject(obj)[17];']);
  11338. ConvertProgram;
  11339. CheckSource('TestClass_PropertyDefault',
  11340. LinesToStr([ // statements
  11341. 'rtl.createClass($mod, "TObject", null, function () {',
  11342. ' this.$init = function () {',
  11343. ' this.FItems = [];',
  11344. ' };',
  11345. ' this.$final = function () {',
  11346. ' this.FItems = undefined;',
  11347. ' };',
  11348. ' this.GetItems = function (Index) {',
  11349. ' var Result = 0;',
  11350. ' return Result;',
  11351. ' };',
  11352. ' this.SetItems = function (Index, Value) {',
  11353. ' this.SetItems(1, 2);',
  11354. ' this.SetItems(3, this.GetItems(Index));',
  11355. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  11356. ' this.SetItems(this.GetItems(4), Value);',
  11357. ' };',
  11358. '});',
  11359. 'this.Obj = null;'
  11360. ]),
  11361. LinesToStr([ // $mod.$main
  11362. '$mod.Obj.SetItems(11, 12);',
  11363. '$mod.Obj.SetItems(13, $mod.Obj.GetItems(14));',
  11364. '$mod.Obj.SetItems($mod.Obj.GetItems(15), $mod.Obj.GetItems($mod.Obj.GetItems(15)));',
  11365. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  11366. '']));
  11367. end;
  11368. procedure TTestModule.TestClass_PropertyDefault2;
  11369. begin
  11370. StartProgram(false);
  11371. Add([
  11372. 'type',
  11373. ' TObject = class end;',
  11374. ' TAlphaList = class',
  11375. ' function GetAlphas(Index: longint): Pointer; virtual; abstract;',
  11376. ' procedure SetAlphas(Index: longint; Value: Pointer); virtual; abstract;',
  11377. ' property Alphas[Index: longint]: Pointer read getAlphas write setAlphas; default;',
  11378. ' end;',
  11379. ' TBetaList = class',
  11380. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  11381. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  11382. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  11383. ' end;',
  11384. ' TBird = class',
  11385. ' procedure DoIt;',
  11386. ' end;',
  11387. 'procedure TBird.DoIt;',
  11388. 'var',
  11389. ' List: TAlphaList;',
  11390. 'begin',
  11391. ' if TBetaList(List[2])[3]=nil then ;',
  11392. ' TBetaList(List[4])[5]:=nil;',
  11393. 'end;',
  11394. 'var',
  11395. ' List: TAlphaList;',
  11396. 'begin',
  11397. ' if TBetaList(List[2])[3]=nil then ;',
  11398. ' TBetaList(List[4])[5]:=nil;',
  11399. '']);
  11400. ConvertProgram;
  11401. CheckSource('TestClass_PropertyDefault2',
  11402. LinesToStr([ // statements
  11403. 'rtl.createClass($mod, "TObject", null, function () {',
  11404. ' this.$init = function () {',
  11405. ' };',
  11406. ' this.$final = function () {',
  11407. ' };',
  11408. '});',
  11409. 'rtl.createClass($mod, "TAlphaList", $mod.TObject, function () {',
  11410. '});',
  11411. 'rtl.createClass($mod, "TBetaList", $mod.TObject, function () {',
  11412. '});',
  11413. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  11414. ' this.DoIt = function () {',
  11415. ' var List = null;',
  11416. ' if (List.GetAlphas(2).GetBetas(3) === null) ;',
  11417. ' List.GetAlphas(4).SetBetas(5, null);',
  11418. ' };',
  11419. '});',
  11420. 'this.List = null;',
  11421. '']),
  11422. LinesToStr([ // $mod.$main
  11423. 'if ($mod.List.GetAlphas(2).GetBetas(3) === null) ;',
  11424. '$mod.List.GetAlphas(4).SetBetas(5, null);',
  11425. '']));
  11426. end;
  11427. procedure TTestModule.TestClass_PropertyOverride;
  11428. begin
  11429. StartProgram(false);
  11430. Add('type');
  11431. Add(' integer = longint;');
  11432. Add(' TObject = class');
  11433. Add(' FItem: integer;');
  11434. Add(' function GetItem: integer; external name ''GetItem'';');
  11435. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  11436. Add(' property Item: integer read getitem write setitem;');
  11437. Add(' end;');
  11438. Add(' TCar = class');
  11439. Add(' FBag: integer;');
  11440. Add(' function GetBag: integer; external name ''GetBag'';');
  11441. Add(' property Item read getbag;');
  11442. Add(' end;');
  11443. Add('var');
  11444. Add(' Obj: tobject;');
  11445. Add(' Car: tcar;');
  11446. Add('begin');
  11447. Add(' Obj.Item:=Obj.Item;');
  11448. Add(' Car.Item:=Car.Item;');
  11449. ConvertProgram;
  11450. CheckSource('TestClass_PropertyOverride',
  11451. LinesToStr([ // statements
  11452. 'rtl.createClass($mod, "TObject", null, function () {',
  11453. ' this.$init = function () {',
  11454. ' this.FItem = 0;',
  11455. ' };',
  11456. ' this.$final = function () {',
  11457. ' };',
  11458. '});',
  11459. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  11460. ' this.$init = function () {',
  11461. ' $mod.TObject.$init.call(this);',
  11462. ' this.FBag = 0;',
  11463. ' };',
  11464. '});',
  11465. 'this.Obj = null;',
  11466. 'this.Car = null;',
  11467. '']),
  11468. LinesToStr([ // $mod.$main
  11469. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  11470. '$mod.Car.SetItem($mod.Car.GetBag());',
  11471. '']));
  11472. end;
  11473. procedure TTestModule.TestClass_PropertyIncVisibility;
  11474. begin
  11475. AddModuleWithIntfImplSrc('unit1.pp',
  11476. LinesToStr([
  11477. 'type',
  11478. ' TNumber = longint;',
  11479. ' TInteger = longint;',
  11480. ' TObject = class',
  11481. ' private',
  11482. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  11483. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  11484. ' protected',
  11485. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  11486. ' end;']),
  11487. LinesToStr([
  11488. '']));
  11489. StartProgram(true);
  11490. Add([
  11491. 'uses unit1;',
  11492. 'type',
  11493. ' TBird = class',
  11494. ' public',
  11495. ' property Items;',
  11496. ' end;',
  11497. 'procedure DoIt(i: TInteger);',
  11498. 'begin',
  11499. 'end;',
  11500. 'var b: TBird;',
  11501. 'begin',
  11502. ' b.Items[1]:=2;',
  11503. ' b.Items[3]:=b.Items[4];',
  11504. ' DoIt(b.Items[5]);',
  11505. '']);
  11506. ConvertProgram;
  11507. CheckSource('TestClass_PropertyIncVisibility',
  11508. LinesToStr([ // statements
  11509. 'rtl.createClass($mod, "TBird", pas.unit1.TObject, function () {',
  11510. '});',
  11511. 'this.DoIt = function (i) {',
  11512. '};',
  11513. 'this.b = null;'
  11514. ]),
  11515. LinesToStr([ // $mod.$main
  11516. '$mod.b.SetItems(1, 2);',
  11517. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  11518. '$mod.DoIt($mod.b.GetItems(5));'
  11519. ]));
  11520. end;
  11521. procedure TTestModule.TestClass_Assigned;
  11522. begin
  11523. StartProgram(false);
  11524. Add('type');
  11525. Add(' TObject = class');
  11526. Add(' end;');
  11527. Add('var');
  11528. Add(' Obj: tobject;');
  11529. Add(' b: boolean;');
  11530. Add('begin');
  11531. Add(' if Assigned(obj) then ;');
  11532. Add(' b:=Assigned(obj) or false;');
  11533. ConvertProgram;
  11534. CheckSource('TestClass_Assigned',
  11535. LinesToStr([ // statements
  11536. 'rtl.createClass($mod, "TObject", null, function () {',
  11537. ' this.$init = function () {',
  11538. ' };',
  11539. ' this.$final = function () {',
  11540. ' };',
  11541. '});',
  11542. 'this.Obj = null;',
  11543. 'this.b = false;'
  11544. ]),
  11545. LinesToStr([ // $mod.$main
  11546. 'if ($mod.Obj != null);',
  11547. '$mod.b = ($mod.Obj != null) || false;'
  11548. ]));
  11549. end;
  11550. procedure TTestModule.TestClass_WithClassDoCreate;
  11551. begin
  11552. StartProgram(false);
  11553. Add('type');
  11554. Add(' TObject = class');
  11555. Add(' aBool: boolean;');
  11556. Add(' Arr: array of boolean;');
  11557. Add(' constructor Create;');
  11558. Add(' end;');
  11559. Add('constructor TObject.Create; begin end;');
  11560. Add('var');
  11561. Add(' Obj: tobject;');
  11562. Add(' b: boolean;');
  11563. Add('begin');
  11564. Add(' with tobject.create do begin');
  11565. Add(' b:=abool;');
  11566. Add(' abool:=b;');
  11567. Add(' b:=arr[1];');
  11568. Add(' arr[2]:=b;');
  11569. Add(' end;');
  11570. Add(' with tobject do');
  11571. Add(' obj:=create;');
  11572. Add(' with obj do begin');
  11573. Add(' create;');
  11574. Add(' b:=abool;');
  11575. Add(' abool:=b;');
  11576. Add(' b:=arr[3];');
  11577. Add(' arr[4]:=b;');
  11578. Add(' end;');
  11579. ConvertProgram;
  11580. CheckSource('TestClass_WithClassDoCreate',
  11581. LinesToStr([ // statements
  11582. 'rtl.createClass($mod, "TObject", null, function () {',
  11583. ' this.$init = function () {',
  11584. ' this.aBool = false;',
  11585. ' this.Arr = [];',
  11586. ' };',
  11587. ' this.$final = function () {',
  11588. ' this.Arr = undefined;',
  11589. ' };',
  11590. ' this.Create = function () {',
  11591. ' return this;',
  11592. ' };',
  11593. '});',
  11594. 'this.Obj = null;',
  11595. 'this.b = false;'
  11596. ]),
  11597. LinesToStr([ // $mod.$main
  11598. 'var $with1 = $mod.TObject.$create("Create");',
  11599. '$mod.b = $with1.aBool;',
  11600. '$with1.aBool = $mod.b;',
  11601. '$mod.b = $with1.Arr[1];',
  11602. '$with1.Arr[2] = $mod.b;',
  11603. 'var $with2 = $mod.TObject;',
  11604. '$mod.Obj = $with2.$create("Create");',
  11605. 'var $with3 = $mod.Obj;',
  11606. '$with3.Create();',
  11607. '$mod.b = $with3.aBool;',
  11608. '$with3.aBool = $mod.b;',
  11609. '$mod.b = $with3.Arr[3];',
  11610. '$with3.Arr[4] = $mod.b;',
  11611. '']));
  11612. end;
  11613. procedure TTestModule.TestClass_WithClassInstDoProperty;
  11614. begin
  11615. StartProgram(false);
  11616. Add('type');
  11617. Add(' TObject = class');
  11618. Add(' FInt: longint;');
  11619. Add(' constructor Create;');
  11620. Add(' function GetSize: longint;');
  11621. Add(' procedure SetSize(Value: longint);');
  11622. Add(' property Int: longint read FInt write FInt;');
  11623. Add(' property Size: longint read GetSize write SetSize;');
  11624. Add(' end;');
  11625. Add('constructor TObject.Create; begin end;');
  11626. Add('function TObject.GetSize: longint; begin; end;');
  11627. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  11628. Add('var');
  11629. Add(' Obj: tobject;');
  11630. Add(' i: longint;');
  11631. Add('begin');
  11632. Add(' with TObject.Create do begin');
  11633. Add(' i:=int;');
  11634. Add(' int:=i;');
  11635. Add(' i:=size;');
  11636. Add(' size:=i;');
  11637. Add(' end;');
  11638. Add(' with obj do begin');
  11639. Add(' i:=int;');
  11640. Add(' int:=i;');
  11641. Add(' i:=size;');
  11642. Add(' size:=i;');
  11643. Add(' end;');
  11644. ConvertProgram;
  11645. CheckSource('TestClass_WithClassInstDoProperty',
  11646. LinesToStr([ // statements
  11647. 'rtl.createClass($mod, "TObject", null, function () {',
  11648. ' this.$init = function () {',
  11649. ' this.FInt = 0;',
  11650. ' };',
  11651. ' this.$final = function () {',
  11652. ' };',
  11653. ' this.Create = function () {',
  11654. ' return this;',
  11655. ' };',
  11656. ' this.GetSize = function () {',
  11657. ' var Result = 0;',
  11658. ' return Result;',
  11659. ' };',
  11660. ' this.SetSize = function (Value) {',
  11661. ' };',
  11662. '});',
  11663. 'this.Obj = null;',
  11664. 'this.i = 0;'
  11665. ]),
  11666. LinesToStr([ // $mod.$main
  11667. 'var $with1 = $mod.TObject.$create("Create");',
  11668. '$mod.i = $with1.FInt;',
  11669. '$with1.FInt = $mod.i;',
  11670. '$mod.i = $with1.GetSize();',
  11671. '$with1.SetSize($mod.i);',
  11672. 'var $with2 = $mod.Obj;',
  11673. '$mod.i = $with2.FInt;',
  11674. '$with2.FInt = $mod.i;',
  11675. '$mod.i = $with2.GetSize();',
  11676. '$with2.SetSize($mod.i);',
  11677. '']));
  11678. end;
  11679. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  11680. begin
  11681. StartProgram(false);
  11682. Add('type');
  11683. Add(' TObject = class');
  11684. Add(' constructor Create;');
  11685. Add(' function GetItems(Index: longint): longint;');
  11686. Add(' procedure SetItems(Index, Value: longint);');
  11687. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  11688. Add(' end;');
  11689. Add('constructor TObject.Create; begin end;');
  11690. Add('function tobject.getitems(index: longint): longint; begin; end;');
  11691. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  11692. Add('var');
  11693. Add(' Obj: tobject;');
  11694. Add(' i: longint;');
  11695. Add('begin');
  11696. Add(' with TObject.Create do begin');
  11697. Add(' i:=Items[1];');
  11698. Add(' Items[2]:=i;');
  11699. Add(' end;');
  11700. Add(' with obj do begin');
  11701. Add(' i:=Items[3];');
  11702. Add(' Items[4]:=i;');
  11703. Add(' end;');
  11704. ConvertProgram;
  11705. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  11706. LinesToStr([ // statements
  11707. 'rtl.createClass($mod, "TObject", null, function () {',
  11708. ' this.$init = function () {',
  11709. ' };',
  11710. ' this.$final = function () {',
  11711. ' };',
  11712. ' this.Create = function () {',
  11713. ' return this;',
  11714. ' };',
  11715. ' this.GetItems = function (Index) {',
  11716. ' var Result = 0;',
  11717. ' return Result;',
  11718. ' };',
  11719. ' this.SetItems = function (Index, Value) {',
  11720. ' };',
  11721. '});',
  11722. 'this.Obj = null;',
  11723. 'this.i = 0;'
  11724. ]),
  11725. LinesToStr([ // $mod.$main
  11726. 'var $with1 = $mod.TObject.$create("Create");',
  11727. '$mod.i = $with1.GetItems(1);',
  11728. '$with1.SetItems(2, $mod.i);',
  11729. 'var $with2 = $mod.Obj;',
  11730. '$mod.i = $with2.GetItems(3);',
  11731. '$with2.SetItems(4, $mod.i);',
  11732. '']));
  11733. end;
  11734. procedure TTestModule.TestClass_WithClassInstDoFunc;
  11735. begin
  11736. StartProgram(false);
  11737. Add('type');
  11738. Add(' TObject = class');
  11739. Add(' constructor Create;');
  11740. Add(' function GetSize: longint;');
  11741. Add(' procedure SetSize(Value: longint);');
  11742. Add(' end;');
  11743. Add('constructor TObject.Create; begin end;');
  11744. Add('function TObject.GetSize: longint; begin; end;');
  11745. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  11746. Add('var');
  11747. Add(' Obj: tobject;');
  11748. Add(' i: longint;');
  11749. Add('begin');
  11750. Add(' with TObject.Create do begin');
  11751. Add(' i:=GetSize;');
  11752. Add(' i:=GetSize();');
  11753. Add(' SetSize(i);');
  11754. Add(' end;');
  11755. Add(' with obj do begin');
  11756. Add(' i:=GetSize;');
  11757. Add(' i:=GetSize();');
  11758. Add(' SetSize(i);');
  11759. Add(' end;');
  11760. ConvertProgram;
  11761. CheckSource('TestClass_WithClassInstDoFunc',
  11762. LinesToStr([ // statements
  11763. 'rtl.createClass($mod, "TObject", null, function () {',
  11764. ' this.$init = function () {',
  11765. ' };',
  11766. ' this.$final = function () {',
  11767. ' };',
  11768. ' this.Create = function () {',
  11769. ' return this;',
  11770. ' };',
  11771. ' this.GetSize = function () {',
  11772. ' var Result = 0;',
  11773. ' return Result;',
  11774. ' };',
  11775. ' this.SetSize = function (Value) {',
  11776. ' };',
  11777. '});',
  11778. 'this.Obj = null;',
  11779. 'this.i = 0;'
  11780. ]),
  11781. LinesToStr([ // $mod.$main
  11782. 'var $with1 = $mod.TObject.$create("Create");',
  11783. '$mod.i = $with1.GetSize();',
  11784. '$mod.i = $with1.GetSize();',
  11785. '$with1.SetSize($mod.i);',
  11786. 'var $with2 = $mod.Obj;',
  11787. '$mod.i = $with2.GetSize();',
  11788. '$mod.i = $with2.GetSize();',
  11789. '$with2.SetSize($mod.i);',
  11790. '']));
  11791. end;
  11792. procedure TTestModule.TestClass_TypeCast;
  11793. begin
  11794. StartProgram(false);
  11795. Add('type');
  11796. Add(' TObject = class');
  11797. Add(' Next: TObject;');
  11798. Add(' constructor Create;');
  11799. Add(' end;');
  11800. Add(' TControl = class(TObject)');
  11801. Add(' Arr: array of TObject;');
  11802. Add(' function GetIt(vI: longint = 0): TObject;');
  11803. Add(' end;');
  11804. Add('constructor tobject.create; begin end;');
  11805. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  11806. Add('var');
  11807. Add(' Obj: tobject;');
  11808. Add('begin');
  11809. Add(' obj:=tcontrol(obj).next;');
  11810. Add(' tcontrol(obj):=nil;');
  11811. Add(' obj:=tcontrol(obj);');
  11812. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  11813. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  11814. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  11815. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  11816. ConvertProgram;
  11817. CheckSource('TestClass_TypeCast',
  11818. LinesToStr([ // statements
  11819. 'rtl.createClass($mod, "TObject", null, function () {',
  11820. ' this.$init = function () {',
  11821. ' this.Next = null;',
  11822. ' };',
  11823. ' this.$final = function () {',
  11824. ' this.Next = undefined;',
  11825. ' };',
  11826. ' this.Create = function () {',
  11827. ' return this;',
  11828. ' };',
  11829. '});',
  11830. 'rtl.createClass($mod, "TControl", $mod.TObject, function () {',
  11831. ' this.$init = function () {',
  11832. ' $mod.TObject.$init.call(this);',
  11833. ' this.Arr = [];',
  11834. ' };',
  11835. ' this.$final = function () {',
  11836. ' this.Arr = undefined;',
  11837. ' $mod.TObject.$final.call(this);',
  11838. ' };',
  11839. ' this.GetIt = function (vI) {',
  11840. ' var Result = null;',
  11841. ' return Result;',
  11842. ' };',
  11843. '});',
  11844. 'this.Obj = null;'
  11845. ]),
  11846. LinesToStr([ // $mod.$main
  11847. '$mod.Obj = $mod.Obj.Next;',
  11848. '$mod.Obj = null;',
  11849. '$mod.Obj = $mod.Obj;',
  11850. '$mod.Obj = $mod.Obj.GetIt(0);',
  11851. '$mod.Obj = $mod.Obj.GetIt(0);',
  11852. '$mod.Obj = $mod.Obj.GetIt(1);',
  11853. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  11854. '']));
  11855. end;
  11856. procedure TTestModule.TestClass_TypeCastUntypedParam;
  11857. begin
  11858. StartProgram(false);
  11859. Add('type');
  11860. Add(' TObject = class end;');
  11861. Add('procedure ProcA(var A);');
  11862. Add('begin');
  11863. Add(' TObject(A):=nil;');
  11864. Add(' TObject(A):=TObject(A);');
  11865. Add(' if TObject(A)=nil then ;');
  11866. Add(' if nil=TObject(A) then ;');
  11867. Add('end;');
  11868. Add('procedure ProcB(out A);');
  11869. Add('begin');
  11870. Add(' TObject(A):=nil;');
  11871. Add(' TObject(A):=TObject(A);');
  11872. Add(' if TObject(A)=nil then ;');
  11873. Add(' if nil=TObject(A) then ;');
  11874. Add('end;');
  11875. Add('procedure ProcC(const A);');
  11876. Add('begin');
  11877. Add(' if TObject(A)=nil then ;');
  11878. Add(' if nil=TObject(A) then ;');
  11879. Add('end;');
  11880. Add('var o: TObject;');
  11881. Add('begin');
  11882. Add(' ProcA(o);');
  11883. Add(' ProcB(o);');
  11884. Add(' ProcC(o);');
  11885. ConvertProgram;
  11886. CheckSource('TestClass_TypeCastUntypedParam',
  11887. LinesToStr([ // statements
  11888. 'rtl.createClass($mod, "TObject", null, function () {',
  11889. ' this.$init = function () {',
  11890. ' };',
  11891. ' this.$final = function () {',
  11892. ' };',
  11893. '});',
  11894. 'this.ProcA = function (A) {',
  11895. ' A.set(null);',
  11896. ' A.set(A.get());',
  11897. ' if (A.get() === null);',
  11898. ' if (null === A.get());',
  11899. '};',
  11900. 'this.ProcB = function (A) {',
  11901. ' A.set(null);',
  11902. ' A.set(A.get());',
  11903. ' if (A.get() === null);',
  11904. ' if (null === A.get());',
  11905. '};',
  11906. 'this.ProcC = function (A) {',
  11907. ' if (A === null);',
  11908. ' if (null === A);',
  11909. '};',
  11910. 'this.o = null;',
  11911. '']),
  11912. LinesToStr([ // $mod.$main
  11913. '$mod.ProcA({',
  11914. ' p: $mod,',
  11915. ' get: function () {',
  11916. ' return this.p.o;',
  11917. ' },',
  11918. ' set: function (v) {',
  11919. ' this.p.o = v;',
  11920. ' }',
  11921. '});',
  11922. '$mod.ProcB({',
  11923. ' p: $mod,',
  11924. ' get: function () {',
  11925. ' return this.p.o;',
  11926. ' },',
  11927. ' set: function (v) {',
  11928. ' this.p.o = v;',
  11929. ' }',
  11930. '});',
  11931. '$mod.ProcC($mod.o);',
  11932. '']));
  11933. end;
  11934. procedure TTestModule.TestClass_Overloads;
  11935. begin
  11936. StartProgram(false);
  11937. Add('type');
  11938. Add(' TObject = class');
  11939. Add(' procedure DoIt;');
  11940. Add(' procedure DoIt(vI: longint);');
  11941. Add(' end;');
  11942. Add('procedure TObject.DoIt;');
  11943. Add('begin');
  11944. Add(' DoIt;');
  11945. Add(' DoIt(1);');
  11946. Add('end;');
  11947. Add('procedure TObject.DoIt(vI: longint); begin end;');
  11948. Add('begin');
  11949. ConvertProgram;
  11950. CheckSource('TestClass_Overloads',
  11951. LinesToStr([ // statements
  11952. 'rtl.createClass($mod, "TObject", null, function () {',
  11953. ' this.$init = function () {',
  11954. ' };',
  11955. ' this.$final = function () {',
  11956. ' };',
  11957. ' this.DoIt = function () {',
  11958. ' this.DoIt();',
  11959. ' this.DoIt$1(1);',
  11960. ' };',
  11961. ' this.DoIt$1 = function (vI) {',
  11962. ' };',
  11963. '});',
  11964. '']),
  11965. LinesToStr([ // $mod.$main
  11966. '']));
  11967. end;
  11968. procedure TTestModule.TestClass_OverloadsAncestor;
  11969. begin
  11970. StartProgram(false);
  11971. Add('type');
  11972. Add(' TObject = class;');
  11973. Add(' TObject = class');
  11974. Add(' procedure DoIt(vA: longint);');
  11975. Add(' procedure DoIt(vA, vB: longint);');
  11976. Add(' end;');
  11977. Add(' TCar = class;');
  11978. Add(' TCar = class');
  11979. Add(' procedure DoIt(vA: longint);');
  11980. Add(' procedure DoIt(vA, vB: longint);');
  11981. Add(' end;');
  11982. Add('procedure tobject.doit(va: longint);');
  11983. Add('begin');
  11984. Add(' doit(1);');
  11985. Add(' doit(1,2);');
  11986. Add('end;');
  11987. Add('procedure tobject.doit(va, vb: longint); begin end;');
  11988. Add('procedure tcar.doit(va: longint);');
  11989. Add('begin');
  11990. Add(' doit(1);');
  11991. Add(' doit(1,2);');
  11992. Add(' inherited doit(1);');
  11993. Add(' inherited doit(1,2);');
  11994. Add('end;');
  11995. Add('procedure tcar.doit(va, vb: longint); begin end;');
  11996. Add('begin');
  11997. ConvertProgram;
  11998. CheckSource('TestClass_OverloadsAncestor',
  11999. LinesToStr([ // statements
  12000. 'rtl.createClass($mod, "TObject", null, function () {',
  12001. ' this.$init = function () {',
  12002. ' };',
  12003. ' this.$final = function () {',
  12004. ' };',
  12005. ' this.DoIt = function (vA) {',
  12006. ' this.DoIt(1);',
  12007. ' this.DoIt$1(1,2);',
  12008. ' };',
  12009. ' this.DoIt$1 = function (vA, vB) {',
  12010. ' };',
  12011. '});',
  12012. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  12013. ' this.DoIt$2 = function (vA) {',
  12014. ' this.DoIt$2(1);',
  12015. ' this.DoIt$3(1, 2);',
  12016. ' $mod.TObject.DoIt.call(this, 1);',
  12017. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  12018. ' };',
  12019. ' this.DoIt$3 = function (vA, vB) {',
  12020. ' };',
  12021. '});',
  12022. '']),
  12023. LinesToStr([ // $mod.$main
  12024. '']));
  12025. end;
  12026. procedure TTestModule.TestClass_OverloadConstructor;
  12027. begin
  12028. StartProgram(false);
  12029. Add('type');
  12030. Add(' TObject = class');
  12031. Add(' constructor Create(vA: longint);');
  12032. Add(' constructor Create(vA, vB: longint);');
  12033. Add(' end;');
  12034. Add(' TCar = class');
  12035. Add(' constructor Create(vA: longint);');
  12036. Add(' constructor Create(vA, vB: longint);');
  12037. Add(' end;');
  12038. Add('constructor tobject.create(va: longint);');
  12039. Add('begin');
  12040. Add(' create(1);');
  12041. Add(' create(1,2);');
  12042. Add('end;');
  12043. Add('constructor tobject.create(va, vb: longint); begin end;');
  12044. Add('constructor tcar.create(va: longint);');
  12045. Add('begin');
  12046. Add(' create(1);');
  12047. Add(' create(1,2);');
  12048. Add(' inherited create(1);');
  12049. Add(' inherited create(1,2);');
  12050. Add('end;');
  12051. Add('constructor tcar.create(va, vb: longint); begin end;');
  12052. Add('begin');
  12053. Add(' tobject.create(1);');
  12054. Add(' tobject.create(1,2);');
  12055. Add(' tcar.create(1);');
  12056. Add(' tcar.create(1,2);');
  12057. ConvertProgram;
  12058. CheckSource('TestClass_OverloadConstructor',
  12059. LinesToStr([ // statements
  12060. 'rtl.createClass($mod, "TObject", null, function () {',
  12061. ' this.$init = function () {',
  12062. ' };',
  12063. ' this.$final = function () {',
  12064. ' };',
  12065. ' this.Create = function (vA) {',
  12066. ' this.Create(1);',
  12067. ' this.Create$1(1,2);',
  12068. ' return this;',
  12069. ' };',
  12070. ' this.Create$1 = function (vA, vB) {',
  12071. ' return this;',
  12072. ' };',
  12073. '});',
  12074. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  12075. ' this.Create$2 = function (vA) {',
  12076. ' this.Create$2(1);',
  12077. ' this.Create$3(1, 2);',
  12078. ' $mod.TObject.Create.call(this, 1);',
  12079. ' $mod.TObject.Create$1.call(this, 1, 2);',
  12080. ' return this;',
  12081. ' };',
  12082. ' this.Create$3 = function (vA, vB) {',
  12083. ' return this;',
  12084. ' };',
  12085. '});',
  12086. '']),
  12087. LinesToStr([ // $mod.$main
  12088. '$mod.TObject.$create("Create", [1]);',
  12089. '$mod.TObject.$create("Create$1", [1, 2]);',
  12090. '$mod.TCar.$create("Create$2", [1]);',
  12091. '$mod.TCar.$create("Create$3", [1, 2]);',
  12092. '']));
  12093. end;
  12094. procedure TTestModule.TestClass_OverloadDelphiOverride;
  12095. begin
  12096. StartProgram(false);
  12097. Add([
  12098. '{$mode delphi}',
  12099. 'type',
  12100. ' TObject = class end;',
  12101. ' TBird = class',
  12102. ' function {#a}GetValue: longint; overload; virtual;',
  12103. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  12104. ' end;',
  12105. ' TEagle = class(TBird)',
  12106. ' function {#c}GetValue: longint; overload; override;',
  12107. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  12108. ' end;',
  12109. 'function TBird.GetValue: longint;',
  12110. 'begin',
  12111. ' if 3={@a}GetValue then ;',
  12112. ' if 4={@b}GetValue(5) then ;',
  12113. 'end;',
  12114. 'function TBird.GetValue(AValue: longint): longint;',
  12115. 'begin',
  12116. 'end;',
  12117. 'function TEagle.GetValue: longint;',
  12118. 'begin',
  12119. ' if 13={@c}GetValue then ;',
  12120. ' if 14={@d}GetValue(15) then ;',
  12121. ' if 15=inherited {@a}GetValue then ;',
  12122. ' if 16=inherited {@b}GetValue(17) then ;',
  12123. 'end;',
  12124. 'function TEagle.GetValue(AValue: longint): longint;',
  12125. 'begin',
  12126. 'end;',
  12127. 'var',
  12128. ' e: TEagle;',
  12129. 'begin',
  12130. ' if 23=e.{@c}GetValue then ;',
  12131. ' if 24=e.{@d}GetValue(25) then ;']);
  12132. ConvertProgram;
  12133. CheckSource('TestClass_OverloadDelphiOverride',
  12134. LinesToStr([ // statements
  12135. 'rtl.createClass($mod, "TObject", null, function () {',
  12136. ' this.$init = function () {',
  12137. ' };',
  12138. ' this.$final = function () {',
  12139. ' };',
  12140. '});',
  12141. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  12142. ' this.GetValue = function () {',
  12143. ' var Result = 0;',
  12144. ' if (3 === this.GetValue()) ;',
  12145. ' if (4 === this.GetValue$1(5)) ;',
  12146. ' return Result;',
  12147. ' };',
  12148. ' this.GetValue$1 = function (AValue) {',
  12149. ' var Result = 0;',
  12150. ' return Result;',
  12151. ' };',
  12152. '});',
  12153. 'rtl.createClass($mod, "TEagle", $mod.TBird, function () {',
  12154. ' this.GetValue = function () {',
  12155. ' var Result = 0;',
  12156. ' if (13 === this.GetValue()) ;',
  12157. ' if (14 === this.GetValue$1(15)) ;',
  12158. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  12159. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  12160. ' return Result;',
  12161. ' };',
  12162. ' this.GetValue$1 = function (AValue) {',
  12163. ' var Result = 0;',
  12164. ' return Result;',
  12165. ' };',
  12166. '});',
  12167. 'this.e = null;',
  12168. '']),
  12169. LinesToStr([ // $mod.$main
  12170. 'if (23 === $mod.e.GetValue()) ;',
  12171. 'if (24 === $mod.e.GetValue$1(25)) ;',
  12172. '']));
  12173. end;
  12174. procedure TTestModule.TestClass_ReintroducedVar;
  12175. begin
  12176. StartProgram(false);
  12177. Add('type');
  12178. Add(' TObject = class');
  12179. Add(' strict private');
  12180. Add(' Some: longint;');
  12181. Add(' end;');
  12182. Add(' TMobile = class');
  12183. Add(' strict private');
  12184. Add(' Some: string;');
  12185. Add(' end;');
  12186. Add(' TCar = class(tmobile)');
  12187. Add(' procedure Some;');
  12188. Add(' procedure Some(vA: longint);');
  12189. Add(' end;');
  12190. Add('procedure tcar.some;');
  12191. Add('begin');
  12192. Add(' Some;');
  12193. Add(' Some(1);');
  12194. Add('end;');
  12195. Add('procedure tcar.some(va: longint); begin end;');
  12196. Add('begin');
  12197. ConvertProgram;
  12198. CheckSource('TestClass_ReintroducedVar',
  12199. LinesToStr([ // statements
  12200. 'rtl.createClass($mod, "TObject", null, function () {',
  12201. ' this.$init = function () {',
  12202. ' this.Some = 0;',
  12203. ' };',
  12204. ' this.$final = function () {',
  12205. ' };',
  12206. '});',
  12207. 'rtl.createClass($mod, "TMobile", $mod.TObject, function () {',
  12208. ' this.$init = function () {',
  12209. ' $mod.TObject.$init.call(this);',
  12210. ' this.Some$1 = "";',
  12211. ' };',
  12212. '});',
  12213. 'rtl.createClass($mod, "TCar", $mod.TMobile, function () {',
  12214. ' this.Some$2 = function () {',
  12215. ' this.Some$2();',
  12216. ' this.Some$3(1);',
  12217. ' };',
  12218. ' this.Some$3 = function (vA) {',
  12219. ' };',
  12220. '});',
  12221. '']),
  12222. LinesToStr([ // $mod.$main
  12223. '']));
  12224. end;
  12225. procedure TTestModule.TestClass_RaiseDescendant;
  12226. begin
  12227. StartProgram(false);
  12228. Add([
  12229. 'type',
  12230. ' TObject = class',
  12231. ' constructor Create(Msg: string);',
  12232. ' end;',
  12233. ' Exception = class',
  12234. ' end;',
  12235. ' EConvertError = class(Exception)',
  12236. ' end;',
  12237. 'constructor TObject.Create(Msg: string); begin end;',
  12238. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  12239. 'begin',
  12240. ' raise Exception.Create(''Bar1'');',
  12241. ' raise EConvertError.Create(''Bar2'');',
  12242. ' raise AssertConv(''Bar2'');',
  12243. ' raise AssertConv;',
  12244. '']);
  12245. ConvertProgram;
  12246. CheckSource('TestClass_RaiseDescendant',
  12247. LinesToStr([ // statements
  12248. 'rtl.createClass($mod, "TObject", null, function () {',
  12249. ' this.$init = function () {',
  12250. ' };',
  12251. ' this.$final = function () {',
  12252. ' };',
  12253. ' this.Create = function (Msg) {',
  12254. ' return this;',
  12255. ' };',
  12256. '});',
  12257. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  12258. '});',
  12259. 'rtl.createClass($mod, "EConvertError", $mod.Exception, function () {',
  12260. '});',
  12261. 'this.AssertConv = function (Msg) {',
  12262. ' var Result = null;',
  12263. ' return Result;',
  12264. '};',
  12265. '']),
  12266. LinesToStr([ // $mod.$main
  12267. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  12268. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  12269. 'throw $mod.AssertConv("Bar2");',
  12270. 'throw $mod.AssertConv("def");',
  12271. '']));
  12272. end;
  12273. procedure TTestModule.TestClass_ExternalMethod;
  12274. begin
  12275. AddModuleWithIntfImplSrc('unit2.pas',
  12276. LinesToStr([
  12277. 'type',
  12278. ' TObject = class',
  12279. ' public',
  12280. ' procedure Intern; external name ''$DoIntern'';',
  12281. ' end;',
  12282. '']),
  12283. LinesToStr([
  12284. '']));
  12285. StartUnit(true);
  12286. Add('interface');
  12287. Add('uses unit2;');
  12288. Add('type');
  12289. Add(' TCar = class(TObject)');
  12290. Add(' public');
  12291. Add(' procedure Intern2; external name ''$DoIntern2'';');
  12292. Add(' procedure DoIt;');
  12293. Add(' end;');
  12294. Add('implementation');
  12295. Add('procedure tcar.doit;');
  12296. Add('begin');
  12297. Add(' Intern;');
  12298. Add(' Intern();');
  12299. Add(' Intern2;');
  12300. Add(' Intern2();');
  12301. Add('end;');
  12302. Add('var Obj: TCar;');
  12303. Add('begin');
  12304. Add(' obj.intern;');
  12305. Add(' obj.intern();');
  12306. Add(' obj.intern2;');
  12307. Add(' obj.intern2();');
  12308. Add(' obj.doit;');
  12309. Add(' obj.doit();');
  12310. Add(' with obj do begin');
  12311. Add(' Intern;');
  12312. Add(' Intern();');
  12313. Add(' Intern2;');
  12314. Add(' Intern2();');
  12315. Add(' end;');
  12316. ConvertUnit;
  12317. CheckSource('TestClass_ExternalMethod',
  12318. LinesToStr([
  12319. 'var $impl = $mod.$impl;',
  12320. 'rtl.createClass($mod, "TCar", pas.unit2.TObject, function () {',
  12321. ' this.DoIt = function () {',
  12322. ' this.$DoIntern();',
  12323. ' this.$DoIntern();',
  12324. ' this.$DoIntern2();',
  12325. ' this.$DoIntern2();',
  12326. ' };',
  12327. ' });',
  12328. '']),
  12329. LinesToStr([ // this.$init
  12330. '$impl.Obj.$DoIntern();',
  12331. '$impl.Obj.$DoIntern();',
  12332. '$impl.Obj.$DoIntern2();',
  12333. '$impl.Obj.$DoIntern2();',
  12334. '$impl.Obj.DoIt();',
  12335. '$impl.Obj.DoIt();',
  12336. 'var $with1 = $impl.Obj;',
  12337. '$with1.$DoIntern();',
  12338. '$with1.$DoIntern();',
  12339. '$with1.$DoIntern2();',
  12340. '$with1.$DoIntern2();',
  12341. '']),
  12342. LinesToStr([ // implementation
  12343. '$impl.Obj = null;',
  12344. '']) );
  12345. end;
  12346. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  12347. begin
  12348. StartProgram(false);
  12349. Add('type');
  12350. Add(' TObject = class');
  12351. Add(' procedure DoIt; virtual; external name ''Foo'';');
  12352. Add(' end;');
  12353. Add('begin');
  12354. SetExpectedPasResolverError('Virtual method name must match external',
  12355. nVirtualMethodNameMustMatchExternal);
  12356. ConvertProgram;
  12357. end;
  12358. procedure TTestModule.TestClass_ExternalOverrideFail;
  12359. begin
  12360. StartProgram(false);
  12361. Add('type');
  12362. Add(' TObject = class');
  12363. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  12364. Add(' end;');
  12365. Add(' TCar = class');
  12366. Add(' procedure DoIt; override; external name ''DoIt'';');
  12367. Add(' end;');
  12368. Add('begin');
  12369. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  12370. nInvalidXModifierY);
  12371. ConvertProgram;
  12372. end;
  12373. procedure TTestModule.TestClass_ExternalVar;
  12374. begin
  12375. AddModuleWithIntfImplSrc('unit2.pas',
  12376. LinesToStr([
  12377. '{$modeswitch externalclass}',
  12378. 'type',
  12379. ' TObject = class',
  12380. ' public',
  12381. ' Intern: longint external name ''$Intern'';',
  12382. ' Bracket: longint external name ''["A B"]'';',
  12383. ' end;',
  12384. '']),
  12385. LinesToStr([
  12386. '']));
  12387. StartUnit(true);
  12388. Add([
  12389. 'interface',
  12390. 'uses unit2;',
  12391. '{$modeswitch externalclass}',
  12392. 'type',
  12393. ' TCar = class(tobject)',
  12394. ' public',
  12395. ' Intern2: longint external name ''$Intern2'';',
  12396. ' procedure DoIt;',
  12397. ' end;',
  12398. 'implementation',
  12399. 'procedure tcar.doit;',
  12400. 'begin',
  12401. ' Intern:=Intern+1;',
  12402. ' Intern2:=Intern2+2;',
  12403. ' Bracket:=Bracket+3;',
  12404. 'end;',
  12405. 'var Obj: TCar;',
  12406. 'begin',
  12407. ' obj.intern:=obj.intern+1;',
  12408. ' obj.intern2:=obj.intern2+2;',
  12409. ' obj.Bracket:=obj.Bracket+3;',
  12410. ' with obj do begin',
  12411. ' intern:=intern+1;',
  12412. ' intern2:=intern2+2;',
  12413. ' Bracket:=Bracket+3;',
  12414. ' end;']);
  12415. ConvertUnit;
  12416. CheckSource('TestClass_ExternalVar',
  12417. LinesToStr([
  12418. 'var $impl = $mod.$impl;',
  12419. 'rtl.createClass($mod, "TCar", pas.unit2.TObject, function () {',
  12420. ' this.DoIt = function () {',
  12421. ' this.$Intern = this.$Intern + 1;',
  12422. ' this.$Intern2 = this.$Intern2 + 2;',
  12423. ' this["A B"] = this["A B"] + 3;',
  12424. ' };',
  12425. ' });',
  12426. '']),
  12427. LinesToStr([
  12428. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  12429. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  12430. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  12431. 'var $with1 = $impl.Obj;',
  12432. '$with1.$Intern = $with1.$Intern + 1;',
  12433. '$with1.$Intern2 = $with1.$Intern2 + 2;',
  12434. '$with1["A B"] = $with1["A B"] + 3;',
  12435. '']),
  12436. LinesToStr([ // implementation
  12437. '$impl.Obj = null;',
  12438. '']));
  12439. end;
  12440. procedure TTestModule.TestClass_Const;
  12441. begin
  12442. StartProgram(false);
  12443. Add('type');
  12444. Add(' integer = longint;');
  12445. Add(' TClass = class of TObject;');
  12446. Add(' TObject = class');
  12447. Add(' public');
  12448. Add(' const cI: integer = 3;');
  12449. Add(' procedure DoIt;');
  12450. Add(' class procedure DoMore;');
  12451. Add(' end;');
  12452. Add('implementation');
  12453. Add('procedure tobject.doit;');
  12454. Add('begin');
  12455. Add(' if cI=4 then;');
  12456. Add(' if 5=cI then;');
  12457. Add(' if Self.cI=6 then;');
  12458. Add(' if 7=Self.cI then;');
  12459. Add(' with Self do begin');
  12460. Add(' if cI=11 then;');
  12461. Add(' if 12=cI then;');
  12462. Add(' end;');
  12463. Add('end;');
  12464. Add('class procedure tobject.domore;');
  12465. Add('begin');
  12466. Add(' if cI=8 then;');
  12467. Add(' if Self.cI=9 then;');
  12468. Add(' if 10=cI then;');
  12469. Add(' if 11=Self.cI then;');
  12470. Add(' with Self do begin');
  12471. Add(' if cI=13 then;');
  12472. Add(' if 14=cI then;');
  12473. Add(' end;');
  12474. Add('end;');
  12475. Add('var');
  12476. Add(' Obj: TObject;');
  12477. Add(' Cla: TClass;');
  12478. Add('begin');
  12479. Add(' if TObject.cI=21 then ;');
  12480. Add(' if Obj.cI=22 then ;');
  12481. Add(' if Cla.cI=23 then ;');
  12482. Add(' with obj do if ci=24 then;');
  12483. Add(' with TObject do if ci=25 then;');
  12484. Add(' with Cla do if ci=26 then;');
  12485. ConvertProgram;
  12486. CheckSource('TestClass_Const',
  12487. LinesToStr([
  12488. 'rtl.createClass($mod, "TObject", null, function () {',
  12489. ' this.cI = 3;',
  12490. ' this.$init = function () {',
  12491. ' };',
  12492. ' this.$final = function () {',
  12493. ' };',
  12494. ' this.DoIt = function () {',
  12495. ' if (this.cI === 4) ;',
  12496. ' if (5 === this.cI) ;',
  12497. ' if (this.cI === 6) ;',
  12498. ' if (7 === this.cI) ;',
  12499. ' if (this.cI === 11) ;',
  12500. ' if (12 === this.cI) ;',
  12501. ' };',
  12502. ' this.DoMore = function () {',
  12503. ' if (this.cI === 8) ;',
  12504. ' if (this.cI === 9) ;',
  12505. ' if (10 === this.cI) ;',
  12506. ' if (11 === this.cI) ;',
  12507. ' if (this.cI === 13) ;',
  12508. ' if (14 === this.cI) ;',
  12509. ' };',
  12510. '});',
  12511. 'this.Obj = null;',
  12512. 'this.Cla = null;',
  12513. '']),
  12514. LinesToStr([
  12515. 'if ($mod.TObject.cI === 21) ;',
  12516. 'if ($mod.Obj.cI === 22) ;',
  12517. 'if ($mod.Cla.cI === 23) ;',
  12518. 'var $with1 = $mod.Obj;',
  12519. 'if ($with1.cI === 24) ;',
  12520. 'var $with2 = $mod.TObject;',
  12521. 'if ($with2.cI === 25) ;',
  12522. 'var $with3 = $mod.Cla;',
  12523. 'if ($with3.cI === 26) ;',
  12524. '']));
  12525. end;
  12526. procedure TTestModule.TestClass_LocalVarSelfFail;
  12527. begin
  12528. StartProgram(false);
  12529. Add([
  12530. 'type',
  12531. ' TObject = class',
  12532. ' constructor Create;',
  12533. ' end;',
  12534. 'constructor tobject.create;',
  12535. 'var self: longint;',
  12536. 'begin',
  12537. 'end',
  12538. 'begin',
  12539. '']);
  12540. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  12541. ConvertProgram;
  12542. end;
  12543. procedure TTestModule.TestClass_ArgSelfFail;
  12544. begin
  12545. StartProgram(false);
  12546. Add([
  12547. 'type',
  12548. ' TObject = class',
  12549. ' procedure DoIt(Self: longint);',
  12550. ' end;',
  12551. 'procedure tobject.doit(self: longint);',
  12552. 'begin',
  12553. 'end',
  12554. 'begin',
  12555. '']);
  12556. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  12557. ConvertProgram;
  12558. end;
  12559. procedure TTestModule.TestClass_NestedProcSelf;
  12560. begin
  12561. StartProgram(false);
  12562. Add([
  12563. 'type',
  12564. ' TObject = class',
  12565. ' Key: longint;',
  12566. ' class var State: longint;',
  12567. ' procedure DoIt;',
  12568. ' function GetSize: longint; virtual; abstract;',
  12569. ' procedure SetSize(Value: longint); virtual; abstract;',
  12570. ' property Size: longint read GetSize write SetSize;',
  12571. ' end;',
  12572. 'procedure tobject.doit;',
  12573. ' procedure Sub;',
  12574. ' begin',
  12575. ' key:=key+2;',
  12576. ' self.key:=self.key+3;',
  12577. ' state:=state+4;',
  12578. ' self.state:=self.state+5;',
  12579. ' tobject.state:=tobject.state+6;',
  12580. ' size:=size+7;',
  12581. ' self.size:=self.size+8;',
  12582. ' end;',
  12583. 'begin',
  12584. ' sub;',
  12585. ' key:=key+12;',
  12586. ' self.key:=self.key+13;',
  12587. ' state:=state+14;',
  12588. ' self.state:=self.state+15;',
  12589. ' tobject.state:=tobject.state+16;',
  12590. ' size:=size+17;',
  12591. ' self.size:=self.size+18;',
  12592. 'end;',
  12593. 'begin',
  12594. '']);
  12595. ConvertProgram;
  12596. CheckSource('TestClass_NestedProcSelf',
  12597. LinesToStr([ // statements
  12598. 'rtl.createClass($mod, "TObject", null, function () {',
  12599. ' this.State = 0;',
  12600. ' this.$init = function () {',
  12601. ' this.Key = 0;',
  12602. ' };',
  12603. ' this.$final = function () {',
  12604. ' };',
  12605. ' this.DoIt = function () {',
  12606. ' var Self = this;',
  12607. ' function Sub() {',
  12608. ' Self.Key = Self.Key + 2;',
  12609. ' Self.Key = Self.Key + 3;',
  12610. ' $mod.TObject.State = Self.State + 4;',
  12611. ' $mod.TObject.State = Self.State + 5;',
  12612. ' $mod.TObject.State = $mod.TObject.State + 6;',
  12613. ' Self.SetSize(Self.GetSize() + 7);',
  12614. ' Self.SetSize(Self.GetSize() + 8);',
  12615. ' };',
  12616. ' Sub();',
  12617. ' Self.Key = Self.Key + 12;',
  12618. ' Self.Key = Self.Key + 13;',
  12619. ' $mod.TObject.State = Self.State + 14;',
  12620. ' $mod.TObject.State = Self.State + 15;',
  12621. ' $mod.TObject.State = $mod.TObject.State + 16;',
  12622. ' Self.SetSize(Self.GetSize() + 17);',
  12623. ' Self.SetSize(Self.GetSize() + 18);',
  12624. ' };',
  12625. '});',
  12626. '']),
  12627. LinesToStr([ // $mod.$main
  12628. '']));
  12629. end;
  12630. procedure TTestModule.TestClass_NestedProcSelf2;
  12631. begin
  12632. StartProgram(false);
  12633. Add([
  12634. 'type',
  12635. ' TObject = class',
  12636. ' Key: longint;',
  12637. ' class var State: longint;',
  12638. ' function GetSize: longint; virtual; abstract;',
  12639. ' procedure SetSize(Value: longint); virtual; abstract;',
  12640. ' property Size: longint read GetSize write SetSize;',
  12641. ' end;',
  12642. ' TBird = class',
  12643. ' procedure DoIt;',
  12644. ' end;',
  12645. 'procedure tbird.doit;',
  12646. ' procedure Sub;',
  12647. ' begin',
  12648. ' key:=key+2;',
  12649. ' self.key:=self.key+3;',
  12650. ' state:=state+4;',
  12651. ' self.state:=self.state+5;',
  12652. ' tobject.state:=tobject.state+6;',
  12653. ' size:=size+7;',
  12654. ' self.size:=self.size+8;',
  12655. ' end;',
  12656. 'begin',
  12657. ' sub;',
  12658. ' key:=key+12;',
  12659. ' self.key:=self.key+13;',
  12660. ' state:=state+14;',
  12661. ' self.state:=self.state+15;',
  12662. ' tobject.state:=tobject.state+16;',
  12663. ' size:=size+17;',
  12664. ' self.size:=self.size+18;',
  12665. 'end;',
  12666. 'begin',
  12667. '']);
  12668. ConvertProgram;
  12669. CheckSource('TestClass_NestedProcSelf2',
  12670. LinesToStr([ // statements
  12671. 'rtl.createClass($mod, "TObject", null, function () {',
  12672. ' this.State = 0;',
  12673. ' this.$init = function () {',
  12674. ' this.Key = 0;',
  12675. ' };',
  12676. ' this.$final = function () {',
  12677. ' };',
  12678. '});',
  12679. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  12680. ' this.DoIt = function () {',
  12681. ' var Self = this;',
  12682. ' function Sub() {',
  12683. ' Self.Key = Self.Key + 2;',
  12684. ' Self.Key = Self.Key + 3;',
  12685. ' $mod.TObject.State = Self.State + 4;',
  12686. ' $mod.TObject.State = Self.State + 5;',
  12687. ' $mod.TObject.State = $mod.TObject.State + 6;',
  12688. ' Self.SetSize(Self.GetSize() + 7);',
  12689. ' Self.SetSize(Self.GetSize() + 8);',
  12690. ' };',
  12691. ' Sub();',
  12692. ' Self.Key = Self.Key + 12;',
  12693. ' Self.Key = Self.Key + 13;',
  12694. ' $mod.TObject.State = Self.State + 14;',
  12695. ' $mod.TObject.State = Self.State + 15;',
  12696. ' $mod.TObject.State = $mod.TObject.State + 16;',
  12697. ' Self.SetSize(Self.GetSize() + 17);',
  12698. ' Self.SetSize(Self.GetSize() + 18);',
  12699. ' };',
  12700. '});',
  12701. '']),
  12702. LinesToStr([ // $mod.$main
  12703. '']));
  12704. end;
  12705. procedure TTestModule.TestClass_NestedProcClassSelf;
  12706. begin
  12707. StartProgram(false);
  12708. Add([
  12709. 'type',
  12710. ' TObject = class',
  12711. ' class var State: longint;',
  12712. ' class procedure DoIt;',
  12713. ' class function GetSize: longint; virtual; abstract;',
  12714. ' class procedure SetSize(Value: longint); virtual; abstract;',
  12715. ' class property Size: longint read GetSize write SetSize;',
  12716. ' end;',
  12717. 'class procedure tobject.doit;',
  12718. ' procedure Sub;',
  12719. ' begin',
  12720. ' state:=state+2;',
  12721. ' self.state:=self.state+3;',
  12722. ' tobject.state:=tobject.state+4;',
  12723. ' size:=size+5;',
  12724. ' self.size:=self.size+6;',
  12725. ' tobject.size:=tobject.size+7;',
  12726. ' end;',
  12727. 'begin',
  12728. ' sub;',
  12729. ' state:=state+12;',
  12730. ' self.state:=self.state+13;',
  12731. ' tobject.state:=tobject.state+14;',
  12732. ' size:=size+15;',
  12733. ' self.size:=self.size+16;',
  12734. ' tobject.size:=tobject.size+17;',
  12735. 'end;',
  12736. 'begin',
  12737. '']);
  12738. ConvertProgram;
  12739. CheckSource('TestClass_NestedProcClassSelf',
  12740. LinesToStr([ // statements
  12741. 'rtl.createClass($mod, "TObject", null, function () {',
  12742. ' this.State = 0;',
  12743. ' this.$init = function () {',
  12744. ' };',
  12745. ' this.$final = function () {',
  12746. ' };',
  12747. ' this.DoIt = function () {',
  12748. ' var Self = this;',
  12749. ' function Sub() {',
  12750. ' $mod.TObject.State = Self.State + 2;',
  12751. ' $mod.TObject.State = Self.State + 3;',
  12752. ' $mod.TObject.State = $mod.TObject.State + 4;',
  12753. ' Self.SetSize(Self.GetSize() + 5);',
  12754. ' Self.SetSize(Self.GetSize() + 6);',
  12755. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  12756. ' };',
  12757. ' Sub();',
  12758. ' $mod.TObject.State = Self.State + 12;',
  12759. ' $mod.TObject.State = Self.State + 13;',
  12760. ' $mod.TObject.State = $mod.TObject.State + 14;',
  12761. ' Self.SetSize(Self.GetSize() + 15);',
  12762. ' Self.SetSize(Self.GetSize() + 16);',
  12763. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  12764. ' };',
  12765. '});',
  12766. '']),
  12767. LinesToStr([ // $mod.$main
  12768. '']));
  12769. end;
  12770. procedure TTestModule.TestClass_NestedProcCallInherited;
  12771. begin
  12772. StartProgram(false);
  12773. Add([
  12774. 'type',
  12775. ' TObject = class',
  12776. ' function DoIt(k: boolean): longint; virtual;',
  12777. ' end;',
  12778. ' TBird = class',
  12779. ' function DoIt(k: boolean): longint; override;',
  12780. ' end;',
  12781. 'function tobject.doit(k: boolean): longint;',
  12782. 'begin',
  12783. 'end;',
  12784. 'function tbird.doit(k: boolean): longint;',
  12785. ' procedure Sub;',
  12786. ' begin',
  12787. ' inherited DoIt(true);',
  12788. //' if inherited DoIt(false)=4 then ;',
  12789. ' end;',
  12790. 'begin',
  12791. ' Sub;',
  12792. ' inherited;',
  12793. ' inherited DoIt(true);',
  12794. //' if inherited DoIt(false)=14 then ;',
  12795. 'end;',
  12796. 'begin',
  12797. '']);
  12798. ConvertProgram;
  12799. CheckSource('TestClass_NestedProcCallInherited',
  12800. LinesToStr([ // statements
  12801. 'rtl.createClass($mod, "TObject", null, function () {',
  12802. ' this.$init = function () {',
  12803. ' };',
  12804. ' this.$final = function () {',
  12805. ' };',
  12806. ' this.DoIt = function (k) {',
  12807. ' var Result = 0;',
  12808. ' return Result;',
  12809. ' };',
  12810. '});',
  12811. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  12812. ' this.DoIt = function (k) {',
  12813. ' var Self = this;',
  12814. ' var Result = 0;',
  12815. ' function Sub() {',
  12816. ' $mod.TObject.DoIt.call(Self, true);',
  12817. ' };',
  12818. ' Sub();',
  12819. ' $mod.TObject.DoIt.apply(Self, arguments);',
  12820. ' $mod.TObject.DoIt.call(Self, true);',
  12821. ' return Result;',
  12822. ' };',
  12823. '});',
  12824. '']),
  12825. LinesToStr([ // $mod.$main
  12826. '']));
  12827. end;
  12828. procedure TTestModule.TestClass_TObjectFree;
  12829. begin
  12830. StartProgram(false);
  12831. Add([
  12832. 'type',
  12833. ' TObject = class',
  12834. ' Obj: tobject;',
  12835. ' procedure Free;',
  12836. ' procedure Release;',
  12837. ' end;',
  12838. 'procedure tobject.free;',
  12839. 'begin',
  12840. 'end;',
  12841. 'procedure tobject.release;',
  12842. 'begin',
  12843. ' free;',
  12844. ' if true then free;',
  12845. 'end;',
  12846. 'function DoIt(o: tobject): tobject;',
  12847. 'var l: tobject;',
  12848. 'begin',
  12849. ' o.free;',
  12850. ' o.free();',
  12851. ' l.free;',
  12852. ' l.free();',
  12853. ' o.obj.free;',
  12854. ' o.obj.free();',
  12855. ' with o do obj.free;',
  12856. ' with o do obj.free();',
  12857. ' result.Free;',
  12858. ' result.Free();',
  12859. 'end;',
  12860. 'var o: tobject;',
  12861. ' a: array of tobject;',
  12862. 'begin',
  12863. ' o.free;',
  12864. ' o.obj.free;',
  12865. ' a[1+2].free;',
  12866. '']);
  12867. ConvertProgram;
  12868. CheckSource('TestClass_TObjectFree',
  12869. LinesToStr([ // statements
  12870. 'rtl.createClass($mod, "TObject", null, function () {',
  12871. ' this.$init = function () {',
  12872. ' this.Obj = null;',
  12873. ' };',
  12874. ' this.$final = function () {',
  12875. ' this.Obj = undefined;',
  12876. ' };',
  12877. ' this.Free = function () {',
  12878. ' };',
  12879. ' this.Release = function () {',
  12880. ' this.Free();',
  12881. ' if (true) this.Free();',
  12882. ' };',
  12883. '});',
  12884. 'this.DoIt = function (o) {',
  12885. ' var Result = null;',
  12886. ' var l = null;',
  12887. ' o = rtl.freeLoc(o);',
  12888. ' o = rtl.freeLoc(o);',
  12889. ' l = rtl.freeLoc(l);',
  12890. ' l = rtl.freeLoc(l);',
  12891. ' rtl.free(o, "Obj");',
  12892. ' rtl.free(o, "Obj");',
  12893. ' rtl.free(o, "Obj");',
  12894. ' rtl.free(o, "Obj");',
  12895. ' Result = rtl.freeLoc(Result);',
  12896. ' Result = rtl.freeLoc(Result);',
  12897. ' return Result;',
  12898. '};',
  12899. 'this.o = null;',
  12900. 'this.a = [];',
  12901. '']),
  12902. LinesToStr([ // $mod.$main
  12903. 'rtl.free($mod, "o");',
  12904. 'rtl.free($mod.o, "Obj");',
  12905. 'rtl.free($mod.a, 1 + 2);',
  12906. '']));
  12907. end;
  12908. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  12909. begin
  12910. StartProgram(false);
  12911. Add([
  12912. 'type',
  12913. ' TObject = class',
  12914. ' constructor Create;',
  12915. ' procedure Free;',
  12916. ' end;',
  12917. 'constructor TObject.Create; begin end;',
  12918. 'procedure tobject.free; begin end;',
  12919. 'begin',
  12920. ' with tobject.create do free;',
  12921. '']);
  12922. ConvertProgram;
  12923. CheckSource('TestClass_TObjectFreeNewInstance',
  12924. LinesToStr([ // statements
  12925. 'rtl.createClass($mod, "TObject", null, function () {',
  12926. ' this.$init = function () {',
  12927. ' };',
  12928. ' this.$final = function () {',
  12929. ' };',
  12930. ' this.Create = function () {',
  12931. ' return this;',
  12932. ' };',
  12933. ' this.Free = function () {',
  12934. ' };',
  12935. '});',
  12936. '']),
  12937. LinesToStr([ // $mod.$main
  12938. 'var $with1 = $mod.TObject.$create("Create");',
  12939. '$with1=rtl.freeLoc($with1);',
  12940. '']));
  12941. end;
  12942. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  12943. begin
  12944. StartProgram(false);
  12945. Add([
  12946. 'type',
  12947. ' TObject = class',
  12948. ' destructor Destroy;',
  12949. ' procedure Free;',
  12950. ' end;',
  12951. 'destructor TObject.Destroy; begin end;',
  12952. 'procedure tobject.free; begin end;',
  12953. 'var o: tobject;',
  12954. 'begin',
  12955. ' o.free;',
  12956. '']);
  12957. Converter.UseLowerCase:=true;
  12958. ConvertProgram;
  12959. CheckSource('TestClass_TObjectFreeLowerCase',
  12960. LinesToStr([ // statements
  12961. 'rtl.createClass($mod, "tobject", null, function () {',
  12962. ' this.$init = function () {',
  12963. ' };',
  12964. ' this.$final = function () {',
  12965. ' };',
  12966. ' rtl.tObjectDestroy = "destroy";',
  12967. ' this.destroy = function () {',
  12968. ' };',
  12969. ' this.free = function () {',
  12970. ' };',
  12971. '});',
  12972. 'this.o = null;',
  12973. '']),
  12974. LinesToStr([ // $mod.$main
  12975. 'rtl.free($mod, "o");',
  12976. '']));
  12977. end;
  12978. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  12979. begin
  12980. StartProgram(false);
  12981. Add([
  12982. 'type',
  12983. ' TObject = class',
  12984. ' procedure Free;',
  12985. ' function GetObj: tobject; virtual; abstract;',
  12986. ' end;',
  12987. 'procedure tobject.free;',
  12988. 'begin',
  12989. 'end;',
  12990. 'var o: tobject;',
  12991. 'begin',
  12992. ' o.getobj.free;',
  12993. '']);
  12994. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  12995. ConvertProgram;
  12996. end;
  12997. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  12998. begin
  12999. StartProgram(false);
  13000. Add([
  13001. 'type',
  13002. ' TObject = class',
  13003. ' procedure Free;',
  13004. ' FObj: TObject;',
  13005. ' property Obj: tobject read FObj write FObj;',
  13006. ' end;',
  13007. 'procedure tobject.free;',
  13008. 'begin',
  13009. 'end;',
  13010. 'var o: tobject;',
  13011. 'begin',
  13012. ' o.obj.free;',
  13013. '']);
  13014. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  13015. ConvertProgram;
  13016. end;
  13017. procedure TTestModule.TestClass_ForIn;
  13018. begin
  13019. StartProgram(false);
  13020. Add([
  13021. 'type',
  13022. ' TObject = class end;',
  13023. ' TItem = TObject;',
  13024. ' TEnumerator = class',
  13025. ' FCurrent: TItem;',
  13026. ' property Current: TItem read FCurrent;',
  13027. ' function MoveNext: boolean;',
  13028. ' end;',
  13029. ' TBird = class',
  13030. ' function GetEnumerator: TEnumerator;',
  13031. ' end;',
  13032. 'function TEnumerator.MoveNext: boolean;',
  13033. 'begin',
  13034. 'end;',
  13035. 'function TBird.GetEnumerator: TEnumerator;',
  13036. 'begin',
  13037. 'end;',
  13038. 'var',
  13039. ' b: TBird;',
  13040. ' i, i2: TItem;',
  13041. 'begin',
  13042. ' for i in b do i2:=i;']);
  13043. ConvertProgram;
  13044. CheckSource('TestClass_ForIn',
  13045. LinesToStr([ // statements
  13046. 'rtl.createClass($mod, "TObject", null, function () {',
  13047. ' this.$init = function () {',
  13048. ' };',
  13049. ' this.$final = function () {',
  13050. ' };',
  13051. '});',
  13052. 'rtl.createClass($mod, "TEnumerator", $mod.TObject, function () {',
  13053. ' this.$init = function () {',
  13054. ' $mod.TObject.$init.call(this);',
  13055. ' this.FCurrent = null;',
  13056. ' };',
  13057. ' this.$final = function () {',
  13058. ' this.FCurrent = undefined;',
  13059. ' $mod.TObject.$final.call(this);',
  13060. ' };',
  13061. ' this.MoveNext = function () {',
  13062. ' var Result = false;',
  13063. ' return Result;',
  13064. ' };',
  13065. '});',
  13066. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  13067. ' this.GetEnumerator = function () {',
  13068. ' var Result = null;',
  13069. ' return Result;',
  13070. ' };',
  13071. '});',
  13072. 'this.b = null;',
  13073. 'this.i = null;',
  13074. 'this.i2 = null;'
  13075. ]),
  13076. LinesToStr([ // $mod.$main
  13077. 'var $in1 = $mod.b.GetEnumerator();',
  13078. 'try {',
  13079. ' while ($in1.MoveNext()){',
  13080. ' $mod.i = $in1.FCurrent;',
  13081. ' $mod.i2 = $mod.i;',
  13082. ' }',
  13083. '} finally {',
  13084. ' $in1 = rtl.freeLoc($in1)',
  13085. '};',
  13086. '']));
  13087. end;
  13088. procedure TTestModule.TestClassOf_Create;
  13089. begin
  13090. StartProgram(false);
  13091. Add('type');
  13092. Add(' TObject = class');
  13093. Add(' constructor Create;');
  13094. Add(' end;');
  13095. Add(' TClass = class of TObject;');
  13096. Add('constructor tobject.create; begin end;');
  13097. Add('var');
  13098. Add(' Obj: tobject;');
  13099. Add(' C: tclass;');
  13100. Add('begin');
  13101. Add(' obj:=C.create;');
  13102. Add(' with c do obj:=create;');
  13103. ConvertProgram;
  13104. CheckSource('TestClassOf_Create',
  13105. LinesToStr([ // statements
  13106. 'rtl.createClass($mod, "TObject", null, function () {',
  13107. ' this.$init = function () {',
  13108. ' };',
  13109. ' this.$final = function () {',
  13110. ' };',
  13111. ' this.Create = function () {',
  13112. ' return this;',
  13113. ' };',
  13114. '});',
  13115. 'this.Obj = null;',
  13116. 'this.C = null;'
  13117. ]),
  13118. LinesToStr([ // $mod.$main
  13119. '$mod.Obj = $mod.C.$create("Create");',
  13120. 'var $with1 = $mod.C;',
  13121. '$mod.Obj = $with1.$create("Create");',
  13122. '']));
  13123. end;
  13124. procedure TTestModule.TestClassOf_Call;
  13125. begin
  13126. StartProgram(false);
  13127. Add('type');
  13128. Add(' TObject = class');
  13129. Add(' class procedure DoIt;');
  13130. Add(' end;');
  13131. Add(' TClass = class of TObject;');
  13132. Add('class procedure tobject.doit; begin end;');
  13133. Add('var');
  13134. Add(' C: tclass;');
  13135. Add('begin');
  13136. Add(' c.doit;');
  13137. Add(' with c do doit;');
  13138. ConvertProgram;
  13139. CheckSource('TestClassOf_Call',
  13140. LinesToStr([ // statements
  13141. 'rtl.createClass($mod, "TObject", null, function () {',
  13142. ' this.$init = function () {',
  13143. ' };',
  13144. ' this.$final = function () {',
  13145. ' };',
  13146. ' this.DoIt = function () {',
  13147. ' };',
  13148. '});',
  13149. 'this.C = null;'
  13150. ]),
  13151. LinesToStr([ // $mod.$main
  13152. '$mod.C.DoIt();',
  13153. 'var $with1 = $mod.C;',
  13154. '$with1.DoIt();',
  13155. '']));
  13156. end;
  13157. procedure TTestModule.TestClassOf_Assign;
  13158. begin
  13159. StartProgram(false);
  13160. Add('type');
  13161. Add(' TClass = class of TObject;');
  13162. Add(' TObject = class');
  13163. Add(' ClassType: TClass; ');
  13164. Add(' end;');
  13165. Add('var');
  13166. Add(' Obj: tobject;');
  13167. Add(' C: tclass;');
  13168. Add('begin');
  13169. Add(' c:=nil;');
  13170. Add(' c:=obj.classtype;');
  13171. ConvertProgram;
  13172. CheckSource('TestClassOf_Assign',
  13173. LinesToStr([ // statements
  13174. 'rtl.createClass($mod, "TObject", null, function () {',
  13175. ' this.$init = function () {',
  13176. ' this.ClassType = null;',
  13177. ' };',
  13178. ' this.$final = function () {',
  13179. ' this.ClassType = undefined;',
  13180. ' };',
  13181. '});',
  13182. 'this.Obj = null;',
  13183. 'this.C = null;'
  13184. ]),
  13185. LinesToStr([ // $mod.$main
  13186. '$mod.C = null;',
  13187. '$mod.C = $mod.Obj.ClassType;',
  13188. '']));
  13189. end;
  13190. procedure TTestModule.TestClassOf_Is;
  13191. begin
  13192. StartProgram(false);
  13193. Add('type');
  13194. Add(' TClass = class of TObject;');
  13195. Add(' TObject = class');
  13196. Add(' end;');
  13197. Add(' TCar = class');
  13198. Add(' end;');
  13199. Add(' TCars = class of TCar;');
  13200. Add('var');
  13201. Add(' Obj: tobject;');
  13202. Add(' C: tclass;');
  13203. Add(' Cars: tcars;');
  13204. Add('begin');
  13205. Add(' if c is tcar then ;');
  13206. Add(' if c is tcars then ;');
  13207. ConvertProgram;
  13208. CheckSource('TestClassOf_Is',
  13209. LinesToStr([ // statements
  13210. 'rtl.createClass($mod, "TObject", null, function () {',
  13211. ' this.$init = function () {',
  13212. ' };',
  13213. ' this.$final = function () {',
  13214. ' };',
  13215. '});',
  13216. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  13217. '});',
  13218. 'this.Obj = null;',
  13219. 'this.C = null;',
  13220. 'this.Cars = null;'
  13221. ]),
  13222. LinesToStr([ // $mod.$main
  13223. 'if(rtl.is($mod.C,$mod.TCar));',
  13224. 'if(rtl.is($mod.C,$mod.TCar));',
  13225. '']));
  13226. end;
  13227. procedure TTestModule.TestClassOf_Compare;
  13228. begin
  13229. StartProgram(false);
  13230. Add('type');
  13231. Add(' TClass = class of TObject;');
  13232. Add(' TObject = class');
  13233. Add(' ClassType: TClass; ');
  13234. Add(' end;');
  13235. Add('var');
  13236. Add(' b: boolean;');
  13237. Add(' Obj: tobject;');
  13238. Add(' C: tclass;');
  13239. Add('begin');
  13240. Add(' b:=c=nil;');
  13241. Add(' b:=nil=c;');
  13242. Add(' b:=c=obj.classtype;');
  13243. Add(' b:=obj.classtype=c;');
  13244. Add(' b:=c=TObject;');
  13245. Add(' b:=TObject=c;');
  13246. Add(' b:=c<>nil;');
  13247. Add(' b:=nil<>c;');
  13248. Add(' b:=c<>obj.classtype;');
  13249. Add(' b:=obj.classtype<>c;');
  13250. Add(' b:=c<>TObject;');
  13251. Add(' b:=TObject<>c;');
  13252. ConvertProgram;
  13253. CheckSource('TestClassOf_Compare',
  13254. LinesToStr([ // statements
  13255. 'rtl.createClass($mod, "TObject", null, function () {',
  13256. ' this.$init = function () {',
  13257. ' this.ClassType = null;',
  13258. ' };',
  13259. ' this.$final = function () {',
  13260. ' this.ClassType = undefined;',
  13261. ' };',
  13262. '});',
  13263. 'this.b = false;',
  13264. 'this.Obj = null;',
  13265. 'this.C = null;'
  13266. ]),
  13267. LinesToStr([ // $mod.$main
  13268. '$mod.b = $mod.C === null;',
  13269. '$mod.b = null === $mod.C;',
  13270. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  13271. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  13272. '$mod.b = $mod.C === $mod.TObject;',
  13273. '$mod.b = $mod.TObject === $mod.C;',
  13274. '$mod.b = $mod.C !== null;',
  13275. '$mod.b = null !== $mod.C;',
  13276. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  13277. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  13278. '$mod.b = $mod.C !== $mod.TObject;',
  13279. '$mod.b = $mod.TObject !== $mod.C;',
  13280. '']));
  13281. end;
  13282. procedure TTestModule.TestClassOf_ClassVar;
  13283. begin
  13284. StartProgram(false);
  13285. Add('type');
  13286. Add(' TObject = class');
  13287. Add(' class var id: longint;');
  13288. Add(' end;');
  13289. Add(' TClass = class of TObject;');
  13290. Add('var');
  13291. Add(' C: tclass;');
  13292. Add('begin');
  13293. Add(' C.id:=C.id;');
  13294. ConvertProgram;
  13295. CheckSource('TestClassOf_ClassVar',
  13296. LinesToStr([ // statements
  13297. 'rtl.createClass($mod, "TObject", null, function () {',
  13298. ' this.id = 0;',
  13299. ' this.$init = function () {',
  13300. ' };',
  13301. ' this.$final = function () {',
  13302. ' };',
  13303. '});',
  13304. 'this.C = null;'
  13305. ]),
  13306. LinesToStr([ // $mod.$main
  13307. '$mod.TObject.id = $mod.C.id;',
  13308. '']));
  13309. end;
  13310. procedure TTestModule.TestClassOf_ClassMethod;
  13311. begin
  13312. StartProgram(false);
  13313. Add('type');
  13314. Add(' TObject = class');
  13315. Add(' class function DoIt(i: longint = 0): longint;');
  13316. Add(' end;');
  13317. Add(' TClass = class of TObject;');
  13318. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  13319. Add('var');
  13320. Add(' i: longint;');
  13321. Add(' C: tclass;');
  13322. Add('begin');
  13323. Add(' C.DoIt;');
  13324. Add(' C.DoIt();');
  13325. Add(' i:=C.DoIt;');
  13326. Add(' i:=C.DoIt();');
  13327. ConvertProgram;
  13328. CheckSource('TestClassOf_ClassMethod',
  13329. LinesToStr([ // statements
  13330. 'rtl.createClass($mod, "TObject", null, function () {',
  13331. ' this.$init = function () {',
  13332. ' };',
  13333. ' this.$final = function () {',
  13334. ' };',
  13335. ' this.DoIt = function (i) {',
  13336. ' var Result = 0;',
  13337. ' return Result;',
  13338. ' };',
  13339. '});',
  13340. 'this.i = 0;',
  13341. 'this.C = null;'
  13342. ]),
  13343. LinesToStr([ // $mod.$main
  13344. '$mod.C.DoIt(0);',
  13345. '$mod.C.DoIt(0);',
  13346. '$mod.i = $mod.C.DoIt(0);',
  13347. '$mod.i = $mod.C.DoIt(0);',
  13348. '']));
  13349. end;
  13350. procedure TTestModule.TestClassOf_ClassProperty;
  13351. begin
  13352. StartProgram(false);
  13353. Add('type');
  13354. Add(' TObject = class');
  13355. Add(' class var FA: longint;');
  13356. Add(' class function GetA: longint;');
  13357. Add(' class procedure SetA(Value: longint);');
  13358. Add(' class property pA: longint read fa write fa;');
  13359. Add(' class property pB: longint read geta write seta;');
  13360. Add(' end;');
  13361. Add(' TObjectClass = class of tobject;');
  13362. Add('class function tobject.geta: longint; begin end;');
  13363. Add('class procedure tobject.seta(value: longint); begin end;');
  13364. Add('var');
  13365. Add(' b: boolean;');
  13366. Add(' Obj: tobject;');
  13367. Add(' Cla: tobjectclass;');
  13368. Add('begin');
  13369. Add(' obj.pa:=obj.pa;');
  13370. Add(' obj.pb:=obj.pb;');
  13371. Add(' b:=obj.pa=4;');
  13372. Add(' b:=obj.pb=obj.pb;');
  13373. Add(' b:=5=obj.pa;');
  13374. Add(' cla.pa:=6;');
  13375. Add(' cla.pa:=cla.pa;');
  13376. Add(' cla.pb:=cla.pb;');
  13377. Add(' b:=cla.pa=7;');
  13378. Add(' b:=cla.pb=cla.pb;');
  13379. Add(' b:=8=cla.pa;');
  13380. Add(' tobject.pa:=9;');
  13381. Add(' tobject.pb:=tobject.pb;');
  13382. Add(' b:=tobject.pa=10;');
  13383. Add(' b:=11=tobject.pa;');
  13384. ConvertProgram;
  13385. CheckSource('TestClassOf_ClassProperty',
  13386. LinesToStr([ // statements
  13387. 'rtl.createClass($mod, "TObject", null, function () {',
  13388. ' this.FA = 0;',
  13389. ' this.$init = function () {',
  13390. ' };',
  13391. ' this.$final = function () {',
  13392. ' };',
  13393. ' this.GetA = function () {',
  13394. ' var Result = 0;',
  13395. ' return Result;',
  13396. ' };',
  13397. ' this.SetA = function (Value) {',
  13398. ' };',
  13399. '});',
  13400. 'this.b = false;',
  13401. 'this.Obj = null;',
  13402. 'this.Cla = null;'
  13403. ]),
  13404. LinesToStr([ // $mod.$main
  13405. '$mod.Obj.$class.FA = $mod.Obj.FA;',
  13406. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  13407. '$mod.b = $mod.Obj.FA === 4;',
  13408. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  13409. '$mod.b = 5 === $mod.Obj.FA;',
  13410. '$mod.Cla.FA = 6;',
  13411. '$mod.Cla.FA = $mod.Cla.FA;',
  13412. '$mod.Cla.SetA($mod.Cla.GetA());',
  13413. '$mod.b = $mod.Cla.FA === 7;',
  13414. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  13415. '$mod.b = 8 === $mod.Cla.FA;',
  13416. '$mod.TObject.FA = 9;',
  13417. '$mod.TObject.SetA($mod.TObject.GetA());',
  13418. '$mod.b = $mod.TObject.FA === 10;',
  13419. '$mod.b = 11 === $mod.TObject.FA;',
  13420. '']));
  13421. end;
  13422. procedure TTestModule.TestClassOf_ClassMethodSelf;
  13423. begin
  13424. StartProgram(false);
  13425. Add('type');
  13426. Add(' TObject = class');
  13427. Add(' class var GlobalId: longint;');
  13428. Add(' class procedure ProcA;');
  13429. Add(' end;');
  13430. Add('class procedure tobject.proca;');
  13431. Add('var b: boolean;');
  13432. Add('begin');
  13433. Add(' b:=self=nil;');
  13434. Add(' b:=self.globalid=3;');
  13435. Add(' b:=4=self.globalid;');
  13436. Add(' self.globalid:=5;');
  13437. Add(' self.proca;');
  13438. Add('end;');
  13439. Add('begin');
  13440. ConvertProgram;
  13441. CheckSource('TestClassOf_ClassMethodSelf',
  13442. LinesToStr([ // statements
  13443. 'rtl.createClass($mod, "TObject", null, function () {',
  13444. ' this.GlobalId = 0;',
  13445. ' this.$init = function () {',
  13446. ' };',
  13447. ' this.$final = function () {',
  13448. ' };',
  13449. ' this.ProcA = function () {',
  13450. ' var b = false;',
  13451. ' b = this === null;',
  13452. ' b = this.GlobalId === 3;',
  13453. ' b = 4 === this.GlobalId;',
  13454. ' $mod.TObject.GlobalId = 5;',
  13455. ' this.ProcA();',
  13456. ' };',
  13457. '});'
  13458. ]),
  13459. LinesToStr([ // $mod.$main
  13460. '']));
  13461. end;
  13462. procedure TTestModule.TestClassOf_TypeCast;
  13463. begin
  13464. StartProgram(false);
  13465. Add('type');
  13466. Add(' TObject = class');
  13467. Add(' class procedure {#TObject_DoIt}DoIt;');
  13468. Add(' end;');
  13469. Add(' TClass = class of TObject;');
  13470. Add(' TMobile = class');
  13471. Add(' class procedure {#TMobile_DoIt}DoIt;');
  13472. Add(' end;');
  13473. Add(' TMobileClass = class of TMobile;');
  13474. Add(' TCar = class(TMobile)');
  13475. Add(' class procedure {#TCar_DoIt}DoIt;');
  13476. Add(' end;');
  13477. Add(' TCarClass = class of TCar;');
  13478. Add('class procedure TObject.DoIt;');
  13479. Add('begin');
  13480. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  13481. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  13482. Add('end;');
  13483. Add('class procedure TMobile.DoIt;');
  13484. Add('begin');
  13485. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  13486. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  13487. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  13488. Add('end;');
  13489. Add('class procedure TCar.DoIt; begin end;');
  13490. Add('var');
  13491. Add(' ObjC: TClass;');
  13492. Add(' MobileC: TMobileClass;');
  13493. Add(' CarC: TCarClass;');
  13494. Add('begin');
  13495. Add(' ObjC.{@TObject_DoIt}DoIt;');
  13496. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  13497. Add(' CarC.{@TCar_DoIt}DoIt;');
  13498. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  13499. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  13500. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  13501. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  13502. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  13503. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  13504. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  13505. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  13506. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  13507. ConvertProgram;
  13508. CheckSource('TestClassOf_TypeCast',
  13509. LinesToStr([ // statements
  13510. 'rtl.createClass($mod, "TObject", null, function () {',
  13511. ' this.$init = function () {',
  13512. ' };',
  13513. ' this.$final = function () {',
  13514. ' };',
  13515. ' this.DoIt = function () {',
  13516. ' this.DoIt();',
  13517. ' this.DoIt$1();',
  13518. ' };',
  13519. '});',
  13520. 'rtl.createClass($mod, "TMobile", $mod.TObject, function () {',
  13521. ' this.DoIt$1 = function () {',
  13522. ' this.DoIt();',
  13523. ' this.DoIt$1();',
  13524. ' this.DoIt$2();',
  13525. ' };',
  13526. '});',
  13527. 'rtl.createClass($mod, "TCar", $mod.TMobile, function () {',
  13528. ' this.DoIt$2 = function () {',
  13529. ' };',
  13530. '});',
  13531. 'this.ObjC = null;',
  13532. 'this.MobileC = null;',
  13533. 'this.CarC = null;',
  13534. '']),
  13535. LinesToStr([ // $mod.$main
  13536. '$mod.ObjC.DoIt();',
  13537. '$mod.MobileC.DoIt$1();',
  13538. '$mod.CarC.DoIt$2();',
  13539. '$mod.ObjC.DoIt();',
  13540. '$mod.ObjC.DoIt$1();',
  13541. '$mod.ObjC.DoIt$2();',
  13542. '$mod.MobileC.DoIt();',
  13543. '$mod.MobileC.DoIt$1();',
  13544. '$mod.MobileC.DoIt$2();',
  13545. '$mod.CarC.DoIt();',
  13546. '$mod.CarC.DoIt$1();',
  13547. '$mod.CarC.DoIt$2();',
  13548. '']));
  13549. end;
  13550. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  13551. begin
  13552. StartProgram(false);
  13553. Add('type');
  13554. Add(' TObject = class');
  13555. Add(' function CurNow: longint; ');
  13556. Add(' class function Now: longint; ');
  13557. Add(' end;');
  13558. Add('function TObject.CurNow: longint; begin end;');
  13559. Add('class function TObject.Now: longint; begin end;');
  13560. Add('var');
  13561. Add(' Obj: tobject;');
  13562. Add(' vI: longint;');
  13563. Add('begin');
  13564. Add(' obj.curnow;');
  13565. Add(' vi:=obj.curnow;');
  13566. Add(' tobject.now;');
  13567. Add(' vi:=tobject.now;');
  13568. ConvertProgram;
  13569. CheckSource('TestClassOf_ImplicitFunctionCall',
  13570. LinesToStr([ // statements
  13571. 'rtl.createClass($mod, "TObject", null, function () {',
  13572. ' this.$init = function () {',
  13573. ' };',
  13574. ' this.$final = function () {',
  13575. ' };',
  13576. ' this.CurNow = function () {',
  13577. ' var Result = 0;',
  13578. ' return Result;',
  13579. ' };',
  13580. ' this.Now = function () {',
  13581. ' var Result = 0;',
  13582. ' return Result;',
  13583. ' };',
  13584. '});',
  13585. 'this.Obj = null;',
  13586. 'this.vI = 0;',
  13587. '']),
  13588. LinesToStr([ // $mod.$main
  13589. '$mod.Obj.CurNow();',
  13590. '$mod.vI = $mod.Obj.CurNow();',
  13591. '$mod.TObject.Now();',
  13592. '$mod.vI = $mod.TObject.Now();',
  13593. '']));
  13594. end;
  13595. procedure TTestModule.TestClassOf_Const;
  13596. begin
  13597. StartProgram(false);
  13598. Add([
  13599. 'type',
  13600. ' TObject = class',
  13601. ' end;',
  13602. ' TBird = TObject;',
  13603. ' TBirds = class of TBird;',
  13604. ' TEagles = TBirds;',
  13605. ' THawk = class(TBird);',
  13606. 'const',
  13607. ' Hawk: TEagles = THawk;',
  13608. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  13609. ' TBird,',
  13610. ' THawk',
  13611. ' );',
  13612. 'begin']);
  13613. ConvertProgram;
  13614. CheckSource('TestClassOf_Const',
  13615. LinesToStr([ // statements
  13616. 'rtl.createClass($mod, "TObject", null, function () {',
  13617. ' this.$init = function () {',
  13618. ' };',
  13619. ' this.$final = function () {',
  13620. ' };',
  13621. '});',
  13622. 'rtl.createClass($mod, "THawk", $mod.TObject, function () {',
  13623. '});',
  13624. 'this.Hawk = $mod.THawk;',
  13625. 'this.DefaultBirdClasses = [$mod.TObject, $mod.THawk];',
  13626. '']),
  13627. LinesToStr([ // $mod.$main
  13628. '']));
  13629. end;
  13630. procedure TTestModule.TestNestedClass_Alias;
  13631. begin
  13632. Converter.Options:=Converter.Options-[coNoTypeInfo];
  13633. StartProgram(false);
  13634. Add([
  13635. 'type',
  13636. ' TObject = class',
  13637. ' type TNested = type longint;',
  13638. ' end;',
  13639. 'type TAlias = type tobject.tnested;',
  13640. 'var i: tobject.tnested = 3;',
  13641. 'var j: TAlias = 4;',
  13642. 'begin',
  13643. ' if typeinfo(TAlias)=nil then ;',
  13644. ' if typeinfo(tobject.tnested)=nil then ;',
  13645. '']);
  13646. ConvertProgram;
  13647. CheckSource('TestNestedClass_Alias',
  13648. LinesToStr([ // statements
  13649. 'rtl.createClass($mod, "TObject", null, function () {',
  13650. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  13651. ' this.$init = function () {',
  13652. ' };',
  13653. ' this.$final = function () {',
  13654. ' };',
  13655. '});',
  13656. '$mod.$rtti.$inherited("TAlias", $mod.$rtti["TObject.TNested"], {});',
  13657. 'this.i = 3;',
  13658. 'this.j = 4;',
  13659. '']),
  13660. LinesToStr([ // $mod.$main
  13661. 'if ($mod.$rtti["TAlias"] === null) ;',
  13662. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  13663. '']));
  13664. end;
  13665. procedure TTestModule.TestNestedClass_Record;
  13666. begin
  13667. Converter.Options:=Converter.Options-[coNoTypeInfo];
  13668. StartProgram(false);
  13669. Add([
  13670. 'type',
  13671. ' TObject = class',
  13672. ' type TPoint = record',
  13673. ' x,y: byte;',
  13674. ' end;',
  13675. ' procedure DoIt(t: TPoint);',
  13676. ' end;',
  13677. 'procedure tobject.DoIt(t: TPoint);',
  13678. 'var p: TPoint;',
  13679. 'begin',
  13680. ' t.x:=t.y;',
  13681. ' p:=t;',
  13682. 'end;',
  13683. 'var',
  13684. ' p: tobject.tpoint = (x:2; y:4);',
  13685. ' o: TObject;',
  13686. 'begin',
  13687. ' p:=p;',
  13688. ' o.doit(p);',
  13689. '']);
  13690. ConvertProgram;
  13691. CheckSource('TestNestedClass_Record',
  13692. LinesToStr([ // statements
  13693. 'rtl.createClass($mod, "TObject", null, function () {',
  13694. ' rtl.recNewT(this, "TPoint", function () {',
  13695. ' this.x = 0;',
  13696. ' this.y = 0;',
  13697. ' this.$eq = function (b) {',
  13698. ' return (this.x === b.x) && (this.y === b.y);',
  13699. ' };',
  13700. ' this.$assign = function (s) {',
  13701. ' this.x = s.x;',
  13702. ' this.y = s.y;',
  13703. ' return this;',
  13704. ' };',
  13705. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  13706. ' $r.addField("x", rtl.byte);',
  13707. ' $r.addField("y", rtl.byte);',
  13708. ' });',
  13709. ' this.$init = function () {',
  13710. ' };',
  13711. ' this.$final = function () {',
  13712. ' };',
  13713. ' this.DoIt = function (t) {',
  13714. ' var p = this.TPoint.$new();',
  13715. ' t.x = t.y;',
  13716. ' p.$assign(t);',
  13717. ' };',
  13718. '});',
  13719. 'this.p = $mod.TObject.TPoint.$clone({',
  13720. ' x: 2,',
  13721. ' y: 4',
  13722. '});',
  13723. 'this.o = null;',
  13724. '']),
  13725. LinesToStr([ // $mod.$main
  13726. '$mod.p.$assign($mod.p);',
  13727. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  13728. '']));
  13729. end;
  13730. procedure TTestModule.TestNestedClass_Class;
  13731. begin
  13732. Converter.Options:=Converter.Options-[coNoTypeInfo];
  13733. StartProgram(false);
  13734. Add([
  13735. 'type',
  13736. ' TObject = class end;',
  13737. ' TBird = class',
  13738. ' type TLeg = class',
  13739. ' FId: longint;',
  13740. ' constructor Create;',
  13741. ' function Create(i: longint): TLeg;',
  13742. ' end;',
  13743. ' function DoIt(b: TBird): Tleg;',
  13744. ' end;',
  13745. 'constructor tbird.tleg.create;',
  13746. 'begin',
  13747. ' FId:=3;',
  13748. 'end;',
  13749. 'function tbird.tleg.Create(i: longint): TLeg;',
  13750. 'begin',
  13751. ' Create;',
  13752. ' Result:=TLeg.Create;',
  13753. ' Result:=TBird.TLeg.Create;',
  13754. ' Result:=Create(3);',
  13755. ' FId:=i;',
  13756. 'end;',
  13757. 'function tbird.DoIt(b: tbird): tleg;',
  13758. 'begin',
  13759. ' Result.Create;',
  13760. ' Result:=TLeg.Create;',
  13761. ' Result:=TBird.TLeg.Create;',
  13762. ' Result:=Result.Create(3);',
  13763. 'end;',
  13764. 'var',
  13765. ' b: Tbird.tleg;',
  13766. 'begin',
  13767. ' b.Create;',
  13768. ' b:=TBird.TLeg.Create;',
  13769. ' b:=b.Create(3);',
  13770. '']);
  13771. ConvertProgram;
  13772. CheckSource('TestNestedClass_Class',
  13773. LinesToStr([ // statements
  13774. 'rtl.createClass($mod, "TObject", null, function () {',
  13775. ' this.$init = function () {',
  13776. ' };',
  13777. ' this.$final = function () {',
  13778. ' };',
  13779. '});',
  13780. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  13781. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  13782. ' this.$init = function () {',
  13783. ' $mod.TObject.$init.call(this);',
  13784. ' this.FId = 0;',
  13785. ' };',
  13786. ' this.Create = function () {',
  13787. ' this.FId = 3;',
  13788. ' return this;',
  13789. ' };',
  13790. ' this.Create$1 = function (i) {',
  13791. ' var Result = null;',
  13792. ' this.Create();',
  13793. ' Result = $mod.TBird.TLeg.$create("Create");',
  13794. ' Result = $mod.TBird.TLeg.$create("Create");',
  13795. ' Result = this.Create$1(3);',
  13796. ' this.FId = i;',
  13797. ' return Result;',
  13798. ' };',
  13799. ' });',
  13800. ' this.DoIt = function (b) {',
  13801. ' var Result = null;',
  13802. ' Result.Create();',
  13803. ' Result = this.TLeg.$create("Create");',
  13804. ' Result = $mod.TBird.TLeg.$create("Create");',
  13805. ' Result = Result.Create$1(3);',
  13806. ' return Result;',
  13807. ' };',
  13808. '});',
  13809. 'this.b = null;',
  13810. '']),
  13811. LinesToStr([ // $mod.$main
  13812. '$mod.b.Create();',
  13813. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  13814. '$mod.b = $mod.b.Create$1(3);',
  13815. '']));
  13816. end;
  13817. procedure TTestModule.TestExternalClass_Var;
  13818. begin
  13819. StartProgram(false);
  13820. Add([
  13821. '{$modeswitch externalclass}',
  13822. 'type',
  13823. ' TExtA = class external name ''ExtObj''',
  13824. ' Id: longint external name ''$Id'';',
  13825. ' B: longint;',
  13826. ' end;',
  13827. 'var Obj: TExtA;',
  13828. 'begin',
  13829. ' obj.id:=obj.id+1;',
  13830. ' obj.B:=obj.B+1;']);
  13831. ConvertProgram;
  13832. CheckSource('TestExternalClass_Var',
  13833. LinesToStr([ // statements
  13834. 'this.Obj = null;',
  13835. '']),
  13836. LinesToStr([ // $mod.$main
  13837. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  13838. '$mod.Obj.B = $mod.Obj.B + 1;',
  13839. '']));
  13840. end;
  13841. procedure TTestModule.TestExternalClass_Const;
  13842. begin
  13843. StartProgram(false);
  13844. Add([
  13845. '{$modeswitch externalclass}',
  13846. 'type',
  13847. ' TExtA = class external name ''ExtObj''',
  13848. ' const Two: longint = 2;',
  13849. ' const Three = 3;',
  13850. ' const Id: longint;',
  13851. ' end;',
  13852. ' TExtB = class external name ''ExtB''',
  13853. ' A: TExtA;',
  13854. ' end;',
  13855. 'var',
  13856. ' A: texta;',
  13857. ' B: textb;',
  13858. ' i: longint;',
  13859. 'begin',
  13860. ' i:=a.two;',
  13861. ' i:=texta.two;',
  13862. ' i:=a.three;',
  13863. ' i:=texta.three;',
  13864. ' i:=a.id;',
  13865. ' i:=texta.id;',
  13866. '']);
  13867. ConvertProgram;
  13868. CheckSource('TestExternalClass_Const',
  13869. LinesToStr([ // statements
  13870. 'this.A = null;',
  13871. 'this.B = null;',
  13872. 'this.i = 0;',
  13873. '']),
  13874. LinesToStr([ // $mod.$main
  13875. '$mod.i = 2;',
  13876. '$mod.i = 2;',
  13877. '$mod.i = 3;',
  13878. '$mod.i = 3;',
  13879. '$mod.i = $mod.A.Id;',
  13880. '$mod.i = ExtObj.Id;',
  13881. '']));
  13882. end;
  13883. procedure TTestModule.TestExternalClass_Dollar;
  13884. begin
  13885. StartProgram(false);
  13886. Add([
  13887. '{$modeswitch externalclass}',
  13888. 'type',
  13889. ' TExtA = class external name ''$''',
  13890. ' Id: longint external name ''$'';',
  13891. ' function Bla(i: longint): longint; external name ''$'';',
  13892. ' end;',
  13893. 'function dollar(k: longint): longint; external name ''$'';',
  13894. 'var Obj: TExtA;',
  13895. 'begin',
  13896. ' dollar(1);',
  13897. ' obj.id:=obj.id+2;',
  13898. ' obj.Bla(3);',
  13899. '']);
  13900. ConvertProgram;
  13901. CheckSource('TestExternalClass_Dollar',
  13902. LinesToStr([ // statements
  13903. 'this.Obj = null;',
  13904. '']),
  13905. LinesToStr([ // $mod.$main
  13906. '$(1);',
  13907. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  13908. '$mod.Obj.$(3);',
  13909. '']));
  13910. end;
  13911. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  13912. begin
  13913. StartProgram(false);
  13914. Add('{$modeswitch externalclass}');
  13915. Add('type');
  13916. Add(' TExtA = class external name ''ExtA''');
  13917. Add(' Id: longint external name ''$Id'';');
  13918. Add(' end;');
  13919. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  13920. Add(' Id: longint;');
  13921. Add(' end;');
  13922. Add('begin');
  13923. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  13924. ConvertProgram;
  13925. end;
  13926. procedure TTestModule.TestExternalClass_Method;
  13927. begin
  13928. StartProgram(false);
  13929. Add('{$modeswitch externalclass}');
  13930. Add('type');
  13931. Add(' TExtA = class external name ''ExtObj''');
  13932. Add(' procedure DoIt(Id: longint = 1); external name ''$Execute'';');
  13933. Add(' procedure DoSome(Id: longint = 1);');
  13934. Add(' end;');
  13935. Add('var Obj: texta;');
  13936. Add('begin');
  13937. Add(' obj.doit;');
  13938. Add(' obj.doit();');
  13939. Add(' obj.doit(2);');
  13940. Add(' with obj do begin');
  13941. Add(' doit;');
  13942. Add(' doit();');
  13943. Add(' doit(3);');
  13944. Add(' end;');
  13945. ConvertProgram;
  13946. CheckSource('TestExternalClass_Method',
  13947. LinesToStr([ // statements
  13948. 'this.Obj = null;',
  13949. '']),
  13950. LinesToStr([ // $mod.$main
  13951. '$mod.Obj.$Execute(1);',
  13952. '$mod.Obj.$Execute(1);',
  13953. '$mod.Obj.$Execute(2);',
  13954. 'var $with1 = $mod.Obj;',
  13955. '$with1.$Execute(1);',
  13956. '$with1.$Execute(1);',
  13957. '$with1.$Execute(3);',
  13958. '']));
  13959. end;
  13960. procedure TTestModule.TestExternalClass_ClassMethod;
  13961. begin
  13962. StartProgram(false);
  13963. Add([
  13964. '{$modeswitch externalclass}',
  13965. 'type',
  13966. ' TExtA = class external name ''ExtObj''',
  13967. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  13968. ' end;',
  13969. ' TExtB = TExtA;',
  13970. 'begin',
  13971. ' texta.doit;',
  13972. ' texta.doit();',
  13973. ' texta.doit(2);',
  13974. ' with texta do begin',
  13975. ' doit;',
  13976. ' doit();',
  13977. ' doit(3);',
  13978. ' end;',
  13979. ' textb.doit;',
  13980. ' textb.doit();',
  13981. ' textb.doit(4);',
  13982. ' with textb do begin',
  13983. ' doit;',
  13984. ' doit();',
  13985. ' doit(5);',
  13986. ' end;',
  13987. '']);
  13988. ConvertProgram;
  13989. CheckSource('TestExternalClass_ClassMethod',
  13990. LinesToStr([ // statements
  13991. '']),
  13992. LinesToStr([ // $mod.$main
  13993. 'ExtObj.$Execute(1);',
  13994. 'ExtObj.$Execute(1);',
  13995. 'ExtObj.$Execute(2);',
  13996. 'ExtObj.$Execute(1);',
  13997. 'ExtObj.$Execute(1);',
  13998. 'ExtObj.$Execute(3);',
  13999. 'ExtObj.$Execute(1);',
  14000. 'ExtObj.$Execute(1);',
  14001. 'ExtObj.$Execute(4);',
  14002. 'ExtObj.$Execute(1);',
  14003. 'ExtObj.$Execute(1);',
  14004. 'ExtObj.$Execute(5);',
  14005. '']));
  14006. end;
  14007. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  14008. begin
  14009. StartProgram(false);
  14010. Add([
  14011. '{$modeswitch externalclass}',
  14012. 'type',
  14013. ' TBird = class external name ''Array''',
  14014. ' end;',
  14015. 'function GetPtr: Pointer;',
  14016. 'begin',
  14017. 'end;',
  14018. 'procedure Write(const p);',
  14019. 'begin',
  14020. 'end;',
  14021. 'procedure WriteLn; varargs;',
  14022. 'begin',
  14023. 'end;',
  14024. 'begin',
  14025. ' if TBird(GetPtr)=nil then ;',
  14026. ' Write(GetPtr);',
  14027. ' WriteLn(GetPtr);',
  14028. ' Write(TBird(GetPtr));',
  14029. ' WriteLn(TBird(GetPtr));',
  14030. '']);
  14031. ConvertProgram;
  14032. CheckSource('TestFunctionResultInTypeCast',
  14033. LinesToStr([ // statements
  14034. 'this.GetPtr = function () {',
  14035. ' var Result = null;',
  14036. ' return Result;',
  14037. '};',
  14038. 'this.Write = function (p) {',
  14039. '};',
  14040. 'this.WriteLn = function () {',
  14041. '};',
  14042. '']),
  14043. LinesToStr([
  14044. 'if ($mod.GetPtr() === null) ;',
  14045. '$mod.Write($mod.GetPtr());',
  14046. '$mod.WriteLn($mod.GetPtr());',
  14047. '$mod.Write($mod.GetPtr());',
  14048. '$mod.WriteLn($mod.GetPtr());',
  14049. '']));
  14050. end;
  14051. procedure TTestModule.TestExternalClass_NonExternalOverride;
  14052. begin
  14053. StartProgram(false);
  14054. Add('{$modeswitch externalclass}');
  14055. Add('type');
  14056. Add(' TExtA = class external name ''ExtObjA''');
  14057. Add(' procedure ProcA; virtual;');
  14058. Add(' procedure ProcB; virtual;');
  14059. Add(' end;');
  14060. Add(' TExtB = class external name ''ExtObjB'' (TExtA)');
  14061. Add(' end;');
  14062. Add(' TExtC = class (TExtB)');
  14063. Add(' procedure ProcA; override;');
  14064. Add(' end;');
  14065. Add('procedure TExtC.ProcA;');
  14066. Add('begin');
  14067. Add(' ProcA;');
  14068. Add(' Self.ProcA;');
  14069. Add(' ProcB;');
  14070. Add(' Self.ProcB;');
  14071. Add('end;');
  14072. Add('var');
  14073. Add(' A: texta;');
  14074. Add(' B: textb;');
  14075. Add(' C: textc;');
  14076. Add('begin');
  14077. Add(' a.proca;');
  14078. Add(' b.proca;');
  14079. Add(' c.proca;');
  14080. ConvertProgram;
  14081. CheckSource('TestExternalClass_NonExternalOverride',
  14082. LinesToStr([ // statements
  14083. 'rtl.createClassExt($mod, "TExtC", ExtObjB, "", function () {',
  14084. ' this.$init = function () {',
  14085. ' };',
  14086. ' this.$final = function () {',
  14087. ' };',
  14088. ' this.ProcA = function () {',
  14089. ' this.ProcA();',
  14090. ' this.ProcA();',
  14091. ' this.ProcB();',
  14092. ' this.ProcB();',
  14093. ' };',
  14094. '});',
  14095. 'this.A = null;',
  14096. 'this.B = null;',
  14097. 'this.C = null;',
  14098. '']),
  14099. LinesToStr([ // $mod.$main
  14100. '$mod.A.ProcA();',
  14101. '$mod.B.ProcA();',
  14102. '$mod.C.ProcA();',
  14103. '']));
  14104. end;
  14105. procedure TTestModule.TestExternalClass_OverloadHint;
  14106. begin
  14107. StartProgram(false);
  14108. Add([
  14109. '{$modeswitch externalclass}',
  14110. 'type',
  14111. ' TExtA = class external name ''ExtObjA''',
  14112. ' procedure DoIt;',
  14113. ' procedure DoIt(i: longint);',
  14114. ' end;',
  14115. 'begin',
  14116. '']);
  14117. ConvertProgram;
  14118. CheckResolverUnexpectedHints(true);
  14119. CheckSource('TestExternalClass_OverloadHint',
  14120. LinesToStr([ // statements
  14121. '']),
  14122. LinesToStr([ // $mod.$main
  14123. '']));
  14124. end;
  14125. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  14126. begin
  14127. StartProgram(false);
  14128. Add([
  14129. '{$modeswitch externalclass}',
  14130. 'type',
  14131. ' JSwiper = class external name ''Swiper''',
  14132. ' constructor New;',
  14133. ' end;',
  14134. ' TObject = class',
  14135. ' private',
  14136. ' FSwiper: JSwiper;',
  14137. ' published',
  14138. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  14139. ' end;',
  14140. 'begin',
  14141. ' JSwiper.new;',
  14142. '']);
  14143. ConvertProgram;
  14144. CheckSource('TestExternalClass_SameNamePublishedProperty',
  14145. LinesToStr([ // statements
  14146. 'rtl.createClass($mod, "TObject", null, function () {',
  14147. ' this.$init = function () {',
  14148. ' this.FSwiper = null;',
  14149. ' };',
  14150. ' this.$final = function () {',
  14151. ' this.FSwiper = undefined;',
  14152. ' };',
  14153. ' var $r = this.$rtti;',
  14154. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  14155. '});',
  14156. '']),
  14157. LinesToStr([ // $mod.$main
  14158. 'new Swiper();',
  14159. '']));
  14160. end;
  14161. procedure TTestModule.TestExternalClass_Property;
  14162. begin
  14163. StartProgram(false);
  14164. Add([
  14165. '{$modeswitch externalclass}',
  14166. 'type',
  14167. ' TExtA = class external name ''ExtA''',
  14168. ' function getYear: longint;',
  14169. ' procedure setYear(Value: longint);',
  14170. ' property Year: longint read getyear write setyear;',
  14171. ' end;',
  14172. ' TExtB = class (TExtA)',
  14173. ' procedure OtherSetYear(Value: longint);',
  14174. ' property year write othersetyear;',
  14175. ' end;',
  14176. 'procedure textb.othersetyear(value: longint);',
  14177. 'begin',
  14178. ' setYear(Value+4);',
  14179. 'end;',
  14180. 'var',
  14181. ' A: texta;',
  14182. ' B: textb;',
  14183. 'begin',
  14184. ' a.year:=a.year+1;',
  14185. ' b.year:=b.year+2;']);
  14186. ConvertProgram;
  14187. CheckSource('TestExternalClass_NonExternalOverride',
  14188. LinesToStr([ // statements
  14189. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  14190. ' this.$init = function () {',
  14191. ' };',
  14192. ' this.$final = function () {',
  14193. ' };',
  14194. ' this.OtherSetYear = function (Value) {',
  14195. ' this.setYear(Value+4);',
  14196. ' };',
  14197. '});',
  14198. 'this.A = null;',
  14199. 'this.B = null;',
  14200. '']),
  14201. LinesToStr([ // $mod.$main
  14202. '$mod.A.setYear($mod.A.getYear()+1);',
  14203. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  14204. '']));
  14205. end;
  14206. procedure TTestModule.TestExternalClass_PropertyDate;
  14207. begin
  14208. StartProgram(false);
  14209. Add([
  14210. '{$modeswitch externalclass}',
  14211. 'type',
  14212. ' TExtA = class external name ''ExtA''',
  14213. ' end;',
  14214. ' TExtB = class (TExtA)',
  14215. ' FDate: string;',
  14216. ' property Date: string read FDate write FDate;',
  14217. ' property ExtA: string read FDate write FDate;',
  14218. ' end;',
  14219. ' {$M+}',
  14220. ' TObject = class',
  14221. ' FDate: string;',
  14222. ' published',
  14223. ' property Date: string read FDate write FDate;',
  14224. ' property ExtA: string read FDate write FDate;',
  14225. ' end;',
  14226. 'var',
  14227. ' B: textb;',
  14228. ' o: TObject;',
  14229. 'begin',
  14230. ' b.date:=b.exta;',
  14231. ' o.date:=o.exta;']);
  14232. ConvertProgram;
  14233. CheckSource('TestExternalClass_PropertyDate',
  14234. LinesToStr([ // statements
  14235. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  14236. ' this.$init = function () {',
  14237. ' this.FDate = "";',
  14238. ' };',
  14239. ' this.$final = function () {',
  14240. ' };',
  14241. '});',
  14242. 'rtl.createClass($mod, "TObject", null, function () {',
  14243. ' this.$init = function () {',
  14244. ' this.FDate = "";',
  14245. ' };',
  14246. ' this.$final = function () {',
  14247. ' };',
  14248. ' var $r = this.$rtti;',
  14249. ' $r.addField("FDate", rtl.string);',
  14250. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  14251. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  14252. '});',
  14253. 'this.B = null;',
  14254. 'this.o = null;',
  14255. '']),
  14256. LinesToStr([ // $mod.$main
  14257. '$mod.B.FDate = $mod.B.FDate;',
  14258. '$mod.o.FDate = $mod.o.FDate;',
  14259. '']));
  14260. end;
  14261. procedure TTestModule.TestExternalClass_ClassProperty;
  14262. begin
  14263. StartProgram(false);
  14264. Add('{$modeswitch externalclass}');
  14265. Add('type');
  14266. Add(' TExtA = class external name ''ExtA''');
  14267. Add(' class function getYear: longint;');
  14268. Add(' class procedure setYear(Value: longint);');
  14269. Add(' class property Year: longint read getyear write setyear;');
  14270. Add(' end;');
  14271. Add(' TExtB = class (TExtA)');
  14272. Add(' class function GetCentury: longint;');
  14273. Add(' class procedure SetCentury(Value: longint);');
  14274. Add(' class property Century: longint read getcentury write setcentury;');
  14275. Add(' end;');
  14276. Add('class function textb.getcentury: longint;');
  14277. Add('begin');
  14278. Add('end;');
  14279. Add('class procedure textb.setcentury(value: longint);');
  14280. Add('begin');
  14281. Add(' setyear(value+11);');
  14282. Add(' texta.year:=texta.year+12;');
  14283. Add(' year:=year+13;');
  14284. Add(' textb.century:=textb.century+14;');
  14285. Add(' century:=century+15;');
  14286. Add('end;');
  14287. Add('var');
  14288. Add(' A: texta;');
  14289. Add(' B: textb;');
  14290. Add('begin');
  14291. Add(' texta.year:=texta.year+1;');
  14292. Add(' textb.year:=textb.year+2;');
  14293. Add(' TextA.year:=TextA.year+3;');
  14294. Add(' b.year:=b.year+4;');
  14295. Add(' textb.century:=textb.century+5;');
  14296. Add(' b.century:=b.century+6;');
  14297. ConvertProgram;
  14298. CheckSource('TestExternalClass_ClassProperty',
  14299. LinesToStr([ // statements
  14300. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  14301. ' this.$init = function () {',
  14302. ' };',
  14303. ' this.$final = function () {',
  14304. ' };',
  14305. ' this.GetCentury = function () {',
  14306. ' var Result = 0;',
  14307. ' return Result;',
  14308. ' };',
  14309. ' this.SetCentury = function (Value) {',
  14310. ' this.setYear(Value + 11);',
  14311. ' ExtA.setYear(ExtA.getYear() + 12);',
  14312. ' this.setYear(this.getYear() + 13);',
  14313. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  14314. ' this.SetCentury(this.GetCentury() + 15);',
  14315. ' };',
  14316. '});',
  14317. 'this.A = null;',
  14318. 'this.B = null;',
  14319. '']),
  14320. LinesToStr([ // $mod.$main
  14321. 'ExtA.setYear(ExtA.getYear() + 1);',
  14322. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  14323. 'ExtA.setYear(ExtA.getYear() + 3);',
  14324. '$mod.B.setYear($mod.B.getYear() + 4);',
  14325. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  14326. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  14327. '']));
  14328. end;
  14329. procedure TTestModule.TestExternalClass_ClassOf;
  14330. begin
  14331. StartProgram(false);
  14332. Add('{$modeswitch externalclass}');
  14333. Add('type');
  14334. Add(' TExtA = class external name ''ExtA''');
  14335. Add(' procedure ProcA; virtual;');
  14336. Add(' procedure ProcB; virtual;');
  14337. Add(' end;');
  14338. Add(' TExtAClass = class of TExtA;');
  14339. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  14340. Add(' end;');
  14341. Add(' TExtBClass = class of TExtB;');
  14342. Add(' TExtC = class (TExtB)');
  14343. Add(' procedure ProcA; override;');
  14344. Add(' end;');
  14345. Add(' TExtCClass = class of TExtC;');
  14346. Add('procedure TExtC.ProcA; begin end;');
  14347. Add('var');
  14348. Add(' A: texta; ClA: TExtAClass;');
  14349. Add(' B: textb; ClB: TExtBClass;');
  14350. Add(' C: textc; ClC: TExtCClass;');
  14351. Add('begin');
  14352. Add(' ClA:=texta;');
  14353. Add(' ClA:=textb;');
  14354. Add(' ClA:=textc;');
  14355. Add(' ClB:=textb;');
  14356. Add(' ClB:=textc;');
  14357. Add(' ClC:=textc;');
  14358. ConvertProgram;
  14359. CheckSource('TestExternalClass_ClassOf',
  14360. LinesToStr([ // statements
  14361. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  14362. ' this.$init = function () {',
  14363. ' };',
  14364. ' this.$final = function () {',
  14365. ' };',
  14366. ' this.ProcA = function () {',
  14367. ' };',
  14368. '});',
  14369. 'this.A = null;',
  14370. 'this.ClA = null;',
  14371. 'this.B = null;',
  14372. 'this.ClB = null;',
  14373. 'this.C = null;',
  14374. 'this.ClC = null;',
  14375. '']),
  14376. LinesToStr([ // $mod.$main
  14377. '$mod.ClA = ExtA;',
  14378. '$mod.ClA = ExtB;',
  14379. '$mod.ClA = $mod.TExtC;',
  14380. '$mod.ClB = ExtB;',
  14381. '$mod.ClB = $mod.TExtC;',
  14382. '$mod.ClC = $mod.TExtC;',
  14383. '']));
  14384. end;
  14385. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  14386. begin
  14387. AddModuleWithIntfImplSrc('unit2.pas',
  14388. LinesToStr([
  14389. '{$modeswitch externalclass}',
  14390. 'type',
  14391. ' TExtA = class external name ''ExtA''',
  14392. ' class var Id: longint;',
  14393. ' end;',
  14394. '']),
  14395. '');
  14396. StartUnit(true);
  14397. Add('interface');
  14398. Add('uses unit2;');
  14399. Add('implementation');
  14400. Add('begin');
  14401. Add(' unit2.texta.id:=unit2.texta.id+1;');
  14402. ConvertUnit;
  14403. CheckSource('TestExternalClass_ClassOtherUnit',
  14404. LinesToStr([
  14405. '']),
  14406. LinesToStr([
  14407. 'ExtA.Id = ExtA.Id + 1;',
  14408. '']));
  14409. end;
  14410. procedure TTestModule.TestExternalClass_Is;
  14411. begin
  14412. StartProgram(false);
  14413. Add('{$modeswitch externalclass}');
  14414. Add('type');
  14415. Add(' TExtA = class external name ''ExtA''');
  14416. Add(' end;');
  14417. Add(' TExtAClass = class of TExtA;');
  14418. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  14419. Add(' end;');
  14420. Add(' TExtBClass = class of TExtB;');
  14421. Add(' TExtC = class (TExtB)');
  14422. Add(' end;');
  14423. Add(' TExtCClass = class of TExtC;');
  14424. Add('var');
  14425. Add(' A: texta; ClA: TExtAClass;');
  14426. Add(' B: textb; ClB: TExtBClass;');
  14427. Add(' C: textc; ClC: TExtCClass;');
  14428. Add('begin');
  14429. Add(' if a is textb then ;');
  14430. Add(' if a is textc then ;');
  14431. Add(' if b is textc then ;');
  14432. Add(' if cla is textb then ;');
  14433. Add(' if cla is textc then ;');
  14434. Add(' if clb is textc then ;');
  14435. ConvertProgram;
  14436. CheckSource('TestExternalClass_Is',
  14437. LinesToStr([ // statements
  14438. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  14439. ' this.$init = function () {',
  14440. ' };',
  14441. ' this.$final = function () {',
  14442. ' };',
  14443. '});',
  14444. 'this.A = null;',
  14445. 'this.ClA = null;',
  14446. 'this.B = null;',
  14447. 'this.ClB = null;',
  14448. 'this.C = null;',
  14449. 'this.ClC = null;',
  14450. '']),
  14451. LinesToStr([ // $mod.$main
  14452. 'if (rtl.isExt($mod.A, ExtB)) ;',
  14453. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  14454. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  14455. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  14456. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  14457. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  14458. '']));
  14459. end;
  14460. procedure TTestModule.TestExternalClass_As;
  14461. begin
  14462. StartProgram(false);
  14463. Add('{$modeswitch externalclass}');
  14464. Add('type');
  14465. Add(' TExtA = class external name ''ExtA''');
  14466. Add(' end;');
  14467. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  14468. Add(' end;');
  14469. Add(' TExtC = class (TExtB)');
  14470. Add(' end;');
  14471. Add('var');
  14472. Add(' A: texta;');
  14473. Add(' B: textb;');
  14474. Add(' C: textc;');
  14475. Add('begin');
  14476. Add(' b:=a as textb;');
  14477. Add(' c:=a as textc;');
  14478. Add(' c:=b as textc;');
  14479. ConvertProgram;
  14480. CheckSource('TestExternalClass_Is',
  14481. LinesToStr([ // statements
  14482. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  14483. ' this.$init = function () {',
  14484. ' };',
  14485. ' this.$final = function () {',
  14486. ' };',
  14487. '});',
  14488. 'this.A = null;',
  14489. 'this.B = null;',
  14490. 'this.C = null;',
  14491. '']),
  14492. LinesToStr([ // $mod.$main
  14493. '$mod.B = rtl.asExt($mod.A, ExtB);',
  14494. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  14495. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  14496. '']));
  14497. end;
  14498. procedure TTestModule.TestExternalClass_DestructorFail;
  14499. begin
  14500. StartProgram(false);
  14501. Add('{$modeswitch externalclass}');
  14502. Add('type');
  14503. Add(' TExtA = class external name ''ExtA''');
  14504. Add(' destructor Free;');
  14505. Add(' end;');
  14506. SetExpectedPasResolverError('Pascal element not supported: destructor',
  14507. nPasElementNotSupported);
  14508. ConvertProgram;
  14509. end;
  14510. procedure TTestModule.TestExternalClass_New;
  14511. begin
  14512. StartProgram(false);
  14513. Add('{$modeswitch externalclass}');
  14514. Add('type');
  14515. Add(' TExtA = class external name ''ExtA''');
  14516. Add(' constructor New;');
  14517. Add(' constructor New(i: longint; j: longint = 2);');
  14518. Add(' end;');
  14519. Add('var');
  14520. Add(' A: texta;');
  14521. Add('begin');
  14522. Add(' a:=texta.new;');
  14523. Add(' a:=texta.new();');
  14524. Add(' a:=texta.new(1);');
  14525. Add(' with texta do begin');
  14526. Add(' a:=new;');
  14527. Add(' a:=new();');
  14528. Add(' a:=new(2);');
  14529. Add(' end;');
  14530. Add(' a:=test1.texta.new;');
  14531. Add(' a:=test1.texta.new();');
  14532. Add(' a:=test1.texta.new(3);');
  14533. ConvertProgram;
  14534. CheckSource('TestExternalClass_New',
  14535. LinesToStr([ // statements
  14536. 'this.A = null;',
  14537. '']),
  14538. LinesToStr([ // $mod.$main
  14539. '$mod.A = new ExtA();',
  14540. '$mod.A = new ExtA();',
  14541. '$mod.A = new ExtA(1,2);',
  14542. '$mod.A = new ExtA();',
  14543. '$mod.A = new ExtA();',
  14544. '$mod.A = new ExtA(2,2);',
  14545. '$mod.A = new ExtA();',
  14546. '$mod.A = new ExtA();',
  14547. '$mod.A = new ExtA(3,2);',
  14548. '']));
  14549. end;
  14550. procedure TTestModule.TestExternalClass_ClassOf_New;
  14551. begin
  14552. StartProgram(false);
  14553. Add('{$modeswitch externalclass}');
  14554. Add('type');
  14555. Add(' TExtAClass = class of TExtA;');
  14556. Add(' TExtA = class external name ''ExtA''');
  14557. Add(' constructor New;');
  14558. Add(' end;');
  14559. Add('var');
  14560. Add(' A: texta;');
  14561. Add(' C: textaclass;');
  14562. Add('begin');
  14563. Add(' a:=c.new;');
  14564. Add(' a:=c.new();');
  14565. Add(' with C do begin');
  14566. Add(' a:=new;');
  14567. Add(' a:=new();');
  14568. Add(' end;');
  14569. Add(' a:=test1.c.new;');
  14570. Add(' a:=test1.c.new();');
  14571. ConvertProgram;
  14572. CheckSource('TestExternalClass_ClassOf_New',
  14573. LinesToStr([ // statements
  14574. 'this.A = null;',
  14575. 'this.C = null;',
  14576. '']),
  14577. LinesToStr([ // $mod.$main
  14578. '$mod.A = new $mod.C();',
  14579. '$mod.A = new $mod.C();',
  14580. 'var $with1 = $mod.C;',
  14581. '$mod.A = new $with1();',
  14582. '$mod.A = new $with1();',
  14583. '$mod.A = new $mod.C();',
  14584. '$mod.A = new $mod.C();',
  14585. '']));
  14586. end;
  14587. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  14588. begin
  14589. StartProgram(false);
  14590. Add([
  14591. '{$modeswitch externalclass}',
  14592. 'type',
  14593. ' TExtAClass = class of TExtA;',
  14594. ' TExtA = class external name ''ExtA''',
  14595. ' constructor New;',
  14596. ' end;',
  14597. 'function GetCreator: TExtAClass;',
  14598. 'begin',
  14599. ' Result:=TExtA;',
  14600. 'end;',
  14601. 'var',
  14602. ' A: texta;',
  14603. 'begin',
  14604. ' a:=getcreator.new;',
  14605. ' a:=getcreator().new;',
  14606. ' a:=getcreator().new();',
  14607. ' a:=getcreator.new();',
  14608. ' with getcreator do begin',
  14609. ' a:=new;',
  14610. ' a:=new();',
  14611. ' end;']);
  14612. ConvertProgram;
  14613. CheckSource('TestExternalClass_FuncClassOf_New',
  14614. LinesToStr([ // statements
  14615. 'this.GetCreator = function () {',
  14616. ' var Result = null;',
  14617. ' Result = ExtA;',
  14618. ' return Result;',
  14619. '};',
  14620. 'this.A = null;',
  14621. '']),
  14622. LinesToStr([ // $mod.$main
  14623. '$mod.A = new ($mod.GetCreator())();',
  14624. '$mod.A = new ($mod.GetCreator())();',
  14625. '$mod.A = new ($mod.GetCreator())();',
  14626. '$mod.A = new ($mod.GetCreator())();',
  14627. 'var $with1 = $mod.GetCreator();',
  14628. '$mod.A = new $with1();',
  14629. '$mod.A = new $with1();',
  14630. '']));
  14631. end;
  14632. procedure TTestModule.TestExternalClass_New_PasClassFail;
  14633. begin
  14634. StartProgram(false);
  14635. Add([
  14636. '{$modeswitch externalclass}',
  14637. 'type',
  14638. ' TExtA = class external name ''ExtA''',
  14639. ' constructor New;',
  14640. ' end;',
  14641. ' TBird = class(TExtA)',
  14642. ' end;',
  14643. 'begin',
  14644. ' TBird.new;',
  14645. '']);
  14646. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  14647. ConvertProgram;
  14648. end;
  14649. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  14650. begin
  14651. StartProgram(false);
  14652. Add([
  14653. '{$modeswitch externalclass}',
  14654. 'type',
  14655. ' TExtA = class external name ''ExtA''',
  14656. ' constructor New;',
  14657. ' end;',
  14658. ' TBird = class(TExtA)',
  14659. ' end;',
  14660. 'begin',
  14661. ' TBird.new();',
  14662. '']);
  14663. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  14664. ConvertProgram;
  14665. end;
  14666. procedure TTestModule.TestExternalClass_LocalConstSameName;
  14667. begin
  14668. StartProgram(false);
  14669. Add('{$modeswitch externalclass}');
  14670. Add('type');
  14671. Add(' TExtA = class external name ''ExtA''');
  14672. Add(' constructor New;');
  14673. Add(' end;');
  14674. Add('function DoIt: longint;');
  14675. Add('const ExtA: longint = 3;');
  14676. Add('begin');
  14677. Add(' Result:=ExtA;');
  14678. Add('end;');
  14679. Add('var');
  14680. Add(' A: texta;');
  14681. Add('begin');
  14682. Add(' a:=texta.new;');
  14683. ConvertProgram;
  14684. CheckSource('TestExternalClass_LocalConstSameName',
  14685. LinesToStr([ // statements
  14686. 'var ExtA$1 = 3;',
  14687. 'this.DoIt = function () {',
  14688. ' var Result = 0;',
  14689. ' Result = ExtA$1;',
  14690. ' return Result;',
  14691. '};',
  14692. 'this.A = null;',
  14693. '']),
  14694. LinesToStr([ // $mod.$main
  14695. '$mod.A = new ExtA();',
  14696. '']));
  14697. end;
  14698. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  14699. begin
  14700. StartProgram(false);
  14701. Add('{$modeswitch externalclass}');
  14702. Add('type');
  14703. Add(' TExtA = class external name ''ExtA''');
  14704. Add(' procedure DoIt;');
  14705. Add(' end;');
  14706. Add(' TMyA = class(TExtA)');
  14707. Add(' procedure DoIt;');
  14708. Add(' end;');
  14709. Add('procedure TMyA.DoIt; begin end;');
  14710. Add('begin');
  14711. ConvertProgram;
  14712. CheckSource('TestExternalClass_ReintroduceOverload',
  14713. LinesToStr([ // statements
  14714. 'rtl.createClassExt($mod, "TMyA", ExtA, "", function () {',
  14715. ' this.$init = function () {',
  14716. ' };',
  14717. ' this.$final = function () {',
  14718. ' };',
  14719. ' this.DoIt$1 = function () {',
  14720. ' };',
  14721. '});',
  14722. '']),
  14723. LinesToStr([ // $mod.$main
  14724. '']));
  14725. end;
  14726. procedure TTestModule.TestExternalClass_Inherited;
  14727. begin
  14728. StartProgram(false);
  14729. Add('{$modeswitch externalclass}');
  14730. Add('type');
  14731. Add(' TExtA = class external name ''ExtA''');
  14732. Add(' procedure DoIt(i: longint = 1); virtual;');
  14733. Add(' procedure DoSome(j: longint = 2);');
  14734. Add(' end;');
  14735. Add(' TExtB = class external name ''ExtB''(TExtA)');
  14736. Add(' end;');
  14737. Add(' TMyC = class(TExtB)');
  14738. Add(' procedure DoIt(i: longint = 1); override;');
  14739. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  14740. Add(' end;');
  14741. Add('procedure TMyC.DoIt(i: longint);');
  14742. Add('begin');
  14743. Add(' inherited;');
  14744. Add(' inherited DoIt;');
  14745. Add(' inherited DoIt();');
  14746. Add(' inherited DoIt(3);');
  14747. Add(' inherited DoSome;');
  14748. Add(' inherited DoSome();');
  14749. Add(' inherited DoSome(4);');
  14750. Add('end;');
  14751. Add('procedure TMyC.DoSome(j: longint);');
  14752. Add('begin');
  14753. Add(' inherited;');
  14754. Add('end;');
  14755. Add('begin');
  14756. ConvertProgram;
  14757. CheckSource('TestExternalClass_ReintroduceOverload',
  14758. LinesToStr([ // statements
  14759. 'rtl.createClassExt($mod, "TMyC", ExtB, "", function () {',
  14760. ' this.$init = function () {',
  14761. ' };',
  14762. ' this.$final = function () {',
  14763. ' };',
  14764. ' this.DoIt = function (i) {',
  14765. ' ExtB.DoIt.apply(this, arguments);',
  14766. ' ExtB.DoIt.call(this, 1);',
  14767. ' ExtB.DoIt.call(this, 1);',
  14768. ' ExtB.DoIt.call(this, 3);',
  14769. ' ExtB.DoSome.call(this, 2);',
  14770. ' ExtB.DoSome.call(this, 2);',
  14771. ' ExtB.DoSome.call(this, 4);',
  14772. ' };',
  14773. ' this.DoSome$1 = function (j) {',
  14774. ' ExtB.DoSome.apply(this, arguments);',
  14775. ' };',
  14776. '});',
  14777. '']),
  14778. LinesToStr([ // $mod.$main
  14779. '']));
  14780. end;
  14781. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  14782. begin
  14783. StartProgram(false);
  14784. Add('{$modeswitch externalclass}');
  14785. Add('type');
  14786. Add(' TObject = class');
  14787. Add(' end;');
  14788. Add(' TExtA = class external name ''ExtA''(TObject)');
  14789. Add(' end;');
  14790. Add('begin');
  14791. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  14792. ConvertProgram;
  14793. end;
  14794. procedure TTestModule.TestExternalClass_NewInstance;
  14795. begin
  14796. StartProgram(false);
  14797. Add('{$modeswitch externalclass}');
  14798. Add('type');
  14799. Add(' TExtA = class external name ''ExtA''');
  14800. Add(' end;');
  14801. Add(' TMyB = class(TExtA)');
  14802. Add(' protected');
  14803. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  14804. Add(' end;');
  14805. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  14806. Add('begin end;');
  14807. Add('begin');
  14808. ConvertProgram;
  14809. CheckSource('TestExternalClass_NewInstance',
  14810. LinesToStr([ // statements
  14811. 'rtl.createClassExt($mod, "TMyB", ExtA, "NewInstance", function () {',
  14812. ' this.$init = function () {',
  14813. ' };',
  14814. ' this.$final = function () {',
  14815. ' };',
  14816. ' this.NewInstance = function (fnname, paramarray) {',
  14817. ' var Result = null;',
  14818. ' return Result;',
  14819. ' };',
  14820. '});',
  14821. '']),
  14822. LinesToStr([ // $mod.$main
  14823. '']));
  14824. end;
  14825. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  14826. begin
  14827. StartProgram(false);
  14828. Add('{$modeswitch externalclass}');
  14829. Add('type');
  14830. Add(' TExtA = class external name ''ExtA''');
  14831. Add(' end;');
  14832. Add(' TMyB = class(TExtA)');
  14833. Add(' protected');
  14834. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  14835. Add(' end;');
  14836. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  14837. Add('begin end;');
  14838. Add('begin');
  14839. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  14840. ConvertProgram;
  14841. end;
  14842. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  14843. begin
  14844. StartProgram(false);
  14845. Add('{$modeswitch externalclass}');
  14846. Add('type');
  14847. Add(' TExtA = class external name ''ExtA''');
  14848. Add(' end;');
  14849. Add(' TMyB = class(TExtA)');
  14850. Add(' protected');
  14851. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  14852. Add(' end;');
  14853. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  14854. Add('begin end;');
  14855. Add('begin');
  14856. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Longint", expected "String"',
  14857. nIncompatibleTypeArgNo);
  14858. ConvertProgram;
  14859. end;
  14860. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  14861. begin
  14862. StartProgram(false);
  14863. Add('{$modeswitch externalclass}');
  14864. Add('type');
  14865. Add(' TExtA = class external name ''ExtA''');
  14866. Add(' end;');
  14867. Add(' TMyB = class(TExtA)');
  14868. Add(' protected');
  14869. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  14870. Add(' end;');
  14871. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  14872. Add('begin end;');
  14873. Add('begin');
  14874. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "type", expected "untyped"',
  14875. nIncompatibleTypeArgNo);
  14876. ConvertProgram;
  14877. end;
  14878. procedure TTestModule.TestExternalClass_PascalProperty;
  14879. begin
  14880. StartProgram(false);
  14881. Add('{$modeswitch externalclass}');
  14882. Add('type');
  14883. Add(' TJSElement = class;');
  14884. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  14885. Add(' TJSElement = class external name ''ExtA''');
  14886. Add(' end;');
  14887. Add(' TControl = class(TJSElement)');
  14888. Add(' private');
  14889. Add(' FOnClick: TJSNotifyEvent;');
  14890. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  14891. Add(' procedure Click(Sender: TJSElement);');
  14892. Add(' end;');
  14893. Add('procedure TControl.Click(Sender: TJSElement);');
  14894. Add('begin');
  14895. Add(' OnClick(Self);');
  14896. Add('end;');
  14897. Add('var');
  14898. Add(' Ctrl: TControl;');
  14899. Add('begin');
  14900. Add(' Ctrl.OnClick:[email protected];');
  14901. Add(' Ctrl.OnClick(Ctrl);');
  14902. ConvertProgram;
  14903. CheckSource('TestExternalClass_PascalProperty',
  14904. LinesToStr([ // statements
  14905. 'rtl.createClassExt($mod, "TControl", ExtA, "", function () {',
  14906. ' this.$init = function () {',
  14907. ' this.FOnClick = null;',
  14908. ' };',
  14909. ' this.$final = function () {',
  14910. ' this.FOnClick = undefined;',
  14911. ' };',
  14912. ' this.Click = function (Sender) {',
  14913. ' this.FOnClick(this);',
  14914. ' };',
  14915. '});',
  14916. 'this.Ctrl = null;',
  14917. '']),
  14918. LinesToStr([ // $mod.$main
  14919. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  14920. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  14921. '']));
  14922. end;
  14923. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  14924. begin
  14925. StartProgram(false);
  14926. Add([
  14927. '{$modeswitch externalclass}',
  14928. 'type',
  14929. ' IUnknown = interface end;',
  14930. ' TObject = class',
  14931. ' end;',
  14932. ' TChild = class',
  14933. ' end;',
  14934. ' TExtRootA = class external name ''ExtRootA''',
  14935. ' end;',
  14936. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  14937. ' end;',
  14938. ' TExtRootB = class external name ''ExtRootB''',
  14939. ' end;',
  14940. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  14941. ' end;',
  14942. 'var',
  14943. ' Obj: TObject;',
  14944. ' Child: TChild;',
  14945. ' RootA: TExtRootA;',
  14946. ' ChildA: TExtChildA;',
  14947. ' RootB: TExtRootB;',
  14948. ' ChildB: TExtChildB;',
  14949. ' i: IUnknown;',
  14950. 'begin',
  14951. ' obj:=tobject(roota);',
  14952. ' obj:=tobject(childa);',
  14953. ' child:=tchild(tobject(roota));',
  14954. ' roota:=textroota(obj);',
  14955. ' roota:=textroota(child);',
  14956. ' roota:=textroota(rootb);',
  14957. ' roota:=textroota(childb);',
  14958. ' childa:=textchilda(textroota(obj));',
  14959. ' roota:=TExtRootA(i)',
  14960. '']);
  14961. ConvertProgram;
  14962. CheckSource('TestExternalClass_TypeCastToRootClass',
  14963. LinesToStr([ // statements
  14964. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  14965. 'rtl.createClass($mod, "TObject", null, function () {',
  14966. ' this.$init = function () {',
  14967. ' };',
  14968. ' this.$final = function () {',
  14969. ' };',
  14970. '});',
  14971. 'rtl.createClass($mod, "TChild", $mod.TObject, function () {',
  14972. '});',
  14973. 'this.Obj = null;',
  14974. 'this.Child = null;',
  14975. 'this.RootA = null;',
  14976. 'this.ChildA = null;',
  14977. 'this.RootB = null;',
  14978. 'this.ChildB = null;',
  14979. 'this.i = null;',
  14980. '']),
  14981. LinesToStr([ // $mod.$main
  14982. '$mod.Obj = $mod.RootA;',
  14983. '$mod.Obj = $mod.ChildA;',
  14984. '$mod.Child = $mod.RootA;',
  14985. '$mod.RootA = $mod.Obj;',
  14986. '$mod.RootA = $mod.Child;',
  14987. '$mod.RootA = $mod.RootB;',
  14988. '$mod.RootA = $mod.ChildB;',
  14989. '$mod.ChildA = $mod.Obj;',
  14990. '$mod.RootA = $mod.i;',
  14991. '']));
  14992. end;
  14993. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  14994. begin
  14995. StartProgram(false);
  14996. Add([
  14997. '{$modeswitch externalclass}',
  14998. 'type',
  14999. ' IUnknown = interface end;',
  15000. ' IBird = interface(IUnknown) end;',
  15001. ' TClass = class of TObject;',
  15002. ' TObject = class',
  15003. ' end;',
  15004. ' TChild = class',
  15005. ' end;',
  15006. ' TJSObject = class external name ''Object''',
  15007. ' end;',
  15008. ' TRec = record end;',
  15009. 'var',
  15010. ' Obj: TObject;',
  15011. ' Child: TChild;',
  15012. ' i: IUnknown;',
  15013. ' Bird: IBird;',
  15014. ' j: TJSObject;',
  15015. ' r: TRec;',
  15016. ' c: TClass;',
  15017. 'begin',
  15018. ' j:=tjsobject(IUnknown);',
  15019. ' j:=tjsobject(IBird);',
  15020. ' j:=tjsobject(TObject);',
  15021. ' j:=tjsobject(TChild);',
  15022. ' j:=tjsobject(TRec);',
  15023. ' j:=tjsobject(Obj);',
  15024. ' j:=tjsobject(Child);',
  15025. ' j:=tjsobject(i);',
  15026. ' j:=tjsobject(Bird);',
  15027. ' j:=tjsobject(r);',
  15028. ' j:=tjsobject(c);',
  15029. '']);
  15030. ConvertProgram;
  15031. CheckSource('TestExternalClass_TypeCastToJSObject',
  15032. LinesToStr([ // statements
  15033. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  15034. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], $mod.IUnknown);',
  15035. 'rtl.createClass($mod, "TObject", null, function () {',
  15036. ' this.$init = function () {',
  15037. ' };',
  15038. ' this.$final = function () {',
  15039. ' };',
  15040. '});',
  15041. 'rtl.createClass($mod, "TChild", $mod.TObject, function () {',
  15042. '});',
  15043. 'rtl.recNewT($mod, "TRec", function () {',
  15044. ' this.$eq = function (b) {',
  15045. ' return true;',
  15046. ' };',
  15047. ' this.$assign = function (s) {',
  15048. ' return this;',
  15049. ' };',
  15050. '});',
  15051. 'this.Obj = null;',
  15052. 'this.Child = null;',
  15053. 'this.i = null;',
  15054. 'this.Bird = null;',
  15055. 'this.j = null;',
  15056. 'this.r = $mod.TRec.$new();',
  15057. 'this.c = null;',
  15058. '']),
  15059. LinesToStr([ // $mod.$main
  15060. '$mod.j = $mod.IUnknown;',
  15061. '$mod.j = $mod.IBird;',
  15062. '$mod.j = $mod.TObject;',
  15063. '$mod.j = $mod.TChild;',
  15064. '$mod.j = $mod.TRec;',
  15065. '$mod.j = $mod.Obj;',
  15066. '$mod.j = $mod.Child;',
  15067. '$mod.j = $mod.i;',
  15068. '$mod.j = $mod.Bird;',
  15069. '$mod.j = $mod.r;',
  15070. '$mod.j = $mod.c;',
  15071. '']));
  15072. end;
  15073. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  15074. begin
  15075. StartProgram(false);
  15076. Add('{$modeswitch externalclass}');
  15077. Add('type');
  15078. Add(' TJSString = class external name ''String''');
  15079. Add(' class function fromCharCode() : string; varargs;');
  15080. Add(' function anchor(const aName : string) : string;');
  15081. Add(' end;');
  15082. Add('var');
  15083. Add(' s: string;');
  15084. Add('begin');
  15085. Add(' s:=TJSString.fromCharCode(65,66);');
  15086. Add(' s:=TJSString(s).anchor(s);');
  15087. Add(' s:=TJSString(''foo'').anchor(s);');
  15088. ConvertProgram;
  15089. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  15090. LinesToStr([ // statements
  15091. 'this.s = "";',
  15092. '']),
  15093. LinesToStr([ // $mod.$main
  15094. '$mod.s = String.fromCharCode(65, 66);',
  15095. '$mod.s = $mod.s.anchor($mod.s);',
  15096. '$mod.s = "foo".anchor($mod.s);',
  15097. '']));
  15098. end;
  15099. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  15100. begin
  15101. StartProgram(false);
  15102. Add([
  15103. '{$modeswitch externalclass}',
  15104. 'type',
  15105. ' TJSObject = class external name ''Object'' end;',
  15106. ' TJSFunction = class external name ''Function''',
  15107. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  15108. ' function call(thisArg: TJSObject): JSValue; varargs;',
  15109. ' end;',
  15110. ' TObject = class',
  15111. ' procedure DoIt(i: longint);',
  15112. ' end;',
  15113. ' TFuncInt = function(o: TObject): longint;',
  15114. 'function GetIt(o: TObject): longint;',
  15115. ' procedure Sub; begin end;',
  15116. 'var',
  15117. ' f: TJSFunction;',
  15118. ' fi: TFuncInt;',
  15119. 'begin',
  15120. ' fi:=TFuncInt(f);',
  15121. ' f:=TJSFunction(fi);',
  15122. ' f:=TJSFunction(@GetIt);',
  15123. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  15124. ' f:=TJSFunction(@Sub);',
  15125. ' f:=TJSFunction(@o.doit);',
  15126. ' f:=TJSFunction(fi).bind(nil,4)',
  15127. 'end;',
  15128. 'procedure TObject.DoIt(i: longint);',
  15129. ' procedure Sub; begin end;',
  15130. 'var f: TJSFunction;',
  15131. 'begin',
  15132. ' f:=TJSFunction(@DoIt);',
  15133. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  15134. ' f:=TJSFunction(@Sub);',
  15135. ' f:=TJSFunction(@GetIt);',
  15136. 'end;',
  15137. 'begin']);
  15138. ConvertProgram;
  15139. CheckSource('TestExternalClass_TypeCastToJSFunction',
  15140. LinesToStr([ // statements
  15141. 'rtl.createClass($mod, "TObject", null, function () {',
  15142. ' this.$init = function () {',
  15143. ' };',
  15144. ' this.$final = function () {',
  15145. ' };',
  15146. ' this.DoIt = function (i) {',
  15147. ' var Self = this;',
  15148. ' function Sub() {',
  15149. ' };',
  15150. ' var f = null;',
  15151. ' f = rtl.createCallback(Self, "DoIt");',
  15152. ' f = rtl.createCallback(Self, "DoIt").bind(null, 13);',
  15153. ' f = Sub;',
  15154. ' f = $mod.GetIt;',
  15155. ' };',
  15156. '});',
  15157. 'this.GetIt = function (o) {',
  15158. ' var Result = 0;',
  15159. ' function Sub() {',
  15160. ' };',
  15161. ' var f = null;',
  15162. ' var fi = null;',
  15163. ' fi = f;',
  15164. ' f = fi;',
  15165. ' f = $mod.GetIt;',
  15166. ' f = $mod.GetIt.bind(null, 3);',
  15167. ' f = Sub;',
  15168. ' f = rtl.createCallback(o, "DoIt");',
  15169. ' f = fi.bind(null, 4);',
  15170. ' return Result;',
  15171. '};',
  15172. '']),
  15173. LinesToStr([ // $mod.$main
  15174. '']));
  15175. end;
  15176. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  15177. begin
  15178. StartProgram(false);
  15179. Add('{$modeswitch externalclass}');
  15180. Add('type');
  15181. Add(' TJSString = class external name ''String''');
  15182. Add(' class function fromCharCode() : string; varargs;');
  15183. Add(' end;');
  15184. Add('var');
  15185. Add(' s: string;');
  15186. Add(' sObj: TJSString;');
  15187. Add('begin');
  15188. Add(' s:=sObj.fromCharCode(65,66);');
  15189. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  15190. nExternalClassInstanceCannotAccessStaticX);
  15191. ConvertProgram;
  15192. end;
  15193. procedure TTestModule.TestExternalClass_BracketAccessor;
  15194. begin
  15195. StartProgram(false);
  15196. Add([
  15197. '{$modeswitch externalclass}',
  15198. 'type',
  15199. ' TJSArray = class external name ''Array2''',
  15200. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  15201. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  15202. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  15203. ' end;',
  15204. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  15205. 'begin end;',
  15206. 'var',
  15207. ' Arr: tjsarray;',
  15208. ' s: string;',
  15209. ' i: longint;',
  15210. ' v: jsvalue;',
  15211. 'begin',
  15212. ' v:=arr[0];',
  15213. ' v:=arr.items[1];',
  15214. ' arr[2]:=s;',
  15215. ' arr.items[3]:=s;',
  15216. ' arr[4]:=i;',
  15217. ' arr[5]:=arr[6];',
  15218. ' arr.items[7]:=arr.items[8];',
  15219. ' with arr do items[9]:=items[10];',
  15220. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  15221. ' with arr do begin',
  15222. ' v:=GetItems(14);',
  15223. ' setitems(15,16);',
  15224. ' end;',
  15225. '']);
  15226. ConvertProgram;
  15227. CheckSource('TestExternalClass_BracketAccessor',
  15228. LinesToStr([ // statements
  15229. 'this.DoIt = function (vI, vJ, vK, vL) {',
  15230. '};',
  15231. 'this.Arr = null;',
  15232. 'this.s = "";',
  15233. 'this.i = 0;',
  15234. 'this.v = undefined;',
  15235. '']),
  15236. LinesToStr([ // $mod.$main
  15237. '$mod.v = $mod.Arr[0];',
  15238. '$mod.v = $mod.Arr[1];',
  15239. '$mod.Arr[2] = $mod.s;',
  15240. '$mod.Arr[3] = $mod.s;',
  15241. '$mod.Arr[4] = $mod.i;',
  15242. '$mod.Arr[5] = $mod.Arr[6];',
  15243. '$mod.Arr[7] = $mod.Arr[8];',
  15244. 'var $with1 = $mod.Arr;',
  15245. '$with1[9] = $with1[10];',
  15246. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  15247. ' a: 9,',
  15248. ' p: $mod.Arr,',
  15249. ' get: function () {',
  15250. ' return this.p[this.a];',
  15251. ' },',
  15252. ' set: function (v) {',
  15253. ' this.p[this.a] = v;',
  15254. ' }',
  15255. '}, {',
  15256. ' a: 10,',
  15257. ' p: $mod.Arr,',
  15258. ' get: function () {',
  15259. ' return this.p[this.a];',
  15260. ' },',
  15261. ' set: function (v) {',
  15262. ' this.p[this.a] = v;',
  15263. ' }',
  15264. '});',
  15265. 'var $with2 = $mod.Arr;',
  15266. '$mod.v = $with2[14];',
  15267. '$with2[15] = 16;',
  15268. '']));
  15269. end;
  15270. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  15271. begin
  15272. StartProgram(false);
  15273. Add([
  15274. '{$modeswitch externalclass}',
  15275. 'type',
  15276. ' TJSArray = class external name ''Array2''',
  15277. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  15278. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  15279. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  15280. ' end;',
  15281. ' TMyArr = class(TJSArray)',
  15282. ' procedure DoIt;',
  15283. ' end;',
  15284. 'procedure tmyarr.DoIt;',
  15285. 'begin',
  15286. ' Items[1]:=Items[2];',
  15287. ' SetItems(3,getItems(4));',
  15288. 'end;',
  15289. 'var',
  15290. ' Arr: tmyarr;',
  15291. ' s: string;',
  15292. ' i: longint;',
  15293. ' v: jsvalue;',
  15294. 'begin',
  15295. ' v:=arr[0];',
  15296. ' v:=arr.items[1];',
  15297. ' arr[2]:=s;',
  15298. ' arr.items[3]:=s;',
  15299. ' arr[4]:=i;',
  15300. ' arr[5]:=arr[6];',
  15301. ' arr.items[7]:=arr.items[8];',
  15302. ' with arr do items[9]:=items[10];',
  15303. ' with arr do begin',
  15304. ' v:=GetItems(14);',
  15305. ' setitems(15,16);',
  15306. ' end;',
  15307. '']);
  15308. ConvertProgram;
  15309. CheckSource('TestExternalClass_BracketAccessor_Call',
  15310. LinesToStr([ // statements
  15311. 'rtl.createClassExt($mod, "TMyArr", Array2, "", function () {',
  15312. ' this.$init = function () {',
  15313. ' };',
  15314. ' this.$final = function () {',
  15315. ' };',
  15316. ' this.DoIt = function () {',
  15317. ' this[1] = this[2];',
  15318. ' this[3] = this[4];',
  15319. ' };',
  15320. '});',
  15321. 'this.Arr = null;',
  15322. 'this.s = "";',
  15323. 'this.i = 0;',
  15324. 'this.v = undefined;',
  15325. '']),
  15326. LinesToStr([ // $mod.$main
  15327. '$mod.v = $mod.Arr[0];',
  15328. '$mod.v = $mod.Arr[1];',
  15329. '$mod.Arr[2] = $mod.s;',
  15330. '$mod.Arr[3] = $mod.s;',
  15331. '$mod.Arr[4] = $mod.i;',
  15332. '$mod.Arr[5] = $mod.Arr[6];',
  15333. '$mod.Arr[7] = $mod.Arr[8];',
  15334. 'var $with1 = $mod.Arr;',
  15335. '$with1[9] = $with1[10];',
  15336. 'var $with2 = $mod.Arr;',
  15337. '$mod.v = $with2[14];',
  15338. '$with2[15] = 16;',
  15339. '']));
  15340. end;
  15341. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  15342. begin
  15343. StartProgram(false);
  15344. Add('{$modeswitch externalclass}');
  15345. Add('type');
  15346. Add(' TJSArray = class external name ''Array2''');
  15347. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  15348. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  15349. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  15350. Add(' end;');
  15351. Add('begin');
  15352. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  15353. nBracketAccessorOfExternalClassMustHaveOneParameter);
  15354. ConvertProgram;
  15355. end;
  15356. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  15357. begin
  15358. StartProgram(false);
  15359. Add('{$modeswitch externalclass}');
  15360. Add('type');
  15361. Add(' TJSArray = class external name ''Array2''');
  15362. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  15363. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  15364. Add(' end;');
  15365. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  15366. Add('begin end;');
  15367. Add('var');
  15368. Add(' Arr: tjsarray;');
  15369. Add(' v: jsvalue;');
  15370. Add('begin');
  15371. Add(' v:=arr[0];');
  15372. Add(' v:=arr.items[1];');
  15373. Add(' with arr do v:=items[2];');
  15374. Add(' doit(arr[3],arr[4]);');
  15375. ConvertProgram;
  15376. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  15377. LinesToStr([ // statements
  15378. 'this.DoIt = function (vI, vJ) {',
  15379. '};',
  15380. 'this.Arr = null;',
  15381. 'this.v = undefined;',
  15382. '']),
  15383. LinesToStr([ // $mod.$main
  15384. '$mod.v = $mod.Arr[0];',
  15385. '$mod.v = $mod.Arr[1];',
  15386. 'var $with1 = $mod.Arr;',
  15387. '$mod.v = $with1[2];',
  15388. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  15389. '']));
  15390. end;
  15391. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  15392. begin
  15393. StartProgram(false);
  15394. Add('{$modeswitch externalclass}');
  15395. Add('type');
  15396. Add(' TJSArray = class external name ''Array2''');
  15397. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  15398. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  15399. Add(' end;');
  15400. Add('var');
  15401. Add(' Arr: tjsarray;');
  15402. Add(' s: string;');
  15403. Add(' i: longint;');
  15404. Add(' v: jsvalue;');
  15405. Add('begin');
  15406. Add(' arr[2]:=s;');
  15407. Add(' arr.items[3]:=s;');
  15408. Add(' arr[4]:=i;');
  15409. Add(' with arr do items[5]:=i;');
  15410. ConvertProgram;
  15411. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  15412. LinesToStr([ // statements
  15413. 'this.Arr = null;',
  15414. 'this.s = "";',
  15415. 'this.i = 0;',
  15416. 'this.v = undefined;',
  15417. '']),
  15418. LinesToStr([ // $mod.$main
  15419. '$mod.Arr[2] = $mod.s;',
  15420. '$mod.Arr[3] = $mod.s;',
  15421. '$mod.Arr[4] = $mod.i;',
  15422. 'var $with1 = $mod.Arr;',
  15423. '$with1[5] = $mod.i;',
  15424. '']));
  15425. end;
  15426. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  15427. begin
  15428. StartProgram(false);
  15429. Add('{$modeswitch externalclass}');
  15430. Add('type');
  15431. Add(' TJSArray = class external name ''Array2''');
  15432. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  15433. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  15434. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  15435. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  15436. Add(' end;');
  15437. Add('var');
  15438. Add(' Arr: tjsarray;');
  15439. Add(' s: string;');
  15440. Add(' i: longint;');
  15441. Add(' v: jsvalue;');
  15442. Add('begin');
  15443. Add(' arr[2]:=s;');
  15444. Add(' arr.items[3]:=s;');
  15445. Add(' arr.numbers[4]:=i;');
  15446. Add(' with arr do items[5]:=i;');
  15447. Add(' with arr do numbers[6]:=i;');
  15448. ConvertProgram;
  15449. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  15450. LinesToStr([ // statements
  15451. 'this.Arr = null;',
  15452. 'this.s = "";',
  15453. 'this.i = 0;',
  15454. 'this.v = undefined;',
  15455. '']),
  15456. LinesToStr([ // $mod.$main
  15457. '$mod.Arr[2] = $mod.s;',
  15458. '$mod.Arr[3] = $mod.s;',
  15459. '$mod.Arr[4] = $mod.i;',
  15460. 'var $with1 = $mod.Arr;',
  15461. '$with1[5] = $mod.i;',
  15462. 'var $with2 = $mod.Arr;',
  15463. '$with2[6] = $mod.i;',
  15464. '']));
  15465. end;
  15466. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  15467. begin
  15468. StartProgram(false);
  15469. Add('{$modeswitch externalclass}');
  15470. Add('type');
  15471. Add(' TJSArray = class external name ''Array2''');
  15472. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  15473. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  15474. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  15475. Add(' end;');
  15476. Add('var');
  15477. Add(' Arr: tjsarray;');
  15478. Add(' i: longint;');
  15479. Add(' IntArr: array of longint;');
  15480. Add(' v: jsvalue;');
  15481. Add('begin');
  15482. Add(' v:=arr.items[i];');
  15483. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  15484. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  15485. ConvertProgram;
  15486. CheckSource('TestExternalClass_BracketAccessor_Index',
  15487. LinesToStr([ // statements
  15488. 'this.Arr = null;',
  15489. 'this.i = 0;',
  15490. 'this.IntArr = [];',
  15491. 'this.v = undefined;',
  15492. '']),
  15493. LinesToStr([ // $mod.$main
  15494. '$mod.v = $mod.Arr[$mod.i];',
  15495. '$mod.Arr[Math.floor($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  15496. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  15497. '']));
  15498. end;
  15499. procedure TTestModule.TestExternalClass_ForInJSObject;
  15500. begin
  15501. StartProgram(false);
  15502. Add([
  15503. '{$modeswitch externalclass}',
  15504. 'type',
  15505. ' TJSObject = class external name ''Object''',
  15506. ' end;',
  15507. 'var',
  15508. ' o: TJSObject;',
  15509. ' key: string;',
  15510. 'begin',
  15511. ' for key in o do',
  15512. ' if key=''abc'' then ;',
  15513. '']);
  15514. ConvertProgram;
  15515. CheckSource('TestExternalClass_ForInJSObject',
  15516. LinesToStr([ // statements
  15517. 'this.o = null;',
  15518. 'this.key = "";',
  15519. '']),
  15520. LinesToStr([ // $mod.$main
  15521. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  15522. '']));
  15523. end;
  15524. procedure TTestModule.TestExternalClass_ForInJSArray;
  15525. begin
  15526. StartProgram(false);
  15527. Add([
  15528. '{$modeswitch externalclass}',
  15529. 'type',
  15530. ' TJSInt8Array = class external name ''Int8Array''',
  15531. ' private',
  15532. ' flength: NativeInt external name ''length'';',
  15533. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  15534. ' public',
  15535. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  15536. ' property Length: NativeInt read flength;',
  15537. ' end;',
  15538. 'var',
  15539. ' a: TJSInt8Array;',
  15540. ' value: shortint;',
  15541. 'begin',
  15542. ' for value in a do',
  15543. ' if value=3 then ;',
  15544. '']);
  15545. ConvertProgram;
  15546. CheckSource('TestExternalClass_ForInJSArray',
  15547. LinesToStr([ // statements
  15548. 'this.a = null;',
  15549. 'this.value = 0;',
  15550. '']),
  15551. LinesToStr([ // $mod.$main
  15552. 'for (var $in1 = $mod.a, $l2 = 0, $end3 = rtl.length($in1) - 1; $l2 <= $end3; $l2++) {',
  15553. ' $mod.value = $in1[$l2];',
  15554. ' if ($mod.value === 3) ;',
  15555. '};',
  15556. '']));
  15557. end;
  15558. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  15559. begin
  15560. AddModuleWithIntfImplSrc('unit2.pas',
  15561. LinesToStr([
  15562. '{$modeswitch externalclass}',
  15563. 'type',
  15564. ' TJSBufferSource = class external name ''BufferSource''',
  15565. ' end;',
  15566. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  15567. '']),
  15568. '');
  15569. AddModuleWithIntfImplSrc('unit3.pas',
  15570. LinesToStr([
  15571. '{$modeswitch externalclass}',
  15572. 'type',
  15573. ' TJSBufferSource = class external name ''BufferSource''',
  15574. ' end;',
  15575. '']),
  15576. '');
  15577. StartUnit(true);
  15578. Add([
  15579. 'interface',
  15580. 'uses unit2, unit3;',
  15581. 'procedure DoSome(s: TJSBufferSource);',
  15582. 'implementation',
  15583. 'procedure DoSome(s: TJSBufferSource);',
  15584. 'begin',
  15585. ' DoIt(s);',
  15586. 'end;',
  15587. '']);
  15588. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  15589. nIncompatibleTypeArgNo);
  15590. ConvertUnit;
  15591. end;
  15592. procedure TTestModule.TestClassInterface_Corba;
  15593. begin
  15594. StartProgram(false);
  15595. Add([
  15596. '{$interfaces corba}',
  15597. 'type',
  15598. ' IUnknown = interface;',
  15599. ' IUnknown = interface',
  15600. ' [''{00000000-0000-0000-C000-000000000046}'']',
  15601. ' end;',
  15602. ' IInterface = IUnknown;',
  15603. ' IBird = interface(IInterface)',
  15604. ' function GetSize: longint;',
  15605. ' procedure SetSize(i: longint);',
  15606. ' property Size: longint read GetSize write SetSize;',
  15607. ' procedure DoIt(i: longint);',
  15608. ' end;',
  15609. ' TObject = class',
  15610. ' end;',
  15611. ' TBird = class(TObject,IBird)',
  15612. ' function GetSize: longint; virtual; abstract;',
  15613. ' procedure SetSize(i: longint); virtual; abstract;',
  15614. ' procedure DoIt(i: longint); virtual; abstract;',
  15615. ' end;',
  15616. 'var',
  15617. ' BirdIntf: IBird;',
  15618. 'begin',
  15619. ' BirdIntf.Size:=BirdIntf.Size;',
  15620. '']);
  15621. ConvertProgram;
  15622. CheckSource('TestClassInterface_Corba',
  15623. LinesToStr([ // statements
  15624. 'rtl.createInterface($mod, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  15625. 'rtl.createInterface($mod, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], $mod.IUnknown);',
  15626. 'rtl.createClass($mod, "TObject", null, function () {',
  15627. ' this.$init = function () {',
  15628. ' };',
  15629. ' this.$final = function () {',
  15630. ' };',
  15631. '});',
  15632. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  15633. ' rtl.addIntf(this, $mod.IBird);',
  15634. '});',
  15635. 'this.BirdIntf = null;',
  15636. '']),
  15637. LinesToStr([ // $mod.$main
  15638. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  15639. '']));
  15640. end;
  15641. procedure TTestModule.TestClassInterface_ProcExternalFail;
  15642. begin
  15643. StartProgram(false);
  15644. Add([
  15645. '{$interfaces corba}',
  15646. 'type',
  15647. ' IUnknown = interface',
  15648. ' procedure DoIt; external name ''foo'';',
  15649. ' end;',
  15650. 'begin']);
  15651. SetExpectedParserError(
  15652. 'Fields are not allowed in Interfaces at token "Identifier external" in file test1.pp at line 6 column 21',
  15653. nParserNoFieldsAllowed);
  15654. ConvertProgram;
  15655. end;
  15656. procedure TTestModule.TestClassInterface_Overloads;
  15657. begin
  15658. StartProgram(false);
  15659. Add([
  15660. '{$interfaces corba}',
  15661. 'type',
  15662. ' integer = longint;',
  15663. ' IUnknown = interface',
  15664. ' procedure DoIt(i: integer);',
  15665. ' procedure DoIt(s: string);',
  15666. ' end;',
  15667. ' IBird = interface(IUnknown)',
  15668. ' procedure DoIt(b: boolean); overload;',
  15669. ' end;',
  15670. ' TObject = class',
  15671. ' end;',
  15672. ' TBird = class(TObject,IBird)',
  15673. ' procedure DoIt(o: TObject);',
  15674. ' procedure DoIt(s: string);',
  15675. ' procedure DoIt(i: integer);',
  15676. ' procedure DoIt(b: boolean);',
  15677. ' end;',
  15678. 'procedure TBird.DoIt(o: TObject); begin end;',
  15679. 'procedure TBird.DoIt(s: string); begin end;',
  15680. 'procedure TBird.DoIt(i: integer); begin end;',
  15681. 'procedure TBird.DoIt(b: boolean); begin end;',
  15682. 'var',
  15683. ' BirdIntf: IBird;',
  15684. 'begin',
  15685. ' BirdIntf.DoIt(3);',
  15686. ' BirdIntf.DoIt(''abc'');',
  15687. ' BirdIntf.DoIt(true);',
  15688. '']);
  15689. ConvertProgram;
  15690. CheckSource('TestClassInterface_Overloads',
  15691. LinesToStr([ // statements
  15692. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  15693. 'rtl.createInterface($mod, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], $mod.IUnknown);',
  15694. 'rtl.createClass($mod, "TObject", null, function () {',
  15695. ' this.$init = function () {',
  15696. ' };',
  15697. ' this.$final = function () {',
  15698. ' };',
  15699. '});',
  15700. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  15701. ' this.DoIt = function (o) {',
  15702. ' };',
  15703. ' this.DoIt$1 = function (s) {',
  15704. ' };',
  15705. ' this.DoIt$2 = function (i) {',
  15706. ' };',
  15707. ' this.DoIt$3 = function (b) {',
  15708. ' };',
  15709. ' rtl.addIntf(this, $mod.IBird, {',
  15710. ' DoIt$2: "DoIt$3",',
  15711. ' DoIt: "DoIt$2"',
  15712. ' });',
  15713. '});',
  15714. 'this.BirdIntf = null;',
  15715. '']),
  15716. LinesToStr([ // $mod.$main
  15717. '$mod.BirdIntf.DoIt(3);',
  15718. '$mod.BirdIntf.DoIt$1("abc");',
  15719. '$mod.BirdIntf.DoIt$2(true);',
  15720. '']));
  15721. end;
  15722. procedure TTestModule.TestClassInterface_DuplicateGUIInIntfListFail;
  15723. begin
  15724. StartProgram(false);
  15725. Add([
  15726. '{$interfaces corba}',
  15727. 'type',
  15728. ' IBird = interface',
  15729. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  15730. ' end;',
  15731. ' IDog = interface',
  15732. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  15733. ' end;',
  15734. ' TObject = class(IBird,IDog)',
  15735. ' end;',
  15736. 'begin']);
  15737. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  15738. nDuplicateGUIDXInYZ);
  15739. ConvertProgram;
  15740. end;
  15741. procedure TTestModule.TestClassInterface_DuplicateGUIInAncestorFail;
  15742. begin
  15743. StartProgram(false);
  15744. Add([
  15745. '{$interfaces corba}',
  15746. 'type',
  15747. ' IAnimal = interface',
  15748. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  15749. ' end;',
  15750. ' IBird = interface(IAnimal)',
  15751. ' end;',
  15752. ' IHawk = interface(IBird)',
  15753. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  15754. ' end;',
  15755. 'begin']);
  15756. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  15757. nDuplicateGUIDXInYZ);
  15758. ConvertProgram;
  15759. end;
  15760. procedure TTestModule.TestClassInterface_AncestorImpl;
  15761. begin
  15762. StartProgram(false);
  15763. Add([
  15764. '{$interfaces corba}',
  15765. 'type',
  15766. ' integer = longint;',
  15767. ' IUnknown = interface',
  15768. ' procedure DoIt(i: integer);',
  15769. ' end;',
  15770. ' IBird = interface',
  15771. ' procedure Fly(i: integer);',
  15772. ' end;',
  15773. ' TObject = class(IUnknown)',
  15774. ' procedure DoIt(i: integer);',
  15775. ' end;',
  15776. ' TBird = class(IBird)',
  15777. ' procedure Fly(i: integer);',
  15778. ' end;',
  15779. 'procedure TObject.DoIt(i: integer); begin end;',
  15780. 'procedure TBird.Fly(i: integer); begin end;',
  15781. 'begin',
  15782. '']);
  15783. ConvertProgram;
  15784. CheckSource('TestClassInterface_AncestorIntf',
  15785. LinesToStr([ // statements
  15786. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  15787. 'rtl.createInterface($mod, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  15788. 'rtl.createClass($mod, "TObject", null, function () {',
  15789. ' this.$init = function () {',
  15790. ' };',
  15791. ' this.$final = function () {',
  15792. ' };',
  15793. ' this.DoIt = function (i) {',
  15794. ' };',
  15795. ' rtl.addIntf(this, $mod.IUnknown);',
  15796. '});',
  15797. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  15798. ' this.Fly = function (i) {',
  15799. ' };',
  15800. ' rtl.addIntf(this, $mod.IBird);',
  15801. ' rtl.addIntf(this, $mod.IUnknown);',
  15802. '});',
  15803. '']),
  15804. LinesToStr([ // $mod.$main
  15805. '']));
  15806. end;
  15807. procedure TTestModule.TestClassInterface_ImplReintroduce;
  15808. begin
  15809. StartProgram(false);
  15810. Add([
  15811. '{$interfaces corba}',
  15812. 'type',
  15813. ' integer = longint;',
  15814. ' IBird = interface',
  15815. ' procedure DoIt(i: integer);',
  15816. ' end;',
  15817. ' TObject = class',
  15818. ' procedure DoIt(i: integer);',
  15819. ' end;',
  15820. ' TBird = class(IBird)',
  15821. ' procedure DoIt(i: integer); virtual; reintroduce;',
  15822. ' end;',
  15823. 'procedure TObject.DoIt(i: integer); begin end;',
  15824. 'procedure TBird.DoIt(i: integer); begin end;',
  15825. 'begin',
  15826. '']);
  15827. ConvertProgram;
  15828. CheckSource('TestClassInterface_ImplReintroduce',
  15829. LinesToStr([ // statements
  15830. 'rtl.createInterface($mod, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  15831. 'rtl.createClass($mod, "TObject", null, function () {',
  15832. ' this.$init = function () {',
  15833. ' };',
  15834. ' this.$final = function () {',
  15835. ' };',
  15836. ' this.DoIt = function (i) {',
  15837. ' };',
  15838. '});',
  15839. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  15840. ' this.DoIt$1 = function (i) {',
  15841. ' };',
  15842. ' rtl.addIntf(this, $mod.IBird, {',
  15843. ' DoIt: "DoIt$1"',
  15844. ' });',
  15845. '});',
  15846. '']),
  15847. LinesToStr([ // $mod.$main
  15848. '']));
  15849. end;
  15850. procedure TTestModule.TestClassInterface_MethodResolution;
  15851. begin
  15852. StartProgram(false);
  15853. Add([
  15854. '{$interfaces corba}',
  15855. 'type',
  15856. ' IUnknown = interface',
  15857. ' procedure Walk(i: longint);',
  15858. ' end;',
  15859. ' IBird = interface(IUnknown)',
  15860. ' procedure Walk(b: boolean); overload;',
  15861. ' procedure Fly(s: string);',
  15862. ' end;',
  15863. ' TObject = class',
  15864. ' end;',
  15865. ' TBird = class(TObject,IBird)',
  15866. ' procedure IBird.Fly = Move;',
  15867. ' procedure IBird.Walk = Hop;',
  15868. ' procedure Hop(i: longint);',
  15869. ' procedure Move(s: string);',
  15870. ' procedure Hop(b: boolean);',
  15871. ' end;',
  15872. 'procedure TBird.Move(s: string); begin end;',
  15873. 'procedure TBird.Hop(i: longint); begin end;',
  15874. 'procedure TBird.Hop(b: boolean); begin end;',
  15875. 'var',
  15876. ' BirdIntf: IBird;',
  15877. 'begin',
  15878. ' BirdIntf.Walk(3);',
  15879. ' BirdIntf.Walk(true);',
  15880. ' BirdIntf.Fly(''abc'');',
  15881. '']);
  15882. ConvertProgram;
  15883. CheckSource('TestClassInterface_MethodResolution',
  15884. LinesToStr([ // statements
  15885. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  15886. 'rtl.createInterface($mod, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], $mod.IUnknown);',
  15887. 'rtl.createClass($mod, "TObject", null, function () {',
  15888. ' this.$init = function () {',
  15889. ' };',
  15890. ' this.$final = function () {',
  15891. ' };',
  15892. '});',
  15893. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  15894. ' this.Hop = function (i) {',
  15895. ' };',
  15896. ' this.Move = function (s) {',
  15897. ' };',
  15898. ' this.Hop$1 = function (b) {',
  15899. ' };',
  15900. ' rtl.addIntf(this, $mod.IBird, {',
  15901. ' Walk$1: "Hop$1",',
  15902. ' Fly: "Move",',
  15903. ' Walk: "Hop"',
  15904. ' });',
  15905. '});',
  15906. 'this.BirdIntf = null;',
  15907. '']),
  15908. LinesToStr([ // $mod.$main
  15909. '$mod.BirdIntf.Walk(3);',
  15910. '$mod.BirdIntf.Walk$1(true);',
  15911. '$mod.BirdIntf.Fly("abc");',
  15912. '']));
  15913. end;
  15914. procedure TTestModule.TestClassInterface_AncestorMoreInterfaces;
  15915. begin
  15916. StartProgram(false);
  15917. Add([
  15918. '{$interfaces com}',
  15919. 'type',
  15920. ' IUnknown = interface',
  15921. ' function _AddRef: longint;',
  15922. ' procedure Walk;',
  15923. ' end;',
  15924. ' IBird = interface end;',
  15925. ' IDog = interface end;',
  15926. ' TObject = class(IBird,IDog)',
  15927. ' function _AddRef: longint; virtual; abstract;',
  15928. ' procedure Walk; virtual; abstract;',
  15929. ' end;',
  15930. ' TBird = class(IUnknown)',
  15931. ' end;',
  15932. 'begin',
  15933. '']);
  15934. ConvertProgram;
  15935. CheckSource('TestClassInterface_COM_AncestorLess',
  15936. LinesToStr([ // statements
  15937. 'rtl.createInterface($mod, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  15938. 'rtl.createInterface($mod, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], $mod.IUnknown);',
  15939. 'rtl.createInterface($mod, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], $mod.IUnknown);',
  15940. 'rtl.createClass($mod, "TObject", null, function () {',
  15941. ' this.$init = function () {',
  15942. ' };',
  15943. ' this.$final = function () {',
  15944. ' };',
  15945. ' rtl.addIntf(this, $mod.IBird);',
  15946. ' rtl.addIntf(this, $mod.IDog);',
  15947. '});',
  15948. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  15949. ' rtl.addIntf(this, $mod.IUnknown);',
  15950. ' rtl.addIntf(this, $mod.IBird);',
  15951. ' rtl.addIntf(this, $mod.IDog);',
  15952. '});',
  15953. '']),
  15954. LinesToStr([ // $mod.$main
  15955. '']));
  15956. end;
  15957. procedure TTestModule.TestClassInterface_MethodOverride;
  15958. begin
  15959. StartProgram(false);
  15960. Add([
  15961. '{$interfaces corba}',
  15962. 'type',
  15963. ' IUnknown = interface',
  15964. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  15965. ' procedure Go;',
  15966. ' end;',
  15967. ' TObject = class(IUnknown)',
  15968. ' procedure Go; virtual; abstract;',
  15969. ' end;',
  15970. ' TBird = class',
  15971. ' procedure Go; override;',
  15972. ' end;',
  15973. ' TCat = class(TObject)',
  15974. ' procedure Go; override;',
  15975. ' end;',
  15976. ' TDog = class(TObject, IUnknown)',
  15977. ' procedure Go; override;',
  15978. ' end;',
  15979. 'procedure TBird.Go; begin end;',
  15980. 'procedure TCat.Go; begin end;',
  15981. 'procedure TDog.Go; begin end;',
  15982. 'begin',
  15983. '']);
  15984. ConvertProgram;
  15985. CheckSource('TestClassInterface_MethodOverride',
  15986. LinesToStr([ // statements
  15987. 'rtl.createInterface($mod, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  15988. 'rtl.createClass($mod, "TObject", null, function () {',
  15989. ' this.$init = function () {',
  15990. ' };',
  15991. ' this.$final = function () {',
  15992. ' };',
  15993. ' rtl.addIntf(this, $mod.IUnknown);',
  15994. '});',
  15995. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  15996. ' this.Go = function () {',
  15997. ' };',
  15998. ' rtl.addIntf(this, $mod.IUnknown);',
  15999. '});',
  16000. 'rtl.createClass($mod, "TCat", $mod.TObject, function () {',
  16001. ' this.Go = function () {',
  16002. ' };',
  16003. ' rtl.addIntf(this, $mod.IUnknown);',
  16004. '});',
  16005. 'rtl.createClass($mod, "TDog", $mod.TObject, function () {',
  16006. ' this.Go = function () {',
  16007. ' };',
  16008. ' rtl.addIntf(this, $mod.IUnknown);',
  16009. '});',
  16010. '']),
  16011. LinesToStr([ // $mod.$main
  16012. '']));
  16013. end;
  16014. procedure TTestModule.TestClassInterface_Corba_Delegation;
  16015. begin
  16016. StartProgram(false);
  16017. Add([
  16018. '{$interfaces corba}',
  16019. 'type',
  16020. ' IUnknown = interface',
  16021. ' end;',
  16022. ' IBird = interface(IUnknown)',
  16023. ' procedure Fly(s: string);',
  16024. ' end;',
  16025. ' IEagle = interface(IBird)',
  16026. ' end;',
  16027. ' IDove = interface(IBird)',
  16028. ' end;',
  16029. ' ISwallow = interface(IBird)',
  16030. ' end;',
  16031. ' TObject = class',
  16032. ' end;',
  16033. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  16034. ' procedure Fly(s: string); virtual; abstract;',
  16035. ' end;',
  16036. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  16037. ' FBirdIntf: IBird;',
  16038. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  16039. ' function GetEagleIntf: IEagle; virtual; abstract;',
  16040. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  16041. ' FDoveObj: TBird;',
  16042. ' property DoveObj: TBird read FDoveObj implements IDove;',
  16043. ' function GetSwallowObj: TBird; virtual; abstract;',
  16044. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  16045. ' end;',
  16046. 'begin',
  16047. '']);
  16048. ConvertProgram;
  16049. CheckSource('TestClassInterface_Delegation',
  16050. LinesToStr([ // statements
  16051. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  16052. 'rtl.createInterface($mod, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], $mod.IUnknown);',
  16053. 'rtl.createInterface($mod, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], $mod.IBird);',
  16054. 'rtl.createInterface($mod, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], $mod.IBird);',
  16055. 'rtl.createInterface($mod, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], $mod.IBird);',
  16056. 'rtl.createClass($mod, "TObject", null, function () {',
  16057. ' this.$init = function () {',
  16058. ' };',
  16059. ' this.$final = function () {',
  16060. ' };',
  16061. '});',
  16062. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16063. ' rtl.addIntf(this, $mod.IBird);',
  16064. ' rtl.addIntf(this, $mod.IEagle);',
  16065. ' rtl.addIntf(this, $mod.IDove);',
  16066. ' rtl.addIntf(this, $mod.ISwallow);',
  16067. '});',
  16068. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  16069. ' this.$init = function () {',
  16070. ' $mod.TObject.$init.call(this);',
  16071. ' this.FBirdIntf = null;',
  16072. ' this.FDoveObj = null;',
  16073. ' };',
  16074. ' this.$final = function () {',
  16075. ' this.FBirdIntf = undefined;',
  16076. ' this.FDoveObj = undefined;',
  16077. ' $mod.TObject.$final.call(this);',
  16078. ' };',
  16079. ' this.$intfmaps = {',
  16080. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  16081. ' return this.FBirdIntf;',
  16082. ' },',
  16083. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  16084. ' return this.GetEagleIntf();',
  16085. ' },',
  16086. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  16087. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  16088. ' },',
  16089. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  16090. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  16091. ' }',
  16092. ' };',
  16093. '});',
  16094. '']),
  16095. LinesToStr([ // $mod.$main
  16096. '']));
  16097. end;
  16098. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  16099. begin
  16100. StartProgram(false);
  16101. Add([
  16102. '{$interfaces corba}',
  16103. 'type',
  16104. ' IUnknown = interface',
  16105. ' end;',
  16106. ' IBird = interface(IUnknown)',
  16107. ' procedure Fly(s: string);',
  16108. ' end;',
  16109. ' IEagle = interface(IBird)',
  16110. ' end;',
  16111. ' IDove = interface(IBird)',
  16112. ' end;',
  16113. ' ISwallow = interface(IBird)',
  16114. ' end;',
  16115. ' TObject = class',
  16116. ' end;',
  16117. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  16118. ' procedure Fly(s: string); virtual; abstract;',
  16119. ' end;',
  16120. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  16121. ' private',
  16122. ' class var FBirdIntf: IBird;',
  16123. ' class var FDoveObj: TBird;',
  16124. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  16125. ' class function GetSwallowObj: TBird; virtual; abstract;',
  16126. ' protected',
  16127. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  16128. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  16129. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  16130. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  16131. ' end;',
  16132. 'begin',
  16133. '']);
  16134. ConvertProgram;
  16135. CheckSource('TestClassInterface_DelegationStatic',
  16136. LinesToStr([ // statements
  16137. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  16138. 'rtl.createInterface($mod, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], $mod.IUnknown);',
  16139. 'rtl.createInterface($mod, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], $mod.IBird);',
  16140. 'rtl.createInterface($mod, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], $mod.IBird);',
  16141. 'rtl.createInterface($mod, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], $mod.IBird);',
  16142. 'rtl.createClass($mod, "TObject", null, function () {',
  16143. ' this.$init = function () {',
  16144. ' };',
  16145. ' this.$final = function () {',
  16146. ' };',
  16147. '});',
  16148. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16149. ' rtl.addIntf(this, $mod.IBird);',
  16150. ' rtl.addIntf(this, $mod.IEagle);',
  16151. ' rtl.addIntf(this, $mod.IDove);',
  16152. ' rtl.addIntf(this, $mod.ISwallow);',
  16153. '});',
  16154. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  16155. ' this.FBirdIntf = null;',
  16156. ' this.FDoveObj = null;',
  16157. ' this.$intfmaps = {',
  16158. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  16159. ' return this.FBirdIntf;',
  16160. ' },',
  16161. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  16162. ' return this.$class.GetEagleIntf();',
  16163. ' },',
  16164. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  16165. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  16166. ' },',
  16167. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  16168. ' return rtl.getIntfT(this.$class.GetSwallowObj(), $mod.ISwallow);',
  16169. ' }',
  16170. ' };',
  16171. '});',
  16172. '']),
  16173. LinesToStr([ // $mod.$main
  16174. '']));
  16175. end;
  16176. procedure TTestModule.TestClassInterface_Corba_Operators;
  16177. begin
  16178. StartProgram(false);
  16179. Add([
  16180. '{$interfaces corba}',
  16181. 'type',
  16182. ' IUnknown = interface',
  16183. ' end;',
  16184. ' IBird = interface(IUnknown)',
  16185. ' function GetItems(Index: longint): longint;',
  16186. ' procedure SetItems(Index: longint; Value: longint);',
  16187. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  16188. ' end;',
  16189. ' TObject = class',
  16190. ' end;',
  16191. ' TBird = class(TObject,IBird)',
  16192. ' function GetItems(Index: longint): longint; virtual; abstract;',
  16193. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  16194. ' end;',
  16195. 'var',
  16196. ' IntfVar: IBird = nil;',
  16197. ' IntfVar2: IBird;',
  16198. ' ObjVar: TBird;',
  16199. ' v: JSValue;',
  16200. 'begin',
  16201. ' IntfVar:=nil;',
  16202. ' IntfVar[3]:=IntfVar[4];',
  16203. ' if Assigned(IntfVar) then ;',
  16204. ' IntfVar:=IntfVar2;',
  16205. ' IntfVar:=ObjVar;',
  16206. ' if IntfVar=IntfVar2 then ;',
  16207. ' if IntfVar<>IntfVar2 then ;',
  16208. ' if IntfVar is IBird then ;',
  16209. ' if IntfVar is TBird then ;',
  16210. ' if ObjVar is IBird then ;',
  16211. ' IntfVar:=IntfVar2 as IBird;',
  16212. ' ObjVar:=IntfVar2 as TBird;',
  16213. ' IntfVar:=ObjVar as IBird;',
  16214. ' IntfVar:=IBird(IntfVar2);',
  16215. ' ObjVar:=TBird(IntfVar);',
  16216. ' IntfVar:=IBird(ObjVar);',
  16217. ' v:=IntfVar;',
  16218. ' IntfVar:=IBird(v);',
  16219. ' if v is IBird then ;',
  16220. ' v:=JSValue(IntfVar);',
  16221. ' v:=IBird;',
  16222. '']);
  16223. ConvertProgram;
  16224. CheckSource('TestClassInterface_Corba_Operators',
  16225. LinesToStr([ // statements
  16226. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  16227. 'rtl.createInterface($mod, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], $mod.IUnknown);',
  16228. 'rtl.createClass($mod, "TObject", null, function () {',
  16229. ' this.$init = function () {',
  16230. ' };',
  16231. ' this.$final = function () {',
  16232. ' };',
  16233. '});',
  16234. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16235. ' rtl.addIntf(this, $mod.IBird);',
  16236. '});',
  16237. 'this.IntfVar = null;',
  16238. 'this.IntfVar2 = null;',
  16239. 'this.ObjVar = null;',
  16240. 'this.v = undefined;',
  16241. '']),
  16242. LinesToStr([ // $mod.$main
  16243. '$mod.IntfVar = null;',
  16244. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  16245. 'if ($mod.IntfVar != null) ;',
  16246. '$mod.IntfVar = $mod.IntfVar2;',
  16247. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  16248. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  16249. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  16250. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  16251. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  16252. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  16253. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  16254. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  16255. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  16256. '$mod.IntfVar = $mod.IntfVar2;',
  16257. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  16258. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  16259. '$mod.v = $mod.IntfVar;',
  16260. '$mod.IntfVar = rtl.getObject($mod.v);',
  16261. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  16262. '$mod.v = rtl.getObject($mod.IntfVar);',
  16263. '$mod.v = $mod.IBird;',
  16264. '']));
  16265. end;
  16266. procedure TTestModule.TestClassInterface_Corba_Args;
  16267. begin
  16268. StartProgram(false);
  16269. Add([
  16270. '{$interfaces corba}',
  16271. 'type',
  16272. ' IUnknown = interface',
  16273. ' end;',
  16274. ' IBird = interface(IUnknown)',
  16275. ' end;',
  16276. ' TObject = class',
  16277. ' end;',
  16278. ' TBird = class(TObject,IBird)',
  16279. ' end;',
  16280. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  16281. 'begin',
  16282. ' DoIt(i,i,i);',
  16283. 'end;',
  16284. 'procedure Change(var i: IBird; out j: IBird);',
  16285. 'begin',
  16286. ' DoIt(i,i,i);',
  16287. ' Change(i,i);',
  16288. 'end;',
  16289. 'var',
  16290. ' i: IBird;',
  16291. ' o: TBird;',
  16292. 'begin',
  16293. ' DoIt(i,i,i);',
  16294. ' Change(i,i);',
  16295. ' DoIt(o,o,o);',
  16296. '']);
  16297. ConvertProgram;
  16298. CheckSource('TestClassInterface_Corba_Args',
  16299. LinesToStr([ // statements
  16300. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  16301. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], $mod.IUnknown);',
  16302. 'rtl.createClass($mod, "TObject", null, function () {',
  16303. ' this.$init = function () {',
  16304. ' };',
  16305. ' this.$final = function () {',
  16306. ' };',
  16307. '});',
  16308. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  16309. ' rtl.addIntf(this, $mod.IBird);',
  16310. '});',
  16311. 'this.DoIt = function (u, i, j) {',
  16312. ' $mod.DoIt({',
  16313. ' get: function () {',
  16314. ' return i;',
  16315. ' },',
  16316. ' set: function (v) {',
  16317. ' i = v;',
  16318. ' }',
  16319. ' }, i, i);',
  16320. '};',
  16321. 'this.Change = function (i, j) {',
  16322. ' $mod.DoIt(i, i.get(), i.get());',
  16323. ' $mod.Change(i, i);',
  16324. '};',
  16325. 'this.i = null;',
  16326. 'this.o = null;',
  16327. '']),
  16328. LinesToStr([ // $mod.$main
  16329. '$mod.DoIt({',
  16330. ' p: $mod,',
  16331. ' get: function () {',
  16332. ' return this.p.i;',
  16333. ' },',
  16334. ' set: function (v) {',
  16335. ' this.p.i = v;',
  16336. ' }',
  16337. '}, $mod.i, $mod.i);',
  16338. '$mod.Change({',
  16339. ' p: $mod,',
  16340. ' get: function () {',
  16341. ' return this.p.i;',
  16342. ' },',
  16343. ' set: function (v) {',
  16344. ' this.p.i = v;',
  16345. ' }',
  16346. '}, {',
  16347. ' p: $mod,',
  16348. ' get: function () {',
  16349. ' return this.p.i;',
  16350. ' },',
  16351. ' set: function (v) {',
  16352. ' this.p.i = v;',
  16353. ' }',
  16354. '});',
  16355. '$mod.DoIt({',
  16356. ' p: $mod,',
  16357. ' get: function () {',
  16358. ' return this.p.o;',
  16359. ' },',
  16360. ' set: function (v) {',
  16361. ' this.p.o = v;',
  16362. ' }',
  16363. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  16364. '']));
  16365. end;
  16366. procedure TTestModule.TestClassInterface_Corba_ForIn;
  16367. begin
  16368. StartProgram(false);
  16369. Add([
  16370. '{$interfaces corba}',
  16371. 'type',
  16372. ' IUnknown = interface end;',
  16373. ' TObject = class',
  16374. ' Id: longint;',
  16375. ' end;',
  16376. ' IEnumerator = interface(IUnknown)',
  16377. ' function GetCurrent: TObject;',
  16378. ' function MoveNext: Boolean;',
  16379. ' property Current: TObject read GetCurrent;',
  16380. ' end;',
  16381. ' IEnumerable = interface(IUnknown)',
  16382. ' function GetEnumerator: IEnumerator;',
  16383. ' end;',
  16384. 'var',
  16385. ' o: TObject;',
  16386. ' i: IEnumerable;',
  16387. 'begin',
  16388. ' for o in i do o.Id:=3;',
  16389. '']);
  16390. ConvertProgram;
  16391. CheckSource('TestClassInterface_Corba_ForIn',
  16392. LinesToStr([ // statements
  16393. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  16394. 'rtl.createClass($mod, "TObject", null, function () {',
  16395. ' this.$init = function () {',
  16396. ' this.Id = 0;',
  16397. ' };',
  16398. ' this.$final = function () {',
  16399. ' };',
  16400. '});',
  16401. 'rtl.createInterface($mod, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], $mod.IUnknown);',
  16402. 'rtl.createInterface($mod, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], $mod.IUnknown);',
  16403. 'this.o = null;',
  16404. 'this.i = null;',
  16405. '']),
  16406. LinesToStr([ // $mod.$main
  16407. 'var $in1 = $mod.i.GetEnumerator();',
  16408. 'while ($in1.MoveNext()) {',
  16409. ' $mod.o = $in1.GetCurrent();',
  16410. ' $mod.o.Id = 3;',
  16411. '};',
  16412. '']));
  16413. end;
  16414. procedure TTestModule.TestClassInterface_COM_AssignVar;
  16415. begin
  16416. StartProgram(false);
  16417. Add([
  16418. '{$interfaces com}',
  16419. 'type',
  16420. ' IUnknown = interface',
  16421. ' function _AddRef: longint;',
  16422. ' function _Release: longint;',
  16423. ' end;',
  16424. ' TObject = class(IUnknown)',
  16425. ' function _AddRef: longint; virtual; abstract;',
  16426. ' function _Release: longint; virtual; abstract;',
  16427. ' end;',
  16428. 'var',
  16429. ' i: IUnknown;',
  16430. 'procedure DoGlobal(o: TObject);',
  16431. 'begin',
  16432. ' i:=nil;',
  16433. ' i:=o;',
  16434. ' i:=i;',
  16435. 'end;',
  16436. 'procedure DoLocal(o: TObject);',
  16437. 'const k: IUnknown = nil;',
  16438. 'var j: IUnknown;',
  16439. 'begin',
  16440. ' k:=o;',
  16441. ' k:=i;',
  16442. ' j:=o;',
  16443. ' j:=i;',
  16444. 'end;',
  16445. 'var o: TObject;',
  16446. 'begin',
  16447. ' i:=nil;',
  16448. ' i:=o;',
  16449. '']);
  16450. ConvertProgram;
  16451. CheckSource('TestClassInterface_COM_AssignVar',
  16452. LinesToStr([ // statements
  16453. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  16454. 'rtl.createClass($mod, "TObject", null, function () {',
  16455. ' this.$init = function () {',
  16456. ' };',
  16457. ' this.$final = function () {',
  16458. ' };',
  16459. ' rtl.addIntf(this, $mod.IUnknown);',
  16460. '});',
  16461. 'this.i = null;',
  16462. 'this.DoGlobal = function (o) {',
  16463. ' rtl.setIntfP($mod, "i", null);',
  16464. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  16465. ' rtl.setIntfP($mod, "i", $mod.i);',
  16466. '};',
  16467. 'var k = null;',
  16468. 'this.DoLocal = function (o) {',
  16469. ' var j = null;',
  16470. ' try{',
  16471. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  16472. ' k = rtl.setIntfL(k, $mod.i);',
  16473. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  16474. ' j = rtl.setIntfL(j, $mod.i);',
  16475. ' }finally{',
  16476. ' rtl._Release(j);',
  16477. ' };',
  16478. '};',
  16479. 'this.o = null;',
  16480. '']),
  16481. LinesToStr([ // $mod.$main
  16482. 'rtl.setIntfP($mod, "i", null);',
  16483. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  16484. '']));
  16485. end;
  16486. procedure TTestModule.TestClassInterface_COM_AssignArg;
  16487. begin
  16488. StartProgram(false);
  16489. Add([
  16490. '{$interfaces com}',
  16491. 'type',
  16492. ' IUnknown = interface',
  16493. ' function _AddRef: longint;',
  16494. ' function _Release: longint;',
  16495. ' end;',
  16496. ' TObject = class(IUnknown)',
  16497. ' function _AddRef: longint; virtual; abstract;',
  16498. ' function _Release: longint; virtual; abstract;',
  16499. ' end;',
  16500. 'procedure DoDefault(i, j: IUnknown);',
  16501. 'begin',
  16502. ' i:=nil;',
  16503. ' i:=j;',
  16504. 'end;',
  16505. 'begin',
  16506. '']);
  16507. ConvertProgram;
  16508. CheckSource('TestClassInterface_COM_AssignArg',
  16509. LinesToStr([ // statements
  16510. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  16511. 'rtl.createClass($mod, "TObject", null, function () {',
  16512. ' this.$init = function () {',
  16513. ' };',
  16514. ' this.$final = function () {',
  16515. ' };',
  16516. ' rtl.addIntf(this, $mod.IUnknown);',
  16517. '});',
  16518. 'this.DoDefault = function (i, j) {',
  16519. ' rtl._AddRef(i);',
  16520. ' try {',
  16521. ' i = rtl.setIntfL(i, null);',
  16522. ' i = rtl.setIntfL(i, j);',
  16523. ' } finally {',
  16524. ' rtl._Release(i);',
  16525. ' };',
  16526. '};',
  16527. '']),
  16528. LinesToStr([ // $mod.$main
  16529. '']));
  16530. end;
  16531. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  16532. begin
  16533. StartProgram(false);
  16534. Add([
  16535. '{$interfaces com}',
  16536. 'type',
  16537. ' IUnknown = interface',
  16538. ' function _AddRef: longint;',
  16539. ' function _Release: longint;',
  16540. ' end;',
  16541. ' TObject = class(IUnknown)',
  16542. ' function _AddRef: longint; virtual; abstract;',
  16543. ' function _Release: longint; virtual; abstract;',
  16544. ' end;',
  16545. 'function DoDefault(i: IUnknown): IUnknown;',
  16546. 'begin',
  16547. ' Result:=i;',
  16548. ' if Result<>nil then exit;',
  16549. 'end;',
  16550. 'begin',
  16551. '']);
  16552. ConvertProgram;
  16553. CheckSource('TestClassInterface_COM_FunctionResult',
  16554. LinesToStr([ // statements
  16555. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  16556. 'rtl.createClass($mod, "TObject", null, function () {',
  16557. ' this.$init = function () {',
  16558. ' };',
  16559. ' this.$final = function () {',
  16560. ' };',
  16561. ' rtl.addIntf(this, $mod.IUnknown);',
  16562. '});',
  16563. 'this.DoDefault = function (i) {',
  16564. ' var Result = null;',
  16565. ' var $ok = false;',
  16566. ' try {',
  16567. ' Result = rtl.setIntfL(Result, i);',
  16568. ' if(Result !== null){',
  16569. ' $ok = true;',
  16570. ' return Result;',
  16571. ' };',
  16572. ' $ok = true;',
  16573. ' } finally {',
  16574. ' if(!$ok) rtl._Release(Result);',
  16575. ' };',
  16576. ' return Result;',
  16577. '};',
  16578. '']),
  16579. LinesToStr([ // $mod.$main
  16580. '']));
  16581. end;
  16582. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  16583. begin
  16584. StartProgram(false);
  16585. Add([
  16586. '{$interfaces com}',
  16587. 'type',
  16588. ' IUnknown = interface',
  16589. ' function _AddRef: longint;',
  16590. ' function _Release: longint;',
  16591. ' end;',
  16592. ' TObject = class(IUnknown)',
  16593. ' function _AddRef: longint; virtual; abstract;',
  16594. ' function _Release: longint; virtual; abstract;',
  16595. ' function GetIntf: IUnknown; virtual;',
  16596. ' end;',
  16597. ' TMouse = class',
  16598. ' function GetIntf: IUnknown; override;',
  16599. ' end;',
  16600. 'function TObject.GetIntf: IUnknown; begin end;',
  16601. 'function TMouse.GetIntf: IUnknown;',
  16602. 'var i: IUnknown;',
  16603. 'begin',
  16604. ' inherited;',
  16605. ' inherited GetIntf;',
  16606. ' inherited GetIntf();',
  16607. ' Result:=inherited GetIntf;',
  16608. ' Result:=inherited GetIntf();',
  16609. ' i:=inherited GetIntf;',
  16610. ' i:=inherited GetIntf();',
  16611. 'end;',
  16612. 'begin',
  16613. '']);
  16614. ConvertProgram;
  16615. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  16616. LinesToStr([ // statements
  16617. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  16618. 'rtl.createClass($mod, "TObject", null, function () {',
  16619. ' this.$init = function () {',
  16620. ' };',
  16621. ' this.$final = function () {',
  16622. ' };',
  16623. ' this.GetIntf = function () {',
  16624. ' var Result = null;',
  16625. ' return Result;',
  16626. ' };',
  16627. ' rtl.addIntf(this, $mod.IUnknown);',
  16628. '});',
  16629. 'rtl.createClass($mod, "TMouse", $mod.TObject, function () {',
  16630. ' this.GetIntf = function () {',
  16631. ' var Result = null;',
  16632. ' var i = null;',
  16633. ' var $ir = rtl.createIntfRefs();',
  16634. ' var $ok = false;',
  16635. ' try {',
  16636. ' $ir.ref(1, $mod.TObject.GetIntf.apply(this, arguments));',
  16637. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  16638. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  16639. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  16640. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  16641. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  16642. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  16643. ' $ok = true;',
  16644. ' } finally {',
  16645. ' $ir.free();',
  16646. ' rtl._Release(i);',
  16647. ' if (!$ok) rtl._Release(Result);',
  16648. ' };',
  16649. ' return Result;',
  16650. ' };',
  16651. ' rtl.addIntf(this, $mod.IUnknown);',
  16652. '});',
  16653. '']),
  16654. LinesToStr([ // $mod.$main
  16655. '']));
  16656. end;
  16657. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  16658. begin
  16659. StartProgram(false);
  16660. Add([
  16661. '{$interfaces com}',
  16662. 'type',
  16663. ' IUnknown = interface',
  16664. ' function _AddRef: longint;',
  16665. ' function _Release: longint;',
  16666. ' end;',
  16667. ' TObject = class(IUnknown)',
  16668. ' function _AddRef: longint; virtual; abstract;',
  16669. ' function _Release: longint; virtual; abstract;',
  16670. ' end;',
  16671. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  16672. 'begin',
  16673. ' if i is IUnknown then ;',
  16674. ' if o is IUnknown then ;',
  16675. ' if i is TObject then ;',
  16676. ' i:=j as IUnknown;',
  16677. ' i:=o as IUnknown;',
  16678. ' o:=j as TObject;',
  16679. ' i:=IUnknown(j);',
  16680. ' i:=IUnknown(o);',
  16681. ' o:=TObject(i);',
  16682. 'end;',
  16683. 'begin',
  16684. '']);
  16685. ConvertProgram;
  16686. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  16687. LinesToStr([ // statements
  16688. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  16689. 'rtl.createClass($mod, "TObject", null, function () {',
  16690. ' this.$init = function () {',
  16691. ' };',
  16692. ' this.$final = function () {',
  16693. ' };',
  16694. ' rtl.addIntf(this, $mod.IUnknown);',
  16695. '});',
  16696. 'this.DoDefault = function (i, j, o) {',
  16697. ' rtl._AddRef(i);',
  16698. ' try {',
  16699. ' if ($mod.IUnknown.isPrototypeOf(i)) ;',
  16700. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  16701. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  16702. ' i = rtl.setIntfL(i, rtl.as(j, $mod.IUnknown));',
  16703. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  16704. ' o = rtl.intfAsClass(j, $mod.TObject);',
  16705. ' i = rtl.setIntfL(i, j);',
  16706. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  16707. ' o = rtl.intfToClass(i, $mod.TObject);',
  16708. ' } finally {',
  16709. ' rtl._Release(i);',
  16710. ' };',
  16711. '};',
  16712. '']),
  16713. LinesToStr([ // $mod.$main
  16714. '']));
  16715. end;
  16716. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  16717. begin
  16718. StartProgram(false);
  16719. Add([
  16720. '{$interfaces com}',
  16721. 'type',
  16722. ' IUnknown = interface',
  16723. ' function _AddRef: longint;',
  16724. ' function _Release: longint;',
  16725. ' end;',
  16726. ' TObject = class(IUnknown)',
  16727. ' function _AddRef: longint; virtual; abstract;',
  16728. ' function _Release: longint; virtual; abstract;',
  16729. ' end;',
  16730. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  16731. 'var o: TObject;',
  16732. 'begin',
  16733. ' DoIt(v,v,v,v);',
  16734. ' DoIt(o,o,k,k);',
  16735. 'end;',
  16736. 'procedure DoSome;',
  16737. 'var v: IUnknown;',
  16738. 'begin',
  16739. ' DoIt(v,v,v,v);',
  16740. 'end;',
  16741. 'var i: IUnknown;',
  16742. 'begin',
  16743. ' DoIt(i,i,i,i);',
  16744. '']);
  16745. ConvertProgram;
  16746. CheckSource('TestClassInterface_COM_PassAsArg',
  16747. LinesToStr([ // statements
  16748. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  16749. 'rtl.createClass($mod, "TObject", null, function () {',
  16750. ' this.$init = function () {',
  16751. ' };',
  16752. ' this.$final = function () {',
  16753. ' };',
  16754. ' rtl.addIntf(this, $mod.IUnknown);',
  16755. '});',
  16756. 'this.DoIt = function (v, j, k, l) {',
  16757. ' var o = null;',
  16758. ' var $ir = rtl.createIntfRefs();',
  16759. ' rtl._AddRef(v);',
  16760. ' try {',
  16761. ' $mod.DoIt(v, v, {',
  16762. ' get: function () {',
  16763. ' return v;',
  16764. ' },',
  16765. ' set: function (w) {',
  16766. ' v = rtl.setIntfL(v, w);',
  16767. ' }',
  16768. ' }, {',
  16769. ' get: function () {',
  16770. ' return v;',
  16771. ' },',
  16772. ' set: function (w) {',
  16773. ' v = rtl.setIntfL(v, w);',
  16774. ' }',
  16775. ' });',
  16776. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  16777. ' } finally {',
  16778. ' $ir.free();',
  16779. ' rtl._Release(v);',
  16780. ' };',
  16781. '};',
  16782. 'this.DoSome = function () {',
  16783. ' var v = null;',
  16784. ' try {',
  16785. ' $mod.DoIt(v, v, {',
  16786. ' get: function () {',
  16787. ' return v;',
  16788. ' },',
  16789. ' set: function (w) {',
  16790. ' v = rtl.setIntfL(v, w);',
  16791. ' }',
  16792. ' }, {',
  16793. ' get: function () {',
  16794. ' return v;',
  16795. ' },',
  16796. ' set: function (w) {',
  16797. ' v = rtl.setIntfL(v, w);',
  16798. ' }',
  16799. ' });',
  16800. ' } finally {',
  16801. ' rtl._Release(v);',
  16802. ' };',
  16803. '};',
  16804. 'this.i = null;',
  16805. '']),
  16806. LinesToStr([ // $mod.$main
  16807. '$mod.DoIt($mod.i, $mod.i, {',
  16808. ' p: $mod,',
  16809. ' get: function () {',
  16810. ' return this.p.i;',
  16811. ' },',
  16812. ' set: function (v) {',
  16813. ' rtl.setIntfP(this.p, "i", v);',
  16814. ' }',
  16815. '}, {',
  16816. ' p: $mod,',
  16817. ' get: function () {',
  16818. ' return this.p.i;',
  16819. ' },',
  16820. ' set: function (v) {',
  16821. ' rtl.setIntfP(this.p, "i", v);',
  16822. ' }',
  16823. '});',
  16824. '']));
  16825. end;
  16826. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  16827. begin
  16828. StartProgram(false);
  16829. Add([
  16830. '{$interfaces com}',
  16831. 'type',
  16832. ' IUnknown = interface',
  16833. ' function _AddRef: longint;',
  16834. ' function _Release: longint;',
  16835. ' end;',
  16836. ' TObject = class(IUnknown)',
  16837. ' function _AddRef: longint; virtual; abstract;',
  16838. ' function _Release: longint; virtual; abstract;',
  16839. ' end;',
  16840. 'procedure DoIt(out i);',
  16841. 'begin end;',
  16842. 'procedure DoSome;',
  16843. 'var v: IUnknown;',
  16844. 'begin',
  16845. ' DoIt(v);',
  16846. 'end;',
  16847. 'function GetIt: IUnknown;',
  16848. 'begin',
  16849. ' DoIt(Result);',
  16850. 'end;',
  16851. 'var i: IUnknown;',
  16852. 'begin',
  16853. ' DoIt(i);',
  16854. '']);
  16855. ConvertProgram;
  16856. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  16857. LinesToStr([ // statements
  16858. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  16859. 'rtl.createClass($mod, "TObject", null, function () {',
  16860. ' this.$init = function () {',
  16861. ' };',
  16862. ' this.$final = function () {',
  16863. ' };',
  16864. ' rtl.addIntf(this, $mod.IUnknown);',
  16865. '});',
  16866. 'this.DoIt = function (i) {',
  16867. '};',
  16868. 'this.DoSome = function () {',
  16869. ' var v = null;',
  16870. ' try {',
  16871. ' $mod.DoIt({',
  16872. ' get: function () {',
  16873. ' return v;',
  16874. ' },',
  16875. ' set: function (w) {',
  16876. ' v = w;',
  16877. ' }',
  16878. ' });',
  16879. ' } finally {',
  16880. ' rtl._Release(v);',
  16881. ' };',
  16882. '};',
  16883. 'this.GetIt = function () {',
  16884. ' var Result = null;',
  16885. ' var $ok = false;',
  16886. ' try {',
  16887. ' $mod.DoIt({',
  16888. ' get: function () {',
  16889. ' return Result;',
  16890. ' },',
  16891. ' set: function (v) {',
  16892. ' Result = v;',
  16893. ' }',
  16894. ' });',
  16895. ' $ok = true;',
  16896. ' } finally {',
  16897. ' if (!$ok) rtl._Release(Result);',
  16898. ' };',
  16899. ' return Result;',
  16900. '};',
  16901. 'this.i = null;',
  16902. '']),
  16903. LinesToStr([ // $mod.$main
  16904. 'try {',
  16905. ' $mod.DoIt({',
  16906. ' p: $mod,',
  16907. ' get: function () {',
  16908. ' return this.p.i;',
  16909. ' },',
  16910. ' set: function (v) {',
  16911. ' this.p.i = v;',
  16912. ' }',
  16913. ' });',
  16914. '} finally {',
  16915. ' rtl._Release($mod.i);',
  16916. '};',
  16917. '']));
  16918. end;
  16919. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  16920. begin
  16921. StartProgram(false);
  16922. Add([
  16923. '{$interfaces com}',
  16924. 'type',
  16925. ' IUnknown = interface',
  16926. ' function _AddRef: longint;',
  16927. ' function _Release: longint;',
  16928. ' end;',
  16929. ' TObject = class(IUnknown)',
  16930. ' function _AddRef: longint; virtual; abstract;',
  16931. ' function _Release: longint; virtual; abstract;',
  16932. ' end;',
  16933. 'function GetIt: IUnknown;',
  16934. 'begin',
  16935. 'end;',
  16936. 'procedure DoSome;',
  16937. 'var v: IUnknown;',
  16938. ' i: longint;',
  16939. 'begin',
  16940. ' v:=GetIt;',
  16941. ' v:=GetIt();',
  16942. ' GetIt()._AddRef;',
  16943. ' i:=GetIt()._AddRef;',
  16944. 'end;',
  16945. 'var v: IUnknown;',
  16946. ' i: longint;',
  16947. 'begin',
  16948. ' v:=GetIt;',
  16949. ' v:=GetIt();',
  16950. ' GetIt()._AddRef;',
  16951. ' i:=GetIt()._AddRef;',
  16952. '']);
  16953. ConvertProgram;
  16954. CheckSource('TestClassInterface_COM_FunctionInExpr',
  16955. LinesToStr([ // statements
  16956. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  16957. 'rtl.createClass($mod, "TObject", null, function () {',
  16958. ' this.$init = function () {',
  16959. ' };',
  16960. ' this.$final = function () {',
  16961. ' };',
  16962. ' rtl.addIntf(this, $mod.IUnknown);',
  16963. '});',
  16964. 'this.GetIt = function () {',
  16965. ' var Result = null;',
  16966. ' return Result;',
  16967. '};',
  16968. 'this.DoSome = function () {',
  16969. ' var v = null;',
  16970. ' var i = 0;',
  16971. ' var $ir = rtl.createIntfRefs();',
  16972. ' try {',
  16973. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  16974. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  16975. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  16976. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  16977. ' } finally {',
  16978. ' $ir.free();',
  16979. ' rtl._Release(v);',
  16980. ' };',
  16981. '};',
  16982. 'this.v = null;',
  16983. 'this.i = 0;',
  16984. '']),
  16985. LinesToStr([ // $mod.$main
  16986. 'var $ir = rtl.createIntfRefs();',
  16987. 'try {',
  16988. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  16989. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  16990. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  16991. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  16992. '} finally {',
  16993. ' $ir.free();',
  16994. '};',
  16995. '']));
  16996. end;
  16997. procedure TTestModule.TestClassInterface_COM_Property;
  16998. begin
  16999. StartProgram(false);
  17000. Add([
  17001. '{$interfaces com}',
  17002. 'type',
  17003. ' IUnknown = interface',
  17004. ' function _AddRef: longint;',
  17005. ' function _Release: longint;',
  17006. ' end;',
  17007. ' TObject = class(IUnknown)',
  17008. ' FAnt: IUnknown;',
  17009. ' function _AddRef: longint; virtual; abstract;',
  17010. ' function _Release: longint; virtual; abstract;',
  17011. ' function GetBird: IUnknown; virtual; abstract;',
  17012. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  17013. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  17014. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  17015. ' property Ant: IUnknown read FAnt write FAnt;',
  17016. ' property Bird: IUnknown read GetBird write SetBird;',
  17017. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  17018. ' end;',
  17019. 'procedure DoIt;',
  17020. 'var',
  17021. ' o: TObject;',
  17022. ' v: IUnknown;',
  17023. 'begin',
  17024. ' v:=o.Ant;',
  17025. ' o.Ant:=v;',
  17026. ' o.Ant:=o.Ant;',
  17027. ' v:=o.Bird;',
  17028. ' o.Bird:=v;',
  17029. ' o.Bird:=o.Bird;',
  17030. ' v:=o.Items[1];',
  17031. ' o.Items[2]:=v;',
  17032. ' o.Items[3]:=o.Items[4];',
  17033. ' v:=o[5];',
  17034. ' o[6]:=v;',
  17035. ' o[7]:=o[8];',
  17036. 'end;',
  17037. 'begin',
  17038. '']);
  17039. ConvertProgram;
  17040. CheckSource('TestClassInterface_COM_Property',
  17041. LinesToStr([ // statements
  17042. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17043. 'rtl.createClass($mod, "TObject", null, function () {',
  17044. ' this.$init = function () {',
  17045. ' this.FAnt = null;',
  17046. ' };',
  17047. ' this.$final = function () {',
  17048. ' this.FAnt = undefined;',
  17049. ' };',
  17050. ' rtl.addIntf(this, $mod.IUnknown);',
  17051. '});',
  17052. 'this.DoIt = function () {',
  17053. ' var o = null;',
  17054. ' var v = null;',
  17055. ' var $ir = rtl.createIntfRefs();',
  17056. ' try {',
  17057. ' v = rtl.setIntfL(v, o.FAnt);',
  17058. ' rtl.setIntfP(o, "FAnt", v);',
  17059. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  17060. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  17061. ' o.SetBird(v);',
  17062. ' o.SetBird($ir.ref(1, o.GetBird()));',
  17063. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  17064. ' o.SetItems(2, v);',
  17065. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  17066. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  17067. ' o.SetItems(6, v);',
  17068. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  17069. ' } finally {',
  17070. ' $ir.free();',
  17071. ' rtl._Release(v);',
  17072. ' };',
  17073. '};',
  17074. '']),
  17075. LinesToStr([ // $mod.$main
  17076. '']));
  17077. end;
  17078. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  17079. begin
  17080. StartProgram(false);
  17081. Add([
  17082. '{$interfaces com}',
  17083. 'type',
  17084. ' IUnknown = interface',
  17085. ' function _AddRef: longint;',
  17086. ' function _Release: longint;',
  17087. ' function GetBird: IUnknown;',
  17088. ' procedure SetBird(Value: IUnknown);',
  17089. ' function GetItems(Index: longint): IUnknown;',
  17090. ' procedure SetItems(Index: longint; Value: IUnknown);',
  17091. ' property Bird: IUnknown read GetBird write SetBird;',
  17092. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  17093. ' end;',
  17094. ' TObject = class(IUnknown)',
  17095. ' function _AddRef: longint; virtual; abstract;',
  17096. ' function _Release: longint; virtual; abstract;',
  17097. ' function GetBird: IUnknown; virtual; abstract;',
  17098. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  17099. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  17100. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  17101. ' end;',
  17102. 'procedure DoIt;',
  17103. 'var',
  17104. ' o: TObject;',
  17105. ' v: IUnknown;',
  17106. 'begin',
  17107. ' v:=v.Items[1];',
  17108. ' v.Items[2]:=v;',
  17109. ' v.Items[3]:=v.Items[4];',
  17110. ' v:=v[5];',
  17111. ' v[6]:=v;',
  17112. ' v[7]:=v[8];',
  17113. ' v[9].Bird.Bird:=v;',
  17114. ' v:=v.Bird[10].Bird',
  17115. 'end;',
  17116. 'begin',
  17117. '']);
  17118. ConvertProgram;
  17119. CheckSource('TestClassInterface_COM_IntfProperty',
  17120. LinesToStr([ // statements
  17121. 'rtl.createInterface($mod, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  17122. ' "_AddRef",',
  17123. ' "_Release",',
  17124. ' "GetBird",',
  17125. ' "SetBird",',
  17126. ' "GetItems",',
  17127. ' "SetItems"',
  17128. '], null);',
  17129. 'rtl.createClass($mod, "TObject", null, function () {',
  17130. ' this.$init = function () {',
  17131. ' };',
  17132. ' this.$final = function () {',
  17133. ' };',
  17134. ' rtl.addIntf(this, $mod.IUnknown);',
  17135. '});',
  17136. 'this.DoIt = function () {',
  17137. ' var o = null;',
  17138. ' var v = null;',
  17139. ' var $ir = rtl.createIntfRefs();',
  17140. ' try {',
  17141. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  17142. ' v.SetItems(2, v);',
  17143. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  17144. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  17145. ' v.SetItems(6, v);',
  17146. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  17147. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  17148. ' v = rtl.setIntfL(v, $ir.ref(6, v.$ir.ref(5, GetBird()).GetItems(10)).GetBird(), true);',
  17149. ' } finally {',
  17150. ' $ir.free();',
  17151. ' rtl._Release(v);',
  17152. ' };',
  17153. '};',
  17154. '']),
  17155. LinesToStr([ // $mod.$main
  17156. '']));
  17157. end;
  17158. procedure TTestModule.TestClassInterface_COM_Delegation;
  17159. begin
  17160. StartProgram(false);
  17161. Add([
  17162. '{$interfaces com}',
  17163. 'type',
  17164. ' IUnknown = interface',
  17165. ' function _AddRef: longint;',
  17166. ' function _Release: longint;',
  17167. ' end;',
  17168. ' IBird = interface(IUnknown)',
  17169. ' procedure Fly(s: string);',
  17170. ' end;',
  17171. ' IEagle = interface(IBird) end;',
  17172. ' IDove = interface(IBird) end;',
  17173. ' ISwallow = interface(IBird) end;',
  17174. ' TObject = class',
  17175. ' end;',
  17176. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  17177. ' function _AddRef: longint; virtual; abstract;',
  17178. ' function _Release: longint; virtual; abstract;',
  17179. ' procedure Fly(s: string); virtual; abstract;',
  17180. ' end;',
  17181. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  17182. ' function _AddRef: longint; virtual; abstract;',
  17183. ' function _Release: longint; virtual; abstract;',
  17184. ' FBirdIntf: IBird;',
  17185. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  17186. ' function GetEagleIntf: IEagle; virtual; abstract;',
  17187. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  17188. ' FDoveObj: TBird;',
  17189. ' property DoveObj: TBird read FDoveObj implements IDove;',
  17190. ' function GetSwallowObj: TBird; virtual; abstract;',
  17191. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  17192. ' end;',
  17193. 'begin',
  17194. '']);
  17195. ConvertProgram;
  17196. CheckSource('TestClassInterface_COM_Delegation',
  17197. LinesToStr([ // statements
  17198. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  17199. 'rtl.createInterface($mod, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], $mod.IUnknown);',
  17200. 'rtl.createInterface($mod, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], $mod.IBird);',
  17201. 'rtl.createInterface($mod, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], $mod.IBird);',
  17202. 'rtl.createInterface($mod, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], $mod.IBird);',
  17203. 'rtl.createClass($mod, "TObject", null, function () {',
  17204. ' this.$init = function () {',
  17205. ' };',
  17206. ' this.$final = function () {',
  17207. ' };',
  17208. '});',
  17209. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17210. ' rtl.addIntf(this, $mod.IBird);',
  17211. ' rtl.addIntf(this, $mod.IEagle);',
  17212. ' rtl.addIntf(this, $mod.IDove);',
  17213. ' rtl.addIntf(this, $mod.ISwallow);',
  17214. '});',
  17215. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  17216. ' this.$init = function () {',
  17217. ' $mod.TObject.$init.call(this);',
  17218. ' this.FBirdIntf = null;',
  17219. ' this.FDoveObj = null;',
  17220. ' };',
  17221. ' this.$final = function () {',
  17222. ' this.FBirdIntf = undefined;',
  17223. ' this.FDoveObj = undefined;',
  17224. ' $mod.TObject.$final.call(this);',
  17225. ' };',
  17226. ' this.$intfmaps = {',
  17227. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  17228. ' return rtl._AddRef(this.FBirdIntf);',
  17229. ' },',
  17230. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  17231. ' return this.GetEagleIntf();',
  17232. ' },',
  17233. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  17234. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  17235. ' },',
  17236. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  17237. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  17238. ' }',
  17239. ' };',
  17240. '});',
  17241. '']),
  17242. LinesToStr([ // $mod.$main
  17243. '']));
  17244. end;
  17245. procedure TTestModule.TestClassInterface_COM_With;
  17246. begin
  17247. StartProgram(false);
  17248. Add([
  17249. '{$interfaces com}',
  17250. 'type',
  17251. ' IUnknown = interface',
  17252. ' function _AddRef: longint;',
  17253. ' function _Release: longint;',
  17254. ' function GetAnt: IUnknown;',
  17255. ' property Ant: IUnknown read GetAnt;',
  17256. ' end;',
  17257. ' TObject = class(IUnknown)',
  17258. ' function _AddRef: longint; virtual; abstract;',
  17259. ' function _Release: longint; virtual; abstract;',
  17260. ' function GetAnt: IUnknown; virtual; abstract;',
  17261. ' property Ant: IUnknown read GetAnt;',
  17262. ' end;',
  17263. 'procedure DoIt;',
  17264. 'var',
  17265. ' i: IUnknown;',
  17266. 'begin',
  17267. ' with i do ',
  17268. ' GetAnt;',
  17269. ' with i.Ant, Ant do ',
  17270. ' GetAnt;',
  17271. 'end;',
  17272. 'begin',
  17273. '']);
  17274. ConvertProgram;
  17275. CheckSource('TestClassInterface_COM_With',
  17276. LinesToStr([ // statements
  17277. 'rtl.createInterface($mod, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  17278. 'rtl.createClass($mod, "TObject", null, function () {',
  17279. ' this.$init = function () {',
  17280. ' };',
  17281. ' this.$final = function () {',
  17282. ' };',
  17283. ' rtl.addIntf(this, $mod.IUnknown);',
  17284. '});',
  17285. 'this.DoIt = function () {',
  17286. ' var i = null;',
  17287. ' var $ir = rtl.createIntfRefs();',
  17288. ' try {',
  17289. ' $ir.ref(1, i.GetAnt());',
  17290. ' var $with1 = $ir.ref(2, i.GetAnt());',
  17291. ' var $with2 = $ir.ref(3, $with1.GetAnt());',
  17292. ' $ir.ref(4, $with2.GetAnt());',
  17293. ' } finally {',
  17294. ' $ir.free();',
  17295. ' };',
  17296. '};',
  17297. '']),
  17298. LinesToStr([ // $mod.$main
  17299. '']));
  17300. end;
  17301. procedure TTestModule.TestClassInterface_COM_ForIn;
  17302. begin
  17303. StartProgram(false);
  17304. Add([
  17305. '{$interfaces com}',
  17306. 'type',
  17307. ' IUnknown = interface end;',
  17308. ' TObject = class',
  17309. ' Id: longint;',
  17310. ' end;',
  17311. ' IEnumerator = interface(IUnknown)',
  17312. ' function GetCurrent: TObject;',
  17313. ' function MoveNext: Boolean;',
  17314. ' property Current: TObject read GetCurrent;',
  17315. ' end;',
  17316. ' IEnumerable = interface(IUnknown)',
  17317. ' function GetEnumerator: IEnumerator;',
  17318. ' end;',
  17319. 'var',
  17320. ' o: TObject;',
  17321. ' i: IEnumerable;',
  17322. 'begin',
  17323. ' for o in i do o.Id:=3;',
  17324. '']);
  17325. ConvertProgram;
  17326. CheckSource('TestClassInterface_COM_ForIn',
  17327. LinesToStr([ // statements
  17328. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17329. 'rtl.createClass($mod, "TObject", null, function () {',
  17330. ' this.$init = function () {',
  17331. ' this.Id = 0;',
  17332. ' };',
  17333. ' this.$final = function () {',
  17334. ' };',
  17335. '});',
  17336. 'rtl.createInterface($mod, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], $mod.IUnknown);',
  17337. 'rtl.createInterface($mod, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], $mod.IUnknown);',
  17338. 'this.o = null;',
  17339. 'this.i = null;',
  17340. '']),
  17341. LinesToStr([ // $mod.$main
  17342. 'var $in1 = $mod.i.GetEnumerator();',
  17343. 'try {',
  17344. ' while ($in1.MoveNext()) {',
  17345. ' $mod.o = $in1.GetCurrent();',
  17346. ' $mod.o.Id = 3;',
  17347. ' }',
  17348. '} finally {',
  17349. ' rtl._Release($in1)',
  17350. '};',
  17351. '']));
  17352. end;
  17353. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  17354. begin
  17355. StartProgram(false);
  17356. Add([
  17357. '{$interfaces com}',
  17358. 'type',
  17359. ' IUnknown = interface',
  17360. ' function _AddRef: longint;',
  17361. ' function _Release: longint;',
  17362. ' end;',
  17363. ' TObject = class',
  17364. ' end;',
  17365. ' TArrOfIntf = array of IUnknown;',
  17366. 'begin',
  17367. '']);
  17368. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  17369. ConvertProgram;
  17370. end;
  17371. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  17372. begin
  17373. StartProgram(false);
  17374. Add([
  17375. '{$interfaces com}',
  17376. 'type',
  17377. ' IUnknown = interface',
  17378. ' function _AddRef: longint;',
  17379. ' function _Release: longint;',
  17380. ' end;',
  17381. ' TRec = record',
  17382. ' i: IUnknown;',
  17383. ' end;',
  17384. 'begin',
  17385. '']);
  17386. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  17387. ConvertProgram;
  17388. end;
  17389. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  17390. begin
  17391. StartUnit(false);
  17392. Add([
  17393. '{$interfaces com}',
  17394. 'interface',
  17395. 'implementation',
  17396. 'type',
  17397. ' IUnknown = interface',
  17398. ' function _AddRef: longint;',
  17399. ' end;',
  17400. ' TObject = class(IUnknown)',
  17401. ' function _AddRef: longint;',
  17402. ' end;',
  17403. 'function TObject._AddRef: longint; begin end;',
  17404. 'var i: IUnknown;',
  17405. ' o: TObject;',
  17406. 'initialization',
  17407. ' i:=nil;',
  17408. ' i:=i;',
  17409. ' i:=o;',
  17410. ' if (o as IUnknown)=nil then ;',
  17411. '']);
  17412. ConvertUnit;
  17413. CheckSource('TestClassInterface_COM_UnitInitialization',
  17414. LinesToStr([ // statements
  17415. 'var $impl = $mod.$impl;',
  17416. '']),
  17417. LinesToStr([ // this.$init
  17418. 'var $ir = rtl.createIntfRefs();',
  17419. 'try {',
  17420. ' rtl.setIntfP($impl, "i", null);',
  17421. ' rtl.setIntfP($impl, "i", $impl.i);',
  17422. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  17423. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  17424. '} finally {',
  17425. ' $ir.free();',
  17426. '};',
  17427. '']),
  17428. LinesToStr([ // implementation
  17429. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  17430. 'rtl.createClass($impl, "TObject", null, function () {',
  17431. ' this.$init = function () {',
  17432. ' };',
  17433. ' this.$final = function () {',
  17434. ' };',
  17435. ' this._AddRef = function () {',
  17436. ' var Result = 0;',
  17437. ' return Result;',
  17438. ' };',
  17439. ' rtl.addIntf(this, $impl.IUnknown);',
  17440. '});',
  17441. '$impl.i = null;',
  17442. '$impl.o = null;',
  17443. ''])
  17444. );
  17445. end;
  17446. procedure TTestModule.TestClassInterface_GUID;
  17447. begin
  17448. StartProgram(false);
  17449. Add([
  17450. '{$interfaces corba}',
  17451. 'type',
  17452. ' IUnknown = interface',
  17453. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  17454. ' end;',
  17455. ' TObject = class end;',
  17456. ' TGUID = record D1, D2, D3, D4: word; end;',
  17457. ' TAliasGUID = TGUID;',
  17458. ' TGUIDString = type string;',
  17459. ' TAliasGUIDString = TGUIDString;',
  17460. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  17461. 'begin end;',
  17462. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  17463. 'begin end;',
  17464. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  17465. 'begin end;',
  17466. 'var',
  17467. ' i: IUnknown;',
  17468. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  17469. ' s: TAliasGUIDString;',
  17470. 'begin',
  17471. ' DoConstGUIDIt(IUnknown);',
  17472. ' DoDefGUID(IUnknown);',
  17473. ' DoStr(IUnknown);',
  17474. ' DoConstGUIDIt(i);',
  17475. ' DoDefGUID(i);',
  17476. ' DoStr(i);',
  17477. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  17478. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  17479. ' DoStr(g);',
  17480. ' g:=i;',
  17481. ' g:=IUnknown;',
  17482. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  17483. ' s:=i;',
  17484. ' s:=IUnknown;',
  17485. ' s:=g;',
  17486. ' if g=i then ;',
  17487. ' if i=g then ;',
  17488. ' if g=IUnknown then ;',
  17489. ' if IUnknown=g then ;',
  17490. ' if s=i then ;',
  17491. ' if i=s then ;',
  17492. ' if s=IUnknown then ;',
  17493. ' if IUnknown=s then ;',
  17494. ' if s=g then ;',
  17495. ' if g=s then ;',
  17496. '']);
  17497. ConvertProgram;
  17498. CheckSource('TestClassInterface_GUID',
  17499. LinesToStr([ // statements
  17500. 'rtl.createInterface($mod, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  17501. 'rtl.createClass($mod, "TObject", null, function () {',
  17502. ' this.$init = function () {',
  17503. ' };',
  17504. ' this.$final = function () {',
  17505. ' };',
  17506. '});',
  17507. 'rtl.recNewT($mod, "TGUID", function () {',
  17508. ' this.D1 = 0;',
  17509. ' this.D2 = 0;',
  17510. ' this.D3 = 0;',
  17511. ' this.D4 = 0;',
  17512. ' this.$eq = function (b) {',
  17513. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  17514. ' };',
  17515. ' this.$assign = function (s) {',
  17516. ' this.D1 = s.D1;',
  17517. ' this.D2 = s.D2;',
  17518. ' this.D3 = s.D3;',
  17519. ' this.D4 = s.D4;',
  17520. ' return this;',
  17521. ' };',
  17522. '});',
  17523. 'this.DoConstGUIDIt = function (g) {',
  17524. '};',
  17525. 'this.DoDefGUID = function (g) {',
  17526. '};',
  17527. 'this.DoStr = function (s) {',
  17528. '};',
  17529. 'this.i = null;',
  17530. 'this.g = $mod.TGUID.$clone({',
  17531. ' D1: 0xD91C9AF4,',
  17532. ' D2: 0x3C93,',
  17533. ' D3: 0x420F,',
  17534. ' D4: [',
  17535. ' 0xA3,',
  17536. ' 0x03,',
  17537. ' 0xBF,',
  17538. ' 0x5B,',
  17539. ' 0xA8,',
  17540. ' 0x2B,',
  17541. ' 0xFD,',
  17542. ' 0x23',
  17543. ' ]',
  17544. '});',
  17545. 'this.s = "";',
  17546. '']),
  17547. LinesToStr([ // $mod.$main
  17548. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  17549. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  17550. '$mod.DoStr($mod.IUnknown.$guid);',
  17551. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  17552. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  17553. '$mod.DoStr($mod.i.$guid);',
  17554. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  17555. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  17556. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  17557. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  17558. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  17559. '$mod.g.$assign({',
  17560. ' D1: 0xD91C9AF4,',
  17561. ' D2: 0x3C93,',
  17562. ' D3: 0x420F,',
  17563. ' D4: [',
  17564. ' 0xA3,',
  17565. ' 0x03,',
  17566. ' 0xBF,',
  17567. ' 0x5B,',
  17568. ' 0xA8,',
  17569. ' 0x2B,',
  17570. ' 0xFD,',
  17571. ' 0x23',
  17572. ' ]',
  17573. '});',
  17574. '$mod.s = $mod.i.$guid;',
  17575. '$mod.s = $mod.IUnknown.$guid;',
  17576. '$mod.s = rtl.guidrToStr($mod.g);',
  17577. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  17578. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  17579. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  17580. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  17581. 'if ($mod.s === $mod.i.$guid) ;',
  17582. 'if ($mod.i.$guid === $mod.s) ;',
  17583. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  17584. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  17585. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  17586. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  17587. '']));
  17588. end;
  17589. procedure TTestModule.TestClassInterface_GUIDProperty;
  17590. begin
  17591. StartProgram(false);
  17592. Add([
  17593. '{$interfaces corba}',
  17594. 'type',
  17595. ' IUnknown = interface',
  17596. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  17597. ' end;',
  17598. ' TGUID = record D1, D2, D3, D4: word; end;',
  17599. ' TAliasGUID = TGUID;',
  17600. ' TGUIDString = type string;',
  17601. ' TAliasGUIDString = TGUIDString;',
  17602. ' TObject = class',
  17603. ' function GetG: TAliasGUID; virtual; abstract;',
  17604. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  17605. ' function GetS: TAliasGUIDString; virtual; abstract;',
  17606. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  17607. ' property g: TAliasGUID read GetG write SetG;',
  17608. ' property s: TAliasGUIDString read GetS write SetS;',
  17609. ' end;',
  17610. 'var o: TObject;',
  17611. 'begin',
  17612. ' o.g:=IUnknown;',
  17613. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  17614. ' o.s:=IUnknown;',
  17615. ' o.s:=o.g;',
  17616. '']);
  17617. ConvertProgram;
  17618. CheckSource('TestClassInterface_GUIDProperty',
  17619. LinesToStr([ // statements
  17620. 'rtl.createInterface($mod, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  17621. 'rtl.recNewT($mod, "TGUID", function () {',
  17622. ' this.D1 = 0;',
  17623. ' this.D2 = 0;',
  17624. ' this.D3 = 0;',
  17625. ' this.D4 = 0;',
  17626. ' this.$eq = function (b) {',
  17627. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  17628. ' };',
  17629. ' this.$assign = function (s) {',
  17630. ' this.D1 = s.D1;',
  17631. ' this.D2 = s.D2;',
  17632. ' this.D3 = s.D3;',
  17633. ' this.D4 = s.D4;',
  17634. ' return this;',
  17635. ' };',
  17636. '});',
  17637. 'rtl.createClass($mod, "TObject", null, function () {',
  17638. ' this.$init = function () {',
  17639. ' };',
  17640. ' this.$final = function () {',
  17641. ' };',
  17642. '});',
  17643. 'this.o = null;',
  17644. '']),
  17645. LinesToStr([ // $mod.$main
  17646. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  17647. '$mod.o.SetG({',
  17648. ' D1: 0xD91C9AF4,',
  17649. ' D2: 0x3C93,',
  17650. ' D3: 0x420F,',
  17651. ' D4: [',
  17652. ' 0xA3,',
  17653. ' 0x03,',
  17654. ' 0xBF,',
  17655. ' 0x5B,',
  17656. ' 0xA8,',
  17657. ' 0x2B,',
  17658. ' 0xFD,',
  17659. ' 0x23',
  17660. ' ]',
  17661. '});',
  17662. '$mod.o.SetS($mod.IUnknown.$guid);',
  17663. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  17664. '']));
  17665. end;
  17666. procedure TTestModule.TestProcType;
  17667. begin
  17668. StartProgram(false);
  17669. Add([
  17670. 'type',
  17671. ' TProcInt = procedure(vI: longint = 1);',
  17672. 'procedure DoIt(vJ: longint);',
  17673. 'begin end;',
  17674. 'var',
  17675. ' b: boolean;',
  17676. ' vP, vQ: tprocint;',
  17677. 'begin',
  17678. ' vp:=nil;',
  17679. ' vp:=vp;',
  17680. ' vp:=@doit;',
  17681. ' vp;',
  17682. ' vp();',
  17683. ' vp(2);',
  17684. ' b:=vp=nil;',
  17685. ' b:=nil=vp;',
  17686. ' b:=vp=vq;',
  17687. ' b:=vp=@doit;',
  17688. ' b:=@doit=vp;',
  17689. ' b:=vp<>nil;',
  17690. ' b:=nil<>vp;',
  17691. ' b:=vp<>vq;',
  17692. ' b:=vp<>@doit;',
  17693. ' b:=@doit<>vp;',
  17694. ' b:=Assigned(vp);',
  17695. ' if Assigned(vp) then ;']);
  17696. ConvertProgram;
  17697. CheckSource('TestProcType',
  17698. LinesToStr([ // statements
  17699. 'this.DoIt = function(vJ) {',
  17700. '};',
  17701. 'this.b = false;',
  17702. 'this.vP = null;',
  17703. 'this.vQ = null;'
  17704. ]),
  17705. LinesToStr([ // $mod.$main
  17706. '$mod.vP = null;',
  17707. '$mod.vP = $mod.vP;',
  17708. '$mod.vP = $mod.DoIt;',
  17709. '$mod.vP(1);',
  17710. '$mod.vP(1);',
  17711. '$mod.vP(2);',
  17712. '$mod.b = $mod.vP === null;',
  17713. '$mod.b = null === $mod.vP;',
  17714. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  17715. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  17716. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  17717. '$mod.b = $mod.vP !== null;',
  17718. '$mod.b = null !== $mod.vP;',
  17719. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  17720. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  17721. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  17722. '$mod.b = $mod.vP != null;',
  17723. 'if ($mod.vP != null) ;',
  17724. '']));
  17725. end;
  17726. procedure TTestModule.TestProcType_Arg;
  17727. begin
  17728. StartProgram(false);
  17729. Add([
  17730. 'type',
  17731. ' TProcInt = procedure(vI: longint = 1);',
  17732. 'procedure DoIt(vJ: longint); begin end;',
  17733. 'procedure DoSome(vP, vQ: TProcInt);',
  17734. 'var',
  17735. ' b: boolean;',
  17736. 'begin',
  17737. ' vp:=nil;',
  17738. ' vp:=vp;',
  17739. ' vp:=@doit;',
  17740. ' vp;',
  17741. ' vp();',
  17742. ' vp(2);',
  17743. ' b:=vp=nil;',
  17744. ' b:=nil=vp;',
  17745. ' b:=vp=vq;',
  17746. ' b:=vp=@doit;',
  17747. ' b:=@doit=vp;',
  17748. ' b:=vp<>nil;',
  17749. ' b:=nil<>vp;',
  17750. ' b:=vp<>vq;',
  17751. ' b:=vp<>@doit;',
  17752. ' b:=@doit<>vp;',
  17753. ' b:=Assigned(vp);',
  17754. ' if Assigned(vp) then ;',
  17755. 'end;',
  17756. 'begin',
  17757. ' DoSome(@DoIt,nil);']);
  17758. ConvertProgram;
  17759. CheckSource('TestProcType_Arg',
  17760. LinesToStr([ // statements
  17761. 'this.DoIt = function(vJ) {',
  17762. '};',
  17763. 'this.DoSome = function(vP, vQ) {',
  17764. ' var b = false;',
  17765. ' vP = null;',
  17766. ' vP = vP;',
  17767. ' vP = $mod.DoIt;',
  17768. ' vP(1);',
  17769. ' vP(1);',
  17770. ' vP(2);',
  17771. ' b = vP === null;',
  17772. ' b = null === vP;',
  17773. ' b = rtl.eqCallback(vP,vQ);',
  17774. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  17775. ' b = rtl.eqCallback($mod.DoIt, vP);',
  17776. ' b = vP !== null;',
  17777. ' b = null !== vP;',
  17778. ' b = !rtl.eqCallback(vP, vQ);',
  17779. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  17780. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  17781. ' b = vP != null;',
  17782. ' if (vP != null) ;',
  17783. '};',
  17784. '']),
  17785. LinesToStr([ // $mod.$main
  17786. '$mod.DoSome($mod.DoIt,null);',
  17787. '']));
  17788. end;
  17789. procedure TTestModule.TestProcType_FunctionFPC;
  17790. begin
  17791. StartProgram(false);
  17792. Add('type');
  17793. Add(' TFuncInt = function(vA: longint = 1): longint;');
  17794. Add('function DoIt(vI: longint): longint;');
  17795. Add('begin end;');
  17796. Add('var');
  17797. Add(' b: boolean;');
  17798. Add(' vP, vQ: tfuncint;');
  17799. Add('begin');
  17800. Add(' vp:=nil;');
  17801. Add(' vp:=vp;');
  17802. Add(' vp:=@doit;'); // ok in fpc and delphi
  17803. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  17804. Add(' vp;'); // ok in fpc and delphi
  17805. Add(' vp();');
  17806. Add(' vp(2);');
  17807. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  17808. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  17809. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  17810. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  17811. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  17812. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  17813. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  17814. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  17815. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  17816. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  17817. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  17818. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  17819. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  17820. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  17821. Add(' b:=Assigned(vp);');
  17822. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  17823. Add(' doit(vp());'); // ok in fpc and delphi
  17824. Add(' doit(vp(2));'); // ok in fpc and delphi
  17825. ConvertProgram;
  17826. CheckSource('TestProcType_FunctionFPC',
  17827. LinesToStr([ // statements
  17828. 'this.DoIt = function(vI) {',
  17829. ' var Result = 0;',
  17830. ' return Result;',
  17831. '};',
  17832. 'this.b = false;',
  17833. 'this.vP = null;',
  17834. 'this.vQ = null;'
  17835. ]),
  17836. LinesToStr([ // $mod.$main
  17837. '$mod.vP = null;',
  17838. '$mod.vP = $mod.vP;',
  17839. '$mod.vP = $mod.DoIt;',
  17840. '$mod.vP(1);',
  17841. '$mod.vP(1);',
  17842. '$mod.vP(2);',
  17843. '$mod.b = $mod.vP === null;',
  17844. '$mod.b = null === $mod.vP;',
  17845. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  17846. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  17847. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  17848. '$mod.b = 4 === $mod.vP(1);',
  17849. '$mod.b = $mod.vP !== null;',
  17850. '$mod.b = null !== $mod.vP;',
  17851. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  17852. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  17853. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  17854. '$mod.b = 6 !== $mod.vP(1);',
  17855. '$mod.b = $mod.vP != null;',
  17856. '$mod.DoIt($mod.vP(1));',
  17857. '$mod.DoIt($mod.vP(2));',
  17858. '']));
  17859. end;
  17860. procedure TTestModule.TestProcType_FunctionDelphi;
  17861. begin
  17862. StartProgram(false);
  17863. Add('{$mode Delphi}');
  17864. Add('type');
  17865. Add(' TFuncInt = function(vA: longint = 1): longint;');
  17866. Add('function DoIt(vI: longint): longint;');
  17867. Add('begin end;');
  17868. Add('var');
  17869. Add(' b: boolean;');
  17870. Add(' vP, vQ: tfuncint;');
  17871. Add('begin');
  17872. Add(' vp:=nil;');
  17873. Add(' vp:=vp;');
  17874. Add(' vp:=@doit;'); // ok in fpc and delphi
  17875. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  17876. Add(' vp;'); // ok in fpc and delphi
  17877. Add(' vp();');
  17878. Add(' vp(2);');
  17879. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  17880. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  17881. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  17882. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  17883. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  17884. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  17885. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  17886. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  17887. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  17888. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  17889. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  17890. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  17891. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  17892. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  17893. Add(' b:=Assigned(vp);');
  17894. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  17895. Add(' doit(vp());'); // ok in fpc and delphi
  17896. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  17897. ConvertProgram;
  17898. CheckSource('TestProcType_FunctionDelphi',
  17899. LinesToStr([ // statements
  17900. 'this.DoIt = function(vI) {',
  17901. ' var Result = 0;',
  17902. ' return Result;',
  17903. '};',
  17904. 'this.b = false;',
  17905. 'this.vP = null;',
  17906. 'this.vQ = null;'
  17907. ]),
  17908. LinesToStr([ // $mod.$main
  17909. '$mod.vP = null;',
  17910. '$mod.vP = $mod.vP;',
  17911. '$mod.vP = $mod.DoIt;',
  17912. '$mod.vP = $mod.DoIt;',
  17913. '$mod.vP(1);',
  17914. '$mod.vP(1);',
  17915. '$mod.vP(2);',
  17916. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  17917. '$mod.b = $mod.vP(1) === 3;',
  17918. '$mod.b = 4 === $mod.vP(1);',
  17919. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  17920. '$mod.b = $mod.vP(1) !== 5;',
  17921. '$mod.b = 6 !== $mod.vP(1);',
  17922. '$mod.b = $mod.vP != null;',
  17923. '$mod.DoIt($mod.vP(1));',
  17924. '$mod.DoIt($mod.vP(1));',
  17925. '$mod.DoIt($mod.vP(2));',
  17926. '']));
  17927. end;
  17928. procedure TTestModule.TestProcType_ProcedureDelphi;
  17929. begin
  17930. StartProgram(false);
  17931. Add('{$mode Delphi}');
  17932. Add('type');
  17933. Add(' TProc = procedure;');
  17934. Add('procedure DoIt;');
  17935. Add('begin end;');
  17936. Add('var');
  17937. Add(' b: boolean;');
  17938. Add(' vP, vQ: tproc;');
  17939. Add('begin');
  17940. Add(' vp:=nil;');
  17941. Add(' vp:=vp;');
  17942. Add(' vp:=vq;');
  17943. 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
  17944. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  17945. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  17946. Add(' vp;'); // ok in fpc and delphi
  17947. Add(' vp();');
  17948. // equal
  17949. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  17950. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  17951. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  17952. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  17953. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  17954. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  17955. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  17956. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  17957. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  17958. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  17959. // unequal
  17960. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  17961. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  17962. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  17963. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  17964. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  17965. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  17966. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  17967. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  17968. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  17969. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  17970. Add(' b:=Assigned(vp);');
  17971. ConvertProgram;
  17972. CheckSource('TestProcType_ProcedureDelphi',
  17973. LinesToStr([ // statements
  17974. 'this.DoIt = function() {',
  17975. '};',
  17976. 'this.b = false;',
  17977. 'this.vP = null;',
  17978. 'this.vQ = null;'
  17979. ]),
  17980. LinesToStr([ // $mod.$main
  17981. '$mod.vP = null;',
  17982. '$mod.vP = $mod.vP;',
  17983. '$mod.vP = $mod.vQ;',
  17984. '$mod.vP = $mod.DoIt;',
  17985. '$mod.vP = $mod.DoIt;',
  17986. '$mod.vP();',
  17987. '$mod.vP();',
  17988. '$mod.b = $mod.vP === null;',
  17989. '$mod.b = null === $mod.vP;',
  17990. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  17991. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  17992. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  17993. '$mod.b = $mod.vP !== null;',
  17994. '$mod.b = null !== $mod.vP;',
  17995. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  17996. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  17997. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  17998. '$mod.b = $mod.vP != null;',
  17999. '']));
  18000. end;
  18001. procedure TTestModule.TestProcType_AsParam;
  18002. begin
  18003. StartProgram(false);
  18004. Add('type');
  18005. Add(' TFuncInt = function(vA: longint = 1): longint;');
  18006. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  18007. Add('var vJ: tfuncint;');
  18008. Add('begin');
  18009. Add(' vg:=vg;');
  18010. Add(' vj:=vh;');
  18011. Add(' vi:=vi;');
  18012. Add(' doit(vg,vg,vg);');
  18013. Add(' doit(vh,vh,vj);');
  18014. Add(' doit(vi,vi,vi);');
  18015. Add(' doit(vj,vj,vj);');
  18016. Add('end;');
  18017. Add('var i: tfuncint;');
  18018. Add('begin');
  18019. Add(' doit(i,i,i);');
  18020. ConvertProgram;
  18021. CheckSource('TestProcType_AsParam',
  18022. LinesToStr([ // statements
  18023. 'this.DoIt = function (vG,vH,vI) {',
  18024. ' var vJ = null;',
  18025. ' vG = vG;',
  18026. ' vJ = vH;',
  18027. ' vI.set(vI.get());',
  18028. ' $mod.DoIt(vG, vG, {',
  18029. ' get: function () {',
  18030. ' return vG;',
  18031. ' },',
  18032. ' set: function (v) {',
  18033. ' vG = v;',
  18034. ' }',
  18035. ' });',
  18036. ' $mod.DoIt(vH, vH, {',
  18037. ' get: function () {',
  18038. ' return vJ;',
  18039. ' },',
  18040. ' set: function (v) {',
  18041. ' vJ = v;',
  18042. ' }',
  18043. ' });',
  18044. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  18045. ' $mod.DoIt(vJ, vJ, {',
  18046. ' get: function () {',
  18047. ' return vJ;',
  18048. ' },',
  18049. ' set: function (v) {',
  18050. ' vJ = v;',
  18051. ' }',
  18052. ' });',
  18053. '};',
  18054. 'this.i = null;'
  18055. ]),
  18056. LinesToStr([
  18057. '$mod.DoIt($mod.i,$mod.i,{',
  18058. ' p: $mod,',
  18059. ' get: function () {',
  18060. ' return this.p.i;',
  18061. ' },',
  18062. ' set: function (v) {',
  18063. ' this.p.i = v;',
  18064. ' }',
  18065. '});'
  18066. ]));
  18067. end;
  18068. procedure TTestModule.TestProcType_MethodFPC;
  18069. begin
  18070. StartProgram(false);
  18071. Add('type');
  18072. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  18073. Add(' TObject = class');
  18074. Add(' function DoIt(vA: longint = 1): longint;');
  18075. Add(' end;');
  18076. Add('function TObject.DoIt(vA: longint = 1): longint;');
  18077. Add('begin');
  18078. Add('end;');
  18079. Add('var');
  18080. Add(' Obj: TObject;');
  18081. Add(' vP: tfuncint;');
  18082. Add(' b: boolean;');
  18083. Add('begin');
  18084. Add(' vp:[email protected];'); // ok in fpc and delphi
  18085. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  18086. Add(' vp;'); // ok in fpc and delphi
  18087. Add(' vp();');
  18088. Add(' vp(2);');
  18089. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  18090. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  18091. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  18092. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  18093. ConvertProgram;
  18094. CheckSource('TestProcType_MethodFPC',
  18095. LinesToStr([ // statements
  18096. 'rtl.createClass($mod, "TObject", null, function () {',
  18097. ' this.$init = function () {',
  18098. ' };',
  18099. ' this.$final = function () {',
  18100. ' };',
  18101. ' this.DoIt = function (vA) {',
  18102. ' var Result = 0;',
  18103. ' return Result;',
  18104. ' };',
  18105. '});',
  18106. 'this.Obj = null;',
  18107. 'this.vP = null;',
  18108. 'this.b = false;'
  18109. ]),
  18110. LinesToStr([
  18111. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  18112. '$mod.vP(1);',
  18113. '$mod.vP(1);',
  18114. '$mod.vP(2);',
  18115. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  18116. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  18117. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  18118. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  18119. '']));
  18120. end;
  18121. procedure TTestModule.TestProcType_MethodDelphi;
  18122. begin
  18123. StartProgram(false);
  18124. Add('{$mode delphi}');
  18125. Add('type');
  18126. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  18127. Add(' TObject = class');
  18128. Add(' function DoIt(vA: longint = 1): longint;');
  18129. Add(' end;');
  18130. Add('function TObject.DoIt(vA: longint = 1): longint;');
  18131. Add('begin');
  18132. Add('end;');
  18133. Add('var');
  18134. Add(' Obj: TObject;');
  18135. Add(' vP: tfuncint;');
  18136. Add(' b: boolean;');
  18137. Add('begin');
  18138. Add(' vp:[email protected];'); // ok in fpc and delphi
  18139. Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  18140. Add(' vp;'); // ok in fpc and delphi
  18141. Add(' vp();');
  18142. Add(' vp(2);');
  18143. //Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  18144. //Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  18145. //Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  18146. //Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  18147. ConvertProgram;
  18148. CheckSource('TestProcType_MethodDelphi',
  18149. LinesToStr([ // statements
  18150. 'rtl.createClass($mod, "TObject", null, function () {',
  18151. ' this.$init = function () {',
  18152. ' };',
  18153. ' this.$final = function () {',
  18154. ' };',
  18155. ' this.DoIt = function (vA) {',
  18156. ' var Result = 0;',
  18157. ' return Result;',
  18158. ' };',
  18159. '});',
  18160. 'this.Obj = null;',
  18161. 'this.vP = null;',
  18162. 'this.b = false;'
  18163. ]),
  18164. LinesToStr([
  18165. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  18166. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  18167. '$mod.vP(1);',
  18168. '$mod.vP(1);',
  18169. '$mod.vP(2);',
  18170. '']));
  18171. end;
  18172. procedure TTestModule.TestProcType_PropertyFPC;
  18173. begin
  18174. StartProgram(false);
  18175. Add('type');
  18176. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  18177. Add(' TObject = class');
  18178. Add(' FOnFoo: TFuncInt;');
  18179. Add(' function DoIt(vA: longint = 1): longint;');
  18180. Add(' function GetFoo: TFuncInt;');
  18181. Add(' procedure SetFoo(const Value: TFuncInt);');
  18182. Add(' function GetEvents(Index: longint): TFuncInt;');
  18183. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  18184. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  18185. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  18186. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  18187. Add(' end;');
  18188. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  18189. Add('function tobject.getfoo: tfuncint; begin end;');
  18190. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  18191. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  18192. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  18193. Add('var');
  18194. Add(' Obj: TObject;');
  18195. Add(' vP: tfuncint;');
  18196. Add(' b: boolean;');
  18197. Add('begin');
  18198. Add(' obj.onfoo:=nil;');
  18199. Add(' obj.onbar:=nil;');
  18200. Add(' obj.events[1]:=nil;');
  18201. Add(' obj.onfoo:=obj.onfoo;');
  18202. Add(' obj.onbar:=obj.onbar;');
  18203. Add(' obj.events[2]:=obj.events[3];');
  18204. Add(' obj.onfoo:[email protected];');
  18205. Add(' obj.onbar:[email protected];');
  18206. Add(' obj.events[4]:[email protected];');
  18207. //Add(' obj.onfoo:=obj.doit;'); // delphi
  18208. //Add(' obj.onbar:=obj.doit;'); // delphi
  18209. //Add(' obj.events[4]:=obj.doit;'); // delphi
  18210. Add(' obj.onfoo;');
  18211. Add(' obj.onbar;');
  18212. //Add(' obj.events[5];'); ToDo in pasresolver
  18213. Add(' obj.onfoo();');
  18214. Add(' obj.onbar();');
  18215. Add(' obj.events[6]();');
  18216. Add(' b:=obj.onfoo=nil;');
  18217. Add(' b:=obj.onbar=nil;');
  18218. Add(' b:=obj.events[7]=nil;');
  18219. Add(' b:=obj.onfoo<>nil;');
  18220. Add(' b:=obj.onbar<>nil;');
  18221. Add(' b:=obj.events[8]<>nil;');
  18222. Add(' b:=obj.onfoo=vp;');
  18223. Add(' b:=obj.onbar=vp;');
  18224. Add(' b:=obj.events[9]=vp;');
  18225. Add(' b:=obj.onfoo=obj.onfoo;');
  18226. Add(' b:=obj.onbar=obj.onfoo;');
  18227. Add(' b:=obj.events[10]=obj.onfoo;');
  18228. Add(' b:=obj.onfoo<>obj.onfoo;');
  18229. Add(' b:=obj.onbar<>obj.onfoo;');
  18230. Add(' b:=obj.events[11]<>obj.onfoo;');
  18231. Add(' b:[email protected];');
  18232. Add(' b:[email protected];');
  18233. Add(' b:=obj.events[12][email protected];');
  18234. Add(' b:=obj.onfoo<>@obj.doit;');
  18235. Add(' b:=obj.onbar<>@obj.doit;');
  18236. Add(' b:=obj.events[12]<>@obj.doit;');
  18237. Add(' b:=Assigned(obj.onfoo);');
  18238. Add(' b:=Assigned(obj.onbar);');
  18239. Add(' b:=Assigned(obj.events[13]);');
  18240. ConvertProgram;
  18241. CheckSource('TestProcType_PropertyFPC',
  18242. LinesToStr([ // statements
  18243. 'rtl.createClass($mod, "TObject", null, function () {',
  18244. ' this.$init = function () {',
  18245. ' this.FOnFoo = null;',
  18246. ' };',
  18247. ' this.$final = function () {',
  18248. ' this.FOnFoo = undefined;',
  18249. ' };',
  18250. ' this.DoIt = function (vA) {',
  18251. ' var Result = 0;',
  18252. ' return Result;',
  18253. ' };',
  18254. 'this.GetFoo = function () {',
  18255. ' var Result = null;',
  18256. ' return Result;',
  18257. '};',
  18258. 'this.SetFoo = function (Value) {',
  18259. '};',
  18260. 'this.GetEvents = function (Index) {',
  18261. ' var Result = null;',
  18262. ' return Result;',
  18263. '};',
  18264. 'this.SetEvents = function (Index, Value) {',
  18265. '};',
  18266. '});',
  18267. 'this.Obj = null;',
  18268. 'this.vP = null;',
  18269. 'this.b = false;'
  18270. ]),
  18271. LinesToStr([
  18272. '$mod.Obj.FOnFoo = null;',
  18273. '$mod.Obj.SetFoo(null);',
  18274. '$mod.Obj.SetEvents(1, null);',
  18275. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  18276. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  18277. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  18278. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  18279. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  18280. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  18281. '$mod.Obj.FOnFoo(1);',
  18282. '$mod.Obj.GetFoo();',
  18283. '$mod.Obj.FOnFoo(1);',
  18284. '$mod.Obj.GetFoo()(1);',
  18285. '$mod.Obj.GetEvents(6)(1);',
  18286. '$mod.b = $mod.Obj.FOnFoo === null;',
  18287. '$mod.b = $mod.Obj.GetFoo() === null;',
  18288. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  18289. '$mod.b = $mod.Obj.FOnFoo !== null;',
  18290. '$mod.b = $mod.Obj.GetFoo() !== null;',
  18291. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  18292. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  18293. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  18294. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  18295. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  18296. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  18297. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  18298. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  18299. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  18300. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  18301. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  18302. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  18303. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  18304. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  18305. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  18306. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  18307. '$mod.b = $mod.Obj.FOnFoo != null;',
  18308. '$mod.b = $mod.Obj.GetFoo() != null;',
  18309. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  18310. '']));
  18311. end;
  18312. procedure TTestModule.TestProcType_PropertyDelphi;
  18313. begin
  18314. StartProgram(false);
  18315. Add('{$mode delphi}');
  18316. Add('type');
  18317. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  18318. Add(' TObject = class');
  18319. Add(' FOnFoo: TFuncInt;');
  18320. Add(' function DoIt(vA: longint = 1): longint;');
  18321. Add(' function GetFoo: TFuncInt;');
  18322. Add(' procedure SetFoo(const Value: TFuncInt);');
  18323. Add(' function GetEvents(Index: longint): TFuncInt;');
  18324. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  18325. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  18326. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  18327. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  18328. Add(' end;');
  18329. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  18330. Add('function tobject.getfoo: tfuncint; begin end;');
  18331. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  18332. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  18333. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  18334. Add('var');
  18335. Add(' Obj: TObject;');
  18336. Add(' vP: tfuncint;');
  18337. Add(' b: boolean;');
  18338. Add('begin');
  18339. Add(' obj.onfoo:=nil;');
  18340. Add(' obj.onbar:=nil;');
  18341. Add(' obj.events[1]:=nil;');
  18342. Add(' obj.onfoo:=obj.onfoo;');
  18343. Add(' obj.onbar:=obj.onbar;');
  18344. Add(' obj.events[2]:=obj.events[3];');
  18345. Add(' obj.onfoo:[email protected];');
  18346. Add(' obj.onbar:[email protected];');
  18347. Add(' obj.events[4]:[email protected];');
  18348. Add(' obj.onfoo:=obj.doit;'); // delphi
  18349. Add(' obj.onbar:=obj.doit;'); // delphi
  18350. Add(' obj.events[4]:=obj.doit;'); // delphi
  18351. Add(' obj.onfoo;');
  18352. Add(' obj.onbar;');
  18353. //Add(' obj.events[5];'); ToDo in pasresolver
  18354. Add(' obj.onfoo();');
  18355. Add(' obj.onbar();');
  18356. Add(' obj.events[6]();');
  18357. //Add(' b:=obj.onfoo=nil;'); // fpc
  18358. //Add(' b:=obj.onbar=nil;'); // fpc
  18359. //Add(' b:=obj.events[7]=nil;'); // fpc
  18360. //Add(' b:=obj.onfoo<>nil;'); // fpc
  18361. //Add(' b:=obj.onbar<>nil;'); // fpc
  18362. //Add(' b:=obj.events[8]<>nil;'); // fpc
  18363. Add(' b:=obj.onfoo=vp;');
  18364. Add(' b:=obj.onbar=vp;');
  18365. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  18366. Add(' b:=obj.onfoo=obj.onfoo;');
  18367. Add(' b:=obj.onbar=obj.onfoo;');
  18368. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  18369. Add(' b:=obj.onfoo<>obj.onfoo;');
  18370. Add(' b:=obj.onbar<>obj.onfoo;');
  18371. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  18372. //Add(' b:[email protected];'); // fpc
  18373. //Add(' b:[email protected];'); // fpc
  18374. //Add(' b:=obj.events[12][email protected];'); // fpc
  18375. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  18376. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  18377. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  18378. Add(' b:=Assigned(obj.onfoo);');
  18379. Add(' b:=Assigned(obj.onbar);');
  18380. Add(' b:=Assigned(obj.events[13]);');
  18381. ConvertProgram;
  18382. CheckSource('TestProcType_PropertyDelphi',
  18383. LinesToStr([ // statements
  18384. 'rtl.createClass($mod, "TObject", null, function () {',
  18385. ' this.$init = function () {',
  18386. ' this.FOnFoo = null;',
  18387. ' };',
  18388. ' this.$final = function () {',
  18389. ' this.FOnFoo = undefined;',
  18390. ' };',
  18391. ' this.DoIt = function (vA) {',
  18392. ' var Result = 0;',
  18393. ' return Result;',
  18394. ' };',
  18395. 'this.GetFoo = function () {',
  18396. ' var Result = null;',
  18397. ' return Result;',
  18398. '};',
  18399. 'this.SetFoo = function (Value) {',
  18400. '};',
  18401. 'this.GetEvents = function (Index) {',
  18402. ' var Result = null;',
  18403. ' return Result;',
  18404. '};',
  18405. 'this.SetEvents = function (Index, Value) {',
  18406. '};',
  18407. '});',
  18408. 'this.Obj = null;',
  18409. 'this.vP = null;',
  18410. 'this.b = false;'
  18411. ]),
  18412. LinesToStr([
  18413. '$mod.Obj.FOnFoo = null;',
  18414. '$mod.Obj.SetFoo(null);',
  18415. '$mod.Obj.SetEvents(1, null);',
  18416. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  18417. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  18418. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  18419. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  18420. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  18421. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  18422. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  18423. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  18424. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  18425. '$mod.Obj.FOnFoo(1);',
  18426. '$mod.Obj.GetFoo();',
  18427. '$mod.Obj.FOnFoo(1);',
  18428. '$mod.Obj.GetFoo()(1);',
  18429. '$mod.Obj.GetEvents(6)(1);',
  18430. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  18431. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  18432. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  18433. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  18434. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  18435. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  18436. '$mod.b = $mod.Obj.FOnFoo != null;',
  18437. '$mod.b = $mod.Obj.GetFoo() != null;',
  18438. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  18439. '']));
  18440. end;
  18441. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  18442. begin
  18443. StartProgram(false);
  18444. Add('type');
  18445. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  18446. Add(' TObject = class');
  18447. Add(' FOnFoo: TFuncInt;');
  18448. Add(' function DoIt(vA: longint = 1): longint;');
  18449. Add(' function GetFoo: TFuncInt;');
  18450. Add(' procedure SetFoo(const Value: TFuncInt);');
  18451. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  18452. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  18453. Add(' end;');
  18454. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  18455. Add('function tobject.getfoo: tfuncint; begin end;');
  18456. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  18457. Add('var');
  18458. Add(' Obj: TObject;');
  18459. Add(' vP: tfuncint;');
  18460. Add(' b: boolean;');
  18461. Add('begin');
  18462. Add('with obj do begin');
  18463. Add(' fonfoo:=nil;');
  18464. Add(' onfoo:=nil;');
  18465. Add(' onbar:=nil;');
  18466. Add(' fonfoo:=fonfoo;');
  18467. Add(' onfoo:=onfoo;');
  18468. Add(' onbar:=onbar;');
  18469. Add(' fonfoo:=@doit;');
  18470. Add(' onfoo:=@doit;');
  18471. Add(' onbar:=@doit;');
  18472. //Add(' fonfoo:=doit;'); // delphi
  18473. //Add(' onfoo:=doit;'); // delphi
  18474. //Add(' onbar:=doit;'); // delphi
  18475. Add(' fonfoo;');
  18476. Add(' onfoo;');
  18477. Add(' onbar;');
  18478. Add(' fonfoo();');
  18479. Add(' onfoo();');
  18480. Add(' onbar();');
  18481. Add(' b:=fonfoo=nil;');
  18482. Add(' b:=onfoo=nil;');
  18483. Add(' b:=onbar=nil;');
  18484. Add(' b:=fonfoo<>nil;');
  18485. Add(' b:=onfoo<>nil;');
  18486. Add(' b:=onbar<>nil;');
  18487. Add(' b:=fonfoo=vp;');
  18488. Add(' b:=onfoo=vp;');
  18489. Add(' b:=onbar=vp;');
  18490. Add(' b:=fonfoo=fonfoo;');
  18491. Add(' b:=onfoo=onfoo;');
  18492. Add(' b:=onbar=onfoo;');
  18493. Add(' b:=fonfoo<>fonfoo;');
  18494. Add(' b:=onfoo<>onfoo;');
  18495. Add(' b:=onbar<>onfoo;');
  18496. Add(' b:=fonfoo=@doit;');
  18497. Add(' b:=onfoo=@doit;');
  18498. Add(' b:=onbar=@doit;');
  18499. Add(' b:=fonfoo<>@doit;');
  18500. Add(' b:=onfoo<>@doit;');
  18501. Add(' b:=onbar<>@doit;');
  18502. Add(' b:=Assigned(fonfoo);');
  18503. Add(' b:=Assigned(onfoo);');
  18504. Add(' b:=Assigned(onbar);');
  18505. Add('end;');
  18506. ConvertProgram;
  18507. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  18508. LinesToStr([ // statements
  18509. 'rtl.createClass($mod, "TObject", null, function () {',
  18510. ' this.$init = function () {',
  18511. ' this.FOnFoo = null;',
  18512. ' };',
  18513. ' this.$final = function () {',
  18514. ' this.FOnFoo = undefined;',
  18515. ' };',
  18516. ' this.DoIt = function (vA) {',
  18517. ' var Result = 0;',
  18518. ' return Result;',
  18519. ' };',
  18520. ' this.GetFoo = function () {',
  18521. ' var Result = null;',
  18522. ' return Result;',
  18523. ' };',
  18524. ' this.SetFoo = function (Value) {',
  18525. ' };',
  18526. '});',
  18527. 'this.Obj = null;',
  18528. 'this.vP = null;',
  18529. 'this.b = false;'
  18530. ]),
  18531. LinesToStr([
  18532. 'var $with1 = $mod.Obj;',
  18533. '$with1.FOnFoo = null;',
  18534. '$with1.FOnFoo = null;',
  18535. '$with1.SetFoo(null);',
  18536. '$with1.FOnFoo = $with1.FOnFoo;',
  18537. '$with1.FOnFoo = $with1.FOnFoo;',
  18538. '$with1.SetFoo($with1.GetFoo());',
  18539. '$with1.FOnFoo = rtl.createCallback($with1, "DoIt");',
  18540. '$with1.FOnFoo = rtl.createCallback($with1, "DoIt");',
  18541. '$with1.SetFoo(rtl.createCallback($with1, "DoIt"));',
  18542. '$with1.FOnFoo(1);',
  18543. '$with1.FOnFoo(1);',
  18544. '$with1.GetFoo();',
  18545. '$with1.FOnFoo(1);',
  18546. '$with1.FOnFoo(1);',
  18547. '$with1.GetFoo()(1);',
  18548. '$mod.b = $with1.FOnFoo === null;',
  18549. '$mod.b = $with1.FOnFoo === null;',
  18550. '$mod.b = $with1.GetFoo() === null;',
  18551. '$mod.b = $with1.FOnFoo !== null;',
  18552. '$mod.b = $with1.FOnFoo !== null;',
  18553. '$mod.b = $with1.GetFoo() !== null;',
  18554. '$mod.b = rtl.eqCallback($with1.FOnFoo, $mod.vP);',
  18555. '$mod.b = rtl.eqCallback($with1.FOnFoo, $mod.vP);',
  18556. '$mod.b = rtl.eqCallback($with1.GetFoo(), $mod.vP);',
  18557. '$mod.b = rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  18558. '$mod.b = rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  18559. '$mod.b = rtl.eqCallback($with1.GetFoo(), $with1.FOnFoo);',
  18560. '$mod.b = !rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  18561. '$mod.b = !rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  18562. '$mod.b = !rtl.eqCallback($with1.GetFoo(), $with1.FOnFoo);',
  18563. '$mod.b = rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  18564. '$mod.b = rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  18565. '$mod.b = rtl.eqCallback($with1.GetFoo(), rtl.createCallback($with1, "DoIt"));',
  18566. '$mod.b = !rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  18567. '$mod.b = !rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  18568. '$mod.b = !rtl.eqCallback($with1.GetFoo(), rtl.createCallback($with1, "DoIt"));',
  18569. '$mod.b = $with1.FOnFoo != null;',
  18570. '$mod.b = $with1.FOnFoo != null;',
  18571. '$mod.b = $with1.GetFoo() != null;',
  18572. '']));
  18573. end;
  18574. procedure TTestModule.TestProcType_Nested;
  18575. begin
  18576. StartProgram(false);
  18577. Add([
  18578. 'type',
  18579. ' TProcInt = procedure(vI: longint = 1);',
  18580. 'procedure DoIt(vJ: longint);',
  18581. 'var aProc: TProcInt;',
  18582. ' b: boolean;',
  18583. ' procedure Sub(vK: longint);',
  18584. ' var aSub: TProcInt;',
  18585. ' procedure SubSub(vK: longint);',
  18586. ' var aSubSub: TProcInt;',
  18587. ' begin;',
  18588. ' aProc:=@DoIt;',
  18589. ' aSub:=@DoIt;',
  18590. ' aSubSub:=@DoIt;',
  18591. ' aProc:=@Sub;',
  18592. ' aSub:=@Sub;',
  18593. ' aSubSub:=@Sub;',
  18594. ' aProc:=@SubSub;',
  18595. ' aSub:=@SubSub;',
  18596. ' aSubSub:=@SubSub;',
  18597. ' end;',
  18598. ' begin;',
  18599. ' end;',
  18600. 'begin;',
  18601. ' aProc:=@Sub;',
  18602. ' b:=aProc=@Sub;',
  18603. ' b:=@Sub=aProc;',
  18604. 'end;',
  18605. 'begin',
  18606. '']);
  18607. ConvertProgram;
  18608. CheckSource('TestProcType_Nested',
  18609. LinesToStr([ // statements
  18610. 'this.DoIt = function (vJ) {',
  18611. ' var aProc = null;',
  18612. ' var b = false;',
  18613. ' function Sub(vK) {',
  18614. ' var aSub = null;',
  18615. ' function SubSub(vK) {',
  18616. ' var aSubSub = null;',
  18617. ' aProc = $mod.DoIt;',
  18618. ' aSub = $mod.DoIt;',
  18619. ' aSubSub = $mod.DoIt;',
  18620. ' aProc = Sub;',
  18621. ' aSub = Sub;',
  18622. ' aSubSub = Sub;',
  18623. ' aProc = SubSub;',
  18624. ' aSub = SubSub;',
  18625. ' aSubSub = SubSub;',
  18626. ' };',
  18627. ' };',
  18628. ' aProc = Sub;',
  18629. ' b = rtl.eqCallback(aProc, Sub);',
  18630. ' b = rtl.eqCallback(Sub, aProc);',
  18631. '};',
  18632. '']),
  18633. LinesToStr([ // $mod.$main
  18634. '']));
  18635. end;
  18636. procedure TTestModule.TestProcType_NestedOfObject;
  18637. begin
  18638. StartProgram(false);
  18639. Add([
  18640. 'type',
  18641. ' TProcInt = procedure(vI: longint = 1) of object;',
  18642. ' TObject = class',
  18643. ' procedure DoIt(vJ: longint);',
  18644. ' end;',
  18645. 'procedure TObject.DoIt(vJ: longint);',
  18646. 'var aProc: TProcInt;',
  18647. ' b: boolean;',
  18648. ' procedure Sub(vK: longint);',
  18649. ' var aSub: TProcInt;',
  18650. ' procedure SubSub(vK: longint);',
  18651. ' var aSubSub: TProcInt;',
  18652. ' begin;',
  18653. ' aProc:=@DoIt;',
  18654. ' aSub:=@DoIt;',
  18655. ' aSubSub:=@DoIt;',
  18656. ' aProc:=@Sub;',
  18657. ' aSub:=@Sub;',
  18658. ' aSubSub:=@Sub;',
  18659. ' aProc:=@SubSub;',
  18660. ' aSub:=@SubSub;',
  18661. ' aSubSub:=@SubSub;',
  18662. ' end;',
  18663. ' begin;',
  18664. ' end;',
  18665. 'begin;',
  18666. ' aProc:=@Sub;',
  18667. ' b:=aProc=@Sub;',
  18668. ' b:=@Sub=aProc;',
  18669. 'end;',
  18670. 'begin',
  18671. '']);
  18672. ConvertProgram;
  18673. CheckSource('TestProcType_Nested',
  18674. LinesToStr([ // statements
  18675. 'rtl.createClass($mod, "TObject", null, function () {',
  18676. ' this.$init = function () {',
  18677. ' };',
  18678. ' this.$final = function () {',
  18679. ' };',
  18680. ' this.DoIt = function (vJ) {',
  18681. ' var Self = this;',
  18682. ' var aProc = null;',
  18683. ' var b = false;',
  18684. ' function Sub(vK) {',
  18685. ' var aSub = null;',
  18686. ' function SubSub(vK) {',
  18687. ' var aSubSub = null;',
  18688. ' aProc = rtl.createCallback(Self, "DoIt");',
  18689. ' aSub = rtl.createCallback(Self, "DoIt");',
  18690. ' aSubSub = rtl.createCallback(Self, "DoIt");',
  18691. ' aProc = Sub;',
  18692. ' aSub = Sub;',
  18693. ' aSubSub = Sub;',
  18694. ' aProc = SubSub;',
  18695. ' aSub = SubSub;',
  18696. ' aSubSub = SubSub;',
  18697. ' };',
  18698. ' };',
  18699. ' aProc = Sub;',
  18700. ' b = rtl.eqCallback(aProc, Sub);',
  18701. ' b = rtl.eqCallback(Sub, aProc);',
  18702. ' };',
  18703. '});',
  18704. '']),
  18705. LinesToStr([ // $mod.$main
  18706. '']));
  18707. end;
  18708. procedure TTestModule.TestProcType_ReferenceToProc;
  18709. begin
  18710. StartProgram(false);
  18711. Add([
  18712. 'type',
  18713. ' TProcRef = reference to procedure(i: longint = 0);',
  18714. ' TFuncRef = reference to function(i: longint = 0): longint;',
  18715. 'var',
  18716. ' p: TProcRef;',
  18717. ' f: TFuncRef;',
  18718. 'procedure DoIt(i: longint);',
  18719. 'begin',
  18720. 'end;',
  18721. 'function GetIt(i: longint): longint;',
  18722. 'begin',
  18723. ' p:=@DoIt;',
  18724. ' f:=@GetIt;',
  18725. ' f;',
  18726. ' f();',
  18727. ' f(1);',
  18728. 'end;',
  18729. 'begin',
  18730. ' p:=@DoIt;',
  18731. ' f:=@GetIt;',
  18732. ' f;',
  18733. ' f();',
  18734. ' f(1);',
  18735. ' p:=TProcRef(f);',
  18736. '']);
  18737. ConvertProgram;
  18738. CheckSource('TestProcType_ReferenceToProc',
  18739. LinesToStr([ // statements
  18740. 'this.p = null;',
  18741. 'this.f = null;',
  18742. 'this.DoIt = function (i) {',
  18743. '};',
  18744. 'this.GetIt = function (i) {',
  18745. ' var Result = 0;',
  18746. ' $mod.p = $mod.DoIt;',
  18747. ' $mod.f = $mod.GetIt;',
  18748. ' $mod.f(0);',
  18749. ' $mod.f(0);',
  18750. ' $mod.f(1);',
  18751. ' return Result;',
  18752. '};',
  18753. '']),
  18754. LinesToStr([ // $mod.$main
  18755. '$mod.p = $mod.DoIt;',
  18756. '$mod.f = $mod.GetIt;',
  18757. '$mod.f(0);',
  18758. '$mod.f(0);',
  18759. '$mod.f(1);',
  18760. '$mod.p = $mod.f;',
  18761. '']));
  18762. end;
  18763. procedure TTestModule.TestProcType_ReferenceToMethod;
  18764. begin
  18765. StartProgram(false);
  18766. Add([
  18767. 'type',
  18768. ' TFuncRef = reference to function(i: longint = 5): longint;',
  18769. ' TObject = class',
  18770. ' function Grow(s: longint): longint;',
  18771. ' end;',
  18772. 'var',
  18773. ' f: tfuncref;',
  18774. 'function tobject.grow(s: longint): longint;',
  18775. ' function GrowSub(i: longint): longint;',
  18776. ' begin',
  18777. ' f:=@grow;',
  18778. ' f:=@growsub;',
  18779. ' end;',
  18780. 'begin',
  18781. ' f:=@grow;',
  18782. ' f:=@growsub;',
  18783. 'end;',
  18784. 'begin',
  18785. '']);
  18786. ConvertProgram;
  18787. CheckSource('TestProcType_ReferenceToMethod',
  18788. LinesToStr([ // statements
  18789. 'rtl.createClass($mod, "TObject", null, function () {',
  18790. ' this.$init = function () {',
  18791. ' };',
  18792. ' this.$final = function () {',
  18793. ' };',
  18794. ' this.Grow = function (s) {',
  18795. ' var Self = this;',
  18796. ' var Result = 0;',
  18797. ' function GrowSub(i) {',
  18798. ' var Result = 0;',
  18799. ' $mod.f = rtl.createCallback(Self, "Grow");',
  18800. ' $mod.f = GrowSub;',
  18801. ' return Result;',
  18802. ' };',
  18803. ' $mod.f = rtl.createCallback(Self, "Grow");',
  18804. ' $mod.f = GrowSub;',
  18805. ' return Result;',
  18806. ' };',
  18807. '});',
  18808. 'this.f = null;',
  18809. '']),
  18810. LinesToStr([ // $mod.$main
  18811. '']));
  18812. end;
  18813. procedure TTestModule.TestProcType_Typecast;
  18814. begin
  18815. StartProgram(false);
  18816. Add([
  18817. 'type',
  18818. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  18819. ' TEvent = procedure of object;',
  18820. ' TGetter = function:longint of object;',
  18821. ' TProcA = procedure(i: longint);',
  18822. ' TFuncB = function(i, j: longint): longint;',
  18823. 'procedure DoIt(); varargs; begin end;',
  18824. 'var',
  18825. ' Notify: tnotifyevent;',
  18826. ' Event: tevent;',
  18827. ' Getter: tgetter;',
  18828. ' ProcA: tproca;',
  18829. ' FuncB: tfuncb;',
  18830. ' p: pointer;',
  18831. 'begin',
  18832. ' notify:=tnotifyevent(event);',
  18833. ' event:=tevent(event);',
  18834. ' event:=tevent(notify);',
  18835. ' event:=tevent(getter);',
  18836. ' event:=tevent(proca);',
  18837. ' proca:=tproca(funcb);',
  18838. ' funcb:=tfuncb(funcb);',
  18839. ' funcb:=tfuncb(proca);',
  18840. ' funcb:=tfuncb(getter);',
  18841. ' proca:=tproca(p);',
  18842. ' funcb:=tfuncb(p);',
  18843. ' getter:=tgetter(p);',
  18844. ' p:=pointer(notify);',
  18845. ' p:=notify;',
  18846. ' p:=pointer(proca);',
  18847. ' p:=proca;',
  18848. ' p:=pointer(funcb);',
  18849. ' p:=funcb;',
  18850. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  18851. '']);
  18852. ConvertProgram;
  18853. CheckSource('TestProcType_Typecast',
  18854. LinesToStr([ // statements
  18855. 'this.DoIt = function () {',
  18856. '};',
  18857. 'this.Notify = null;',
  18858. 'this.Event = null;',
  18859. 'this.Getter = null;',
  18860. 'this.ProcA = null;',
  18861. 'this.FuncB = null;',
  18862. 'this.p = null;',
  18863. '']),
  18864. LinesToStr([ // $mod.$main
  18865. '$mod.Notify = $mod.Event;',
  18866. '$mod.Event = $mod.Event;',
  18867. '$mod.Event = $mod.Notify;',
  18868. '$mod.Event = $mod.Getter;',
  18869. '$mod.Event = $mod.ProcA;',
  18870. '$mod.ProcA = $mod.FuncB;',
  18871. '$mod.FuncB = $mod.FuncB;',
  18872. '$mod.FuncB = $mod.ProcA;',
  18873. '$mod.FuncB = $mod.Getter;',
  18874. '$mod.ProcA = $mod.p;',
  18875. '$mod.FuncB = $mod.p;',
  18876. '$mod.Getter = $mod.p;',
  18877. '$mod.p = $mod.Notify;',
  18878. '$mod.p = $mod.Notify;',
  18879. '$mod.p = $mod.ProcA;',
  18880. '$mod.p = $mod.ProcA;',
  18881. '$mod.p = $mod.FuncB;',
  18882. '$mod.p = $mod.FuncB;',
  18883. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  18884. '']));
  18885. end;
  18886. procedure TTestModule.TestProcType_PassProcToUntyped;
  18887. begin
  18888. StartProgram(false);
  18889. Add([
  18890. 'type',
  18891. ' TEvent = procedure of object;',
  18892. ' TFunc = function: longint;',
  18893. 'procedure DoIt(); varargs; begin end;',
  18894. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  18895. 'var',
  18896. ' Event: tevent;',
  18897. ' Func: TFunc;',
  18898. 'begin',
  18899. ' doit(event,func);',
  18900. ' dosome(event,event,event);',
  18901. ' dosome(func,func,func);',
  18902. '']);
  18903. ConvertProgram;
  18904. CheckSource('TestProcType_PassProcToUntyped',
  18905. LinesToStr([ // statements
  18906. 'this.DoIt = function () {',
  18907. '};',
  18908. 'this.DoSome = function (a, b, p) {',
  18909. '};',
  18910. 'this.Event = null;',
  18911. 'this.Func = null;',
  18912. '']),
  18913. LinesToStr([ // $mod.$main
  18914. '$mod.DoIt($mod.Event, $mod.Func);',
  18915. '$mod.DoSome($mod.Event, {',
  18916. ' p: $mod,',
  18917. ' get: function () {',
  18918. ' return this.p.Event;',
  18919. ' },',
  18920. ' set: function (v) {',
  18921. ' this.p.Event = v;',
  18922. ' }',
  18923. '}, $mod.Event);',
  18924. '$mod.DoSome($mod.Func, {',
  18925. ' p: $mod,',
  18926. ' get: function () {',
  18927. ' return this.p.Func;',
  18928. ' },',
  18929. ' set: function (v) {',
  18930. ' this.p.Func = v;',
  18931. ' }',
  18932. '}, $mod.Func);',
  18933. '']));
  18934. end;
  18935. procedure TTestModule.TestProcType_PassProcToArray;
  18936. begin
  18937. StartProgram(false);
  18938. Add([
  18939. 'type',
  18940. ' TFunc = function: longint;',
  18941. ' TArrFunc = array of TFunc;',
  18942. 'procedure DoIt(Arr: TArrFunc); begin end;',
  18943. 'function GetIt: longint; begin end;',
  18944. 'var',
  18945. ' Func: tfunc;',
  18946. 'begin',
  18947. ' doit([]);',
  18948. ' doit([@GetIt]);',
  18949. ' doit([Func]);',
  18950. '']);
  18951. ConvertProgram;
  18952. CheckSource('TestProcType_PassProcToArray',
  18953. LinesToStr([ // statements
  18954. 'this.DoIt = function (Arr) {',
  18955. '};',
  18956. 'this.GetIt = function () {',
  18957. ' var Result = 0;',
  18958. ' return Result;',
  18959. '};',
  18960. 'this.Func = null;',
  18961. '']),
  18962. LinesToStr([ // $mod.$main
  18963. '$mod.DoIt([]);',
  18964. '$mod.DoIt([$mod.GetIt]);',
  18965. '$mod.DoIt([$mod.Func]);',
  18966. '']));
  18967. end;
  18968. procedure TTestModule.TestPointer;
  18969. begin
  18970. StartProgram(false);
  18971. Add(['type',
  18972. ' TObject = class end;',
  18973. ' TClass = class of TObject;',
  18974. ' TArrInt = array of longint;',
  18975. 'const',
  18976. ' n = nil;',
  18977. 'var',
  18978. ' v: jsvalue;',
  18979. ' Obj: tobject;',
  18980. ' C: tclass;',
  18981. ' a: tarrint;',
  18982. ' p: Pointer = nil;',
  18983. ' s: string;',
  18984. 'begin',
  18985. ' p:=p;',
  18986. ' p:=nil;',
  18987. ' if p=nil then;',
  18988. ' if nil=p then;',
  18989. ' if Assigned(p) then;',
  18990. ' p:=Pointer(v);',
  18991. ' p:=obj;',
  18992. ' p:=c;',
  18993. ' p:=a;',
  18994. ' p:=tobject;',
  18995. ' obj:=TObject(p);',
  18996. ' c:=TClass(p);',
  18997. ' a:=TArrInt(p);',
  18998. ' p:=n;',
  18999. ' p:=Pointer(a);',
  19000. ' p:=pointer(s);',
  19001. ' s:=string(p);',
  19002. '']);
  19003. ConvertProgram;
  19004. CheckSource('TestPointer',
  19005. LinesToStr([ // statements
  19006. 'rtl.createClass($mod, "TObject", null, function () {',
  19007. ' this.$init = function () {',
  19008. ' };',
  19009. ' this.$final = function () {',
  19010. ' };',
  19011. '});',
  19012. 'this.n = null;',
  19013. 'this.v = undefined;',
  19014. 'this.Obj = null;',
  19015. 'this.C = null;',
  19016. 'this.a = [];',
  19017. 'this.p = null;',
  19018. 'this.s = "";',
  19019. '']),
  19020. LinesToStr([ // $mod.$main
  19021. '$mod.p = $mod.p;',
  19022. '$mod.p = null;',
  19023. 'if ($mod.p === null) ;',
  19024. 'if (null === $mod.p) ;',
  19025. 'if ($mod.p != null) ;',
  19026. '$mod.p = $mod.v;',
  19027. '$mod.p = $mod.Obj;',
  19028. '$mod.p = $mod.C;',
  19029. '$mod.p = $mod.a;',
  19030. '$mod.p = $mod.TObject;',
  19031. '$mod.Obj = $mod.p;',
  19032. '$mod.C = $mod.p;',
  19033. '$mod.a = $mod.p;',
  19034. '$mod.p = null;',
  19035. '$mod.p = $mod.a;',
  19036. '$mod.p = $mod.s;',
  19037. '$mod.s = $mod.p;',
  19038. '']));
  19039. end;
  19040. procedure TTestModule.TestPointer_Proc;
  19041. begin
  19042. StartProgram(false);
  19043. Add('type');
  19044. Add(' TObject = class');
  19045. Add(' procedure DoIt; virtual; abstract;');
  19046. Add(' end;');
  19047. Add('procedure DoSome; begin end;');
  19048. Add('var');
  19049. Add(' o: TObject;');
  19050. Add(' p: Pointer;');
  19051. Add('begin');
  19052. Add(' p:=@DoSome;');
  19053. Add(' p:[email protected];');
  19054. ConvertProgram;
  19055. CheckSource('TestPointer_Proc',
  19056. LinesToStr([ // statements
  19057. 'rtl.createClass($mod, "TObject", null, function () {',
  19058. ' this.$init = function () {',
  19059. ' };',
  19060. ' this.$final = function () {',
  19061. ' };',
  19062. '});',
  19063. 'this.DoSome = function () {',
  19064. '};',
  19065. 'this.o = null;',
  19066. 'this.p = null;',
  19067. '']),
  19068. LinesToStr([ // $mod.$main
  19069. '$mod.p = $mod.DoSome;',
  19070. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  19071. '']));
  19072. end;
  19073. procedure TTestModule.TestPointer_AssignRecordFail;
  19074. begin
  19075. StartProgram(false);
  19076. Add('type');
  19077. Add(' TRec = record end;');
  19078. Add('var');
  19079. Add(' p: Pointer;');
  19080. Add(' r: TRec;');
  19081. Add('begin');
  19082. Add(' p:=r;');
  19083. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  19084. nIncompatibleTypesGotExpected);
  19085. ConvertProgram;
  19086. end;
  19087. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  19088. begin
  19089. StartProgram(false);
  19090. Add('type');
  19091. Add(' TArr = array[boolean] of longint;');
  19092. Add('var');
  19093. Add(' p: Pointer;');
  19094. Add(' a: TArr;');
  19095. Add('begin');
  19096. Add(' p:=a;');
  19097. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  19098. nIncompatibleTypesGotExpected);
  19099. ConvertProgram;
  19100. end;
  19101. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  19102. begin
  19103. StartProgram(false);
  19104. Add([
  19105. 'procedure DoIt(args: array of jsvalue); begin end;',
  19106. 'procedure DoAll; varargs; begin end;',
  19107. 'var',
  19108. ' v: jsvalue;',
  19109. 'begin',
  19110. ' DoIt([pointer(v)]);',
  19111. ' DoAll(pointer(v));',
  19112. '']);
  19113. ConvertProgram;
  19114. CheckSource('TestPointer_TypeCastJSValueToPointer',
  19115. LinesToStr([ // statements
  19116. 'this.DoIt = function (args) {',
  19117. '};',
  19118. 'this.DoAll = function () {',
  19119. '};',
  19120. 'this.v = undefined;',
  19121. '']),
  19122. LinesToStr([ // $mod.$main
  19123. '$mod.DoIt([$mod.v]);',
  19124. '$mod.DoAll($mod.v);',
  19125. '']));
  19126. end;
  19127. procedure TTestModule.TestPointer_NonRecordFail;
  19128. begin
  19129. StartProgram(false);
  19130. Add([
  19131. 'type',
  19132. ' p = ^longint;',
  19133. 'begin',
  19134. '']);
  19135. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  19136. ConvertProgram;
  19137. end;
  19138. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  19139. begin
  19140. StartProgram(false);
  19141. Add([
  19142. 'procedure DoIt(p: ^longint); begin end;',
  19143. 'begin',
  19144. '']);
  19145. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  19146. ConvertProgram;
  19147. end;
  19148. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  19149. begin
  19150. StartProgram(false);
  19151. Add([
  19152. 'var p: ^longint;',
  19153. 'begin',
  19154. '']);
  19155. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  19156. ConvertProgram;
  19157. end;
  19158. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  19159. begin
  19160. StartProgram(false);
  19161. Add([
  19162. 'function DoIt: ^longint; begin end;',
  19163. 'begin',
  19164. '']);
  19165. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  19166. ConvertProgram;
  19167. end;
  19168. procedure TTestModule.TestPointer_AddrOperatorFail;
  19169. begin
  19170. StartProgram(false);
  19171. Add([
  19172. 'var i: longint;',
  19173. 'begin',
  19174. ' if @i=nil then ;',
  19175. '']);
  19176. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  19177. ConvertProgram;
  19178. end;
  19179. procedure TTestModule.TestPointer_ArrayParamsFail;
  19180. begin
  19181. StartProgram(false);
  19182. Add([
  19183. 'var',
  19184. ' p: Pointer;',
  19185. 'begin',
  19186. ' p:=p[1];',
  19187. '']);
  19188. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  19189. ConvertProgram;
  19190. end;
  19191. procedure TTestModule.TestPointer_PointerAddFail;
  19192. begin
  19193. StartProgram(false);
  19194. Add([
  19195. 'var',
  19196. ' p: Pointer;',
  19197. 'begin',
  19198. ' p:=p+1;',
  19199. '']);
  19200. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  19201. ConvertProgram;
  19202. end;
  19203. procedure TTestModule.TestPointer_IncPointerFail;
  19204. begin
  19205. StartProgram(false);
  19206. Add([
  19207. 'var',
  19208. ' p: Pointer;',
  19209. 'begin',
  19210. ' inc(p,1);',
  19211. '']);
  19212. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Pointer", expected "integer"',
  19213. nIncompatibleTypeArgNo);
  19214. ConvertProgram;
  19215. end;
  19216. procedure TTestModule.TestPointer_Record;
  19217. begin
  19218. StartProgram(false);
  19219. Add([
  19220. 'type',
  19221. ' TRec = record x: longint; end;',
  19222. ' PRec = ^TRec;',
  19223. 'var',
  19224. ' r: TRec;',
  19225. ' p: PRec;',
  19226. ' q: ^TRec;',
  19227. ' Ptr: pointer;',
  19228. 'begin',
  19229. ' new(p);',
  19230. ' p:=@r;',
  19231. ' r:=p^;',
  19232. ' r.x:=p^.x;',
  19233. ' p^.x:=r.x;',
  19234. ' if p^.x=3 then ;',
  19235. ' if 4=p^.x then ;',
  19236. ' dispose(p);',
  19237. ' new(q);',
  19238. ' dispose(q);',
  19239. ' Ptr:=p;',
  19240. ' p:=PRec(ptr);',
  19241. '']);
  19242. ConvertProgram;
  19243. CheckSource('TestPointer_Record',
  19244. LinesToStr([ // statements
  19245. 'rtl.recNewT($mod, "TRec", function () {',
  19246. ' this.x = 0;',
  19247. ' this.$eq = function (b) {',
  19248. ' return this.x === b.x;',
  19249. ' };',
  19250. ' this.$assign = function (s) {',
  19251. ' this.x = s.x;',
  19252. ' return this;',
  19253. ' };',
  19254. '});',
  19255. 'this.r = $mod.TRec.$new();',
  19256. 'this.p = null;',
  19257. 'this.q = null;',
  19258. 'this.Ptr = null;',
  19259. '']),
  19260. LinesToStr([ // $mod.$main
  19261. '$mod.p = $mod.TRec.$new();',
  19262. '$mod.p = $mod.r;',
  19263. '$mod.r.$assign($mod.p);',
  19264. '$mod.r.x = $mod.p.x;',
  19265. '$mod.p.x = $mod.r.x;',
  19266. 'if ($mod.p.x === 3) ;',
  19267. 'if (4 === $mod.p.x) ;',
  19268. '$mod.p = null;',
  19269. '$mod.q = $mod.TRec.$new();',
  19270. '$mod.q = null;',
  19271. '$mod.Ptr = $mod.p;',
  19272. '$mod.p = $mod.Ptr;',
  19273. '']));
  19274. end;
  19275. procedure TTestModule.TestPointer_RecordArg;
  19276. begin
  19277. StartProgram(false);
  19278. Add([
  19279. '{$modeswitch autoderef}',
  19280. 'type',
  19281. ' TRec = record x: longint; end;',
  19282. ' PRec = ^TRec;',
  19283. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  19284. 'begin',
  19285. ' a.x:=a.x;',
  19286. ' a^.x:=a^.x;',
  19287. ' with a^ do',
  19288. ' x:=x;',
  19289. 'end;',
  19290. 'function GetIt(p: PRec): PRec;',
  19291. 'begin',
  19292. ' p.x:=p.x;',
  19293. ' p^.x:=p^.x;',
  19294. ' with p^ do',
  19295. ' x:=x;',
  19296. 'end;',
  19297. 'var',
  19298. ' r: TRec;',
  19299. ' p: PRec;',
  19300. 'begin',
  19301. ' p:=GetIt(p);',
  19302. ' p^:=GetIt(@r)^;',
  19303. ' DoIt(p,p,p);',
  19304. ' DoIt(@r,p,p);',
  19305. '']);
  19306. ConvertProgram;
  19307. CheckSource('TestPointer_Record',
  19308. LinesToStr([ // statements
  19309. 'rtl.recNewT($mod, "TRec", function () {',
  19310. ' this.x = 0;',
  19311. ' this.$eq = function (b) {',
  19312. ' return this.x === b.x;',
  19313. ' };',
  19314. ' this.$assign = function (s) {',
  19315. ' this.x = s.x;',
  19316. ' return this;',
  19317. ' };',
  19318. '});',
  19319. 'this.DoIt = function (a, b, c) {',
  19320. ' var Result = $mod.TRec.$new();',
  19321. ' a.x = a.x;',
  19322. ' a.x = a.x;',
  19323. ' a.x = a.x;',
  19324. ' return Result;',
  19325. '};',
  19326. 'this.GetIt = function (p) {',
  19327. ' var Result = null;',
  19328. ' p.x = p.x;',
  19329. ' p.x = p.x;',
  19330. ' p.x = p.x;',
  19331. ' return Result;',
  19332. '};',
  19333. 'this.r = $mod.TRec.$new();',
  19334. 'this.p = null;',
  19335. '']),
  19336. LinesToStr([ // $mod.$main
  19337. '$mod.p = $mod.GetIt($mod.p);',
  19338. '$mod.p.$assign($mod.GetIt($mod.r));',
  19339. '$mod.DoIt($mod.p, {',
  19340. ' p: $mod,',
  19341. ' get: function () {',
  19342. ' return this.p.p;',
  19343. ' },',
  19344. ' set: function (v) {',
  19345. ' this.p.p = v;',
  19346. ' }',
  19347. '}, {',
  19348. ' p: $mod,',
  19349. ' get: function () {',
  19350. ' return this.p.p;',
  19351. ' },',
  19352. ' set: function (v) {',
  19353. ' this.p.p = v;',
  19354. ' }',
  19355. '});',
  19356. '$mod.DoIt($mod.r, {',
  19357. ' p: $mod,',
  19358. ' get: function () {',
  19359. ' return this.p.p;',
  19360. ' },',
  19361. ' set: function (v) {',
  19362. ' this.p.p = v;',
  19363. ' }',
  19364. '}, {',
  19365. ' p: $mod,',
  19366. ' get: function () {',
  19367. ' return this.p.p;',
  19368. ' },',
  19369. ' set: function (v) {',
  19370. ' this.p.p = v;',
  19371. ' }',
  19372. '});',
  19373. '']));
  19374. end;
  19375. procedure TTestModule.TestJSValue_AssignToJSValue;
  19376. begin
  19377. StartProgram(false);
  19378. Add('var');
  19379. Add(' v: jsvalue;');
  19380. Add(' i: longint;');
  19381. Add(' s: string;');
  19382. Add(' b: boolean;');
  19383. Add(' d: double;');
  19384. Add(' p: pointer;');
  19385. Add('begin');
  19386. Add(' v:=v;');
  19387. Add(' v:=1;');
  19388. Add(' v:=i;');
  19389. Add(' v:='''';');
  19390. Add(' v:=''c'';');
  19391. Add(' v:=''foo'';');
  19392. Add(' v:=s;');
  19393. Add(' v:=false;');
  19394. Add(' v:=true;');
  19395. Add(' v:=b;');
  19396. Add(' v:=0.1;');
  19397. Add(' v:=d;');
  19398. Add(' v:=nil;');
  19399. Add(' v:=p;');
  19400. ConvertProgram;
  19401. CheckSource('TestJSValue_AssignToJSValue',
  19402. LinesToStr([ // statements
  19403. 'this.v = undefined;',
  19404. 'this.i = 0;',
  19405. 'this.s = "";',
  19406. 'this.b = false;',
  19407. 'this.d = 0.0;',
  19408. 'this.p = null;',
  19409. '']),
  19410. LinesToStr([ // $mod.$main
  19411. '$mod.v = $mod.v;',
  19412. '$mod.v = 1;',
  19413. '$mod.v = $mod.i;',
  19414. '$mod.v = "";',
  19415. '$mod.v = "c";',
  19416. '$mod.v = "foo";',
  19417. '$mod.v = $mod.s;',
  19418. '$mod.v = false;',
  19419. '$mod.v = true;',
  19420. '$mod.v = $mod.b;',
  19421. '$mod.v = 0.1;',
  19422. '$mod.v = $mod.d;',
  19423. '$mod.v = null;',
  19424. '$mod.v = $mod.p;',
  19425. '']));
  19426. end;
  19427. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  19428. begin
  19429. StartProgram(false);
  19430. Add('type');
  19431. Add(' integer = longint;');
  19432. Add(' TYesNo = boolean;');
  19433. Add(' TFloat = double;');
  19434. Add(' TCaption = string;');
  19435. Add(' TChar = char;');
  19436. Add('var');
  19437. Add(' v: jsvalue;');
  19438. Add(' i: integer;');
  19439. Add(' s: TCaption;');
  19440. Add(' b: TYesNo;');
  19441. Add(' d: TFloat;');
  19442. Add(' c: char;');
  19443. Add('begin');
  19444. Add(' i:=longint(v);');
  19445. Add(' i:=integer(v);');
  19446. Add(' s:=string(v);');
  19447. Add(' s:=TCaption(v);');
  19448. Add(' b:=boolean(v);');
  19449. Add(' b:=TYesNo(v);');
  19450. Add(' d:=double(v);');
  19451. Add(' d:=TFloat(v);');
  19452. Add(' c:=char(v);');
  19453. Add(' c:=TChar(v);');
  19454. ConvertProgram;
  19455. CheckSource('TestJSValue_TypeCastToBaseType',
  19456. LinesToStr([ // statements
  19457. 'this.v = undefined;',
  19458. 'this.i = 0;',
  19459. 'this.s = "";',
  19460. 'this.b = false;',
  19461. 'this.d = 0.0;',
  19462. 'this.c = "";',
  19463. '']),
  19464. LinesToStr([ // $mod.$main
  19465. '$mod.i = Math.floor($mod.v);',
  19466. '$mod.i = Math.floor($mod.v);',
  19467. '$mod.s = "" + $mod.v;',
  19468. '$mod.s = "" + $mod.v;',
  19469. '$mod.b = !($mod.v == false);',
  19470. '$mod.b = !($mod.v == false);',
  19471. '$mod.d = rtl.getNumber($mod.v);',
  19472. '$mod.d = rtl.getNumber($mod.v);',
  19473. '$mod.c = rtl.getChar($mod.v);',
  19474. '$mod.c = rtl.getChar($mod.v);',
  19475. '']));
  19476. end;
  19477. procedure TTestModule.TestJSValue_Equal;
  19478. begin
  19479. StartProgram(false);
  19480. Add('type');
  19481. Add(' integer = longint;');
  19482. Add(' TYesNo = boolean;');
  19483. Add(' TFloat = double;');
  19484. Add(' TCaption = string;');
  19485. Add(' TChar = char;');
  19486. Add(' TMulti = JSValue;');
  19487. Add('var');
  19488. Add(' v: jsvalue;');
  19489. Add(' i: integer;');
  19490. Add(' s: TCaption;');
  19491. Add(' b: TYesNo;');
  19492. Add(' d: TFloat;');
  19493. Add(' c: char;');
  19494. Add(' m: TMulti;');
  19495. Add('begin');
  19496. Add(' b:=v=v;');
  19497. Add(' b:=v<>v;');
  19498. Add(' b:=v=1;');
  19499. Add(' b:=v<>1;');
  19500. Add(' b:=2=v;');
  19501. Add(' b:=2<>v;');
  19502. Add(' b:=v=i;');
  19503. Add(' b:=i=v;');
  19504. Add(' b:=v=nil;');
  19505. Add(' b:=nil=v;');
  19506. Add(' b:=v=false;');
  19507. Add(' b:=true=v;');
  19508. Add(' b:=v=b;');
  19509. Add(' b:=b=v;');
  19510. Add(' b:=v=s;');
  19511. Add(' b:=s=v;');
  19512. Add(' b:=v=''foo'';');
  19513. Add(' b:=''''=v;');
  19514. Add(' b:=v=d;');
  19515. Add(' b:=d=v;');
  19516. Add(' b:=v=3.4;');
  19517. Add(' b:=5.6=v;');
  19518. Add(' b:=v=c;');
  19519. Add(' b:=c=v;');
  19520. Add(' b:=m=m;');
  19521. Add(' b:=v=m;');
  19522. Add(' b:=m=v;');
  19523. ConvertProgram;
  19524. CheckSource('TestJSValue_Equal',
  19525. LinesToStr([ // statements
  19526. 'this.v = undefined;',
  19527. 'this.i = 0;',
  19528. 'this.s = "";',
  19529. 'this.b = false;',
  19530. 'this.d = 0.0;',
  19531. 'this.c = "";',
  19532. 'this.m = undefined;',
  19533. '']),
  19534. LinesToStr([ // $mod.$main
  19535. '$mod.b = $mod.v == $mod.v;',
  19536. '$mod.b = $mod.v != $mod.v;',
  19537. '$mod.b = $mod.v == 1;',
  19538. '$mod.b = $mod.v != 1;',
  19539. '$mod.b = 2 == $mod.v;',
  19540. '$mod.b = 2 != $mod.v;',
  19541. '$mod.b = $mod.v == $mod.i;',
  19542. '$mod.b = $mod.i == $mod.v;',
  19543. '$mod.b = $mod.v == null;',
  19544. '$mod.b = null == $mod.v;',
  19545. '$mod.b = $mod.v == false;',
  19546. '$mod.b = true == $mod.v;',
  19547. '$mod.b = $mod.v == $mod.b;',
  19548. '$mod.b = $mod.b == $mod.v;',
  19549. '$mod.b = $mod.v == $mod.s;',
  19550. '$mod.b = $mod.s == $mod.v;',
  19551. '$mod.b = $mod.v == "foo";',
  19552. '$mod.b = "" == $mod.v;',
  19553. '$mod.b = $mod.v == $mod.d;',
  19554. '$mod.b = $mod.d == $mod.v;',
  19555. '$mod.b = $mod.v == 3.4;',
  19556. '$mod.b = 5.6 == $mod.v;',
  19557. '$mod.b = $mod.v == $mod.c;',
  19558. '$mod.b = $mod.c == $mod.v;',
  19559. '$mod.b = $mod.m == $mod.m;',
  19560. '$mod.b = $mod.v == $mod.m;',
  19561. '$mod.b = $mod.m == $mod.v;',
  19562. '']));
  19563. end;
  19564. procedure TTestModule.TestJSValue_If;
  19565. begin
  19566. StartProgram(false);
  19567. Add([
  19568. 'var',
  19569. ' v: jsvalue;',
  19570. 'begin',
  19571. ' if v then ;',
  19572. ' while v do ;',
  19573. ' repeat until v;',
  19574. '']);
  19575. ConvertProgram;
  19576. CheckSource('TestJSValue_If',
  19577. LinesToStr([ // statements
  19578. 'this.v = undefined;',
  19579. '']),
  19580. LinesToStr([ // $mod.$main
  19581. 'if ($mod.v) ;',
  19582. 'while($mod.v){',
  19583. '};',
  19584. 'do{',
  19585. '} while(!$mod.v);',
  19586. '']));
  19587. end;
  19588. procedure TTestModule.TestJSValue_Not;
  19589. begin
  19590. StartProgram(false);
  19591. Add([
  19592. 'var',
  19593. ' v: jsvalue;',
  19594. ' b: boolean;',
  19595. 'begin',
  19596. ' b:=not v;',
  19597. ' if not v then ;',
  19598. ' while not v do ;',
  19599. ' repeat until not v;',
  19600. '']);
  19601. ConvertProgram;
  19602. CheckSource('TestJSValue_If',
  19603. LinesToStr([ // statements
  19604. 'this.v = undefined;',
  19605. 'this.b = false;',
  19606. '']),
  19607. LinesToStr([ // $mod.$main
  19608. '$mod.b=!$mod.v;',
  19609. 'if (!$mod.v) ;',
  19610. 'while(!$mod.v){',
  19611. '};',
  19612. 'do{',
  19613. '} while($mod.v);',
  19614. '']));
  19615. end;
  19616. procedure TTestModule.TestJSValue_Enum;
  19617. begin
  19618. StartProgram(false);
  19619. Add('type');
  19620. Add(' TColor = (red, blue);');
  19621. Add(' TRedBlue = TColor;');
  19622. Add('var');
  19623. Add(' v: jsvalue;');
  19624. Add(' e: TColor;');
  19625. Add('begin');
  19626. Add(' v:=e;');
  19627. Add(' v:=TColor(e);');
  19628. Add(' v:=TRedBlue(e);');
  19629. Add(' e:=TColor(v);');
  19630. Add(' e:=TRedBlue(v);');
  19631. ConvertProgram;
  19632. CheckSource('TestJSValue_Enum',
  19633. LinesToStr([ // statements
  19634. 'this.TColor = {',
  19635. ' "0": "red",',
  19636. ' red: 0,',
  19637. ' "1": "blue",',
  19638. ' blue: 1',
  19639. '};',
  19640. 'this.v = undefined;',
  19641. 'this.e = 0;',
  19642. '']),
  19643. LinesToStr([ // $mod.$main
  19644. '$mod.v = $mod.e;',
  19645. '$mod.v = $mod.e;',
  19646. '$mod.v = $mod.e;',
  19647. '$mod.e = $mod.v;',
  19648. '$mod.e = $mod.v;',
  19649. '']));
  19650. end;
  19651. procedure TTestModule.TestJSValue_ClassInstance;
  19652. begin
  19653. StartProgram(false);
  19654. Add([
  19655. 'type',
  19656. ' TObject = class',
  19657. ' end;',
  19658. ' TBirdObject = TObject;',
  19659. 'var',
  19660. ' v: jsvalue;',
  19661. ' o: TObject;',
  19662. 'begin',
  19663. ' v:=o;',
  19664. ' v:=TObject(o);',
  19665. ' v:=TBirdObject(o);',
  19666. ' o:=TObject(v);',
  19667. ' o:=TBirdObject(v);',
  19668. ' if v is TObject then ;',
  19669. '']);
  19670. ConvertProgram;
  19671. CheckSource('TestJSValue_ClassInstance',
  19672. LinesToStr([ // statements
  19673. 'rtl.createClass($mod, "TObject", null, function () {',
  19674. ' this.$init = function () {',
  19675. ' };',
  19676. ' this.$final = function () {',
  19677. ' };',
  19678. '});',
  19679. 'this.v = undefined;',
  19680. 'this.o = null;',
  19681. '']),
  19682. LinesToStr([ // $mod.$main
  19683. '$mod.v = $mod.o;',
  19684. '$mod.v = $mod.o;',
  19685. '$mod.v = $mod.o;',
  19686. '$mod.o = rtl.getObject($mod.v);',
  19687. '$mod.o = rtl.getObject($mod.v);',
  19688. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  19689. '']));
  19690. end;
  19691. procedure TTestModule.TestJSValue_ClassOf;
  19692. begin
  19693. StartProgram(false);
  19694. Add([
  19695. 'type',
  19696. ' TClass = class of TObject;',
  19697. ' TObject = class',
  19698. ' end;',
  19699. ' TBirds = class of TBird;',
  19700. ' TBird = class(TObject) end;',
  19701. 'var',
  19702. ' v: jsvalue;',
  19703. ' c: TClass;',
  19704. 'begin',
  19705. ' v:=c;',
  19706. ' v:=TObject;',
  19707. ' v:=TClass(c);',
  19708. ' v:=TBirds(c);',
  19709. ' c:=TClass(v);',
  19710. ' c:=TBirds(v);',
  19711. ' if v is TClass then ;',
  19712. '']);
  19713. ConvertProgram;
  19714. CheckSource('TestJSValue_ClassOf',
  19715. LinesToStr([ // statements
  19716. 'rtl.createClass($mod, "TObject", null, function () {',
  19717. ' this.$init = function () {',
  19718. ' };',
  19719. ' this.$final = function () {',
  19720. ' };',
  19721. '});',
  19722. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  19723. '});',
  19724. 'this.v = undefined;',
  19725. 'this.c = null;',
  19726. '']),
  19727. LinesToStr([ // $mod.$main
  19728. '$mod.v = $mod.c;',
  19729. '$mod.v = $mod.TObject;',
  19730. '$mod.v = $mod.c;',
  19731. '$mod.v = $mod.c;',
  19732. '$mod.c = rtl.getObject($mod.v);',
  19733. '$mod.c = rtl.getObject($mod.v);',
  19734. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  19735. '']));
  19736. end;
  19737. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  19738. begin
  19739. StartProgram(false);
  19740. Add([
  19741. 'type',
  19742. ' integer = longint;',
  19743. ' TArray = array of JSValue;',
  19744. ' TArrgh = tarray;',
  19745. ' TArrInt = array of integer;',
  19746. 'var',
  19747. ' v: jsvalue;',
  19748. ' TheArray: tarray = (1,''2'');',
  19749. ' Arr: tarrgh;',
  19750. ' i: integer;',
  19751. ' ArrInt: tarrint;',
  19752. 'begin',
  19753. ' arr:=thearray;',
  19754. ' thearray:=arr;',
  19755. ' setlength(arr,2);',
  19756. ' setlength(thearray,3);',
  19757. ' arr[4]:=v;',
  19758. ' arr[5]:=length(thearray);',
  19759. ' arr[6]:=nil;',
  19760. ' arr[7]:=thearray[8];',
  19761. ' arr[low(arr)]:=high(thearray);',
  19762. ' arr:=arrint;',
  19763. ' arrInt:=tarrint(arr);',
  19764. ' if TheArray = nil then ;',
  19765. ' if nil = TheArray then ;',
  19766. ' if TheArray <> nil then ;',
  19767. ' if nil <> TheArray then ;',
  19768. '']);
  19769. ConvertProgram;
  19770. CheckSource('TestJSValue_ArrayOfJSValue',
  19771. LinesToStr([ // statements
  19772. 'this.v = undefined;',
  19773. 'this.TheArray = [1, "2"];',
  19774. 'this.Arr = [];',
  19775. 'this.i = 0;',
  19776. 'this.ArrInt = [];',
  19777. '']),
  19778. LinesToStr([ // $mod.$main
  19779. '$mod.Arr = $mod.TheArray;',
  19780. '$mod.TheArray = $mod.Arr;',
  19781. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  19782. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  19783. '$mod.Arr[4] = $mod.v;',
  19784. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  19785. '$mod.Arr[6] = null;',
  19786. '$mod.Arr[7] = $mod.TheArray[8];',
  19787. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  19788. '$mod.Arr = $mod.ArrInt;',
  19789. '$mod.ArrInt = $mod.Arr;',
  19790. 'if (rtl.length($mod.TheArray) === 0) ;',
  19791. 'if (rtl.length($mod.TheArray) === 0) ;',
  19792. 'if (rtl.length($mod.TheArray) > 0) ;',
  19793. 'if (rtl.length($mod.TheArray) > 0) ;',
  19794. '']));
  19795. end;
  19796. procedure TTestModule.TestJSValue_ArrayLit;
  19797. begin
  19798. StartProgram(false);
  19799. Add([
  19800. 'type',
  19801. ' TFlag = (big,small);',
  19802. ' TArray = array of JSValue;',
  19803. ' TObject = class end;',
  19804. ' TClass = class of TObject;',
  19805. 'var',
  19806. ' v: jsvalue;',
  19807. ' a: TArray;',
  19808. ' o: TObject;',
  19809. 'begin',
  19810. ' a:=[];',
  19811. ' a:=[1];',
  19812. ' a:=[1,2];',
  19813. ' a:=[big];',
  19814. ' a:=[1,big];',
  19815. ' a:=[o,nil];',
  19816. '']);
  19817. ConvertProgram;
  19818. CheckSource('TestJSValue_ArrayLit',
  19819. LinesToStr([ // statements
  19820. 'this.TFlag = {',
  19821. ' "0": "big",',
  19822. ' big: 0,',
  19823. ' "1": "small",',
  19824. ' small: 1',
  19825. '};',
  19826. 'rtl.createClass($mod, "TObject", null, function () {',
  19827. ' this.$init = function () {',
  19828. ' };',
  19829. ' this.$final = function () {',
  19830. ' };',
  19831. '});',
  19832. 'this.v = undefined;',
  19833. 'this.a = [];',
  19834. 'this.o = null;',
  19835. '']),
  19836. LinesToStr([ // $mod.$main
  19837. '$mod.a = [];',
  19838. '$mod.a = [1];',
  19839. '$mod.a = [1, 2];',
  19840. '$mod.a = [$mod.TFlag.big];',
  19841. '$mod.a = [1, $mod.TFlag.big];',
  19842. '$mod.a = [$mod.o, null];',
  19843. '']));
  19844. end;
  19845. procedure TTestModule.TestJSValue_Params;
  19846. begin
  19847. StartProgram(false);
  19848. Add('type');
  19849. Add(' integer = longint;');
  19850. Add(' TYesNo = boolean;');
  19851. Add(' TFloat = double;');
  19852. Add(' TCaption = string;');
  19853. Add(' TChar = char;');
  19854. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  19855. Add('var');
  19856. Add(' l: jsvalue;');
  19857. Add('begin');
  19858. Add(' a:=a;');
  19859. Add(' l:=b;');
  19860. Add(' c:=c;');
  19861. Add(' d:=d;');
  19862. Add(' Result:=l;');
  19863. Add('end;');
  19864. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  19865. Add('var');
  19866. Add(' v: jsvalue;');
  19867. Add(' i: integer;');
  19868. Add(' b: TYesNo;');
  19869. Add(' d: TFloat;');
  19870. Add(' s: TCaption;');
  19871. Add(' c: TChar;');
  19872. Add('begin');
  19873. Add(' v:=doit(v,v,v,v);');
  19874. Add(' i:=integer(dosome(i,i));');
  19875. Add(' b:=TYesNo(dosome(b,b));');
  19876. Add(' d:=TFloat(dosome(d,d));');
  19877. Add(' s:=TCaption(dosome(s,s));');
  19878. Add(' c:=TChar(dosome(c,c));');
  19879. ConvertProgram;
  19880. CheckSource('TestJSValue_Params',
  19881. LinesToStr([ // statements
  19882. 'this.DoIt = function (a, b, c, d) {',
  19883. ' var Result = undefined;',
  19884. ' var l = undefined;',
  19885. ' a = a;',
  19886. ' l = b;',
  19887. ' c.set(c.get());',
  19888. ' d.set(d.get());',
  19889. ' Result = l;',
  19890. ' return Result;',
  19891. '};',
  19892. 'this.DoSome = function (a, b) {',
  19893. ' var Result = undefined;',
  19894. ' return Result;',
  19895. '};',
  19896. 'this.v = undefined;',
  19897. 'this.i = 0;',
  19898. 'this.b = false;',
  19899. 'this.d = 0.0;',
  19900. 'this.s = "";',
  19901. 'this.c = "";',
  19902. '']),
  19903. LinesToStr([ // $mod.$main
  19904. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  19905. ' p: $mod,',
  19906. ' get: function () {',
  19907. ' return this.p.v;',
  19908. ' },',
  19909. ' set: function (v) {',
  19910. ' this.p.v = v;',
  19911. ' }',
  19912. '}, {',
  19913. ' p: $mod,',
  19914. ' get: function () {',
  19915. ' return this.p.v;',
  19916. ' },',
  19917. ' set: function (v) {',
  19918. ' this.p.v = v;',
  19919. ' }',
  19920. '});',
  19921. '$mod.i = Math.floor($mod.DoSome($mod.i, $mod.i));',
  19922. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  19923. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  19924. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  19925. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  19926. '']));
  19927. end;
  19928. procedure TTestModule.TestJSValue_UntypedParam;
  19929. begin
  19930. StartProgram(false);
  19931. Add('function DoIt(const a; var b; out c): jsvalue;');
  19932. Add('begin');
  19933. Add(' Result:=a;');
  19934. Add(' Result:=b;');
  19935. Add(' Result:=c;');
  19936. Add(' b:=Result;');
  19937. Add(' c:=Result;');
  19938. Add('end;');
  19939. Add('var i: longint;');
  19940. Add('begin');
  19941. Add(' doit(i,i,i);');
  19942. ConvertProgram;
  19943. CheckSource('TestJSValue_UntypedParam',
  19944. LinesToStr([ // statements
  19945. 'this.DoIt = function (a, b, c) {',
  19946. ' var Result = undefined;',
  19947. ' Result = a;',
  19948. ' Result = b.get();',
  19949. ' Result = c.get();',
  19950. ' b.set(Result);',
  19951. ' c.set(Result);',
  19952. ' return Result;',
  19953. '};',
  19954. 'this.i = 0;',
  19955. '']),
  19956. LinesToStr([ // $mod.$main
  19957. '$mod.DoIt($mod.i, {',
  19958. ' p: $mod,',
  19959. ' get: function () {',
  19960. ' return this.p.i;',
  19961. ' },',
  19962. ' set: function (v) {',
  19963. ' this.p.i = v;',
  19964. ' }',
  19965. '}, {',
  19966. ' p: $mod,',
  19967. ' get: function () {',
  19968. ' return this.p.i;',
  19969. ' },',
  19970. ' set: function (v) {',
  19971. ' this.p.i = v;',
  19972. ' }',
  19973. '});',
  19974. '']));
  19975. end;
  19976. procedure TTestModule.TestJSValue_FuncResultType;
  19977. begin
  19978. StartProgram(false);
  19979. Add('type');
  19980. Add(' integer = longint;');
  19981. Add(' TJSValueArray = array of JSValue;');
  19982. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  19983. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  19984. Add('begin');
  19985. Add(' while Compare(P,aList[0])>0 do ;');
  19986. Add('end;');
  19987. Add('var');
  19988. Add(' Compare: TListSortCompare;');
  19989. Add(' V: JSValue;');
  19990. Add(' i: integer;');
  19991. Add('begin');
  19992. Add(' if Compare(V,V)>0 then ;');
  19993. Add(' if Compare(i,i)>1 then ;');
  19994. Add(' if Compare(nil,false)>2 then ;');
  19995. Add(' if Compare(1,true)>3 then ;');
  19996. ConvertProgram;
  19997. CheckSource('TestJSValue_UntypedParam',
  19998. LinesToStr([ // statements
  19999. 'this.Sort = function (P, aList, Compare) {',
  20000. ' while (Compare(P, aList[0]) > 0) {',
  20001. ' };',
  20002. '};',
  20003. 'this.Compare = null;',
  20004. 'this.V = undefined;',
  20005. 'this.i = 0;',
  20006. '']),
  20007. LinesToStr([ // $mod.$main
  20008. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  20009. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  20010. 'if ($mod.Compare(null, false) > 2) ;',
  20011. 'if ($mod.Compare(1, true) > 3) ;',
  20012. '']));
  20013. end;
  20014. procedure TTestModule.TestJSValue_ProcType_Assign;
  20015. begin
  20016. StartProgram(false);
  20017. Add('type');
  20018. Add(' integer = longint;');
  20019. Add(' TObject = class');
  20020. Add(' class function GetGlob: integer;');
  20021. Add(' function Getter: integer;');
  20022. Add(' end;');
  20023. Add('class function TObject.GetGlob: integer;');
  20024. Add('var v1: jsvalue;');
  20025. Add('begin');
  20026. Add(' v1:=@GetGlob;');
  20027. Add(' v1:[email protected];');
  20028. Add('end;');
  20029. Add('function TObject.Getter: integer;');
  20030. Add('var v2: jsvalue;');
  20031. Add('begin');
  20032. Add(' v2:=@Getter;');
  20033. Add(' v2:[email protected];');
  20034. Add(' v2:=@GetGlob;');
  20035. Add(' v2:[email protected];');
  20036. Add('end;');
  20037. Add('function GetIt(i: integer): integer;');
  20038. Add('var v3: jsvalue;');
  20039. Add('begin');
  20040. Add(' v3:=@GetIt;');
  20041. Add('end;');
  20042. Add('var');
  20043. Add(' V: JSValue;');
  20044. Add(' o: TObject;');
  20045. Add('begin');
  20046. Add(' v:=@GetIt;');
  20047. Add(' v:[email protected];');
  20048. Add(' v:[email protected];');
  20049. ConvertProgram;
  20050. CheckSource('TestJSValue_ProcType_Assign',
  20051. LinesToStr([ // statements
  20052. 'rtl.createClass($mod, "TObject", null, function () {',
  20053. ' this.$init = function () {',
  20054. ' };',
  20055. ' this.$final = function () {',
  20056. ' };',
  20057. ' this.GetGlob = function () {',
  20058. ' var Result = 0;',
  20059. ' var v1 = undefined;',
  20060. ' v1 = rtl.createCallback(this, "GetGlob");',
  20061. ' v1 = rtl.createCallback(this, "GetGlob");',
  20062. ' return Result;',
  20063. ' };',
  20064. ' this.Getter = function () {',
  20065. ' var Result = 0;',
  20066. ' var v2 = undefined;',
  20067. ' v2 = rtl.createCallback(this, "Getter");',
  20068. ' v2 = rtl.createCallback(this, "Getter");',
  20069. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  20070. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  20071. ' return Result;',
  20072. ' };',
  20073. '});',
  20074. 'this.GetIt = function (i) {',
  20075. ' var Result = 0;',
  20076. ' var v3 = undefined;',
  20077. ' v3 = $mod.GetIt;',
  20078. ' return Result;',
  20079. '};',
  20080. 'this.V = undefined;',
  20081. 'this.o = null;',
  20082. '']),
  20083. LinesToStr([ // $mod.$main
  20084. '$mod.V = $mod.GetIt;',
  20085. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  20086. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  20087. '']));
  20088. end;
  20089. procedure TTestModule.TestJSValue_ProcType_Equal;
  20090. begin
  20091. StartProgram(false);
  20092. Add('type');
  20093. Add(' integer = longint;');
  20094. Add(' TObject = class');
  20095. Add(' class function GetGlob: integer;');
  20096. Add(' function Getter: integer;');
  20097. Add(' end;');
  20098. Add('class function TObject.GetGlob: integer;');
  20099. Add('var v1: jsvalue;');
  20100. Add('begin');
  20101. Add(' if v1=@GetGlob then;');
  20102. Add(' if [email protected] then ;');
  20103. Add('end;');
  20104. Add('function TObject.Getter: integer;');
  20105. Add('var v2: jsvalue;');
  20106. Add('begin');
  20107. Add(' if v2=@Getter then;');
  20108. Add(' if [email protected] then ;');
  20109. Add(' if v2=@GetGlob then;');
  20110. Add(' if [email protected] then;');
  20111. Add('end;');
  20112. Add('function GetIt(i: integer): integer;');
  20113. Add('var v3: jsvalue;');
  20114. Add('begin');
  20115. Add(' if v3=@GetIt then;');
  20116. Add('end;');
  20117. Add('var');
  20118. Add(' V: JSValue;');
  20119. Add(' o: TObject;');
  20120. Add('begin');
  20121. Add(' if v=@GetIt then;');
  20122. Add(' if [email protected] then;');
  20123. Add(' if [email protected] then;');
  20124. Add(' if @GetIt=v then;');
  20125. Add(' if @o.Getter=v then;');
  20126. Add(' if @o.GetGlob=v then;');
  20127. ConvertProgram;
  20128. CheckSource('TestJSValue_ProcType_Equal',
  20129. LinesToStr([ // statements
  20130. 'rtl.createClass($mod, "TObject", null, function () {',
  20131. ' this.$init = function () {',
  20132. ' };',
  20133. ' this.$final = function () {',
  20134. ' };',
  20135. ' this.GetGlob = function () {',
  20136. ' var Result = 0;',
  20137. ' var v1 = undefined;',
  20138. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  20139. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  20140. ' return Result;',
  20141. ' };',
  20142. ' this.Getter = function () {',
  20143. ' var Result = 0;',
  20144. ' var v2 = undefined;',
  20145. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  20146. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  20147. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  20148. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  20149. ' return Result;',
  20150. ' };',
  20151. '});',
  20152. 'this.GetIt = function (i) {',
  20153. ' var Result = 0;',
  20154. ' var v3 = undefined;',
  20155. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  20156. ' return Result;',
  20157. '};',
  20158. 'this.V = undefined;',
  20159. 'this.o = null;',
  20160. '']),
  20161. LinesToStr([ // $mod.$main
  20162. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  20163. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  20164. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  20165. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  20166. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  20167. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  20168. '']));
  20169. end;
  20170. procedure TTestModule.TestJSValue_ProcType_Param;
  20171. begin
  20172. StartProgram(false);
  20173. Add([
  20174. 'type',
  20175. ' variant = jsvalue;',
  20176. ' TArrVariant = array of variant;',
  20177. ' TArrVar2 = TArrVariant;',
  20178. ' TFuncInt = function: longint;',
  20179. 'function GetIt: longint;',
  20180. 'begin',
  20181. 'end;',
  20182. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  20183. 'var v: variant;',
  20184. 'begin',
  20185. ' v:=arr[1];',
  20186. 'end;',
  20187. 'var s: string;',
  20188. 'begin',
  20189. ' DoIt(GetIt,[]);',
  20190. ' DoIt(@GetIt,[]);',
  20191. ' DoIt(1,[s,GetIt]);',
  20192. ' DoIt(1,[s,@GetIt]);',
  20193. '']);
  20194. ConvertProgram;
  20195. CheckSource('TestJSValue_ProcType_Param',
  20196. LinesToStr([ // statements
  20197. 'this.GetIt = function () {',
  20198. ' var Result = 0;',
  20199. ' return Result;',
  20200. '};',
  20201. 'this.DoIt = function (p, Arr) {',
  20202. ' var v = undefined;',
  20203. ' v = Arr[1];',
  20204. '};',
  20205. 'this.s = "";',
  20206. '']),
  20207. LinesToStr([ // $mod.$main
  20208. '$mod.DoIt($mod.GetIt(), []);',
  20209. '$mod.DoIt($mod.GetIt, []);',
  20210. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  20211. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  20212. '']));
  20213. end;
  20214. procedure TTestModule.TestJSValue_AssignToPointerFail;
  20215. begin
  20216. StartProgram(false);
  20217. Add([
  20218. 'var',
  20219. ' v: JSValue;',
  20220. ' p: Pointer;',
  20221. 'begin',
  20222. ' p:=v;',
  20223. '']);
  20224. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  20225. nIncompatibleTypesGotExpected);
  20226. ConvertProgram;
  20227. end;
  20228. procedure TTestModule.TestJSValue_OverloadDouble;
  20229. begin
  20230. StartProgram(false);
  20231. Add([
  20232. 'type',
  20233. ' integer = longint;',
  20234. ' tdatetime = double;',
  20235. 'procedure DoIt(d: double); begin end;',
  20236. 'procedure DoIt(v: jsvalue); begin end;',
  20237. 'var',
  20238. ' d: double;',
  20239. ' dt: tdatetime;',
  20240. ' i: integer;',
  20241. ' b: byte;',
  20242. ' shi: shortint;',
  20243. ' w: word;',
  20244. ' smi: smallint;',
  20245. ' lw: longword;',
  20246. ' li: longint;',
  20247. ' ni: nativeint;',
  20248. ' nu: nativeuint;',
  20249. 'begin',
  20250. ' DoIt(d);',
  20251. ' DoIt(dt);',
  20252. ' DoIt(i);',
  20253. ' DoIt(b);',
  20254. ' DoIt(shi);',
  20255. ' DoIt(w);',
  20256. ' DoIt(smi);',
  20257. ' DoIt(lw);',
  20258. ' DoIt(li);',
  20259. ' DoIt(ni);',
  20260. ' DoIt(nu);',
  20261. '']);
  20262. ConvertProgram;
  20263. CheckSource('TestJSValue_OverloadDouble',
  20264. LinesToStr([ // statements
  20265. 'this.DoIt = function (d) {',
  20266. '};',
  20267. 'this.DoIt$1 = function (v) {',
  20268. '};',
  20269. 'this.d = 0.0;',
  20270. 'this.dt = 0.0;',
  20271. 'this.i = 0;',
  20272. 'this.b = 0;',
  20273. 'this.shi = 0;',
  20274. 'this.w = 0;',
  20275. 'this.smi = 0;',
  20276. 'this.lw = 0;',
  20277. 'this.li = 0;',
  20278. 'this.ni = 0;',
  20279. 'this.nu = 0;',
  20280. '']),
  20281. LinesToStr([ // $mod.$main
  20282. '$mod.DoIt($mod.d);',
  20283. '$mod.DoIt($mod.dt);',
  20284. '$mod.DoIt$1($mod.i);',
  20285. '$mod.DoIt$1($mod.b);',
  20286. '$mod.DoIt$1($mod.shi);',
  20287. '$mod.DoIt$1($mod.w);',
  20288. '$mod.DoIt$1($mod.smi);',
  20289. '$mod.DoIt$1($mod.lw);',
  20290. '$mod.DoIt$1($mod.li);',
  20291. '$mod.DoIt$1($mod.ni);',
  20292. '$mod.DoIt$1($mod.nu);',
  20293. '']));
  20294. end;
  20295. procedure TTestModule.TestJSValue_OverloadNativeInt;
  20296. begin
  20297. StartProgram(false);
  20298. Add([
  20299. 'type',
  20300. ' integer = longint;',
  20301. ' int53 = nativeint;',
  20302. ' tdatetime = double;',
  20303. 'procedure DoIt(n: nativeint); begin end;',
  20304. 'procedure DoIt(v: jsvalue); begin end;',
  20305. 'var',
  20306. ' d: double;',
  20307. ' dt: tdatetime;',
  20308. ' i: integer;',
  20309. ' b: byte;',
  20310. ' shi: shortint;',
  20311. ' w: word;',
  20312. ' smi: smallint;',
  20313. ' lw: longword;',
  20314. ' li: longint;',
  20315. ' ni: nativeint;',
  20316. ' nu: nativeuint;',
  20317. 'begin',
  20318. ' DoIt(d);',
  20319. ' DoIt(dt);',
  20320. ' DoIt(i);',
  20321. ' DoIt(b);',
  20322. ' DoIt(shi);',
  20323. ' DoIt(w);',
  20324. ' DoIt(smi);',
  20325. ' DoIt(lw);',
  20326. ' DoIt(li);',
  20327. ' DoIt(ni);',
  20328. ' DoIt(nu);',
  20329. '']);
  20330. ConvertProgram;
  20331. CheckSource('TestJSValue_OverloadNativeInt',
  20332. LinesToStr([ // statements
  20333. 'this.DoIt = function (n) {',
  20334. '};',
  20335. 'this.DoIt$1 = function (v) {',
  20336. '};',
  20337. 'this.d = 0.0;',
  20338. 'this.dt = 0.0;',
  20339. 'this.i = 0;',
  20340. 'this.b = 0;',
  20341. 'this.shi = 0;',
  20342. 'this.w = 0;',
  20343. 'this.smi = 0;',
  20344. 'this.lw = 0;',
  20345. 'this.li = 0;',
  20346. 'this.ni = 0;',
  20347. 'this.nu = 0;',
  20348. '']),
  20349. LinesToStr([ // $mod.$main
  20350. '$mod.DoIt$1($mod.d);',
  20351. '$mod.DoIt$1($mod.dt);',
  20352. '$mod.DoIt($mod.i);',
  20353. '$mod.DoIt($mod.b);',
  20354. '$mod.DoIt($mod.shi);',
  20355. '$mod.DoIt($mod.w);',
  20356. '$mod.DoIt($mod.smi);',
  20357. '$mod.DoIt($mod.lw);',
  20358. '$mod.DoIt($mod.li);',
  20359. '$mod.DoIt($mod.ni);',
  20360. '$mod.DoIt($mod.nu);',
  20361. '']));
  20362. end;
  20363. procedure TTestModule.TestJSValue_OverloadWord;
  20364. begin
  20365. StartProgram(false);
  20366. Add([
  20367. 'type',
  20368. ' integer = longint;',
  20369. ' int53 = nativeint;',
  20370. ' tdatetime = double;',
  20371. 'procedure DoIt(w: word); begin end;',
  20372. 'procedure DoIt(v: jsvalue); begin end;',
  20373. 'var',
  20374. ' d: double;',
  20375. ' dt: tdatetime;',
  20376. ' i: integer;',
  20377. ' b: byte;',
  20378. ' shi: shortint;',
  20379. ' w: word;',
  20380. ' smi: smallint;',
  20381. ' lw: longword;',
  20382. ' li: longint;',
  20383. ' ni: nativeint;',
  20384. ' nu: nativeuint;',
  20385. 'begin',
  20386. ' DoIt(d);',
  20387. ' DoIt(dt);',
  20388. ' DoIt(i);',
  20389. ' DoIt(b);',
  20390. ' DoIt(shi);',
  20391. ' DoIt(w);',
  20392. ' DoIt(smi);',
  20393. ' DoIt(lw);',
  20394. ' DoIt(li);',
  20395. ' DoIt(ni);',
  20396. ' DoIt(nu);',
  20397. '']);
  20398. ConvertProgram;
  20399. CheckSource('TestJSValue_OverloadWord',
  20400. LinesToStr([ // statements
  20401. 'this.DoIt = function (w) {',
  20402. '};',
  20403. 'this.DoIt$1 = function (v) {',
  20404. '};',
  20405. 'this.d = 0.0;',
  20406. 'this.dt = 0.0;',
  20407. 'this.i = 0;',
  20408. 'this.b = 0;',
  20409. 'this.shi = 0;',
  20410. 'this.w = 0;',
  20411. 'this.smi = 0;',
  20412. 'this.lw = 0;',
  20413. 'this.li = 0;',
  20414. 'this.ni = 0;',
  20415. 'this.nu = 0;',
  20416. '']),
  20417. LinesToStr([ // $mod.$main
  20418. '$mod.DoIt$1($mod.d);',
  20419. '$mod.DoIt$1($mod.dt);',
  20420. '$mod.DoIt$1($mod.i);',
  20421. '$mod.DoIt($mod.b);',
  20422. '$mod.DoIt($mod.shi);',
  20423. '$mod.DoIt($mod.w);',
  20424. '$mod.DoIt$1($mod.smi);',
  20425. '$mod.DoIt$1($mod.lw);',
  20426. '$mod.DoIt$1($mod.li);',
  20427. '$mod.DoIt$1($mod.ni);',
  20428. '$mod.DoIt$1($mod.nu);',
  20429. '']));
  20430. end;
  20431. procedure TTestModule.TestJSValue_OverloadString;
  20432. begin
  20433. StartProgram(false);
  20434. Add([
  20435. 'type',
  20436. ' uni = string;',
  20437. ' WChar = char;',
  20438. 'procedure DoIt(s: string); begin end;',
  20439. 'procedure DoIt(v: jsvalue); begin end;',
  20440. 'var',
  20441. ' s: string;',
  20442. ' c: char;',
  20443. ' u: uni;',
  20444. 'begin',
  20445. ' DoIt(s);',
  20446. ' DoIt(c);',
  20447. ' DoIt(u);',
  20448. '']);
  20449. ConvertProgram;
  20450. CheckSource('TestJSValue_OverloadString',
  20451. LinesToStr([ // statements
  20452. 'this.DoIt = function (s) {',
  20453. '};',
  20454. 'this.DoIt$1 = function (v) {',
  20455. '};',
  20456. 'this.s = "";',
  20457. 'this.c = "";',
  20458. 'this.u = "";',
  20459. '']),
  20460. LinesToStr([ // $mod.$main
  20461. '$mod.DoIt($mod.s);',
  20462. '$mod.DoIt($mod.c);',
  20463. '$mod.DoIt($mod.u);',
  20464. '']));
  20465. end;
  20466. procedure TTestModule.TestJSValue_OverloadChar;
  20467. begin
  20468. StartProgram(false);
  20469. Add([
  20470. 'type',
  20471. ' uni = string;',
  20472. ' WChar = char;',
  20473. 'procedure DoIt(c: char); begin end;',
  20474. 'procedure DoIt(v: jsvalue); begin end;',
  20475. 'var',
  20476. ' s: string;',
  20477. ' c: char;',
  20478. ' u: uni;',
  20479. 'begin',
  20480. ' DoIt(s);',
  20481. ' DoIt(c);',
  20482. ' DoIt(u);',
  20483. '']);
  20484. ConvertProgram;
  20485. CheckSource('TestJSValue_OverloadChar',
  20486. LinesToStr([ // statements
  20487. 'this.DoIt = function (c) {',
  20488. '};',
  20489. 'this.DoIt$1 = function (v) {',
  20490. '};',
  20491. 'this.s = "";',
  20492. 'this.c = "";',
  20493. 'this.u = "";',
  20494. '']),
  20495. LinesToStr([ // $mod.$main
  20496. '$mod.DoIt$1($mod.s);',
  20497. '$mod.DoIt($mod.c);',
  20498. '$mod.DoIt$1($mod.u);',
  20499. '']));
  20500. end;
  20501. procedure TTestModule.TestJSValue_OverloadPointer;
  20502. begin
  20503. StartProgram(false);
  20504. Add([
  20505. 'type',
  20506. ' TObject = class end;',
  20507. 'procedure DoIt(p: pointer); begin end;',
  20508. 'procedure DoIt(v: jsvalue); begin end;',
  20509. 'var',
  20510. ' o: TObject;',
  20511. 'begin',
  20512. ' DoIt(o);',
  20513. '']);
  20514. ConvertProgram;
  20515. CheckSource('TestJSValue_OverloadPointer',
  20516. LinesToStr([ // statements
  20517. 'rtl.createClass($mod, "TObject", null, function () {',
  20518. ' this.$init = function () {',
  20519. ' };',
  20520. ' this.$final = function () {',
  20521. ' };',
  20522. '});',
  20523. 'this.DoIt = function (p) {',
  20524. '};',
  20525. 'this.DoIt$1 = function (v) {',
  20526. '};',
  20527. 'this.o = null;',
  20528. '']),
  20529. LinesToStr([ // $mod.$main
  20530. '$mod.DoIt($mod.o);',
  20531. '']));
  20532. end;
  20533. procedure TTestModule.TestJSValue_ForIn;
  20534. begin
  20535. StartProgram(false);
  20536. Add([
  20537. 'var',
  20538. ' v: JSValue;',
  20539. ' key: string;',
  20540. 'begin',
  20541. ' for key in v do begin',
  20542. ' if key=''abc'' then ;',
  20543. ' end;',
  20544. '']);
  20545. ConvertProgram;
  20546. CheckSource('TestJSValue_ForIn',
  20547. LinesToStr([ // statements
  20548. 'this.v = undefined;',
  20549. 'this.key = "";',
  20550. '']),
  20551. LinesToStr([ // $mod.$main
  20552. 'for ($mod.key in $mod.v) {',
  20553. ' if ($mod.key === "abc") ;',
  20554. '};',
  20555. '']));
  20556. end;
  20557. procedure TTestModule.TestRTTI_IntRange;
  20558. begin
  20559. Converter.Options:=Converter.Options-[coNoTypeInfo];
  20560. StartProgram(false);
  20561. Add([
  20562. '{$modeswitch externalclass}',
  20563. 'type',
  20564. ' TTypeInfo = class external name ''rtl.tTypeInfo''',
  20565. ' end;',
  20566. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  20567. ' end;',
  20568. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  20569. ' TColor = type TGraphicsColor;',
  20570. 'var',
  20571. ' p: TTypeInfo;',
  20572. 'begin',
  20573. ' p:=typeinfo(TGraphicsColor);',
  20574. ' p:=typeinfo(TColor);',
  20575. '']);
  20576. ConvertProgram;
  20577. CheckSource('TestRTTI_IntRange',
  20578. LinesToStr([ // statements
  20579. '$mod.$rtti.$Int("TGraphicsColor", {',
  20580. ' minvalue: -2147483648,',
  20581. ' maxvalue: 2147483647,',
  20582. ' ordtype: 4',
  20583. '});',
  20584. '$mod.$rtti.$inherited("TColor", $mod.$rtti["TGraphicsColor"], {});',
  20585. 'this.p = null;',
  20586. '']),
  20587. LinesToStr([ // $mod.$main
  20588. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  20589. '$mod.p = $mod.$rtti["TColor"];',
  20590. '']));
  20591. end;
  20592. procedure TTestModule.TestRTTI_Double;
  20593. begin
  20594. Converter.Options:=Converter.Options-[coNoTypeInfo];
  20595. StartProgram(false);
  20596. Add([
  20597. '{$modeswitch externalclass}',
  20598. 'type',
  20599. ' TTypeInfo = class external name ''rtl.tTypeInfo''',
  20600. ' end;',
  20601. ' TFloat = type double;',
  20602. 'var',
  20603. ' p: TTypeInfo;',
  20604. 'begin',
  20605. ' p:=typeinfo(double);',
  20606. ' p:=typeinfo(TFloat);',
  20607. '']);
  20608. ConvertProgram;
  20609. CheckSource('TestRTTI_Double',
  20610. LinesToStr([ // statements
  20611. '$mod.$rtti.$inherited("TFloat", rtl.double, {});',
  20612. 'this.p = null;',
  20613. '']),
  20614. LinesToStr([ // $mod.$main
  20615. '$mod.p = rtl.double;',
  20616. '$mod.p = $mod.$rtti["TFloat"];',
  20617. '']));
  20618. end;
  20619. procedure TTestModule.TestRTTI_ProcType;
  20620. begin
  20621. Converter.Options:=Converter.Options-[coNoTypeInfo];
  20622. StartProgram(false);
  20623. Add('type');
  20624. Add(' TProcA = procedure;');
  20625. Add(' TMethodB = procedure of object;');
  20626. Add(' TProcC = procedure; varargs;');
  20627. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  20628. Add(' TProcE = function: nativeint;');
  20629. Add(' TProcF = function(const p: TProcA): nativeuint;');
  20630. Add('var p: pointer;');
  20631. Add('begin');
  20632. Add(' p:=typeinfo(tproca);');
  20633. ConvertProgram;
  20634. CheckSource('TestRTTI_ProcType',
  20635. LinesToStr([ // statements
  20636. '$mod.$rtti.$ProcVar("TProcA", {',
  20637. ' procsig: rtl.newTIProcSig(null)',
  20638. '});',
  20639. '$mod.$rtti.$MethodVar("TMethodB", {',
  20640. ' procsig: rtl.newTIProcSig(null),',
  20641. ' methodkind: 0',
  20642. '});',
  20643. '$mod.$rtti.$ProcVar("TProcC", {',
  20644. ' procsig: rtl.newTIProcSig(null, 2)',
  20645. '});',
  20646. '$mod.$rtti.$ProcVar("TProcD", {',
  20647. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  20648. '});',
  20649. '$mod.$rtti.$ProcVar("TProcE", {',
  20650. ' procsig: rtl.newTIProcSig(null, rtl.nativeint)',
  20651. '});',
  20652. '$mod.$rtti.$ProcVar("TProcF", {',
  20653. ' procsig: rtl.newTIProcSig([["p", $mod.$rtti["TProcA"], 2]], rtl.nativeuint)',
  20654. '});',
  20655. 'this.p = null;',
  20656. '']),
  20657. LinesToStr([ // $mod.$main
  20658. '$mod.p = $mod.$rtti["TProcA"];',
  20659. '']));
  20660. end;
  20661. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  20662. begin
  20663. Converter.Options:=Converter.Options-[coNoTypeInfo];
  20664. AddModuleWithIntfImplSrc('unit2.pas',
  20665. LinesToStr([
  20666. 'type',
  20667. ' TObject = class end;'
  20668. ]),
  20669. '');
  20670. StartUnit(true);
  20671. Add('interface');
  20672. Add('uses unit2;');
  20673. Add('type');
  20674. Add(' TProcA = function(o: tobject): tobject;');
  20675. Add('implementation');
  20676. Add('type');
  20677. Add(' TProcB = function(o: tobject): tobject;');
  20678. Add('var p: Pointer;');
  20679. Add('initialization');
  20680. Add(' p:=typeinfo(tproca);');
  20681. Add(' p:=typeinfo(tprocb);');
  20682. ConvertUnit;
  20683. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  20684. LinesToStr([ // statements
  20685. 'var $impl = $mod.$impl;',
  20686. '$mod.$rtti.$ProcVar("TProcA", {',
  20687. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  20688. '});',
  20689. '']),
  20690. LinesToStr([ // this.$init
  20691. '$impl.p = $mod.$rtti["TProcA"];',
  20692. '$impl.p = $mod.$rtti["TProcB"];',
  20693. '']),
  20694. LinesToStr([ // implementation
  20695. '$mod.$rtti.$ProcVar("TProcB", {',
  20696. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  20697. '});',
  20698. '$impl.p = null;',
  20699. '']) );
  20700. end;
  20701. procedure TTestModule.TestRTTI_EnumAndSetType;
  20702. begin
  20703. Converter.Options:=Converter.Options-[coNoTypeInfo];
  20704. StartProgram(false);
  20705. Add('type');
  20706. Add(' TFlag = (light,dark);');
  20707. Add(' TFlags = set of TFlag;');
  20708. Add(' TProc = function(f: TFlags): TFlag;');
  20709. Add('var p: pointer;');
  20710. Add('begin');
  20711. Add(' p:=typeinfo(tflag);');
  20712. Add(' p:=typeinfo(tflags);');
  20713. ConvertProgram;
  20714. CheckSource('TestRTTI_EnumAndType',
  20715. LinesToStr([ // statements
  20716. 'this.TFlag = {',
  20717. ' "0": "light",',
  20718. ' light: 0,',
  20719. ' "1": "dark",',
  20720. ' dark: 1',
  20721. '};',
  20722. '$mod.$rtti.$Enum("TFlag", {',
  20723. ' minvalue: 0,',
  20724. ' maxvalue: 1,',
  20725. ' ordtype: 1,',
  20726. ' enumtype: this.TFlag',
  20727. '});',
  20728. '$mod.$rtti.$Set("TFlags", {',
  20729. ' comptype: $mod.$rtti["TFlag"]',
  20730. '});',
  20731. '$mod.$rtti.$ProcVar("TProc", {',
  20732. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TFlags"]]], $mod.$rtti["TFlag"])',
  20733. '});',
  20734. 'this.p = null;',
  20735. '']),
  20736. LinesToStr([ // $mod.$main
  20737. '$mod.p = $mod.$rtti["TFlag"];',
  20738. '$mod.p = $mod.$rtti["TFlags"];',
  20739. '']));
  20740. end;
  20741. procedure TTestModule.TestRTTI_EnumRange;
  20742. begin
  20743. Converter.Options:=Converter.Options-[coNoTypeInfo];
  20744. StartProgram(false);
  20745. Add([
  20746. 'type',
  20747. ' TCol = (red,green,blue);',
  20748. ' TColRg = green..blue;',
  20749. ' TSetOfColRg = set of TColRg;',
  20750. 'var p: pointer;',
  20751. 'begin',
  20752. ' p:=typeinfo(tcolrg);',
  20753. ' p:=typeinfo(tsetofcolrg);',
  20754. '']);
  20755. ConvertProgram;
  20756. end;
  20757. procedure TTestModule.TestRTTI_AnonymousEnumType;
  20758. begin
  20759. Converter.Options:=Converter.Options-[coNoTypeInfo];
  20760. StartProgram(false);
  20761. Add('type');
  20762. Add(' TFlags = set of (red, green);');
  20763. Add('var');
  20764. Add(' f: TFlags;');
  20765. Add('begin');
  20766. Add(' Include(f,red);');
  20767. ConvertProgram;
  20768. CheckSource('TestRTTI_AnonymousEnumType',
  20769. LinesToStr([ // statements
  20770. 'this.TFlags$a = {',
  20771. ' "0": "red",',
  20772. ' red: 0,',
  20773. ' "1": "green",',
  20774. ' green: 1',
  20775. '};',
  20776. '$mod.$rtti.$Enum("TFlags$a", {',
  20777. ' minvalue: 0,',
  20778. ' maxvalue: 1,',
  20779. ' ordtype: 1,',
  20780. ' enumtype: this.TFlags$a',
  20781. '});',
  20782. '$mod.$rtti.$Set("TFlags", {',
  20783. ' comptype: $mod.$rtti["TFlags$a"]',
  20784. '});',
  20785. 'this.f = {};',
  20786. '']),
  20787. LinesToStr([
  20788. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  20789. '']));
  20790. end;
  20791. procedure TTestModule.TestRTTI_StaticArray;
  20792. begin
  20793. Converter.Options:=Converter.Options-[coNoTypeInfo];
  20794. StartProgram(false);
  20795. Add('type');
  20796. Add(' TFlag = (light,dark);');
  20797. Add(' TFlagNames = array[TFlag] of string;');
  20798. Add(' TBoolNames = array[boolean] of string;');
  20799. Add(' TByteArray = array[1..32768] of byte;');
  20800. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  20801. Add('var p: pointer;');
  20802. Add('begin');
  20803. Add(' p:=typeinfo(TFlagNames);');
  20804. Add(' p:=typeinfo(TBoolNames);');
  20805. ConvertProgram;
  20806. CheckSource('TestRTTI_StaticArray',
  20807. LinesToStr([ // statements
  20808. 'this.TFlag = {',
  20809. ' "0": "light",',
  20810. ' light: 0,',
  20811. ' "1": "dark",',
  20812. ' dark: 1',
  20813. '};',
  20814. '$mod.$rtti.$Enum("TFlag", {',
  20815. ' minvalue: 0,',
  20816. ' maxvalue: 1,',
  20817. ' ordtype: 1,',
  20818. ' enumtype: this.TFlag',
  20819. '});',
  20820. '$mod.$rtti.$StaticArray("TFlagNames", {',
  20821. ' dims: [2],',
  20822. ' eltype: rtl.string',
  20823. '});',
  20824. '$mod.$rtti.$StaticArray("TBoolNames", {',
  20825. ' dims: [2],',
  20826. ' eltype: rtl.string',
  20827. '});',
  20828. '$mod.$rtti.$StaticArray("TByteArray", {',
  20829. ' dims: [32768],',
  20830. ' eltype: rtl.byte',
  20831. '});',
  20832. '$mod.$rtti.$ProcVar("TProc", {',
  20833. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TBoolNames"]]], $mod.$rtti["TFlagNames"])',
  20834. '});',
  20835. 'this.p = null;',
  20836. '']),
  20837. LinesToStr([ // $mod.$main
  20838. '$mod.p = $mod.$rtti["TFlagNames"];',
  20839. '$mod.p = $mod.$rtti["TBoolNames"];',
  20840. '']));
  20841. end;
  20842. procedure TTestModule.TestRTTI_DynArray;
  20843. begin
  20844. Converter.Options:=Converter.Options-[coNoTypeInfo];
  20845. StartProgram(false);
  20846. Add('type');
  20847. Add(' TArrStr = array of string;');
  20848. Add(' TArr2Dim = array of tarrstr;');
  20849. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  20850. Add('var p: pointer;');
  20851. Add('begin');
  20852. Add(' p:=typeinfo(tarrstr);');
  20853. Add(' p:=typeinfo(tarr2dim);');
  20854. ConvertProgram;
  20855. CheckSource('TestRTTI_DynArray',
  20856. LinesToStr([ // statements
  20857. '$mod.$rtti.$DynArray("TArrStr", {',
  20858. ' eltype: rtl.string',
  20859. '});',
  20860. '$mod.$rtti.$DynArray("TArr2Dim", {',
  20861. ' eltype: $mod.$rtti["TArrStr"]',
  20862. '});',
  20863. '$mod.$rtti.$ProcVar("TProc", {',
  20864. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TArrStr"]]], $mod.$rtti["TArr2Dim"])',
  20865. '});',
  20866. 'this.p = null;',
  20867. '']),
  20868. LinesToStr([ // $mod.$main
  20869. '$mod.p = $mod.$rtti["TArrStr"];',
  20870. '$mod.p = $mod.$rtti["TArr2Dim"];',
  20871. '']));
  20872. end;
  20873. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  20874. begin
  20875. Converter.Options:=Converter.Options-[coNoTypeInfo];
  20876. StartProgram(false);
  20877. Add('type');
  20878. Add(' TArr = array of array of longint;');
  20879. Add('var a: TArr;');
  20880. Add('begin');
  20881. ConvertProgram;
  20882. CheckSource('TestRTTI_ArrayNestedAnonymous',
  20883. LinesToStr([ // statements
  20884. '$mod.$rtti.$DynArray("TArr$a", {',
  20885. ' eltype: rtl.longint',
  20886. '});',
  20887. '$mod.$rtti.$DynArray("TArr", {',
  20888. ' eltype: $mod.$rtti["TArr$a"]',
  20889. '});',
  20890. 'this.a = [];',
  20891. '']),
  20892. LinesToStr([ // $mod.$main
  20893. ]));
  20894. end;
  20895. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  20896. begin
  20897. Converter.Options:=Converter.Options-[coNoTypeInfo];
  20898. StartProgram(false);
  20899. Add('type');
  20900. Add(' TObject = class');
  20901. Add(' published');
  20902. Add(' procedure Proc; virtual; abstract;');
  20903. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  20904. Add(' end;');
  20905. Add('begin');
  20906. SetExpectedPasResolverError('Duplicate identifier "Proc" at test1.pp(6,19)',
  20907. nDuplicateIdentifier);
  20908. ConvertProgram;
  20909. end;
  20910. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  20911. begin
  20912. Converter.Options:=Converter.Options-[coNoTypeInfo];
  20913. StartProgram(false);
  20914. Add('type');
  20915. Add(' TObject = class');
  20916. Add(' published');
  20917. Add(' procedure Proc; external name ''foo'';');
  20918. Add(' end;');
  20919. Add('begin');
  20920. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  20921. nPublishedNameMustMatchExternal);
  20922. ConvertProgram;
  20923. end;
  20924. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  20925. begin
  20926. Converter.Options:=Converter.Options-[coNoTypeInfo];
  20927. StartProgram(false);
  20928. Add('type');
  20929. Add(' TObject = class');
  20930. Add(' class var FA: longint;');
  20931. Add(' published');
  20932. Add(' class property A: longint read FA;');
  20933. Add(' end;');
  20934. Add('begin');
  20935. SetExpectedPasResolverError('Invalid published property modifier "class"',
  20936. nInvalidXModifierY);
  20937. ConvertProgram;
  20938. end;
  20939. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  20940. begin
  20941. Converter.Options:=Converter.Options-[coNoTypeInfo];
  20942. StartProgram(false);
  20943. Add('type');
  20944. Add(' TObject = class');
  20945. Add(' published');
  20946. Add(' class var FA: longint;');
  20947. Add(' end;');
  20948. Add('begin');
  20949. SetExpectedPasResolverError(sSymbolCannotBePublished,
  20950. nSymbolCannotBePublished);
  20951. ConvertProgram;
  20952. end;
  20953. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  20954. begin
  20955. Converter.Options:=Converter.Options-[coNoTypeInfo];
  20956. StartProgram(false);
  20957. Add('{$modeswitch externalclass}');
  20958. Add('type');
  20959. Add(' TObject = class');
  20960. Add(' published');
  20961. Add(' V: longint; external name ''foo'';');
  20962. Add(' end;');
  20963. Add('begin');
  20964. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  20965. nPublishedNameMustMatchExternal);
  20966. ConvertProgram;
  20967. end;
  20968. procedure TTestModule.TestRTTI_Class_Field;
  20969. begin
  20970. Converter.Options:=Converter.Options-[coNoTypeInfo];
  20971. StartProgram(false);
  20972. Add('{$modeswitch externalclass}');
  20973. Add('type');
  20974. Add(' TObject = class');
  20975. Add(' private');
  20976. Add(' FPropA: string;');
  20977. Add(' published');
  20978. Add(' VarLI: longint;');
  20979. Add(' VarC: char;');
  20980. Add(' VarS: string;');
  20981. Add(' VarD: double;');
  20982. Add(' VarB: boolean;');
  20983. Add(' VarLW: longword;');
  20984. Add(' VarSmI: smallint;');
  20985. Add(' VarW: word;');
  20986. Add(' VarShI: shortint;');
  20987. Add(' VarBy: byte;');
  20988. Add(' VarExt: longint external name ''VarExt'';');
  20989. Add(' end;');
  20990. Add('var p: pointer;');
  20991. Add(' Obj: tobject;');
  20992. Add('begin');
  20993. Add(' p:=typeinfo(tobject);');
  20994. Add(' p:=typeinfo(p);');
  20995. Add(' p:=typeinfo(obj);');
  20996. ConvertProgram;
  20997. CheckSource('TestRTTI_Class_Field',
  20998. LinesToStr([ // statements
  20999. 'rtl.createClass($mod, "TObject", null, function () {',
  21000. ' this.$init = function () {',
  21001. ' this.FPropA = "";',
  21002. ' this.VarLI = 0;',
  21003. ' this.VarC = "";',
  21004. ' this.VarS = "";',
  21005. ' this.VarD = 0.0;',
  21006. ' this.VarB = false;',
  21007. ' this.VarLW = 0;',
  21008. ' this.VarSmI = 0;',
  21009. ' this.VarW = 0;',
  21010. ' this.VarShI = 0;',
  21011. ' this.VarBy = 0;',
  21012. ' };',
  21013. ' this.$final = function () {',
  21014. ' };',
  21015. ' var $r = this.$rtti;',
  21016. ' $r.addField("VarLI", rtl.longint);',
  21017. ' $r.addField("VarC", rtl.char);',
  21018. ' $r.addField("VarS", rtl.string);',
  21019. ' $r.addField("VarD", rtl.double);',
  21020. ' $r.addField("VarB", rtl.boolean);',
  21021. ' $r.addField("VarLW", rtl.longword);',
  21022. ' $r.addField("VarSmI", rtl.smallint);',
  21023. ' $r.addField("VarW", rtl.word);',
  21024. ' $r.addField("VarShI", rtl.shortint);',
  21025. ' $r.addField("VarBy", rtl.byte);',
  21026. ' $r.addField("VarExt", rtl.longint);',
  21027. '});',
  21028. 'this.p = null;',
  21029. 'this.Obj = null;',
  21030. '']),
  21031. LinesToStr([ // $mod.$main
  21032. '$mod.p = $mod.$rtti["TObject"];',
  21033. '$mod.p = rtl.pointer;',
  21034. '$mod.p = $mod.Obj.$rtti;',
  21035. '']));
  21036. end;
  21037. procedure TTestModule.TestRTTI_Class_Method;
  21038. begin
  21039. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21040. StartProgram(false);
  21041. Add('type');
  21042. Add(' TObject = class');
  21043. Add(' private');
  21044. Add(' procedure Internal; external name ''$intern'';');
  21045. Add(' published');
  21046. Add(' procedure Click; virtual; abstract;');
  21047. Add(' procedure Notify(Sender: TObject); virtual; abstract;');
  21048. Add(' function GetNotify: boolean; external name ''GetNotify'';');
  21049. Add(' procedure Println(a,b: longint); varargs; virtual; abstract;');
  21050. Add(' end;');
  21051. Add('begin');
  21052. ConvertProgram;
  21053. CheckSource('TestRTTI_Class_Method',
  21054. LinesToStr([ // statements
  21055. 'rtl.createClass($mod, "TObject", null, function () {',
  21056. ' this.$init = function () {',
  21057. ' };',
  21058. ' this.$final = function () {',
  21059. ' };',
  21060. ' var $r = this.$rtti;',
  21061. ' $r.addMethod("Click", 0, null);',
  21062. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  21063. ' $r.addMethod("GetNotify", 1, null, rtl.boolean,{flags: 4});',
  21064. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, {',
  21065. ' flags: 2',
  21066. ' });',
  21067. '});',
  21068. '']),
  21069. LinesToStr([ // $mod.$main
  21070. '']));
  21071. end;
  21072. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  21073. begin
  21074. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21075. StartProgram(false);
  21076. Add('type');
  21077. Add(' TObject = class');
  21078. Add(' published');
  21079. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  21080. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  21081. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  21082. Add(' end;');
  21083. Add('begin');
  21084. ConvertProgram;
  21085. CheckSource('TestRTTI_Class_MethodOpenArray',
  21086. LinesToStr([ // statements
  21087. 'rtl.createClass($mod, "TObject", null, function () {',
  21088. ' this.$init = function () {',
  21089. ' };',
  21090. ' this.$final = function () {',
  21091. ' };',
  21092. ' var $r = this.$rtti;',
  21093. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  21094. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  21095. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  21096. '});',
  21097. '']),
  21098. LinesToStr([ // $mod.$main
  21099. '']));
  21100. end;
  21101. procedure TTestModule.TestRTTI_Class_Property;
  21102. begin
  21103. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21104. StartProgram(false);
  21105. Add('{$modeswitch externalclass}');
  21106. Add('type');
  21107. Add(' TObject = class');
  21108. Add(' private');
  21109. Add(' FColor: longint;');
  21110. Add(' FColorStored: boolean;');
  21111. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  21112. Add(' function GetColor: longint; virtual; abstract;');
  21113. Add(' function GetColorStored: boolean; virtual; abstract;');
  21114. Add(' FExtSize: longint external name ''$extSize'';');
  21115. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  21116. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  21117. Add(' function GetExtSize: longint; external name ''$getSize'';');
  21118. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  21119. Add(' published');
  21120. Add(' property ColorA: longint read FColor;');
  21121. Add(' property ColorB: longint write FColor;');
  21122. Add(' property ColorC: longint read GetColor write SetColor;');
  21123. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  21124. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  21125. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  21126. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  21127. Add(' end;');
  21128. Add('begin');
  21129. ConvertProgram;
  21130. CheckSource('TestRTTI_Class_Property',
  21131. LinesToStr([ // statements
  21132. 'rtl.createClass($mod, "TObject", null, function () {',
  21133. ' this.$init = function () {',
  21134. ' this.FColor = 0;',
  21135. ' this.FColorStored = false;',
  21136. ' };',
  21137. ' this.$final = function () {',
  21138. ' };',
  21139. ' var $r = this.$rtti;',
  21140. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  21141. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  21142. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  21143. ' $r.addProperty(',
  21144. ' "ColorD",',
  21145. ' 8,',
  21146. ' rtl.longint,',
  21147. ' "FColor",',
  21148. ' "FColor",',
  21149. ' {',
  21150. ' stored: "FColorStored"',
  21151. ' }',
  21152. ' );',
  21153. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  21154. ' $r.addProperty(',
  21155. ' "ExtSizeB",',
  21156. ' 11,',
  21157. ' rtl.longint,',
  21158. ' "$getSize",',
  21159. ' "$setSize",',
  21160. ' {',
  21161. ' stored: "$extSizeStored"',
  21162. ' }',
  21163. ' );',
  21164. ' $r.addProperty(',
  21165. ' "ExtSizeC",',
  21166. ' 12,',
  21167. ' rtl.longint,',
  21168. ' "$extSize",',
  21169. ' "$extSize",',
  21170. ' {',
  21171. ' stored: "$getExtSizeStored"',
  21172. ' }',
  21173. ' );',
  21174. '});',
  21175. '']),
  21176. LinesToStr([ // $mod.$main
  21177. '']));
  21178. end;
  21179. procedure TTestModule.TestRTTI_Class_PropertyParams;
  21180. begin
  21181. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21182. StartProgram(false);
  21183. Add('{$modeswitch externalclass}');
  21184. Add('type');
  21185. Add(' integer = longint;');
  21186. Add(' TObject = class');
  21187. Add(' private');
  21188. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  21189. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  21190. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  21191. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  21192. Add(' published');
  21193. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  21194. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  21195. Add(' end;');
  21196. Add('begin');
  21197. ConvertProgram;
  21198. CheckSource('TestRTTI_Class_PropertyParams',
  21199. LinesToStr([ // statements
  21200. 'rtl.createClass($mod, "TObject", null, function () {',
  21201. ' this.$init = function () {',
  21202. ' };',
  21203. ' this.$final = function () {',
  21204. ' };',
  21205. ' var $r = this.$rtti;',
  21206. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  21207. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  21208. '});',
  21209. '']),
  21210. LinesToStr([ // $mod.$main
  21211. '']));
  21212. end;
  21213. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  21214. begin
  21215. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21216. AddModuleWithIntfImplSrc('unit1.pas',
  21217. 'type TColor = -5..5;',
  21218. '');
  21219. StartProgram(true);
  21220. Add([
  21221. 'uses unit1;',
  21222. 'type',
  21223. ' TColorAlias = TColor;',
  21224. ' TColorTypeAlias = type TColor;',
  21225. ' TObject = class',
  21226. ' private',
  21227. ' fColor: TColor;',
  21228. ' fAlias: TColorAlias;',
  21229. ' fTypeAlias: TColorTypeAlias;',
  21230. ' published',
  21231. ' property Color: TColor read fcolor;',
  21232. ' property Alias: TColorAlias read falias;',
  21233. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  21234. ' end;',
  21235. 'begin',
  21236. '']);
  21237. ConvertProgram;
  21238. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  21239. LinesToStr([ // statements
  21240. '$mod.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  21241. 'rtl.createClass($mod, "TObject", null, function () {',
  21242. ' this.$init = function () {',
  21243. ' this.fColor = 0;',
  21244. ' this.fAlias = 0;',
  21245. ' this.fTypeAlias = 0;',
  21246. ' };',
  21247. ' this.$final = function () {',
  21248. ' };',
  21249. ' var $r = this.$rtti;',
  21250. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  21251. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  21252. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  21253. '});',
  21254. '']),
  21255. LinesToStr([ // $mod.$main
  21256. '']));
  21257. end;
  21258. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  21259. begin
  21260. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21261. StartProgram(false);
  21262. Add([
  21263. '{$modeswitch omitrtti}',
  21264. 'type',
  21265. ' TObject = class',
  21266. ' private',
  21267. ' FA: byte;',
  21268. ' published',
  21269. ' property A: byte read FA write FA;',
  21270. ' end;',
  21271. 'begin']);
  21272. ConvertProgram;
  21273. CheckSource('TestRTTI_Class_OmitRTTI',
  21274. LinesToStr([ // statements
  21275. 'rtl.createClass($mod, "TObject", null, function () {',
  21276. ' this.$init = function () {',
  21277. ' this.FA = 0;',
  21278. ' };',
  21279. ' this.$final = function () {',
  21280. ' };',
  21281. '});',
  21282. '']),
  21283. LinesToStr([ // $mod.$main
  21284. '']));
  21285. end;
  21286. procedure TTestModule.TestRTTI_IndexModifier;
  21287. begin
  21288. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21289. StartProgram(false);
  21290. Add([
  21291. 'type',
  21292. ' TEnum = (red, blue);',
  21293. ' TObject = class',
  21294. ' FB: boolean;',
  21295. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  21296. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  21297. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  21298. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  21299. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  21300. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  21301. ' published',
  21302. ' property B1: boolean index 1 read FB write SetIntBool;',
  21303. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  21304. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  21305. ' end;',
  21306. 'begin']);
  21307. ConvertProgram;
  21308. CheckSource('TestRTTI_IndexModifier',
  21309. LinesToStr([ // statements
  21310. 'this.TEnum = {',
  21311. ' "0": "red",',
  21312. ' red: 0,',
  21313. ' "1": "blue",',
  21314. ' blue: 1',
  21315. '};',
  21316. '$mod.$rtti.$Enum("TEnum", {',
  21317. ' minvalue: 0,',
  21318. ' maxvalue: 1,',
  21319. ' ordtype: 1,',
  21320. ' enumtype: this.TEnum',
  21321. '});',
  21322. 'rtl.createClass($mod, "TObject", null, function () {',
  21323. ' this.$init = function () {',
  21324. ' this.FB = false;',
  21325. ' };',
  21326. ' this.$final = function () {',
  21327. ' };',
  21328. ' var $r = this.$rtti;',
  21329. ' $r.addProperty(',
  21330. ' "B1",',
  21331. ' 18,',
  21332. ' rtl.boolean,',
  21333. ' "FB",',
  21334. ' "SetIntBool",',
  21335. ' {',
  21336. ' index: 1',
  21337. ' }',
  21338. ' );',
  21339. ' $r.addProperty(',
  21340. ' "B2",',
  21341. ' 17,',
  21342. ' rtl.boolean,',
  21343. ' "GetEnumBool",',
  21344. ' "FB",',
  21345. ' {',
  21346. ' index: $mod.TEnum.blue',
  21347. ' }',
  21348. ' );',
  21349. ' $r.addProperty(',
  21350. ' "I1",',
  21351. ' 19,',
  21352. ' rtl.boolean,',
  21353. ' "GetStrIntBool",',
  21354. ' "SetStrIntBool",',
  21355. ' {',
  21356. ' index: 2',
  21357. ' }',
  21358. ' );',
  21359. '});',
  21360. '']),
  21361. LinesToStr([ // $mod.$main
  21362. '']));
  21363. end;
  21364. procedure TTestModule.TestRTTI_StoredModifier;
  21365. begin
  21366. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21367. StartProgram(false);
  21368. Add([
  21369. 'const',
  21370. ' ConstB = true;',
  21371. 'type',
  21372. ' TObject = class',
  21373. ' private',
  21374. ' FB: boolean;',
  21375. ' function IsBStored: boolean; virtual; abstract;',
  21376. ' published',
  21377. ' property BoolA: boolean read FB stored true;',
  21378. ' property BoolB: boolean read FB stored false;',
  21379. ' property BoolC: boolean read FB stored FB;',
  21380. ' property BoolD: boolean read FB stored ConstB;',
  21381. ' property BoolE: boolean read FB stored IsBStored;',
  21382. ' end;',
  21383. 'begin']);
  21384. ConvertProgram;
  21385. CheckSource('TestRTTI_StoredModifier',
  21386. LinesToStr([ // statements
  21387. 'this.ConstB = true;',
  21388. 'rtl.createClass($mod, "TObject", null, function () {',
  21389. ' this.$init = function () {',
  21390. ' this.FB = false;',
  21391. ' };',
  21392. ' this.$final = function () {',
  21393. ' };',
  21394. ' var $r = this.$rtti;',
  21395. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  21396. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  21397. ' $r.addProperty(',
  21398. ' "BoolC",',
  21399. ' 8,',
  21400. ' rtl.boolean,',
  21401. ' "FB",',
  21402. ' "",',
  21403. ' {',
  21404. ' stored: "FB"',
  21405. ' }',
  21406. ' );',
  21407. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  21408. ' $r.addProperty(',
  21409. ' "BoolE",',
  21410. ' 12,',
  21411. ' rtl.boolean,',
  21412. ' "FB",',
  21413. ' "",',
  21414. ' {',
  21415. ' stored: "IsBStored"',
  21416. ' }',
  21417. ' );',
  21418. '});',
  21419. '']),
  21420. LinesToStr([ // $mod.$main
  21421. '']));
  21422. end;
  21423. procedure TTestModule.TestRTTI_DefaultValue;
  21424. begin
  21425. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21426. StartProgram(false);
  21427. Add([
  21428. 'type',
  21429. ' TEnum = (red, blue);',
  21430. 'const',
  21431. ' CB = true or false;',
  21432. ' CI = 1+2;',
  21433. 'type',
  21434. ' TObject = class',
  21435. ' FB: boolean;',
  21436. ' FI: longint;',
  21437. ' FE: TEnum;',
  21438. ' published',
  21439. ' property B1: boolean read FB default true;',
  21440. ' property B2: boolean read FB default CB;',
  21441. ' property B3: boolean read FB default test1.cb;',
  21442. ' property I1: longint read FI default 2;',
  21443. ' property I2: longint read FI default CI;',
  21444. ' property E1: TEnum read FE default red;',
  21445. ' property E2: TEnum read FE default TEnum.blue;',
  21446. ' end;',
  21447. 'begin']);
  21448. ConvertProgram;
  21449. CheckSource('TestRTTI_DefaultValue',
  21450. LinesToStr([ // statements
  21451. 'this.TEnum = {',
  21452. ' "0": "red",',
  21453. ' red: 0,',
  21454. ' "1": "blue",',
  21455. ' blue: 1',
  21456. '};',
  21457. '$mod.$rtti.$Enum("TEnum", {',
  21458. ' minvalue: 0,',
  21459. ' maxvalue: 1,',
  21460. ' ordtype: 1,',
  21461. ' enumtype: this.TEnum',
  21462. '});',
  21463. 'this.CB = true || false;',
  21464. 'this.CI = 1 + 2;',
  21465. 'rtl.createClass($mod, "TObject", null, function () {',
  21466. ' this.$init = function () {',
  21467. ' this.FB = false;',
  21468. ' this.FI = 0;',
  21469. ' this.FE = 0;',
  21470. ' };',
  21471. ' this.$final = function () {',
  21472. ' };',
  21473. ' var $r = this.$rtti;',
  21474. ' $r.addProperty(',
  21475. ' "B1",',
  21476. ' 0,',
  21477. ' rtl.boolean,',
  21478. ' "FB",',
  21479. ' "",',
  21480. ' {',
  21481. ' Default: true',
  21482. ' }',
  21483. ' );',
  21484. ' $r.addProperty(',
  21485. ' "B2",',
  21486. ' 0,',
  21487. ' rtl.boolean,',
  21488. ' "FB",',
  21489. ' "",',
  21490. ' {',
  21491. ' Default: true',
  21492. ' }',
  21493. ' );',
  21494. ' $r.addProperty(',
  21495. ' "B3",',
  21496. ' 0,',
  21497. ' rtl.boolean,',
  21498. ' "FB",',
  21499. ' "",',
  21500. ' {',
  21501. ' Default: true',
  21502. ' }',
  21503. ' );',
  21504. ' $r.addProperty(',
  21505. ' "I1",',
  21506. ' 0,',
  21507. ' rtl.longint,',
  21508. ' "FI",',
  21509. ' "",',
  21510. ' {',
  21511. ' Default: 2',
  21512. ' }',
  21513. ' );',
  21514. ' $r.addProperty(',
  21515. ' "I2",',
  21516. ' 0,',
  21517. ' rtl.longint,',
  21518. ' "FI",',
  21519. ' "",',
  21520. ' {',
  21521. ' Default: 3',
  21522. ' }',
  21523. ' );',
  21524. ' $r.addProperty(',
  21525. ' "E1",',
  21526. ' 0,',
  21527. ' $mod.$rtti["TEnum"],',
  21528. ' "FE",',
  21529. ' "",',
  21530. ' {',
  21531. ' Default: $mod.TEnum.red',
  21532. ' }',
  21533. ' );',
  21534. ' $r.addProperty(',
  21535. ' "E2",',
  21536. ' 0,',
  21537. ' $mod.$rtti["TEnum"],',
  21538. ' "FE",',
  21539. ' "",',
  21540. ' {',
  21541. ' Default: $mod.TEnum.blue',
  21542. ' }',
  21543. ' );',
  21544. '});',
  21545. '']),
  21546. LinesToStr([ // $mod.$main
  21547. '']));
  21548. end;
  21549. procedure TTestModule.TestRTTI_DefaultValueSet;
  21550. begin
  21551. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21552. StartProgram(false);
  21553. Add([
  21554. 'type',
  21555. ' TEnum = (red, blue);',
  21556. ' TSet = set of TEnum;',
  21557. 'const',
  21558. ' CSet = [red,blue];',
  21559. 'type',
  21560. ' TObject = class',
  21561. ' FSet: TSet;',
  21562. ' published',
  21563. ' property Set1: TSet read FSet default [];',
  21564. ' property Set2: TSet read FSet default [red];',
  21565. ' property Set3: TSet read FSet default [red,blue];',
  21566. ' property Set4: TSet read FSet default CSet;',
  21567. ' end;',
  21568. 'begin']);
  21569. ConvertProgram;
  21570. CheckSource('TestRTTI_DefaultValueSet',
  21571. LinesToStr([ // statements
  21572. 'this.TEnum = {',
  21573. ' "0": "red",',
  21574. ' red: 0,',
  21575. ' "1": "blue",',
  21576. ' blue: 1',
  21577. '};',
  21578. '$mod.$rtti.$Enum("TEnum", {',
  21579. ' minvalue: 0,',
  21580. ' maxvalue: 1,',
  21581. ' ordtype: 1,',
  21582. ' enumtype: this.TEnum',
  21583. '});',
  21584. '$mod.$rtti.$Set("TSet", {',
  21585. ' comptype: $mod.$rtti["TEnum"]',
  21586. '});',
  21587. 'this.CSet = rtl.createSet($mod.TEnum.red, $mod.TEnum.blue);',
  21588. 'rtl.createClass($mod, "TObject", null, function () {',
  21589. ' this.$init = function () {',
  21590. ' this.FSet = {};',
  21591. ' };',
  21592. ' this.$final = function () {',
  21593. ' this.FSet = undefined;',
  21594. ' };',
  21595. ' var $r = this.$rtti;',
  21596. ' $r.addProperty(',
  21597. ' "Set1",',
  21598. ' 0,',
  21599. ' $mod.$rtti["TSet"],',
  21600. ' "FSet",',
  21601. ' "",',
  21602. ' {',
  21603. ' Default: {}',
  21604. ' }',
  21605. ' );',
  21606. ' $r.addProperty(',
  21607. ' "Set2",',
  21608. ' 0,',
  21609. ' $mod.$rtti["TSet"],',
  21610. ' "FSet",',
  21611. ' "",',
  21612. ' {',
  21613. ' Default: rtl.createSet($mod.TEnum.red)',
  21614. ' }',
  21615. ' );',
  21616. ' $r.addProperty(',
  21617. ' "Set3",',
  21618. ' 0,',
  21619. ' $mod.$rtti["TSet"],',
  21620. ' "FSet",',
  21621. ' "",',
  21622. ' {',
  21623. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  21624. ' }',
  21625. ' );',
  21626. ' $r.addProperty(',
  21627. ' "Set4",',
  21628. ' 0,',
  21629. ' $mod.$rtti["TSet"],',
  21630. ' "FSet",',
  21631. ' "",',
  21632. ' {',
  21633. ' Default: $mod.CSet',
  21634. ' }',
  21635. ' );',
  21636. '});',
  21637. '']),
  21638. LinesToStr([ // $mod.$main
  21639. '']));
  21640. end;
  21641. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  21642. begin
  21643. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21644. StartProgram(false);
  21645. Add([
  21646. 'type',
  21647. ' TRg = -1..1;',
  21648. 'const',
  21649. ' l = low(TRg);',
  21650. ' h = high(TRg);',
  21651. 'type',
  21652. ' TObject = class',
  21653. ' FV: TRg;',
  21654. ' published',
  21655. ' property V1: TRg read FV default -1;',
  21656. ' end;',
  21657. 'begin']);
  21658. ConvertProgram;
  21659. CheckSource('TestRTTI_DefaultValueRangeType',
  21660. LinesToStr([ // statements
  21661. '$mod.$rtti.$Int("TRg", {',
  21662. ' minvalue: -1,',
  21663. ' maxvalue: 1,',
  21664. ' ordtype: 0',
  21665. '});',
  21666. 'this.l = -1;',
  21667. 'this.h = 1;',
  21668. 'rtl.createClass($mod, "TObject", null, function () {',
  21669. ' this.$init = function () {',
  21670. ' this.FV = 0;',
  21671. ' };',
  21672. ' this.$final = function () {',
  21673. ' };',
  21674. ' var $r = this.$rtti;',
  21675. ' $r.addProperty(',
  21676. ' "V1",',
  21677. ' 0,',
  21678. ' $mod.$rtti["TRg"],',
  21679. ' "FV",',
  21680. ' "",',
  21681. ' {',
  21682. ' Default: -1',
  21683. ' }',
  21684. ' );',
  21685. '});',
  21686. '']),
  21687. LinesToStr([ // $mod.$main
  21688. '']));
  21689. end;
  21690. procedure TTestModule.TestRTTI_DefaultValueInherit;
  21691. begin
  21692. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21693. StartProgram(false);
  21694. Add([
  21695. 'type',
  21696. ' TObject = class',
  21697. ' FA, FB: byte;',
  21698. ' property A: byte read FA default 1;',
  21699. ' property B: byte read FB default 2;',
  21700. ' end;',
  21701. ' TBird = class',
  21702. ' published',
  21703. ' property A;',
  21704. ' property B nodefault;',
  21705. ' end;',
  21706. 'begin']);
  21707. ConvertProgram;
  21708. CheckSource('TestRTTI_DefaultValueInherit',
  21709. LinesToStr([ // statements
  21710. 'rtl.createClass($mod, "TObject", null, function () {',
  21711. ' this.$init = function () {',
  21712. ' this.FA = 0;',
  21713. ' this.FB = 0;',
  21714. ' };',
  21715. ' this.$final = function () {',
  21716. ' };',
  21717. '});',
  21718. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  21719. ' var $r = this.$rtti;',
  21720. ' $r.addProperty(',
  21721. ' "A",',
  21722. ' 0,',
  21723. ' rtl.byte,',
  21724. ' "FA",',
  21725. ' "",',
  21726. ' {',
  21727. ' Default: 1',
  21728. ' }',
  21729. ' );',
  21730. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  21731. '});',
  21732. '']),
  21733. LinesToStr([ // $mod.$main
  21734. '']));
  21735. end;
  21736. procedure TTestModule.TestRTTI_OverrideMethod;
  21737. begin
  21738. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21739. StartProgram(false);
  21740. Add('type');
  21741. Add(' TObject = class');
  21742. Add(' published');
  21743. Add(' procedure DoIt; virtual; abstract;');
  21744. Add(' end;');
  21745. Add(' TSky = class');
  21746. Add(' published');
  21747. Add(' procedure DoIt; override;');
  21748. Add(' end;');
  21749. Add('procedure TSky.DoIt; begin end;');
  21750. Add('begin');
  21751. ConvertProgram;
  21752. CheckSource('TestRTTI_OverrideMethod',
  21753. LinesToStr([ // statements
  21754. 'rtl.createClass($mod, "TObject", null, function () {',
  21755. ' this.$init = function () {',
  21756. ' };',
  21757. ' this.$final = function () {',
  21758. ' };',
  21759. ' var $r = this.$rtti;',
  21760. ' $r.addMethod("DoIt", 0, null);',
  21761. '});',
  21762. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  21763. ' this.DoIt = function () {',
  21764. ' };',
  21765. '});',
  21766. '']),
  21767. LinesToStr([ // $mod.$main
  21768. '']));
  21769. end;
  21770. procedure TTestModule.TestRTTI_OverloadProperty;
  21771. begin
  21772. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21773. StartProgram(false);
  21774. Add('type');
  21775. Add(' TObject = class');
  21776. Add(' protected');
  21777. Add(' FFlag: longint;');
  21778. Add(' published');
  21779. Add(' property Flag: longint read fflag;');
  21780. Add(' end;');
  21781. Add(' TSky = class');
  21782. Add(' published');
  21783. Add(' property FLAG: longint write fflag;');
  21784. Add(' end;');
  21785. Add('begin');
  21786. ConvertProgram;
  21787. CheckSource('TestRTTI_OverrideMethod',
  21788. LinesToStr([ // statements
  21789. 'rtl.createClass($mod, "TObject", null, function () {',
  21790. ' this.$init = function () {',
  21791. ' this.FFlag = 0;',
  21792. ' };',
  21793. ' this.$final = function () {',
  21794. ' };',
  21795. ' var $r = this.$rtti;',
  21796. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  21797. '});',
  21798. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  21799. ' var $r = this.$rtti;',
  21800. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  21801. '});',
  21802. '']),
  21803. LinesToStr([ // $mod.$main
  21804. '']));
  21805. end;
  21806. procedure TTestModule.TestRTTI_ClassForward;
  21807. begin
  21808. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21809. StartProgram(false);
  21810. Add('type');
  21811. Add(' TObject = class end;');
  21812. Add(' tbridge = class;');
  21813. Add(' TProc = function: tbridge;');
  21814. Add(' TOger = class');
  21815. Add(' published');
  21816. Add(' FBridge: tbridge;');
  21817. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  21818. Add(' property Bridge: tbridge read fbridge write setbridge;');
  21819. Add(' end;');
  21820. Add(' TBridge = class');
  21821. Add(' FOger: toger;');
  21822. Add(' end;');
  21823. Add('var p: Pointer;');
  21824. Add(' b: tbridge;');
  21825. Add('begin');
  21826. Add(' p:=typeinfo(tbridge);');
  21827. Add(' p:=typeinfo(b);');
  21828. ConvertProgram;
  21829. CheckSource('TestRTTI_ClassForward',
  21830. LinesToStr([ // statements
  21831. 'rtl.createClass($mod, "TObject", null, function () {',
  21832. ' this.$init = function () {',
  21833. ' };',
  21834. ' this.$final = function () {',
  21835. ' };',
  21836. '});',
  21837. '$mod.$rtti.$Class("TBridge");',
  21838. '$mod.$rtti.$ProcVar("TProc", {',
  21839. ' procsig: rtl.newTIProcSig(null, $mod.$rtti["TBridge"])',
  21840. '});',
  21841. 'rtl.createClass($mod, "TOger", $mod.TObject, function () {',
  21842. ' this.$init = function () {',
  21843. ' $mod.TObject.$init.call(this);',
  21844. ' this.FBridge = null;',
  21845. ' };',
  21846. ' this.$final = function () {',
  21847. ' this.FBridge = undefined;',
  21848. ' $mod.TObject.$final.call(this);',
  21849. ' };',
  21850. ' var $r = this.$rtti;',
  21851. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  21852. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  21853. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  21854. '});',
  21855. 'rtl.createClass($mod, "TBridge", $mod.TObject, function () {',
  21856. ' this.$init = function () {',
  21857. ' $mod.TObject.$init.call(this);',
  21858. ' this.FOger = null;',
  21859. ' };',
  21860. ' this.$final = function () {',
  21861. ' this.FOger = undefined;',
  21862. ' $mod.TObject.$final.call(this);',
  21863. ' };',
  21864. '});',
  21865. 'this.p = null;',
  21866. 'this.b = null;',
  21867. '']),
  21868. LinesToStr([ // $mod.$main
  21869. '$mod.p = $mod.$rtti["TBridge"];',
  21870. '$mod.p = $mod.b.$rtti;',
  21871. '']));
  21872. end;
  21873. procedure TTestModule.TestRTTI_ClassOf;
  21874. begin
  21875. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21876. StartProgram(false);
  21877. Add('type');
  21878. Add(' TClass = class of tobject;');
  21879. Add(' TProcA = function: TClass;');
  21880. Add(' TObject = class');
  21881. Add(' published');
  21882. Add(' C: tclass;');
  21883. Add(' end;');
  21884. Add(' tfox = class;');
  21885. Add(' TBird = class end;');
  21886. Add(' TBirds = class of tbird;');
  21887. Add(' TFox = class end;');
  21888. Add(' TFoxes = class of tfox;');
  21889. Add(' TCows = class of TCow;');
  21890. Add(' TCow = class;');
  21891. Add(' TCow = class end;');
  21892. Add('begin');
  21893. ConvertProgram;
  21894. CheckSource('TestRTTI_ClassOf',
  21895. LinesToStr([ // statements
  21896. '$mod.$rtti.$Class("TObject");',
  21897. '$mod.$rtti.$ClassRef("TClass", {',
  21898. ' instancetype: $mod.$rtti["TObject"]',
  21899. '});',
  21900. '$mod.$rtti.$ProcVar("TProcA", {',
  21901. ' procsig: rtl.newTIProcSig(null, $mod.$rtti["TClass"])',
  21902. '});',
  21903. 'rtl.createClass($mod, "TObject", null, function () {',
  21904. ' this.$init = function () {',
  21905. ' this.C = null;',
  21906. ' };',
  21907. ' this.$final = function () {',
  21908. ' this.C = undefined;',
  21909. ' };',
  21910. ' var $r = this.$rtti;',
  21911. ' $r.addField("C", $mod.$rtti["TClass"]);',
  21912. '});',
  21913. '$mod.$rtti.$Class("TFox");',
  21914. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  21915. '});',
  21916. '$mod.$rtti.$ClassRef("TBirds", {',
  21917. ' instancetype: $mod.$rtti["TBird"]',
  21918. '});',
  21919. 'rtl.createClass($mod, "TFox", $mod.TObject, function () {',
  21920. '});',
  21921. '$mod.$rtti.$ClassRef("TFoxes", {',
  21922. ' instancetype: $mod.$rtti["TFox"]',
  21923. '});',
  21924. '$mod.$rtti.$Class("TCow");',
  21925. '$mod.$rtti.$ClassRef("TCows", {',
  21926. ' instancetype: $mod.$rtti["TCow"]',
  21927. '});',
  21928. 'rtl.createClass($mod, "TCow", $mod.TObject, function () {',
  21929. '});',
  21930. '']),
  21931. LinesToStr([ // $mod.$main
  21932. '']));
  21933. end;
  21934. procedure TTestModule.TestRTTI_Record;
  21935. begin
  21936. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21937. StartProgram(false);
  21938. Add('type');
  21939. Add(' integer = longint;');
  21940. Add(' TPoint = record');
  21941. Add(' x,y: integer;');
  21942. Add(' end;');
  21943. Add('var p: pointer;');
  21944. Add(' r: tpoint;');
  21945. Add('begin');
  21946. Add(' p:=typeinfo(tpoint);');
  21947. Add(' p:=typeinfo(r);');
  21948. Add(' p:=typeinfo(r.x);');
  21949. ConvertProgram;
  21950. CheckSource('TestRTTI_Record',
  21951. LinesToStr([ // statements
  21952. 'rtl.recNewT($mod, "TPoint", function () {',
  21953. ' this.x = 0;',
  21954. ' this.y = 0;',
  21955. ' this.$eq = function (b) {',
  21956. ' return (this.x === b.x) && (this.y === b.y);',
  21957. ' };',
  21958. ' this.$assign = function (s) {',
  21959. ' this.x = s.x;',
  21960. ' this.y = s.y;',
  21961. ' return this;',
  21962. ' };',
  21963. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  21964. ' $r.addField("x", rtl.longint);',
  21965. ' $r.addField("y", rtl.longint);',
  21966. '});',
  21967. 'this.p = null;',
  21968. 'this.r = $mod.TPoint.$new();',
  21969. '']),
  21970. LinesToStr([ // $mod.$main
  21971. '$mod.p = $mod.$rtti["TPoint"];',
  21972. '$mod.p = $mod.$rtti["TPoint"];',
  21973. '$mod.p = rtl.longint;',
  21974. '']));
  21975. end;
  21976. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  21977. begin
  21978. Converter.Options:=Converter.Options-[coNoTypeInfo];
  21979. StartProgram(false);
  21980. Add('type');
  21981. Add(' TFloatRec = record');
  21982. Add(' d: array of char;');
  21983. // Add(' i: array of array of longint;');
  21984. Add(' end;');
  21985. Add('var p: pointer;');
  21986. Add(' r: tfloatrec;');
  21987. Add('begin');
  21988. Add(' p:=typeinfo(tfloatrec);');
  21989. Add(' p:=typeinfo(r);');
  21990. Add(' p:=typeinfo(r.d);');
  21991. ConvertProgram;
  21992. CheckSource('TestRTTI_Record',
  21993. LinesToStr([ // statements
  21994. 'rtl.recNewT($mod, "TFloatRec", function () {',
  21995. ' this.d = [];',
  21996. ' this.$eq = function (b) {',
  21997. ' return this.d === b.d;',
  21998. ' };',
  21999. ' this.$assign = function (s) {',
  22000. ' this.d = s.d;',
  22001. ' return this;',
  22002. ' };',
  22003. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  22004. ' eltype: rtl.char',
  22005. ' });',
  22006. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  22007. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  22008. '});',
  22009. 'this.p = null;',
  22010. 'this.r = $mod.TFloatRec.$new();',
  22011. '']),
  22012. LinesToStr([ // $mod.$main
  22013. '$mod.p = $mod.$rtti["TFloatRec"];',
  22014. '$mod.p = $mod.$rtti["TFloatRec"];',
  22015. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  22016. '']));
  22017. end;
  22018. procedure TTestModule.TestRTTI_LocalTypes;
  22019. begin
  22020. Converter.Options:=Converter.Options-[coNoTypeInfo];
  22021. StartProgram(false);
  22022. Add([
  22023. 'procedure DoIt;',
  22024. 'type',
  22025. ' integer = longint;',
  22026. ' TPoint = record',
  22027. ' x,y: integer;',
  22028. ' end;',
  22029. 'var p: TPoint;',
  22030. 'begin',
  22031. 'end;',
  22032. 'begin']);
  22033. ConvertProgram;
  22034. CheckSource('TestRTTI_LocalTypes',
  22035. LinesToStr([ // statements
  22036. 'var TPoint = rtl.recNewT(null, "", function () {',
  22037. ' this.x = 0;',
  22038. ' this.y = 0;',
  22039. ' this.$eq = function (b) {',
  22040. ' return (this.x === b.x) && (this.y === b.y);',
  22041. ' };',
  22042. ' this.$assign = function (s) {',
  22043. ' this.x = s.x;',
  22044. ' this.y = s.y;',
  22045. ' return this;',
  22046. ' };',
  22047. '});',
  22048. 'this.DoIt = function () {',
  22049. ' var p = TPoint.$new();',
  22050. '};',
  22051. '']),
  22052. LinesToStr([ // $mod.$main
  22053. '']));
  22054. end;
  22055. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  22056. begin
  22057. Converter.Options:=Converter.Options-[coNoTypeInfo];
  22058. StartProgram(false);
  22059. Add([
  22060. 'type',
  22061. ' TCaption = string;',
  22062. ' TYesNo = boolean;',
  22063. ' TLetter = char;',
  22064. ' TFloat = double;',
  22065. ' TPtr = pointer;',
  22066. ' TShortInt = shortint;',
  22067. ' TByte = byte;',
  22068. ' TSmallInt = smallint;',
  22069. ' TWord = word;',
  22070. ' TInt32 = longint;',
  22071. ' TDWord = longword;',
  22072. ' TValue = jsvalue;',
  22073. 'var p: TPtr;',
  22074. 'begin',
  22075. ' p:=typeinfo(string);',
  22076. ' p:=typeinfo(tcaption);',
  22077. ' p:=typeinfo(boolean);',
  22078. ' p:=typeinfo(tyesno);',
  22079. ' p:=typeinfo(char);',
  22080. ' p:=typeinfo(tletter);',
  22081. ' p:=typeinfo(double);',
  22082. ' p:=typeinfo(tfloat);',
  22083. ' p:=typeinfo(pointer);',
  22084. ' p:=typeinfo(tptr);',
  22085. ' p:=typeinfo(shortint);',
  22086. ' p:=typeinfo(tshortint);',
  22087. ' p:=typeinfo(byte);',
  22088. ' p:=typeinfo(tbyte);',
  22089. ' p:=typeinfo(smallint);',
  22090. ' p:=typeinfo(tsmallint);',
  22091. ' p:=typeinfo(word);',
  22092. ' p:=typeinfo(tword);',
  22093. ' p:=typeinfo(longword);',
  22094. ' p:=typeinfo(tdword);',
  22095. ' p:=typeinfo(jsvalue);',
  22096. ' p:=typeinfo(tvalue);',
  22097. '']);
  22098. ConvertProgram;
  22099. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  22100. LinesToStr([ // statements
  22101. 'this.p = null;',
  22102. '']),
  22103. LinesToStr([ // $mod.$main
  22104. '$mod.p = rtl.string;',
  22105. '$mod.p = rtl.string;',
  22106. '$mod.p = rtl.boolean;',
  22107. '$mod.p = rtl.boolean;',
  22108. '$mod.p = rtl.char;',
  22109. '$mod.p = rtl.char;',
  22110. '$mod.p = rtl.double;',
  22111. '$mod.p = rtl.double;',
  22112. '$mod.p = rtl.pointer;',
  22113. '$mod.p = rtl.pointer;',
  22114. '$mod.p = rtl.shortint;',
  22115. '$mod.p = rtl.shortint;',
  22116. '$mod.p = rtl.byte;',
  22117. '$mod.p = rtl.byte;',
  22118. '$mod.p = rtl.smallint;',
  22119. '$mod.p = rtl.smallint;',
  22120. '$mod.p = rtl.word;',
  22121. '$mod.p = rtl.word;',
  22122. '$mod.p = rtl.longword;',
  22123. '$mod.p = rtl.longword;',
  22124. '$mod.p = rtl.jsvalue;',
  22125. '$mod.p = rtl.jsvalue;',
  22126. '']));
  22127. end;
  22128. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  22129. begin
  22130. Converter.Options:=Converter.Options-[coNoTypeInfo];
  22131. StartProgram(false);
  22132. Add([
  22133. 'type',
  22134. ' TCaption = type string;',
  22135. ' TYesNo = type boolean;',
  22136. ' TLetter = type char;',
  22137. ' TFloat = type double;',
  22138. ' TPtr = type pointer;',
  22139. ' TShortInt = type shortint;',
  22140. ' TByte = type byte;',
  22141. ' TSmallInt = type smallint;',
  22142. ' TWord = type word;',
  22143. ' TInt32 = type longint;',
  22144. ' TDWord = type longword;',
  22145. ' TValue = type jsvalue;',
  22146. ' TAliasValue = type TValue;',
  22147. 'var',
  22148. ' p: TPtr;',
  22149. ' a: TAliasValue;',
  22150. 'begin',
  22151. ' p:=typeinfo(tcaption);',
  22152. ' p:=typeinfo(tyesno);',
  22153. ' p:=typeinfo(tletter);',
  22154. ' p:=typeinfo(tfloat);',
  22155. ' p:=typeinfo(tptr);',
  22156. ' p:=typeinfo(tshortint);',
  22157. ' p:=typeinfo(tbyte);',
  22158. ' p:=typeinfo(tsmallint);',
  22159. ' p:=typeinfo(tword);',
  22160. ' p:=typeinfo(tdword);',
  22161. ' p:=typeinfo(tvalue);',
  22162. ' p:=typeinfo(taliasvalue);',
  22163. ' p:=typeinfo(a);',
  22164. '']);
  22165. ConvertProgram;
  22166. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  22167. LinesToStr([ // statements
  22168. '$mod.$rtti.$inherited("TCaption", rtl.string, {});',
  22169. '$mod.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  22170. '$mod.$rtti.$inherited("TLetter", rtl.char, {});',
  22171. '$mod.$rtti.$inherited("TFloat", rtl.double, {});',
  22172. '$mod.$rtti.$inherited("TPtr", rtl.pointer, {});',
  22173. '$mod.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  22174. '$mod.$rtti.$inherited("TByte", rtl.byte, {});',
  22175. '$mod.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  22176. '$mod.$rtti.$inherited("TWord", rtl.word, {});',
  22177. '$mod.$rtti.$inherited("TInt32", rtl.longint, {});',
  22178. '$mod.$rtti.$inherited("TDWord", rtl.longword, {});',
  22179. '$mod.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  22180. '$mod.$rtti.$inherited("TAliasValue", $mod.$rtti["TValue"], {});',
  22181. 'this.p = null;',
  22182. 'this.a = undefined;',
  22183. '']),
  22184. LinesToStr([ // $mod.$main
  22185. '$mod.p = $mod.$rtti["TCaption"];',
  22186. '$mod.p = $mod.$rtti["TYesNo"];',
  22187. '$mod.p = $mod.$rtti["TLetter"];',
  22188. '$mod.p = $mod.$rtti["TFloat"];',
  22189. '$mod.p = $mod.$rtti["TPtr"];',
  22190. '$mod.p = $mod.$rtti["TShortInt"];',
  22191. '$mod.p = $mod.$rtti["TByte"];',
  22192. '$mod.p = $mod.$rtti["TSmallInt"];',
  22193. '$mod.p = $mod.$rtti["TWord"];',
  22194. '$mod.p = $mod.$rtti["TDWord"];',
  22195. '$mod.p = $mod.$rtti["TValue"];',
  22196. '$mod.p = $mod.$rtti["TAliasValue"];',
  22197. '$mod.p = $mod.$rtti["TAliasValue"];',
  22198. '']));
  22199. end;
  22200. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  22201. begin
  22202. Converter.Options:=Converter.Options-[coNoTypeInfo];
  22203. StartProgram(false);
  22204. Add('procedure DoIt;');
  22205. Add('type');
  22206. Add(' integer = longint;');
  22207. Add(' TPoint = record');
  22208. Add(' x,y: integer;');
  22209. Add(' end;');
  22210. Add('var p: pointer;');
  22211. Add('begin');
  22212. Add(' p:=typeinfo(tpoint);');
  22213. Add('end;');
  22214. Add('begin');
  22215. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  22216. ConvertProgram;
  22217. end;
  22218. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  22219. begin
  22220. Converter.Options:=Converter.Options-[coNoTypeInfo];
  22221. StartProgram(false);
  22222. Add([
  22223. '{$modeswitch externalclass}',
  22224. 'type',
  22225. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  22226. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  22227. ' TFlag = (up,down);',
  22228. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  22229. ' TFlags = set of TFlag;',
  22230. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  22231. 'var',
  22232. ' ti: TTypeInfo;',
  22233. ' tiInt: TTypeInfoInteger;',
  22234. ' tiEnum: TTypeInfoEnum;',
  22235. ' tiSet: TTypeInfoSet;',
  22236. 'begin',
  22237. ' ti:=typeinfo(string);',
  22238. ' ti:=typeinfo(boolean);',
  22239. ' ti:=typeinfo(char);',
  22240. ' ti:=typeinfo(double);',
  22241. ' tiInt:=typeinfo(shortint);',
  22242. ' tiInt:=typeinfo(byte);',
  22243. ' tiInt:=typeinfo(smallint);',
  22244. ' tiInt:=typeinfo(word);',
  22245. ' tiInt:=typeinfo(longint);',
  22246. ' tiInt:=typeinfo(longword);',
  22247. ' ti:=typeinfo(jsvalue);',
  22248. ' tiEnum:=typeinfo(tflag);',
  22249. ' tiSet:=typeinfo(tflags);']);
  22250. ConvertProgram;
  22251. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  22252. LinesToStr([ // statements
  22253. 'this.TFlag = {',
  22254. ' "0": "up",',
  22255. ' up: 0,',
  22256. ' "1": "down",',
  22257. ' down: 1',
  22258. '};',
  22259. '$mod.$rtti.$Enum("TFlag", {',
  22260. ' minvalue: 0,',
  22261. ' maxvalue: 1,',
  22262. ' ordtype: 1,',
  22263. ' enumtype: this.TFlag',
  22264. '});',
  22265. '$mod.$rtti.$Set("TFlags", {',
  22266. ' comptype: $mod.$rtti["TFlag"]',
  22267. '});',
  22268. 'this.ti = null;',
  22269. 'this.tiInt = null;',
  22270. 'this.tiEnum = null;',
  22271. 'this.tiSet = null;',
  22272. '']),
  22273. LinesToStr([ // $mod.$main
  22274. '$mod.ti = rtl.string;',
  22275. '$mod.ti = rtl.boolean;',
  22276. '$mod.ti = rtl.char;',
  22277. '$mod.ti = rtl.double;',
  22278. '$mod.tiInt = rtl.shortint;',
  22279. '$mod.tiInt = rtl.byte;',
  22280. '$mod.tiInt = rtl.smallint;',
  22281. '$mod.tiInt = rtl.word;',
  22282. '$mod.tiInt = rtl.longint;',
  22283. '$mod.tiInt = rtl.longword;',
  22284. '$mod.ti = rtl.jsvalue;',
  22285. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  22286. '$mod.tiSet = $mod.$rtti["TFlags"];',
  22287. '']));
  22288. end;
  22289. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  22290. begin
  22291. Converter.Options:=Converter.Options-[coNoTypeInfo];
  22292. StartProgram(false);
  22293. Add('{$modeswitch externalclass}');
  22294. Add('type');
  22295. Add(' TTypeInfo = class external name ''rtl.tTypeInfo'' end;');
  22296. Add(' TStaticArr = array[boolean] of string;');
  22297. Add(' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;');
  22298. Add(' TDynArr = array of string;');
  22299. Add(' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;');
  22300. Add(' TProc = procedure;');
  22301. Add(' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;');
  22302. Add(' TMethod = procedure of object;');
  22303. Add(' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;');
  22304. Add('var');
  22305. Add(' StaticArray: TStaticArr;');
  22306. Add(' tiStaticArray: TTypeInfoStaticArray;');
  22307. Add(' DynArray: TDynArr;');
  22308. Add(' tiDynArray: TTypeInfoDynArray;');
  22309. Add(' ProcVar: TProc;');
  22310. Add(' tiProcVar: TTypeInfoProcVar;');
  22311. Add(' MethodVar: TMethod;');
  22312. Add(' tiMethodVar: TTypeInfoMethodVar;');
  22313. Add('begin');
  22314. Add(' tiStaticArray:=typeinfo(StaticArray);');
  22315. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  22316. Add(' tiDynArray:=typeinfo(DynArray);');
  22317. Add(' tiDynArray:=typeinfo(TDynArr);');
  22318. Add(' tiProcVar:=typeinfo(ProcVar);');
  22319. Add(' tiProcVar:=typeinfo(TProc);');
  22320. Add(' tiMethodVar:=typeinfo(MethodVar);');
  22321. Add(' tiMethodVar:=typeinfo(TMethod);');
  22322. ConvertProgram;
  22323. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  22324. LinesToStr([ // statements
  22325. ' $mod.$rtti.$StaticArray("TStaticArr", {',
  22326. ' dims: [2],',
  22327. ' eltype: rtl.string',
  22328. '});',
  22329. '$mod.$rtti.$DynArray("TDynArr", {',
  22330. ' eltype: rtl.string',
  22331. '});',
  22332. '$mod.$rtti.$ProcVar("TProc", {',
  22333. ' procsig: rtl.newTIProcSig(null)',
  22334. '});',
  22335. '$mod.$rtti.$MethodVar("TMethod", {',
  22336. ' procsig: rtl.newTIProcSig(null),',
  22337. ' methodkind: 0',
  22338. '});',
  22339. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  22340. 'this.tiStaticArray = null;',
  22341. 'this.DynArray = [];',
  22342. 'this.tiDynArray = null;',
  22343. 'this.ProcVar = null;',
  22344. 'this.tiProcVar = null;',
  22345. 'this.MethodVar = null;',
  22346. 'this.tiMethodVar = null;',
  22347. '']),
  22348. LinesToStr([ // $mod.$main
  22349. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  22350. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  22351. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  22352. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  22353. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  22354. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  22355. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  22356. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  22357. '']));
  22358. end;
  22359. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  22360. begin
  22361. Converter.Options:=Converter.Options-[coNoTypeInfo];
  22362. StartProgram(false);
  22363. Add('{$modeswitch externalclass}');
  22364. Add('type');
  22365. Add(' TTypeInfo = class external name ''rtl.tTypeInfo'' end;');
  22366. Add(' TRec = record end;');
  22367. Add(' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;');
  22368. // ToDo: ^PRec
  22369. Add(' TObject = class end;');
  22370. Add(' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;');
  22371. Add(' TClass = class of tobject;');
  22372. Add(' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;');
  22373. Add(' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;');
  22374. Add('var');
  22375. Add(' Rec: trec;');
  22376. Add(' tiRecord: ttypeinforecord;');
  22377. Add(' Obj: tobject;');
  22378. Add(' tiClass: ttypeinfoclass;');
  22379. Add(' aClass: tclass;');
  22380. Add(' tiClassRef: ttypeinfoclassref;');
  22381. // ToDo: ^PRec
  22382. Add(' tiPointer: ttypeinfopointer;');
  22383. Add('begin');
  22384. Add(' tirecord:=typeinfo(trec);');
  22385. Add(' tirecord:=typeinfo(trec);');
  22386. Add(' ticlass:=typeinfo(obj);');
  22387. Add(' ticlass:=typeinfo(tobject);');
  22388. Add(' ticlass:=typeinfo(aclass);');
  22389. Add(' ticlassref:=typeinfo(tclass);');
  22390. ConvertProgram;
  22391. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  22392. LinesToStr([ // statements
  22393. 'rtl.recNewT($mod, "TRec", function () {',
  22394. ' this.$eq = function (b) {',
  22395. ' return true;',
  22396. ' };',
  22397. ' this.$assign = function (s) {',
  22398. ' return this;',
  22399. ' };',
  22400. ' $mod.$rtti.$Record("TRec", {});',
  22401. '});',
  22402. 'rtl.createClass($mod, "TObject", null, function () {',
  22403. ' this.$init = function () {',
  22404. ' };',
  22405. ' this.$final = function () {',
  22406. ' };',
  22407. '});',
  22408. '$mod.$rtti.$ClassRef("TClass", {',
  22409. ' instancetype: $mod.$rtti["TObject"]',
  22410. '});',
  22411. 'this.Rec = $mod.TRec.$new();',
  22412. 'this.tiRecord = null;',
  22413. 'this.Obj = null;',
  22414. 'this.tiClass = null;',
  22415. 'this.aClass = null;',
  22416. 'this.tiClassRef = null;',
  22417. 'this.tiPointer = null;',
  22418. '']),
  22419. LinesToStr([ // $mod.$main
  22420. '$mod.tiRecord = $mod.$rtti["TRec"];',
  22421. '$mod.tiRecord = $mod.$rtti["TRec"];',
  22422. '$mod.tiClass = $mod.Obj.$rtti;',
  22423. '$mod.tiClass = $mod.$rtti["TObject"];',
  22424. '$mod.tiClass = $mod.aClass.$rtti;',
  22425. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  22426. '']));
  22427. end;
  22428. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  22429. begin
  22430. Converter.Options:=Converter.Options-[coNoTypeInfo];
  22431. StartProgram(false);
  22432. Add([
  22433. '{$modeswitch externalclass}',
  22434. 'type',
  22435. ' TClass = class of tobject;',
  22436. ' TObject = class',
  22437. ' function MyClass: TClass;',
  22438. ' class function ClassType: TClass;',
  22439. ' end;',
  22440. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  22441. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  22442. 'function TObject.MyClass: TClass;',
  22443. 'var t: TTypeInfoClass;',
  22444. 'begin',
  22445. ' t:=TypeInfo(Self);',
  22446. ' t:=TypeInfo(Result);',
  22447. ' t:=TypeInfo(TObject);',
  22448. 'end;',
  22449. 'class function TObject.ClassType: TClass;',
  22450. 'var t: TTypeInfoClass;',
  22451. 'begin',
  22452. ' t:=TypeInfo(Self);',
  22453. ' t:=TypeInfo(Result);',
  22454. 'end;',
  22455. 'var',
  22456. ' Obj: TObject;',
  22457. ' t: TTypeInfoClass;',
  22458. 'begin',
  22459. ' t:=TypeInfo(TObject.ClassType);',
  22460. ' t:=TypeInfo(Obj.ClassType);',
  22461. ' t:=TypeInfo(Obj.MyClass);',
  22462. '']);
  22463. ConvertProgram;
  22464. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  22465. LinesToStr([ // statements
  22466. '$mod.$rtti.$Class("TObject");',
  22467. '$mod.$rtti.$ClassRef("TClass", {',
  22468. ' instancetype: $mod.$rtti["TObject"]',
  22469. '});',
  22470. 'rtl.createClass($mod, "TObject", null, function () {',
  22471. ' this.$init = function () {',
  22472. ' };',
  22473. ' this.$final = function () {',
  22474. ' };',
  22475. ' this.MyClass = function () {',
  22476. ' var Result = null;',
  22477. ' var t = null;',
  22478. ' t = this.$rtti;',
  22479. ' t = Result.$rtti;',
  22480. ' t = $mod.$rtti["TObject"];',
  22481. ' return Result;',
  22482. ' };',
  22483. ' this.ClassType = function () {',
  22484. ' var Result = null;',
  22485. ' var t = null;',
  22486. ' t = this.$rtti;',
  22487. ' t = Result.$rtti;',
  22488. ' return Result;',
  22489. ' };',
  22490. '});',
  22491. 'this.Obj = null;',
  22492. 'this.t = null;',
  22493. '']),
  22494. LinesToStr([ // $mod.$main
  22495. '$mod.t = $mod.TObject.ClassType().$rtti;',
  22496. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  22497. '$mod.t = $mod.Obj.MyClass().$rtti;',
  22498. '']));
  22499. end;
  22500. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  22501. begin
  22502. Converter.Options:=Converter.Options-[coNoTypeInfo];
  22503. AddModuleWithIntfImplSrc('typinfo.pas',
  22504. LinesToStr([
  22505. '{$modeswitch externalclass}',
  22506. 'type',
  22507. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  22508. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  22509. '']),
  22510. '');
  22511. AddModuleWithIntfImplSrc('unit2.pas',
  22512. LinesToStr([
  22513. 'uses typinfo;',
  22514. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  22515. 'procedure DoPtr(p: PTypeInfo);',
  22516. 'procedure DoInfo(t: TTypeInfo);',
  22517. 'procedure DoInt(t: TTypeInfoInteger);',
  22518. '']),
  22519. LinesToStr([
  22520. 'procedure DoPtr(p: PTypeInfo);',
  22521. 'begin end;',
  22522. 'procedure DoInfo(t: TTypeInfo);',
  22523. 'begin end;',
  22524. 'procedure DoInt(t: TTypeInfoInteger);',
  22525. 'begin end;',
  22526. '']));
  22527. StartUnit(true);
  22528. Add([
  22529. 'interface',
  22530. 'uses unit2;', // does not use unit typinfo
  22531. 'implementation',
  22532. 'var',
  22533. ' i: byte;',
  22534. ' p: pointer;',
  22535. ' t: PTypeInfo;',
  22536. 'initialization',
  22537. ' p:=typeinfo(i);',
  22538. ' t:=typeinfo(i);',
  22539. ' if p=t then ;',
  22540. ' if p=typeinfo(i) then ;',
  22541. ' if typeinfo(i)=p then ;',
  22542. ' if t=typeinfo(i) then ;',
  22543. ' if typeinfo(i)=t then ;',
  22544. ' DoPtr(p);',
  22545. ' DoPtr(t);',
  22546. ' DoPtr(typeinfo(i));',
  22547. ' DoInfo(p);',
  22548. ' DoInfo(t);',
  22549. ' DoInfo(typeinfo(i));',
  22550. ' DoInt(typeinfo(i));',
  22551. '']);
  22552. ConvertUnit;
  22553. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  22554. LinesToStr([ // statements
  22555. 'var $impl = $mod.$impl;',
  22556. '']),
  22557. LinesToStr([ // this.$init
  22558. '$impl.p = rtl.byte;',
  22559. '$impl.t = rtl.byte;',
  22560. 'if ($impl.p === $impl.t) ;',
  22561. 'if ($impl.p === rtl.byte) ;',
  22562. 'if (rtl.byte === $impl.p) ;',
  22563. 'if ($impl.t === rtl.byte) ;',
  22564. 'if (rtl.byte === $impl.t) ;',
  22565. 'pas.unit2.DoPtr($impl.p);',
  22566. 'pas.unit2.DoPtr($impl.t);',
  22567. 'pas.unit2.DoPtr(rtl.byte);',
  22568. 'pas.unit2.DoInfo($impl.p);',
  22569. 'pas.unit2.DoInfo($impl.t);',
  22570. 'pas.unit2.DoInfo(rtl.byte);',
  22571. 'pas.unit2.DoInt(rtl.byte);',
  22572. '']),
  22573. LinesToStr([ // implementation
  22574. '$impl.i = 0;',
  22575. '$impl.p = null;',
  22576. '$impl.t = null;',
  22577. '']) );
  22578. end;
  22579. procedure TTestModule.TestRTTI_Interface_Corba;
  22580. begin
  22581. Converter.Options:=Converter.Options-[coNoTypeInfo];
  22582. StartProgram(false);
  22583. Add([
  22584. '{$interfaces corba}',
  22585. '{$modeswitch externalclass}',
  22586. 'type',
  22587. ' IUnknown = interface',
  22588. ' end;',
  22589. ' IBird = interface',
  22590. ' function GetItem: longint;',
  22591. ' procedure SetItem(Value: longint);',
  22592. ' property Item: longint read GetItem write SetItem;',
  22593. ' end;',
  22594. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  22595. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  22596. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  22597. 'var',
  22598. ' i: IBird;',
  22599. ' t: TTypeInfoInterface;',
  22600. 'begin',
  22601. ' t:=TypeInfo(IBird);',
  22602. ' t:=TypeInfo(i);',
  22603. ' DoIt(t);',
  22604. ' DoIt(TypeInfo(IBird));',
  22605. '']);
  22606. ConvertProgram;
  22607. CheckSource('TestRTTI_Interface_Corba',
  22608. LinesToStr([ // statements
  22609. 'rtl.createInterface(',
  22610. ' $mod,',
  22611. ' "IUnknown",',
  22612. ' "{B92D5841-758A-322B-B800-000000000000}",',
  22613. ' [],',
  22614. ' null,',
  22615. ' function () {',
  22616. ' }',
  22617. ');',
  22618. 'rtl.createInterface(',
  22619. ' $mod,',
  22620. ' "IBird",',
  22621. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  22622. ' ["GetItem", "SetItem"],',
  22623. ' null,',
  22624. ' function () {',
  22625. ' var $r = this.$rtti;',
  22626. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  22627. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  22628. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  22629. ' }',
  22630. ');',
  22631. 'this.DoIt = function (t) {',
  22632. '}; ',
  22633. 'this.i = null;',
  22634. 'this.t = null;',
  22635. '']),
  22636. LinesToStr([ // $mod.$main
  22637. '$mod.t = $mod.$rtti["IBird"];',
  22638. '$mod.t = $mod.i.$rtti;',
  22639. '$mod.DoIt($mod.t);',
  22640. '$mod.DoIt($mod.$rtti["IBird"]);',
  22641. '']));
  22642. end;
  22643. procedure TTestModule.TestRTTI_Interface_COM;
  22644. begin
  22645. Converter.Options:=Converter.Options-[coNoTypeInfo];
  22646. StartProgram(false);
  22647. Add([
  22648. '{$interfaces com}',
  22649. '{$modeswitch externalclass}',
  22650. 'type',
  22651. ' TGuid = record end;',
  22652. ' integer = longint;',
  22653. ' IUnknown = interface',
  22654. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  22655. ' function _AddRef: Integer;',
  22656. ' function _Release: Integer;',
  22657. ' end;',
  22658. ' IBird = interface',
  22659. ' function GetItem: longint;',
  22660. ' procedure SetItem(Value: longint);',
  22661. ' property Item: longint read GetItem write SetItem;',
  22662. ' end;',
  22663. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  22664. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  22665. 'var',
  22666. ' i: IBird;',
  22667. ' t: TTypeInfoInterface;',
  22668. 'begin',
  22669. ' t:=TypeInfo(IBird);',
  22670. ' t:=TypeInfo(i);',
  22671. '']);
  22672. ConvertProgram;
  22673. CheckSource('TestRTTI_Interface_COM',
  22674. LinesToStr([ // statements
  22675. 'rtl.recNewT($mod, "TGuid", function () {',
  22676. ' this.$eq = function (b) {',
  22677. ' return true;',
  22678. ' };',
  22679. ' this.$assign = function (s) {',
  22680. ' return this;',
  22681. ' };',
  22682. ' $mod.$rtti.$Record("TGuid", {});',
  22683. '});',
  22684. 'rtl.createInterface(',
  22685. ' $mod,',
  22686. ' "IUnknown",',
  22687. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  22688. ' ["QueryInterface", "_AddRef", "_Release"],',
  22689. ' null,',
  22690. ' function () {',
  22691. ' this.$kind = "com";',
  22692. ' var $r = this.$rtti;',
  22693. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  22694. ' $r.addMethod("_AddRef", 1, null, rtl.longint);',
  22695. ' $r.addMethod("_Release", 1, null, rtl.longint);',
  22696. ' }',
  22697. ');',
  22698. 'rtl.createInterface(',
  22699. ' $mod,',
  22700. ' "IBird",',
  22701. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  22702. ' ["GetItem", "SetItem"],',
  22703. ' $mod.IUnknown,',
  22704. ' function () {',
  22705. ' var $r = this.$rtti;',
  22706. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  22707. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  22708. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  22709. ' }',
  22710. ');',
  22711. 'this.i = null;',
  22712. 'this.t = null;',
  22713. '']),
  22714. LinesToStr([ // $mod.$main
  22715. '$mod.t = $mod.$rtti["IBird"];',
  22716. '$mod.t = $mod.i.$rtti;',
  22717. '']));
  22718. end;
  22719. procedure TTestModule.TestResourcestringProgram;
  22720. begin
  22721. StartProgram(false);
  22722. Add([
  22723. 'const Bar = ''bar'';',
  22724. 'resourcestring',
  22725. ' Red = ''red'';',
  22726. ' Foobar = ''fOo''+bar;',
  22727. 'var s: string;',
  22728. ' c: char;',
  22729. 'begin',
  22730. ' s:=red;',
  22731. ' s:=test1.red;',
  22732. ' c:=red[1];',
  22733. ' c:=test1.red[2];',
  22734. ' if red=foobar then ;',
  22735. ' if red[3]=red[4] then ;']);
  22736. ConvertProgram;
  22737. CheckSource('TestResourcestringProgram',
  22738. LinesToStr([ // statements
  22739. 'this.Bar = "bar";',
  22740. 'this.s = "";',
  22741. 'this.c = "";',
  22742. '$mod.$resourcestrings = {',
  22743. ' Red: {',
  22744. ' org: "red"',
  22745. ' },',
  22746. ' Foobar: {',
  22747. ' org: "fOobar"',
  22748. ' }',
  22749. '};',
  22750. '']),
  22751. LinesToStr([ // $mod.$main
  22752. '$mod.s = rtl.getResStr(pas.program, "Red");',
  22753. '$mod.s = rtl.getResStr(pas.program, "Red");',
  22754. '$mod.c = rtl.getResStr(pas.program, "Red").charAt(0);',
  22755. '$mod.c = rtl.getResStr(pas.program, "Red").charAt(1);',
  22756. 'if (rtl.getResStr(pas.program, "Red") === rtl.getResStr(pas.program, "Foobar")) ;',
  22757. 'if (rtl.getResStr(pas.program, "Red").charAt(2) === rtl.getResStr(pas.program, "Red").charAt(3)) ;',
  22758. '']));
  22759. end;
  22760. procedure TTestModule.TestResourcestringUnit;
  22761. begin
  22762. StartUnit(false);
  22763. Add([
  22764. 'interface',
  22765. 'const Red = ''rEd'';',
  22766. 'resourcestring',
  22767. ' Blue = ''blue'';',
  22768. ' NotRed = ''not''+Red;',
  22769. 'var s: string;',
  22770. 'implementation',
  22771. 'resourcestring',
  22772. ' ImplGreen = ''green'';',
  22773. 'initialization',
  22774. ' s:=blue+ImplGreen;',
  22775. ' s:=test1.blue+test1.implgreen;',
  22776. ' s:=blue[1]+implgreen[2];']);
  22777. ConvertUnit;
  22778. CheckSource('TestResourcestringUnit',
  22779. LinesToStr([ // statements
  22780. 'this.Red = "rEd";',
  22781. 'this.s = "";',
  22782. '$mod.$resourcestrings = {',
  22783. ' Blue: {',
  22784. ' org: "blue"',
  22785. ' },',
  22786. ' NotRed: {',
  22787. ' org: "notrEd"',
  22788. ' },',
  22789. ' ImplGreen: {',
  22790. ' org: "green"',
  22791. ' }',
  22792. '};',
  22793. '']),
  22794. LinesToStr([ // $mod.$main
  22795. '$mod.s = rtl.getResStr(pas.Test1, "Blue") + rtl.getResStr(pas.Test1, "ImplGreen");',
  22796. '$mod.s = rtl.getResStr(pas.Test1, "Blue") + rtl.getResStr(pas.Test1, "ImplGreen");',
  22797. '$mod.s = rtl.getResStr(pas.Test1, "Blue").charAt(0) + rtl.getResStr(pas.Test1, "ImplGreen").charAt(1);',
  22798. '']));
  22799. end;
  22800. procedure TTestModule.TestResourcestringImplementation;
  22801. begin
  22802. StartUnit(false);
  22803. Add([
  22804. 'interface',
  22805. 'implementation',
  22806. 'resourcestring',
  22807. ' ImplRed = ''red'';']);
  22808. ConvertUnit;
  22809. CheckSource('TestResourcestringImplementation',
  22810. LinesToStr([ // intf statements
  22811. 'var $impl = $mod.$impl;']),
  22812. LinesToStr([ // $mod.$init
  22813. '']),
  22814. LinesToStr([ // impl statements
  22815. '$mod.$resourcestrings = {',
  22816. ' ImplRed: {',
  22817. ' org: "red"',
  22818. ' }',
  22819. '};',
  22820. '']));
  22821. end;
  22822. procedure TTestModule.TestAtributes_Ignore;
  22823. begin
  22824. StartProgram(false);
  22825. Add([
  22826. '{$modeswitch ignoreattributes}',
  22827. 'type',
  22828. ' [custom1, custom2(1+3,''foo'')] [mod1.custom3]',
  22829. ' TObject = class',
  22830. ' [custom5()] FS: string;',
  22831. ' [customProp] property S: string read FS;',
  22832. ' end;',
  22833. 'var',
  22834. ' [custom6]',
  22835. ' o: TObject;',
  22836. 'begin',
  22837. '']);
  22838. ConvertProgram;
  22839. CheckSource('TestAtributes_Ignore',
  22840. LinesToStr([ // statements
  22841. 'rtl.createClass($mod, "TObject", null, function () {',
  22842. ' this.$init = function () {',
  22843. ' this.FS = "";',
  22844. ' };',
  22845. ' this.$final = function () {',
  22846. ' };',
  22847. '});',
  22848. 'this.o = null;',
  22849. '']),
  22850. LinesToStr([ // $mod.$main
  22851. '']));
  22852. end;
  22853. procedure TTestModule.TestAssert;
  22854. begin
  22855. StartProgram(false);
  22856. Add([
  22857. 'procedure DoIt;',
  22858. 'var',
  22859. ' b: boolean;',
  22860. ' s: string;',
  22861. 'begin',
  22862. ' {$Assertions on}',
  22863. ' Assert(b);',
  22864. 'end;',
  22865. 'begin',
  22866. ' DoIt;',
  22867. '']);
  22868. ConvertProgram;
  22869. CheckSource('TestAssert',
  22870. LinesToStr([ // statements
  22871. 'this.DoIt = function () {',
  22872. ' var b = false;',
  22873. ' var s = "";',
  22874. ' if (!b) throw "assert failed";',
  22875. '};',
  22876. '']),
  22877. LinesToStr([ // $mod.$main
  22878. '$mod.DoIt();',
  22879. '']));
  22880. end;
  22881. procedure TTestModule.TestAssert_SysUtils;
  22882. begin
  22883. AddModuleWithIntfImplSrc('SysUtils.pas',
  22884. LinesToStr([
  22885. 'type',
  22886. ' TObject = class',
  22887. ' constructor Create;',
  22888. ' end;',
  22889. ' EAssertionFailed = class',
  22890. ' constructor Create(s: string);',
  22891. ' end;',
  22892. '']),
  22893. LinesToStr([
  22894. 'constructor TObject.Create;',
  22895. 'begin end;',
  22896. 'constructor EAssertionFailed.Create(s: string);',
  22897. 'begin end;',
  22898. '']) );
  22899. StartProgram(true);
  22900. Add([
  22901. 'uses sysutils;',
  22902. 'procedure DoIt;',
  22903. 'var',
  22904. ' b: boolean;',
  22905. ' s: string;',
  22906. 'begin',
  22907. ' {$Assertions on}',
  22908. ' Assert(b);',
  22909. ' Assert(b,''msg'');',
  22910. 'end;',
  22911. 'begin',
  22912. ' DoIt;',
  22913. '']);
  22914. ConvertProgram;
  22915. CheckSource('TestAssert_SysUtils',
  22916. LinesToStr([ // statements
  22917. 'this.DoIt = function () {',
  22918. ' var b = false;',
  22919. ' var s = "";',
  22920. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  22921. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  22922. '};',
  22923. '']),
  22924. LinesToStr([ // $mod.$main
  22925. '$mod.DoIt();',
  22926. '']));
  22927. end;
  22928. procedure TTestModule.TestObjectChecks;
  22929. begin
  22930. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  22931. StartProgram(false);
  22932. Add([
  22933. 'type',
  22934. ' TObject = class',
  22935. ' procedure DoIt;',
  22936. ' end;',
  22937. ' TClass = class of tobject;',
  22938. ' TBird = class',
  22939. ' end;',
  22940. ' TBirdClass = class of TBird;',
  22941. 'var',
  22942. ' o : TObject;',
  22943. ' c: TClass;',
  22944. ' b: TBird;',
  22945. ' bc: TBirdClass;',
  22946. 'procedure TObject.DoIt;',
  22947. 'begin',
  22948. ' b:=TBird(o);',
  22949. 'end;',
  22950. 'begin',
  22951. ' o.DoIt;',
  22952. ' b:=TBird(o);',
  22953. ' bc:=TBirdClass(c);',
  22954. '']);
  22955. ConvertProgram;
  22956. CheckSource('TestCheckMethodCall',
  22957. LinesToStr([ // statements
  22958. 'rtl.createClass($mod, "TObject", null, function () {',
  22959. ' this.$init = function () {',
  22960. ' };',
  22961. ' this.$final = function () {',
  22962. ' };',
  22963. ' this.DoIt = function () {',
  22964. ' rtl.checkMethodCall(this,$mod.TObject);',
  22965. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  22966. ' };',
  22967. '});',
  22968. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  22969. '});',
  22970. 'this.o = null;',
  22971. 'this.c = null;',
  22972. 'this.b = null;',
  22973. 'this.bc = null;',
  22974. '']),
  22975. LinesToStr([ // $mod.$main
  22976. '$mod.o.DoIt();',
  22977. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  22978. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  22979. '']));
  22980. end;
  22981. procedure TTestModule.TestRangeChecks_AssignInt;
  22982. begin
  22983. Scanner.Options:=Scanner.Options+[po_CAssignments];
  22984. StartProgram(false);
  22985. Add([
  22986. '{$R+}',
  22987. 'var',
  22988. ' b: byte = 2;',
  22989. ' w: word = 3;',
  22990. 'procedure DoIt(p: byte);',
  22991. 'begin',
  22992. ' b:=w;',
  22993. ' b+=w;',
  22994. ' b:=1;',
  22995. 'end;',
  22996. '{$R-}',
  22997. 'procedure DoSome;',
  22998. 'begin',
  22999. ' DoIt(w);',
  23000. ' b:=w;',
  23001. ' b:=2;',
  23002. 'end;',
  23003. 'begin',
  23004. '{$R+}',
  23005. '']);
  23006. ConvertProgram;
  23007. CheckSource('TestRangeChecks_AssignInt',
  23008. LinesToStr([ // statements
  23009. 'this.b = 2;',
  23010. 'this.w = 3;',
  23011. 'this.DoIt = function (p) {',
  23012. ' rtl.rc(p, 0, 255);',
  23013. ' $mod.b = rtl.rc($mod.w,0,255);',
  23014. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  23015. ' $mod.b = 1;',
  23016. '};',
  23017. 'this.DoSome = function () {',
  23018. ' $mod.DoIt($mod.w);',
  23019. ' $mod.b = $mod.w;',
  23020. ' $mod.b = 2;',
  23021. '};',
  23022. '']),
  23023. LinesToStr([ // $mod.$main
  23024. '']));
  23025. end;
  23026. procedure TTestModule.TestRangeChecks_AssignIntRange;
  23027. begin
  23028. Scanner.Options:=Scanner.Options+[po_CAssignments];
  23029. StartProgram(false);
  23030. Add([
  23031. '{$R+}',
  23032. 'type Ten = 1..10;',
  23033. 'var',
  23034. ' b: Ten = 2;',
  23035. ' w: Ten = 3;',
  23036. 'procedure DoIt(p: Ten);',
  23037. 'begin',
  23038. ' b:=w;',
  23039. ' b+=w;',
  23040. ' b:=1;',
  23041. 'end;',
  23042. '{$R-}',
  23043. 'procedure DoSome;',
  23044. 'begin',
  23045. ' DoIt(w);',
  23046. ' b:=w;',
  23047. ' b:=2;',
  23048. 'end;',
  23049. 'begin',
  23050. '{$R+}',
  23051. '']);
  23052. ConvertProgram;
  23053. CheckSource('TestRangeChecks_AssignIntRange',
  23054. LinesToStr([ // statements
  23055. 'this.b = 2;',
  23056. 'this.w = 3;',
  23057. 'this.DoIt = function (p) {',
  23058. ' rtl.rc(p, 1, 10);',
  23059. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  23060. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  23061. ' $mod.b = 1;',
  23062. '};',
  23063. 'this.DoSome = function () {',
  23064. ' $mod.DoIt($mod.w);',
  23065. ' $mod.b = $mod.w;',
  23066. ' $mod.b = 2;',
  23067. '};',
  23068. '']),
  23069. LinesToStr([ // $mod.$main
  23070. '']));
  23071. end;
  23072. procedure TTestModule.TestRangeChecks_AssignEnum;
  23073. begin
  23074. StartProgram(false);
  23075. Add([
  23076. '{$R+}',
  23077. 'type TEnum = (red,green);',
  23078. 'var',
  23079. ' e: TEnum = red;',
  23080. 'procedure DoIt(p: TEnum);',
  23081. 'begin',
  23082. ' e:=p;',
  23083. ' p:=TEnum(0);',
  23084. ' p:=succ(e);',
  23085. 'end;',
  23086. '{$R-}',
  23087. 'procedure DoSome;',
  23088. 'begin',
  23089. ' DoIt(e);',
  23090. ' e:=TEnum(1);',
  23091. ' e:=pred(e);',
  23092. 'end;',
  23093. 'begin',
  23094. '{$R+}',
  23095. '']);
  23096. ConvertProgram;
  23097. CheckSource('TestRangeChecks_AssignEnum',
  23098. LinesToStr([ // statements
  23099. 'this.TEnum = {',
  23100. ' "0": "red",',
  23101. ' red: 0,',
  23102. ' "1": "green",',
  23103. ' green: 1',
  23104. '};',
  23105. 'this.e = $mod.TEnum.red;',
  23106. 'this.DoIt = function (p) {',
  23107. ' rtl.rc(p, 0, 1);',
  23108. ' $mod.e = rtl.rc(p, 0, 1);',
  23109. ' p = 0;',
  23110. ' p = rtl.rc($mod.e + 1, 0, 1);',
  23111. '};',
  23112. 'this.DoSome = function () {',
  23113. ' $mod.DoIt($mod.e);',
  23114. ' $mod.e = 1;',
  23115. ' $mod.e = $mod.e - 1;',
  23116. '};',
  23117. '']),
  23118. LinesToStr([ // $mod.$main
  23119. '']));
  23120. end;
  23121. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  23122. begin
  23123. StartProgram(false);
  23124. Add([
  23125. '{$R+}',
  23126. 'type',
  23127. ' TEnum = (red,green);',
  23128. ' TEnumRg = red..green;',
  23129. 'var',
  23130. ' e: TEnumRg = red;',
  23131. 'procedure DoIt(p: TEnumRg);',
  23132. 'begin',
  23133. ' e:=p;',
  23134. ' p:=TEnumRg(0);',
  23135. ' p:=succ(e);',
  23136. 'end;',
  23137. '{$R-}',
  23138. 'procedure DoSome;',
  23139. 'begin',
  23140. ' DoIt(e);',
  23141. ' e:=TEnum(1);',
  23142. ' e:=pred(e);',
  23143. 'end;',
  23144. 'begin',
  23145. '{$R+}',
  23146. '']);
  23147. ConvertProgram;
  23148. CheckSource('TestRangeChecks_AssignEnumRange',
  23149. LinesToStr([ // statements
  23150. 'this.TEnum = {',
  23151. ' "0": "red",',
  23152. ' red: 0,',
  23153. ' "1": "green",',
  23154. ' green: 1',
  23155. '};',
  23156. 'this.e = $mod.TEnum.red;',
  23157. 'this.DoIt = function (p) {',
  23158. ' rtl.rc(p, 0, 1);',
  23159. ' $mod.e = rtl.rc(p, 0, 1);',
  23160. ' p = 0;',
  23161. ' p = rtl.rc($mod.e + 1, 0, 1);',
  23162. '};',
  23163. 'this.DoSome = function () {',
  23164. ' $mod.DoIt($mod.e);',
  23165. ' $mod.e = 1;',
  23166. ' $mod.e = $mod.e - 1;',
  23167. '};',
  23168. '']),
  23169. LinesToStr([ // $mod.$main
  23170. '']));
  23171. end;
  23172. procedure TTestModule.TestRangeChecks_AssignChar;
  23173. begin
  23174. StartProgram(false);
  23175. Add([
  23176. '{$R+}',
  23177. 'type',
  23178. ' TLetter = char;',
  23179. 'var',
  23180. ' b: TLetter = ''2'';',
  23181. ' w: TLetter = ''3'';',
  23182. 'procedure DoIt(p: TLetter);',
  23183. 'begin',
  23184. ' b:=w;',
  23185. ' b:=''1'';',
  23186. 'end;',
  23187. '{$R-}',
  23188. 'procedure DoSome;',
  23189. 'begin',
  23190. ' DoIt(w);',
  23191. ' b:=w;',
  23192. ' b:=''2'';',
  23193. 'end;',
  23194. 'begin',
  23195. '{$R+}',
  23196. '']);
  23197. ConvertProgram;
  23198. CheckSource('TestRangeChecks_AssignChar',
  23199. LinesToStr([ // statements
  23200. 'this.b = "2";',
  23201. 'this.w = "3";',
  23202. 'this.DoIt = function (p) {',
  23203. ' rtl.rcc(p, 0, 65535);',
  23204. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  23205. ' $mod.b = "1";',
  23206. '};',
  23207. 'this.DoSome = function () {',
  23208. ' $mod.DoIt($mod.w);',
  23209. ' $mod.b = $mod.w;',
  23210. ' $mod.b = "2";',
  23211. '};',
  23212. '']),
  23213. LinesToStr([ // $mod.$main
  23214. '']));
  23215. end;
  23216. procedure TTestModule.TestRangeChecks_AssignCharRange;
  23217. begin
  23218. StartProgram(false);
  23219. Add([
  23220. '{$R+}',
  23221. 'type TDigit = ''0''..''9'';',
  23222. 'var',
  23223. ' b: TDigit = ''2'';',
  23224. ' w: TDigit = ''3'';',
  23225. 'procedure DoIt(p: TDigit);',
  23226. 'begin',
  23227. ' b:=w;',
  23228. ' b:=''1'';',
  23229. 'end;',
  23230. '{$R-}',
  23231. 'procedure DoSome;',
  23232. 'begin',
  23233. ' DoIt(w);',
  23234. ' b:=w;',
  23235. ' b:=''2'';',
  23236. 'end;',
  23237. 'begin',
  23238. '{$R+}',
  23239. '']);
  23240. ConvertProgram;
  23241. CheckSource('TestRangeChecks_AssignCharRange',
  23242. LinesToStr([ // statements
  23243. 'this.b = "2";',
  23244. 'this.w = "3";',
  23245. 'this.DoIt = function (p) {',
  23246. ' rtl.rcc(p, 48, 57);',
  23247. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  23248. ' $mod.b = "1";',
  23249. '};',
  23250. 'this.DoSome = function () {',
  23251. ' $mod.DoIt($mod.w);',
  23252. ' $mod.b = $mod.w;',
  23253. ' $mod.b = "2";',
  23254. '};',
  23255. '']),
  23256. LinesToStr([ // $mod.$main
  23257. '']));
  23258. end;
  23259. procedure TTestModule.TestRangeChecks_ArrayIndex;
  23260. begin
  23261. StartProgram(false);
  23262. Add([
  23263. '{$R+}',
  23264. 'type',
  23265. ' Ten = 1..10;',
  23266. ' TArr = array of Ten;',
  23267. ' TArrArr = array of TArr;',
  23268. ' TArrByte = array[byte] of Ten;',
  23269. ' TArrChar = array[''0''..''9''] of Ten;',
  23270. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  23271. ' TObject = class',
  23272. ' A: TArr;',
  23273. ' end;',
  23274. 'procedure DoIt;',
  23275. 'var',
  23276. ' Arr: TArr;',
  23277. ' ArrArr: TArrArr;',
  23278. ' ArrByte: TArrByte;',
  23279. ' ArrChar: TArrChar;',
  23280. ' ArrByteChar: TArrByteChar;',
  23281. ' i: Ten;',
  23282. ' c: char;',
  23283. ' o: tobject;',
  23284. 'begin',
  23285. ' i:=Arr[1];',
  23286. ' i:=ArrByteChar[1,''2''];',
  23287. ' Arr[1]:=Arr[1];',
  23288. ' Arr[i]:=Arr[i];',
  23289. ' ArrByte[3]:=ArrByte[3];',
  23290. ' ArrByte[i]:=ArrByte[i];',
  23291. ' ArrChar[''5'']:=ArrChar[''5''];',
  23292. ' ArrChar[c]:=ArrChar[c];',
  23293. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  23294. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  23295. ' o.a[i]:=o.a[i];',
  23296. 'end;',
  23297. 'begin',
  23298. '']);
  23299. ConvertProgram;
  23300. CheckSource('TestRangeChecks_ArrayIndex',
  23301. LinesToStr([ // statements
  23302. 'rtl.createClass($mod, "TObject", null, function () {',
  23303. ' this.$init = function () {',
  23304. ' this.A = [];',
  23305. ' };',
  23306. ' this.$final = function () {',
  23307. ' this.A = undefined;',
  23308. ' };',
  23309. '});',
  23310. 'this.DoIt = function () {',
  23311. ' var Arr = [];',
  23312. ' var ArrArr = [];',
  23313. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  23314. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  23315. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  23316. ' var i = 0;',
  23317. ' var c = "";',
  23318. ' var o = null;',
  23319. ' i = rtl.rc(Arr[1], 1, 10);',
  23320. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  23321. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  23322. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  23323. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  23324. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  23325. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  23326. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  23327. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  23328. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  23329. ' o.A[i] = rtl.rc(o.A[i], 1, 10);',
  23330. '};',
  23331. '']),
  23332. LinesToStr([ // $mod.$main
  23333. '']));
  23334. end;
  23335. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  23336. begin
  23337. StartProgram(false);
  23338. Add([
  23339. '{$R+}',
  23340. 'type',
  23341. ' Ten = 1..10;',
  23342. ' TRec = record x: Ten end;',
  23343. ' TArr = array of TRec;',
  23344. ' TArrArr = array of TArr;',
  23345. ' TObject = class',
  23346. ' A: TArr;',
  23347. ' end;',
  23348. 'procedure DoIt;',
  23349. 'var',
  23350. ' Arr: TArr;',
  23351. ' ArrArr: TArrArr;',
  23352. ' i: Ten;',
  23353. ' o: tobject;',
  23354. 'begin',
  23355. ' Arr[1]:=Arr[1];',
  23356. ' Arr[i]:=Arr[i+1];',
  23357. ' o.a[i]:=o.a[i+2];',
  23358. 'end;',
  23359. 'begin',
  23360. '']);
  23361. ConvertProgram;
  23362. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  23363. LinesToStr([ // statements
  23364. 'rtl.recNewT($mod, "TRec", function () {',
  23365. ' this.x = 0;',
  23366. ' this.$eq = function (b) {',
  23367. ' return this.x === b.x;',
  23368. ' };',
  23369. ' this.$assign = function (s) {',
  23370. ' this.x = s.x;',
  23371. ' return this;',
  23372. ' };',
  23373. '});',
  23374. 'rtl.createClass($mod, "TObject", null, function () {',
  23375. ' this.$init = function () {',
  23376. ' this.A = [];',
  23377. ' };',
  23378. ' this.$final = function () {',
  23379. ' this.A = undefined;',
  23380. ' };',
  23381. '});',
  23382. 'this.DoIt = function () {',
  23383. ' var Arr = [];',
  23384. ' var ArrArr = [];',
  23385. ' var i = 0;',
  23386. ' var o = null;',
  23387. ' Arr[1].$assign(Arr[1]);',
  23388. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  23389. ' o.A[i].$assign(o.A[i + 2]);',
  23390. '};',
  23391. '']),
  23392. LinesToStr([ // $mod.$main
  23393. '']));
  23394. end;
  23395. procedure TTestModule.TestRangeChecks_StringIndex;
  23396. begin
  23397. StartProgram(false);
  23398. Add([
  23399. 'type',
  23400. ' TObject = class',
  23401. ' S: string;',
  23402. ' end;',
  23403. '{$R+}',
  23404. 'procedure DoIt(var h: string);',
  23405. 'var',
  23406. ' s: string;',
  23407. ' i: longint;',
  23408. ' c: char;',
  23409. ' o: tobject;',
  23410. 'begin',
  23411. ' c:=s[1];',
  23412. ' s[i]:=s[i];',
  23413. ' h[i]:=h[i];',
  23414. ' c:=o.s[i];',
  23415. 'end;',
  23416. 'begin',
  23417. '']);
  23418. ConvertProgram;
  23419. CheckSource('TestRangeChecks_StringIndex',
  23420. LinesToStr([ // statements
  23421. 'rtl.createClass($mod, "TObject", null, function () {',
  23422. ' this.$init = function () {',
  23423. ' this.S = "";',
  23424. ' };',
  23425. ' this.$final = function () {',
  23426. ' };',
  23427. '});',
  23428. 'this.DoIt = function (h) {',
  23429. ' var s = "";',
  23430. ' var i = 0;',
  23431. ' var c = "";',
  23432. ' var o = null;',
  23433. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  23434. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  23435. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  23436. ' c = rtl.rcc(o.S.charAt(i - 1), 0, 65535);',
  23437. '};',
  23438. '']),
  23439. LinesToStr([ // $mod.$main
  23440. '']));
  23441. end;
  23442. procedure TTestModule.TestRangeChecks_TypecastInt;
  23443. begin
  23444. StartProgram(false);
  23445. Add([
  23446. '{$R+}',
  23447. 'var',
  23448. ' i: nativeint;',
  23449. ' b: byte;',
  23450. ' sh: shortint;',
  23451. ' w: word;',
  23452. ' sm: smallint;',
  23453. ' lw: longword;',
  23454. ' li: longint;',
  23455. 'begin',
  23456. ' b:=12+byte(i);',
  23457. ' sh:=12+shortint(i);',
  23458. ' w:=12+word(i);',
  23459. ' sm:=12+smallint(i);',
  23460. ' lw:=12+longword(i);',
  23461. ' li:=12+longint(i);',
  23462. '']);
  23463. ConvertProgram;
  23464. CheckSource('TestRangeChecks_TypecastInt',
  23465. LinesToStr([
  23466. 'this.i = 0;',
  23467. 'this.b = 0;',
  23468. 'this.sh = 0;',
  23469. 'this.w = 0;',
  23470. 'this.sm = 0;',
  23471. 'this.lw = 0;',
  23472. 'this.li = 0;',
  23473. '']),
  23474. LinesToStr([
  23475. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  23476. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  23477. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  23478. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  23479. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  23480. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  23481. '']));
  23482. end;
  23483. Initialization
  23484. RegisterTests([TTestModule]);
  23485. end.