tcmodules.pas 369 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2014 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, fppas2js, pastree,
  20. PScanner, PasResolver, PParser, PasResolveEval, jstree, jswriter, jsbase;
  21. const
  22. // default parser+scanner options
  23. po_pas2js = [po_asmwhole,po_resolvestandardtypes];
  24. co_tcmodules = [coNoTypeInfo];
  25. type
  26. { TTestPasParser }
  27. TTestPasParser = Class(TPasParser)
  28. end;
  29. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  30. { TTestEnginePasResolver }
  31. TTestEnginePasResolver = class(TPas2JsResolver)
  32. private
  33. FFilename: string;
  34. FModule: TPasModule;
  35. FOnFindUnit: TOnFindUnit;
  36. FParser: TTestPasParser;
  37. FResolver: TStreamResolver;
  38. FScanner: TPascalScanner;
  39. FSource: string;
  40. procedure SetModule(AValue: TPasModule);
  41. public
  42. destructor Destroy; override;
  43. function FindModule(const AName: String): TPasModule; override;
  44. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  45. property Filename: string read FFilename write FFilename;
  46. property Resolver: TStreamResolver read FResolver write FResolver;
  47. property Scanner: TPascalScanner read FScanner write FScanner;
  48. property Parser: TTestPasParser read FParser write FParser;
  49. property Source: string read FSource write FSource;
  50. property Module: TPasModule read FModule write SetModule;
  51. end;
  52. { TCustomTestModule }
  53. TCustomTestModule = Class(TTestCase)
  54. private
  55. FConverter: TPasToJSConverter;
  56. FEngine: TTestEnginePasResolver;
  57. FExpectedErrorClass: ExceptClass;
  58. FExpectedErrorMsg: string;
  59. FExpectedErrorNumber: integer;
  60. FFilename: string;
  61. FFileResolver: TStreamResolver;
  62. FJSImplementationSrc: TJSSourceElements;
  63. FJSImplementationUses: TJSArrayLiteral;
  64. FJSInitBody: TJSFunctionBody;
  65. FJSImplentationUses: TJSArrayLiteral;
  66. FJSInterfaceUses: TJSArrayLiteral;
  67. FJSModule: TJSSourceElements;
  68. FJSModuleSrc: TJSSourceElements;
  69. FJSSource: TStringList;
  70. FModule: TPasModule;
  71. FJSModuleCallArgs: TJSArguments;
  72. FModules: TObjectList;// list of TTestEnginePasResolver
  73. FParser: TTestPasParser;
  74. FPasProgram: TPasProgram;
  75. FJSRegModuleCall: TJSCallExpression;
  76. FScanner: TPascalScanner;
  77. FSkipTests: boolean;
  78. FSource: TStringList;
  79. FFirstPasStatement: TPasImplBlock;
  80. function GetModuleCount: integer;
  81. function GetModules(Index: integer): TTestEnginePasResolver;
  82. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  83. function FindUnit(const aUnitName: String): TPasModule;
  84. protected
  85. procedure SetUp; override;
  86. procedure TearDown; override;
  87. Procedure Add(Line: string); virtual;
  88. Procedure Add(const Lines: array of string);
  89. Procedure StartParsing; virtual;
  90. procedure ParseModule; virtual;
  91. procedure ParseProgram; virtual;
  92. procedure ParseUnit; virtual;
  93. protected
  94. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  95. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  96. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  97. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  98. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  99. procedure AddSystemUnit; virtual;
  100. procedure StartProgram(NeedSystemUnit: boolean); virtual;
  101. procedure StartUnit(NeedSystemUnit: boolean); virtual;
  102. procedure ConvertModule; virtual;
  103. procedure ConvertProgram; virtual;
  104. procedure ConvertUnit; virtual;
  105. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  106. function GetDottedIdentifier(El: TJSElement): string;
  107. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  108. ImplStatements: string = ''); virtual;
  109. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  110. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  111. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  112. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  113. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  114. function IsErrorExpected(E: Exception): boolean;
  115. procedure HandleScannerError(E: EScannerError);
  116. procedure HandleParserError(E: EParserError);
  117. procedure HandlePasResolveError(E: EPasResolve);
  118. procedure HandlePas2JSError(E: EPas2JS);
  119. procedure HandleException(E: Exception);
  120. procedure RaiseException(E: Exception);
  121. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  122. function GetDefaultNamespace: string;
  123. property PasProgram: TPasProgram Read FPasProgram;
  124. property Modules[Index: integer]: TTestEnginePasResolver read GetModules;
  125. property ModuleCount: integer read GetModuleCount;
  126. property Engine: TTestEnginePasResolver read FEngine;
  127. property Filename: string read FFilename;
  128. Property Module: TPasModule Read FModule;
  129. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  130. property Converter: TPasToJSConverter read FConverter;
  131. property JSSource: TStringList read FJSSource;
  132. property JSModule: TJSSourceElements read FJSModule;
  133. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  134. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  135. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  136. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  137. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  138. property JSInitBody: TJSFunctionBody read FJSInitBody;
  139. property JSImplementationSrc: TJSSourceElements read FJSImplementationSrc;
  140. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  141. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  142. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  143. property SkipTests: boolean read FSkipTests write FSkipTests;
  144. public
  145. property Source: TStringList read FSource;
  146. property FileResolver: TStreamResolver read FFileResolver;
  147. property Scanner: TPascalScanner read FScanner;
  148. property Parser: TTestPasParser read FParser;
  149. end;
  150. { TTestModule }
  151. TTestModule = class(TCustomTestModule)
  152. Published
  153. // modules
  154. Procedure TestEmptyProgram;
  155. Procedure TestEmptyProgramUseStrict;
  156. Procedure TestEmptyUnit;
  157. Procedure TestEmptyUnitUseStrict;
  158. Procedure TestDottedUnitNames;
  159. Procedure TestDottedUnitExpr;
  160. Procedure Test_ModeFPCFail;
  161. Procedure Test_ModeSwitchCBlocksFail;
  162. // vars/const
  163. Procedure TestVarInt;
  164. Procedure TestVarBaseTypes;
  165. Procedure TestBaseTypeSingleFail;
  166. Procedure TestBaseTypeExtendedFail;
  167. Procedure TestConstBaseTypes;
  168. Procedure TestUnitImplVars;
  169. Procedure TestUnitImplConsts;
  170. Procedure TestUnitImplRecord;
  171. Procedure TestRenameJSNameConflict;
  172. Procedure TestLocalConst;
  173. Procedure TestVarExternal;
  174. Procedure TestVarExternalOtherUnit;
  175. // numbers
  176. Procedure TestDouble;
  177. // strings
  178. Procedure TestCharConst;
  179. Procedure TestChar_Compare;
  180. Procedure TestChar_Ord;
  181. Procedure TestChar_Chr;
  182. Procedure TestStringConst;
  183. Procedure TestString_Length;
  184. Procedure TestString_Compare;
  185. Procedure TestString_SetLength;
  186. Procedure TestString_CharAt;
  187. Procedure TestStr;
  188. Procedure TestBaseType_AnsiStringFail;
  189. Procedure TestBaseType_UnicodeStringFail;
  190. Procedure TestBaseType_ShortStringFail;
  191. Procedure TestBaseType_RawByteStringFail;
  192. Procedure TestTypeShortstring_Fail;
  193. // alias types
  194. Procedure TestAliasTypeRef;
  195. Procedure TestTypeCast_BaseTypes;
  196. Procedure TestTypeCast_AliasBaseTypes;
  197. // functions
  198. Procedure TestEmptyProc;
  199. Procedure TestProcOneParam;
  200. Procedure TestFunctionWithoutParams;
  201. Procedure TestProcedureWithoutParams;
  202. Procedure TestPrgProcVar;
  203. Procedure TestProcTwoArgs;
  204. Procedure TestProc_DefaultValue;
  205. Procedure TestUnitProcVar;
  206. Procedure TestImplProc;
  207. Procedure TestFunctionResult;
  208. Procedure TestNestedProc;
  209. Procedure TestForwardProc;
  210. Procedure TestNestedForwardProc;
  211. Procedure TestAssignFunctionResult;
  212. Procedure TestFunctionResultInCondition;
  213. Procedure TestExit;
  214. Procedure TestBreak;
  215. Procedure TestContinue;
  216. Procedure TestProc_External;
  217. Procedure TestProc_ExternalOtherUnit;
  218. Procedure TestProc_Asm;
  219. Procedure TestProc_Assembler;
  220. Procedure TestProc_VarParam;
  221. Procedure TestProc_Overload;
  222. Procedure TestProc_OverloadForward;
  223. Procedure TestProc_OverloadUnit;
  224. Procedure TestProc_OverloadNested;
  225. Procedure TestProc_Varargs;
  226. // enums, sets
  227. Procedure TestEnum_Name;
  228. Procedure TestEnum_Number;
  229. Procedure TestEnum_Functions;
  230. Procedure TestEnum_AsParams;
  231. Procedure TestSet;
  232. Procedure TestSet_Operators;
  233. Procedure TestSet_Operator_In;
  234. Procedure TestSet_Functions;
  235. Procedure TestSet_PassAsArgClone;
  236. Procedure TestSet_AsParams;
  237. Procedure TestSet_Property;
  238. Procedure TestSet_EnumConst;
  239. Procedure TestSet_AnonymousEnumType;
  240. Procedure TestSet_CharFail;
  241. Procedure TestSet_BooleanFail;
  242. Procedure TestSet_ConstEnum;
  243. Procedure TestSet_ConstChar;
  244. // statements
  245. Procedure TestNestBegin;
  246. Procedure TestIncDec;
  247. Procedure TestAssignments;
  248. Procedure TestArithmeticOperators1;
  249. Procedure TestLogicalOperators;
  250. Procedure TestBitwiseOperators;
  251. Procedure TestFunctionInt;
  252. Procedure TestFunctionString;
  253. Procedure TestForLoop;
  254. Procedure TestForLoopInFunction;
  255. Procedure TestForLoop_ReadVarAfter;
  256. Procedure TestForLoop_Nested;
  257. Procedure TestRepeatUntil;
  258. Procedure TestAsmBlock;
  259. Procedure TestAsmPas_Impl; // ToDo
  260. Procedure TestTryFinally;
  261. Procedure TestTryExcept;
  262. Procedure TestCaseOf;
  263. Procedure TestCaseOf_UseSwitch;
  264. Procedure TestCaseOfNoElse;
  265. Procedure TestCaseOfNoElse_UseSwitch;
  266. Procedure TestCaseOfRange;
  267. // arrays
  268. Procedure TestArray_Dynamic;
  269. Procedure TestArray_Dynamic_Nil;
  270. Procedure TestArray_DynMultiDimensional;
  271. Procedure TestArrayOfRecord;
  272. // ToDo: Procedure TestArrayOfSet;
  273. Procedure TestArray_AsParams;
  274. Procedure TestArrayElement_AsParams;
  275. Procedure TestArrayElementFromFuncResult_AsParams;
  276. Procedure TestArrayEnumTypeRange;
  277. Procedure TestArray_SetLengthOutArg;
  278. Procedure TestArray_SetLengthProperty;
  279. Procedure TestArray_OpenArrayOfString;
  280. Procedure TestArray_Concat;
  281. Procedure TestArray_Copy;
  282. Procedure TestArray_InsertDelete;
  283. Procedure TestArray_DynArrayConst;
  284. Procedure TestExternalClass_TypeCastArrayToExternalArray;
  285. Procedure TestExternalClass_TypeCastArrayFromExternalArray;
  286. // ToDo: static array const
  287. // ToDo: SetLength(array of static array)
  288. // ToDo: SetLength(dim1,dim2)
  289. // record
  290. Procedure TestRecord_Var;
  291. Procedure TestWithRecordDo;
  292. Procedure TestRecord_Assign;
  293. Procedure TestRecord_PassAsArgClone;
  294. Procedure TestRecord_AsParams;
  295. Procedure TestRecordElement_AsParams;
  296. Procedure TestRecordElementFromFuncResult_AsParams;
  297. Procedure TestRecordElementFromWith_AsParams;
  298. Procedure TestRecord_Equal;
  299. Procedure TestRecord_TypeCastJSValueToRecord;
  300. // ToDo: const record
  301. // classes
  302. Procedure TestClass_TObjectDefaultConstructor;
  303. Procedure TestClass_TObjectConstructorWithParams;
  304. Procedure TestClass_Var;
  305. Procedure TestClass_Method;
  306. Procedure TestClass_Implementation;
  307. Procedure TestClass_Inheritance;
  308. Procedure TestClass_AbstractMethod;
  309. Procedure TestClass_CallInherited_NoParams;
  310. Procedure TestClass_CallInherited_WithParams;
  311. Procedure TestClasS_CallInheritedConstructor;
  312. Procedure TestClass_ClassVar;
  313. Procedure TestClass_CallClassMethod;
  314. Procedure TestClass_Property;
  315. Procedure TestClass_Property_ClassMethod;
  316. Procedure TestClass_Property_Index;
  317. Procedure TestClass_PropertyOfTypeArray;
  318. Procedure TestClass_PropertyDefault;
  319. Procedure TestClass_PropertyOverride;
  320. Procedure TestClass_Assigned;
  321. Procedure TestClass_WithClassDoCreate;
  322. Procedure TestClass_WithClassInstDoProperty;
  323. Procedure TestClass_WithClassInstDoPropertyWithParams;
  324. Procedure TestClass_WithClassInstDoFunc;
  325. Procedure TestClass_TypeCast;
  326. Procedure TestClass_TypeCastUntypedParam;
  327. Procedure TestClass_Overloads;
  328. Procedure TestClass_OverloadsAncestor;
  329. Procedure TestClass_OverloadConstructor;
  330. Procedure TestClass_ReintroducedVar;
  331. Procedure TestClass_RaiseDescendant;
  332. Procedure TestClass_ExternalMethod;
  333. Procedure TestClass_ExternalVirtualNameMismatchFail;
  334. Procedure TestClass_ExternalOverrideFail;
  335. Procedure TestClass_ExternalVar;
  336. Procedure TestClass_Const;
  337. Procedure TestClass_LocalVarSelfFail;
  338. Procedure TestClass_ArgSelfFail;
  339. Procedure TestClass_NestedSelf;
  340. Procedure TestClass_NestedClassSelf;
  341. Procedure TestClass_NestedCallInherited;
  342. Procedure TestClass_TObjectFree;
  343. Procedure TestClass_TObjectFreeNewInstance;
  344. Procedure TestClass_TObjectFreeLowerCase;
  345. Procedure TestClass_TObjectFreeFunctionFail;
  346. Procedure TestClass_TObjectFreePropertyFail;
  347. // class of
  348. Procedure TestClassOf_Create;
  349. Procedure TestClassOf_Call;
  350. Procedure TestClassOf_Assign;
  351. Procedure TestClassOf_Is;
  352. Procedure TestClassOf_Compare;
  353. Procedure TestClassOf_ClassVar;
  354. Procedure TestClassOf_ClassMethod;
  355. Procedure TestClassOf_ClassProperty;
  356. Procedure TestClassOf_ClassMethodSelf;
  357. Procedure TestClassOf_TypeCast;
  358. Procedure TestClassOf_ImplicitFunctionCall;
  359. // nested class
  360. Procedure TestNestedClass_Fail;
  361. // external class
  362. Procedure TestExternalClass_Var;
  363. //ToDo Procedure TestExternalClass_Const;
  364. Procedure TestExternalClass_Dollar;
  365. Procedure TestExternalClass_DuplicateVarFail;
  366. Procedure TestExternalClass_Method;
  367. Procedure TestExternalClass_NonExternalOverride;
  368. Procedure TestExternalClass_Property;
  369. Procedure TestExternalClass_ClassProperty;
  370. Procedure TestExternalClass_ClassOf;
  371. Procedure TestExternalClass_ClassOtherUnit;
  372. Procedure TestExternalClass_Is;
  373. Procedure TestExternalClass_As;
  374. Procedure TestExternalClass_DestructorFail;
  375. Procedure TestExternalClass_New;
  376. Procedure TestExternalClass_ClassOf_New;
  377. Procedure TestExternalClass_FuncClassOf_New;
  378. Procedure TestExternalClass_LocalConstSameName;
  379. Procedure TestExternalClass_ReintroduceOverload;
  380. Procedure TestExternalClass_Inherited;
  381. Procedure TestExternalClass_PascalAncestorFail;
  382. Procedure TestExternalClass_NewInstance;
  383. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  384. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  385. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  386. Procedure TestExternalClass_PascalProperty;
  387. Procedure TestExternalClass_TypeCastToRootClass;
  388. Procedure TestExternalClass_TypeCastStringToExternalString;
  389. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  390. Procedure TestExternalClass_BracketAccessor;
  391. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  392. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  393. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  394. Procedure TestExternalClass_BracketAccessor_MultiType;
  395. Procedure TestExternalClass_BracketAccessor_Index;
  396. // proc types
  397. Procedure TestProcType;
  398. Procedure TestProcType_FunctionFPC;
  399. Procedure TestProcType_FunctionDelphi;
  400. Procedure TestProcType_AsParam;
  401. Procedure TestProcType_MethodFPC;
  402. Procedure TestProcType_MethodDelphi;
  403. Procedure TestProcType_PropertyFPC;
  404. Procedure TestProcType_PropertyDelphi;
  405. Procedure TestProcType_WithClassInstDoPropertyFPC;
  406. Procedure TestProcType_Nested;
  407. Procedure TestProcType_NestedOfObject;
  408. Procedure TestProcType_ReferenceToProc;
  409. Procedure TestProcType_ReferenceToMethod;
  410. Procedure TestProcType_Typecast;
  411. Procedure TestProcType_PassProcToUntyped;
  412. // pointer
  413. Procedure TestPointer;
  414. Procedure TestPointer_Proc;
  415. Procedure TestPointer_AssignRecordFail;
  416. Procedure TestPointer_AssignStaticArrayFail;
  417. Procedure TestPointer_ArrayParamsFail;
  418. Procedure TestPointer_TypeCastJSValueToPointer;
  419. // jsvalue
  420. Procedure TestJSValue_AssignToJSValue;
  421. Procedure TestJSValue_TypeCastToBaseType;
  422. Procedure TestJSValue_Equal;
  423. Procedure TestJSValue_If;
  424. Procedure TestJSValue_Enum;
  425. Procedure TestJSValue_ClassInstance;
  426. Procedure TestJSValue_ClassOf;
  427. Procedure TestJSValue_ArrayOfJSValue;
  428. Procedure TestJSValue_Params;
  429. Procedure TestJSValue_UntypedParam;
  430. Procedure TestJSValue_FuncResultType;
  431. Procedure TestJSValue_ProcType_Assign;
  432. Procedure TestJSValue_ProcType_Equal;
  433. Procedure TestJSValue_AssignToPointerFail;
  434. Procedure TestJSValue_OverloadDouble;
  435. Procedure TestJSValue_OverloadNativeInt;
  436. Procedure TestJSValue_OverloadWord;
  437. Procedure TestJSValue_OverloadString;
  438. Procedure TestJSValue_OverloadChar;
  439. Procedure TestJSValue_OverloadPointer;
  440. // RTTI
  441. Procedure TestRTTI_ProcType;
  442. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  443. Procedure TestRTTI_EnumAndSetType;
  444. Procedure TestRTTI_AnonymousEnumType;
  445. Procedure TestRTTI_StaticArray;
  446. Procedure TestRTTI_DynArray;
  447. Procedure TestRTTI_ArrayNestedAnonymous;
  448. // ToDo: Procedure TestRTTI_Pointer;
  449. Procedure TestRTTI_PublishedMethodOverloadFail;
  450. Procedure TestRTTI_PublishedMethodExternalFail;
  451. Procedure TestRTTI_PublishedClassPropertyFail;
  452. Procedure TestRTTI_PublishedClassFieldFail;
  453. Procedure TestRTTI_PublishedFieldExternalFail;
  454. Procedure TestRTTI_Class_Field;
  455. Procedure TestRTTI_Class_Method;
  456. Procedure TestRTTI_Class_MethodArgFlags;
  457. Procedure TestRTTI_Class_Property;
  458. Procedure TestRTTI_Class_PropertyParams;
  459. // ToDo: property default value
  460. Procedure TestRTTI_OverrideMethod;
  461. Procedure TestRTTI_OverloadProperty;
  462. // ToDo: array argument
  463. Procedure TestRTTI_ClassForward;
  464. Procedure TestRTTI_ClassOf;
  465. Procedure TestRTTI_Record;
  466. Procedure TestRTTI_LocalTypes;
  467. Procedure TestRTTI_TypeInfo_BaseTypes;
  468. Procedure TestRTTI_TypeInfo_LocalFail;
  469. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  470. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  471. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  472. Procedure TestRTTI_TypeInfo_FunctionClassType;
  473. end;
  474. function LinesToStr(Args: array of const): string;
  475. function ExtractFileUnitName(aFilename: string): string;
  476. function JSToStr(El: TJSElement): string;
  477. implementation
  478. function LinesToStr(Args: array of const): string;
  479. var
  480. s: String;
  481. i: Integer;
  482. begin
  483. s:='';
  484. for i:=Low(Args) to High(Args) do
  485. case Args[i].VType of
  486. vtChar: s += Args[i].VChar+LineEnding;
  487. vtString: s += Args[i].VString^+LineEnding;
  488. vtPChar: s += Args[i].VPChar+LineEnding;
  489. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  490. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  491. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  492. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  493. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  494. end;
  495. Result:=s;
  496. end;
  497. function ExtractFileUnitName(aFilename: string): string;
  498. var
  499. p: Integer;
  500. begin
  501. Result:=ExtractFileName(aFilename);
  502. if Result='' then exit;
  503. for p:=length(Result) downto 1 do
  504. case Result[p] of
  505. '/','\': exit;
  506. '.':
  507. begin
  508. Delete(Result,p,length(Result));
  509. exit;
  510. end;
  511. end;
  512. end;
  513. function JSToStr(El: TJSElement): string;
  514. var
  515. aWriter: TBufferWriter;
  516. aJSWriter: TJSWriter;
  517. begin
  518. aWriter:=TBufferWriter.Create(1000);
  519. try
  520. aJSWriter:=TJSWriter.Create(aWriter);
  521. aJSWriter.IndentSize:=2;
  522. aJSWriter.WriteJS(El);
  523. Result:=aWriter.AsAnsistring;
  524. finally
  525. aWriter.Free;
  526. end;
  527. end;
  528. { TTestEnginePasResolver }
  529. procedure TTestEnginePasResolver.SetModule(AValue: TPasModule);
  530. begin
  531. if FModule=AValue then Exit;
  532. if Module<>nil then
  533. Module.Release;
  534. FModule:=AValue;
  535. if Module<>nil then
  536. Module.AddRef;
  537. end;
  538. destructor TTestEnginePasResolver.Destroy;
  539. begin
  540. FreeAndNil(FResolver);
  541. Module:=nil;
  542. FreeAndNil(FParser);
  543. FreeAndNil(FScanner);
  544. FreeAndNil(FResolver);
  545. inherited Destroy;
  546. end;
  547. function TTestEnginePasResolver.FindModule(const AName: String): TPasModule;
  548. begin
  549. Result:=nil;
  550. if Assigned(OnFindUnit) then
  551. Result:=OnFindUnit(AName);
  552. end;
  553. { TCustomTestModule }
  554. function TCustomTestModule.GetModuleCount: integer;
  555. begin
  556. Result:=FModules.Count;
  557. end;
  558. function TCustomTestModule.GetModules(Index: integer
  559. ): TTestEnginePasResolver;
  560. begin
  561. Result:=TTestEnginePasResolver(FModules[Index]);
  562. end;
  563. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  564. ): TPasModule;
  565. var
  566. DefNamespace: String;
  567. begin
  568. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  569. if (Pos('.',aUnitName)<1) then
  570. begin
  571. DefNamespace:=GetDefaultNamespace;
  572. if DefNamespace<>'' then
  573. begin
  574. Result:=FindUnit(DefNamespace+'.'+aUnitName);
  575. if Result<>nil then exit;
  576. end;
  577. end;
  578. Result:=FindUnit(aUnitName);
  579. if Result<>nil then exit;
  580. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  581. Fail('can''t find unit "'+aUnitName+'"');
  582. end;
  583. function TCustomTestModule.FindUnit(const aUnitName: String): TPasModule;
  584. var
  585. i: Integer;
  586. CurEngine: TTestEnginePasResolver;
  587. CurUnitName: String;
  588. begin
  589. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  590. Result:=nil;
  591. for i:=0 to ModuleCount-1 do
  592. begin
  593. CurEngine:=Modules[i];
  594. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  595. //writeln('TTestModule.FindUnit Checking ',i,'/',ModuleCount,' ',CurEngine.Filename,' ',CurUnitName);
  596. if CompareText(aUnitName,CurUnitName)=0 then
  597. begin
  598. Result:=CurEngine.Module;
  599. if Result<>nil then exit;
  600. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  601. FileResolver.FindSourceFile(aUnitName);
  602. CurEngine.Resolver:=TStreamResolver.Create;
  603. CurEngine.Resolver.OwnsStreams:=True;
  604. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  605. CurEngine.Resolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  606. CurEngine.Scanner:=TPascalScanner.Create(CurEngine.Resolver);
  607. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.Resolver,CurEngine);
  608. CurEngine.Parser.Options:=CurEngine.Parser.Options+po_pas2js+[po_KeepScannerError];
  609. if CompareText(CurUnitName,'System')=0 then
  610. CurEngine.Parser.ImplicitUses.Clear;
  611. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  612. try
  613. CurEngine.Parser.NextToken;
  614. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  615. except
  616. on E: Exception do
  617. HandleException(E);
  618. end;
  619. //writeln('TTestModule.FindUnit END ',CurUnitName);
  620. Result:=CurEngine.Module;
  621. exit;
  622. end;
  623. end;
  624. end;
  625. procedure TCustomTestModule.SetUp;
  626. begin
  627. inherited SetUp;
  628. FSkipTests:=false;
  629. FSource:=TStringList.Create;
  630. FModules:=TObjectList.Create(true);
  631. FFilename:='test1.pp';
  632. FFileResolver:=TStreamResolver.Create;
  633. FFileResolver.OwnsStreams:=True;
  634. FScanner:=TPascalScanner.Create(FFileResolver);
  635. FScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  636. FScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  637. FScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  638. FEngine:=AddModule(Filename);
  639. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  640. Parser.Options:=Parser.Options+po_pas2js+[po_KeepScannerError];
  641. FModule:=Nil;
  642. FConverter:=TPasToJSConverter.Create;
  643. FConverter.Options:=co_tcmodules;
  644. FExpectedErrorClass:=nil;
  645. end;
  646. procedure TCustomTestModule.TearDown;
  647. begin
  648. FSkipTests:=false;
  649. FJSModule:=nil;
  650. FJSRegModuleCall:=nil;
  651. FJSModuleCallArgs:=nil;
  652. FJSImplentationUses:=nil;
  653. FJSInterfaceUses:=nil;
  654. FJSModuleSrc:=nil;
  655. FJSInitBody:=nil;
  656. FreeAndNil(FJSSource);
  657. FreeAndNil(FJSModule);
  658. FreeAndNil(FConverter);
  659. Engine.Clear;
  660. if Assigned(FModule) then
  661. begin
  662. FModule.Release;
  663. FModule:=nil;
  664. end;
  665. FreeAndNil(FSource);
  666. FreeAndNil(FParser);
  667. FreeAndNil(FScanner);
  668. FreeAndNil(FFileResolver);
  669. if FModules<>nil then
  670. begin
  671. FreeAndNil(FModules);
  672. FEngine:=nil;
  673. end;
  674. inherited TearDown;
  675. end;
  676. procedure TCustomTestModule.Add(Line: string);
  677. begin
  678. Source.Add(Line);
  679. end;
  680. procedure TCustomTestModule.Add(const Lines: array of string);
  681. var
  682. i: Integer;
  683. begin
  684. for i:=low(Lines) to high(Lines) do
  685. Add(Lines[i]);
  686. end;
  687. procedure TCustomTestModule.StartParsing;
  688. var
  689. Src: String;
  690. begin
  691. Src:=Source.Text;
  692. FEngine.Source:=Src;
  693. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  694. Scanner.OpenFile(FileName);
  695. Writeln('// Test : ',Self.TestName);
  696. Writeln(Src);
  697. end;
  698. procedure TCustomTestModule.ParseModule;
  699. begin
  700. if SkipTests then exit;
  701. FFirstPasStatement:=nil;
  702. try
  703. StartParsing;
  704. Parser.ParseMain(FModule);
  705. except
  706. on E: Exception do
  707. HandleException(E);
  708. end;
  709. if SkipTests then exit;
  710. AssertNotNull('Module resulted in Module',FModule);
  711. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  712. TAssert.AssertSame('Has resolver',Engine,Parser.Engine);
  713. end;
  714. procedure TCustomTestModule.ParseProgram;
  715. begin
  716. if SkipTests then exit;
  717. ParseModule;
  718. if SkipTests then exit;
  719. AssertEquals('Has program',TPasProgram,Module.ClassType);
  720. FPasProgram:=TPasProgram(Module);
  721. AssertNotNull('Has program section',PasProgram.ProgramSection);
  722. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  723. if (PasProgram.InitializationSection.Elements.Count>0) then
  724. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  725. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  726. end;
  727. procedure TCustomTestModule.ParseUnit;
  728. begin
  729. if SkipTests then exit;
  730. ParseModule;
  731. if SkipTests then exit;
  732. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  733. AssertNotNull('Has interface section',Module.InterfaceSection);
  734. AssertNotNull('Has implementation section',Module.ImplementationSection);
  735. if (Module.InitializationSection<>nil)
  736. and (Module.InitializationSection.Elements.Count>0)
  737. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  738. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  739. end;
  740. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  741. ): TTestEnginePasResolver;
  742. var
  743. i: Integer;
  744. begin
  745. for i:=0 to ModuleCount-1 do
  746. if CompareText(Modules[i].Filename,aFilename)=0 then
  747. exit(Modules[i]);
  748. Result:=nil;
  749. end;
  750. function TCustomTestModule.AddModule(aFilename: string
  751. ): TTestEnginePasResolver;
  752. begin
  753. //writeln('TTestModuleConverter.AddModule ',aFilename);
  754. if FindModuleWithFilename(aFilename)<>nil then
  755. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  756. Result:=TTestEnginePasResolver.Create;
  757. Result.Filename:=aFilename;
  758. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  759. Result.OnFindUnit:=@OnPasResolverFindUnit;
  760. FModules.Add(Result);
  761. end;
  762. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  763. ): TTestEnginePasResolver;
  764. begin
  765. Result:=AddModule(aFilename);
  766. Result.Source:=Src;
  767. end;
  768. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  769. ImplementationSrc: string): TTestEnginePasResolver;
  770. var
  771. Src: String;
  772. begin
  773. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  774. Src+=LineEnding;
  775. Src+='interface'+LineEnding;
  776. Src+=LineEnding;
  777. Src+=InterfaceSrc;
  778. Src+='implementation'+LineEnding;
  779. Src+=LineEnding;
  780. Src+=ImplementationSrc;
  781. Src+='end.'+LineEnding;
  782. Result:=AddModuleWithSrc(aFilename,Src);
  783. end;
  784. procedure TCustomTestModule.AddSystemUnit;
  785. begin
  786. AddModuleWithIntfImplSrc('system.pp',
  787. // interface
  788. LinesToStr([
  789. 'type',
  790. ' integer=longint;',
  791. 'var',
  792. ' ExitCode: Longint;',
  793. ''
  794. // implementation
  795. ]),LinesToStr([
  796. ''
  797. ]));
  798. end;
  799. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean);
  800. begin
  801. if NeedSystemUnit then
  802. AddSystemUnit
  803. else
  804. Parser.ImplicitUses.Clear;
  805. Add('program '+ExtractFileUnitName(Filename)+';');
  806. Add('');
  807. end;
  808. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean);
  809. begin
  810. if NeedSystemUnit then
  811. AddSystemUnit
  812. else
  813. Parser.ImplicitUses.Clear;
  814. Add('unit Test1;');
  815. Add('');
  816. end;
  817. procedure TCustomTestModule.ConvertModule;
  818. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  819. out UsesLit: TJSArrayLiteral);
  820. var
  821. i: Integer;
  822. Item: TJSElement;
  823. Lit: TJSLiteral;
  824. begin
  825. UsesLit:=nil;
  826. AssertNotNull(UsesName+' uses section',Arg.Expr);
  827. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  828. exit; // null is ok
  829. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  830. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  831. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  832. begin
  833. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  834. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  835. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  836. Lit:=TJSLiteral(Item);
  837. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  838. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  839. end;
  840. end;
  841. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  842. out Src: TJSSourceElements);
  843. var
  844. FunDecl: TJSFunctionDeclarationStatement;
  845. FunDef: TJSFuncDef;
  846. FunBody: TJSFunctionBody;
  847. begin
  848. Src:=nil;
  849. AssertNotNull(ParamName,Arg.Expr);
  850. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  851. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  852. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  853. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  854. FunDef:=FunDecl.AFunction as TJSFuncDef;
  855. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  856. AssertNotNull(ParamName+' body',FunDef.Body);
  857. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  858. FunBody:=FunDef.Body as TJSFunctionBody;
  859. AssertNotNull(ParamName+' body.A',FunBody.A);
  860. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  861. Src:=FunBody.A as TJSSourceElements;
  862. end;
  863. var
  864. ModuleNameExpr: TJSLiteral;
  865. InitFunction: TJSFunctionDeclarationStatement;
  866. InitAssign: TJSSimpleAssignStatement;
  867. InitName: String;
  868. LastNode: TJSElement;
  869. Arg: TJSArrayLiteralElement;
  870. begin
  871. if SkipTests then exit;
  872. try
  873. FJSModule:=FConverter.ConvertPasElement(Module,Engine) as TJSSourceElements;
  874. except
  875. on E: Exception do
  876. HandleException(E);
  877. end;
  878. if SkipTests then exit;
  879. if ExpectedErrorClass<>nil then
  880. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  881. FJSSource:=TStringList.Create;
  882. FJSSource.Text:=JSToStr(JSModule);
  883. {$IFDEF VerbosePas2JS}
  884. writeln('TTestModule.ConvertModule JS:');
  885. write(FJSSource.Text);
  886. {$ENDIF}
  887. // rtl.module(...
  888. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  889. AssertNotNull('register module call',JSModule.Statements.Nodes[0].Node);
  890. AssertEquals('register module call',TJSCallExpression,JSModule.Statements.Nodes[0].Node.ClassType);
  891. FJSRegModuleCall:=JSModule.Statements.Nodes[0].Node as TJSCallExpression;
  892. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  893. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  894. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  895. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  896. // parameter 'unitname'
  897. if JSModuleCallArgs.Elements.Count<1 then
  898. Fail('rtl.module first param unit missing');
  899. Arg:=JSModuleCallArgs.Elements.Elements[0];
  900. AssertNotNull('module name param',Arg.Expr);
  901. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  902. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  903. if Module is TPasProgram then
  904. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString))
  905. else
  906. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  907. // main uses section
  908. if JSModuleCallArgs.Elements.Count<2 then
  909. Fail('rtl.module second param main uses missing');
  910. Arg:=JSModuleCallArgs.Elements.Elements[1];
  911. CheckUsesList('interface',Arg,FJSInterfaceUses);
  912. // program/library/interface function()
  913. if JSModuleCallArgs.Elements.Count<3 then
  914. Fail('rtl.module third param intf-function missing');
  915. Arg:=JSModuleCallArgs.Elements.Elements[2];
  916. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  917. // search for $mod.$init or $mod.$main - the last statement
  918. if Module is TPasProgram then
  919. begin
  920. InitName:='$main';
  921. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  922. end
  923. else
  924. InitName:='$init';
  925. FJSInitBody:=nil;
  926. if JSModuleSrc.Statements.Count>0 then
  927. begin
  928. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  929. if LastNode is TJSSimpleAssignStatement then
  930. begin
  931. InitAssign:=LastNode as TJSSimpleAssignStatement;
  932. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  933. begin
  934. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  935. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  936. end
  937. else if Module is TPasProgram then
  938. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  939. end;
  940. end;
  941. // optional: implementation uses section
  942. if JSModuleCallArgs.Elements.Count<4 then
  943. exit;
  944. Arg:=JSModuleCallArgs.Elements.Elements[3];
  945. CheckUsesList('implementation',Arg,FJSImplentationUses);
  946. // optional: implementation function()
  947. if JSModuleCallArgs.Elements.Count<5 then
  948. exit;
  949. Arg:=JSModuleCallArgs.Elements.Elements[4];
  950. CheckFunctionParam('module impl-function',Arg,FJSImplementationSrc);
  951. end;
  952. procedure TCustomTestModule.ConvertProgram;
  953. begin
  954. Add('end.');
  955. ParseProgram;
  956. ConvertModule;
  957. end;
  958. procedure TCustomTestModule.ConvertUnit;
  959. begin
  960. Add('end.');
  961. ParseUnit;
  962. ConvertModule;
  963. end;
  964. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  965. DottedName: string);
  966. begin
  967. if DottedName='' then
  968. begin
  969. AssertNull(Msg,El);
  970. end
  971. else
  972. begin
  973. AssertNotNull(Msg,El);
  974. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  975. end;
  976. end;
  977. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  978. begin
  979. if El=nil then
  980. Result:=''
  981. else if El is TJSPrimaryExpressionIdent then
  982. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  983. else if El is TJSDotMemberExpression then
  984. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  985. else
  986. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  987. end;
  988. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  989. InitStatements: string; ImplStatements: string);
  990. var
  991. ActualSrc, ExpectedSrc, InitName: String;
  992. begin
  993. ActualSrc:=JSToStr(JSModuleSrc);
  994. ExpectedSrc:=
  995. 'var $mod = this;'+LineEnding
  996. +Statements;
  997. if coUseStrict in Converter.Options then
  998. ExpectedSrc:='"use strict";'+LineEnding+ExpectedSrc;
  999. if Module is TPasProgram then
  1000. InitName:='$main'
  1001. else
  1002. InitName:='$init';
  1003. if (Module is TPasProgram) or (Trim(InitStatements)<>'') then
  1004. ExpectedSrc:=ExpectedSrc+LineEnding
  1005. +'$mod.'+InitName+' = function () {'+LineEnding
  1006. +InitStatements
  1007. +'};'+LineEnding;
  1008. //writeln('TTestModule.CheckSource InitStatements="',InitStatements,'"');
  1009. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1010. if (JSImplementationSrc<>nil) then
  1011. begin
  1012. ActualSrc:=JSToStr(JSImplementationSrc);
  1013. ExpectedSrc:=
  1014. 'var $mod = this;'+LineEnding
  1015. +'var $impl = $mod.$impl;'+LineEnding
  1016. +ImplStatements;
  1017. end
  1018. else
  1019. begin
  1020. ActualSrc:='';
  1021. ExpectedSrc:=ImplStatements;
  1022. end;
  1023. //writeln('TTestModule.CheckSource InitStatements="',InitStatements,'"');
  1024. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1025. end;
  1026. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  1027. // search diff, ignore changes in spaces
  1028. const
  1029. SpaceChars = [#9,#10,#13,' '];
  1030. var
  1031. ExpectedP, ActualP: PChar;
  1032. function FindLineEnd(p: PChar): PChar;
  1033. begin
  1034. Result:=p;
  1035. while not (Result^ in [#0,#10,#13]) do inc(Result);
  1036. end;
  1037. function FindLineStart(p, MinP: PChar): PChar;
  1038. begin
  1039. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  1040. Result:=p;
  1041. end;
  1042. procedure DiffFound;
  1043. var
  1044. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  1045. ExpLine, ActLine: String;
  1046. i: Integer;
  1047. begin
  1048. writeln('Diff found "',Msg,'". Lines:');
  1049. // write correct lines
  1050. p:=PChar(Expected);
  1051. repeat
  1052. StartPos:=p;
  1053. while not (p^ in [#0,#10,#13]) do inc(p);
  1054. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  1055. if p^ in [#10,#13] then begin
  1056. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  1057. inc(p,2)
  1058. else
  1059. inc(p);
  1060. end;
  1061. if p<=ExpectedP then begin
  1062. writeln('= ',ExpLine);
  1063. end else begin
  1064. // diff line
  1065. // write actual line
  1066. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  1067. ActLineEndP:=FindLineEnd(ActualP);
  1068. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1069. writeln('- ',ActLine);
  1070. // write expected line
  1071. writeln('+ ',ExpLine);
  1072. // write empty line with pointer ^
  1073. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  1074. writeln('^');
  1075. AssertEquals(Msg,ExpLine,ActLine);
  1076. break;
  1077. end;
  1078. until p^=#0;
  1079. Fail('diff found, but lines are the same, internal error');
  1080. end;
  1081. var
  1082. IsSpaceNeeded: Boolean;
  1083. LastChar: Char;
  1084. begin
  1085. if Expected='' then Expected:=' ';
  1086. if Actual='' then Actual:=' ';
  1087. ExpectedP:=PChar(Expected);
  1088. ActualP:=PChar(Actual);
  1089. repeat
  1090. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  1091. case ExpectedP^ of
  1092. #0:
  1093. begin
  1094. // check that rest of Actual has only spaces
  1095. while ActualP^ in SpaceChars do inc(ActualP);
  1096. if ActualP^<>#0 then
  1097. DiffFound;
  1098. exit;
  1099. end;
  1100. ' ',#9,#10,#13:
  1101. begin
  1102. // skip space in Expected
  1103. IsSpaceNeeded:=false;
  1104. if ExpectedP>PChar(Expected) then
  1105. LastChar:=ExpectedP[-1]
  1106. else
  1107. LastChar:=#0;
  1108. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1109. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1110. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1111. IsSpaceNeeded:=true;
  1112. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1113. DiffFound;
  1114. while ActualP^ in SpaceChars do inc(ActualP);
  1115. end;
  1116. else
  1117. while ActualP^ in SpaceChars do inc(ActualP);
  1118. if ExpectedP^<>ActualP^ then
  1119. DiffFound;
  1120. inc(ExpectedP);
  1121. inc(ActualP);
  1122. end;
  1123. until false;
  1124. end;
  1125. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  1126. MsgNumber: integer);
  1127. begin
  1128. ExpectedErrorClass:=EScannerError;
  1129. ExpectedErrorMsg:=Msg;
  1130. ExpectedErrorNumber:=MsgNumber;
  1131. end;
  1132. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  1133. MsgNumber: integer);
  1134. begin
  1135. ExpectedErrorClass:=EParserError;
  1136. ExpectedErrorMsg:=Msg;
  1137. ExpectedErrorNumber:=MsgNumber;
  1138. end;
  1139. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  1140. MsgNumber: integer);
  1141. begin
  1142. ExpectedErrorClass:=EPasResolve;
  1143. ExpectedErrorMsg:=Msg;
  1144. ExpectedErrorNumber:=MsgNumber;
  1145. end;
  1146. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  1147. MsgNumber: integer);
  1148. begin
  1149. ExpectedErrorClass:=EPas2JS;
  1150. ExpectedErrorMsg:=Msg;
  1151. ExpectedErrorNumber:=MsgNumber;
  1152. end;
  1153. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  1154. var
  1155. MsgNumber: Integer;
  1156. begin
  1157. Result:=false;
  1158. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  1159. if E is EPas2JS then
  1160. MsgNumber:=EPas2JS(E).MsgNumber
  1161. else if E is EPasResolve then
  1162. MsgNumber:=EPasResolve(E).MsgNumber
  1163. else if E is EParserError then
  1164. MsgNumber:=Parser.LastMsgNumber
  1165. else if E is EScannerError then
  1166. MsgNumber:=Scanner.LastMsgNumber
  1167. else
  1168. MsgNumber:=0;
  1169. Result:=(MsgNumber=ExpectedErrorNumber) and (E.Message=ExpectedErrorMsg);
  1170. if Result then
  1171. SkipTests:=true;
  1172. end;
  1173. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  1174. begin
  1175. if IsErrorExpected(E) then exit;
  1176. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  1177. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  1178. +' '+Scanner.CurFilename
  1179. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  1180. RaiseException(E);
  1181. end;
  1182. procedure TCustomTestModule.HandleParserError(E: EParserError);
  1183. begin
  1184. if IsErrorExpected(E) then exit;
  1185. WriteSources(E.Filename,E.Row,E.Column);
  1186. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  1187. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  1188. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  1189. );
  1190. RaiseException(E);
  1191. end;
  1192. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  1193. var
  1194. P: TPasSourcePos;
  1195. begin
  1196. if IsErrorExpected(E) then exit;
  1197. P:=E.SourcePos;
  1198. WriteSources(P.FileName,P.Row,P.Column);
  1199. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  1200. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  1201. RaiseException(E);
  1202. end;
  1203. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  1204. var
  1205. Row, Col: integer;
  1206. begin
  1207. if IsErrorExpected(E) then exit;
  1208. Engine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  1209. WriteSources(E.PasElement.SourceFilename,Row,Col);
  1210. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  1211. +' '+E.PasElement.SourceFilename
  1212. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  1213. RaiseException(E);
  1214. end;
  1215. procedure TCustomTestModule.HandleException(E: Exception);
  1216. begin
  1217. if E is EScannerError then
  1218. HandleScannerError(EScannerError(E))
  1219. else if E is EParserError then
  1220. HandleParserError(EParserError(E))
  1221. else if E is EPasResolve then
  1222. HandlePasResolveError(EPasResolve(E))
  1223. else if E is EPas2JS then
  1224. HandlePas2JSError(EPas2JS(E))
  1225. else
  1226. begin
  1227. if IsErrorExpected(E) then exit;
  1228. if not (E is EAssertionFailedError) then
  1229. begin
  1230. WriteSources('',0,0);
  1231. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  1232. end;
  1233. RaiseException(E);
  1234. end;
  1235. end;
  1236. procedure TCustomTestModule.RaiseException(E: Exception);
  1237. var
  1238. MsgNumber: Integer;
  1239. begin
  1240. if ExpectedErrorClass<>nil then begin
  1241. if FExpectedErrorClass=E.ClassType then begin
  1242. if E is EPas2JS then
  1243. MsgNumber:=EPas2JS(E).MsgNumber
  1244. else if E is EPasResolve then
  1245. MsgNumber:=EPasResolve(E).MsgNumber
  1246. else if E is EParserError then
  1247. MsgNumber:=Parser.LastMsgNumber
  1248. else if E is EScannerError then
  1249. MsgNumber:=Scanner.LastMsgNumber
  1250. else
  1251. MsgNumber:=0;
  1252. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  1253. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  1254. ExpectedErrorNumber,MsgNumber);
  1255. end else begin
  1256. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  1257. end;
  1258. end;
  1259. Fail(E.Message);
  1260. end;
  1261. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  1262. aCol: integer);
  1263. var
  1264. IsSrc: Boolean;
  1265. i, j: Integer;
  1266. SrcLines: TStringList;
  1267. Line: string;
  1268. aModule: TTestEnginePasResolver;
  1269. begin
  1270. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  1271. for i:=0 to ModuleCount-1 do
  1272. begin
  1273. aModule:=Modules[i];
  1274. SrcLines:=TStringList.Create;
  1275. try
  1276. SrcLines.Text:=aModule.Source;
  1277. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  1278. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  1279. for j:=1 to SrcLines.Count do
  1280. begin
  1281. Line:=SrcLines[j-1];
  1282. if IsSrc and (j=aRow) then
  1283. begin
  1284. write('*');
  1285. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  1286. end;
  1287. writeln(Format('%:4d: ',[j]),Line);
  1288. end;
  1289. finally
  1290. SrcLines.Free;
  1291. end;
  1292. end;
  1293. end;
  1294. function TCustomTestModule.GetDefaultNamespace: string;
  1295. var
  1296. C: TClass;
  1297. begin
  1298. Result:='';
  1299. if FModule=nil then exit;
  1300. C:=FModule.ClassType;
  1301. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  1302. Result:=Engine.DefaultNameSpace;
  1303. end;
  1304. { TTestModule }
  1305. procedure TTestModule.TestEmptyProgram;
  1306. begin
  1307. StartProgram(false);
  1308. Add('begin');
  1309. ConvertProgram;
  1310. CheckSource('TestEmptyProgram','','');
  1311. end;
  1312. procedure TTestModule.TestEmptyProgramUseStrict;
  1313. begin
  1314. Converter.Options:=Converter.Options+[coUseStrict];
  1315. StartProgram(false);
  1316. Add('begin');
  1317. ConvertProgram;
  1318. CheckSource('TestEmptyProgramUseStrict','','');
  1319. end;
  1320. procedure TTestModule.TestEmptyUnit;
  1321. begin
  1322. StartUnit(false);
  1323. Add('interface');
  1324. Add('implementation');
  1325. ConvertUnit;
  1326. CheckSource('TestEmptyUnit',
  1327. LinesToStr([
  1328. ]),
  1329. '');
  1330. end;
  1331. procedure TTestModule.TestEmptyUnitUseStrict;
  1332. begin
  1333. Converter.Options:=Converter.Options+[coUseStrict];
  1334. StartUnit(false);
  1335. Add('interface');
  1336. Add('implementation');
  1337. ConvertUnit;
  1338. CheckSource('TestEmptyUnitUseStrict',
  1339. LinesToStr([
  1340. ''
  1341. ]),
  1342. '');
  1343. end;
  1344. procedure TTestModule.TestDottedUnitNames;
  1345. begin
  1346. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  1347. LinesToStr([
  1348. 'var iV: longint;'
  1349. ]),
  1350. '');
  1351. FFilename:='ns1.test1.pp';
  1352. StartProgram(true);
  1353. Add('uses unIt2;');
  1354. Add('implementation');
  1355. Add('var');
  1356. Add(' i: longint;');
  1357. Add('begin');
  1358. Add(' i:=iv;');
  1359. Add(' i:=uNit2.iv;');
  1360. Add(' i:=Ns1.TEst1.i;');
  1361. ConvertProgram;
  1362. CheckSource('TestDottedUnitNames',
  1363. LinesToStr([
  1364. 'this.i = 0;',
  1365. '']),
  1366. LinesToStr([ // this.$init
  1367. '$mod.i = pas["NS1.Unit2"].iV;',
  1368. '$mod.i = pas["NS1.Unit2"].iV;',
  1369. '$mod.i = $mod.i;',
  1370. '']) );
  1371. end;
  1372. procedure TTestModule.TestDottedUnitExpr;
  1373. begin
  1374. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  1375. LinesToStr([
  1376. 'procedure DoIt;'
  1377. ]),
  1378. 'procedure DoIt; begin end;');
  1379. FFilename:='Ns1.SubNs1.Test1.pp';
  1380. StartProgram(true);
  1381. Add('uses Ns2.sUbnS2.unIt2;');
  1382. Add('implementation');
  1383. Add('var');
  1384. Add(' i: longint;');
  1385. Add('begin');
  1386. Add(' ns2.subns2.unit2.doit;');
  1387. Add(' i:=Ns1.SubNS1.TEst1.i;');
  1388. ConvertProgram;
  1389. CheckSource('TestDottedUnitExpr',
  1390. LinesToStr([
  1391. 'this.i = 0;',
  1392. '']),
  1393. LinesToStr([ // this.$init
  1394. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  1395. '$mod.i = $mod.i;',
  1396. '']) );
  1397. end;
  1398. procedure TTestModule.Test_ModeFPCFail;
  1399. begin
  1400. StartProgram(false);
  1401. Add('{$mode FPC}');
  1402. Add('begin');
  1403. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  1404. ConvertProgram;
  1405. end;
  1406. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  1407. begin
  1408. StartProgram(false);
  1409. Add('{$modeswitch cblocks-}');
  1410. Add('begin');
  1411. SetExpectedScannerError('Invalid mode switch: "cblocks-"',nErrInvalidModeSwitch);
  1412. ConvertProgram;
  1413. end;
  1414. procedure TTestModule.TestVarInt;
  1415. begin
  1416. StartProgram(false);
  1417. Add('var MyI: longint;');
  1418. Add('begin');
  1419. ConvertProgram;
  1420. CheckSource('TestVarInt','this.MyI=0;','');
  1421. end;
  1422. procedure TTestModule.TestVarBaseTypes;
  1423. begin
  1424. StartProgram(false);
  1425. Add('var');
  1426. Add(' i: longint;');
  1427. Add(' s: string;');
  1428. Add(' c: char;');
  1429. Add(' b: boolean;');
  1430. Add(' d: double;');
  1431. Add(' i2: longint = 3;');
  1432. Add(' s2: string = ''foo'';');
  1433. Add(' c2: char = ''4'';');
  1434. Add(' b2: boolean = true;');
  1435. Add(' d2: double = 5.6;');
  1436. Add(' i3: longint = $707;');
  1437. Add(' i4: nativeint = 4503599627370495;');
  1438. Add(' i5: nativeint = -4503599627370496;');
  1439. Add(' i6: nativeint = $fffffffffffff;');
  1440. Add(' i7: nativeint = -$10000000000000;');
  1441. Add(' u8: nativeuint = $fffffffffffff;');
  1442. Add(' u9: nativeuint = $0000000000000;');
  1443. Add('begin');
  1444. ConvertProgram;
  1445. CheckSource('TestVarBaseTypes',
  1446. LinesToStr([
  1447. 'this.i=0;',
  1448. 'this.s="";',
  1449. 'this.c="";',
  1450. 'this.b=false;',
  1451. 'this.d=0.0;',
  1452. 'this.i2=3;',
  1453. 'this.s2="foo";',
  1454. 'this.c2="4";',
  1455. 'this.b2=true;',
  1456. 'this.d2=5.6;',
  1457. 'this.i3=0x707;',
  1458. 'this.i4= 4503599627370495;',
  1459. 'this.i5= -4503599627370496;',
  1460. 'this.i6= 0xfffffffffffff;',
  1461. 'this.i7=-0x10000000000000;',
  1462. 'this.u8= 0xfffffffffffff;',
  1463. 'this.u9= 0x0000000000000;'
  1464. ]),
  1465. '');
  1466. end;
  1467. procedure TTestModule.TestBaseTypeSingleFail;
  1468. begin
  1469. StartProgram(false);
  1470. Add('var s: single;');
  1471. SetExpectedPasResolverError('identifier not found "single"',nIdentifierNotFound);
  1472. ConvertProgram;
  1473. end;
  1474. procedure TTestModule.TestBaseTypeExtendedFail;
  1475. begin
  1476. StartProgram(false);
  1477. Add('var e: extended;');
  1478. SetExpectedPasResolverError('identifier not found "extended"',nIdentifierNotFound);
  1479. ConvertProgram;
  1480. end;
  1481. procedure TTestModule.TestConstBaseTypes;
  1482. begin
  1483. StartProgram(false);
  1484. Add('const');
  1485. Add(' i: longint = 3;');
  1486. Add(' s: string = ''foo'';');
  1487. Add(' c: char = ''4'';');
  1488. Add(' b: boolean = true;');
  1489. Add(' d: double = 5.6;');
  1490. Add('begin');
  1491. ConvertProgram;
  1492. CheckSource('TestVarBaseTypes',
  1493. LinesToStr([
  1494. 'this.i=3;',
  1495. 'this.s="foo";',
  1496. 'this.c="4";',
  1497. 'this.b=true;',
  1498. 'this.d=5.6;'
  1499. ]),
  1500. '');
  1501. end;
  1502. procedure TTestModule.TestAliasTypeRef;
  1503. begin
  1504. StartProgram(false);
  1505. Add('type');
  1506. Add(' a=longint;');
  1507. Add(' b=a;');
  1508. Add('var');
  1509. Add(' c: A;');
  1510. Add(' d: B;');
  1511. Add('begin');
  1512. ConvertProgram;
  1513. CheckSource('TestAliasTypeRef',
  1514. LinesToStr([ // statements
  1515. 'this.c = 0;',
  1516. 'this.d = 0;'
  1517. ]),
  1518. LinesToStr([ // this.$main
  1519. ''
  1520. ]));
  1521. end;
  1522. procedure TTestModule.TestTypeCast_BaseTypes;
  1523. begin
  1524. StartProgram(false);
  1525. Add('var');
  1526. Add(' i: longint;');
  1527. Add(' b: boolean;');
  1528. Add(' d: double;');
  1529. Add(' s: string;');
  1530. Add(' c: char;');
  1531. Add('begin');
  1532. Add(' i:=longint(i);');
  1533. Add(' i:=longint(b);');
  1534. Add(' b:=boolean(b);');
  1535. Add(' b:=boolean(i);');
  1536. Add(' d:=double(d);');
  1537. Add(' d:=double(i);');
  1538. Add(' s:=string(s);');
  1539. Add(' s:=string(c);');
  1540. Add(' c:=char(c);');
  1541. ConvertProgram;
  1542. CheckSource('TestAliasTypeRef',
  1543. LinesToStr([ // statements
  1544. 'this.i = 0;',
  1545. 'this.b = false;',
  1546. 'this.d = 0.0;',
  1547. 'this.s = "";',
  1548. 'this.c = "";',
  1549. '']),
  1550. LinesToStr([ // this.$main
  1551. '$mod.i = $mod.i;',
  1552. '$mod.i = ($mod.b ? 1 : 0);',
  1553. '$mod.b = $mod.b;',
  1554. '$mod.b = $mod.i != 0;',
  1555. '$mod.d = $mod.d;',
  1556. '$mod.d = $mod.i;',
  1557. '$mod.s = $mod.s;',
  1558. '$mod.s = $mod.c;',
  1559. '$mod.c = $mod.c;',
  1560. '']));
  1561. end;
  1562. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  1563. begin
  1564. StartProgram(false);
  1565. Add('type');
  1566. Add(' integer = longint;');
  1567. Add(' TYesNo = boolean;');
  1568. Add(' TFloat = double;');
  1569. Add(' TCaption = string;');
  1570. Add(' TChar = char;');
  1571. Add('var');
  1572. Add(' i: integer;');
  1573. Add(' b: TYesNo;');
  1574. Add(' d: TFloat;');
  1575. Add(' s: TCaption;');
  1576. Add(' c: TChar;');
  1577. Add('begin');
  1578. Add(' i:=integer(i);');
  1579. Add(' i:=integer(b);');
  1580. Add(' b:=TYesNo(b);');
  1581. Add(' b:=TYesNo(i);');
  1582. Add(' d:=TFloat(d);');
  1583. Add(' d:=TFloat(i);');
  1584. Add(' s:=TCaption(s);');
  1585. Add(' s:=TCaption(c);');
  1586. Add(' c:=TChar(c);');
  1587. ConvertProgram;
  1588. CheckSource('TestAliasTypeRef',
  1589. LinesToStr([ // statements
  1590. 'this.i = 0;',
  1591. 'this.b = false;',
  1592. 'this.d = 0.0;',
  1593. 'this.s = "";',
  1594. 'this.c = "";',
  1595. '']),
  1596. LinesToStr([ // this.$main
  1597. '$mod.i = $mod.i;',
  1598. '$mod.i = ($mod.b ? 1 : 0);',
  1599. '$mod.b = $mod.b;',
  1600. '$mod.b = $mod.i != 0;',
  1601. '$mod.d = $mod.d;',
  1602. '$mod.d = $mod.i;',
  1603. '$mod.s = $mod.s;',
  1604. '$mod.s = $mod.c;',
  1605. '$mod.c = $mod.c;',
  1606. '']));
  1607. end;
  1608. procedure TTestModule.TestEmptyProc;
  1609. begin
  1610. StartProgram(false);
  1611. Add('procedure Test;');
  1612. Add('begin');
  1613. Add('end;');
  1614. Add('begin');
  1615. ConvertProgram;
  1616. CheckSource('TestEmptyProc',
  1617. LinesToStr([ // statements
  1618. 'this.Test = function () {',
  1619. '};'
  1620. ]),
  1621. LinesToStr([ // this.$main
  1622. ''
  1623. ]));
  1624. end;
  1625. procedure TTestModule.TestProcOneParam;
  1626. begin
  1627. StartProgram(false);
  1628. Add('procedure ProcA(i: longint);');
  1629. Add('begin');
  1630. Add('end;');
  1631. Add('begin');
  1632. Add(' PROCA(3);');
  1633. ConvertProgram;
  1634. CheckSource('TestProcOneParam',
  1635. LinesToStr([ // statements
  1636. 'this.ProcA = function (i) {',
  1637. '};'
  1638. ]),
  1639. LinesToStr([ // this.$main
  1640. '$mod.ProcA(3);'
  1641. ]));
  1642. end;
  1643. procedure TTestModule.TestFunctionWithoutParams;
  1644. begin
  1645. StartProgram(false);
  1646. Add('function FuncA: longint;');
  1647. Add('begin');
  1648. Add('end;');
  1649. Add('var i: longint;');
  1650. Add('begin');
  1651. Add(' I:=FUNCA();');
  1652. Add(' I:=FUNCA;');
  1653. Add(' FUNCA();');
  1654. Add(' FUNCA;');
  1655. ConvertProgram;
  1656. CheckSource('TestProcWithoutParams',
  1657. LinesToStr([ // statements
  1658. 'this.FuncA = function () {',
  1659. ' var Result = 0;',
  1660. ' return Result;',
  1661. '};',
  1662. 'this.i=0;'
  1663. ]),
  1664. LinesToStr([ // this.$main
  1665. '$mod.i=$mod.FuncA();',
  1666. '$mod.i=$mod.FuncA();',
  1667. '$mod.FuncA();',
  1668. '$mod.FuncA();'
  1669. ]));
  1670. end;
  1671. procedure TTestModule.TestProcedureWithoutParams;
  1672. begin
  1673. StartProgram(false);
  1674. Add('procedure ProcA;');
  1675. Add('begin');
  1676. Add('end;');
  1677. Add('begin');
  1678. Add(' PROCA();');
  1679. Add(' PROCA;');
  1680. ConvertProgram;
  1681. CheckSource('TestProcWithoutParams',
  1682. LinesToStr([ // statements
  1683. 'this.ProcA = function () {',
  1684. '};'
  1685. ]),
  1686. LinesToStr([ // this.$main
  1687. '$mod.ProcA();',
  1688. '$mod.ProcA();'
  1689. ]));
  1690. end;
  1691. procedure TTestModule.TestIncDec;
  1692. begin
  1693. StartProgram(false);
  1694. Add([
  1695. 'procedure DoIt(var i: longint);',
  1696. 'begin',
  1697. ' inc(i);',
  1698. ' inc(i,2);',
  1699. 'end;',
  1700. 'var',
  1701. ' Bar: longint;',
  1702. 'begin',
  1703. ' inc(bar);',
  1704. ' inc(bar,2);',
  1705. ' dec(bar);',
  1706. ' dec(bar,3);',
  1707. '']);
  1708. ConvertProgram;
  1709. CheckSource('TestIncDec',
  1710. LinesToStr([ // statements
  1711. 'this.DoIt = function (i) {',
  1712. ' i.set(i.get()+1);',
  1713. ' i.set(i.get()+2);',
  1714. '};',
  1715. 'this.Bar = 0;'
  1716. ]),
  1717. LinesToStr([ // this.$main
  1718. '$mod.Bar+=1;',
  1719. '$mod.Bar+=2;',
  1720. '$mod.Bar-=1;',
  1721. '$mod.Bar-=3;'
  1722. ]));
  1723. end;
  1724. procedure TTestModule.TestAssignments;
  1725. begin
  1726. StartProgram(false);
  1727. Parser.Options:=Parser.Options+[po_cassignments];
  1728. Add('var');
  1729. Add(' Bar:longint;');
  1730. Add('begin');
  1731. Add(' bar:=3;');
  1732. Add(' bar+=4;');
  1733. Add(' bar-=5;');
  1734. Add(' bar*=6;');
  1735. ConvertProgram;
  1736. CheckSource('TestAssignments',
  1737. LinesToStr([ // statements
  1738. 'this.Bar = 0;'
  1739. ]),
  1740. LinesToStr([ // this.$main
  1741. '$mod.Bar=3;',
  1742. '$mod.Bar+=4;',
  1743. '$mod.Bar-=5;',
  1744. '$mod.Bar*=6;'
  1745. ]));
  1746. end;
  1747. procedure TTestModule.TestArithmeticOperators1;
  1748. begin
  1749. StartProgram(false);
  1750. Add('var');
  1751. Add(' vA,vB,vC:longint;');
  1752. Add('begin');
  1753. Add(' va:=1;');
  1754. Add(' vb:=va+va;');
  1755. Add(' vb:=va div vb;');
  1756. Add(' vb:=va mod vb;');
  1757. Add(' vb:=va+va*vb+va div vb;');
  1758. Add(' vc:=-va;');
  1759. Add(' va:=va-vb;');
  1760. Add(' vb:=va;');
  1761. Add(' if va<vb then vc:=va else vc:=vb;');
  1762. ConvertProgram;
  1763. CheckSource('TestArithmeticOperators1',
  1764. LinesToStr([ // statements
  1765. 'this.vA = 0;',
  1766. 'this.vB = 0;',
  1767. 'this.vC = 0;'
  1768. ]),
  1769. LinesToStr([ // this.$main
  1770. '$mod.vA = 1;',
  1771. '$mod.vB = $mod.vA + $mod.vA;',
  1772. '$mod.vB = Math.floor($mod.vA / $mod.vB);',
  1773. '$mod.vB = $mod.vA % $mod.vB;',
  1774. '$mod.vB = ($mod.vA + ($mod.vA * $mod.vB)) + Math.floor($mod.vA / $mod.vB);',
  1775. '$mod.vC = -$mod.vA;',
  1776. '$mod.vA = $mod.vA - $mod.vB;',
  1777. '$mod.vB = $mod.vA;',
  1778. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  1779. ]));
  1780. end;
  1781. procedure TTestModule.TestLogicalOperators;
  1782. begin
  1783. StartProgram(false);
  1784. Add('var');
  1785. Add(' vA,vB,vC:boolean;');
  1786. Add('begin');
  1787. Add(' va:=vb and vc;');
  1788. Add(' va:=vb or vc;');
  1789. Add(' va:=true and vc;');
  1790. Add(' va:=(vb and vc) or (va and vb);');
  1791. Add(' va:=not vb;');
  1792. ConvertProgram;
  1793. CheckSource('TestLogicalOperators',
  1794. LinesToStr([ // statements
  1795. 'this.vA = false;',
  1796. 'this.vB = false;',
  1797. 'this.vC = false;'
  1798. ]),
  1799. LinesToStr([ // this.$main
  1800. '$mod.vA = $mod.vB && $mod.vC;',
  1801. '$mod.vA = $mod.vB || $mod.vC;',
  1802. '$mod.vA = true && $mod.vC;',
  1803. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  1804. '$mod.vA = !$mod.vB;'
  1805. ]));
  1806. end;
  1807. procedure TTestModule.TestBitwiseOperators;
  1808. begin
  1809. StartProgram(false);
  1810. Add('var');
  1811. Add(' vA,vB,vC:longint;');
  1812. Add('begin');
  1813. Add(' va:=vb and vc;');
  1814. Add(' va:=vb or vc;');
  1815. Add(' va:=vb xor vc;');
  1816. Add(' va:=vb shl vc;');
  1817. Add(' va:=vb shr vc;');
  1818. Add(' va:=3 and vc;');
  1819. Add(' va:=(vb and vc) or (va and vb);');
  1820. Add(' va:=not vb;');
  1821. ConvertProgram;
  1822. CheckSource('TestBitwiseOperators',
  1823. LinesToStr([ // statements
  1824. 'this.vA = 0;',
  1825. 'this.vB = 0;',
  1826. 'this.vC = 0;'
  1827. ]),
  1828. LinesToStr([ // this.$main
  1829. '$mod.vA = $mod.vB & $mod.vC;',
  1830. '$mod.vA = $mod.vB | $mod.vC;',
  1831. '$mod.vA = $mod.vB ^ $mod.vC;',
  1832. '$mod.vA = $mod.vB << $mod.vC;',
  1833. '$mod.vA = $mod.vB >>> $mod.vC;',
  1834. '$mod.vA = 3 & $mod.vC;',
  1835. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  1836. '$mod.vA = ~$mod.vB;'
  1837. ]));
  1838. end;
  1839. procedure TTestModule.TestPrgProcVar;
  1840. begin
  1841. StartProgram(false);
  1842. Add('procedure Proc1;');
  1843. Add('type');
  1844. Add(' t1=longint;');
  1845. Add('var');
  1846. Add(' vA:t1;');
  1847. Add('begin');
  1848. Add('end;');
  1849. Add('begin');
  1850. ConvertProgram;
  1851. CheckSource('TestPrgProcVar',
  1852. LinesToStr([ // statements
  1853. 'this.Proc1 = function () {',
  1854. ' var vA=0;',
  1855. '};'
  1856. ]),
  1857. LinesToStr([ // this.$main
  1858. ''
  1859. ]));
  1860. end;
  1861. procedure TTestModule.TestUnitProcVar;
  1862. begin
  1863. StartUnit(false);
  1864. Add('interface');
  1865. Add('');
  1866. Add('type tA=string; // unit scope');
  1867. Add('procedure Proc1;');
  1868. Add('');
  1869. Add('implementation');
  1870. Add('');
  1871. Add('procedure Proc1;');
  1872. Add('type tA=longint; // local proc scope');
  1873. Add('var v1:tA; // using local tA');
  1874. Add('begin');
  1875. Add('end;');
  1876. Add('var v2:tA; // using interface tA');
  1877. ConvertUnit;
  1878. CheckSource('TestUnitProcVar',
  1879. LinesToStr([ // statements
  1880. 'var $impl = $mod.$impl;',
  1881. 'this.Proc1 = function () {',
  1882. ' var v1 = 0;',
  1883. '};',
  1884. '']),
  1885. // this.$init
  1886. '',
  1887. // implementation
  1888. LinesToStr([
  1889. '$impl.v2 = "";',
  1890. '']));
  1891. end;
  1892. procedure TTestModule.TestImplProc;
  1893. begin
  1894. StartUnit(false);
  1895. Add('interface');
  1896. Add('');
  1897. Add('procedure Proc1;');
  1898. Add('');
  1899. Add('implementation');
  1900. Add('');
  1901. Add('procedure Proc1; begin end;');
  1902. Add('procedure Proc2; begin end;');
  1903. Add('initialization');
  1904. Add(' Proc1;');
  1905. Add(' Proc2;');
  1906. ConvertUnit;
  1907. CheckSource('TestImplProc',
  1908. LinesToStr([ // statements
  1909. 'var $impl = $mod.$impl;',
  1910. 'this.Proc1 = function () {',
  1911. '};',
  1912. '']),
  1913. LinesToStr([ // this.$init
  1914. '$mod.Proc1();',
  1915. '$impl.Proc2();',
  1916. '']),
  1917. LinesToStr([ // implementation
  1918. '$impl.Proc2 = function () {',
  1919. '};',
  1920. ''])
  1921. );
  1922. end;
  1923. procedure TTestModule.TestFunctionResult;
  1924. begin
  1925. StartProgram(false);
  1926. Add('function Func1: longint;');
  1927. Add('begin');
  1928. Add(' Result:=3;');
  1929. Add('end;');
  1930. Add('begin');
  1931. ConvertProgram;
  1932. CheckSource('TestFunctionResult',
  1933. LinesToStr([ // statements
  1934. 'this.Func1 = function () {',
  1935. ' var Result = 0;',
  1936. ' Result = 3;',
  1937. ' return Result;',
  1938. '};'
  1939. ]),
  1940. '');
  1941. end;
  1942. procedure TTestModule.TestNestedProc;
  1943. begin
  1944. StartProgram(false);
  1945. Add('var vInUnit: longint;');
  1946. Add('function DoIt(pA,pD: longint): longint;');
  1947. Add('var');
  1948. Add(' vB: longint;');
  1949. Add(' vC: longint;');
  1950. Add(' function Nesty(pA: longint): longint; ');
  1951. Add(' var vB: longint;');
  1952. Add(' begin');
  1953. Add(' Result:=pa+vb+vc+pd+vInUnit;');
  1954. Add(' end;');
  1955. Add('begin');
  1956. Add(' Result:=pa+vb+vc;');
  1957. Add('end;');
  1958. Add('begin');
  1959. ConvertProgram;
  1960. CheckSource('TestNestedProc',
  1961. LinesToStr([ // statements
  1962. 'this.vInUnit = 0;',
  1963. 'this.DoIt = function (pA, pD) {',
  1964. ' var Result = 0;',
  1965. ' var vB = 0;',
  1966. ' var vC = 0;',
  1967. ' function Nesty(pA) {',
  1968. ' var Result = 0;',
  1969. ' var vB = 0;',
  1970. ' Result = (((pA + vB) + vC) + pD) + $mod.vInUnit;',
  1971. ' return Result;',
  1972. ' };',
  1973. ' Result = (pA + vB) + vC;',
  1974. ' return Result;',
  1975. '};'
  1976. ]),
  1977. '');
  1978. end;
  1979. procedure TTestModule.TestForwardProc;
  1980. begin
  1981. StartProgram(false);
  1982. Add('procedure FuncA(Bar: longint); forward;');
  1983. Add('procedure FuncB(Bar: longint);');
  1984. Add('begin');
  1985. Add(' funca(bar);');
  1986. Add('end;');
  1987. Add('procedure funca(bar: longint);');
  1988. Add('begin');
  1989. Add(' if bar=3 then ;');
  1990. Add('end;');
  1991. Add('begin');
  1992. Add(' funca(4);');
  1993. Add(' funcb(5);');
  1994. ConvertProgram;
  1995. CheckSource('TestForwardProc',
  1996. LinesToStr([ // statements'
  1997. 'this.FuncB = function (Bar) {',
  1998. ' $mod.FuncA(Bar);',
  1999. '};',
  2000. 'this.FuncA = function (Bar) {',
  2001. ' if (Bar == 3);',
  2002. '};'
  2003. ]),
  2004. LinesToStr([
  2005. '$mod.FuncA(4);',
  2006. '$mod.FuncB(5);'
  2007. ])
  2008. );
  2009. end;
  2010. procedure TTestModule.TestNestedForwardProc;
  2011. begin
  2012. StartProgram(false);
  2013. Add('procedure FuncA;');
  2014. Add(' procedure FuncB(i: longint); forward;');
  2015. Add(' procedure FuncC(i: longint);');
  2016. Add(' begin');
  2017. Add(' funcb(i);');
  2018. Add(' end;');
  2019. Add(' procedure FuncB(i: longint);');
  2020. Add(' begin');
  2021. Add(' if i=3 then ;');
  2022. Add(' end;');
  2023. Add('begin');
  2024. Add(' funcc(4)');
  2025. Add('end;');
  2026. Add('begin');
  2027. Add(' funca;');
  2028. ConvertProgram;
  2029. CheckSource('TestNestedForwardProc',
  2030. LinesToStr([ // statements'
  2031. 'this.FuncA = function () {',
  2032. ' function FuncC(i) {',
  2033. ' FuncB(i);',
  2034. ' };',
  2035. ' function FuncB(i) {',
  2036. ' if (i == 3);',
  2037. ' };',
  2038. ' FuncC(4);',
  2039. '};'
  2040. ]),
  2041. LinesToStr([
  2042. '$mod.FuncA();'
  2043. ])
  2044. );
  2045. end;
  2046. procedure TTestModule.TestAssignFunctionResult;
  2047. begin
  2048. StartProgram(false);
  2049. Add('function Func1: longint;');
  2050. Add('begin');
  2051. Add('end;');
  2052. Add('var i: longint;');
  2053. Add('begin');
  2054. Add(' i:=func1();');
  2055. Add(' i:=func1()+func1();');
  2056. ConvertProgram;
  2057. CheckSource('TestAssignFunctionResult',
  2058. LinesToStr([ // statements
  2059. 'this.Func1 = function () {',
  2060. ' var Result = 0;',
  2061. ' return Result;',
  2062. '};',
  2063. 'this.i = 0;'
  2064. ]),
  2065. LinesToStr([
  2066. '$mod.i = $mod.Func1();',
  2067. '$mod.i = $mod.Func1() + $mod.Func1();'
  2068. ]));
  2069. end;
  2070. procedure TTestModule.TestFunctionResultInCondition;
  2071. begin
  2072. StartProgram(false);
  2073. Add('function Func1: longint;');
  2074. Add('begin');
  2075. Add('end;');
  2076. Add('function Func2: boolean;');
  2077. Add('begin');
  2078. Add('end;');
  2079. Add('var i: longint;');
  2080. Add('begin');
  2081. Add(' if func2 then ;');
  2082. Add(' if i=func1() then ;');
  2083. Add(' if i=func1 then ;');
  2084. ConvertProgram;
  2085. CheckSource('TestFunctionResultInCondition',
  2086. LinesToStr([ // statements
  2087. 'this.Func1 = function () {',
  2088. ' var Result = 0;',
  2089. ' return Result;',
  2090. '};',
  2091. 'this.Func2 = function () {',
  2092. ' var Result = false;',
  2093. ' return Result;',
  2094. '};',
  2095. 'this.i = 0;'
  2096. ]),
  2097. LinesToStr([
  2098. 'if ($mod.Func2());',
  2099. 'if ($mod.i == $mod.Func1());',
  2100. 'if ($mod.i == $mod.Func1());'
  2101. ]));
  2102. end;
  2103. procedure TTestModule.TestExit;
  2104. begin
  2105. StartProgram(false);
  2106. Add('procedure ProcA;');
  2107. Add('begin');
  2108. Add(' exit;');
  2109. Add('end;');
  2110. Add('function FuncB: longint;');
  2111. Add('begin');
  2112. Add(' exit;');
  2113. Add(' exit(3);');
  2114. Add('end;');
  2115. Add('function FuncC: string;');
  2116. Add('begin');
  2117. Add(' exit;');
  2118. Add(' exit(''a'');');
  2119. Add(' exit(''abc'');');
  2120. Add('end;');
  2121. Add('begin');
  2122. Add(' exit;');
  2123. Add(' exit(1);');
  2124. ConvertProgram;
  2125. CheckSource('TestExit',
  2126. LinesToStr([ // statements
  2127. 'this.ProcA = function () {',
  2128. ' return;',
  2129. '};',
  2130. 'this.FuncB = function () {',
  2131. ' var Result = 0;',
  2132. ' return Result;',
  2133. ' return 3;',
  2134. ' return Result;',
  2135. '};',
  2136. 'this.FuncC = function () {',
  2137. ' var Result = "";',
  2138. ' return Result;',
  2139. ' return "a";',
  2140. ' return "abc";',
  2141. ' return Result;',
  2142. '};'
  2143. ]),
  2144. LinesToStr([
  2145. 'return;',
  2146. 'return 1;',
  2147. '']));
  2148. end;
  2149. procedure TTestModule.TestBreak;
  2150. begin
  2151. StartProgram(false);
  2152. Add('var i: longint;');
  2153. Add('begin');
  2154. Add(' repeat');
  2155. Add(' break;');
  2156. Add(' until true;');
  2157. Add(' while true do');
  2158. Add(' break;');
  2159. Add(' for i:=1 to 2 do');
  2160. Add(' break;');
  2161. ConvertProgram;
  2162. CheckSource('TestBreak',
  2163. LinesToStr([ // statements
  2164. 'this.i = 0;'
  2165. ]),
  2166. LinesToStr([
  2167. 'do {',
  2168. ' break;',
  2169. '} while (!true);',
  2170. 'while (true) break;',
  2171. 'var $loopend1 = 2;',
  2172. 'for ($mod.i = 1; $mod.i <= $loopend1; $mod.i++) break;',
  2173. 'if ($mod.i > $loopend1) $mod.i--;'
  2174. ]));
  2175. end;
  2176. procedure TTestModule.TestContinue;
  2177. begin
  2178. StartProgram(false);
  2179. Add('var i: longint;');
  2180. Add('begin');
  2181. Add(' repeat');
  2182. Add(' continue;');
  2183. Add(' until true;');
  2184. Add(' while true do');
  2185. Add(' continue;');
  2186. Add(' for i:=1 to 2 do');
  2187. Add(' continue;');
  2188. ConvertProgram;
  2189. CheckSource('TestContinue',
  2190. LinesToStr([ // statements
  2191. 'this.i = 0;'
  2192. ]),
  2193. LinesToStr([
  2194. 'do {',
  2195. ' continue;',
  2196. '} while (!true);',
  2197. 'while (true) continue;',
  2198. 'var $loopend1 = 2;',
  2199. 'for ($mod.i = 1; $mod.i <= $loopend1; $mod.i++) continue;',
  2200. 'if ($mod.i > $loopend1) $mod.i--;'
  2201. ]));
  2202. end;
  2203. procedure TTestModule.TestProc_External;
  2204. begin
  2205. StartProgram(false);
  2206. Add('procedure Foo; external name ''console.log'';');
  2207. Add('function Bar: longint; external name ''get.item'';');
  2208. Add('function Bla(s: string): longint; external name ''apply.something'';');
  2209. Add('var');
  2210. Add(' i: longint;');
  2211. Add('begin');
  2212. Add(' Foo;');
  2213. Add(' i:=Bar;');
  2214. Add(' i:=Bla(''abc'');');
  2215. ConvertProgram;
  2216. CheckSource('TestProcedureExternal',
  2217. LinesToStr([ // statements
  2218. 'this.i = 0;'
  2219. ]),
  2220. LinesToStr([
  2221. 'console.log();',
  2222. '$mod.i = get.item();',
  2223. '$mod.i = apply.something("abc");'
  2224. ]));
  2225. end;
  2226. procedure TTestModule.TestProc_ExternalOtherUnit;
  2227. begin
  2228. AddModuleWithIntfImplSrc('unit2.pas',
  2229. LinesToStr([
  2230. 'procedure Now; external name ''Date.now'';',
  2231. 'procedure DoIt;'
  2232. ]),
  2233. 'procedure doit; begin end;');
  2234. StartUnit(true);
  2235. Add('interface');
  2236. Add('uses unit2;');
  2237. Add('implementation');
  2238. Add('begin');
  2239. Add(' now;');
  2240. Add(' now();');
  2241. Add(' uNit2.now;');
  2242. Add(' uNit2.now();');
  2243. Add(' doit;');
  2244. Add(' uNit2.doit;');
  2245. ConvertUnit;
  2246. CheckSource('TestProcedureExternalOtherUnit',
  2247. LinesToStr([
  2248. '']),
  2249. LinesToStr([
  2250. 'Date.now();',
  2251. 'Date.now();',
  2252. 'Date.now();',
  2253. 'Date.now();',
  2254. 'pas.unit2.DoIt();',
  2255. 'pas.unit2.DoIt();',
  2256. '']));
  2257. end;
  2258. procedure TTestModule.TestProc_Asm;
  2259. begin
  2260. StartProgram(false);
  2261. Add('function DoIt: longint;');
  2262. Add('begin;');
  2263. Add(' asm');
  2264. Add(' { a:{ b:{}, c:[]}, d:''1'' };');
  2265. Add(' end;');
  2266. Add('end;');
  2267. Add('begin');
  2268. ConvertProgram;
  2269. CheckSource('TestProcedureAsm',
  2270. LinesToStr([ // statements
  2271. 'this.DoIt = function () {',
  2272. ' var Result = 0;',
  2273. ' { a:{ b:{}, c:[]}, d:''1'' };',
  2274. ' return Result;',
  2275. '};'
  2276. ]),
  2277. LinesToStr([
  2278. ''
  2279. ]));
  2280. end;
  2281. procedure TTestModule.TestProc_Assembler;
  2282. begin
  2283. StartProgram(false);
  2284. Add('function DoIt: longint; assembler;');
  2285. Add('asm');
  2286. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  2287. Add('end;');
  2288. Add('begin');
  2289. ConvertProgram;
  2290. CheckSource('TestProcedureAssembler',
  2291. LinesToStr([ // statements
  2292. 'this.DoIt = function () {',
  2293. ' { a:{ b:{}, c:[]}, d:''1'' };',
  2294. '};'
  2295. ]),
  2296. LinesToStr([
  2297. ''
  2298. ]));
  2299. end;
  2300. procedure TTestModule.TestProc_VarParam;
  2301. begin
  2302. StartProgram(false);
  2303. Add('type integer = longint;');
  2304. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  2305. Add('var vJ: integer;');
  2306. Add('begin');
  2307. Add(' vg:=vg+1;');
  2308. Add(' vj:=vh+2;');
  2309. Add(' vi:=vi+3;');
  2310. Add(' doit(vg,vg,vg);');
  2311. Add(' doit(vh,vh,vj);');
  2312. Add(' doit(vi,vi,vi);');
  2313. Add(' doit(vj,vj,vj);');
  2314. Add('end;');
  2315. Add('var i: integer;');
  2316. Add('begin');
  2317. Add(' doit(i,i,i);');
  2318. ConvertProgram;
  2319. CheckSource('TestProcedure_VarParam',
  2320. LinesToStr([ // statements
  2321. 'this.DoIt = function (vG,vH,vI) {',
  2322. ' var vJ = 0;',
  2323. ' vG = vG + 1;',
  2324. ' vJ = vH + 2;',
  2325. ' vI.set(vI.get()+3);',
  2326. ' $mod.DoIt(vG, vG, {',
  2327. ' get: function () {',
  2328. ' return vG;',
  2329. ' },',
  2330. ' set: function (v) {',
  2331. ' vG = v;',
  2332. ' }',
  2333. ' });',
  2334. ' $mod.DoIt(vH, vH, {',
  2335. ' get: function () {',
  2336. ' return vJ;',
  2337. ' },',
  2338. ' set: function (v) {',
  2339. ' vJ = v;',
  2340. ' }',
  2341. ' });',
  2342. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  2343. ' $mod.DoIt(vJ, vJ, {',
  2344. ' get: function () {',
  2345. ' return vJ;',
  2346. ' },',
  2347. ' set: function (v) {',
  2348. ' vJ = v;',
  2349. ' }',
  2350. ' });',
  2351. '};',
  2352. 'this.i = 0;'
  2353. ]),
  2354. LinesToStr([
  2355. '$mod.DoIt($mod.i,$mod.i,{',
  2356. ' p: $mod,',
  2357. ' get: function () {',
  2358. ' return this.p.i;',
  2359. ' },',
  2360. ' set: function (v) {',
  2361. ' this.p.i = v;',
  2362. ' }',
  2363. '});'
  2364. ]));
  2365. end;
  2366. procedure TTestModule.TestProc_Overload;
  2367. begin
  2368. StartProgram(false);
  2369. Add('procedure DoIt(vI: longint); begin end;');
  2370. Add('procedure DoIt(vI, vJ: longint); begin end;');
  2371. Add('procedure DoIt(vD: double); begin end;');
  2372. Add('begin');
  2373. Add(' DoIt(1);');
  2374. Add(' DoIt(2,3);');
  2375. Add(' DoIt(4.5);');
  2376. ConvertProgram;
  2377. CheckSource('TestProcedureOverload',
  2378. LinesToStr([ // statements
  2379. 'this.DoIt = function (vI) {',
  2380. '};',
  2381. 'this.DoIt$1 = function (vI, vJ) {',
  2382. '};',
  2383. 'this.DoIt$2 = function (vD) {',
  2384. '};',
  2385. '']),
  2386. LinesToStr([
  2387. '$mod.DoIt(1);',
  2388. '$mod.DoIt$1(2, 3);',
  2389. '$mod.DoIt$2(4.5);',
  2390. '']));
  2391. end;
  2392. procedure TTestModule.TestProc_OverloadForward;
  2393. begin
  2394. StartProgram(false);
  2395. Add('procedure DoIt(vI: longint); forward;');
  2396. Add('procedure DoIt(vI, vJ: longint); begin end;');
  2397. Add('procedure doit(vi: longint); begin end;');
  2398. Add('begin');
  2399. Add(' doit(1);');
  2400. Add(' doit(2,3);');
  2401. ConvertProgram;
  2402. CheckSource('TestProcedureOverloadForward',
  2403. LinesToStr([ // statements
  2404. 'this.DoIt$1 = function (vI, vJ) {',
  2405. '};',
  2406. 'this.DoIt = function (vI) {',
  2407. '};',
  2408. '']),
  2409. LinesToStr([
  2410. '$mod.DoIt(1);',
  2411. '$mod.DoIt$1(2, 3);',
  2412. '']));
  2413. end;
  2414. procedure TTestModule.TestProc_OverloadUnit;
  2415. begin
  2416. StartUnit(false);
  2417. Add('interface');
  2418. Add('procedure DoIt(vI: longint);');
  2419. Add('procedure DoIt(vI, vJ: longint);');
  2420. Add('implementation');
  2421. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  2422. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  2423. Add('procedure DoIt(vi: longint); begin end;');
  2424. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  2425. Add('procedure DoIt(vi, vj: longint); begin end;');
  2426. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  2427. Add('begin');
  2428. Add(' doit(1);');
  2429. Add(' doit(2,3);');
  2430. Add(' doit(4,5,6);');
  2431. Add(' doit(7,8,9,10);');
  2432. Add(' doit(11,12,13,14,15);');
  2433. ConvertUnit;
  2434. CheckSource('TestProcedureOverloadUnit',
  2435. LinesToStr([ // statements
  2436. 'var $impl = $mod.$impl;',
  2437. 'this.DoIt = function (vI) {',
  2438. '};',
  2439. 'this.DoIt$1 = function (vI, vJ) {',
  2440. '};',
  2441. '']),
  2442. LinesToStr([ // this.$init
  2443. '$mod.DoIt(1);',
  2444. '$mod.DoIt$1(2, 3);',
  2445. '$impl.DoIt$3(4,5,6);',
  2446. '$impl.DoIt$4(7,8,9,10);',
  2447. '$impl.DoIt$2(11,12,13,14,15);',
  2448. '']),
  2449. LinesToStr([ // implementation
  2450. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  2451. '};',
  2452. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  2453. '};',
  2454. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  2455. '};',
  2456. '']));
  2457. end;
  2458. procedure TTestModule.TestProc_OverloadNested;
  2459. begin
  2460. StartProgram(false);
  2461. Add('procedure DoIt(vA: longint); forward;');
  2462. Add('procedure DoIt(vB, vC: longint);');
  2463. Add('begin // 2 param overload');
  2464. Add(' doit(1);');
  2465. Add(' doit(1,2);');
  2466. Add('end;');
  2467. Add('procedure doit(vA: longint);');
  2468. Add(' procedure DoIt(vA, vB, vC: longint); forward;');
  2469. Add(' procedure DoIt(vA, vB, vC, vD: longint);');
  2470. Add(' begin // 4 param overload');
  2471. Add(' doit(1);');
  2472. Add(' doit(1,2);');
  2473. Add(' doit(1,2,3);');
  2474. Add(' doit(1,2,3,4);');
  2475. Add(' end;');
  2476. Add(' procedure doit(vA, vB, vC: longint);');
  2477. Add(' procedure DoIt(vA, vB, vC, vD, vE: longint); forward;');
  2478. Add(' procedure DoIt(vA, vB, vC, vD, vE, vF: longint);');
  2479. Add(' begin // 6 param overload');
  2480. Add(' doit(1);');
  2481. Add(' doit(1,2);');
  2482. Add(' doit(1,2,3);');
  2483. Add(' doit(1,2,3,4);');
  2484. Add(' doit(1,2,3,4,5);');
  2485. Add(' doit(1,2,3,4,5,6);');
  2486. Add(' end;');
  2487. Add(' procedure doit(vA, vB, vC, vD, vE: longint);');
  2488. Add(' begin // 5 param overload');
  2489. Add(' doit(1);');
  2490. Add(' doit(1,2);');
  2491. Add(' doit(1,2,3);');
  2492. Add(' doit(1,2,3,4);');
  2493. Add(' doit(1,2,3,4,5);');
  2494. Add(' doit(1,2,3,4,5,6);');
  2495. Add(' end;');
  2496. Add(' begin // 3 param overload');
  2497. Add(' doit(1);');
  2498. Add(' doit(1,2);');
  2499. Add(' doit(1,2,3);');
  2500. Add(' doit(1,2,3,4);');
  2501. Add(' doit(1,2,3,4,5);');
  2502. Add(' doit(1,2,3,4,5,6);');
  2503. Add(' end;');
  2504. Add('begin // 1 param overload');
  2505. Add(' doit(1);');
  2506. Add(' doit(1,2);');
  2507. Add(' doit(1,2,3);');
  2508. Add(' doit(1,2,3,4);');
  2509. Add('end;');
  2510. Add('begin // main');
  2511. Add(' doit(1);');
  2512. Add(' doit(1,2);');
  2513. ConvertProgram;
  2514. CheckSource('TestProcedureOverloadNested',
  2515. LinesToStr([ // statements
  2516. 'this.DoIt$1 = function (vB, vC) {',
  2517. ' $mod.DoIt(1);',
  2518. ' $mod.DoIt$1(1, 2);',
  2519. '};',
  2520. 'this.DoIt = function (vA) {',
  2521. ' function DoIt$3(vA, vB, vC, vD) {',
  2522. ' $mod.DoIt(1);',
  2523. ' $mod.DoIt$1(1, 2);',
  2524. ' DoIt$2(1, 2, 3);',
  2525. ' DoIt$3(1, 2, 3, 4);',
  2526. ' };',
  2527. ' function DoIt$2(vA, vB, vC) {',
  2528. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  2529. ' $mod.DoIt(1);',
  2530. ' $mod.DoIt$1(1, 2);',
  2531. ' DoIt$2(1, 2, 3);',
  2532. ' DoIt$3(1, 2, 3, 4);',
  2533. ' DoIt$4(1, 2, 3, 4, 5);',
  2534. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  2535. ' };',
  2536. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  2537. ' $mod.DoIt(1);',
  2538. ' $mod.DoIt$1(1, 2);',
  2539. ' DoIt$2(1, 2, 3);',
  2540. ' DoIt$3(1, 2, 3, 4);',
  2541. ' DoIt$4(1, 2, 3, 4, 5);',
  2542. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  2543. ' };',
  2544. ' $mod.DoIt(1);',
  2545. ' $mod.DoIt$1(1, 2);',
  2546. ' DoIt$2(1, 2, 3);',
  2547. ' DoIt$3(1, 2, 3, 4);',
  2548. ' DoIt$4(1, 2, 3, 4, 5);',
  2549. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  2550. ' };',
  2551. ' $mod.DoIt(1);',
  2552. ' $mod.DoIt$1(1, 2);',
  2553. ' DoIt$2(1, 2, 3);',
  2554. ' DoIt$3(1, 2, 3, 4);',
  2555. '};',
  2556. '']),
  2557. LinesToStr([
  2558. '$mod.DoIt(1);',
  2559. '$mod.DoIt$1(1, 2);',
  2560. '']));
  2561. end;
  2562. procedure TTestModule.TestProc_Varargs;
  2563. begin
  2564. StartProgram(false);
  2565. Add('procedure ProcA(i:longint); varargs; external name ''ProcA'';');
  2566. Add('procedure ProcB; varargs; external name ''ProcB'';');
  2567. Add('procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';');
  2568. Add('function GetIt: longint; begin end;');
  2569. Add('begin');
  2570. Add(' ProcA(1);');
  2571. Add(' ProcA(1,2);');
  2572. Add(' ProcA(1,2.0);');
  2573. Add(' ProcA(1,2,3);');
  2574. Add(' ProcA(1,''2'');');
  2575. Add(' ProcA(2,'''');');
  2576. Add(' ProcA(3,false);');
  2577. Add(' ProcB;');
  2578. Add(' ProcB();');
  2579. Add(' ProcB(4);');
  2580. Add(' ProcB(''foo'');');
  2581. Add(' ProcC;');
  2582. Add(' ProcC();');
  2583. Add(' ProcC(4);');
  2584. Add(' ProcC(5,''foo'');');
  2585. Add(' ProcB(GetIt);');
  2586. Add(' ProcB(GetIt());');
  2587. Add(' ProcB(GetIt,GetIt());');
  2588. ConvertProgram;
  2589. CheckSource('TestProc_Varargs',
  2590. LinesToStr([ // statements
  2591. 'this.GetIt = function () {',
  2592. ' var Result = 0;',
  2593. ' return Result;',
  2594. '};',
  2595. '']),
  2596. LinesToStr([
  2597. 'ProcA(1);',
  2598. 'ProcA(1, 2);',
  2599. 'ProcA(1, 2.0);',
  2600. 'ProcA(1, 2, 3);',
  2601. 'ProcA(1, "2");',
  2602. 'ProcA(2, "");',
  2603. 'ProcA(3, false);',
  2604. 'ProcB();',
  2605. 'ProcB();',
  2606. 'ProcB(4);',
  2607. 'ProcB("foo");',
  2608. 'ProcC(17);',
  2609. 'ProcC(17);',
  2610. 'ProcC(4);',
  2611. 'ProcC(5, "foo");',
  2612. 'ProcB($mod.GetIt());',
  2613. 'ProcB($mod.GetIt());',
  2614. 'ProcB($mod.GetIt(), $mod.GetIt());',
  2615. '']));
  2616. end;
  2617. procedure TTestModule.TestEnum_Name;
  2618. begin
  2619. StartProgram(false);
  2620. Add('type TMyEnum = (Red, Green, Blue);');
  2621. Add('var e: TMyEnum;');
  2622. Add('var f: TMyEnum = Blue;');
  2623. Add('begin');
  2624. Add(' e:=green;');
  2625. ConvertProgram;
  2626. CheckSource('TestEnumName',
  2627. LinesToStr([ // statements
  2628. 'this.TMyEnum = {',
  2629. ' "0":"Red",',
  2630. ' Red:0,',
  2631. ' "1":"Green",',
  2632. ' Green:1,',
  2633. ' "2":"Blue",',
  2634. ' Blue:2',
  2635. ' };',
  2636. 'this.e = 0;',
  2637. 'this.f = $mod.TMyEnum.Blue;'
  2638. ]),
  2639. LinesToStr([
  2640. '$mod.e=$mod.TMyEnum.Green;'
  2641. ]));
  2642. end;
  2643. procedure TTestModule.TestEnum_Number;
  2644. begin
  2645. Converter.Options:=Converter.Options+[coEnumNumbers];
  2646. StartProgram(false);
  2647. Add('type TMyEnum = (Red, Green);');
  2648. Add('var');
  2649. Add(' e: TMyEnum;');
  2650. Add(' f: TMyEnum = Green;');
  2651. Add('begin');
  2652. Add(' e:=green;');
  2653. ConvertProgram;
  2654. CheckSource('TestEnumNumber',
  2655. LinesToStr([ // statements
  2656. 'this.TMyEnum = {',
  2657. ' "0":"Red",',
  2658. ' Red:0,',
  2659. ' "1":"Green",',
  2660. ' Green:1',
  2661. ' };',
  2662. 'this.e = 0;',
  2663. 'this.f = 1;'
  2664. ]),
  2665. LinesToStr([
  2666. '$mod.e=1;'
  2667. ]));
  2668. end;
  2669. procedure TTestModule.TestEnum_Functions;
  2670. begin
  2671. StartProgram(false);
  2672. Add('type TMyEnum = (Red, Green);');
  2673. Add('var');
  2674. Add(' e: TMyEnum;');
  2675. Add(' i: longint;');
  2676. Add(' s: string;');
  2677. Add('begin');
  2678. Add(' i:=ord(red);');
  2679. Add(' i:=ord(green);');
  2680. Add(' i:=ord(e);');
  2681. Add(' e:=low(tmyenum);');
  2682. Add(' e:=low(e);');
  2683. Add(' e:=high(tmyenum);');
  2684. Add(' e:=high(e);');
  2685. Add(' e:=pred(green);');
  2686. Add(' e:=pred(e);');
  2687. Add(' e:=succ(red);');
  2688. Add(' e:=succ(e);');
  2689. Add(' e:=tmyenum(1);');
  2690. Add(' e:=tmyenum(i);');
  2691. Add(' s:=str(e);');
  2692. Add(' str(e,s)');
  2693. Add(' s:=str(e:3);');
  2694. ConvertProgram;
  2695. CheckSource('TestEnumNumber',
  2696. LinesToStr([ // statements
  2697. 'this.TMyEnum = {',
  2698. ' "0":"Red",',
  2699. ' Red:0,',
  2700. ' "1":"Green",',
  2701. ' Green:1',
  2702. ' };',
  2703. 'this.e = 0;',
  2704. 'this.i = 0;',
  2705. 'this.s = "";'
  2706. ]),
  2707. LinesToStr([
  2708. '$mod.i=$mod.TMyEnum.Red;',
  2709. '$mod.i=$mod.TMyEnum.Green;',
  2710. '$mod.i=$mod.e;',
  2711. '$mod.e=$mod.TMyEnum.Red;',
  2712. '$mod.e=$mod.TMyEnum.Red;',
  2713. '$mod.e=$mod.TMyEnum.Green;',
  2714. '$mod.e=$mod.TMyEnum.Green;',
  2715. '$mod.e=$mod.TMyEnum.Green-1;',
  2716. '$mod.e=$mod.e-1;',
  2717. '$mod.e=$mod.TMyEnum.Red+1;',
  2718. '$mod.e=$mod.e+1;',
  2719. '$mod.e=1;',
  2720. '$mod.e=$mod.i;',
  2721. '$mod.s = $mod.TMyEnum[$mod.e];',
  2722. '$mod.s = $mod.TMyEnum[$mod.e];',
  2723. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  2724. '']));
  2725. end;
  2726. procedure TTestModule.TestEnum_AsParams;
  2727. begin
  2728. StartProgram(false);
  2729. Add('type TEnum = (Red,Blue);');
  2730. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  2731. Add('var vJ: TEnum;');
  2732. Add('begin');
  2733. Add(' vg:=vg;');
  2734. Add(' vj:=vh;');
  2735. Add(' vi:=vi;');
  2736. Add(' doit(vg,vg,vg);');
  2737. Add(' doit(vh,vh,vj);');
  2738. Add(' doit(vi,vi,vi);');
  2739. Add(' doit(vj,vj,vj);');
  2740. Add('end;');
  2741. Add('var i: TEnum;');
  2742. Add('begin');
  2743. Add(' doit(i,i,i);');
  2744. ConvertProgram;
  2745. CheckSource('TestEnum_AsParams',
  2746. LinesToStr([ // statements
  2747. 'this.TEnum = {',
  2748. ' "0": "Red",',
  2749. ' Red: 0,',
  2750. ' "1": "Blue",',
  2751. ' Blue: 1',
  2752. '};',
  2753. 'this.DoIt = function (vG,vH,vI) {',
  2754. ' var vJ = 0;',
  2755. ' vG = vG;',
  2756. ' vJ = vH;',
  2757. ' vI.set(vI.get());',
  2758. ' $mod.DoIt(vG, vG, {',
  2759. ' get: function () {',
  2760. ' return vG;',
  2761. ' },',
  2762. ' set: function (v) {',
  2763. ' vG = v;',
  2764. ' }',
  2765. ' });',
  2766. ' $mod.DoIt(vH, vH, {',
  2767. ' get: function () {',
  2768. ' return vJ;',
  2769. ' },',
  2770. ' set: function (v) {',
  2771. ' vJ = v;',
  2772. ' }',
  2773. ' });',
  2774. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  2775. ' $mod.DoIt(vJ, vJ, {',
  2776. ' get: function () {',
  2777. ' return vJ;',
  2778. ' },',
  2779. ' set: function (v) {',
  2780. ' vJ = v;',
  2781. ' }',
  2782. ' });',
  2783. '};',
  2784. 'this.i = 0;'
  2785. ]),
  2786. LinesToStr([
  2787. '$mod.DoIt($mod.i,$mod.i,{',
  2788. ' p: $mod,',
  2789. ' get: function () {',
  2790. ' return this.p.i;',
  2791. ' },',
  2792. ' set: function (v) {',
  2793. ' this.p.i = v;',
  2794. ' }',
  2795. '});'
  2796. ]));
  2797. end;
  2798. procedure TTestModule.TestSet;
  2799. begin
  2800. StartProgram(false);
  2801. Add('type');
  2802. Add(' TColor = (Red, Green, Blue);');
  2803. Add(' TColors = set of TColor;');
  2804. Add('var');
  2805. Add(' c: TColor;');
  2806. Add(' s: TColors;');
  2807. Add(' t: TColors = [];');
  2808. Add(' u: TColors = [Red];');
  2809. Add('begin');
  2810. Add(' s:=[];');
  2811. Add(' s:=[Green];');
  2812. Add(' s:=[Green,Blue];');
  2813. Add(' s:=[Red..Blue];');
  2814. Add(' s:=[Red,Green..Blue];');
  2815. Add(' s:=[Red,c];');
  2816. Add(' s:=t;');
  2817. ConvertProgram;
  2818. CheckSource('TestEnumName',
  2819. LinesToStr([ // statements
  2820. 'this.TColor = {',
  2821. ' "0":"Red",',
  2822. ' Red:0,',
  2823. ' "1":"Green",',
  2824. ' Green:1,',
  2825. ' "2":"Blue",',
  2826. ' Blue:2',
  2827. ' };',
  2828. 'this.c = 0;',
  2829. 'this.s = {};',
  2830. 'this.t = {};',
  2831. 'this.u = rtl.createSet($mod.TColor.Red);'
  2832. ]),
  2833. LinesToStr([
  2834. '$mod.s={};',
  2835. '$mod.s=rtl.createSet($mod.TColor.Green);',
  2836. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  2837. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  2838. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  2839. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  2840. '$mod.s=rtl.refSet($mod.t);',
  2841. '']));
  2842. end;
  2843. procedure TTestModule.TestSet_Operators;
  2844. begin
  2845. StartProgram(false);
  2846. Add('type');
  2847. Add(' TColor = (Red, Green, Blue);');
  2848. Add(' TColors = set of tcolor;');
  2849. Add('var');
  2850. Add(' vC: TColor;');
  2851. Add(' vS: TColors;');
  2852. Add(' vT: TColors;');
  2853. Add(' vU: TColors;');
  2854. Add(' B: boolean;');
  2855. Add('begin');
  2856. Add(' include(vs,green);');
  2857. Add(' exclude(vs,vc);');
  2858. Add(' vs:=vt+vu;');
  2859. Add(' vs:=vt+[red];');
  2860. Add(' vs:=[red]+vt;');
  2861. Add(' vs:=[red]+[green];');
  2862. Add(' vs:=vt-vu;');
  2863. Add(' vs:=vt-[red];');
  2864. Add(' vs:=[red]-vt;');
  2865. Add(' vs:=[red]-[green];');
  2866. Add(' vs:=vt*vu;');
  2867. Add(' vs:=vt*[red];');
  2868. Add(' vs:=[red]*vt;');
  2869. Add(' vs:=[red]*[green];');
  2870. Add(' vs:=vt><vu;');
  2871. Add(' vs:=vt><[red];');
  2872. Add(' vs:=[red]><vt;');
  2873. Add(' vs:=[red]><[green];');
  2874. Add(' b:=vt=vu;');
  2875. Add(' b:=vt=[red];');
  2876. Add(' b:=[red]=vt;');
  2877. Add(' b:=[red]=[green];');
  2878. Add(' b:=vt<>vu;');
  2879. Add(' b:=vt<>[red];');
  2880. Add(' b:=[red]<>vt;');
  2881. Add(' b:=[red]<>[green];');
  2882. Add(' b:=vt<=vu;');
  2883. Add(' b:=vt<=[red];');
  2884. Add(' b:=[red]<=vt;');
  2885. Add(' b:=[red]<=[green];');
  2886. Add(' b:=vt>=vu;');
  2887. Add(' b:=vt>=[red];');
  2888. Add(' b:=[red]>=vt;');
  2889. Add(' b:=[red]>=[green];');
  2890. ConvertProgram;
  2891. CheckSource('TestSet_Operators',
  2892. LinesToStr([ // statements
  2893. 'this.TColor = {',
  2894. ' "0":"Red",',
  2895. ' Red:0,',
  2896. ' "1":"Green",',
  2897. ' Green:1,',
  2898. ' "2":"Blue",',
  2899. ' Blue:2',
  2900. ' };',
  2901. 'this.vC = 0;',
  2902. 'this.vS = {};',
  2903. 'this.vT = {};',
  2904. 'this.vU = {};',
  2905. 'this.B = false;'
  2906. ]),
  2907. LinesToStr([
  2908. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  2909. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  2910. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  2911. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  2912. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  2913. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  2914. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  2915. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  2916. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  2917. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  2918. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  2919. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  2920. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  2921. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  2922. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  2923. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  2924. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  2925. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  2926. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  2927. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  2928. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  2929. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  2930. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  2931. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  2932. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  2933. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  2934. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  2935. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  2936. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  2937. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  2938. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  2939. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  2940. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  2941. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  2942. '']));
  2943. end;
  2944. procedure TTestModule.TestSet_Operator_In;
  2945. begin
  2946. StartProgram(false);
  2947. Add('type');
  2948. Add(' TColor = (Red, Green, Blue);');
  2949. Add(' TColors = set of tcolor;');
  2950. Add('var');
  2951. Add(' vC: tcolor;');
  2952. Add(' vT: tcolors;');
  2953. Add(' B: boolean;');
  2954. Add('begin');
  2955. Add(' b:=red in vt;');
  2956. Add(' b:=vc in vt;');
  2957. Add(' b:=green in [red..blue];');
  2958. Add(' b:=vc in [red..blue];');
  2959. Add(' ');
  2960. Add(' if red in vt then ;');
  2961. Add(' while vC in vt do ;');
  2962. Add(' repeat');
  2963. Add(' until vC in vt;');
  2964. ConvertProgram;
  2965. CheckSource('TestSet_Operator_In',
  2966. LinesToStr([ // statements
  2967. 'this.TColor = {',
  2968. ' "0":"Red",',
  2969. ' Red:0,',
  2970. ' "1":"Green",',
  2971. ' Green:1,',
  2972. ' "2":"Blue",',
  2973. ' Blue:2',
  2974. ' };',
  2975. 'this.vC = 0;',
  2976. 'this.vT = {};',
  2977. 'this.B = false;'
  2978. ]),
  2979. LinesToStr([
  2980. '$mod.B = $mod.TColor.Red in $mod.vT;',
  2981. '$mod.B = $mod.vC in $mod.vT;',
  2982. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  2983. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  2984. 'if ($mod.TColor.Red in $mod.vT) ;',
  2985. 'while ($mod.vC in $mod.vT) {',
  2986. '};',
  2987. 'do {',
  2988. '} while (!($mod.vC in $mod.vT));',
  2989. '']));
  2990. end;
  2991. procedure TTestModule.TestSet_Functions;
  2992. begin
  2993. StartProgram(false);
  2994. Add('type');
  2995. Add(' TMyEnum = (Red, Green);');
  2996. Add(' TMyEnums = set of TMyEnum;');
  2997. Add('var');
  2998. Add(' e: TMyEnum;');
  2999. Add(' s: TMyEnums;');
  3000. Add('begin');
  3001. Add(' e:=Low(TMyEnums);');
  3002. Add(' e:=Low(s);');
  3003. Add(' e:=High(TMyEnums);');
  3004. Add(' e:=High(s);');
  3005. ConvertProgram;
  3006. CheckSource('TestSetFunctions',
  3007. LinesToStr([ // statements
  3008. 'this.TMyEnum = {',
  3009. ' "0":"Red",',
  3010. ' Red:0,',
  3011. ' "1":"Green",',
  3012. ' Green:1',
  3013. ' };',
  3014. 'this.e = 0;',
  3015. 'this.s = {};'
  3016. ]),
  3017. LinesToStr([
  3018. '$mod.e=$mod.TMyEnum.Red;',
  3019. '$mod.e=$mod.TMyEnum.Red;',
  3020. '$mod.e=$mod.TMyEnum.Green;',
  3021. '$mod.e=$mod.TMyEnum.Green;',
  3022. '']));
  3023. end;
  3024. procedure TTestModule.TestSet_PassAsArgClone;
  3025. begin
  3026. StartProgram(false);
  3027. Add('type');
  3028. Add(' TMyEnum = (Red, Green);');
  3029. Add(' TMyEnums = set of TMyEnum;');
  3030. Add('procedure DoDefault(s: tmyenums); begin end;');
  3031. Add('procedure DoConst(const s: tmyenums); begin end;');
  3032. Add('var');
  3033. Add(' aSet: tmyenums;');
  3034. Add('begin');
  3035. Add(' dodefault(aset);');
  3036. Add(' doconst(aset);');
  3037. ConvertProgram;
  3038. CheckSource('TestSetFunctions',
  3039. LinesToStr([ // statements
  3040. 'this.TMyEnum = {',
  3041. ' "0":"Red",',
  3042. ' Red:0,',
  3043. ' "1":"Green",',
  3044. ' Green:1',
  3045. ' };',
  3046. 'this.DoDefault = function (s) {',
  3047. '};',
  3048. 'this.DoConst = function (s) {',
  3049. '};',
  3050. 'this.aSet = {};'
  3051. ]),
  3052. LinesToStr([
  3053. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  3054. '$mod.DoConst($mod.aSet);',
  3055. '']));
  3056. end;
  3057. procedure TTestModule.TestSet_AsParams;
  3058. begin
  3059. StartProgram(false);
  3060. Add('type TEnum = (Red,Blue);');
  3061. Add('type TEnums = set of TEnum;');
  3062. Add('procedure DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums);');
  3063. Add('var vJ: TEnums;');
  3064. Add('begin');
  3065. Add(' vg:=vg;');
  3066. Add(' vj:=vh;');
  3067. Add(' vi:=vi;');
  3068. Add(' doit(vg,vg,vg);');
  3069. Add(' doit(vh,vh,vj);');
  3070. Add(' doit(vi,vi,vi);');
  3071. Add(' doit(vj,vj,vj);');
  3072. Add('end;');
  3073. Add('var i: TEnums;');
  3074. Add('begin');
  3075. Add(' doit(i,i,i);');
  3076. ConvertProgram;
  3077. CheckSource('TestSet_AsParams',
  3078. LinesToStr([ // statements
  3079. 'this.TEnum = {',
  3080. ' "0": "Red",',
  3081. ' Red: 0,',
  3082. ' "1": "Blue",',
  3083. ' Blue: 1',
  3084. '};',
  3085. 'this.DoIt = function (vG,vH,vI) {',
  3086. ' var vJ = {};',
  3087. ' vG = rtl.refSet(vG);',
  3088. ' vJ = rtl.refSet(vH);',
  3089. ' vI.set(rtl.refSet(vI.get()));',
  3090. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  3091. ' get: function () {',
  3092. ' return vG;',
  3093. ' },',
  3094. ' set: function (v) {',
  3095. ' vG = v;',
  3096. ' }',
  3097. ' });',
  3098. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  3099. ' get: function () {',
  3100. ' return vJ;',
  3101. ' },',
  3102. ' set: function (v) {',
  3103. ' vJ = v;',
  3104. ' }',
  3105. ' });',
  3106. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  3107. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  3108. ' get: function () {',
  3109. ' return vJ;',
  3110. ' },',
  3111. ' set: function (v) {',
  3112. ' vJ = v;',
  3113. ' }',
  3114. ' });',
  3115. '};',
  3116. 'this.i = {};'
  3117. ]),
  3118. LinesToStr([
  3119. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  3120. ' p: $mod,',
  3121. ' get: function () {',
  3122. ' return this.p.i;',
  3123. ' },',
  3124. ' set: function (v) {',
  3125. ' this.p.i = v;',
  3126. ' }',
  3127. '});'
  3128. ]));
  3129. end;
  3130. procedure TTestModule.TestSet_Property;
  3131. begin
  3132. StartProgram(false);
  3133. Add('type');
  3134. Add(' TEnum = (Red,Blue);');
  3135. Add(' TEnums = set of TEnum;');
  3136. Add(' TObject = class');
  3137. Add(' function GetColors: TEnums; external name ''GetColors'';');
  3138. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  3139. Add(' property Colors: TEnums read GetColors write SetColors;');
  3140. Add(' end;');
  3141. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  3142. Add('begin end;');
  3143. Add('var Obj: TObject;');
  3144. Add('begin');
  3145. Add(' Include(Obj.Colors,Red);');
  3146. Add(' Exclude(Obj.Colors,Red);');
  3147. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  3148. ConvertProgram;
  3149. CheckSource('TestSet_Property',
  3150. LinesToStr([ // statements
  3151. 'this.TEnum = {',
  3152. ' "0": "Red",',
  3153. ' Red: 0,',
  3154. ' "1": "Blue",',
  3155. ' Blue: 1',
  3156. '};',
  3157. 'rtl.createClass($mod, "TObject", null, function () {',
  3158. ' this.$init = function () {',
  3159. ' };',
  3160. ' this.$final = function () {',
  3161. ' };',
  3162. '});',
  3163. 'this.DoIt = function (i, j, k, l) {',
  3164. '};',
  3165. 'this.Obj = null;',
  3166. '']),
  3167. LinesToStr([
  3168. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  3169. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  3170. '']));
  3171. end;
  3172. procedure TTestModule.TestSet_EnumConst;
  3173. begin
  3174. StartProgram(false);
  3175. Add('type');
  3176. Add(' TEnum = (Red,Blue);');
  3177. Add(' TEnums = set of TEnum;');
  3178. Add('const');
  3179. Add(' Orange = red;');
  3180. Add('var');
  3181. Add(' Enum: tenum;');
  3182. Add(' Enums: tenums;');
  3183. Add('begin');
  3184. Add(' Include(enums,orange);');
  3185. Add(' Exclude(enums,orange);');
  3186. Add(' if orange in enums then;');
  3187. Add(' if orange in [orange,red] then;');
  3188. ConvertProgram;
  3189. CheckSource('TestEnumConst',
  3190. LinesToStr([ // statements
  3191. 'this.TEnum = {',
  3192. ' "0": "Red",',
  3193. ' Red: 0,',
  3194. ' "1": "Blue",',
  3195. ' Blue: 1',
  3196. '};',
  3197. 'this.Orange = $mod.TEnum.Red;',
  3198. 'this.Enum = 0;',
  3199. 'this.Enums = {};',
  3200. '']),
  3201. LinesToStr([
  3202. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.Orange);',
  3203. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.Orange);',
  3204. 'if ($mod.Orange in $mod.Enums) ;',
  3205. 'if ($mod.Orange in rtl.createSet($mod.Orange, $mod.TEnum.Red)) ;',
  3206. '']));
  3207. end;
  3208. procedure TTestModule.TestSet_AnonymousEnumType;
  3209. begin
  3210. StartProgram(false);
  3211. Add('type');
  3212. Add(' TFlags = set of (red, green);');
  3213. Add('const');
  3214. Add(' favorite = red;');
  3215. Add('var');
  3216. Add(' f: TFlags;');
  3217. Add(' i: longint;');
  3218. Add('begin');
  3219. Add(' Include(f,red);');
  3220. Add(' Include(f,favorite);');
  3221. Add(' i:=ord(red);');
  3222. Add(' i:=ord(favorite);');
  3223. Add(' i:=ord(low(TFlags));');
  3224. Add(' i:=ord(low(f));');
  3225. Add(' i:=ord(low(favorite));');
  3226. Add(' i:=ord(high(TFlags));');
  3227. Add(' i:=ord(high(f));');
  3228. Add(' i:=ord(high(favorite));');
  3229. Add(' f:=[green,favorite];');
  3230. ConvertProgram;
  3231. CheckSource('TestSet_AnonymousEnumType',
  3232. LinesToStr([ // statements
  3233. 'this.TFlags$a = {',
  3234. ' "0": "red",',
  3235. ' red: 0,',
  3236. ' "1": "green",',
  3237. ' green: 1',
  3238. '};',
  3239. 'this.favorite = $mod.TFlags$a.red;',
  3240. 'this.f = {};',
  3241. 'this.i = 0;',
  3242. '']),
  3243. LinesToStr([
  3244. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  3245. '$mod.f = rtl.includeSet($mod.f, $mod.favorite);',
  3246. '$mod.i = $mod.TFlags$a.red;',
  3247. '$mod.i = $mod.favorite;',
  3248. '$mod.i = $mod.TFlags$a.red;',
  3249. '$mod.i = $mod.TFlags$a.red;',
  3250. '$mod.i = $mod.TFlags$a.red;',
  3251. '$mod.i = $mod.TFlags$a.green;',
  3252. '$mod.i = $mod.TFlags$a.green;',
  3253. '$mod.i = $mod.TFlags$a.green;',
  3254. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.favorite);',
  3255. '']));
  3256. end;
  3257. procedure TTestModule.TestSet_CharFail;
  3258. begin
  3259. StartProgram(false);
  3260. Add('type');
  3261. Add(' TChars = set of char;');
  3262. Add('begin');
  3263. SetExpectedPasResolverError('Not supported: set of Char',nNotSupportedX);
  3264. ConvertProgram;
  3265. end;
  3266. procedure TTestModule.TestSet_BooleanFail;
  3267. begin
  3268. StartProgram(false);
  3269. Add('type');
  3270. Add(' TBools = set of boolean;');
  3271. Add('begin');
  3272. SetExpectedPasResolverError('Not supported: set of Boolean',nNotSupportedX);
  3273. ConvertProgram;
  3274. end;
  3275. procedure TTestModule.TestSet_ConstEnum;
  3276. begin
  3277. StartProgram(false);
  3278. Add([
  3279. 'type',
  3280. ' TEnum = (red,blue,green);',
  3281. ' TEnums = set of TEnum;',
  3282. 'const',
  3283. ' teAny = [low(TEnum)..high(TEnum)];',
  3284. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  3285. 'var',
  3286. ' e: TEnum;',
  3287. ' s: TEnums;',
  3288. 'begin',
  3289. ' if blue in teAny then;',
  3290. ' if blue in teAny+[e] then;',
  3291. ' if blue in teAny+teRedBlue then;',
  3292. ' s:=teAny;',
  3293. ' s:=teAny+[e];',
  3294. ' s:=[e]+teAny;',
  3295. ' s:=teAny+teRedBlue;',
  3296. ' s:=teAny+teRedBlue+[e];',
  3297. '']);
  3298. ConvertProgram;
  3299. CheckSource('TestSet_ConstEnum',
  3300. LinesToStr([ // statements
  3301. 'this.TEnum = {',
  3302. ' "0": "red",',
  3303. ' red: 0,',
  3304. ' "1": "blue",',
  3305. ' blue: 1,',
  3306. ' "2": "green",',
  3307. ' green: 2',
  3308. '};',
  3309. 'this.teAny = rtl.createSet(null, $mod.TEnum.red, $mod.TEnum.green);',
  3310. 'this.teRedBlue = rtl.createSet(null, $mod.TEnum.red, $mod.TEnum.green - 1);',
  3311. 'this.e = 0;',
  3312. 'this.s = {};',
  3313. '']),
  3314. LinesToStr([
  3315. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  3316. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  3317. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  3318. '$mod.s = rtl.refSet($mod.teAny);',
  3319. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  3320. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  3321. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  3322. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  3323. '']));
  3324. end;
  3325. procedure TTestModule.TestSet_ConstChar;
  3326. begin
  3327. StartProgram(false);
  3328. Add('const');
  3329. Add(' LowChars = [''a''..''z''];');
  3330. Add(' Chars = LowChars+[''A''..''Z''];');
  3331. Add('var');
  3332. Add(' c: char;');
  3333. Add(' s: string;');
  3334. Add('begin');
  3335. Add(' if c in lowchars then ;');
  3336. Add(' if ''a'' in lowchars then ;');
  3337. Add(' if s[1] in lowchars then ;');
  3338. Add(' if c in chars then ;');
  3339. Add(' if c in [''a''..''z'',''_''] then ;');
  3340. Add(' if ''b'' in [''a''..''z'',''_''] then ;');
  3341. ConvertProgram;
  3342. CheckSource('TestSet_ConstChar',
  3343. LinesToStr([ // statements
  3344. 'this.LowChars = rtl.createSet(null, 97, 122);',
  3345. 'this.Chars = rtl.unionSet($mod.LowChars, rtl.createSet(null, 65, 90));',
  3346. 'this.c = "";',
  3347. 'this.s = "";',
  3348. '']),
  3349. LinesToStr([
  3350. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  3351. 'if (97 in $mod.LowChars) ;',
  3352. 'if ($mod.s.charCodeAt(1 - 1) in $mod.LowChars) ;',
  3353. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  3354. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  3355. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  3356. '']));
  3357. end;
  3358. procedure TTestModule.TestNestBegin;
  3359. begin
  3360. StartProgram(false);
  3361. Add('begin');
  3362. Add(' begin');
  3363. Add(' begin');
  3364. Add(' end;');
  3365. Add(' begin');
  3366. Add(' if true then ;');
  3367. Add(' end;');
  3368. Add(' end;');
  3369. ConvertProgram;
  3370. CheckSource('TestNestBegin',
  3371. '',
  3372. 'if (true) ;');
  3373. end;
  3374. procedure TTestModule.TestUnitImplVars;
  3375. begin
  3376. StartUnit(false);
  3377. Add('interface');
  3378. Add('implementation');
  3379. Add('var');
  3380. Add(' V1:longint;');
  3381. Add(' V2:longint = 3;');
  3382. Add(' V3:string = ''abc'';');
  3383. ConvertUnit;
  3384. CheckSource('TestUnitImplVars',
  3385. LinesToStr([ // statements
  3386. 'var $impl = $mod.$impl;',
  3387. '']),
  3388. '', // this.$init
  3389. LinesToStr([ // implementation
  3390. '$impl.V1 = 0;',
  3391. '$impl.V2 = 3;',
  3392. '$impl.V3 = "abc";',
  3393. '']) );
  3394. end;
  3395. procedure TTestModule.TestUnitImplConsts;
  3396. begin
  3397. StartUnit(false);
  3398. Add('interface');
  3399. Add('implementation');
  3400. Add('const');
  3401. Add(' v1 = 3;');
  3402. Add(' v2:longint = 4;');
  3403. Add(' v3:string = ''abc'';');
  3404. ConvertUnit;
  3405. CheckSource('TestUnitImplConsts',
  3406. LinesToStr([ // statements
  3407. 'var $impl = $mod.$impl;',
  3408. '']),
  3409. '', // this.$init
  3410. LinesToStr([ // implementation
  3411. '$impl.v1 = 3;',
  3412. '$impl.v2 = 4;',
  3413. '$impl.v3 = "abc";',
  3414. '']) );
  3415. end;
  3416. procedure TTestModule.TestUnitImplRecord;
  3417. begin
  3418. StartUnit(false);
  3419. Add('interface');
  3420. Add('implementation');
  3421. Add('type');
  3422. Add(' TMyRecord = record');
  3423. Add(' i: longint;');
  3424. Add(' end;');
  3425. Add('var aRec: TMyRecord;');
  3426. Add('initialization');
  3427. Add(' arec.i:=3;');
  3428. ConvertUnit;
  3429. CheckSource('TestUnitImplRecord',
  3430. LinesToStr([ // statements
  3431. 'var $impl = $mod.$impl;',
  3432. '']),
  3433. // this.$init
  3434. '$impl.aRec.i = 3;',
  3435. LinesToStr([ // implementation
  3436. '$impl.TMyRecord = function (s) {',
  3437. ' if (s) {',
  3438. ' this.i = s.i;',
  3439. ' } else {',
  3440. ' this.i = 0;',
  3441. ' };',
  3442. ' this.$equal = function (b) {',
  3443. ' return this.i == b.i;',
  3444. ' };',
  3445. '};',
  3446. '$impl.aRec = new $impl.TMyRecord();',
  3447. '']) );
  3448. end;
  3449. procedure TTestModule.TestRenameJSNameConflict;
  3450. begin
  3451. StartProgram(false);
  3452. Add('var apply: longint;');
  3453. Add('var bind: longint;');
  3454. Add('var call: longint;');
  3455. Add('begin');
  3456. ConvertProgram;
  3457. CheckSource('TestRenameJSNameConflict',
  3458. LinesToStr([ // statements
  3459. 'this.Apply = 0;',
  3460. 'this.Bind = 0;',
  3461. 'this.Call = 0;'
  3462. ]),
  3463. LinesToStr([ // this.$main
  3464. ''
  3465. ]));
  3466. end;
  3467. procedure TTestModule.TestLocalConst;
  3468. begin
  3469. StartProgram(false);
  3470. Add('procedure DoIt;');
  3471. Add('const');
  3472. Add(' cA: longint = 1;');
  3473. Add(' cB = 2;');
  3474. Add(' procedure Sub;');
  3475. Add(' const');
  3476. Add(' csA = 3;');
  3477. Add(' cB: double = 4;');
  3478. Add(' begin');
  3479. Add(' cb:=cb+csa;');
  3480. Add(' ca:=ca+csa+5;');
  3481. Add(' end;');
  3482. Add('begin');
  3483. Add(' ca:=ca+cb+6;');
  3484. Add('end;');
  3485. Add('begin');
  3486. ConvertProgram;
  3487. CheckSource('TestLocalConst',
  3488. LinesToStr([
  3489. 'var cA = 1;',
  3490. 'var cB = 2;',
  3491. 'var csA = 3;',
  3492. 'var cB$1 = 4;',
  3493. 'this.DoIt = function () {',
  3494. ' function Sub() {',
  3495. ' cB$1 = cB$1 + csA;',
  3496. ' cA = (cA + csA) + 5;',
  3497. ' };',
  3498. ' cA = (cA + cB) + 6;',
  3499. '};'
  3500. ]),
  3501. LinesToStr([
  3502. ]));
  3503. end;
  3504. procedure TTestModule.TestVarExternal;
  3505. begin
  3506. StartProgram(false);
  3507. Add('var');
  3508. Add(' NaN: double; external name ''Global.NaN'';');
  3509. Add(' d: double;');
  3510. Add('begin');
  3511. Add(' d:=NaN;');
  3512. ConvertProgram;
  3513. CheckSource('TestVarExternal',
  3514. LinesToStr([
  3515. 'this.d = 0.0;'
  3516. ]),
  3517. LinesToStr([
  3518. '$mod.d = Global.NaN;'
  3519. ]));
  3520. end;
  3521. procedure TTestModule.TestVarExternalOtherUnit;
  3522. begin
  3523. AddModuleWithIntfImplSrc('unit2.pas',
  3524. LinesToStr([
  3525. 'var NaN: double; external name ''Global.NaN'';',
  3526. 'var iV: longint;'
  3527. ]),
  3528. '');
  3529. StartUnit(true);
  3530. Add('interface');
  3531. Add('uses unit2;');
  3532. Add('implementation');
  3533. Add('var');
  3534. Add(' d: double;');
  3535. Add(' i: longint; external name ''$i'';');
  3536. Add('begin');
  3537. Add(' d:=nan;');
  3538. Add(' d:=uNit2.nan;');
  3539. Add(' d:=test1.d;');
  3540. Add(' i:=iv;');
  3541. Add(' i:=uNit2.iv;');
  3542. Add(' i:=test1.i;');
  3543. ConvertUnit;
  3544. CheckSource('TestVarExternalOtherUnit',
  3545. LinesToStr([
  3546. 'var $impl = $mod.$impl;',
  3547. '']),
  3548. LinesToStr([ // this.$init
  3549. '$impl.d = Global.NaN;',
  3550. '$impl.d = Global.NaN;',
  3551. '$impl.d = $impl.d;',
  3552. '$i = pas.unit2.iV;',
  3553. '$i = pas.unit2.iV;',
  3554. '$i = $i;',
  3555. '']),
  3556. LinesToStr([ // implementation
  3557. '$impl.d = 0.0;',
  3558. '']) );
  3559. end;
  3560. procedure TTestModule.TestDouble;
  3561. begin
  3562. StartProgram(false);
  3563. Add([
  3564. 'var',
  3565. ' d: double;',
  3566. 'begin',
  3567. ' d:=1.0;',
  3568. ' d:=1.0/3.0;',
  3569. ' d:=1/3;',
  3570. ' d:=5.0E-324;',
  3571. ' d:=1.7E308;',
  3572. ' d:=10**3;',
  3573. ' d:=10 mod 3;',
  3574. ' d:=10 div 3;',
  3575. '']);
  3576. ConvertProgram;
  3577. CheckSource('TestDouble',
  3578. LinesToStr([
  3579. 'this.d=0.0;'
  3580. ]),
  3581. LinesToStr([
  3582. '$mod.d = 1.0;',
  3583. '$mod.d = 1.0 / 3.0;',
  3584. '$mod.d = 1 / 3;',
  3585. '$mod.d = 5.0E-324;',
  3586. '$mod.d = 1.7E308;',
  3587. '$mod.d = Math.pow(10, 3);',
  3588. '$mod.d = 10 % 3;',
  3589. '$mod.d = Math.floor(10 / 3);',
  3590. '']));
  3591. end;
  3592. procedure TTestModule.TestCharConst;
  3593. begin
  3594. StartProgram(false);
  3595. Add('const');
  3596. Add(' c: char = ''1'';');
  3597. Add('begin');
  3598. Add(' c:=#0;');
  3599. Add(' c:=#1;');
  3600. Add(' c:=#9;');
  3601. Add(' c:=#10;');
  3602. Add(' c:=#13;');
  3603. Add(' c:=#31;');
  3604. Add(' c:=#32;');
  3605. Add(' c:=#$A;');
  3606. Add(' c:=#$0A;');
  3607. Add(' c:=#$b;');
  3608. Add(' c:=#$0b;');
  3609. Add(' c:=^A;');
  3610. Add(' c:=''"'';');
  3611. ConvertProgram;
  3612. CheckSource('TestCharConst',
  3613. LinesToStr([
  3614. 'this.c="1";'
  3615. ]),
  3616. LinesToStr([
  3617. '$mod.c="\x00";',
  3618. '$mod.c="\x01";',
  3619. '$mod.c="\t";',
  3620. '$mod.c="\n";',
  3621. '$mod.c="\r";',
  3622. '$mod.c="\x1F";',
  3623. '$mod.c=" ";',
  3624. '$mod.c="\n";',
  3625. '$mod.c="\n";',
  3626. '$mod.c="\x0B";',
  3627. '$mod.c="\x0B";',
  3628. '$mod.c="\x01";',
  3629. '$mod.c=''"'';'
  3630. ]));
  3631. end;
  3632. procedure TTestModule.TestChar_Compare;
  3633. begin
  3634. StartProgram(false);
  3635. Add('var');
  3636. Add(' c: char;');
  3637. Add(' b: boolean;');
  3638. Add('begin');
  3639. Add(' b:=c=''1'';');
  3640. Add(' b:=''2''=c;');
  3641. Add(' b:=''3''=''4'';');
  3642. Add(' b:=c<>''5'';');
  3643. Add(' b:=''6''<>c;');
  3644. Add(' b:=c>''7'';');
  3645. Add(' b:=''8''>c;');
  3646. Add(' b:=c>=''9'';');
  3647. Add(' b:=''A''>=c;');
  3648. Add(' b:=c<''B'';');
  3649. Add(' b:=''C''<c;');
  3650. Add(' b:=c<=''D'';');
  3651. Add(' b:=''E''<=c;');
  3652. ConvertProgram;
  3653. CheckSource('TestChar_Compare',
  3654. LinesToStr([
  3655. 'this.c="";',
  3656. 'this.b = false;'
  3657. ]),
  3658. LinesToStr([
  3659. '$mod.b = $mod.c == "1";',
  3660. '$mod.b = "2" == $mod.c;',
  3661. '$mod.b = "3" == "4";',
  3662. '$mod.b = $mod.c != "5";',
  3663. '$mod.b = "6" != $mod.c;',
  3664. '$mod.b = $mod.c > "7";',
  3665. '$mod.b = "8" > $mod.c;',
  3666. '$mod.b = $mod.c >= "9";',
  3667. '$mod.b = "A" >= $mod.c;',
  3668. '$mod.b = $mod.c < "B";',
  3669. '$mod.b = "C" < $mod.c;',
  3670. '$mod.b = $mod.c <= "D";',
  3671. '$mod.b = "E" <= $mod.c;',
  3672. '']));
  3673. end;
  3674. procedure TTestModule.TestChar_Ord;
  3675. begin
  3676. StartProgram(false);
  3677. Add('var');
  3678. Add(' c: char;');
  3679. Add(' i: longint;');
  3680. Add(' s: string;');
  3681. Add('begin');
  3682. Add(' i:=ord(c);');
  3683. Add(' i:=ord(s[i]);');
  3684. ConvertProgram;
  3685. CheckSource('TestChar_Ord',
  3686. LinesToStr([
  3687. 'this.c = "";',
  3688. 'this.i = 0;',
  3689. 'this.s = "";'
  3690. ]),
  3691. LinesToStr([
  3692. '$mod.i = $mod.c.charCodeAt();',
  3693. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  3694. '']));
  3695. end;
  3696. procedure TTestModule.TestChar_Chr;
  3697. begin
  3698. StartProgram(false);
  3699. Add('var');
  3700. Add(' c: char;');
  3701. Add(' i: longint;');
  3702. Add('begin');
  3703. Add(' c:=chr(i);');
  3704. ConvertProgram;
  3705. CheckSource('TestChar_Chr',
  3706. LinesToStr([
  3707. 'this.c = "";',
  3708. 'this.i = 0;'
  3709. ]),
  3710. LinesToStr([
  3711. '$mod.c = String.fromCharCode($mod.i);',
  3712. '']));
  3713. end;
  3714. procedure TTestModule.TestStringConst;
  3715. begin
  3716. StartProgram(false);
  3717. Add('var');
  3718. Add(' s: string = ''abc'';');
  3719. Add('begin');
  3720. Add(' s:='''';');
  3721. Add(' s:=#13#10;');
  3722. Add(' s:=#9''foo'';');
  3723. Add(' s:=#$A9;');
  3724. Add(' s:=''foo''#13''bar'';');
  3725. Add(' s:=''"'';');
  3726. Add(' s:=''"''''"'';');
  3727. ConvertProgram;
  3728. CheckSource('TestStringConst',
  3729. LinesToStr([
  3730. 'this.s="abc";'
  3731. ]),
  3732. LinesToStr([
  3733. '$mod.s="";',
  3734. '$mod.s="\r\n";',
  3735. '$mod.s="\tfoo";',
  3736. '$mod.s="©";',
  3737. '$mod.s="foo\rbar";',
  3738. '$mod.s=''"'';',
  3739. '$mod.s=''"\''"'';'
  3740. ]));
  3741. end;
  3742. procedure TTestModule.TestString_Length;
  3743. begin
  3744. StartProgram(false);
  3745. Add('const c = ''foo'';');
  3746. Add('var');
  3747. Add(' s: string;');
  3748. Add(' i: longint;');
  3749. Add('begin');
  3750. Add(' i:=length(s);');
  3751. Add(' i:=length(s+s);');
  3752. Add(' i:=length(''abc'');');
  3753. Add(' i:=length(c);');
  3754. ConvertProgram;
  3755. CheckSource('TestString_Length',
  3756. LinesToStr([
  3757. 'this.c = "foo";',
  3758. 'this.s = "";',
  3759. 'this.i = 0;',
  3760. '']),
  3761. LinesToStr([
  3762. '$mod.i = $mod.s.length;',
  3763. '$mod.i = ($mod.s+$mod.s).length;',
  3764. '$mod.i = "abc".length;',
  3765. '$mod.i = $mod.c.length;',
  3766. '']));
  3767. end;
  3768. procedure TTestModule.TestString_Compare;
  3769. begin
  3770. StartProgram(false);
  3771. Add('var');
  3772. Add(' s, t: string;');
  3773. Add(' b: boolean;');
  3774. Add('begin');
  3775. Add(' b:=s=t;');
  3776. Add(' b:=s<>t;');
  3777. Add(' b:=s>t;');
  3778. Add(' b:=s>=t;');
  3779. Add(' b:=s<t;');
  3780. Add(' b:=s<=t;');
  3781. ConvertProgram;
  3782. CheckSource('TestString_Compare',
  3783. LinesToStr([ // statements
  3784. 'this.s = "";',
  3785. 'this.t = "";',
  3786. 'this.b =false;'
  3787. ]),
  3788. LinesToStr([ // this.$main
  3789. '$mod.b = $mod.s == $mod.t;',
  3790. '$mod.b = $mod.s != $mod.t;',
  3791. '$mod.b = $mod.s > $mod.t;',
  3792. '$mod.b = $mod.s >= $mod.t;',
  3793. '$mod.b = $mod.s < $mod.t;',
  3794. '$mod.b = $mod.s <= $mod.t;',
  3795. '']));
  3796. end;
  3797. procedure TTestModule.TestString_SetLength;
  3798. begin
  3799. StartProgram(false);
  3800. Add([
  3801. 'procedure DoIt(var s: string);',
  3802. 'begin',
  3803. ' SetLength(s,2);',
  3804. 'end;',
  3805. 'var s: string;',
  3806. 'begin',
  3807. ' SetLength(s,3);',
  3808. '']);
  3809. ConvertProgram;
  3810. CheckSource('TestString_SetLength',
  3811. LinesToStr([ // statements
  3812. 'this.DoIt = function (s) {',
  3813. ' s.set(rtl.strSetLength(s.get(), 2));',
  3814. '};',
  3815. 'this.s = "";',
  3816. '']),
  3817. LinesToStr([ // this.$main
  3818. '$mod.s = rtl.strSetLength($mod.s, 3);'
  3819. ]));
  3820. end;
  3821. procedure TTestModule.TestString_CharAt;
  3822. begin
  3823. StartProgram(false);
  3824. Add('var');
  3825. Add(' s: string;');
  3826. Add(' c: char;');
  3827. Add(' b: boolean;');
  3828. Add('begin');
  3829. Add(' b:= s[1] = c;');
  3830. Add(' b:= c = s[1];');
  3831. Add(' b:= c <> s[1];');
  3832. Add(' b:= c > s[1];');
  3833. Add(' b:= c >= s[1];');
  3834. Add(' b:= c < s[1];');
  3835. Add(' b:= c <= s[1];');
  3836. Add(' s[1] := c;');
  3837. ConvertProgram;
  3838. CheckSource('TestString_CharAt',
  3839. LinesToStr([ // statements
  3840. 'this.s = "";',
  3841. 'this.c = "";',
  3842. 'this.b = false;'
  3843. ]),
  3844. LinesToStr([ // this.$main
  3845. '$mod.b = $mod.s.charAt(1-1) == $mod.c;',
  3846. '$mod.b = $mod.c == $mod.s.charAt(1 - 1);',
  3847. '$mod.b = $mod.c != $mod.s.charAt(1 - 1);',
  3848. '$mod.b = $mod.c > $mod.s.charAt(1 - 1);',
  3849. '$mod.b = $mod.c >= $mod.s.charAt(1 - 1);',
  3850. '$mod.b = $mod.c < $mod.s.charAt(1 - 1);',
  3851. '$mod.b = $mod.c <= $mod.s.charAt(1 - 1);',
  3852. '$mod.s = rtl.setCharAt($mod.s, 1, $mod.c);',
  3853. '']));
  3854. end;
  3855. procedure TTestModule.TestStr;
  3856. begin
  3857. StartProgram(false);
  3858. Add('var');
  3859. Add(' b: boolean;');
  3860. Add(' i: longint;');
  3861. Add(' d: double;');
  3862. Add(' s: string;');
  3863. Add('begin');
  3864. Add(' str(b,s);');
  3865. Add(' str(i,s);');
  3866. Add(' str(d,s);');
  3867. Add(' str(i:3,s);');
  3868. Add(' str(d:3:2,s);');
  3869. Add(' s:=str(b);');
  3870. Add(' s:=str(i);');
  3871. Add(' s:=str(d);');
  3872. Add(' s:=str(i,i);');
  3873. Add(' s:=str(i:3);');
  3874. Add(' s:=str(d:3:2);');
  3875. Add(' s:=str(i:4,i);');
  3876. Add(' s:=str(i,i:5);');
  3877. Add(' s:=str(i:4,i:5);');
  3878. Add(' s:=str(s,s);');
  3879. Add(' s:=str(s,''foo'');');
  3880. ConvertProgram;
  3881. CheckSource('TestStr',
  3882. LinesToStr([ // statements
  3883. 'this.b = false;',
  3884. 'this.i = 0;',
  3885. 'this.d = 0.0;',
  3886. 'this.s = "";',
  3887. '']),
  3888. LinesToStr([ // this.$main
  3889. '$mod.s = ""+$mod.b;',
  3890. '$mod.s = ""+$mod.i;',
  3891. '$mod.s = ""+$mod.d;',
  3892. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  3893. '$mod.s = rtl.spaceLeft($mod.d.toFixed(2),3);',
  3894. '$mod.s = ""+$mod.b;',
  3895. '$mod.s = ""+$mod.i;',
  3896. '$mod.s = ""+$mod.d;',
  3897. '$mod.s = (""+$mod.i)+$mod.i;',
  3898. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  3899. '$mod.s = rtl.spaceLeft($mod.d.toFixed(2),3);',
  3900. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  3901. '$mod.s = ("" + $mod.i) + rtl.spaceLeft("" + $mod.i, 5);',
  3902. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  3903. '$mod.s = $mod.s + $mod.s;',
  3904. '$mod.s = $mod.s + "foo";',
  3905. '']));
  3906. end;
  3907. procedure TTestModule.TestBaseType_AnsiStringFail;
  3908. begin
  3909. StartProgram(false);
  3910. Add('var s: AnsiString');
  3911. SetExpectedPasResolverError('identifier not found "AnsiString"',nIdentifierNotFound);
  3912. ConvertProgram;
  3913. end;
  3914. procedure TTestModule.TestBaseType_UnicodeStringFail;
  3915. begin
  3916. StartProgram(false);
  3917. Add('var s: UnicodeString');
  3918. SetExpectedPasResolverError('identifier not found "UnicodeString"',nIdentifierNotFound);
  3919. ConvertProgram;
  3920. end;
  3921. procedure TTestModule.TestBaseType_ShortStringFail;
  3922. begin
  3923. StartProgram(false);
  3924. Add('var s: ShortString');
  3925. SetExpectedPasResolverError('identifier not found "ShortString"',nIdentifierNotFound);
  3926. ConvertProgram;
  3927. end;
  3928. procedure TTestModule.TestBaseType_RawByteStringFail;
  3929. begin
  3930. StartProgram(false);
  3931. Add('var s: RawByteString');
  3932. SetExpectedPasResolverError('identifier not found "RawByteString"',nIdentifierNotFound);
  3933. ConvertProgram;
  3934. end;
  3935. procedure TTestModule.TestTypeShortstring_Fail;
  3936. begin
  3937. StartProgram(false);
  3938. Add('type t = string[12];');
  3939. Add('var s: t;');
  3940. Add('begin');
  3941. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  3942. ConvertProgram;
  3943. end;
  3944. procedure TTestModule.TestProcTwoArgs;
  3945. begin
  3946. StartProgram(false);
  3947. Add('procedure Test(a,b: longint);');
  3948. Add('begin');
  3949. Add('end;');
  3950. Add('begin');
  3951. ConvertProgram;
  3952. CheckSource('TestProcTwoArgs',
  3953. LinesToStr([ // statements
  3954. 'this.Test = function (a,b) {',
  3955. '};'
  3956. ]),
  3957. LinesToStr([ // this.$main
  3958. ''
  3959. ]));
  3960. end;
  3961. procedure TTestModule.TestProc_DefaultValue;
  3962. begin
  3963. StartProgram(false);
  3964. Add('procedure p1(i: longint = 1);');
  3965. Add('begin');
  3966. Add('end;');
  3967. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  3968. Add('begin');
  3969. Add('end;');
  3970. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  3971. Add('begin');
  3972. Add('end;');
  3973. Add('begin');
  3974. Add(' p1;');
  3975. Add(' p1();');
  3976. Add(' p1(11);');
  3977. Add(' p2;');
  3978. Add(' p2();');
  3979. Add(' p2(12);');
  3980. Add(' p2(13,''b'');');
  3981. Add(' p3();');
  3982. ConvertProgram;
  3983. CheckSource('TestProc_DefaultValue',
  3984. LinesToStr([ // statements
  3985. 'this.p1 = function (i) {',
  3986. '};',
  3987. 'this.p2 = function (i,c) {',
  3988. '};',
  3989. 'this.p3 = function (d,b,s) {',
  3990. '};'
  3991. ]),
  3992. LinesToStr([ // this.$main
  3993. ' $mod.p1(1);',
  3994. ' $mod.p1(1);',
  3995. ' $mod.p1(11);',
  3996. ' $mod.p2(1,"a");',
  3997. ' $mod.p2(1,"a");',
  3998. ' $mod.p2(12,"a");',
  3999. ' $mod.p2(13,"b");',
  4000. ' $mod.p3(1.0,false,"abc");'
  4001. ]));
  4002. end;
  4003. procedure TTestModule.TestFunctionInt;
  4004. begin
  4005. StartProgram(false);
  4006. Add('function MyTest(Bar: longint): longint;');
  4007. Add('begin');
  4008. Add(' Result:=2*bar');
  4009. Add('end;');
  4010. Add('begin');
  4011. ConvertProgram;
  4012. CheckSource('TestFunctionInt',
  4013. LinesToStr([ // statements
  4014. 'this.MyTest = function (Bar) {',
  4015. ' var Result = 0;',
  4016. ' Result = 2*Bar;',
  4017. ' return Result;',
  4018. '};'
  4019. ]),
  4020. LinesToStr([ // this.$main
  4021. ''
  4022. ]));
  4023. end;
  4024. procedure TTestModule.TestFunctionString;
  4025. begin
  4026. StartProgram(false);
  4027. Add('function Test(Bar: string): string;');
  4028. Add('begin');
  4029. Add(' Result:=bar+BAR');
  4030. Add('end;');
  4031. Add('begin');
  4032. ConvertProgram;
  4033. CheckSource('TestFunctionString',
  4034. LinesToStr([ // statements
  4035. 'this.Test = function (Bar) {',
  4036. ' var Result = "";',
  4037. ' Result = Bar+Bar;',
  4038. ' return Result;',
  4039. '};'
  4040. ]),
  4041. LinesToStr([ // this.$main
  4042. ''
  4043. ]));
  4044. end;
  4045. procedure TTestModule.TestForLoop;
  4046. begin
  4047. StartProgram(false);
  4048. Add('var');
  4049. Add(' vI, vJ, vN: longint;');
  4050. Add('begin');
  4051. Add(' VJ:=0;');
  4052. Add(' VN:=3;');
  4053. Add(' for VI:=1 to VN do');
  4054. Add(' begin');
  4055. Add(' VJ:=VJ+VI;');
  4056. Add(' end;');
  4057. ConvertProgram;
  4058. CheckSource('TestForLoop',
  4059. LinesToStr([ // statements
  4060. 'this.vI = 0;',
  4061. 'this.vJ = 0;',
  4062. 'this.vN = 0;'
  4063. ]),
  4064. LinesToStr([ // this.$main
  4065. ' $mod.vJ = 0;',
  4066. ' $mod.vN = 3;',
  4067. ' var $loopend1 = $mod.vN;',
  4068. ' for ($mod.vI = 1; $mod.vI <= $loopend1; $mod.vI++) {',
  4069. ' $mod.vJ = $mod.vJ + $mod.vI;',
  4070. ' };',
  4071. ' if ($mod.vI > $loopend1) $mod.vI--;'
  4072. ]));
  4073. end;
  4074. procedure TTestModule.TestForLoopInFunction;
  4075. begin
  4076. StartProgram(false);
  4077. Add('function SumNumbers(Count: longint): longint;');
  4078. Add('var');
  4079. Add(' vI, vJ: longint;');
  4080. Add('begin');
  4081. Add(' vj:=0;');
  4082. Add(' for vi:=1 to count do');
  4083. Add(' begin');
  4084. Add(' vj:=vj+vi;');
  4085. Add(' end;');
  4086. Add('end;');
  4087. Add('begin');
  4088. Add(' sumnumbers(3);');
  4089. ConvertProgram;
  4090. CheckSource('TestForLoopInFunction',
  4091. LinesToStr([ // statements
  4092. 'this.SumNumbers = function (Count) {',
  4093. ' var Result = 0;',
  4094. ' var vI = 0;',
  4095. ' var vJ = 0;',
  4096. ' vJ = 0;',
  4097. ' var $loopend1 = Count;',
  4098. ' for (vI = 1; vI <= $loopend1; vI++) {',
  4099. ' vJ = vJ + vI;',
  4100. ' };',
  4101. ' return Result;',
  4102. '};'
  4103. ]),
  4104. LinesToStr([ // $mod.$main
  4105. ' $mod.SumNumbers(3);'
  4106. ]));
  4107. end;
  4108. procedure TTestModule.TestForLoop_ReadVarAfter;
  4109. begin
  4110. StartProgram(false);
  4111. Add('var');
  4112. Add(' vI: longint;');
  4113. Add('begin');
  4114. Add(' for vi:=1 to 2 do ;');
  4115. Add(' if vi=3 then ;');
  4116. ConvertProgram;
  4117. CheckSource('TestForLoop',
  4118. LinesToStr([ // statements
  4119. 'this.vI = 0;'
  4120. ]),
  4121. LinesToStr([ // this.$main
  4122. ' var $loopend1 = 2;',
  4123. ' for ($mod.vI = 1; $mod.vI <= $loopend1; $mod.vI++);',
  4124. ' if($mod.vI>$loopend1)$mod.vI--;',
  4125. ' if ($mod.vI==3) ;'
  4126. ]));
  4127. end;
  4128. procedure TTestModule.TestForLoop_Nested;
  4129. begin
  4130. StartProgram(false);
  4131. Add('function SumNumbers(Count: longint): longint;');
  4132. Add('var');
  4133. Add(' vI, vJ, vK: longint;');
  4134. Add('begin');
  4135. Add(' VK:=0;');
  4136. Add(' for VI:=1 to count do');
  4137. Add(' begin');
  4138. Add(' for vj:=1 to vi do');
  4139. Add(' begin');
  4140. Add(' vk:=VK+VI;');
  4141. Add(' end;');
  4142. Add(' end;');
  4143. Add('end;');
  4144. Add('begin');
  4145. Add(' sumnumbers(3);');
  4146. ConvertProgram;
  4147. CheckSource('TestForLoopInFunction',
  4148. LinesToStr([ // statements
  4149. 'this.SumNumbers = function (Count) {',
  4150. ' var Result = 0;',
  4151. ' var vI = 0;',
  4152. ' var vJ = 0;',
  4153. ' var vK = 0;',
  4154. ' vK = 0;',
  4155. ' var $loopend1 = Count;',
  4156. ' for (vI = 1; vI <= $loopend1; vI++) {',
  4157. ' var $loopend2 = vI;',
  4158. ' for (vJ = 1; vJ <= $loopend2; vJ++) {',
  4159. ' vK = vK + vI;',
  4160. ' };',
  4161. ' };',
  4162. ' return Result;',
  4163. '};'
  4164. ]),
  4165. LinesToStr([ // $mod.$main
  4166. ' $mod.SumNumbers(3);'
  4167. ]));
  4168. end;
  4169. procedure TTestModule.TestRepeatUntil;
  4170. begin
  4171. StartProgram(false);
  4172. Add('var');
  4173. Add(' vI, vJ, vN: longint;');
  4174. Add('begin');
  4175. Add(' vn:=3;');
  4176. Add(' vj:=0;');
  4177. Add(' VI:=0;');
  4178. Add(' repeat');
  4179. Add(' VI:=vi+1;');
  4180. Add(' vj:=VJ+vI;');
  4181. Add(' until vi>=vn');
  4182. ConvertProgram;
  4183. CheckSource('TestRepeatUntil',
  4184. LinesToStr([ // statements
  4185. 'this.vI = 0;',
  4186. 'this.vJ = 0;',
  4187. 'this.vN = 0;'
  4188. ]),
  4189. LinesToStr([ // $mod.$main
  4190. ' $mod.vN = 3;',
  4191. ' $mod.vJ = 0;',
  4192. ' $mod.vI = 0;',
  4193. ' do{',
  4194. ' $mod.vI = $mod.vI + 1;',
  4195. ' $mod.vJ = $mod.vJ + $mod.vI;',
  4196. ' }while(!($mod.vI>=$mod.vN));'
  4197. ]));
  4198. end;
  4199. procedure TTestModule.TestAsmBlock;
  4200. begin
  4201. StartProgram(false);
  4202. Add('var');
  4203. Add(' vI: longint;');
  4204. Add('begin');
  4205. Add(' vi:=1;');
  4206. Add(' asm');
  4207. Add(' if (vI==1) {');
  4208. Add(' vI=2;');
  4209. Add(' }');
  4210. Add(' if (vI==2){ vI=3; }');
  4211. Add(' end;');
  4212. Add(' VI:=4;');
  4213. ConvertProgram;
  4214. CheckSource('TestAsmBlock',
  4215. LinesToStr([ // statements
  4216. 'this.vI = 0;'
  4217. ]),
  4218. LinesToStr([ // $mod.$main
  4219. '$mod.vI = 1;',
  4220. 'if (vI==1) {',
  4221. ' vI=2;',
  4222. '}',
  4223. 'if (vI==2){ vI=3; }',
  4224. ';',
  4225. '$mod.vI = 4;'
  4226. ]));
  4227. end;
  4228. procedure TTestModule.TestAsmPas_Impl;
  4229. begin
  4230. StartUnit(false);
  4231. Add('interface');
  4232. Add('const cIntf: longint = 1;');
  4233. Add('var vIntf: longint;');
  4234. Add('implementation');
  4235. Add('const cImpl: longint = 2;');
  4236. Add('var vImpl: longint;');
  4237. Add('procedure DoIt;');
  4238. Add('const cLoc: longint = 3;');
  4239. Add('var vLoc: longint;');
  4240. Add('begin;');
  4241. Add(' asm');
  4242. //Add(' pas(vIntf)=pas(cIntf);');
  4243. //Add(' pas(vImpl)=pas(cImpl);');
  4244. //Add(' pas(vLoc)=pas(cLoc);');
  4245. Add(' end;');
  4246. Add('end;');
  4247. ConvertUnit;
  4248. // ToDo: check use analyzer
  4249. CheckSource('TestAsmPas_Impl',
  4250. LinesToStr([
  4251. 'var $impl = $mod.$impl;',
  4252. 'this.cIntf = 1;',
  4253. 'this.vIntf = 0;',
  4254. '']),
  4255. '', // this.$init
  4256. LinesToStr([ // implementation
  4257. 'var cLoc = 3;',
  4258. '$impl.cImpl = 2;',
  4259. '$impl.vImpl = 0;',
  4260. '$impl.DoIt = function () {',
  4261. ' var vLoc = 0;',
  4262. '};',
  4263. '']) );
  4264. end;
  4265. procedure TTestModule.TestTryFinally;
  4266. begin
  4267. StartProgram(false);
  4268. Add('var i: longint;');
  4269. Add('begin');
  4270. Add(' try');
  4271. Add(' i:=0; i:=2 div i;');
  4272. Add(' finally');
  4273. Add(' i:=3');
  4274. Add(' end;');
  4275. ConvertProgram;
  4276. CheckSource('TestTryFinally',
  4277. LinesToStr([ // statements
  4278. 'this.i = 0;'
  4279. ]),
  4280. LinesToStr([ // $mod.$main
  4281. 'try {',
  4282. ' $mod.i = 0;',
  4283. ' $mod.i = Math.floor(2 / $mod.i);',
  4284. '} finally {',
  4285. ' $mod.i = 3;',
  4286. '};'
  4287. ]));
  4288. end;
  4289. procedure TTestModule.TestTryExcept;
  4290. begin
  4291. StartProgram(false);
  4292. Add('type');
  4293. Add(' TObject = class end;');
  4294. Add(' Exception = class Msg: string; end;');
  4295. Add(' EInvalidCast = class(Exception) end;');
  4296. Add('var vI: longint;');
  4297. Add('begin');
  4298. Add(' try');
  4299. Add(' vi:=1;');
  4300. Add(' except');
  4301. Add(' vi:=2');
  4302. Add(' end;');
  4303. Add(' try');
  4304. Add(' vi:=3;');
  4305. Add(' except');
  4306. Add(' raise;');
  4307. Add(' end;');
  4308. Add(' try');
  4309. Add(' VI:=4;');
  4310. Add(' except');
  4311. Add(' on einvalidcast do');
  4312. Add(' raise;');
  4313. Add(' on E: exception do');
  4314. Add(' if e.msg='''' then');
  4315. Add(' raise e;');
  4316. Add(' else');
  4317. Add(' vi:=5');
  4318. Add(' end;');
  4319. Add(' try');
  4320. Add(' VI:=6;');
  4321. Add(' except');
  4322. Add(' on einvalidcast do ;');
  4323. Add(' end;');
  4324. ConvertProgram;
  4325. CheckSource('TestTryExcept',
  4326. LinesToStr([ // statements
  4327. 'rtl.createClass($mod, "TObject", null, function () {',
  4328. ' this.$init = function () {',
  4329. ' };',
  4330. ' this.$final = function () {',
  4331. ' };',
  4332. '});',
  4333. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  4334. ' this.$init = function () {',
  4335. ' $mod.TObject.$init.call(this);',
  4336. ' this.Msg = "";',
  4337. ' };',
  4338. '});',
  4339. 'rtl.createClass($mod, "EInvalidCast", $mod.Exception, function () {',
  4340. '});',
  4341. 'this.vI = 0;'
  4342. ]),
  4343. LinesToStr([ // $mod.$main
  4344. 'try {',
  4345. ' $mod.vI = 1;',
  4346. '} catch ($e) {',
  4347. ' $mod.vI = 2;',
  4348. '};',
  4349. 'try {',
  4350. ' $mod.vI = 3;',
  4351. '} catch ($e) {',
  4352. ' throw $e;',
  4353. '};',
  4354. 'try {',
  4355. ' $mod.vI = 4;',
  4356. '} catch ($e) {',
  4357. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  4358. ' throw $e',
  4359. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  4360. ' var E = $e;',
  4361. ' if (E.Msg == "") throw E;',
  4362. ' } else {',
  4363. ' $mod.vI = 5;',
  4364. ' }',
  4365. '};',
  4366. 'try {',
  4367. ' $mod.vI = 6;',
  4368. '} catch ($e) {',
  4369. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  4370. ' } else throw $e',
  4371. '};',
  4372. '']));
  4373. end;
  4374. procedure TTestModule.TestCaseOf;
  4375. begin
  4376. StartProgram(false);
  4377. Add('var vI: longint;');
  4378. Add('begin');
  4379. Add(' case vi of');
  4380. Add(' 1: ;');
  4381. Add(' 2: vi:=3;');
  4382. Add(' else');
  4383. Add(' VI:=4');
  4384. Add(' end;');
  4385. ConvertProgram;
  4386. CheckSource('TestCaseOf',
  4387. LinesToStr([ // statements
  4388. 'this.vI = 0;'
  4389. ]),
  4390. LinesToStr([ // $mod.$main
  4391. 'var $tmp1 = $mod.vI;',
  4392. 'if ($tmp1 == 1) {} else if ($tmp1 == 2){ $mod.vI = 3 }else {',
  4393. ' $mod.vI = 4;',
  4394. '};'
  4395. ]));
  4396. end;
  4397. procedure TTestModule.TestCaseOf_UseSwitch;
  4398. begin
  4399. StartProgram(false);
  4400. Converter.UseSwitchStatement:=true;
  4401. Add('var Vi: longint;');
  4402. Add('begin');
  4403. Add(' case vi of');
  4404. Add(' 1: ;');
  4405. Add(' 2: VI:=3;');
  4406. Add(' else');
  4407. Add(' vi:=4');
  4408. Add(' end;');
  4409. ConvertProgram;
  4410. CheckSource('TestCaseOf_UseSwitch',
  4411. LinesToStr([ // statements
  4412. 'this.Vi = 0;'
  4413. ]),
  4414. LinesToStr([ // $mod.$main
  4415. 'switch ($mod.Vi) {',
  4416. 'case 1:',
  4417. ' break;',
  4418. 'case 2:',
  4419. ' $mod.Vi = 3;',
  4420. ' break;',
  4421. 'default:',
  4422. ' $mod.Vi = 4;',
  4423. '};'
  4424. ]));
  4425. end;
  4426. procedure TTestModule.TestCaseOfNoElse;
  4427. begin
  4428. StartProgram(false);
  4429. Add('var Vi: longint;');
  4430. Add('begin');
  4431. Add(' case vi of');
  4432. Add(' 1: begin vi:=2; VI:=3; end;');
  4433. Add(' end;');
  4434. ConvertProgram;
  4435. CheckSource('TestCaseOfNoElse',
  4436. LinesToStr([ // statements
  4437. 'this.Vi = 0;'
  4438. ]),
  4439. LinesToStr([ // $mod.$main
  4440. 'var $tmp1 = $mod.Vi;',
  4441. 'if ($tmp1 == 1) {',
  4442. ' $mod.Vi = 2;',
  4443. ' $mod.Vi = 3;',
  4444. '};'
  4445. ]));
  4446. end;
  4447. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  4448. begin
  4449. StartProgram(false);
  4450. Converter.UseSwitchStatement:=true;
  4451. Add('var vI: longint;');
  4452. Add('begin');
  4453. Add(' case vi of');
  4454. Add(' 1: begin VI:=2; vi:=3; end;');
  4455. Add(' end;');
  4456. ConvertProgram;
  4457. CheckSource('TestCaseOfNoElse_UseSwitch',
  4458. LinesToStr([ // statements
  4459. 'this.vI = 0;'
  4460. ]),
  4461. LinesToStr([ // $mod.$main
  4462. 'switch ($mod.vI) {',
  4463. 'case 1:',
  4464. ' $mod.vI = 2;',
  4465. ' $mod.vI = 3;',
  4466. ' break;',
  4467. '};'
  4468. ]));
  4469. end;
  4470. procedure TTestModule.TestCaseOfRange;
  4471. begin
  4472. StartProgram(false);
  4473. Add('var vI: longint;');
  4474. Add('begin');
  4475. Add(' case vi of');
  4476. Add(' 1..3: vi:=14;');
  4477. Add(' 4,5: vi:=16;');
  4478. Add(' 6..7,9..10: ;');
  4479. Add(' else ;');
  4480. Add(' end;');
  4481. ConvertProgram;
  4482. CheckSource('TestCaseOfRange',
  4483. LinesToStr([ // statements
  4484. 'this.vI = 0;'
  4485. ]),
  4486. LinesToStr([ // $mod.$main
  4487. 'var $tmp1 = $mod.vI;',
  4488. 'if (($tmp1 >= 1) && ($tmp1 <= 3)){',
  4489. ' $mod.vI = 14',
  4490. '} else if (($tmp1 == 4) || ($tmp1 == 5)){',
  4491. ' $mod.vI = 16',
  4492. '} else if ((($tmp1 >= 6) && ($tmp1 <= 7)) || (($tmp1 >= 9) && ($tmp1 <= 10))) ;'
  4493. ]));
  4494. end;
  4495. procedure TTestModule.TestArray_Dynamic;
  4496. begin
  4497. StartProgram(false);
  4498. Add('type');
  4499. Add(' TArrayInt = array of longint;');
  4500. Add('var');
  4501. Add(' Arr: TArrayInt;');
  4502. Add(' i: longint;');
  4503. Add(' b: boolean;');
  4504. Add('begin');
  4505. Add(' SetLength(arr,3);');
  4506. Add(' arr[0]:=4;');
  4507. Add(' arr[1]:=length(arr)+arr[0];');
  4508. Add(' arr[i]:=5;');
  4509. Add(' arr[arr[i]]:=arr[6];');
  4510. Add(' i:=low(arr);');
  4511. Add(' i:=high(arr);');
  4512. Add(' b:=Assigned(arr);');
  4513. ConvertProgram;
  4514. CheckSource('TestArray_Dynamic',
  4515. LinesToStr([ // statements
  4516. 'this.Arr = [];',
  4517. 'this.i = 0;',
  4518. 'this.b = false;'
  4519. ]),
  4520. LinesToStr([ // $mod.$main
  4521. '$mod.Arr = rtl.arraySetLength($mod.Arr,3,0);',
  4522. '$mod.Arr[0] = 4;',
  4523. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  4524. '$mod.Arr[$mod.i] = 5;',
  4525. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  4526. '$mod.i = 0;',
  4527. '$mod.i = rtl.length($mod.Arr) - 1;',
  4528. '$mod.b = rtl.length($mod.Arr) > 0;',
  4529. '']));
  4530. end;
  4531. procedure TTestModule.TestArray_Dynamic_Nil;
  4532. begin
  4533. StartProgram(false);
  4534. Add('type');
  4535. Add(' TArrayInt = array of longint;');
  4536. Add('var');
  4537. Add(' Arr: TArrayInt;');
  4538. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  4539. Add('begin');
  4540. Add(' arr:=nil;');
  4541. Add(' if arr=nil then;');
  4542. Add(' if nil=arr then;');
  4543. Add(' if arr<>nil then;');
  4544. Add(' if nil<>arr then;');
  4545. Add(' DoIt(nil,nil);');
  4546. ConvertProgram;
  4547. CheckSource('TestArray_Dynamic',
  4548. LinesToStr([ // statements
  4549. 'this.Arr = [];',
  4550. 'this.DoIt = function(i,j){',
  4551. '};'
  4552. ]),
  4553. LinesToStr([ // $mod.$main
  4554. '$mod.Arr = [];',
  4555. 'if (rtl.length($mod.Arr) == 0) ;',
  4556. 'if (rtl.length($mod.Arr) == 0) ;',
  4557. 'if (rtl.length($mod.Arr) > 0) ;',
  4558. 'if (rtl.length($mod.Arr) > 0) ;',
  4559. '$mod.DoIt([],[]);',
  4560. '']));
  4561. end;
  4562. procedure TTestModule.TestArray_DynMultiDimensional;
  4563. begin
  4564. StartProgram(false);
  4565. Add('type');
  4566. Add(' TArrayInt = array of longint;');
  4567. Add(' TArrayArrayInt = array of TArrayInt;');
  4568. Add('var');
  4569. Add(' Arr: TArrayInt;');
  4570. Add(' Arr2: TArrayArrayInt;');
  4571. Add(' i: longint;');
  4572. Add('begin');
  4573. Add(' arr2:=nil;');
  4574. Add(' if arr2=nil then;');
  4575. Add(' if nil=arr2 then;');
  4576. Add(' i:=low(arr2);');
  4577. Add(' i:=low(arr2[1]);');
  4578. Add(' i:=high(arr2);');
  4579. Add(' i:=high(arr2[2]);');
  4580. Add(' arr2[3]:=arr;');
  4581. Add(' arr2[4][5]:=i;');
  4582. Add(' i:=arr2[6][7];');
  4583. Add(' arr2[8,9]:=i;');
  4584. Add(' i:=arr2[10,11];');
  4585. Add(' SetLength(arr2,14);');
  4586. Add(' SetLength(arr2[15],16);');
  4587. ConvertProgram;
  4588. CheckSource('TestArray_Dynamic',
  4589. LinesToStr([ // statements
  4590. 'this.Arr = [];',
  4591. 'this.Arr2 = [];',
  4592. 'this.i = 0;'
  4593. ]),
  4594. LinesToStr([ // $mod.$main
  4595. '$mod.Arr2 = [];',
  4596. 'if (rtl.length($mod.Arr2) == 0) ;',
  4597. 'if (rtl.length($mod.Arr2) == 0) ;',
  4598. '$mod.i = 0;',
  4599. '$mod.i = 0;',
  4600. '$mod.i = rtl.length($mod.Arr2) - 1;',
  4601. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  4602. '$mod.Arr2[3] = $mod.Arr;',
  4603. '$mod.Arr2[4][5] = $mod.i;',
  4604. '$mod.i = $mod.Arr2[6][7];',
  4605. '$mod.Arr2[8][9] = $mod.i;',
  4606. '$mod.i = $mod.Arr2[10][11];',
  4607. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, 14, []);',
  4608. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 16, 0);',
  4609. '']));
  4610. end;
  4611. procedure TTestModule.TestArrayOfRecord;
  4612. begin
  4613. StartProgram(false);
  4614. Add('type');
  4615. Add(' TRec = record');
  4616. Add(' Int: longint;');
  4617. Add(' end;');
  4618. Add(' TArrayRec = array of TRec;');
  4619. Add('var');
  4620. Add(' Arr: TArrayRec;');
  4621. Add(' r: TRec;');
  4622. Add(' i: longint;');
  4623. Add('begin');
  4624. Add(' SetLength(arr,3);');
  4625. Add(' arr[0].int:=4;');
  4626. Add(' arr[1].int:=length(arr)+arr[2].int;');
  4627. Add(' arr[arr[i].int].int:=arr[5].int;');
  4628. Add(' arr[7]:=r;');
  4629. Add(' r:=arr[8];');
  4630. Add(' i:=low(arr);');
  4631. Add(' i:=high(arr);');
  4632. ConvertProgram;
  4633. CheckSource('TestArrayOfRecord',
  4634. LinesToStr([ // statements
  4635. 'this.TRec = function (s) {',
  4636. ' if (s) {',
  4637. ' this.Int = s.Int;',
  4638. ' } else {',
  4639. ' this.Int = 0;',
  4640. ' };',
  4641. ' this.$equal = function (b) {',
  4642. ' return this.Int == b.Int;',
  4643. ' };',
  4644. '};',
  4645. 'this.Arr = [];',
  4646. 'this.r = new $mod.TRec();',
  4647. 'this.i = 0;'
  4648. ]),
  4649. LinesToStr([ // $mod.$main
  4650. '$mod.Arr = rtl.arraySetLength($mod.Arr,3, $mod.TRec);',
  4651. '$mod.Arr[0].Int = 4;',
  4652. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  4653. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  4654. '$mod.Arr[7] = new $mod.TRec($mod.r);',
  4655. '$mod.r = new $mod.TRec($mod.Arr[8]);',
  4656. '$mod.i = 0;',
  4657. '$mod.i = rtl.length($mod.Arr)-1;',
  4658. '']));
  4659. end;
  4660. procedure TTestModule.TestArray_AsParams;
  4661. begin
  4662. StartProgram(false);
  4663. Add('type integer = longint;');
  4664. Add('type TArrInt = array of integer;');
  4665. Add('procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);');
  4666. Add('var vJ: TArrInt;');
  4667. Add('begin');
  4668. Add(' vg:=vg;');
  4669. Add(' vj:=vh;');
  4670. Add(' vi:=vi;');
  4671. Add(' doit(vg,vg,vg);');
  4672. Add(' doit(vh,vh,vj);');
  4673. Add(' doit(vi,vi,vi);');
  4674. Add(' doit(vj,vj,vj);');
  4675. Add('end;');
  4676. Add('var i: TArrInt;');
  4677. Add('begin');
  4678. Add(' doit(i,i,i);');
  4679. ConvertProgram;
  4680. CheckSource('TestArray_AsParams',
  4681. LinesToStr([ // statements
  4682. 'this.DoIt = function (vG,vH,vI) {',
  4683. ' var vJ = [];',
  4684. ' vG = vG;',
  4685. ' vJ = vH;',
  4686. ' vI.set(vI.get());',
  4687. ' $mod.DoIt(vG, vG, {',
  4688. ' get: function () {',
  4689. ' return vG;',
  4690. ' },',
  4691. ' set: function (v) {',
  4692. ' vG = v;',
  4693. ' }',
  4694. ' });',
  4695. ' $mod.DoIt(vH, vH, {',
  4696. ' get: function () {',
  4697. ' return vJ;',
  4698. ' },',
  4699. ' set: function (v) {',
  4700. ' vJ = v;',
  4701. ' }',
  4702. ' });',
  4703. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4704. ' $mod.DoIt(vJ, vJ, {',
  4705. ' get: function () {',
  4706. ' return vJ;',
  4707. ' },',
  4708. ' set: function (v) {',
  4709. ' vJ = v;',
  4710. ' }',
  4711. ' });',
  4712. '};',
  4713. 'this.i = [];'
  4714. ]),
  4715. LinesToStr([
  4716. '$mod.DoIt($mod.i,$mod.i,{',
  4717. ' p: $mod,',
  4718. ' get: function () {',
  4719. ' return this.p.i;',
  4720. ' },',
  4721. ' set: function (v) {',
  4722. ' this.p.i = v;',
  4723. ' }',
  4724. '});'
  4725. ]));
  4726. end;
  4727. procedure TTestModule.TestArrayElement_AsParams;
  4728. begin
  4729. StartProgram(false);
  4730. Add('type integer = longint;');
  4731. Add('type TArrayInt = array of integer;');
  4732. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  4733. Add('var vJ: tarrayint;');
  4734. Add('begin');
  4735. Add(' vi:=vi;');
  4736. Add(' doit(vi,vi,vi);');
  4737. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  4738. Add('end;');
  4739. Add('var a: TArrayInt;');
  4740. Add('begin');
  4741. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  4742. ConvertProgram;
  4743. CheckSource('TestArrayElement_AsParams',
  4744. LinesToStr([ // statements
  4745. 'this.DoIt = function (vG,vH,vI) {',
  4746. ' var vJ = [];',
  4747. ' vI.set(vI.get());',
  4748. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4749. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  4750. ' a:1+3,',
  4751. ' p:vJ,',
  4752. ' get: function () {',
  4753. ' return this.p[this.a];',
  4754. ' },',
  4755. ' set: function (v) {',
  4756. ' this.p[this.a] = v;',
  4757. ' }',
  4758. ' });',
  4759. '};',
  4760. 'this.a = [];'
  4761. ]),
  4762. LinesToStr([
  4763. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  4764. ' a: 1+6,',
  4765. ' p: $mod.a,',
  4766. ' get: function () {',
  4767. ' return this.p[this.a];',
  4768. ' },',
  4769. ' set: function (v) {',
  4770. ' this.p[this.a] = v;',
  4771. ' }',
  4772. '});'
  4773. ]));
  4774. end;
  4775. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  4776. begin
  4777. StartProgram(false);
  4778. Add('type Integer = longint;');
  4779. Add('type TArrayInt = array of integer;');
  4780. Add('function GetArr(vB: integer = 0): tarrayint;');
  4781. Add('begin');
  4782. Add('end;');
  4783. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  4784. Add('begin');
  4785. Add('end;');
  4786. Add('begin');
  4787. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  4788. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  4789. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  4790. ConvertProgram;
  4791. CheckSource('TestArrayElementFromFuncResult_AsParams',
  4792. LinesToStr([ // statements
  4793. 'this.GetArr = function (vB) {',
  4794. ' var Result = [];',
  4795. ' return Result;',
  4796. '};',
  4797. 'this.DoIt = function (vG,vH,vI) {',
  4798. '};'
  4799. ]),
  4800. LinesToStr([
  4801. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  4802. ' a: 1+3,',
  4803. ' p: $mod.GetArr(0),',
  4804. ' get: function () {',
  4805. ' return this.p[this.a];',
  4806. ' },',
  4807. ' set: function (v) {',
  4808. ' this.p[this.a] = v;',
  4809. ' }',
  4810. '});',
  4811. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  4812. ' a: 2+3,',
  4813. ' p: $mod.GetArr(0),',
  4814. ' get: function () {',
  4815. ' return this.p[this.a];',
  4816. ' },',
  4817. ' set: function (v) {',
  4818. ' this.p[this.a] = v;',
  4819. ' }',
  4820. '});',
  4821. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  4822. ' a: 3+3,',
  4823. ' p: $mod.GetArr(9),',
  4824. ' get: function () {',
  4825. ' return this.p[this.a];',
  4826. ' },',
  4827. ' set: function (v) {',
  4828. ' this.p[this.a] = v;',
  4829. ' }',
  4830. '});',
  4831. '']));
  4832. end;
  4833. procedure TTestModule.TestArrayEnumTypeRange;
  4834. begin
  4835. StartProgram(false);
  4836. Add('type');
  4837. Add(' TEnum = (red,blue);');
  4838. Add(' TEnumArray = array[TEnum] of longint;');
  4839. Add('var');
  4840. Add(' e: TEnum;');
  4841. Add(' i: longint;');
  4842. Add(' a: TEnumArray;');
  4843. Add(' numbers: TEnumArray = (1,2);');
  4844. Add(' names: array[TEnum] of string = (''red'',''blue'');');
  4845. Add('begin');
  4846. Add(' e:=low(a);');
  4847. Add(' e:=high(a);');
  4848. Add(' i:=a[red]+length(a);');
  4849. Add(' a[e]:=a[e];');
  4850. ConvertProgram;
  4851. CheckSource('TestArrayEnumTypeRange',
  4852. LinesToStr([ // statements
  4853. ' this.TEnum = {',
  4854. ' "0": "red",',
  4855. ' red: 0,',
  4856. ' "1": "blue",',
  4857. ' blue: 1',
  4858. '};',
  4859. 'this.e = 0;',
  4860. 'this.i = 0;',
  4861. 'this.a = rtl.arrayNewMultiDim([2],0);',
  4862. 'this.numbers = [1, 2];',
  4863. 'this.names = ["red", "blue"];',
  4864. '']),
  4865. LinesToStr([ // $mod.$main
  4866. '$mod.e = $mod.TEnum.red;',
  4867. '$mod.e = $mod.TEnum.blue;',
  4868. '$mod.i = $mod.a[$mod.TEnum.red]+2;',
  4869. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  4870. '']));
  4871. end;
  4872. procedure TTestModule.TestArray_SetLengthOutArg;
  4873. begin
  4874. StartProgram(false);
  4875. Add([
  4876. 'type TArrInt = array of longint;',
  4877. 'procedure DoIt(out a: TArrInt);',
  4878. 'begin',
  4879. ' SetLength(a,2);',
  4880. 'end;',
  4881. 'begin',
  4882. '']);
  4883. ConvertProgram;
  4884. CheckSource('TestArray_SetLengthOutArg',
  4885. LinesToStr([ // statements
  4886. 'this.DoIt = function (a) {',
  4887. ' a.set(rtl.arraySetLength(a.get(), 2, 0));',
  4888. '};',
  4889. '']),
  4890. LinesToStr([
  4891. '']));
  4892. end;
  4893. procedure TTestModule.TestArray_SetLengthProperty;
  4894. begin
  4895. StartProgram(false);
  4896. Add('type');
  4897. Add(' TArrInt = array of longint;');
  4898. Add(' TObject = class');
  4899. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  4900. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  4901. Add(' property Colors: TArrInt read GetColors write SetColors;');
  4902. Add(' end;');
  4903. Add('var Obj: TObject;');
  4904. Add('begin');
  4905. Add(' SetLength(Obj.Colors,2);');
  4906. ConvertProgram;
  4907. CheckSource('TestArray_SetLengthProperty',
  4908. LinesToStr([ // statements
  4909. 'rtl.createClass($mod, "TObject", null, function () {',
  4910. ' this.$init = function () {',
  4911. ' };',
  4912. ' this.$final = function () {',
  4913. ' };',
  4914. '});',
  4915. 'this.Obj = null;',
  4916. '']),
  4917. LinesToStr([
  4918. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 2, 0));',
  4919. '']));
  4920. end;
  4921. procedure TTestModule.TestArray_OpenArrayOfString;
  4922. begin
  4923. StartProgram(false);
  4924. Add('procedure DoIt(const a: array of String);');
  4925. Add('var');
  4926. Add(' i: longint;');
  4927. Add(' s: string;');
  4928. Add('begin');
  4929. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  4930. Add('end;');
  4931. Add('var s: string;');
  4932. Add('begin');
  4933. Add(' DoIt([]);');
  4934. Add(' DoIt([s,''foo'','''',s+s]);');
  4935. ConvertProgram;
  4936. CheckSource('TestArray_OpenArrayOfString',
  4937. LinesToStr([ // statements
  4938. 'this.DoIt = function (a) {',
  4939. ' var i = 0;',
  4940. ' var s = "";',
  4941. ' var $loopend1 = rtl.length(a) - 1;',
  4942. ' for (i = 0; i <= $loopend1; i++) s = a[(rtl.length(a) - i) - 1];',
  4943. '};',
  4944. 'this.s = "";',
  4945. '']),
  4946. LinesToStr([
  4947. '$mod.DoIt([]);',
  4948. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  4949. '']));
  4950. end;
  4951. procedure TTestModule.TestArray_Concat;
  4952. begin
  4953. StartProgram(false);
  4954. Add('type');
  4955. Add(' integer = longint;');
  4956. Add(' TFlag = (big,small);');
  4957. Add(' TFlags = set of TFlag;');
  4958. Add(' TRec = record');
  4959. Add(' i: integer;');
  4960. Add(' end;');
  4961. Add(' TArrInt = array of integer;');
  4962. Add(' TArrRec = array of TRec;');
  4963. Add(' TArrSet = array of TFlags;');
  4964. Add(' TArrJSValue = array of jsvalue;');
  4965. Add('var');
  4966. Add(' ArrInt: tarrint;');
  4967. Add(' ArrRec: tarrrec;');
  4968. Add(' ArrSet: tarrset;');
  4969. Add(' ArrJSValue: tarrjsvalue;');
  4970. Add('begin');
  4971. Add(' arrint:=concat(arrint);');
  4972. Add(' arrint:=concat(arrint,arrint);');
  4973. Add(' arrint:=concat(arrint,arrint,arrint);');
  4974. Add(' arrrec:=concat(arrrec);');
  4975. Add(' arrrec:=concat(arrrec,arrrec);');
  4976. Add(' arrrec:=concat(arrrec,arrrec,arrrec);');
  4977. Add(' arrset:=concat(arrset);');
  4978. Add(' arrset:=concat(arrset,arrset);');
  4979. Add(' arrset:=concat(arrset,arrset,arrset);');
  4980. Add(' arrjsvalue:=concat(arrjsvalue);');
  4981. Add(' arrjsvalue:=concat(arrjsvalue,arrjsvalue);');
  4982. Add(' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);');
  4983. ConvertProgram;
  4984. CheckSource('TestArray_Concat',
  4985. LinesToStr([ // statements
  4986. 'this.TFlag = {',
  4987. ' "0": "big",',
  4988. ' big: 0,',
  4989. ' "1": "small",',
  4990. ' small: 1',
  4991. '};',
  4992. 'this.TRec = function (s) {',
  4993. ' if (s) {',
  4994. ' this.i = s.i;',
  4995. ' } else {',
  4996. ' this.i = 0;',
  4997. ' };',
  4998. ' this.$equal = function (b) {',
  4999. ' return this.i == b.i;',
  5000. ' };',
  5001. '};',
  5002. 'this.ArrInt = [];',
  5003. 'this.ArrRec = [];',
  5004. 'this.ArrSet = [];',
  5005. 'this.ArrJSValue = [];',
  5006. '']),
  5007. LinesToStr([ // $mod.$main
  5008. '$mod.ArrInt = $mod.ArrInt;',
  5009. '$mod.ArrInt = $mod.ArrInt.concat($mod.ArrInt);',
  5010. '$mod.ArrInt = $mod.ArrInt.concat($mod.ArrInt,$mod.ArrInt);',
  5011. '$mod.ArrRec = $mod.ArrRec;',
  5012. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec);',
  5013. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  5014. '$mod.ArrSet = $mod.ArrSet;',
  5015. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet);',
  5016. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  5017. '$mod.ArrJSValue = $mod.ArrJSValue;',
  5018. '$mod.ArrJSValue = $mod.ArrJSValue.concat($mod.ArrJSValue);',
  5019. '$mod.ArrJSValue = $mod.ArrJSValue.concat($mod.ArrJSValue, $mod.ArrJSValue);',
  5020. '']));
  5021. end;
  5022. procedure TTestModule.TestArray_Copy;
  5023. begin
  5024. StartProgram(false);
  5025. Add('type');
  5026. Add(' integer = longint;');
  5027. Add(' TFlag = (big,small);');
  5028. Add(' TFlags = set of TFlag;');
  5029. Add(' TRec = record');
  5030. Add(' i: integer;');
  5031. Add(' end;');
  5032. Add(' TArrInt = array of integer;');
  5033. Add(' TArrRec = array of TRec;');
  5034. Add(' TArrSet = array of TFlags;');
  5035. Add(' TArrJSValue = array of jsvalue;');
  5036. Add('var');
  5037. Add(' ArrInt: tarrint;');
  5038. Add(' ArrRec: tarrrec;');
  5039. Add(' ArrSet: tarrset;');
  5040. Add(' ArrJSValue: tarrjsvalue;');
  5041. Add('begin');
  5042. Add(' arrint:=copy(arrint);');
  5043. Add(' arrint:=copy(arrint,2);');
  5044. Add(' arrint:=copy(arrint,3,4);');
  5045. Add(' arrrec:=copy(arrrec);');
  5046. Add(' arrrec:=copy(arrrec,5);');
  5047. Add(' arrrec:=copy(arrrec,6,7);');
  5048. Add(' arrset:=copy(arrset);');
  5049. Add(' arrset:=copy(arrset,8);');
  5050. Add(' arrset:=copy(arrset,9,10);');
  5051. Add(' arrjsvalue:=copy(arrjsvalue);');
  5052. Add(' arrjsvalue:=copy(arrjsvalue,11);');
  5053. Add(' arrjsvalue:=copy(arrjsvalue,12,13);');
  5054. ConvertProgram;
  5055. CheckSource('TestArray_Copy',
  5056. LinesToStr([ // statements
  5057. 'this.TFlag = {',
  5058. ' "0": "big",',
  5059. ' big: 0,',
  5060. ' "1": "small",',
  5061. ' small: 1',
  5062. '};',
  5063. 'this.TRec = function (s) {',
  5064. ' if (s) {',
  5065. ' this.i = s.i;',
  5066. ' } else {',
  5067. ' this.i = 0;',
  5068. ' };',
  5069. ' this.$equal = function (b) {',
  5070. ' return this.i == b.i;',
  5071. ' };',
  5072. '};',
  5073. 'this.ArrInt = [];',
  5074. 'this.ArrRec = [];',
  5075. 'this.ArrSet = [];',
  5076. 'this.ArrJSValue = [];',
  5077. '']),
  5078. LinesToStr([ // $mod.$main
  5079. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  5080. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  5081. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  5082. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  5083. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  5084. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  5085. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  5086. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  5087. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  5088. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  5089. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  5090. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  5091. '']));
  5092. end;
  5093. procedure TTestModule.TestArray_InsertDelete;
  5094. begin
  5095. StartProgram(false);
  5096. Add('type');
  5097. Add(' integer = longint;');
  5098. Add(' TFlag = (big,small);');
  5099. Add(' TFlags = set of TFlag;');
  5100. Add(' TRec = record');
  5101. Add(' i: integer;');
  5102. Add(' end;');
  5103. Add(' TArrInt = array of integer;');
  5104. Add(' TArrRec = array of TRec;');
  5105. Add(' TArrSet = array of TFlags;');
  5106. Add(' TArrJSValue = array of jsvalue;');
  5107. Add('var');
  5108. Add(' ArrInt: tarrint;');
  5109. Add(' ArrRec: tarrrec;');
  5110. Add(' ArrSet: tarrset;');
  5111. Add(' ArrJSValue: tarrjsvalue;');
  5112. Add('begin');
  5113. Add(' Insert(1,arrint,2);');
  5114. Add(' Insert(arrint[3],arrint,4);');
  5115. Add(' Insert(arrrec[5],arrrec,6);');
  5116. Add(' Insert(arrset[7],arrset,7);');
  5117. Add(' Insert(arrjsvalue[8],arrjsvalue,9);');
  5118. Add(' Insert(10,arrjsvalue,11);');
  5119. Add(' Delete(arrint,12,13);');
  5120. Add(' Delete(arrrec,14,15);');
  5121. Add(' Delete(arrset,17,18);');
  5122. Add(' Delete(arrjsvalue,19,10);');
  5123. ConvertProgram;
  5124. CheckSource('TestArray_InsertDelete',
  5125. LinesToStr([ // statements
  5126. 'this.TFlag = {',
  5127. ' "0": "big",',
  5128. ' big: 0,',
  5129. ' "1": "small",',
  5130. ' small: 1',
  5131. '};',
  5132. 'this.TRec = function (s) {',
  5133. ' if (s) {',
  5134. ' this.i = s.i;',
  5135. ' } else {',
  5136. ' this.i = 0;',
  5137. ' };',
  5138. ' this.$equal = function (b) {',
  5139. ' return this.i == b.i;',
  5140. ' };',
  5141. '};',
  5142. 'this.ArrInt = [];',
  5143. 'this.ArrRec = [];',
  5144. 'this.ArrSet = [];',
  5145. 'this.ArrJSValue = [];',
  5146. '']),
  5147. LinesToStr([ // $mod.$main
  5148. '$mod.ArrInt.splice(2, 1, 1);',
  5149. '$mod.ArrInt.splice(4, 1, $mod.ArrInt[3]);',
  5150. '$mod.ArrRec.splice(6, 1, $mod.ArrRec[5]);',
  5151. '$mod.ArrSet.splice(7, 1, $mod.ArrSet[7]);',
  5152. '$mod.ArrJSValue.splice(9, 1, $mod.ArrJSValue[8]);',
  5153. '$mod.ArrJSValue.splice(11, 1, 10);',
  5154. '$mod.ArrInt.splice(12, 13);',
  5155. '$mod.ArrRec.splice(14, 15);',
  5156. '$mod.ArrSet.splice(17, 18);',
  5157. '$mod.ArrJSValue.splice(19, 10);',
  5158. '']));
  5159. end;
  5160. procedure TTestModule.TestArray_DynArrayConst;
  5161. begin
  5162. StartProgram(false);
  5163. Add([
  5164. 'type',
  5165. ' integer = longint;',
  5166. ' TArrInt = array of integer;',
  5167. ' TArrStr = array of string;',
  5168. 'const',
  5169. ' Ints: TArrInt = (1,2,3);',
  5170. ' Names: array of string = (''a'',''foo'');',
  5171. ' Aliases: TarrStr = (''foo'',''b'');',
  5172. ' OneInt: TArrInt = (7);',
  5173. ' OneStr: array of integer = (7);',
  5174. //' Chars: array of char = ''aoc'';',
  5175. 'begin',
  5176. '']);
  5177. ConvertProgram;
  5178. CheckSource('TestArray_DynArrayConst',
  5179. LinesToStr([ // statements
  5180. 'this.Ints = [1, 2, 3];',
  5181. 'this.Names = ["a", "foo"];',
  5182. 'this.Aliases = ["foo", "b"];',
  5183. 'this.OneInt = [7];',
  5184. 'this.OneStr = [7];',
  5185. '']),
  5186. LinesToStr([ // $mod.$main
  5187. '']));
  5188. end;
  5189. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalArray;
  5190. begin
  5191. StartProgram(false);
  5192. Add('{$modeswitch externalclass}');
  5193. Add('type');
  5194. Add(' TJSArray = class external name ''Array''');
  5195. Add(' class function isArray(Value: JSValue) : boolean;');
  5196. Add(' function concat() : TJSArray; varargs;');
  5197. Add(' end;');
  5198. Add('var');
  5199. Add(' aObj: TJSArray;');
  5200. Add(' a: array of longint;');
  5201. Add('begin');
  5202. Add(' if TJSArray.isArray(65) then ;');
  5203. Add(' aObj:=TJSArray(a).concat(a);');
  5204. ConvertProgram;
  5205. CheckSource('TestExternalClass_TypeCastArrayToExternalArray',
  5206. LinesToStr([ // statements
  5207. 'this.aObj = null;',
  5208. 'this.a = [];',
  5209. '']),
  5210. LinesToStr([ // $mod.$main
  5211. 'if (Array.isArray(65)) ;',
  5212. '$mod.aObj = $mod.a.concat($mod.a);',
  5213. '']));
  5214. end;
  5215. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalArray;
  5216. begin
  5217. StartProgram(false);
  5218. Add('{$modeswitch externalclass}');
  5219. Add('type');
  5220. Add(' TArrStr = array of string;');
  5221. Add(' TJSArray = class external name ''Array''');
  5222. Add(' end;');
  5223. Add('var');
  5224. Add(' aObj: TJSArray;');
  5225. Add(' a: TArrStr;');
  5226. Add('begin');
  5227. Add(' a:=TArrStr(aObj);');
  5228. Add(' TArrStr(aObj)[1]:=TArrStr(aObj)[2];');
  5229. ConvertProgram;
  5230. CheckSource('TestExternalClass_TypeCastArrayFromExternalArray',
  5231. LinesToStr([ // statements
  5232. 'this.aObj = null;',
  5233. 'this.a = [];',
  5234. '']),
  5235. LinesToStr([ // $mod.$main
  5236. '$mod.a = $mod.aObj;',
  5237. '$mod.aObj[1] = $mod.aObj[2];',
  5238. '']));
  5239. end;
  5240. procedure TTestModule.TestRecord_Var;
  5241. begin
  5242. StartProgram(false);
  5243. Add('type');
  5244. Add(' TRecA = record');
  5245. Add(' Bold: longint;');
  5246. Add(' end;');
  5247. Add('var Rec: TRecA;');
  5248. Add('begin');
  5249. Add(' rec.bold:=123');
  5250. ConvertProgram;
  5251. CheckSource('TestRecord_Var',
  5252. LinesToStr([ // statements
  5253. 'this.TRecA = function (s) {',
  5254. ' if (s) {',
  5255. ' this.Bold = s.Bold;',
  5256. ' } else {',
  5257. ' this.Bold = 0;',
  5258. ' };',
  5259. ' this.$equal = function (b) {',
  5260. ' return this.Bold == b.Bold;',
  5261. ' };',
  5262. '};',
  5263. 'this.Rec = new $mod.TRecA();'
  5264. ]),
  5265. LinesToStr([ // $mod.$main
  5266. '$mod.Rec.Bold = 123;'
  5267. ]));
  5268. end;
  5269. procedure TTestModule.TestWithRecordDo;
  5270. begin
  5271. StartProgram(false);
  5272. Add('type');
  5273. Add(' TRec = record');
  5274. Add(' vI: longint;');
  5275. Add(' end;');
  5276. Add('var');
  5277. Add(' Int: longint;');
  5278. Add(' r: TRec;');
  5279. Add('begin');
  5280. Add(' with r do');
  5281. Add(' int:=vi;');
  5282. Add(' with r do begin');
  5283. Add(' int:=vi;');
  5284. Add(' vi:=int;');
  5285. Add(' end;');
  5286. ConvertProgram;
  5287. CheckSource('TestWithRecordDo',
  5288. LinesToStr([ // statements
  5289. 'this.TRec = function (s) {',
  5290. ' if (s) {',
  5291. ' this.vI = s.vI;',
  5292. ' } else {',
  5293. ' this.vI = 0;',
  5294. ' };',
  5295. ' this.$equal = function (b) {',
  5296. ' return this.vI == b.vI;',
  5297. ' };',
  5298. '};',
  5299. 'this.Int = 0;',
  5300. 'this.r = new $mod.TRec();'
  5301. ]),
  5302. LinesToStr([ // $mod.$main
  5303. 'var $with1 = $mod.r;',
  5304. '$mod.Int = $with1.vI;',
  5305. 'var $with2 = $mod.r;',
  5306. '$mod.Int = $with2.vI;',
  5307. '$with2.vI = $mod.Int;'
  5308. ]));
  5309. end;
  5310. procedure TTestModule.TestRecord_Assign;
  5311. begin
  5312. StartProgram(false);
  5313. Add('type');
  5314. Add(' TEnum = (red,green);');
  5315. Add(' TEnums = set of TEnum;');
  5316. Add(' TSmallRec = record');
  5317. Add(' N: longint;');
  5318. Add(' end;');
  5319. Add(' TBigRec = record');
  5320. Add(' Int: longint;');
  5321. Add(' D: double;');
  5322. Add(' Arr: array of longint;');
  5323. Add(' Small: TSmallRec;');
  5324. Add(' Enums: TEnums;');
  5325. Add(' end;');
  5326. Add('var');
  5327. Add(' r, s: TBigRec;');
  5328. Add('begin');
  5329. Add(' r:=s;');
  5330. ConvertProgram;
  5331. CheckSource('TestRecord_Assign',
  5332. LinesToStr([ // statements
  5333. 'this.TEnum = {',
  5334. ' "0": "red",',
  5335. ' red: 0,',
  5336. ' "1": "green",',
  5337. ' green: 1',
  5338. '};',
  5339. 'this.TSmallRec = function (s) {',
  5340. ' if(s){',
  5341. ' this.N = s.N;',
  5342. ' } else {',
  5343. ' this.N = 0;',
  5344. ' };',
  5345. ' this.$equal = function (b) {',
  5346. ' return this.N == b.N;',
  5347. ' };',
  5348. '};',
  5349. 'this.TBigRec = function (s) {',
  5350. ' if(s){',
  5351. ' this.Int = s.Int;',
  5352. ' this.D = s.D;',
  5353. ' this.Arr = s.Arr;',
  5354. ' this.Small = new $mod.TSmallRec(s.Small);',
  5355. ' this.Enums = rtl.refSet(s.Enums);',
  5356. ' } else {',
  5357. ' this.Int = 0;',
  5358. ' this.D = 0.0;',
  5359. ' this.Arr = [];',
  5360. ' this.Small = new $mod.TSmallRec();',
  5361. ' this.Enums = {};',
  5362. ' };',
  5363. ' this.$equal = function (b) {',
  5364. ' return (this.Int == b.Int) && ((this.D == b.D) && ((this.Arr == b.Arr)',
  5365. ' && (this.Small.$equal(b.Small) && rtl.eqSet(this.Enums, b.Enums))));',
  5366. ' };',
  5367. '};',
  5368. 'this.r = new $mod.TBigRec();',
  5369. 'this.s = new $mod.TBigRec();'
  5370. ]),
  5371. LinesToStr([ // $mod.$main
  5372. '$mod.r = new $mod.TBigRec($mod.s);',
  5373. '']));
  5374. end;
  5375. procedure TTestModule.TestRecord_PassAsArgClone;
  5376. begin
  5377. StartProgram(false);
  5378. Add('type');
  5379. Add(' TRecA = record');
  5380. Add(' Bold: longint;');
  5381. Add(' end;');
  5382. Add('procedure DoDefault(r: treca); begin end;');
  5383. Add('procedure DoConst(const r: treca); begin end;');
  5384. Add('var Rec: treca;');
  5385. Add('begin');
  5386. Add(' dodefault(rec);');
  5387. Add(' doconst(rec);');
  5388. ConvertProgram;
  5389. CheckSource('TestRecord_PassAsArgClone',
  5390. LinesToStr([ // statements
  5391. 'this.TRecA = function (s) {',
  5392. ' if (s) {',
  5393. ' this.Bold = s.Bold;',
  5394. ' } else {',
  5395. ' this.Bold = 0;',
  5396. ' };',
  5397. ' this.$equal = function (b) {',
  5398. ' return this.Bold == b.Bold;',
  5399. ' };',
  5400. '};',
  5401. 'this.DoDefault = function (r) {',
  5402. '};',
  5403. 'this.DoConst = function (r) {',
  5404. '};',
  5405. 'this.Rec = new $mod.TRecA();'
  5406. ]),
  5407. LinesToStr([ // $mod.$main
  5408. '$mod.DoDefault(new $mod.TRecA($mod.Rec));',
  5409. '$mod.DoConst($mod.Rec);',
  5410. '']));
  5411. end;
  5412. procedure TTestModule.TestRecord_AsParams;
  5413. begin
  5414. StartProgram(false);
  5415. Add('type');
  5416. Add(' integer = longint;');
  5417. Add(' TRecord = record');
  5418. Add(' i: integer;');
  5419. Add(' end;');
  5420. Add('procedure DoIt(vG: TRecord; const vH: TRecord; var vI: TRecord);');
  5421. Add('var vJ: TRecord;');
  5422. Add('begin');
  5423. Add(' vg:=vg;');
  5424. Add(' vj:=vh;');
  5425. Add(' vi:=vi;');
  5426. Add(' doit(vg,vg,vg);');
  5427. Add(' doit(vh,vh,vj);');
  5428. Add(' doit(vi,vi,vi);');
  5429. Add(' doit(vj,vj,vj);');
  5430. Add('end;');
  5431. Add('var i: TRecord;');
  5432. Add('begin');
  5433. Add(' doit(i,i,i);');
  5434. ConvertProgram;
  5435. CheckSource('TestRecord_AsParams',
  5436. LinesToStr([ // statements
  5437. 'this.TRecord = function (s) {',
  5438. ' if (s) {',
  5439. ' this.i = s.i;',
  5440. ' } else {',
  5441. ' this.i = 0;',
  5442. ' };',
  5443. ' this.$equal = function (b) {',
  5444. ' return this.i == b.i;',
  5445. ' };',
  5446. '};',
  5447. 'this.DoIt = function (vG,vH,vI) {',
  5448. ' var vJ = new $mod.TRecord();',
  5449. ' vG = new $mod.TRecord(vG);',
  5450. ' vJ = new $mod.TRecord(vH);',
  5451. ' vI.set(new $mod.TRecord(vI.get()));',
  5452. ' $mod.DoIt(new $mod.TRecord(vG), vG, {',
  5453. ' get: function () {',
  5454. ' return vG;',
  5455. ' },',
  5456. ' set: function (v) {',
  5457. ' vG = v;',
  5458. ' }',
  5459. ' });',
  5460. ' $mod.DoIt(new $mod.TRecord(vH), vH, {',
  5461. ' get: function () {',
  5462. ' return vJ;',
  5463. ' },',
  5464. ' set: function (v) {',
  5465. ' vJ = v;',
  5466. ' }',
  5467. ' });',
  5468. ' $mod.DoIt(new $mod.TRecord(vI.get()), vI.get(), vI);',
  5469. ' $mod.DoIt(new $mod.TRecord(vJ), vJ, {',
  5470. ' get: function () {',
  5471. ' return vJ;',
  5472. ' },',
  5473. ' set: function (v) {',
  5474. ' vJ = v;',
  5475. ' }',
  5476. ' });',
  5477. '};',
  5478. 'this.i = new $mod.TRecord();'
  5479. ]),
  5480. LinesToStr([
  5481. '$mod.DoIt(new $mod.TRecord($mod.i),$mod.i,{',
  5482. ' p: $mod,',
  5483. ' get: function () {',
  5484. ' return this.p.i;',
  5485. ' },',
  5486. ' set: function (v) {',
  5487. ' this.p.i = v;',
  5488. ' }',
  5489. '});'
  5490. ]));
  5491. end;
  5492. procedure TTestModule.TestRecordElement_AsParams;
  5493. begin
  5494. StartProgram(false);
  5495. Add('type');
  5496. Add(' integer = longint;');
  5497. Add(' TRecord = record');
  5498. Add(' i: integer;');
  5499. Add(' end;');
  5500. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  5501. Add('var vJ: TRecord;');
  5502. Add('begin');
  5503. Add(' doit(vj.i,vj.i,vj.i);');
  5504. Add('end;');
  5505. Add('var r: TRecord;');
  5506. Add('begin');
  5507. Add(' doit(r.i,r.i,r.i);');
  5508. ConvertProgram;
  5509. CheckSource('TestRecordElement_AsParams',
  5510. LinesToStr([ // statements
  5511. 'this.TRecord = function (s) {',
  5512. ' if (s) {',
  5513. ' this.i = s.i;',
  5514. ' } else {',
  5515. ' this.i = 0;',
  5516. ' };',
  5517. ' this.$equal = function (b) {',
  5518. ' return this.i == b.i;',
  5519. ' };',
  5520. '};',
  5521. 'this.DoIt = function (vG,vH,vI) {',
  5522. ' var vJ = new $mod.TRecord();',
  5523. ' $mod.DoIt(vJ.i, vJ.i, {',
  5524. ' p: vJ,',
  5525. ' get: function () {',
  5526. ' return this.p.i;',
  5527. ' },',
  5528. ' set: function (v) {',
  5529. ' this.p.i = v;',
  5530. ' }',
  5531. ' });',
  5532. '};',
  5533. 'this.r = new $mod.TRecord();'
  5534. ]),
  5535. LinesToStr([
  5536. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  5537. ' p: $mod.r,',
  5538. ' get: function () {',
  5539. ' return this.p.i;',
  5540. ' },',
  5541. ' set: function (v) {',
  5542. ' this.p.i = v;',
  5543. ' }',
  5544. '});'
  5545. ]));
  5546. end;
  5547. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  5548. begin
  5549. StartProgram(false);
  5550. Add('type');
  5551. Add(' integer = longint;');
  5552. Add(' TRecord = record');
  5553. Add(' i: integer;');
  5554. Add(' end;');
  5555. Add('function GetRec(vB: integer = 0): TRecord;');
  5556. Add('begin');
  5557. Add('end;');
  5558. Add('procedure DoIt(vG: integer; const vH: integer);');
  5559. Add('begin');
  5560. Add('end;');
  5561. Add('begin');
  5562. Add(' doit(getrec.i,getrec.i);');
  5563. Add(' doit(getrec().i,getrec().i);');
  5564. Add(' doit(getrec(1).i,getrec(2).i);');
  5565. ConvertProgram;
  5566. CheckSource('TestRecordElementFromFuncResult_AsParams',
  5567. LinesToStr([ // statements
  5568. 'this.TRecord = function (s) {',
  5569. ' if (s) {',
  5570. ' this.i = s.i;',
  5571. ' } else {',
  5572. ' this.i = 0;',
  5573. ' };',
  5574. ' this.$equal = function (b) {',
  5575. ' return this.i == b.i;',
  5576. ' };',
  5577. '};',
  5578. 'this.GetRec = function (vB) {',
  5579. ' var Result = new $mod.TRecord();',
  5580. ' return Result;',
  5581. '};',
  5582. 'this.DoIt = function (vG,vH) {',
  5583. '};'
  5584. ]),
  5585. LinesToStr([
  5586. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  5587. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  5588. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  5589. '']));
  5590. end;
  5591. procedure TTestModule.TestRecordElementFromWith_AsParams;
  5592. begin
  5593. StartProgram(false);
  5594. Add('type');
  5595. Add(' integer = longint;');
  5596. Add(' TRecord = record');
  5597. Add(' i: integer;');
  5598. Add(' end;');
  5599. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  5600. Add('begin');
  5601. Add('end;');
  5602. Add('var r: trecord;');
  5603. Add('begin');
  5604. Add(' with r do ');
  5605. Add(' doit(i,i,i);');
  5606. ConvertProgram;
  5607. CheckSource('TestRecordElementFromWith_AsParams',
  5608. LinesToStr([ // statements
  5609. 'this.TRecord = function (s) {',
  5610. ' if (s) {',
  5611. ' this.i = s.i;',
  5612. ' } else {',
  5613. ' this.i = 0;',
  5614. ' };',
  5615. ' this.$equal = function (b) {',
  5616. ' return this.i == b.i;',
  5617. ' };',
  5618. '};',
  5619. 'this.DoIt = function (vG,vH,vI) {',
  5620. '};',
  5621. 'this.r = new $mod.TRecord();'
  5622. ]),
  5623. LinesToStr([
  5624. 'var $with1 = $mod.r;',
  5625. '$mod.DoIt($with1.i,$with1.i,{',
  5626. ' p: $with1,',
  5627. ' get: function () {',
  5628. ' return this.p.i;',
  5629. ' },',
  5630. ' set: function (v) {',
  5631. ' this.p.i = v;',
  5632. ' }',
  5633. '});',
  5634. '']));
  5635. end;
  5636. procedure TTestModule.TestRecord_Equal;
  5637. begin
  5638. StartProgram(false);
  5639. Add('type');
  5640. Add(' integer = longint;');
  5641. Add(' TFlag = (red,blue);');
  5642. Add(' TFlags = set of TFlag;');
  5643. Add(' TProc = procedure;');
  5644. Add(' TRecord = record');
  5645. Add(' i: integer;');
  5646. Add(' Event: TProc;');
  5647. Add(' f: TFlags;');
  5648. Add(' end;');
  5649. Add(' TNested = record');
  5650. Add(' r: TRecord;');
  5651. Add(' end;');
  5652. Add('var');
  5653. Add(' b: boolean;');
  5654. Add(' r,s: trecord;');
  5655. Add('begin');
  5656. Add(' b:=r=s;');
  5657. Add(' b:=r<>s;');
  5658. ConvertProgram;
  5659. CheckSource('TestRecord_Equal',
  5660. LinesToStr([ // statements
  5661. 'this.TFlag = {',
  5662. ' "0": "red",',
  5663. ' red: 0,',
  5664. ' "1": "blue",',
  5665. ' blue: 1',
  5666. '};',
  5667. 'this.TRecord = function (s) {',
  5668. ' if (s) {',
  5669. ' this.i = s.i;',
  5670. ' this.Event = s.Event;',
  5671. ' this.f = rtl.refSet(s.f);',
  5672. ' } else {',
  5673. ' this.i = 0;',
  5674. ' this.Event = null;',
  5675. ' this.f = {};',
  5676. ' };',
  5677. ' this.$equal = function (b) {',
  5678. ' return (this.i == b.i) && (rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f));',
  5679. ' };',
  5680. '};',
  5681. 'this.TNested = function (s) {',
  5682. ' if (s) {',
  5683. ' this.r = new $mod.TRecord(s.r);',
  5684. ' } else {',
  5685. ' this.r = new $mod.TRecord();',
  5686. ' };',
  5687. ' this.$equal = function (b) {',
  5688. ' return this.r.$equal(b.r);',
  5689. ' };',
  5690. '};',
  5691. 'this.b = false;',
  5692. 'this.r = new $mod.TRecord();',
  5693. 'this.s = new $mod.TRecord();'
  5694. ]),
  5695. LinesToStr([
  5696. '$mod.b = $mod.r.$equal($mod.s);',
  5697. '$mod.b = !$mod.r.$equal($mod.s);',
  5698. '']));
  5699. end;
  5700. procedure TTestModule.TestRecord_TypeCastJSValueToRecord;
  5701. begin
  5702. StartProgram(false);
  5703. Add('type');
  5704. Add(' TRecord = record');
  5705. Add(' i: longint;');
  5706. Add(' end;');
  5707. Add('var');
  5708. Add(' Jv: jsvalue;');
  5709. Add(' Rec: trecord;');
  5710. Add('begin');
  5711. Add(' rec:=trecord(jv);');
  5712. ConvertProgram;
  5713. CheckSource('TestRecord_TypeCastJSValueToRecord',
  5714. LinesToStr([ // statements
  5715. 'this.TRecord = function (s) {',
  5716. ' if (s) {',
  5717. ' this.i = s.i;',
  5718. ' } else {',
  5719. ' this.i = 0;',
  5720. ' };',
  5721. ' this.$equal = function (b) {',
  5722. ' return this.i == b.i;',
  5723. ' };',
  5724. '};',
  5725. 'this.Jv = undefined;',
  5726. 'this.Rec = new $mod.TRecord();'
  5727. ]),
  5728. LinesToStr([
  5729. '$mod.Rec = new $mod.TRecord(rtl.getObject($mod.Jv));',
  5730. '']));
  5731. end;
  5732. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  5733. begin
  5734. StartProgram(false);
  5735. Add('type');
  5736. Add(' TObject = class');
  5737. Add(' public');
  5738. Add(' constructor Create;');
  5739. Add(' destructor Destroy;');
  5740. Add(' end;');
  5741. Add('constructor tobject.create;');
  5742. Add('begin end;');
  5743. Add('destructor tobject.destroy;');
  5744. Add('begin end;');
  5745. Add('var Obj: tobject;');
  5746. Add('begin');
  5747. Add(' obj:=tobject.create;');
  5748. Add(' obj.destroy;');
  5749. ConvertProgram;
  5750. CheckSource('TestClass_TObjectDefaultConstructor',
  5751. LinesToStr([ // statements
  5752. 'rtl.createClass($mod,"TObject",null,function(){',
  5753. ' this.$init = function () {',
  5754. ' };',
  5755. ' this.$final = function () {',
  5756. ' };',
  5757. ' this.Create = function(){',
  5758. ' };',
  5759. ' this.Destroy = function(){',
  5760. ' };',
  5761. '});',
  5762. 'this.Obj = null;'
  5763. ]),
  5764. LinesToStr([ // $mod.$main
  5765. '$mod.Obj = $mod.TObject.$create("Create");',
  5766. '$mod.Obj.$destroy("Destroy");',
  5767. '']));
  5768. end;
  5769. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  5770. begin
  5771. StartProgram(false);
  5772. Add('type');
  5773. Add(' TObject = class');
  5774. Add(' public');
  5775. Add(' constructor Create(Par: longint);');
  5776. Add(' end;');
  5777. Add('constructor tobject.create(par: longint);');
  5778. Add('begin end;');
  5779. Add('var Obj: tobject;');
  5780. Add('begin');
  5781. Add(' obj:=tobject.create(3);');
  5782. ConvertProgram;
  5783. CheckSource('TestClass_TObjectConstructorWithParams',
  5784. LinesToStr([ // statements
  5785. 'rtl.createClass($mod,"TObject",null,function(){',
  5786. ' this.$init = function () {',
  5787. ' };',
  5788. ' this.$final = function () {',
  5789. ' };',
  5790. ' this.Create = function(Par){',
  5791. ' };',
  5792. '});',
  5793. 'this.Obj = null;'
  5794. ]),
  5795. LinesToStr([ // $mod.$main
  5796. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  5797. ]));
  5798. end;
  5799. procedure TTestModule.TestClass_Var;
  5800. begin
  5801. StartProgram(false);
  5802. Add('type');
  5803. Add(' TObject = class');
  5804. Add(' public');
  5805. Add(' vI: longint;');
  5806. Add(' constructor Create(Par: longint);');
  5807. Add(' end;');
  5808. Add('constructor tobject.create(par: longint);');
  5809. Add('begin');
  5810. Add(' vi:=par+3');
  5811. Add('end;');
  5812. Add('var Obj: tobject;');
  5813. Add('begin');
  5814. Add(' obj:=tobject.create(4);');
  5815. Add(' obj.vi:=obj.VI+5;');
  5816. ConvertProgram;
  5817. CheckSource('TestClass_Var',
  5818. LinesToStr([ // statements
  5819. 'rtl.createClass($mod,"TObject",null,function(){',
  5820. ' this.$init = function () {',
  5821. ' this.vI = 0;',
  5822. ' };',
  5823. ' this.$final = function () {',
  5824. ' };',
  5825. ' this.Create = function(Par){',
  5826. ' this.vI = Par+3;',
  5827. ' };',
  5828. '});',
  5829. 'this.Obj = null;'
  5830. ]),
  5831. LinesToStr([ // $mod.$main
  5832. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  5833. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  5834. ]));
  5835. end;
  5836. procedure TTestModule.TestClass_Method;
  5837. begin
  5838. StartProgram(false);
  5839. Add('type');
  5840. Add(' TObject = class');
  5841. Add(' public');
  5842. Add(' vI: longint;');
  5843. Add(' Sub: TObject;');
  5844. Add(' constructor Create;');
  5845. Add(' function GetIt(Par: longint): tobject;');
  5846. Add(' end;');
  5847. Add('constructor tobject.create; begin end;');
  5848. Add('function tobject.getit(par: longint): tobject;');
  5849. Add('begin');
  5850. Add(' Self.vi:=par+3;');
  5851. Add(' Result:=self.sub;');
  5852. Add('end;');
  5853. Add('var Obj: tobject;');
  5854. Add('begin');
  5855. Add(' obj:=tobject.create;');
  5856. Add(' obj.getit(4);');
  5857. Add(' obj.sub.sub:=nil;');
  5858. Add(' obj.sub.getit(5);');
  5859. Add(' obj.sub.getit(6).SUB:=nil;');
  5860. Add(' obj.sub.getit(7).GETIT(8);');
  5861. Add(' obj.sub.getit(9).SuB.getit(10);');
  5862. ConvertProgram;
  5863. CheckSource('TestClass_Method',
  5864. LinesToStr([ // statements
  5865. 'rtl.createClass($mod,"TObject",null,function(){',
  5866. ' this.$init = function () {',
  5867. ' this.vI = 0;',
  5868. ' this.Sub = null;',
  5869. ' };',
  5870. ' this.$final = function () {',
  5871. ' this.Sub = undefined;',
  5872. ' };',
  5873. ' this.Create = function(){',
  5874. ' };',
  5875. ' this.GetIt = function(Par){',
  5876. ' var Result = null;',
  5877. ' this.vI = Par + 3;',
  5878. ' Result = this.Sub;',
  5879. ' return Result;',
  5880. ' };',
  5881. '});',
  5882. 'this.Obj = null;'
  5883. ]),
  5884. LinesToStr([ // $mod.$main
  5885. '$mod.Obj = $mod.TObject.$create("Create");',
  5886. '$mod.Obj.GetIt(4);',
  5887. '$mod.Obj.Sub.Sub=null;',
  5888. '$mod.Obj.Sub.GetIt(5);',
  5889. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  5890. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  5891. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  5892. ]));
  5893. end;
  5894. procedure TTestModule.TestClass_Implementation;
  5895. begin
  5896. StartUnit(false);
  5897. Add([
  5898. 'interface',
  5899. 'type',
  5900. ' TObject = class',
  5901. ' constructor Create;',
  5902. ' end;',
  5903. 'implementation',
  5904. 'type',
  5905. ' TIntClass = class',
  5906. ' constructor Create; reintroduce;',
  5907. ' class procedure DoGlob;',
  5908. ' end;',
  5909. 'constructor tintclass.create;',
  5910. 'begin',
  5911. ' inherited;',
  5912. ' inherited create;',
  5913. ' doglob;',
  5914. 'end;',
  5915. 'class procedure tintclass.doglob;',
  5916. 'begin',
  5917. 'end;',
  5918. 'constructor tobject.create;',
  5919. 'var',
  5920. ' iC: tintclass;',
  5921. 'begin',
  5922. ' ic:=tintclass.create;',
  5923. ' tintclass.doglob;',
  5924. ' ic.doglob;',
  5925. 'end;',
  5926. 'initialization',
  5927. ' tintclass.doglob;',
  5928. '']);
  5929. ConvertUnit;
  5930. CheckSource('TestClass_Implementation',
  5931. LinesToStr([ // statements
  5932. 'var $impl = $mod.$impl;',
  5933. 'rtl.createClass($mod, "TObject", null, function () {',
  5934. ' this.$init = function () {',
  5935. ' };',
  5936. ' this.$final = function () {',
  5937. ' };',
  5938. ' this.Create = function () {',
  5939. ' var iC = null;',
  5940. ' iC = $impl.TIntClass.$create("Create$1");',
  5941. ' $impl.TIntClass.DoGlob();',
  5942. ' iC.$class.DoGlob();',
  5943. ' };',
  5944. '});',
  5945. '']),
  5946. LinesToStr([ // $mod.$main
  5947. '$impl.TIntClass.DoGlob();',
  5948. '']),
  5949. LinesToStr([
  5950. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  5951. ' this.Create$1 = function () {',
  5952. ' $mod.TObject.Create.apply(this, arguments);',
  5953. ' $mod.TObject.Create.call(this);',
  5954. ' this.$class.DoGlob();',
  5955. ' };',
  5956. ' this.DoGlob = function () {',
  5957. ' };',
  5958. '});',
  5959. '']));
  5960. end;
  5961. procedure TTestModule.TestClass_Inheritance;
  5962. begin
  5963. StartProgram(false);
  5964. Add('type');
  5965. Add(' TObject = class');
  5966. Add(' public');
  5967. Add(' constructor Create;');
  5968. Add(' end;');
  5969. Add(' TClassA = class');
  5970. Add(' end;');
  5971. Add(' TClassB = class(TObject)');
  5972. Add(' procedure ProcB;');
  5973. Add(' end;');
  5974. Add('constructor tobject.create; begin end;');
  5975. Add('procedure tclassb.procb; begin end;');
  5976. Add('var');
  5977. Add(' oO: TObject;');
  5978. Add(' oA: TClassA;');
  5979. Add(' oB: TClassB;');
  5980. Add('begin');
  5981. Add(' oO:=tobject.Create;');
  5982. Add(' oA:=tclassa.Create;');
  5983. Add(' ob:=tclassb.Create;');
  5984. Add(' if oo is tclassa then ;');
  5985. Add(' ob:=oo as tclassb;');
  5986. Add(' (oo as tclassb).procb;');
  5987. ConvertProgram;
  5988. CheckSource('TestClass_Inheritance',
  5989. LinesToStr([ // statements
  5990. 'rtl.createClass($mod,"TObject",null,function(){',
  5991. ' this.$init = function () {',
  5992. ' };',
  5993. ' this.$final = function () {',
  5994. ' };',
  5995. ' this.Create = function () {',
  5996. ' };',
  5997. '});',
  5998. 'rtl.createClass($mod,"TClassA",$mod.TObject,function(){',
  5999. '});',
  6000. 'rtl.createClass($mod,"TClassB",$mod.TObject,function(){',
  6001. ' this.ProcB = function () {',
  6002. ' };',
  6003. '});',
  6004. 'this.oO = null;',
  6005. 'this.oA = null;',
  6006. 'this.oB = null;'
  6007. ]),
  6008. LinesToStr([ // $mod.$main
  6009. '$mod.oO = $mod.TObject.$create("Create");',
  6010. '$mod.oA = $mod.TClassA.$create("Create");',
  6011. '$mod.oB = $mod.TClassB.$create("Create");',
  6012. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  6013. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  6014. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  6015. ]));
  6016. end;
  6017. procedure TTestModule.TestClass_AbstractMethod;
  6018. begin
  6019. StartProgram(false);
  6020. Add('type');
  6021. Add(' TObject = class');
  6022. Add(' public');
  6023. Add(' procedure DoIt; virtual; abstract;');
  6024. Add(' end;');
  6025. Add('begin');
  6026. ConvertProgram;
  6027. CheckSource('TestClass_AbstractMethod',
  6028. LinesToStr([ // statements
  6029. 'rtl.createClass($mod,"TObject",null,function(){',
  6030. ' this.$init = function () {',
  6031. ' };',
  6032. ' this.$final = function () {',
  6033. ' };',
  6034. '});'
  6035. ]),
  6036. LinesToStr([ // this.$main
  6037. ''
  6038. ]));
  6039. end;
  6040. procedure TTestModule.TestClass_CallInherited_NoParams;
  6041. begin
  6042. StartProgram(false);
  6043. Add('type');
  6044. Add(' TObject = class');
  6045. Add(' procedure DoAbstract; virtual; abstract;');
  6046. Add(' procedure DoVirtual; virtual;');
  6047. Add(' procedure DoIt;');
  6048. Add(' end;');
  6049. Add(' TA = class');
  6050. Add(' procedure doabstract; override;');
  6051. Add(' procedure dovirtual; override;');
  6052. Add(' procedure DoSome;');
  6053. Add(' end;');
  6054. Add('procedure tobject.dovirtual;');
  6055. Add('begin');
  6056. Add(' inherited; // call non existing ancestor -> ignore silently');
  6057. Add('end;');
  6058. Add('procedure tobject.doit;');
  6059. Add('begin');
  6060. Add('end;');
  6061. Add('procedure ta.doabstract;');
  6062. Add('begin');
  6063. Add(' inherited dovirtual; // call TObject.DoVirtual');
  6064. Add('end;');
  6065. Add('procedure ta.dovirtual;');
  6066. Add('begin');
  6067. Add(' inherited; // call TObject.DoVirtual');
  6068. Add(' inherited dovirtual; // call TObject.DoVirtual');
  6069. Add(' inherited dovirtual(); // call TObject.DoVirtual');
  6070. Add(' doit;');
  6071. Add(' doit();');
  6072. Add('end;');
  6073. Add('procedure ta.dosome;');
  6074. Add('begin');
  6075. Add(' inherited; // call non existing ancestor method -> silently ignore');
  6076. Add('end;');
  6077. Add('begin');
  6078. ConvertProgram;
  6079. CheckSource('TestClass_CallInherited_NoParams',
  6080. LinesToStr([ // statements
  6081. 'rtl.createClass($mod,"TObject",null,function(){',
  6082. ' this.$init = function () {',
  6083. ' };',
  6084. ' this.$final = function () {',
  6085. ' };',
  6086. ' this.DoVirtual = function () {',
  6087. ' };',
  6088. ' this.DoIt = function () {',
  6089. ' };',
  6090. '});',
  6091. 'rtl.createClass($mod, "TA", $mod.TObject, function () {',
  6092. ' this.DoAbstract = function () {',
  6093. ' $mod.TObject.DoVirtual.call(this);',
  6094. ' };',
  6095. ' this.DoVirtual = function () {',
  6096. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  6097. ' $mod.TObject.DoVirtual.call(this);',
  6098. ' $mod.TObject.DoVirtual.call(this);',
  6099. ' this.DoIt();',
  6100. ' this.DoIt();',
  6101. ' };',
  6102. ' this.DoSome = function () {',
  6103. ' };',
  6104. '});'
  6105. ]),
  6106. LinesToStr([ // this.$main
  6107. ''
  6108. ]));
  6109. end;
  6110. procedure TTestModule.TestClass_CallInherited_WithParams;
  6111. begin
  6112. StartProgram(false);
  6113. Add('type');
  6114. Add(' TObject = class');
  6115. Add(' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;');
  6116. Add(' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;');
  6117. Add(' procedure DoIt(pA: longint; pB: longint = 0);');
  6118. Add(' procedure DoIt2(pA: longint = 1; pB: longint = 2);');
  6119. Add(' end;');
  6120. Add(' TClassA = class');
  6121. Add(' procedure DoAbstract(pA: longint; pB: longint = 0); override;');
  6122. Add(' procedure DoVirtual(pA: longint; pB: longint = 0); override;');
  6123. Add(' end;');
  6124. Add('procedure tobject.dovirtual(pa: longint; pb: longint = 0);');
  6125. Add('begin');
  6126. Add('end;');
  6127. Add('procedure tobject.doit(pa: longint; pb: longint = 0);');
  6128. Add('begin');
  6129. Add('end;');
  6130. Add('procedure tobject.doit2(pa: longint; pb: longint = 0);');
  6131. Add('begin');
  6132. Add('end;');
  6133. Add('procedure tclassa.doabstract(pa: longint; pb: longint = 0);');
  6134. Add('begin');
  6135. Add(' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)');
  6136. Add(' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)');
  6137. Add('end;');
  6138. Add('procedure tclassa.dovirtual(pa: longint; pb: longint = 0);');
  6139. Add('begin');
  6140. Add(' inherited; // call TObject.DoVirtual(pA,pB)');
  6141. Add(' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)');
  6142. Add(' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)');
  6143. Add(' doit(pa,pb);');
  6144. Add(' doit(pa);');
  6145. Add(' doit2(pa);');
  6146. Add(' doit2;');
  6147. Add('end;');
  6148. Add('begin');
  6149. ConvertProgram;
  6150. CheckSource('TestClass_CallInherited_WithParams',
  6151. LinesToStr([ // statements
  6152. 'rtl.createClass($mod,"TObject",null,function(){',
  6153. ' this.$init = function () {',
  6154. ' };',
  6155. ' this.$final = function () {',
  6156. ' };',
  6157. ' this.DoVirtual = function (pA,pB) {',
  6158. ' };',
  6159. ' this.DoIt = function (pA,pB) {',
  6160. ' };',
  6161. ' this.DoIt2 = function (pA,pB) {',
  6162. ' };',
  6163. '});',
  6164. 'rtl.createClass($mod, "TClassA", $mod.TObject, function () {',
  6165. ' this.DoAbstract = function (pA,pB) {',
  6166. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  6167. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  6168. ' };',
  6169. ' this.DoVirtual = function (pA,pB) {',
  6170. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  6171. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  6172. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  6173. ' this.DoIt(pA,pB);',
  6174. ' this.DoIt(pA,0);',
  6175. ' this.DoIt2(pA,2);',
  6176. ' this.DoIt2(1,2);',
  6177. ' };',
  6178. '});'
  6179. ]),
  6180. LinesToStr([ // this.$main
  6181. ''
  6182. ]));
  6183. end;
  6184. procedure TTestModule.TestClasS_CallInheritedConstructor;
  6185. begin
  6186. StartProgram(false);
  6187. Add('type');
  6188. Add(' TObject = class');
  6189. Add(' constructor Create; virtual;');
  6190. Add(' constructor CreateWithB(b: boolean);');
  6191. Add(' end;');
  6192. Add(' TA = class');
  6193. Add(' constructor Create; override;');
  6194. Add(' constructor CreateWithC(c: char);');
  6195. Add(' procedure DoIt;');
  6196. Add(' class function DoSome: TObject;');
  6197. Add(' end;');
  6198. Add('constructor tobject.create;');
  6199. Add('begin');
  6200. Add(' inherited; // call non existing ancestor -> ignore silently');
  6201. Add('end;');
  6202. Add('constructor tobject.createwithb(b: boolean);');
  6203. Add('begin');
  6204. Add(' inherited; // call non existing ancestor -> ignore silently');
  6205. Add(' create; // normal call');
  6206. Add('end;');
  6207. Add('constructor ta.create;');
  6208. Add('begin');
  6209. Add(' inherited; // normal call TObject.Create');
  6210. Add(' inherited create; // normal call TObject.Create');
  6211. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  6212. Add('end;');
  6213. Add('constructor ta.createwithc(c: char);');
  6214. Add('begin');
  6215. Add(' inherited create; // call TObject.Create');
  6216. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  6217. Add(' doit;');
  6218. Add(' doit();');
  6219. Add(' dosome;');
  6220. Add('end;');
  6221. Add('procedure ta.doit;');
  6222. Add('begin');
  6223. Add(' create; // normal call');
  6224. Add(' createwithb(false); // normal call');
  6225. Add(' createwithc(''c''); // normal call');
  6226. Add('end;');
  6227. Add('class function ta.dosome: TObject;');
  6228. Add('begin');
  6229. Add(' Result:=create; // constructor');
  6230. Add(' Result:=createwithb(true); // constructor');
  6231. Add(' Result:=createwithc(''c''); // constructor');
  6232. Add('end;');
  6233. Add('begin');
  6234. ConvertProgram;
  6235. CheckSource('TestClass_CallInheritedConstructor',
  6236. LinesToStr([ // statements
  6237. 'rtl.createClass($mod,"TObject",null,function(){',
  6238. ' this.$init = function () {',
  6239. ' };',
  6240. ' this.$final = function () {',
  6241. ' };',
  6242. ' this.Create = function () {',
  6243. ' };',
  6244. ' this.CreateWithB = function (b) {',
  6245. ' this.Create();',
  6246. ' };',
  6247. '});',
  6248. 'rtl.createClass($mod, "TA", $mod.TObject, function () {',
  6249. ' this.Create = function () {',
  6250. ' $mod.TObject.Create.apply(this, arguments);',
  6251. ' $mod.TObject.Create.call(this);',
  6252. ' $mod.TObject.CreateWithB.call(this, false);',
  6253. ' };',
  6254. ' this.CreateWithC = function (c) {',
  6255. ' $mod.TObject.Create.call(this);',
  6256. ' $mod.TObject.CreateWithB.call(this, true);',
  6257. ' this.DoIt();',
  6258. ' this.DoIt();',
  6259. ' this.$class.DoSome();',
  6260. ' };',
  6261. ' this.DoIt = function () {',
  6262. ' this.Create();',
  6263. ' this.CreateWithB(false);',
  6264. ' this.CreateWithC("c");',
  6265. ' };',
  6266. ' this.DoSome = function () {',
  6267. ' var Result = null;',
  6268. ' Result = this.$create("Create");',
  6269. ' Result = this.$create("CreateWithB", [true]);',
  6270. ' Result = this.$create("CreateWithC", ["c"]);',
  6271. ' return Result;',
  6272. ' };',
  6273. '});'
  6274. ]),
  6275. LinesToStr([ // this.$main
  6276. ''
  6277. ]));
  6278. end;
  6279. procedure TTestModule.TestClass_ClassVar;
  6280. begin
  6281. StartProgram(false);
  6282. Add('type');
  6283. Add(' TObject = class');
  6284. Add(' public');
  6285. Add(' class var vI: longint;');
  6286. Add(' class var Sub: TObject;');
  6287. Add(' constructor Create;');
  6288. Add(' class function GetIt(Par: longint): tobject;');
  6289. Add(' end;');
  6290. Add('constructor tobject.create;');
  6291. Add('begin');
  6292. Add(' vi:=vi+1;');
  6293. Add(' Self.vi:=Self.vi+1;');
  6294. Add('end;');
  6295. Add('class function tobject.getit(par: longint): tobject;');
  6296. Add('begin');
  6297. Add(' vi:=vi+par;');
  6298. Add(' Self.vi:=Self.vi+par;');
  6299. Add(' Result:=self.sub;');
  6300. Add('end;');
  6301. Add('var Obj: tobject;');
  6302. Add('begin');
  6303. Add(' obj:=tobject.create;');
  6304. Add(' tobject.vi:=3;');
  6305. Add(' if tobject.vi=4 then ;');
  6306. Add(' tobject.sub:=nil;');
  6307. Add(' obj.sub:=nil;');
  6308. Add(' obj.sub.sub:=nil;');
  6309. ConvertProgram;
  6310. CheckSource('TestClass_ClassVar',
  6311. LinesToStr([ // statements
  6312. 'rtl.createClass($mod,"TObject",null,function(){',
  6313. ' this.vI = 0;',
  6314. ' this.Sub = null;',
  6315. ' this.$init = function () {',
  6316. ' };',
  6317. ' this.$final = function () {',
  6318. ' };',
  6319. ' this.Create = function(){',
  6320. ' this.$class.vI = this.vI+1;',
  6321. ' this.$class.vI = this.vI+1;',
  6322. ' };',
  6323. ' this.GetIt = function(Par){',
  6324. ' var Result = null;',
  6325. ' this.vI = this.vI + Par;',
  6326. ' this.vI = this.vI + Par;',
  6327. ' Result = this.Sub;',
  6328. ' return Result;',
  6329. ' };',
  6330. '});',
  6331. 'this.Obj = null;'
  6332. ]),
  6333. LinesToStr([ // $mod.$main
  6334. '$mod.Obj = $mod.TObject.$create("Create");',
  6335. '$mod.TObject.vI = 3;',
  6336. 'if ($mod.TObject.vI == 4);',
  6337. '$mod.TObject.Sub=null;',
  6338. '$mod.Obj.$class.Sub=null;',
  6339. '$mod.Obj.Sub.$class.Sub=null;',
  6340. '']));
  6341. end;
  6342. procedure TTestModule.TestClass_CallClassMethod;
  6343. begin
  6344. StartProgram(false);
  6345. Add('type');
  6346. Add(' TObject = class');
  6347. Add(' public');
  6348. Add(' class var vI: longint;');
  6349. Add(' class var Sub: TObject;');
  6350. Add(' constructor Create;');
  6351. Add(' function GetMore(Par: longint): longint;');
  6352. Add(' class function GetIt(Par: longint): tobject;');
  6353. Add(' end;');
  6354. Add('constructor tobject.create;');
  6355. Add('begin');
  6356. Add(' sub:=getit(3);');
  6357. Add(' vi:=getmore(4);');
  6358. Add(' sub:=Self.getit(5);');
  6359. Add(' vi:=Self.getmore(6);');
  6360. Add('end;');
  6361. Add('function tobject.getmore(par: longint): longint;');
  6362. Add('begin');
  6363. Add(' sub:=getit(11);');
  6364. Add(' vi:=getmore(12);');
  6365. Add(' sub:=self.getit(13);');
  6366. Add(' vi:=self.getmore(14);');
  6367. Add('end;');
  6368. Add('class function tobject.getit(par: longint): tobject;');
  6369. Add('begin');
  6370. Add(' sub:=getit(21);');
  6371. Add(' vi:=sub.getmore(22);');
  6372. Add(' sub:=self.getit(23);');
  6373. Add(' vi:=self.sub.getmore(24);');
  6374. Add('end;');
  6375. Add('var Obj: tobject;');
  6376. Add('begin');
  6377. Add(' obj:=tobject.create;');
  6378. Add(' tobject.getit(5);');
  6379. Add(' obj.getit(6);');
  6380. Add(' obj.sub.getit(7);');
  6381. Add(' obj.sub.getit(8).SUB:=nil;');
  6382. Add(' obj.sub.getit(9).GETIT(10);');
  6383. Add(' obj.sub.getit(11).SuB.getit(12);');
  6384. ConvertProgram;
  6385. CheckSource('TestClass_CallClassMethod',
  6386. LinesToStr([ // statements
  6387. 'rtl.createClass($mod,"TObject",null,function(){',
  6388. ' this.vI = 0;',
  6389. ' this.Sub = null;',
  6390. ' this.$init = function () {',
  6391. ' };',
  6392. ' this.$final = function () {',
  6393. ' };',
  6394. ' this.Create = function(){',
  6395. ' this.$class.Sub = this.$class.GetIt(3);',
  6396. ' this.$class.vI = this.GetMore(4);',
  6397. ' this.$class.Sub = this.$class.GetIt(5);',
  6398. ' this.$class.vI = this.GetMore(6);',
  6399. ' };',
  6400. ' this.GetMore = function(Par){',
  6401. ' var Result = 0;',
  6402. ' this.$class.Sub = this.$class.GetIt(11);',
  6403. ' this.$class.vI = this.GetMore(12);',
  6404. ' this.$class.Sub = this.$class.GetIt(13);',
  6405. ' this.$class.vI = this.GetMore(14);',
  6406. ' return Result;',
  6407. ' };',
  6408. ' this.GetIt = function(Par){',
  6409. ' var Result = null;',
  6410. ' this.Sub = this.GetIt(21);',
  6411. ' this.vI = this.Sub.GetMore(22);',
  6412. ' this.Sub = this.GetIt(23);',
  6413. ' this.vI = this.Sub.GetMore(24);',
  6414. ' return Result;',
  6415. ' };',
  6416. '});',
  6417. 'this.Obj = null;'
  6418. ]),
  6419. LinesToStr([ // $mod.$main
  6420. '$mod.Obj = $mod.TObject.$create("Create");',
  6421. '$mod.TObject.GetIt(5);',
  6422. '$mod.Obj.$class.GetIt(6);',
  6423. '$mod.Obj.Sub.$class.GetIt(7);',
  6424. '$mod.Obj.Sub.$class.GetIt(8).$class.Sub=null;',
  6425. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  6426. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  6427. '']));
  6428. end;
  6429. procedure TTestModule.TestClass_Property;
  6430. begin
  6431. StartProgram(false);
  6432. Add('type');
  6433. Add(' TObject = class');
  6434. Add(' Fx: longint;');
  6435. Add(' Fy: longint;');
  6436. Add(' function GetInt: longint;');
  6437. Add(' procedure SetInt(Value: longint);');
  6438. Add(' procedure DoIt;');
  6439. Add(' property IntA: longint read Fx write Fy;');
  6440. Add(' property IntB: longint read GetInt write SetInt;');
  6441. Add(' end;');
  6442. Add('function tobject.getint: longint;');
  6443. Add('begin');
  6444. Add(' result:=fx;');
  6445. Add('end;');
  6446. Add('procedure tobject.setint(value: longint);');
  6447. Add('begin');
  6448. Add(' if value=fy then exit;');
  6449. Add(' fy:=value;');
  6450. Add('end;');
  6451. Add('procedure tobject.doit;');
  6452. Add('begin');
  6453. Add(' IntA:=IntA+1;');
  6454. Add(' Self.IntA:=Self.IntA+1;');
  6455. Add(' IntB:=IntB+1;');
  6456. Add(' Self.IntB:=Self.IntB+1;');
  6457. Add('end;');
  6458. Add('var Obj: tobject;');
  6459. Add('begin');
  6460. Add(' obj.inta:=obj.inta+1;');
  6461. Add(' if obj.intb=2 then;');
  6462. Add(' obj.intb:=obj.intb+2;');
  6463. Add(' obj.setint(obj.inta);');
  6464. ConvertProgram;
  6465. CheckSource('TestClass_Property',
  6466. LinesToStr([ // statements
  6467. 'rtl.createClass($mod, "TObject", null, function () {',
  6468. ' this.$init = function () {',
  6469. ' this.Fx = 0;',
  6470. ' this.Fy = 0;',
  6471. ' };',
  6472. ' this.$final = function () {',
  6473. ' };',
  6474. ' this.GetInt = function () {',
  6475. ' var Result = 0;',
  6476. ' Result = this.Fx;',
  6477. ' return Result;',
  6478. ' };',
  6479. ' this.SetInt = function (Value) {',
  6480. ' if (Value == this.Fy) return;',
  6481. ' this.Fy = Value;',
  6482. ' };',
  6483. ' this.DoIt = function () {',
  6484. ' this.Fy = this.Fx + 1;',
  6485. ' this.Fy = this.Fx + 1;',
  6486. ' this.SetInt(this.GetInt() + 1);',
  6487. ' this.SetInt(this.GetInt() + 1);',
  6488. ' };',
  6489. '});',
  6490. 'this.Obj = null;'
  6491. ]),
  6492. LinesToStr([ // $mod.$main
  6493. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  6494. 'if ($mod.Obj.GetInt() == 2);',
  6495. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  6496. '$mod.Obj.SetInt($mod.Obj.Fx);'
  6497. ]));
  6498. end;
  6499. procedure TTestModule.TestClass_Property_ClassMethod;
  6500. begin
  6501. StartProgram(false);
  6502. Add('type');
  6503. Add(' TObject = class');
  6504. Add(' class var Fx: longint;');
  6505. Add(' class var Fy: longint;');
  6506. Add(' class function GetInt: longint;');
  6507. Add(' class procedure SetInt(Value: longint);');
  6508. Add(' class procedure DoIt;');
  6509. Add(' class property IntA: longint read Fx write Fy;');
  6510. Add(' class property IntB: longint read GetInt write SetInt;');
  6511. Add(' end;');
  6512. Add('class function tobject.getint: longint;');
  6513. Add('begin');
  6514. Add(' result:=fx;');
  6515. Add('end;');
  6516. Add('class procedure tobject.setint(value: longint);');
  6517. Add('begin');
  6518. Add('end;');
  6519. Add('class procedure tobject.doit;');
  6520. Add('begin');
  6521. Add(' IntA:=IntA+1;');
  6522. Add(' Self.IntA:=Self.IntA+1;');
  6523. Add(' IntB:=IntB+1;');
  6524. Add(' Self.IntB:=Self.IntB+1;');
  6525. Add('end;');
  6526. Add('var Obj: tobject;');
  6527. Add('begin');
  6528. Add(' tobject.inta:=tobject.inta+1;');
  6529. Add(' if tobject.intb=2 then;');
  6530. Add(' tobject.intb:=tobject.intb+2;');
  6531. Add(' tobject.setint(tobject.inta);');
  6532. Add(' obj.inta:=obj.inta+1;');
  6533. Add(' if obj.intb=2 then;');
  6534. Add(' obj.intb:=obj.intb+2;');
  6535. Add(' obj.setint(obj.inta);');
  6536. ConvertProgram;
  6537. CheckSource('TestClass_Property_ClassMethod',
  6538. LinesToStr([ // statements
  6539. 'rtl.createClass($mod, "TObject", null, function () {',
  6540. ' this.Fx = 0;',
  6541. ' this.Fy = 0;',
  6542. ' this.$init = function () {',
  6543. ' };',
  6544. ' this.$final = function () {',
  6545. ' };',
  6546. ' this.GetInt = function () {',
  6547. ' var Result = 0;',
  6548. ' Result = this.Fx;',
  6549. ' return Result;',
  6550. ' };',
  6551. ' this.SetInt = function (Value) {',
  6552. ' };',
  6553. ' this.DoIt = function () {',
  6554. ' this.Fy = this.Fx + 1;',
  6555. ' this.Fy = this.Fx + 1;',
  6556. ' this.SetInt(this.GetInt() + 1);',
  6557. ' this.SetInt(this.GetInt() + 1);',
  6558. ' };',
  6559. '});',
  6560. 'this.Obj = null;'
  6561. ]),
  6562. LinesToStr([ // $mod.$main
  6563. '$mod.TObject.Fy = $mod.TObject.Fx + 1;',
  6564. 'if ($mod.TObject.GetInt() == 2);',
  6565. '$mod.TObject.SetInt($mod.TObject.GetInt() + 2);',
  6566. '$mod.TObject.SetInt($mod.TObject.Fx);',
  6567. '$mod.Obj.$class.Fy = $mod.Obj.Fx + 1;',
  6568. 'if ($mod.Obj.$class.GetInt() == 2);',
  6569. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  6570. '$mod.Obj.$class.SetInt($mod.Obj.Fx);'
  6571. ]));
  6572. end;
  6573. procedure TTestModule.TestClass_Property_Index;
  6574. begin
  6575. StartProgram(false);
  6576. Add('type');
  6577. Add(' TObject = class');
  6578. Add(' FItems: array of longint;');
  6579. Add(' function GetItems(Index: longint): longint;');
  6580. Add(' procedure SetItems(Index: longint; Value: longint);');
  6581. Add(' procedure DoIt;');
  6582. Add(' property Items[Index: longint]: longint read getitems write setitems;');
  6583. Add(' end;');
  6584. Add('function tobject.getitems(index: longint): longint;');
  6585. Add('begin');
  6586. Add(' Result:=fitems[index];');
  6587. Add('end;');
  6588. Add('procedure tobject.setitems(index: longint; value: longint);');
  6589. Add('begin');
  6590. Add(' fitems[index]:=value;');
  6591. Add('end;');
  6592. Add('procedure tobject.doit;');
  6593. Add('begin');
  6594. Add(' items[1]:=2;');
  6595. Add(' items[3]:=items[4];');
  6596. Add(' self.items[5]:=self.items[6];');
  6597. Add(' items[items[7]]:=items[items[8]];');
  6598. Add('end;');
  6599. Add('var Obj: tobject;');
  6600. Add('begin');
  6601. Add(' obj.Items[11]:=obj.Items[12];');
  6602. ConvertProgram;
  6603. CheckSource('TestClass_Property_Index',
  6604. LinesToStr([ // statements
  6605. 'rtl.createClass($mod, "TObject", null, function () {',
  6606. ' this.$init = function () {',
  6607. ' this.FItems = [];',
  6608. ' };',
  6609. ' this.$final = function () {',
  6610. ' this.FItems = undefined;',
  6611. ' };',
  6612. ' this.GetItems = function (Index) {',
  6613. ' var Result = 0;',
  6614. ' Result = this.FItems[Index];',
  6615. ' return Result;',
  6616. ' };',
  6617. ' this.SetItems = function (Index, Value) {',
  6618. ' this.FItems[Index] = Value;',
  6619. ' };',
  6620. ' this.DoIt = function () {',
  6621. ' this.SetItems(1, 2);',
  6622. ' this.SetItems(3,this.GetItems(4));',
  6623. ' this.SetItems(5,this.GetItems(6));',
  6624. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  6625. ' };',
  6626. '});',
  6627. 'this.Obj = null;'
  6628. ]),
  6629. LinesToStr([ // $mod.$main
  6630. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  6631. ]));
  6632. end;
  6633. procedure TTestModule.TestClass_PropertyOfTypeArray;
  6634. begin
  6635. StartProgram(false);
  6636. Add('type');
  6637. Add(' TArray = array of longint;');
  6638. Add(' TObject = class');
  6639. Add(' FItems: TArray;');
  6640. Add(' function GetItems: tarray;');
  6641. Add(' procedure SetItems(Value: tarray);');
  6642. Add(' property Items: tarray read getitems write setitems;');
  6643. Add(' end;');
  6644. Add('function tobject.getitems: tarray;');
  6645. Add('begin');
  6646. Add(' Result:=fitems;');
  6647. Add('end;');
  6648. Add('procedure tobject.setitems(value: tarray);');
  6649. Add('begin');
  6650. Add(' fitems:=value;');
  6651. Add(' fitems:=nil;');
  6652. Add(' Items:=nil;');
  6653. Add(' Items:=Items;');
  6654. Add(' Items[1]:=2;');
  6655. Add(' fitems[3]:=Items[4];');
  6656. Add(' Items[5]:=Items[6];');
  6657. Add(' Self.Items[7]:=8;');
  6658. Add(' Self.Items[9]:=Self.Items[10];');
  6659. Add(' Items[Items[11]]:=Items[Items[12]];');
  6660. Add('end;');
  6661. Add('var Obj: tobject;');
  6662. Add('begin');
  6663. Add(' obj.items:=nil;');
  6664. Add(' obj.items:=obj.items;');
  6665. Add(' obj.items[11]:=obj.items[12];');
  6666. ConvertProgram;
  6667. CheckSource('TestClass_PropertyOfTypeArray',
  6668. LinesToStr([ // statements
  6669. 'rtl.createClass($mod, "TObject", null, function () {',
  6670. ' this.$init = function () {',
  6671. ' this.FItems = [];',
  6672. ' };',
  6673. ' this.$final = function () {',
  6674. ' this.FItems = undefined;',
  6675. ' };',
  6676. ' this.GetItems = function () {',
  6677. ' var Result = [];',
  6678. ' Result = this.FItems;',
  6679. ' return Result;',
  6680. ' };',
  6681. ' this.SetItems = function (Value) {',
  6682. ' this.FItems = Value;',
  6683. ' this.FItems = [];',
  6684. ' this.SetItems([]);',
  6685. ' this.SetItems(this.GetItems());',
  6686. ' this.GetItems()[1] = 2;',
  6687. ' this.FItems[3] = this.GetItems()[4];',
  6688. ' this.GetItems()[5] = this.GetItems()[6];',
  6689. ' this.GetItems()[7] = 8;',
  6690. ' this.GetItems()[9] = this.GetItems()[10];',
  6691. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  6692. ' };',
  6693. '});',
  6694. 'this.Obj = null;'
  6695. ]),
  6696. LinesToStr([ // $mod.$main
  6697. '$mod.Obj.SetItems([]);',
  6698. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  6699. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  6700. ]));
  6701. end;
  6702. procedure TTestModule.TestClass_PropertyDefault;
  6703. begin
  6704. StartProgram(false);
  6705. Add('type');
  6706. Add(' TArray = array of longint;');
  6707. Add(' TObject = class');
  6708. Add(' FItems: TArray;');
  6709. Add(' function GetItems(Index: longint): longint;');
  6710. Add(' procedure SetItems(Index, Value: longint);');
  6711. Add(' property Items[Index: longint]: longint read getitems write setitems; default;');
  6712. Add(' end;');
  6713. Add('function tobject.getitems(index: longint): longint;');
  6714. Add('begin');
  6715. Add('end;');
  6716. Add('procedure tobject.setitems(index, value: longint);');
  6717. Add('begin');
  6718. Add(' Self[1]:=2;');
  6719. Add(' Self[3]:=Self[index];');
  6720. Add(' Self[index]:=Self[Self[value]];');
  6721. Add(' Self[Self[4]]:=value;');
  6722. Add('end;');
  6723. Add('var Obj: tobject;');
  6724. Add('begin');
  6725. Add(' obj[11]:=12;');
  6726. Add(' obj[13]:=obj[14];');
  6727. Add(' obj[obj[15]]:=obj[obj[15]];');
  6728. ConvertProgram;
  6729. CheckSource('TestClass_PropertyDefault',
  6730. LinesToStr([ // statements
  6731. 'rtl.createClass($mod, "TObject", null, function () {',
  6732. ' this.$init = function () {',
  6733. ' this.FItems = [];',
  6734. ' };',
  6735. ' this.$final = function () {',
  6736. ' this.FItems = undefined;',
  6737. ' };',
  6738. ' this.GetItems = function (Index) {',
  6739. ' var Result = 0;',
  6740. ' return Result;',
  6741. ' };',
  6742. ' this.SetItems = function (Index, Value) {',
  6743. ' this.SetItems(1, 2);',
  6744. ' this.SetItems(3, this.GetItems(Index));',
  6745. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  6746. ' this.SetItems(this.GetItems(4), Value);',
  6747. ' };',
  6748. '});',
  6749. 'this.Obj = null;'
  6750. ]),
  6751. LinesToStr([ // $mod.$main
  6752. '$mod.Obj.SetItems(11, 12);',
  6753. '$mod.Obj.SetItems(13, $mod.Obj.GetItems(14));',
  6754. '$mod.Obj.SetItems($mod.Obj.GetItems(15), $mod.Obj.GetItems($mod.Obj.GetItems(15)));'
  6755. ]));
  6756. end;
  6757. procedure TTestModule.TestClass_PropertyOverride;
  6758. begin
  6759. StartProgram(false);
  6760. Add('type');
  6761. Add(' integer = longint;');
  6762. Add(' TObject = class');
  6763. Add(' FItem: integer;');
  6764. Add(' function GetItem: integer; external name ''GetItem'';');
  6765. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  6766. Add(' property Item: integer read getitem write setitem;');
  6767. Add(' end;');
  6768. Add(' TCar = class');
  6769. Add(' FBag: integer;');
  6770. Add(' function GetBag: integer; external name ''GetBag'';');
  6771. Add(' property Item read getbag;');
  6772. Add(' end;');
  6773. Add('var');
  6774. Add(' Obj: tobject;');
  6775. Add(' Car: tcar;');
  6776. Add('begin');
  6777. Add(' Obj.Item:=Obj.Item;');
  6778. Add(' Car.Item:=Car.Item;');
  6779. ConvertProgram;
  6780. CheckSource('TestClass_PropertyOverride',
  6781. LinesToStr([ // statements
  6782. 'rtl.createClass($mod, "TObject", null, function () {',
  6783. ' this.$init = function () {',
  6784. ' this.FItem = 0;',
  6785. ' };',
  6786. ' this.$final = function () {',
  6787. ' };',
  6788. '});',
  6789. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  6790. ' this.$init = function () {',
  6791. ' $mod.TObject.$init.call(this);',
  6792. ' this.FBag = 0;',
  6793. ' };',
  6794. '});',
  6795. 'this.Obj = null;',
  6796. 'this.Car = null;',
  6797. '']),
  6798. LinesToStr([ // $mod.$main
  6799. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  6800. '$mod.Car.SetItem($mod.Car.GetBag());',
  6801. '']));
  6802. end;
  6803. procedure TTestModule.TestClass_Assigned;
  6804. begin
  6805. StartProgram(false);
  6806. Add('type');
  6807. Add(' TObject = class');
  6808. Add(' end;');
  6809. Add('var');
  6810. Add(' Obj: tobject;');
  6811. Add(' b: boolean;');
  6812. Add('begin');
  6813. Add(' if Assigned(obj) then ;');
  6814. Add(' b:=Assigned(obj) or false;');
  6815. ConvertProgram;
  6816. CheckSource('TestClass_Assigned',
  6817. LinesToStr([ // statements
  6818. 'rtl.createClass($mod, "TObject", null, function () {',
  6819. ' this.$init = function () {',
  6820. ' };',
  6821. ' this.$final = function () {',
  6822. ' };',
  6823. '});',
  6824. 'this.Obj = null;',
  6825. 'this.b = false;'
  6826. ]),
  6827. LinesToStr([ // $mod.$main
  6828. 'if ($mod.Obj != null);',
  6829. '$mod.b = ($mod.Obj != null) || false;'
  6830. ]));
  6831. end;
  6832. procedure TTestModule.TestClass_WithClassDoCreate;
  6833. begin
  6834. StartProgram(false);
  6835. Add('type');
  6836. Add(' TObject = class');
  6837. Add(' aBool: boolean;');
  6838. Add(' Arr: array of boolean;');
  6839. Add(' constructor Create;');
  6840. Add(' end;');
  6841. Add('constructor TObject.Create; begin end;');
  6842. Add('var');
  6843. Add(' Obj: tobject;');
  6844. Add(' b: boolean;');
  6845. Add('begin');
  6846. Add(' with tobject.create do begin');
  6847. Add(' b:=abool;');
  6848. Add(' abool:=b;');
  6849. Add(' b:=arr[1];');
  6850. Add(' arr[2]:=b;');
  6851. Add(' end;');
  6852. Add(' with tobject do');
  6853. Add(' obj:=create;');
  6854. Add(' with obj do begin');
  6855. Add(' create;');
  6856. Add(' b:=abool;');
  6857. Add(' abool:=b;');
  6858. Add(' b:=arr[3];');
  6859. Add(' arr[4]:=b;');
  6860. Add(' end;');
  6861. ConvertProgram;
  6862. CheckSource('TestClass_WithClassDoCreate',
  6863. LinesToStr([ // statements
  6864. 'rtl.createClass($mod, "TObject", null, function () {',
  6865. ' this.$init = function () {',
  6866. ' this.aBool = false;',
  6867. ' this.Arr = [];',
  6868. ' };',
  6869. ' this.$final = function () {',
  6870. ' this.Arr = undefined;',
  6871. ' };',
  6872. ' this.Create = function () {',
  6873. ' };',
  6874. '});',
  6875. 'this.Obj = null;',
  6876. 'this.b = false;'
  6877. ]),
  6878. LinesToStr([ // $mod.$main
  6879. 'var $with1 = $mod.TObject.$create("Create");',
  6880. '$mod.b = $with1.aBool;',
  6881. '$with1.aBool = $mod.b;',
  6882. '$mod.b = $with1.Arr[1];',
  6883. '$with1.Arr[2] = $mod.b;',
  6884. 'var $with2 = $mod.TObject;',
  6885. '$mod.Obj = $with2.$create("Create");',
  6886. 'var $with3 = $mod.Obj;',
  6887. '$with3.Create();',
  6888. '$mod.b = $with3.aBool;',
  6889. '$with3.aBool = $mod.b;',
  6890. '$mod.b = $with3.Arr[3];',
  6891. '$with3.Arr[4] = $mod.b;',
  6892. '']));
  6893. end;
  6894. procedure TTestModule.TestClass_WithClassInstDoProperty;
  6895. begin
  6896. StartProgram(false);
  6897. Add('type');
  6898. Add(' TObject = class');
  6899. Add(' FInt: longint;');
  6900. Add(' constructor Create;');
  6901. Add(' function GetSize: longint;');
  6902. Add(' procedure SetSize(Value: longint);');
  6903. Add(' property Int: longint read FInt write FInt;');
  6904. Add(' property Size: longint read GetSize write SetSize;');
  6905. Add(' end;');
  6906. Add('constructor TObject.Create; begin end;');
  6907. Add('function TObject.GetSize: longint; begin; end;');
  6908. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  6909. Add('var');
  6910. Add(' Obj: tobject;');
  6911. Add(' i: longint;');
  6912. Add('begin');
  6913. Add(' with TObject.Create do begin');
  6914. Add(' i:=int;');
  6915. Add(' int:=i;');
  6916. Add(' i:=size;');
  6917. Add(' size:=i;');
  6918. Add(' end;');
  6919. Add(' with obj do begin');
  6920. Add(' i:=int;');
  6921. Add(' int:=i;');
  6922. Add(' i:=size;');
  6923. Add(' size:=i;');
  6924. Add(' end;');
  6925. ConvertProgram;
  6926. CheckSource('TestClass_WithClassInstDoProperty',
  6927. LinesToStr([ // statements
  6928. 'rtl.createClass($mod, "TObject", null, function () {',
  6929. ' this.$init = function () {',
  6930. ' this.FInt = 0;',
  6931. ' };',
  6932. ' this.$final = function () {',
  6933. ' };',
  6934. ' this.Create = function () {',
  6935. ' };',
  6936. ' this.GetSize = function () {',
  6937. ' var Result = 0;',
  6938. ' return Result;',
  6939. ' };',
  6940. ' this.SetSize = function (Value) {',
  6941. ' };',
  6942. '});',
  6943. 'this.Obj = null;',
  6944. 'this.i = 0;'
  6945. ]),
  6946. LinesToStr([ // $mod.$main
  6947. 'var $with1 = $mod.TObject.$create("Create");',
  6948. '$mod.i = $with1.FInt;',
  6949. '$with1.FInt = $mod.i;',
  6950. '$mod.i = $with1.GetSize();',
  6951. '$with1.SetSize($mod.i);',
  6952. 'var $with2 = $mod.Obj;',
  6953. '$mod.i = $with2.FInt;',
  6954. '$with2.FInt = $mod.i;',
  6955. '$mod.i = $with2.GetSize();',
  6956. '$with2.SetSize($mod.i);',
  6957. '']));
  6958. end;
  6959. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  6960. begin
  6961. StartProgram(false);
  6962. Add('type');
  6963. Add(' TObject = class');
  6964. Add(' constructor Create;');
  6965. Add(' function GetItems(Index: longint): longint;');
  6966. Add(' procedure SetItems(Index, Value: longint);');
  6967. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  6968. Add(' end;');
  6969. Add('constructor TObject.Create; begin end;');
  6970. Add('function tobject.getitems(index: longint): longint; begin; end;');
  6971. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  6972. Add('var');
  6973. Add(' Obj: tobject;');
  6974. Add(' i: longint;');
  6975. Add('begin');
  6976. Add(' with TObject.Create do begin');
  6977. Add(' i:=Items[1];');
  6978. Add(' Items[2]:=i;');
  6979. Add(' end;');
  6980. Add(' with obj do begin');
  6981. Add(' i:=Items[3];');
  6982. Add(' Items[4]:=i;');
  6983. Add(' end;');
  6984. ConvertProgram;
  6985. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  6986. LinesToStr([ // statements
  6987. 'rtl.createClass($mod, "TObject", null, function () {',
  6988. ' this.$init = function () {',
  6989. ' };',
  6990. ' this.$final = function () {',
  6991. ' };',
  6992. ' this.Create = function () {',
  6993. ' };',
  6994. ' this.GetItems = function (Index) {',
  6995. ' var Result = 0;',
  6996. ' return Result;',
  6997. ' };',
  6998. ' this.SetItems = function (Index, Value) {',
  6999. ' };',
  7000. '});',
  7001. 'this.Obj = null;',
  7002. 'this.i = 0;'
  7003. ]),
  7004. LinesToStr([ // $mod.$main
  7005. 'var $with1 = $mod.TObject.$create("Create");',
  7006. '$mod.i = $with1.GetItems(1);',
  7007. '$with1.SetItems(2, $mod.i);',
  7008. 'var $with2 = $mod.Obj;',
  7009. '$mod.i = $with2.GetItems(3);',
  7010. '$with2.SetItems(4, $mod.i);',
  7011. '']));
  7012. end;
  7013. procedure TTestModule.TestClass_WithClassInstDoFunc;
  7014. begin
  7015. StartProgram(false);
  7016. Add('type');
  7017. Add(' TObject = class');
  7018. Add(' constructor Create;');
  7019. Add(' function GetSize: longint;');
  7020. Add(' procedure SetSize(Value: longint);');
  7021. Add(' end;');
  7022. Add('constructor TObject.Create; begin end;');
  7023. Add('function TObject.GetSize: longint; begin; end;');
  7024. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  7025. Add('var');
  7026. Add(' Obj: tobject;');
  7027. Add(' i: longint;');
  7028. Add('begin');
  7029. Add(' with TObject.Create do begin');
  7030. Add(' i:=GetSize;');
  7031. Add(' i:=GetSize();');
  7032. Add(' SetSize(i);');
  7033. Add(' end;');
  7034. Add(' with obj do begin');
  7035. Add(' i:=GetSize;');
  7036. Add(' i:=GetSize();');
  7037. Add(' SetSize(i);');
  7038. Add(' end;');
  7039. ConvertProgram;
  7040. CheckSource('TestClass_WithClassInstDoFunc',
  7041. LinesToStr([ // statements
  7042. 'rtl.createClass($mod, "TObject", null, function () {',
  7043. ' this.$init = function () {',
  7044. ' };',
  7045. ' this.$final = function () {',
  7046. ' };',
  7047. ' this.Create = function () {',
  7048. ' };',
  7049. ' this.GetSize = function () {',
  7050. ' var Result = 0;',
  7051. ' return Result;',
  7052. ' };',
  7053. ' this.SetSize = function (Value) {',
  7054. ' };',
  7055. '});',
  7056. 'this.Obj = null;',
  7057. 'this.i = 0;'
  7058. ]),
  7059. LinesToStr([ // $mod.$main
  7060. 'var $with1 = $mod.TObject.$create("Create");',
  7061. '$mod.i = $with1.GetSize();',
  7062. '$mod.i = $with1.GetSize();',
  7063. '$with1.SetSize($mod.i);',
  7064. 'var $with2 = $mod.Obj;',
  7065. '$mod.i = $with2.GetSize();',
  7066. '$mod.i = $with2.GetSize();',
  7067. '$with2.SetSize($mod.i);',
  7068. '']));
  7069. end;
  7070. procedure TTestModule.TestClass_TypeCast;
  7071. begin
  7072. StartProgram(false);
  7073. Add('type');
  7074. Add(' TObject = class');
  7075. Add(' Next: TObject;');
  7076. Add(' constructor Create;');
  7077. Add(' end;');
  7078. Add(' TControl = class(TObject)');
  7079. Add(' Arr: array of TObject;');
  7080. Add(' function GetIt(vI: longint = 0): TObject;');
  7081. Add(' end;');
  7082. Add('constructor tobject.create; begin end;');
  7083. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  7084. Add('var');
  7085. Add(' Obj: tobject;');
  7086. Add('begin');
  7087. Add(' obj:=tcontrol(obj).next;');
  7088. Add(' tcontrol(obj):=nil;');
  7089. Add(' obj:=tcontrol(obj);');
  7090. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  7091. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  7092. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  7093. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  7094. ConvertProgram;
  7095. CheckSource('TestClass_TypeCast',
  7096. LinesToStr([ // statements
  7097. 'rtl.createClass($mod, "TObject", null, function () {',
  7098. ' this.$init = function () {',
  7099. ' this.Next = null;',
  7100. ' };',
  7101. ' this.$final = function () {',
  7102. ' this.Next = undefined;',
  7103. ' };',
  7104. ' this.Create = function () {',
  7105. ' };',
  7106. '});',
  7107. 'rtl.createClass($mod, "TControl", $mod.TObject, function () {',
  7108. ' this.$init = function () {',
  7109. ' $mod.TObject.$init.call(this);',
  7110. ' this.Arr = [];',
  7111. ' };',
  7112. ' this.$final = function () {',
  7113. ' this.Arr = undefined;',
  7114. ' $mod.TObject.$final.call(this);',
  7115. ' };',
  7116. ' this.GetIt = function (vI) {',
  7117. ' var Result = null;',
  7118. ' return Result;',
  7119. ' };',
  7120. '});',
  7121. 'this.Obj = null;'
  7122. ]),
  7123. LinesToStr([ // $mod.$main
  7124. '$mod.Obj = $mod.Obj.Next;',
  7125. '$mod.Obj = null;',
  7126. '$mod.Obj = $mod.Obj;',
  7127. '$mod.Obj = $mod.Obj.GetIt(0);',
  7128. '$mod.Obj = $mod.Obj.GetIt(0);',
  7129. '$mod.Obj = $mod.Obj.GetIt(1);',
  7130. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  7131. '']));
  7132. end;
  7133. procedure TTestModule.TestClass_TypeCastUntypedParam;
  7134. begin
  7135. StartProgram(false);
  7136. Add('type');
  7137. Add(' TObject = class end;');
  7138. Add('procedure ProcA(var A);');
  7139. Add('begin');
  7140. Add(' TObject(A):=nil;');
  7141. Add(' TObject(A):=TObject(A);');
  7142. Add(' if TObject(A)=nil then ;');
  7143. Add(' if nil=TObject(A) then ;');
  7144. Add('end;');
  7145. Add('procedure ProcB(out A);');
  7146. Add('begin');
  7147. Add(' TObject(A):=nil;');
  7148. Add(' TObject(A):=TObject(A);');
  7149. Add(' if TObject(A)=nil then ;');
  7150. Add(' if nil=TObject(A) then ;');
  7151. Add('end;');
  7152. Add('procedure ProcC(const A);');
  7153. Add('begin');
  7154. Add(' if TObject(A)=nil then ;');
  7155. Add(' if nil=TObject(A) then ;');
  7156. Add('end;');
  7157. Add('var o: TObject;');
  7158. Add('begin');
  7159. Add(' ProcA(o);');
  7160. Add(' ProcB(o);');
  7161. Add(' ProcC(o);');
  7162. ConvertProgram;
  7163. CheckSource('TestClass_TypeCastUntypedParam',
  7164. LinesToStr([ // statements
  7165. 'rtl.createClass($mod, "TObject", null, function () {',
  7166. ' this.$init = function () {',
  7167. ' };',
  7168. ' this.$final = function () {',
  7169. ' };',
  7170. '});',
  7171. 'this.ProcA = function (A) {',
  7172. ' A.set(null);',
  7173. ' A.set(A.get());',
  7174. ' if (A.get() == null);',
  7175. ' if (null == A.get());',
  7176. '};',
  7177. 'this.ProcB = function (A) {',
  7178. ' A.set(null);',
  7179. ' A.set(A.get());',
  7180. ' if (A.get() == null);',
  7181. ' if (null == A.get());',
  7182. '};',
  7183. 'this.ProcC = function (A) {',
  7184. ' if (A == null);',
  7185. ' if (null == A);',
  7186. '};',
  7187. 'this.o = null;',
  7188. '']),
  7189. LinesToStr([ // $mod.$main
  7190. '$mod.ProcA({',
  7191. ' p: $mod,',
  7192. ' get: function () {',
  7193. ' return this.p.o;',
  7194. ' },',
  7195. ' set: function (v) {',
  7196. ' this.p.o = v;',
  7197. ' }',
  7198. '});',
  7199. '$mod.ProcB({',
  7200. ' p: $mod,',
  7201. ' get: function () {',
  7202. ' return this.p.o;',
  7203. ' },',
  7204. ' set: function (v) {',
  7205. ' this.p.o = v;',
  7206. ' }',
  7207. '});',
  7208. '$mod.ProcC($mod.o);',
  7209. '']));
  7210. end;
  7211. procedure TTestModule.TestClass_Overloads;
  7212. begin
  7213. StartProgram(false);
  7214. Add('type');
  7215. Add(' TObject = class');
  7216. Add(' procedure DoIt;');
  7217. Add(' procedure DoIt(vI: longint);');
  7218. Add(' end;');
  7219. Add('procedure TObject.DoIt;');
  7220. Add('begin');
  7221. Add(' DoIt;');
  7222. Add(' DoIt(1);');
  7223. Add('end;');
  7224. Add('procedure TObject.DoIt(vI: longint); begin end;');
  7225. Add('begin');
  7226. ConvertProgram;
  7227. CheckSource('TestClass_Overloads',
  7228. LinesToStr([ // statements
  7229. 'rtl.createClass($mod, "TObject", null, function () {',
  7230. ' this.$init = function () {',
  7231. ' };',
  7232. ' this.$final = function () {',
  7233. ' };',
  7234. ' this.DoIt = function () {',
  7235. ' this.DoIt();',
  7236. ' this.DoIt$1(1);',
  7237. ' };',
  7238. ' this.DoIt$1 = function (vI) {',
  7239. ' };',
  7240. '});',
  7241. '']),
  7242. LinesToStr([ // $mod.$main
  7243. '']));
  7244. end;
  7245. procedure TTestModule.TestClass_OverloadsAncestor;
  7246. begin
  7247. StartProgram(false);
  7248. Add('type');
  7249. Add(' TObject = class;');
  7250. Add(' TObject = class');
  7251. Add(' procedure DoIt(vA: longint);');
  7252. Add(' procedure DoIt(vA, vB: longint);');
  7253. Add(' end;');
  7254. Add(' TCar = class;');
  7255. Add(' TCar = class');
  7256. Add(' procedure DoIt(vA: longint);');
  7257. Add(' procedure DoIt(vA, vB: longint);');
  7258. Add(' end;');
  7259. Add('procedure tobject.doit(va: longint);');
  7260. Add('begin');
  7261. Add(' doit(1);');
  7262. Add(' doit(1,2);');
  7263. Add('end;');
  7264. Add('procedure tobject.doit(va, vb: longint); begin end;');
  7265. Add('procedure tcar.doit(va: longint);');
  7266. Add('begin');
  7267. Add(' doit(1);');
  7268. Add(' doit(1,2);');
  7269. Add(' inherited doit(1);');
  7270. Add(' inherited doit(1,2);');
  7271. Add('end;');
  7272. Add('procedure tcar.doit(va, vb: longint); begin end;');
  7273. Add('begin');
  7274. ConvertProgram;
  7275. CheckSource('TestClass_OverloadsAncestor',
  7276. LinesToStr([ // statements
  7277. 'rtl.createClass($mod, "TObject", null, function () {',
  7278. ' this.$init = function () {',
  7279. ' };',
  7280. ' this.$final = function () {',
  7281. ' };',
  7282. ' this.DoIt = function (vA) {',
  7283. ' this.DoIt(1);',
  7284. ' this.DoIt$1(1,2);',
  7285. ' };',
  7286. ' this.DoIt$1 = function (vA, vB) {',
  7287. ' };',
  7288. '});',
  7289. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  7290. ' this.DoIt$2 = function (vA) {',
  7291. ' this.DoIt$2(1);',
  7292. ' this.DoIt$3(1, 2);',
  7293. ' $mod.TObject.DoIt.call(this, 1);',
  7294. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  7295. ' };',
  7296. ' this.DoIt$3 = function (vA, vB) {',
  7297. ' };',
  7298. '});',
  7299. '']),
  7300. LinesToStr([ // $mod.$main
  7301. '']));
  7302. end;
  7303. procedure TTestModule.TestClass_OverloadConstructor;
  7304. begin
  7305. StartProgram(false);
  7306. Add('type');
  7307. Add(' TObject = class');
  7308. Add(' constructor Create(vA: longint);');
  7309. Add(' constructor Create(vA, vB: longint);');
  7310. Add(' end;');
  7311. Add(' TCar = class');
  7312. Add(' constructor Create(vA: longint);');
  7313. Add(' constructor Create(vA, vB: longint);');
  7314. Add(' end;');
  7315. Add('constructor tobject.create(va: longint);');
  7316. Add('begin');
  7317. Add(' create(1);');
  7318. Add(' create(1,2);');
  7319. Add('end;');
  7320. Add('constructor tobject.create(va, vb: longint); begin end;');
  7321. Add('constructor tcar.create(va: longint);');
  7322. Add('begin');
  7323. Add(' create(1);');
  7324. Add(' create(1,2);');
  7325. Add(' inherited create(1);');
  7326. Add(' inherited create(1,2);');
  7327. Add('end;');
  7328. Add('constructor tcar.create(va, vb: longint); begin end;');
  7329. Add('begin');
  7330. Add(' tobject.create(1);');
  7331. Add(' tobject.create(1,2);');
  7332. Add(' tcar.create(1);');
  7333. Add(' tcar.create(1,2);');
  7334. ConvertProgram;
  7335. CheckSource('TestClass_OverloadConstructor',
  7336. LinesToStr([ // statements
  7337. 'rtl.createClass($mod, "TObject", null, function () {',
  7338. ' this.$init = function () {',
  7339. ' };',
  7340. ' this.$final = function () {',
  7341. ' };',
  7342. ' this.Create = function (vA) {',
  7343. ' this.Create(1);',
  7344. ' this.Create$1(1,2);',
  7345. ' };',
  7346. ' this.Create$1 = function (vA, vB) {',
  7347. ' };',
  7348. '});',
  7349. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  7350. ' this.Create$2 = function (vA) {',
  7351. ' this.Create$2(1);',
  7352. ' this.Create$3(1, 2);',
  7353. ' $mod.TObject.Create.call(this, 1);',
  7354. ' $mod.TObject.Create$1.call(this, 1, 2);',
  7355. ' };',
  7356. ' this.Create$3 = function (vA, vB) {',
  7357. ' };',
  7358. '});',
  7359. '']),
  7360. LinesToStr([ // $mod.$main
  7361. '$mod.TObject.$create("Create", [1]);',
  7362. '$mod.TObject.$create("Create$1", [1, 2]);',
  7363. '$mod.TCar.$create("Create$2", [1]);',
  7364. '$mod.TCar.$create("Create$3", [1, 2]);',
  7365. '']));
  7366. end;
  7367. procedure TTestModule.TestClass_ReintroducedVar;
  7368. begin
  7369. StartProgram(false);
  7370. Add('type');
  7371. Add(' TObject = class');
  7372. Add(' strict private');
  7373. Add(' Some: longint;');
  7374. Add(' end;');
  7375. Add(' TMobile = class');
  7376. Add(' strict private');
  7377. Add(' Some: string;');
  7378. Add(' end;');
  7379. Add(' TCar = class(tmobile)');
  7380. Add(' procedure Some;');
  7381. Add(' procedure Some(vA: longint);');
  7382. Add(' end;');
  7383. Add('procedure tcar.some;');
  7384. Add('begin');
  7385. Add(' Some;');
  7386. Add(' Some(1);');
  7387. Add('end;');
  7388. Add('procedure tcar.some(va: longint); begin end;');
  7389. Add('begin');
  7390. ConvertProgram;
  7391. CheckSource('TestClass_ReintroducedVar',
  7392. LinesToStr([ // statements
  7393. 'rtl.createClass($mod, "TObject", null, function () {',
  7394. ' this.$init = function () {',
  7395. ' this.Some = 0;',
  7396. ' };',
  7397. ' this.$final = function () {',
  7398. ' };',
  7399. '});',
  7400. 'rtl.createClass($mod, "TMobile", $mod.TObject, function () {',
  7401. ' this.$init = function () {',
  7402. ' $mod.TObject.$init.call(this);',
  7403. ' this.Some$1 = "";',
  7404. ' };',
  7405. '});',
  7406. 'rtl.createClass($mod, "TCar", $mod.TMobile, function () {',
  7407. ' this.Some$2 = function () {',
  7408. ' this.Some$2();',
  7409. ' this.Some$3(1);',
  7410. ' };',
  7411. ' this.Some$3 = function (vA) {',
  7412. ' };',
  7413. '});',
  7414. '']),
  7415. LinesToStr([ // $mod.$main
  7416. '']));
  7417. end;
  7418. procedure TTestModule.TestClass_RaiseDescendant;
  7419. begin
  7420. StartProgram(false);
  7421. Add('type');
  7422. Add(' TObject = class');
  7423. Add(' constructor Create(Msg: string);');
  7424. Add(' end;');
  7425. Add(' Exception = class');
  7426. Add(' end;');
  7427. Add(' EConvertError = class(Exception)');
  7428. Add(' end;');
  7429. Add('constructor TObject.Create(Msg: string); begin end;');
  7430. Add('begin');
  7431. Add(' raise Exception.Create(''Bar1'');');
  7432. Add(' raise EConvertError.Create(''Bar2'');');
  7433. ConvertProgram;
  7434. CheckSource('TestClass_RaiseDescendant',
  7435. LinesToStr([ // statements
  7436. 'rtl.createClass($mod, "TObject", null, function () {',
  7437. ' this.$init = function () {',
  7438. ' };',
  7439. ' this.$final = function () {',
  7440. ' };',
  7441. ' this.Create = function (Msg) {',
  7442. ' };',
  7443. '});',
  7444. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  7445. '});',
  7446. 'rtl.createClass($mod, "EConvertError", $mod.Exception, function () {',
  7447. '});',
  7448. '']),
  7449. LinesToStr([ // $mod.$main
  7450. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  7451. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  7452. '']));
  7453. end;
  7454. procedure TTestModule.TestClass_ExternalMethod;
  7455. begin
  7456. AddModuleWithIntfImplSrc('unit2.pas',
  7457. LinesToStr([
  7458. 'type',
  7459. ' TObject = class',
  7460. ' public',
  7461. ' procedure Intern; external name ''$DoIntern'';',
  7462. ' end;',
  7463. '']),
  7464. LinesToStr([
  7465. '']));
  7466. StartUnit(true);
  7467. Add('interface');
  7468. Add('uses unit2;');
  7469. Add('type');
  7470. Add(' TCar = class(TObject)');
  7471. Add(' public');
  7472. Add(' procedure Intern2; external name ''$DoIntern2'';');
  7473. Add(' procedure DoIt;');
  7474. Add(' end;');
  7475. Add('implementation');
  7476. Add('procedure tcar.doit;');
  7477. Add('begin');
  7478. Add(' Intern;');
  7479. Add(' Intern();');
  7480. Add(' Intern2;');
  7481. Add(' Intern2();');
  7482. Add('end;');
  7483. Add('var Obj: TCar;');
  7484. Add('begin');
  7485. Add(' obj.intern;');
  7486. Add(' obj.intern();');
  7487. Add(' obj.intern2;');
  7488. Add(' obj.intern2();');
  7489. Add(' obj.doit;');
  7490. Add(' obj.doit();');
  7491. Add(' with obj do begin');
  7492. Add(' Intern;');
  7493. Add(' Intern();');
  7494. Add(' Intern2;');
  7495. Add(' Intern2();');
  7496. Add(' end;');
  7497. ConvertUnit;
  7498. CheckSource('TestClass_ExternalMethod',
  7499. LinesToStr([
  7500. 'var $impl = $mod.$impl;',
  7501. 'rtl.createClass($mod, "TCar", pas.unit2.TObject, function () {',
  7502. ' this.DoIt = function () {',
  7503. ' this.$DoIntern();',
  7504. ' this.$DoIntern();',
  7505. ' this.$DoIntern2();',
  7506. ' this.$DoIntern2();',
  7507. ' };',
  7508. ' });',
  7509. '']),
  7510. LinesToStr([ // this.$init
  7511. '$impl.Obj.$DoIntern();',
  7512. '$impl.Obj.$DoIntern();',
  7513. '$impl.Obj.$DoIntern2();',
  7514. '$impl.Obj.$DoIntern2();',
  7515. '$impl.Obj.DoIt();',
  7516. '$impl.Obj.DoIt();',
  7517. 'var $with1 = $impl.Obj;',
  7518. '$with1.$DoIntern();',
  7519. '$with1.$DoIntern();',
  7520. '$with1.$DoIntern2();',
  7521. '$with1.$DoIntern2();',
  7522. '']),
  7523. LinesToStr([ // implementation
  7524. '$impl.Obj = null;',
  7525. '']) );
  7526. end;
  7527. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  7528. begin
  7529. StartProgram(false);
  7530. Add('type');
  7531. Add(' TObject = class');
  7532. Add(' procedure DoIt; virtual; external name ''Foo'';');
  7533. Add(' end;');
  7534. Add('begin');
  7535. SetExpectedPasResolverError('Virtual method name must match external',
  7536. nVirtualMethodNameMustMatchExternal);
  7537. ConvertProgram;
  7538. end;
  7539. procedure TTestModule.TestClass_ExternalOverrideFail;
  7540. begin
  7541. StartProgram(false);
  7542. Add('type');
  7543. Add(' TObject = class');
  7544. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  7545. Add(' end;');
  7546. Add(' TCar = class');
  7547. Add(' procedure DoIt; override; external name ''DoIt'';');
  7548. Add(' end;');
  7549. Add('begin');
  7550. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  7551. nInvalidXModifierY);
  7552. ConvertProgram;
  7553. end;
  7554. procedure TTestModule.TestClass_ExternalVar;
  7555. begin
  7556. AddModuleWithIntfImplSrc('unit2.pas',
  7557. LinesToStr([
  7558. '{$modeswitch externalclass}',
  7559. 'type',
  7560. ' TObject = class',
  7561. ' public',
  7562. ' Intern: longint external name ''$Intern'';',
  7563. ' end;',
  7564. '']),
  7565. LinesToStr([
  7566. '']));
  7567. StartUnit(true);
  7568. Add('interface');
  7569. Add('uses unit2;');
  7570. Add('{$modeswitch externalclass}');
  7571. Add('type');
  7572. Add(' TCar = class(tobject)');
  7573. Add(' public');
  7574. Add(' Intern2: longint external name ''$Intern2'';');
  7575. Add(' procedure DoIt;');
  7576. Add(' end;');
  7577. Add('implementation');
  7578. Add('procedure tcar.doit;');
  7579. Add('begin');
  7580. Add(' Intern:=Intern+1;');
  7581. Add(' Intern2:=Intern2+2;');
  7582. Add('end;');
  7583. Add('var Obj: TCar;');
  7584. Add('begin');
  7585. Add(' obj.intern:=obj.intern+1;');
  7586. Add(' obj.intern2:=obj.intern2+2;');
  7587. Add(' with obj do begin');
  7588. Add(' intern:=intern+1;');
  7589. Add(' intern2:=intern2+2;');
  7590. Add(' end;');
  7591. ConvertUnit;
  7592. CheckSource('TestClass_ExternalVar',
  7593. LinesToStr([
  7594. 'var $impl = $mod.$impl;',
  7595. 'rtl.createClass($mod, "TCar", pas.unit2.TObject, function () {',
  7596. ' this.DoIt = function () {',
  7597. ' this.$Intern = this.$Intern + 1;',
  7598. ' this.$Intern2 = this.$Intern2 + 2;',
  7599. ' };',
  7600. ' });',
  7601. '']),
  7602. LinesToStr([
  7603. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  7604. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  7605. 'var $with1 = $impl.Obj;',
  7606. '$with1.$Intern = $with1.$Intern + 1;',
  7607. '$with1.$Intern2 = $with1.$Intern2 + 2;',
  7608. '']),
  7609. LinesToStr([ // implementation
  7610. '$impl.Obj = null;',
  7611. '']));
  7612. end;
  7613. procedure TTestModule.TestClass_Const;
  7614. begin
  7615. StartProgram(false);
  7616. Add('type');
  7617. Add(' integer = longint;');
  7618. Add(' TClass = class of TObject;');
  7619. Add(' TObject = class');
  7620. Add(' public');
  7621. Add(' const cI: integer = 3;');
  7622. Add(' procedure DoIt;');
  7623. Add(' class procedure DoMore;');
  7624. Add(' end;');
  7625. Add('implementation');
  7626. Add('procedure tobject.doit;');
  7627. Add('begin');
  7628. Add(' if cI=4 then;');
  7629. Add(' if 5=cI then;');
  7630. Add(' if Self.cI=6 then;');
  7631. Add(' if 7=Self.cI then;');
  7632. Add(' with Self do begin');
  7633. Add(' if cI=11 then;');
  7634. Add(' if 12=cI then;');
  7635. Add(' end;');
  7636. Add('end;');
  7637. Add('class procedure tobject.domore;');
  7638. Add('begin');
  7639. Add(' if cI=8 then;');
  7640. Add(' if Self.cI=9 then;');
  7641. Add(' if 10=cI then;');
  7642. Add(' if 11=Self.cI then;');
  7643. Add(' with Self do begin');
  7644. Add(' if cI=13 then;');
  7645. Add(' if 14=cI then;');
  7646. Add(' end;');
  7647. Add('end;');
  7648. Add('var');
  7649. Add(' Obj: TObject;');
  7650. Add(' Cla: TClass;');
  7651. Add('begin');
  7652. Add(' if TObject.cI=21 then ;');
  7653. Add(' if Obj.cI=22 then ;');
  7654. Add(' if Cla.cI=23 then ;');
  7655. Add(' with obj do if ci=24 then;');
  7656. Add(' with TObject do if ci=25 then;');
  7657. Add(' with Cla do if ci=26 then;');
  7658. ConvertProgram;
  7659. CheckSource('TestClass_Const',
  7660. LinesToStr([
  7661. 'rtl.createClass($mod, "TObject", null, function () {',
  7662. ' this.cI = 3;',
  7663. ' this.$init = function () {',
  7664. ' };',
  7665. ' this.$final = function () {',
  7666. ' };',
  7667. ' this.DoIt = function () {',
  7668. ' if (this.cI == 4) ;',
  7669. ' if (5 == this.cI) ;',
  7670. ' if (this.cI == 6) ;',
  7671. ' if (7 == this.cI) ;',
  7672. ' if (this.cI == 11) ;',
  7673. ' if (12 == this.cI) ;',
  7674. ' };',
  7675. ' this.DoMore = function () {',
  7676. ' if (this.cI == 8) ;',
  7677. ' if (this.cI == 9) ;',
  7678. ' if (10 == this.cI) ;',
  7679. ' if (11 == this.cI) ;',
  7680. ' if (this.cI == 13) ;',
  7681. ' if (14 == this.cI) ;',
  7682. ' };',
  7683. '});',
  7684. 'this.Obj = null;',
  7685. 'this.Cla = null;',
  7686. '']),
  7687. LinesToStr([
  7688. 'if ($mod.TObject.cI == 21) ;',
  7689. 'if ($mod.Obj.cI == 22) ;',
  7690. 'if ($mod.Cla.cI == 23) ;',
  7691. 'var $with1 = $mod.Obj;',
  7692. 'if ($with1.cI == 24) ;',
  7693. 'var $with2 = $mod.TObject;',
  7694. 'if ($with2.cI == 25) ;',
  7695. 'var $with3 = $mod.Cla;',
  7696. 'if ($with3.cI == 26) ;',
  7697. '']));
  7698. end;
  7699. procedure TTestModule.TestClass_LocalVarSelfFail;
  7700. begin
  7701. StartProgram(false);
  7702. Add([
  7703. 'type',
  7704. ' TObject = class',
  7705. ' constructor Create;',
  7706. ' end;',
  7707. 'constructor tobject.create;',
  7708. 'var self: longint;',
  7709. 'begin',
  7710. 'end',
  7711. 'begin',
  7712. '']);
  7713. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  7714. ConvertProgram;
  7715. end;
  7716. procedure TTestModule.TestClass_ArgSelfFail;
  7717. begin
  7718. StartProgram(false);
  7719. Add([
  7720. 'type',
  7721. ' TObject = class',
  7722. ' procedure DoIt(Self: longint);',
  7723. ' end;',
  7724. 'procedure tobject.doit(self: longint);',
  7725. 'begin',
  7726. 'end',
  7727. 'begin',
  7728. '']);
  7729. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,23)',nDuplicateIdentifier);
  7730. ConvertProgram;
  7731. end;
  7732. procedure TTestModule.TestClass_NestedSelf;
  7733. begin
  7734. StartProgram(false);
  7735. Add([
  7736. 'type',
  7737. ' TObject = class',
  7738. ' Key: longint;',
  7739. ' class var State: longint;',
  7740. ' procedure DoIt;',
  7741. ' function GetSize: longint; virtual; abstract;',
  7742. ' procedure SetSize(Value: longint); virtual; abstract;',
  7743. ' property Size: longint read GetSize write SetSize;',
  7744. ' end;',
  7745. 'procedure tobject.doit;',
  7746. ' procedure Sub;',
  7747. ' begin',
  7748. ' key:=key+2;',
  7749. ' self.key:=self.key+3;',
  7750. ' state:=state+4;',
  7751. ' self.state:=self.state+5;',
  7752. ' tobject.state:=tobject.state+6;',
  7753. ' size:=size+7;',
  7754. ' self.size:=self.size+8;',
  7755. ' end;',
  7756. 'begin',
  7757. ' sub;',
  7758. ' key:=key+12;',
  7759. ' self.key:=self.key+13;',
  7760. ' state:=state+14;',
  7761. ' self.state:=self.state+15;',
  7762. ' tobject.state:=tobject.state+16;',
  7763. ' size:=size+17;',
  7764. ' self.size:=self.size+18;',
  7765. 'end;',
  7766. 'begin',
  7767. '']);
  7768. ConvertProgram;
  7769. CheckSource('TestClass_NestedSelf',
  7770. LinesToStr([ // statements
  7771. 'rtl.createClass($mod, "TObject", null, function () {',
  7772. ' this.State = 0;',
  7773. ' this.$init = function () {',
  7774. ' this.Key = 0;',
  7775. ' };',
  7776. ' this.$final = function () {',
  7777. ' };',
  7778. ' this.DoIt = function () {',
  7779. ' var Self = this;',
  7780. ' function Sub() {',
  7781. ' Self.Key = Self.Key + 2;',
  7782. ' Self.Key = Self.Key + 3;',
  7783. ' Self.$class.State = Self.State + 4;',
  7784. ' Self.$class.State = Self.State + 5;',
  7785. ' $mod.TObject.State = $mod.TObject.State + 6;',
  7786. ' Self.SetSize(Self.GetSize() + 7);',
  7787. ' Self.SetSize(Self.GetSize() + 8);',
  7788. ' };',
  7789. ' Sub();',
  7790. ' Self.Key = Self.Key + 12;',
  7791. ' Self.Key = Self.Key + 13;',
  7792. ' Self.$class.State = Self.State + 14;',
  7793. ' Self.$class.State = Self.State + 15;',
  7794. ' $mod.TObject.State = $mod.TObject.State + 16;',
  7795. ' Self.SetSize(Self.GetSize() + 17);',
  7796. ' Self.SetSize(Self.GetSize() + 18);',
  7797. ' };',
  7798. '});',
  7799. '']),
  7800. LinesToStr([ // $mod.$main
  7801. '']));
  7802. end;
  7803. procedure TTestModule.TestClass_NestedClassSelf;
  7804. begin
  7805. StartProgram(false);
  7806. Add([
  7807. 'type',
  7808. ' TObject = class',
  7809. ' class var State: longint;',
  7810. ' class procedure DoIt;',
  7811. ' class function GetSize: longint; virtual; abstract;',
  7812. ' class procedure SetSize(Value: longint); virtual; abstract;',
  7813. ' class property Size: longint read GetSize write SetSize;',
  7814. ' end;',
  7815. 'class procedure tobject.doit;',
  7816. ' procedure Sub;',
  7817. ' begin',
  7818. ' state:=state+2;',
  7819. ' self.state:=self.state+3;',
  7820. ' tobject.state:=tobject.state+4;',
  7821. ' size:=size+5;',
  7822. ' self.size:=self.size+6;',
  7823. ' tobject.size:=tobject.size+7;',
  7824. ' end;',
  7825. 'begin',
  7826. ' sub;',
  7827. ' state:=state+12;',
  7828. ' self.state:=self.state+13;',
  7829. ' tobject.state:=tobject.state+14;',
  7830. ' size:=size+15;',
  7831. ' self.size:=self.size+16;',
  7832. ' tobject.size:=tobject.size+17;',
  7833. 'end;',
  7834. 'begin',
  7835. '']);
  7836. ConvertProgram;
  7837. CheckSource('TestClass_NestedClassSelf',
  7838. LinesToStr([ // statements
  7839. 'rtl.createClass($mod, "TObject", null, function () {',
  7840. ' this.State = 0;',
  7841. ' this.$init = function () {',
  7842. ' };',
  7843. ' this.$final = function () {',
  7844. ' };',
  7845. ' this.DoIt = function () {',
  7846. ' var Self = this;',
  7847. ' function Sub() {',
  7848. ' Self.State = Self.State + 2;',
  7849. ' Self.State = Self.State + 3;',
  7850. ' $mod.TObject.State = $mod.TObject.State + 4;',
  7851. ' Self.SetSize(Self.GetSize() + 5);',
  7852. ' Self.SetSize(Self.GetSize() + 6);',
  7853. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  7854. ' };',
  7855. ' Sub();',
  7856. ' Self.State = Self.State + 12;',
  7857. ' Self.State = Self.State + 13;',
  7858. ' $mod.TObject.State = $mod.TObject.State + 14;',
  7859. ' Self.SetSize(Self.GetSize() + 15);',
  7860. ' Self.SetSize(Self.GetSize() + 16);',
  7861. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  7862. ' };',
  7863. '});',
  7864. '']),
  7865. LinesToStr([ // $mod.$main
  7866. '']));
  7867. end;
  7868. procedure TTestModule.TestClass_NestedCallInherited;
  7869. begin
  7870. StartProgram(false);
  7871. Add([
  7872. 'type',
  7873. ' TObject = class',
  7874. ' function DoIt(k: boolean): longint; virtual;',
  7875. ' end;',
  7876. ' TBird = class',
  7877. ' function DoIt(k: boolean): longint; override;',
  7878. ' end;',
  7879. 'function tobject.doit(k: boolean): longint;',
  7880. 'begin',
  7881. 'end;',
  7882. 'function tbird.doit(k: boolean): longint;',
  7883. ' procedure Sub;',
  7884. ' begin',
  7885. ' inherited DoIt(true);',
  7886. //' if inherited DoIt(false)=4 then ;',
  7887. ' end;',
  7888. 'begin',
  7889. ' Sub;',
  7890. ' inherited;',
  7891. ' inherited DoIt(true);',
  7892. //' if inherited DoIt(false)=14 then ;',
  7893. 'end;',
  7894. 'begin',
  7895. '']);
  7896. ConvertProgram;
  7897. CheckSource('TestClass_NestedCallInherited',
  7898. LinesToStr([ // statements
  7899. 'rtl.createClass($mod, "TObject", null, function () {',
  7900. ' this.$init = function () {',
  7901. ' };',
  7902. ' this.$final = function () {',
  7903. ' };',
  7904. ' this.DoIt = function (k) {',
  7905. ' var Result = 0;',
  7906. ' return Result;',
  7907. ' };',
  7908. '});',
  7909. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  7910. ' this.DoIt = function (k) {',
  7911. ' var Self = this;',
  7912. ' var Result = 0;',
  7913. ' function Sub() {',
  7914. ' $mod.TObject.DoIt.call(Self, true);',
  7915. ' };',
  7916. ' Sub();',
  7917. ' $mod.TObject.DoIt.apply(Self, arguments);',
  7918. ' $mod.TObject.DoIt.call(Self, true);',
  7919. ' return Result;',
  7920. ' };',
  7921. '});',
  7922. '']),
  7923. LinesToStr([ // $mod.$main
  7924. '']));
  7925. end;
  7926. procedure TTestModule.TestClass_TObjectFree;
  7927. begin
  7928. StartProgram(false);
  7929. Add([
  7930. 'type',
  7931. ' TObject = class',
  7932. ' Obj: tobject;',
  7933. ' procedure Free;',
  7934. ' end;',
  7935. 'procedure tobject.free;',
  7936. 'begin',
  7937. 'end;',
  7938. 'function DoIt(o: tobject): tobject;',
  7939. 'var l: tobject;',
  7940. 'begin',
  7941. ' o.free;',
  7942. ' o.free();',
  7943. ' l.free;',
  7944. ' l.free();',
  7945. ' o.obj.free;',
  7946. ' o.obj.free();',
  7947. ' with o do obj.free;',
  7948. ' with o do obj.free();',
  7949. ' result.Free;',
  7950. ' result.Free();',
  7951. 'end;',
  7952. 'var o: tobject;',
  7953. ' a: array of tobject;',
  7954. 'begin',
  7955. ' o.free;',
  7956. ' o.obj.free;',
  7957. ' a[1+2].free;',
  7958. '']);
  7959. ConvertProgram;
  7960. CheckSource('TestClass_TObjectFree',
  7961. LinesToStr([ // statements
  7962. 'rtl.createClass($mod, "TObject", null, function () {',
  7963. ' this.$init = function () {',
  7964. ' this.Obj = null;',
  7965. ' };',
  7966. ' this.$final = function () {',
  7967. ' this.Obj = undefined;',
  7968. ' };',
  7969. ' this.Free = function () {',
  7970. ' };',
  7971. '});',
  7972. 'this.DoIt = function (o) {',
  7973. ' var Result = null;',
  7974. ' var l = null;',
  7975. ' o = rtl.freeLoc(o);',
  7976. ' o = rtl.freeLoc(o);',
  7977. ' l = rtl.freeLoc(l);',
  7978. ' l = rtl.freeLoc(l);',
  7979. ' rtl.free(o, "Obj");',
  7980. ' rtl.free(o, "Obj");',
  7981. ' rtl.free(o, "Obj");',
  7982. ' rtl.free(o, "Obj");',
  7983. ' Result = rtl.freeLoc(Result);',
  7984. ' Result = rtl.freeLoc(Result);',
  7985. ' return Result;',
  7986. '};',
  7987. 'this.o = null;',
  7988. 'this.a = [];',
  7989. '']),
  7990. LinesToStr([ // $mod.$main
  7991. 'rtl.free($mod, "o");',
  7992. 'rtl.free($mod.o, "Obj");',
  7993. 'rtl.free($mod.a, 1 + 2);',
  7994. '']));
  7995. end;
  7996. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  7997. begin
  7998. StartProgram(false);
  7999. Add([
  8000. 'type',
  8001. ' TObject = class',
  8002. ' constructor Create;',
  8003. ' procedure Free;',
  8004. ' end;',
  8005. 'constructor TObject.Create; begin end;',
  8006. 'procedure tobject.free; begin end;',
  8007. 'begin',
  8008. ' with tobject.create do free;',
  8009. '']);
  8010. ConvertProgram;
  8011. CheckSource('TestClass_TObjectFreeNewInstance',
  8012. LinesToStr([ // statements
  8013. 'rtl.createClass($mod, "TObject", null, function () {',
  8014. ' this.$init = function () {',
  8015. ' };',
  8016. ' this.$final = function () {',
  8017. ' };',
  8018. ' this.Create = function () {',
  8019. ' };',
  8020. ' this.Free = function () {',
  8021. ' };',
  8022. '});',
  8023. '']),
  8024. LinesToStr([ // $mod.$main
  8025. 'var $with1 = $mod.TObject.$create("Create");',
  8026. '$with1=rtl.freeLoc($with1);',
  8027. '']));
  8028. end;
  8029. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  8030. begin
  8031. StartProgram(false);
  8032. Add([
  8033. 'type',
  8034. ' TObject = class',
  8035. ' destructor Destroy;',
  8036. ' procedure Free;',
  8037. ' end;',
  8038. 'destructor TObject.Destroy; begin end;',
  8039. 'procedure tobject.free; begin end;',
  8040. 'var o: tobject;',
  8041. 'begin',
  8042. ' o.free;',
  8043. '']);
  8044. Converter.UseLowerCase:=true;
  8045. ConvertProgram;
  8046. CheckSource('TestClass_TObjectFreeLowerCase',
  8047. LinesToStr([ // statements
  8048. 'rtl.createClass($mod, "tobject", null, function () {',
  8049. ' this.$init = function () {',
  8050. ' };',
  8051. ' this.$final = function () {',
  8052. ' };',
  8053. ' rtl.tObjectDestroy = "destroy";',
  8054. ' this.destroy = function () {',
  8055. ' };',
  8056. ' this.free = function () {',
  8057. ' };',
  8058. '});',
  8059. 'this.o = null;',
  8060. '']),
  8061. LinesToStr([ // $mod.$main
  8062. 'rtl.free($mod, "o");',
  8063. '']));
  8064. end;
  8065. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  8066. begin
  8067. StartProgram(false);
  8068. Add([
  8069. 'type',
  8070. ' TObject = class',
  8071. ' procedure Free;',
  8072. ' function GetObj: tobject; virtual; abstract;',
  8073. ' end;',
  8074. 'procedure tobject.free;',
  8075. 'begin',
  8076. 'end;',
  8077. 'var o: tobject;',
  8078. 'begin',
  8079. ' o.getobj.free;',
  8080. '']);
  8081. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  8082. ConvertProgram;
  8083. end;
  8084. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  8085. begin
  8086. StartProgram(false);
  8087. Add([
  8088. 'type',
  8089. ' TObject = class',
  8090. ' procedure Free;',
  8091. ' FObj: TObject;',
  8092. ' property Obj: tobject read FObj write FObj;',
  8093. ' end;',
  8094. 'procedure tobject.free;',
  8095. 'begin',
  8096. 'end;',
  8097. 'var o: tobject;',
  8098. 'begin',
  8099. ' o.obj.free;',
  8100. '']);
  8101. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  8102. ConvertProgram;
  8103. end;
  8104. procedure TTestModule.TestClassOf_Create;
  8105. begin
  8106. StartProgram(false);
  8107. Add('type');
  8108. Add(' TObject = class');
  8109. Add(' constructor Create;');
  8110. Add(' end;');
  8111. Add(' TClass = class of TObject;');
  8112. Add('constructor tobject.create; begin end;');
  8113. Add('var');
  8114. Add(' Obj: tobject;');
  8115. Add(' C: tclass;');
  8116. Add('begin');
  8117. Add(' obj:=C.create;');
  8118. Add(' with c do obj:=create;');
  8119. ConvertProgram;
  8120. CheckSource('TestClassOf_Create',
  8121. LinesToStr([ // statements
  8122. 'rtl.createClass($mod, "TObject", null, function () {',
  8123. ' this.$init = function () {',
  8124. ' };',
  8125. ' this.$final = function () {',
  8126. ' };',
  8127. ' this.Create = function () {',
  8128. ' };',
  8129. '});',
  8130. 'this.Obj = null;',
  8131. 'this.C = null;'
  8132. ]),
  8133. LinesToStr([ // $mod.$main
  8134. '$mod.Obj = $mod.C.$create("Create");',
  8135. 'var $with1 = $mod.C;',
  8136. '$mod.Obj = $with1.$create("Create");',
  8137. '']));
  8138. end;
  8139. procedure TTestModule.TestClassOf_Call;
  8140. begin
  8141. StartProgram(false);
  8142. Add('type');
  8143. Add(' TObject = class');
  8144. Add(' class procedure DoIt;');
  8145. Add(' end;');
  8146. Add(' TClass = class of TObject;');
  8147. Add('class procedure tobject.doit; begin end;');
  8148. Add('var');
  8149. Add(' C: tclass;');
  8150. Add('begin');
  8151. Add(' c.doit;');
  8152. Add(' with c do doit;');
  8153. ConvertProgram;
  8154. CheckSource('TestClassOf_Call',
  8155. LinesToStr([ // statements
  8156. 'rtl.createClass($mod, "TObject", null, function () {',
  8157. ' this.$init = function () {',
  8158. ' };',
  8159. ' this.$final = function () {',
  8160. ' };',
  8161. ' this.DoIt = function () {',
  8162. ' };',
  8163. '});',
  8164. 'this.C = null;'
  8165. ]),
  8166. LinesToStr([ // $mod.$main
  8167. '$mod.C.DoIt();',
  8168. 'var $with1 = $mod.C;',
  8169. '$with1.DoIt();',
  8170. '']));
  8171. end;
  8172. procedure TTestModule.TestClassOf_Assign;
  8173. begin
  8174. StartProgram(false);
  8175. Add('type');
  8176. Add(' TClass = class of TObject;');
  8177. Add(' TObject = class');
  8178. Add(' ClassType: TClass; ');
  8179. Add(' end;');
  8180. Add('var');
  8181. Add(' Obj: tobject;');
  8182. Add(' C: tclass;');
  8183. Add('begin');
  8184. Add(' c:=nil;');
  8185. Add(' c:=obj.classtype;');
  8186. ConvertProgram;
  8187. CheckSource('TestClassOf_Assign',
  8188. LinesToStr([ // statements
  8189. 'rtl.createClass($mod, "TObject", null, function () {',
  8190. ' this.$init = function () {',
  8191. ' this.ClassType = null;',
  8192. ' };',
  8193. ' this.$final = function () {',
  8194. ' this.ClassType = undefined;',
  8195. ' };',
  8196. '});',
  8197. 'this.Obj = null;',
  8198. 'this.C = null;'
  8199. ]),
  8200. LinesToStr([ // $mod.$main
  8201. '$mod.C = null;',
  8202. '$mod.C = $mod.Obj.ClassType;',
  8203. '']));
  8204. end;
  8205. procedure TTestModule.TestClassOf_Is;
  8206. begin
  8207. StartProgram(false);
  8208. Add('type');
  8209. Add(' TClass = class of TObject;');
  8210. Add(' TObject = class');
  8211. Add(' end;');
  8212. Add(' TCar = class');
  8213. Add(' end;');
  8214. Add(' TCars = class of TCar;');
  8215. Add('var');
  8216. Add(' Obj: tobject;');
  8217. Add(' C: tclass;');
  8218. Add(' Cars: tcars;');
  8219. Add('begin');
  8220. Add(' if c is tcar then ;');
  8221. Add(' if c is tcars then ;');
  8222. ConvertProgram;
  8223. CheckSource('TestClassOf_Is',
  8224. LinesToStr([ // statements
  8225. 'rtl.createClass($mod, "TObject", null, function () {',
  8226. ' this.$init = function () {',
  8227. ' };',
  8228. ' this.$final = function () {',
  8229. ' };',
  8230. '});',
  8231. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  8232. '});',
  8233. 'this.Obj = null;',
  8234. 'this.C = null;',
  8235. 'this.Cars = null;'
  8236. ]),
  8237. LinesToStr([ // $mod.$main
  8238. 'if(rtl.is($mod.C,$mod.TCar));',
  8239. 'if(rtl.is($mod.C,$mod.TCar));',
  8240. '']));
  8241. end;
  8242. procedure TTestModule.TestClassOf_Compare;
  8243. begin
  8244. StartProgram(false);
  8245. Add('type');
  8246. Add(' TClass = class of TObject;');
  8247. Add(' TObject = class');
  8248. Add(' ClassType: TClass; ');
  8249. Add(' end;');
  8250. Add('var');
  8251. Add(' b: boolean;');
  8252. Add(' Obj: tobject;');
  8253. Add(' C: tclass;');
  8254. Add('begin');
  8255. Add(' b:=c=nil;');
  8256. Add(' b:=nil=c;');
  8257. Add(' b:=c=obj.classtype;');
  8258. Add(' b:=obj.classtype=c;');
  8259. Add(' b:=c=TObject;');
  8260. Add(' b:=TObject=c;');
  8261. Add(' b:=c<>nil;');
  8262. Add(' b:=nil<>c;');
  8263. Add(' b:=c<>obj.classtype;');
  8264. Add(' b:=obj.classtype<>c;');
  8265. Add(' b:=c<>TObject;');
  8266. Add(' b:=TObject<>c;');
  8267. ConvertProgram;
  8268. CheckSource('TestClassOf_Compare',
  8269. LinesToStr([ // statements
  8270. 'rtl.createClass($mod, "TObject", null, function () {',
  8271. ' this.$init = function () {',
  8272. ' this.ClassType = null;',
  8273. ' };',
  8274. ' this.$final = function () {',
  8275. ' this.ClassType = undefined;',
  8276. ' };',
  8277. '});',
  8278. 'this.b = false;',
  8279. 'this.Obj = null;',
  8280. 'this.C = null;'
  8281. ]),
  8282. LinesToStr([ // $mod.$main
  8283. '$mod.b = $mod.C == null;',
  8284. '$mod.b = null == $mod.C;',
  8285. '$mod.b = $mod.C == $mod.Obj.ClassType;',
  8286. '$mod.b = $mod.Obj.ClassType == $mod.C;',
  8287. '$mod.b = $mod.C == $mod.TObject;',
  8288. '$mod.b = $mod.TObject == $mod.C;',
  8289. '$mod.b = $mod.C != null;',
  8290. '$mod.b = null != $mod.C;',
  8291. '$mod.b = $mod.C != $mod.Obj.ClassType;',
  8292. '$mod.b = $mod.Obj.ClassType != $mod.C;',
  8293. '$mod.b = $mod.C != $mod.TObject;',
  8294. '$mod.b = $mod.TObject != $mod.C;',
  8295. '']));
  8296. end;
  8297. procedure TTestModule.TestClassOf_ClassVar;
  8298. begin
  8299. StartProgram(false);
  8300. Add('type');
  8301. Add(' TObject = class');
  8302. Add(' class var id: longint;');
  8303. Add(' end;');
  8304. Add(' TClass = class of TObject;');
  8305. Add('var');
  8306. Add(' C: tclass;');
  8307. Add('begin');
  8308. Add(' C.id:=C.id;');
  8309. ConvertProgram;
  8310. CheckSource('TestClassOf_ClassVar',
  8311. LinesToStr([ // statements
  8312. 'rtl.createClass($mod, "TObject", null, function () {',
  8313. ' this.id = 0;',
  8314. ' this.$init = function () {',
  8315. ' };',
  8316. ' this.$final = function () {',
  8317. ' };',
  8318. '});',
  8319. 'this.C = null;'
  8320. ]),
  8321. LinesToStr([ // $mod.$main
  8322. '$mod.C.id = $mod.C.id;',
  8323. '']));
  8324. end;
  8325. procedure TTestModule.TestClassOf_ClassMethod;
  8326. begin
  8327. StartProgram(false);
  8328. Add('type');
  8329. Add(' TObject = class');
  8330. Add(' class function DoIt(i: longint = 0): longint;');
  8331. Add(' end;');
  8332. Add(' TClass = class of TObject;');
  8333. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  8334. Add('var');
  8335. Add(' i: longint;');
  8336. Add(' C: tclass;');
  8337. Add('begin');
  8338. Add(' C.DoIt;');
  8339. Add(' C.DoIt();');
  8340. Add(' i:=C.DoIt;');
  8341. Add(' i:=C.DoIt();');
  8342. ConvertProgram;
  8343. CheckSource('TestClassOf_ClassMethod',
  8344. LinesToStr([ // statements
  8345. 'rtl.createClass($mod, "TObject", null, function () {',
  8346. ' this.$init = function () {',
  8347. ' };',
  8348. ' this.$final = function () {',
  8349. ' };',
  8350. ' this.DoIt = function (i) {',
  8351. ' var Result = 0;',
  8352. ' return Result;',
  8353. ' };',
  8354. '});',
  8355. 'this.i = 0;',
  8356. 'this.C = null;'
  8357. ]),
  8358. LinesToStr([ // $mod.$main
  8359. '$mod.C.DoIt(0);',
  8360. '$mod.C.DoIt(0);',
  8361. '$mod.i = $mod.C.DoIt(0);',
  8362. '$mod.i = $mod.C.DoIt(0);',
  8363. '']));
  8364. end;
  8365. procedure TTestModule.TestClassOf_ClassProperty;
  8366. begin
  8367. StartProgram(false);
  8368. Add('type');
  8369. Add(' TObject = class');
  8370. Add(' class var FA: longint;');
  8371. Add(' class function GetA: longint;');
  8372. Add(' class procedure SetA(Value: longint);');
  8373. Add(' class property pA: longint read fa write fa;');
  8374. Add(' class property pB: longint read geta write seta;');
  8375. Add(' end;');
  8376. Add(' TObjectClass = class of tobject;');
  8377. Add('class function tobject.geta: longint; begin end;');
  8378. Add('class procedure tobject.seta(value: longint); begin end;');
  8379. Add('var');
  8380. Add(' b: boolean;');
  8381. Add(' Obj: tobject;');
  8382. Add(' Cla: tobjectclass;');
  8383. Add('begin');
  8384. Add(' obj.pa:=obj.pa;');
  8385. Add(' obj.pb:=obj.pb;');
  8386. Add(' b:=obj.pa=4;');
  8387. Add(' b:=obj.pb=obj.pb;');
  8388. Add(' b:=5=obj.pa;');
  8389. Add(' cla.pa:=6;');
  8390. Add(' cla.pa:=cla.pa;');
  8391. Add(' cla.pb:=cla.pb;');
  8392. Add(' b:=cla.pa=7;');
  8393. Add(' b:=cla.pb=cla.pb;');
  8394. Add(' b:=8=cla.pa;');
  8395. Add(' tobject.pa:=9;');
  8396. Add(' tobject.pb:=tobject.pb;');
  8397. Add(' b:=tobject.pa=10;');
  8398. Add(' b:=11=tobject.pa;');
  8399. ConvertProgram;
  8400. CheckSource('TestClassOf_ClassProperty',
  8401. LinesToStr([ // statements
  8402. 'rtl.createClass($mod, "TObject", null, function () {',
  8403. ' this.FA = 0;',
  8404. ' this.$init = function () {',
  8405. ' };',
  8406. ' this.$final = function () {',
  8407. ' };',
  8408. ' this.GetA = function () {',
  8409. ' var Result = 0;',
  8410. ' return Result;',
  8411. ' };',
  8412. ' this.SetA = function (Value) {',
  8413. ' };',
  8414. '});',
  8415. 'this.b = false;',
  8416. 'this.Obj = null;',
  8417. 'this.Cla = null;'
  8418. ]),
  8419. LinesToStr([ // $mod.$main
  8420. '$mod.Obj.$class.FA = $mod.Obj.FA;',
  8421. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  8422. '$mod.b = $mod.Obj.FA == 4;',
  8423. '$mod.b = $mod.Obj.$class.GetA() == $mod.Obj.$class.GetA();',
  8424. '$mod.b = 5 == $mod.Obj.FA;',
  8425. '$mod.Cla.FA = 6;',
  8426. '$mod.Cla.FA = $mod.Cla.FA;',
  8427. '$mod.Cla.SetA($mod.Cla.GetA());',
  8428. '$mod.b = $mod.Cla.FA == 7;',
  8429. '$mod.b = $mod.Cla.GetA() == $mod.Cla.GetA();',
  8430. '$mod.b = 8 == $mod.Cla.FA;',
  8431. '$mod.TObject.FA = 9;',
  8432. '$mod.TObject.SetA($mod.TObject.GetA());',
  8433. '$mod.b = $mod.TObject.FA == 10;',
  8434. '$mod.b = 11 == $mod.TObject.FA;',
  8435. '']));
  8436. end;
  8437. procedure TTestModule.TestClassOf_ClassMethodSelf;
  8438. begin
  8439. StartProgram(false);
  8440. Add('type');
  8441. Add(' TObject = class');
  8442. Add(' class var GlobalId: longint;');
  8443. Add(' class procedure ProcA;');
  8444. Add(' end;');
  8445. Add('class procedure tobject.proca;');
  8446. Add('var b: boolean;');
  8447. Add('begin');
  8448. Add(' b:=self=nil;');
  8449. Add(' b:=self.globalid=3;');
  8450. Add(' b:=4=self.globalid;');
  8451. Add(' self.globalid:=5;');
  8452. Add(' self.proca;');
  8453. Add('end;');
  8454. Add('begin');
  8455. ConvertProgram;
  8456. CheckSource('TestClassOf_ClassMethodSelf',
  8457. LinesToStr([ // statements
  8458. 'rtl.createClass($mod, "TObject", null, function () {',
  8459. ' this.GlobalId = 0;',
  8460. ' this.$init = function () {',
  8461. ' };',
  8462. ' this.$final = function () {',
  8463. ' };',
  8464. ' this.ProcA = function () {',
  8465. ' var b = false;',
  8466. ' b = this == null;',
  8467. ' b = this.GlobalId == 3;',
  8468. ' b = 4 == this.GlobalId;',
  8469. ' this.GlobalId = 5;',
  8470. ' this.ProcA();',
  8471. ' };',
  8472. '});'
  8473. ]),
  8474. LinesToStr([ // $mod.$main
  8475. '']));
  8476. end;
  8477. procedure TTestModule.TestClassOf_TypeCast;
  8478. begin
  8479. StartProgram(false);
  8480. Add('type');
  8481. Add(' TObject = class');
  8482. Add(' class procedure {#TObject_DoIt}DoIt;');
  8483. Add(' end;');
  8484. Add(' TClass = class of TObject;');
  8485. Add(' TMobile = class');
  8486. Add(' class procedure {#TMobile_DoIt}DoIt;');
  8487. Add(' end;');
  8488. Add(' TMobileClass = class of TMobile;');
  8489. Add(' TCar = class(TMobile)');
  8490. Add(' class procedure {#TCar_DoIt}DoIt;');
  8491. Add(' end;');
  8492. Add(' TCarClass = class of TCar;');
  8493. Add('class procedure TObject.DoIt;');
  8494. Add('begin');
  8495. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  8496. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  8497. Add('end;');
  8498. Add('class procedure TMobile.DoIt;');
  8499. Add('begin');
  8500. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  8501. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  8502. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  8503. Add('end;');
  8504. Add('class procedure TCar.DoIt; begin end;');
  8505. Add('var');
  8506. Add(' ObjC: TClass;');
  8507. Add(' MobileC: TMobileClass;');
  8508. Add(' CarC: TCarClass;');
  8509. Add('begin');
  8510. Add(' ObjC.{@TObject_DoIt}DoIt;');
  8511. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  8512. Add(' CarC.{@TCar_DoIt}DoIt;');
  8513. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  8514. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  8515. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  8516. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  8517. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  8518. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  8519. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  8520. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  8521. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  8522. ConvertProgram;
  8523. CheckSource('TestClassOf_TypeCast',
  8524. LinesToStr([ // statements
  8525. 'rtl.createClass($mod, "TObject", null, function () {',
  8526. ' this.$init = function () {',
  8527. ' };',
  8528. ' this.$final = function () {',
  8529. ' };',
  8530. ' this.DoIt = function () {',
  8531. ' this.DoIt();',
  8532. ' this.DoIt$1();',
  8533. ' };',
  8534. '});',
  8535. 'rtl.createClass($mod, "TMobile", $mod.TObject, function () {',
  8536. ' this.DoIt$1 = function () {',
  8537. ' this.DoIt();',
  8538. ' this.DoIt$1();',
  8539. ' this.DoIt$2();',
  8540. ' };',
  8541. '});',
  8542. 'rtl.createClass($mod, "TCar", $mod.TMobile, function () {',
  8543. ' this.DoIt$2 = function () {',
  8544. ' };',
  8545. '});',
  8546. 'this.ObjC = null;',
  8547. 'this.MobileC = null;',
  8548. 'this.CarC = null;',
  8549. '']),
  8550. LinesToStr([ // $mod.$main
  8551. '$mod.ObjC.DoIt();',
  8552. '$mod.MobileC.DoIt$1();',
  8553. '$mod.CarC.DoIt$2();',
  8554. '$mod.ObjC.DoIt();',
  8555. '$mod.ObjC.DoIt$1();',
  8556. '$mod.ObjC.DoIt$2();',
  8557. '$mod.MobileC.DoIt();',
  8558. '$mod.MobileC.DoIt$1();',
  8559. '$mod.MobileC.DoIt$2();',
  8560. '$mod.CarC.DoIt();',
  8561. '$mod.CarC.DoIt$1();',
  8562. '$mod.CarC.DoIt$2();',
  8563. '']));
  8564. end;
  8565. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  8566. begin
  8567. StartProgram(false);
  8568. Add('type');
  8569. Add(' TObject = class');
  8570. Add(' function CurNow: longint; ');
  8571. Add(' class function Now: longint; ');
  8572. Add(' end;');
  8573. Add('function TObject.CurNow: longint; begin end;');
  8574. Add('class function TObject.Now: longint; begin end;');
  8575. Add('var');
  8576. Add(' Obj: tobject;');
  8577. Add(' vI: longint;');
  8578. Add('begin');
  8579. Add(' obj.curnow;');
  8580. Add(' vi:=obj.curnow;');
  8581. Add(' tobject.now;');
  8582. Add(' vi:=tobject.now;');
  8583. ConvertProgram;
  8584. CheckSource('TestClassOf_ImplicitFunctionCall',
  8585. LinesToStr([ // statements
  8586. 'rtl.createClass($mod, "TObject", null, function () {',
  8587. ' this.$init = function () {',
  8588. ' };',
  8589. ' this.$final = function () {',
  8590. ' };',
  8591. ' this.CurNow = function () {',
  8592. ' var Result = 0;',
  8593. ' return Result;',
  8594. ' };',
  8595. ' this.Now = function () {',
  8596. ' var Result = 0;',
  8597. ' return Result;',
  8598. ' };',
  8599. '});',
  8600. 'this.Obj = null;',
  8601. 'this.vI = 0;',
  8602. '']),
  8603. LinesToStr([ // $mod.$main
  8604. '$mod.Obj.CurNow();',
  8605. '$mod.vI = $mod.Obj.CurNow();',
  8606. '$mod.TObject.Now();',
  8607. '$mod.vI = $mod.TObject.Now();',
  8608. '']));
  8609. end;
  8610. procedure TTestModule.TestNestedClass_Fail;
  8611. begin
  8612. StartProgram(false);
  8613. Add([
  8614. 'type',
  8615. ' TObject = class',
  8616. ' type TNested = longint;',
  8617. ' end;',
  8618. 'begin']);
  8619. SetExpectedPasResolverError('not yet implemented: TNested:TPasAliasType [20170608232534] nested types',
  8620. nNotYetImplemented);
  8621. ConvertProgram;
  8622. end;
  8623. procedure TTestModule.TestExternalClass_Var;
  8624. begin
  8625. StartProgram(false);
  8626. Add('{$modeswitch externalclass}');
  8627. Add('type');
  8628. Add(' TExtA = class external name ''ExtObj''');
  8629. Add(' Id: longint external name ''$Id'';');
  8630. Add(' B: longint;');
  8631. Add(' end;');
  8632. Add('var Obj: TExtA;');
  8633. Add('begin');
  8634. Add(' obj.id:=obj.id+1;');
  8635. Add(' obj.B:=obj.B+1;');
  8636. ConvertProgram;
  8637. CheckSource('TestExternalClass_Var',
  8638. LinesToStr([ // statements
  8639. 'this.Obj = null;',
  8640. '']),
  8641. LinesToStr([ // $mod.$main
  8642. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  8643. '$mod.Obj.B = $mod.Obj.B + 1;',
  8644. '']));
  8645. end;
  8646. procedure TTestModule.TestExternalClass_Dollar;
  8647. begin
  8648. StartProgram(false);
  8649. Add([
  8650. '{$modeswitch externalclass}',
  8651. 'type',
  8652. ' TExtA = class external name ''$''',
  8653. ' Id: longint external name ''$'';',
  8654. ' function Bla(i: longint): longint; external name ''$'';',
  8655. ' end;',
  8656. 'function dollar(k: longint): longint; external name ''$'';',
  8657. 'var Obj: TExtA;',
  8658. 'begin',
  8659. ' dollar(1);',
  8660. ' obj.id:=obj.id+2;',
  8661. ' obj.Bla(3);',
  8662. '']);
  8663. ConvertProgram;
  8664. CheckSource('TestExternalClass_Dollar',
  8665. LinesToStr([ // statements
  8666. 'this.Obj = null;',
  8667. '']),
  8668. LinesToStr([ // $mod.$main
  8669. '$(1);',
  8670. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  8671. '$mod.Obj.$(3);',
  8672. '']));
  8673. end;
  8674. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  8675. begin
  8676. StartProgram(false);
  8677. Add('{$modeswitch externalclass}');
  8678. Add('type');
  8679. Add(' TExtA = class external name ''ExtA''');
  8680. Add(' Id: longint external name ''$Id'';');
  8681. Add(' end;');
  8682. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  8683. Add(' Id: longint;');
  8684. Add(' end;');
  8685. Add('begin');
  8686. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,6)',nDuplicateIdentifier);
  8687. ConvertProgram;
  8688. end;
  8689. procedure TTestModule.TestExternalClass_Method;
  8690. begin
  8691. StartProgram(false);
  8692. Add('{$modeswitch externalclass}');
  8693. Add('type');
  8694. Add(' TExtA = class external name ''ExtObj''');
  8695. Add(' procedure DoIt(Id: longint = 1); external name ''$Execute'';');
  8696. Add(' procedure DoSome(Id: longint = 1);');
  8697. Add(' end;');
  8698. Add('var Obj: texta;');
  8699. Add('begin');
  8700. Add(' obj.doit;');
  8701. Add(' obj.doit();');
  8702. Add(' obj.doit(2);');
  8703. Add(' with obj do begin');
  8704. Add(' doit;');
  8705. Add(' doit();');
  8706. Add(' doit(3);');
  8707. Add(' end;');
  8708. ConvertProgram;
  8709. CheckSource('TestExternalClass_Method',
  8710. LinesToStr([ // statements
  8711. 'this.Obj = null;',
  8712. '']),
  8713. LinesToStr([ // $mod.$main
  8714. '$mod.Obj.$Execute(1);',
  8715. '$mod.Obj.$Execute(1);',
  8716. '$mod.Obj.$Execute(2);',
  8717. 'var $with1 = $mod.Obj;',
  8718. '$with1.$Execute(1);',
  8719. '$with1.$Execute(1);',
  8720. '$with1.$Execute(3);',
  8721. '']));
  8722. end;
  8723. procedure TTestModule.TestExternalClass_NonExternalOverride;
  8724. begin
  8725. StartProgram(false);
  8726. Add('{$modeswitch externalclass}');
  8727. Add('type');
  8728. Add(' TExtA = class external name ''ExtObjA''');
  8729. Add(' procedure ProcA; virtual;');
  8730. Add(' procedure ProcB; virtual;');
  8731. Add(' end;');
  8732. Add(' TExtB = class external name ''ExtObjB'' (TExtA)');
  8733. Add(' end;');
  8734. Add(' TExtC = class (TExtB)');
  8735. Add(' procedure ProcA; override;');
  8736. Add(' end;');
  8737. Add('procedure TExtC.ProcA;');
  8738. Add('begin');
  8739. Add(' ProcA;');
  8740. Add(' Self.ProcA;');
  8741. Add(' ProcB;');
  8742. Add(' Self.ProcB;');
  8743. Add('end;');
  8744. Add('var');
  8745. Add(' A: texta;');
  8746. Add(' B: textb;');
  8747. Add(' C: textc;');
  8748. Add('begin');
  8749. Add(' a.proca;');
  8750. Add(' b.proca;');
  8751. Add(' c.proca;');
  8752. ConvertProgram;
  8753. CheckSource('TestExternalClass_NonExternalOverride',
  8754. LinesToStr([ // statements
  8755. 'rtl.createClassExt($mod, "TExtC", ExtObjB, "", function () {',
  8756. ' this.$init = function () {',
  8757. ' };',
  8758. ' this.$final = function () {',
  8759. ' };',
  8760. ' this.ProcA = function () {',
  8761. ' this.ProcA();',
  8762. ' this.ProcA();',
  8763. ' this.ProcB();',
  8764. ' this.ProcB();',
  8765. ' };',
  8766. '});',
  8767. 'this.A = null;',
  8768. 'this.B = null;',
  8769. 'this.C = null;',
  8770. '']),
  8771. LinesToStr([ // $mod.$main
  8772. '$mod.A.ProcA();',
  8773. '$mod.B.ProcA();',
  8774. '$mod.C.ProcA();',
  8775. '']));
  8776. end;
  8777. procedure TTestModule.TestExternalClass_Property;
  8778. begin
  8779. StartProgram(false);
  8780. Add('{$modeswitch externalclass}');
  8781. Add('type');
  8782. Add(' TExtA = class external name ''ExtA''');
  8783. Add(' function getYear: longint;');
  8784. Add(' procedure setYear(Value: longint);');
  8785. Add(' property Year: longint read getyear write setyear;');
  8786. Add(' end;');
  8787. Add(' TExtB = class (TExtA)');
  8788. Add(' procedure OtherSetYear(Value: longint);');
  8789. Add(' property year write othersetyear;');
  8790. Add(' end;');
  8791. Add('procedure textb.othersetyear(value: longint);');
  8792. Add('begin');
  8793. Add(' setYear(Value+4);');
  8794. Add('end;');
  8795. Add('var');
  8796. Add(' A: texta;');
  8797. Add(' B: textb;');
  8798. Add('begin');
  8799. Add(' a.year:=a.year+1;');
  8800. Add(' b.year:=b.year+2;');
  8801. ConvertProgram;
  8802. CheckSource('TestExternalClass_NonExternalOverride',
  8803. LinesToStr([ // statements
  8804. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  8805. ' this.$init = function () {',
  8806. ' };',
  8807. ' this.$final = function () {',
  8808. ' };',
  8809. ' this.OtherSetYear = function (Value) {',
  8810. ' this.setYear(Value+4);',
  8811. ' };',
  8812. '});',
  8813. 'this.A = null;',
  8814. 'this.B = null;',
  8815. '']),
  8816. LinesToStr([ // $mod.$main
  8817. '$mod.A.setYear($mod.A.getYear()+1);',
  8818. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  8819. '']));
  8820. end;
  8821. procedure TTestModule.TestExternalClass_ClassProperty;
  8822. begin
  8823. StartProgram(false);
  8824. Add('{$modeswitch externalclass}');
  8825. Add('type');
  8826. Add(' TExtA = class external name ''ExtA''');
  8827. Add(' class function getYear: longint;');
  8828. Add(' class procedure setYear(Value: longint);');
  8829. Add(' class property Year: longint read getyear write setyear;');
  8830. Add(' end;');
  8831. Add(' TExtB = class (TExtA)');
  8832. Add(' class function GetCentury: longint;');
  8833. Add(' class procedure SetCentury(Value: longint);');
  8834. Add(' class property Century: longint read getcentury write setcentury;');
  8835. Add(' end;');
  8836. Add('class function textb.getcentury: longint;');
  8837. Add('begin');
  8838. Add('end;');
  8839. Add('class procedure textb.setcentury(value: longint);');
  8840. Add('begin');
  8841. Add(' setyear(value+11);');
  8842. Add(' texta.year:=texta.year+12;');
  8843. Add(' year:=year+13;');
  8844. Add(' textb.century:=textb.century+14;');
  8845. Add(' century:=century+15;');
  8846. Add('end;');
  8847. Add('var');
  8848. Add(' A: texta;');
  8849. Add(' B: textb;');
  8850. Add('begin');
  8851. Add(' texta.year:=texta.year+1;');
  8852. Add(' textb.year:=textb.year+2;');
  8853. Add(' TextA.year:=TextA.year+3;');
  8854. Add(' b.year:=b.year+4;');
  8855. Add(' textb.century:=textb.century+5;');
  8856. Add(' b.century:=b.century+6;');
  8857. ConvertProgram;
  8858. CheckSource('TestExternalClass_ClassProperty',
  8859. LinesToStr([ // statements
  8860. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  8861. ' this.$init = function () {',
  8862. ' };',
  8863. ' this.$final = function () {',
  8864. ' };',
  8865. ' this.GetCentury = function () {',
  8866. ' var Result = 0;',
  8867. ' return Result;',
  8868. ' };',
  8869. ' this.SetCentury = function (Value) {',
  8870. ' this.setYear(Value + 11);',
  8871. ' ExtA.setYear(ExtA.getYear() + 12);',
  8872. ' this.setYear(this.getYear() + 13);',
  8873. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  8874. ' this.SetCentury(this.GetCentury() + 15);',
  8875. ' };',
  8876. '});',
  8877. 'this.A = null;',
  8878. 'this.B = null;',
  8879. '']),
  8880. LinesToStr([ // $mod.$main
  8881. 'ExtA.setYear(ExtA.getYear() + 1);',
  8882. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  8883. 'ExtA.setYear(ExtA.getYear() + 3);',
  8884. '$mod.B.setYear($mod.B.getYear() + 4);',
  8885. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  8886. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  8887. '']));
  8888. end;
  8889. procedure TTestModule.TestExternalClass_ClassOf;
  8890. begin
  8891. StartProgram(false);
  8892. Add('{$modeswitch externalclass}');
  8893. Add('type');
  8894. Add(' TExtA = class external name ''ExtA''');
  8895. Add(' procedure ProcA; virtual;');
  8896. Add(' procedure ProcB; virtual;');
  8897. Add(' end;');
  8898. Add(' TExtAClass = class of TExtA;');
  8899. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  8900. Add(' end;');
  8901. Add(' TExtBClass = class of TExtB;');
  8902. Add(' TExtC = class (TExtB)');
  8903. Add(' procedure ProcA; override;');
  8904. Add(' end;');
  8905. Add(' TExtCClass = class of TExtC;');
  8906. Add('procedure TExtC.ProcA; begin end;');
  8907. Add('var');
  8908. Add(' A: texta; ClA: TExtAClass;');
  8909. Add(' B: textb; ClB: TExtBClass;');
  8910. Add(' C: textc; ClC: TExtCClass;');
  8911. Add('begin');
  8912. Add(' ClA:=texta;');
  8913. Add(' ClA:=textb;');
  8914. Add(' ClA:=textc;');
  8915. Add(' ClB:=textb;');
  8916. Add(' ClB:=textc;');
  8917. Add(' ClC:=textc;');
  8918. ConvertProgram;
  8919. CheckSource('TestExternalClass_ClassOf',
  8920. LinesToStr([ // statements
  8921. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  8922. ' this.$init = function () {',
  8923. ' };',
  8924. ' this.$final = function () {',
  8925. ' };',
  8926. ' this.ProcA = function () {',
  8927. ' };',
  8928. '});',
  8929. 'this.A = null;',
  8930. 'this.ClA = null;',
  8931. 'this.B = null;',
  8932. 'this.ClB = null;',
  8933. 'this.C = null;',
  8934. 'this.ClC = null;',
  8935. '']),
  8936. LinesToStr([ // $mod.$main
  8937. '$mod.ClA = ExtA;',
  8938. '$mod.ClA = ExtB;',
  8939. '$mod.ClA = $mod.TExtC;',
  8940. '$mod.ClB = ExtB;',
  8941. '$mod.ClB = $mod.TExtC;',
  8942. '$mod.ClC = $mod.TExtC;',
  8943. '']));
  8944. end;
  8945. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  8946. begin
  8947. AddModuleWithIntfImplSrc('unit2.pas',
  8948. LinesToStr([
  8949. '{$modeswitch externalclass}',
  8950. 'type',
  8951. ' TExtA = class external name ''ExtA''',
  8952. ' class var Id: longint;',
  8953. ' end;',
  8954. '']),
  8955. '');
  8956. StartUnit(true);
  8957. Add('interface');
  8958. Add('uses unit2;');
  8959. Add('implementation');
  8960. Add('begin');
  8961. Add(' unit2.texta.id:=unit2.texta.id+1;');
  8962. ConvertUnit;
  8963. CheckSource('TestExternalClass_ClassOtherUnit',
  8964. LinesToStr([
  8965. '']),
  8966. LinesToStr([
  8967. 'ExtA.Id = ExtA.Id + 1;',
  8968. '']));
  8969. end;
  8970. procedure TTestModule.TestExternalClass_Is;
  8971. begin
  8972. StartProgram(false);
  8973. Add('{$modeswitch externalclass}');
  8974. Add('type');
  8975. Add(' TExtA = class external name ''ExtA''');
  8976. Add(' end;');
  8977. Add(' TExtAClass = class of TExtA;');
  8978. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  8979. Add(' end;');
  8980. Add(' TExtBClass = class of TExtB;');
  8981. Add(' TExtC = class (TExtB)');
  8982. Add(' end;');
  8983. Add(' TExtCClass = class of TExtC;');
  8984. Add('var');
  8985. Add(' A: texta; ClA: TExtAClass;');
  8986. Add(' B: textb; ClB: TExtBClass;');
  8987. Add(' C: textc; ClC: TExtCClass;');
  8988. Add('begin');
  8989. Add(' if a is textb then ;');
  8990. Add(' if a is textc then ;');
  8991. Add(' if b is textc then ;');
  8992. Add(' if cla is textb then ;');
  8993. Add(' if cla is textc then ;');
  8994. Add(' if clb is textc then ;');
  8995. ConvertProgram;
  8996. CheckSource('TestExternalClass_Is',
  8997. LinesToStr([ // statements
  8998. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  8999. ' this.$init = function () {',
  9000. ' };',
  9001. ' this.$final = function () {',
  9002. ' };',
  9003. '});',
  9004. 'this.A = null;',
  9005. 'this.ClA = null;',
  9006. 'this.B = null;',
  9007. 'this.ClB = null;',
  9008. 'this.C = null;',
  9009. 'this.ClC = null;',
  9010. '']),
  9011. LinesToStr([ // $mod.$main
  9012. 'if (rtl.isExt($mod.A, ExtB)) ;',
  9013. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  9014. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  9015. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  9016. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  9017. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  9018. '']));
  9019. end;
  9020. procedure TTestModule.TestExternalClass_As;
  9021. begin
  9022. StartProgram(false);
  9023. Add('{$modeswitch externalclass}');
  9024. Add('type');
  9025. Add(' TExtA = class external name ''ExtA''');
  9026. Add(' end;');
  9027. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  9028. Add(' end;');
  9029. Add(' TExtC = class (TExtB)');
  9030. Add(' end;');
  9031. Add('var');
  9032. Add(' A: texta;');
  9033. Add(' B: textb;');
  9034. Add(' C: textc;');
  9035. Add('begin');
  9036. Add(' b:=a as textb;');
  9037. Add(' c:=a as textc;');
  9038. Add(' c:=b as textc;');
  9039. ConvertProgram;
  9040. CheckSource('TestExternalClass_Is',
  9041. LinesToStr([ // statements
  9042. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  9043. ' this.$init = function () {',
  9044. ' };',
  9045. ' this.$final = function () {',
  9046. ' };',
  9047. '});',
  9048. 'this.A = null;',
  9049. 'this.B = null;',
  9050. 'this.C = null;',
  9051. '']),
  9052. LinesToStr([ // $mod.$main
  9053. '$mod.B = rtl.asExt($mod.A, ExtB);',
  9054. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  9055. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  9056. '']));
  9057. end;
  9058. procedure TTestModule.TestExternalClass_DestructorFail;
  9059. begin
  9060. StartProgram(false);
  9061. Add('{$modeswitch externalclass}');
  9062. Add('type');
  9063. Add(' TExtA = class external name ''ExtA''');
  9064. Add(' destructor Free;');
  9065. Add(' end;');
  9066. SetExpectedPasResolverError('Pascal element not supported: destructor',
  9067. nPasElementNotSupported);
  9068. ConvertProgram;
  9069. end;
  9070. procedure TTestModule.TestExternalClass_New;
  9071. begin
  9072. StartProgram(false);
  9073. Add('{$modeswitch externalclass}');
  9074. Add('type');
  9075. Add(' TExtA = class external name ''ExtA''');
  9076. Add(' constructor New;');
  9077. Add(' constructor New(i: longint; j: longint = 2);');
  9078. Add(' end;');
  9079. Add('var');
  9080. Add(' A: texta;');
  9081. Add('begin');
  9082. Add(' a:=texta.new;');
  9083. Add(' a:=texta.new();');
  9084. Add(' a:=texta.new(1);');
  9085. Add(' with texta do begin');
  9086. Add(' a:=new;');
  9087. Add(' a:=new();');
  9088. Add(' a:=new(2);');
  9089. Add(' end;');
  9090. Add(' a:=test1.texta.new;');
  9091. Add(' a:=test1.texta.new();');
  9092. Add(' a:=test1.texta.new(3);');
  9093. ConvertProgram;
  9094. CheckSource('TestExternalClass_New',
  9095. LinesToStr([ // statements
  9096. 'this.A = null;',
  9097. '']),
  9098. LinesToStr([ // $mod.$main
  9099. '$mod.A = new ExtA();',
  9100. '$mod.A = new ExtA();',
  9101. '$mod.A = new ExtA(1,2);',
  9102. '$mod.A = new ExtA();',
  9103. '$mod.A = new ExtA();',
  9104. '$mod.A = new ExtA(2,2);',
  9105. '$mod.A = new ExtA();',
  9106. '$mod.A = new ExtA();',
  9107. '$mod.A = new ExtA(3,2);',
  9108. '']));
  9109. end;
  9110. procedure TTestModule.TestExternalClass_ClassOf_New;
  9111. begin
  9112. StartProgram(false);
  9113. Add('{$modeswitch externalclass}');
  9114. Add('type');
  9115. Add(' TExtAClass = class of TExtA;');
  9116. Add(' TExtA = class external name ''ExtA''');
  9117. Add(' constructor New;');
  9118. Add(' end;');
  9119. Add('var');
  9120. Add(' A: texta;');
  9121. Add(' C: textaclass;');
  9122. Add('begin');
  9123. Add(' a:=c.new;');
  9124. Add(' a:=c.new();');
  9125. Add(' with C do begin');
  9126. Add(' a:=new;');
  9127. Add(' a:=new();');
  9128. Add(' end;');
  9129. Add(' a:=test1.c.new;');
  9130. Add(' a:=test1.c.new();');
  9131. ConvertProgram;
  9132. CheckSource('TestExternalClass_ClassOf_New',
  9133. LinesToStr([ // statements
  9134. 'this.A = null;',
  9135. 'this.C = null;',
  9136. '']),
  9137. LinesToStr([ // $mod.$main
  9138. '$mod.A = new $mod.C();',
  9139. '$mod.A = new $mod.C();',
  9140. 'var $with1 = $mod.C;',
  9141. '$mod.A = new $with1();',
  9142. '$mod.A = new $with1();',
  9143. '$mod.A = new $mod.C();',
  9144. '$mod.A = new $mod.C();',
  9145. '']));
  9146. end;
  9147. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  9148. begin
  9149. StartProgram(false);
  9150. Add('{$modeswitch externalclass}');
  9151. Add('type');
  9152. Add(' TExtAClass = class of TExtA;');
  9153. Add(' TExtA = class external name ''ExtA''');
  9154. Add(' constructor New;');
  9155. Add(' end;');
  9156. Add('function GetCreator: TExtAClass;');
  9157. Add('begin');
  9158. Add(' Result:=TExtA;');
  9159. Add('end;');
  9160. Add('var');
  9161. Add(' A: texta;');
  9162. Add('begin');
  9163. Add(' a:=getcreator.new;');
  9164. Add(' a:=getcreator().new;');
  9165. Add(' a:=getcreator().new();');
  9166. Add(' a:=getcreator.new();');
  9167. Add(' with getcreator do begin');
  9168. Add(' a:=new;');
  9169. Add(' a:=new();');
  9170. Add(' end;');
  9171. ConvertProgram;
  9172. CheckSource('TestExternalClass_FuncClassOf_New',
  9173. LinesToStr([ // statements
  9174. 'this.GetCreator = function () {',
  9175. ' var Result = null;',
  9176. ' Result = ExtA;',
  9177. ' return Result;',
  9178. '};',
  9179. 'this.A = null;',
  9180. '']),
  9181. LinesToStr([ // $mod.$main
  9182. '$mod.A = new ($mod.GetCreator())();',
  9183. '$mod.A = new ($mod.GetCreator())();',
  9184. '$mod.A = new ($mod.GetCreator())();',
  9185. '$mod.A = new ($mod.GetCreator())();',
  9186. 'var $with1 = $mod.GetCreator();',
  9187. '$mod.A = new $with1();',
  9188. '$mod.A = new $with1();',
  9189. '']));
  9190. end;
  9191. procedure TTestModule.TestExternalClass_LocalConstSameName;
  9192. begin
  9193. StartProgram(false);
  9194. Add('{$modeswitch externalclass}');
  9195. Add('type');
  9196. Add(' TExtA = class external name ''ExtA''');
  9197. Add(' constructor New;');
  9198. Add(' end;');
  9199. Add('function DoIt: longint;');
  9200. Add('const ExtA = 3;');
  9201. Add('begin');
  9202. Add(' Result:=ExtA;');
  9203. Add('end;');
  9204. Add('var');
  9205. Add(' A: texta;');
  9206. Add('begin');
  9207. Add(' a:=texta.new;');
  9208. ConvertProgram;
  9209. CheckSource('TestExternalClass_LocalConstSameName',
  9210. LinesToStr([ // statements
  9211. 'var ExtA$1 = 3;',
  9212. 'this.DoIt = function () {',
  9213. ' var Result = 0;',
  9214. ' Result = ExtA$1;',
  9215. ' return Result;',
  9216. '};',
  9217. 'this.A = null;',
  9218. '']),
  9219. LinesToStr([ // $mod.$main
  9220. '$mod.A = new ExtA();',
  9221. '']));
  9222. end;
  9223. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  9224. begin
  9225. StartProgram(false);
  9226. Add('{$modeswitch externalclass}');
  9227. Add('type');
  9228. Add(' TExtA = class external name ''ExtA''');
  9229. Add(' procedure DoIt;');
  9230. Add(' end;');
  9231. Add(' TMyA = class(TExtA)');
  9232. Add(' procedure DoIt;');
  9233. Add(' end;');
  9234. Add('procedure TMyA.DoIt; begin end;');
  9235. Add('begin');
  9236. ConvertProgram;
  9237. CheckSource('TestExternalClass_ReintroduceOverload',
  9238. LinesToStr([ // statements
  9239. 'rtl.createClassExt($mod, "TMyA", ExtA, "", function () {',
  9240. ' this.$init = function () {',
  9241. ' };',
  9242. ' this.$final = function () {',
  9243. ' };',
  9244. ' this.DoIt$1 = function () {',
  9245. ' };',
  9246. '});',
  9247. '']),
  9248. LinesToStr([ // $mod.$main
  9249. '']));
  9250. end;
  9251. procedure TTestModule.TestExternalClass_Inherited;
  9252. begin
  9253. StartProgram(false);
  9254. Add('{$modeswitch externalclass}');
  9255. Add('type');
  9256. Add(' TExtA = class external name ''ExtA''');
  9257. Add(' procedure DoIt(i: longint = 1); virtual;');
  9258. Add(' procedure DoSome(j: longint = 2);');
  9259. Add(' end;');
  9260. Add(' TExtB = class external name ''ExtB''(TExtA)');
  9261. Add(' end;');
  9262. Add(' TMyC = class(TExtB)');
  9263. Add(' procedure DoIt(i: longint = 1); override;');
  9264. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  9265. Add(' end;');
  9266. Add('procedure TMyC.DoIt(i: longint);');
  9267. Add('begin');
  9268. Add(' inherited;');
  9269. Add(' inherited DoIt;');
  9270. Add(' inherited DoIt();');
  9271. Add(' inherited DoIt(3);');
  9272. Add(' inherited DoSome;');
  9273. Add(' inherited DoSome();');
  9274. Add(' inherited DoSome(4);');
  9275. Add('end;');
  9276. Add('procedure TMyC.DoSome(j: longint);');
  9277. Add('begin');
  9278. Add(' inherited;');
  9279. Add('end;');
  9280. Add('begin');
  9281. ConvertProgram;
  9282. CheckSource('TestExternalClass_ReintroduceOverload',
  9283. LinesToStr([ // statements
  9284. 'rtl.createClassExt($mod, "TMyC", ExtB, "", function () {',
  9285. ' this.$init = function () {',
  9286. ' };',
  9287. ' this.$final = function () {',
  9288. ' };',
  9289. ' this.DoIt = function (i) {',
  9290. ' ExtB.DoIt.apply(this, arguments);',
  9291. ' ExtB.DoIt.call(this, 1);',
  9292. ' ExtB.DoIt.call(this, 1);',
  9293. ' ExtB.DoIt.call(this, 3);',
  9294. ' ExtB.DoSome.call(this, 2);',
  9295. ' ExtB.DoSome.call(this, 2);',
  9296. ' ExtB.DoSome.call(this, 4);',
  9297. ' };',
  9298. ' this.DoSome$1 = function (j) {',
  9299. ' ExtB.DoSome.apply(this, arguments);',
  9300. ' };',
  9301. '});',
  9302. '']),
  9303. LinesToStr([ // $mod.$main
  9304. '']));
  9305. end;
  9306. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  9307. begin
  9308. StartProgram(false);
  9309. Add('{$modeswitch externalclass}');
  9310. Add('type');
  9311. Add(' TObject = class');
  9312. Add(' end;');
  9313. Add(' TExtA = class external name ''ExtA''(TObject)');
  9314. Add(' end;');
  9315. Add('begin');
  9316. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  9317. ConvertProgram;
  9318. end;
  9319. procedure TTestModule.TestExternalClass_NewInstance;
  9320. begin
  9321. StartProgram(false);
  9322. Add('{$modeswitch externalclass}');
  9323. Add('type');
  9324. Add(' TExtA = class external name ''ExtA''');
  9325. Add(' end;');
  9326. Add(' TMyB = class(TExtA)');
  9327. Add(' protected');
  9328. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  9329. Add(' end;');
  9330. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  9331. Add('begin end;');
  9332. Add('begin');
  9333. ConvertProgram;
  9334. CheckSource('TestExternalClass_NewInstance',
  9335. LinesToStr([ // statements
  9336. 'rtl.createClassExt($mod, "TMyB", ExtA, "NewInstance", function () {',
  9337. ' this.$init = function () {',
  9338. ' };',
  9339. ' this.$final = function () {',
  9340. ' };',
  9341. ' this.NewInstance = function (fnname, paramarray) {',
  9342. ' var Result = null;',
  9343. ' return Result;',
  9344. ' };',
  9345. '});',
  9346. '']),
  9347. LinesToStr([ // $mod.$main
  9348. '']));
  9349. end;
  9350. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  9351. begin
  9352. StartProgram(false);
  9353. Add('{$modeswitch externalclass}');
  9354. Add('type');
  9355. Add(' TExtA = class external name ''ExtA''');
  9356. Add(' end;');
  9357. Add(' TMyB = class(TExtA)');
  9358. Add(' protected');
  9359. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  9360. Add(' end;');
  9361. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  9362. Add('begin end;');
  9363. Add('begin');
  9364. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  9365. ConvertProgram;
  9366. end;
  9367. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  9368. begin
  9369. StartProgram(false);
  9370. Add('{$modeswitch externalclass}');
  9371. Add('type');
  9372. Add(' TExtA = class external name ''ExtA''');
  9373. Add(' end;');
  9374. Add(' TMyB = class(TExtA)');
  9375. Add(' protected');
  9376. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  9377. Add(' end;');
  9378. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  9379. Add('begin end;');
  9380. Add('begin');
  9381. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Longint", expected "String"',
  9382. nIncompatibleTypeArgNo);
  9383. ConvertProgram;
  9384. end;
  9385. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  9386. begin
  9387. StartProgram(false);
  9388. Add('{$modeswitch externalclass}');
  9389. Add('type');
  9390. Add(' TExtA = class external name ''ExtA''');
  9391. Add(' end;');
  9392. Add(' TMyB = class(TExtA)');
  9393. Add(' protected');
  9394. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  9395. Add(' end;');
  9396. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  9397. Add('begin end;');
  9398. Add('begin');
  9399. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "type", expected "untyped"',
  9400. nIncompatibleTypeArgNo);
  9401. ConvertProgram;
  9402. end;
  9403. procedure TTestModule.TestExternalClass_PascalProperty;
  9404. begin
  9405. StartProgram(false);
  9406. Add('{$modeswitch externalclass}');
  9407. Add('type');
  9408. Add(' TJSElement = class;');
  9409. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  9410. Add(' TJSElement = class external name ''ExtA''');
  9411. Add(' end;');
  9412. Add(' TControl = class(TJSElement)');
  9413. Add(' private');
  9414. Add(' FOnClick: TJSNotifyEvent;');
  9415. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  9416. Add(' procedure Click(Sender: TJSElement);');
  9417. Add(' end;');
  9418. Add('procedure TControl.Click(Sender: TJSElement);');
  9419. Add('begin');
  9420. Add(' OnClick(Self);');
  9421. Add('end;');
  9422. Add('var');
  9423. Add(' Ctrl: TControl;');
  9424. Add('begin');
  9425. Add(' Ctrl.OnClick:[email protected];');
  9426. Add(' Ctrl.OnClick(Ctrl);');
  9427. ConvertProgram;
  9428. CheckSource('TestExternalClass_PascalProperty',
  9429. LinesToStr([ // statements
  9430. 'rtl.createClassExt($mod, "TControl", ExtA, "", function () {',
  9431. ' this.$init = function () {',
  9432. ' this.FOnClick = null;',
  9433. ' };',
  9434. ' this.$final = function () {',
  9435. ' this.FOnClick = undefined;',
  9436. ' };',
  9437. ' this.Click = function (Sender) {',
  9438. ' this.FOnClick(this);',
  9439. ' };',
  9440. '});',
  9441. 'this.Ctrl = null;',
  9442. '']),
  9443. LinesToStr([ // $mod.$main
  9444. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  9445. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  9446. '']));
  9447. end;
  9448. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  9449. begin
  9450. StartProgram(false);
  9451. Add('{$modeswitch externalclass}');
  9452. Add('type');
  9453. Add(' TObject = class');
  9454. Add(' end;');
  9455. Add(' TChild = class');
  9456. Add(' end;');
  9457. Add(' TExtRootA = class external name ''ExtRootA''');
  9458. Add(' end;');
  9459. Add(' TExtChildA = class external name ''ExtChildA''(TExtRootA)');
  9460. Add(' end;');
  9461. Add(' TExtRootB = class external name ''ExtRootB''');
  9462. Add(' end;');
  9463. Add(' TExtChildB = class external name ''ExtChildB''(TExtRootB)');
  9464. Add(' end;');
  9465. Add('var');
  9466. Add(' Obj: TObject;');
  9467. Add(' Child: TChild;');
  9468. Add(' RootA: TExtRootA;');
  9469. Add(' ChildA: TExtChildA;');
  9470. Add(' RootB: TExtRootB;');
  9471. Add(' ChildB: TExtChildB;');
  9472. Add('begin');
  9473. Add(' obj:=tobject(roota);');
  9474. Add(' obj:=tobject(childa);');
  9475. Add(' child:=tchild(tobject(roota));');
  9476. Add(' roota:=textroota(obj);');
  9477. Add(' roota:=textroota(child);');
  9478. Add(' roota:=textroota(rootb);');
  9479. Add(' roota:=textroota(childb);');
  9480. Add(' childa:=textchilda(textroota(obj));');
  9481. ConvertProgram;
  9482. CheckSource('TestExternalClass_TypeCastToRootClass',
  9483. LinesToStr([ // statements
  9484. 'rtl.createClass($mod, "TObject", null, function () {',
  9485. ' this.$init = function () {',
  9486. ' };',
  9487. ' this.$final = function () {',
  9488. ' };',
  9489. '});',
  9490. 'rtl.createClass($mod, "TChild", $mod.TObject, function () {',
  9491. '});',
  9492. 'this.Obj = null;',
  9493. 'this.Child = null;',
  9494. 'this.RootA = null;',
  9495. 'this.ChildA = null;',
  9496. 'this.RootB = null;',
  9497. 'this.ChildB = null;',
  9498. '']),
  9499. LinesToStr([ // $mod.$main
  9500. '$mod.Obj = $mod.RootA;',
  9501. '$mod.Obj = $mod.ChildA;',
  9502. '$mod.Child = $mod.RootA;',
  9503. '$mod.RootA = $mod.Obj;',
  9504. '$mod.RootA = $mod.Child;',
  9505. '$mod.RootA = $mod.RootB;',
  9506. '$mod.RootA = $mod.ChildB;',
  9507. '$mod.ChildA = $mod.Obj;',
  9508. '']));
  9509. end;
  9510. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  9511. begin
  9512. StartProgram(false);
  9513. Add('{$modeswitch externalclass}');
  9514. Add('type');
  9515. Add(' TJSString = class external name ''String''');
  9516. Add(' class function fromCharCode() : string; varargs;');
  9517. Add(' function anchor(const aName : string) : string;');
  9518. Add(' end;');
  9519. Add('var');
  9520. Add(' s: string;');
  9521. Add('begin');
  9522. Add(' s:=TJSString.fromCharCode(65,66);');
  9523. Add(' s:=TJSString(s).anchor(s);');
  9524. Add(' s:=TJSString(''foo'').anchor(s);');
  9525. ConvertProgram;
  9526. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  9527. LinesToStr([ // statements
  9528. 'this.s = "";',
  9529. '']),
  9530. LinesToStr([ // $mod.$main
  9531. '$mod.s = String.fromCharCode(65, 66);',
  9532. '$mod.s = $mod.s.anchor($mod.s);',
  9533. '$mod.s = "foo".anchor($mod.s);',
  9534. '']));
  9535. end;
  9536. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  9537. begin
  9538. StartProgram(false);
  9539. Add('{$modeswitch externalclass}');
  9540. Add('type');
  9541. Add(' TJSString = class external name ''String''');
  9542. Add(' class function fromCharCode() : string; varargs;');
  9543. Add(' end;');
  9544. Add('var');
  9545. Add(' s: string;');
  9546. Add(' sObj: TJSString;');
  9547. Add('begin');
  9548. Add(' s:=sObj.fromCharCode(65,66);');
  9549. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  9550. nExternalClassInstanceCannotAccessStaticX);
  9551. ConvertProgram;
  9552. end;
  9553. procedure TTestModule.TestExternalClass_BracketAccessor;
  9554. begin
  9555. StartProgram(false);
  9556. Add('{$modeswitch externalclass}');
  9557. Add('type');
  9558. Add(' TJSArray = class external name ''Array2''');
  9559. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  9560. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  9561. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  9562. Add(' end;');
  9563. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);');
  9564. Add('begin end;');
  9565. Add('var');
  9566. Add(' Arr: tjsarray;');
  9567. Add(' s: string;');
  9568. Add(' i: longint;');
  9569. Add(' v: jsvalue;');
  9570. Add('begin');
  9571. Add(' v:=arr[0];');
  9572. Add(' v:=arr.items[1];');
  9573. Add(' arr[2]:=s;');
  9574. Add(' arr.items[3]:=s;');
  9575. Add(' arr[4]:=i;');
  9576. Add(' arr[5]:=arr[6];');
  9577. Add(' arr.items[7]:=arr.items[8];');
  9578. Add(' with arr do items[9]:=items[10];');
  9579. Add(' doit(arr[7],arr[8],arr[9],arr[10]);');
  9580. ConvertProgram;
  9581. CheckSource('TestExternalClass_BracketAccessor',
  9582. LinesToStr([ // statements
  9583. 'this.DoIt = function (vI, vJ, vK, vL) {',
  9584. '};',
  9585. 'this.Arr = null;',
  9586. 'this.s = "";',
  9587. 'this.i = 0;',
  9588. 'this.v = undefined;',
  9589. '']),
  9590. LinesToStr([ // $mod.$main
  9591. '$mod.v = $mod.Arr[0];',
  9592. '$mod.v = $mod.Arr[1];',
  9593. '$mod.Arr[2] = $mod.s;',
  9594. '$mod.Arr[3] = $mod.s;',
  9595. '$mod.Arr[4] = $mod.i;',
  9596. '$mod.Arr[5] = $mod.Arr[6];',
  9597. '$mod.Arr[7] = $mod.Arr[8];',
  9598. 'var $with1 = $mod.Arr;',
  9599. '$with1[9] = $with1[10];',
  9600. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  9601. ' a: 9,',
  9602. ' p: $mod.Arr,',
  9603. ' get: function () {',
  9604. ' return this.p[this.a];',
  9605. ' },',
  9606. ' set: function (v) {',
  9607. ' this.p[this.a] = v;',
  9608. ' }',
  9609. '}, {',
  9610. ' a: 10,',
  9611. ' p: $mod.Arr,',
  9612. ' get: function () {',
  9613. ' return this.p[this.a];',
  9614. ' },',
  9615. ' set: function (v) {',
  9616. ' this.p[this.a] = v;',
  9617. ' }',
  9618. '});',
  9619. '']));
  9620. end;
  9621. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  9622. begin
  9623. StartProgram(false);
  9624. Add('{$modeswitch externalclass}');
  9625. Add('type');
  9626. Add(' TJSArray = class external name ''Array2''');
  9627. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  9628. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  9629. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  9630. Add(' end;');
  9631. Add('begin');
  9632. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  9633. nBracketAccessorOfExternalClassMustHaveOneParameter);
  9634. ConvertProgram;
  9635. end;
  9636. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  9637. begin
  9638. StartProgram(false);
  9639. Add('{$modeswitch externalclass}');
  9640. Add('type');
  9641. Add(' TJSArray = class external name ''Array2''');
  9642. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  9643. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  9644. Add(' end;');
  9645. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  9646. Add('begin end;');
  9647. Add('var');
  9648. Add(' Arr: tjsarray;');
  9649. Add(' v: jsvalue;');
  9650. Add('begin');
  9651. Add(' v:=arr[0];');
  9652. Add(' v:=arr.items[1];');
  9653. Add(' with arr do v:=items[2];');
  9654. Add(' doit(arr[3],arr[4]);');
  9655. ConvertProgram;
  9656. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  9657. LinesToStr([ // statements
  9658. 'this.DoIt = function (vI, vJ) {',
  9659. '};',
  9660. 'this.Arr = null;',
  9661. 'this.v = undefined;',
  9662. '']),
  9663. LinesToStr([ // $mod.$main
  9664. '$mod.v = $mod.Arr[0];',
  9665. '$mod.v = $mod.Arr[1];',
  9666. 'var $with1 = $mod.Arr;',
  9667. '$mod.v = $with1[2];',
  9668. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  9669. '']));
  9670. end;
  9671. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  9672. begin
  9673. StartProgram(false);
  9674. Add('{$modeswitch externalclass}');
  9675. Add('type');
  9676. Add(' TJSArray = class external name ''Array2''');
  9677. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  9678. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  9679. Add(' end;');
  9680. Add('var');
  9681. Add(' Arr: tjsarray;');
  9682. Add(' s: string;');
  9683. Add(' i: longint;');
  9684. Add(' v: jsvalue;');
  9685. Add('begin');
  9686. Add(' arr[2]:=s;');
  9687. Add(' arr.items[3]:=s;');
  9688. Add(' arr[4]:=i;');
  9689. Add(' with arr do items[5]:=i;');
  9690. ConvertProgram;
  9691. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  9692. LinesToStr([ // statements
  9693. 'this.Arr = null;',
  9694. 'this.s = "";',
  9695. 'this.i = 0;',
  9696. 'this.v = undefined;',
  9697. '']),
  9698. LinesToStr([ // $mod.$main
  9699. '$mod.Arr[2] = $mod.s;',
  9700. '$mod.Arr[3] = $mod.s;',
  9701. '$mod.Arr[4] = $mod.i;',
  9702. 'var $with1 = $mod.Arr;',
  9703. '$with1[5] = $mod.i;',
  9704. '']));
  9705. end;
  9706. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  9707. begin
  9708. StartProgram(false);
  9709. Add('{$modeswitch externalclass}');
  9710. Add('type');
  9711. Add(' TJSArray = class external name ''Array2''');
  9712. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  9713. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  9714. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  9715. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  9716. Add(' end;');
  9717. Add('var');
  9718. Add(' Arr: tjsarray;');
  9719. Add(' s: string;');
  9720. Add(' i: longint;');
  9721. Add(' v: jsvalue;');
  9722. Add('begin');
  9723. Add(' arr[2]:=s;');
  9724. Add(' arr.items[3]:=s;');
  9725. Add(' arr.numbers[4]:=i;');
  9726. Add(' with arr do items[5]:=i;');
  9727. Add(' with arr do numbers[6]:=i;');
  9728. ConvertProgram;
  9729. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  9730. LinesToStr([ // statements
  9731. 'this.Arr = null;',
  9732. 'this.s = "";',
  9733. 'this.i = 0;',
  9734. 'this.v = undefined;',
  9735. '']),
  9736. LinesToStr([ // $mod.$main
  9737. '$mod.Arr[2] = $mod.s;',
  9738. '$mod.Arr[3] = $mod.s;',
  9739. '$mod.Arr[4] = $mod.i;',
  9740. 'var $with1 = $mod.Arr;',
  9741. '$with1[5] = $mod.i;',
  9742. 'var $with2 = $mod.Arr;',
  9743. '$with2[6] = $mod.i;',
  9744. '']));
  9745. end;
  9746. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  9747. begin
  9748. StartProgram(false);
  9749. Add('{$modeswitch externalclass}');
  9750. Add('type');
  9751. Add(' TJSArray = class external name ''Array2''');
  9752. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  9753. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  9754. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  9755. Add(' end;');
  9756. Add('var');
  9757. Add(' Arr: tjsarray;');
  9758. Add(' i: longint;');
  9759. Add(' IntArr: array of longint;');
  9760. Add(' v: jsvalue;');
  9761. Add('begin');
  9762. Add(' v:=arr.items[i];');
  9763. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  9764. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  9765. ConvertProgram;
  9766. CheckSource('TestExternalClass_BracketAccessor_Index',
  9767. LinesToStr([ // statements
  9768. 'this.Arr = null;',
  9769. 'this.i = 0;',
  9770. 'this.IntArr = [];',
  9771. 'this.v = undefined;',
  9772. '']),
  9773. LinesToStr([ // $mod.$main
  9774. '$mod.v = $mod.Arr[$mod.i];',
  9775. '$mod.Arr[Math.floor($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  9776. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  9777. '']));
  9778. end;
  9779. procedure TTestModule.TestProcType;
  9780. begin
  9781. StartProgram(false);
  9782. Add('type');
  9783. Add(' TProcInt = procedure(vI: longint = 1);');
  9784. Add('procedure DoIt(vJ: longint);');
  9785. Add('begin end;');
  9786. Add('var');
  9787. Add(' b: boolean;');
  9788. Add(' vP, vQ: tprocint;');
  9789. Add('begin');
  9790. Add(' vp:=nil;');
  9791. Add(' vp:=vp;');
  9792. Add(' vp:=@doit;');
  9793. Add(' vp;');
  9794. Add(' vp();');
  9795. Add(' vp(2);');
  9796. Add(' b:=vp=nil;');
  9797. Add(' b:=nil=vp;');
  9798. Add(' b:=vp=vq;');
  9799. Add(' b:=vp=@doit;');
  9800. Add(' b:=@doit=vp;');
  9801. Add(' b:=vp<>nil;');
  9802. Add(' b:=nil<>vp;');
  9803. Add(' b:=vp<>vq;');
  9804. Add(' b:=vp<>@doit;');
  9805. Add(' b:=@doit<>vp;');
  9806. Add(' b:=Assigned(vp);');
  9807. Add(' if Assigned(vp) then ;');
  9808. ConvertProgram;
  9809. CheckSource('TestProcType',
  9810. LinesToStr([ // statements
  9811. 'this.DoIt = function(vJ) {',
  9812. '};',
  9813. 'this.b = false;',
  9814. 'this.vP = null;',
  9815. 'this.vQ = null;'
  9816. ]),
  9817. LinesToStr([ // $mod.$main
  9818. '$mod.vP = null;',
  9819. '$mod.vP = $mod.vP;',
  9820. '$mod.vP = $mod.DoIt;',
  9821. '$mod.vP(1);',
  9822. '$mod.vP(1);',
  9823. '$mod.vP(2);',
  9824. '$mod.b = $mod.vP == null;',
  9825. '$mod.b = null == $mod.vP;',
  9826. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  9827. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  9828. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  9829. '$mod.b = $mod.vP != null;',
  9830. '$mod.b = null != $mod.vP;',
  9831. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  9832. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  9833. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  9834. '$mod.b = $mod.vP != null;',
  9835. 'if ($mod.vP != null) ;',
  9836. '']));
  9837. end;
  9838. procedure TTestModule.TestProcType_FunctionFPC;
  9839. begin
  9840. StartProgram(false);
  9841. Add('type');
  9842. Add(' TFuncInt = function(vA: longint = 1): longint;');
  9843. Add('function DoIt(vI: longint): longint;');
  9844. Add('begin end;');
  9845. Add('var');
  9846. Add(' b: boolean;');
  9847. Add(' vP, vQ: tfuncint;');
  9848. Add('begin');
  9849. Add(' vp:=nil;');
  9850. Add(' vp:=vp;');
  9851. Add(' vp:=@doit;'); // ok in fpc and delphi
  9852. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  9853. Add(' vp;'); // ok in fpc and delphi
  9854. Add(' vp();');
  9855. Add(' vp(2);');
  9856. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  9857. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  9858. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  9859. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  9860. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  9861. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  9862. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  9863. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  9864. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  9865. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  9866. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  9867. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  9868. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  9869. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  9870. Add(' b:=Assigned(vp);');
  9871. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  9872. Add(' doit(vp());'); // ok in fpc and delphi
  9873. Add(' doit(vp(2));'); // ok in fpc and delphi
  9874. ConvertProgram;
  9875. CheckSource('TestProcType_FunctionFPC',
  9876. LinesToStr([ // statements
  9877. 'this.DoIt = function(vI) {',
  9878. ' var Result = 0;',
  9879. ' return Result;',
  9880. '};',
  9881. 'this.b = false;',
  9882. 'this.vP = null;',
  9883. 'this.vQ = null;'
  9884. ]),
  9885. LinesToStr([ // $mod.$main
  9886. '$mod.vP = null;',
  9887. '$mod.vP = $mod.vP;',
  9888. '$mod.vP = $mod.DoIt;',
  9889. '$mod.vP(1);',
  9890. '$mod.vP(1);',
  9891. '$mod.vP(2);',
  9892. '$mod.b = $mod.vP == null;',
  9893. '$mod.b = null == $mod.vP;',
  9894. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  9895. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  9896. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  9897. '$mod.b = 4 == $mod.vP(1);',
  9898. '$mod.b = $mod.vP != null;',
  9899. '$mod.b = null != $mod.vP;',
  9900. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  9901. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  9902. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  9903. '$mod.b = 6 != $mod.vP(1);',
  9904. '$mod.b = $mod.vP != null;',
  9905. '$mod.DoIt($mod.vP(1));',
  9906. '$mod.DoIt($mod.vP(2));',
  9907. '']));
  9908. end;
  9909. procedure TTestModule.TestProcType_FunctionDelphi;
  9910. begin
  9911. StartProgram(false);
  9912. Add('{$mode Delphi}');
  9913. Add('type');
  9914. Add(' TFuncInt = function(vA: longint = 1): longint;');
  9915. Add('function DoIt(vI: longint): longint;');
  9916. Add('begin end;');
  9917. Add('var');
  9918. Add(' b: boolean;');
  9919. Add(' vP, vQ: tfuncint;');
  9920. Add('begin');
  9921. Add(' vp:=nil;');
  9922. Add(' vp:=vp;');
  9923. Add(' vp:=@doit;'); // ok in fpc and delphi
  9924. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  9925. Add(' vp;'); // ok in fpc and delphi
  9926. Add(' vp();');
  9927. Add(' vp(2);');
  9928. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  9929. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  9930. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  9931. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  9932. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  9933. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  9934. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  9935. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  9936. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  9937. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  9938. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  9939. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  9940. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  9941. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  9942. Add(' b:=Assigned(vp);');
  9943. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  9944. Add(' doit(vp());'); // ok in fpc and delphi
  9945. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  9946. ConvertProgram;
  9947. CheckSource('TestProcType_FunctionDelphi',
  9948. LinesToStr([ // statements
  9949. 'this.DoIt = function(vI) {',
  9950. ' var Result = 0;',
  9951. ' return Result;',
  9952. '};',
  9953. 'this.b = false;',
  9954. 'this.vP = null;',
  9955. 'this.vQ = null;'
  9956. ]),
  9957. LinesToStr([ // $mod.$main
  9958. '$mod.vP = null;',
  9959. '$mod.vP = $mod.vP;',
  9960. '$mod.vP = $mod.DoIt;',
  9961. '$mod.vP = $mod.DoIt;',
  9962. '$mod.vP(1);',
  9963. '$mod.vP(1);',
  9964. '$mod.vP(2);',
  9965. '$mod.b = $mod.vP(1) == $mod.vQ(1);',
  9966. '$mod.b = $mod.vP(1) == 3;',
  9967. '$mod.b = 4 == $mod.vP(1);',
  9968. '$mod.b = $mod.vP(1) != $mod.vQ(1);',
  9969. '$mod.b = $mod.vP(1) != 5;',
  9970. '$mod.b = 6 != $mod.vP(1);',
  9971. '$mod.b = $mod.vP != null;',
  9972. '$mod.DoIt($mod.vP(1));',
  9973. '$mod.DoIt($mod.vP(1));',
  9974. '$mod.DoIt($mod.vP(2));',
  9975. '']));
  9976. end;
  9977. procedure TTestModule.TestProcType_AsParam;
  9978. begin
  9979. StartProgram(false);
  9980. Add('type');
  9981. Add(' TFuncInt = function(vA: longint = 1): longint;');
  9982. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  9983. Add('var vJ: tfuncint;');
  9984. Add('begin');
  9985. Add(' vg:=vg;');
  9986. Add(' vj:=vh;');
  9987. Add(' vi:=vi;');
  9988. Add(' doit(vg,vg,vg);');
  9989. Add(' doit(vh,vh,vj);');
  9990. Add(' doit(vi,vi,vi);');
  9991. Add(' doit(vj,vj,vj);');
  9992. Add('end;');
  9993. Add('var i: tfuncint;');
  9994. Add('begin');
  9995. Add(' doit(i,i,i);');
  9996. ConvertProgram;
  9997. CheckSource('TestProcType_AsParam',
  9998. LinesToStr([ // statements
  9999. 'this.DoIt = function (vG,vH,vI) {',
  10000. ' var vJ = null;',
  10001. ' vG = vG;',
  10002. ' vJ = vH;',
  10003. ' vI.set(vI.get());',
  10004. ' $mod.DoIt(vG, vG, {',
  10005. ' get: function () {',
  10006. ' return vG;',
  10007. ' },',
  10008. ' set: function (v) {',
  10009. ' vG = v;',
  10010. ' }',
  10011. ' });',
  10012. ' $mod.DoIt(vH, vH, {',
  10013. ' get: function () {',
  10014. ' return vJ;',
  10015. ' },',
  10016. ' set: function (v) {',
  10017. ' vJ = v;',
  10018. ' }',
  10019. ' });',
  10020. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  10021. ' $mod.DoIt(vJ, vJ, {',
  10022. ' get: function () {',
  10023. ' return vJ;',
  10024. ' },',
  10025. ' set: function (v) {',
  10026. ' vJ = v;',
  10027. ' }',
  10028. ' });',
  10029. '};',
  10030. 'this.i = null;'
  10031. ]),
  10032. LinesToStr([
  10033. '$mod.DoIt($mod.i,$mod.i,{',
  10034. ' p: $mod,',
  10035. ' get: function () {',
  10036. ' return this.p.i;',
  10037. ' },',
  10038. ' set: function (v) {',
  10039. ' this.p.i = v;',
  10040. ' }',
  10041. '});'
  10042. ]));
  10043. end;
  10044. procedure TTestModule.TestProcType_MethodFPC;
  10045. begin
  10046. StartProgram(false);
  10047. Add('type');
  10048. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  10049. Add(' TObject = class');
  10050. Add(' function DoIt(vA: longint = 1): longint;');
  10051. Add(' end;');
  10052. Add('function TObject.DoIt(vA: longint = 1): longint;');
  10053. Add('begin');
  10054. Add('end;');
  10055. Add('var');
  10056. Add(' Obj: TObject;');
  10057. Add(' vP: tfuncint;');
  10058. Add(' b: boolean;');
  10059. Add('begin');
  10060. Add(' vp:[email protected];'); // ok in fpc and delphi
  10061. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  10062. Add(' vp;'); // ok in fpc and delphi
  10063. Add(' vp();');
  10064. Add(' vp(2);');
  10065. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  10066. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  10067. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  10068. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  10069. ConvertProgram;
  10070. CheckSource('TestProcType_MethodFPC',
  10071. LinesToStr([ // statements
  10072. 'rtl.createClass($mod, "TObject", null, function () {',
  10073. ' this.$init = function () {',
  10074. ' };',
  10075. ' this.$final = function () {',
  10076. ' };',
  10077. ' this.DoIt = function (vA) {',
  10078. ' var Result = 0;',
  10079. ' return Result;',
  10080. ' };',
  10081. '});',
  10082. 'this.Obj = null;',
  10083. 'this.vP = null;',
  10084. 'this.b = false;'
  10085. ]),
  10086. LinesToStr([
  10087. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  10088. '$mod.vP(1);',
  10089. '$mod.vP(1);',
  10090. '$mod.vP(2);',
  10091. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  10092. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  10093. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  10094. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  10095. '']));
  10096. end;
  10097. procedure TTestModule.TestProcType_MethodDelphi;
  10098. begin
  10099. StartProgram(false);
  10100. Add('{$mode delphi}');
  10101. Add('type');
  10102. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  10103. Add(' TObject = class');
  10104. Add(' function DoIt(vA: longint = 1): longint;');
  10105. Add(' end;');
  10106. Add('function TObject.DoIt(vA: longint = 1): longint;');
  10107. Add('begin');
  10108. Add('end;');
  10109. Add('var');
  10110. Add(' Obj: TObject;');
  10111. Add(' vP: tfuncint;');
  10112. Add(' b: boolean;');
  10113. Add('begin');
  10114. Add(' vp:[email protected];'); // ok in fpc and delphi
  10115. Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  10116. Add(' vp;'); // ok in fpc and delphi
  10117. Add(' vp();');
  10118. Add(' vp(2);');
  10119. //Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  10120. //Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  10121. //Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  10122. //Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  10123. ConvertProgram;
  10124. CheckSource('TestProcType_MethodDelphi',
  10125. LinesToStr([ // statements
  10126. 'rtl.createClass($mod, "TObject", null, function () {',
  10127. ' this.$init = function () {',
  10128. ' };',
  10129. ' this.$final = function () {',
  10130. ' };',
  10131. ' this.DoIt = function (vA) {',
  10132. ' var Result = 0;',
  10133. ' return Result;',
  10134. ' };',
  10135. '});',
  10136. 'this.Obj = null;',
  10137. 'this.vP = null;',
  10138. 'this.b = false;'
  10139. ]),
  10140. LinesToStr([
  10141. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  10142. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  10143. '$mod.vP(1);',
  10144. '$mod.vP(1);',
  10145. '$mod.vP(2);',
  10146. '']));
  10147. end;
  10148. procedure TTestModule.TestProcType_PropertyFPC;
  10149. begin
  10150. StartProgram(false);
  10151. Add('type');
  10152. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  10153. Add(' TObject = class');
  10154. Add(' FOnFoo: TFuncInt;');
  10155. Add(' function DoIt(vA: longint = 1): longint;');
  10156. Add(' function GetFoo: TFuncInt;');
  10157. Add(' procedure SetFoo(const Value: TFuncInt);');
  10158. Add(' function GetEvents(Index: longint): TFuncInt;');
  10159. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  10160. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  10161. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  10162. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  10163. Add(' end;');
  10164. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  10165. Add('function tobject.getfoo: tfuncint; begin end;');
  10166. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  10167. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  10168. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  10169. Add('var');
  10170. Add(' Obj: TObject;');
  10171. Add(' vP: tfuncint;');
  10172. Add(' b: boolean;');
  10173. Add('begin');
  10174. Add(' obj.onfoo:=nil;');
  10175. Add(' obj.onbar:=nil;');
  10176. Add(' obj.events[1]:=nil;');
  10177. Add(' obj.onfoo:=obj.onfoo;');
  10178. Add(' obj.onbar:=obj.onbar;');
  10179. Add(' obj.events[2]:=obj.events[3];');
  10180. Add(' obj.onfoo:[email protected];');
  10181. Add(' obj.onbar:[email protected];');
  10182. Add(' obj.events[4]:[email protected];');
  10183. //Add(' obj.onfoo:=obj.doit;'); // delphi
  10184. //Add(' obj.onbar:=obj.doit;'); // delphi
  10185. //Add(' obj.events[4]:=obj.doit;'); // delphi
  10186. Add(' obj.onfoo;');
  10187. Add(' obj.onbar;');
  10188. //Add(' obj.events[5];'); ToDo in pasresolver
  10189. Add(' obj.onfoo();');
  10190. Add(' obj.onbar();');
  10191. Add(' obj.events[6]();');
  10192. Add(' b:=obj.onfoo=nil;');
  10193. Add(' b:=obj.onbar=nil;');
  10194. Add(' b:=obj.events[7]=nil;');
  10195. Add(' b:=obj.onfoo<>nil;');
  10196. Add(' b:=obj.onbar<>nil;');
  10197. Add(' b:=obj.events[8]<>nil;');
  10198. Add(' b:=obj.onfoo=vp;');
  10199. Add(' b:=obj.onbar=vp;');
  10200. Add(' b:=obj.events[9]=vp;');
  10201. Add(' b:=obj.onfoo=obj.onfoo;');
  10202. Add(' b:=obj.onbar=obj.onfoo;');
  10203. Add(' b:=obj.events[10]=obj.onfoo;');
  10204. Add(' b:=obj.onfoo<>obj.onfoo;');
  10205. Add(' b:=obj.onbar<>obj.onfoo;');
  10206. Add(' b:=obj.events[11]<>obj.onfoo;');
  10207. Add(' b:[email protected];');
  10208. Add(' b:[email protected];');
  10209. Add(' b:=obj.events[12][email protected];');
  10210. Add(' b:=obj.onfoo<>@obj.doit;');
  10211. Add(' b:=obj.onbar<>@obj.doit;');
  10212. Add(' b:=obj.events[12]<>@obj.doit;');
  10213. Add(' b:=Assigned(obj.onfoo);');
  10214. Add(' b:=Assigned(obj.onbar);');
  10215. Add(' b:=Assigned(obj.events[13]);');
  10216. ConvertProgram;
  10217. CheckSource('TestProcType_PropertyFPC',
  10218. LinesToStr([ // statements
  10219. 'rtl.createClass($mod, "TObject", null, function () {',
  10220. ' this.$init = function () {',
  10221. ' this.FOnFoo = null;',
  10222. ' };',
  10223. ' this.$final = function () {',
  10224. ' this.FOnFoo = undefined;',
  10225. ' };',
  10226. ' this.DoIt = function (vA) {',
  10227. ' var Result = 0;',
  10228. ' return Result;',
  10229. ' };',
  10230. 'this.GetFoo = function () {',
  10231. ' var Result = null;',
  10232. ' return Result;',
  10233. '};',
  10234. 'this.SetFoo = function (Value) {',
  10235. '};',
  10236. 'this.GetEvents = function (Index) {',
  10237. ' var Result = null;',
  10238. ' return Result;',
  10239. '};',
  10240. 'this.SetEvents = function (Index, Value) {',
  10241. '};',
  10242. '});',
  10243. 'this.Obj = null;',
  10244. 'this.vP = null;',
  10245. 'this.b = false;'
  10246. ]),
  10247. LinesToStr([
  10248. '$mod.Obj.FOnFoo = null;',
  10249. '$mod.Obj.SetFoo(null);',
  10250. '$mod.Obj.SetEvents(1, null);',
  10251. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  10252. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  10253. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  10254. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  10255. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  10256. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  10257. '$mod.Obj.FOnFoo(1);',
  10258. '$mod.Obj.GetFoo();',
  10259. '$mod.Obj.FOnFoo(1);',
  10260. '$mod.Obj.GetFoo()(1);',
  10261. '$mod.Obj.GetEvents(6)(1);',
  10262. '$mod.b = $mod.Obj.FOnFoo == null;',
  10263. '$mod.b = $mod.Obj.GetFoo() == null;',
  10264. '$mod.b = $mod.Obj.GetEvents(7) == null;',
  10265. '$mod.b = $mod.Obj.FOnFoo != null;',
  10266. '$mod.b = $mod.Obj.GetFoo() != null;',
  10267. '$mod.b = $mod.Obj.GetEvents(8) != null;',
  10268. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  10269. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  10270. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  10271. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  10272. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  10273. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  10274. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  10275. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  10276. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  10277. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  10278. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  10279. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  10280. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  10281. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  10282. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  10283. '$mod.b = $mod.Obj.FOnFoo != null;',
  10284. '$mod.b = $mod.Obj.GetFoo() != null;',
  10285. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  10286. '']));
  10287. end;
  10288. procedure TTestModule.TestProcType_PropertyDelphi;
  10289. begin
  10290. StartProgram(false);
  10291. Add('{$mode delphi}');
  10292. Add('type');
  10293. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  10294. Add(' TObject = class');
  10295. Add(' FOnFoo: TFuncInt;');
  10296. Add(' function DoIt(vA: longint = 1): longint;');
  10297. Add(' function GetFoo: TFuncInt;');
  10298. Add(' procedure SetFoo(const Value: TFuncInt);');
  10299. Add(' function GetEvents(Index: longint): TFuncInt;');
  10300. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  10301. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  10302. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  10303. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  10304. Add(' end;');
  10305. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  10306. Add('function tobject.getfoo: tfuncint; begin end;');
  10307. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  10308. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  10309. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  10310. Add('var');
  10311. Add(' Obj: TObject;');
  10312. Add(' vP: tfuncint;');
  10313. Add(' b: boolean;');
  10314. Add('begin');
  10315. Add(' obj.onfoo:=nil;');
  10316. Add(' obj.onbar:=nil;');
  10317. Add(' obj.events[1]:=nil;');
  10318. Add(' obj.onfoo:=obj.onfoo;');
  10319. Add(' obj.onbar:=obj.onbar;');
  10320. Add(' obj.events[2]:=obj.events[3];');
  10321. Add(' obj.onfoo:[email protected];');
  10322. Add(' obj.onbar:[email protected];');
  10323. Add(' obj.events[4]:[email protected];');
  10324. Add(' obj.onfoo:=obj.doit;'); // delphi
  10325. Add(' obj.onbar:=obj.doit;'); // delphi
  10326. Add(' obj.events[4]:=obj.doit;'); // delphi
  10327. Add(' obj.onfoo;');
  10328. Add(' obj.onbar;');
  10329. //Add(' obj.events[5];'); ToDo in pasresolver
  10330. Add(' obj.onfoo();');
  10331. Add(' obj.onbar();');
  10332. Add(' obj.events[6]();');
  10333. //Add(' b:=obj.onfoo=nil;'); // fpc
  10334. //Add(' b:=obj.onbar=nil;'); // fpc
  10335. //Add(' b:=obj.events[7]=nil;'); // fpc
  10336. //Add(' b:=obj.onfoo<>nil;'); // fpc
  10337. //Add(' b:=obj.onbar<>nil;'); // fpc
  10338. //Add(' b:=obj.events[8]<>nil;'); // fpc
  10339. Add(' b:=obj.onfoo=vp;');
  10340. Add(' b:=obj.onbar=vp;');
  10341. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  10342. Add(' b:=obj.onfoo=obj.onfoo;');
  10343. Add(' b:=obj.onbar=obj.onfoo;');
  10344. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  10345. Add(' b:=obj.onfoo<>obj.onfoo;');
  10346. Add(' b:=obj.onbar<>obj.onfoo;');
  10347. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  10348. //Add(' b:[email protected];'); // fpc
  10349. //Add(' b:[email protected];'); // fpc
  10350. //Add(' b:=obj.events[12][email protected];'); // fpc
  10351. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  10352. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  10353. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  10354. Add(' b:=Assigned(obj.onfoo);');
  10355. Add(' b:=Assigned(obj.onbar);');
  10356. Add(' b:=Assigned(obj.events[13]);');
  10357. ConvertProgram;
  10358. CheckSource('TestProcType_PropertyDelphi',
  10359. LinesToStr([ // statements
  10360. 'rtl.createClass($mod, "TObject", null, function () {',
  10361. ' this.$init = function () {',
  10362. ' this.FOnFoo = null;',
  10363. ' };',
  10364. ' this.$final = function () {',
  10365. ' this.FOnFoo = undefined;',
  10366. ' };',
  10367. ' this.DoIt = function (vA) {',
  10368. ' var Result = 0;',
  10369. ' return Result;',
  10370. ' };',
  10371. 'this.GetFoo = function () {',
  10372. ' var Result = null;',
  10373. ' return Result;',
  10374. '};',
  10375. 'this.SetFoo = function (Value) {',
  10376. '};',
  10377. 'this.GetEvents = function (Index) {',
  10378. ' var Result = null;',
  10379. ' return Result;',
  10380. '};',
  10381. 'this.SetEvents = function (Index, Value) {',
  10382. '};',
  10383. '});',
  10384. 'this.Obj = null;',
  10385. 'this.vP = null;',
  10386. 'this.b = false;'
  10387. ]),
  10388. LinesToStr([
  10389. '$mod.Obj.FOnFoo = null;',
  10390. '$mod.Obj.SetFoo(null);',
  10391. '$mod.Obj.SetEvents(1, null);',
  10392. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  10393. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  10394. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  10395. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  10396. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  10397. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  10398. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  10399. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  10400. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  10401. '$mod.Obj.FOnFoo(1);',
  10402. '$mod.Obj.GetFoo();',
  10403. '$mod.Obj.FOnFoo(1);',
  10404. '$mod.Obj.GetFoo()(1);',
  10405. '$mod.Obj.GetEvents(6)(1);',
  10406. '$mod.b = $mod.Obj.FOnFoo(1) == $mod.vP(1);',
  10407. '$mod.b = $mod.Obj.GetFoo() == $mod.vP(1);',
  10408. '$mod.b = $mod.Obj.FOnFoo(1) == $mod.Obj.FOnFoo(1);',
  10409. '$mod.b = $mod.Obj.GetFoo() == $mod.Obj.FOnFoo(1);',
  10410. '$mod.b = $mod.Obj.FOnFoo(1) != $mod.Obj.FOnFoo(1);',
  10411. '$mod.b = $mod.Obj.GetFoo() != $mod.Obj.FOnFoo(1);',
  10412. '$mod.b = $mod.Obj.FOnFoo != null;',
  10413. '$mod.b = $mod.Obj.GetFoo() != null;',
  10414. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  10415. '']));
  10416. end;
  10417. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  10418. begin
  10419. StartProgram(false);
  10420. Add('type');
  10421. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  10422. Add(' TObject = class');
  10423. Add(' FOnFoo: TFuncInt;');
  10424. Add(' function DoIt(vA: longint = 1): longint;');
  10425. Add(' function GetFoo: TFuncInt;');
  10426. Add(' procedure SetFoo(const Value: TFuncInt);');
  10427. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  10428. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  10429. Add(' end;');
  10430. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  10431. Add('function tobject.getfoo: tfuncint; begin end;');
  10432. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  10433. Add('var');
  10434. Add(' Obj: TObject;');
  10435. Add(' vP: tfuncint;');
  10436. Add(' b: boolean;');
  10437. Add('begin');
  10438. Add('with obj do begin');
  10439. Add(' fonfoo:=nil;');
  10440. Add(' onfoo:=nil;');
  10441. Add(' onbar:=nil;');
  10442. Add(' fonfoo:=fonfoo;');
  10443. Add(' onfoo:=onfoo;');
  10444. Add(' onbar:=onbar;');
  10445. Add(' fonfoo:=@doit;');
  10446. Add(' onfoo:=@doit;');
  10447. Add(' onbar:=@doit;');
  10448. //Add(' fonfoo:=doit;'); // delphi
  10449. //Add(' onfoo:=doit;'); // delphi
  10450. //Add(' onbar:=doit;'); // delphi
  10451. Add(' fonfoo;');
  10452. Add(' onfoo;');
  10453. Add(' onbar;');
  10454. Add(' fonfoo();');
  10455. Add(' onfoo();');
  10456. Add(' onbar();');
  10457. Add(' b:=fonfoo=nil;');
  10458. Add(' b:=onfoo=nil;');
  10459. Add(' b:=onbar=nil;');
  10460. Add(' b:=fonfoo<>nil;');
  10461. Add(' b:=onfoo<>nil;');
  10462. Add(' b:=onbar<>nil;');
  10463. Add(' b:=fonfoo=vp;');
  10464. Add(' b:=onfoo=vp;');
  10465. Add(' b:=onbar=vp;');
  10466. Add(' b:=fonfoo=fonfoo;');
  10467. Add(' b:=onfoo=onfoo;');
  10468. Add(' b:=onbar=onfoo;');
  10469. Add(' b:=fonfoo<>fonfoo;');
  10470. Add(' b:=onfoo<>onfoo;');
  10471. Add(' b:=onbar<>onfoo;');
  10472. Add(' b:=fonfoo=@doit;');
  10473. Add(' b:=onfoo=@doit;');
  10474. Add(' b:=onbar=@doit;');
  10475. Add(' b:=fonfoo<>@doit;');
  10476. Add(' b:=onfoo<>@doit;');
  10477. Add(' b:=onbar<>@doit;');
  10478. Add(' b:=Assigned(fonfoo);');
  10479. Add(' b:=Assigned(onfoo);');
  10480. Add(' b:=Assigned(onbar);');
  10481. Add('end;');
  10482. ConvertProgram;
  10483. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  10484. LinesToStr([ // statements
  10485. 'rtl.createClass($mod, "TObject", null, function () {',
  10486. ' this.$init = function () {',
  10487. ' this.FOnFoo = null;',
  10488. ' };',
  10489. ' this.$final = function () {',
  10490. ' this.FOnFoo = undefined;',
  10491. ' };',
  10492. ' this.DoIt = function (vA) {',
  10493. ' var Result = 0;',
  10494. ' return Result;',
  10495. ' };',
  10496. ' this.GetFoo = function () {',
  10497. ' var Result = null;',
  10498. ' return Result;',
  10499. ' };',
  10500. ' this.SetFoo = function (Value) {',
  10501. ' };',
  10502. '});',
  10503. 'this.Obj = null;',
  10504. 'this.vP = null;',
  10505. 'this.b = false;'
  10506. ]),
  10507. LinesToStr([
  10508. 'var $with1 = $mod.Obj;',
  10509. '$with1.FOnFoo = null;',
  10510. '$with1.FOnFoo = null;',
  10511. '$with1.SetFoo(null);',
  10512. '$with1.FOnFoo = $with1.FOnFoo;',
  10513. '$with1.FOnFoo = $with1.FOnFoo;',
  10514. '$with1.SetFoo($with1.GetFoo());',
  10515. '$with1.FOnFoo = rtl.createCallback($with1, "DoIt");',
  10516. '$with1.FOnFoo = rtl.createCallback($with1, "DoIt");',
  10517. '$with1.SetFoo(rtl.createCallback($with1, "DoIt"));',
  10518. '$with1.FOnFoo(1);',
  10519. '$with1.FOnFoo(1);',
  10520. '$with1.GetFoo();',
  10521. '$with1.FOnFoo(1);',
  10522. '$with1.FOnFoo(1);',
  10523. '$with1.GetFoo()(1);',
  10524. '$mod.b = $with1.FOnFoo == null;',
  10525. '$mod.b = $with1.FOnFoo == null;',
  10526. '$mod.b = $with1.GetFoo() == null;',
  10527. '$mod.b = $with1.FOnFoo != null;',
  10528. '$mod.b = $with1.FOnFoo != null;',
  10529. '$mod.b = $with1.GetFoo() != null;',
  10530. '$mod.b = rtl.eqCallback($with1.FOnFoo, $mod.vP);',
  10531. '$mod.b = rtl.eqCallback($with1.FOnFoo, $mod.vP);',
  10532. '$mod.b = rtl.eqCallback($with1.GetFoo(), $mod.vP);',
  10533. '$mod.b = rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  10534. '$mod.b = rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  10535. '$mod.b = rtl.eqCallback($with1.GetFoo(), $with1.FOnFoo);',
  10536. '$mod.b = !rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  10537. '$mod.b = !rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
  10538. '$mod.b = !rtl.eqCallback($with1.GetFoo(), $with1.FOnFoo);',
  10539. '$mod.b = rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  10540. '$mod.b = rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  10541. '$mod.b = rtl.eqCallback($with1.GetFoo(), rtl.createCallback($with1, "DoIt"));',
  10542. '$mod.b = !rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  10543. '$mod.b = !rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
  10544. '$mod.b = !rtl.eqCallback($with1.GetFoo(), rtl.createCallback($with1, "DoIt"));',
  10545. '$mod.b = $with1.FOnFoo != null;',
  10546. '$mod.b = $with1.FOnFoo != null;',
  10547. '$mod.b = $with1.GetFoo() != null;',
  10548. '']));
  10549. end;
  10550. procedure TTestModule.TestProcType_Nested;
  10551. begin
  10552. StartProgram(false);
  10553. Add([
  10554. 'type',
  10555. ' TProcInt = procedure(vI: longint = 1);',
  10556. 'procedure DoIt(vJ: longint);',
  10557. 'var aProc: TProcInt;',
  10558. ' b: boolean;',
  10559. ' procedure Sub(vK: longint);',
  10560. ' var aSub: TProcInt;',
  10561. ' procedure SubSub(vK: longint);',
  10562. ' var aSubSub: TProcInt;',
  10563. ' begin;',
  10564. ' aProc:=@DoIt;',
  10565. ' aSub:=@DoIt;',
  10566. ' aSubSub:=@DoIt;',
  10567. ' aProc:=@Sub;',
  10568. ' aSub:=@Sub;',
  10569. ' aSubSub:=@Sub;',
  10570. ' aProc:=@SubSub;',
  10571. ' aSub:=@SubSub;',
  10572. ' aSubSub:=@SubSub;',
  10573. ' end;',
  10574. ' begin;',
  10575. ' end;',
  10576. 'begin;',
  10577. ' aProc:=@Sub;',
  10578. ' b:=aProc=@Sub;',
  10579. ' b:=@Sub=aProc;',
  10580. 'end;',
  10581. 'begin',
  10582. '']);
  10583. ConvertProgram;
  10584. CheckSource('TestProcType_Nested',
  10585. LinesToStr([ // statements
  10586. 'this.DoIt = function (vJ) {',
  10587. ' var aProc = null;',
  10588. ' var b = false;',
  10589. ' function Sub(vK) {',
  10590. ' var aSub = null;',
  10591. ' function SubSub(vK) {',
  10592. ' var aSubSub = null;',
  10593. ' aProc = $mod.DoIt;',
  10594. ' aSub = $mod.DoIt;',
  10595. ' aSubSub = $mod.DoIt;',
  10596. ' aProc = Sub;',
  10597. ' aSub = Sub;',
  10598. ' aSubSub = Sub;',
  10599. ' aProc = SubSub;',
  10600. ' aSub = SubSub;',
  10601. ' aSubSub = SubSub;',
  10602. ' };',
  10603. ' };',
  10604. ' aProc = Sub;',
  10605. ' b = rtl.eqCallback(aProc, Sub);',
  10606. ' b = rtl.eqCallback(Sub, aProc);',
  10607. '};',
  10608. '']),
  10609. LinesToStr([ // $mod.$main
  10610. '']));
  10611. end;
  10612. procedure TTestModule.TestProcType_NestedOfObject;
  10613. begin
  10614. StartProgram(false);
  10615. Add([
  10616. 'type',
  10617. ' TProcInt = procedure(vI: longint = 1) of object;',
  10618. ' TObject = class',
  10619. ' procedure DoIt(vJ: longint);',
  10620. ' end;',
  10621. 'procedure TObject.DoIt(vJ: longint);',
  10622. 'var aProc: TProcInt;',
  10623. ' b: boolean;',
  10624. ' procedure Sub(vK: longint);',
  10625. ' var aSub: TProcInt;',
  10626. ' procedure SubSub(vK: longint);',
  10627. ' var aSubSub: TProcInt;',
  10628. ' begin;',
  10629. ' aProc:=@DoIt;',
  10630. ' aSub:=@DoIt;',
  10631. ' aSubSub:=@DoIt;',
  10632. ' aProc:=@Sub;',
  10633. ' aSub:=@Sub;',
  10634. ' aSubSub:=@Sub;',
  10635. ' aProc:=@SubSub;',
  10636. ' aSub:=@SubSub;',
  10637. ' aSubSub:=@SubSub;',
  10638. ' end;',
  10639. ' begin;',
  10640. ' end;',
  10641. 'begin;',
  10642. ' aProc:=@Sub;',
  10643. ' b:=aProc=@Sub;',
  10644. ' b:=@Sub=aProc;',
  10645. 'end;',
  10646. 'begin',
  10647. '']);
  10648. ConvertProgram;
  10649. CheckSource('TestProcType_Nested',
  10650. LinesToStr([ // statements
  10651. 'rtl.createClass($mod, "TObject", null, function () {',
  10652. ' this.$init = function () {',
  10653. ' };',
  10654. ' this.$final = function () {',
  10655. ' };',
  10656. ' this.DoIt = function (vJ) {',
  10657. ' var Self = this;',
  10658. ' var aProc = null;',
  10659. ' var b = false;',
  10660. ' function Sub(vK) {',
  10661. ' var aSub = null;',
  10662. ' function SubSub(vK) {',
  10663. ' var aSubSub = null;',
  10664. ' aProc = rtl.createCallback(Self, "DoIt");',
  10665. ' aSub = rtl.createCallback(Self, "DoIt");',
  10666. ' aSubSub = rtl.createCallback(Self, "DoIt");',
  10667. ' aProc = Sub;',
  10668. ' aSub = Sub;',
  10669. ' aSubSub = Sub;',
  10670. ' aProc = SubSub;',
  10671. ' aSub = SubSub;',
  10672. ' aSubSub = SubSub;',
  10673. ' };',
  10674. ' };',
  10675. ' aProc = Sub;',
  10676. ' b = rtl.eqCallback(aProc, Sub);',
  10677. ' b = rtl.eqCallback(Sub, aProc);',
  10678. ' };',
  10679. '});',
  10680. '']),
  10681. LinesToStr([ // $mod.$main
  10682. '']));
  10683. end;
  10684. procedure TTestModule.TestProcType_ReferenceToProc;
  10685. begin
  10686. StartProgram(false);
  10687. Add([
  10688. 'type',
  10689. ' TProcRef = reference to procedure(i: longint = 0);',
  10690. ' TFuncRef = reference to function(i: longint = 0): longint;',
  10691. 'var',
  10692. ' p: TProcRef;',
  10693. ' f: TFuncRef;',
  10694. 'procedure DoIt(i: longint);',
  10695. 'begin',
  10696. 'end;',
  10697. 'function GetIt(i: longint): longint;',
  10698. 'begin',
  10699. ' p:=@DoIt;',
  10700. ' f:=@GetIt;',
  10701. ' f;',
  10702. ' f();',
  10703. ' f(1);',
  10704. 'end;',
  10705. 'begin',
  10706. ' p:=@DoIt;',
  10707. ' f:=@GetIt;',
  10708. ' f;',
  10709. ' f();',
  10710. ' f(1);',
  10711. ' p:=TProcRef(f);',
  10712. '']);
  10713. ConvertProgram;
  10714. CheckSource('TestProcType_ReferenceToProc',
  10715. LinesToStr([ // statements
  10716. 'this.p = null;',
  10717. 'this.f = null;',
  10718. 'this.DoIt = function (i) {',
  10719. '};',
  10720. 'this.GetIt = function (i) {',
  10721. ' var Result = 0;',
  10722. ' $mod.p = $mod.DoIt;',
  10723. ' $mod.f = $mod.GetIt;',
  10724. ' $mod.f(0);',
  10725. ' $mod.f(0);',
  10726. ' $mod.f(1);',
  10727. ' return Result;',
  10728. '};',
  10729. '']),
  10730. LinesToStr([ // $mod.$main
  10731. '$mod.p = $mod.DoIt;',
  10732. '$mod.f = $mod.GetIt;',
  10733. '$mod.f(0);',
  10734. '$mod.f(0);',
  10735. '$mod.f(1);',
  10736. '$mod.p = $mod.f;',
  10737. '']));
  10738. end;
  10739. procedure TTestModule.TestProcType_ReferenceToMethod;
  10740. begin
  10741. StartProgram(false);
  10742. Add([
  10743. 'type',
  10744. ' TFuncRef = reference to function(i: longint = 5): longint;',
  10745. ' TObject = class',
  10746. ' function Grow(s: longint): longint;',
  10747. ' end;',
  10748. 'var',
  10749. ' f: tfuncref;',
  10750. 'function tobject.grow(s: longint): longint;',
  10751. ' function GrowSub(i: longint): longint;',
  10752. ' begin',
  10753. ' f:=@grow;',
  10754. ' f:=@growsub;',
  10755. ' end;',
  10756. 'begin',
  10757. ' f:=@grow;',
  10758. ' f:=@growsub;',
  10759. 'end;',
  10760. 'begin',
  10761. '']);
  10762. ConvertProgram;
  10763. CheckSource('TestProcType_ReferenceToMethod',
  10764. LinesToStr([ // statements
  10765. 'rtl.createClass($mod, "TObject", null, function () {',
  10766. ' this.$init = function () {',
  10767. ' };',
  10768. ' this.$final = function () {',
  10769. ' };',
  10770. ' this.Grow = function (s) {',
  10771. ' var Self = this;',
  10772. ' var Result = 0;',
  10773. ' function GrowSub(i) {',
  10774. ' var Result = 0;',
  10775. ' $mod.f = rtl.createCallback(Self, "Grow");',
  10776. ' $mod.f = GrowSub;',
  10777. ' return Result;',
  10778. ' };',
  10779. ' $mod.f = rtl.createCallback(Self, "Grow");',
  10780. ' $mod.f = GrowSub;',
  10781. ' return Result;',
  10782. ' };',
  10783. '});',
  10784. 'this.f = null;',
  10785. '']),
  10786. LinesToStr([ // $mod.$main
  10787. '']));
  10788. end;
  10789. procedure TTestModule.TestProcType_Typecast;
  10790. begin
  10791. StartProgram(false);
  10792. Add([
  10793. 'type',
  10794. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  10795. ' TEvent = procedure of object;',
  10796. ' TGetter = function:longint of object;',
  10797. ' TProcA = procedure(i: longint);',
  10798. ' TFuncB = function(i, j: longint): longint;',
  10799. 'procedure DoIt(); varargs; begin end;',
  10800. 'var',
  10801. ' Notify: tnotifyevent;',
  10802. ' Event: tevent;',
  10803. ' Getter: tgetter;',
  10804. ' ProcA: tproca;',
  10805. ' FuncB: tfuncb;',
  10806. ' p: pointer;',
  10807. 'begin',
  10808. ' notify:=tnotifyevent(event);',
  10809. ' event:=tevent(event);',
  10810. ' event:=tevent(notify);',
  10811. ' event:=tevent(getter);',
  10812. ' event:=tevent(proca);',
  10813. ' proca:=tproca(funcb);',
  10814. ' funcb:=tfuncb(funcb);',
  10815. ' funcb:=tfuncb(proca);',
  10816. ' funcb:=tfuncb(getter);',
  10817. ' proca:=tproca(p);',
  10818. ' funcb:=tfuncb(p);',
  10819. ' getter:=tgetter(p);',
  10820. ' p:=pointer(notify);',
  10821. ' p:=notify;',
  10822. ' p:=pointer(proca);',
  10823. ' p:=proca;',
  10824. ' p:=pointer(funcb);',
  10825. ' p:=funcb;',
  10826. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  10827. '']);
  10828. ConvertProgram;
  10829. CheckSource('TestProcType_Typecast',
  10830. LinesToStr([ // statements
  10831. 'this.DoIt = function () {',
  10832. '};',
  10833. 'this.Notify = null;',
  10834. 'this.Event = null;',
  10835. 'this.Getter = null;',
  10836. 'this.ProcA = null;',
  10837. 'this.FuncB = null;',
  10838. 'this.p = null;',
  10839. '']),
  10840. LinesToStr([ // $mod.$main
  10841. '$mod.Notify = $mod.Event;',
  10842. '$mod.Event = $mod.Event;',
  10843. '$mod.Event = $mod.Notify;',
  10844. '$mod.Event = $mod.Getter;',
  10845. '$mod.Event = $mod.ProcA;',
  10846. '$mod.ProcA = $mod.FuncB;',
  10847. '$mod.FuncB = $mod.FuncB;',
  10848. '$mod.FuncB = $mod.ProcA;',
  10849. '$mod.FuncB = $mod.Getter;',
  10850. '$mod.ProcA = $mod.p;',
  10851. '$mod.FuncB = $mod.p;',
  10852. '$mod.Getter = $mod.p;',
  10853. '$mod.p = $mod.Notify;',
  10854. '$mod.p = $mod.Notify;',
  10855. '$mod.p = $mod.ProcA;',
  10856. '$mod.p = $mod.ProcA;',
  10857. '$mod.p = $mod.FuncB;',
  10858. '$mod.p = $mod.FuncB;',
  10859. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  10860. '']));
  10861. end;
  10862. procedure TTestModule.TestProcType_PassProcToUntyped;
  10863. begin
  10864. StartProgram(false);
  10865. Add([
  10866. 'type',
  10867. ' TEvent = procedure of object;',
  10868. ' TFunc = function: longint;',
  10869. 'procedure DoIt(); varargs; begin end;',
  10870. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  10871. 'var',
  10872. ' Event: tevent;',
  10873. ' Func: TFunc;',
  10874. 'begin',
  10875. ' doit(event,func);',
  10876. ' dosome(event,event,event);',
  10877. ' dosome(func,func,func);',
  10878. '']);
  10879. ConvertProgram;
  10880. CheckSource('TestProcType_PassProcToUntyped',
  10881. LinesToStr([ // statements
  10882. 'this.DoIt = function () {',
  10883. '};',
  10884. 'this.DoSome = function (a, b, p) {',
  10885. '};',
  10886. 'this.Event = null;',
  10887. 'this.Func = null;',
  10888. '']),
  10889. LinesToStr([ // $mod.$main
  10890. '$mod.DoIt($mod.Event, $mod.Func);',
  10891. '$mod.DoSome($mod.Event, {',
  10892. ' p: $mod,',
  10893. ' get: function () {',
  10894. ' return this.p.Event;',
  10895. ' },',
  10896. ' set: function (v) {',
  10897. ' this.p.Event = v;',
  10898. ' }',
  10899. '}, $mod.Event);',
  10900. '$mod.DoSome($mod.Func, {',
  10901. ' p: $mod,',
  10902. ' get: function () {',
  10903. ' return this.p.Func;',
  10904. ' },',
  10905. ' set: function (v) {',
  10906. ' this.p.Func = v;',
  10907. ' }',
  10908. '}, $mod.Func);',
  10909. '']));
  10910. end;
  10911. procedure TTestModule.TestPointer;
  10912. begin
  10913. StartProgram(false);
  10914. Add('type');
  10915. Add(' TObject = class end;');
  10916. Add(' TClass = class of TObject;');
  10917. Add(' TArrInt = array of longint;');
  10918. Add('var');
  10919. Add(' v: jsvalue;');
  10920. Add(' Obj: tobject;');
  10921. Add(' C: tclass;');
  10922. Add(' a: tarrint;');
  10923. Add(' p: Pointer;');
  10924. Add('begin');
  10925. Add(' p:=p;');
  10926. Add(' p:=nil;');
  10927. Add(' if p=nil then;');
  10928. Add(' if nil=p then;');
  10929. Add(' if Assigned(p) then;');
  10930. Add(' p:=Pointer(v);');
  10931. Add(' p:=obj;');
  10932. Add(' p:=c;');
  10933. Add(' p:=a;');
  10934. Add(' p:=tobject;');
  10935. Add(' obj:=TObject(p);');
  10936. Add(' c:=TClass(p);');
  10937. Add(' a:=TArrInt(p);');
  10938. ConvertProgram;
  10939. CheckSource('TestPointer',
  10940. LinesToStr([ // statements
  10941. 'rtl.createClass($mod, "TObject", null, function () {',
  10942. ' this.$init = function () {',
  10943. ' };',
  10944. ' this.$final = function () {',
  10945. ' };',
  10946. '});',
  10947. 'this.v = undefined;',
  10948. 'this.Obj = null;',
  10949. 'this.C = null;',
  10950. 'this.a = [];',
  10951. 'this.p = null;',
  10952. '']),
  10953. LinesToStr([ // $mod.$main
  10954. '$mod.p = $mod.p;',
  10955. '$mod.p = null;',
  10956. 'if ($mod.p == null) ;',
  10957. 'if (null == $mod.p) ;',
  10958. 'if ($mod.p != null) ;',
  10959. '$mod.p = $mod.v;',
  10960. '$mod.p = $mod.Obj;',
  10961. '$mod.p = $mod.C;',
  10962. '$mod.p = $mod.a;',
  10963. '$mod.p = $mod.TObject;',
  10964. '$mod.Obj = $mod.p;',
  10965. '$mod.C = $mod.p;',
  10966. '$mod.a = $mod.p;',
  10967. '']));
  10968. end;
  10969. procedure TTestModule.TestPointer_Proc;
  10970. begin
  10971. StartProgram(false);
  10972. Add('type');
  10973. Add(' TObject = class');
  10974. Add(' procedure DoIt; virtual; abstract;');
  10975. Add(' end;');
  10976. Add('procedure DoSome; begin end;');
  10977. Add('var');
  10978. Add(' o: TObject;');
  10979. Add(' p: Pointer;');
  10980. Add('begin');
  10981. Add(' p:=@DoSome;');
  10982. Add(' p:[email protected];');
  10983. ConvertProgram;
  10984. CheckSource('TestPointer_Proc',
  10985. LinesToStr([ // statements
  10986. 'rtl.createClass($mod, "TObject", null, function () {',
  10987. ' this.$init = function () {',
  10988. ' };',
  10989. ' this.$final = function () {',
  10990. ' };',
  10991. '});',
  10992. 'this.DoSome = function () {',
  10993. '};',
  10994. 'this.o = null;',
  10995. 'this.p = null;',
  10996. '']),
  10997. LinesToStr([ // $mod.$main
  10998. '$mod.p = $mod.DoSome;',
  10999. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  11000. '']));
  11001. end;
  11002. procedure TTestModule.TestPointer_AssignRecordFail;
  11003. begin
  11004. StartProgram(false);
  11005. Add('type');
  11006. Add(' TRec = record end;');
  11007. Add('var');
  11008. Add(' p: Pointer;');
  11009. Add(' r: TRec;');
  11010. Add('begin');
  11011. Add(' p:=r;');
  11012. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  11013. nIncompatibleTypesGotExpected);
  11014. ConvertProgram;
  11015. end;
  11016. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  11017. begin
  11018. StartProgram(false);
  11019. Add('type');
  11020. Add(' TArr = array[boolean] of longint;');
  11021. Add('var');
  11022. Add(' p: Pointer;');
  11023. Add(' a: TArr;');
  11024. Add('begin');
  11025. Add(' p:=a;');
  11026. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  11027. nIncompatibleTypesGotExpected);
  11028. ConvertProgram;
  11029. end;
  11030. procedure TTestModule.TestPointer_ArrayParamsFail;
  11031. begin
  11032. StartProgram(false);
  11033. Add('var');
  11034. Add(' p: Pointer;');
  11035. Add('begin');
  11036. Add(' p:=p[1];');
  11037. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  11038. ConvertProgram;
  11039. end;
  11040. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  11041. begin
  11042. StartProgram(false);
  11043. Add([
  11044. 'procedure DoIt(args: array of jsvalue); begin end;',
  11045. 'procedure DoAll; varargs; begin end;',
  11046. 'var',
  11047. ' v: jsvalue;',
  11048. 'begin',
  11049. ' DoIt([pointer(v)]);',
  11050. ' DoAll(pointer(v));',
  11051. '']);
  11052. ConvertProgram;
  11053. CheckSource('TestPointer_TypeCastJSValueToPointer',
  11054. LinesToStr([ // statements
  11055. 'this.DoIt = function (args) {',
  11056. '};',
  11057. 'this.DoAll = function () {',
  11058. '};',
  11059. 'this.v = undefined;',
  11060. '']),
  11061. LinesToStr([ // $mod.$main
  11062. '$mod.DoIt([$mod.v]);',
  11063. '$mod.DoAll($mod.v);',
  11064. '']));
  11065. end;
  11066. procedure TTestModule.TestJSValue_AssignToJSValue;
  11067. begin
  11068. StartProgram(false);
  11069. Add('var');
  11070. Add(' v: jsvalue;');
  11071. Add(' i: longint;');
  11072. Add(' s: string;');
  11073. Add(' b: boolean;');
  11074. Add(' d: double;');
  11075. Add(' p: pointer;');
  11076. Add('begin');
  11077. Add(' v:=v;');
  11078. Add(' v:=1;');
  11079. Add(' v:=i;');
  11080. Add(' v:='''';');
  11081. Add(' v:=''c'';');
  11082. Add(' v:=''foo'';');
  11083. Add(' v:=s;');
  11084. Add(' v:=false;');
  11085. Add(' v:=true;');
  11086. Add(' v:=b;');
  11087. Add(' v:=0.1;');
  11088. Add(' v:=d;');
  11089. Add(' v:=nil;');
  11090. Add(' v:=p;');
  11091. ConvertProgram;
  11092. CheckSource('TestJSValue_AssignToJSValue',
  11093. LinesToStr([ // statements
  11094. 'this.v = undefined;',
  11095. 'this.i = 0;',
  11096. 'this.s = "";',
  11097. 'this.b = false;',
  11098. 'this.d = 0.0;',
  11099. 'this.p = null;',
  11100. '']),
  11101. LinesToStr([ // $mod.$main
  11102. '$mod.v = $mod.v;',
  11103. '$mod.v = 1;',
  11104. '$mod.v = $mod.i;',
  11105. '$mod.v = "";',
  11106. '$mod.v = "c";',
  11107. '$mod.v = "foo";',
  11108. '$mod.v = $mod.s;',
  11109. '$mod.v = false;',
  11110. '$mod.v = true;',
  11111. '$mod.v = $mod.b;',
  11112. '$mod.v = 0.1;',
  11113. '$mod.v = $mod.d;',
  11114. '$mod.v = null;',
  11115. '$mod.v = $mod.p;',
  11116. '']));
  11117. end;
  11118. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  11119. begin
  11120. StartProgram(false);
  11121. Add('type');
  11122. Add(' integer = longint;');
  11123. Add(' TYesNo = boolean;');
  11124. Add(' TFloat = double;');
  11125. Add(' TCaption = string;');
  11126. Add(' TChar = char;');
  11127. Add('var');
  11128. Add(' v: jsvalue;');
  11129. Add(' i: integer;');
  11130. Add(' s: TCaption;');
  11131. Add(' b: TYesNo;');
  11132. Add(' d: TFloat;');
  11133. Add(' c: char;');
  11134. Add('begin');
  11135. Add(' i:=longint(v);');
  11136. Add(' i:=integer(v);');
  11137. Add(' s:=string(v);');
  11138. Add(' s:=TCaption(v);');
  11139. Add(' b:=boolean(v);');
  11140. Add(' b:=TYesNo(v);');
  11141. Add(' d:=double(v);');
  11142. Add(' d:=TFloat(v);');
  11143. Add(' c:=char(v);');
  11144. Add(' c:=TChar(v);');
  11145. ConvertProgram;
  11146. CheckSource('TestJSValue_TypeCastToBaseType',
  11147. LinesToStr([ // statements
  11148. 'this.v = undefined;',
  11149. 'this.i = 0;',
  11150. 'this.s = "";',
  11151. 'this.b = false;',
  11152. 'this.d = 0.0;',
  11153. 'this.c = "";',
  11154. '']),
  11155. LinesToStr([ // $mod.$main
  11156. '$mod.i = Math.floor($mod.v);',
  11157. '$mod.i = Math.floor($mod.v);',
  11158. '$mod.s = "" + $mod.v;',
  11159. '$mod.s = "" + $mod.v;',
  11160. '$mod.b = !($mod.v == false);',
  11161. '$mod.b = !($mod.v == false);',
  11162. '$mod.d = rtl.getNumber($mod.v);',
  11163. '$mod.d = rtl.getNumber($mod.v);',
  11164. '$mod.c = rtl.getChar($mod.v);',
  11165. '$mod.c = rtl.getChar($mod.v);',
  11166. '']));
  11167. end;
  11168. procedure TTestModule.TestJSValue_Equal;
  11169. begin
  11170. StartProgram(false);
  11171. Add('type');
  11172. Add(' integer = longint;');
  11173. Add(' TYesNo = boolean;');
  11174. Add(' TFloat = double;');
  11175. Add(' TCaption = string;');
  11176. Add(' TChar = char;');
  11177. Add(' TMulti = JSValue;');
  11178. Add('var');
  11179. Add(' v: jsvalue;');
  11180. Add(' i: integer;');
  11181. Add(' s: TCaption;');
  11182. Add(' b: TYesNo;');
  11183. Add(' d: TFloat;');
  11184. Add(' c: char;');
  11185. Add(' m: TMulti;');
  11186. Add('begin');
  11187. Add(' b:=v=v;');
  11188. Add(' b:=v<>v;');
  11189. Add(' b:=v=1;');
  11190. Add(' b:=v<>1;');
  11191. Add(' b:=2=v;');
  11192. Add(' b:=2<>v;');
  11193. Add(' b:=v=i;');
  11194. Add(' b:=i=v;');
  11195. Add(' b:=v=nil;');
  11196. Add(' b:=nil=v;');
  11197. Add(' b:=v=false;');
  11198. Add(' b:=true=v;');
  11199. Add(' b:=v=b;');
  11200. Add(' b:=b=v;');
  11201. Add(' b:=v=s;');
  11202. Add(' b:=s=v;');
  11203. Add(' b:=v=''foo'';');
  11204. Add(' b:=''''=v;');
  11205. Add(' b:=v=d;');
  11206. Add(' b:=d=v;');
  11207. Add(' b:=v=3.4;');
  11208. Add(' b:=5.6=v;');
  11209. Add(' b:=v=c;');
  11210. Add(' b:=c=v;');
  11211. Add(' b:=m=m;');
  11212. Add(' b:=v=m;');
  11213. Add(' b:=m=v;');
  11214. ConvertProgram;
  11215. CheckSource('TestJSValue_Equal',
  11216. LinesToStr([ // statements
  11217. 'this.v = undefined;',
  11218. 'this.i = 0;',
  11219. 'this.s = "";',
  11220. 'this.b = false;',
  11221. 'this.d = 0.0;',
  11222. 'this.c = "";',
  11223. 'this.m = undefined;',
  11224. '']),
  11225. LinesToStr([ // $mod.$main
  11226. '$mod.b = $mod.v == $mod.v;',
  11227. '$mod.b = $mod.v != $mod.v;',
  11228. '$mod.b = $mod.v == 1;',
  11229. '$mod.b = $mod.v != 1;',
  11230. '$mod.b = 2 == $mod.v;',
  11231. '$mod.b = 2 != $mod.v;',
  11232. '$mod.b = $mod.v == $mod.i;',
  11233. '$mod.b = $mod.i == $mod.v;',
  11234. '$mod.b = $mod.v == null;',
  11235. '$mod.b = null == $mod.v;',
  11236. '$mod.b = $mod.v == false;',
  11237. '$mod.b = true == $mod.v;',
  11238. '$mod.b = $mod.v == $mod.b;',
  11239. '$mod.b = $mod.b == $mod.v;',
  11240. '$mod.b = $mod.v == $mod.s;',
  11241. '$mod.b = $mod.s == $mod.v;',
  11242. '$mod.b = $mod.v == "foo";',
  11243. '$mod.b = "" == $mod.v;',
  11244. '$mod.b = $mod.v == $mod.d;',
  11245. '$mod.b = $mod.d == $mod.v;',
  11246. '$mod.b = $mod.v == 3.4;',
  11247. '$mod.b = 5.6 == $mod.v;',
  11248. '$mod.b = $mod.v == $mod.c;',
  11249. '$mod.b = $mod.c == $mod.v;',
  11250. '$mod.b = $mod.m == $mod.m;',
  11251. '$mod.b = $mod.v == $mod.m;',
  11252. '$mod.b = $mod.m == $mod.v;',
  11253. '']));
  11254. end;
  11255. procedure TTestModule.TestJSValue_If;
  11256. begin
  11257. StartProgram(false);
  11258. Add([
  11259. 'var',
  11260. ' v: jsvalue;',
  11261. 'begin',
  11262. ' if v then ;',
  11263. ' while v do ;',
  11264. ' repeat until v;',
  11265. '']);
  11266. ConvertProgram;
  11267. CheckSource('TestJSValue_If',
  11268. LinesToStr([ // statements
  11269. 'this.v = undefined;',
  11270. '']),
  11271. LinesToStr([ // $mod.$main
  11272. 'if ($mod.v) ;',
  11273. 'while($mod.v){',
  11274. '};',
  11275. 'do{',
  11276. '} while(!$mod.v);',
  11277. '']));
  11278. end;
  11279. procedure TTestModule.TestJSValue_Enum;
  11280. begin
  11281. StartProgram(false);
  11282. Add('type');
  11283. Add(' TColor = (red, blue);');
  11284. Add(' TRedBlue = TColor;');
  11285. Add('var');
  11286. Add(' v: jsvalue;');
  11287. Add(' e: TColor;');
  11288. Add('begin');
  11289. Add(' v:=e;');
  11290. Add(' v:=TColor(e);');
  11291. Add(' v:=TRedBlue(e);');
  11292. Add(' e:=TColor(v);');
  11293. Add(' e:=TRedBlue(v);');
  11294. ConvertProgram;
  11295. CheckSource('TestJSValue_Enum',
  11296. LinesToStr([ // statements
  11297. 'this.TColor = {',
  11298. ' "0": "red",',
  11299. ' red: 0,',
  11300. ' "1": "blue",',
  11301. ' blue: 1',
  11302. '};',
  11303. 'this.v = undefined;',
  11304. 'this.e = 0;',
  11305. '']),
  11306. LinesToStr([ // $mod.$main
  11307. '$mod.v = $mod.e;',
  11308. '$mod.v = $mod.e;',
  11309. '$mod.v = $mod.e;',
  11310. '$mod.e = $mod.v;',
  11311. '$mod.e = $mod.v;',
  11312. '']));
  11313. end;
  11314. procedure TTestModule.TestJSValue_ClassInstance;
  11315. begin
  11316. StartProgram(false);
  11317. Add('type');
  11318. Add(' TObject = class');
  11319. Add(' end;');
  11320. Add(' TBirdObject = TObject;');
  11321. Add('var');
  11322. Add(' v: jsvalue;');
  11323. Add(' o: TObject;');
  11324. Add('begin');
  11325. Add(' v:=o;');
  11326. Add(' v:=TObject(o);');
  11327. Add(' v:=TBirdObject(o);');
  11328. Add(' o:=TObject(v);');
  11329. Add(' o:=TBirdObject(v);');
  11330. ConvertProgram;
  11331. CheckSource('TestJSValue_ClassInstance',
  11332. LinesToStr([ // statements
  11333. 'rtl.createClass($mod, "TObject", null, function () {',
  11334. ' this.$init = function () {',
  11335. ' };',
  11336. ' this.$final = function () {',
  11337. ' };',
  11338. '});',
  11339. 'this.v = undefined;',
  11340. 'this.o = null;',
  11341. '']),
  11342. LinesToStr([ // $mod.$main
  11343. '$mod.v = $mod.o;',
  11344. '$mod.v = $mod.o;',
  11345. '$mod.v = $mod.o;',
  11346. '$mod.o = rtl.getObject($mod.v);',
  11347. '$mod.o = rtl.getObject($mod.v);',
  11348. '']));
  11349. end;
  11350. procedure TTestModule.TestJSValue_ClassOf;
  11351. begin
  11352. StartProgram(false);
  11353. Add('type');
  11354. Add(' TClass = class of TObject;');
  11355. Add(' TObject = class');
  11356. Add(' end;');
  11357. Add(' TBirds = class of TBird;');
  11358. Add(' TBird = class(TObject) end;');
  11359. Add('var');
  11360. Add(' v: jsvalue;');
  11361. Add(' c: TClass;');
  11362. Add('begin');
  11363. Add(' v:=c;');
  11364. Add(' v:=TObject;');
  11365. Add(' v:=TClass(c);');
  11366. Add(' v:=TBirds(c);');
  11367. Add(' c:=TClass(v);');
  11368. Add(' c:=TBirds(v);');
  11369. ConvertProgram;
  11370. CheckSource('TestJSValue_ClassOf',
  11371. LinesToStr([ // statements
  11372. 'rtl.createClass($mod, "TObject", null, function () {',
  11373. ' this.$init = function () {',
  11374. ' };',
  11375. ' this.$final = function () {',
  11376. ' };',
  11377. '});',
  11378. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  11379. '});',
  11380. 'this.v = undefined;',
  11381. 'this.c = null;',
  11382. '']),
  11383. LinesToStr([ // $mod.$main
  11384. '$mod.v = $mod.c;',
  11385. '$mod.v = $mod.TObject;',
  11386. '$mod.v = $mod.c;',
  11387. '$mod.v = $mod.c;',
  11388. '$mod.c = rtl.getObject($mod.v);',
  11389. '$mod.c = rtl.getObject($mod.v);',
  11390. '']));
  11391. end;
  11392. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  11393. begin
  11394. StartProgram(false);
  11395. Add('type');
  11396. Add(' integer = longint;');
  11397. Add(' TArray = array of JSValue;');
  11398. Add(' TArrgh = tarray;');
  11399. Add(' TArrInt = array of integer;');
  11400. Add('var');
  11401. Add(' v: jsvalue;');
  11402. Add(' TheArray: tarray;');
  11403. Add(' Arr: tarrgh;');
  11404. Add(' i: integer;');
  11405. Add(' ArrInt: tarrint;');
  11406. Add('begin');
  11407. Add(' arr:=thearray;');
  11408. Add(' thearray:=arr;');
  11409. Add(' setlength(arr,2);');
  11410. Add(' setlength(thearray,3);');
  11411. Add(' arr[4]:=v;');
  11412. Add(' arr[5]:=length(thearray);');
  11413. Add(' arr[6]:=nil;');
  11414. Add(' arr[7]:=thearray[8];');
  11415. Add(' arr[low(arr)]:=high(thearray);');
  11416. Add(' arr:=arrint;');
  11417. Add(' arrInt:=tarrint(arr);');
  11418. Add(' if TheArray = nil then ;');
  11419. Add(' if nil = TheArray then ;');
  11420. Add(' if TheArray <> nil then ;');
  11421. Add(' if nil <> TheArray then ;');
  11422. ConvertProgram;
  11423. CheckSource('TestJSValue_ArrayOfJSValue',
  11424. LinesToStr([ // statements
  11425. 'this.v = undefined;',
  11426. 'this.TheArray = [];',
  11427. 'this.Arr = [];',
  11428. 'this.i = 0;',
  11429. 'this.ArrInt = [];',
  11430. '']),
  11431. LinesToStr([ // $mod.$main
  11432. '$mod.Arr = $mod.TheArray;',
  11433. '$mod.TheArray = $mod.Arr;',
  11434. '$mod.Arr = rtl.arraySetLength($mod.Arr,2,undefined);',
  11435. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,3,undefined);',
  11436. '$mod.Arr[4] = $mod.v;',
  11437. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  11438. '$mod.Arr[6] = null;',
  11439. '$mod.Arr[7] = $mod.TheArray[8];',
  11440. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  11441. '$mod.Arr = $mod.ArrInt;',
  11442. '$mod.ArrInt = $mod.Arr;',
  11443. 'if (rtl.length($mod.TheArray) == 0) ;',
  11444. 'if (rtl.length($mod.TheArray) == 0) ;',
  11445. 'if (rtl.length($mod.TheArray) > 0) ;',
  11446. 'if (rtl.length($mod.TheArray) > 0) ;',
  11447. '']));
  11448. end;
  11449. procedure TTestModule.TestJSValue_Params;
  11450. begin
  11451. StartProgram(false);
  11452. Add('type');
  11453. Add(' integer = longint;');
  11454. Add(' TYesNo = boolean;');
  11455. Add(' TFloat = double;');
  11456. Add(' TCaption = string;');
  11457. Add(' TChar = char;');
  11458. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  11459. Add('var');
  11460. Add(' l: jsvalue;');
  11461. Add('begin');
  11462. Add(' a:=a;');
  11463. Add(' l:=b;');
  11464. Add(' c:=c;');
  11465. Add(' d:=d;');
  11466. Add(' Result:=l;');
  11467. Add('end;');
  11468. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  11469. Add('var');
  11470. Add(' v: jsvalue;');
  11471. Add(' i: integer;');
  11472. Add(' b: TYesNo;');
  11473. Add(' d: TFloat;');
  11474. Add(' s: TCaption;');
  11475. Add(' c: TChar;');
  11476. Add('begin');
  11477. Add(' v:=doit(v,v,v,v);');
  11478. Add(' i:=integer(dosome(i,i));');
  11479. Add(' b:=TYesNo(dosome(b,b));');
  11480. Add(' d:=TFloat(dosome(d,d));');
  11481. Add(' s:=TCaption(dosome(s,s));');
  11482. Add(' c:=TChar(dosome(c,c));');
  11483. ConvertProgram;
  11484. CheckSource('TestJSValue_Params',
  11485. LinesToStr([ // statements
  11486. 'this.DoIt = function (a, b, c, d) {',
  11487. ' var Result = undefined;',
  11488. ' var l = undefined;',
  11489. ' a = a;',
  11490. ' l = b;',
  11491. ' c.set(c.get());',
  11492. ' d.set(d.get());',
  11493. ' Result = l;',
  11494. ' return Result;',
  11495. '};',
  11496. 'this.DoSome = function (a, b) {',
  11497. ' var Result = undefined;',
  11498. ' return Result;',
  11499. '};',
  11500. 'this.v = undefined;',
  11501. 'this.i = 0;',
  11502. 'this.b = false;',
  11503. 'this.d = 0.0;',
  11504. 'this.s = "";',
  11505. 'this.c = "";',
  11506. '']),
  11507. LinesToStr([ // $mod.$main
  11508. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  11509. ' p: $mod,',
  11510. ' get: function () {',
  11511. ' return this.p.v;',
  11512. ' },',
  11513. ' set: function (v) {',
  11514. ' this.p.v = v;',
  11515. ' }',
  11516. '}, {',
  11517. ' p: $mod,',
  11518. ' get: function () {',
  11519. ' return this.p.v;',
  11520. ' },',
  11521. ' set: function (v) {',
  11522. ' this.p.v = v;',
  11523. ' }',
  11524. '});',
  11525. '$mod.i = Math.floor($mod.DoSome($mod.i, $mod.i));',
  11526. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  11527. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  11528. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  11529. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  11530. '']));
  11531. end;
  11532. procedure TTestModule.TestJSValue_UntypedParam;
  11533. begin
  11534. StartProgram(false);
  11535. Add('function DoIt(const a; var b; out c): jsvalue;');
  11536. Add('begin');
  11537. Add(' Result:=a;');
  11538. Add(' Result:=b;');
  11539. Add(' Result:=c;');
  11540. Add(' b:=Result;');
  11541. Add(' c:=Result;');
  11542. Add('end;');
  11543. Add('var i: longint;');
  11544. Add('begin');
  11545. Add(' doit(i,i,i);');
  11546. ConvertProgram;
  11547. CheckSource('TestJSValue_UntypedParam',
  11548. LinesToStr([ // statements
  11549. 'this.DoIt = function (a, b, c) {',
  11550. ' var Result = undefined;',
  11551. ' Result = a;',
  11552. ' Result = b.get();',
  11553. ' Result = c.get();',
  11554. ' b.set(Result);',
  11555. ' c.set(Result);',
  11556. ' return Result;',
  11557. '};',
  11558. 'this.i = 0;',
  11559. '']),
  11560. LinesToStr([ // $mod.$main
  11561. '$mod.DoIt($mod.i, {',
  11562. ' p: $mod,',
  11563. ' get: function () {',
  11564. ' return this.p.i;',
  11565. ' },',
  11566. ' set: function (v) {',
  11567. ' this.p.i = v;',
  11568. ' }',
  11569. '}, {',
  11570. ' p: $mod,',
  11571. ' get: function () {',
  11572. ' return this.p.i;',
  11573. ' },',
  11574. ' set: function (v) {',
  11575. ' this.p.i = v;',
  11576. ' }',
  11577. '});',
  11578. '']));
  11579. end;
  11580. procedure TTestModule.TestJSValue_FuncResultType;
  11581. begin
  11582. StartProgram(false);
  11583. Add('type');
  11584. Add(' integer = longint;');
  11585. Add(' TJSValueArray = array of JSValue;');
  11586. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  11587. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  11588. Add('begin');
  11589. Add(' while Compare(P,aList[0])>0 do ;');
  11590. Add('end;');
  11591. Add('var');
  11592. Add(' Compare: TListSortCompare;');
  11593. Add(' V: JSValue;');
  11594. Add(' i: integer;');
  11595. Add('begin');
  11596. Add(' if Compare(V,V)>0 then ;');
  11597. Add(' if Compare(i,i)>1 then ;');
  11598. Add(' if Compare(nil,false)>2 then ;');
  11599. Add(' if Compare(1,true)>3 then ;');
  11600. ConvertProgram;
  11601. CheckSource('TestJSValue_UntypedParam',
  11602. LinesToStr([ // statements
  11603. 'this.Sort = function (P, aList, Compare) {',
  11604. ' while (Compare(P, aList[0]) > 0) {',
  11605. ' };',
  11606. '};',
  11607. 'this.Compare = null;',
  11608. 'this.V = undefined;',
  11609. 'this.i = 0;',
  11610. '']),
  11611. LinesToStr([ // $mod.$main
  11612. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  11613. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  11614. 'if ($mod.Compare(null, false) > 2) ;',
  11615. 'if ($mod.Compare(1, true) > 3) ;',
  11616. '']));
  11617. end;
  11618. procedure TTestModule.TestJSValue_ProcType_Assign;
  11619. begin
  11620. StartProgram(false);
  11621. Add('type');
  11622. Add(' integer = longint;');
  11623. Add(' TObject = class');
  11624. Add(' class function GetGlob: integer;');
  11625. Add(' function Getter: integer;');
  11626. Add(' end;');
  11627. Add('class function TObject.GetGlob: integer;');
  11628. Add('var v1: jsvalue;');
  11629. Add('begin');
  11630. Add(' v1:=@GetGlob;');
  11631. Add(' v1:[email protected];');
  11632. Add('end;');
  11633. Add('function TObject.Getter: integer;');
  11634. Add('var v2: jsvalue;');
  11635. Add('begin');
  11636. Add(' v2:=@Getter;');
  11637. Add(' v2:[email protected];');
  11638. Add(' v2:=@GetGlob;');
  11639. Add(' v2:[email protected];');
  11640. Add('end;');
  11641. Add('function GetIt(i: integer): integer;');
  11642. Add('var v3: jsvalue;');
  11643. Add('begin');
  11644. Add(' v3:=@GetIt;');
  11645. Add('end;');
  11646. Add('var');
  11647. Add(' V: JSValue;');
  11648. Add(' o: TObject;');
  11649. Add('begin');
  11650. Add(' v:=@GetIt;');
  11651. Add(' v:[email protected];');
  11652. Add(' v:[email protected];');
  11653. ConvertProgram;
  11654. CheckSource('TestJSValue_ProcType_Assign',
  11655. LinesToStr([ // statements
  11656. 'rtl.createClass($mod, "TObject", null, function () {',
  11657. ' this.$init = function () {',
  11658. ' };',
  11659. ' this.$final = function () {',
  11660. ' };',
  11661. ' this.GetGlob = function () {',
  11662. ' var Result = 0;',
  11663. ' var v1 = undefined;',
  11664. ' v1 = rtl.createCallback(this, "GetGlob");',
  11665. ' v1 = rtl.createCallback(this, "GetGlob");',
  11666. ' return Result;',
  11667. ' };',
  11668. ' this.Getter = function () {',
  11669. ' var Result = 0;',
  11670. ' var v2 = undefined;',
  11671. ' v2 = rtl.createCallback(this, "Getter");',
  11672. ' v2 = rtl.createCallback(this, "Getter");',
  11673. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  11674. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  11675. ' return Result;',
  11676. ' };',
  11677. '});',
  11678. 'this.GetIt = function (i) {',
  11679. ' var Result = 0;',
  11680. ' var v3 = undefined;',
  11681. ' v3 = $mod.GetIt;',
  11682. ' return Result;',
  11683. '};',
  11684. 'this.V = undefined;',
  11685. 'this.o = null;',
  11686. '']),
  11687. LinesToStr([ // $mod.$main
  11688. '$mod.V = $mod.GetIt;',
  11689. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  11690. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  11691. '']));
  11692. end;
  11693. procedure TTestModule.TestJSValue_ProcType_Equal;
  11694. begin
  11695. StartProgram(false);
  11696. Add('type');
  11697. Add(' integer = longint;');
  11698. Add(' TObject = class');
  11699. Add(' class function GetGlob: integer;');
  11700. Add(' function Getter: integer;');
  11701. Add(' end;');
  11702. Add('class function TObject.GetGlob: integer;');
  11703. Add('var v1: jsvalue;');
  11704. Add('begin');
  11705. Add(' if v1=@GetGlob then;');
  11706. Add(' if [email protected] then ;');
  11707. Add('end;');
  11708. Add('function TObject.Getter: integer;');
  11709. Add('var v2: jsvalue;');
  11710. Add('begin');
  11711. Add(' if v2=@Getter then;');
  11712. Add(' if [email protected] then ;');
  11713. Add(' if v2=@GetGlob then;');
  11714. Add(' if [email protected] then;');
  11715. Add('end;');
  11716. Add('function GetIt(i: integer): integer;');
  11717. Add('var v3: jsvalue;');
  11718. Add('begin');
  11719. Add(' if v3=@GetIt then;');
  11720. Add('end;');
  11721. Add('var');
  11722. Add(' V: JSValue;');
  11723. Add(' o: TObject;');
  11724. Add('begin');
  11725. Add(' if v=@GetIt then;');
  11726. Add(' if [email protected] then;');
  11727. Add(' if [email protected] then;');
  11728. Add(' if @GetIt=v then;');
  11729. Add(' if @o.Getter=v then;');
  11730. Add(' if @o.GetGlob=v then;');
  11731. ConvertProgram;
  11732. CheckSource('TestJSValue_ProcType_Equal',
  11733. LinesToStr([ // statements
  11734. 'rtl.createClass($mod, "TObject", null, function () {',
  11735. ' this.$init = function () {',
  11736. ' };',
  11737. ' this.$final = function () {',
  11738. ' };',
  11739. ' this.GetGlob = function () {',
  11740. ' var Result = 0;',
  11741. ' var v1 = undefined;',
  11742. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  11743. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  11744. ' return Result;',
  11745. ' };',
  11746. ' this.Getter = function () {',
  11747. ' var Result = 0;',
  11748. ' var v2 = undefined;',
  11749. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  11750. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  11751. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  11752. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  11753. ' return Result;',
  11754. ' };',
  11755. '});',
  11756. 'this.GetIt = function (i) {',
  11757. ' var Result = 0;',
  11758. ' var v3 = undefined;',
  11759. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  11760. ' return Result;',
  11761. '};',
  11762. 'this.V = undefined;',
  11763. 'this.o = null;',
  11764. '']),
  11765. LinesToStr([ // $mod.$main
  11766. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  11767. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  11768. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  11769. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  11770. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  11771. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  11772. '']));
  11773. end;
  11774. procedure TTestModule.TestJSValue_AssignToPointerFail;
  11775. begin
  11776. StartProgram(false);
  11777. Add([
  11778. 'var',
  11779. ' v: JSValue;',
  11780. ' p: Pointer;',
  11781. 'begin',
  11782. ' p:=v;',
  11783. '']);
  11784. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  11785. nIncompatibleTypesGotExpected);
  11786. ConvertProgram;
  11787. end;
  11788. procedure TTestModule.TestJSValue_OverloadDouble;
  11789. begin
  11790. StartProgram(false);
  11791. Add([
  11792. 'type',
  11793. ' integer = longint;',
  11794. ' tdatetime = double;',
  11795. 'procedure DoIt(d: double); begin end;',
  11796. 'procedure DoIt(v: jsvalue); begin end;',
  11797. 'var',
  11798. ' d: double;',
  11799. ' dt: tdatetime;',
  11800. ' i: integer;',
  11801. ' b: byte;',
  11802. ' shi: shortint;',
  11803. ' w: word;',
  11804. ' smi: smallint;',
  11805. ' lw: longword;',
  11806. ' li: longint;',
  11807. ' ni: nativeint;',
  11808. ' nu: nativeuint;',
  11809. 'begin',
  11810. ' DoIt(d);',
  11811. ' DoIt(dt);',
  11812. ' DoIt(i);',
  11813. ' DoIt(b);',
  11814. ' DoIt(shi);',
  11815. ' DoIt(w);',
  11816. ' DoIt(smi);',
  11817. ' DoIt(lw);',
  11818. ' DoIt(li);',
  11819. ' DoIt(ni);',
  11820. ' DoIt(nu);',
  11821. '']);
  11822. ConvertProgram;
  11823. CheckSource('TestJSValue_OverloadDouble',
  11824. LinesToStr([ // statements
  11825. 'this.DoIt = function (d) {',
  11826. '};',
  11827. 'this.DoIt$1 = function (v) {',
  11828. '};',
  11829. 'this.d = 0.0;',
  11830. 'this.dt = 0.0;',
  11831. 'this.i = 0;',
  11832. 'this.b = 0;',
  11833. 'this.shi = 0;',
  11834. 'this.w = 0;',
  11835. 'this.smi = 0;',
  11836. 'this.lw = 0;',
  11837. 'this.li = 0;',
  11838. 'this.ni = 0;',
  11839. 'this.nu = 0;',
  11840. '']),
  11841. LinesToStr([ // $mod.$main
  11842. '$mod.DoIt($mod.d);',
  11843. '$mod.DoIt($mod.dt);',
  11844. '$mod.DoIt($mod.i);',
  11845. '$mod.DoIt($mod.b);',
  11846. '$mod.DoIt($mod.shi);',
  11847. '$mod.DoIt($mod.w);',
  11848. '$mod.DoIt($mod.smi);',
  11849. '$mod.DoIt($mod.lw);',
  11850. '$mod.DoIt($mod.li);',
  11851. '$mod.DoIt($mod.ni);',
  11852. '$mod.DoIt($mod.nu);',
  11853. '']));
  11854. end;
  11855. procedure TTestModule.TestJSValue_OverloadNativeInt;
  11856. begin
  11857. StartProgram(false);
  11858. Add([
  11859. 'type',
  11860. ' integer = longint;',
  11861. ' int53 = nativeint;',
  11862. ' tdatetime = double;',
  11863. 'procedure DoIt(n: nativeint); begin end;',
  11864. 'procedure DoIt(v: jsvalue); begin end;',
  11865. 'var',
  11866. ' d: double;',
  11867. ' dt: tdatetime;',
  11868. ' i: integer;',
  11869. ' b: byte;',
  11870. ' shi: shortint;',
  11871. ' w: word;',
  11872. ' smi: smallint;',
  11873. ' lw: longword;',
  11874. ' li: longint;',
  11875. ' ni: nativeint;',
  11876. ' nu: nativeuint;',
  11877. 'begin',
  11878. ' DoIt(d);',
  11879. ' DoIt(dt);',
  11880. ' DoIt(i);',
  11881. ' DoIt(b);',
  11882. ' DoIt(shi);',
  11883. ' DoIt(w);',
  11884. ' DoIt(smi);',
  11885. ' DoIt(lw);',
  11886. ' DoIt(li);',
  11887. ' DoIt(ni);',
  11888. ' DoIt(nu);',
  11889. '']);
  11890. ConvertProgram;
  11891. CheckSource('TestJSValue_OverloadNativeInt',
  11892. LinesToStr([ // statements
  11893. 'this.DoIt = function (n) {',
  11894. '};',
  11895. 'this.DoIt$1 = function (v) {',
  11896. '};',
  11897. 'this.d = 0.0;',
  11898. 'this.dt = 0.0;',
  11899. 'this.i = 0;',
  11900. 'this.b = 0;',
  11901. 'this.shi = 0;',
  11902. 'this.w = 0;',
  11903. 'this.smi = 0;',
  11904. 'this.lw = 0;',
  11905. 'this.li = 0;',
  11906. 'this.ni = 0;',
  11907. 'this.nu = 0;',
  11908. '']),
  11909. LinesToStr([ // $mod.$main
  11910. '$mod.DoIt$1($mod.d);',
  11911. '$mod.DoIt$1($mod.dt);',
  11912. '$mod.DoIt($mod.i);',
  11913. '$mod.DoIt($mod.b);',
  11914. '$mod.DoIt($mod.shi);',
  11915. '$mod.DoIt($mod.w);',
  11916. '$mod.DoIt($mod.smi);',
  11917. '$mod.DoIt($mod.lw);',
  11918. '$mod.DoIt($mod.li);',
  11919. '$mod.DoIt($mod.ni);',
  11920. '$mod.DoIt($mod.nu);',
  11921. '']));
  11922. end;
  11923. procedure TTestModule.TestJSValue_OverloadWord;
  11924. begin
  11925. StartProgram(false);
  11926. Add([
  11927. 'type',
  11928. ' integer = longint;',
  11929. ' int53 = nativeint;',
  11930. ' tdatetime = double;',
  11931. 'procedure DoIt(w: word); begin end;',
  11932. 'procedure DoIt(v: jsvalue); begin end;',
  11933. 'var',
  11934. ' d: double;',
  11935. ' dt: tdatetime;',
  11936. ' i: integer;',
  11937. ' b: byte;',
  11938. ' shi: shortint;',
  11939. ' w: word;',
  11940. ' smi: smallint;',
  11941. ' lw: longword;',
  11942. ' li: longint;',
  11943. ' ni: nativeint;',
  11944. ' nu: nativeuint;',
  11945. 'begin',
  11946. ' DoIt(d);',
  11947. ' DoIt(dt);',
  11948. ' DoIt(i);',
  11949. ' DoIt(b);',
  11950. ' DoIt(shi);',
  11951. ' DoIt(w);',
  11952. ' DoIt(smi);',
  11953. ' DoIt(lw);',
  11954. ' DoIt(li);',
  11955. ' DoIt(ni);',
  11956. ' DoIt(nu);',
  11957. '']);
  11958. ConvertProgram;
  11959. CheckSource('TestJSValue_OverloadWord',
  11960. LinesToStr([ // statements
  11961. 'this.DoIt = function (w) {',
  11962. '};',
  11963. 'this.DoIt$1 = function (v) {',
  11964. '};',
  11965. 'this.d = 0.0;',
  11966. 'this.dt = 0.0;',
  11967. 'this.i = 0;',
  11968. 'this.b = 0;',
  11969. 'this.shi = 0;',
  11970. 'this.w = 0;',
  11971. 'this.smi = 0;',
  11972. 'this.lw = 0;',
  11973. 'this.li = 0;',
  11974. 'this.ni = 0;',
  11975. 'this.nu = 0;',
  11976. '']),
  11977. LinesToStr([ // $mod.$main
  11978. '$mod.DoIt$1($mod.d);',
  11979. '$mod.DoIt$1($mod.dt);',
  11980. '$mod.DoIt$1($mod.i);',
  11981. '$mod.DoIt($mod.b);',
  11982. '$mod.DoIt($mod.shi);',
  11983. '$mod.DoIt($mod.w);',
  11984. '$mod.DoIt$1($mod.smi);',
  11985. '$mod.DoIt$1($mod.lw);',
  11986. '$mod.DoIt$1($mod.li);',
  11987. '$mod.DoIt$1($mod.ni);',
  11988. '$mod.DoIt$1($mod.nu);',
  11989. '']));
  11990. end;
  11991. procedure TTestModule.TestJSValue_OverloadString;
  11992. begin
  11993. StartProgram(false);
  11994. Add([
  11995. 'type',
  11996. ' uni = string;',
  11997. ' WideChar = char;',
  11998. 'procedure DoIt(s: string); begin end;',
  11999. 'procedure DoIt(v: jsvalue); begin end;',
  12000. 'var',
  12001. ' s: string;',
  12002. ' c: char;',
  12003. ' u: uni;',
  12004. 'begin',
  12005. ' DoIt(s);',
  12006. ' DoIt(c);',
  12007. ' DoIt(u);',
  12008. '']);
  12009. ConvertProgram;
  12010. CheckSource('TestJSValue_OverloadString',
  12011. LinesToStr([ // statements
  12012. 'this.DoIt = function (s) {',
  12013. '};',
  12014. 'this.DoIt$1 = function (v) {',
  12015. '};',
  12016. 'this.s = "";',
  12017. 'this.c = "";',
  12018. 'this.u = "";',
  12019. '']),
  12020. LinesToStr([ // $mod.$main
  12021. '$mod.DoIt($mod.s);',
  12022. '$mod.DoIt($mod.c);',
  12023. '$mod.DoIt($mod.u);',
  12024. '']));
  12025. end;
  12026. procedure TTestModule.TestJSValue_OverloadChar;
  12027. begin
  12028. StartProgram(false);
  12029. Add([
  12030. 'type',
  12031. ' uni = string;',
  12032. ' WideChar = char;',
  12033. 'procedure DoIt(c: char); begin end;',
  12034. 'procedure DoIt(v: jsvalue); begin end;',
  12035. 'var',
  12036. ' s: string;',
  12037. ' c: char;',
  12038. ' u: uni;',
  12039. 'begin',
  12040. ' DoIt(s);',
  12041. ' DoIt(c);',
  12042. ' DoIt(u);',
  12043. '']);
  12044. ConvertProgram;
  12045. CheckSource('TestJSValue_OverloadChar',
  12046. LinesToStr([ // statements
  12047. 'this.DoIt = function (c) {',
  12048. '};',
  12049. 'this.DoIt$1 = function (v) {',
  12050. '};',
  12051. 'this.s = "";',
  12052. 'this.c = "";',
  12053. 'this.u = "";',
  12054. '']),
  12055. LinesToStr([ // $mod.$main
  12056. '$mod.DoIt$1($mod.s);',
  12057. '$mod.DoIt($mod.c);',
  12058. '$mod.DoIt$1($mod.u);',
  12059. '']));
  12060. end;
  12061. procedure TTestModule.TestJSValue_OverloadPointer;
  12062. begin
  12063. StartProgram(false);
  12064. Add([
  12065. 'type',
  12066. ' TObject = class end;',
  12067. 'procedure DoIt(p: pointer); begin end;',
  12068. 'procedure DoIt(v: jsvalue); begin end;',
  12069. 'var',
  12070. ' o: TObject;',
  12071. 'begin',
  12072. ' DoIt(o);',
  12073. '']);
  12074. ConvertProgram;
  12075. CheckSource('TestJSValue_OverloadPointer',
  12076. LinesToStr([ // statements
  12077. 'rtl.createClass($mod, "TObject", null, function () {',
  12078. ' this.$init = function () {',
  12079. ' };',
  12080. ' this.$final = function () {',
  12081. ' };',
  12082. '});',
  12083. 'this.DoIt = function (p) {',
  12084. '};',
  12085. 'this.DoIt$1 = function (v) {',
  12086. '};',
  12087. 'this.o = null;',
  12088. '']),
  12089. LinesToStr([ // $mod.$main
  12090. '$mod.DoIt($mod.o);',
  12091. '']));
  12092. end;
  12093. procedure TTestModule.TestRTTI_ProcType;
  12094. begin
  12095. Converter.Options:=Converter.Options-[coNoTypeInfo];
  12096. StartProgram(false);
  12097. Add('type');
  12098. Add(' TProcA = procedure;');
  12099. Add(' TMethodB = procedure of object;');
  12100. Add(' TProcC = procedure; varargs;');
  12101. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  12102. Add(' TProcE = function: nativeint;');
  12103. Add(' TProcF = function(const p: TProcA): nativeuint;');
  12104. Add('var p: pointer;');
  12105. Add('begin');
  12106. Add(' p:=typeinfo(tproca);');
  12107. ConvertProgram;
  12108. CheckSource('TestRTTI_ProcType',
  12109. LinesToStr([ // statements
  12110. '$mod.$rtti.$ProcVar("TProcA", {',
  12111. ' procsig: rtl.newTIProcSig(null)',
  12112. '});',
  12113. '$mod.$rtti.$MethodVar("TMethodB", {',
  12114. ' procsig: rtl.newTIProcSig(null),',
  12115. ' methodkind: 0',
  12116. '});',
  12117. '$mod.$rtti.$ProcVar("TProcC", {',
  12118. ' procsig: rtl.newTIProcSig(null, 2)',
  12119. '});',
  12120. '$mod.$rtti.$ProcVar("TProcD", {',
  12121. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  12122. '});',
  12123. '$mod.$rtti.$ProcVar("TProcE", {',
  12124. ' procsig: rtl.newTIProcSig(null, rtl.nativeint)',
  12125. '});',
  12126. '$mod.$rtti.$ProcVar("TProcF", {',
  12127. ' procsig: rtl.newTIProcSig([["p", $mod.$rtti["TProcA"], 2]], rtl.nativeuint)',
  12128. '});',
  12129. 'this.p = null;',
  12130. '']),
  12131. LinesToStr([ // $mod.$main
  12132. '$mod.p = $mod.$rtti["TProcA"];',
  12133. '']));
  12134. end;
  12135. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  12136. begin
  12137. Converter.Options:=Converter.Options-[coNoTypeInfo];
  12138. AddModuleWithIntfImplSrc('unit2.pas',
  12139. LinesToStr([
  12140. 'type',
  12141. ' TObject = class end;'
  12142. ]),
  12143. '');
  12144. StartUnit(true);
  12145. Add('interface');
  12146. Add('uses unit2;');
  12147. Add('type');
  12148. Add(' TProcA = function(o: tobject): tobject;');
  12149. Add('implementation');
  12150. Add('type');
  12151. Add(' TProcB = function(o: tobject): tobject;');
  12152. Add('var p: Pointer;');
  12153. Add('initialization');
  12154. Add(' p:=typeinfo(tproca);');
  12155. Add(' p:=typeinfo(tprocb);');
  12156. ConvertUnit;
  12157. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  12158. LinesToStr([ // statements
  12159. 'var $impl = $mod.$impl;',
  12160. '$mod.$rtti.$ProcVar("TProcA", {',
  12161. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  12162. '});',
  12163. '']),
  12164. LinesToStr([ // this.$init
  12165. '$impl.p = $mod.$rtti["TProcA"];',
  12166. '$impl.p = $mod.$rtti["TProcB"];',
  12167. '']),
  12168. LinesToStr([ // implementation
  12169. '$mod.$rtti.$ProcVar("TProcB", {',
  12170. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  12171. '});',
  12172. '$impl.p = null;',
  12173. '']) );
  12174. end;
  12175. procedure TTestModule.TestRTTI_EnumAndSetType;
  12176. begin
  12177. Converter.Options:=Converter.Options-[coNoTypeInfo];
  12178. StartProgram(false);
  12179. Add('type');
  12180. Add(' TFlag = (light,dark);');
  12181. Add(' TFlags = set of TFlag;');
  12182. Add(' TProc = function(f: TFlags): TFlag;');
  12183. Add('var p: pointer;');
  12184. Add('begin');
  12185. Add(' p:=typeinfo(tflag);');
  12186. Add(' p:=typeinfo(tflags);');
  12187. ConvertProgram;
  12188. CheckSource('TestRTTI_EnumAndType',
  12189. LinesToStr([ // statements
  12190. 'this.TFlag = {',
  12191. ' "0": "light",',
  12192. ' light: 0,',
  12193. ' "1": "dark",',
  12194. ' dark: 1',
  12195. '};',
  12196. '$mod.$rtti.$Enum("TFlag", {',
  12197. ' minvalue: 0,',
  12198. ' maxvalue: 1,',
  12199. ' enumtype: this.TFlag',
  12200. '});',
  12201. '$mod.$rtti.$Set("TFlags", {',
  12202. ' comptype: $mod.$rtti["TFlag"]',
  12203. '});',
  12204. '$mod.$rtti.$ProcVar("TProc", {',
  12205. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TFlags"]]], $mod.$rtti["TFlag"])',
  12206. '});',
  12207. 'this.p = null;',
  12208. '']),
  12209. LinesToStr([ // $mod.$main
  12210. '$mod.p = $mod.$rtti["TFlag"];',
  12211. '$mod.p = $mod.$rtti["TFlags"];',
  12212. '']));
  12213. end;
  12214. procedure TTestModule.TestRTTI_AnonymousEnumType;
  12215. begin
  12216. Converter.Options:=Converter.Options-[coNoTypeInfo];
  12217. StartProgram(false);
  12218. Add('type');
  12219. Add(' TFlags = set of (red, green);');
  12220. Add('var');
  12221. Add(' f: TFlags;');
  12222. Add('begin');
  12223. Add(' Include(f,red);');
  12224. ConvertProgram;
  12225. CheckSource('TestRTTI_AnonymousEnumType',
  12226. LinesToStr([ // statements
  12227. 'this.TFlags$a = {',
  12228. ' "0": "red",',
  12229. ' red: 0,',
  12230. ' "1": "green",',
  12231. ' green: 1',
  12232. '};',
  12233. '$mod.$rtti.$Enum("TFlags$a", {',
  12234. ' minvalue: 0,',
  12235. ' maxvalue: 1,',
  12236. ' enumtype: this.TFlags$a',
  12237. '});',
  12238. '$mod.$rtti.$Set("TFlags", {',
  12239. ' comptype: $mod.$rtti["TFlags$a"]',
  12240. '});',
  12241. 'this.f = {};',
  12242. '']),
  12243. LinesToStr([
  12244. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  12245. '']));
  12246. end;
  12247. procedure TTestModule.TestRTTI_StaticArray;
  12248. begin
  12249. Converter.Options:=Converter.Options-[coNoTypeInfo];
  12250. StartProgram(false);
  12251. Add('type');
  12252. Add(' TFlag = (light,dark);');
  12253. Add(' TFlagNames = array[TFlag] of string;');
  12254. Add(' TBoolNames = array[boolean] of string;');
  12255. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  12256. Add('var p: pointer;');
  12257. Add('begin');
  12258. Add(' p:=typeinfo(TFlagNames);');
  12259. Add(' p:=typeinfo(TBoolNames);');
  12260. ConvertProgram;
  12261. CheckSource('TestRTTI_StaticArray',
  12262. LinesToStr([ // statements
  12263. 'this.TFlag = {',
  12264. ' "0": "light",',
  12265. ' light: 0,',
  12266. ' "1": "dark",',
  12267. ' dark: 1',
  12268. '};',
  12269. '$mod.$rtti.$Enum("TFlag", {',
  12270. ' minvalue: 0,',
  12271. ' maxvalue: 1,',
  12272. ' enumtype: this.TFlag',
  12273. '});',
  12274. '$mod.$rtti.$StaticArray("TFlagNames", {',
  12275. ' dims: [2],',
  12276. ' eltype: rtl.string',
  12277. '});',
  12278. '$mod.$rtti.$StaticArray("TBoolNames", {',
  12279. ' dims: [2],',
  12280. ' eltype: rtl.string',
  12281. '});',
  12282. '$mod.$rtti.$ProcVar("TProc", {',
  12283. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TBoolNames"]]], $mod.$rtti["TFlagNames"])',
  12284. '});',
  12285. 'this.p = null;',
  12286. '']),
  12287. LinesToStr([ // $mod.$main
  12288. '$mod.p = $mod.$rtti["TFlagNames"];',
  12289. '$mod.p = $mod.$rtti["TBoolNames"];',
  12290. '']));
  12291. end;
  12292. procedure TTestModule.TestRTTI_DynArray;
  12293. begin
  12294. Converter.Options:=Converter.Options-[coNoTypeInfo];
  12295. StartProgram(false);
  12296. Add('type');
  12297. Add(' TArrStr = array of string;');
  12298. Add(' TArr2Dim = array of tarrstr;');
  12299. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  12300. Add('var p: pointer;');
  12301. Add('begin');
  12302. Add(' p:=typeinfo(tarrstr);');
  12303. Add(' p:=typeinfo(tarr2dim);');
  12304. ConvertProgram;
  12305. CheckSource('TestRTTI_DynArray',
  12306. LinesToStr([ // statements
  12307. '$mod.$rtti.$DynArray("TArrStr", {',
  12308. ' eltype: rtl.string',
  12309. '});',
  12310. '$mod.$rtti.$DynArray("TArr2Dim", {',
  12311. ' eltype: $mod.$rtti["TArrStr"]',
  12312. '});',
  12313. '$mod.$rtti.$ProcVar("TProc", {',
  12314. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TArrStr"]]], $mod.$rtti["TArr2Dim"])',
  12315. '});',
  12316. 'this.p = null;',
  12317. '']),
  12318. LinesToStr([ // $mod.$main
  12319. '$mod.p = $mod.$rtti["TArrStr"];',
  12320. '$mod.p = $mod.$rtti["TArr2Dim"];',
  12321. '']));
  12322. end;
  12323. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  12324. begin
  12325. Converter.Options:=Converter.Options-[coNoTypeInfo];
  12326. StartProgram(false);
  12327. Add('type');
  12328. Add(' TArr = array of array of longint;');
  12329. Add('var a: TArr;');
  12330. Add('begin');
  12331. ConvertProgram;
  12332. CheckSource('TestRTTI_ArrayNestedAnonymous',
  12333. LinesToStr([ // statements
  12334. '$mod.$rtti.$DynArray("TArr$a", {',
  12335. ' eltype: rtl.longint',
  12336. '});',
  12337. '$mod.$rtti.$DynArray("TArr", {',
  12338. ' eltype: $mod.$rtti["TArr$a"]',
  12339. '});',
  12340. 'this.a = [];',
  12341. '']),
  12342. LinesToStr([ // $mod.$main
  12343. ]));
  12344. end;
  12345. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  12346. begin
  12347. Converter.Options:=Converter.Options-[coNoTypeInfo];
  12348. StartProgram(false);
  12349. Add('type');
  12350. Add(' TObject = class');
  12351. Add(' published');
  12352. Add(' procedure Proc; virtual; abstract;');
  12353. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  12354. Add(' end;');
  12355. Add('begin');
  12356. SetExpectedPasResolverError('Duplicate identifier "Proc" at test1.pp(6,18)',
  12357. nDuplicateIdentifier);
  12358. ConvertProgram;
  12359. end;
  12360. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  12361. begin
  12362. Converter.Options:=Converter.Options-[coNoTypeInfo];
  12363. StartProgram(false);
  12364. Add('type');
  12365. Add(' TObject = class');
  12366. Add(' published');
  12367. Add(' procedure Proc; external name ''foo'';');
  12368. Add(' end;');
  12369. Add('begin');
  12370. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  12371. nPublishedNameMustMatchExternal);
  12372. ConvertProgram;
  12373. end;
  12374. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  12375. begin
  12376. Converter.Options:=Converter.Options-[coNoTypeInfo];
  12377. StartProgram(false);
  12378. Add('type');
  12379. Add(' TObject = class');
  12380. Add(' class var FA: longint;');
  12381. Add(' published');
  12382. Add(' class property A: longint read FA;');
  12383. Add(' end;');
  12384. Add('begin');
  12385. SetExpectedPasResolverError('Invalid published property modifier "class"',
  12386. nInvalidXModifierY);
  12387. ConvertProgram;
  12388. end;
  12389. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  12390. begin
  12391. Converter.Options:=Converter.Options-[coNoTypeInfo];
  12392. StartProgram(false);
  12393. Add('type');
  12394. Add(' TObject = class');
  12395. Add(' published');
  12396. Add(' class var FA: longint;');
  12397. Add(' end;');
  12398. Add('begin');
  12399. SetExpectedPasResolverError(sSymbolCannotBePublished,
  12400. nSymbolCannotBePublished);
  12401. ConvertProgram;
  12402. end;
  12403. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  12404. begin
  12405. Converter.Options:=Converter.Options-[coNoTypeInfo];
  12406. StartProgram(false);
  12407. Add('{$modeswitch externalclass}');
  12408. Add('type');
  12409. Add(' TObject = class');
  12410. Add(' published');
  12411. Add(' V: longint; external name ''foo'';');
  12412. Add(' end;');
  12413. Add('begin');
  12414. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  12415. nPublishedNameMustMatchExternal);
  12416. ConvertProgram;
  12417. end;
  12418. procedure TTestModule.TestRTTI_Class_Field;
  12419. begin
  12420. Converter.Options:=Converter.Options-[coNoTypeInfo];
  12421. StartProgram(false);
  12422. Add('{$modeswitch externalclass}');
  12423. Add('type');
  12424. Add(' TObject = class');
  12425. Add(' private');
  12426. Add(' FPropA: string;');
  12427. Add(' published');
  12428. Add(' VarLI: longint;');
  12429. Add(' VarC: char;');
  12430. Add(' VarS: string;');
  12431. Add(' VarD: double;');
  12432. Add(' VarB: boolean;');
  12433. Add(' VarLW: longword;');
  12434. Add(' VarSmI: smallint;');
  12435. Add(' VarW: word;');
  12436. Add(' VarShI: shortint;');
  12437. Add(' VarBy: byte;');
  12438. Add(' VarExt: longint external name ''VarExt'';');
  12439. Add(' end;');
  12440. Add('var p: pointer;');
  12441. Add(' Obj: tobject;');
  12442. Add('begin');
  12443. Add(' p:=typeinfo(tobject);');
  12444. Add(' p:=typeinfo(p);');
  12445. Add(' p:=typeinfo(obj);');
  12446. ConvertProgram;
  12447. CheckSource('TestRTTI_Class_Field',
  12448. LinesToStr([ // statements
  12449. 'rtl.createClass($mod, "TObject", null, function () {',
  12450. ' this.$init = function () {',
  12451. ' this.FPropA = "";',
  12452. ' this.VarLI = 0;',
  12453. ' this.VarC = "";',
  12454. ' this.VarS = "";',
  12455. ' this.VarD = 0.0;',
  12456. ' this.VarB = false;',
  12457. ' this.VarLW = 0;',
  12458. ' this.VarSmI = 0;',
  12459. ' this.VarW = 0;',
  12460. ' this.VarShI = 0;',
  12461. ' this.VarBy = 0;',
  12462. ' };',
  12463. ' this.$final = function () {',
  12464. ' };',
  12465. ' var $r = this.$rtti;',
  12466. ' $r.addField("VarLI", rtl.longint);',
  12467. ' $r.addField("VarC", rtl.char);',
  12468. ' $r.addField("VarS", rtl.string);',
  12469. ' $r.addField("VarD", rtl.double);',
  12470. ' $r.addField("VarB", rtl.boolean);',
  12471. ' $r.addField("VarLW", rtl.longword);',
  12472. ' $r.addField("VarSmI", rtl.smallint);',
  12473. ' $r.addField("VarW", rtl.word);',
  12474. ' $r.addField("VarShI", rtl.shortint);',
  12475. ' $r.addField("VarBy", rtl.byte);',
  12476. ' $r.addField("VarExt", rtl.longint);',
  12477. '});',
  12478. 'this.p = null;',
  12479. 'this.Obj = null;',
  12480. '']),
  12481. LinesToStr([ // $mod.$main
  12482. '$mod.p = $mod.$rtti["TObject"];',
  12483. '$mod.p = rtl.pointer;',
  12484. '$mod.p = $mod.Obj.$rtti;',
  12485. '']));
  12486. end;
  12487. procedure TTestModule.TestRTTI_Class_Method;
  12488. begin
  12489. Converter.Options:=Converter.Options-[coNoTypeInfo];
  12490. StartProgram(false);
  12491. Add('type');
  12492. Add(' TObject = class');
  12493. Add(' private');
  12494. Add(' procedure Internal; external name ''$intern'';');
  12495. Add(' published');
  12496. Add(' procedure Click; virtual; abstract;');
  12497. Add(' procedure Notify(Sender: TObject); virtual; abstract;');
  12498. Add(' function GetNotify: boolean; external name ''GetNotify'';');
  12499. Add(' procedure Println(a,b: longint); varargs; virtual; abstract;');
  12500. Add(' end;');
  12501. Add('begin');
  12502. ConvertProgram;
  12503. CheckSource('TestRTTI_Class_Method',
  12504. LinesToStr([ // statements
  12505. 'rtl.createClass($mod, "TObject", null, function () {',
  12506. ' this.$init = function () {',
  12507. ' };',
  12508. ' this.$final = function () {',
  12509. ' };',
  12510. ' var $r = this.$rtti;',
  12511. ' $r.addMethod("Click", 0, null);',
  12512. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  12513. ' $r.addMethod("GetNotify", 1, null, rtl.boolean,{flags: 4});',
  12514. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, {',
  12515. ' flags: 2',
  12516. ' });',
  12517. '});',
  12518. '']),
  12519. LinesToStr([ // $mod.$main
  12520. '']));
  12521. end;
  12522. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  12523. begin
  12524. Converter.Options:=Converter.Options-[coNoTypeInfo];
  12525. StartProgram(false);
  12526. Add('type');
  12527. Add(' TObject = class');
  12528. Add(' published');
  12529. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  12530. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  12531. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  12532. Add(' end;');
  12533. Add('begin');
  12534. ConvertProgram;
  12535. CheckSource('TestRTTI_Class_MethodOpenArray',
  12536. LinesToStr([ // statements
  12537. 'rtl.createClass($mod, "TObject", null, function () {',
  12538. ' this.$init = function () {',
  12539. ' };',
  12540. ' this.$final = function () {',
  12541. ' };',
  12542. ' var $r = this.$rtti;',
  12543. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  12544. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  12545. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  12546. '});',
  12547. '']),
  12548. LinesToStr([ // $mod.$main
  12549. '']));
  12550. end;
  12551. procedure TTestModule.TestRTTI_Class_Property;
  12552. begin
  12553. Converter.Options:=Converter.Options-[coNoTypeInfo];
  12554. StartProgram(false);
  12555. Add('{$modeswitch externalclass}');
  12556. Add('type');
  12557. Add(' TObject = class');
  12558. Add(' private');
  12559. Add(' FColor: longint;');
  12560. Add(' FColorStored: boolean;');
  12561. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  12562. Add(' function GetColor: longint; virtual; abstract;');
  12563. Add(' function GetColorStored: boolean; virtual; abstract;');
  12564. Add(' FExtSize: longint external name ''$extSize'';');
  12565. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  12566. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  12567. Add(' function GetExtSize: longint; external name ''$getSize'';');
  12568. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  12569. Add(' published');
  12570. Add(' property ColorA: longint read FColor;');
  12571. Add(' property ColorB: longint write FColor;');
  12572. Add(' property ColorC: longint read GetColor write SetColor;');
  12573. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  12574. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  12575. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  12576. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  12577. Add(' end;');
  12578. Add('begin');
  12579. ConvertProgram;
  12580. CheckSource('TestRTTI_Class_Property',
  12581. LinesToStr([ // statements
  12582. 'rtl.createClass($mod, "TObject", null, function () {',
  12583. ' this.$init = function () {',
  12584. ' this.FColor = 0;',
  12585. ' this.FColorStored = false;',
  12586. ' };',
  12587. ' this.$final = function () {',
  12588. ' };',
  12589. ' var $r = this.$rtti;',
  12590. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  12591. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  12592. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  12593. ' $r.addProperty("ColorD", 0, rtl.longint, "FColor", "FColor",{',
  12594. ' stored: "FColorStored"',
  12595. ' }',
  12596. ' );',
  12597. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  12598. ' $r.addProperty("ExtSizeB", 3, rtl.longint, "$getSize", "$setSize",{',
  12599. ' stored: "$extSizeStored"',
  12600. ' }',
  12601. ' );',
  12602. ' $r.addProperty("ExtSizeC", 4, rtl.longint, "$extSize", "$extSize",{',
  12603. ' stored: "$getExtSizeStored"',
  12604. ' }',
  12605. ' );',
  12606. '});',
  12607. '']),
  12608. LinesToStr([ // $mod.$main
  12609. '']));
  12610. end;
  12611. procedure TTestModule.TestRTTI_Class_PropertyParams;
  12612. begin
  12613. Converter.Options:=Converter.Options-[coNoTypeInfo];
  12614. StartProgram(false);
  12615. Add('{$modeswitch externalclass}');
  12616. Add('type');
  12617. Add(' integer = longint;');
  12618. Add(' TObject = class');
  12619. Add(' private');
  12620. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  12621. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  12622. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  12623. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  12624. Add(' published');
  12625. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  12626. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  12627. Add(' end;');
  12628. Add('begin');
  12629. ConvertProgram;
  12630. CheckSource('TestRTTI_Class_PropertyParams',
  12631. LinesToStr([ // statements
  12632. 'rtl.createClass($mod, "TObject", null, function () {',
  12633. ' this.$init = function () {',
  12634. ' };',
  12635. ' this.$final = function () {',
  12636. ' };',
  12637. ' var $r = this.$rtti;',
  12638. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  12639. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  12640. '});',
  12641. '']),
  12642. LinesToStr([ // $mod.$main
  12643. '']));
  12644. end;
  12645. procedure TTestModule.TestRTTI_OverrideMethod;
  12646. begin
  12647. Converter.Options:=Converter.Options-[coNoTypeInfo];
  12648. StartProgram(false);
  12649. Add('type');
  12650. Add(' TObject = class');
  12651. Add(' published');
  12652. Add(' procedure DoIt; virtual; abstract;');
  12653. Add(' end;');
  12654. Add(' TSky = class');
  12655. Add(' published');
  12656. Add(' procedure DoIt; override;');
  12657. Add(' end;');
  12658. Add('procedure TSky.DoIt; begin end;');
  12659. Add('begin');
  12660. ConvertProgram;
  12661. CheckSource('TestRTTI_OverrideMethod',
  12662. LinesToStr([ // statements
  12663. 'rtl.createClass($mod, "TObject", null, function () {',
  12664. ' this.$init = function () {',
  12665. ' };',
  12666. ' this.$final = function () {',
  12667. ' };',
  12668. ' var $r = this.$rtti;',
  12669. ' $r.addMethod("DoIt", 0, null);',
  12670. '});',
  12671. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  12672. ' this.DoIt = function () {',
  12673. ' };',
  12674. '});',
  12675. '']),
  12676. LinesToStr([ // $mod.$main
  12677. '']));
  12678. end;
  12679. procedure TTestModule.TestRTTI_OverloadProperty;
  12680. begin
  12681. Converter.Options:=Converter.Options-[coNoTypeInfo];
  12682. StartProgram(false);
  12683. Add('type');
  12684. Add(' TObject = class');
  12685. Add(' protected');
  12686. Add(' FFlag: longint;');
  12687. Add(' published');
  12688. Add(' property Flag: longint read fflag;');
  12689. Add(' end;');
  12690. Add(' TSky = class');
  12691. Add(' published');
  12692. Add(' property FLAG: longint write fflag;');
  12693. Add(' end;');
  12694. Add('begin');
  12695. ConvertProgram;
  12696. CheckSource('TestRTTI_OverrideMethod',
  12697. LinesToStr([ // statements
  12698. 'rtl.createClass($mod, "TObject", null, function () {',
  12699. ' this.$init = function () {',
  12700. ' this.FFlag = 0;',
  12701. ' };',
  12702. ' this.$final = function () {',
  12703. ' };',
  12704. ' var $r = this.$rtti;',
  12705. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  12706. '});',
  12707. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  12708. ' var $r = this.$rtti;',
  12709. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  12710. '});',
  12711. '']),
  12712. LinesToStr([ // $mod.$main
  12713. '']));
  12714. end;
  12715. procedure TTestModule.TestRTTI_ClassForward;
  12716. begin
  12717. Converter.Options:=Converter.Options-[coNoTypeInfo];
  12718. StartProgram(false);
  12719. Add('type');
  12720. Add(' TObject = class end;');
  12721. Add(' tbridge = class;');
  12722. Add(' TProc = function: tbridge;');
  12723. Add(' TOger = class');
  12724. Add(' published');
  12725. Add(' FBridge: tbridge;');
  12726. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  12727. Add(' property Bridge: tbridge read fbridge write setbridge;');
  12728. Add(' end;');
  12729. Add(' TBridge = class');
  12730. Add(' FOger: toger;');
  12731. Add(' end;');
  12732. Add('var p: Pointer;');
  12733. Add(' b: tbridge;');
  12734. Add('begin');
  12735. Add(' p:=typeinfo(tbridge);');
  12736. Add(' p:=typeinfo(b);');
  12737. ConvertProgram;
  12738. CheckSource('TestRTTI_ClassForward',
  12739. LinesToStr([ // statements
  12740. 'rtl.createClass($mod, "TObject", null, function () {',
  12741. ' this.$init = function () {',
  12742. ' };',
  12743. ' this.$final = function () {',
  12744. ' };',
  12745. '});',
  12746. '$mod.$rtti.$Class("TBridge");',
  12747. '$mod.$rtti.$ProcVar("TProc", {',
  12748. ' procsig: rtl.newTIProcSig(null, $mod.$rtti["TBridge"])',
  12749. '});',
  12750. 'rtl.createClass($mod, "TOger", $mod.TObject, function () {',
  12751. ' this.$init = function () {',
  12752. ' $mod.TObject.$init.call(this);',
  12753. ' this.FBridge = null;',
  12754. ' };',
  12755. ' this.$final = function () {',
  12756. ' this.FBridge = undefined;',
  12757. ' $mod.TObject.$final.call(this);',
  12758. ' };',
  12759. ' var $r = this.$rtti;',
  12760. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  12761. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  12762. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  12763. '});',
  12764. 'rtl.createClass($mod, "TBridge", $mod.TObject, function () {',
  12765. ' this.$init = function () {',
  12766. ' $mod.TObject.$init.call(this);',
  12767. ' this.FOger = null;',
  12768. ' };',
  12769. ' this.$final = function () {',
  12770. ' this.FOger = undefined;',
  12771. ' $mod.TObject.$final.call(this);',
  12772. ' };',
  12773. '});',
  12774. 'this.p = null;',
  12775. 'this.b = null;',
  12776. '']),
  12777. LinesToStr([ // $mod.$main
  12778. '$mod.p = $mod.$rtti["TBridge"];',
  12779. '$mod.p = $mod.b.$rtti;',
  12780. '']));
  12781. end;
  12782. procedure TTestModule.TestRTTI_ClassOf;
  12783. begin
  12784. Converter.Options:=Converter.Options-[coNoTypeInfo];
  12785. StartProgram(false);
  12786. Add('type');
  12787. Add(' TClass = class of tobject;');
  12788. Add(' TProcA = function: TClass;');
  12789. Add(' TObject = class');
  12790. Add(' published');
  12791. Add(' C: tclass;');
  12792. Add(' end;');
  12793. Add(' tfox = class;');
  12794. Add(' TBird = class end;');
  12795. Add(' TBirds = class of tbird;');
  12796. Add(' TFox = class end;');
  12797. Add(' TFoxes = class of tfox;');
  12798. Add(' TCows = class of TCow;');
  12799. Add(' TCow = class;');
  12800. Add(' TCow = class end;');
  12801. Add('begin');
  12802. ConvertProgram;
  12803. CheckSource('TestRTTI_ClassOf',
  12804. LinesToStr([ // statements
  12805. '$mod.$rtti.$Class("TObject");',
  12806. '$mod.$rtti.$ClassRef("TClass", {',
  12807. ' instancetype: $mod.$rtti["TObject"]',
  12808. '});',
  12809. '$mod.$rtti.$ProcVar("TProcA", {',
  12810. ' procsig: rtl.newTIProcSig(null, $mod.$rtti["TClass"])',
  12811. '});',
  12812. 'rtl.createClass($mod, "TObject", null, function () {',
  12813. ' this.$init = function () {',
  12814. ' this.C = null;',
  12815. ' };',
  12816. ' this.$final = function () {',
  12817. ' this.C = undefined;',
  12818. ' };',
  12819. ' var $r = this.$rtti;',
  12820. ' $r.addField("C", $mod.$rtti["TClass"]);',
  12821. '});',
  12822. '$mod.$rtti.$Class("TFox");',
  12823. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  12824. '});',
  12825. '$mod.$rtti.$ClassRef("TBirds", {',
  12826. ' instancetype: $mod.$rtti["TBird"]',
  12827. '});',
  12828. 'rtl.createClass($mod, "TFox", $mod.TObject, function () {',
  12829. '});',
  12830. '$mod.$rtti.$ClassRef("TFoxes", {',
  12831. ' instancetype: $mod.$rtti["TFox"]',
  12832. '});',
  12833. '$mod.$rtti.$Class("TCow");',
  12834. '$mod.$rtti.$ClassRef("TCows", {',
  12835. ' instancetype: $mod.$rtti["TCow"]',
  12836. '});',
  12837. 'rtl.createClass($mod, "TCow", $mod.TObject, function () {',
  12838. '});',
  12839. '']),
  12840. LinesToStr([ // $mod.$main
  12841. '']));
  12842. end;
  12843. procedure TTestModule.TestRTTI_Record;
  12844. begin
  12845. Converter.Options:=Converter.Options-[coNoTypeInfo];
  12846. StartProgram(false);
  12847. Add('type');
  12848. Add(' integer = longint;');
  12849. Add(' TPoint = record');
  12850. Add(' x,y: integer;');
  12851. Add(' end;');
  12852. Add('var p: pointer;');
  12853. Add(' r: tpoint;');
  12854. Add('begin');
  12855. Add(' p:=typeinfo(tpoint);');
  12856. Add(' p:=typeinfo(r);');
  12857. Add(' p:=typeinfo(r.x);');
  12858. ConvertProgram;
  12859. CheckSource('TestRTTI_Record',
  12860. LinesToStr([ // statements
  12861. 'this.TPoint = function (s) {',
  12862. ' if (s) {',
  12863. ' this.x = s.x;',
  12864. ' this.y = s.y;',
  12865. ' } else {',
  12866. ' this.x = 0;',
  12867. ' this.y = 0;',
  12868. ' };',
  12869. ' this.$equal = function (b) {',
  12870. ' return (this.x == b.x) && (this.y == b.y);',
  12871. ' };',
  12872. '};',
  12873. '$mod.$rtti.$Record("TPoint", {}).addFields("x", rtl.longint, "y", rtl.longint);',
  12874. 'this.p = null;',
  12875. 'this.r = new $mod.TPoint();',
  12876. '']),
  12877. LinesToStr([ // $mod.$main
  12878. '$mod.p = $mod.$rtti["TPoint"];',
  12879. '$mod.p = $mod.$rtti["TPoint"];',
  12880. '$mod.p = rtl.longint;',
  12881. '']));
  12882. end;
  12883. procedure TTestModule.TestRTTI_LocalTypes;
  12884. begin
  12885. Converter.Options:=Converter.Options-[coNoTypeInfo];
  12886. StartProgram(false);
  12887. Add('procedure DoIt;');
  12888. Add('type');
  12889. Add(' integer = longint;');
  12890. Add(' TPoint = record');
  12891. Add(' x,y: integer;');
  12892. Add(' end;');
  12893. Add('begin');
  12894. Add('end;');
  12895. Add('begin');
  12896. ConvertProgram;
  12897. CheckSource('TestRTTI_LocalTypes',
  12898. LinesToStr([ // statements
  12899. 'this.DoIt = function () {',
  12900. ' this.TPoint = function (s) {',
  12901. ' if (s) {',
  12902. ' this.x = s.x;',
  12903. ' this.y = s.y;',
  12904. ' } else {',
  12905. ' this.x = 0;',
  12906. ' this.y = 0;',
  12907. ' };',
  12908. ' this.$equal = function (b) {',
  12909. ' return (this.x == b.x) && (this.y == b.y);',
  12910. ' };',
  12911. ' };',
  12912. '};',
  12913. '']),
  12914. LinesToStr([ // $mod.$main
  12915. '']));
  12916. end;
  12917. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  12918. begin
  12919. Converter.Options:=Converter.Options-[coNoTypeInfo];
  12920. StartProgram(false);
  12921. Add('type');
  12922. Add(' TCaption = string;');
  12923. Add(' TYesNo = boolean;');
  12924. Add(' TLetter = char;');
  12925. Add(' TFloat = double;');
  12926. Add(' TPtr = pointer;');
  12927. Add(' TShortInt = shortint;');
  12928. Add(' TByte = byte;');
  12929. Add(' TSmallInt = smallint;');
  12930. Add(' TWord = word;');
  12931. Add(' TInt32 = longint;');
  12932. Add(' TDWord = longword;');
  12933. Add(' TValue = jsvalue;');
  12934. Add('var p: TPtr;');
  12935. Add('begin');
  12936. Add(' p:=typeinfo(string);');
  12937. Add(' p:=typeinfo(tcaption);');
  12938. Add(' p:=typeinfo(boolean);');
  12939. Add(' p:=typeinfo(tyesno);');
  12940. Add(' p:=typeinfo(char);');
  12941. Add(' p:=typeinfo(tletter);');
  12942. Add(' p:=typeinfo(double);');
  12943. Add(' p:=typeinfo(tfloat);');
  12944. Add(' p:=typeinfo(pointer);');
  12945. Add(' p:=typeinfo(tptr);');
  12946. Add(' p:=typeinfo(shortint);');
  12947. Add(' p:=typeinfo(tshortint);');
  12948. Add(' p:=typeinfo(byte);');
  12949. Add(' p:=typeinfo(tbyte);');
  12950. Add(' p:=typeinfo(smallint);');
  12951. Add(' p:=typeinfo(tsmallint);');
  12952. Add(' p:=typeinfo(word);');
  12953. Add(' p:=typeinfo(tword);');
  12954. Add(' p:=typeinfo(longword);');
  12955. Add(' p:=typeinfo(tdword);');
  12956. Add(' p:=typeinfo(jsvalue);');
  12957. Add(' p:=typeinfo(tvalue);');
  12958. ConvertProgram;
  12959. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  12960. LinesToStr([ // statements
  12961. 'this.p = null;',
  12962. '']),
  12963. LinesToStr([ // $mod.$main
  12964. '$mod.p = rtl.string;',
  12965. '$mod.p = rtl.string;',
  12966. '$mod.p = rtl.boolean;',
  12967. '$mod.p = rtl.boolean;',
  12968. '$mod.p = rtl.char;',
  12969. '$mod.p = rtl.char;',
  12970. '$mod.p = rtl.double;',
  12971. '$mod.p = rtl.double;',
  12972. '$mod.p = rtl.pointer;',
  12973. '$mod.p = rtl.pointer;',
  12974. '$mod.p = rtl.shortint;',
  12975. '$mod.p = rtl.shortint;',
  12976. '$mod.p = rtl.byte;',
  12977. '$mod.p = rtl.byte;',
  12978. '$mod.p = rtl.smallint;',
  12979. '$mod.p = rtl.smallint;',
  12980. '$mod.p = rtl.word;',
  12981. '$mod.p = rtl.word;',
  12982. '$mod.p = rtl.longword;',
  12983. '$mod.p = rtl.longword;',
  12984. '$mod.p = rtl.jsvalue;',
  12985. '$mod.p = rtl.jsvalue;',
  12986. '']));
  12987. end;
  12988. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  12989. begin
  12990. Converter.Options:=Converter.Options-[coNoTypeInfo];
  12991. StartProgram(false);
  12992. Add('procedure DoIt;');
  12993. Add('type');
  12994. Add(' integer = longint;');
  12995. Add(' TPoint = record');
  12996. Add(' x,y: integer;');
  12997. Add(' end;');
  12998. Add('var p: pointer;');
  12999. Add('begin');
  13000. Add(' p:=typeinfo(tpoint);');
  13001. Add('end;');
  13002. Add('begin');
  13003. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  13004. ConvertProgram;
  13005. end;
  13006. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  13007. begin
  13008. Converter.Options:=Converter.Options-[coNoTypeInfo];
  13009. StartProgram(false);
  13010. Add('{$modeswitch externalclass}');
  13011. Add('type');
  13012. Add(' TTypeInfo = class external name ''rtl.tTypeInfo'' end;');
  13013. Add(' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;');
  13014. Add(' TFlag = (up,down);');
  13015. Add(' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;');
  13016. Add(' TFlags = set of TFlag;');
  13017. Add(' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;');
  13018. Add('var');
  13019. Add(' ti: TTypeInfo;');
  13020. Add(' tiInt: TTypeInfoInteger;');
  13021. Add(' tiEnum: TTypeInfoEnum;');
  13022. Add(' tiSet: TTypeInfoSet;');
  13023. Add('begin');
  13024. Add(' ti:=typeinfo(string);');
  13025. Add(' ti:=typeinfo(boolean);');
  13026. Add(' ti:=typeinfo(char);');
  13027. Add(' ti:=typeinfo(double);');
  13028. Add(' tiInt:=typeinfo(shortint);');
  13029. Add(' tiInt:=typeinfo(byte);');
  13030. Add(' tiInt:=typeinfo(smallint);');
  13031. Add(' tiInt:=typeinfo(word);');
  13032. Add(' tiInt:=typeinfo(longint);');
  13033. Add(' tiInt:=typeinfo(longword);');
  13034. Add(' ti:=typeinfo(jsvalue);');
  13035. Add(' tiEnum:=typeinfo(tflag);');
  13036. Add(' tiSet:=typeinfo(tflags);');
  13037. ConvertProgram;
  13038. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  13039. LinesToStr([ // statements
  13040. 'this.TFlag = {',
  13041. ' "0": "up",',
  13042. ' up: 0,',
  13043. ' "1": "down",',
  13044. ' down: 1',
  13045. '};',
  13046. '$mod.$rtti.$Enum("TFlag", {',
  13047. ' minvalue: 0,',
  13048. ' maxvalue: 1,',
  13049. ' enumtype: this.TFlag',
  13050. '});',
  13051. '$mod.$rtti.$Set("TFlags", {',
  13052. ' comptype: $mod.$rtti["TFlag"]',
  13053. '});',
  13054. 'this.ti = null;',
  13055. 'this.tiInt = null;',
  13056. 'this.tiEnum = null;',
  13057. 'this.tiSet = null;',
  13058. '']),
  13059. LinesToStr([ // $mod.$main
  13060. '$mod.ti = rtl.string;',
  13061. '$mod.ti = rtl.boolean;',
  13062. '$mod.ti = rtl.char;',
  13063. '$mod.ti = rtl.double;',
  13064. '$mod.tiInt = rtl.shortint;',
  13065. '$mod.tiInt = rtl.byte;',
  13066. '$mod.tiInt = rtl.smallint;',
  13067. '$mod.tiInt = rtl.word;',
  13068. '$mod.tiInt = rtl.longint;',
  13069. '$mod.tiInt = rtl.longword;',
  13070. '$mod.ti = rtl.jsvalue;',
  13071. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  13072. '$mod.tiSet = $mod.$rtti["TFlags"];',
  13073. '']));
  13074. end;
  13075. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  13076. begin
  13077. Converter.Options:=Converter.Options-[coNoTypeInfo];
  13078. StartProgram(false);
  13079. Add('{$modeswitch externalclass}');
  13080. Add('type');
  13081. Add(' TTypeInfo = class external name ''rtl.tTypeInfo'' end;');
  13082. Add(' TStaticArr = array[boolean] of string;');
  13083. Add(' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;');
  13084. Add(' TDynArr = array of string;');
  13085. Add(' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;');
  13086. Add(' TProc = procedure;');
  13087. Add(' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;');
  13088. Add(' TMethod = procedure of object;');
  13089. Add(' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;');
  13090. Add('var');
  13091. Add(' StaticArray: TStaticArr;');
  13092. Add(' tiStaticArray: TTypeInfoStaticArray;');
  13093. Add(' DynArray: TDynArr;');
  13094. Add(' tiDynArray: TTypeInfoDynArray;');
  13095. Add(' ProcVar: TProc;');
  13096. Add(' tiProcVar: TTypeInfoProcVar;');
  13097. Add(' MethodVar: TMethod;');
  13098. Add(' tiMethodVar: TTypeInfoMethodVar;');
  13099. Add('begin');
  13100. Add(' tiStaticArray:=typeinfo(StaticArray);');
  13101. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  13102. Add(' tiDynArray:=typeinfo(DynArray);');
  13103. Add(' tiDynArray:=typeinfo(TDynArr);');
  13104. Add(' tiProcVar:=typeinfo(ProcVar);');
  13105. Add(' tiProcVar:=typeinfo(TProc);');
  13106. Add(' tiMethodVar:=typeinfo(MethodVar);');
  13107. Add(' tiMethodVar:=typeinfo(TMethod);');
  13108. ConvertProgram;
  13109. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  13110. LinesToStr([ // statements
  13111. ' $mod.$rtti.$StaticArray("TStaticArr", {',
  13112. ' dims: [2],',
  13113. ' eltype: rtl.string',
  13114. '});',
  13115. '$mod.$rtti.$DynArray("TDynArr", {',
  13116. ' eltype: rtl.string',
  13117. '});',
  13118. '$mod.$rtti.$ProcVar("TProc", {',
  13119. ' procsig: rtl.newTIProcSig(null)',
  13120. '});',
  13121. '$mod.$rtti.$MethodVar("TMethod", {',
  13122. ' procsig: rtl.newTIProcSig(null),',
  13123. ' methodkind: 0',
  13124. '});',
  13125. 'this.StaticArray = rtl.arrayNewMultiDim([2], "");',
  13126. 'this.tiStaticArray = null;',
  13127. 'this.DynArray = [];',
  13128. 'this.tiDynArray = null;',
  13129. 'this.ProcVar = null;',
  13130. 'this.tiProcVar = null;',
  13131. 'this.MethodVar = null;',
  13132. 'this.tiMethodVar = null;',
  13133. '']),
  13134. LinesToStr([ // $mod.$main
  13135. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  13136. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  13137. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  13138. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  13139. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  13140. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  13141. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  13142. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  13143. '']));
  13144. end;
  13145. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  13146. begin
  13147. Converter.Options:=Converter.Options-[coNoTypeInfo];
  13148. StartProgram(false);
  13149. Add('{$modeswitch externalclass}');
  13150. Add('type');
  13151. Add(' TTypeInfo = class external name ''rtl.tTypeInfo'' end;');
  13152. Add(' TRec = record end;');
  13153. Add(' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;');
  13154. // ToDo: ^PRec
  13155. Add(' TObject = class end;');
  13156. Add(' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;');
  13157. Add(' TClass = class of tobject;');
  13158. Add(' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;');
  13159. Add(' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;');
  13160. Add('var');
  13161. Add(' Rec: trec;');
  13162. Add(' tiRecord: ttypeinforecord;');
  13163. Add(' Obj: tobject;');
  13164. Add(' tiClass: ttypeinfoclass;');
  13165. Add(' aClass: tclass;');
  13166. Add(' tiClassRef: ttypeinfoclassref;');
  13167. // ToDo: ^PRec
  13168. Add(' tiPointer: ttypeinfopointer;');
  13169. Add('begin');
  13170. Add(' tirecord:=typeinfo(trec);');
  13171. Add(' tirecord:=typeinfo(trec);');
  13172. Add(' ticlass:=typeinfo(obj);');
  13173. Add(' ticlass:=typeinfo(tobject);');
  13174. Add(' ticlass:=typeinfo(aclass);');
  13175. Add(' ticlassref:=typeinfo(tclass);');
  13176. ConvertProgram;
  13177. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  13178. LinesToStr([ // statements
  13179. 'this.TRec = function (s) {',
  13180. '};',
  13181. '$mod.$rtti.$Record("TRec", {});',
  13182. 'rtl.createClass($mod, "TObject", null, function () {',
  13183. ' this.$init = function () {',
  13184. ' };',
  13185. ' this.$final = function () {',
  13186. ' };',
  13187. '});',
  13188. '$mod.$rtti.$ClassRef("TClass", {',
  13189. ' instancetype: $mod.$rtti["TObject"]',
  13190. '});',
  13191. 'this.Rec = new $mod.TRec();',
  13192. 'this.tiRecord = null;',
  13193. 'this.Obj = null;',
  13194. 'this.tiClass = null;',
  13195. 'this.aClass = null;',
  13196. 'this.tiClassRef = null;',
  13197. 'this.tiPointer = null;',
  13198. '']),
  13199. LinesToStr([ // $mod.$main
  13200. '$mod.tiRecord = $mod.$rtti["TRec"];',
  13201. '$mod.tiRecord = $mod.$rtti["TRec"];',
  13202. '$mod.tiClass = $mod.Obj.$rtti;',
  13203. '$mod.tiClass = $mod.$rtti["TObject"];',
  13204. '$mod.tiClass = $mod.aClass.$rtti;',
  13205. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  13206. '']));
  13207. end;
  13208. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  13209. begin
  13210. Converter.Options:=Converter.Options-[coNoTypeInfo];
  13211. StartProgram(false);
  13212. Add([
  13213. '{$modeswitch externalclass}',
  13214. 'type',
  13215. ' TClass = class of tobject;',
  13216. ' TObject = class',
  13217. ' function MyClass: TClass;',
  13218. ' class function ClassType: TClass;',
  13219. ' end;',
  13220. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  13221. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  13222. 'function TObject.MyClass: TClass;',
  13223. 'var t: TTypeInfoClass;',
  13224. 'begin',
  13225. ' t:=TypeInfo(Self);',
  13226. ' t:=TypeInfo(Result);',
  13227. 'end;',
  13228. 'class function TObject.ClassType: TClass;',
  13229. 'var t: TTypeInfoClass;',
  13230. 'begin',
  13231. ' t:=TypeInfo(Self);',
  13232. ' t:=TypeInfo(Result);',
  13233. 'end;',
  13234. 'var',
  13235. ' Obj: TObject;',
  13236. ' t: TTypeInfoClass;',
  13237. 'begin',
  13238. ' t:=TypeInfo(TObject.ClassType);',
  13239. ' t:=TypeInfo(Obj.ClassType);',
  13240. ' t:=TypeInfo(Obj.MyClass);',
  13241. '']);
  13242. ConvertProgram;
  13243. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  13244. LinesToStr([ // statements
  13245. '$mod.$rtti.$Class("TObject");',
  13246. '$mod.$rtti.$ClassRef("TClass", {',
  13247. ' instancetype: $mod.$rtti["TObject"]',
  13248. '});',
  13249. 'rtl.createClass($mod, "TObject", null, function () {',
  13250. ' this.$init = function () {',
  13251. ' };',
  13252. ' this.$final = function () {',
  13253. ' };',
  13254. ' this.MyClass = function () {',
  13255. ' var Result = null;',
  13256. ' var t = null;',
  13257. ' t = this.$rtti;',
  13258. ' t = Result.$rtti;',
  13259. ' return Result;',
  13260. ' };',
  13261. ' this.ClassType = function () {',
  13262. ' var Result = null;',
  13263. ' var t = null;',
  13264. ' t = this.$rtti;',
  13265. ' t = Result.$rtti;',
  13266. ' return Result;',
  13267. ' };',
  13268. '});',
  13269. 'this.Obj = null;',
  13270. 'this.t = null;',
  13271. '']),
  13272. LinesToStr([ // $mod.$main
  13273. '$mod.t = $mod.TObject.ClassType().$rtti;',
  13274. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  13275. '$mod.t = $mod.Obj.MyClass().$rtti;',
  13276. '']));
  13277. end;
  13278. Initialization
  13279. RegisterTests([TTestModule]);
  13280. end.