123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689 |
- {
- This file is part of the Free Component Library (FCL)
- Copyright (c) 2014 by Michael Van Canneyt
- Unit tests for Pascal-to-Javascript converter class.
- See the file COPYING.FPC, included in this distribution,
- for details about the copyright.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- **********************************************************************
- Examples:
- ./testpas2js --suite=TTestModule.TestEmptyProgram
- ./testpas2js --suite=TTestModule.TestEmptyUnit
- }
- unit tcmodules;
- {$mode objfpc}{$H+}
- interface
- uses
- Classes, SysUtils, fpcunit, testregistry, contnrs, fppas2js, pastree,
- PScanner, PasResolver, PParser, PasResolveEval, jstree, jswriter, jsbase;
- const
- // default parser+scanner options
- po_pas2js = [po_asmwhole,po_resolvestandardtypes];
- co_tcmodules = [coNoTypeInfo];
- type
- { TTestPasParser }
- TTestPasParser = Class(TPasParser)
- end;
- TOnFindUnit = function(const aUnitName: String): TPasModule of object;
- { TTestEnginePasResolver }
- TTestEnginePasResolver = class(TPas2JsResolver)
- private
- FFilename: string;
- FModule: TPasModule;
- FOnFindUnit: TOnFindUnit;
- FParser: TTestPasParser;
- FResolver: TStreamResolver;
- FScanner: TPascalScanner;
- FSource: string;
- procedure SetModule(AValue: TPasModule);
- public
- destructor Destroy; override;
- function FindModule(const AName: String): TPasModule; override;
- property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
- property Filename: string read FFilename write FFilename;
- property Resolver: TStreamResolver read FResolver write FResolver;
- property Scanner: TPascalScanner read FScanner write FScanner;
- property Parser: TTestPasParser read FParser write FParser;
- property Source: string read FSource write FSource;
- property Module: TPasModule read FModule write SetModule;
- end;
- { TCustomTestModule }
- TCustomTestModule = Class(TTestCase)
- private
- FConverter: TPasToJSConverter;
- FEngine: TTestEnginePasResolver;
- FExpectedErrorClass: ExceptClass;
- FExpectedErrorMsg: string;
- FExpectedErrorNumber: integer;
- FFilename: string;
- FFileResolver: TStreamResolver;
- FJSImplementationSrc: TJSSourceElements;
- FJSImplementationUses: TJSArrayLiteral;
- FJSInitBody: TJSFunctionBody;
- FJSImplentationUses: TJSArrayLiteral;
- FJSInterfaceUses: TJSArrayLiteral;
- FJSModule: TJSSourceElements;
- FJSModuleSrc: TJSSourceElements;
- FJSSource: TStringList;
- FModule: TPasModule;
- FJSModuleCallArgs: TJSArguments;
- FModules: TObjectList;// list of TTestEnginePasResolver
- FParser: TTestPasParser;
- FPasProgram: TPasProgram;
- FJSRegModuleCall: TJSCallExpression;
- FScanner: TPascalScanner;
- FSkipTests: boolean;
- FSource: TStringList;
- FFirstPasStatement: TPasImplBlock;
- function GetModuleCount: integer;
- function GetModules(Index: integer): TTestEnginePasResolver;
- function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
- function FindUnit(const aUnitName: String): TPasModule;
- protected
- procedure SetUp; override;
- procedure TearDown; override;
- Procedure Add(Line: string); virtual;
- Procedure Add(const Lines: array of string);
- Procedure StartParsing; virtual;
- procedure ParseModule; virtual;
- procedure ParseProgram; virtual;
- procedure ParseUnit; virtual;
- protected
- function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
- function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
- function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
- function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
- ImplementationSrc: string): TTestEnginePasResolver; virtual;
- procedure AddSystemUnit; virtual;
- procedure StartProgram(NeedSystemUnit: boolean); virtual;
- procedure StartUnit(NeedSystemUnit: boolean); virtual;
- procedure ConvertModule; virtual;
- procedure ConvertProgram; virtual;
- procedure ConvertUnit; virtual;
- procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
- function GetDottedIdentifier(El: TJSElement): string;
- procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
- ImplStatements: string = ''); virtual;
- procedure CheckDiff(Msg, Expected, Actual: string); virtual;
- procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
- procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
- procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
- procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
- function IsErrorExpected(E: Exception): boolean;
- procedure HandleScannerError(E: EScannerError);
- procedure HandleParserError(E: EParserError);
- procedure HandlePasResolveError(E: EPasResolve);
- procedure HandlePas2JSError(E: EPas2JS);
- procedure HandleException(E: Exception);
- procedure RaiseException(E: Exception);
- procedure WriteSources(const aFilename: string; aRow, aCol: integer);
- function GetDefaultNamespace: string;
- property PasProgram: TPasProgram Read FPasProgram;
- property Modules[Index: integer]: TTestEnginePasResolver read GetModules;
- property ModuleCount: integer read GetModuleCount;
- property Engine: TTestEnginePasResolver read FEngine;
- property Filename: string read FFilename;
- Property Module: TPasModule Read FModule;
- property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
- property Converter: TPasToJSConverter read FConverter;
- property JSSource: TStringList read FJSSource;
- property JSModule: TJSSourceElements read FJSModule;
- property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
- property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
- property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
- property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
- property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
- property JSInitBody: TJSFunctionBody read FJSInitBody;
- property JSImplementationSrc: TJSSourceElements read FJSImplementationSrc;
- property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
- property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
- property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
- property SkipTests: boolean read FSkipTests write FSkipTests;
- public
- property Source: TStringList read FSource;
- property FileResolver: TStreamResolver read FFileResolver;
- property Scanner: TPascalScanner read FScanner;
- property Parser: TTestPasParser read FParser;
- end;
- { TTestModule }
- TTestModule = class(TCustomTestModule)
- Published
- // modules
- Procedure TestEmptyProgram;
- Procedure TestEmptyProgramUseStrict;
- Procedure TestEmptyUnit;
- Procedure TestEmptyUnitUseStrict;
- Procedure TestDottedUnitNames;
- Procedure TestDottedUnitExpr;
- Procedure Test_ModeFPCFail;
- Procedure Test_ModeSwitchCBlocksFail;
- // vars/const
- Procedure TestVarInt;
- Procedure TestVarBaseTypes;
- Procedure TestBaseTypeSingleFail;
- Procedure TestBaseTypeExtendedFail;
- Procedure TestConstBaseTypes;
- Procedure TestUnitImplVars;
- Procedure TestUnitImplConsts;
- Procedure TestUnitImplRecord;
- Procedure TestRenameJSNameConflict;
- Procedure TestLocalConst;
- Procedure TestVarExternal;
- Procedure TestVarExternalOtherUnit;
- // numbers
- Procedure TestDouble;
- // strings
- Procedure TestCharConst;
- Procedure TestChar_Compare;
- Procedure TestChar_Ord;
- Procedure TestChar_Chr;
- Procedure TestStringConst;
- Procedure TestString_Length;
- Procedure TestString_Compare;
- Procedure TestString_SetLength;
- Procedure TestString_CharAt;
- Procedure TestStr;
- Procedure TestBaseType_AnsiStringFail;
- Procedure TestBaseType_UnicodeStringFail;
- Procedure TestBaseType_ShortStringFail;
- Procedure TestBaseType_RawByteStringFail;
- Procedure TestTypeShortstring_Fail;
- // alias types
- Procedure TestAliasTypeRef;
- Procedure TestTypeCast_BaseTypes;
- Procedure TestTypeCast_AliasBaseTypes;
- // functions
- Procedure TestEmptyProc;
- Procedure TestProcOneParam;
- Procedure TestFunctionWithoutParams;
- Procedure TestProcedureWithoutParams;
- Procedure TestPrgProcVar;
- Procedure TestProcTwoArgs;
- Procedure TestProc_DefaultValue;
- Procedure TestUnitProcVar;
- Procedure TestImplProc;
- Procedure TestFunctionResult;
- Procedure TestNestedProc;
- Procedure TestForwardProc;
- Procedure TestNestedForwardProc;
- Procedure TestAssignFunctionResult;
- Procedure TestFunctionResultInCondition;
- Procedure TestExit;
- Procedure TestBreak;
- Procedure TestContinue;
- Procedure TestProc_External;
- Procedure TestProc_ExternalOtherUnit;
- Procedure TestProc_Asm;
- Procedure TestProc_Assembler;
- Procedure TestProc_VarParam;
- Procedure TestProc_Overload;
- Procedure TestProc_OverloadForward;
- Procedure TestProc_OverloadUnit;
- Procedure TestProc_OverloadNested;
- Procedure TestProc_Varargs;
- // enums, sets
- Procedure TestEnum_Name;
- Procedure TestEnum_Number;
- Procedure TestEnum_Functions;
- Procedure TestEnum_AsParams;
- Procedure TestSet;
- Procedure TestSet_Operators;
- Procedure TestSet_Operator_In;
- Procedure TestSet_Functions;
- Procedure TestSet_PassAsArgClone;
- Procedure TestSet_AsParams;
- Procedure TestSet_Property;
- Procedure TestSet_EnumConst;
- Procedure TestSet_AnonymousEnumType;
- Procedure TestSet_CharFail;
- Procedure TestSet_BooleanFail;
- Procedure TestSet_ConstEnum;
- Procedure TestSet_ConstChar;
- // statements
- Procedure TestNestBegin;
- Procedure TestIncDec;
- Procedure TestAssignments;
- Procedure TestArithmeticOperators1;
- Procedure TestLogicalOperators;
- Procedure TestBitwiseOperators;
- Procedure TestFunctionInt;
- Procedure TestFunctionString;
- Procedure TestForLoop;
- Procedure TestForLoopInFunction;
- Procedure TestForLoop_ReadVarAfter;
- Procedure TestForLoop_Nested;
- Procedure TestRepeatUntil;
- Procedure TestAsmBlock;
- Procedure TestAsmPas_Impl; // ToDo
- Procedure TestTryFinally;
- Procedure TestTryExcept;
- Procedure TestCaseOf;
- Procedure TestCaseOf_UseSwitch;
- Procedure TestCaseOfNoElse;
- Procedure TestCaseOfNoElse_UseSwitch;
- Procedure TestCaseOfRange;
- // arrays
- Procedure TestArray_Dynamic;
- Procedure TestArray_Dynamic_Nil;
- Procedure TestArray_DynMultiDimensional;
- Procedure TestArrayOfRecord;
- // ToDo: Procedure TestArrayOfSet;
- Procedure TestArray_AsParams;
- Procedure TestArrayElement_AsParams;
- Procedure TestArrayElementFromFuncResult_AsParams;
- Procedure TestArrayEnumTypeRange;
- Procedure TestArray_SetLengthOutArg;
- Procedure TestArray_SetLengthProperty;
- Procedure TestArray_OpenArrayOfString;
- Procedure TestArray_Concat;
- Procedure TestArray_Copy;
- Procedure TestArray_InsertDelete;
- Procedure TestArray_DynArrayConst;
- Procedure TestExternalClass_TypeCastArrayToExternalArray;
- Procedure TestExternalClass_TypeCastArrayFromExternalArray;
- // ToDo: static array const
- // ToDo: SetLength(array of static array)
- // ToDo: SetLength(dim1,dim2)
- // record
- Procedure TestRecord_Var;
- Procedure TestWithRecordDo;
- Procedure TestRecord_Assign;
- Procedure TestRecord_PassAsArgClone;
- Procedure TestRecord_AsParams;
- Procedure TestRecordElement_AsParams;
- Procedure TestRecordElementFromFuncResult_AsParams;
- Procedure TestRecordElementFromWith_AsParams;
- Procedure TestRecord_Equal;
- Procedure TestRecord_TypeCastJSValueToRecord;
- // ToDo: const record
- // classes
- Procedure TestClass_TObjectDefaultConstructor;
- Procedure TestClass_TObjectConstructorWithParams;
- Procedure TestClass_Var;
- Procedure TestClass_Method;
- Procedure TestClass_Implementation;
- Procedure TestClass_Inheritance;
- Procedure TestClass_AbstractMethod;
- Procedure TestClass_CallInherited_NoParams;
- Procedure TestClass_CallInherited_WithParams;
- Procedure TestClasS_CallInheritedConstructor;
- Procedure TestClass_ClassVar;
- Procedure TestClass_CallClassMethod;
- Procedure TestClass_Property;
- Procedure TestClass_Property_ClassMethod;
- Procedure TestClass_Property_Index;
- Procedure TestClass_PropertyOfTypeArray;
- Procedure TestClass_PropertyDefault;
- Procedure TestClass_PropertyOverride;
- Procedure TestClass_Assigned;
- Procedure TestClass_WithClassDoCreate;
- Procedure TestClass_WithClassInstDoProperty;
- Procedure TestClass_WithClassInstDoPropertyWithParams;
- Procedure TestClass_WithClassInstDoFunc;
- Procedure TestClass_TypeCast;
- Procedure TestClass_TypeCastUntypedParam;
- Procedure TestClass_Overloads;
- Procedure TestClass_OverloadsAncestor;
- Procedure TestClass_OverloadConstructor;
- Procedure TestClass_ReintroducedVar;
- Procedure TestClass_RaiseDescendant;
- Procedure TestClass_ExternalMethod;
- Procedure TestClass_ExternalVirtualNameMismatchFail;
- Procedure TestClass_ExternalOverrideFail;
- Procedure TestClass_ExternalVar;
- Procedure TestClass_Const;
- Procedure TestClass_LocalVarSelfFail;
- Procedure TestClass_ArgSelfFail;
- Procedure TestClass_NestedSelf;
- Procedure TestClass_NestedClassSelf;
- Procedure TestClass_NestedCallInherited;
- Procedure TestClass_TObjectFree;
- Procedure TestClass_TObjectFreeNewInstance;
- Procedure TestClass_TObjectFreeLowerCase;
- Procedure TestClass_TObjectFreeFunctionFail;
- Procedure TestClass_TObjectFreePropertyFail;
- // class of
- Procedure TestClassOf_Create;
- Procedure TestClassOf_Call;
- Procedure TestClassOf_Assign;
- Procedure TestClassOf_Is;
- Procedure TestClassOf_Compare;
- Procedure TestClassOf_ClassVar;
- Procedure TestClassOf_ClassMethod;
- Procedure TestClassOf_ClassProperty;
- Procedure TestClassOf_ClassMethodSelf;
- Procedure TestClassOf_TypeCast;
- Procedure TestClassOf_ImplicitFunctionCall;
- // nested class
- Procedure TestNestedClass_Fail;
- // external class
- Procedure TestExternalClass_Var;
- //ToDo Procedure TestExternalClass_Const;
- Procedure TestExternalClass_Dollar;
- Procedure TestExternalClass_DuplicateVarFail;
- Procedure TestExternalClass_Method;
- Procedure TestExternalClass_NonExternalOverride;
- Procedure TestExternalClass_Property;
- Procedure TestExternalClass_ClassProperty;
- Procedure TestExternalClass_ClassOf;
- Procedure TestExternalClass_ClassOtherUnit;
- Procedure TestExternalClass_Is;
- Procedure TestExternalClass_As;
- Procedure TestExternalClass_DestructorFail;
- Procedure TestExternalClass_New;
- Procedure TestExternalClass_ClassOf_New;
- Procedure TestExternalClass_FuncClassOf_New;
- Procedure TestExternalClass_LocalConstSameName;
- Procedure TestExternalClass_ReintroduceOverload;
- Procedure TestExternalClass_Inherited;
- Procedure TestExternalClass_PascalAncestorFail;
- Procedure TestExternalClass_NewInstance;
- Procedure TestExternalClass_NewInstance_NonVirtualFail;
- Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
- Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
- Procedure TestExternalClass_PascalProperty;
- Procedure TestExternalClass_TypeCastToRootClass;
- Procedure TestExternalClass_TypeCastStringToExternalString;
- Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
- Procedure TestExternalClass_BracketAccessor;
- Procedure TestExternalClass_BracketAccessor_2ParamsFail;
- Procedure TestExternalClass_BracketAccessor_ReadOnly;
- Procedure TestExternalClass_BracketAccessor_WriteOnly;
- Procedure TestExternalClass_BracketAccessor_MultiType;
- Procedure TestExternalClass_BracketAccessor_Index;
- // proc types
- Procedure TestProcType;
- Procedure TestProcType_FunctionFPC;
- Procedure TestProcType_FunctionDelphi;
- Procedure TestProcType_AsParam;
- Procedure TestProcType_MethodFPC;
- Procedure TestProcType_MethodDelphi;
- Procedure TestProcType_PropertyFPC;
- Procedure TestProcType_PropertyDelphi;
- Procedure TestProcType_WithClassInstDoPropertyFPC;
- Procedure TestProcType_Nested;
- Procedure TestProcType_NestedOfObject;
- Procedure TestProcType_ReferenceToProc;
- Procedure TestProcType_ReferenceToMethod;
- Procedure TestProcType_Typecast;
- Procedure TestProcType_PassProcToUntyped;
- // pointer
- Procedure TestPointer;
- Procedure TestPointer_Proc;
- Procedure TestPointer_AssignRecordFail;
- Procedure TestPointer_AssignStaticArrayFail;
- Procedure TestPointer_ArrayParamsFail;
- Procedure TestPointer_TypeCastJSValueToPointer;
- // jsvalue
- Procedure TestJSValue_AssignToJSValue;
- Procedure TestJSValue_TypeCastToBaseType;
- Procedure TestJSValue_Equal;
- Procedure TestJSValue_If;
- Procedure TestJSValue_Enum;
- Procedure TestJSValue_ClassInstance;
- Procedure TestJSValue_ClassOf;
- Procedure TestJSValue_ArrayOfJSValue;
- Procedure TestJSValue_Params;
- Procedure TestJSValue_UntypedParam;
- Procedure TestJSValue_FuncResultType;
- Procedure TestJSValue_ProcType_Assign;
- Procedure TestJSValue_ProcType_Equal;
- Procedure TestJSValue_AssignToPointerFail;
- Procedure TestJSValue_OverloadDouble;
- Procedure TestJSValue_OverloadNativeInt;
- Procedure TestJSValue_OverloadWord;
- Procedure TestJSValue_OverloadString;
- Procedure TestJSValue_OverloadChar;
- Procedure TestJSValue_OverloadPointer;
- // RTTI
- Procedure TestRTTI_ProcType;
- Procedure TestRTTI_ProcType_ArgFromOtherUnit;
- Procedure TestRTTI_EnumAndSetType;
- Procedure TestRTTI_AnonymousEnumType;
- Procedure TestRTTI_StaticArray;
- Procedure TestRTTI_DynArray;
- Procedure TestRTTI_ArrayNestedAnonymous;
- // ToDo: Procedure TestRTTI_Pointer;
- Procedure TestRTTI_PublishedMethodOverloadFail;
- Procedure TestRTTI_PublishedMethodExternalFail;
- Procedure TestRTTI_PublishedClassPropertyFail;
- Procedure TestRTTI_PublishedClassFieldFail;
- Procedure TestRTTI_PublishedFieldExternalFail;
- Procedure TestRTTI_Class_Field;
- Procedure TestRTTI_Class_Method;
- Procedure TestRTTI_Class_MethodArgFlags;
- Procedure TestRTTI_Class_Property;
- Procedure TestRTTI_Class_PropertyParams;
- // ToDo: property default value
- Procedure TestRTTI_OverrideMethod;
- Procedure TestRTTI_OverloadProperty;
- // ToDo: array argument
- Procedure TestRTTI_ClassForward;
- Procedure TestRTTI_ClassOf;
- Procedure TestRTTI_Record;
- Procedure TestRTTI_LocalTypes;
- Procedure TestRTTI_TypeInfo_BaseTypes;
- Procedure TestRTTI_TypeInfo_LocalFail;
- Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
- Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
- Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
- Procedure TestRTTI_TypeInfo_FunctionClassType;
- end;
- function LinesToStr(Args: array of const): string;
- function ExtractFileUnitName(aFilename: string): string;
- function JSToStr(El: TJSElement): string;
- implementation
- function LinesToStr(Args: array of const): string;
- var
- s: String;
- i: Integer;
- begin
- s:='';
- for i:=Low(Args) to High(Args) do
- case Args[i].VType of
- vtChar: s += Args[i].VChar+LineEnding;
- vtString: s += Args[i].VString^+LineEnding;
- vtPChar: s += Args[i].VPChar+LineEnding;
- vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
- vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
- vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
- vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
- vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
- end;
- Result:=s;
- end;
- function ExtractFileUnitName(aFilename: string): string;
- var
- p: Integer;
- begin
- Result:=ExtractFileName(aFilename);
- if Result='' then exit;
- for p:=length(Result) downto 1 do
- case Result[p] of
- '/','\': exit;
- '.':
- begin
- Delete(Result,p,length(Result));
- exit;
- end;
- end;
- end;
- function JSToStr(El: TJSElement): string;
- var
- aWriter: TBufferWriter;
- aJSWriter: TJSWriter;
- begin
- aWriter:=TBufferWriter.Create(1000);
- try
- aJSWriter:=TJSWriter.Create(aWriter);
- aJSWriter.IndentSize:=2;
- aJSWriter.WriteJS(El);
- Result:=aWriter.AsAnsistring;
- finally
- aWriter.Free;
- end;
- end;
- { TTestEnginePasResolver }
- procedure TTestEnginePasResolver.SetModule(AValue: TPasModule);
- begin
- if FModule=AValue then Exit;
- if Module<>nil then
- Module.Release;
- FModule:=AValue;
- if Module<>nil then
- Module.AddRef;
- end;
- destructor TTestEnginePasResolver.Destroy;
- begin
- FreeAndNil(FResolver);
- Module:=nil;
- FreeAndNil(FParser);
- FreeAndNil(FScanner);
- FreeAndNil(FResolver);
- inherited Destroy;
- end;
- function TTestEnginePasResolver.FindModule(const AName: String): TPasModule;
- begin
- Result:=nil;
- if Assigned(OnFindUnit) then
- Result:=OnFindUnit(AName);
- end;
- { TCustomTestModule }
- function TCustomTestModule.GetModuleCount: integer;
- begin
- Result:=FModules.Count;
- end;
- function TCustomTestModule.GetModules(Index: integer
- ): TTestEnginePasResolver;
- begin
- Result:=TTestEnginePasResolver(FModules[Index]);
- end;
- function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
- ): TPasModule;
- var
- DefNamespace: String;
- begin
- //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
- if (Pos('.',aUnitName)<1) then
- begin
- DefNamespace:=GetDefaultNamespace;
- if DefNamespace<>'' then
- begin
- Result:=FindUnit(DefNamespace+'.'+aUnitName);
- if Result<>nil then exit;
- end;
- end;
- Result:=FindUnit(aUnitName);
- if Result<>nil then exit;
- writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
- Fail('can''t find unit "'+aUnitName+'"');
- end;
- function TCustomTestModule.FindUnit(const aUnitName: String): TPasModule;
- var
- i: Integer;
- CurEngine: TTestEnginePasResolver;
- CurUnitName: String;
- begin
- //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
- Result:=nil;
- for i:=0 to ModuleCount-1 do
- begin
- CurEngine:=Modules[i];
- CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
- //writeln('TTestModule.FindUnit Checking ',i,'/',ModuleCount,' ',CurEngine.Filename,' ',CurUnitName);
- if CompareText(aUnitName,CurUnitName)=0 then
- begin
- Result:=CurEngine.Module;
- if Result<>nil then exit;
- //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
- FileResolver.FindSourceFile(aUnitName);
- CurEngine.Resolver:=TStreamResolver.Create;
- CurEngine.Resolver.OwnsStreams:=True;
- //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
- CurEngine.Resolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
- CurEngine.Scanner:=TPascalScanner.Create(CurEngine.Resolver);
- CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.Resolver,CurEngine);
- CurEngine.Parser.Options:=CurEngine.Parser.Options+po_pas2js+[po_KeepScannerError];
- if CompareText(CurUnitName,'System')=0 then
- CurEngine.Parser.ImplicitUses.Clear;
- CurEngine.Scanner.OpenFile(CurEngine.Filename);
- try
- CurEngine.Parser.NextToken;
- CurEngine.Parser.ParseUnit(CurEngine.FModule);
- except
- on E: Exception do
- HandleException(E);
- end;
- //writeln('TTestModule.FindUnit END ',CurUnitName);
- Result:=CurEngine.Module;
- exit;
- end;
- end;
- end;
- procedure TCustomTestModule.SetUp;
- begin
- inherited SetUp;
- FSkipTests:=false;
- FSource:=TStringList.Create;
- FModules:=TObjectList.Create(true);
- FFilename:='test1.pp';
- FFileResolver:=TStreamResolver.Create;
- FFileResolver.OwnsStreams:=True;
- FScanner:=TPascalScanner.Create(FFileResolver);
- FScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
- FScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
- FScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
- FEngine:=AddModule(Filename);
- FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
- Parser.Options:=Parser.Options+po_pas2js+[po_KeepScannerError];
- FModule:=Nil;
- FConverter:=TPasToJSConverter.Create;
- FConverter.Options:=co_tcmodules;
- FExpectedErrorClass:=nil;
- end;
- procedure TCustomTestModule.TearDown;
- begin
- FSkipTests:=false;
- FJSModule:=nil;
- FJSRegModuleCall:=nil;
- FJSModuleCallArgs:=nil;
- FJSImplentationUses:=nil;
- FJSInterfaceUses:=nil;
- FJSModuleSrc:=nil;
- FJSInitBody:=nil;
- FreeAndNil(FJSSource);
- FreeAndNil(FJSModule);
- FreeAndNil(FConverter);
- Engine.Clear;
- if Assigned(FModule) then
- begin
- FModule.Release;
- FModule:=nil;
- end;
- FreeAndNil(FSource);
- FreeAndNil(FParser);
- FreeAndNil(FScanner);
- FreeAndNil(FFileResolver);
- if FModules<>nil then
- begin
- FreeAndNil(FModules);
- FEngine:=nil;
- end;
- inherited TearDown;
- end;
- procedure TCustomTestModule.Add(Line: string);
- begin
- Source.Add(Line);
- end;
- procedure TCustomTestModule.Add(const Lines: array of string);
- var
- i: Integer;
- begin
- for i:=low(Lines) to high(Lines) do
- Add(Lines[i]);
- end;
- procedure TCustomTestModule.StartParsing;
- var
- Src: String;
- begin
- Src:=Source.Text;
- FEngine.Source:=Src;
- FileResolver.AddStream(FileName,TStringStream.Create(Src));
- Scanner.OpenFile(FileName);
- Writeln('// Test : ',Self.TestName);
- Writeln(Src);
- end;
- procedure TCustomTestModule.ParseModule;
- begin
- if SkipTests then exit;
- FFirstPasStatement:=nil;
- try
- StartParsing;
- Parser.ParseMain(FModule);
- except
- on E: Exception do
- HandleException(E);
- end;
- if SkipTests then exit;
- AssertNotNull('Module resulted in Module',FModule);
- AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
- TAssert.AssertSame('Has resolver',Engine,Parser.Engine);
- end;
- procedure TCustomTestModule.ParseProgram;
- begin
- if SkipTests then exit;
- ParseModule;
- if SkipTests then exit;
- AssertEquals('Has program',TPasProgram,Module.ClassType);
- FPasProgram:=TPasProgram(Module);
- AssertNotNull('Has program section',PasProgram.ProgramSection);
- AssertNotNull('Has initialization section',PasProgram.InitializationSection);
- if (PasProgram.InitializationSection.Elements.Count>0) then
- if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
- FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
- end;
- procedure TCustomTestModule.ParseUnit;
- begin
- if SkipTests then exit;
- ParseModule;
- if SkipTests then exit;
- AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
- AssertNotNull('Has interface section',Module.InterfaceSection);
- AssertNotNull('Has implementation section',Module.ImplementationSection);
- if (Module.InitializationSection<>nil)
- and (Module.InitializationSection.Elements.Count>0)
- and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
- FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
- end;
- function TCustomTestModule.FindModuleWithFilename(aFilename: string
- ): TTestEnginePasResolver;
- var
- i: Integer;
- begin
- for i:=0 to ModuleCount-1 do
- if CompareText(Modules[i].Filename,aFilename)=0 then
- exit(Modules[i]);
- Result:=nil;
- end;
- function TCustomTestModule.AddModule(aFilename: string
- ): TTestEnginePasResolver;
- begin
- //writeln('TTestModuleConverter.AddModule ',aFilename);
- if FindModuleWithFilename(aFilename)<>nil then
- Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
- Result:=TTestEnginePasResolver.Create;
- Result.Filename:=aFilename;
- Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
- Result.OnFindUnit:=@OnPasResolverFindUnit;
- FModules.Add(Result);
- end;
- function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
- ): TTestEnginePasResolver;
- begin
- Result:=AddModule(aFilename);
- Result.Source:=Src;
- end;
- function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
- ImplementationSrc: string): TTestEnginePasResolver;
- var
- Src: String;
- begin
- Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
- Src+=LineEnding;
- Src+='interface'+LineEnding;
- Src+=LineEnding;
- Src+=InterfaceSrc;
- Src+='implementation'+LineEnding;
- Src+=LineEnding;
- Src+=ImplementationSrc;
- Src+='end.'+LineEnding;
- Result:=AddModuleWithSrc(aFilename,Src);
- end;
- procedure TCustomTestModule.AddSystemUnit;
- begin
- AddModuleWithIntfImplSrc('system.pp',
- // interface
- LinesToStr([
- 'type',
- ' integer=longint;',
- 'var',
- ' ExitCode: Longint;',
- ''
- // implementation
- ]),LinesToStr([
- ''
- ]));
- end;
- procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean);
- begin
- if NeedSystemUnit then
- AddSystemUnit
- else
- Parser.ImplicitUses.Clear;
- Add('program '+ExtractFileUnitName(Filename)+';');
- Add('');
- end;
- procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean);
- begin
- if NeedSystemUnit then
- AddSystemUnit
- else
- Parser.ImplicitUses.Clear;
- Add('unit Test1;');
- Add('');
- end;
- procedure TCustomTestModule.ConvertModule;
- procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
- out UsesLit: TJSArrayLiteral);
- var
- i: Integer;
- Item: TJSElement;
- Lit: TJSLiteral;
- begin
- UsesLit:=nil;
- AssertNotNull(UsesName+' uses section',Arg.Expr);
- if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
- exit; // null is ok
- AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
- FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
- for i:=0 to FJSInterfaceUses.Elements.Count-1 do
- begin
- Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
- AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
- AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
- Lit:=TJSLiteral(Item);
- AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
- ord(jsbase.jstString),ord(Lit.Value.ValueType));
- end;
- end;
- procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
- out Src: TJSSourceElements);
- var
- FunDecl: TJSFunctionDeclarationStatement;
- FunDef: TJSFuncDef;
- FunBody: TJSFunctionBody;
- begin
- Src:=nil;
- AssertNotNull(ParamName,Arg.Expr);
- AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
- FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
- AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
- AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
- FunDef:=FunDecl.AFunction as TJSFuncDef;
- AssertEquals(ParamName+' name empty','',String(FunDef.Name));
- AssertNotNull(ParamName+' body',FunDef.Body);
- AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
- FunBody:=FunDef.Body as TJSFunctionBody;
- AssertNotNull(ParamName+' body.A',FunBody.A);
- AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
- Src:=FunBody.A as TJSSourceElements;
- end;
- var
- ModuleNameExpr: TJSLiteral;
- InitFunction: TJSFunctionDeclarationStatement;
- InitAssign: TJSSimpleAssignStatement;
- InitName: String;
- LastNode: TJSElement;
- Arg: TJSArrayLiteralElement;
- begin
- if SkipTests then exit;
- try
- FJSModule:=FConverter.ConvertPasElement(Module,Engine) as TJSSourceElements;
- except
- on E: Exception do
- HandleException(E);
- end;
- if SkipTests then exit;
- if ExpectedErrorClass<>nil then
- Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
- FJSSource:=TStringList.Create;
- FJSSource.Text:=JSToStr(JSModule);
- {$IFDEF VerbosePas2JS}
- writeln('TTestModule.ConvertModule JS:');
- write(FJSSource.Text);
- {$ENDIF}
- // rtl.module(...
- AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
- AssertNotNull('register module call',JSModule.Statements.Nodes[0].Node);
- AssertEquals('register module call',TJSCallExpression,JSModule.Statements.Nodes[0].Node.ClassType);
- FJSRegModuleCall:=JSModule.Statements.Nodes[0].Node as TJSCallExpression;
- AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
- AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
- AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
- FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
- // parameter 'unitname'
- if JSModuleCallArgs.Elements.Count<1 then
- Fail('rtl.module first param unit missing');
- Arg:=JSModuleCallArgs.Elements.Elements[0];
- AssertNotNull('module name param',Arg.Expr);
- ModuleNameExpr:=Arg.Expr as TJSLiteral;
- AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
- if Module is TPasProgram then
- AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString))
- else
- AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
- // main uses section
- if JSModuleCallArgs.Elements.Count<2 then
- Fail('rtl.module second param main uses missing');
- Arg:=JSModuleCallArgs.Elements.Elements[1];
- CheckUsesList('interface',Arg,FJSInterfaceUses);
- // program/library/interface function()
- if JSModuleCallArgs.Elements.Count<3 then
- Fail('rtl.module third param intf-function missing');
- Arg:=JSModuleCallArgs.Elements.Elements[2];
- CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
- // search for $mod.$init or $mod.$main - the last statement
- if Module is TPasProgram then
- begin
- InitName:='$main';
- AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
- end
- else
- InitName:='$init';
- FJSInitBody:=nil;
- if JSModuleSrc.Statements.Count>0 then
- begin
- LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
- if LastNode is TJSSimpleAssignStatement then
- begin
- InitAssign:=LastNode as TJSSimpleAssignStatement;
- if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
- begin
- InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
- FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
- end
- else if Module is TPasProgram then
- CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
- end;
- end;
- // optional: implementation uses section
- if JSModuleCallArgs.Elements.Count<4 then
- exit;
- Arg:=JSModuleCallArgs.Elements.Elements[3];
- CheckUsesList('implementation',Arg,FJSImplentationUses);
- // optional: implementation function()
- if JSModuleCallArgs.Elements.Count<5 then
- exit;
- Arg:=JSModuleCallArgs.Elements.Elements[4];
- CheckFunctionParam('module impl-function',Arg,FJSImplementationSrc);
- end;
- procedure TCustomTestModule.ConvertProgram;
- begin
- Add('end.');
- ParseProgram;
- ConvertModule;
- end;
- procedure TCustomTestModule.ConvertUnit;
- begin
- Add('end.');
- ParseUnit;
- ConvertModule;
- end;
- procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
- DottedName: string);
- begin
- if DottedName='' then
- begin
- AssertNull(Msg,El);
- end
- else
- begin
- AssertNotNull(Msg,El);
- AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
- end;
- end;
- function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
- begin
- if El=nil then
- Result:=''
- else if El is TJSPrimaryExpressionIdent then
- Result:=String(TJSPrimaryExpressionIdent(El).Name)
- else if El is TJSDotMemberExpression then
- Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
- else
- AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
- end;
- procedure TCustomTestModule.CheckSource(Msg, Statements: String;
- InitStatements: string; ImplStatements: string);
- var
- ActualSrc, ExpectedSrc, InitName: String;
- begin
- ActualSrc:=JSToStr(JSModuleSrc);
- ExpectedSrc:=
- 'var $mod = this;'+LineEnding
- +Statements;
- if coUseStrict in Converter.Options then
- ExpectedSrc:='"use strict";'+LineEnding+ExpectedSrc;
- if Module is TPasProgram then
- InitName:='$main'
- else
- InitName:='$init';
- if (Module is TPasProgram) or (Trim(InitStatements)<>'') then
- ExpectedSrc:=ExpectedSrc+LineEnding
- +'$mod.'+InitName+' = function () {'+LineEnding
- +InitStatements
- +'};'+LineEnding;
- //writeln('TTestModule.CheckSource InitStatements="',InitStatements,'"');
- CheckDiff(Msg,ExpectedSrc,ActualSrc);
- if (JSImplementationSrc<>nil) then
- begin
- ActualSrc:=JSToStr(JSImplementationSrc);
- ExpectedSrc:=
- 'var $mod = this;'+LineEnding
- +'var $impl = $mod.$impl;'+LineEnding
- +ImplStatements;
- end
- else
- begin
- ActualSrc:='';
- ExpectedSrc:=ImplStatements;
- end;
- //writeln('TTestModule.CheckSource InitStatements="',InitStatements,'"');
- CheckDiff(Msg,ExpectedSrc,ActualSrc);
- end;
- procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
- // search diff, ignore changes in spaces
- const
- SpaceChars = [#9,#10,#13,' '];
- var
- ExpectedP, ActualP: PChar;
- function FindLineEnd(p: PChar): PChar;
- begin
- Result:=p;
- while not (Result^ in [#0,#10,#13]) do inc(Result);
- end;
- function FindLineStart(p, MinP: PChar): PChar;
- begin
- while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
- Result:=p;
- end;
- procedure DiffFound;
- var
- ActLineStartP, ActLineEndP, p, StartPos: PChar;
- ExpLine, ActLine: String;
- i: Integer;
- begin
- writeln('Diff found "',Msg,'". Lines:');
- // write correct lines
- p:=PChar(Expected);
- repeat
- StartPos:=p;
- while not (p^ in [#0,#10,#13]) do inc(p);
- ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
- if p^ in [#10,#13] then begin
- if (p[1] in [#10,#13]) and (p^<>p[1]) then
- inc(p,2)
- else
- inc(p);
- end;
- if p<=ExpectedP then begin
- writeln('= ',ExpLine);
- end else begin
- // diff line
- // write actual line
- ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
- ActLineEndP:=FindLineEnd(ActualP);
- ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
- writeln('- ',ActLine);
- // write expected line
- writeln('+ ',ExpLine);
- // write empty line with pointer ^
- for i:=1 to 2+ExpectedP-StartPos do write(' ');
- writeln('^');
- AssertEquals(Msg,ExpLine,ActLine);
- break;
- end;
- until p^=#0;
- Fail('diff found, but lines are the same, internal error');
- end;
- var
- IsSpaceNeeded: Boolean;
- LastChar: Char;
- begin
- if Expected='' then Expected:=' ';
- if Actual='' then Actual:=' ';
- ExpectedP:=PChar(Expected);
- ActualP:=PChar(Actual);
- repeat
- //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
- case ExpectedP^ of
- #0:
- begin
- // check that rest of Actual has only spaces
- while ActualP^ in SpaceChars do inc(ActualP);
- if ActualP^<>#0 then
- DiffFound;
- exit;
- end;
- ' ',#9,#10,#13:
- begin
- // skip space in Expected
- IsSpaceNeeded:=false;
- if ExpectedP>PChar(Expected) then
- LastChar:=ExpectedP[-1]
- else
- LastChar:=#0;
- while ExpectedP^ in SpaceChars do inc(ExpectedP);
- if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
- and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
- IsSpaceNeeded:=true;
- if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
- DiffFound;
- while ActualP^ in SpaceChars do inc(ActualP);
- end;
- else
- while ActualP^ in SpaceChars do inc(ActualP);
- if ExpectedP^<>ActualP^ then
- DiffFound;
- inc(ExpectedP);
- inc(ActualP);
- end;
- until false;
- end;
- procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
- MsgNumber: integer);
- begin
- ExpectedErrorClass:=EScannerError;
- ExpectedErrorMsg:=Msg;
- ExpectedErrorNumber:=MsgNumber;
- end;
- procedure TCustomTestModule.SetExpectedParserError(Msg: string;
- MsgNumber: integer);
- begin
- ExpectedErrorClass:=EParserError;
- ExpectedErrorMsg:=Msg;
- ExpectedErrorNumber:=MsgNumber;
- end;
- procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
- MsgNumber: integer);
- begin
- ExpectedErrorClass:=EPasResolve;
- ExpectedErrorMsg:=Msg;
- ExpectedErrorNumber:=MsgNumber;
- end;
- procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
- MsgNumber: integer);
- begin
- ExpectedErrorClass:=EPas2JS;
- ExpectedErrorMsg:=Msg;
- ExpectedErrorNumber:=MsgNumber;
- end;
- function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
- var
- MsgNumber: Integer;
- begin
- Result:=false;
- if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
- if E is EPas2JS then
- MsgNumber:=EPas2JS(E).MsgNumber
- else if E is EPasResolve then
- MsgNumber:=EPasResolve(E).MsgNumber
- else if E is EParserError then
- MsgNumber:=Parser.LastMsgNumber
- else if E is EScannerError then
- MsgNumber:=Scanner.LastMsgNumber
- else
- MsgNumber:=0;
- Result:=(MsgNumber=ExpectedErrorNumber) and (E.Message=ExpectedErrorMsg);
- if Result then
- SkipTests:=true;
- end;
- procedure TCustomTestModule.HandleScannerError(E: EScannerError);
- begin
- if IsErrorExpected(E) then exit;
- WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
- writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
- +' '+Scanner.CurFilename
- +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
- RaiseException(E);
- end;
- procedure TCustomTestModule.HandleParserError(E: EParserError);
- begin
- if IsErrorExpected(E) then exit;
- WriteSources(E.Filename,E.Row,E.Column);
- writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
- +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
- +' MainModuleScannerLine="'+Scanner.CurLine+'"'
- );
- RaiseException(E);
- end;
- procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
- var
- P: TPasSourcePos;
- begin
- if IsErrorExpected(E) then exit;
- P:=E.SourcePos;
- WriteSources(P.FileName,P.Row,P.Column);
- writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
- +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
- RaiseException(E);
- end;
- procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
- var
- Row, Col: integer;
- begin
- if IsErrorExpected(E) then exit;
- Engine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
- WriteSources(E.PasElement.SourceFilename,Row,Col);
- writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
- +' '+E.PasElement.SourceFilename
- +'('+IntToStr(Row)+','+IntToStr(Col)+')');
- RaiseException(E);
- end;
- procedure TCustomTestModule.HandleException(E: Exception);
- begin
- if E is EScannerError then
- HandleScannerError(EScannerError(E))
- else if E is EParserError then
- HandleParserError(EParserError(E))
- else if E is EPasResolve then
- HandlePasResolveError(EPasResolve(E))
- else if E is EPas2JS then
- HandlePas2JSError(EPas2JS(E))
- else
- begin
- if IsErrorExpected(E) then exit;
- if not (E is EAssertionFailedError) then
- begin
- WriteSources('',0,0);
- writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
- end;
- RaiseException(E);
- end;
- end;
- procedure TCustomTestModule.RaiseException(E: Exception);
- var
- MsgNumber: Integer;
- begin
- if ExpectedErrorClass<>nil then begin
- if FExpectedErrorClass=E.ClassType then begin
- if E is EPas2JS then
- MsgNumber:=EPas2JS(E).MsgNumber
- else if E is EPasResolve then
- MsgNumber:=EPasResolve(E).MsgNumber
- else if E is EParserError then
- MsgNumber:=Parser.LastMsgNumber
- else if E is EScannerError then
- MsgNumber:=Scanner.LastMsgNumber
- else
- MsgNumber:=0;
- AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
- AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
- ExpectedErrorNumber,MsgNumber);
- end else begin
- AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
- end;
- end;
- Fail(E.Message);
- end;
- procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
- aCol: integer);
- var
- IsSrc: Boolean;
- i, j: Integer;
- SrcLines: TStringList;
- Line: string;
- aModule: TTestEnginePasResolver;
- begin
- writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
- for i:=0 to ModuleCount-1 do
- begin
- aModule:=Modules[i];
- SrcLines:=TStringList.Create;
- try
- SrcLines.Text:=aModule.Source;
- IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
- writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
- for j:=1 to SrcLines.Count do
- begin
- Line:=SrcLines[j-1];
- if IsSrc and (j=aRow) then
- begin
- write('*');
- Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
- end;
- writeln(Format('%:4d: ',[j]),Line);
- end;
- finally
- SrcLines.Free;
- end;
- end;
- end;
- function TCustomTestModule.GetDefaultNamespace: string;
- var
- C: TClass;
- begin
- Result:='';
- if FModule=nil then exit;
- C:=FModule.ClassType;
- if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
- Result:=Engine.DefaultNameSpace;
- end;
- { TTestModule }
- procedure TTestModule.TestEmptyProgram;
- begin
- StartProgram(false);
- Add('begin');
- ConvertProgram;
- CheckSource('TestEmptyProgram','','');
- end;
- procedure TTestModule.TestEmptyProgramUseStrict;
- begin
- Converter.Options:=Converter.Options+[coUseStrict];
- StartProgram(false);
- Add('begin');
- ConvertProgram;
- CheckSource('TestEmptyProgramUseStrict','','');
- end;
- procedure TTestModule.TestEmptyUnit;
- begin
- StartUnit(false);
- Add('interface');
- Add('implementation');
- ConvertUnit;
- CheckSource('TestEmptyUnit',
- LinesToStr([
- ]),
- '');
- end;
- procedure TTestModule.TestEmptyUnitUseStrict;
- begin
- Converter.Options:=Converter.Options+[coUseStrict];
- StartUnit(false);
- Add('interface');
- Add('implementation');
- ConvertUnit;
- CheckSource('TestEmptyUnitUseStrict',
- LinesToStr([
- ''
- ]),
- '');
- end;
- procedure TTestModule.TestDottedUnitNames;
- begin
- AddModuleWithIntfImplSrc('NS1.Unit2.pas',
- LinesToStr([
- 'var iV: longint;'
- ]),
- '');
- FFilename:='ns1.test1.pp';
- StartProgram(true);
- Add('uses unIt2;');
- Add('implementation');
- Add('var');
- Add(' i: longint;');
- Add('begin');
- Add(' i:=iv;');
- Add(' i:=uNit2.iv;');
- Add(' i:=Ns1.TEst1.i;');
- ConvertProgram;
- CheckSource('TestDottedUnitNames',
- LinesToStr([
- 'this.i = 0;',
- '']),
- LinesToStr([ // this.$init
- '$mod.i = pas["NS1.Unit2"].iV;',
- '$mod.i = pas["NS1.Unit2"].iV;',
- '$mod.i = $mod.i;',
- '']) );
- end;
- procedure TTestModule.TestDottedUnitExpr;
- begin
- AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
- LinesToStr([
- 'procedure DoIt;'
- ]),
- 'procedure DoIt; begin end;');
- FFilename:='Ns1.SubNs1.Test1.pp';
- StartProgram(true);
- Add('uses Ns2.sUbnS2.unIt2;');
- Add('implementation');
- Add('var');
- Add(' i: longint;');
- Add('begin');
- Add(' ns2.subns2.unit2.doit;');
- Add(' i:=Ns1.SubNS1.TEst1.i;');
- ConvertProgram;
- CheckSource('TestDottedUnitExpr',
- LinesToStr([
- 'this.i = 0;',
- '']),
- LinesToStr([ // this.$init
- 'pas["NS2.SubNs2.Unit2"].DoIt();',
- '$mod.i = $mod.i;',
- '']) );
- end;
- procedure TTestModule.Test_ModeFPCFail;
- begin
- StartProgram(false);
- Add('{$mode FPC}');
- Add('begin');
- SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
- ConvertProgram;
- end;
- procedure TTestModule.Test_ModeSwitchCBlocksFail;
- begin
- StartProgram(false);
- Add('{$modeswitch cblocks-}');
- Add('begin');
- SetExpectedScannerError('Invalid mode switch: "cblocks-"',nErrInvalidModeSwitch);
- ConvertProgram;
- end;
- procedure TTestModule.TestVarInt;
- begin
- StartProgram(false);
- Add('var MyI: longint;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestVarInt','this.MyI=0;','');
- end;
- procedure TTestModule.TestVarBaseTypes;
- begin
- StartProgram(false);
- Add('var');
- Add(' i: longint;');
- Add(' s: string;');
- Add(' c: char;');
- Add(' b: boolean;');
- Add(' d: double;');
- Add(' i2: longint = 3;');
- Add(' s2: string = ''foo'';');
- Add(' c2: char = ''4'';');
- Add(' b2: boolean = true;');
- Add(' d2: double = 5.6;');
- Add(' i3: longint = $707;');
- Add(' i4: nativeint = 4503599627370495;');
- Add(' i5: nativeint = -4503599627370496;');
- Add(' i6: nativeint = $fffffffffffff;');
- Add(' i7: nativeint = -$10000000000000;');
- Add(' u8: nativeuint = $fffffffffffff;');
- Add(' u9: nativeuint = $0000000000000;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestVarBaseTypes',
- LinesToStr([
- 'this.i=0;',
- 'this.s="";',
- 'this.c="";',
- 'this.b=false;',
- 'this.d=0.0;',
- 'this.i2=3;',
- 'this.s2="foo";',
- 'this.c2="4";',
- 'this.b2=true;',
- 'this.d2=5.6;',
- 'this.i3=0x707;',
- 'this.i4= 4503599627370495;',
- 'this.i5= -4503599627370496;',
- 'this.i6= 0xfffffffffffff;',
- 'this.i7=-0x10000000000000;',
- 'this.u8= 0xfffffffffffff;',
- 'this.u9= 0x0000000000000;'
- ]),
- '');
- end;
- procedure TTestModule.TestBaseTypeSingleFail;
- begin
- StartProgram(false);
- Add('var s: single;');
- SetExpectedPasResolverError('identifier not found "single"',nIdentifierNotFound);
- ConvertProgram;
- end;
- procedure TTestModule.TestBaseTypeExtendedFail;
- begin
- StartProgram(false);
- Add('var e: extended;');
- SetExpectedPasResolverError('identifier not found "extended"',nIdentifierNotFound);
- ConvertProgram;
- end;
- procedure TTestModule.TestConstBaseTypes;
- begin
- StartProgram(false);
- Add('const');
- Add(' i: longint = 3;');
- Add(' s: string = ''foo'';');
- Add(' c: char = ''4'';');
- Add(' b: boolean = true;');
- Add(' d: double = 5.6;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestVarBaseTypes',
- LinesToStr([
- 'this.i=3;',
- 'this.s="foo";',
- 'this.c="4";',
- 'this.b=true;',
- 'this.d=5.6;'
- ]),
- '');
- end;
- procedure TTestModule.TestAliasTypeRef;
- begin
- StartProgram(false);
- Add('type');
- Add(' a=longint;');
- Add(' b=a;');
- Add('var');
- Add(' c: A;');
- Add(' d: B;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestAliasTypeRef',
- LinesToStr([ // statements
- 'this.c = 0;',
- 'this.d = 0;'
- ]),
- LinesToStr([ // this.$main
- ''
- ]));
- end;
- procedure TTestModule.TestTypeCast_BaseTypes;
- begin
- StartProgram(false);
- Add('var');
- Add(' i: longint;');
- Add(' b: boolean;');
- Add(' d: double;');
- Add(' s: string;');
- Add(' c: char;');
- Add('begin');
- Add(' i:=longint(i);');
- Add(' i:=longint(b);');
- Add(' b:=boolean(b);');
- Add(' b:=boolean(i);');
- Add(' d:=double(d);');
- Add(' d:=double(i);');
- Add(' s:=string(s);');
- Add(' s:=string(c);');
- Add(' c:=char(c);');
- ConvertProgram;
- CheckSource('TestAliasTypeRef',
- LinesToStr([ // statements
- 'this.i = 0;',
- 'this.b = false;',
- 'this.d = 0.0;',
- 'this.s = "";',
- 'this.c = "";',
- '']),
- LinesToStr([ // this.$main
- '$mod.i = $mod.i;',
- '$mod.i = ($mod.b ? 1 : 0);',
- '$mod.b = $mod.b;',
- '$mod.b = $mod.i != 0;',
- '$mod.d = $mod.d;',
- '$mod.d = $mod.i;',
- '$mod.s = $mod.s;',
- '$mod.s = $mod.c;',
- '$mod.c = $mod.c;',
- '']));
- end;
- procedure TTestModule.TestTypeCast_AliasBaseTypes;
- begin
- StartProgram(false);
- Add('type');
- Add(' integer = longint;');
- Add(' TYesNo = boolean;');
- Add(' TFloat = double;');
- Add(' TCaption = string;');
- Add(' TChar = char;');
- Add('var');
- Add(' i: integer;');
- Add(' b: TYesNo;');
- Add(' d: TFloat;');
- Add(' s: TCaption;');
- Add(' c: TChar;');
- Add('begin');
- Add(' i:=integer(i);');
- Add(' i:=integer(b);');
- Add(' b:=TYesNo(b);');
- Add(' b:=TYesNo(i);');
- Add(' d:=TFloat(d);');
- Add(' d:=TFloat(i);');
- Add(' s:=TCaption(s);');
- Add(' s:=TCaption(c);');
- Add(' c:=TChar(c);');
- ConvertProgram;
- CheckSource('TestAliasTypeRef',
- LinesToStr([ // statements
- 'this.i = 0;',
- 'this.b = false;',
- 'this.d = 0.0;',
- 'this.s = "";',
- 'this.c = "";',
- '']),
- LinesToStr([ // this.$main
- '$mod.i = $mod.i;',
- '$mod.i = ($mod.b ? 1 : 0);',
- '$mod.b = $mod.b;',
- '$mod.b = $mod.i != 0;',
- '$mod.d = $mod.d;',
- '$mod.d = $mod.i;',
- '$mod.s = $mod.s;',
- '$mod.s = $mod.c;',
- '$mod.c = $mod.c;',
- '']));
- end;
- procedure TTestModule.TestEmptyProc;
- begin
- StartProgram(false);
- Add('procedure Test;');
- Add('begin');
- Add('end;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestEmptyProc',
- LinesToStr([ // statements
- 'this.Test = function () {',
- '};'
- ]),
- LinesToStr([ // this.$main
- ''
- ]));
- end;
- procedure TTestModule.TestProcOneParam;
- begin
- StartProgram(false);
- Add('procedure ProcA(i: longint);');
- Add('begin');
- Add('end;');
- Add('begin');
- Add(' PROCA(3);');
- ConvertProgram;
- CheckSource('TestProcOneParam',
- LinesToStr([ // statements
- 'this.ProcA = function (i) {',
- '};'
- ]),
- LinesToStr([ // this.$main
- '$mod.ProcA(3);'
- ]));
- end;
- procedure TTestModule.TestFunctionWithoutParams;
- begin
- StartProgram(false);
- Add('function FuncA: longint;');
- Add('begin');
- Add('end;');
- Add('var i: longint;');
- Add('begin');
- Add(' I:=FUNCA();');
- Add(' I:=FUNCA;');
- Add(' FUNCA();');
- Add(' FUNCA;');
- ConvertProgram;
- CheckSource('TestProcWithoutParams',
- LinesToStr([ // statements
- 'this.FuncA = function () {',
- ' var Result = 0;',
- ' return Result;',
- '};',
- 'this.i=0;'
- ]),
- LinesToStr([ // this.$main
- '$mod.i=$mod.FuncA();',
- '$mod.i=$mod.FuncA();',
- '$mod.FuncA();',
- '$mod.FuncA();'
- ]));
- end;
- procedure TTestModule.TestProcedureWithoutParams;
- begin
- StartProgram(false);
- Add('procedure ProcA;');
- Add('begin');
- Add('end;');
- Add('begin');
- Add(' PROCA();');
- Add(' PROCA;');
- ConvertProgram;
- CheckSource('TestProcWithoutParams',
- LinesToStr([ // statements
- 'this.ProcA = function () {',
- '};'
- ]),
- LinesToStr([ // this.$main
- '$mod.ProcA();',
- '$mod.ProcA();'
- ]));
- end;
- procedure TTestModule.TestIncDec;
- begin
- StartProgram(false);
- Add([
- 'procedure DoIt(var i: longint);',
- 'begin',
- ' inc(i);',
- ' inc(i,2);',
- 'end;',
- 'var',
- ' Bar: longint;',
- 'begin',
- ' inc(bar);',
- ' inc(bar,2);',
- ' dec(bar);',
- ' dec(bar,3);',
- '']);
- ConvertProgram;
- CheckSource('TestIncDec',
- LinesToStr([ // statements
- 'this.DoIt = function (i) {',
- ' i.set(i.get()+1);',
- ' i.set(i.get()+2);',
- '};',
- 'this.Bar = 0;'
- ]),
- LinesToStr([ // this.$main
- '$mod.Bar+=1;',
- '$mod.Bar+=2;',
- '$mod.Bar-=1;',
- '$mod.Bar-=3;'
- ]));
- end;
- procedure TTestModule.TestAssignments;
- begin
- StartProgram(false);
- Parser.Options:=Parser.Options+[po_cassignments];
- Add('var');
- Add(' Bar:longint;');
- Add('begin');
- Add(' bar:=3;');
- Add(' bar+=4;');
- Add(' bar-=5;');
- Add(' bar*=6;');
- ConvertProgram;
- CheckSource('TestAssignments',
- LinesToStr([ // statements
- 'this.Bar = 0;'
- ]),
- LinesToStr([ // this.$main
- '$mod.Bar=3;',
- '$mod.Bar+=4;',
- '$mod.Bar-=5;',
- '$mod.Bar*=6;'
- ]));
- end;
- procedure TTestModule.TestArithmeticOperators1;
- begin
- StartProgram(false);
- Add('var');
- Add(' vA,vB,vC:longint;');
- Add('begin');
- Add(' va:=1;');
- Add(' vb:=va+va;');
- Add(' vb:=va div vb;');
- Add(' vb:=va mod vb;');
- Add(' vb:=va+va*vb+va div vb;');
- Add(' vc:=-va;');
- Add(' va:=va-vb;');
- Add(' vb:=va;');
- Add(' if va<vb then vc:=va else vc:=vb;');
- ConvertProgram;
- CheckSource('TestArithmeticOperators1',
- LinesToStr([ // statements
- 'this.vA = 0;',
- 'this.vB = 0;',
- 'this.vC = 0;'
- ]),
- LinesToStr([ // this.$main
- '$mod.vA = 1;',
- '$mod.vB = $mod.vA + $mod.vA;',
- '$mod.vB = Math.floor($mod.vA / $mod.vB);',
- '$mod.vB = $mod.vA % $mod.vB;',
- '$mod.vB = ($mod.vA + ($mod.vA * $mod.vB)) + Math.floor($mod.vA / $mod.vB);',
- '$mod.vC = -$mod.vA;',
- '$mod.vA = $mod.vA - $mod.vB;',
- '$mod.vB = $mod.vA;',
- 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
- ]));
- end;
- procedure TTestModule.TestLogicalOperators;
- begin
- StartProgram(false);
- Add('var');
- Add(' vA,vB,vC:boolean;');
- Add('begin');
- Add(' va:=vb and vc;');
- Add(' va:=vb or vc;');
- Add(' va:=true and vc;');
- Add(' va:=(vb and vc) or (va and vb);');
- Add(' va:=not vb;');
- ConvertProgram;
- CheckSource('TestLogicalOperators',
- LinesToStr([ // statements
- 'this.vA = false;',
- 'this.vB = false;',
- 'this.vC = false;'
- ]),
- LinesToStr([ // this.$main
- '$mod.vA = $mod.vB && $mod.vC;',
- '$mod.vA = $mod.vB || $mod.vC;',
- '$mod.vA = true && $mod.vC;',
- '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
- '$mod.vA = !$mod.vB;'
- ]));
- end;
- procedure TTestModule.TestBitwiseOperators;
- begin
- StartProgram(false);
- Add('var');
- Add(' vA,vB,vC:longint;');
- Add('begin');
- Add(' va:=vb and vc;');
- Add(' va:=vb or vc;');
- Add(' va:=vb xor vc;');
- Add(' va:=vb shl vc;');
- Add(' va:=vb shr vc;');
- Add(' va:=3 and vc;');
- Add(' va:=(vb and vc) or (va and vb);');
- Add(' va:=not vb;');
- ConvertProgram;
- CheckSource('TestBitwiseOperators',
- LinesToStr([ // statements
- 'this.vA = 0;',
- 'this.vB = 0;',
- 'this.vC = 0;'
- ]),
- LinesToStr([ // this.$main
- '$mod.vA = $mod.vB & $mod.vC;',
- '$mod.vA = $mod.vB | $mod.vC;',
- '$mod.vA = $mod.vB ^ $mod.vC;',
- '$mod.vA = $mod.vB << $mod.vC;',
- '$mod.vA = $mod.vB >>> $mod.vC;',
- '$mod.vA = 3 & $mod.vC;',
- '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
- '$mod.vA = ~$mod.vB;'
- ]));
- end;
- procedure TTestModule.TestPrgProcVar;
- begin
- StartProgram(false);
- Add('procedure Proc1;');
- Add('type');
- Add(' t1=longint;');
- Add('var');
- Add(' vA:t1;');
- Add('begin');
- Add('end;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestPrgProcVar',
- LinesToStr([ // statements
- 'this.Proc1 = function () {',
- ' var vA=0;',
- '};'
- ]),
- LinesToStr([ // this.$main
- ''
- ]));
- end;
- procedure TTestModule.TestUnitProcVar;
- begin
- StartUnit(false);
- Add('interface');
- Add('');
- Add('type tA=string; // unit scope');
- Add('procedure Proc1;');
- Add('');
- Add('implementation');
- Add('');
- Add('procedure Proc1;');
- Add('type tA=longint; // local proc scope');
- Add('var v1:tA; // using local tA');
- Add('begin');
- Add('end;');
- Add('var v2:tA; // using interface tA');
- ConvertUnit;
- CheckSource('TestUnitProcVar',
- LinesToStr([ // statements
- 'var $impl = $mod.$impl;',
- 'this.Proc1 = function () {',
- ' var v1 = 0;',
- '};',
- '']),
- // this.$init
- '',
- // implementation
- LinesToStr([
- '$impl.v2 = "";',
- '']));
- end;
- procedure TTestModule.TestImplProc;
- begin
- StartUnit(false);
- Add('interface');
- Add('');
- Add('procedure Proc1;');
- Add('');
- Add('implementation');
- Add('');
- Add('procedure Proc1; begin end;');
- Add('procedure Proc2; begin end;');
- Add('initialization');
- Add(' Proc1;');
- Add(' Proc2;');
- ConvertUnit;
- CheckSource('TestImplProc',
- LinesToStr([ // statements
- 'var $impl = $mod.$impl;',
- 'this.Proc1 = function () {',
- '};',
- '']),
- LinesToStr([ // this.$init
- '$mod.Proc1();',
- '$impl.Proc2();',
- '']),
- LinesToStr([ // implementation
- '$impl.Proc2 = function () {',
- '};',
- ''])
- );
- end;
- procedure TTestModule.TestFunctionResult;
- begin
- StartProgram(false);
- Add('function Func1: longint;');
- Add('begin');
- Add(' Result:=3;');
- Add('end;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestFunctionResult',
- LinesToStr([ // statements
- 'this.Func1 = function () {',
- ' var Result = 0;',
- ' Result = 3;',
- ' return Result;',
- '};'
- ]),
- '');
- end;
- procedure TTestModule.TestNestedProc;
- begin
- StartProgram(false);
- Add('var vInUnit: longint;');
- Add('function DoIt(pA,pD: longint): longint;');
- Add('var');
- Add(' vB: longint;');
- Add(' vC: longint;');
- Add(' function Nesty(pA: longint): longint; ');
- Add(' var vB: longint;');
- Add(' begin');
- Add(' Result:=pa+vb+vc+pd+vInUnit;');
- Add(' end;');
- Add('begin');
- Add(' Result:=pa+vb+vc;');
- Add('end;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestNestedProc',
- LinesToStr([ // statements
- 'this.vInUnit = 0;',
- 'this.DoIt = function (pA, pD) {',
- ' var Result = 0;',
- ' var vB = 0;',
- ' var vC = 0;',
- ' function Nesty(pA) {',
- ' var Result = 0;',
- ' var vB = 0;',
- ' Result = (((pA + vB) + vC) + pD) + $mod.vInUnit;',
- ' return Result;',
- ' };',
- ' Result = (pA + vB) + vC;',
- ' return Result;',
- '};'
- ]),
- '');
- end;
- procedure TTestModule.TestForwardProc;
- begin
- StartProgram(false);
- Add('procedure FuncA(Bar: longint); forward;');
- Add('procedure FuncB(Bar: longint);');
- Add('begin');
- Add(' funca(bar);');
- Add('end;');
- Add('procedure funca(bar: longint);');
- Add('begin');
- Add(' if bar=3 then ;');
- Add('end;');
- Add('begin');
- Add(' funca(4);');
- Add(' funcb(5);');
- ConvertProgram;
- CheckSource('TestForwardProc',
- LinesToStr([ // statements'
- 'this.FuncB = function (Bar) {',
- ' $mod.FuncA(Bar);',
- '};',
- 'this.FuncA = function (Bar) {',
- ' if (Bar == 3);',
- '};'
- ]),
- LinesToStr([
- '$mod.FuncA(4);',
- '$mod.FuncB(5);'
- ])
- );
- end;
- procedure TTestModule.TestNestedForwardProc;
- begin
- StartProgram(false);
- Add('procedure FuncA;');
- Add(' procedure FuncB(i: longint); forward;');
- Add(' procedure FuncC(i: longint);');
- Add(' begin');
- Add(' funcb(i);');
- Add(' end;');
- Add(' procedure FuncB(i: longint);');
- Add(' begin');
- Add(' if i=3 then ;');
- Add(' end;');
- Add('begin');
- Add(' funcc(4)');
- Add('end;');
- Add('begin');
- Add(' funca;');
- ConvertProgram;
- CheckSource('TestNestedForwardProc',
- LinesToStr([ // statements'
- 'this.FuncA = function () {',
- ' function FuncC(i) {',
- ' FuncB(i);',
- ' };',
- ' function FuncB(i) {',
- ' if (i == 3);',
- ' };',
- ' FuncC(4);',
- '};'
- ]),
- LinesToStr([
- '$mod.FuncA();'
- ])
- );
- end;
- procedure TTestModule.TestAssignFunctionResult;
- begin
- StartProgram(false);
- Add('function Func1: longint;');
- Add('begin');
- Add('end;');
- Add('var i: longint;');
- Add('begin');
- Add(' i:=func1();');
- Add(' i:=func1()+func1();');
- ConvertProgram;
- CheckSource('TestAssignFunctionResult',
- LinesToStr([ // statements
- 'this.Func1 = function () {',
- ' var Result = 0;',
- ' return Result;',
- '};',
- 'this.i = 0;'
- ]),
- LinesToStr([
- '$mod.i = $mod.Func1();',
- '$mod.i = $mod.Func1() + $mod.Func1();'
- ]));
- end;
- procedure TTestModule.TestFunctionResultInCondition;
- begin
- StartProgram(false);
- Add('function Func1: longint;');
- Add('begin');
- Add('end;');
- Add('function Func2: boolean;');
- Add('begin');
- Add('end;');
- Add('var i: longint;');
- Add('begin');
- Add(' if func2 then ;');
- Add(' if i=func1() then ;');
- Add(' if i=func1 then ;');
- ConvertProgram;
- CheckSource('TestFunctionResultInCondition',
- LinesToStr([ // statements
- 'this.Func1 = function () {',
- ' var Result = 0;',
- ' return Result;',
- '};',
- 'this.Func2 = function () {',
- ' var Result = false;',
- ' return Result;',
- '};',
- 'this.i = 0;'
- ]),
- LinesToStr([
- 'if ($mod.Func2());',
- 'if ($mod.i == $mod.Func1());',
- 'if ($mod.i == $mod.Func1());'
- ]));
- end;
- procedure TTestModule.TestExit;
- begin
- StartProgram(false);
- Add('procedure ProcA;');
- Add('begin');
- Add(' exit;');
- Add('end;');
- Add('function FuncB: longint;');
- Add('begin');
- Add(' exit;');
- Add(' exit(3);');
- Add('end;');
- Add('function FuncC: string;');
- Add('begin');
- Add(' exit;');
- Add(' exit(''a'');');
- Add(' exit(''abc'');');
- Add('end;');
- Add('begin');
- Add(' exit;');
- Add(' exit(1);');
- ConvertProgram;
- CheckSource('TestExit',
- LinesToStr([ // statements
- 'this.ProcA = function () {',
- ' return;',
- '};',
- 'this.FuncB = function () {',
- ' var Result = 0;',
- ' return Result;',
- ' return 3;',
- ' return Result;',
- '};',
- 'this.FuncC = function () {',
- ' var Result = "";',
- ' return Result;',
- ' return "a";',
- ' return "abc";',
- ' return Result;',
- '};'
- ]),
- LinesToStr([
- 'return;',
- 'return 1;',
- '']));
- end;
- procedure TTestModule.TestBreak;
- begin
- StartProgram(false);
- Add('var i: longint;');
- Add('begin');
- Add(' repeat');
- Add(' break;');
- Add(' until true;');
- Add(' while true do');
- Add(' break;');
- Add(' for i:=1 to 2 do');
- Add(' break;');
- ConvertProgram;
- CheckSource('TestBreak',
- LinesToStr([ // statements
- 'this.i = 0;'
- ]),
- LinesToStr([
- 'do {',
- ' break;',
- '} while (!true);',
- 'while (true) break;',
- 'var $loopend1 = 2;',
- 'for ($mod.i = 1; $mod.i <= $loopend1; $mod.i++) break;',
- 'if ($mod.i > $loopend1) $mod.i--;'
- ]));
- end;
- procedure TTestModule.TestContinue;
- begin
- StartProgram(false);
- Add('var i: longint;');
- Add('begin');
- Add(' repeat');
- Add(' continue;');
- Add(' until true;');
- Add(' while true do');
- Add(' continue;');
- Add(' for i:=1 to 2 do');
- Add(' continue;');
- ConvertProgram;
- CheckSource('TestContinue',
- LinesToStr([ // statements
- 'this.i = 0;'
- ]),
- LinesToStr([
- 'do {',
- ' continue;',
- '} while (!true);',
- 'while (true) continue;',
- 'var $loopend1 = 2;',
- 'for ($mod.i = 1; $mod.i <= $loopend1; $mod.i++) continue;',
- 'if ($mod.i > $loopend1) $mod.i--;'
- ]));
- end;
- procedure TTestModule.TestProc_External;
- begin
- StartProgram(false);
- Add('procedure Foo; external name ''console.log'';');
- Add('function Bar: longint; external name ''get.item'';');
- Add('function Bla(s: string): longint; external name ''apply.something'';');
- Add('var');
- Add(' i: longint;');
- Add('begin');
- Add(' Foo;');
- Add(' i:=Bar;');
- Add(' i:=Bla(''abc'');');
- ConvertProgram;
- CheckSource('TestProcedureExternal',
- LinesToStr([ // statements
- 'this.i = 0;'
- ]),
- LinesToStr([
- 'console.log();',
- '$mod.i = get.item();',
- '$mod.i = apply.something("abc");'
- ]));
- end;
- procedure TTestModule.TestProc_ExternalOtherUnit;
- begin
- AddModuleWithIntfImplSrc('unit2.pas',
- LinesToStr([
- 'procedure Now; external name ''Date.now'';',
- 'procedure DoIt;'
- ]),
- 'procedure doit; begin end;');
- StartUnit(true);
- Add('interface');
- Add('uses unit2;');
- Add('implementation');
- Add('begin');
- Add(' now;');
- Add(' now();');
- Add(' uNit2.now;');
- Add(' uNit2.now();');
- Add(' doit;');
- Add(' uNit2.doit;');
- ConvertUnit;
- CheckSource('TestProcedureExternalOtherUnit',
- LinesToStr([
- '']),
- LinesToStr([
- 'Date.now();',
- 'Date.now();',
- 'Date.now();',
- 'Date.now();',
- 'pas.unit2.DoIt();',
- 'pas.unit2.DoIt();',
- '']));
- end;
- procedure TTestModule.TestProc_Asm;
- begin
- StartProgram(false);
- Add('function DoIt: longint;');
- Add('begin;');
- Add(' asm');
- Add(' { a:{ b:{}, c:[]}, d:''1'' };');
- Add(' end;');
- Add('end;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestProcedureAsm',
- LinesToStr([ // statements
- 'this.DoIt = function () {',
- ' var Result = 0;',
- ' { a:{ b:{}, c:[]}, d:''1'' };',
- ' return Result;',
- '};'
- ]),
- LinesToStr([
- ''
- ]));
- end;
- procedure TTestModule.TestProc_Assembler;
- begin
- StartProgram(false);
- Add('function DoIt: longint; assembler;');
- Add('asm');
- Add('{ a:{ b:{}, c:[]}, d:''1'' };');
- Add('end;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestProcedureAssembler',
- LinesToStr([ // statements
- 'this.DoIt = function () {',
- ' { a:{ b:{}, c:[]}, d:''1'' };',
- '};'
- ]),
- LinesToStr([
- ''
- ]));
- end;
- procedure TTestModule.TestProc_VarParam;
- begin
- StartProgram(false);
- Add('type integer = longint;');
- Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
- Add('var vJ: integer;');
- Add('begin');
- Add(' vg:=vg+1;');
- Add(' vj:=vh+2;');
- Add(' vi:=vi+3;');
- Add(' doit(vg,vg,vg);');
- Add(' doit(vh,vh,vj);');
- Add(' doit(vi,vi,vi);');
- Add(' doit(vj,vj,vj);');
- Add('end;');
- Add('var i: integer;');
- Add('begin');
- Add(' doit(i,i,i);');
- ConvertProgram;
- CheckSource('TestProcedure_VarParam',
- LinesToStr([ // statements
- 'this.DoIt = function (vG,vH,vI) {',
- ' var vJ = 0;',
- ' vG = vG + 1;',
- ' vJ = vH + 2;',
- ' vI.set(vI.get()+3);',
- ' $mod.DoIt(vG, vG, {',
- ' get: function () {',
- ' return vG;',
- ' },',
- ' set: function (v) {',
- ' vG = v;',
- ' }',
- ' });',
- ' $mod.DoIt(vH, vH, {',
- ' get: function () {',
- ' return vJ;',
- ' },',
- ' set: function (v) {',
- ' vJ = v;',
- ' }',
- ' });',
- ' $mod.DoIt(vI.get(), vI.get(), vI);',
- ' $mod.DoIt(vJ, vJ, {',
- ' get: function () {',
- ' return vJ;',
- ' },',
- ' set: function (v) {',
- ' vJ = v;',
- ' }',
- ' });',
- '};',
- 'this.i = 0;'
- ]),
- LinesToStr([
- '$mod.DoIt($mod.i,$mod.i,{',
- ' p: $mod,',
- ' get: function () {',
- ' return this.p.i;',
- ' },',
- ' set: function (v) {',
- ' this.p.i = v;',
- ' }',
- '});'
- ]));
- end;
- procedure TTestModule.TestProc_Overload;
- begin
- StartProgram(false);
- Add('procedure DoIt(vI: longint); begin end;');
- Add('procedure DoIt(vI, vJ: longint); begin end;');
- Add('procedure DoIt(vD: double); begin end;');
- Add('begin');
- Add(' DoIt(1);');
- Add(' DoIt(2,3);');
- Add(' DoIt(4.5);');
- ConvertProgram;
- CheckSource('TestProcedureOverload',
- LinesToStr([ // statements
- 'this.DoIt = function (vI) {',
- '};',
- 'this.DoIt$1 = function (vI, vJ) {',
- '};',
- 'this.DoIt$2 = function (vD) {',
- '};',
- '']),
- LinesToStr([
- '$mod.DoIt(1);',
- '$mod.DoIt$1(2, 3);',
- '$mod.DoIt$2(4.5);',
- '']));
- end;
- procedure TTestModule.TestProc_OverloadForward;
- begin
- StartProgram(false);
- Add('procedure DoIt(vI: longint); forward;');
- Add('procedure DoIt(vI, vJ: longint); begin end;');
- Add('procedure doit(vi: longint); begin end;');
- Add('begin');
- Add(' doit(1);');
- Add(' doit(2,3);');
- ConvertProgram;
- CheckSource('TestProcedureOverloadForward',
- LinesToStr([ // statements
- 'this.DoIt$1 = function (vI, vJ) {',
- '};',
- 'this.DoIt = function (vI) {',
- '};',
- '']),
- LinesToStr([
- '$mod.DoIt(1);',
- '$mod.DoIt$1(2, 3);',
- '']));
- end;
- procedure TTestModule.TestProc_OverloadUnit;
- begin
- StartUnit(false);
- Add('interface');
- Add('procedure DoIt(vI: longint);');
- Add('procedure DoIt(vI, vJ: longint);');
- Add('implementation');
- Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
- Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
- Add('procedure DoIt(vi: longint); begin end;');
- Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
- Add('procedure DoIt(vi, vj: longint); begin end;');
- Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
- Add('begin');
- Add(' doit(1);');
- Add(' doit(2,3);');
- Add(' doit(4,5,6);');
- Add(' doit(7,8,9,10);');
- Add(' doit(11,12,13,14,15);');
- ConvertUnit;
- CheckSource('TestProcedureOverloadUnit',
- LinesToStr([ // statements
- 'var $impl = $mod.$impl;',
- 'this.DoIt = function (vI) {',
- '};',
- 'this.DoIt$1 = function (vI, vJ) {',
- '};',
- '']),
- LinesToStr([ // this.$init
- '$mod.DoIt(1);',
- '$mod.DoIt$1(2, 3);',
- '$impl.DoIt$3(4,5,6);',
- '$impl.DoIt$4(7,8,9,10);',
- '$impl.DoIt$2(11,12,13,14,15);',
- '']),
- LinesToStr([ // implementation
- '$impl.DoIt$3 = function (vI, vJ, vK) {',
- '};',
- '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
- '};',
- '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
- '};',
- '']));
- end;
- procedure TTestModule.TestProc_OverloadNested;
- begin
- StartProgram(false);
- Add('procedure DoIt(vA: longint); forward;');
- Add('procedure DoIt(vB, vC: longint);');
- Add('begin // 2 param overload');
- Add(' doit(1);');
- Add(' doit(1,2);');
- Add('end;');
- Add('procedure doit(vA: longint);');
- Add(' procedure DoIt(vA, vB, vC: longint); forward;');
- Add(' procedure DoIt(vA, vB, vC, vD: longint);');
- Add(' begin // 4 param overload');
- Add(' doit(1);');
- Add(' doit(1,2);');
- Add(' doit(1,2,3);');
- Add(' doit(1,2,3,4);');
- Add(' end;');
- Add(' procedure doit(vA, vB, vC: longint);');
- Add(' procedure DoIt(vA, vB, vC, vD, vE: longint); forward;');
- Add(' procedure DoIt(vA, vB, vC, vD, vE, vF: longint);');
- Add(' begin // 6 param overload');
- Add(' doit(1);');
- Add(' doit(1,2);');
- Add(' doit(1,2,3);');
- Add(' doit(1,2,3,4);');
- Add(' doit(1,2,3,4,5);');
- Add(' doit(1,2,3,4,5,6);');
- Add(' end;');
- Add(' procedure doit(vA, vB, vC, vD, vE: longint);');
- Add(' begin // 5 param overload');
- Add(' doit(1);');
- Add(' doit(1,2);');
- Add(' doit(1,2,3);');
- Add(' doit(1,2,3,4);');
- Add(' doit(1,2,3,4,5);');
- Add(' doit(1,2,3,4,5,6);');
- Add(' end;');
- Add(' begin // 3 param overload');
- Add(' doit(1);');
- Add(' doit(1,2);');
- Add(' doit(1,2,3);');
- Add(' doit(1,2,3,4);');
- Add(' doit(1,2,3,4,5);');
- Add(' doit(1,2,3,4,5,6);');
- Add(' end;');
- Add('begin // 1 param overload');
- Add(' doit(1);');
- Add(' doit(1,2);');
- Add(' doit(1,2,3);');
- Add(' doit(1,2,3,4);');
- Add('end;');
- Add('begin // main');
- Add(' doit(1);');
- Add(' doit(1,2);');
- ConvertProgram;
- CheckSource('TestProcedureOverloadNested',
- LinesToStr([ // statements
- 'this.DoIt$1 = function (vB, vC) {',
- ' $mod.DoIt(1);',
- ' $mod.DoIt$1(1, 2);',
- '};',
- 'this.DoIt = function (vA) {',
- ' function DoIt$3(vA, vB, vC, vD) {',
- ' $mod.DoIt(1);',
- ' $mod.DoIt$1(1, 2);',
- ' DoIt$2(1, 2, 3);',
- ' DoIt$3(1, 2, 3, 4);',
- ' };',
- ' function DoIt$2(vA, vB, vC) {',
- ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
- ' $mod.DoIt(1);',
- ' $mod.DoIt$1(1, 2);',
- ' DoIt$2(1, 2, 3);',
- ' DoIt$3(1, 2, 3, 4);',
- ' DoIt$4(1, 2, 3, 4, 5);',
- ' DoIt$5(1, 2, 3, 4, 5, 6);',
- ' };',
- ' function DoIt$4(vA, vB, vC, vD, vE) {',
- ' $mod.DoIt(1);',
- ' $mod.DoIt$1(1, 2);',
- ' DoIt$2(1, 2, 3);',
- ' DoIt$3(1, 2, 3, 4);',
- ' DoIt$4(1, 2, 3, 4, 5);',
- ' DoIt$5(1, 2, 3, 4, 5, 6);',
- ' };',
- ' $mod.DoIt(1);',
- ' $mod.DoIt$1(1, 2);',
- ' DoIt$2(1, 2, 3);',
- ' DoIt$3(1, 2, 3, 4);',
- ' DoIt$4(1, 2, 3, 4, 5);',
- ' DoIt$5(1, 2, 3, 4, 5, 6);',
- ' };',
- ' $mod.DoIt(1);',
- ' $mod.DoIt$1(1, 2);',
- ' DoIt$2(1, 2, 3);',
- ' DoIt$3(1, 2, 3, 4);',
- '};',
- '']),
- LinesToStr([
- '$mod.DoIt(1);',
- '$mod.DoIt$1(1, 2);',
- '']));
- end;
- procedure TTestModule.TestProc_Varargs;
- begin
- StartProgram(false);
- Add('procedure ProcA(i:longint); varargs; external name ''ProcA'';');
- Add('procedure ProcB; varargs; external name ''ProcB'';');
- Add('procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';');
- Add('function GetIt: longint; begin end;');
- Add('begin');
- Add(' ProcA(1);');
- Add(' ProcA(1,2);');
- Add(' ProcA(1,2.0);');
- Add(' ProcA(1,2,3);');
- Add(' ProcA(1,''2'');');
- Add(' ProcA(2,'''');');
- Add(' ProcA(3,false);');
- Add(' ProcB;');
- Add(' ProcB();');
- Add(' ProcB(4);');
- Add(' ProcB(''foo'');');
- Add(' ProcC;');
- Add(' ProcC();');
- Add(' ProcC(4);');
- Add(' ProcC(5,''foo'');');
- Add(' ProcB(GetIt);');
- Add(' ProcB(GetIt());');
- Add(' ProcB(GetIt,GetIt());');
- ConvertProgram;
- CheckSource('TestProc_Varargs',
- LinesToStr([ // statements
- 'this.GetIt = function () {',
- ' var Result = 0;',
- ' return Result;',
- '};',
- '']),
- LinesToStr([
- 'ProcA(1);',
- 'ProcA(1, 2);',
- 'ProcA(1, 2.0);',
- 'ProcA(1, 2, 3);',
- 'ProcA(1, "2");',
- 'ProcA(2, "");',
- 'ProcA(3, false);',
- 'ProcB();',
- 'ProcB();',
- 'ProcB(4);',
- 'ProcB("foo");',
- 'ProcC(17);',
- 'ProcC(17);',
- 'ProcC(4);',
- 'ProcC(5, "foo");',
- 'ProcB($mod.GetIt());',
- 'ProcB($mod.GetIt());',
- 'ProcB($mod.GetIt(), $mod.GetIt());',
- '']));
- end;
- procedure TTestModule.TestEnum_Name;
- begin
- StartProgram(false);
- Add('type TMyEnum = (Red, Green, Blue);');
- Add('var e: TMyEnum;');
- Add('var f: TMyEnum = Blue;');
- Add('begin');
- Add(' e:=green;');
- ConvertProgram;
- CheckSource('TestEnumName',
- LinesToStr([ // statements
- 'this.TMyEnum = {',
- ' "0":"Red",',
- ' Red:0,',
- ' "1":"Green",',
- ' Green:1,',
- ' "2":"Blue",',
- ' Blue:2',
- ' };',
- 'this.e = 0;',
- 'this.f = $mod.TMyEnum.Blue;'
- ]),
- LinesToStr([
- '$mod.e=$mod.TMyEnum.Green;'
- ]));
- end;
- procedure TTestModule.TestEnum_Number;
- begin
- Converter.Options:=Converter.Options+[coEnumNumbers];
- StartProgram(false);
- Add('type TMyEnum = (Red, Green);');
- Add('var');
- Add(' e: TMyEnum;');
- Add(' f: TMyEnum = Green;');
- Add('begin');
- Add(' e:=green;');
- ConvertProgram;
- CheckSource('TestEnumNumber',
- LinesToStr([ // statements
- 'this.TMyEnum = {',
- ' "0":"Red",',
- ' Red:0,',
- ' "1":"Green",',
- ' Green:1',
- ' };',
- 'this.e = 0;',
- 'this.f = 1;'
- ]),
- LinesToStr([
- '$mod.e=1;'
- ]));
- end;
- procedure TTestModule.TestEnum_Functions;
- begin
- StartProgram(false);
- Add('type TMyEnum = (Red, Green);');
- Add('var');
- Add(' e: TMyEnum;');
- Add(' i: longint;');
- Add(' s: string;');
- Add('begin');
- Add(' i:=ord(red);');
- Add(' i:=ord(green);');
- Add(' i:=ord(e);');
- Add(' e:=low(tmyenum);');
- Add(' e:=low(e);');
- Add(' e:=high(tmyenum);');
- Add(' e:=high(e);');
- Add(' e:=pred(green);');
- Add(' e:=pred(e);');
- Add(' e:=succ(red);');
- Add(' e:=succ(e);');
- Add(' e:=tmyenum(1);');
- Add(' e:=tmyenum(i);');
- Add(' s:=str(e);');
- Add(' str(e,s)');
- Add(' s:=str(e:3);');
- ConvertProgram;
- CheckSource('TestEnumNumber',
- LinesToStr([ // statements
- 'this.TMyEnum = {',
- ' "0":"Red",',
- ' Red:0,',
- ' "1":"Green",',
- ' Green:1',
- ' };',
- 'this.e = 0;',
- 'this.i = 0;',
- 'this.s = "";'
- ]),
- LinesToStr([
- '$mod.i=$mod.TMyEnum.Red;',
- '$mod.i=$mod.TMyEnum.Green;',
- '$mod.i=$mod.e;',
- '$mod.e=$mod.TMyEnum.Red;',
- '$mod.e=$mod.TMyEnum.Red;',
- '$mod.e=$mod.TMyEnum.Green;',
- '$mod.e=$mod.TMyEnum.Green;',
- '$mod.e=$mod.TMyEnum.Green-1;',
- '$mod.e=$mod.e-1;',
- '$mod.e=$mod.TMyEnum.Red+1;',
- '$mod.e=$mod.e+1;',
- '$mod.e=1;',
- '$mod.e=$mod.i;',
- '$mod.s = $mod.TMyEnum[$mod.e];',
- '$mod.s = $mod.TMyEnum[$mod.e];',
- '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
- '']));
- end;
- procedure TTestModule.TestEnum_AsParams;
- begin
- StartProgram(false);
- Add('type TEnum = (Red,Blue);');
- Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
- Add('var vJ: TEnum;');
- Add('begin');
- Add(' vg:=vg;');
- Add(' vj:=vh;');
- Add(' vi:=vi;');
- Add(' doit(vg,vg,vg);');
- Add(' doit(vh,vh,vj);');
- Add(' doit(vi,vi,vi);');
- Add(' doit(vj,vj,vj);');
- Add('end;');
- Add('var i: TEnum;');
- Add('begin');
- Add(' doit(i,i,i);');
- ConvertProgram;
- CheckSource('TestEnum_AsParams',
- LinesToStr([ // statements
- 'this.TEnum = {',
- ' "0": "Red",',
- ' Red: 0,',
- ' "1": "Blue",',
- ' Blue: 1',
- '};',
- 'this.DoIt = function (vG,vH,vI) {',
- ' var vJ = 0;',
- ' vG = vG;',
- ' vJ = vH;',
- ' vI.set(vI.get());',
- ' $mod.DoIt(vG, vG, {',
- ' get: function () {',
- ' return vG;',
- ' },',
- ' set: function (v) {',
- ' vG = v;',
- ' }',
- ' });',
- ' $mod.DoIt(vH, vH, {',
- ' get: function () {',
- ' return vJ;',
- ' },',
- ' set: function (v) {',
- ' vJ = v;',
- ' }',
- ' });',
- ' $mod.DoIt(vI.get(), vI.get(), vI);',
- ' $mod.DoIt(vJ, vJ, {',
- ' get: function () {',
- ' return vJ;',
- ' },',
- ' set: function (v) {',
- ' vJ = v;',
- ' }',
- ' });',
- '};',
- 'this.i = 0;'
- ]),
- LinesToStr([
- '$mod.DoIt($mod.i,$mod.i,{',
- ' p: $mod,',
- ' get: function () {',
- ' return this.p.i;',
- ' },',
- ' set: function (v) {',
- ' this.p.i = v;',
- ' }',
- '});'
- ]));
- end;
- procedure TTestModule.TestSet;
- begin
- StartProgram(false);
- Add('type');
- Add(' TColor = (Red, Green, Blue);');
- Add(' TColors = set of TColor;');
- Add('var');
- Add(' c: TColor;');
- Add(' s: TColors;');
- Add(' t: TColors = [];');
- Add(' u: TColors = [Red];');
- Add('begin');
- Add(' s:=[];');
- Add(' s:=[Green];');
- Add(' s:=[Green,Blue];');
- Add(' s:=[Red..Blue];');
- Add(' s:=[Red,Green..Blue];');
- Add(' s:=[Red,c];');
- Add(' s:=t;');
- ConvertProgram;
- CheckSource('TestEnumName',
- LinesToStr([ // statements
- 'this.TColor = {',
- ' "0":"Red",',
- ' Red:0,',
- ' "1":"Green",',
- ' Green:1,',
- ' "2":"Blue",',
- ' Blue:2',
- ' };',
- 'this.c = 0;',
- 'this.s = {};',
- 'this.t = {};',
- 'this.u = rtl.createSet($mod.TColor.Red);'
- ]),
- LinesToStr([
- '$mod.s={};',
- '$mod.s=rtl.createSet($mod.TColor.Green);',
- '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
- '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
- '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
- '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
- '$mod.s=rtl.refSet($mod.t);',
- '']));
- end;
- procedure TTestModule.TestSet_Operators;
- begin
- StartProgram(false);
- Add('type');
- Add(' TColor = (Red, Green, Blue);');
- Add(' TColors = set of tcolor;');
- Add('var');
- Add(' vC: TColor;');
- Add(' vS: TColors;');
- Add(' vT: TColors;');
- Add(' vU: TColors;');
- Add(' B: boolean;');
- Add('begin');
- Add(' include(vs,green);');
- Add(' exclude(vs,vc);');
- Add(' vs:=vt+vu;');
- Add(' vs:=vt+[red];');
- Add(' vs:=[red]+vt;');
- Add(' vs:=[red]+[green];');
- Add(' vs:=vt-vu;');
- Add(' vs:=vt-[red];');
- Add(' vs:=[red]-vt;');
- Add(' vs:=[red]-[green];');
- Add(' vs:=vt*vu;');
- Add(' vs:=vt*[red];');
- Add(' vs:=[red]*vt;');
- Add(' vs:=[red]*[green];');
- Add(' vs:=vt><vu;');
- Add(' vs:=vt><[red];');
- Add(' vs:=[red]><vt;');
- Add(' vs:=[red]><[green];');
- Add(' b:=vt=vu;');
- Add(' b:=vt=[red];');
- Add(' b:=[red]=vt;');
- Add(' b:=[red]=[green];');
- Add(' b:=vt<>vu;');
- Add(' b:=vt<>[red];');
- Add(' b:=[red]<>vt;');
- Add(' b:=[red]<>[green];');
- Add(' b:=vt<=vu;');
- Add(' b:=vt<=[red];');
- Add(' b:=[red]<=vt;');
- Add(' b:=[red]<=[green];');
- Add(' b:=vt>=vu;');
- Add(' b:=vt>=[red];');
- Add(' b:=[red]>=vt;');
- Add(' b:=[red]>=[green];');
- ConvertProgram;
- CheckSource('TestSet_Operators',
- LinesToStr([ // statements
- 'this.TColor = {',
- ' "0":"Red",',
- ' Red:0,',
- ' "1":"Green",',
- ' Green:1,',
- ' "2":"Blue",',
- ' Blue:2',
- ' };',
- 'this.vC = 0;',
- 'this.vS = {};',
- 'this.vT = {};',
- 'this.vU = {};',
- 'this.B = false;'
- ]),
- LinesToStr([
- '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
- '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
- '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
- '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
- '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
- '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
- '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
- '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
- '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
- '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
- '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
- '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
- '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
- '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
- '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
- '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
- '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
- '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
- '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
- '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
- '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
- '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
- '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
- '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
- '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
- '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
- '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
- '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
- '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
- '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
- '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
- '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
- '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
- '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
- '']));
- end;
- procedure TTestModule.TestSet_Operator_In;
- begin
- StartProgram(false);
- Add('type');
- Add(' TColor = (Red, Green, Blue);');
- Add(' TColors = set of tcolor;');
- Add('var');
- Add(' vC: tcolor;');
- Add(' vT: tcolors;');
- Add(' B: boolean;');
- Add('begin');
- Add(' b:=red in vt;');
- Add(' b:=vc in vt;');
- Add(' b:=green in [red..blue];');
- Add(' b:=vc in [red..blue];');
- Add(' ');
- Add(' if red in vt then ;');
- Add(' while vC in vt do ;');
- Add(' repeat');
- Add(' until vC in vt;');
- ConvertProgram;
- CheckSource('TestSet_Operator_In',
- LinesToStr([ // statements
- 'this.TColor = {',
- ' "0":"Red",',
- ' Red:0,',
- ' "1":"Green",',
- ' Green:1,',
- ' "2":"Blue",',
- ' Blue:2',
- ' };',
- 'this.vC = 0;',
- 'this.vT = {};',
- 'this.B = false;'
- ]),
- LinesToStr([
- '$mod.B = $mod.TColor.Red in $mod.vT;',
- '$mod.B = $mod.vC in $mod.vT;',
- '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
- '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
- 'if ($mod.TColor.Red in $mod.vT) ;',
- 'while ($mod.vC in $mod.vT) {',
- '};',
- 'do {',
- '} while (!($mod.vC in $mod.vT));',
- '']));
- end;
- procedure TTestModule.TestSet_Functions;
- begin
- StartProgram(false);
- Add('type');
- Add(' TMyEnum = (Red, Green);');
- Add(' TMyEnums = set of TMyEnum;');
- Add('var');
- Add(' e: TMyEnum;');
- Add(' s: TMyEnums;');
- Add('begin');
- Add(' e:=Low(TMyEnums);');
- Add(' e:=Low(s);');
- Add(' e:=High(TMyEnums);');
- Add(' e:=High(s);');
- ConvertProgram;
- CheckSource('TestSetFunctions',
- LinesToStr([ // statements
- 'this.TMyEnum = {',
- ' "0":"Red",',
- ' Red:0,',
- ' "1":"Green",',
- ' Green:1',
- ' };',
- 'this.e = 0;',
- 'this.s = {};'
- ]),
- LinesToStr([
- '$mod.e=$mod.TMyEnum.Red;',
- '$mod.e=$mod.TMyEnum.Red;',
- '$mod.e=$mod.TMyEnum.Green;',
- '$mod.e=$mod.TMyEnum.Green;',
- '']));
- end;
- procedure TTestModule.TestSet_PassAsArgClone;
- begin
- StartProgram(false);
- Add('type');
- Add(' TMyEnum = (Red, Green);');
- Add(' TMyEnums = set of TMyEnum;');
- Add('procedure DoDefault(s: tmyenums); begin end;');
- Add('procedure DoConst(const s: tmyenums); begin end;');
- Add('var');
- Add(' aSet: tmyenums;');
- Add('begin');
- Add(' dodefault(aset);');
- Add(' doconst(aset);');
- ConvertProgram;
- CheckSource('TestSetFunctions',
- LinesToStr([ // statements
- 'this.TMyEnum = {',
- ' "0":"Red",',
- ' Red:0,',
- ' "1":"Green",',
- ' Green:1',
- ' };',
- 'this.DoDefault = function (s) {',
- '};',
- 'this.DoConst = function (s) {',
- '};',
- 'this.aSet = {};'
- ]),
- LinesToStr([
- '$mod.DoDefault(rtl.refSet($mod.aSet));',
- '$mod.DoConst($mod.aSet);',
- '']));
- end;
- procedure TTestModule.TestSet_AsParams;
- begin
- StartProgram(false);
- Add('type TEnum = (Red,Blue);');
- Add('type TEnums = set of TEnum;');
- Add('procedure DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums);');
- Add('var vJ: TEnums;');
- Add('begin');
- Add(' vg:=vg;');
- Add(' vj:=vh;');
- Add(' vi:=vi;');
- Add(' doit(vg,vg,vg);');
- Add(' doit(vh,vh,vj);');
- Add(' doit(vi,vi,vi);');
- Add(' doit(vj,vj,vj);');
- Add('end;');
- Add('var i: TEnums;');
- Add('begin');
- Add(' doit(i,i,i);');
- ConvertProgram;
- CheckSource('TestSet_AsParams',
- LinesToStr([ // statements
- 'this.TEnum = {',
- ' "0": "Red",',
- ' Red: 0,',
- ' "1": "Blue",',
- ' Blue: 1',
- '};',
- 'this.DoIt = function (vG,vH,vI) {',
- ' var vJ = {};',
- ' vG = rtl.refSet(vG);',
- ' vJ = rtl.refSet(vH);',
- ' vI.set(rtl.refSet(vI.get()));',
- ' $mod.DoIt(rtl.refSet(vG), vG, {',
- ' get: function () {',
- ' return vG;',
- ' },',
- ' set: function (v) {',
- ' vG = v;',
- ' }',
- ' });',
- ' $mod.DoIt(rtl.refSet(vH), vH, {',
- ' get: function () {',
- ' return vJ;',
- ' },',
- ' set: function (v) {',
- ' vJ = v;',
- ' }',
- ' });',
- ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
- ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
- ' get: function () {',
- ' return vJ;',
- ' },',
- ' set: function (v) {',
- ' vJ = v;',
- ' }',
- ' });',
- '};',
- 'this.i = {};'
- ]),
- LinesToStr([
- '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
- ' p: $mod,',
- ' get: function () {',
- ' return this.p.i;',
- ' },',
- ' set: function (v) {',
- ' this.p.i = v;',
- ' }',
- '});'
- ]));
- end;
- procedure TTestModule.TestSet_Property;
- begin
- StartProgram(false);
- Add('type');
- Add(' TEnum = (Red,Blue);');
- Add(' TEnums = set of TEnum;');
- Add(' TObject = class');
- Add(' function GetColors: TEnums; external name ''GetColors'';');
- Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
- Add(' property Colors: TEnums read GetColors write SetColors;');
- Add(' end;');
- Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
- Add('begin end;');
- Add('var Obj: TObject;');
- Add('begin');
- Add(' Include(Obj.Colors,Red);');
- Add(' Exclude(Obj.Colors,Red);');
- //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
- ConvertProgram;
- CheckSource('TestSet_Property',
- LinesToStr([ // statements
- 'this.TEnum = {',
- ' "0": "Red",',
- ' Red: 0,',
- ' "1": "Blue",',
- ' Blue: 1',
- '};',
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'this.DoIt = function (i, j, k, l) {',
- '};',
- 'this.Obj = null;',
- '']),
- LinesToStr([
- '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
- '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
- '']));
- end;
- procedure TTestModule.TestSet_EnumConst;
- begin
- StartProgram(false);
- Add('type');
- Add(' TEnum = (Red,Blue);');
- Add(' TEnums = set of TEnum;');
- Add('const');
- Add(' Orange = red;');
- Add('var');
- Add(' Enum: tenum;');
- Add(' Enums: tenums;');
- Add('begin');
- Add(' Include(enums,orange);');
- Add(' Exclude(enums,orange);');
- Add(' if orange in enums then;');
- Add(' if orange in [orange,red] then;');
- ConvertProgram;
- CheckSource('TestEnumConst',
- LinesToStr([ // statements
- 'this.TEnum = {',
- ' "0": "Red",',
- ' Red: 0,',
- ' "1": "Blue",',
- ' Blue: 1',
- '};',
- 'this.Orange = $mod.TEnum.Red;',
- 'this.Enum = 0;',
- 'this.Enums = {};',
- '']),
- LinesToStr([
- '$mod.Enums = rtl.includeSet($mod.Enums, $mod.Orange);',
- '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.Orange);',
- 'if ($mod.Orange in $mod.Enums) ;',
- 'if ($mod.Orange in rtl.createSet($mod.Orange, $mod.TEnum.Red)) ;',
- '']));
- end;
- procedure TTestModule.TestSet_AnonymousEnumType;
- begin
- StartProgram(false);
- Add('type');
- Add(' TFlags = set of (red, green);');
- Add('const');
- Add(' favorite = red;');
- Add('var');
- Add(' f: TFlags;');
- Add(' i: longint;');
- Add('begin');
- Add(' Include(f,red);');
- Add(' Include(f,favorite);');
- Add(' i:=ord(red);');
- Add(' i:=ord(favorite);');
- Add(' i:=ord(low(TFlags));');
- Add(' i:=ord(low(f));');
- Add(' i:=ord(low(favorite));');
- Add(' i:=ord(high(TFlags));');
- Add(' i:=ord(high(f));');
- Add(' i:=ord(high(favorite));');
- Add(' f:=[green,favorite];');
- ConvertProgram;
- CheckSource('TestSet_AnonymousEnumType',
- LinesToStr([ // statements
- 'this.TFlags$a = {',
- ' "0": "red",',
- ' red: 0,',
- ' "1": "green",',
- ' green: 1',
- '};',
- 'this.favorite = $mod.TFlags$a.red;',
- 'this.f = {};',
- 'this.i = 0;',
- '']),
- LinesToStr([
- '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
- '$mod.f = rtl.includeSet($mod.f, $mod.favorite);',
- '$mod.i = $mod.TFlags$a.red;',
- '$mod.i = $mod.favorite;',
- '$mod.i = $mod.TFlags$a.red;',
- '$mod.i = $mod.TFlags$a.red;',
- '$mod.i = $mod.TFlags$a.red;',
- '$mod.i = $mod.TFlags$a.green;',
- '$mod.i = $mod.TFlags$a.green;',
- '$mod.i = $mod.TFlags$a.green;',
- '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.favorite);',
- '']));
- end;
- procedure TTestModule.TestSet_CharFail;
- begin
- StartProgram(false);
- Add('type');
- Add(' TChars = set of char;');
- Add('begin');
- SetExpectedPasResolverError('Not supported: set of Char',nNotSupportedX);
- ConvertProgram;
- end;
- procedure TTestModule.TestSet_BooleanFail;
- begin
- StartProgram(false);
- Add('type');
- Add(' TBools = set of boolean;');
- Add('begin');
- SetExpectedPasResolverError('Not supported: set of Boolean',nNotSupportedX);
- ConvertProgram;
- end;
- procedure TTestModule.TestSet_ConstEnum;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' TEnum = (red,blue,green);',
- ' TEnums = set of TEnum;',
- 'const',
- ' teAny = [low(TEnum)..high(TEnum)];',
- ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
- 'var',
- ' e: TEnum;',
- ' s: TEnums;',
- 'begin',
- ' if blue in teAny then;',
- ' if blue in teAny+[e] then;',
- ' if blue in teAny+teRedBlue then;',
- ' s:=teAny;',
- ' s:=teAny+[e];',
- ' s:=[e]+teAny;',
- ' s:=teAny+teRedBlue;',
- ' s:=teAny+teRedBlue+[e];',
- '']);
- ConvertProgram;
- CheckSource('TestSet_ConstEnum',
- LinesToStr([ // statements
- 'this.TEnum = {',
- ' "0": "red",',
- ' red: 0,',
- ' "1": "blue",',
- ' blue: 1,',
- ' "2": "green",',
- ' green: 2',
- '};',
- 'this.teAny = rtl.createSet(null, $mod.TEnum.red, $mod.TEnum.green);',
- 'this.teRedBlue = rtl.createSet(null, $mod.TEnum.red, $mod.TEnum.green - 1);',
- 'this.e = 0;',
- 'this.s = {};',
- '']),
- LinesToStr([
- 'if ($mod.TEnum.blue in $mod.teAny) ;',
- 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
- 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
- '$mod.s = rtl.refSet($mod.teAny);',
- '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
- '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
- '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
- '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
- '']));
- end;
- procedure TTestModule.TestSet_ConstChar;
- begin
- StartProgram(false);
- Add('const');
- Add(' LowChars = [''a''..''z''];');
- Add(' Chars = LowChars+[''A''..''Z''];');
- Add('var');
- Add(' c: char;');
- Add(' s: string;');
- Add('begin');
- Add(' if c in lowchars then ;');
- Add(' if ''a'' in lowchars then ;');
- Add(' if s[1] in lowchars then ;');
- Add(' if c in chars then ;');
- Add(' if c in [''a''..''z'',''_''] then ;');
- Add(' if ''b'' in [''a''..''z'',''_''] then ;');
- ConvertProgram;
- CheckSource('TestSet_ConstChar',
- LinesToStr([ // statements
- 'this.LowChars = rtl.createSet(null, 97, 122);',
- 'this.Chars = rtl.unionSet($mod.LowChars, rtl.createSet(null, 65, 90));',
- 'this.c = "";',
- 'this.s = "";',
- '']),
- LinesToStr([
- 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
- 'if (97 in $mod.LowChars) ;',
- 'if ($mod.s.charCodeAt(1 - 1) in $mod.LowChars) ;',
- 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
- 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
- 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
- '']));
- end;
- procedure TTestModule.TestNestBegin;
- begin
- StartProgram(false);
- Add('begin');
- Add(' begin');
- Add(' begin');
- Add(' end;');
- Add(' begin');
- Add(' if true then ;');
- Add(' end;');
- Add(' end;');
- ConvertProgram;
- CheckSource('TestNestBegin',
- '',
- 'if (true) ;');
- end;
- procedure TTestModule.TestUnitImplVars;
- begin
- StartUnit(false);
- Add('interface');
- Add('implementation');
- Add('var');
- Add(' V1:longint;');
- Add(' V2:longint = 3;');
- Add(' V3:string = ''abc'';');
- ConvertUnit;
- CheckSource('TestUnitImplVars',
- LinesToStr([ // statements
- 'var $impl = $mod.$impl;',
- '']),
- '', // this.$init
- LinesToStr([ // implementation
- '$impl.V1 = 0;',
- '$impl.V2 = 3;',
- '$impl.V3 = "abc";',
- '']) );
- end;
- procedure TTestModule.TestUnitImplConsts;
- begin
- StartUnit(false);
- Add('interface');
- Add('implementation');
- Add('const');
- Add(' v1 = 3;');
- Add(' v2:longint = 4;');
- Add(' v3:string = ''abc'';');
- ConvertUnit;
- CheckSource('TestUnitImplConsts',
- LinesToStr([ // statements
- 'var $impl = $mod.$impl;',
- '']),
- '', // this.$init
- LinesToStr([ // implementation
- '$impl.v1 = 3;',
- '$impl.v2 = 4;',
- '$impl.v3 = "abc";',
- '']) );
- end;
- procedure TTestModule.TestUnitImplRecord;
- begin
- StartUnit(false);
- Add('interface');
- Add('implementation');
- Add('type');
- Add(' TMyRecord = record');
- Add(' i: longint;');
- Add(' end;');
- Add('var aRec: TMyRecord;');
- Add('initialization');
- Add(' arec.i:=3;');
- ConvertUnit;
- CheckSource('TestUnitImplRecord',
- LinesToStr([ // statements
- 'var $impl = $mod.$impl;',
- '']),
- // this.$init
- '$impl.aRec.i = 3;',
- LinesToStr([ // implementation
- '$impl.TMyRecord = function (s) {',
- ' if (s) {',
- ' this.i = s.i;',
- ' } else {',
- ' this.i = 0;',
- ' };',
- ' this.$equal = function (b) {',
- ' return this.i == b.i;',
- ' };',
- '};',
- '$impl.aRec = new $impl.TMyRecord();',
- '']) );
- end;
- procedure TTestModule.TestRenameJSNameConflict;
- begin
- StartProgram(false);
- Add('var apply: longint;');
- Add('var bind: longint;');
- Add('var call: longint;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestRenameJSNameConflict',
- LinesToStr([ // statements
- 'this.Apply = 0;',
- 'this.Bind = 0;',
- 'this.Call = 0;'
- ]),
- LinesToStr([ // this.$main
- ''
- ]));
- end;
- procedure TTestModule.TestLocalConst;
- begin
- StartProgram(false);
- Add('procedure DoIt;');
- Add('const');
- Add(' cA: longint = 1;');
- Add(' cB = 2;');
- Add(' procedure Sub;');
- Add(' const');
- Add(' csA = 3;');
- Add(' cB: double = 4;');
- Add(' begin');
- Add(' cb:=cb+csa;');
- Add(' ca:=ca+csa+5;');
- Add(' end;');
- Add('begin');
- Add(' ca:=ca+cb+6;');
- Add('end;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestLocalConst',
- LinesToStr([
- 'var cA = 1;',
- 'var cB = 2;',
- 'var csA = 3;',
- 'var cB$1 = 4;',
- 'this.DoIt = function () {',
- ' function Sub() {',
- ' cB$1 = cB$1 + csA;',
- ' cA = (cA + csA) + 5;',
- ' };',
- ' cA = (cA + cB) + 6;',
- '};'
- ]),
- LinesToStr([
- ]));
- end;
- procedure TTestModule.TestVarExternal;
- begin
- StartProgram(false);
- Add('var');
- Add(' NaN: double; external name ''Global.NaN'';');
- Add(' d: double;');
- Add('begin');
- Add(' d:=NaN;');
- ConvertProgram;
- CheckSource('TestVarExternal',
- LinesToStr([
- 'this.d = 0.0;'
- ]),
- LinesToStr([
- '$mod.d = Global.NaN;'
- ]));
- end;
- procedure TTestModule.TestVarExternalOtherUnit;
- begin
- AddModuleWithIntfImplSrc('unit2.pas',
- LinesToStr([
- 'var NaN: double; external name ''Global.NaN'';',
- 'var iV: longint;'
- ]),
- '');
- StartUnit(true);
- Add('interface');
- Add('uses unit2;');
- Add('implementation');
- Add('var');
- Add(' d: double;');
- Add(' i: longint; external name ''$i'';');
- Add('begin');
- Add(' d:=nan;');
- Add(' d:=uNit2.nan;');
- Add(' d:=test1.d;');
- Add(' i:=iv;');
- Add(' i:=uNit2.iv;');
- Add(' i:=test1.i;');
- ConvertUnit;
- CheckSource('TestVarExternalOtherUnit',
- LinesToStr([
- 'var $impl = $mod.$impl;',
- '']),
- LinesToStr([ // this.$init
- '$impl.d = Global.NaN;',
- '$impl.d = Global.NaN;',
- '$impl.d = $impl.d;',
- '$i = pas.unit2.iV;',
- '$i = pas.unit2.iV;',
- '$i = $i;',
- '']),
- LinesToStr([ // implementation
- '$impl.d = 0.0;',
- '']) );
- end;
- procedure TTestModule.TestDouble;
- begin
- StartProgram(false);
- Add([
- 'var',
- ' d: double;',
- 'begin',
- ' d:=1.0;',
- ' d:=1.0/3.0;',
- ' d:=1/3;',
- ' d:=5.0E-324;',
- ' d:=1.7E308;',
- ' d:=10**3;',
- ' d:=10 mod 3;',
- ' d:=10 div 3;',
- '']);
- ConvertProgram;
- CheckSource('TestDouble',
- LinesToStr([
- 'this.d=0.0;'
- ]),
- LinesToStr([
- '$mod.d = 1.0;',
- '$mod.d = 1.0 / 3.0;',
- '$mod.d = 1 / 3;',
- '$mod.d = 5.0E-324;',
- '$mod.d = 1.7E308;',
- '$mod.d = Math.pow(10, 3);',
- '$mod.d = 10 % 3;',
- '$mod.d = Math.floor(10 / 3);',
- '']));
- end;
- procedure TTestModule.TestCharConst;
- begin
- StartProgram(false);
- Add('const');
- Add(' c: char = ''1'';');
- Add('begin');
- Add(' c:=#0;');
- Add(' c:=#1;');
- Add(' c:=#9;');
- Add(' c:=#10;');
- Add(' c:=#13;');
- Add(' c:=#31;');
- Add(' c:=#32;');
- Add(' c:=#$A;');
- Add(' c:=#$0A;');
- Add(' c:=#$b;');
- Add(' c:=#$0b;');
- Add(' c:=^A;');
- Add(' c:=''"'';');
- ConvertProgram;
- CheckSource('TestCharConst',
- LinesToStr([
- 'this.c="1";'
- ]),
- LinesToStr([
- '$mod.c="\x00";',
- '$mod.c="\x01";',
- '$mod.c="\t";',
- '$mod.c="\n";',
- '$mod.c="\r";',
- '$mod.c="\x1F";',
- '$mod.c=" ";',
- '$mod.c="\n";',
- '$mod.c="\n";',
- '$mod.c="\x0B";',
- '$mod.c="\x0B";',
- '$mod.c="\x01";',
- '$mod.c=''"'';'
- ]));
- end;
- procedure TTestModule.TestChar_Compare;
- begin
- StartProgram(false);
- Add('var');
- Add(' c: char;');
- Add(' b: boolean;');
- Add('begin');
- Add(' b:=c=''1'';');
- Add(' b:=''2''=c;');
- Add(' b:=''3''=''4'';');
- Add(' b:=c<>''5'';');
- Add(' b:=''6''<>c;');
- Add(' b:=c>''7'';');
- Add(' b:=''8''>c;');
- Add(' b:=c>=''9'';');
- Add(' b:=''A''>=c;');
- Add(' b:=c<''B'';');
- Add(' b:=''C''<c;');
- Add(' b:=c<=''D'';');
- Add(' b:=''E''<=c;');
- ConvertProgram;
- CheckSource('TestChar_Compare',
- LinesToStr([
- 'this.c="";',
- 'this.b = false;'
- ]),
- LinesToStr([
- '$mod.b = $mod.c == "1";',
- '$mod.b = "2" == $mod.c;',
- '$mod.b = "3" == "4";',
- '$mod.b = $mod.c != "5";',
- '$mod.b = "6" != $mod.c;',
- '$mod.b = $mod.c > "7";',
- '$mod.b = "8" > $mod.c;',
- '$mod.b = $mod.c >= "9";',
- '$mod.b = "A" >= $mod.c;',
- '$mod.b = $mod.c < "B";',
- '$mod.b = "C" < $mod.c;',
- '$mod.b = $mod.c <= "D";',
- '$mod.b = "E" <= $mod.c;',
- '']));
- end;
- procedure TTestModule.TestChar_Ord;
- begin
- StartProgram(false);
- Add('var');
- Add(' c: char;');
- Add(' i: longint;');
- Add(' s: string;');
- Add('begin');
- Add(' i:=ord(c);');
- Add(' i:=ord(s[i]);');
- ConvertProgram;
- CheckSource('TestChar_Ord',
- LinesToStr([
- 'this.c = "";',
- 'this.i = 0;',
- 'this.s = "";'
- ]),
- LinesToStr([
- '$mod.i = $mod.c.charCodeAt();',
- '$mod.i = $mod.s.charCodeAt($mod.i-1);',
- '']));
- end;
- procedure TTestModule.TestChar_Chr;
- begin
- StartProgram(false);
- Add('var');
- Add(' c: char;');
- Add(' i: longint;');
- Add('begin');
- Add(' c:=chr(i);');
- ConvertProgram;
- CheckSource('TestChar_Chr',
- LinesToStr([
- 'this.c = "";',
- 'this.i = 0;'
- ]),
- LinesToStr([
- '$mod.c = String.fromCharCode($mod.i);',
- '']));
- end;
- procedure TTestModule.TestStringConst;
- begin
- StartProgram(false);
- Add('var');
- Add(' s: string = ''abc'';');
- Add('begin');
- Add(' s:='''';');
- Add(' s:=#13#10;');
- Add(' s:=#9''foo'';');
- Add(' s:=#$A9;');
- Add(' s:=''foo''#13''bar'';');
- Add(' s:=''"'';');
- Add(' s:=''"''''"'';');
- ConvertProgram;
- CheckSource('TestStringConst',
- LinesToStr([
- 'this.s="abc";'
- ]),
- LinesToStr([
- '$mod.s="";',
- '$mod.s="\r\n";',
- '$mod.s="\tfoo";',
- '$mod.s="©";',
- '$mod.s="foo\rbar";',
- '$mod.s=''"'';',
- '$mod.s=''"\''"'';'
- ]));
- end;
- procedure TTestModule.TestString_Length;
- begin
- StartProgram(false);
- Add('const c = ''foo'';');
- Add('var');
- Add(' s: string;');
- Add(' i: longint;');
- Add('begin');
- Add(' i:=length(s);');
- Add(' i:=length(s+s);');
- Add(' i:=length(''abc'');');
- Add(' i:=length(c);');
- ConvertProgram;
- CheckSource('TestString_Length',
- LinesToStr([
- 'this.c = "foo";',
- 'this.s = "";',
- 'this.i = 0;',
- '']),
- LinesToStr([
- '$mod.i = $mod.s.length;',
- '$mod.i = ($mod.s+$mod.s).length;',
- '$mod.i = "abc".length;',
- '$mod.i = $mod.c.length;',
- '']));
- end;
- procedure TTestModule.TestString_Compare;
- begin
- StartProgram(false);
- Add('var');
- Add(' s, t: string;');
- Add(' b: boolean;');
- Add('begin');
- Add(' b:=s=t;');
- Add(' b:=s<>t;');
- Add(' b:=s>t;');
- Add(' b:=s>=t;');
- Add(' b:=s<t;');
- Add(' b:=s<=t;');
- ConvertProgram;
- CheckSource('TestString_Compare',
- LinesToStr([ // statements
- 'this.s = "";',
- 'this.t = "";',
- 'this.b =false;'
- ]),
- LinesToStr([ // this.$main
- '$mod.b = $mod.s == $mod.t;',
- '$mod.b = $mod.s != $mod.t;',
- '$mod.b = $mod.s > $mod.t;',
- '$mod.b = $mod.s >= $mod.t;',
- '$mod.b = $mod.s < $mod.t;',
- '$mod.b = $mod.s <= $mod.t;',
- '']));
- end;
- procedure TTestModule.TestString_SetLength;
- begin
- StartProgram(false);
- Add([
- 'procedure DoIt(var s: string);',
- 'begin',
- ' SetLength(s,2);',
- 'end;',
- 'var s: string;',
- 'begin',
- ' SetLength(s,3);',
- '']);
- ConvertProgram;
- CheckSource('TestString_SetLength',
- LinesToStr([ // statements
- 'this.DoIt = function (s) {',
- ' s.set(rtl.strSetLength(s.get(), 2));',
- '};',
- 'this.s = "";',
- '']),
- LinesToStr([ // this.$main
- '$mod.s = rtl.strSetLength($mod.s, 3);'
- ]));
- end;
- procedure TTestModule.TestString_CharAt;
- begin
- StartProgram(false);
- Add('var');
- Add(' s: string;');
- Add(' c: char;');
- Add(' b: boolean;');
- Add('begin');
- Add(' b:= s[1] = c;');
- Add(' b:= c = s[1];');
- Add(' b:= c <> s[1];');
- Add(' b:= c > s[1];');
- Add(' b:= c >= s[1];');
- Add(' b:= c < s[1];');
- Add(' b:= c <= s[1];');
- Add(' s[1] := c;');
- ConvertProgram;
- CheckSource('TestString_CharAt',
- LinesToStr([ // statements
- 'this.s = "";',
- 'this.c = "";',
- 'this.b = false;'
- ]),
- LinesToStr([ // this.$main
- '$mod.b = $mod.s.charAt(1-1) == $mod.c;',
- '$mod.b = $mod.c == $mod.s.charAt(1 - 1);',
- '$mod.b = $mod.c != $mod.s.charAt(1 - 1);',
- '$mod.b = $mod.c > $mod.s.charAt(1 - 1);',
- '$mod.b = $mod.c >= $mod.s.charAt(1 - 1);',
- '$mod.b = $mod.c < $mod.s.charAt(1 - 1);',
- '$mod.b = $mod.c <= $mod.s.charAt(1 - 1);',
- '$mod.s = rtl.setCharAt($mod.s, 1, $mod.c);',
- '']));
- end;
- procedure TTestModule.TestStr;
- begin
- StartProgram(false);
- Add('var');
- Add(' b: boolean;');
- Add(' i: longint;');
- Add(' d: double;');
- Add(' s: string;');
- Add('begin');
- Add(' str(b,s);');
- Add(' str(i,s);');
- Add(' str(d,s);');
- Add(' str(i:3,s);');
- Add(' str(d:3:2,s);');
- Add(' s:=str(b);');
- Add(' s:=str(i);');
- Add(' s:=str(d);');
- Add(' s:=str(i,i);');
- Add(' s:=str(i:3);');
- Add(' s:=str(d:3:2);');
- Add(' s:=str(i:4,i);');
- Add(' s:=str(i,i:5);');
- Add(' s:=str(i:4,i:5);');
- Add(' s:=str(s,s);');
- Add(' s:=str(s,''foo'');');
- ConvertProgram;
- CheckSource('TestStr',
- LinesToStr([ // statements
- 'this.b = false;',
- 'this.i = 0;',
- 'this.d = 0.0;',
- 'this.s = "";',
- '']),
- LinesToStr([ // this.$main
- '$mod.s = ""+$mod.b;',
- '$mod.s = ""+$mod.i;',
- '$mod.s = ""+$mod.d;',
- '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
- '$mod.s = rtl.spaceLeft($mod.d.toFixed(2),3);',
- '$mod.s = ""+$mod.b;',
- '$mod.s = ""+$mod.i;',
- '$mod.s = ""+$mod.d;',
- '$mod.s = (""+$mod.i)+$mod.i;',
- '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
- '$mod.s = rtl.spaceLeft($mod.d.toFixed(2),3);',
- '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
- '$mod.s = ("" + $mod.i) + rtl.spaceLeft("" + $mod.i, 5);',
- '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
- '$mod.s = $mod.s + $mod.s;',
- '$mod.s = $mod.s + "foo";',
- '']));
- end;
- procedure TTestModule.TestBaseType_AnsiStringFail;
- begin
- StartProgram(false);
- Add('var s: AnsiString');
- SetExpectedPasResolverError('identifier not found "AnsiString"',nIdentifierNotFound);
- ConvertProgram;
- end;
- procedure TTestModule.TestBaseType_UnicodeStringFail;
- begin
- StartProgram(false);
- Add('var s: UnicodeString');
- SetExpectedPasResolverError('identifier not found "UnicodeString"',nIdentifierNotFound);
- ConvertProgram;
- end;
- procedure TTestModule.TestBaseType_ShortStringFail;
- begin
- StartProgram(false);
- Add('var s: ShortString');
- SetExpectedPasResolverError('identifier not found "ShortString"',nIdentifierNotFound);
- ConvertProgram;
- end;
- procedure TTestModule.TestBaseType_RawByteStringFail;
- begin
- StartProgram(false);
- Add('var s: RawByteString');
- SetExpectedPasResolverError('identifier not found "RawByteString"',nIdentifierNotFound);
- ConvertProgram;
- end;
- procedure TTestModule.TestTypeShortstring_Fail;
- begin
- StartProgram(false);
- Add('type t = string[12];');
- Add('var s: t;');
- Add('begin');
- SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
- ConvertProgram;
- end;
- procedure TTestModule.TestProcTwoArgs;
- begin
- StartProgram(false);
- Add('procedure Test(a,b: longint);');
- Add('begin');
- Add('end;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestProcTwoArgs',
- LinesToStr([ // statements
- 'this.Test = function (a,b) {',
- '};'
- ]),
- LinesToStr([ // this.$main
- ''
- ]));
- end;
- procedure TTestModule.TestProc_DefaultValue;
- begin
- StartProgram(false);
- Add('procedure p1(i: longint = 1);');
- Add('begin');
- Add('end;');
- Add('procedure p2(i: longint = 1; c: char = ''a'');');
- Add('begin');
- Add('end;');
- Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
- Add('begin');
- Add('end;');
- Add('begin');
- Add(' p1;');
- Add(' p1();');
- Add(' p1(11);');
- Add(' p2;');
- Add(' p2();');
- Add(' p2(12);');
- Add(' p2(13,''b'');');
- Add(' p3();');
- ConvertProgram;
- CheckSource('TestProc_DefaultValue',
- LinesToStr([ // statements
- 'this.p1 = function (i) {',
- '};',
- 'this.p2 = function (i,c) {',
- '};',
- 'this.p3 = function (d,b,s) {',
- '};'
- ]),
- LinesToStr([ // this.$main
- ' $mod.p1(1);',
- ' $mod.p1(1);',
- ' $mod.p1(11);',
- ' $mod.p2(1,"a");',
- ' $mod.p2(1,"a");',
- ' $mod.p2(12,"a");',
- ' $mod.p2(13,"b");',
- ' $mod.p3(1.0,false,"abc");'
- ]));
- end;
- procedure TTestModule.TestFunctionInt;
- begin
- StartProgram(false);
- Add('function MyTest(Bar: longint): longint;');
- Add('begin');
- Add(' Result:=2*bar');
- Add('end;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestFunctionInt',
- LinesToStr([ // statements
- 'this.MyTest = function (Bar) {',
- ' var Result = 0;',
- ' Result = 2*Bar;',
- ' return Result;',
- '};'
- ]),
- LinesToStr([ // this.$main
- ''
- ]));
- end;
- procedure TTestModule.TestFunctionString;
- begin
- StartProgram(false);
- Add('function Test(Bar: string): string;');
- Add('begin');
- Add(' Result:=bar+BAR');
- Add('end;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestFunctionString',
- LinesToStr([ // statements
- 'this.Test = function (Bar) {',
- ' var Result = "";',
- ' Result = Bar+Bar;',
- ' return Result;',
- '};'
- ]),
- LinesToStr([ // this.$main
- ''
- ]));
- end;
- procedure TTestModule.TestForLoop;
- begin
- StartProgram(false);
- Add('var');
- Add(' vI, vJ, vN: longint;');
- Add('begin');
- Add(' VJ:=0;');
- Add(' VN:=3;');
- Add(' for VI:=1 to VN do');
- Add(' begin');
- Add(' VJ:=VJ+VI;');
- Add(' end;');
- ConvertProgram;
- CheckSource('TestForLoop',
- LinesToStr([ // statements
- 'this.vI = 0;',
- 'this.vJ = 0;',
- 'this.vN = 0;'
- ]),
- LinesToStr([ // this.$main
- ' $mod.vJ = 0;',
- ' $mod.vN = 3;',
- ' var $loopend1 = $mod.vN;',
- ' for ($mod.vI = 1; $mod.vI <= $loopend1; $mod.vI++) {',
- ' $mod.vJ = $mod.vJ + $mod.vI;',
- ' };',
- ' if ($mod.vI > $loopend1) $mod.vI--;'
- ]));
- end;
- procedure TTestModule.TestForLoopInFunction;
- begin
- StartProgram(false);
- Add('function SumNumbers(Count: longint): longint;');
- Add('var');
- Add(' vI, vJ: longint;');
- Add('begin');
- Add(' vj:=0;');
- Add(' for vi:=1 to count do');
- Add(' begin');
- Add(' vj:=vj+vi;');
- Add(' end;');
- Add('end;');
- Add('begin');
- Add(' sumnumbers(3);');
- ConvertProgram;
- CheckSource('TestForLoopInFunction',
- LinesToStr([ // statements
- 'this.SumNumbers = function (Count) {',
- ' var Result = 0;',
- ' var vI = 0;',
- ' var vJ = 0;',
- ' vJ = 0;',
- ' var $loopend1 = Count;',
- ' for (vI = 1; vI <= $loopend1; vI++) {',
- ' vJ = vJ + vI;',
- ' };',
- ' return Result;',
- '};'
- ]),
- LinesToStr([ // $mod.$main
- ' $mod.SumNumbers(3);'
- ]));
- end;
- procedure TTestModule.TestForLoop_ReadVarAfter;
- begin
- StartProgram(false);
- Add('var');
- Add(' vI: longint;');
- Add('begin');
- Add(' for vi:=1 to 2 do ;');
- Add(' if vi=3 then ;');
- ConvertProgram;
- CheckSource('TestForLoop',
- LinesToStr([ // statements
- 'this.vI = 0;'
- ]),
- LinesToStr([ // this.$main
- ' var $loopend1 = 2;',
- ' for ($mod.vI = 1; $mod.vI <= $loopend1; $mod.vI++);',
- ' if($mod.vI>$loopend1)$mod.vI--;',
- ' if ($mod.vI==3) ;'
- ]));
- end;
- procedure TTestModule.TestForLoop_Nested;
- begin
- StartProgram(false);
- Add('function SumNumbers(Count: longint): longint;');
- Add('var');
- Add(' vI, vJ, vK: longint;');
- Add('begin');
- Add(' VK:=0;');
- Add(' for VI:=1 to count do');
- Add(' begin');
- Add(' for vj:=1 to vi do');
- Add(' begin');
- Add(' vk:=VK+VI;');
- Add(' end;');
- Add(' end;');
- Add('end;');
- Add('begin');
- Add(' sumnumbers(3);');
- ConvertProgram;
- CheckSource('TestForLoopInFunction',
- LinesToStr([ // statements
- 'this.SumNumbers = function (Count) {',
- ' var Result = 0;',
- ' var vI = 0;',
- ' var vJ = 0;',
- ' var vK = 0;',
- ' vK = 0;',
- ' var $loopend1 = Count;',
- ' for (vI = 1; vI <= $loopend1; vI++) {',
- ' var $loopend2 = vI;',
- ' for (vJ = 1; vJ <= $loopend2; vJ++) {',
- ' vK = vK + vI;',
- ' };',
- ' };',
- ' return Result;',
- '};'
- ]),
- LinesToStr([ // $mod.$main
- ' $mod.SumNumbers(3);'
- ]));
- end;
- procedure TTestModule.TestRepeatUntil;
- begin
- StartProgram(false);
- Add('var');
- Add(' vI, vJ, vN: longint;');
- Add('begin');
- Add(' vn:=3;');
- Add(' vj:=0;');
- Add(' VI:=0;');
- Add(' repeat');
- Add(' VI:=vi+1;');
- Add(' vj:=VJ+vI;');
- Add(' until vi>=vn');
- ConvertProgram;
- CheckSource('TestRepeatUntil',
- LinesToStr([ // statements
- 'this.vI = 0;',
- 'this.vJ = 0;',
- 'this.vN = 0;'
- ]),
- LinesToStr([ // $mod.$main
- ' $mod.vN = 3;',
- ' $mod.vJ = 0;',
- ' $mod.vI = 0;',
- ' do{',
- ' $mod.vI = $mod.vI + 1;',
- ' $mod.vJ = $mod.vJ + $mod.vI;',
- ' }while(!($mod.vI>=$mod.vN));'
- ]));
- end;
- procedure TTestModule.TestAsmBlock;
- begin
- StartProgram(false);
- Add('var');
- Add(' vI: longint;');
- Add('begin');
- Add(' vi:=1;');
- Add(' asm');
- Add(' if (vI==1) {');
- Add(' vI=2;');
- Add(' }');
- Add(' if (vI==2){ vI=3; }');
- Add(' end;');
- Add(' VI:=4;');
- ConvertProgram;
- CheckSource('TestAsmBlock',
- LinesToStr([ // statements
- 'this.vI = 0;'
- ]),
- LinesToStr([ // $mod.$main
- '$mod.vI = 1;',
- 'if (vI==1) {',
- ' vI=2;',
- '}',
- 'if (vI==2){ vI=3; }',
- ';',
- '$mod.vI = 4;'
- ]));
- end;
- procedure TTestModule.TestAsmPas_Impl;
- begin
- StartUnit(false);
- Add('interface');
- Add('const cIntf: longint = 1;');
- Add('var vIntf: longint;');
- Add('implementation');
- Add('const cImpl: longint = 2;');
- Add('var vImpl: longint;');
- Add('procedure DoIt;');
- Add('const cLoc: longint = 3;');
- Add('var vLoc: longint;');
- Add('begin;');
- Add(' asm');
- //Add(' pas(vIntf)=pas(cIntf);');
- //Add(' pas(vImpl)=pas(cImpl);');
- //Add(' pas(vLoc)=pas(cLoc);');
- Add(' end;');
- Add('end;');
- ConvertUnit;
- // ToDo: check use analyzer
- CheckSource('TestAsmPas_Impl',
- LinesToStr([
- 'var $impl = $mod.$impl;',
- 'this.cIntf = 1;',
- 'this.vIntf = 0;',
- '']),
- '', // this.$init
- LinesToStr([ // implementation
- 'var cLoc = 3;',
- '$impl.cImpl = 2;',
- '$impl.vImpl = 0;',
- '$impl.DoIt = function () {',
- ' var vLoc = 0;',
- '};',
- '']) );
- end;
- procedure TTestModule.TestTryFinally;
- begin
- StartProgram(false);
- Add('var i: longint;');
- Add('begin');
- Add(' try');
- Add(' i:=0; i:=2 div i;');
- Add(' finally');
- Add(' i:=3');
- Add(' end;');
- ConvertProgram;
- CheckSource('TestTryFinally',
- LinesToStr([ // statements
- 'this.i = 0;'
- ]),
- LinesToStr([ // $mod.$main
- 'try {',
- ' $mod.i = 0;',
- ' $mod.i = Math.floor(2 / $mod.i);',
- '} finally {',
- ' $mod.i = 3;',
- '};'
- ]));
- end;
- procedure TTestModule.TestTryExcept;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class end;');
- Add(' Exception = class Msg: string; end;');
- Add(' EInvalidCast = class(Exception) end;');
- Add('var vI: longint;');
- Add('begin');
- Add(' try');
- Add(' vi:=1;');
- Add(' except');
- Add(' vi:=2');
- Add(' end;');
- Add(' try');
- Add(' vi:=3;');
- Add(' except');
- Add(' raise;');
- Add(' end;');
- Add(' try');
- Add(' VI:=4;');
- Add(' except');
- Add(' on einvalidcast do');
- Add(' raise;');
- Add(' on E: exception do');
- Add(' if e.msg='''' then');
- Add(' raise e;');
- Add(' else');
- Add(' vi:=5');
- Add(' end;');
- Add(' try');
- Add(' VI:=6;');
- Add(' except');
- Add(' on einvalidcast do ;');
- Add(' end;');
- ConvertProgram;
- CheckSource('TestTryExcept',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
- ' this.$init = function () {',
- ' $mod.TObject.$init.call(this);',
- ' this.Msg = "";',
- ' };',
- '});',
- 'rtl.createClass($mod, "EInvalidCast", $mod.Exception, function () {',
- '});',
- 'this.vI = 0;'
- ]),
- LinesToStr([ // $mod.$main
- 'try {',
- ' $mod.vI = 1;',
- '} catch ($e) {',
- ' $mod.vI = 2;',
- '};',
- 'try {',
- ' $mod.vI = 3;',
- '} catch ($e) {',
- ' throw $e;',
- '};',
- 'try {',
- ' $mod.vI = 4;',
- '} catch ($e) {',
- ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
- ' throw $e',
- ' } else if ($mod.Exception.isPrototypeOf($e)) {',
- ' var E = $e;',
- ' if (E.Msg == "") throw E;',
- ' } else {',
- ' $mod.vI = 5;',
- ' }',
- '};',
- 'try {',
- ' $mod.vI = 6;',
- '} catch ($e) {',
- ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
- ' } else throw $e',
- '};',
- '']));
- end;
- procedure TTestModule.TestCaseOf;
- begin
- StartProgram(false);
- Add('var vI: longint;');
- Add('begin');
- Add(' case vi of');
- Add(' 1: ;');
- Add(' 2: vi:=3;');
- Add(' else');
- Add(' VI:=4');
- Add(' end;');
- ConvertProgram;
- CheckSource('TestCaseOf',
- LinesToStr([ // statements
- 'this.vI = 0;'
- ]),
- LinesToStr([ // $mod.$main
- 'var $tmp1 = $mod.vI;',
- 'if ($tmp1 == 1) {} else if ($tmp1 == 2){ $mod.vI = 3 }else {',
- ' $mod.vI = 4;',
- '};'
- ]));
- end;
- procedure TTestModule.TestCaseOf_UseSwitch;
- begin
- StartProgram(false);
- Converter.UseSwitchStatement:=true;
- Add('var Vi: longint;');
- Add('begin');
- Add(' case vi of');
- Add(' 1: ;');
- Add(' 2: VI:=3;');
- Add(' else');
- Add(' vi:=4');
- Add(' end;');
- ConvertProgram;
- CheckSource('TestCaseOf_UseSwitch',
- LinesToStr([ // statements
- 'this.Vi = 0;'
- ]),
- LinesToStr([ // $mod.$main
- 'switch ($mod.Vi) {',
- 'case 1:',
- ' break;',
- 'case 2:',
- ' $mod.Vi = 3;',
- ' break;',
- 'default:',
- ' $mod.Vi = 4;',
- '};'
- ]));
- end;
- procedure TTestModule.TestCaseOfNoElse;
- begin
- StartProgram(false);
- Add('var Vi: longint;');
- Add('begin');
- Add(' case vi of');
- Add(' 1: begin vi:=2; VI:=3; end;');
- Add(' end;');
- ConvertProgram;
- CheckSource('TestCaseOfNoElse',
- LinesToStr([ // statements
- 'this.Vi = 0;'
- ]),
- LinesToStr([ // $mod.$main
- 'var $tmp1 = $mod.Vi;',
- 'if ($tmp1 == 1) {',
- ' $mod.Vi = 2;',
- ' $mod.Vi = 3;',
- '};'
- ]));
- end;
- procedure TTestModule.TestCaseOfNoElse_UseSwitch;
- begin
- StartProgram(false);
- Converter.UseSwitchStatement:=true;
- Add('var vI: longint;');
- Add('begin');
- Add(' case vi of');
- Add(' 1: begin VI:=2; vi:=3; end;');
- Add(' end;');
- ConvertProgram;
- CheckSource('TestCaseOfNoElse_UseSwitch',
- LinesToStr([ // statements
- 'this.vI = 0;'
- ]),
- LinesToStr([ // $mod.$main
- 'switch ($mod.vI) {',
- 'case 1:',
- ' $mod.vI = 2;',
- ' $mod.vI = 3;',
- ' break;',
- '};'
- ]));
- end;
- procedure TTestModule.TestCaseOfRange;
- begin
- StartProgram(false);
- Add('var vI: longint;');
- Add('begin');
- Add(' case vi of');
- Add(' 1..3: vi:=14;');
- Add(' 4,5: vi:=16;');
- Add(' 6..7,9..10: ;');
- Add(' else ;');
- Add(' end;');
- ConvertProgram;
- CheckSource('TestCaseOfRange',
- LinesToStr([ // statements
- 'this.vI = 0;'
- ]),
- LinesToStr([ // $mod.$main
- 'var $tmp1 = $mod.vI;',
- 'if (($tmp1 >= 1) && ($tmp1 <= 3)){',
- ' $mod.vI = 14',
- '} else if (($tmp1 == 4) || ($tmp1 == 5)){',
- ' $mod.vI = 16',
- '} else if ((($tmp1 >= 6) && ($tmp1 <= 7)) || (($tmp1 >= 9) && ($tmp1 <= 10))) ;'
- ]));
- end;
- procedure TTestModule.TestArray_Dynamic;
- begin
- StartProgram(false);
- Add('type');
- Add(' TArrayInt = array of longint;');
- Add('var');
- Add(' Arr: TArrayInt;');
- Add(' i: longint;');
- Add(' b: boolean;');
- Add('begin');
- Add(' SetLength(arr,3);');
- Add(' arr[0]:=4;');
- Add(' arr[1]:=length(arr)+arr[0];');
- Add(' arr[i]:=5;');
- Add(' arr[arr[i]]:=arr[6];');
- Add(' i:=low(arr);');
- Add(' i:=high(arr);');
- Add(' b:=Assigned(arr);');
- ConvertProgram;
- CheckSource('TestArray_Dynamic',
- LinesToStr([ // statements
- 'this.Arr = [];',
- 'this.i = 0;',
- 'this.b = false;'
- ]),
- LinesToStr([ // $mod.$main
- '$mod.Arr = rtl.arraySetLength($mod.Arr,3,0);',
- '$mod.Arr[0] = 4;',
- '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
- '$mod.Arr[$mod.i] = 5;',
- '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
- '$mod.i = 0;',
- '$mod.i = rtl.length($mod.Arr) - 1;',
- '$mod.b = rtl.length($mod.Arr) > 0;',
- '']));
- end;
- procedure TTestModule.TestArray_Dynamic_Nil;
- begin
- StartProgram(false);
- Add('type');
- Add(' TArrayInt = array of longint;');
- Add('var');
- Add(' Arr: TArrayInt;');
- Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
- Add('begin');
- Add(' arr:=nil;');
- Add(' if arr=nil then;');
- Add(' if nil=arr then;');
- Add(' if arr<>nil then;');
- Add(' if nil<>arr then;');
- Add(' DoIt(nil,nil);');
- ConvertProgram;
- CheckSource('TestArray_Dynamic',
- LinesToStr([ // statements
- 'this.Arr = [];',
- 'this.DoIt = function(i,j){',
- '};'
- ]),
- LinesToStr([ // $mod.$main
- '$mod.Arr = [];',
- 'if (rtl.length($mod.Arr) == 0) ;',
- 'if (rtl.length($mod.Arr) == 0) ;',
- 'if (rtl.length($mod.Arr) > 0) ;',
- 'if (rtl.length($mod.Arr) > 0) ;',
- '$mod.DoIt([],[]);',
- '']));
- end;
- procedure TTestModule.TestArray_DynMultiDimensional;
- begin
- StartProgram(false);
- Add('type');
- Add(' TArrayInt = array of longint;');
- Add(' TArrayArrayInt = array of TArrayInt;');
- Add('var');
- Add(' Arr: TArrayInt;');
- Add(' Arr2: TArrayArrayInt;');
- Add(' i: longint;');
- Add('begin');
- Add(' arr2:=nil;');
- Add(' if arr2=nil then;');
- Add(' if nil=arr2 then;');
- Add(' i:=low(arr2);');
- Add(' i:=low(arr2[1]);');
- Add(' i:=high(arr2);');
- Add(' i:=high(arr2[2]);');
- Add(' arr2[3]:=arr;');
- Add(' arr2[4][5]:=i;');
- Add(' i:=arr2[6][7];');
- Add(' arr2[8,9]:=i;');
- Add(' i:=arr2[10,11];');
- Add(' SetLength(arr2,14);');
- Add(' SetLength(arr2[15],16);');
- ConvertProgram;
- CheckSource('TestArray_Dynamic',
- LinesToStr([ // statements
- 'this.Arr = [];',
- 'this.Arr2 = [];',
- 'this.i = 0;'
- ]),
- LinesToStr([ // $mod.$main
- '$mod.Arr2 = [];',
- 'if (rtl.length($mod.Arr2) == 0) ;',
- 'if (rtl.length($mod.Arr2) == 0) ;',
- '$mod.i = 0;',
- '$mod.i = 0;',
- '$mod.i = rtl.length($mod.Arr2) - 1;',
- '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
- '$mod.Arr2[3] = $mod.Arr;',
- '$mod.Arr2[4][5] = $mod.i;',
- '$mod.i = $mod.Arr2[6][7];',
- '$mod.Arr2[8][9] = $mod.i;',
- '$mod.i = $mod.Arr2[10][11];',
- '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, 14, []);',
- '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 16, 0);',
- '']));
- end;
- procedure TTestModule.TestArrayOfRecord;
- begin
- StartProgram(false);
- Add('type');
- Add(' TRec = record');
- Add(' Int: longint;');
- Add(' end;');
- Add(' TArrayRec = array of TRec;');
- Add('var');
- Add(' Arr: TArrayRec;');
- Add(' r: TRec;');
- Add(' i: longint;');
- Add('begin');
- Add(' SetLength(arr,3);');
- Add(' arr[0].int:=4;');
- Add(' arr[1].int:=length(arr)+arr[2].int;');
- Add(' arr[arr[i].int].int:=arr[5].int;');
- Add(' arr[7]:=r;');
- Add(' r:=arr[8];');
- Add(' i:=low(arr);');
- Add(' i:=high(arr);');
- ConvertProgram;
- CheckSource('TestArrayOfRecord',
- LinesToStr([ // statements
- 'this.TRec = function (s) {',
- ' if (s) {',
- ' this.Int = s.Int;',
- ' } else {',
- ' this.Int = 0;',
- ' };',
- ' this.$equal = function (b) {',
- ' return this.Int == b.Int;',
- ' };',
- '};',
- 'this.Arr = [];',
- 'this.r = new $mod.TRec();',
- 'this.i = 0;'
- ]),
- LinesToStr([ // $mod.$main
- '$mod.Arr = rtl.arraySetLength($mod.Arr,3, $mod.TRec);',
- '$mod.Arr[0].Int = 4;',
- '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
- '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
- '$mod.Arr[7] = new $mod.TRec($mod.r);',
- '$mod.r = new $mod.TRec($mod.Arr[8]);',
- '$mod.i = 0;',
- '$mod.i = rtl.length($mod.Arr)-1;',
- '']));
- end;
- procedure TTestModule.TestArray_AsParams;
- begin
- StartProgram(false);
- Add('type integer = longint;');
- Add('type TArrInt = array of integer;');
- Add('procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);');
- Add('var vJ: TArrInt;');
- Add('begin');
- Add(' vg:=vg;');
- Add(' vj:=vh;');
- Add(' vi:=vi;');
- Add(' doit(vg,vg,vg);');
- Add(' doit(vh,vh,vj);');
- Add(' doit(vi,vi,vi);');
- Add(' doit(vj,vj,vj);');
- Add('end;');
- Add('var i: TArrInt;');
- Add('begin');
- Add(' doit(i,i,i);');
- ConvertProgram;
- CheckSource('TestArray_AsParams',
- LinesToStr([ // statements
- 'this.DoIt = function (vG,vH,vI) {',
- ' var vJ = [];',
- ' vG = vG;',
- ' vJ = vH;',
- ' vI.set(vI.get());',
- ' $mod.DoIt(vG, vG, {',
- ' get: function () {',
- ' return vG;',
- ' },',
- ' set: function (v) {',
- ' vG = v;',
- ' }',
- ' });',
- ' $mod.DoIt(vH, vH, {',
- ' get: function () {',
- ' return vJ;',
- ' },',
- ' set: function (v) {',
- ' vJ = v;',
- ' }',
- ' });',
- ' $mod.DoIt(vI.get(), vI.get(), vI);',
- ' $mod.DoIt(vJ, vJ, {',
- ' get: function () {',
- ' return vJ;',
- ' },',
- ' set: function (v) {',
- ' vJ = v;',
- ' }',
- ' });',
- '};',
- 'this.i = [];'
- ]),
- LinesToStr([
- '$mod.DoIt($mod.i,$mod.i,{',
- ' p: $mod,',
- ' get: function () {',
- ' return this.p.i;',
- ' },',
- ' set: function (v) {',
- ' this.p.i = v;',
- ' }',
- '});'
- ]));
- end;
- procedure TTestModule.TestArrayElement_AsParams;
- begin
- StartProgram(false);
- Add('type integer = longint;');
- Add('type TArrayInt = array of integer;');
- Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
- Add('var vJ: tarrayint;');
- Add('begin');
- Add(' vi:=vi;');
- Add(' doit(vi,vi,vi);');
- Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
- Add('end;');
- Add('var a: TArrayInt;');
- Add('begin');
- Add(' doit(a[1+4],a[1+5],a[1+6]);');
- ConvertProgram;
- CheckSource('TestArrayElement_AsParams',
- LinesToStr([ // statements
- 'this.DoIt = function (vG,vH,vI) {',
- ' var vJ = [];',
- ' vI.set(vI.get());',
- ' $mod.DoIt(vI.get(), vI.get(), vI);',
- ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
- ' a:1+3,',
- ' p:vJ,',
- ' get: function () {',
- ' return this.p[this.a];',
- ' },',
- ' set: function (v) {',
- ' this.p[this.a] = v;',
- ' }',
- ' });',
- '};',
- 'this.a = [];'
- ]),
- LinesToStr([
- '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
- ' a: 1+6,',
- ' p: $mod.a,',
- ' get: function () {',
- ' return this.p[this.a];',
- ' },',
- ' set: function (v) {',
- ' this.p[this.a] = v;',
- ' }',
- '});'
- ]));
- end;
- procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
- begin
- StartProgram(false);
- Add('type Integer = longint;');
- Add('type TArrayInt = array of integer;');
- Add('function GetArr(vB: integer = 0): tarrayint;');
- Add('begin');
- Add('end;');
- Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
- Add('begin');
- Add('end;');
- Add('begin');
- Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
- Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
- Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
- ConvertProgram;
- CheckSource('TestArrayElementFromFuncResult_AsParams',
- LinesToStr([ // statements
- 'this.GetArr = function (vB) {',
- ' var Result = [];',
- ' return Result;',
- '};',
- 'this.DoIt = function (vG,vH,vI) {',
- '};'
- ]),
- LinesToStr([
- '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
- ' a: 1+3,',
- ' p: $mod.GetArr(0),',
- ' get: function () {',
- ' return this.p[this.a];',
- ' },',
- ' set: function (v) {',
- ' this.p[this.a] = v;',
- ' }',
- '});',
- '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
- ' a: 2+3,',
- ' p: $mod.GetArr(0),',
- ' get: function () {',
- ' return this.p[this.a];',
- ' },',
- ' set: function (v) {',
- ' this.p[this.a] = v;',
- ' }',
- '});',
- '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
- ' a: 3+3,',
- ' p: $mod.GetArr(9),',
- ' get: function () {',
- ' return this.p[this.a];',
- ' },',
- ' set: function (v) {',
- ' this.p[this.a] = v;',
- ' }',
- '});',
- '']));
- end;
- procedure TTestModule.TestArrayEnumTypeRange;
- begin
- StartProgram(false);
- Add('type');
- Add(' TEnum = (red,blue);');
- Add(' TEnumArray = array[TEnum] of longint;');
- Add('var');
- Add(' e: TEnum;');
- Add(' i: longint;');
- Add(' a: TEnumArray;');
- Add(' numbers: TEnumArray = (1,2);');
- Add(' names: array[TEnum] of string = (''red'',''blue'');');
- Add('begin');
- Add(' e:=low(a);');
- Add(' e:=high(a);');
- Add(' i:=a[red]+length(a);');
- Add(' a[e]:=a[e];');
- ConvertProgram;
- CheckSource('TestArrayEnumTypeRange',
- LinesToStr([ // statements
- ' this.TEnum = {',
- ' "0": "red",',
- ' red: 0,',
- ' "1": "blue",',
- ' blue: 1',
- '};',
- 'this.e = 0;',
- 'this.i = 0;',
- 'this.a = rtl.arrayNewMultiDim([2],0);',
- 'this.numbers = [1, 2];',
- 'this.names = ["red", "blue"];',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.e = $mod.TEnum.red;',
- '$mod.e = $mod.TEnum.blue;',
- '$mod.i = $mod.a[$mod.TEnum.red]+2;',
- '$mod.a[$mod.e] = $mod.a[$mod.e];',
- '']));
- end;
- procedure TTestModule.TestArray_SetLengthOutArg;
- begin
- StartProgram(false);
- Add([
- 'type TArrInt = array of longint;',
- 'procedure DoIt(out a: TArrInt);',
- 'begin',
- ' SetLength(a,2);',
- 'end;',
- 'begin',
- '']);
- ConvertProgram;
- CheckSource('TestArray_SetLengthOutArg',
- LinesToStr([ // statements
- 'this.DoIt = function (a) {',
- ' a.set(rtl.arraySetLength(a.get(), 2, 0));',
- '};',
- '']),
- LinesToStr([
- '']));
- end;
- procedure TTestModule.TestArray_SetLengthProperty;
- begin
- StartProgram(false);
- Add('type');
- Add(' TArrInt = array of longint;');
- Add(' TObject = class');
- Add(' function GetColors: TArrInt; external name ''GetColors'';');
- Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
- Add(' property Colors: TArrInt read GetColors write SetColors;');
- Add(' end;');
- Add('var Obj: TObject;');
- Add('begin');
- Add(' SetLength(Obj.Colors,2);');
- ConvertProgram;
- CheckSource('TestArray_SetLengthProperty',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'this.Obj = null;',
- '']),
- LinesToStr([
- '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 2, 0));',
- '']));
- end;
- procedure TTestModule.TestArray_OpenArrayOfString;
- begin
- StartProgram(false);
- Add('procedure DoIt(const a: array of String);');
- Add('var');
- Add(' i: longint;');
- Add(' s: string;');
- Add('begin');
- Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
- Add('end;');
- Add('var s: string;');
- Add('begin');
- Add(' DoIt([]);');
- Add(' DoIt([s,''foo'','''',s+s]);');
- ConvertProgram;
- CheckSource('TestArray_OpenArrayOfString',
- LinesToStr([ // statements
- 'this.DoIt = function (a) {',
- ' var i = 0;',
- ' var s = "";',
- ' var $loopend1 = rtl.length(a) - 1;',
- ' for (i = 0; i <= $loopend1; i++) s = a[(rtl.length(a) - i) - 1];',
- '};',
- 'this.s = "";',
- '']),
- LinesToStr([
- '$mod.DoIt([]);',
- '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
- '']));
- end;
- procedure TTestModule.TestArray_Concat;
- begin
- StartProgram(false);
- Add('type');
- Add(' integer = longint;');
- Add(' TFlag = (big,small);');
- Add(' TFlags = set of TFlag;');
- Add(' TRec = record');
- Add(' i: integer;');
- Add(' end;');
- Add(' TArrInt = array of integer;');
- Add(' TArrRec = array of TRec;');
- Add(' TArrSet = array of TFlags;');
- Add(' TArrJSValue = array of jsvalue;');
- Add('var');
- Add(' ArrInt: tarrint;');
- Add(' ArrRec: tarrrec;');
- Add(' ArrSet: tarrset;');
- Add(' ArrJSValue: tarrjsvalue;');
- Add('begin');
- Add(' arrint:=concat(arrint);');
- Add(' arrint:=concat(arrint,arrint);');
- Add(' arrint:=concat(arrint,arrint,arrint);');
- Add(' arrrec:=concat(arrrec);');
- Add(' arrrec:=concat(arrrec,arrrec);');
- Add(' arrrec:=concat(arrrec,arrrec,arrrec);');
- Add(' arrset:=concat(arrset);');
- Add(' arrset:=concat(arrset,arrset);');
- Add(' arrset:=concat(arrset,arrset,arrset);');
- Add(' arrjsvalue:=concat(arrjsvalue);');
- Add(' arrjsvalue:=concat(arrjsvalue,arrjsvalue);');
- Add(' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);');
- ConvertProgram;
- CheckSource('TestArray_Concat',
- LinesToStr([ // statements
- 'this.TFlag = {',
- ' "0": "big",',
- ' big: 0,',
- ' "1": "small",',
- ' small: 1',
- '};',
- 'this.TRec = function (s) {',
- ' if (s) {',
- ' this.i = s.i;',
- ' } else {',
- ' this.i = 0;',
- ' };',
- ' this.$equal = function (b) {',
- ' return this.i == b.i;',
- ' };',
- '};',
- 'this.ArrInt = [];',
- 'this.ArrRec = [];',
- 'this.ArrSet = [];',
- 'this.ArrJSValue = [];',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.ArrInt = $mod.ArrInt;',
- '$mod.ArrInt = $mod.ArrInt.concat($mod.ArrInt);',
- '$mod.ArrInt = $mod.ArrInt.concat($mod.ArrInt,$mod.ArrInt);',
- '$mod.ArrRec = $mod.ArrRec;',
- '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec);',
- '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
- '$mod.ArrSet = $mod.ArrSet;',
- '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet);',
- '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
- '$mod.ArrJSValue = $mod.ArrJSValue;',
- '$mod.ArrJSValue = $mod.ArrJSValue.concat($mod.ArrJSValue);',
- '$mod.ArrJSValue = $mod.ArrJSValue.concat($mod.ArrJSValue, $mod.ArrJSValue);',
- '']));
- end;
- procedure TTestModule.TestArray_Copy;
- begin
- StartProgram(false);
- Add('type');
- Add(' integer = longint;');
- Add(' TFlag = (big,small);');
- Add(' TFlags = set of TFlag;');
- Add(' TRec = record');
- Add(' i: integer;');
- Add(' end;');
- Add(' TArrInt = array of integer;');
- Add(' TArrRec = array of TRec;');
- Add(' TArrSet = array of TFlags;');
- Add(' TArrJSValue = array of jsvalue;');
- Add('var');
- Add(' ArrInt: tarrint;');
- Add(' ArrRec: tarrrec;');
- Add(' ArrSet: tarrset;');
- Add(' ArrJSValue: tarrjsvalue;');
- Add('begin');
- Add(' arrint:=copy(arrint);');
- Add(' arrint:=copy(arrint,2);');
- Add(' arrint:=copy(arrint,3,4);');
- Add(' arrrec:=copy(arrrec);');
- Add(' arrrec:=copy(arrrec,5);');
- Add(' arrrec:=copy(arrrec,6,7);');
- Add(' arrset:=copy(arrset);');
- Add(' arrset:=copy(arrset,8);');
- Add(' arrset:=copy(arrset,9,10);');
- Add(' arrjsvalue:=copy(arrjsvalue);');
- Add(' arrjsvalue:=copy(arrjsvalue,11);');
- Add(' arrjsvalue:=copy(arrjsvalue,12,13);');
- ConvertProgram;
- CheckSource('TestArray_Copy',
- LinesToStr([ // statements
- 'this.TFlag = {',
- ' "0": "big",',
- ' big: 0,',
- ' "1": "small",',
- ' small: 1',
- '};',
- 'this.TRec = function (s) {',
- ' if (s) {',
- ' this.i = s.i;',
- ' } else {',
- ' this.i = 0;',
- ' };',
- ' this.$equal = function (b) {',
- ' return this.i == b.i;',
- ' };',
- '};',
- 'this.ArrInt = [];',
- 'this.ArrRec = [];',
- 'this.ArrSet = [];',
- 'this.ArrJSValue = [];',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
- '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
- '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
- '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
- '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
- '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
- '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
- '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
- '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
- '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
- '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
- '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
- '']));
- end;
- procedure TTestModule.TestArray_InsertDelete;
- begin
- StartProgram(false);
- Add('type');
- Add(' integer = longint;');
- Add(' TFlag = (big,small);');
- Add(' TFlags = set of TFlag;');
- Add(' TRec = record');
- Add(' i: integer;');
- Add(' end;');
- Add(' TArrInt = array of integer;');
- Add(' TArrRec = array of TRec;');
- Add(' TArrSet = array of TFlags;');
- Add(' TArrJSValue = array of jsvalue;');
- Add('var');
- Add(' ArrInt: tarrint;');
- Add(' ArrRec: tarrrec;');
- Add(' ArrSet: tarrset;');
- Add(' ArrJSValue: tarrjsvalue;');
- Add('begin');
- Add(' Insert(1,arrint,2);');
- Add(' Insert(arrint[3],arrint,4);');
- Add(' Insert(arrrec[5],arrrec,6);');
- Add(' Insert(arrset[7],arrset,7);');
- Add(' Insert(arrjsvalue[8],arrjsvalue,9);');
- Add(' Insert(10,arrjsvalue,11);');
- Add(' Delete(arrint,12,13);');
- Add(' Delete(arrrec,14,15);');
- Add(' Delete(arrset,17,18);');
- Add(' Delete(arrjsvalue,19,10);');
- ConvertProgram;
- CheckSource('TestArray_InsertDelete',
- LinesToStr([ // statements
- 'this.TFlag = {',
- ' "0": "big",',
- ' big: 0,',
- ' "1": "small",',
- ' small: 1',
- '};',
- 'this.TRec = function (s) {',
- ' if (s) {',
- ' this.i = s.i;',
- ' } else {',
- ' this.i = 0;',
- ' };',
- ' this.$equal = function (b) {',
- ' return this.i == b.i;',
- ' };',
- '};',
- 'this.ArrInt = [];',
- 'this.ArrRec = [];',
- 'this.ArrSet = [];',
- 'this.ArrJSValue = [];',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.ArrInt.splice(2, 1, 1);',
- '$mod.ArrInt.splice(4, 1, $mod.ArrInt[3]);',
- '$mod.ArrRec.splice(6, 1, $mod.ArrRec[5]);',
- '$mod.ArrSet.splice(7, 1, $mod.ArrSet[7]);',
- '$mod.ArrJSValue.splice(9, 1, $mod.ArrJSValue[8]);',
- '$mod.ArrJSValue.splice(11, 1, 10);',
- '$mod.ArrInt.splice(12, 13);',
- '$mod.ArrRec.splice(14, 15);',
- '$mod.ArrSet.splice(17, 18);',
- '$mod.ArrJSValue.splice(19, 10);',
- '']));
- end;
- procedure TTestModule.TestArray_DynArrayConst;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' integer = longint;',
- ' TArrInt = array of integer;',
- ' TArrStr = array of string;',
- 'const',
- ' Ints: TArrInt = (1,2,3);',
- ' Names: array of string = (''a'',''foo'');',
- ' Aliases: TarrStr = (''foo'',''b'');',
- ' OneInt: TArrInt = (7);',
- ' OneStr: array of integer = (7);',
- //' Chars: array of char = ''aoc'';',
- 'begin',
- '']);
- ConvertProgram;
- CheckSource('TestArray_DynArrayConst',
- LinesToStr([ // statements
- 'this.Ints = [1, 2, 3];',
- 'this.Names = ["a", "foo"];',
- 'this.Aliases = ["foo", "b"];',
- 'this.OneInt = [7];',
- 'this.OneStr = [7];',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestModule.TestExternalClass_TypeCastArrayToExternalArray;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TJSArray = class external name ''Array''');
- Add(' class function isArray(Value: JSValue) : boolean;');
- Add(' function concat() : TJSArray; varargs;');
- Add(' end;');
- Add('var');
- Add(' aObj: TJSArray;');
- Add(' a: array of longint;');
- Add('begin');
- Add(' if TJSArray.isArray(65) then ;');
- Add(' aObj:=TJSArray(a).concat(a);');
- ConvertProgram;
- CheckSource('TestExternalClass_TypeCastArrayToExternalArray',
- LinesToStr([ // statements
- 'this.aObj = null;',
- 'this.a = [];',
- '']),
- LinesToStr([ // $mod.$main
- 'if (Array.isArray(65)) ;',
- '$mod.aObj = $mod.a.concat($mod.a);',
- '']));
- end;
- procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalArray;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TArrStr = array of string;');
- Add(' TJSArray = class external name ''Array''');
- Add(' end;');
- Add('var');
- Add(' aObj: TJSArray;');
- Add(' a: TArrStr;');
- Add('begin');
- Add(' a:=TArrStr(aObj);');
- Add(' TArrStr(aObj)[1]:=TArrStr(aObj)[2];');
- ConvertProgram;
- CheckSource('TestExternalClass_TypeCastArrayFromExternalArray',
- LinesToStr([ // statements
- 'this.aObj = null;',
- 'this.a = [];',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.a = $mod.aObj;',
- '$mod.aObj[1] = $mod.aObj[2];',
- '']));
- end;
- procedure TTestModule.TestRecord_Var;
- begin
- StartProgram(false);
- Add('type');
- Add(' TRecA = record');
- Add(' Bold: longint;');
- Add(' end;');
- Add('var Rec: TRecA;');
- Add('begin');
- Add(' rec.bold:=123');
- ConvertProgram;
- CheckSource('TestRecord_Var',
- LinesToStr([ // statements
- 'this.TRecA = function (s) {',
- ' if (s) {',
- ' this.Bold = s.Bold;',
- ' } else {',
- ' this.Bold = 0;',
- ' };',
- ' this.$equal = function (b) {',
- ' return this.Bold == b.Bold;',
- ' };',
- '};',
- 'this.Rec = new $mod.TRecA();'
- ]),
- LinesToStr([ // $mod.$main
- '$mod.Rec.Bold = 123;'
- ]));
- end;
- procedure TTestModule.TestWithRecordDo;
- begin
- StartProgram(false);
- Add('type');
- Add(' TRec = record');
- Add(' vI: longint;');
- Add(' end;');
- Add('var');
- Add(' Int: longint;');
- Add(' r: TRec;');
- Add('begin');
- Add(' with r do');
- Add(' int:=vi;');
- Add(' with r do begin');
- Add(' int:=vi;');
- Add(' vi:=int;');
- Add(' end;');
- ConvertProgram;
- CheckSource('TestWithRecordDo',
- LinesToStr([ // statements
- 'this.TRec = function (s) {',
- ' if (s) {',
- ' this.vI = s.vI;',
- ' } else {',
- ' this.vI = 0;',
- ' };',
- ' this.$equal = function (b) {',
- ' return this.vI == b.vI;',
- ' };',
- '};',
- 'this.Int = 0;',
- 'this.r = new $mod.TRec();'
- ]),
- LinesToStr([ // $mod.$main
- 'var $with1 = $mod.r;',
- '$mod.Int = $with1.vI;',
- 'var $with2 = $mod.r;',
- '$mod.Int = $with2.vI;',
- '$with2.vI = $mod.Int;'
- ]));
- end;
- procedure TTestModule.TestRecord_Assign;
- begin
- StartProgram(false);
- Add('type');
- Add(' TEnum = (red,green);');
- Add(' TEnums = set of TEnum;');
- Add(' TSmallRec = record');
- Add(' N: longint;');
- Add(' end;');
- Add(' TBigRec = record');
- Add(' Int: longint;');
- Add(' D: double;');
- Add(' Arr: array of longint;');
- Add(' Small: TSmallRec;');
- Add(' Enums: TEnums;');
- Add(' end;');
- Add('var');
- Add(' r, s: TBigRec;');
- Add('begin');
- Add(' r:=s;');
- ConvertProgram;
- CheckSource('TestRecord_Assign',
- LinesToStr([ // statements
- 'this.TEnum = {',
- ' "0": "red",',
- ' red: 0,',
- ' "1": "green",',
- ' green: 1',
- '};',
- 'this.TSmallRec = function (s) {',
- ' if(s){',
- ' this.N = s.N;',
- ' } else {',
- ' this.N = 0;',
- ' };',
- ' this.$equal = function (b) {',
- ' return this.N == b.N;',
- ' };',
- '};',
- 'this.TBigRec = function (s) {',
- ' if(s){',
- ' this.Int = s.Int;',
- ' this.D = s.D;',
- ' this.Arr = s.Arr;',
- ' this.Small = new $mod.TSmallRec(s.Small);',
- ' this.Enums = rtl.refSet(s.Enums);',
- ' } else {',
- ' this.Int = 0;',
- ' this.D = 0.0;',
- ' this.Arr = [];',
- ' this.Small = new $mod.TSmallRec();',
- ' this.Enums = {};',
- ' };',
- ' this.$equal = function (b) {',
- ' return (this.Int == b.Int) && ((this.D == b.D) && ((this.Arr == b.Arr)',
- ' && (this.Small.$equal(b.Small) && rtl.eqSet(this.Enums, b.Enums))));',
- ' };',
- '};',
- 'this.r = new $mod.TBigRec();',
- 'this.s = new $mod.TBigRec();'
- ]),
- LinesToStr([ // $mod.$main
- '$mod.r = new $mod.TBigRec($mod.s);',
- '']));
- end;
- procedure TTestModule.TestRecord_PassAsArgClone;
- begin
- StartProgram(false);
- Add('type');
- Add(' TRecA = record');
- Add(' Bold: longint;');
- Add(' end;');
- Add('procedure DoDefault(r: treca); begin end;');
- Add('procedure DoConst(const r: treca); begin end;');
- Add('var Rec: treca;');
- Add('begin');
- Add(' dodefault(rec);');
- Add(' doconst(rec);');
- ConvertProgram;
- CheckSource('TestRecord_PassAsArgClone',
- LinesToStr([ // statements
- 'this.TRecA = function (s) {',
- ' if (s) {',
- ' this.Bold = s.Bold;',
- ' } else {',
- ' this.Bold = 0;',
- ' };',
- ' this.$equal = function (b) {',
- ' return this.Bold == b.Bold;',
- ' };',
- '};',
- 'this.DoDefault = function (r) {',
- '};',
- 'this.DoConst = function (r) {',
- '};',
- 'this.Rec = new $mod.TRecA();'
- ]),
- LinesToStr([ // $mod.$main
- '$mod.DoDefault(new $mod.TRecA($mod.Rec));',
- '$mod.DoConst($mod.Rec);',
- '']));
- end;
- procedure TTestModule.TestRecord_AsParams;
- begin
- StartProgram(false);
- Add('type');
- Add(' integer = longint;');
- Add(' TRecord = record');
- Add(' i: integer;');
- Add(' end;');
- Add('procedure DoIt(vG: TRecord; const vH: TRecord; var vI: TRecord);');
- Add('var vJ: TRecord;');
- Add('begin');
- Add(' vg:=vg;');
- Add(' vj:=vh;');
- Add(' vi:=vi;');
- Add(' doit(vg,vg,vg);');
- Add(' doit(vh,vh,vj);');
- Add(' doit(vi,vi,vi);');
- Add(' doit(vj,vj,vj);');
- Add('end;');
- Add('var i: TRecord;');
- Add('begin');
- Add(' doit(i,i,i);');
- ConvertProgram;
- CheckSource('TestRecord_AsParams',
- LinesToStr([ // statements
- 'this.TRecord = function (s) {',
- ' if (s) {',
- ' this.i = s.i;',
- ' } else {',
- ' this.i = 0;',
- ' };',
- ' this.$equal = function (b) {',
- ' return this.i == b.i;',
- ' };',
- '};',
- 'this.DoIt = function (vG,vH,vI) {',
- ' var vJ = new $mod.TRecord();',
- ' vG = new $mod.TRecord(vG);',
- ' vJ = new $mod.TRecord(vH);',
- ' vI.set(new $mod.TRecord(vI.get()));',
- ' $mod.DoIt(new $mod.TRecord(vG), vG, {',
- ' get: function () {',
- ' return vG;',
- ' },',
- ' set: function (v) {',
- ' vG = v;',
- ' }',
- ' });',
- ' $mod.DoIt(new $mod.TRecord(vH), vH, {',
- ' get: function () {',
- ' return vJ;',
- ' },',
- ' set: function (v) {',
- ' vJ = v;',
- ' }',
- ' });',
- ' $mod.DoIt(new $mod.TRecord(vI.get()), vI.get(), vI);',
- ' $mod.DoIt(new $mod.TRecord(vJ), vJ, {',
- ' get: function () {',
- ' return vJ;',
- ' },',
- ' set: function (v) {',
- ' vJ = v;',
- ' }',
- ' });',
- '};',
- 'this.i = new $mod.TRecord();'
- ]),
- LinesToStr([
- '$mod.DoIt(new $mod.TRecord($mod.i),$mod.i,{',
- ' p: $mod,',
- ' get: function () {',
- ' return this.p.i;',
- ' },',
- ' set: function (v) {',
- ' this.p.i = v;',
- ' }',
- '});'
- ]));
- end;
- procedure TTestModule.TestRecordElement_AsParams;
- begin
- StartProgram(false);
- Add('type');
- Add(' integer = longint;');
- Add(' TRecord = record');
- Add(' i: integer;');
- Add(' end;');
- Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
- Add('var vJ: TRecord;');
- Add('begin');
- Add(' doit(vj.i,vj.i,vj.i);');
- Add('end;');
- Add('var r: TRecord;');
- Add('begin');
- Add(' doit(r.i,r.i,r.i);');
- ConvertProgram;
- CheckSource('TestRecordElement_AsParams',
- LinesToStr([ // statements
- 'this.TRecord = function (s) {',
- ' if (s) {',
- ' this.i = s.i;',
- ' } else {',
- ' this.i = 0;',
- ' };',
- ' this.$equal = function (b) {',
- ' return this.i == b.i;',
- ' };',
- '};',
- 'this.DoIt = function (vG,vH,vI) {',
- ' var vJ = new $mod.TRecord();',
- ' $mod.DoIt(vJ.i, vJ.i, {',
- ' p: vJ,',
- ' get: function () {',
- ' return this.p.i;',
- ' },',
- ' set: function (v) {',
- ' this.p.i = v;',
- ' }',
- ' });',
- '};',
- 'this.r = new $mod.TRecord();'
- ]),
- LinesToStr([
- '$mod.DoIt($mod.r.i,$mod.r.i,{',
- ' p: $mod.r,',
- ' get: function () {',
- ' return this.p.i;',
- ' },',
- ' set: function (v) {',
- ' this.p.i = v;',
- ' }',
- '});'
- ]));
- end;
- procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
- begin
- StartProgram(false);
- Add('type');
- Add(' integer = longint;');
- Add(' TRecord = record');
- Add(' i: integer;');
- Add(' end;');
- Add('function GetRec(vB: integer = 0): TRecord;');
- Add('begin');
- Add('end;');
- Add('procedure DoIt(vG: integer; const vH: integer);');
- Add('begin');
- Add('end;');
- Add('begin');
- Add(' doit(getrec.i,getrec.i);');
- Add(' doit(getrec().i,getrec().i);');
- Add(' doit(getrec(1).i,getrec(2).i);');
- ConvertProgram;
- CheckSource('TestRecordElementFromFuncResult_AsParams',
- LinesToStr([ // statements
- 'this.TRecord = function (s) {',
- ' if (s) {',
- ' this.i = s.i;',
- ' } else {',
- ' this.i = 0;',
- ' };',
- ' this.$equal = function (b) {',
- ' return this.i == b.i;',
- ' };',
- '};',
- 'this.GetRec = function (vB) {',
- ' var Result = new $mod.TRecord();',
- ' return Result;',
- '};',
- 'this.DoIt = function (vG,vH) {',
- '};'
- ]),
- LinesToStr([
- '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
- '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
- '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
- '']));
- end;
- procedure TTestModule.TestRecordElementFromWith_AsParams;
- begin
- StartProgram(false);
- Add('type');
- Add(' integer = longint;');
- Add(' TRecord = record');
- Add(' i: integer;');
- Add(' end;');
- Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
- Add('begin');
- Add('end;');
- Add('var r: trecord;');
- Add('begin');
- Add(' with r do ');
- Add(' doit(i,i,i);');
- ConvertProgram;
- CheckSource('TestRecordElementFromWith_AsParams',
- LinesToStr([ // statements
- 'this.TRecord = function (s) {',
- ' if (s) {',
- ' this.i = s.i;',
- ' } else {',
- ' this.i = 0;',
- ' };',
- ' this.$equal = function (b) {',
- ' return this.i == b.i;',
- ' };',
- '};',
- 'this.DoIt = function (vG,vH,vI) {',
- '};',
- 'this.r = new $mod.TRecord();'
- ]),
- LinesToStr([
- 'var $with1 = $mod.r;',
- '$mod.DoIt($with1.i,$with1.i,{',
- ' p: $with1,',
- ' get: function () {',
- ' return this.p.i;',
- ' },',
- ' set: function (v) {',
- ' this.p.i = v;',
- ' }',
- '});',
- '']));
- end;
- procedure TTestModule.TestRecord_Equal;
- begin
- StartProgram(false);
- Add('type');
- Add(' integer = longint;');
- Add(' TFlag = (red,blue);');
- Add(' TFlags = set of TFlag;');
- Add(' TProc = procedure;');
- Add(' TRecord = record');
- Add(' i: integer;');
- Add(' Event: TProc;');
- Add(' f: TFlags;');
- Add(' end;');
- Add(' TNested = record');
- Add(' r: TRecord;');
- Add(' end;');
- Add('var');
- Add(' b: boolean;');
- Add(' r,s: trecord;');
- Add('begin');
- Add(' b:=r=s;');
- Add(' b:=r<>s;');
- ConvertProgram;
- CheckSource('TestRecord_Equal',
- LinesToStr([ // statements
- 'this.TFlag = {',
- ' "0": "red",',
- ' red: 0,',
- ' "1": "blue",',
- ' blue: 1',
- '};',
- 'this.TRecord = function (s) {',
- ' if (s) {',
- ' this.i = s.i;',
- ' this.Event = s.Event;',
- ' this.f = rtl.refSet(s.f);',
- ' } else {',
- ' this.i = 0;',
- ' this.Event = null;',
- ' this.f = {};',
- ' };',
- ' this.$equal = function (b) {',
- ' return (this.i == b.i) && (rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f));',
- ' };',
- '};',
- 'this.TNested = function (s) {',
- ' if (s) {',
- ' this.r = new $mod.TRecord(s.r);',
- ' } else {',
- ' this.r = new $mod.TRecord();',
- ' };',
- ' this.$equal = function (b) {',
- ' return this.r.$equal(b.r);',
- ' };',
- '};',
- 'this.b = false;',
- 'this.r = new $mod.TRecord();',
- 'this.s = new $mod.TRecord();'
- ]),
- LinesToStr([
- '$mod.b = $mod.r.$equal($mod.s);',
- '$mod.b = !$mod.r.$equal($mod.s);',
- '']));
- end;
- procedure TTestModule.TestRecord_TypeCastJSValueToRecord;
- begin
- StartProgram(false);
- Add('type');
- Add(' TRecord = record');
- Add(' i: longint;');
- Add(' end;');
- Add('var');
- Add(' Jv: jsvalue;');
- Add(' Rec: trecord;');
- Add('begin');
- Add(' rec:=trecord(jv);');
- ConvertProgram;
- CheckSource('TestRecord_TypeCastJSValueToRecord',
- LinesToStr([ // statements
- 'this.TRecord = function (s) {',
- ' if (s) {',
- ' this.i = s.i;',
- ' } else {',
- ' this.i = 0;',
- ' };',
- ' this.$equal = function (b) {',
- ' return this.i == b.i;',
- ' };',
- '};',
- 'this.Jv = undefined;',
- 'this.Rec = new $mod.TRecord();'
- ]),
- LinesToStr([
- '$mod.Rec = new $mod.TRecord(rtl.getObject($mod.Jv));',
- '']));
- end;
- procedure TTestModule.TestClass_TObjectDefaultConstructor;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' public');
- Add(' constructor Create;');
- Add(' destructor Destroy;');
- Add(' end;');
- Add('constructor tobject.create;');
- Add('begin end;');
- Add('destructor tobject.destroy;');
- Add('begin end;');
- Add('var Obj: tobject;');
- Add('begin');
- Add(' obj:=tobject.create;');
- Add(' obj.destroy;');
- ConvertProgram;
- CheckSource('TestClass_TObjectDefaultConstructor',
- LinesToStr([ // statements
- 'rtl.createClass($mod,"TObject",null,function(){',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.Create = function(){',
- ' };',
- ' this.Destroy = function(){',
- ' };',
- '});',
- 'this.Obj = null;'
- ]),
- LinesToStr([ // $mod.$main
- '$mod.Obj = $mod.TObject.$create("Create");',
- '$mod.Obj.$destroy("Destroy");',
- '']));
- end;
- procedure TTestModule.TestClass_TObjectConstructorWithParams;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' public');
- Add(' constructor Create(Par: longint);');
- Add(' end;');
- Add('constructor tobject.create(par: longint);');
- Add('begin end;');
- Add('var Obj: tobject;');
- Add('begin');
- Add(' obj:=tobject.create(3);');
- ConvertProgram;
- CheckSource('TestClass_TObjectConstructorWithParams',
- LinesToStr([ // statements
- 'rtl.createClass($mod,"TObject",null,function(){',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.Create = function(Par){',
- ' };',
- '});',
- 'this.Obj = null;'
- ]),
- LinesToStr([ // $mod.$main
- '$mod.Obj = $mod.TObject.$create("Create",[3]);'
- ]));
- end;
- procedure TTestModule.TestClass_Var;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' public');
- Add(' vI: longint;');
- Add(' constructor Create(Par: longint);');
- Add(' end;');
- Add('constructor tobject.create(par: longint);');
- Add('begin');
- Add(' vi:=par+3');
- Add('end;');
- Add('var Obj: tobject;');
- Add('begin');
- Add(' obj:=tobject.create(4);');
- Add(' obj.vi:=obj.VI+5;');
- ConvertProgram;
- CheckSource('TestClass_Var',
- LinesToStr([ // statements
- 'rtl.createClass($mod,"TObject",null,function(){',
- ' this.$init = function () {',
- ' this.vI = 0;',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.Create = function(Par){',
- ' this.vI = Par+3;',
- ' };',
- '});',
- 'this.Obj = null;'
- ]),
- LinesToStr([ // $mod.$main
- '$mod.Obj = $mod.TObject.$create("Create",[4]);',
- '$mod.Obj.vI = $mod.Obj.vI + 5;'
- ]));
- end;
- procedure TTestModule.TestClass_Method;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' public');
- Add(' vI: longint;');
- Add(' Sub: TObject;');
- Add(' constructor Create;');
- Add(' function GetIt(Par: longint): tobject;');
- Add(' end;');
- Add('constructor tobject.create; begin end;');
- Add('function tobject.getit(par: longint): tobject;');
- Add('begin');
- Add(' Self.vi:=par+3;');
- Add(' Result:=self.sub;');
- Add('end;');
- Add('var Obj: tobject;');
- Add('begin');
- Add(' obj:=tobject.create;');
- Add(' obj.getit(4);');
- Add(' obj.sub.sub:=nil;');
- Add(' obj.sub.getit(5);');
- Add(' obj.sub.getit(6).SUB:=nil;');
- Add(' obj.sub.getit(7).GETIT(8);');
- Add(' obj.sub.getit(9).SuB.getit(10);');
- ConvertProgram;
- CheckSource('TestClass_Method',
- LinesToStr([ // statements
- 'rtl.createClass($mod,"TObject",null,function(){',
- ' this.$init = function () {',
- ' this.vI = 0;',
- ' this.Sub = null;',
- ' };',
- ' this.$final = function () {',
- ' this.Sub = undefined;',
- ' };',
- ' this.Create = function(){',
- ' };',
- ' this.GetIt = function(Par){',
- ' var Result = null;',
- ' this.vI = Par + 3;',
- ' Result = this.Sub;',
- ' return Result;',
- ' };',
- '});',
- 'this.Obj = null;'
- ]),
- LinesToStr([ // $mod.$main
- '$mod.Obj = $mod.TObject.$create("Create");',
- '$mod.Obj.GetIt(4);',
- '$mod.Obj.Sub.Sub=null;',
- '$mod.Obj.Sub.GetIt(5);',
- '$mod.Obj.Sub.GetIt(6).Sub=null;',
- '$mod.Obj.Sub.GetIt(7).GetIt(8);',
- '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
- ]));
- end;
- procedure TTestModule.TestClass_Implementation;
- begin
- StartUnit(false);
- Add([
- 'interface',
- 'type',
- ' TObject = class',
- ' constructor Create;',
- ' end;',
- 'implementation',
- 'type',
- ' TIntClass = class',
- ' constructor Create; reintroduce;',
- ' class procedure DoGlob;',
- ' end;',
- 'constructor tintclass.create;',
- 'begin',
- ' inherited;',
- ' inherited create;',
- ' doglob;',
- 'end;',
- 'class procedure tintclass.doglob;',
- 'begin',
- 'end;',
- 'constructor tobject.create;',
- 'var',
- ' iC: tintclass;',
- 'begin',
- ' ic:=tintclass.create;',
- ' tintclass.doglob;',
- ' ic.doglob;',
- 'end;',
- 'initialization',
- ' tintclass.doglob;',
- '']);
- ConvertUnit;
- CheckSource('TestClass_Implementation',
- LinesToStr([ // statements
- 'var $impl = $mod.$impl;',
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.Create = function () {',
- ' var iC = null;',
- ' iC = $impl.TIntClass.$create("Create$1");',
- ' $impl.TIntClass.DoGlob();',
- ' iC.$class.DoGlob();',
- ' };',
- '});',
- '']),
- LinesToStr([ // $mod.$main
- '$impl.TIntClass.DoGlob();',
- '']),
- LinesToStr([
- 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
- ' this.Create$1 = function () {',
- ' $mod.TObject.Create.apply(this, arguments);',
- ' $mod.TObject.Create.call(this);',
- ' this.$class.DoGlob();',
- ' };',
- ' this.DoGlob = function () {',
- ' };',
- '});',
- '']));
- end;
- procedure TTestModule.TestClass_Inheritance;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' public');
- Add(' constructor Create;');
- Add(' end;');
- Add(' TClassA = class');
- Add(' end;');
- Add(' TClassB = class(TObject)');
- Add(' procedure ProcB;');
- Add(' end;');
- Add('constructor tobject.create; begin end;');
- Add('procedure tclassb.procb; begin end;');
- Add('var');
- Add(' oO: TObject;');
- Add(' oA: TClassA;');
- Add(' oB: TClassB;');
- Add('begin');
- Add(' oO:=tobject.Create;');
- Add(' oA:=tclassa.Create;');
- Add(' ob:=tclassb.Create;');
- Add(' if oo is tclassa then ;');
- Add(' ob:=oo as tclassb;');
- Add(' (oo as tclassb).procb;');
- ConvertProgram;
- CheckSource('TestClass_Inheritance',
- LinesToStr([ // statements
- 'rtl.createClass($mod,"TObject",null,function(){',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.Create = function () {',
- ' };',
- '});',
- 'rtl.createClass($mod,"TClassA",$mod.TObject,function(){',
- '});',
- 'rtl.createClass($mod,"TClassB",$mod.TObject,function(){',
- ' this.ProcB = function () {',
- ' };',
- '});',
- 'this.oO = null;',
- 'this.oA = null;',
- 'this.oB = null;'
- ]),
- LinesToStr([ // $mod.$main
- '$mod.oO = $mod.TObject.$create("Create");',
- '$mod.oA = $mod.TClassA.$create("Create");',
- '$mod.oB = $mod.TClassB.$create("Create");',
- 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
- '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
- 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
- ]));
- end;
- procedure TTestModule.TestClass_AbstractMethod;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' public');
- Add(' procedure DoIt; virtual; abstract;');
- Add(' end;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestClass_AbstractMethod',
- LinesToStr([ // statements
- 'rtl.createClass($mod,"TObject",null,function(){',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});'
- ]),
- LinesToStr([ // this.$main
- ''
- ]));
- end;
- procedure TTestModule.TestClass_CallInherited_NoParams;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' procedure DoAbstract; virtual; abstract;');
- Add(' procedure DoVirtual; virtual;');
- Add(' procedure DoIt;');
- Add(' end;');
- Add(' TA = class');
- Add(' procedure doabstract; override;');
- Add(' procedure dovirtual; override;');
- Add(' procedure DoSome;');
- Add(' end;');
- Add('procedure tobject.dovirtual;');
- Add('begin');
- Add(' inherited; // call non existing ancestor -> ignore silently');
- Add('end;');
- Add('procedure tobject.doit;');
- Add('begin');
- Add('end;');
- Add('procedure ta.doabstract;');
- Add('begin');
- Add(' inherited dovirtual; // call TObject.DoVirtual');
- Add('end;');
- Add('procedure ta.dovirtual;');
- Add('begin');
- Add(' inherited; // call TObject.DoVirtual');
- Add(' inherited dovirtual; // call TObject.DoVirtual');
- Add(' inherited dovirtual(); // call TObject.DoVirtual');
- Add(' doit;');
- Add(' doit();');
- Add('end;');
- Add('procedure ta.dosome;');
- Add('begin');
- Add(' inherited; // call non existing ancestor method -> silently ignore');
- Add('end;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestClass_CallInherited_NoParams',
- LinesToStr([ // statements
- 'rtl.createClass($mod,"TObject",null,function(){',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.DoVirtual = function () {',
- ' };',
- ' this.DoIt = function () {',
- ' };',
- '});',
- 'rtl.createClass($mod, "TA", $mod.TObject, function () {',
- ' this.DoAbstract = function () {',
- ' $mod.TObject.DoVirtual.call(this);',
- ' };',
- ' this.DoVirtual = function () {',
- ' $mod.TObject.DoVirtual.apply(this, arguments);',
- ' $mod.TObject.DoVirtual.call(this);',
- ' $mod.TObject.DoVirtual.call(this);',
- ' this.DoIt();',
- ' this.DoIt();',
- ' };',
- ' this.DoSome = function () {',
- ' };',
- '});'
- ]),
- LinesToStr([ // this.$main
- ''
- ]));
- end;
- procedure TTestModule.TestClass_CallInherited_WithParams;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;');
- Add(' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;');
- Add(' procedure DoIt(pA: longint; pB: longint = 0);');
- Add(' procedure DoIt2(pA: longint = 1; pB: longint = 2);');
- Add(' end;');
- Add(' TClassA = class');
- Add(' procedure DoAbstract(pA: longint; pB: longint = 0); override;');
- Add(' procedure DoVirtual(pA: longint; pB: longint = 0); override;');
- Add(' end;');
- Add('procedure tobject.dovirtual(pa: longint; pb: longint = 0);');
- Add('begin');
- Add('end;');
- Add('procedure tobject.doit(pa: longint; pb: longint = 0);');
- Add('begin');
- Add('end;');
- Add('procedure tobject.doit2(pa: longint; pb: longint = 0);');
- Add('begin');
- Add('end;');
- Add('procedure tclassa.doabstract(pa: longint; pb: longint = 0);');
- Add('begin');
- Add(' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)');
- Add(' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)');
- Add('end;');
- Add('procedure tclassa.dovirtual(pa: longint; pb: longint = 0);');
- Add('begin');
- Add(' inherited; // call TObject.DoVirtual(pA,pB)');
- Add(' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)');
- Add(' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)');
- Add(' doit(pa,pb);');
- Add(' doit(pa);');
- Add(' doit2(pa);');
- Add(' doit2;');
- Add('end;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestClass_CallInherited_WithParams',
- LinesToStr([ // statements
- 'rtl.createClass($mod,"TObject",null,function(){',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.DoVirtual = function (pA,pB) {',
- ' };',
- ' this.DoIt = function (pA,pB) {',
- ' };',
- ' this.DoIt2 = function (pA,pB) {',
- ' };',
- '});',
- 'rtl.createClass($mod, "TClassA", $mod.TObject, function () {',
- ' this.DoAbstract = function (pA,pB) {',
- ' $mod.TObject.DoVirtual.call(this,pA,pB);',
- ' $mod.TObject.DoVirtual.call(this,pA,0);',
- ' };',
- ' this.DoVirtual = function (pA,pB) {',
- ' $mod.TObject.DoVirtual.apply(this, arguments);',
- ' $mod.TObject.DoVirtual.call(this,pA,pB);',
- ' $mod.TObject.DoVirtual.call(this,pA,0);',
- ' this.DoIt(pA,pB);',
- ' this.DoIt(pA,0);',
- ' this.DoIt2(pA,2);',
- ' this.DoIt2(1,2);',
- ' };',
- '});'
- ]),
- LinesToStr([ // this.$main
- ''
- ]));
- end;
- procedure TTestModule.TestClasS_CallInheritedConstructor;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' constructor Create; virtual;');
- Add(' constructor CreateWithB(b: boolean);');
- Add(' end;');
- Add(' TA = class');
- Add(' constructor Create; override;');
- Add(' constructor CreateWithC(c: char);');
- Add(' procedure DoIt;');
- Add(' class function DoSome: TObject;');
- Add(' end;');
- Add('constructor tobject.create;');
- Add('begin');
- Add(' inherited; // call non existing ancestor -> ignore silently');
- Add('end;');
- Add('constructor tobject.createwithb(b: boolean);');
- Add('begin');
- Add(' inherited; // call non existing ancestor -> ignore silently');
- Add(' create; // normal call');
- Add('end;');
- Add('constructor ta.create;');
- Add('begin');
- Add(' inherited; // normal call TObject.Create');
- Add(' inherited create; // normal call TObject.Create');
- Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
- Add('end;');
- Add('constructor ta.createwithc(c: char);');
- Add('begin');
- Add(' inherited create; // call TObject.Create');
- Add(' inherited createwithb(true); // call TObject.CreateWithB');
- Add(' doit;');
- Add(' doit();');
- Add(' dosome;');
- Add('end;');
- Add('procedure ta.doit;');
- Add('begin');
- Add(' create; // normal call');
- Add(' createwithb(false); // normal call');
- Add(' createwithc(''c''); // normal call');
- Add('end;');
- Add('class function ta.dosome: TObject;');
- Add('begin');
- Add(' Result:=create; // constructor');
- Add(' Result:=createwithb(true); // constructor');
- Add(' Result:=createwithc(''c''); // constructor');
- Add('end;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestClass_CallInheritedConstructor',
- LinesToStr([ // statements
- 'rtl.createClass($mod,"TObject",null,function(){',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.Create = function () {',
- ' };',
- ' this.CreateWithB = function (b) {',
- ' this.Create();',
- ' };',
- '});',
- 'rtl.createClass($mod, "TA", $mod.TObject, function () {',
- ' this.Create = function () {',
- ' $mod.TObject.Create.apply(this, arguments);',
- ' $mod.TObject.Create.call(this);',
- ' $mod.TObject.CreateWithB.call(this, false);',
- ' };',
- ' this.CreateWithC = function (c) {',
- ' $mod.TObject.Create.call(this);',
- ' $mod.TObject.CreateWithB.call(this, true);',
- ' this.DoIt();',
- ' this.DoIt();',
- ' this.$class.DoSome();',
- ' };',
- ' this.DoIt = function () {',
- ' this.Create();',
- ' this.CreateWithB(false);',
- ' this.CreateWithC("c");',
- ' };',
- ' this.DoSome = function () {',
- ' var Result = null;',
- ' Result = this.$create("Create");',
- ' Result = this.$create("CreateWithB", [true]);',
- ' Result = this.$create("CreateWithC", ["c"]);',
- ' return Result;',
- ' };',
- '});'
- ]),
- LinesToStr([ // this.$main
- ''
- ]));
- end;
- procedure TTestModule.TestClass_ClassVar;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' public');
- Add(' class var vI: longint;');
- Add(' class var Sub: TObject;');
- Add(' constructor Create;');
- Add(' class function GetIt(Par: longint): tobject;');
- Add(' end;');
- Add('constructor tobject.create;');
- Add('begin');
- Add(' vi:=vi+1;');
- Add(' Self.vi:=Self.vi+1;');
- Add('end;');
- Add('class function tobject.getit(par: longint): tobject;');
- Add('begin');
- Add(' vi:=vi+par;');
- Add(' Self.vi:=Self.vi+par;');
- Add(' Result:=self.sub;');
- Add('end;');
- Add('var Obj: tobject;');
- Add('begin');
- Add(' obj:=tobject.create;');
- Add(' tobject.vi:=3;');
- Add(' if tobject.vi=4 then ;');
- Add(' tobject.sub:=nil;');
- Add(' obj.sub:=nil;');
- Add(' obj.sub.sub:=nil;');
- ConvertProgram;
- CheckSource('TestClass_ClassVar',
- LinesToStr([ // statements
- 'rtl.createClass($mod,"TObject",null,function(){',
- ' this.vI = 0;',
- ' this.Sub = null;',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.Create = function(){',
- ' this.$class.vI = this.vI+1;',
- ' this.$class.vI = this.vI+1;',
- ' };',
- ' this.GetIt = function(Par){',
- ' var Result = null;',
- ' this.vI = this.vI + Par;',
- ' this.vI = this.vI + Par;',
- ' Result = this.Sub;',
- ' return Result;',
- ' };',
- '});',
- 'this.Obj = null;'
- ]),
- LinesToStr([ // $mod.$main
- '$mod.Obj = $mod.TObject.$create("Create");',
- '$mod.TObject.vI = 3;',
- 'if ($mod.TObject.vI == 4);',
- '$mod.TObject.Sub=null;',
- '$mod.Obj.$class.Sub=null;',
- '$mod.Obj.Sub.$class.Sub=null;',
- '']));
- end;
- procedure TTestModule.TestClass_CallClassMethod;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' public');
- Add(' class var vI: longint;');
- Add(' class var Sub: TObject;');
- Add(' constructor Create;');
- Add(' function GetMore(Par: longint): longint;');
- Add(' class function GetIt(Par: longint): tobject;');
- Add(' end;');
- Add('constructor tobject.create;');
- Add('begin');
- Add(' sub:=getit(3);');
- Add(' vi:=getmore(4);');
- Add(' sub:=Self.getit(5);');
- Add(' vi:=Self.getmore(6);');
- Add('end;');
- Add('function tobject.getmore(par: longint): longint;');
- Add('begin');
- Add(' sub:=getit(11);');
- Add(' vi:=getmore(12);');
- Add(' sub:=self.getit(13);');
- Add(' vi:=self.getmore(14);');
- Add('end;');
- Add('class function tobject.getit(par: longint): tobject;');
- Add('begin');
- Add(' sub:=getit(21);');
- Add(' vi:=sub.getmore(22);');
- Add(' sub:=self.getit(23);');
- Add(' vi:=self.sub.getmore(24);');
- Add('end;');
- Add('var Obj: tobject;');
- Add('begin');
- Add(' obj:=tobject.create;');
- Add(' tobject.getit(5);');
- Add(' obj.getit(6);');
- Add(' obj.sub.getit(7);');
- Add(' obj.sub.getit(8).SUB:=nil;');
- Add(' obj.sub.getit(9).GETIT(10);');
- Add(' obj.sub.getit(11).SuB.getit(12);');
- ConvertProgram;
- CheckSource('TestClass_CallClassMethod',
- LinesToStr([ // statements
- 'rtl.createClass($mod,"TObject",null,function(){',
- ' this.vI = 0;',
- ' this.Sub = null;',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.Create = function(){',
- ' this.$class.Sub = this.$class.GetIt(3);',
- ' this.$class.vI = this.GetMore(4);',
- ' this.$class.Sub = this.$class.GetIt(5);',
- ' this.$class.vI = this.GetMore(6);',
- ' };',
- ' this.GetMore = function(Par){',
- ' var Result = 0;',
- ' this.$class.Sub = this.$class.GetIt(11);',
- ' this.$class.vI = this.GetMore(12);',
- ' this.$class.Sub = this.$class.GetIt(13);',
- ' this.$class.vI = this.GetMore(14);',
- ' return Result;',
- ' };',
- ' this.GetIt = function(Par){',
- ' var Result = null;',
- ' this.Sub = this.GetIt(21);',
- ' this.vI = this.Sub.GetMore(22);',
- ' this.Sub = this.GetIt(23);',
- ' this.vI = this.Sub.GetMore(24);',
- ' return Result;',
- ' };',
- '});',
- 'this.Obj = null;'
- ]),
- LinesToStr([ // $mod.$main
- '$mod.Obj = $mod.TObject.$create("Create");',
- '$mod.TObject.GetIt(5);',
- '$mod.Obj.$class.GetIt(6);',
- '$mod.Obj.Sub.$class.GetIt(7);',
- '$mod.Obj.Sub.$class.GetIt(8).$class.Sub=null;',
- '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
- '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
- '']));
- end;
- procedure TTestModule.TestClass_Property;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' Fx: longint;');
- Add(' Fy: longint;');
- Add(' function GetInt: longint;');
- Add(' procedure SetInt(Value: longint);');
- Add(' procedure DoIt;');
- Add(' property IntA: longint read Fx write Fy;');
- Add(' property IntB: longint read GetInt write SetInt;');
- Add(' end;');
- Add('function tobject.getint: longint;');
- Add('begin');
- Add(' result:=fx;');
- Add('end;');
- Add('procedure tobject.setint(value: longint);');
- Add('begin');
- Add(' if value=fy then exit;');
- Add(' fy:=value;');
- Add('end;');
- Add('procedure tobject.doit;');
- Add('begin');
- Add(' IntA:=IntA+1;');
- Add(' Self.IntA:=Self.IntA+1;');
- Add(' IntB:=IntB+1;');
- Add(' Self.IntB:=Self.IntB+1;');
- Add('end;');
- Add('var Obj: tobject;');
- Add('begin');
- Add(' obj.inta:=obj.inta+1;');
- Add(' if obj.intb=2 then;');
- Add(' obj.intb:=obj.intb+2;');
- Add(' obj.setint(obj.inta);');
- ConvertProgram;
- CheckSource('TestClass_Property',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' this.Fx = 0;',
- ' this.Fy = 0;',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.GetInt = function () {',
- ' var Result = 0;',
- ' Result = this.Fx;',
- ' return Result;',
- ' };',
- ' this.SetInt = function (Value) {',
- ' if (Value == this.Fy) return;',
- ' this.Fy = Value;',
- ' };',
- ' this.DoIt = function () {',
- ' this.Fy = this.Fx + 1;',
- ' this.Fy = this.Fx + 1;',
- ' this.SetInt(this.GetInt() + 1);',
- ' this.SetInt(this.GetInt() + 1);',
- ' };',
- '});',
- 'this.Obj = null;'
- ]),
- LinesToStr([ // $mod.$main
- '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
- 'if ($mod.Obj.GetInt() == 2);',
- '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
- '$mod.Obj.SetInt($mod.Obj.Fx);'
- ]));
- end;
- procedure TTestModule.TestClass_Property_ClassMethod;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' class var Fx: longint;');
- Add(' class var Fy: longint;');
- Add(' class function GetInt: longint;');
- Add(' class procedure SetInt(Value: longint);');
- Add(' class procedure DoIt;');
- Add(' class property IntA: longint read Fx write Fy;');
- Add(' class property IntB: longint read GetInt write SetInt;');
- Add(' end;');
- Add('class function tobject.getint: longint;');
- Add('begin');
- Add(' result:=fx;');
- Add('end;');
- Add('class procedure tobject.setint(value: longint);');
- Add('begin');
- Add('end;');
- Add('class procedure tobject.doit;');
- Add('begin');
- Add(' IntA:=IntA+1;');
- Add(' Self.IntA:=Self.IntA+1;');
- Add(' IntB:=IntB+1;');
- Add(' Self.IntB:=Self.IntB+1;');
- Add('end;');
- Add('var Obj: tobject;');
- Add('begin');
- Add(' tobject.inta:=tobject.inta+1;');
- Add(' if tobject.intb=2 then;');
- Add(' tobject.intb:=tobject.intb+2;');
- Add(' tobject.setint(tobject.inta);');
- Add(' obj.inta:=obj.inta+1;');
- Add(' if obj.intb=2 then;');
- Add(' obj.intb:=obj.intb+2;');
- Add(' obj.setint(obj.inta);');
- ConvertProgram;
- CheckSource('TestClass_Property_ClassMethod',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.Fx = 0;',
- ' this.Fy = 0;',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.GetInt = function () {',
- ' var Result = 0;',
- ' Result = this.Fx;',
- ' return Result;',
- ' };',
- ' this.SetInt = function (Value) {',
- ' };',
- ' this.DoIt = function () {',
- ' this.Fy = this.Fx + 1;',
- ' this.Fy = this.Fx + 1;',
- ' this.SetInt(this.GetInt() + 1);',
- ' this.SetInt(this.GetInt() + 1);',
- ' };',
- '});',
- 'this.Obj = null;'
- ]),
- LinesToStr([ // $mod.$main
- '$mod.TObject.Fy = $mod.TObject.Fx + 1;',
- 'if ($mod.TObject.GetInt() == 2);',
- '$mod.TObject.SetInt($mod.TObject.GetInt() + 2);',
- '$mod.TObject.SetInt($mod.TObject.Fx);',
- '$mod.Obj.$class.Fy = $mod.Obj.Fx + 1;',
- 'if ($mod.Obj.$class.GetInt() == 2);',
- '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
- '$mod.Obj.$class.SetInt($mod.Obj.Fx);'
- ]));
- end;
- procedure TTestModule.TestClass_Property_Index;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' FItems: array of longint;');
- Add(' function GetItems(Index: longint): longint;');
- Add(' procedure SetItems(Index: longint; Value: longint);');
- Add(' procedure DoIt;');
- Add(' property Items[Index: longint]: longint read getitems write setitems;');
- Add(' end;');
- Add('function tobject.getitems(index: longint): longint;');
- Add('begin');
- Add(' Result:=fitems[index];');
- Add('end;');
- Add('procedure tobject.setitems(index: longint; value: longint);');
- Add('begin');
- Add(' fitems[index]:=value;');
- Add('end;');
- Add('procedure tobject.doit;');
- Add('begin');
- Add(' items[1]:=2;');
- Add(' items[3]:=items[4];');
- Add(' self.items[5]:=self.items[6];');
- Add(' items[items[7]]:=items[items[8]];');
- Add('end;');
- Add('var Obj: tobject;');
- Add('begin');
- Add(' obj.Items[11]:=obj.Items[12];');
- ConvertProgram;
- CheckSource('TestClass_Property_Index',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' this.FItems = [];',
- ' };',
- ' this.$final = function () {',
- ' this.FItems = undefined;',
- ' };',
- ' this.GetItems = function (Index) {',
- ' var Result = 0;',
- ' Result = this.FItems[Index];',
- ' return Result;',
- ' };',
- ' this.SetItems = function (Index, Value) {',
- ' this.FItems[Index] = Value;',
- ' };',
- ' this.DoIt = function () {',
- ' this.SetItems(1, 2);',
- ' this.SetItems(3,this.GetItems(4));',
- ' this.SetItems(5,this.GetItems(6));',
- ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
- ' };',
- '});',
- 'this.Obj = null;'
- ]),
- LinesToStr([ // $mod.$main
- '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
- ]));
- end;
- procedure TTestModule.TestClass_PropertyOfTypeArray;
- begin
- StartProgram(false);
- Add('type');
- Add(' TArray = array of longint;');
- Add(' TObject = class');
- Add(' FItems: TArray;');
- Add(' function GetItems: tarray;');
- Add(' procedure SetItems(Value: tarray);');
- Add(' property Items: tarray read getitems write setitems;');
- Add(' end;');
- Add('function tobject.getitems: tarray;');
- Add('begin');
- Add(' Result:=fitems;');
- Add('end;');
- Add('procedure tobject.setitems(value: tarray);');
- Add('begin');
- Add(' fitems:=value;');
- Add(' fitems:=nil;');
- Add(' Items:=nil;');
- Add(' Items:=Items;');
- Add(' Items[1]:=2;');
- Add(' fitems[3]:=Items[4];');
- Add(' Items[5]:=Items[6];');
- Add(' Self.Items[7]:=8;');
- Add(' Self.Items[9]:=Self.Items[10];');
- Add(' Items[Items[11]]:=Items[Items[12]];');
- Add('end;');
- Add('var Obj: tobject;');
- Add('begin');
- Add(' obj.items:=nil;');
- Add(' obj.items:=obj.items;');
- Add(' obj.items[11]:=obj.items[12];');
- ConvertProgram;
- CheckSource('TestClass_PropertyOfTypeArray',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' this.FItems = [];',
- ' };',
- ' this.$final = function () {',
- ' this.FItems = undefined;',
- ' };',
- ' this.GetItems = function () {',
- ' var Result = [];',
- ' Result = this.FItems;',
- ' return Result;',
- ' };',
- ' this.SetItems = function (Value) {',
- ' this.FItems = Value;',
- ' this.FItems = [];',
- ' this.SetItems([]);',
- ' this.SetItems(this.GetItems());',
- ' this.GetItems()[1] = 2;',
- ' this.FItems[3] = this.GetItems()[4];',
- ' this.GetItems()[5] = this.GetItems()[6];',
- ' this.GetItems()[7] = 8;',
- ' this.GetItems()[9] = this.GetItems()[10];',
- ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
- ' };',
- '});',
- 'this.Obj = null;'
- ]),
- LinesToStr([ // $mod.$main
- '$mod.Obj.SetItems([]);',
- '$mod.Obj.SetItems($mod.Obj.GetItems());',
- '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
- ]));
- end;
- procedure TTestModule.TestClass_PropertyDefault;
- begin
- StartProgram(false);
- Add('type');
- Add(' TArray = array of longint;');
- Add(' TObject = class');
- Add(' FItems: TArray;');
- Add(' function GetItems(Index: longint): longint;');
- Add(' procedure SetItems(Index, Value: longint);');
- Add(' property Items[Index: longint]: longint read getitems write setitems; default;');
- Add(' end;');
- Add('function tobject.getitems(index: longint): longint;');
- Add('begin');
- Add('end;');
- Add('procedure tobject.setitems(index, value: longint);');
- Add('begin');
- Add(' Self[1]:=2;');
- Add(' Self[3]:=Self[index];');
- Add(' Self[index]:=Self[Self[value]];');
- Add(' Self[Self[4]]:=value;');
- Add('end;');
- Add('var Obj: tobject;');
- Add('begin');
- Add(' obj[11]:=12;');
- Add(' obj[13]:=obj[14];');
- Add(' obj[obj[15]]:=obj[obj[15]];');
- ConvertProgram;
- CheckSource('TestClass_PropertyDefault',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' this.FItems = [];',
- ' };',
- ' this.$final = function () {',
- ' this.FItems = undefined;',
- ' };',
- ' this.GetItems = function (Index) {',
- ' var Result = 0;',
- ' return Result;',
- ' };',
- ' this.SetItems = function (Index, Value) {',
- ' this.SetItems(1, 2);',
- ' this.SetItems(3, this.GetItems(Index));',
- ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
- ' this.SetItems(this.GetItems(4), Value);',
- ' };',
- '});',
- 'this.Obj = null;'
- ]),
- LinesToStr([ // $mod.$main
- '$mod.Obj.SetItems(11, 12);',
- '$mod.Obj.SetItems(13, $mod.Obj.GetItems(14));',
- '$mod.Obj.SetItems($mod.Obj.GetItems(15), $mod.Obj.GetItems($mod.Obj.GetItems(15)));'
- ]));
- end;
- procedure TTestModule.TestClass_PropertyOverride;
- begin
- StartProgram(false);
- Add('type');
- Add(' integer = longint;');
- Add(' TObject = class');
- Add(' FItem: integer;');
- Add(' function GetItem: integer; external name ''GetItem'';');
- Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
- Add(' property Item: integer read getitem write setitem;');
- Add(' end;');
- Add(' TCar = class');
- Add(' FBag: integer;');
- Add(' function GetBag: integer; external name ''GetBag'';');
- Add(' property Item read getbag;');
- Add(' end;');
- Add('var');
- Add(' Obj: tobject;');
- Add(' Car: tcar;');
- Add('begin');
- Add(' Obj.Item:=Obj.Item;');
- Add(' Car.Item:=Car.Item;');
- ConvertProgram;
- CheckSource('TestClass_PropertyOverride',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' this.FItem = 0;',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
- ' this.$init = function () {',
- ' $mod.TObject.$init.call(this);',
- ' this.FBag = 0;',
- ' };',
- '});',
- 'this.Obj = null;',
- 'this.Car = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.Obj.SetItem($mod.Obj.GetItem());',
- '$mod.Car.SetItem($mod.Car.GetBag());',
- '']));
- end;
- procedure TTestModule.TestClass_Assigned;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' end;');
- Add('var');
- Add(' Obj: tobject;');
- Add(' b: boolean;');
- Add('begin');
- Add(' if Assigned(obj) then ;');
- Add(' b:=Assigned(obj) or false;');
- ConvertProgram;
- CheckSource('TestClass_Assigned',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'this.Obj = null;',
- 'this.b = false;'
- ]),
- LinesToStr([ // $mod.$main
- 'if ($mod.Obj != null);',
- '$mod.b = ($mod.Obj != null) || false;'
- ]));
- end;
- procedure TTestModule.TestClass_WithClassDoCreate;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' aBool: boolean;');
- Add(' Arr: array of boolean;');
- Add(' constructor Create;');
- Add(' end;');
- Add('constructor TObject.Create; begin end;');
- Add('var');
- Add(' Obj: tobject;');
- Add(' b: boolean;');
- Add('begin');
- Add(' with tobject.create do begin');
- Add(' b:=abool;');
- Add(' abool:=b;');
- Add(' b:=arr[1];');
- Add(' arr[2]:=b;');
- Add(' end;');
- Add(' with tobject do');
- Add(' obj:=create;');
- Add(' with obj do begin');
- Add(' create;');
- Add(' b:=abool;');
- Add(' abool:=b;');
- Add(' b:=arr[3];');
- Add(' arr[4]:=b;');
- Add(' end;');
- ConvertProgram;
- CheckSource('TestClass_WithClassDoCreate',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' this.aBool = false;',
- ' this.Arr = [];',
- ' };',
- ' this.$final = function () {',
- ' this.Arr = undefined;',
- ' };',
- ' this.Create = function () {',
- ' };',
- '});',
- 'this.Obj = null;',
- 'this.b = false;'
- ]),
- LinesToStr([ // $mod.$main
- 'var $with1 = $mod.TObject.$create("Create");',
- '$mod.b = $with1.aBool;',
- '$with1.aBool = $mod.b;',
- '$mod.b = $with1.Arr[1];',
- '$with1.Arr[2] = $mod.b;',
- 'var $with2 = $mod.TObject;',
- '$mod.Obj = $with2.$create("Create");',
- 'var $with3 = $mod.Obj;',
- '$with3.Create();',
- '$mod.b = $with3.aBool;',
- '$with3.aBool = $mod.b;',
- '$mod.b = $with3.Arr[3];',
- '$with3.Arr[4] = $mod.b;',
- '']));
- end;
- procedure TTestModule.TestClass_WithClassInstDoProperty;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' FInt: longint;');
- Add(' constructor Create;');
- Add(' function GetSize: longint;');
- Add(' procedure SetSize(Value: longint);');
- Add(' property Int: longint read FInt write FInt;');
- Add(' property Size: longint read GetSize write SetSize;');
- Add(' end;');
- Add('constructor TObject.Create; begin end;');
- Add('function TObject.GetSize: longint; begin; end;');
- Add('procedure TObject.SetSize(Value: longint); begin; end;');
- Add('var');
- Add(' Obj: tobject;');
- Add(' i: longint;');
- Add('begin');
- Add(' with TObject.Create do begin');
- Add(' i:=int;');
- Add(' int:=i;');
- Add(' i:=size;');
- Add(' size:=i;');
- Add(' end;');
- Add(' with obj do begin');
- Add(' i:=int;');
- Add(' int:=i;');
- Add(' i:=size;');
- Add(' size:=i;');
- Add(' end;');
- ConvertProgram;
- CheckSource('TestClass_WithClassInstDoProperty',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' this.FInt = 0;',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.Create = function () {',
- ' };',
- ' this.GetSize = function () {',
- ' var Result = 0;',
- ' return Result;',
- ' };',
- ' this.SetSize = function (Value) {',
- ' };',
- '});',
- 'this.Obj = null;',
- 'this.i = 0;'
- ]),
- LinesToStr([ // $mod.$main
- 'var $with1 = $mod.TObject.$create("Create");',
- '$mod.i = $with1.FInt;',
- '$with1.FInt = $mod.i;',
- '$mod.i = $with1.GetSize();',
- '$with1.SetSize($mod.i);',
- 'var $with2 = $mod.Obj;',
- '$mod.i = $with2.FInt;',
- '$with2.FInt = $mod.i;',
- '$mod.i = $with2.GetSize();',
- '$with2.SetSize($mod.i);',
- '']));
- end;
- procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' constructor Create;');
- Add(' function GetItems(Index: longint): longint;');
- Add(' procedure SetItems(Index, Value: longint);');
- Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
- Add(' end;');
- Add('constructor TObject.Create; begin end;');
- Add('function tobject.getitems(index: longint): longint; begin; end;');
- Add('procedure tobject.setitems(index, value: longint); begin; end;');
- Add('var');
- Add(' Obj: tobject;');
- Add(' i: longint;');
- Add('begin');
- Add(' with TObject.Create do begin');
- Add(' i:=Items[1];');
- Add(' Items[2]:=i;');
- Add(' end;');
- Add(' with obj do begin');
- Add(' i:=Items[3];');
- Add(' Items[4]:=i;');
- Add(' end;');
- ConvertProgram;
- CheckSource('TestClass_WithClassInstDoPropertyWithParams',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.Create = function () {',
- ' };',
- ' this.GetItems = function (Index) {',
- ' var Result = 0;',
- ' return Result;',
- ' };',
- ' this.SetItems = function (Index, Value) {',
- ' };',
- '});',
- 'this.Obj = null;',
- 'this.i = 0;'
- ]),
- LinesToStr([ // $mod.$main
- 'var $with1 = $mod.TObject.$create("Create");',
- '$mod.i = $with1.GetItems(1);',
- '$with1.SetItems(2, $mod.i);',
- 'var $with2 = $mod.Obj;',
- '$mod.i = $with2.GetItems(3);',
- '$with2.SetItems(4, $mod.i);',
- '']));
- end;
- procedure TTestModule.TestClass_WithClassInstDoFunc;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' constructor Create;');
- Add(' function GetSize: longint;');
- Add(' procedure SetSize(Value: longint);');
- Add(' end;');
- Add('constructor TObject.Create; begin end;');
- Add('function TObject.GetSize: longint; begin; end;');
- Add('procedure TObject.SetSize(Value: longint); begin; end;');
- Add('var');
- Add(' Obj: tobject;');
- Add(' i: longint;');
- Add('begin');
- Add(' with TObject.Create do begin');
- Add(' i:=GetSize;');
- Add(' i:=GetSize();');
- Add(' SetSize(i);');
- Add(' end;');
- Add(' with obj do begin');
- Add(' i:=GetSize;');
- Add(' i:=GetSize();');
- Add(' SetSize(i);');
- Add(' end;');
- ConvertProgram;
- CheckSource('TestClass_WithClassInstDoFunc',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.Create = function () {',
- ' };',
- ' this.GetSize = function () {',
- ' var Result = 0;',
- ' return Result;',
- ' };',
- ' this.SetSize = function (Value) {',
- ' };',
- '});',
- 'this.Obj = null;',
- 'this.i = 0;'
- ]),
- LinesToStr([ // $mod.$main
- 'var $with1 = $mod.TObject.$create("Create");',
- '$mod.i = $with1.GetSize();',
- '$mod.i = $with1.GetSize();',
- '$with1.SetSize($mod.i);',
- 'var $with2 = $mod.Obj;',
- '$mod.i = $with2.GetSize();',
- '$mod.i = $with2.GetSize();',
- '$with2.SetSize($mod.i);',
- '']));
- end;
- procedure TTestModule.TestClass_TypeCast;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' Next: TObject;');
- Add(' constructor Create;');
- Add(' end;');
- Add(' TControl = class(TObject)');
- Add(' Arr: array of TObject;');
- Add(' function GetIt(vI: longint = 0): TObject;');
- Add(' end;');
- Add('constructor tobject.create; begin end;');
- Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
- Add('var');
- Add(' Obj: tobject;');
- Add('begin');
- Add(' obj:=tcontrol(obj).next;');
- Add(' tcontrol(obj):=nil;');
- Add(' obj:=tcontrol(obj);');
- Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
- Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
- Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
- Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
- ConvertProgram;
- CheckSource('TestClass_TypeCast',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' this.Next = null;',
- ' };',
- ' this.$final = function () {',
- ' this.Next = undefined;',
- ' };',
- ' this.Create = function () {',
- ' };',
- '});',
- 'rtl.createClass($mod, "TControl", $mod.TObject, function () {',
- ' this.$init = function () {',
- ' $mod.TObject.$init.call(this);',
- ' this.Arr = [];',
- ' };',
- ' this.$final = function () {',
- ' this.Arr = undefined;',
- ' $mod.TObject.$final.call(this);',
- ' };',
- ' this.GetIt = function (vI) {',
- ' var Result = null;',
- ' return Result;',
- ' };',
- '});',
- 'this.Obj = null;'
- ]),
- LinesToStr([ // $mod.$main
- '$mod.Obj = $mod.Obj.Next;',
- '$mod.Obj = null;',
- '$mod.Obj = $mod.Obj;',
- '$mod.Obj = $mod.Obj.GetIt(0);',
- '$mod.Obj = $mod.Obj.GetIt(0);',
- '$mod.Obj = $mod.Obj.GetIt(1);',
- '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
- '']));
- end;
- procedure TTestModule.TestClass_TypeCastUntypedParam;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class end;');
- Add('procedure ProcA(var A);');
- Add('begin');
- Add(' TObject(A):=nil;');
- Add(' TObject(A):=TObject(A);');
- Add(' if TObject(A)=nil then ;');
- Add(' if nil=TObject(A) then ;');
- Add('end;');
- Add('procedure ProcB(out A);');
- Add('begin');
- Add(' TObject(A):=nil;');
- Add(' TObject(A):=TObject(A);');
- Add(' if TObject(A)=nil then ;');
- Add(' if nil=TObject(A) then ;');
- Add('end;');
- Add('procedure ProcC(const A);');
- Add('begin');
- Add(' if TObject(A)=nil then ;');
- Add(' if nil=TObject(A) then ;');
- Add('end;');
- Add('var o: TObject;');
- Add('begin');
- Add(' ProcA(o);');
- Add(' ProcB(o);');
- Add(' ProcC(o);');
- ConvertProgram;
- CheckSource('TestClass_TypeCastUntypedParam',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'this.ProcA = function (A) {',
- ' A.set(null);',
- ' A.set(A.get());',
- ' if (A.get() == null);',
- ' if (null == A.get());',
- '};',
- 'this.ProcB = function (A) {',
- ' A.set(null);',
- ' A.set(A.get());',
- ' if (A.get() == null);',
- ' if (null == A.get());',
- '};',
- 'this.ProcC = function (A) {',
- ' if (A == null);',
- ' if (null == A);',
- '};',
- 'this.o = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.ProcA({',
- ' p: $mod,',
- ' get: function () {',
- ' return this.p.o;',
- ' },',
- ' set: function (v) {',
- ' this.p.o = v;',
- ' }',
- '});',
- '$mod.ProcB({',
- ' p: $mod,',
- ' get: function () {',
- ' return this.p.o;',
- ' },',
- ' set: function (v) {',
- ' this.p.o = v;',
- ' }',
- '});',
- '$mod.ProcC($mod.o);',
- '']));
- end;
- procedure TTestModule.TestClass_Overloads;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' procedure DoIt;');
- Add(' procedure DoIt(vI: longint);');
- Add(' end;');
- Add('procedure TObject.DoIt;');
- Add('begin');
- Add(' DoIt;');
- Add(' DoIt(1);');
- Add('end;');
- Add('procedure TObject.DoIt(vI: longint); begin end;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestClass_Overloads',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.DoIt = function () {',
- ' this.DoIt();',
- ' this.DoIt$1(1);',
- ' };',
- ' this.DoIt$1 = function (vI) {',
- ' };',
- '});',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestModule.TestClass_OverloadsAncestor;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class;');
- Add(' TObject = class');
- Add(' procedure DoIt(vA: longint);');
- Add(' procedure DoIt(vA, vB: longint);');
- Add(' end;');
- Add(' TCar = class;');
- Add(' TCar = class');
- Add(' procedure DoIt(vA: longint);');
- Add(' procedure DoIt(vA, vB: longint);');
- Add(' end;');
- Add('procedure tobject.doit(va: longint);');
- Add('begin');
- Add(' doit(1);');
- Add(' doit(1,2);');
- Add('end;');
- Add('procedure tobject.doit(va, vb: longint); begin end;');
- Add('procedure tcar.doit(va: longint);');
- Add('begin');
- Add(' doit(1);');
- Add(' doit(1,2);');
- Add(' inherited doit(1);');
- Add(' inherited doit(1,2);');
- Add('end;');
- Add('procedure tcar.doit(va, vb: longint); begin end;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestClass_OverloadsAncestor',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.DoIt = function (vA) {',
- ' this.DoIt(1);',
- ' this.DoIt$1(1,2);',
- ' };',
- ' this.DoIt$1 = function (vA, vB) {',
- ' };',
- '});',
- 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
- ' this.DoIt$2 = function (vA) {',
- ' this.DoIt$2(1);',
- ' this.DoIt$3(1, 2);',
- ' $mod.TObject.DoIt.call(this, 1);',
- ' $mod.TObject.DoIt$1.call(this, 1, 2);',
- ' };',
- ' this.DoIt$3 = function (vA, vB) {',
- ' };',
- '});',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestModule.TestClass_OverloadConstructor;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' constructor Create(vA: longint);');
- Add(' constructor Create(vA, vB: longint);');
- Add(' end;');
- Add(' TCar = class');
- Add(' constructor Create(vA: longint);');
- Add(' constructor Create(vA, vB: longint);');
- Add(' end;');
- Add('constructor tobject.create(va: longint);');
- Add('begin');
- Add(' create(1);');
- Add(' create(1,2);');
- Add('end;');
- Add('constructor tobject.create(va, vb: longint); begin end;');
- Add('constructor tcar.create(va: longint);');
- Add('begin');
- Add(' create(1);');
- Add(' create(1,2);');
- Add(' inherited create(1);');
- Add(' inherited create(1,2);');
- Add('end;');
- Add('constructor tcar.create(va, vb: longint); begin end;');
- Add('begin');
- Add(' tobject.create(1);');
- Add(' tobject.create(1,2);');
- Add(' tcar.create(1);');
- Add(' tcar.create(1,2);');
- ConvertProgram;
- CheckSource('TestClass_OverloadConstructor',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.Create = function (vA) {',
- ' this.Create(1);',
- ' this.Create$1(1,2);',
- ' };',
- ' this.Create$1 = function (vA, vB) {',
- ' };',
- '});',
- 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
- ' this.Create$2 = function (vA) {',
- ' this.Create$2(1);',
- ' this.Create$3(1, 2);',
- ' $mod.TObject.Create.call(this, 1);',
- ' $mod.TObject.Create$1.call(this, 1, 2);',
- ' };',
- ' this.Create$3 = function (vA, vB) {',
- ' };',
- '});',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.TObject.$create("Create", [1]);',
- '$mod.TObject.$create("Create$1", [1, 2]);',
- '$mod.TCar.$create("Create$2", [1]);',
- '$mod.TCar.$create("Create$3", [1, 2]);',
- '']));
- end;
- procedure TTestModule.TestClass_ReintroducedVar;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' strict private');
- Add(' Some: longint;');
- Add(' end;');
- Add(' TMobile = class');
- Add(' strict private');
- Add(' Some: string;');
- Add(' end;');
- Add(' TCar = class(tmobile)');
- Add(' procedure Some;');
- Add(' procedure Some(vA: longint);');
- Add(' end;');
- Add('procedure tcar.some;');
- Add('begin');
- Add(' Some;');
- Add(' Some(1);');
- Add('end;');
- Add('procedure tcar.some(va: longint); begin end;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestClass_ReintroducedVar',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' this.Some = 0;',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'rtl.createClass($mod, "TMobile", $mod.TObject, function () {',
- ' this.$init = function () {',
- ' $mod.TObject.$init.call(this);',
- ' this.Some$1 = "";',
- ' };',
- '});',
- 'rtl.createClass($mod, "TCar", $mod.TMobile, function () {',
- ' this.Some$2 = function () {',
- ' this.Some$2();',
- ' this.Some$3(1);',
- ' };',
- ' this.Some$3 = function (vA) {',
- ' };',
- '});',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestModule.TestClass_RaiseDescendant;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' constructor Create(Msg: string);');
- Add(' end;');
- Add(' Exception = class');
- Add(' end;');
- Add(' EConvertError = class(Exception)');
- Add(' end;');
- Add('constructor TObject.Create(Msg: string); begin end;');
- Add('begin');
- Add(' raise Exception.Create(''Bar1'');');
- Add(' raise EConvertError.Create(''Bar2'');');
- ConvertProgram;
- CheckSource('TestClass_RaiseDescendant',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.Create = function (Msg) {',
- ' };',
- '});',
- 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
- '});',
- 'rtl.createClass($mod, "EConvertError", $mod.Exception, function () {',
- '});',
- '']),
- LinesToStr([ // $mod.$main
- 'throw $mod.Exception.$create("Create",["Bar1"]);',
- 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
- '']));
- end;
- procedure TTestModule.TestClass_ExternalMethod;
- begin
- AddModuleWithIntfImplSrc('unit2.pas',
- LinesToStr([
- 'type',
- ' TObject = class',
- ' public',
- ' procedure Intern; external name ''$DoIntern'';',
- ' end;',
- '']),
- LinesToStr([
- '']));
- StartUnit(true);
- Add('interface');
- Add('uses unit2;');
- Add('type');
- Add(' TCar = class(TObject)');
- Add(' public');
- Add(' procedure Intern2; external name ''$DoIntern2'';');
- Add(' procedure DoIt;');
- Add(' end;');
- Add('implementation');
- Add('procedure tcar.doit;');
- Add('begin');
- Add(' Intern;');
- Add(' Intern();');
- Add(' Intern2;');
- Add(' Intern2();');
- Add('end;');
- Add('var Obj: TCar;');
- Add('begin');
- Add(' obj.intern;');
- Add(' obj.intern();');
- Add(' obj.intern2;');
- Add(' obj.intern2();');
- Add(' obj.doit;');
- Add(' obj.doit();');
- Add(' with obj do begin');
- Add(' Intern;');
- Add(' Intern();');
- Add(' Intern2;');
- Add(' Intern2();');
- Add(' end;');
- ConvertUnit;
- CheckSource('TestClass_ExternalMethod',
- LinesToStr([
- 'var $impl = $mod.$impl;',
- 'rtl.createClass($mod, "TCar", pas.unit2.TObject, function () {',
- ' this.DoIt = function () {',
- ' this.$DoIntern();',
- ' this.$DoIntern();',
- ' this.$DoIntern2();',
- ' this.$DoIntern2();',
- ' };',
- ' });',
- '']),
- LinesToStr([ // this.$init
- '$impl.Obj.$DoIntern();',
- '$impl.Obj.$DoIntern();',
- '$impl.Obj.$DoIntern2();',
- '$impl.Obj.$DoIntern2();',
- '$impl.Obj.DoIt();',
- '$impl.Obj.DoIt();',
- 'var $with1 = $impl.Obj;',
- '$with1.$DoIntern();',
- '$with1.$DoIntern();',
- '$with1.$DoIntern2();',
- '$with1.$DoIntern2();',
- '']),
- LinesToStr([ // implementation
- '$impl.Obj = null;',
- '']) );
- end;
- procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' procedure DoIt; virtual; external name ''Foo'';');
- Add(' end;');
- Add('begin');
- SetExpectedPasResolverError('Virtual method name must match external',
- nVirtualMethodNameMustMatchExternal);
- ConvertProgram;
- end;
- procedure TTestModule.TestClass_ExternalOverrideFail;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' procedure DoIt; virtual; external name ''DoIt'';');
- Add(' end;');
- Add(' TCar = class');
- Add(' procedure DoIt; override; external name ''DoIt'';');
- Add(' end;');
- Add('begin');
- SetExpectedPasResolverError('Invalid procedure modifier override,external',
- nInvalidXModifierY);
- ConvertProgram;
- end;
- procedure TTestModule.TestClass_ExternalVar;
- begin
- AddModuleWithIntfImplSrc('unit2.pas',
- LinesToStr([
- '{$modeswitch externalclass}',
- 'type',
- ' TObject = class',
- ' public',
- ' Intern: longint external name ''$Intern'';',
- ' end;',
- '']),
- LinesToStr([
- '']));
- StartUnit(true);
- Add('interface');
- Add('uses unit2;');
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TCar = class(tobject)');
- Add(' public');
- Add(' Intern2: longint external name ''$Intern2'';');
- Add(' procedure DoIt;');
- Add(' end;');
- Add('implementation');
- Add('procedure tcar.doit;');
- Add('begin');
- Add(' Intern:=Intern+1;');
- Add(' Intern2:=Intern2+2;');
- Add('end;');
- Add('var Obj: TCar;');
- Add('begin');
- Add(' obj.intern:=obj.intern+1;');
- Add(' obj.intern2:=obj.intern2+2;');
- Add(' with obj do begin');
- Add(' intern:=intern+1;');
- Add(' intern2:=intern2+2;');
- Add(' end;');
- ConvertUnit;
- CheckSource('TestClass_ExternalVar',
- LinesToStr([
- 'var $impl = $mod.$impl;',
- 'rtl.createClass($mod, "TCar", pas.unit2.TObject, function () {',
- ' this.DoIt = function () {',
- ' this.$Intern = this.$Intern + 1;',
- ' this.$Intern2 = this.$Intern2 + 2;',
- ' };',
- ' });',
- '']),
- LinesToStr([
- '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
- '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
- 'var $with1 = $impl.Obj;',
- '$with1.$Intern = $with1.$Intern + 1;',
- '$with1.$Intern2 = $with1.$Intern2 + 2;',
- '']),
- LinesToStr([ // implementation
- '$impl.Obj = null;',
- '']));
- end;
- procedure TTestModule.TestClass_Const;
- begin
- StartProgram(false);
- Add('type');
- Add(' integer = longint;');
- Add(' TClass = class of TObject;');
- Add(' TObject = class');
- Add(' public');
- Add(' const cI: integer = 3;');
- Add(' procedure DoIt;');
- Add(' class procedure DoMore;');
- Add(' end;');
- Add('implementation');
- Add('procedure tobject.doit;');
- Add('begin');
- Add(' if cI=4 then;');
- Add(' if 5=cI then;');
- Add(' if Self.cI=6 then;');
- Add(' if 7=Self.cI then;');
- Add(' with Self do begin');
- Add(' if cI=11 then;');
- Add(' if 12=cI then;');
- Add(' end;');
- Add('end;');
- Add('class procedure tobject.domore;');
- Add('begin');
- Add(' if cI=8 then;');
- Add(' if Self.cI=9 then;');
- Add(' if 10=cI then;');
- Add(' if 11=Self.cI then;');
- Add(' with Self do begin');
- Add(' if cI=13 then;');
- Add(' if 14=cI then;');
- Add(' end;');
- Add('end;');
- Add('var');
- Add(' Obj: TObject;');
- Add(' Cla: TClass;');
- Add('begin');
- Add(' if TObject.cI=21 then ;');
- Add(' if Obj.cI=22 then ;');
- Add(' if Cla.cI=23 then ;');
- Add(' with obj do if ci=24 then;');
- Add(' with TObject do if ci=25 then;');
- Add(' with Cla do if ci=26 then;');
- ConvertProgram;
- CheckSource('TestClass_Const',
- LinesToStr([
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.cI = 3;',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.DoIt = function () {',
- ' if (this.cI == 4) ;',
- ' if (5 == this.cI) ;',
- ' if (this.cI == 6) ;',
- ' if (7 == this.cI) ;',
- ' if (this.cI == 11) ;',
- ' if (12 == this.cI) ;',
- ' };',
- ' this.DoMore = function () {',
- ' if (this.cI == 8) ;',
- ' if (this.cI == 9) ;',
- ' if (10 == this.cI) ;',
- ' if (11 == this.cI) ;',
- ' if (this.cI == 13) ;',
- ' if (14 == this.cI) ;',
- ' };',
- '});',
- 'this.Obj = null;',
- 'this.Cla = null;',
- '']),
- LinesToStr([
- 'if ($mod.TObject.cI == 21) ;',
- 'if ($mod.Obj.cI == 22) ;',
- 'if ($mod.Cla.cI == 23) ;',
- 'var $with1 = $mod.Obj;',
- 'if ($with1.cI == 24) ;',
- 'var $with2 = $mod.TObject;',
- 'if ($with2.cI == 25) ;',
- 'var $with3 = $mod.Cla;',
- 'if ($with3.cI == 26) ;',
- '']));
- end;
- procedure TTestModule.TestClass_LocalVarSelfFail;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' TObject = class',
- ' constructor Create;',
- ' end;',
- 'constructor tobject.create;',
- 'var self: longint;',
- 'begin',
- 'end',
- 'begin',
- '']);
- SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
- ConvertProgram;
- end;
- procedure TTestModule.TestClass_ArgSelfFail;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' TObject = class',
- ' procedure DoIt(Self: longint);',
- ' end;',
- 'procedure tobject.doit(self: longint);',
- 'begin',
- 'end',
- 'begin',
- '']);
- SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,23)',nDuplicateIdentifier);
- ConvertProgram;
- end;
- procedure TTestModule.TestClass_NestedSelf;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' TObject = class',
- ' Key: longint;',
- ' class var State: longint;',
- ' procedure DoIt;',
- ' function GetSize: longint; virtual; abstract;',
- ' procedure SetSize(Value: longint); virtual; abstract;',
- ' property Size: longint read GetSize write SetSize;',
- ' end;',
- 'procedure tobject.doit;',
- ' procedure Sub;',
- ' begin',
- ' key:=key+2;',
- ' self.key:=self.key+3;',
- ' state:=state+4;',
- ' self.state:=self.state+5;',
- ' tobject.state:=tobject.state+6;',
- ' size:=size+7;',
- ' self.size:=self.size+8;',
- ' end;',
- 'begin',
- ' sub;',
- ' key:=key+12;',
- ' self.key:=self.key+13;',
- ' state:=state+14;',
- ' self.state:=self.state+15;',
- ' tobject.state:=tobject.state+16;',
- ' size:=size+17;',
- ' self.size:=self.size+18;',
- 'end;',
- 'begin',
- '']);
- ConvertProgram;
- CheckSource('TestClass_NestedSelf',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.State = 0;',
- ' this.$init = function () {',
- ' this.Key = 0;',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.DoIt = function () {',
- ' var Self = this;',
- ' function Sub() {',
- ' Self.Key = Self.Key + 2;',
- ' Self.Key = Self.Key + 3;',
- ' Self.$class.State = Self.State + 4;',
- ' Self.$class.State = Self.State + 5;',
- ' $mod.TObject.State = $mod.TObject.State + 6;',
- ' Self.SetSize(Self.GetSize() + 7);',
- ' Self.SetSize(Self.GetSize() + 8);',
- ' };',
- ' Sub();',
- ' Self.Key = Self.Key + 12;',
- ' Self.Key = Self.Key + 13;',
- ' Self.$class.State = Self.State + 14;',
- ' Self.$class.State = Self.State + 15;',
- ' $mod.TObject.State = $mod.TObject.State + 16;',
- ' Self.SetSize(Self.GetSize() + 17);',
- ' Self.SetSize(Self.GetSize() + 18);',
- ' };',
- '});',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestModule.TestClass_NestedClassSelf;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' TObject = class',
- ' class var State: longint;',
- ' class procedure DoIt;',
- ' class function GetSize: longint; virtual; abstract;',
- ' class procedure SetSize(Value: longint); virtual; abstract;',
- ' class property Size: longint read GetSize write SetSize;',
- ' end;',
- 'class procedure tobject.doit;',
- ' procedure Sub;',
- ' begin',
- ' state:=state+2;',
- ' self.state:=self.state+3;',
- ' tobject.state:=tobject.state+4;',
- ' size:=size+5;',
- ' self.size:=self.size+6;',
- ' tobject.size:=tobject.size+7;',
- ' end;',
- 'begin',
- ' sub;',
- ' state:=state+12;',
- ' self.state:=self.state+13;',
- ' tobject.state:=tobject.state+14;',
- ' size:=size+15;',
- ' self.size:=self.size+16;',
- ' tobject.size:=tobject.size+17;',
- 'end;',
- 'begin',
- '']);
- ConvertProgram;
- CheckSource('TestClass_NestedClassSelf',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.State = 0;',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.DoIt = function () {',
- ' var Self = this;',
- ' function Sub() {',
- ' Self.State = Self.State + 2;',
- ' Self.State = Self.State + 3;',
- ' $mod.TObject.State = $mod.TObject.State + 4;',
- ' Self.SetSize(Self.GetSize() + 5);',
- ' Self.SetSize(Self.GetSize() + 6);',
- ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
- ' };',
- ' Sub();',
- ' Self.State = Self.State + 12;',
- ' Self.State = Self.State + 13;',
- ' $mod.TObject.State = $mod.TObject.State + 14;',
- ' Self.SetSize(Self.GetSize() + 15);',
- ' Self.SetSize(Self.GetSize() + 16);',
- ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
- ' };',
- '});',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestModule.TestClass_NestedCallInherited;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' TObject = class',
- ' function DoIt(k: boolean): longint; virtual;',
- ' end;',
- ' TBird = class',
- ' function DoIt(k: boolean): longint; override;',
- ' end;',
- 'function tobject.doit(k: boolean): longint;',
- 'begin',
- 'end;',
- 'function tbird.doit(k: boolean): longint;',
- ' procedure Sub;',
- ' begin',
- ' inherited DoIt(true);',
- //' if inherited DoIt(false)=4 then ;',
- ' end;',
- 'begin',
- ' Sub;',
- ' inherited;',
- ' inherited DoIt(true);',
- //' if inherited DoIt(false)=14 then ;',
- 'end;',
- 'begin',
- '']);
- ConvertProgram;
- CheckSource('TestClass_NestedCallInherited',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.DoIt = function (k) {',
- ' var Result = 0;',
- ' return Result;',
- ' };',
- '});',
- 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
- ' this.DoIt = function (k) {',
- ' var Self = this;',
- ' var Result = 0;',
- ' function Sub() {',
- ' $mod.TObject.DoIt.call(Self, true);',
- ' };',
- ' Sub();',
- ' $mod.TObject.DoIt.apply(Self, arguments);',
- ' $mod.TObject.DoIt.call(Self, true);',
- ' return Result;',
- ' };',
- '});',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestModule.TestClass_TObjectFree;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' TObject = class',
- ' Obj: tobject;',
- ' procedure Free;',
- ' end;',
- 'procedure tobject.free;',
- 'begin',
- 'end;',
- 'function DoIt(o: tobject): tobject;',
- 'var l: tobject;',
- 'begin',
- ' o.free;',
- ' o.free();',
- ' l.free;',
- ' l.free();',
- ' o.obj.free;',
- ' o.obj.free();',
- ' with o do obj.free;',
- ' with o do obj.free();',
- ' result.Free;',
- ' result.Free();',
- 'end;',
- 'var o: tobject;',
- ' a: array of tobject;',
- 'begin',
- ' o.free;',
- ' o.obj.free;',
- ' a[1+2].free;',
- '']);
- ConvertProgram;
- CheckSource('TestClass_TObjectFree',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' this.Obj = null;',
- ' };',
- ' this.$final = function () {',
- ' this.Obj = undefined;',
- ' };',
- ' this.Free = function () {',
- ' };',
- '});',
- 'this.DoIt = function (o) {',
- ' var Result = null;',
- ' var l = null;',
- ' o = rtl.freeLoc(o);',
- ' o = rtl.freeLoc(o);',
- ' l = rtl.freeLoc(l);',
- ' l = rtl.freeLoc(l);',
- ' rtl.free(o, "Obj");',
- ' rtl.free(o, "Obj");',
- ' rtl.free(o, "Obj");',
- ' rtl.free(o, "Obj");',
- ' Result = rtl.freeLoc(Result);',
- ' Result = rtl.freeLoc(Result);',
- ' return Result;',
- '};',
- 'this.o = null;',
- 'this.a = [];',
- '']),
- LinesToStr([ // $mod.$main
- 'rtl.free($mod, "o");',
- 'rtl.free($mod.o, "Obj");',
- 'rtl.free($mod.a, 1 + 2);',
- '']));
- end;
- procedure TTestModule.TestClass_TObjectFreeNewInstance;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' TObject = class',
- ' constructor Create;',
- ' procedure Free;',
- ' end;',
- 'constructor TObject.Create; begin end;',
- 'procedure tobject.free; begin end;',
- 'begin',
- ' with tobject.create do free;',
- '']);
- ConvertProgram;
- CheckSource('TestClass_TObjectFreeNewInstance',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.Create = function () {',
- ' };',
- ' this.Free = function () {',
- ' };',
- '});',
- '']),
- LinesToStr([ // $mod.$main
- 'var $with1 = $mod.TObject.$create("Create");',
- '$with1=rtl.freeLoc($with1);',
- '']));
- end;
- procedure TTestModule.TestClass_TObjectFreeLowerCase;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' TObject = class',
- ' destructor Destroy;',
- ' procedure Free;',
- ' end;',
- 'destructor TObject.Destroy; begin end;',
- 'procedure tobject.free; begin end;',
- 'var o: tobject;',
- 'begin',
- ' o.free;',
- '']);
- Converter.UseLowerCase:=true;
- ConvertProgram;
- CheckSource('TestClass_TObjectFreeLowerCase',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "tobject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' rtl.tObjectDestroy = "destroy";',
- ' this.destroy = function () {',
- ' };',
- ' this.free = function () {',
- ' };',
- '});',
- 'this.o = null;',
- '']),
- LinesToStr([ // $mod.$main
- 'rtl.free($mod, "o");',
- '']));
- end;
- procedure TTestModule.TestClass_TObjectFreeFunctionFail;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' TObject = class',
- ' procedure Free;',
- ' function GetObj: tobject; virtual; abstract;',
- ' end;',
- 'procedure tobject.free;',
- 'begin',
- 'end;',
- 'var o: tobject;',
- 'begin',
- ' o.getobj.free;',
- '']);
- SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
- ConvertProgram;
- end;
- procedure TTestModule.TestClass_TObjectFreePropertyFail;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' TObject = class',
- ' procedure Free;',
- ' FObj: TObject;',
- ' property Obj: tobject read FObj write FObj;',
- ' end;',
- 'procedure tobject.free;',
- 'begin',
- 'end;',
- 'var o: tobject;',
- 'begin',
- ' o.obj.free;',
- '']);
- SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
- ConvertProgram;
- end;
- procedure TTestModule.TestClassOf_Create;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' constructor Create;');
- Add(' end;');
- Add(' TClass = class of TObject;');
- Add('constructor tobject.create; begin end;');
- Add('var');
- Add(' Obj: tobject;');
- Add(' C: tclass;');
- Add('begin');
- Add(' obj:=C.create;');
- Add(' with c do obj:=create;');
- ConvertProgram;
- CheckSource('TestClassOf_Create',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.Create = function () {',
- ' };',
- '});',
- 'this.Obj = null;',
- 'this.C = null;'
- ]),
- LinesToStr([ // $mod.$main
- '$mod.Obj = $mod.C.$create("Create");',
- 'var $with1 = $mod.C;',
- '$mod.Obj = $with1.$create("Create");',
- '']));
- end;
- procedure TTestModule.TestClassOf_Call;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' class procedure DoIt;');
- Add(' end;');
- Add(' TClass = class of TObject;');
- Add('class procedure tobject.doit; begin end;');
- Add('var');
- Add(' C: tclass;');
- Add('begin');
- Add(' c.doit;');
- Add(' with c do doit;');
- ConvertProgram;
- CheckSource('TestClassOf_Call',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.DoIt = function () {',
- ' };',
- '});',
- 'this.C = null;'
- ]),
- LinesToStr([ // $mod.$main
- '$mod.C.DoIt();',
- 'var $with1 = $mod.C;',
- '$with1.DoIt();',
- '']));
- end;
- procedure TTestModule.TestClassOf_Assign;
- begin
- StartProgram(false);
- Add('type');
- Add(' TClass = class of TObject;');
- Add(' TObject = class');
- Add(' ClassType: TClass; ');
- Add(' end;');
- Add('var');
- Add(' Obj: tobject;');
- Add(' C: tclass;');
- Add('begin');
- Add(' c:=nil;');
- Add(' c:=obj.classtype;');
- ConvertProgram;
- CheckSource('TestClassOf_Assign',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' this.ClassType = null;',
- ' };',
- ' this.$final = function () {',
- ' this.ClassType = undefined;',
- ' };',
- '});',
- 'this.Obj = null;',
- 'this.C = null;'
- ]),
- LinesToStr([ // $mod.$main
- '$mod.C = null;',
- '$mod.C = $mod.Obj.ClassType;',
- '']));
- end;
- procedure TTestModule.TestClassOf_Is;
- begin
- StartProgram(false);
- Add('type');
- Add(' TClass = class of TObject;');
- Add(' TObject = class');
- Add(' end;');
- Add(' TCar = class');
- Add(' end;');
- Add(' TCars = class of TCar;');
- Add('var');
- Add(' Obj: tobject;');
- Add(' C: tclass;');
- Add(' Cars: tcars;');
- Add('begin');
- Add(' if c is tcar then ;');
- Add(' if c is tcars then ;');
- ConvertProgram;
- CheckSource('TestClassOf_Is',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
- '});',
- 'this.Obj = null;',
- 'this.C = null;',
- 'this.Cars = null;'
- ]),
- LinesToStr([ // $mod.$main
- 'if(rtl.is($mod.C,$mod.TCar));',
- 'if(rtl.is($mod.C,$mod.TCar));',
- '']));
- end;
- procedure TTestModule.TestClassOf_Compare;
- begin
- StartProgram(false);
- Add('type');
- Add(' TClass = class of TObject;');
- Add(' TObject = class');
- Add(' ClassType: TClass; ');
- Add(' end;');
- Add('var');
- Add(' b: boolean;');
- Add(' Obj: tobject;');
- Add(' C: tclass;');
- Add('begin');
- Add(' b:=c=nil;');
- Add(' b:=nil=c;');
- Add(' b:=c=obj.classtype;');
- Add(' b:=obj.classtype=c;');
- Add(' b:=c=TObject;');
- Add(' b:=TObject=c;');
- Add(' b:=c<>nil;');
- Add(' b:=nil<>c;');
- Add(' b:=c<>obj.classtype;');
- Add(' b:=obj.classtype<>c;');
- Add(' b:=c<>TObject;');
- Add(' b:=TObject<>c;');
- ConvertProgram;
- CheckSource('TestClassOf_Compare',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' this.ClassType = null;',
- ' };',
- ' this.$final = function () {',
- ' this.ClassType = undefined;',
- ' };',
- '});',
- 'this.b = false;',
- 'this.Obj = null;',
- 'this.C = null;'
- ]),
- LinesToStr([ // $mod.$main
- '$mod.b = $mod.C == null;',
- '$mod.b = null == $mod.C;',
- '$mod.b = $mod.C == $mod.Obj.ClassType;',
- '$mod.b = $mod.Obj.ClassType == $mod.C;',
- '$mod.b = $mod.C == $mod.TObject;',
- '$mod.b = $mod.TObject == $mod.C;',
- '$mod.b = $mod.C != null;',
- '$mod.b = null != $mod.C;',
- '$mod.b = $mod.C != $mod.Obj.ClassType;',
- '$mod.b = $mod.Obj.ClassType != $mod.C;',
- '$mod.b = $mod.C != $mod.TObject;',
- '$mod.b = $mod.TObject != $mod.C;',
- '']));
- end;
- procedure TTestModule.TestClassOf_ClassVar;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' class var id: longint;');
- Add(' end;');
- Add(' TClass = class of TObject;');
- Add('var');
- Add(' C: tclass;');
- Add('begin');
- Add(' C.id:=C.id;');
- ConvertProgram;
- CheckSource('TestClassOf_ClassVar',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.id = 0;',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'this.C = null;'
- ]),
- LinesToStr([ // $mod.$main
- '$mod.C.id = $mod.C.id;',
- '']));
- end;
- procedure TTestModule.TestClassOf_ClassMethod;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' class function DoIt(i: longint = 0): longint;');
- Add(' end;');
- Add(' TClass = class of TObject;');
- Add('class function tobject.doit(i: longint = 0): longint; begin end;');
- Add('var');
- Add(' i: longint;');
- Add(' C: tclass;');
- Add('begin');
- Add(' C.DoIt;');
- Add(' C.DoIt();');
- Add(' i:=C.DoIt;');
- Add(' i:=C.DoIt();');
- ConvertProgram;
- CheckSource('TestClassOf_ClassMethod',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.DoIt = function (i) {',
- ' var Result = 0;',
- ' return Result;',
- ' };',
- '});',
- 'this.i = 0;',
- 'this.C = null;'
- ]),
- LinesToStr([ // $mod.$main
- '$mod.C.DoIt(0);',
- '$mod.C.DoIt(0);',
- '$mod.i = $mod.C.DoIt(0);',
- '$mod.i = $mod.C.DoIt(0);',
- '']));
- end;
- procedure TTestModule.TestClassOf_ClassProperty;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' class var FA: longint;');
- Add(' class function GetA: longint;');
- Add(' class procedure SetA(Value: longint);');
- Add(' class property pA: longint read fa write fa;');
- Add(' class property pB: longint read geta write seta;');
- Add(' end;');
- Add(' TObjectClass = class of tobject;');
- Add('class function tobject.geta: longint; begin end;');
- Add('class procedure tobject.seta(value: longint); begin end;');
- Add('var');
- Add(' b: boolean;');
- Add(' Obj: tobject;');
- Add(' Cla: tobjectclass;');
- Add('begin');
- Add(' obj.pa:=obj.pa;');
- Add(' obj.pb:=obj.pb;');
- Add(' b:=obj.pa=4;');
- Add(' b:=obj.pb=obj.pb;');
- Add(' b:=5=obj.pa;');
- Add(' cla.pa:=6;');
- Add(' cla.pa:=cla.pa;');
- Add(' cla.pb:=cla.pb;');
- Add(' b:=cla.pa=7;');
- Add(' b:=cla.pb=cla.pb;');
- Add(' b:=8=cla.pa;');
- Add(' tobject.pa:=9;');
- Add(' tobject.pb:=tobject.pb;');
- Add(' b:=tobject.pa=10;');
- Add(' b:=11=tobject.pa;');
- ConvertProgram;
- CheckSource('TestClassOf_ClassProperty',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.FA = 0;',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.GetA = function () {',
- ' var Result = 0;',
- ' return Result;',
- ' };',
- ' this.SetA = function (Value) {',
- ' };',
- '});',
- 'this.b = false;',
- 'this.Obj = null;',
- 'this.Cla = null;'
- ]),
- LinesToStr([ // $mod.$main
- '$mod.Obj.$class.FA = $mod.Obj.FA;',
- '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
- '$mod.b = $mod.Obj.FA == 4;',
- '$mod.b = $mod.Obj.$class.GetA() == $mod.Obj.$class.GetA();',
- '$mod.b = 5 == $mod.Obj.FA;',
- '$mod.Cla.FA = 6;',
- '$mod.Cla.FA = $mod.Cla.FA;',
- '$mod.Cla.SetA($mod.Cla.GetA());',
- '$mod.b = $mod.Cla.FA == 7;',
- '$mod.b = $mod.Cla.GetA() == $mod.Cla.GetA();',
- '$mod.b = 8 == $mod.Cla.FA;',
- '$mod.TObject.FA = 9;',
- '$mod.TObject.SetA($mod.TObject.GetA());',
- '$mod.b = $mod.TObject.FA == 10;',
- '$mod.b = 11 == $mod.TObject.FA;',
- '']));
- end;
- procedure TTestModule.TestClassOf_ClassMethodSelf;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' class var GlobalId: longint;');
- Add(' class procedure ProcA;');
- Add(' end;');
- Add('class procedure tobject.proca;');
- Add('var b: boolean;');
- Add('begin');
- Add(' b:=self=nil;');
- Add(' b:=self.globalid=3;');
- Add(' b:=4=self.globalid;');
- Add(' self.globalid:=5;');
- Add(' self.proca;');
- Add('end;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestClassOf_ClassMethodSelf',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.GlobalId = 0;',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.ProcA = function () {',
- ' var b = false;',
- ' b = this == null;',
- ' b = this.GlobalId == 3;',
- ' b = 4 == this.GlobalId;',
- ' this.GlobalId = 5;',
- ' this.ProcA();',
- ' };',
- '});'
- ]),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestModule.TestClassOf_TypeCast;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' class procedure {#TObject_DoIt}DoIt;');
- Add(' end;');
- Add(' TClass = class of TObject;');
- Add(' TMobile = class');
- Add(' class procedure {#TMobile_DoIt}DoIt;');
- Add(' end;');
- Add(' TMobileClass = class of TMobile;');
- Add(' TCar = class(TMobile)');
- Add(' class procedure {#TCar_DoIt}DoIt;');
- Add(' end;');
- Add(' TCarClass = class of TCar;');
- Add('class procedure TObject.DoIt;');
- Add('begin');
- Add(' TClass(Self).{@TObject_DoIt}DoIt;');
- Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
- Add('end;');
- Add('class procedure TMobile.DoIt;');
- Add('begin');
- Add(' TClass(Self).{@TObject_DoIt}DoIt;');
- Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
- Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
- Add('end;');
- Add('class procedure TCar.DoIt; begin end;');
- Add('var');
- Add(' ObjC: TClass;');
- Add(' MobileC: TMobileClass;');
- Add(' CarC: TCarClass;');
- Add('begin');
- Add(' ObjC.{@TObject_DoIt}DoIt;');
- Add(' MobileC.{@TMobile_DoIt}DoIt;');
- Add(' CarC.{@TCar_DoIt}DoIt;');
- Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
- Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
- Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
- Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
- Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
- Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
- Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
- Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
- Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
- ConvertProgram;
- CheckSource('TestClassOf_TypeCast',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.DoIt = function () {',
- ' this.DoIt();',
- ' this.DoIt$1();',
- ' };',
- '});',
- 'rtl.createClass($mod, "TMobile", $mod.TObject, function () {',
- ' this.DoIt$1 = function () {',
- ' this.DoIt();',
- ' this.DoIt$1();',
- ' this.DoIt$2();',
- ' };',
- '});',
- 'rtl.createClass($mod, "TCar", $mod.TMobile, function () {',
- ' this.DoIt$2 = function () {',
- ' };',
- '});',
- 'this.ObjC = null;',
- 'this.MobileC = null;',
- 'this.CarC = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.ObjC.DoIt();',
- '$mod.MobileC.DoIt$1();',
- '$mod.CarC.DoIt$2();',
- '$mod.ObjC.DoIt();',
- '$mod.ObjC.DoIt$1();',
- '$mod.ObjC.DoIt$2();',
- '$mod.MobileC.DoIt();',
- '$mod.MobileC.DoIt$1();',
- '$mod.MobileC.DoIt$2();',
- '$mod.CarC.DoIt();',
- '$mod.CarC.DoIt$1();',
- '$mod.CarC.DoIt$2();',
- '']));
- end;
- procedure TTestModule.TestClassOf_ImplicitFunctionCall;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' function CurNow: longint; ');
- Add(' class function Now: longint; ');
- Add(' end;');
- Add('function TObject.CurNow: longint; begin end;');
- Add('class function TObject.Now: longint; begin end;');
- Add('var');
- Add(' Obj: tobject;');
- Add(' vI: longint;');
- Add('begin');
- Add(' obj.curnow;');
- Add(' vi:=obj.curnow;');
- Add(' tobject.now;');
- Add(' vi:=tobject.now;');
- ConvertProgram;
- CheckSource('TestClassOf_ImplicitFunctionCall',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.CurNow = function () {',
- ' var Result = 0;',
- ' return Result;',
- ' };',
- ' this.Now = function () {',
- ' var Result = 0;',
- ' return Result;',
- ' };',
- '});',
- 'this.Obj = null;',
- 'this.vI = 0;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.Obj.CurNow();',
- '$mod.vI = $mod.Obj.CurNow();',
- '$mod.TObject.Now();',
- '$mod.vI = $mod.TObject.Now();',
- '']));
- end;
- procedure TTestModule.TestNestedClass_Fail;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' TObject = class',
- ' type TNested = longint;',
- ' end;',
- 'begin']);
- SetExpectedPasResolverError('not yet implemented: TNested:TPasAliasType [20170608232534] nested types',
- nNotYetImplemented);
- ConvertProgram;
- end;
- procedure TTestModule.TestExternalClass_Var;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TExtA = class external name ''ExtObj''');
- Add(' Id: longint external name ''$Id'';');
- Add(' B: longint;');
- Add(' end;');
- Add('var Obj: TExtA;');
- Add('begin');
- Add(' obj.id:=obj.id+1;');
- Add(' obj.B:=obj.B+1;');
- ConvertProgram;
- CheckSource('TestExternalClass_Var',
- LinesToStr([ // statements
- 'this.Obj = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
- '$mod.Obj.B = $mod.Obj.B + 1;',
- '']));
- end;
- procedure TTestModule.TestExternalClass_Dollar;
- begin
- StartProgram(false);
- Add([
- '{$modeswitch externalclass}',
- 'type',
- ' TExtA = class external name ''$''',
- ' Id: longint external name ''$'';',
- ' function Bla(i: longint): longint; external name ''$'';',
- ' end;',
- 'function dollar(k: longint): longint; external name ''$'';',
- 'var Obj: TExtA;',
- 'begin',
- ' dollar(1);',
- ' obj.id:=obj.id+2;',
- ' obj.Bla(3);',
- '']);
- ConvertProgram;
- CheckSource('TestExternalClass_Dollar',
- LinesToStr([ // statements
- 'this.Obj = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$(1);',
- '$mod.Obj.$ = $mod.Obj.$ + 2;',
- '$mod.Obj.$(3);',
- '']));
- end;
- procedure TTestModule.TestExternalClass_DuplicateVarFail;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TExtA = class external name ''ExtA''');
- Add(' Id: longint external name ''$Id'';');
- Add(' end;');
- Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
- Add(' Id: longint;');
- Add(' end;');
- Add('begin');
- SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,6)',nDuplicateIdentifier);
- ConvertProgram;
- end;
- procedure TTestModule.TestExternalClass_Method;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TExtA = class external name ''ExtObj''');
- Add(' procedure DoIt(Id: longint = 1); external name ''$Execute'';');
- Add(' procedure DoSome(Id: longint = 1);');
- Add(' end;');
- Add('var Obj: texta;');
- Add('begin');
- Add(' obj.doit;');
- Add(' obj.doit();');
- Add(' obj.doit(2);');
- Add(' with obj do begin');
- Add(' doit;');
- Add(' doit();');
- Add(' doit(3);');
- Add(' end;');
- ConvertProgram;
- CheckSource('TestExternalClass_Method',
- LinesToStr([ // statements
- 'this.Obj = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.Obj.$Execute(1);',
- '$mod.Obj.$Execute(1);',
- '$mod.Obj.$Execute(2);',
- 'var $with1 = $mod.Obj;',
- '$with1.$Execute(1);',
- '$with1.$Execute(1);',
- '$with1.$Execute(3);',
- '']));
- end;
- procedure TTestModule.TestExternalClass_NonExternalOverride;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TExtA = class external name ''ExtObjA''');
- Add(' procedure ProcA; virtual;');
- Add(' procedure ProcB; virtual;');
- Add(' end;');
- Add(' TExtB = class external name ''ExtObjB'' (TExtA)');
- Add(' end;');
- Add(' TExtC = class (TExtB)');
- Add(' procedure ProcA; override;');
- Add(' end;');
- Add('procedure TExtC.ProcA;');
- Add('begin');
- Add(' ProcA;');
- Add(' Self.ProcA;');
- Add(' ProcB;');
- Add(' Self.ProcB;');
- Add('end;');
- Add('var');
- Add(' A: texta;');
- Add(' B: textb;');
- Add(' C: textc;');
- Add('begin');
- Add(' a.proca;');
- Add(' b.proca;');
- Add(' c.proca;');
- ConvertProgram;
- CheckSource('TestExternalClass_NonExternalOverride',
- LinesToStr([ // statements
- 'rtl.createClassExt($mod, "TExtC", ExtObjB, "", function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.ProcA = function () {',
- ' this.ProcA();',
- ' this.ProcA();',
- ' this.ProcB();',
- ' this.ProcB();',
- ' };',
- '});',
- 'this.A = null;',
- 'this.B = null;',
- 'this.C = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.A.ProcA();',
- '$mod.B.ProcA();',
- '$mod.C.ProcA();',
- '']));
- end;
- procedure TTestModule.TestExternalClass_Property;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TExtA = class external name ''ExtA''');
- Add(' function getYear: longint;');
- Add(' procedure setYear(Value: longint);');
- Add(' property Year: longint read getyear write setyear;');
- Add(' end;');
- Add(' TExtB = class (TExtA)');
- Add(' procedure OtherSetYear(Value: longint);');
- Add(' property year write othersetyear;');
- Add(' end;');
- Add('procedure textb.othersetyear(value: longint);');
- Add('begin');
- Add(' setYear(Value+4);');
- Add('end;');
- Add('var');
- Add(' A: texta;');
- Add(' B: textb;');
- Add('begin');
- Add(' a.year:=a.year+1;');
- Add(' b.year:=b.year+2;');
- ConvertProgram;
- CheckSource('TestExternalClass_NonExternalOverride',
- LinesToStr([ // statements
- 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.OtherSetYear = function (Value) {',
- ' this.setYear(Value+4);',
- ' };',
- '});',
- 'this.A = null;',
- 'this.B = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.A.setYear($mod.A.getYear()+1);',
- '$mod.B.OtherSetYear($mod.B.getYear()+2);',
- '']));
- end;
- procedure TTestModule.TestExternalClass_ClassProperty;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TExtA = class external name ''ExtA''');
- Add(' class function getYear: longint;');
- Add(' class procedure setYear(Value: longint);');
- Add(' class property Year: longint read getyear write setyear;');
- Add(' end;');
- Add(' TExtB = class (TExtA)');
- Add(' class function GetCentury: longint;');
- Add(' class procedure SetCentury(Value: longint);');
- Add(' class property Century: longint read getcentury write setcentury;');
- Add(' end;');
- Add('class function textb.getcentury: longint;');
- Add('begin');
- Add('end;');
- Add('class procedure textb.setcentury(value: longint);');
- Add('begin');
- Add(' setyear(value+11);');
- Add(' texta.year:=texta.year+12;');
- Add(' year:=year+13;');
- Add(' textb.century:=textb.century+14;');
- Add(' century:=century+15;');
- Add('end;');
- Add('var');
- Add(' A: texta;');
- Add(' B: textb;');
- Add('begin');
- Add(' texta.year:=texta.year+1;');
- Add(' textb.year:=textb.year+2;');
- Add(' TextA.year:=TextA.year+3;');
- Add(' b.year:=b.year+4;');
- Add(' textb.century:=textb.century+5;');
- Add(' b.century:=b.century+6;');
- ConvertProgram;
- CheckSource('TestExternalClass_ClassProperty',
- LinesToStr([ // statements
- 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.GetCentury = function () {',
- ' var Result = 0;',
- ' return Result;',
- ' };',
- ' this.SetCentury = function (Value) {',
- ' this.setYear(Value + 11);',
- ' ExtA.setYear(ExtA.getYear() + 12);',
- ' this.setYear(this.getYear() + 13);',
- ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
- ' this.SetCentury(this.GetCentury() + 15);',
- ' };',
- '});',
- 'this.A = null;',
- 'this.B = null;',
- '']),
- LinesToStr([ // $mod.$main
- 'ExtA.setYear(ExtA.getYear() + 1);',
- '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
- 'ExtA.setYear(ExtA.getYear() + 3);',
- '$mod.B.setYear($mod.B.getYear() + 4);',
- '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
- '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
- '']));
- end;
- procedure TTestModule.TestExternalClass_ClassOf;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TExtA = class external name ''ExtA''');
- Add(' procedure ProcA; virtual;');
- Add(' procedure ProcB; virtual;');
- Add(' end;');
- Add(' TExtAClass = class of TExtA;');
- Add(' TExtB = class external name ''ExtB'' (TExtA)');
- Add(' end;');
- Add(' TExtBClass = class of TExtB;');
- Add(' TExtC = class (TExtB)');
- Add(' procedure ProcA; override;');
- Add(' end;');
- Add(' TExtCClass = class of TExtC;');
- Add('procedure TExtC.ProcA; begin end;');
- Add('var');
- Add(' A: texta; ClA: TExtAClass;');
- Add(' B: textb; ClB: TExtBClass;');
- Add(' C: textc; ClC: TExtCClass;');
- Add('begin');
- Add(' ClA:=texta;');
- Add(' ClA:=textb;');
- Add(' ClA:=textc;');
- Add(' ClB:=textb;');
- Add(' ClB:=textc;');
- Add(' ClC:=textc;');
- ConvertProgram;
- CheckSource('TestExternalClass_ClassOf',
- LinesToStr([ // statements
- 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.ProcA = function () {',
- ' };',
- '});',
- 'this.A = null;',
- 'this.ClA = null;',
- 'this.B = null;',
- 'this.ClB = null;',
- 'this.C = null;',
- 'this.ClC = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.ClA = ExtA;',
- '$mod.ClA = ExtB;',
- '$mod.ClA = $mod.TExtC;',
- '$mod.ClB = ExtB;',
- '$mod.ClB = $mod.TExtC;',
- '$mod.ClC = $mod.TExtC;',
- '']));
- end;
- procedure TTestModule.TestExternalClass_ClassOtherUnit;
- begin
- AddModuleWithIntfImplSrc('unit2.pas',
- LinesToStr([
- '{$modeswitch externalclass}',
- 'type',
- ' TExtA = class external name ''ExtA''',
- ' class var Id: longint;',
- ' end;',
- '']),
- '');
- StartUnit(true);
- Add('interface');
- Add('uses unit2;');
- Add('implementation');
- Add('begin');
- Add(' unit2.texta.id:=unit2.texta.id+1;');
- ConvertUnit;
- CheckSource('TestExternalClass_ClassOtherUnit',
- LinesToStr([
- '']),
- LinesToStr([
- 'ExtA.Id = ExtA.Id + 1;',
- '']));
- end;
- procedure TTestModule.TestExternalClass_Is;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TExtA = class external name ''ExtA''');
- Add(' end;');
- Add(' TExtAClass = class of TExtA;');
- Add(' TExtB = class external name ''ExtB'' (TExtA)');
- Add(' end;');
- Add(' TExtBClass = class of TExtB;');
- Add(' TExtC = class (TExtB)');
- Add(' end;');
- Add(' TExtCClass = class of TExtC;');
- Add('var');
- Add(' A: texta; ClA: TExtAClass;');
- Add(' B: textb; ClB: TExtBClass;');
- Add(' C: textc; ClC: TExtCClass;');
- Add('begin');
- Add(' if a is textb then ;');
- Add(' if a is textc then ;');
- Add(' if b is textc then ;');
- Add(' if cla is textb then ;');
- Add(' if cla is textc then ;');
- Add(' if clb is textc then ;');
- ConvertProgram;
- CheckSource('TestExternalClass_Is',
- LinesToStr([ // statements
- 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'this.A = null;',
- 'this.ClA = null;',
- 'this.B = null;',
- 'this.ClB = null;',
- 'this.C = null;',
- 'this.ClC = null;',
- '']),
- LinesToStr([ // $mod.$main
- 'if (rtl.isExt($mod.A, ExtB)) ;',
- 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
- 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
- 'if (rtl.isExt($mod.ClA, ExtB)) ;',
- 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
- 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
- '']));
- end;
- procedure TTestModule.TestExternalClass_As;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TExtA = class external name ''ExtA''');
- Add(' end;');
- Add(' TExtB = class external name ''ExtB'' (TExtA)');
- Add(' end;');
- Add(' TExtC = class (TExtB)');
- Add(' end;');
- Add('var');
- Add(' A: texta;');
- Add(' B: textb;');
- Add(' C: textc;');
- Add('begin');
- Add(' b:=a as textb;');
- Add(' c:=a as textc;');
- Add(' c:=b as textc;');
- ConvertProgram;
- CheckSource('TestExternalClass_Is',
- LinesToStr([ // statements
- 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'this.A = null;',
- 'this.B = null;',
- 'this.C = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.B = rtl.asExt($mod.A, ExtB);',
- '$mod.C = rtl.as($mod.A, $mod.TExtC);',
- '$mod.C = rtl.as($mod.B, $mod.TExtC);',
- '']));
- end;
- procedure TTestModule.TestExternalClass_DestructorFail;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TExtA = class external name ''ExtA''');
- Add(' destructor Free;');
- Add(' end;');
- SetExpectedPasResolverError('Pascal element not supported: destructor',
- nPasElementNotSupported);
- ConvertProgram;
- end;
- procedure TTestModule.TestExternalClass_New;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TExtA = class external name ''ExtA''');
- Add(' constructor New;');
- Add(' constructor New(i: longint; j: longint = 2);');
- Add(' end;');
- Add('var');
- Add(' A: texta;');
- Add('begin');
- Add(' a:=texta.new;');
- Add(' a:=texta.new();');
- Add(' a:=texta.new(1);');
- Add(' with texta do begin');
- Add(' a:=new;');
- Add(' a:=new();');
- Add(' a:=new(2);');
- Add(' end;');
- Add(' a:=test1.texta.new;');
- Add(' a:=test1.texta.new();');
- Add(' a:=test1.texta.new(3);');
- ConvertProgram;
- CheckSource('TestExternalClass_New',
- LinesToStr([ // statements
- 'this.A = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.A = new ExtA();',
- '$mod.A = new ExtA();',
- '$mod.A = new ExtA(1,2);',
- '$mod.A = new ExtA();',
- '$mod.A = new ExtA();',
- '$mod.A = new ExtA(2,2);',
- '$mod.A = new ExtA();',
- '$mod.A = new ExtA();',
- '$mod.A = new ExtA(3,2);',
- '']));
- end;
- procedure TTestModule.TestExternalClass_ClassOf_New;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TExtAClass = class of TExtA;');
- Add(' TExtA = class external name ''ExtA''');
- Add(' constructor New;');
- Add(' end;');
- Add('var');
- Add(' A: texta;');
- Add(' C: textaclass;');
- Add('begin');
- Add(' a:=c.new;');
- Add(' a:=c.new();');
- Add(' with C do begin');
- Add(' a:=new;');
- Add(' a:=new();');
- Add(' end;');
- Add(' a:=test1.c.new;');
- Add(' a:=test1.c.new();');
- ConvertProgram;
- CheckSource('TestExternalClass_ClassOf_New',
- LinesToStr([ // statements
- 'this.A = null;',
- 'this.C = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.A = new $mod.C();',
- '$mod.A = new $mod.C();',
- 'var $with1 = $mod.C;',
- '$mod.A = new $with1();',
- '$mod.A = new $with1();',
- '$mod.A = new $mod.C();',
- '$mod.A = new $mod.C();',
- '']));
- end;
- procedure TTestModule.TestExternalClass_FuncClassOf_New;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TExtAClass = class of TExtA;');
- Add(' TExtA = class external name ''ExtA''');
- Add(' constructor New;');
- Add(' end;');
- Add('function GetCreator: TExtAClass;');
- Add('begin');
- Add(' Result:=TExtA;');
- Add('end;');
- Add('var');
- Add(' A: texta;');
- Add('begin');
- Add(' a:=getcreator.new;');
- Add(' a:=getcreator().new;');
- Add(' a:=getcreator().new();');
- Add(' a:=getcreator.new();');
- Add(' with getcreator do begin');
- Add(' a:=new;');
- Add(' a:=new();');
- Add(' end;');
- ConvertProgram;
- CheckSource('TestExternalClass_FuncClassOf_New',
- LinesToStr([ // statements
- 'this.GetCreator = function () {',
- ' var Result = null;',
- ' Result = ExtA;',
- ' return Result;',
- '};',
- 'this.A = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.A = new ($mod.GetCreator())();',
- '$mod.A = new ($mod.GetCreator())();',
- '$mod.A = new ($mod.GetCreator())();',
- '$mod.A = new ($mod.GetCreator())();',
- 'var $with1 = $mod.GetCreator();',
- '$mod.A = new $with1();',
- '$mod.A = new $with1();',
- '']));
- end;
- procedure TTestModule.TestExternalClass_LocalConstSameName;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TExtA = class external name ''ExtA''');
- Add(' constructor New;');
- Add(' end;');
- Add('function DoIt: longint;');
- Add('const ExtA = 3;');
- Add('begin');
- Add(' Result:=ExtA;');
- Add('end;');
- Add('var');
- Add(' A: texta;');
- Add('begin');
- Add(' a:=texta.new;');
- ConvertProgram;
- CheckSource('TestExternalClass_LocalConstSameName',
- LinesToStr([ // statements
- 'var ExtA$1 = 3;',
- 'this.DoIt = function () {',
- ' var Result = 0;',
- ' Result = ExtA$1;',
- ' return Result;',
- '};',
- 'this.A = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.A = new ExtA();',
- '']));
- end;
- procedure TTestModule.TestExternalClass_ReintroduceOverload;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TExtA = class external name ''ExtA''');
- Add(' procedure DoIt;');
- Add(' end;');
- Add(' TMyA = class(TExtA)');
- Add(' procedure DoIt;');
- Add(' end;');
- Add('procedure TMyA.DoIt; begin end;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestExternalClass_ReintroduceOverload',
- LinesToStr([ // statements
- 'rtl.createClassExt($mod, "TMyA", ExtA, "", function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.DoIt$1 = function () {',
- ' };',
- '});',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestModule.TestExternalClass_Inherited;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TExtA = class external name ''ExtA''');
- Add(' procedure DoIt(i: longint = 1); virtual;');
- Add(' procedure DoSome(j: longint = 2);');
- Add(' end;');
- Add(' TExtB = class external name ''ExtB''(TExtA)');
- Add(' end;');
- Add(' TMyC = class(TExtB)');
- Add(' procedure DoIt(i: longint = 1); override;');
- Add(' procedure DoSome(j: longint = 2); reintroduce;');
- Add(' end;');
- Add('procedure TMyC.DoIt(i: longint);');
- Add('begin');
- Add(' inherited;');
- Add(' inherited DoIt;');
- Add(' inherited DoIt();');
- Add(' inherited DoIt(3);');
- Add(' inherited DoSome;');
- Add(' inherited DoSome();');
- Add(' inherited DoSome(4);');
- Add('end;');
- Add('procedure TMyC.DoSome(j: longint);');
- Add('begin');
- Add(' inherited;');
- Add('end;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestExternalClass_ReintroduceOverload',
- LinesToStr([ // statements
- 'rtl.createClassExt($mod, "TMyC", ExtB, "", function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.DoIt = function (i) {',
- ' ExtB.DoIt.apply(this, arguments);',
- ' ExtB.DoIt.call(this, 1);',
- ' ExtB.DoIt.call(this, 1);',
- ' ExtB.DoIt.call(this, 3);',
- ' ExtB.DoSome.call(this, 2);',
- ' ExtB.DoSome.call(this, 2);',
- ' ExtB.DoSome.call(this, 4);',
- ' };',
- ' this.DoSome$1 = function (j) {',
- ' ExtB.DoSome.apply(this, arguments);',
- ' };',
- '});',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestModule.TestExternalClass_PascalAncestorFail;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TObject = class');
- Add(' end;');
- Add(' TExtA = class external name ''ExtA''(TObject)');
- Add(' end;');
- Add('begin');
- SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
- ConvertProgram;
- end;
- procedure TTestModule.TestExternalClass_NewInstance;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TExtA = class external name ''ExtA''');
- Add(' end;');
- Add(' TMyB = class(TExtA)');
- Add(' protected');
- Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
- Add(' end;');
- Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
- Add('begin end;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestExternalClass_NewInstance',
- LinesToStr([ // statements
- 'rtl.createClassExt($mod, "TMyB", ExtA, "NewInstance", function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.NewInstance = function (fnname, paramarray) {',
- ' var Result = null;',
- ' return Result;',
- ' };',
- '});',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TExtA = class external name ''ExtA''');
- Add(' end;');
- Add(' TMyB = class(TExtA)');
- Add(' protected');
- Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
- Add(' end;');
- Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
- Add('begin end;');
- Add('begin');
- SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
- ConvertProgram;
- end;
- procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TExtA = class external name ''ExtA''');
- Add(' end;');
- Add(' TMyB = class(TExtA)');
- Add(' protected');
- Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
- Add(' end;');
- Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
- Add('begin end;');
- Add('begin');
- SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Longint", expected "String"',
- nIncompatibleTypeArgNo);
- ConvertProgram;
- end;
- procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TExtA = class external name ''ExtA''');
- Add(' end;');
- Add(' TMyB = class(TExtA)');
- Add(' protected');
- Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
- Add(' end;');
- Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
- Add('begin end;');
- Add('begin');
- SetExpectedPasResolverError('Incompatible type arg no. 2: Got "type", expected "untyped"',
- nIncompatibleTypeArgNo);
- ConvertProgram;
- end;
- procedure TTestModule.TestExternalClass_PascalProperty;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TJSElement = class;');
- Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
- Add(' TJSElement = class external name ''ExtA''');
- Add(' end;');
- Add(' TControl = class(TJSElement)');
- Add(' private');
- Add(' FOnClick: TJSNotifyEvent;');
- Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
- Add(' procedure Click(Sender: TJSElement);');
- Add(' end;');
- Add('procedure TControl.Click(Sender: TJSElement);');
- Add('begin');
- Add(' OnClick(Self);');
- Add('end;');
- Add('var');
- Add(' Ctrl: TControl;');
- Add('begin');
- Add(' Ctrl.OnClick:[email protected];');
- Add(' Ctrl.OnClick(Ctrl);');
- ConvertProgram;
- CheckSource('TestExternalClass_PascalProperty',
- LinesToStr([ // statements
- 'rtl.createClassExt($mod, "TControl", ExtA, "", function () {',
- ' this.$init = function () {',
- ' this.FOnClick = null;',
- ' };',
- ' this.$final = function () {',
- ' this.FOnClick = undefined;',
- ' };',
- ' this.Click = function (Sender) {',
- ' this.FOnClick(this);',
- ' };',
- '});',
- 'this.Ctrl = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
- '$mod.Ctrl.FOnClick($mod.Ctrl);',
- '']));
- end;
- procedure TTestModule.TestExternalClass_TypeCastToRootClass;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TObject = class');
- Add(' end;');
- Add(' TChild = class');
- Add(' end;');
- Add(' TExtRootA = class external name ''ExtRootA''');
- Add(' end;');
- Add(' TExtChildA = class external name ''ExtChildA''(TExtRootA)');
- Add(' end;');
- Add(' TExtRootB = class external name ''ExtRootB''');
- Add(' end;');
- Add(' TExtChildB = class external name ''ExtChildB''(TExtRootB)');
- Add(' end;');
- Add('var');
- Add(' Obj: TObject;');
- Add(' Child: TChild;');
- Add(' RootA: TExtRootA;');
- Add(' ChildA: TExtChildA;');
- Add(' RootB: TExtRootB;');
- Add(' ChildB: TExtChildB;');
- Add('begin');
- Add(' obj:=tobject(roota);');
- Add(' obj:=tobject(childa);');
- Add(' child:=tchild(tobject(roota));');
- Add(' roota:=textroota(obj);');
- Add(' roota:=textroota(child);');
- Add(' roota:=textroota(rootb);');
- Add(' roota:=textroota(childb);');
- Add(' childa:=textchilda(textroota(obj));');
- ConvertProgram;
- CheckSource('TestExternalClass_TypeCastToRootClass',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'rtl.createClass($mod, "TChild", $mod.TObject, function () {',
- '});',
- 'this.Obj = null;',
- 'this.Child = null;',
- 'this.RootA = null;',
- 'this.ChildA = null;',
- 'this.RootB = null;',
- 'this.ChildB = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.Obj = $mod.RootA;',
- '$mod.Obj = $mod.ChildA;',
- '$mod.Child = $mod.RootA;',
- '$mod.RootA = $mod.Obj;',
- '$mod.RootA = $mod.Child;',
- '$mod.RootA = $mod.RootB;',
- '$mod.RootA = $mod.ChildB;',
- '$mod.ChildA = $mod.Obj;',
- '']));
- end;
- procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TJSString = class external name ''String''');
- Add(' class function fromCharCode() : string; varargs;');
- Add(' function anchor(const aName : string) : string;');
- Add(' end;');
- Add('var');
- Add(' s: string;');
- Add('begin');
- Add(' s:=TJSString.fromCharCode(65,66);');
- Add(' s:=TJSString(s).anchor(s);');
- Add(' s:=TJSString(''foo'').anchor(s);');
- ConvertProgram;
- CheckSource('TestExternalClass_TypeCastStringToExternalString',
- LinesToStr([ // statements
- 'this.s = "";',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.s = String.fromCharCode(65, 66);',
- '$mod.s = $mod.s.anchor($mod.s);',
- '$mod.s = "foo".anchor($mod.s);',
- '']));
- end;
- procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TJSString = class external name ''String''');
- Add(' class function fromCharCode() : string; varargs;');
- Add(' end;');
- Add('var');
- Add(' s: string;');
- Add(' sObj: TJSString;');
- Add('begin');
- Add(' s:=sObj.fromCharCode(65,66);');
- SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
- nExternalClassInstanceCannotAccessStaticX);
- ConvertProgram;
- end;
- procedure TTestModule.TestExternalClass_BracketAccessor;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TJSArray = class external name ''Array2''');
- Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
- Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
- Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
- Add(' end;');
- Add('procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);');
- Add('begin end;');
- Add('var');
- Add(' Arr: tjsarray;');
- Add(' s: string;');
- Add(' i: longint;');
- Add(' v: jsvalue;');
- Add('begin');
- Add(' v:=arr[0];');
- Add(' v:=arr.items[1];');
- Add(' arr[2]:=s;');
- Add(' arr.items[3]:=s;');
- Add(' arr[4]:=i;');
- Add(' arr[5]:=arr[6];');
- Add(' arr.items[7]:=arr.items[8];');
- Add(' with arr do items[9]:=items[10];');
- Add(' doit(arr[7],arr[8],arr[9],arr[10]);');
- ConvertProgram;
- CheckSource('TestExternalClass_BracketAccessor',
- LinesToStr([ // statements
- 'this.DoIt = function (vI, vJ, vK, vL) {',
- '};',
- 'this.Arr = null;',
- 'this.s = "";',
- 'this.i = 0;',
- 'this.v = undefined;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.v = $mod.Arr[0];',
- '$mod.v = $mod.Arr[1];',
- '$mod.Arr[2] = $mod.s;',
- '$mod.Arr[3] = $mod.s;',
- '$mod.Arr[4] = $mod.i;',
- '$mod.Arr[5] = $mod.Arr[6];',
- '$mod.Arr[7] = $mod.Arr[8];',
- 'var $with1 = $mod.Arr;',
- '$with1[9] = $with1[10];',
- '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
- ' a: 9,',
- ' p: $mod.Arr,',
- ' get: function () {',
- ' return this.p[this.a];',
- ' },',
- ' set: function (v) {',
- ' this.p[this.a] = v;',
- ' }',
- '}, {',
- ' a: 10,',
- ' p: $mod.Arr,',
- ' get: function () {',
- ' return this.p[this.a];',
- ' },',
- ' set: function (v) {',
- ' this.p[this.a] = v;',
- ' }',
- '});',
- '']));
- end;
- procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TJSArray = class external name ''Array2''');
- Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
- Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
- Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
- Add(' end;');
- Add('begin');
- SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
- nBracketAccessorOfExternalClassMustHaveOneParameter);
- ConvertProgram;
- end;
- procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TJSArray = class external name ''Array2''');
- Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
- Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
- Add(' end;');
- Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
- Add('begin end;');
- Add('var');
- Add(' Arr: tjsarray;');
- Add(' v: jsvalue;');
- Add('begin');
- Add(' v:=arr[0];');
- Add(' v:=arr.items[1];');
- Add(' with arr do v:=items[2];');
- Add(' doit(arr[3],arr[4]);');
- ConvertProgram;
- CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
- LinesToStr([ // statements
- 'this.DoIt = function (vI, vJ) {',
- '};',
- 'this.Arr = null;',
- 'this.v = undefined;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.v = $mod.Arr[0];',
- '$mod.v = $mod.Arr[1];',
- 'var $with1 = $mod.Arr;',
- '$mod.v = $with1[2];',
- '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
- '']));
- end;
- procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TJSArray = class external name ''Array2''');
- Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
- Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
- Add(' end;');
- Add('var');
- Add(' Arr: tjsarray;');
- Add(' s: string;');
- Add(' i: longint;');
- Add(' v: jsvalue;');
- Add('begin');
- Add(' arr[2]:=s;');
- Add(' arr.items[3]:=s;');
- Add(' arr[4]:=i;');
- Add(' with arr do items[5]:=i;');
- ConvertProgram;
- CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
- LinesToStr([ // statements
- 'this.Arr = null;',
- 'this.s = "";',
- 'this.i = 0;',
- 'this.v = undefined;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.Arr[2] = $mod.s;',
- '$mod.Arr[3] = $mod.s;',
- '$mod.Arr[4] = $mod.i;',
- 'var $with1 = $mod.Arr;',
- '$with1[5] = $mod.i;',
- '']));
- end;
- procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TJSArray = class external name ''Array2''');
- Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
- Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
- Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
- Add(' property Numbers[Index: longint]: longint write SetNumbers;');
- Add(' end;');
- Add('var');
- Add(' Arr: tjsarray;');
- Add(' s: string;');
- Add(' i: longint;');
- Add(' v: jsvalue;');
- Add('begin');
- Add(' arr[2]:=s;');
- Add(' arr.items[3]:=s;');
- Add(' arr.numbers[4]:=i;');
- Add(' with arr do items[5]:=i;');
- Add(' with arr do numbers[6]:=i;');
- ConvertProgram;
- CheckSource('TestExternalClass_BracketAccessor_MultiType',
- LinesToStr([ // statements
- 'this.Arr = null;',
- 'this.s = "";',
- 'this.i = 0;',
- 'this.v = undefined;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.Arr[2] = $mod.s;',
- '$mod.Arr[3] = $mod.s;',
- '$mod.Arr[4] = $mod.i;',
- 'var $with1 = $mod.Arr;',
- '$with1[5] = $mod.i;',
- 'var $with2 = $mod.Arr;',
- '$with2[6] = $mod.i;',
- '']));
- end;
- procedure TTestModule.TestExternalClass_BracketAccessor_Index;
- begin
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TJSArray = class external name ''Array2''');
- Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
- Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
- Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
- Add(' end;');
- Add('var');
- Add(' Arr: tjsarray;');
- Add(' i: longint;');
- Add(' IntArr: array of longint;');
- Add(' v: jsvalue;');
- Add('begin');
- Add(' v:=arr.items[i];');
- Add(' arr[longint(v)]:=arr.items[intarr[0]];');
- Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
- ConvertProgram;
- CheckSource('TestExternalClass_BracketAccessor_Index',
- LinesToStr([ // statements
- 'this.Arr = null;',
- 'this.i = 0;',
- 'this.IntArr = [];',
- 'this.v = undefined;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.v = $mod.Arr[$mod.i];',
- '$mod.Arr[Math.floor($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
- '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
- '']));
- end;
- procedure TTestModule.TestProcType;
- begin
- StartProgram(false);
- Add('type');
- Add(' TProcInt = procedure(vI: longint = 1);');
- Add('procedure DoIt(vJ: longint);');
- Add('begin end;');
- Add('var');
- Add(' b: boolean;');
- Add(' vP, vQ: tprocint;');
- Add('begin');
- Add(' vp:=nil;');
- Add(' vp:=vp;');
- Add(' vp:=@doit;');
- Add(' vp;');
- Add(' vp();');
- Add(' vp(2);');
- Add(' b:=vp=nil;');
- Add(' b:=nil=vp;');
- Add(' b:=vp=vq;');
- Add(' b:=vp=@doit;');
- Add(' b:=@doit=vp;');
- Add(' b:=vp<>nil;');
- Add(' b:=nil<>vp;');
- Add(' b:=vp<>vq;');
- Add(' b:=vp<>@doit;');
- Add(' b:=@doit<>vp;');
- Add(' b:=Assigned(vp);');
- Add(' if Assigned(vp) then ;');
- ConvertProgram;
- CheckSource('TestProcType',
- LinesToStr([ // statements
- 'this.DoIt = function(vJ) {',
- '};',
- 'this.b = false;',
- 'this.vP = null;',
- 'this.vQ = null;'
- ]),
- LinesToStr([ // $mod.$main
- '$mod.vP = null;',
- '$mod.vP = $mod.vP;',
- '$mod.vP = $mod.DoIt;',
- '$mod.vP(1);',
- '$mod.vP(1);',
- '$mod.vP(2);',
- '$mod.b = $mod.vP == null;',
- '$mod.b = null == $mod.vP;',
- '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
- '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
- '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
- '$mod.b = $mod.vP != null;',
- '$mod.b = null != $mod.vP;',
- '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
- '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
- '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
- '$mod.b = $mod.vP != null;',
- 'if ($mod.vP != null) ;',
- '']));
- end;
- procedure TTestModule.TestProcType_FunctionFPC;
- begin
- StartProgram(false);
- Add('type');
- Add(' TFuncInt = function(vA: longint = 1): longint;');
- Add('function DoIt(vI: longint): longint;');
- Add('begin end;');
- Add('var');
- Add(' b: boolean;');
- Add(' vP, vQ: tfuncint;');
- Add('begin');
- Add(' vp:=nil;');
- Add(' vp:=vp;');
- Add(' vp:=@doit;'); // ok in fpc and delphi
- //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
- Add(' vp;'); // ok in fpc and delphi
- Add(' vp();');
- Add(' vp(2);');
- Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
- Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
- Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
- Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
- Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
- //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
- Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
- Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
- Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
- Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
- Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
- Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
- //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
- Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
- Add(' b:=Assigned(vp);');
- //Add(' doit(vp);'); // illegal in fpc, ok in delphi
- Add(' doit(vp());'); // ok in fpc and delphi
- Add(' doit(vp(2));'); // ok in fpc and delphi
- ConvertProgram;
- CheckSource('TestProcType_FunctionFPC',
- LinesToStr([ // statements
- 'this.DoIt = function(vI) {',
- ' var Result = 0;',
- ' return Result;',
- '};',
- 'this.b = false;',
- 'this.vP = null;',
- 'this.vQ = null;'
- ]),
- LinesToStr([ // $mod.$main
- '$mod.vP = null;',
- '$mod.vP = $mod.vP;',
- '$mod.vP = $mod.DoIt;',
- '$mod.vP(1);',
- '$mod.vP(1);',
- '$mod.vP(2);',
- '$mod.b = $mod.vP == null;',
- '$mod.b = null == $mod.vP;',
- '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
- '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
- '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
- '$mod.b = 4 == $mod.vP(1);',
- '$mod.b = $mod.vP != null;',
- '$mod.b = null != $mod.vP;',
- '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
- '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
- '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
- '$mod.b = 6 != $mod.vP(1);',
- '$mod.b = $mod.vP != null;',
- '$mod.DoIt($mod.vP(1));',
- '$mod.DoIt($mod.vP(2));',
- '']));
- end;
- procedure TTestModule.TestProcType_FunctionDelphi;
- begin
- StartProgram(false);
- Add('{$mode Delphi}');
- Add('type');
- Add(' TFuncInt = function(vA: longint = 1): longint;');
- Add('function DoIt(vI: longint): longint;');
- Add('begin end;');
- Add('var');
- Add(' b: boolean;');
- Add(' vP, vQ: tfuncint;');
- Add('begin');
- Add(' vp:=nil;');
- Add(' vp:=vp;');
- Add(' vp:=@doit;'); // ok in fpc and delphi
- Add(' vp:=doit;'); // illegal in fpc, ok in delphi
- Add(' vp;'); // ok in fpc and delphi
- Add(' vp();');
- Add(' vp(2);');
- //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
- //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
- Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
- //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
- //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
- Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
- Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
- //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
- //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
- Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
- //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
- //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
- Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
- Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
- Add(' b:=Assigned(vp);');
- Add(' doit(vp);'); // illegal in fpc, ok in delphi
- Add(' doit(vp());'); // ok in fpc and delphi
- Add(' doit(vp(2));'); // ok in fpc and delphi *)
- ConvertProgram;
- CheckSource('TestProcType_FunctionDelphi',
- LinesToStr([ // statements
- 'this.DoIt = function(vI) {',
- ' var Result = 0;',
- ' return Result;',
- '};',
- 'this.b = false;',
- 'this.vP = null;',
- 'this.vQ = null;'
- ]),
- LinesToStr([ // $mod.$main
- '$mod.vP = null;',
- '$mod.vP = $mod.vP;',
- '$mod.vP = $mod.DoIt;',
- '$mod.vP = $mod.DoIt;',
- '$mod.vP(1);',
- '$mod.vP(1);',
- '$mod.vP(2);',
- '$mod.b = $mod.vP(1) == $mod.vQ(1);',
- '$mod.b = $mod.vP(1) == 3;',
- '$mod.b = 4 == $mod.vP(1);',
- '$mod.b = $mod.vP(1) != $mod.vQ(1);',
- '$mod.b = $mod.vP(1) != 5;',
- '$mod.b = 6 != $mod.vP(1);',
- '$mod.b = $mod.vP != null;',
- '$mod.DoIt($mod.vP(1));',
- '$mod.DoIt($mod.vP(1));',
- '$mod.DoIt($mod.vP(2));',
- '']));
- end;
- procedure TTestModule.TestProcType_AsParam;
- begin
- StartProgram(false);
- Add('type');
- Add(' TFuncInt = function(vA: longint = 1): longint;');
- Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
- Add('var vJ: tfuncint;');
- Add('begin');
- Add(' vg:=vg;');
- Add(' vj:=vh;');
- Add(' vi:=vi;');
- Add(' doit(vg,vg,vg);');
- Add(' doit(vh,vh,vj);');
- Add(' doit(vi,vi,vi);');
- Add(' doit(vj,vj,vj);');
- Add('end;');
- Add('var i: tfuncint;');
- Add('begin');
- Add(' doit(i,i,i);');
- ConvertProgram;
- CheckSource('TestProcType_AsParam',
- LinesToStr([ // statements
- 'this.DoIt = function (vG,vH,vI) {',
- ' var vJ = null;',
- ' vG = vG;',
- ' vJ = vH;',
- ' vI.set(vI.get());',
- ' $mod.DoIt(vG, vG, {',
- ' get: function () {',
- ' return vG;',
- ' },',
- ' set: function (v) {',
- ' vG = v;',
- ' }',
- ' });',
- ' $mod.DoIt(vH, vH, {',
- ' get: function () {',
- ' return vJ;',
- ' },',
- ' set: function (v) {',
- ' vJ = v;',
- ' }',
- ' });',
- ' $mod.DoIt(vI.get(), vI.get(), vI);',
- ' $mod.DoIt(vJ, vJ, {',
- ' get: function () {',
- ' return vJ;',
- ' },',
- ' set: function (v) {',
- ' vJ = v;',
- ' }',
- ' });',
- '};',
- 'this.i = null;'
- ]),
- LinesToStr([
- '$mod.DoIt($mod.i,$mod.i,{',
- ' p: $mod,',
- ' get: function () {',
- ' return this.p.i;',
- ' },',
- ' set: function (v) {',
- ' this.p.i = v;',
- ' }',
- '});'
- ]));
- end;
- procedure TTestModule.TestProcType_MethodFPC;
- begin
- StartProgram(false);
- Add('type');
- Add(' TFuncInt = function(vA: longint = 1): longint of object;');
- Add(' TObject = class');
- Add(' function DoIt(vA: longint = 1): longint;');
- Add(' end;');
- Add('function TObject.DoIt(vA: longint = 1): longint;');
- Add('begin');
- Add('end;');
- Add('var');
- Add(' Obj: TObject;');
- Add(' vP: tfuncint;');
- Add(' b: boolean;');
- Add('begin');
- Add(' vp:[email protected];'); // ok in fpc and delphi
- //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
- Add(' vp;'); // ok in fpc and delphi
- Add(' vp();');
- Add(' vp(2);');
- Add(' b:[email protected];'); // ok in fpc, illegal in delphi
- Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
- Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
- Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
- ConvertProgram;
- CheckSource('TestProcType_MethodFPC',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.DoIt = function (vA) {',
- ' var Result = 0;',
- ' return Result;',
- ' };',
- '});',
- 'this.Obj = null;',
- 'this.vP = null;',
- 'this.b = false;'
- ]),
- LinesToStr([
- '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
- '$mod.vP(1);',
- '$mod.vP(1);',
- '$mod.vP(2);',
- '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
- '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
- '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
- '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
- '']));
- end;
- procedure TTestModule.TestProcType_MethodDelphi;
- begin
- StartProgram(false);
- Add('{$mode delphi}');
- Add('type');
- Add(' TFuncInt = function(vA: longint = 1): longint of object;');
- Add(' TObject = class');
- Add(' function DoIt(vA: longint = 1): longint;');
- Add(' end;');
- Add('function TObject.DoIt(vA: longint = 1): longint;');
- Add('begin');
- Add('end;');
- Add('var');
- Add(' Obj: TObject;');
- Add(' vP: tfuncint;');
- Add(' b: boolean;');
- Add('begin');
- Add(' vp:[email protected];'); // ok in fpc and delphi
- Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
- Add(' vp;'); // ok in fpc and delphi
- Add(' vp();');
- Add(' vp(2);');
- //Add(' b:[email protected];'); // ok in fpc, illegal in delphi
- //Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
- //Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
- //Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
- ConvertProgram;
- CheckSource('TestProcType_MethodDelphi',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.DoIt = function (vA) {',
- ' var Result = 0;',
- ' return Result;',
- ' };',
- '});',
- 'this.Obj = null;',
- 'this.vP = null;',
- 'this.b = false;'
- ]),
- LinesToStr([
- '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
- '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
- '$mod.vP(1);',
- '$mod.vP(1);',
- '$mod.vP(2);',
- '']));
- end;
- procedure TTestModule.TestProcType_PropertyFPC;
- begin
- StartProgram(false);
- Add('type');
- Add(' TFuncInt = function(vA: longint = 1): longint of object;');
- Add(' TObject = class');
- Add(' FOnFoo: TFuncInt;');
- Add(' function DoIt(vA: longint = 1): longint;');
- Add(' function GetFoo: TFuncInt;');
- Add(' procedure SetFoo(const Value: TFuncInt);');
- Add(' function GetEvents(Index: longint): TFuncInt;');
- Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
- Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
- Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
- Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
- Add(' end;');
- Add('function tobject.doit(va: longint = 1): longint; begin end;');
- Add('function tobject.getfoo: tfuncint; begin end;');
- Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
- Add('function tobject.getevents(index: longint): tfuncint; begin end;');
- Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
- Add('var');
- Add(' Obj: TObject;');
- Add(' vP: tfuncint;');
- Add(' b: boolean;');
- Add('begin');
- Add(' obj.onfoo:=nil;');
- Add(' obj.onbar:=nil;');
- Add(' obj.events[1]:=nil;');
- Add(' obj.onfoo:=obj.onfoo;');
- Add(' obj.onbar:=obj.onbar;');
- Add(' obj.events[2]:=obj.events[3];');
- Add(' obj.onfoo:[email protected];');
- Add(' obj.onbar:[email protected];');
- Add(' obj.events[4]:[email protected];');
- //Add(' obj.onfoo:=obj.doit;'); // delphi
- //Add(' obj.onbar:=obj.doit;'); // delphi
- //Add(' obj.events[4]:=obj.doit;'); // delphi
- Add(' obj.onfoo;');
- Add(' obj.onbar;');
- //Add(' obj.events[5];'); ToDo in pasresolver
- Add(' obj.onfoo();');
- Add(' obj.onbar();');
- Add(' obj.events[6]();');
- Add(' b:=obj.onfoo=nil;');
- Add(' b:=obj.onbar=nil;');
- Add(' b:=obj.events[7]=nil;');
- Add(' b:=obj.onfoo<>nil;');
- Add(' b:=obj.onbar<>nil;');
- Add(' b:=obj.events[8]<>nil;');
- Add(' b:=obj.onfoo=vp;');
- Add(' b:=obj.onbar=vp;');
- Add(' b:=obj.events[9]=vp;');
- Add(' b:=obj.onfoo=obj.onfoo;');
- Add(' b:=obj.onbar=obj.onfoo;');
- Add(' b:=obj.events[10]=obj.onfoo;');
- Add(' b:=obj.onfoo<>obj.onfoo;');
- Add(' b:=obj.onbar<>obj.onfoo;');
- Add(' b:=obj.events[11]<>obj.onfoo;');
- Add(' b:[email protected];');
- Add(' b:[email protected];');
- Add(' b:=obj.events[12][email protected];');
- Add(' b:=obj.onfoo<>@obj.doit;');
- Add(' b:=obj.onbar<>@obj.doit;');
- Add(' b:=obj.events[12]<>@obj.doit;');
- Add(' b:=Assigned(obj.onfoo);');
- Add(' b:=Assigned(obj.onbar);');
- Add(' b:=Assigned(obj.events[13]);');
- ConvertProgram;
- CheckSource('TestProcType_PropertyFPC',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' this.FOnFoo = null;',
- ' };',
- ' this.$final = function () {',
- ' this.FOnFoo = undefined;',
- ' };',
- ' this.DoIt = function (vA) {',
- ' var Result = 0;',
- ' return Result;',
- ' };',
- 'this.GetFoo = function () {',
- ' var Result = null;',
- ' return Result;',
- '};',
- 'this.SetFoo = function (Value) {',
- '};',
- 'this.GetEvents = function (Index) {',
- ' var Result = null;',
- ' return Result;',
- '};',
- 'this.SetEvents = function (Index, Value) {',
- '};',
- '});',
- 'this.Obj = null;',
- 'this.vP = null;',
- 'this.b = false;'
- ]),
- LinesToStr([
- '$mod.Obj.FOnFoo = null;',
- '$mod.Obj.SetFoo(null);',
- '$mod.Obj.SetEvents(1, null);',
- '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
- '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
- '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
- '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
- '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
- '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
- '$mod.Obj.FOnFoo(1);',
- '$mod.Obj.GetFoo();',
- '$mod.Obj.FOnFoo(1);',
- '$mod.Obj.GetFoo()(1);',
- '$mod.Obj.GetEvents(6)(1);',
- '$mod.b = $mod.Obj.FOnFoo == null;',
- '$mod.b = $mod.Obj.GetFoo() == null;',
- '$mod.b = $mod.Obj.GetEvents(7) == null;',
- '$mod.b = $mod.Obj.FOnFoo != null;',
- '$mod.b = $mod.Obj.GetFoo() != null;',
- '$mod.b = $mod.Obj.GetEvents(8) != null;',
- '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
- '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
- '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
- '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
- '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
- '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
- '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
- '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
- '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
- '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
- '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
- '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
- '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
- '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
- '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
- '$mod.b = $mod.Obj.FOnFoo != null;',
- '$mod.b = $mod.Obj.GetFoo() != null;',
- '$mod.b = $mod.Obj.GetEvents(13) != null;',
- '']));
- end;
- procedure TTestModule.TestProcType_PropertyDelphi;
- begin
- StartProgram(false);
- Add('{$mode delphi}');
- Add('type');
- Add(' TFuncInt = function(vA: longint = 1): longint of object;');
- Add(' TObject = class');
- Add(' FOnFoo: TFuncInt;');
- Add(' function DoIt(vA: longint = 1): longint;');
- Add(' function GetFoo: TFuncInt;');
- Add(' procedure SetFoo(const Value: TFuncInt);');
- Add(' function GetEvents(Index: longint): TFuncInt;');
- Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
- Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
- Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
- Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
- Add(' end;');
- Add('function tobject.doit(va: longint = 1): longint; begin end;');
- Add('function tobject.getfoo: tfuncint; begin end;');
- Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
- Add('function tobject.getevents(index: longint): tfuncint; begin end;');
- Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
- Add('var');
- Add(' Obj: TObject;');
- Add(' vP: tfuncint;');
- Add(' b: boolean;');
- Add('begin');
- Add(' obj.onfoo:=nil;');
- Add(' obj.onbar:=nil;');
- Add(' obj.events[1]:=nil;');
- Add(' obj.onfoo:=obj.onfoo;');
- Add(' obj.onbar:=obj.onbar;');
- Add(' obj.events[2]:=obj.events[3];');
- Add(' obj.onfoo:[email protected];');
- Add(' obj.onbar:[email protected];');
- Add(' obj.events[4]:[email protected];');
- Add(' obj.onfoo:=obj.doit;'); // delphi
- Add(' obj.onbar:=obj.doit;'); // delphi
- Add(' obj.events[4]:=obj.doit;'); // delphi
- Add(' obj.onfoo;');
- Add(' obj.onbar;');
- //Add(' obj.events[5];'); ToDo in pasresolver
- Add(' obj.onfoo();');
- Add(' obj.onbar();');
- Add(' obj.events[6]();');
- //Add(' b:=obj.onfoo=nil;'); // fpc
- //Add(' b:=obj.onbar=nil;'); // fpc
- //Add(' b:=obj.events[7]=nil;'); // fpc
- //Add(' b:=obj.onfoo<>nil;'); // fpc
- //Add(' b:=obj.onbar<>nil;'); // fpc
- //Add(' b:=obj.events[8]<>nil;'); // fpc
- Add(' b:=obj.onfoo=vp;');
- Add(' b:=obj.onbar=vp;');
- //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
- Add(' b:=obj.onfoo=obj.onfoo;');
- Add(' b:=obj.onbar=obj.onfoo;');
- //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
- Add(' b:=obj.onfoo<>obj.onfoo;');
- Add(' b:=obj.onbar<>obj.onfoo;');
- //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
- //Add(' b:[email protected];'); // fpc
- //Add(' b:[email protected];'); // fpc
- //Add(' b:=obj.events[12][email protected];'); // fpc
- //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
- //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
- //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
- Add(' b:=Assigned(obj.onfoo);');
- Add(' b:=Assigned(obj.onbar);');
- Add(' b:=Assigned(obj.events[13]);');
- ConvertProgram;
- CheckSource('TestProcType_PropertyDelphi',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' this.FOnFoo = null;',
- ' };',
- ' this.$final = function () {',
- ' this.FOnFoo = undefined;',
- ' };',
- ' this.DoIt = function (vA) {',
- ' var Result = 0;',
- ' return Result;',
- ' };',
- 'this.GetFoo = function () {',
- ' var Result = null;',
- ' return Result;',
- '};',
- 'this.SetFoo = function (Value) {',
- '};',
- 'this.GetEvents = function (Index) {',
- ' var Result = null;',
- ' return Result;',
- '};',
- 'this.SetEvents = function (Index, Value) {',
- '};',
- '});',
- 'this.Obj = null;',
- 'this.vP = null;',
- 'this.b = false;'
- ]),
- LinesToStr([
- '$mod.Obj.FOnFoo = null;',
- '$mod.Obj.SetFoo(null);',
- '$mod.Obj.SetEvents(1, null);',
- '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
- '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
- '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
- '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
- '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
- '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
- '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
- '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
- '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
- '$mod.Obj.FOnFoo(1);',
- '$mod.Obj.GetFoo();',
- '$mod.Obj.FOnFoo(1);',
- '$mod.Obj.GetFoo()(1);',
- '$mod.Obj.GetEvents(6)(1);',
- '$mod.b = $mod.Obj.FOnFoo(1) == $mod.vP(1);',
- '$mod.b = $mod.Obj.GetFoo() == $mod.vP(1);',
- '$mod.b = $mod.Obj.FOnFoo(1) == $mod.Obj.FOnFoo(1);',
- '$mod.b = $mod.Obj.GetFoo() == $mod.Obj.FOnFoo(1);',
- '$mod.b = $mod.Obj.FOnFoo(1) != $mod.Obj.FOnFoo(1);',
- '$mod.b = $mod.Obj.GetFoo() != $mod.Obj.FOnFoo(1);',
- '$mod.b = $mod.Obj.FOnFoo != null;',
- '$mod.b = $mod.Obj.GetFoo() != null;',
- '$mod.b = $mod.Obj.GetEvents(13) != null;',
- '']));
- end;
- procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
- begin
- StartProgram(false);
- Add('type');
- Add(' TFuncInt = function(vA: longint = 1): longint of object;');
- Add(' TObject = class');
- Add(' FOnFoo: TFuncInt;');
- Add(' function DoIt(vA: longint = 1): longint;');
- Add(' function GetFoo: TFuncInt;');
- Add(' procedure SetFoo(const Value: TFuncInt);');
- Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
- Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
- Add(' end;');
- Add('function tobject.doit(va: longint = 1): longint; begin end;');
- Add('function tobject.getfoo: tfuncint; begin end;');
- Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
- Add('var');
- Add(' Obj: TObject;');
- Add(' vP: tfuncint;');
- Add(' b: boolean;');
- Add('begin');
- Add('with obj do begin');
- Add(' fonfoo:=nil;');
- Add(' onfoo:=nil;');
- Add(' onbar:=nil;');
- Add(' fonfoo:=fonfoo;');
- Add(' onfoo:=onfoo;');
- Add(' onbar:=onbar;');
- Add(' fonfoo:=@doit;');
- Add(' onfoo:=@doit;');
- Add(' onbar:=@doit;');
- //Add(' fonfoo:=doit;'); // delphi
- //Add(' onfoo:=doit;'); // delphi
- //Add(' onbar:=doit;'); // delphi
- Add(' fonfoo;');
- Add(' onfoo;');
- Add(' onbar;');
- Add(' fonfoo();');
- Add(' onfoo();');
- Add(' onbar();');
- Add(' b:=fonfoo=nil;');
- Add(' b:=onfoo=nil;');
- Add(' b:=onbar=nil;');
- Add(' b:=fonfoo<>nil;');
- Add(' b:=onfoo<>nil;');
- Add(' b:=onbar<>nil;');
- Add(' b:=fonfoo=vp;');
- Add(' b:=onfoo=vp;');
- Add(' b:=onbar=vp;');
- Add(' b:=fonfoo=fonfoo;');
- Add(' b:=onfoo=onfoo;');
- Add(' b:=onbar=onfoo;');
- Add(' b:=fonfoo<>fonfoo;');
- Add(' b:=onfoo<>onfoo;');
- Add(' b:=onbar<>onfoo;');
- Add(' b:=fonfoo=@doit;');
- Add(' b:=onfoo=@doit;');
- Add(' b:=onbar=@doit;');
- Add(' b:=fonfoo<>@doit;');
- Add(' b:=onfoo<>@doit;');
- Add(' b:=onbar<>@doit;');
- Add(' b:=Assigned(fonfoo);');
- Add(' b:=Assigned(onfoo);');
- Add(' b:=Assigned(onbar);');
- Add('end;');
- ConvertProgram;
- CheckSource('TestProcType_WithClassInstDoPropertyFPC',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' this.FOnFoo = null;',
- ' };',
- ' this.$final = function () {',
- ' this.FOnFoo = undefined;',
- ' };',
- ' this.DoIt = function (vA) {',
- ' var Result = 0;',
- ' return Result;',
- ' };',
- ' this.GetFoo = function () {',
- ' var Result = null;',
- ' return Result;',
- ' };',
- ' this.SetFoo = function (Value) {',
- ' };',
- '});',
- 'this.Obj = null;',
- 'this.vP = null;',
- 'this.b = false;'
- ]),
- LinesToStr([
- 'var $with1 = $mod.Obj;',
- '$with1.FOnFoo = null;',
- '$with1.FOnFoo = null;',
- '$with1.SetFoo(null);',
- '$with1.FOnFoo = $with1.FOnFoo;',
- '$with1.FOnFoo = $with1.FOnFoo;',
- '$with1.SetFoo($with1.GetFoo());',
- '$with1.FOnFoo = rtl.createCallback($with1, "DoIt");',
- '$with1.FOnFoo = rtl.createCallback($with1, "DoIt");',
- '$with1.SetFoo(rtl.createCallback($with1, "DoIt"));',
- '$with1.FOnFoo(1);',
- '$with1.FOnFoo(1);',
- '$with1.GetFoo();',
- '$with1.FOnFoo(1);',
- '$with1.FOnFoo(1);',
- '$with1.GetFoo()(1);',
- '$mod.b = $with1.FOnFoo == null;',
- '$mod.b = $with1.FOnFoo == null;',
- '$mod.b = $with1.GetFoo() == null;',
- '$mod.b = $with1.FOnFoo != null;',
- '$mod.b = $with1.FOnFoo != null;',
- '$mod.b = $with1.GetFoo() != null;',
- '$mod.b = rtl.eqCallback($with1.FOnFoo, $mod.vP);',
- '$mod.b = rtl.eqCallback($with1.FOnFoo, $mod.vP);',
- '$mod.b = rtl.eqCallback($with1.GetFoo(), $mod.vP);',
- '$mod.b = rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
- '$mod.b = rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
- '$mod.b = rtl.eqCallback($with1.GetFoo(), $with1.FOnFoo);',
- '$mod.b = !rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
- '$mod.b = !rtl.eqCallback($with1.FOnFoo, $with1.FOnFoo);',
- '$mod.b = !rtl.eqCallback($with1.GetFoo(), $with1.FOnFoo);',
- '$mod.b = rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
- '$mod.b = rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
- '$mod.b = rtl.eqCallback($with1.GetFoo(), rtl.createCallback($with1, "DoIt"));',
- '$mod.b = !rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
- '$mod.b = !rtl.eqCallback($with1.FOnFoo, rtl.createCallback($with1, "DoIt"));',
- '$mod.b = !rtl.eqCallback($with1.GetFoo(), rtl.createCallback($with1, "DoIt"));',
- '$mod.b = $with1.FOnFoo != null;',
- '$mod.b = $with1.FOnFoo != null;',
- '$mod.b = $with1.GetFoo() != null;',
- '']));
- end;
- procedure TTestModule.TestProcType_Nested;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' TProcInt = procedure(vI: longint = 1);',
- 'procedure DoIt(vJ: longint);',
- 'var aProc: TProcInt;',
- ' b: boolean;',
- ' procedure Sub(vK: longint);',
- ' var aSub: TProcInt;',
- ' procedure SubSub(vK: longint);',
- ' var aSubSub: TProcInt;',
- ' begin;',
- ' aProc:=@DoIt;',
- ' aSub:=@DoIt;',
- ' aSubSub:=@DoIt;',
- ' aProc:=@Sub;',
- ' aSub:=@Sub;',
- ' aSubSub:=@Sub;',
- ' aProc:=@SubSub;',
- ' aSub:=@SubSub;',
- ' aSubSub:=@SubSub;',
- ' end;',
- ' begin;',
- ' end;',
- 'begin;',
- ' aProc:=@Sub;',
- ' b:=aProc=@Sub;',
- ' b:=@Sub=aProc;',
- 'end;',
- 'begin',
- '']);
- ConvertProgram;
- CheckSource('TestProcType_Nested',
- LinesToStr([ // statements
- 'this.DoIt = function (vJ) {',
- ' var aProc = null;',
- ' var b = false;',
- ' function Sub(vK) {',
- ' var aSub = null;',
- ' function SubSub(vK) {',
- ' var aSubSub = null;',
- ' aProc = $mod.DoIt;',
- ' aSub = $mod.DoIt;',
- ' aSubSub = $mod.DoIt;',
- ' aProc = Sub;',
- ' aSub = Sub;',
- ' aSubSub = Sub;',
- ' aProc = SubSub;',
- ' aSub = SubSub;',
- ' aSubSub = SubSub;',
- ' };',
- ' };',
- ' aProc = Sub;',
- ' b = rtl.eqCallback(aProc, Sub);',
- ' b = rtl.eqCallback(Sub, aProc);',
- '};',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestModule.TestProcType_NestedOfObject;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' TProcInt = procedure(vI: longint = 1) of object;',
- ' TObject = class',
- ' procedure DoIt(vJ: longint);',
- ' end;',
- 'procedure TObject.DoIt(vJ: longint);',
- 'var aProc: TProcInt;',
- ' b: boolean;',
- ' procedure Sub(vK: longint);',
- ' var aSub: TProcInt;',
- ' procedure SubSub(vK: longint);',
- ' var aSubSub: TProcInt;',
- ' begin;',
- ' aProc:=@DoIt;',
- ' aSub:=@DoIt;',
- ' aSubSub:=@DoIt;',
- ' aProc:=@Sub;',
- ' aSub:=@Sub;',
- ' aSubSub:=@Sub;',
- ' aProc:=@SubSub;',
- ' aSub:=@SubSub;',
- ' aSubSub:=@SubSub;',
- ' end;',
- ' begin;',
- ' end;',
- 'begin;',
- ' aProc:=@Sub;',
- ' b:=aProc=@Sub;',
- ' b:=@Sub=aProc;',
- 'end;',
- 'begin',
- '']);
- ConvertProgram;
- CheckSource('TestProcType_Nested',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.DoIt = function (vJ) {',
- ' var Self = this;',
- ' var aProc = null;',
- ' var b = false;',
- ' function Sub(vK) {',
- ' var aSub = null;',
- ' function SubSub(vK) {',
- ' var aSubSub = null;',
- ' aProc = rtl.createCallback(Self, "DoIt");',
- ' aSub = rtl.createCallback(Self, "DoIt");',
- ' aSubSub = rtl.createCallback(Self, "DoIt");',
- ' aProc = Sub;',
- ' aSub = Sub;',
- ' aSubSub = Sub;',
- ' aProc = SubSub;',
- ' aSub = SubSub;',
- ' aSubSub = SubSub;',
- ' };',
- ' };',
- ' aProc = Sub;',
- ' b = rtl.eqCallback(aProc, Sub);',
- ' b = rtl.eqCallback(Sub, aProc);',
- ' };',
- '});',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestModule.TestProcType_ReferenceToProc;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' TProcRef = reference to procedure(i: longint = 0);',
- ' TFuncRef = reference to function(i: longint = 0): longint;',
- 'var',
- ' p: TProcRef;',
- ' f: TFuncRef;',
- 'procedure DoIt(i: longint);',
- 'begin',
- 'end;',
- 'function GetIt(i: longint): longint;',
- 'begin',
- ' p:=@DoIt;',
- ' f:=@GetIt;',
- ' f;',
- ' f();',
- ' f(1);',
- 'end;',
- 'begin',
- ' p:=@DoIt;',
- ' f:=@GetIt;',
- ' f;',
- ' f();',
- ' f(1);',
- ' p:=TProcRef(f);',
- '']);
- ConvertProgram;
- CheckSource('TestProcType_ReferenceToProc',
- LinesToStr([ // statements
- 'this.p = null;',
- 'this.f = null;',
- 'this.DoIt = function (i) {',
- '};',
- 'this.GetIt = function (i) {',
- ' var Result = 0;',
- ' $mod.p = $mod.DoIt;',
- ' $mod.f = $mod.GetIt;',
- ' $mod.f(0);',
- ' $mod.f(0);',
- ' $mod.f(1);',
- ' return Result;',
- '};',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.p = $mod.DoIt;',
- '$mod.f = $mod.GetIt;',
- '$mod.f(0);',
- '$mod.f(0);',
- '$mod.f(1);',
- '$mod.p = $mod.f;',
- '']));
- end;
- procedure TTestModule.TestProcType_ReferenceToMethod;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' TFuncRef = reference to function(i: longint = 5): longint;',
- ' TObject = class',
- ' function Grow(s: longint): longint;',
- ' end;',
- 'var',
- ' f: tfuncref;',
- 'function tobject.grow(s: longint): longint;',
- ' function GrowSub(i: longint): longint;',
- ' begin',
- ' f:=@grow;',
- ' f:=@growsub;',
- ' end;',
- 'begin',
- ' f:=@grow;',
- ' f:=@growsub;',
- 'end;',
- 'begin',
- '']);
- ConvertProgram;
- CheckSource('TestProcType_ReferenceToMethod',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.Grow = function (s) {',
- ' var Self = this;',
- ' var Result = 0;',
- ' function GrowSub(i) {',
- ' var Result = 0;',
- ' $mod.f = rtl.createCallback(Self, "Grow");',
- ' $mod.f = GrowSub;',
- ' return Result;',
- ' };',
- ' $mod.f = rtl.createCallback(Self, "Grow");',
- ' $mod.f = GrowSub;',
- ' return Result;',
- ' };',
- '});',
- 'this.f = null;',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestModule.TestProcType_Typecast;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' TNotifyEvent = procedure(Sender: Pointer) of object;',
- ' TEvent = procedure of object;',
- ' TGetter = function:longint of object;',
- ' TProcA = procedure(i: longint);',
- ' TFuncB = function(i, j: longint): longint;',
- 'procedure DoIt(); varargs; begin end;',
- 'var',
- ' Notify: tnotifyevent;',
- ' Event: tevent;',
- ' Getter: tgetter;',
- ' ProcA: tproca;',
- ' FuncB: tfuncb;',
- ' p: pointer;',
- 'begin',
- ' notify:=tnotifyevent(event);',
- ' event:=tevent(event);',
- ' event:=tevent(notify);',
- ' event:=tevent(getter);',
- ' event:=tevent(proca);',
- ' proca:=tproca(funcb);',
- ' funcb:=tfuncb(funcb);',
- ' funcb:=tfuncb(proca);',
- ' funcb:=tfuncb(getter);',
- ' proca:=tproca(p);',
- ' funcb:=tfuncb(p);',
- ' getter:=tgetter(p);',
- ' p:=pointer(notify);',
- ' p:=notify;',
- ' p:=pointer(proca);',
- ' p:=proca;',
- ' p:=pointer(funcb);',
- ' p:=funcb;',
- ' doit(Pointer(notify),pointer(event),pointer(proca));',
- '']);
- ConvertProgram;
- CheckSource('TestProcType_Typecast',
- LinesToStr([ // statements
- 'this.DoIt = function () {',
- '};',
- 'this.Notify = null;',
- 'this.Event = null;',
- 'this.Getter = null;',
- 'this.ProcA = null;',
- 'this.FuncB = null;',
- 'this.p = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.Notify = $mod.Event;',
- '$mod.Event = $mod.Event;',
- '$mod.Event = $mod.Notify;',
- '$mod.Event = $mod.Getter;',
- '$mod.Event = $mod.ProcA;',
- '$mod.ProcA = $mod.FuncB;',
- '$mod.FuncB = $mod.FuncB;',
- '$mod.FuncB = $mod.ProcA;',
- '$mod.FuncB = $mod.Getter;',
- '$mod.ProcA = $mod.p;',
- '$mod.FuncB = $mod.p;',
- '$mod.Getter = $mod.p;',
- '$mod.p = $mod.Notify;',
- '$mod.p = $mod.Notify;',
- '$mod.p = $mod.ProcA;',
- '$mod.p = $mod.ProcA;',
- '$mod.p = $mod.FuncB;',
- '$mod.p = $mod.FuncB;',
- '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
- '']));
- end;
- procedure TTestModule.TestProcType_PassProcToUntyped;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' TEvent = procedure of object;',
- ' TFunc = function: longint;',
- 'procedure DoIt(); varargs; begin end;',
- 'procedure DoSome(const a; var b; p: pointer); begin end;',
- 'var',
- ' Event: tevent;',
- ' Func: TFunc;',
- 'begin',
- ' doit(event,func);',
- ' dosome(event,event,event);',
- ' dosome(func,func,func);',
- '']);
- ConvertProgram;
- CheckSource('TestProcType_PassProcToUntyped',
- LinesToStr([ // statements
- 'this.DoIt = function () {',
- '};',
- 'this.DoSome = function (a, b, p) {',
- '};',
- 'this.Event = null;',
- 'this.Func = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.DoIt($mod.Event, $mod.Func);',
- '$mod.DoSome($mod.Event, {',
- ' p: $mod,',
- ' get: function () {',
- ' return this.p.Event;',
- ' },',
- ' set: function (v) {',
- ' this.p.Event = v;',
- ' }',
- '}, $mod.Event);',
- '$mod.DoSome($mod.Func, {',
- ' p: $mod,',
- ' get: function () {',
- ' return this.p.Func;',
- ' },',
- ' set: function (v) {',
- ' this.p.Func = v;',
- ' }',
- '}, $mod.Func);',
- '']));
- end;
- procedure TTestModule.TestPointer;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class end;');
- Add(' TClass = class of TObject;');
- Add(' TArrInt = array of longint;');
- Add('var');
- Add(' v: jsvalue;');
- Add(' Obj: tobject;');
- Add(' C: tclass;');
- Add(' a: tarrint;');
- Add(' p: Pointer;');
- Add('begin');
- Add(' p:=p;');
- Add(' p:=nil;');
- Add(' if p=nil then;');
- Add(' if nil=p then;');
- Add(' if Assigned(p) then;');
- Add(' p:=Pointer(v);');
- Add(' p:=obj;');
- Add(' p:=c;');
- Add(' p:=a;');
- Add(' p:=tobject;');
- Add(' obj:=TObject(p);');
- Add(' c:=TClass(p);');
- Add(' a:=TArrInt(p);');
- ConvertProgram;
- CheckSource('TestPointer',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'this.v = undefined;',
- 'this.Obj = null;',
- 'this.C = null;',
- 'this.a = [];',
- 'this.p = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.p = $mod.p;',
- '$mod.p = null;',
- 'if ($mod.p == null) ;',
- 'if (null == $mod.p) ;',
- 'if ($mod.p != null) ;',
- '$mod.p = $mod.v;',
- '$mod.p = $mod.Obj;',
- '$mod.p = $mod.C;',
- '$mod.p = $mod.a;',
- '$mod.p = $mod.TObject;',
- '$mod.Obj = $mod.p;',
- '$mod.C = $mod.p;',
- '$mod.a = $mod.p;',
- '']));
- end;
- procedure TTestModule.TestPointer_Proc;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' procedure DoIt; virtual; abstract;');
- Add(' end;');
- Add('procedure DoSome; begin end;');
- Add('var');
- Add(' o: TObject;');
- Add(' p: Pointer;');
- Add('begin');
- Add(' p:=@DoSome;');
- Add(' p:[email protected];');
- ConvertProgram;
- CheckSource('TestPointer_Proc',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'this.DoSome = function () {',
- '};',
- 'this.o = null;',
- 'this.p = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.p = $mod.DoSome;',
- '$mod.p = rtl.createCallback($mod.o, "DoIt");',
- '']));
- end;
- procedure TTestModule.TestPointer_AssignRecordFail;
- begin
- StartProgram(false);
- Add('type');
- Add(' TRec = record end;');
- Add('var');
- Add(' p: Pointer;');
- Add(' r: TRec;');
- Add('begin');
- Add(' p:=r;');
- SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
- nIncompatibleTypesGotExpected);
- ConvertProgram;
- end;
- procedure TTestModule.TestPointer_AssignStaticArrayFail;
- begin
- StartProgram(false);
- Add('type');
- Add(' TArr = array[boolean] of longint;');
- Add('var');
- Add(' p: Pointer;');
- Add(' a: TArr;');
- Add('begin');
- Add(' p:=a;');
- SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
- nIncompatibleTypesGotExpected);
- ConvertProgram;
- end;
- procedure TTestModule.TestPointer_ArrayParamsFail;
- begin
- StartProgram(false);
- Add('var');
- Add(' p: Pointer;');
- Add('begin');
- Add(' p:=p[1];');
- SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
- ConvertProgram;
- end;
- procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
- begin
- StartProgram(false);
- Add([
- 'procedure DoIt(args: array of jsvalue); begin end;',
- 'procedure DoAll; varargs; begin end;',
- 'var',
- ' v: jsvalue;',
- 'begin',
- ' DoIt([pointer(v)]);',
- ' DoAll(pointer(v));',
- '']);
- ConvertProgram;
- CheckSource('TestPointer_TypeCastJSValueToPointer',
- LinesToStr([ // statements
- 'this.DoIt = function (args) {',
- '};',
- 'this.DoAll = function () {',
- '};',
- 'this.v = undefined;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.DoIt([$mod.v]);',
- '$mod.DoAll($mod.v);',
- '']));
- end;
- procedure TTestModule.TestJSValue_AssignToJSValue;
- begin
- StartProgram(false);
- Add('var');
- Add(' v: jsvalue;');
- Add(' i: longint;');
- Add(' s: string;');
- Add(' b: boolean;');
- Add(' d: double;');
- Add(' p: pointer;');
- Add('begin');
- Add(' v:=v;');
- Add(' v:=1;');
- Add(' v:=i;');
- Add(' v:='''';');
- Add(' v:=''c'';');
- Add(' v:=''foo'';');
- Add(' v:=s;');
- Add(' v:=false;');
- Add(' v:=true;');
- Add(' v:=b;');
- Add(' v:=0.1;');
- Add(' v:=d;');
- Add(' v:=nil;');
- Add(' v:=p;');
- ConvertProgram;
- CheckSource('TestJSValue_AssignToJSValue',
- LinesToStr([ // statements
- 'this.v = undefined;',
- 'this.i = 0;',
- 'this.s = "";',
- 'this.b = false;',
- 'this.d = 0.0;',
- 'this.p = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.v = $mod.v;',
- '$mod.v = 1;',
- '$mod.v = $mod.i;',
- '$mod.v = "";',
- '$mod.v = "c";',
- '$mod.v = "foo";',
- '$mod.v = $mod.s;',
- '$mod.v = false;',
- '$mod.v = true;',
- '$mod.v = $mod.b;',
- '$mod.v = 0.1;',
- '$mod.v = $mod.d;',
- '$mod.v = null;',
- '$mod.v = $mod.p;',
- '']));
- end;
- procedure TTestModule.TestJSValue_TypeCastToBaseType;
- begin
- StartProgram(false);
- Add('type');
- Add(' integer = longint;');
- Add(' TYesNo = boolean;');
- Add(' TFloat = double;');
- Add(' TCaption = string;');
- Add(' TChar = char;');
- Add('var');
- Add(' v: jsvalue;');
- Add(' i: integer;');
- Add(' s: TCaption;');
- Add(' b: TYesNo;');
- Add(' d: TFloat;');
- Add(' c: char;');
- Add('begin');
- Add(' i:=longint(v);');
- Add(' i:=integer(v);');
- Add(' s:=string(v);');
- Add(' s:=TCaption(v);');
- Add(' b:=boolean(v);');
- Add(' b:=TYesNo(v);');
- Add(' d:=double(v);');
- Add(' d:=TFloat(v);');
- Add(' c:=char(v);');
- Add(' c:=TChar(v);');
- ConvertProgram;
- CheckSource('TestJSValue_TypeCastToBaseType',
- LinesToStr([ // statements
- 'this.v = undefined;',
- 'this.i = 0;',
- 'this.s = "";',
- 'this.b = false;',
- 'this.d = 0.0;',
- 'this.c = "";',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.i = Math.floor($mod.v);',
- '$mod.i = Math.floor($mod.v);',
- '$mod.s = "" + $mod.v;',
- '$mod.s = "" + $mod.v;',
- '$mod.b = !($mod.v == false);',
- '$mod.b = !($mod.v == false);',
- '$mod.d = rtl.getNumber($mod.v);',
- '$mod.d = rtl.getNumber($mod.v);',
- '$mod.c = rtl.getChar($mod.v);',
- '$mod.c = rtl.getChar($mod.v);',
- '']));
- end;
- procedure TTestModule.TestJSValue_Equal;
- begin
- StartProgram(false);
- Add('type');
- Add(' integer = longint;');
- Add(' TYesNo = boolean;');
- Add(' TFloat = double;');
- Add(' TCaption = string;');
- Add(' TChar = char;');
- Add(' TMulti = JSValue;');
- Add('var');
- Add(' v: jsvalue;');
- Add(' i: integer;');
- Add(' s: TCaption;');
- Add(' b: TYesNo;');
- Add(' d: TFloat;');
- Add(' c: char;');
- Add(' m: TMulti;');
- Add('begin');
- Add(' b:=v=v;');
- Add(' b:=v<>v;');
- Add(' b:=v=1;');
- Add(' b:=v<>1;');
- Add(' b:=2=v;');
- Add(' b:=2<>v;');
- Add(' b:=v=i;');
- Add(' b:=i=v;');
- Add(' b:=v=nil;');
- Add(' b:=nil=v;');
- Add(' b:=v=false;');
- Add(' b:=true=v;');
- Add(' b:=v=b;');
- Add(' b:=b=v;');
- Add(' b:=v=s;');
- Add(' b:=s=v;');
- Add(' b:=v=''foo'';');
- Add(' b:=''''=v;');
- Add(' b:=v=d;');
- Add(' b:=d=v;');
- Add(' b:=v=3.4;');
- Add(' b:=5.6=v;');
- Add(' b:=v=c;');
- Add(' b:=c=v;');
- Add(' b:=m=m;');
- Add(' b:=v=m;');
- Add(' b:=m=v;');
- ConvertProgram;
- CheckSource('TestJSValue_Equal',
- LinesToStr([ // statements
- 'this.v = undefined;',
- 'this.i = 0;',
- 'this.s = "";',
- 'this.b = false;',
- 'this.d = 0.0;',
- 'this.c = "";',
- 'this.m = undefined;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.b = $mod.v == $mod.v;',
- '$mod.b = $mod.v != $mod.v;',
- '$mod.b = $mod.v == 1;',
- '$mod.b = $mod.v != 1;',
- '$mod.b = 2 == $mod.v;',
- '$mod.b = 2 != $mod.v;',
- '$mod.b = $mod.v == $mod.i;',
- '$mod.b = $mod.i == $mod.v;',
- '$mod.b = $mod.v == null;',
- '$mod.b = null == $mod.v;',
- '$mod.b = $mod.v == false;',
- '$mod.b = true == $mod.v;',
- '$mod.b = $mod.v == $mod.b;',
- '$mod.b = $mod.b == $mod.v;',
- '$mod.b = $mod.v == $mod.s;',
- '$mod.b = $mod.s == $mod.v;',
- '$mod.b = $mod.v == "foo";',
- '$mod.b = "" == $mod.v;',
- '$mod.b = $mod.v == $mod.d;',
- '$mod.b = $mod.d == $mod.v;',
- '$mod.b = $mod.v == 3.4;',
- '$mod.b = 5.6 == $mod.v;',
- '$mod.b = $mod.v == $mod.c;',
- '$mod.b = $mod.c == $mod.v;',
- '$mod.b = $mod.m == $mod.m;',
- '$mod.b = $mod.v == $mod.m;',
- '$mod.b = $mod.m == $mod.v;',
- '']));
- end;
- procedure TTestModule.TestJSValue_If;
- begin
- StartProgram(false);
- Add([
- 'var',
- ' v: jsvalue;',
- 'begin',
- ' if v then ;',
- ' while v do ;',
- ' repeat until v;',
- '']);
- ConvertProgram;
- CheckSource('TestJSValue_If',
- LinesToStr([ // statements
- 'this.v = undefined;',
- '']),
- LinesToStr([ // $mod.$main
- 'if ($mod.v) ;',
- 'while($mod.v){',
- '};',
- 'do{',
- '} while(!$mod.v);',
- '']));
- end;
- procedure TTestModule.TestJSValue_Enum;
- begin
- StartProgram(false);
- Add('type');
- Add(' TColor = (red, blue);');
- Add(' TRedBlue = TColor;');
- Add('var');
- Add(' v: jsvalue;');
- Add(' e: TColor;');
- Add('begin');
- Add(' v:=e;');
- Add(' v:=TColor(e);');
- Add(' v:=TRedBlue(e);');
- Add(' e:=TColor(v);');
- Add(' e:=TRedBlue(v);');
- ConvertProgram;
- CheckSource('TestJSValue_Enum',
- LinesToStr([ // statements
- 'this.TColor = {',
- ' "0": "red",',
- ' red: 0,',
- ' "1": "blue",',
- ' blue: 1',
- '};',
- 'this.v = undefined;',
- 'this.e = 0;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.v = $mod.e;',
- '$mod.v = $mod.e;',
- '$mod.v = $mod.e;',
- '$mod.e = $mod.v;',
- '$mod.e = $mod.v;',
- '']));
- end;
- procedure TTestModule.TestJSValue_ClassInstance;
- begin
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' end;');
- Add(' TBirdObject = TObject;');
- Add('var');
- Add(' v: jsvalue;');
- Add(' o: TObject;');
- Add('begin');
- Add(' v:=o;');
- Add(' v:=TObject(o);');
- Add(' v:=TBirdObject(o);');
- Add(' o:=TObject(v);');
- Add(' o:=TBirdObject(v);');
- ConvertProgram;
- CheckSource('TestJSValue_ClassInstance',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'this.v = undefined;',
- 'this.o = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.v = $mod.o;',
- '$mod.v = $mod.o;',
- '$mod.v = $mod.o;',
- '$mod.o = rtl.getObject($mod.v);',
- '$mod.o = rtl.getObject($mod.v);',
- '']));
- end;
- procedure TTestModule.TestJSValue_ClassOf;
- begin
- StartProgram(false);
- Add('type');
- Add(' TClass = class of TObject;');
- Add(' TObject = class');
- Add(' end;');
- Add(' TBirds = class of TBird;');
- Add(' TBird = class(TObject) end;');
- Add('var');
- Add(' v: jsvalue;');
- Add(' c: TClass;');
- Add('begin');
- Add(' v:=c;');
- Add(' v:=TObject;');
- Add(' v:=TClass(c);');
- Add(' v:=TBirds(c);');
- Add(' c:=TClass(v);');
- Add(' c:=TBirds(v);');
- ConvertProgram;
- CheckSource('TestJSValue_ClassOf',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
- '});',
- 'this.v = undefined;',
- 'this.c = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.v = $mod.c;',
- '$mod.v = $mod.TObject;',
- '$mod.v = $mod.c;',
- '$mod.v = $mod.c;',
- '$mod.c = rtl.getObject($mod.v);',
- '$mod.c = rtl.getObject($mod.v);',
- '']));
- end;
- procedure TTestModule.TestJSValue_ArrayOfJSValue;
- begin
- StartProgram(false);
- Add('type');
- Add(' integer = longint;');
- Add(' TArray = array of JSValue;');
- Add(' TArrgh = tarray;');
- Add(' TArrInt = array of integer;');
- Add('var');
- Add(' v: jsvalue;');
- Add(' TheArray: tarray;');
- Add(' Arr: tarrgh;');
- Add(' i: integer;');
- Add(' ArrInt: tarrint;');
- Add('begin');
- Add(' arr:=thearray;');
- Add(' thearray:=arr;');
- Add(' setlength(arr,2);');
- Add(' setlength(thearray,3);');
- Add(' arr[4]:=v;');
- Add(' arr[5]:=length(thearray);');
- Add(' arr[6]:=nil;');
- Add(' arr[7]:=thearray[8];');
- Add(' arr[low(arr)]:=high(thearray);');
- Add(' arr:=arrint;');
- Add(' arrInt:=tarrint(arr);');
- Add(' if TheArray = nil then ;');
- Add(' if nil = TheArray then ;');
- Add(' if TheArray <> nil then ;');
- Add(' if nil <> TheArray then ;');
- ConvertProgram;
- CheckSource('TestJSValue_ArrayOfJSValue',
- LinesToStr([ // statements
- 'this.v = undefined;',
- 'this.TheArray = [];',
- 'this.Arr = [];',
- 'this.i = 0;',
- 'this.ArrInt = [];',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.Arr = $mod.TheArray;',
- '$mod.TheArray = $mod.Arr;',
- '$mod.Arr = rtl.arraySetLength($mod.Arr,2,undefined);',
- '$mod.TheArray = rtl.arraySetLength($mod.TheArray,3,undefined);',
- '$mod.Arr[4] = $mod.v;',
- '$mod.Arr[5] = rtl.length($mod.TheArray);',
- '$mod.Arr[6] = null;',
- '$mod.Arr[7] = $mod.TheArray[8];',
- '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
- '$mod.Arr = $mod.ArrInt;',
- '$mod.ArrInt = $mod.Arr;',
- 'if (rtl.length($mod.TheArray) == 0) ;',
- 'if (rtl.length($mod.TheArray) == 0) ;',
- 'if (rtl.length($mod.TheArray) > 0) ;',
- 'if (rtl.length($mod.TheArray) > 0) ;',
- '']));
- end;
- procedure TTestModule.TestJSValue_Params;
- begin
- StartProgram(false);
- Add('type');
- Add(' integer = longint;');
- Add(' TYesNo = boolean;');
- Add(' TFloat = double;');
- Add(' TCaption = string;');
- Add(' TChar = char;');
- Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
- Add('var');
- Add(' l: jsvalue;');
- Add('begin');
- Add(' a:=a;');
- Add(' l:=b;');
- Add(' c:=c;');
- Add(' d:=d;');
- Add(' Result:=l;');
- Add('end;');
- Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
- Add('var');
- Add(' v: jsvalue;');
- Add(' i: integer;');
- Add(' b: TYesNo;');
- Add(' d: TFloat;');
- Add(' s: TCaption;');
- Add(' c: TChar;');
- Add('begin');
- Add(' v:=doit(v,v,v,v);');
- Add(' i:=integer(dosome(i,i));');
- Add(' b:=TYesNo(dosome(b,b));');
- Add(' d:=TFloat(dosome(d,d));');
- Add(' s:=TCaption(dosome(s,s));');
- Add(' c:=TChar(dosome(c,c));');
- ConvertProgram;
- CheckSource('TestJSValue_Params',
- LinesToStr([ // statements
- 'this.DoIt = function (a, b, c, d) {',
- ' var Result = undefined;',
- ' var l = undefined;',
- ' a = a;',
- ' l = b;',
- ' c.set(c.get());',
- ' d.set(d.get());',
- ' Result = l;',
- ' return Result;',
- '};',
- 'this.DoSome = function (a, b) {',
- ' var Result = undefined;',
- ' return Result;',
- '};',
- 'this.v = undefined;',
- 'this.i = 0;',
- 'this.b = false;',
- 'this.d = 0.0;',
- 'this.s = "";',
- 'this.c = "";',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
- ' p: $mod,',
- ' get: function () {',
- ' return this.p.v;',
- ' },',
- ' set: function (v) {',
- ' this.p.v = v;',
- ' }',
- '}, {',
- ' p: $mod,',
- ' get: function () {',
- ' return this.p.v;',
- ' },',
- ' set: function (v) {',
- ' this.p.v = v;',
- ' }',
- '});',
- '$mod.i = Math.floor($mod.DoSome($mod.i, $mod.i));',
- '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
- '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
- '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
- '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
- '']));
- end;
- procedure TTestModule.TestJSValue_UntypedParam;
- begin
- StartProgram(false);
- Add('function DoIt(const a; var b; out c): jsvalue;');
- Add('begin');
- Add(' Result:=a;');
- Add(' Result:=b;');
- Add(' Result:=c;');
- Add(' b:=Result;');
- Add(' c:=Result;');
- Add('end;');
- Add('var i: longint;');
- Add('begin');
- Add(' doit(i,i,i);');
- ConvertProgram;
- CheckSource('TestJSValue_UntypedParam',
- LinesToStr([ // statements
- 'this.DoIt = function (a, b, c) {',
- ' var Result = undefined;',
- ' Result = a;',
- ' Result = b.get();',
- ' Result = c.get();',
- ' b.set(Result);',
- ' c.set(Result);',
- ' return Result;',
- '};',
- 'this.i = 0;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.DoIt($mod.i, {',
- ' p: $mod,',
- ' get: function () {',
- ' return this.p.i;',
- ' },',
- ' set: function (v) {',
- ' this.p.i = v;',
- ' }',
- '}, {',
- ' p: $mod,',
- ' get: function () {',
- ' return this.p.i;',
- ' },',
- ' set: function (v) {',
- ' this.p.i = v;',
- ' }',
- '});',
- '']));
- end;
- procedure TTestModule.TestJSValue_FuncResultType;
- begin
- StartProgram(false);
- Add('type');
- Add(' integer = longint;');
- Add(' TJSValueArray = array of JSValue;');
- Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
- Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
- Add('begin');
- Add(' while Compare(P,aList[0])>0 do ;');
- Add('end;');
- Add('var');
- Add(' Compare: TListSortCompare;');
- Add(' V: JSValue;');
- Add(' i: integer;');
- Add('begin');
- Add(' if Compare(V,V)>0 then ;');
- Add(' if Compare(i,i)>1 then ;');
- Add(' if Compare(nil,false)>2 then ;');
- Add(' if Compare(1,true)>3 then ;');
- ConvertProgram;
- CheckSource('TestJSValue_UntypedParam',
- LinesToStr([ // statements
- 'this.Sort = function (P, aList, Compare) {',
- ' while (Compare(P, aList[0]) > 0) {',
- ' };',
- '};',
- 'this.Compare = null;',
- 'this.V = undefined;',
- 'this.i = 0;',
- '']),
- LinesToStr([ // $mod.$main
- 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
- 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
- 'if ($mod.Compare(null, false) > 2) ;',
- 'if ($mod.Compare(1, true) > 3) ;',
- '']));
- end;
- procedure TTestModule.TestJSValue_ProcType_Assign;
- begin
- StartProgram(false);
- Add('type');
- Add(' integer = longint;');
- Add(' TObject = class');
- Add(' class function GetGlob: integer;');
- Add(' function Getter: integer;');
- Add(' end;');
- Add('class function TObject.GetGlob: integer;');
- Add('var v1: jsvalue;');
- Add('begin');
- Add(' v1:=@GetGlob;');
- Add(' v1:[email protected];');
- Add('end;');
- Add('function TObject.Getter: integer;');
- Add('var v2: jsvalue;');
- Add('begin');
- Add(' v2:=@Getter;');
- Add(' v2:[email protected];');
- Add(' v2:=@GetGlob;');
- Add(' v2:[email protected];');
- Add('end;');
- Add('function GetIt(i: integer): integer;');
- Add('var v3: jsvalue;');
- Add('begin');
- Add(' v3:=@GetIt;');
- Add('end;');
- Add('var');
- Add(' V: JSValue;');
- Add(' o: TObject;');
- Add('begin');
- Add(' v:=@GetIt;');
- Add(' v:[email protected];');
- Add(' v:[email protected];');
- ConvertProgram;
- CheckSource('TestJSValue_ProcType_Assign',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.GetGlob = function () {',
- ' var Result = 0;',
- ' var v1 = undefined;',
- ' v1 = rtl.createCallback(this, "GetGlob");',
- ' v1 = rtl.createCallback(this, "GetGlob");',
- ' return Result;',
- ' };',
- ' this.Getter = function () {',
- ' var Result = 0;',
- ' var v2 = undefined;',
- ' v2 = rtl.createCallback(this, "Getter");',
- ' v2 = rtl.createCallback(this, "Getter");',
- ' v2 = rtl.createCallback(this.$class, "GetGlob");',
- ' v2 = rtl.createCallback(this.$class, "GetGlob");',
- ' return Result;',
- ' };',
- '});',
- 'this.GetIt = function (i) {',
- ' var Result = 0;',
- ' var v3 = undefined;',
- ' v3 = $mod.GetIt;',
- ' return Result;',
- '};',
- 'this.V = undefined;',
- 'this.o = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.V = $mod.GetIt;',
- '$mod.V = rtl.createCallback($mod.o, "Getter");',
- '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
- '']));
- end;
- procedure TTestModule.TestJSValue_ProcType_Equal;
- begin
- StartProgram(false);
- Add('type');
- Add(' integer = longint;');
- Add(' TObject = class');
- Add(' class function GetGlob: integer;');
- Add(' function Getter: integer;');
- Add(' end;');
- Add('class function TObject.GetGlob: integer;');
- Add('var v1: jsvalue;');
- Add('begin');
- Add(' if v1=@GetGlob then;');
- Add(' if [email protected] then ;');
- Add('end;');
- Add('function TObject.Getter: integer;');
- Add('var v2: jsvalue;');
- Add('begin');
- Add(' if v2=@Getter then;');
- Add(' if [email protected] then ;');
- Add(' if v2=@GetGlob then;');
- Add(' if [email protected] then;');
- Add('end;');
- Add('function GetIt(i: integer): integer;');
- Add('var v3: jsvalue;');
- Add('begin');
- Add(' if v3=@GetIt then;');
- Add('end;');
- Add('var');
- Add(' V: JSValue;');
- Add(' o: TObject;');
- Add('begin');
- Add(' if v=@GetIt then;');
- Add(' if [email protected] then;');
- Add(' if [email protected] then;');
- Add(' if @GetIt=v then;');
- Add(' if @o.Getter=v then;');
- Add(' if @o.GetGlob=v then;');
- ConvertProgram;
- CheckSource('TestJSValue_ProcType_Equal',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.GetGlob = function () {',
- ' var Result = 0;',
- ' var v1 = undefined;',
- ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
- ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
- ' return Result;',
- ' };',
- ' this.Getter = function () {',
- ' var Result = 0;',
- ' var v2 = undefined;',
- ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
- ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
- ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
- ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
- ' return Result;',
- ' };',
- '});',
- 'this.GetIt = function (i) {',
- ' var Result = 0;',
- ' var v3 = undefined;',
- ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
- ' return Result;',
- '};',
- 'this.V = undefined;',
- 'this.o = null;',
- '']),
- LinesToStr([ // $mod.$main
- 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
- 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
- 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
- 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
- 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
- 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
- '']));
- end;
- procedure TTestModule.TestJSValue_AssignToPointerFail;
- begin
- StartProgram(false);
- Add([
- 'var',
- ' v: JSValue;',
- ' p: Pointer;',
- 'begin',
- ' p:=v;',
- '']);
- SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
- nIncompatibleTypesGotExpected);
- ConvertProgram;
- end;
- procedure TTestModule.TestJSValue_OverloadDouble;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' integer = longint;',
- ' tdatetime = double;',
- 'procedure DoIt(d: double); begin end;',
- 'procedure DoIt(v: jsvalue); begin end;',
- 'var',
- ' d: double;',
- ' dt: tdatetime;',
- ' i: integer;',
- ' b: byte;',
- ' shi: shortint;',
- ' w: word;',
- ' smi: smallint;',
- ' lw: longword;',
- ' li: longint;',
- ' ni: nativeint;',
- ' nu: nativeuint;',
- 'begin',
- ' DoIt(d);',
- ' DoIt(dt);',
- ' DoIt(i);',
- ' DoIt(b);',
- ' DoIt(shi);',
- ' DoIt(w);',
- ' DoIt(smi);',
- ' DoIt(lw);',
- ' DoIt(li);',
- ' DoIt(ni);',
- ' DoIt(nu);',
- '']);
- ConvertProgram;
- CheckSource('TestJSValue_OverloadDouble',
- LinesToStr([ // statements
- 'this.DoIt = function (d) {',
- '};',
- 'this.DoIt$1 = function (v) {',
- '};',
- 'this.d = 0.0;',
- 'this.dt = 0.0;',
- 'this.i = 0;',
- 'this.b = 0;',
- 'this.shi = 0;',
- 'this.w = 0;',
- 'this.smi = 0;',
- 'this.lw = 0;',
- 'this.li = 0;',
- 'this.ni = 0;',
- 'this.nu = 0;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.DoIt($mod.d);',
- '$mod.DoIt($mod.dt);',
- '$mod.DoIt($mod.i);',
- '$mod.DoIt($mod.b);',
- '$mod.DoIt($mod.shi);',
- '$mod.DoIt($mod.w);',
- '$mod.DoIt($mod.smi);',
- '$mod.DoIt($mod.lw);',
- '$mod.DoIt($mod.li);',
- '$mod.DoIt($mod.ni);',
- '$mod.DoIt($mod.nu);',
- '']));
- end;
- procedure TTestModule.TestJSValue_OverloadNativeInt;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' integer = longint;',
- ' int53 = nativeint;',
- ' tdatetime = double;',
- 'procedure DoIt(n: nativeint); begin end;',
- 'procedure DoIt(v: jsvalue); begin end;',
- 'var',
- ' d: double;',
- ' dt: tdatetime;',
- ' i: integer;',
- ' b: byte;',
- ' shi: shortint;',
- ' w: word;',
- ' smi: smallint;',
- ' lw: longword;',
- ' li: longint;',
- ' ni: nativeint;',
- ' nu: nativeuint;',
- 'begin',
- ' DoIt(d);',
- ' DoIt(dt);',
- ' DoIt(i);',
- ' DoIt(b);',
- ' DoIt(shi);',
- ' DoIt(w);',
- ' DoIt(smi);',
- ' DoIt(lw);',
- ' DoIt(li);',
- ' DoIt(ni);',
- ' DoIt(nu);',
- '']);
- ConvertProgram;
- CheckSource('TestJSValue_OverloadNativeInt',
- LinesToStr([ // statements
- 'this.DoIt = function (n) {',
- '};',
- 'this.DoIt$1 = function (v) {',
- '};',
- 'this.d = 0.0;',
- 'this.dt = 0.0;',
- 'this.i = 0;',
- 'this.b = 0;',
- 'this.shi = 0;',
- 'this.w = 0;',
- 'this.smi = 0;',
- 'this.lw = 0;',
- 'this.li = 0;',
- 'this.ni = 0;',
- 'this.nu = 0;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.DoIt$1($mod.d);',
- '$mod.DoIt$1($mod.dt);',
- '$mod.DoIt($mod.i);',
- '$mod.DoIt($mod.b);',
- '$mod.DoIt($mod.shi);',
- '$mod.DoIt($mod.w);',
- '$mod.DoIt($mod.smi);',
- '$mod.DoIt($mod.lw);',
- '$mod.DoIt($mod.li);',
- '$mod.DoIt($mod.ni);',
- '$mod.DoIt($mod.nu);',
- '']));
- end;
- procedure TTestModule.TestJSValue_OverloadWord;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' integer = longint;',
- ' int53 = nativeint;',
- ' tdatetime = double;',
- 'procedure DoIt(w: word); begin end;',
- 'procedure DoIt(v: jsvalue); begin end;',
- 'var',
- ' d: double;',
- ' dt: tdatetime;',
- ' i: integer;',
- ' b: byte;',
- ' shi: shortint;',
- ' w: word;',
- ' smi: smallint;',
- ' lw: longword;',
- ' li: longint;',
- ' ni: nativeint;',
- ' nu: nativeuint;',
- 'begin',
- ' DoIt(d);',
- ' DoIt(dt);',
- ' DoIt(i);',
- ' DoIt(b);',
- ' DoIt(shi);',
- ' DoIt(w);',
- ' DoIt(smi);',
- ' DoIt(lw);',
- ' DoIt(li);',
- ' DoIt(ni);',
- ' DoIt(nu);',
- '']);
- ConvertProgram;
- CheckSource('TestJSValue_OverloadWord',
- LinesToStr([ // statements
- 'this.DoIt = function (w) {',
- '};',
- 'this.DoIt$1 = function (v) {',
- '};',
- 'this.d = 0.0;',
- 'this.dt = 0.0;',
- 'this.i = 0;',
- 'this.b = 0;',
- 'this.shi = 0;',
- 'this.w = 0;',
- 'this.smi = 0;',
- 'this.lw = 0;',
- 'this.li = 0;',
- 'this.ni = 0;',
- 'this.nu = 0;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.DoIt$1($mod.d);',
- '$mod.DoIt$1($mod.dt);',
- '$mod.DoIt$1($mod.i);',
- '$mod.DoIt($mod.b);',
- '$mod.DoIt($mod.shi);',
- '$mod.DoIt($mod.w);',
- '$mod.DoIt$1($mod.smi);',
- '$mod.DoIt$1($mod.lw);',
- '$mod.DoIt$1($mod.li);',
- '$mod.DoIt$1($mod.ni);',
- '$mod.DoIt$1($mod.nu);',
- '']));
- end;
- procedure TTestModule.TestJSValue_OverloadString;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' uni = string;',
- ' WideChar = char;',
- 'procedure DoIt(s: string); begin end;',
- 'procedure DoIt(v: jsvalue); begin end;',
- 'var',
- ' s: string;',
- ' c: char;',
- ' u: uni;',
- 'begin',
- ' DoIt(s);',
- ' DoIt(c);',
- ' DoIt(u);',
- '']);
- ConvertProgram;
- CheckSource('TestJSValue_OverloadString',
- LinesToStr([ // statements
- 'this.DoIt = function (s) {',
- '};',
- 'this.DoIt$1 = function (v) {',
- '};',
- 'this.s = "";',
- 'this.c = "";',
- 'this.u = "";',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.DoIt($mod.s);',
- '$mod.DoIt($mod.c);',
- '$mod.DoIt($mod.u);',
- '']));
- end;
- procedure TTestModule.TestJSValue_OverloadChar;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' uni = string;',
- ' WideChar = char;',
- 'procedure DoIt(c: char); begin end;',
- 'procedure DoIt(v: jsvalue); begin end;',
- 'var',
- ' s: string;',
- ' c: char;',
- ' u: uni;',
- 'begin',
- ' DoIt(s);',
- ' DoIt(c);',
- ' DoIt(u);',
- '']);
- ConvertProgram;
- CheckSource('TestJSValue_OverloadChar',
- LinesToStr([ // statements
- 'this.DoIt = function (c) {',
- '};',
- 'this.DoIt$1 = function (v) {',
- '};',
- 'this.s = "";',
- 'this.c = "";',
- 'this.u = "";',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.DoIt$1($mod.s);',
- '$mod.DoIt($mod.c);',
- '$mod.DoIt$1($mod.u);',
- '']));
- end;
- procedure TTestModule.TestJSValue_OverloadPointer;
- begin
- StartProgram(false);
- Add([
- 'type',
- ' TObject = class end;',
- 'procedure DoIt(p: pointer); begin end;',
- 'procedure DoIt(v: jsvalue); begin end;',
- 'var',
- ' o: TObject;',
- 'begin',
- ' DoIt(o);',
- '']);
- ConvertProgram;
- CheckSource('TestJSValue_OverloadPointer',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- 'this.DoIt = function (p) {',
- '};',
- 'this.DoIt$1 = function (v) {',
- '};',
- 'this.o = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.DoIt($mod.o);',
- '']));
- end;
- procedure TTestModule.TestRTTI_ProcType;
- begin
- Converter.Options:=Converter.Options-[coNoTypeInfo];
- StartProgram(false);
- Add('type');
- Add(' TProcA = procedure;');
- Add(' TMethodB = procedure of object;');
- Add(' TProcC = procedure; varargs;');
- Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
- Add(' TProcE = function: nativeint;');
- Add(' TProcF = function(const p: TProcA): nativeuint;');
- Add('var p: pointer;');
- Add('begin');
- Add(' p:=typeinfo(tproca);');
- ConvertProgram;
- CheckSource('TestRTTI_ProcType',
- LinesToStr([ // statements
- '$mod.$rtti.$ProcVar("TProcA", {',
- ' procsig: rtl.newTIProcSig(null)',
- '});',
- '$mod.$rtti.$MethodVar("TMethodB", {',
- ' procsig: rtl.newTIProcSig(null),',
- ' methodkind: 0',
- '});',
- '$mod.$rtti.$ProcVar("TProcC", {',
- ' procsig: rtl.newTIProcSig(null, 2)',
- '});',
- '$mod.$rtti.$ProcVar("TProcD", {',
- ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
- '});',
- '$mod.$rtti.$ProcVar("TProcE", {',
- ' procsig: rtl.newTIProcSig(null, rtl.nativeint)',
- '});',
- '$mod.$rtti.$ProcVar("TProcF", {',
- ' procsig: rtl.newTIProcSig([["p", $mod.$rtti["TProcA"], 2]], rtl.nativeuint)',
- '});',
- 'this.p = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.p = $mod.$rtti["TProcA"];',
- '']));
- end;
- procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
- begin
- Converter.Options:=Converter.Options-[coNoTypeInfo];
- AddModuleWithIntfImplSrc('unit2.pas',
- LinesToStr([
- 'type',
- ' TObject = class end;'
- ]),
- '');
- StartUnit(true);
- Add('interface');
- Add('uses unit2;');
- Add('type');
- Add(' TProcA = function(o: tobject): tobject;');
- Add('implementation');
- Add('type');
- Add(' TProcB = function(o: tobject): tobject;');
- Add('var p: Pointer;');
- Add('initialization');
- Add(' p:=typeinfo(tproca);');
- Add(' p:=typeinfo(tprocb);');
- ConvertUnit;
- CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
- LinesToStr([ // statements
- 'var $impl = $mod.$impl;',
- '$mod.$rtti.$ProcVar("TProcA", {',
- ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
- '});',
- '']),
- LinesToStr([ // this.$init
- '$impl.p = $mod.$rtti["TProcA"];',
- '$impl.p = $mod.$rtti["TProcB"];',
- '']),
- LinesToStr([ // implementation
- '$mod.$rtti.$ProcVar("TProcB", {',
- ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
- '});',
- '$impl.p = null;',
- '']) );
- end;
- procedure TTestModule.TestRTTI_EnumAndSetType;
- begin
- Converter.Options:=Converter.Options-[coNoTypeInfo];
- StartProgram(false);
- Add('type');
- Add(' TFlag = (light,dark);');
- Add(' TFlags = set of TFlag;');
- Add(' TProc = function(f: TFlags): TFlag;');
- Add('var p: pointer;');
- Add('begin');
- Add(' p:=typeinfo(tflag);');
- Add(' p:=typeinfo(tflags);');
- ConvertProgram;
- CheckSource('TestRTTI_EnumAndType',
- LinesToStr([ // statements
- 'this.TFlag = {',
- ' "0": "light",',
- ' light: 0,',
- ' "1": "dark",',
- ' dark: 1',
- '};',
- '$mod.$rtti.$Enum("TFlag", {',
- ' minvalue: 0,',
- ' maxvalue: 1,',
- ' enumtype: this.TFlag',
- '});',
- '$mod.$rtti.$Set("TFlags", {',
- ' comptype: $mod.$rtti["TFlag"]',
- '});',
- '$mod.$rtti.$ProcVar("TProc", {',
- ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TFlags"]]], $mod.$rtti["TFlag"])',
- '});',
- 'this.p = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.p = $mod.$rtti["TFlag"];',
- '$mod.p = $mod.$rtti["TFlags"];',
- '']));
- end;
- procedure TTestModule.TestRTTI_AnonymousEnumType;
- begin
- Converter.Options:=Converter.Options-[coNoTypeInfo];
- StartProgram(false);
- Add('type');
- Add(' TFlags = set of (red, green);');
- Add('var');
- Add(' f: TFlags;');
- Add('begin');
- Add(' Include(f,red);');
- ConvertProgram;
- CheckSource('TestRTTI_AnonymousEnumType',
- LinesToStr([ // statements
- 'this.TFlags$a = {',
- ' "0": "red",',
- ' red: 0,',
- ' "1": "green",',
- ' green: 1',
- '};',
- '$mod.$rtti.$Enum("TFlags$a", {',
- ' minvalue: 0,',
- ' maxvalue: 1,',
- ' enumtype: this.TFlags$a',
- '});',
- '$mod.$rtti.$Set("TFlags", {',
- ' comptype: $mod.$rtti["TFlags$a"]',
- '});',
- 'this.f = {};',
- '']),
- LinesToStr([
- '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
- '']));
- end;
- procedure TTestModule.TestRTTI_StaticArray;
- begin
- Converter.Options:=Converter.Options-[coNoTypeInfo];
- StartProgram(false);
- Add('type');
- Add(' TFlag = (light,dark);');
- Add(' TFlagNames = array[TFlag] of string;');
- Add(' TBoolNames = array[boolean] of string;');
- Add(' TProc = function(f: TBoolNames): TFlagNames;');
- Add('var p: pointer;');
- Add('begin');
- Add(' p:=typeinfo(TFlagNames);');
- Add(' p:=typeinfo(TBoolNames);');
- ConvertProgram;
- CheckSource('TestRTTI_StaticArray',
- LinesToStr([ // statements
- 'this.TFlag = {',
- ' "0": "light",',
- ' light: 0,',
- ' "1": "dark",',
- ' dark: 1',
- '};',
- '$mod.$rtti.$Enum("TFlag", {',
- ' minvalue: 0,',
- ' maxvalue: 1,',
- ' enumtype: this.TFlag',
- '});',
- '$mod.$rtti.$StaticArray("TFlagNames", {',
- ' dims: [2],',
- ' eltype: rtl.string',
- '});',
- '$mod.$rtti.$StaticArray("TBoolNames", {',
- ' dims: [2],',
- ' eltype: rtl.string',
- '});',
- '$mod.$rtti.$ProcVar("TProc", {',
- ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TBoolNames"]]], $mod.$rtti["TFlagNames"])',
- '});',
- 'this.p = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.p = $mod.$rtti["TFlagNames"];',
- '$mod.p = $mod.$rtti["TBoolNames"];',
- '']));
- end;
- procedure TTestModule.TestRTTI_DynArray;
- begin
- Converter.Options:=Converter.Options-[coNoTypeInfo];
- StartProgram(false);
- Add('type');
- Add(' TArrStr = array of string;');
- Add(' TArr2Dim = array of tarrstr;');
- Add(' TProc = function(f: TArrStr): TArr2Dim;');
- Add('var p: pointer;');
- Add('begin');
- Add(' p:=typeinfo(tarrstr);');
- Add(' p:=typeinfo(tarr2dim);');
- ConvertProgram;
- CheckSource('TestRTTI_DynArray',
- LinesToStr([ // statements
- '$mod.$rtti.$DynArray("TArrStr", {',
- ' eltype: rtl.string',
- '});',
- '$mod.$rtti.$DynArray("TArr2Dim", {',
- ' eltype: $mod.$rtti["TArrStr"]',
- '});',
- '$mod.$rtti.$ProcVar("TProc", {',
- ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TArrStr"]]], $mod.$rtti["TArr2Dim"])',
- '});',
- 'this.p = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.p = $mod.$rtti["TArrStr"];',
- '$mod.p = $mod.$rtti["TArr2Dim"];',
- '']));
- end;
- procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
- begin
- Converter.Options:=Converter.Options-[coNoTypeInfo];
- StartProgram(false);
- Add('type');
- Add(' TArr = array of array of longint;');
- Add('var a: TArr;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestRTTI_ArrayNestedAnonymous',
- LinesToStr([ // statements
- '$mod.$rtti.$DynArray("TArr$a", {',
- ' eltype: rtl.longint',
- '});',
- '$mod.$rtti.$DynArray("TArr", {',
- ' eltype: $mod.$rtti["TArr$a"]',
- '});',
- 'this.a = [];',
- '']),
- LinesToStr([ // $mod.$main
- ]));
- end;
- procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
- begin
- Converter.Options:=Converter.Options-[coNoTypeInfo];
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' published');
- Add(' procedure Proc; virtual; abstract;');
- Add(' procedure Proc(Sender: tobject); virtual; abstract;');
- Add(' end;');
- Add('begin');
- SetExpectedPasResolverError('Duplicate identifier "Proc" at test1.pp(6,18)',
- nDuplicateIdentifier);
- ConvertProgram;
- end;
- procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
- begin
- Converter.Options:=Converter.Options-[coNoTypeInfo];
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' published');
- Add(' procedure Proc; external name ''foo'';');
- Add(' end;');
- Add('begin');
- SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
- nPublishedNameMustMatchExternal);
- ConvertProgram;
- end;
- procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
- begin
- Converter.Options:=Converter.Options-[coNoTypeInfo];
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' class var FA: longint;');
- Add(' published');
- Add(' class property A: longint read FA;');
- Add(' end;');
- Add('begin');
- SetExpectedPasResolverError('Invalid published property modifier "class"',
- nInvalidXModifierY);
- ConvertProgram;
- end;
- procedure TTestModule.TestRTTI_PublishedClassFieldFail;
- begin
- Converter.Options:=Converter.Options-[coNoTypeInfo];
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' published');
- Add(' class var FA: longint;');
- Add(' end;');
- Add('begin');
- SetExpectedPasResolverError(sSymbolCannotBePublished,
- nSymbolCannotBePublished);
- ConvertProgram;
- end;
- procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
- begin
- Converter.Options:=Converter.Options-[coNoTypeInfo];
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TObject = class');
- Add(' published');
- Add(' V: longint; external name ''foo'';');
- Add(' end;');
- Add('begin');
- SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
- nPublishedNameMustMatchExternal);
- ConvertProgram;
- end;
- procedure TTestModule.TestRTTI_Class_Field;
- begin
- Converter.Options:=Converter.Options-[coNoTypeInfo];
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TObject = class');
- Add(' private');
- Add(' FPropA: string;');
- Add(' published');
- Add(' VarLI: longint;');
- Add(' VarC: char;');
- Add(' VarS: string;');
- Add(' VarD: double;');
- Add(' VarB: boolean;');
- Add(' VarLW: longword;');
- Add(' VarSmI: smallint;');
- Add(' VarW: word;');
- Add(' VarShI: shortint;');
- Add(' VarBy: byte;');
- Add(' VarExt: longint external name ''VarExt'';');
- Add(' end;');
- Add('var p: pointer;');
- Add(' Obj: tobject;');
- Add('begin');
- Add(' p:=typeinfo(tobject);');
- Add(' p:=typeinfo(p);');
- Add(' p:=typeinfo(obj);');
- ConvertProgram;
- CheckSource('TestRTTI_Class_Field',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' this.FPropA = "";',
- ' this.VarLI = 0;',
- ' this.VarC = "";',
- ' this.VarS = "";',
- ' this.VarD = 0.0;',
- ' this.VarB = false;',
- ' this.VarLW = 0;',
- ' this.VarSmI = 0;',
- ' this.VarW = 0;',
- ' this.VarShI = 0;',
- ' this.VarBy = 0;',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' var $r = this.$rtti;',
- ' $r.addField("VarLI", rtl.longint);',
- ' $r.addField("VarC", rtl.char);',
- ' $r.addField("VarS", rtl.string);',
- ' $r.addField("VarD", rtl.double);',
- ' $r.addField("VarB", rtl.boolean);',
- ' $r.addField("VarLW", rtl.longword);',
- ' $r.addField("VarSmI", rtl.smallint);',
- ' $r.addField("VarW", rtl.word);',
- ' $r.addField("VarShI", rtl.shortint);',
- ' $r.addField("VarBy", rtl.byte);',
- ' $r.addField("VarExt", rtl.longint);',
- '});',
- 'this.p = null;',
- 'this.Obj = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.p = $mod.$rtti["TObject"];',
- '$mod.p = rtl.pointer;',
- '$mod.p = $mod.Obj.$rtti;',
- '']));
- end;
- procedure TTestModule.TestRTTI_Class_Method;
- begin
- Converter.Options:=Converter.Options-[coNoTypeInfo];
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' private');
- Add(' procedure Internal; external name ''$intern'';');
- Add(' published');
- Add(' procedure Click; virtual; abstract;');
- Add(' procedure Notify(Sender: TObject); virtual; abstract;');
- Add(' function GetNotify: boolean; external name ''GetNotify'';');
- Add(' procedure Println(a,b: longint); varargs; virtual; abstract;');
- Add(' end;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestRTTI_Class_Method',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' var $r = this.$rtti;',
- ' $r.addMethod("Click", 0, null);',
- ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
- ' $r.addMethod("GetNotify", 1, null, rtl.boolean,{flags: 4});',
- ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, {',
- ' flags: 2',
- ' });',
- '});',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestModule.TestRTTI_Class_MethodArgFlags;
- begin
- Converter.Options:=Converter.Options-[coNoTypeInfo];
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' published');
- Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
- Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
- Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
- Add(' end;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestRTTI_Class_MethodOpenArray',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' var $r = this.$rtti;',
- '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
- '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
- '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
- '});',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestModule.TestRTTI_Class_Property;
- begin
- Converter.Options:=Converter.Options-[coNoTypeInfo];
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TObject = class');
- Add(' private');
- Add(' FColor: longint;');
- Add(' FColorStored: boolean;');
- Add(' procedure SetColor(Value: longint); virtual; abstract;');
- Add(' function GetColor: longint; virtual; abstract;');
- Add(' function GetColorStored: boolean; virtual; abstract;');
- Add(' FExtSize: longint external name ''$extSize'';');
- Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
- Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
- Add(' function GetExtSize: longint; external name ''$getSize'';');
- Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
- Add(' published');
- Add(' property ColorA: longint read FColor;');
- Add(' property ColorB: longint write FColor;');
- Add(' property ColorC: longint read GetColor write SetColor;');
- Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
- Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
- Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
- Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
- Add(' end;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestRTTI_Class_Property',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' this.FColor = 0;',
- ' this.FColorStored = false;',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' var $r = this.$rtti;',
- ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
- ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
- ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
- ' $r.addProperty("ColorD", 0, rtl.longint, "FColor", "FColor",{',
- ' stored: "FColorStored"',
- ' }',
- ' );',
- ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
- ' $r.addProperty("ExtSizeB", 3, rtl.longint, "$getSize", "$setSize",{',
- ' stored: "$extSizeStored"',
- ' }',
- ' );',
- ' $r.addProperty("ExtSizeC", 4, rtl.longint, "$extSize", "$extSize",{',
- ' stored: "$getExtSizeStored"',
- ' }',
- ' );',
- '});',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestModule.TestRTTI_Class_PropertyParams;
- begin
- Converter.Options:=Converter.Options-[coNoTypeInfo];
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' integer = longint;');
- Add(' TObject = class');
- Add(' private');
- Add(' function GetItems(i: integer): tobject; virtual; abstract;');
- Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
- Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
- Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
- Add(' published');
- Add(' property Items[Index: integer]: tobject read getitems write setitems;');
- Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
- Add(' end;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestRTTI_Class_PropertyParams',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' var $r = this.$rtti;',
- ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
- ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
- '});',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestModule.TestRTTI_OverrideMethod;
- begin
- Converter.Options:=Converter.Options-[coNoTypeInfo];
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' published');
- Add(' procedure DoIt; virtual; abstract;');
- Add(' end;');
- Add(' TSky = class');
- Add(' published');
- Add(' procedure DoIt; override;');
- Add(' end;');
- Add('procedure TSky.DoIt; begin end;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestRTTI_OverrideMethod',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' var $r = this.$rtti;',
- ' $r.addMethod("DoIt", 0, null);',
- '});',
- 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
- ' this.DoIt = function () {',
- ' };',
- '});',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestModule.TestRTTI_OverloadProperty;
- begin
- Converter.Options:=Converter.Options-[coNoTypeInfo];
- StartProgram(false);
- Add('type');
- Add(' TObject = class');
- Add(' protected');
- Add(' FFlag: longint;');
- Add(' published');
- Add(' property Flag: longint read fflag;');
- Add(' end;');
- Add(' TSky = class');
- Add(' published');
- Add(' property FLAG: longint write fflag;');
- Add(' end;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestRTTI_OverrideMethod',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' this.FFlag = 0;',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' var $r = this.$rtti;',
- ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
- '});',
- 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
- ' var $r = this.$rtti;',
- ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
- '});',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestModule.TestRTTI_ClassForward;
- begin
- Converter.Options:=Converter.Options-[coNoTypeInfo];
- StartProgram(false);
- Add('type');
- Add(' TObject = class end;');
- Add(' tbridge = class;');
- Add(' TProc = function: tbridge;');
- Add(' TOger = class');
- Add(' published');
- Add(' FBridge: tbridge;');
- Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
- Add(' property Bridge: tbridge read fbridge write setbridge;');
- Add(' end;');
- Add(' TBridge = class');
- Add(' FOger: toger;');
- Add(' end;');
- Add('var p: Pointer;');
- Add(' b: tbridge;');
- Add('begin');
- Add(' p:=typeinfo(tbridge);');
- Add(' p:=typeinfo(b);');
- ConvertProgram;
- CheckSource('TestRTTI_ClassForward',
- LinesToStr([ // statements
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- '$mod.$rtti.$Class("TBridge");',
- '$mod.$rtti.$ProcVar("TProc", {',
- ' procsig: rtl.newTIProcSig(null, $mod.$rtti["TBridge"])',
- '});',
- 'rtl.createClass($mod, "TOger", $mod.TObject, function () {',
- ' this.$init = function () {',
- ' $mod.TObject.$init.call(this);',
- ' this.FBridge = null;',
- ' };',
- ' this.$final = function () {',
- ' this.FBridge = undefined;',
- ' $mod.TObject.$final.call(this);',
- ' };',
- ' var $r = this.$rtti;',
- ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
- ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
- ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
- '});',
- 'rtl.createClass($mod, "TBridge", $mod.TObject, function () {',
- ' this.$init = function () {',
- ' $mod.TObject.$init.call(this);',
- ' this.FOger = null;',
- ' };',
- ' this.$final = function () {',
- ' this.FOger = undefined;',
- ' $mod.TObject.$final.call(this);',
- ' };',
- '});',
- 'this.p = null;',
- 'this.b = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.p = $mod.$rtti["TBridge"];',
- '$mod.p = $mod.b.$rtti;',
- '']));
- end;
- procedure TTestModule.TestRTTI_ClassOf;
- begin
- Converter.Options:=Converter.Options-[coNoTypeInfo];
- StartProgram(false);
- Add('type');
- Add(' TClass = class of tobject;');
- Add(' TProcA = function: TClass;');
- Add(' TObject = class');
- Add(' published');
- Add(' C: tclass;');
- Add(' end;');
- Add(' tfox = class;');
- Add(' TBird = class end;');
- Add(' TBirds = class of tbird;');
- Add(' TFox = class end;');
- Add(' TFoxes = class of tfox;');
- Add(' TCows = class of TCow;');
- Add(' TCow = class;');
- Add(' TCow = class end;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestRTTI_ClassOf',
- LinesToStr([ // statements
- '$mod.$rtti.$Class("TObject");',
- '$mod.$rtti.$ClassRef("TClass", {',
- ' instancetype: $mod.$rtti["TObject"]',
- '});',
- '$mod.$rtti.$ProcVar("TProcA", {',
- ' procsig: rtl.newTIProcSig(null, $mod.$rtti["TClass"])',
- '});',
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' this.C = null;',
- ' };',
- ' this.$final = function () {',
- ' this.C = undefined;',
- ' };',
- ' var $r = this.$rtti;',
- ' $r.addField("C", $mod.$rtti["TClass"]);',
- '});',
- '$mod.$rtti.$Class("TFox");',
- 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
- '});',
- '$mod.$rtti.$ClassRef("TBirds", {',
- ' instancetype: $mod.$rtti["TBird"]',
- '});',
- 'rtl.createClass($mod, "TFox", $mod.TObject, function () {',
- '});',
- '$mod.$rtti.$ClassRef("TFoxes", {',
- ' instancetype: $mod.$rtti["TFox"]',
- '});',
- '$mod.$rtti.$Class("TCow");',
- '$mod.$rtti.$ClassRef("TCows", {',
- ' instancetype: $mod.$rtti["TCow"]',
- '});',
- 'rtl.createClass($mod, "TCow", $mod.TObject, function () {',
- '});',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestModule.TestRTTI_Record;
- begin
- Converter.Options:=Converter.Options-[coNoTypeInfo];
- StartProgram(false);
- Add('type');
- Add(' integer = longint;');
- Add(' TPoint = record');
- Add(' x,y: integer;');
- Add(' end;');
- Add('var p: pointer;');
- Add(' r: tpoint;');
- Add('begin');
- Add(' p:=typeinfo(tpoint);');
- Add(' p:=typeinfo(r);');
- Add(' p:=typeinfo(r.x);');
- ConvertProgram;
- CheckSource('TestRTTI_Record',
- LinesToStr([ // statements
- 'this.TPoint = function (s) {',
- ' if (s) {',
- ' this.x = s.x;',
- ' this.y = s.y;',
- ' } else {',
- ' this.x = 0;',
- ' this.y = 0;',
- ' };',
- ' this.$equal = function (b) {',
- ' return (this.x == b.x) && (this.y == b.y);',
- ' };',
- '};',
- '$mod.$rtti.$Record("TPoint", {}).addFields("x", rtl.longint, "y", rtl.longint);',
- 'this.p = null;',
- 'this.r = new $mod.TPoint();',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.p = $mod.$rtti["TPoint"];',
- '$mod.p = $mod.$rtti["TPoint"];',
- '$mod.p = rtl.longint;',
- '']));
- end;
- procedure TTestModule.TestRTTI_LocalTypes;
- begin
- Converter.Options:=Converter.Options-[coNoTypeInfo];
- StartProgram(false);
- Add('procedure DoIt;');
- Add('type');
- Add(' integer = longint;');
- Add(' TPoint = record');
- Add(' x,y: integer;');
- Add(' end;');
- Add('begin');
- Add('end;');
- Add('begin');
- ConvertProgram;
- CheckSource('TestRTTI_LocalTypes',
- LinesToStr([ // statements
- 'this.DoIt = function () {',
- ' this.TPoint = function (s) {',
- ' if (s) {',
- ' this.x = s.x;',
- ' this.y = s.y;',
- ' } else {',
- ' this.x = 0;',
- ' this.y = 0;',
- ' };',
- ' this.$equal = function (b) {',
- ' return (this.x == b.x) && (this.y == b.y);',
- ' };',
- ' };',
- '};',
- '']),
- LinesToStr([ // $mod.$main
- '']));
- end;
- procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
- begin
- Converter.Options:=Converter.Options-[coNoTypeInfo];
- StartProgram(false);
- Add('type');
- Add(' TCaption = string;');
- Add(' TYesNo = boolean;');
- Add(' TLetter = char;');
- Add(' TFloat = double;');
- Add(' TPtr = pointer;');
- Add(' TShortInt = shortint;');
- Add(' TByte = byte;');
- Add(' TSmallInt = smallint;');
- Add(' TWord = word;');
- Add(' TInt32 = longint;');
- Add(' TDWord = longword;');
- Add(' TValue = jsvalue;');
- Add('var p: TPtr;');
- Add('begin');
- Add(' p:=typeinfo(string);');
- Add(' p:=typeinfo(tcaption);');
- Add(' p:=typeinfo(boolean);');
- Add(' p:=typeinfo(tyesno);');
- Add(' p:=typeinfo(char);');
- Add(' p:=typeinfo(tletter);');
- Add(' p:=typeinfo(double);');
- Add(' p:=typeinfo(tfloat);');
- Add(' p:=typeinfo(pointer);');
- Add(' p:=typeinfo(tptr);');
- Add(' p:=typeinfo(shortint);');
- Add(' p:=typeinfo(tshortint);');
- Add(' p:=typeinfo(byte);');
- Add(' p:=typeinfo(tbyte);');
- Add(' p:=typeinfo(smallint);');
- Add(' p:=typeinfo(tsmallint);');
- Add(' p:=typeinfo(word);');
- Add(' p:=typeinfo(tword);');
- Add(' p:=typeinfo(longword);');
- Add(' p:=typeinfo(tdword);');
- Add(' p:=typeinfo(jsvalue);');
- Add(' p:=typeinfo(tvalue);');
- ConvertProgram;
- CheckSource('TestRTTI_TypeInfo_BaseTypes',
- LinesToStr([ // statements
- 'this.p = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.p = rtl.string;',
- '$mod.p = rtl.string;',
- '$mod.p = rtl.boolean;',
- '$mod.p = rtl.boolean;',
- '$mod.p = rtl.char;',
- '$mod.p = rtl.char;',
- '$mod.p = rtl.double;',
- '$mod.p = rtl.double;',
- '$mod.p = rtl.pointer;',
- '$mod.p = rtl.pointer;',
- '$mod.p = rtl.shortint;',
- '$mod.p = rtl.shortint;',
- '$mod.p = rtl.byte;',
- '$mod.p = rtl.byte;',
- '$mod.p = rtl.smallint;',
- '$mod.p = rtl.smallint;',
- '$mod.p = rtl.word;',
- '$mod.p = rtl.word;',
- '$mod.p = rtl.longword;',
- '$mod.p = rtl.longword;',
- '$mod.p = rtl.jsvalue;',
- '$mod.p = rtl.jsvalue;',
- '']));
- end;
- procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
- begin
- Converter.Options:=Converter.Options-[coNoTypeInfo];
- StartProgram(false);
- Add('procedure DoIt;');
- Add('type');
- Add(' integer = longint;');
- Add(' TPoint = record');
- Add(' x,y: integer;');
- Add(' end;');
- Add('var p: pointer;');
- Add('begin');
- Add(' p:=typeinfo(tpoint);');
- Add('end;');
- Add('begin');
- SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
- ConvertProgram;
- end;
- procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
- begin
- Converter.Options:=Converter.Options-[coNoTypeInfo];
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TTypeInfo = class external name ''rtl.tTypeInfo'' end;');
- Add(' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;');
- Add(' TFlag = (up,down);');
- Add(' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;');
- Add(' TFlags = set of TFlag;');
- Add(' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;');
- Add('var');
- Add(' ti: TTypeInfo;');
- Add(' tiInt: TTypeInfoInteger;');
- Add(' tiEnum: TTypeInfoEnum;');
- Add(' tiSet: TTypeInfoSet;');
- Add('begin');
- Add(' ti:=typeinfo(string);');
- Add(' ti:=typeinfo(boolean);');
- Add(' ti:=typeinfo(char);');
- Add(' ti:=typeinfo(double);');
- Add(' tiInt:=typeinfo(shortint);');
- Add(' tiInt:=typeinfo(byte);');
- Add(' tiInt:=typeinfo(smallint);');
- Add(' tiInt:=typeinfo(word);');
- Add(' tiInt:=typeinfo(longint);');
- Add(' tiInt:=typeinfo(longword);');
- Add(' ti:=typeinfo(jsvalue);');
- Add(' tiEnum:=typeinfo(tflag);');
- Add(' tiSet:=typeinfo(tflags);');
- ConvertProgram;
- CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
- LinesToStr([ // statements
- 'this.TFlag = {',
- ' "0": "up",',
- ' up: 0,',
- ' "1": "down",',
- ' down: 1',
- '};',
- '$mod.$rtti.$Enum("TFlag", {',
- ' minvalue: 0,',
- ' maxvalue: 1,',
- ' enumtype: this.TFlag',
- '});',
- '$mod.$rtti.$Set("TFlags", {',
- ' comptype: $mod.$rtti["TFlag"]',
- '});',
- 'this.ti = null;',
- 'this.tiInt = null;',
- 'this.tiEnum = null;',
- 'this.tiSet = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.ti = rtl.string;',
- '$mod.ti = rtl.boolean;',
- '$mod.ti = rtl.char;',
- '$mod.ti = rtl.double;',
- '$mod.tiInt = rtl.shortint;',
- '$mod.tiInt = rtl.byte;',
- '$mod.tiInt = rtl.smallint;',
- '$mod.tiInt = rtl.word;',
- '$mod.tiInt = rtl.longint;',
- '$mod.tiInt = rtl.longword;',
- '$mod.ti = rtl.jsvalue;',
- '$mod.tiEnum = $mod.$rtti["TFlag"];',
- '$mod.tiSet = $mod.$rtti["TFlags"];',
- '']));
- end;
- procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
- begin
- Converter.Options:=Converter.Options-[coNoTypeInfo];
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TTypeInfo = class external name ''rtl.tTypeInfo'' end;');
- Add(' TStaticArr = array[boolean] of string;');
- Add(' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;');
- Add(' TDynArr = array of string;');
- Add(' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;');
- Add(' TProc = procedure;');
- Add(' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;');
- Add(' TMethod = procedure of object;');
- Add(' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;');
- Add('var');
- Add(' StaticArray: TStaticArr;');
- Add(' tiStaticArray: TTypeInfoStaticArray;');
- Add(' DynArray: TDynArr;');
- Add(' tiDynArray: TTypeInfoDynArray;');
- Add(' ProcVar: TProc;');
- Add(' tiProcVar: TTypeInfoProcVar;');
- Add(' MethodVar: TMethod;');
- Add(' tiMethodVar: TTypeInfoMethodVar;');
- Add('begin');
- Add(' tiStaticArray:=typeinfo(StaticArray);');
- Add(' tiStaticArray:=typeinfo(TStaticArr);');
- Add(' tiDynArray:=typeinfo(DynArray);');
- Add(' tiDynArray:=typeinfo(TDynArr);');
- Add(' tiProcVar:=typeinfo(ProcVar);');
- Add(' tiProcVar:=typeinfo(TProc);');
- Add(' tiMethodVar:=typeinfo(MethodVar);');
- Add(' tiMethodVar:=typeinfo(TMethod);');
- ConvertProgram;
- CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
- LinesToStr([ // statements
- ' $mod.$rtti.$StaticArray("TStaticArr", {',
- ' dims: [2],',
- ' eltype: rtl.string',
- '});',
- '$mod.$rtti.$DynArray("TDynArr", {',
- ' eltype: rtl.string',
- '});',
- '$mod.$rtti.$ProcVar("TProc", {',
- ' procsig: rtl.newTIProcSig(null)',
- '});',
- '$mod.$rtti.$MethodVar("TMethod", {',
- ' procsig: rtl.newTIProcSig(null),',
- ' methodkind: 0',
- '});',
- 'this.StaticArray = rtl.arrayNewMultiDim([2], "");',
- 'this.tiStaticArray = null;',
- 'this.DynArray = [];',
- 'this.tiDynArray = null;',
- 'this.ProcVar = null;',
- 'this.tiProcVar = null;',
- 'this.MethodVar = null;',
- 'this.tiMethodVar = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
- '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
- '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
- '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
- '$mod.tiProcVar = $mod.$rtti["TProc"];',
- '$mod.tiProcVar = $mod.$rtti["TProc"];',
- '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
- '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
- '']));
- end;
- procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
- begin
- Converter.Options:=Converter.Options-[coNoTypeInfo];
- StartProgram(false);
- Add('{$modeswitch externalclass}');
- Add('type');
- Add(' TTypeInfo = class external name ''rtl.tTypeInfo'' end;');
- Add(' TRec = record end;');
- Add(' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;');
- // ToDo: ^PRec
- Add(' TObject = class end;');
- Add(' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;');
- Add(' TClass = class of tobject;');
- Add(' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;');
- Add(' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;');
- Add('var');
- Add(' Rec: trec;');
- Add(' tiRecord: ttypeinforecord;');
- Add(' Obj: tobject;');
- Add(' tiClass: ttypeinfoclass;');
- Add(' aClass: tclass;');
- Add(' tiClassRef: ttypeinfoclassref;');
- // ToDo: ^PRec
- Add(' tiPointer: ttypeinfopointer;');
- Add('begin');
- Add(' tirecord:=typeinfo(trec);');
- Add(' tirecord:=typeinfo(trec);');
- Add(' ticlass:=typeinfo(obj);');
- Add(' ticlass:=typeinfo(tobject);');
- Add(' ticlass:=typeinfo(aclass);');
- Add(' ticlassref:=typeinfo(tclass);');
- ConvertProgram;
- CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
- LinesToStr([ // statements
- 'this.TRec = function (s) {',
- '};',
- '$mod.$rtti.$Record("TRec", {});',
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- '});',
- '$mod.$rtti.$ClassRef("TClass", {',
- ' instancetype: $mod.$rtti["TObject"]',
- '});',
- 'this.Rec = new $mod.TRec();',
- 'this.tiRecord = null;',
- 'this.Obj = null;',
- 'this.tiClass = null;',
- 'this.aClass = null;',
- 'this.tiClassRef = null;',
- 'this.tiPointer = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.tiRecord = $mod.$rtti["TRec"];',
- '$mod.tiRecord = $mod.$rtti["TRec"];',
- '$mod.tiClass = $mod.Obj.$rtti;',
- '$mod.tiClass = $mod.$rtti["TObject"];',
- '$mod.tiClass = $mod.aClass.$rtti;',
- '$mod.tiClassRef = $mod.$rtti["TClass"];',
- '']));
- end;
- procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
- begin
- Converter.Options:=Converter.Options-[coNoTypeInfo];
- StartProgram(false);
- Add([
- '{$modeswitch externalclass}',
- 'type',
- ' TClass = class of tobject;',
- ' TObject = class',
- ' function MyClass: TClass;',
- ' class function ClassType: TClass;',
- ' end;',
- ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
- ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
- 'function TObject.MyClass: TClass;',
- 'var t: TTypeInfoClass;',
- 'begin',
- ' t:=TypeInfo(Self);',
- ' t:=TypeInfo(Result);',
- 'end;',
- 'class function TObject.ClassType: TClass;',
- 'var t: TTypeInfoClass;',
- 'begin',
- ' t:=TypeInfo(Self);',
- ' t:=TypeInfo(Result);',
- 'end;',
- 'var',
- ' Obj: TObject;',
- ' t: TTypeInfoClass;',
- 'begin',
- ' t:=TypeInfo(TObject.ClassType);',
- ' t:=TypeInfo(Obj.ClassType);',
- ' t:=TypeInfo(Obj.MyClass);',
- '']);
- ConvertProgram;
- CheckSource('TestRTTI_TypeInfo_FunctionClassType',
- LinesToStr([ // statements
- '$mod.$rtti.$Class("TObject");',
- '$mod.$rtti.$ClassRef("TClass", {',
- ' instancetype: $mod.$rtti["TObject"]',
- '});',
- 'rtl.createClass($mod, "TObject", null, function () {',
- ' this.$init = function () {',
- ' };',
- ' this.$final = function () {',
- ' };',
- ' this.MyClass = function () {',
- ' var Result = null;',
- ' var t = null;',
- ' t = this.$rtti;',
- ' t = Result.$rtti;',
- ' return Result;',
- ' };',
- ' this.ClassType = function () {',
- ' var Result = null;',
- ' var t = null;',
- ' t = this.$rtti;',
- ' t = Result.$rtti;',
- ' return Result;',
- ' };',
- '});',
- 'this.Obj = null;',
- 'this.t = null;',
- '']),
- LinesToStr([ // $mod.$main
- '$mod.t = $mod.TObject.ClassType().$rtti;',
- '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
- '$mod.t = $mod.Obj.MyClass().$rtti;',
- '']));
- end;
- Initialization
- RegisterTests([TTestModule]);
- end.
|