2
0

tcmodules.pas 974 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338363393634036341363423634336344363453634636347363483634936350363513635236353363543635536356363573635836359363603636136362363633636436365363663636736368363693637036371363723637336374363753637636377363783637936380363813638236383363843638536386363873638836389363903639136392363933639436395363963639736398363993640036401364023640336404364053640636407364083640936410364113641236413364143641536416364173641836419364203642136422364233642436425364263642736428364293643036431364323643336434364353643636437364383643936440364413644236443364443644536446364473644836449364503645136452364533645436455364563645736458364593646036461364623646336464364653646636467364683646936470364713647236473364743647536476364773647836479364803648136482364833648436485364863648736488364893649036491364923649336494364953649636497364983649936500365013650236503365043650536506365073650836509365103651136512365133651436515365163651736518365193652036521365223652336524365253652636527365283652936530365313653236533365343653536536365373653836539365403654136542365433654436545365463654736548365493655036551365523655336554365553655636557365583655936560365613656236563365643656536566365673656836569365703657136572365733657436575365763657736578365793658036581365823658336584365853658636587365883658936590365913659236593365943659536596365973659836599366003660136602366033660436605366063660736608366093661036611366123661336614366153661636617366183661936620366213662236623366243662536626366273662836629366303663136632366333663436635366363663736638366393664036641366423664336644366453664636647366483664936650366513665236653366543665536656366573665836659366603666136662366633666436665366663666736668366693667036671366723667336674366753667636677366783667936680366813668236683366843668536686366873668836689366903669136692366933669436695366963669736698366993670036701367023670336704367053670636707367083670936710367113671236713367143671536716367173671836719367203672136722367233672436725367263672736728367293673036731367323673336734367353673636737367383673936740367413674236743367443674536746367473674836749367503675136752367533675436755367563675736758367593676036761367623676336764367653676636767367683676936770367713677236773367743677536776367773677836779367803678136782367833678436785367863678736788367893679036791367923679336794367953679636797367983679936800368013680236803368043680536806368073680836809368103681136812368133681436815368163681736818368193682036821368223682336824368253682636827368283682936830368313683236833368343683536836368373683836839368403684136842368433684436845368463684736848368493685036851368523685336854368553685636857368583685936860368613686236863368643686536866368673686836869368703687136872368733687436875368763687736878368793688036881368823688336884368853688636887368883688936890368913689236893368943689536896368973689836899369003690136902369033690436905
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2018 by Michael Van Canneyt
  4. Unit tests for Pascal-to-Javascript converter class.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************
  11. Examples:
  12. ./testpas2js --suite=TTestModule.TestEmptyProgram
  13. ./testpas2js --suite=TTestModule.TestEmptyUnit
  14. }
  15. unit TCModules;
  16. {$mode objfpc}{$H+}
  17. interface
  18. uses
  19. Classes, SysUtils, fpcunit, testregistry, contnrs,
  20. jstree, jswriter, jsbase,
  21. PasTree, PScanner, PasResolver, PParser, PasResolveEval, FPPas2Js;
  22. const
  23. // default parser+scanner options
  24. po_tcmodules = po_Pas2js+[po_KeepScannerError];
  25. co_tcmodules = [];
  26. JSONNewLine = {$IFDEF Windows}'\r\n'{$ELSE}'\n'{$ENDIF};
  27. type
  28. TSrcMarkerKind = (
  29. mkLabel,
  30. mkResolverReference,
  31. mkDirectReference
  32. );
  33. const
  34. SrcMarker: array[TSrcMarkerKind] of char = (
  35. '#', // mkLabel
  36. '@', // mkResolverReference
  37. '=' // mkDirectReference
  38. );
  39. type
  40. PSrcMarker = ^TSrcMarker;
  41. TSrcMarker = record
  42. Kind: TSrcMarkerKind;
  43. Filename: string;
  44. Row: integer;
  45. StartCol, EndCol: integer; // token start, end column
  46. Identifier: string;
  47. Next: PSrcMarker;
  48. end;
  49. TSystemUnitPart = (
  50. supTObject,
  51. supTVarRec,
  52. supTypeInfo,
  53. supTInterfacedObject,
  54. supWriteln
  55. );
  56. TSystemUnitParts = set of TSystemUnitPart;
  57. { TTestHintMessage }
  58. TTestHintMessage = class
  59. public
  60. Id: int64;
  61. MsgType: TMessageType;
  62. MsgNumber: integer;
  63. Msg: string;
  64. SourcePos: TPasSourcePos;
  65. end;
  66. TTestResolverReferenceData = record
  67. Filename: string;
  68. Row: integer;
  69. StartCol: integer;
  70. EndCol: integer;
  71. Found: TFPList; // list of TPasElement at this token
  72. end;
  73. PTestResolverReferenceData = ^TTestResolverReferenceData;
  74. { TTestPasParser }
  75. TTestPasParser = Class(TPasParser)
  76. end;
  77. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  78. { TTestEnginePasResolver }
  79. TTestEnginePasResolver = class(TPas2JsResolver)
  80. private
  81. FFilename: string;
  82. FModule: TPasModule;
  83. FOnFindUnit: TOnFindUnit;
  84. FParser: TTestPasParser;
  85. FStreamResolver: TStreamResolver;
  86. FScanner: TPas2jsPasScanner;
  87. FSource: string;
  88. procedure SetModule(const AValue: TPasModule);
  89. public
  90. destructor Destroy; override;
  91. function CreateElement(AClass: TPTreeElement; const AName: String;
  92. AParent: TPasElement; AVisibility: TPasMemberVisibility;
  93. const ASrcPos: TPasSourcePos; TypeParams: TFPList = nil): TPasElement;
  94. overload; override;
  95. function FindUnit(const AName, InFilename: String; NameExpr,
  96. InFileExpr: TPasExpr): TPasModule; override;
  97. procedure UsedInterfacesFinished(Section: TPasSection); override;
  98. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  99. property Filename: string read FFilename write FFilename;
  100. property StreamResolver: TStreamResolver read FStreamResolver write FStreamResolver;
  101. property Scanner: TPas2jsPasScanner read FScanner write FScanner;
  102. property Parser: TTestPasParser read FParser write FParser;
  103. property Source: string read FSource write FSource;
  104. property Module: TPasModule read FModule write SetModule;
  105. end;
  106. { TCustomTestModule }
  107. TCustomTestModule = Class(TTestCase)
  108. private
  109. FWithTypeInfo: boolean;
  110. FSource: TStringList;
  111. FSkipTests: boolean;
  112. FScanner: TPas2jsPasScanner;
  113. FResolvers: TObjectList;// list of TTestEnginePasResolver
  114. FPasProgram: TPasProgram;
  115. FPasLibrary: TPasLibrary;
  116. FParser: TTestPasParser;
  117. FModule: TPasModule;
  118. FJSSource: TStringList;
  119. FJSRegModuleCall: TJSCallExpression;
  120. FJSModuleSrc: TJSSourceElements;
  121. FJSModuleCallArgs: TJSArguments;
  122. FJSModule: TJSSourceElements;
  123. FJSInterfaceUses: TJSArrayLiteral;
  124. FJSInitBody: TJSFunctionBody;
  125. FJSImplentationUses: TJSArrayLiteral;
  126. FJSImplementationUses: TJSArrayLiteral;
  127. FHub: TPas2JSResolverHub;
  128. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  129. FHintMsgs: TObjectList; // list of TTestHintMessage
  130. FFirstPasStatement: TPasImplBlock;
  131. FFileResolver: TStreamResolver;
  132. FFilename: string;
  133. FExpectedErrorNumber: integer;
  134. FExpectedErrorMsg: string;
  135. FExpectedErrorClass: ExceptClass;
  136. FEngine: TTestEnginePasResolver;
  137. FConverter: TPasToJSConverter;
  138. {$IFDEF EnablePasTreeGlobalRefCount}
  139. FElementRefCountAtSetup: int64;
  140. {$ENDIF}
  141. procedure FreeSrcMarkers;
  142. function GetResolverCount: integer;
  143. function GetResolvers(Index: integer): TTestEnginePasResolver;
  144. function GetMsgCount: integer;
  145. function GetMsgs(Index: integer): TTestHintMessage;
  146. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  147. procedure OnParserLog(Sender: TObject; const Msg: String);
  148. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  149. procedure OnScannerLog(Sender: TObject; const Msg: String);
  150. procedure OnCheckElementParent(El: TPasElement; arg: pointer);
  151. procedure OnFindReference(El: TPasElement; FindData: pointer);
  152. procedure SetWithTypeInfo(const AValue: boolean);
  153. protected
  154. procedure SetUp; override;
  155. function CreateConverter: TPasToJSConverter; virtual;
  156. function LoadUnit(const aUnitName: String): TPasModule;
  157. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  158. procedure TearDown; override;
  159. Procedure Add(Line: string); virtual;
  160. Procedure Add(const Lines: array of string);
  161. Procedure StartParsing; virtual;
  162. procedure ParseModuleQueue; virtual;
  163. procedure ParseModule; virtual;
  164. procedure ParseProgram; virtual;
  165. procedure ParseLibrary; virtual;
  166. procedure ParseUnit; virtual;
  167. protected
  168. FirstSrcMarker, LastSrcMarker: PSrcMarker;
  169. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  170. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  171. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  172. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  173. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  174. procedure AddSystemUnit(Parts: TSystemUnitParts = []); virtual;
  175. procedure StartProgram(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  176. procedure StartLibrary(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  177. procedure StartUnit(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  178. procedure ConvertModule; virtual;
  179. procedure ConvertProgram; virtual;
  180. procedure ConvertLibrary; virtual;
  181. procedure ConvertUnit; virtual;
  182. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  183. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  184. function GetDottedIdentifier(El: TJSElement): string;
  185. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  186. ImplStatements: string = ''); virtual;
  187. procedure CheckFullSource(Msg,ExpectedSrc: String); virtual;
  188. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  189. procedure CheckUnit(aFilename, ExpectedSrc: string); virtual;
  190. procedure CheckReferenceDirectives; virtual;
  191. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  192. Msg: string; Marker: PSrcMarker = nil); virtual;
  193. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  194. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  195. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  196. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  197. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  198. function IsErrorExpected(E: Exception): boolean;
  199. procedure RaiseErrorAtSrc(Msg: string; const aFilename: string; aRow, aCol: integer);
  200. procedure RaiseErrorAtSrcMarker(Msg: string; aMarker: PSrcMarker);
  201. procedure HandleScannerError(E: EScannerError);
  202. procedure HandleParserError(E: EParserError);
  203. procedure HandlePasResolveError(E: EPasResolve);
  204. procedure HandlePas2JSError(E: EPas2JS);
  205. procedure HandleException(E: Exception);
  206. procedure FailException(E: Exception);
  207. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  208. function IndexOfResolver(const aFilename: string): integer;
  209. function GetResolver(const aFilename: string): TTestEnginePasResolver;
  210. procedure GetSrc(Index: integer; out SrcLines: TStringList; out aFilename: string);
  211. function FindElementsAt(aFilename: string; aLine, aStartCol, aEndCol: integer): TFPList;// list of TPasElement
  212. function FindElementsAt(aMarker: PSrcMarker; ErrorOnNoElements: boolean = true): TFPList;// list of TPasElement
  213. function FindSrcLabel(const Identifier: string): PSrcMarker;
  214. function FindElementsAtSrcLabel(const Identifier: string; ErrorOnNoElements: boolean = true): TFPList;// list of TPasElement
  215. function GetDefaultNamespace: string;
  216. property PasProgram: TPasProgram Read FPasProgram;
  217. property PasLibrary: TPasLibrary Read FPasLibrary;
  218. property ResolverEngine: TTestEnginePasResolver read FEngine;
  219. property Filename: string read FFilename;
  220. Property Module: TPasModule Read FModule;
  221. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  222. property Converter: TPasToJSConverter read FConverter;
  223. property JSSource: TStringList read FJSSource;
  224. property JSModule: TJSSourceElements read FJSModule;
  225. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  226. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  227. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  228. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  229. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  230. property JSInitBody: TJSFunctionBody read FJSInitBody;
  231. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  232. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  233. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  234. property SkipTests: boolean read FSkipTests write FSkipTests;
  235. public
  236. constructor Create; override;
  237. destructor Destroy; override;
  238. property Hub: TPas2JSResolverHub read FHub;
  239. property Source: TStringList read FSource;
  240. property FileResolver: TStreamResolver read FFileResolver;
  241. property Scanner: TPas2jsPasScanner read FScanner;
  242. property Parser: TTestPasParser read FParser;
  243. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  244. property ResolverCount: integer read GetResolverCount;
  245. property MsgCount: integer read GetMsgCount;
  246. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  247. property WithTypeInfo: boolean read FWithTypeInfo write SetWithTypeInfo;
  248. end;
  249. { TTestModule }
  250. TTestModule = class(TCustomTestModule)
  251. Published
  252. Procedure TestReservedWords;
  253. // program, units, includes
  254. Procedure TestEmptyProgram;
  255. Procedure TestEmptyProgramUseStrict;
  256. Procedure TestEmptyUnit;
  257. Procedure TestEmptyUnitUseStrict;
  258. Procedure TestDottedUnitNames;
  259. Procedure TestDottedUnitNameImpl;
  260. Procedure TestDottedUnitExpr;
  261. Procedure Test_ModeFPCFail;
  262. Procedure Test_ModeSwitchCBlocksFail;
  263. Procedure TestUnit_UseSystem;
  264. Procedure TestUnit_Intf1Impl2Intf1;
  265. Procedure TestIncludeVersion;
  266. // vars/const
  267. Procedure TestVarInt;
  268. Procedure TestVarBaseTypes;
  269. Procedure TestBaseTypeSingleFail;
  270. Procedure TestBaseTypeExtendedFail;
  271. Procedure TestConstBaseTypes;
  272. Procedure TestUnitImplVars;
  273. Procedure TestUnitImplConsts;
  274. Procedure TestUnitImplRecord;
  275. Procedure TestRenameJSNameConflict;
  276. Procedure TestLocalConst;
  277. Procedure TestVarExternal;
  278. Procedure TestVarExternalOtherUnit;
  279. Procedure TestVarAbsoluteFail;
  280. Procedure TestConstExternal;
  281. // numbers
  282. Procedure TestDouble;
  283. Procedure TestDoubleSmall;
  284. Procedure TestInteger;
  285. Procedure TestIntegerRange;
  286. Procedure TestIntegerTypecasts;
  287. Procedure TestInteger_BitwiseShrNativeInt;
  288. Procedure TestInteger_BitwiseShlNativeInt;
  289. Procedure TestInteger_SystemFunc;
  290. Procedure TestInteger_AssignOutsideConst;
  291. Procedure TestCurrency;
  292. Procedure TestForBoolDo;
  293. Procedure TestForIntDo;
  294. Procedure TestForIntInDo;
  295. // strings
  296. Procedure TestCharConst;
  297. Procedure TestChar_Compare;
  298. Procedure TestChar_BuiltInProcs;
  299. Procedure TestStringConst;
  300. Procedure TestStringConst_InvalidUTF16;
  301. Procedure TestStringConstSurrogate;
  302. Procedure TestStringConstWhitespaces;
  303. Procedure TestStringConst_Multiline;
  304. Procedure TestString_Length;
  305. Procedure TestString_Compare;
  306. Procedure TestString_SetLength;
  307. Procedure TestString_CharAt;
  308. Procedure TestStringHMinusFail;
  309. Procedure TestStr;
  310. Procedure TestBaseType_AnsiStringFail;
  311. Procedure TestBaseType_WideStringFail;
  312. Procedure TestBaseType_ShortStringFail;
  313. Procedure TestBaseType_RawByteStringFail;
  314. Procedure TestTypeShortstring_Fail;
  315. Procedure TestCharSet_Custom;
  316. Procedure TestWideChar;
  317. Procedure TestForCharDo;
  318. Procedure TestForCharInDo;
  319. // alias types
  320. Procedure TestAliasTypeRef;
  321. Procedure TestTypeCast_BaseTypes;
  322. Procedure TestTypeCast_AliasBaseTypes;
  323. // functions
  324. Procedure TestEmptyProc;
  325. Procedure TestProcOneParam;
  326. Procedure TestFunctionWithoutParams;
  327. Procedure TestProcedureWithoutParams;
  328. Procedure TestPrgProcVar;
  329. Procedure TestProcTwoArgs;
  330. Procedure TestProc_DefaultValue;
  331. Procedure TestUnitProcVar;
  332. Procedure TestImplProc;
  333. Procedure TestFunctionResult;
  334. Procedure TestNestedProc;
  335. Procedure TestNestedProc_ResultString;
  336. Procedure TestForwardProc;
  337. Procedure TestNestedForwardProc;
  338. Procedure TestAssignFunctionResult;
  339. Procedure TestFunctionResultInCondition;
  340. Procedure TestFunctionResultInForLoop;
  341. Procedure TestFunctionResultInTypeCast;
  342. Procedure TestExit;
  343. Procedure TestExit_ResultInFinally;
  344. Procedure TestBreak;
  345. Procedure TestBreakAsVar;
  346. Procedure TestContinue;
  347. Procedure TestProc_External;
  348. Procedure TestProc_ExternalOtherUnit;
  349. Procedure TestProc_Asm;
  350. Procedure TestProc_AsmSubBlock;
  351. Procedure TestProc_Assembler;
  352. Procedure TestProc_VarParam;
  353. Procedure TestProc_VarParamString;
  354. Procedure TestProc_VarParamV;
  355. Procedure TestProc_Overload;
  356. Procedure TestProc_OverloadForward;
  357. Procedure TestProc_OverloadIntfImpl;
  358. Procedure TestProc_OverloadNested;
  359. Procedure TestProc_OverloadNestedForward;
  360. Procedure TestProc_OverloadUnitCycle;
  361. Procedure TestProc_Varargs;
  362. Procedure TestProc_ConstOrder;
  363. Procedure TestProc_DuplicateConst;
  364. Procedure TestProc_LocalVarAbsolute;
  365. Procedure TestProc_ResultAbsolute;
  366. Procedure TestProc_LocalVarInit;
  367. Procedure TestProc_ReservedWords;
  368. Procedure TestProc_ConstRefWord;
  369. // anonymous functions
  370. Procedure TestAnonymousProc_Assign_ObjFPC;
  371. Procedure TestAnonymousProc_Assign_Delphi;
  372. Procedure TestAnonymousProc_Arg;
  373. Procedure TestAnonymousProc_Typecast;
  374. Procedure TestAnonymousProc_With;
  375. Procedure TestAnonymousProc_ExceptOn;
  376. Procedure TestAnonymousProc_Nested;
  377. Procedure TestAnonymousProc_NestedAssignResult;
  378. Procedure TestAnonymousProc_Class;
  379. Procedure TestAnonymousProc_ForLoop;
  380. Procedure TestAnonymousProc_AsmDelphi;
  381. // enums, sets
  382. Procedure TestEnum_Name;
  383. Procedure TestEnum_Number;
  384. Procedure TestEnum_ConstFail;
  385. Procedure TestEnum_Functions;
  386. Procedure TestEnumRg_Functions;
  387. Procedure TestEnum_AsParams;
  388. Procedure TestEnumRange_Array;
  389. Procedure TestEnum_ForIn;
  390. Procedure TestEnum_ScopedNumber;
  391. Procedure TestEnum_InFunction;
  392. Procedure TestEnum_Name_Anonymous_Unit;
  393. Procedure TestSet_Enum;
  394. Procedure TestSet_Operators;
  395. Procedure TestSet_Operator_In;
  396. Procedure TestSet_Functions;
  397. Procedure TestSet_PassAsArgClone;
  398. Procedure TestSet_AsParams;
  399. Procedure TestSet_Property;
  400. Procedure TestSet_EnumConst;
  401. Procedure TestSet_IntConst;
  402. Procedure TestSet_IntRange;
  403. Procedure TestSet_AnonymousEnumType;
  404. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  405. Procedure TestSet_ConstEnum;
  406. Procedure TestSet_ConstChar;
  407. Procedure TestSet_ConstInt;
  408. Procedure TestSet_InFunction;
  409. Procedure TestSet_ForIn;
  410. // statements
  411. Procedure TestNestBegin;
  412. Procedure TestIncDec;
  413. Procedure TestLoHiFpcMode;
  414. Procedure TestLoHiDelphiMode;
  415. Procedure TestAssignments;
  416. Procedure TestArithmeticOperators1;
  417. Procedure TestMultiAdd;
  418. Procedure TestLogicalOperators;
  419. Procedure TestBitwiseOperators;
  420. Procedure TestBitwiseOperatorsLongword;
  421. Procedure TestFunctionInt;
  422. Procedure TestFunctionString;
  423. Procedure TestIfThen;
  424. Procedure TestForLoop;
  425. Procedure TestForLoopInsideFunction;
  426. Procedure TestForLoop_ReadVarAfter;
  427. Procedure TestForLoop_Nested;
  428. Procedure TestRepeatUntil;
  429. Procedure TestAsmBlock;
  430. Procedure TestAsmPas_Impl; // ToDo
  431. Procedure TestTryFinally;
  432. Procedure TestTryExcept;
  433. Procedure TestTryExcept_ReservedWords;
  434. Procedure TestIfThenRaiseElse;
  435. Procedure TestCaseOf;
  436. Procedure TestCaseOf_UseSwitch;
  437. Procedure TestCaseOfNoElse;
  438. Procedure TestCaseOfNoElse_UseSwitch;
  439. Procedure TestCaseOfRange;
  440. Procedure TestCaseOfString;
  441. Procedure TestCaseOfChar;
  442. Procedure TestCaseOfExternalClassConst;
  443. Procedure TestDebugger;
  444. // arrays
  445. Procedure TestArray_Dynamic;
  446. Procedure TestArray_Dynamic_Nil;
  447. Procedure TestArray_DynMultiDimensional;
  448. Procedure TestArray_DynamicAssign;
  449. Procedure TestArray_StaticInt;
  450. Procedure TestArray_StaticBool;
  451. Procedure TestArray_StaticChar;
  452. Procedure TestArray_StaticMultiDim;
  453. Procedure TestArray_StaticInFunction;
  454. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  455. Procedure TestArrayOfRecord;
  456. Procedure TestArray_StaticRecord;
  457. Procedure TestArrayOfSet;
  458. Procedure TestArray_DynAsParam;
  459. Procedure TestArray_StaticAsParam;
  460. Procedure TestArrayElement_AsParams;
  461. Procedure TestArrayElementFromFuncResult_AsParams;
  462. Procedure TestArrayEnumTypeRange;
  463. Procedure TestArray_SetLengthOutArg;
  464. Procedure TestArray_SetLengthProperty;
  465. Procedure TestArray_SetLengthMultiDim;
  466. Procedure TestArray_SetLengthDynOfStatic;
  467. Procedure TestArray_OpenArrayOfString;
  468. Procedure TestArray_ArrayOfCharAssignString;
  469. Procedure TestArray_ConstRef;
  470. Procedure TestArray_Concat;
  471. Procedure TestArray_Concat_Append;
  472. Procedure TestArray_Concat_Append_Var;
  473. Procedure TestArray_Copy;
  474. Procedure TestArray_InsertDelete;
  475. Procedure TestArray_Add_Append;
  476. Procedure TestArray_DynArrayConstObjFPC;
  477. Procedure TestArray_DynArrayConstDelphi;
  478. Procedure TestArray_ArrayLitAsParam;
  479. Procedure TestArray_ArrayLitMultiDimAsParam;
  480. Procedure TestArray_ArrayLitStaticAsParam;
  481. Procedure TestArray_ForInArrOfString;
  482. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  483. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  484. Procedure TestArrayOfConst_TVarRec;
  485. Procedure TestArrayOfConst_PassBaseTypes;
  486. Procedure TestArrayOfConst_PassObj;
  487. // record
  488. Procedure TestRecord_Empty;
  489. Procedure TestRecord_Var;
  490. Procedure TestRecord_VarExternal;
  491. Procedure TestRecord_WithDo;
  492. Procedure TestRecord_Assign;
  493. Procedure TestRecord_AsParams;
  494. Procedure TestRecord_ConstRef;
  495. Procedure TestRecordElement_AsParams;
  496. Procedure TestRecordElementFromFuncResult_AsParams;
  497. Procedure TestRecordElementFromWith_AsParams;
  498. Procedure TestRecord_Equal;
  499. Procedure TestRecord_JSValue;
  500. Procedure TestRecord_VariantFail;
  501. Procedure TestRecord_FieldArray;
  502. Procedure TestRecord_Const;
  503. Procedure TestRecord_TypecastFail;
  504. Procedure TestRecord_InFunction;
  505. Procedure TestRecord_ArrayConstMultiline;
  506. // ToDo: insert(record,ArrayOfRecord,0)
  507. // anonymous record
  508. Procedure TestRecordAnonym_Field;
  509. Procedure TestRecordAnonym_Assign;
  510. Procedure TestRecordAnonym_Nested;
  511. Procedure TestRecordAnonym_Const;
  512. Procedure TestRecordAnonym_InFunction;
  513. // advanced record
  514. Procedure TestAdvRecord_Function;
  515. Procedure TestAdvRecord_Property;
  516. Procedure TestAdvRecord_PropertyDefault;
  517. Procedure TestAdvRecord_Property_ClassMethod;
  518. Procedure TestAdvRecord_Const;
  519. Procedure TestAdvRecord_ExternalField;
  520. Procedure TestAdvRecord_SubRecord;
  521. Procedure TestAdvRecord_SubClass;
  522. Procedure TestAdvRecord_SubInterfaceFail;
  523. Procedure TestAdvRecord_Constructor;
  524. Procedure TestAdvRecord_ClassConstructor_Program;
  525. Procedure TestAdvRecord_ClassConstructor_Unit;
  526. // classes
  527. Procedure TestClass_TObjectDefaultConstructor;
  528. Procedure TestClass_TObjectConstructorWithParams;
  529. Procedure TestClass_TObjectConstructorWithDefaultParam;
  530. Procedure TestClass_Var;
  531. Procedure TestClass_Method;
  532. Procedure TestClass_Implementation;
  533. Procedure TestClass_Inheritance;
  534. Procedure TestClass_TypeAlias;
  535. Procedure TestClass_AbstractMethod;
  536. Procedure TestClass_CallInherited_ProcNoParams;
  537. Procedure TestClass_CallInherited_WithParams;
  538. Procedure TestClasS_CallInheritedConstructor;
  539. Procedure TestClass_ClassVar_Assign;
  540. Procedure TestClass_CallClassMethod;
  541. Procedure TestClass_CallClassMethodStatic;
  542. Procedure TestClass_Property;
  543. Procedure TestClass_Property_ClassMethod;
  544. Procedure TestClass_Property_ClassMethodStatic;
  545. Procedure TestClass_Property_Indexed;
  546. Procedure TestClass_Property_IndexSpec;
  547. Procedure TestClass_PropertyOfTypeArray;
  548. Procedure TestClass_PropertyDefault;
  549. Procedure TestClass_PropertyDefault_TypecastToOtherDefault;
  550. //Procedure TestClass_PropertyDefault;
  551. Procedure TestClass_PropertyOverride;
  552. Procedure TestClass_PropertyIncVisibility;
  553. Procedure TestClass_Assigned;
  554. Procedure TestClass_WithClassDoCreate;
  555. Procedure TestClass_WithClassInstDoProperty;
  556. Procedure TestClass_WithClassInstDoPropertyWithParams;
  557. Procedure TestClass_WithClassInstDoFunc;
  558. Procedure TestClass_TypeCast;
  559. Procedure TestClass_TypeCastUntypedParam;
  560. Procedure TestClass_Overloads;
  561. Procedure TestClass_OverloadsAncestor;
  562. Procedure TestClass_OverloadConstructor;
  563. Procedure TestClass_OverloadDelphiOverride;
  564. Procedure TestClass_ReintroduceVarDelphi;
  565. Procedure TestClass_ReintroducedVar;
  566. Procedure TestClass_RaiseDescendant;
  567. Procedure TestClass_ExternalMethod;
  568. Procedure TestClass_ExternalVirtualNameMismatchFail;
  569. Procedure TestClass_ExternalOverrideFail;
  570. Procedure TestClass_ExternalVar;
  571. Procedure TestClass_Const;
  572. Procedure TestClass_ConstEnum;
  573. Procedure TestClass_LocalConstDuplicate_Prg;
  574. Procedure TestClass_LocalConstDuplicate_Unit;
  575. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  576. Procedure TestClass_LocalVarSelfFail;
  577. Procedure TestClass_ArgSelfFail;
  578. Procedure TestClass_NestedProcSelf;
  579. Procedure TestClass_NestedProcSelf2;
  580. Procedure TestClass_NestedProcClassSelf;
  581. Procedure TestClass_NestedProcCallInherited;
  582. Procedure TestClass_TObjectFree;
  583. Procedure TestClass_TObjectFree_VarArg;
  584. Procedure TestClass_TObjectFreeNewInstance;
  585. Procedure TestClass_TObjectFreeLowerCase;
  586. Procedure TestClass_TObjectFreeFunctionFail;
  587. Procedure TestClass_TObjectFreePropertyFail;
  588. Procedure TestClass_ForIn;
  589. Procedure TestClass_DispatchMessage;
  590. Procedure TestClass_Message_DuplicateIntFail;
  591. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  592. // class of
  593. Procedure TestClassOf_Create;
  594. Procedure TestClassOf_Call;
  595. Procedure TestClassOf_Assign;
  596. Procedure TestClassOf_Is;
  597. Procedure TestClassOf_Compare;
  598. Procedure TestClassOf_ClassVar;
  599. Procedure TestClassOf_ClassMethod;
  600. Procedure TestClassOf_ClassProperty;
  601. Procedure TestClassOf_ClassMethodSelf;
  602. Procedure TestClassOf_TypeCast;
  603. Procedure TestClassOf_ImplicitFunctionCall;
  604. Procedure TestClassOf_Const;
  605. // nested class
  606. Procedure TestNestedClass_Alias;
  607. Procedure TestNestedClass_Record;
  608. Procedure TestNestedClass_Class;
  609. Procedure TestNestedClass_CallInherited;
  610. // external class
  611. Procedure TestExternalClass_Var;
  612. Procedure TestExternalClass_Const;
  613. Procedure TestExternalClass_Dollar;
  614. Procedure TestExternalClass_DuplicateVarFail;
  615. Procedure TestExternalClass_Method;
  616. Procedure TestExternalClass_ClassMethod;
  617. Procedure TestExternalClass_ClassMethodStatic;
  618. Procedure TestExternalClass_FunctionResultInTypeCast;
  619. Procedure TestExternalClass_NonExternalOverride;
  620. Procedure TestExternalClass_OverloadHint;
  621. Procedure TestExternalClass_SameNamePublishedProperty;
  622. Procedure TestExternalClass_Property;
  623. Procedure TestExternalClass_PropertyDate;
  624. Procedure TestExternalClass_ClassProperty;
  625. Procedure TestExternalClass_ClassOf;
  626. Procedure TestExternalClass_ClassOtherUnit;
  627. Procedure TestExternalClass_Is;
  628. Procedure TestExternalClass_As;
  629. Procedure TestExternalClass_DestructorFail;
  630. Procedure TestExternalClass_New;
  631. Procedure TestExternalClass_ClassOf_New;
  632. Procedure TestExternalClass_FuncClassOf_New;
  633. Procedure TestExternalClass_New_PasClassFail;
  634. Procedure TestExternalClass_New_PasClassBracketsFail;
  635. Procedure TestExternalClass_NewExtName;
  636. Procedure TestExternalClass_Constructor;
  637. Procedure TestExternalClass_ConstructorBrackets;
  638. Procedure TestExternalClass_LocalConstSameName;
  639. Procedure TestExternalClass_ReintroduceOverload;
  640. Procedure TestExternalClass_Inherited;
  641. Procedure TestExternalClass_PascalAncestorFail;
  642. Procedure TestExternalClass_NewInstance;
  643. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  644. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  645. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  646. Procedure TestExternalClass_JSFunctionPasDescendant;
  647. Procedure TestExternalClass_PascalProperty;
  648. Procedure TestExternalClass_TypeCastToRootClass;
  649. Procedure TestExternalClass_TypeCastToJSObject;
  650. Procedure TestExternalClass_TypeCastStringToExternalString;
  651. Procedure TestExternalClass_TypeCastToJSFunction;
  652. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  653. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  654. Procedure TestExternalClass_BracketAccessor;
  655. Procedure TestExternalClass_BracketAccessor_Call;
  656. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  657. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  658. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  659. Procedure TestExternalClass_BracketAccessor_MultiType;
  660. Procedure TestExternalClass_BracketAccessor_Index;
  661. Procedure TestExternalClass_ForInJSObject;
  662. Procedure TestExternalClass_ForInJSArray;
  663. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  664. Procedure TestExternalClass_NestedConstructor;
  665. // class interfaces
  666. Procedure TestClassInterface_Corba;
  667. Procedure TestClassInterface_Corba_ProcExternalFail;
  668. Procedure TestClassInterface_Corba_Overloads;
  669. Procedure TestClassInterface_Corba_DuplicateGUIInIntfListFail;
  670. Procedure TestClassInterface_Corba_DuplicateGUIInAncestorFail;
  671. Procedure TestClassInterface_Corba_AncestorImpl;
  672. Procedure TestClassInterface_Corba_ImplReintroduce;
  673. Procedure TestClassInterface_Corba_MethodResolution;
  674. Procedure TestClassInterface_COM_AncestorMoreInterfaces;
  675. Procedure TestClassInterface_Corba_MethodOverride;
  676. Procedure TestClassInterface_Corba_Delegation;
  677. Procedure TestClassInterface_Corba_DelegationStatic;
  678. Procedure TestClassInterface_Corba_Operators;
  679. Procedure TestClassInterface_Corba_Args;
  680. Procedure TestClassInterface_Corba_ForIn;
  681. Procedure TestClassInterface_Corba_ArrayOfIntf;
  682. Procedure TestClassInterface_COM_AssignVar;
  683. Procedure TestClassInterface_COM_AssignArg;
  684. Procedure TestClassInterface_COM_FunctionResult;
  685. Procedure TestClassInterface_COM_InheritedFuncResult;
  686. Procedure TestClassInterface_COM_FunctionExit;
  687. Procedure TestClassInterface_COM_IsAsTypeCasts;
  688. Procedure TestClassInterface_COM_PassAsArg;
  689. Procedure TestClassInterface_COM_PassToUntypedParam;
  690. Procedure TestClassInterface_COM_FunctionInExpr;
  691. Procedure TestClassInterface_COM_Property;
  692. Procedure TestClassInterface_COM_IntfProperty;
  693. Procedure TestClassInterface_COM_Delegation;
  694. Procedure TestClassInterface_COM_With;
  695. Procedure TestClassInterface_COM_ForObjectInInterface;
  696. Procedure TestClassInterface_COM_ForInterfaceInObject;
  697. Procedure TestClassInterface_COM_ArrayOfIntf_AssignVar;
  698. Procedure TestClassInterface_COM_ArrayOfIntf_AssignPlus;
  699. Procedure TestClassInterface_COM_ArrayOfIntf_AssignArg;
  700. Procedure TestClassInterface_COM_ArrayOfIntf_InitFail;
  701. Procedure TestClassInterface_COM_ArrayOfIntf_FunctionResult;
  702. Procedure TestClassInterface_COM_ArrayOfIntf_InheritedFuncResult;
  703. Procedure TestClassInterface_COM_ArrayOfIntf_FunctionExit;
  704. Procedure TestClassInterface_COM_ArrayOfIntf_Property;
  705. Procedure TestClassInterface_COM_ArrayOfIntf_BIFuncs;
  706. Procedure TestClassInterface_COM_ArrayOfIntf_ForIn;
  707. Procedure TestClassInterface_COM_StaticArrayOfIntfFail;
  708. Procedure TestClassInterface_COM_RecordIntfFail;
  709. Procedure TestClassInterface_COM_UnitInitialization;
  710. Procedure TestClassInterface_Corba_GUID;
  711. Procedure TestClassInterface_Corba_GUIDProperty;
  712. // helpers
  713. Procedure TestClassHelper_ClassVar;
  714. Procedure TestClassHelper_Method_AccessInstanceFields;
  715. Procedure TestClassHelper_Method_Call;
  716. Procedure TestClassHelper_Method_Nested_Call;
  717. Procedure TestClassHelper_ClassMethod_Call;
  718. Procedure TestClassHelper_ClassOf;
  719. Procedure TestClassHelper_MethodRefObjFPC;
  720. Procedure TestClassHelper_Constructor;
  721. Procedure TestClassHelper_InheritedObjFPC;
  722. Procedure TestClassHelper_Property;
  723. Procedure TestClassHelper_Property_Array;
  724. Procedure TestClassHelper_Property_Array_Default;
  725. Procedure TestClassHelper_Property_Array_DefaultDefault;
  726. Procedure TestClassHelper_ClassProperty;
  727. Procedure TestClassHelper_ClassPropertyStatic;
  728. Procedure TestClassHelper_ClassProperty_Array;
  729. Procedure TestClassHelper_ForIn;
  730. Procedure TestClassHelper_PassProperty;
  731. Procedure TestExtClassHelper_ClassVar;
  732. Procedure TestExtClassHelper_Method_Call;
  733. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  734. Procedure TestRecordHelper_ClassVar;
  735. Procedure TestRecordHelper_Method_Call;
  736. Procedure TestRecordHelper_Constructor;
  737. Procedure TestTypeHelper_ClassVar;
  738. Procedure TestTypeHelper_PassResultElement;
  739. Procedure TestTypeHelper_PassArgs;
  740. Procedure TestTypeHelper_PassVarConst;
  741. Procedure TestTypeHelper_PassFuncResult;
  742. Procedure TestTypeHelper_PassPropertyField;
  743. Procedure TestTypeHelper_PassPropertyGetter;
  744. Procedure TestTypeHelper_PassClassPropertyField;
  745. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  746. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  747. Procedure TestTypeHelper_Property;
  748. Procedure TestTypeHelper_Property_Array;
  749. Procedure TestTypeHelper_ClassProperty;
  750. Procedure TestTypeHelper_ClassProperty_Array;
  751. Procedure TestTypeHelper_ClassMethod;
  752. Procedure TestTypeHelper_ExtClassMethodFail;
  753. Procedure TestTypeHelper_Constructor;
  754. Procedure TestTypeHelper_Word;
  755. Procedure TestTypeHelper_Boolean;
  756. Procedure TestTypeHelper_WordBool;
  757. Procedure TestTypeHelper_Double;
  758. Procedure TestTypeHelper_NativeInt;
  759. Procedure TestTypeHelper_StringChar;
  760. Procedure TestTypeHelper_JSValue;
  761. Procedure TestTypeHelper_Array;
  762. Procedure TestTypeHelper_EnumType;
  763. Procedure TestTypeHelper_SetType;
  764. Procedure TestTypeHelper_InterfaceType;
  765. Procedure TestTypeHelper_NestedSelf;
  766. // proc types
  767. Procedure TestProcType;
  768. Procedure TestProcType_Arg;
  769. Procedure TestProcType_FunctionFPC;
  770. Procedure TestProcType_FunctionDelphi;
  771. Procedure TestProcType_ProcedureDelphi;
  772. Procedure TestProcType_AsParam;
  773. Procedure TestProcType_MethodFPC;
  774. Procedure TestProcType_MethodDelphi;
  775. Procedure TestProcType_PropertyFPC;
  776. Procedure TestProcType_PropertyDelphi;
  777. Procedure TestProcType_WithClassInstDoPropertyFPC;
  778. Procedure TestProcType_Nested;
  779. Procedure TestProcType_NestedOfObject;
  780. Procedure TestProcType_ReferenceToProc;
  781. Procedure TestProcType_ReferenceToMethod;
  782. Procedure TestProcType_Typecast;
  783. Procedure TestProcType_PassProcToUntyped;
  784. Procedure TestProcType_PassProcToArray;
  785. Procedure TestProcType_SafeCallObjFPC;
  786. Procedure TestProcType_SafeCallDelphi;
  787. Procedure TestProcType_SafeCall_Arg;
  788. // pointer
  789. Procedure TestPointer;
  790. Procedure TestPointer_Proc;
  791. Procedure TestPointer_AssignRecordFail;
  792. Procedure TestPointer_AssignStaticArrayFail;
  793. Procedure TestPointer_TypeCastJSValueToPointer;
  794. Procedure TestPointer_NonRecordFail;
  795. Procedure TestPointer_AnonymousArgTypeFail;
  796. Procedure TestPointer_AnonymousVarTypeFail;
  797. Procedure TestPointer_AnonymousResultTypeFail;
  798. Procedure TestPointer_AddrOperatorFail;
  799. Procedure TestPointer_ArrayParamsFail;
  800. Procedure TestPointer_PointerAddFail;
  801. Procedure TestPointer_IncPointerFail;
  802. Procedure TestPointer_Record;
  803. Procedure TestPointer_RecordArg;
  804. // jsvalue
  805. Procedure TestJSValue_AssignToJSValue;
  806. Procedure TestJSValue_TypeCastToBaseType;
  807. Procedure TestJSValue_TypecastToJSValue;
  808. Procedure TestJSValue_Equal;
  809. Procedure TestJSValue_If;
  810. Procedure TestJSValue_Not;
  811. Procedure TestJSValue_Enum;
  812. Procedure TestJSValue_ClassInstance;
  813. Procedure TestJSValue_ClassOf;
  814. Procedure TestJSValue_ArrayOfJSValue;
  815. Procedure TestJSValue_ArrayLit;
  816. Procedure TestJSValue_Params;
  817. Procedure TestJSValue_UntypedParam;
  818. Procedure TestJSValue_FuncResultType;
  819. Procedure TestJSValue_ProcType_Assign;
  820. Procedure TestJSValue_ProcType_Equal;
  821. Procedure TestJSValue_ProcType_Param;
  822. Procedure TestJSValue_AssignToPointerFail;
  823. Procedure TestJSValue_OverloadDouble;
  824. Procedure TestJSValue_OverloadNativeInt;
  825. Procedure TestJSValue_OverloadWord;
  826. Procedure TestJSValue_OverloadString;
  827. Procedure TestJSValue_OverloadChar;
  828. Procedure TestJSValue_OverloadPointer;
  829. Procedure TestJSValue_ForIn;
  830. // RTTI
  831. Procedure TestRTTI_IntRange;
  832. Procedure TestRTTI_Double;
  833. Procedure TestRTTI_ProcType;
  834. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  835. Procedure TestRTTI_ProcTypeAnonymous;
  836. Procedure TestRTTI_EnumAndSetType;
  837. Procedure TestRTTI_EnumRange;
  838. Procedure TestRTTI_AnonymousEnumType;
  839. Procedure TestRTTI_StaticArray;
  840. Procedure TestRTTI_DynArray;
  841. Procedure TestRTTI_ArrayNestedAnonymous;
  842. Procedure TestRTTI_PublishedMethodOverloadFail;
  843. Procedure TestRTTI_PublishedMethodHideNoHint;
  844. Procedure TestRTTI_PublishedMethodExternalFail;
  845. Procedure TestRTTI_PublishedClassPropertyFail;
  846. Procedure TestRTTI_PublishedClassFieldFail;
  847. Procedure TestRTTI_PublishedFieldExternalFail;
  848. Procedure TestRTTI_Class_Field;
  849. Procedure TestRTTI_Class_FieldPrivate;
  850. Procedure TestRTTI_Class_Method;
  851. Procedure TestRTTI_Class_MethodArgFlags;
  852. Procedure TestRTTI_Class_MethodPrivate;
  853. Procedure TestRTTI_Class_Property;
  854. Procedure TestRTTI_Class_PropertyParams;
  855. Procedure TestRTTI_Class_PropertyPrivate;
  856. Procedure TestRTTI_Class_ClassProperty;
  857. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  858. Procedure TestRTTI_Class_OmitRTTI;
  859. Procedure TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  860. Procedure TestRTTI_IndexModifier;
  861. Procedure TestRTTI_StoredModifier;
  862. Procedure TestRTTI_DefaultValue;
  863. Procedure TestRTTI_DefaultValueSet;
  864. Procedure TestRTTI_DefaultValueRangeType;
  865. Procedure TestRTTI_DefaultValueInherit;
  866. Procedure TestRTTI_OverrideMethod;
  867. Procedure TestRTTI_ReintroduceMethod;
  868. Procedure TestRTTI_OverloadProperty;
  869. // ToDo: array argument
  870. Procedure TestRTTI_ClassForward;
  871. Procedure TestRTTI_ClassOf;
  872. Procedure TestRTTI_Record;
  873. Procedure TestRTTI_RecordAnonymousArray;
  874. Procedure TestRTTI_Record_ClassVarType;
  875. Procedure TestRTTI_LocalTypes;
  876. Procedure TestRTTI_TypeInfo_BaseTypes;
  877. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  878. Procedure TestRTTI_TypeInfo_LocalFail;
  879. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  880. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  881. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  882. Procedure TestRTTI_TypeInfo_FunctionClassType;
  883. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  884. Procedure TestRTTI_Interface_Corba;
  885. Procedure TestRTTI_Interface_COM;
  886. Procedure TestRTTI_ClassHelper;
  887. Procedure TestRTTI_ExternalClass;
  888. Procedure TestRTTI_Unit;
  889. // Resourcestring
  890. Procedure TestResourcestringProgram;
  891. Procedure TestResourcestringUnit;
  892. Procedure TestResourcestringImplementation;
  893. // Attributes
  894. Procedure TestAttributes_Members;
  895. Procedure TestAttributes_Types;
  896. Procedure TestAttributes_HelperConstructor_Fail;
  897. Procedure TestAttributes_InterfacesList;
  898. // Assertions, checks
  899. procedure TestAssert;
  900. procedure TestAssert_SysUtils;
  901. procedure TestObjectChecks;
  902. procedure TestOverflowChecks_Int;
  903. procedure TestRangeChecks_AssignInt;
  904. procedure TestRangeChecks_AssignIntRange;
  905. procedure TestRangeChecks_AssignEnum;
  906. procedure TestRangeChecks_AssignEnumRange;
  907. procedure TestRangeChecks_AssignChar;
  908. procedure TestRangeChecks_AssignCharRange;
  909. procedure TestRangeChecks_ArrayIndex;
  910. procedure TestRangeChecks_ArrayOfRecIndex;
  911. procedure TestRangeChecks_StringIndex;
  912. procedure TestRangeChecks_TypecastInt;
  913. procedure TestRangeChecks_TypeHelperInt;
  914. procedure TestRangeChecks_AssignCurrency;
  915. // Async/AWait
  916. Procedure TestAsync_Proc;
  917. Procedure TestAsync_CallResultIsPromise;
  918. Procedure TestAsync_ConstructorFail;
  919. Procedure TestAsync_PropertyGetterFail;
  920. Procedure TestAwait_NonPromiseWithTypeFail;
  921. Procedure TestAwait_AsyncCallTypeMismatch;
  922. Procedure TestAWait_OutsideAsyncFail;
  923. Procedure TestAWait_IntegerFail;
  924. Procedure TestAWait_ExternalClassPromise;
  925. Procedure TestAWait_JSValue;
  926. Procedure TestAWait_Result;
  927. Procedure TestAWait_ResultPromiseMissingTypeFail; // await(AsyncCallResultPromise) needs T
  928. Procedure TestAsync_AnonymousProc;
  929. Procedure TestAsync_AnonymousProc_PromiseViaDotContext;
  930. Procedure TestAsync_ProcType;
  931. Procedure TestAsync_ProcTypeAsyncModMismatchFail;
  932. Procedure TestAsync_Inherited;
  933. Procedure TestAsync_ClassInterface;
  934. Procedure TestAsync_ClassInterface_AsyncMissmatchFail;
  935. Procedure TestAWait_ClassAs;
  936. // Library
  937. Procedure TestLibrary_Empty;
  938. Procedure TestLibrary_ExportFunc;
  939. Procedure TestLibrary_ExportFuncOverloadedFail;
  940. Procedure TestLibrary_Export_Index_Fail;
  941. Procedure TestLibrary_ExportVar;
  942. Procedure TestLibrary_ExportUnitFunc;
  943. end;
  944. function LinesToStr(Args: array of const): string;
  945. function ExtractFileUnitName(aFilename: string): string;
  946. function JSToStr(El: TJSElement): string;
  947. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  948. implementation
  949. function LinesToStr(Args: array of const): string;
  950. var
  951. s: String;
  952. i: Integer;
  953. begin
  954. s:='';
  955. for i:=Low(Args) to High(Args) do
  956. case Args[i].VType of
  957. vtChar: s += Args[i].VChar+LineEnding;
  958. vtString: s += Args[i].VString^+LineEnding;
  959. vtPChar: s += Args[i].VPChar+LineEnding;
  960. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  961. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  962. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  963. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  964. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  965. end;
  966. Result:=s;
  967. end;
  968. function ExtractFileUnitName(aFilename: string): string;
  969. var
  970. p: Integer;
  971. begin
  972. Result:=ExtractFileName(aFilename);
  973. if Result='' then exit;
  974. for p:=length(Result) downto 1 do
  975. case Result[p] of
  976. '/','\': exit;
  977. '.':
  978. begin
  979. Delete(Result,p,length(Result));
  980. exit;
  981. end;
  982. end;
  983. end;
  984. function JSToStr(El: TJSElement): string;
  985. var
  986. aWriter: TBufferWriter;
  987. aJSWriter: TJSWriter;
  988. begin
  989. aJSWriter:=nil;
  990. aWriter:=TBufferWriter.Create(1000);
  991. try
  992. aJSWriter:=TJSWriter.Create(aWriter);
  993. aJSWriter.IndentSize:=2;
  994. aJSWriter.WriteJS(El);
  995. Result:=aWriter.AsString;
  996. finally
  997. aJSWriter.Free;
  998. aWriter.Free;
  999. end;
  1000. end;
  1001. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  1002. // search diff, ignore changes in spaces
  1003. const
  1004. SpaceChars = [#9,#10,#13,' '];
  1005. var
  1006. ExpectedP, ActualP: PChar;
  1007. function FindLineEnd(p: PChar): PChar;
  1008. begin
  1009. Result:=p;
  1010. while not (Result^ in [#0,#10,#13]) do inc(Result);
  1011. end;
  1012. function FindLineStart(p, MinP: PChar): PChar;
  1013. begin
  1014. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  1015. Result:=p;
  1016. end;
  1017. procedure SkipLineEnd(var p: PChar);
  1018. begin
  1019. if p^ in [#10,#13] then
  1020. begin
  1021. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  1022. inc(p,2)
  1023. else
  1024. inc(p);
  1025. end;
  1026. end;
  1027. function HasSpecialChar(s: string): boolean;
  1028. var
  1029. i: Integer;
  1030. begin
  1031. for i:=1 to length(s) do
  1032. if s[i] in [#0..#31,#127..#255] then
  1033. exit(true);
  1034. Result:=false;
  1035. end;
  1036. function HashSpecialChars(s: string): string;
  1037. var
  1038. i: Integer;
  1039. begin
  1040. Result:='';
  1041. for i:=1 to length(s) do
  1042. if s[i] in [#0..#31,#127..#255] then
  1043. Result:=Result+'#'+hexstr(ord(s[i]),2)
  1044. else
  1045. Result:=Result+s[i];
  1046. end;
  1047. procedure DiffFound;
  1048. var
  1049. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  1050. ExpLine, ActLine: String;
  1051. i, LineNo, DiffLineNo: Integer;
  1052. begin
  1053. writeln('Diff found "',Msg,'". Lines:');
  1054. // write correct lines
  1055. p:=PChar(Expected);
  1056. LineNo:=0;
  1057. DiffLineNo:=0;
  1058. repeat
  1059. StartPos:=p;
  1060. while not (p^ in [#0,#10,#13]) do inc(p);
  1061. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  1062. SkipLineEnd(p);
  1063. inc(LineNo);
  1064. if (p<=ExpectedP) and (p^<>#0) then
  1065. begin
  1066. writeln('= ',ExpLine);
  1067. end else begin
  1068. // diff line
  1069. if DiffLineNo=0 then DiffLineNo:=LineNo;
  1070. // write actual line
  1071. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  1072. ActLineEndP:=FindLineEnd(ActualP);
  1073. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1074. writeln('- ',ActLine);
  1075. if HasSpecialChar(ActLine) then
  1076. writeln('- ',HashSpecialChars(ActLine));
  1077. // write expected line
  1078. writeln('+ ',ExpLine);
  1079. if HasSpecialChar(ExpLine) then
  1080. writeln('- ',HashSpecialChars(ExpLine));
  1081. // write empty line with pointer ^
  1082. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  1083. writeln('^');
  1084. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  1085. CheckSrcDiff:=false;
  1086. // write up to three following actual lines to get some context
  1087. for i:=1 to 3 do begin
  1088. ActLineStartP:=ActLineEndP;
  1089. SkipLineEnd(ActLineStartP);
  1090. if ActLineStartP^=#0 then break;
  1091. ActLineEndP:=FindLineEnd(ActLineStartP);
  1092. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1093. writeln('~ ',ActLine);
  1094. end;
  1095. exit;
  1096. end;
  1097. until p^=#0;
  1098. writeln('DiffFound Actual:-----------------------');
  1099. writeln(Actual);
  1100. writeln('DiffFound Expected:---------------------');
  1101. writeln(Expected);
  1102. writeln('DiffFound ------------------------------');
  1103. Msg:='diff found, but lines are the same, internal error';
  1104. CheckSrcDiff:=false;
  1105. end;
  1106. var
  1107. IsSpaceNeeded: Boolean;
  1108. LastChar, Quote: Char;
  1109. begin
  1110. Result:=true;
  1111. Msg:='';
  1112. if Expected='' then Expected:=' ';
  1113. if Actual='' then Actual:=' ';
  1114. ExpectedP:=PChar(Expected);
  1115. ActualP:=PChar(Actual);
  1116. repeat
  1117. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  1118. case ExpectedP^ of
  1119. #0:
  1120. begin
  1121. // check that rest of Actual has only spaces
  1122. while ActualP^ in SpaceChars do inc(ActualP);
  1123. if ActualP^<>#0 then
  1124. begin
  1125. DiffFound;
  1126. exit;
  1127. end;
  1128. exit(true);
  1129. end;
  1130. ' ',#9,#10,#13:
  1131. begin
  1132. // skip space in Expected
  1133. IsSpaceNeeded:=false;
  1134. if ExpectedP>PChar(Expected) then
  1135. LastChar:=ExpectedP[-1]
  1136. else
  1137. LastChar:=#0;
  1138. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1139. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1140. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1141. IsSpaceNeeded:=true;
  1142. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1143. begin
  1144. DiffFound;
  1145. exit;
  1146. end;
  1147. while ActualP^ in SpaceChars do inc(ActualP);
  1148. end;
  1149. '''','"':
  1150. begin
  1151. while ActualP^ in SpaceChars do inc(ActualP);
  1152. if ExpectedP^<>ActualP^ then
  1153. begin
  1154. DiffFound;
  1155. exit;
  1156. end;
  1157. Quote:=ExpectedP^;
  1158. repeat
  1159. inc(ExpectedP);
  1160. inc(ActualP);
  1161. if ExpectedP^<>ActualP^ then
  1162. begin
  1163. DiffFound;
  1164. exit;
  1165. end;
  1166. if (ExpectedP^ in [#0,#10,#13]) then
  1167. break
  1168. else if (ExpectedP^=Quote) then
  1169. begin
  1170. inc(ExpectedP);
  1171. inc(ActualP);
  1172. break;
  1173. end;
  1174. until false;
  1175. end;
  1176. else
  1177. while ActualP^ in SpaceChars do inc(ActualP);
  1178. if ExpectedP^<>ActualP^ then
  1179. begin
  1180. DiffFound;
  1181. exit;
  1182. end;
  1183. inc(ExpectedP);
  1184. inc(ActualP);
  1185. end;
  1186. until false;
  1187. end;
  1188. { TTestEnginePasResolver }
  1189. procedure TTestEnginePasResolver.SetModule(const AValue: TPasModule);
  1190. begin
  1191. if FModule=AValue then Exit;
  1192. FModule:=AValue;
  1193. end;
  1194. destructor TTestEnginePasResolver.Destroy;
  1195. begin
  1196. FreeAndNil(FStreamResolver);
  1197. FreeAndNil(FParser);
  1198. FreeAndNil(FScanner);
  1199. FreeAndNil(FStreamResolver);
  1200. Module:=nil;
  1201. inherited Destroy;
  1202. end;
  1203. function TTestEnginePasResolver.CreateElement(AClass: TPTreeElement;
  1204. const AName: String; AParent: TPasElement; AVisibility: TPasMemberVisibility;
  1205. const ASrcPos: TPasSourcePos; TypeParams: TFPList): TPasElement;
  1206. begin
  1207. Result:=inherited CreateElement(AClass, AName, AParent, AVisibility, ASrcPos,
  1208. TypeParams);
  1209. if (FModule=nil) and AClass.InheritsFrom(TPasModule) then
  1210. Module:=TPasModule(Result);
  1211. end;
  1212. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1213. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1214. begin
  1215. Result:=nil;
  1216. if InFilename<>'' then
  1217. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1218. if Assigned(OnFindUnit) then
  1219. Result:=OnFindUnit(AName);
  1220. if NameExpr=nil then ;
  1221. end;
  1222. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1223. begin
  1224. // do not parse recursively
  1225. // parse via the queue
  1226. if Section=nil then ;
  1227. end;
  1228. { TCustomTestModule }
  1229. procedure TCustomTestModule.FreeSrcMarkers;
  1230. var
  1231. aMarker, Last: PSrcMarker;
  1232. begin
  1233. aMarker:=FirstSrcMarker;
  1234. while aMarker<>nil do
  1235. begin
  1236. Last:=aMarker;
  1237. aMarker:=aMarker^.Next;
  1238. Dispose(Last);
  1239. end;
  1240. FirstSrcMarker:=nil;
  1241. LastSrcMarker:=nil;
  1242. end;
  1243. function TCustomTestModule.GetResolverCount: integer;
  1244. begin
  1245. Result:=FResolvers.Count;
  1246. end;
  1247. function TCustomTestModule.GetResolvers(Index: integer): TTestEnginePasResolver;
  1248. begin
  1249. Result:=TTestEnginePasResolver(FResolvers[Index]);
  1250. end;
  1251. function TCustomTestModule.GetMsgCount: integer;
  1252. begin
  1253. Result:=FHintMsgs.Count;
  1254. end;
  1255. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1256. begin
  1257. Result:=TTestHintMessage(FHintMsgs[Index]);
  1258. end;
  1259. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1260. ): TPasModule;
  1261. var
  1262. DefNamespace: String;
  1263. begin
  1264. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1265. if (Pos('.',aUnitName)<1) then
  1266. begin
  1267. DefNamespace:=GetDefaultNamespace;
  1268. if DefNamespace<>'' then
  1269. begin
  1270. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1271. if Result<>nil then exit;
  1272. end;
  1273. end;
  1274. Result:=LoadUnit(aUnitName);
  1275. if Result<>nil then exit;
  1276. {$IFDEF VerbosePas2JS}
  1277. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1278. {$ENDIF}
  1279. Fail('can''t find unit "'+aUnitName+'"');
  1280. end;
  1281. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1282. var
  1283. aParser: TPasParser;
  1284. Item: TTestHintMessage;
  1285. begin
  1286. aParser:=Sender as TPasParser;
  1287. Item:=TTestHintMessage.Create;
  1288. Item.Id:=aParser.LastMsgNumber;
  1289. Item.MsgType:=aParser.LastMsgType;
  1290. Item.MsgNumber:=aParser.LastMsgNumber;
  1291. Item.Msg:=Msg;
  1292. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1293. {$IFDEF VerbosePas2JS}
  1294. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1295. {$ENDIF}
  1296. FHintMsgs.Add(Item);
  1297. end;
  1298. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1299. );
  1300. var
  1301. aResolver: TTestEnginePasResolver;
  1302. Item: TTestHintMessage;
  1303. begin
  1304. aResolver:=Sender as TTestEnginePasResolver;
  1305. Item:=TTestHintMessage.Create;
  1306. Item.Id:=aResolver.LastMsgId;
  1307. Item.MsgType:=aResolver.LastMsgType;
  1308. Item.MsgNumber:=aResolver.LastMsgNumber;
  1309. Item.Msg:=Msg;
  1310. Item.SourcePos:=aResolver.LastSourcePos;
  1311. {$IFDEF VerbosePas2JS}
  1312. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1313. {$ENDIF}
  1314. FHintMsgs.Add(Item);
  1315. end;
  1316. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1317. var
  1318. Item: TTestHintMessage;
  1319. aScanner: TPas2jsPasScanner;
  1320. begin
  1321. aScanner:=Sender as TPas2jsPasScanner;
  1322. Item:=TTestHintMessage.Create;
  1323. Item.Id:=aScanner.LastMsgNumber;
  1324. Item.MsgType:=aScanner.LastMsgType;
  1325. Item.MsgNumber:=aScanner.LastMsgNumber;
  1326. Item.Msg:=Msg;
  1327. Item.SourcePos:=aScanner.CurSourcePos;
  1328. {$IFDEF VerbosePas2JS}
  1329. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1330. {$ENDIF}
  1331. FHintMsgs.Add(Item);
  1332. end;
  1333. procedure TCustomTestModule.OnCheckElementParent(El: TPasElement; arg: pointer);
  1334. var
  1335. SubEl: TPasElement;
  1336. i: Integer;
  1337. procedure E(Msg: string);
  1338. var
  1339. s: String;
  1340. begin
  1341. s:='TCustomTestModule.OnCheckElementParent El='+GetTreeDbg(El)+' '+
  1342. ResolverEngine.GetElementSourcePosStr(El)+' '+Msg;
  1343. writeln('ERROR: ',s);
  1344. Fail(s);
  1345. end;
  1346. begin
  1347. if arg=nil then ;
  1348. if El=nil then exit;
  1349. if El.Parent=El then
  1350. E('El.Parent=El='+GetObjName(El));
  1351. if El is TBinaryExpr then
  1352. begin
  1353. if (TBinaryExpr(El).left<>nil) and (TBinaryExpr(El).left.Parent<>El) then
  1354. E('TBinaryExpr(El).left.Parent='+GetObjName(TBinaryExpr(El).left.Parent)+'<>El');
  1355. if (TBinaryExpr(El).right<>nil) and (TBinaryExpr(El).right.Parent<>El) then
  1356. E('TBinaryExpr(El).right.Parent='+GetObjName(TBinaryExpr(El).right.Parent)+'<>El');
  1357. end
  1358. else if El is TParamsExpr then
  1359. begin
  1360. if (TParamsExpr(El).Value<>nil) and (TParamsExpr(El).Value.Parent<>El) then
  1361. E('TParamsExpr(El).Value.Parent='+GetObjName(TParamsExpr(El).Value.Parent)+'<>El');
  1362. for i:=0 to length(TParamsExpr(El).Params)-1 do
  1363. if TParamsExpr(El).Params[i].Parent<>El then
  1364. E('TParamsExpr(El).Params[i].Parent='+GetObjName(TParamsExpr(El).Params[i].Parent)+'<>El');
  1365. end
  1366. else if El is TProcedureExpr then
  1367. begin
  1368. if (TProcedureExpr(El).Proc<>nil) and (TProcedureExpr(El).Proc.Parent<>El) then
  1369. E('TProcedureExpr(El).Proc.Parent='+GetObjName(TProcedureExpr(El).Proc.Parent)+'<>El');
  1370. end
  1371. else if El is TPasDeclarations then
  1372. begin
  1373. for i:=0 to TPasDeclarations(El).Declarations.Count-1 do
  1374. begin
  1375. SubEl:=TPasElement(TPasDeclarations(El).Declarations[i]);
  1376. if SubEl.Parent<>El then
  1377. E('SubEl=TPasElement(TPasDeclarations(El).Declarations[i])='+GetObjName(SubEl)+' SubEl.Parent='+GetObjName(SubEl.Parent)+'<>El');
  1378. end;
  1379. end
  1380. else if El is TPasImplBlock then
  1381. begin
  1382. for i:=0 to TPasImplBlock(El).Elements.Count-1 do
  1383. begin
  1384. SubEl:=TPasElement(TPasImplBlock(El).Elements[i]);
  1385. if SubEl.Parent<>El then
  1386. E('TPasElement(TPasImplBlock(El).Elements[i]).Parent='+GetObjName(SubEl.Parent)+'<>El');
  1387. end;
  1388. end
  1389. else if El is TPasImplWithDo then
  1390. begin
  1391. for i:=0 to TPasImplWithDo(El).Expressions.Count-1 do
  1392. begin
  1393. SubEl:=TPasExpr(TPasImplWithDo(El).Expressions[i]);
  1394. if SubEl.Parent<>El then
  1395. E('TPasExpr(TPasImplWithDo(El).Expressions[i]).Parent='+GetObjName(SubEl.Parent)+'<>El');
  1396. end;
  1397. end
  1398. else if El is TPasProcedure then
  1399. begin
  1400. if TPasProcedure(El).ProcType.Parent<>El then
  1401. E('TPasProcedure(El).ProcType.Parent='+GetObjName(TPasProcedure(El).ProcType.Parent)+'<>El');
  1402. end
  1403. else if El is TPasProcedureType then
  1404. begin
  1405. for i:=0 to TPasProcedureType(El).Args.Count-1 do
  1406. if TPasArgument(TPasProcedureType(El).Args[i]).Parent<>El then
  1407. E('TPasArgument(TPasProcedureType(El).Args[i]).Parent='+GetObjName(TPasArgument(TPasProcedureType(El).Args[i]).Parent)+'<>El');
  1408. end;
  1409. end;
  1410. procedure TCustomTestModule.OnFindReference(El: TPasElement; FindData: pointer);
  1411. var
  1412. Data: PTestResolverReferenceData absolute FindData;
  1413. Line, Col: integer;
  1414. begin
  1415. ResolverEngine.UnmangleSourceLineNumber(El.SourceLinenumber,Line,Col);
  1416. //writeln('TCustomTestModule.OnFindReference ',El.SourceFilename,' Line=',Line,',Col=',Col,' ',GetObjName(El),' SearchFile=',Data^.Filename,',Line=',Data^.Row,',Col=',Data^.StartCol,'-',Data^.EndCol);
  1417. if (Data^.Filename=El.SourceFilename)
  1418. and (Data^.Row=Line)
  1419. and (Data^.StartCol<=Col)
  1420. and (Data^.EndCol>=Col)
  1421. then
  1422. Data^.Found.Add(El);
  1423. end;
  1424. procedure TCustomTestModule.SetWithTypeInfo(const AValue: boolean);
  1425. begin
  1426. if FWithTypeInfo=AValue then Exit;
  1427. FWithTypeInfo:=AValue;
  1428. if AValue then
  1429. Converter.Options:=Converter.Options-[coNoTypeInfo]
  1430. else
  1431. Converter.Options:=Converter.Options+[coNoTypeInfo];
  1432. end;
  1433. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1434. var
  1435. i: Integer;
  1436. CurEngine: TTestEnginePasResolver;
  1437. CurUnitName: String;
  1438. begin
  1439. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1440. Result:=nil;
  1441. if (Module.ClassType=TPasModule)
  1442. and (CompareText(Module.Name,aUnitName)=0) then
  1443. exit(Module);
  1444. for i:=0 to ResolverCount-1 do
  1445. begin
  1446. CurEngine:=Resolvers[i];
  1447. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1448. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1449. if CompareText(aUnitName,CurUnitName)=0 then
  1450. begin
  1451. Result:=CurEngine.Module;
  1452. if Result<>nil then exit;
  1453. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1454. FileResolver.FindSourceFile(aUnitName);
  1455. CurEngine.StreamResolver:=TStreamResolver.Create;
  1456. CurEngine.StreamResolver.OwnsStreams:=True;
  1457. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1458. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1459. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1460. InitScanner(CurEngine.Scanner);
  1461. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1462. CurEngine.Parser.Options:=po_tcmodules;
  1463. if CompareText(CurUnitName,'System')=0 then
  1464. CurEngine.Parser.ImplicitUses.Clear;
  1465. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1466. try
  1467. CurEngine.Parser.NextToken;
  1468. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1469. except
  1470. on E: Exception do
  1471. HandleException(E);
  1472. end;
  1473. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1474. Result:=CurEngine.Module;
  1475. exit;
  1476. end;
  1477. end;
  1478. end;
  1479. procedure TCustomTestModule.SetUp;
  1480. begin
  1481. {$IFDEF EnablePasTreeGlobalRefCount}
  1482. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1483. {$ENDIF}
  1484. if FResolvers<>nil then
  1485. begin
  1486. writeln('TCustomTestModule.SetUp FModules<>nil');
  1487. Halt;
  1488. end;
  1489. inherited SetUp;
  1490. FSkipTests:=false;
  1491. FWithTypeInfo:=false;
  1492. FSource:=TStringList.Create;
  1493. FHub:=TPas2JSResolverHub.Create(Self);
  1494. FResolvers:=TObjectList.Create(true);
  1495. FFilename:='test1.pp';
  1496. FFileResolver:=TStreamResolver.Create;
  1497. FFileResolver.OwnsStreams:=True;
  1498. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1499. InitScanner(FScanner);
  1500. FEngine:=AddModule(Filename);
  1501. FEngine.Scanner:=FScanner;
  1502. FScanner.Resolver:=FEngine;
  1503. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1504. FParser.OnLog:=@OnParserLog;
  1505. FEngine.Parser:=FParser;
  1506. Parser.Options:=po_tcmodules;
  1507. FModule:=Nil;
  1508. FConverter:=CreateConverter;
  1509. FExpectedErrorClass:=nil;
  1510. end;
  1511. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1512. var
  1513. Options: TPasToJsConverterOptions;
  1514. begin
  1515. Result:=TPasToJSConverter.Create;
  1516. Options:=co_tcmodules;
  1517. if WithTypeInfo then
  1518. Exclude(Options,coNoTypeInfo)
  1519. else
  1520. Include(Options,coNoTypeInfo);
  1521. Result.Options:=Options;
  1522. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1523. end;
  1524. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1525. begin
  1526. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1527. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1528. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1529. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1530. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1531. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1532. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1533. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1534. aScanner.OnLog:=@OnScannerLog;
  1535. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1536. end;
  1537. procedure TCustomTestModule.TearDown;
  1538. {$IFDEF CheckPasTreeRefCount}
  1539. var
  1540. El: TPasElement;
  1541. {$ENDIF}
  1542. var
  1543. i: Integer;
  1544. CurModule: TPasModule;
  1545. begin
  1546. FreeSrcMarkers;
  1547. FHintMsgs.Clear;
  1548. FHintMsgsGood.Clear;
  1549. FSkipTests:=false;
  1550. FWithTypeInfo:=false;
  1551. FJSRegModuleCall:=nil;
  1552. FJSModuleCallArgs:=nil;
  1553. FJSImplentationUses:=nil;
  1554. FJSInterfaceUses:=nil;
  1555. FJSModuleSrc:=nil;
  1556. FJSInitBody:=nil;
  1557. FreeAndNil(FJSSource);
  1558. FreeAndNil(FJSModule);
  1559. FreeAndNil(FConverter);
  1560. ResolverEngine.Clear;
  1561. FreeAndNil(FSource);
  1562. FreeAndNil(FFileResolver);
  1563. if FResolvers<>nil then
  1564. begin
  1565. for i:=0 to FResolvers.Count-1 do
  1566. begin
  1567. CurModule:=TTestEnginePasResolver(FResolvers[i]).Module;
  1568. if CurModule=nil then continue;
  1569. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1570. end;
  1571. for i:=0 to FResolvers.Count-1 do
  1572. begin
  1573. CurModule:=TTestEnginePasResolver(FResolvers[i]).Module;
  1574. if CurModule=nil then continue;
  1575. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1576. end;
  1577. FreeAndNil(FResolvers);
  1578. FModule:=nil;
  1579. FEngine:=nil;
  1580. end;
  1581. FreeAndNil(FHub);
  1582. inherited TearDown;
  1583. {$IFDEF EnablePasTreeGlobalRefCount}
  1584. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1585. begin
  1586. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1587. {$IFDEF CheckPasTreeRefCount}
  1588. El:=TPasElement.FirstRefEl;
  1589. while El<>nil do
  1590. begin
  1591. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1592. for i:=0 to El.RefIds.Count-1 do
  1593. writeln(' ',El.RefIds[i]);
  1594. El:=El.NextRefEl;
  1595. end;
  1596. {$ENDIF}
  1597. Halt;
  1598. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1599. end;
  1600. {$ENDIF}
  1601. end;
  1602. procedure TCustomTestModule.Add(Line: string);
  1603. begin
  1604. Source.Add(Line);
  1605. end;
  1606. procedure TCustomTestModule.Add(const Lines: array of string);
  1607. var
  1608. i: Integer;
  1609. begin
  1610. for i:=low(Lines) to high(Lines) do
  1611. Add(Lines[i]);
  1612. end;
  1613. procedure TCustomTestModule.StartParsing;
  1614. var
  1615. Src: String;
  1616. begin
  1617. Src:=Source.Text;
  1618. FEngine.Source:=Src;
  1619. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1620. Scanner.OpenFile(FileName);
  1621. Writeln('// Test : ',Self.TestName);
  1622. Writeln(Src);
  1623. end;
  1624. procedure TCustomTestModule.ParseModuleQueue;
  1625. var
  1626. i: Integer;
  1627. CurResolver: TTestEnginePasResolver;
  1628. Found: Boolean;
  1629. Section: TPasSection;
  1630. begin
  1631. // parse til exception or all Resolvers finished
  1632. while not SkipTests do
  1633. begin
  1634. Found:=false;
  1635. for i:=0 to ResolverCount-1 do
  1636. begin
  1637. CurResolver:=Resolvers[i];
  1638. if CurResolver.CurrentParser=nil then continue;
  1639. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1640. continue;
  1641. CurResolver.Parser.ParseContinue;
  1642. Found:=true;
  1643. break;
  1644. end;
  1645. if not Found then break;
  1646. end;
  1647. for i:=0 to ResolverCount-1 do
  1648. begin
  1649. CurResolver:=Resolvers[i];
  1650. if CurResolver.Parser=nil then
  1651. begin
  1652. if CurResolver.CurrentParser<>nil then
  1653. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1654. continue;
  1655. end;
  1656. if CurResolver.Parser.CurModule<>nil then
  1657. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1658. end;
  1659. end;
  1660. procedure TCustomTestModule.ParseModule;
  1661. begin
  1662. if SkipTests then exit;
  1663. FFirstPasStatement:=nil;
  1664. try
  1665. StartParsing;
  1666. Parser.ParseMain(FModule);
  1667. ParseModuleQueue;
  1668. except
  1669. on E: Exception do
  1670. HandleException(E);
  1671. end;
  1672. if SkipTests then exit;
  1673. AssertNotNull('Module resulted in Module',Module);
  1674. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1675. TAssert.AssertSame('Has resolver',ResolverEngine,Parser.Engine);
  1676. end;
  1677. procedure TCustomTestModule.ParseProgram;
  1678. begin
  1679. if SkipTests then exit;
  1680. ParseModule;
  1681. if SkipTests then exit;
  1682. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1683. FPasProgram:=TPasProgram(Module);
  1684. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1685. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1686. if (PasProgram.InitializationSection.Elements.Count>0) then
  1687. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1688. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1689. end;
  1690. procedure TCustomTestModule.ParseLibrary;
  1691. var
  1692. Init: TInitializationSection;
  1693. begin
  1694. if SkipTests then exit;
  1695. ParseModule;
  1696. if SkipTests then exit;
  1697. AssertEquals('Has library',TPasLibrary,Module.ClassType);
  1698. FPasLibrary:=TPasLibrary(Module);
  1699. AssertNotNull('Has library section',PasLibrary.LibrarySection);
  1700. Init:=PasLibrary.InitializationSection;
  1701. if (Init<>nil) and (Init.Elements.Count>0) then
  1702. if TObject(Init.Elements[0]) is TPasImplBlock then
  1703. FFirstPasStatement:=TPasImplBlock(PasLibrary.InitializationSection.Elements[0]);
  1704. end;
  1705. procedure TCustomTestModule.ParseUnit;
  1706. begin
  1707. if SkipTests then exit;
  1708. ParseModule;
  1709. if SkipTests then exit;
  1710. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1711. AssertNotNull('Has interface section',Module.InterfaceSection);
  1712. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1713. if (Module.InitializationSection<>nil)
  1714. and (Module.InitializationSection.Elements.Count>0)
  1715. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1716. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1717. end;
  1718. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1719. ): TTestEnginePasResolver;
  1720. var
  1721. i: Integer;
  1722. begin
  1723. for i:=0 to ResolverCount-1 do
  1724. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1725. exit(Resolvers[i]);
  1726. Result:=nil;
  1727. end;
  1728. function TCustomTestModule.AddModule(aFilename: string
  1729. ): TTestEnginePasResolver;
  1730. begin
  1731. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1732. if FindModuleWithFilename(aFilename)<>nil then
  1733. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1734. Result:=TTestEnginePasResolver.Create;
  1735. Result.Filename:=aFilename;
  1736. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1737. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1738. Result.OnLog:=@OnPasResolverLog;
  1739. Result.Hub:=Hub;
  1740. FResolvers.Add(Result);
  1741. end;
  1742. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1743. ): TTestEnginePasResolver;
  1744. begin
  1745. Result:=AddModule(aFilename);
  1746. Result.Source:=Src;
  1747. end;
  1748. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1749. ImplementationSrc: string): TTestEnginePasResolver;
  1750. var
  1751. Src: String;
  1752. begin
  1753. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1754. Src+=LineEnding;
  1755. Src+='interface'+LineEnding;
  1756. Src+=LineEnding;
  1757. Src+=InterfaceSrc;
  1758. Src+='implementation'+LineEnding;
  1759. Src+=LineEnding;
  1760. Src+=ImplementationSrc;
  1761. Src+='end.'+LineEnding;
  1762. Result:=AddModuleWithSrc(aFilename,Src);
  1763. end;
  1764. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1765. var
  1766. Intf, Impl: TStringList;
  1767. begin
  1768. Intf:=TStringList.Create;
  1769. if supTInterfacedObject in Parts then Include(Parts,supTObject);
  1770. // unit interface
  1771. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1772. Intf.Add('{$modeswitch externalclass}');
  1773. Intf.Add('type');
  1774. Intf.Add(' integer=longint;');
  1775. Intf.Add(' sizeint=nativeint;');
  1776. //'const',
  1777. //' LineEnding = #10;',
  1778. //' DirectorySeparator = ''/'';',
  1779. //' DriveSeparator = '''';',
  1780. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1781. //' AllowDriveSeparators : set of char = [];',
  1782. if supTObject in Parts then
  1783. Intf.AddStrings([
  1784. 'type',
  1785. ' TClass = class of TObject;',
  1786. ' TObject = class',
  1787. ' constructor Create;',
  1788. ' destructor Destroy; virtual;',
  1789. ' class function ClassType: TClass; assembler;',
  1790. ' class function ClassName: String; assembler;',
  1791. ' class function ClassNameIs(const Name: string): boolean;',
  1792. ' class function ClassParent: TClass; assembler;',
  1793. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1794. ' class function UnitName: String; assembler;',
  1795. ' procedure AfterConstruction; virtual;',
  1796. ' procedure BeforeDestruction;virtual;',
  1797. ' function Equals(Obj: TObject): boolean; virtual;',
  1798. ' function ToString: String; virtual;',
  1799. ' end;']);
  1800. if supTInterfacedObject in Parts then
  1801. Intf.AddStrings([
  1802. ' {$Interfaces COM}',
  1803. ' IUnknown = interface',
  1804. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1805. //' function QueryInterface(const iid: TGuid; out obj): Integer;',
  1806. ' function _AddRef: Integer;',
  1807. ' function _Release: Integer;',
  1808. ' end;',
  1809. ' IInterface = IUnknown;',
  1810. ' TInterfacedObject = class(TObject,IUnknown)',
  1811. ' protected',
  1812. ' fRefCount: Integer;',
  1813. ' { implement methods of IUnknown }',
  1814. //' function QueryInterface(const iid: TGuid; out obj): Integer; virtual;',
  1815. ' function _AddRef: Integer; virtual;',
  1816. ' function _Release: Integer; virtual;',
  1817. ' end;',
  1818. ' TInterfacedClass = class of TInterfacedObject;',
  1819. '',
  1820. '']);
  1821. if supTVarRec in Parts then
  1822. Intf.AddStrings([
  1823. 'const',
  1824. ' vtInteger = 0;',
  1825. ' vtBoolean = 1;',
  1826. ' vtJSValue = 19;',
  1827. 'type',
  1828. ' PVarRec = ^TVarRec;',
  1829. ' TVarRec = record',
  1830. ' VType : byte;',
  1831. ' VJSValue: JSValue;',
  1832. ' vInteger: longint external name ''VJSValue'';',
  1833. ' vBoolean: boolean external name ''VJSValue'';',
  1834. ' end;',
  1835. ' TVarRecArray = array of TVarRec;',
  1836. 'function VarRecs: TVarRecArray; varargs;',
  1837. '']);
  1838. if supTypeInfo in Parts then
  1839. begin
  1840. Intf.AddStrings([
  1841. 'type',
  1842. ' TTypeKind = (',
  1843. ' tkUnknown, // 0',
  1844. ' tkInteger, // 1',
  1845. ' tkChar, // 2 in Delphi/FPC tkWChar, tkUChar',
  1846. ' tkString, // 3 in Delphi/FPC tkSString, tkWString or tkUString',
  1847. ' tkEnumeration, // 4',
  1848. ' tkSet, // 5',
  1849. ' tkDouble, // 6',
  1850. ' tkBool, // 7',
  1851. ' tkProcVar, // 8 function or procedure',
  1852. ' tkMethod, // 9 proc var of object',
  1853. ' tkArray, // 10 static array',
  1854. ' tkDynArray, // 11',
  1855. ' tkRecord, // 12',
  1856. ' tkClass, // 13',
  1857. ' tkClassRef, // 14',
  1858. ' tkPointer, // 15',
  1859. ' tkJSValue, // 16',
  1860. ' tkRefToProcVar, // 17 variable of procedure type',
  1861. ' tkInterface, // 18',
  1862. ' //tkObject,',
  1863. ' //tkSString,tkLString,tkAString,tkWString,',
  1864. ' //tkVariant,',
  1865. ' //tkWChar,',
  1866. ' //tkInt64,',
  1867. ' //tkQWord,',
  1868. ' //tkInterfaceRaw,',
  1869. ' //tkUString,tkUChar,',
  1870. ' tkHelper, // 19',
  1871. ' //tkFile,',
  1872. ' tkExtClass // 20',
  1873. ' );',
  1874. ' TTypeKinds = set of TTypeKind;',
  1875. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1876. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1877. ' end;',
  1878. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1879. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1880. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1881. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1882. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1883. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1884. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1885. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1886. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1887. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1888. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1889. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1890. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1891. '']);
  1892. end;
  1893. if supWriteln in Parts then
  1894. Intf.Add('procedure writeln; varargs; external name ''console.log'';');
  1895. Intf.Add('var');
  1896. Intf.Add(' ExitCode: Longint = 0;');
  1897. // unit implementation
  1898. Impl:=TStringList.Create;
  1899. if supTObject in Parts then
  1900. Impl.AddStrings([
  1901. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1902. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1903. 'asm',
  1904. 'end;',
  1905. 'constructor TObject.Create; begin end;',
  1906. 'destructor TObject.Destroy; begin end;',
  1907. 'class function TObject.ClassType: TClass; assembler;',
  1908. 'asm',
  1909. 'end;',
  1910. 'class function TObject.ClassName: String; assembler;',
  1911. 'asm',
  1912. 'end;',
  1913. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1914. 'begin',
  1915. ' Result:=SameText(Name,ClassName);',
  1916. 'end;',
  1917. 'class function TObject.ClassParent: TClass; assembler;',
  1918. 'asm',
  1919. 'end;',
  1920. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1921. 'asm',
  1922. 'end;',
  1923. 'class function TObject.UnitName: String; assembler;',
  1924. 'asm',
  1925. 'end;',
  1926. 'procedure TObject.AfterConstruction; begin end;',
  1927. 'procedure TObject.BeforeDestruction; begin end;',
  1928. 'function TObject.Equals(Obj: TObject): boolean;',
  1929. 'begin',
  1930. ' Result:=Obj=Self;',
  1931. 'end;',
  1932. 'function TObject.ToString: String;',
  1933. 'begin',
  1934. ' Result:=ClassName;',
  1935. 'end;'
  1936. ]);
  1937. if supTInterfacedObject in Parts then
  1938. Impl.AddStrings([
  1939. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1940. //'begin',
  1941. //'end;',
  1942. 'function TInterfacedObject._AddRef: Integer;',
  1943. 'begin',
  1944. 'end;',
  1945. 'function TInterfacedObject._Release: Integer;',
  1946. 'begin',
  1947. 'end;',
  1948. '']);
  1949. if supTVarRec in Parts then
  1950. Impl.AddStrings([
  1951. 'function VarRecs: TVarRecArray; varargs;',
  1952. 'var',
  1953. ' v: PVarRec;',
  1954. 'begin',
  1955. ' v^.VType:=1;',
  1956. ' v^.VJSValue:=2;',
  1957. 'end;',
  1958. '']);
  1959. try
  1960. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1961. finally
  1962. Intf.Free;
  1963. Impl.Free;
  1964. end;
  1965. end;
  1966. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1967. SystemUnitParts: TSystemUnitParts);
  1968. begin
  1969. if NeedSystemUnit then
  1970. AddSystemUnit(SystemUnitParts)
  1971. else
  1972. Parser.ImplicitUses.Clear;
  1973. Add('program '+ExtractFileUnitName(Filename)+';');
  1974. Add('');
  1975. end;
  1976. procedure TCustomTestModule.StartLibrary(NeedSystemUnit: boolean;
  1977. SystemUnitParts: TSystemUnitParts);
  1978. begin
  1979. if NeedSystemUnit then
  1980. AddSystemUnit(SystemUnitParts)
  1981. else
  1982. Parser.ImplicitUses.Clear;
  1983. Add('library '+ExtractFileUnitName(Filename)+';');
  1984. Add('');
  1985. end;
  1986. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1987. SystemUnitParts: TSystemUnitParts);
  1988. begin
  1989. if NeedSystemUnit then
  1990. AddSystemUnit(SystemUnitParts)
  1991. else
  1992. Parser.ImplicitUses.Clear;
  1993. Add('unit Test1;');
  1994. Add('');
  1995. end;
  1996. procedure TCustomTestModule.ConvertModule;
  1997. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1998. out UsesLit: TJSArrayLiteral);
  1999. var
  2000. i: Integer;
  2001. Item: TJSElement;
  2002. Lit: TJSLiteral;
  2003. begin
  2004. UsesLit:=nil;
  2005. AssertNotNull(UsesName+' uses section',Arg.Expr);
  2006. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  2007. exit; // null is ok
  2008. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  2009. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  2010. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  2011. begin
  2012. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  2013. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  2014. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  2015. Lit:=TJSLiteral(Item);
  2016. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  2017. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  2018. end;
  2019. end;
  2020. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  2021. out Src: TJSSourceElements);
  2022. var
  2023. FunDecl: TJSFunctionDeclarationStatement;
  2024. FunDef: TJSFuncDef;
  2025. FunBody: TJSFunctionBody;
  2026. begin
  2027. Src:=nil;
  2028. AssertNotNull(ParamName,Arg.Expr);
  2029. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  2030. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  2031. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  2032. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  2033. FunDef:=FunDecl.AFunction as TJSFuncDef;
  2034. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  2035. AssertNotNull(ParamName+' body',FunDef.Body);
  2036. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  2037. FunBody:=FunDef.Body as TJSFunctionBody;
  2038. AssertNotNull(ParamName+' body.A',FunBody.A);
  2039. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  2040. Src:=FunBody.A as TJSSourceElements;
  2041. end;
  2042. var
  2043. ModuleNameExpr: TJSLiteral;
  2044. InitFunction: TJSFunctionDeclarationStatement;
  2045. InitAssign: TJSSimpleAssignStatement;
  2046. InitName: String;
  2047. LastNode, FirstNode: TJSElement;
  2048. Arg: TJSArrayLiteralElement;
  2049. IsProg, IsLib: Boolean;
  2050. begin
  2051. if SkipTests then exit;
  2052. IsProg:=false;
  2053. IsLib:=false;
  2054. if Module is TPasProgram then
  2055. IsProg:=true
  2056. else if Module is TPasLibrary then
  2057. IsLib:=true;
  2058. try
  2059. FJSModule:=FConverter.ConvertPasElement(Module,ResolverEngine) as TJSSourceElements;
  2060. except
  2061. on E: Exception do
  2062. HandleException(E);
  2063. end;
  2064. if SkipTests then exit;
  2065. if ExpectedErrorClass<>nil then
  2066. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  2067. FJSSource:=TStringList.Create;
  2068. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  2069. {$IFDEF VerbosePas2JS}
  2070. writeln('TTestModule.ConvertModule JS:');
  2071. write(FJSSource.Text);
  2072. {$ENDIF}
  2073. // rtl.module(...
  2074. if JSModule.Statements.Count<1 then
  2075. AssertEquals('jsmodule has at least one statement - the call',1,JSModule.Statements.Count);
  2076. FirstNode:=JSModule.Statements.Nodes[0].Node;
  2077. AssertNotNull('register module call',FirstNode);
  2078. AssertEquals('register module call',TJSCallExpression,FirstNode.ClassType);
  2079. FJSRegModuleCall:=FirstNode as TJSCallExpression;
  2080. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  2081. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  2082. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  2083. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  2084. // parameter 'unitname'
  2085. if JSModuleCallArgs.Elements.Count<1 then
  2086. Fail('rtl.module first param unit missing');
  2087. Arg:=JSModuleCallArgs.Elements.Elements[0];
  2088. AssertNotNull('module name param',Arg.Expr);
  2089. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  2090. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  2091. if IsProg then
  2092. begin
  2093. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString));
  2094. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  2095. end
  2096. else if IsLib then
  2097. AssertEquals('module name','library',String(ModuleNameExpr.Value.AsString))
  2098. else
  2099. begin
  2100. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  2101. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  2102. end;
  2103. // main uses section
  2104. if JSModuleCallArgs.Elements.Count<2 then
  2105. Fail('rtl.module second param main uses missing');
  2106. Arg:=JSModuleCallArgs.Elements.Elements[1];
  2107. CheckUsesList('interface',Arg,FJSInterfaceUses);
  2108. // program/library/interface function()
  2109. if JSModuleCallArgs.Elements.Count<3 then
  2110. Fail('rtl.module third param intf-function missing');
  2111. Arg:=JSModuleCallArgs.Elements.Elements[2];
  2112. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  2113. // search for $mod.$init or $mod.$main - the last statement
  2114. if IsProg or IsLib then
  2115. begin
  2116. InitName:='$main';
  2117. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  2118. end
  2119. else
  2120. InitName:='$init';
  2121. InitAssign:=nil;
  2122. InitFunction:=nil;
  2123. FJSInitBody:=nil;
  2124. if JSModuleSrc.Statements.Count>0 then
  2125. begin
  2126. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  2127. if LastNode is TJSSimpleAssignStatement then
  2128. begin
  2129. InitAssign:=LastNode as TJSSimpleAssignStatement;
  2130. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  2131. begin
  2132. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  2133. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  2134. end
  2135. else if IsProg or IsLib then
  2136. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  2137. end;
  2138. end;
  2139. // optional: implementation uses section
  2140. if JSModuleCallArgs.Elements.Count<4 then
  2141. exit;
  2142. Arg:=JSModuleCallArgs.Elements.Elements[3];
  2143. CheckUsesList('implementation',Arg,FJSImplentationUses);
  2144. end;
  2145. procedure TCustomTestModule.ConvertProgram;
  2146. begin
  2147. Add('end.');
  2148. ParseProgram;
  2149. ConvertModule;
  2150. end;
  2151. procedure TCustomTestModule.ConvertLibrary;
  2152. begin
  2153. Add('end.');
  2154. ParseLibrary;
  2155. ConvertModule;
  2156. end;
  2157. procedure TCustomTestModule.ConvertUnit;
  2158. begin
  2159. Add('end.');
  2160. ParseUnit;
  2161. ConvertModule;
  2162. end;
  2163. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  2164. begin
  2165. Result:=tcmodules.JSToStr(El);
  2166. end;
  2167. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  2168. DottedName: string);
  2169. begin
  2170. if DottedName='' then
  2171. begin
  2172. AssertNull(Msg,El);
  2173. end
  2174. else
  2175. begin
  2176. AssertNotNull(Msg,El);
  2177. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  2178. end;
  2179. end;
  2180. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  2181. begin
  2182. if El=nil then
  2183. Result:=''
  2184. else if El is TJSPrimaryExpressionIdent then
  2185. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  2186. else if El is TJSDotMemberExpression then
  2187. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  2188. else
  2189. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  2190. end;
  2191. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  2192. InitStatements: string; ImplStatements: string);
  2193. var
  2194. ActualSrc, ExpectedSrc, InitName: String;
  2195. IsProg, IsLib: Boolean;
  2196. begin
  2197. ActualSrc:=JSToStr(JSModuleSrc);
  2198. if coUseStrict in Converter.Options then
  2199. ExpectedSrc:='"use strict";'+LineEnding
  2200. else
  2201. ExpectedSrc:='';
  2202. ExpectedSrc:=ExpectedSrc+'var $mod = this;'+LineEnding;
  2203. ExpectedSrc:=ExpectedSrc+Statements;
  2204. // unit implementation
  2205. if (Trim(ImplStatements)<>'') then
  2206. ExpectedSrc:=ExpectedSrc+LineEnding
  2207. +'$mod.$implcode = function () {'+LineEnding
  2208. +ImplStatements
  2209. +'};'+LineEnding;
  2210. // program main or unit initialization
  2211. IsProg:=false;
  2212. IsLib:=false;
  2213. if Module is TPasProgram then
  2214. IsProg:=true
  2215. else if Module is TPasLibrary then
  2216. IsLib:=true;
  2217. if IsProg or IsLib or (Trim(InitStatements)<>'') then
  2218. begin
  2219. if IsProg or IsLib then
  2220. InitName:='$main'
  2221. else
  2222. InitName:='$init';
  2223. ExpectedSrc:=ExpectedSrc+LineEnding
  2224. +'$mod.'+InitName+' = function () {'+LineEnding
  2225. +InitStatements
  2226. +'};'+LineEnding;
  2227. end;
  2228. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  2229. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  2230. //writeln('TCustomTestModule.CheckSource ',ActualSrc);
  2231. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2232. end;
  2233. procedure TCustomTestModule.CheckFullSource(Msg, ExpectedSrc: String);
  2234. var
  2235. ActualSrc: String;
  2236. begin
  2237. ActualSrc:=JSToStr(JSModule);
  2238. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2239. end;
  2240. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  2241. // search diff, ignore changes in spaces
  2242. var
  2243. s: string;
  2244. begin
  2245. if CheckSrcDiff(Expected,Actual,s) then exit;
  2246. Fail(Msg+': '+s);
  2247. end;
  2248. procedure TCustomTestModule.CheckUnit(aFilename, ExpectedSrc: string);
  2249. var
  2250. aResolver: TTestEnginePasResolver;
  2251. aConverter: TPasToJSConverter;
  2252. aJSModule: TJSSourceElements;
  2253. ActualSrc: String;
  2254. begin
  2255. aResolver:=GetResolver(aFilename);
  2256. AssertNotNull('missing resolver of unit '+aFilename,aResolver);
  2257. AssertNotNull('missing resolver.module of unit '+aFilename,aResolver.Module);
  2258. {$IFDEF VerbosePas2JS}
  2259. writeln('CheckUnit '+aFilename+' converting ...');
  2260. {$ENDIF}
  2261. aConverter:=CreateConverter;
  2262. aJSModule:=nil;
  2263. try
  2264. try
  2265. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  2266. except
  2267. on E: Exception do
  2268. HandleException(E);
  2269. end;
  2270. ActualSrc:=ConvertJSModuleToString(aJSModule);
  2271. {$IFDEF VerbosePas2JS}
  2272. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  2273. write(aResolver.Source);
  2274. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  2275. write(ActualSrc);
  2276. {$ENDIF}
  2277. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  2278. finally
  2279. aJSModule.Free;
  2280. aConverter.Free;
  2281. end;
  2282. end;
  2283. procedure TCustomTestModule.CheckReferenceDirectives;
  2284. var
  2285. CurFilename: string;
  2286. LineNumber: Integer;
  2287. SrcLine: String;
  2288. CommentStartP, CommentEndP: PChar;
  2289. procedure RaiseError(Msg: string; p: PChar);
  2290. begin
  2291. RaiseErrorAtSrc(Msg,CurFilename,LineNumber,p-PChar(SrcLine)+1);
  2292. end;
  2293. procedure AddMarker(Marker: PSrcMarker);
  2294. begin
  2295. if LastSrcMarker<>nil then
  2296. LastSrcMarker^.Next:=Marker
  2297. else
  2298. FirstSrcMarker:=Marker;
  2299. LastSrcMarker:=Marker;
  2300. end;
  2301. function AddMarker(Kind: TSrcMarkerKind; const aFilename: string;
  2302. aLine, aStartCol, aEndCol: integer; const Identifier: string): PSrcMarker;
  2303. begin
  2304. New(Result);
  2305. Result^.Kind:=Kind;
  2306. Result^.Filename:=aFilename;
  2307. Result^.Row:=aLine;
  2308. Result^.StartCol:=aStartCol;
  2309. Result^.EndCol:=aEndCol;
  2310. Result^.Identifier:=Identifier;
  2311. Result^.Next:=nil;
  2312. //writeln('AddMarker Line="',SrcLine,'" Identifier=',Identifier,' Col=',aStartCol,'-',aEndCol,' "',copy(SrcLine,aStartCol,aEndCol-aStartCol),'"');
  2313. AddMarker(Result);
  2314. end;
  2315. function AddMarkerForTokenBehindComment(Kind: TSrcMarkerKind;
  2316. const Identifier: string): PSrcMarker;
  2317. var
  2318. TokenStart, p: PChar;
  2319. begin
  2320. p:=CommentEndP;
  2321. ReadNextPascalToken(p,TokenStart,false,false);
  2322. Result:=AddMarker(Kind,CurFilename,LineNumber,
  2323. CommentEndP-PChar(SrcLine)+1,p-PChar(SrcLine)+1,Identifier);
  2324. end;
  2325. function ReadIdentifier(var p: PChar): string;
  2326. var
  2327. StartP: PChar;
  2328. begin
  2329. if not (p^ in ['a'..'z','A'..'Z','_']) then
  2330. RaiseError('identifier expected',p);
  2331. StartP:=p;
  2332. inc(p);
  2333. while p^ in ['a'..'z','A'..'Z','_','0'..'9'] do inc(p);
  2334. Result:='';
  2335. SetLength(Result,p-StartP);
  2336. Move(StartP^,Result[1],length(Result));
  2337. end;
  2338. procedure AddLabel;
  2339. var
  2340. Identifier: String;
  2341. p: PChar;
  2342. begin
  2343. p:=CommentStartP+2;
  2344. Identifier:=ReadIdentifier(p);
  2345. //writeln('TCustomTestModule.CheckReferenceDirectives.AddLabel ',Identifier);
  2346. if FindSrcLabel(Identifier)<>nil then
  2347. RaiseError('duplicate label "'+Identifier+'"',p);
  2348. AddMarkerForTokenBehindComment(mkLabel,Identifier);
  2349. end;
  2350. procedure AddResolverReference;
  2351. var
  2352. Identifier: String;
  2353. p: PChar;
  2354. begin
  2355. p:=CommentStartP+2;
  2356. Identifier:=ReadIdentifier(p);
  2357. //writeln('TCustomTestModule.CheckReferenceDirectives.AddReference ',Identifier);
  2358. AddMarkerForTokenBehindComment(mkResolverReference,Identifier);
  2359. end;
  2360. procedure AddDirectReference;
  2361. var
  2362. Identifier: String;
  2363. p: PChar;
  2364. begin
  2365. p:=CommentStartP+2;
  2366. Identifier:=ReadIdentifier(p);
  2367. //writeln('TCustomTestModule.CheckReferenceDirectives.AddDirectReference ',Identifier);
  2368. AddMarkerForTokenBehindComment(mkDirectReference,Identifier);
  2369. end;
  2370. procedure ParseCode(SrcLines: TStringList; aFilename: string);
  2371. var
  2372. p: PChar;
  2373. IsDirective: Boolean;
  2374. begin
  2375. //writeln('TCustomTestModule.CheckReferenceDirectives.ParseCode File=',aFilename);
  2376. CurFilename:=aFilename;
  2377. // parse code, find all labels
  2378. LineNumber:=0;
  2379. while LineNumber<SrcLines.Count do
  2380. begin
  2381. inc(LineNumber);
  2382. SrcLine:=SrcLines[LineNumber-1];
  2383. if SrcLine='' then continue;
  2384. //writeln('TCustomTestModule.CheckReferenceDirectives Line=',SrcLine);
  2385. p:=PChar(SrcLine);
  2386. repeat
  2387. case p^ of
  2388. #0: if (p-PChar(SrcLine)=length(SrcLine)) then break;
  2389. '{':
  2390. begin
  2391. CommentStartP:=p;
  2392. inc(p);
  2393. IsDirective:=p^ in ['#','@','='];
  2394. // skip to end of comment
  2395. repeat
  2396. case p^ of
  2397. #0:
  2398. if (p-PChar(SrcLine)=length(SrcLine)) then
  2399. begin
  2400. // multi line comment
  2401. if IsDirective then
  2402. RaiseError('directive missing closing bracket',CommentStartP);
  2403. repeat
  2404. inc(LineNumber);
  2405. if LineNumber>SrcLines.Count then exit;
  2406. SrcLine:=SrcLines[LineNumber-1];
  2407. //writeln('TCustomTestModule.CheckReferenceDirectives Comment Line=',SrcLine);
  2408. until SrcLine<>'';
  2409. p:=PChar(SrcLine);
  2410. continue;
  2411. end;
  2412. '}':
  2413. begin
  2414. inc(p);
  2415. break;
  2416. end;
  2417. end;
  2418. inc(p);
  2419. until false;
  2420. CommentEndP:=p;
  2421. case CommentStartP[1] of
  2422. '#': AddLabel;
  2423. '@': AddResolverReference;
  2424. '=': AddDirectReference;
  2425. end;
  2426. p:=CommentEndP;
  2427. continue;
  2428. end;
  2429. '/':
  2430. if p[1]='/' then
  2431. break; // rest of line is comment -> skip
  2432. end;
  2433. inc(p);
  2434. until false;
  2435. end;
  2436. end;
  2437. procedure CheckResolverReference(aMarker: PSrcMarker);
  2438. // check if one element at {@a} has a TResolvedReference to an element labeled {#a}
  2439. var
  2440. aLabel: PSrcMarker;
  2441. ReferenceElements, LabelElements: TFPList;
  2442. i, j, aLine, aCol: Integer;
  2443. El, Ref, LabelEl: TPasElement;
  2444. begin
  2445. //writeln('TCustomTestModule.CheckResolverReference searching reference: ',aMarker^.Filename,' Line=',aMarker^.Row,' Col=',aMarker^.StartCol,'-',aMarker^.EndCol,' Label="',aMarker^.Identifier,'"');
  2446. aLabel:=FindSrcLabel(aMarker^.Identifier);
  2447. if aLabel=nil then
  2448. RaiseErrorAtSrc('label "'+aMarker^.Identifier+'" not found',aMarker^.Filename,aMarker^.Row,aMarker^.StartCol);
  2449. LabelElements:=nil;
  2450. ReferenceElements:=nil;
  2451. try
  2452. LabelElements:=FindElementsAt(aLabel);
  2453. ReferenceElements:=FindElementsAt(aMarker);
  2454. for i:=0 to ReferenceElements.Count-1 do
  2455. begin
  2456. El:=TPasElement(ReferenceElements[i]);
  2457. Ref:=nil;
  2458. if El.CustomData is TResolvedReference then
  2459. Ref:=TResolvedReference(El.CustomData).Declaration
  2460. else if El.CustomData is TPasPropertyScope then
  2461. Ref:=TPasPropertyScope(El.CustomData).AncestorProp
  2462. else if El.CustomData is TPasSpecializeTypeData then
  2463. Ref:=TPasSpecializeTypeData(El.CustomData).SpecializedType;
  2464. if Ref<>nil then
  2465. for j:=0 to LabelElements.Count-1 do
  2466. begin
  2467. LabelEl:=TPasElement(LabelElements[j]);
  2468. if Ref=LabelEl then
  2469. exit; // success
  2470. end;
  2471. end;
  2472. // failure write candidates
  2473. for i:=0 to ReferenceElements.Count-1 do
  2474. begin
  2475. El:=TPasElement(ReferenceElements[i]);
  2476. write('Reference candidate for "',aMarker^.Identifier,'" at reference ',aMarker^.Filename,'(',aMarker^.Row,',',aMarker^.StartCol,'-',aMarker^.EndCol,')');
  2477. write(' El=',GetObjName(El));
  2478. if EL is TPrimitiveExpr then
  2479. begin
  2480. writeln('TCustomTestModule.CheckResolverReference ',TPrimitiveExpr(El).Value);
  2481. end;
  2482. Ref:=nil;
  2483. if El.CustomData is TResolvedReference then
  2484. Ref:=TResolvedReference(El.CustomData).Declaration
  2485. else if El.CustomData is TPasPropertyScope then
  2486. Ref:=TPasPropertyScope(El.CustomData).AncestorProp
  2487. else if El.CustomData is TPasSpecializeTypeData then
  2488. Ref:=TPasSpecializeTypeData(El.CustomData).SpecializedType;
  2489. if Ref<>nil then
  2490. begin
  2491. write(' Decl=',GetObjName(Ref));
  2492. ResolverEngine.UnmangleSourceLineNumber(Ref.SourceLinenumber,aLine,aCol);
  2493. write(',',Ref.SourceFilename,'(',aLine,',',aCol,')');
  2494. end
  2495. else
  2496. write(' has no TResolvedReference. El.CustomData=',GetObjName(El.CustomData));
  2497. writeln;
  2498. end;
  2499. for i:=0 to LabelElements.Count-1 do
  2500. begin
  2501. El:=TPasElement(LabelElements[i]);
  2502. write('Label candidate for "',aLabel^.Identifier,'" at reference ',aLabel^.Filename,'(',aLabel^.Row,',',aLabel^.StartCol,'-',aLabel^.EndCol,')');
  2503. write(' El=',GetObjName(El));
  2504. writeln;
  2505. end;
  2506. RaiseErrorAtSrcMarker('wrong resolved reference "'+aMarker^.Identifier+'"',aMarker);
  2507. finally
  2508. LabelElements.Free;
  2509. ReferenceElements.Free;
  2510. end;
  2511. end;
  2512. procedure CheckDirectReference(aMarker: PSrcMarker);
  2513. // check if one element at {=a} is a TPasAliasType pointing to an element labeled {#a}
  2514. var
  2515. aLabel: PSrcMarker;
  2516. ReferenceElements, LabelElements: TFPList;
  2517. i, LabelLine, LabelCol, j: Integer;
  2518. El, LabelEl: TPasElement;
  2519. DeclEl, TypeEl: TPasType;
  2520. begin
  2521. //writeln('CheckDirectReference searching pointer: ',aMarker^.Filename,' Line=',aMarker^.Row,' Col=',aMarker^.StartCol,'-',aMarker^.EndCol,' Label="',aMarker^.Identifier,'"');
  2522. aLabel:=FindSrcLabel(aMarker^.Identifier);
  2523. if aLabel=nil then
  2524. RaiseErrorAtSrcMarker('label "'+aMarker^.Identifier+'" not found',aMarker);
  2525. LabelElements:=nil;
  2526. ReferenceElements:=nil;
  2527. try
  2528. //writeln('CheckDirectReference finding elements at label ...');
  2529. LabelElements:=FindElementsAt(aLabel);
  2530. //writeln('CheckDirectReference finding elements at reference ...');
  2531. ReferenceElements:=FindElementsAt(aMarker);
  2532. for i:=0 to ReferenceElements.Count-1 do
  2533. begin
  2534. El:=TPasElement(ReferenceElements[i]);
  2535. //writeln('CheckDirectReference ',i,'/',ReferenceElements.Count,' ',GetTreeDbg(El,2));
  2536. if El.ClassType=TPasVariable then
  2537. begin
  2538. if TPasVariable(El).VarType=nil then
  2539. begin
  2540. //writeln('CheckDirectReference Var without Type: ',GetObjName(El),' El.Parent=',GetObjName(El.Parent));
  2541. AssertNotNull('TPasVariable(El='+El.Name+').VarType',TPasVariable(El).VarType);
  2542. end;
  2543. TypeEl:=TPasVariable(El).VarType;
  2544. for j:=0 to LabelElements.Count-1 do
  2545. begin
  2546. LabelEl:=TPasElement(LabelElements[j]);
  2547. if TypeEl=LabelEl then
  2548. exit; // success
  2549. end;
  2550. end
  2551. else if El is TPasAliasType then
  2552. begin
  2553. DeclEl:=TPasAliasType(El).DestType;
  2554. ResolverEngine.UnmangleSourceLineNumber(DeclEl.SourceLinenumber,LabelLine,LabelCol);
  2555. if (aLabel^.Filename=DeclEl.SourceFilename)
  2556. and (integer(aLabel^.Row)=LabelLine)
  2557. and (aLabel^.StartCol<=LabelCol)
  2558. and (aLabel^.EndCol>=LabelCol) then
  2559. exit; // success
  2560. end
  2561. else if El.ClassType=TPasArgument then
  2562. begin
  2563. TypeEl:=TPasArgument(El).ArgType;
  2564. for j:=0 to LabelElements.Count-1 do
  2565. begin
  2566. LabelEl:=TPasElement(LabelElements[j]);
  2567. if TypeEl=LabelEl then
  2568. exit; // success
  2569. end;
  2570. end;
  2571. end;
  2572. // failed -> show candidates
  2573. writeln('CheckDirectReference failed: Labels:');
  2574. for j:=0 to LabelElements.Count-1 do
  2575. begin
  2576. LabelEl:=TPasElement(LabelElements[j]);
  2577. writeln(' Label ',GetObjName(LabelEl),' at ',ResolverEngine.GetElementSourcePosStr(LabelEl));
  2578. end;
  2579. writeln('CheckDirectReference failed: References:');
  2580. for i:=0 to ReferenceElements.Count-1 do
  2581. begin
  2582. El:=TPasElement(ReferenceElements[i]);
  2583. writeln(' Reference ',GetObjName(El),' at ',ResolverEngine.GetElementSourcePosStr(El));
  2584. //if EL is TPasVariable then
  2585. // writeln('CheckDirectReference ',GetObjPath(TPasVariable(El).VarType),' ',ResolverEngine.GetElementSourcePosStr(TPasVariable(EL).VarType));
  2586. end;
  2587. RaiseErrorAtSrcMarker('wrong direct reference "'+aMarker^.Identifier+'"',aMarker);
  2588. finally
  2589. LabelElements.Free;
  2590. ReferenceElements.Free;
  2591. end;
  2592. end;
  2593. var
  2594. aMarker: PSrcMarker;
  2595. i: Integer;
  2596. SrcLines: TStringList;
  2597. begin
  2598. Module.ForEachCall(@OnCheckElementParent,nil);
  2599. //writeln('TCustomTestModule.CheckReferenceDirectives find all markers');
  2600. // find all markers
  2601. for i:=0 to FileResolver.Streams.Count-1 do
  2602. begin
  2603. GetSrc(i,SrcLines,CurFilename);
  2604. ParseCode(SrcLines,CurFilename);
  2605. SrcLines.Free;
  2606. end;
  2607. //writeln('TCustomTestModule.CheckReferenceDirectives check references');
  2608. // check references
  2609. aMarker:=FirstSrcMarker;
  2610. while aMarker<>nil do
  2611. begin
  2612. case aMarker^.Kind of
  2613. mkResolverReference: CheckResolverReference(aMarker);
  2614. mkDirectReference: CheckDirectReference(aMarker);
  2615. end;
  2616. aMarker:=aMarker^.Next;
  2617. end;
  2618. //writeln('TCustomTestModule.CheckReferenceDirectives COMPLETE');
  2619. end;
  2620. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  2621. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  2622. var
  2623. i: Integer;
  2624. Item: TTestHintMessage;
  2625. Expected,Actual: string;
  2626. begin
  2627. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  2628. for i:=0 to MsgCount-1 do
  2629. begin
  2630. Item:=Msgs[i];
  2631. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  2632. if (Marker<>nil) then
  2633. begin
  2634. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  2635. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  2636. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  2637. end;
  2638. // found
  2639. FHintMsgsGood.Add(Item);
  2640. str(Item.MsgType,Actual);
  2641. str(MsgType,Expected);
  2642. AssertEquals('MsgType',Expected,Actual);
  2643. exit;
  2644. end;
  2645. // needed message missing -> show emitted messages
  2646. WriteSources('',0,0);
  2647. for i:=0 to MsgCount-1 do
  2648. begin
  2649. Item:=Msgs[i];
  2650. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  2651. ' ('+IntToStr(Item.MsgNumber),')');
  2652. if Marker<>nil then
  2653. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  2654. writeln(' {',Item.Msg,'}');
  2655. end;
  2656. str(MsgType,Expected);
  2657. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  2658. if Marker<>nil then
  2659. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  2660. Actual:=Actual+' '+Msg;
  2661. Fail(Actual);
  2662. end;
  2663. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  2664. );
  2665. var
  2666. i: Integer;
  2667. s, Txt: String;
  2668. Msg: TTestHintMessage;
  2669. begin
  2670. for i:=0 to MsgCount-1 do
  2671. begin
  2672. Msg:=Msgs[i];
  2673. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  2674. s:='';
  2675. str(Msg.MsgType,s);
  2676. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  2677. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  2678. if WithSourcePos then
  2679. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  2680. Txt:=Txt+' {'+Msg.Msg+'}';
  2681. Fail(Txt);
  2682. end;
  2683. end;
  2684. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  2685. MsgNumber: integer);
  2686. begin
  2687. ExpectedErrorClass:=EScannerError;
  2688. ExpectedErrorMsg:=Msg;
  2689. ExpectedErrorNumber:=MsgNumber;
  2690. end;
  2691. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2692. MsgNumber: integer);
  2693. begin
  2694. ExpectedErrorClass:=EParserError;
  2695. ExpectedErrorMsg:=Msg;
  2696. ExpectedErrorNumber:=MsgNumber;
  2697. end;
  2698. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2699. MsgNumber: integer);
  2700. begin
  2701. ExpectedErrorClass:=EPasResolve;
  2702. ExpectedErrorMsg:=Msg;
  2703. ExpectedErrorNumber:=MsgNumber;
  2704. end;
  2705. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2706. MsgNumber: integer);
  2707. begin
  2708. ExpectedErrorClass:=EPas2JS;
  2709. ExpectedErrorMsg:=Msg;
  2710. ExpectedErrorNumber:=MsgNumber;
  2711. end;
  2712. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2713. var
  2714. MsgNumber: Integer;
  2715. Msg: String;
  2716. begin
  2717. Result:=false;
  2718. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2719. Msg:=E.Message;
  2720. if E is EPas2JS then
  2721. MsgNumber:=EPas2JS(E).MsgNumber
  2722. else if E is EPasResolve then
  2723. MsgNumber:=EPasResolve(E).MsgNumber
  2724. else if E is EParserError then
  2725. MsgNumber:=Parser.LastMsgNumber
  2726. else if E is EScannerError then
  2727. begin
  2728. MsgNumber:=Scanner.LastMsgNumber;
  2729. Msg:=Scanner.LastMsg;
  2730. end
  2731. else
  2732. MsgNumber:=0;
  2733. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2734. if Result then
  2735. SkipTests:=true;
  2736. end;
  2737. procedure TCustomTestModule.RaiseErrorAtSrc(Msg: string;
  2738. const aFilename: string; aRow, aCol: integer);
  2739. var
  2740. s: String;
  2741. begin
  2742. WriteSources(aFilename,aRow,aCol);
  2743. s:='[TCustomTestModule.RaiseErrorAtSrc] '+aFilename+'('+IntToStr(aRow)+','+IntToStr(aCol)+') Error: '+Msg;
  2744. writeln('ERROR: ',s);
  2745. Fail(s);
  2746. end;
  2747. procedure TCustomTestModule.RaiseErrorAtSrcMarker(Msg: string;
  2748. aMarker: PSrcMarker);
  2749. begin
  2750. RaiseErrorAtSrc(Msg,aMarker^.Filename,aMarker^.Row,aMarker^.StartCol);
  2751. end;
  2752. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2753. begin
  2754. if IsErrorExpected(E) then exit;
  2755. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2756. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2757. +' '+Scanner.CurFilename
  2758. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2759. FailException(E);
  2760. end;
  2761. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2762. begin
  2763. if IsErrorExpected(E) then exit;
  2764. WriteSources(E.Filename,E.Row,E.Column);
  2765. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2766. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2767. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2768. );
  2769. FailException(E);
  2770. end;
  2771. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2772. var
  2773. P: TPasSourcePos;
  2774. begin
  2775. if IsErrorExpected(E) then exit;
  2776. P:=E.SourcePos;
  2777. WriteSources(P.FileName,P.Row,P.Column);
  2778. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+'['+IntToStr(E.Id)+']:'
  2779. +E.Message
  2780. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2781. FailException(E);
  2782. end;
  2783. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2784. var
  2785. Row, Col: integer;
  2786. begin
  2787. if IsErrorExpected(E) then exit;
  2788. ResolverEngine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2789. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2790. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2791. +' '+E.PasElement.SourceFilename
  2792. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2793. FailException(E);
  2794. end;
  2795. procedure TCustomTestModule.HandleException(E: Exception);
  2796. begin
  2797. if E is EScannerError then
  2798. HandleScannerError(EScannerError(E))
  2799. else if E is EParserError then
  2800. HandleParserError(EParserError(E))
  2801. else if E is EPasResolve then
  2802. HandlePasResolveError(EPasResolve(E))
  2803. else if E is EPas2JS then
  2804. HandlePas2JSError(EPas2JS(E))
  2805. else
  2806. begin
  2807. if IsErrorExpected(E) then exit;
  2808. if not (E is EAssertionFailedError) then
  2809. begin
  2810. WriteSources('',0,0);
  2811. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2812. end;
  2813. FailException(E);
  2814. end;
  2815. end;
  2816. procedure TCustomTestModule.FailException(E: Exception);
  2817. var
  2818. MsgNumber: Integer;
  2819. begin
  2820. if ExpectedErrorClass<>nil then
  2821. begin
  2822. if FExpectedErrorClass=E.ClassType then
  2823. begin
  2824. if E is EPas2JS then
  2825. MsgNumber:=EPas2JS(E).MsgNumber
  2826. else if E is EPasResolve then
  2827. MsgNumber:=EPasResolve(E).MsgNumber
  2828. else if E is EParserError then
  2829. MsgNumber:=Parser.LastMsgNumber
  2830. else if E is EScannerError then
  2831. MsgNumber:=Scanner.LastMsgNumber
  2832. else
  2833. MsgNumber:=0;
  2834. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2835. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2836. ExpectedErrorNumber,MsgNumber);
  2837. end else begin
  2838. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2839. end;
  2840. end;
  2841. Fail(E.Message);
  2842. end;
  2843. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2844. aCol: integer);
  2845. var
  2846. IsSrc: Boolean;
  2847. i, j: Integer;
  2848. SrcLines: TStringList;
  2849. Line: string;
  2850. aModule: TTestEnginePasResolver;
  2851. begin
  2852. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2853. for i:=0 to ResolverCount-1 do
  2854. begin
  2855. aModule:=Resolvers[i];
  2856. SrcLines:=TStringList.Create;
  2857. try
  2858. SrcLines.Text:=aModule.Source;
  2859. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2860. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2861. for j:=1 to SrcLines.Count do
  2862. begin
  2863. Line:=SrcLines[j-1];
  2864. if IsSrc and (j=aRow) then
  2865. begin
  2866. write('*');
  2867. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2868. end;
  2869. writeln(Format('%:4d: ',[j]),Line);
  2870. end;
  2871. finally
  2872. SrcLines.Free;
  2873. end;
  2874. end;
  2875. end;
  2876. function TCustomTestModule.IndexOfResolver(const aFilename: string): integer;
  2877. var
  2878. i: Integer;
  2879. begin
  2880. for i:=0 to ResolverCount-1 do
  2881. if aFilename=Resolvers[i].Filename then exit(i);
  2882. Result:=-1;
  2883. end;
  2884. function TCustomTestModule.GetResolver(const aFilename: string
  2885. ): TTestEnginePasResolver;
  2886. var
  2887. i: Integer;
  2888. begin
  2889. i:=IndexOfResolver(aFilename);
  2890. if i<0 then exit(nil);
  2891. Result:=Resolvers[i];
  2892. end;
  2893. procedure TCustomTestModule.GetSrc(Index: integer; out SrcLines: TStringList;
  2894. out aFilename: string);
  2895. var
  2896. aStream: TStream;
  2897. begin
  2898. SrcLines:=TStringList.Create;
  2899. aStream:=FileResolver.Streams.Objects[Index] as TStream;
  2900. aStream.Position:=0;
  2901. SrcLines.LoadFromStream(aStream);
  2902. aFilename:=FileResolver.Streams[Index];
  2903. end;
  2904. function TCustomTestModule.FindElementsAt(aFilename: string; aLine, aStartCol,
  2905. aEndCol: integer): TFPList;
  2906. var
  2907. ok: Boolean;
  2908. FoundRefs: TTestResolverReferenceData;
  2909. i: Integer;
  2910. CurResolver: TTestEnginePasResolver;
  2911. begin
  2912. //writeln('TCustomTestModule.FindElementsAt START "',aFilename,'" Line=',aLine,' Col=',aStartCol,'-',aEndCol);
  2913. FoundRefs:=Default(TTestResolverReferenceData);
  2914. FoundRefs.Filename:=aFilename;
  2915. FoundRefs.Row:=aLine;
  2916. FoundRefs.StartCol:=aStartCol;
  2917. FoundRefs.EndCol:=aEndCol;
  2918. FoundRefs.Found:=TFPList.Create;
  2919. ok:=false;
  2920. try
  2921. // find all markers
  2922. Module.ForEachCall(@OnFindReference,@FoundRefs);
  2923. for i:=0 to ResolverCount-1 do
  2924. begin
  2925. CurResolver:=Resolvers[i];
  2926. if CurResolver.Module=Module then continue;
  2927. //writeln('TCustomTestResolver.FindElementsAt ',CurResolver.Filename);
  2928. CurResolver.Module.ForEachCall(@OnFindReference,@FoundRefs);
  2929. end;
  2930. ok:=true;
  2931. finally
  2932. if not ok then
  2933. FreeAndNil(FoundRefs.Found);
  2934. end;
  2935. Result:=FoundRefs.Found;
  2936. FoundRefs.Found:=nil;
  2937. end;
  2938. function TCustomTestModule.FindElementsAt(aMarker: PSrcMarker;
  2939. ErrorOnNoElements: boolean): TFPList;
  2940. begin
  2941. Result:=FindElementsAt(aMarker^.Filename,aMarker^.Row,aMarker^.StartCol,aMarker^.EndCol);
  2942. if ErrorOnNoElements and ((Result=nil) or (Result.Count=0)) then
  2943. RaiseErrorAtSrcMarker('marker '+SrcMarker[aMarker^.Kind]+aMarker^.Identifier+' has no elements',aMarker);
  2944. end;
  2945. function TCustomTestModule.FindSrcLabel(const Identifier: string): PSrcMarker;
  2946. begin
  2947. Result:=FirstSrcMarker;
  2948. while Result<>nil do
  2949. begin
  2950. if (Result^.Kind=mkLabel)
  2951. and (CompareText(Result^.Identifier,Identifier)=0) then
  2952. exit;
  2953. Result:=Result^.Next;
  2954. end;
  2955. end;
  2956. function TCustomTestModule.FindElementsAtSrcLabel(const Identifier: string;
  2957. ErrorOnNoElements: boolean): TFPList;
  2958. var
  2959. SrcLabel: PSrcMarker;
  2960. begin
  2961. SrcLabel:=FindSrcLabel(Identifier);
  2962. if SrcLabel=nil then
  2963. Fail('missing label "'+Identifier+'"');
  2964. Result:=FindElementsAt(SrcLabel,ErrorOnNoElements);
  2965. end;
  2966. function TCustomTestModule.GetDefaultNamespace: string;
  2967. var
  2968. C: TClass;
  2969. begin
  2970. Result:='';
  2971. if FModule=nil then exit;
  2972. C:=FModule.ClassType;
  2973. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2974. Result:=ResolverEngine.DefaultNameSpace;
  2975. end;
  2976. constructor TCustomTestModule.Create;
  2977. begin
  2978. inherited Create;
  2979. FHintMsgs:=TObjectList.Create(true);
  2980. FHintMsgsGood:=TFPList.Create;
  2981. end;
  2982. destructor TCustomTestModule.Destroy;
  2983. begin
  2984. FreeAndNil(FHintMsgs);
  2985. FreeAndNil(FHintMsgsGood);
  2986. inherited Destroy;
  2987. end;
  2988. { TTestModule }
  2989. procedure TTestModule.TestReservedWords;
  2990. var
  2991. i: integer;
  2992. begin
  2993. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2994. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2995. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2996. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2997. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2998. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2999. end;
  3000. procedure TTestModule.TestEmptyProgram;
  3001. begin
  3002. StartProgram(false);
  3003. Add('begin');
  3004. ConvertProgram;
  3005. CheckSource('TestEmptyProgram','','');
  3006. end;
  3007. procedure TTestModule.TestEmptyProgramUseStrict;
  3008. begin
  3009. Converter.Options:=Converter.Options+[coUseStrict];
  3010. StartProgram(false);
  3011. Add('begin');
  3012. ConvertProgram;
  3013. CheckSource('TestEmptyProgramUseStrict','','');
  3014. end;
  3015. procedure TTestModule.TestEmptyUnit;
  3016. begin
  3017. StartUnit(false);
  3018. Add('interface');
  3019. Add('implementation');
  3020. ConvertUnit;
  3021. CheckSource('TestEmptyUnit',
  3022. LinesToStr([
  3023. ]),
  3024. '');
  3025. end;
  3026. procedure TTestModule.TestEmptyUnitUseStrict;
  3027. begin
  3028. Converter.Options:=Converter.Options+[coUseStrict];
  3029. StartUnit(false);
  3030. Add('interface');
  3031. Add('implementation');
  3032. ConvertUnit;
  3033. CheckSource('TestEmptyUnitUseStrict',
  3034. LinesToStr([
  3035. ''
  3036. ]),
  3037. '');
  3038. end;
  3039. procedure TTestModule.TestDottedUnitNames;
  3040. begin
  3041. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  3042. LinesToStr([
  3043. 'var iV: longint;'
  3044. ]),
  3045. '');
  3046. FFilename:='ns1.test1.pp';
  3047. StartProgram(true);
  3048. Add('uses unIt2;');
  3049. Add('var');
  3050. Add(' i: longint;');
  3051. Add('begin');
  3052. Add(' i:=iv;');
  3053. Add(' i:=uNit2.iv;');
  3054. Add(' i:=Ns1.TEst1.i;');
  3055. ConvertProgram;
  3056. CheckSource('TestDottedUnitNames',
  3057. LinesToStr([
  3058. 'this.i = 0;',
  3059. '']),
  3060. LinesToStr([ // this.$init
  3061. '$mod.i = pas["NS1.Unit2"].iV;',
  3062. '$mod.i = pas["NS1.Unit2"].iV;',
  3063. '$mod.i = $mod.i;',
  3064. '']) );
  3065. end;
  3066. procedure TTestModule.TestDottedUnitNameImpl;
  3067. begin
  3068. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  3069. LinesToStr([
  3070. 'type',
  3071. ' TObject = class end;',
  3072. ' TTestA = class',
  3073. ' end;'
  3074. ]),
  3075. LinesToStr(['uses TEST.UnitB;'])
  3076. );
  3077. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  3078. LinesToStr([
  3079. 'uses TEST.UnitA;',
  3080. 'type TTestB = class(TTestA);'
  3081. ]),
  3082. ''
  3083. );
  3084. StartProgram(true);
  3085. Add('uses TEST.UnitA;');
  3086. Add('begin');
  3087. ConvertProgram;
  3088. CheckSource('TestDottedUnitNameImpl',
  3089. LinesToStr([
  3090. '']),
  3091. LinesToStr([ // this.$init
  3092. '']) );
  3093. CheckUnit('TEST.UnitA.pas',
  3094. LinesToStr([
  3095. 'rtl.module("TEST.UnitA", ["system"], function () {',
  3096. ' var $mod = this;',
  3097. ' rtl.createClass(this, "TObject", null, function () {',
  3098. ' this.$init = function () {',
  3099. ' };',
  3100. ' this.$final = function () {',
  3101. ' };',
  3102. ' });',
  3103. ' rtl.createClass(this, "TTestA", this.TObject, function () {',
  3104. ' });',
  3105. '}, ["TEST.UnitB"]);'
  3106. ]));
  3107. CheckUnit('TEST.UnitB.pas',
  3108. LinesToStr([
  3109. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  3110. ' var $mod = this;',
  3111. ' rtl.createClass(this, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  3112. ' });',
  3113. '});'
  3114. ]));
  3115. end;
  3116. procedure TTestModule.TestDottedUnitExpr;
  3117. begin
  3118. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  3119. LinesToStr([
  3120. 'procedure DoIt;'
  3121. ]),
  3122. 'procedure DoIt; begin end;');
  3123. FFilename:='Ns1.SubNs1.Test1.pp';
  3124. StartProgram(true);
  3125. Add('uses Ns2.sUbnS2.unIt2;');
  3126. Add('var');
  3127. Add(' i: longint;');
  3128. Add('begin');
  3129. Add(' ns2.subns2.unit2.doit;');
  3130. Add(' i:=Ns1.SubNS1.TEst1.i;');
  3131. ConvertProgram;
  3132. CheckSource('TestDottedUnitExpr',
  3133. LinesToStr([
  3134. 'this.i = 0;',
  3135. '']),
  3136. LinesToStr([ // this.$init
  3137. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  3138. '$mod.i = $mod.i;',
  3139. '']) );
  3140. end;
  3141. procedure TTestModule.Test_ModeFPCFail;
  3142. begin
  3143. StartProgram(false);
  3144. Add('{$mode FPC}');
  3145. Add('begin');
  3146. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  3147. ConvertProgram;
  3148. end;
  3149. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  3150. begin
  3151. StartProgram(false);
  3152. Add('{$modeswitch cblocks-}');
  3153. Add('begin');
  3154. ConvertProgram;
  3155. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  3156. CheckResolverUnexpectedHints();
  3157. end;
  3158. procedure TTestModule.TestUnit_UseSystem;
  3159. begin
  3160. StartUnit(true);
  3161. Add([
  3162. 'interface',
  3163. 'var i: integer;',
  3164. 'implementation']);
  3165. ConvertUnit;
  3166. CheckSource('TestUnit_UseSystem',
  3167. LinesToStr([
  3168. 'this.i = 0;',
  3169. '']),
  3170. LinesToStr([
  3171. '']) );
  3172. end;
  3173. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  3174. begin
  3175. AddModuleWithIntfImplSrc('unit1.pp',
  3176. LinesToStr([
  3177. 'type number = longint;']),
  3178. LinesToStr([
  3179. 'uses test1;',
  3180. 'procedure DoIt;',
  3181. 'begin',
  3182. ' i:=3;',
  3183. 'end;']));
  3184. StartUnit(true);
  3185. Add([
  3186. 'interface',
  3187. 'uses unit1;',
  3188. 'var i: number;',
  3189. 'implementation']);
  3190. ConvertUnit;
  3191. CheckSource('TestUnit_Intf1Impl2Intf1',
  3192. LinesToStr([
  3193. 'this.i = 0;',
  3194. '']),
  3195. LinesToStr([
  3196. '']) );
  3197. end;
  3198. procedure TTestModule.TestIncludeVersion;
  3199. begin
  3200. StartProgram(false);
  3201. Add([
  3202. 'var',
  3203. ' s: string;',
  3204. ' i: word;',
  3205. 'begin',
  3206. ' s:={$I %line%};',
  3207. ' i:={$I %linenum%};',
  3208. ' s:={$I %currentroutine%};',
  3209. ' s:={$I %pas2jsversion%};',
  3210. ' s:={$I %pas2jstarget%};',
  3211. ' s:={$I %pas2jstargetos%};',
  3212. ' s:={$I %pas2jstargetcpu%};',
  3213. ' s:={$I %file%};',
  3214. '']);
  3215. ConvertProgram;
  3216. CheckSource('TestIncludeVersion',
  3217. LinesToStr([
  3218. 'this.s="";',
  3219. 'this.i = 0;']),
  3220. LinesToStr([
  3221. '$mod.s = "7";',
  3222. '$mod.i = 8;',
  3223. '$mod.s = "<anonymous>";',
  3224. '$mod.s = "Comp.Ver.tcmodules";',
  3225. '$mod.s = "Browser";',
  3226. '$mod.s = "Browser";',
  3227. '$mod.s = "ECMAScript5";',
  3228. '$mod.s = "test1.pp";',
  3229. '']));
  3230. end;
  3231. procedure TTestModule.TestVarInt;
  3232. begin
  3233. StartProgram(false);
  3234. Add('var MyI: longint;');
  3235. Add('begin');
  3236. ConvertProgram;
  3237. CheckSource('TestVarInt','this.MyI=0;','');
  3238. end;
  3239. procedure TTestModule.TestVarBaseTypes;
  3240. begin
  3241. StartProgram(false);
  3242. Add('var');
  3243. Add(' i: longint;');
  3244. Add(' s: string;');
  3245. Add(' c: char;');
  3246. Add(' b: boolean;');
  3247. Add(' d: double;');
  3248. Add(' i2: longint = 3;');
  3249. Add(' s2: string = ''foo'';');
  3250. Add(' c2: char = ''4'';');
  3251. Add(' b2: boolean = true;');
  3252. Add(' d2: double = 5.6;');
  3253. Add(' i3: longint = $707;');
  3254. Add(' i4: nativeint = 9007199254740991;');
  3255. Add(' i5: nativeint = -9007199254740991-1;');
  3256. Add(' i6: nativeint = $fffffffffffff;');
  3257. Add(' i7: nativeint = -$fffffffffffff-1;');
  3258. Add(' i8: byte = 00;');
  3259. Add(' u8: nativeuint = $fffffffffffff;');
  3260. Add(' u9: nativeuint = $0000000000000;');
  3261. Add(' u10: nativeuint = $00ff00;');
  3262. Add('begin');
  3263. ConvertProgram;
  3264. CheckSource('TestVarBaseTypes',
  3265. LinesToStr([
  3266. 'this.i = 0;',
  3267. 'this.s = "";',
  3268. 'this.c = "\x00";',
  3269. 'this.b = false;',
  3270. 'this.d = 0.0;',
  3271. 'this.i2 = 3;',
  3272. 'this.s2 = "foo";',
  3273. 'this.c2 = "4";',
  3274. 'this.b2 = true;',
  3275. 'this.d2 = 5.6;',
  3276. 'this.i3 = 0x707;',
  3277. 'this.i4 = 9007199254740991;',
  3278. 'this.i5 = -9007199254740991-1;',
  3279. 'this.i6 = 0xfffffffffffff;',
  3280. 'this.i7 =-0xfffffffffffff-1;',
  3281. 'this.i8 = 0;',
  3282. 'this.u8 = 0xfffffffffffff;',
  3283. 'this.u9 = 0x0;',
  3284. 'this.u10 = 0xff00;'
  3285. ]),
  3286. '');
  3287. end;
  3288. procedure TTestModule.TestBaseTypeSingleFail;
  3289. begin
  3290. StartProgram(false);
  3291. Add('var s: single;');
  3292. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  3293. ConvertProgram;
  3294. end;
  3295. procedure TTestModule.TestBaseTypeExtendedFail;
  3296. begin
  3297. StartProgram(false);
  3298. Add('var e: extended;');
  3299. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  3300. ConvertProgram;
  3301. end;
  3302. procedure TTestModule.TestConstBaseTypes;
  3303. begin
  3304. StartProgram(false);
  3305. Add('const');
  3306. Add(' i: longint = 3;');
  3307. Add(' s: string = ''foo'';');
  3308. Add(' c: char = ''4'';');
  3309. Add(' b: boolean = true;');
  3310. Add(' d: double = 5.6;');
  3311. Add(' e = low(word);');
  3312. Add(' f = high(word);');
  3313. Add('begin');
  3314. ConvertProgram;
  3315. CheckSource('TestVarBaseTypes',
  3316. LinesToStr([
  3317. 'this.i=3;',
  3318. 'this.s="foo";',
  3319. 'this.c="4";',
  3320. 'this.b=true;',
  3321. 'this.d=5.6;',
  3322. 'this.e = 0;',
  3323. 'this.f = 65535;'
  3324. ]),
  3325. '');
  3326. end;
  3327. procedure TTestModule.TestAliasTypeRef;
  3328. begin
  3329. StartProgram(false);
  3330. Add('type');
  3331. Add(' a=longint;');
  3332. Add(' b=a;');
  3333. Add('var');
  3334. Add(' c: A;');
  3335. Add(' d: B;');
  3336. Add('begin');
  3337. ConvertProgram;
  3338. CheckSource('TestAliasTypeRef',
  3339. LinesToStr([ // statements
  3340. 'this.c = 0;',
  3341. 'this.d = 0;'
  3342. ]),
  3343. LinesToStr([ // this.$main
  3344. ''
  3345. ]));
  3346. end;
  3347. procedure TTestModule.TestTypeCast_BaseTypes;
  3348. begin
  3349. StartProgram(false);
  3350. Add([
  3351. 'var',
  3352. ' i: longint;',
  3353. ' b: boolean;',
  3354. ' d: double;',
  3355. ' s: string;',
  3356. ' c: char;',
  3357. 'begin',
  3358. ' i:=longint(i);',
  3359. ' i:=longint(b);',
  3360. ' b:=boolean(b);',
  3361. ' b:=boolean(i);',
  3362. ' d:=double(d);',
  3363. ' d:=double(i);',
  3364. ' s:=string(s);',
  3365. ' s:=string(c);',
  3366. ' c:=char(c);',
  3367. ' c:=char(i);',
  3368. ' c:=char(65);',
  3369. ' c:=char(#10);',
  3370. ' c:=char(#$E000);',
  3371. '']);
  3372. ConvertProgram;
  3373. CheckSource('TestAliasTypeRef',
  3374. LinesToStr([ // statements
  3375. 'this.i = 0;',
  3376. 'this.b = false;',
  3377. 'this.d = 0.0;',
  3378. 'this.s = "";',
  3379. 'this.c = "\x00";',
  3380. '']),
  3381. LinesToStr([ // this.$main
  3382. '$mod.i = $mod.i;',
  3383. '$mod.i = ($mod.b ? 1 : 0);',
  3384. '$mod.b = $mod.b;',
  3385. '$mod.b = $mod.i != 0;',
  3386. '$mod.d = $mod.d;',
  3387. '$mod.d = $mod.i;',
  3388. '$mod.s = $mod.s;',
  3389. '$mod.s = $mod.c;',
  3390. '$mod.c = $mod.c;',
  3391. '$mod.c = String.fromCharCode($mod.i);',
  3392. '$mod.c = "A";',
  3393. '$mod.c = "\n";',
  3394. '$mod.c = "";',
  3395. '']));
  3396. end;
  3397. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  3398. begin
  3399. StartProgram(false);
  3400. Add('type');
  3401. Add(' integer = longint;');
  3402. Add(' TYesNo = boolean;');
  3403. Add(' TFloat = double;');
  3404. Add(' TCaption = string;');
  3405. Add(' TChar = char;');
  3406. Add('var');
  3407. Add(' i: integer;');
  3408. Add(' b: TYesNo;');
  3409. Add(' d: TFloat;');
  3410. Add(' s: TCaption;');
  3411. Add(' c: TChar;');
  3412. Add('begin');
  3413. Add(' i:=integer(i);');
  3414. Add(' i:=integer(b);');
  3415. Add(' b:=TYesNo(b);');
  3416. Add(' b:=TYesNo(i);');
  3417. Add(' d:=TFloat(d);');
  3418. Add(' d:=TFloat(i);');
  3419. Add(' s:=TCaption(s);');
  3420. Add(' s:=TCaption(c);');
  3421. Add(' c:=TChar(c);');
  3422. ConvertProgram;
  3423. CheckSource('TestAliasTypeRef',
  3424. LinesToStr([ // statements
  3425. 'this.i = 0;',
  3426. 'this.b = false;',
  3427. 'this.d = 0.0;',
  3428. 'this.s = "";',
  3429. 'this.c = "\x00";',
  3430. '']),
  3431. LinesToStr([ // this.$main
  3432. '$mod.i = $mod.i;',
  3433. '$mod.i = ($mod.b ? 1 : 0);',
  3434. '$mod.b = $mod.b;',
  3435. '$mod.b = $mod.i != 0;',
  3436. '$mod.d = $mod.d;',
  3437. '$mod.d = $mod.i;',
  3438. '$mod.s = $mod.s;',
  3439. '$mod.s = $mod.c;',
  3440. '$mod.c = $mod.c;',
  3441. '']));
  3442. end;
  3443. procedure TTestModule.TestEmptyProc;
  3444. begin
  3445. StartProgram(false);
  3446. Add('procedure Test;');
  3447. Add('begin');
  3448. Add('end;');
  3449. Add('begin');
  3450. ConvertProgram;
  3451. CheckSource('TestEmptyProc',
  3452. LinesToStr([ // statements
  3453. 'this.Test = function () {',
  3454. '};'
  3455. ]),
  3456. LinesToStr([ // this.$main
  3457. ''
  3458. ]));
  3459. end;
  3460. procedure TTestModule.TestProcOneParam;
  3461. begin
  3462. StartProgram(false);
  3463. Add('procedure ProcA(i: longint);');
  3464. Add('begin');
  3465. Add('end;');
  3466. Add('begin');
  3467. Add(' PROCA(3);');
  3468. ConvertProgram;
  3469. CheckSource('TestProcOneParam',
  3470. LinesToStr([ // statements
  3471. 'this.ProcA = function (i) {',
  3472. '};'
  3473. ]),
  3474. LinesToStr([ // this.$main
  3475. '$mod.ProcA(3);'
  3476. ]));
  3477. end;
  3478. procedure TTestModule.TestFunctionWithoutParams;
  3479. begin
  3480. StartProgram(false);
  3481. Add('function FuncA: longint;');
  3482. Add('begin');
  3483. Add('end;');
  3484. Add('var i: longint;');
  3485. Add('begin');
  3486. Add(' I:=FUNCA();');
  3487. Add(' I:=FUNCA;');
  3488. Add(' FUNCA();');
  3489. Add(' FUNCA;');
  3490. ConvertProgram;
  3491. CheckSource('TestProcWithoutParams',
  3492. LinesToStr([ // statements
  3493. 'this.FuncA = function () {',
  3494. ' var Result = 0;',
  3495. ' return Result;',
  3496. '};',
  3497. 'this.i=0;'
  3498. ]),
  3499. LinesToStr([ // this.$main
  3500. '$mod.i=$mod.FuncA();',
  3501. '$mod.i=$mod.FuncA();',
  3502. '$mod.FuncA();',
  3503. '$mod.FuncA();'
  3504. ]));
  3505. end;
  3506. procedure TTestModule.TestProcedureWithoutParams;
  3507. begin
  3508. StartProgram(false);
  3509. Add('procedure ProcA;');
  3510. Add('begin');
  3511. Add('end;');
  3512. Add('begin');
  3513. Add(' PROCA();');
  3514. Add(' PROCA;');
  3515. ConvertProgram;
  3516. CheckSource('TestProcWithoutParams',
  3517. LinesToStr([ // statements
  3518. 'this.ProcA = function () {',
  3519. '};'
  3520. ]),
  3521. LinesToStr([ // this.$main
  3522. '$mod.ProcA();',
  3523. '$mod.ProcA();'
  3524. ]));
  3525. end;
  3526. procedure TTestModule.TestIncDec;
  3527. begin
  3528. StartProgram(false);
  3529. Add([
  3530. 'procedure DoIt(var i: longint);',
  3531. 'begin',
  3532. ' inc(i);',
  3533. ' inc(i,2);',
  3534. 'end;',
  3535. 'var',
  3536. ' Bar: longint;',
  3537. 'begin',
  3538. ' inc(bar);',
  3539. ' inc(bar,2);',
  3540. ' dec(bar);',
  3541. ' dec(bar,3);',
  3542. '']);
  3543. ConvertProgram;
  3544. CheckSource('TestIncDec',
  3545. LinesToStr([ // statements
  3546. 'this.DoIt = function (i) {',
  3547. ' i.set(i.get()+1);',
  3548. ' i.set(i.get()+2);',
  3549. '};',
  3550. 'this.Bar = 0;'
  3551. ]),
  3552. LinesToStr([ // this.$main
  3553. '$mod.Bar+=1;',
  3554. '$mod.Bar+=2;',
  3555. '$mod.Bar-=1;',
  3556. '$mod.Bar-=3;'
  3557. ]));
  3558. end;
  3559. procedure TTestModule.TestLoHiFpcMode;
  3560. begin
  3561. StartProgram(false);
  3562. Add([
  3563. '{$mode objfpc}',
  3564. 'const',
  3565. ' LoByte1 = Lo(Word($1234));',
  3566. ' HiByte1 = Hi(Word($1234));',
  3567. ' LoByte2 = Lo(SmallInt($1234));',
  3568. ' HiByte2 = Hi(SmallInt($1234));',
  3569. ' LoWord1 = Lo($1234CDEF);',
  3570. ' HiWord1 = Hi($1234CDEF);',
  3571. ' LoWord2 = Lo(-$1234CDEF);',
  3572. ' HiWord2 = Hi(-$1234CDEF);',
  3573. ' lo4:byte=lo(byte($34));',
  3574. ' hi4:byte=hi(byte($34));',
  3575. ' lo5:byte=lo(shortint(-$34));',
  3576. ' hi5:byte=hi(shortint(-$34));',
  3577. ' lo6:longword=lo($123456789ABCD);',
  3578. ' hi6:longword=hi($123456789ABCD);',
  3579. ' lo7:longword=lo(-$123456789ABCD);',
  3580. ' hi7:longword=hi(-$123456789ABCD);',
  3581. 'var',
  3582. ' b: Byte;',
  3583. ' ss: shortint;',
  3584. ' w: Word;',
  3585. ' si: SmallInt;',
  3586. ' lw: LongWord;',
  3587. ' li: LongInt;',
  3588. ' b2: Byte;',
  3589. ' ni: nativeint;',
  3590. 'begin',
  3591. ' w := $1234;',
  3592. ' ss := -$12;',
  3593. ' b := lo(ss);',
  3594. ' b := HI(ss);',
  3595. ' b := lo(w);',
  3596. ' b := HI(w);',
  3597. ' b2 := lo(b);',
  3598. ' b2 := hi(b);',
  3599. ' lw := $1234CDEF;',
  3600. ' w := lo(lw);',
  3601. ' w := hi(lw);',
  3602. ' ni := $123456789ABCD;',
  3603. ' lw := lo(ni);',
  3604. ' lw := hi(ni);',
  3605. '']);
  3606. ConvertProgram;
  3607. CheckSource('TestLoHiFpcMode',
  3608. LinesToStr([ // statements
  3609. 'this.LoByte1 = 0x1234 & 0xFF;',
  3610. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3611. 'this.LoByte2 = 0x1234 & 0xFF;',
  3612. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3613. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  3614. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  3615. 'this.LoWord2 = -0x1234CDEF >>> 0;',
  3616. 'this.HiWord2 = Math.floor(-0x1234CDEF / 4294967296) >>> 0;',
  3617. 'this.lo4 = 0x34 & 0xF;',
  3618. 'this.hi4 = (0x34 >> 4) & 0xF;',
  3619. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  3620. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  3621. 'this.lo6 = 0x123456789ABCD >>> 0;',
  3622. 'this.hi6 = 74565 >>> 0;',
  3623. 'this.lo7 = -0x123456789ABCD >>> 0;',
  3624. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  3625. 'this.b = 0;',
  3626. 'this.ss = 0;',
  3627. 'this.w = 0;',
  3628. 'this.si = 0;',
  3629. 'this.lw = 0;',
  3630. 'this.li = 0;',
  3631. 'this.b2 = 0;',
  3632. 'this.ni = 0;',
  3633. '']),
  3634. LinesToStr([ // this.$main
  3635. '$mod.w = 0x1234;',
  3636. '$mod.ss = -0x12;',
  3637. '$mod.b = $mod.ss & 0xFF;',
  3638. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  3639. '$mod.b = $mod.w & 0xFF;',
  3640. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3641. '$mod.b2 = $mod.b & 0xF;',
  3642. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  3643. '$mod.lw = 0x1234CDEF;',
  3644. '$mod.w = $mod.lw & 0xFFFF;',
  3645. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  3646. '$mod.ni = 0x123456789ABCD;',
  3647. '$mod.lw = $mod.ni >>> 0;',
  3648. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  3649. '']));
  3650. end;
  3651. procedure TTestModule.TestLoHiDelphiMode;
  3652. begin
  3653. StartProgram(false);
  3654. Add([
  3655. '{$mode delphi}',
  3656. 'const',
  3657. ' LoByte1 = Lo(Word($1234));',
  3658. ' HiByte1 = Hi(Word($1234));',
  3659. ' LoByte2 = Lo(SmallInt($1234));',
  3660. ' HiByte2 = Hi(SmallInt($1234));',
  3661. ' LoByte3 = Lo($1234CDEF);',
  3662. ' HiByte3 = Hi($1234CDEF);',
  3663. ' LoByte4 = Lo(-$1234CDEF);',
  3664. ' HiByte4 = Hi(-$1234CDEF);',
  3665. 'var',
  3666. ' b: Byte;',
  3667. ' w: Word;',
  3668. ' si: SmallInt;',
  3669. ' lw: LongWord;',
  3670. ' li: LongInt;',
  3671. 'begin',
  3672. ' w := $1234;',
  3673. ' b := lo(w);',
  3674. ' b := HI(w);',
  3675. ' lw := $1234CDEF;',
  3676. ' b := lo(lw);',
  3677. ' b := hi(lw);',
  3678. '']);
  3679. ConvertProgram;
  3680. CheckSource('TestLoHiDelphiMode',
  3681. LinesToStr([ // statements
  3682. 'this.LoByte1 = 0x1234 & 0xFF;',
  3683. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3684. 'this.LoByte2 = 0x1234 & 0xFF;',
  3685. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3686. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  3687. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  3688. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  3689. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  3690. 'this.b = 0;',
  3691. 'this.w = 0;',
  3692. 'this.si = 0;',
  3693. 'this.lw = 0;',
  3694. 'this.li = 0;'
  3695. ]),
  3696. LinesToStr([ // this.$main
  3697. '$mod.w = 0x1234;',
  3698. '$mod.b = $mod.w & 0xFF;',
  3699. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3700. '$mod.lw = 0x1234CDEF;',
  3701. '$mod.b = $mod.lw & 0xFF;',
  3702. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  3703. ]));
  3704. end;
  3705. procedure TTestModule.TestAssignments;
  3706. begin
  3707. StartProgram(false);
  3708. Parser.Options:=Parser.Options+[po_cassignments];
  3709. Add('var');
  3710. Add(' Bar:longint;');
  3711. Add('begin');
  3712. Add(' bar:=3;');
  3713. Add(' bar+=4;');
  3714. Add(' bar-=5;');
  3715. Add(' bar*=6;');
  3716. ConvertProgram;
  3717. CheckSource('TestAssignments',
  3718. LinesToStr([ // statements
  3719. 'this.Bar = 0;'
  3720. ]),
  3721. LinesToStr([ // this.$main
  3722. '$mod.Bar=3;',
  3723. '$mod.Bar+=4;',
  3724. '$mod.Bar-=5;',
  3725. '$mod.Bar*=6;'
  3726. ]));
  3727. end;
  3728. procedure TTestModule.TestArithmeticOperators1;
  3729. begin
  3730. StartProgram(false);
  3731. Add('var');
  3732. Add(' vA,vB,vC:longint;');
  3733. Add('begin');
  3734. Add(' va:=1;');
  3735. Add(' vb:=va+va;');
  3736. Add(' vb:=va div vb;');
  3737. Add(' vb:=va mod vb;');
  3738. Add(' vb:=va+va*vb+va div vb;');
  3739. Add(' vc:=-va;');
  3740. Add(' va:=va-vb;');
  3741. Add(' vb:=va;');
  3742. Add(' if va<vb then vc:=va else vc:=vb;');
  3743. ConvertProgram;
  3744. CheckSource('TestArithmeticOperators1',
  3745. LinesToStr([ // statements
  3746. 'this.vA = 0;',
  3747. 'this.vB = 0;',
  3748. 'this.vC = 0;'
  3749. ]),
  3750. LinesToStr([ // this.$main
  3751. '$mod.vA = 1;',
  3752. '$mod.vB = $mod.vA + $mod.vA;',
  3753. '$mod.vB = rtl.trunc($mod.vA / $mod.vB);',
  3754. '$mod.vB = $mod.vA % $mod.vB;',
  3755. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + rtl.trunc($mod.vA / $mod.vB);',
  3756. '$mod.vC = -$mod.vA;',
  3757. '$mod.vA = $mod.vA - $mod.vB;',
  3758. '$mod.vB = $mod.vA;',
  3759. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  3760. ]));
  3761. end;
  3762. procedure TTestModule.TestMultiAdd;
  3763. begin
  3764. StartProgram(false);
  3765. Add([
  3766. 'function Fly: string; external name ''fly'';',
  3767. 'function TryEncodeDate(Year, Month, Day: Word): Boolean;',
  3768. 'var',
  3769. ' Date: double;',
  3770. 'begin',
  3771. ' Result:=(Year>0) and (Year<10000) and',
  3772. ' (Month >= 1) and (Month<=12) and',
  3773. ' (Day>0) and (Day<=31);',
  3774. ' Date := (146097*Year) SHR 2 + (1461*Year) SHR 2 + (153*LongWord(Month)+2) DIV 5 + LongWord(Day);',
  3775. 'end;',
  3776. 'var s: string;',
  3777. 'begin',
  3778. ' s:=''a''+''b''+''c''+''d'';',
  3779. ' s:=s+Fly+''e'';',
  3780. ' s:=Fly+Fly+Fly;',
  3781. '']);
  3782. ConvertProgram;
  3783. CheckSource('TestMultiAdd',
  3784. LinesToStr([ // statements
  3785. 'this.TryEncodeDate = function (Year, Month, Day) {',
  3786. ' var Result = false;',
  3787. ' var date = 0.0;',
  3788. ' Result = (Year > 0) && (Year < 10000) && (Month >= 1) && (Month <= 12) && (Day > 0) && (Day <= 31);',
  3789. ' date = ((146097 * Year) >>> 2) + ((1461 * Year) >>> 2) + rtl.trunc(((153 * Month) + 2) / 5) + Day;',
  3790. ' return Result;',
  3791. '};',
  3792. 'this.s = "";',
  3793. '']),
  3794. LinesToStr([ // this.$main
  3795. '$mod.s = "a" + "b" + "c" + "d";',
  3796. '$mod.s = $mod.s + fly() + "e";',
  3797. '$mod.s = fly() + fly() + fly();',
  3798. '']));
  3799. end;
  3800. procedure TTestModule.TestLogicalOperators;
  3801. begin
  3802. StartProgram(false);
  3803. Add('var');
  3804. Add(' vA,vB,vC:boolean;');
  3805. Add('begin');
  3806. Add(' va:=vb and vc;');
  3807. Add(' va:=vb or vc;');
  3808. Add(' va:=vb xor vc;');
  3809. Add(' va:=true and vc;');
  3810. Add(' va:=(vb and vc) or (va and vb);');
  3811. Add(' va:=not vb;');
  3812. ConvertProgram;
  3813. CheckSource('TestLogicalOperators',
  3814. LinesToStr([ // statements
  3815. 'this.vA = false;',
  3816. 'this.vB = false;',
  3817. 'this.vC = false;'
  3818. ]),
  3819. LinesToStr([ // this.$main
  3820. '$mod.vA = $mod.vB && $mod.vC;',
  3821. '$mod.vA = $mod.vB || $mod.vC;',
  3822. '$mod.vA = $mod.vB ^ $mod.vC;',
  3823. '$mod.vA = true && $mod.vC;',
  3824. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3825. '$mod.vA = !$mod.vB;'
  3826. ]));
  3827. end;
  3828. procedure TTestModule.TestBitwiseOperators;
  3829. begin
  3830. StartProgram(false);
  3831. Add([
  3832. 'var',
  3833. ' vA,vB,vC:longint;',
  3834. ' X,Y,Z: nativeint;',
  3835. 'begin',
  3836. ' va:=vb and vc;',
  3837. ' va:=vb or vc;',
  3838. ' va:=vb xor vc;',
  3839. ' va:=vb shl vc;',
  3840. ' va:=vb shr vc;',
  3841. ' va:=3 and vc;',
  3842. ' va:=(vb and vc) or (va and vb);',
  3843. ' va:=not vb;',
  3844. ' X:=Y and Z;',
  3845. ' X:=Y and va;',
  3846. ' X:=Y or Z;',
  3847. ' X:=Y or va;',
  3848. ' X:=Y xor Z;',
  3849. ' X:=Y xor va;',
  3850. '']);
  3851. ConvertProgram;
  3852. CheckSource('TestBitwiseOperators',
  3853. LinesToStr([ // statements
  3854. 'this.vA = 0;',
  3855. 'this.vB = 0;',
  3856. 'this.vC = 0;',
  3857. 'this.X = 0;',
  3858. 'this.Y = 0;',
  3859. 'this.Z = 0;',
  3860. '']),
  3861. LinesToStr([ // this.$main
  3862. '$mod.vA = $mod.vB & $mod.vC;',
  3863. '$mod.vA = $mod.vB | $mod.vC;',
  3864. '$mod.vA = $mod.vB ^ $mod.vC;',
  3865. '$mod.vA = $mod.vB << $mod.vC;',
  3866. '$mod.vA = $mod.vB >>> $mod.vC;',
  3867. '$mod.vA = 3 & $mod.vC;',
  3868. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3869. '$mod.vA = ~$mod.vB;',
  3870. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3871. '$mod.X = $mod.Y & $mod.vA;',
  3872. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3873. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3874. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3875. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3876. '']));
  3877. end;
  3878. procedure TTestModule.TestBitwiseOperatorsLongword;
  3879. begin
  3880. StartProgram(false);
  3881. Add([
  3882. 'var',
  3883. ' a,b,c:longword;',
  3884. ' i: longint;',
  3885. 'begin',
  3886. ' a:=$12345678;',
  3887. ' b:=$EDCBA987;',
  3888. ' c:=not a;',
  3889. ' c:=a and b;',
  3890. ' c:=a and $ffff0000;',
  3891. ' c:=a or b;',
  3892. ' c:=a or $ff00ff00;',
  3893. ' c:=a xor b;',
  3894. ' c:=a xor $f0f0f0f0;',
  3895. ' c:=a shl 1;',
  3896. ' c:=a shl 16;',
  3897. ' c:=a shl 24;',
  3898. ' c:=a shl b;',
  3899. ' c:=a shr 1;',
  3900. ' c:=a shr 16;',
  3901. ' c:=a shr 24;',
  3902. ' c:=a shr b;',
  3903. ' c:=(b and c) or (a and b);',
  3904. ' c:=i and a;',
  3905. ' c:=i or a;',
  3906. ' c:=i xor a;',
  3907. '']);
  3908. ConvertProgram;
  3909. CheckSource('TestBitwiseOperatorsLongword',
  3910. LinesToStr([ // statements
  3911. 'this.a = 0;',
  3912. 'this.b = 0;',
  3913. 'this.c = 0;',
  3914. 'this.i = 0;',
  3915. '']),
  3916. LinesToStr([ // this.$main
  3917. '$mod.a = 0x12345678;',
  3918. '$mod.b = 0xEDCBA987;',
  3919. '$mod.c = rtl.lw(~$mod.a);',
  3920. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3921. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3922. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3923. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3924. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3925. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3926. '$mod.c = rtl.lw($mod.a << 1);',
  3927. '$mod.c = rtl.lw($mod.a << 16);',
  3928. '$mod.c = rtl.lw($mod.a << 24);',
  3929. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3930. '$mod.c = rtl.lw($mod.a >>> 1);',
  3931. '$mod.c = rtl.lw($mod.a >>> 16);',
  3932. '$mod.c = rtl.lw($mod.a >>> 24);',
  3933. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3934. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3935. '$mod.c = $mod.i & $mod.a;',
  3936. '$mod.c = $mod.i | $mod.a;',
  3937. '$mod.c = $mod.i ^ $mod.a;',
  3938. '']));
  3939. end;
  3940. procedure TTestModule.TestPrgProcVar;
  3941. begin
  3942. StartProgram(false);
  3943. Add('procedure Proc1;');
  3944. Add('type');
  3945. Add(' t1=longint;');
  3946. Add('var');
  3947. Add(' vA:t1;');
  3948. Add('begin');
  3949. Add('end;');
  3950. Add('begin');
  3951. ConvertProgram;
  3952. CheckSource('TestPrgProcVar',
  3953. LinesToStr([ // statements
  3954. 'this.Proc1 = function () {',
  3955. ' var vA=0;',
  3956. '};'
  3957. ]),
  3958. LinesToStr([ // this.$main
  3959. ''
  3960. ]));
  3961. end;
  3962. procedure TTestModule.TestUnitProcVar;
  3963. begin
  3964. StartUnit(false);
  3965. Add('interface');
  3966. Add('');
  3967. Add('type tA=string; // unit scope');
  3968. Add('procedure Proc1;');
  3969. Add('');
  3970. Add('implementation');
  3971. Add('');
  3972. Add('procedure Proc1;');
  3973. Add('type tA=longint; // local proc scope');
  3974. Add('var v1:tA; // using local tA');
  3975. Add('begin');
  3976. Add('end;');
  3977. Add('var v2:tA; // using interface tA');
  3978. ConvertUnit;
  3979. CheckSource('TestUnitProcVar',
  3980. LinesToStr([ // statements
  3981. 'var $impl = $mod.$impl;',
  3982. 'this.Proc1 = function () {',
  3983. ' var v1 = 0;',
  3984. '};',
  3985. '']),
  3986. // this.$init
  3987. '',
  3988. // implementation
  3989. LinesToStr([
  3990. '$impl.v2 = "";',
  3991. '']));
  3992. end;
  3993. procedure TTestModule.TestImplProc;
  3994. begin
  3995. StartUnit(false);
  3996. Add('interface');
  3997. Add('');
  3998. Add('procedure Proc1;');
  3999. Add('');
  4000. Add('implementation');
  4001. Add('');
  4002. Add('procedure Proc1; begin end;');
  4003. Add('procedure Proc2; begin end;');
  4004. Add('initialization');
  4005. Add(' Proc1;');
  4006. Add(' Proc2;');
  4007. ConvertUnit;
  4008. CheckSource('TestImplProc',
  4009. LinesToStr([ // statements
  4010. 'var $impl = $mod.$impl;',
  4011. 'this.Proc1 = function () {',
  4012. '};',
  4013. '']),
  4014. LinesToStr([ // this.$init
  4015. '$mod.Proc1();',
  4016. '$impl.Proc2();',
  4017. '']),
  4018. LinesToStr([ // implementation
  4019. '$impl.Proc2 = function () {',
  4020. '};',
  4021. ''])
  4022. );
  4023. end;
  4024. procedure TTestModule.TestFunctionResult;
  4025. begin
  4026. StartProgram(false);
  4027. Add('function Func1: longint;');
  4028. Add('begin');
  4029. Add(' Result:=3;');
  4030. Add(' Func1:=4;');
  4031. Add('end;');
  4032. Add('begin');
  4033. ConvertProgram;
  4034. CheckSource('TestFunctionResult',
  4035. LinesToStr([ // statements
  4036. 'this.Func1 = function () {',
  4037. ' var Result = 0;',
  4038. ' Result = 3;',
  4039. ' Result = 4;',
  4040. ' return Result;',
  4041. '};'
  4042. ]),
  4043. '');
  4044. end;
  4045. procedure TTestModule.TestNestedProc;
  4046. begin
  4047. StartProgram(false);
  4048. Add([
  4049. 'var vInUnit: longint;',
  4050. 'function DoIt(pA,pD: longint): longint;',
  4051. 'var',
  4052. ' vB: longint;',
  4053. ' vC: longint;',
  4054. ' function Nesty(pA: longint): longint; ',
  4055. ' var vB: longint;',
  4056. ' begin',
  4057. ' Result:=pa+vb+vc+pd+vInUnit;',
  4058. ' nesty:=3;',
  4059. ' doit:=4;',
  4060. ' exit;',
  4061. ' end;',
  4062. 'begin',
  4063. ' Result:=pa+vb+vc;',
  4064. ' doit:=6;',
  4065. ' exit;',
  4066. 'end;',
  4067. 'begin']);
  4068. ConvertProgram;
  4069. CheckSource('TestNestedProc',
  4070. LinesToStr([ // statements
  4071. 'this.vInUnit = 0;',
  4072. 'this.DoIt = function (pA, pD) {',
  4073. ' var Result = 0;',
  4074. ' var vB = 0;',
  4075. ' var vC = 0;',
  4076. ' function Nesty(pA) {',
  4077. ' var Result$1 = 0;',
  4078. ' var vB = 0;',
  4079. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  4080. ' Result$1 = 3;',
  4081. ' Result = 4;',
  4082. ' return Result$1;',
  4083. ' return Result$1;',
  4084. ' };',
  4085. ' Result = pA + vB + vC;',
  4086. ' Result = 6;',
  4087. ' return Result;',
  4088. ' return Result;',
  4089. '};'
  4090. ]),
  4091. '');
  4092. end;
  4093. procedure TTestModule.TestNestedProc_ResultString;
  4094. begin
  4095. StartProgram(false);
  4096. Add([
  4097. 'function DoIt: string;',
  4098. ' function Nesty: string; ',
  4099. ' begin',
  4100. ' nesty:=#65#66;',
  4101. ' nesty[1]:=#67;',
  4102. ' doit:=#68;',
  4103. ' doit[2]:=#69;',
  4104. ' end;',
  4105. 'begin',
  4106. ' doit:=#70;',
  4107. ' doit[3]:=#71;',
  4108. 'end;',
  4109. 'begin']);
  4110. ConvertProgram;
  4111. CheckSource('TestNestedProc_ResultString',
  4112. LinesToStr([ // statements
  4113. 'this.DoIt = function () {',
  4114. ' var Result = "";',
  4115. ' function Nesty() {',
  4116. ' var Result$1 = "";',
  4117. ' Result$1 = "AB";',
  4118. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  4119. ' Result = "D";',
  4120. ' Result = rtl.setCharAt(Result, 1, "E");',
  4121. ' return Result$1;',
  4122. ' };',
  4123. ' Result = "F";',
  4124. ' Result = rtl.setCharAt(Result, 2, "G");',
  4125. ' return Result;',
  4126. '};'
  4127. ]),
  4128. '');
  4129. end;
  4130. procedure TTestModule.TestForwardProc;
  4131. begin
  4132. StartProgram(false);
  4133. Add('procedure FuncA(Bar: longint); forward;');
  4134. Add('procedure FuncB(Bar: longint);');
  4135. Add('begin');
  4136. Add(' funca(bar);');
  4137. Add('end;');
  4138. Add('procedure funca(bar: longint);');
  4139. Add('begin');
  4140. Add(' if bar=3 then ;');
  4141. Add('end;');
  4142. Add('begin');
  4143. Add(' funca(4);');
  4144. Add(' funcb(5);');
  4145. ConvertProgram;
  4146. CheckSource('TestForwardProc',
  4147. LinesToStr([ // statements'
  4148. 'this.FuncB = function (Bar) {',
  4149. ' $mod.FuncA(Bar);',
  4150. '};',
  4151. 'this.FuncA = function (Bar) {',
  4152. ' if (Bar === 3);',
  4153. '};'
  4154. ]),
  4155. LinesToStr([
  4156. '$mod.FuncA(4);',
  4157. '$mod.FuncB(5);'
  4158. ])
  4159. );
  4160. end;
  4161. procedure TTestModule.TestNestedForwardProc;
  4162. begin
  4163. StartProgram(false);
  4164. Add('procedure FuncA;');
  4165. Add(' procedure FuncB(i: longint); forward;');
  4166. Add(' procedure FuncC(i: longint);');
  4167. Add(' begin');
  4168. Add(' funcb(i);');
  4169. Add(' end;');
  4170. Add(' procedure FuncB(i: longint);');
  4171. Add(' begin');
  4172. Add(' if i=3 then ;');
  4173. Add(' end;');
  4174. Add('begin');
  4175. Add(' funcc(4)');
  4176. Add('end;');
  4177. Add('begin');
  4178. Add(' funca;');
  4179. ConvertProgram;
  4180. CheckSource('TestNestedForwardProc',
  4181. LinesToStr([ // statements'
  4182. 'this.FuncA = function () {',
  4183. ' function FuncC(i) {',
  4184. ' FuncB(i);',
  4185. ' };',
  4186. ' function FuncB(i) {',
  4187. ' if (i === 3);',
  4188. ' };',
  4189. ' FuncC(4);',
  4190. '};'
  4191. ]),
  4192. LinesToStr([
  4193. '$mod.FuncA();'
  4194. ])
  4195. );
  4196. end;
  4197. procedure TTestModule.TestAssignFunctionResult;
  4198. begin
  4199. StartProgram(false);
  4200. Add('function Func1: longint;');
  4201. Add('begin');
  4202. Add('end;');
  4203. Add('var i: longint;');
  4204. Add('begin');
  4205. Add(' i:=func1();');
  4206. Add(' i:=func1()+func1();');
  4207. ConvertProgram;
  4208. CheckSource('TestAssignFunctionResult',
  4209. LinesToStr([ // statements
  4210. 'this.Func1 = function () {',
  4211. ' var Result = 0;',
  4212. ' return Result;',
  4213. '};',
  4214. 'this.i = 0;'
  4215. ]),
  4216. LinesToStr([
  4217. '$mod.i = $mod.Func1();',
  4218. '$mod.i = $mod.Func1() + $mod.Func1();'
  4219. ]));
  4220. end;
  4221. procedure TTestModule.TestFunctionResultInCondition;
  4222. begin
  4223. StartProgram(false);
  4224. Add('function Func1: longint;');
  4225. Add('begin');
  4226. Add('end;');
  4227. Add('function Func2: boolean;');
  4228. Add('begin');
  4229. Add('end;');
  4230. Add('var i: longint;');
  4231. Add('begin');
  4232. Add(' if func2 then ;');
  4233. Add(' if i=func1() then ;');
  4234. Add(' if i=func1 then ;');
  4235. ConvertProgram;
  4236. CheckSource('TestFunctionResultInCondition',
  4237. LinesToStr([ // statements
  4238. 'this.Func1 = function () {',
  4239. ' var Result = 0;',
  4240. ' return Result;',
  4241. '};',
  4242. 'this.Func2 = function () {',
  4243. ' var Result = false;',
  4244. ' return Result;',
  4245. '};',
  4246. 'this.i = 0;'
  4247. ]),
  4248. LinesToStr([
  4249. 'if ($mod.Func2());',
  4250. 'if ($mod.i === $mod.Func1());',
  4251. 'if ($mod.i === $mod.Func1());'
  4252. ]));
  4253. end;
  4254. procedure TTestModule.TestFunctionResultInForLoop;
  4255. begin
  4256. StartProgram(false);
  4257. Add([
  4258. 'function Func1(a: array of longint): longint;',
  4259. 'begin',
  4260. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  4261. ' for Result in a do if a[Result]=0 then exit;',
  4262. 'end;',
  4263. 'begin',
  4264. ' Func1([1,2,3])']);
  4265. ConvertProgram;
  4266. CheckSource('TestFunctionResultInForLoop',
  4267. LinesToStr([ // statements
  4268. 'this.Func1 = function (a) {',
  4269. ' var Result = 0;',
  4270. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  4271. ' Result = $l;',
  4272. ' if (a[Result] === 0) return Result;',
  4273. ' };',
  4274. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  4275. ' Result = $in[$l1];',
  4276. ' if (a[Result] === 0) return Result;',
  4277. ' };',
  4278. ' return Result;',
  4279. '};',
  4280. '']),
  4281. LinesToStr([
  4282. '$mod.Func1([1, 2, 3]);'
  4283. ]));
  4284. end;
  4285. procedure TTestModule.TestFunctionResultInTypeCast;
  4286. begin
  4287. StartProgram(false);
  4288. Add([
  4289. 'function GetInt: longint;',
  4290. 'begin',
  4291. 'end;',
  4292. 'begin',
  4293. ' if Byte(GetInt)=0 then ;',
  4294. '']);
  4295. ConvertProgram;
  4296. CheckSource('TestFunctionResultInTypeCast',
  4297. LinesToStr([ // statements
  4298. 'this.GetInt = function () {',
  4299. ' var Result = 0;',
  4300. ' return Result;',
  4301. '};',
  4302. '']),
  4303. LinesToStr([
  4304. 'if (($mod.GetInt() & 255) === 0) ;'
  4305. ]));
  4306. end;
  4307. procedure TTestModule.TestExit;
  4308. begin
  4309. StartProgram(false);
  4310. Add('procedure ProcA;');
  4311. Add('begin');
  4312. Add(' exit;');
  4313. Add('end;');
  4314. Add('function FuncB: longint;');
  4315. Add('begin');
  4316. Add(' exit;');
  4317. Add(' exit(3);');
  4318. Add('end;');
  4319. Add('function FuncC: string;');
  4320. Add('begin');
  4321. Add(' exit;');
  4322. Add(' exit(''a'');');
  4323. Add(' exit(''abc'');');
  4324. Add('end;');
  4325. Add('begin');
  4326. Add(' exit;');
  4327. Add(' exit(1);');
  4328. ConvertProgram;
  4329. CheckSource('TestExit',
  4330. LinesToStr([ // statements
  4331. 'this.ProcA = function () {',
  4332. ' return;',
  4333. '};',
  4334. 'this.FuncB = function () {',
  4335. ' var Result = 0;',
  4336. ' return Result;',
  4337. ' return 3;',
  4338. ' return Result;',
  4339. '};',
  4340. 'this.FuncC = function () {',
  4341. ' var Result = "";',
  4342. ' return Result;',
  4343. ' return "a";',
  4344. ' return "abc";',
  4345. ' return Result;',
  4346. '};'
  4347. ]),
  4348. LinesToStr([
  4349. 'return;',
  4350. 'return 1;',
  4351. '']));
  4352. end;
  4353. procedure TTestModule.TestExit_ResultInFinally;
  4354. begin
  4355. StartProgram(false);
  4356. Add([
  4357. 'function Run: word;',
  4358. 'begin',
  4359. ' try',
  4360. ' exit(3);', // no Result in finally -> use return 3
  4361. ' finally',
  4362. ' end;',
  4363. 'end;',
  4364. 'function Fly: word;',
  4365. 'begin',
  4366. ' try',
  4367. ' exit(3);',
  4368. ' finally',
  4369. ' if Result>0 then ;',
  4370. ' end;',
  4371. 'end;',
  4372. 'function Jump: word;',
  4373. 'begin',
  4374. ' try',
  4375. ' try',
  4376. ' exit(4);',
  4377. ' finally',
  4378. ' end;',
  4379. ' finally',
  4380. ' if Result>0 then ;',
  4381. ' end;',
  4382. 'end;',
  4383. 'begin',
  4384. '']);
  4385. ConvertProgram;
  4386. CheckSource('TestExit_ResultInFinally',
  4387. LinesToStr([ // statements
  4388. 'this.Run = function () {',
  4389. ' var Result = 0;',
  4390. ' try {',
  4391. ' return 3;',
  4392. ' } finally {',
  4393. ' };',
  4394. ' return Result;',
  4395. '};',
  4396. 'this.Fly = function () {',
  4397. ' var Result = 0;',
  4398. ' try {',
  4399. ' Result = 3;',
  4400. ' return Result;',
  4401. ' } finally {',
  4402. ' if (Result > 0) ;',
  4403. ' };',
  4404. ' return Result;',
  4405. '};',
  4406. 'this.Jump = function () {',
  4407. ' var Result = 0;',
  4408. ' try {',
  4409. ' try {',
  4410. ' Result = 4;',
  4411. ' return Result;',
  4412. ' } finally {',
  4413. ' };',
  4414. ' } finally {',
  4415. ' if (Result > 0) ;',
  4416. ' };',
  4417. ' return Result;',
  4418. '};',
  4419. '']),
  4420. LinesToStr([
  4421. '']));
  4422. end;
  4423. procedure TTestModule.TestBreak;
  4424. begin
  4425. StartProgram(false);
  4426. Add([
  4427. 'var',
  4428. ' i: longint;',
  4429. 'begin',
  4430. ' repeat',
  4431. ' break;',
  4432. ' until true;',
  4433. ' while true do',
  4434. ' break;',
  4435. ' for i:=1 to 2 do',
  4436. ' break;']);
  4437. ConvertProgram;
  4438. CheckSource('TestBreak',
  4439. LinesToStr([ // statements
  4440. 'this.i = 0;'
  4441. ]),
  4442. LinesToStr([
  4443. 'do {',
  4444. ' break;',
  4445. '} while (!true);',
  4446. 'while (true) break;',
  4447. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  4448. '']));
  4449. end;
  4450. procedure TTestModule.TestBreakAsVar;
  4451. begin
  4452. StartProgram(false);
  4453. Add([
  4454. 'procedure DoIt(break: boolean);',
  4455. 'begin',
  4456. ' if break then ;',
  4457. 'end;',
  4458. 'var',
  4459. ' break: boolean;',
  4460. 'begin',
  4461. ' if break then ;']);
  4462. ConvertProgram;
  4463. CheckSource('TestBreakAsVar',
  4464. LinesToStr([ // statements
  4465. 'this.DoIt = function (Break) {',
  4466. ' if (Break) ;',
  4467. '};',
  4468. 'this.Break = false;',
  4469. '']),
  4470. LinesToStr([
  4471. 'if($mod.Break) ;',
  4472. '']));
  4473. end;
  4474. procedure TTestModule.TestContinue;
  4475. begin
  4476. StartProgram(false);
  4477. Add('var i: longint;');
  4478. Add('begin');
  4479. Add(' repeat');
  4480. Add(' continue;');
  4481. Add(' until true;');
  4482. Add(' while true do');
  4483. Add(' continue;');
  4484. Add(' for i:=1 to 2 do');
  4485. Add(' continue;');
  4486. ConvertProgram;
  4487. CheckSource('TestContinue',
  4488. LinesToStr([ // statements
  4489. 'this.i = 0;'
  4490. ]),
  4491. LinesToStr([
  4492. 'do {',
  4493. ' continue;',
  4494. '} while (!true);',
  4495. 'while (true) continue;',
  4496. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  4497. '']));
  4498. end;
  4499. procedure TTestModule.TestProc_External;
  4500. begin
  4501. StartProgram(false);
  4502. Add('procedure Foo; external name ''console.log'';');
  4503. Add('function Bar: longint; external name ''get.item'';');
  4504. Add('function Bla(s: string): longint; external name ''apply.something'';');
  4505. Add('var');
  4506. Add(' i: longint;');
  4507. Add('begin');
  4508. Add(' Foo;');
  4509. Add(' i:=Bar;');
  4510. Add(' i:=Bla(''abc'');');
  4511. ConvertProgram;
  4512. CheckSource('TestProc_External',
  4513. LinesToStr([ // statements
  4514. 'this.i = 0;'
  4515. ]),
  4516. LinesToStr([
  4517. 'console.log();',
  4518. '$mod.i = get.item();',
  4519. '$mod.i = apply.something("abc");'
  4520. ]));
  4521. end;
  4522. procedure TTestModule.TestProc_ExternalOtherUnit;
  4523. begin
  4524. AddModuleWithIntfImplSrc('unit2.pas',
  4525. LinesToStr([
  4526. 'procedure Now; external name ''Date.now'';',
  4527. 'procedure DoIt;'
  4528. ]),
  4529. 'procedure doit; begin end;');
  4530. StartUnit(true);
  4531. Add('interface');
  4532. Add('uses unit2;');
  4533. Add('implementation');
  4534. Add('begin');
  4535. Add(' now;');
  4536. Add(' now();');
  4537. Add(' uNit2.now;');
  4538. Add(' uNit2.now();');
  4539. Add(' doit;');
  4540. Add(' uNit2.doit;');
  4541. ConvertUnit;
  4542. CheckSource('TestProc_ExternalOtherUnit',
  4543. LinesToStr([
  4544. '']),
  4545. LinesToStr([
  4546. 'Date.now();',
  4547. 'Date.now();',
  4548. 'Date.now();',
  4549. 'Date.now();',
  4550. 'pas.unit2.DoIt();',
  4551. 'pas.unit2.DoIt();',
  4552. '']));
  4553. end;
  4554. procedure TTestModule.TestProc_Asm;
  4555. begin
  4556. StartProgram(false);
  4557. Add([
  4558. '{$mode delphi}',
  4559. 'function DoIt: longint;',
  4560. 'begin;',
  4561. ' asm',
  4562. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4563. ' end;',
  4564. ' asm console.log(); end;',
  4565. ' asm',
  4566. ' s = "'' ";',
  4567. ' s = ''" '';',
  4568. ' s = s + "world" + "''";',
  4569. ' // end',
  4570. ' s = ''end'';',
  4571. ' s = "end";',
  4572. ' s = "foo\"bar";',
  4573. ' s = ''a\''b'';',
  4574. ' s = `${expr}\`-"-''-`;',
  4575. ' s = `multi',
  4576. 'line`;',
  4577. ' end;',
  4578. 'end;',
  4579. 'procedure Fly;',
  4580. 'asm',
  4581. ' return;',
  4582. 'end;',
  4583. 'begin']);
  4584. ConvertProgram;
  4585. CheckSource('TestProc_Asm',
  4586. LinesToStr([ // statements
  4587. 'this.DoIt = function () {',
  4588. ' var Result = 0;',
  4589. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4590. ' console.log();',
  4591. ' s = "'' ";',
  4592. ' s = ''" '';',
  4593. ' s = s + "world" + "''";',
  4594. ' // end',
  4595. ' s = ''end'';',
  4596. ' s = "end";',
  4597. ' s = "foo\"bar";',
  4598. ' s = ''a\''b'';',
  4599. ' s = `${expr}\`-"-''-`;',
  4600. ' s = `multi',
  4601. 'line`;',
  4602. ' return Result;',
  4603. '};',
  4604. 'this.Fly = function () {',
  4605. ' return;',
  4606. '};',
  4607. '']),
  4608. LinesToStr([
  4609. ''
  4610. ]));
  4611. end;
  4612. procedure TTestModule.TestProc_AsmSubBlock;
  4613. begin
  4614. StartProgram(true,[supTObject]);
  4615. Add([
  4616. '{$mode delphi}',
  4617. 'type',
  4618. ' TBird = class end;',
  4619. 'procedure Run(w: word);',
  4620. 'begin;',
  4621. ' if true then asm console.log(); end;',
  4622. ' if w>3 then asm',
  4623. ' var a = w+1;',
  4624. ' w = a+3;',
  4625. ' end;',
  4626. ' while (w>7) do asm',
  4627. ' w+=3; w*=2;',
  4628. ' end;',
  4629. ' try',
  4630. ' except',
  4631. ' on E: TBird do',
  4632. ' asm console.log(E); end;',
  4633. ' on E: TObject do',
  4634. ' asm var i=3; i--; end;',
  4635. ' else asm Fly; High; end;',
  4636. ' end;',
  4637. 'end;',
  4638. 'begin']);
  4639. ConvertProgram;
  4640. CheckSource('TestProc_AsmSubBlock',
  4641. LinesToStr([ // statements
  4642. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  4643. '});',
  4644. 'this.Run = function (w) {',
  4645. ' if (true) console.log();',
  4646. ' if (w > 3) {',
  4647. ' var a = w+1;',
  4648. ' w = a+3;',
  4649. ' };',
  4650. ' while (w > 7) {',
  4651. ' w+=3; w*=2;',
  4652. ' };',
  4653. ' try {} catch ($e) {',
  4654. ' if ($mod.TBird.isPrototypeOf($e)) {',
  4655. ' var E = $e;',
  4656. ' console.log(E);',
  4657. ' } else if (pas.system.TObject.isPrototypeOf($e)) {',
  4658. ' var E = $e;',
  4659. ' var i=3; i--;',
  4660. ' } else {',
  4661. ' Fly; High;',
  4662. ' }',
  4663. ' };',
  4664. '};',
  4665. '']),
  4666. LinesToStr([
  4667. ''
  4668. ]));
  4669. end;
  4670. procedure TTestModule.TestProc_Assembler;
  4671. begin
  4672. StartProgram(false);
  4673. Add('function DoIt: longint; assembler;');
  4674. Add('asm');
  4675. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  4676. Add('end;');
  4677. Add('begin');
  4678. ConvertProgram;
  4679. CheckSource('TestProc_Assembler',
  4680. LinesToStr([ // statements
  4681. 'this.DoIt = function () {',
  4682. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4683. '};'
  4684. ]),
  4685. LinesToStr([
  4686. ''
  4687. ]));
  4688. end;
  4689. procedure TTestModule.TestProc_VarParam;
  4690. begin
  4691. StartProgram(false);
  4692. Add('type integer = longint;');
  4693. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  4694. Add('var vJ: integer;');
  4695. Add('begin');
  4696. Add(' vg:=vg+1;');
  4697. Add(' vj:=vh+2;');
  4698. Add(' vi:=vi+3;');
  4699. Add(' doit(vg,vg,vg);');
  4700. Add(' doit(vh,vh,vj);');
  4701. Add(' doit(vi,vi,vi);');
  4702. Add(' doit(vj,vj,vj);');
  4703. Add('end;');
  4704. Add('var i: integer;');
  4705. Add('begin');
  4706. Add(' doit(i,i,i);');
  4707. ConvertProgram;
  4708. CheckSource('TestProc_VarParam',
  4709. LinesToStr([ // statements
  4710. 'this.DoIt = function (vG,vH,vI) {',
  4711. ' var vJ = 0;',
  4712. ' vG = vG + 1;',
  4713. ' vJ = vH + 2;',
  4714. ' vI.set(vI.get()+3);',
  4715. ' $mod.DoIt(vG, vG, {',
  4716. ' get: function () {',
  4717. ' return vG;',
  4718. ' },',
  4719. ' set: function (v) {',
  4720. ' vG = v;',
  4721. ' }',
  4722. ' });',
  4723. ' $mod.DoIt(vH, vH, {',
  4724. ' get: function () {',
  4725. ' return vJ;',
  4726. ' },',
  4727. ' set: function (v) {',
  4728. ' vJ = v;',
  4729. ' }',
  4730. ' });',
  4731. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4732. ' $mod.DoIt(vJ, vJ, {',
  4733. ' get: function () {',
  4734. ' return vJ;',
  4735. ' },',
  4736. ' set: function (v) {',
  4737. ' vJ = v;',
  4738. ' }',
  4739. ' });',
  4740. '};',
  4741. 'this.i = 0;'
  4742. ]),
  4743. LinesToStr([
  4744. '$mod.DoIt($mod.i,$mod.i,{',
  4745. ' p: $mod,',
  4746. ' get: function () {',
  4747. ' return this.p.i;',
  4748. ' },',
  4749. ' set: function (v) {',
  4750. ' this.p.i = v;',
  4751. ' }',
  4752. '});'
  4753. ]));
  4754. end;
  4755. procedure TTestModule.TestProc_VarParamString;
  4756. begin
  4757. StartProgram(false);
  4758. Add(['type TCaption = string;',
  4759. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  4760. 'var c: char;',
  4761. 'begin',
  4762. ' va[1]:=c;',
  4763. ' vb[2]:=c;',
  4764. ' vc[3]:=c;',
  4765. 'end;',
  4766. 'begin']);
  4767. ConvertProgram;
  4768. CheckSource('TestProc_VarParamString',
  4769. LinesToStr([ // statements
  4770. 'this.DoIt = function (vA,vB,vC) {',
  4771. ' var c = "\x00";',
  4772. ' vA = rtl.setCharAt(vA, 0, c);',
  4773. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  4774. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  4775. '};',
  4776. '']),
  4777. LinesToStr([
  4778. ]));
  4779. end;
  4780. procedure TTestModule.TestProc_VarParamV;
  4781. begin
  4782. StartProgram(false);
  4783. Add([
  4784. 'procedure Inc2(var i: longint);',
  4785. 'begin',
  4786. ' i:=i+2;',
  4787. 'end;',
  4788. 'procedure DoIt(v: longint);',
  4789. 'var p: array of longint;',
  4790. 'begin',
  4791. ' Inc2(v);',
  4792. ' Inc2(p[v]);',
  4793. 'end;',
  4794. 'begin']);
  4795. ConvertProgram;
  4796. CheckSource('TestProc_VarParamV',
  4797. LinesToStr([ // statements
  4798. 'this.Inc2 = function (i) {',
  4799. ' i.set(i.get()+2);',
  4800. '};',
  4801. 'this.DoIt = function (v) {',
  4802. ' var p = [];',
  4803. ' $mod.Inc2({get: function () {',
  4804. ' return v;',
  4805. ' }, set: function (w) {',
  4806. ' v = w;',
  4807. ' }});',
  4808. ' $mod.Inc2({',
  4809. ' a: v,',
  4810. ' p: p,',
  4811. ' get: function () {',
  4812. ' return this.p[this.a];',
  4813. ' },',
  4814. ' set: function (v) {',
  4815. ' this.p[this.a] = v;',
  4816. ' }',
  4817. ' });',
  4818. '};',
  4819. '']),
  4820. LinesToStr([
  4821. '']));
  4822. end;
  4823. procedure TTestModule.TestProc_Overload;
  4824. begin
  4825. StartProgram(false);
  4826. Add('procedure DoIt(vI: longint); begin end;');
  4827. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4828. Add('procedure DoIt(vD: double); begin end;');
  4829. Add('begin');
  4830. Add(' DoIt(1);');
  4831. Add(' DoIt(2,3);');
  4832. Add(' DoIt(4.5);');
  4833. ConvertProgram;
  4834. CheckSource('TestProcedureOverload',
  4835. LinesToStr([ // statements
  4836. 'this.DoIt = function (vI) {',
  4837. '};',
  4838. 'this.DoIt$1 = function (vI, vJ) {',
  4839. '};',
  4840. 'this.DoIt$2 = function (vD) {',
  4841. '};',
  4842. '']),
  4843. LinesToStr([
  4844. '$mod.DoIt(1);',
  4845. '$mod.DoIt$1(2, 3);',
  4846. '$mod.DoIt$2(4.5);',
  4847. '']));
  4848. end;
  4849. procedure TTestModule.TestProc_OverloadForward;
  4850. begin
  4851. StartProgram(false);
  4852. Add('procedure DoIt(vI: longint); forward;');
  4853. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4854. Add('procedure doit(vi: longint); begin end;');
  4855. Add('begin');
  4856. Add(' doit(1);');
  4857. Add(' doit(2,3);');
  4858. ConvertProgram;
  4859. CheckSource('TestProcedureOverloadForward',
  4860. LinesToStr([ // statements
  4861. 'this.DoIt$1 = function (vI, vJ) {',
  4862. '};',
  4863. 'this.DoIt = function (vI) {',
  4864. '};',
  4865. '']),
  4866. LinesToStr([
  4867. '$mod.DoIt(1);',
  4868. '$mod.DoIt$1(2, 3);',
  4869. '']));
  4870. end;
  4871. procedure TTestModule.TestProc_OverloadIntfImpl;
  4872. begin
  4873. StartUnit(false);
  4874. Add('interface');
  4875. Add('procedure DoIt(vI: longint);');
  4876. Add('procedure DoIt(vI, vJ: longint);');
  4877. Add('implementation');
  4878. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4879. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4880. Add('procedure DoIt(vi: longint); begin end;');
  4881. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4882. Add('procedure DoIt(vi, vj: longint); begin end;');
  4883. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4884. Add('begin');
  4885. Add(' doit(1);');
  4886. Add(' doit(2,3);');
  4887. Add(' doit(4,5,6);');
  4888. Add(' doit(7,8,9,10);');
  4889. Add(' doit(11,12,13,14,15);');
  4890. ConvertUnit;
  4891. CheckSource('TestProcedureOverloadUnit',
  4892. LinesToStr([ // statements
  4893. 'var $impl = $mod.$impl;',
  4894. 'this.DoIt = function (vI) {',
  4895. '};',
  4896. 'this.DoIt$1 = function (vI, vJ) {',
  4897. '};',
  4898. '']),
  4899. LinesToStr([ // this.$init
  4900. '$mod.DoIt(1);',
  4901. '$mod.DoIt$1(2, 3);',
  4902. '$impl.DoIt$3(4,5,6);',
  4903. '$impl.DoIt$4(7,8,9,10);',
  4904. '$impl.DoIt$2(11,12,13,14,15);',
  4905. '']),
  4906. LinesToStr([ // implementation
  4907. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4908. '};',
  4909. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4910. '};',
  4911. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4912. '};',
  4913. '']));
  4914. end;
  4915. procedure TTestModule.TestProc_OverloadNested;
  4916. begin
  4917. StartProgram(false);
  4918. Add([
  4919. 'procedure doit(vA: longint);',
  4920. ' procedure DoIt(vA, vB: longint); overload;',
  4921. ' begin',
  4922. ' doit(1);',
  4923. ' doit(1,2);',
  4924. ' end;',
  4925. ' procedure doit(vA, vB, vC: longint);',
  4926. ' begin',
  4927. ' doit(1);',
  4928. ' doit(1,2);',
  4929. ' doit(1,2,3);',
  4930. ' end;',
  4931. 'begin',
  4932. ' doit(1);',
  4933. ' doit(1,2);',
  4934. ' doit(1,2,3);',
  4935. 'end;',
  4936. 'begin // main',
  4937. ' doit(1);']);
  4938. ConvertProgram;
  4939. CheckSource('TestProcedureOverloadNested',
  4940. LinesToStr([ // statements
  4941. 'this.doit = function (vA) {',
  4942. ' function DoIt$1(vA, vB) {',
  4943. ' $mod.doit(1);',
  4944. ' DoIt$1(1, 2);',
  4945. ' };',
  4946. ' function doit$2(vA, vB, vC) {',
  4947. ' $mod.doit(1);',
  4948. ' DoIt$1(1, 2);',
  4949. ' doit$2(1, 2, 3);',
  4950. ' };',
  4951. ' $mod.doit(1);',
  4952. ' DoIt$1(1, 2);',
  4953. ' doit$2(1, 2, 3);',
  4954. '};',
  4955. '']),
  4956. LinesToStr([
  4957. '$mod.doit(1);',
  4958. '']));
  4959. end;
  4960. procedure TTestModule.TestProc_OverloadNestedForward;
  4961. begin
  4962. StartProgram(false);
  4963. Add([
  4964. 'procedure DoIt(vA: longint); overload; forward;',
  4965. 'procedure DoIt(vB, vC: longint); overload;',
  4966. 'begin // 2 param overload',
  4967. ' doit(1);',
  4968. ' doit(1,2);',
  4969. 'end;',
  4970. 'procedure doit(vA: longint);',
  4971. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4972. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4973. ' begin // 4 param overload',
  4974. ' doit(1);',
  4975. ' doit(1,2);',
  4976. ' doit(1,2,3);',
  4977. ' doit(1,2,3,4);',
  4978. ' end;',
  4979. ' procedure doit(vA, vB, vC: longint);',
  4980. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4981. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4982. ' begin // 6 param overload',
  4983. ' doit(1);',
  4984. ' doit(1,2);',
  4985. ' doit(1,2,3);',
  4986. ' doit(1,2,3,4);',
  4987. ' doit(1,2,3,4,5);',
  4988. ' doit(1,2,3,4,5,6);',
  4989. ' end;',
  4990. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4991. ' begin // 5 param overload',
  4992. ' doit(1);',
  4993. ' doit(1,2);',
  4994. ' doit(1,2,3);',
  4995. ' doit(1,2,3,4);',
  4996. ' doit(1,2,3,4,5);',
  4997. ' doit(1,2,3,4,5,6);',
  4998. ' end;',
  4999. ' begin // 3 param overload',
  5000. ' doit(1);',
  5001. ' doit(1,2);',
  5002. ' doit(1,2,3);',
  5003. ' doit(1,2,3,4);',
  5004. ' doit(1,2,3,4,5);',
  5005. ' doit(1,2,3,4,5,6);',
  5006. ' end;',
  5007. 'begin // 1 param overload',
  5008. ' doit(1);',
  5009. ' doit(1,2);',
  5010. ' doit(1,2,3);',
  5011. ' doit(1,2,3,4);',
  5012. 'end;',
  5013. 'begin // main',
  5014. ' doit(1);',
  5015. ' doit(1,2);']);
  5016. ConvertProgram;
  5017. CheckSource('TestProc_OverloadNestedForward',
  5018. LinesToStr([ // statements
  5019. 'this.DoIt$1 = function (vB, vC) {',
  5020. ' $mod.DoIt(1);',
  5021. ' $mod.DoIt$1(1, 2);',
  5022. '};',
  5023. 'this.DoIt = function (vA) {',
  5024. ' function DoIt$3(vA, vB, vC, vD) {',
  5025. ' $mod.DoIt(1);',
  5026. ' $mod.DoIt$1(1, 2);',
  5027. ' DoIt$2(1, 2, 3);',
  5028. ' DoIt$3(1, 2, 3, 4);',
  5029. ' };',
  5030. ' function DoIt$2(vA, vB, vC) {',
  5031. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  5032. ' $mod.DoIt(1);',
  5033. ' $mod.DoIt$1(1, 2);',
  5034. ' DoIt$2(1, 2, 3);',
  5035. ' DoIt$3(1, 2, 3, 4);',
  5036. ' DoIt$4(1, 2, 3, 4, 5);',
  5037. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5038. ' };',
  5039. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  5040. ' $mod.DoIt(1);',
  5041. ' $mod.DoIt$1(1, 2);',
  5042. ' DoIt$2(1, 2, 3);',
  5043. ' DoIt$3(1, 2, 3, 4);',
  5044. ' DoIt$4(1, 2, 3, 4, 5);',
  5045. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5046. ' };',
  5047. ' $mod.DoIt(1);',
  5048. ' $mod.DoIt$1(1, 2);',
  5049. ' DoIt$2(1, 2, 3);',
  5050. ' DoIt$3(1, 2, 3, 4);',
  5051. ' DoIt$4(1, 2, 3, 4, 5);',
  5052. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5053. ' };',
  5054. ' $mod.DoIt(1);',
  5055. ' $mod.DoIt$1(1, 2);',
  5056. ' DoIt$2(1, 2, 3);',
  5057. ' DoIt$3(1, 2, 3, 4);',
  5058. '};',
  5059. '']),
  5060. LinesToStr([
  5061. '$mod.DoIt(1);',
  5062. '$mod.DoIt$1(1, 2);',
  5063. '']));
  5064. end;
  5065. procedure TTestModule.TestProc_OverloadUnitCycle;
  5066. begin
  5067. AddModuleWithIntfImplSrc('Unit2.pas',
  5068. LinesToStr([
  5069. 'type',
  5070. ' TObject = class',
  5071. ' procedure DoIt(b: boolean); virtual; abstract;',
  5072. ' procedure DoIt(i: longint); virtual; abstract;',
  5073. ' end;',
  5074. '']),
  5075. 'uses test1;');
  5076. StartUnit(true);
  5077. Add([
  5078. 'interface',
  5079. 'uses unit2;',
  5080. 'type',
  5081. ' TEagle = class(TObject)',
  5082. ' procedure DoIt(b: boolean); override;',
  5083. ' procedure DoIt(i: longint); override;',
  5084. ' end;',
  5085. 'implementation',
  5086. 'procedure TEagle.DoIt(b: boolean); begin end;',
  5087. 'procedure TEagle.DoIt(i: longint); begin end;',
  5088. '']);
  5089. ConvertUnit;
  5090. CheckSource('TestProc_OverloadUnitCycle',
  5091. LinesToStr([ // statements
  5092. 'rtl.createClass(this, "TEagle", pas.Unit2.TObject, function () {',
  5093. ' this.DoIt = function (b) {',
  5094. ' };',
  5095. ' this.DoIt$1 = function (i) {',
  5096. ' };',
  5097. '});',
  5098. '']),
  5099. '',
  5100. LinesToStr([
  5101. '']));
  5102. end;
  5103. procedure TTestModule.TestProc_Varargs;
  5104. begin
  5105. StartProgram(false);
  5106. Add([
  5107. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  5108. 'procedure ProcB; varargs; external name ''ProcB'';',
  5109. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  5110. 'function GetIt: longint; begin end;',
  5111. 'begin',
  5112. ' ProcA(1);',
  5113. ' ProcA(1,2);',
  5114. ' ProcA(1,2.0);',
  5115. ' ProcA(1,2,3);',
  5116. ' ProcA(1,''2'');',
  5117. ' ProcA(2,'''');',
  5118. ' ProcA(3,false);',
  5119. ' ProcB;',
  5120. ' ProcB();',
  5121. ' ProcB(4);',
  5122. ' ProcB(''foo'');',
  5123. ' ProcC;',
  5124. ' ProcC();',
  5125. ' ProcC(4);',
  5126. ' ProcC(5,''foo'');',
  5127. ' ProcB(GetIt);',
  5128. ' ProcB(GetIt());',
  5129. ' ProcB(GetIt,GetIt());']);
  5130. ConvertProgram;
  5131. CheckSource('TestProc_Varargs',
  5132. LinesToStr([ // statements
  5133. 'this.GetIt = function () {',
  5134. ' var Result = 0;',
  5135. ' return Result;',
  5136. '};',
  5137. '']),
  5138. LinesToStr([
  5139. 'ProcA(1);',
  5140. 'ProcA(1, 2);',
  5141. 'ProcA(1, 2.0);',
  5142. 'ProcA(1, 2, 3);',
  5143. 'ProcA(1, "2");',
  5144. 'ProcA(2, "");',
  5145. 'ProcA(3, false);',
  5146. 'ProcB();',
  5147. 'ProcB();',
  5148. 'ProcB(4);',
  5149. 'ProcB("foo");',
  5150. 'ProcC(17);',
  5151. 'ProcC(17);',
  5152. 'ProcC(4);',
  5153. 'ProcC(5, "foo");',
  5154. 'ProcB($mod.GetIt());',
  5155. 'ProcB($mod.GetIt());',
  5156. 'ProcB($mod.GetIt(), $mod.GetIt());',
  5157. '']));
  5158. end;
  5159. procedure TTestModule.TestProc_ConstOrder;
  5160. begin
  5161. StartProgram(false);
  5162. Add([
  5163. 'const A = 3;',
  5164. 'const B = A+1;',
  5165. 'procedure DoIt;',
  5166. 'const C = A+1;',
  5167. 'const D = B+1;',
  5168. 'const E = D+C+B+A;',
  5169. 'begin',
  5170. 'end;',
  5171. 'begin'
  5172. ]);
  5173. ConvertProgram;
  5174. CheckSource('TestProc_ConstOrder',
  5175. LinesToStr([ // statements
  5176. 'this.A = 3;',
  5177. 'this.B = 3 + 1;',
  5178. 'var C = 3 + 1;',
  5179. 'var D = 4 + 1;',
  5180. 'var E = 5 + 4 + 4 + 3;',
  5181. 'this.DoIt = function () {',
  5182. '};',
  5183. '']),
  5184. LinesToStr([
  5185. ''
  5186. ]));
  5187. end;
  5188. procedure TTestModule.TestProc_DuplicateConst;
  5189. begin
  5190. StartProgram(false);
  5191. Add([
  5192. 'const A = 1;',
  5193. 'procedure DoIt;',
  5194. 'const A = 2;',
  5195. ' procedure SubIt;',
  5196. ' const A = 21;',
  5197. ' begin',
  5198. ' end;',
  5199. 'begin',
  5200. 'end;',
  5201. 'procedure DoSome;',
  5202. 'const A = 3;',
  5203. 'begin',
  5204. 'end;',
  5205. 'begin'
  5206. ]);
  5207. ConvertProgram;
  5208. CheckSource('TestProc_DuplicateConst',
  5209. LinesToStr([ // statements
  5210. 'this.A = 1;',
  5211. 'var A$1 = 2;',
  5212. 'var A$2 = 21;',
  5213. 'this.DoIt = function () {',
  5214. ' function SubIt() {',
  5215. ' };',
  5216. '};',
  5217. 'var A$3 = 3;',
  5218. 'this.DoSome = function () {',
  5219. '};',
  5220. '']),
  5221. LinesToStr([
  5222. ''
  5223. ]));
  5224. end;
  5225. procedure TTestModule.TestProc_LocalVarAbsolute;
  5226. begin
  5227. StartProgram(false);
  5228. Add([
  5229. 'type',
  5230. ' TObject = class',
  5231. ' Index: longint;',
  5232. ' procedure DoAbs(Item: pointer);',
  5233. ' end;',
  5234. 'procedure TObject.DoAbs(Item: pointer);',
  5235. 'var',
  5236. ' o: TObject absolute Item;',
  5237. 'begin',
  5238. ' if o.Index<o.Index then o.Index:=o.Index;',
  5239. 'end;',
  5240. 'procedure DoIt(i: longint; p: pointer);',
  5241. 'var',
  5242. ' d: double absolute i;',
  5243. ' s: string absolute d;',
  5244. ' oi: TObject absolute i;',
  5245. ' op: TObject absolute p;',
  5246. 'begin',
  5247. ' if d=d then d:=d;',
  5248. ' if s=s then s:=s;',
  5249. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  5250. ' if op.Index=op.Index then op.Index:=op.Index;',
  5251. 'end;',
  5252. 'begin']);
  5253. ConvertProgram;
  5254. CheckSource('TestProc_LocalVarAbsolute',
  5255. LinesToStr([ // statements
  5256. 'rtl.createClass(this, "TObject", null, function () {',
  5257. ' this.$init = function () {',
  5258. ' this.Index = 0;',
  5259. ' };',
  5260. ' this.$final = function () {',
  5261. ' };',
  5262. ' this.DoAbs = function (Item) {',
  5263. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  5264. ' };',
  5265. '});',
  5266. 'this.DoIt = function (i, p) {',
  5267. ' if (i === i) i = i;',
  5268. ' if (i === i) i = i;',
  5269. ' if (i.Index < i.Index) i.Index = i.Index;',
  5270. ' if (p.Index === p.Index) p.Index = p.Index;',
  5271. '};'
  5272. ]),
  5273. LinesToStr([
  5274. ]));
  5275. end;
  5276. procedure TTestModule.TestProc_ResultAbsolute;
  5277. begin
  5278. StartProgram(false);
  5279. Add([
  5280. 'type',
  5281. ' TObject = class',
  5282. ' Index: longint;',
  5283. ' function DoAbs: pointer;',
  5284. ' end;',
  5285. 'function TObject.DoAbs: pointer;',
  5286. 'var',
  5287. ' o: TObject absolute Result;',
  5288. 'begin',
  5289. ' if o.Index<o.Index then o.Index:=o.Index;',
  5290. 'end;',
  5291. 'function DoIt: jsvalue;',
  5292. 'var',
  5293. ' d: double absolute Result;',
  5294. ' s: string absolute Result;',
  5295. ' o: TObject absolute Result;',
  5296. 'begin',
  5297. ' if d=d then d:=d;',
  5298. ' if s=s then s:=s;',
  5299. ' if o.Index<o.Index then o.Index:=o.Index;',
  5300. 'end;',
  5301. 'begin']);
  5302. ConvertProgram;
  5303. CheckSource('TestProc_ResultAbsolute',
  5304. LinesToStr([ // statements
  5305. 'rtl.createClass(this, "TObject", null, function () {',
  5306. ' this.$init = function () {',
  5307. ' this.Index = 0;',
  5308. ' };',
  5309. ' this.$final = function () {',
  5310. ' };',
  5311. ' this.DoAbs = function () {',
  5312. ' var Result = null;',
  5313. ' if (Result.Index < Result.Index) Result.Index = Result.Index;',
  5314. ' return Result;',
  5315. ' };',
  5316. '});',
  5317. 'this.DoIt = function () {',
  5318. ' var Result = undefined;',
  5319. ' if (Result === Result) Result = Result;',
  5320. ' if (Result === Result) Result = Result;',
  5321. ' if (Result.Index < Result.Index) Result.Index = Result.Index;',
  5322. ' return Result;',
  5323. '};',
  5324. '']),
  5325. LinesToStr([
  5326. ]));
  5327. end;
  5328. procedure TTestModule.TestProc_LocalVarInit;
  5329. begin
  5330. StartProgram(false);
  5331. Add([
  5332. 'type TBytes = array of byte;',
  5333. 'procedure DoIt;',
  5334. 'const c = 4;',
  5335. 'var',
  5336. ' b: byte = 1;',
  5337. ' w: word = 2+c;',
  5338. ' p: pointer = nil;',
  5339. ' Buffer: TBytes = nil;',
  5340. 'begin',
  5341. 'end;',
  5342. 'begin']);
  5343. ConvertProgram;
  5344. CheckSource('TestProc_LocalVarInit',
  5345. LinesToStr([ // statements
  5346. 'var c = 4;',
  5347. 'this.DoIt = function () {',
  5348. ' var b = 1;',
  5349. ' var w = 2 + 4;',
  5350. ' var p = null;',
  5351. ' var Buffer = [];',
  5352. '};',
  5353. '']),
  5354. LinesToStr([
  5355. ]));
  5356. end;
  5357. procedure TTestModule.TestProc_ReservedWords;
  5358. begin
  5359. StartProgram(false);
  5360. Add([
  5361. 'procedure Date(ArrayBuffer: longint);',
  5362. 'const',
  5363. ' NaN: longint = 3;',
  5364. 'var',
  5365. ' &Boolean: longint;',
  5366. ' procedure Error(ArrayBuffer: longint);',
  5367. ' begin',
  5368. ' end;',
  5369. 'begin',
  5370. ' Nan:=&bOolean;',
  5371. 'end;',
  5372. 'begin',
  5373. ' Date(1);']);
  5374. ConvertProgram;
  5375. CheckSource('TestProc_ReservedWords',
  5376. LinesToStr([ // statements
  5377. 'var naN = 3;',
  5378. 'this.Date = function (arrayBuffer) {',
  5379. ' var boolean = 0;',
  5380. ' function error(arrayBuffer) {',
  5381. ' };',
  5382. ' naN = boolean;',
  5383. '};',
  5384. '']),
  5385. LinesToStr([
  5386. ' $mod.Date(1);'
  5387. ]));
  5388. end;
  5389. procedure TTestModule.TestProc_ConstRefWord;
  5390. begin
  5391. StartProgram(false);
  5392. Add([
  5393. 'procedure Run(constref w: word);',
  5394. 'var l: word;',
  5395. 'begin',
  5396. ' l:=w;',
  5397. ' Run(w);',
  5398. ' Run(l);',
  5399. 'end;',
  5400. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  5401. 'begin',
  5402. ' Run(a);',
  5403. ' Run(b);',
  5404. ' Run(c);',
  5405. ' Run(d);',
  5406. ' Run(e);',
  5407. 'end;',
  5408. 'begin',
  5409. ' Run(1);']);
  5410. ConvertProgram;
  5411. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  5412. CheckSource('TestProc_ConstRefWord',
  5413. LinesToStr([ // statements
  5414. 'this.Run = function (w) {',
  5415. ' var l = 0;',
  5416. ' l = w;',
  5417. ' $mod.Run(w);',
  5418. ' $mod.Run(l);',
  5419. '};',
  5420. 'this.Fly = function (a, b, c, d, e) {',
  5421. ' $mod.Run(a);',
  5422. ' $mod.Run(b.get());',
  5423. ' $mod.Run(c.get());',
  5424. ' $mod.Run(d);',
  5425. ' $mod.Run(e);',
  5426. '};',
  5427. '']),
  5428. LinesToStr([
  5429. '$mod.Run(1);'
  5430. ]));
  5431. end;
  5432. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  5433. begin
  5434. StartProgram(false);
  5435. Add([
  5436. '{$mode objfpc}',
  5437. 'type',
  5438. ' TFunc = reference to function(x: word): word;',
  5439. 'var Func: TFunc;',
  5440. 'procedure DoIt(a: word);',
  5441. 'begin',
  5442. ' Func:=function(b:word): word',
  5443. ' begin',
  5444. ' Result:=a+b;',
  5445. ' exit(b);',
  5446. ' exit(Result);',
  5447. ' end;',// test semicolon
  5448. ' a:=3;',
  5449. 'end;',
  5450. 'begin',
  5451. ' Func:=function(c:word):word begin',
  5452. ' Result:=3+c;',
  5453. ' exit(c);',
  5454. ' exit(Result);',
  5455. ' end;']);
  5456. ConvertProgram;
  5457. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  5458. LinesToStr([ // statements
  5459. 'this.Func = null;',
  5460. 'this.DoIt = function (a) {',
  5461. ' $mod.Func = function (b) {',
  5462. ' var Result = 0;',
  5463. ' Result = a + b;',
  5464. ' return b;',
  5465. ' return Result;',
  5466. ' return Result;',
  5467. ' };',
  5468. ' a = 3;',
  5469. '};',
  5470. '']),
  5471. LinesToStr([
  5472. '$mod.Func = function (c) {',
  5473. ' var Result = 0;',
  5474. ' Result = 3 + c;',
  5475. ' return c;',
  5476. ' return Result;',
  5477. ' return Result;',
  5478. '};',
  5479. '']));
  5480. end;
  5481. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  5482. begin
  5483. StartProgram(false);
  5484. Add([
  5485. '{$mode delphi}',
  5486. 'type',
  5487. ' TProc = reference to procedure(x: word);',
  5488. 'procedure DoIt(a: word);',
  5489. 'var Proc: TProc;',
  5490. 'begin',
  5491. ' Proc:=procedure(b:word) begin end;',
  5492. 'end;',
  5493. 'var Proc: TProc;',
  5494. 'begin',
  5495. ' Proc:=procedure(c:word) begin end;',
  5496. '']);
  5497. ConvertProgram;
  5498. CheckSource('TestAnonymousProc_Assign_Delphi',
  5499. LinesToStr([ // statements
  5500. 'this.DoIt = function (a) {',
  5501. ' var Proc = null;',
  5502. ' Proc = function (b) {',
  5503. ' };',
  5504. '};',
  5505. 'this.Proc = null;',
  5506. '']),
  5507. LinesToStr([
  5508. '$mod.Proc = function (c) {',
  5509. '};',
  5510. '']));
  5511. end;
  5512. procedure TTestModule.TestAnonymousProc_Arg;
  5513. begin
  5514. StartProgram(false);
  5515. Add([
  5516. 'type',
  5517. ' TProc = reference to procedure;',
  5518. ' TFunc = reference to function(x: word): word;',
  5519. 'procedure DoMore(f,g: TProc);',
  5520. 'begin',
  5521. 'end;',
  5522. 'procedure DoOdd(v: jsvalue);',
  5523. 'begin',
  5524. 'end;',
  5525. 'procedure DoIt(f: TFunc);',
  5526. 'begin',
  5527. ' DoIt(function(b:word): word',
  5528. ' begin',
  5529. ' Result:=1+b;',
  5530. ' end);',
  5531. ' DoMore(procedure begin end, procedure begin end);',
  5532. ' DoOdd(procedure begin end);',
  5533. 'end;',
  5534. 'begin',
  5535. ' DoMore(procedure begin end,',
  5536. ' procedure assembler asm',
  5537. ' console.log("c");',
  5538. ' end);',
  5539. '']);
  5540. ConvertProgram;
  5541. CheckSource('TestAnonymousProc_Arg',
  5542. LinesToStr([ // statements
  5543. 'this.DoMore = function (f, g) {',
  5544. '};',
  5545. 'this.DoOdd = function (v) {',
  5546. '};',
  5547. 'this.DoIt = function (f) {',
  5548. ' $mod.DoIt(function (b) {',
  5549. ' var Result = 0;',
  5550. ' Result = 1 + b;',
  5551. ' return Result;',
  5552. ' });',
  5553. ' $mod.DoMore(function () {',
  5554. ' }, function () {',
  5555. ' });',
  5556. ' $mod.DoOdd(function () {',
  5557. ' });',
  5558. '};',
  5559. '']),
  5560. LinesToStr([
  5561. '$mod.DoMore(function () {',
  5562. '}, function () {',
  5563. ' console.log("c");',
  5564. '});',
  5565. '']));
  5566. end;
  5567. procedure TTestModule.TestAnonymousProc_Typecast;
  5568. begin
  5569. StartProgram(false);
  5570. Add([
  5571. 'type',
  5572. ' TProc = reference to procedure(w: word);',
  5573. ' TArr = array of word;',
  5574. ' TFuncArr = reference to function: TArr;',
  5575. 'procedure DoIt(p: TProc);',
  5576. 'var',
  5577. ' w: word;',
  5578. ' a: TArr;',
  5579. 'begin',
  5580. ' p:=TProc(procedure(b: smallint) begin end);',
  5581. ' a:=TFuncArr(function: TArr begin end)();',
  5582. ' w:=TFuncArr(function: TArr begin end)()[3];',
  5583. 'end;',
  5584. 'begin']);
  5585. ConvertProgram;
  5586. CheckSource('TestAnonymousProc_Typecast',
  5587. LinesToStr([ // statements
  5588. 'this.DoIt = function (p) {',
  5589. ' var w = 0;',
  5590. ' var a = [];',
  5591. ' p = function (b) {',
  5592. ' };',
  5593. ' a = function () {',
  5594. ' var Result = [];',
  5595. ' return Result;',
  5596. ' }();',
  5597. ' w = function () {',
  5598. ' var Result = [];',
  5599. ' return Result;',
  5600. ' }()[3];',
  5601. '};',
  5602. '']),
  5603. LinesToStr([
  5604. '']));
  5605. end;
  5606. procedure TTestModule.TestAnonymousProc_With;
  5607. begin
  5608. StartProgram(false);
  5609. Add([
  5610. 'type',
  5611. ' TProc = reference to procedure(w: word);',
  5612. ' TObject = class',
  5613. ' b: boolean;',
  5614. ' end;',
  5615. 'var',
  5616. ' p: TProc;',
  5617. ' bird: TObject;',
  5618. 'begin',
  5619. ' with bird do',
  5620. ' p:=procedure(w: word)',
  5621. ' begin',
  5622. ' b:=w>2;',
  5623. ' end;',
  5624. '']);
  5625. ConvertProgram;
  5626. CheckSource('TestAnonymousProc_With',
  5627. LinesToStr([ // statements
  5628. 'rtl.createClass(this, "TObject", null, function () {',
  5629. ' this.$init = function () {',
  5630. ' this.b = false;',
  5631. ' };',
  5632. ' this.$final = function () {',
  5633. ' };',
  5634. '});',
  5635. 'this.p = null;',
  5636. 'this.bird = null;',
  5637. '']),
  5638. LinesToStr([
  5639. 'var $with = $mod.bird;',
  5640. '$mod.p = function (w) {',
  5641. ' $with.b = w > 2;',
  5642. '};',
  5643. '']));
  5644. end;
  5645. procedure TTestModule.TestAnonymousProc_ExceptOn;
  5646. begin
  5647. StartProgram(false);
  5648. Add([
  5649. 'type',
  5650. ' TProc = reference to procedure;',
  5651. ' TObject = class',
  5652. ' b: boolean;',
  5653. ' end;',
  5654. 'procedure DoIt;',
  5655. 'var',
  5656. ' p: TProc;',
  5657. 'begin',
  5658. ' try',
  5659. ' except',
  5660. ' on E: TObject do',
  5661. ' p:=procedure',
  5662. ' begin',
  5663. ' E.b:=true;',
  5664. ' end;',
  5665. ' end;',
  5666. 'end;',
  5667. 'begin']);
  5668. ConvertProgram;
  5669. CheckSource('TestAnonymousProc_ExceptOn',
  5670. LinesToStr([ // statements
  5671. 'rtl.createClass(this, "TObject", null, function () {',
  5672. ' this.$init = function () {',
  5673. ' this.b = false;',
  5674. ' };',
  5675. ' this.$final = function () {',
  5676. ' };',
  5677. '});',
  5678. 'this.DoIt = function () {',
  5679. ' var p = null;',
  5680. ' try {} catch ($e) {',
  5681. ' if ($mod.TObject.isPrototypeOf($e)) {',
  5682. ' var E = $e;',
  5683. ' p = function () {',
  5684. ' E.b = true;',
  5685. ' };',
  5686. ' } else throw $e',
  5687. ' };',
  5688. '};',
  5689. '']),
  5690. LinesToStr([
  5691. '']));
  5692. end;
  5693. procedure TTestModule.TestAnonymousProc_Nested;
  5694. begin
  5695. StartProgram(false);
  5696. Add([
  5697. 'type',
  5698. ' TProc = reference to procedure;',
  5699. ' TObject = class',
  5700. ' i: byte;',
  5701. ' procedure DoIt;',
  5702. ' end;',
  5703. 'procedure TObject.DoIt;',
  5704. 'var',
  5705. ' p: TProc;',
  5706. ' procedure Sub;',
  5707. ' begin',
  5708. ' p:=procedure',
  5709. ' begin',
  5710. ' i:=3;',
  5711. ' Self.i:=4;',
  5712. ' p:=procedure',
  5713. ' procedure SubSub;',
  5714. ' begin',
  5715. ' i:=13;',
  5716. ' Self.i:=14;',
  5717. ' end;',
  5718. ' begin',
  5719. ' i:=13;',
  5720. ' Self.i:=14;',
  5721. ' end;',
  5722. ' end;',
  5723. ' end;',
  5724. 'begin',
  5725. 'end;',
  5726. 'begin']);
  5727. ConvertProgram;
  5728. CheckSource('TestAnonymousProc_Nested',
  5729. LinesToStr([ // statements
  5730. 'rtl.createClass(this, "TObject", null, function () {',
  5731. ' this.$init = function () {',
  5732. ' this.i = 0;',
  5733. ' };',
  5734. ' this.$final = function () {',
  5735. ' };',
  5736. ' this.DoIt = function () {',
  5737. ' var $Self = this;',
  5738. ' var p = null;',
  5739. ' function Sub() {',
  5740. ' p = function () {',
  5741. ' $Self.i = 3;',
  5742. ' $Self.i = 4;',
  5743. ' p = function () {',
  5744. ' function SubSub() {',
  5745. ' $Self.i = 13;',
  5746. ' $Self.i = 14;',
  5747. ' };',
  5748. ' $Self.i = 13;',
  5749. ' $Self.i = 14;',
  5750. ' };',
  5751. ' };',
  5752. ' };',
  5753. ' };',
  5754. '});',
  5755. '']),
  5756. LinesToStr([
  5757. '']));
  5758. end;
  5759. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  5760. begin
  5761. StartProgram(false);
  5762. Add([
  5763. 'type',
  5764. ' TProc = reference to procedure;',
  5765. 'function DoIt: TProc;',
  5766. ' function Sub: TProc;',
  5767. ' begin',
  5768. ' Result:=procedure',
  5769. ' begin',
  5770. ' Sub:=procedure',
  5771. ' procedure SubSub;',
  5772. ' begin',
  5773. ' Result:=nil;',
  5774. ' Sub:=nil;',
  5775. ' DoIt:=nil;',
  5776. ' end;',
  5777. ' begin',
  5778. ' Result:=nil;',
  5779. ' Sub:=nil;',
  5780. ' DoIt:=nil;',
  5781. ' end;',
  5782. ' end;',
  5783. ' end;',
  5784. 'begin',
  5785. 'end;',
  5786. 'begin']);
  5787. ConvertProgram;
  5788. CheckSource('TestAnonymousProc_NestedAssignResult',
  5789. LinesToStr([ // statements
  5790. 'this.DoIt = function () {',
  5791. ' var Result = null;',
  5792. ' function Sub() {',
  5793. ' var Result$1 = null;',
  5794. ' Result$1 = function () {',
  5795. ' Result$1 = function () {',
  5796. ' function SubSub() {',
  5797. ' Result$1 = null;',
  5798. ' Result$1 = null;',
  5799. ' Result = null;',
  5800. ' };',
  5801. ' Result$1 = null;',
  5802. ' Result$1 = null;',
  5803. ' Result = null;',
  5804. ' };',
  5805. ' };',
  5806. ' return Result$1;',
  5807. ' };',
  5808. ' return Result;',
  5809. '};',
  5810. '']),
  5811. LinesToStr([
  5812. '']));
  5813. end;
  5814. procedure TTestModule.TestAnonymousProc_Class;
  5815. begin
  5816. StartProgram(false);
  5817. Add([
  5818. 'type',
  5819. ' TProc = reference to procedure;',
  5820. ' TEvent = procedure of object;',
  5821. ' TObject = class',
  5822. ' Size: word;',
  5823. ' function GetIt: TProc;',
  5824. ' procedure DoIt; virtual; abstract;',
  5825. ' end;',
  5826. 'function TObject.GetIt: TProc;',
  5827. 'begin',
  5828. ' Result:=procedure',
  5829. ' var p: TEvent;',
  5830. ' begin',
  5831. ' Size:=Size;',
  5832. ' Size:=Self.Size;',
  5833. ' p:=@DoIt;',
  5834. ' p:[email protected];',
  5835. ' end;',
  5836. 'end;',
  5837. 'begin']);
  5838. ConvertProgram;
  5839. CheckSource('TestAnonymousProc_Class',
  5840. LinesToStr([ // statements
  5841. 'rtl.createClass(this, "TObject", null, function () {',
  5842. ' this.$init = function () {',
  5843. ' this.Size = 0;',
  5844. ' };',
  5845. ' this.$final = function () {',
  5846. ' };',
  5847. ' this.GetIt = function () {',
  5848. ' var $Self = this;',
  5849. ' var Result = null;',
  5850. ' Result = function () {',
  5851. ' var p = null;',
  5852. ' $Self.Size = $Self.Size;',
  5853. ' $Self.Size = $Self.Size;',
  5854. ' p = rtl.createCallback($Self, "DoIt");',
  5855. ' p = rtl.createCallback($Self, "DoIt");',
  5856. ' };',
  5857. ' return Result;',
  5858. ' };',
  5859. '});',
  5860. '']),
  5861. LinesToStr([
  5862. '']));
  5863. end;
  5864. procedure TTestModule.TestAnonymousProc_ForLoop;
  5865. begin
  5866. StartProgram(false);
  5867. Add([
  5868. 'type TProc = reference to procedure;',
  5869. 'procedure Foo(p: TProc);',
  5870. 'begin',
  5871. 'end;',
  5872. 'procedure DoIt;',
  5873. 'var i: word;',
  5874. ' a: word;',
  5875. 'begin',
  5876. ' for i:=1 to 10 do begin',
  5877. ' Foo(procedure begin a:=3; end);',
  5878. ' end;',
  5879. 'end;',
  5880. 'begin',
  5881. ' DoIt;']);
  5882. ConvertProgram;
  5883. CheckSource('TestAnonymousProc_ForLoop',
  5884. LinesToStr([ // statements
  5885. 'this.Foo = function (p) {',
  5886. '};',
  5887. 'this.DoIt = function () {',
  5888. ' var i = 0;',
  5889. ' var a = 0;',
  5890. ' for (i = 1; i <= 10; i++) {',
  5891. ' $mod.Foo(function () {',
  5892. ' a = 3;',
  5893. ' });',
  5894. ' };',
  5895. '};',
  5896. '']),
  5897. LinesToStr([
  5898. '$mod.DoIt();'
  5899. ]));
  5900. end;
  5901. procedure TTestModule.TestAnonymousProc_AsmDelphi;
  5902. begin
  5903. StartProgram(false);
  5904. Add([
  5905. '{$mode delphi}',
  5906. 'type',
  5907. ' TProc = reference to procedure;',
  5908. ' TFunc = reference to function(x: word): word;',
  5909. 'procedure Run;',
  5910. 'asm',
  5911. 'end;',
  5912. 'procedure Walk(p: TProc; f: TFunc);',
  5913. 'begin',
  5914. ' Walk(procedure asm end, function(b:word): word asm return 1+b; end);',
  5915. 'end;',
  5916. 'begin',
  5917. ' Walk(procedure',
  5918. ' asm',
  5919. ' console.log("a");',
  5920. ' end,',
  5921. ' function(x: word): word asm',
  5922. ' console.log("c");',
  5923. ' end);',
  5924. '']);
  5925. ConvertProgram;
  5926. CheckSource('TestAnonymousProc_AsmDelphi',
  5927. LinesToStr([ // statements
  5928. 'this.Run = function () {',
  5929. '};',
  5930. 'this.Walk = function (p, f) {',
  5931. ' $mod.Walk(function () {',
  5932. ' }, function (b) {',
  5933. ' return 1+b;',
  5934. ' });',
  5935. '};',
  5936. '']),
  5937. LinesToStr([
  5938. '$mod.Walk(function () {',
  5939. ' console.log("a");',
  5940. '}, function (x) {',
  5941. ' console.log("c");',
  5942. '});',
  5943. '']));
  5944. end;
  5945. procedure TTestModule.TestEnum_Name;
  5946. begin
  5947. StartProgram(false);
  5948. Add('type TMyEnum = (Red, Green, Blue);');
  5949. Add('var e: TMyEnum;');
  5950. Add('var f: TMyEnum = Blue;');
  5951. Add('begin');
  5952. Add(' e:=green;');
  5953. Add(' e:=default(TMyEnum);');
  5954. ConvertProgram;
  5955. CheckSource('TestEnum_Name',
  5956. LinesToStr([ // statements
  5957. 'this.TMyEnum = {',
  5958. ' "0":"Red",',
  5959. ' Red:0,',
  5960. ' "1":"Green",',
  5961. ' Green:1,',
  5962. ' "2":"Blue",',
  5963. ' Blue:2',
  5964. ' };',
  5965. 'this.e = 0;',
  5966. 'this.f = this.TMyEnum.Blue;'
  5967. ]),
  5968. LinesToStr([
  5969. '$mod.e=$mod.TMyEnum.Green;',
  5970. '$mod.e=$mod.TMyEnum.Red;'
  5971. ]));
  5972. end;
  5973. procedure TTestModule.TestEnum_Number;
  5974. begin
  5975. Converter.Options:=Converter.Options+[coEnumNumbers];
  5976. StartProgram(false);
  5977. Add('type TMyEnum = (Red, Green);');
  5978. Add('var');
  5979. Add(' e: TMyEnum;');
  5980. Add(' f: TMyEnum = Green;');
  5981. Add(' i: longint;');
  5982. Add('begin');
  5983. Add(' e:=green;');
  5984. Add(' i:=longint(e);');
  5985. ConvertProgram;
  5986. CheckSource('TestEnumNumber',
  5987. LinesToStr([ // statements
  5988. 'this.TMyEnum = {',
  5989. ' "0":"Red",',
  5990. ' Red:0,',
  5991. ' "1":"Green",',
  5992. ' Green:1',
  5993. ' };',
  5994. 'this.e = 0;',
  5995. 'this.f = 1;',
  5996. 'this.i = 0;'
  5997. ]),
  5998. LinesToStr([
  5999. '$mod.e=1;',
  6000. '$mod.i=$mod.e;'
  6001. ]));
  6002. end;
  6003. procedure TTestModule.TestEnum_ConstFail;
  6004. begin
  6005. StartProgram(false);
  6006. Add([
  6007. 'type TMyEnum = (Red = 100, Green = 101);',
  6008. 'var',
  6009. ' e: TMyEnum;',
  6010. ' f: TMyEnum = Green;',
  6011. 'begin',
  6012. ' e:=green;']);
  6013. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  6014. ConvertProgram;
  6015. end;
  6016. procedure TTestModule.TestEnum_Functions;
  6017. begin
  6018. StartProgram(false);
  6019. Add([
  6020. 'type TMyEnum = (Red, Green);',
  6021. 'procedure DoIt(var e: TMyEnum; var i: word);',
  6022. 'var',
  6023. ' v: longint;',
  6024. ' s: string;',
  6025. 'begin',
  6026. ' val(s,e,v);',
  6027. ' val(s,e,i);',
  6028. 'end;',
  6029. 'var',
  6030. ' e: TMyEnum;',
  6031. ' i: longint;',
  6032. ' s: string;',
  6033. ' b: boolean;',
  6034. 'begin',
  6035. ' i:=ord(red);',
  6036. ' i:=ord(green);',
  6037. ' i:=ord(e);',
  6038. ' i:=ord(b);',
  6039. ' e:=low(tmyenum);',
  6040. ' e:=low(e);',
  6041. ' b:=low(boolean);',
  6042. ' e:=high(tmyenum);',
  6043. ' e:=high(e);',
  6044. ' b:=high(boolean);',
  6045. ' e:=pred(green);',
  6046. ' e:=pred(e);',
  6047. ' b:=pred(b);',
  6048. ' e:=succ(red);',
  6049. ' e:=succ(e);',
  6050. ' b:=succ(b);',
  6051. ' e:=tmyenum(1);',
  6052. ' e:=tmyenum(i);',
  6053. ' s:=str(e);',
  6054. ' str(e,s);',
  6055. ' str(red,s);',
  6056. ' s:=str(e:3);',
  6057. ' writestr(s,e:3,red);',
  6058. ' val(s,e,i);',
  6059. ' i:=longint(e);']);
  6060. ConvertProgram;
  6061. CheckSource('TestEnum_Functions',
  6062. LinesToStr([ // statements
  6063. 'this.TMyEnum = {',
  6064. ' "0":"Red",',
  6065. ' Red:0,',
  6066. ' "1":"Green",',
  6067. ' Green:1',
  6068. ' };',
  6069. 'this.DoIt = function (e, i) {',
  6070. ' var v = 0;',
  6071. ' var s = "";',
  6072. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  6073. ' v = w;',
  6074. ' }));',
  6075. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  6076. '};',
  6077. 'this.e = 0;',
  6078. 'this.i = 0;',
  6079. 'this.s = "";',
  6080. 'this.b = false;',
  6081. '']),
  6082. LinesToStr([
  6083. '$mod.i=$mod.TMyEnum.Red;',
  6084. '$mod.i=$mod.TMyEnum.Green;',
  6085. '$mod.i=$mod.e;',
  6086. '$mod.i=$mod.b+0;',
  6087. '$mod.e=$mod.TMyEnum.Red;',
  6088. '$mod.e=$mod.TMyEnum.Red;',
  6089. '$mod.b=false;',
  6090. '$mod.e=$mod.TMyEnum.Green;',
  6091. '$mod.e=$mod.TMyEnum.Green;',
  6092. '$mod.b=true;',
  6093. '$mod.e=$mod.TMyEnum.Green-1;',
  6094. '$mod.e=$mod.e-1;',
  6095. '$mod.b=false;',
  6096. '$mod.e=$mod.TMyEnum.Red+1;',
  6097. '$mod.e=$mod.e+1;',
  6098. '$mod.b=true;',
  6099. '$mod.e=1;',
  6100. '$mod.e=$mod.i;',
  6101. '$mod.s = $mod.TMyEnum[$mod.e];',
  6102. '$mod.s = $mod.TMyEnum[$mod.e];',
  6103. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  6104. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  6105. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  6106. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  6107. ' $mod.i = v;',
  6108. '});',
  6109. '$mod.i=$mod.e;',
  6110. '']));
  6111. end;
  6112. procedure TTestModule.TestEnumRg_Functions;
  6113. begin
  6114. StartProgram(false);
  6115. Add([
  6116. 'type',
  6117. ' TEnum = (Red, Green, Blue);',
  6118. ' TEnumRg = Green..Blue;',
  6119. 'procedure DoIt(var e: TEnumRg; var i: word);',
  6120. 'var',
  6121. ' v: longint;',
  6122. ' s: string;',
  6123. 'begin',
  6124. ' val(s,e,v);',
  6125. ' val(s,e,i);',
  6126. 'end;',
  6127. 'var',
  6128. ' e: TEnumRg;',
  6129. ' i: longint;',
  6130. ' s: string;',
  6131. 'begin',
  6132. ' i:=ord(green);',
  6133. ' i:=ord(e);',
  6134. ' e:=low(tenumrg);',
  6135. ' e:=low(e);',
  6136. ' e:=high(tenumrg);',
  6137. ' e:=high(e);',
  6138. ' e:=pred(blue);',
  6139. ' e:=pred(e);',
  6140. ' e:=succ(green);',
  6141. ' e:=succ(e);',
  6142. ' e:=tenumrg(1);',
  6143. ' e:=tenumrg(i);',
  6144. ' s:=str(e);',
  6145. ' str(e,s);',
  6146. ' str(red,s);',
  6147. ' s:=str(e:3);',
  6148. ' writestr(s,e:3,blue);',
  6149. ' val(s,e,i);',
  6150. ' i:=longint(e);']);
  6151. ConvertProgram;
  6152. CheckSource('TestEnumRg_Functions',
  6153. LinesToStr([ // statements
  6154. 'this.TEnum = {',
  6155. ' "0":"Red",',
  6156. ' Red:0,',
  6157. ' "1":"Green",',
  6158. ' Green:1,',
  6159. ' "2":"Blue",',
  6160. ' Blue:2',
  6161. ' };',
  6162. 'this.DoIt = function (e, i) {',
  6163. ' var v = 0;',
  6164. ' var s = "";',
  6165. ' e.set(rtl.valEnum(s, $mod.TEnum, function (w) {',
  6166. ' v = w;',
  6167. ' }));',
  6168. ' e.set(rtl.valEnum(s, $mod.TEnum, i.set));',
  6169. '};',
  6170. 'this.e = this.TEnum.Green;',
  6171. 'this.i = 0;',
  6172. 'this.s = "";',
  6173. '']),
  6174. LinesToStr([
  6175. '$mod.i=$mod.TEnum.Green;',
  6176. '$mod.i=$mod.e;',
  6177. '$mod.e=$mod.TEnum.Green;',
  6178. '$mod.e=$mod.TEnum.Green;',
  6179. '$mod.e=$mod.TEnum.Blue;',
  6180. '$mod.e=$mod.TEnum.Blue;',
  6181. '$mod.e=$mod.TEnum.Blue-1;',
  6182. '$mod.e=$mod.e-1;',
  6183. '$mod.e=$mod.TEnum.Green+1;',
  6184. '$mod.e=$mod.e+1;',
  6185. '$mod.e=1;',
  6186. '$mod.e=$mod.i;',
  6187. '$mod.s = $mod.TEnum[$mod.e];',
  6188. '$mod.s = $mod.TEnum[$mod.e];',
  6189. '$mod.s = $mod.TEnum[$mod.TEnum.Red];',
  6190. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3);',
  6191. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3)+$mod.TEnum[$mod.TEnum.Blue];',
  6192. '$mod.e = rtl.valEnum($mod.s, $mod.TEnum, function (v) {',
  6193. ' $mod.i = v;',
  6194. '});',
  6195. '$mod.i=$mod.e;',
  6196. '']));
  6197. end;
  6198. procedure TTestModule.TestEnum_AsParams;
  6199. begin
  6200. StartProgram(false);
  6201. Add('type TEnum = (Red,Blue);');
  6202. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  6203. Add('var vJ: TEnum;');
  6204. Add('begin');
  6205. Add(' vg:=vg;');
  6206. Add(' vj:=vh;');
  6207. Add(' vi:=vi;');
  6208. Add(' doit(vg,vg,vg);');
  6209. Add(' doit(vh,vh,vj);');
  6210. Add(' doit(vi,vi,vi);');
  6211. Add(' doit(vj,vj,vj);');
  6212. Add('end;');
  6213. Add('var i: TEnum;');
  6214. Add('begin');
  6215. Add(' doit(i,i,i);');
  6216. ConvertProgram;
  6217. CheckSource('TestEnum_AsParams',
  6218. LinesToStr([ // statements
  6219. 'this.TEnum = {',
  6220. ' "0": "Red",',
  6221. ' Red: 0,',
  6222. ' "1": "Blue",',
  6223. ' Blue: 1',
  6224. '};',
  6225. 'this.DoIt = function (vG,vH,vI) {',
  6226. ' var vJ = 0;',
  6227. ' vG = vG;',
  6228. ' vJ = vH;',
  6229. ' vI.set(vI.get());',
  6230. ' $mod.DoIt(vG, vG, {',
  6231. ' get: function () {',
  6232. ' return vG;',
  6233. ' },',
  6234. ' set: function (v) {',
  6235. ' vG = v;',
  6236. ' }',
  6237. ' });',
  6238. ' $mod.DoIt(vH, vH, {',
  6239. ' get: function () {',
  6240. ' return vJ;',
  6241. ' },',
  6242. ' set: function (v) {',
  6243. ' vJ = v;',
  6244. ' }',
  6245. ' });',
  6246. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  6247. ' $mod.DoIt(vJ, vJ, {',
  6248. ' get: function () {',
  6249. ' return vJ;',
  6250. ' },',
  6251. ' set: function (v) {',
  6252. ' vJ = v;',
  6253. ' }',
  6254. ' });',
  6255. '};',
  6256. 'this.i = 0;'
  6257. ]),
  6258. LinesToStr([
  6259. '$mod.DoIt($mod.i,$mod.i,{',
  6260. ' p: $mod,',
  6261. ' get: function () {',
  6262. ' return this.p.i;',
  6263. ' },',
  6264. ' set: function (v) {',
  6265. ' this.p.i = v;',
  6266. ' }',
  6267. '});'
  6268. ]));
  6269. end;
  6270. procedure TTestModule.TestEnumRange_Array;
  6271. begin
  6272. StartProgram(false);
  6273. Add([
  6274. 'type',
  6275. ' TEnum = (Red, Green, Blue);',
  6276. ' TEnumRg = green..blue;',
  6277. ' TArr = array[TEnumRg] of byte;',
  6278. ' TArr2 = array[green..blue] of byte;',
  6279. 'var',
  6280. ' a: TArr;',
  6281. ' b: TArr = (3,4);',
  6282. ' c: TArr2 = (5,6);',
  6283. 'begin',
  6284. ' a[green] := b[blue];',
  6285. ' c[green] := c[blue];',
  6286. '']);
  6287. ConvertProgram;
  6288. CheckSource('TestEnumRange_Array',
  6289. LinesToStr([ // statements
  6290. 'this.TEnum = {',
  6291. ' "0": "Red",',
  6292. ' Red: 0,',
  6293. ' "1": "Green",',
  6294. ' Green: 1,',
  6295. ' "2": "Blue",',
  6296. ' Blue: 2',
  6297. '};',
  6298. 'this.a = rtl.arraySetLength(null, 0, 2);',
  6299. 'this.b = [3, 4];',
  6300. 'this.c = [5, 6];',
  6301. '']),
  6302. LinesToStr([
  6303. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  6304. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  6305. '']));
  6306. end;
  6307. procedure TTestModule.TestEnum_ForIn;
  6308. begin
  6309. StartProgram(false);
  6310. Add([
  6311. 'type',
  6312. ' TEnum = (Red, Green, Blue);',
  6313. ' TEnumRg = green..blue;',
  6314. ' TArr = array[TEnum] of byte;',
  6315. ' TArrRg = array[TEnumRg] of byte;',
  6316. 'var',
  6317. ' e: TEnum;',
  6318. ' a1: TArr = (3,4,5);',
  6319. ' a2: TArrRg = (11,12);',
  6320. ' b: byte;',
  6321. 'begin',
  6322. ' for e in TEnum do ;',
  6323. ' for e in TEnumRg do ;',
  6324. ' for e in TArr do ;',
  6325. ' for e in TArrRg do ;',
  6326. ' for b in a1 do ;',
  6327. ' for b in a2 do ;',
  6328. '']);
  6329. ConvertProgram;
  6330. CheckSource('TestEnum_ForIn',
  6331. LinesToStr([ // statements
  6332. 'this.TEnum = {',
  6333. ' "0": "Red",',
  6334. ' Red: 0,',
  6335. ' "1": "Green",',
  6336. ' Green: 1,',
  6337. ' "2": "Blue",',
  6338. ' Blue: 2',
  6339. '};',
  6340. 'this.e = 0;',
  6341. 'this.a1 = [3, 4, 5];',
  6342. 'this.a2 = [11, 12];',
  6343. 'this.b = 0;',
  6344. '']),
  6345. LinesToStr([
  6346. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6347. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6348. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6349. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6350. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  6351. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  6352. '']));
  6353. end;
  6354. procedure TTestModule.TestEnum_ScopedNumber;
  6355. begin
  6356. Converter.Options:=Converter.Options+[coEnumNumbers];
  6357. StartProgram(false);
  6358. Add([
  6359. 'type',
  6360. ' TEnum = (Red, Green);',
  6361. 'var',
  6362. ' e: TEnum;',
  6363. 'begin',
  6364. ' e:=TEnum.Green;',
  6365. '']);
  6366. ConvertProgram;
  6367. CheckSource('TestEnum_ScopedNumber',
  6368. LinesToStr([ // statements
  6369. 'this.TEnum = {',
  6370. ' "0": "Red",',
  6371. ' Red: 0,',
  6372. ' "1": "Green",',
  6373. ' Green: 1',
  6374. '};',
  6375. 'this.e = 0;',
  6376. '']),
  6377. LinesToStr([
  6378. '$mod.e = 1;']));
  6379. end;
  6380. procedure TTestModule.TestEnum_InFunction;
  6381. begin
  6382. StartProgram(false);
  6383. Add([
  6384. 'const TEnum = 3;',
  6385. 'procedure DoIt;',
  6386. 'type',
  6387. ' TEnum = (Red, Green, Blue);',
  6388. ' procedure Sub;',
  6389. ' type',
  6390. ' TEnumSub = (Left, Right);',
  6391. ' var',
  6392. ' es: TEnumSub;',
  6393. ' begin',
  6394. ' es:=Left;',
  6395. ' end;',
  6396. 'var',
  6397. ' e, e2: TEnum;',
  6398. 'begin',
  6399. ' if e in [red,blue] then e2:=e;',
  6400. 'end;',
  6401. 'begin']);
  6402. ConvertProgram;
  6403. CheckSource('TestEnum_InFunction',
  6404. LinesToStr([ // statements
  6405. 'this.TEnum = 3;',
  6406. 'var TEnum$1 = {',
  6407. ' "0":"Red",',
  6408. ' Red:0,',
  6409. ' "1":"Green",',
  6410. ' Green:1,',
  6411. ' "2":"Blue",',
  6412. ' Blue:2',
  6413. ' };',
  6414. 'var TEnumSub = {',
  6415. ' "0": "Left",',
  6416. ' Left: 0,',
  6417. ' "1": "Right",',
  6418. ' Right: 1',
  6419. '};',
  6420. 'this.DoIt = function () {',
  6421. ' function Sub() {',
  6422. ' var es = 0;',
  6423. ' es = TEnumSub.Left;',
  6424. ' };',
  6425. ' var e = 0;',
  6426. ' var e2 = 0;',
  6427. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  6428. '};',
  6429. '']),
  6430. LinesToStr([
  6431. '']));
  6432. end;
  6433. procedure TTestModule.TestEnum_Name_Anonymous_Unit;
  6434. begin
  6435. StartUnit(true);
  6436. Add([
  6437. 'interface',
  6438. 'var color: (red, green);',
  6439. 'implementation',
  6440. 'initialization',
  6441. ' color:=green;',
  6442. '']);
  6443. ConvertUnit;
  6444. CheckSource('TestEnum_Name_Anonymous_Unit',
  6445. LinesToStr([
  6446. 'this.color$a = {',
  6447. ' "0": "red",',
  6448. ' red: 0,',
  6449. ' "1": "green",',
  6450. ' green: 1',
  6451. '};',
  6452. 'this.color = 0;',
  6453. '']),
  6454. LinesToStr([ // this.$init
  6455. '$mod.color = $mod.color$a.green;',
  6456. '']),
  6457. LinesToStr([ // implementation
  6458. '']) );
  6459. end;
  6460. procedure TTestModule.TestSet_Enum;
  6461. begin
  6462. StartProgram(false);
  6463. Add([
  6464. 'type',
  6465. ' TColor = (Red, Green, Blue);',
  6466. ' TColors = set of TColor;',
  6467. 'var',
  6468. ' c: TColor;',
  6469. ' s: TColors;',
  6470. ' t: TColors = [];',
  6471. ' u: TColors = [Red];',
  6472. 'begin',
  6473. ' s:=[];',
  6474. ' s:=[Green];',
  6475. ' s:=[Green,Blue];',
  6476. ' s:=[Red..Blue];',
  6477. ' s:=[Red,Green..Blue];',
  6478. ' s:=[Red,c];',
  6479. ' s:=t;',
  6480. ' s:=default(TColors);',
  6481. '']);
  6482. ConvertProgram;
  6483. CheckSource('TestSet',
  6484. LinesToStr([ // statements
  6485. 'this.TColor = {',
  6486. ' "0":"Red",',
  6487. ' Red:0,',
  6488. ' "1":"Green",',
  6489. ' Green:1,',
  6490. ' "2":"Blue",',
  6491. ' Blue:2',
  6492. ' };',
  6493. 'this.c = 0;',
  6494. 'this.s = {};',
  6495. 'this.t = {};',
  6496. 'this.u = rtl.createSet(this.TColor.Red);'
  6497. ]),
  6498. LinesToStr([
  6499. '$mod.s={};',
  6500. '$mod.s=rtl.createSet($mod.TColor.Green);',
  6501. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  6502. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  6503. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  6504. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  6505. '$mod.s=rtl.refSet($mod.t);',
  6506. '$mod.s={};',
  6507. '']));
  6508. end;
  6509. procedure TTestModule.TestSet_Operators;
  6510. begin
  6511. StartProgram(false);
  6512. Add('type');
  6513. Add(' TColor = (Red, Green, Blue);');
  6514. Add(' TColors = set of tcolor;');
  6515. Add('var');
  6516. Add(' vC: TColor;');
  6517. Add(' vS: TColors;');
  6518. Add(' vT: TColors;');
  6519. Add(' vU: TColors;');
  6520. Add(' B: boolean;');
  6521. Add('begin');
  6522. Add(' include(vs,green);');
  6523. Add(' exclude(vs,vc);');
  6524. Add(' vs:=vt+vu;');
  6525. Add(' vs:=vt+[red];');
  6526. Add(' vs:=[red]+vt;');
  6527. Add(' vs:=[red]+[green];');
  6528. Add(' vs:=vt-vu;');
  6529. Add(' vs:=vt-[red];');
  6530. Add(' vs:=[red]-vt;');
  6531. Add(' vs:=[red]-[green];');
  6532. Add(' vs:=vt*vu;');
  6533. Add(' vs:=vt*[red];');
  6534. Add(' vs:=[red]*vt;');
  6535. Add(' vs:=[red]*[green];');
  6536. Add(' vs:=vt><vu;');
  6537. Add(' vs:=vt><[red];');
  6538. Add(' vs:=[red]><vt;');
  6539. Add(' vs:=[red]><[green];');
  6540. Add(' b:=vt=vu;');
  6541. Add(' b:=vt=[red];');
  6542. Add(' b:=[red]=vt;');
  6543. Add(' b:=[red]=[green];');
  6544. Add(' b:=vt<>vu;');
  6545. Add(' b:=vt<>[red];');
  6546. Add(' b:=[red]<>vt;');
  6547. Add(' b:=[red]<>[green];');
  6548. Add(' b:=vt<=vu;');
  6549. Add(' b:=vt<=[red];');
  6550. Add(' b:=[red]<=vt;');
  6551. Add(' b:=[red]<=[green];');
  6552. Add(' b:=vt>=vu;');
  6553. Add(' b:=vt>=[red];');
  6554. Add(' b:=[red]>=vt;');
  6555. Add(' b:=[red]>=[green];');
  6556. ConvertProgram;
  6557. CheckSource('TestSet_Operators',
  6558. LinesToStr([ // statements
  6559. 'this.TColor = {',
  6560. ' "0":"Red",',
  6561. ' Red:0,',
  6562. ' "1":"Green",',
  6563. ' Green:1,',
  6564. ' "2":"Blue",',
  6565. ' Blue:2',
  6566. ' };',
  6567. 'this.vC = 0;',
  6568. 'this.vS = {};',
  6569. 'this.vT = {};',
  6570. 'this.vU = {};',
  6571. 'this.B = false;'
  6572. ]),
  6573. LinesToStr([
  6574. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  6575. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  6576. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  6577. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6578. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6579. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6580. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  6581. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6582. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6583. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6584. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  6585. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6586. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6587. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6588. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  6589. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6590. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6591. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6592. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  6593. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6594. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6595. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6596. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  6597. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6598. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6599. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6600. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  6601. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6602. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6603. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6604. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  6605. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6606. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6607. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6608. '']));
  6609. end;
  6610. procedure TTestModule.TestSet_Operator_In;
  6611. begin
  6612. StartProgram(false);
  6613. Add([
  6614. 'type',
  6615. ' TColor = (Red, Green, Blue);',
  6616. ' TColors = set of tcolor;',
  6617. ' TColorRg = green..blue;',
  6618. 'var',
  6619. ' vC: tcolor;',
  6620. ' vT: tcolors;',
  6621. ' B: boolean;',
  6622. ' rg: TColorRg;',
  6623. 'begin',
  6624. ' b:=red in vt;',
  6625. ' b:=vc in vt;',
  6626. ' b:=green in [red..blue];',
  6627. ' b:=vc in [red..blue];',
  6628. ' ',
  6629. ' if red in vt then ;',
  6630. ' while vC in vt do ;',
  6631. ' repeat',
  6632. ' until vC in vt;',
  6633. ' if rg in [green..blue] then ;',
  6634. '']);
  6635. ConvertProgram;
  6636. CheckSource('TestSet_Operator_In',
  6637. LinesToStr([ // statements
  6638. 'this.TColor = {',
  6639. ' "0":"Red",',
  6640. ' Red:0,',
  6641. ' "1":"Green",',
  6642. ' Green:1,',
  6643. ' "2":"Blue",',
  6644. ' Blue:2',
  6645. ' };',
  6646. 'this.vC = 0;',
  6647. 'this.vT = {};',
  6648. 'this.B = false;',
  6649. 'this.rg = this.TColor.Green;',
  6650. '']),
  6651. LinesToStr([
  6652. '$mod.B = $mod.TColor.Red in $mod.vT;',
  6653. '$mod.B = $mod.vC in $mod.vT;',
  6654. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  6655. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  6656. 'if ($mod.TColor.Red in $mod.vT) ;',
  6657. 'while ($mod.vC in $mod.vT) {',
  6658. '};',
  6659. 'do {',
  6660. '} while (!($mod.vC in $mod.vT));',
  6661. 'if ($mod.rg in rtl.createSet(null, $mod.TColor.Green, $mod.TColor.Blue)) ;',
  6662. '']));
  6663. end;
  6664. procedure TTestModule.TestSet_Functions;
  6665. begin
  6666. StartProgram(false);
  6667. Add('type');
  6668. Add(' TMyEnum = (Red, Green);');
  6669. Add(' TMyEnums = set of TMyEnum;');
  6670. Add('var');
  6671. Add(' e: TMyEnum;');
  6672. Add(' s: TMyEnums;');
  6673. Add('begin');
  6674. Add(' e:=Low(TMyEnums);');
  6675. Add(' e:=Low(s);');
  6676. Add(' e:=High(TMyEnums);');
  6677. Add(' e:=High(s);');
  6678. ConvertProgram;
  6679. CheckSource('TestSetFunctions',
  6680. LinesToStr([ // statements
  6681. 'this.TMyEnum = {',
  6682. ' "0":"Red",',
  6683. ' Red:0,',
  6684. ' "1":"Green",',
  6685. ' Green:1',
  6686. ' };',
  6687. 'this.e = 0;',
  6688. 'this.s = {};'
  6689. ]),
  6690. LinesToStr([
  6691. '$mod.e=$mod.TMyEnum.Red;',
  6692. '$mod.e=$mod.TMyEnum.Red;',
  6693. '$mod.e=$mod.TMyEnum.Green;',
  6694. '$mod.e=$mod.TMyEnum.Green;',
  6695. '']));
  6696. end;
  6697. procedure TTestModule.TestSet_PassAsArgClone;
  6698. begin
  6699. StartProgram(false);
  6700. Add('type');
  6701. Add(' TMyEnum = (Red, Green);');
  6702. Add(' TMyEnums = set of TMyEnum;');
  6703. Add('procedure DoDefault(s: tmyenums); begin end;');
  6704. Add('procedure DoConst(const s: tmyenums); begin end;');
  6705. Add('var');
  6706. Add(' aSet: tmyenums;');
  6707. Add('begin');
  6708. Add(' dodefault(aset);');
  6709. Add(' doconst(aset);');
  6710. ConvertProgram;
  6711. CheckSource('TestSetFunctions',
  6712. LinesToStr([ // statements
  6713. 'this.TMyEnum = {',
  6714. ' "0":"Red",',
  6715. ' Red:0,',
  6716. ' "1":"Green",',
  6717. ' Green:1',
  6718. ' };',
  6719. 'this.DoDefault = function (s) {',
  6720. '};',
  6721. 'this.DoConst = function (s) {',
  6722. '};',
  6723. 'this.aSet = {};'
  6724. ]),
  6725. LinesToStr([
  6726. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  6727. '$mod.DoConst($mod.aSet);',
  6728. '']));
  6729. end;
  6730. procedure TTestModule.TestSet_AsParams;
  6731. begin
  6732. StartProgram(false);
  6733. Add([
  6734. 'type TEnum = (Red,Blue);',
  6735. 'type TEnums = set of TEnum;',
  6736. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  6737. 'var vJ: TEnums;',
  6738. 'begin',
  6739. ' Include(vg,red);',
  6740. ' Include(result,blue);',
  6741. ' vg:=vg;',
  6742. ' vj:=vh;',
  6743. ' vi:=vi;',
  6744. ' doit(vg,vg,vg);',
  6745. ' doit(vh,vh,vj);',
  6746. ' doit(vi,vi,vi);',
  6747. ' doit(vj,vj,vj);',
  6748. 'end;',
  6749. 'var i: TEnums;',
  6750. 'begin',
  6751. ' doit(i,i,i);']);
  6752. ConvertProgram;
  6753. CheckSource('TestSet_AsParams',
  6754. LinesToStr([ // statements
  6755. 'this.TEnum = {',
  6756. ' "0": "Red",',
  6757. ' Red: 0,',
  6758. ' "1": "Blue",',
  6759. ' Blue: 1',
  6760. '};',
  6761. 'this.DoIt = function (vG,vH,vI) {',
  6762. ' var Result = {};',
  6763. ' var vJ = {};',
  6764. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  6765. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  6766. ' vG = rtl.refSet(vG);',
  6767. ' vJ = rtl.refSet(vH);',
  6768. ' vI.set(rtl.refSet(vI.get()));',
  6769. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  6770. ' get: function () {',
  6771. ' return vG;',
  6772. ' },',
  6773. ' set: function (v) {',
  6774. ' vG = v;',
  6775. ' }',
  6776. ' });',
  6777. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  6778. ' get: function () {',
  6779. ' return vJ;',
  6780. ' },',
  6781. ' set: function (v) {',
  6782. ' vJ = v;',
  6783. ' }',
  6784. ' });',
  6785. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  6786. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  6787. ' get: function () {',
  6788. ' return vJ;',
  6789. ' },',
  6790. ' set: function (v) {',
  6791. ' vJ = v;',
  6792. ' }',
  6793. ' });',
  6794. ' return Result;',
  6795. '};',
  6796. 'this.i = {};'
  6797. ]),
  6798. LinesToStr([
  6799. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  6800. ' p: $mod,',
  6801. ' get: function () {',
  6802. ' return this.p.i;',
  6803. ' },',
  6804. ' set: function (v) {',
  6805. ' this.p.i = v;',
  6806. ' }',
  6807. '});'
  6808. ]));
  6809. end;
  6810. procedure TTestModule.TestSet_Property;
  6811. begin
  6812. StartProgram(false);
  6813. Add('type');
  6814. Add(' TEnum = (Red,Blue);');
  6815. Add(' TEnums = set of TEnum;');
  6816. Add(' TObject = class');
  6817. Add(' function GetColors: TEnums; external name ''GetColors'';');
  6818. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  6819. Add(' property Colors: TEnums read GetColors write SetColors;');
  6820. Add(' end;');
  6821. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  6822. Add('begin end;');
  6823. Add('var Obj: TObject;');
  6824. Add('begin');
  6825. Add(' Include(Obj.Colors,Red);');
  6826. Add(' Exclude(Obj.Colors,Red);');
  6827. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  6828. ConvertProgram;
  6829. CheckSource('TestSet_Property',
  6830. LinesToStr([ // statements
  6831. 'this.TEnum = {',
  6832. ' "0": "Red",',
  6833. ' Red: 0,',
  6834. ' "1": "Blue",',
  6835. ' Blue: 1',
  6836. '};',
  6837. 'rtl.createClass(this, "TObject", null, function () {',
  6838. ' this.$init = function () {',
  6839. ' };',
  6840. ' this.$final = function () {',
  6841. ' };',
  6842. '});',
  6843. 'this.DoIt = function (i, j, k, l) {',
  6844. '};',
  6845. 'this.Obj = null;',
  6846. '']),
  6847. LinesToStr([
  6848. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6849. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6850. '']));
  6851. end;
  6852. procedure TTestModule.TestSet_EnumConst;
  6853. begin
  6854. StartProgram(false);
  6855. Add([
  6856. 'type',
  6857. ' TEnum = (Red,Blue);',
  6858. ' TEnums = set of TEnum;',
  6859. 'const',
  6860. ' Orange = red;',
  6861. 'var',
  6862. ' Enum: tenum;',
  6863. ' Enums: tenums;',
  6864. 'begin',
  6865. ' Include(enums,orange);',
  6866. ' Exclude(enums,orange);',
  6867. ' if orange in enums then;',
  6868. ' if orange in [orange,red] then;']);
  6869. ConvertProgram;
  6870. CheckSource('TestSet_EnumConst',
  6871. LinesToStr([ // statements
  6872. 'this.TEnum = {',
  6873. ' "0": "Red",',
  6874. ' Red: 0,',
  6875. ' "1": "Blue",',
  6876. ' Blue: 1',
  6877. '};',
  6878. 'this.Orange = this.TEnum.Red;',
  6879. 'this.Enum = 0;',
  6880. 'this.Enums = {};',
  6881. '']),
  6882. LinesToStr([
  6883. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  6884. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  6885. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  6886. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  6887. '']));
  6888. end;
  6889. procedure TTestModule.TestSet_IntConst;
  6890. begin
  6891. StartProgram(false);
  6892. Add([
  6893. 'type',
  6894. ' TEnums = set of Byte;',
  6895. 'const',
  6896. ' Orange = 0;',
  6897. 'var',
  6898. ' Enum: byte;',
  6899. ' Enums: tenums;',
  6900. 'begin',
  6901. ' Enum:=ord(1);',
  6902. ' Enums:=[];',
  6903. ' Enums:=[0];',
  6904. ' Enums:=[1..2];',
  6905. //' Include(enums,orange);',
  6906. //' Exclude(enums,orange);',
  6907. ' if orange in enums then;',
  6908. ' if orange in [orange,1] then;']);
  6909. ConvertProgram;
  6910. CheckSource('TestSet_IntConst',
  6911. LinesToStr([ // statements
  6912. 'this.Orange = 0;',
  6913. 'this.Enum = 0;',
  6914. 'this.Enums = {};',
  6915. '']),
  6916. LinesToStr([
  6917. '$mod.Enum = 1;',
  6918. '$mod.Enums = {};',
  6919. '$mod.Enums = rtl.createSet(0);',
  6920. '$mod.Enums = rtl.createSet(null, 1, 2);',
  6921. 'if (0 in $mod.Enums) ;',
  6922. 'if (0 in rtl.createSet(0, 1)) ;',
  6923. '']));
  6924. end;
  6925. procedure TTestModule.TestSet_IntRange;
  6926. begin
  6927. StartProgram(false);
  6928. Add([
  6929. 'type',
  6930. ' TRange = 1..3;',
  6931. ' TEnums = set of TRange;',
  6932. 'const',
  6933. ' Orange = 2;',
  6934. 'var',
  6935. ' Enum: byte;',
  6936. ' Enums: TEnums;',
  6937. 'begin',
  6938. ' Enums:=[];',
  6939. ' Enums:=[1];',
  6940. ' Enums:=[2..3];',
  6941. ' Include(enums,orange);',
  6942. ' Exclude(enums,orange);',
  6943. ' if orange in enums then;',
  6944. ' if orange in [orange,1] then;']);
  6945. ConvertProgram;
  6946. CheckSource('TestSet_IntRange',
  6947. LinesToStr([ // statements
  6948. 'this.Orange = 2;',
  6949. 'this.Enum = 0;',
  6950. 'this.Enums = {};',
  6951. '']),
  6952. LinesToStr([
  6953. '$mod.Enums = {};',
  6954. '$mod.Enums = rtl.createSet(1);',
  6955. '$mod.Enums = rtl.createSet(null, 2, 3);',
  6956. '$mod.Enums = rtl.includeSet($mod.Enums, 2);',
  6957. '$mod.Enums = rtl.excludeSet($mod.Enums, 2);',
  6958. 'if (2 in $mod.Enums) ;',
  6959. 'if (2 in rtl.createSet(2, 1)) ;',
  6960. '']));
  6961. end;
  6962. procedure TTestModule.TestSet_AnonymousEnumType;
  6963. begin
  6964. StartProgram(false);
  6965. Add('type');
  6966. Add(' TFlags = set of (red, green);');
  6967. Add('const');
  6968. Add(' favorite = red;');
  6969. Add('var');
  6970. Add(' f: TFlags;');
  6971. Add(' i: longint;');
  6972. Add('begin');
  6973. Add(' Include(f,red);');
  6974. Add(' Include(f,favorite);');
  6975. Add(' i:=ord(red);');
  6976. Add(' i:=ord(favorite);');
  6977. Add(' i:=ord(low(TFlags));');
  6978. Add(' i:=ord(low(f));');
  6979. Add(' i:=ord(low(favorite));');
  6980. Add(' i:=ord(high(TFlags));');
  6981. Add(' i:=ord(high(f));');
  6982. Add(' i:=ord(high(favorite));');
  6983. Add(' f:=[green,favorite];');
  6984. ConvertProgram;
  6985. CheckSource('TestSet_AnonymousEnumType',
  6986. LinesToStr([ // statements
  6987. 'this.TFlags$a = {',
  6988. ' "0": "red",',
  6989. ' red: 0,',
  6990. ' "1": "green",',
  6991. ' green: 1',
  6992. '};',
  6993. 'this.favorite = this.TFlags$a.red;',
  6994. 'this.f = {};',
  6995. 'this.i = 0;',
  6996. '']),
  6997. LinesToStr([
  6998. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6999. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  7000. '$mod.i = $mod.TFlags$a.red;',
  7001. '$mod.i = $mod.TFlags$a.red;',
  7002. '$mod.i = $mod.TFlags$a.red;',
  7003. '$mod.i = $mod.TFlags$a.red;',
  7004. '$mod.i = $mod.TFlags$a.red;',
  7005. '$mod.i = $mod.TFlags$a.green;',
  7006. '$mod.i = $mod.TFlags$a.green;',
  7007. '$mod.i = $mod.TFlags$a.green;',
  7008. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  7009. '']));
  7010. end;
  7011. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  7012. begin
  7013. exit;
  7014. StartProgram(false);
  7015. Add([
  7016. 'type',
  7017. ' TAtoZ = ''A''..''Z'';',
  7018. ' TSetOfAZ = set of TAtoZ;',
  7019. 'var',
  7020. ' c: char;',
  7021. ' a: TAtoZ;',
  7022. ' s: TSetOfAZ = [''P'',''A''];',
  7023. ' i: longint;',
  7024. 'begin',
  7025. ' Include(s,''S'');',
  7026. ' Include(s,c);',
  7027. ' Include(s,a);',
  7028. ' c:=low(TAtoZ);',
  7029. ' i:=ord(low(TAtoZ));',
  7030. ' a:=high(TAtoZ);',
  7031. ' a:=high(TSetOfAtoZ);',
  7032. ' s:=[a,c,''M''];',
  7033. '']);
  7034. ConvertProgram;
  7035. CheckSource('TestSet_AnonymousEnumTypeChar',
  7036. LinesToStr([ // statements
  7037. '']),
  7038. LinesToStr([
  7039. '']));
  7040. end;
  7041. procedure TTestModule.TestSet_ConstEnum;
  7042. begin
  7043. StartProgram(false);
  7044. Add([
  7045. 'type',
  7046. ' TEnum = (red,blue,green);',
  7047. ' TEnums = set of TEnum;',
  7048. 'const',
  7049. ' teAny = [low(TEnum)..high(TEnum)];',
  7050. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  7051. 'var',
  7052. ' e: TEnum;',
  7053. ' s: TEnums;',
  7054. 'begin',
  7055. ' if blue in teAny then;',
  7056. ' if blue in teAny+[e] then;',
  7057. ' if blue in teAny+teRedBlue then;',
  7058. ' if e in [red,blue] then;',
  7059. ' s:=teAny;',
  7060. ' s:=teAny+[e];',
  7061. ' s:=[e]+teAny;',
  7062. ' s:=teAny+teRedBlue;',
  7063. ' s:=teAny+teRedBlue+[e];',
  7064. '']);
  7065. ConvertProgram;
  7066. CheckSource('TestSet_ConstEnum',
  7067. LinesToStr([ // statements
  7068. 'this.TEnum = {',
  7069. ' "0": "red",',
  7070. ' red: 0,',
  7071. ' "1": "blue",',
  7072. ' blue: 1,',
  7073. ' "2": "green",',
  7074. ' green: 2',
  7075. '};',
  7076. 'this.teAny = rtl.createSet(null, this.TEnum.red, this.TEnum.green);',
  7077. 'this.teRedBlue = rtl.createSet(null, this.TEnum.red, this.TEnum.green - 1);',
  7078. 'this.e = 0;',
  7079. 'this.s = {};',
  7080. '']),
  7081. LinesToStr([
  7082. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  7083. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  7084. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  7085. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  7086. '$mod.s = rtl.refSet($mod.teAny);',
  7087. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  7088. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  7089. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  7090. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  7091. '']));
  7092. end;
  7093. procedure TTestModule.TestSet_ConstChar;
  7094. begin
  7095. StartProgram(false);
  7096. Add([
  7097. 'const',
  7098. ' LowChars = [''a''..''z''];',
  7099. ' Chars = LowChars+[''A''..''Z''];',
  7100. ' sc = [''А'', ''Я''];',
  7101. 'var',
  7102. ' c: char;',
  7103. ' s: string;',
  7104. 'begin',
  7105. ' if c in lowchars then ;',
  7106. ' if ''a'' in lowchars then ;',
  7107. ' if s[1] in lowchars then ;',
  7108. ' if c in chars then ;',
  7109. ' if c in [''a''..''z'',''_''] then ;',
  7110. ' if ''b'' in [''a''..''z'',''_''] then ;',
  7111. ' if ''Я'' in sc then ;',
  7112. ' if 3=ord('' '') then ;',
  7113. '']);
  7114. ConvertProgram;
  7115. CheckSource('TestSet_ConstChar',
  7116. LinesToStr([ // statements
  7117. 'this.LowChars = rtl.createSet(null, 97, 122);',
  7118. 'this.Chars = rtl.unionSet(this.LowChars, rtl.createSet(null, 65, 90));',
  7119. 'this.sc = rtl.createSet(1040, 1071);',
  7120. 'this.c = "\x00";',
  7121. 'this.s = "";',
  7122. '']),
  7123. LinesToStr([
  7124. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  7125. 'if (97 in $mod.LowChars) ;',
  7126. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  7127. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  7128. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  7129. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  7130. 'if (1071 in $mod.sc) ;',
  7131. 'if (3 === 32) ;',
  7132. '']));
  7133. end;
  7134. procedure TTestModule.TestSet_ConstInt;
  7135. begin
  7136. StartProgram(false);
  7137. Add([
  7138. 'const',
  7139. ' Months = [1..12];',
  7140. ' Mirror = [-12..-1]+Months;',
  7141. 'var',
  7142. ' i: smallint;',
  7143. 'begin',
  7144. ' if 3 in Months then;',
  7145. ' if i in Months+[i] then;',
  7146. ' if i in Months+Mirror then;',
  7147. ' if i in [4..6,8] then;',
  7148. '']);
  7149. ConvertProgram;
  7150. CheckSource('TestSet_ConstInt',
  7151. LinesToStr([ // statements
  7152. 'this.Months = rtl.createSet(null, 1, 12);',
  7153. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), this.Months);',
  7154. 'this.i = 0;',
  7155. '']),
  7156. LinesToStr([
  7157. 'if (3 in $mod.Months) ;',
  7158. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  7159. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  7160. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  7161. '']));
  7162. end;
  7163. procedure TTestModule.TestSet_InFunction;
  7164. begin
  7165. StartProgram(false);
  7166. Add([
  7167. 'const',
  7168. ' TEnum = 3;',
  7169. ' TSetOfEnum = 4;',
  7170. ' TSetOfAno = 5;',
  7171. 'procedure DoIt;',
  7172. 'type',
  7173. ' TEnum = (red, blue);',
  7174. ' TSetOfEnum = set of TEnum;',
  7175. ' TSetOfAno = set of (up,down);',
  7176. 'var',
  7177. ' e: TEnum;',
  7178. ' se: TSetOfEnum;',
  7179. ' sa: TSetOfAno;',
  7180. 'begin',
  7181. ' se:=[e];',
  7182. ' sa:=[up];',
  7183. 'end;',
  7184. 'begin',
  7185. '']);
  7186. ConvertProgram;
  7187. CheckSource('TestSet_InFunction',
  7188. LinesToStr([ // statements
  7189. 'this.TEnum = 3;',
  7190. 'this.TSetOfEnum = 4;',
  7191. 'this.TSetOfAno = 5;',
  7192. 'var TEnum$1 = {',
  7193. ' "0": "red",',
  7194. ' red: 0,',
  7195. ' "1": "blue",',
  7196. ' blue: 1',
  7197. '};',
  7198. 'var TSetOfAno$a = {',
  7199. ' "0": "up",',
  7200. ' up: 0,',
  7201. ' "1": "down",',
  7202. ' down: 1',
  7203. '};',
  7204. 'this.DoIt = function () {',
  7205. ' var e = 0;',
  7206. ' var se = {};',
  7207. ' var sa = {};',
  7208. ' se = rtl.createSet(e);',
  7209. ' sa = rtl.createSet(TSetOfAno$a.up);',
  7210. '};',
  7211. '']),
  7212. LinesToStr([
  7213. '']));
  7214. end;
  7215. procedure TTestModule.TestSet_ForIn;
  7216. begin
  7217. StartProgram(false);
  7218. Add([
  7219. 'type',
  7220. ' TEnum = (Red, Green, Blue);',
  7221. ' TEnumRg = green..blue;',
  7222. ' TSetOfEnum = set of TEnum;',
  7223. ' TSetOfEnumRg = set of TEnumRg;',
  7224. 'var',
  7225. ' e, e2: TEnum;',
  7226. ' er: TEnum;',
  7227. ' s: TSetOfEnum;',
  7228. 'begin',
  7229. ' for e in TSetOfEnum do ;',
  7230. ' for e in TSetOfEnumRg do ;',
  7231. ' for e in [] do e2:=e;',
  7232. ' for e in [red..green] do e2:=e;',
  7233. ' for e in [green,blue] do e2:=e;',
  7234. ' for e in [red,blue] do e2:=e;',
  7235. ' for e in s do e2:=e;',
  7236. ' for er in TSetOfEnumRg do ;',
  7237. '']);
  7238. ConvertProgram;
  7239. CheckSource('TestSet_ForIn',
  7240. LinesToStr([ // statements
  7241. 'this.TEnum = {',
  7242. ' "0":"Red",',
  7243. ' Red:0,',
  7244. ' "1":"Green",',
  7245. ' Green:1,',
  7246. ' "2":"Blue",',
  7247. ' Blue:2',
  7248. ' };',
  7249. 'this.e = 0;',
  7250. 'this.e2 = 0;',
  7251. 'this.er = 0;',
  7252. 'this.s = {};',
  7253. '']),
  7254. LinesToStr([
  7255. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  7256. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  7257. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  7258. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  7259. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  7260. 'for (var $l in $mod.s){',
  7261. ' $mod.e = +$l;',
  7262. ' $mod.e2 = $mod.e;',
  7263. '};',
  7264. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  7265. '']));
  7266. end;
  7267. procedure TTestModule.TestNestBegin;
  7268. begin
  7269. StartProgram(false);
  7270. Add('begin');
  7271. Add(' begin');
  7272. Add(' begin');
  7273. Add(' end;');
  7274. Add(' begin');
  7275. Add(' if true then ;');
  7276. Add(' end;');
  7277. Add(' end;');
  7278. ConvertProgram;
  7279. CheckSource('TestNestBegin',
  7280. '',
  7281. 'if (true) ;');
  7282. end;
  7283. procedure TTestModule.TestUnitImplVars;
  7284. begin
  7285. StartUnit(false);
  7286. Add('interface');
  7287. Add('implementation');
  7288. Add('var');
  7289. Add(' V1:longint;');
  7290. Add(' V2:longint = 3;');
  7291. Add(' V3:string = ''abc'';');
  7292. ConvertUnit;
  7293. CheckSource('TestUnitImplVars',
  7294. LinesToStr([ // statements
  7295. 'var $impl = $mod.$impl;',
  7296. '']),
  7297. '', // this.$init
  7298. LinesToStr([ // implementation
  7299. '$impl.V1 = 0;',
  7300. '$impl.V2 = 3;',
  7301. '$impl.V3 = "abc";',
  7302. '']) );
  7303. end;
  7304. procedure TTestModule.TestUnitImplConsts;
  7305. begin
  7306. StartUnit(false);
  7307. Add('interface');
  7308. Add('implementation');
  7309. Add('const');
  7310. Add(' v1 = 3;');
  7311. Add(' v2:longint = 4;');
  7312. Add(' v3:string = ''abc'';');
  7313. ConvertUnit;
  7314. CheckSource('TestUnitImplConsts',
  7315. LinesToStr([ // statements
  7316. 'var $impl = $mod.$impl;',
  7317. '']),
  7318. '', // this.$init
  7319. LinesToStr([ // implementation
  7320. '$impl.v1 = 3;',
  7321. '$impl.v2 = 4;',
  7322. '$impl.v3 = "abc";',
  7323. '']) );
  7324. end;
  7325. procedure TTestModule.TestUnitImplRecord;
  7326. begin
  7327. StartUnit(false);
  7328. Add('interface');
  7329. Add('implementation');
  7330. Add('type');
  7331. Add(' TMyRecord = record');
  7332. Add(' i: longint;');
  7333. Add(' end;');
  7334. Add('var aRec: TMyRecord;');
  7335. Add('initialization');
  7336. Add(' arec.i:=3;');
  7337. ConvertUnit;
  7338. CheckSource('TestUnitImplRecord',
  7339. LinesToStr([ // statements
  7340. 'var $impl = $mod.$impl;',
  7341. '']),
  7342. // this.$init
  7343. '$impl.aRec.i = 3;',
  7344. LinesToStr([ // implementation
  7345. 'rtl.recNewT($impl, "TMyRecord", function () {',
  7346. ' this.i = 0;',
  7347. ' this.$eq = function (b) {',
  7348. ' return this.i === b.i;',
  7349. ' };',
  7350. ' this.$assign = function (s) {',
  7351. ' this.i = s.i;',
  7352. ' return this;',
  7353. ' };',
  7354. '});',
  7355. '$impl.aRec = $impl.TMyRecord.$new();',
  7356. '']) );
  7357. end;
  7358. procedure TTestModule.TestRenameJSNameConflict;
  7359. begin
  7360. StartProgram(false);
  7361. Add('var apply: longint;');
  7362. Add('var bind: longint;');
  7363. Add('var call: longint;');
  7364. Add('begin');
  7365. ConvertProgram;
  7366. CheckSource('TestRenameJSNameConflict',
  7367. LinesToStr([ // statements
  7368. 'this.Apply = 0;',
  7369. 'this.Bind = 0;',
  7370. 'this.Call = 0;'
  7371. ]),
  7372. LinesToStr([ // this.$main
  7373. ''
  7374. ]));
  7375. end;
  7376. procedure TTestModule.TestLocalConst;
  7377. begin
  7378. StartProgram(false);
  7379. Add('procedure DoIt;');
  7380. Add('const');
  7381. Add(' cA: longint = 1;');
  7382. Add(' cB = 2;');
  7383. Add(' procedure Sub;');
  7384. Add(' const');
  7385. Add(' csA = 3;');
  7386. Add(' cB: double = 4;');
  7387. Add(' begin');
  7388. Add(' cb:=cb+csa;');
  7389. Add(' ca:=ca+csa+5;');
  7390. Add(' end;');
  7391. Add('begin');
  7392. Add(' ca:=ca+cb+6;');
  7393. Add('end;');
  7394. Add('begin');
  7395. ConvertProgram;
  7396. CheckSource('TestLocalConst',
  7397. LinesToStr([
  7398. 'var cA = 1;',
  7399. 'var cB = 2;',
  7400. 'var csA = 3;',
  7401. 'var cB$1 = 4;',
  7402. 'this.DoIt = function () {',
  7403. ' function Sub() {',
  7404. ' cB$1 = cB$1 + 3;',
  7405. ' cA = cA + 3 + 5;',
  7406. ' };',
  7407. ' cA = cA + 2 + 6;',
  7408. '};'
  7409. ]),
  7410. LinesToStr([
  7411. ]));
  7412. end;
  7413. procedure TTestModule.TestVarExternal;
  7414. begin
  7415. StartProgram(false);
  7416. Add('var');
  7417. Add(' NaN: double; external name ''Global.NaN'';');
  7418. Add(' d: double;');
  7419. Add('begin');
  7420. Add(' d:=NaN;');
  7421. ConvertProgram;
  7422. CheckSource('TestVarExternal',
  7423. LinesToStr([
  7424. 'this.d = 0.0;'
  7425. ]),
  7426. LinesToStr([
  7427. '$mod.d = Global.NaN;'
  7428. ]));
  7429. end;
  7430. procedure TTestModule.TestVarExternalOtherUnit;
  7431. begin
  7432. AddModuleWithIntfImplSrc('unit2.pas',
  7433. LinesToStr([
  7434. 'var NaN: double; external name ''Global.NaN'';',
  7435. 'var iV: longint;'
  7436. ]),
  7437. '');
  7438. StartUnit(true);
  7439. Add('interface');
  7440. Add('uses unit2;');
  7441. Add('implementation');
  7442. Add('var');
  7443. Add(' d: double;');
  7444. Add(' i: longint; external name ''$i'';');
  7445. Add('begin');
  7446. Add(' d:=nan;');
  7447. Add(' d:=uNit2.nan;');
  7448. Add(' d:=test1.d;');
  7449. Add(' i:=iv;');
  7450. Add(' i:=uNit2.iv;');
  7451. Add(' i:=test1.i;');
  7452. ConvertUnit;
  7453. CheckSource('TestVarExternalOtherUnit',
  7454. LinesToStr([
  7455. 'var $impl = $mod.$impl;',
  7456. '']),
  7457. LinesToStr([ // this.$init
  7458. '$impl.d = Global.NaN;',
  7459. '$impl.d = Global.NaN;',
  7460. '$impl.d = $impl.d;',
  7461. '$i = pas.unit2.iV;',
  7462. '$i = pas.unit2.iV;',
  7463. '$i = $i;',
  7464. '']),
  7465. LinesToStr([ // implementation
  7466. '$impl.d = 0.0;',
  7467. '']) );
  7468. end;
  7469. procedure TTestModule.TestVarAbsoluteFail;
  7470. begin
  7471. StartProgram(false);
  7472. Add([
  7473. 'var',
  7474. ' a: longint;',
  7475. ' b: longword absolute a;',
  7476. 'begin']);
  7477. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  7478. ConvertProgram;
  7479. end;
  7480. procedure TTestModule.TestConstExternal;
  7481. begin
  7482. StartProgram(false);
  7483. Add([
  7484. 'const',
  7485. ' PI: double; external name ''Global.PI'';',
  7486. ' Tau = 2*pi;',
  7487. 'var d: double;',
  7488. 'begin',
  7489. ' d:=pi;',
  7490. ' d:=tau+pi;']);
  7491. ConvertProgram;
  7492. CheckSource('TestConstExternal',
  7493. LinesToStr([
  7494. 'this.Tau = 2*Global.PI;',
  7495. 'this.d = 0.0;'
  7496. ]),
  7497. LinesToStr([
  7498. '$mod.d = Global.PI;',
  7499. '$mod.d = $mod.Tau + Global.PI;'
  7500. ]));
  7501. end;
  7502. procedure TTestModule.TestDouble;
  7503. begin
  7504. StartProgram(false);
  7505. Add([
  7506. 'type',
  7507. ' TDateTime = double;',
  7508. 'const',
  7509. ' a = TDateTime(2.7);',
  7510. ' b = a + TDateTime(1.7);',
  7511. ' c = 0.9 + 0.1;',
  7512. ' f0_1 = 0.1;',
  7513. ' f0_3 = 0.3;',
  7514. ' fn0_1 = -0.1;',
  7515. ' fn0_3 = -0.3;',
  7516. ' fn0_003 = -0.003;',
  7517. ' fn0_123456789 = -0.123456789;',
  7518. ' fn300_0 = -300.0;',
  7519. ' fn123456_0 = -123456.0;',
  7520. ' fn1234567_8 = -1234567.8;',
  7521. ' fn12345678_9 = -12345678.9;',
  7522. ' f1_0En12 = 1E-12;',
  7523. ' fn1_0En12 = -1E-12;',
  7524. ' maxdouble = 1.7e+308;',
  7525. ' mindouble = -1.7e+308;',
  7526. ' MinSafeIntDouble = -$1fffffffffffff;',
  7527. ' MinSafeIntDouble2 = -$20000000000000-1;',
  7528. ' MaxSafeIntDouble = $1fffffffffffff;',
  7529. ' DZeroResolution = 1E-12;',
  7530. ' Minus1 = -1E-12;',
  7531. ' EPS = 1E-9;',
  7532. ' DELTA = 0.001;',
  7533. ' Big = 129.789E+100;',
  7534. ' Test0_15 = 0.15;',
  7535. ' Test999 = 2.9999999999999;',
  7536. ' Test111999 = 211199999999999000.0;',
  7537. ' TestMinus111999 = -211199999999999000.0;',
  7538. ' Inf = 1.0 / 0.0;',
  7539. ' NegInf = -1.0 / 0.0;',
  7540. 'procedure Run(d: double); external name ''Run'';',
  7541. 'var',
  7542. ' d: double = b;',
  7543. 'begin',
  7544. ' d:=1.0;',
  7545. ' d:=1.0/3.0;',
  7546. ' d:=1.0/(3-2-1);',
  7547. ' d:=1/3;',
  7548. ' d:=5.0E-324;',
  7549. ' d:=1.7E308;',
  7550. ' d:=001.00E00;',
  7551. ' d:=002.00E001;',
  7552. ' d:=003.000E000;',
  7553. ' d:=-004.00E-00;',
  7554. ' d:=-005.00E-001;',
  7555. ' d:=10**3;',
  7556. ' d:=100*9**0.5;',
  7557. ' d:=10 mod 3;',
  7558. ' d:=10 div 3;',
  7559. ' d:=c;',
  7560. ' d:=f0_1;',
  7561. ' d:=f0_3;',
  7562. ' d:=fn0_1;',
  7563. ' d:=fn0_3;',
  7564. ' d:=fn0_003;',
  7565. ' d:=fn0_123456789;',
  7566. ' d:=fn300_0;',
  7567. ' d:=fn123456_0;',
  7568. ' d:=fn1234567_8;',
  7569. ' d:=fn12345678_9;',
  7570. ' d:=f1_0En12;',
  7571. ' d:=fn1_0En12;',
  7572. ' d:=maxdouble;',
  7573. ' d:=mindouble;',
  7574. ' d:=MinSafeIntDouble;',
  7575. ' d:=double(MinSafeIntDouble);',
  7576. ' d:=MinSafeIntDouble2;',
  7577. ' d:=double(MinSafeIntDouble2);',
  7578. ' d:=MaxSafeIntDouble;',
  7579. ' d:=default(double);',
  7580. ' Run(Inf);',
  7581. ' Run(NegInf);',
  7582. '']);
  7583. ConvertProgram;
  7584. CheckSource('TestDouble',
  7585. LinesToStr([
  7586. 'this.a = 2.7;',
  7587. 'this.b = 2.7 + 1.7;',
  7588. 'this.c = 0.9 + 0.1;',
  7589. 'this.f0_1 = 0.1;',
  7590. 'this.f0_3 = 0.3;',
  7591. 'this.fn0_1 = -0.1;',
  7592. 'this.fn0_3 = -0.3;',
  7593. 'this.fn0_003 = -0.003;',
  7594. 'this.fn0_123456789 = -0.123456789;',
  7595. 'this.fn300_0 = -300.0;',
  7596. 'this.fn123456_0 = -123456.0;',
  7597. 'this.fn1234567_8 = -1234567.8;',
  7598. 'this.fn12345678_9 = -12345678.9;',
  7599. 'this.f1_0En12 = 1E-12;',
  7600. 'this.fn1_0En12 = -1E-12;',
  7601. 'this.maxdouble = 1.7e+308;',
  7602. 'this.mindouble = -1.7e+308;',
  7603. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  7604. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  7605. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  7606. 'this.DZeroResolution = 1E-12;',
  7607. 'this.Minus1 = -1E-12;',
  7608. 'this.EPS = 1E-9;',
  7609. 'this.DELTA = 0.001;',
  7610. 'this.Big = 129.789E+100;',
  7611. 'this.Test0_15 = 0.15;',
  7612. 'this.Test999 = 2.9999999999999;',
  7613. 'this.Test111999 = 211199999999999000.0;',
  7614. 'this.TestMinus111999 = -211199999999999000.0;',
  7615. 'this.Inf = 1.0 / 0.0;',
  7616. 'this.NegInf = -1.0 / 0.0;',
  7617. 'this.d = 4.4;',
  7618. '']),
  7619. LinesToStr([
  7620. '$mod.d = 1.0;',
  7621. '$mod.d = 1.0 / 3.0;',
  7622. '$mod.d = 1.0 / (3 - 2 - 1);',
  7623. '$mod.d = 1 / 3;',
  7624. '$mod.d = 5.0E-324;',
  7625. '$mod.d = 1.7E308;',
  7626. '$mod.d = 1.00E0;',
  7627. '$mod.d = 2.00E1;',
  7628. '$mod.d = 3.000E0;',
  7629. '$mod.d = -4.00E-0;',
  7630. '$mod.d = -5.00E-1;',
  7631. '$mod.d = 10 ** 3;',
  7632. '$mod.d = 100 * (9 ** 0.5);',
  7633. '$mod.d = 10 % 3;',
  7634. '$mod.d = rtl.trunc(10 / 3);',
  7635. '$mod.d = 1;',
  7636. '$mod.d = 0.1;',
  7637. '$mod.d = 0.3;',
  7638. '$mod.d = -0.1;',
  7639. '$mod.d = -0.3;',
  7640. '$mod.d = -3E-3;',
  7641. '$mod.d = -0.123456789;',
  7642. '$mod.d = -300;',
  7643. '$mod.d = -123456;',
  7644. '$mod.d = -1234567.8;',
  7645. '$mod.d = -1.23456789E7;',
  7646. '$mod.d = 1E-12;',
  7647. '$mod.d = -1E-12;',
  7648. '$mod.d = 1.7E308;',
  7649. '$mod.d = -1.7E308;',
  7650. '$mod.d = -9007199254740991;',
  7651. '$mod.d = -9007199254740991;',
  7652. '$mod.d = -9.007199254740992E15;',
  7653. '$mod.d = -9.007199254740992E15;',
  7654. '$mod.d = 9007199254740991;',
  7655. '$mod.d = 0.0;',
  7656. 'Run(1 / 0);',
  7657. 'Run(-1 / 0);',
  7658. '']));
  7659. end;
  7660. procedure TTestModule.TestDoubleSmall;
  7661. begin
  7662. StartProgram(false);
  7663. Add([
  7664. 'const',
  7665. ' a = 1e-1;',
  7666. ' b = 1e-2;',
  7667. ' c = 1e-3;',
  7668. ' d = 1e-4;',
  7669. ' e = 1e-5;',
  7670. ' f = 1e-6;',
  7671. ' g = 1e-7;',
  7672. ' h = -1e-1;',
  7673. ' i = -1e-2;',
  7674. 'procedure Fly(d: double);',
  7675. 'begin',
  7676. 'end;',
  7677. 'begin',
  7678. ' Fly(a);',
  7679. ' Fly(b);',
  7680. ' Fly(c);',
  7681. ' Fly(d);',
  7682. ' Fly(e);',
  7683. ' Fly(f);',
  7684. ' Fly(g);',
  7685. ' Fly(h);',
  7686. ' Fly(i);',
  7687. '']);
  7688. ConvertProgram;
  7689. CheckSource('TestDoubleSmall',
  7690. LinesToStr([
  7691. 'this.a = 1e-1;',
  7692. 'this.b = 1e-2;',
  7693. 'this.c = 1e-3;',
  7694. 'this.d = 1e-4;',
  7695. 'this.e = 1e-5;',
  7696. 'this.f = 1e-6;',
  7697. 'this.g = 1e-7;',
  7698. 'this.h = -1e-1;',
  7699. 'this.i = -1e-2;',
  7700. 'this.Fly = function (d) {',
  7701. '};',
  7702. '']),
  7703. LinesToStr([
  7704. '$mod.Fly(0.1);',
  7705. '$mod.Fly(0.01);',
  7706. '$mod.Fly(1E-3);',
  7707. '$mod.Fly(1E-4);',
  7708. '$mod.Fly(1E-5);',
  7709. '$mod.Fly(1E-6);',
  7710. '$mod.Fly(1E-7);',
  7711. '$mod.Fly(-0.1);',
  7712. '$mod.Fly(-0.01);',
  7713. '']));
  7714. end;
  7715. procedure TTestModule.TestInteger;
  7716. begin
  7717. StartProgram(false);
  7718. Add([
  7719. 'const',
  7720. ' MinInt = low(NativeInt);',
  7721. ' MaxInt = high(NativeInt);',
  7722. 'type',
  7723. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7724. 'const',
  7725. ' a = low(TMyInt)+High(TMyInt);',
  7726. 'var',
  7727. ' i: TMyInt;',
  7728. 'begin',
  7729. ' i:=-MinInt;',
  7730. ' i:=default(TMyInt);',
  7731. ' i:=low(i)+high(i);',
  7732. '']);
  7733. ConvertProgram;
  7734. CheckSource('TestIntegerRange',
  7735. LinesToStr([
  7736. 'this.MinInt = -9007199254740991;',
  7737. 'this.MaxInt = 9007199254740991;',
  7738. 'this.a = -9007199254740991 + 9007199254740991;',
  7739. 'this.i = 0;',
  7740. '']),
  7741. LinesToStr([
  7742. '$mod.i = - -9007199254740991;',
  7743. '$mod.i = -9007199254740991;',
  7744. '$mod.i = -9007199254740991 + 9007199254740991;',
  7745. '']));
  7746. end;
  7747. procedure TTestModule.TestIntegerRange;
  7748. begin
  7749. StartProgram(false);
  7750. Add([
  7751. 'const',
  7752. ' MinInt = -1;',
  7753. ' MaxInt = +1;',
  7754. 'type',
  7755. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7756. ' TInt2 = 1..3;',
  7757. 'const',
  7758. ' a = low(TMyInt)+High(TMyInt);',
  7759. ' b = low(TInt2)+High(TInt2);',
  7760. ' s1 = [1];',
  7761. ' s2 = [1,2];',
  7762. ' s3 = [1..3];',
  7763. ' s4 = [low(shortint)..high(shortint)];',
  7764. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  7765. ' s6 = 1 in s2;',
  7766. 'var',
  7767. ' i: TMyInt;',
  7768. ' i2: TInt2;',
  7769. 'begin',
  7770. ' i:=i2;',
  7771. ' i:=default(TMyInt);',
  7772. ' if i=i2 then ;',
  7773. ' i:=ord(i2);',
  7774. '']);
  7775. ConvertProgram;
  7776. CheckSource('TestIntegerRange',
  7777. LinesToStr([
  7778. 'this.MinInt = -1;',
  7779. 'this.MaxInt = +1;',
  7780. 'this.a = -1 + 1;',
  7781. 'this.b = 1 + 3;',
  7782. 'this.s1 = rtl.createSet(1);',
  7783. 'this.s2 = rtl.createSet(1, 2);',
  7784. 'this.s3 = rtl.createSet(null, 1, 3);',
  7785. 'this.s4 = rtl.createSet(null, -128, 127);',
  7786. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  7787. 'this.s6 = 1 in this.s2;',
  7788. 'this.i = 0;',
  7789. 'this.i2 = 0;',
  7790. '']),
  7791. LinesToStr([
  7792. '$mod.i = $mod.i2;',
  7793. '$mod.i = -1;',
  7794. 'if ($mod.i === $mod.i2) ;',
  7795. '$mod.i = $mod.i2;',
  7796. '']));
  7797. end;
  7798. procedure TTestModule.TestIntegerTypecasts;
  7799. begin
  7800. StartProgram(false);
  7801. Add([
  7802. 'var',
  7803. ' i: nativeint;',
  7804. ' b: byte;',
  7805. ' sh: shortint;',
  7806. ' w: word;',
  7807. ' sm: smallint;',
  7808. ' lw: longword;',
  7809. ' li: longint;',
  7810. 'begin',
  7811. ' b:=byte(i);',
  7812. ' sh:=shortint(i);',
  7813. ' w:=word(i);',
  7814. ' sm:=smallint(i);',
  7815. ' lw:=longword(i);',
  7816. ' li:=longint(i);',
  7817. '']);
  7818. ConvertProgram;
  7819. CheckSource('TestIntegerTypecasts',
  7820. LinesToStr([
  7821. 'this.i = 0;',
  7822. 'this.b = 0;',
  7823. 'this.sh = 0;',
  7824. 'this.w = 0;',
  7825. 'this.sm = 0;',
  7826. 'this.lw = 0;',
  7827. 'this.li = 0;',
  7828. '']),
  7829. LinesToStr([
  7830. '$mod.b = $mod.i & 255;',
  7831. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  7832. '$mod.w = $mod.i & 65535;',
  7833. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  7834. '$mod.lw = $mod.i >>> 0;',
  7835. '$mod.li = $mod.i & 0xFFFFFFFF;',
  7836. '']));
  7837. end;
  7838. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  7839. begin
  7840. StartProgram(false);
  7841. Add([
  7842. 'var',
  7843. ' i,j: nativeint;',
  7844. 'begin',
  7845. ' i:=i shr 0;',
  7846. ' i:=i shr 1;',
  7847. ' i:=i shr 3;',
  7848. ' i:=i shr 54;',
  7849. ' i:=j shr i;',
  7850. '']);
  7851. ConvertProgram;
  7852. CheckResolverUnexpectedHints;
  7853. CheckSource('TestInteger_BitwiseShrNativeInt',
  7854. LinesToStr([
  7855. 'this.i = 0;',
  7856. 'this.j = 0;',
  7857. '']),
  7858. LinesToStr([
  7859. '$mod.i = $mod.i;',
  7860. '$mod.i = Math.floor($mod.i / 2);',
  7861. '$mod.i = Math.floor($mod.i / 8);',
  7862. '$mod.i = 0;',
  7863. '$mod.i = rtl.shr($mod.j, $mod.i);',
  7864. '']));
  7865. end;
  7866. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  7867. begin
  7868. StartProgram(false);
  7869. Add([
  7870. 'var',
  7871. ' i: nativeint;',
  7872. 'begin',
  7873. ' i:=i shl 0;',
  7874. ' i:=i shl 54;',
  7875. ' i:=123456789012 shl 1;',
  7876. ' i:=i shl 1;',
  7877. '']);
  7878. ConvertProgram;
  7879. CheckResolverUnexpectedHints;
  7880. CheckSource('TestInteger_BitwiseShrNativeInt',
  7881. LinesToStr([
  7882. 'this.i = 0;',
  7883. '']),
  7884. LinesToStr([
  7885. '$mod.i = $mod.i;',
  7886. '$mod.i = 0;',
  7887. '$mod.i = 246913578024;',
  7888. '$mod.i = rtl.shl($mod.i, 1);',
  7889. '']));
  7890. end;
  7891. procedure TTestModule.TestInteger_SystemFunc;
  7892. begin
  7893. StartProgram(true);
  7894. Add([
  7895. 'var',
  7896. ' i: byte;',
  7897. ' s: string;',
  7898. 'begin',
  7899. ' system.inc(i);',
  7900. ' system.str(i,s);',
  7901. ' s:=system.str(i);',
  7902. ' i:=system.low(i);',
  7903. ' i:=system.high(i);',
  7904. ' i:=system.pred(i);',
  7905. ' i:=system.succ(i);',
  7906. ' i:=system.ord(i);',
  7907. '']);
  7908. ConvertProgram;
  7909. CheckResolverUnexpectedHints;
  7910. CheckSource('TestInteger_SystemFunc',
  7911. LinesToStr([
  7912. 'this.i = 0;',
  7913. 'this.s = "";',
  7914. '']),
  7915. LinesToStr([
  7916. '$mod.i += 1;',
  7917. '$mod.s = "" + $mod.i;',
  7918. '$mod.s = "" + $mod.i;',
  7919. '$mod.i = 0;',
  7920. '$mod.i = 255;',
  7921. '$mod.i = $mod.i - 1;',
  7922. '$mod.i = $mod.i + 1;',
  7923. '$mod.i = $mod.i;',
  7924. '']));
  7925. end;
  7926. procedure TTestModule.TestInteger_AssignOutsideConst;
  7927. begin
  7928. StartProgram(false);
  7929. Add([
  7930. 'const',
  7931. ' MinInt = low(longint);',
  7932. ' MaxInt = high(longint);',
  7933. 'type',
  7934. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7935. 'var',
  7936. ' i: TMyInt;',
  7937. ' aByte: byte;',
  7938. ' aShortInt: shortint;',
  7939. ' aWord: word;',
  7940. ' aSmallInt: smallint;',
  7941. ' aLongWord: longword;',
  7942. ' aLongInt: longint;',
  7943. ' aNativeInt: nativeint;',
  7944. ' aNativeUInt: nativeuint;',
  7945. 'begin',
  7946. ' aByte:=$FF;',
  7947. ' aByte:=$100;',
  7948. ' aByte:=-1;',
  7949. ' aByte:=-127;',
  7950. ' aByte:=-128;',
  7951. ' aByte:=-254;',
  7952. ' aByte:=-255;',
  7953. ' aByte:=-256;',
  7954. ' aShortInt:=127;',
  7955. ' aShortInt:=128;',
  7956. ' aShortInt:=-128;',
  7957. ' aShortInt:=-129;',
  7958. ' aWord:=$ffff;',
  7959. ' aWord:=$10000;',
  7960. ' aWord:=-1;',
  7961. ' aWord:=-$ffff;',
  7962. ' aWord:=-$10000;',
  7963. ' aWord:=-$10001;',
  7964. ' aSmallInt:=$7fff;',
  7965. ' aSmallInt:=$8000;',
  7966. ' aSmallInt:=-$8000;',
  7967. ' aSmallInt:=-$8001;',
  7968. ' aLongWord:=$ffffffff;',
  7969. ' aLongWord:=$100000000;',
  7970. ' aLongWord:=-1;',
  7971. ' aLongWord:=-$ffffffff;',
  7972. ' aNativeInt:=$1fffffffffffff;',
  7973. ' aNativeInt:=-$1fffffffffffff;',
  7974. ' aNativeUInt:=$1fffffffffffff;',
  7975. ' aNativeUInt:=-$1fffffffffffff;',
  7976. '']);
  7977. ConvertProgram;
  7978. CheckSource('TestInteger_AssignOutsideConst',
  7979. LinesToStr([
  7980. 'this.MinInt = -2147483648;',
  7981. 'this.MaxInt = 2147483647;',
  7982. 'this.i = 0;',
  7983. 'this.aByte = 0;',
  7984. 'this.aShortInt = 0;',
  7985. 'this.aWord = 0;',
  7986. 'this.aSmallInt = 0;',
  7987. 'this.aLongWord = 0;',
  7988. 'this.aLongInt = 0;',
  7989. 'this.aNativeInt = 0;',
  7990. 'this.aNativeUInt = 0;',
  7991. '']),
  7992. LinesToStr([
  7993. '$mod.aByte = 0xFF;',
  7994. '$mod.aByte = 0;',
  7995. '$mod.aByte = 255;',
  7996. '$mod.aByte = 129;',
  7997. '$mod.aByte = 128;',
  7998. '$mod.aByte = 2;',
  7999. '$mod.aByte = 1;',
  8000. '$mod.aByte = 0;',
  8001. '$mod.aShortInt = 127;',
  8002. '$mod.aShortInt = -128;',
  8003. '$mod.aShortInt = -128;',
  8004. '$mod.aShortInt = 127;',
  8005. '$mod.aWord = 0xffff;',
  8006. '$mod.aWord = 0;',
  8007. '$mod.aWord = 65535;',
  8008. '$mod.aWord = 1;',
  8009. '$mod.aWord = 0;',
  8010. '$mod.aWord = 65535;',
  8011. '$mod.aSmallInt = 0x7fff;',
  8012. '$mod.aSmallInt = -32768;',
  8013. '$mod.aSmallInt = -0x8000;',
  8014. '$mod.aSmallInt = 32767;',
  8015. '$mod.aLongWord = 0xffffffff;',
  8016. '$mod.aLongWord = 0;',
  8017. '$mod.aLongWord = 4294967295;',
  8018. '$mod.aLongWord = 1;',
  8019. '$mod.aNativeInt = 0x1fffffffffffff;',
  8020. '$mod.aNativeInt = -0x1fffffffffffff;',
  8021. '$mod.aNativeUInt = 0x1fffffffffffff;',
  8022. '$mod.aNativeUInt = 1;',
  8023. '']));
  8024. end;
  8025. procedure TTestModule.TestCurrency;
  8026. begin
  8027. StartProgram(false);
  8028. Add([
  8029. 'type',
  8030. ' TCoin = currency;',
  8031. 'const',
  8032. ' a = TCoin(2.7);',
  8033. ' b = a + TCoin(1.7);',
  8034. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  8035. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  8036. 'var',
  8037. ' c: TCoin = b;',
  8038. ' i: nativeint;',
  8039. ' d: double;',
  8040. ' j: jsvalue;',
  8041. 'function DoIt(c: currency): currency; begin end;',
  8042. 'function GetIt(d: double): double; begin end;',
  8043. 'procedure Write(v: jsvalue); begin end;',
  8044. 'begin',
  8045. ' c:=1.0;',
  8046. ' c:=0.1;',
  8047. ' c:=1.0/3.0;',
  8048. ' c:=1/3;',
  8049. ' c:=a;',
  8050. ' d:=c;',
  8051. ' c:=d;',
  8052. ' c:=currency(c);',
  8053. ' c:=currency(d);',
  8054. ' d:=double(c);',
  8055. ' c:=i;',
  8056. ' c:=currency(i);',
  8057. //' i:=c;', not allowed
  8058. ' i:=nativeint(c);',
  8059. ' c:=c+a;',
  8060. ' c:=-c-a;',
  8061. ' c:=d+c;',
  8062. ' c:=c+d;',
  8063. ' c:=d-c;',
  8064. ' c:=c-d;',
  8065. ' c:=c*a;',
  8066. ' c:=a*c;',
  8067. ' c:=d*c;',
  8068. ' c:=c*d;',
  8069. ' c:=c/a;',
  8070. ' c:=a/c;',
  8071. ' c:=d/c;',
  8072. ' c:=c/d;',
  8073. ' c:=c**a;',
  8074. ' c:=a**c;',
  8075. ' c:=d**c;',
  8076. ' c:=c**d;',
  8077. ' if c=c then ;',
  8078. ' if c=a then ;',
  8079. ' if a=c then ;',
  8080. ' if d=c then ;',
  8081. ' if c=d then ;',
  8082. ' c:=DoIt(c);',
  8083. ' c:=DoIt(i);',
  8084. ' c:=DoIt(d);',
  8085. ' c:=GetIt(c);',
  8086. ' j:=c;',
  8087. ' Write(c);',
  8088. ' c:=default(currency);',
  8089. ' j:=str(c);',
  8090. ' j:=str(c:0:3);',
  8091. '']);
  8092. ConvertProgram;
  8093. CheckSource('TestCurrency',
  8094. LinesToStr([
  8095. 'this.a = 27000;',
  8096. 'this.b = this.a + 17000;',
  8097. 'this.MinSafeIntCurrency = -92233720368.5477;',
  8098. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  8099. 'this.c = this.b;',
  8100. 'this.i = 0;',
  8101. 'this.d = 0.0;',
  8102. 'this.j = undefined;',
  8103. 'this.DoIt = function (c) {',
  8104. ' var Result = 0;',
  8105. ' return Result;',
  8106. '};',
  8107. 'this.GetIt = function (d) {',
  8108. ' var Result = 0.0;',
  8109. ' return Result;',
  8110. '};',
  8111. 'this.Write = function (v) {',
  8112. '};',
  8113. '']),
  8114. LinesToStr([
  8115. '$mod.c = 10000;',
  8116. '$mod.c = 1000;',
  8117. '$mod.c = rtl.trunc((1.0 / 3.0) * 10000);',
  8118. '$mod.c = rtl.trunc((1 / 3) * 10000);',
  8119. '$mod.c = $mod.a;',
  8120. '$mod.d = $mod.c / 10000;',
  8121. '$mod.c = rtl.trunc($mod.d * 10000);',
  8122. '$mod.c = $mod.c;',
  8123. '$mod.c = $mod.d * 10000;',
  8124. '$mod.d = $mod.c / 10000;',
  8125. '$mod.c = $mod.i * 10000;',
  8126. '$mod.c = $mod.i * 10000;',
  8127. '$mod.i = rtl.trunc($mod.c / 10000);',
  8128. '$mod.c = $mod.c + $mod.a;',
  8129. '$mod.c = -$mod.c - $mod.a;',
  8130. '$mod.c = ($mod.d * 10000) + $mod.c;',
  8131. '$mod.c = $mod.c + ($mod.d * 10000);',
  8132. '$mod.c = ($mod.d * 10000) - $mod.c;',
  8133. '$mod.c = $mod.c - ($mod.d * 10000);',
  8134. '$mod.c = ($mod.c * $mod.a) / 10000;',
  8135. '$mod.c = ($mod.a * $mod.c) / 10000;',
  8136. '$mod.c = $mod.d * $mod.c;',
  8137. '$mod.c = $mod.c * $mod.d;',
  8138. '$mod.c = rtl.trunc(($mod.c / $mod.a) * 10000);',
  8139. '$mod.c = rtl.trunc(($mod.a / $mod.c) * 10000);',
  8140. '$mod.c = rtl.trunc($mod.d / $mod.c);',
  8141. '$mod.c = rtl.trunc($mod.c / $mod.d);',
  8142. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  8143. '$mod.c = rtl.trunc(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  8144. '$mod.c = rtl.trunc(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  8145. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  8146. 'if ($mod.c === $mod.c) ;',
  8147. 'if ($mod.c === $mod.a) ;',
  8148. 'if ($mod.a === $mod.c) ;',
  8149. 'if (($mod.d * 10000) === $mod.c) ;',
  8150. 'if ($mod.c === ($mod.d * 10000)) ;',
  8151. '$mod.c = $mod.DoIt($mod.c);',
  8152. '$mod.c = $mod.DoIt($mod.i * 10000);',
  8153. '$mod.c = $mod.DoIt($mod.d * 10000);',
  8154. '$mod.c = rtl.trunc($mod.GetIt($mod.c / 10000) * 10000);',
  8155. '$mod.j = $mod.c / 10000;',
  8156. '$mod.Write($mod.c / 10000);',
  8157. '$mod.c = 0;',
  8158. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  8159. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  8160. '']));
  8161. end;
  8162. procedure TTestModule.TestForBoolDo;
  8163. begin
  8164. StartProgram(false);
  8165. Add([
  8166. 'var b: boolean;',
  8167. 'begin',
  8168. ' for b:=false to true do ;',
  8169. ' for b:=b downto false do ;',
  8170. ' for b in boolean do ;',
  8171. '']);
  8172. ConvertProgram;
  8173. CheckSource('TestForBoolDo',
  8174. LinesToStr([ // statements
  8175. 'this.b = false;']),
  8176. LinesToStr([ // this.$main
  8177. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  8178. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  8179. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  8180. '']));
  8181. end;
  8182. procedure TTestModule.TestForIntDo;
  8183. begin
  8184. StartProgram(false);
  8185. Add([
  8186. 'var i: longint;',
  8187. 'begin',
  8188. ' for i:=3 to 5 do ;',
  8189. ' for i:=i downto 2 do ;',
  8190. ' for i in byte do ;',
  8191. '']);
  8192. ConvertProgram;
  8193. CheckSource('TestForIntDo',
  8194. LinesToStr([ // statements
  8195. 'this.i = 0;']),
  8196. LinesToStr([ // this.$main
  8197. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  8198. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  8199. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  8200. '']));
  8201. end;
  8202. procedure TTestModule.TestForIntInDo;
  8203. begin
  8204. StartProgram(false);
  8205. Add([
  8206. 'type',
  8207. ' TSetOfInt = set of byte;',
  8208. ' TIntRg = 3..7;',
  8209. ' TSetOfIntRg = set of TIntRg;',
  8210. 'var',
  8211. ' i,i2: longint;',
  8212. ' a1: array of byte;',
  8213. ' a2: array[1..3] of byte;',
  8214. ' soi: TSetOfInt;',
  8215. ' soir: TSetOfIntRg;',
  8216. ' ir: TIntRg;',
  8217. 'begin',
  8218. ' for i in byte do ;',
  8219. ' for i in a1 do ;',
  8220. ' for i in a2 do ;',
  8221. ' for i in [11..13] do ;',
  8222. ' for i in TSetOfInt do ;',
  8223. ' for i in TIntRg do ;',
  8224. ' for i in soi do i2:=i;',
  8225. ' for i in TSetOfIntRg do ;',
  8226. ' for i in soir do ;',
  8227. ' for ir in TIntRg do ;',
  8228. ' for ir in TSetOfIntRg do ;',
  8229. ' for ir in soir do ;',
  8230. '']);
  8231. ConvertProgram;
  8232. CheckSource('TestForIntInDo',
  8233. LinesToStr([ // statements
  8234. 'this.i = 0;',
  8235. 'this.i2 = 0;',
  8236. 'this.a1 = [];',
  8237. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  8238. 'this.soi = {};',
  8239. 'this.soir = {};',
  8240. 'this.ir = 0;',
  8241. '']),
  8242. LinesToStr([ // this.$main
  8243. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  8244. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  8245. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  8246. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  8247. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  8248. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  8249. 'for (var $l6 in $mod.soi) {',
  8250. ' $mod.i = +$l6;',
  8251. ' $mod.i2 = $mod.i;',
  8252. '};',
  8253. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  8254. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  8255. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  8256. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  8257. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  8258. '']));
  8259. end;
  8260. procedure TTestModule.TestCharConst;
  8261. begin
  8262. StartProgram(false);
  8263. Add([
  8264. 'const',
  8265. ' a = #$00F3;',
  8266. ' c: char = ''1'';',
  8267. ' wc: widechar = ''ä'';',
  8268. 'begin',
  8269. ' c:=#0;',
  8270. ' c:=#1;',
  8271. ' c:=#9;',
  8272. ' c:=#10;',
  8273. ' c:=#13;',
  8274. ' c:=#31;',
  8275. ' c:=#32;',
  8276. ' c:=#$A;',
  8277. ' c:=#$0A;',
  8278. ' c:=#$b;',
  8279. ' c:=#$0b;',
  8280. ' c:=^A;',
  8281. ' c:=''"'';',
  8282. ' c:=default(char);',
  8283. ' c:=#$00E4;', // ä
  8284. ' c:=''ä'';',
  8285. ' c:=#$E4;', // ä
  8286. ' c:=#$D800;', // invalid UTF-16
  8287. ' c:=#$DFFF;', // invalid UTF-16
  8288. ' c:=#$FFFF;', // last UCS-2
  8289. ' c:=high(c);', // last UCS-2
  8290. ' c:=#269;',
  8291. '']);
  8292. ConvertProgram;
  8293. CheckSource('TestCharConst',
  8294. LinesToStr([
  8295. 'this.a="ó";',
  8296. 'this.c="1";',
  8297. 'this.wc="ä";'
  8298. ]),
  8299. LinesToStr([
  8300. '$mod.c="\x00";',
  8301. '$mod.c="\x01";',
  8302. '$mod.c="\t";',
  8303. '$mod.c="\n";',
  8304. '$mod.c="\r";',
  8305. '$mod.c="\x1F";',
  8306. '$mod.c=" ";',
  8307. '$mod.c="\n";',
  8308. '$mod.c="\n";',
  8309. '$mod.c="\x0B";',
  8310. '$mod.c="\x0B";',
  8311. '$mod.c="\x01";',
  8312. '$mod.c=''"'';',
  8313. '$mod.c="\x00";',
  8314. '$mod.c = "ä";',
  8315. '$mod.c = "ä";',
  8316. '$mod.c = "ä";',
  8317. '$mod.c="\uD800";',
  8318. '$mod.c="\uDFFF";',
  8319. '$mod.c="\uFFFF";',
  8320. '$mod.c="\uFFFF";',
  8321. '$mod.c = "č";',
  8322. '']));
  8323. end;
  8324. procedure TTestModule.TestChar_Compare;
  8325. begin
  8326. StartProgram(false);
  8327. Add('var');
  8328. Add(' c: char;');
  8329. Add(' b: boolean;');
  8330. Add('begin');
  8331. Add(' b:=c=''1'';');
  8332. Add(' b:=''2''=c;');
  8333. Add(' b:=''3''=''4'';');
  8334. Add(' b:=c<>''5'';');
  8335. Add(' b:=''6''<>c;');
  8336. Add(' b:=c>''7'';');
  8337. Add(' b:=''8''>c;');
  8338. Add(' b:=c>=''9'';');
  8339. Add(' b:=''A''>=c;');
  8340. Add(' b:=c<''B'';');
  8341. Add(' b:=''C''<c;');
  8342. Add(' b:=c<=''D'';');
  8343. Add(' b:=''E''<=c;');
  8344. ConvertProgram;
  8345. CheckSource('TestChar_Compare',
  8346. LinesToStr([
  8347. 'this.c = "\x00";',
  8348. 'this.b = false;'
  8349. ]),
  8350. LinesToStr([
  8351. '$mod.b = $mod.c === "1";',
  8352. '$mod.b = "2" === $mod.c;',
  8353. '$mod.b = "3" === "4";',
  8354. '$mod.b = $mod.c !== "5";',
  8355. '$mod.b = "6" !== $mod.c;',
  8356. '$mod.b = $mod.c > "7";',
  8357. '$mod.b = "8" > $mod.c;',
  8358. '$mod.b = $mod.c >= "9";',
  8359. '$mod.b = "A" >= $mod.c;',
  8360. '$mod.b = $mod.c < "B";',
  8361. '$mod.b = "C" < $mod.c;',
  8362. '$mod.b = $mod.c <= "D";',
  8363. '$mod.b = "E" <= $mod.c;',
  8364. '']));
  8365. end;
  8366. procedure TTestModule.TestChar_BuiltInProcs;
  8367. begin
  8368. StartProgram(false);
  8369. Add([
  8370. 'var',
  8371. ' c: char;',
  8372. ' i: longint;',
  8373. ' s: string;',
  8374. 'begin',
  8375. ' i:=ord(c);',
  8376. ' i:=ord(s[i]);',
  8377. ' c:=chr(i);',
  8378. ' c:=pred(c);',
  8379. ' c:=succ(c);',
  8380. ' c:=low(c);',
  8381. ' c:=high(c);',
  8382. ' i:=byte(c);',
  8383. ' i:=word(c);',
  8384. ' i:=longint(c);',
  8385. '']);
  8386. ConvertProgram;
  8387. CheckSource('TestChar_BuiltInProcs',
  8388. LinesToStr([
  8389. 'this.c = "\x00";',
  8390. 'this.i = 0;',
  8391. 'this.s = "";'
  8392. ]),
  8393. LinesToStr([
  8394. '$mod.i = $mod.c.charCodeAt();',
  8395. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  8396. '$mod.c = String.fromCharCode($mod.i);',
  8397. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  8398. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  8399. '$mod.c = "\x00";',
  8400. '$mod.c = "\uFFFF";',
  8401. '$mod.i = $mod.c.charCodeAt() & 255;',
  8402. '$mod.i = $mod.c.charCodeAt();',
  8403. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  8404. '']));
  8405. end;
  8406. procedure TTestModule.TestStringConst;
  8407. begin
  8408. StartProgram(false);
  8409. Add([
  8410. '{$H+}',
  8411. 'const',
  8412. ' a = #$00F3#$017C;', // first <256, then >=256
  8413. ' b = string(''a'');',
  8414. ' c = string(''ä'');',
  8415. ' d = UnicodeString(''b'');',
  8416. ' e = UnicodeString(''ö'');',
  8417. ' f = low(a)+high(b);',
  8418. ' g: word = low(a);',
  8419. 'var',
  8420. ' s: string = ''abc'';',
  8421. ' i: longint;',
  8422. 'begin',
  8423. ' s:='''';',
  8424. ' s:=#13#10;',
  8425. ' s:=#9''foo'';',
  8426. ' s:=#$A9;',
  8427. ' s:=''foo''#13''bar'';',
  8428. ' s:=''"'';',
  8429. ' s:=''"''''"'';',
  8430. ' s:=#$20AC;', // euro
  8431. ' s:=#$10437;', // outside BMP
  8432. ' s:=''abc''#$20AC;', // ascii,#
  8433. ' s:=''ä''#$20AC;', // non ascii,#
  8434. ' s:=#$20AC''abc'';', // #, ascii
  8435. ' s:=#$20AC''ä'';', // #, non ascii
  8436. ' s:=default(string);',
  8437. ' s:=concat(s);',
  8438. ' s:=concat(s,''a'',s);',
  8439. ' s:=#250#269;',
  8440. ' i:=low(s)+high(a);',
  8441. ' s:=''a/b'';',
  8442. // ToDo: \uD87E\uDC04 -> \u{2F804}
  8443. '']);
  8444. ConvertProgram;
  8445. CheckSource('TestStringConst',
  8446. LinesToStr([
  8447. 'this.a = "óż";',
  8448. 'this.b = "a";',
  8449. 'this.c = "ä";',
  8450. 'this.d = "b";',
  8451. 'this.e = "ö";',
  8452. 'this.f = 1 + this.b.length;',
  8453. 'this.g = 1;',
  8454. 'this.s="abc";',
  8455. 'this.i = 0;',
  8456. '']),
  8457. LinesToStr([
  8458. '$mod.s="";',
  8459. '$mod.s="\r\n";',
  8460. '$mod.s="\tfoo";',
  8461. '$mod.s="©";',
  8462. '$mod.s="foo\rbar";',
  8463. '$mod.s=''"'';',
  8464. '$mod.s=''"\''"'';',
  8465. '$mod.s="€";',
  8466. '$mod.s="'#$F0#$90#$90#$B7'";',
  8467. '$mod.s = "abc€";',
  8468. '$mod.s = "ä€";',
  8469. '$mod.s = "€abc";',
  8470. '$mod.s = "ۊ";',
  8471. '$mod.s="";',
  8472. '$mod.s = $mod.s;',
  8473. '$mod.s = $mod.s.concat("a", $mod.s);',
  8474. '$mod.s = "úč";',
  8475. '$mod.i = 1 + $mod.a.length;',
  8476. '$mod.s = "a/b";',
  8477. '']));
  8478. end;
  8479. procedure TTestModule.TestStringConst_InvalidUTF16;
  8480. begin
  8481. StartProgram(false);
  8482. Add([
  8483. 'const',
  8484. ' a: char = #$D87E;',
  8485. ' b: string = #$D87E;',
  8486. ' c: string = #$D87E#43;',
  8487. 'begin',
  8488. ' c:=''abc''#$D87E;',
  8489. ' c:=#0#1#2;',
  8490. ' c:=#127;',
  8491. ' c:=#128;',
  8492. ' c:=#255;',
  8493. ' c:=#256;',
  8494. '']);
  8495. ConvertProgram;
  8496. CheckSource('TestStringConst',
  8497. LinesToStr([
  8498. 'this.a = "\uD87E";',
  8499. 'this.b = "\uD87E";',
  8500. 'this.c = "\uD87E+";',
  8501. '']),
  8502. LinesToStr([
  8503. '$mod.c = "abc\uD87E";',
  8504. '$mod.c = "\x00\x01\x02";',
  8505. '$mod.c = "'#127'";',
  8506. '$mod.c = "'#$c2#$80'";',
  8507. '$mod.c = "'#$c3#$BF'";',
  8508. '$mod.c = "'#$c4#$80'";',
  8509. '']));
  8510. end;
  8511. procedure TTestModule.TestStringConstSurrogate;
  8512. begin
  8513. StartProgram(false);
  8514. Add([
  8515. 'var',
  8516. ' s: string;',
  8517. 'begin',
  8518. ' s:=''😊'';', // 1F60A
  8519. ' s:=''Hello ''#55357#56841', // #$D83D#$DE09
  8520. '']);
  8521. ConvertProgram;
  8522. CheckSource('TestStringConstSurrogate',
  8523. LinesToStr([
  8524. 'this.s="";'
  8525. ]),
  8526. LinesToStr([
  8527. '$mod.s="😊";',
  8528. '$mod.s="Hello 😉";'
  8529. ]));
  8530. end;
  8531. procedure TTestModule.TestStringConstWhitespaces;
  8532. begin
  8533. StartProgram(false);
  8534. Add([
  8535. 'var',
  8536. ' s: string;',
  8537. 'begin',
  8538. ' s:=#$2028;', // line separator not supported by some editors, e.g. vsc
  8539. ' s:=''Medium Mathematical Space ''#$205f',
  8540. '']);
  8541. ConvertProgram;
  8542. CheckSource('TestStringConstSurrogate',
  8543. LinesToStr([
  8544. 'this.s="";'
  8545. ]),
  8546. LinesToStr([
  8547. '$mod.s="\u2028";',
  8548. '$mod.s="Medium Mathematical Space \u205F";'
  8549. ]));
  8550. end;
  8551. procedure TTestModule.TestStringConst_Multiline;
  8552. begin
  8553. StartProgram(false);
  8554. Add([
  8555. '{$modeswitch multilinestrings}',
  8556. 'const',
  8557. ' a = ``;',
  8558. ' b = `',
  8559. 'line`;',
  8560. ' c = `Single`;',
  8561. ' d = ````;',
  8562. ' e = `abc``xyz`;',
  8563. ' f = `first''line',
  8564. ' second''line`#10;',
  8565. 'begin',
  8566. '']);
  8567. ConvertProgram;
  8568. CheckSource('TestStringConst_Multiline',
  8569. LinesToStr([
  8570. 'this.a = "";',
  8571. 'this.b = "'+JSONNewLine+'line";',
  8572. 'this.c = "Single";',
  8573. 'this.d = "`";',
  8574. 'this.e = "abc`xyz";',
  8575. 'this.f = "first''line'+JSONNewLine+' second''line\n";',
  8576. '']),
  8577. LinesToStr([
  8578. ]));
  8579. end;
  8580. procedure TTestModule.TestString_Length;
  8581. begin
  8582. StartProgram(false);
  8583. Add('const c = ''foo'';');
  8584. Add('var');
  8585. Add(' s: string;');
  8586. Add(' i: longint;');
  8587. Add('begin');
  8588. Add(' i:=length(s);');
  8589. Add(' i:=length(s+s);');
  8590. Add(' i:=length(''abc'');');
  8591. Add(' i:=length(c);');
  8592. ConvertProgram;
  8593. CheckSource('TestString_Length',
  8594. LinesToStr([
  8595. 'this.c = "foo";',
  8596. 'this.s = "";',
  8597. 'this.i = 0;',
  8598. '']),
  8599. LinesToStr([
  8600. '$mod.i = $mod.s.length;',
  8601. '$mod.i = ($mod.s+$mod.s).length;',
  8602. '$mod.i = "abc".length;',
  8603. '$mod.i = $mod.c.length;',
  8604. '']));
  8605. end;
  8606. procedure TTestModule.TestString_Compare;
  8607. begin
  8608. StartProgram(false);
  8609. Add('var');
  8610. Add(' s, t: string;');
  8611. Add(' b: boolean;');
  8612. Add('begin');
  8613. Add(' b:=s=t;');
  8614. Add(' b:=s<>t;');
  8615. Add(' b:=s>t;');
  8616. Add(' b:=s>=t;');
  8617. Add(' b:=s<t;');
  8618. Add(' b:=s<=t;');
  8619. ConvertProgram;
  8620. CheckSource('TestString_Compare',
  8621. LinesToStr([ // statements
  8622. 'this.s = "";',
  8623. 'this.t = "";',
  8624. 'this.b =false;'
  8625. ]),
  8626. LinesToStr([ // this.$main
  8627. '$mod.b = $mod.s === $mod.t;',
  8628. '$mod.b = $mod.s !== $mod.t;',
  8629. '$mod.b = $mod.s > $mod.t;',
  8630. '$mod.b = $mod.s >= $mod.t;',
  8631. '$mod.b = $mod.s < $mod.t;',
  8632. '$mod.b = $mod.s <= $mod.t;',
  8633. '']));
  8634. end;
  8635. procedure TTestModule.TestString_SetLength;
  8636. begin
  8637. StartProgram(false);
  8638. Add([
  8639. 'procedure Fly(var s: string);',
  8640. 'begin',
  8641. ' SetLength(s,1);',
  8642. 'end;',
  8643. 'procedure Run(var s: unicodestring);',
  8644. 'begin',
  8645. ' SetLength(s,2);',
  8646. 'end;',
  8647. 'var s: string;',
  8648. ' u: unicodestring;',
  8649. 'begin',
  8650. ' SetLength(s,3);',
  8651. ' SetLength(u,4);',
  8652. '']);
  8653. ConvertProgram;
  8654. CheckSource('TestString_SetLength',
  8655. LinesToStr([ // statements
  8656. 'this.Fly = function (s) {',
  8657. ' s.set(rtl.strSetLength(s.get(), 1));',
  8658. '};',
  8659. 'this.Run = function (s) {',
  8660. ' s.set(rtl.strSetLength(s.get(), 2));',
  8661. '};',
  8662. 'this.s = "";',
  8663. 'this.u = "";',
  8664. '']),
  8665. LinesToStr([ // this.$main
  8666. '$mod.s = rtl.strSetLength($mod.s, 3);',
  8667. '$mod.u = rtl.strSetLength($mod.u, 4);'
  8668. ]));
  8669. end;
  8670. procedure TTestModule.TestString_CharAt;
  8671. begin
  8672. StartProgram(false);
  8673. Add([
  8674. 'var',
  8675. ' s: string;',
  8676. ' c: char;',
  8677. ' b: boolean;',
  8678. 'begin',
  8679. ' b:= s[1] = c;',
  8680. ' b:= c = s[1];',
  8681. ' b:= c <> s[1];',
  8682. ' b:= c > s[1];',
  8683. ' b:= c >= s[1];',
  8684. ' b:= c < s[2];',
  8685. ' b:= c <= s[1];',
  8686. ' s[1] := c;',
  8687. ' s[2+3] := c;']);
  8688. ConvertProgram;
  8689. CheckSource('TestString_CharAt',
  8690. LinesToStr([ // statements
  8691. 'this.s = "";',
  8692. 'this.c = "\x00";',
  8693. 'this.b = false;'
  8694. ]),
  8695. LinesToStr([ // this.$main
  8696. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  8697. '$mod.b = $mod.c === $mod.s.charAt(0);',
  8698. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  8699. '$mod.b = $mod.c > $mod.s.charAt(0);',
  8700. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  8701. '$mod.b = $mod.c < $mod.s.charAt(1);',
  8702. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  8703. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  8704. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  8705. '']));
  8706. end;
  8707. procedure TTestModule.TestStringHMinusFail;
  8708. begin
  8709. StartProgram(false);
  8710. Add([
  8711. '{$H-}',
  8712. 'var s: string;',
  8713. 'begin']);
  8714. ConvertProgram;
  8715. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  8716. end;
  8717. procedure TTestModule.TestStr;
  8718. begin
  8719. StartProgram(false);
  8720. Add('var');
  8721. Add(' b: boolean;');
  8722. Add(' i: longint;');
  8723. Add(' d: double;');
  8724. Add(' s: string;');
  8725. Add('begin');
  8726. Add(' str(b,s);');
  8727. Add(' str(i,s);');
  8728. Add(' str(d,s);');
  8729. Add(' str(i:3,s);');
  8730. Add(' str(d:3:2,s);');
  8731. Add(' Str(12.456:12:1,s);');
  8732. Add(' Str(12.456:12,s);');
  8733. Add(' s:=str(b);');
  8734. Add(' s:=str(i);');
  8735. Add(' s:=str(d);');
  8736. Add(' s:=str(i,i);');
  8737. Add(' s:=str(i:3);');
  8738. Add(' s:=str(d:3:2);');
  8739. Add(' s:=str(i:4,i);');
  8740. Add(' s:=str(i,i:5);');
  8741. Add(' s:=str(i:4,i:5);');
  8742. Add(' s:=str(s,s);');
  8743. Add(' s:=str(s,''foo'');');
  8744. ConvertProgram;
  8745. CheckSource('TestStr',
  8746. LinesToStr([ // statements
  8747. 'this.b = false;',
  8748. 'this.i = 0;',
  8749. 'this.d = 0.0;',
  8750. 'this.s = "";',
  8751. '']),
  8752. LinesToStr([ // this.$main
  8753. '$mod.s = ""+$mod.b;',
  8754. '$mod.s = ""+$mod.i;',
  8755. '$mod.s = rtl.floatToStr($mod.d);',
  8756. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  8757. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  8758. '$mod.s = rtl.floatToStr(12.456,12,1);',
  8759. '$mod.s = rtl.floatToStr(12.456,12);',
  8760. '$mod.s = ""+$mod.b;',
  8761. '$mod.s = ""+$mod.i;',
  8762. '$mod.s = rtl.floatToStr($mod.d);',
  8763. '$mod.s = ""+$mod.i+$mod.i;',
  8764. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  8765. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  8766. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  8767. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  8768. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  8769. '$mod.s = $mod.s + $mod.s;',
  8770. '$mod.s = $mod.s + "foo";',
  8771. '']));
  8772. end;
  8773. procedure TTestModule.TestBaseType_AnsiStringFail;
  8774. begin
  8775. StartProgram(false);
  8776. Add('var s: AnsiString');
  8777. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  8778. ConvertProgram;
  8779. end;
  8780. procedure TTestModule.TestBaseType_WideStringFail;
  8781. begin
  8782. StartProgram(false);
  8783. Add('var s: WideString');
  8784. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  8785. ConvertProgram;
  8786. end;
  8787. procedure TTestModule.TestBaseType_ShortStringFail;
  8788. begin
  8789. StartProgram(false);
  8790. Add('var s: ShortString');
  8791. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  8792. ConvertProgram;
  8793. end;
  8794. procedure TTestModule.TestBaseType_RawByteStringFail;
  8795. begin
  8796. StartProgram(false);
  8797. Add('var s: RawByteString');
  8798. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  8799. ConvertProgram;
  8800. end;
  8801. procedure TTestModule.TestTypeShortstring_Fail;
  8802. begin
  8803. StartProgram(false);
  8804. Add('type t = string[12];');
  8805. Add('var s: t;');
  8806. Add('begin');
  8807. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  8808. ConvertProgram;
  8809. end;
  8810. procedure TTestModule.TestCharSet_Custom;
  8811. begin
  8812. StartProgram(false);
  8813. Add([
  8814. 'type',
  8815. ' TCharRg = ''a''..''z'';',
  8816. ' TSetOfCharRg = set of TCharRg;',
  8817. ' TCharRg2 = ''m''..''p'';',
  8818. 'const',
  8819. ' crg: TCharRg = ''b'';',
  8820. 'var',
  8821. ' c: char;',
  8822. ' crg2: TCharRg2;',
  8823. ' s: TSetOfCharRg;',
  8824. 'begin',
  8825. ' c:=crg;',
  8826. ' crg:=c;',
  8827. ' crg2:=crg;',
  8828. ' if c=crg then ;',
  8829. ' if crg=c then ;',
  8830. ' if crg=crg2 then ;',
  8831. ' if c in s then ;',
  8832. ' if crg2 in s then ;',
  8833. ' c:=default(TCharRg);',
  8834. '']);
  8835. ConvertProgram;
  8836. CheckSource('TestCharSet_Custom',
  8837. LinesToStr([ // statements
  8838. 'this.crg = "b";',
  8839. 'this.c = "\x00";',
  8840. 'this.crg2 = "m";',
  8841. 'this.s = {};',
  8842. '']),
  8843. LinesToStr([ // this.$main
  8844. '$mod.c = $mod.crg;',
  8845. '$mod.crg = $mod.c;',
  8846. '$mod.crg2 = $mod.crg;',
  8847. 'if ($mod.c === $mod.crg) ;',
  8848. 'if ($mod.crg === $mod.c) ;',
  8849. 'if ($mod.crg === $mod.crg2) ;',
  8850. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  8851. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  8852. '$mod.c = "a";',
  8853. '']));
  8854. end;
  8855. procedure TTestModule.TestWideChar;
  8856. begin
  8857. StartProgram(false);
  8858. Add([
  8859. 'procedure Fly(var c: char);',
  8860. 'begin',
  8861. 'end;',
  8862. 'procedure Run(var c: widechar);',
  8863. 'begin',
  8864. 'end;',
  8865. 'var',
  8866. ' c: char;',
  8867. ' wc: widechar;',
  8868. ' w: word;',
  8869. 'begin',
  8870. ' Fly(wc);',
  8871. ' Run(c);',
  8872. ' wc:=WideChar(w);',
  8873. ' w:=ord(wc);',
  8874. '']);
  8875. ConvertProgram;
  8876. CheckSource('TestWideChar_VarArg',
  8877. LinesToStr([ // statements
  8878. 'this.Fly = function (c) {',
  8879. '};',
  8880. 'this.Run = function (c) {',
  8881. '};',
  8882. 'this.c = "\x00";',
  8883. 'this.wc = "\x00";',
  8884. 'this.w = 0;',
  8885. '']),
  8886. LinesToStr([ // this.$main
  8887. '$mod.Fly({',
  8888. ' p: $mod,',
  8889. ' get: function () {',
  8890. ' return this.p.wc;',
  8891. ' },',
  8892. ' set: function (v) {',
  8893. ' this.p.wc = v;',
  8894. ' }',
  8895. '});',
  8896. '$mod.Run({',
  8897. ' p: $mod,',
  8898. ' get: function () {',
  8899. ' return this.p.c;',
  8900. ' },',
  8901. ' set: function (v) {',
  8902. ' this.p.c = v;',
  8903. ' }',
  8904. '});',
  8905. '$mod.wc = String.fromCharCode($mod.w);',
  8906. '$mod.w = $mod.wc.charCodeAt();',
  8907. '',
  8908. '']));
  8909. end;
  8910. procedure TTestModule.TestForCharDo;
  8911. begin
  8912. StartProgram(false);
  8913. Add([
  8914. 'var c: char;',
  8915. 'begin',
  8916. ' for c:=''a'' to ''c'' do ;',
  8917. ' for c:=c downto ''a'' do ;',
  8918. ' for c:=''Б'' to ''Я'' do ;',
  8919. '']);
  8920. ConvertProgram;
  8921. CheckSource('TestForCharDo',
  8922. LinesToStr([ // statements
  8923. 'this.c = "\x00";']),
  8924. LinesToStr([ // this.$main
  8925. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  8926. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  8927. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  8928. '']));
  8929. end;
  8930. procedure TTestModule.TestForCharInDo;
  8931. begin
  8932. StartProgram(false);
  8933. Add([
  8934. 'type',
  8935. ' TSetOfChar = set of char;',
  8936. ' TCharRg = ''a''..''z'';',
  8937. ' TSetOfCharRg = set of TCharRg;',
  8938. 'const Foo = ''foo'';',
  8939. 'var',
  8940. ' c,c2: char;',
  8941. ' s: string;',
  8942. ' a1: array of char;',
  8943. ' a2: array[1..3] of char;',
  8944. ' soc: TSetOfChar;',
  8945. ' socr: TSetOfCharRg;',
  8946. ' cr: TCharRg;',
  8947. 'begin',
  8948. ' for c in foo do ;',
  8949. ' for c in s do ;',
  8950. ' for c in char do ;',
  8951. ' for c in a1 do ;',
  8952. ' for c in a2 do ;',
  8953. ' for c in [''1''..''3''] do ;',
  8954. ' for c in TSetOfChar do ;',
  8955. ' for c in TCharRg do ;',
  8956. ' for c in soc do c2:=c;',
  8957. ' for c in TSetOfCharRg do ;',
  8958. ' for c in socr do ;',
  8959. ' for cr in TCharRg do ;',
  8960. ' for cr in TSetOfCharRg do ;',
  8961. ' for cr in socr do ;',
  8962. '']);
  8963. ConvertProgram;
  8964. CheckSource('TestForCharInDo',
  8965. LinesToStr([ // statements
  8966. 'this.Foo = "foo";',
  8967. 'this.c = "\x00";',
  8968. 'this.c2 = "\x00";',
  8969. 'this.s = "";',
  8970. 'this.a1 = [];',
  8971. 'this.a2 = rtl.arraySetLength(null, "\x00", 3);',
  8972. 'this.soc = {};',
  8973. 'this.socr = {};',
  8974. 'this.cr = "a";',
  8975. '']),
  8976. LinesToStr([ // this.$main
  8977. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  8978. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  8979. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  8980. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  8981. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  8982. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  8983. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  8984. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  8985. 'for (var $l8 in $mod.soc) {',
  8986. ' $mod.c = String.fromCharCode($l8);',
  8987. ' $mod.c2 = $mod.c;',
  8988. '};',
  8989. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  8990. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  8991. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  8992. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  8993. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  8994. '']));
  8995. end;
  8996. procedure TTestModule.TestProcTwoArgs;
  8997. begin
  8998. StartProgram(false);
  8999. Add('procedure Test(a,b: longint);');
  9000. Add('begin');
  9001. Add('end;');
  9002. Add('begin');
  9003. ConvertProgram;
  9004. CheckSource('TestProcTwoArgs',
  9005. LinesToStr([ // statements
  9006. 'this.Test = function (a,b) {',
  9007. '};'
  9008. ]),
  9009. LinesToStr([ // this.$main
  9010. ''
  9011. ]));
  9012. end;
  9013. procedure TTestModule.TestProc_DefaultValue;
  9014. begin
  9015. StartProgram(false);
  9016. Add('procedure p1(i: longint = 1);');
  9017. Add('begin');
  9018. Add('end;');
  9019. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  9020. Add('begin');
  9021. Add('end;');
  9022. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  9023. Add('begin');
  9024. Add('end;');
  9025. Add('begin');
  9026. Add(' p1;');
  9027. Add(' p1();');
  9028. Add(' p1(11);');
  9029. Add(' p2;');
  9030. Add(' p2();');
  9031. Add(' p2(12);');
  9032. Add(' p2(13,''b'');');
  9033. Add(' p3();');
  9034. ConvertProgram;
  9035. CheckSource('TestProc_DefaultValue',
  9036. LinesToStr([ // statements
  9037. 'this.p1 = function (i) {',
  9038. '};',
  9039. 'this.p2 = function (i,c) {',
  9040. '};',
  9041. 'this.p3 = function (d,b,s) {',
  9042. '};'
  9043. ]),
  9044. LinesToStr([ // this.$main
  9045. ' $mod.p1(1);',
  9046. ' $mod.p1(1);',
  9047. ' $mod.p1(11);',
  9048. ' $mod.p2(1,"a");',
  9049. ' $mod.p2(1,"a");',
  9050. ' $mod.p2(12,"a");',
  9051. ' $mod.p2(13,"b");',
  9052. ' $mod.p3(1.0,false,"abc");'
  9053. ]));
  9054. end;
  9055. procedure TTestModule.TestFunctionInt;
  9056. begin
  9057. StartProgram(false);
  9058. Add('function MyTest(Bar: longint): longint;');
  9059. Add('begin');
  9060. Add(' Result:=2*bar');
  9061. Add('end;');
  9062. Add('begin');
  9063. ConvertProgram;
  9064. CheckSource('TestFunctionInt',
  9065. LinesToStr([ // statements
  9066. 'this.MyTest = function (Bar) {',
  9067. ' var Result = 0;',
  9068. ' Result = 2*Bar;',
  9069. ' return Result;',
  9070. '};'
  9071. ]),
  9072. LinesToStr([ // this.$main
  9073. ''
  9074. ]));
  9075. end;
  9076. procedure TTestModule.TestFunctionString;
  9077. begin
  9078. StartProgram(false);
  9079. Add('function Test(Bar: string): string;');
  9080. Add('begin');
  9081. Add(' Result:=bar+BAR');
  9082. Add('end;');
  9083. Add('begin');
  9084. ConvertProgram;
  9085. CheckSource('TestFunctionString',
  9086. LinesToStr([ // statements
  9087. 'this.Test = function (Bar) {',
  9088. ' var Result = "";',
  9089. ' Result = Bar+Bar;',
  9090. ' return Result;',
  9091. '};'
  9092. ]),
  9093. LinesToStr([ // this.$main
  9094. ''
  9095. ]));
  9096. end;
  9097. procedure TTestModule.TestIfThen;
  9098. begin
  9099. StartProgram(false);
  9100. Add([
  9101. 'var b: boolean;',
  9102. 'begin',
  9103. ' if b then ;',
  9104. ' if b then else ;']);
  9105. ConvertProgram;
  9106. CheckSource('TestIfThen',
  9107. LinesToStr([ // statements
  9108. 'this.b = false;',
  9109. '']),
  9110. LinesToStr([ // this.$main
  9111. 'if ($mod.b) ;',
  9112. 'if ($mod.b) ;',
  9113. '']));
  9114. end;
  9115. procedure TTestModule.TestForLoop;
  9116. begin
  9117. StartProgram(false);
  9118. Add('var');
  9119. Add(' vI, vJ, vN: longint;');
  9120. Add('begin');
  9121. Add(' VJ:=0;');
  9122. Add(' VN:=3;');
  9123. Add(' for VI:=1 to VN do');
  9124. Add(' begin');
  9125. Add(' VJ:=VJ+VI;');
  9126. Add(' end;');
  9127. ConvertProgram;
  9128. CheckSource('TestForLoop',
  9129. LinesToStr([ // statements
  9130. 'this.vI = 0;',
  9131. 'this.vJ = 0;',
  9132. 'this.vN = 0;'
  9133. ]),
  9134. LinesToStr([ // this.$main
  9135. ' $mod.vJ = 0;',
  9136. ' $mod.vN = 3;',
  9137. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  9138. ' $mod.vI = $l;',
  9139. ' $mod.vJ = $mod.vJ + $mod.vI;',
  9140. ' };',
  9141. '']));
  9142. end;
  9143. procedure TTestModule.TestForLoopInsideFunction;
  9144. begin
  9145. StartProgram(false);
  9146. Add('function SumNumbers(Count: longint): longint;');
  9147. Add('var');
  9148. Add(' vI, vJ: longint;');
  9149. Add('begin');
  9150. Add(' vj:=0;');
  9151. Add(' for vi:=1 to count do');
  9152. Add(' begin');
  9153. Add(' vj:=vj+vi;');
  9154. Add(' end;');
  9155. Add('end;');
  9156. Add('begin');
  9157. Add(' sumnumbers(3);');
  9158. ConvertProgram;
  9159. CheckSource('TestForLoopInsideFunction',
  9160. LinesToStr([ // statements
  9161. 'this.SumNumbers = function (Count) {',
  9162. ' var Result = 0;',
  9163. ' var vI = 0;',
  9164. ' var vJ = 0;',
  9165. ' vJ = 0;',
  9166. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  9167. ' vI = $l;',
  9168. ' vJ = vJ + vI;',
  9169. ' };',
  9170. ' return Result;',
  9171. '};'
  9172. ]),
  9173. LinesToStr([ // $mod.$main
  9174. ' $mod.SumNumbers(3);'
  9175. ]));
  9176. end;
  9177. procedure TTestModule.TestForLoop_ReadVarAfter;
  9178. begin
  9179. StartProgram(false);
  9180. Add('var');
  9181. Add(' vI: longint;');
  9182. Add('begin');
  9183. Add(' for vi:=1 to 2 do ;');
  9184. Add(' if vi=3 then ;');
  9185. ConvertProgram;
  9186. CheckSource('TestForLoop',
  9187. LinesToStr([ // statements
  9188. 'this.vI = 0;'
  9189. ]),
  9190. LinesToStr([ // this.$main
  9191. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  9192. ' if ($mod.vI===3) ;'
  9193. ]));
  9194. end;
  9195. procedure TTestModule.TestForLoop_Nested;
  9196. begin
  9197. StartProgram(false);
  9198. Add('function SumNumbers(Count: longint): longint;');
  9199. Add('var');
  9200. Add(' vI, vJ, vK: longint;');
  9201. Add('begin');
  9202. Add(' VK:=0;');
  9203. Add(' for VI:=1 to count do');
  9204. Add(' begin');
  9205. Add(' for vj:=1 to vi do');
  9206. Add(' begin');
  9207. Add(' vk:=VK+VI;');
  9208. Add(' end;');
  9209. Add(' end;');
  9210. Add('end;');
  9211. Add('begin');
  9212. Add(' sumnumbers(3);');
  9213. ConvertProgram;
  9214. CheckSource('TestForLoopInFunction',
  9215. LinesToStr([ // statements
  9216. 'this.SumNumbers = function (Count) {',
  9217. ' var Result = 0;',
  9218. ' var vI = 0;',
  9219. ' var vJ = 0;',
  9220. ' var vK = 0;',
  9221. ' vK = 0;',
  9222. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  9223. ' vI = $l;',
  9224. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  9225. ' vJ = $l1;',
  9226. ' vK = vK + vI;',
  9227. ' };',
  9228. ' };',
  9229. ' return Result;',
  9230. '};'
  9231. ]),
  9232. LinesToStr([ // $mod.$main
  9233. ' $mod.SumNumbers(3);'
  9234. ]));
  9235. end;
  9236. procedure TTestModule.TestRepeatUntil;
  9237. begin
  9238. StartProgram(false);
  9239. Add('var');
  9240. Add(' vI, vJ, vN: longint;');
  9241. Add('begin');
  9242. Add(' vn:=3;');
  9243. Add(' vj:=0;');
  9244. Add(' VI:=0;');
  9245. Add(' repeat');
  9246. Add(' VI:=vi+1;');
  9247. Add(' vj:=VJ+vI;');
  9248. Add(' until vi>=vn');
  9249. ConvertProgram;
  9250. CheckSource('TestRepeatUntil',
  9251. LinesToStr([ // statements
  9252. 'this.vI = 0;',
  9253. 'this.vJ = 0;',
  9254. 'this.vN = 0;'
  9255. ]),
  9256. LinesToStr([ // $mod.$main
  9257. ' $mod.vN = 3;',
  9258. ' $mod.vJ = 0;',
  9259. ' $mod.vI = 0;',
  9260. ' do{',
  9261. ' $mod.vI = $mod.vI + 1;',
  9262. ' $mod.vJ = $mod.vJ + $mod.vI;',
  9263. ' }while(!($mod.vI>=$mod.vN));'
  9264. ]));
  9265. end;
  9266. procedure TTestModule.TestAsmBlock;
  9267. begin
  9268. StartProgram(false);
  9269. Add([
  9270. 'var',
  9271. ' vI: longint;',
  9272. 'begin',
  9273. ' vi:=1;',
  9274. ' asm',
  9275. ' if (vI===1) {',
  9276. ' vI=2;',
  9277. //' console.log(''end;'');', ToDo
  9278. ' }',
  9279. ' if (vI===2){ vI=3; }',
  9280. ' end;',
  9281. ' VI:=4;']);
  9282. ConvertProgram;
  9283. CheckSource('TestAsmBlock',
  9284. LinesToStr([ // statements
  9285. 'this.vI = 0;'
  9286. ]),
  9287. LinesToStr([ // $mod.$main
  9288. '$mod.vI = 1;',
  9289. 'if (vI===1) {',
  9290. ' vI=2;',
  9291. '}',
  9292. 'if (vI===2){ vI=3; }',
  9293. ';',
  9294. '$mod.vI = 4;'
  9295. ]));
  9296. end;
  9297. procedure TTestModule.TestAsmPas_Impl;
  9298. begin
  9299. StartUnit(false);
  9300. Add('interface');
  9301. Add('const cIntf: longint = 1;');
  9302. Add('var vIntf: longint;');
  9303. Add('implementation');
  9304. Add('const cImpl: longint = 2;');
  9305. Add('var vImpl: longint;');
  9306. Add('procedure DoIt;');
  9307. Add('const cLoc: longint = 3;');
  9308. Add('var vLoc: longint;');
  9309. Add('begin;');
  9310. Add(' asm');
  9311. //Add(' pas(vIntf)=pas(cIntf);');
  9312. //Add(' pas(vImpl)=pas(cImpl);');
  9313. //Add(' pas(vLoc)=pas(cLoc);');
  9314. Add(' end;');
  9315. Add('end;');
  9316. ConvertUnit;
  9317. CheckSource('TestAsmPas_Impl',
  9318. LinesToStr([
  9319. 'var $impl = $mod.$impl;',
  9320. 'this.cIntf = 1;',
  9321. 'this.vIntf = 0;',
  9322. '']),
  9323. '', // this.$init
  9324. LinesToStr([ // implementation
  9325. '$impl.cImpl = 2;',
  9326. '$impl.vImpl = 0;',
  9327. 'var cLoc = 3;',
  9328. '$impl.DoIt = function () {',
  9329. ' var vLoc = 0;',
  9330. '};',
  9331. '']) );
  9332. end;
  9333. procedure TTestModule.TestTryFinally;
  9334. begin
  9335. StartProgram(false);
  9336. Add('var i: longint;');
  9337. Add('begin');
  9338. Add(' try');
  9339. Add(' i:=0; i:=2 div i;');
  9340. Add(' finally');
  9341. Add(' i:=3');
  9342. Add(' end;');
  9343. ConvertProgram;
  9344. CheckSource('TestTryFinally',
  9345. LinesToStr([ // statements
  9346. 'this.i = 0;'
  9347. ]),
  9348. LinesToStr([ // $mod.$main
  9349. 'try {',
  9350. ' $mod.i = 0;',
  9351. ' $mod.i = rtl.trunc(2 / $mod.i);',
  9352. '} finally {',
  9353. ' $mod.i = 3;',
  9354. '};'
  9355. ]));
  9356. end;
  9357. procedure TTestModule.TestTryExcept;
  9358. begin
  9359. StartProgram(false);
  9360. Add([
  9361. 'type',
  9362. ' TObject = class end;',
  9363. ' Exception = class Msg: string; end;',
  9364. ' EInvalidCast = class(Exception) end;',
  9365. 'var vI: longint;',
  9366. 'begin',
  9367. ' try',
  9368. ' vi:=1;',
  9369. ' except',
  9370. ' vi:=2',
  9371. ' end;',
  9372. ' try',
  9373. ' vi:=3;',
  9374. ' except',
  9375. ' raise;',
  9376. ' end;',
  9377. ' try',
  9378. ' VI:=4;',
  9379. ' except',
  9380. ' on einvalidcast do',
  9381. ' raise;',
  9382. ' on E: exception do',
  9383. ' if e.msg='''' then',
  9384. ' raise e;',
  9385. ' else',
  9386. ' vi:=5',
  9387. ' end;',
  9388. ' try',
  9389. ' VI:=6;',
  9390. ' except',
  9391. ' on einvalidcast do ;',
  9392. ' end;',
  9393. '']);
  9394. ConvertProgram;
  9395. CheckSource('TestTryExcept',
  9396. LinesToStr([ // statements
  9397. 'rtl.createClass(this, "TObject", null, function () {',
  9398. ' this.$init = function () {',
  9399. ' };',
  9400. ' this.$final = function () {',
  9401. ' };',
  9402. '});',
  9403. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  9404. ' this.$init = function () {',
  9405. ' $mod.TObject.$init.call(this);',
  9406. ' this.Msg = "";',
  9407. ' };',
  9408. '});',
  9409. 'rtl.createClass(this, "EInvalidCast", this.Exception, function () {',
  9410. '});',
  9411. 'this.vI = 0;'
  9412. ]),
  9413. LinesToStr([ // $mod.$main
  9414. 'try {',
  9415. ' $mod.vI = 1;',
  9416. '} catch ($e) {',
  9417. ' $mod.vI = 2;',
  9418. '};',
  9419. 'try {',
  9420. ' $mod.vI = 3;',
  9421. '} catch ($e) {',
  9422. ' throw $e;',
  9423. '};',
  9424. 'try {',
  9425. ' $mod.vI = 4;',
  9426. '} catch ($e) {',
  9427. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  9428. ' throw $e',
  9429. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  9430. ' var E = $e;',
  9431. ' if (E.Msg === "") throw E;',
  9432. ' } else {',
  9433. ' $mod.vI = 5;',
  9434. ' }',
  9435. '};',
  9436. 'try {',
  9437. ' $mod.vI = 6;',
  9438. '} catch ($e) {',
  9439. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  9440. ' } else throw $e',
  9441. '};',
  9442. '']));
  9443. end;
  9444. procedure TTestModule.TestTryExcept_ReservedWords;
  9445. begin
  9446. StartProgram(false);
  9447. Add([
  9448. 'type',
  9449. ' TObject = class end;',
  9450. ' Exception = class',
  9451. ' Symbol: string;',
  9452. ' end;',
  9453. 'var &try: longint;',
  9454. 'begin',
  9455. ' try',
  9456. ' &try:=4;',
  9457. ' except',
  9458. ' on Error: exception do',
  9459. ' if errOR.symBol='''' then',
  9460. ' raise ERRor;',
  9461. ' end;',
  9462. '']);
  9463. ConvertProgram;
  9464. CheckSource('TestTryExcept_ReservedWords',
  9465. LinesToStr([ // statements
  9466. 'rtl.createClass(this, "TObject", null, function () {',
  9467. ' this.$init = function () {',
  9468. ' };',
  9469. ' this.$final = function () {',
  9470. ' };',
  9471. '});',
  9472. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  9473. ' this.$init = function () {',
  9474. ' $mod.TObject.$init.call(this);',
  9475. ' this.Symbol = "";',
  9476. ' };',
  9477. '});',
  9478. 'this.Try = 0;',
  9479. '']),
  9480. LinesToStr([ // $mod.$main
  9481. 'try {',
  9482. ' $mod.Try = 4;',
  9483. '} catch ($e) {',
  9484. ' if ($mod.Exception.isPrototypeOf($e)) {',
  9485. ' var error = $e;',
  9486. ' if (error.Symbol === "") throw error;',
  9487. ' } else throw $e',
  9488. '};',
  9489. '']));
  9490. end;
  9491. procedure TTestModule.TestIfThenRaiseElse;
  9492. begin
  9493. StartProgram(false);
  9494. Add([
  9495. 'type',
  9496. ' TObject = class',
  9497. ' constructor Create;',
  9498. ' end;',
  9499. 'constructor TObject.Create;',
  9500. 'begin',
  9501. 'end;',
  9502. 'var b: boolean;',
  9503. 'begin',
  9504. ' if b then',
  9505. ' raise TObject.Create',
  9506. ' else',
  9507. ' b:=false;',
  9508. '']);
  9509. ConvertProgram;
  9510. CheckSource('TestIfThenRaiseElse',
  9511. LinesToStr([ // statements
  9512. 'rtl.createClass(this, "TObject", null, function () {',
  9513. ' this.$init = function () {',
  9514. ' };',
  9515. ' this.$final = function () {',
  9516. ' };',
  9517. ' this.Create = function () {',
  9518. ' return this;',
  9519. ' };',
  9520. '});',
  9521. 'this.b = false;',
  9522. '']),
  9523. LinesToStr([ // $mod.$main
  9524. 'if ($mod.b) {',
  9525. ' throw $mod.TObject.$create("Create")}',
  9526. ' else $mod.b = false;',
  9527. '']));
  9528. end;
  9529. procedure TTestModule.TestCaseOf;
  9530. begin
  9531. StartProgram(false);
  9532. Add([
  9533. 'const e: longint; external name ''$e'';',
  9534. 'var vI: longint;',
  9535. 'begin',
  9536. ' case vi of',
  9537. ' 1: ;',
  9538. ' 2: vi:=3;',
  9539. ' e: ;',
  9540. ' else',
  9541. ' VI:=4',
  9542. ' end;']);
  9543. ConvertProgram;
  9544. CheckSource('TestCaseOf',
  9545. LinesToStr([ // statements
  9546. 'this.vI = 0;'
  9547. ]),
  9548. LinesToStr([ // $mod.$main
  9549. 'var $tmp = $mod.vI;',
  9550. 'if ($tmp === 1) {}',
  9551. 'else if ($tmp === 2) {',
  9552. ' $mod.vI = 3}',
  9553. ' else if ($tmp === $e) {}',
  9554. 'else {',
  9555. ' $mod.vI = 4;',
  9556. '};'
  9557. ]));
  9558. end;
  9559. procedure TTestModule.TestCaseOf_UseSwitch;
  9560. begin
  9561. StartProgram(false);
  9562. Converter.UseSwitchStatement:=true;
  9563. Add('var Vi: longint;');
  9564. Add('begin');
  9565. Add(' case vi of');
  9566. Add(' 1: ;');
  9567. Add(' 2: VI:=3;');
  9568. Add(' else');
  9569. Add(' vi:=4');
  9570. Add(' end;');
  9571. ConvertProgram;
  9572. CheckSource('TestCaseOf_UseSwitch',
  9573. LinesToStr([ // statements
  9574. 'this.Vi = 0;'
  9575. ]),
  9576. LinesToStr([ // $mod.$main
  9577. 'switch ($mod.Vi) {',
  9578. 'case 1:',
  9579. ' break;',
  9580. 'case 2:',
  9581. ' $mod.Vi = 3;',
  9582. ' break;',
  9583. 'default:',
  9584. ' $mod.Vi = 4;',
  9585. '};'
  9586. ]));
  9587. end;
  9588. procedure TTestModule.TestCaseOfNoElse;
  9589. begin
  9590. StartProgram(false);
  9591. Add('var Vi: longint;');
  9592. Add('begin');
  9593. Add(' case vi of');
  9594. Add(' 1: begin vi:=2; VI:=3; end;');
  9595. Add(' end;');
  9596. ConvertProgram;
  9597. CheckSource('TestCaseOfNoElse',
  9598. LinesToStr([ // statements
  9599. 'this.Vi = 0;'
  9600. ]),
  9601. LinesToStr([ // $mod.$main
  9602. 'var $tmp = $mod.Vi;',
  9603. 'if ($tmp === 1) {',
  9604. ' $mod.Vi = 2;',
  9605. ' $mod.Vi = 3;',
  9606. '};'
  9607. ]));
  9608. end;
  9609. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  9610. begin
  9611. StartProgram(false);
  9612. Converter.UseSwitchStatement:=true;
  9613. Add('var vI: longint;');
  9614. Add('begin');
  9615. Add(' case vi of');
  9616. Add(' 1: begin VI:=2; vi:=3; end;');
  9617. Add(' end;');
  9618. ConvertProgram;
  9619. CheckSource('TestCaseOfNoElse_UseSwitch',
  9620. LinesToStr([ // statements
  9621. 'this.vI = 0;'
  9622. ]),
  9623. LinesToStr([ // $mod.$main
  9624. 'switch ($mod.vI) {',
  9625. 'case 1:',
  9626. ' $mod.vI = 2;',
  9627. ' $mod.vI = 3;',
  9628. ' break;',
  9629. '};'
  9630. ]));
  9631. end;
  9632. procedure TTestModule.TestCaseOfRange;
  9633. begin
  9634. StartProgram(false);
  9635. Add('var vI: longint;');
  9636. Add('begin');
  9637. Add(' case vi of');
  9638. Add(' 1..3: vi:=14;');
  9639. Add(' 4,5: vi:=16;');
  9640. Add(' 6..7,9..10: ;');
  9641. Add(' else ;');
  9642. Add(' end;');
  9643. ConvertProgram;
  9644. CheckSource('TestCaseOfRange',
  9645. LinesToStr([ // statements
  9646. 'this.vI = 0;'
  9647. ]),
  9648. LinesToStr([ // $mod.$main
  9649. 'var $tmp = $mod.vI;',
  9650. 'if (($tmp >= 1) && ($tmp <= 3)){',
  9651. ' $mod.vI = 14',
  9652. '} else if (($tmp === 4) || ($tmp === 5)){',
  9653. ' $mod.vI = 16',
  9654. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  9655. ]));
  9656. end;
  9657. procedure TTestModule.TestCaseOfString;
  9658. begin
  9659. StartProgram(false);
  9660. Add([
  9661. 'var s,h: string;',
  9662. 'begin',
  9663. ' case s of',
  9664. ' ''foo'': s:=h;',
  9665. ' ''a''..''z'': h:=s;',
  9666. ' ''ў'', ''ё'': ;',
  9667. ' ''Б''..''Я'': ;',
  9668. ' end;',
  9669. '']);
  9670. ConvertProgram;
  9671. CheckSource('TestCaseOfString',
  9672. LinesToStr([ // statements
  9673. 'this.s = "";',
  9674. 'this.h = "";',
  9675. '']),
  9676. LinesToStr([ // $mod.$main
  9677. 'var $tmp = $mod.s;',
  9678. 'if ($tmp === "foo") {',
  9679. ' $mod.s = $mod.h}',
  9680. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  9681. ' $mod.h = $mod.s}',
  9682. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  9683. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  9684. '']));
  9685. end;
  9686. procedure TTestModule.TestCaseOfChar;
  9687. begin
  9688. StartProgram(false);
  9689. Add([
  9690. 'var s,h: char;',
  9691. 'begin',
  9692. ' case s of',
  9693. ' ''a''..''z'': h:=s;',
  9694. ' ''ä'': ;',
  9695. ' ''ў'', ''ё'': ;',
  9696. ' ''Б''..''Я'': ;',
  9697. ' end;',
  9698. '']);
  9699. ConvertProgram;
  9700. CheckSource('TestCaseOfString',
  9701. LinesToStr([ // statements
  9702. 'this.s = "\x00";',
  9703. 'this.h = "\x00";',
  9704. '']),
  9705. LinesToStr([ // $mod.$main
  9706. 'var $tmp = $mod.s;',
  9707. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  9708. ' $mod.h = $mod.s}',
  9709. ' else if ($tmp === "ä") {}',
  9710. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  9711. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  9712. '']));
  9713. end;
  9714. procedure TTestModule.TestCaseOfExternalClassConst;
  9715. begin
  9716. StartProgram(false);
  9717. Add([
  9718. '{$modeswitch externalclass}',
  9719. 'type',
  9720. ' TBird = class external name ''Bird''',
  9721. ' const e: longint;',
  9722. ' end;',
  9723. 'var vI: longint;',
  9724. 'begin',
  9725. ' case vi of',
  9726. ' 1: vi:=3;',
  9727. ' TBird.e: ;',
  9728. ' end;']);
  9729. ConvertProgram;
  9730. CheckSource('TestCaseOfExternalClassConst',
  9731. LinesToStr([ // statements
  9732. 'this.vI = 0;'
  9733. ]),
  9734. LinesToStr([ // $mod.$main
  9735. 'var $tmp = $mod.vI;',
  9736. 'if ($tmp === 1) {',
  9737. ' $mod.vI = 3}',
  9738. ' else if ($tmp === Bird.e) ;'
  9739. ]));
  9740. end;
  9741. procedure TTestModule.TestDebugger;
  9742. begin
  9743. StartProgram(false);
  9744. Add([
  9745. 'procedure DoIt;',
  9746. 'begin',
  9747. ' deBugger;',
  9748. ' DeBugger();',
  9749. 'end;',
  9750. 'begin',
  9751. ' Debugger;']);
  9752. ConvertProgram;
  9753. CheckSource('TestDebugger',
  9754. LinesToStr([ // statements
  9755. 'this.DoIt = function () {',
  9756. ' debugger;',
  9757. ' debugger;',
  9758. '};',
  9759. '']),
  9760. LinesToStr([ // $mod.$main
  9761. 'debugger;',
  9762. '']));
  9763. end;
  9764. procedure TTestModule.TestArray_Dynamic;
  9765. begin
  9766. StartProgram(false);
  9767. Add([
  9768. 'type',
  9769. ' TArrayInt = array of longint;',
  9770. 'var',
  9771. ' Arr: TArrayInt;',
  9772. ' i: longint;',
  9773. ' b: boolean;',
  9774. 'begin',
  9775. ' SetLength(arr,3);',
  9776. ' arr[0]:=4;',
  9777. ' arr[1]:=length(arr)+arr[0];',
  9778. ' arr[i]:=5;',
  9779. ' arr[arr[i]]:=arr[6];',
  9780. ' i:=low(arr);',
  9781. ' i:=high(arr);',
  9782. ' b:=Assigned(arr);',
  9783. ' Arr:=default(TArrayInt);']);
  9784. ConvertProgram;
  9785. CheckSource('TestArray_Dynamic',
  9786. LinesToStr([ // statements
  9787. 'this.Arr = [];',
  9788. 'this.i = 0;',
  9789. 'this.b = false;'
  9790. ]),
  9791. LinesToStr([ // $mod.$main
  9792. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  9793. '$mod.Arr[0] = 4;',
  9794. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  9795. '$mod.Arr[$mod.i] = 5;',
  9796. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  9797. '$mod.i = 0;',
  9798. '$mod.i = rtl.length($mod.Arr) - 1;',
  9799. '$mod.b = rtl.length($mod.Arr) > 0;',
  9800. '$mod.Arr = [];',
  9801. '']));
  9802. end;
  9803. procedure TTestModule.TestArray_Dynamic_Nil;
  9804. begin
  9805. StartProgram(false);
  9806. Add('type');
  9807. Add(' TArrayInt = array of longint;');
  9808. Add('var');
  9809. Add(' Arr: TArrayInt;');
  9810. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  9811. Add('begin');
  9812. Add(' arr:=nil;');
  9813. Add(' if arr=nil then;');
  9814. Add(' if nil=arr then;');
  9815. Add(' if arr<>nil then;');
  9816. Add(' if nil<>arr then;');
  9817. Add(' DoIt(nil,nil);');
  9818. ConvertProgram;
  9819. CheckSource('TestArray_Dynamic',
  9820. LinesToStr([ // statements
  9821. 'this.Arr = [];',
  9822. 'this.DoIt = function(i,j){',
  9823. '};'
  9824. ]),
  9825. LinesToStr([ // $mod.$main
  9826. '$mod.Arr = [];',
  9827. 'if (rtl.length($mod.Arr) === 0) ;',
  9828. 'if (rtl.length($mod.Arr) === 0) ;',
  9829. 'if (rtl.length($mod.Arr) > 0) ;',
  9830. 'if (rtl.length($mod.Arr) > 0) ;',
  9831. '$mod.DoIt([],[]);',
  9832. '']));
  9833. end;
  9834. procedure TTestModule.TestArray_DynMultiDimensional;
  9835. begin
  9836. StartProgram(false);
  9837. Add([
  9838. 'type',
  9839. ' TArrayInt = array of longint;',
  9840. ' TArrayArrayInt = array of TArrayInt;',
  9841. 'var',
  9842. ' Arr: TArrayInt;',
  9843. ' Arr2: TArrayArrayInt;',
  9844. ' i: longint;',
  9845. 'begin',
  9846. ' arr2:=nil;',
  9847. ' if arr2=nil then;',
  9848. ' if nil=arr2 then;',
  9849. ' i:=low(arr2);',
  9850. ' i:=low(arr2[1]);',
  9851. ' i:=high(arr2);',
  9852. ' i:=high(arr2[2]);',
  9853. ' arr2[3]:=arr;',
  9854. ' arr2[4][5]:=i;',
  9855. ' i:=arr2[6][7];',
  9856. ' arr2[8,9]:=i;',
  9857. ' i:=arr2[10,11];',
  9858. ' SetLength(arr2,14);',
  9859. ' SetLength(arr2[15],16);']);
  9860. ConvertProgram;
  9861. CheckSource('TestArray_Dynamic',
  9862. LinesToStr([ // statements
  9863. 'this.Arr = [];',
  9864. 'this.Arr2 = [];',
  9865. 'this.i = 0;'
  9866. ]),
  9867. LinesToStr([ // $mod.$main
  9868. '$mod.Arr2 = [];',
  9869. 'if (rtl.length($mod.Arr2) === 0) ;',
  9870. 'if (rtl.length($mod.Arr2) === 0) ;',
  9871. '$mod.i = 0;',
  9872. '$mod.i = 0;',
  9873. '$mod.i = rtl.length($mod.Arr2) - 1;',
  9874. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  9875. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  9876. '$mod.Arr2[4][5] = $mod.i;',
  9877. '$mod.i = $mod.Arr2[6][7];',
  9878. '$mod.Arr2[8][9] = $mod.i;',
  9879. '$mod.i = $mod.Arr2[10][11];',
  9880. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  9881. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  9882. '']));
  9883. end;
  9884. procedure TTestModule.TestArray_DynamicAssign;
  9885. begin
  9886. StartProgram(false);
  9887. Add([
  9888. 'type',
  9889. ' TArrayInt = array of longint;',
  9890. ' TArrayArrayInt = array of TArrayInt;',
  9891. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  9892. 'begin',
  9893. 'end;',
  9894. 'procedure Fly(var a: TArrayInt);',
  9895. 'begin',
  9896. 'end;',
  9897. 'var',
  9898. ' Arr: TArrayInt;',
  9899. ' Arr2: TArrayArrayInt;',
  9900. 'begin',
  9901. ' arr:=nil;',
  9902. ' arr2:=nil;',
  9903. ' arr2[1]:=nil;',
  9904. ' arr2[2]:=arr;',
  9905. ' Run(arr,arr,arr);',
  9906. ' Fly(arr);',
  9907. ' Run(arr2[4],arr2[5],arr2[6]);',
  9908. ' Fly(arr2[7]);',
  9909. '']);
  9910. ConvertProgram;
  9911. CheckSource('TestArray_DynamicAssign',
  9912. LinesToStr([ // statements
  9913. 'this.Run = function (a, b, c) {',
  9914. '};',
  9915. 'this.Fly = function (a) {',
  9916. '};',
  9917. 'this.Arr = [];',
  9918. 'this.Arr2 = [];',
  9919. '']),
  9920. LinesToStr([ // $mod.$main
  9921. '$mod.Arr = [];',
  9922. '$mod.Arr2 = [];',
  9923. '$mod.Arr2[1] = [];',
  9924. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  9925. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  9926. '$mod.Fly({',
  9927. ' p: $mod,',
  9928. ' get: function () {',
  9929. ' return this.p.Arr;',
  9930. ' },',
  9931. ' set: function (v) {',
  9932. ' this.p.Arr = v;',
  9933. ' }',
  9934. '});',
  9935. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  9936. '$mod.Fly({',
  9937. ' a: 7,',
  9938. ' p: $mod.Arr2,',
  9939. ' get: function () {',
  9940. ' return this.p[this.a];',
  9941. ' },',
  9942. ' set: function (v) {',
  9943. ' this.p[this.a] = v;',
  9944. ' }',
  9945. '});',
  9946. '']));
  9947. end;
  9948. procedure TTestModule.TestArray_StaticInt;
  9949. begin
  9950. StartProgram(false);
  9951. Add('type');
  9952. Add(' TArrayInt = array[2..4] of longint;');
  9953. Add('var');
  9954. Add(' Arr: TArrayInt;');
  9955. Add(' Arr2: TArrayInt = (5,6,7);');
  9956. Add(' i: longint;');
  9957. Add(' b: boolean;');
  9958. Add('begin');
  9959. Add(' arr[2]:=4;');
  9960. Add(' arr[3]:=arr[2]+arr[3];');
  9961. Add(' arr[i]:=5;');
  9962. Add(' arr[arr[i]]:=arr[high(arr)];');
  9963. Add(' i:=low(arr);');
  9964. Add(' i:=high(arr);');
  9965. Add(' b:=arr[2]=arr[3];');
  9966. Add(' arr:=default(TArrayInt);');
  9967. ConvertProgram;
  9968. CheckSource('TestArray_StaticInt',
  9969. LinesToStr([ // statements
  9970. 'this.Arr = rtl.arraySetLength(null,0,3);',
  9971. 'this.Arr2 = [5, 6, 7];',
  9972. 'this.i = 0;',
  9973. 'this.b = false;'
  9974. ]),
  9975. LinesToStr([ // $mod.$main
  9976. '$mod.Arr[0] = 4;',
  9977. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  9978. '$mod.Arr[$mod.i-2] = 5;',
  9979. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  9980. '$mod.i = 2;',
  9981. '$mod.i = 4;',
  9982. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9983. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  9984. '']));
  9985. end;
  9986. procedure TTestModule.TestArray_StaticBool;
  9987. begin
  9988. StartProgram(false);
  9989. Add('type');
  9990. Add(' TBools = array[boolean] of boolean;');
  9991. Add(' TBool2 = array[true..true] of boolean;');
  9992. Add('var');
  9993. Add(' Arr: TBools;');
  9994. Add(' Arr2: TBool2;');
  9995. Add(' Arr3: TBools = (true,false);');
  9996. Add(' b: boolean;');
  9997. Add('begin');
  9998. Add(' b:=low(arr);');
  9999. Add(' b:=high(arr);');
  10000. Add(' arr[true]:=false;');
  10001. Add(' arr[false]:=arr[b] or arr[true];');
  10002. Add(' arr[b]:=true;');
  10003. Add(' arr[arr[b]]:=arr[high(arr)];');
  10004. Add(' b:=arr[false]=arr[true];');
  10005. Add(' b:=low(arr2);');
  10006. Add(' b:=high(arr2);');
  10007. Add(' arr2[true]:=true;');
  10008. Add(' arr2[true]:=arr2[true] and arr2[b];');
  10009. Add(' arr2[b]:=false;');
  10010. ConvertProgram;
  10011. CheckSource('TestArray_StaticBool',
  10012. LinesToStr([ // statements
  10013. 'this.Arr = rtl.arraySetLength(null,false,2);',
  10014. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  10015. 'this.Arr3 = [true, false];',
  10016. 'this.b = false;'
  10017. ]),
  10018. LinesToStr([ // $mod.$main
  10019. '$mod.b = false;',
  10020. '$mod.b = true;',
  10021. '$mod.Arr[1] = false;',
  10022. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  10023. '$mod.Arr[+$mod.b] = true;',
  10024. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  10025. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  10026. '$mod.b = true;',
  10027. '$mod.b = true;',
  10028. '$mod.Arr2[0] = true;',
  10029. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  10030. '$mod.Arr2[1-$mod.b] = false;',
  10031. '']));
  10032. end;
  10033. procedure TTestModule.TestArray_StaticChar;
  10034. begin
  10035. StartProgram(false);
  10036. Add([
  10037. 'type',
  10038. ' TChars = array[char] of char;',
  10039. ' TChars2 = array[''a''..''z''] of char;',
  10040. 'var',
  10041. ' Arr: TChars;',
  10042. ' Arr2: TChars2;',
  10043. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  10044. ' Arr4: array[11..13] of char = ''pas'';',
  10045. ' Arr5: array[21..22] of char = ''äö'';',
  10046. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  10047. ' c: char;',
  10048. ' b: boolean;',
  10049. 'begin',
  10050. ' c:=low(arr);',
  10051. ' c:=high(arr);',
  10052. ' arr[''B'']:=''a'';',
  10053. ' arr[''D'']:=arr[c];',
  10054. ' arr[c]:=arr[''d''];',
  10055. ' arr[arr[c]]:=arr[high(arr)];',
  10056. ' b:=arr[low(arr)]=arr[''e''];',
  10057. ' c:=low(arr2);',
  10058. ' c:=high(arr2);',
  10059. ' arr2[''b'']:=''f'';',
  10060. ' arr2[''a'']:=arr2[c];',
  10061. ' arr2[c]:=arr2[''g''];']);
  10062. ConvertProgram;
  10063. CheckSource('TestArray_StaticChar',
  10064. LinesToStr([ // statements
  10065. 'this.Arr = rtl.arraySetLength(null, "\x00", 65536);',
  10066. 'this.Arr2 = rtl.arraySetLength(null, "\x00", 26);',
  10067. 'this.Arr3 = ["p", "a", "s"];',
  10068. 'this.Arr4 = ["p", "a", "s"];',
  10069. 'this.Arr5 = ["ä", "ö"];',
  10070. 'this.Arr6 = ["ä", "ö"];',
  10071. 'this.c = "\x00";',
  10072. 'this.b = false;',
  10073. '']),
  10074. LinesToStr([ // $mod.$main
  10075. '$mod.c = "\x00";',
  10076. '$mod.c = "\uFFFF";',
  10077. '$mod.Arr[66] = "a";',
  10078. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  10079. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  10080. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  10081. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  10082. '$mod.c = "a";',
  10083. '$mod.c = "z";',
  10084. '$mod.Arr2[1] = "f";',
  10085. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  10086. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  10087. '']));
  10088. end;
  10089. procedure TTestModule.TestArray_StaticMultiDim;
  10090. begin
  10091. StartProgram(false);
  10092. Add([
  10093. 'type',
  10094. ' TArrayInt = array[1..3] of longint;',
  10095. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  10096. ' TArrayArrayArrayInt = array[7..8] of TArrayArrayInt;',
  10097. ' TArrayDim2Int = array[1..2,1..3] of longint;',
  10098. ' TArrayDim3Int = array[1..2,1..3,1..4] of longint;',
  10099. ' TArrayDim4Int = array[1..2,1..3,1..4,1..5] of longint;',
  10100. 'var',
  10101. ' Arr: TArrayInt;',
  10102. ' Arr2: TArrayArrayInt;',
  10103. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  10104. ' Arr4: TArrayArrayInt;',
  10105. ' ArrDim2: TArrayDim2Int;',
  10106. ' ArrDim3: TArrayDim3Int;',
  10107. ' ArrDim4: TArrayDim4Int;',
  10108. ' i: longint;',
  10109. 'begin',
  10110. ' i:=low(arr);',
  10111. ' i:=low(arr2);',
  10112. ' i:=low(arr2[5]);',
  10113. ' i:=high(arr);',
  10114. ' i:=high(arr2);',
  10115. ' i:=high(arr2[6]);',
  10116. ' arr2[5]:=arr;',
  10117. ' arr2[6][2]:=i;',
  10118. ' i:=arr2[6][3];',
  10119. ' arr2[6,3]:=i;',
  10120. ' i:=arr2[5,2];',
  10121. ' arr2:=arr2;',// clone multi dim static array
  10122. ' arr3:=arr3;',// clone anonymous multi dim static array
  10123. ' arr4:=arr4;',
  10124. ' Arr:=Arr;',
  10125. ' ArrDim2:=ArrDim2;',
  10126. ' ArrDim3:=ArrDim3;',
  10127. ' ArrDim4:=ArrDim4;',
  10128. '']);
  10129. ConvertProgram;
  10130. CheckSource('TestArray_StaticMultiDim',
  10131. LinesToStr([ // statements
  10132. 'this.TArrayArrayInt$clone = function (a) {',
  10133. ' var b = [];',
  10134. ' b.length = 2;',
  10135. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10136. ' return b;',
  10137. '};',
  10138. 'this.TArrayArrayArrayInt$clone = function (a) {',
  10139. ' var b = [];',
  10140. ' b.length = 2;',
  10141. ' for (var c = 0; c < 2; c++) b[c] = $mod.TArrayArrayInt$clone(a[c]);',
  10142. ' return b;',
  10143. '};',
  10144. 'this.TArrayDim2Int$clone = function (a) {',
  10145. ' var b = [];',
  10146. ' b.length = 2;',
  10147. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10148. ' return b;',
  10149. '};',
  10150. 'this.TArrayDim3Int$clone = function (a) {',
  10151. ' var b = [];',
  10152. ' b.length = 2;',
  10153. ' for (var c = 0; c < 2; c++) {',
  10154. ' var d = b[c] = [];',
  10155. ' d.length = 3;',
  10156. ' var e = a[c];',
  10157. ' for (var f = 0; f < 3; f++) d[f] = e[f].slice(0);',
  10158. ' };',
  10159. ' return b;',
  10160. '};',
  10161. 'this.TArrayDim4Int$clone = function (a) {',
  10162. ' var b = [];',
  10163. ' b.length = 2;',
  10164. ' for (var c = 0; c < 2; c++) {',
  10165. ' var d = b[c] = [];',
  10166. ' d.length = 3;',
  10167. ' var e = a[c];',
  10168. ' for (var f = 0; f < 3; f++) {',
  10169. ' var g = d[f] = [];',
  10170. ' g.length = 4;',
  10171. ' var h = e[f];',
  10172. ' for (var i = 0; i < 4; i++) g[i] = h[i].slice(0);',
  10173. ' };',
  10174. ' };',
  10175. ' return b;',
  10176. '};',
  10177. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  10178. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  10179. 'this.Arr3$a$clone = function (a) {',
  10180. ' var b = [];',
  10181. ' b.length = 2;',
  10182. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10183. ' return b;',
  10184. '};',
  10185. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  10186. 'this.Arr4 = rtl.arraySetLength(null, 0, 2, 3);',
  10187. 'this.ArrDim2 = rtl.arraySetLength(null, 0, 2, 3);',
  10188. 'this.ArrDim3 = rtl.arraySetLength(null, 0, 2, 3, 4);',
  10189. 'this.ArrDim4 = rtl.arraySetLength(',
  10190. ' null,',
  10191. ' 0,',
  10192. ' 2,',
  10193. ' 3,',
  10194. ' 4,',
  10195. ' 5',
  10196. ');',
  10197. 'this.i = 0;'
  10198. ]),
  10199. LinesToStr([ // $mod.$main
  10200. '$mod.i = 1;',
  10201. '$mod.i = 5;',
  10202. '$mod.i = 1;',
  10203. '$mod.i = 3;',
  10204. '$mod.i = 6;',
  10205. '$mod.i = 3;',
  10206. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  10207. '$mod.Arr2[1][1] = $mod.i;',
  10208. '$mod.i = $mod.Arr2[1][2];',
  10209. '$mod.Arr2[1][2] = $mod.i;',
  10210. '$mod.i = $mod.Arr2[0][1];',
  10211. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  10212. '$mod.Arr3 = $mod.Arr3$a$clone($mod.Arr3);',
  10213. '$mod.Arr4 = $mod.TArrayArrayInt$clone($mod.Arr4);',
  10214. '$mod.Arr = $mod.Arr.slice(0);',
  10215. '$mod.ArrDim2 = $mod.TArrayDim2Int$clone($mod.ArrDim2);',
  10216. '$mod.ArrDim3 = $mod.TArrayDim3Int$clone($mod.ArrDim3);',
  10217. '$mod.ArrDim4 = $mod.TArrayDim4Int$clone($mod.ArrDim4);',
  10218. '']));
  10219. end;
  10220. procedure TTestModule.TestArray_StaticInFunction;
  10221. begin
  10222. StartProgram(false);
  10223. Add([
  10224. 'const TArrayInt = 3;',
  10225. 'const TArrayArrayInt = 4;',
  10226. 'procedure DoIt;',
  10227. 'type',
  10228. ' TArrayInt = array[1..3] of longint;',
  10229. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  10230. 'var',
  10231. ' Arr: TArrayInt;',
  10232. ' Arr2: TArrayArrayInt;',
  10233. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  10234. ' i: longint;',
  10235. 'begin',
  10236. ' arr2[5]:=arr;',
  10237. ' arr2:=arr2;',// clone multi dim static array
  10238. ' arr3:=arr3;',// clone multi dim anonymous static array
  10239. 'end;',
  10240. 'begin',
  10241. '']);
  10242. ConvertProgram;
  10243. CheckSource('TestArray_StaticInFunction',
  10244. LinesToStr([ // statements
  10245. 'this.TArrayInt = 3;',
  10246. 'this.TArrayArrayInt = 4;',
  10247. 'var TArrayArrayInt$1$clone = function (a) {',
  10248. ' var b = [];',
  10249. ' b.length = 2;',
  10250. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10251. ' return b;',
  10252. '};',
  10253. 'var Arr3$a$clone = function (a) {',
  10254. ' var b = [];',
  10255. ' b.length = 2;',
  10256. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10257. ' return b;',
  10258. '};',
  10259. 'this.DoIt = function () {',
  10260. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  10261. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  10262. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  10263. ' var i = 0;',
  10264. ' Arr2[0] = Arr.slice(0);',
  10265. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  10266. ' Arr3 = Arr3$a$clone(Arr3);',
  10267. '};',
  10268. '']),
  10269. LinesToStr([ // $mod.$main
  10270. '']));
  10271. end;
  10272. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  10273. begin
  10274. StartProgram(false);
  10275. Add([
  10276. 'type',
  10277. ' TArrayInt = array[1..3,1..2] of longint;',
  10278. 'var',
  10279. ' a,b: TArrayInt;',
  10280. 'begin',
  10281. ' if a=b then ;',
  10282. '']);
  10283. SetExpectedPasResolverError('compare static array is not supported',
  10284. nXIsNotSupported);
  10285. ConvertProgram;
  10286. end;
  10287. procedure TTestModule.TestArrayOfRecord;
  10288. begin
  10289. StartProgram(false);
  10290. Add([
  10291. 'type',
  10292. ' TRec = record',
  10293. ' Int: longint;',
  10294. ' end;',
  10295. ' TArrayRec = array of TRec;',
  10296. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  10297. 'begin',
  10298. 'end;',
  10299. 'var',
  10300. ' Arr: TArrayRec;',
  10301. ' r: TRec;',
  10302. ' i: longint;',
  10303. 'begin',
  10304. ' SetLength(arr,3);',
  10305. ' arr[0].int:=4;',
  10306. ' arr[1].int:=length(arr)+arr[2].int;',
  10307. ' arr[arr[i].int].int:=arr[5].int;',
  10308. ' arr[7]:=r;',
  10309. ' r:=arr[8];',
  10310. ' i:=low(arr);',
  10311. ' i:=high(arr);',
  10312. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  10313. ConvertProgram;
  10314. CheckSource('TestArrayOfRecord',
  10315. LinesToStr([ // statements
  10316. 'rtl.recNewT(this, "TRec", function () {',
  10317. ' this.Int = 0;',
  10318. ' this.$eq = function (b) {',
  10319. ' return this.Int === b.Int;',
  10320. ' };',
  10321. ' this.$assign = function (s) {',
  10322. ' this.Int = s.Int;',
  10323. ' return this;',
  10324. ' };',
  10325. '});',
  10326. 'this.DoIt = function (vd, vc, vv) {',
  10327. '};',
  10328. 'this.Arr = [];',
  10329. 'this.r = this.TRec.$new();',
  10330. 'this.i = 0;'
  10331. ]),
  10332. LinesToStr([ // $mod.$main
  10333. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  10334. '$mod.Arr[0].Int = 4;',
  10335. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  10336. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  10337. '$mod.Arr[7].$assign($mod.r);',
  10338. '$mod.r.$assign($mod.Arr[8]);',
  10339. '$mod.i = 0;',
  10340. '$mod.i = rtl.length($mod.Arr)-1;',
  10341. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  10342. '']));
  10343. end;
  10344. procedure TTestModule.TestArray_StaticRecord;
  10345. begin
  10346. StartProgram(false);
  10347. Add([
  10348. 'type',
  10349. ' TRec = record',
  10350. ' Int: longint;',
  10351. ' end;',
  10352. ' TArrayRec = array[1..2] of TRec;',
  10353. 'var',
  10354. ' Arr: TArrayRec;',
  10355. 'begin',
  10356. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  10357. '']);
  10358. ConvertProgram;
  10359. CheckSource('TestArray_StaticRecord',
  10360. LinesToStr([ // statements
  10361. 'rtl.recNewT(this, "TRec", function () {',
  10362. ' this.Int = 0;',
  10363. ' this.$eq = function (b) {',
  10364. ' return this.Int === b.Int;',
  10365. ' };',
  10366. ' this.$assign = function (s) {',
  10367. ' this.Int = s.Int;',
  10368. ' return this;',
  10369. ' };',
  10370. '});',
  10371. 'this.TArrayRec$clone = function (a) {',
  10372. ' var b = [];',
  10373. ' b.length = 2;',
  10374. ' for (var c = 0; c < 2; c++) b[c] = $mod.TRec.$clone(a[c]);',
  10375. ' return b;',
  10376. '};',
  10377. 'this.Arr = rtl.arraySetLength(null, this.TRec, 2);',
  10378. '']),
  10379. LinesToStr([ // $mod.$main
  10380. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  10381. end;
  10382. procedure TTestModule.TestArrayOfSet;
  10383. begin
  10384. StartProgram(false);
  10385. Add([
  10386. 'type',
  10387. ' TFlag = (big,small);',
  10388. ' TSetOfFlag = set of tflag;',
  10389. ' TArrayFlag = array of TSetOfFlag;',
  10390. 'procedure DoIt(const a: Tarrayflag);',
  10391. 'begin',
  10392. 'end;',
  10393. 'var',
  10394. ' f: TFlag;',
  10395. ' s: TSetOfFlag;',
  10396. ' Arr: TArrayFlag;',
  10397. ' i: longint;',
  10398. 'begin',
  10399. ' SetLength(arr,3);',
  10400. ' arr[0]:=s;',
  10401. ' arr[1]:=[big];',
  10402. ' arr[2]:=[big]+s;',
  10403. ' arr[3]:=s+[big];',
  10404. ' arr[4]:=arr[5];',
  10405. ' s:=arr[6];',
  10406. ' i:=low(arr);',
  10407. ' i:=high(arr);',
  10408. ' DoIt(arr);',
  10409. ' DoIt([s]);',
  10410. ' DoIt([[],s]);',
  10411. ' DoIt([s,[]]);',
  10412. '']);
  10413. ConvertProgram;
  10414. CheckSource('TestArrayOfSet',
  10415. LinesToStr([ // statements
  10416. 'this.TFlag = {',
  10417. ' "0": "big",',
  10418. ' big: 0,',
  10419. ' "1": "small",',
  10420. ' small: 1',
  10421. '};',
  10422. 'this.DoIt = function (a) {',
  10423. '};',
  10424. 'this.f = 0;',
  10425. 'this.s = {};',
  10426. 'this.Arr = [];',
  10427. 'this.i = 0;',
  10428. '']),
  10429. LinesToStr([ // $mod.$main
  10430. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  10431. '$mod.Arr[0] = rtl.refSet($mod.s);',
  10432. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  10433. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  10434. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  10435. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  10436. '$mod.s = rtl.refSet($mod.Arr[6]);',
  10437. '$mod.i = 0;',
  10438. '$mod.i = rtl.length($mod.Arr) - 1;',
  10439. '$mod.DoIt($mod.Arr);',
  10440. '$mod.DoIt([rtl.refSet($mod.s)]);',
  10441. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  10442. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  10443. '']));
  10444. end;
  10445. procedure TTestModule.TestArray_DynAsParam;
  10446. begin
  10447. StartProgram(false);
  10448. Add([
  10449. 'type integer = longint;',
  10450. 'type TArrInt = array of integer;',
  10451. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  10452. 'var vJ: TArrInt;',
  10453. 'begin',
  10454. ' vg:=vg;',
  10455. ' vj:=vh;',
  10456. ' vi:=vi;',
  10457. ' doit(vg,vg,vg);',
  10458. ' doit(vh,vh,vj);',
  10459. ' doit(vi,vi,vi);',
  10460. ' doit(vj,vj,vj);',
  10461. 'end;',
  10462. 'var i: TArrInt;',
  10463. 'begin',
  10464. ' doit(i,i,i);']);
  10465. ConvertProgram;
  10466. CheckSource('TestArray_DynAsParams',
  10467. LinesToStr([ // statements
  10468. 'this.DoIt = function (vG,vH,vI) {',
  10469. ' var vJ = [];',
  10470. ' vG = rtl.arrayRef(vG);',
  10471. ' vJ = rtl.arrayRef(vH);',
  10472. ' vI.set(rtl.arrayRef(vI.get()));',
  10473. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  10474. ' get: function () {',
  10475. ' return vG;',
  10476. ' },',
  10477. ' set: function (v) {',
  10478. ' vG = v;',
  10479. ' }',
  10480. ' });',
  10481. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  10482. ' get: function () {',
  10483. ' return vJ;',
  10484. ' },',
  10485. ' set: function (v) {',
  10486. ' vJ = v;',
  10487. ' }',
  10488. ' });',
  10489. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  10490. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  10491. ' get: function () {',
  10492. ' return vJ;',
  10493. ' },',
  10494. ' set: function (v) {',
  10495. ' vJ = v;',
  10496. ' }',
  10497. ' });',
  10498. '};',
  10499. 'this.i = [];'
  10500. ]),
  10501. LinesToStr([
  10502. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  10503. ' p: $mod,',
  10504. ' get: function () {',
  10505. ' return this.p.i;',
  10506. ' },',
  10507. ' set: function (v) {',
  10508. ' this.p.i = v;',
  10509. ' }',
  10510. '});'
  10511. ]));
  10512. end;
  10513. procedure TTestModule.TestArray_StaticAsParam;
  10514. begin
  10515. StartProgram(false);
  10516. Add([
  10517. 'type integer = longint;',
  10518. 'type TArrInt = array[1..2] of integer;',
  10519. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  10520. 'var vJ: TArrInt;',
  10521. 'begin',
  10522. ' vg:=vg;',
  10523. ' vj:=vh;',
  10524. ' vi:=vi;',
  10525. ' doit(vg,vg,vg);',
  10526. ' doit(vh,vh,vj);',
  10527. ' doit(vi,vi,vi);',
  10528. ' doit(vj,vj,vj);',
  10529. 'end;',
  10530. 'var i: TArrInt;',
  10531. 'begin',
  10532. ' doit(i,i,i);']);
  10533. ConvertProgram;
  10534. CheckSource('TestArray_StaticAsParams',
  10535. LinesToStr([ // statements
  10536. 'this.DoIt = function (vG,vH,vI) {',
  10537. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  10538. ' vG = vG.slice(0);',
  10539. ' vJ = vH.slice(0);',
  10540. ' vI.set(vI.get().slice(0));',
  10541. ' $mod.DoIt(vG.slice(0), vG, {',
  10542. ' get: function () {',
  10543. ' return vG;',
  10544. ' },',
  10545. ' set: function (v) {',
  10546. ' vG = v;',
  10547. ' }',
  10548. ' });',
  10549. ' $mod.DoIt(vH.slice(0), vH, {',
  10550. ' get: function () {',
  10551. ' return vJ;',
  10552. ' },',
  10553. ' set: function (v) {',
  10554. ' vJ = v;',
  10555. ' }',
  10556. ' });',
  10557. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  10558. ' $mod.DoIt(vJ.slice(0), vJ, {',
  10559. ' get: function () {',
  10560. ' return vJ;',
  10561. ' },',
  10562. ' set: function (v) {',
  10563. ' vJ = v;',
  10564. ' }',
  10565. ' });',
  10566. '};',
  10567. 'this.i = rtl.arraySetLength(null, 0, 2);'
  10568. ]),
  10569. LinesToStr([
  10570. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  10571. ' p: $mod,',
  10572. ' get: function () {',
  10573. ' return this.p.i;',
  10574. ' },',
  10575. ' set: function (v) {',
  10576. ' this.p.i = v;',
  10577. ' }',
  10578. '});'
  10579. ]));
  10580. end;
  10581. procedure TTestModule.TestArrayElement_AsParams;
  10582. begin
  10583. StartProgram(false);
  10584. Add('type integer = longint;');
  10585. Add('type TArrayInt = array of integer;');
  10586. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  10587. Add('var vJ: tarrayint;');
  10588. Add('begin');
  10589. Add(' vi:=vi;');
  10590. Add(' doit(vi,vi,vi);');
  10591. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  10592. Add('end;');
  10593. Add('var a: TArrayInt;');
  10594. Add('begin');
  10595. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  10596. ConvertProgram;
  10597. CheckSource('TestArrayElement_AsParams',
  10598. LinesToStr([ // statements
  10599. 'this.DoIt = function (vG,vH,vI) {',
  10600. ' var vJ = [];',
  10601. ' vI.set(vI.get());',
  10602. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  10603. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  10604. ' a:1+3,',
  10605. ' p:vJ,',
  10606. ' get: function () {',
  10607. ' return this.p[this.a];',
  10608. ' },',
  10609. ' set: function (v) {',
  10610. ' this.p[this.a] = v;',
  10611. ' }',
  10612. ' });',
  10613. '};',
  10614. 'this.a = [];'
  10615. ]),
  10616. LinesToStr([
  10617. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  10618. ' a: 1+6,',
  10619. ' p: $mod.a,',
  10620. ' get: function () {',
  10621. ' return this.p[this.a];',
  10622. ' },',
  10623. ' set: function (v) {',
  10624. ' this.p[this.a] = v;',
  10625. ' }',
  10626. '});'
  10627. ]));
  10628. end;
  10629. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  10630. begin
  10631. StartProgram(false);
  10632. Add('type Integer = longint;');
  10633. Add('type TArrayInt = array of integer;');
  10634. Add('function GetArr(vB: integer = 0): tarrayint;');
  10635. Add('begin');
  10636. Add('end;');
  10637. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10638. Add('begin');
  10639. Add('end;');
  10640. Add('begin');
  10641. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  10642. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  10643. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  10644. ConvertProgram;
  10645. CheckSource('TestArrayElementFromFuncResult_AsParams',
  10646. LinesToStr([ // statements
  10647. 'this.GetArr = function (vB) {',
  10648. ' var Result = [];',
  10649. ' return Result;',
  10650. '};',
  10651. 'this.DoIt = function (vG,vH,vI) {',
  10652. '};'
  10653. ]),
  10654. LinesToStr([
  10655. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  10656. ' a: 1+3,',
  10657. ' p: $mod.GetArr(0),',
  10658. ' get: function () {',
  10659. ' return this.p[this.a];',
  10660. ' },',
  10661. ' set: function (v) {',
  10662. ' this.p[this.a] = v;',
  10663. ' }',
  10664. '});',
  10665. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  10666. ' a: 2+3,',
  10667. ' p: $mod.GetArr(0),',
  10668. ' get: function () {',
  10669. ' return this.p[this.a];',
  10670. ' },',
  10671. ' set: function (v) {',
  10672. ' this.p[this.a] = v;',
  10673. ' }',
  10674. '});',
  10675. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  10676. ' a: 3+3,',
  10677. ' p: $mod.GetArr(9),',
  10678. ' get: function () {',
  10679. ' return this.p[this.a];',
  10680. ' },',
  10681. ' set: function (v) {',
  10682. ' this.p[this.a] = v;',
  10683. ' }',
  10684. '});',
  10685. '']));
  10686. end;
  10687. procedure TTestModule.TestArrayEnumTypeRange;
  10688. begin
  10689. StartProgram(false);
  10690. Add([
  10691. 'type',
  10692. ' TEnum = (red,blue);',
  10693. ' TEnumArray = array[TEnum] of longint;',
  10694. 'var',
  10695. ' e: TEnum;',
  10696. ' i: longint;',
  10697. ' a: TEnumArray;',
  10698. ' numbers: TEnumArray = (1,2);',
  10699. ' names: array[TEnum] of string = (''red'',''blue'');',
  10700. 'begin',
  10701. ' e:=low(a);',
  10702. ' e:=high(a);',
  10703. ' i:=a[red];',
  10704. ' a[e]:=a[e];']);
  10705. ConvertProgram;
  10706. CheckSource('TestArrayEnumTypeRange',
  10707. LinesToStr([ // statements
  10708. ' this.TEnum = {',
  10709. ' "0": "red",',
  10710. ' red: 0,',
  10711. ' "1": "blue",',
  10712. ' blue: 1',
  10713. '};',
  10714. 'this.e = 0;',
  10715. 'this.i = 0;',
  10716. 'this.a = rtl.arraySetLength(null,0,2);',
  10717. 'this.numbers = [1, 2];',
  10718. 'this.names = ["red", "blue"];',
  10719. '']),
  10720. LinesToStr([ // $mod.$main
  10721. '$mod.e = $mod.TEnum.red;',
  10722. '$mod.e = $mod.TEnum.blue;',
  10723. '$mod.i = $mod.a[$mod.TEnum.red];',
  10724. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  10725. '']));
  10726. end;
  10727. procedure TTestModule.TestArray_SetLengthOutArg;
  10728. begin
  10729. StartProgram(false);
  10730. Add([
  10731. 'type TArrInt = array of longint;',
  10732. 'procedure DoIt(out a: TArrInt);',
  10733. 'begin',
  10734. ' SetLength(a,2);',
  10735. 'end;',
  10736. 'begin',
  10737. '']);
  10738. ConvertProgram;
  10739. CheckSource('TestArray_SetLengthOutArg',
  10740. LinesToStr([ // statements
  10741. 'this.DoIt = function (a) {',
  10742. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  10743. '};',
  10744. '']),
  10745. LinesToStr([
  10746. '']));
  10747. end;
  10748. procedure TTestModule.TestArray_SetLengthProperty;
  10749. begin
  10750. StartProgram(false);
  10751. Add('type');
  10752. Add(' TArrInt = array of longint;');
  10753. Add(' TObject = class');
  10754. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  10755. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  10756. Add(' property Colors: TArrInt read GetColors write SetColors;');
  10757. Add(' end;');
  10758. Add('var Obj: TObject;');
  10759. Add('begin');
  10760. Add(' SetLength(Obj.Colors,2);');
  10761. ConvertProgram;
  10762. CheckSource('TestArray_SetLengthProperty',
  10763. LinesToStr([ // statements
  10764. 'rtl.createClass(this, "TObject", null, function () {',
  10765. ' this.$init = function () {',
  10766. ' };',
  10767. ' this.$final = function () {',
  10768. ' };',
  10769. '});',
  10770. 'this.Obj = null;',
  10771. '']),
  10772. LinesToStr([
  10773. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  10774. '']));
  10775. end;
  10776. procedure TTestModule.TestArray_SetLengthMultiDim;
  10777. begin
  10778. StartProgram(false);
  10779. Add([
  10780. 'type',
  10781. ' TArrArrInt = array of array of longint;',
  10782. ' TArrStaInt = array of array[1..2] of longint;',
  10783. 'var',
  10784. ' a: TArrArrInt;',
  10785. ' b: TArrStaInt;',
  10786. 'begin',
  10787. ' SetLength(a,2);',
  10788. ' SetLength(a,3,4);',
  10789. ' SetLength(b,5);',
  10790. '']);
  10791. ConvertProgram;
  10792. CheckSource('TestArray_SetLengthMultiDim',
  10793. LinesToStr([ // statements
  10794. 'this.a = [];',
  10795. 'this.b = [];',
  10796. '']),
  10797. LinesToStr([
  10798. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  10799. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  10800. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  10801. '']));
  10802. end;
  10803. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  10804. begin
  10805. StartProgram(false);
  10806. Add([
  10807. 'type',
  10808. ' TStaArr1 = array[1..3] of boolean;',
  10809. //' TStaArr2 = array[5..6] of TStaArr1;',
  10810. ' TDynArr1StaArr1 = array of TStaArr1;',
  10811. //' TDynArr1StaArr2 = array of TStaArr2;',
  10812. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  10813. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  10814. 'var',
  10815. ' DynArr1StaArr1: TDynArr1StaArr1;',
  10816. //' DynArr1StaArr2: TDynArr1StaArr1;',
  10817. ' DynArr2StaArr1: TDynArr2StaArr1;',
  10818. //' DynArr2StaArr2: TDynArr2StaArr2;',
  10819. 'begin',
  10820. ' SetLength(DynArr1StaArr1,11);',
  10821. ' SetLength(DynArr2StaArr1,12);',
  10822. ' SetLength(DynArr2StaArr1[13],14);',
  10823. ' SetLength(DynArr2StaArr1,15,16);',
  10824. //' SetLength(DynArr1StaArr2,21);',
  10825. //' SetLength(DynArr2StaArr2,22);',
  10826. //' SetLength(DynArr2StaArr2[23],24);',
  10827. //' SetLength(DynArr2StaArr2,25,26);',
  10828. '']);
  10829. ConvertProgram;
  10830. CheckSource('TestArray_DynOfStatic',
  10831. LinesToStr([ // statements
  10832. 'this.DynArr1StaArr1 = [];',
  10833. 'this.DynArr2StaArr1 = [];',
  10834. '']),
  10835. LinesToStr([ // $mod.$main
  10836. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  10837. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  10838. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  10839. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  10840. ' $mod.DynArr2StaArr1,',
  10841. ' false,',
  10842. ' 15,',
  10843. ' 16,',
  10844. ' "s",',
  10845. ' 3',
  10846. ');',
  10847. '']));
  10848. end;
  10849. procedure TTestModule.TestArray_OpenArrayOfString;
  10850. begin
  10851. StartProgram(false);
  10852. Add('procedure DoIt(const a: array of String);');
  10853. Add('var');
  10854. Add(' i: longint;');
  10855. Add(' s: string;');
  10856. Add('begin');
  10857. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  10858. Add('end;');
  10859. Add('var s: string;');
  10860. Add('begin');
  10861. Add(' DoIt([]);');
  10862. Add(' DoIt([s,''foo'','''',s+s]);');
  10863. ConvertProgram;
  10864. CheckSource('TestArray_OpenArrayOfString',
  10865. LinesToStr([ // statements
  10866. 'this.DoIt = function (a) {',
  10867. ' var i = 0;',
  10868. ' var s = "";',
  10869. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  10870. ' i = $l;',
  10871. ' s = a[rtl.length(a) - i - 1];',
  10872. ' };',
  10873. '};',
  10874. 'this.s = "";',
  10875. '']),
  10876. LinesToStr([
  10877. '$mod.DoIt([]);',
  10878. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  10879. '']));
  10880. end;
  10881. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  10882. begin
  10883. StartProgram(false);
  10884. Add([
  10885. 'type TArr = array of char;',
  10886. 'var',
  10887. ' c: char;',
  10888. ' s: string;',
  10889. ' a: TArr;',
  10890. 'procedure Run(const a: array of char);',
  10891. 'begin',
  10892. ' Run(c);',
  10893. ' Run(s);',
  10894. 'end;',
  10895. 'begin',
  10896. ' a:=c;',
  10897. ' a:=s;',
  10898. ' a:=#13;',
  10899. ' a:=''Foo'';',
  10900. ' Run(c);',
  10901. ' Run(s);',
  10902. '']);
  10903. ConvertProgram;
  10904. CheckSource('TestArray_ArrayOfCharAssignString',
  10905. LinesToStr([ // statements
  10906. 'this.c = "\x00";',
  10907. 'this.s = "";',
  10908. 'this.a = [];',
  10909. 'this.Run = function (a) {',
  10910. ' $mod.Run($mod.c.split(""));',
  10911. ' $mod.Run($mod.s.split(""));',
  10912. '};',
  10913. '']),
  10914. LinesToStr([
  10915. '$mod.a = $mod.c.split("");',
  10916. '$mod.a = $mod.s.split("");',
  10917. '$mod.a = "\r".split("");',
  10918. '$mod.a = "Foo".split("");',
  10919. '$mod.Run($mod.c.split(""));',
  10920. '$mod.Run($mod.s.split(""));',
  10921. '']));
  10922. end;
  10923. procedure TTestModule.TestArray_ConstRef;
  10924. begin
  10925. StartProgram(false);
  10926. Add([
  10927. 'type TArr = array of word;',
  10928. 'procedure Run(constref a: TArr);',
  10929. 'begin',
  10930. 'end;',
  10931. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  10932. 'var l: TArr;',
  10933. 'begin',
  10934. ' Run(l);',
  10935. ' Run(a);',
  10936. ' Run(b);',
  10937. ' Run(c);',
  10938. ' Run(d);',
  10939. ' Run(e);',
  10940. 'end;',
  10941. 'begin',
  10942. '']);
  10943. ConvertProgram;
  10944. CheckResolverUnexpectedHints();
  10945. CheckSource('TestArray_ConstRef',
  10946. LinesToStr([ // statements
  10947. 'this.Run = function (a) {',
  10948. '};',
  10949. 'this.Fly = function (a, b, c, d, e) {',
  10950. ' var l = [];',
  10951. ' $mod.Run(l);',
  10952. ' $mod.Run(a);',
  10953. ' $mod.Run(b.get());',
  10954. ' $mod.Run(c.get());',
  10955. ' $mod.Run(d);',
  10956. ' $mod.Run(e);',
  10957. '};',
  10958. '']),
  10959. LinesToStr([
  10960. '']));
  10961. end;
  10962. procedure TTestModule.TestArray_Concat;
  10963. begin
  10964. StartProgram(false);
  10965. Add([
  10966. 'type',
  10967. ' integer = longint;',
  10968. ' TFlag = (big,small);',
  10969. ' TFlags = set of TFlag;',
  10970. ' TRec = record',
  10971. ' i: integer;',
  10972. ' end;',
  10973. ' TArrInt = array of integer;',
  10974. ' TArrRec = array of TRec;',
  10975. ' TArrFlag = array of TFlag;',
  10976. ' TArrSet = array of TFlags;',
  10977. ' TArrJSValue = array of jsvalue;',
  10978. 'var',
  10979. ' ArrInt1, ArrInt2: tarrint;',
  10980. ' ArrRec1, ArrRec2: tarrrec;',
  10981. ' ArrFlag1, ArrFlag2: tarrflag;',
  10982. ' ArrSet1, ArrSet2: tarrset;',
  10983. ' ArrJSValue1, ArrJSValue2: tarrjsvalue;',
  10984. 'begin',
  10985. ' arrint1:=concat(arrint2);',
  10986. ' arrint1:=concat(arrint2,arrint2);',
  10987. ' arrint1:=concat(arrint2,arrint2,arrint2);',
  10988. ' arrrec1:=concat(arrrec2);',
  10989. ' arrrec1:=concat(arrrec2,arrrec2);',
  10990. ' arrrec1:=concat(arrrec2,arrrec2,arrrec2);',
  10991. ' arrset1:=concat(arrset2);',
  10992. ' arrset1:=concat(arrset2,arrset2);',
  10993. ' arrset1:=concat(arrset2,arrset2,arrset2);',
  10994. ' arrjsvalue1:=concat(arrjsvalue2);',
  10995. ' arrjsvalue1:=concat(arrjsvalue2,arrjsvalue2);',
  10996. ' arrjsvalue1:=concat(arrjsvalue2,arrjsvalue2,arrjsvalue2);',
  10997. ' arrint1:=concat([1],arrint2);',
  10998. ' arrflag1:=concat([big]);',
  10999. ' arrflag1:=concat([big],arrflag2);',
  11000. ' arrflag1:=concat(arrflag2,[small]);',
  11001. '']);
  11002. ConvertProgram;
  11003. CheckSource('TestArray_Concat',
  11004. LinesToStr([ // statements
  11005. 'this.TFlag = {',
  11006. ' "0": "big",',
  11007. ' big: 0,',
  11008. ' "1": "small",',
  11009. ' small: 1',
  11010. '};',
  11011. 'rtl.recNewT(this, "TRec", function () {',
  11012. ' this.i = 0;',
  11013. ' this.$eq = function (b) {',
  11014. ' return this.i === b.i;',
  11015. ' };',
  11016. ' this.$assign = function (s) {',
  11017. ' this.i = s.i;',
  11018. ' return this;',
  11019. ' };',
  11020. '});',
  11021. 'this.ArrInt1 = [];',
  11022. 'this.ArrInt2 = [];',
  11023. 'this.ArrRec1 = [];',
  11024. 'this.ArrRec2 = [];',
  11025. 'this.ArrFlag1 = [];',
  11026. 'this.ArrFlag2 = [];',
  11027. 'this.ArrSet1 = [];',
  11028. 'this.ArrSet2 = [];',
  11029. 'this.ArrJSValue1 = [];',
  11030. 'this.ArrJSValue2 = [];',
  11031. '']),
  11032. LinesToStr([ // $mod.$main
  11033. '$mod.ArrInt1 = rtl.arrayRef($mod.ArrInt2);',
  11034. '$mod.ArrInt1 = rtl.arrayConcatN($mod.ArrInt2, $mod.ArrInt2);',
  11035. '$mod.ArrInt1 = rtl.arrayConcatN($mod.ArrInt2, $mod.ArrInt2, $mod.ArrInt2);',
  11036. '$mod.ArrRec1 = rtl.arrayRef($mod.ArrRec2);',
  11037. '$mod.ArrRec1 = rtl.arrayConcat($mod.TRec, $mod.ArrRec2, $mod.ArrRec2);',
  11038. '$mod.ArrRec1 = rtl.arrayConcat($mod.TRec, $mod.ArrRec2, $mod.ArrRec2, $mod.ArrRec2);',
  11039. '$mod.ArrSet1 = rtl.arrayRef($mod.ArrSet2);',
  11040. '$mod.ArrSet1 = rtl.arrayConcat("refSet", $mod.ArrSet2, $mod.ArrSet2);',
  11041. '$mod.ArrSet1 = rtl.arrayConcat("refSet", $mod.ArrSet2, $mod.ArrSet2, $mod.ArrSet2);',
  11042. '$mod.ArrJSValue1 = rtl.arrayRef($mod.ArrJSValue2);',
  11043. '$mod.ArrJSValue1 = rtl.arrayConcatN($mod.ArrJSValue2, $mod.ArrJSValue2);',
  11044. '$mod.ArrJSValue1 = rtl.arrayConcatN($mod.ArrJSValue2, $mod.ArrJSValue2, $mod.ArrJSValue2);',
  11045. '$mod.ArrInt1 = rtl.arrayConcatN([1], $mod.ArrInt2);',
  11046. '$mod.ArrFlag1 = [$mod.TFlag.big];',
  11047. '$mod.ArrFlag1 = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag2);',
  11048. '$mod.ArrFlag1 = rtl.arrayConcatN($mod.ArrFlag2, [$mod.TFlag.small]);',
  11049. '']));
  11050. end;
  11051. procedure TTestModule.TestArray_Concat_Append;
  11052. begin
  11053. StartProgram(false);
  11054. Add([
  11055. 'type',
  11056. ' integer = longint;',
  11057. ' TFlag = (big,small);',
  11058. ' TFlags = set of TFlag;',
  11059. ' TRec = record',
  11060. ' i: integer;',
  11061. ' end;',
  11062. ' TArrInt = array of integer;',
  11063. ' TArrRec = array of TRec;',
  11064. ' TArrFlag = array of TFlag;',
  11065. ' TArrSet = array of TFlags;',
  11066. ' TArrJSValue = array of jsvalue;',
  11067. 'var',
  11068. ' ArrInt: tarrint;',
  11069. ' ArrRec: tarrrec;',
  11070. ' ArrFlag: tarrflag;',
  11071. ' ArrSet: tarrset;',
  11072. ' ArrJSValue: tarrjsvalue;',
  11073. ' r: TRec;',
  11074. ' f: TFlags;',
  11075. 'begin',
  11076. ' // append',
  11077. ' arrint:=concat(arrint);',
  11078. ' arrint:=concat(arrint,[2]);',
  11079. ' arrint:=concat(arrint,[3,4]);',
  11080. ' arrrec:=concat(arrrec);',
  11081. ' arrrec:=concat(arrrec,[r]);',
  11082. ' arrrec:=concat(arrrec,[r,r]);',
  11083. ' arrset:=concat(arrset);',
  11084. ' arrset:=concat(arrset,[f]);',
  11085. ' arrset:=concat(arrset,[f,f]);',
  11086. ' arrjsvalue:=concat(arrjsvalue);',
  11087. ' arrjsvalue:=concat(arrjsvalue,[11]);',
  11088. ' arrjsvalue:=concat(arrjsvalue,[12,13]);',
  11089. ' arrflag:=concat(arrflag);',
  11090. ' arrflag:=concat(arrflag,[small]);',
  11091. ' arrflag:=concat(arrflag,[small,big]);',
  11092. '']);
  11093. ConvertProgram;
  11094. CheckSource('TestArray_Concat_Append',
  11095. LinesToStr([ // statements
  11096. 'this.TFlag = {',
  11097. ' "0": "big",',
  11098. ' big: 0,',
  11099. ' "1": "small",',
  11100. ' small: 1',
  11101. '};',
  11102. 'rtl.recNewT(this, "TRec", function () {',
  11103. ' this.i = 0;',
  11104. ' this.$eq = function (b) {',
  11105. ' return this.i === b.i;',
  11106. ' };',
  11107. ' this.$assign = function (s) {',
  11108. ' this.i = s.i;',
  11109. ' return this;',
  11110. ' };',
  11111. '});',
  11112. 'this.ArrInt = [];',
  11113. 'this.ArrRec = [];',
  11114. 'this.ArrFlag = [];',
  11115. 'this.ArrSet = [];',
  11116. 'this.ArrJSValue = [];',
  11117. 'this.r = this.TRec.$new();',
  11118. 'this.f = {};',
  11119. '']),
  11120. LinesToStr([ // $mod.$main
  11121. '$mod.ArrInt = $mod.ArrInt;',
  11122. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 2);',
  11123. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 3, 4);',
  11124. '$mod.ArrRec = $mod.ArrRec;',
  11125. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r);',
  11126. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r, $mod.r);',
  11127. '$mod.ArrSet = $mod.ArrSet;',
  11128. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f);',
  11129. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f, $mod.f);',
  11130. '$mod.ArrJSValue = $mod.ArrJSValue;',
  11131. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 11);',
  11132. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 12, 13);',
  11133. '$mod.ArrFlag = $mod.ArrFlag;',
  11134. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small);',
  11135. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small, $mod.TFlag.big);',
  11136. '']));
  11137. end;
  11138. procedure TTestModule.TestArray_Concat_Append_Var;
  11139. begin
  11140. StartProgram(false);
  11141. Add([
  11142. 'type',
  11143. ' TArrInt = array of word;',
  11144. '',
  11145. 'procedure Fly(a: TArrInt; var b: TArrInt);',
  11146. 'begin',
  11147. ' a:=concat(a,[2]);',
  11148. ' b:=concat(b,[2]);',
  11149. 'end;',
  11150. 'var',
  11151. ' ArrInt: tarrint;',
  11152. 'begin',
  11153. ' Fly(ArrInt,ArrInt);',
  11154. '']);
  11155. ConvertProgram;
  11156. CheckSource('TestArray_Concat_Append_Var',
  11157. LinesToStr([ // statements
  11158. 'this.Fly = function (a, b) {',
  11159. ' a = rtl.arrayPushN(a, 2);',
  11160. ' b.set(rtl.arrayPushN(b.get(), 2));',
  11161. '};',
  11162. 'this.ArrInt = [];',
  11163. '']),
  11164. LinesToStr([ // $mod.$main
  11165. '$mod.Fly(rtl.arrayRef($mod.ArrInt), {',
  11166. ' p: $mod,',
  11167. ' get: function () {',
  11168. ' return this.p.ArrInt;',
  11169. ' },',
  11170. ' set: function (v) {',
  11171. ' this.p.ArrInt = v;',
  11172. ' }',
  11173. '});',
  11174. '']));
  11175. end;
  11176. procedure TTestModule.TestArray_Copy;
  11177. begin
  11178. StartProgram(false);
  11179. Add([
  11180. 'type',
  11181. ' integer = longint;',
  11182. ' TFlag = (big,small);',
  11183. ' TFlags = set of TFlag;',
  11184. ' TRec = record',
  11185. ' i: integer;',
  11186. ' end;',
  11187. ' TArrInt = array of integer;',
  11188. ' TArrRec = array of TRec;',
  11189. ' TArrSet = array of TFlags;',
  11190. ' TArrJSValue = array of jsvalue;',
  11191. 'var',
  11192. ' ArrInt: tarrint;',
  11193. ' ArrRec: tarrrec;',
  11194. ' ArrSet: tarrset;',
  11195. ' ArrJSValue: tarrjsvalue;',
  11196. 'begin',
  11197. ' arrint:=copy(arrint);',
  11198. ' arrint:=copy(arrint,2);',
  11199. ' arrint:=copy(arrint,3,4);',
  11200. ' arrint:=copy([1,1],1,2);',
  11201. ' arrrec:=copy(arrrec);',
  11202. ' arrrec:=copy(arrrec,5);',
  11203. ' arrrec:=copy(arrrec,6,7);',
  11204. ' arrset:=copy(arrset);',
  11205. ' arrset:=copy(arrset,8);',
  11206. ' arrset:=copy(arrset,9,10);',
  11207. ' arrjsvalue:=copy(arrjsvalue);',
  11208. ' arrjsvalue:=copy(arrjsvalue,11);',
  11209. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  11210. ' ']);
  11211. ConvertProgram;
  11212. CheckSource('TestArray_Copy',
  11213. LinesToStr([ // statements
  11214. 'this.TFlag = {',
  11215. ' "0": "big",',
  11216. ' big: 0,',
  11217. ' "1": "small",',
  11218. ' small: 1',
  11219. '};',
  11220. 'rtl.recNewT(this, "TRec", function () {',
  11221. ' this.i = 0;',
  11222. ' this.$eq = function (b) {',
  11223. ' return this.i === b.i;',
  11224. ' };',
  11225. ' this.$assign = function (s) {',
  11226. ' this.i = s.i;',
  11227. ' return this;',
  11228. ' };',
  11229. '});',
  11230. 'this.ArrInt = [];',
  11231. 'this.ArrRec = [];',
  11232. 'this.ArrSet = [];',
  11233. 'this.ArrJSValue = [];',
  11234. '']),
  11235. LinesToStr([ // $mod.$main
  11236. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  11237. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  11238. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  11239. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  11240. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  11241. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  11242. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  11243. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  11244. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  11245. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  11246. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  11247. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  11248. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  11249. '']));
  11250. end;
  11251. procedure TTestModule.TestArray_InsertDelete;
  11252. begin
  11253. StartProgram(false);
  11254. Add([
  11255. 'type',
  11256. ' integer = longint;',
  11257. ' TFlag = (big,small);',
  11258. ' TFlags = set of TFlag;',
  11259. ' TRec = record',
  11260. ' i: integer;',
  11261. ' end;',
  11262. ' TArrInt = array of integer;',
  11263. ' TArrRec = array of TRec;',
  11264. ' TArrSet = array of TFlags;',
  11265. ' TArrJSValue = array of jsvalue;',
  11266. ' TArrArrInt = array of TArrInt;',
  11267. 'var',
  11268. ' ArrInt: tarrint;',
  11269. ' ArrRec: tarrrec;',
  11270. ' ArrSet: tarrset;',
  11271. ' ArrJSValue: tarrjsvalue;',
  11272. ' ArrArrInt: TArrArrInt;',
  11273. 'begin',
  11274. ' Insert(1,arrint,2);',
  11275. ' Insert(arrint[3],arrint,4);',
  11276. ' Insert(arrrec[5],arrrec,6);',
  11277. ' Insert(arrset[7],arrset,7);',
  11278. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  11279. ' Insert(10,arrjsvalue,11);',
  11280. ' Insert([23],arrarrint,22);',
  11281. ' Delete(arrint,12,13);',
  11282. ' Delete(arrrec,14,15);',
  11283. ' Delete(arrset,17,18);',
  11284. ' Delete(arrjsvalue,19,10);']);
  11285. ConvertProgram;
  11286. CheckSource('TestArray_InsertDelete',
  11287. LinesToStr([ // statements
  11288. 'this.TFlag = {',
  11289. ' "0": "big",',
  11290. ' big: 0,',
  11291. ' "1": "small",',
  11292. ' small: 1',
  11293. '};',
  11294. 'rtl.recNewT(this, "TRec", function () {',
  11295. ' this.i = 0;',
  11296. ' this.$eq = function (b) {',
  11297. ' return this.i === b.i;',
  11298. ' };',
  11299. ' this.$assign = function (s) {',
  11300. ' this.i = s.i;',
  11301. ' return this;',
  11302. ' };',
  11303. '});',
  11304. 'this.ArrInt = [];',
  11305. 'this.ArrRec = [];',
  11306. 'this.ArrSet = [];',
  11307. 'this.ArrJSValue = [];',
  11308. 'this.ArrArrInt = [];',
  11309. '']),
  11310. LinesToStr([ // $mod.$main
  11311. '$mod.ArrInt = rtl.arrayInsert(1, $mod.ArrInt, 2);',
  11312. '$mod.ArrInt = rtl.arrayInsert($mod.ArrInt[3], $mod.ArrInt, 4);',
  11313. '$mod.ArrRec = rtl.arrayInsert($mod.ArrRec[5], $mod.ArrRec, 6);',
  11314. '$mod.ArrSet = rtl.arrayInsert($mod.ArrSet[7], $mod.ArrSet, 7);',
  11315. '$mod.ArrJSValue = rtl.arrayInsert($mod.ArrJSValue[8], $mod.ArrJSValue, 9);',
  11316. '$mod.ArrJSValue = rtl.arrayInsert(10, $mod.ArrJSValue, 11);',
  11317. '$mod.ArrArrInt = rtl.arrayInsert([23], $mod.ArrArrInt, 22);',
  11318. '$mod.ArrInt.splice(12, 13);',
  11319. '$mod.ArrRec.splice(14, 15);',
  11320. '$mod.ArrSet.splice(17, 18);',
  11321. '$mod.ArrJSValue.splice(19, 10);',
  11322. '']));
  11323. end;
  11324. procedure TTestModule.TestArray_Add_Append;
  11325. begin
  11326. StartProgram(false);
  11327. Add([
  11328. '{$modeswitch arrayoperators}',
  11329. 'type',
  11330. ' integer = longint;',
  11331. ' TFlag = (big,small);',
  11332. ' TFlags = set of TFlag;',
  11333. ' TRec = record',
  11334. ' i: integer;',
  11335. ' end;',
  11336. ' TArrInt = array of integer;',
  11337. ' TArrRec = array of TRec;',
  11338. ' TArrFlag = array of TFlag;',
  11339. ' TArrSet = array of TFlags;',
  11340. ' TArrJSValue = array of jsvalue;',
  11341. 'var',
  11342. ' ArrInt: tarrint;',
  11343. ' ArrRec: tarrrec;',
  11344. ' ArrFlag: tarrflag;',
  11345. ' ArrSet: tarrset;',
  11346. ' ArrJSValue: tarrjsvalue;',
  11347. ' r: TRec;',
  11348. ' f: TFlags;',
  11349. 'begin',
  11350. ' // append',
  11351. ' arrint:=arrint+[2];',
  11352. ' arrint:=arrint+[3,4];',
  11353. ' arrrec:=arrrec+[r];',
  11354. ' arrrec:=arrrec+[r,r];',
  11355. ' arrset:=arrset+[f];',
  11356. ' arrset:=arrset+[f,f];',
  11357. ' arrjsvalue:=arrjsvalue+[11];',
  11358. ' arrjsvalue:=arrjsvalue+[12,13];',
  11359. ' arrflag:=arrflag+[small];',
  11360. ' arrflag:=arrflag+[small,big];',
  11361. '']);
  11362. ConvertProgram;
  11363. CheckSource('TestArray_Add_Append',
  11364. LinesToStr([ // statements
  11365. 'this.TFlag = {',
  11366. ' "0": "big",',
  11367. ' big: 0,',
  11368. ' "1": "small",',
  11369. ' small: 1',
  11370. '};',
  11371. 'rtl.recNewT(this, "TRec", function () {',
  11372. ' this.i = 0;',
  11373. ' this.$eq = function (b) {',
  11374. ' return this.i === b.i;',
  11375. ' };',
  11376. ' this.$assign = function (s) {',
  11377. ' this.i = s.i;',
  11378. ' return this;',
  11379. ' };',
  11380. '});',
  11381. 'this.ArrInt = [];',
  11382. 'this.ArrRec = [];',
  11383. 'this.ArrFlag = [];',
  11384. 'this.ArrSet = [];',
  11385. 'this.ArrJSValue = [];',
  11386. 'this.r = this.TRec.$new();',
  11387. 'this.f = {};',
  11388. '']),
  11389. LinesToStr([ // $mod.$main
  11390. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 2);',
  11391. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 3, 4);',
  11392. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r);',
  11393. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r, $mod.r);',
  11394. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f);',
  11395. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f, $mod.f);',
  11396. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 11);',
  11397. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 12, 13);',
  11398. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small);',
  11399. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small, $mod.TFlag.big);',
  11400. '']));
  11401. end;
  11402. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  11403. begin
  11404. Parser.Options:=Parser.Options+[po_cassignments];
  11405. StartProgram(false);
  11406. Add([
  11407. '{$modeswitch arrayoperators}',
  11408. 'type',
  11409. ' integer = longint;',
  11410. ' TArrInt = array of integer;',
  11411. ' TArrStr = array of string;',
  11412. 'const',
  11413. ' Ints: TArrInt = (1,2,3);',
  11414. ' Aliases: TarrStr = (''foo'',''b'');',
  11415. ' OneInt: TArrInt = (7);',
  11416. ' OneStr: array of integer = (7);',
  11417. ' Chars: array of char = ''aoc'';',
  11418. ' Names: array of string = (''a'',''foo'');',
  11419. ' NameCount = low(Names)+high(Names)+length(Names);',
  11420. 'var i: integer;',
  11421. 'begin',
  11422. ' Ints:=[];',
  11423. ' Ints:=[1,1];',
  11424. ' Ints:=[1]+[2];',
  11425. ' Ints:=[2];',
  11426. ' Ints:=[]+ints;',
  11427. ' Ints:=Ints+[];',
  11428. ' Ints:=Ints+OneInt;',
  11429. ' Ints:=Ints+[1,1];',
  11430. ' Ints:=[i,i]+Ints;',
  11431. ' Ints:=[1]+[i]+[3];',
  11432. '']);
  11433. ConvertProgram;
  11434. CheckSource('TestArray_DynArrayConstObjFPC',
  11435. LinesToStr([ // statements
  11436. 'this.Ints = [1, 2, 3];',
  11437. 'this.Aliases = ["foo", "b"];',
  11438. 'this.OneInt = [7];',
  11439. 'this.OneStr = [7];',
  11440. 'this.Chars = ["a", "o", "c"];',
  11441. 'this.Names = ["a", "foo"];',
  11442. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  11443. 'this.i = 0;',
  11444. '']),
  11445. LinesToStr([ // $mod.$main
  11446. '$mod.Ints = [];',
  11447. '$mod.Ints = [1, 1];',
  11448. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  11449. '$mod.Ints = [2];',
  11450. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  11451. '$mod.Ints = $mod.Ints;',
  11452. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  11453. '$mod.Ints = rtl.arrayPushN($mod.Ints, 1, 1);',
  11454. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  11455. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  11456. '']));
  11457. end;
  11458. procedure TTestModule.TestArray_DynArrayConstDelphi;
  11459. begin
  11460. StartProgram(false);
  11461. // Note: const c = [1,1]; defines a set!
  11462. Add([
  11463. '{$mode delphi}',
  11464. 'type',
  11465. ' integer = longint;',
  11466. ' TArrInt = array of integer;',
  11467. ' TArrStr = array of string;',
  11468. 'const',
  11469. ' Ints: TArrInt = [1,1,2];',
  11470. ' Aliases: TarrStr = [''foo'',''b''];',
  11471. ' OneInt: TArrInt = [7];',
  11472. ' OneStr: array of integer = [7]+[8];',
  11473. ' Chars: array of char = ''aoc'';',
  11474. ' Names: array of string = [''a'',''a''];',
  11475. ' NameCount = low(Names)+high(Names)+length(Names);',
  11476. 'begin',
  11477. '']);
  11478. ConvertProgram;
  11479. CheckSource('TestArray_DynArrayConstDelphi',
  11480. LinesToStr([ // statements
  11481. 'this.Ints = [1, 1, 2];',
  11482. 'this.Aliases = ["foo", "b"];',
  11483. 'this.OneInt = [7];',
  11484. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  11485. 'this.Chars = ["a", "o", "c"];',
  11486. 'this.Names = ["a", "a"];',
  11487. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  11488. '']),
  11489. LinesToStr([ // $mod.$main
  11490. '']));
  11491. end;
  11492. procedure TTestModule.TestArray_ArrayLitAsParam;
  11493. begin
  11494. StartProgram(false);
  11495. Add([
  11496. '{$modeswitch arrayoperators}',
  11497. 'type',
  11498. ' integer = longint;',
  11499. ' TArrInt = array of integer;',
  11500. ' TArrSet = array of (red,green,blue);',
  11501. 'procedure DoOpenInt(const a: array of integer); forward;',
  11502. 'procedure DoInt(const a: TArrInt);',
  11503. 'begin',
  11504. ' DoInt(a+[1]);',
  11505. ' DoInt([1]+a);',
  11506. ' DoOpenInt(a);',
  11507. ' DoOpenInt(a+[1]);',
  11508. ' DoOpenInt([1]+a);',
  11509. 'end;',
  11510. 'procedure DoOpenInt(const a: array of integer);',
  11511. 'begin',
  11512. ' DoOpenInt(a+[1]);',
  11513. ' DoOpenInt([1]+a);',
  11514. ' DoInt(a);',
  11515. ' DoInt(a+[1]);',
  11516. ' DoInt([1]+a);',
  11517. 'end;',
  11518. 'procedure DoSet(const a: TArrSet);',
  11519. 'begin',
  11520. ' DoSet(a+[red]);',
  11521. ' DoSet([blue]+a);',
  11522. 'end;',
  11523. 'var',
  11524. ' i: TArrInt;',
  11525. ' s: TArrSet;',
  11526. 'begin',
  11527. ' DoInt([1]);',
  11528. ' DoInt([1]+[2]);',
  11529. ' DoInt(i+[1]);',
  11530. ' DoInt([1]+i);',
  11531. ' DoOpenInt([1]);',
  11532. ' DoOpenInt([1]+[2]);',
  11533. ' DoOpenInt(i+[1]);',
  11534. ' DoOpenInt([1]+i);',
  11535. ' DoSet([red]);',
  11536. ' DoSet([blue]+[green]);',
  11537. ' DoSet(s+[blue]);',
  11538. ' DoSet([red]+s);',
  11539. '']);
  11540. ConvertProgram;
  11541. CheckSource('TestArray_ArrayLitAsParam',
  11542. LinesToStr([ // statements
  11543. 'this.TArrSet$a = {',
  11544. ' "0": "red",',
  11545. ' red: 0,',
  11546. ' "1": "green",',
  11547. ' green: 1,',
  11548. ' "2": "blue",',
  11549. ' blue: 2',
  11550. '};',
  11551. 'this.DoInt = function (a) {',
  11552. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  11553. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  11554. ' $mod.DoOpenInt(a);',
  11555. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  11556. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  11557. '};',
  11558. 'this.DoOpenInt = function (a) {',
  11559. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  11560. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  11561. ' $mod.DoInt(a);',
  11562. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  11563. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  11564. '};',
  11565. 'this.DoSet = function (a) {',
  11566. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  11567. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  11568. '};',
  11569. 'this.i = [];',
  11570. 'this.s = [];',
  11571. '']),
  11572. LinesToStr([ // $mod.$main
  11573. '$mod.DoInt([1]);',
  11574. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  11575. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  11576. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  11577. '$mod.DoOpenInt([1]);',
  11578. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  11579. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  11580. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  11581. '$mod.DoSet([$mod.TArrSet$a.red]);',
  11582. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  11583. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  11584. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  11585. '']));
  11586. end;
  11587. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  11588. begin
  11589. StartProgram(false);
  11590. Add([
  11591. '{$modeswitch arrayoperators}',
  11592. 'type',
  11593. ' integer = longint;',
  11594. ' TArrInt = array of integer;',
  11595. ' TArrArrInt = array of TArrInt;',
  11596. 'procedure DoInt(const a: TArrArrInt);',
  11597. 'begin',
  11598. ' DoInt(a+[[1]]);',
  11599. ' DoInt([[1]]+a);',
  11600. ' DoInt(a);',
  11601. 'end;',
  11602. 'var',
  11603. ' i: TArrInt;',
  11604. ' a: TArrArrInt;',
  11605. 'begin',
  11606. ' a:=[[1]];',
  11607. ' a:=[i];',
  11608. ' a:=a+[i];',
  11609. ' a:=[i]+a;',
  11610. ' a:=[[1]+i];',
  11611. ' a:=[[1]+[2]];',
  11612. ' a:=[i+[2]];',
  11613. ' DoInt([[1]]);',
  11614. ' DoInt([[1]+[2],[3,4],[5]]);',
  11615. ' DoInt([i+[1]]+a);',
  11616. ' DoInt([i]+a);',
  11617. '']);
  11618. ConvertProgram;
  11619. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  11620. LinesToStr([ // statements
  11621. 'this.DoInt = function (a) {',
  11622. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  11623. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  11624. ' $mod.DoInt(a);',
  11625. '};',
  11626. 'this.i = [];',
  11627. 'this.a = [];',
  11628. '']),
  11629. LinesToStr([ // $mod.$main
  11630. '$mod.a = [[1]];',
  11631. '$mod.a = [$mod.i];',
  11632. '$mod.a = rtl.arrayPushN($mod.a, $mod.i);',
  11633. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  11634. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  11635. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  11636. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  11637. '$mod.DoInt([[1]]);',
  11638. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  11639. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  11640. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  11641. '']));
  11642. end;
  11643. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  11644. begin
  11645. StartProgram(false);
  11646. Add([
  11647. '{$modeswitch arrayoperators}',
  11648. 'type',
  11649. ' integer = longint;',
  11650. ' TArrInt = array[1..2] of integer;',
  11651. ' TArrArrInt = array of TArrInt;',
  11652. 'procedure DoInt(const a: TArrArrInt);',
  11653. 'begin',
  11654. ' DoInt(a+[[1,2]]);',
  11655. ' DoInt([[1,2]]+a);',
  11656. ' DoInt(a);',
  11657. 'end;',
  11658. 'var',
  11659. ' i: TArrInt;',
  11660. ' a: TArrArrInt;',
  11661. 'begin',
  11662. ' a:=[[1,1]];',
  11663. ' a:=[i];',
  11664. ' a:=a+[i];',
  11665. ' a:=[i]+a;',
  11666. ' DoInt([[1,1]]);',
  11667. ' DoInt([[1,2],[3,4]]);',
  11668. '']);
  11669. ConvertProgram;
  11670. CheckSource('TestArray_ArrayLitStaticAsParam',
  11671. LinesToStr([ // statements
  11672. 'this.DoInt = function (a) {',
  11673. ' $mod.DoInt(rtl.arrayConcat("slice", a, [[1, 2]]));',
  11674. ' $mod.DoInt(rtl.arrayConcat("slice", [[1, 2]], a));',
  11675. ' $mod.DoInt(a);',
  11676. '};',
  11677. 'this.i = rtl.arraySetLength(null, 0, 2);',
  11678. 'this.a = [];',
  11679. '']),
  11680. LinesToStr([ // $mod.$main
  11681. '$mod.a = [[1, 1]];',
  11682. '$mod.a = [$mod.i.slice(0)];',
  11683. '$mod.a = rtl.arrayPush("slice", $mod.a, $mod.i);',
  11684. '$mod.a = rtl.arrayConcat("slice", [$mod.i.slice(0)], $mod.a);',
  11685. '$mod.DoInt([[1, 1]]);',
  11686. '$mod.DoInt([[1, 2], [3, 4]]);',
  11687. '']));
  11688. end;
  11689. procedure TTestModule.TestArray_ForInArrOfString;
  11690. begin
  11691. StartProgram(false);
  11692. Add([
  11693. 'type',
  11694. 'type',
  11695. ' TMonthNameArray = array [1..12] of string;',
  11696. ' TMonthNames = TMonthNameArray;',
  11697. ' TObject = class',
  11698. ' private',
  11699. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  11700. ' public',
  11701. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  11702. ' end;',
  11703. 'var',
  11704. ' f: TObject;',
  11705. ' Month: string;',
  11706. ' Names: array of string = (''a'',''foo'',''bar'');',
  11707. ' i: longint;',
  11708. 'begin',
  11709. ' for Month in f.LongMonthNames do ;',
  11710. ' for Month in Names do ;',
  11711. ' for i:=low(Names) to high(Names) do ;',
  11712. '']);
  11713. ConvertProgram;
  11714. CheckSource('TestArray_ForInArrOfString',
  11715. LinesToStr([ // statements
  11716. 'rtl.createClass(this, "TObject", null, function () {',
  11717. ' this.$init = function () {',
  11718. ' };',
  11719. ' this.$final = function () {',
  11720. ' };',
  11721. '});',
  11722. 'this.f = null;',
  11723. 'this.Month = "";',
  11724. 'this.Names = ["a", "foo", "bar"];',
  11725. 'this.i = 0;',
  11726. '']),
  11727. LinesToStr([ // $mod.$main
  11728. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  11729. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  11730. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  11731. '']));
  11732. end;
  11733. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  11734. begin
  11735. StartProgram(false);
  11736. Add([
  11737. '{$modeswitch externalclass}',
  11738. 'type',
  11739. ' TJSObject = class external name ''Object''',
  11740. ' end;',
  11741. ' TJSArray = class external name ''Array''',
  11742. ' class function isArray(Value: JSValue) : boolean;',
  11743. ' function concat() : TJSArray; varargs;',
  11744. ' end;',
  11745. 'var',
  11746. ' aObj: TJSArray;',
  11747. ' a: array of longint;',
  11748. ' o: TJSObject;',
  11749. 'begin',
  11750. ' if TJSArray.isArray(65) then ;',
  11751. ' aObj:=TJSArray(a).concat(a);',
  11752. ' o:=TJSObject(a);',
  11753. ' aObj:=TJSArray([''bird'',''ant'']);',
  11754. '']);
  11755. ConvertProgram;
  11756. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  11757. LinesToStr([ // statements
  11758. 'this.aObj = null;',
  11759. 'this.a = [];',
  11760. 'this.o = null;',
  11761. '']),
  11762. LinesToStr([ // $mod.$main
  11763. 'if (Array.isArray(65)) ;',
  11764. '$mod.aObj = $mod.a.concat($mod.a);',
  11765. '$mod.o = $mod.a;',
  11766. '$mod.aObj = ["bird", "ant"];',
  11767. '']));
  11768. end;
  11769. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  11770. begin
  11771. StartProgram(false);
  11772. Add([
  11773. '{$modeswitch externalclass}',
  11774. 'type',
  11775. ' TArrStr = array of string;',
  11776. ' TJSArray = class external name ''Array''',
  11777. ' end;',
  11778. ' TJSObject = class external name ''Object''',
  11779. ' end;',
  11780. 'var',
  11781. ' aObj: TJSArray;',
  11782. ' a: TArrStr;',
  11783. ' jo: TJSObject;',
  11784. 'begin',
  11785. ' a:=TArrStr(aObj);',
  11786. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  11787. ' a:=TarrStr(jo);',
  11788. '']);
  11789. ConvertProgram;
  11790. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  11791. LinesToStr([ // statements
  11792. 'this.aObj = null;',
  11793. 'this.a = [];',
  11794. 'this.jo = null;',
  11795. '']),
  11796. LinesToStr([ // $mod.$main
  11797. '$mod.a = $mod.aObj;',
  11798. '$mod.aObj[1] = $mod.aObj[2];',
  11799. '$mod.a = $mod.jo;',
  11800. '']));
  11801. end;
  11802. procedure TTestModule.TestArrayOfConst_TVarRec;
  11803. begin
  11804. StartProgram(true,[supTVarRec]);
  11805. Add([
  11806. 'procedure Say(args: array of const);',
  11807. 'var',
  11808. ' i: longint;',
  11809. ' v: TVarRec;',
  11810. 'begin',
  11811. ' for i:=low(args) to high(args) do begin',
  11812. ' v:=args[i];',
  11813. ' case v.vtype of',
  11814. ' vtInteger: if length(args)=args[i].vInteger then ;',
  11815. ' end;',
  11816. ' end;',
  11817. ' for v in args do ;',
  11818. ' args:=nil;',
  11819. ' SetLength(args,2);',
  11820. 'end;',
  11821. 'begin']);
  11822. ConvertProgram;
  11823. CheckSource('TestArrayOfConst_TVarRec',
  11824. LinesToStr([ // statements
  11825. 'this.Say = function (args) {',
  11826. ' var i = 0;',
  11827. ' var v = pas.system.TVarRec.$new();',
  11828. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  11829. ' i = $l;',
  11830. ' v.$assign(args[i]);',
  11831. ' var $tmp = v.VType;',
  11832. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  11833. ' };',
  11834. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  11835. ' args = [];',
  11836. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  11837. '};',
  11838. '']),
  11839. LinesToStr([ // $mod.$main
  11840. ]));
  11841. end;
  11842. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  11843. begin
  11844. StartProgram(true,[supTVarRec]);
  11845. Add([
  11846. 'procedure Say(args: array of const);',
  11847. 'begin',
  11848. ' Say(args);',
  11849. 'end;',
  11850. 'var',
  11851. ' p: Pointer;',
  11852. ' j: jsvalue;',
  11853. ' c: currency;',
  11854. 'begin',
  11855. ' Say([]);',
  11856. ' Say([1]);',
  11857. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  11858. '']);
  11859. ConvertProgram;
  11860. CheckSource('TestArrayOfConst_PassBaseTypes',
  11861. LinesToStr([ // statements
  11862. 'this.Say = function (args) {',
  11863. ' $mod.Say(args);',
  11864. '};',
  11865. 'this.p = null;',
  11866. 'this.j = undefined;',
  11867. 'this.c = 0;',
  11868. '']),
  11869. LinesToStr([ // $mod.$main
  11870. '$mod.Say([]);',
  11871. '$mod.Say(pas.system.VarRecs(0, 1));',
  11872. '$mod.Say(pas.system.VarRecs(',
  11873. ' 9,',
  11874. ' "c",',
  11875. ' 18,',
  11876. ' "foo",',
  11877. ' 5,',
  11878. ' null,',
  11879. ' 1,',
  11880. ' true,',
  11881. ' 3,',
  11882. ' 1.3,',
  11883. ' 5,',
  11884. ' $mod.p,',
  11885. ' 20,',
  11886. ' $mod.j,',
  11887. ' 12,',
  11888. ' $mod.c',
  11889. ' ));',
  11890. '']));
  11891. end;
  11892. procedure TTestModule.TestArrayOfConst_PassObj;
  11893. begin
  11894. StartProgram(true,[supTVarRec]);
  11895. Add([
  11896. '{$interfaces corba}',
  11897. 'type',
  11898. ' TObject = class',
  11899. ' end;',
  11900. ' TClass = class of TObject;',
  11901. ' IUnknown = interface',
  11902. ' end;',
  11903. 'procedure Say(args: array of const);',
  11904. 'begin',
  11905. 'end;',
  11906. 'var',
  11907. ' o: TObject;',
  11908. ' c: TClass;',
  11909. ' i: IUnknown;',
  11910. 'begin',
  11911. ' Say([o,c,TObject]);',
  11912. ' Say([nil,i]);',
  11913. '']);
  11914. ConvertProgram;
  11915. CheckSource('TestArrayOfConst_PassObj',
  11916. LinesToStr([ // statements
  11917. 'rtl.createClass(this, "TObject", null, function () {',
  11918. ' this.$init = function () {',
  11919. ' };',
  11920. ' this.$final = function () {',
  11921. ' };',
  11922. '});',
  11923. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  11924. 'this.Say = function (args) {',
  11925. '};',
  11926. 'this.o = null;',
  11927. 'this.c = null;',
  11928. 'this.i = null;',
  11929. '']),
  11930. LinesToStr([ // $mod.$main
  11931. '$mod.Say(pas.system.VarRecs(',
  11932. ' 7,',
  11933. ' $mod.o,',
  11934. ' 8,',
  11935. ' $mod.c,',
  11936. ' 8,',
  11937. ' $mod.TObject',
  11938. '));',
  11939. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  11940. '']));
  11941. end;
  11942. procedure TTestModule.TestRecord_Empty;
  11943. begin
  11944. StartProgram(false);
  11945. Add([
  11946. 'type',
  11947. ' TRecA = record',
  11948. ' end;',
  11949. 'var a,b: TRecA;',
  11950. 'begin',
  11951. ' if a=b then ;']);
  11952. ConvertProgram;
  11953. CheckSource('TestRecord_Empty',
  11954. LinesToStr([ // statements
  11955. 'rtl.recNewT(this, "TRecA", function () {',
  11956. ' this.$eq = function (b) {',
  11957. ' return true;',
  11958. ' };',
  11959. ' this.$assign = function (s) {',
  11960. ' return this;',
  11961. ' };',
  11962. '});',
  11963. 'this.a = this.TRecA.$new();',
  11964. 'this.b = this.TRecA.$new();',
  11965. '']),
  11966. LinesToStr([ // $mod.$main
  11967. 'if ($mod.a.$eq($mod.b)) ;'
  11968. ]));
  11969. end;
  11970. procedure TTestModule.TestRecord_Var;
  11971. begin
  11972. StartProgram(false);
  11973. Add('type');
  11974. Add(' TRecA = record');
  11975. Add(' Bold: longint;');
  11976. Add(' end;');
  11977. Add('var Rec: TRecA;');
  11978. Add('begin');
  11979. Add(' rec.bold:=123');
  11980. ConvertProgram;
  11981. CheckSource('TestRecord_Var',
  11982. LinesToStr([ // statements
  11983. 'rtl.recNewT(this, "TRecA", function () {',
  11984. ' this.Bold = 0;',
  11985. ' this.$eq = function (b) {',
  11986. ' return this.Bold === b.Bold;',
  11987. ' };',
  11988. ' this.$assign = function (s) {',
  11989. ' this.Bold = s.Bold;',
  11990. ' return this;',
  11991. ' };',
  11992. '});',
  11993. 'this.Rec = this.TRecA.$new();',
  11994. '']),
  11995. LinesToStr([ // $mod.$main
  11996. '$mod.Rec.Bold = 123;'
  11997. ]));
  11998. end;
  11999. procedure TTestModule.TestRecord_VarExternal;
  12000. begin
  12001. StartProgram(false);
  12002. Add([
  12003. '{$modeswitch externalclass}',
  12004. 'type',
  12005. ' TRecA = record',
  12006. ' i: byte;',
  12007. ' length_: longint external name ''length'';',
  12008. ' end;',
  12009. 'var Rec: TRecA;',
  12010. 'begin',
  12011. ' rec.length_ := rec.length_',
  12012. '']);
  12013. ConvertProgram;
  12014. CheckSource('TestRecord_VarExternal',
  12015. LinesToStr([ // statements
  12016. 'rtl.recNewT(this, "TRecA", function () {',
  12017. ' this.i = 0;',
  12018. ' this.$eq = function (b) {',
  12019. ' return (this.i === b.i) && (this.length === b.length);',
  12020. ' };',
  12021. ' this.$assign = function (s) {',
  12022. ' this.i = s.i;',
  12023. ' this.length = s.length;',
  12024. ' return this;',
  12025. ' };',
  12026. '});',
  12027. 'this.Rec = this.TRecA.$new();',
  12028. '']),
  12029. LinesToStr([ // $mod.$main
  12030. '$mod.Rec.length = $mod.Rec.length;'
  12031. ]));
  12032. end;
  12033. procedure TTestModule.TestRecord_WithDo;
  12034. begin
  12035. StartProgram(false);
  12036. Add('type');
  12037. Add(' TRec = record');
  12038. Add(' vI: longint;');
  12039. Add(' end;');
  12040. Add('var');
  12041. Add(' Int: longint;');
  12042. Add(' r: TRec;');
  12043. Add('begin');
  12044. Add(' with r do');
  12045. Add(' int:=vi;');
  12046. Add(' with r do begin');
  12047. Add(' int:=vi;');
  12048. Add(' vi:=int;');
  12049. Add(' end;');
  12050. ConvertProgram;
  12051. CheckSource('TestWithRecordDo',
  12052. LinesToStr([ // statements
  12053. 'rtl.recNewT(this, "TRec", function () {',
  12054. ' this.vI = 0;',
  12055. ' this.$eq = function (b) {',
  12056. ' return this.vI === b.vI;',
  12057. ' };',
  12058. ' this.$assign = function (s) {',
  12059. ' this.vI = s.vI;',
  12060. ' return this;',
  12061. ' };',
  12062. '});',
  12063. 'this.Int = 0;',
  12064. 'this.r = this.TRec.$new();',
  12065. '']),
  12066. LinesToStr([ // $mod.$main
  12067. 'var $with = $mod.r;',
  12068. '$mod.Int = $with.vI;',
  12069. 'var $with1 = $mod.r;',
  12070. '$mod.Int = $with1.vI;',
  12071. '$with1.vI = $mod.Int;'
  12072. ]));
  12073. end;
  12074. procedure TTestModule.TestRecord_Assign;
  12075. begin
  12076. StartProgram(false);
  12077. Add([
  12078. 'type',
  12079. ' TEnum = (red,green);',
  12080. ' TEnums = set of TEnum;',
  12081. ' TSmallRec = record',
  12082. ' N: longint;',
  12083. ' end;',
  12084. ' TBigRec = record',
  12085. ' Int: longint;',
  12086. ' D: double;',
  12087. ' Arr: array of longint;',
  12088. ' Arr2: array[1..2] of longint;',
  12089. ' Small: TSmallRec;',
  12090. ' Enums: TEnums;',
  12091. ' end;',
  12092. 'var',
  12093. ' r, s: TBigRec;',
  12094. 'begin',
  12095. ' r:=s;',
  12096. ' r:=default(TBigRec);',
  12097. ' r:=default(s);',
  12098. '']);
  12099. ConvertProgram;
  12100. CheckSource('TestRecord_Assign',
  12101. LinesToStr([ // statements
  12102. 'this.TEnum = {',
  12103. ' "0": "red",',
  12104. ' red: 0,',
  12105. ' "1": "green",',
  12106. ' green: 1',
  12107. '};',
  12108. 'rtl.recNewT(this, "TSmallRec", function () {',
  12109. ' this.N = 0;',
  12110. ' this.$eq = function (b) {',
  12111. ' return this.N === b.N;',
  12112. ' };',
  12113. ' this.$assign = function (s) {',
  12114. ' this.N = s.N;',
  12115. ' return this;',
  12116. ' };',
  12117. '});',
  12118. 'rtl.recNewT(this, "TBigRec", function () {',
  12119. ' this.Int = 0;',
  12120. ' this.D = 0.0;',
  12121. ' this.$new = function () {',
  12122. ' var r = Object.create(this);',
  12123. ' r.Arr = [];',
  12124. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  12125. ' r.Small = $mod.TSmallRec.$new();',
  12126. ' r.Enums = {};',
  12127. ' return r;',
  12128. ' };',
  12129. ' this.$eq = function (b) {',
  12130. ' return (this.Int === b.Int) && (this.D === b.D) && (this.Arr === b.Arr) && rtl.arrayEq(this.Arr2, b.Arr2) && this.Small.$eq(b.Small) && rtl.eqSet(this.Enums, b.Enums);',
  12131. ' };',
  12132. ' this.$assign = function (s) {',
  12133. ' this.Int = s.Int;',
  12134. ' this.D = s.D;',
  12135. ' this.Arr = rtl.arrayRef(s.Arr);',
  12136. ' this.Arr2 = s.Arr2.slice(0);',
  12137. ' this.Small.$assign(s.Small);',
  12138. ' this.Enums = rtl.refSet(s.Enums);',
  12139. ' return this;',
  12140. ' };',
  12141. '});',
  12142. 'this.r = this.TBigRec.$new();',
  12143. 'this.s = this.TBigRec.$new();',
  12144. '']),
  12145. LinesToStr([ // $mod.$main
  12146. '$mod.r.$assign($mod.s);',
  12147. '$mod.r.$assign($mod.TBigRec.$new());',
  12148. '$mod.r.$assign($mod.TBigRec.$new());',
  12149. '']));
  12150. end;
  12151. procedure TTestModule.TestRecord_AsParams;
  12152. begin
  12153. StartProgram(false);
  12154. Add([
  12155. 'type',
  12156. ' integer = longint;',
  12157. ' TRecord = record',
  12158. ' i: integer;',
  12159. ' end;',
  12160. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  12161. 'var vL: TRecord;',
  12162. 'begin',
  12163. ' vd:=vd;',
  12164. ' vd.i:=vd.i;',
  12165. ' vl:=vc;',
  12166. ' vv:=vv;',
  12167. ' vv.i:=vv.i;',
  12168. ' U:=vl;',
  12169. ' U:=vd;',
  12170. ' U:=vc;',
  12171. ' U:=vv;',
  12172. ' vl:=TRecord(U);',
  12173. ' vd:=TRecord(U);',
  12174. ' vv:=TRecord(U);',
  12175. ' doit(vd,vd,vd,vd);',
  12176. ' doit(vc,vc,vl,vl);',
  12177. ' doit(vv,vv,vv,vv);',
  12178. ' doit(vl,vl,vl,vl);',
  12179. ' TRecord(U).i:=3;',
  12180. 'end;',
  12181. 'var i: TRecord;',
  12182. 'begin',
  12183. ' doit(i,i,i,i);',
  12184. '']);
  12185. ConvertProgram;
  12186. CheckSource('TestRecord_AsParams',
  12187. LinesToStr([ // statements
  12188. 'rtl.recNewT(this, "TRecord", function () {',
  12189. ' this.i = 0;',
  12190. ' this.$eq = function (b) {',
  12191. ' return this.i === b.i;',
  12192. ' };',
  12193. ' this.$assign = function (s) {',
  12194. ' this.i = s.i;',
  12195. ' return this;',
  12196. ' };',
  12197. '});',
  12198. 'this.DoIt = function (vD, vC, vV, U) {',
  12199. ' var vL = $mod.TRecord.$new();',
  12200. ' vD.$assign(vD);',
  12201. ' vD.i = vD.i;',
  12202. ' vL.$assign(vC);',
  12203. ' vV.$assign(vV);',
  12204. ' vV.i = vV.i;',
  12205. ' U.$assign(vL);',
  12206. ' U.$assign(vD);',
  12207. ' U.$assign(vC);',
  12208. ' U.$assign(vV);',
  12209. ' vL.$assign(U);',
  12210. ' vD.$assign(U);',
  12211. ' vV.$assign(U);',
  12212. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  12213. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  12214. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  12215. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  12216. ' U.i = 3;',
  12217. '};',
  12218. 'this.i = this.TRecord.$new();'
  12219. ]),
  12220. LinesToStr([
  12221. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  12222. '']));
  12223. end;
  12224. procedure TTestModule.TestRecord_ConstRef;
  12225. begin
  12226. StartProgram(false);
  12227. Add([
  12228. 'type TRec = record i: word; end;',
  12229. 'procedure Run(constref a: TRec);',
  12230. 'begin',
  12231. 'end;',
  12232. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  12233. 'var l: TRec;',
  12234. 'begin',
  12235. ' Run(l);',
  12236. ' Run(a);',
  12237. ' Run(b);',
  12238. ' Run(c);',
  12239. ' Run(d);',
  12240. ' Run(e);',
  12241. 'end;',
  12242. 'begin',
  12243. '']);
  12244. ConvertProgram;
  12245. CheckResolverUnexpectedHints();
  12246. CheckSource('TestRecord_ConstRef',
  12247. LinesToStr([ // statements
  12248. 'rtl.recNewT(this, "TRec", function () {',
  12249. ' this.i = 0;',
  12250. ' this.$eq = function (b) {',
  12251. ' return this.i === b.i;',
  12252. ' };',
  12253. ' this.$assign = function (s) {',
  12254. ' this.i = s.i;',
  12255. ' return this;',
  12256. ' };',
  12257. '});',
  12258. 'this.Run = function (a) {',
  12259. '};',
  12260. 'this.Fly = function (a, b, c, d, e) {',
  12261. ' var l = $mod.TRec.$new();',
  12262. ' $mod.Run(l);',
  12263. ' $mod.Run(a);',
  12264. ' $mod.Run(b);',
  12265. ' $mod.Run(c);',
  12266. ' $mod.Run(d);',
  12267. ' $mod.Run(e);',
  12268. '};',
  12269. '']),
  12270. LinesToStr([
  12271. '']));
  12272. end;
  12273. procedure TTestModule.TestRecordElement_AsParams;
  12274. begin
  12275. StartProgram(false);
  12276. Add('type');
  12277. Add(' integer = longint;');
  12278. Add(' TRecord = record');
  12279. Add(' i: integer;');
  12280. Add(' end;');
  12281. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  12282. Add('var vJ: TRecord;');
  12283. Add('begin');
  12284. Add(' doit(vj.i,vj.i,vj.i);');
  12285. Add('end;');
  12286. Add('var r: TRecord;');
  12287. Add('begin');
  12288. Add(' doit(r.i,r.i,r.i);');
  12289. ConvertProgram;
  12290. CheckSource('TestRecordElement_AsParams',
  12291. LinesToStr([ // statements
  12292. 'rtl.recNewT(this, "TRecord", function () {',
  12293. ' this.i = 0;',
  12294. ' this.$eq = function (b) {',
  12295. ' return this.i === b.i;',
  12296. ' };',
  12297. ' this.$assign = function (s) {',
  12298. ' this.i = s.i;',
  12299. ' return this;',
  12300. ' };',
  12301. '});',
  12302. 'this.DoIt = function (vG,vH,vI) {',
  12303. ' var vJ = $mod.TRecord.$new();',
  12304. ' $mod.DoIt(vJ.i, vJ.i, {',
  12305. ' p: vJ,',
  12306. ' get: function () {',
  12307. ' return this.p.i;',
  12308. ' },',
  12309. ' set: function (v) {',
  12310. ' this.p.i = v;',
  12311. ' }',
  12312. ' });',
  12313. '};',
  12314. 'this.r = this.TRecord.$new();'
  12315. ]),
  12316. LinesToStr([
  12317. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  12318. ' p: $mod.r,',
  12319. ' get: function () {',
  12320. ' return this.p.i;',
  12321. ' },',
  12322. ' set: function (v) {',
  12323. ' this.p.i = v;',
  12324. ' }',
  12325. '});'
  12326. ]));
  12327. end;
  12328. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  12329. begin
  12330. StartProgram(false);
  12331. Add('type');
  12332. Add(' integer = longint;');
  12333. Add(' TRecord = record');
  12334. Add(' i: integer;');
  12335. Add(' end;');
  12336. Add('function GetRec(vB: integer = 0): TRecord;');
  12337. Add('begin');
  12338. Add('end;');
  12339. Add('procedure DoIt(vG: integer; const vH: integer);');
  12340. Add('begin');
  12341. Add('end;');
  12342. Add('begin');
  12343. Add(' doit(getrec.i,getrec.i);');
  12344. Add(' doit(getrec().i,getrec().i);');
  12345. Add(' doit(getrec(1).i,getrec(2).i);');
  12346. ConvertProgram;
  12347. CheckSource('TestRecordElementFromFuncResult_AsParams',
  12348. LinesToStr([ // statements
  12349. 'rtl.recNewT(this, "TRecord", function () {',
  12350. ' this.i = 0;',
  12351. ' this.$eq = function (b) {',
  12352. ' return this.i === b.i;',
  12353. ' };',
  12354. ' this.$assign = function (s) {',
  12355. ' this.i = s.i;',
  12356. ' return this;',
  12357. ' };',
  12358. '});',
  12359. 'this.GetRec = function (vB) {',
  12360. ' var Result = $mod.TRecord.$new();',
  12361. ' return Result;',
  12362. '};',
  12363. 'this.DoIt = function (vG, vH) {',
  12364. '};',
  12365. '']),
  12366. LinesToStr([
  12367. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  12368. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  12369. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  12370. '']));
  12371. end;
  12372. procedure TTestModule.TestRecordElementFromWith_AsParams;
  12373. begin
  12374. StartProgram(false);
  12375. Add('type');
  12376. Add(' integer = longint;');
  12377. Add(' TRecord = record');
  12378. Add(' i: integer;');
  12379. Add(' end;');
  12380. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  12381. Add('begin');
  12382. Add('end;');
  12383. Add('var r: trecord;');
  12384. Add('begin');
  12385. Add(' with r do ');
  12386. Add(' doit(i,i,i);');
  12387. ConvertProgram;
  12388. CheckSource('TestRecordElementFromWith_AsParams',
  12389. LinesToStr([ // statements
  12390. 'rtl.recNewT(this, "TRecord", function () {',
  12391. ' this.i = 0;',
  12392. ' this.$eq = function (b) {',
  12393. ' return this.i === b.i;',
  12394. ' };',
  12395. ' this.$assign = function (s) {',
  12396. ' this.i = s.i;',
  12397. ' return this;',
  12398. ' };',
  12399. '});',
  12400. 'this.DoIt = function (vG,vH,vI) {',
  12401. '};',
  12402. 'this.r = this.TRecord.$new();'
  12403. ]),
  12404. LinesToStr([
  12405. 'var $with = $mod.r;',
  12406. '$mod.DoIt($with.i,$with.i,{',
  12407. ' p: $with,',
  12408. ' get: function () {',
  12409. ' return this.p.i;',
  12410. ' },',
  12411. ' set: function (v) {',
  12412. ' this.p.i = v;',
  12413. ' }',
  12414. '});',
  12415. '']));
  12416. end;
  12417. procedure TTestModule.TestRecord_Equal;
  12418. begin
  12419. StartProgram(false);
  12420. Add('type');
  12421. Add(' integer = longint;');
  12422. Add(' TFlag = (red,blue);');
  12423. Add(' TFlags = set of TFlag;');
  12424. Add(' TProc = procedure;');
  12425. Add(' TRecord = record');
  12426. Add(' i: integer;');
  12427. Add(' Event: TProc;');
  12428. Add(' f: TFlags;');
  12429. Add(' end;');
  12430. Add(' TNested = record');
  12431. Add(' r: TRecord;');
  12432. Add(' end;');
  12433. Add('var');
  12434. Add(' b: boolean;');
  12435. Add(' r,s: trecord;');
  12436. Add('begin');
  12437. Add(' b:=r=s;');
  12438. Add(' b:=r<>s;');
  12439. ConvertProgram;
  12440. CheckSource('TestRecord_Equal',
  12441. LinesToStr([ // statements
  12442. 'this.TFlag = {',
  12443. ' "0": "red",',
  12444. ' red: 0,',
  12445. ' "1": "blue",',
  12446. ' blue: 1',
  12447. '};',
  12448. 'rtl.recNewT(this, "TRecord", function () {',
  12449. ' this.i = 0;',
  12450. ' this.Event = null;',
  12451. ' this.$new = function () {',
  12452. ' var r = Object.create(this);',
  12453. ' r.f = {};',
  12454. ' return r;',
  12455. ' };',
  12456. ' this.$eq = function (b) {',
  12457. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  12458. ' };',
  12459. ' this.$assign = function (s) {',
  12460. ' this.i = s.i;',
  12461. ' this.Event = s.Event;',
  12462. ' this.f = rtl.refSet(s.f);',
  12463. ' return this;',
  12464. ' };',
  12465. '});',
  12466. 'rtl.recNewT(this, "TNested", function () {',
  12467. ' this.$new = function () {',
  12468. ' var r = Object.create(this);',
  12469. ' r.r = $mod.TRecord.$new();',
  12470. ' return r;',
  12471. ' };',
  12472. ' this.$eq = function (b) {',
  12473. ' return this.r.$eq(b.r);',
  12474. ' };',
  12475. ' this.$assign = function (s) {',
  12476. ' this.r.$assign(s.r);',
  12477. ' return this;',
  12478. ' };',
  12479. '});',
  12480. 'this.b = false;',
  12481. 'this.r = this.TRecord.$new();',
  12482. 'this.s = this.TRecord.$new();',
  12483. '']),
  12484. LinesToStr([
  12485. '$mod.b = $mod.r.$eq($mod.s);',
  12486. '$mod.b = !$mod.r.$eq($mod.s);',
  12487. '']));
  12488. end;
  12489. procedure TTestModule.TestRecord_JSValue;
  12490. begin
  12491. StartProgram(false);
  12492. Add([
  12493. 'type',
  12494. ' TRecord = record',
  12495. ' i: longint;',
  12496. ' end;',
  12497. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  12498. 'begin',
  12499. 'end;',
  12500. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  12501. 'begin',
  12502. ' if jsvalue(d) then ;',
  12503. ' if jsvalue(c) then ;',
  12504. ' if jsvalue(v) then ;',
  12505. 'end;',
  12506. 'var',
  12507. ' Jv: jsvalue;',
  12508. ' Rec: trecord;',
  12509. 'begin',
  12510. ' rec:=trecord(jv);',
  12511. ' jv:=rec;',
  12512. ' Fly(rec,rec);',
  12513. ' Fly(@rec,@rec);',
  12514. ' if jsvalue(Rec) then ;',
  12515. ' Run(trecord(jv),trecord(jv),rec);',
  12516. '']);
  12517. ConvertProgram;
  12518. CheckSource('TestRecord_JSValue',
  12519. LinesToStr([ // statements
  12520. 'rtl.recNewT(this, "TRecord", function () {',
  12521. ' this.i = 0;',
  12522. ' this.$eq = function (b) {',
  12523. ' return this.i === b.i;',
  12524. ' };',
  12525. ' this.$assign = function (s) {',
  12526. ' this.i = s.i;',
  12527. ' return this;',
  12528. ' };',
  12529. '});',
  12530. 'this.Fly = function (d, c) {',
  12531. '};',
  12532. 'this.Run = function (d, c, v) {',
  12533. ' if (d) ;',
  12534. ' if (c) ;',
  12535. ' if (v) ;',
  12536. '};',
  12537. 'this.Jv = undefined;',
  12538. 'this.Rec = this.TRecord.$new();',
  12539. '']),
  12540. LinesToStr([
  12541. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  12542. '$mod.Jv = $mod.Rec;',
  12543. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  12544. '$mod.Fly($mod.Rec, $mod.Rec);',
  12545. 'if ($mod.Rec) ;',
  12546. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  12547. '']));
  12548. end;
  12549. procedure TTestModule.TestRecord_VariantFail;
  12550. begin
  12551. StartProgram(false);
  12552. Add([
  12553. 'type',
  12554. ' TRec = record',
  12555. ' case word of',
  12556. ' 0: (b0, b1: Byte);',
  12557. ' 1: (i: word);',
  12558. ' end;',
  12559. 'begin']);
  12560. SetExpectedPasResolverError('Not supported: variant record',
  12561. nNotSupportedX);
  12562. ConvertProgram;
  12563. end;
  12564. procedure TTestModule.TestRecord_FieldArray;
  12565. begin
  12566. StartProgram(false);
  12567. Add([
  12568. 'type',
  12569. ' TArrInt = array[3..4] of longint;',
  12570. ' TArrArrInt = array[3..4] of longint;',
  12571. ' TRec = record',
  12572. ' a: array of longint;',
  12573. ' s: array[1..2] of longint;',
  12574. ' m: array[1..2,3..4] of longint;',
  12575. ' o: TArrArrInt;',
  12576. ' end;',
  12577. 'begin']);
  12578. ConvertProgram;
  12579. CheckSource('TestRecord_FieldArray',
  12580. LinesToStr([ // statements
  12581. 'rtl.recNewT(this, "TRec", function () {',
  12582. ' this.m$a$clone = function (a) {',
  12583. ' var b = [];',
  12584. ' b.length = 2;',
  12585. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12586. ' return b;',
  12587. ' };',
  12588. ' this.$new = function () {',
  12589. ' var r = Object.create(this);',
  12590. ' r.a = [];',
  12591. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12592. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12593. ' r.o = rtl.arraySetLength(null, 0, 2);',
  12594. ' return r;',
  12595. ' };',
  12596. ' this.$eq = function (b) {',
  12597. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  12598. ' };',
  12599. ' this.$assign = function (s) {',
  12600. ' this.a = rtl.arrayRef(s.a);',
  12601. ' this.s = s.s.slice(0);',
  12602. ' this.m = this.m$a$clone(s.m);',
  12603. ' this.o = s.o.slice(0);',
  12604. ' return this;',
  12605. ' };',
  12606. '});',
  12607. '']),
  12608. LinesToStr([ // $mod.$main
  12609. '']));
  12610. end;
  12611. procedure TTestModule.TestRecord_Const;
  12612. begin
  12613. StartProgram(false);
  12614. Add([
  12615. 'type',
  12616. ' TArrInt = array[3..4] of longint;',
  12617. ' TPoint = record x,y: longint; end;',
  12618. ' TRec = record',
  12619. ' i: longint;',
  12620. ' a: array of longint;',
  12621. ' s: array[1..2] of longint;',
  12622. ' m: array[1..2,3..4] of longint;',
  12623. ' p: TPoint;',
  12624. ' end;',
  12625. ' TPoints = array of TPoint;',
  12626. 'const',
  12627. ' r: TRec = (',
  12628. ' i:1;',
  12629. ' a:(2,3);',
  12630. ' s:(4,5);',
  12631. ' m:( (11,12), (13,14) );',
  12632. ' p: (x:21; y:22)',
  12633. ' );',
  12634. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  12635. 'begin']);
  12636. ConvertProgram;
  12637. CheckSource('TestRecord_Const',
  12638. LinesToStr([ // statements
  12639. 'rtl.recNewT(this, "TPoint", function () {',
  12640. ' this.x = 0;',
  12641. ' this.y = 0;',
  12642. ' this.$eq = function (b) {',
  12643. ' return (this.x === b.x) && (this.y === b.y);',
  12644. ' };',
  12645. ' this.$assign = function (s) {',
  12646. ' this.x = s.x;',
  12647. ' this.y = s.y;',
  12648. ' return this;',
  12649. ' };',
  12650. '});',
  12651. 'rtl.recNewT(this, "TRec", function () {',
  12652. ' this.i = 0;',
  12653. ' this.m$a$clone = function (a) {',
  12654. ' var b = [];',
  12655. ' b.length = 2;',
  12656. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12657. ' return b;',
  12658. ' };',
  12659. ' this.$new = function () {',
  12660. ' var r = Object.create(this);',
  12661. ' r.a = [];',
  12662. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12663. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12664. ' r.p = $mod.TPoint.$new();',
  12665. ' return r;',
  12666. ' };',
  12667. ' this.$eq = function (b) {',
  12668. ' return (this.i === b.i) && (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && this.p.$eq(b.p);',
  12669. ' };',
  12670. ' this.$assign = function (s) {',
  12671. ' this.i = s.i;',
  12672. ' this.a = rtl.arrayRef(s.a);',
  12673. ' this.s = s.s.slice(0);',
  12674. ' this.m = this.m$a$clone(s.m);',
  12675. ' this.p.$assign(s.p);',
  12676. ' return this;',
  12677. ' };',
  12678. '});',
  12679. 'this.r = this.TRec.$clone({',
  12680. ' i: 1,',
  12681. ' a: [2, 3],',
  12682. ' s: [4, 5],',
  12683. ' m: [[11, 12], [13, 14]],',
  12684. ' p: this.TPoint.$clone({',
  12685. ' x: 21,',
  12686. ' y: 22',
  12687. ' })',
  12688. '});',
  12689. 'this.p = [this.TPoint.$clone({',
  12690. ' x: 1,',
  12691. ' y: 2',
  12692. '}), this.TPoint.$clone({',
  12693. ' x: 3,',
  12694. ' y: 4',
  12695. '})];',
  12696. '']),
  12697. LinesToStr([ // $mod.$main
  12698. '']));
  12699. end;
  12700. procedure TTestModule.TestRecord_TypecastFail;
  12701. begin
  12702. StartProgram(false);
  12703. Add([
  12704. 'type',
  12705. ' TPoint = record x,y: longint; end;',
  12706. ' TRec = record l: longint end;',
  12707. 'var p: TPoint;',
  12708. 'begin',
  12709. ' if TRec(p).l=2 then ;']);
  12710. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  12711. nIllegalTypeConversionTo);
  12712. ConvertProgram;
  12713. end;
  12714. procedure TTestModule.TestRecord_InFunction;
  12715. begin
  12716. StartProgram(false);
  12717. Add([
  12718. 'var TPoint: longint = 3;',
  12719. 'procedure DoIt;',
  12720. 'type',
  12721. ' TPoint = record x,y: longint; end;',
  12722. ' TPoints = array of TPoint;',
  12723. 'var',
  12724. ' r: TPoint;',
  12725. ' p: TPoints;',
  12726. 'begin',
  12727. ' SetLength(p,2);',
  12728. 'end;',
  12729. 'begin']);
  12730. ConvertProgram;
  12731. CheckSource('TestRecord_InFunction',
  12732. LinesToStr([ // statements
  12733. 'this.TPoint = 3;',
  12734. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  12735. ' this.x = 0;',
  12736. ' this.y = 0;',
  12737. ' this.$eq = function (b) {',
  12738. ' return (this.x === b.x) && (this.y === b.y);',
  12739. ' };',
  12740. ' this.$assign = function (s) {',
  12741. ' this.x = s.x;',
  12742. ' this.y = s.y;',
  12743. ' return this;',
  12744. ' };',
  12745. '});',
  12746. 'this.DoIt = function () {',
  12747. ' var r = TPoint$1.$new();',
  12748. ' var p = [];',
  12749. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  12750. '};',
  12751. '']),
  12752. LinesToStr([ // $mod.$main
  12753. '']));
  12754. end;
  12755. procedure TTestModule.TestRecord_ArrayConstMultiline;
  12756. begin
  12757. StartProgram(false);
  12758. Add([
  12759. '{$mode delphi}',
  12760. 'type',
  12761. ' TBird = record Wing: string; end;',
  12762. 'const',
  12763. ' Birds: array[1..2] of TBird = (',
  12764. ' (Wing: ''''''',
  12765. ' First',
  12766. ' Second',
  12767. ' Third',
  12768. ' ''''''),',
  12769. ' (Wing: ''''''',
  12770. ' Value:=''Im in quotes''; ',
  12771. ' '''''')',
  12772. ' );',
  12773. 'begin']);
  12774. ConvertProgram;
  12775. CheckSource('TestRecord_ArrayConstMultiline',
  12776. LinesToStr([ // statements
  12777. 'rtl.recNewT(this, "TBird", function () {',
  12778. ' this.Wing = "";',
  12779. ' this.$eq = function (b) {',
  12780. ' return this.Wing === b.Wing;',
  12781. ' };',
  12782. ' this.$assign = function (s) {',
  12783. ' this.Wing = s.Wing;',
  12784. ' return this;',
  12785. ' };',
  12786. '});',
  12787. 'this.Birds$a$clone = function (a) {',
  12788. ' var b = [];',
  12789. ' b.length = 2;',
  12790. ' for (var c = 0; c < 2; c++) b[c] = $mod.TBird.$clone(a[c]);',
  12791. ' return b;',
  12792. '};',
  12793. 'this.Birds = [this.TBird.$clone({',
  12794. ' Wing: " First\n Second\n Third"',
  12795. '}), this.TBird.$clone({',
  12796. ' Wing: " Value:=''Im in quotes''; "',
  12797. '})];',
  12798. '']),
  12799. LinesToStr([ // $mod.$main
  12800. '']));
  12801. end;
  12802. procedure TTestModule.TestRecordAnonym_Field;
  12803. begin
  12804. StartProgram(false);
  12805. Add(['',
  12806. 'var Rec: record',
  12807. ' Bold: longint;',
  12808. ' end;',
  12809. 'begin',
  12810. ' rec.bold:=123;',
  12811. ' rec.bold:=rec.bold+7;',
  12812. '']);
  12813. ConvertProgram;
  12814. CheckSource('TestRecordAnonym_Field',
  12815. LinesToStr([ // statements
  12816. 'rtl.recNewT(this, "Rec$a", function () {',
  12817. ' this.Bold = 0;',
  12818. ' this.$eq = function (b) {',
  12819. ' return this.Bold === b.Bold;',
  12820. ' };',
  12821. ' this.$assign = function (s) {',
  12822. ' this.Bold = s.Bold;',
  12823. ' return this;',
  12824. ' };',
  12825. '});',
  12826. 'this.Rec = this.Rec$a.$new();',
  12827. '']),
  12828. LinesToStr([ // $mod.$main
  12829. '$mod.Rec.Bold = 123;',
  12830. '$mod.Rec.Bold = $mod.Rec.Bold + 7;',
  12831. '']));
  12832. end;
  12833. procedure TTestModule.TestRecordAnonym_Assign;
  12834. begin
  12835. StartProgram(false);
  12836. Add(['',
  12837. 'var S,T: record',
  12838. ' Bold: longint;',
  12839. ' end;',
  12840. ' b: boolean;',
  12841. 'begin',
  12842. ' S:=T;',
  12843. ' b:=s=t;',
  12844. '']);
  12845. ConvertProgram;
  12846. CheckSource('TestRecordAnonym_Assign',
  12847. LinesToStr([ // statements
  12848. 'rtl.recNewT(this, "T$a", function () {',
  12849. ' this.Bold = 0;',
  12850. ' this.$eq = function (b) {',
  12851. ' return this.Bold === b.Bold;',
  12852. ' };',
  12853. ' this.$assign = function (s) {',
  12854. ' this.Bold = s.Bold;',
  12855. ' return this;',
  12856. ' };',
  12857. '});',
  12858. 'this.S = this.T$a.$new();',
  12859. 'this.T = this.T$a.$new();',
  12860. 'this.b = false;',
  12861. '']),
  12862. LinesToStr([ // $mod.$main
  12863. '$mod.S.$assign($mod.T);',
  12864. '$mod.b = $mod.S.$eq($mod.T);',
  12865. '']));
  12866. end;
  12867. procedure TTestModule.TestRecordAnonym_Nested;
  12868. begin
  12869. StartProgram(false);
  12870. Add(['',
  12871. 'var S,T: record',
  12872. ' Bold: longint;',
  12873. ' Sub: record',
  12874. ' Color: word;',
  12875. ' end;',
  12876. ' end;',
  12877. ' b: boolean;',
  12878. 'begin',
  12879. ' S:=T;',
  12880. ' S.Sub:=T.Sub;',
  12881. ' S.Sub.Color:=T.Sub.Color+3;',
  12882. ' b:=s=t;',
  12883. ' b:=s.Sub=t.Sub;',
  12884. '']);
  12885. ConvertProgram;
  12886. CheckSource('TestRecordAnonym_Nested',
  12887. LinesToStr([ // statements
  12888. 'rtl.recNewT(this, "T$a", function () {',
  12889. ' this.Bold = 0;',
  12890. ' rtl.recNewT(this, "Sub$a", function () {',
  12891. ' this.Color = 0;',
  12892. ' this.$eq = function (b) {',
  12893. ' return this.Color === b.Color;',
  12894. ' };',
  12895. ' this.$assign = function (s) {',
  12896. ' this.Color = s.Color;',
  12897. ' return this;',
  12898. ' };',
  12899. ' });',
  12900. ' this.$new = function () {',
  12901. ' var r = Object.create(this);',
  12902. ' r.Sub = this.Sub$a.$new();',
  12903. ' return r;',
  12904. ' };',
  12905. ' this.$eq = function (b) {',
  12906. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12907. ' };',
  12908. ' this.$assign = function (s) {',
  12909. ' this.Bold = s.Bold;',
  12910. ' this.Sub.$assign(s.Sub);',
  12911. ' return this;',
  12912. ' };',
  12913. '}, true);',
  12914. 'this.S = this.T$a.$new();',
  12915. 'this.T = this.T$a.$new();',
  12916. 'this.b = false;',
  12917. '']),
  12918. LinesToStr([ // $mod.$main
  12919. '$mod.S.$assign($mod.T);',
  12920. '$mod.S.Sub.$assign($mod.T.Sub);',
  12921. '$mod.S.Sub.Color = $mod.T.Sub.Color + 3;',
  12922. '$mod.b = $mod.S.$eq($mod.T);',
  12923. '$mod.b = $mod.S.Sub.$eq($mod.T.Sub);',
  12924. '']));
  12925. end;
  12926. procedure TTestModule.TestRecordAnonym_Const;
  12927. begin
  12928. StartProgram(false);
  12929. Add(['',
  12930. 'var T: record',
  12931. ' Bold: longint;',
  12932. ' Sub: record',
  12933. ' Color: word;',
  12934. ' end;',
  12935. ' end = (Bold: 2; Sub: (Color: 3));',
  12936. 'begin',
  12937. '']);
  12938. ConvertProgram;
  12939. CheckSource('TestRecordAnonym_Const',
  12940. LinesToStr([ // statements
  12941. 'rtl.recNewT(this, "T$a", function () {',
  12942. ' this.Bold = 0;',
  12943. ' rtl.recNewT(this, "Sub$a", function () {',
  12944. ' this.Color = 0;',
  12945. ' this.$eq = function (b) {',
  12946. ' return this.Color === b.Color;',
  12947. ' };',
  12948. ' this.$assign = function (s) {',
  12949. ' this.Color = s.Color;',
  12950. ' return this;',
  12951. ' };',
  12952. ' });',
  12953. ' this.$new = function () {',
  12954. ' var r = Object.create(this);',
  12955. ' r.Sub = this.Sub$a.$new();',
  12956. ' return r;',
  12957. ' };',
  12958. ' this.$eq = function (b) {',
  12959. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12960. ' };',
  12961. ' this.$assign = function (s) {',
  12962. ' this.Bold = s.Bold;',
  12963. ' this.Sub.$assign(s.Sub);',
  12964. ' return this;',
  12965. ' };',
  12966. '}, true);',
  12967. 'this.T = this.T$a.$clone({',
  12968. ' Bold: 2,',
  12969. ' Sub: this.T$a.Sub$a.$clone({',
  12970. ' Color: 3',
  12971. ' })',
  12972. '});',
  12973. '']),
  12974. LinesToStr([ // $mod.$main
  12975. '']));
  12976. end;
  12977. procedure TTestModule.TestRecordAnonym_InFunction;
  12978. begin
  12979. StartProgram(false);
  12980. Add(['',
  12981. 'procedure Fly;',
  12982. 'var T: record',
  12983. ' Bold: longint;',
  12984. ' Sub: record',
  12985. ' Color: word;',
  12986. ' end;',
  12987. ' end = (Bold: 2; Sub: (Color: 3));',
  12988. 'begin',
  12989. 'end;',
  12990. 'begin',
  12991. '']);
  12992. ConvertProgram;
  12993. CheckSource('TestRecordAnonym_InFunction',
  12994. LinesToStr([ // statements
  12995. 'var T$a = rtl.recNewT(null, "", function () {',
  12996. ' this.Bold = 0;',
  12997. ' rtl.recNewT(this, "Sub$a", function () {',
  12998. ' this.Color = 0;',
  12999. ' this.$eq = function (b) {',
  13000. ' return this.Color === b.Color;',
  13001. ' };',
  13002. ' this.$assign = function (s) {',
  13003. ' this.Color = s.Color;',
  13004. ' return this;',
  13005. ' };',
  13006. ' });',
  13007. ' this.$new = function () {',
  13008. ' var r = Object.create(this);',
  13009. ' r.Sub = this.Sub$a.$new();',
  13010. ' return r;',
  13011. ' };',
  13012. ' this.$eq = function (b) {',
  13013. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  13014. ' };',
  13015. ' this.$assign = function (s) {',
  13016. ' this.Bold = s.Bold;',
  13017. ' this.Sub.$assign(s.Sub);',
  13018. ' return this;',
  13019. ' };',
  13020. '}, true);',
  13021. 'this.Fly = function () {',
  13022. ' var T = T$a.$clone({',
  13023. ' Bold: 2,',
  13024. ' Sub: T$a.Sub$a.$clone({',
  13025. ' Color: 3',
  13026. ' })',
  13027. ' });',
  13028. '};',
  13029. '']),
  13030. LinesToStr([ // $mod.$main
  13031. '']));
  13032. end;
  13033. procedure TTestModule.TestAdvRecord_Function;
  13034. begin
  13035. StartProgram(false);
  13036. Parser.Options:=Parser.Options+[po_cassignments];
  13037. Add([
  13038. '{$modeswitch AdvancedRecords}',
  13039. 'type',
  13040. ' TPoint = record',
  13041. ' x,y: word;',
  13042. ' function Add(const apt: TPoint): TPoint;',
  13043. ' end;',
  13044. 'function TPoint.Add(const apt: TPoint): TPoint;',
  13045. 'begin',
  13046. ' Result:=Self;',
  13047. ' Result.x+=apt.x;',
  13048. ' Result.y:=Result.y+apt.y;',
  13049. ' Self:=apt;',
  13050. 'end;',
  13051. 'var p,q: TPoint;',
  13052. 'begin',
  13053. ' p.add(q);',
  13054. ' p:=default(TPoint);',
  13055. ' p:=q;',
  13056. '']);
  13057. ConvertProgram;
  13058. CheckSource('TestAdvRecord_Function',
  13059. LinesToStr([ // statements
  13060. 'rtl.recNewT(this, "TPoint", function () {',
  13061. ' this.x = 0;',
  13062. ' this.y = 0;',
  13063. ' this.$eq = function (b) {',
  13064. ' return (this.x === b.x) && (this.y === b.y);',
  13065. ' };',
  13066. ' this.$assign = function (s) {',
  13067. ' this.x = s.x;',
  13068. ' this.y = s.y;',
  13069. ' return this;',
  13070. ' };',
  13071. ' this.Add = function (apt) {',
  13072. ' var Result = $mod.TPoint.$new();',
  13073. ' Result.$assign(this);',
  13074. ' Result.x += apt.x;',
  13075. ' Result.y = Result.y + apt.y;',
  13076. ' this.$assign(apt);',
  13077. ' return Result;',
  13078. ' };',
  13079. '});',
  13080. 'this.p = this.TPoint.$new();',
  13081. 'this.q = this.TPoint.$new();',
  13082. '']),
  13083. LinesToStr([ // $mod.$main
  13084. '$mod.p.Add($mod.q);',
  13085. '$mod.p.$assign($mod.TPoint.$new());',
  13086. '$mod.p.$assign($mod.q);',
  13087. '']));
  13088. end;
  13089. procedure TTestModule.TestAdvRecord_Property;
  13090. begin
  13091. StartProgram(false);
  13092. Add([
  13093. '{$modeswitch AdvancedRecords}',
  13094. 'type',
  13095. ' TPoint = record',
  13096. ' x,y: word;',
  13097. ' strict private',
  13098. ' function GetSize: longword;',
  13099. ' procedure SetSize(Value: longword);',
  13100. ' public',
  13101. ' property Size: longword read GetSize write SetSize;',
  13102. ' property Left: word read x write y;',
  13103. ' end;',
  13104. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  13105. 'function TPoint.GetSize: longword;',
  13106. 'begin',
  13107. ' x:=y;',
  13108. ' Size:=Size;',
  13109. ' Left:=Left;',
  13110. 'end;',
  13111. 'procedure TPoint.SetSize(Value: longword);',
  13112. 'begin',
  13113. 'end;',
  13114. 'var p,q: TPoint;',
  13115. 'begin',
  13116. ' p.Size:=q.Size;',
  13117. ' p.Left:=q.Left;',
  13118. '']);
  13119. ConvertProgram;
  13120. CheckSource('TestAdvRecord_Property',
  13121. LinesToStr([ // statements
  13122. 'rtl.recNewT(this, "TPoint", function () {',
  13123. ' this.x = 0;',
  13124. ' this.y = 0;',
  13125. ' this.$eq = function (b) {',
  13126. ' return (this.x === b.x) && (this.y === b.y);',
  13127. ' };',
  13128. ' this.$assign = function (s) {',
  13129. ' this.x = s.x;',
  13130. ' this.y = s.y;',
  13131. ' return this;',
  13132. ' };',
  13133. ' this.GetSize = function () {',
  13134. ' var Result = 0;',
  13135. ' this.x = this.y;',
  13136. ' this.SetSize(this.GetSize());',
  13137. ' this.y = this.x;',
  13138. ' return Result;',
  13139. ' };',
  13140. ' this.SetSize = function (Value) {',
  13141. ' };',
  13142. '});',
  13143. 'this.SetSize = function (Value) {',
  13144. '};',
  13145. 'this.p = this.TPoint.$new();',
  13146. 'this.q = this.TPoint.$new();',
  13147. '']),
  13148. LinesToStr([ // $mod.$main
  13149. '$mod.p.SetSize($mod.q.GetSize());',
  13150. '$mod.p.y = $mod.q.x;',
  13151. '']));
  13152. end;
  13153. procedure TTestModule.TestAdvRecord_PropertyDefault;
  13154. begin
  13155. StartProgram(false);
  13156. Add([
  13157. '{$modeswitch AdvancedRecords}',
  13158. 'type',
  13159. ' TPoint = record',
  13160. ' strict private',
  13161. ' function GetItems(Index: word): word;',
  13162. ' procedure SetItems(Index: word; Value: word);',
  13163. ' public',
  13164. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  13165. ' end;',
  13166. 'function TPoint.GetItems(Index: word): word;',
  13167. 'begin',
  13168. ' Items[index]:=Items[index];',
  13169. ' self.Items[index]:=self.Items[index];',
  13170. 'end;',
  13171. 'procedure TPoint.SetItems(Index: word; Value: word);',
  13172. 'begin',
  13173. 'end;',
  13174. 'var p: TPoint;',
  13175. 'begin',
  13176. ' p[1]:=p[2];',
  13177. ' p.Items[3]:=p.Items[4];',
  13178. '']);
  13179. ConvertProgram;
  13180. CheckSource('TestAdvRecord_PropertyDefault',
  13181. LinesToStr([ // statements
  13182. 'rtl.recNewT(this, "TPoint", function () {',
  13183. ' this.$eq = function (b) {',
  13184. ' return true;',
  13185. ' };',
  13186. ' this.$assign = function (s) {',
  13187. ' return this;',
  13188. ' };',
  13189. ' this.GetItems = function (Index) {',
  13190. ' var Result = 0;',
  13191. ' this.SetItems(Index, this.GetItems(Index));',
  13192. ' this.SetItems(Index, this.GetItems(Index));',
  13193. ' return Result;',
  13194. ' };',
  13195. ' this.SetItems = function (Index, Value) {',
  13196. ' };',
  13197. '});',
  13198. 'this.p = this.TPoint.$new();',
  13199. '']),
  13200. LinesToStr([ // $mod.$main
  13201. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  13202. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  13203. '']));
  13204. end;
  13205. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  13206. begin
  13207. StartProgram(false);
  13208. Add([
  13209. '{$modeswitch AdvancedRecords}',
  13210. 'type',
  13211. ' TRec = record',
  13212. ' class var',
  13213. ' Fx: longint;',
  13214. ' Fy: longint;',
  13215. ' class function GetInt: longint; static;',
  13216. ' class procedure SetInt(Value: longint); static;',
  13217. ' class procedure DoIt; static;',
  13218. ' class property IntA: longint read Fx write Fy;',
  13219. ' class property IntB: longint read GetInt write SetInt;',
  13220. ' end;',
  13221. 'class function trec.getint: longint;',
  13222. 'begin',
  13223. ' result:=fx;',
  13224. 'end;',
  13225. 'class procedure trec.setint(value: longint);',
  13226. 'begin',
  13227. 'end;',
  13228. 'class procedure trec.doit;',
  13229. 'begin',
  13230. ' IntA:=IntA+1;',
  13231. ' IntB:=IntB+1;',
  13232. 'end;',
  13233. 'var r: trec;',
  13234. 'begin',
  13235. ' trec.inta:=trec.inta+1;',
  13236. ' if trec.intb=2 then;',
  13237. ' trec.intb:=trec.intb+2;',
  13238. ' trec.setint(trec.inta);',
  13239. ' r.inta:=r.inta+1;',
  13240. ' if r.intb=2 then;',
  13241. ' r.intb:=r.intb+2;',
  13242. ' r.setint(r.inta);']);
  13243. ConvertProgram;
  13244. CheckSource('TestAdvRecord_Property_ClassMethod',
  13245. LinesToStr([ // statements
  13246. 'rtl.recNewT(this, "TRec", function () {',
  13247. ' this.Fx = 0;',
  13248. ' this.Fy = 0;',
  13249. ' this.$eq = function (b) {',
  13250. ' return true;',
  13251. ' };',
  13252. ' this.$assign = function (s) {',
  13253. ' return this;',
  13254. ' };',
  13255. ' this.GetInt = function () {',
  13256. ' var Result = 0;',
  13257. ' Result = $mod.TRec.Fx;',
  13258. ' return Result;',
  13259. ' };',
  13260. ' this.SetInt = function (Value) {',
  13261. ' };',
  13262. ' this.DoIt = function () {',
  13263. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  13264. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  13265. ' };',
  13266. '}, true);',
  13267. 'this.r = this.TRec.$new();',
  13268. '']),
  13269. LinesToStr([ // $mod.$main
  13270. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  13271. 'if ($mod.TRec.GetInt() === 2) ;',
  13272. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  13273. '$mod.TRec.SetInt($mod.TRec.Fx);',
  13274. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  13275. 'if ($mod.TRec.GetInt() === 2) ;',
  13276. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  13277. '$mod.TRec.SetInt($mod.r.Fx);',
  13278. '']));
  13279. end;
  13280. procedure TTestModule.TestAdvRecord_Const;
  13281. begin
  13282. StartProgram(false);
  13283. Add([
  13284. '{$modeswitch AdvancedRecords}',
  13285. 'type',
  13286. ' TArrInt = array[3..4] of longint;',
  13287. ' TPoint = record',
  13288. ' x,y: longint;',
  13289. ' class var Count: nativeint;',
  13290. ' end;',
  13291. ' TRec = record',
  13292. ' i: longint;',
  13293. ' a: array of longint;',
  13294. ' s: array[1..2] of longint;',
  13295. ' m: array[1..2,3..4] of longint;',
  13296. ' p: TPoint;',
  13297. ' end;',
  13298. ' TPoints = array of TPoint;',
  13299. 'const',
  13300. ' r: TRec = (',
  13301. ' i:1;',
  13302. ' a:(2,3);',
  13303. ' s:(4,5);',
  13304. ' m:( (11,12), (13,14) );',
  13305. ' p: (x:21)',
  13306. ' );',
  13307. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  13308. 'begin']);
  13309. ConvertProgram;
  13310. CheckSource('TestAdvRecord_Const',
  13311. LinesToStr([ // statements
  13312. 'rtl.recNewT(this, "TPoint", function () {',
  13313. ' this.x = 0;',
  13314. ' this.y = 0;',
  13315. ' this.Count = 0;',
  13316. ' this.$eq = function (b) {',
  13317. ' return (this.x === b.x) && (this.y === b.y);',
  13318. ' };',
  13319. ' this.$assign = function (s) {',
  13320. ' this.x = s.x;',
  13321. ' this.y = s.y;',
  13322. ' return this;',
  13323. ' };',
  13324. '}, true);',
  13325. 'rtl.recNewT(this, "TRec", function () {',
  13326. ' this.i = 0;',
  13327. ' this.m$a$clone = function (a) {',
  13328. ' var b = [];',
  13329. ' b.length = 2;',
  13330. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  13331. ' return b;',
  13332. ' };',
  13333. ' this.$new = function () {',
  13334. ' var r = Object.create(this);',
  13335. ' r.a = [];',
  13336. ' r.s = rtl.arraySetLength(null, 0, 2);',
  13337. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  13338. ' r.p = $mod.TPoint.$new();',
  13339. ' return r;',
  13340. ' };',
  13341. ' this.$eq = function (b) {',
  13342. ' return (this.i === b.i) && (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && this.p.$eq(b.p);',
  13343. ' };',
  13344. ' this.$assign = function (s) {',
  13345. ' this.i = s.i;',
  13346. ' this.a = rtl.arrayRef(s.a);',
  13347. ' this.s = s.s.slice(0);',
  13348. ' this.m = this.m$a$clone(s.m);',
  13349. ' this.p.$assign(s.p);',
  13350. ' return this;',
  13351. ' };',
  13352. '});',
  13353. 'this.r = this.TRec.$clone({',
  13354. ' i: 1,',
  13355. ' a: [2, 3],',
  13356. ' s: [4, 5],',
  13357. ' m: [[11, 12], [13, 14]],',
  13358. ' p: this.TPoint.$clone({',
  13359. ' x: 21,',
  13360. ' y: 0',
  13361. ' })',
  13362. '});',
  13363. 'this.p = [this.TPoint.$clone({',
  13364. ' x: 1,',
  13365. ' y: 2',
  13366. '}), this.TPoint.$clone({',
  13367. ' x: 3,',
  13368. ' y: 4',
  13369. '})];',
  13370. '']),
  13371. LinesToStr([ // $mod.$main
  13372. '']));
  13373. end;
  13374. procedure TTestModule.TestAdvRecord_ExternalField;
  13375. begin
  13376. StartProgram(false);
  13377. Add([
  13378. '{$modeswitch AdvancedRecords}',
  13379. '{$modeswitch externalclass}',
  13380. 'type',
  13381. ' TCar = record',
  13382. ' public',
  13383. ' Intern: longint external name ''$Intern'';',
  13384. ' Intern2: longint external name ''$Intern2'';',
  13385. ' Bracket: longint external name ''["A B"]'';',
  13386. ' procedure DoIt;',
  13387. ' end;',
  13388. 'procedure tcar.doit;',
  13389. 'begin',
  13390. ' Intern:=Intern+1;',
  13391. ' Intern2:=Intern2+2;',
  13392. ' Bracket:=Bracket+3;',
  13393. 'end;',
  13394. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  13395. 'begin',
  13396. ' Rec.intern:=Rec.intern+1;',
  13397. ' Rec.intern2:=Rec.intern2+2;',
  13398. ' Rec.Bracket:=Rec.Bracket+3;',
  13399. ' with Rec do begin',
  13400. ' intern:=intern+1;',
  13401. ' intern2:=intern2+2;',
  13402. ' Bracket:=Bracket+3;',
  13403. ' end;']);
  13404. ConvertProgram;
  13405. CheckSource('TestAdvRecord_ExternalField',
  13406. LinesToStr([ // statements
  13407. 'rtl.recNewT(this, "TCar", function () {',
  13408. ' this.$eq = function (b) {',
  13409. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  13410. ' };',
  13411. ' this.$assign = function (s) {',
  13412. ' this.$Intern = s.$Intern;',
  13413. ' this.$Intern2 = s.$Intern2;',
  13414. ' this["A B"] = s["A B"];',
  13415. ' return this;',
  13416. ' };',
  13417. ' this.DoIt = function () {',
  13418. ' this.$Intern = this.$Intern + 1;',
  13419. ' this.$Intern2 = this.$Intern2 + 2;',
  13420. ' this["A B"] = this["A B"] + 3;',
  13421. ' };',
  13422. '});',
  13423. 'this.Rec = this.TCar.$clone({',
  13424. ' $Intern: 11,',
  13425. ' $Intern2: 12,',
  13426. ' "A B": 13',
  13427. '});',
  13428. '']),
  13429. LinesToStr([ // $mod.$main
  13430. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  13431. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  13432. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  13433. 'var $with = $mod.Rec;',
  13434. '$with.$Intern = $with.$Intern + 1;',
  13435. '$with.$Intern2 = $with.$Intern2 + 2;',
  13436. '$with["A B"] = $with["A B"] + 3;',
  13437. '']));
  13438. end;
  13439. procedure TTestModule.TestAdvRecord_SubRecord;
  13440. begin
  13441. StartProgram(false);
  13442. Add([
  13443. '{$modeswitch AdvancedRecords}',
  13444. 'type',
  13445. ' TRec = record',
  13446. ' type',
  13447. ' TPoint = record',
  13448. ' x,y: longint;',
  13449. ' class var Count: nativeint;',
  13450. ' procedure DoIt;',
  13451. ' class procedure DoThat; static;',
  13452. ' end;',
  13453. ' var',
  13454. ' i: longint;',
  13455. ' p: TPoint;',
  13456. ' procedure DoSome;',
  13457. ' end;',
  13458. 'const',
  13459. ' r: TRec = (',
  13460. ' i:1;',
  13461. ' p: (x:21;y:22)',
  13462. ' );',
  13463. 'procedure TRec.DoSome;',
  13464. 'begin',
  13465. ' p.x:=p.y+1;',
  13466. ' p.Count:=p.Count+2;',
  13467. 'end;',
  13468. 'procedure TRec.TPoint.DoIt;',
  13469. 'begin',
  13470. ' Count:=Count+3;',
  13471. 'end;',
  13472. 'class procedure TRec.TPoint.DoThat;',
  13473. 'begin',
  13474. ' Count:=Count+4;',
  13475. 'end;',
  13476. 'begin']);
  13477. ConvertProgram;
  13478. CheckSource('TestAdvRecord_SubRecord',
  13479. LinesToStr([ // statements
  13480. 'rtl.recNewT(this, "TRec", function () {',
  13481. ' rtl.recNewT(this, "TPoint", function () {',
  13482. ' this.x = 0;',
  13483. ' this.y = 0;',
  13484. ' this.Count = 0;',
  13485. ' this.$eq = function (b) {',
  13486. ' return (this.x === b.x) && (this.y === b.y);',
  13487. ' };',
  13488. ' this.$assign = function (s) {',
  13489. ' this.x = s.x;',
  13490. ' this.y = s.y;',
  13491. ' return this;',
  13492. ' };',
  13493. ' this.DoIt = function () {',
  13494. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  13495. ' };',
  13496. ' this.DoThat = function () {',
  13497. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  13498. ' };',
  13499. ' }, true);',
  13500. ' this.i = 0;',
  13501. ' this.$new = function () {',
  13502. ' var r = Object.create(this);',
  13503. ' r.p = this.TPoint.$new();',
  13504. ' return r;',
  13505. ' };',
  13506. ' this.$eq = function (b) {',
  13507. ' return (this.i === b.i) && this.p.$eq(b.p);',
  13508. ' };',
  13509. ' this.$assign = function (s) {',
  13510. ' this.i = s.i;',
  13511. ' this.p.$assign(s.p);',
  13512. ' return this;',
  13513. ' };',
  13514. ' this.DoSome = function () {',
  13515. ' this.p.x = this.p.y + 1;',
  13516. ' this.TPoint.Count = this.p.Count + 2;',
  13517. ' };',
  13518. '}, true);',
  13519. 'this.r = this.TRec.$clone({',
  13520. ' i: 1,',
  13521. ' p: this.TRec.TPoint.$clone({',
  13522. ' x: 21,',
  13523. ' y: 22',
  13524. ' })',
  13525. '});',
  13526. '']),
  13527. LinesToStr([ // $mod.$main
  13528. '']));
  13529. end;
  13530. procedure TTestModule.TestAdvRecord_SubClass;
  13531. begin
  13532. StartProgram(false);
  13533. Add([
  13534. '{$modeswitch AdvancedRecords}',
  13535. 'type',
  13536. ' TObject = class end;',
  13537. ' TPoint = record',
  13538. ' type',
  13539. ' TBird = class',
  13540. ' procedure DoIt;',
  13541. ' class procedure Glob;',
  13542. ' end;',
  13543. ' procedure DoIt(b: TBird);',
  13544. ' end;',
  13545. 'procedure TPoint.TBird.DoIt;',
  13546. 'begin',
  13547. ' doit;',
  13548. ' self.doit;',
  13549. ' glob;',
  13550. ' self.glob;',
  13551. 'end;',
  13552. 'class procedure TPoint.TBird.Glob;',
  13553. 'begin',
  13554. ' glob;',
  13555. ' self.glob;',
  13556. 'end;',
  13557. 'procedure TPoint.DoIt(b: TBird);',
  13558. 'begin',
  13559. ' b.doit;',
  13560. ' b.glob;',
  13561. ' TBird.glob;',
  13562. 'end;',
  13563. 'begin',
  13564. '']);
  13565. ConvertProgram;
  13566. CheckSource('TestAdvRecord_SubClass',
  13567. LinesToStr([ // statements
  13568. 'rtl.createClass(this, "TObject", null, function () {',
  13569. ' this.$init = function () {',
  13570. ' };',
  13571. ' this.$final = function () {',
  13572. ' };',
  13573. '});',
  13574. 'rtl.recNewT(this, "TPoint", function () {',
  13575. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  13576. ' this.DoIt = function () {',
  13577. ' this.DoIt();',
  13578. ' this.DoIt();',
  13579. ' this.$class.Glob();',
  13580. ' this.$class.Glob();',
  13581. ' };',
  13582. ' this.Glob = function () {',
  13583. ' this.Glob();',
  13584. ' this.Glob();',
  13585. ' };',
  13586. ' }, "TPoint.TBird");',
  13587. ' this.$eq = function (b) {',
  13588. ' return true;',
  13589. ' };',
  13590. ' this.$assign = function (s) {',
  13591. ' return this;',
  13592. ' };',
  13593. ' this.DoIt = function (b) {',
  13594. ' b.DoIt();',
  13595. ' b.$class.Glob();',
  13596. ' this.TBird.Glob();',
  13597. ' };',
  13598. '}, true);',
  13599. '']),
  13600. LinesToStr([ // $mod.$main
  13601. '']));
  13602. end;
  13603. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  13604. begin
  13605. StartProgram(false);
  13606. Add([
  13607. '{$modeswitch AdvancedRecords}',
  13608. 'type',
  13609. ' IUnknown = interface end;',
  13610. ' TPoint = record',
  13611. ' type IBird = interface end;',
  13612. ' end;',
  13613. 'begin',
  13614. '']);
  13615. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  13616. nNotYetImplemented);
  13617. ParseProgram;
  13618. end;
  13619. procedure TTestModule.TestAdvRecord_Constructor;
  13620. begin
  13621. StartProgram(false);
  13622. Add([
  13623. '{$modeswitch AdvancedRecords}',
  13624. 'type',
  13625. ' TPoint = record',
  13626. ' x,y: longint;',
  13627. ' class procedure Run(w: longint = 13); static;',
  13628. ' constructor Create(ax: longint; ay: longint = -1);',
  13629. ' end;',
  13630. 'class procedure tpoint.run(w: longint);',
  13631. 'begin',
  13632. ' run;',
  13633. ' run();',
  13634. 'end;',
  13635. 'constructor tpoint.create(ax,ay: longint);',
  13636. 'begin',
  13637. ' x:=ax;',
  13638. ' self.y:=ay;',
  13639. ' run;',
  13640. ' run(ax);',
  13641. 'end;',
  13642. 'var r: TPoint;',
  13643. 'begin',
  13644. ' r:=TPoint.Create(1,2);',
  13645. ' with TPoint do r:=Create(1,2);',
  13646. ' r.Create(3);',
  13647. ' r:=r.Create(4);',
  13648. '']);
  13649. ConvertProgram;
  13650. CheckSource('TestAdvRecord_Constructor',
  13651. LinesToStr([ // statements
  13652. 'rtl.recNewT(this, "TPoint", function () {',
  13653. ' this.x = 0;',
  13654. ' this.y = 0;',
  13655. ' this.$eq = function (b) {',
  13656. ' return (this.x === b.x) && (this.y === b.y);',
  13657. ' };',
  13658. ' this.$assign = function (s) {',
  13659. ' this.x = s.x;',
  13660. ' this.y = s.y;',
  13661. ' return this;',
  13662. ' };',
  13663. ' this.Run = function (w) {',
  13664. ' $mod.TPoint.Run(13);',
  13665. ' $mod.TPoint.Run(13);',
  13666. ' };',
  13667. ' this.Create = function (ax, ay) {',
  13668. ' this.x = ax;',
  13669. ' this.y = ay;',
  13670. ' this.Run(13);',
  13671. ' this.Run(ax);',
  13672. ' return this;',
  13673. ' };',
  13674. '});',
  13675. 'this.r = this.TPoint.$new();',
  13676. '']),
  13677. LinesToStr([ // $mod.$main
  13678. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  13679. 'var $with = $mod.TPoint;',
  13680. '$mod.r.$assign($with.$new().Create(1, 2));',
  13681. '$mod.r.Create(3, -1);',
  13682. '$mod.r.$assign($mod.r.Create(4, -1));',
  13683. '']));
  13684. end;
  13685. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  13686. begin
  13687. StartProgram(false);
  13688. Add([
  13689. '{$modeswitch AdvancedRecords}',
  13690. 'type',
  13691. ' TPoint = record',
  13692. ' class var x: longint;',
  13693. ' class procedure Fly; static;',
  13694. ' class constructor Init;',
  13695. ' end;',
  13696. 'var count: word;',
  13697. 'class procedure Tpoint.Fly;',
  13698. 'begin',
  13699. 'end;',
  13700. 'class constructor tpoint.init;',
  13701. 'begin',
  13702. ' count:=count+1;',
  13703. ' x:=x+3;',
  13704. ' tpoint.x:=tpoint.x+4;',
  13705. ' fly;',
  13706. ' tpoint.fly;',
  13707. 'end;',
  13708. 'var r: TPoint;',
  13709. 'begin',
  13710. ' r.x:=r.x+10;',
  13711. ' r.Fly;',
  13712. ' r.Fly();',
  13713. '']);
  13714. ConvertProgram;
  13715. CheckSource('TestAdvRecord_ClassConstructor_Program',
  13716. LinesToStr([ // statements
  13717. 'rtl.recNewT(this, "TPoint", function () {',
  13718. ' this.x = 0;',
  13719. ' this.$eq = function (b) {',
  13720. ' return true;',
  13721. ' };',
  13722. ' this.$assign = function (s) {',
  13723. ' return this;',
  13724. ' };',
  13725. ' this.Fly = function () {',
  13726. ' };',
  13727. '}, true);',
  13728. 'this.count = 0;',
  13729. 'this.r = this.TPoint.$new();',
  13730. '']),
  13731. LinesToStr([ // $mod.$main
  13732. '(function () {',
  13733. ' $mod.count = $mod.count + 1;',
  13734. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  13735. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  13736. ' $mod.TPoint.Fly();',
  13737. ' $mod.TPoint.Fly();',
  13738. '})();',
  13739. '$mod.TPoint.x = $mod.r.x + 10;',
  13740. '$mod.TPoint.Fly();',
  13741. '$mod.TPoint.Fly();',
  13742. '']));
  13743. end;
  13744. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  13745. begin
  13746. StartUnit(false);
  13747. Add([
  13748. 'interface',
  13749. '{$modeswitch AdvancedRecords}',
  13750. 'type',
  13751. ' TPoint = record',
  13752. ' class var x: longint;',
  13753. ' class procedure Fly; static;',
  13754. ' class constructor Init;',
  13755. ' end;',
  13756. 'implementation',
  13757. 'var count: word;',
  13758. 'class procedure Tpoint.Fly;',
  13759. 'begin',
  13760. 'end;',
  13761. 'class constructor tpoint.init;',
  13762. 'begin',
  13763. ' count:=count+1;',
  13764. ' x:=3;',
  13765. ' tpoint.x:=4;',
  13766. ' fly;',
  13767. ' tpoint.fly;',
  13768. 'end;',
  13769. '']);
  13770. ConvertUnit;
  13771. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  13772. LinesToStr([ // statements
  13773. 'var $impl = $mod.$impl;',
  13774. 'rtl.recNewT(this, "TPoint", function () {',
  13775. ' this.x = 0;',
  13776. ' this.$eq = function (b) {',
  13777. ' return true;',
  13778. ' };',
  13779. ' this.$assign = function (s) {',
  13780. ' return this;',
  13781. ' };',
  13782. ' this.Fly = function () {',
  13783. ' };',
  13784. '}, true);',
  13785. '']),
  13786. LinesToStr([ // $mod.$init
  13787. '(function () {',
  13788. ' $impl.count = $impl.count + 1;',
  13789. ' $mod.TPoint.x = 3;',
  13790. ' $mod.TPoint.x = 4;',
  13791. ' $mod.TPoint.Fly();',
  13792. ' $mod.TPoint.Fly();',
  13793. '})();',
  13794. '']),
  13795. LinesToStr([ // $mod.$main
  13796. '$impl.count = 0;',
  13797. '']));
  13798. end;
  13799. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  13800. begin
  13801. StartProgram(false);
  13802. Add(['type',
  13803. ' TObject = class',
  13804. ' public',
  13805. ' constructor Create;',
  13806. ' destructor Destroy;',
  13807. ' end;',
  13808. ' TBird = TObject;',
  13809. 'constructor tobject.create;',
  13810. 'begin end;',
  13811. 'destructor tobject.destroy;',
  13812. 'begin end;',
  13813. 'var Obj: tobject;',
  13814. 'begin',
  13815. ' obj:=tobject.create;',
  13816. ' obj:=tobject.create();',
  13817. ' obj:=tbird.create;',
  13818. ' obj:=tbird.create();',
  13819. ' obj:=obj.create();',
  13820. ' obj.destroy;',
  13821. '']);
  13822. ConvertProgram;
  13823. CheckSource('TestClass_TObjectDefaultConstructor',
  13824. LinesToStr([ // statements
  13825. 'rtl.createClass(this,"TObject",null,function(){',
  13826. ' this.$init = function () {',
  13827. ' };',
  13828. ' this.$final = function () {',
  13829. ' };',
  13830. ' this.Create = function(){',
  13831. ' return this;',
  13832. ' };',
  13833. ' this.Destroy = function(){',
  13834. ' };',
  13835. '});',
  13836. 'this.Obj = null;'
  13837. ]),
  13838. LinesToStr([ // $mod.$main
  13839. '$mod.Obj = $mod.TObject.$create("Create");',
  13840. '$mod.Obj = $mod.TObject.$create("Create");',
  13841. '$mod.Obj = $mod.TObject.$create("Create");',
  13842. '$mod.Obj = $mod.TObject.$create("Create");',
  13843. '$mod.Obj = $mod.Obj.Create();',
  13844. '$mod.Obj.$destroy("Destroy");',
  13845. '']));
  13846. end;
  13847. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  13848. begin
  13849. StartProgram(false);
  13850. Add('type');
  13851. Add(' TObject = class');
  13852. Add(' public');
  13853. Add(' constructor Create(Par: longint);');
  13854. Add(' end;');
  13855. Add('constructor tobject.create(par: longint);');
  13856. Add('begin end;');
  13857. Add('var Obj: tobject;');
  13858. Add('begin');
  13859. Add(' obj:=tobject.create(3);');
  13860. ConvertProgram;
  13861. CheckSource('TestClass_TObjectConstructorWithParams',
  13862. LinesToStr([ // statements
  13863. 'rtl.createClass(this,"TObject",null,function(){',
  13864. ' this.$init = function () {',
  13865. ' };',
  13866. ' this.$final = function () {',
  13867. ' };',
  13868. ' this.Create = function(Par){',
  13869. ' return this;',
  13870. ' };',
  13871. '});',
  13872. 'this.Obj = null;'
  13873. ]),
  13874. LinesToStr([ // $mod.$main
  13875. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  13876. ]));
  13877. end;
  13878. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  13879. begin
  13880. StartProgram(false);
  13881. Add('type');
  13882. Add(' TObject = class');
  13883. Add(' public');
  13884. Add(' constructor Create;');
  13885. Add(' end;');
  13886. Add(' TTest = class(TObject)');
  13887. Add(' public');
  13888. Add(' constructor Create(const Par: longint = 1);');
  13889. Add(' end;');
  13890. Add('constructor tobject.create;');
  13891. Add('begin end;');
  13892. Add('constructor ttest.create(const par: longint);');
  13893. Add('begin end;');
  13894. Add('var t: ttest;');
  13895. Add('begin');
  13896. Add(' t:=ttest.create;');
  13897. Add(' t:=ttest.create(2);');
  13898. ConvertProgram;
  13899. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  13900. LinesToStr([ // statements
  13901. 'rtl.createClass(this,"TObject",null,function(){',
  13902. ' this.$init = function () {',
  13903. ' };',
  13904. ' this.$final = function () {',
  13905. ' };',
  13906. ' this.Create = function(){',
  13907. ' return this;',
  13908. ' };',
  13909. '});',
  13910. 'rtl.createClass(this, "TTest", this.TObject, function () {',
  13911. ' this.Create$1 = function (Par) {',
  13912. ' return this;',
  13913. ' };',
  13914. '});',
  13915. 'this.t = null;'
  13916. ]),
  13917. LinesToStr([ // $mod.$main
  13918. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  13919. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  13920. ]));
  13921. end;
  13922. procedure TTestModule.TestClass_Var;
  13923. begin
  13924. StartProgram(false);
  13925. Add([
  13926. 'type',
  13927. ' TObject = class',
  13928. ' public',
  13929. ' vI: longint;',
  13930. ' constructor Create(Par: longint);',
  13931. ' end;',
  13932. 'constructor tobject.create(par: longint);',
  13933. 'begin',
  13934. ' vi:=par+3',
  13935. 'end;',
  13936. 'var Obj: tobject;',
  13937. 'begin',
  13938. ' obj:=tobject.create(4);',
  13939. ' obj.vi:=obj.VI+5;']);
  13940. ConvertProgram;
  13941. CheckSource('TestClass_Var',
  13942. LinesToStr([ // statements
  13943. 'rtl.createClass(this,"TObject",null,function(){',
  13944. ' this.$init = function () {',
  13945. ' this.vI = 0;',
  13946. ' };',
  13947. ' this.$final = function () {',
  13948. ' };',
  13949. ' this.Create = function(Par){',
  13950. ' this.vI = Par+3;',
  13951. ' return this;',
  13952. ' };',
  13953. '});',
  13954. 'this.Obj = null;'
  13955. ]),
  13956. LinesToStr([ // $mod.$main
  13957. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  13958. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  13959. ]));
  13960. end;
  13961. procedure TTestModule.TestClass_Method;
  13962. begin
  13963. StartProgram(false);
  13964. Add('type');
  13965. Add(' TObject = class');
  13966. Add(' public');
  13967. Add(' vI: longint;');
  13968. Add(' Sub: TObject;');
  13969. Add(' constructor Create;');
  13970. Add(' function GetIt(Par: longint): tobject;');
  13971. Add(' end;');
  13972. Add('constructor tobject.create; begin end;');
  13973. Add('function tobject.getit(par: longint): tobject;');
  13974. Add('begin');
  13975. Add(' Self.vi:=par+3;');
  13976. Add(' Result:=self.sub;');
  13977. Add('end;');
  13978. Add('var Obj: tobject;');
  13979. Add('begin');
  13980. Add(' obj:=tobject.create;');
  13981. Add(' obj.getit(4);');
  13982. Add(' obj.sub.sub:=nil;');
  13983. Add(' obj.sub.getit(5);');
  13984. Add(' obj.sub.getit(6).SUB:=nil;');
  13985. Add(' obj.sub.getit(7).GETIT(8);');
  13986. Add(' obj.sub.getit(9).SuB.getit(10);');
  13987. ConvertProgram;
  13988. CheckSource('TestClass_Method',
  13989. LinesToStr([ // statements
  13990. 'rtl.createClass(this,"TObject",null,function(){',
  13991. ' this.$init = function () {',
  13992. ' this.vI = 0;',
  13993. ' this.Sub = null;',
  13994. ' };',
  13995. ' this.$final = function () {',
  13996. ' this.Sub = undefined;',
  13997. ' };',
  13998. ' this.Create = function(){',
  13999. ' return this;',
  14000. ' };',
  14001. ' this.GetIt = function(Par){',
  14002. ' var Result = null;',
  14003. ' this.vI = Par + 3;',
  14004. ' Result = this.Sub;',
  14005. ' return Result;',
  14006. ' };',
  14007. '});',
  14008. 'this.Obj = null;'
  14009. ]),
  14010. LinesToStr([ // $mod.$main
  14011. '$mod.Obj = $mod.TObject.$create("Create");',
  14012. '$mod.Obj.GetIt(4);',
  14013. '$mod.Obj.Sub.Sub=null;',
  14014. '$mod.Obj.Sub.GetIt(5);',
  14015. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  14016. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  14017. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  14018. ]));
  14019. end;
  14020. procedure TTestModule.TestClass_Implementation;
  14021. begin
  14022. StartUnit(false);
  14023. Add([
  14024. 'interface',
  14025. 'type',
  14026. ' TObject = class',
  14027. ' constructor Create;',
  14028. ' end;',
  14029. 'implementation',
  14030. 'type',
  14031. ' TIntClass = class',
  14032. ' constructor Create; reintroduce;',
  14033. ' class procedure DoGlob;',
  14034. ' end;',
  14035. 'constructor tintclass.create;',
  14036. 'begin',
  14037. ' inherited;',
  14038. ' inherited create;',
  14039. ' doglob;',
  14040. 'end;',
  14041. 'class procedure tintclass.doglob;',
  14042. 'begin',
  14043. 'end;',
  14044. 'constructor tobject.create;',
  14045. 'var',
  14046. ' iC: tintclass;',
  14047. 'begin',
  14048. ' ic:=tintclass.create;',
  14049. ' tintclass.doglob;',
  14050. ' ic.doglob;',
  14051. 'end;',
  14052. 'initialization',
  14053. ' tintclass.doglob;',
  14054. '']);
  14055. ConvertUnit;
  14056. CheckSource('TestClass_Implementation',
  14057. LinesToStr([ // statements
  14058. 'var $impl = $mod.$impl;',
  14059. 'rtl.createClass(this, "TObject", null, function () {',
  14060. ' this.$init = function () {',
  14061. ' };',
  14062. ' this.$final = function () {',
  14063. ' };',
  14064. ' this.Create = function () {',
  14065. ' var iC = null;',
  14066. ' iC = $impl.TIntClass.$create("Create$1");',
  14067. ' $impl.TIntClass.DoGlob();',
  14068. ' iC.$class.DoGlob();',
  14069. ' return this;',
  14070. ' };',
  14071. '});',
  14072. '']),
  14073. LinesToStr([ // $mod.$main
  14074. '$impl.TIntClass.DoGlob();',
  14075. '']),
  14076. LinesToStr([
  14077. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  14078. ' this.Create$1 = function () {',
  14079. ' $mod.TObject.Create.call(this);',
  14080. ' $mod.TObject.Create.call(this);',
  14081. ' this.$class.DoGlob();',
  14082. ' return this;',
  14083. ' };',
  14084. ' this.DoGlob = function () {',
  14085. ' };',
  14086. '});',
  14087. '']));
  14088. end;
  14089. procedure TTestModule.TestClass_Inheritance;
  14090. begin
  14091. StartProgram(false);
  14092. Add('type');
  14093. Add(' TObject = class');
  14094. Add(' public');
  14095. Add(' constructor Create;');
  14096. Add(' end;');
  14097. Add(' TClassA = class');
  14098. Add(' end;');
  14099. Add(' TClassB = class(TObject)');
  14100. Add(' procedure ProcB;');
  14101. Add(' end;');
  14102. Add('constructor tobject.create; begin end;');
  14103. Add('procedure tclassb.procb; begin end;');
  14104. Add('var');
  14105. Add(' oO: TObject;');
  14106. Add(' oA: TClassA;');
  14107. Add(' oB: TClassB;');
  14108. Add('begin');
  14109. Add(' oO:=tobject.Create;');
  14110. Add(' oA:=tclassa.Create;');
  14111. Add(' ob:=tclassb.Create;');
  14112. Add(' if oo is tclassa then ;');
  14113. Add(' ob:=oo as tclassb;');
  14114. Add(' (oo as tclassb).procb;');
  14115. ConvertProgram;
  14116. CheckSource('TestClass_Inheritance',
  14117. LinesToStr([ // statements
  14118. 'rtl.createClass(this,"TObject",null,function(){',
  14119. ' this.$init = function () {',
  14120. ' };',
  14121. ' this.$final = function () {',
  14122. ' };',
  14123. ' this.Create = function () {',
  14124. ' return this;',
  14125. ' };',
  14126. '});',
  14127. 'rtl.createClass(this,"TClassA",this.TObject,function(){',
  14128. '});',
  14129. 'rtl.createClass(this,"TClassB",this.TObject,function(){',
  14130. ' this.ProcB = function () {',
  14131. ' };',
  14132. '});',
  14133. 'this.oO = null;',
  14134. 'this.oA = null;',
  14135. 'this.oB = null;'
  14136. ]),
  14137. LinesToStr([ // $mod.$main
  14138. '$mod.oO = $mod.TObject.$create("Create");',
  14139. '$mod.oA = $mod.TClassA.$create("Create");',
  14140. '$mod.oB = $mod.TClassB.$create("Create");',
  14141. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  14142. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  14143. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  14144. ]));
  14145. end;
  14146. procedure TTestModule.TestClass_TypeAlias;
  14147. begin
  14148. StartProgram(false);
  14149. Add([
  14150. '{$interfaces corba}',
  14151. 'type',
  14152. ' IObject = interface',
  14153. ' end;',
  14154. ' IBird = type IObject;',
  14155. ' TObject = class',
  14156. ' end;',
  14157. ' TBird = type TObject;',
  14158. 'var',
  14159. ' oObj: TObject;',
  14160. ' oBird: TBird;',
  14161. ' IntfObj: IObject;',
  14162. ' IntfBird: IBird;',
  14163. 'begin',
  14164. ' oObj:=oBird;',
  14165. '']);
  14166. ConvertProgram;
  14167. CheckSource('TestClass_TypeAlias',
  14168. LinesToStr([ // statements
  14169. 'rtl.createInterface(this, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  14170. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], this.IObject);',
  14171. 'rtl.createClass(this, "TObject", null, function () {',
  14172. ' this.$init = function () {',
  14173. ' };',
  14174. ' this.$final = function () {',
  14175. ' };',
  14176. '});',
  14177. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14178. '});',
  14179. 'this.oObj = null;',
  14180. 'this.oBird = null;',
  14181. 'this.IntfObj = null;',
  14182. 'this.IntfBird = null;',
  14183. '']),
  14184. LinesToStr([ // $mod.$main
  14185. '$mod.oObj = $mod.oBird;',
  14186. '']));
  14187. end;
  14188. procedure TTestModule.TestClass_AbstractMethod;
  14189. begin
  14190. StartProgram(false);
  14191. Add('type');
  14192. Add(' TObject = class');
  14193. Add(' public');
  14194. Add(' procedure DoIt; virtual; abstract;');
  14195. Add(' end;');
  14196. Add('begin');
  14197. ConvertProgram;
  14198. CheckSource('TestClass_AbstractMethod',
  14199. LinesToStr([ // statements
  14200. 'rtl.createClass(this,"TObject",null,function(){',
  14201. ' this.$init = function () {',
  14202. ' };',
  14203. ' this.$final = function () {',
  14204. ' };',
  14205. '});'
  14206. ]),
  14207. LinesToStr([ // this.$main
  14208. ''
  14209. ]));
  14210. end;
  14211. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  14212. begin
  14213. StartProgram(false);
  14214. Add([
  14215. 'type',
  14216. ' TObject = class',
  14217. ' procedure DoAbstract; virtual; abstract;',
  14218. ' procedure DoVirtual; virtual;',
  14219. ' procedure DoIt;',
  14220. ' end;',
  14221. ' TA = class',
  14222. ' procedure doabstract; override;',
  14223. ' procedure dovirtual; override;',
  14224. ' procedure DoSome;',
  14225. ' end;',
  14226. 'procedure tobject.dovirtual;',
  14227. 'begin',
  14228. ' inherited; // call non existing ancestor -> ignore silently',
  14229. 'end;',
  14230. 'procedure tobject.doit;',
  14231. 'begin',
  14232. 'end;',
  14233. 'procedure ta.doabstract;',
  14234. 'begin',
  14235. ' inherited dovirtual; // call TObject.DoVirtual',
  14236. 'end;',
  14237. 'procedure ta.dovirtual;',
  14238. 'begin',
  14239. ' inherited; // call TObject.DoVirtual',
  14240. ' inherited dovirtual; // call TObject.DoVirtual',
  14241. ' inherited dovirtual(); // call TObject.DoVirtual',
  14242. ' doit;',
  14243. ' doit();',
  14244. 'end;',
  14245. 'procedure ta.dosome;',
  14246. 'begin',
  14247. ' inherited; // call non existing ancestor method -> silently ignore',
  14248. 'end;',
  14249. 'begin']);
  14250. ConvertProgram;
  14251. CheckSource('TestClass_CallInherited_ProcNoParams',
  14252. LinesToStr([ // statements
  14253. 'rtl.createClass(this,"TObject",null,function(){',
  14254. ' this.$init = function () {',
  14255. ' };',
  14256. ' this.$final = function () {',
  14257. ' };',
  14258. ' this.DoVirtual = function () {',
  14259. ' };',
  14260. ' this.DoIt = function () {',
  14261. ' };',
  14262. '});',
  14263. 'rtl.createClass(this, "TA", this.TObject, function () {',
  14264. ' this.DoAbstract = function () {',
  14265. ' $mod.TObject.DoVirtual.call(this);',
  14266. ' };',
  14267. ' this.DoVirtual = function () {',
  14268. ' $mod.TObject.DoVirtual.call(this);',
  14269. ' $mod.TObject.DoVirtual.call(this);',
  14270. ' $mod.TObject.DoVirtual.call(this);',
  14271. ' this.DoIt();',
  14272. ' this.DoIt();',
  14273. ' };',
  14274. ' this.DoSome = function () {',
  14275. ' };',
  14276. '});'
  14277. ]),
  14278. LinesToStr([ // this.$main
  14279. ''
  14280. ]));
  14281. end;
  14282. procedure TTestModule.TestClass_CallInherited_WithParams;
  14283. begin
  14284. StartProgram(false);
  14285. Add([
  14286. 'type',
  14287. ' TObject = class',
  14288. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  14289. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  14290. ' procedure DoIt(pA: longint; pB: longint = 0);',
  14291. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  14292. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  14293. ' end;',
  14294. ' TClassA = class',
  14295. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  14296. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  14297. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  14298. ' end;',
  14299. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  14300. 'begin',
  14301. 'end;',
  14302. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  14303. 'begin',
  14304. 'end;',
  14305. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  14306. 'begin',
  14307. 'end;',
  14308. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  14309. 'begin',
  14310. 'end;',
  14311. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  14312. 'begin',
  14313. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  14314. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  14315. 'end;',
  14316. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  14317. 'begin',
  14318. ' inherited; // call TObject.DoVirtual(pA,pB)',
  14319. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  14320. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  14321. ' doit(pa,pb);',
  14322. ' doit(pa);',
  14323. ' doit2(pa);',
  14324. ' doit2;',
  14325. 'end;',
  14326. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  14327. 'begin',
  14328. ' pa:=inherited;',
  14329. 'end;',
  14330. 'begin']);
  14331. ConvertProgram;
  14332. CheckSource('TestClass_CallInherited_WithParams',
  14333. LinesToStr([ // statements
  14334. 'rtl.createClass(this,"TObject",null,function(){',
  14335. ' this.$init = function () {',
  14336. ' };',
  14337. ' this.$final = function () {',
  14338. ' };',
  14339. ' this.DoVirtual = function (pA,pB) {',
  14340. ' };',
  14341. ' this.DoIt = function (pA,pB) {',
  14342. ' };',
  14343. ' this.DoIt2 = function (pA,pB) {',
  14344. ' };',
  14345. ' this.GetIt = function (pA, pB) {',
  14346. ' var Result = 0;',
  14347. ' return Result;',
  14348. ' };',
  14349. '});',
  14350. 'rtl.createClass(this, "TClassA", this.TObject, function () {',
  14351. ' this.DoAbstract = function (pA,pB) {',
  14352. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  14353. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  14354. ' };',
  14355. ' this.DoVirtual = function (pA,pB) {',
  14356. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  14357. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  14358. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  14359. ' this.DoIt(pA,pB);',
  14360. ' this.DoIt(pA,0);',
  14361. ' this.DoIt2(pA,2);',
  14362. ' this.DoIt2(1,2);',
  14363. ' };',
  14364. ' this.GetIt$1 = function (pA, pB) {',
  14365. ' var Result = 0;',
  14366. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  14367. ' return Result;',
  14368. ' };',
  14369. '});'
  14370. ]),
  14371. LinesToStr([ // this.$main
  14372. ''
  14373. ]));
  14374. end;
  14375. procedure TTestModule.TestClasS_CallInheritedConstructor;
  14376. begin
  14377. StartProgram(false);
  14378. Add('type');
  14379. Add(' TObject = class');
  14380. Add(' constructor Create; virtual;');
  14381. Add(' constructor CreateWithB(b: boolean);');
  14382. Add(' end;');
  14383. Add(' TA = class');
  14384. Add(' constructor Create; override;');
  14385. Add(' constructor CreateWithC(c: char);');
  14386. Add(' procedure DoIt;');
  14387. Add(' class function DoSome: TObject;');
  14388. Add(' end;');
  14389. Add('constructor tobject.create;');
  14390. Add('begin');
  14391. Add(' inherited; // call non existing ancestor -> ignore silently');
  14392. Add('end;');
  14393. Add('constructor tobject.createwithb(b: boolean);');
  14394. Add('begin');
  14395. Add(' inherited; // call non existing ancestor -> ignore silently');
  14396. Add(' create; // normal call');
  14397. Add('end;');
  14398. Add('constructor ta.create;');
  14399. Add('begin');
  14400. Add(' inherited; // normal call TObject.Create');
  14401. Add(' inherited create; // normal call TObject.Create');
  14402. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  14403. Add('end;');
  14404. Add('constructor ta.createwithc(c: char);');
  14405. Add('begin');
  14406. Add(' inherited create; // call TObject.Create');
  14407. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  14408. Add(' doit;');
  14409. Add(' doit();');
  14410. Add(' dosome;');
  14411. Add('end;');
  14412. Add('procedure ta.doit;');
  14413. Add('begin');
  14414. Add(' create; // normal call');
  14415. Add(' createwithb(false); // normal call');
  14416. Add(' createwithc(''c''); // normal call');
  14417. Add('end;');
  14418. Add('class function ta.dosome: TObject;');
  14419. Add('begin');
  14420. Add(' Result:=create; // constructor');
  14421. Add(' Result:=createwithb(true); // constructor');
  14422. Add(' Result:=createwithc(''c''); // constructor');
  14423. Add('end;');
  14424. Add('begin');
  14425. ConvertProgram;
  14426. CheckSource('TestClass_CallInheritedConstructor',
  14427. LinesToStr([ // statements
  14428. 'rtl.createClass(this,"TObject",null,function(){',
  14429. ' this.$init = function () {',
  14430. ' };',
  14431. ' this.$final = function () {',
  14432. ' };',
  14433. ' this.Create = function () {',
  14434. ' return this;',
  14435. ' };',
  14436. ' this.CreateWithB = function (b) {',
  14437. ' this.Create();',
  14438. ' return this;',
  14439. ' };',
  14440. '});',
  14441. 'rtl.createClass(this, "TA", this.TObject, function () {',
  14442. ' this.Create = function () {',
  14443. ' $mod.TObject.Create.call(this);',
  14444. ' $mod.TObject.Create.call(this);',
  14445. ' $mod.TObject.CreateWithB.call(this, false);',
  14446. ' return this;',
  14447. ' };',
  14448. ' this.CreateWithC = function (c) {',
  14449. ' $mod.TObject.Create.call(this);',
  14450. ' $mod.TObject.CreateWithB.call(this, true);',
  14451. ' this.DoIt();',
  14452. ' this.DoIt();',
  14453. ' this.$class.DoSome();',
  14454. ' return this;',
  14455. ' };',
  14456. ' this.DoIt = function () {',
  14457. ' this.Create();',
  14458. ' this.CreateWithB(false);',
  14459. ' this.CreateWithC("c");',
  14460. ' };',
  14461. ' this.DoSome = function () {',
  14462. ' var Result = null;',
  14463. ' Result = this.$create("Create");',
  14464. ' Result = this.$create("CreateWithB", [true]);',
  14465. ' Result = this.$create("CreateWithC", ["c"]);',
  14466. ' return Result;',
  14467. ' };',
  14468. '});'
  14469. ]),
  14470. LinesToStr([ // this.$main
  14471. ''
  14472. ]));
  14473. end;
  14474. procedure TTestModule.TestClass_ClassVar_Assign;
  14475. begin
  14476. StartProgram(false);
  14477. Add([
  14478. 'type',
  14479. ' TObject = class',
  14480. ' public',
  14481. ' class var vI: longint;',
  14482. ' class var Sub: TObject;',
  14483. ' constructor Create;',
  14484. ' class function GetIt(var Par: longint): tobject;',
  14485. ' end;',
  14486. 'constructor tobject.create;',
  14487. 'begin',
  14488. ' vi:=vi+1;',
  14489. ' Self.vi:=Self.vi+1;',
  14490. ' inc(vi);',
  14491. 'end;',
  14492. 'class function tobject.getit(var par: longint): tobject;',
  14493. 'begin',
  14494. ' vi:=vi+3;',
  14495. ' Self.vi:=Self.vi+4;',
  14496. ' inc(vi);',
  14497. ' Result:=self.sub;',
  14498. ' GetIt(vi);',
  14499. 'end;',
  14500. 'var Obj: tobject;',
  14501. 'begin',
  14502. ' obj:=tobject.create;',
  14503. ' tobject.vi:=3;',
  14504. ' if tobject.vi=4 then ;',
  14505. ' tobject.sub:=nil;',
  14506. ' obj.sub:=nil;',
  14507. ' obj.sub.sub:=nil;']);
  14508. ConvertProgram;
  14509. CheckSource('TestClass_ClassVar_Assign',
  14510. LinesToStr([ // statements
  14511. 'rtl.createClass(this,"TObject",null,function(){',
  14512. ' this.vI = 0;',
  14513. ' this.Sub = null;',
  14514. ' this.$init = function () {',
  14515. ' };',
  14516. ' this.$final = function () {',
  14517. ' };',
  14518. ' this.Create = function(){',
  14519. ' $mod.TObject.vI = this.vI+1;',
  14520. ' $mod.TObject.vI = this.vI+1;',
  14521. ' $mod.TObject.vI += 1;',
  14522. ' return this;',
  14523. ' };',
  14524. ' this.GetIt = function(Par){',
  14525. ' var Result = null;',
  14526. ' $mod.TObject.vI = this.vI + 3;',
  14527. ' $mod.TObject.vI = this.vI + 4;',
  14528. ' $mod.TObject.vI += 1;',
  14529. ' Result = this.Sub;',
  14530. ' this.GetIt({',
  14531. ' p: $mod.TObject,',
  14532. ' get: function () {',
  14533. ' return this.p.vI;',
  14534. ' },',
  14535. ' set: function (v) {',
  14536. ' this.p.vI = v;',
  14537. ' }',
  14538. ' });',
  14539. ' return Result;',
  14540. ' };',
  14541. '});',
  14542. 'this.Obj = null;'
  14543. ]),
  14544. LinesToStr([ // $mod.$main
  14545. '$mod.Obj = $mod.TObject.$create("Create");',
  14546. '$mod.TObject.vI = 3;',
  14547. 'if ($mod.TObject.vI === 4);',
  14548. '$mod.TObject.Sub=null;',
  14549. '$mod.TObject.Sub=null;',
  14550. '$mod.TObject.Sub=null;',
  14551. '']));
  14552. end;
  14553. procedure TTestModule.TestClass_CallClassMethod;
  14554. begin
  14555. StartProgram(false);
  14556. Add('type');
  14557. Add(' TObject = class');
  14558. Add(' public');
  14559. Add(' class var vI: longint;');
  14560. Add(' class var Sub: TObject;');
  14561. Add(' constructor Create;');
  14562. Add(' function GetMore(Par: longint): longint;');
  14563. Add(' class function GetIt(Par: longint): tobject;');
  14564. Add(' end;');
  14565. Add('constructor tobject.create;');
  14566. Add('begin');
  14567. Add(' sub:=getit(3);');
  14568. Add(' vi:=getmore(4);');
  14569. Add(' sub:=Self.getit(5);');
  14570. Add(' vi:=Self.getmore(6);');
  14571. Add('end;');
  14572. Add('function tobject.getmore(par: longint): longint;');
  14573. Add('begin');
  14574. Add(' sub:=getit(11);');
  14575. Add(' vi:=getmore(12);');
  14576. Add(' sub:=self.getit(13);');
  14577. Add(' vi:=self.getmore(14);');
  14578. Add('end;');
  14579. Add('class function tobject.getit(par: longint): tobject;');
  14580. Add('begin');
  14581. Add(' sub:=getit(21);');
  14582. Add(' vi:=sub.getmore(22);');
  14583. Add(' sub:=self.getit(23);');
  14584. Add(' vi:=self.sub.getmore(24);');
  14585. Add('end;');
  14586. Add('var Obj: tobject;');
  14587. Add('begin');
  14588. Add(' obj:=tobject.create;');
  14589. Add(' tobject.getit(5);');
  14590. Add(' obj.getit(6);');
  14591. Add(' obj.sub.getit(7);');
  14592. Add(' obj.sub.getit(8).SUB:=nil;');
  14593. Add(' obj.sub.getit(9).GETIT(10);');
  14594. Add(' obj.sub.getit(11).SuB.getit(12);');
  14595. ConvertProgram;
  14596. CheckSource('TestClass_CallClassMethod',
  14597. LinesToStr([ // statements
  14598. 'rtl.createClass(this,"TObject",null,function(){',
  14599. ' this.vI = 0;',
  14600. ' this.Sub = null;',
  14601. ' this.$init = function () {',
  14602. ' };',
  14603. ' this.$final = function () {',
  14604. ' };',
  14605. ' this.Create = function(){',
  14606. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  14607. ' $mod.TObject.vI = this.GetMore(4);',
  14608. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  14609. ' $mod.TObject.vI = this.GetMore(6);',
  14610. ' return this;',
  14611. ' };',
  14612. ' this.GetMore = function(Par){',
  14613. ' var Result = 0;',
  14614. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  14615. ' $mod.TObject.vI = this.GetMore(12);',
  14616. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  14617. ' $mod.TObject.vI = this.GetMore(14);',
  14618. ' return Result;',
  14619. ' };',
  14620. ' this.GetIt = function(Par){',
  14621. ' var Result = null;',
  14622. ' $mod.TObject.Sub = this.GetIt(21);',
  14623. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  14624. ' $mod.TObject.Sub = this.GetIt(23);',
  14625. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  14626. ' return Result;',
  14627. ' };',
  14628. '});',
  14629. 'this.Obj = null;'
  14630. ]),
  14631. LinesToStr([ // $mod.$main
  14632. '$mod.Obj = $mod.TObject.$create("Create");',
  14633. '$mod.TObject.GetIt(5);',
  14634. '$mod.Obj.$class.GetIt(6);',
  14635. '$mod.Obj.Sub.$class.GetIt(7);',
  14636. '$mod.TObject.Sub=null;',
  14637. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  14638. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  14639. '']));
  14640. end;
  14641. procedure TTestModule.TestClass_CallClassMethodStatic;
  14642. begin
  14643. StartProgram(false);
  14644. Add([
  14645. 'type',
  14646. ' TObject = class',
  14647. ' public',
  14648. ' class function Fly: tobject; static;',
  14649. ' end;',
  14650. 'class function tobject.Fly: tobject;',
  14651. 'begin',
  14652. ' Result.Fly;',
  14653. ' Result.Fly();',
  14654. ' Fly;',
  14655. ' Fly();',
  14656. ' Fly.Fly;',
  14657. ' Fly.Fly();',
  14658. 'end;',
  14659. 'var Obj: tobject;',
  14660. 'begin',
  14661. ' obj.Fly;',
  14662. ' obj.Fly();',
  14663. ' with obj do begin',
  14664. ' Fly;',
  14665. ' Fly();',
  14666. ' end;',
  14667. '']);
  14668. ConvertProgram;
  14669. CheckSource('TestClass_CallClassMethodStatic',
  14670. LinesToStr([ // statements
  14671. 'rtl.createClass(this, "TObject", null, function () {',
  14672. ' this.$init = function () {',
  14673. ' };',
  14674. ' this.$final = function () {',
  14675. ' };',
  14676. ' this.Fly = function () {',
  14677. ' var Result = null;',
  14678. ' $mod.TObject.Fly();',
  14679. ' $mod.TObject.Fly();',
  14680. ' $mod.TObject.Fly();',
  14681. ' $mod.TObject.Fly();',
  14682. ' $mod.TObject.Fly();',
  14683. ' $mod.TObject.Fly();',
  14684. ' return Result;',
  14685. ' };',
  14686. '});',
  14687. 'this.Obj = null;'
  14688. ]),
  14689. LinesToStr([ // $mod.$main
  14690. '$mod.TObject.Fly();',
  14691. '$mod.TObject.Fly();',
  14692. 'var $with = $mod.Obj;',
  14693. '$with.Fly();',
  14694. '$with.Fly();',
  14695. '']));
  14696. end;
  14697. procedure TTestModule.TestClass_Property;
  14698. begin
  14699. StartProgram(false);
  14700. Add('type');
  14701. Add(' TObject = class');
  14702. Add(' Fx: longint;');
  14703. Add(' Fy: longint;');
  14704. Add(' function GetInt: longint;');
  14705. Add(' procedure SetInt(Value: longint);');
  14706. Add(' procedure DoIt;');
  14707. Add(' property IntA: longint read Fx write Fy;');
  14708. Add(' property IntB: longint read GetInt write SetInt;');
  14709. Add(' end;');
  14710. Add('function tobject.getint: longint;');
  14711. Add('begin');
  14712. Add(' result:=fx;');
  14713. Add('end;');
  14714. Add('procedure tobject.setint(value: longint);');
  14715. Add('begin');
  14716. Add(' if value=fy then exit;');
  14717. Add(' fy:=value;');
  14718. Add('end;');
  14719. Add('procedure tobject.doit;');
  14720. Add('begin');
  14721. Add(' IntA:=IntA+1;');
  14722. Add(' Self.IntA:=Self.IntA+1;');
  14723. Add(' IntB:=IntB+1;');
  14724. Add(' Self.IntB:=Self.IntB+1;');
  14725. Add('end;');
  14726. Add('var Obj: tobject;');
  14727. Add('begin');
  14728. Add(' obj.inta:=obj.inta+1;');
  14729. Add(' if obj.intb=2 then;');
  14730. Add(' obj.intb:=obj.intb+2;');
  14731. Add(' obj.setint(obj.inta);');
  14732. ConvertProgram;
  14733. CheckSource('TestClass_Property',
  14734. LinesToStr([ // statements
  14735. 'rtl.createClass(this, "TObject", null, function () {',
  14736. ' this.$init = function () {',
  14737. ' this.Fx = 0;',
  14738. ' this.Fy = 0;',
  14739. ' };',
  14740. ' this.$final = function () {',
  14741. ' };',
  14742. ' this.GetInt = function () {',
  14743. ' var Result = 0;',
  14744. ' Result = this.Fx;',
  14745. ' return Result;',
  14746. ' };',
  14747. ' this.SetInt = function (Value) {',
  14748. ' if (Value === this.Fy) return;',
  14749. ' this.Fy = Value;',
  14750. ' };',
  14751. ' this.DoIt = function () {',
  14752. ' this.Fy = this.Fx + 1;',
  14753. ' this.Fy = this.Fx + 1;',
  14754. ' this.SetInt(this.GetInt() + 1);',
  14755. ' this.SetInt(this.GetInt() + 1);',
  14756. ' };',
  14757. '});',
  14758. 'this.Obj = null;'
  14759. ]),
  14760. LinesToStr([ // $mod.$main
  14761. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  14762. 'if ($mod.Obj.GetInt() === 2);',
  14763. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  14764. '$mod.Obj.SetInt($mod.Obj.Fx);'
  14765. ]));
  14766. end;
  14767. procedure TTestModule.TestClass_Property_ClassMethod;
  14768. begin
  14769. StartProgram(false);
  14770. Add([
  14771. 'type',
  14772. ' TObject = class',
  14773. ' class var Fx: longint;',
  14774. ' class var Fy: longint;',
  14775. ' class function GetInt: longint;',
  14776. ' class procedure SetInt(Value: longint);',
  14777. ' end;',
  14778. ' TBird = class',
  14779. ' class procedure DoIt;',
  14780. ' class property IntA: longint read Fx write Fy;',
  14781. ' class property IntB: longint read GetInt write SetInt;',
  14782. ' end;',
  14783. 'class function tobject.getint: longint;',
  14784. 'begin',
  14785. ' result:=fx;',
  14786. 'end;',
  14787. 'class procedure tobject.setint(value: longint);',
  14788. 'begin',
  14789. 'end;',
  14790. 'class procedure tbird.doit;',
  14791. 'begin',
  14792. ' FX:=3;',
  14793. ' IntA:=IntA+1;',
  14794. ' Self.IntA:=Self.IntA+1;',
  14795. ' IntB:=IntB+1;',
  14796. ' Self.IntB:=Self.IntB+1;',
  14797. ' with Self do begin',
  14798. ' FX:=11;',
  14799. ' IntA:=IntA+12;',
  14800. ' IntB:=IntB+13;',
  14801. ' end;',
  14802. 'end;',
  14803. 'var Obj: tbird;',
  14804. 'begin',
  14805. ' tbird.fx:=tbird.fx+1;',
  14806. ' tbird.inta:=tbird.inta+1;',
  14807. ' if tbird.intb=2 then;',
  14808. ' tbird.intb:=tbird.intb+2;',
  14809. ' tbird.setint(tbird.inta);',
  14810. ' obj.inta:=obj.inta+1;',
  14811. ' if obj.intb=2 then;',
  14812. ' obj.intb:=obj.intb+2;',
  14813. ' obj.setint(obj.inta);',
  14814. ' with Tbird do begin',
  14815. ' FX:=FY+1;',
  14816. ' inta:=inta+2;',
  14817. ' intb:=intb+3;',
  14818. ' end;',
  14819. ' with Obj do begin',
  14820. ' FX:=FY+1;',
  14821. ' inta:=inta+2;',
  14822. ' intb:=intb+3;',
  14823. ' end;',
  14824. '']);
  14825. ConvertProgram;
  14826. CheckSource('TestClass_Property_ClassMethod',
  14827. LinesToStr([ // statements
  14828. 'rtl.createClass(this, "TObject", null, function () {',
  14829. ' this.Fx = 0;',
  14830. ' this.Fy = 0;',
  14831. ' this.$init = function () {',
  14832. ' };',
  14833. ' this.$final = function () {',
  14834. ' };',
  14835. ' this.GetInt = function () {',
  14836. ' var Result = 0;',
  14837. ' Result = this.Fx;',
  14838. ' return Result;',
  14839. ' };',
  14840. ' this.SetInt = function (Value) {',
  14841. ' };',
  14842. '});',
  14843. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14844. ' this.DoIt = function () {',
  14845. ' $mod.TObject.Fx = 3;',
  14846. ' $mod.TObject.Fy = this.Fx + 1;',
  14847. ' $mod.TObject.Fy = this.Fx + 1;',
  14848. ' this.SetInt(this.GetInt() + 1);',
  14849. ' this.SetInt(this.GetInt() + 1);',
  14850. ' $mod.TObject.Fx = 11;',
  14851. ' $mod.TObject.Fy = this.Fx + 12;',
  14852. ' this.SetInt(this.GetInt() + 13);',
  14853. ' };',
  14854. '});',
  14855. 'this.Obj = null;'
  14856. ]),
  14857. LinesToStr([ // $mod.$main
  14858. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  14859. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  14860. 'if ($mod.TBird.GetInt() === 2);',
  14861. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  14862. '$mod.TBird.SetInt($mod.TBird.Fx);',
  14863. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  14864. 'if ($mod.Obj.$class.GetInt() === 2);',
  14865. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  14866. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  14867. 'var $with = $mod.TBird;',
  14868. '$mod.TObject.Fx = $with.Fy + 1;',
  14869. '$mod.TObject.Fy = $with.Fx + 2;',
  14870. '$with.SetInt($with.GetInt() + 3);',
  14871. 'var $with1 = $mod.Obj;',
  14872. '$mod.TObject.Fx = $with1.Fy + 1;',
  14873. '$mod.TObject.Fy = $with1.Fx + 2;',
  14874. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  14875. '']));
  14876. end;
  14877. procedure TTestModule.TestClass_Property_ClassMethodStatic;
  14878. begin
  14879. StartProgram(false);
  14880. Add([
  14881. 'type',
  14882. ' TObject = class',
  14883. ' class function GetInt: longint; static;',
  14884. ' class procedure SetInt(Value: longint); static;',
  14885. ' class function GetItems(Index: word): longint; static;',
  14886. ' class procedure SetItems(Index: word; const Value: longint); static;',
  14887. ' end;',
  14888. ' TBird = class',
  14889. ' class procedure Fly;',
  14890. ' class property IntA: longint read GetInt write SetInt;',
  14891. ' class property Items[Index: word]: longint read GetItems write SetItems;',
  14892. ' end;',
  14893. 'class function tobject.getint: longint;',
  14894. 'begin',
  14895. 'end;',
  14896. 'class procedure tobject.setint(value: longint);',
  14897. 'begin',
  14898. 'end;',
  14899. 'class function tobject.GetItems(Index: word): longint;',
  14900. 'begin',
  14901. 'end;',
  14902. 'class procedure TObject.SetItems(Index: word; const Value: longint);',
  14903. 'begin',
  14904. 'end;',
  14905. 'class procedure tbird.fly;',
  14906. 'var w: longint;',
  14907. 'begin',
  14908. ' inta:=inta+51;',
  14909. ' w:=items[52];',
  14910. ' items[53]:=54;',
  14911. 'end;',
  14912. 'var Obj: tbird;',
  14913. ' i: longint;',
  14914. 'begin',
  14915. ' tbird.inta:=tbird.inta+1;',
  14916. ' i:=tbird.items[2];',
  14917. ' tbird.items[3]:=4;',
  14918. ' obj.inta:=obj.inta+11;',
  14919. ' i:=obj.items[12];',
  14920. ' obj.items[13]:=14;',
  14921. ' with Tbird do begin',
  14922. ' inta:=inta+21;',
  14923. ' i:=items[22];',
  14924. ' items[23]:=24;',
  14925. ' end;',
  14926. ' with Obj do begin',
  14927. ' inta:=inta+31;',
  14928. ' i:=items[32];',
  14929. ' items[33]:=34;',
  14930. ' end;',
  14931. '']);
  14932. ConvertProgram;
  14933. CheckSource('TestClass_Property_ClassMethod',
  14934. LinesToStr([ // statements
  14935. 'rtl.createClass(this, "TObject", null, function () {',
  14936. ' this.$init = function () {',
  14937. ' };',
  14938. ' this.$final = function () {',
  14939. ' };',
  14940. ' this.GetInt = function () {',
  14941. ' var Result = 0;',
  14942. ' return Result;',
  14943. ' };',
  14944. ' this.SetInt = function (Value) {',
  14945. ' };',
  14946. ' this.GetItems = function (Index) {',
  14947. ' var Result = 0;',
  14948. ' return Result;',
  14949. ' };',
  14950. ' this.SetItems = function (Index, Value) {',
  14951. ' };',
  14952. '});',
  14953. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14954. ' this.Fly = function () {',
  14955. ' var w = 0;',
  14956. ' this.SetInt(this.GetInt() + 51);',
  14957. ' w = this.GetItems(52);',
  14958. ' this.SetItems(53, 54);',
  14959. ' };',
  14960. '});',
  14961. 'this.Obj = null;',
  14962. 'this.i = 0;',
  14963. '']),
  14964. LinesToStr([ // $mod.$main
  14965. '$mod.TObject.SetInt($mod.TObject.GetInt() + 1);',
  14966. '$mod.i = $mod.TObject.GetItems(2);',
  14967. '$mod.TObject.SetItems(3, 4);',
  14968. '$mod.TObject.SetInt($mod.TObject.GetInt() + 11);',
  14969. '$mod.i = $mod.TObject.GetItems(12);',
  14970. '$mod.TObject.SetItems(13, 14);',
  14971. 'var $with = $mod.TBird;',
  14972. '$with.SetInt($with.GetInt() + 21);',
  14973. '$mod.i = $with.GetItems(22);',
  14974. '$with.SetItems(23, 24);',
  14975. 'var $with1 = $mod.Obj;',
  14976. '$with1.SetInt($with1.GetInt() + 31);',
  14977. '$mod.i = $with1.GetItems(32);',
  14978. '$with1.SetItems(33, 34);',
  14979. '']));
  14980. end;
  14981. procedure TTestModule.TestClass_Property_Indexed;
  14982. begin
  14983. StartProgram(false);
  14984. Add([
  14985. 'type',
  14986. ' TObject = class',
  14987. ' FItems: array of longint;',
  14988. ' function GetItems(Index: longint): longint;',
  14989. ' procedure SetItems(Index: longint; Value: longint);',
  14990. ' procedure DoIt;',
  14991. ' property Items[Index: longint]: longint read getitems write setitems;',
  14992. ' end;',
  14993. 'function tobject.getitems(index: longint): longint;',
  14994. 'begin',
  14995. ' Result:=fitems[index];',
  14996. 'end;',
  14997. 'procedure tobject.setitems(index: longint; value: longint);',
  14998. 'begin',
  14999. ' fitems[index]:=value;',
  15000. 'end;',
  15001. 'procedure tobject.doit;',
  15002. 'begin',
  15003. ' items[1]:=2;',
  15004. ' items[3]:=items[4];',
  15005. ' self.items[5]:=self.items[6];',
  15006. ' items[items[7]]:=items[items[8]];',
  15007. 'end;',
  15008. 'var Obj: tobject;',
  15009. 'begin',
  15010. ' obj.Items[11]:=obj.Items[12];',
  15011. '']);
  15012. ConvertProgram;
  15013. CheckSource('TestClass_Property_Indexed',
  15014. LinesToStr([ // statements
  15015. 'rtl.createClass(this, "TObject", null, function () {',
  15016. ' this.$init = function () {',
  15017. ' this.FItems = [];',
  15018. ' };',
  15019. ' this.$final = function () {',
  15020. ' this.FItems = undefined;',
  15021. ' };',
  15022. ' this.GetItems = function (Index) {',
  15023. ' var Result = 0;',
  15024. ' Result = this.FItems[Index];',
  15025. ' return Result;',
  15026. ' };',
  15027. ' this.SetItems = function (Index, Value) {',
  15028. ' this.FItems[Index] = Value;',
  15029. ' };',
  15030. ' this.DoIt = function () {',
  15031. ' this.SetItems(1, 2);',
  15032. ' this.SetItems(3,this.GetItems(4));',
  15033. ' this.SetItems(5,this.GetItems(6));',
  15034. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  15035. ' };',
  15036. '});',
  15037. 'this.Obj = null;'
  15038. ]),
  15039. LinesToStr([ // $mod.$main
  15040. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  15041. ]));
  15042. end;
  15043. procedure TTestModule.TestClass_Property_IndexSpec;
  15044. begin
  15045. StartProgram(false);
  15046. Add([
  15047. 'type',
  15048. ' TEnum = (red, blue);',
  15049. ' TObject = class',
  15050. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  15051. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  15052. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  15053. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  15054. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  15055. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  15056. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  15057. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  15058. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  15059. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  15060. ' end;',
  15061. 'procedure DoIt(b: boolean); begin end;',
  15062. 'var',
  15063. ' o: TObject;',
  15064. 'begin',
  15065. ' o.B1:=o.B1;',
  15066. ' o.B2:=o.B2;',
  15067. ' o.B3:=o.B3;',
  15068. ' o.I1[''a'']:=o.I1[''b''];',
  15069. ' doit(o.b1);',
  15070. ' doit(o.b2);',
  15071. ' doit(o.i1[''c'']);',
  15072. '']);
  15073. ConvertProgram;
  15074. CheckSource('TestClass_Property_IndexSpec',
  15075. LinesToStr([ // statements
  15076. 'this.TEnum = {',
  15077. ' "0": "red",',
  15078. ' red: 0,',
  15079. ' "1": "blue",',
  15080. ' blue: 1',
  15081. '};',
  15082. 'rtl.createClass(this, "TObject", null, function () {',
  15083. ' this.$init = function () {',
  15084. ' };',
  15085. ' this.$final = function () {',
  15086. ' };',
  15087. '});',
  15088. 'this.DoIt = function (b) {',
  15089. '};',
  15090. 'this.o = null;',
  15091. '']),
  15092. LinesToStr([ // $mod.$main
  15093. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  15094. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  15095. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  15096. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  15097. '$mod.DoIt($mod.o.GetIntBool(1));',
  15098. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  15099. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  15100. '']));
  15101. end;
  15102. procedure TTestModule.TestClass_PropertyOfTypeArray;
  15103. begin
  15104. StartProgram(false);
  15105. Add('type');
  15106. Add(' TArray = array of longint;');
  15107. Add(' TObject = class');
  15108. Add(' FItems: TArray;');
  15109. Add(' function GetItems: tarray;');
  15110. Add(' procedure SetItems(Value: tarray);');
  15111. Add(' property Items: tarray read getitems write setitems;');
  15112. Add(' procedure SetNumbers(const Value: tarray);');
  15113. Add(' property Numbers: tarray write setnumbers;');
  15114. Add(' end;');
  15115. Add('function tobject.getitems: tarray;');
  15116. Add('begin');
  15117. Add(' Result:=fitems;');
  15118. Add('end;');
  15119. Add('procedure tobject.setitems(value: tarray);');
  15120. Add('begin');
  15121. Add(' fitems:=value;');
  15122. Add(' fitems:=nil;');
  15123. Add(' Items:=nil;');
  15124. Add(' Items:=Items;');
  15125. Add(' Items[1]:=2;');
  15126. Add(' fitems[3]:=Items[4];');
  15127. Add(' Items[5]:=Items[6];');
  15128. Add(' Self.Items[7]:=8;');
  15129. Add(' Self.Items[9]:=Self.Items[10];');
  15130. Add(' Items[Items[11]]:=Items[Items[12]];');
  15131. Add('end;');
  15132. Add('procedure tobject.SetNumbers(const Value: tarray);');
  15133. Add('begin;');
  15134. Add(' Numbers:=nil;');
  15135. Add(' Numbers:=Value;');
  15136. Add(' Self.Numbers:=Value;');
  15137. Add('end;');
  15138. Add('var Obj: tobject;');
  15139. Add('begin');
  15140. Add(' obj.items:=nil;');
  15141. Add(' obj.items:=obj.items;');
  15142. Add(' obj.items[11]:=obj.items[12];');
  15143. ConvertProgram;
  15144. CheckSource('TestClass_PropertyOfTypeArray',
  15145. LinesToStr([ // statements
  15146. 'rtl.createClass(this, "TObject", null, function () {',
  15147. ' this.$init = function () {',
  15148. ' this.FItems = [];',
  15149. ' };',
  15150. ' this.$final = function () {',
  15151. ' this.FItems = undefined;',
  15152. ' };',
  15153. ' this.GetItems = function () {',
  15154. ' var Result = [];',
  15155. ' Result = rtl.arrayRef(this.FItems);',
  15156. ' return Result;',
  15157. ' };',
  15158. ' this.SetItems = function (Value) {',
  15159. ' this.FItems = rtl.arrayRef(Value);',
  15160. ' this.FItems = [];',
  15161. ' this.SetItems([]);',
  15162. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  15163. ' this.GetItems()[1] = 2;',
  15164. ' this.FItems[3] = this.GetItems()[4];',
  15165. ' this.GetItems()[5] = this.GetItems()[6];',
  15166. ' this.GetItems()[7] = 8;',
  15167. ' this.GetItems()[9] = this.GetItems()[10];',
  15168. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  15169. ' };',
  15170. ' this.SetNumbers = function (Value) {',
  15171. ' this.SetNumbers([]);',
  15172. ' this.SetNumbers(Value);',
  15173. ' this.SetNumbers(Value);',
  15174. ' };',
  15175. '});',
  15176. 'this.Obj = null;'
  15177. ]),
  15178. LinesToStr([ // $mod.$main
  15179. '$mod.Obj.SetItems([]);',
  15180. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  15181. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  15182. ]));
  15183. end;
  15184. procedure TTestModule.TestClass_PropertyDefault;
  15185. begin
  15186. StartProgram(false);
  15187. Add([
  15188. 'type',
  15189. ' TArray = array of longint;',
  15190. ' TObject = class',
  15191. ' end;',
  15192. ' TBird = class',
  15193. ' FItems: TArray;',
  15194. ' function GetItems(Index: longint): longint;',
  15195. ' procedure SetItems(Index, Value: longint);',
  15196. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  15197. ' end;',
  15198. 'function TBird.getitems(index: longint): longint;',
  15199. 'begin',
  15200. 'end;',
  15201. 'procedure TBird.setitems(index, value: longint);',
  15202. 'begin',
  15203. ' Self[1]:=2;',
  15204. ' Self[3]:=Self[index];',
  15205. ' Self[index]:=Self[Self[value]];',
  15206. ' Self[Self[4]]:=value;',
  15207. 'end;',
  15208. 'var',
  15209. ' Bird: TBird;',
  15210. ' Obj: TObject;',
  15211. 'begin',
  15212. ' bird[11]:=12;',
  15213. ' bird[13]:=bird[14];',
  15214. ' bird[Bird[15]]:=bird[Bird[15]];',
  15215. ' TBird(obj)[16]:=TBird(obj)[17];',
  15216. ' (obj as tbird)[18]:=19;',
  15217. '']);
  15218. ConvertProgram;
  15219. CheckSource('TestClass_PropertyDefault',
  15220. LinesToStr([ // statements
  15221. 'rtl.createClass(this, "TObject", null, function () {',
  15222. ' this.$init = function () {',
  15223. ' };',
  15224. ' this.$final = function () {',
  15225. ' };',
  15226. '});',
  15227. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15228. ' this.$init = function () {',
  15229. ' $mod.TObject.$init.call(this);',
  15230. ' this.FItems = [];',
  15231. ' };',
  15232. ' this.$final = function () {',
  15233. ' this.FItems = undefined;',
  15234. ' $mod.TObject.$final.call(this);',
  15235. ' };',
  15236. ' this.GetItems = function (Index) {',
  15237. ' var Result = 0;',
  15238. ' return Result;',
  15239. ' };',
  15240. ' this.SetItems = function (Index, Value) {',
  15241. ' this.SetItems(1, 2);',
  15242. ' this.SetItems(3, this.GetItems(Index));',
  15243. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  15244. ' this.SetItems(this.GetItems(4), Value);',
  15245. ' };',
  15246. '});',
  15247. 'this.Bird = null;',
  15248. 'this.Obj = null;',
  15249. '']),
  15250. LinesToStr([ // $mod.$main
  15251. '$mod.Bird.SetItems(11, 12);',
  15252. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  15253. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  15254. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  15255. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  15256. '']));
  15257. end;
  15258. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  15259. begin
  15260. StartProgram(false);
  15261. Add([
  15262. 'type',
  15263. ' TObject = class end;',
  15264. ' TAlphaList = class',
  15265. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  15266. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  15267. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  15268. ' end;',
  15269. ' TBetaList = class',
  15270. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  15271. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  15272. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  15273. ' end;',
  15274. ' TBird = class',
  15275. ' procedure DoIt;',
  15276. ' end;',
  15277. 'procedure TBird.DoIt;',
  15278. 'var',
  15279. ' List: TAlphaList;',
  15280. 'begin',
  15281. ' if TBetaList(List[true])[3]=nil then ;',
  15282. ' TBetaList(List[false])[5]:=nil;',
  15283. 'end;',
  15284. 'var',
  15285. ' List: TAlphaList;',
  15286. 'begin',
  15287. ' if TBetaList(List[true])[3]=nil then ;',
  15288. ' TBetaList(List[false])[5]:=nil;',
  15289. '']);
  15290. ConvertProgram;
  15291. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  15292. LinesToStr([ // statements
  15293. 'rtl.createClass(this, "TObject", null, function () {',
  15294. ' this.$init = function () {',
  15295. ' };',
  15296. ' this.$final = function () {',
  15297. ' };',
  15298. '});',
  15299. 'rtl.createClass(this, "TAlphaList", this.TObject, function () {',
  15300. '});',
  15301. 'rtl.createClass(this, "TBetaList", this.TObject, function () {',
  15302. '});',
  15303. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15304. ' this.DoIt = function () {',
  15305. ' var List = null;',
  15306. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  15307. ' List.GetAlphas(false).SetBetas(5, null);',
  15308. ' };',
  15309. '});',
  15310. 'this.List = null;',
  15311. '']),
  15312. LinesToStr([ // $mod.$main
  15313. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  15314. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  15315. '']));
  15316. end;
  15317. procedure TTestModule.TestClass_PropertyOverride;
  15318. begin
  15319. StartProgram(false);
  15320. Add('type');
  15321. Add(' integer = longint;');
  15322. Add(' TObject = class');
  15323. Add(' FItem: integer;');
  15324. Add(' function GetItem: integer; external name ''GetItem'';');
  15325. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  15326. Add(' property Item: integer read getitem write setitem;');
  15327. Add(' end;');
  15328. Add(' TCar = class');
  15329. Add(' FBag: integer;');
  15330. Add(' function GetBag: integer; external name ''GetBag'';');
  15331. Add(' property Item read getbag;');
  15332. Add(' end;');
  15333. Add('var');
  15334. Add(' Obj: tobject;');
  15335. Add(' Car: tcar;');
  15336. Add('begin');
  15337. Add(' Obj.Item:=Obj.Item;');
  15338. Add(' Car.Item:=Car.Item;');
  15339. ConvertProgram;
  15340. CheckSource('TestClass_PropertyOverride',
  15341. LinesToStr([ // statements
  15342. 'rtl.createClass(this, "TObject", null, function () {',
  15343. ' this.$init = function () {',
  15344. ' this.FItem = 0;',
  15345. ' };',
  15346. ' this.$final = function () {',
  15347. ' };',
  15348. '});',
  15349. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15350. ' this.$init = function () {',
  15351. ' $mod.TObject.$init.call(this);',
  15352. ' this.FBag = 0;',
  15353. ' };',
  15354. '});',
  15355. 'this.Obj = null;',
  15356. 'this.Car = null;',
  15357. '']),
  15358. LinesToStr([ // $mod.$main
  15359. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  15360. '$mod.Car.SetItem($mod.Car.GetBag());',
  15361. '']));
  15362. end;
  15363. procedure TTestModule.TestClass_PropertyIncVisibility;
  15364. begin
  15365. AddModuleWithIntfImplSrc('unit1.pp',
  15366. LinesToStr([
  15367. 'type',
  15368. ' TNumber = longint;',
  15369. ' TInteger = longint;',
  15370. ' TObject = class',
  15371. ' private',
  15372. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  15373. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  15374. ' protected',
  15375. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  15376. ' end;']),
  15377. LinesToStr([
  15378. '']));
  15379. StartProgram(true);
  15380. Add([
  15381. 'uses unit1;',
  15382. 'type',
  15383. ' TBird = class',
  15384. ' public',
  15385. ' property Items;',
  15386. ' end;',
  15387. 'procedure DoIt(i: TInteger);',
  15388. 'begin',
  15389. 'end;',
  15390. 'var b: TBird;',
  15391. 'begin',
  15392. ' b.Items[1]:=2;',
  15393. ' b.Items[3]:=b.Items[4];',
  15394. ' DoIt(b.Items[5]);',
  15395. '']);
  15396. ConvertProgram;
  15397. CheckSource('TestClass_PropertyIncVisibility',
  15398. LinesToStr([ // statements
  15399. 'rtl.createClass(this, "TBird", pas.unit1.TObject, function () {',
  15400. '});',
  15401. 'this.DoIt = function (i) {',
  15402. '};',
  15403. 'this.b = null;'
  15404. ]),
  15405. LinesToStr([ // $mod.$main
  15406. '$mod.b.SetItems(1, 2);',
  15407. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  15408. '$mod.DoIt($mod.b.GetItems(5));'
  15409. ]));
  15410. end;
  15411. procedure TTestModule.TestClass_Assigned;
  15412. begin
  15413. StartProgram(false);
  15414. Add('type');
  15415. Add(' TObject = class');
  15416. Add(' end;');
  15417. Add('var');
  15418. Add(' Obj: tobject;');
  15419. Add(' b: boolean;');
  15420. Add('begin');
  15421. Add(' if Assigned(obj) then ;');
  15422. Add(' b:=Assigned(obj) or false;');
  15423. ConvertProgram;
  15424. CheckSource('TestClass_Assigned',
  15425. LinesToStr([ // statements
  15426. 'rtl.createClass(this, "TObject", null, function () {',
  15427. ' this.$init = function () {',
  15428. ' };',
  15429. ' this.$final = function () {',
  15430. ' };',
  15431. '});',
  15432. 'this.Obj = null;',
  15433. 'this.b = false;'
  15434. ]),
  15435. LinesToStr([ // $mod.$main
  15436. 'if ($mod.Obj != null);',
  15437. '$mod.b = ($mod.Obj != null) || false;'
  15438. ]));
  15439. end;
  15440. procedure TTestModule.TestClass_WithClassDoCreate;
  15441. begin
  15442. StartProgram(false);
  15443. Add('type');
  15444. Add(' TObject = class');
  15445. Add(' aBool: boolean;');
  15446. Add(' Arr: array of boolean;');
  15447. Add(' constructor Create;');
  15448. Add(' end;');
  15449. Add('constructor TObject.Create; begin end;');
  15450. Add('var');
  15451. Add(' Obj: tobject;');
  15452. Add(' b: boolean;');
  15453. Add('begin');
  15454. Add(' with tobject.create do begin');
  15455. Add(' b:=abool;');
  15456. Add(' abool:=b;');
  15457. Add(' b:=arr[1];');
  15458. Add(' arr[2]:=b;');
  15459. Add(' end;');
  15460. Add(' with tobject do');
  15461. Add(' obj:=create;');
  15462. Add(' with obj do begin');
  15463. Add(' create;');
  15464. Add(' b:=abool;');
  15465. Add(' abool:=b;');
  15466. Add(' b:=arr[3];');
  15467. Add(' arr[4]:=b;');
  15468. Add(' end;');
  15469. ConvertProgram;
  15470. CheckSource('TestClass_WithClassDoCreate',
  15471. LinesToStr([ // statements
  15472. 'rtl.createClass(this, "TObject", null, function () {',
  15473. ' this.$init = function () {',
  15474. ' this.aBool = false;',
  15475. ' this.Arr = [];',
  15476. ' };',
  15477. ' this.$final = function () {',
  15478. ' this.Arr = undefined;',
  15479. ' };',
  15480. ' this.Create = function () {',
  15481. ' return this;',
  15482. ' };',
  15483. '});',
  15484. 'this.Obj = null;',
  15485. 'this.b = false;'
  15486. ]),
  15487. LinesToStr([ // $mod.$main
  15488. 'var $with = $mod.TObject.$create("Create");',
  15489. '$mod.b = $with.aBool;',
  15490. '$with.aBool = $mod.b;',
  15491. '$mod.b = $with.Arr[1];',
  15492. '$with.Arr[2] = $mod.b;',
  15493. 'var $with1 = $mod.TObject;',
  15494. '$mod.Obj = $with1.$create("Create");',
  15495. 'var $with2 = $mod.Obj;',
  15496. '$with2.Create();',
  15497. '$mod.b = $with2.aBool;',
  15498. '$with2.aBool = $mod.b;',
  15499. '$mod.b = $with2.Arr[3];',
  15500. '$with2.Arr[4] = $mod.b;',
  15501. '']));
  15502. end;
  15503. procedure TTestModule.TestClass_WithClassInstDoProperty;
  15504. begin
  15505. StartProgram(false);
  15506. Add('type');
  15507. Add(' TObject = class');
  15508. Add(' FInt: longint;');
  15509. Add(' constructor Create;');
  15510. Add(' function GetSize: longint;');
  15511. Add(' procedure SetSize(Value: longint);');
  15512. Add(' property Int: longint read FInt write FInt;');
  15513. Add(' property Size: longint read GetSize write SetSize;');
  15514. Add(' end;');
  15515. Add('constructor TObject.Create; begin end;');
  15516. Add('function TObject.GetSize: longint; begin; end;');
  15517. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  15518. Add('var');
  15519. Add(' Obj: tobject;');
  15520. Add(' i: longint;');
  15521. Add('begin');
  15522. Add(' with TObject.Create do begin');
  15523. Add(' i:=int;');
  15524. Add(' int:=i;');
  15525. Add(' i:=size;');
  15526. Add(' size:=i;');
  15527. Add(' end;');
  15528. Add(' with obj do begin');
  15529. Add(' i:=int;');
  15530. Add(' int:=i;');
  15531. Add(' i:=size;');
  15532. Add(' size:=i;');
  15533. Add(' end;');
  15534. ConvertProgram;
  15535. CheckSource('TestClass_WithClassInstDoProperty',
  15536. LinesToStr([ // statements
  15537. 'rtl.createClass(this, "TObject", null, function () {',
  15538. ' this.$init = function () {',
  15539. ' this.FInt = 0;',
  15540. ' };',
  15541. ' this.$final = function () {',
  15542. ' };',
  15543. ' this.Create = function () {',
  15544. ' return this;',
  15545. ' };',
  15546. ' this.GetSize = function () {',
  15547. ' var Result = 0;',
  15548. ' return Result;',
  15549. ' };',
  15550. ' this.SetSize = function (Value) {',
  15551. ' };',
  15552. '});',
  15553. 'this.Obj = null;',
  15554. 'this.i = 0;'
  15555. ]),
  15556. LinesToStr([ // $mod.$main
  15557. 'var $with = $mod.TObject.$create("Create");',
  15558. '$mod.i = $with.FInt;',
  15559. '$with.FInt = $mod.i;',
  15560. '$mod.i = $with.GetSize();',
  15561. '$with.SetSize($mod.i);',
  15562. 'var $with1 = $mod.Obj;',
  15563. '$mod.i = $with1.FInt;',
  15564. '$with1.FInt = $mod.i;',
  15565. '$mod.i = $with1.GetSize();',
  15566. '$with1.SetSize($mod.i);',
  15567. '']));
  15568. end;
  15569. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  15570. begin
  15571. StartProgram(false);
  15572. Add('type');
  15573. Add(' TObject = class');
  15574. Add(' constructor Create;');
  15575. Add(' function GetItems(Index: longint): longint;');
  15576. Add(' procedure SetItems(Index, Value: longint);');
  15577. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  15578. Add(' end;');
  15579. Add('constructor TObject.Create; begin end;');
  15580. Add('function tobject.getitems(index: longint): longint; begin; end;');
  15581. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  15582. Add('var');
  15583. Add(' Obj: tobject;');
  15584. Add(' i: longint;');
  15585. Add('begin');
  15586. Add(' with TObject.Create do begin');
  15587. Add(' i:=Items[1];');
  15588. Add(' Items[2]:=i;');
  15589. Add(' end;');
  15590. Add(' with obj do begin');
  15591. Add(' i:=Items[3];');
  15592. Add(' Items[4]:=i;');
  15593. Add(' end;');
  15594. ConvertProgram;
  15595. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  15596. LinesToStr([ // statements
  15597. 'rtl.createClass(this, "TObject", null, function () {',
  15598. ' this.$init = function () {',
  15599. ' };',
  15600. ' this.$final = function () {',
  15601. ' };',
  15602. ' this.Create = function () {',
  15603. ' return this;',
  15604. ' };',
  15605. ' this.GetItems = function (Index) {',
  15606. ' var Result = 0;',
  15607. ' return Result;',
  15608. ' };',
  15609. ' this.SetItems = function (Index, Value) {',
  15610. ' };',
  15611. '});',
  15612. 'this.Obj = null;',
  15613. 'this.i = 0;'
  15614. ]),
  15615. LinesToStr([ // $mod.$main
  15616. 'var $with = $mod.TObject.$create("Create");',
  15617. '$mod.i = $with.GetItems(1);',
  15618. '$with.SetItems(2, $mod.i);',
  15619. 'var $with1 = $mod.Obj;',
  15620. '$mod.i = $with1.GetItems(3);',
  15621. '$with1.SetItems(4, $mod.i);',
  15622. '']));
  15623. end;
  15624. procedure TTestModule.TestClass_WithClassInstDoFunc;
  15625. begin
  15626. StartProgram(false);
  15627. Add('type');
  15628. Add(' TObject = class');
  15629. Add(' constructor Create;');
  15630. Add(' function GetSize: longint;');
  15631. Add(' procedure SetSize(Value: longint);');
  15632. Add(' end;');
  15633. Add('constructor TObject.Create; begin end;');
  15634. Add('function TObject.GetSize: longint; begin; end;');
  15635. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  15636. Add('var');
  15637. Add(' Obj: tobject;');
  15638. Add(' i: longint;');
  15639. Add('begin');
  15640. Add(' with TObject.Create do begin');
  15641. Add(' i:=GetSize;');
  15642. Add(' i:=GetSize();');
  15643. Add(' SetSize(i);');
  15644. Add(' end;');
  15645. Add(' with obj do begin');
  15646. Add(' i:=GetSize;');
  15647. Add(' i:=GetSize();');
  15648. Add(' SetSize(i);');
  15649. Add(' end;');
  15650. ConvertProgram;
  15651. CheckSource('TestClass_WithClassInstDoFunc',
  15652. LinesToStr([ // statements
  15653. 'rtl.createClass(this, "TObject", null, function () {',
  15654. ' this.$init = function () {',
  15655. ' };',
  15656. ' this.$final = function () {',
  15657. ' };',
  15658. ' this.Create = function () {',
  15659. ' return this;',
  15660. ' };',
  15661. ' this.GetSize = function () {',
  15662. ' var Result = 0;',
  15663. ' return Result;',
  15664. ' };',
  15665. ' this.SetSize = function (Value) {',
  15666. ' };',
  15667. '});',
  15668. 'this.Obj = null;',
  15669. 'this.i = 0;'
  15670. ]),
  15671. LinesToStr([ // $mod.$main
  15672. 'var $with = $mod.TObject.$create("Create");',
  15673. '$mod.i = $with.GetSize();',
  15674. '$mod.i = $with.GetSize();',
  15675. '$with.SetSize($mod.i);',
  15676. 'var $with1 = $mod.Obj;',
  15677. '$mod.i = $with1.GetSize();',
  15678. '$mod.i = $with1.GetSize();',
  15679. '$with1.SetSize($mod.i);',
  15680. '']));
  15681. end;
  15682. procedure TTestModule.TestClass_TypeCast;
  15683. begin
  15684. StartProgram(false);
  15685. Add('type');
  15686. Add(' TObject = class');
  15687. Add(' Next: TObject;');
  15688. Add(' constructor Create;');
  15689. Add(' end;');
  15690. Add(' TControl = class(TObject)');
  15691. Add(' Arr: array of TObject;');
  15692. Add(' function GetIt(vI: longint = 0): TObject;');
  15693. Add(' end;');
  15694. Add('constructor tobject.create; begin end;');
  15695. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  15696. Add('var');
  15697. Add(' Obj: tobject;');
  15698. Add('begin');
  15699. Add(' obj:=tcontrol(obj).next;');
  15700. Add(' tcontrol(obj):=nil;');
  15701. Add(' obj:=tcontrol(obj);');
  15702. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  15703. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  15704. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  15705. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  15706. Add(' obj:=tcontrol(nil);');
  15707. ConvertProgram;
  15708. CheckSource('TestClass_TypeCast',
  15709. LinesToStr([ // statements
  15710. 'rtl.createClass(this, "TObject", null, function () {',
  15711. ' this.$init = function () {',
  15712. ' this.Next = null;',
  15713. ' };',
  15714. ' this.$final = function () {',
  15715. ' this.Next = undefined;',
  15716. ' };',
  15717. ' this.Create = function () {',
  15718. ' return this;',
  15719. ' };',
  15720. '});',
  15721. 'rtl.createClass(this, "TControl", this.TObject, function () {',
  15722. ' this.$init = function () {',
  15723. ' $mod.TObject.$init.call(this);',
  15724. ' this.Arr = [];',
  15725. ' };',
  15726. ' this.$final = function () {',
  15727. ' this.Arr = undefined;',
  15728. ' $mod.TObject.$final.call(this);',
  15729. ' };',
  15730. ' this.GetIt = function (vI) {',
  15731. ' var Result = null;',
  15732. ' return Result;',
  15733. ' };',
  15734. '});',
  15735. 'this.Obj = null;'
  15736. ]),
  15737. LinesToStr([ // $mod.$main
  15738. '$mod.Obj = $mod.Obj.Next;',
  15739. '$mod.Obj = null;',
  15740. '$mod.Obj = $mod.Obj;',
  15741. '$mod.Obj = $mod.Obj.GetIt(0);',
  15742. '$mod.Obj = $mod.Obj.GetIt(0);',
  15743. '$mod.Obj = $mod.Obj.GetIt(1);',
  15744. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  15745. '$mod.Obj = null;',
  15746. '']));
  15747. end;
  15748. procedure TTestModule.TestClass_TypeCastUntypedParam;
  15749. begin
  15750. StartProgram(false);
  15751. Add('type');
  15752. Add(' TObject = class end;');
  15753. Add('procedure ProcA(var A);');
  15754. Add('begin');
  15755. Add(' TObject(A):=nil;');
  15756. Add(' TObject(A):=TObject(A);');
  15757. Add(' if TObject(A)=nil then ;');
  15758. Add(' if nil=TObject(A) then ;');
  15759. Add('end;');
  15760. Add('procedure ProcB(out A);');
  15761. Add('begin');
  15762. Add(' TObject(A):=nil;');
  15763. Add(' TObject(A):=TObject(A);');
  15764. Add(' if TObject(A)=nil then ;');
  15765. Add(' if nil=TObject(A) then ;');
  15766. Add('end;');
  15767. Add('procedure ProcC(const A);');
  15768. Add('begin');
  15769. Add(' if TObject(A)=nil then ;');
  15770. Add(' if nil=TObject(A) then ;');
  15771. Add('end;');
  15772. Add('var o: TObject;');
  15773. Add('begin');
  15774. Add(' ProcA(o);');
  15775. Add(' ProcB(o);');
  15776. Add(' ProcC(o);');
  15777. ConvertProgram;
  15778. CheckSource('TestClass_TypeCastUntypedParam',
  15779. LinesToStr([ // statements
  15780. 'rtl.createClass(this, "TObject", null, function () {',
  15781. ' this.$init = function () {',
  15782. ' };',
  15783. ' this.$final = function () {',
  15784. ' };',
  15785. '});',
  15786. 'this.ProcA = function (A) {',
  15787. ' A.set(null);',
  15788. ' A.set(A.get());',
  15789. ' if (A.get() === null);',
  15790. ' if (null === A.get());',
  15791. '};',
  15792. 'this.ProcB = function (A) {',
  15793. ' A.set(null);',
  15794. ' A.set(A.get());',
  15795. ' if (A.get() === null);',
  15796. ' if (null === A.get());',
  15797. '};',
  15798. 'this.ProcC = function (A) {',
  15799. ' if (A === null);',
  15800. ' if (null === A);',
  15801. '};',
  15802. 'this.o = null;',
  15803. '']),
  15804. LinesToStr([ // $mod.$main
  15805. '$mod.ProcA({',
  15806. ' p: $mod,',
  15807. ' get: function () {',
  15808. ' return this.p.o;',
  15809. ' },',
  15810. ' set: function (v) {',
  15811. ' this.p.o = v;',
  15812. ' }',
  15813. '});',
  15814. '$mod.ProcB({',
  15815. ' p: $mod,',
  15816. ' get: function () {',
  15817. ' return this.p.o;',
  15818. ' },',
  15819. ' set: function (v) {',
  15820. ' this.p.o = v;',
  15821. ' }',
  15822. '});',
  15823. '$mod.ProcC($mod.o);',
  15824. '']));
  15825. end;
  15826. procedure TTestModule.TestClass_Overloads;
  15827. begin
  15828. StartProgram(false);
  15829. Add('type');
  15830. Add(' TObject = class');
  15831. Add(' procedure DoIt;');
  15832. Add(' procedure DoIt(vI: longint);');
  15833. Add(' end;');
  15834. Add('procedure TObject.DoIt;');
  15835. Add('begin');
  15836. Add(' DoIt;');
  15837. Add(' DoIt(1);');
  15838. Add('end;');
  15839. Add('procedure TObject.DoIt(vI: longint); begin end;');
  15840. Add('begin');
  15841. ConvertProgram;
  15842. CheckSource('TestClass_Overloads',
  15843. LinesToStr([ // statements
  15844. 'rtl.createClass(this, "TObject", null, function () {',
  15845. ' this.$init = function () {',
  15846. ' };',
  15847. ' this.$final = function () {',
  15848. ' };',
  15849. ' this.DoIt = function () {',
  15850. ' this.DoIt();',
  15851. ' this.DoIt$1(1);',
  15852. ' };',
  15853. ' this.DoIt$1 = function (vI) {',
  15854. ' };',
  15855. '});',
  15856. '']),
  15857. LinesToStr([ // $mod.$main
  15858. '']));
  15859. end;
  15860. procedure TTestModule.TestClass_OverloadsAncestor;
  15861. begin
  15862. StartProgram(false);
  15863. Add('type');
  15864. Add(' TObject = class;');
  15865. Add(' TObject = class');
  15866. Add(' procedure DoIt(vA: longint);');
  15867. Add(' procedure DoIt(vA, vB: longint);');
  15868. Add(' end;');
  15869. Add(' TCar = class;');
  15870. Add(' TCar = class');
  15871. Add(' procedure DoIt(vA: longint);');
  15872. Add(' procedure DoIt(vA, vB: longint);');
  15873. Add(' end;');
  15874. Add('procedure tobject.doit(va: longint);');
  15875. Add('begin');
  15876. Add(' doit(1);');
  15877. Add(' doit(1,2);');
  15878. Add('end;');
  15879. Add('procedure tobject.doit(va, vb: longint); begin end;');
  15880. Add('procedure tcar.doit(va: longint);');
  15881. Add('begin');
  15882. Add(' doit(1);');
  15883. Add(' doit(1,2);');
  15884. Add(' inherited doit(1);');
  15885. Add(' inherited doit(1,2);');
  15886. Add('end;');
  15887. Add('procedure tcar.doit(va, vb: longint); begin end;');
  15888. Add('begin');
  15889. ConvertProgram;
  15890. CheckSource('TestClass_OverloadsAncestor',
  15891. LinesToStr([ // statements
  15892. 'rtl.createClass(this, "TObject", null, function () {',
  15893. ' this.$init = function () {',
  15894. ' };',
  15895. ' this.$final = function () {',
  15896. ' };',
  15897. ' this.DoIt = function (vA) {',
  15898. ' this.DoIt(1);',
  15899. ' this.DoIt$1(1,2);',
  15900. ' };',
  15901. ' this.DoIt$1 = function (vA, vB) {',
  15902. ' };',
  15903. '});',
  15904. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15905. ' this.DoIt$2 = function (vA) {',
  15906. ' this.DoIt$2(1);',
  15907. ' this.DoIt$3(1, 2);',
  15908. ' $mod.TObject.DoIt.call(this, 1);',
  15909. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  15910. ' };',
  15911. ' this.DoIt$3 = function (vA, vB) {',
  15912. ' };',
  15913. '});',
  15914. '']),
  15915. LinesToStr([ // $mod.$main
  15916. '']));
  15917. end;
  15918. procedure TTestModule.TestClass_OverloadConstructor;
  15919. begin
  15920. StartProgram(false);
  15921. Add('type');
  15922. Add(' TObject = class');
  15923. Add(' constructor Create(vA: longint);');
  15924. Add(' constructor Create(vA, vB: longint);');
  15925. Add(' end;');
  15926. Add(' TCar = class');
  15927. Add(' constructor Create(vA: longint);');
  15928. Add(' constructor Create(vA, vB: longint);');
  15929. Add(' end;');
  15930. Add('constructor tobject.create(va: longint);');
  15931. Add('begin');
  15932. Add(' create(1);');
  15933. Add(' create(1,2);');
  15934. Add('end;');
  15935. Add('constructor tobject.create(va, vb: longint); begin end;');
  15936. Add('constructor tcar.create(va: longint);');
  15937. Add('begin');
  15938. Add(' create(1);');
  15939. Add(' create(1,2);');
  15940. Add(' inherited create(1);');
  15941. Add(' inherited create(1,2);');
  15942. Add('end;');
  15943. Add('constructor tcar.create(va, vb: longint); begin end;');
  15944. Add('begin');
  15945. Add(' tobject.create(1);');
  15946. Add(' tobject.create(1,2);');
  15947. Add(' tcar.create(1);');
  15948. Add(' tcar.create(1,2);');
  15949. ConvertProgram;
  15950. CheckSource('TestClass_OverloadConstructor',
  15951. LinesToStr([ // statements
  15952. 'rtl.createClass(this, "TObject", null, function () {',
  15953. ' this.$init = function () {',
  15954. ' };',
  15955. ' this.$final = function () {',
  15956. ' };',
  15957. ' this.Create = function (vA) {',
  15958. ' this.Create(1);',
  15959. ' this.Create$1(1,2);',
  15960. ' return this;',
  15961. ' };',
  15962. ' this.Create$1 = function (vA, vB) {',
  15963. ' return this;',
  15964. ' };',
  15965. '});',
  15966. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15967. ' this.Create$2 = function (vA) {',
  15968. ' this.Create$2(1);',
  15969. ' this.Create$3(1, 2);',
  15970. ' $mod.TObject.Create.call(this, 1);',
  15971. ' $mod.TObject.Create$1.call(this, 1, 2);',
  15972. ' return this;',
  15973. ' };',
  15974. ' this.Create$3 = function (vA, vB) {',
  15975. ' return this;',
  15976. ' };',
  15977. '});',
  15978. '']),
  15979. LinesToStr([ // $mod.$main
  15980. '$mod.TObject.$create("Create", [1]);',
  15981. '$mod.TObject.$create("Create$1", [1, 2]);',
  15982. '$mod.TCar.$create("Create$2", [1]);',
  15983. '$mod.TCar.$create("Create$3", [1, 2]);',
  15984. '']));
  15985. end;
  15986. procedure TTestModule.TestClass_OverloadDelphiOverride;
  15987. begin
  15988. StartProgram(false);
  15989. Add([
  15990. '{$mode delphi}',
  15991. 'type',
  15992. ' TObject = class end;',
  15993. ' TBird = class',
  15994. ' function {#a}GetValue: longint; overload; virtual;',
  15995. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  15996. ' end;',
  15997. ' TEagle = class(TBird)',
  15998. ' function {#c}GetValue: longint; overload; override;',
  15999. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  16000. ' end;',
  16001. 'function TBird.GetValue: longint;',
  16002. 'begin',
  16003. ' if 3={@a}GetValue then ;',
  16004. ' if 4={@b}GetValue(5) then ;',
  16005. 'end;',
  16006. 'function TBird.GetValue(AValue: longint): longint;',
  16007. 'begin',
  16008. 'end;',
  16009. 'function TEagle.GetValue: longint;',
  16010. 'begin',
  16011. ' if 13={@c}GetValue then ;',
  16012. ' if 14={@d}GetValue(15) then ;',
  16013. ' if 15=inherited {@a}GetValue then ;',
  16014. ' if 16=inherited {@b}GetValue(17) then ;',
  16015. 'end;',
  16016. 'function TEagle.GetValue(AValue: longint): longint;',
  16017. 'begin',
  16018. 'end;',
  16019. 'var',
  16020. ' e: TEagle;',
  16021. 'begin',
  16022. ' if 23=e.{@c}GetValue then ;',
  16023. ' if 24=e.{@d}GetValue(25) then ;']);
  16024. ConvertProgram;
  16025. CheckSource('TestClass_OverloadDelphiOverride',
  16026. LinesToStr([ // statements
  16027. 'rtl.createClass(this, "TObject", null, function () {',
  16028. ' this.$init = function () {',
  16029. ' };',
  16030. ' this.$final = function () {',
  16031. ' };',
  16032. '});',
  16033. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16034. ' this.GetValue = function () {',
  16035. ' var Result = 0;',
  16036. ' if (3 === this.GetValue()) ;',
  16037. ' if (4 === this.GetValue$1(5)) ;',
  16038. ' return Result;',
  16039. ' };',
  16040. ' this.GetValue$1 = function (AValue) {',
  16041. ' var Result = 0;',
  16042. ' return Result;',
  16043. ' };',
  16044. '});',
  16045. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  16046. ' this.GetValue = function () {',
  16047. ' var Result = 0;',
  16048. ' if (13 === this.GetValue()) ;',
  16049. ' if (14 === this.GetValue$1(15)) ;',
  16050. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  16051. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  16052. ' return Result;',
  16053. ' };',
  16054. ' this.GetValue$1 = function (AValue) {',
  16055. ' var Result = 0;',
  16056. ' return Result;',
  16057. ' };',
  16058. '});',
  16059. 'this.e = null;',
  16060. '']),
  16061. LinesToStr([ // $mod.$main
  16062. 'if (23 === $mod.e.GetValue()) ;',
  16063. 'if (24 === $mod.e.GetValue$1(25)) ;',
  16064. '']));
  16065. end;
  16066. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  16067. begin
  16068. StartProgram(false);
  16069. Add([
  16070. '{$mode delphi}',
  16071. 'type',
  16072. ' TObject = class end;',
  16073. ' TAnimal = class',
  16074. ' public',
  16075. ' {#animal_a}A: longint;',
  16076. ' function {#animal_b}B: longint;',
  16077. ' end;',
  16078. ' TBird = class(TAnimal)',
  16079. ' public',
  16080. ' {#bird_a}A: double;',
  16081. ' {#bird_b}B: boolean;',
  16082. ' end;',
  16083. ' TEagle = class(TBird)',
  16084. ' public',
  16085. ' function {#eagle_a}A: boolean;',
  16086. ' {#eagle_b}B: double;',
  16087. ' end;',
  16088. 'function TAnimal.B: longint;',
  16089. 'begin',
  16090. 'end;',
  16091. 'function TEagle.A: boolean;',
  16092. 'begin',
  16093. ' {@eagle_b}B:=3.3;',
  16094. ' {@eagle_a}A();',
  16095. ' TBird(Self).{@bird_b}B:=true;',
  16096. ' TAnimal(Self).{@animal_a}A:=17;',
  16097. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  16098. 'end;',
  16099. 'var',
  16100. ' e: TEagle;',
  16101. 'begin',
  16102. ' e.{@eagle_b}B:=5.3;',
  16103. ' if e.{@eagle_a}A then ;',
  16104. '']);
  16105. ConvertProgram;
  16106. CheckSource('TestClass_ReintroduceVarDelphi',
  16107. LinesToStr([ // statements
  16108. 'rtl.createClass(this, "TObject", null, function () {',
  16109. ' this.$init = function () {',
  16110. ' };',
  16111. ' this.$final = function () {',
  16112. ' };',
  16113. '});',
  16114. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  16115. ' this.$init = function () {',
  16116. ' $mod.TObject.$init.call(this);',
  16117. ' this.A = 0;',
  16118. ' };',
  16119. ' this.B = function () {',
  16120. ' var Result = 0;',
  16121. ' return Result;',
  16122. ' };',
  16123. '});',
  16124. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  16125. ' this.$init = function () {',
  16126. ' $mod.TAnimal.$init.call(this);',
  16127. ' this.A$1 = 0.0;',
  16128. ' this.B$1 = false;',
  16129. ' };',
  16130. '});',
  16131. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  16132. ' this.$init = function () {',
  16133. ' $mod.TBird.$init.call(this);',
  16134. ' this.B$2 = 0.0;',
  16135. ' };',
  16136. ' this.A$2 = function () {',
  16137. ' var Result = false;',
  16138. ' this.B$2 = 3.3;',
  16139. ' this.A$2();',
  16140. ' this.B$1 = true;',
  16141. ' this.A = 17;',
  16142. ' this.B$1 = this.A$1 > 1;',
  16143. ' return Result;',
  16144. ' };',
  16145. '});',
  16146. 'this.e = null;',
  16147. '']),
  16148. LinesToStr([ // $mod.$main
  16149. '$mod.e.B$2 = 5.3;',
  16150. 'if ($mod.e.A$2()) ;',
  16151. '']));
  16152. end;
  16153. procedure TTestModule.TestClass_ReintroducedVar;
  16154. begin
  16155. StartProgram(false);
  16156. Add('type');
  16157. Add(' TObject = class');
  16158. Add(' strict private');
  16159. Add(' Some: longint;');
  16160. Add(' end;');
  16161. Add(' TMobile = class');
  16162. Add(' strict private');
  16163. Add(' Some: string;');
  16164. Add(' end;');
  16165. Add(' TCar = class(tmobile)');
  16166. Add(' procedure Some;');
  16167. Add(' procedure Some(vA: longint);');
  16168. Add(' end;');
  16169. Add('procedure tcar.some;');
  16170. Add('begin');
  16171. Add(' Some;');
  16172. Add(' Some(1);');
  16173. Add('end;');
  16174. Add('procedure tcar.some(va: longint); begin end;');
  16175. Add('begin');
  16176. ConvertProgram;
  16177. CheckSource('TestClass_ReintroducedVar',
  16178. LinesToStr([ // statements
  16179. 'rtl.createClass(this, "TObject", null, function () {',
  16180. ' this.$init = function () {',
  16181. ' this.Some = 0;',
  16182. ' };',
  16183. ' this.$final = function () {',
  16184. ' };',
  16185. '});',
  16186. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  16187. ' this.$init = function () {',
  16188. ' $mod.TObject.$init.call(this);',
  16189. ' this.Some$1 = "";',
  16190. ' };',
  16191. '});',
  16192. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  16193. ' this.Some$2 = function () {',
  16194. ' this.Some$2();',
  16195. ' this.Some$3(1);',
  16196. ' };',
  16197. ' this.Some$3 = function (vA) {',
  16198. ' };',
  16199. '});',
  16200. '']),
  16201. LinesToStr([ // $mod.$main
  16202. '']));
  16203. end;
  16204. procedure TTestModule.TestClass_RaiseDescendant;
  16205. begin
  16206. StartProgram(false);
  16207. Add([
  16208. 'type',
  16209. ' TObject = class',
  16210. ' constructor Create(Msg: string);',
  16211. ' end;',
  16212. ' Exception = class',
  16213. ' end;',
  16214. ' EConvertError = class(Exception)',
  16215. ' end;',
  16216. 'constructor TObject.Create(Msg: string); begin end;',
  16217. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  16218. 'begin',
  16219. ' raise Exception.Create(''Bar1'');',
  16220. ' raise EConvertError.Create(''Bar2'');',
  16221. ' raise AssertConv(''Bar2'');',
  16222. ' raise AssertConv;',
  16223. '']);
  16224. ConvertProgram;
  16225. CheckSource('TestClass_RaiseDescendant',
  16226. LinesToStr([ // statements
  16227. 'rtl.createClass(this, "TObject", null, function () {',
  16228. ' this.$init = function () {',
  16229. ' };',
  16230. ' this.$final = function () {',
  16231. ' };',
  16232. ' this.Create = function (Msg) {',
  16233. ' return this;',
  16234. ' };',
  16235. '});',
  16236. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  16237. '});',
  16238. 'rtl.createClass(this, "EConvertError", this.Exception, function () {',
  16239. '});',
  16240. 'this.AssertConv = function (Msg) {',
  16241. ' var Result = null;',
  16242. ' return Result;',
  16243. '};',
  16244. '']),
  16245. LinesToStr([ // $mod.$main
  16246. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  16247. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  16248. 'throw $mod.AssertConv("Bar2");',
  16249. 'throw $mod.AssertConv("def");',
  16250. '']));
  16251. end;
  16252. procedure TTestModule.TestClass_ExternalMethod;
  16253. begin
  16254. AddModuleWithIntfImplSrc('unit2.pas',
  16255. LinesToStr([
  16256. 'type',
  16257. ' TObject = class',
  16258. ' public',
  16259. ' procedure Intern; external name ''$DoIntern'';',
  16260. ' end;',
  16261. '']),
  16262. LinesToStr([
  16263. '']));
  16264. StartUnit(true);
  16265. Add('interface');
  16266. Add('uses unit2;');
  16267. Add('type');
  16268. Add(' TCar = class(TObject)');
  16269. Add(' public');
  16270. Add(' procedure Intern2; external name ''$DoIntern2'';');
  16271. Add(' procedure DoIt;');
  16272. Add(' end;');
  16273. Add('implementation');
  16274. Add('procedure tcar.doit;');
  16275. Add('begin');
  16276. Add(' Intern;');
  16277. Add(' Intern();');
  16278. Add(' Intern2;');
  16279. Add(' Intern2();');
  16280. Add('end;');
  16281. Add('var Obj: TCar;');
  16282. Add('begin');
  16283. Add(' obj.intern;');
  16284. Add(' obj.intern();');
  16285. Add(' obj.intern2;');
  16286. Add(' obj.intern2();');
  16287. Add(' obj.doit;');
  16288. Add(' obj.doit();');
  16289. Add(' with obj do begin');
  16290. Add(' Intern;');
  16291. Add(' Intern();');
  16292. Add(' Intern2;');
  16293. Add(' Intern2();');
  16294. Add(' end;');
  16295. ConvertUnit;
  16296. CheckSource('TestClass_ExternalMethod',
  16297. LinesToStr([
  16298. 'var $impl = $mod.$impl;',
  16299. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  16300. ' this.DoIt = function () {',
  16301. ' this.$DoIntern();',
  16302. ' this.$DoIntern();',
  16303. ' this.$DoIntern2();',
  16304. ' this.$DoIntern2();',
  16305. ' };',
  16306. ' });',
  16307. '']),
  16308. LinesToStr([ // this.$init
  16309. '$impl.Obj.$DoIntern();',
  16310. '$impl.Obj.$DoIntern();',
  16311. '$impl.Obj.$DoIntern2();',
  16312. '$impl.Obj.$DoIntern2();',
  16313. '$impl.Obj.DoIt();',
  16314. '$impl.Obj.DoIt();',
  16315. 'var $with = $impl.Obj;',
  16316. '$with.$DoIntern();',
  16317. '$with.$DoIntern();',
  16318. '$with.$DoIntern2();',
  16319. '$with.$DoIntern2();',
  16320. '']),
  16321. LinesToStr([ // implementation
  16322. '$impl.Obj = null;',
  16323. '']) );
  16324. end;
  16325. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  16326. begin
  16327. StartProgram(false);
  16328. Add('type');
  16329. Add(' TObject = class');
  16330. Add(' procedure DoIt; virtual; external name ''Foo'';');
  16331. Add(' end;');
  16332. Add('begin');
  16333. SetExpectedPasResolverError('Virtual method name must match external',
  16334. nVirtualMethodNameMustMatchExternal);
  16335. ConvertProgram;
  16336. end;
  16337. procedure TTestModule.TestClass_ExternalOverrideFail;
  16338. begin
  16339. StartProgram(false);
  16340. Add('type');
  16341. Add(' TObject = class');
  16342. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  16343. Add(' end;');
  16344. Add(' TCar = class');
  16345. Add(' procedure DoIt; override; external name ''DoIt'';');
  16346. Add(' end;');
  16347. Add('begin');
  16348. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  16349. nInvalidXModifierY);
  16350. ConvertProgram;
  16351. end;
  16352. procedure TTestModule.TestClass_ExternalVar;
  16353. begin
  16354. AddModuleWithIntfImplSrc('unit2.pas',
  16355. LinesToStr([
  16356. '{$modeswitch externalclass}',
  16357. 'type',
  16358. ' TObject = class',
  16359. ' public',
  16360. ' Intern: longint external name ''$Intern'';',
  16361. ' Bracket: longint external name ''["A B"]'';',
  16362. ' end;',
  16363. '']),
  16364. LinesToStr([
  16365. '']));
  16366. StartUnit(true);
  16367. Add([
  16368. 'interface',
  16369. 'uses unit2;',
  16370. '{$modeswitch externalclass}',
  16371. 'type',
  16372. ' TCar = class(tobject)',
  16373. ' public',
  16374. ' Intern2: longint external name ''$Intern2'';',
  16375. ' procedure DoIt;',
  16376. ' end;',
  16377. 'implementation',
  16378. 'procedure tcar.doit;',
  16379. 'begin',
  16380. ' Intern:=Intern+1;',
  16381. ' Intern2:=Intern2+2;',
  16382. ' Bracket:=Bracket+3;',
  16383. 'end;',
  16384. 'var Obj: TCar;',
  16385. 'begin',
  16386. ' obj.intern:=obj.intern+1;',
  16387. ' obj.intern2:=obj.intern2+2;',
  16388. ' obj.Bracket:=obj.Bracket+3;',
  16389. ' with obj do begin',
  16390. ' intern:=intern+1;',
  16391. ' intern2:=intern2+2;',
  16392. ' Bracket:=Bracket+3;',
  16393. ' end;']);
  16394. ConvertUnit;
  16395. CheckSource('TestClass_ExternalVar',
  16396. LinesToStr([
  16397. 'var $impl = $mod.$impl;',
  16398. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  16399. ' this.DoIt = function () {',
  16400. ' this.$Intern = this.$Intern + 1;',
  16401. ' this.$Intern2 = this.$Intern2 + 2;',
  16402. ' this["A B"] = this["A B"] + 3;',
  16403. ' };',
  16404. ' });',
  16405. '']),
  16406. LinesToStr([
  16407. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  16408. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  16409. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  16410. 'var $with = $impl.Obj;',
  16411. '$with.$Intern = $with.$Intern + 1;',
  16412. '$with.$Intern2 = $with.$Intern2 + 2;',
  16413. '$with["A B"] = $with["A B"] + 3;',
  16414. '']),
  16415. LinesToStr([ // implementation
  16416. '$impl.Obj = null;',
  16417. '']));
  16418. end;
  16419. procedure TTestModule.TestClass_Const;
  16420. begin
  16421. StartProgram(false);
  16422. Add([
  16423. 'type',
  16424. ' integer = longint;',
  16425. ' TClass = class of TObject;',
  16426. ' TObject = class',
  16427. ' public',
  16428. ' const cI: integer = 3;',
  16429. ' procedure DoIt;',
  16430. ' class procedure DoMore;',
  16431. ' end;',
  16432. 'procedure tobject.doit;',
  16433. 'begin',
  16434. ' if cI=4 then;',
  16435. ' if 5=cI then;',
  16436. ' if Self.cI=6 then;',
  16437. ' if 7=Self.cI then;',
  16438. ' with Self do begin',
  16439. ' if cI=11 then;',
  16440. ' if 12=cI then;',
  16441. ' end;',
  16442. 'end;',
  16443. 'class procedure tobject.domore;',
  16444. 'begin',
  16445. ' if cI=8 then;',
  16446. ' if Self.cI=9 then;',
  16447. ' if 10=cI then;',
  16448. ' if 11=Self.cI then;',
  16449. ' with Self do begin',
  16450. ' if cI=13 then;',
  16451. ' if 14=cI then;',
  16452. ' end;',
  16453. 'end;',
  16454. 'var',
  16455. ' Obj: TObject;',
  16456. ' Cla: TClass;',
  16457. 'begin',
  16458. ' if TObject.cI=21 then ;',
  16459. ' if Obj.cI=22 then ;',
  16460. ' if Cla.cI=23 then ;',
  16461. ' with obj do if ci=24 then;',
  16462. ' with TObject do if ci=25 then;',
  16463. ' with Cla do if ci=26 then;']);
  16464. ConvertProgram;
  16465. CheckSource('TestClass_Const',
  16466. LinesToStr([
  16467. 'rtl.createClass(this, "TObject", null, function () {',
  16468. ' this.cI = 3;',
  16469. ' this.$init = function () {',
  16470. ' };',
  16471. ' this.$final = function () {',
  16472. ' };',
  16473. ' this.DoIt = function () {',
  16474. ' if (this.cI === 4) ;',
  16475. ' if (5 === this.cI) ;',
  16476. ' if (this.cI === 6) ;',
  16477. ' if (7 === this.cI) ;',
  16478. ' if (this.cI === 11) ;',
  16479. ' if (12 === this.cI) ;',
  16480. ' };',
  16481. ' this.DoMore = function () {',
  16482. ' if (this.cI === 8) ;',
  16483. ' if (this.cI === 9) ;',
  16484. ' if (10 === this.cI) ;',
  16485. ' if (11 === this.cI) ;',
  16486. ' if (this.cI === 13) ;',
  16487. ' if (14 === this.cI) ;',
  16488. ' };',
  16489. '});',
  16490. 'this.Obj = null;',
  16491. 'this.Cla = null;',
  16492. '']),
  16493. LinesToStr([
  16494. 'if ($mod.TObject.cI === 21) ;',
  16495. 'if ($mod.Obj.cI === 22) ;',
  16496. 'if ($mod.Cla.cI === 23) ;',
  16497. 'var $with = $mod.Obj;',
  16498. 'if ($with.cI === 24) ;',
  16499. 'var $with1 = $mod.TObject;',
  16500. 'if ($with1.cI === 25) ;',
  16501. 'var $with2 = $mod.Cla;',
  16502. 'if ($with2.cI === 26) ;',
  16503. '']));
  16504. end;
  16505. procedure TTestModule.TestClass_ConstEnum;
  16506. begin
  16507. StartProgram(false);
  16508. Add([
  16509. 'type',
  16510. ' TEnum = (red,blue);',
  16511. ' TObject = class',
  16512. ' end;',
  16513. ' TAnimal = class',
  16514. ' public',
  16515. ' type TSubEnum = (light,dark);',
  16516. ' const a = high(TEnum);',
  16517. ' const b = high(TSubEnum);',
  16518. ' end;',
  16519. ' TBird = class(TAnimal)',
  16520. ' public',
  16521. ' const c = high(TEnum);',
  16522. ' const d = high(TSubEnum);',
  16523. ' end;',
  16524. ' TAnt = class',
  16525. ' public',
  16526. ' const e = high(TEnum);',
  16527. ' const f = high(TBird.TSubEnum);',
  16528. ' end;',
  16529. 'begin',
  16530. '']);
  16531. ConvertProgram;
  16532. CheckSource('TestClass_ConstEnum',
  16533. LinesToStr([
  16534. 'this.TEnum = {',
  16535. ' "0": "red",',
  16536. ' red: 0,',
  16537. ' "1": "blue",',
  16538. ' blue: 1',
  16539. '};',
  16540. 'rtl.createClass(this, "TObject", null, function () {',
  16541. ' this.$init = function () {',
  16542. ' };',
  16543. ' this.$final = function () {',
  16544. ' };',
  16545. '});',
  16546. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  16547. ' this.TSubEnum = {',
  16548. ' "0": "light",',
  16549. ' light: 0,',
  16550. ' "1": "dark",',
  16551. ' dark: 1',
  16552. ' };',
  16553. ' this.a = $mod.TEnum.blue;',
  16554. ' this.b = this.TSubEnum.dark;',
  16555. '});',
  16556. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  16557. ' this.c = $mod.TEnum.blue;',
  16558. ' this.d = this.TSubEnum.dark;',
  16559. '});',
  16560. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  16561. ' this.e = $mod.TEnum.blue;',
  16562. ' this.f = $mod.TAnimal.TSubEnum.dark;',
  16563. '});',
  16564. '']),
  16565. LinesToStr([
  16566. '']));
  16567. end;
  16568. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  16569. begin
  16570. StartProgram(false);
  16571. Add([
  16572. 'type',
  16573. ' TObject = class',
  16574. ' const cI: longint = 3;',
  16575. ' procedure Fly;',
  16576. ' procedure Run;',
  16577. ' end;',
  16578. ' TBird = class',
  16579. ' procedure Go;',
  16580. ' end;',
  16581. 'procedure tobject.fly;',
  16582. 'const cI: word = 4;',
  16583. 'begin',
  16584. ' if cI=Self.cI then ;',
  16585. 'end;',
  16586. 'procedure tobject.run;',
  16587. 'const cI: word = 5;',
  16588. 'begin',
  16589. ' if cI=Self.cI then ;',
  16590. 'end;',
  16591. 'procedure tbird.go;',
  16592. 'const cI: word = 6;',
  16593. 'begin',
  16594. ' if cI=Self.cI then ;',
  16595. 'end;',
  16596. 'begin',
  16597. '']);
  16598. ConvertProgram;
  16599. CheckSource('TestClass_LocalConstDuplicate_Prg',
  16600. LinesToStr([
  16601. 'rtl.createClass(this, "TObject", null, function () {',
  16602. ' this.cI = 3;',
  16603. ' this.$init = function () {',
  16604. ' };',
  16605. ' this.$final = function () {',
  16606. ' };',
  16607. ' var cI$1 = 4;',
  16608. ' this.Fly = function () {',
  16609. ' if (cI$1 === this.cI) ;',
  16610. ' };',
  16611. ' var cI$2 = 5;',
  16612. ' this.Run = function () {',
  16613. ' if (cI$2 === this.cI) ;',
  16614. ' };',
  16615. '});',
  16616. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16617. ' var cI$3 = 6;',
  16618. ' this.Go = function () {',
  16619. ' if (cI$3 === this.cI) ;',
  16620. ' };',
  16621. '});',
  16622. '']),
  16623. LinesToStr([
  16624. '']));
  16625. end;
  16626. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  16627. begin
  16628. StartUnit(false);
  16629. Add([
  16630. 'interface',
  16631. 'type',
  16632. ' TObject = class',
  16633. ' const cI: longint = 3;',
  16634. ' procedure Fly;',
  16635. ' procedure Run;',
  16636. ' end;',
  16637. ' TBird = class',
  16638. ' procedure Go;',
  16639. ' end;',
  16640. 'implementation',
  16641. 'procedure tobject.fly;',
  16642. 'const cI: word = 4;',
  16643. 'begin',
  16644. ' if cI=Self.cI then ;',
  16645. 'end;',
  16646. 'procedure tobject.run;',
  16647. 'const cI: word = 5;',
  16648. 'begin',
  16649. ' if cI=Self.cI then ;',
  16650. 'end;',
  16651. 'procedure tbird.go;',
  16652. 'const cI: word = 6;',
  16653. 'begin',
  16654. ' if cI=Self.cI then ;',
  16655. 'end;',
  16656. '']);
  16657. ConvertUnit;
  16658. CheckSource('TestClass_LocalConstDuplicate_Unit',
  16659. LinesToStr([
  16660. 'rtl.createClass(this, "TObject", null, function () {',
  16661. ' this.cI = 3;',
  16662. ' this.$init = function () {',
  16663. ' };',
  16664. ' this.$final = function () {',
  16665. ' };',
  16666. ' var cI$1 = 4;',
  16667. ' this.Fly = function () {',
  16668. ' if (cI$1 === this.cI) ;',
  16669. ' };',
  16670. ' var cI$2 = 5;',
  16671. ' this.Run = function () {',
  16672. ' if (cI$2 === this.cI) ;',
  16673. ' };',
  16674. '});',
  16675. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16676. ' var cI$3 = 6;',
  16677. ' this.Go = function () {',
  16678. ' if (cI$3 === this.cI) ;',
  16679. ' };',
  16680. '});',
  16681. '']),
  16682. '',
  16683. '');
  16684. end;
  16685. procedure TTestModule.TestClass_LocalVarSelfFail;
  16686. begin
  16687. StartProgram(false);
  16688. Add([
  16689. 'type',
  16690. ' TObject = class',
  16691. ' constructor Create;',
  16692. ' end;',
  16693. 'constructor tobject.create;',
  16694. 'var self: longint;',
  16695. 'begin',
  16696. 'end',
  16697. 'begin',
  16698. '']);
  16699. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  16700. ConvertProgram;
  16701. end;
  16702. procedure TTestModule.TestClass_ArgSelfFail;
  16703. begin
  16704. StartProgram(false);
  16705. Add([
  16706. 'type',
  16707. ' TObject = class',
  16708. ' procedure DoIt(Self: longint);',
  16709. ' end;',
  16710. 'procedure tobject.doit(self: longint);',
  16711. 'begin',
  16712. 'end',
  16713. 'begin',
  16714. '']);
  16715. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  16716. ConvertProgram;
  16717. end;
  16718. procedure TTestModule.TestClass_NestedProcSelf;
  16719. begin
  16720. StartProgram(false);
  16721. Add([
  16722. 'type',
  16723. ' TObject = class',
  16724. ' Key: longint;',
  16725. ' class var State: longint;',
  16726. ' procedure DoIt;',
  16727. ' function GetSize: longint; virtual; abstract;',
  16728. ' procedure SetSize(Value: longint); virtual; abstract;',
  16729. ' property Size: longint read GetSize write SetSize;',
  16730. ' end;',
  16731. 'procedure tobject.doit;',
  16732. ' procedure Sub;',
  16733. ' begin',
  16734. ' key:=key+2;',
  16735. ' self.key:=self.key+3;',
  16736. ' state:=state+4;',
  16737. ' self.state:=self.state+5;',
  16738. ' tobject.state:=tobject.state+6;',
  16739. ' size:=size+7;',
  16740. ' self.size:=self.size+8;',
  16741. ' end;',
  16742. 'begin',
  16743. ' sub;',
  16744. ' key:=key+12;',
  16745. ' self.key:=self.key+13;',
  16746. ' state:=state+14;',
  16747. ' self.state:=self.state+15;',
  16748. ' tobject.state:=tobject.state+16;',
  16749. ' size:=size+17;',
  16750. ' self.size:=self.size+18;',
  16751. 'end;',
  16752. 'begin',
  16753. '']);
  16754. ConvertProgram;
  16755. CheckSource('TestClass_NestedProcSelf',
  16756. LinesToStr([ // statements
  16757. 'rtl.createClass(this, "TObject", null, function () {',
  16758. ' this.State = 0;',
  16759. ' this.$init = function () {',
  16760. ' this.Key = 0;',
  16761. ' };',
  16762. ' this.$final = function () {',
  16763. ' };',
  16764. ' this.DoIt = function () {',
  16765. ' var $Self = this;',
  16766. ' function Sub() {',
  16767. ' $Self.Key = $Self.Key + 2;',
  16768. ' $Self.Key = $Self.Key + 3;',
  16769. ' $mod.TObject.State = $Self.State + 4;',
  16770. ' $mod.TObject.State = $Self.State + 5;',
  16771. ' $mod.TObject.State = $mod.TObject.State + 6;',
  16772. ' $Self.SetSize($Self.GetSize() + 7);',
  16773. ' $Self.SetSize($Self.GetSize() + 8);',
  16774. ' };',
  16775. ' Sub();',
  16776. ' this.Key = this.Key + 12;',
  16777. ' $Self.Key = $Self.Key + 13;',
  16778. ' $mod.TObject.State = this.State + 14;',
  16779. ' $mod.TObject.State = $Self.State + 15;',
  16780. ' $mod.TObject.State = $mod.TObject.State + 16;',
  16781. ' this.SetSize(this.GetSize() + 17);',
  16782. ' $Self.SetSize($Self.GetSize() + 18);',
  16783. ' };',
  16784. '});',
  16785. '']),
  16786. LinesToStr([ // $mod.$main
  16787. '']));
  16788. end;
  16789. procedure TTestModule.TestClass_NestedProcSelf2;
  16790. begin
  16791. StartProgram(false);
  16792. Add([
  16793. 'type',
  16794. ' TObject = class',
  16795. ' Key: longint;',
  16796. ' class var State: longint;',
  16797. ' function GetSize: longint; virtual; abstract;',
  16798. ' procedure SetSize(Value: longint); virtual; abstract;',
  16799. ' property Size: longint read GetSize write SetSize;',
  16800. ' end;',
  16801. ' TBird = class',
  16802. ' procedure DoIt;',
  16803. ' end;',
  16804. 'procedure tbird.doit;',
  16805. ' procedure Sub;',
  16806. ' begin',
  16807. ' key:=key+2;',
  16808. ' self.key:=self.key+3;',
  16809. ' state:=state+4;',
  16810. ' self.state:=self.state+5;',
  16811. ' tobject.state:=tobject.state+6;',
  16812. ' size:=size+7;',
  16813. ' self.size:=self.size+8;',
  16814. ' end;',
  16815. 'begin',
  16816. ' sub;',
  16817. ' key:=key+12;',
  16818. ' self.key:=self.key+13;',
  16819. ' state:=state+14;',
  16820. ' self.state:=self.state+15;',
  16821. ' tobject.state:=tobject.state+16;',
  16822. ' size:=size+17;',
  16823. ' self.size:=self.size+18;',
  16824. 'end;',
  16825. 'begin',
  16826. '']);
  16827. ConvertProgram;
  16828. CheckSource('TestClass_NestedProcSelf2',
  16829. LinesToStr([ // statements
  16830. 'rtl.createClass(this, "TObject", null, function () {',
  16831. ' this.State = 0;',
  16832. ' this.$init = function () {',
  16833. ' this.Key = 0;',
  16834. ' };',
  16835. ' this.$final = function () {',
  16836. ' };',
  16837. '});',
  16838. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16839. ' this.DoIt = function () {',
  16840. ' var $Self = this;',
  16841. ' function Sub() {',
  16842. ' $Self.Key = $Self.Key + 2;',
  16843. ' $Self.Key = $Self.Key + 3;',
  16844. ' $mod.TObject.State = $Self.State + 4;',
  16845. ' $mod.TObject.State = $Self.State + 5;',
  16846. ' $mod.TObject.State = $mod.TObject.State + 6;',
  16847. ' $Self.SetSize($Self.GetSize() + 7);',
  16848. ' $Self.SetSize($Self.GetSize() + 8);',
  16849. ' };',
  16850. ' Sub();',
  16851. ' this.Key = this.Key + 12;',
  16852. ' $Self.Key = $Self.Key + 13;',
  16853. ' $mod.TObject.State = this.State + 14;',
  16854. ' $mod.TObject.State = $Self.State + 15;',
  16855. ' $mod.TObject.State = $mod.TObject.State + 16;',
  16856. ' this.SetSize(this.GetSize() + 17);',
  16857. ' $Self.SetSize($Self.GetSize() + 18);',
  16858. ' };',
  16859. '});',
  16860. '']),
  16861. LinesToStr([ // $mod.$main
  16862. '']));
  16863. end;
  16864. procedure TTestModule.TestClass_NestedProcClassSelf;
  16865. begin
  16866. StartProgram(false);
  16867. Add([
  16868. 'type',
  16869. ' TObject = class',
  16870. ' class var State: longint;',
  16871. ' class procedure DoIt;',
  16872. ' class function GetSize: longint; virtual; abstract;',
  16873. ' class procedure SetSize(Value: longint); virtual; abstract;',
  16874. ' class property Size: longint read GetSize write SetSize;',
  16875. ' end;',
  16876. 'class procedure tobject.doit;',
  16877. ' procedure Sub;',
  16878. ' begin',
  16879. ' state:=state+2;',
  16880. ' self.state:=self.state+3;',
  16881. ' tobject.state:=tobject.state+4;',
  16882. ' size:=size+5;',
  16883. ' self.size:=self.size+6;',
  16884. ' tobject.size:=tobject.size+7;',
  16885. ' end;',
  16886. 'begin',
  16887. ' sub;',
  16888. ' state:=state+12;',
  16889. ' self.state:=self.state+13;',
  16890. ' tobject.state:=tobject.state+14;',
  16891. ' size:=size+15;',
  16892. ' self.size:=self.size+16;',
  16893. ' tobject.size:=tobject.size+17;',
  16894. 'end;',
  16895. 'begin',
  16896. '']);
  16897. ConvertProgram;
  16898. CheckSource('TestClass_NestedProcClassSelf',
  16899. LinesToStr([ // statements
  16900. 'rtl.createClass(this, "TObject", null, function () {',
  16901. ' this.State = 0;',
  16902. ' this.$init = function () {',
  16903. ' };',
  16904. ' this.$final = function () {',
  16905. ' };',
  16906. ' this.DoIt = function () {',
  16907. ' var $Self = this;',
  16908. ' function Sub() {',
  16909. ' $mod.TObject.State = $Self.State + 2;',
  16910. ' $mod.TObject.State = $Self.State + 3;',
  16911. ' $mod.TObject.State = $mod.TObject.State + 4;',
  16912. ' $Self.SetSize($Self.GetSize() + 5);',
  16913. ' $Self.SetSize($Self.GetSize() + 6);',
  16914. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  16915. ' };',
  16916. ' Sub();',
  16917. ' $mod.TObject.State = this.State + 12;',
  16918. ' $mod.TObject.State = $Self.State + 13;',
  16919. ' $mod.TObject.State = $mod.TObject.State + 14;',
  16920. ' this.SetSize(this.GetSize() + 15);',
  16921. ' $Self.SetSize($Self.GetSize() + 16);',
  16922. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  16923. ' };',
  16924. '});',
  16925. '']),
  16926. LinesToStr([ // $mod.$main
  16927. '']));
  16928. end;
  16929. procedure TTestModule.TestClass_NestedProcCallInherited;
  16930. begin
  16931. StartProgram(false);
  16932. Add([
  16933. 'type',
  16934. ' TObject = class',
  16935. ' function DoIt(k: boolean): longint; virtual;',
  16936. ' end;',
  16937. ' TBird = class',
  16938. ' function DoIt(k: boolean): longint; override;',
  16939. ' end;',
  16940. 'function tobject.doit(k: boolean): longint;',
  16941. 'begin',
  16942. 'end;',
  16943. 'function tbird.doit(k: boolean): longint;',
  16944. ' procedure Sub;',
  16945. ' begin',
  16946. ' inherited DoIt(true);',
  16947. //' if inherited DoIt(false)=4 then ;',
  16948. ' end;',
  16949. 'begin',
  16950. ' Sub;',
  16951. ' inherited;',
  16952. ' inherited DoIt(true);',
  16953. //' if inherited DoIt(false)=14 then ;',
  16954. 'end;',
  16955. 'begin',
  16956. '']);
  16957. ConvertProgram;
  16958. CheckSource('TestClass_NestedProcCallInherited',
  16959. LinesToStr([ // statements
  16960. 'rtl.createClass(this, "TObject", null, function () {',
  16961. ' this.$init = function () {',
  16962. ' };',
  16963. ' this.$final = function () {',
  16964. ' };',
  16965. ' this.DoIt = function (k) {',
  16966. ' var Result = 0;',
  16967. ' return Result;',
  16968. ' };',
  16969. '});',
  16970. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16971. ' this.DoIt = function (k) {',
  16972. ' var $Self = this;',
  16973. ' var Result = 0;',
  16974. ' function Sub() {',
  16975. ' $mod.TObject.DoIt.call($Self, true);',
  16976. ' };',
  16977. ' Sub();',
  16978. ' $mod.TObject.DoIt.apply(this, arguments);',
  16979. ' $mod.TObject.DoIt.call(this, true);',
  16980. ' return Result;',
  16981. ' };',
  16982. '});',
  16983. '']),
  16984. LinesToStr([ // $mod.$main
  16985. '']));
  16986. end;
  16987. procedure TTestModule.TestClass_TObjectFree;
  16988. begin
  16989. StartProgram(false);
  16990. Add([
  16991. 'type',
  16992. ' TObject = class',
  16993. ' Obj: tobject;',
  16994. ' procedure Free;',
  16995. ' procedure Release;',
  16996. ' end;',
  16997. 'procedure tobject.free;',
  16998. 'begin',
  16999. 'end;',
  17000. 'procedure tobject.release;',
  17001. 'begin',
  17002. ' free;',
  17003. ' if true then free;',
  17004. 'end;',
  17005. 'function DoIt(o: tobject): tobject;',
  17006. 'var l: tobject;',
  17007. 'begin',
  17008. ' o.free;',
  17009. ' o.free();',
  17010. ' l.free;',
  17011. ' l.free();',
  17012. ' o.obj.free;',
  17013. ' o.obj.free();',
  17014. ' with o do obj.free;',
  17015. ' with o do obj.free();',
  17016. ' result.Free;',
  17017. ' result.Free();',
  17018. 'end;',
  17019. 'var o: tobject;',
  17020. ' a: array of tobject;',
  17021. 'begin',
  17022. ' o.free;',
  17023. ' o.obj.free;',
  17024. ' a[1+2].free;',
  17025. '']);
  17026. ConvertProgram;
  17027. CheckSource('TestClass_TObjectFree',
  17028. LinesToStr([ // statements
  17029. 'rtl.createClass(this, "TObject", null, function () {',
  17030. ' this.$init = function () {',
  17031. ' this.Obj = null;',
  17032. ' };',
  17033. ' this.$final = function () {',
  17034. ' this.Obj = undefined;',
  17035. ' };',
  17036. ' this.Free = function () {',
  17037. ' };',
  17038. ' this.Release = function () {',
  17039. ' this.Free();',
  17040. ' if (true) this.Free();',
  17041. ' };',
  17042. '});',
  17043. 'this.DoIt = function (o) {',
  17044. ' var Result = null;',
  17045. ' var l = null;',
  17046. ' o = rtl.freeLoc(o);',
  17047. ' o = rtl.freeLoc(o);',
  17048. ' l = rtl.freeLoc(l);',
  17049. ' l = rtl.freeLoc(l);',
  17050. ' rtl.free(o, "Obj");',
  17051. ' rtl.free(o, "Obj");',
  17052. ' rtl.free(o, "Obj");',
  17053. ' rtl.free(o, "Obj");',
  17054. ' Result = rtl.freeLoc(Result);',
  17055. ' Result = rtl.freeLoc(Result);',
  17056. ' return Result;',
  17057. '};',
  17058. 'this.o = null;',
  17059. 'this.a = [];',
  17060. '']),
  17061. LinesToStr([ // $mod.$main
  17062. 'rtl.free($mod, "o");',
  17063. 'rtl.free($mod.o, "Obj");',
  17064. 'rtl.free($mod.a, 1 + 2);',
  17065. '']));
  17066. end;
  17067. procedure TTestModule.TestClass_TObjectFree_VarArg;
  17068. begin
  17069. StartProgram(false);
  17070. Add([
  17071. 'type',
  17072. ' TObject = class',
  17073. ' Obj: tobject;',
  17074. ' procedure Free;',
  17075. ' end;',
  17076. 'procedure tobject.free;',
  17077. 'begin',
  17078. 'end;',
  17079. 'procedure DoIt(var o: tobject);',
  17080. 'begin',
  17081. ' o.free;',
  17082. ' o.free();',
  17083. 'end;',
  17084. 'begin',
  17085. '']);
  17086. ConvertProgram;
  17087. CheckSource('TestClass_TObjectFree_VarArg',
  17088. LinesToStr([ // statements
  17089. 'rtl.createClass(this, "TObject", null, function () {',
  17090. ' this.$init = function () {',
  17091. ' this.Obj = null;',
  17092. ' };',
  17093. ' this.$final = function () {',
  17094. ' this.Obj = undefined;',
  17095. ' };',
  17096. ' this.Free = function () {',
  17097. ' };',
  17098. '});',
  17099. 'this.DoIt = function (o) {',
  17100. ' o.set(rtl.freeLoc(o.get()));',
  17101. ' o.set(rtl.freeLoc(o.get()));',
  17102. '};',
  17103. '']),
  17104. LinesToStr([ // $mod.$main
  17105. '']));
  17106. end;
  17107. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  17108. begin
  17109. StartProgram(false);
  17110. Add([
  17111. 'type',
  17112. ' TObject = class',
  17113. ' constructor Create;',
  17114. ' procedure Free;',
  17115. ' end;',
  17116. 'constructor TObject.Create; begin end;',
  17117. 'procedure tobject.free; begin end;',
  17118. 'begin',
  17119. ' with tobject.create do free;',
  17120. '']);
  17121. ConvertProgram;
  17122. CheckSource('TestClass_TObjectFreeNewInstance',
  17123. LinesToStr([ // statements
  17124. 'rtl.createClass(this, "TObject", null, function () {',
  17125. ' this.$init = function () {',
  17126. ' };',
  17127. ' this.$final = function () {',
  17128. ' };',
  17129. ' this.Create = function () {',
  17130. ' return this;',
  17131. ' };',
  17132. ' this.Free = function () {',
  17133. ' };',
  17134. '});',
  17135. '']),
  17136. LinesToStr([ // $mod.$main
  17137. 'var $with = $mod.TObject.$create("Create");',
  17138. '$with=rtl.freeLoc($with);',
  17139. '']));
  17140. end;
  17141. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  17142. begin
  17143. StartProgram(false);
  17144. Add([
  17145. 'type',
  17146. ' TObject = class',
  17147. ' destructor Destroy;',
  17148. ' procedure Free;',
  17149. ' end;',
  17150. 'destructor TObject.Destroy; begin end;',
  17151. 'procedure tobject.free; begin end;',
  17152. 'var o: tobject;',
  17153. 'begin',
  17154. ' o.free;',
  17155. '']);
  17156. Converter.UseLowerCase:=true;
  17157. ConvertProgram;
  17158. CheckSource('TestClass_TObjectFreeLowerCase',
  17159. LinesToStr([ // statements
  17160. 'rtl.createClass(this, "tobject", null, function () {',
  17161. ' this.$init = function () {',
  17162. ' };',
  17163. ' this.$final = function () {',
  17164. ' };',
  17165. ' rtl.tObjectDestroy = "destroy";',
  17166. ' this.destroy = function () {',
  17167. ' };',
  17168. ' this.free = function () {',
  17169. ' };',
  17170. '});',
  17171. 'this.o = null;',
  17172. '']),
  17173. LinesToStr([ // $mod.$main
  17174. 'rtl.free($mod, "o");',
  17175. '']));
  17176. end;
  17177. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  17178. begin
  17179. StartProgram(false);
  17180. Add([
  17181. 'type',
  17182. ' TObject = class',
  17183. ' procedure Free;',
  17184. ' function GetObj: tobject; virtual; abstract;',
  17185. ' end;',
  17186. 'procedure tobject.free;',
  17187. 'begin',
  17188. 'end;',
  17189. 'var o: tobject;',
  17190. 'begin',
  17191. ' o.getobj.free;',
  17192. '']);
  17193. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  17194. ConvertProgram;
  17195. end;
  17196. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  17197. begin
  17198. StartProgram(false);
  17199. Add([
  17200. 'type',
  17201. ' TObject = class',
  17202. ' procedure Free;',
  17203. ' FObj: TObject;',
  17204. ' property Obj: tobject read FObj write FObj;',
  17205. ' end;',
  17206. 'procedure tobject.free;',
  17207. 'begin',
  17208. 'end;',
  17209. 'var o: tobject;',
  17210. 'begin',
  17211. ' o.obj.free;',
  17212. '']);
  17213. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  17214. ConvertProgram;
  17215. end;
  17216. procedure TTestModule.TestClass_ForIn;
  17217. begin
  17218. StartProgram(false);
  17219. Add([
  17220. 'type',
  17221. ' TObject = class end;',
  17222. ' TItem = TObject;',
  17223. ' TEnumerator = class',
  17224. ' FCurrent: TItem;',
  17225. ' property Current: TItem read FCurrent;',
  17226. ' function MoveNext: boolean;',
  17227. ' end;',
  17228. ' TBird = class',
  17229. ' function GetEnumerator: TEnumerator;',
  17230. ' end;',
  17231. 'function TEnumerator.MoveNext: boolean;',
  17232. 'begin',
  17233. 'end;',
  17234. 'function TBird.GetEnumerator: TEnumerator;',
  17235. 'begin',
  17236. 'end;',
  17237. 'var',
  17238. ' b: TBird;',
  17239. ' i, i2: TItem;',
  17240. 'begin',
  17241. ' for i in b do i2:=i;']);
  17242. ConvertProgram;
  17243. CheckSource('TestClass_ForIn',
  17244. LinesToStr([ // statements
  17245. 'rtl.createClass(this, "TObject", null, function () {',
  17246. ' this.$init = function () {',
  17247. ' };',
  17248. ' this.$final = function () {',
  17249. ' };',
  17250. '});',
  17251. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  17252. ' this.$init = function () {',
  17253. ' $mod.TObject.$init.call(this);',
  17254. ' this.FCurrent = null;',
  17255. ' };',
  17256. ' this.$final = function () {',
  17257. ' this.FCurrent = undefined;',
  17258. ' $mod.TObject.$final.call(this);',
  17259. ' };',
  17260. ' this.MoveNext = function () {',
  17261. ' var Result = false;',
  17262. ' return Result;',
  17263. ' };',
  17264. '});',
  17265. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17266. ' this.GetEnumerator = function () {',
  17267. ' var Result = null;',
  17268. ' return Result;',
  17269. ' };',
  17270. '});',
  17271. 'this.b = null;',
  17272. 'this.i = null;',
  17273. 'this.i2 = null;'
  17274. ]),
  17275. LinesToStr([ // $mod.$main
  17276. 'var $in = $mod.b.GetEnumerator();',
  17277. 'try {',
  17278. ' while ($in.MoveNext()){',
  17279. ' $mod.i = $in.FCurrent;',
  17280. ' $mod.i2 = $mod.i;',
  17281. ' }',
  17282. '} finally {',
  17283. ' $in = rtl.freeLoc($in)',
  17284. '};',
  17285. '']));
  17286. end;
  17287. procedure TTestModule.TestClass_DispatchMessage;
  17288. begin
  17289. StartProgram(false);
  17290. Add([
  17291. 'type',
  17292. ' {$DispatchField DispInt}',
  17293. ' {$DispatchStrField DispStr}',
  17294. ' TObject = class',
  17295. ' procedure Dispatch(var Msg); virtual; abstract;',
  17296. ' procedure DispatchStr(var Msg); virtual; abstract;',
  17297. ' end;',
  17298. ' THopMsg = record',
  17299. ' DispInt: longint;',
  17300. ' end;',
  17301. ' TPutMsg = record',
  17302. ' DispStr: string;',
  17303. ' end;',
  17304. ' TBird = class',
  17305. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  17306. ' procedure Run; overload; virtual; abstract;',
  17307. ' procedure Run(var Msg); overload; message ''Fast'';',
  17308. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  17309. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  17310. ' end;',
  17311. 'procedure TBird.Run(var Msg);',
  17312. 'begin',
  17313. 'end;',
  17314. 'begin',
  17315. '']);
  17316. ConvertProgram;
  17317. CheckResolverUnexpectedHints(true);
  17318. CheckSource('TestClass_Message',
  17319. LinesToStr([ // statements
  17320. 'rtl.createClass(this, "TObject", null, function () {',
  17321. ' this.$init = function () {',
  17322. ' };',
  17323. ' this.$final = function () {',
  17324. ' };',
  17325. '});',
  17326. 'rtl.recNewT(this, "THopMsg", function () {',
  17327. ' this.DispInt = 0;',
  17328. ' this.$eq = function (b) {',
  17329. ' return this.DispInt === b.DispInt;',
  17330. ' };',
  17331. ' this.$assign = function (s) {',
  17332. ' this.DispInt = s.DispInt;',
  17333. ' return this;',
  17334. ' };',
  17335. '});',
  17336. 'rtl.recNewT(this, "TPutMsg", function () {',
  17337. ' this.DispStr = "";',
  17338. ' this.$eq = function (b) {',
  17339. ' return this.DispStr === b.DispStr;',
  17340. ' };',
  17341. ' this.$assign = function (s) {',
  17342. ' this.DispStr = s.DispStr;',
  17343. ' return this;',
  17344. ' };',
  17345. '});',
  17346. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17347. ' this.Run$1 = function (Msg) {',
  17348. ' };',
  17349. ' this.$msgint = {',
  17350. ' "2": "Fly",',
  17351. ' "3": "Hop"',
  17352. ' };',
  17353. ' this.$msgstr = {',
  17354. ' Fast: "Run$1",',
  17355. ' foo: "Put"',
  17356. ' };',
  17357. '});',
  17358. '']),
  17359. LinesToStr([ // $mod.$main
  17360. '']));
  17361. end;
  17362. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  17363. begin
  17364. StartProgram(false);
  17365. Add([
  17366. 'type',
  17367. ' TObject = class',
  17368. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  17369. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  17370. ' end;',
  17371. 'begin',
  17372. '']);
  17373. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  17374. ConvertProgram;
  17375. end;
  17376. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  17377. begin
  17378. StartProgram(false);
  17379. Add([
  17380. 'type',
  17381. ' TObject = class',
  17382. ' procedure Dispatch(var Msg); virtual; abstract;',
  17383. ' end;',
  17384. ' TFlyMsg = record',
  17385. ' FlyId: longint;',
  17386. ' end;',
  17387. ' TBird = class',
  17388. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  17389. ' end;',
  17390. 'begin',
  17391. '']);
  17392. ConvertProgram;
  17393. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  17394. end;
  17395. procedure TTestModule.TestClassOf_Create;
  17396. begin
  17397. StartProgram(false);
  17398. Add('type');
  17399. Add(' TObject = class');
  17400. Add(' constructor Create;');
  17401. Add(' end;');
  17402. Add(' TClass = class of TObject;');
  17403. Add('constructor tobject.create; begin end;');
  17404. Add('var');
  17405. Add(' Obj: tobject;');
  17406. Add(' C: tclass;');
  17407. Add('begin');
  17408. Add(' obj:=C.create;');
  17409. Add(' with c do obj:=create;');
  17410. ConvertProgram;
  17411. CheckSource('TestClassOf_Create',
  17412. LinesToStr([ // statements
  17413. 'rtl.createClass(this, "TObject", null, function () {',
  17414. ' this.$init = function () {',
  17415. ' };',
  17416. ' this.$final = function () {',
  17417. ' };',
  17418. ' this.Create = function () {',
  17419. ' return this;',
  17420. ' };',
  17421. '});',
  17422. 'this.Obj = null;',
  17423. 'this.C = null;'
  17424. ]),
  17425. LinesToStr([ // $mod.$main
  17426. '$mod.Obj = $mod.C.$create("Create");',
  17427. 'var $with = $mod.C;',
  17428. '$mod.Obj = $with.$create("Create");',
  17429. '']));
  17430. end;
  17431. procedure TTestModule.TestClassOf_Call;
  17432. begin
  17433. StartProgram(false);
  17434. Add('type');
  17435. Add(' TObject = class');
  17436. Add(' class procedure DoIt;');
  17437. Add(' end;');
  17438. Add(' TClass = class of TObject;');
  17439. Add('class procedure tobject.doit; begin end;');
  17440. Add('var');
  17441. Add(' C: tclass;');
  17442. Add('begin');
  17443. Add(' c.doit;');
  17444. Add(' with c do doit;');
  17445. ConvertProgram;
  17446. CheckSource('TestClassOf_Call',
  17447. LinesToStr([ // statements
  17448. 'rtl.createClass(this, "TObject", null, function () {',
  17449. ' this.$init = function () {',
  17450. ' };',
  17451. ' this.$final = function () {',
  17452. ' };',
  17453. ' this.DoIt = function () {',
  17454. ' };',
  17455. '});',
  17456. 'this.C = null;'
  17457. ]),
  17458. LinesToStr([ // $mod.$main
  17459. '$mod.C.DoIt();',
  17460. 'var $with = $mod.C;',
  17461. '$with.DoIt();',
  17462. '']));
  17463. end;
  17464. procedure TTestModule.TestClassOf_Assign;
  17465. begin
  17466. StartProgram(false);
  17467. Add('type');
  17468. Add(' TClass = class of TObject;');
  17469. Add(' TObject = class');
  17470. Add(' ClassType: TClass; ');
  17471. Add(' end;');
  17472. Add('var');
  17473. Add(' Obj: tobject;');
  17474. Add(' C: tclass;');
  17475. Add('begin');
  17476. Add(' c:=nil;');
  17477. Add(' c:=obj.classtype;');
  17478. ConvertProgram;
  17479. CheckSource('TestClassOf_Assign',
  17480. LinesToStr([ // statements
  17481. 'rtl.createClass(this, "TObject", null, function () {',
  17482. ' this.$init = function () {',
  17483. ' this.ClassType = null;',
  17484. ' };',
  17485. ' this.$final = function () {',
  17486. ' this.ClassType = undefined;',
  17487. ' };',
  17488. '});',
  17489. 'this.Obj = null;',
  17490. 'this.C = null;'
  17491. ]),
  17492. LinesToStr([ // $mod.$main
  17493. '$mod.C = null;',
  17494. '$mod.C = $mod.Obj.ClassType;',
  17495. '']));
  17496. end;
  17497. procedure TTestModule.TestClassOf_Is;
  17498. begin
  17499. StartProgram(false);
  17500. Add('type');
  17501. Add(' TClass = class of TObject;');
  17502. Add(' TObject = class');
  17503. Add(' end;');
  17504. Add(' TCar = class');
  17505. Add(' end;');
  17506. Add(' TCars = class of TCar;');
  17507. Add('var');
  17508. Add(' Obj: tobject;');
  17509. Add(' C: tclass;');
  17510. Add(' Cars: tcars;');
  17511. Add('begin');
  17512. Add(' if c is tcar then ;');
  17513. Add(' if c is tcars then ;');
  17514. ConvertProgram;
  17515. CheckSource('TestClassOf_Is',
  17516. LinesToStr([ // statements
  17517. 'rtl.createClass(this, "TObject", null, function () {',
  17518. ' this.$init = function () {',
  17519. ' };',
  17520. ' this.$final = function () {',
  17521. ' };',
  17522. '});',
  17523. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  17524. '});',
  17525. 'this.Obj = null;',
  17526. 'this.C = null;',
  17527. 'this.Cars = null;'
  17528. ]),
  17529. LinesToStr([ // $mod.$main
  17530. 'if(rtl.is($mod.C,$mod.TCar));',
  17531. 'if(rtl.is($mod.C,$mod.TCar));',
  17532. '']));
  17533. end;
  17534. procedure TTestModule.TestClassOf_Compare;
  17535. begin
  17536. StartProgram(false);
  17537. Add('type');
  17538. Add(' TClass = class of TObject;');
  17539. Add(' TObject = class');
  17540. Add(' ClassType: TClass; ');
  17541. Add(' end;');
  17542. Add('var');
  17543. Add(' b: boolean;');
  17544. Add(' Obj: tobject;');
  17545. Add(' C: tclass;');
  17546. Add('begin');
  17547. Add(' b:=c=nil;');
  17548. Add(' b:=nil=c;');
  17549. Add(' b:=c=obj.classtype;');
  17550. Add(' b:=obj.classtype=c;');
  17551. Add(' b:=c=TObject;');
  17552. Add(' b:=TObject=c;');
  17553. Add(' b:=c<>nil;');
  17554. Add(' b:=nil<>c;');
  17555. Add(' b:=c<>obj.classtype;');
  17556. Add(' b:=obj.classtype<>c;');
  17557. Add(' b:=c<>TObject;');
  17558. Add(' b:=TObject<>c;');
  17559. ConvertProgram;
  17560. CheckSource('TestClassOf_Compare',
  17561. LinesToStr([ // statements
  17562. 'rtl.createClass(this, "TObject", null, function () {',
  17563. ' this.$init = function () {',
  17564. ' this.ClassType = null;',
  17565. ' };',
  17566. ' this.$final = function () {',
  17567. ' this.ClassType = undefined;',
  17568. ' };',
  17569. '});',
  17570. 'this.b = false;',
  17571. 'this.Obj = null;',
  17572. 'this.C = null;'
  17573. ]),
  17574. LinesToStr([ // $mod.$main
  17575. '$mod.b = $mod.C === null;',
  17576. '$mod.b = null === $mod.C;',
  17577. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  17578. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  17579. '$mod.b = $mod.C === $mod.TObject;',
  17580. '$mod.b = $mod.TObject === $mod.C;',
  17581. '$mod.b = $mod.C !== null;',
  17582. '$mod.b = null !== $mod.C;',
  17583. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  17584. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  17585. '$mod.b = $mod.C !== $mod.TObject;',
  17586. '$mod.b = $mod.TObject !== $mod.C;',
  17587. '']));
  17588. end;
  17589. procedure TTestModule.TestClassOf_ClassVar;
  17590. begin
  17591. StartProgram(false);
  17592. Add('type');
  17593. Add(' TObject = class');
  17594. Add(' class var id: longint;');
  17595. Add(' end;');
  17596. Add(' TClass = class of TObject;');
  17597. Add('var');
  17598. Add(' C: tclass;');
  17599. Add('begin');
  17600. Add(' C.id:=C.id;');
  17601. ConvertProgram;
  17602. CheckSource('TestClassOf_ClassVar',
  17603. LinesToStr([ // statements
  17604. 'rtl.createClass(this, "TObject", null, function () {',
  17605. ' this.id = 0;',
  17606. ' this.$init = function () {',
  17607. ' };',
  17608. ' this.$final = function () {',
  17609. ' };',
  17610. '});',
  17611. 'this.C = null;'
  17612. ]),
  17613. LinesToStr([ // $mod.$main
  17614. '$mod.TObject.id = $mod.C.id;',
  17615. '']));
  17616. end;
  17617. procedure TTestModule.TestClassOf_ClassMethod;
  17618. begin
  17619. StartProgram(false);
  17620. Add('type');
  17621. Add(' TObject = class');
  17622. Add(' class function DoIt(i: longint = 0): longint;');
  17623. Add(' end;');
  17624. Add(' TClass = class of TObject;');
  17625. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  17626. Add('var');
  17627. Add(' i: longint;');
  17628. Add(' C: tclass;');
  17629. Add('begin');
  17630. Add(' C.DoIt;');
  17631. Add(' C.DoIt();');
  17632. Add(' i:=C.DoIt;');
  17633. Add(' i:=C.DoIt();');
  17634. ConvertProgram;
  17635. CheckSource('TestClassOf_ClassMethod',
  17636. LinesToStr([ // statements
  17637. 'rtl.createClass(this, "TObject", null, function () {',
  17638. ' this.$init = function () {',
  17639. ' };',
  17640. ' this.$final = function () {',
  17641. ' };',
  17642. ' this.DoIt = function (i) {',
  17643. ' var Result = 0;',
  17644. ' return Result;',
  17645. ' };',
  17646. '});',
  17647. 'this.i = 0;',
  17648. 'this.C = null;'
  17649. ]),
  17650. LinesToStr([ // $mod.$main
  17651. '$mod.C.DoIt(0);',
  17652. '$mod.C.DoIt(0);',
  17653. '$mod.i = $mod.C.DoIt(0);',
  17654. '$mod.i = $mod.C.DoIt(0);',
  17655. '']));
  17656. end;
  17657. procedure TTestModule.TestClassOf_ClassProperty;
  17658. begin
  17659. StartProgram(false);
  17660. Add([
  17661. 'type',
  17662. ' TObject = class',
  17663. ' class var FA: longint;',
  17664. ' class function GetA: longint;',
  17665. ' class procedure SetA(Value: longint);',
  17666. ' class property pA: longint read fa write fa;',
  17667. ' class property pB: longint read geta write seta;',
  17668. ' end;',
  17669. ' TObjectClass = class of tobject;',
  17670. 'class function tobject.geta: longint; begin end;',
  17671. 'class procedure tobject.seta(value: longint); begin end;',
  17672. 'var',
  17673. ' b: boolean;',
  17674. ' Obj: tobject;',
  17675. ' Cla: tobjectclass;',
  17676. 'begin',
  17677. ' obj.pa:=obj.pa;',
  17678. ' obj.pb:=obj.pb;',
  17679. ' b:=obj.pa=4;',
  17680. ' b:=obj.pb=obj.pb;',
  17681. ' b:=5=obj.pa;',
  17682. ' cla.pa:=6;',
  17683. ' cla.pa:=cla.pa;',
  17684. ' cla.pb:=cla.pb;',
  17685. ' b:=cla.pa=7;',
  17686. ' b:=cla.pb=cla.pb;',
  17687. ' b:=8=cla.pa;',
  17688. ' tobject.pa:=9;',
  17689. ' tobject.pb:=tobject.pb;',
  17690. ' b:=tobject.pa=10;',
  17691. ' b:=11=tobject.pa;',
  17692. '']);
  17693. ConvertProgram;
  17694. CheckSource('TestClassOf_ClassProperty',
  17695. LinesToStr([ // statements
  17696. 'rtl.createClass(this, "TObject", null, function () {',
  17697. ' this.FA = 0;',
  17698. ' this.$init = function () {',
  17699. ' };',
  17700. ' this.$final = function () {',
  17701. ' };',
  17702. ' this.GetA = function () {',
  17703. ' var Result = 0;',
  17704. ' return Result;',
  17705. ' };',
  17706. ' this.SetA = function (Value) {',
  17707. ' };',
  17708. '});',
  17709. 'this.b = false;',
  17710. 'this.Obj = null;',
  17711. 'this.Cla = null;'
  17712. ]),
  17713. LinesToStr([ // $mod.$main
  17714. '$mod.TObject.FA = $mod.Obj.FA;',
  17715. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  17716. '$mod.b = $mod.Obj.FA === 4;',
  17717. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  17718. '$mod.b = 5 === $mod.Obj.FA;',
  17719. '$mod.TObject.FA = 6;',
  17720. '$mod.TObject.FA = $mod.Cla.FA;',
  17721. '$mod.Cla.SetA($mod.Cla.GetA());',
  17722. '$mod.b = $mod.Cla.FA === 7;',
  17723. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  17724. '$mod.b = 8 === $mod.Cla.FA;',
  17725. '$mod.TObject.FA = 9;',
  17726. '$mod.TObject.SetA($mod.TObject.GetA());',
  17727. '$mod.b = $mod.TObject.FA === 10;',
  17728. '$mod.b = 11 === $mod.TObject.FA;',
  17729. '']));
  17730. end;
  17731. procedure TTestModule.TestClassOf_ClassMethodSelf;
  17732. begin
  17733. StartProgram(false);
  17734. Add('type');
  17735. Add(' TObject = class');
  17736. Add(' class var GlobalId: longint;');
  17737. Add(' class procedure ProcA;');
  17738. Add(' end;');
  17739. Add('class procedure tobject.proca;');
  17740. Add('var b: boolean;');
  17741. Add('begin');
  17742. Add(' b:=self=nil;');
  17743. Add(' b:=self.globalid=3;');
  17744. Add(' b:=4=self.globalid;');
  17745. Add(' self.globalid:=5;');
  17746. Add(' self.proca;');
  17747. Add('end;');
  17748. Add('begin');
  17749. ConvertProgram;
  17750. CheckSource('TestClassOf_ClassMethodSelf',
  17751. LinesToStr([ // statements
  17752. 'rtl.createClass(this, "TObject", null, function () {',
  17753. ' this.GlobalId = 0;',
  17754. ' this.$init = function () {',
  17755. ' };',
  17756. ' this.$final = function () {',
  17757. ' };',
  17758. ' this.ProcA = function () {',
  17759. ' var b = false;',
  17760. ' b = this === null;',
  17761. ' b = this.GlobalId === 3;',
  17762. ' b = 4 === this.GlobalId;',
  17763. ' $mod.TObject.GlobalId = 5;',
  17764. ' this.ProcA();',
  17765. ' };',
  17766. '});'
  17767. ]),
  17768. LinesToStr([ // $mod.$main
  17769. '']));
  17770. end;
  17771. procedure TTestModule.TestClassOf_TypeCast;
  17772. begin
  17773. StartProgram(false);
  17774. Add('type');
  17775. Add(' TObject = class');
  17776. Add(' class procedure {#TObject_DoIt}DoIt;');
  17777. Add(' end;');
  17778. Add(' TClass = class of TObject;');
  17779. Add(' TMobile = class');
  17780. Add(' class procedure {#TMobile_DoIt}DoIt;');
  17781. Add(' end;');
  17782. Add(' TMobileClass = class of TMobile;');
  17783. Add(' TCar = class(TMobile)');
  17784. Add(' class procedure {#TCar_DoIt}DoIt;');
  17785. Add(' end;');
  17786. Add(' TCarClass = class of TCar;');
  17787. Add('class procedure TObject.DoIt;');
  17788. Add('begin');
  17789. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  17790. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  17791. Add('end;');
  17792. Add('class procedure TMobile.DoIt;');
  17793. Add('begin');
  17794. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  17795. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  17796. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  17797. Add('end;');
  17798. Add('class procedure TCar.DoIt; begin end;');
  17799. Add('var');
  17800. Add(' ObjC: TClass;');
  17801. Add(' MobileC: TMobileClass;');
  17802. Add(' CarC: TCarClass;');
  17803. Add('begin');
  17804. Add(' ObjC.{@TObject_DoIt}DoIt;');
  17805. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  17806. Add(' CarC.{@TCar_DoIt}DoIt;');
  17807. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  17808. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  17809. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  17810. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  17811. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  17812. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  17813. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  17814. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  17815. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  17816. ConvertProgram;
  17817. CheckSource('TestClassOf_TypeCast',
  17818. LinesToStr([ // statements
  17819. 'rtl.createClass(this, "TObject", null, function () {',
  17820. ' this.$init = function () {',
  17821. ' };',
  17822. ' this.$final = function () {',
  17823. ' };',
  17824. ' this.DoIt = function () {',
  17825. ' this.DoIt();',
  17826. ' this.DoIt$1();',
  17827. ' };',
  17828. '});',
  17829. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  17830. ' this.DoIt$1 = function () {',
  17831. ' this.DoIt();',
  17832. ' this.DoIt$1();',
  17833. ' this.DoIt$2();',
  17834. ' };',
  17835. '});',
  17836. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  17837. ' this.DoIt$2 = function () {',
  17838. ' };',
  17839. '});',
  17840. 'this.ObjC = null;',
  17841. 'this.MobileC = null;',
  17842. 'this.CarC = null;',
  17843. '']),
  17844. LinesToStr([ // $mod.$main
  17845. '$mod.ObjC.DoIt();',
  17846. '$mod.MobileC.DoIt$1();',
  17847. '$mod.CarC.DoIt$2();',
  17848. '$mod.ObjC.DoIt();',
  17849. '$mod.ObjC.DoIt$1();',
  17850. '$mod.ObjC.DoIt$2();',
  17851. '$mod.MobileC.DoIt();',
  17852. '$mod.MobileC.DoIt$1();',
  17853. '$mod.MobileC.DoIt$2();',
  17854. '$mod.CarC.DoIt();',
  17855. '$mod.CarC.DoIt$1();',
  17856. '$mod.CarC.DoIt$2();',
  17857. '']));
  17858. end;
  17859. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  17860. begin
  17861. StartProgram(false);
  17862. Add('type');
  17863. Add(' TObject = class');
  17864. Add(' function CurNow: longint; ');
  17865. Add(' class function Now: longint; ');
  17866. Add(' end;');
  17867. Add('function TObject.CurNow: longint; begin end;');
  17868. Add('class function TObject.Now: longint; begin end;');
  17869. Add('var');
  17870. Add(' Obj: tobject;');
  17871. Add(' vI: longint;');
  17872. Add('begin');
  17873. Add(' obj.curnow;');
  17874. Add(' vi:=obj.curnow;');
  17875. Add(' tobject.now;');
  17876. Add(' vi:=tobject.now;');
  17877. ConvertProgram;
  17878. CheckSource('TestClassOf_ImplicitFunctionCall',
  17879. LinesToStr([ // statements
  17880. 'rtl.createClass(this, "TObject", null, function () {',
  17881. ' this.$init = function () {',
  17882. ' };',
  17883. ' this.$final = function () {',
  17884. ' };',
  17885. ' this.CurNow = function () {',
  17886. ' var Result = 0;',
  17887. ' return Result;',
  17888. ' };',
  17889. ' this.Now = function () {',
  17890. ' var Result = 0;',
  17891. ' return Result;',
  17892. ' };',
  17893. '});',
  17894. 'this.Obj = null;',
  17895. 'this.vI = 0;',
  17896. '']),
  17897. LinesToStr([ // $mod.$main
  17898. '$mod.Obj.CurNow();',
  17899. '$mod.vI = $mod.Obj.CurNow();',
  17900. '$mod.TObject.Now();',
  17901. '$mod.vI = $mod.TObject.Now();',
  17902. '']));
  17903. end;
  17904. procedure TTestModule.TestClassOf_Const;
  17905. begin
  17906. StartProgram(false);
  17907. Add([
  17908. 'type',
  17909. ' TObject = class',
  17910. ' end;',
  17911. ' TBird = TObject;',
  17912. ' TBirds = class of TBird;',
  17913. ' TEagles = TBirds;',
  17914. ' THawk = class(TBird);',
  17915. 'const',
  17916. ' Hawk: TEagles = THawk;',
  17917. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  17918. ' TBird,',
  17919. ' THawk',
  17920. ' );',
  17921. 'begin']);
  17922. ConvertProgram;
  17923. CheckSource('TestClassOf_Const',
  17924. LinesToStr([ // statements
  17925. 'rtl.createClass(this, "TObject", null, function () {',
  17926. ' this.$init = function () {',
  17927. ' };',
  17928. ' this.$final = function () {',
  17929. ' };',
  17930. '});',
  17931. 'rtl.createClass(this, "THawk", this.TObject, function () {',
  17932. '});',
  17933. 'this.Hawk = this.THawk;',
  17934. 'this.DefaultBirdClasses = [this.TObject, this.THawk];',
  17935. '']),
  17936. LinesToStr([ // $mod.$main
  17937. '']));
  17938. end;
  17939. procedure TTestModule.TestNestedClass_Alias;
  17940. begin
  17941. WithTypeInfo:=true;
  17942. StartProgram(false);
  17943. Add([
  17944. 'type',
  17945. ' TObject = class',
  17946. ' type TNested = type longint;',
  17947. ' end;',
  17948. 'type TAlias = type tobject.tnested;',
  17949. 'var i: tobject.tnested = 3;',
  17950. 'var j: TAlias = 4;',
  17951. 'begin',
  17952. ' if typeinfo(TAlias)=nil then ;',
  17953. ' if typeinfo(tobject.tnested)=nil then ;',
  17954. '']);
  17955. ConvertProgram;
  17956. CheckSource('TestNestedClass_Alias',
  17957. LinesToStr([ // statements
  17958. 'rtl.createClass(this, "TObject", null, function () {',
  17959. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  17960. ' this.$init = function () {',
  17961. ' };',
  17962. ' this.$final = function () {',
  17963. ' };',
  17964. '});',
  17965. 'this.$rtti.$inherited("TAlias", this.$rtti["TObject.TNested"], {});',
  17966. 'this.i = 3;',
  17967. 'this.j = 4;',
  17968. '']),
  17969. LinesToStr([ // $mod.$main
  17970. 'if ($mod.$rtti["TAlias"] === null) ;',
  17971. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  17972. '']));
  17973. end;
  17974. procedure TTestModule.TestNestedClass_Record;
  17975. begin
  17976. WithTypeInfo:=true;
  17977. StartProgram(false);
  17978. Add([
  17979. 'type',
  17980. ' TObject = class',
  17981. ' type TPoint = record',
  17982. ' x,y: byte;',
  17983. ' end;',
  17984. ' procedure DoIt(t: TPoint);',
  17985. ' end;',
  17986. 'procedure tobject.DoIt(t: TPoint);',
  17987. 'var p: TPoint;',
  17988. 'begin',
  17989. ' t.x:=t.y;',
  17990. ' p:=t;',
  17991. 'end;',
  17992. 'var',
  17993. ' p: tobject.tpoint = (x:2; y:4);',
  17994. ' o: TObject;',
  17995. 'begin',
  17996. ' p:=p;',
  17997. ' o.doit(p);',
  17998. '']);
  17999. ConvertProgram;
  18000. CheckSource('TestNestedClass_Record',
  18001. LinesToStr([ // statements
  18002. 'rtl.createClass(this, "TObject", null, function () {',
  18003. ' rtl.recNewT(this, "TPoint", function () {',
  18004. ' this.x = 0;',
  18005. ' this.y = 0;',
  18006. ' this.$eq = function (b) {',
  18007. ' return (this.x === b.x) && (this.y === b.y);',
  18008. ' };',
  18009. ' this.$assign = function (s) {',
  18010. ' this.x = s.x;',
  18011. ' this.y = s.y;',
  18012. ' return this;',
  18013. ' };',
  18014. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {}, this);',
  18015. ' $r.addField("x", rtl.byte);',
  18016. ' $r.addField("y", rtl.byte);',
  18017. ' });',
  18018. ' this.$init = function () {',
  18019. ' };',
  18020. ' this.$final = function () {',
  18021. ' };',
  18022. ' this.DoIt = function (t) {',
  18023. ' var p = this.TPoint.$new();',
  18024. ' t.x = t.y;',
  18025. ' p.$assign(t);',
  18026. ' };',
  18027. '});',
  18028. 'this.p = this.TObject.TPoint.$clone({',
  18029. ' x: 2,',
  18030. ' y: 4',
  18031. '});',
  18032. 'this.o = null;',
  18033. '']),
  18034. LinesToStr([ // $mod.$main
  18035. '$mod.p.$assign($mod.p);',
  18036. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  18037. '']));
  18038. end;
  18039. procedure TTestModule.TestNestedClass_Class;
  18040. begin
  18041. StartProgram(false);
  18042. Add([
  18043. 'type',
  18044. ' TObject = class end;',
  18045. ' TBird = class',
  18046. ' type TLeg = class',
  18047. ' FId: longint;',
  18048. ' constructor Create;',
  18049. ' function Create(i: longint): TLeg;',
  18050. ' end;',
  18051. ' function DoIt(b: TBird): Tleg;',
  18052. ' end;',
  18053. 'constructor tbird.tleg.create;',
  18054. 'begin',
  18055. ' FId:=3;',
  18056. 'end;',
  18057. 'function tbird.tleg.Create(i: longint): TLeg;',
  18058. 'begin',
  18059. ' Create;',
  18060. ' Result:=TLeg.Create;',
  18061. ' Result:=TBird.TLeg.Create;',
  18062. ' Result:=Create(3);',
  18063. ' FId:=i;',
  18064. 'end;',
  18065. 'function tbird.DoIt(b: tbird): tleg;',
  18066. 'begin',
  18067. ' Result.Create;',
  18068. ' Result:=TLeg.Create;',
  18069. ' Result:=TBird.TLeg.Create;',
  18070. ' Result:=Result.Create(3);',
  18071. 'end;',
  18072. 'var',
  18073. ' b: Tbird.tleg;',
  18074. 'begin',
  18075. ' b.Create;',
  18076. ' b:=TBird.TLeg.Create;',
  18077. ' b:=b.Create(3);',
  18078. '']);
  18079. ConvertProgram;
  18080. CheckSource('TestNestedClass_Class',
  18081. LinesToStr([ // statements
  18082. 'rtl.createClass(this, "TObject", null, function () {',
  18083. ' this.$init = function () {',
  18084. ' };',
  18085. ' this.$final = function () {',
  18086. ' };',
  18087. '});',
  18088. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18089. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  18090. ' this.$init = function () {',
  18091. ' $mod.TObject.$init.call(this);',
  18092. ' this.FId = 0;',
  18093. ' };',
  18094. ' this.Create = function () {',
  18095. ' this.FId = 3;',
  18096. ' return this;',
  18097. ' };',
  18098. ' this.Create$1 = function (i) {',
  18099. ' var Result = null;',
  18100. ' this.Create();',
  18101. ' Result = $mod.TBird.TLeg.$create("Create");',
  18102. ' Result = $mod.TBird.TLeg.$create("Create");',
  18103. ' Result = this.Create$1(3);',
  18104. ' this.FId = i;',
  18105. ' return Result;',
  18106. ' };',
  18107. ' }, "TBird.TLeg");',
  18108. ' this.DoIt = function (b) {',
  18109. ' var Result = null;',
  18110. ' Result.Create();',
  18111. ' Result = this.TLeg.$create("Create");',
  18112. ' Result = $mod.TBird.TLeg.$create("Create");',
  18113. ' Result = Result.Create$1(3);',
  18114. ' return Result;',
  18115. ' };',
  18116. '});',
  18117. 'this.b = null;',
  18118. '']),
  18119. LinesToStr([ // $mod.$main
  18120. '$mod.b.Create();',
  18121. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  18122. '$mod.b = $mod.b.Create$1(3);',
  18123. '']));
  18124. end;
  18125. procedure TTestModule.TestNestedClass_CallInherited;
  18126. begin
  18127. StartProgram(false);
  18128. Add([
  18129. 'type',
  18130. ' TObject = class end;',
  18131. ' TBird = class',
  18132. ' type',
  18133. ' TWing = class',
  18134. ' function Fly(w: word = 17): word; virtual;',
  18135. ' end;',
  18136. ' end;',
  18137. ' TEagle = class(TBird)',
  18138. ' type',
  18139. ' TEagleWing = class(TWing)',
  18140. ' function Fly(w: word): word; override;',
  18141. ' end;',
  18142. ' end;',
  18143. 'function TBird.TWing.Fly(w: word): word;',
  18144. 'begin',
  18145. 'end;',
  18146. 'function TEagle.TEagleWing.Fly(w: word): word;',
  18147. 'begin',
  18148. ' inherited;',
  18149. ' inherited Fly;',
  18150. ' inherited Fly(3);',
  18151. ' Result:=inherited Fly;',
  18152. ' Result:=inherited Fly(4);',
  18153. 'end;',
  18154. 'begin',
  18155. '']);
  18156. ConvertProgram;
  18157. CheckSource('TestNestedClass_CallInherited',
  18158. LinesToStr([ // statements
  18159. 'rtl.createClass(this, "TObject", null, function () {',
  18160. ' this.$init = function () {',
  18161. ' };',
  18162. ' this.$final = function () {',
  18163. ' };',
  18164. '});',
  18165. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18166. ' rtl.createClass(this, "TWing", $mod.TObject, function () {',
  18167. ' this.Fly = function (w) {',
  18168. ' var Result = 0;',
  18169. ' return Result;',
  18170. ' };',
  18171. ' }, "TBird.TWing");',
  18172. '});',
  18173. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  18174. ' rtl.createClass(this, "TEagleWing", this.TWing, function () {',
  18175. ' this.Fly = function (w) {',
  18176. ' var Result = 0;',
  18177. ' $mod.TBird.TWing.Fly.apply(this, arguments);',
  18178. ' $mod.TBird.TWing.Fly.call(this, 17);',
  18179. ' $mod.TBird.TWing.Fly.call(this, 3);',
  18180. ' Result = $mod.TBird.TWing.Fly.call(this, 17);',
  18181. ' Result = $mod.TBird.TWing.Fly.call(this, 4);',
  18182. ' return Result;',
  18183. ' };',
  18184. ' }, "TEagle.TEagleWing");',
  18185. '});',
  18186. '']),
  18187. LinesToStr([ // $mod.$main
  18188. '']));
  18189. end;
  18190. procedure TTestModule.TestExternalClass_Var;
  18191. begin
  18192. StartProgram(false);
  18193. Add([
  18194. '{$modeswitch externalclass}',
  18195. 'type',
  18196. ' TExtA = class external name ''ExtObj''',
  18197. ' Id: longint external name ''$Id'';',
  18198. ' B: longint;',
  18199. ' end;',
  18200. 'var Obj: TExtA;',
  18201. 'begin',
  18202. ' obj.id:=obj.id+1;',
  18203. ' obj.B:=obj.B+1;']);
  18204. ConvertProgram;
  18205. CheckSource('TestExternalClass_Var',
  18206. LinesToStr([ // statements
  18207. 'this.Obj = null;',
  18208. '']),
  18209. LinesToStr([ // $mod.$main
  18210. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  18211. '$mod.Obj.B = $mod.Obj.B + 1;',
  18212. '']));
  18213. end;
  18214. procedure TTestModule.TestExternalClass_Const;
  18215. begin
  18216. StartProgram(false);
  18217. Add([
  18218. '{$modeswitch externalclass}',
  18219. 'type',
  18220. ' TExtA = class external name ''ExtObj''',
  18221. ' const Two: longint = 2;',
  18222. ' const Three = 3;',
  18223. ' const Id: longint;',
  18224. ' end;',
  18225. ' TExtB = class external name ''ExtB''',
  18226. ' A: TExtA;',
  18227. ' end;',
  18228. 'var',
  18229. ' A: texta;',
  18230. ' B: textb;',
  18231. ' i: longint;',
  18232. 'begin',
  18233. ' i:=a.two;',
  18234. ' i:=texta.two;',
  18235. ' i:=a.three;',
  18236. ' i:=texta.three;',
  18237. ' i:=a.id;',
  18238. ' i:=texta.id;',
  18239. '']);
  18240. ConvertProgram;
  18241. CheckSource('TestExternalClass_Const',
  18242. LinesToStr([ // statements
  18243. 'this.A = null;',
  18244. 'this.B = null;',
  18245. 'this.i = 0;',
  18246. '']),
  18247. LinesToStr([ // $mod.$main
  18248. '$mod.i = 2;',
  18249. '$mod.i = 2;',
  18250. '$mod.i = 3;',
  18251. '$mod.i = 3;',
  18252. '$mod.i = $mod.A.Id;',
  18253. '$mod.i = ExtObj.Id;',
  18254. '']));
  18255. end;
  18256. procedure TTestModule.TestExternalClass_Dollar;
  18257. begin
  18258. StartProgram(false);
  18259. Add([
  18260. '{$modeswitch externalclass}',
  18261. 'type',
  18262. ' TExtA = class external name ''$''',
  18263. ' Id: longint external name ''$'';',
  18264. ' function Bla(i: longint): longint; external name ''$'';',
  18265. ' end;',
  18266. 'function dollar(k: longint): longint; external name ''$'';',
  18267. 'var Obj: TExtA;',
  18268. 'begin',
  18269. ' dollar(1);',
  18270. ' obj.id:=obj.id+2;',
  18271. ' obj.Bla(3);',
  18272. '']);
  18273. ConvertProgram;
  18274. CheckSource('TestExternalClass_Dollar',
  18275. LinesToStr([ // statements
  18276. 'this.Obj = null;',
  18277. '']),
  18278. LinesToStr([ // $mod.$main
  18279. '$(1);',
  18280. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  18281. '$mod.Obj.$(3);',
  18282. '']));
  18283. end;
  18284. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  18285. begin
  18286. StartProgram(false);
  18287. Add('{$modeswitch externalclass}');
  18288. Add('type');
  18289. Add(' TExtA = class external name ''ExtA''');
  18290. Add(' Id: longint external name ''$Id'';');
  18291. Add(' end;');
  18292. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  18293. Add(' Id: longint;');
  18294. Add(' end;');
  18295. Add('begin');
  18296. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  18297. ConvertProgram;
  18298. end;
  18299. procedure TTestModule.TestExternalClass_Method;
  18300. begin
  18301. StartProgram(false);
  18302. Add(['{$modeswitch externalclass}',
  18303. 'type',
  18304. ' TExtA = class external name ''ExtObj''',
  18305. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  18306. ' procedure DoSome(Id: longint = 1);',
  18307. ' end;',
  18308. 'var Obj: texta;',
  18309. 'begin',
  18310. ' obj.doit;',
  18311. ' obj.doit();',
  18312. ' obj.doit(2);',
  18313. ' with obj do begin',
  18314. ' doit;',
  18315. ' doit();',
  18316. ' doit(3);',
  18317. ' end;']);
  18318. ConvertProgram;
  18319. CheckSource('TestExternalClass_Method',
  18320. LinesToStr([ // statements
  18321. 'this.Obj = null;',
  18322. '']),
  18323. LinesToStr([ // $mod.$main
  18324. '$mod.Obj.$Execute(1);',
  18325. '$mod.Obj.$Execute(1);',
  18326. '$mod.Obj.$Execute(2);',
  18327. 'var $with = $mod.Obj;',
  18328. '$with.$Execute(1);',
  18329. '$with.$Execute(1);',
  18330. '$with.$Execute(3);',
  18331. '']));
  18332. end;
  18333. procedure TTestModule.TestExternalClass_ClassMethod;
  18334. begin
  18335. StartProgram(false);
  18336. Add([
  18337. '{$modeswitch externalclass}',
  18338. 'type',
  18339. ' TExtA = class external name ''ExtObj''',
  18340. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  18341. ' end;',
  18342. ' TExtB = TExtA;',
  18343. 'var p: Pointer;',
  18344. 'begin',
  18345. ' texta.doit;',
  18346. ' texta.doit();',
  18347. ' texta.doit(2);',
  18348. ' p:[email protected];',
  18349. ' with texta do begin',
  18350. ' doit;',
  18351. ' doit();',
  18352. ' doit(3);',
  18353. ' p:=@DoIt;',
  18354. ' end;',
  18355. ' textb.doit;',
  18356. ' textb.doit();',
  18357. ' textb.doit(4);',
  18358. ' with textb do begin',
  18359. ' doit;',
  18360. ' doit();',
  18361. ' doit(5);',
  18362. ' end;',
  18363. '']);
  18364. ConvertProgram;
  18365. CheckSource('TestExternalClass_ClassMethod',
  18366. LinesToStr([ // statements
  18367. 'this.p = null;',
  18368. '']),
  18369. LinesToStr([ // $mod.$main
  18370. 'ExtObj.$Execute(1);',
  18371. 'ExtObj.$Execute(1);',
  18372. 'ExtObj.$Execute(2);',
  18373. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  18374. 'ExtObj.$Execute(1);',
  18375. 'ExtObj.$Execute(1);',
  18376. 'ExtObj.$Execute(3);',
  18377. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  18378. 'ExtObj.$Execute(1);',
  18379. 'ExtObj.$Execute(1);',
  18380. 'ExtObj.$Execute(4);',
  18381. 'ExtObj.$Execute(1);',
  18382. 'ExtObj.$Execute(1);',
  18383. 'ExtObj.$Execute(5);',
  18384. '']));
  18385. end;
  18386. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  18387. begin
  18388. StartProgram(false);
  18389. Add([
  18390. '{$modeswitch externalclass}',
  18391. 'type',
  18392. ' TExtA = class external name ''ExtObj''',
  18393. ' class procedure DoIt(Id: longint = 1); static;',
  18394. ' end;',
  18395. 'var p: Pointer;',
  18396. 'begin',
  18397. ' texta.doit;',
  18398. ' texta.doit();',
  18399. ' texta.doit(2);',
  18400. ' p:[email protected];',
  18401. ' with texta do begin',
  18402. ' doit;',
  18403. ' doit();',
  18404. ' doit(3);',
  18405. ' p:=@DoIt;',
  18406. ' end;',
  18407. '']);
  18408. ConvertProgram;
  18409. CheckSource('TestExternalClass_ClassMethodStatic',
  18410. LinesToStr([ // statements
  18411. 'this.p = null;',
  18412. '']),
  18413. LinesToStr([ // $mod.$main
  18414. 'ExtObj.DoIt(1);',
  18415. 'ExtObj.DoIt(1);',
  18416. 'ExtObj.DoIt(2);',
  18417. '$mod.p = ExtObj.DoIt;',
  18418. 'ExtObj.DoIt(1);',
  18419. 'ExtObj.DoIt(1);',
  18420. 'ExtObj.DoIt(3);',
  18421. '$mod.p = ExtObj.DoIt;',
  18422. '']));
  18423. end;
  18424. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  18425. begin
  18426. StartProgram(false);
  18427. Add([
  18428. '{$modeswitch externalclass}',
  18429. 'type',
  18430. ' TBird = class external name ''Array''',
  18431. ' end;',
  18432. 'function GetPtr: Pointer;',
  18433. 'begin',
  18434. 'end;',
  18435. 'procedure Write(const p);',
  18436. 'begin',
  18437. 'end;',
  18438. 'procedure WriteLn; varargs;',
  18439. 'begin',
  18440. 'end;',
  18441. 'begin',
  18442. ' if TBird(GetPtr)=nil then ;',
  18443. ' Write(GetPtr);',
  18444. ' WriteLn(GetPtr);',
  18445. ' Write(TBird(GetPtr));',
  18446. ' WriteLn(TBird(GetPtr));',
  18447. '']);
  18448. ConvertProgram;
  18449. CheckSource('TestFunctionResultInTypeCast',
  18450. LinesToStr([ // statements
  18451. 'this.GetPtr = function () {',
  18452. ' var Result = null;',
  18453. ' return Result;',
  18454. '};',
  18455. 'this.Write = function (p) {',
  18456. '};',
  18457. 'this.WriteLn = function () {',
  18458. '};',
  18459. '']),
  18460. LinesToStr([
  18461. 'if ($mod.GetPtr() === null) ;',
  18462. '$mod.Write($mod.GetPtr());',
  18463. '$mod.WriteLn($mod.GetPtr());',
  18464. '$mod.Write($mod.GetPtr());',
  18465. '$mod.WriteLn($mod.GetPtr());',
  18466. '']));
  18467. end;
  18468. procedure TTestModule.TestExternalClass_NonExternalOverride;
  18469. begin
  18470. StartProgram(false);
  18471. Add([
  18472. '{$modeswitch externalclass}',
  18473. 'type',
  18474. ' TExtA = class external name ''ExtObjA''',
  18475. ' procedure ProcA; virtual;',
  18476. ' procedure ProcB; virtual;',
  18477. ' end;',
  18478. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  18479. ' end;',
  18480. ' TExtC = class (TExtB)',
  18481. ' procedure ProcA; override;',
  18482. ' end;',
  18483. 'procedure TExtC.ProcA;',
  18484. 'begin',
  18485. ' ProcA;',
  18486. ' Self.ProcA;',
  18487. ' ProcB;',
  18488. ' Self.ProcB;',
  18489. 'end;',
  18490. 'var',
  18491. ' A: texta;',
  18492. ' B: textb;',
  18493. ' C: textc;',
  18494. 'begin',
  18495. ' a.proca;',
  18496. ' b.proca;',
  18497. ' c.proca;']);
  18498. ConvertProgram;
  18499. CheckSource('TestExternalClass_NonExternalOverride',
  18500. LinesToStr([ // statements
  18501. 'rtl.createClassExt(this, "TExtC", ExtObjB, "", function () {',
  18502. ' this.$init = function () {',
  18503. ' };',
  18504. ' this.$final = function () {',
  18505. ' };',
  18506. ' this.ProcA = function () {',
  18507. ' this.ProcA();',
  18508. ' this.ProcA();',
  18509. ' this.ProcB();',
  18510. ' this.ProcB();',
  18511. ' };',
  18512. '});',
  18513. 'this.A = null;',
  18514. 'this.B = null;',
  18515. 'this.C = null;',
  18516. '']),
  18517. LinesToStr([ // $mod.$main
  18518. '$mod.A.ProcA();',
  18519. '$mod.B.ProcA();',
  18520. '$mod.C.ProcA();',
  18521. '']));
  18522. end;
  18523. procedure TTestModule.TestExternalClass_OverloadHint;
  18524. begin
  18525. StartProgram(false);
  18526. Add([
  18527. '{$modeswitch externalclass}',
  18528. 'type',
  18529. ' TExtA = class external name ''ExtObjA''',
  18530. ' procedure DoIt;',
  18531. ' procedure DoIt(i: longint);',
  18532. ' end;',
  18533. 'begin',
  18534. '']);
  18535. ConvertProgram;
  18536. CheckResolverUnexpectedHints(true);
  18537. CheckSource('TestExternalClass_OverloadHint',
  18538. LinesToStr([ // statements
  18539. '']),
  18540. LinesToStr([ // $mod.$main
  18541. '']));
  18542. end;
  18543. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  18544. begin
  18545. WithTypeInfo:=true;
  18546. StartProgram(false);
  18547. Add([
  18548. '{$modeswitch externalclass}',
  18549. 'type',
  18550. ' JSwiper = class external name ''Swiper''',
  18551. ' constructor New;',
  18552. ' end;',
  18553. ' TObject = class',
  18554. ' private',
  18555. ' FSwiper: JSwiper;',
  18556. ' published',
  18557. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  18558. ' end;',
  18559. 'begin',
  18560. ' JSwiper.new;',
  18561. '']);
  18562. ConvertProgram;
  18563. CheckSource('TestExternalClass_SameNamePublishedProperty',
  18564. LinesToStr([ // statements
  18565. 'this.$rtti.$ExtClass("JSwiper", {',
  18566. ' jsclass: "Swiper"',
  18567. '});',
  18568. 'rtl.createClass(this, "TObject", null, function () {',
  18569. ' this.$init = function () {',
  18570. ' this.FSwiper = null;',
  18571. ' };',
  18572. ' this.$final = function () {',
  18573. ' this.FSwiper = undefined;',
  18574. ' };',
  18575. ' var $r = this.$rtti;',
  18576. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  18577. '});',
  18578. '']),
  18579. LinesToStr([ // $mod.$main
  18580. 'new Swiper();',
  18581. '']));
  18582. end;
  18583. procedure TTestModule.TestExternalClass_Property;
  18584. begin
  18585. StartProgram(false);
  18586. Add([
  18587. '{$modeswitch externalclass}',
  18588. 'type',
  18589. ' TExtA = class external name ''ExtA''',
  18590. ' function getYear: longint;',
  18591. ' procedure setYear(Value: longint);',
  18592. ' property Year: longint read getyear write setyear;',
  18593. ' end;',
  18594. ' TExtB = class (TExtA)',
  18595. ' procedure OtherSetYear(Value: longint);',
  18596. ' property year write othersetyear;',
  18597. ' end;',
  18598. 'procedure textb.othersetyear(value: longint);',
  18599. 'begin',
  18600. ' setYear(Value+4);',
  18601. 'end;',
  18602. 'var',
  18603. ' A: texta;',
  18604. ' B: textb;',
  18605. 'begin',
  18606. ' a.year:=a.year+1;',
  18607. ' b.year:=b.year+2;']);
  18608. ConvertProgram;
  18609. CheckSource('TestExternalClass_NonExternalOverride',
  18610. LinesToStr([ // statements
  18611. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18612. ' this.$init = function () {',
  18613. ' };',
  18614. ' this.$final = function () {',
  18615. ' };',
  18616. ' this.OtherSetYear = function (Value) {',
  18617. ' this.setYear(Value+4);',
  18618. ' };',
  18619. '});',
  18620. 'this.A = null;',
  18621. 'this.B = null;',
  18622. '']),
  18623. LinesToStr([ // $mod.$main
  18624. '$mod.A.setYear($mod.A.getYear()+1);',
  18625. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  18626. '']));
  18627. end;
  18628. procedure TTestModule.TestExternalClass_PropertyDate;
  18629. begin
  18630. StartProgram(false);
  18631. Add([
  18632. '{$modeswitch externalclass}',
  18633. 'type',
  18634. ' TExtA = class external name ''ExtA''',
  18635. ' end;',
  18636. ' TExtB = class (TExtA)',
  18637. ' FDate: string;',
  18638. ' property Date: string read FDate write FDate;',
  18639. ' property ExtA: string read FDate write FDate;',
  18640. ' end;',
  18641. ' {$M+}',
  18642. ' TObject = class',
  18643. ' FDate: string;',
  18644. ' published',
  18645. ' property Date: string read FDate write FDate;',
  18646. ' property ExtA: string read FDate write FDate;',
  18647. ' end;',
  18648. 'var',
  18649. ' B: textb;',
  18650. ' o: TObject;',
  18651. 'begin',
  18652. ' b.date:=b.exta;',
  18653. ' o.date:=o.exta;']);
  18654. ConvertProgram;
  18655. CheckSource('TestExternalClass_PropertyDate',
  18656. LinesToStr([ // statements
  18657. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18658. ' this.$init = function () {',
  18659. ' this.FDate = "";',
  18660. ' };',
  18661. ' this.$final = function () {',
  18662. ' };',
  18663. '});',
  18664. 'rtl.createClass(this, "TObject", null, function () {',
  18665. ' this.$init = function () {',
  18666. ' this.FDate = "";',
  18667. ' };',
  18668. ' this.$final = function () {',
  18669. ' };',
  18670. ' var $r = this.$rtti;',
  18671. ' $r.addField("FDate", rtl.string, 4);',
  18672. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  18673. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  18674. '});',
  18675. 'this.B = null;',
  18676. 'this.o = null;',
  18677. '']),
  18678. LinesToStr([ // $mod.$main
  18679. '$mod.B.FDate = $mod.B.FDate;',
  18680. '$mod.o.FDate = $mod.o.FDate;',
  18681. '']));
  18682. end;
  18683. procedure TTestModule.TestExternalClass_ClassProperty;
  18684. begin
  18685. StartProgram(false);
  18686. Add('{$modeswitch externalclass}');
  18687. Add('type');
  18688. Add(' TExtA = class external name ''ExtA''');
  18689. Add(' class function getYear: longint;');
  18690. Add(' class procedure setYear(Value: longint);');
  18691. Add(' class property Year: longint read getyear write setyear;');
  18692. Add(' end;');
  18693. Add(' TExtB = class (TExtA)');
  18694. Add(' class function GetCentury: longint;');
  18695. Add(' class procedure SetCentury(Value: longint);');
  18696. Add(' class property Century: longint read getcentury write setcentury;');
  18697. Add(' end;');
  18698. Add('class function textb.getcentury: longint;');
  18699. Add('begin');
  18700. Add('end;');
  18701. Add('class procedure textb.setcentury(value: longint);');
  18702. Add('begin');
  18703. Add(' setyear(value+11);');
  18704. Add(' texta.year:=texta.year+12;');
  18705. Add(' year:=year+13;');
  18706. Add(' textb.century:=textb.century+14;');
  18707. Add(' century:=century+15;');
  18708. Add('end;');
  18709. Add('var');
  18710. Add(' A: texta;');
  18711. Add(' B: textb;');
  18712. Add('begin');
  18713. Add(' texta.year:=texta.year+1;');
  18714. Add(' textb.year:=textb.year+2;');
  18715. Add(' TextA.year:=TextA.year+3;');
  18716. Add(' b.year:=b.year+4;');
  18717. Add(' textb.century:=textb.century+5;');
  18718. Add(' b.century:=b.century+6;');
  18719. ConvertProgram;
  18720. CheckSource('TestExternalClass_ClassProperty',
  18721. LinesToStr([ // statements
  18722. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18723. ' this.$init = function () {',
  18724. ' };',
  18725. ' this.$final = function () {',
  18726. ' };',
  18727. ' this.GetCentury = function () {',
  18728. ' var Result = 0;',
  18729. ' return Result;',
  18730. ' };',
  18731. ' this.SetCentury = function (Value) {',
  18732. ' this.setYear(Value + 11);',
  18733. ' ExtA.setYear(ExtA.getYear() + 12);',
  18734. ' this.setYear(this.getYear() + 13);',
  18735. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  18736. ' this.SetCentury(this.GetCentury() + 15);',
  18737. ' };',
  18738. '});',
  18739. 'this.A = null;',
  18740. 'this.B = null;',
  18741. '']),
  18742. LinesToStr([ // $mod.$main
  18743. 'ExtA.setYear(ExtA.getYear() + 1);',
  18744. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  18745. 'ExtA.setYear(ExtA.getYear() + 3);',
  18746. '$mod.B.setYear($mod.B.getYear() + 4);',
  18747. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  18748. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  18749. '']));
  18750. end;
  18751. procedure TTestModule.TestExternalClass_ClassOf;
  18752. begin
  18753. StartProgram(false);
  18754. Add('{$modeswitch externalclass}');
  18755. Add('type');
  18756. Add(' TExtA = class external name ''ExtA''');
  18757. Add(' procedure ProcA; virtual;');
  18758. Add(' procedure ProcB; virtual;');
  18759. Add(' end;');
  18760. Add(' TExtAClass = class of TExtA;');
  18761. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  18762. Add(' end;');
  18763. Add(' TExtBClass = class of TExtB;');
  18764. Add(' TExtC = class (TExtB)');
  18765. Add(' procedure ProcA; override;');
  18766. Add(' end;');
  18767. Add(' TExtCClass = class of TExtC;');
  18768. Add('procedure TExtC.ProcA; begin end;');
  18769. Add('var');
  18770. Add(' A: texta; ClA: TExtAClass;');
  18771. Add(' B: textb; ClB: TExtBClass;');
  18772. Add(' C: textc; ClC: TExtCClass;');
  18773. Add('begin');
  18774. Add(' ClA:=texta;');
  18775. Add(' ClA:=textb;');
  18776. Add(' ClA:=textc;');
  18777. Add(' ClB:=textb;');
  18778. Add(' ClB:=textc;');
  18779. Add(' ClC:=textc;');
  18780. ConvertProgram;
  18781. CheckSource('TestExternalClass_ClassOf',
  18782. LinesToStr([ // statements
  18783. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18784. ' this.$init = function () {',
  18785. ' };',
  18786. ' this.$final = function () {',
  18787. ' };',
  18788. ' this.ProcA = function () {',
  18789. ' };',
  18790. '});',
  18791. 'this.A = null;',
  18792. 'this.ClA = null;',
  18793. 'this.B = null;',
  18794. 'this.ClB = null;',
  18795. 'this.C = null;',
  18796. 'this.ClC = null;',
  18797. '']),
  18798. LinesToStr([ // $mod.$main
  18799. '$mod.ClA = ExtA;',
  18800. '$mod.ClA = ExtB;',
  18801. '$mod.ClA = $mod.TExtC;',
  18802. '$mod.ClB = ExtB;',
  18803. '$mod.ClB = $mod.TExtC;',
  18804. '$mod.ClC = $mod.TExtC;',
  18805. '']));
  18806. end;
  18807. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  18808. begin
  18809. AddModuleWithIntfImplSrc('unit2.pas',
  18810. LinesToStr([
  18811. '{$modeswitch externalclass}',
  18812. 'type',
  18813. ' TExtA = class external name ''ExtA''',
  18814. ' class var Id: longint;',
  18815. ' end;',
  18816. '']),
  18817. '');
  18818. StartUnit(true);
  18819. Add('interface');
  18820. Add('uses unit2;');
  18821. Add('implementation');
  18822. Add('begin');
  18823. Add(' unit2.texta.id:=unit2.texta.id+1;');
  18824. ConvertUnit;
  18825. CheckSource('TestExternalClass_ClassOtherUnit',
  18826. LinesToStr([
  18827. '']),
  18828. LinesToStr([
  18829. 'ExtA.Id = ExtA.Id + 1;',
  18830. '']));
  18831. end;
  18832. procedure TTestModule.TestExternalClass_Is;
  18833. begin
  18834. StartProgram(false);
  18835. Add([
  18836. '{$modeswitch externalclass}',
  18837. 'type',
  18838. ' TExtA = class external name ''ExtA''',
  18839. ' end;',
  18840. ' TExtAClass = class of TExtA;',
  18841. ' TExtB = class external name ''ExtB'' (TExtA)',
  18842. ' end;',
  18843. ' TExtBClass = class of TExtB;',
  18844. ' TExtC = class (TExtB)',
  18845. ' end;',
  18846. ' TExtCClass = class of TExtC;',
  18847. 'var',
  18848. ' A: texta; ClA: TExtAClass;',
  18849. ' B: textb; ClB: TExtBClass;',
  18850. ' C: textc; ClC: TExtCClass;',
  18851. 'begin',
  18852. ' if a is textb then ;',
  18853. ' if a is textc then ;',
  18854. ' if b is textc then ;',
  18855. ' if cla is textb then ;',
  18856. ' if cla is textc then ;',
  18857. ' if clb is textc then ;',
  18858. ' try',
  18859. ' except',
  18860. ' on TExtA do ;',
  18861. ' on e: TExtB do ;',
  18862. ' end;',
  18863. '']);
  18864. ConvertProgram;
  18865. CheckSource('TestExternalClass_Is',
  18866. LinesToStr([ // statements
  18867. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18868. ' this.$init = function () {',
  18869. ' };',
  18870. ' this.$final = function () {',
  18871. ' };',
  18872. '});',
  18873. 'this.A = null;',
  18874. 'this.ClA = null;',
  18875. 'this.B = null;',
  18876. 'this.ClB = null;',
  18877. 'this.C = null;',
  18878. 'this.ClC = null;',
  18879. '']),
  18880. LinesToStr([ // $mod.$main
  18881. 'if (rtl.isExt($mod.A, ExtB)) ;',
  18882. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  18883. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  18884. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  18885. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  18886. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  18887. 'try {} catch ($e) {',
  18888. ' if (rtl.isExt($e,ExtA)) {}',
  18889. ' else if (rtl.isExt($e,ExtB)) {',
  18890. ' var e = $e;',
  18891. ' } else throw $e',
  18892. '};',
  18893. '']));
  18894. end;
  18895. procedure TTestModule.TestExternalClass_As;
  18896. begin
  18897. StartProgram(false);
  18898. Add('{$modeswitch externalclass}');
  18899. Add('type');
  18900. Add(' TExtA = class external name ''ExtA''');
  18901. Add(' end;');
  18902. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  18903. Add(' end;');
  18904. Add(' TExtC = class (TExtB)');
  18905. Add(' end;');
  18906. Add('var');
  18907. Add(' A: texta;');
  18908. Add(' B: textb;');
  18909. Add(' C: textc;');
  18910. Add('begin');
  18911. Add(' b:=a as textb;');
  18912. Add(' c:=a as textc;');
  18913. Add(' c:=b as textc;');
  18914. ConvertProgram;
  18915. CheckSource('TestExternalClass_Is',
  18916. LinesToStr([ // statements
  18917. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18918. ' this.$init = function () {',
  18919. ' };',
  18920. ' this.$final = function () {',
  18921. ' };',
  18922. '});',
  18923. 'this.A = null;',
  18924. 'this.B = null;',
  18925. 'this.C = null;',
  18926. '']),
  18927. LinesToStr([ // $mod.$main
  18928. '$mod.B = rtl.asExt($mod.A, ExtB);',
  18929. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  18930. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  18931. '']));
  18932. end;
  18933. procedure TTestModule.TestExternalClass_DestructorFail;
  18934. begin
  18935. StartProgram(false);
  18936. Add('{$modeswitch externalclass}');
  18937. Add('type');
  18938. Add(' TExtA = class external name ''ExtA''');
  18939. Add(' destructor Free;');
  18940. Add(' end;');
  18941. SetExpectedPasResolverError('Pascal element not supported: destructor',
  18942. nPasElementNotSupported);
  18943. ConvertProgram;
  18944. end;
  18945. procedure TTestModule.TestExternalClass_New;
  18946. begin
  18947. StartProgram(false);
  18948. Add([
  18949. '{$modeswitch externalclass}',
  18950. 'type',
  18951. ' TExtA = class external name ''ExtA''',
  18952. ' constructor New;',
  18953. ' constructor New(i: longint; j: longint = 2);',
  18954. ' end;',
  18955. 'var',
  18956. ' A: texta;',
  18957. 'begin',
  18958. ' a:=texta.new;',
  18959. ' a:=texta(texta.new);',
  18960. ' a:=texta.new();',
  18961. ' a:=texta.new(1);',
  18962. ' with texta do begin',
  18963. ' a:=new;',
  18964. ' a:=new();',
  18965. ' a:=new(2);',
  18966. ' end;',
  18967. ' a:=test1.texta.new;',
  18968. ' a:=test1.texta.new();',
  18969. ' a:=test1.texta.new(3);',
  18970. '']);
  18971. ConvertProgram;
  18972. CheckSource('TestExternalClass_New',
  18973. LinesToStr([ // statements
  18974. 'this.A = null;',
  18975. '']),
  18976. LinesToStr([ // $mod.$main
  18977. '$mod.A = new ExtA();',
  18978. '$mod.A = new ExtA();',
  18979. '$mod.A = new ExtA();',
  18980. '$mod.A = new ExtA(1,2);',
  18981. '$mod.A = new ExtA();',
  18982. '$mod.A = new ExtA();',
  18983. '$mod.A = new ExtA(2,2);',
  18984. '$mod.A = new ExtA();',
  18985. '$mod.A = new ExtA();',
  18986. '$mod.A = new ExtA(3,2);',
  18987. '']));
  18988. end;
  18989. procedure TTestModule.TestExternalClass_ClassOf_New;
  18990. begin
  18991. StartProgram(false);
  18992. Add('{$modeswitch externalclass}');
  18993. Add('type');
  18994. Add(' TExtAClass = class of TExtA;');
  18995. Add(' TExtA = class external name ''ExtA''');
  18996. Add(' C: TExtAClass;');
  18997. Add(' constructor New;');
  18998. Add(' end;');
  18999. Add('var');
  19000. Add(' A: texta;');
  19001. Add(' C: textaclass;');
  19002. Add('begin');
  19003. Add(' a:=c.new;');
  19004. Add(' a:=c.new();');
  19005. Add(' with C do begin');
  19006. Add(' a:=new;');
  19007. Add(' a:=new();');
  19008. Add(' end;');
  19009. Add(' a:=test1.c.new;');
  19010. Add(' a:=test1.c.new();');
  19011. Add(' a:=A.c.new();');
  19012. ConvertProgram;
  19013. CheckSource('TestExternalClass_ClassOf_New',
  19014. LinesToStr([ // statements
  19015. 'this.A = null;',
  19016. 'this.C = null;',
  19017. '']),
  19018. LinesToStr([ // $mod.$main
  19019. '$mod.A = new $mod.C();',
  19020. '$mod.A = new $mod.C();',
  19021. 'var $with = $mod.C;',
  19022. '$mod.A = new $with();',
  19023. '$mod.A = new $with();',
  19024. '$mod.A = new $mod.C();',
  19025. '$mod.A = new $mod.C();',
  19026. '$mod.A = new $mod.A.C();',
  19027. '']));
  19028. end;
  19029. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  19030. begin
  19031. StartProgram(false);
  19032. Add([
  19033. '{$modeswitch externalclass}',
  19034. 'type',
  19035. ' TExtAClass = class of TExtA;',
  19036. ' TExtA = class external name ''ExtA''',
  19037. ' constructor New;',
  19038. ' end;',
  19039. 'function GetCreator: TExtAClass;',
  19040. 'begin',
  19041. ' Result:=TExtA;',
  19042. 'end;',
  19043. 'var',
  19044. ' A: texta;',
  19045. 'begin',
  19046. ' a:=getcreator.new;',
  19047. ' a:=getcreator().new;',
  19048. ' a:=getcreator().new();',
  19049. ' a:=getcreator.new();',
  19050. ' with getcreator do begin',
  19051. ' a:=new;',
  19052. ' a:=new();',
  19053. ' end;']);
  19054. ConvertProgram;
  19055. CheckSource('TestExternalClass_FuncClassOf_New',
  19056. LinesToStr([ // statements
  19057. 'this.GetCreator = function () {',
  19058. ' var Result = null;',
  19059. ' Result = ExtA;',
  19060. ' return Result;',
  19061. '};',
  19062. 'this.A = null;',
  19063. '']),
  19064. LinesToStr([ // $mod.$main
  19065. '$mod.A = new ($mod.GetCreator())();',
  19066. '$mod.A = new ($mod.GetCreator())();',
  19067. '$mod.A = new ($mod.GetCreator())();',
  19068. '$mod.A = new ($mod.GetCreator())();',
  19069. 'var $with = $mod.GetCreator();',
  19070. '$mod.A = new $with();',
  19071. '$mod.A = new $with();',
  19072. '']));
  19073. end;
  19074. procedure TTestModule.TestExternalClass_New_PasClassFail;
  19075. begin
  19076. StartProgram(false);
  19077. Add([
  19078. '{$modeswitch externalclass}',
  19079. 'type',
  19080. ' TExtA = class external name ''ExtA''',
  19081. ' constructor New;',
  19082. ' end;',
  19083. ' TBird = class(TExtA)',
  19084. ' end;',
  19085. 'begin',
  19086. ' TBird.new;',
  19087. '']);
  19088. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  19089. ConvertProgram;
  19090. end;
  19091. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  19092. begin
  19093. StartProgram(false);
  19094. Add([
  19095. '{$modeswitch externalclass}',
  19096. 'type',
  19097. ' TExtA = class external name ''ExtA''',
  19098. ' constructor New;',
  19099. ' end;',
  19100. ' TBird = class(TExtA)',
  19101. ' end;',
  19102. 'begin',
  19103. ' TBird.new();',
  19104. '']);
  19105. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  19106. ConvertProgram;
  19107. end;
  19108. procedure TTestModule.TestExternalClass_NewExtName;
  19109. begin
  19110. StartProgram(false);
  19111. Add([
  19112. '{$modeswitch externalclass}',
  19113. 'type',
  19114. ' TExtA = class external name ''ExtA''',
  19115. ' constructor New; external name ''Other'';',
  19116. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  19117. ' end;',
  19118. 'var',
  19119. ' A: texta;',
  19120. 'begin',
  19121. ' a:=texta.new;',
  19122. ' a:=texta(texta.new);',
  19123. ' a:=texta.new();',
  19124. ' a:=texta.new(1);',
  19125. ' with texta do begin',
  19126. ' a:=new;',
  19127. ' a:=new();',
  19128. ' a:=new(2);',
  19129. ' end;',
  19130. ' a:=test1.texta.new;',
  19131. ' a:=test1.texta.new();',
  19132. ' a:=test1.texta.new(3);',
  19133. '']);
  19134. ConvertProgram;
  19135. CheckSource('TestExternalClass_NewExtName',
  19136. LinesToStr([ // statements
  19137. 'this.A = null;',
  19138. '']),
  19139. LinesToStr([ // $mod.$main
  19140. '$mod.A = new Other();',
  19141. '$mod.A = new Other();',
  19142. '$mod.A = new Other();',
  19143. '$mod.A = new A.B(1,2);',
  19144. '$mod.A = new Other();',
  19145. '$mod.A = new Other();',
  19146. '$mod.A = new A.B(2,2);',
  19147. '$mod.A = new Other();',
  19148. '$mod.A = new Other();',
  19149. '$mod.A = new A.B(3,2);',
  19150. '']));
  19151. end;
  19152. procedure TTestModule.TestExternalClass_Constructor;
  19153. begin
  19154. StartProgram(false);
  19155. Add([
  19156. '{$modeswitch externalclass}',
  19157. 'type',
  19158. ' TExtA = class external name ''ExtA''',
  19159. ' public type',
  19160. ' TExtB = class external name ''ExtB''',
  19161. ' public type',
  19162. ' TExtC = class external name ''ExtC''',
  19163. ' constructor New;',
  19164. ' constructor New(i: word);',
  19165. ' end;',
  19166. ' end;',
  19167. ' constructor Create;',
  19168. ' constructor Create(i: longint; j: longint = 2);',
  19169. ' end;',
  19170. 'var',
  19171. ' A: texta;',
  19172. ' C: texta.textb.textc;',
  19173. 'begin',
  19174. ' a:=texta.create;',
  19175. ' a:=texta(texta.create);',
  19176. ' a:=texta.create();',
  19177. ' a:=texta.create(1);',
  19178. ' with texta do begin',
  19179. ' a:=create;',
  19180. ' a:=create();',
  19181. ' a:=create(2);',
  19182. ' end;',
  19183. ' a:=test1.texta.create;',
  19184. ' a:=test1.texta.create();',
  19185. ' a:=test1.texta.create(3);',
  19186. ' c:=texta.textb.textc.new;',
  19187. ' c:=texta.textb.textc.new();',
  19188. ' c:=texta.textb.textc.new(4);',
  19189. '']);
  19190. ConvertProgram;
  19191. CheckSource('TestExternalClass_Constructor',
  19192. LinesToStr([ // statements
  19193. 'this.A = null;',
  19194. 'this.C = null;',
  19195. '']),
  19196. LinesToStr([ // $mod.$main
  19197. '$mod.A = new ExtA.Create();',
  19198. '$mod.A = new ExtA.Create();',
  19199. '$mod.A = new ExtA.Create();',
  19200. '$mod.A = new ExtA.Create(1,2);',
  19201. '$mod.A = new ExtA.Create();',
  19202. '$mod.A = new ExtA.Create();',
  19203. '$mod.A = new ExtA.Create(2,2);',
  19204. '$mod.A = new ExtA.Create();',
  19205. '$mod.A = new ExtA.Create();',
  19206. '$mod.A = new ExtA.Create(3,2);',
  19207. '$mod.C = new ExtA.ExtB.ExtC();',
  19208. '$mod.C = new ExtA.ExtB.ExtC();',
  19209. '$mod.C = new ExtA.ExtB.ExtC(4);',
  19210. '']));
  19211. end;
  19212. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  19213. begin
  19214. StartProgram(false);
  19215. Add([
  19216. '{$modeswitch externalclass}',
  19217. 'type',
  19218. ' TExtA = class external name ''ExtA''',
  19219. ' constructor Create; external name ''{}'';',
  19220. ' end;',
  19221. 'var',
  19222. ' A: texta;',
  19223. 'begin',
  19224. ' a:=texta.create;',
  19225. ' a:=texta(texta.create);',
  19226. ' a:=texta.create();',
  19227. ' with texta do begin',
  19228. ' a:=create;',
  19229. ' a:=create();',
  19230. ' end;',
  19231. ' a:=test1.texta.create;',
  19232. ' a:=test1.texta.create();',
  19233. '']);
  19234. ConvertProgram;
  19235. CheckSource('TestExternalClass_ConstructorBrackets',
  19236. LinesToStr([ // statements
  19237. 'this.A = null;',
  19238. '']),
  19239. LinesToStr([ // $mod.$main
  19240. '$mod.A = {};',
  19241. '$mod.A = {};',
  19242. '$mod.A = {};',
  19243. '$mod.A = {};',
  19244. '$mod.A = {};',
  19245. '$mod.A = {};',
  19246. '$mod.A = {};',
  19247. '']));
  19248. end;
  19249. procedure TTestModule.TestExternalClass_LocalConstSameName;
  19250. begin
  19251. StartProgram(false);
  19252. Add('{$modeswitch externalclass}');
  19253. Add('type');
  19254. Add(' TExtA = class external name ''ExtA''');
  19255. Add(' constructor New;');
  19256. Add(' end;');
  19257. Add('function DoIt: longint;');
  19258. Add('const ExtA: longint = 3;');
  19259. Add('begin');
  19260. Add(' Result:=ExtA;');
  19261. Add('end;');
  19262. Add('var');
  19263. Add(' A: texta;');
  19264. Add('begin');
  19265. Add(' a:=texta.new;');
  19266. ConvertProgram;
  19267. CheckSource('TestExternalClass_LocalConstSameName',
  19268. LinesToStr([ // statements
  19269. 'var ExtA$1 = 3;',
  19270. 'this.DoIt = function () {',
  19271. ' var Result = 0;',
  19272. ' Result = ExtA$1;',
  19273. ' return Result;',
  19274. '};',
  19275. 'this.A = null;',
  19276. '']),
  19277. LinesToStr([ // $mod.$main
  19278. '$mod.A = new ExtA();',
  19279. '']));
  19280. end;
  19281. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  19282. begin
  19283. StartProgram(false);
  19284. Add('{$modeswitch externalclass}');
  19285. Add('type');
  19286. Add(' TExtA = class external name ''ExtA''');
  19287. Add(' procedure DoIt;');
  19288. Add(' end;');
  19289. Add(' TMyA = class(TExtA)');
  19290. Add(' procedure DoIt;');
  19291. Add(' end;');
  19292. Add('procedure TMyA.DoIt; begin end;');
  19293. Add('begin');
  19294. ConvertProgram;
  19295. CheckSource('TestExternalClass_ReintroduceOverload',
  19296. LinesToStr([ // statements
  19297. 'rtl.createClassExt(this, "TMyA", ExtA, "", function () {',
  19298. ' this.$init = function () {',
  19299. ' };',
  19300. ' this.$final = function () {',
  19301. ' };',
  19302. ' this.DoIt$1 = function () {',
  19303. ' };',
  19304. '});',
  19305. '']),
  19306. LinesToStr([ // $mod.$main
  19307. '']));
  19308. end;
  19309. procedure TTestModule.TestExternalClass_Inherited;
  19310. begin
  19311. StartProgram(false);
  19312. Add('{$modeswitch externalclass}');
  19313. Add('type');
  19314. Add(' TExtA = class external name ''ExtA''');
  19315. Add(' procedure DoIt(i: longint = 1); virtual;');
  19316. Add(' procedure DoSome(j: longint = 2);');
  19317. Add(' end;');
  19318. Add(' TExtB = class external name ''ExtB''(TExtA)');
  19319. Add(' end;');
  19320. Add(' TMyC = class(TExtB)');
  19321. Add(' procedure DoIt(i: longint = 1); override;');
  19322. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  19323. Add(' end;');
  19324. Add('procedure TMyC.DoIt(i: longint);');
  19325. Add('begin');
  19326. Add(' inherited;');
  19327. Add(' inherited DoIt;');
  19328. Add(' inherited DoIt();');
  19329. Add(' inherited DoIt(3);');
  19330. Add(' inherited DoSome;');
  19331. Add(' inherited DoSome();');
  19332. Add(' inherited DoSome(4);');
  19333. Add('end;');
  19334. Add('procedure TMyC.DoSome(j: longint);');
  19335. Add('begin');
  19336. Add(' inherited;');
  19337. Add('end;');
  19338. Add('begin');
  19339. ConvertProgram;
  19340. CheckSource('TestExternalClass_ReintroduceOverload',
  19341. LinesToStr([ // statements
  19342. 'rtl.createClassExt(this, "TMyC", ExtB, "", function () {',
  19343. ' this.$init = function () {',
  19344. ' };',
  19345. ' this.$final = function () {',
  19346. ' };',
  19347. ' this.DoIt = function (i) {',
  19348. ' ExtB.DoIt.apply(this, arguments);',
  19349. ' ExtB.DoIt.call(this, 1);',
  19350. ' ExtB.DoIt.call(this, 1);',
  19351. ' ExtB.DoIt.call(this, 3);',
  19352. ' ExtB.DoSome.call(this, 2);',
  19353. ' ExtB.DoSome.call(this, 2);',
  19354. ' ExtB.DoSome.call(this, 4);',
  19355. ' };',
  19356. ' this.DoSome$1 = function (j) {',
  19357. ' ExtB.DoSome.apply(this, arguments);',
  19358. ' };',
  19359. '});',
  19360. '']),
  19361. LinesToStr([ // $mod.$main
  19362. '']));
  19363. end;
  19364. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  19365. begin
  19366. StartProgram(false);
  19367. Add('{$modeswitch externalclass}');
  19368. Add('type');
  19369. Add(' TObject = class');
  19370. Add(' end;');
  19371. Add(' TExtA = class external name ''ExtA''(TObject)');
  19372. Add(' end;');
  19373. Add('begin');
  19374. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  19375. ConvertProgram;
  19376. end;
  19377. procedure TTestModule.TestExternalClass_NewInstance;
  19378. begin
  19379. StartProgram(false);
  19380. Add('{$modeswitch externalclass}');
  19381. Add('type');
  19382. Add(' TExtA = class external name ''ExtA''');
  19383. Add(' end;');
  19384. Add(' TMyB = class(TExtA)');
  19385. Add(' protected');
  19386. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  19387. Add(' end;');
  19388. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  19389. Add('begin end;');
  19390. Add('begin');
  19391. ConvertProgram;
  19392. CheckSource('TestExternalClass_NewInstance',
  19393. LinesToStr([ // statements
  19394. 'rtl.createClassExt(this, "TMyB", ExtA, "NewInstance", function () {',
  19395. ' this.$init = function () {',
  19396. ' };',
  19397. ' this.$final = function () {',
  19398. ' };',
  19399. ' this.NewInstance = function (fnname, paramarray) {',
  19400. ' var Result = null;',
  19401. ' return Result;',
  19402. ' };',
  19403. '});',
  19404. '']),
  19405. LinesToStr([ // $mod.$main
  19406. '']));
  19407. end;
  19408. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  19409. begin
  19410. StartProgram(false);
  19411. Add('{$modeswitch externalclass}');
  19412. Add('type');
  19413. Add(' TExtA = class external name ''ExtA''');
  19414. Add(' end;');
  19415. Add(' TMyB = class(TExtA)');
  19416. Add(' protected');
  19417. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  19418. Add(' end;');
  19419. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  19420. Add('begin end;');
  19421. Add('begin');
  19422. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  19423. ConvertProgram;
  19424. end;
  19425. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  19426. begin
  19427. StartProgram(false);
  19428. Add('{$modeswitch externalclass}');
  19429. Add('type');
  19430. Add(' TExtA = class external name ''ExtA''');
  19431. Add(' end;');
  19432. Add(' TMyB = class(TExtA)');
  19433. Add(' protected');
  19434. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  19435. Add(' end;');
  19436. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  19437. Add('begin end;');
  19438. Add('begin');
  19439. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "Longint", expected "String"',
  19440. nIncompatibleTypeArgNo);
  19441. ConvertProgram;
  19442. end;
  19443. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  19444. begin
  19445. StartProgram(false);
  19446. Add('{$modeswitch externalclass}');
  19447. Add('type');
  19448. Add(' TExtA = class external name ''ExtA''');
  19449. Add(' end;');
  19450. Add(' TMyB = class(TExtA)');
  19451. Add(' protected');
  19452. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  19453. Add(' end;');
  19454. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  19455. Add('begin end;');
  19456. Add('begin');
  19457. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "type", expected "untyped"',
  19458. nIncompatibleTypeArgNo);
  19459. ConvertProgram;
  19460. end;
  19461. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  19462. begin
  19463. StartProgram(false);
  19464. Add([
  19465. '{$modeswitch externalclass}',
  19466. 'type',
  19467. ' TJSFunction = class external name ''Function''',
  19468. ' end;',
  19469. ' TExtA = class external name ''ExtA''(TJSFunction)',
  19470. ' constructor New(w: word);',
  19471. ' end;',
  19472. ' TBird = class (TExtA)',
  19473. ' public',
  19474. ' Size: word;',
  19475. ' class var Legs: word;',
  19476. ' constructor Create(a: word);',
  19477. ' end;',
  19478. ' TEagle = class (TBird)',
  19479. ' public',
  19480. ' constructor Create(b: word); reintroduce;',
  19481. ' end;',
  19482. 'constructor TBird.Create(a: word);',
  19483. 'begin',
  19484. ' inherited;', // silently ignored
  19485. ' inherited New(a);', // this.$func(a)
  19486. 'end;',
  19487. 'constructor TEagle.Create(b: word);',
  19488. 'begin',
  19489. ' inherited Create(b);',
  19490. 'end;',
  19491. 'var',
  19492. ' Bird: TBird;',
  19493. ' Eagle: TEagle;',
  19494. 'begin',
  19495. ' Bird:=TBird.Create(3);',
  19496. ' Eagle:=TEagle.Create(4);',
  19497. ' Bird.Size:=Bird.Size+5;',
  19498. ' Bird.Legs:=Bird.Legs+6;',
  19499. ' Eagle.Size:=Eagle.Size+5;',
  19500. ' Eagle.Legs:=Eagle.Legs+6;',
  19501. '']);
  19502. ConvertProgram;
  19503. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  19504. LinesToStr([ // statements
  19505. 'rtl.createClassExt(this, "TBird", ExtA, "", function () {',
  19506. ' this.Legs = 0;',
  19507. ' this.$init = function () {',
  19508. ' this.Size = 0;',
  19509. ' };',
  19510. ' this.$final = function () {',
  19511. ' };',
  19512. ' this.Create = function (a) {',
  19513. ' this.$ancestorfunc(a);',
  19514. ' return this;',
  19515. ' };',
  19516. '});',
  19517. 'rtl.createClassExt(this, "TEagle", this.TBird, "", function () {',
  19518. ' this.Create$1 = function (b) {',
  19519. ' $mod.TBird.Create.call(this, b);',
  19520. ' return this;',
  19521. ' };',
  19522. '});',
  19523. 'this.Bird = null;',
  19524. 'this.Eagle = null;',
  19525. '']),
  19526. LinesToStr([ // $mod.$main
  19527. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  19528. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  19529. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  19530. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  19531. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  19532. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  19533. '']));
  19534. end;
  19535. procedure TTestModule.TestExternalClass_PascalProperty;
  19536. begin
  19537. StartProgram(false);
  19538. Add('{$modeswitch externalclass}');
  19539. Add('type');
  19540. Add(' TJSElement = class;');
  19541. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  19542. Add(' TJSElement = class external name ''ExtA''');
  19543. Add(' end;');
  19544. Add(' TControl = class(TJSElement)');
  19545. Add(' private');
  19546. Add(' FOnClick: TJSNotifyEvent;');
  19547. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  19548. Add(' procedure Click(Sender: TJSElement);');
  19549. Add(' end;');
  19550. Add('procedure TControl.Click(Sender: TJSElement);');
  19551. Add('begin');
  19552. Add(' OnClick(Self);');
  19553. Add('end;');
  19554. Add('var');
  19555. Add(' Ctrl: TControl;');
  19556. Add('begin');
  19557. Add(' Ctrl.OnClick:[email protected];');
  19558. Add(' Ctrl.OnClick(Ctrl);');
  19559. ConvertProgram;
  19560. CheckSource('TestExternalClass_PascalProperty',
  19561. LinesToStr([ // statements
  19562. 'rtl.createClassExt(this, "TControl", ExtA, "", function () {',
  19563. ' this.$init = function () {',
  19564. ' this.FOnClick = null;',
  19565. ' };',
  19566. ' this.$final = function () {',
  19567. ' this.FOnClick = undefined;',
  19568. ' };',
  19569. ' this.Click = function (Sender) {',
  19570. ' this.FOnClick(this);',
  19571. ' };',
  19572. '});',
  19573. 'this.Ctrl = null;',
  19574. '']),
  19575. LinesToStr([ // $mod.$main
  19576. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  19577. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  19578. '']));
  19579. end;
  19580. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  19581. begin
  19582. StartProgram(false);
  19583. Add([
  19584. '{$modeswitch externalclass}',
  19585. 'type',
  19586. ' IUnknown = interface end;',
  19587. ' TObject = class',
  19588. ' end;',
  19589. ' TChild = class',
  19590. ' end;',
  19591. ' TExtRootA = class external name ''ExtRootA''',
  19592. ' end;',
  19593. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  19594. ' end;',
  19595. ' TExtRootB = class external name ''ExtRootB''',
  19596. ' end;',
  19597. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  19598. ' end;',
  19599. ' TExtString = class external name ''String''',
  19600. ' function charAt(aIndex : NativeInt) : string;',
  19601. ' end;',
  19602. 'var',
  19603. ' Obj: TObject;',
  19604. ' Child: TChild;',
  19605. ' RootA: TExtRootA;',
  19606. ' ChildA: TExtChildA;',
  19607. ' RootB: TExtRootB;',
  19608. ' ChildB: TExtChildB;',
  19609. ' i: IUnknown;',
  19610. ' s: string;',
  19611. ' v: jsvalue;',
  19612. 'begin',
  19613. ' obj:=tobject(roota);',
  19614. ' obj:=tobject(childa);',
  19615. ' child:=tchild(tobject(roota));',
  19616. ' roota:=textroota(obj);',
  19617. ' roota:=textroota(child);',
  19618. ' roota:=textroota(rootb);',
  19619. ' roota:=textroota(childb);',
  19620. ' childa:=textchilda(textroota(obj));',
  19621. ' roota:=TExtRootA(i);',
  19622. ' s:=TExtString(s).charAt(7);',
  19623. ' s:=TExtString(v).charAt(8);',
  19624. '']);
  19625. ConvertProgram;
  19626. CheckSource('TestExternalClass_TypeCastToRootClass',
  19627. LinesToStr([ // statements
  19628. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19629. 'rtl.createClass(this, "TObject", null, function () {',
  19630. ' this.$init = function () {',
  19631. ' };',
  19632. ' this.$final = function () {',
  19633. ' };',
  19634. '});',
  19635. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  19636. '});',
  19637. 'this.Obj = null;',
  19638. 'this.Child = null;',
  19639. 'this.RootA = null;',
  19640. 'this.ChildA = null;',
  19641. 'this.RootB = null;',
  19642. 'this.ChildB = null;',
  19643. 'this.i = null;',
  19644. 'this.s = "";',
  19645. 'this.v = undefined;',
  19646. '']),
  19647. LinesToStr([ // $mod.$main
  19648. '$mod.Obj = $mod.RootA;',
  19649. '$mod.Obj = $mod.ChildA;',
  19650. '$mod.Child = $mod.RootA;',
  19651. '$mod.RootA = $mod.Obj;',
  19652. '$mod.RootA = $mod.Child;',
  19653. '$mod.RootA = $mod.RootB;',
  19654. '$mod.RootA = $mod.ChildB;',
  19655. '$mod.ChildA = $mod.Obj;',
  19656. '$mod.RootA = $mod.i;',
  19657. '$mod.s = $mod.s.charAt(7);',
  19658. '$mod.s = $mod.v.charAt(8);',
  19659. '']));
  19660. end;
  19661. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  19662. begin
  19663. StartProgram(false);
  19664. Add([
  19665. '{$modeswitch externalclass}',
  19666. 'type',
  19667. ' IUnknown = interface end;',
  19668. ' IBird = interface(IUnknown) end;',
  19669. ' TClass = class of TObject;',
  19670. ' TObject = class',
  19671. ' end;',
  19672. ' TChild = class',
  19673. ' end;',
  19674. ' TJSObject = class external name ''Object''',
  19675. ' end;',
  19676. ' TRec = record end;',
  19677. 'var',
  19678. ' Obj: TObject;',
  19679. ' Child: TChild;',
  19680. ' i: IUnknown;',
  19681. ' Bird: IBird;',
  19682. ' j: TJSObject;',
  19683. ' r: TRec;',
  19684. ' c: TClass;',
  19685. 'begin',
  19686. ' j:=tjsobject(IUnknown);',
  19687. ' j:=tjsobject(IBird);',
  19688. ' j:=tjsobject(TObject);',
  19689. ' j:=tjsobject(TChild);',
  19690. ' j:=tjsobject(TRec);',
  19691. ' j:=tjsobject(Obj);',
  19692. ' j:=tjsobject(Child);',
  19693. ' j:=tjsobject(i);',
  19694. ' j:=tjsobject(Bird);',
  19695. ' j:=tjsobject(r);',
  19696. ' j:=tjsobject(c);',
  19697. '']);
  19698. ConvertProgram;
  19699. CheckSource('TestExternalClass_TypeCastToJSObject',
  19700. LinesToStr([ // statements
  19701. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19702. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  19703. 'rtl.createClass(this, "TObject", null, function () {',
  19704. ' this.$init = function () {',
  19705. ' };',
  19706. ' this.$final = function () {',
  19707. ' };',
  19708. '});',
  19709. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  19710. '});',
  19711. 'rtl.recNewT(this, "TRec", function () {',
  19712. ' this.$eq = function (b) {',
  19713. ' return true;',
  19714. ' };',
  19715. ' this.$assign = function (s) {',
  19716. ' return this;',
  19717. ' };',
  19718. '});',
  19719. 'this.Obj = null;',
  19720. 'this.Child = null;',
  19721. 'this.i = null;',
  19722. 'this.Bird = null;',
  19723. 'this.j = null;',
  19724. 'this.r = this.TRec.$new();',
  19725. 'this.c = null;',
  19726. '']),
  19727. LinesToStr([ // $mod.$main
  19728. '$mod.j = $mod.IUnknown;',
  19729. '$mod.j = $mod.IBird;',
  19730. '$mod.j = $mod.TObject;',
  19731. '$mod.j = $mod.TChild;',
  19732. '$mod.j = $mod.TRec;',
  19733. '$mod.j = $mod.Obj;',
  19734. '$mod.j = $mod.Child;',
  19735. '$mod.j = $mod.i;',
  19736. '$mod.j = $mod.Bird;',
  19737. '$mod.j = $mod.r;',
  19738. '$mod.j = $mod.c;',
  19739. '']));
  19740. end;
  19741. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  19742. begin
  19743. StartProgram(false);
  19744. Add('{$modeswitch externalclass}');
  19745. Add('type');
  19746. Add(' TJSString = class external name ''String''');
  19747. Add(' class function fromCharCode() : string; varargs;');
  19748. Add(' function anchor(const aName : string) : string;');
  19749. Add(' end;');
  19750. Add('var');
  19751. Add(' s: string;');
  19752. Add('begin');
  19753. Add(' s:=TJSString.fromCharCode(65,66);');
  19754. Add(' s:=TJSString(s).anchor(s);');
  19755. Add(' s:=TJSString(''foo'').anchor(s);');
  19756. ConvertProgram;
  19757. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  19758. LinesToStr([ // statements
  19759. 'this.s = "";',
  19760. '']),
  19761. LinesToStr([ // $mod.$main
  19762. '$mod.s = String.fromCharCode(65, 66);',
  19763. '$mod.s = $mod.s.anchor($mod.s);',
  19764. '$mod.s = "foo".anchor($mod.s);',
  19765. '']));
  19766. end;
  19767. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  19768. begin
  19769. StartProgram(false);
  19770. Add([
  19771. '{$modeswitch externalclass}',
  19772. 'type',
  19773. ' TJSObject = class external name ''Object'' end;',
  19774. ' TJSFunction = class external name ''Function''',
  19775. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  19776. ' function call(thisArg: TJSObject): JSValue; varargs;',
  19777. ' end;',
  19778. ' TObject = class',
  19779. ' procedure DoIt(i: longint);',
  19780. ' end;',
  19781. ' TFuncInt = function(o: TObject): longint;',
  19782. 'function GetIt(o: TObject): longint;',
  19783. ' procedure Sub; begin end;',
  19784. 'var',
  19785. ' f: TJSFunction;',
  19786. ' fi: TFuncInt;',
  19787. 'begin',
  19788. ' fi:=TFuncInt(f);',
  19789. ' f:=TJSFunction(fi);',
  19790. ' f:=TJSFunction(@GetIt);',
  19791. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  19792. ' f:=TJSFunction(@Sub);',
  19793. ' f:=TJSFunction(@o.doit);',
  19794. ' f:=TJSFunction(fi).bind(nil,4)',
  19795. 'end;',
  19796. 'procedure TObject.DoIt(i: longint);',
  19797. ' procedure Sub; begin end;',
  19798. 'var f: TJSFunction;',
  19799. 'begin',
  19800. ' f:=TJSFunction(@DoIt);',
  19801. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  19802. ' f:=TJSFunction(@Sub);',
  19803. ' f:=TJSFunction(@GetIt);',
  19804. 'end;',
  19805. 'begin']);
  19806. ConvertProgram;
  19807. CheckSource('TestExternalClass_TypeCastToJSFunction',
  19808. LinesToStr([ // statements
  19809. 'rtl.createClass(this, "TObject", null, function () {',
  19810. ' this.$init = function () {',
  19811. ' };',
  19812. ' this.$final = function () {',
  19813. ' };',
  19814. ' this.DoIt = function (i) {',
  19815. ' var $Self = this;',
  19816. ' function Sub() {',
  19817. ' };',
  19818. ' var f = null;',
  19819. ' f = this.DoIt;',
  19820. ' f = this.DoIt.bind(null, 13);',
  19821. ' f = Sub;',
  19822. ' f = $mod.GetIt;',
  19823. ' };',
  19824. '});',
  19825. 'this.GetIt = function (o) {',
  19826. ' var Result = 0;',
  19827. ' function Sub() {',
  19828. ' };',
  19829. ' var f = null;',
  19830. ' var fi = null;',
  19831. ' fi = f;',
  19832. ' f = fi;',
  19833. ' f = $mod.GetIt;',
  19834. ' f = $mod.GetIt.bind(null, 3);',
  19835. ' f = Sub;',
  19836. ' f = $mod.TObject.DoIt;',
  19837. ' f = fi.bind(null, 4);',
  19838. ' return Result;',
  19839. '};',
  19840. '']),
  19841. LinesToStr([ // $mod.$main
  19842. '']));
  19843. end;
  19844. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  19845. begin
  19846. StartProgram(false);
  19847. Add([
  19848. '{$mode delphi}',
  19849. '{$modeswitch externalclass}',
  19850. 'type',
  19851. ' TJSObject = class external name ''Object'' end;',
  19852. ' TJSWindow = class external name ''Window''(TJSObject)',
  19853. ' procedure Open;',
  19854. ' end;',
  19855. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  19856. ' procedure Execute;',
  19857. ' end;',
  19858. 'procedure Fly;',
  19859. 'var',
  19860. ' w: TJSWindow;',
  19861. ' e: TJSEventTarget;',
  19862. 'begin',
  19863. ' w:=TJSWindow(e);',
  19864. ' e:=TJSEventTarget(w);',
  19865. 'end;',
  19866. 'begin']);
  19867. ConvertProgram;
  19868. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  19869. LinesToStr([ // statements
  19870. 'this.Fly = function () {',
  19871. ' var w = null;',
  19872. ' var e = null;',
  19873. ' w = e;',
  19874. ' e = w;',
  19875. '};',
  19876. '']),
  19877. LinesToStr([ // $mod.$main
  19878. '']));
  19879. end;
  19880. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  19881. begin
  19882. StartProgram(false);
  19883. Add('{$modeswitch externalclass}');
  19884. Add('type');
  19885. Add(' TJSString = class external name ''String''');
  19886. Add(' class function fromCharCode() : string; varargs;');
  19887. Add(' end;');
  19888. Add('var');
  19889. Add(' s: string;');
  19890. Add(' sObj: TJSString;');
  19891. Add('begin');
  19892. Add(' s:=sObj.fromCharCode(65,66);');
  19893. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  19894. nExternalClassInstanceCannotAccessStaticX);
  19895. ConvertProgram;
  19896. end;
  19897. procedure TTestModule.TestExternalClass_BracketAccessor;
  19898. begin
  19899. StartProgram(false);
  19900. Add([
  19901. '{$modeswitch externalclass}',
  19902. 'type',
  19903. ' TJSArray = class external name ''Array2''',
  19904. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  19905. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  19906. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  19907. ' end;',
  19908. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  19909. 'begin end;',
  19910. 'var',
  19911. ' Arr: tjsarray;',
  19912. ' s: string;',
  19913. ' i: longint;',
  19914. ' v: jsvalue;',
  19915. 'begin',
  19916. ' v:=arr[0];',
  19917. ' v:=arr.items[1];',
  19918. ' arr[2]:=s;',
  19919. ' arr.items[3]:=s;',
  19920. ' arr[4]:=i;',
  19921. ' arr[5]:=arr[6];',
  19922. ' arr.items[7]:=arr.items[8];',
  19923. ' with arr do items[9]:=items[10];',
  19924. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  19925. ' with arr do begin',
  19926. ' v:=GetItems(14);',
  19927. ' setitems(15,16);',
  19928. ' end;',
  19929. ' v:=test1.arr.items[17];',
  19930. ' test1.arr.items[18]:=v;',
  19931. '']);
  19932. ConvertProgram;
  19933. CheckSource('TestExternalClass_BracketAccessor',
  19934. LinesToStr([ // statements
  19935. 'this.DoIt = function (vI, vJ, vK, vL) {',
  19936. '};',
  19937. 'this.Arr = null;',
  19938. 'this.s = "";',
  19939. 'this.i = 0;',
  19940. 'this.v = undefined;',
  19941. '']),
  19942. LinesToStr([ // $mod.$main
  19943. '$mod.v = $mod.Arr[0];',
  19944. '$mod.v = $mod.Arr[1];',
  19945. '$mod.Arr[2] = $mod.s;',
  19946. '$mod.Arr[3] = $mod.s;',
  19947. '$mod.Arr[4] = $mod.i;',
  19948. '$mod.Arr[5] = $mod.Arr[6];',
  19949. '$mod.Arr[7] = $mod.Arr[8];',
  19950. 'var $with = $mod.Arr;',
  19951. '$with[9] = $with[10];',
  19952. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  19953. ' a: 9,',
  19954. ' p: $mod.Arr,',
  19955. ' get: function () {',
  19956. ' return this.p[this.a];',
  19957. ' },',
  19958. ' set: function (v) {',
  19959. ' this.p[this.a] = v;',
  19960. ' }',
  19961. '}, {',
  19962. ' a: 10,',
  19963. ' p: $mod.Arr,',
  19964. ' get: function () {',
  19965. ' return this.p[this.a];',
  19966. ' },',
  19967. ' set: function (v) {',
  19968. ' this.p[this.a] = v;',
  19969. ' }',
  19970. '});',
  19971. 'var $with1 = $mod.Arr;',
  19972. '$mod.v = $with1[14];',
  19973. '$with1[15] = 16;',
  19974. '$mod.v = $mod.Arr[17];',
  19975. '$mod.Arr[18] = $mod.v;',
  19976. '']));
  19977. end;
  19978. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  19979. begin
  19980. StartProgram(false);
  19981. Add([
  19982. '{$modeswitch externalclass}',
  19983. 'type',
  19984. ' TJSArray = class external name ''Array2''',
  19985. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  19986. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  19987. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  19988. ' end;',
  19989. ' TMyArr = class(TJSArray)',
  19990. ' procedure DoIt;',
  19991. ' end;',
  19992. 'procedure tmyarr.DoIt;',
  19993. 'begin',
  19994. ' Items[1]:=Items[2];',
  19995. ' SetItems(3,getItems(4));',
  19996. 'end;',
  19997. 'var',
  19998. ' Arr: tmyarr;',
  19999. ' s: string;',
  20000. ' i: longint;',
  20001. ' v: jsvalue;',
  20002. 'begin',
  20003. ' v:=arr[0];',
  20004. ' v:=arr.items[1];',
  20005. ' arr[2]:=s;',
  20006. ' arr.items[3]:=s;',
  20007. ' arr[4]:=i;',
  20008. ' arr[5]:=arr[6];',
  20009. ' arr.items[7]:=arr.items[8];',
  20010. ' with arr do items[9]:=items[10];',
  20011. ' with arr do begin',
  20012. ' v:=GetItems(14);',
  20013. ' setitems(15,16);',
  20014. ' end;',
  20015. '']);
  20016. ConvertProgram;
  20017. CheckSource('TestExternalClass_BracketAccessor_Call',
  20018. LinesToStr([ // statements
  20019. 'rtl.createClassExt(this, "TMyArr", Array2, "", function () {',
  20020. ' this.$init = function () {',
  20021. ' };',
  20022. ' this.$final = function () {',
  20023. ' };',
  20024. ' this.DoIt = function () {',
  20025. ' this[1] = this[2];',
  20026. ' this[3] = this[4];',
  20027. ' };',
  20028. '});',
  20029. 'this.Arr = null;',
  20030. 'this.s = "";',
  20031. 'this.i = 0;',
  20032. 'this.v = undefined;',
  20033. '']),
  20034. LinesToStr([ // $mod.$main
  20035. '$mod.v = $mod.Arr[0];',
  20036. '$mod.v = $mod.Arr[1];',
  20037. '$mod.Arr[2] = $mod.s;',
  20038. '$mod.Arr[3] = $mod.s;',
  20039. '$mod.Arr[4] = $mod.i;',
  20040. '$mod.Arr[5] = $mod.Arr[6];',
  20041. '$mod.Arr[7] = $mod.Arr[8];',
  20042. 'var $with = $mod.Arr;',
  20043. '$with[9] = $with[10];',
  20044. 'var $with1 = $mod.Arr;',
  20045. '$mod.v = $with1[14];',
  20046. '$with1[15] = 16;',
  20047. '']));
  20048. end;
  20049. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  20050. begin
  20051. StartProgram(false);
  20052. Add('{$modeswitch externalclass}');
  20053. Add('type');
  20054. Add(' TJSArray = class external name ''Array2''');
  20055. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  20056. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  20057. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  20058. Add(' end;');
  20059. Add('begin');
  20060. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  20061. nBracketAccessorOfExternalClassMustHaveOneParameter);
  20062. ConvertProgram;
  20063. end;
  20064. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  20065. begin
  20066. StartProgram(false);
  20067. Add('{$modeswitch externalclass}');
  20068. Add('type');
  20069. Add(' TJSArray = class external name ''Array2''');
  20070. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  20071. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  20072. Add(' end;');
  20073. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  20074. Add('begin end;');
  20075. Add('var');
  20076. Add(' Arr: tjsarray;');
  20077. Add(' v: jsvalue;');
  20078. Add('begin');
  20079. Add(' v:=arr[0];');
  20080. Add(' v:=arr.items[1];');
  20081. Add(' with arr do v:=items[2];');
  20082. Add(' doit(arr[3],arr[4]);');
  20083. ConvertProgram;
  20084. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  20085. LinesToStr([ // statements
  20086. 'this.DoIt = function (vI, vJ) {',
  20087. '};',
  20088. 'this.Arr = null;',
  20089. 'this.v = undefined;',
  20090. '']),
  20091. LinesToStr([ // $mod.$main
  20092. '$mod.v = $mod.Arr[0];',
  20093. '$mod.v = $mod.Arr[1];',
  20094. 'var $with = $mod.Arr;',
  20095. '$mod.v = $with[2];',
  20096. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  20097. '']));
  20098. end;
  20099. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  20100. begin
  20101. StartProgram(false);
  20102. Add('{$modeswitch externalclass}');
  20103. Add('type');
  20104. Add(' TJSArray = class external name ''Array2''');
  20105. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20106. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  20107. Add(' end;');
  20108. Add('var');
  20109. Add(' Arr: tjsarray;');
  20110. Add(' s: string;');
  20111. Add(' i: longint;');
  20112. Add(' v: jsvalue;');
  20113. Add('begin');
  20114. Add(' arr[2]:=s;');
  20115. Add(' arr.items[3]:=s;');
  20116. Add(' arr[4]:=i;');
  20117. Add(' with arr do items[5]:=i;');
  20118. ConvertProgram;
  20119. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  20120. LinesToStr([ // statements
  20121. 'this.Arr = null;',
  20122. 'this.s = "";',
  20123. 'this.i = 0;',
  20124. 'this.v = undefined;',
  20125. '']),
  20126. LinesToStr([ // $mod.$main
  20127. '$mod.Arr[2] = $mod.s;',
  20128. '$mod.Arr[3] = $mod.s;',
  20129. '$mod.Arr[4] = $mod.i;',
  20130. 'var $with = $mod.Arr;',
  20131. '$with[5] = $mod.i;',
  20132. '']));
  20133. end;
  20134. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  20135. begin
  20136. StartProgram(false);
  20137. Add('{$modeswitch externalclass}');
  20138. Add('type');
  20139. Add(' TJSArray = class external name ''Array2''');
  20140. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20141. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  20142. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  20143. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  20144. Add(' end;');
  20145. Add('var');
  20146. Add(' Arr: tjsarray;');
  20147. Add(' s: string;');
  20148. Add(' i: longint;');
  20149. Add(' v: jsvalue;');
  20150. Add('begin');
  20151. Add(' arr[2]:=s;');
  20152. Add(' arr.items[3]:=s;');
  20153. Add(' arr.numbers[4]:=i;');
  20154. Add(' with arr do items[5]:=i;');
  20155. Add(' with arr do numbers[6]:=i;');
  20156. ConvertProgram;
  20157. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  20158. LinesToStr([ // statements
  20159. 'this.Arr = null;',
  20160. 'this.s = "";',
  20161. 'this.i = 0;',
  20162. 'this.v = undefined;',
  20163. '']),
  20164. LinesToStr([ // $mod.$main
  20165. '$mod.Arr[2] = $mod.s;',
  20166. '$mod.Arr[3] = $mod.s;',
  20167. '$mod.Arr[4] = $mod.i;',
  20168. 'var $with = $mod.Arr;',
  20169. '$with[5] = $mod.i;',
  20170. 'var $with1 = $mod.Arr;',
  20171. '$with1[6] = $mod.i;',
  20172. '']));
  20173. end;
  20174. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  20175. begin
  20176. StartProgram(false);
  20177. Add('{$modeswitch externalclass}');
  20178. Add('type');
  20179. Add(' TJSArray = class external name ''Array2''');
  20180. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  20181. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20182. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  20183. Add(' end;');
  20184. Add('var');
  20185. Add(' Arr: tjsarray;');
  20186. Add(' i: longint;');
  20187. Add(' IntArr: array of longint;');
  20188. Add(' v: jsvalue;');
  20189. Add('begin');
  20190. Add(' v:=arr.items[i];');
  20191. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  20192. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  20193. ConvertProgram;
  20194. CheckSource('TestExternalClass_BracketAccessor_Index',
  20195. LinesToStr([ // statements
  20196. 'this.Arr = null;',
  20197. 'this.i = 0;',
  20198. 'this.IntArr = [];',
  20199. 'this.v = undefined;',
  20200. '']),
  20201. LinesToStr([ // $mod.$main
  20202. '$mod.v = $mod.Arr[$mod.i];',
  20203. '$mod.Arr[rtl.trunc($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  20204. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  20205. '']));
  20206. end;
  20207. procedure TTestModule.TestExternalClass_ForInJSObject;
  20208. begin
  20209. StartProgram(false);
  20210. Add([
  20211. '{$modeswitch externalclass}',
  20212. 'type',
  20213. ' TJSObject = class external name ''Object''',
  20214. ' end;',
  20215. 'var',
  20216. ' o: TJSObject;',
  20217. ' key: string;',
  20218. 'begin',
  20219. ' for key in o do',
  20220. ' if key=''abc'' then ;',
  20221. '']);
  20222. ConvertProgram;
  20223. CheckSource('TestExternalClass_ForInJSObject',
  20224. LinesToStr([ // statements
  20225. 'this.o = null;',
  20226. 'this.key = "";',
  20227. '']),
  20228. LinesToStr([ // $mod.$main
  20229. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  20230. '']));
  20231. end;
  20232. procedure TTestModule.TestExternalClass_ForInJSArray;
  20233. begin
  20234. StartProgram(false);
  20235. Add([
  20236. '{$modeswitch externalclass}',
  20237. 'type',
  20238. ' TJSInt8Array = class external name ''Int8Array''',
  20239. ' private',
  20240. ' flength: NativeInt external name ''length'';',
  20241. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  20242. ' public',
  20243. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  20244. ' property Length: NativeInt read flength;',
  20245. ' end;',
  20246. 'var',
  20247. ' a: TJSInt8Array;',
  20248. ' value: shortint;',
  20249. 'begin',
  20250. ' for value in a do',
  20251. ' if value=3 then ;',
  20252. '']);
  20253. ConvertProgram;
  20254. CheckSource('TestExternalClass_ForInJSArray',
  20255. LinesToStr([ // statements
  20256. 'this.a = null;',
  20257. 'this.value = 0;',
  20258. '']),
  20259. LinesToStr([ // $mod.$main
  20260. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  20261. ' $mod.value = $in[$l];',
  20262. ' if ($mod.value === 3) ;',
  20263. '};',
  20264. '']));
  20265. end;
  20266. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  20267. begin
  20268. AddModuleWithIntfImplSrc('unit2.pas',
  20269. LinesToStr([
  20270. '{$modeswitch externalclass}',
  20271. 'type',
  20272. ' TJSBufferSource = class external name ''BufferSource''',
  20273. ' end;',
  20274. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  20275. '']),
  20276. '');
  20277. AddModuleWithIntfImplSrc('unit3.pas',
  20278. LinesToStr([
  20279. '{$modeswitch externalclass}',
  20280. 'type',
  20281. ' TJSBufferSource = class external name ''BufferSource''',
  20282. ' end;',
  20283. '']),
  20284. '');
  20285. StartUnit(true);
  20286. Add([
  20287. 'interface',
  20288. 'uses unit2, unit3;',
  20289. 'procedure DoSome(s: TJSBufferSource);',
  20290. 'implementation',
  20291. 'procedure DoSome(s: TJSBufferSource);',
  20292. 'begin',
  20293. ' DoIt(s);',
  20294. 'end;',
  20295. '']);
  20296. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  20297. nIncompatibleTypeArgNo);
  20298. ConvertUnit;
  20299. end;
  20300. procedure TTestModule.TestExternalClass_NestedConstructor;
  20301. begin
  20302. StartProgram(false);
  20303. Add([
  20304. '{$modeswitch externalclass}',
  20305. 'type',
  20306. ' TJSObject = class external name ''Object''',
  20307. ' type TBird = class external name ''Bird''',
  20308. ' type TWing = class external name ''Wing''',
  20309. ' constructor New;',
  20310. ' constructor Create(w: word = 3);',
  20311. ' end;',
  20312. ' end;',
  20313. ' end;',
  20314. 'var',
  20315. ' w: TJSObject.TBird.TWing;',
  20316. 'begin',
  20317. ' w:=tjsobject.tbird.twing.new;',
  20318. ' w:=tjsobject.tbird.twing.new();',
  20319. ' w:=tjsobject.tbird.twing.create;',
  20320. ' w:=tjsobject.tbird.twing.create(4);',
  20321. ' with tjsobject do begin',
  20322. ' w:=tbird.twing.new;',
  20323. ' w:=tbird.twing.new();',
  20324. ' w:=tbird.twing.create;',
  20325. ' w:=tbird.twing.create(11);',
  20326. ' end;',
  20327. ' with tjsobject.tbird do begin',
  20328. ' w:=twing.new;',
  20329. ' w:=twing.new();',
  20330. ' w:=twing.create;',
  20331. ' w:=twing.create(21);',
  20332. ' end;',
  20333. ' with tjsobject.tbird.twing do begin',
  20334. ' w:=new;',
  20335. ' w:=new();',
  20336. ' w:=create;',
  20337. ' w:=create(31);',
  20338. ' end;',
  20339. '']);
  20340. ConvertProgram;
  20341. CheckSource('TestExternalClass_NestedConstructor',
  20342. LinesToStr([ // statements
  20343. 'this.w = null;',
  20344. '']),
  20345. LinesToStr([ // $mod.$main
  20346. '$mod.w = new Object.Bird.Wing();',
  20347. '$mod.w = new Object.Bird.Wing();',
  20348. '$mod.w = new Object.Bird.Wing.Create();',
  20349. '$mod.w = new Object.Bird.Wing.Create(4);',
  20350. '$mod.w = new Object.Bird.Wing();',
  20351. '$mod.w = new Object.Bird.Wing();',
  20352. '$mod.w = new Object.Bird.Wing.Create();',
  20353. '$mod.w = new Object.Bird.Wing.Create(11);',
  20354. 'var $with = Object.Bird;',
  20355. '$mod.w = new Object.Bird.Wing();',
  20356. '$mod.w = new Object.Bird.Wing();',
  20357. '$mod.w = new Object.Bird.Wing.Create();',
  20358. '$mod.w = new Object.Bird.Wing.Create(21);',
  20359. 'var $with1 = Object.Bird.Wing;',
  20360. '$mod.w = new $with1();',
  20361. '$mod.w = new $with1();',
  20362. '$mod.w = new Object.Bird.Wing.Create();',
  20363. '$mod.w = new Object.Bird.Wing.Create(31);',
  20364. '']));
  20365. end;
  20366. procedure TTestModule.TestClassInterface_Corba;
  20367. begin
  20368. StartProgram(false);
  20369. Add([
  20370. '{$interfaces corba}',
  20371. 'type',
  20372. ' IUnknown = interface;',
  20373. ' IUnknown = interface',
  20374. ' [''{00000000-0000-0000-C000-000000000046}'']',
  20375. ' end;',
  20376. ' IInterface = IUnknown;',
  20377. ' IBird = interface(IInterface)',
  20378. ' function GetSize: longint;',
  20379. ' procedure SetSize(i: longint);',
  20380. ' property Size: longint read GetSize write SetSize;',
  20381. ' procedure DoIt(i: longint);',
  20382. ' end;',
  20383. ' TObject = class',
  20384. ' end;',
  20385. ' TBird = class(TObject,IBird)',
  20386. ' function GetSize: longint; virtual; abstract;',
  20387. ' procedure SetSize(i: longint); virtual; abstract;',
  20388. ' procedure DoIt(i: longint); virtual; abstract;',
  20389. ' end;',
  20390. 'var',
  20391. ' BirdIntf: IBird;',
  20392. 'begin',
  20393. ' BirdIntf.Size:=BirdIntf.Size;',
  20394. '']);
  20395. ConvertProgram;
  20396. CheckSource('TestClassInterface_Corba',
  20397. LinesToStr([ // statements
  20398. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  20399. 'rtl.createInterface(this, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], this.IUnknown);',
  20400. 'rtl.createClass(this, "TObject", null, function () {',
  20401. ' this.$init = function () {',
  20402. ' };',
  20403. ' this.$final = function () {',
  20404. ' };',
  20405. '});',
  20406. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20407. ' rtl.addIntf(this, $mod.IBird);',
  20408. '});',
  20409. 'this.BirdIntf = null;',
  20410. '']),
  20411. LinesToStr([ // $mod.$main
  20412. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  20413. '']));
  20414. end;
  20415. procedure TTestModule.TestClassInterface_Corba_ProcExternalFail;
  20416. begin
  20417. StartProgram(false);
  20418. Add([
  20419. '{$interfaces corba}',
  20420. 'type',
  20421. ' IUnknown = interface',
  20422. ' procedure DoIt; external name ''foo'';',
  20423. ' end;',
  20424. 'begin']);
  20425. SetExpectedParserError(
  20426. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  20427. nParserNoFieldsAllowed);
  20428. ConvertProgram;
  20429. end;
  20430. procedure TTestModule.TestClassInterface_Corba_Overloads;
  20431. begin
  20432. StartProgram(false);
  20433. Add([
  20434. '{$interfaces corba}',
  20435. 'type',
  20436. ' integer = longint;',
  20437. ' IUnknown = interface',
  20438. ' procedure DoIt(i: integer);',
  20439. ' procedure DoIt(s: string);',
  20440. ' end;',
  20441. ' IBird = interface(IUnknown)',
  20442. ' procedure DoIt(b: boolean); overload;',
  20443. ' end;',
  20444. ' TObject = class',
  20445. ' end;',
  20446. ' TBird = class(TObject,IBird)',
  20447. ' procedure DoIt(o: TObject);',
  20448. ' procedure DoIt(s: string);',
  20449. ' procedure DoIt(i: integer);',
  20450. ' procedure DoIt(b: boolean);',
  20451. ' end;',
  20452. 'procedure TBird.DoIt(o: TObject); begin end;',
  20453. 'procedure TBird.DoIt(s: string); begin end;',
  20454. 'procedure TBird.DoIt(i: integer); begin end;',
  20455. 'procedure TBird.DoIt(b: boolean); begin end;',
  20456. 'var',
  20457. ' BirdIntf: IBird;',
  20458. 'begin',
  20459. ' BirdIntf.DoIt(3);',
  20460. ' BirdIntf.DoIt(''abc'');',
  20461. ' BirdIntf.DoIt(true);',
  20462. '']);
  20463. ConvertProgram;
  20464. CheckSource('TestClassInterface_Overloads',
  20465. LinesToStr([ // statements
  20466. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  20467. 'rtl.createInterface(this, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], this.IUnknown);',
  20468. 'rtl.createClass(this, "TObject", null, function () {',
  20469. ' this.$init = function () {',
  20470. ' };',
  20471. ' this.$final = function () {',
  20472. ' };',
  20473. '});',
  20474. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20475. ' this.DoIt = function (o) {',
  20476. ' };',
  20477. ' this.DoIt$1 = function (s) {',
  20478. ' };',
  20479. ' this.DoIt$2 = function (i) {',
  20480. ' };',
  20481. ' this.DoIt$3 = function (b) {',
  20482. ' };',
  20483. ' rtl.addIntf(this, $mod.IBird, {',
  20484. ' DoIt$2: "DoIt$3",',
  20485. ' DoIt: "DoIt$2"',
  20486. ' });',
  20487. '});',
  20488. 'this.BirdIntf = null;',
  20489. '']),
  20490. LinesToStr([ // $mod.$main
  20491. '$mod.BirdIntf.DoIt(3);',
  20492. '$mod.BirdIntf.DoIt$1("abc");',
  20493. '$mod.BirdIntf.DoIt$2(true);',
  20494. '']));
  20495. end;
  20496. procedure TTestModule.TestClassInterface_Corba_DuplicateGUIInIntfListFail;
  20497. begin
  20498. StartProgram(false);
  20499. Add([
  20500. '{$interfaces corba}',
  20501. 'type',
  20502. ' IBird = interface',
  20503. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20504. ' end;',
  20505. ' IDog = interface',
  20506. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20507. ' end;',
  20508. ' TObject = class(IBird,IDog)',
  20509. ' end;',
  20510. 'begin']);
  20511. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  20512. nDuplicateGUIDXInYZ);
  20513. ConvertProgram;
  20514. end;
  20515. procedure TTestModule.TestClassInterface_Corba_DuplicateGUIInAncestorFail;
  20516. begin
  20517. StartProgram(false);
  20518. Add([
  20519. '{$interfaces corba}',
  20520. 'type',
  20521. ' IAnimal = interface',
  20522. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20523. ' end;',
  20524. ' IBird = interface(IAnimal)',
  20525. ' end;',
  20526. ' IHawk = interface(IBird)',
  20527. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20528. ' end;',
  20529. 'begin']);
  20530. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  20531. nDuplicateGUIDXInYZ);
  20532. ConvertProgram;
  20533. end;
  20534. procedure TTestModule.TestClassInterface_Corba_AncestorImpl;
  20535. begin
  20536. StartProgram(false);
  20537. Add([
  20538. '{$interfaces corba}',
  20539. 'type',
  20540. ' integer = longint;',
  20541. ' IUnknown = interface',
  20542. ' procedure DoIt(i: integer);',
  20543. ' end;',
  20544. ' IBird = interface',
  20545. ' procedure Fly(i: integer);',
  20546. ' end;',
  20547. ' TObject = class(IUnknown)',
  20548. ' procedure DoIt(i: integer);',
  20549. ' end;',
  20550. ' TBird = class(IBird)',
  20551. ' procedure Fly(i: integer);',
  20552. ' end;',
  20553. 'procedure TObject.DoIt(i: integer); begin end;',
  20554. 'procedure TBird.Fly(i: integer); begin end;',
  20555. 'begin',
  20556. '']);
  20557. ConvertProgram;
  20558. CheckSource('TestClassInterface_Corba_AncestorImpl',
  20559. LinesToStr([ // statements
  20560. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  20561. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  20562. 'rtl.createClass(this, "TObject", null, function () {',
  20563. ' this.$init = function () {',
  20564. ' };',
  20565. ' this.$final = function () {',
  20566. ' };',
  20567. ' this.DoIt = function (i) {',
  20568. ' };',
  20569. ' rtl.addIntf(this, $mod.IUnknown);',
  20570. '});',
  20571. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20572. ' this.Fly = function (i) {',
  20573. ' };',
  20574. ' rtl.addIntf(this, $mod.IBird);',
  20575. ' rtl.addIntf(this, $mod.IUnknown);',
  20576. '});',
  20577. '']),
  20578. LinesToStr([ // $mod.$main
  20579. '']));
  20580. end;
  20581. procedure TTestModule.TestClassInterface_Corba_ImplReintroduce;
  20582. begin
  20583. StartProgram(false);
  20584. Add([
  20585. '{$interfaces corba}',
  20586. 'type',
  20587. ' integer = longint;',
  20588. ' IBird = interface',
  20589. ' procedure DoIt(i: integer);',
  20590. ' end;',
  20591. ' TObject = class',
  20592. ' procedure DoIt(i: integer);',
  20593. ' end;',
  20594. ' TBird = class(IBird)',
  20595. ' procedure DoIt(i: integer); virtual; reintroduce;',
  20596. ' end;',
  20597. 'procedure TObject.DoIt(i: integer); begin end;',
  20598. 'procedure TBird.DoIt(i: integer); begin end;',
  20599. 'begin',
  20600. '']);
  20601. ConvertProgram;
  20602. CheckSource('TestClassInterface_Corba_ImplReintroduce',
  20603. LinesToStr([ // statements
  20604. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  20605. 'rtl.createClass(this, "TObject", null, function () {',
  20606. ' this.$init = function () {',
  20607. ' };',
  20608. ' this.$final = function () {',
  20609. ' };',
  20610. ' this.DoIt = function (i) {',
  20611. ' };',
  20612. '});',
  20613. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20614. ' this.DoIt$1 = function (i) {',
  20615. ' };',
  20616. ' rtl.addIntf(this, $mod.IBird, {',
  20617. ' DoIt: "DoIt$1"',
  20618. ' });',
  20619. '});',
  20620. '']),
  20621. LinesToStr([ // $mod.$main
  20622. '']));
  20623. end;
  20624. procedure TTestModule.TestClassInterface_Corba_MethodResolution;
  20625. begin
  20626. StartProgram(false);
  20627. Add([
  20628. '{$interfaces corba}',
  20629. 'type',
  20630. ' IUnknown = interface',
  20631. ' procedure Walk(i: longint);',
  20632. ' end;',
  20633. ' IBird = interface(IUnknown)',
  20634. ' procedure Walk(b: boolean); overload;',
  20635. ' procedure Fly(s: string);',
  20636. ' end;',
  20637. ' TObject = class',
  20638. ' end;',
  20639. ' TBird = class(TObject,IBird)',
  20640. ' procedure IBird.Fly = Move;',
  20641. ' procedure IBird.Walk = Hop;',
  20642. ' procedure Hop(i: longint);',
  20643. ' procedure Move(s: string);',
  20644. ' procedure Hop(b: boolean);',
  20645. ' end;',
  20646. 'procedure TBird.Move(s: string); begin end;',
  20647. 'procedure TBird.Hop(i: longint); begin end;',
  20648. 'procedure TBird.Hop(b: boolean); begin end;',
  20649. 'var',
  20650. ' BirdIntf: IBird;',
  20651. 'begin',
  20652. ' BirdIntf.Walk(3);',
  20653. ' BirdIntf.Walk(true);',
  20654. ' BirdIntf.Fly(''abc'');',
  20655. '']);
  20656. ConvertProgram;
  20657. CheckSource('TestClassInterface_Corba_MethodResolution',
  20658. LinesToStr([ // statements
  20659. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  20660. 'rtl.createInterface(this, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], this.IUnknown);',
  20661. 'rtl.createClass(this, "TObject", null, function () {',
  20662. ' this.$init = function () {',
  20663. ' };',
  20664. ' this.$final = function () {',
  20665. ' };',
  20666. '});',
  20667. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20668. ' this.Hop = function (i) {',
  20669. ' };',
  20670. ' this.Move = function (s) {',
  20671. ' };',
  20672. ' this.Hop$1 = function (b) {',
  20673. ' };',
  20674. ' rtl.addIntf(this, $mod.IBird, {',
  20675. ' Walk$1: "Hop$1",',
  20676. ' Fly: "Move",',
  20677. ' Walk: "Hop"',
  20678. ' });',
  20679. '});',
  20680. 'this.BirdIntf = null;',
  20681. '']),
  20682. LinesToStr([ // $mod.$main
  20683. '$mod.BirdIntf.Walk(3);',
  20684. '$mod.BirdIntf.Walk$1(true);',
  20685. '$mod.BirdIntf.Fly("abc");',
  20686. '']));
  20687. end;
  20688. procedure TTestModule.TestClassInterface_COM_AncestorMoreInterfaces;
  20689. begin
  20690. StartProgram(false);
  20691. Add([
  20692. '{$interfaces com}',
  20693. 'type',
  20694. ' IUnknown = interface',
  20695. ' function _AddRef: longint;',
  20696. ' procedure Walk;',
  20697. ' end;',
  20698. ' IBird = interface end;',
  20699. ' IDog = interface end;',
  20700. ' TObject = class(IBird,IDog)',
  20701. ' function _AddRef: longint; virtual; abstract;',
  20702. ' procedure Walk; virtual; abstract;',
  20703. ' end;',
  20704. ' TBird = class(IUnknown)',
  20705. ' end;',
  20706. 'begin',
  20707. '']);
  20708. ConvertProgram;
  20709. CheckSource('TestClassInterface_COM_AncestorMoreInterfaces',
  20710. LinesToStr([ // statements
  20711. 'rtl.createInterface(this, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  20712. 'rtl.createInterface(this, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], this.IUnknown);',
  20713. 'rtl.createInterface(this, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], this.IUnknown);',
  20714. 'rtl.createClass(this, "TObject", null, function () {',
  20715. ' this.$init = function () {',
  20716. ' };',
  20717. ' this.$final = function () {',
  20718. ' };',
  20719. ' rtl.addIntf(this, $mod.IBird);',
  20720. ' rtl.addIntf(this, $mod.IDog);',
  20721. '});',
  20722. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20723. ' rtl.addIntf(this, $mod.IUnknown);',
  20724. ' rtl.addIntf(this, $mod.IBird);',
  20725. ' rtl.addIntf(this, $mod.IDog);',
  20726. '});',
  20727. '']),
  20728. LinesToStr([ // $mod.$main
  20729. '']));
  20730. end;
  20731. procedure TTestModule.TestClassInterface_Corba_MethodOverride;
  20732. begin
  20733. StartProgram(false);
  20734. Add([
  20735. '{$interfaces corba}',
  20736. 'type',
  20737. ' IUnknown = interface',
  20738. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  20739. ' procedure Go;',
  20740. ' end;',
  20741. ' TObject = class(IUnknown)',
  20742. ' procedure Go; virtual; abstract;',
  20743. ' end;',
  20744. ' TBird = class',
  20745. ' procedure Go; override;',
  20746. ' end;',
  20747. ' TCat = class(TObject)',
  20748. ' procedure Go; override;',
  20749. ' end;',
  20750. ' TDog = class(TObject, IUnknown)',
  20751. ' procedure Go; override;',
  20752. ' end;',
  20753. 'procedure TBird.Go; begin end;',
  20754. 'procedure TCat.Go; begin end;',
  20755. 'procedure TDog.Go; begin end;',
  20756. 'begin',
  20757. '']);
  20758. ConvertProgram;
  20759. CheckSource('TestClassInterface_Corba_MethodOverride',
  20760. LinesToStr([ // statements
  20761. 'rtl.createInterface(this, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  20762. 'rtl.createClass(this, "TObject", null, function () {',
  20763. ' this.$init = function () {',
  20764. ' };',
  20765. ' this.$final = function () {',
  20766. ' };',
  20767. ' rtl.addIntf(this, $mod.IUnknown);',
  20768. '});',
  20769. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20770. ' this.Go = function () {',
  20771. ' };',
  20772. ' rtl.addIntf(this, $mod.IUnknown);',
  20773. '});',
  20774. 'rtl.createClass(this, "TCat", this.TObject, function () {',
  20775. ' this.Go = function () {',
  20776. ' };',
  20777. ' rtl.addIntf(this, $mod.IUnknown);',
  20778. '});',
  20779. 'rtl.createClass(this, "TDog", this.TObject, function () {',
  20780. ' this.Go = function () {',
  20781. ' };',
  20782. ' rtl.addIntf(this, $mod.IUnknown);',
  20783. '});',
  20784. '']),
  20785. LinesToStr([ // $mod.$main
  20786. '']));
  20787. end;
  20788. procedure TTestModule.TestClassInterface_Corba_Delegation;
  20789. begin
  20790. StartProgram(false);
  20791. Add([
  20792. '{$interfaces corba}',
  20793. 'type',
  20794. ' IUnknown = interface',
  20795. ' end;',
  20796. ' IBird = interface(IUnknown)',
  20797. ' procedure Fly(s: string);',
  20798. ' end;',
  20799. ' IEagle = interface(IBird)',
  20800. ' end;',
  20801. ' IDove = interface(IBird)',
  20802. ' end;',
  20803. ' ISwallow = interface(IBird)',
  20804. ' end;',
  20805. ' TObject = class',
  20806. ' end;',
  20807. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20808. ' procedure Fly(s: string); virtual; abstract;',
  20809. ' end;',
  20810. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20811. ' FBirdIntf: IBird;',
  20812. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  20813. ' function GetEagleIntf: IEagle; virtual; abstract;',
  20814. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20815. ' FDoveObj: TBird;',
  20816. ' property DoveObj: TBird read FDoveObj implements IDove;',
  20817. ' function GetSwallowObj: TBird; virtual; abstract;',
  20818. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20819. ' end;',
  20820. 'begin',
  20821. '']);
  20822. ConvertProgram;
  20823. CheckSource('TestClassInterface_Corba_Delegation',
  20824. LinesToStr([ // statements
  20825. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20826. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  20827. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  20828. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  20829. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  20830. 'rtl.createClass(this, "TObject", null, function () {',
  20831. ' this.$init = function () {',
  20832. ' };',
  20833. ' this.$final = function () {',
  20834. ' };',
  20835. '});',
  20836. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20837. ' rtl.addIntf(this, $mod.IBird);',
  20838. ' rtl.addIntf(this, $mod.IEagle);',
  20839. ' rtl.addIntf(this, $mod.IDove);',
  20840. ' rtl.addIntf(this, $mod.ISwallow);',
  20841. '});',
  20842. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20843. ' this.$init = function () {',
  20844. ' $mod.TObject.$init.call(this);',
  20845. ' this.FBirdIntf = null;',
  20846. ' this.FDoveObj = null;',
  20847. ' };',
  20848. ' this.$final = function () {',
  20849. ' this.FBirdIntf = undefined;',
  20850. ' this.FDoveObj = undefined;',
  20851. ' $mod.TObject.$final.call(this);',
  20852. ' };',
  20853. ' this.$intfmaps = {',
  20854. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  20855. ' return this.FBirdIntf;',
  20856. ' },',
  20857. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  20858. ' return this.GetEagleIntf();',
  20859. ' },',
  20860. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  20861. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  20862. ' },',
  20863. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  20864. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20865. ' }',
  20866. ' };',
  20867. '});',
  20868. '']),
  20869. LinesToStr([ // $mod.$main
  20870. '']));
  20871. end;
  20872. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  20873. begin
  20874. StartProgram(false);
  20875. Add([
  20876. '{$interfaces corba}',
  20877. 'type',
  20878. ' IUnknown = interface',
  20879. ' end;',
  20880. ' IBird = interface(IUnknown)',
  20881. ' procedure Fly(s: string);',
  20882. ' end;',
  20883. ' IEagle = interface(IBird)',
  20884. ' end;',
  20885. ' IDove = interface(IBird)',
  20886. ' end;',
  20887. ' ISwallow = interface(IBird)',
  20888. ' end;',
  20889. ' TObject = class',
  20890. ' end;',
  20891. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20892. ' procedure Fly(s: string); virtual; abstract;',
  20893. ' end;',
  20894. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20895. ' private',
  20896. ' class var FBirdIntf: IBird;',
  20897. ' class var FDoveObj: TBird;',
  20898. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  20899. ' class function GetSwallowObj: TBird; virtual; abstract;',
  20900. ' protected',
  20901. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  20902. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20903. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  20904. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20905. ' end;',
  20906. 'begin',
  20907. '']);
  20908. ConvertProgram;
  20909. CheckSource('TestClassInterface_Corba_DelegationStatic',
  20910. LinesToStr([ // statements
  20911. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20912. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  20913. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  20914. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  20915. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  20916. 'rtl.createClass(this, "TObject", null, function () {',
  20917. ' this.$init = function () {',
  20918. ' };',
  20919. ' this.$final = function () {',
  20920. ' };',
  20921. '});',
  20922. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20923. ' rtl.addIntf(this, $mod.IBird);',
  20924. ' rtl.addIntf(this, $mod.IEagle);',
  20925. ' rtl.addIntf(this, $mod.IDove);',
  20926. ' rtl.addIntf(this, $mod.ISwallow);',
  20927. '});',
  20928. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20929. ' this.FBirdIntf = null;',
  20930. ' this.FDoveObj = null;',
  20931. ' this.$intfmaps = {',
  20932. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  20933. ' return this.FBirdIntf;',
  20934. ' },',
  20935. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  20936. ' return this.GetEagleIntf();',
  20937. ' },',
  20938. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  20939. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  20940. ' },',
  20941. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  20942. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20943. ' }',
  20944. ' };',
  20945. '});',
  20946. '']),
  20947. LinesToStr([ // $mod.$main
  20948. '']));
  20949. end;
  20950. procedure TTestModule.TestClassInterface_Corba_Operators;
  20951. begin
  20952. StartProgram(false);
  20953. Add([
  20954. '{$interfaces corba}',
  20955. 'type',
  20956. ' IUnknown = interface',
  20957. ' end;',
  20958. ' IBird = interface(IUnknown)',
  20959. ' function GetItems(Index: longint): longint;',
  20960. ' procedure SetItems(Index: longint; Value: longint);',
  20961. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  20962. ' end;',
  20963. ' TObject = class',
  20964. ' end;',
  20965. ' TBird = class(TObject,IBird)',
  20966. ' function GetItems(Index: longint): longint; virtual; abstract;',
  20967. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  20968. ' end;',
  20969. 'var',
  20970. ' IntfVar: IBird = nil;',
  20971. ' IntfVar2: IBird;',
  20972. ' ObjVar: TBird;',
  20973. ' v: JSValue;',
  20974. 'begin',
  20975. ' IntfVar:=nil;',
  20976. ' IntfVar[3]:=IntfVar[4];',
  20977. ' if Assigned(IntfVar) then ;',
  20978. ' IntfVar:=IntfVar2;',
  20979. ' IntfVar:=ObjVar;',
  20980. ' if IntfVar=IntfVar2 then ;',
  20981. ' if IntfVar<>IntfVar2 then ;',
  20982. ' if IntfVar is IBird then ;',
  20983. ' if IntfVar is TBird then ;',
  20984. ' if ObjVar is IBird then ;',
  20985. ' IntfVar:=IntfVar2 as IBird;',
  20986. ' ObjVar:=IntfVar2 as TBird;',
  20987. ' IntfVar:=ObjVar as IBird;',
  20988. ' IntfVar:=IBird(IntfVar2);',
  20989. ' ObjVar:=TBird(IntfVar);',
  20990. ' IntfVar:=IBird(ObjVar);',
  20991. ' v:=IntfVar;',
  20992. ' IntfVar:=IBird(v);',
  20993. ' if v is IBird then ;',
  20994. ' v:=JSValue(IntfVar);',
  20995. ' v:=IBird;',
  20996. '']);
  20997. ConvertProgram;
  20998. CheckSource('TestClassInterface_Corba_Operators',
  20999. LinesToStr([ // statements
  21000. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21001. 'rtl.createInterface(this, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], this.IUnknown);',
  21002. 'rtl.createClass(this, "TObject", null, function () {',
  21003. ' this.$init = function () {',
  21004. ' };',
  21005. ' this.$final = function () {',
  21006. ' };',
  21007. '});',
  21008. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21009. ' rtl.addIntf(this, $mod.IBird);',
  21010. '});',
  21011. 'this.IntfVar = null;',
  21012. 'this.IntfVar2 = null;',
  21013. 'this.ObjVar = null;',
  21014. 'this.v = undefined;',
  21015. '']),
  21016. LinesToStr([ // $mod.$main
  21017. '$mod.IntfVar = null;',
  21018. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  21019. 'if ($mod.IntfVar != null) ;',
  21020. '$mod.IntfVar = $mod.IntfVar2;',
  21021. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  21022. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  21023. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  21024. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  21025. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  21026. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  21027. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  21028. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  21029. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  21030. '$mod.IntfVar = $mod.IntfVar2;',
  21031. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  21032. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  21033. '$mod.v = $mod.IntfVar;',
  21034. '$mod.IntfVar = rtl.getObject($mod.v);',
  21035. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  21036. '$mod.v = $mod.IntfVar;',
  21037. '$mod.v = $mod.IBird;',
  21038. '']));
  21039. end;
  21040. procedure TTestModule.TestClassInterface_Corba_Args;
  21041. begin
  21042. StartProgram(false);
  21043. Add([
  21044. '{$interfaces corba}',
  21045. 'type',
  21046. ' IUnknown = interface',
  21047. ' end;',
  21048. ' IBird = interface(IUnknown)',
  21049. ' end;',
  21050. ' TObject = class',
  21051. ' end;',
  21052. ' TBird = class(TObject,IBird)',
  21053. ' end;',
  21054. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  21055. 'begin',
  21056. ' DoIt(i,i,i);',
  21057. 'end;',
  21058. 'procedure Change(var i: IBird; out j: IBird);',
  21059. 'begin',
  21060. ' DoIt(i,i,i);',
  21061. ' Change(i,i);',
  21062. 'end;',
  21063. 'var',
  21064. ' i: IBird;',
  21065. ' o: TBird;',
  21066. 'begin',
  21067. ' DoIt(i,i,i);',
  21068. ' Change(i,i);',
  21069. ' DoIt(o,o,o);',
  21070. '']);
  21071. ConvertProgram;
  21072. CheckSource('TestClassInterface_Corba_Args',
  21073. LinesToStr([ // statements
  21074. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21075. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  21076. 'rtl.createClass(this, "TObject", null, function () {',
  21077. ' this.$init = function () {',
  21078. ' };',
  21079. ' this.$final = function () {',
  21080. ' };',
  21081. '});',
  21082. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21083. ' rtl.addIntf(this, $mod.IBird);',
  21084. '});',
  21085. 'this.DoIt = function (u, i, j) {',
  21086. ' $mod.DoIt({',
  21087. ' get: function () {',
  21088. ' return i;',
  21089. ' },',
  21090. ' set: function (v) {',
  21091. ' i = v;',
  21092. ' }',
  21093. ' }, i, i);',
  21094. '};',
  21095. 'this.Change = function (i, j) {',
  21096. ' $mod.DoIt(i, i.get(), i.get());',
  21097. ' $mod.Change(i, i);',
  21098. '};',
  21099. 'this.i = null;',
  21100. 'this.o = null;',
  21101. '']),
  21102. LinesToStr([ // $mod.$main
  21103. '$mod.DoIt({',
  21104. ' p: $mod,',
  21105. ' get: function () {',
  21106. ' return this.p.i;',
  21107. ' },',
  21108. ' set: function (v) {',
  21109. ' this.p.i = v;',
  21110. ' }',
  21111. '}, $mod.i, $mod.i);',
  21112. '$mod.Change({',
  21113. ' p: $mod,',
  21114. ' get: function () {',
  21115. ' return this.p.i;',
  21116. ' },',
  21117. ' set: function (v) {',
  21118. ' this.p.i = v;',
  21119. ' }',
  21120. '}, {',
  21121. ' p: $mod,',
  21122. ' get: function () {',
  21123. ' return this.p.i;',
  21124. ' },',
  21125. ' set: function (v) {',
  21126. ' this.p.i = v;',
  21127. ' }',
  21128. '});',
  21129. '$mod.DoIt({',
  21130. ' p: $mod,',
  21131. ' get: function () {',
  21132. ' return this.p.o;',
  21133. ' },',
  21134. ' set: function (v) {',
  21135. ' this.p.o = v;',
  21136. ' }',
  21137. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  21138. '']));
  21139. end;
  21140. procedure TTestModule.TestClassInterface_Corba_ForIn;
  21141. begin
  21142. StartProgram(false);
  21143. Add([
  21144. '{$interfaces corba}',
  21145. 'type',
  21146. ' IUnknown = interface end;',
  21147. ' TObject = class',
  21148. ' Id: longint;',
  21149. ' end;',
  21150. ' IEnumerator = interface(IUnknown)',
  21151. ' function GetCurrent: TObject;',
  21152. ' function MoveNext: Boolean;',
  21153. ' property Current: TObject read GetCurrent;',
  21154. ' end;',
  21155. ' IEnumerable = interface(IUnknown)',
  21156. ' function GetEnumerator: IEnumerator;',
  21157. ' end;',
  21158. 'var',
  21159. ' o: TObject;',
  21160. ' i: IEnumerable;',
  21161. 'begin',
  21162. ' for o in i do o.Id:=3;',
  21163. '']);
  21164. ConvertProgram;
  21165. CheckSource('TestClassInterface_Corba_ForIn',
  21166. LinesToStr([ // statements
  21167. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21168. 'rtl.createClass(this, "TObject", null, function () {',
  21169. ' this.$init = function () {',
  21170. ' this.Id = 0;',
  21171. ' };',
  21172. ' this.$final = function () {',
  21173. ' };',
  21174. '});',
  21175. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  21176. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  21177. 'this.o = null;',
  21178. 'this.i = null;',
  21179. '']),
  21180. LinesToStr([ // $mod.$main
  21181. 'var $in = $mod.i.GetEnumerator();',
  21182. 'while ($in.MoveNext()) {',
  21183. ' $mod.o = $in.GetCurrent();',
  21184. ' $mod.o.Id = 3;',
  21185. '};',
  21186. '']));
  21187. end;
  21188. procedure TTestModule.TestClassInterface_Corba_ArrayOfIntf;
  21189. begin
  21190. StartProgram(false);
  21191. Add([
  21192. '{$interfaces corba}',
  21193. 'type',
  21194. ' IUnknown = interface end;',
  21195. ' IBird = interface(IUnknown)',
  21196. ' function Fly(w: word): word;',
  21197. ' end;',
  21198. ' TBirdArray = array of IBird;',
  21199. 'var',
  21200. ' i: IBird;',
  21201. ' a: TBirdArray;',
  21202. 'begin',
  21203. ' SetLength(a,3);',
  21204. ' i:=a[1];',
  21205. ' a[2]:=i;',
  21206. ' for i in a do i.fly(3);',
  21207. '']);
  21208. ConvertProgram;
  21209. CheckSource('TestClassInterface_Corba_ArrayOfIntf',
  21210. LinesToStr([ // statements
  21211. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21212. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  21213. 'this.i = null;',
  21214. 'this.a = [];',
  21215. '']),
  21216. LinesToStr([ // $mod.$main
  21217. '$mod.a = rtl.arraySetLength($mod.a, null, 3);',
  21218. '$mod.i = $mod.a[1];',
  21219. '$mod.a[2] = $mod.i;',
  21220. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  21221. ' $mod.i = $in[$l];',
  21222. ' $mod.i.Fly(3);',
  21223. '};',
  21224. '']));
  21225. end;
  21226. procedure TTestModule.TestClassInterface_COM_AssignVar;
  21227. begin
  21228. StartProgram(false);
  21229. Add([
  21230. '{$interfaces com}',
  21231. 'type',
  21232. ' IUnknown = interface',
  21233. ' function _AddRef: longint;',
  21234. ' function _Release: longint;',
  21235. ' end;',
  21236. ' TObject = class(IUnknown)',
  21237. ' function _AddRef: longint; virtual; abstract;',
  21238. ' function _Release: longint; virtual; abstract;',
  21239. ' end;',
  21240. 'var',
  21241. ' i: IUnknown;',
  21242. 'procedure DoGlobal(o: TObject);',
  21243. 'begin',
  21244. ' i:=nil;',
  21245. ' i:=o;',
  21246. ' i:=i;',
  21247. 'end;',
  21248. 'procedure DoLocal(o: TObject);',
  21249. 'const k: IUnknown = nil;',
  21250. 'var j: IUnknown;',
  21251. 'begin',
  21252. ' k:=o;',
  21253. ' k:=i;',
  21254. ' j:=o;',
  21255. ' j:=i;',
  21256. 'end;',
  21257. 'var o: TObject;',
  21258. 'begin',
  21259. ' i:=nil;',
  21260. ' i:=o;',
  21261. '']);
  21262. ConvertProgram;
  21263. CheckSource('TestClassInterface_COM_AssignVar',
  21264. LinesToStr([ // statements
  21265. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21266. 'rtl.createClass(this, "TObject", null, function () {',
  21267. ' this.$init = function () {',
  21268. ' };',
  21269. ' this.$final = function () {',
  21270. ' };',
  21271. ' rtl.addIntf(this, $mod.IUnknown);',
  21272. '});',
  21273. 'this.i = null;',
  21274. 'this.DoGlobal = function (o) {',
  21275. ' rtl.setIntfP($mod, "i", null);',
  21276. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  21277. ' rtl.setIntfP($mod, "i", $mod.i);',
  21278. '};',
  21279. 'var k = null;',
  21280. 'this.DoLocal = function (o) {',
  21281. ' var j = null;',
  21282. ' try{',
  21283. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21284. ' k = rtl.setIntfL(k, $mod.i);',
  21285. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21286. ' j = rtl.setIntfL(j, $mod.i);',
  21287. ' }finally{',
  21288. ' rtl._Release(j);',
  21289. ' };',
  21290. '};',
  21291. 'this.o = null;',
  21292. '']),
  21293. LinesToStr([ // $mod.$main
  21294. 'rtl.setIntfP($mod, "i", null);',
  21295. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  21296. '']));
  21297. end;
  21298. procedure TTestModule.TestClassInterface_COM_AssignArg;
  21299. begin
  21300. StartProgram(false);
  21301. Add([
  21302. '{$interfaces com}',
  21303. 'type',
  21304. ' IUnknown = interface',
  21305. ' function _AddRef: longint;',
  21306. ' function _Release: longint;',
  21307. ' end;',
  21308. ' TObject = class(IUnknown)',
  21309. ' function _AddRef: longint; virtual; abstract;',
  21310. ' function _Release: longint; virtual; abstract;',
  21311. ' end;',
  21312. 'procedure DoDefault(i, j: IUnknown);',
  21313. 'begin',
  21314. ' i:=nil;',
  21315. ' i:=j;',
  21316. 'end;',
  21317. 'begin',
  21318. '']);
  21319. ConvertProgram;
  21320. CheckSource('TestClassInterface_COM_AssignArg',
  21321. LinesToStr([ // statements
  21322. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21323. 'rtl.createClass(this, "TObject", null, function () {',
  21324. ' this.$init = function () {',
  21325. ' };',
  21326. ' this.$final = function () {',
  21327. ' };',
  21328. ' rtl.addIntf(this, $mod.IUnknown);',
  21329. '});',
  21330. 'this.DoDefault = function (i, j) {',
  21331. ' rtl._AddRef(i);',
  21332. ' rtl._AddRef(j);',
  21333. ' try {',
  21334. ' i = rtl.setIntfL(i, null);',
  21335. ' i = rtl.setIntfL(i, j);',
  21336. ' } finally {',
  21337. ' rtl._Release(i);',
  21338. ' rtl._Release(j);',
  21339. ' };',
  21340. '};',
  21341. '']),
  21342. LinesToStr([ // $mod.$main
  21343. '']));
  21344. end;
  21345. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  21346. begin
  21347. StartProgram(false);
  21348. Add([
  21349. '{$interfaces com}',
  21350. 'type',
  21351. ' IUnknown = interface',
  21352. ' function _AddRef: longint;',
  21353. ' function _Release: longint;',
  21354. ' end;',
  21355. ' TObject = class(IUnknown)',
  21356. ' function _AddRef: longint; virtual; abstract;',
  21357. ' function _Release: longint; virtual; abstract;',
  21358. ' end;',
  21359. 'function DoDefault(i: IUnknown): IUnknown;',
  21360. 'begin',
  21361. ' Result:=i;',
  21362. ' if Result<>nil then exit;',
  21363. 'end;',
  21364. 'var i: IUnknown;',
  21365. 'begin',
  21366. ' DoDefault(i);',
  21367. ' i:=DoDefault(i);',
  21368. '']);
  21369. ConvertProgram;
  21370. CheckSource('TestClassInterface_COM_FunctionResult',
  21371. LinesToStr([ // statements
  21372. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21373. 'rtl.createClass(this, "TObject", null, function () {',
  21374. ' this.$init = function () {',
  21375. ' };',
  21376. ' this.$final = function () {',
  21377. ' };',
  21378. ' rtl.addIntf(this, $mod.IUnknown);',
  21379. '});',
  21380. 'this.DoDefault = function (i) {',
  21381. ' var Result = null;',
  21382. ' rtl._AddRef(i);',
  21383. ' var $ok = false;',
  21384. ' try {',
  21385. ' Result = rtl.setIntfL(Result, i);',
  21386. ' if(Result !== null){',
  21387. ' $ok = true;',
  21388. ' return Result;',
  21389. ' };',
  21390. ' $ok = true;',
  21391. ' } finally {',
  21392. ' rtl._Release(i);',
  21393. ' if(!$ok) rtl._Release(Result);',
  21394. ' };',
  21395. ' return Result;',
  21396. '};',
  21397. 'this.i = null;',
  21398. '']),
  21399. LinesToStr([ // $mod.$main
  21400. 'var $ir = rtl.createIntfRefs();',
  21401. 'try {',
  21402. ' $ir.ref(1, $mod.DoDefault($mod.i));',
  21403. ' rtl.setIntfP($mod, "i", $mod.DoDefault($mod.i), true);',
  21404. '} finally {',
  21405. ' $ir.free();',
  21406. '};',
  21407. '']));
  21408. end;
  21409. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  21410. begin
  21411. StartProgram(false);
  21412. Add([
  21413. '{$interfaces com}',
  21414. 'type',
  21415. ' IUnknown = interface',
  21416. ' function _AddRef: longint;',
  21417. ' function _Release: longint;',
  21418. ' end;',
  21419. ' TObject = class(IUnknown)',
  21420. ' function _AddRef: longint; virtual; abstract;',
  21421. ' function _Release: longint; virtual; abstract;',
  21422. ' function GetIntf: IUnknown; virtual;',
  21423. ' end;',
  21424. ' TMouse = class',
  21425. ' function GetIntf: IUnknown; override;',
  21426. ' end;',
  21427. 'function TObject.GetIntf: IUnknown; begin end;',
  21428. 'function TMouse.GetIntf: IUnknown;',
  21429. 'var i: IUnknown;',
  21430. 'begin',
  21431. ' inherited;',
  21432. ' inherited GetIntf;',
  21433. ' inherited GetIntf();',
  21434. ' Result:=inherited GetIntf;',
  21435. ' Result:=inherited GetIntf();',
  21436. ' i:=inherited GetIntf;',
  21437. ' i:=inherited GetIntf();',
  21438. 'end;',
  21439. 'begin',
  21440. '']);
  21441. ConvertProgram;
  21442. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  21443. LinesToStr([ // statements
  21444. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21445. 'rtl.createClass(this, "TObject", null, function () {',
  21446. ' this.$init = function () {',
  21447. ' };',
  21448. ' this.$final = function () {',
  21449. ' };',
  21450. ' this.GetIntf = function () {',
  21451. ' var Result = null;',
  21452. ' return Result;',
  21453. ' };',
  21454. ' rtl.addIntf(this, $mod.IUnknown);',
  21455. '});',
  21456. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  21457. ' this.GetIntf = function () {',
  21458. ' var Result = null;',
  21459. ' var i = null;',
  21460. ' var $ir = rtl.createIntfRefs();',
  21461. ' var $ok = false;',
  21462. ' try {',
  21463. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  21464. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  21465. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  21466. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  21467. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  21468. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  21469. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  21470. ' $ok = true;',
  21471. ' } finally {',
  21472. ' $ir.free();',
  21473. ' rtl._Release(i);',
  21474. ' if (!$ok) rtl._Release(Result);',
  21475. ' };',
  21476. ' return Result;',
  21477. ' };',
  21478. ' rtl.addIntf(this, $mod.IUnknown);',
  21479. '});',
  21480. '']),
  21481. LinesToStr([ // $mod.$main
  21482. '']));
  21483. end;
  21484. procedure TTestModule.TestClassInterface_COM_FunctionExit;
  21485. begin
  21486. StartProgram(false);
  21487. Add([
  21488. '{$interfaces com}',
  21489. 'type',
  21490. ' IUnknown = interface',
  21491. ' function _AddRef: longint;',
  21492. ' function _Release: longint;',
  21493. ' end;',
  21494. ' TObject = class(IUnknown)',
  21495. ' function _AddRef: longint; virtual; abstract;',
  21496. ' function _Release: longint; virtual; abstract;',
  21497. ' constructor Create;',
  21498. ' end;',
  21499. 'constructor TObject.Create;',
  21500. 'begin',
  21501. 'end;',
  21502. 'function GetIntf: IUnknown;',
  21503. 'var Intf: IUnknown;',
  21504. 'begin',
  21505. ' Intf := TObject.Create;',
  21506. ' Exit(Intf);',
  21507. 'end;',
  21508. 'begin',
  21509. '']);
  21510. ConvertProgram;
  21511. CheckSource('TestClassInterface_COM_FunctionExit',
  21512. LinesToStr([ // statements
  21513. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21514. 'rtl.createClass(this, "TObject", null, function () {',
  21515. ' this.$init = function () {',
  21516. ' };',
  21517. ' this.$final = function () {',
  21518. ' };',
  21519. ' this.Create = function () {',
  21520. ' return this;',
  21521. ' };',
  21522. ' rtl.addIntf(this, $mod.IUnknown);',
  21523. '});',
  21524. 'this.GetIntf = function () {',
  21525. ' var Result = null;',
  21526. ' var Intf = null;',
  21527. ' var $ok = false;',
  21528. ' try {',
  21529. ' Intf = rtl.setIntfL(Intf, rtl.queryIntfT($mod.TObject.$create("Create"), $mod.IUnknown), true);',
  21530. ' $ok = true;',
  21531. ' Result = rtl.setIntfL(Result, Intf);',
  21532. ' return Result;',
  21533. ' $ok = true;',
  21534. ' } finally {',
  21535. ' rtl._Release(Intf);',
  21536. ' if (!$ok) rtl._Release(Result);',
  21537. ' };',
  21538. ' return Result;',
  21539. '};',
  21540. '']),
  21541. LinesToStr([ // $mod.$main
  21542. '']));
  21543. end;
  21544. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  21545. begin
  21546. StartProgram(false);
  21547. Add([
  21548. '{$interfaces com}',
  21549. 'type',
  21550. ' IUnknown = interface',
  21551. ' function _AddRef: longint;',
  21552. ' function _Release: longint;',
  21553. ' end;',
  21554. ' TObject = class(IUnknown)',
  21555. ' function _AddRef: longint; virtual; abstract;',
  21556. ' function _Release: longint; virtual; abstract;',
  21557. ' end;',
  21558. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  21559. 'begin',
  21560. ' if i is IUnknown then ;',
  21561. ' if o is IUnknown then ;',
  21562. ' if i is TObject then ;',
  21563. ' i:=j as IUnknown;',
  21564. ' i:=o as IUnknown;',
  21565. ' o:=j as TObject;',
  21566. ' i:=IUnknown(j);',
  21567. ' i:=IUnknown(o);',
  21568. ' o:=TObject(i);',
  21569. 'end;',
  21570. 'begin',
  21571. '']);
  21572. ConvertProgram;
  21573. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  21574. LinesToStr([ // statements
  21575. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21576. 'rtl.createClass(this, "TObject", null, function () {',
  21577. ' this.$init = function () {',
  21578. ' };',
  21579. ' this.$final = function () {',
  21580. ' };',
  21581. ' rtl.addIntf(this, $mod.IUnknown);',
  21582. '});',
  21583. 'this.DoDefault = function (i, j, o) {',
  21584. ' rtl._AddRef(i);',
  21585. ' rtl._AddRef(j);',
  21586. ' try {',
  21587. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  21588. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  21589. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  21590. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  21591. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21592. ' o = rtl.intfAsClass(j, $mod.TObject);',
  21593. ' i = rtl.setIntfL(i, j);',
  21594. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21595. ' o = rtl.intfToClass(i, $mod.TObject);',
  21596. ' } finally {',
  21597. ' rtl._Release(i);',
  21598. ' rtl._Release(j);',
  21599. ' };',
  21600. '};',
  21601. '']),
  21602. LinesToStr([ // $mod.$main
  21603. '']));
  21604. end;
  21605. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  21606. begin
  21607. StartProgram(false);
  21608. Add([
  21609. '{$interfaces com}',
  21610. 'type',
  21611. ' IUnknown = interface',
  21612. ' function _AddRef: longint;',
  21613. ' function _Release: longint;',
  21614. ' end;',
  21615. ' TObject = class(IUnknown)',
  21616. ' function _AddRef: longint; virtual; abstract;',
  21617. ' function _Release: longint; virtual; abstract;',
  21618. ' end;',
  21619. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  21620. 'var o: TObject;',
  21621. 'begin',
  21622. ' DoIt(v,v,v,v);',
  21623. ' DoIt(o,o,k,k);',
  21624. 'end;',
  21625. 'procedure DoSome;',
  21626. 'var v: IUnknown;',
  21627. 'begin',
  21628. ' DoIt(v,v,v,v);',
  21629. 'end;',
  21630. 'var i: IUnknown;',
  21631. 'begin',
  21632. ' DoIt(i,i,i,i);',
  21633. '']);
  21634. ConvertProgram;
  21635. CheckSource('TestClassInterface_COM_PassAsArg',
  21636. LinesToStr([ // statements
  21637. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21638. 'rtl.createClass(this, "TObject", null, function () {',
  21639. ' this.$init = function () {',
  21640. ' };',
  21641. ' this.$final = function () {',
  21642. ' };',
  21643. ' rtl.addIntf(this, $mod.IUnknown);',
  21644. '});',
  21645. 'this.DoIt = function (v, j, k, l) {',
  21646. ' var o = null;',
  21647. ' var $ir = rtl.createIntfRefs();',
  21648. ' rtl._AddRef(v);',
  21649. ' try {',
  21650. ' $mod.DoIt(v, v, {',
  21651. ' get: function () {',
  21652. ' return v;',
  21653. ' },',
  21654. ' set: function (w) {',
  21655. ' v = rtl.setIntfL(v, w);',
  21656. ' }',
  21657. ' }, {',
  21658. ' get: function () {',
  21659. ' return v;',
  21660. ' },',
  21661. ' set: function (w) {',
  21662. ' v = rtl.setIntfL(v, w);',
  21663. ' }',
  21664. ' });',
  21665. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  21666. ' } finally {',
  21667. ' $ir.free();',
  21668. ' rtl._Release(v);',
  21669. ' };',
  21670. '};',
  21671. 'this.DoSome = function () {',
  21672. ' var v = null;',
  21673. ' try {',
  21674. ' $mod.DoIt(v, v, {',
  21675. ' get: function () {',
  21676. ' return v;',
  21677. ' },',
  21678. ' set: function (w) {',
  21679. ' v = rtl.setIntfL(v, w);',
  21680. ' }',
  21681. ' }, {',
  21682. ' get: function () {',
  21683. ' return v;',
  21684. ' },',
  21685. ' set: function (w) {',
  21686. ' v = rtl.setIntfL(v, w);',
  21687. ' }',
  21688. ' });',
  21689. ' } finally {',
  21690. ' rtl._Release(v);',
  21691. ' };',
  21692. '};',
  21693. 'this.i = null;',
  21694. '']),
  21695. LinesToStr([ // $mod.$main
  21696. '$mod.DoIt($mod.i, $mod.i, {',
  21697. ' p: $mod,',
  21698. ' get: function () {',
  21699. ' return this.p.i;',
  21700. ' },',
  21701. ' set: function (v) {',
  21702. ' rtl.setIntfP(this.p, "i", v);',
  21703. ' }',
  21704. '}, {',
  21705. ' p: $mod,',
  21706. ' get: function () {',
  21707. ' return this.p.i;',
  21708. ' },',
  21709. ' set: function (v) {',
  21710. ' rtl.setIntfP(this.p, "i", v);',
  21711. ' }',
  21712. '});',
  21713. '']));
  21714. end;
  21715. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  21716. begin
  21717. StartProgram(false);
  21718. Add([
  21719. '{$interfaces com}',
  21720. 'type',
  21721. ' IUnknown = interface',
  21722. ' function _AddRef: longint;',
  21723. ' function _Release: longint;',
  21724. ' end;',
  21725. ' TObject = class(IUnknown)',
  21726. ' function _AddRef: longint; virtual; abstract;',
  21727. ' function _Release: longint; virtual; abstract;',
  21728. ' end;',
  21729. 'procedure DoIt(out i);',
  21730. 'begin end;',
  21731. 'procedure DoSome;',
  21732. 'var v: IUnknown;',
  21733. 'begin',
  21734. ' DoIt(v);',
  21735. 'end;',
  21736. 'function GetIt: IUnknown;',
  21737. 'begin',
  21738. ' DoIt(Result);',
  21739. 'end;',
  21740. 'var i: IUnknown;',
  21741. 'begin',
  21742. ' DoIt(i);',
  21743. '']);
  21744. ConvertProgram;
  21745. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  21746. LinesToStr([ // statements
  21747. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21748. 'rtl.createClass(this, "TObject", null, function () {',
  21749. ' this.$init = function () {',
  21750. ' };',
  21751. ' this.$final = function () {',
  21752. ' };',
  21753. ' rtl.addIntf(this, $mod.IUnknown);',
  21754. '});',
  21755. 'this.DoIt = function (i) {',
  21756. '};',
  21757. 'this.DoSome = function () {',
  21758. ' var v = null;',
  21759. ' try {',
  21760. ' $mod.DoIt({',
  21761. ' get: function () {',
  21762. ' return v;',
  21763. ' },',
  21764. ' set: function (w) {',
  21765. ' v = w;',
  21766. ' }',
  21767. ' });',
  21768. ' } finally {',
  21769. ' rtl._Release(v);',
  21770. ' };',
  21771. '};',
  21772. 'this.GetIt = function () {',
  21773. ' var Result = null;',
  21774. ' var $ok = false;',
  21775. ' try {',
  21776. ' $mod.DoIt({',
  21777. ' get: function () {',
  21778. ' return Result;',
  21779. ' },',
  21780. ' set: function (v) {',
  21781. ' Result = v;',
  21782. ' }',
  21783. ' });',
  21784. ' $ok = true;',
  21785. ' } finally {',
  21786. ' if (!$ok) rtl._Release(Result);',
  21787. ' };',
  21788. ' return Result;',
  21789. '};',
  21790. 'this.i = null;',
  21791. '']),
  21792. LinesToStr([ // $mod.$main
  21793. 'try {',
  21794. ' $mod.DoIt({',
  21795. ' p: $mod,',
  21796. ' get: function () {',
  21797. ' return this.p.i;',
  21798. ' },',
  21799. ' set: function (v) {',
  21800. ' this.p.i = v;',
  21801. ' }',
  21802. ' });',
  21803. '} finally {',
  21804. ' rtl._Release($mod.i);',
  21805. '};',
  21806. '']));
  21807. end;
  21808. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  21809. begin
  21810. StartProgram(false);
  21811. Add([
  21812. '{$interfaces com}',
  21813. 'type',
  21814. ' IUnknown = interface',
  21815. ' function _AddRef: longint;',
  21816. ' function _Release: longint;',
  21817. ' end;',
  21818. ' TObject = class(IUnknown)',
  21819. ' function _AddRef: longint; virtual; abstract;',
  21820. ' function _Release: longint; virtual; abstract;',
  21821. ' end;',
  21822. 'function GetIt: IUnknown;',
  21823. 'begin',
  21824. 'end;',
  21825. 'procedure DoSome;',
  21826. 'var v: IUnknown;',
  21827. ' i: longint;',
  21828. 'begin',
  21829. ' v:=GetIt;',
  21830. ' v:=GetIt();',
  21831. ' GetIt()._AddRef;',
  21832. ' i:=GetIt()._AddRef;',
  21833. 'end;',
  21834. 'var v: IUnknown;',
  21835. ' i: longint;',
  21836. 'begin',
  21837. ' v:=GetIt;',
  21838. ' v:=GetIt();',
  21839. ' GetIt()._AddRef;',
  21840. ' i:=GetIt()._AddRef;',
  21841. '']);
  21842. ConvertProgram;
  21843. CheckSource('TestClassInterface_COM_FunctionInExpr',
  21844. LinesToStr([ // statements
  21845. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21846. 'rtl.createClass(this, "TObject", null, function () {',
  21847. ' this.$init = function () {',
  21848. ' };',
  21849. ' this.$final = function () {',
  21850. ' };',
  21851. ' rtl.addIntf(this, $mod.IUnknown);',
  21852. '});',
  21853. 'this.GetIt = function () {',
  21854. ' var Result = null;',
  21855. ' return Result;',
  21856. '};',
  21857. 'this.DoSome = function () {',
  21858. ' var v = null;',
  21859. ' var i = 0;',
  21860. ' var $ir = rtl.createIntfRefs();',
  21861. ' try {',
  21862. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  21863. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  21864. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  21865. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  21866. ' } finally {',
  21867. ' $ir.free();',
  21868. ' rtl._Release(v);',
  21869. ' };',
  21870. '};',
  21871. 'this.v = null;',
  21872. 'this.i = 0;',
  21873. '']),
  21874. LinesToStr([ // $mod.$main
  21875. 'var $ir = rtl.createIntfRefs();',
  21876. 'try {',
  21877. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  21878. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  21879. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  21880. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  21881. '} finally {',
  21882. ' $ir.free();',
  21883. '};',
  21884. '']));
  21885. end;
  21886. procedure TTestModule.TestClassInterface_COM_Property;
  21887. begin
  21888. StartProgram(false);
  21889. Add([
  21890. '{$interfaces com}',
  21891. 'type',
  21892. ' IUnknown = interface',
  21893. ' function _AddRef: longint;',
  21894. ' function _Release: longint;',
  21895. ' end;',
  21896. ' TObject = class(IUnknown)',
  21897. ' FAnt: IUnknown;',
  21898. ' function _AddRef: longint; virtual; abstract;',
  21899. ' function _Release: longint; virtual; abstract;',
  21900. ' function GetBird: IUnknown; virtual; abstract;',
  21901. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  21902. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  21903. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  21904. ' property Ant: IUnknown read FAnt write FAnt;',
  21905. ' property Bird: IUnknown read GetBird write SetBird;',
  21906. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  21907. ' end;',
  21908. 'procedure DoIt;',
  21909. 'var',
  21910. ' o: TObject;',
  21911. ' v: IUnknown;',
  21912. 'begin',
  21913. ' v:=o.Ant;',
  21914. ' o.Ant:=v;',
  21915. ' o.Ant:=o.Ant;',
  21916. ' v:=o.Bird;',
  21917. ' o.Bird:=v;',
  21918. ' o.Bird:=o.Bird;',
  21919. ' v:=o.Items[1];',
  21920. ' o.Items[2]:=v;',
  21921. ' o.Items[3]:=o.Items[4];',
  21922. ' v:=o[5];',
  21923. ' o[6]:=v;',
  21924. ' o[7]:=o[8];',
  21925. 'end;',
  21926. 'begin',
  21927. '']);
  21928. ConvertProgram;
  21929. CheckSource('TestClassInterface_COM_Property',
  21930. LinesToStr([ // statements
  21931. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21932. 'rtl.createClass(this, "TObject", null, function () {',
  21933. ' this.$init = function () {',
  21934. ' this.FAnt = null;',
  21935. ' };',
  21936. ' this.$final = function () {',
  21937. ' rtl.setIntfP(this, "FAnt", null);',
  21938. ' };',
  21939. ' rtl.addIntf(this, $mod.IUnknown);',
  21940. '});',
  21941. 'this.DoIt = function () {',
  21942. ' var o = null;',
  21943. ' var v = null;',
  21944. ' var $ir = rtl.createIntfRefs();',
  21945. ' try {',
  21946. ' v = rtl.setIntfL(v, o.FAnt);',
  21947. ' rtl.setIntfP(o, "FAnt", v);',
  21948. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  21949. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  21950. ' o.SetBird(v);',
  21951. ' o.SetBird($ir.ref(1, o.GetBird()));',
  21952. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  21953. ' o.SetItems(2, v);',
  21954. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  21955. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  21956. ' o.SetItems(6, v);',
  21957. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  21958. ' } finally {',
  21959. ' $ir.free();',
  21960. ' rtl._Release(v);',
  21961. ' };',
  21962. '};',
  21963. '']),
  21964. LinesToStr([ // $mod.$main
  21965. '']));
  21966. end;
  21967. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  21968. begin
  21969. StartProgram(false);
  21970. Add([
  21971. '{$interfaces com}',
  21972. 'type',
  21973. ' IUnknown = interface',
  21974. ' function _AddRef: longint;',
  21975. ' function _Release: longint;',
  21976. ' function GetBird: IUnknown;',
  21977. ' procedure SetBird(Value: IUnknown);',
  21978. ' function GetItems(Index: longint): IUnknown;',
  21979. ' procedure SetItems(Index: longint; Value: IUnknown);',
  21980. ' property Bird: IUnknown read GetBird write SetBird;',
  21981. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  21982. ' end;',
  21983. ' TObject = class(IUnknown)',
  21984. ' function _AddRef: longint; virtual; abstract;',
  21985. ' function _Release: longint; virtual; abstract;',
  21986. ' function GetBird: IUnknown; virtual; abstract;',
  21987. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  21988. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  21989. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  21990. ' end;',
  21991. 'procedure DoIt;',
  21992. 'var',
  21993. ' o: TObject;',
  21994. ' v: IUnknown;',
  21995. 'begin',
  21996. ' v:=v.Items[1];',
  21997. ' v.Items[2]:=v;',
  21998. ' v.Items[3]:=v.Items[4];',
  21999. ' v:=v[5];',
  22000. ' v[6]:=v;',
  22001. ' v[7]:=v[8];',
  22002. ' v[9].Bird.Bird:=v;',
  22003. ' v:=v.Bird[10].Bird',
  22004. 'end;',
  22005. 'begin',
  22006. '']);
  22007. ConvertProgram;
  22008. CheckSource('TestClassInterface_COM_IntfProperty',
  22009. LinesToStr([ // statements
  22010. 'rtl.createInterface(this, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  22011. ' "_AddRef",',
  22012. ' "_Release",',
  22013. ' "GetBird",',
  22014. ' "SetBird",',
  22015. ' "GetItems",',
  22016. ' "SetItems"',
  22017. '], null);',
  22018. 'rtl.createClass(this, "TObject", null, function () {',
  22019. ' this.$init = function () {',
  22020. ' };',
  22021. ' this.$final = function () {',
  22022. ' };',
  22023. ' rtl.addIntf(this, $mod.IUnknown);',
  22024. '});',
  22025. 'this.DoIt = function () {',
  22026. ' var o = null;',
  22027. ' var v = null;',
  22028. ' var $ir = rtl.createIntfRefs();',
  22029. ' try {',
  22030. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  22031. ' v.SetItems(2, v);',
  22032. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  22033. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  22034. ' v.SetItems(6, v);',
  22035. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  22036. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  22037. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  22038. ' } finally {',
  22039. ' $ir.free();',
  22040. ' rtl._Release(v);',
  22041. ' };',
  22042. '};',
  22043. '']),
  22044. LinesToStr([ // $mod.$main
  22045. '']));
  22046. end;
  22047. procedure TTestModule.TestClassInterface_COM_Delegation;
  22048. begin
  22049. StartProgram(false);
  22050. Add([
  22051. '{$interfaces com}',
  22052. 'type',
  22053. ' IUnknown = interface',
  22054. ' function _AddRef: longint;',
  22055. ' function _Release: longint;',
  22056. ' end;',
  22057. ' IBird = interface(IUnknown)',
  22058. ' procedure Fly(s: string);',
  22059. ' end;',
  22060. ' IEagle = interface(IBird) end;',
  22061. ' IDove = interface(IBird) end;',
  22062. ' ISwallow = interface(IBird) end;',
  22063. ' TObject = class',
  22064. ' end;',
  22065. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  22066. ' function _AddRef: longint; virtual; abstract;',
  22067. ' function _Release: longint; virtual; abstract;',
  22068. ' procedure Fly(s: string); virtual; abstract;',
  22069. ' end;',
  22070. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  22071. ' function _AddRef: longint; virtual; abstract;',
  22072. ' function _Release: longint; virtual; abstract;',
  22073. ' FBirdIntf: IBird;',
  22074. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  22075. ' function GetEagleIntf: IEagle; virtual; abstract;',
  22076. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  22077. ' FDoveObj: TBird;',
  22078. ' property DoveObj: TBird read FDoveObj implements IDove;',
  22079. ' function GetSwallowObj: TBird; virtual; abstract;',
  22080. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  22081. ' end;',
  22082. 'begin',
  22083. '']);
  22084. ConvertProgram;
  22085. CheckSource('TestClassInterface_COM_Delegation',
  22086. LinesToStr([ // statements
  22087. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  22088. 'rtl.createInterface(this, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], this.IUnknown);',
  22089. 'rtl.createInterface(this, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], this.IBird);',
  22090. 'rtl.createInterface(this, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], this.IBird);',
  22091. 'rtl.createInterface(this, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], this.IBird);',
  22092. 'rtl.createClass(this, "TObject", null, function () {',
  22093. ' this.$init = function () {',
  22094. ' };',
  22095. ' this.$final = function () {',
  22096. ' };',
  22097. '});',
  22098. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22099. ' rtl.addIntf(this, $mod.IBird);',
  22100. ' rtl.addIntf(this, $mod.IEagle);',
  22101. ' rtl.addIntf(this, $mod.IDove);',
  22102. ' rtl.addIntf(this, $mod.ISwallow);',
  22103. '});',
  22104. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  22105. ' this.$init = function () {',
  22106. ' $mod.TObject.$init.call(this);',
  22107. ' this.FBirdIntf = null;',
  22108. ' this.FDoveObj = null;',
  22109. ' };',
  22110. ' this.$final = function () {',
  22111. ' rtl.setIntfP(this, "FBirdIntf", null);',
  22112. ' this.FDoveObj = undefined;',
  22113. ' $mod.TObject.$final.call(this);',
  22114. ' };',
  22115. ' this.$intfmaps = {',
  22116. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  22117. ' return rtl._AddRef(this.FBirdIntf);',
  22118. ' },',
  22119. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  22120. ' return this.GetEagleIntf();',
  22121. ' },',
  22122. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  22123. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  22124. ' },',
  22125. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  22126. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  22127. ' }',
  22128. ' };',
  22129. '});',
  22130. '']),
  22131. LinesToStr([ // $mod.$main
  22132. '']));
  22133. end;
  22134. procedure TTestModule.TestClassInterface_COM_With;
  22135. begin
  22136. StartProgram(false);
  22137. Add([
  22138. '{$interfaces com}',
  22139. 'type',
  22140. ' IUnknown = interface',
  22141. ' function _AddRef: longint;',
  22142. ' function _Release: longint;',
  22143. ' function GetAnt: IUnknown;',
  22144. ' property Ant: IUnknown read GetAnt;',
  22145. ' end;',
  22146. ' TObject = class(IUnknown)',
  22147. ' function _AddRef: longint; virtual; abstract;',
  22148. ' function _Release: longint; virtual; abstract;',
  22149. ' function GetAnt: IUnknown; virtual; abstract;',
  22150. ' property Ant: IUnknown read GetAnt;',
  22151. ' end;',
  22152. 'procedure DoIt;',
  22153. 'var',
  22154. ' i: IUnknown;',
  22155. 'begin',
  22156. ' with i do ',
  22157. ' GetAnt;',
  22158. ' with i.Ant, Ant do ',
  22159. ' GetAnt;',
  22160. 'end;',
  22161. 'begin',
  22162. '']);
  22163. ConvertProgram;
  22164. CheckSource('TestClassInterface_COM_With',
  22165. LinesToStr([ // statements
  22166. 'rtl.createInterface(this, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  22167. 'rtl.createClass(this, "TObject", null, function () {',
  22168. ' this.$init = function () {',
  22169. ' };',
  22170. ' this.$final = function () {',
  22171. ' };',
  22172. ' rtl.addIntf(this, $mod.IUnknown);',
  22173. '});',
  22174. 'this.DoIt = function () {',
  22175. ' var i = null;',
  22176. ' var $ir = rtl.createIntfRefs();',
  22177. ' try {',
  22178. ' $ir.ref(1, i.GetAnt());',
  22179. ' var $with = $ir.ref(2, i.GetAnt());',
  22180. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  22181. ' $ir.ref(4, $with1.GetAnt());',
  22182. ' } finally {',
  22183. ' $ir.free();',
  22184. ' };',
  22185. '};',
  22186. '']),
  22187. LinesToStr([ // $mod.$main
  22188. '']));
  22189. end;
  22190. procedure TTestModule.TestClassInterface_COM_ForObjectInInterface;
  22191. begin
  22192. StartProgram(false);
  22193. Add([
  22194. '{$interfaces com}',
  22195. 'type',
  22196. ' IUnknown = interface end;',
  22197. ' TObject = class',
  22198. ' Id: longint;',
  22199. ' end;',
  22200. ' IEnumerator = interface(IUnknown)',
  22201. ' function GetCurrent: TObject;',
  22202. ' function MoveNext: Boolean;',
  22203. ' property Current: TObject read GetCurrent;',
  22204. ' end;',
  22205. ' IEnumerable = interface(IUnknown)',
  22206. ' function GetEnumerator: IEnumerator;',
  22207. ' end;',
  22208. 'var',
  22209. ' o: TObject;',
  22210. ' i: IEnumerable;',
  22211. 'begin',
  22212. ' for o in i do o.Id:=3;',
  22213. '']);
  22214. ConvertProgram;
  22215. CheckSource('TestClassInterface_COM_ForObjectInInterface',
  22216. LinesToStr([ // statements
  22217. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22218. 'rtl.createClass(this, "TObject", null, function () {',
  22219. ' this.$init = function () {',
  22220. ' this.Id = 0;',
  22221. ' };',
  22222. ' this.$final = function () {',
  22223. ' };',
  22224. '});',
  22225. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  22226. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  22227. 'this.o = null;',
  22228. 'this.i = null;',
  22229. '']),
  22230. LinesToStr([ // $mod.$main
  22231. 'var $in = $mod.i.GetEnumerator();',
  22232. 'try {',
  22233. ' while ($in.MoveNext()) {',
  22234. ' $mod.o = $in.GetCurrent();',
  22235. ' $mod.o.Id = 3;',
  22236. ' }',
  22237. '} finally {',
  22238. ' rtl._Release($in)',
  22239. '};',
  22240. '']));
  22241. end;
  22242. procedure TTestModule.TestClassInterface_COM_ForInterfaceInObject;
  22243. begin
  22244. StartProgram(false);
  22245. Add([
  22246. '{$interfaces com}',
  22247. 'type',
  22248. ' IUnknown = interface end;',
  22249. ' TObject = class',
  22250. ' end;',
  22251. ' IWing = interface',
  22252. ' function Id: longint;',
  22253. ' end;',
  22254. ' TEnumerator = class',
  22255. ' function GetCurrent: IWing; virtual; abstract;',
  22256. ' function MoveNext: Boolean; virtual; abstract;',
  22257. ' property Current: IWing read GetCurrent;',
  22258. ' end;',
  22259. ' TBird = class',
  22260. ' function GetEnumerator: TEnumerator; virtual; abstract;',
  22261. ' procedure Test;',
  22262. ' end;',
  22263. 'procedure TBird.Test;',
  22264. 'var',
  22265. ' Wing: IWing;',
  22266. 'begin',
  22267. ' for Wing in Self do',
  22268. ' if Wing.Id=1 then ;',
  22269. 'end;',
  22270. 'var',
  22271. ' Bird: TBird;',
  22272. ' Wing: IWing;',
  22273. 'begin',
  22274. ' for Wing in Bird do',
  22275. ' if Wing.Id=2 then ;',
  22276. '']);
  22277. ConvertProgram;
  22278. CheckSource('TestClassInterface_COM_ForInterfaceInObject',
  22279. LinesToStr([ // statements
  22280. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22281. 'rtl.createClass(this, "TObject", null, function () {',
  22282. ' this.$init = function () {',
  22283. ' };',
  22284. ' this.$final = function () {',
  22285. ' };',
  22286. '});',
  22287. 'rtl.createInterface(this, "IWing", "{8B0D080B-C0F6-396E-AE88-000BDB74730C}", ["Id"], this.IUnknown);',
  22288. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  22289. '});',
  22290. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22291. ' this.Test = function () {',
  22292. ' var Wing = null;',
  22293. ' try {',
  22294. ' var $in = this.GetEnumerator();',
  22295. ' try {',
  22296. ' while ($in.MoveNext()) {',
  22297. ' Wing = rtl.setIntfL(Wing, $in.GetCurrent(), true);',
  22298. ' if (Wing.Id() === 1) ;',
  22299. ' }',
  22300. ' } finally {',
  22301. ' $in = rtl.freeLoc($in)',
  22302. ' };',
  22303. ' } finally {',
  22304. ' rtl._Release(Wing);',
  22305. ' };',
  22306. ' };',
  22307. '});',
  22308. 'this.Bird = null;',
  22309. 'this.Wing = null;',
  22310. '']),
  22311. LinesToStr([ // $mod.$main
  22312. 'var $in = $mod.Bird.GetEnumerator();',
  22313. 'try {',
  22314. ' while ($in.MoveNext()) {',
  22315. ' rtl.setIntfP($mod, "Wing", $in.GetCurrent(), true);',
  22316. ' if ($mod.Wing.Id() === 2) ;',
  22317. ' }',
  22318. '} finally {',
  22319. ' $in = rtl.freeLoc($in)',
  22320. '};',
  22321. '']));
  22322. end;
  22323. procedure TTestModule.TestClassInterface_COM_ArrayOfIntf_AssignVar;
  22324. begin
  22325. StartProgram(false);
  22326. Add([
  22327. '{$interfaces com}',
  22328. 'type',
  22329. ' IUnknown = interface end;',
  22330. ' IBird = interface(IUnknown)',
  22331. ' function Fly(w: word): word;',
  22332. ' end;',
  22333. ' TBirdArray = array of IBird;',
  22334. 'procedure Run;',
  22335. 'var',
  22336. ' i: IBird;',
  22337. ' a: TBirdArray;',
  22338. ' b: TBirdArray = nil;',
  22339. 'begin',
  22340. ' a:=nil;',
  22341. ' a:=[];',
  22342. ' SetLength(a,3);',
  22343. ' b:=a;',
  22344. ' i:=a[1];',
  22345. ' a[2]:=i;',
  22346. 'end;',
  22347. 'begin',
  22348. '']);
  22349. ConvertProgram;
  22350. CheckSource('TestClassInterface_COM_ArrayOfIntf_AssignVar',
  22351. LinesToStr([ // statements
  22352. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22353. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  22354. 'this.Run = function () {',
  22355. ' var i = null;',
  22356. ' var a = null;',
  22357. ' var b = null;',
  22358. ' try {',
  22359. ' a = rtl.setIntfL(a, null);',
  22360. ' a = rtl.setIntfL(a, null);',
  22361. ' a = rtl.arraySetLength(a, "R", 3);',
  22362. ' b = rtl.setIntfL(b, a);',
  22363. ' i = rtl.setIntfL(i, a[1]);',
  22364. ' rtl.setIntfP(a, 2, i);',
  22365. ' } finally {',
  22366. ' rtl._Release(a);',
  22367. ' rtl._Release(b);',
  22368. ' rtl._Release(i);',
  22369. ' };',
  22370. '};',
  22371. '']),
  22372. LinesToStr([ // $mod.$main
  22373. '']));
  22374. end;
  22375. procedure TTestModule.TestClassInterface_COM_ArrayOfIntf_AssignPlus;
  22376. begin
  22377. StartProgram(false);
  22378. Add([
  22379. '{$interfaces com}',
  22380. '{$modeswitch ArrayOperators}',
  22381. 'type',
  22382. ' IUnknown = interface end;',
  22383. ' IBird = interface(IUnknown)',
  22384. ' function Fly(w: word): word;',
  22385. ' end;',
  22386. ' TBirdArray = array of IBird;',
  22387. 'procedure Run;',
  22388. 'var',
  22389. ' i: IBird;',
  22390. ' a: TBirdArray;',
  22391. ' b: TBirdArray = nil;',
  22392. 'begin',
  22393. ' a:=a+b;',
  22394. ' a:=[i,i];',
  22395. ' a:=a+[i];',
  22396. ' a:=b+[i];',
  22397. ' a:=[i]+a;',
  22398. 'end;',
  22399. 'begin',
  22400. '']);
  22401. ConvertProgram;
  22402. CheckSource('TestClassInterface_COM_ArrayOfIntf_AssignPlus',
  22403. LinesToStr([ // statements
  22404. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22405. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  22406. 'this.Run = function () {',
  22407. ' var i = null;',
  22408. ' var a = null;',
  22409. ' var b = null;',
  22410. ' var $ir = rtl.createIntfRefs();',
  22411. ' try {',
  22412. ' a = rtl.setIntfL(a, rtl.arrayConcat("R", a, b), true);',
  22413. ' a = rtl.setIntfL(a, rtl.arrayManaged(1, 2, [i, i]), true);',
  22414. ' a = rtl.setIntfL(a, rtl.arrayPush("R", a, i), true);',
  22415. ' a = rtl.setIntfL(a, rtl.arrayConcat("R", b, $ir.ref(1, rtl.arrayManaged(1, 2, [i]))), true);',
  22416. ' a = rtl.setIntfL(a, rtl.arrayConcat("R", $ir.ref(2, rtl.arrayManaged(1, 2, [i])), a), true);',
  22417. ' } finally {',
  22418. ' $ir.free();',
  22419. ' rtl._Release(a);',
  22420. ' };',
  22421. '};',
  22422. '']),
  22423. LinesToStr([ // $mod.$main
  22424. '']));
  22425. end;
  22426. procedure TTestModule.TestClassInterface_COM_ArrayOfIntf_AssignArg;
  22427. begin
  22428. StartProgram(false);
  22429. Add([
  22430. '{$interfaces com}',
  22431. 'type',
  22432. ' IUnknown = interface end;',
  22433. ' IBird = interface(IUnknown)',
  22434. ' function Fly(w: word): word;',
  22435. ' end;',
  22436. ' TBirdArray = array of IBird;',
  22437. 'procedure ArgDefault(a: TBirdArray);',
  22438. 'var b: TBirdArray;',
  22439. 'begin',
  22440. ' b:=a;',
  22441. 'end;',
  22442. 'procedure ArgConst(const a: TBirdArray);',
  22443. 'begin',
  22444. 'end;',
  22445. 'procedure ArgVar(var a: TBirdArray);',
  22446. 'begin',
  22447. ' a:=nil;',
  22448. 'end;',
  22449. 'procedure ArgOut(out a: TBirdArray);',
  22450. 'begin',
  22451. 'end;',
  22452. 'procedure Run;',
  22453. 'var',
  22454. ' i: IBird;',
  22455. ' a: TBirdArray;',
  22456. 'begin',
  22457. ' ArgDefault(a);',
  22458. ' ArgDefault(nil);',
  22459. ' ArgDefault([i]);',
  22460. ' ArgConst(a);',
  22461. ' ArgConst([i]);',
  22462. ' ArgVar(a);',
  22463. ' ArgOut(a);',
  22464. 'end;',
  22465. 'begin',
  22466. '']);
  22467. ConvertProgram;
  22468. CheckSource('TestClassInterface_COM_ArrayOfIntf_AssignArg',
  22469. LinesToStr([ // statements
  22470. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22471. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  22472. 'this.ArgDefault = function (a) {',
  22473. ' var b = null;',
  22474. ' rtl._AddRef(a);',
  22475. ' try {',
  22476. ' b = rtl.setIntfL(b, a);',
  22477. ' } finally {',
  22478. ' rtl._Release(a);',
  22479. ' rtl._Release(b);',
  22480. ' };',
  22481. '};',
  22482. 'this.ArgConst = function (a) {',
  22483. '};',
  22484. 'this.ArgVar = function (a) {',
  22485. ' a.set(null);',
  22486. '};',
  22487. 'this.ArgOut = function (a) {',
  22488. '};',
  22489. 'this.Run = function () {',
  22490. ' var i = null;',
  22491. ' var a = null;',
  22492. ' var $ir = rtl.createIntfRefs();',
  22493. ' try {',
  22494. ' $mod.ArgDefault(a);',
  22495. ' $mod.ArgDefault(null);',
  22496. ' $mod.ArgDefault($ir.ref(1, rtl.arrayManaged(1, 2, [i])));',
  22497. ' $mod.ArgConst(a);',
  22498. ' $mod.ArgConst($ir.ref(2, rtl.arrayManaged(1, 2, [i])));',
  22499. ' $mod.ArgVar({',
  22500. ' get: function () {',
  22501. ' return a;',
  22502. ' },',
  22503. ' set: function (v) {',
  22504. ' a = rtl.setIntfL(a, v);',
  22505. ' }',
  22506. ' });',
  22507. ' $mod.ArgOut({',
  22508. ' get: function () {',
  22509. ' return a;',
  22510. ' },',
  22511. ' set: function (v) {',
  22512. ' a = rtl.setIntfL(a, v);',
  22513. ' }',
  22514. ' });',
  22515. ' } finally {',
  22516. ' $ir.free();',
  22517. ' rtl._Release(a);',
  22518. ' };',
  22519. '};',
  22520. '']),
  22521. LinesToStr([ // $mod.$main
  22522. '']));
  22523. end;
  22524. procedure TTestModule.TestClassInterface_COM_ArrayOfIntf_InitFail;
  22525. begin
  22526. StartProgram(false);
  22527. Add([
  22528. '{$interfaces com}',
  22529. 'type',
  22530. ' IUnknown = interface',
  22531. ' function _AddRef: longint;',
  22532. ' function _Release: longint;',
  22533. ' end;',
  22534. ' TObject = class',
  22535. ' end;',
  22536. ' TBirdArray = array of IUnknown;',
  22537. 'var',
  22538. ' i: IUnknown;',
  22539. ' a: TBirdArray = (i);',
  22540. 'begin',
  22541. '']);
  22542. SetExpectedPasResolverError('Not supported: initial value of managed type',nNotSupportedX);
  22543. ConvertProgram;
  22544. end;
  22545. procedure TTestModule.TestClassInterface_COM_ArrayOfIntf_FunctionResult;
  22546. begin
  22547. StartProgram(false);
  22548. Add([
  22549. '{$interfaces com}',
  22550. 'type',
  22551. ' IUnknown = interface',
  22552. ' function _AddRef: longint;',
  22553. ' function _Release: longint;',
  22554. ' end;',
  22555. ' TObject = class end;',
  22556. ' TBird = array of IUnknown;',
  22557. 'function DoDefault(i: TBird): TBird;',
  22558. 'begin',
  22559. ' Result:=i;',
  22560. ' if Result<>nil then exit;',
  22561. 'end;',
  22562. 'var b: TBird;',
  22563. 'begin',
  22564. ' DoDefault(b);',
  22565. ' b:=DoDefault(b);',
  22566. '']);
  22567. ConvertProgram;
  22568. CheckSource('TestClassInterface_COM_ArrayOfIntf_FunctionResult',
  22569. LinesToStr([ // statements
  22570. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  22571. 'rtl.createClass(this, "TObject", null, function () {',
  22572. ' this.$init = function () {',
  22573. ' };',
  22574. ' this.$final = function () {',
  22575. ' };',
  22576. '});',
  22577. 'this.DoDefault = function (i) {',
  22578. ' var Result = null;',
  22579. ' rtl._AddRef(i);',
  22580. ' var $ok = false;',
  22581. ' try {',
  22582. ' Result = rtl.setIntfL(Result, i);',
  22583. ' if (rtl.length(Result) > 0) {',
  22584. ' $ok = true;',
  22585. ' return Result;',
  22586. ' };',
  22587. ' $ok = true;',
  22588. ' } finally {',
  22589. ' rtl._Release(i);',
  22590. ' if(!$ok) rtl._Release(Result);',
  22591. ' };',
  22592. ' return Result;',
  22593. '};',
  22594. 'this.b = null;',
  22595. '']),
  22596. LinesToStr([ // $mod.$main
  22597. 'var $ir = rtl.createIntfRefs();',
  22598. 'try {',
  22599. ' $ir.ref(1, $mod.DoDefault($mod.b));',
  22600. ' rtl.setIntfP($mod, "b", $mod.DoDefault($mod.b), true);',
  22601. '} finally {',
  22602. ' $ir.free();',
  22603. '};',
  22604. '']));
  22605. end;
  22606. procedure TTestModule.TestClassInterface_COM_ArrayOfIntf_InheritedFuncResult;
  22607. begin
  22608. StartProgram(false);
  22609. Add([
  22610. '{$interfaces com}',
  22611. 'type',
  22612. ' IUnknown = interface',
  22613. ' function _AddRef: longint;',
  22614. ' function _Release: longint;',
  22615. ' end;',
  22616. ' TBird = array of IUnknown;',
  22617. ' TObject = class',
  22618. ' function GetIntf: TBird; virtual;',
  22619. ' end;',
  22620. ' TMouse = class',
  22621. ' function GetIntf: TBird; override;',
  22622. ' end;',
  22623. 'function TObject.GetIntf: TBird; begin end;',
  22624. 'function TMouse.GetIntf: TBird;',
  22625. 'var i: TBird;',
  22626. 'begin',
  22627. ' inherited;',
  22628. ' inherited GetIntf;',
  22629. ' inherited GetIntf();',
  22630. ' Result:=inherited GetIntf;',
  22631. ' Result:=inherited GetIntf();',
  22632. ' i:=inherited GetIntf;',
  22633. ' i:=inherited GetIntf();',
  22634. 'end;',
  22635. 'begin',
  22636. '']);
  22637. ConvertProgram;
  22638. CheckSource('TestClassInterface_COM_ArrayOfIntf_InheritedFuncResult',
  22639. LinesToStr([ // statements
  22640. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  22641. 'rtl.createClass(this, "TObject", null, function () {',
  22642. ' this.$init = function () {',
  22643. ' };',
  22644. ' this.$final = function () {',
  22645. ' };',
  22646. ' this.GetIntf = function () {',
  22647. ' var Result = null;',
  22648. ' return Result;',
  22649. ' };',
  22650. '});',
  22651. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  22652. ' this.GetIntf = function () {',
  22653. ' var Result = null;',
  22654. ' var i = null;',
  22655. ' var $ir = rtl.createIntfRefs();',
  22656. ' var $ok = false;',
  22657. ' try {',
  22658. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  22659. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  22660. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  22661. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  22662. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  22663. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  22664. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  22665. ' $ok = true;',
  22666. ' } finally {',
  22667. ' $ir.free();',
  22668. ' rtl._Release(i);',
  22669. ' if (!$ok) rtl._Release(Result);',
  22670. ' };',
  22671. ' return Result;',
  22672. ' };',
  22673. '});',
  22674. '']),
  22675. LinesToStr([ // $mod.$main
  22676. '']));
  22677. end;
  22678. procedure TTestModule.TestClassInterface_COM_ArrayOfIntf_FunctionExit;
  22679. begin
  22680. StartProgram(false);
  22681. Add([
  22682. '{$interfaces com}',
  22683. 'type',
  22684. ' IUnknown = interface',
  22685. ' function _AddRef: longint;',
  22686. ' function _Release: longint;',
  22687. ' end;',
  22688. ' TBird = array of IUnknown;',
  22689. ' TObject = class',
  22690. ' constructor Create;',
  22691. ' end;',
  22692. 'constructor TObject.Create;',
  22693. 'begin',
  22694. 'end;',
  22695. 'function GetIntf: TBird;',
  22696. 'var b: TBird;',
  22697. 'begin',
  22698. ' b:=[];',
  22699. ' Exit(b);',
  22700. 'end;',
  22701. 'begin',
  22702. '']);
  22703. ConvertProgram;
  22704. CheckSource('TestClassInterface_COM_ArrayOfIntf_FunctionExit',
  22705. LinesToStr([ // statements
  22706. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  22707. 'rtl.createClass(this, "TObject", null, function () {',
  22708. ' this.$init = function () {',
  22709. ' };',
  22710. ' this.$final = function () {',
  22711. ' };',
  22712. ' this.Create = function () {',
  22713. ' return this;',
  22714. ' };',
  22715. '});',
  22716. 'this.GetIntf = function () {',
  22717. ' var Result = null;',
  22718. ' var b = null;',
  22719. ' var $ok = false;',
  22720. ' try {',
  22721. ' b = rtl.setIntfL(b, null);',
  22722. ' $ok = true;',
  22723. ' Result = rtl.setIntfL(Result, b);',
  22724. ' return Result;',
  22725. ' $ok = true;',
  22726. ' } finally {',
  22727. ' rtl._Release(b);',
  22728. ' if (!$ok) rtl._Release(Result);',
  22729. ' };',
  22730. ' return Result;',
  22731. '};',
  22732. '']),
  22733. LinesToStr([ // $mod.$main
  22734. '']));
  22735. end;
  22736. procedure TTestModule.TestClassInterface_COM_ArrayOfIntf_Property;
  22737. begin
  22738. StartProgram(false);
  22739. Add([
  22740. '{$interfaces com}',
  22741. 'type',
  22742. ' IUnknown = interface',
  22743. ' function _AddRef: longint;',
  22744. ' function _Release: longint;',
  22745. ' end;',
  22746. ' TAnimal = array of IUnknown;',
  22747. ' TObject = class',
  22748. ' FAnt: TAnimal;',
  22749. ' function GetBird: TAnimal; virtual; abstract;',
  22750. ' procedure SetBird(Value: TAnimal); virtual; abstract;',
  22751. ' function GetItems(Index: longint): TAnimal; virtual; abstract;',
  22752. ' procedure SetItems(Index: longint; Value: TAnimal); virtual; abstract;',
  22753. ' property Ant: TAnimal read FAnt write FAnt;',
  22754. ' property Bird: TAnimal read GetBird write SetBird;',
  22755. ' property Items[Index: longint]: TAnimal read GetItems write SetItems; default;',
  22756. ' end;',
  22757. 'procedure DoIt;',
  22758. 'var',
  22759. ' o: TObject;',
  22760. ' v: TAnimal;',
  22761. 'begin',
  22762. ' v:=o.Ant;',
  22763. ' o.Ant:=v;',
  22764. ' o.Ant:=o.Ant;',
  22765. ' v:=o.Bird;',
  22766. ' o.Bird:=v;',
  22767. ' o.Bird:=o.Bird;',
  22768. ' v:=o.Items[1];',
  22769. ' o.Items[2]:=v;',
  22770. ' o.Items[3]:=o.Items[4];',
  22771. ' v:=o[5];',
  22772. ' o[6]:=v;',
  22773. ' o[7]:=o[8];',
  22774. 'end;',
  22775. 'begin',
  22776. '']);
  22777. ConvertProgram;
  22778. CheckSource('TestClassInterface_COM_ArrayOfIntf_Property',
  22779. LinesToStr([ // statements
  22780. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  22781. 'rtl.createClass(this, "TObject", null, function () {',
  22782. ' this.$init = function () {',
  22783. ' this.FAnt = null;',
  22784. ' };',
  22785. ' this.$final = function () {',
  22786. ' rtl.setIntfP(this, "FAnt", null);',
  22787. ' };',
  22788. '});',
  22789. 'this.DoIt = function () {',
  22790. ' var o = null;',
  22791. ' var v = null;',
  22792. ' var $ir = rtl.createIntfRefs();',
  22793. ' try {',
  22794. ' v = rtl.setIntfL(v, o.FAnt);',
  22795. ' rtl.setIntfP(o, "FAnt", v);',
  22796. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  22797. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  22798. ' o.SetBird(v);',
  22799. ' o.SetBird($ir.ref(1, o.GetBird()));',
  22800. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  22801. ' o.SetItems(2, v);',
  22802. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  22803. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  22804. ' o.SetItems(6, v);',
  22805. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  22806. ' } finally {',
  22807. ' $ir.free();',
  22808. ' rtl._Release(v);',
  22809. ' };',
  22810. '};',
  22811. '']),
  22812. LinesToStr([ // $mod.$main
  22813. '']));
  22814. end;
  22815. procedure TTestModule.TestClassInterface_COM_ArrayOfIntf_BIFuncs;
  22816. begin
  22817. StartProgram(false);
  22818. Add([
  22819. '{$interfaces com}',
  22820. 'type',
  22821. ' IUnknown = interface end;',
  22822. ' IBird = interface(IUnknown)',
  22823. ' end;',
  22824. ' TBirdArray = array of IBird;',
  22825. 'procedure Run;',
  22826. 'var',
  22827. ' i: IBird;',
  22828. ' a, b: TBirdArray;',
  22829. 'begin',
  22830. ' SetLength(a,3);',
  22831. ' a:=copy(b,1,2);',
  22832. ' a:=concat(b);',
  22833. ' a:=concat(b,a);',
  22834. ' insert(i,b,1);',
  22835. ' delete(a,1,2);', // array,index,count
  22836. 'end;',
  22837. 'begin',
  22838. '']);
  22839. ConvertProgram;
  22840. CheckSource('TestClassInterface_COM_ArrayOfIntf_BIFuncs',
  22841. LinesToStr([ // statements
  22842. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22843. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  22844. 'this.Run = function () {',
  22845. ' var i = null;',
  22846. ' var a = null;',
  22847. ' var b = null;',
  22848. ' try {',
  22849. ' a = rtl.arraySetLength(a, "R", 3);',
  22850. ' a = rtl.setIntfL(a, rtl.arrayCopy("R", b, 1, 2), true);',
  22851. ' a = rtl.setIntfL(a, b);',
  22852. ' a = rtl.setIntfL(a, rtl.arrayConcat("R", b, a), true);',
  22853. ' b = rtl.arrayInsert(i, b, 1, "R");',
  22854. ' a = rtl.arrayDeleteR(a, 1, 2);',
  22855. ' } finally {',
  22856. ' rtl._Release(a);',
  22857. ' };',
  22858. '};',
  22859. '']),
  22860. LinesToStr([ // $mod.$main
  22861. '']));
  22862. end;
  22863. procedure TTestModule.TestClassInterface_COM_ArrayOfIntf_ForIn;
  22864. begin
  22865. StartProgram(false);
  22866. Add([
  22867. '{$interfaces com}',
  22868. 'type',
  22869. ' IUnknown = interface end;',
  22870. ' IBird = interface(IUnknown)',
  22871. ' end;',
  22872. ' TBirdArray = array of IBird;',
  22873. 'procedure Run;',
  22874. 'var',
  22875. ' i, j: IBird;',
  22876. ' a: TBirdArray;',
  22877. 'begin',
  22878. ' for i in a do begin',
  22879. ' j:=i;',
  22880. ' end;',
  22881. 'end;',
  22882. 'begin',
  22883. '']);
  22884. ConvertProgram;
  22885. CheckSource('TestClassInterface_COM_ArrayOfIntf_ForIn',
  22886. LinesToStr([ // statements
  22887. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22888. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  22889. 'this.Run = function () {',
  22890. ' var i = null;',
  22891. ' var j = null;',
  22892. ' var a = null;',
  22893. ' try {',
  22894. ' for (var $in = a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  22895. ' i = rtl.setIntfL(i, $in[$l]);',
  22896. ' j = rtl.setIntfL(j, i);',
  22897. ' };',
  22898. ' } finally {',
  22899. ' rtl._Release(i);',
  22900. ' rtl._Release(j);',
  22901. ' };',
  22902. '};',
  22903. '']),
  22904. LinesToStr([ // $mod.$main
  22905. '']));
  22906. end;
  22907. procedure TTestModule.TestClassInterface_COM_StaticArrayOfIntfFail;
  22908. begin
  22909. StartProgram(false);
  22910. Add([
  22911. '{$interfaces com}',
  22912. 'type',
  22913. ' IUnknown = interface',
  22914. ' function _AddRef: longint;',
  22915. ' function _Release: longint;',
  22916. ' end;',
  22917. ' TObject = class',
  22918. ' end;',
  22919. ' TArrOfIntf = array[0..1] of IUnknown;',
  22920. 'begin',
  22921. '']);
  22922. SetExpectedPasResolverError('Not supported: static array of COM-interface',nNotSupportedX);
  22923. ConvertProgram;
  22924. end;
  22925. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  22926. begin
  22927. StartProgram(false);
  22928. Add([
  22929. '{$interfaces com}',
  22930. 'type',
  22931. ' IUnknown = interface',
  22932. ' function _AddRef: longint;',
  22933. ' function _Release: longint;',
  22934. ' end;',
  22935. ' TRec = record',
  22936. ' i: IUnknown;',
  22937. ' end;',
  22938. 'begin',
  22939. '']);
  22940. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  22941. ConvertProgram;
  22942. end;
  22943. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  22944. begin
  22945. StartUnit(false);
  22946. Add([
  22947. '{$interfaces com}',
  22948. 'interface',
  22949. 'implementation',
  22950. 'type',
  22951. ' IUnknown = interface',
  22952. ' function _AddRef: longint;',
  22953. ' end;',
  22954. ' TObject = class(IUnknown)',
  22955. ' function _AddRef: longint;',
  22956. ' end;',
  22957. 'function TObject._AddRef: longint; begin end;',
  22958. 'var i: IUnknown;',
  22959. ' o: TObject;',
  22960. 'initialization',
  22961. ' i:=nil;',
  22962. ' i:=i;',
  22963. ' i:=o;',
  22964. ' if (o as IUnknown)=nil then ;',
  22965. '']);
  22966. ConvertUnit;
  22967. CheckSource('TestClassInterface_COM_UnitInitialization',
  22968. LinesToStr([ // statements
  22969. 'var $impl = $mod.$impl;',
  22970. '']),
  22971. LinesToStr([ // this.$init
  22972. 'var $ir = rtl.createIntfRefs();',
  22973. 'try {',
  22974. ' rtl.setIntfP($impl, "i", null);',
  22975. ' rtl.setIntfP($impl, "i", $impl.i);',
  22976. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  22977. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  22978. '} finally {',
  22979. ' $ir.free();',
  22980. '};',
  22981. '']),
  22982. LinesToStr([ // implementation
  22983. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  22984. 'rtl.createClass($impl, "TObject", null, function () {',
  22985. ' this.$init = function () {',
  22986. ' };',
  22987. ' this.$final = function () {',
  22988. ' };',
  22989. ' this._AddRef = function () {',
  22990. ' var Result = 0;',
  22991. ' return Result;',
  22992. ' };',
  22993. ' rtl.addIntf(this, $impl.IUnknown);',
  22994. '});',
  22995. '$impl.i = null;',
  22996. '$impl.o = null;',
  22997. ''])
  22998. );
  22999. end;
  23000. procedure TTestModule.TestClassInterface_Corba_GUID;
  23001. begin
  23002. StartProgram(false);
  23003. Add([
  23004. '{$interfaces corba}',
  23005. 'type',
  23006. ' IUnknown = interface',
  23007. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  23008. ' end;',
  23009. ' TObject = class end;',
  23010. ' TGUID = record D1, D2, D3, D4: word; end;',
  23011. ' TAliasGUID = TGUID;',
  23012. ' TGUIDString = type string;',
  23013. ' TAliasGUIDString = TGUIDString;',
  23014. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  23015. 'begin end;',
  23016. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  23017. 'begin end;',
  23018. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  23019. 'begin end;',
  23020. 'var',
  23021. ' i: IUnknown;',
  23022. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  23023. ' s: TAliasGUIDString;',
  23024. 'begin',
  23025. ' DoConstGUIDIt(IUnknown);',
  23026. ' DoDefGUID(IUnknown);',
  23027. ' DoStr(IUnknown);',
  23028. ' DoConstGUIDIt(i);',
  23029. ' DoDefGUID(i);',
  23030. ' DoStr(i);',
  23031. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  23032. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  23033. ' DoStr(g);',
  23034. ' g:=i;',
  23035. ' g:=IUnknown;',
  23036. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  23037. ' s:=i;',
  23038. ' s:=IUnknown;',
  23039. ' s:=g;',
  23040. ' if g=i then ;',
  23041. ' if i=g then ;',
  23042. ' if g=IUnknown then ;',
  23043. ' if IUnknown=g then ;',
  23044. ' if s=i then ;',
  23045. ' if i=s then ;',
  23046. ' if s=IUnknown then ;',
  23047. ' if IUnknown=s then ;',
  23048. ' if s=g then ;',
  23049. ' if g=s then ;',
  23050. '']);
  23051. ConvertProgram;
  23052. CheckSource('TestClassInterface_Corba_GUID',
  23053. LinesToStr([ // statements
  23054. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  23055. 'rtl.createClass(this, "TObject", null, function () {',
  23056. ' this.$init = function () {',
  23057. ' };',
  23058. ' this.$final = function () {',
  23059. ' };',
  23060. '});',
  23061. 'rtl.recNewT(this, "TGUID", function () {',
  23062. ' this.D1 = 0;',
  23063. ' this.D2 = 0;',
  23064. ' this.D3 = 0;',
  23065. ' this.D4 = 0;',
  23066. ' this.$eq = function (b) {',
  23067. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  23068. ' };',
  23069. ' this.$assign = function (s) {',
  23070. ' this.D1 = s.D1;',
  23071. ' this.D2 = s.D2;',
  23072. ' this.D3 = s.D3;',
  23073. ' this.D4 = s.D4;',
  23074. ' return this;',
  23075. ' };',
  23076. '});',
  23077. 'this.DoConstGUIDIt = function (g) {',
  23078. '};',
  23079. 'this.DoDefGUID = function (g) {',
  23080. '};',
  23081. 'this.DoStr = function (s) {',
  23082. '};',
  23083. 'this.i = null;',
  23084. 'this.g = this.TGUID.$clone({',
  23085. ' D1: 0xD91C9AF4,',
  23086. ' D2: 0x3C93,',
  23087. ' D3: 0x420F,',
  23088. ' D4: [',
  23089. ' 0xA3,',
  23090. ' 0x03,',
  23091. ' 0xBF,',
  23092. ' 0x5B,',
  23093. ' 0xA8,',
  23094. ' 0x2B,',
  23095. ' 0xFD,',
  23096. ' 0x23',
  23097. ' ]',
  23098. '});',
  23099. 'this.s = "";',
  23100. '']),
  23101. LinesToStr([ // $mod.$main
  23102. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  23103. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  23104. '$mod.DoStr($mod.IUnknown.$guid);',
  23105. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  23106. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  23107. '$mod.DoStr($mod.i.$guid);',
  23108. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  23109. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  23110. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  23111. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  23112. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  23113. '$mod.g.$assign({',
  23114. ' D1: 0xD91C9AF4,',
  23115. ' D2: 0x3C93,',
  23116. ' D3: 0x420F,',
  23117. ' D4: [',
  23118. ' 0xA3,',
  23119. ' 0x03,',
  23120. ' 0xBF,',
  23121. ' 0x5B,',
  23122. ' 0xA8,',
  23123. ' 0x2B,',
  23124. ' 0xFD,',
  23125. ' 0x23',
  23126. ' ]',
  23127. '});',
  23128. '$mod.s = $mod.i.$guid;',
  23129. '$mod.s = $mod.IUnknown.$guid;',
  23130. '$mod.s = rtl.guidrToStr($mod.g);',
  23131. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  23132. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  23133. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  23134. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  23135. 'if ($mod.s === $mod.i.$guid) ;',
  23136. 'if ($mod.i.$guid === $mod.s) ;',
  23137. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  23138. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  23139. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  23140. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  23141. '']));
  23142. end;
  23143. procedure TTestModule.TestClassInterface_Corba_GUIDProperty;
  23144. begin
  23145. StartProgram(false);
  23146. Add([
  23147. '{$interfaces corba}',
  23148. 'type',
  23149. ' IUnknown = interface',
  23150. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  23151. ' end;',
  23152. ' TGUID = record D1, D2, D3, D4: word; end;',
  23153. ' TAliasGUID = TGUID;',
  23154. ' TGUIDString = type string;',
  23155. ' TAliasGUIDString = TGUIDString;',
  23156. ' TObject = class',
  23157. ' function GetG: TAliasGUID; virtual; abstract;',
  23158. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  23159. ' function GetS: TAliasGUIDString; virtual; abstract;',
  23160. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  23161. ' property g: TAliasGUID read GetG write SetG;',
  23162. ' property s: TAliasGUIDString read GetS write SetS;',
  23163. ' end;',
  23164. 'var o: TObject;',
  23165. 'begin',
  23166. ' o.g:=IUnknown;',
  23167. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  23168. ' o.s:=IUnknown;',
  23169. ' o.s:=o.g;',
  23170. '']);
  23171. ConvertProgram;
  23172. CheckSource('TestClassInterface_GUIDProperty',
  23173. LinesToStr([ // statements
  23174. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  23175. 'rtl.recNewT(this, "TGUID", function () {',
  23176. ' this.D1 = 0;',
  23177. ' this.D2 = 0;',
  23178. ' this.D3 = 0;',
  23179. ' this.D4 = 0;',
  23180. ' this.$eq = function (b) {',
  23181. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  23182. ' };',
  23183. ' this.$assign = function (s) {',
  23184. ' this.D1 = s.D1;',
  23185. ' this.D2 = s.D2;',
  23186. ' this.D3 = s.D3;',
  23187. ' this.D4 = s.D4;',
  23188. ' return this;',
  23189. ' };',
  23190. '});',
  23191. 'rtl.createClass(this, "TObject", null, function () {',
  23192. ' this.$init = function () {',
  23193. ' };',
  23194. ' this.$final = function () {',
  23195. ' };',
  23196. '});',
  23197. 'this.o = null;',
  23198. '']),
  23199. LinesToStr([ // $mod.$main
  23200. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  23201. '$mod.o.SetG({',
  23202. ' D1: 0xD91C9AF4,',
  23203. ' D2: 0x3C93,',
  23204. ' D3: 0x420F,',
  23205. ' D4: [',
  23206. ' 0xA3,',
  23207. ' 0x03,',
  23208. ' 0xBF,',
  23209. ' 0x5B,',
  23210. ' 0xA8,',
  23211. ' 0x2B,',
  23212. ' 0xFD,',
  23213. ' 0x23',
  23214. ' ]',
  23215. '});',
  23216. '$mod.o.SetS($mod.IUnknown.$guid);',
  23217. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  23218. '']));
  23219. end;
  23220. procedure TTestModule.TestClassHelper_ClassVar;
  23221. begin
  23222. StartProgram(false);
  23223. Add([
  23224. 'type',
  23225. ' TObject = class',
  23226. ' end;',
  23227. ' THelper = class helper for TObject',
  23228. ' const',
  23229. ' One = 1;',
  23230. ' Two: word = 2;',
  23231. ' class var',
  23232. ' Glob: word;',
  23233. ' function Foo(w: word): word;',
  23234. ' class function Bar(w: word): word;',
  23235. ' end;',
  23236. 'function THelper.foo(w: word): word;',
  23237. 'begin',
  23238. ' Result:=w;',
  23239. ' Two:=One+w;',
  23240. ' Glob:=Glob;',
  23241. ' Result:=Self.Glob;',
  23242. ' Self.Glob:=Self.Glob;',
  23243. ' with Self do Glob:=Glob;',
  23244. 'end;',
  23245. 'class function THelper.bar(w: word): word;',
  23246. 'begin',
  23247. ' Result:=w;',
  23248. ' Two:=One;',
  23249. ' Glob:=Glob;',
  23250. ' Self.Glob:=Self.Glob;',
  23251. ' with Self do Glob:=Glob;',
  23252. 'end;',
  23253. 'var o: TObject;',
  23254. 'begin',
  23255. ' tobject.two:=tobject.one;',
  23256. ' tobject.Glob:=tobject.Glob;',
  23257. ' with tobject do begin',
  23258. ' two:=one;',
  23259. ' Glob:=Glob;',
  23260. ' end;',
  23261. ' o.two:=o.one;',
  23262. ' o.Glob:=o.Glob;',
  23263. ' with o do begin',
  23264. ' two:=one;',
  23265. ' Glob:=Glob;',
  23266. ' end;',
  23267. '']);
  23268. ConvertProgram;
  23269. CheckSource('TestClassHelper_ClassVar',
  23270. LinesToStr([ // statements
  23271. 'rtl.createClass(this, "TObject", null, function () {',
  23272. ' this.$init = function () {',
  23273. ' };',
  23274. ' this.$final = function () {',
  23275. ' };',
  23276. '});',
  23277. 'rtl.createHelper(this, "THelper", null, function () {',
  23278. ' this.One = 1;',
  23279. ' this.Two = 2;',
  23280. ' this.Glob = 0;',
  23281. ' this.Foo = function (w) {',
  23282. ' var Result = 0;',
  23283. ' Result = w;',
  23284. ' $mod.THelper.Two = 1 + w;',
  23285. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23286. ' Result = $mod.THelper.Glob;',
  23287. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23288. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23289. ' return Result;',
  23290. ' };',
  23291. ' this.Bar = function (w) {',
  23292. ' var Result = 0;',
  23293. ' Result = w;',
  23294. ' $mod.THelper.Two = 1;',
  23295. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23296. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23297. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23298. ' return Result;',
  23299. ' };',
  23300. '});',
  23301. 'this.o = null;',
  23302. '']),
  23303. LinesToStr([ // $mod.$main
  23304. '$mod.THelper.Two = 1;',
  23305. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23306. 'var $with = $mod.TObject;',
  23307. '$mod.THelper.Two = 1;',
  23308. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23309. '$mod.THelper.Two = 1;',
  23310. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23311. 'var $with1 = $mod.o;',
  23312. '$mod.THelper.Two = 1;',
  23313. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23314. '']));
  23315. end;
  23316. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  23317. begin
  23318. StartProgram(false);
  23319. Add([
  23320. 'type',
  23321. ' TObject = class',
  23322. ' FSize: word;',
  23323. ' property Size: word read FSize write FSize;',
  23324. ' end;',
  23325. ' THelper = class helper for TObject',
  23326. ' function Foo(w: word = 1): word;',
  23327. ' end;',
  23328. 'function THelper.foo(w: word): word;',
  23329. 'begin',
  23330. ' Result:=Size;',
  23331. ' Size:=Size+2;',
  23332. ' Self.Size:=Self.Size+3;',
  23333. ' FSize:=FSize+4;',
  23334. ' Self.FSize:=Self.FSize+5;',
  23335. ' with Self do begin',
  23336. ' Size:=Size+6;',
  23337. ' FSize:=FSize+7;',
  23338. ' FSize:=FSize+8;',
  23339. ' end;',
  23340. 'end;',
  23341. 'begin',
  23342. '']);
  23343. ConvertProgram;
  23344. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  23345. LinesToStr([ // statements
  23346. 'rtl.createClass(this, "TObject", null, function () {',
  23347. ' this.$init = function () {',
  23348. ' this.FSize = 0;',
  23349. ' };',
  23350. ' this.$final = function () {',
  23351. ' };',
  23352. '});',
  23353. 'rtl.createHelper(this, "THelper", null, function () {',
  23354. ' this.Foo = function (w) {',
  23355. ' var Result = 0;',
  23356. ' Result = this.FSize;',
  23357. ' this.FSize = this.FSize + 2;',
  23358. ' this.FSize = this.FSize + 3;',
  23359. ' this.FSize = this.FSize + 4;',
  23360. ' this.FSize = this.FSize + 5;',
  23361. ' this.FSize = this.FSize + 6;',
  23362. ' this.FSize = this.FSize + 7;',
  23363. ' this.FSize = this.FSize + 8;',
  23364. ' return Result;',
  23365. ' };',
  23366. '});',
  23367. '']),
  23368. LinesToStr([ // $mod.$main
  23369. '']));
  23370. end;
  23371. procedure TTestModule.TestClassHelper_Method_Call;
  23372. begin
  23373. StartProgram(false);
  23374. Add([
  23375. 'type',
  23376. ' TObject = class',
  23377. ' procedure Run(w: word = 10);',
  23378. ' end;',
  23379. ' THelper = class helper for TObject',
  23380. ' function Foo(w: word = 1): word;',
  23381. ' end;',
  23382. 'procedure TObject.Run(w: word);',
  23383. 'var o: TObject;',
  23384. 'begin',
  23385. ' Foo;',
  23386. ' Foo();',
  23387. ' Foo(2);',
  23388. ' Self.Foo;',
  23389. ' Self.Foo();',
  23390. ' Self.Foo(3);',
  23391. ' with Self do begin',
  23392. ' Foo;',
  23393. ' Foo();',
  23394. ' Foo(4);',
  23395. ' end;',
  23396. ' with o do Foo(5);',
  23397. 'end;',
  23398. 'function THelper.foo(w: word): word;',
  23399. 'begin',
  23400. ' Run;',
  23401. ' Run();',
  23402. ' Run(11);',
  23403. ' Foo;',
  23404. ' Foo();',
  23405. ' Foo(12);',
  23406. ' Self.Foo;',
  23407. ' Self.Foo();',
  23408. ' Self.Foo(13);',
  23409. ' with Self do begin',
  23410. ' Foo;',
  23411. ' Foo();',
  23412. ' Foo(14);',
  23413. ' end;',
  23414. 'end;',
  23415. 'var Obj: TObject;',
  23416. 'begin',
  23417. ' obj.Foo;',
  23418. ' obj.Foo();',
  23419. ' obj.Foo(21);',
  23420. ' with obj do begin',
  23421. ' Foo;',
  23422. ' Foo();',
  23423. ' Foo(22);',
  23424. ' end;',
  23425. '']);
  23426. ConvertProgram;
  23427. CheckSource('TestClassHelper_Method_Call',
  23428. LinesToStr([ // statements
  23429. 'rtl.createClass(this, "TObject", null, function () {',
  23430. ' this.$init = function () {',
  23431. ' };',
  23432. ' this.$final = function () {',
  23433. ' };',
  23434. ' this.Run = function (w) {',
  23435. ' var o = null;',
  23436. ' $mod.THelper.Foo.call(this, 1);',
  23437. ' $mod.THelper.Foo.call(this, 1);',
  23438. ' $mod.THelper.Foo.call(this, 2);',
  23439. ' $mod.THelper.Foo.call(this, 1);',
  23440. ' $mod.THelper.Foo.call(this, 1);',
  23441. ' $mod.THelper.Foo.call(this, 3);',
  23442. ' $mod.THelper.Foo.call(this, 1);',
  23443. ' $mod.THelper.Foo.call(this, 1);',
  23444. ' $mod.THelper.Foo.call(this, 4);',
  23445. ' $mod.THelper.Foo.call(o, 5);',
  23446. ' };',
  23447. '});',
  23448. 'rtl.createHelper(this, "THelper", null, function () {',
  23449. ' this.Foo = function (w) {',
  23450. ' var Result = 0;',
  23451. ' this.Run(10);',
  23452. ' this.Run(10);',
  23453. ' this.Run(11);',
  23454. ' $mod.THelper.Foo.call(this, 1);',
  23455. ' $mod.THelper.Foo.call(this, 1);',
  23456. ' $mod.THelper.Foo.call(this, 12);',
  23457. ' $mod.THelper.Foo.call(this, 1);',
  23458. ' $mod.THelper.Foo.call(this, 1);',
  23459. ' $mod.THelper.Foo.call(this, 13);',
  23460. ' $mod.THelper.Foo.call(this, 1);',
  23461. ' $mod.THelper.Foo.call(this, 1);',
  23462. ' $mod.THelper.Foo.call(this, 14);',
  23463. ' return Result;',
  23464. ' };',
  23465. '});',
  23466. 'this.Obj = null;',
  23467. '']),
  23468. LinesToStr([ // $mod.$main
  23469. '$mod.THelper.Foo.call($mod.Obj, 1);',
  23470. '$mod.THelper.Foo.call($mod.Obj, 1);',
  23471. '$mod.THelper.Foo.call($mod.Obj, 21);',
  23472. 'var $with = $mod.Obj;',
  23473. '$mod.THelper.Foo.call($with, 1);',
  23474. '$mod.THelper.Foo.call($with, 1);',
  23475. '$mod.THelper.Foo.call($with, 22);',
  23476. '']));
  23477. end;
  23478. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  23479. begin
  23480. StartProgram(false);
  23481. Add([
  23482. 'type',
  23483. ' TObject = class',
  23484. ' procedure Run(w: word = 10);',
  23485. ' end;',
  23486. ' THelper = class helper for TObject',
  23487. ' function Foo(w: word = 1): word;',
  23488. ' end;',
  23489. 'procedure TObject.Run(w: word);',
  23490. ' procedure Sub(Self: TObject);',
  23491. ' begin',
  23492. ' Foo;',
  23493. ' Foo();',
  23494. ' Self.Foo;',
  23495. ' Self.Foo();',
  23496. ' with Self do begin',
  23497. ' Foo;',
  23498. ' Foo();',
  23499. ' end;',
  23500. ' end;',
  23501. 'begin',
  23502. 'end;',
  23503. 'function THelper.foo(w: word): word;',
  23504. ' procedure Sub(Self: TObject);',
  23505. ' begin',
  23506. ' Run;',
  23507. ' Run();',
  23508. ' Foo;',
  23509. ' Foo();',
  23510. ' Self.Foo;',
  23511. ' Self.Foo();',
  23512. ' with Self do begin',
  23513. ' Foo;',
  23514. ' Foo();',
  23515. ' end;',
  23516. ' end;',
  23517. 'begin',
  23518. 'end;',
  23519. 'begin',
  23520. '']);
  23521. ConvertProgram;
  23522. CheckSource('TestClassHelper_Method_Nested_Call',
  23523. LinesToStr([ // statements
  23524. 'rtl.createClass(this, "TObject", null, function () {',
  23525. ' this.$init = function () {',
  23526. ' };',
  23527. ' this.$final = function () {',
  23528. ' };',
  23529. ' this.Run = function (w) {',
  23530. ' var $Self = this;',
  23531. ' function Sub(Self) {',
  23532. ' $mod.THelper.Foo.call($Self, 1);',
  23533. ' $mod.THelper.Foo.call($Self, 1);',
  23534. ' $mod.THelper.Foo.call(Self, 1);',
  23535. ' $mod.THelper.Foo.call(Self, 1);',
  23536. ' $mod.THelper.Foo.call(Self, 1);',
  23537. ' $mod.THelper.Foo.call(Self, 1);',
  23538. ' };',
  23539. ' };',
  23540. '});',
  23541. 'rtl.createHelper(this, "THelper", null, function () {',
  23542. ' this.Foo = function (w) {',
  23543. ' var $Self = this;',
  23544. ' var Result = 0;',
  23545. ' function Sub(Self) {',
  23546. ' $Self.Run(10);',
  23547. ' $Self.Run(10);',
  23548. ' $mod.THelper.Foo.call($Self, 1);',
  23549. ' $mod.THelper.Foo.call($Self, 1);',
  23550. ' $mod.THelper.Foo.call(Self, 1);',
  23551. ' $mod.THelper.Foo.call(Self, 1);',
  23552. ' $mod.THelper.Foo.call(Self, 1);',
  23553. ' $mod.THelper.Foo.call(Self, 1);',
  23554. ' };',
  23555. ' return Result;',
  23556. ' };',
  23557. '});',
  23558. '']),
  23559. LinesToStr([ // $mod.$main
  23560. '']));
  23561. end;
  23562. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  23563. begin
  23564. StartProgram(false);
  23565. Add([
  23566. 'type',
  23567. ' TObject = class',
  23568. ' class procedure Run(w: word = 10);',
  23569. ' end;',
  23570. ' THelper = class helper for TObject',
  23571. ' class function Foo(w: word = 1): word;',
  23572. ' end;',
  23573. 'class procedure TObject.Run(w: word);',
  23574. 'begin',
  23575. ' Foo;',
  23576. ' Foo();',
  23577. ' Self.Foo;',
  23578. ' Self.Foo();',
  23579. ' with Self do begin',
  23580. ' Foo;',
  23581. ' Foo();',
  23582. ' end;',
  23583. 'end;',
  23584. 'class function THelper.foo(w: word): word;',
  23585. 'begin',
  23586. ' Run;',
  23587. ' Run();',
  23588. ' Foo;',
  23589. ' Foo();',
  23590. ' Self.Foo;',
  23591. ' Self.Foo();',
  23592. ' with Self do begin',
  23593. ' Foo;',
  23594. ' Foo();',
  23595. ' end;',
  23596. 'end;',
  23597. 'var',
  23598. ' Obj: TObject;',
  23599. 'begin',
  23600. ' obj.Foo;',
  23601. ' obj.Foo();',
  23602. ' with obj do begin',
  23603. ' Foo;',
  23604. ' Foo();',
  23605. ' end;',
  23606. ' tobject.Foo;',
  23607. ' tobject.Foo();',
  23608. ' with tobject do begin',
  23609. ' Foo;',
  23610. ' Foo();',
  23611. ' end;',
  23612. '']);
  23613. ConvertProgram;
  23614. CheckSource('TestClassHelper_ClassMethod_Call',
  23615. LinesToStr([ // statements
  23616. 'rtl.createClass(this, "TObject", null, function () {',
  23617. ' this.$init = function () {',
  23618. ' };',
  23619. ' this.$final = function () {',
  23620. ' };',
  23621. ' this.Run = function (w) {',
  23622. ' $mod.THelper.Foo.call(this, 1);',
  23623. ' $mod.THelper.Foo.call(this, 1);',
  23624. ' $mod.THelper.Foo.call(this, 1);',
  23625. ' $mod.THelper.Foo.call(this, 1);',
  23626. ' $mod.THelper.Foo.call(this, 1);',
  23627. ' $mod.THelper.Foo.call(this, 1);',
  23628. ' };',
  23629. '});',
  23630. 'rtl.createHelper(this, "THelper", null, function () {',
  23631. ' this.Foo = function (w) {',
  23632. ' var Result = 0;',
  23633. ' this.Run(10);',
  23634. ' this.Run(10);',
  23635. ' $mod.THelper.Foo.call(this, 1);',
  23636. ' $mod.THelper.Foo.call(this, 1);',
  23637. ' $mod.THelper.Foo.call(this, 1);',
  23638. ' $mod.THelper.Foo.call(this, 1);',
  23639. ' $mod.THelper.Foo.call(this, 1);',
  23640. ' $mod.THelper.Foo.call(this, 1);',
  23641. ' return Result;',
  23642. ' };',
  23643. '});',
  23644. 'this.Obj = null;',
  23645. '']),
  23646. LinesToStr([ // $mod.$main
  23647. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  23648. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  23649. 'var $with = $mod.Obj;',
  23650. '$mod.THelper.Foo.call($with.$class, 1);',
  23651. '$mod.THelper.Foo.call($with.$class, 1);',
  23652. '$mod.THelper.Foo.call($mod.TObject, 1);',
  23653. '$mod.THelper.Foo.call($mod.TObject, 1);',
  23654. 'var $with1 = $mod.TObject;',
  23655. '$mod.THelper.Foo.call($mod.TObject, 1);',
  23656. '$mod.THelper.Foo.call($mod.TObject, 1);',
  23657. '']));
  23658. end;
  23659. procedure TTestModule.TestClassHelper_ClassOf;
  23660. begin
  23661. StartProgram(false);
  23662. Add([
  23663. 'type',
  23664. ' TObject = class',
  23665. ' end;',
  23666. ' TClass = class of TObject;',
  23667. ' THelper = class helper for TObject',
  23668. ' class function Foo(w: word = 1): word;',
  23669. ' end;',
  23670. 'class function THelper.foo(w: word): word;',
  23671. 'begin',
  23672. 'end;',
  23673. 'var',
  23674. ' c: TClass;',
  23675. 'begin',
  23676. ' c.Foo;',
  23677. ' c.Foo();',
  23678. ' with c do begin',
  23679. ' Foo;',
  23680. ' Foo();',
  23681. ' end;',
  23682. '']);
  23683. ConvertProgram;
  23684. CheckSource('TestClassHelper_ClassOf',
  23685. LinesToStr([ // statements
  23686. 'rtl.createClass(this, "TObject", null, function () {',
  23687. ' this.$init = function () {',
  23688. ' };',
  23689. ' this.$final = function () {',
  23690. ' };',
  23691. '});',
  23692. 'rtl.createHelper(this, "THelper", null, function () {',
  23693. ' this.Foo = function (w) {',
  23694. ' var Result = 0;',
  23695. ' return Result;',
  23696. ' };',
  23697. '});',
  23698. 'this.c = null;',
  23699. '']),
  23700. LinesToStr([ // $mod.$main
  23701. '$mod.THelper.Foo.call($mod.c, 1);',
  23702. '$mod.THelper.Foo.call($mod.c, 1);',
  23703. 'var $with = $mod.c;',
  23704. '$mod.THelper.Foo.call($with, 1);',
  23705. '$mod.THelper.Foo.call($with, 1);',
  23706. '']));
  23707. end;
  23708. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  23709. begin
  23710. StartProgram(false);
  23711. Add([
  23712. '{$mode objfpc}',
  23713. 'type',
  23714. ' TObject = class',
  23715. ' procedure DoIt;',
  23716. ' end;',
  23717. ' THelper = class helper for TObject',
  23718. ' procedure Fly(w: word = 1);',
  23719. ' class procedure Glide(w: word = 1);',
  23720. ' class procedure Run(w: word = 1); static;',
  23721. ' end;',
  23722. ' TFly = procedure(w: word) of object;',
  23723. ' TGlide = TFly;',
  23724. ' TRun = procedure(w: word);',
  23725. 'var',
  23726. ' f: TFly;',
  23727. ' g: TGlide;',
  23728. ' r: TRun;',
  23729. 'procedure TObject.DoIt;',
  23730. 'begin',
  23731. ' f:=@fly;',
  23732. ' g:=@glide;',
  23733. ' r:=@run;',
  23734. ' f:[email protected];',
  23735. ' g:[email protected];',
  23736. ' r:[email protected];',
  23737. ' with self do begin',
  23738. ' f:=@fly;',
  23739. ' g:=@glide;',
  23740. ' r:=@run;',
  23741. ' end;',
  23742. 'end;',
  23743. 'procedure THelper.fly(w: word);',
  23744. 'begin',
  23745. ' f:=@fly;',
  23746. ' g:=@glide;',
  23747. ' r:=@run;',
  23748. 'end;',
  23749. 'class procedure THelper.glide(w: word);',
  23750. 'begin',
  23751. ' g:=@glide;',
  23752. ' r:=@run;',
  23753. 'end;',
  23754. 'class procedure THelper.run(w: word);',
  23755. 'begin',
  23756. ' g:=@glide;',
  23757. ' r:=@run;',
  23758. 'end;',
  23759. 'var',
  23760. ' Obj: TObject;',
  23761. 'begin',
  23762. ' f:[email protected];',
  23763. ' g:[email protected];',
  23764. ' r:[email protected];',
  23765. ' with obj do begin',
  23766. ' f:=@fly;',
  23767. ' g:=@glide;',
  23768. ' r:=@run;',
  23769. ' end;',
  23770. ' g:[email protected];',
  23771. ' r:[email protected];',
  23772. ' with tobject do begin',
  23773. ' g:=@glide;',
  23774. ' r:=@run;',
  23775. ' end;',
  23776. '']);
  23777. ConvertProgram;
  23778. CheckSource('TestClassHelper_MethodRefObjFPC',
  23779. LinesToStr([ // statements
  23780. 'rtl.createClass(this, "TObject", null, function () {',
  23781. ' this.$init = function () {',
  23782. ' };',
  23783. ' this.$final = function () {',
  23784. ' };',
  23785. ' this.DoIt = function () {',
  23786. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23787. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23788. ' $mod.r = $mod.THelper.Run;',
  23789. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23790. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23791. ' $mod.r = $mod.THelper.Run;',
  23792. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23793. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23794. ' $mod.r = $mod.THelper.Run;',
  23795. ' };',
  23796. '});',
  23797. 'rtl.createHelper(this, "THelper", null, function () {',
  23798. ' this.Fly = function (w) {',
  23799. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23800. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23801. ' $mod.r = $mod.THelper.Run;',
  23802. ' };',
  23803. ' this.Glide = function (w) {',
  23804. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  23805. ' $mod.r = $mod.THelper.Run;',
  23806. ' };',
  23807. ' this.Run = function (w) {',
  23808. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  23809. ' $mod.r = $mod.THelper.Run;',
  23810. ' };',
  23811. '});',
  23812. 'this.f = null;',
  23813. 'this.g = null;',
  23814. 'this.r = null;',
  23815. 'this.Obj = null;',
  23816. '']),
  23817. LinesToStr([ // $mod.$main
  23818. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  23819. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  23820. '$mod.r = $mod.THelper.Run;',
  23821. 'var $with = $mod.Obj;',
  23822. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  23823. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  23824. '$mod.r = $mod.THelper.Run;',
  23825. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  23826. '$mod.r = $mod.THelper.Run;',
  23827. 'var $with1 = $mod.TObject;',
  23828. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  23829. '$mod.r = $mod.THelper.Run;',
  23830. '']));
  23831. end;
  23832. procedure TTestModule.TestClassHelper_Constructor;
  23833. begin
  23834. StartProgram(false);
  23835. Add([
  23836. 'type',
  23837. ' TObject = class',
  23838. ' constructor Create;',
  23839. ' end;',
  23840. ' TClass = class of TObject;',
  23841. ' THelper = class helper for TObject',
  23842. ' constructor NewHlp(w: word);',
  23843. ' end;',
  23844. 'var',
  23845. ' obj: TObject;',
  23846. ' c: TClass;',
  23847. 'constructor TObject.Create;',
  23848. 'begin',
  23849. ' NewHlp(2);', // normal call
  23850. ' tobject.NewHlp(3);', // new instance
  23851. ' c.newhlp(4);', // new instance
  23852. 'end;',
  23853. 'constructor THelper.NewHlp(w: word);',
  23854. 'begin',
  23855. ' create;', // normal call
  23856. ' tobject.create;', // new instance
  23857. ' NewHlp(2);', // normal call
  23858. ' tobject.NewHlp(3);', // new instance
  23859. ' c.newhlp(4);', // new instance
  23860. 'end;',
  23861. 'begin',
  23862. ' obj.newhlp(2);', // normal call
  23863. ' with Obj do newhlp(12);', // normal call
  23864. ' tobject.newhlp(3);', // new instance
  23865. ' with tobject do newhlp(13);', // new instance
  23866. ' c.newhlp(4);', // new instance
  23867. ' with c do newhlp(14);', // new instance
  23868. '']);
  23869. ConvertProgram;
  23870. CheckSource('TestClassHelper_Constructor',
  23871. LinesToStr([ // statements
  23872. 'rtl.createClass(this, "TObject", null, function () {',
  23873. ' this.$init = function () {',
  23874. ' };',
  23875. ' this.$final = function () {',
  23876. ' };',
  23877. ' this.Create = function () {',
  23878. ' $mod.THelper.NewHlp.call(this, 2);',
  23879. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23880. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  23881. ' return this;',
  23882. ' };',
  23883. '});',
  23884. 'rtl.createHelper(this, "THelper", null, function () {',
  23885. ' this.NewHlp = function (w) {',
  23886. ' this.Create();',
  23887. ' $mod.TObject.$create("Create");',
  23888. ' $mod.THelper.NewHlp.call(this, 2);',
  23889. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23890. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  23891. ' return this;',
  23892. ' };',
  23893. '});',
  23894. 'this.obj = null;',
  23895. 'this.c = null;',
  23896. '']),
  23897. LinesToStr([ // $mod.$main
  23898. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  23899. 'var $with = $mod.obj;',
  23900. '$mod.THelper.NewHlp.call($with, 12);',
  23901. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23902. 'var $with1 = $mod.TObject;',
  23903. '$with1.$create($mod.THelper.NewHlp, [13]);',
  23904. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  23905. 'var $with2 = $mod.c;',
  23906. '$with2.$create($mod.THelper.NewHlp, [14]);',
  23907. '']));
  23908. end;
  23909. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  23910. begin
  23911. StartProgram(false);
  23912. Add([
  23913. 'type',
  23914. ' TObject = class',
  23915. ' procedure Fly;',
  23916. ' end;',
  23917. ' TObjHelper = class helper for TObject',
  23918. ' procedure Fly;',
  23919. ' end;',
  23920. ' TBird = class',
  23921. ' procedure Fly;',
  23922. ' end;',
  23923. ' TBirdHelper = class helper for TBird',
  23924. ' procedure Fly;',
  23925. ' procedure Walk(w: word);',
  23926. ' end;',
  23927. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  23928. ' procedure Fly;',
  23929. ' procedure Walk(w: word);',
  23930. ' end;',
  23931. 'procedure Tobject.fly;',
  23932. 'begin',
  23933. ' inherited;', // ignore
  23934. 'end;',
  23935. 'procedure Tobjhelper.fly;',
  23936. 'begin',
  23937. ' {@TObject_Fly}inherited;',
  23938. ' inherited {@TObject_Fly}Fly;',
  23939. 'end;',
  23940. 'procedure Tbird.fly;',
  23941. 'begin',
  23942. ' {@TObjHelper_Fly}inherited;',
  23943. ' inherited {@TObjHelper_Fly}Fly;',
  23944. 'end;',
  23945. 'procedure Tbirdhelper.fly;',
  23946. 'begin',
  23947. ' {@TBird_Fly}inherited;',
  23948. ' inherited {@TBird_Fly}Fly;',
  23949. 'end;',
  23950. 'procedure Tbirdhelper.walk(w: word);',
  23951. 'begin',
  23952. 'end;',
  23953. 'procedure teagleHelper.fly;',
  23954. 'begin',
  23955. ' {@TBird_Fly}inherited;',
  23956. ' inherited {@TBird_Fly}Fly;',
  23957. 'end;',
  23958. 'procedure teagleHelper.walk(w: word);',
  23959. 'begin',
  23960. ' {@TBirdHelper_Walk}inherited;',
  23961. ' inherited {@TBirdHelper_Walk}Walk(3);',
  23962. 'end;',
  23963. 'begin',
  23964. '']);
  23965. ConvertProgram;
  23966. CheckSource('TestClassHelper_InheritedObjFPC',
  23967. LinesToStr([ // statements
  23968. 'rtl.createClass(this, "TObject", null, function () {',
  23969. ' this.$init = function () {',
  23970. ' };',
  23971. ' this.$final = function () {',
  23972. ' };',
  23973. ' this.Fly = function () {',
  23974. ' };',
  23975. '});',
  23976. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23977. ' this.Fly = function () {',
  23978. ' $mod.TObject.Fly.call(this);',
  23979. ' $mod.TObject.Fly.call(this);',
  23980. ' };',
  23981. '});',
  23982. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23983. ' this.Fly$1 = function () {',
  23984. ' $mod.TObjHelper.Fly.call(this);',
  23985. ' $mod.TObjHelper.Fly.call(this);',
  23986. ' };',
  23987. '});',
  23988. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  23989. ' this.Fly = function () {',
  23990. ' $mod.TBird.Fly$1.call(this);',
  23991. ' $mod.TBird.Fly$1.call(this);',
  23992. ' };',
  23993. ' this.Walk = function (w) {',
  23994. ' };',
  23995. '});',
  23996. 'rtl.createHelper(this, "TEagleHelper", this.TBirdHelper, function () {',
  23997. ' this.Fly$1 = function () {',
  23998. ' $mod.TBird.Fly$1.call(this);',
  23999. ' $mod.TBird.Fly$1.call(this);',
  24000. ' };',
  24001. ' this.Walk$1 = function (w) {',
  24002. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  24003. ' $mod.TBirdHelper.Walk.call(this, 3);',
  24004. ' };',
  24005. '});',
  24006. '']),
  24007. LinesToStr([ // $mod.$main
  24008. '']));
  24009. end;
  24010. procedure TTestModule.TestClassHelper_Property;
  24011. begin
  24012. StartProgram(false);
  24013. Add([
  24014. 'type',
  24015. ' TObject = class',
  24016. ' FSize: word;',
  24017. ' function GetSpeed: word;',
  24018. ' procedure SetSpeed(Value: word);',
  24019. ' end;',
  24020. ' TObjHelper = class helper for TObject',
  24021. ' function GetLeft: word;',
  24022. ' procedure SetLeft(Value: word);',
  24023. ' property Size: word read FSize write FSize;',
  24024. ' property Speed: word read GetSpeed write SetSpeed;',
  24025. ' property Left: word read GetLeft write SetLeft;',
  24026. ' end;',
  24027. ' TBird = class',
  24028. ' property NotRight: word read GetLeft write SetLeft;',
  24029. ' procedure DoIt;',
  24030. ' end;',
  24031. 'var',
  24032. ' b: TBird;',
  24033. 'function Tobject.GetSpeed: word;',
  24034. 'begin',
  24035. ' Size:=Size+11;',
  24036. ' Speed:=Speed+12;',
  24037. ' Result:=Left+13;',
  24038. ' Left:=13;',
  24039. ' Left:=Left+13;',
  24040. ' Self.Size:=Self.Size+21;',
  24041. ' Self.Speed:=Self.Speed+22;',
  24042. ' Self.Left:=Self.Left+23;',
  24043. ' with Self do begin',
  24044. ' Size:=Size+31;',
  24045. ' Speed:=Speed+32;',
  24046. ' Left:=Left+33;',
  24047. ' end;',
  24048. 'end;',
  24049. 'procedure Tobject.SetSpeed(Value: word);',
  24050. 'begin',
  24051. 'end;',
  24052. 'function TObjHelper.GetLeft: word;',
  24053. 'begin',
  24054. ' Size:=Size+11;',
  24055. ' Speed:=Speed+12;',
  24056. ' Left:=Left+13;',
  24057. ' Self.Size:=Self.Size+21;',
  24058. ' Self.Speed:=Self.Speed+22;',
  24059. ' Self.Left:=Self.Left+23;',
  24060. ' with Self do begin',
  24061. ' Size:=Size+31;',
  24062. ' Speed:=Speed+32;',
  24063. ' Left:=Left+33;',
  24064. ' end;',
  24065. 'end;',
  24066. 'procedure TObjHelper.SetLeft(Value: word);',
  24067. 'begin',
  24068. 'end;',
  24069. 'procedure TBird.DoIt;',
  24070. 'begin',
  24071. ' NotRight:=NotRight+11;',
  24072. ' Self.NotRight:=Self.NotRight+21;',
  24073. ' with Self do begin',
  24074. ' NotRight:=NotRight+31;',
  24075. ' end;',
  24076. 'end;',
  24077. 'begin',
  24078. ' b.Size:=b.Size+11;',
  24079. ' b.Speed:=b.Speed+12;',
  24080. ' b.Left:=b.Left+13;',
  24081. ' b.NotRight:=b.NotRight+14;',
  24082. ' with b do begin',
  24083. ' Size:=Size+31;',
  24084. ' Speed:=Speed+32;',
  24085. ' Left:=Left+33;',
  24086. ' NotRight:=NotRight+34;',
  24087. ' end;',
  24088. '']);
  24089. ConvertProgram;
  24090. CheckSource('TestClassHelper_Property',
  24091. LinesToStr([ // statements
  24092. 'rtl.createClass(this, "TObject", null, function () {',
  24093. ' this.$init = function () {',
  24094. ' this.FSize = 0;',
  24095. ' };',
  24096. ' this.$final = function () {',
  24097. ' };',
  24098. ' this.GetSpeed = function () {',
  24099. ' var Result = 0;',
  24100. ' this.FSize = this.FSize + 11;',
  24101. ' this.SetSpeed(this.GetSpeed() + 12);',
  24102. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  24103. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  24104. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  24105. ' this.FSize = this.FSize + 21;',
  24106. ' this.SetSpeed(this.GetSpeed() + 22);',
  24107. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  24108. ' this.FSize = this.FSize + 31;',
  24109. ' this.SetSpeed(this.GetSpeed() + 32);',
  24110. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  24111. ' return Result;',
  24112. ' };',
  24113. ' this.SetSpeed = function (Value) {',
  24114. ' };',
  24115. '});',
  24116. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  24117. ' this.GetLeft = function () {',
  24118. ' var Result = 0;',
  24119. ' this.FSize = this.FSize + 11;',
  24120. ' this.SetSpeed(this.GetSpeed() + 12);',
  24121. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  24122. ' this.FSize = this.FSize + 21;',
  24123. ' this.SetSpeed(this.GetSpeed() + 22);',
  24124. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  24125. ' this.FSize = this.FSize + 31;',
  24126. ' this.SetSpeed(this.GetSpeed() + 32);',
  24127. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  24128. ' return Result;',
  24129. ' };',
  24130. ' this.SetLeft = function (Value) {',
  24131. ' };',
  24132. '});',
  24133. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24134. ' this.DoIt = function () {',
  24135. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  24136. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  24137. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  24138. ' };',
  24139. '});',
  24140. 'this.b = null;',
  24141. '']),
  24142. LinesToStr([ // $mod.$main
  24143. '$mod.b.FSize = $mod.b.FSize + 11;',
  24144. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  24145. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  24146. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  24147. 'var $with = $mod.b;',
  24148. '$with.FSize = $with.FSize + 31;',
  24149. '$with.SetSpeed($with.GetSpeed() + 32);',
  24150. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  24151. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  24152. '']));
  24153. end;
  24154. procedure TTestModule.TestClassHelper_Property_Array;
  24155. begin
  24156. StartProgram(false);
  24157. Add([
  24158. 'type',
  24159. ' TObject = class',
  24160. ' function GetSpeed(Index: boolean): word;',
  24161. ' procedure SetSpeed(Index: boolean; Value: word);',
  24162. ' end;',
  24163. ' TObjHelper = class helper for TObject',
  24164. ' function GetSize(Index: boolean): word;',
  24165. ' procedure SetSize(Index: boolean; Value: word);',
  24166. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  24167. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  24168. ' end;',
  24169. ' TBird = class',
  24170. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  24171. ' procedure DoIt;',
  24172. ' end;',
  24173. 'var',
  24174. ' b: TBird;',
  24175. 'function Tobject.GetSpeed(Index: boolean): word;',
  24176. 'begin',
  24177. ' Result:=Size[false];',
  24178. ' Size[true]:=Size[false]+11;',
  24179. ' Speed[true]:=Speed[false]+12;',
  24180. ' Self.Size[true]:=Self.Size[false]+21;',
  24181. ' Self.Speed[true]:=Self.Speed[false]+22;',
  24182. ' with Self do begin',
  24183. ' Size[true]:=Size[false]+31;',
  24184. ' Speed[true]:=Speed[false]+32;',
  24185. ' end;',
  24186. 'end;',
  24187. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  24188. 'begin',
  24189. 'end;',
  24190. 'function TObjHelper.GetSize(Index: boolean): word;',
  24191. 'begin',
  24192. ' Size[true]:=Size[false]+11;',
  24193. ' Speed[true]:=Speed[false]+12;',
  24194. ' Self.Size[true]:=Self.Size[false]+21;',
  24195. ' Self.Speed[true]:=Self.Speed[false]+22;',
  24196. ' with Self do begin',
  24197. ' Size[true]:=Size[false]+31;',
  24198. ' Speed[true]:=Speed[false]+32;',
  24199. ' end;',
  24200. 'end;',
  24201. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  24202. 'begin',
  24203. 'end;',
  24204. 'procedure TBird.DoIt;',
  24205. 'begin',
  24206. ' Items[true]:=Items[false]+11;',
  24207. ' Self.Items[true]:=Self.Items[false]+21;',
  24208. ' with Self do Items[true]:=Items[false]+31;',
  24209. 'end;',
  24210. 'begin',
  24211. ' b.Size[true]:=b.Size[false]+11;',
  24212. ' b.Speed[true]:=b.Speed[false]+12;',
  24213. ' b.Items[true]:=b.Items[false]+13;',
  24214. ' with b do begin',
  24215. ' Size[true]:=Size[false]+21;',
  24216. ' Speed[true]:=Speed[false]+22;',
  24217. ' Items[true]:=Items[false]+23;',
  24218. ' end;',
  24219. '']);
  24220. ConvertProgram;
  24221. CheckSource('TestClassHelper_Property_Array',
  24222. LinesToStr([ // statements
  24223. 'rtl.createClass(this, "TObject", null, function () {',
  24224. ' this.$init = function () {',
  24225. ' };',
  24226. ' this.$final = function () {',
  24227. ' };',
  24228. ' this.GetSpeed = function (Index) {',
  24229. ' var Result = 0;',
  24230. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  24231. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24232. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  24233. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24234. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  24235. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24236. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  24237. ' return Result;',
  24238. ' };',
  24239. ' this.SetSpeed = function (Index, Value) {',
  24240. ' };',
  24241. '});',
  24242. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  24243. ' this.GetSize = function (Index) {',
  24244. ' var Result = 0;',
  24245. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24246. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  24247. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24248. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  24249. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24250. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  24251. ' return Result;',
  24252. ' };',
  24253. ' this.SetSize = function (Index, Value) {',
  24254. ' };',
  24255. '});',
  24256. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24257. ' this.DoIt = function () {',
  24258. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24259. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24260. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24261. ' };',
  24262. '});',
  24263. 'this.b = null;',
  24264. '']),
  24265. LinesToStr([ // $mod.$main
  24266. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  24267. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  24268. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  24269. 'var $with = $mod.b;',
  24270. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  24271. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  24272. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  24273. '']));
  24274. end;
  24275. procedure TTestModule.TestClassHelper_Property_Array_Default;
  24276. begin
  24277. StartProgram(false);
  24278. Add([
  24279. 'type',
  24280. ' TObject = class',
  24281. ' function GetSpeed(Index: boolean): word;',
  24282. ' procedure SetSpeed(Index: boolean; Value: word);',
  24283. ' end;',
  24284. ' TObjHelper = class helper for TObject',
  24285. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  24286. ' end;',
  24287. ' TBird = class',
  24288. ' end;',
  24289. ' TBirdHelper = class helper for TBird',
  24290. ' function GetSize(Index: word): boolean;',
  24291. ' procedure SetSize(Index: word; Value: boolean);',
  24292. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  24293. ' end;',
  24294. 'function Tobject.GetSpeed(Index: boolean): word;',
  24295. 'begin',
  24296. ' Self[true]:=Self[false]+1;',
  24297. 'end;',
  24298. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  24299. 'begin',
  24300. 'end;',
  24301. 'function TBirdHelper.GetSize(Index: word): boolean;',
  24302. 'begin',
  24303. ' Self[1]:=not Self[2];',
  24304. 'end;',
  24305. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  24306. 'begin',
  24307. 'end;',
  24308. 'var',
  24309. ' o: TObject;',
  24310. ' b: TBird;',
  24311. 'begin',
  24312. ' o[true]:=o[false]+1;',
  24313. ' b[3]:=not b[4];',
  24314. '']);
  24315. ConvertProgram;
  24316. CheckSource('TestClassHelper_Property_Array_Default',
  24317. LinesToStr([ // statements
  24318. 'rtl.createClass(this, "TObject", null, function () {',
  24319. ' this.$init = function () {',
  24320. ' };',
  24321. ' this.$final = function () {',
  24322. ' };',
  24323. ' this.GetSpeed = function (Index) {',
  24324. ' var Result = 0;',
  24325. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  24326. ' return Result;',
  24327. ' };',
  24328. ' this.SetSpeed = function (Index, Value) {',
  24329. ' };',
  24330. '});',
  24331. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  24332. '});',
  24333. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24334. '});',
  24335. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  24336. ' this.GetSize = function (Index) {',
  24337. ' var Result = false;',
  24338. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  24339. ' return Result;',
  24340. ' };',
  24341. ' this.SetSize = function (Index, Value) {',
  24342. ' };',
  24343. '});',
  24344. 'this.o = null;',
  24345. 'this.b = null;',
  24346. '']),
  24347. LinesToStr([ // $mod.$main
  24348. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  24349. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  24350. '']));
  24351. end;
  24352. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  24353. begin
  24354. StartProgram(false);
  24355. Add([
  24356. 'type',
  24357. ' TObject = class',
  24358. ' end;',
  24359. ' TObjHelper = class helper for TObject',
  24360. ' function GetItems(Index: word): TObject;',
  24361. ' procedure SetItems(Index: word; Value: TObject);',
  24362. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  24363. ' end;',
  24364. 'function Tobjhelper.GetItems(Index: word): TObject;',
  24365. 'begin',
  24366. ' Self[1][2]:=Self[3][4];',
  24367. 'end;',
  24368. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  24369. 'begin',
  24370. 'end;',
  24371. 'var',
  24372. ' o: TObject;',
  24373. 'begin',
  24374. ' o[1][2]:=o[3][4];',
  24375. '']);
  24376. ConvertProgram;
  24377. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  24378. LinesToStr([ // statements
  24379. 'rtl.createClass(this, "TObject", null, function () {',
  24380. ' this.$init = function () {',
  24381. ' };',
  24382. ' this.$final = function () {',
  24383. ' };',
  24384. '});',
  24385. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  24386. ' this.GetItems = function (Index) {',
  24387. ' var Result = null;',
  24388. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  24389. ' return Result;',
  24390. ' };',
  24391. ' this.SetItems = function (Index, Value) {',
  24392. ' };',
  24393. '});',
  24394. 'this.o = null;',
  24395. '']),
  24396. LinesToStr([ // $mod.$main
  24397. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  24398. '']));
  24399. end;
  24400. procedure TTestModule.TestClassHelper_ClassProperty;
  24401. begin
  24402. StartProgram(false);
  24403. Add([
  24404. 'type',
  24405. ' TObject = class',
  24406. ' class var FSize: word;',
  24407. ' class function GetSpeed: word;',
  24408. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  24409. ' end;',
  24410. ' TObjHelper = class helper for TObject',
  24411. ' class function GetLeft: word;',
  24412. ' class procedure SetLeft(Value: word);',
  24413. ' class property Size: word read FSize write FSize;',
  24414. ' class property Speed: word read GetSpeed write SetSpeed;',
  24415. ' class property Left: word read GetLeft write SetLeft;',
  24416. ' end;',
  24417. ' TBird = class',
  24418. ' class property NotRight: word read GetLeft write SetLeft;',
  24419. ' class procedure DoIt;',
  24420. ' end;',
  24421. ' TBirdClass = class of TBird;',
  24422. 'class function Tobject.GetSpeed: word;',
  24423. 'begin',
  24424. ' Size:=Size+11;',
  24425. ' Speed:=Speed+12;',
  24426. ' Left:=Left+13;',
  24427. ' Self.Size:=Self.Size+21;',
  24428. ' Self.Speed:=Self.Speed+22;',
  24429. ' Self.Left:=Self.Left+23;',
  24430. ' with Self do begin',
  24431. ' Size:=Size+31;',
  24432. ' Speed:=Speed+32;',
  24433. ' Left:=Left+33;',
  24434. ' end;',
  24435. 'end;',
  24436. 'class function TObjHelper.GetLeft: word;',
  24437. 'begin',
  24438. ' Size:=Size+11;',
  24439. ' Speed:=Speed+12;',
  24440. ' Left:=Left+13;',
  24441. ' Self.Size:=Self.Size+21;',
  24442. ' Self.Speed:=Self.Speed+22;',
  24443. ' Self.Left:=Self.Left+23;',
  24444. ' with Self do begin',
  24445. ' Size:=Size+31;',
  24446. ' Speed:=Speed+32;',
  24447. ' Left:=Left+33;',
  24448. ' end;',
  24449. 'end;',
  24450. 'class procedure TObjHelper.SetLeft(Value: word);',
  24451. 'begin',
  24452. 'end;',
  24453. 'class procedure TBird.DoIt;',
  24454. 'begin',
  24455. ' NotRight:=NotRight+11;',
  24456. ' Self.NotRight:=Self.NotRight+21;',
  24457. ' with Self do NotRight:=NotRight+31;',
  24458. 'end;',
  24459. 'var',
  24460. ' b: TBird;',
  24461. ' c: TBirdClass;',
  24462. 'begin',
  24463. ' b.Size:=b.Size+11;',
  24464. ' b.Speed:=b.Speed+12;',
  24465. ' b.Left:=b.Left+13;',
  24466. ' b.NotRight:=b.NotRight+14;',
  24467. ' with b do begin',
  24468. ' Size:=Size+31;',
  24469. ' Speed:=Speed+32;',
  24470. ' Left:=Left+33;',
  24471. ' NotRight:=NotRight+34;',
  24472. ' end;',
  24473. ' c.Size:=c.Size+11;',
  24474. ' c.Speed:=c.Speed+12;',
  24475. ' c.Left:=c.Left+13;',
  24476. ' c.NotRight:=c.NotRight+14;',
  24477. ' with c do begin',
  24478. ' Size:=Size+31;',
  24479. ' Speed:=Speed+32;',
  24480. ' Left:=Left+33;',
  24481. ' NotRight:=NotRight+34;',
  24482. ' end;',
  24483. ' tbird.Size:=tbird.Size+11;',
  24484. ' tbird.Speed:=tbird.Speed+12;',
  24485. ' tbird.Left:=tbird.Left+13;',
  24486. ' tbird.NotRight:=tbird.NotRight+14;',
  24487. ' with tbird do begin',
  24488. ' Size:=Size+31;',
  24489. ' Speed:=Speed+32;',
  24490. ' Left:=Left+33;',
  24491. ' NotRight:=NotRight+34;',
  24492. ' end;',
  24493. '']);
  24494. ConvertProgram;
  24495. CheckSource('TestClassHelper_ClassProperty',
  24496. LinesToStr([ // statements
  24497. 'rtl.createClass(this, "TObject", null, function () {',
  24498. ' this.FSize = 0;',
  24499. ' this.$init = function () {',
  24500. ' };',
  24501. ' this.$final = function () {',
  24502. ' };',
  24503. ' this.GetSpeed = function () {',
  24504. ' var Result = 0;',
  24505. ' $mod.TObject.FSize = this.FSize + 11;',
  24506. ' this.SetSpeed(this.GetSpeed() + 12);',
  24507. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  24508. ' $mod.TObject.FSize = this.FSize + 21;',
  24509. ' this.SetSpeed(this.GetSpeed() + 22);',
  24510. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  24511. ' $mod.TObject.FSize = this.FSize + 31;',
  24512. ' this.SetSpeed(this.GetSpeed() + 32);',
  24513. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  24514. ' return Result;',
  24515. ' };',
  24516. '});',
  24517. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  24518. ' this.GetLeft = function () {',
  24519. ' var Result = 0;',
  24520. ' $mod.TObject.FSize = this.FSize + 11;',
  24521. ' this.SetSpeed(this.GetSpeed() + 12);',
  24522. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  24523. ' $mod.TObject.FSize = this.FSize + 21;',
  24524. ' this.SetSpeed(this.GetSpeed() + 22);',
  24525. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  24526. ' $mod.TObject.FSize = this.FSize + 31;',
  24527. ' this.SetSpeed(this.GetSpeed() + 32);',
  24528. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  24529. ' return Result;',
  24530. ' };',
  24531. ' this.SetLeft = function (Value) {',
  24532. ' };',
  24533. '});',
  24534. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24535. ' this.DoIt = function () {',
  24536. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  24537. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  24538. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  24539. ' };',
  24540. '});',
  24541. 'this.b = null;',
  24542. 'this.c = null;',
  24543. '']),
  24544. LinesToStr([ // $mod.$main
  24545. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  24546. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  24547. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  24548. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  24549. 'var $with = $mod.b;',
  24550. '$mod.TObject.FSize = $with.FSize + 31;',
  24551. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  24552. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  24553. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  24554. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  24555. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  24556. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  24557. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  24558. 'var $with1 = $mod.c;',
  24559. '$mod.TObject.FSize = $with1.FSize + 31;',
  24560. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  24561. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  24562. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  24563. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  24564. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  24565. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  24566. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  24567. 'var $with2 = $mod.TBird;',
  24568. '$mod.TObject.FSize = $with2.FSize + 31;',
  24569. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  24570. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  24571. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  24572. '']));
  24573. end;
  24574. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  24575. begin
  24576. StartProgram(false);
  24577. Add([
  24578. 'type',
  24579. ' TObject = class',
  24580. ' class function GetSpeed: word; static;',
  24581. ' class procedure SetSpeed(Value: word); static;',
  24582. ' end;',
  24583. ' TObjHelper = class helper for TObject',
  24584. ' class function GetLeft: word; static;',
  24585. ' class procedure SetLeft(Value: word); static;',
  24586. ' class property Speed: word read GetSpeed write SetSpeed;',
  24587. ' class property Left: word read GetLeft write SetLeft;',
  24588. ' end;',
  24589. ' TBird = class',
  24590. ' class property NotRight: word read GetLeft write SetLeft;',
  24591. ' class procedure DoIt; static;',
  24592. ' class procedure DoSome;',
  24593. ' end;',
  24594. ' TBirdClass = class of TBird;',
  24595. 'class function Tobject.GetSpeed: word;',
  24596. 'begin',
  24597. ' Speed:=Speed+12;',
  24598. ' Left:=Left+13;',
  24599. 'end;',
  24600. 'class procedure TObject.SetSpeed(Value: word);',
  24601. 'begin',
  24602. 'end;',
  24603. 'class function TObjHelper.GetLeft: word;',
  24604. 'begin',
  24605. ' Speed:=Speed+12;',
  24606. ' Left:=Left+13;',
  24607. 'end;',
  24608. 'class procedure TObjHelper.SetLeft(Value: word);',
  24609. 'begin',
  24610. 'end;',
  24611. 'class procedure TBird.DoIt;',
  24612. 'begin',
  24613. ' NotRight:=NotRight+11;',
  24614. 'end;',
  24615. 'class procedure TBird.DoSome;',
  24616. 'begin',
  24617. ' Speed:=Speed+12;',
  24618. ' Left:=Left+13;',
  24619. ' Self.Speed:=Self.Speed+22;',
  24620. ' Self.Left:=Self.Left+23;',
  24621. ' with Self do begin',
  24622. ' Speed:=Speed+32;',
  24623. ' Left:=Left+33;',
  24624. ' end;',
  24625. ' NotRight:=NotRight+11;',
  24626. ' Self.NotRight:=Self.NotRight+21;',
  24627. ' with Self do NotRight:=NotRight+31;',
  24628. 'end;',
  24629. 'var',
  24630. ' b: TBird;',
  24631. ' c: TBirdClass;',
  24632. 'begin',
  24633. ' b.Speed:=b.Speed+12;',
  24634. ' b.Left:=b.Left+13;',
  24635. ' b.NotRight:=b.NotRight+14;',
  24636. ' with b do begin',
  24637. ' Speed:=Speed+32;',
  24638. ' Left:=Left+33;',
  24639. ' NotRight:=NotRight+34;',
  24640. ' end;',
  24641. ' c.Speed:=c.Speed+12;',
  24642. ' c.Left:=c.Left+13;',
  24643. ' c.NotRight:=c.NotRight+14;',
  24644. ' with c do begin',
  24645. ' Speed:=Speed+32;',
  24646. ' Left:=Left+33;',
  24647. ' NotRight:=NotRight+34;',
  24648. ' end;',
  24649. ' tbird.Speed:=tbird.Speed+12;',
  24650. ' tbird.Left:=tbird.Left+13;',
  24651. ' tbird.NotRight:=tbird.NotRight+14;',
  24652. ' with tbird do begin',
  24653. ' Speed:=Speed+32;',
  24654. ' Left:=Left+33;',
  24655. ' NotRight:=NotRight+34;',
  24656. ' end;',
  24657. '']);
  24658. ConvertProgram;
  24659. CheckSource('TestClassHelper_ClassPropertyStatic',
  24660. LinesToStr([ // statements
  24661. 'rtl.createClass(this, "TObject", null, function () {',
  24662. ' this.$init = function () {',
  24663. ' };',
  24664. ' this.$final = function () {',
  24665. ' };',
  24666. ' this.GetSpeed = function () {',
  24667. ' var Result = 0;',
  24668. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  24669. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  24670. ' return Result;',
  24671. ' };',
  24672. ' this.SetSpeed = function (Value) {',
  24673. ' };',
  24674. '});',
  24675. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  24676. ' this.GetLeft = function () {',
  24677. ' var Result = 0;',
  24678. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  24679. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  24680. ' return Result;',
  24681. ' };',
  24682. ' this.SetLeft = function (Value) {',
  24683. ' };',
  24684. '});',
  24685. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24686. ' this.DoIt = function () {',
  24687. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  24688. ' };',
  24689. ' this.DoSome = function () {',
  24690. ' this.SetSpeed(this.GetSpeed() + 12);',
  24691. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  24692. ' this.SetSpeed(this.GetSpeed() + 22);',
  24693. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  24694. ' this.SetSpeed(this.GetSpeed() + 32);',
  24695. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  24696. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  24697. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  24698. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  24699. ' };',
  24700. '});',
  24701. 'this.b = null;',
  24702. 'this.c = null;',
  24703. '']),
  24704. LinesToStr([ // $mod.$main
  24705. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  24706. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  24707. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  24708. 'var $with = $mod.b;',
  24709. '$with.SetSpeed($with.GetSpeed() + 32);',
  24710. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  24711. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  24712. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  24713. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  24714. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  24715. 'var $with1 = $mod.c;',
  24716. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  24717. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  24718. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  24719. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  24720. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  24721. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  24722. 'var $with2 = $mod.TBird;',
  24723. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  24724. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  24725. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  24726. '']));
  24727. end;
  24728. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  24729. begin
  24730. StartProgram(false);
  24731. Add([
  24732. 'type',
  24733. ' TObject = class',
  24734. ' class function GetSpeed(Index: boolean): word;',
  24735. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  24736. ' end;',
  24737. ' TObjHelper = class helper for TObject',
  24738. ' class function GetSize(Index: boolean): word;',
  24739. ' class procedure SetSize(Index: boolean; Value: word);',
  24740. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  24741. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  24742. ' end;',
  24743. ' TBird = class',
  24744. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  24745. ' class procedure DoIt;',
  24746. ' end;',
  24747. ' TBirdClass = class of TBird;',
  24748. 'class function Tobject.GetSpeed(Index: boolean): word;',
  24749. 'begin',
  24750. ' Size[true]:=Size[false]+11;',
  24751. ' Speed[true]:=Speed[false]+12;',
  24752. ' Self.Size[true]:=Self.Size[false]+21;',
  24753. ' Self.Speed[true]:=Self.Speed[false]+22;',
  24754. ' with Self do begin',
  24755. ' Size[true]:=Size[false]+31;',
  24756. ' Speed[true]:=Speed[false]+32;',
  24757. ' end;',
  24758. 'end;',
  24759. 'class function TObjHelper.GetSize(Index: boolean): word;',
  24760. 'begin',
  24761. ' Size[true]:=Size[false]+11;',
  24762. ' Speed[true]:=Speed[false]+12;',
  24763. ' Self.Size[true]:=Self.Size[false]+21;',
  24764. ' Self.Speed[true]:=Self.Speed[false]+22;',
  24765. ' with Self do begin',
  24766. ' Size[true]:=Size[false]+31;',
  24767. ' Speed[true]:=Speed[false]+32;',
  24768. ' end;',
  24769. 'end;',
  24770. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  24771. 'begin',
  24772. 'end;',
  24773. 'class procedure TBird.DoIt;',
  24774. 'begin',
  24775. ' Items[true]:=Items[false]+11;',
  24776. ' Self.Items[true]:=Self.Items[false]+21;',
  24777. ' with Self do Items[true]:=Items[false]+31;',
  24778. 'end;',
  24779. 'var',
  24780. ' b: TBird;',
  24781. ' c: TBirdClass;',
  24782. 'begin',
  24783. ' b.Size[true]:=b.Size[false]+11;',
  24784. ' b.Speed[true]:=b.Speed[false]+12;',
  24785. ' b.Items[true]:=b.Items[false]+13;',
  24786. ' with b do begin',
  24787. ' Size[true]:=Size[false]+21;',
  24788. ' Speed[true]:=Speed[false]+22;',
  24789. ' Items[true]:=Items[false]+23;',
  24790. ' end;',
  24791. ' c.Size[true]:=c.Size[false]+11;',
  24792. ' c.Speed[true]:=c.Speed[false]+12;',
  24793. ' c.Items[true]:=c.Items[false]+13;',
  24794. ' with c do begin',
  24795. ' Size[true]:=Size[false]+21;',
  24796. ' Speed[true]:=Speed[false]+22;',
  24797. ' Items[true]:=Items[false]+23;',
  24798. ' end;',
  24799. ' TBird.Size[true]:=TBird.Size[false]+11;',
  24800. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  24801. ' TBird.Items[true]:=TBird.Items[false]+13;',
  24802. ' with TBird do begin',
  24803. ' Size[true]:=Size[false]+21;',
  24804. ' Speed[true]:=Speed[false]+22;',
  24805. ' Items[true]:=Items[false]+23;',
  24806. ' end;',
  24807. '']);
  24808. ConvertProgram;
  24809. CheckSource('TestClassHelper_ClassProperty_Array',
  24810. LinesToStr([ // statements
  24811. 'rtl.createClass(this, "TObject", null, function () {',
  24812. ' this.$init = function () {',
  24813. ' };',
  24814. ' this.$final = function () {',
  24815. ' };',
  24816. ' this.GetSpeed = function (Index) {',
  24817. ' var Result = 0;',
  24818. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24819. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  24820. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24821. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  24822. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24823. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  24824. ' return Result;',
  24825. ' };',
  24826. '});',
  24827. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  24828. ' this.GetSize = function (Index) {',
  24829. ' var Result = 0;',
  24830. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24831. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  24832. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24833. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  24834. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24835. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  24836. ' return Result;',
  24837. ' };',
  24838. ' this.SetSize = function (Index, Value) {',
  24839. ' };',
  24840. '});',
  24841. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24842. ' this.DoIt = function () {',
  24843. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24844. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24845. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24846. ' };',
  24847. '});',
  24848. 'this.b = null;',
  24849. 'this.c = null;',
  24850. '']),
  24851. LinesToStr([ // $mod.$main
  24852. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  24853. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  24854. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  24855. 'var $with = $mod.b;',
  24856. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  24857. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  24858. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  24859. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  24860. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  24861. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  24862. 'var $with1 = $mod.c;',
  24863. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  24864. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  24865. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  24866. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  24867. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  24868. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  24869. 'var $with2 = $mod.TBird;',
  24870. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  24871. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  24872. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  24873. '']));
  24874. end;
  24875. procedure TTestModule.TestClassHelper_ForIn;
  24876. begin
  24877. StartProgram(false);
  24878. Add([
  24879. 'type',
  24880. ' TObject = class end;',
  24881. ' TItem = TObject;',
  24882. ' TEnumerator = class',
  24883. ' FCurrent: TItem;',
  24884. ' property Current: TItem read FCurrent;',
  24885. ' function MoveNext: boolean;',
  24886. ' end;',
  24887. ' TBird = class',
  24888. ' end;',
  24889. ' TBirdHelper = class helper for TBird',
  24890. ' function GetEnumerator: TEnumerator;',
  24891. ' end;',
  24892. 'function TEnumerator.MoveNext: boolean;',
  24893. 'begin',
  24894. 'end;',
  24895. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  24896. 'begin',
  24897. 'end;',
  24898. 'var',
  24899. ' b: TBird;',
  24900. ' i, i2: TItem;',
  24901. 'begin',
  24902. ' for i in b do i2:=i;']);
  24903. ConvertProgram;
  24904. CheckSource('TestClassHelper_ForIn',
  24905. LinesToStr([ // statements
  24906. 'rtl.createClass(this, "TObject", null, function () {',
  24907. ' this.$init = function () {',
  24908. ' };',
  24909. ' this.$final = function () {',
  24910. ' };',
  24911. '});',
  24912. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  24913. ' this.$init = function () {',
  24914. ' $mod.TObject.$init.call(this);',
  24915. ' this.FCurrent = null;',
  24916. ' };',
  24917. ' this.$final = function () {',
  24918. ' this.FCurrent = undefined;',
  24919. ' $mod.TObject.$final.call(this);',
  24920. ' };',
  24921. ' this.MoveNext = function () {',
  24922. ' var Result = false;',
  24923. ' return Result;',
  24924. ' };',
  24925. '});',
  24926. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24927. '});',
  24928. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  24929. ' this.GetEnumerator = function () {',
  24930. ' var Result = null;',
  24931. ' return Result;',
  24932. ' };',
  24933. '});',
  24934. 'this.b = null;',
  24935. 'this.i = null;',
  24936. 'this.i2 = null;'
  24937. ]),
  24938. LinesToStr([ // $mod.$main
  24939. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  24940. 'try {',
  24941. ' while ($in.MoveNext()){',
  24942. ' $mod.i = $in.FCurrent;',
  24943. ' $mod.i2 = $mod.i;',
  24944. ' }',
  24945. '} finally {',
  24946. ' $in = rtl.freeLoc($in)',
  24947. '};',
  24948. '']));
  24949. end;
  24950. procedure TTestModule.TestClassHelper_PassProperty;
  24951. begin
  24952. StartProgram(false);
  24953. Add([
  24954. 'type',
  24955. ' TObject = class',
  24956. ' FField: TObject;',
  24957. ' property Field: TObject read FField write FField;',
  24958. ' end;',
  24959. ' THelper = class helper for TObject',
  24960. ' procedure Fly;',
  24961. ' class procedure Run;',
  24962. ' class procedure Jump; static;',
  24963. ' end;',
  24964. 'procedure THelper.Fly;',
  24965. 'begin',
  24966. ' Field.Fly;',
  24967. ' Field.Run;',
  24968. ' Field.Jump;',
  24969. ' with Field do begin',
  24970. ' Fly;',
  24971. ' Run;',
  24972. ' Jump;',
  24973. ' end;',
  24974. 'end;',
  24975. 'class procedure THelper.Run;',
  24976. 'begin',
  24977. 'end;',
  24978. 'class procedure THelper.Jump;',
  24979. 'begin',
  24980. 'end;',
  24981. 'var',
  24982. ' b: TObject;',
  24983. 'begin',
  24984. ' b.Field.Fly;',
  24985. ' b.Field.Run;',
  24986. ' b.Field.Jump;',
  24987. ' with b do begin',
  24988. ' Field.Run;',
  24989. ' Field.Fly;',
  24990. ' Field.Jump;',
  24991. ' end;',
  24992. ' with b.Field do begin',
  24993. ' Run;',
  24994. ' Fly;',
  24995. ' Jump;',
  24996. ' end;',
  24997. '']);
  24998. ConvertProgram;
  24999. CheckSource('TestClassHelper_PassProperty',
  25000. LinesToStr([ // statements
  25001. 'rtl.createClass(this, "TObject", null, function () {',
  25002. ' this.$init = function () {',
  25003. ' this.FField = null;',
  25004. ' };',
  25005. ' this.$final = function () {',
  25006. ' this.FField = undefined;',
  25007. ' };',
  25008. '});',
  25009. 'rtl.createHelper(this, "THelper", null, function () {',
  25010. ' this.Fly = function () {',
  25011. ' $mod.THelper.Fly.call(this.FField);',
  25012. ' $mod.THelper.Run.call(this.FField.$class);',
  25013. ' $mod.THelper.Jump();',
  25014. ' var $with = this.FField;',
  25015. ' $mod.THelper.Fly.call($with);',
  25016. ' $mod.THelper.Run.call($with.$class);',
  25017. ' $mod.THelper.Jump();',
  25018. ' };',
  25019. ' this.Run = function () {',
  25020. ' };',
  25021. ' this.Jump = function () {',
  25022. ' };',
  25023. '});',
  25024. 'this.b = null;',
  25025. '']),
  25026. LinesToStr([ // $mod.$main
  25027. '$mod.THelper.Fly.call($mod.b.FField);',
  25028. '$mod.THelper.Run.call($mod.b.FField.$class);',
  25029. '$mod.THelper.Jump();',
  25030. 'var $with = $mod.b;',
  25031. '$mod.THelper.Run.call($with.FField.$class);',
  25032. '$mod.THelper.Fly.call($with.FField);',
  25033. '$mod.THelper.Jump();',
  25034. 'var $with1 = $mod.b.FField;',
  25035. '$mod.THelper.Run.call($with1.$class);',
  25036. '$mod.THelper.Fly.call($with1);',
  25037. '$mod.THelper.Jump();',
  25038. '']));
  25039. end;
  25040. procedure TTestModule.TestExtClassHelper_ClassVar;
  25041. begin
  25042. StartProgram(false);
  25043. Add([
  25044. '{$modeswitch externalclass}',
  25045. 'type',
  25046. ' TExtA = class external name ''ExtObj''',
  25047. ' end;',
  25048. ' THelper = class helper for TExtA',
  25049. ' const',
  25050. ' One = 1;',
  25051. ' Two: word = 2;',
  25052. ' class var',
  25053. ' Glob: word;',
  25054. ' function Foo(w: word): word;',
  25055. ' class function Bar(w: word): word; static;',
  25056. ' end;',
  25057. 'function THelper.foo(w: word): word;',
  25058. 'begin',
  25059. ' Result:=w;',
  25060. ' Two:=One+w;',
  25061. ' Glob:=Glob;',
  25062. ' Result:=Self.Glob;',
  25063. ' Self.Glob:=Self.Glob;',
  25064. ' with Self do Glob:=Glob;',
  25065. 'end;',
  25066. 'class function THelper.bar(w: word): word;',
  25067. 'begin',
  25068. ' Result:=w;',
  25069. ' Two:=One;',
  25070. ' Glob:=Glob;',
  25071. 'end;',
  25072. 'var o: TExtA;',
  25073. 'begin',
  25074. ' texta.two:=texta.one;',
  25075. ' texta.Glob:=texta.Glob;',
  25076. ' with texta do begin',
  25077. ' two:=one;',
  25078. ' Glob:=Glob;',
  25079. ' end;',
  25080. ' o.two:=o.one;',
  25081. ' o.Glob:=o.Glob;',
  25082. ' with o do begin',
  25083. ' two:=one;',
  25084. ' Glob:=Glob;',
  25085. ' end;',
  25086. '']);
  25087. ConvertProgram;
  25088. CheckSource('TestExtClassHelper_ClassVar',
  25089. LinesToStr([ // statements
  25090. 'rtl.createHelper(this, "THelper", null, function () {',
  25091. ' this.One = 1;',
  25092. ' this.Two = 2;',
  25093. ' this.Glob = 0;',
  25094. ' this.Foo = function (w) {',
  25095. ' var Result = 0;',
  25096. ' Result = w;',
  25097. ' $mod.THelper.Two = 1 + w;',
  25098. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  25099. ' Result = $mod.THelper.Glob;',
  25100. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  25101. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  25102. ' return Result;',
  25103. ' };',
  25104. ' this.Bar = function (w) {',
  25105. ' var Result = 0;',
  25106. ' Result = w;',
  25107. ' $mod.THelper.Two = 1;',
  25108. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  25109. ' return Result;',
  25110. ' };',
  25111. '});',
  25112. 'this.o = null;',
  25113. '']),
  25114. LinesToStr([ // $mod.$main
  25115. '$mod.THelper.Two = 1;',
  25116. '$mod.THelper.Glob = $mod.THelper.Glob;',
  25117. '$mod.THelper.Two = 1;',
  25118. '$mod.THelper.Glob = $mod.THelper.Glob;',
  25119. '$mod.THelper.Two = 1;',
  25120. '$mod.THelper.Glob = $mod.THelper.Glob;',
  25121. 'var $with = $mod.o;',
  25122. '$mod.THelper.Two = 1;',
  25123. '$mod.THelper.Glob = $mod.THelper.Glob;',
  25124. '']));
  25125. end;
  25126. procedure TTestModule.TestExtClassHelper_Method_Call;
  25127. begin
  25128. StartProgram(false);
  25129. Add([
  25130. '{$modeswitch externalclass}',
  25131. 'type',
  25132. ' TFly = function(w: word): word of object;',
  25133. ' TExtA = class external name ''ExtObj''',
  25134. ' procedure Run(w: word = 10);',
  25135. ' end;',
  25136. ' THelper = class helper for TExtA',
  25137. ' function Foo(w: word = 1): word;',
  25138. ' function Fly(w: word = 2): word; external name ''Fly'';',
  25139. ' end;',
  25140. 'var p: TFly;',
  25141. 'function THelper.foo(w: word): word;',
  25142. 'begin',
  25143. ' Run;',
  25144. ' Run();',
  25145. ' Run(11);',
  25146. ' Foo;',
  25147. ' Foo();',
  25148. ' Foo(12);',
  25149. ' Self.Foo;',
  25150. ' Self.Foo();',
  25151. ' Self.Foo(13);',
  25152. ' Fly;',
  25153. ' Fly();',
  25154. ' with Self do begin',
  25155. ' Foo;',
  25156. ' Foo();',
  25157. ' Foo(14);',
  25158. ' Fly;',
  25159. ' Fly();',
  25160. ' end;',
  25161. ' p:=@Fly;',
  25162. 'end;',
  25163. 'var Obj: TExtA;',
  25164. 'begin',
  25165. ' obj.Foo;',
  25166. ' obj.Foo();',
  25167. ' obj.Foo(21);',
  25168. ' obj.Fly;',
  25169. ' obj.Fly();',
  25170. ' with obj do begin',
  25171. ' Foo;',
  25172. ' Foo();',
  25173. ' Foo(22);',
  25174. ' Fly;',
  25175. ' Fly();',
  25176. ' end;',
  25177. ' p:[email protected];',
  25178. '']);
  25179. ConvertProgram;
  25180. CheckSource('TestExtClassHelper_Method_Call',
  25181. LinesToStr([ // statements
  25182. 'rtl.createHelper(this, "THelper", null, function () {',
  25183. ' this.Foo = function (w) {',
  25184. ' var Result = 0;',
  25185. ' this.Run(10);',
  25186. ' this.Run(10);',
  25187. ' this.Run(11);',
  25188. ' $mod.THelper.Foo.call(this, 1);',
  25189. ' $mod.THelper.Foo.call(this, 1);',
  25190. ' $mod.THelper.Foo.call(this, 12);',
  25191. ' $mod.THelper.Foo.call(this, 1);',
  25192. ' $mod.THelper.Foo.call(this, 1);',
  25193. ' $mod.THelper.Foo.call(this, 13);',
  25194. ' this.Fly(2);',
  25195. ' this.Fly(2);',
  25196. ' $mod.THelper.Foo.call(this, 1);',
  25197. ' $mod.THelper.Foo.call(this, 1);',
  25198. ' $mod.THelper.Foo.call(this, 14);',
  25199. ' this.Fly(2);',
  25200. ' this.Fly(2);',
  25201. ' $mod.p = rtl.createCallback(this, "Fly");',
  25202. ' return Result;',
  25203. ' };',
  25204. '});',
  25205. 'this.p = null;',
  25206. 'this.Obj = null;',
  25207. '']),
  25208. LinesToStr([ // $mod.$main
  25209. '$mod.THelper.Foo.call($mod.Obj, 1);',
  25210. '$mod.THelper.Foo.call($mod.Obj, 1);',
  25211. '$mod.THelper.Foo.call($mod.Obj, 21);',
  25212. '$mod.Obj.Fly(2);',
  25213. '$mod.Obj.Fly(2);',
  25214. 'var $with = $mod.Obj;',
  25215. '$mod.THelper.Foo.call($with, 1);',
  25216. '$mod.THelper.Foo.call($with, 1);',
  25217. '$mod.THelper.Foo.call($with, 22);',
  25218. '$with.Fly(2);',
  25219. '$with.Fly(2);',
  25220. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  25221. '']));
  25222. end;
  25223. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  25224. begin
  25225. StartProgram(false);
  25226. Add([
  25227. '{$modeswitch externalclass}',
  25228. 'type',
  25229. ' TExtA = class external name ''ExtObj''',
  25230. ' procedure Run(w: word = 10);',
  25231. ' end;',
  25232. ' THelper = class helper for TExtA',
  25233. ' class procedure Fly;',
  25234. ' end;',
  25235. 'class procedure THelper.Fly;',
  25236. 'begin end;',
  25237. 'begin',
  25238. '']);
  25239. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  25240. nHelperClassMethodForExtClassMustBeStatic);
  25241. ConvertProgram;
  25242. end;
  25243. procedure TTestModule.TestRecordHelper_ClassVar;
  25244. begin
  25245. StartProgram(false);
  25246. Add([
  25247. 'type',
  25248. ' TRec = record',
  25249. ' end;',
  25250. ' THelper = record helper for TRec',
  25251. ' const',
  25252. ' One = 1;',
  25253. ' Two: word = 2;',
  25254. ' class var',
  25255. ' Glob: word;',
  25256. ' function Foo(w: word): word;',
  25257. ' class function Bar(w: word): word; static;',
  25258. ' end;',
  25259. 'function THelper.foo(w: word): word;',
  25260. 'begin',
  25261. ' Result:=w;',
  25262. ' Two:=One+w;',
  25263. ' Glob:=Glob;',
  25264. ' Result:=Self.Glob;',
  25265. ' Self.Glob:=Self.Glob;',
  25266. ' with Self do Glob:=Glob;',
  25267. ' Self:=Self;',
  25268. 'end;',
  25269. 'class function THelper.bar(w: word): word;',
  25270. 'begin',
  25271. ' Result:=w;',
  25272. ' Two:=One;',
  25273. ' Glob:=Glob;',
  25274. 'end;',
  25275. 'var r: TRec;',
  25276. 'begin',
  25277. ' trec.two:=trec.one;',
  25278. ' trec.Glob:=trec.Glob;',
  25279. ' with trec do begin',
  25280. ' two:=one;',
  25281. ' Glob:=Glob;',
  25282. ' end;',
  25283. ' r.two:=r.one;',
  25284. ' r.Glob:=r.Glob;',
  25285. ' with r do begin',
  25286. ' two:=one;',
  25287. ' Glob:=Glob;',
  25288. ' end;',
  25289. '']);
  25290. ConvertProgram;
  25291. CheckSource('TestRecordHelper_ClassVar',
  25292. LinesToStr([ // statements
  25293. 'rtl.recNewT(this, "TRec", function () {',
  25294. ' this.$eq = function (b) {',
  25295. ' return true;',
  25296. ' };',
  25297. ' this.$assign = function (s) {',
  25298. ' return this;',
  25299. ' };',
  25300. '});',
  25301. 'rtl.createHelper(this, "THelper", null, function () {',
  25302. ' this.One = 1;',
  25303. ' this.Two = 2;',
  25304. ' this.Glob = 0;',
  25305. ' this.Foo = function (w) {',
  25306. ' var Result = 0;',
  25307. ' Result = w;',
  25308. ' $mod.THelper.Two = 1 + w;',
  25309. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  25310. ' Result = $mod.THelper.Glob;',
  25311. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  25312. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  25313. ' this.$assign(this);',
  25314. ' return Result;',
  25315. ' };',
  25316. ' this.Bar = function (w) {',
  25317. ' var Result = 0;',
  25318. ' Result = w;',
  25319. ' $mod.THelper.Two = 1;',
  25320. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  25321. ' return Result;',
  25322. ' };',
  25323. '});',
  25324. 'this.r = this.TRec.$new();',
  25325. '']),
  25326. LinesToStr([ // $mod.$main
  25327. '$mod.THelper.Two = 1;',
  25328. '$mod.THelper.Glob = $mod.THelper.Glob;',
  25329. 'var $with = $mod.TRec;',
  25330. '$mod.THelper.Two = 1;',
  25331. '$mod.THelper.Glob = $mod.THelper.Glob;',
  25332. '$mod.THelper.Two = 1;',
  25333. '$mod.THelper.Glob = $mod.THelper.Glob;',
  25334. 'var $with1 = $mod.r;',
  25335. '$mod.THelper.Two = 1;',
  25336. '$mod.THelper.Glob = $mod.THelper.Glob;',
  25337. '']));
  25338. end;
  25339. procedure TTestModule.TestRecordHelper_Method_Call;
  25340. begin
  25341. StartProgram(false);
  25342. Add([
  25343. '{$modeswitch AdvancedRecords}',
  25344. 'type',
  25345. ' TRec = record',
  25346. ' procedure Run(w: word = 10);',
  25347. ' end;',
  25348. ' THelper = record helper for TRec',
  25349. ' function Foo(w: word = 1): word;',
  25350. ' end;',
  25351. 'procedure TRec.Run(w: word);',
  25352. 'begin',
  25353. ' Foo;',
  25354. ' Foo();',
  25355. ' Foo(2);',
  25356. ' Self.Foo;',
  25357. ' Self.Foo();',
  25358. ' Self.Foo(3);',
  25359. ' with Self do begin',
  25360. ' Foo;',
  25361. ' Foo();',
  25362. ' Foo(4);',
  25363. ' end;',
  25364. 'end;',
  25365. 'function THelper.foo(w: word): word;',
  25366. 'begin',
  25367. ' Run;',
  25368. ' Run();',
  25369. ' Run(11);',
  25370. ' Foo;',
  25371. ' Foo();',
  25372. ' Foo(12);',
  25373. ' Self.Foo;',
  25374. ' Self.Foo();',
  25375. ' Self.Foo(13);',
  25376. ' with Self do begin',
  25377. ' Foo;',
  25378. ' Foo();',
  25379. ' Foo(14);',
  25380. ' end;',
  25381. 'end;',
  25382. 'var Rec: TRec;',
  25383. 'begin',
  25384. ' Rec.Foo;',
  25385. ' Rec.Foo();',
  25386. ' Rec.Foo(21);',
  25387. ' with Rec do begin',
  25388. ' Foo;',
  25389. ' Foo();',
  25390. ' Foo(22);',
  25391. ' end;',
  25392. '']);
  25393. ConvertProgram;
  25394. CheckSource('TestRecordHelper_Method_Call',
  25395. LinesToStr([ // statements
  25396. 'rtl.recNewT(this, "TRec", function () {',
  25397. ' this.$eq = function (b) {',
  25398. ' return true;',
  25399. ' };',
  25400. ' this.$assign = function (s) {',
  25401. ' return this;',
  25402. ' };',
  25403. ' this.Run = function (w) {',
  25404. ' $mod.THelper.Foo.call(this, 1);',
  25405. ' $mod.THelper.Foo.call(this, 1);',
  25406. ' $mod.THelper.Foo.call(this, 2);',
  25407. ' $mod.THelper.Foo.call(this, 1);',
  25408. ' $mod.THelper.Foo.call(this, 1);',
  25409. ' $mod.THelper.Foo.call(this, 3);',
  25410. ' $mod.THelper.Foo.call(this, 1);',
  25411. ' $mod.THelper.Foo.call(this, 1);',
  25412. ' $mod.THelper.Foo.call(this, 4);',
  25413. ' };',
  25414. '});',
  25415. 'rtl.createHelper(this, "THelper", null, function () {',
  25416. ' this.Foo = function (w) {',
  25417. ' var Result = 0;',
  25418. ' this.Run(10);',
  25419. ' this.Run(10);',
  25420. ' this.Run(11);',
  25421. ' $mod.THelper.Foo.call(this, 1);',
  25422. ' $mod.THelper.Foo.call(this, 1);',
  25423. ' $mod.THelper.Foo.call(this, 12);',
  25424. ' $mod.THelper.Foo.call(this, 1);',
  25425. ' $mod.THelper.Foo.call(this, 1);',
  25426. ' $mod.THelper.Foo.call(this, 13);',
  25427. ' $mod.THelper.Foo.call(this, 1);',
  25428. ' $mod.THelper.Foo.call(this, 1);',
  25429. ' $mod.THelper.Foo.call(this, 14);',
  25430. ' return Result;',
  25431. ' };',
  25432. '});',
  25433. 'this.Rec = this.TRec.$new();',
  25434. '']),
  25435. LinesToStr([ // $mod.$main
  25436. '$mod.THelper.Foo.call($mod.Rec, 1);',
  25437. '$mod.THelper.Foo.call($mod.Rec, 1);',
  25438. '$mod.THelper.Foo.call($mod.Rec, 21);',
  25439. 'var $with = $mod.Rec;',
  25440. '$mod.THelper.Foo.call($with, 1);',
  25441. '$mod.THelper.Foo.call($with, 1);',
  25442. '$mod.THelper.Foo.call($with, 22);',
  25443. '']));
  25444. end;
  25445. procedure TTestModule.TestRecordHelper_Constructor;
  25446. begin
  25447. StartProgram(false);
  25448. Add([
  25449. '{$modeswitch AdvancedRecords}',
  25450. 'type',
  25451. ' TRec = record',
  25452. ' constructor Create(w: word);',
  25453. ' end;',
  25454. ' THelper = record helper for TRec',
  25455. ' constructor NewHlp(w: word);',
  25456. ' end;',
  25457. 'var',
  25458. ' Rec: TRec;',
  25459. 'constructor TRec.Create(w: word);',
  25460. 'begin',
  25461. ' NewHlp(2);', // normal call
  25462. ' trec.NewHlp(3);', // new instance
  25463. 'end;',
  25464. 'constructor THelper.NewHlp(w: word);',
  25465. 'begin',
  25466. ' create(2);', // normal call
  25467. ' trec.create(3);', // new instance
  25468. ' NewHlp(4);', // normal call
  25469. ' trec.NewHlp(5);', // new instance
  25470. 'end;',
  25471. 'begin',
  25472. ' rec.newhlp(2);', // normal call
  25473. ' with rec do newhlp(12);', // normal call
  25474. ' trec.newhlp(3);', // new instance
  25475. ' with trec do newhlp(13);', // new instance
  25476. '']);
  25477. ConvertProgram;
  25478. CheckSource('TestRecordHelper_Constructor',
  25479. LinesToStr([ // statements
  25480. 'rtl.recNewT(this, "TRec", function () {',
  25481. ' this.$eq = function (b) {',
  25482. ' return true;',
  25483. ' };',
  25484. ' this.$assign = function (s) {',
  25485. ' return this;',
  25486. ' };',
  25487. ' this.Create = function (w) {',
  25488. ' $mod.THelper.NewHlp.call(this, 2);',
  25489. ' $mod.THelper.$new("NewHlp", [3]);',
  25490. ' return this;',
  25491. ' };',
  25492. '});',
  25493. 'rtl.createHelper(this, "THelper", null, function () {',
  25494. ' this.NewHlp = function (w) {',
  25495. ' this.Create(2);',
  25496. ' $mod.TRec.$new().Create(3);',
  25497. ' $mod.THelper.NewHlp.call(this, 4);',
  25498. ' $mod.THelper.$new("NewHlp", [5]);',
  25499. ' return this;',
  25500. ' };',
  25501. ' this.$new = function (fn, args) {',
  25502. ' return this[fn].apply($mod.TRec.$new(), args);',
  25503. ' };',
  25504. '});',
  25505. 'this.Rec = this.TRec.$new();',
  25506. '']),
  25507. LinesToStr([ // $mod.$main
  25508. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  25509. 'var $with = $mod.Rec;',
  25510. '$mod.THelper.NewHlp.call($with, 12);',
  25511. '$mod.THelper.$new("NewHlp", [3]);',
  25512. 'var $with1 = $mod.TRec;',
  25513. '$mod.THelper.$new("NewHlp", [13]);',
  25514. '']));
  25515. end;
  25516. procedure TTestModule.TestTypeHelper_ClassVar;
  25517. begin
  25518. StartProgram(false);
  25519. Add([
  25520. '{$modeswitch typehelpers}',
  25521. 'type',
  25522. ' THelper = type helper for byte',
  25523. ' const',
  25524. ' One = 1;',
  25525. ' Two: word = 2;',
  25526. ' class var',
  25527. ' Glob: word;',
  25528. ' function Foo(w: word): word;',
  25529. ' class function Bar(w: word): word; static;',
  25530. ' end;',
  25531. 'function THelper.foo(w: word): word;',
  25532. 'begin',
  25533. ' Result:=w;',
  25534. ' Two:=One+w;',
  25535. ' Glob:=Glob;',
  25536. ' Result:=Self.Glob;',
  25537. ' Self.Glob:=Self.Glob;',
  25538. ' with Self do Glob:=Glob;',
  25539. 'end;',
  25540. 'class function THelper.bar(w: word): word;',
  25541. 'begin',
  25542. ' Result:=w;',
  25543. ' Two:=One;',
  25544. ' Glob:=Glob;',
  25545. 'end;',
  25546. 'var b: byte;',
  25547. 'begin',
  25548. ' byte.two:=byte.one;',
  25549. ' byte.Glob:=byte.Glob;',
  25550. ' with byte do begin',
  25551. ' two:=one;',
  25552. ' Glob:=Glob;',
  25553. ' end;',
  25554. ' b.two:=b.one;',
  25555. ' b.Glob:=b.Glob;',
  25556. ' with b do begin',
  25557. ' two:=one;',
  25558. ' Glob:=Glob;',
  25559. ' end;',
  25560. '']);
  25561. ConvertProgram;
  25562. CheckSource('TestTypeHelper_ClassVar',
  25563. LinesToStr([ // statements
  25564. 'rtl.createHelper(this, "THelper", null, function () {',
  25565. ' this.One = 1;',
  25566. ' this.Two = 2;',
  25567. ' this.Glob = 0;',
  25568. ' this.Foo = function (w) {',
  25569. ' var Result = 0;',
  25570. ' Result = w;',
  25571. ' $mod.THelper.Two = 1 + w;',
  25572. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  25573. ' Result = $mod.THelper.Glob;',
  25574. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  25575. ' var $with = this.get();',
  25576. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  25577. ' return Result;',
  25578. ' };',
  25579. ' this.Bar = function (w) {',
  25580. ' var Result = 0;',
  25581. ' Result = w;',
  25582. ' $mod.THelper.Two = 1;',
  25583. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  25584. ' return Result;',
  25585. ' };',
  25586. '});',
  25587. 'this.b = 0;',
  25588. '']),
  25589. LinesToStr([ // $mod.$main
  25590. '$mod.THelper.Two = 1;',
  25591. '$mod.THelper.Glob = $mod.THelper.Glob;',
  25592. '$mod.THelper.Two = 1;',
  25593. '$mod.THelper.Glob = $mod.THelper.Glob;',
  25594. '$mod.THelper.Two = 1;',
  25595. '$mod.THelper.Glob = $mod.THelper.Glob;',
  25596. 'var $with = $mod.b;',
  25597. '$mod.THelper.Two = 1;',
  25598. '$mod.THelper.Glob = $mod.THelper.Glob;',
  25599. '']));
  25600. end;
  25601. procedure TTestModule.TestTypeHelper_PassResultElement;
  25602. begin
  25603. StartProgram(false);
  25604. Add([
  25605. '{$modeswitch typehelpers}',
  25606. 'type',
  25607. ' THelper = type helper for word',
  25608. ' procedure DoIt(e: byte = 123);',
  25609. ' class procedure DoSome(e: byte = 456); static;',
  25610. ' end;',
  25611. 'procedure THelper.DoIt(e: byte);',
  25612. 'begin',
  25613. 'end;',
  25614. 'class procedure THelper.DoSome(e: byte);',
  25615. 'begin',
  25616. 'end;',
  25617. 'function Foo(w: word): word;',
  25618. 'begin',
  25619. ' Result.DoIt;',
  25620. ' Result.DoIt();',
  25621. ' Result.DoSome;',
  25622. ' Result.DoSome();',
  25623. ' with Result do begin',
  25624. ' DoIt;',
  25625. ' DoIt();',
  25626. ' DoSome;',
  25627. ' DoSome();',
  25628. ' end;',
  25629. 'end;',
  25630. 'begin',
  25631. '']);
  25632. ConvertProgram;
  25633. CheckSource('TestTypeHelper_PassResultElement',
  25634. LinesToStr([ // statements
  25635. 'rtl.createHelper(this, "THelper", null, function () {',
  25636. ' this.DoIt = function (e) {',
  25637. ' };',
  25638. ' this.DoSome = function (e) {',
  25639. ' };',
  25640. '});',
  25641. 'this.Foo = function (w) {',
  25642. ' var Result = 0;',
  25643. ' $mod.THelper.DoIt.call({',
  25644. ' get: function () {',
  25645. ' return Result;',
  25646. ' },',
  25647. ' set: function (v) {',
  25648. ' Result = v;',
  25649. ' }',
  25650. ' }, 123);',
  25651. ' $mod.THelper.DoIt.call({',
  25652. ' get: function () {',
  25653. ' return Result;',
  25654. ' },',
  25655. ' set: function (v) {',
  25656. ' Result = v;',
  25657. ' }',
  25658. ' }, 123);',
  25659. ' $mod.THelper.DoSome(456);',
  25660. ' $mod.THelper.DoSome(456);',
  25661. ' $mod.THelper.DoIt.call({',
  25662. ' get: function () {',
  25663. ' return Result;',
  25664. ' },',
  25665. ' set: function (v) {',
  25666. ' Result = v;',
  25667. ' }',
  25668. ' }, 123);',
  25669. ' $mod.THelper.DoIt.call({',
  25670. ' get: function () {',
  25671. ' return Result;',
  25672. ' },',
  25673. ' set: function (v) {',
  25674. ' Result = v;',
  25675. ' }',
  25676. ' }, 123);',
  25677. ' $mod.THelper.DoSome(456);',
  25678. ' $mod.THelper.DoSome(456);',
  25679. ' return Result;',
  25680. '};',
  25681. '']),
  25682. LinesToStr([ // $mod.$main
  25683. '']));
  25684. end;
  25685. procedure TTestModule.TestTypeHelper_PassArgs;
  25686. begin
  25687. StartProgram(false);
  25688. Add([
  25689. '{$modeswitch typehelpers}',
  25690. 'type',
  25691. ' THelper = type helper for word',
  25692. ' procedure DoIt(e: byte = 123);',
  25693. ' end;',
  25694. 'procedure THelper.DoIt(e: byte);',
  25695. 'begin',
  25696. 'end;',
  25697. 'procedure FooDefault(a: word);',
  25698. 'begin',
  25699. ' a.DoIt;',
  25700. ' with a do DoIt;',
  25701. 'end;',
  25702. 'procedure FooConst(const a: word);',
  25703. 'begin',
  25704. ' a.DoIt;',
  25705. ' with a do DoIt;',
  25706. 'end;',
  25707. 'procedure FooVar(var a: word);',
  25708. 'begin',
  25709. ' a.DoIt;',
  25710. ' with a do DoIt;',
  25711. 'end;',
  25712. 'begin',
  25713. '']);
  25714. ConvertProgram;
  25715. CheckSource('TestTypeHelper_PassArgs',
  25716. LinesToStr([ // statements
  25717. 'rtl.createHelper(this, "THelper", null, function () {',
  25718. ' this.DoIt = function (e) {',
  25719. ' };',
  25720. '});',
  25721. 'this.FooDefault = function (a) {',
  25722. ' $mod.THelper.DoIt.call({',
  25723. ' get: function () {',
  25724. ' return a;',
  25725. ' },',
  25726. ' set: function (v) {',
  25727. ' a = v;',
  25728. ' }',
  25729. ' }, 123);',
  25730. ' $mod.THelper.DoIt.call({',
  25731. ' get: function () {',
  25732. ' return a;',
  25733. ' },',
  25734. ' set: function (v) {',
  25735. ' a = v;',
  25736. ' }',
  25737. ' }, 123);',
  25738. '};',
  25739. 'this.FooConst = function (a) {',
  25740. ' $mod.THelper.DoIt.call({',
  25741. ' get: function () {',
  25742. ' return a;',
  25743. ' },',
  25744. ' set: function (v) {',
  25745. ' rtl.raiseE("EPropReadOnly");',
  25746. ' }',
  25747. ' }, 123);',
  25748. ' $mod.THelper.DoIt.call({',
  25749. ' get: function () {',
  25750. ' return a;',
  25751. ' },',
  25752. ' set: function () {',
  25753. ' rtl.raiseE("EPropReadOnly");',
  25754. ' }',
  25755. ' }, 123);',
  25756. '};',
  25757. 'this.FooVar = function (a) {',
  25758. ' $mod.THelper.DoIt.call(a, 123);',
  25759. ' var $with = a.get();',
  25760. ' $mod.THelper.DoIt.call(a, 123);',
  25761. '};',
  25762. '']),
  25763. LinesToStr([ // $mod.$main
  25764. '']));
  25765. end;
  25766. procedure TTestModule.TestTypeHelper_PassVarConst;
  25767. begin
  25768. StartProgram(false);
  25769. Add([
  25770. '{$modeswitch typehelpers}',
  25771. 'type',
  25772. ' THelper = type helper for word',
  25773. ' procedure DoIt(e: byte = 123);',
  25774. ' end;',
  25775. 'procedure THelper.DoIt(e: byte);',
  25776. 'begin',
  25777. 'end;',
  25778. 'var a: word;',
  25779. 'const c: word = 2;',
  25780. '{$writeableconst off}',
  25781. 'const r: word = 3;',
  25782. 'begin',
  25783. ' a.DoIt;',
  25784. ' with a do DoIt;',
  25785. ' c.DoIt;',
  25786. ' with c do DoIt;',
  25787. ' r.DoIt;',
  25788. ' with r do DoIt;',
  25789. '']);
  25790. ConvertProgram;
  25791. CheckSource('TestTypeHelper_PassVarConst',
  25792. LinesToStr([ // statements
  25793. 'rtl.createHelper(this, "THelper", null, function () {',
  25794. ' this.DoIt = function (e) {',
  25795. ' };',
  25796. '});',
  25797. 'this.a = 0;',
  25798. 'this.c = 2;',
  25799. 'this.r = 3;',
  25800. '']),
  25801. LinesToStr([ // $mod.$main
  25802. '$mod.THelper.DoIt.call({',
  25803. ' p: $mod,',
  25804. ' get: function () {',
  25805. ' return this.p.a;',
  25806. ' },',
  25807. ' set: function (v) {',
  25808. ' this.p.a = v;',
  25809. ' }',
  25810. '}, 123);',
  25811. 'var $with = $mod.a;',
  25812. '$mod.THelper.DoIt.call({',
  25813. ' get: function () {',
  25814. ' return $with;',
  25815. ' },',
  25816. ' set: function (v) {',
  25817. ' $with = v;',
  25818. ' }',
  25819. '}, 123);',
  25820. '$mod.THelper.DoIt.call({',
  25821. ' p: $mod,',
  25822. ' get: function () {',
  25823. ' return this.p.c;',
  25824. ' },',
  25825. ' set: function (v) {',
  25826. ' this.p.c = v;',
  25827. ' }',
  25828. '}, 123);',
  25829. 'var $with1 = $mod.c;',
  25830. '$mod.THelper.DoIt.call({',
  25831. ' get: function () {',
  25832. ' return $with1;',
  25833. ' },',
  25834. ' set: function (v) {',
  25835. ' $with1 = v;',
  25836. ' }',
  25837. '}, 123);',
  25838. '$mod.THelper.DoIt.call({',
  25839. ' get: function () {',
  25840. ' return 3;',
  25841. ' },',
  25842. ' set: function (v) {',
  25843. ' rtl.raiseE("EPropReadOnly");',
  25844. ' }',
  25845. '}, 123);',
  25846. 'var $with2 = 3;',
  25847. ' $mod.THelper.DoIt.call({',
  25848. ' get: function () {',
  25849. ' return $with2;',
  25850. ' },',
  25851. ' set: function () {',
  25852. ' rtl.raiseE("EPropReadOnly");',
  25853. ' }',
  25854. ' }, 123);',
  25855. '']));
  25856. end;
  25857. procedure TTestModule.TestTypeHelper_PassFuncResult;
  25858. begin
  25859. StartProgram(false);
  25860. Add([
  25861. '{$modeswitch typehelpers}',
  25862. 'type',
  25863. ' THelper = type helper for word',
  25864. ' procedure DoIt(e: byte = 123);',
  25865. ' end;',
  25866. 'procedure THelper.DoIt(e: byte);',
  25867. 'begin',
  25868. 'end;',
  25869. 'function Foo(b: byte = 1): word;',
  25870. 'begin',
  25871. 'end;',
  25872. 'begin',
  25873. ' Foo.DoIt;',
  25874. ' Foo().DoIt;',
  25875. ' with Foo do DoIt;',
  25876. ' with Foo() do DoIt;',
  25877. '']);
  25878. ConvertProgram;
  25879. CheckSource('TestTypeHelper_PassFuncResult',
  25880. LinesToStr([ // statements
  25881. 'rtl.createHelper(this, "THelper", null, function () {',
  25882. ' this.DoIt = function (e) {',
  25883. ' };',
  25884. '});',
  25885. 'this.Foo = function (b) {',
  25886. ' var Result = 0;',
  25887. ' return Result;',
  25888. '};',
  25889. '']),
  25890. LinesToStr([ // $mod.$main
  25891. '$mod.THelper.DoIt.call({',
  25892. ' a: $mod.Foo(1),',
  25893. ' get: function () {',
  25894. ' return this.a;',
  25895. ' },',
  25896. ' set: function (v) {',
  25897. ' this.a = v;',
  25898. ' }',
  25899. '}, 123);',
  25900. '$mod.THelper.DoIt.call({',
  25901. ' a: $mod.Foo(1),',
  25902. ' get: function () {',
  25903. ' return this.a;',
  25904. ' },',
  25905. ' set: function (v) {',
  25906. ' this.a = v;',
  25907. ' }',
  25908. '}, 123);',
  25909. 'var $with = $mod.Foo(1);',
  25910. '$mod.THelper.DoIt.call({',
  25911. ' get: function () {',
  25912. ' return $with;',
  25913. ' },',
  25914. ' set: function (v) {',
  25915. ' $with = v;',
  25916. ' }',
  25917. '}, 123);',
  25918. 'var $with1 = $mod.Foo(1);',
  25919. '$mod.THelper.DoIt.call({',
  25920. ' get: function () {',
  25921. ' return $with1;',
  25922. ' },',
  25923. ' set: function (v) {',
  25924. ' $with1 = v;',
  25925. ' }',
  25926. '}, 123);',
  25927. '']));
  25928. end;
  25929. procedure TTestModule.TestTypeHelper_PassPropertyField;
  25930. begin
  25931. StartProgram(false);
  25932. Add([
  25933. '{$modeswitch typehelpers}',
  25934. 'type',
  25935. ' TObject = class',
  25936. ' FField: word;',
  25937. ' procedure SetField(Value: word);',
  25938. ' property Field: word read FField write SetField;',
  25939. ' end;',
  25940. ' THelper = type helper for word',
  25941. ' procedure Fly;',
  25942. ' class procedure Run; static;',
  25943. ' end;',
  25944. 'procedure TObject.SetField(Value: word);',
  25945. 'begin',
  25946. ' Field.Fly;',
  25947. ' Field.Run;',
  25948. ' Self.Field.Fly;',
  25949. ' Self.Field.Run;',
  25950. ' with Self do begin',
  25951. ' Field.Fly;',
  25952. ' Field.Run;',
  25953. ' end;',
  25954. ' with Self.Field do begin',
  25955. ' Fly;',
  25956. ' Run;',
  25957. ' end;',
  25958. 'end;',
  25959. 'procedure THelper.Fly;',
  25960. 'begin',
  25961. 'end;',
  25962. 'class procedure THelper.Run;',
  25963. 'begin',
  25964. 'end;',
  25965. 'var',
  25966. ' o: TObject;',
  25967. 'begin',
  25968. ' o.Field.Fly;',
  25969. ' o.Field.Run;',
  25970. ' with o do begin',
  25971. ' Field.Fly;',
  25972. ' Field.Run;',
  25973. ' end;',
  25974. ' with o.Field do begin',
  25975. ' Fly;',
  25976. ' Run;',
  25977. ' end;',
  25978. '']);
  25979. ConvertProgram;
  25980. CheckSource('TestTypeHelper_PassPropertyField',
  25981. LinesToStr([ // statements
  25982. 'rtl.createClass(this, "TObject", null, function () {',
  25983. ' this.$init = function () {',
  25984. ' this.FField = 0;',
  25985. ' };',
  25986. ' this.$final = function () {',
  25987. ' };',
  25988. ' this.SetField = function (Value) {',
  25989. ' $mod.THelper.Fly.call({',
  25990. ' p: this,',
  25991. ' get: function () {',
  25992. ' return this.p.FField;',
  25993. ' },',
  25994. ' set: function (v) {',
  25995. ' this.p.FField = v;',
  25996. ' }',
  25997. ' });',
  25998. ' $mod.THelper.Run();',
  25999. ' $mod.THelper.Fly.call({',
  26000. ' p: this,',
  26001. ' get: function () {',
  26002. ' return this.p.FField;',
  26003. ' },',
  26004. ' set: function (v) {',
  26005. ' this.p.FField = v;',
  26006. ' }',
  26007. ' });',
  26008. ' $mod.THelper.Run();',
  26009. ' $mod.THelper.Fly.call({',
  26010. ' p: this,',
  26011. ' get: function () {',
  26012. ' return this.p.FField;',
  26013. ' },',
  26014. ' set: function (v) {',
  26015. ' this.p.FField = v;',
  26016. ' }',
  26017. ' });',
  26018. ' $mod.THelper.Run();',
  26019. ' var $with = this.FField;',
  26020. ' $mod.THelper.Fly.call({',
  26021. ' get: function () {',
  26022. ' return $with;',
  26023. ' },',
  26024. ' set: function (v) {',
  26025. ' $with = v;',
  26026. ' }',
  26027. ' });',
  26028. ' $mod.THelper.Run();',
  26029. ' };',
  26030. '});',
  26031. 'rtl.createHelper(this, "THelper", null, function () {',
  26032. ' this.Fly = function () {',
  26033. ' };',
  26034. ' this.Run = function () {',
  26035. ' };',
  26036. '});',
  26037. 'this.o = null;',
  26038. '']),
  26039. LinesToStr([ // $mod.$main
  26040. '$mod.THelper.Fly.call({',
  26041. ' p: $mod.o,',
  26042. ' get: function () {',
  26043. ' return this.p.FField;',
  26044. ' },',
  26045. ' set: function (v) {',
  26046. ' this.p.FField = v;',
  26047. ' }',
  26048. '});',
  26049. '$mod.THelper.Run();',
  26050. 'var $with = $mod.o;',
  26051. '$mod.THelper.Fly.call({',
  26052. ' p: $with,',
  26053. ' get: function () {',
  26054. ' return this.p.FField;',
  26055. ' },',
  26056. ' set: function (v) {',
  26057. ' this.p.FField = v;',
  26058. ' }',
  26059. '});',
  26060. '$mod.THelper.Run();',
  26061. 'var $with1 = $mod.o.FField;',
  26062. '$mod.THelper.Fly.call({',
  26063. ' get: function () {',
  26064. ' return $with1;',
  26065. ' },',
  26066. ' set: function (v) {',
  26067. ' $with1 = v;',
  26068. ' }',
  26069. '});',
  26070. '$mod.THelper.Run();',
  26071. '']));
  26072. end;
  26073. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  26074. begin
  26075. StartProgram(false);
  26076. Add([
  26077. '{$modeswitch typehelpers}',
  26078. 'type',
  26079. ' TObject = class',
  26080. ' FField: word;',
  26081. ' function GetField: word;',
  26082. ' property Field: word read GetField write FField;',
  26083. ' end;',
  26084. ' THelper = type helper for word',
  26085. ' procedure Fly;',
  26086. ' class procedure Run; static;',
  26087. ' end;',
  26088. 'function TObject.GetField: word;',
  26089. 'begin',
  26090. ' Field.Fly;',
  26091. ' Field.Run;',
  26092. ' Self.Field.Fly;',
  26093. ' Self.Field.Run;',
  26094. ' with Self do begin',
  26095. ' Field.Fly;',
  26096. ' Field.Run;',
  26097. ' end;',
  26098. ' with Self.Field do begin',
  26099. ' Fly;',
  26100. ' Run;',
  26101. ' end;',
  26102. 'end;',
  26103. 'procedure THelper.Fly;',
  26104. 'begin',
  26105. 'end;',
  26106. 'class procedure THelper.Run;',
  26107. 'begin',
  26108. 'end;',
  26109. 'var',
  26110. ' o: TObject;',
  26111. 'begin',
  26112. ' o.Field.Fly;',
  26113. ' o.Field.Run;',
  26114. ' with o do begin',
  26115. ' Field.Fly;',
  26116. ' Field.Run;',
  26117. ' end;',
  26118. ' with o.Field do begin',
  26119. ' Fly;',
  26120. ' Run;',
  26121. ' end;',
  26122. '']);
  26123. ConvertProgram;
  26124. CheckSource('TestTypeHelper_PassPropertyGetter',
  26125. LinesToStr([ // statements
  26126. 'rtl.createClass(this, "TObject", null, function () {',
  26127. ' this.$init = function () {',
  26128. ' this.FField = 0;',
  26129. ' };',
  26130. ' this.$final = function () {',
  26131. ' };',
  26132. ' this.GetField = function () {',
  26133. ' var Result = 0;',
  26134. ' $mod.THelper.Fly.call({',
  26135. ' p: this.GetField(),',
  26136. ' get: function () {',
  26137. ' return this.p;',
  26138. ' },',
  26139. ' set: function (v) {',
  26140. ' this.p = v;',
  26141. ' }',
  26142. ' });',
  26143. ' $mod.THelper.Run();',
  26144. ' $mod.THelper.Fly.call({',
  26145. ' p: this.GetField(),',
  26146. ' get: function () {',
  26147. ' return this.p;',
  26148. ' },',
  26149. ' set: function (v) {',
  26150. ' this.p = v;',
  26151. ' }',
  26152. ' });',
  26153. ' $mod.THelper.Run();',
  26154. ' $mod.THelper.Fly.call({',
  26155. ' p: this.GetField(),',
  26156. ' get: function () {',
  26157. ' return this.p;',
  26158. ' },',
  26159. ' set: function (v) {',
  26160. ' this.p = v;',
  26161. ' }',
  26162. ' });',
  26163. ' $mod.THelper.Run();',
  26164. ' var $with = this.GetField();',
  26165. ' $mod.THelper.Fly.call({',
  26166. ' get: function () {',
  26167. ' return $with;',
  26168. ' },',
  26169. ' set: function (v) {',
  26170. ' $with = v;',
  26171. ' }',
  26172. ' });',
  26173. ' $mod.THelper.Run();',
  26174. ' return Result;',
  26175. ' };',
  26176. '});',
  26177. 'rtl.createHelper(this, "THelper", null, function () {',
  26178. ' this.Fly = function () {',
  26179. ' };',
  26180. ' this.Run = function () {',
  26181. ' };',
  26182. '});',
  26183. 'this.o = null;',
  26184. '']),
  26185. LinesToStr([ // $mod.$main
  26186. '$mod.THelper.Fly.call({',
  26187. ' p: $mod.o.GetField(),',
  26188. ' get: function () {',
  26189. ' return this.p;',
  26190. ' },',
  26191. ' set: function (v) {',
  26192. ' this.p = v;',
  26193. ' }',
  26194. '});',
  26195. '$mod.THelper.Run();',
  26196. 'var $with = $mod.o;',
  26197. '$mod.THelper.Fly.call({',
  26198. ' p: $with.GetField(),',
  26199. ' get: function () {',
  26200. ' return this.p;',
  26201. ' },',
  26202. ' set: function (v) {',
  26203. ' this.p = v;',
  26204. ' }',
  26205. '});',
  26206. '$mod.THelper.Run();',
  26207. 'var $with1 = $mod.o.GetField();',
  26208. '$mod.THelper.Fly.call({',
  26209. ' get: function () {',
  26210. ' return $with1;',
  26211. ' },',
  26212. ' set: function (v) {',
  26213. ' $with1 = v;',
  26214. ' }',
  26215. '});',
  26216. '$mod.THelper.Run();',
  26217. '']));
  26218. end;
  26219. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  26220. begin
  26221. StartProgram(false);
  26222. Add([
  26223. '{$modeswitch typehelpers}',
  26224. 'type',
  26225. ' TObject = class',
  26226. ' class var FField: word;',
  26227. ' class procedure SetField(Value: word);',
  26228. ' class property Field: word read FField write SetField;',
  26229. ' end;',
  26230. ' THelper = type helper for word',
  26231. ' procedure Fly(n: byte);',
  26232. ' end;',
  26233. 'class procedure TObject.SetField(Value: word);',
  26234. 'begin',
  26235. ' Field.Fly(1);',
  26236. ' Self.Field.Fly(2);',
  26237. ' with Self do Field.Fly(3);',
  26238. ' with Self.Field do Fly(4);',
  26239. ' TObject.Field.Fly(5);',
  26240. ' with TObject do Field.Fly(6);',
  26241. ' with TObject.Field do Fly(7);',
  26242. 'end;',
  26243. 'procedure THelper.Fly(n: byte);',
  26244. 'begin',
  26245. 'end;',
  26246. 'var',
  26247. ' o: TObject;',
  26248. 'begin',
  26249. ' o.Field.Fly(11);',
  26250. ' with o do Field.Fly(12);',
  26251. ' with o.Field do Fly(13);',
  26252. ' TObject.Field.Fly(14);',
  26253. ' with TObject do Field.Fly(15);',
  26254. ' with TObject.Field do Fly(16);',
  26255. '']);
  26256. ConvertProgram;
  26257. CheckSource('TestTypeHelper_PassClassPropertyField',
  26258. LinesToStr([ // statements
  26259. 'rtl.createClass(this, "TObject", null, function () {',
  26260. ' this.FField = 0;',
  26261. ' this.$init = function () {',
  26262. ' };',
  26263. ' this.$final = function () {',
  26264. ' };',
  26265. ' this.SetField = function (Value) {',
  26266. ' $mod.THelper.Fly.call({',
  26267. ' p: this,',
  26268. ' get: function () {',
  26269. ' return this.p.FField;',
  26270. ' },',
  26271. ' set: function (v) {',
  26272. ' $mod.TObject.FField = v;',
  26273. ' }',
  26274. ' }, 1);',
  26275. ' $mod.THelper.Fly.call({',
  26276. ' p: this,',
  26277. ' get: function () {',
  26278. ' return this.p.FField;',
  26279. ' },',
  26280. ' set: function (v) {',
  26281. ' $mod.TObject.FField = v;',
  26282. ' }',
  26283. ' }, 2);',
  26284. ' $mod.THelper.Fly.call({',
  26285. ' p: this,',
  26286. ' get: function () {',
  26287. ' return this.p.FField;',
  26288. ' },',
  26289. ' set: function (v) {',
  26290. ' $mod.TObject.FField = v;',
  26291. ' }',
  26292. ' }, 3);',
  26293. ' var $with = this.FField;',
  26294. ' $mod.THelper.Fly.call({',
  26295. ' get: function () {',
  26296. ' return $with;',
  26297. ' },',
  26298. ' set: function (v) {',
  26299. ' $with = v;',
  26300. ' }',
  26301. ' }, 4);',
  26302. ' $mod.THelper.Fly.call({',
  26303. ' p: $mod.TObject,',
  26304. ' get: function () {',
  26305. ' return this.p.FField;',
  26306. ' },',
  26307. ' set: function (v) {',
  26308. ' $mod.TObject.FField = v;',
  26309. ' }',
  26310. ' }, 5);',
  26311. ' var $with1 = $mod.TObject;',
  26312. ' $mod.THelper.Fly.call({',
  26313. ' p: $with1,',
  26314. ' get: function () {',
  26315. ' return this.p.FField;',
  26316. ' },',
  26317. ' set: function (v) {',
  26318. ' $mod.TObject.FField = v;',
  26319. ' }',
  26320. ' }, 6);',
  26321. ' var $with2 = $mod.TObject.FField;',
  26322. ' $mod.THelper.Fly.call({',
  26323. ' get: function () {',
  26324. ' return $with2;',
  26325. ' },',
  26326. ' set: function (v) {',
  26327. ' $with2 = v;',
  26328. ' }',
  26329. ' }, 7);',
  26330. ' };',
  26331. '});',
  26332. 'rtl.createHelper(this, "THelper", null, function () {',
  26333. ' this.Fly = function (n) {',
  26334. ' };',
  26335. '});',
  26336. 'this.o = null;',
  26337. '']),
  26338. LinesToStr([ // $mod.$main
  26339. '$mod.THelper.Fly.call({',
  26340. ' p: $mod.o,',
  26341. ' get: function () {',
  26342. ' return this.p.FField;',
  26343. ' },',
  26344. ' set: function (v) {',
  26345. ' $mod.TObject.FField = v;',
  26346. ' }',
  26347. '}, 11);',
  26348. 'var $with = $mod.o;',
  26349. '$mod.THelper.Fly.call({',
  26350. ' p: $with,',
  26351. ' get: function () {',
  26352. ' return this.p.FField;',
  26353. ' },',
  26354. ' set: function (v) {',
  26355. ' $mod.TObject.FField = v;',
  26356. ' }',
  26357. '}, 12);',
  26358. 'var $with1 = $mod.o.FField;',
  26359. '$mod.THelper.Fly.call({',
  26360. ' get: function () {',
  26361. ' return $with1;',
  26362. ' },',
  26363. ' set: function (v) {',
  26364. ' $with1 = v;',
  26365. ' }',
  26366. '}, 13);',
  26367. '$mod.THelper.Fly.call({',
  26368. ' p: $mod.TObject,',
  26369. ' get: function () {',
  26370. ' return this.p.FField;',
  26371. ' },',
  26372. ' set: function (v) {',
  26373. ' $mod.TObject.FField = v;',
  26374. ' }',
  26375. '}, 14);',
  26376. 'var $with2 = $mod.TObject;',
  26377. '$mod.THelper.Fly.call({',
  26378. ' p: $with2,',
  26379. ' get: function () {',
  26380. ' return this.p.FField;',
  26381. ' },',
  26382. ' set: function (v) {',
  26383. ' $mod.TObject.FField = v;',
  26384. ' }',
  26385. '}, 15);',
  26386. 'var $with3 = $mod.TObject.FField;',
  26387. '$mod.THelper.Fly.call({',
  26388. ' get: function () {',
  26389. ' return $with3;',
  26390. ' },',
  26391. ' set: function (v) {',
  26392. ' $with3 = v;',
  26393. ' }',
  26394. '}, 16);',
  26395. '']));
  26396. end;
  26397. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  26398. begin
  26399. StartProgram(false);
  26400. Add([
  26401. '{$modeswitch typehelpers}',
  26402. 'type',
  26403. ' TObject = class',
  26404. ' class var FField: word;',
  26405. ' class function GetField: word; static;',
  26406. ' class property Field: word read GetField write FField;',
  26407. ' end;',
  26408. ' THelper = type helper for word',
  26409. ' procedure Fly(n: byte);',
  26410. ' end;',
  26411. 'class function TObject.GetField: word;',
  26412. 'begin',
  26413. ' Field.Fly(1);',
  26414. ' TObject.Field.Fly(5);',
  26415. ' with TObject do Field.Fly(6);',
  26416. ' with TObject.Field do Fly(7);',
  26417. 'end;',
  26418. 'procedure THelper.Fly(n: byte);',
  26419. 'begin',
  26420. 'end;',
  26421. 'var',
  26422. ' o: TObject;',
  26423. 'begin',
  26424. ' o.Field.Fly(11);',
  26425. ' with o do Field.Fly(12);',
  26426. ' with o.Field do Fly(13);',
  26427. '']);
  26428. ConvertProgram;
  26429. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  26430. LinesToStr([ // statements
  26431. 'rtl.createClass(this, "TObject", null, function () {',
  26432. ' this.FField = 0;',
  26433. ' this.$init = function () {',
  26434. ' };',
  26435. ' this.$final = function () {',
  26436. ' };',
  26437. ' this.GetField = function () {',
  26438. ' var Result = 0;',
  26439. ' $mod.THelper.Fly.call({',
  26440. ' p: $mod.TObject.GetField(),',
  26441. ' get: function () {',
  26442. ' return this.p;',
  26443. ' },',
  26444. ' set: function (v) {',
  26445. ' this.p = v;',
  26446. ' }',
  26447. ' }, 1);',
  26448. ' $mod.THelper.Fly.call({',
  26449. ' p: $mod.TObject.GetField(),',
  26450. ' get: function () {',
  26451. ' return this.p;',
  26452. ' },',
  26453. ' set: function (v) {',
  26454. ' this.p = v;',
  26455. ' }',
  26456. ' }, 5);',
  26457. ' var $with = $mod.TObject;',
  26458. ' $mod.THelper.Fly.call({',
  26459. ' p: $with.GetField(),',
  26460. ' get: function () {',
  26461. ' return this.p;',
  26462. ' },',
  26463. ' set: function (v) {',
  26464. ' this.p = v;',
  26465. ' }',
  26466. ' }, 6);',
  26467. ' var $with1 = $mod.TObject.GetField();',
  26468. ' $mod.THelper.Fly.call({',
  26469. ' get: function () {',
  26470. ' return $with1;',
  26471. ' },',
  26472. ' set: function (v) {',
  26473. ' $with1 = v;',
  26474. ' }',
  26475. ' }, 7);',
  26476. ' return Result;',
  26477. ' };',
  26478. '});',
  26479. 'rtl.createHelper(this, "THelper", null, function () {',
  26480. ' this.Fly = function (n) {',
  26481. ' };',
  26482. '});',
  26483. 'this.o = null;',
  26484. '']),
  26485. LinesToStr([ // $mod.$main
  26486. '$mod.THelper.Fly.call({',
  26487. ' p: $mod.TObject.GetField(),',
  26488. ' get: function () {',
  26489. ' return this.p;',
  26490. ' },',
  26491. ' set: function (v) {',
  26492. ' this.p = v;',
  26493. ' }',
  26494. '}, 11);',
  26495. 'var $with = $mod.o;',
  26496. '$mod.THelper.Fly.call({',
  26497. ' p: $with.GetField(),',
  26498. ' get: function () {',
  26499. ' return this.p;',
  26500. ' },',
  26501. ' set: function (v) {',
  26502. ' this.p = v;',
  26503. ' }',
  26504. '}, 12);',
  26505. 'var $with1 = $mod.TObject.GetField();',
  26506. '$mod.THelper.Fly.call({',
  26507. ' get: function () {',
  26508. ' return $with1;',
  26509. ' },',
  26510. ' set: function (v) {',
  26511. ' $with1 = v;',
  26512. ' }',
  26513. '}, 13);',
  26514. '']));
  26515. end;
  26516. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  26517. begin
  26518. StartProgram(false);
  26519. Add([
  26520. '{$modeswitch typehelpers}',
  26521. 'type',
  26522. ' TObject = class',
  26523. ' class var FField: word;',
  26524. ' class function GetField: word;',
  26525. ' class property Field: word read GetField write FField;',
  26526. ' end;',
  26527. ' TClass = class of TObject;',
  26528. ' THelper = type helper for word',
  26529. ' procedure Fly(n: byte);',
  26530. ' end;',
  26531. 'class function TObject.GetField: word;',
  26532. 'begin',
  26533. ' Field.Fly(1);',
  26534. ' Self.Field.Fly(5);',
  26535. ' with Self do Field.Fly(6);',
  26536. ' with Self.Field do Fly(7);',
  26537. 'end;',
  26538. 'procedure THelper.Fly(n: byte);',
  26539. 'begin',
  26540. 'end;',
  26541. 'var',
  26542. ' o: TObject;',
  26543. ' c: TClass;',
  26544. 'begin',
  26545. ' o.Field.Fly(11);',
  26546. ' with o do Field.Fly(12);',
  26547. ' with o.Field do Fly(13);',
  26548. ' c.Field.Fly(14);',
  26549. ' with c do Field.Fly(15);',
  26550. ' with c.Field do Fly(16);',
  26551. '']);
  26552. ConvertProgram;
  26553. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  26554. LinesToStr([ // statements
  26555. 'rtl.createClass(this, "TObject", null, function () {',
  26556. ' this.FField = 0;',
  26557. ' this.$init = function () {',
  26558. ' };',
  26559. ' this.$final = function () {',
  26560. ' };',
  26561. ' this.GetField = function () {',
  26562. ' var Result = 0;',
  26563. ' $mod.THelper.Fly.call({',
  26564. ' p: this.GetField(),',
  26565. ' get: function () {',
  26566. ' return this.p;',
  26567. ' },',
  26568. ' set: function (v) {',
  26569. ' this.p = v;',
  26570. ' }',
  26571. ' }, 1);',
  26572. ' $mod.THelper.Fly.call({',
  26573. ' p: this.GetField(),',
  26574. ' get: function () {',
  26575. ' return this.p;',
  26576. ' },',
  26577. ' set: function (v) {',
  26578. ' this.p = v;',
  26579. ' }',
  26580. ' }, 5);',
  26581. ' $mod.THelper.Fly.call({',
  26582. ' p: this.GetField(),',
  26583. ' get: function () {',
  26584. ' return this.p;',
  26585. ' },',
  26586. ' set: function (v) {',
  26587. ' this.p = v;',
  26588. ' }',
  26589. ' }, 6);',
  26590. ' var $with = this.GetField();',
  26591. ' $mod.THelper.Fly.call({',
  26592. ' get: function () {',
  26593. ' return $with;',
  26594. ' },',
  26595. ' set: function (v) {',
  26596. ' $with = v;',
  26597. ' }',
  26598. ' }, 7);',
  26599. ' return Result;',
  26600. ' };',
  26601. '});',
  26602. 'rtl.createHelper(this, "THelper", null, function () {',
  26603. ' this.Fly = function (n) {',
  26604. ' };',
  26605. '});',
  26606. 'this.o = null;',
  26607. 'this.c = null;',
  26608. '']),
  26609. LinesToStr([ // $mod.$main
  26610. '$mod.THelper.Fly.call({',
  26611. ' p: $mod.o.$class.GetField(),',
  26612. ' get: function () {',
  26613. ' return this.p;',
  26614. ' },',
  26615. ' set: function (v) {',
  26616. ' this.p = v;',
  26617. ' }',
  26618. '}, 11);',
  26619. 'var $with = $mod.o;',
  26620. '$mod.THelper.Fly.call({',
  26621. ' p: $with.$class.GetField(),',
  26622. ' get: function () {',
  26623. ' return this.p;',
  26624. ' },',
  26625. ' set: function (v) {',
  26626. ' this.p = v;',
  26627. ' }',
  26628. '}, 12);',
  26629. 'var $with1 = $mod.o.$class.GetField();',
  26630. '$mod.THelper.Fly.call({',
  26631. ' get: function () {',
  26632. ' return $with1;',
  26633. ' },',
  26634. ' set: function (v) {',
  26635. ' $with1 = v;',
  26636. ' }',
  26637. '}, 13);',
  26638. '$mod.THelper.Fly.call({',
  26639. ' p: $mod.c.GetField(),',
  26640. ' get: function () {',
  26641. ' return this.p;',
  26642. ' },',
  26643. ' set: function (v) {',
  26644. ' this.p = v;',
  26645. ' }',
  26646. '}, 14);',
  26647. 'var $with2 = $mod.c;',
  26648. '$mod.THelper.Fly.call({',
  26649. ' p: $with2.GetField(),',
  26650. ' get: function () {',
  26651. ' return this.p;',
  26652. ' },',
  26653. ' set: function (v) {',
  26654. ' this.p = v;',
  26655. ' }',
  26656. '}, 15);',
  26657. 'var $with3 = $mod.c.GetField();',
  26658. '$mod.THelper.Fly.call({',
  26659. ' get: function () {',
  26660. ' return $with3;',
  26661. ' },',
  26662. ' set: function (v) {',
  26663. ' $with3 = v;',
  26664. ' }',
  26665. '}, 16);',
  26666. '']));
  26667. end;
  26668. procedure TTestModule.TestTypeHelper_Property;
  26669. begin
  26670. StartProgram(false);
  26671. Add([
  26672. '{$modeswitch typehelpers}',
  26673. 'type',
  26674. ' THelper = type helper for word',
  26675. ' function GetSize: longint;',
  26676. ' procedure SetSize(Value: longint);',
  26677. ' property Size: longint read GetSize write SetSize;',
  26678. ' end;',
  26679. 'function THelper.GetSize: longint;',
  26680. 'begin',
  26681. ' Result:=Size+1;',
  26682. ' Size:=2;',
  26683. ' Result:=Self.Size+3;',
  26684. ' Self.Size:=4;',
  26685. ' with Self do begin',
  26686. ' Result:=Size+5;',
  26687. ' Size:=6;',
  26688. ' end;',
  26689. 'end;',
  26690. 'procedure THelper.SetSize(Value: longint);',
  26691. 'begin',
  26692. 'end;',
  26693. 'var w: word;',
  26694. 'begin',
  26695. ' w:=w.Size+7;',
  26696. ' w.Size:=w+8;',
  26697. ' with w do begin',
  26698. ' w:=Size+9;',
  26699. ' Size:=w+10;',
  26700. ' end;',
  26701. '']);
  26702. ConvertProgram;
  26703. CheckSource('TestTypeHelper_Property',
  26704. LinesToStr([ // statements
  26705. 'rtl.createHelper(this, "THelper", null, function () {',
  26706. ' this.GetSize = function () {',
  26707. ' var Result = 0;',
  26708. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  26709. ' $mod.THelper.SetSize.call(this, 2);',
  26710. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  26711. ' $mod.THelper.SetSize.call(this, 4);',
  26712. ' var $with = this.get();',
  26713. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  26714. ' $mod.THelper.SetSize.call(this, 6);',
  26715. ' return Result;',
  26716. ' };',
  26717. ' this.SetSize = function (Value) {',
  26718. ' };',
  26719. '});',
  26720. 'this.w = 0;',
  26721. '']),
  26722. LinesToStr([ // $mod.$main
  26723. '$mod.w = $mod.THelper.GetSize.call({',
  26724. ' p: $mod,',
  26725. ' get: function () {',
  26726. ' return this.p.w;',
  26727. ' },',
  26728. ' set: function (v) {',
  26729. ' this.p.w = v;',
  26730. ' }',
  26731. '}) + 7;',
  26732. '$mod.THelper.SetSize.call({',
  26733. ' p: $mod,',
  26734. ' get: function () {',
  26735. ' return this.p.w;',
  26736. ' },',
  26737. ' set: function (v) {',
  26738. ' this.p.w = v;',
  26739. ' }',
  26740. '}, $mod.w + 8);',
  26741. 'var $with = $mod.w;',
  26742. '$mod.w = $mod.THelper.GetSize.call({',
  26743. ' get: function () {',
  26744. ' return $with;',
  26745. ' },',
  26746. ' set: function (v) {',
  26747. ' $with = v;',
  26748. ' }',
  26749. '}) + 9;',
  26750. '$mod.THelper.SetSize.call({',
  26751. ' get: function () {',
  26752. ' return $with;',
  26753. ' },',
  26754. ' set: function (v) {',
  26755. ' $with = v;',
  26756. ' }',
  26757. '}, $mod.w + 10);',
  26758. '']));
  26759. end;
  26760. procedure TTestModule.TestTypeHelper_Property_Array;
  26761. begin
  26762. StartProgram(false);
  26763. Add([
  26764. '{$modeswitch typehelpers}',
  26765. 'type',
  26766. ' THelper = type helper for word',
  26767. ' function GetItems(Index: byte): boolean;',
  26768. ' procedure SetItems(Index: byte; Value: boolean);',
  26769. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  26770. ' end;',
  26771. 'function THelper.GetItems(Index: byte): boolean;',
  26772. 'begin',
  26773. ' Result:=Items[1];',
  26774. ' Items[2]:=false;',
  26775. ' Result:=Self.Items[3];',
  26776. ' Self.Items[4]:=true;',
  26777. ' with Self do begin',
  26778. ' Result:=Items[5];',
  26779. ' Items[6]:=false;',
  26780. ' end;',
  26781. 'end;',
  26782. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  26783. 'begin',
  26784. 'end;',
  26785. 'var',
  26786. ' w: word;',
  26787. ' b: boolean;',
  26788. 'begin',
  26789. ' b:=w.Items[1];',
  26790. ' w.Items[2]:=b;',
  26791. ' with w do begin',
  26792. ' b:=Items[3];',
  26793. ' Items[4]:=b;',
  26794. ' end;',
  26795. '']);
  26796. ConvertProgram;
  26797. CheckSource('TestTypeHelper_Property_Array',
  26798. LinesToStr([ // statements
  26799. 'rtl.createHelper(this, "THelper", null, function () {',
  26800. ' this.GetItems = function (Index) {',
  26801. ' var Result = false;',
  26802. ' Result = $mod.THelper.GetItems.call(this, 1);',
  26803. ' $mod.THelper.SetItems.call(this, 2, false);',
  26804. ' Result = $mod.THelper.GetItems.call(this, 3);',
  26805. ' $mod.THelper.SetItems.call(this, 4, true);',
  26806. ' var $with = this.get();',
  26807. ' Result = $mod.THelper.GetItems.call(this, 5);',
  26808. ' $mod.THelper.SetItems.call(this, 6, false);',
  26809. ' return Result;',
  26810. ' };',
  26811. ' this.SetItems = function (Index, Value) {',
  26812. ' };',
  26813. '});',
  26814. 'this.w = 0;',
  26815. 'this.b = false;',
  26816. '']),
  26817. LinesToStr([ // $mod.$main
  26818. '$mod.b = $mod.THelper.GetItems.call({',
  26819. ' p: $mod,',
  26820. ' get: function () {',
  26821. ' return this.p.w;',
  26822. ' },',
  26823. ' set: function (v) {',
  26824. ' this.p.w = v;',
  26825. ' }',
  26826. '}, 1);',
  26827. '$mod.THelper.SetItems.call({',
  26828. ' p: $mod,',
  26829. ' get: function () {',
  26830. ' return this.p.w;',
  26831. ' },',
  26832. ' set: function (v) {',
  26833. ' this.p.w = v;',
  26834. ' }',
  26835. '}, 2, $mod.b);',
  26836. 'var $with = $mod.w;',
  26837. '$mod.b = $mod.THelper.GetItems.call({',
  26838. ' get: function () {',
  26839. ' return $with;',
  26840. ' },',
  26841. ' set: function (v) {',
  26842. ' $with = v;',
  26843. ' }',
  26844. '}, 3);',
  26845. '$mod.THelper.SetItems.call({',
  26846. ' get: function () {',
  26847. ' return $with;',
  26848. ' },',
  26849. ' set: function (v) {',
  26850. ' $with = v;',
  26851. ' }',
  26852. '}, 4, $mod.b);',
  26853. '']));
  26854. end;
  26855. procedure TTestModule.TestTypeHelper_ClassProperty;
  26856. begin
  26857. StartProgram(false);
  26858. Add([
  26859. '{$modeswitch typehelpers}',
  26860. 'type',
  26861. ' THelper = type helper for word',
  26862. ' class function GetSize: longint; static;',
  26863. ' class procedure SetSize(Value: longint); static;',
  26864. ' class property Size: longint read GetSize write SetSize;',
  26865. ' end;',
  26866. 'class function THelper.GetSize: longint;',
  26867. 'begin',
  26868. ' Result:=Size+1;',
  26869. ' Size:=2;',
  26870. 'end;',
  26871. 'class procedure THelper.SetSize(Value: longint);',
  26872. 'begin',
  26873. 'end;',
  26874. 'begin',
  26875. '']);
  26876. ConvertProgram;
  26877. CheckSource('TestTypeHelper_ClassProperty',
  26878. LinesToStr([ // statements
  26879. 'rtl.createHelper(this, "THelper", null, function () {',
  26880. ' this.GetSize = function () {',
  26881. ' var Result = 0;',
  26882. ' Result = $mod.THelper.GetSize() + 1;',
  26883. ' $mod.THelper.SetSize(2);',
  26884. ' return Result;',
  26885. ' };',
  26886. ' this.SetSize = function (Value) {',
  26887. ' };',
  26888. '});',
  26889. '']),
  26890. LinesToStr([ // $mod.$main
  26891. '']));
  26892. end;
  26893. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  26894. begin
  26895. StartProgram(false);
  26896. Add([
  26897. '{$modeswitch typehelpers}',
  26898. 'type',
  26899. ' THelper = type helper for word',
  26900. ' class function GetItems(Index: byte): boolean; static;',
  26901. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  26902. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  26903. ' end;',
  26904. 'class function THelper.GetItems(Index: byte): boolean;',
  26905. 'begin',
  26906. ' Result:=Items[1];',
  26907. ' Items[2]:=false;',
  26908. 'end;',
  26909. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  26910. 'begin',
  26911. 'end;',
  26912. 'var',
  26913. ' w: word;',
  26914. ' b: boolean;',
  26915. 'begin',
  26916. ' b:=w.Items[1];',
  26917. ' w.Items[2]:=b;',
  26918. ' with w do begin',
  26919. ' b:=Items[3];',
  26920. ' Items[4]:=b;',
  26921. ' end;',
  26922. '']);
  26923. ConvertProgram;
  26924. CheckSource('TestTypeHelper_ClassProperty_Array',
  26925. LinesToStr([ // statements
  26926. 'rtl.createHelper(this, "THelper", null, function () {',
  26927. ' this.GetItems = function (Index) {',
  26928. ' var Result = false;',
  26929. ' Result = $mod.THelper.GetItems(1);',
  26930. ' $mod.THelper.SetItems(2, false);',
  26931. ' return Result;',
  26932. ' };',
  26933. ' this.SetItems = function (Index, Value) {',
  26934. ' };',
  26935. '});',
  26936. 'this.w = 0;',
  26937. 'this.b = false;',
  26938. '']),
  26939. LinesToStr([ // $mod.$main
  26940. '$mod.b = $mod.THelper.GetItems(1);',
  26941. '$mod.THelper.SetItems(2, $mod.b);',
  26942. 'var $with = $mod.w;',
  26943. '$mod.b = $mod.THelper.GetItems(3);',
  26944. '$mod.THelper.SetItems(4, $mod.b);',
  26945. '']));
  26946. end;
  26947. procedure TTestModule.TestTypeHelper_ClassMethod;
  26948. begin
  26949. StartProgram(false);
  26950. Add([
  26951. '{$modeswitch typehelpers}',
  26952. 'type',
  26953. ' THelper = type helper for word',
  26954. ' class procedure DoStatic; static;',
  26955. ' end;',
  26956. 'class procedure THelper.DoStatic;',
  26957. 'begin',
  26958. ' DoStatic;',
  26959. ' DoStatic();',
  26960. 'end;',
  26961. 'var w: word;',
  26962. 'begin',
  26963. ' w.DoStatic;',
  26964. ' w.DoStatic();',
  26965. '']);
  26966. ConvertProgram;
  26967. CheckSource('TestTypeHelper_ClassMethod',
  26968. LinesToStr([ // statements
  26969. 'rtl.createHelper(this, "THelper", null, function () {',
  26970. ' this.DoStatic = function () {',
  26971. ' $mod.THelper.DoStatic();',
  26972. ' $mod.THelper.DoStatic();',
  26973. ' };',
  26974. '});',
  26975. 'this.w = 0;',
  26976. '']),
  26977. LinesToStr([ // $mod.$main
  26978. '$mod.THelper.DoStatic();',
  26979. '$mod.THelper.DoStatic();',
  26980. '']));
  26981. end;
  26982. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  26983. begin
  26984. StartProgram(false);
  26985. Add([
  26986. '{$modeswitch typehelpers}',
  26987. 'type',
  26988. ' THelper = type helper for word',
  26989. ' procedure Run; external name ''Run'';',
  26990. ' end;',
  26991. 'var w: word;',
  26992. 'begin',
  26993. ' w.Run;',
  26994. '']);
  26995. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  26996. ConvertProgram;
  26997. end;
  26998. procedure TTestModule.TestTypeHelper_Constructor;
  26999. begin
  27000. StartProgram(false);
  27001. Add([
  27002. '{$modeswitch typehelpers}',
  27003. 'type',
  27004. ' THelper = type helper for word',
  27005. ' constructor Init(e: longint);',
  27006. ' end;',
  27007. 'constructor THelper.Init(e: longint);',
  27008. 'begin',
  27009. ' Self:=e;',
  27010. ' Init(e+1);',
  27011. 'end;',
  27012. 'var w: word;',
  27013. 'begin',
  27014. ' w:=word.Init(2);',
  27015. ' w:=w.Init(3);',
  27016. ' with word do w:=Init(4);',
  27017. ' with w do w:=Init(5);',
  27018. '']);
  27019. ConvertProgram;
  27020. CheckSource('TestTypeHelper_Constructor',
  27021. LinesToStr([ // statements
  27022. 'rtl.createHelper(this, "THelper", null, function () {',
  27023. ' this.Init = function (e) {',
  27024. ' this.set(e);',
  27025. ' $mod.THelper.Init.call(this, e + 1);',
  27026. ' return this.get();',
  27027. ' };',
  27028. ' this.$new = function (fn, args) {',
  27029. ' return this[fn].apply({',
  27030. ' p: 0,',
  27031. ' get: function () {',
  27032. ' return this.p;',
  27033. ' },',
  27034. ' set: function (v) {',
  27035. ' this.p = v;',
  27036. ' }',
  27037. ' }, args);',
  27038. ' };',
  27039. '});',
  27040. 'this.w = 0;',
  27041. '']),
  27042. LinesToStr([ // $mod.$main
  27043. '$mod.w = $mod.THelper.$new("Init", [2]);',
  27044. '$mod.w = $mod.THelper.Init.call({',
  27045. ' p: $mod,',
  27046. ' get: function () {',
  27047. ' return this.p.w;',
  27048. ' },',
  27049. ' set: function (v) {',
  27050. ' this.p.w = v;',
  27051. ' }',
  27052. '}, 3);',
  27053. '$mod.w = $mod.THelper.$new("Init", [4]);',
  27054. 'var $with = $mod.w;',
  27055. '$mod.w = $mod.THelper.Init.call({',
  27056. ' get: function () {',
  27057. ' return $with;',
  27058. ' },',
  27059. ' set: function (v) {',
  27060. ' $with = v;',
  27061. ' }',
  27062. '}, 5);',
  27063. '']));
  27064. end;
  27065. procedure TTestModule.TestTypeHelper_Word;
  27066. begin
  27067. StartProgram(false);
  27068. Add([
  27069. '{$modeswitch typehelpers}',
  27070. 'type',
  27071. ' THelper = type helper for word',
  27072. ' procedure DoIt(e: byte = 123);',
  27073. ' end;',
  27074. 'procedure THelper.DoIt(e: byte);',
  27075. 'begin',
  27076. ' Self:=e;',
  27077. ' Self:=Self+1;',
  27078. ' with Self do Doit;',
  27079. 'end;',
  27080. 'begin',
  27081. ' word(3).DoIt;',
  27082. '']);
  27083. ConvertProgram;
  27084. CheckSource('TestTypeHelper_Word',
  27085. LinesToStr([ // statements
  27086. 'rtl.createHelper(this, "THelper", null, function () {',
  27087. ' this.DoIt = function (e) {',
  27088. ' this.set(e);',
  27089. ' this.set(this.get() + 1);',
  27090. ' var $with = this.get();',
  27091. ' $mod.THelper.DoIt.call(this, 123);',
  27092. ' };',
  27093. '});',
  27094. '']),
  27095. LinesToStr([ // $mod.$main
  27096. '$mod.THelper.DoIt.call({',
  27097. ' get: function () {',
  27098. ' return 3;',
  27099. ' },',
  27100. ' set: function (v) {',
  27101. ' rtl.raiseE("EPropReadOnly");',
  27102. ' }',
  27103. '}, 123);',
  27104. '']));
  27105. end;
  27106. procedure TTestModule.TestTypeHelper_Boolean;
  27107. begin
  27108. StartProgram(false);
  27109. Add([
  27110. '{$modeswitch typehelpers}',
  27111. 'type',
  27112. ' Integer = longint;',
  27113. ' THelper = type helper for boolean',
  27114. ' procedure Run(e: wordbool = true);',
  27115. ' end;',
  27116. 'procedure THelper.Run(e: wordbool);',
  27117. 'begin',
  27118. ' Self:=e;',
  27119. ' Self:=not Self;',
  27120. ' with Self do Run;',
  27121. ' if Integer(Self)=0 then ;',
  27122. 'end;',
  27123. 'begin',
  27124. ' boolean(3).Run;',
  27125. '']);
  27126. ConvertProgram;
  27127. CheckSource('TestTypeHelper_Boolean',
  27128. LinesToStr([ // statements
  27129. 'rtl.createHelper(this, "THelper", null, function () {',
  27130. ' this.Run = function (e) {',
  27131. ' this.set(e);',
  27132. ' this.set(!this.get());',
  27133. ' var $with = this.get();',
  27134. ' $mod.THelper.Run.call(this, true);',
  27135. ' if ((this.get() ? 1 : 0) === 0) ;',
  27136. ' };',
  27137. '});',
  27138. '']),
  27139. LinesToStr([ // $mod.$main
  27140. '$mod.THelper.Run.call({',
  27141. ' a: 3 != 0,',
  27142. ' get: function () {',
  27143. ' return this.a;',
  27144. ' },',
  27145. ' set: function (v) {',
  27146. ' rtl.raiseE("EPropReadOnly");',
  27147. ' }',
  27148. '}, true);',
  27149. '']));
  27150. end;
  27151. procedure TTestModule.TestTypeHelper_WordBool;
  27152. begin
  27153. StartProgram(false);
  27154. Add([
  27155. '{$modeswitch typehelpers}',
  27156. 'type',
  27157. ' Integer = longint;',
  27158. ' THelper = type helper for WordBool',
  27159. ' procedure Run(e: wordbool = true);',
  27160. ' end;',
  27161. 'procedure THelper.Run(e: wordbool);',
  27162. 'var i: integer;',
  27163. 'begin',
  27164. ' i:=Integer(Self);',
  27165. 'end;',
  27166. 'var w: wordbool;',
  27167. 'begin',
  27168. ' w.Run;',
  27169. ' wordbool(3).Run;',
  27170. '']);
  27171. ConvertProgram;
  27172. CheckSource('TestTypeHelper_WordBool',
  27173. LinesToStr([ // statements
  27174. 'rtl.createHelper(this, "THelper", null, function () {',
  27175. ' this.Run = function (e) {',
  27176. ' var i = 0;',
  27177. ' i = (this.get() ? 1 : 0);',
  27178. ' };',
  27179. '});',
  27180. 'this.w = false;',
  27181. '']),
  27182. LinesToStr([ // $mod.$main
  27183. '$mod.THelper.Run.call({',
  27184. ' p: $mod,',
  27185. ' get: function () {',
  27186. ' return this.p.w;',
  27187. ' },',
  27188. ' set: function (v) {',
  27189. ' this.p.w = v;',
  27190. ' }',
  27191. '}, true);',
  27192. '$mod.THelper.Run.call({',
  27193. ' a: 3 != 0,',
  27194. ' get: function () {',
  27195. ' return this.a;',
  27196. ' },',
  27197. ' set: function (v) {',
  27198. ' rtl.raiseE("EPropReadOnly");',
  27199. ' }',
  27200. '}, true);',
  27201. '']));
  27202. end;
  27203. procedure TTestModule.TestTypeHelper_Double;
  27204. begin
  27205. StartProgram(false);
  27206. Add([
  27207. '{$modeswitch typehelpers}',
  27208. 'type',
  27209. ' Float = type double;',
  27210. ' THelper = type helper for Float',
  27211. ' const NPI = 3.141592;',
  27212. ' function ToStr: String;',
  27213. ' end;',
  27214. 'function THelper.ToStr: String;',
  27215. 'begin',
  27216. 'end;',
  27217. 'procedure DoIt(s: string);',
  27218. 'begin',
  27219. 'end;',
  27220. 'var f: Float;',
  27221. 'begin',
  27222. ' DoIt(f.toStr);',
  27223. ' DoIt(f.toStr());',
  27224. ' (f*f).toStr;',
  27225. ' DoIt((f*f).toStr);',
  27226. '']);
  27227. ConvertProgram;
  27228. CheckSource('TestTypeHelper_Double',
  27229. LinesToStr([ // statements
  27230. 'rtl.createHelper(this, "THelper", null, function () {',
  27231. ' this.NPI = 3.141592;',
  27232. ' this.ToStr = function () {',
  27233. ' var Result = "";',
  27234. ' return Result;',
  27235. ' };',
  27236. '});',
  27237. 'this.DoIt = function (s) {',
  27238. '};',
  27239. 'this.f = 0.0;',
  27240. '']),
  27241. LinesToStr([ // $mod.$main
  27242. '$mod.DoIt($mod.THelper.ToStr.call({',
  27243. ' p: $mod,',
  27244. ' get: function () {',
  27245. ' return this.p.f;',
  27246. ' },',
  27247. ' set: function (v) {',
  27248. ' this.p.f = v;',
  27249. ' }',
  27250. '}));',
  27251. '$mod.DoIt($mod.THelper.ToStr.call({',
  27252. ' p: $mod,',
  27253. ' get: function () {',
  27254. ' return this.p.f;',
  27255. ' },',
  27256. ' set: function (v) {',
  27257. ' this.p.f = v;',
  27258. ' }',
  27259. '}));',
  27260. '$mod.THelper.ToStr.call({',
  27261. ' a: $mod.f * $mod.f,',
  27262. ' get: function () {',
  27263. ' return this.a;',
  27264. ' },',
  27265. ' set: function (v) {',
  27266. ' rtl.raiseE("EPropReadOnly");',
  27267. ' }',
  27268. '});',
  27269. '$mod.DoIt($mod.THelper.ToStr.call({',
  27270. ' a: $mod.f * $mod.f,',
  27271. ' get: function () {',
  27272. ' return this.a;',
  27273. ' },',
  27274. ' set: function (v) {',
  27275. ' rtl.raiseE("EPropReadOnly");',
  27276. ' }',
  27277. '}));',
  27278. '']));
  27279. end;
  27280. procedure TTestModule.TestTypeHelper_NativeInt;
  27281. begin
  27282. StartProgram(false);
  27283. Add([
  27284. '{$modeswitch typehelpers}',
  27285. 'type',
  27286. ' MaxInt = type nativeint;',
  27287. ' THelperI = type helper for MaxInt',
  27288. ' function ToStr: String;',
  27289. ' end;',
  27290. ' MaxUInt = type nativeuint;',
  27291. ' THelperU = type helper for MaxUInt',
  27292. ' function ToStr: String;',
  27293. ' end;',
  27294. 'function THelperI.ToStr: String;',
  27295. 'begin',
  27296. ' Result:=str(Self);',
  27297. 'end;',
  27298. 'function THelperU.ToStr: String;',
  27299. 'begin',
  27300. ' Result:=str(Self);',
  27301. 'end;',
  27302. 'procedure DoIt(s: string);',
  27303. 'begin',
  27304. 'end;',
  27305. 'var i: MaxInt;',
  27306. 'begin',
  27307. ' DoIt(i.toStr);',
  27308. ' DoIt(i.toStr());',
  27309. ' (i*i).toStr;',
  27310. ' DoIt((i*i).toStr);',
  27311. '']);
  27312. ConvertProgram;
  27313. CheckSource('TestTypeHelper_NativeInt',
  27314. LinesToStr([ // statements
  27315. 'rtl.createHelper(this, "THelperI", null, function () {',
  27316. ' this.ToStr = function () {',
  27317. ' var Result = "";',
  27318. ' Result = "" + this.get();',
  27319. ' return Result;',
  27320. ' };',
  27321. '});',
  27322. 'rtl.createHelper(this, "THelperU", null, function () {',
  27323. ' this.ToStr = function () {',
  27324. ' var Result = "";',
  27325. ' Result = "" + this.get();',
  27326. ' return Result;',
  27327. ' };',
  27328. '});',
  27329. 'this.DoIt = function (s) {',
  27330. '};',
  27331. 'this.i = 0;',
  27332. '']),
  27333. LinesToStr([ // $mod.$main
  27334. '$mod.DoIt($mod.THelperI.ToStr.call({',
  27335. ' p: $mod,',
  27336. ' get: function () {',
  27337. ' return this.p.i;',
  27338. ' },',
  27339. ' set: function (v) {',
  27340. ' this.p.i = v;',
  27341. ' }',
  27342. '}));',
  27343. '$mod.DoIt($mod.THelperI.ToStr.call({',
  27344. ' p: $mod,',
  27345. ' get: function () {',
  27346. ' return this.p.i;',
  27347. ' },',
  27348. ' set: function (v) {',
  27349. ' this.p.i = v;',
  27350. ' }',
  27351. '}));',
  27352. '$mod.THelperI.ToStr.call({',
  27353. ' a: $mod.i * $mod.i,',
  27354. ' get: function () {',
  27355. ' return this.a;',
  27356. ' },',
  27357. ' set: function (v) {',
  27358. ' rtl.raiseE("EPropReadOnly");',
  27359. ' }',
  27360. '});',
  27361. '$mod.DoIt($mod.THelperI.ToStr.call({',
  27362. ' a: $mod.i * $mod.i,',
  27363. ' get: function () {',
  27364. ' return this.a;',
  27365. ' },',
  27366. ' set: function (v) {',
  27367. ' rtl.raiseE("EPropReadOnly");',
  27368. ' }',
  27369. '}));',
  27370. '']));
  27371. end;
  27372. procedure TTestModule.TestTypeHelper_StringChar;
  27373. begin
  27374. StartProgram(false);
  27375. Add([
  27376. '{$modeswitch typehelpers}',
  27377. 'type',
  27378. ' TStringHelper = type helper for string',
  27379. ' procedure DoIt(e: byte = 123);',
  27380. ' end;',
  27381. ' TCharHelper = type helper for char',
  27382. ' procedure Fly;',
  27383. ' end;',
  27384. 'procedure TStringHelper.DoIt(e: byte);',
  27385. 'begin',
  27386. ' Self[1]:=''c'';',
  27387. ' Self[2]:=Self[3];',
  27388. 'end;',
  27389. 'procedure TCharHelper.Fly;',
  27390. 'begin',
  27391. ' Self:=''c'';',
  27392. 'end;',
  27393. 'begin',
  27394. ' ''abc''.DoIt;',
  27395. ' ''xyz''.DoIt();',
  27396. ' ''c''.Fly();',
  27397. '']);
  27398. ConvertProgram;
  27399. CheckSource('TestTypeHelper_StringChar',
  27400. LinesToStr([ // statements
  27401. 'rtl.createHelper(this, "TStringHelper", null, function () {',
  27402. ' this.DoIt = function (e) {',
  27403. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  27404. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  27405. ' };',
  27406. '});',
  27407. 'rtl.createHelper(this, "TCharHelper", null, function () {',
  27408. ' this.Fly = function () {',
  27409. ' this.set("c");',
  27410. ' };',
  27411. '});',
  27412. '']),
  27413. LinesToStr([ // $mod.$main
  27414. '$mod.TStringHelper.DoIt.call({',
  27415. ' get: function () {',
  27416. ' return "abc";',
  27417. ' },',
  27418. ' set: function (v) {',
  27419. ' rtl.raiseE("EPropReadOnly");',
  27420. ' }',
  27421. '}, 123);',
  27422. '$mod.TStringHelper.DoIt.call({',
  27423. ' get: function () {',
  27424. ' return "xyz";',
  27425. ' },',
  27426. ' set: function (v) {',
  27427. ' rtl.raiseE("EPropReadOnly");',
  27428. ' }',
  27429. '}, 123);',
  27430. '$mod.TCharHelper.Fly.call({',
  27431. ' get: function () {',
  27432. ' return "c";',
  27433. ' },',
  27434. ' set: function (v) {',
  27435. ' rtl.raiseE("EPropReadOnly");',
  27436. ' }',
  27437. '});',
  27438. '']));
  27439. end;
  27440. procedure TTestModule.TestTypeHelper_JSValue;
  27441. begin
  27442. StartProgram(false);
  27443. Add([
  27444. '{$modeswitch typehelpers}',
  27445. 'type',
  27446. ' TExtValue = type jsvalue;',
  27447. ' THelper = type helper for TExtValue',
  27448. ' function ToStr: String;',
  27449. ' end;',
  27450. 'function THelper.ToStr: String;',
  27451. 'begin',
  27452. 'end;',
  27453. 'var',
  27454. ' s: string;',
  27455. ' v: TExtValue;',
  27456. 'begin',
  27457. ' s:=v.toStr;',
  27458. ' s:=v.toStr();',
  27459. ' TExtValue(s).toStr;',
  27460. '']);
  27461. ConvertProgram;
  27462. CheckSource('TestTypeHelper_JSValue',
  27463. LinesToStr([ // statements
  27464. 'rtl.createHelper(this, "THelper", null, function () {',
  27465. ' this.ToStr = function () {',
  27466. ' var Result = "";',
  27467. ' return Result;',
  27468. ' };',
  27469. '});',
  27470. 'this.s = "";',
  27471. 'this.v = undefined;',
  27472. '']),
  27473. LinesToStr([ // $mod.$main
  27474. '$mod.s = $mod.THelper.ToStr.call({',
  27475. ' p: $mod,',
  27476. ' get: function () {',
  27477. ' return this.p.v;',
  27478. ' },',
  27479. ' set: function (v) {',
  27480. ' this.p.v = v;',
  27481. ' }',
  27482. '});',
  27483. '$mod.s = $mod.THelper.ToStr.call({',
  27484. ' p: $mod,',
  27485. ' get: function () {',
  27486. ' return this.p.v;',
  27487. ' },',
  27488. ' set: function (v) {',
  27489. ' this.p.v = v;',
  27490. ' }',
  27491. '});',
  27492. '$mod.THelper.ToStr.call({',
  27493. ' p: $mod,',
  27494. ' get: function () {',
  27495. ' return this.p.s;',
  27496. ' },',
  27497. ' set: function (v) {',
  27498. ' rtl.raiseE("EPropReadOnly");',
  27499. ' }',
  27500. '});',
  27501. '']));
  27502. end;
  27503. procedure TTestModule.TestTypeHelper_Array;
  27504. begin
  27505. StartProgram(false);
  27506. Add([
  27507. '{$modeswitch typehelpers}',
  27508. 'type',
  27509. ' TArrOfBool = array of boolean;',
  27510. ' TArrOfJS = array of jsvalue;',
  27511. ' THelper = type helper for TArrOfBool',
  27512. ' procedure DoIt(e: byte = 123);',
  27513. ' end;',
  27514. 'procedure THelper.DoIt(e: byte);',
  27515. 'begin',
  27516. ' Self[1]:=true;',
  27517. ' Self[2]:=not Self[3];',
  27518. ' SetLength(Self,4);',
  27519. ' Self:=Concat(Self,[true]);',
  27520. 'end;',
  27521. 'var',
  27522. ' b: TArrOfBool;',
  27523. ' j: TArrOfJS;',
  27524. 'begin',
  27525. ' b.DoIt;',
  27526. ' TArrOfBool(j).DoIt();',
  27527. '']);
  27528. ConvertProgram;
  27529. CheckSource('TestTypeHelper_Array',
  27530. LinesToStr([ // statements
  27531. 'rtl.createHelper(this, "THelper", null, function () {',
  27532. ' this.DoIt = function (e) {',
  27533. ' this.get()[1] = true;',
  27534. ' this.get()[2] = !this.get()[3];',
  27535. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  27536. ' this.set(rtl.arrayPushN(this.get(), true));',
  27537. ' };',
  27538. '});',
  27539. 'this.b = [];',
  27540. 'this.j = [];',
  27541. '']),
  27542. LinesToStr([ // $mod.$main
  27543. '$mod.THelper.DoIt.call({',
  27544. ' p: $mod,',
  27545. ' get: function () {',
  27546. ' return this.p.b;',
  27547. ' },',
  27548. ' set: function (v) {',
  27549. ' this.p.b = v;',
  27550. ' }',
  27551. '}, 123);',
  27552. '$mod.THelper.DoIt.call({',
  27553. ' p: $mod,',
  27554. ' get: function () {',
  27555. ' return this.p.j;',
  27556. ' },',
  27557. ' set: function (v) {',
  27558. ' this.p.j = v;',
  27559. ' }',
  27560. '}, 123);',
  27561. '']));
  27562. end;
  27563. procedure TTestModule.TestTypeHelper_EnumType;
  27564. begin
  27565. StartProgram(false);
  27566. Add([
  27567. '{$modeswitch typehelpers}',
  27568. 'type',
  27569. ' TEnum = (red,blue);',
  27570. ' THelper = type helper for TEnum',
  27571. ' procedure DoIt(e: byte = 123);',
  27572. ' class procedure Swing(w: word); static;',
  27573. ' end;',
  27574. 'procedure THelper.DoIt(e: byte);',
  27575. 'begin',
  27576. ' Self:=red;',
  27577. ' Self:=succ(Self);',
  27578. ' with Self do Doit;',
  27579. 'end;',
  27580. 'class procedure THelper.Swing(w: word);',
  27581. 'begin',
  27582. 'end;',
  27583. 'var e: TEnum;',
  27584. 'begin',
  27585. ' e.DoIt;',
  27586. ' red.DoIt;',
  27587. ' TEnum.blue.DoIt;',
  27588. ' TEnum(1).DoIt;',
  27589. ' TEnum.Swing(3);',
  27590. '']);
  27591. ConvertProgram;
  27592. CheckSource('TestTypeHelper_EnumType',
  27593. LinesToStr([ // statements
  27594. 'this.TEnum = {',
  27595. ' "0": "red",',
  27596. ' red: 0,',
  27597. ' "1": "blue",',
  27598. ' blue: 1',
  27599. '};',
  27600. 'rtl.createHelper(this, "THelper", null, function () {',
  27601. ' this.DoIt = function (e) {',
  27602. ' this.set($mod.TEnum.red);',
  27603. ' this.set(this.get() + 1);',
  27604. ' var $with = this.get();',
  27605. ' $mod.THelper.DoIt.call(this, 123);',
  27606. ' };',
  27607. ' this.Swing = function (w) {',
  27608. ' };',
  27609. '});',
  27610. 'this.e = 0;',
  27611. '']),
  27612. LinesToStr([ // $mod.$main
  27613. '$mod.THelper.DoIt.call({',
  27614. ' p: $mod,',
  27615. ' get: function () {',
  27616. ' return this.p.e;',
  27617. ' },',
  27618. ' set: function (v) {',
  27619. ' this.p.e = v;',
  27620. ' }',
  27621. '}, 123);',
  27622. '$mod.THelper.DoIt.call({',
  27623. ' p: $mod.TEnum,',
  27624. ' get: function () {',
  27625. ' return this.p.red;',
  27626. ' },',
  27627. ' set: function (v) {',
  27628. ' rtl.raiseE("EPropReadOnly");',
  27629. ' }',
  27630. '}, 123);',
  27631. '$mod.THelper.DoIt.call({',
  27632. ' p: $mod.TEnum,',
  27633. ' get: function () {',
  27634. ' return this.p.blue;',
  27635. ' },',
  27636. ' set: function (v) {',
  27637. ' rtl.raiseE("EPropReadOnly");',
  27638. ' }',
  27639. '}, 123);',
  27640. '$mod.THelper.DoIt.call({',
  27641. ' get: function () {',
  27642. ' return 1;',
  27643. ' },',
  27644. ' set: function (v) {',
  27645. ' rtl.raiseE("EPropReadOnly");',
  27646. ' }',
  27647. '}, 123);',
  27648. '$mod.THelper.Swing(3);',
  27649. '']));
  27650. end;
  27651. procedure TTestModule.TestTypeHelper_SetType;
  27652. begin
  27653. StartProgram(false);
  27654. Add([
  27655. '{$modeswitch typehelpers}',
  27656. 'type',
  27657. ' TEnum = (red,blue);',
  27658. ' TSetOfEnum = set of TEnum;',
  27659. ' THelper = type helper for TSetOfEnum',
  27660. ' procedure DoIt(e: byte = 123);',
  27661. ' constructor Init(e: TEnum);',
  27662. ' constructor InitEmpty;',
  27663. ' end;',
  27664. 'procedure THelper.DoIt(e: byte);',
  27665. 'begin',
  27666. ' Self:=[];',
  27667. ' Self:=[red];',
  27668. ' Include(Self,blue);',
  27669. 'end;',
  27670. 'constructor THelper.Init(e: TEnum);',
  27671. 'begin',
  27672. ' Self:=[];',
  27673. ' Self:=[e];',
  27674. ' Include(Self,blue);',
  27675. 'end;',
  27676. 'constructor THelper.InitEmpty;',
  27677. 'begin',
  27678. 'end;',
  27679. 'var s: TSetOfEnum;',
  27680. 'begin',
  27681. ' s.DoIt;',
  27682. //' [red].DoIt;',
  27683. //' with s do DoIt;',
  27684. //' with [red,blue] do DoIt;',
  27685. ' s:=TSetOfEnum.Init(blue);',
  27686. ' s:=s.Init(blue);',
  27687. '']);
  27688. ConvertProgram;
  27689. CheckSource('TestTypeHelper_SetType',
  27690. LinesToStr([ // statements
  27691. 'this.TEnum = {',
  27692. ' "0": "red",',
  27693. ' red: 0,',
  27694. ' "1": "blue",',
  27695. ' blue: 1',
  27696. '};',
  27697. 'rtl.createHelper(this, "THelper", null, function () {',
  27698. ' this.DoIt = function (e) {',
  27699. ' this.set({});',
  27700. ' this.set(rtl.createSet($mod.TEnum.red));',
  27701. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  27702. ' };',
  27703. ' this.Init = function (e) {',
  27704. ' this.set({});',
  27705. ' this.set(rtl.createSet(e));',
  27706. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  27707. ' return this.get();',
  27708. ' };',
  27709. ' this.InitEmpty = function () {',
  27710. ' return this.get();',
  27711. ' };',
  27712. ' this.$new = function (fn, args) {',
  27713. ' return this[fn].apply({',
  27714. ' p: {},',
  27715. ' get: function () {',
  27716. ' return this.p;',
  27717. ' },',
  27718. ' set: function (v) {',
  27719. ' this.p = v;',
  27720. ' }',
  27721. ' }, args);',
  27722. ' };',
  27723. '});',
  27724. 'this.s = {};',
  27725. '']),
  27726. LinesToStr([ // $mod.$main
  27727. '$mod.THelper.DoIt.call({',
  27728. ' p: $mod,',
  27729. ' get: function () {',
  27730. ' return this.p.s;',
  27731. ' },',
  27732. ' set: function (v) {',
  27733. ' this.p.s = v;',
  27734. ' }',
  27735. '}, 123);',
  27736. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  27737. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  27738. ' p: $mod,',
  27739. ' get: function () {',
  27740. ' return this.p.s;',
  27741. ' },',
  27742. ' set: function (v) {',
  27743. ' this.p.s = v;',
  27744. ' }',
  27745. '}, $mod.TEnum.blue));',
  27746. '']));
  27747. end;
  27748. procedure TTestModule.TestTypeHelper_InterfaceType;
  27749. begin
  27750. StartProgram(false);
  27751. Add([
  27752. '{$interfaces com}',
  27753. '{$modeswitch typehelpers}',
  27754. 'type',
  27755. ' IUnknown = interface',
  27756. ' function _AddRef: longint;',
  27757. ' function _Release: longint;',
  27758. ' end;',
  27759. ' TObject = class(IUnknown)',
  27760. ' function _AddRef: longint; virtual; abstract;',
  27761. ' function _Release: longint; virtual; abstract;',
  27762. ' end;',
  27763. ' THelper = type helper for IUnknown',
  27764. ' procedure Fly(e: byte = 123);',
  27765. ' class procedure Run; static;',
  27766. ' end;',
  27767. 'var',
  27768. ' i: IUnknown;',
  27769. ' o: TObject;',
  27770. 'procedure THelper.Fly(e: byte);',
  27771. 'begin',
  27772. ' i:=Self;',
  27773. ' o:=Self as TObject;',
  27774. ' Self:=nil;',
  27775. ' Self:=i;',
  27776. ' Self:=o;',
  27777. ' with Self do begin',
  27778. ' Fly;',
  27779. ' Fly();',
  27780. ' end;',
  27781. 'end;',
  27782. 'class procedure THelper.Run;',
  27783. 'var l: IUnknown;',
  27784. 'begin',
  27785. ' l.Fly;',
  27786. ' l.Fly();',
  27787. 'end;',
  27788. 'begin',
  27789. ' i.Fly;',
  27790. ' i.Fly();',
  27791. ' i.Run;',
  27792. ' i.Run();',
  27793. ' IUnknown.Run;',
  27794. ' IUnknown.Run();',
  27795. '']);
  27796. ConvertProgram;
  27797. CheckSource('TestTypeHelper_InterfaceType',
  27798. LinesToStr([ // statements
  27799. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  27800. 'rtl.createClass(this, "TObject", null, function () {',
  27801. ' this.$init = function () {',
  27802. ' };',
  27803. ' this.$final = function () {',
  27804. ' };',
  27805. ' rtl.addIntf(this, $mod.IUnknown);',
  27806. '});',
  27807. 'rtl.createHelper(this, "THelper", null, function () {',
  27808. ' this.Fly = function (e) {',
  27809. ' var $ir = rtl.createIntfRefs();',
  27810. ' try {',
  27811. ' rtl.setIntfP($mod, "i", this.get());',
  27812. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  27813. ' this.set(null);',
  27814. ' this.set($mod.i);',
  27815. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  27816. ' var $with = this.get();',
  27817. ' $mod.THelper.Fly.call(this, 123);',
  27818. ' $mod.THelper.Fly.call(this, 123);',
  27819. ' } finally {',
  27820. ' $ir.free();',
  27821. ' };',
  27822. ' };',
  27823. ' this.Run = function () {',
  27824. ' var l = null;',
  27825. ' try {',
  27826. ' $mod.THelper.Fly.call({',
  27827. ' get: function () {',
  27828. ' return l;',
  27829. ' },',
  27830. ' set: function (v) {',
  27831. ' l = rtl.setIntfL(l, v);',
  27832. ' }',
  27833. ' }, 123);',
  27834. ' $mod.THelper.Fly.call({',
  27835. ' get: function () {',
  27836. ' return l;',
  27837. ' },',
  27838. ' set: function (v) {',
  27839. ' l = rtl.setIntfL(l, v);',
  27840. ' }',
  27841. ' }, 123);',
  27842. ' } finally {',
  27843. ' rtl._Release(l);',
  27844. ' };',
  27845. ' };',
  27846. '});',
  27847. 'this.i = null;',
  27848. 'this.o = null;',
  27849. '']),
  27850. LinesToStr([ // $mod.$main
  27851. '$mod.THelper.Fly.call({',
  27852. ' p: $mod,',
  27853. ' get: function () {',
  27854. ' return this.p.i;',
  27855. ' },',
  27856. ' set: function (v) {',
  27857. ' rtl.setIntfP(this.p, "i", v);',
  27858. ' }',
  27859. '}, 123);',
  27860. '$mod.THelper.Fly.call({',
  27861. ' p: $mod,',
  27862. ' get: function () {',
  27863. ' return this.p.i;',
  27864. ' },',
  27865. ' set: function (v) {',
  27866. ' rtl.setIntfP(this.p, "i", v);',
  27867. ' }',
  27868. '}, 123);',
  27869. '$mod.THelper.Run();',
  27870. '$mod.THelper.Run();',
  27871. '$mod.THelper.Run();',
  27872. '$mod.THelper.Run();',
  27873. '']));
  27874. end;
  27875. procedure TTestModule.TestTypeHelper_NestedSelf;
  27876. begin
  27877. StartProgram(false);
  27878. Add([
  27879. '{$modeswitch typehelpers}',
  27880. 'type',
  27881. ' THelper = type helper for string',
  27882. ' procedure Run(Value: string);',
  27883. ' end;',
  27884. 'procedure THelper.Run(Value: string);',
  27885. ' function Sub(i: nativeint): boolean;',
  27886. ' begin',
  27887. ' Result:=Self[i+1]=Value[i];',
  27888. ' end;',
  27889. 'begin',
  27890. ' if Self[3]=Value[4] then ;',
  27891. 'end;',
  27892. 'begin',
  27893. '']);
  27894. ConvertProgram;
  27895. CheckSource('TestTypeHelper_NestedSelf',
  27896. LinesToStr([ // statements
  27897. 'rtl.createHelper(this, "THelper", null, function () {',
  27898. ' this.Run = function (Value) {',
  27899. ' var $Self = this;',
  27900. ' function Sub(i) {',
  27901. ' var Result = false;',
  27902. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  27903. ' return Result;',
  27904. ' };',
  27905. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  27906. ' };',
  27907. '});',
  27908. '']),
  27909. LinesToStr([ // $mod.$main
  27910. '']));
  27911. end;
  27912. procedure TTestModule.TestProcType;
  27913. begin
  27914. StartProgram(false);
  27915. Add([
  27916. 'type',
  27917. ' TProcInt = procedure(vI: longint = 1);',
  27918. 'procedure DoIt(vJ: longint);',
  27919. 'begin end;',
  27920. 'var',
  27921. ' b: boolean;',
  27922. ' vP, vQ: tprocint;',
  27923. 'begin',
  27924. ' vp:=nil;',
  27925. ' vp:=vp;',
  27926. ' vp:=@doit;',
  27927. ' vp;',
  27928. ' vp();',
  27929. ' vp(2);',
  27930. ' b:=vp=nil;',
  27931. ' b:=nil=vp;',
  27932. ' b:=vp=vq;',
  27933. ' b:=vp=@doit;',
  27934. ' b:=@doit=vp;',
  27935. ' b:=vp<>nil;',
  27936. ' b:=nil<>vp;',
  27937. ' b:=vp<>vq;',
  27938. ' b:=vp<>@doit;',
  27939. ' b:=@doit<>vp;',
  27940. ' b:=Assigned(vp);',
  27941. ' if Assigned(vp) then ;']);
  27942. ConvertProgram;
  27943. CheckSource('TestProcType',
  27944. LinesToStr([ // statements
  27945. 'this.DoIt = function(vJ) {',
  27946. '};',
  27947. 'this.b = false;',
  27948. 'this.vP = null;',
  27949. 'this.vQ = null;'
  27950. ]),
  27951. LinesToStr([ // $mod.$main
  27952. '$mod.vP = null;',
  27953. '$mod.vP = $mod.vP;',
  27954. '$mod.vP = $mod.DoIt;',
  27955. '$mod.vP(1);',
  27956. '$mod.vP(1);',
  27957. '$mod.vP(2);',
  27958. '$mod.b = $mod.vP === null;',
  27959. '$mod.b = null === $mod.vP;',
  27960. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  27961. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27962. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27963. '$mod.b = $mod.vP !== null;',
  27964. '$mod.b = null !== $mod.vP;',
  27965. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  27966. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27967. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27968. '$mod.b = $mod.vP != null;',
  27969. 'if ($mod.vP != null) ;',
  27970. '']));
  27971. end;
  27972. procedure TTestModule.TestProcType_Arg;
  27973. begin
  27974. StartProgram(false);
  27975. Add([
  27976. 'type',
  27977. ' TProcInt = procedure(vI: longint = 1);',
  27978. 'procedure DoIt(vJ: longint); begin end;',
  27979. 'procedure DoSome(vP, vQ: TProcInt);',
  27980. 'var',
  27981. ' b: boolean;',
  27982. 'begin',
  27983. ' vp:=nil;',
  27984. ' vp:=vp;',
  27985. ' vp:=@doit;',
  27986. ' vp;',
  27987. ' vp();',
  27988. ' vp(2);',
  27989. ' b:=vp=nil;',
  27990. ' b:=nil=vp;',
  27991. ' b:=vp=vq;',
  27992. ' b:=vp=@doit;',
  27993. ' b:=@doit=vp;',
  27994. ' b:=vp<>nil;',
  27995. ' b:=nil<>vp;',
  27996. ' b:=vp<>vq;',
  27997. ' b:=vp<>@doit;',
  27998. ' b:=@doit<>vp;',
  27999. ' b:=Assigned(vp);',
  28000. ' if Assigned(vp) then ;',
  28001. 'end;',
  28002. 'begin',
  28003. ' DoSome(@DoIt,nil);']);
  28004. ConvertProgram;
  28005. CheckSource('TestProcType_Arg',
  28006. LinesToStr([ // statements
  28007. 'this.DoIt = function(vJ) {',
  28008. '};',
  28009. 'this.DoSome = function(vP, vQ) {',
  28010. ' var b = false;',
  28011. ' vP = null;',
  28012. ' vP = vP;',
  28013. ' vP = $mod.DoIt;',
  28014. ' vP(1);',
  28015. ' vP(1);',
  28016. ' vP(2);',
  28017. ' b = vP === null;',
  28018. ' b = null === vP;',
  28019. ' b = rtl.eqCallback(vP,vQ);',
  28020. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  28021. ' b = rtl.eqCallback($mod.DoIt, vP);',
  28022. ' b = vP !== null;',
  28023. ' b = null !== vP;',
  28024. ' b = !rtl.eqCallback(vP, vQ);',
  28025. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  28026. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  28027. ' b = vP != null;',
  28028. ' if (vP != null) ;',
  28029. '};',
  28030. '']),
  28031. LinesToStr([ // $mod.$main
  28032. '$mod.DoSome($mod.DoIt,null);',
  28033. '']));
  28034. end;
  28035. procedure TTestModule.TestProcType_FunctionFPC;
  28036. begin
  28037. StartProgram(false);
  28038. Add('type');
  28039. Add(' TFuncInt = function(vA: longint = 1): longint;');
  28040. Add('function DoIt(vI: longint): longint;');
  28041. Add('begin end;');
  28042. Add('var');
  28043. Add(' b: boolean;');
  28044. Add(' vP, vQ: tfuncint;');
  28045. Add('begin');
  28046. Add(' vp:=nil;');
  28047. Add(' vp:=vp;');
  28048. Add(' vp:=@doit;'); // ok in fpc and delphi
  28049. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  28050. Add(' vp;'); // ok in fpc and delphi
  28051. Add(' vp();');
  28052. Add(' vp(2);');
  28053. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  28054. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  28055. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  28056. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  28057. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  28058. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  28059. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  28060. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  28061. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  28062. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  28063. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  28064. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  28065. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  28066. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  28067. Add(' b:=Assigned(vp);');
  28068. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  28069. Add(' doit(vp());'); // ok in fpc and delphi
  28070. Add(' doit(vp(2));'); // ok in fpc and delphi
  28071. ConvertProgram;
  28072. CheckSource('TestProcType_FunctionFPC',
  28073. LinesToStr([ // statements
  28074. 'this.DoIt = function(vI) {',
  28075. ' var Result = 0;',
  28076. ' return Result;',
  28077. '};',
  28078. 'this.b = false;',
  28079. 'this.vP = null;',
  28080. 'this.vQ = null;'
  28081. ]),
  28082. LinesToStr([ // $mod.$main
  28083. '$mod.vP = null;',
  28084. '$mod.vP = $mod.vP;',
  28085. '$mod.vP = $mod.DoIt;',
  28086. '$mod.vP(1);',
  28087. '$mod.vP(1);',
  28088. '$mod.vP(2);',
  28089. '$mod.b = $mod.vP === null;',
  28090. '$mod.b = null === $mod.vP;',
  28091. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  28092. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  28093. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  28094. '$mod.b = 4 === $mod.vP(1);',
  28095. '$mod.b = $mod.vP !== null;',
  28096. '$mod.b = null !== $mod.vP;',
  28097. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  28098. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  28099. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  28100. '$mod.b = 6 !== $mod.vP(1);',
  28101. '$mod.b = $mod.vP != null;',
  28102. '$mod.DoIt($mod.vP(1));',
  28103. '$mod.DoIt($mod.vP(2));',
  28104. '']));
  28105. end;
  28106. procedure TTestModule.TestProcType_FunctionDelphi;
  28107. begin
  28108. StartProgram(false);
  28109. Add('{$mode Delphi}');
  28110. Add('type');
  28111. Add(' TFuncInt = function(vA: longint = 1): longint;');
  28112. Add('function DoIt(vI: longint): longint;');
  28113. Add('begin end;');
  28114. Add('var');
  28115. Add(' b: boolean;');
  28116. Add(' vP, vQ: tfuncint;');
  28117. Add('begin');
  28118. Add(' vp:=nil;');
  28119. Add(' vp:=vp;');
  28120. Add(' vp:=@doit;'); // ok in fpc and delphi
  28121. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  28122. Add(' vp;'); // ok in fpc and delphi
  28123. Add(' vp();');
  28124. Add(' vp(2);');
  28125. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  28126. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  28127. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  28128. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  28129. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  28130. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  28131. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  28132. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  28133. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  28134. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  28135. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  28136. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  28137. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  28138. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  28139. Add(' b:=Assigned(vp);');
  28140. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  28141. Add(' doit(vp());'); // ok in fpc and delphi
  28142. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  28143. ConvertProgram;
  28144. CheckSource('TestProcType_FunctionDelphi',
  28145. LinesToStr([ // statements
  28146. 'this.DoIt = function(vI) {',
  28147. ' var Result = 0;',
  28148. ' return Result;',
  28149. '};',
  28150. 'this.b = false;',
  28151. 'this.vP = null;',
  28152. 'this.vQ = null;'
  28153. ]),
  28154. LinesToStr([ // $mod.$main
  28155. '$mod.vP = null;',
  28156. '$mod.vP = $mod.vP;',
  28157. '$mod.vP = $mod.DoIt;',
  28158. '$mod.vP = $mod.DoIt;',
  28159. '$mod.vP(1);',
  28160. '$mod.vP(1);',
  28161. '$mod.vP(2);',
  28162. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  28163. '$mod.b = $mod.vP(1) === 3;',
  28164. '$mod.b = 4 === $mod.vP(1);',
  28165. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  28166. '$mod.b = $mod.vP(1) !== 5;',
  28167. '$mod.b = 6 !== $mod.vP(1);',
  28168. '$mod.b = $mod.vP != null;',
  28169. '$mod.DoIt($mod.vP(1));',
  28170. '$mod.DoIt($mod.vP(1));',
  28171. '$mod.DoIt($mod.vP(2));',
  28172. '']));
  28173. end;
  28174. procedure TTestModule.TestProcType_ProcedureDelphi;
  28175. begin
  28176. StartProgram(false);
  28177. Add('{$mode Delphi}');
  28178. Add('type');
  28179. Add(' TProc = procedure;');
  28180. Add('procedure DoIt;');
  28181. Add('begin end;');
  28182. Add('var');
  28183. Add(' b: boolean;');
  28184. Add(' vP, vQ: tproc;');
  28185. Add('begin');
  28186. Add(' vp:=nil;');
  28187. Add(' vp:=vp;');
  28188. Add(' vp:=vq;');
  28189. Add(' vp:=@doit;'); // ok in fpc and delphi, Note that in Delphi type of @F is Pointer, while in FPC it is the proc type
  28190. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  28191. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  28192. Add(' vp;'); // ok in fpc and delphi
  28193. Add(' vp();');
  28194. // equal
  28195. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  28196. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  28197. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  28198. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  28199. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  28200. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  28201. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  28202. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  28203. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  28204. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  28205. // unequal
  28206. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  28207. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  28208. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  28209. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  28210. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  28211. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  28212. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  28213. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  28214. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  28215. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  28216. Add(' b:=Assigned(vp);');
  28217. ConvertProgram;
  28218. CheckSource('TestProcType_ProcedureDelphi',
  28219. LinesToStr([ // statements
  28220. 'this.DoIt = function() {',
  28221. '};',
  28222. 'this.b = false;',
  28223. 'this.vP = null;',
  28224. 'this.vQ = null;'
  28225. ]),
  28226. LinesToStr([ // $mod.$main
  28227. '$mod.vP = null;',
  28228. '$mod.vP = $mod.vP;',
  28229. '$mod.vP = $mod.vQ;',
  28230. '$mod.vP = $mod.DoIt;',
  28231. '$mod.vP = $mod.DoIt;',
  28232. '$mod.vP();',
  28233. '$mod.vP();',
  28234. '$mod.b = $mod.vP === null;',
  28235. '$mod.b = null === $mod.vP;',
  28236. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  28237. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  28238. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  28239. '$mod.b = $mod.vP !== null;',
  28240. '$mod.b = null !== $mod.vP;',
  28241. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  28242. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  28243. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  28244. '$mod.b = $mod.vP != null;',
  28245. '']));
  28246. end;
  28247. procedure TTestModule.TestProcType_AsParam;
  28248. begin
  28249. StartProgram(false);
  28250. Add('type');
  28251. Add(' TFuncInt = function(vA: longint = 1): longint;');
  28252. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  28253. Add('var vJ: tfuncint;');
  28254. Add('begin');
  28255. Add(' vg:=vg;');
  28256. Add(' vj:=vh;');
  28257. Add(' vi:=vi;');
  28258. Add(' doit(vg,vg,vg);');
  28259. Add(' doit(vh,vh,vj);');
  28260. Add(' doit(vi,vi,vi);');
  28261. Add(' doit(vj,vj,vj);');
  28262. Add('end;');
  28263. Add('var i: tfuncint;');
  28264. Add('begin');
  28265. Add(' doit(i,i,i);');
  28266. ConvertProgram;
  28267. CheckSource('TestProcType_AsParam',
  28268. LinesToStr([ // statements
  28269. 'this.DoIt = function (vG,vH,vI) {',
  28270. ' var vJ = null;',
  28271. ' vG = vG;',
  28272. ' vJ = vH;',
  28273. ' vI.set(vI.get());',
  28274. ' $mod.DoIt(vG, vG, {',
  28275. ' get: function () {',
  28276. ' return vG;',
  28277. ' },',
  28278. ' set: function (v) {',
  28279. ' vG = v;',
  28280. ' }',
  28281. ' });',
  28282. ' $mod.DoIt(vH, vH, {',
  28283. ' get: function () {',
  28284. ' return vJ;',
  28285. ' },',
  28286. ' set: function (v) {',
  28287. ' vJ = v;',
  28288. ' }',
  28289. ' });',
  28290. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  28291. ' $mod.DoIt(vJ, vJ, {',
  28292. ' get: function () {',
  28293. ' return vJ;',
  28294. ' },',
  28295. ' set: function (v) {',
  28296. ' vJ = v;',
  28297. ' }',
  28298. ' });',
  28299. '};',
  28300. 'this.i = null;'
  28301. ]),
  28302. LinesToStr([
  28303. '$mod.DoIt($mod.i,$mod.i,{',
  28304. ' p: $mod,',
  28305. ' get: function () {',
  28306. ' return this.p.i;',
  28307. ' },',
  28308. ' set: function (v) {',
  28309. ' this.p.i = v;',
  28310. ' }',
  28311. '});'
  28312. ]));
  28313. end;
  28314. procedure TTestModule.TestProcType_MethodFPC;
  28315. begin
  28316. StartProgram(false);
  28317. Add('type');
  28318. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  28319. Add(' TObject = class');
  28320. Add(' function DoIt(vA: longint = 1): longint;');
  28321. Add(' end;');
  28322. Add('function TObject.DoIt(vA: longint = 1): longint;');
  28323. Add('begin');
  28324. Add('end;');
  28325. Add('var');
  28326. Add(' Obj: TObject;');
  28327. Add(' vP: tfuncint;');
  28328. Add(' b: boolean;');
  28329. Add('begin');
  28330. Add(' vp:[email protected];'); // ok in fpc and delphi
  28331. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  28332. Add(' vp;'); // ok in fpc and delphi
  28333. Add(' vp();');
  28334. Add(' vp(2);');
  28335. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  28336. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  28337. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  28338. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  28339. ConvertProgram;
  28340. CheckSource('TestProcType_MethodFPC',
  28341. LinesToStr([ // statements
  28342. 'rtl.createClass(this, "TObject", null, function () {',
  28343. ' this.$init = function () {',
  28344. ' };',
  28345. ' this.$final = function () {',
  28346. ' };',
  28347. ' this.DoIt = function (vA) {',
  28348. ' var Result = 0;',
  28349. ' return Result;',
  28350. ' };',
  28351. '});',
  28352. 'this.Obj = null;',
  28353. 'this.vP = null;',
  28354. 'this.b = false;'
  28355. ]),
  28356. LinesToStr([
  28357. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  28358. '$mod.vP(1);',
  28359. '$mod.vP(1);',
  28360. '$mod.vP(2);',
  28361. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  28362. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  28363. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  28364. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  28365. '']));
  28366. end;
  28367. procedure TTestModule.TestProcType_MethodDelphi;
  28368. begin
  28369. StartProgram(false);
  28370. Add([
  28371. '{$mode delphi}',
  28372. 'type',
  28373. ' TFuncInt = function(vA: longint = 1): longint of object;',
  28374. ' TObject = class',
  28375. ' function DoIt(vA: longint = 1): longint;',
  28376. ' end;',
  28377. 'function TObject.DoIt(vA: longint = 1): longint;',
  28378. 'begin',
  28379. 'end;',
  28380. 'var',
  28381. ' Obj: TObject;',
  28382. ' vP: tfuncint;',
  28383. ' b: boolean;',
  28384. 'begin',
  28385. ' vp:[email protected];', // ok in fpc and delphi
  28386. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  28387. ' vp;', // ok in fpc and delphi
  28388. ' vp();',
  28389. ' vp(2);',
  28390. //' b:[email protected];', // ok in fpc, illegal in delphi
  28391. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  28392. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  28393. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  28394. '']);
  28395. ConvertProgram;
  28396. CheckSource('TestProcType_MethodDelphi',
  28397. LinesToStr([ // statements
  28398. 'rtl.createClass(this, "TObject", null, function () {',
  28399. ' this.$init = function () {',
  28400. ' };',
  28401. ' this.$final = function () {',
  28402. ' };',
  28403. ' this.DoIt = function (vA) {',
  28404. ' var Result = 0;',
  28405. ' return Result;',
  28406. ' };',
  28407. '});',
  28408. 'this.Obj = null;',
  28409. 'this.vP = null;',
  28410. 'this.b = false;'
  28411. ]),
  28412. LinesToStr([
  28413. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  28414. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  28415. '$mod.vP(1);',
  28416. '$mod.vP(1);',
  28417. '$mod.vP(2);',
  28418. '']));
  28419. end;
  28420. procedure TTestModule.TestProcType_PropertyFPC;
  28421. begin
  28422. StartProgram(false);
  28423. Add('type');
  28424. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  28425. Add(' TObject = class');
  28426. Add(' FOnFoo: TFuncInt;');
  28427. Add(' function DoIt(vA: longint = 1): longint;');
  28428. Add(' function GetFoo: TFuncInt;');
  28429. Add(' procedure SetFoo(const Value: TFuncInt);');
  28430. Add(' function GetEvents(Index: longint): TFuncInt;');
  28431. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  28432. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  28433. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  28434. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  28435. Add(' end;');
  28436. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  28437. Add('function tobject.getfoo: tfuncint; begin end;');
  28438. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  28439. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  28440. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  28441. Add('var');
  28442. Add(' Obj: TObject;');
  28443. Add(' vP: tfuncint;');
  28444. Add(' b: boolean;');
  28445. Add('begin');
  28446. Add(' obj.onfoo:=nil;');
  28447. Add(' obj.onbar:=nil;');
  28448. Add(' obj.events[1]:=nil;');
  28449. Add(' obj.onfoo:=obj.onfoo;');
  28450. Add(' obj.onbar:=obj.onbar;');
  28451. Add(' obj.events[2]:=obj.events[3];');
  28452. Add(' obj.onfoo:[email protected];');
  28453. Add(' obj.onbar:[email protected];');
  28454. Add(' obj.events[4]:[email protected];');
  28455. //Add(' obj.onfoo:=obj.doit;'); // delphi
  28456. //Add(' obj.onbar:=obj.doit;'); // delphi
  28457. //Add(' obj.events[4]:=obj.doit;'); // delphi
  28458. Add(' obj.onfoo;');
  28459. Add(' obj.onbar;');
  28460. //Add(' obj.events[5];'); ToDo in pasresolver
  28461. Add(' obj.onfoo();');
  28462. Add(' obj.onbar();');
  28463. Add(' obj.events[6]();');
  28464. Add(' b:=obj.onfoo=nil;');
  28465. Add(' b:=obj.onbar=nil;');
  28466. Add(' b:=obj.events[7]=nil;');
  28467. Add(' b:=obj.onfoo<>nil;');
  28468. Add(' b:=obj.onbar<>nil;');
  28469. Add(' b:=obj.events[8]<>nil;');
  28470. Add(' b:=obj.onfoo=vp;');
  28471. Add(' b:=obj.onbar=vp;');
  28472. Add(' b:=obj.events[9]=vp;');
  28473. Add(' b:=obj.onfoo=obj.onfoo;');
  28474. Add(' b:=obj.onbar=obj.onfoo;');
  28475. Add(' b:=obj.events[10]=obj.onfoo;');
  28476. Add(' b:=obj.onfoo<>obj.onfoo;');
  28477. Add(' b:=obj.onbar<>obj.onfoo;');
  28478. Add(' b:=obj.events[11]<>obj.onfoo;');
  28479. Add(' b:[email protected];');
  28480. Add(' b:[email protected];');
  28481. Add(' b:=obj.events[12][email protected];');
  28482. Add(' b:=obj.onfoo<>@obj.doit;');
  28483. Add(' b:=obj.onbar<>@obj.doit;');
  28484. Add(' b:=obj.events[12]<>@obj.doit;');
  28485. Add(' b:=Assigned(obj.onfoo);');
  28486. Add(' b:=Assigned(obj.onbar);');
  28487. Add(' b:=Assigned(obj.events[13]);');
  28488. ConvertProgram;
  28489. CheckSource('TestProcType_PropertyFPC',
  28490. LinesToStr([ // statements
  28491. 'rtl.createClass(this, "TObject", null, function () {',
  28492. ' this.$init = function () {',
  28493. ' this.FOnFoo = null;',
  28494. ' };',
  28495. ' this.$final = function () {',
  28496. ' this.FOnFoo = undefined;',
  28497. ' };',
  28498. ' this.DoIt = function (vA) {',
  28499. ' var Result = 0;',
  28500. ' return Result;',
  28501. ' };',
  28502. 'this.GetFoo = function () {',
  28503. ' var Result = null;',
  28504. ' return Result;',
  28505. '};',
  28506. 'this.SetFoo = function (Value) {',
  28507. '};',
  28508. 'this.GetEvents = function (Index) {',
  28509. ' var Result = null;',
  28510. ' return Result;',
  28511. '};',
  28512. 'this.SetEvents = function (Index, Value) {',
  28513. '};',
  28514. '});',
  28515. 'this.Obj = null;',
  28516. 'this.vP = null;',
  28517. 'this.b = false;'
  28518. ]),
  28519. LinesToStr([
  28520. '$mod.Obj.FOnFoo = null;',
  28521. '$mod.Obj.SetFoo(null);',
  28522. '$mod.Obj.SetEvents(1, null);',
  28523. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  28524. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  28525. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  28526. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  28527. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  28528. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  28529. '$mod.Obj.FOnFoo(1);',
  28530. '$mod.Obj.GetFoo();',
  28531. '$mod.Obj.FOnFoo(1);',
  28532. '$mod.Obj.GetFoo()(1);',
  28533. '$mod.Obj.GetEvents(6)(1);',
  28534. '$mod.b = $mod.Obj.FOnFoo === null;',
  28535. '$mod.b = $mod.Obj.GetFoo() === null;',
  28536. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  28537. '$mod.b = $mod.Obj.FOnFoo !== null;',
  28538. '$mod.b = $mod.Obj.GetFoo() !== null;',
  28539. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  28540. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  28541. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  28542. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  28543. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  28544. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  28545. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  28546. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  28547. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  28548. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  28549. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  28550. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  28551. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  28552. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  28553. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  28554. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  28555. '$mod.b = $mod.Obj.FOnFoo != null;',
  28556. '$mod.b = $mod.Obj.GetFoo() != null;',
  28557. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  28558. '']));
  28559. end;
  28560. procedure TTestModule.TestProcType_PropertyDelphi;
  28561. begin
  28562. StartProgram(false);
  28563. Add('{$mode delphi}');
  28564. Add('type');
  28565. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  28566. Add(' TObject = class');
  28567. Add(' FOnFoo: TFuncInt;');
  28568. Add(' function DoIt(vA: longint = 1): longint;');
  28569. Add(' function GetFoo: TFuncInt;');
  28570. Add(' procedure SetFoo(const Value: TFuncInt);');
  28571. Add(' function GetEvents(Index: longint): TFuncInt;');
  28572. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  28573. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  28574. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  28575. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  28576. Add(' end;');
  28577. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  28578. Add('function tobject.getfoo: tfuncint; begin end;');
  28579. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  28580. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  28581. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  28582. Add('var');
  28583. Add(' Obj: TObject;');
  28584. Add(' vP: tfuncint;');
  28585. Add(' b: boolean;');
  28586. Add('begin');
  28587. Add(' obj.onfoo:=nil;');
  28588. Add(' obj.onbar:=nil;');
  28589. Add(' obj.events[1]:=nil;');
  28590. Add(' obj.onfoo:=obj.onfoo;');
  28591. Add(' obj.onbar:=obj.onbar;');
  28592. Add(' obj.events[2]:=obj.events[3];');
  28593. Add(' obj.onfoo:[email protected];');
  28594. Add(' obj.onbar:[email protected];');
  28595. Add(' obj.events[4]:[email protected];');
  28596. Add(' obj.onfoo:=obj.doit;'); // delphi
  28597. Add(' obj.onbar:=obj.doit;'); // delphi
  28598. Add(' obj.events[4]:=obj.doit;'); // delphi
  28599. Add(' obj.onfoo;');
  28600. Add(' obj.onbar;');
  28601. //Add(' obj.events[5];'); ToDo in pasresolver
  28602. Add(' obj.onfoo();');
  28603. Add(' obj.onbar();');
  28604. Add(' obj.events[6]();');
  28605. //Add(' b:=obj.onfoo=nil;'); // fpc
  28606. //Add(' b:=obj.onbar=nil;'); // fpc
  28607. //Add(' b:=obj.events[7]=nil;'); // fpc
  28608. //Add(' b:=obj.onfoo<>nil;'); // fpc
  28609. //Add(' b:=obj.onbar<>nil;'); // fpc
  28610. //Add(' b:=obj.events[8]<>nil;'); // fpc
  28611. Add(' b:=obj.onfoo=vp;');
  28612. Add(' b:=obj.onbar=vp;');
  28613. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  28614. Add(' b:=obj.onfoo=obj.onfoo;');
  28615. Add(' b:=obj.onbar=obj.onfoo;');
  28616. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  28617. Add(' b:=obj.onfoo<>obj.onfoo;');
  28618. Add(' b:=obj.onbar<>obj.onfoo;');
  28619. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  28620. //Add(' b:[email protected];'); // fpc
  28621. //Add(' b:[email protected];'); // fpc
  28622. //Add(' b:=obj.events[12][email protected];'); // fpc
  28623. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  28624. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  28625. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  28626. Add(' b:=Assigned(obj.onfoo);');
  28627. Add(' b:=Assigned(obj.onbar);');
  28628. Add(' b:=Assigned(obj.events[13]);');
  28629. ConvertProgram;
  28630. CheckSource('TestProcType_PropertyDelphi',
  28631. LinesToStr([ // statements
  28632. 'rtl.createClass(this, "TObject", null, function () {',
  28633. ' this.$init = function () {',
  28634. ' this.FOnFoo = null;',
  28635. ' };',
  28636. ' this.$final = function () {',
  28637. ' this.FOnFoo = undefined;',
  28638. ' };',
  28639. ' this.DoIt = function (vA) {',
  28640. ' var Result = 0;',
  28641. ' return Result;',
  28642. ' };',
  28643. 'this.GetFoo = function () {',
  28644. ' var Result = null;',
  28645. ' return Result;',
  28646. '};',
  28647. 'this.SetFoo = function (Value) {',
  28648. '};',
  28649. 'this.GetEvents = function (Index) {',
  28650. ' var Result = null;',
  28651. ' return Result;',
  28652. '};',
  28653. 'this.SetEvents = function (Index, Value) {',
  28654. '};',
  28655. '});',
  28656. 'this.Obj = null;',
  28657. 'this.vP = null;',
  28658. 'this.b = false;'
  28659. ]),
  28660. LinesToStr([
  28661. '$mod.Obj.FOnFoo = null;',
  28662. '$mod.Obj.SetFoo(null);',
  28663. '$mod.Obj.SetEvents(1, null);',
  28664. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  28665. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  28666. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  28667. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  28668. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  28669. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  28670. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  28671. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  28672. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  28673. '$mod.Obj.FOnFoo(1);',
  28674. '$mod.Obj.GetFoo();',
  28675. '$mod.Obj.FOnFoo(1);',
  28676. '$mod.Obj.GetFoo()(1);',
  28677. '$mod.Obj.GetEvents(6)(1);',
  28678. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  28679. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  28680. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  28681. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  28682. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  28683. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  28684. '$mod.b = $mod.Obj.FOnFoo != null;',
  28685. '$mod.b = $mod.Obj.GetFoo() != null;',
  28686. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  28687. '']));
  28688. end;
  28689. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  28690. begin
  28691. StartProgram(false);
  28692. Add('type');
  28693. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  28694. Add(' TObject = class');
  28695. Add(' FOnFoo: TFuncInt;');
  28696. Add(' function DoIt(vA: longint = 1): longint;');
  28697. Add(' function GetFoo: TFuncInt;');
  28698. Add(' procedure SetFoo(const Value: TFuncInt);');
  28699. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  28700. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  28701. Add(' end;');
  28702. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  28703. Add('function tobject.getfoo: tfuncint; begin end;');
  28704. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  28705. Add('var');
  28706. Add(' Obj: TObject;');
  28707. Add(' vP: tfuncint;');
  28708. Add(' b: boolean;');
  28709. Add('begin');
  28710. Add('with obj do begin');
  28711. Add(' fonfoo:=nil;');
  28712. Add(' onfoo:=nil;');
  28713. Add(' onbar:=nil;');
  28714. Add(' fonfoo:=fonfoo;');
  28715. Add(' onfoo:=onfoo;');
  28716. Add(' onbar:=onbar;');
  28717. Add(' fonfoo:=@doit;');
  28718. Add(' onfoo:=@doit;');
  28719. Add(' onbar:=@doit;');
  28720. //Add(' fonfoo:=doit;'); // delphi
  28721. //Add(' onfoo:=doit;'); // delphi
  28722. //Add(' onbar:=doit;'); // delphi
  28723. Add(' fonfoo;');
  28724. Add(' onfoo;');
  28725. Add(' onbar;');
  28726. Add(' fonfoo();');
  28727. Add(' onfoo();');
  28728. Add(' onbar();');
  28729. Add(' b:=fonfoo=nil;');
  28730. Add(' b:=onfoo=nil;');
  28731. Add(' b:=onbar=nil;');
  28732. Add(' b:=fonfoo<>nil;');
  28733. Add(' b:=onfoo<>nil;');
  28734. Add(' b:=onbar<>nil;');
  28735. Add(' b:=fonfoo=vp;');
  28736. Add(' b:=onfoo=vp;');
  28737. Add(' b:=onbar=vp;');
  28738. Add(' b:=fonfoo=fonfoo;');
  28739. Add(' b:=onfoo=onfoo;');
  28740. Add(' b:=onbar=onfoo;');
  28741. Add(' b:=fonfoo<>fonfoo;');
  28742. Add(' b:=onfoo<>onfoo;');
  28743. Add(' b:=onbar<>onfoo;');
  28744. Add(' b:=fonfoo=@doit;');
  28745. Add(' b:=onfoo=@doit;');
  28746. Add(' b:=onbar=@doit;');
  28747. Add(' b:=fonfoo<>@doit;');
  28748. Add(' b:=onfoo<>@doit;');
  28749. Add(' b:=onbar<>@doit;');
  28750. Add(' b:=Assigned(fonfoo);');
  28751. Add(' b:=Assigned(onfoo);');
  28752. Add(' b:=Assigned(onbar);');
  28753. Add('end;');
  28754. ConvertProgram;
  28755. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  28756. LinesToStr([ // statements
  28757. 'rtl.createClass(this, "TObject", null, function () {',
  28758. ' this.$init = function () {',
  28759. ' this.FOnFoo = null;',
  28760. ' };',
  28761. ' this.$final = function () {',
  28762. ' this.FOnFoo = undefined;',
  28763. ' };',
  28764. ' this.DoIt = function (vA) {',
  28765. ' var Result = 0;',
  28766. ' return Result;',
  28767. ' };',
  28768. ' this.GetFoo = function () {',
  28769. ' var Result = null;',
  28770. ' return Result;',
  28771. ' };',
  28772. ' this.SetFoo = function (Value) {',
  28773. ' };',
  28774. '});',
  28775. 'this.Obj = null;',
  28776. 'this.vP = null;',
  28777. 'this.b = false;'
  28778. ]),
  28779. LinesToStr([
  28780. 'var $with = $mod.Obj;',
  28781. '$with.FOnFoo = null;',
  28782. '$with.FOnFoo = null;',
  28783. '$with.SetFoo(null);',
  28784. '$with.FOnFoo = $with.FOnFoo;',
  28785. '$with.FOnFoo = $with.FOnFoo;',
  28786. '$with.SetFoo($with.GetFoo());',
  28787. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  28788. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  28789. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  28790. '$with.FOnFoo(1);',
  28791. '$with.FOnFoo(1);',
  28792. '$with.GetFoo();',
  28793. '$with.FOnFoo(1);',
  28794. '$with.FOnFoo(1);',
  28795. '$with.GetFoo()(1);',
  28796. '$mod.b = $with.FOnFoo === null;',
  28797. '$mod.b = $with.FOnFoo === null;',
  28798. '$mod.b = $with.GetFoo() === null;',
  28799. '$mod.b = $with.FOnFoo !== null;',
  28800. '$mod.b = $with.FOnFoo !== null;',
  28801. '$mod.b = $with.GetFoo() !== null;',
  28802. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  28803. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  28804. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  28805. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28806. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28807. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  28808. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28809. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28810. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  28811. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28812. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28813. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  28814. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28815. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28816. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  28817. '$mod.b = $with.FOnFoo != null;',
  28818. '$mod.b = $with.FOnFoo != null;',
  28819. '$mod.b = $with.GetFoo() != null;',
  28820. '']));
  28821. end;
  28822. procedure TTestModule.TestProcType_Nested;
  28823. begin
  28824. StartProgram(false);
  28825. Add([
  28826. 'type',
  28827. ' TProcInt = procedure(vI: longint = 1);',
  28828. 'procedure DoIt(vJ: longint);',
  28829. 'var aProc: TProcInt;',
  28830. ' b: boolean;',
  28831. ' procedure Sub(vK: longint);',
  28832. ' var aSub: TProcInt;',
  28833. ' procedure SubSub(vK: longint);',
  28834. ' var aSubSub: TProcInt;',
  28835. ' begin;',
  28836. ' aProc:=@DoIt;',
  28837. ' aSub:=@DoIt;',
  28838. ' aSubSub:=@DoIt;',
  28839. ' aProc:=@Sub;',
  28840. ' aSub:=@Sub;',
  28841. ' aSubSub:=@Sub;',
  28842. ' aProc:=@SubSub;',
  28843. ' aSub:=@SubSub;',
  28844. ' aSubSub:=@SubSub;',
  28845. ' end;',
  28846. ' begin;',
  28847. ' end;',
  28848. 'begin;',
  28849. ' aProc:=@Sub;',
  28850. ' b:=aProc=@Sub;',
  28851. ' b:=@Sub=aProc;',
  28852. 'end;',
  28853. 'begin',
  28854. '']);
  28855. ConvertProgram;
  28856. CheckSource('TestProcType_Nested',
  28857. LinesToStr([ // statements
  28858. 'this.DoIt = function (vJ) {',
  28859. ' var aProc = null;',
  28860. ' var b = false;',
  28861. ' function Sub(vK) {',
  28862. ' var aSub = null;',
  28863. ' function SubSub(vK) {',
  28864. ' var aSubSub = null;',
  28865. ' aProc = $mod.DoIt;',
  28866. ' aSub = $mod.DoIt;',
  28867. ' aSubSub = $mod.DoIt;',
  28868. ' aProc = Sub;',
  28869. ' aSub = Sub;',
  28870. ' aSubSub = Sub;',
  28871. ' aProc = SubSub;',
  28872. ' aSub = SubSub;',
  28873. ' aSubSub = SubSub;',
  28874. ' };',
  28875. ' };',
  28876. ' aProc = Sub;',
  28877. ' b = rtl.eqCallback(aProc, Sub);',
  28878. ' b = rtl.eqCallback(Sub, aProc);',
  28879. '};',
  28880. '']),
  28881. LinesToStr([ // $mod.$main
  28882. '']));
  28883. end;
  28884. procedure TTestModule.TestProcType_NestedOfObject;
  28885. begin
  28886. StartProgram(false);
  28887. Add([
  28888. 'type',
  28889. ' TProcInt = procedure(vI: longint = 1) of object;',
  28890. ' TObject = class',
  28891. ' procedure DoIt(vJ: longint);',
  28892. ' end;',
  28893. 'procedure TObject.DoIt(vJ: longint);',
  28894. 'var aProc: TProcInt;',
  28895. ' b: boolean;',
  28896. ' procedure Sub(vK: longint);',
  28897. ' var aSub: TProcInt;',
  28898. ' procedure SubSub(vK: longint);',
  28899. ' var aSubSub: TProcInt;',
  28900. ' begin;',
  28901. ' aProc:=@DoIt;',
  28902. ' aSub:=@DoIt;',
  28903. ' aSubSub:=@DoIt;',
  28904. ' aProc:=@Sub;',
  28905. ' aSub:=@Sub;',
  28906. ' aSubSub:=@Sub;',
  28907. ' aProc:=@SubSub;',
  28908. ' aSub:=@SubSub;',
  28909. ' aSubSub:=@SubSub;',
  28910. ' end;',
  28911. ' begin;',
  28912. ' end;',
  28913. 'begin;',
  28914. ' aProc:=@Sub;',
  28915. ' b:=aProc=@Sub;',
  28916. ' b:=@Sub=aProc;',
  28917. 'end;',
  28918. 'begin',
  28919. '']);
  28920. ConvertProgram;
  28921. CheckSource('TestProcType_Nested',
  28922. LinesToStr([ // statements
  28923. 'rtl.createClass(this, "TObject", null, function () {',
  28924. ' this.$init = function () {',
  28925. ' };',
  28926. ' this.$final = function () {',
  28927. ' };',
  28928. ' this.DoIt = function (vJ) {',
  28929. ' var $Self = this;',
  28930. ' var aProc = null;',
  28931. ' var b = false;',
  28932. ' function Sub(vK) {',
  28933. ' var aSub = null;',
  28934. ' function SubSub(vK) {',
  28935. ' var aSubSub = null;',
  28936. ' aProc = rtl.createCallback($Self, "DoIt");',
  28937. ' aSub = rtl.createCallback($Self, "DoIt");',
  28938. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  28939. ' aProc = Sub;',
  28940. ' aSub = Sub;',
  28941. ' aSubSub = Sub;',
  28942. ' aProc = SubSub;',
  28943. ' aSub = SubSub;',
  28944. ' aSubSub = SubSub;',
  28945. ' };',
  28946. ' };',
  28947. ' aProc = Sub;',
  28948. ' b = rtl.eqCallback(aProc, Sub);',
  28949. ' b = rtl.eqCallback(Sub, aProc);',
  28950. ' };',
  28951. '});',
  28952. '']),
  28953. LinesToStr([ // $mod.$main
  28954. '']));
  28955. end;
  28956. procedure TTestModule.TestProcType_ReferenceToProc;
  28957. begin
  28958. StartProgram(false);
  28959. Add([
  28960. 'type',
  28961. ' TProcRef = reference to procedure(i: longint = 0);',
  28962. ' TFuncRef = reference to function(i: longint = 0): longint;',
  28963. 'var',
  28964. ' p: TProcRef;',
  28965. ' f: TFuncRef;',
  28966. 'procedure DoIt(i: longint);',
  28967. 'begin',
  28968. 'end;',
  28969. 'function GetIt(i: longint): longint;',
  28970. 'begin',
  28971. ' p:=@DoIt;',
  28972. ' f:=@GetIt;',
  28973. ' f;',
  28974. ' f();',
  28975. ' f(1);',
  28976. 'end;',
  28977. 'begin',
  28978. ' p:=@DoIt;',
  28979. ' f:=@GetIt;',
  28980. ' f;',
  28981. ' f();',
  28982. ' f(1);',
  28983. ' p:=TProcRef(f);',
  28984. '']);
  28985. ConvertProgram;
  28986. CheckSource('TestProcType_ReferenceToProc',
  28987. LinesToStr([ // statements
  28988. 'this.p = null;',
  28989. 'this.f = null;',
  28990. 'this.DoIt = function (i) {',
  28991. '};',
  28992. 'this.GetIt = function (i) {',
  28993. ' var Result = 0;',
  28994. ' $mod.p = $mod.DoIt;',
  28995. ' $mod.f = $mod.GetIt;',
  28996. ' $mod.f(0);',
  28997. ' $mod.f(0);',
  28998. ' $mod.f(1);',
  28999. ' return Result;',
  29000. '};',
  29001. '']),
  29002. LinesToStr([ // $mod.$main
  29003. '$mod.p = $mod.DoIt;',
  29004. '$mod.f = $mod.GetIt;',
  29005. '$mod.f(0);',
  29006. '$mod.f(0);',
  29007. '$mod.f(1);',
  29008. '$mod.p = $mod.f;',
  29009. '']));
  29010. end;
  29011. procedure TTestModule.TestProcType_ReferenceToMethod;
  29012. begin
  29013. StartProgram(false);
  29014. Add([
  29015. 'type',
  29016. ' TFuncRef = reference to function(i: longint = 5): longint;',
  29017. ' TObject = class',
  29018. ' function Grow(s: longint): longint;',
  29019. ' end;',
  29020. 'var',
  29021. ' f: tfuncref;',
  29022. 'function tobject.grow(s: longint): longint;',
  29023. ' function GrowSub(i: longint): longint;',
  29024. ' begin',
  29025. ' f:=@grow;',
  29026. ' f:=@growsub;',
  29027. ' end;',
  29028. 'begin',
  29029. ' f:=@grow;',
  29030. ' f:=@growsub;',
  29031. 'end;',
  29032. 'begin',
  29033. '']);
  29034. ConvertProgram;
  29035. CheckSource('TestProcType_ReferenceToMethod',
  29036. LinesToStr([ // statements
  29037. 'rtl.createClass(this, "TObject", null, function () {',
  29038. ' this.$init = function () {',
  29039. ' };',
  29040. ' this.$final = function () {',
  29041. ' };',
  29042. ' this.Grow = function (s) {',
  29043. ' var $Self = this;',
  29044. ' var Result = 0;',
  29045. ' function GrowSub(i) {',
  29046. ' var Result = 0;',
  29047. ' $mod.f = rtl.createCallback($Self, "Grow");',
  29048. ' $mod.f = GrowSub;',
  29049. ' return Result;',
  29050. ' };',
  29051. ' $mod.f = rtl.createCallback($Self, "Grow");',
  29052. ' $mod.f = GrowSub;',
  29053. ' return Result;',
  29054. ' };',
  29055. '});',
  29056. 'this.f = null;',
  29057. '']),
  29058. LinesToStr([ // $mod.$main
  29059. '']));
  29060. end;
  29061. procedure TTestModule.TestProcType_Typecast;
  29062. begin
  29063. StartProgram(false);
  29064. Add([
  29065. 'type',
  29066. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  29067. ' TEvent = procedure of object;',
  29068. ' TGetter = function:longint of object;',
  29069. ' TProcA = procedure(i: longint);',
  29070. ' TFuncB = function(i, j: longint): longint;',
  29071. 'procedure DoIt(); varargs; begin end;',
  29072. 'var',
  29073. ' Notify: tnotifyevent;',
  29074. ' Event: tevent;',
  29075. ' Getter: tgetter;',
  29076. ' ProcA: tproca;',
  29077. ' FuncB: tfuncb;',
  29078. ' p: pointer;',
  29079. 'begin',
  29080. ' notify:=tnotifyevent(event);',
  29081. ' event:=tevent(event);',
  29082. ' event:=tevent(notify);',
  29083. ' event:=tevent(getter);',
  29084. ' event:=tevent(proca);',
  29085. ' proca:=tproca(funcb);',
  29086. ' funcb:=tfuncb(funcb);',
  29087. ' funcb:=tfuncb(proca);',
  29088. ' funcb:=tfuncb(getter);',
  29089. ' proca:=tproca(p);',
  29090. ' funcb:=tfuncb(p);',
  29091. ' getter:=tgetter(p);',
  29092. ' p:=pointer(notify);',
  29093. ' p:=notify;',
  29094. ' p:=pointer(proca);',
  29095. ' p:=proca;',
  29096. ' p:=pointer(funcb);',
  29097. ' p:=funcb;',
  29098. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  29099. '']);
  29100. ConvertProgram;
  29101. CheckSource('TestProcType_Typecast',
  29102. LinesToStr([ // statements
  29103. 'this.DoIt = function () {',
  29104. '};',
  29105. 'this.Notify = null;',
  29106. 'this.Event = null;',
  29107. 'this.Getter = null;',
  29108. 'this.ProcA = null;',
  29109. 'this.FuncB = null;',
  29110. 'this.p = null;',
  29111. '']),
  29112. LinesToStr([ // $mod.$main
  29113. '$mod.Notify = $mod.Event;',
  29114. '$mod.Event = $mod.Event;',
  29115. '$mod.Event = $mod.Notify;',
  29116. '$mod.Event = $mod.Getter;',
  29117. '$mod.Event = $mod.ProcA;',
  29118. '$mod.ProcA = $mod.FuncB;',
  29119. '$mod.FuncB = $mod.FuncB;',
  29120. '$mod.FuncB = $mod.ProcA;',
  29121. '$mod.FuncB = $mod.Getter;',
  29122. '$mod.ProcA = $mod.p;',
  29123. '$mod.FuncB = $mod.p;',
  29124. '$mod.Getter = $mod.p;',
  29125. '$mod.p = $mod.Notify;',
  29126. '$mod.p = $mod.Notify;',
  29127. '$mod.p = $mod.ProcA;',
  29128. '$mod.p = $mod.ProcA;',
  29129. '$mod.p = $mod.FuncB;',
  29130. '$mod.p = $mod.FuncB;',
  29131. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  29132. '']));
  29133. end;
  29134. procedure TTestModule.TestProcType_PassProcToUntyped;
  29135. begin
  29136. StartProgram(false);
  29137. Add([
  29138. 'type',
  29139. ' TEvent = procedure of object;',
  29140. ' TFunc = function: longint;',
  29141. 'procedure DoIt(); varargs; begin end;',
  29142. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  29143. 'var',
  29144. ' Event: tevent;',
  29145. ' Func: TFunc;',
  29146. 'begin',
  29147. ' doit(event,func);',
  29148. ' dosome(event,event,event);',
  29149. ' dosome(func,func,func);',
  29150. '']);
  29151. ConvertProgram;
  29152. CheckSource('TestProcType_PassProcToUntyped',
  29153. LinesToStr([ // statements
  29154. 'this.DoIt = function () {',
  29155. '};',
  29156. 'this.DoSome = function (a, b, p) {',
  29157. '};',
  29158. 'this.Event = null;',
  29159. 'this.Func = null;',
  29160. '']),
  29161. LinesToStr([ // $mod.$main
  29162. '$mod.DoIt($mod.Event, $mod.Func);',
  29163. '$mod.DoSome($mod.Event, {',
  29164. ' p: $mod,',
  29165. ' get: function () {',
  29166. ' return this.p.Event;',
  29167. ' },',
  29168. ' set: function (v) {',
  29169. ' this.p.Event = v;',
  29170. ' }',
  29171. '}, $mod.Event);',
  29172. '$mod.DoSome($mod.Func, {',
  29173. ' p: $mod,',
  29174. ' get: function () {',
  29175. ' return this.p.Func;',
  29176. ' },',
  29177. ' set: function (v) {',
  29178. ' this.p.Func = v;',
  29179. ' }',
  29180. '}, $mod.Func);',
  29181. '']));
  29182. end;
  29183. procedure TTestModule.TestProcType_PassProcToArray;
  29184. begin
  29185. StartProgram(false);
  29186. Add([
  29187. 'type',
  29188. ' TFunc = function: longint;',
  29189. ' TArrFunc = array of TFunc;',
  29190. 'procedure DoIt(Arr: TArrFunc); begin end;',
  29191. 'function GetIt: longint; begin end;',
  29192. 'var',
  29193. ' Func: tfunc;',
  29194. 'begin',
  29195. ' doit([]);',
  29196. ' doit([@GetIt]);',
  29197. ' doit([Func]);',
  29198. '']);
  29199. ConvertProgram;
  29200. CheckSource('TestProcType_PassProcToArray',
  29201. LinesToStr([ // statements
  29202. 'this.DoIt = function (Arr) {',
  29203. '};',
  29204. 'this.GetIt = function () {',
  29205. ' var Result = 0;',
  29206. ' return Result;',
  29207. '};',
  29208. 'this.Func = null;',
  29209. '']),
  29210. LinesToStr([ // $mod.$main
  29211. '$mod.DoIt([]);',
  29212. '$mod.DoIt([$mod.GetIt]);',
  29213. '$mod.DoIt([$mod.Func]);',
  29214. '']));
  29215. end;
  29216. procedure TTestModule.TestProcType_SafeCallObjFPC;
  29217. begin
  29218. StartProgram(false);
  29219. Add([
  29220. '{$modeswitch externalclass}',
  29221. 'type',
  29222. ' TProc = reference to procedure(i: longint); safecall;',
  29223. ' TEvent = procedure(i: longint) of object; safecall;',
  29224. ' TExtA = class external name ''ExtObj''',
  29225. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  29226. ' procedure DoSome(Id: longint = 1);',
  29227. ' procedure SetOnClick(const e: TEvent);',
  29228. ' property OnClick: TEvent write SetOnClick;',
  29229. ' class procedure Fly(Id: longint = 1); static;',
  29230. ' procedure SetOnShow(const p: TProc);',
  29231. ' property OnShow: TProc write SetOnShow;',
  29232. ' end;',
  29233. 'procedure Run(i: longint = 1);',
  29234. 'begin',
  29235. 'end;',
  29236. 'var',
  29237. ' Obj: texta;',
  29238. ' e: TEvent;',
  29239. ' p: TProc;',
  29240. 'begin',
  29241. ' e:=e;',
  29242. ' e:[email protected];',
  29243. ' e:[email protected];',
  29244. ' e:=TEvent(@obj.dosome);', // no safecall
  29245. ' obj.OnClick:[email protected];',
  29246. ' obj.OnClick:[email protected];',
  29247. ' obj.setonclick(@obj.doit);',
  29248. ' obj.setonclick(@obj.dosome);',
  29249. ' p:=@Run;',
  29250. ' p:[email protected];',
  29251. ' obj.OnShow:=@Run;',
  29252. ' obj.OnShow:[email protected];',
  29253. ' obj.setOnShow(@Run);',
  29254. ' obj.setOnShow(@TExtA.Fly);',
  29255. ' with obj do begin',
  29256. ' e:=@doit;',
  29257. ' e:=@dosome;',
  29258. ' OnClick:=@doit;',
  29259. ' OnClick:=@dosome;',
  29260. ' setonclick(@doit);',
  29261. ' setonclick(@dosome);',
  29262. ' OnShow:=@Run;',
  29263. ' setOnShow(@Run);',
  29264. ' end;']);
  29265. ConvertProgram;
  29266. CheckSource('TestProcType_SafeCallObjFPC',
  29267. LinesToStr([ // statements
  29268. 'this.Run = function (i) {',
  29269. '};',
  29270. 'this.Obj = null;',
  29271. 'this.e = null;',
  29272. 'this.p = null;',
  29273. '']),
  29274. LinesToStr([ // $mod.$main
  29275. '$mod.e = $mod.e;',
  29276. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  29277. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  29278. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  29279. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  29280. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  29281. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  29282. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  29283. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  29284. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  29285. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  29286. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  29287. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  29288. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  29289. 'var $with = $mod.Obj;',
  29290. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  29291. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  29292. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  29293. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  29294. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  29295. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  29296. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  29297. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  29298. '']));
  29299. end;
  29300. procedure TTestModule.TestProcType_SafeCallDelphi;
  29301. begin
  29302. StartProgram(false);
  29303. Add([
  29304. '{$mode delphi}',
  29305. '{$modeswitch externalclass}',
  29306. 'type',
  29307. ' TProc = reference to procedure(i: longint); safecall;',
  29308. ' TEvent = procedure(i: longint) of object; safecall;',
  29309. ' TExtA = class external name ''ExtObj''',
  29310. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  29311. ' procedure DoSome(Id: longint = 1);',
  29312. ' procedure SetOnClick(const e: TEvent);',
  29313. ' property OnClick: TEvent write SetOnClick;',
  29314. ' class procedure Fly(Id: longint = 1); static;',
  29315. ' procedure SetOnShow(const p: TProc);',
  29316. ' property OnShow: TProc write SetOnShow;',
  29317. ' end;',
  29318. 'procedure Run(i: longint = 1);',
  29319. 'begin',
  29320. 'end;',
  29321. 'var',
  29322. ' Obj: texta;',
  29323. ' e: TEvent;',
  29324. ' p: TProc;',
  29325. 'begin',
  29326. ' e:=e;',
  29327. ' e:=obj.doit;',
  29328. ' e:=obj.dosome;',
  29329. ' e:=TEvent(@obj.dosome);', // no safecall
  29330. ' obj.OnClick:=obj.doit;',
  29331. ' obj.OnClick:=obj.dosome;',
  29332. ' obj.setonclick(obj.doit);',
  29333. ' obj.setonclick(obj.dosome);',
  29334. ' p:=Run;',
  29335. ' p:=TExtA.Fly;',
  29336. ' obj.OnShow:=Run;',
  29337. ' obj.OnShow:=TExtA.Fly;',
  29338. ' obj.setOnShow(Run);',
  29339. ' obj.setOnShow(TExtA.Fly);',
  29340. ' with obj do begin',
  29341. ' e:=doit;',
  29342. ' e:=dosome;',
  29343. ' OnClick:=doit;',
  29344. ' OnClick:=dosome;',
  29345. ' setonclick(doit);',
  29346. ' setonclick(dosome);',
  29347. ' OnShow:=@Run;',
  29348. ' setOnShow(@Run);',
  29349. ' end;']);
  29350. ConvertProgram;
  29351. CheckSource('TestProcType_SafeCallDelphi',
  29352. LinesToStr([ // statements
  29353. 'this.Run = function (i) {',
  29354. '};',
  29355. 'this.Obj = null;',
  29356. 'this.e = null;',
  29357. 'this.p = null;',
  29358. '']),
  29359. LinesToStr([ // $mod.$main
  29360. '$mod.e = $mod.e;',
  29361. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  29362. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  29363. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  29364. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  29365. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  29366. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  29367. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  29368. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  29369. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  29370. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  29371. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  29372. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  29373. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  29374. 'var $with = $mod.Obj;',
  29375. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  29376. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  29377. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  29378. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  29379. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  29380. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  29381. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  29382. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  29383. '']));
  29384. end;
  29385. procedure TTestModule.TestProcType_SafeCall_Arg;
  29386. begin
  29387. StartProgram(false);
  29388. Add([
  29389. 'type',
  29390. ' TSafecallProc = reference to procedure; safecall;',
  29391. 'procedure Fly(const aHandler: TSafecallProc);',
  29392. 'var',
  29393. ' P: TSafecallProc;',
  29394. 'begin',
  29395. ' P := aHandler;',
  29396. ' Fly(P);',
  29397. ' Fly(aHandler);',
  29398. 'end;',
  29399. 'begin',
  29400. ' Fly(nil);',
  29401. '']);
  29402. ConvertProgram;
  29403. CheckSource('TestProcType_SafeCall_Arg',
  29404. LinesToStr([ // statements
  29405. 'this.Fly = function (aHandler) {',
  29406. ' var P = null;',
  29407. ' P = aHandler;',
  29408. ' $mod.Fly(P);',
  29409. ' $mod.Fly(aHandler);',
  29410. '};',
  29411. '']),
  29412. LinesToStr([ // $mod.$main
  29413. '$mod.Fly(null);',
  29414. '']));
  29415. end;
  29416. procedure TTestModule.TestPointer;
  29417. begin
  29418. StartProgram(false);
  29419. Add(['type',
  29420. ' TObject = class end;',
  29421. ' TClass = class of TObject;',
  29422. ' TArrInt = array of longint;',
  29423. 'const',
  29424. ' n = nil;',
  29425. 'var',
  29426. ' v: jsvalue;',
  29427. ' Obj: tobject;',
  29428. ' C: tclass;',
  29429. ' a: tarrint;',
  29430. ' p: Pointer = nil;',
  29431. ' s: string;',
  29432. 'begin',
  29433. ' p:=p;',
  29434. ' p:=nil;',
  29435. ' if p=nil then;',
  29436. ' if nil=p then;',
  29437. ' if Assigned(p) then;',
  29438. ' p:=Pointer(v);',
  29439. ' p:=obj;',
  29440. ' p:=c;',
  29441. ' p:=a;',
  29442. ' p:=tobject;',
  29443. ' obj:=TObject(p);',
  29444. ' c:=TClass(p);',
  29445. ' a:=TArrInt(p);',
  29446. ' p:=n;',
  29447. ' p:=Pointer(a);',
  29448. ' p:=pointer(s);',
  29449. ' s:=string(p);',
  29450. '']);
  29451. ConvertProgram;
  29452. CheckSource('TestPointer',
  29453. LinesToStr([ // statements
  29454. 'rtl.createClass(this, "TObject", null, function () {',
  29455. ' this.$init = function () {',
  29456. ' };',
  29457. ' this.$final = function () {',
  29458. ' };',
  29459. '});',
  29460. 'this.n = null;',
  29461. 'this.v = undefined;',
  29462. 'this.Obj = null;',
  29463. 'this.C = null;',
  29464. 'this.a = [];',
  29465. 'this.p = null;',
  29466. 'this.s = "";',
  29467. '']),
  29468. LinesToStr([ // $mod.$main
  29469. '$mod.p = $mod.p;',
  29470. '$mod.p = null;',
  29471. 'if ($mod.p === null) ;',
  29472. 'if (null === $mod.p) ;',
  29473. 'if ($mod.p != null) ;',
  29474. '$mod.p = $mod.v;',
  29475. '$mod.p = $mod.Obj;',
  29476. '$mod.p = $mod.C;',
  29477. '$mod.p = $mod.a;',
  29478. '$mod.p = $mod.TObject;',
  29479. '$mod.Obj = $mod.p;',
  29480. '$mod.C = $mod.p;',
  29481. '$mod.a = $mod.p;',
  29482. '$mod.p = null;',
  29483. '$mod.p = $mod.a;',
  29484. '$mod.p = $mod.s;',
  29485. '$mod.s = $mod.p;',
  29486. '']));
  29487. end;
  29488. procedure TTestModule.TestPointer_Proc;
  29489. begin
  29490. StartProgram(false);
  29491. Add('type');
  29492. Add(' TObject = class');
  29493. Add(' procedure DoIt; virtual; abstract;');
  29494. Add(' end;');
  29495. Add('procedure DoSome; begin end;');
  29496. Add('var');
  29497. Add(' o: TObject;');
  29498. Add(' p: Pointer;');
  29499. Add('begin');
  29500. Add(' p:=@DoSome;');
  29501. Add(' p:[email protected];');
  29502. ConvertProgram;
  29503. CheckSource('TestPointer_Proc',
  29504. LinesToStr([ // statements
  29505. 'rtl.createClass(this, "TObject", null, function () {',
  29506. ' this.$init = function () {',
  29507. ' };',
  29508. ' this.$final = function () {',
  29509. ' };',
  29510. '});',
  29511. 'this.DoSome = function () {',
  29512. '};',
  29513. 'this.o = null;',
  29514. 'this.p = null;',
  29515. '']),
  29516. LinesToStr([ // $mod.$main
  29517. '$mod.p = $mod.DoSome;',
  29518. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  29519. '']));
  29520. end;
  29521. procedure TTestModule.TestPointer_AssignRecordFail;
  29522. begin
  29523. StartProgram(false);
  29524. Add('type');
  29525. Add(' TRec = record end;');
  29526. Add('var');
  29527. Add(' p: Pointer;');
  29528. Add(' r: TRec;');
  29529. Add('begin');
  29530. Add(' p:=r;');
  29531. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  29532. nIncompatibleTypesGotExpected);
  29533. ConvertProgram;
  29534. end;
  29535. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  29536. begin
  29537. StartProgram(false);
  29538. Add('type');
  29539. Add(' TArr = array[boolean] of longint;');
  29540. Add('var');
  29541. Add(' p: Pointer;');
  29542. Add(' a: TArr;');
  29543. Add('begin');
  29544. Add(' p:=a;');
  29545. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  29546. nIncompatibleTypesGotExpected);
  29547. ConvertProgram;
  29548. end;
  29549. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  29550. begin
  29551. StartProgram(false);
  29552. Add([
  29553. 'procedure DoIt(args: array of jsvalue); begin end;',
  29554. 'procedure DoAll; varargs; begin end;',
  29555. 'var',
  29556. ' v: jsvalue;',
  29557. 'begin',
  29558. ' DoIt([pointer(v)]);',
  29559. ' DoAll(pointer(v));',
  29560. '']);
  29561. ConvertProgram;
  29562. CheckSource('TestPointer_TypeCastJSValueToPointer',
  29563. LinesToStr([ // statements
  29564. 'this.DoIt = function (args) {',
  29565. '};',
  29566. 'this.DoAll = function () {',
  29567. '};',
  29568. 'this.v = undefined;',
  29569. '']),
  29570. LinesToStr([ // $mod.$main
  29571. '$mod.DoIt([$mod.v]);',
  29572. '$mod.DoAll($mod.v);',
  29573. '']));
  29574. end;
  29575. procedure TTestModule.TestPointer_NonRecordFail;
  29576. begin
  29577. StartProgram(false);
  29578. Add([
  29579. 'type',
  29580. ' p = ^longint;',
  29581. 'begin',
  29582. '']);
  29583. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  29584. ConvertProgram;
  29585. end;
  29586. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  29587. begin
  29588. StartProgram(false);
  29589. Add([
  29590. 'procedure DoIt(p: ^longint); begin end;',
  29591. 'begin',
  29592. '']);
  29593. SetExpectedParserError('Parameters or result types cannot contain local type definitions. Use a separate type definition in a type block. at token "^" in file test1.pp at line 3 column 19',nParserParamsOrResultTypesNoLocalTypeDefs);
  29594. ConvertProgram;
  29595. end;
  29596. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  29597. begin
  29598. StartProgram(false);
  29599. Add([
  29600. 'var p: ^longint;',
  29601. 'begin',
  29602. '']);
  29603. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  29604. ConvertProgram;
  29605. end;
  29606. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  29607. begin
  29608. StartProgram(false);
  29609. Add([
  29610. 'function DoIt: ^longint; begin end;',
  29611. 'begin',
  29612. '']);
  29613. SetExpectedParserError('Parameters or result types cannot contain local type definitions. Use a separate type definition in a type block. at token "^" in file test1.pp at line 3 column 16',nParserParamsOrResultTypesNoLocalTypeDefs);
  29614. ConvertProgram;
  29615. end;
  29616. procedure TTestModule.TestPointer_AddrOperatorFail;
  29617. begin
  29618. StartProgram(false);
  29619. Add([
  29620. 'var i: longint;',
  29621. 'begin',
  29622. ' if @i=nil then ;',
  29623. '']);
  29624. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  29625. ConvertProgram;
  29626. end;
  29627. procedure TTestModule.TestPointer_ArrayParamsFail;
  29628. begin
  29629. StartProgram(false);
  29630. Add([
  29631. 'var',
  29632. ' p: Pointer;',
  29633. 'begin',
  29634. ' p:=p[1];',
  29635. '']);
  29636. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  29637. ConvertProgram;
  29638. end;
  29639. procedure TTestModule.TestPointer_PointerAddFail;
  29640. begin
  29641. StartProgram(false);
  29642. Add([
  29643. 'var',
  29644. ' p: Pointer;',
  29645. 'begin',
  29646. ' p:=p+1;',
  29647. '']);
  29648. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  29649. ConvertProgram;
  29650. end;
  29651. procedure TTestModule.TestPointer_IncPointerFail;
  29652. begin
  29653. StartProgram(false);
  29654. Add([
  29655. 'var',
  29656. ' p: Pointer;',
  29657. 'begin',
  29658. ' inc(p,1);',
  29659. '']);
  29660. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "Pointer", expected "integer"',
  29661. nIncompatibleTypeArgNo);
  29662. ConvertProgram;
  29663. end;
  29664. procedure TTestModule.TestPointer_Record;
  29665. begin
  29666. StartProgram(false);
  29667. Add([
  29668. 'type',
  29669. ' TRec = record x: longint; end;',
  29670. ' PRec = ^TRec;',
  29671. 'var',
  29672. ' r: TRec;',
  29673. ' p: PRec;',
  29674. ' q: ^TRec;',
  29675. ' Ptr: pointer;',
  29676. 'begin',
  29677. ' new(p);',
  29678. ' p:=@r;',
  29679. ' r:=p^;',
  29680. ' r.x:=p^.x;',
  29681. ' p^.x:=r.x;',
  29682. ' if p^.x=3 then ;',
  29683. ' if 4=p^.x then ;',
  29684. ' dispose(p);',
  29685. ' new(q);',
  29686. ' dispose(q);',
  29687. ' Ptr:=p;',
  29688. ' p:=PRec(ptr);',
  29689. '']);
  29690. ConvertProgram;
  29691. CheckSource('TestPointer_Record',
  29692. LinesToStr([ // statements
  29693. 'rtl.recNewT(this, "TRec", function () {',
  29694. ' this.x = 0;',
  29695. ' this.$eq = function (b) {',
  29696. ' return this.x === b.x;',
  29697. ' };',
  29698. ' this.$assign = function (s) {',
  29699. ' this.x = s.x;',
  29700. ' return this;',
  29701. ' };',
  29702. '});',
  29703. 'this.r = this.TRec.$new();',
  29704. 'this.p = null;',
  29705. 'this.q = null;',
  29706. 'this.Ptr = null;',
  29707. '']),
  29708. LinesToStr([ // $mod.$main
  29709. '$mod.p = $mod.TRec.$new();',
  29710. '$mod.p = $mod.r;',
  29711. '$mod.r.$assign($mod.p);',
  29712. '$mod.r.x = $mod.p.x;',
  29713. '$mod.p.x = $mod.r.x;',
  29714. 'if ($mod.p.x === 3) ;',
  29715. 'if (4 === $mod.p.x) ;',
  29716. '$mod.p = null;',
  29717. '$mod.q = $mod.TRec.$new();',
  29718. '$mod.q = null;',
  29719. '$mod.Ptr = $mod.p;',
  29720. '$mod.p = $mod.Ptr;',
  29721. '']));
  29722. end;
  29723. procedure TTestModule.TestPointer_RecordArg;
  29724. begin
  29725. StartProgram(false);
  29726. Add([
  29727. '{$modeswitch autoderef}',
  29728. 'type',
  29729. ' TRec = record x: longint; end;',
  29730. ' PRec = ^TRec;',
  29731. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  29732. 'begin',
  29733. ' a.x:=a.x;',
  29734. ' a^.x:=a^.x;',
  29735. ' with a^ do',
  29736. ' x:=x;',
  29737. 'end;',
  29738. 'function GetIt(p: PRec): PRec;',
  29739. 'begin',
  29740. ' p.x:=p.x;',
  29741. ' p^.x:=p^.x;',
  29742. ' with p^ do',
  29743. ' x:=x;',
  29744. 'end;',
  29745. 'var',
  29746. ' r: TRec;',
  29747. ' p: PRec;',
  29748. 'begin',
  29749. ' p:=GetIt(p);',
  29750. ' p^:=GetIt(@r)^;',
  29751. ' DoIt(p,p,p);',
  29752. ' DoIt(@r,p,p);',
  29753. '']);
  29754. ConvertProgram;
  29755. CheckSource('TestPointer_RecordArg',
  29756. LinesToStr([ // statements
  29757. 'rtl.recNewT(this, "TRec", function () {',
  29758. ' this.x = 0;',
  29759. ' this.$eq = function (b) {',
  29760. ' return this.x === b.x;',
  29761. ' };',
  29762. ' this.$assign = function (s) {',
  29763. ' this.x = s.x;',
  29764. ' return this;',
  29765. ' };',
  29766. '});',
  29767. 'this.DoIt = function (a, b, c) {',
  29768. ' var Result = $mod.TRec.$new();',
  29769. ' a.x = a.x;',
  29770. ' a.x = a.x;',
  29771. ' a.x = a.x;',
  29772. ' return Result;',
  29773. '};',
  29774. 'this.GetIt = function (p) {',
  29775. ' var Result = null;',
  29776. ' p.x = p.x;',
  29777. ' p.x = p.x;',
  29778. ' p.x = p.x;',
  29779. ' return Result;',
  29780. '};',
  29781. 'this.r = this.TRec.$new();',
  29782. 'this.p = null;',
  29783. '']),
  29784. LinesToStr([ // $mod.$main
  29785. '$mod.p = $mod.GetIt($mod.p);',
  29786. '$mod.p.$assign($mod.GetIt($mod.r));',
  29787. '$mod.DoIt($mod.p, {',
  29788. ' p: $mod,',
  29789. ' get: function () {',
  29790. ' return this.p.p;',
  29791. ' },',
  29792. ' set: function (v) {',
  29793. ' this.p.p = v;',
  29794. ' }',
  29795. '}, {',
  29796. ' p: $mod,',
  29797. ' get: function () {',
  29798. ' return this.p.p;',
  29799. ' },',
  29800. ' set: function (v) {',
  29801. ' this.p.p = v;',
  29802. ' }',
  29803. '});',
  29804. '$mod.DoIt($mod.r, {',
  29805. ' p: $mod,',
  29806. ' get: function () {',
  29807. ' return this.p.p;',
  29808. ' },',
  29809. ' set: function (v) {',
  29810. ' this.p.p = v;',
  29811. ' }',
  29812. '}, {',
  29813. ' p: $mod,',
  29814. ' get: function () {',
  29815. ' return this.p.p;',
  29816. ' },',
  29817. ' set: function (v) {',
  29818. ' this.p.p = v;',
  29819. ' }',
  29820. '});',
  29821. '']));
  29822. end;
  29823. procedure TTestModule.TestJSValue_AssignToJSValue;
  29824. begin
  29825. StartProgram(false);
  29826. Add('var');
  29827. Add(' v: jsvalue;');
  29828. Add(' i: longint;');
  29829. Add(' s: string;');
  29830. Add(' b: boolean;');
  29831. Add(' d: double;');
  29832. Add(' p: pointer;');
  29833. Add('begin');
  29834. Add(' v:=v;');
  29835. Add(' v:=1;');
  29836. Add(' v:=i;');
  29837. Add(' v:='''';');
  29838. Add(' v:=''c'';');
  29839. Add(' v:=''foo'';');
  29840. Add(' v:=s;');
  29841. Add(' v:=false;');
  29842. Add(' v:=true;');
  29843. Add(' v:=b;');
  29844. Add(' v:=0.1;');
  29845. Add(' v:=d;');
  29846. Add(' v:=nil;');
  29847. Add(' v:=p;');
  29848. ConvertProgram;
  29849. CheckSource('TestJSValue_AssignToJSValue',
  29850. LinesToStr([ // statements
  29851. 'this.v = undefined;',
  29852. 'this.i = 0;',
  29853. 'this.s = "";',
  29854. 'this.b = false;',
  29855. 'this.d = 0.0;',
  29856. 'this.p = null;',
  29857. '']),
  29858. LinesToStr([ // $mod.$main
  29859. '$mod.v = $mod.v;',
  29860. '$mod.v = 1;',
  29861. '$mod.v = $mod.i;',
  29862. '$mod.v = "";',
  29863. '$mod.v = "c";',
  29864. '$mod.v = "foo";',
  29865. '$mod.v = $mod.s;',
  29866. '$mod.v = false;',
  29867. '$mod.v = true;',
  29868. '$mod.v = $mod.b;',
  29869. '$mod.v = 0.1;',
  29870. '$mod.v = $mod.d;',
  29871. '$mod.v = null;',
  29872. '$mod.v = $mod.p;',
  29873. '']));
  29874. end;
  29875. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  29876. begin
  29877. StartProgram(false);
  29878. Add('type');
  29879. Add(' integer = longint;');
  29880. Add(' TYesNo = boolean;');
  29881. Add(' TFloat = double;');
  29882. Add(' TCaption = string;');
  29883. Add(' TChar = char;');
  29884. Add('var');
  29885. Add(' v: jsvalue;');
  29886. Add(' i: integer;');
  29887. Add(' s: TCaption;');
  29888. Add(' b: TYesNo;');
  29889. Add(' d: TFloat;');
  29890. Add(' c: char;');
  29891. Add('begin');
  29892. Add(' i:=longint(v);');
  29893. Add(' i:=integer(v);');
  29894. Add(' s:=string(v);');
  29895. Add(' s:=TCaption(v);');
  29896. Add(' b:=boolean(v);');
  29897. Add(' b:=TYesNo(v);');
  29898. Add(' d:=double(v);');
  29899. Add(' d:=TFloat(v);');
  29900. Add(' c:=char(v);');
  29901. Add(' c:=TChar(v);');
  29902. ConvertProgram;
  29903. CheckSource('TestJSValue_TypeCastToBaseType',
  29904. LinesToStr([ // statements
  29905. 'this.v = undefined;',
  29906. 'this.i = 0;',
  29907. 'this.s = "";',
  29908. 'this.b = false;',
  29909. 'this.d = 0.0;',
  29910. 'this.c = "\x00";',
  29911. '']),
  29912. LinesToStr([ // $mod.$main
  29913. '$mod.i = rtl.trunc($mod.v);',
  29914. '$mod.i = rtl.trunc($mod.v);',
  29915. '$mod.s = "" + $mod.v;',
  29916. '$mod.s = "" + $mod.v;',
  29917. '$mod.b = !($mod.v == false);',
  29918. '$mod.b = !($mod.v == false);',
  29919. '$mod.d = rtl.getNumber($mod.v);',
  29920. '$mod.d = rtl.getNumber($mod.v);',
  29921. '$mod.c = rtl.getChar($mod.v);',
  29922. '$mod.c = rtl.getChar($mod.v);',
  29923. '']));
  29924. end;
  29925. procedure TTestModule.TestJSValue_TypecastToJSValue;
  29926. begin
  29927. StartProgram(false);
  29928. Add([
  29929. 'type',
  29930. ' TArr = array of word;',
  29931. ' TRec = record end;',
  29932. ' TSet = set of boolean;',
  29933. 'procedure Fly(v: jsvalue);',
  29934. 'begin',
  29935. 'end;',
  29936. 'var',
  29937. ' a: TArr;',
  29938. ' r: TRec;',
  29939. ' s: TSet;',
  29940. 'begin',
  29941. ' Fly(jsvalue(a));',
  29942. ' Fly(jsvalue(r));',
  29943. ' Fly(jsvalue(s));',
  29944. '']);
  29945. ConvertProgram;
  29946. CheckSource('TestJSValue_TypecastToJSValue',
  29947. LinesToStr([ // statements
  29948. 'rtl.recNewT(this, "TRec", function () {',
  29949. ' this.$eq = function (b) {',
  29950. ' return true;',
  29951. ' };',
  29952. ' this.$assign = function (s) {',
  29953. ' return this;',
  29954. ' };',
  29955. '});',
  29956. 'this.Fly = function (v) {',
  29957. '};',
  29958. 'this.a = [];',
  29959. 'this.r = this.TRec.$new();',
  29960. 'this.s = {};',
  29961. '']),
  29962. LinesToStr([ // $mod.$main
  29963. '$mod.Fly($mod.a);',
  29964. '$mod.Fly($mod.r);',
  29965. '$mod.Fly($mod.s);',
  29966. '']));
  29967. end;
  29968. procedure TTestModule.TestJSValue_Equal;
  29969. begin
  29970. StartProgram(false);
  29971. Add('type');
  29972. Add(' integer = longint;');
  29973. Add(' TYesNo = boolean;');
  29974. Add(' TFloat = double;');
  29975. Add(' TCaption = string;');
  29976. Add(' TChar = char;');
  29977. Add(' TMulti = JSValue;');
  29978. Add('var');
  29979. Add(' v: jsvalue;');
  29980. Add(' i: integer;');
  29981. Add(' s: TCaption;');
  29982. Add(' b: TYesNo;');
  29983. Add(' d: TFloat;');
  29984. Add(' c: char;');
  29985. Add(' m: TMulti;');
  29986. Add('begin');
  29987. Add(' b:=v=v;');
  29988. Add(' b:=v<>v;');
  29989. Add(' b:=v=1;');
  29990. Add(' b:=v<>1;');
  29991. Add(' b:=2=v;');
  29992. Add(' b:=2<>v;');
  29993. Add(' b:=v=i;');
  29994. Add(' b:=i=v;');
  29995. Add(' b:=v=nil;');
  29996. Add(' b:=nil=v;');
  29997. Add(' b:=v=false;');
  29998. Add(' b:=true=v;');
  29999. Add(' b:=v=b;');
  30000. Add(' b:=b=v;');
  30001. Add(' b:=v=s;');
  30002. Add(' b:=s=v;');
  30003. Add(' b:=v=''foo'';');
  30004. Add(' b:=''''=v;');
  30005. Add(' b:=v=d;');
  30006. Add(' b:=d=v;');
  30007. Add(' b:=v=3.4;');
  30008. Add(' b:=5.6=v;');
  30009. Add(' b:=v=c;');
  30010. Add(' b:=c=v;');
  30011. Add(' b:=m=m;');
  30012. Add(' b:=v=m;');
  30013. Add(' b:=m=v;');
  30014. ConvertProgram;
  30015. CheckSource('TestJSValue_Equal',
  30016. LinesToStr([ // statements
  30017. 'this.v = undefined;',
  30018. 'this.i = 0;',
  30019. 'this.s = "";',
  30020. 'this.b = false;',
  30021. 'this.d = 0.0;',
  30022. 'this.c = "\x00";',
  30023. 'this.m = undefined;',
  30024. '']),
  30025. LinesToStr([ // $mod.$main
  30026. '$mod.b = $mod.v == $mod.v;',
  30027. '$mod.b = $mod.v != $mod.v;',
  30028. '$mod.b = $mod.v == 1;',
  30029. '$mod.b = $mod.v != 1;',
  30030. '$mod.b = 2 == $mod.v;',
  30031. '$mod.b = 2 != $mod.v;',
  30032. '$mod.b = $mod.v == $mod.i;',
  30033. '$mod.b = $mod.i == $mod.v;',
  30034. '$mod.b = $mod.v == null;',
  30035. '$mod.b = null == $mod.v;',
  30036. '$mod.b = $mod.v == false;',
  30037. '$mod.b = true == $mod.v;',
  30038. '$mod.b = $mod.v == $mod.b;',
  30039. '$mod.b = $mod.b == $mod.v;',
  30040. '$mod.b = $mod.v == $mod.s;',
  30041. '$mod.b = $mod.s == $mod.v;',
  30042. '$mod.b = $mod.v == "foo";',
  30043. '$mod.b = "" == $mod.v;',
  30044. '$mod.b = $mod.v == $mod.d;',
  30045. '$mod.b = $mod.d == $mod.v;',
  30046. '$mod.b = $mod.v == 3.4;',
  30047. '$mod.b = 5.6 == $mod.v;',
  30048. '$mod.b = $mod.v == $mod.c;',
  30049. '$mod.b = $mod.c == $mod.v;',
  30050. '$mod.b = $mod.m == $mod.m;',
  30051. '$mod.b = $mod.v == $mod.m;',
  30052. '$mod.b = $mod.m == $mod.v;',
  30053. '']));
  30054. end;
  30055. procedure TTestModule.TestJSValue_If;
  30056. begin
  30057. StartProgram(false);
  30058. Add([
  30059. 'procedure Fly(var u);',
  30060. 'begin',
  30061. ' if jsvalue(u) then ;',
  30062. 'end;',
  30063. 'var',
  30064. ' v: jsvalue;',
  30065. 'begin',
  30066. ' if v then ;',
  30067. ' while v do ;',
  30068. ' repeat until v;',
  30069. '']);
  30070. ConvertProgram;
  30071. CheckSource('TestJSValue_If',
  30072. LinesToStr([ // statements
  30073. 'this.Fly = function (u) {',
  30074. ' if (u.get()) ;',
  30075. '};',
  30076. 'this.v = undefined;',
  30077. '']),
  30078. LinesToStr([ // $mod.$main
  30079. 'if ($mod.v) ;',
  30080. 'while($mod.v){',
  30081. '};',
  30082. 'do{',
  30083. '} while(!$mod.v);',
  30084. '']));
  30085. end;
  30086. procedure TTestModule.TestJSValue_Not;
  30087. begin
  30088. StartProgram(false);
  30089. Add([
  30090. 'var',
  30091. ' v: jsvalue;',
  30092. ' b: boolean;',
  30093. 'begin',
  30094. ' b:=not v;',
  30095. ' if not v then ;',
  30096. ' while not v do ;',
  30097. ' repeat until not v;',
  30098. '']);
  30099. ConvertProgram;
  30100. CheckSource('TestJSValue_If',
  30101. LinesToStr([ // statements
  30102. 'this.v = undefined;',
  30103. 'this.b = false;',
  30104. '']),
  30105. LinesToStr([ // $mod.$main
  30106. '$mod.b=!$mod.v;',
  30107. 'if (!$mod.v) ;',
  30108. 'while(!$mod.v){',
  30109. '};',
  30110. 'do{',
  30111. '} while($mod.v);',
  30112. '']));
  30113. end;
  30114. procedure TTestModule.TestJSValue_Enum;
  30115. begin
  30116. StartProgram(false);
  30117. Add('type');
  30118. Add(' TColor = (red, blue);');
  30119. Add(' TRedBlue = TColor;');
  30120. Add('var');
  30121. Add(' v: jsvalue;');
  30122. Add(' e: TColor;');
  30123. Add('begin');
  30124. Add(' v:=e;');
  30125. Add(' v:=TColor(e);');
  30126. Add(' v:=TRedBlue(e);');
  30127. Add(' e:=TColor(v);');
  30128. Add(' e:=TRedBlue(v);');
  30129. ConvertProgram;
  30130. CheckSource('TestJSValue_Enum',
  30131. LinesToStr([ // statements
  30132. 'this.TColor = {',
  30133. ' "0": "red",',
  30134. ' red: 0,',
  30135. ' "1": "blue",',
  30136. ' blue: 1',
  30137. '};',
  30138. 'this.v = undefined;',
  30139. 'this.e = 0;',
  30140. '']),
  30141. LinesToStr([ // $mod.$main
  30142. '$mod.v = $mod.e;',
  30143. '$mod.v = $mod.e;',
  30144. '$mod.v = $mod.e;',
  30145. '$mod.e = $mod.v;',
  30146. '$mod.e = $mod.v;',
  30147. '']));
  30148. end;
  30149. procedure TTestModule.TestJSValue_ClassInstance;
  30150. begin
  30151. StartProgram(false);
  30152. Add([
  30153. 'type',
  30154. ' TObject = class',
  30155. ' end;',
  30156. ' TBirdObject = TObject;',
  30157. 'var',
  30158. ' v: jsvalue;',
  30159. ' o: TObject;',
  30160. 'begin',
  30161. ' v:=o;',
  30162. ' v:=TObject(o);',
  30163. ' v:=TBirdObject(o);',
  30164. ' o:=TObject(v);',
  30165. ' o:=TBirdObject(v);',
  30166. ' if v is TObject then ;',
  30167. '']);
  30168. ConvertProgram;
  30169. CheckSource('TestJSValue_ClassInstance',
  30170. LinesToStr([ // statements
  30171. 'rtl.createClass(this, "TObject", null, function () {',
  30172. ' this.$init = function () {',
  30173. ' };',
  30174. ' this.$final = function () {',
  30175. ' };',
  30176. '});',
  30177. 'this.v = undefined;',
  30178. 'this.o = null;',
  30179. '']),
  30180. LinesToStr([ // $mod.$main
  30181. '$mod.v = $mod.o;',
  30182. '$mod.v = $mod.o;',
  30183. '$mod.v = $mod.o;',
  30184. '$mod.o = rtl.getObject($mod.v);',
  30185. '$mod.o = rtl.getObject($mod.v);',
  30186. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  30187. '']));
  30188. end;
  30189. procedure TTestModule.TestJSValue_ClassOf;
  30190. begin
  30191. StartProgram(false);
  30192. Add([
  30193. 'type',
  30194. ' TClass = class of TObject;',
  30195. ' TObject = class',
  30196. ' end;',
  30197. ' TBirds = class of TBird;',
  30198. ' TBird = class(TObject) end;',
  30199. 'var',
  30200. ' v: jsvalue;',
  30201. ' c: TClass;',
  30202. 'begin',
  30203. ' v:=c;',
  30204. ' v:=TObject;',
  30205. ' v:=TClass(c);',
  30206. ' v:=TBirds(c);',
  30207. ' c:=TClass(v);',
  30208. ' c:=TBirds(v);',
  30209. ' if v is TClass then ;',
  30210. '']);
  30211. ConvertProgram;
  30212. CheckSource('TestJSValue_ClassOf',
  30213. LinesToStr([ // statements
  30214. 'rtl.createClass(this, "TObject", null, function () {',
  30215. ' this.$init = function () {',
  30216. ' };',
  30217. ' this.$final = function () {',
  30218. ' };',
  30219. '});',
  30220. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30221. '});',
  30222. 'this.v = undefined;',
  30223. 'this.c = null;',
  30224. '']),
  30225. LinesToStr([ // $mod.$main
  30226. '$mod.v = $mod.c;',
  30227. '$mod.v = $mod.TObject;',
  30228. '$mod.v = $mod.c;',
  30229. '$mod.v = $mod.c;',
  30230. '$mod.c = rtl.getObject($mod.v);',
  30231. '$mod.c = rtl.getObject($mod.v);',
  30232. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  30233. '']));
  30234. end;
  30235. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  30236. begin
  30237. StartProgram(false);
  30238. Add([
  30239. 'type',
  30240. ' integer = longint;',
  30241. ' TArray = array of JSValue;',
  30242. ' TArrgh = tarray;',
  30243. ' TArrInt = array of integer;',
  30244. 'var',
  30245. ' v: jsvalue;',
  30246. ' TheArray: tarray = (1,''2'');',
  30247. ' Arr: tarrgh;',
  30248. ' i: integer;',
  30249. ' ArrInt: tarrint;',
  30250. 'begin',
  30251. ' arr:=thearray;',
  30252. ' thearray:=arr;',
  30253. ' setlength(arr,2);',
  30254. ' setlength(thearray,3);',
  30255. ' arr[4]:=v;',
  30256. ' arr[5]:=length(thearray);',
  30257. ' arr[6]:=nil;',
  30258. ' arr[7]:=thearray[8];',
  30259. ' arr[low(arr)]:=high(thearray);',
  30260. ' arr:=arrint;',
  30261. ' arrInt:=tarrint(arr);',
  30262. ' if TheArray = nil then ;',
  30263. ' if nil = TheArray then ;',
  30264. ' if TheArray <> nil then ;',
  30265. ' if nil <> TheArray then ;',
  30266. '']);
  30267. ConvertProgram;
  30268. CheckSource('TestJSValue_ArrayOfJSValue',
  30269. LinesToStr([ // statements
  30270. 'this.v = undefined;',
  30271. 'this.TheArray = [1, "2"];',
  30272. 'this.Arr = [];',
  30273. 'this.i = 0;',
  30274. 'this.ArrInt = [];',
  30275. '']),
  30276. LinesToStr([ // $mod.$main
  30277. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  30278. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  30279. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  30280. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  30281. '$mod.Arr[4] = $mod.v;',
  30282. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  30283. '$mod.Arr[6] = null;',
  30284. '$mod.Arr[7] = $mod.TheArray[8];',
  30285. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  30286. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  30287. '$mod.ArrInt = $mod.Arr;',
  30288. 'if (rtl.length($mod.TheArray) === 0) ;',
  30289. 'if (rtl.length($mod.TheArray) === 0) ;',
  30290. 'if (rtl.length($mod.TheArray) > 0) ;',
  30291. 'if (rtl.length($mod.TheArray) > 0) ;',
  30292. '']));
  30293. end;
  30294. procedure TTestModule.TestJSValue_ArrayLit;
  30295. begin
  30296. StartProgram(false);
  30297. Add([
  30298. 'type',
  30299. ' TFlag = (big,small);',
  30300. ' TArray = array of JSValue;',
  30301. ' TObject = class end;',
  30302. ' TClass = class of TObject;',
  30303. 'var',
  30304. ' v: jsvalue;',
  30305. ' a: TArray;',
  30306. ' o: TObject;',
  30307. 'begin',
  30308. ' a:=[];',
  30309. ' a:=[1];',
  30310. ' a:=[1,2];',
  30311. ' a:=[big];',
  30312. ' a:=[1,big];',
  30313. ' a:=[o,nil];',
  30314. '']);
  30315. ConvertProgram;
  30316. CheckSource('TestJSValue_ArrayLit',
  30317. LinesToStr([ // statements
  30318. 'this.TFlag = {',
  30319. ' "0": "big",',
  30320. ' big: 0,',
  30321. ' "1": "small",',
  30322. ' small: 1',
  30323. '};',
  30324. 'rtl.createClass(this, "TObject", null, function () {',
  30325. ' this.$init = function () {',
  30326. ' };',
  30327. ' this.$final = function () {',
  30328. ' };',
  30329. '});',
  30330. 'this.v = undefined;',
  30331. 'this.a = [];',
  30332. 'this.o = null;',
  30333. '']),
  30334. LinesToStr([ // $mod.$main
  30335. '$mod.a = [];',
  30336. '$mod.a = [1];',
  30337. '$mod.a = [1, 2];',
  30338. '$mod.a = [$mod.TFlag.big];',
  30339. '$mod.a = [1, $mod.TFlag.big];',
  30340. '$mod.a = [$mod.o, null];',
  30341. '']));
  30342. end;
  30343. procedure TTestModule.TestJSValue_Params;
  30344. begin
  30345. StartProgram(false);
  30346. Add('type');
  30347. Add(' integer = longint;');
  30348. Add(' TYesNo = boolean;');
  30349. Add(' TFloat = double;');
  30350. Add(' TCaption = string;');
  30351. Add(' TChar = char;');
  30352. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  30353. Add('var');
  30354. Add(' l: jsvalue;');
  30355. Add('begin');
  30356. Add(' a:=a;');
  30357. Add(' l:=b;');
  30358. Add(' c:=c;');
  30359. Add(' d:=d;');
  30360. Add(' Result:=l;');
  30361. Add('end;');
  30362. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  30363. Add('var');
  30364. Add(' v: jsvalue;');
  30365. Add(' i: integer;');
  30366. Add(' b: TYesNo;');
  30367. Add(' d: TFloat;');
  30368. Add(' s: TCaption;');
  30369. Add(' c: TChar;');
  30370. Add('begin');
  30371. Add(' v:=doit(v,v,v,v);');
  30372. Add(' i:=integer(dosome(i,i));');
  30373. Add(' b:=TYesNo(dosome(b,b));');
  30374. Add(' d:=TFloat(dosome(d,d));');
  30375. Add(' s:=TCaption(dosome(s,s));');
  30376. Add(' c:=TChar(dosome(c,c));');
  30377. ConvertProgram;
  30378. CheckSource('TestJSValue_Params',
  30379. LinesToStr([ // statements
  30380. 'this.DoIt = function (a, b, c, d) {',
  30381. ' var Result = undefined;',
  30382. ' var l = undefined;',
  30383. ' a = a;',
  30384. ' l = b;',
  30385. ' c.set(c.get());',
  30386. ' d.set(d.get());',
  30387. ' Result = l;',
  30388. ' return Result;',
  30389. '};',
  30390. 'this.DoSome = function (a, b) {',
  30391. ' var Result = undefined;',
  30392. ' return Result;',
  30393. '};',
  30394. 'this.v = undefined;',
  30395. 'this.i = 0;',
  30396. 'this.b = false;',
  30397. 'this.d = 0.0;',
  30398. 'this.s = "";',
  30399. 'this.c = "\x00";',
  30400. '']),
  30401. LinesToStr([ // $mod.$main
  30402. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  30403. ' p: $mod,',
  30404. ' get: function () {',
  30405. ' return this.p.v;',
  30406. ' },',
  30407. ' set: function (v) {',
  30408. ' this.p.v = v;',
  30409. ' }',
  30410. '}, {',
  30411. ' p: $mod,',
  30412. ' get: function () {',
  30413. ' return this.p.v;',
  30414. ' },',
  30415. ' set: function (v) {',
  30416. ' this.p.v = v;',
  30417. ' }',
  30418. '});',
  30419. '$mod.i = rtl.trunc($mod.DoSome($mod.i, $mod.i));',
  30420. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  30421. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  30422. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  30423. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  30424. '']));
  30425. end;
  30426. procedure TTestModule.TestJSValue_UntypedParam;
  30427. begin
  30428. StartProgram(false);
  30429. Add('function DoIt(const a; var b; out c): jsvalue;');
  30430. Add('begin');
  30431. Add(' Result:=a;');
  30432. Add(' Result:=b;');
  30433. Add(' Result:=c;');
  30434. Add(' b:=Result;');
  30435. Add(' c:=Result;');
  30436. Add('end;');
  30437. Add('var i: longint;');
  30438. Add('begin');
  30439. Add(' doit(i,i,i);');
  30440. ConvertProgram;
  30441. CheckSource('TestJSValue_UntypedParam',
  30442. LinesToStr([ // statements
  30443. 'this.DoIt = function (a, b, c) {',
  30444. ' var Result = undefined;',
  30445. ' Result = a;',
  30446. ' Result = b.get();',
  30447. ' Result = c.get();',
  30448. ' b.set(Result);',
  30449. ' c.set(Result);',
  30450. ' return Result;',
  30451. '};',
  30452. 'this.i = 0;',
  30453. '']),
  30454. LinesToStr([ // $mod.$main
  30455. '$mod.DoIt($mod.i, {',
  30456. ' p: $mod,',
  30457. ' get: function () {',
  30458. ' return this.p.i;',
  30459. ' },',
  30460. ' set: function (v) {',
  30461. ' this.p.i = v;',
  30462. ' }',
  30463. '}, {',
  30464. ' p: $mod,',
  30465. ' get: function () {',
  30466. ' return this.p.i;',
  30467. ' },',
  30468. ' set: function (v) {',
  30469. ' this.p.i = v;',
  30470. ' }',
  30471. '});',
  30472. '']));
  30473. end;
  30474. procedure TTestModule.TestJSValue_FuncResultType;
  30475. begin
  30476. StartProgram(false);
  30477. Add('type');
  30478. Add(' integer = longint;');
  30479. Add(' TJSValueArray = array of JSValue;');
  30480. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  30481. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  30482. Add('begin');
  30483. Add(' while Compare(P,aList[0])>0 do ;');
  30484. Add('end;');
  30485. Add('var');
  30486. Add(' Compare: TListSortCompare;');
  30487. Add(' V: JSValue;');
  30488. Add(' i: integer;');
  30489. Add('begin');
  30490. Add(' if Compare(V,V)>0 then ;');
  30491. Add(' if Compare(i,i)>1 then ;');
  30492. Add(' if Compare(nil,false)>2 then ;');
  30493. Add(' if Compare(1,true)>3 then ;');
  30494. ConvertProgram;
  30495. CheckSource('TestJSValue_UntypedParam',
  30496. LinesToStr([ // statements
  30497. 'this.Sort = function (P, aList, Compare) {',
  30498. ' while (Compare(P, aList[0]) > 0) {',
  30499. ' };',
  30500. '};',
  30501. 'this.Compare = null;',
  30502. 'this.V = undefined;',
  30503. 'this.i = 0;',
  30504. '']),
  30505. LinesToStr([ // $mod.$main
  30506. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  30507. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  30508. 'if ($mod.Compare(null, false) > 2) ;',
  30509. 'if ($mod.Compare(1, true) > 3) ;',
  30510. '']));
  30511. end;
  30512. procedure TTestModule.TestJSValue_ProcType_Assign;
  30513. begin
  30514. StartProgram(false);
  30515. Add('type');
  30516. Add(' integer = longint;');
  30517. Add(' TObject = class');
  30518. Add(' class function GetGlob: integer;');
  30519. Add(' function Getter: integer;');
  30520. Add(' end;');
  30521. Add('class function TObject.GetGlob: integer;');
  30522. Add('var v1: jsvalue;');
  30523. Add('begin');
  30524. Add(' v1:=@GetGlob;');
  30525. Add(' v1:[email protected];');
  30526. Add('end;');
  30527. Add('function TObject.Getter: integer;');
  30528. Add('var v2: jsvalue;');
  30529. Add('begin');
  30530. Add(' v2:=@Getter;');
  30531. Add(' v2:[email protected];');
  30532. Add(' v2:=@GetGlob;');
  30533. Add(' v2:[email protected];');
  30534. Add('end;');
  30535. Add('function GetIt(i: integer): integer;');
  30536. Add('var v3: jsvalue;');
  30537. Add('begin');
  30538. Add(' v3:=@GetIt;');
  30539. Add('end;');
  30540. Add('var');
  30541. Add(' V: JSValue;');
  30542. Add(' o: TObject;');
  30543. Add('begin');
  30544. Add(' v:=@GetIt;');
  30545. Add(' v:[email protected];');
  30546. Add(' v:[email protected];');
  30547. ConvertProgram;
  30548. CheckSource('TestJSValue_ProcType_Assign',
  30549. LinesToStr([ // statements
  30550. 'rtl.createClass(this, "TObject", null, function () {',
  30551. ' this.$init = function () {',
  30552. ' };',
  30553. ' this.$final = function () {',
  30554. ' };',
  30555. ' this.GetGlob = function () {',
  30556. ' var Result = 0;',
  30557. ' var v1 = undefined;',
  30558. ' v1 = rtl.createCallback(this, "GetGlob");',
  30559. ' v1 = rtl.createCallback(this, "GetGlob");',
  30560. ' return Result;',
  30561. ' };',
  30562. ' this.Getter = function () {',
  30563. ' var Result = 0;',
  30564. ' var v2 = undefined;',
  30565. ' v2 = rtl.createCallback(this, "Getter");',
  30566. ' v2 = rtl.createCallback(this, "Getter");',
  30567. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  30568. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  30569. ' return Result;',
  30570. ' };',
  30571. '});',
  30572. 'this.GetIt = function (i) {',
  30573. ' var Result = 0;',
  30574. ' var v3 = undefined;',
  30575. ' v3 = $mod.GetIt;',
  30576. ' return Result;',
  30577. '};',
  30578. 'this.V = undefined;',
  30579. 'this.o = null;',
  30580. '']),
  30581. LinesToStr([ // $mod.$main
  30582. '$mod.V = $mod.GetIt;',
  30583. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  30584. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  30585. '']));
  30586. end;
  30587. procedure TTestModule.TestJSValue_ProcType_Equal;
  30588. begin
  30589. StartProgram(false);
  30590. Add('type');
  30591. Add(' integer = longint;');
  30592. Add(' TObject = class');
  30593. Add(' class function GetGlob: integer;');
  30594. Add(' function Getter: integer;');
  30595. Add(' end;');
  30596. Add('class function TObject.GetGlob: integer;');
  30597. Add('var v1: jsvalue;');
  30598. Add('begin');
  30599. Add(' if v1=@GetGlob then;');
  30600. Add(' if [email protected] then ;');
  30601. Add('end;');
  30602. Add('function TObject.Getter: integer;');
  30603. Add('var v2: jsvalue;');
  30604. Add('begin');
  30605. Add(' if v2=@Getter then;');
  30606. Add(' if [email protected] then ;');
  30607. Add(' if v2=@GetGlob then;');
  30608. Add(' if [email protected] then;');
  30609. Add('end;');
  30610. Add('function GetIt(i: integer): integer;');
  30611. Add('var v3: jsvalue;');
  30612. Add('begin');
  30613. Add(' if v3=@GetIt then;');
  30614. Add('end;');
  30615. Add('var');
  30616. Add(' V: JSValue;');
  30617. Add(' o: TObject;');
  30618. Add('begin');
  30619. Add(' if v=@GetIt then;');
  30620. Add(' if [email protected] then;');
  30621. Add(' if [email protected] then;');
  30622. Add(' if @GetIt=v then;');
  30623. Add(' if @o.Getter=v then;');
  30624. Add(' if @o.GetGlob=v then;');
  30625. ConvertProgram;
  30626. CheckSource('TestJSValue_ProcType_Equal',
  30627. LinesToStr([ // statements
  30628. 'rtl.createClass(this, "TObject", null, function () {',
  30629. ' this.$init = function () {',
  30630. ' };',
  30631. ' this.$final = function () {',
  30632. ' };',
  30633. ' this.GetGlob = function () {',
  30634. ' var Result = 0;',
  30635. ' var v1 = undefined;',
  30636. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  30637. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  30638. ' return Result;',
  30639. ' };',
  30640. ' this.Getter = function () {',
  30641. ' var Result = 0;',
  30642. ' var v2 = undefined;',
  30643. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  30644. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  30645. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  30646. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  30647. ' return Result;',
  30648. ' };',
  30649. '});',
  30650. 'this.GetIt = function (i) {',
  30651. ' var Result = 0;',
  30652. ' var v3 = undefined;',
  30653. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  30654. ' return Result;',
  30655. '};',
  30656. 'this.V = undefined;',
  30657. 'this.o = null;',
  30658. '']),
  30659. LinesToStr([ // $mod.$main
  30660. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  30661. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  30662. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  30663. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  30664. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  30665. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  30666. '']));
  30667. end;
  30668. procedure TTestModule.TestJSValue_ProcType_Param;
  30669. begin
  30670. StartProgram(false);
  30671. Add([
  30672. 'type',
  30673. ' variant = jsvalue;',
  30674. ' TArrVariant = array of variant;',
  30675. ' TArrVar2 = TArrVariant;',
  30676. ' TFuncInt = function: longint;',
  30677. 'function GetIt: longint;',
  30678. 'begin',
  30679. 'end;',
  30680. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  30681. 'var v: variant;',
  30682. 'begin',
  30683. ' v:=arr[1];',
  30684. 'end;',
  30685. 'var s: string;',
  30686. 'begin',
  30687. ' DoIt(GetIt,[]);',
  30688. ' DoIt(@GetIt,[]);',
  30689. ' DoIt(1,[s,GetIt]);',
  30690. ' DoIt(1,[s,@GetIt]);',
  30691. '']);
  30692. ConvertProgram;
  30693. CheckSource('TestJSValue_ProcType_Param',
  30694. LinesToStr([ // statements
  30695. 'this.GetIt = function () {',
  30696. ' var Result = 0;',
  30697. ' return Result;',
  30698. '};',
  30699. 'this.DoIt = function (p, Arr) {',
  30700. ' var v = undefined;',
  30701. ' v = Arr[1];',
  30702. '};',
  30703. 'this.s = "";',
  30704. '']),
  30705. LinesToStr([ // $mod.$main
  30706. '$mod.DoIt($mod.GetIt(), []);',
  30707. '$mod.DoIt($mod.GetIt, []);',
  30708. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  30709. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  30710. '']));
  30711. end;
  30712. procedure TTestModule.TestJSValue_AssignToPointerFail;
  30713. begin
  30714. StartProgram(false);
  30715. Add([
  30716. 'var',
  30717. ' v: JSValue;',
  30718. ' p: Pointer;',
  30719. 'begin',
  30720. ' p:=v;',
  30721. '']);
  30722. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  30723. nIncompatibleTypesGotExpected);
  30724. ConvertProgram;
  30725. end;
  30726. procedure TTestModule.TestJSValue_OverloadDouble;
  30727. begin
  30728. StartProgram(false);
  30729. Add([
  30730. 'type',
  30731. ' integer = longint;',
  30732. ' tdatetime = double;',
  30733. 'procedure DoIt(d: double); begin end;',
  30734. 'procedure DoIt(v: jsvalue); begin end;',
  30735. 'var',
  30736. ' d: double;',
  30737. ' dt: tdatetime;',
  30738. ' i: integer;',
  30739. ' b: byte;',
  30740. ' shi: shortint;',
  30741. ' w: word;',
  30742. ' smi: smallint;',
  30743. ' lw: longword;',
  30744. ' li: longint;',
  30745. ' ni: nativeint;',
  30746. ' nu: nativeuint;',
  30747. 'begin',
  30748. ' DoIt(d);',
  30749. ' DoIt(dt);',
  30750. ' DoIt(i);',
  30751. ' DoIt(b);',
  30752. ' DoIt(shi);',
  30753. ' DoIt(w);',
  30754. ' DoIt(smi);',
  30755. ' DoIt(lw);',
  30756. ' DoIt(li);',
  30757. ' DoIt(ni);',
  30758. ' DoIt(nu);',
  30759. '']);
  30760. ConvertProgram;
  30761. CheckSource('TestJSValue_OverloadDouble',
  30762. LinesToStr([ // statements
  30763. 'this.DoIt = function (d) {',
  30764. '};',
  30765. 'this.DoIt$1 = function (v) {',
  30766. '};',
  30767. 'this.d = 0.0;',
  30768. 'this.dt = 0.0;',
  30769. 'this.i = 0;',
  30770. 'this.b = 0;',
  30771. 'this.shi = 0;',
  30772. 'this.w = 0;',
  30773. 'this.smi = 0;',
  30774. 'this.lw = 0;',
  30775. 'this.li = 0;',
  30776. 'this.ni = 0;',
  30777. 'this.nu = 0;',
  30778. '']),
  30779. LinesToStr([ // $mod.$main
  30780. '$mod.DoIt($mod.d);',
  30781. '$mod.DoIt($mod.dt);',
  30782. '$mod.DoIt$1($mod.i);',
  30783. '$mod.DoIt$1($mod.b);',
  30784. '$mod.DoIt$1($mod.shi);',
  30785. '$mod.DoIt$1($mod.w);',
  30786. '$mod.DoIt$1($mod.smi);',
  30787. '$mod.DoIt$1($mod.lw);',
  30788. '$mod.DoIt$1($mod.li);',
  30789. '$mod.DoIt$1($mod.ni);',
  30790. '$mod.DoIt$1($mod.nu);',
  30791. '']));
  30792. end;
  30793. procedure TTestModule.TestJSValue_OverloadNativeInt;
  30794. begin
  30795. StartProgram(false);
  30796. Add([
  30797. 'type',
  30798. ' integer = longint;',
  30799. ' int53 = nativeint;',
  30800. ' tdatetime = double;',
  30801. 'procedure DoIt(n: nativeint); begin end;',
  30802. 'procedure DoIt(v: jsvalue); begin end;',
  30803. 'var',
  30804. ' d: double;',
  30805. ' dt: tdatetime;',
  30806. ' i: integer;',
  30807. ' b: byte;',
  30808. ' shi: shortint;',
  30809. ' w: word;',
  30810. ' smi: smallint;',
  30811. ' lw: longword;',
  30812. ' li: longint;',
  30813. ' ni: nativeint;',
  30814. ' nu: nativeuint;',
  30815. 'begin',
  30816. ' DoIt(d);',
  30817. ' DoIt(dt);',
  30818. ' DoIt(i);',
  30819. ' DoIt(b);',
  30820. ' DoIt(shi);',
  30821. ' DoIt(w);',
  30822. ' DoIt(smi);',
  30823. ' DoIt(lw);',
  30824. ' DoIt(li);',
  30825. ' DoIt(ni);',
  30826. ' DoIt(nu);',
  30827. '']);
  30828. ConvertProgram;
  30829. CheckSource('TestJSValue_OverloadNativeInt',
  30830. LinesToStr([ // statements
  30831. 'this.DoIt = function (n) {',
  30832. '};',
  30833. 'this.DoIt$1 = function (v) {',
  30834. '};',
  30835. 'this.d = 0.0;',
  30836. 'this.dt = 0.0;',
  30837. 'this.i = 0;',
  30838. 'this.b = 0;',
  30839. 'this.shi = 0;',
  30840. 'this.w = 0;',
  30841. 'this.smi = 0;',
  30842. 'this.lw = 0;',
  30843. 'this.li = 0;',
  30844. 'this.ni = 0;',
  30845. 'this.nu = 0;',
  30846. '']),
  30847. LinesToStr([ // $mod.$main
  30848. '$mod.DoIt$1($mod.d);',
  30849. '$mod.DoIt$1($mod.dt);',
  30850. '$mod.DoIt($mod.i);',
  30851. '$mod.DoIt($mod.b);',
  30852. '$mod.DoIt($mod.shi);',
  30853. '$mod.DoIt($mod.w);',
  30854. '$mod.DoIt($mod.smi);',
  30855. '$mod.DoIt($mod.lw);',
  30856. '$mod.DoIt($mod.li);',
  30857. '$mod.DoIt($mod.ni);',
  30858. '$mod.DoIt($mod.nu);',
  30859. '']));
  30860. end;
  30861. procedure TTestModule.TestJSValue_OverloadWord;
  30862. begin
  30863. StartProgram(false);
  30864. Add([
  30865. 'type',
  30866. ' integer = longint;',
  30867. ' int53 = nativeint;',
  30868. ' tdatetime = double;',
  30869. 'procedure DoIt(w: word); begin end;',
  30870. 'procedure DoIt(v: jsvalue); begin end;',
  30871. 'var',
  30872. ' d: double;',
  30873. ' dt: tdatetime;',
  30874. ' i: integer;',
  30875. ' b: byte;',
  30876. ' shi: shortint;',
  30877. ' w: word;',
  30878. ' smi: smallint;',
  30879. ' lw: longword;',
  30880. ' li: longint;',
  30881. ' ni: nativeint;',
  30882. ' nu: nativeuint;',
  30883. 'begin',
  30884. ' DoIt(d);',
  30885. ' DoIt(dt);',
  30886. ' DoIt(i);',
  30887. ' DoIt(b);',
  30888. ' DoIt(shi);',
  30889. ' DoIt(w);',
  30890. ' DoIt(smi);',
  30891. ' DoIt(lw);',
  30892. ' DoIt(li);',
  30893. ' DoIt(ni);',
  30894. ' DoIt(nu);',
  30895. '']);
  30896. ConvertProgram;
  30897. CheckSource('TestJSValue_OverloadWord',
  30898. LinesToStr([ // statements
  30899. 'this.DoIt = function (w) {',
  30900. '};',
  30901. 'this.DoIt$1 = function (v) {',
  30902. '};',
  30903. 'this.d = 0.0;',
  30904. 'this.dt = 0.0;',
  30905. 'this.i = 0;',
  30906. 'this.b = 0;',
  30907. 'this.shi = 0;',
  30908. 'this.w = 0;',
  30909. 'this.smi = 0;',
  30910. 'this.lw = 0;',
  30911. 'this.li = 0;',
  30912. 'this.ni = 0;',
  30913. 'this.nu = 0;',
  30914. '']),
  30915. LinesToStr([ // $mod.$main
  30916. '$mod.DoIt$1($mod.d);',
  30917. '$mod.DoIt$1($mod.dt);',
  30918. '$mod.DoIt$1($mod.i);',
  30919. '$mod.DoIt($mod.b);',
  30920. '$mod.DoIt($mod.shi);',
  30921. '$mod.DoIt($mod.w);',
  30922. '$mod.DoIt$1($mod.smi);',
  30923. '$mod.DoIt$1($mod.lw);',
  30924. '$mod.DoIt$1($mod.li);',
  30925. '$mod.DoIt$1($mod.ni);',
  30926. '$mod.DoIt$1($mod.nu);',
  30927. '']));
  30928. end;
  30929. procedure TTestModule.TestJSValue_OverloadString;
  30930. begin
  30931. StartProgram(false);
  30932. Add([
  30933. 'type',
  30934. ' uni = string;',
  30935. ' WChar = char;',
  30936. 'procedure DoIt(s: string); begin end;',
  30937. 'procedure DoIt(v: jsvalue); begin end;',
  30938. 'var',
  30939. ' s: string;',
  30940. ' c: char;',
  30941. ' u: uni;',
  30942. 'begin',
  30943. ' DoIt(s);',
  30944. ' DoIt(c);',
  30945. ' DoIt(u);',
  30946. '']);
  30947. ConvertProgram;
  30948. CheckSource('TestJSValue_OverloadString',
  30949. LinesToStr([ // statements
  30950. 'this.DoIt = function (s) {',
  30951. '};',
  30952. 'this.DoIt$1 = function (v) {',
  30953. '};',
  30954. 'this.s = "";',
  30955. 'this.c = "\x00";',
  30956. 'this.u = "";',
  30957. '']),
  30958. LinesToStr([ // $mod.$main
  30959. '$mod.DoIt($mod.s);',
  30960. '$mod.DoIt($mod.c);',
  30961. '$mod.DoIt($mod.u);',
  30962. '']));
  30963. end;
  30964. procedure TTestModule.TestJSValue_OverloadChar;
  30965. begin
  30966. StartProgram(false);
  30967. Add([
  30968. 'type',
  30969. ' uni = string;',
  30970. ' WChar = char;',
  30971. 'procedure DoIt(c: char); begin end;',
  30972. 'procedure DoIt(v: jsvalue); begin end;',
  30973. 'var',
  30974. ' s: string;',
  30975. ' c: char;',
  30976. ' u: uni;',
  30977. 'begin',
  30978. ' DoIt(s);',
  30979. ' DoIt(c);',
  30980. ' DoIt(u);',
  30981. '']);
  30982. ConvertProgram;
  30983. CheckSource('TestJSValue_OverloadChar',
  30984. LinesToStr([ // statements
  30985. 'this.DoIt = function (c) {',
  30986. '};',
  30987. 'this.DoIt$1 = function (v) {',
  30988. '};',
  30989. 'this.s = "";',
  30990. 'this.c = "\x00";',
  30991. 'this.u = "";',
  30992. '']),
  30993. LinesToStr([ // $mod.$main
  30994. '$mod.DoIt$1($mod.s);',
  30995. '$mod.DoIt($mod.c);',
  30996. '$mod.DoIt$1($mod.u);',
  30997. '']));
  30998. end;
  30999. procedure TTestModule.TestJSValue_OverloadPointer;
  31000. begin
  31001. StartProgram(false);
  31002. Add([
  31003. 'type',
  31004. ' TObject = class end;',
  31005. 'procedure DoIt(p: pointer); begin end;',
  31006. 'procedure DoIt(v: jsvalue); begin end;',
  31007. 'var',
  31008. ' o: TObject;',
  31009. 'begin',
  31010. ' DoIt(o);',
  31011. '']);
  31012. ConvertProgram;
  31013. CheckSource('TestJSValue_OverloadPointer',
  31014. LinesToStr([ // statements
  31015. 'rtl.createClass(this, "TObject", null, function () {',
  31016. ' this.$init = function () {',
  31017. ' };',
  31018. ' this.$final = function () {',
  31019. ' };',
  31020. '});',
  31021. 'this.DoIt = function (p) {',
  31022. '};',
  31023. 'this.DoIt$1 = function (v) {',
  31024. '};',
  31025. 'this.o = null;',
  31026. '']),
  31027. LinesToStr([ // $mod.$main
  31028. '$mod.DoIt($mod.o);',
  31029. '']));
  31030. end;
  31031. procedure TTestModule.TestJSValue_ForIn;
  31032. begin
  31033. StartProgram(false);
  31034. Add([
  31035. 'var',
  31036. ' v: JSValue;',
  31037. ' key: string;',
  31038. 'begin',
  31039. ' for key in v do begin',
  31040. ' if key=''abc'' then ;',
  31041. ' end;',
  31042. '']);
  31043. ConvertProgram;
  31044. CheckSource('TestJSValue_ForIn',
  31045. LinesToStr([ // statements
  31046. 'this.v = undefined;',
  31047. 'this.key = "";',
  31048. '']),
  31049. LinesToStr([ // $mod.$main
  31050. 'for ($mod.key in $mod.v) {',
  31051. ' if ($mod.key === "abc") ;',
  31052. '};',
  31053. '']));
  31054. end;
  31055. procedure TTestModule.TestRTTI_IntRange;
  31056. begin
  31057. WithTypeInfo:=true;
  31058. StartProgram(true,[supTypeInfo]);
  31059. Add([
  31060. '{$modeswitch externalclass}',
  31061. 'type',
  31062. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  31063. ' TColor = type TGraphicsColor;',
  31064. 'var',
  31065. ' p: TTypeInfo;',
  31066. ' k: TTypeKind;',
  31067. 'begin',
  31068. ' p:=typeinfo(TGraphicsColor);',
  31069. ' p:=typeinfo(TColor);',
  31070. ' k:=GetTypeKind(TGraphicsColor);',
  31071. ' k:=GetTypeKind(TColor);',
  31072. '']);
  31073. ConvertProgram;
  31074. CheckSource('TestRTTI_IntRange',
  31075. LinesToStr([ // statements
  31076. 'this.$rtti.$Int("TGraphicsColor", {',
  31077. ' minvalue: -2147483648,',
  31078. ' maxvalue: 2147483647,',
  31079. ' ordtype: 4',
  31080. '});',
  31081. 'this.$rtti.$inherited("TColor", this.$rtti["TGraphicsColor"], {});',
  31082. 'this.p = null;',
  31083. 'this.k = 0;',
  31084. '']),
  31085. LinesToStr([ // $mod.$main
  31086. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  31087. '$mod.p = $mod.$rtti["TColor"];',
  31088. '$mod.k = 1;',
  31089. '$mod.k = 1;',
  31090. '']));
  31091. end;
  31092. procedure TTestModule.TestRTTI_Double;
  31093. begin
  31094. WithTypeInfo:=true;
  31095. StartProgram(true,[supTypeInfo]);
  31096. Add([
  31097. '{$modeswitch externalclass}',
  31098. 'type',
  31099. ' TFloat = type double;',
  31100. 'var',
  31101. ' p: TTypeInfo;',
  31102. 'begin',
  31103. ' p:=typeinfo(double);',
  31104. ' p:=typeinfo(TFloat);',
  31105. '']);
  31106. ConvertProgram;
  31107. CheckSource('TestRTTI_Double',
  31108. LinesToStr([ // statements
  31109. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  31110. 'this.p = null;',
  31111. '']),
  31112. LinesToStr([ // $mod.$main
  31113. '$mod.p = rtl.double;',
  31114. '$mod.p = $mod.$rtti["TFloat"];',
  31115. '']));
  31116. end;
  31117. procedure TTestModule.TestRTTI_ProcType;
  31118. begin
  31119. WithTypeInfo:=true;
  31120. StartProgram(false);
  31121. Add('type');
  31122. Add(' TProcA = procedure;');
  31123. Add(' TMethodB = procedure of object;');
  31124. Add(' TProcC = procedure; varargs;');
  31125. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  31126. Add(' TProcE = function: nativeint;');
  31127. Add(' TProcF = function(const p: TProcA): nativeuint;');
  31128. Add('var p: pointer;');
  31129. Add('begin');
  31130. Add(' p:=typeinfo(tproca);');
  31131. ConvertProgram;
  31132. CheckSource('TestRTTI_ProcType',
  31133. LinesToStr([ // statements
  31134. 'this.$rtti.$ProcVar("TProcA", {',
  31135. ' procsig: rtl.newTIProcSig([])',
  31136. '});',
  31137. 'this.$rtti.$MethodVar("TMethodB", {',
  31138. ' procsig: rtl.newTIProcSig([]),',
  31139. ' methodkind: 0',
  31140. '});',
  31141. 'this.$rtti.$ProcVar("TProcC", {',
  31142. ' procsig: rtl.newTIProcSig([], null, 2)',
  31143. '});',
  31144. 'this.$rtti.$ProcVar("TProcD", {',
  31145. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  31146. '});',
  31147. 'this.$rtti.$ProcVar("TProcE", {',
  31148. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  31149. '});',
  31150. 'this.$rtti.$ProcVar("TProcF", {',
  31151. ' procsig: rtl.newTIProcSig([["p", this.$rtti["TProcA"], 2]], rtl.nativeuint)',
  31152. '});',
  31153. 'this.p = null;',
  31154. '']),
  31155. LinesToStr([ // $mod.$main
  31156. '$mod.p = $mod.$rtti["TProcA"];',
  31157. '']));
  31158. end;
  31159. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  31160. begin
  31161. WithTypeInfo:=true;
  31162. AddModuleWithIntfImplSrc('unit2.pas',
  31163. LinesToStr([
  31164. 'type',
  31165. ' TObject = class end;'
  31166. ]),
  31167. '');
  31168. StartUnit(true);
  31169. Add('interface');
  31170. Add('uses unit2;');
  31171. Add('type');
  31172. Add(' TProcA = function(o: tobject): tobject;');
  31173. Add('implementation');
  31174. Add('type');
  31175. Add(' TProcB = function(o: tobject): tobject;');
  31176. Add('var p: Pointer;');
  31177. Add('initialization');
  31178. Add(' p:=typeinfo(tproca);');
  31179. Add(' p:=typeinfo(tprocb);');
  31180. ConvertUnit;
  31181. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  31182. LinesToStr([ // statements
  31183. 'var $impl = $mod.$impl;',
  31184. 'this.$rtti.$ProcVar("TProcA", {',
  31185. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  31186. '});',
  31187. '']),
  31188. LinesToStr([ // this.$init
  31189. '$impl.p = $mod.$rtti["TProcA"];',
  31190. '$impl.p = $mod.$rtti["TProcB"];',
  31191. '']),
  31192. LinesToStr([ // implementation
  31193. '$mod.$rtti.$ProcVar("TProcB", {',
  31194. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  31195. '});',
  31196. '$impl.p = null;',
  31197. '']) );
  31198. end;
  31199. procedure TTestModule.TestRTTI_ProcTypeAnonymous;
  31200. begin
  31201. WithTypeInfo:=true;
  31202. StartProgram(false);
  31203. Add(['var',
  31204. ' ProcA: procedure;',
  31205. ' MethodB: procedure of object;',
  31206. ' ProcC: procedure; varargs;',
  31207. ' ProcD: procedure(i: longint; const j: string; var c: char; out d: double);',
  31208. ' ProcE: function: nativeint;',
  31209. ' p: pointer;',
  31210. 'begin',
  31211. ' p:=typeinfo(proca);']);
  31212. ConvertProgram;
  31213. CheckSource('TestRTTI_ProcTypeAnonymous',
  31214. LinesToStr([ // statements
  31215. 'this.$rtti.$ProcVar("ProcA$a", {',
  31216. ' procsig: rtl.newTIProcSig([])',
  31217. '});',
  31218. 'this.ProcA = null;',
  31219. 'this.$rtti.$MethodVar("MethodB$a", {',
  31220. ' procsig: rtl.newTIProcSig([]),',
  31221. ' methodkind: 0',
  31222. '});',
  31223. 'this.MethodB = null;',
  31224. 'this.$rtti.$ProcVar("ProcC$a", {',
  31225. ' procsig: rtl.newTIProcSig([], null, 2)',
  31226. '});',
  31227. 'this.ProcC = null;',
  31228. 'this.$rtti.$ProcVar("ProcD$a", {',
  31229. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  31230. '});',
  31231. 'this.ProcD = null;',
  31232. 'this.$rtti.$ProcVar("ProcE$a", {',
  31233. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  31234. '});',
  31235. 'this.ProcE = null;',
  31236. 'this.p = null;',
  31237. '']),
  31238. LinesToStr([ // $mod.$main
  31239. '$mod.p = $mod.$rtti["ProcA$a"];',
  31240. '']));
  31241. end;
  31242. procedure TTestModule.TestRTTI_EnumAndSetType;
  31243. begin
  31244. WithTypeInfo:=true;
  31245. StartProgram(false);
  31246. Add('type');
  31247. Add(' TFlag = (light,dark);');
  31248. Add(' TFlags = set of TFlag;');
  31249. Add(' TProc = function(f: TFlags): TFlag;');
  31250. Add('var p: pointer;');
  31251. Add('begin');
  31252. Add(' p:=typeinfo(tflag);');
  31253. Add(' p:=typeinfo(tflags);');
  31254. ConvertProgram;
  31255. CheckSource('TestRTTI_EnumAndType',
  31256. LinesToStr([ // statements
  31257. 'this.TFlag = {',
  31258. ' "0": "light",',
  31259. ' light: 0,',
  31260. ' "1": "dark",',
  31261. ' dark: 1',
  31262. '};',
  31263. 'this.$rtti.$Enum("TFlag", {',
  31264. ' minvalue: 0,',
  31265. ' maxvalue: 1,',
  31266. ' ordtype: 1,',
  31267. ' enumtype: this.TFlag',
  31268. '});',
  31269. 'this.$rtti.$Set("TFlags", {',
  31270. ' comptype: this.$rtti["TFlag"]',
  31271. '});',
  31272. 'this.$rtti.$ProcVar("TProc", {',
  31273. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TFlags"]]], this.$rtti["TFlag"])',
  31274. '});',
  31275. 'this.p = null;',
  31276. '']),
  31277. LinesToStr([ // $mod.$main
  31278. '$mod.p = $mod.$rtti["TFlag"];',
  31279. '$mod.p = $mod.$rtti["TFlags"];',
  31280. '']));
  31281. end;
  31282. procedure TTestModule.TestRTTI_EnumRange;
  31283. begin
  31284. WithTypeInfo:=true;
  31285. StartProgram(false);
  31286. Add([
  31287. 'type',
  31288. ' TCol = (red,green,blue);',
  31289. ' TColRg = green..blue;',
  31290. ' TSetOfColRg = set of TColRg;',
  31291. 'var p: pointer;',
  31292. 'begin',
  31293. ' p:=typeinfo(tcolrg);',
  31294. ' p:=typeinfo(tsetofcolrg);',
  31295. '']);
  31296. ConvertProgram;
  31297. end;
  31298. procedure TTestModule.TestRTTI_AnonymousEnumType;
  31299. begin
  31300. WithTypeInfo:=true;
  31301. StartProgram(false);
  31302. Add('type');
  31303. Add(' TFlags = set of (red, green);');
  31304. Add('var');
  31305. Add(' f: TFlags;');
  31306. Add('begin');
  31307. Add(' Include(f,red);');
  31308. ConvertProgram;
  31309. CheckSource('TestRTTI_AnonymousEnumType',
  31310. LinesToStr([ // statements
  31311. 'this.TFlags$a = {',
  31312. ' "0": "red",',
  31313. ' red: 0,',
  31314. ' "1": "green",',
  31315. ' green: 1',
  31316. '};',
  31317. 'this.$rtti.$Enum("TFlags$a", {',
  31318. ' minvalue: 0,',
  31319. ' maxvalue: 1,',
  31320. ' ordtype: 1,',
  31321. ' enumtype: this.TFlags$a',
  31322. '});',
  31323. 'this.$rtti.$Set("TFlags", {',
  31324. ' comptype: this.$rtti["TFlags$a"]',
  31325. '});',
  31326. 'this.f = {};',
  31327. '']),
  31328. LinesToStr([
  31329. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  31330. '']));
  31331. end;
  31332. procedure TTestModule.TestRTTI_StaticArray;
  31333. begin
  31334. WithTypeInfo:=true;
  31335. StartProgram(false);
  31336. Add('type');
  31337. Add(' TFlag = (light,dark);');
  31338. Add(' TFlagNames = array[TFlag] of string;');
  31339. Add(' TBoolNames = array[boolean] of string;');
  31340. Add(' TByteArray = array[1..32768] of byte;');
  31341. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  31342. Add('var p: pointer;');
  31343. Add('begin');
  31344. Add(' p:=typeinfo(TFlagNames);');
  31345. Add(' p:=typeinfo(TBoolNames);');
  31346. ConvertProgram;
  31347. CheckSource('TestRTTI_StaticArray',
  31348. LinesToStr([ // statements
  31349. 'this.TFlag = {',
  31350. ' "0": "light",',
  31351. ' light: 0,',
  31352. ' "1": "dark",',
  31353. ' dark: 1',
  31354. '};',
  31355. 'this.$rtti.$Enum("TFlag", {',
  31356. ' minvalue: 0,',
  31357. ' maxvalue: 1,',
  31358. ' ordtype: 1,',
  31359. ' enumtype: this.TFlag',
  31360. '});',
  31361. 'this.$rtti.$StaticArray("TFlagNames", {',
  31362. ' dims: [2],',
  31363. ' eltype: rtl.string',
  31364. '});',
  31365. 'this.$rtti.$StaticArray("TBoolNames", {',
  31366. ' dims: [2],',
  31367. ' eltype: rtl.string',
  31368. '});',
  31369. 'this.$rtti.$StaticArray("TByteArray", {',
  31370. ' dims: [32768],',
  31371. ' eltype: rtl.byte',
  31372. '});',
  31373. 'this.$rtti.$ProcVar("TProc", {',
  31374. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TBoolNames"]]], this.$rtti["TFlagNames"])',
  31375. '});',
  31376. 'this.p = null;',
  31377. '']),
  31378. LinesToStr([ // $mod.$main
  31379. '$mod.p = $mod.$rtti["TFlagNames"];',
  31380. '$mod.p = $mod.$rtti["TBoolNames"];',
  31381. '']));
  31382. end;
  31383. procedure TTestModule.TestRTTI_DynArray;
  31384. begin
  31385. WithTypeInfo:=true;
  31386. StartProgram(false);
  31387. Add('type');
  31388. Add(' TArrStr = array of string;');
  31389. Add(' TArr2Dim = array of tarrstr;');
  31390. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  31391. Add('var p: pointer;');
  31392. Add('begin');
  31393. Add(' p:=typeinfo(tarrstr);');
  31394. Add(' p:=typeinfo(tarr2dim);');
  31395. ConvertProgram;
  31396. CheckSource('TestRTTI_DynArray',
  31397. LinesToStr([ // statements
  31398. 'this.$rtti.$DynArray("TArrStr", {',
  31399. ' eltype: rtl.string',
  31400. '});',
  31401. 'this.$rtti.$DynArray("TArr2Dim", {',
  31402. ' eltype: this.$rtti["TArrStr"]',
  31403. '});',
  31404. 'this.$rtti.$ProcVar("TProc", {',
  31405. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TArrStr"]]], this.$rtti["TArr2Dim"])',
  31406. '});',
  31407. 'this.p = null;',
  31408. '']),
  31409. LinesToStr([ // $mod.$main
  31410. '$mod.p = $mod.$rtti["TArrStr"];',
  31411. '$mod.p = $mod.$rtti["TArr2Dim"];',
  31412. '']));
  31413. end;
  31414. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  31415. begin
  31416. WithTypeInfo:=true;
  31417. StartProgram(false);
  31418. Add('type');
  31419. Add(' TArr = array of array of longint;');
  31420. Add('var a: TArr;');
  31421. Add('begin');
  31422. ConvertProgram;
  31423. CheckSource('TestRTTI_ArrayNestedAnonymous',
  31424. LinesToStr([ // statements
  31425. 'this.$rtti.$DynArray("TArr$a", {',
  31426. ' eltype: rtl.longint',
  31427. '});',
  31428. 'this.$rtti.$DynArray("TArr", {',
  31429. ' eltype: this.$rtti["TArr$a"]',
  31430. '});',
  31431. 'this.a = [];',
  31432. '']),
  31433. LinesToStr([ // $mod.$main
  31434. ]));
  31435. end;
  31436. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  31437. begin
  31438. WithTypeInfo:=true;
  31439. StartProgram(false);
  31440. Add('type');
  31441. Add(' TObject = class');
  31442. Add(' published');
  31443. Add(' procedure Proc; virtual; abstract;');
  31444. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  31445. Add(' end;');
  31446. Add('begin');
  31447. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  31448. nDuplicatePublishedMethodXAtY);
  31449. ConvertProgram;
  31450. end;
  31451. procedure TTestModule.TestRTTI_PublishedMethodHideNoHint;
  31452. begin
  31453. WithTypeInfo:=true;
  31454. StartUnit(false);
  31455. Add([
  31456. 'interface',
  31457. 'type',
  31458. ' TObject = class',
  31459. ' end;',
  31460. ' {$M+}',
  31461. ' TBird = class',
  31462. ' procedure Fly;',
  31463. ' end;',
  31464. ' {$M-}',
  31465. 'type',
  31466. ' TEagle = class(TBird)',
  31467. ' procedure Fly;',
  31468. ' end;',
  31469. 'implementation',
  31470. 'procedure TBird.Fly;',
  31471. 'begin',
  31472. 'end;',
  31473. 'procedure TEagle.Fly;',
  31474. 'begin',
  31475. 'end;',
  31476. '']);
  31477. ConvertUnit;
  31478. CheckSource('TestRTTI_PublishedMethodHideNoHint',
  31479. LinesToStr([ // statements
  31480. 'rtl.createClass(this, "TObject", null, function () {',
  31481. ' this.$init = function () {',
  31482. ' };',
  31483. ' this.$final = function () {',
  31484. ' };',
  31485. '});',
  31486. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31487. ' this.Fly = function () {',
  31488. ' };',
  31489. ' var $r = this.$rtti;',
  31490. ' $r.addMethod("Fly", 0, [], 4);',
  31491. '});',
  31492. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  31493. ' this.Fly = function () {',
  31494. ' };',
  31495. ' var $r = this.$rtti;',
  31496. ' $r.addMethod("Fly", 0, [], 4);',
  31497. '});',
  31498. '']),
  31499. LinesToStr([ // $mod.$main
  31500. ]));
  31501. CheckResolverUnexpectedHints(true);
  31502. end;
  31503. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  31504. begin
  31505. WithTypeInfo:=true;
  31506. StartProgram(false);
  31507. Add('type');
  31508. Add(' TObject = class');
  31509. Add(' published');
  31510. Add(' procedure Proc; external name ''foo'';');
  31511. Add(' end;');
  31512. Add('begin');
  31513. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  31514. nPublishedNameMustMatchExternal);
  31515. ConvertProgram;
  31516. end;
  31517. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  31518. begin
  31519. WithTypeInfo:=true;
  31520. StartProgram(false);
  31521. Add('type');
  31522. Add(' TObject = class');
  31523. Add(' class var FA: longint;');
  31524. Add(' published');
  31525. Add(' class property A: longint read FA;');
  31526. Add(' end;');
  31527. Add('begin');
  31528. SetExpectedPasResolverError('Invalid published property modifier "class"',
  31529. nInvalidXModifierY);
  31530. ConvertProgram;
  31531. end;
  31532. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  31533. begin
  31534. WithTypeInfo:=true;
  31535. StartProgram(false);
  31536. Add('type');
  31537. Add(' TObject = class');
  31538. Add(' published');
  31539. Add(' class var FA: longint;');
  31540. Add(' end;');
  31541. Add('begin');
  31542. SetExpectedPasResolverError(sSymbolCannotBePublished,
  31543. nSymbolCannotBePublished);
  31544. ConvertProgram;
  31545. end;
  31546. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  31547. begin
  31548. WithTypeInfo:=true;
  31549. StartProgram(false);
  31550. Add('{$modeswitch externalclass}');
  31551. Add('type');
  31552. Add(' TObject = class');
  31553. Add(' published');
  31554. Add(' V: longint; external name ''foo'';');
  31555. Add(' end;');
  31556. Add('begin');
  31557. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  31558. nPublishedNameMustMatchExternal);
  31559. ConvertProgram;
  31560. end;
  31561. procedure TTestModule.TestRTTI_Class_Field;
  31562. begin
  31563. WithTypeInfo:=true;
  31564. StartProgram(false);
  31565. Add('{$modeswitch externalclass}');
  31566. Add('type');
  31567. Add(' TObject = class');
  31568. Add(' private');
  31569. Add(' FPropA: string;');
  31570. Add(' published');
  31571. Add(' VarLI: longint;');
  31572. Add(' VarC: char;');
  31573. Add(' VarS: string;');
  31574. Add(' VarD: double;');
  31575. Add(' VarB: boolean;');
  31576. Add(' VarLW: longword;');
  31577. Add(' VarSmI: smallint;');
  31578. Add(' VarW: word;');
  31579. Add(' VarShI: shortint;');
  31580. Add(' VarBy: byte;');
  31581. Add(' VarExt: longint external name ''VarExt'';');
  31582. Add(' ArrA, ArrB: array of byte;');
  31583. Add(' end;');
  31584. Add('var p: pointer;');
  31585. Add(' Obj: tobject;');
  31586. Add('begin');
  31587. Add(' p:=typeinfo(tobject);');
  31588. Add(' p:=typeinfo(p);');
  31589. Add(' p:=typeinfo(obj);');
  31590. ConvertProgram;
  31591. CheckSource('TestRTTI_Class_Field',
  31592. LinesToStr([ // statements
  31593. 'rtl.createClass(this, "TObject", null, function () {',
  31594. ' this.$init = function () {',
  31595. ' this.FPropA = "";',
  31596. ' this.VarLI = 0;',
  31597. ' this.VarC = "\x00";',
  31598. ' this.VarS = "";',
  31599. ' this.VarD = 0.0;',
  31600. ' this.VarB = false;',
  31601. ' this.VarLW = 0;',
  31602. ' this.VarSmI = 0;',
  31603. ' this.VarW = 0;',
  31604. ' this.VarShI = 0;',
  31605. ' this.VarBy = 0;',
  31606. ' this.ArrA = [];',
  31607. ' this.ArrB = [];',
  31608. ' };',
  31609. ' this.$final = function () {',
  31610. ' this.ArrA = undefined;',
  31611. ' this.ArrB = undefined;',
  31612. ' };',
  31613. ' var $r = this.$rtti;',
  31614. ' $r.addField("VarLI", rtl.longint, 4);',
  31615. ' $r.addField("VarC", rtl.char, 4);',
  31616. ' $r.addField("VarS", rtl.string, 4);',
  31617. ' $r.addField("VarD", rtl.double, 4);',
  31618. ' $r.addField("VarB", rtl.boolean, 4);',
  31619. ' $r.addField("VarLW", rtl.longword, 4);',
  31620. ' $r.addField("VarSmI", rtl.smallint, 4);',
  31621. ' $r.addField("VarW", rtl.word, 4);',
  31622. ' $r.addField("VarShI", rtl.shortint, 4);',
  31623. ' $r.addField("VarBy", rtl.byte, 4);',
  31624. ' $r.addField("VarExt", rtl.longint, 4);',
  31625. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  31626. ' eltype: rtl.byte',
  31627. ' });',
  31628. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"], 4);',
  31629. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"], 4);',
  31630. '});',
  31631. 'this.p = null;',
  31632. 'this.Obj = null;',
  31633. '']),
  31634. LinesToStr([ // $mod.$main
  31635. '$mod.p = $mod.$rtti["TObject"];',
  31636. '$mod.p = rtl.pointer;',
  31637. '$mod.p = $mod.Obj.$rtti;',
  31638. '']));
  31639. end;
  31640. procedure TTestModule.TestRTTI_Class_FieldPrivate;
  31641. begin
  31642. WithTypeInfo:=true;
  31643. StartProgram(false);
  31644. Add('type');
  31645. Add('{$RTTI explicit fields([vcPrivate,vcProtected,vcPublic,vcPublished])}');
  31646. Add(' TObject = class');
  31647. Add(' strict private');
  31648. Add(' A1: word;');
  31649. Add(' private');
  31650. Add(' A2: word;');
  31651. Add(' strict protected');
  31652. Add(' B1: word;');
  31653. Add(' protected');
  31654. Add(' B2, B3: word;');
  31655. Add(' public');
  31656. Add(' C: word;');
  31657. Add(' published');
  31658. Add(' D: word;');
  31659. Add(' end;');
  31660. Add('begin');
  31661. ConvertProgram;
  31662. CheckSource('TestRTTI_Class_FieldPrivate',
  31663. LinesToStr([ // statements
  31664. 'rtl.createClass(this, "TObject", null, function () {',
  31665. ' this.$init = function () {',
  31666. ' this.A1 = 0;',
  31667. ' this.A2 = 0;',
  31668. ' this.B1 = 0;',
  31669. ' this.B2 = 0;',
  31670. ' this.B3 = 0;',
  31671. ' this.C = 0;',
  31672. ' this.D = 0;',
  31673. ' };',
  31674. ' this.$final = function () {',
  31675. ' };',
  31676. ' var $r = this.$rtti;',
  31677. ' $r.addField("A1", rtl.word, 5);',
  31678. ' $r.addField("A2", rtl.word, 0);',
  31679. ' $r.addField("B1", rtl.word, 6);',
  31680. ' $r.addField("B2", rtl.word, 1);',
  31681. ' $r.addField("B3", rtl.word, 1);',
  31682. ' $r.addField("C", rtl.word);',
  31683. ' $r.addField("D", rtl.word, 3);',
  31684. '});',
  31685. '']),
  31686. LinesToStr([ // $mod.$main
  31687. '']));
  31688. end;
  31689. procedure TTestModule.TestRTTI_Class_Method;
  31690. begin
  31691. WithTypeInfo:=true;
  31692. StartProgram(false);
  31693. Add([
  31694. 'type',
  31695. ' TObject = class',
  31696. ' private',
  31697. ' procedure Internal; external name ''$intern'';',
  31698. ' published',
  31699. ' procedure Click; virtual; abstract;',
  31700. ' procedure Notify(Sender: TObject); virtual; abstract;',
  31701. ' function GetNotify: boolean; external name ''GetNotify'';',
  31702. ' procedure Println(a,b: longint); varargs; virtual; abstract;',
  31703. ' function Fetch(URL: string): word; async; external name ''Fetch'';',
  31704. ' end;',
  31705. 'begin']);
  31706. ConvertProgram;
  31707. CheckSource('TestRTTI_Class_Method',
  31708. LinesToStr([ // statements
  31709. 'rtl.createClass(this, "TObject", null, function () {',
  31710. ' this.$init = function () {',
  31711. ' };',
  31712. ' this.$final = function () {',
  31713. ' };',
  31714. ' var $r = this.$rtti;',
  31715. ' $r.addMethod("Click", 0, [], 4);',
  31716. ' $r.addMethod("Notify", 0, [["Sender", $r]], 4);',
  31717. ' $r.addMethod("GetNotify", 1, [], 4, rtl.boolean, 4);',
  31718. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], 4, null, 2);',
  31719. ' $r.addMethod("Fetch", 1, [["URL", rtl.string]], 4, rtl.word, 20);',
  31720. '});',
  31721. '']),
  31722. LinesToStr([ // $mod.$main
  31723. '']));
  31724. end;
  31725. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  31726. begin
  31727. WithTypeInfo:=true;
  31728. StartProgram(false);
  31729. Add('type');
  31730. Add(' TObject = class');
  31731. Add(' published');
  31732. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  31733. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  31734. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  31735. Add(' end;');
  31736. Add('begin');
  31737. ConvertProgram;
  31738. CheckSource('TestRTTI_Class_MethodOpenArray',
  31739. LinesToStr([ // statements
  31740. 'rtl.createClass(this, "TObject", null, function () {',
  31741. ' this.$init = function () {',
  31742. ' };',
  31743. ' this.$final = function () {',
  31744. ' };',
  31745. ' var $r = this.$rtti;',
  31746. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]], 4);',
  31747. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]], 4);',
  31748. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]], 4);',
  31749. '});',
  31750. '']),
  31751. LinesToStr([ // $mod.$main
  31752. '']));
  31753. end;
  31754. procedure TTestModule.TestRTTI_Class_MethodPrivate;
  31755. begin
  31756. WithTypeInfo:=true;
  31757. StartProgram(false);
  31758. Add('type');
  31759. Add('{$RTTI explicit methods([vcPrivate,vcProtected,vcPublic,vcPublished])}');
  31760. Add(' TObject = class');
  31761. Add(' private');
  31762. Add(' procedure PrivateProc(a: word); virtual; abstract;');
  31763. Add(' protected');
  31764. Add(' class function ProtectedFunc: word; virtual; abstract;');
  31765. Add(' public');
  31766. Add(' class procedure PublicProc; virtual; abstract;');
  31767. Add(' constructor Create;');
  31768. Add(' destructor Destroy;');
  31769. Add(' published');
  31770. Add(' function PublishedProc: word; virtual; abstract;');
  31771. Add(' end;');
  31772. Add('constructor TObject.Create;');
  31773. Add('begin');
  31774. Add('end;');
  31775. Add('destructor TObject.Destroy;');
  31776. Add('begin');
  31777. Add('end;');
  31778. Add('begin');
  31779. ConvertProgram;
  31780. CheckSource('TestRTTI_Class_MethodPrivate',
  31781. LinesToStr([ // statements
  31782. 'rtl.createClass(this, "TObject", null, function () {',
  31783. ' this.$init = function () {',
  31784. ' };',
  31785. ' this.$final = function () {',
  31786. ' };',
  31787. ' this.Create = function () {',
  31788. ' return this;',
  31789. ' };',
  31790. ' this.Destroy = function () {',
  31791. ' };',
  31792. ' var $r = this.$rtti;',
  31793. ' $r.addMethod("PrivateProc", 0, [["a", rtl.word]], 0);',
  31794. ' $r.addMethod("ProtectedFunc", 5, [], 1, rtl.word);',
  31795. ' $r.addMethod("PublicProc", 4, []);',
  31796. ' $r.addMethod("Create", 2, []);',
  31797. ' $r.addMethod("Destroy", 3, []);',
  31798. ' $r.addMethod("PublishedProc", 1, [], 3, rtl.word);',
  31799. '});',
  31800. '']),
  31801. LinesToStr([ // $mod.$main
  31802. '']));
  31803. end;
  31804. procedure TTestModule.TestRTTI_Class_Property;
  31805. begin
  31806. WithTypeInfo:=true;
  31807. StartProgram(false);
  31808. Add('{$modeswitch externalclass}');
  31809. Add('type');
  31810. Add(' TObject = class');
  31811. Add(' private');
  31812. Add(' FColor: longint;');
  31813. Add(' FColorStored: boolean;');
  31814. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  31815. Add(' function GetColor: longint; virtual; abstract;');
  31816. Add(' function GetColorStored: boolean; virtual; abstract;');
  31817. Add(' FExtSize: longint external name ''$extSize'';');
  31818. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  31819. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  31820. Add(' function GetExtSize: longint; external name ''$getSize'';');
  31821. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  31822. Add(' published');
  31823. Add(' property ColorA: longint read FColor;');
  31824. Add(' property ColorB: longint write FColor;');
  31825. Add(' property ColorC: longint read GetColor write SetColor;');
  31826. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  31827. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  31828. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  31829. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  31830. Add(' end;');
  31831. Add('begin');
  31832. ConvertProgram;
  31833. CheckSource('TestRTTI_Class_Property',
  31834. LinesToStr([ // statements
  31835. 'rtl.createClass(this, "TObject", null, function () {',
  31836. ' this.$init = function () {',
  31837. ' this.FColor = 0;',
  31838. ' this.FColorStored = false;',
  31839. ' };',
  31840. ' this.$final = function () {',
  31841. ' };',
  31842. ' var $r = this.$rtti;',
  31843. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  31844. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  31845. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  31846. ' $r.addProperty(',
  31847. ' "ColorD",',
  31848. ' 8,',
  31849. ' rtl.longint,',
  31850. ' "FColor",',
  31851. ' "FColor",',
  31852. ' 4,',
  31853. ' {',
  31854. ' stored: "FColorStored"',
  31855. ' }',
  31856. ' );',
  31857. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  31858. ' $r.addProperty(',
  31859. ' "ExtSizeB",',
  31860. ' 11,',
  31861. ' rtl.longint,',
  31862. ' "$getSize",',
  31863. ' "$setSize",',
  31864. ' 4,',
  31865. ' {',
  31866. ' stored: "$extSizeStored"',
  31867. ' }',
  31868. ' );',
  31869. ' $r.addProperty(',
  31870. ' "ExtSizeC",',
  31871. ' 12,',
  31872. ' rtl.longint,',
  31873. ' "$extSize",',
  31874. ' "$extSize",',
  31875. ' 4,',
  31876. ' {',
  31877. ' stored: "$getExtSizeStored"',
  31878. ' }',
  31879. ' );',
  31880. '});',
  31881. '']),
  31882. LinesToStr([ // $mod.$main
  31883. '']));
  31884. end;
  31885. procedure TTestModule.TestRTTI_Class_PropertyParams;
  31886. begin
  31887. WithTypeInfo:=true;
  31888. StartProgram(false);
  31889. Add('{$modeswitch externalclass}');
  31890. Add('type');
  31891. Add(' integer = longint;');
  31892. Add(' TObject = class');
  31893. Add(' private');
  31894. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  31895. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  31896. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  31897. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  31898. Add(' published');
  31899. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  31900. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  31901. Add(' end;');
  31902. Add('begin');
  31903. ConvertProgram;
  31904. CheckSource('TestRTTI_Class_PropertyParams',
  31905. LinesToStr([ // statements
  31906. 'rtl.createClass(this, "TObject", null, function () {',
  31907. ' this.$init = function () {',
  31908. ' };',
  31909. ' this.$final = function () {',
  31910. ' };',
  31911. ' var $r = this.$rtti;',
  31912. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  31913. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  31914. '});',
  31915. '']),
  31916. LinesToStr([ // $mod.$main
  31917. '']));
  31918. end;
  31919. procedure TTestModule.TestRTTI_Class_PropertyPrivate;
  31920. begin
  31921. WithTypeInfo:=true;
  31922. StartProgram(false);
  31923. Add('type');
  31924. Add('{$RTTI explicit properties([vcPrivate,vcProtected,vcPublic,vcPublished])}');
  31925. Add(' TObject = class');
  31926. Add(' private');
  31927. Add(' FWord: word;');
  31928. Add(' function GetWord: word; virtual; abstract;');
  31929. Add(' procedure SetWord(Value: word); virtual; abstract;');
  31930. Add(' property PrivateWord: word read FWord write FWord;');
  31931. Add(' protected');
  31932. Add(' property ProtectedWord: word read FWord write SetWord;');
  31933. Add(' public');
  31934. Add(' property PublicWord: word read GetWord;');
  31935. Add(' published');
  31936. Add(' property PublishedWord: word read FWord;');
  31937. Add(' end;');
  31938. Add('begin');
  31939. ConvertProgram;
  31940. CheckSource('TestRTTI_Class_PropertyPrivate',
  31941. LinesToStr([ // statements
  31942. 'rtl.createClass(this, "TObject", null, function () {',
  31943. ' this.$init = function () {',
  31944. ' this.FWord = 0;',
  31945. ' };',
  31946. ' this.$final = function () {',
  31947. ' };',
  31948. ' var $r = this.$rtti;',
  31949. ' $r.addProperty(',
  31950. ' "PrivateWord",',
  31951. ' 0,',
  31952. ' rtl.word,',
  31953. ' "FWord",',
  31954. ' "FWord",',
  31955. ' 0',
  31956. ' );',
  31957. ' $r.addProperty(',
  31958. ' "ProtectedWord",',
  31959. ' 2,',
  31960. ' rtl.word,',
  31961. ' "FWord",',
  31962. ' "SetWord",',
  31963. ' 1',
  31964. ' );',
  31965. ' $r.addProperty("PublicWord", 1, rtl.word, "GetWord", "", 2);',
  31966. ' $r.addProperty(',
  31967. ' "PublishedWord",',
  31968. ' 0,',
  31969. ' rtl.word,',
  31970. ' "FWord",',
  31971. ' "",',
  31972. ' 3',
  31973. ' );',
  31974. '});',
  31975. '']),
  31976. LinesToStr([ // $mod.$main
  31977. '']));
  31978. end;
  31979. procedure TTestModule.TestRTTI_Class_ClassProperty;
  31980. begin
  31981. WithTypeInfo:=true;
  31982. StartProgram(false);
  31983. Add('type');
  31984. Add('{$RTTI explicit properties([vcPrivate,vcProtected,vcPublic,vcPublished])}');
  31985. Add(' TObject = class');
  31986. Add(' private');
  31987. Add(' class var FWord: word;');
  31988. Add(' class function GetWord: word; virtual; abstract;');
  31989. Add(' class procedure SetWord(Value: word); virtual; abstract;');
  31990. Add(' class property PrivateWord: word read FWord write FWord;');
  31991. Add(' protected');
  31992. Add(' class property ProtectedWord: word read FWord write SetWord;');
  31993. Add(' public');
  31994. Add(' class property PublicWord: word read GetWord;');
  31995. Add(' end;');
  31996. Add('begin');
  31997. ConvertProgram;
  31998. CheckSource('TestRTTI_Class_ClassProperty',
  31999. LinesToStr([ // statements
  32000. 'rtl.createClass(this, "TObject", null, function () {',
  32001. ' this.FWord = 0;',
  32002. ' this.$init = function () {',
  32003. ' };',
  32004. ' this.$final = function () {',
  32005. ' };',
  32006. ' var $r = this.$rtti;',
  32007. ' $r.addProperty(',
  32008. ' "PrivateWord",',
  32009. ' 32,',
  32010. ' rtl.word,',
  32011. ' "FWord",',
  32012. ' "FWord",',
  32013. ' 0',
  32014. ' );',
  32015. ' $r.addProperty(',
  32016. ' "ProtectedWord",',
  32017. ' 34,',
  32018. ' rtl.word,',
  32019. ' "FWord",',
  32020. ' "SetWord",',
  32021. ' 1',
  32022. ' );',
  32023. ' $r.addProperty(',
  32024. ' "PublicWord",',
  32025. ' 33,',
  32026. ' rtl.word,',
  32027. ' "GetWord",',
  32028. ' "",',
  32029. ' 2',
  32030. ' );',
  32031. '});',
  32032. '']),
  32033. LinesToStr([ // $mod.$main
  32034. '']));
  32035. end;
  32036. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  32037. begin
  32038. WithTypeInfo:=true;
  32039. AddModuleWithIntfImplSrc('unit1.pas',
  32040. 'type TColor = -5..5;',
  32041. '');
  32042. StartProgram(true);
  32043. Add([
  32044. 'uses unit1;',
  32045. 'type',
  32046. ' TColorAlias = TColor;',
  32047. ' TColorTypeAlias = type TColor;',
  32048. ' TObject = class',
  32049. ' private',
  32050. ' fColor: TColor;',
  32051. ' fAlias: TColorAlias;',
  32052. ' fTypeAlias: TColorTypeAlias;',
  32053. ' published',
  32054. ' property Color: TColor read fcolor;',
  32055. ' property Alias: TColorAlias read falias;',
  32056. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  32057. ' end;',
  32058. 'begin',
  32059. '']);
  32060. ConvertProgram;
  32061. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  32062. LinesToStr([ // statements
  32063. 'this.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  32064. 'rtl.createClass(this, "TObject", null, function () {',
  32065. ' this.$init = function () {',
  32066. ' this.fColor = 0;',
  32067. ' this.fAlias = 0;',
  32068. ' this.fTypeAlias = 0;',
  32069. ' };',
  32070. ' this.$final = function () {',
  32071. ' };',
  32072. ' var $r = this.$rtti;',
  32073. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  32074. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  32075. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  32076. '});',
  32077. '']),
  32078. LinesToStr([ // $mod.$main
  32079. '']));
  32080. end;
  32081. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  32082. begin
  32083. WithTypeInfo:=true;
  32084. StartProgram(false);
  32085. Add([
  32086. '{$modeswitch omitrtti}',
  32087. 'type',
  32088. ' TObject = class',
  32089. ' private',
  32090. ' FA: byte;',
  32091. ' published',
  32092. ' property A: byte read FA write FA;',
  32093. ' end;',
  32094. 'begin']);
  32095. ConvertProgram;
  32096. CheckSource('TestRTTI_Class_OmitRTTI',
  32097. LinesToStr([ // statements
  32098. 'rtl.createClass(this, "TObject", null, function () {',
  32099. ' this.$init = function () {',
  32100. ' this.FA = 0;',
  32101. ' };',
  32102. ' this.$final = function () {',
  32103. ' };',
  32104. '});',
  32105. '']),
  32106. LinesToStr([ // $mod.$main
  32107. '']));
  32108. end;
  32109. procedure TTestModule.TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  32110. begin
  32111. WithTypeInfo:=true;
  32112. StartUnit(true,[supTObject]);
  32113. Add([
  32114. 'interface',
  32115. 'type',
  32116. ' {$M+}',
  32117. ' TBird = class',
  32118. ' published',
  32119. ' Swarm: array of TBird;',
  32120. ' end;',
  32121. 'implementation',
  32122. '']);
  32123. ConvertUnit;
  32124. CheckSource('TestRTTI_Class_Field_AnonymousArrayOfSelfClass',
  32125. LinesToStr([ // statements
  32126. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  32127. ' this.$init = function () {',
  32128. ' pas.system.TObject.$init.call(this);',
  32129. ' this.Swarm = [];',
  32130. ' };',
  32131. ' this.$final = function () {',
  32132. ' this.Swarm = undefined;',
  32133. ' pas.system.TObject.$final.call(this);',
  32134. ' };',
  32135. ' var $r = this.$rtti;',
  32136. ' $mod.$rtti.$DynArray("TBird.Swarm$a", {',
  32137. ' eltype: $r',
  32138. ' });',
  32139. ' $r.addField("Swarm", $mod.$rtti["TBird.Swarm$a"], 4);',
  32140. '});',
  32141. '']),
  32142. LinesToStr([ // $mod.$main
  32143. '']));
  32144. end;
  32145. procedure TTestModule.TestRTTI_IndexModifier;
  32146. begin
  32147. WithTypeInfo:=true;
  32148. StartProgram(false);
  32149. Add([
  32150. 'type',
  32151. ' TEnum = (red, blue);',
  32152. ' TObject = class',
  32153. ' FB: boolean;',
  32154. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  32155. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  32156. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  32157. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  32158. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  32159. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  32160. ' published',
  32161. ' property B1: boolean index 1 read FB write SetIntBool;',
  32162. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  32163. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  32164. ' end;',
  32165. 'begin']);
  32166. ConvertProgram;
  32167. CheckSource('TestRTTI_IndexModifier',
  32168. LinesToStr([ // statements
  32169. 'this.TEnum = {',
  32170. ' "0": "red",',
  32171. ' red: 0,',
  32172. ' "1": "blue",',
  32173. ' blue: 1',
  32174. '};',
  32175. 'this.$rtti.$Enum("TEnum", {',
  32176. ' minvalue: 0,',
  32177. ' maxvalue: 1,',
  32178. ' ordtype: 1,',
  32179. ' enumtype: this.TEnum',
  32180. '});',
  32181. 'rtl.createClass(this, "TObject", null, function () {',
  32182. ' this.$init = function () {',
  32183. ' this.FB = false;',
  32184. ' };',
  32185. ' this.$final = function () {',
  32186. ' };',
  32187. ' var $r = this.$rtti;',
  32188. ' $r.addProperty(',
  32189. ' "B1",',
  32190. ' 18,',
  32191. ' rtl.boolean,',
  32192. ' "FB",',
  32193. ' "SetIntBool",',
  32194. ' 4,',
  32195. ' {',
  32196. ' index: 1',
  32197. ' }',
  32198. ' );',
  32199. ' $r.addProperty(',
  32200. ' "B2",',
  32201. ' 17,',
  32202. ' rtl.boolean,',
  32203. ' "GetEnumBool",',
  32204. ' "FB",',
  32205. ' 4,',
  32206. ' {',
  32207. ' index: $mod.TEnum.blue',
  32208. ' }',
  32209. ' );',
  32210. ' $r.addProperty(',
  32211. ' "I1",',
  32212. ' 19,',
  32213. ' rtl.boolean,',
  32214. ' "GetStrIntBool",',
  32215. ' "SetStrIntBool",',
  32216. ' 4,',
  32217. ' {',
  32218. ' index: 2',
  32219. ' }',
  32220. ' );',
  32221. '});',
  32222. '']),
  32223. LinesToStr([ // $mod.$main
  32224. '']));
  32225. end;
  32226. procedure TTestModule.TestRTTI_StoredModifier;
  32227. begin
  32228. WithTypeInfo:=true;
  32229. StartProgram(false);
  32230. Add([
  32231. 'const',
  32232. ' ConstB = true;',
  32233. 'type',
  32234. ' TObject = class',
  32235. ' private',
  32236. ' FB: boolean;',
  32237. ' function IsBStored: boolean; virtual; abstract;',
  32238. ' published',
  32239. ' property BoolA: boolean read FB stored true;',
  32240. ' property BoolB: boolean read FB stored false;',
  32241. ' property BoolC: boolean read FB stored FB;',
  32242. ' property BoolD: boolean read FB stored ConstB;',
  32243. ' property BoolE: boolean read FB stored IsBStored;',
  32244. ' end;',
  32245. 'begin']);
  32246. ConvertProgram;
  32247. CheckSource('TestRTTI_StoredModifier',
  32248. LinesToStr([ // statements
  32249. 'this.ConstB = true;',
  32250. 'rtl.createClass(this, "TObject", null, function () {',
  32251. ' this.$init = function () {',
  32252. ' this.FB = false;',
  32253. ' };',
  32254. ' this.$final = function () {',
  32255. ' };',
  32256. ' var $r = this.$rtti;',
  32257. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  32258. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  32259. ' $r.addProperty(',
  32260. ' "BoolC",',
  32261. ' 8,',
  32262. ' rtl.boolean,',
  32263. ' "FB",',
  32264. ' "",',
  32265. ' 4,',
  32266. ' {',
  32267. ' stored: "FB"',
  32268. ' }',
  32269. ' );',
  32270. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  32271. ' $r.addProperty(',
  32272. ' "BoolE",',
  32273. ' 12,',
  32274. ' rtl.boolean,',
  32275. ' "FB",',
  32276. ' "",',
  32277. ' 4,',
  32278. ' {',
  32279. ' stored: "IsBStored"',
  32280. ' }',
  32281. ' );',
  32282. '});',
  32283. '']),
  32284. LinesToStr([ // $mod.$main
  32285. '']));
  32286. end;
  32287. procedure TTestModule.TestRTTI_DefaultValue;
  32288. begin
  32289. WithTypeInfo:=true;
  32290. StartProgram(false);
  32291. Add([
  32292. 'type',
  32293. ' TEnum = (red, blue);',
  32294. 'const',
  32295. ' CB = true or false;',
  32296. ' CI = 1+2;',
  32297. 'type',
  32298. ' TObject = class',
  32299. ' FB: boolean;',
  32300. ' FI: longint;',
  32301. ' FE: TEnum;',
  32302. ' published',
  32303. ' property B1: boolean read FB default true;',
  32304. ' property B2: boolean read FB default CB;',
  32305. ' property B3: boolean read FB default test1.cb;',
  32306. ' property I1: longint read FI default 2;',
  32307. ' property I2: longint read FI default CI;',
  32308. ' property E1: TEnum read FE default red;',
  32309. ' property E2: TEnum read FE default TEnum.blue;',
  32310. ' end;',
  32311. 'begin']);
  32312. ConvertProgram;
  32313. CheckSource('TestRTTI_DefaultValue',
  32314. LinesToStr([ // statements
  32315. 'this.TEnum = {',
  32316. ' "0": "red",',
  32317. ' red: 0,',
  32318. ' "1": "blue",',
  32319. ' blue: 1',
  32320. '};',
  32321. 'this.$rtti.$Enum("TEnum", {',
  32322. ' minvalue: 0,',
  32323. ' maxvalue: 1,',
  32324. ' ordtype: 1,',
  32325. ' enumtype: this.TEnum',
  32326. '});',
  32327. 'this.CB = true || false;',
  32328. 'this.CI = 1 + 2;',
  32329. 'rtl.createClass(this, "TObject", null, function () {',
  32330. ' this.$init = function () {',
  32331. ' this.FB = false;',
  32332. ' this.FI = 0;',
  32333. ' this.FE = 0;',
  32334. ' };',
  32335. ' this.$final = function () {',
  32336. ' };',
  32337. ' var $r = this.$rtti;',
  32338. ' $r.addProperty(',
  32339. ' "B1",',
  32340. ' 0,',
  32341. ' rtl.boolean,',
  32342. ' "FB",',
  32343. ' "",',
  32344. ' 4,',
  32345. ' {',
  32346. ' Default: true',
  32347. ' }',
  32348. ' );',
  32349. ' $r.addProperty(',
  32350. ' "B2",',
  32351. ' 0,',
  32352. ' rtl.boolean,',
  32353. ' "FB",',
  32354. ' "",',
  32355. ' 4,',
  32356. ' {',
  32357. ' Default: true',
  32358. ' }',
  32359. ' );',
  32360. ' $r.addProperty(',
  32361. ' "B3",',
  32362. ' 0,',
  32363. ' rtl.boolean,',
  32364. ' "FB",',
  32365. ' "",',
  32366. ' 4,',
  32367. ' {',
  32368. ' Default: true',
  32369. ' }',
  32370. ' );',
  32371. ' $r.addProperty(',
  32372. ' "I1",',
  32373. ' 0,',
  32374. ' rtl.longint,',
  32375. ' "FI",',
  32376. ' "",',
  32377. ' 4,',
  32378. ' {',
  32379. ' Default: 2',
  32380. ' }',
  32381. ' );',
  32382. ' $r.addProperty(',
  32383. ' "I2",',
  32384. ' 0,',
  32385. ' rtl.longint,',
  32386. ' "FI",',
  32387. ' "",',
  32388. ' 4,',
  32389. ' {',
  32390. ' Default: 3',
  32391. ' }',
  32392. ' );',
  32393. ' $r.addProperty(',
  32394. ' "E1",',
  32395. ' 0,',
  32396. ' $mod.$rtti["TEnum"],',
  32397. ' "FE",',
  32398. ' "",',
  32399. ' 4,',
  32400. ' {',
  32401. ' Default: $mod.TEnum.red',
  32402. ' }',
  32403. ' );',
  32404. ' $r.addProperty(',
  32405. ' "E2",',
  32406. ' 0,',
  32407. ' $mod.$rtti["TEnum"],',
  32408. ' "FE",',
  32409. ' "",',
  32410. ' 4,',
  32411. ' {',
  32412. ' Default: $mod.TEnum.blue',
  32413. ' }',
  32414. ' );',
  32415. '});',
  32416. '']),
  32417. LinesToStr([ // $mod.$main
  32418. '']));
  32419. end;
  32420. procedure TTestModule.TestRTTI_DefaultValueSet;
  32421. begin
  32422. WithTypeInfo:=true;
  32423. StartProgram(false);
  32424. Add([
  32425. 'type',
  32426. ' TEnum = (red, blue);',
  32427. ' TSet = set of TEnum;',
  32428. 'const',
  32429. ' CSet = [red,blue];',
  32430. 'type',
  32431. ' TObject = class',
  32432. ' FSet: TSet;',
  32433. ' published',
  32434. ' property Set1: TSet read FSet default [];',
  32435. ' property Set2: TSet read FSet default [red];',
  32436. ' property Set3: TSet read FSet default [red,blue];',
  32437. ' property Set4: TSet read FSet default CSet;',
  32438. ' end;',
  32439. 'begin']);
  32440. ConvertProgram;
  32441. CheckSource('TestRTTI_DefaultValueSet',
  32442. LinesToStr([ // statements
  32443. 'this.TEnum = {',
  32444. ' "0": "red",',
  32445. ' red: 0,',
  32446. ' "1": "blue",',
  32447. ' blue: 1',
  32448. '};',
  32449. 'this.$rtti.$Enum("TEnum", {',
  32450. ' minvalue: 0,',
  32451. ' maxvalue: 1,',
  32452. ' ordtype: 1,',
  32453. ' enumtype: this.TEnum',
  32454. '});',
  32455. 'this.$rtti.$Set("TSet", {',
  32456. ' comptype: this.$rtti["TEnum"]',
  32457. '});',
  32458. 'this.CSet = rtl.createSet(this.TEnum.red, this.TEnum.blue);',
  32459. 'rtl.createClass(this, "TObject", null, function () {',
  32460. ' this.$init = function () {',
  32461. ' this.FSet = {};',
  32462. ' };',
  32463. ' this.$final = function () {',
  32464. ' this.FSet = undefined;',
  32465. ' };',
  32466. ' var $r = this.$rtti;',
  32467. ' $r.addProperty(',
  32468. ' "Set1",',
  32469. ' 0,',
  32470. ' $mod.$rtti["TSet"],',
  32471. ' "FSet",',
  32472. ' "",',
  32473. ' 4,',
  32474. ' {',
  32475. ' Default: {}',
  32476. ' }',
  32477. ' );',
  32478. ' $r.addProperty(',
  32479. ' "Set2",',
  32480. ' 0,',
  32481. ' $mod.$rtti["TSet"],',
  32482. ' "FSet",',
  32483. ' "",',
  32484. ' 4,',
  32485. ' {',
  32486. ' Default: rtl.createSet($mod.TEnum.red)',
  32487. ' }',
  32488. ' );',
  32489. ' $r.addProperty(',
  32490. ' "Set3",',
  32491. ' 0,',
  32492. ' $mod.$rtti["TSet"],',
  32493. ' "FSet",',
  32494. ' "",',
  32495. ' 4,',
  32496. ' {',
  32497. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  32498. ' }',
  32499. ' );',
  32500. ' $r.addProperty(',
  32501. ' "Set4",',
  32502. ' 0,',
  32503. ' $mod.$rtti["TSet"],',
  32504. ' "FSet",',
  32505. ' "",',
  32506. ' 4,',
  32507. ' {',
  32508. ' Default: $mod.CSet',
  32509. ' }',
  32510. ' );',
  32511. '});',
  32512. '']),
  32513. LinesToStr([ // $mod.$main
  32514. '']));
  32515. end;
  32516. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  32517. begin
  32518. WithTypeInfo:=true;
  32519. StartProgram(false);
  32520. Add([
  32521. 'type',
  32522. ' TRg = -1..1;',
  32523. 'const',
  32524. ' l = low(TRg);',
  32525. ' h = high(TRg);',
  32526. 'type',
  32527. ' TObject = class',
  32528. ' FV: TRg;',
  32529. ' published',
  32530. ' property V1: TRg read FV default -1;',
  32531. ' end;',
  32532. 'begin']);
  32533. ConvertProgram;
  32534. CheckSource('TestRTTI_DefaultValueRangeType',
  32535. LinesToStr([ // statements
  32536. 'this.$rtti.$Int("TRg", {',
  32537. ' minvalue: -1,',
  32538. ' maxvalue: 1,',
  32539. ' ordtype: 0',
  32540. '});',
  32541. 'this.l = -1;',
  32542. 'this.h = 1;',
  32543. 'rtl.createClass(this, "TObject", null, function () {',
  32544. ' this.$init = function () {',
  32545. ' this.FV = 0;',
  32546. ' };',
  32547. ' this.$final = function () {',
  32548. ' };',
  32549. ' var $r = this.$rtti;',
  32550. ' $r.addProperty(',
  32551. ' "V1",',
  32552. ' 0,',
  32553. ' $mod.$rtti["TRg"],',
  32554. ' "FV",',
  32555. ' "",',
  32556. ' 4,',
  32557. ' {',
  32558. ' Default: -1',
  32559. ' }',
  32560. ' );',
  32561. '});',
  32562. '']),
  32563. LinesToStr([ // $mod.$main
  32564. '']));
  32565. end;
  32566. procedure TTestModule.TestRTTI_DefaultValueInherit;
  32567. begin
  32568. WithTypeInfo:=true;
  32569. StartProgram(false);
  32570. Add([
  32571. 'type',
  32572. ' TObject = class',
  32573. ' FA, FB: byte;',
  32574. ' property A: byte read FA default 1;',
  32575. ' property B: byte read FB default 2;',
  32576. ' end;',
  32577. ' TBird = class',
  32578. ' published',
  32579. ' property A;',
  32580. ' property B nodefault;',
  32581. ' end;',
  32582. 'begin']);
  32583. ConvertProgram;
  32584. CheckSource('TestRTTI_DefaultValueInherit',
  32585. LinesToStr([ // statements
  32586. 'rtl.createClass(this, "TObject", null, function () {',
  32587. ' this.$init = function () {',
  32588. ' this.FA = 0;',
  32589. ' this.FB = 0;',
  32590. ' };',
  32591. ' this.$final = function () {',
  32592. ' };',
  32593. '});',
  32594. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32595. ' var $r = this.$rtti;',
  32596. ' $r.addProperty(',
  32597. ' "A",',
  32598. ' 0,',
  32599. ' rtl.byte,',
  32600. ' "FA",',
  32601. ' "",',
  32602. ' 4,',
  32603. ' {',
  32604. ' Default: 1',
  32605. ' }',
  32606. ' );',
  32607. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  32608. '});',
  32609. '']),
  32610. LinesToStr([ // $mod.$main
  32611. '']));
  32612. end;
  32613. procedure TTestModule.TestRTTI_OverrideMethod;
  32614. begin
  32615. WithTypeInfo:=true;
  32616. StartProgram(false);
  32617. Add('type');
  32618. Add(' TObject = class');
  32619. Add(' published');
  32620. Add(' procedure DoIt; virtual; abstract;');
  32621. Add(' end;');
  32622. Add(' TSky = class');
  32623. Add(' published');
  32624. Add(' procedure DoIt; override;');
  32625. Add(' end;');
  32626. Add('procedure TSky.DoIt; begin end;');
  32627. Add('begin');
  32628. ConvertProgram;
  32629. CheckSource('TestRTTI_OverrideMethod',
  32630. LinesToStr([ // statements
  32631. 'rtl.createClass(this, "TObject", null, function () {',
  32632. ' this.$init = function () {',
  32633. ' };',
  32634. ' this.$final = function () {',
  32635. ' };',
  32636. ' var $r = this.$rtti;',
  32637. ' $r.addMethod("DoIt", 0, [], 4);',
  32638. '});',
  32639. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  32640. ' this.DoIt = function () {',
  32641. ' };',
  32642. '});',
  32643. '']),
  32644. LinesToStr([ // $mod.$main
  32645. '']));
  32646. end;
  32647. procedure TTestModule.TestRTTI_ReintroduceMethod;
  32648. begin
  32649. WithTypeInfo:=true;
  32650. StartProgram(false);
  32651. Add([
  32652. 'type',
  32653. ' TObject = class',
  32654. ' published',
  32655. ' procedure DoIt;',
  32656. ' end;',
  32657. ' TSky = class',
  32658. ' published',
  32659. ' procedure DoIt; reintroduce;',
  32660. ' end;',
  32661. 'procedure TObject.DoIt; begin end;',
  32662. 'procedure TSky.DoIt;',
  32663. 'begin',
  32664. ' inherited DoIt;',
  32665. 'end;',
  32666. 'begin']);
  32667. ConvertProgram;
  32668. CheckSource('TestRTTI_ReintroduceMethod',
  32669. LinesToStr([ // statements
  32670. 'rtl.createClass(this, "TObject", null, function () {',
  32671. ' this.$init = function () {',
  32672. ' };',
  32673. ' this.$final = function () {',
  32674. ' };',
  32675. ' this.DoIt = function () {',
  32676. ' };',
  32677. ' var $r = this.$rtti;',
  32678. ' $r.addMethod("DoIt", 0, [], 4);',
  32679. '});',
  32680. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  32681. ' this.DoIt = function () {',
  32682. ' $mod.TObject.DoIt.call(this);',
  32683. ' };',
  32684. ' var $r = this.$rtti;',
  32685. ' $r.addMethod("DoIt", 0, [], 4);',
  32686. '});',
  32687. '']),
  32688. LinesToStr([ // $mod.$main
  32689. '']));
  32690. end;
  32691. procedure TTestModule.TestRTTI_OverloadProperty;
  32692. begin
  32693. WithTypeInfo:=true;
  32694. StartProgram(false);
  32695. Add('type');
  32696. Add(' TObject = class');
  32697. Add(' protected');
  32698. Add(' FFlag: longint;');
  32699. Add(' published');
  32700. Add(' property Flag: longint read fflag;');
  32701. Add(' end;');
  32702. Add(' TSky = class');
  32703. Add(' published');
  32704. Add(' property FLAG: longint write fflag;');
  32705. Add(' end;');
  32706. Add('begin');
  32707. ConvertProgram;
  32708. CheckSource('TestRTTI_OverrideMethod',
  32709. LinesToStr([ // statements
  32710. 'rtl.createClass(this, "TObject", null, function () {',
  32711. ' this.$init = function () {',
  32712. ' this.FFlag = 0;',
  32713. ' };',
  32714. ' this.$final = function () {',
  32715. ' };',
  32716. ' var $r = this.$rtti;',
  32717. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  32718. '});',
  32719. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  32720. ' var $r = this.$rtti;',
  32721. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  32722. '});',
  32723. '']),
  32724. LinesToStr([ // $mod.$main
  32725. '']));
  32726. end;
  32727. procedure TTestModule.TestRTTI_ClassForward;
  32728. begin
  32729. WithTypeInfo:=true;
  32730. StartProgram(false);
  32731. Add('type');
  32732. Add(' TObject = class end;');
  32733. Add(' tbridge = class;');
  32734. Add(' TProc = function: tbridge;');
  32735. Add(' TOger = class');
  32736. Add(' published');
  32737. Add(' FBridge: tbridge;');
  32738. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  32739. Add(' property Bridge: tbridge read fbridge write setbridge;');
  32740. Add(' end;');
  32741. Add(' TBridge = class');
  32742. Add(' FOger: toger;');
  32743. Add(' end;');
  32744. Add('var p: Pointer;');
  32745. Add(' b: tbridge;');
  32746. Add('begin');
  32747. Add(' p:=typeinfo(tbridge);');
  32748. Add(' p:=typeinfo(b);');
  32749. ConvertProgram;
  32750. CheckSource('TestRTTI_ClassForward',
  32751. LinesToStr([ // statements
  32752. 'rtl.createClass(this, "TObject", null, function () {',
  32753. ' this.$init = function () {',
  32754. ' };',
  32755. ' this.$final = function () {',
  32756. ' };',
  32757. '});',
  32758. 'this.$rtti.$Class("TBridge");',
  32759. 'this.$rtti.$ProcVar("TProc", {',
  32760. ' procsig: rtl.newTIProcSig([], this.$rtti["TBridge"])',
  32761. '});',
  32762. 'rtl.createClass(this, "TOger", this.TObject, function () {',
  32763. ' this.$init = function () {',
  32764. ' $mod.TObject.$init.call(this);',
  32765. ' this.FBridge = null;',
  32766. ' };',
  32767. ' this.$final = function () {',
  32768. ' this.FBridge = undefined;',
  32769. ' $mod.TObject.$final.call(this);',
  32770. ' };',
  32771. ' var $r = this.$rtti;',
  32772. ' $r.addField("FBridge", $mod.$rtti["TBridge"], 4);',
  32773. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]], 4);',
  32774. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  32775. '});',
  32776. 'rtl.createClass(this, "TBridge", this.TObject, function () {',
  32777. ' this.$init = function () {',
  32778. ' $mod.TObject.$init.call(this);',
  32779. ' this.FOger = null;',
  32780. ' };',
  32781. ' this.$final = function () {',
  32782. ' this.FOger = undefined;',
  32783. ' $mod.TObject.$final.call(this);',
  32784. ' };',
  32785. '});',
  32786. 'this.p = null;',
  32787. 'this.b = null;',
  32788. '']),
  32789. LinesToStr([ // $mod.$main
  32790. '$mod.p = $mod.$rtti["TBridge"];',
  32791. '$mod.p = $mod.b.$rtti;',
  32792. '']));
  32793. end;
  32794. procedure TTestModule.TestRTTI_ClassOf;
  32795. begin
  32796. WithTypeInfo:=true;
  32797. StartProgram(false);
  32798. Add('type');
  32799. Add(' TClass = class of tobject;');
  32800. Add(' TProcA = function: TClass;');
  32801. Add(' TObject = class');
  32802. Add(' published');
  32803. Add(' C: tclass;');
  32804. Add(' end;');
  32805. Add(' tfox = class;');
  32806. Add(' TBird = class end;');
  32807. Add(' TBirds = class of tbird;');
  32808. Add(' TFox = class end;');
  32809. Add(' TFoxes = class of tfox;');
  32810. Add(' TCows = class of TCow;');
  32811. Add(' TCow = class;');
  32812. Add(' TCow = class end;');
  32813. Add('begin');
  32814. ConvertProgram;
  32815. CheckSource('TestRTTI_ClassOf',
  32816. LinesToStr([ // statements
  32817. 'this.$rtti.$Class("TObject");',
  32818. 'this.$rtti.$ClassRef("TClass", {',
  32819. ' instancetype: this.$rtti["TObject"]',
  32820. '});',
  32821. 'this.$rtti.$ProcVar("TProcA", {',
  32822. ' procsig: rtl.newTIProcSig([], this.$rtti["TClass"])',
  32823. '});',
  32824. 'rtl.createClass(this, "TObject", null, function () {',
  32825. ' this.$init = function () {',
  32826. ' this.C = null;',
  32827. ' };',
  32828. ' this.$final = function () {',
  32829. ' this.C = undefined;',
  32830. ' };',
  32831. ' var $r = this.$rtti;',
  32832. ' $r.addField("C", $mod.$rtti["TClass"], 4);',
  32833. '});',
  32834. 'this.$rtti.$Class("TFox");',
  32835. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32836. '});',
  32837. 'this.$rtti.$ClassRef("TBirds", {',
  32838. ' instancetype: this.$rtti["TBird"]',
  32839. '});',
  32840. 'rtl.createClass(this, "TFox", this.TObject, function () {',
  32841. '});',
  32842. 'this.$rtti.$ClassRef("TFoxes", {',
  32843. ' instancetype: this.$rtti["TFox"]',
  32844. '});',
  32845. 'this.$rtti.$Class("TCow");',
  32846. 'this.$rtti.$ClassRef("TCows", {',
  32847. ' instancetype: this.$rtti["TCow"]',
  32848. '});',
  32849. 'rtl.createClass(this, "TCow", this.TObject, function () {',
  32850. '});',
  32851. '']),
  32852. LinesToStr([ // $mod.$main
  32853. '']));
  32854. end;
  32855. procedure TTestModule.TestRTTI_Record;
  32856. begin
  32857. WithTypeInfo:=true;
  32858. StartProgram(false);
  32859. Add('type');
  32860. Add(' integer = longint;');
  32861. Add(' TPoint = record');
  32862. Add(' x,y: integer;');
  32863. Add(' end;');
  32864. Add('var p: pointer;');
  32865. Add(' r: tpoint;');
  32866. Add('begin');
  32867. Add(' p:=typeinfo(tpoint);');
  32868. Add(' p:=typeinfo(r);');
  32869. Add(' p:=typeinfo(r.x);');
  32870. ConvertProgram;
  32871. CheckSource('TestRTTI_Record',
  32872. LinesToStr([ // statements
  32873. 'rtl.recNewT(this, "TPoint", function () {',
  32874. ' this.x = 0;',
  32875. ' this.y = 0;',
  32876. ' this.$eq = function (b) {',
  32877. ' return (this.x === b.x) && (this.y === b.y);',
  32878. ' };',
  32879. ' this.$assign = function (s) {',
  32880. ' this.x = s.x;',
  32881. ' this.y = s.y;',
  32882. ' return this;',
  32883. ' };',
  32884. ' var $r = $mod.$rtti.$Record("TPoint", {}, this);',
  32885. ' $r.addField("x", rtl.longint);',
  32886. ' $r.addField("y", rtl.longint);',
  32887. '});',
  32888. 'this.p = null;',
  32889. 'this.r = this.TPoint.$new();',
  32890. '']),
  32891. LinesToStr([ // $mod.$main
  32892. '$mod.p = $mod.$rtti["TPoint"];',
  32893. '$mod.p = $mod.$rtti["TPoint"];',
  32894. '$mod.p = rtl.longint;',
  32895. '']));
  32896. end;
  32897. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  32898. begin
  32899. WithTypeInfo:=true;
  32900. StartProgram(false);
  32901. Add('type');
  32902. Add(' TFloatRec = record');
  32903. Add(' c,d: array of char;');
  32904. // Add(' i: array of array of longint;');
  32905. Add(' end;');
  32906. Add('var p: pointer;');
  32907. Add(' r: tfloatrec;');
  32908. Add('begin');
  32909. Add(' p:=typeinfo(tfloatrec);');
  32910. Add(' p:=typeinfo(r);');
  32911. Add(' p:=typeinfo(r.d);');
  32912. ConvertProgram;
  32913. CheckSource('TestRTTI_Record',
  32914. LinesToStr([ // statements
  32915. 'rtl.recNewT(this, "TFloatRec", function () {',
  32916. ' this.$new = function () {',
  32917. ' var r = Object.create(this);',
  32918. ' r.c = [];',
  32919. ' r.d = [];',
  32920. ' return r;',
  32921. ' };',
  32922. ' this.$eq = function (b) {',
  32923. ' return (this.c === b.c) && (this.d === b.d);',
  32924. ' };',
  32925. ' this.$assign = function (s) {',
  32926. ' this.c = rtl.arrayRef(s.c);',
  32927. ' this.d = rtl.arrayRef(s.d);',
  32928. ' return this;',
  32929. ' };',
  32930. ' var $r = $mod.$rtti.$Record("TFloatRec", {}, this);',
  32931. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  32932. ' eltype: rtl.char',
  32933. ' });',
  32934. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  32935. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  32936. '});',
  32937. 'this.p = null;',
  32938. 'this.r = this.TFloatRec.$new();',
  32939. '']),
  32940. LinesToStr([ // $mod.$main
  32941. '$mod.p = $mod.$rtti["TFloatRec"];',
  32942. '$mod.p = $mod.$rtti["TFloatRec"];',
  32943. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  32944. '']));
  32945. end;
  32946. procedure TTestModule.TestRTTI_Record_ClassVarType;
  32947. begin
  32948. WithTypeInfo:=true;
  32949. StartProgram(false);
  32950. Add([
  32951. '{$modeswitch AdvancedRecords}',
  32952. 'type',
  32953. ' TPoint = record',
  32954. ' type TProc = procedure(w: word);',
  32955. ' class var p: TProc;',
  32956. ' end;',
  32957. 'begin',
  32958. '']);
  32959. ConvertProgram;
  32960. CheckSource('TestRTTI_Record_ClassVarType',
  32961. LinesToStr([ // statements
  32962. 'rtl.recNewT(this, "TPoint", function () {',
  32963. ' $mod.$rtti.$ProcVar("TPoint.TProc", {',
  32964. ' procsig: rtl.newTIProcSig([["w", rtl.word]])',
  32965. ' });',
  32966. ' this.p = null;',
  32967. ' this.$eq = function (b) {',
  32968. ' return true;',
  32969. ' };',
  32970. ' this.$assign = function (s) {',
  32971. ' return this;',
  32972. ' };',
  32973. ' var $r = $mod.$rtti.$Record("TPoint", {}, this);',
  32974. ' $r.addField("p", $mod.$rtti["TPoint.TProc"]);',
  32975. '}, true);',
  32976. '']),
  32977. LinesToStr([ // $mod.$main
  32978. '']));
  32979. end;
  32980. procedure TTestModule.TestRTTI_LocalTypes;
  32981. begin
  32982. WithTypeInfo:=true;
  32983. StartProgram(false);
  32984. Add([
  32985. 'procedure DoIt;',
  32986. 'type',
  32987. ' integer = longint;',
  32988. ' TPoint = record',
  32989. ' x,y: integer;',
  32990. ' end;',
  32991. 'var p: TPoint;',
  32992. 'begin',
  32993. 'end;',
  32994. 'begin']);
  32995. ConvertProgram;
  32996. CheckSource('TestRTTI_LocalTypes',
  32997. LinesToStr([ // statements
  32998. 'var TPoint = rtl.recNewT(null, "", function () {',
  32999. ' this.x = 0;',
  33000. ' this.y = 0;',
  33001. ' this.$eq = function (b) {',
  33002. ' return (this.x === b.x) && (this.y === b.y);',
  33003. ' };',
  33004. ' this.$assign = function (s) {',
  33005. ' this.x = s.x;',
  33006. ' this.y = s.y;',
  33007. ' return this;',
  33008. ' };',
  33009. '});',
  33010. 'this.DoIt = function () {',
  33011. ' var p = TPoint.$new();',
  33012. '};',
  33013. '']),
  33014. LinesToStr([ // $mod.$main
  33015. '']));
  33016. end;
  33017. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  33018. begin
  33019. WithTypeInfo:=true;
  33020. StartProgram(false);
  33021. Add([
  33022. 'type',
  33023. ' TCaption = string;',
  33024. ' TYesNo = boolean;',
  33025. ' TLetter = char;',
  33026. ' TFloat = double;',
  33027. ' TPtr = pointer;',
  33028. ' TShortInt = shortint;',
  33029. ' TByte = byte;',
  33030. ' TSmallInt = smallint;',
  33031. ' TWord = word;',
  33032. ' TInt32 = longint;',
  33033. ' TDWord = longword;',
  33034. ' TValue = jsvalue;',
  33035. 'var p: TPtr;',
  33036. 'begin',
  33037. ' p:=typeinfo(string);',
  33038. ' p:=typeinfo(tcaption);',
  33039. ' p:=typeinfo(boolean);',
  33040. ' p:=typeinfo(tyesno);',
  33041. ' p:=typeinfo(char);',
  33042. ' p:=typeinfo(tletter);',
  33043. ' p:=typeinfo(double);',
  33044. ' p:=typeinfo(tfloat);',
  33045. ' p:=typeinfo(pointer);',
  33046. ' p:=typeinfo(tptr);',
  33047. ' p:=typeinfo(shortint);',
  33048. ' p:=typeinfo(tshortint);',
  33049. ' p:=typeinfo(byte);',
  33050. ' p:=typeinfo(tbyte);',
  33051. ' p:=typeinfo(smallint);',
  33052. ' p:=typeinfo(tsmallint);',
  33053. ' p:=typeinfo(word);',
  33054. ' p:=typeinfo(tword);',
  33055. ' p:=typeinfo(longword);',
  33056. ' p:=typeinfo(tdword);',
  33057. ' p:=typeinfo(jsvalue);',
  33058. ' p:=typeinfo(tvalue);',
  33059. '']);
  33060. ConvertProgram;
  33061. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  33062. LinesToStr([ // statements
  33063. 'this.p = null;',
  33064. '']),
  33065. LinesToStr([ // $mod.$main
  33066. '$mod.p = rtl.string;',
  33067. '$mod.p = rtl.string;',
  33068. '$mod.p = rtl.boolean;',
  33069. '$mod.p = rtl.boolean;',
  33070. '$mod.p = rtl.char;',
  33071. '$mod.p = rtl.char;',
  33072. '$mod.p = rtl.double;',
  33073. '$mod.p = rtl.double;',
  33074. '$mod.p = rtl.pointer;',
  33075. '$mod.p = rtl.pointer;',
  33076. '$mod.p = rtl.shortint;',
  33077. '$mod.p = rtl.shortint;',
  33078. '$mod.p = rtl.byte;',
  33079. '$mod.p = rtl.byte;',
  33080. '$mod.p = rtl.smallint;',
  33081. '$mod.p = rtl.smallint;',
  33082. '$mod.p = rtl.word;',
  33083. '$mod.p = rtl.word;',
  33084. '$mod.p = rtl.longword;',
  33085. '$mod.p = rtl.longword;',
  33086. '$mod.p = rtl.jsvalue;',
  33087. '$mod.p = rtl.jsvalue;',
  33088. '']));
  33089. end;
  33090. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  33091. begin
  33092. WithTypeInfo:=true;
  33093. StartProgram(false);
  33094. Add([
  33095. 'type',
  33096. ' TCaption = type string;',
  33097. ' TYesNo = type boolean;',
  33098. ' TLetter = type char;',
  33099. ' TFloat = type double;',
  33100. ' TPtr = type pointer;',
  33101. ' TShortInt = type shortint;',
  33102. ' TByte = type byte;',
  33103. ' TSmallInt = type smallint;',
  33104. ' TWord = type word;',
  33105. ' TInt32 = type longint;',
  33106. ' TDWord = type longword;',
  33107. ' TValue = type jsvalue;',
  33108. ' TAliasValue = type TValue;',
  33109. 'var',
  33110. ' p: TPtr;',
  33111. ' a: TAliasValue;',
  33112. 'begin',
  33113. ' p:=typeinfo(tcaption);',
  33114. ' p:=typeinfo(tyesno);',
  33115. ' p:=typeinfo(tletter);',
  33116. ' p:=typeinfo(tfloat);',
  33117. ' p:=typeinfo(tptr);',
  33118. ' p:=typeinfo(tshortint);',
  33119. ' p:=typeinfo(tbyte);',
  33120. ' p:=typeinfo(tsmallint);',
  33121. ' p:=typeinfo(tword);',
  33122. ' p:=typeinfo(tdword);',
  33123. ' p:=typeinfo(tvalue);',
  33124. ' p:=typeinfo(taliasvalue);',
  33125. ' p:=typeinfo(a);',
  33126. '']);
  33127. ConvertProgram;
  33128. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  33129. LinesToStr([ // statements
  33130. 'this.$rtti.$inherited("TCaption", rtl.string, {});',
  33131. 'this.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  33132. 'this.$rtti.$inherited("TLetter", rtl.char, {});',
  33133. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  33134. 'this.$rtti.$inherited("TPtr", rtl.pointer, {});',
  33135. 'this.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  33136. 'this.$rtti.$inherited("TByte", rtl.byte, {});',
  33137. 'this.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  33138. 'this.$rtti.$inherited("TWord", rtl.word, {});',
  33139. 'this.$rtti.$inherited("TInt32", rtl.longint, {});',
  33140. 'this.$rtti.$inherited("TDWord", rtl.longword, {});',
  33141. 'this.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  33142. 'this.$rtti.$inherited("TAliasValue", this.$rtti["TValue"], {});',
  33143. 'this.p = null;',
  33144. 'this.a = undefined;',
  33145. '']),
  33146. LinesToStr([ // $mod.$main
  33147. '$mod.p = $mod.$rtti["TCaption"];',
  33148. '$mod.p = $mod.$rtti["TYesNo"];',
  33149. '$mod.p = $mod.$rtti["TLetter"];',
  33150. '$mod.p = $mod.$rtti["TFloat"];',
  33151. '$mod.p = $mod.$rtti["TPtr"];',
  33152. '$mod.p = $mod.$rtti["TShortInt"];',
  33153. '$mod.p = $mod.$rtti["TByte"];',
  33154. '$mod.p = $mod.$rtti["TSmallInt"];',
  33155. '$mod.p = $mod.$rtti["TWord"];',
  33156. '$mod.p = $mod.$rtti["TDWord"];',
  33157. '$mod.p = $mod.$rtti["TValue"];',
  33158. '$mod.p = $mod.$rtti["TAliasValue"];',
  33159. '$mod.p = $mod.$rtti["TAliasValue"];',
  33160. '']));
  33161. end;
  33162. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  33163. begin
  33164. WithTypeInfo:=true;
  33165. StartProgram(false);
  33166. Add('procedure DoIt;');
  33167. Add('type');
  33168. Add(' integer = longint;');
  33169. Add(' TPoint = record');
  33170. Add(' x,y: integer;');
  33171. Add(' end;');
  33172. Add('var p: pointer;');
  33173. Add('begin');
  33174. Add(' p:=typeinfo(tpoint);');
  33175. Add('end;');
  33176. Add('begin');
  33177. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  33178. ConvertProgram;
  33179. end;
  33180. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  33181. begin
  33182. WithTypeInfo:=true;
  33183. StartProgram(true,[supTypeInfo]);
  33184. Add([
  33185. '{$modeswitch externalclass}',
  33186. 'type',
  33187. ' TFlag = (up,down);',
  33188. ' TFlags = set of TFlag;',
  33189. 'var',
  33190. ' ti: TTypeInfo;',
  33191. ' tiInt: TTypeInfoInteger;',
  33192. ' tiEnum: TTypeInfoEnum;',
  33193. ' tiSet: TTypeInfoSet;',
  33194. 'begin',
  33195. ' ti:=typeinfo(string);',
  33196. ' ti:=typeinfo(boolean);',
  33197. ' ti:=typeinfo(char);',
  33198. ' ti:=typeinfo(double);',
  33199. ' tiInt:=typeinfo(shortint);',
  33200. ' tiInt:=typeinfo(byte);',
  33201. ' tiInt:=typeinfo(smallint);',
  33202. ' tiInt:=typeinfo(word);',
  33203. ' tiInt:=typeinfo(longint);',
  33204. ' tiInt:=typeinfo(longword);',
  33205. ' ti:=typeinfo(jsvalue);',
  33206. ' tiEnum:=typeinfo(tflag);',
  33207. ' tiSet:=typeinfo(tflags);']);
  33208. ConvertProgram;
  33209. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  33210. LinesToStr([ // statements
  33211. 'this.TFlag = {',
  33212. ' "0": "up",',
  33213. ' up: 0,',
  33214. ' "1": "down",',
  33215. ' down: 1',
  33216. '};',
  33217. 'this.$rtti.$Enum("TFlag", {',
  33218. ' minvalue: 0,',
  33219. ' maxvalue: 1,',
  33220. ' ordtype: 1,',
  33221. ' enumtype: this.TFlag',
  33222. '});',
  33223. 'this.$rtti.$Set("TFlags", {',
  33224. ' comptype: this.$rtti["TFlag"]',
  33225. '});',
  33226. 'this.ti = null;',
  33227. 'this.tiInt = null;',
  33228. 'this.tiEnum = null;',
  33229. 'this.tiSet = null;',
  33230. '']),
  33231. LinesToStr([ // $mod.$main
  33232. '$mod.ti = rtl.string;',
  33233. '$mod.ti = rtl.boolean;',
  33234. '$mod.ti = rtl.char;',
  33235. '$mod.ti = rtl.double;',
  33236. '$mod.tiInt = rtl.shortint;',
  33237. '$mod.tiInt = rtl.byte;',
  33238. '$mod.tiInt = rtl.smallint;',
  33239. '$mod.tiInt = rtl.word;',
  33240. '$mod.tiInt = rtl.longint;',
  33241. '$mod.tiInt = rtl.longword;',
  33242. '$mod.ti = rtl.jsvalue;',
  33243. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  33244. '$mod.tiSet = $mod.$rtti["TFlags"];',
  33245. '']));
  33246. end;
  33247. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  33248. begin
  33249. WithTypeInfo:=true;
  33250. StartProgram(true,[supTypeInfo]);
  33251. Add('{$modeswitch externalclass}');
  33252. Add('type');
  33253. Add(' TStaticArr = array[boolean] of string;');
  33254. Add(' TDynArr = array of string;');
  33255. Add(' TProc = procedure;');
  33256. Add(' TMethod = procedure of object;');
  33257. Add('var');
  33258. Add(' StaticArray: TStaticArr;');
  33259. Add(' tiStaticArray: TTypeInfoStaticArray;');
  33260. Add(' DynArray: TDynArr;');
  33261. Add(' tiDynArray: TTypeInfoDynArray;');
  33262. Add(' ProcVar: TProc;');
  33263. Add(' tiProcVar: TTypeInfoProcVar;');
  33264. Add(' MethodVar: TMethod;');
  33265. Add(' tiMethodVar: TTypeInfoMethodVar;');
  33266. Add('begin');
  33267. Add(' tiStaticArray:=typeinfo(StaticArray);');
  33268. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  33269. Add(' tiDynArray:=typeinfo(DynArray);');
  33270. Add(' tiDynArray:=typeinfo(TDynArr);');
  33271. Add(' tiProcVar:=typeinfo(ProcVar);');
  33272. Add(' tiProcVar:=typeinfo(TProc);');
  33273. Add(' tiMethodVar:=typeinfo(MethodVar);');
  33274. Add(' tiMethodVar:=typeinfo(TMethod);');
  33275. ConvertProgram;
  33276. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  33277. LinesToStr([ // statements
  33278. 'this.$rtti.$StaticArray("TStaticArr", {',
  33279. ' dims: [2],',
  33280. ' eltype: rtl.string',
  33281. '});',
  33282. 'this.$rtti.$DynArray("TDynArr", {',
  33283. ' eltype: rtl.string',
  33284. '});',
  33285. 'this.$rtti.$ProcVar("TProc", {',
  33286. ' procsig: rtl.newTIProcSig([])',
  33287. '});',
  33288. 'this.$rtti.$MethodVar("TMethod", {',
  33289. ' procsig: rtl.newTIProcSig([]),',
  33290. ' methodkind: 0',
  33291. '});',
  33292. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  33293. 'this.tiStaticArray = null;',
  33294. 'this.DynArray = [];',
  33295. 'this.tiDynArray = null;',
  33296. 'this.ProcVar = null;',
  33297. 'this.tiProcVar = null;',
  33298. 'this.MethodVar = null;',
  33299. 'this.tiMethodVar = null;',
  33300. '']),
  33301. LinesToStr([ // $mod.$main
  33302. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  33303. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  33304. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  33305. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  33306. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  33307. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  33308. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  33309. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  33310. '']));
  33311. end;
  33312. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  33313. begin
  33314. WithTypeInfo:=true;
  33315. StartProgram(true,[supTypeInfo]);
  33316. Add('{$modeswitch externalclass}');
  33317. Add('type');
  33318. Add(' TRec = record end;');
  33319. // ToDo: ^TRec
  33320. Add(' TObject = class end;');
  33321. Add(' TClass = class of tobject;');
  33322. Add('var');
  33323. Add(' Rec: trec;');
  33324. Add(' tiRecord: ttypeinforecord;');
  33325. Add(' Obj: tobject;');
  33326. Add(' tiClass: ttypeinfoclass;');
  33327. Add(' aClass: tclass;');
  33328. Add(' tiClassRef: ttypeinfoclassref;');
  33329. // ToDo: ^TRec
  33330. Add(' tiPointer: ttypeinfopointer;');
  33331. Add('begin');
  33332. Add(' tirecord:=typeinfo(trec);');
  33333. Add(' tirecord:=typeinfo(trec);');
  33334. Add(' ticlass:=typeinfo(obj);');
  33335. Add(' ticlass:=typeinfo(tobject);');
  33336. Add(' ticlass:=typeinfo(aclass);');
  33337. Add(' ticlassref:=typeinfo(tclass);');
  33338. ConvertProgram;
  33339. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  33340. LinesToStr([ // statements
  33341. 'rtl.recNewT(this, "TRec", function () {',
  33342. ' this.$eq = function (b) {',
  33343. ' return true;',
  33344. ' };',
  33345. ' this.$assign = function (s) {',
  33346. ' return this;',
  33347. ' };',
  33348. ' $mod.$rtti.$Record("TRec", {});',
  33349. '});',
  33350. 'rtl.createClass(this, "TObject", null, function () {',
  33351. ' this.$init = function () {',
  33352. ' };',
  33353. ' this.$final = function () {',
  33354. ' };',
  33355. '});',
  33356. 'this.$rtti.$ClassRef("TClass", {',
  33357. ' instancetype: this.$rtti["TObject"]',
  33358. '});',
  33359. 'this.Rec = this.TRec.$new();',
  33360. 'this.tiRecord = null;',
  33361. 'this.Obj = null;',
  33362. 'this.tiClass = null;',
  33363. 'this.aClass = null;',
  33364. 'this.tiClassRef = null;',
  33365. 'this.tiPointer = null;',
  33366. '']),
  33367. LinesToStr([ // $mod.$main
  33368. '$mod.tiRecord = $mod.$rtti["TRec"];',
  33369. '$mod.tiRecord = $mod.$rtti["TRec"];',
  33370. '$mod.tiClass = $mod.Obj.$rtti;',
  33371. '$mod.tiClass = $mod.$rtti["TObject"];',
  33372. '$mod.tiClass = $mod.aClass.$rtti;',
  33373. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  33374. '']));
  33375. end;
  33376. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  33377. begin
  33378. WithTypeInfo:=true;
  33379. StartProgram(true,[supTypeInfo]);
  33380. Add([
  33381. '{$modeswitch externalclass}',
  33382. 'type',
  33383. ' TClass = class of tobject;',
  33384. ' TObject = class',
  33385. ' function MyClass: TClass;',
  33386. ' class function ClassType: TClass;',
  33387. ' end;',
  33388. 'function TObject.MyClass: TClass;',
  33389. 'var t: TTypeInfoClass;',
  33390. 'begin',
  33391. ' t:=TypeInfo(Self);',
  33392. ' t:=TypeInfo(Result);',
  33393. ' t:=TypeInfo(TObject);',
  33394. 'end;',
  33395. 'class function TObject.ClassType: TClass;',
  33396. 'var t: TTypeInfoClass;',
  33397. 'begin',
  33398. ' t:=TypeInfo(Self);',
  33399. ' t:=TypeInfo(Result);',
  33400. 'end;',
  33401. 'var',
  33402. ' Obj: TObject;',
  33403. ' t: TTypeInfoClass;',
  33404. 'begin',
  33405. ' t:=TypeInfo(TObject.ClassType);',
  33406. ' t:=TypeInfo(Obj.ClassType);',
  33407. ' t:=TypeInfo(Obj.MyClass);',
  33408. '']);
  33409. ConvertProgram;
  33410. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  33411. LinesToStr([ // statements
  33412. 'this.$rtti.$Class("TObject");',
  33413. 'this.$rtti.$ClassRef("TClass", {',
  33414. ' instancetype: this.$rtti["TObject"]',
  33415. '});',
  33416. 'rtl.createClass(this, "TObject", null, function () {',
  33417. ' this.$init = function () {',
  33418. ' };',
  33419. ' this.$final = function () {',
  33420. ' };',
  33421. ' this.MyClass = function () {',
  33422. ' var Result = null;',
  33423. ' var t = null;',
  33424. ' t = this.$rtti;',
  33425. ' t = Result.$rtti;',
  33426. ' t = $mod.$rtti["TObject"];',
  33427. ' return Result;',
  33428. ' };',
  33429. ' this.ClassType = function () {',
  33430. ' var Result = null;',
  33431. ' var t = null;',
  33432. ' t = this.$rtti;',
  33433. ' t = Result.$rtti;',
  33434. ' return Result;',
  33435. ' };',
  33436. '});',
  33437. 'this.Obj = null;',
  33438. 'this.t = null;',
  33439. '']),
  33440. LinesToStr([ // $mod.$main
  33441. '$mod.t = $mod.TObject.ClassType().$rtti;',
  33442. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  33443. '$mod.t = $mod.Obj.MyClass().$rtti;',
  33444. '']));
  33445. end;
  33446. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  33447. begin
  33448. WithTypeInfo:=true;
  33449. AddModuleWithIntfImplSrc('typinfo.pas',
  33450. LinesToStr([
  33451. '{$modeswitch externalclass}',
  33452. 'type',
  33453. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  33454. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  33455. '']),
  33456. '');
  33457. AddModuleWithIntfImplSrc('unit2.pas',
  33458. LinesToStr([
  33459. 'uses typinfo;',
  33460. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  33461. 'procedure DoPtr(p: PTypeInfo);',
  33462. 'procedure DoInfo(t: TTypeInfo);',
  33463. 'procedure DoInt(t: TTypeInfoInteger);',
  33464. '']),
  33465. LinesToStr([
  33466. 'procedure DoPtr(p: PTypeInfo);',
  33467. 'begin end;',
  33468. 'procedure DoInfo(t: TTypeInfo);',
  33469. 'begin end;',
  33470. 'procedure DoInt(t: TTypeInfoInteger);',
  33471. 'begin end;',
  33472. '']));
  33473. StartUnit(true);
  33474. Add([
  33475. 'interface',
  33476. 'uses unit2;', // does not use unit typinfo
  33477. 'implementation',
  33478. 'var',
  33479. ' i: byte;',
  33480. ' p: pointer;',
  33481. ' t: PTypeInfo;',
  33482. 'initialization',
  33483. ' p:=typeinfo(i);',
  33484. ' t:=typeinfo(i);',
  33485. ' if p=t then ;',
  33486. ' if p=typeinfo(i) then ;',
  33487. ' if typeinfo(i)=p then ;',
  33488. ' if t=typeinfo(i) then ;',
  33489. ' if typeinfo(i)=t then ;',
  33490. ' DoPtr(p);',
  33491. ' DoPtr(t);',
  33492. ' DoPtr(typeinfo(i));',
  33493. ' DoInfo(p);',
  33494. ' DoInfo(t);',
  33495. ' DoInfo(typeinfo(i));',
  33496. ' DoInt(typeinfo(i));',
  33497. '']);
  33498. ConvertUnit;
  33499. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  33500. LinesToStr([ // statements
  33501. 'var $impl = $mod.$impl;',
  33502. '']),
  33503. LinesToStr([ // this.$init
  33504. '$impl.p = rtl.byte;',
  33505. '$impl.t = rtl.byte;',
  33506. 'if ($impl.p === $impl.t) ;',
  33507. 'if ($impl.p === rtl.byte) ;',
  33508. 'if (rtl.byte === $impl.p) ;',
  33509. 'if ($impl.t === rtl.byte) ;',
  33510. 'if (rtl.byte === $impl.t) ;',
  33511. 'pas.unit2.DoPtr($impl.p);',
  33512. 'pas.unit2.DoPtr($impl.t);',
  33513. 'pas.unit2.DoPtr(rtl.byte);',
  33514. 'pas.unit2.DoInfo($impl.p);',
  33515. 'pas.unit2.DoInfo($impl.t);',
  33516. 'pas.unit2.DoInfo(rtl.byte);',
  33517. 'pas.unit2.DoInt(rtl.byte);',
  33518. '']),
  33519. LinesToStr([ // implementation
  33520. '$impl.i = 0;',
  33521. '$impl.p = null;',
  33522. '$impl.t = null;',
  33523. '']) );
  33524. end;
  33525. procedure TTestModule.TestRTTI_Interface_Corba;
  33526. begin
  33527. WithTypeInfo:=true;
  33528. StartProgram(true,[supTypeInfo]);
  33529. Add([
  33530. '{$interfaces corba}',
  33531. '{$modeswitch externalclass}',
  33532. 'type',
  33533. ' IUnknown = interface',
  33534. ' end;',
  33535. ' IBird = interface',
  33536. ' function GetItem: longint;',
  33537. ' procedure SetItem(Value: longint);',
  33538. ' property Item: longint read GetItem write SetItem;',
  33539. ' end;',
  33540. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  33541. 'var',
  33542. ' i: IBird;',
  33543. ' t: TTypeInfoInterface;',
  33544. 'begin',
  33545. ' t:=TypeInfo(IBird);',
  33546. ' t:=TypeInfo(i);',
  33547. ' DoIt(t);',
  33548. ' DoIt(TypeInfo(IBird));',
  33549. '']);
  33550. ConvertProgram;
  33551. CheckSource('TestRTTI_Interface_Corba',
  33552. LinesToStr([ // statements
  33553. 'rtl.createInterface(',
  33554. ' this,',
  33555. ' "IUnknown",',
  33556. ' "{B92D5841-758A-322B-B800-000000000000}",',
  33557. ' [],',
  33558. ' null,',
  33559. ' function () {',
  33560. ' }',
  33561. ');',
  33562. 'rtl.createInterface(',
  33563. ' this,',
  33564. ' "IBird",',
  33565. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  33566. ' ["GetItem", "SetItem"],',
  33567. ' null,',
  33568. ' function () {',
  33569. ' var $r = this.$rtti;',
  33570. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  33571. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  33572. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem", 2);',
  33573. ' }',
  33574. ');',
  33575. 'this.DoIt = function (t) {',
  33576. '}; ',
  33577. 'this.i = null;',
  33578. 'this.t = null;',
  33579. '']),
  33580. LinesToStr([ // $mod.$main
  33581. '$mod.t = $mod.$rtti["IBird"];',
  33582. '$mod.t = $mod.i.$rtti;',
  33583. '$mod.DoIt($mod.t);',
  33584. '$mod.DoIt($mod.$rtti["IBird"]);',
  33585. '']));
  33586. end;
  33587. procedure TTestModule.TestRTTI_Interface_COM;
  33588. begin
  33589. WithTypeInfo:=true;
  33590. StartProgram(true,[supTypeInfo]);
  33591. Add([
  33592. '{$interfaces com}',
  33593. '{$modeswitch externalclass}',
  33594. 'type',
  33595. ' TGuid = record end;',
  33596. ' integer = longint;',
  33597. ' IUnknown = interface',
  33598. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  33599. ' function _AddRef: Integer;',
  33600. ' function _Release: Integer;',
  33601. ' end;',
  33602. ' IBird = interface',
  33603. ' function GetItem: longint;',
  33604. ' procedure SetItem(Value: longint);',
  33605. ' property Item: longint read GetItem write SetItem;',
  33606. ' end;',
  33607. 'var',
  33608. ' i: IBird;',
  33609. ' t: TTypeInfoInterface;',
  33610. 'begin',
  33611. ' t:=TypeInfo(IBird);',
  33612. ' t:=TypeInfo(i);',
  33613. '']);
  33614. ConvertProgram;
  33615. CheckSource('TestRTTI_Interface_COM',
  33616. LinesToStr([ // statements
  33617. 'rtl.recNewT(this, "TGuid", function () {',
  33618. ' this.$eq = function (b) {',
  33619. ' return true;',
  33620. ' };',
  33621. ' this.$assign = function (s) {',
  33622. ' return this;',
  33623. ' };',
  33624. ' $mod.$rtti.$Record("TGuid", {});',
  33625. '});',
  33626. 'rtl.createInterface(',
  33627. ' this,',
  33628. ' "IUnknown",',
  33629. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  33630. ' ["QueryInterface", "_AddRef", "_Release"],',
  33631. ' null,',
  33632. ' function () {',
  33633. ' this.$kind = "com";',
  33634. ' var $r = this.$rtti;',
  33635. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  33636. ' $r.addMethod("_AddRef", 1, [], rtl.longint);',
  33637. ' $r.addMethod("_Release", 1, [], rtl.longint);',
  33638. ' }',
  33639. ');',
  33640. 'rtl.createInterface(',
  33641. ' this,',
  33642. ' "IBird",',
  33643. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  33644. ' ["GetItem", "SetItem"],',
  33645. ' this.IUnknown,',
  33646. ' function () {',
  33647. ' var $r = this.$rtti;',
  33648. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  33649. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  33650. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem", 2);',
  33651. ' }',
  33652. ');',
  33653. 'this.i = null;',
  33654. 'this.t = null;',
  33655. '']),
  33656. LinesToStr([ // $mod.$main
  33657. '$mod.t = $mod.$rtti["IBird"];',
  33658. '$mod.t = $mod.i.$rtti;',
  33659. '']));
  33660. end;
  33661. procedure TTestModule.TestRTTI_ClassHelper;
  33662. begin
  33663. WithTypeInfo:=true;
  33664. StartProgram(true,[supTypeInfo]);
  33665. Add([
  33666. '{$interfaces com}',
  33667. '{$modeswitch externalclass}',
  33668. 'type',
  33669. ' TObject = class',
  33670. ' end;',
  33671. ' THelper = class helper for TObject',
  33672. ' published',
  33673. ' function GetItem: longint;',
  33674. ' property Item: longint read GetItem;',
  33675. ' end;',
  33676. 'function THelper.GetItem: longint;',
  33677. 'begin',
  33678. 'end;',
  33679. 'var',
  33680. ' t: TTypeInfoHelper;',
  33681. 'begin',
  33682. ' t:=TypeInfo(THelper);',
  33683. '']);
  33684. ConvertProgram;
  33685. CheckSource('TestRTTI_ClassHelper',
  33686. LinesToStr([ // statements
  33687. 'rtl.createClass(this, "TObject", null, function () {',
  33688. ' this.$init = function () {',
  33689. ' };',
  33690. ' this.$final = function () {',
  33691. ' };',
  33692. '});',
  33693. 'rtl.createHelper(this, "THelper", null, function () {',
  33694. ' this.GetItem = function () {',
  33695. ' var Result = 0;',
  33696. ' return Result;',
  33697. ' };',
  33698. ' var $r = this.$rtti;',
  33699. ' $r.addMethod("GetItem", 1, [], 4, rtl.longint);',
  33700. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  33701. '});',
  33702. 'this.t = null;',
  33703. '']),
  33704. LinesToStr([ // $mod.$main
  33705. '$mod.t = $mod.$rtti["THelper"];',
  33706. '']));
  33707. end;
  33708. procedure TTestModule.TestRTTI_ExternalClass;
  33709. begin
  33710. WithTypeInfo:=true;
  33711. StartProgram(true,[supTypeInfo]);
  33712. Add([
  33713. '{$modeswitch externalclass}',
  33714. 'type',
  33715. ' TJSObject = class external name ''Object''',
  33716. ' end;',
  33717. ' TJSArray = class external name ''Array'' (TJSObject)',
  33718. ' end;',
  33719. 'var',
  33720. ' p: Pointer;',
  33721. ' tc: TTypeInfoExtClass;',
  33722. 'begin',
  33723. ' p:=typeinfo(TJSArray);']);
  33724. ConvertProgram;
  33725. CheckSource('TestRTTI_ExternalClass',
  33726. LinesToStr([ // statements
  33727. 'this.$rtti.$ExtClass("TJSObject", {',
  33728. ' jsclass: "Object"',
  33729. '});',
  33730. 'this.$rtti.$ExtClass("TJSArray", {',
  33731. ' ancestor: this.$rtti["TJSObject"],',
  33732. ' jsclass: "Array"',
  33733. '});',
  33734. 'this.p = null;',
  33735. 'this.tc = null;',
  33736. '']),
  33737. LinesToStr([ // $mod.$main
  33738. '$mod.p = $mod.$rtti["TJSArray"];',
  33739. '']));
  33740. end;
  33741. procedure TTestModule.TestRTTI_Unit;
  33742. begin
  33743. WithTypeInfo:=true;
  33744. AddModuleWithIntfImplSrc('unit2.pas',
  33745. LinesToStr([
  33746. '{$mode delphi}',
  33747. 'type',
  33748. ' TWordArray = array of word;',
  33749. ' TArray<T> = array of T;',
  33750. '']),
  33751. '');
  33752. StartUnit(true,[supTypeInfo,supTInterfacedObject]);
  33753. Add([
  33754. '{$mode delphi}',
  33755. 'interface',
  33756. 'uses unit2;',
  33757. 'type',
  33758. ' IBird = interface',
  33759. ' function Swoop: TWordArray;',
  33760. ' function Glide: TArray<word>;',
  33761. ' end;',
  33762. 'procedure Fly;',
  33763. 'implementation',
  33764. 'procedure Fly;',
  33765. 'var',
  33766. ' ta: tTypeInfoDynArray;',
  33767. ' ti: tTypeInfoInterface;',
  33768. 'begin',
  33769. ' ta:=typeinfo(TWordArray);',
  33770. ' ta:=typeinfo(TArray<word>);',
  33771. ' ti:=typeinfo(IBird);',
  33772. 'end;',
  33773. '']);
  33774. ConvertUnit;
  33775. CheckSource('TestRTTI_ExternalClass',
  33776. LinesToStr([ // statements
  33777. 'rtl.createInterface(',
  33778. ' this,',
  33779. ' "IBird",',
  33780. ' "{3B98AAAC-6116-3E17-AA85-F16786D85B09}",',
  33781. ' ["Swoop", "Glide"],',
  33782. ' pas.system.IUnknown,',
  33783. ' function () {',
  33784. ' var $r = this.$rtti;',
  33785. ' $r.addMethod("Swoop", 1, [], pas.unit2.$rtti["TWordArray"]);',
  33786. ' $r.addMethod("Glide", 1, [], pas.unit2.$rtti["TArray<System.Word>"]);',
  33787. ' }',
  33788. ');',
  33789. 'this.Fly = function () {',
  33790. ' var ta = null;',
  33791. ' var ti = null;',
  33792. ' ta = pas.unit2.$rtti["TWordArray"];',
  33793. ' ta = pas.unit2.$rtti["TArray<System.Word>"];',
  33794. ' ti = $mod.$rtti["IBird"];',
  33795. '};',
  33796. '']),
  33797. LinesToStr([ // $mod.$main
  33798. '']));
  33799. end;
  33800. procedure TTestModule.TestResourcestringProgram;
  33801. begin
  33802. AddModuleWithIntfImplSrc('unit2.pas',
  33803. LinesToStr([
  33804. 'resourcestring Title = ''Nice'';',
  33805. '']),
  33806. '');
  33807. StartProgram(true);
  33808. Add([
  33809. 'uses unit2;',
  33810. 'const Bar = ''bar'';',
  33811. 'resourcestring',
  33812. ' Red = ''red'';',
  33813. ' Foobar = ''fOo''+bar;',
  33814. 'var s: string;',
  33815. ' c: char;',
  33816. 'begin',
  33817. ' s:=red;',
  33818. ' s:=test1.red;',
  33819. ' s:=Title;',
  33820. ' c:=red[1];',
  33821. ' c:=test1.red[2];',
  33822. ' if red=foobar then ;',
  33823. ' if red[3]=red[4] then ;']);
  33824. ConvertProgram;
  33825. CheckSource('TestResourcestringProgram',
  33826. LinesToStr([ // statements
  33827. 'this.Bar = "bar";',
  33828. 'this.s = "";',
  33829. 'this.c = "\x00";',
  33830. '$mod.$resourcestrings = {',
  33831. ' Red: {',
  33832. ' org: "red"',
  33833. ' },',
  33834. ' Foobar: {',
  33835. ' org: "fOobar"',
  33836. ' }',
  33837. '};',
  33838. '']),
  33839. LinesToStr([ // $mod.$main
  33840. '$mod.s = rtl.getResStr($mod, "Red");',
  33841. '$mod.s = rtl.getResStr($mod, "Red");',
  33842. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  33843. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  33844. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  33845. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  33846. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  33847. '']));
  33848. end;
  33849. procedure TTestModule.TestResourcestringUnit;
  33850. begin
  33851. AddModuleWithIntfImplSrc('unit2.pas',
  33852. LinesToStr([
  33853. 'resourcestring Title = ''Nice'';',
  33854. '']),
  33855. '');
  33856. StartUnit(true);
  33857. Add([
  33858. 'interface',
  33859. 'uses unit2;',
  33860. 'const Red = ''rEd'';',
  33861. 'resourcestring',
  33862. ' Blue = ''blue'';',
  33863. ' NotRed = ''not''+Red;',
  33864. 'var s: string;',
  33865. 'implementation',
  33866. 'resourcestring',
  33867. ' ImplGreen = ''green'';',
  33868. 'initialization',
  33869. ' s:=blue+ImplGreen;',
  33870. ' s:=test1.blue+test1.implgreen;',
  33871. ' s:=blue[1]+implgreen[2];',
  33872. ' s:=Title;',
  33873. '']);
  33874. ConvertUnit;
  33875. CheckSource('TestResourcestringUnit',
  33876. LinesToStr([ // statements
  33877. 'this.Red = "rEd";',
  33878. 'this.s = "";',
  33879. '$mod.$resourcestrings = {',
  33880. ' Blue: {',
  33881. ' org: "blue"',
  33882. ' },',
  33883. ' NotRed: {',
  33884. ' org: "notrEd"',
  33885. ' },',
  33886. ' ImplGreen: {',
  33887. ' org: "green"',
  33888. ' }',
  33889. '};',
  33890. '']),
  33891. LinesToStr([ // $mod.$main
  33892. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  33893. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  33894. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  33895. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  33896. '']));
  33897. end;
  33898. procedure TTestModule.TestResourcestringImplementation;
  33899. begin
  33900. StartUnit(false);
  33901. Add([
  33902. 'interface',
  33903. 'implementation',
  33904. 'resourcestring',
  33905. ' ImplRed = ''red'';']);
  33906. ConvertUnit;
  33907. CheckSource('TestResourcestringImplementation',
  33908. LinesToStr([ // intf statements
  33909. 'var $impl = $mod.$impl;']),
  33910. LinesToStr([ // $mod.$init
  33911. '']),
  33912. LinesToStr([ // impl statements
  33913. '$mod.$resourcestrings = {',
  33914. ' ImplRed: {',
  33915. ' org: "red"',
  33916. ' }',
  33917. '};',
  33918. '']));
  33919. end;
  33920. procedure TTestModule.TestAttributes_Members;
  33921. begin
  33922. WithTypeInfo:=true;
  33923. StartProgram(false);
  33924. Add([
  33925. '{$modeswitch PrefixedAttributes}',
  33926. 'type',
  33927. ' TObject = class',
  33928. ' constructor Create;',
  33929. ' end;',
  33930. ' TCustomAttribute = class',
  33931. ' constructor Create(Id: word);',
  33932. ' end;',
  33933. ' [Missing]',
  33934. ' TBird = class',
  33935. ' published',
  33936. ' [Tcustom]',
  33937. ' FField: word;',
  33938. ' [tcustom(14)]',
  33939. ' property Size: word read FField;',
  33940. ' [Tcustom(15)]',
  33941. ' procedure Fly; virtual; abstract;',
  33942. ' end;',
  33943. ' TRec = record',
  33944. ' [Tcustom,tcustom(14)]',
  33945. ' Size: word;',
  33946. ' [Tcustom(15)]',
  33947. ' Width, Height: word;',
  33948. ' end;',
  33949. 'constructor TObject.Create; begin end;',
  33950. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  33951. 'begin',
  33952. '']);
  33953. ConvertProgram;
  33954. CheckSource('TestAttributes_Members',
  33955. LinesToStr([ // statements
  33956. 'rtl.createClass(this, "TObject", null, function () {',
  33957. ' this.$init = function () {',
  33958. ' };',
  33959. ' this.$final = function () {',
  33960. ' };',
  33961. ' this.Create = function () {',
  33962. ' return this;',
  33963. ' };',
  33964. '});',
  33965. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  33966. ' this.Create$1 = function (Id) {',
  33967. ' return this;',
  33968. ' };',
  33969. '});',
  33970. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  33971. ' this.$init = function () {',
  33972. ' $mod.TObject.$init.call(this);',
  33973. ' this.FField = 0;',
  33974. ' };',
  33975. ' var $r = this.$rtti;',
  33976. ' $r.addField("FField", rtl.word, 4, {',
  33977. ' attr: [$mod.TCustomAttribute, "Create"]',
  33978. ' });',
  33979. ' $r.addProperty(',
  33980. ' "Size",',
  33981. ' 0,',
  33982. ' rtl.word,',
  33983. ' "FField",',
  33984. ' "",',
  33985. ' 4,',
  33986. ' {',
  33987. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  33988. ' }',
  33989. ' );',
  33990. ' $r.addMethod(',
  33991. ' "Fly",',
  33992. ' 0,',
  33993. ' [],',
  33994. ' 4,',
  33995. ' null,',
  33996. ' 0,',
  33997. ' {',
  33998. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  33999. ' });',
  34000. '});',
  34001. 'rtl.recNewT(this, "TRec", function () {',
  34002. ' this.Size = 0;',
  34003. ' this.Width = 0;',
  34004. ' this.Height = 0;',
  34005. ' this.$eq = function (b) {',
  34006. ' return (this.Size === b.Size) && (this.Width === b.Width) && (this.Height === b.Height);',
  34007. ' };',
  34008. ' this.$assign = function (s) {',
  34009. ' this.Size = s.Size;',
  34010. ' this.Width = s.Width;',
  34011. ' this.Height = s.Height;',
  34012. ' return this;',
  34013. ' };',
  34014. ' var $r = $mod.$rtti.$Record("TRec", {}, this);',
  34015. ' $r.addField("Size", rtl.word, 2, {',
  34016. ' attr: [',
  34017. ' $mod.TCustomAttribute,',
  34018. ' "Create",',
  34019. ' $mod.TCustomAttribute,',
  34020. ' "Create$1",',
  34021. ' [14]',
  34022. ' ]',
  34023. ' });',
  34024. ' $r.addField("Width", rtl.word, 2, {',
  34025. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  34026. ' });',
  34027. ' $r.addField("Height", rtl.word);',
  34028. '});',
  34029. '']),
  34030. LinesToStr([ // $mod.$main
  34031. '']));
  34032. end;
  34033. procedure TTestModule.TestAttributes_Types;
  34034. begin
  34035. WithTypeInfo:=true;
  34036. StartProgram(false);
  34037. Add([
  34038. '{$modeswitch PrefixedAttributes}',
  34039. 'type',
  34040. ' TObject = class',
  34041. ' constructor Create(Id: word);',
  34042. ' end;',
  34043. ' TCustomAttribute = class',
  34044. ' end;',
  34045. ' [TCustom(1)]',
  34046. ' TMyClass = class',
  34047. ' end;',
  34048. ' [TCustom(11)]',
  34049. ' TMyDescendant = class(TMyClass)',
  34050. ' end;',
  34051. ' [TCustom(2)]',
  34052. ' TRec = record',
  34053. ' end;',
  34054. ' [TCustom(3)]',
  34055. ' TInt = type word;',
  34056. 'constructor TObject.Create(Id: word);',
  34057. 'begin',
  34058. 'end;',
  34059. 'var p: pointer;',
  34060. 'begin',
  34061. ' p:=typeinfo(TMyClass);',
  34062. ' p:=typeinfo(TRec);',
  34063. ' p:=typeinfo(TInt);',
  34064. '']);
  34065. ConvertProgram;
  34066. CheckSource('TestAttributes_Types',
  34067. LinesToStr([ // statements
  34068. 'rtl.createClass(this, "TObject", null, function () {',
  34069. ' this.$init = function () {',
  34070. ' };',
  34071. ' this.$final = function () {',
  34072. ' };',
  34073. ' this.Create = function (Id) {',
  34074. ' return this;',
  34075. ' };',
  34076. '});',
  34077. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  34078. '});',
  34079. 'rtl.createClass(this, "TMyClass", this.TObject, function () {',
  34080. ' var $r = this.$rtti;',
  34081. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  34082. '});',
  34083. 'rtl.createClass(this, "TMyDescendant", this.TMyClass, function () {',
  34084. ' var $r = this.$rtti;',
  34085. ' $r.attr = [$mod.TCustomAttribute, "Create", [11]];',
  34086. '});',
  34087. 'rtl.recNewT(this, "TRec", function () {',
  34088. ' this.$eq = function (b) {',
  34089. ' return true;',
  34090. ' };',
  34091. ' this.$assign = function (s) {',
  34092. ' return this;',
  34093. ' };',
  34094. ' $mod.$rtti.$Record("TRec", {',
  34095. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  34096. ' });',
  34097. '});',
  34098. 'this.$rtti.$inherited("TInt", rtl.word, {',
  34099. ' attr: [this.TCustomAttribute, "Create", [3]]',
  34100. '});',
  34101. 'this.p = null;',
  34102. '']),
  34103. LinesToStr([ // $mod.$main
  34104. '$mod.p = $mod.$rtti["TMyClass"];',
  34105. '$mod.p = $mod.$rtti["TRec"];',
  34106. '$mod.p = $mod.$rtti["TInt"];',
  34107. '']));
  34108. end;
  34109. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  34110. begin
  34111. WithTypeInfo:=true;
  34112. StartProgram(false);
  34113. Add([
  34114. '{$modeswitch PrefixedAttributes}',
  34115. 'type',
  34116. ' TObject = class',
  34117. ' constructor Create;',
  34118. ' end;',
  34119. ' TCustomAttribute = class',
  34120. ' end;',
  34121. ' THelper = class helper for TCustomAttribute',
  34122. ' constructor Create(Id: word);',
  34123. ' end;',
  34124. ' [TCustom(3)]',
  34125. ' TMyInt = word;',
  34126. 'constructor TObject.Create; begin end;',
  34127. 'constructor THelper.Create(Id: word); begin end;',
  34128. 'begin',
  34129. ' if typeinfo(TMyInt)=nil then ;']);
  34130. ConvertProgram;
  34131. end;
  34132. procedure TTestModule.TestAttributes_InterfacesList;
  34133. begin
  34134. WithTypeInfo:=true;
  34135. StartProgram(false);
  34136. Add([
  34137. '{$mode Delphi}',
  34138. 'type',
  34139. ' TObject = class',
  34140. ' constructor Create;',
  34141. ' end;',
  34142. ' IInterface = interface end;',
  34143. ' TCustomAttribute = class',
  34144. ' end;',
  34145. ' Red = class(TCustomAttribute);',
  34146. ' Blue = class(TCustomAttribute);',
  34147. ' [Red]',
  34148. ' IBird<T> = interface',
  34149. ' procedure Fly;',
  34150. ' end;',
  34151. ' [Blue]',
  34152. ' IEagle = interface(IBird<Word>)',
  34153. ' procedure Dive;',
  34154. ' end;',
  34155. ' TAnt = class(TObject, IEagle)',
  34156. ' procedure Fly; virtual; abstract;',
  34157. ' procedure Dive; virtual; abstract;',
  34158. ' end;',
  34159. 'constructor TObject.Create;',
  34160. 'begin',
  34161. 'end;',
  34162. 'begin',
  34163. '']);
  34164. ConvertProgram;
  34165. CheckSource('TestAttributes_InterfacesList',
  34166. LinesToStr([ // statements
  34167. '$mod.$rtti.$Interface("IBird<System.Word>");',
  34168. 'rtl.createClass(this, "TObject", null, function () {',
  34169. ' this.$init = function () {',
  34170. ' };',
  34171. ' this.$final = function () {',
  34172. ' };',
  34173. ' this.Create = function () {',
  34174. ' return this;',
  34175. ' };',
  34176. '});',
  34177. 'rtl.createInterface(',
  34178. ' this,',
  34179. ' "IInterface",',
  34180. ' "{B92D5841-698D-3153-90C5-000000000000}",',
  34181. ' [],',
  34182. ' null,',
  34183. ' function () {',
  34184. ' this.$kind = "com";',
  34185. ' }',
  34186. ');',
  34187. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  34188. '});',
  34189. 'rtl.createClass(this, "Red", this.TCustomAttribute, function () {',
  34190. '});',
  34191. 'rtl.createClass(this, "Blue", this.TCustomAttribute, function () {',
  34192. '});',
  34193. 'rtl.createInterface(',
  34194. ' this,',
  34195. ' "IBird$G1",',
  34196. ' "{14691591-6648-3574-B8C8-FAAD81DAC421}",',
  34197. ' ["Fly"],',
  34198. ' this.IInterface,',
  34199. ' function () {',
  34200. ' var $r = this.$rtti;',
  34201. ' $r.addMethod("Fly", 0, []);',
  34202. ' $r.attr = [$mod.Red, "Create"];',
  34203. ' },',
  34204. ' "IBird<System.Word>"',
  34205. ');',
  34206. 'rtl.createInterface(',
  34207. ' this,',
  34208. ' "IEagle",',
  34209. ' "{5F4202AE-F2BE-37FD-8A88-1A2F926F1117}",',
  34210. ' ["Dive"],',
  34211. ' this.IBird$G1,',
  34212. ' function () {',
  34213. ' var $r = this.$rtti;',
  34214. ' $r.addMethod("Dive", 0, []);',
  34215. ' $r.attr = [$mod.Blue, "Create"];',
  34216. ' }',
  34217. ');',
  34218. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  34219. ' rtl.addIntf(this, $mod.IEagle);',
  34220. '});',
  34221. '']),
  34222. LinesToStr([ // $mod.$main
  34223. '']));
  34224. end;
  34225. procedure TTestModule.TestAssert;
  34226. begin
  34227. StartProgram(false);
  34228. Add([
  34229. 'procedure DoIt;',
  34230. 'var',
  34231. ' b: boolean;',
  34232. ' s: string;',
  34233. 'begin',
  34234. ' {$Assertions on}',
  34235. ' Assert(b);',
  34236. 'end;',
  34237. 'begin',
  34238. ' DoIt;',
  34239. '']);
  34240. ConvertProgram;
  34241. CheckSource('TestAssert',
  34242. LinesToStr([ // statements
  34243. 'this.DoIt = function () {',
  34244. ' var b = false;',
  34245. ' var s = "";',
  34246. ' if (!b) throw "assert failed";',
  34247. '};',
  34248. '']),
  34249. LinesToStr([ // $mod.$main
  34250. '$mod.DoIt();',
  34251. '']));
  34252. end;
  34253. procedure TTestModule.TestAssert_SysUtils;
  34254. begin
  34255. AddModuleWithIntfImplSrc('SysUtils.pas',
  34256. LinesToStr([
  34257. 'type',
  34258. ' TObject = class',
  34259. ' constructor Create;',
  34260. ' end;',
  34261. ' EAssertionFailed = class',
  34262. ' constructor Create(s: string);',
  34263. ' end;',
  34264. '']),
  34265. LinesToStr([
  34266. 'constructor TObject.Create;',
  34267. 'begin end;',
  34268. 'constructor EAssertionFailed.Create(s: string);',
  34269. 'begin end;',
  34270. '']) );
  34271. StartProgram(true);
  34272. Add([
  34273. 'uses sysutils;',
  34274. 'procedure DoIt;',
  34275. 'var',
  34276. ' b: boolean;',
  34277. ' s: string;',
  34278. 'begin',
  34279. ' {$Assertions on}',
  34280. ' Assert(b);',
  34281. ' Assert(b,''msg'');',
  34282. 'end;',
  34283. 'begin',
  34284. ' DoIt;',
  34285. '']);
  34286. ConvertProgram;
  34287. CheckSource('TestAssert_SysUtils',
  34288. LinesToStr([ // statements
  34289. 'this.DoIt = function () {',
  34290. ' var b = false;',
  34291. ' var s = "";',
  34292. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  34293. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  34294. '};',
  34295. '']),
  34296. LinesToStr([ // $mod.$main
  34297. '$mod.DoIt();',
  34298. '']));
  34299. end;
  34300. procedure TTestModule.TestObjectChecks;
  34301. begin
  34302. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  34303. StartProgram(false);
  34304. Add([
  34305. 'type',
  34306. ' TObject = class',
  34307. ' procedure DoIt;',
  34308. ' end;',
  34309. ' TClass = class of tobject;',
  34310. ' TBird = class',
  34311. ' end;',
  34312. ' TBirdClass = class of TBird;',
  34313. 'var',
  34314. ' o : TObject;',
  34315. ' c: TClass;',
  34316. ' b: TBird;',
  34317. ' bc: TBirdClass;',
  34318. 'procedure TObject.DoIt;',
  34319. 'begin',
  34320. ' b:=TBird(o);',
  34321. 'end;',
  34322. 'begin',
  34323. ' o.DoIt;',
  34324. ' b:=TBird(o);',
  34325. ' bc:=TBirdClass(c);',
  34326. '']);
  34327. ConvertProgram;
  34328. CheckSource('TestCheckMethodCall',
  34329. LinesToStr([ // statements
  34330. 'rtl.createClass(this, "TObject", null, function () {',
  34331. ' this.$init = function () {',
  34332. ' };',
  34333. ' this.$final = function () {',
  34334. ' };',
  34335. ' this.DoIt = function () {',
  34336. ' rtl.checkMethodCall(this,$mod.TObject);',
  34337. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  34338. ' };',
  34339. '});',
  34340. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34341. '});',
  34342. 'this.o = null;',
  34343. 'this.c = null;',
  34344. 'this.b = null;',
  34345. 'this.bc = null;',
  34346. '']),
  34347. LinesToStr([ // $mod.$main
  34348. '$mod.o.DoIt();',
  34349. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  34350. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  34351. '']));
  34352. end;
  34353. procedure TTestModule.TestOverflowChecks_Int;
  34354. begin
  34355. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  34356. StartProgram(false);
  34357. Add([
  34358. 'procedure DoIt;',
  34359. 'var',
  34360. ' b: byte;',
  34361. ' n: nativeint;',
  34362. ' u: nativeuint;',
  34363. ' c: currency;',
  34364. 'begin',
  34365. ' n:=n+n;',
  34366. ' n:=n-n;',
  34367. ' n:=n+b;',
  34368. ' n:=b-n;',
  34369. ' n:=n*n;',
  34370. ' n:=n*u;',
  34371. ' c:=c+b;',
  34372. ' c:=b+c;',
  34373. ' c:=c*b;',
  34374. ' c:=b*c;',
  34375. 'end;',
  34376. 'begin',
  34377. '']);
  34378. ConvertProgram;
  34379. CheckSource('TestOverflowChecks_Int',
  34380. LinesToStr([ // statements
  34381. 'this.DoIt = function () {',
  34382. ' var b = 0;',
  34383. ' var n = 0;',
  34384. ' var u = 0;',
  34385. ' var c = 0;',
  34386. ' n = rtl.oc(n + n);',
  34387. ' n = rtl.oc(n - n);',
  34388. ' n = rtl.oc(n + b);',
  34389. ' n = rtl.oc(b - n);',
  34390. ' n = rtl.oc(n * n);',
  34391. ' n = rtl.oc(n * u);',
  34392. ' c = rtl.oc(c + (b * 10000));',
  34393. ' c = rtl.oc((b * 10000) + c);',
  34394. ' c = rtl.oc(c * b);',
  34395. ' c = rtl.oc(b * c);',
  34396. '};',
  34397. '']),
  34398. LinesToStr([ // $mod.$main
  34399. '']));
  34400. end;
  34401. procedure TTestModule.TestRangeChecks_AssignInt;
  34402. begin
  34403. Scanner.Options:=Scanner.Options+[po_CAssignments];
  34404. StartProgram(false);
  34405. Add([
  34406. '{$R+}',
  34407. 'var',
  34408. ' b: byte = 2;',
  34409. ' w: word = 3;',
  34410. 'procedure DoIt(p: byte);',
  34411. 'begin',
  34412. ' b:=w;',
  34413. ' b+=w;',
  34414. ' b:=1;',
  34415. 'end;',
  34416. '{$R-}',
  34417. 'procedure DoSome;',
  34418. 'begin',
  34419. ' DoIt(w);',
  34420. ' b:=w;',
  34421. ' b:=2;',
  34422. 'end;',
  34423. 'begin',
  34424. '{$R+}',
  34425. '']);
  34426. ConvertProgram;
  34427. CheckSource('TestRangeChecks_AssignInt',
  34428. LinesToStr([ // statements
  34429. 'this.b = 2;',
  34430. 'this.w = 3;',
  34431. 'this.DoIt = function (p) {',
  34432. ' rtl.rc(p, 0, 255);',
  34433. ' $mod.b = rtl.rc($mod.w,0,255);',
  34434. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  34435. ' $mod.b = 1;',
  34436. '};',
  34437. 'this.DoSome = function () {',
  34438. ' $mod.DoIt($mod.w);',
  34439. ' $mod.b = $mod.w;',
  34440. ' $mod.b = 2;',
  34441. '};',
  34442. '']),
  34443. LinesToStr([ // $mod.$main
  34444. '']));
  34445. end;
  34446. procedure TTestModule.TestRangeChecks_AssignIntRange;
  34447. begin
  34448. Scanner.Options:=Scanner.Options+[po_CAssignments];
  34449. StartProgram(false);
  34450. Add([
  34451. '{$R+}',
  34452. 'type Ten = 1..10;',
  34453. 'var',
  34454. ' b: Ten = 2;',
  34455. ' w: Ten = 3;',
  34456. 'procedure DoIt(p: Ten);',
  34457. 'begin',
  34458. ' b:=w;',
  34459. ' b+=w;',
  34460. ' b:=1;',
  34461. 'end;',
  34462. '{$R-}',
  34463. 'procedure DoSome;',
  34464. 'begin',
  34465. ' DoIt(w);',
  34466. ' b:=w;',
  34467. ' b:=2;',
  34468. 'end;',
  34469. 'begin',
  34470. '{$R+}',
  34471. '']);
  34472. ConvertProgram;
  34473. CheckSource('TestRangeChecks_AssignIntRange',
  34474. LinesToStr([ // statements
  34475. 'this.b = 2;',
  34476. 'this.w = 3;',
  34477. 'this.DoIt = function (p) {',
  34478. ' rtl.rc(p, 1, 10);',
  34479. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  34480. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  34481. ' $mod.b = 1;',
  34482. '};',
  34483. 'this.DoSome = function () {',
  34484. ' $mod.DoIt($mod.w);',
  34485. ' $mod.b = $mod.w;',
  34486. ' $mod.b = 2;',
  34487. '};',
  34488. '']),
  34489. LinesToStr([ // $mod.$main
  34490. '']));
  34491. end;
  34492. procedure TTestModule.TestRangeChecks_AssignEnum;
  34493. begin
  34494. StartProgram(false);
  34495. Add([
  34496. '{$R+}',
  34497. 'type TEnum = (red,green);',
  34498. 'var',
  34499. ' e: TEnum = red;',
  34500. 'procedure DoIt(p: TEnum);',
  34501. 'begin',
  34502. ' e:=p;',
  34503. ' p:=TEnum(0);',
  34504. ' p:=succ(e);',
  34505. 'end;',
  34506. '{$R-}',
  34507. 'procedure DoSome;',
  34508. 'begin',
  34509. ' DoIt(e);',
  34510. ' e:=TEnum(1);',
  34511. ' e:=pred(e);',
  34512. 'end;',
  34513. 'begin',
  34514. '{$R+}',
  34515. '']);
  34516. ConvertProgram;
  34517. CheckSource('TestRangeChecks_AssignEnum',
  34518. LinesToStr([ // statements
  34519. 'this.TEnum = {',
  34520. ' "0": "red",',
  34521. ' red: 0,',
  34522. ' "1": "green",',
  34523. ' green: 1',
  34524. '};',
  34525. 'this.e = this.TEnum.red;',
  34526. 'this.DoIt = function (p) {',
  34527. ' rtl.rc(p, 0, 1);',
  34528. ' $mod.e = rtl.rc(p, 0, 1);',
  34529. ' p = 0;',
  34530. ' p = rtl.rc($mod.e + 1, 0, 1);',
  34531. '};',
  34532. 'this.DoSome = function () {',
  34533. ' $mod.DoIt($mod.e);',
  34534. ' $mod.e = 1;',
  34535. ' $mod.e = $mod.e - 1;',
  34536. '};',
  34537. '']),
  34538. LinesToStr([ // $mod.$main
  34539. '']));
  34540. end;
  34541. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  34542. begin
  34543. StartProgram(false);
  34544. Add([
  34545. '{$R+}',
  34546. 'type',
  34547. ' TEnum = (red,green);',
  34548. ' TEnumRg = red..green;',
  34549. 'var',
  34550. ' e: TEnumRg = red;',
  34551. 'procedure DoIt(p: TEnumRg);',
  34552. 'begin',
  34553. ' e:=p;',
  34554. ' p:=TEnumRg(0);',
  34555. ' p:=succ(e);',
  34556. 'end;',
  34557. '{$R-}',
  34558. 'procedure DoSome;',
  34559. 'begin',
  34560. ' DoIt(e);',
  34561. ' e:=TEnum(1);',
  34562. ' e:=pred(e);',
  34563. 'end;',
  34564. 'begin',
  34565. '{$R+}',
  34566. '']);
  34567. ConvertProgram;
  34568. CheckSource('TestRangeChecks_AssignEnumRange',
  34569. LinesToStr([ // statements
  34570. 'this.TEnum = {',
  34571. ' "0": "red",',
  34572. ' red: 0,',
  34573. ' "1": "green",',
  34574. ' green: 1',
  34575. '};',
  34576. 'this.e = this.TEnum.red;',
  34577. 'this.DoIt = function (p) {',
  34578. ' rtl.rc(p, 0, 1);',
  34579. ' $mod.e = rtl.rc(p, 0, 1);',
  34580. ' p = 0;',
  34581. ' p = rtl.rc($mod.e + 1, 0, 1);',
  34582. '};',
  34583. 'this.DoSome = function () {',
  34584. ' $mod.DoIt($mod.e);',
  34585. ' $mod.e = 1;',
  34586. ' $mod.e = $mod.e - 1;',
  34587. '};',
  34588. '']),
  34589. LinesToStr([ // $mod.$main
  34590. '']));
  34591. end;
  34592. procedure TTestModule.TestRangeChecks_AssignChar;
  34593. begin
  34594. StartProgram(false);
  34595. Add([
  34596. '{$R+}',
  34597. 'type',
  34598. ' TLetter = char;',
  34599. 'var',
  34600. ' b: TLetter = ''2'';',
  34601. ' w: TLetter = ''3'';',
  34602. 'procedure DoIt(p: TLetter);',
  34603. 'begin',
  34604. ' b:=w;',
  34605. ' b:=''1'';',
  34606. 'end;',
  34607. '{$R-}',
  34608. 'procedure DoSome;',
  34609. 'begin',
  34610. ' DoIt(w);',
  34611. ' b:=w;',
  34612. ' b:=''2'';',
  34613. 'end;',
  34614. 'begin',
  34615. '{$R+}',
  34616. '']);
  34617. ConvertProgram;
  34618. CheckSource('TestRangeChecks_AssignChar',
  34619. LinesToStr([ // statements
  34620. 'this.b = "2";',
  34621. 'this.w = "3";',
  34622. 'this.DoIt = function (p) {',
  34623. ' rtl.rcc(p, 0, 65535);',
  34624. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  34625. ' $mod.b = "1";',
  34626. '};',
  34627. 'this.DoSome = function () {',
  34628. ' $mod.DoIt($mod.w);',
  34629. ' $mod.b = $mod.w;',
  34630. ' $mod.b = "2";',
  34631. '};',
  34632. '']),
  34633. LinesToStr([ // $mod.$main
  34634. '']));
  34635. end;
  34636. procedure TTestModule.TestRangeChecks_AssignCharRange;
  34637. begin
  34638. StartProgram(false);
  34639. Add([
  34640. '{$R+}',
  34641. 'type TDigit = ''0''..''9'';',
  34642. 'var',
  34643. ' b: TDigit = ''2'';',
  34644. ' w: TDigit = ''3'';',
  34645. 'procedure DoIt(p: TDigit);',
  34646. 'begin',
  34647. ' b:=w;',
  34648. ' b:=''1'';',
  34649. 'end;',
  34650. '{$R-}',
  34651. 'procedure DoSome;',
  34652. 'begin',
  34653. ' DoIt(w);',
  34654. ' b:=w;',
  34655. ' b:=''2'';',
  34656. 'end;',
  34657. 'begin',
  34658. '{$R+}',
  34659. '']);
  34660. ConvertProgram;
  34661. CheckSource('TestRangeChecks_AssignCharRange',
  34662. LinesToStr([ // statements
  34663. 'this.b = "2";',
  34664. 'this.w = "3";',
  34665. 'this.DoIt = function (p) {',
  34666. ' rtl.rcc(p, 48, 57);',
  34667. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  34668. ' $mod.b = "1";',
  34669. '};',
  34670. 'this.DoSome = function () {',
  34671. ' $mod.DoIt($mod.w);',
  34672. ' $mod.b = $mod.w;',
  34673. ' $mod.b = "2";',
  34674. '};',
  34675. '']),
  34676. LinesToStr([ // $mod.$main
  34677. '']));
  34678. end;
  34679. procedure TTestModule.TestRangeChecks_ArrayIndex;
  34680. begin
  34681. StartProgram(false);
  34682. Add([
  34683. '{$R+}',
  34684. 'type',
  34685. ' Ten = 1..10;',
  34686. ' TArr = array of Ten;',
  34687. ' TArrArr = array of TArr;',
  34688. ' TArrByte = array[byte] of Ten;',
  34689. ' TArrChar = array[''0''..''9''] of Ten;',
  34690. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  34691. ' TObject = class',
  34692. ' A: TArr;',
  34693. ' end;',
  34694. 'procedure DoIt;',
  34695. 'var',
  34696. ' Arr: TArr;',
  34697. ' ArrArr: TArrArr;',
  34698. ' ArrByte: TArrByte;',
  34699. ' ArrChar: TArrChar;',
  34700. ' ArrByteChar: TArrByteChar;',
  34701. ' i: Ten;',
  34702. ' c: char;',
  34703. ' o: tobject;',
  34704. 'begin',
  34705. ' i:=Arr[1];',
  34706. ' i:=ArrByteChar[1,''2''];',
  34707. ' Arr[1]:=Arr[1];',
  34708. ' Arr[i]:=Arr[i];',
  34709. ' ArrByte[3]:=ArrByte[3];',
  34710. ' ArrByte[i]:=ArrByte[i];',
  34711. ' ArrChar[''5'']:=ArrChar[''5''];',
  34712. ' ArrChar[c]:=ArrChar[c];',
  34713. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  34714. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  34715. ' o.a[i]:=o.a[i];',
  34716. 'end;',
  34717. 'begin',
  34718. '']);
  34719. ConvertProgram;
  34720. CheckSource('TestRangeChecks_ArrayIndex',
  34721. LinesToStr([ // statements
  34722. 'this.TArrByteChar$clone = function (a) {',
  34723. ' var b = [];',
  34724. ' b.length = 256;',
  34725. ' for (var c = 0; c < 256; c++) b[c] = a[c].slice(0);',
  34726. ' return b;',
  34727. '};',
  34728. 'rtl.createClass(this, "TObject", null, function () {',
  34729. ' this.$init = function () {',
  34730. ' this.A = [];',
  34731. ' };',
  34732. ' this.$final = function () {',
  34733. ' this.A = undefined;',
  34734. ' };',
  34735. '});',
  34736. 'this.DoIt = function () {',
  34737. ' var Arr = [];',
  34738. ' var ArrArr = [];',
  34739. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  34740. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  34741. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  34742. ' var i = 0;',
  34743. ' var c = "\x00";',
  34744. ' var o = null;',
  34745. ' i = rtl.rc(Arr[1], 1, 10);',
  34746. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  34747. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  34748. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  34749. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  34750. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  34751. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  34752. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  34753. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  34754. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  34755. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  34756. '};',
  34757. '']),
  34758. LinesToStr([ // $mod.$main
  34759. '']));
  34760. end;
  34761. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  34762. begin
  34763. StartProgram(false);
  34764. Add([
  34765. '{$R+}',
  34766. 'type',
  34767. ' Ten = 1..10;',
  34768. ' TRec = record x: Ten end;',
  34769. ' TArr = array of TRec;',
  34770. ' TArrArr = array of TArr;',
  34771. ' TObject = class',
  34772. ' A: TArr;',
  34773. ' end;',
  34774. 'procedure DoIt;',
  34775. 'var',
  34776. ' Arr: TArr;',
  34777. ' ArrArr: TArrArr;',
  34778. ' i: Ten;',
  34779. ' o: tobject;',
  34780. 'begin',
  34781. ' Arr[1]:=Arr[1];',
  34782. ' Arr[i]:=Arr[i+1];',
  34783. ' o.a[i]:=o.a[i+2];',
  34784. 'end;',
  34785. 'begin',
  34786. '']);
  34787. ConvertProgram;
  34788. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  34789. LinesToStr([ // statements
  34790. 'rtl.recNewT(this, "TRec", function () {',
  34791. ' this.x = 0;',
  34792. ' this.$eq = function (b) {',
  34793. ' return this.x === b.x;',
  34794. ' };',
  34795. ' this.$assign = function (s) {',
  34796. ' this.x = s.x;',
  34797. ' return this;',
  34798. ' };',
  34799. '});',
  34800. 'rtl.createClass(this, "TObject", null, function () {',
  34801. ' this.$init = function () {',
  34802. ' this.A = [];',
  34803. ' };',
  34804. ' this.$final = function () {',
  34805. ' this.A = undefined;',
  34806. ' };',
  34807. '});',
  34808. 'this.DoIt = function () {',
  34809. ' var Arr = [];',
  34810. ' var ArrArr = [];',
  34811. ' var i = 0;',
  34812. ' var o = null;',
  34813. ' Arr[1].$assign(Arr[1]);',
  34814. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  34815. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  34816. '};',
  34817. '']),
  34818. LinesToStr([ // $mod.$main
  34819. '']));
  34820. end;
  34821. procedure TTestModule.TestRangeChecks_StringIndex;
  34822. begin
  34823. StartProgram(false);
  34824. Add([
  34825. 'type',
  34826. ' TObject = class',
  34827. ' S: string;',
  34828. ' end;',
  34829. '{$R+}',
  34830. 'procedure DoIt(var h: string);',
  34831. 'var',
  34832. ' s: string;',
  34833. ' i: longint;',
  34834. ' c: char;',
  34835. ' o: tobject;',
  34836. 'begin',
  34837. ' c:=s[1];',
  34838. ' s[i]:=s[i];',
  34839. ' h[i]:=h[i];',
  34840. ' c:=o.s[i];',
  34841. ' o.s[i]:=c;',
  34842. 'end;',
  34843. 'begin',
  34844. '']);
  34845. ConvertProgram;
  34846. CheckSource('TestRangeChecks_StringIndex',
  34847. LinesToStr([ // statements
  34848. 'rtl.createClass(this, "TObject", null, function () {',
  34849. ' this.$init = function () {',
  34850. ' this.S = "";',
  34851. ' };',
  34852. ' this.$final = function () {',
  34853. ' };',
  34854. '});',
  34855. 'this.DoIt = function (h) {',
  34856. ' var s = "";',
  34857. ' var i = 0;',
  34858. ' var c = "\x00";',
  34859. ' var o = null;',
  34860. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  34861. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  34862. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  34863. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  34864. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  34865. '};',
  34866. '']),
  34867. LinesToStr([ // $mod.$main
  34868. '']));
  34869. end;
  34870. procedure TTestModule.TestRangeChecks_TypecastInt;
  34871. begin
  34872. StartProgram(false);
  34873. Add([
  34874. '{$R+}',
  34875. 'var',
  34876. ' i: nativeint;',
  34877. ' b: byte;',
  34878. ' sh: shortint;',
  34879. ' w: word;',
  34880. ' sm: smallint;',
  34881. ' lw: longword;',
  34882. ' li: longint;',
  34883. 'begin',
  34884. ' b:=12+byte(i);',
  34885. ' sh:=12+shortint(i);',
  34886. ' w:=12+word(i);',
  34887. ' sm:=12+smallint(i);',
  34888. ' lw:=12+longword(i);',
  34889. ' li:=12+longint(i);',
  34890. '']);
  34891. ConvertProgram;
  34892. CheckSource('TestRangeChecks_TypecastInt',
  34893. LinesToStr([
  34894. 'this.i = 0;',
  34895. 'this.b = 0;',
  34896. 'this.sh = 0;',
  34897. 'this.w = 0;',
  34898. 'this.sm = 0;',
  34899. 'this.lw = 0;',
  34900. 'this.li = 0;',
  34901. '']),
  34902. LinesToStr([
  34903. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  34904. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  34905. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  34906. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  34907. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  34908. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  34909. '']));
  34910. end;
  34911. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  34912. begin
  34913. Scanner.Options:=Scanner.Options+[po_CAssignments];
  34914. StartProgram(false);
  34915. Add([
  34916. '{$modeswitch typehelpers}',
  34917. '{$R+}',
  34918. 'type',
  34919. ' TObject = class',
  34920. ' FSize: byte;',
  34921. ' property Size: byte read FSize;',
  34922. ' end;',
  34923. ' THelper = type helper for byte',
  34924. ' procedure SetIt(w: word);',
  34925. ' end;',
  34926. 'procedure THelper.SetIt(w: word);',
  34927. 'begin',
  34928. ' Self:=w;',
  34929. 'end;',
  34930. 'function GetIt: byte;',
  34931. 'begin',
  34932. ' Result.SetIt(2);',
  34933. 'end;',
  34934. 'var',
  34935. ' b: byte = 3;',
  34936. ' o: TObject;',
  34937. 'begin',
  34938. ' b.SetIt(14);',
  34939. ' with b do SetIt(15);',
  34940. ' o.Size.SetIt(16);',
  34941. '']);
  34942. ConvertProgram;
  34943. CheckSource('TestRangeChecks_AssignInt',
  34944. LinesToStr([ // statements
  34945. 'rtl.createClass(this, "TObject", null, function () {',
  34946. ' this.$init = function () {',
  34947. ' this.FSize = 0;',
  34948. ' };',
  34949. ' this.$final = function () {',
  34950. ' };',
  34951. '});',
  34952. 'rtl.createHelper(this, "THelper", null, function () {',
  34953. ' this.SetIt = function (w) {',
  34954. ' rtl.rc(w, 0, 65535);',
  34955. ' this.set(w);',
  34956. ' };',
  34957. '});',
  34958. 'this.GetIt = function () {',
  34959. ' var Result = 0;',
  34960. ' $mod.THelper.SetIt.call({',
  34961. ' get: function () {',
  34962. ' return Result;',
  34963. ' },',
  34964. ' set: function (v) {',
  34965. ' rtl.rc(v, 0, 255);',
  34966. ' Result = v;',
  34967. ' }',
  34968. ' }, 2);',
  34969. ' return Result;',
  34970. '};',
  34971. 'this.b = 3;',
  34972. 'this.o = null;',
  34973. '']),
  34974. LinesToStr([ // $mod.$main
  34975. '$mod.THelper.SetIt.call({',
  34976. ' p: $mod,',
  34977. ' get: function () {',
  34978. ' return this.p.b;',
  34979. ' },',
  34980. ' set: function (v) {',
  34981. ' rtl.rc(v, 0, 255);',
  34982. ' this.p.b = v;',
  34983. ' }',
  34984. '}, 14);',
  34985. 'var $with = $mod.b;',
  34986. '$mod.THelper.SetIt.call({',
  34987. ' get: function () {',
  34988. ' return $with;',
  34989. ' },',
  34990. ' set: function (v) {',
  34991. ' rtl.rc(v, 0, 255);',
  34992. ' $with = v;',
  34993. ' }',
  34994. '}, 15);',
  34995. '$mod.THelper.SetIt.call({',
  34996. ' p: $mod.o,',
  34997. ' get: function () {',
  34998. ' return this.p.FSize;',
  34999. ' },',
  35000. ' set: function (v) {',
  35001. ' rtl.rc(v, 0, 255);',
  35002. ' this.p.FSize = v;',
  35003. ' }',
  35004. '}, 16);',
  35005. '']));
  35006. end;
  35007. procedure TTestModule.TestRangeChecks_AssignCurrency;
  35008. begin
  35009. Scanner.Options:=Scanner.Options+[po_CAssignments];
  35010. StartProgram(false);
  35011. Add([
  35012. '{$R+}',
  35013. 'var',
  35014. ' c: currency = 2.34;',
  35015. ' i: double;',
  35016. 'procedure DoIt(p: currency);',
  35017. 'begin',
  35018. ' c:=i;',
  35019. ' c+=i;',
  35020. ' c:=1;',
  35021. 'end;',
  35022. '{$R-}',
  35023. 'procedure DoSome;',
  35024. 'begin',
  35025. ' DoIt(i);',
  35026. ' c:=i;',
  35027. ' c:=2;',
  35028. 'end;',
  35029. 'begin',
  35030. '{$R+}',
  35031. '']);
  35032. ConvertProgram;
  35033. CheckSource('TestRangeChecks_AssignCurrency',
  35034. LinesToStr([ // statements
  35035. 'this.c = 2.34;',
  35036. 'this.i = 0.0;',
  35037. 'this.DoIt = function (p) {',
  35038. ' rtl.rc(p, -922337203685477, 922337203685477);',
  35039. ' $mod.c = rtl.rc(rtl.trunc($mod.i * 10000), -922337203685477, 922337203685477);',
  35040. ' rtl.rc($mod.c += rtl.trunc($mod.i * 10000), -922337203685477, 922337203685477);',
  35041. ' $mod.c = 10000;',
  35042. '};',
  35043. 'this.DoSome = function () {',
  35044. ' $mod.DoIt($mod.i * 10000);',
  35045. ' $mod.c = rtl.trunc($mod.i * 10000);',
  35046. ' $mod.c = 20000;',
  35047. '};',
  35048. '']),
  35049. LinesToStr([ // $mod.$main
  35050. '']));
  35051. end;
  35052. procedure TTestModule.TestAsync_Proc;
  35053. begin
  35054. StartProgram(false);
  35055. Add([
  35056. 'procedure Fly(w: word = 1); async; forward;',
  35057. 'procedure Run(w: word = 2); async;',
  35058. 'begin',
  35059. ' Fly(w);',
  35060. ' Fly;',
  35061. ' await(Fly(w));',
  35062. ' await(Fly);',
  35063. 'end;',
  35064. 'procedure Fly(w: word); ',
  35065. 'begin',
  35066. 'end;',
  35067. 'begin',
  35068. ' Run;',
  35069. ' Run(3);',
  35070. '']);
  35071. CheckResolverUnexpectedHints();
  35072. ConvertProgram;
  35073. CheckSource('TestAsync_Proc',
  35074. LinesToStr([ // statements
  35075. 'this.Run = async function (w) {',
  35076. ' $mod.Fly(w);',
  35077. ' $mod.Fly(1);',
  35078. ' await $mod.Fly(w);',
  35079. ' await $mod.Fly(1);',
  35080. '};',
  35081. 'this.Fly = async function (w) {',
  35082. '};',
  35083. '']),
  35084. LinesToStr([
  35085. '$mod.Run(2);',
  35086. '$mod.Run(3);',
  35087. '']));
  35088. end;
  35089. procedure TTestModule.TestAsync_CallResultIsPromise;
  35090. begin
  35091. StartProgram(false);
  35092. Add([
  35093. '{$modeswitch externalclass}',
  35094. 'type',
  35095. ' TObject = class',
  35096. ' end;',
  35097. ' TJSPromise = class external name ''Promise''',
  35098. ' end;',
  35099. ' TBird = class',
  35100. ' function Fly: word; async; ',
  35101. ' end;',
  35102. 'function TBird.Fly: word; async; ',
  35103. 'begin',
  35104. ' Result:=3;',
  35105. ' Fly:=4+Result;',
  35106. ' if Result=5 then ;',
  35107. ' exit(6);',
  35108. 'end;',
  35109. 'function Run: word; async;',
  35110. 'begin',
  35111. ' Result:=11+Result;',
  35112. ' inc(Result);',
  35113. 'end;',
  35114. 'var',
  35115. ' p: TJSPromise;',
  35116. ' o: TBird;',
  35117. 'begin',
  35118. ' p:=Run;',
  35119. ' p:=Run();',
  35120. ' if Run=p then ;',
  35121. ' if p=Run then ;',
  35122. ' if Run()=p then ;',
  35123. ' if p=Run() then ;',
  35124. ' p:=o.Fly;',
  35125. ' p:=o.Fly();',
  35126. ' if o.Fly=p then ;',
  35127. ' if o.Fly()=p then ;',
  35128. ' with o do begin',
  35129. ' p:=Fly;',
  35130. ' p:=Fly();',
  35131. ' if Fly=p then ;',
  35132. ' if Fly()=p then ;',
  35133. ' end;',
  35134. '']);
  35135. CheckResolverUnexpectedHints();
  35136. ConvertProgram;
  35137. CheckSource('TestAsync_CallResultIsPromise',
  35138. LinesToStr([ // statements
  35139. 'rtl.createClass(this, "TObject", null, function () {',
  35140. ' this.$init = function () {',
  35141. ' };',
  35142. ' this.$final = function () {',
  35143. ' };',
  35144. '});',
  35145. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  35146. ' this.Fly = async function () {',
  35147. ' var Result = 0;',
  35148. ' Result = 3;',
  35149. ' Result = 4 + Result;',
  35150. ' if (Result === 5) ;',
  35151. ' return 6;',
  35152. ' return Result;',
  35153. ' };',
  35154. '});',
  35155. 'this.Run = async function () {',
  35156. ' var Result = 0;',
  35157. ' Result = 11 + Result;',
  35158. ' Result += 1;',
  35159. ' return Result;',
  35160. '};',
  35161. 'this.p = null;',
  35162. 'this.o = null;',
  35163. '']),
  35164. LinesToStr([
  35165. '$mod.p = $mod.Run();',
  35166. '$mod.p = $mod.Run();',
  35167. 'if ($mod.Run() === $mod.p) ;',
  35168. 'if ($mod.p === $mod.Run()) ;',
  35169. 'if ($mod.Run() === $mod.p) ;',
  35170. 'if ($mod.p === $mod.Run()) ;',
  35171. '$mod.p = $mod.o.Fly();',
  35172. '$mod.p = $mod.o.Fly();',
  35173. 'if ($mod.o.Fly() === $mod.p) ;',
  35174. 'if ($mod.o.Fly() === $mod.p) ;',
  35175. 'var $with = $mod.o;',
  35176. '$mod.p = $with.Fly();',
  35177. '$mod.p = $with.Fly();',
  35178. 'if ($with.Fly() === $mod.p) ;',
  35179. 'if ($with.Fly() === $mod.p) ;',
  35180. '']));
  35181. end;
  35182. procedure TTestModule.TestAsync_ConstructorFail;
  35183. begin
  35184. StartProgram(false);
  35185. Add([
  35186. 'type',
  35187. ' TObject = class',
  35188. ' end;',
  35189. ' TBird = class',
  35190. ' constructor Create; async;',
  35191. ' end;',
  35192. 'constructor TBird.Create; async;',
  35193. 'begin',
  35194. 'end;',
  35195. 'begin',
  35196. '']);
  35197. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  35198. ConvertProgram;
  35199. end;
  35200. procedure TTestModule.TestAsync_PropertyGetterFail;
  35201. begin
  35202. StartProgram(false);
  35203. Add([
  35204. 'type',
  35205. ' TObject = class',
  35206. ' end;',
  35207. ' TBird = class',
  35208. ' function GetSize: word; async;',
  35209. ' property Size: word read GetSize;',
  35210. ' end;',
  35211. 'function TBird.GetSize: word; async;',
  35212. 'begin',
  35213. 'end;',
  35214. 'begin',
  35215. '']);
  35216. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  35217. ConvertProgram;
  35218. end;
  35219. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  35220. begin
  35221. StartProgram(false);
  35222. Add([
  35223. 'procedure Run; async;',
  35224. 'begin',
  35225. ' await(word,1);',
  35226. 'end;',
  35227. 'begin',
  35228. '']);
  35229. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  35230. ConvertProgram;
  35231. end;
  35232. procedure TTestModule.TestAwait_AsyncCallTypeMismatch;
  35233. begin
  35234. StartProgram(false);
  35235. Add([
  35236. 'type',
  35237. ' TObject = class',
  35238. ' end;',
  35239. ' TBird = class',
  35240. ' end;',
  35241. 'function Fly: TObject; async;',
  35242. 'begin',
  35243. 'end;',
  35244. 'procedure Run; async;',
  35245. 'begin',
  35246. ' await(TBird,Fly);',
  35247. 'end;',
  35248. 'begin',
  35249. '']);
  35250. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "TObject", expected "TBird"',nIncompatibleTypeArgNo);
  35251. ConvertProgram;
  35252. end;
  35253. procedure TTestModule.TestAWait_OutsideAsyncFail;
  35254. begin
  35255. StartProgram(false);
  35256. Add([
  35257. 'procedure Crawl(w: double); ',
  35258. 'begin',
  35259. 'end;',
  35260. 'procedure Run(w: double);',
  35261. 'begin',
  35262. ' await(Crawl(w));',
  35263. 'end;',
  35264. 'begin',
  35265. ' Run(1);']);
  35266. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  35267. ConvertProgram;
  35268. end;
  35269. procedure TTestModule.TestAWait_IntegerFail;
  35270. begin
  35271. StartProgram(false);
  35272. Add([
  35273. 'function Run: word;',
  35274. 'begin',
  35275. 'end;',
  35276. 'procedure Fly(w: word); async;',
  35277. 'begin',
  35278. ' await(Run());',
  35279. 'end;',
  35280. 'begin',
  35281. ' Fly(1);']);
  35282. SetExpectedPasResolverError('async function or promise expected, but Result:Word found',nXExpectedButYFound);
  35283. ConvertProgram;
  35284. end;
  35285. procedure TTestModule.TestAWait_ExternalClassPromise;
  35286. begin
  35287. StartProgram(false);
  35288. Add([
  35289. '{$modeswitch externalclass}',
  35290. 'type',
  35291. ' TJSPromise = class external name ''Promise''',
  35292. ' end;',
  35293. ' TJSThenable = class external name ''Thenable''',
  35294. ' end;',
  35295. 'function Fly(w: word): TJSPromise;',
  35296. 'begin',
  35297. 'end;',
  35298. 'function Jump(w: word): word; async;',
  35299. 'begin',
  35300. 'end;',
  35301. 'function Eat(w: word): TJSPromise; async;',
  35302. 'begin',
  35303. 'end;',
  35304. 'function Run(d: double): word; async;',
  35305. 'var',
  35306. ' p: TJSPromise;',
  35307. 'begin',
  35308. ' Result:=await(word,p);', // promise needs type
  35309. ' Result:=await(word,Fly(3));', // promise needs type
  35310. ' Result:=await(Jump(4));', // async non promise must omit the type
  35311. ' Result:=await(word,Jump(5));', // async call can provide fitting type
  35312. ' Result:=await(word,Eat(6));', // promise needs type
  35313. 'end;',
  35314. 'begin',
  35315. '']);
  35316. ConvertProgram;
  35317. CheckSource('TestAWait_ExternalClassPromise',
  35318. LinesToStr([ // statements
  35319. 'this.Fly = function (w) {',
  35320. ' var Result = null;',
  35321. ' return Result;',
  35322. '};',
  35323. 'this.Jump = async function (w) {',
  35324. ' var Result = 0;',
  35325. ' return Result;',
  35326. '};',
  35327. 'this.Eat = async function (w) {',
  35328. ' var Result = null;',
  35329. ' return Result;',
  35330. '};',
  35331. 'this.Run = async function (d) {',
  35332. ' var Result = 0;',
  35333. ' var p = null;',
  35334. ' Result = await p;',
  35335. ' Result = await $mod.Fly(3);',
  35336. ' Result = await $mod.Jump(4);',
  35337. ' Result = await $mod.Jump(5);',
  35338. ' Result = await $mod.Eat(6);',
  35339. ' return Result;',
  35340. '};',
  35341. '']),
  35342. LinesToStr([
  35343. ]));
  35344. CheckResolverUnexpectedHints();
  35345. end;
  35346. procedure TTestModule.TestAWait_JSValue;
  35347. begin
  35348. StartProgram(false);
  35349. Add([
  35350. '{$modeswitch externalclass}',
  35351. 'type',
  35352. ' TJSPromise = class external name ''Promise''',
  35353. ' end;',
  35354. 'function Fly(w: word): jsvalue; async;',
  35355. 'begin',
  35356. 'end;',
  35357. 'function Run(d: jsvalue; var e): word; async;',
  35358. 'begin',
  35359. ' Result:=await(word,d);', // promise needs type
  35360. ' d:=await(Fly(4));', // async non promise must omit the type
  35361. ' Result:=await(word,e);', // promise needs type
  35362. 'end;',
  35363. 'begin',
  35364. '']);
  35365. ConvertProgram;
  35366. CheckSource('TestAWait_JSValue',
  35367. LinesToStr([ // statements
  35368. 'this.Fly = async function (w) {',
  35369. ' var Result = undefined;',
  35370. ' return Result;',
  35371. '};',
  35372. 'this.Run = async function (d, e) {',
  35373. ' var Result = 0;',
  35374. ' Result = await d;',
  35375. ' d = await $mod.Fly(4);',
  35376. ' Result = await e.get();',
  35377. ' return Result;',
  35378. '};',
  35379. '']),
  35380. LinesToStr([
  35381. ]));
  35382. CheckResolverUnexpectedHints();
  35383. end;
  35384. procedure TTestModule.TestAWait_Result;
  35385. begin
  35386. StartProgram(false);
  35387. Add([
  35388. '{$modeswitch externalclass}',
  35389. 'type',
  35390. ' TJSPromise = class external name ''Promise''',
  35391. ' end;',
  35392. 'function Crawl(d: double = 1.3): TJSPromise; ',
  35393. 'begin',
  35394. 'end;',
  35395. 'function Run(d: double = 1.6): word; async;',
  35396. 'begin',
  35397. ' Result:=await(word,Crawl);',
  35398. ' Result:=await(word,Crawl(4.5));',
  35399. ' Result:=await(Run);',
  35400. ' Result:=await(Run(6.7));',
  35401. 'end;',
  35402. 'begin',
  35403. ' Run(1);']);
  35404. ConvertProgram;
  35405. CheckSource('TestAWait_Result',
  35406. LinesToStr([ // statements
  35407. 'this.Crawl = function (d) {',
  35408. ' var Result = null;',
  35409. ' return Result;',
  35410. '};',
  35411. 'this.Run = async function (d) {',
  35412. ' var Result = 0;',
  35413. ' Result = await $mod.Crawl(1.3);',
  35414. ' Result = await $mod.Crawl(4.5);',
  35415. ' Result = await $mod.Run(1.6);',
  35416. ' Result = await $mod.Run(6.7);',
  35417. ' return Result;',
  35418. '};',
  35419. '']),
  35420. LinesToStr([
  35421. '$mod.Run(1);'
  35422. ]));
  35423. CheckResolverUnexpectedHints();
  35424. end;
  35425. procedure TTestModule.TestAWait_ResultPromiseMissingTypeFail;
  35426. begin
  35427. StartProgram(false);
  35428. Add([
  35429. '{$mode objfpc}',
  35430. '{$modeswitch externalclass}',
  35431. 'type',
  35432. ' TJSPromise = class external name ''Promise''',
  35433. ' end;',
  35434. 'function Run: TJSPromise; async;',
  35435. 'begin',
  35436. 'end;',
  35437. 'procedure Fly(w: word); async;',
  35438. 'begin',
  35439. ' await(Run());',
  35440. 'end;',
  35441. 'begin',
  35442. ' Fly(1);']);
  35443. SetExpectedPasResolverError('Wrong number of parameters specified for call to "function await(aType,TJSPromise):aType"',
  35444. nWrongNumberOfParametersForCallTo);
  35445. ConvertProgram;
  35446. end;
  35447. procedure TTestModule.TestAsync_AnonymousProc;
  35448. begin
  35449. StartProgram(false);
  35450. Add([
  35451. '{$mode objfpc}',
  35452. '{$modeswitch externalclass}',
  35453. 'type',
  35454. ' TJSPromise = class external name ''Promise''',
  35455. ' end;',
  35456. 'type',
  35457. ' TFunc = reference to function(x: double): word; async;',
  35458. 'function Crawl(d: double = 1.3): word; async;',
  35459. 'begin',
  35460. 'end;',
  35461. 'var Func: TFunc;',
  35462. 'begin',
  35463. ' Func:=function(c:double):word async begin',
  35464. ' Result:=await(Crawl(c));',
  35465. ' end;',
  35466. ' Func:=function(c:double):word async assembler asm',
  35467. ' end;',
  35468. '']);
  35469. ConvertProgram;
  35470. CheckSource('TestAsync_AnonymousProc',
  35471. LinesToStr([ // statements
  35472. 'this.Crawl = async function (d) {',
  35473. ' var Result = 0;',
  35474. ' return Result;',
  35475. '};',
  35476. 'this.Func = null;',
  35477. '']),
  35478. LinesToStr([
  35479. '$mod.Func = async function (c) {',
  35480. ' var Result = 0;',
  35481. ' Result = await $mod.Crawl(c);',
  35482. ' return Result;',
  35483. '};',
  35484. '$mod.Func = async function (c) {',
  35485. '};',
  35486. '']));
  35487. CheckResolverUnexpectedHints();
  35488. end;
  35489. procedure TTestModule.TestAsync_AnonymousProc_PromiseViaDotContext;
  35490. begin
  35491. StartProgram(false);
  35492. Add([
  35493. '{$mode objfpc}',
  35494. '{$modeswitch externalclass}',
  35495. 'type',
  35496. ' TJSPromise = class external name ''Promise''',
  35497. ' end;',
  35498. ' TObject = class',
  35499. ' public',
  35500. ' procedure Fly(Prom: TJSPromise);',
  35501. ' end;',
  35502. ' TFunc = reference to procedure(Bird: TObject);',
  35503. 'procedure TObject.Fly(Prom: TJSPromise);',
  35504. 'begin',
  35505. 'end;',
  35506. 'function Crawl: jsvalue; async;',
  35507. 'begin',
  35508. 'end;',
  35509. 'procedure Add(Func: TFunc);',
  35510. 'begin',
  35511. 'end;',
  35512. 'begin',
  35513. ' Add(procedure(Bird: TObject)',
  35514. ' begin',
  35515. ' Bird.Fly(Crawl());',
  35516. ' end);',
  35517. '']);
  35518. ConvertProgram;
  35519. CheckSource('TestAsync_AnonymousProc_PromiseViaDotContext',
  35520. LinesToStr([ // statements
  35521. 'rtl.createClass(this, "TObject", null, function () {',
  35522. ' this.$init = function () {',
  35523. ' };',
  35524. ' this.$final = function () {',
  35525. ' };',
  35526. ' this.Fly = function (Prom) {',
  35527. ' };',
  35528. '});',
  35529. 'this.Crawl = async function () {',
  35530. ' var Result = undefined;',
  35531. ' return Result;',
  35532. '};',
  35533. 'this.Add = function (Func) {',
  35534. '};',
  35535. '']),
  35536. LinesToStr([
  35537. '$mod.Add(function (Bird) {',
  35538. ' Bird.Fly($mod.Crawl());',
  35539. '});',
  35540. '']));
  35541. end;
  35542. procedure TTestModule.TestAsync_ProcType;
  35543. begin
  35544. StartProgram(false);
  35545. Add([
  35546. '{$mode objfpc}',
  35547. 'type',
  35548. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  35549. ' TFunc = function(x: double = 1.1): word; async;',
  35550. ' TProc = procedure(x: longint = 7); async;',
  35551. 'function Crawl(d: double): word; async;',
  35552. 'begin',
  35553. 'end;',
  35554. 'procedure Run(e:longint); async;',
  35555. 'begin',
  35556. 'end;',
  35557. 'procedure Fly(p: TProc); async;',
  35558. 'begin',
  35559. ' await(p);',
  35560. ' await(p());',
  35561. 'end;',
  35562. 'var',
  35563. ' RefFunc: TRefFunc;',
  35564. ' Func: TFunc;',
  35565. ' Proc, ProcB: TProc;',
  35566. 'begin',
  35567. ' Func:=@Crawl;',
  35568. ' RefFunc:=@Crawl;',
  35569. ' RefFunc:=function(c:double):word async begin',
  35570. ' Result:=await(RefFunc);',
  35571. ' Result:=await(RefFunc());',
  35572. ' Result:=await(Func);',
  35573. ' Result:=await(Func());',
  35574. ' await(Proc);',
  35575. ' await(Proc());',
  35576. ' await(Proc(13));',
  35577. ' end;',
  35578. ' Proc:=@Run;',
  35579. ' if Proc=ProcB then ;',
  35580. ' ']);
  35581. ConvertProgram;
  35582. CheckResolverUnexpectedHints();
  35583. CheckSource('TestAsync_ProcType',
  35584. LinesToStr([ // statements
  35585. 'this.Crawl = async function (d) {',
  35586. ' var Result = 0;',
  35587. ' return Result;',
  35588. '};',
  35589. 'this.Run = async function (e) {',
  35590. '};',
  35591. 'this.Fly = async function (p) {',
  35592. ' await p(7);',
  35593. ' await p(7);',
  35594. '};',
  35595. 'this.RefFunc = null;',
  35596. 'this.Func = null;',
  35597. 'this.Proc = null;',
  35598. 'this.ProcB = null;',
  35599. '']),
  35600. LinesToStr([
  35601. '$mod.Func = $mod.Crawl;',
  35602. '$mod.RefFunc = $mod.Crawl;',
  35603. '$mod.RefFunc = async function (c) {',
  35604. ' var Result = 0;',
  35605. ' Result = await $mod.RefFunc(1.3);',
  35606. ' Result = await $mod.RefFunc(1.3);',
  35607. ' Result = await $mod.Func(1.1);',
  35608. ' Result = await $mod.Func(1.1);',
  35609. ' await $mod.Proc(7);',
  35610. ' await $mod.Proc(7);',
  35611. ' await $mod.Proc(13);',
  35612. ' return Result;',
  35613. '};',
  35614. '$mod.Proc = $mod.Run;',
  35615. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  35616. '']));
  35617. end;
  35618. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  35619. begin
  35620. StartProgram(false);
  35621. Add([
  35622. '{$mode objfpc}',
  35623. 'type',
  35624. ' TRefFunc = reference to function(x: double = 1.3): word;',
  35625. 'function Crawl(d: double): word; async;',
  35626. 'begin',
  35627. 'end;',
  35628. 'var',
  35629. ' RefFunc: TRefFunc;',
  35630. 'begin',
  35631. ' RefFunc:=@Crawl;',
  35632. ' ']);
  35633. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  35634. ConvertProgram;
  35635. end;
  35636. procedure TTestModule.TestAsync_Inherited;
  35637. begin
  35638. StartProgram(false);
  35639. Add([
  35640. '{$mode objfpc}',
  35641. '{$modeswitch externalclass}',
  35642. 'type',
  35643. ' TJSPromise = class external name ''Promise''',
  35644. ' end;',
  35645. ' TObject = class',
  35646. ' function Run(w: word = 3): word; async; virtual;',
  35647. ' end;',
  35648. ' TBird = class',
  35649. ' function Run(w: word = 3): word; async; override;',
  35650. ' end;',
  35651. 'function TObject.Run(w: word = 3): word; async;',
  35652. 'begin',
  35653. 'end;',
  35654. 'function TBird.Run(w: word = 3): word;', // async modifier not needed in impl
  35655. 'var p: TJSPromise;',
  35656. 'begin',
  35657. ' p:=inherited;',
  35658. ' p:=inherited Run;',
  35659. ' p:=inherited Run();',
  35660. ' p:=inherited Run(4);',
  35661. ' exit(p);',
  35662. ' exit(inherited);',
  35663. ' exit(inherited Run);',
  35664. ' exit(inherited Run(5));',
  35665. ' exit(6);',
  35666. 'end;',
  35667. 'begin',
  35668. ' ']);
  35669. ConvertProgram;
  35670. CheckSource('TestAsync_Inherited',
  35671. LinesToStr([ // statements
  35672. 'rtl.createClass(this, "TObject", null, function () {',
  35673. ' this.$init = function () {',
  35674. ' };',
  35675. ' this.$final = function () {',
  35676. ' };',
  35677. ' this.Run = async function (w) {',
  35678. ' var Result = 0;',
  35679. ' return Result;',
  35680. ' };',
  35681. '});',
  35682. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  35683. ' this.Run = async function (w) {',
  35684. ' var Result = 0;',
  35685. ' var p = null;',
  35686. ' p = $mod.TObject.Run.apply(this, arguments);',
  35687. ' p = $mod.TObject.Run.call(this, 3);',
  35688. ' p = $mod.TObject.Run.call(this, 3);',
  35689. ' p = $mod.TObject.Run.call(this, 4);',
  35690. ' return p;',
  35691. ' return $mod.TObject.Run.apply(this, arguments);',
  35692. ' return $mod.TObject.Run.call(this, 3);',
  35693. ' return $mod.TObject.Run.call(this, 5);',
  35694. ' return 6;',
  35695. ' return Result;',
  35696. ' };',
  35697. '});',
  35698. '']),
  35699. LinesToStr([
  35700. '']));
  35701. CheckResolverUnexpectedHints();
  35702. end;
  35703. procedure TTestModule.TestAsync_ClassInterface;
  35704. begin
  35705. StartProgram(false);
  35706. Add([
  35707. '{$mode objfpc}',
  35708. '{$modeswitch externalclass}',
  35709. 'type',
  35710. ' TJSPromise = class external name ''Promise''',
  35711. ' end;',
  35712. ' IUnknown = interface',
  35713. ' function _AddRef: longint;',
  35714. ' function _Release: longint;',
  35715. ' end;',
  35716. 'function Say(i: IUnknown): IUnknown; async;',
  35717. 'begin',
  35718. 'end;',
  35719. 'function Run: IUnknown; async;',
  35720. 'begin',
  35721. ' Result:=await(Run);',
  35722. ' Result:=await(Run());',
  35723. ' Result:=await(Run) as IUnknown;',
  35724. ' Result:=await(Say(nil));',
  35725. ' Result:=await(Say(await(Run())));',
  35726. ' Result:=await(Say(await(Run()) as IUnknown));',
  35727. ' Result:=await(Say(await(Run()) as IUnknown)) as IUnknown;',
  35728. 'end;',
  35729. 'procedure Fly;',
  35730. 'var p: TJSPromise;',
  35731. 'begin',
  35732. ' Run;',
  35733. ' Run();',
  35734. ' p:=Run;',
  35735. ' p:=Run();',
  35736. 'end;',
  35737. 'begin',
  35738. ' ']);
  35739. ConvertProgram;
  35740. CheckSource('TestAsync_ClassInterface',
  35741. LinesToStr([ // statements
  35742. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  35743. 'this.Say = async function (i) {',
  35744. ' var Result = null;',
  35745. ' return Result;',
  35746. '};',
  35747. 'this.Run = async function () {',
  35748. ' var Result = null;',
  35749. ' var $ok = false;',
  35750. ' try {',
  35751. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  35752. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  35753. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown));',
  35754. ' Result = rtl.setIntfL(Result, await $mod.Say(null));',
  35755. ' Result = rtl.setIntfL(Result, await $mod.Say(await $mod.Run()));',
  35756. ' Result = rtl.setIntfL(Result, await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)));',
  35757. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)), $mod.IUnknown));',
  35758. ' $ok = true;',
  35759. ' } finally {',
  35760. ' if (!$ok) rtl._Release(Result);',
  35761. ' };',
  35762. ' return Result;',
  35763. '};',
  35764. 'this.Fly = function () {',
  35765. ' var p = null;',
  35766. ' $mod.Run();',
  35767. ' $mod.Run();',
  35768. ' p = $mod.Run();',
  35769. ' p = $mod.Run();',
  35770. '};',
  35771. '']),
  35772. LinesToStr([
  35773. '']));
  35774. CheckResolverUnexpectedHints();
  35775. end;
  35776. procedure TTestModule.TestAsync_ClassInterface_AsyncMissmatchFail;
  35777. begin
  35778. StartProgram(true,[supTInterfacedObject]);
  35779. Add([
  35780. '{$mode objfpc}',
  35781. '{$modeswitch externalclass}',
  35782. 'type',
  35783. ' TJSPromise = class external name ''Promise''',
  35784. ' end;',
  35785. ' IBird = interface',
  35786. ' procedure Run;',
  35787. ' end;',
  35788. ' TBird = class(TInterfacedObject,IBird)',
  35789. ' procedure Run; async;',
  35790. ' end;',
  35791. 'procedure TBird.Run;',
  35792. 'begin',
  35793. 'end;',
  35794. 'begin',
  35795. ' ']);
  35796. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  35797. ConvertProgram;
  35798. end;
  35799. procedure TTestModule.TestAWait_ClassAs;
  35800. begin
  35801. StartProgram(false);
  35802. Add([
  35803. '{$mode objfpc}',
  35804. '{$modeswitch externalclass}',
  35805. 'type',
  35806. ' TJSPromise = class external name ''Promise''',
  35807. ' end;',
  35808. ' TObject = class',
  35809. ' function Run: TObject; async;',
  35810. ' end;',
  35811. ' TBird = class',
  35812. ' function Fly: TBird; async;',
  35813. ' end;',
  35814. 'function TObject.Run: TObject; async;',
  35815. 'begin',
  35816. 'end;',
  35817. 'function TBird.Fly: TBird;', // async modifier not needed in impl
  35818. 'var o: TObject;',
  35819. 'begin',
  35820. ' o:=await(TObject,Run);',
  35821. ' o:=await(TObject,Fly);',
  35822. ' o:=await(TBird,Fly);',
  35823. ' o:=await(TObject,inherited Run);',
  35824. ' o:=await(TObject,inherited Run) as TBird;',
  35825. 'end;',
  35826. 'begin',
  35827. ' ']);
  35828. ConvertProgram;
  35829. CheckSource('TestAWait_ClassAs',
  35830. LinesToStr([ // statements
  35831. 'rtl.createClass(this, "TObject", null, function () {',
  35832. ' this.$init = function () {',
  35833. ' };',
  35834. ' this.$final = function () {',
  35835. ' };',
  35836. ' this.Run = async function () {',
  35837. ' var Result = null;',
  35838. ' return Result;',
  35839. ' };',
  35840. '});',
  35841. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  35842. ' this.Fly = async function () {',
  35843. ' var Result = null;',
  35844. ' var o = null;',
  35845. ' o = await this.Run();',
  35846. ' o = await this.Fly();',
  35847. ' o = await this.Fly();',
  35848. ' o = await $mod.TObject.Run.call(this);',
  35849. ' o = rtl.as(await $mod.TObject.Run.call(this), $mod.TBird);',
  35850. ' return Result;',
  35851. ' };',
  35852. '});',
  35853. '']),
  35854. LinesToStr([
  35855. '']));
  35856. CheckResolverUnexpectedHints();
  35857. end;
  35858. procedure TTestModule.TestLibrary_Empty;
  35859. begin
  35860. StartLibrary(false);
  35861. Add([
  35862. '']);
  35863. ConvertLibrary;
  35864. CheckFullSource('TestLibrary_Empty',
  35865. LinesToStr([ // statements
  35866. 'rtl.module("library", [], function () {',
  35867. ' var $mod = this;',
  35868. ' $mod.$main = function () {',
  35869. ' };',
  35870. '});',
  35871. 'rtl.run("library");',
  35872. '']));
  35873. CheckResolverUnexpectedHints();
  35874. end;
  35875. procedure TTestModule.TestLibrary_ExportFunc;
  35876. begin
  35877. StartLibrary(false);
  35878. Add([
  35879. 'procedure Run(w: word);',
  35880. 'begin',
  35881. 'end;',
  35882. 'exports',
  35883. ' Run;',
  35884. ' run name ''Foo'';',
  35885. ' test1.run name ''Test1Run'';',
  35886. '']);
  35887. ConvertLibrary;
  35888. CheckFullSource('TestLibrary_ExportFunc',
  35889. LinesToStr([ // statements
  35890. 'rtl.module("library", [], function () {',
  35891. ' var $mod = this;',
  35892. ' this.Run = function (w) {',
  35893. ' };',
  35894. ' $mod.$main = function () {',
  35895. ' };',
  35896. '});',
  35897. 'rtl.run("library");',
  35898. 'export const Run = pas.library.Run;',
  35899. 'export const Foo = pas.library.Run;',
  35900. 'export const Test1Run = pas.library.Run;',
  35901. '']));
  35902. CheckResolverUnexpectedHints();
  35903. end;
  35904. procedure TTestModule.TestLibrary_ExportFuncOverloadedFail;
  35905. begin
  35906. StartLibrary(false);
  35907. Add([
  35908. 'procedure Run(w: word); overload;',
  35909. 'begin',
  35910. 'end;',
  35911. 'procedure Run(s: string); overload;',
  35912. 'begin',
  35913. 'end;',
  35914. 'exports',
  35915. ' Run;',
  35916. '']);
  35917. SetExpectedPasResolverError(sCantDetermineWhichOverloadedFunctionToCall,
  35918. nCantDetermineWhichOverloadedFunctionToCall);
  35919. ConvertLibrary;
  35920. end;
  35921. procedure TTestModule.TestLibrary_Export_Index_Fail;
  35922. begin
  35923. StartLibrary(false);
  35924. Add([
  35925. 'procedure Run(w: word);',
  35926. 'begin',
  35927. 'end;',
  35928. 'exports',
  35929. ' Run index 3;',
  35930. '']);
  35931. SetExpectedPasResolverError('Not supported: export index',nNotSupportedX);
  35932. ConvertLibrary;
  35933. end;
  35934. procedure TTestModule.TestLibrary_ExportVar;
  35935. begin
  35936. StartLibrary(false);
  35937. Add([
  35938. 'var Wing: word;',
  35939. 'exports',
  35940. ' Wing, wing name ''BirdArm'';',
  35941. '']);
  35942. ConvertLibrary;
  35943. CheckFullSource('TestLibrary_ExportVar',
  35944. LinesToStr([ // statements
  35945. 'rtl.module("library", [], function () {',
  35946. ' var $mod = this;',
  35947. ' this.Wing = 0;',
  35948. ' $mod.$main = function () {',
  35949. ' };',
  35950. '});',
  35951. 'rtl.run("library");',
  35952. 'export const vars = {};',
  35953. 'Object.defineProperties(vars, {',
  35954. ' Wing: {',
  35955. ' enumerable: true,',
  35956. ' get: function () {',
  35957. ' return pas.library.Wing;',
  35958. ' },',
  35959. ' set: function (v) {',
  35960. ' pas.library.Wing = v;',
  35961. ' }',
  35962. ' },',
  35963. ' BirdArm: {',
  35964. ' enumerable: true,',
  35965. ' get: function () {',
  35966. ' return pas.library.Wing;',
  35967. ' },',
  35968. ' set: function (v) {',
  35969. ' pas.library.Wing = v;',
  35970. ' }',
  35971. ' }',
  35972. '});',
  35973. '']));
  35974. CheckResolverUnexpectedHints();
  35975. end;
  35976. procedure TTestModule.TestLibrary_ExportUnitFunc;
  35977. begin
  35978. AddModuleWithIntfImplSrc('Unit1.pas',
  35979. LinesToStr([
  35980. 'type',
  35981. ' TAnt = class',
  35982. ' class function Crawl: word; static;',
  35983. ' end;',
  35984. 'function Fly: word;',
  35985. '']),
  35986. LinesToStr([
  35987. 'function Fly: word;',
  35988. 'begin',
  35989. 'end;',
  35990. 'class function TAnt.Crawl: word;',
  35991. 'begin',
  35992. 'end;',
  35993. '']));
  35994. StartLibrary(true,[supTObject]);
  35995. Add([
  35996. 'uses unit1;',
  35997. 'exports',
  35998. ' Fly;',
  35999. ' TAnt.Crawl;',
  36000. '']);
  36001. ConvertLibrary;
  36002. CheckFullSource('TestLibrary_ExportUnitFunc',
  36003. LinesToStr([ // statements
  36004. 'rtl.module("library", ["system", "Unit1"], function () {',
  36005. ' var $mod = this;',
  36006. ' $mod.$main = function () {',
  36007. ' };',
  36008. '});',
  36009. 'rtl.run("library");',
  36010. 'export const Fly = pas.Unit1.Fly;',
  36011. 'export const Crawl = pas.Unit1.TAnt.Crawl;',
  36012. '']));
  36013. CheckResolverUnexpectedHints();
  36014. end;
  36015. Initialization
  36016. RegisterTests([TTestModule]);
  36017. end.