php_reflection.c 193 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Timm Friebe <thekid@thekid.de> |
  14. | George Schlossnagle <george@omniti.com> |
  15. | Andrei Zmievski <andrei@gravitonic.com> |
  16. | Marcus Boerger <helly@php.net> |
  17. | Johannes Schlueter <johannes@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "php.h"
  24. #include "php_ini.h"
  25. #include "php_reflection.h"
  26. #include "ext/standard/info.h"
  27. #include "ext/standard/sha1.h"
  28. #include "ext/standard/php_random.h"
  29. #include "zend.h"
  30. #include "zend_API.h"
  31. #include "zend_ast.h"
  32. #include "zend_attributes.h"
  33. #include "zend_exceptions.h"
  34. #include "zend_operators.h"
  35. #include "zend_constants.h"
  36. #include "zend_ini.h"
  37. #include "zend_interfaces.h"
  38. #include "zend_closures.h"
  39. #include "zend_generators.h"
  40. #include "zend_extensions.h"
  41. #include "zend_builtin_functions.h"
  42. #include "zend_smart_str.h"
  43. #include "zend_enum.h"
  44. #include "zend_fibers.h"
  45. #include "php_reflection_arginfo.h"
  46. /* Key used to avoid leaking addresses in ReflectionProperty::getId() */
  47. #define REFLECTION_KEY_LEN 16
  48. ZEND_BEGIN_MODULE_GLOBALS(reflection)
  49. bool key_initialized;
  50. unsigned char key[REFLECTION_KEY_LEN];
  51. ZEND_END_MODULE_GLOBALS(reflection)
  52. ZEND_DECLARE_MODULE_GLOBALS(reflection)
  53. #define REFLECTION_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(reflection, v)
  54. static zend_always_inline zval *reflection_prop_name(zval *object) {
  55. /* $name is always in the first property slot. */
  56. ZEND_ASSERT(Z_OBJCE_P(object)->default_properties_count >= 1);
  57. return &Z_OBJ_P(object)->properties_table[0];
  58. }
  59. static zend_always_inline zval *reflection_prop_class(zval *object) {
  60. /* $class is always in the second property slot. */
  61. ZEND_ASSERT(Z_OBJCE_P(object)->default_properties_count >= 2);
  62. return &Z_OBJ_P(object)->properties_table[1];
  63. }
  64. /* Class entry pointers */
  65. PHPAPI zend_class_entry *reflector_ptr;
  66. PHPAPI zend_class_entry *reflection_exception_ptr;
  67. PHPAPI zend_class_entry *reflection_ptr;
  68. PHPAPI zend_class_entry *reflection_function_abstract_ptr;
  69. PHPAPI zend_class_entry *reflection_function_ptr;
  70. PHPAPI zend_class_entry *reflection_generator_ptr;
  71. PHPAPI zend_class_entry *reflection_parameter_ptr;
  72. PHPAPI zend_class_entry *reflection_type_ptr;
  73. PHPAPI zend_class_entry *reflection_named_type_ptr;
  74. PHPAPI zend_class_entry *reflection_intersection_type_ptr;
  75. PHPAPI zend_class_entry *reflection_union_type_ptr;
  76. PHPAPI zend_class_entry *reflection_class_ptr;
  77. PHPAPI zend_class_entry *reflection_object_ptr;
  78. PHPAPI zend_class_entry *reflection_method_ptr;
  79. PHPAPI zend_class_entry *reflection_property_ptr;
  80. PHPAPI zend_class_entry *reflection_class_constant_ptr;
  81. PHPAPI zend_class_entry *reflection_extension_ptr;
  82. PHPAPI zend_class_entry *reflection_zend_extension_ptr;
  83. PHPAPI zend_class_entry *reflection_reference_ptr;
  84. PHPAPI zend_class_entry *reflection_attribute_ptr;
  85. PHPAPI zend_class_entry *reflection_enum_ptr;
  86. PHPAPI zend_class_entry *reflection_enum_unit_case_ptr;
  87. PHPAPI zend_class_entry *reflection_enum_backed_case_ptr;
  88. PHPAPI zend_class_entry *reflection_fiber_ptr;
  89. /* Exception throwing macro */
  90. #define _DO_THROW(msg) \
  91. zend_throw_exception(reflection_exception_ptr, msg, 0);
  92. #define GET_REFLECTION_OBJECT() do { \
  93. intern = Z_REFLECTION_P(ZEND_THIS); \
  94. if (intern->ptr == NULL) { \
  95. if (EG(exception) && EG(exception)->ce == reflection_exception_ptr) { \
  96. RETURN_THROWS(); \
  97. } \
  98. zend_throw_error(NULL, "Internal error: Failed to retrieve the reflection object"); \
  99. RETURN_THROWS(); \
  100. } \
  101. } while (0)
  102. #define GET_REFLECTION_OBJECT_PTR(target) do { \
  103. GET_REFLECTION_OBJECT(); \
  104. target = intern->ptr; \
  105. } while (0)
  106. /* Class constants */
  107. #define REGISTER_REFLECTION_CLASS_CONST_LONG(class_name, const_name, value) \
  108. zend_declare_class_constant_long(reflection_ ## class_name ## _ptr, const_name, sizeof(const_name)-1, (zend_long)value);
  109. #define REFLECTION_ATTRIBUTE_IS_INSTANCEOF (1 << 1)
  110. /* {{{ Object structure */
  111. /* Struct for properties */
  112. typedef struct _property_reference {
  113. zend_property_info *prop;
  114. zend_string *unmangled_name;
  115. } property_reference;
  116. /* Struct for parameters */
  117. typedef struct _parameter_reference {
  118. uint32_t offset;
  119. bool required;
  120. struct _zend_arg_info *arg_info;
  121. zend_function *fptr;
  122. } parameter_reference;
  123. /* Struct for type hints */
  124. typedef struct _type_reference {
  125. zend_type type;
  126. /* Whether to use backwards compatible null representation */
  127. bool legacy_behavior;
  128. } type_reference;
  129. /* Struct for attributes */
  130. typedef struct _attribute_reference {
  131. HashTable *attributes;
  132. zend_attribute *data;
  133. zend_class_entry *scope;
  134. zend_string *filename;
  135. uint32_t target;
  136. } attribute_reference;
  137. typedef enum {
  138. REF_TYPE_OTHER, /* Must be 0 */
  139. REF_TYPE_FUNCTION,
  140. REF_TYPE_GENERATOR,
  141. REF_TYPE_FIBER,
  142. REF_TYPE_PARAMETER,
  143. REF_TYPE_TYPE,
  144. REF_TYPE_PROPERTY,
  145. REF_TYPE_CLASS_CONSTANT,
  146. REF_TYPE_ATTRIBUTE
  147. } reflection_type_t;
  148. /* Struct for reflection objects */
  149. typedef struct {
  150. zval obj;
  151. void *ptr;
  152. zend_class_entry *ce;
  153. reflection_type_t ref_type;
  154. zend_object zo;
  155. } reflection_object;
  156. static inline reflection_object *reflection_object_from_obj(zend_object *obj) {
  157. return (reflection_object*)((char*)(obj) - XtOffsetOf(reflection_object, zo));
  158. }
  159. #define Z_REFLECTION_P(zv) reflection_object_from_obj(Z_OBJ_P((zv)))
  160. /* }}} */
  161. static zend_object_handlers reflection_object_handlers;
  162. static zend_always_inline uint32_t prop_get_flags(property_reference *ref) {
  163. return ref->prop ? ref->prop->flags : ZEND_ACC_PUBLIC;
  164. }
  165. static inline bool is_closure_invoke(zend_class_entry *ce, zend_string *lcname) {
  166. return ce == zend_ce_closure
  167. && zend_string_equals_literal(lcname, ZEND_INVOKE_FUNC_NAME);
  168. }
  169. static zend_function *_copy_function(zend_function *fptr) /* {{{ */
  170. {
  171. if (fptr
  172. && (fptr->internal_function.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE))
  173. {
  174. zend_function *copy_fptr;
  175. copy_fptr = emalloc(sizeof(zend_function));
  176. memcpy(copy_fptr, fptr, sizeof(zend_function));
  177. copy_fptr->internal_function.function_name = zend_string_copy(fptr->internal_function.function_name);
  178. return copy_fptr;
  179. } else {
  180. /* no copy needed */
  181. return fptr;
  182. }
  183. }
  184. /* }}} */
  185. static void _free_function(zend_function *fptr) /* {{{ */
  186. {
  187. if (fptr
  188. && (fptr->internal_function.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE))
  189. {
  190. zend_string_release_ex(fptr->internal_function.function_name, 0);
  191. zend_free_trampoline(fptr);
  192. }
  193. }
  194. /* }}} */
  195. static void reflection_free_objects_storage(zend_object *object) /* {{{ */
  196. {
  197. reflection_object *intern = reflection_object_from_obj(object);
  198. parameter_reference *reference;
  199. property_reference *prop_reference;
  200. if (intern->ptr) {
  201. switch (intern->ref_type) {
  202. case REF_TYPE_PARAMETER:
  203. reference = (parameter_reference*)intern->ptr;
  204. _free_function(reference->fptr);
  205. efree(intern->ptr);
  206. break;
  207. case REF_TYPE_TYPE:
  208. {
  209. type_reference *type_ref = intern->ptr;
  210. if (ZEND_TYPE_HAS_NAME(type_ref->type)) {
  211. zend_string_release(ZEND_TYPE_NAME(type_ref->type));
  212. }
  213. efree(type_ref);
  214. break;
  215. }
  216. case REF_TYPE_FUNCTION:
  217. _free_function(intern->ptr);
  218. break;
  219. case REF_TYPE_PROPERTY:
  220. prop_reference = (property_reference*)intern->ptr;
  221. zend_string_release_ex(prop_reference->unmangled_name, 0);
  222. efree(intern->ptr);
  223. break;
  224. case REF_TYPE_ATTRIBUTE: {
  225. attribute_reference *attr_ref = intern->ptr;
  226. if (attr_ref->filename) {
  227. zend_string_release(attr_ref->filename);
  228. }
  229. efree(intern->ptr);
  230. break;
  231. }
  232. case REF_TYPE_GENERATOR:
  233. case REF_TYPE_FIBER:
  234. case REF_TYPE_CLASS_CONSTANT:
  235. case REF_TYPE_OTHER:
  236. break;
  237. }
  238. }
  239. intern->ptr = NULL;
  240. zval_ptr_dtor(&intern->obj);
  241. zend_object_std_dtor(object);
  242. }
  243. /* }}} */
  244. static HashTable *reflection_get_gc(zend_object *obj, zval **gc_data, int *gc_data_count) /* {{{ */
  245. {
  246. reflection_object *intern = reflection_object_from_obj(obj);
  247. *gc_data = &intern->obj;
  248. *gc_data_count = 1;
  249. return zend_std_get_properties(obj);
  250. }
  251. /* }}} */
  252. static zend_object *reflection_objects_new(zend_class_entry *class_type) /* {{{ */
  253. {
  254. reflection_object *intern = zend_object_alloc(sizeof(reflection_object), class_type);
  255. zend_object_std_init(&intern->zo, class_type);
  256. object_properties_init(&intern->zo, class_type);
  257. intern->zo.handlers = &reflection_object_handlers;
  258. return &intern->zo;
  259. }
  260. /* }}} */
  261. static zval *reflection_instantiate(zend_class_entry *pce, zval *object) /* {{{ */
  262. {
  263. object_init_ex(object, pce);
  264. return object;
  265. }
  266. /* }}} */
  267. static void _const_string(smart_str *str, char *name, zval *value, char *indent);
  268. static void _function_string(smart_str *str, zend_function *fptr, zend_class_entry *scope, char* indent);
  269. static void _property_string(smart_str *str, zend_property_info *prop, const char *prop_name, char* indent);
  270. static void _class_const_string(smart_str *str, char *name, zend_class_constant *c, char* indent);
  271. static void _class_string(smart_str *str, zend_class_entry *ce, zval *obj, char *indent);
  272. static void _extension_string(smart_str *str, zend_module_entry *module, char *indent);
  273. static void _zend_extension_string(smart_str *str, zend_extension *extension, char *indent);
  274. /* {{{ _class_string */
  275. static void _class_string(smart_str *str, zend_class_entry *ce, zval *obj, char *indent)
  276. {
  277. int count, count_static_props = 0, count_static_funcs = 0, count_shadow_props = 0;
  278. zend_string *sub_indent = strpprintf(0, "%s ", indent);
  279. /* TBD: Repair indenting of doc comment (or is this to be done in the parser?) */
  280. if (ce->type == ZEND_USER_CLASS && ce->info.user.doc_comment) {
  281. smart_str_append_printf(str, "%s%s", indent, ZSTR_VAL(ce->info.user.doc_comment));
  282. smart_str_appendc(str, '\n');
  283. }
  284. if (obj && Z_TYPE_P(obj) == IS_OBJECT) {
  285. smart_str_append_printf(str, "%sObject of class [ ", indent);
  286. } else {
  287. char *kind = "Class";
  288. if (ce->ce_flags & ZEND_ACC_INTERFACE) {
  289. kind = "Interface";
  290. } else if (ce->ce_flags & ZEND_ACC_TRAIT) {
  291. kind = "Trait";
  292. }
  293. smart_str_append_printf(str, "%s%s [ ", indent, kind);
  294. }
  295. smart_str_append_printf(str, (ce->type == ZEND_USER_CLASS) ? "<user" : "<internal");
  296. if (ce->type == ZEND_INTERNAL_CLASS && ce->info.internal.module) {
  297. smart_str_append_printf(str, ":%s", ce->info.internal.module->name);
  298. }
  299. smart_str_append_printf(str, "> ");
  300. if (ce->get_iterator != NULL) {
  301. smart_str_append_printf(str, "<iterateable> ");
  302. }
  303. if (ce->ce_flags & ZEND_ACC_INTERFACE) {
  304. smart_str_append_printf(str, "interface ");
  305. } else if (ce->ce_flags & ZEND_ACC_TRAIT) {
  306. smart_str_append_printf(str, "trait ");
  307. } else {
  308. if (ce->ce_flags & (ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
  309. smart_str_append_printf(str, "abstract ");
  310. }
  311. if (ce->ce_flags & ZEND_ACC_FINAL) {
  312. smart_str_append_printf(str, "final ");
  313. }
  314. smart_str_append_printf(str, "class ");
  315. }
  316. smart_str_append_printf(str, "%s", ZSTR_VAL(ce->name));
  317. if (ce->parent) {
  318. smart_str_append_printf(str, " extends %s", ZSTR_VAL(ce->parent->name));
  319. }
  320. if (ce->num_interfaces) {
  321. uint32_t i;
  322. ZEND_ASSERT(ce->ce_flags & ZEND_ACC_LINKED);
  323. if (ce->ce_flags & ZEND_ACC_INTERFACE) {
  324. smart_str_append_printf(str, " extends %s", ZSTR_VAL(ce->interfaces[0]->name));
  325. } else {
  326. smart_str_append_printf(str, " implements %s", ZSTR_VAL(ce->interfaces[0]->name));
  327. }
  328. for (i = 1; i < ce->num_interfaces; ++i) {
  329. smart_str_append_printf(str, ", %s", ZSTR_VAL(ce->interfaces[i]->name));
  330. }
  331. }
  332. smart_str_append_printf(str, " ] {\n");
  333. /* The information where a class is declared is only available for user classes */
  334. if (ce->type == ZEND_USER_CLASS) {
  335. smart_str_append_printf(str, "%s @@ %s %d-%d\n", indent, ZSTR_VAL(ce->info.user.filename),
  336. ce->info.user.line_start, ce->info.user.line_end);
  337. }
  338. /* Constants */
  339. smart_str_append_printf(str, "\n");
  340. count = zend_hash_num_elements(&ce->constants_table);
  341. smart_str_append_printf(str, "%s - Constants [%d] {\n", indent, count);
  342. if (count > 0) {
  343. zend_string *key;
  344. zend_class_constant *c;
  345. ZEND_HASH_FOREACH_STR_KEY_PTR(CE_CONSTANTS_TABLE(ce), key, c) {
  346. _class_const_string(str, ZSTR_VAL(key), c, ZSTR_VAL(sub_indent));
  347. if (UNEXPECTED(EG(exception))) {
  348. zend_string_release(sub_indent);
  349. return;
  350. }
  351. } ZEND_HASH_FOREACH_END();
  352. }
  353. smart_str_append_printf(str, "%s }\n", indent);
  354. /* Static properties */
  355. /* counting static properties */
  356. count = zend_hash_num_elements(&ce->properties_info);
  357. if (count > 0) {
  358. zend_property_info *prop;
  359. ZEND_HASH_FOREACH_PTR(&ce->properties_info, prop) {
  360. if ((prop->flags & ZEND_ACC_PRIVATE) && prop->ce != ce) {
  361. count_shadow_props++;
  362. } else if (prop->flags & ZEND_ACC_STATIC) {
  363. count_static_props++;
  364. }
  365. } ZEND_HASH_FOREACH_END();
  366. }
  367. /* static properties */
  368. smart_str_append_printf(str, "\n%s - Static properties [%d] {\n", indent, count_static_props);
  369. if (count_static_props > 0) {
  370. zend_property_info *prop;
  371. ZEND_HASH_FOREACH_PTR(&ce->properties_info, prop) {
  372. if ((prop->flags & ZEND_ACC_STATIC) && (!(prop->flags & ZEND_ACC_PRIVATE) || prop->ce == ce)) {
  373. _property_string(str, prop, NULL, ZSTR_VAL(sub_indent));
  374. }
  375. } ZEND_HASH_FOREACH_END();
  376. }
  377. smart_str_append_printf(str, "%s }\n", indent);
  378. /* Static methods */
  379. /* counting static methods */
  380. count = zend_hash_num_elements(&ce->function_table);
  381. if (count > 0) {
  382. zend_function *mptr;
  383. ZEND_HASH_FOREACH_PTR(&ce->function_table, mptr) {
  384. if ((mptr->common.fn_flags & ZEND_ACC_STATIC)
  385. && ((mptr->common.fn_flags & ZEND_ACC_PRIVATE) == 0 || mptr->common.scope == ce))
  386. {
  387. count_static_funcs++;
  388. }
  389. } ZEND_HASH_FOREACH_END();
  390. }
  391. /* static methods */
  392. smart_str_append_printf(str, "\n%s - Static methods [%d] {", indent, count_static_funcs);
  393. if (count_static_funcs > 0) {
  394. zend_function *mptr;
  395. ZEND_HASH_FOREACH_PTR(&ce->function_table, mptr) {
  396. if ((mptr->common.fn_flags & ZEND_ACC_STATIC)
  397. && ((mptr->common.fn_flags & ZEND_ACC_PRIVATE) == 0 || mptr->common.scope == ce))
  398. {
  399. smart_str_append_printf(str, "\n");
  400. _function_string(str, mptr, ce, ZSTR_VAL(sub_indent));
  401. }
  402. } ZEND_HASH_FOREACH_END();
  403. } else {
  404. smart_str_append_printf(str, "\n");
  405. }
  406. smart_str_append_printf(str, "%s }\n", indent);
  407. /* Default/Implicit properties */
  408. count = zend_hash_num_elements(&ce->properties_info) - count_static_props - count_shadow_props;
  409. smart_str_append_printf(str, "\n%s - Properties [%d] {\n", indent, count);
  410. if (count > 0) {
  411. zend_property_info *prop;
  412. ZEND_HASH_FOREACH_PTR(&ce->properties_info, prop) {
  413. if (!(prop->flags & ZEND_ACC_STATIC)
  414. && (!(prop->flags & ZEND_ACC_PRIVATE) || prop->ce == ce)) {
  415. _property_string(str, prop, NULL, ZSTR_VAL(sub_indent));
  416. }
  417. } ZEND_HASH_FOREACH_END();
  418. }
  419. smart_str_append_printf(str, "%s }\n", indent);
  420. if (obj && Z_TYPE_P(obj) == IS_OBJECT) {
  421. HashTable *properties = Z_OBJ_HT_P(obj)->get_properties(Z_OBJ_P(obj));
  422. zend_string *prop_name;
  423. smart_str prop_str = {0};
  424. count = 0;
  425. if (properties && zend_hash_num_elements(properties)) {
  426. ZEND_HASH_FOREACH_STR_KEY(properties, prop_name) {
  427. if (prop_name && ZSTR_LEN(prop_name) && ZSTR_VAL(prop_name)[0]) { /* skip all private and protected properties */
  428. if (!zend_hash_exists(&ce->properties_info, prop_name)) {
  429. count++;
  430. _property_string(&prop_str, NULL, ZSTR_VAL(prop_name), ZSTR_VAL(sub_indent));
  431. }
  432. }
  433. } ZEND_HASH_FOREACH_END();
  434. }
  435. smart_str_append_printf(str, "\n%s - Dynamic properties [%d] {\n", indent, count);
  436. smart_str_append_smart_str(str, &prop_str);
  437. smart_str_append_printf(str, "%s }\n", indent);
  438. smart_str_free(&prop_str);
  439. }
  440. /* Non static methods */
  441. count = zend_hash_num_elements(&ce->function_table) - count_static_funcs;
  442. if (count > 0) {
  443. zend_function *mptr;
  444. smart_str method_str = {0};
  445. count = 0;
  446. ZEND_HASH_FOREACH_PTR(&ce->function_table, mptr) {
  447. if ((mptr->common.fn_flags & ZEND_ACC_STATIC) == 0
  448. && ((mptr->common.fn_flags & ZEND_ACC_PRIVATE) == 0 || mptr->common.scope == ce))
  449. {
  450. zend_function *closure;
  451. /* see if this is a closure */
  452. if (obj && is_closure_invoke(ce, mptr->common.function_name)
  453. && (closure = zend_get_closure_invoke_method(Z_OBJ_P(obj))) != NULL)
  454. {
  455. mptr = closure;
  456. } else {
  457. closure = NULL;
  458. }
  459. smart_str_appendc(&method_str, '\n');
  460. _function_string(&method_str, mptr, ce, ZSTR_VAL(sub_indent));
  461. count++;
  462. _free_function(closure);
  463. }
  464. } ZEND_HASH_FOREACH_END();
  465. smart_str_append_printf(str, "\n%s - Methods [%d] {", indent, count);
  466. smart_str_append_smart_str(str, &method_str);
  467. if (!count) {
  468. smart_str_append_printf(str, "\n");
  469. }
  470. smart_str_free(&method_str);
  471. } else {
  472. smart_str_append_printf(str, "\n%s - Methods [0] {\n", indent);
  473. }
  474. smart_str_append_printf(str, "%s }\n", indent);
  475. smart_str_append_printf(str, "%s}\n", indent);
  476. zend_string_release_ex(sub_indent, 0);
  477. }
  478. /* }}} */
  479. /* {{{ _const_string */
  480. static void _const_string(smart_str *str, char *name, zval *value, char *indent)
  481. {
  482. const char *type = zend_zval_type_name(value);
  483. if (Z_TYPE_P(value) == IS_ARRAY) {
  484. smart_str_append_printf(str, "%s Constant [ %s %s ] { Array }\n",
  485. indent, type, name);
  486. } else if (Z_TYPE_P(value) == IS_STRING) {
  487. smart_str_append_printf(str, "%s Constant [ %s %s ] { %s }\n",
  488. indent, type, name, Z_STRVAL_P(value));
  489. } else {
  490. zend_string *tmp_value_str;
  491. zend_string *value_str = zval_get_tmp_string(value, &tmp_value_str);
  492. smart_str_append_printf(str, "%s Constant [ %s %s ] { %s }\n",
  493. indent, type, name, ZSTR_VAL(value_str));
  494. zend_tmp_string_release(tmp_value_str);
  495. }
  496. }
  497. /* }}} */
  498. /* {{{ _class_const_string */
  499. static void _class_const_string(smart_str *str, char *name, zend_class_constant *c, char *indent)
  500. {
  501. if (zval_update_constant_ex(&c->value, c->ce) == FAILURE) {
  502. return;
  503. }
  504. const char *visibility = zend_visibility_string(ZEND_CLASS_CONST_FLAGS(c));
  505. const char *final = ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_FINAL ? "final " : "";
  506. const char *type = zend_zval_type_name(&c->value);
  507. smart_str_append_printf(str, "%sConstant [ %s%s %s %s ] { ",
  508. indent, final, visibility, type, name);
  509. if (Z_TYPE(c->value) == IS_ARRAY) {
  510. smart_str_appends(str, "Array");
  511. } else if (Z_TYPE(c->value) == IS_OBJECT) {
  512. smart_str_appends(str, "Object");
  513. } else {
  514. zend_string *tmp_value_str;
  515. zend_string *value_str = zval_get_tmp_string(&c->value, &tmp_value_str);
  516. smart_str_append(str, value_str);
  517. zend_tmp_string_release(tmp_value_str);
  518. }
  519. smart_str_appends(str, " }\n");
  520. }
  521. /* }}} */
  522. static zend_op *get_recv_op(zend_op_array *op_array, uint32_t offset)
  523. {
  524. zend_op *op = op_array->opcodes;
  525. zend_op *end = op + op_array->last;
  526. ++offset;
  527. while (op < end) {
  528. if ((op->opcode == ZEND_RECV || op->opcode == ZEND_RECV_INIT
  529. || op->opcode == ZEND_RECV_VARIADIC) && op->op1.num == offset)
  530. {
  531. return op;
  532. }
  533. ++op;
  534. }
  535. ZEND_ASSERT(0 && "Failed to find op");
  536. return NULL;
  537. }
  538. static zval *get_default_from_recv(zend_op_array *op_array, uint32_t offset) {
  539. zend_op *recv = get_recv_op(op_array, offset);
  540. if (!recv || recv->opcode != ZEND_RECV_INIT) {
  541. return NULL;
  542. }
  543. return RT_CONSTANT(recv, recv->op2);
  544. }
  545. static int format_default_value(smart_str *str, zval *value) {
  546. if (Z_TYPE_P(value) <= IS_STRING) {
  547. smart_str_append_scalar(str, value, SIZE_MAX);
  548. } else if (Z_TYPE_P(value) == IS_ARRAY) {
  549. zend_string *str_key;
  550. zend_long num_key;
  551. zval *zv;
  552. bool is_list = zend_array_is_list(Z_ARRVAL_P(value));
  553. bool first = true;
  554. smart_str_appendc(str, '[');
  555. ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(value), num_key, str_key, zv) {
  556. if (!first) {
  557. smart_str_appends(str, ", ");
  558. }
  559. first = false;
  560. if (!is_list) {
  561. if (str_key) {
  562. smart_str_appendc(str, '\'');
  563. smart_str_append_escaped(str, ZSTR_VAL(str_key), ZSTR_LEN(str_key));
  564. smart_str_appendc(str, '\'');
  565. } else {
  566. smart_str_append_long(str, num_key);
  567. }
  568. smart_str_appends(str, " => ");
  569. }
  570. format_default_value(str, zv);
  571. } ZEND_HASH_FOREACH_END();
  572. smart_str_appendc(str, ']');
  573. } else if (Z_TYPE_P(value) == IS_OBJECT) {
  574. zend_object *obj = Z_OBJ_P(value);
  575. zend_class_entry *class = obj->ce;
  576. ZEND_ASSERT(class->ce_flags & ZEND_ACC_ENUM);
  577. smart_str_append(str, class->name);
  578. smart_str_appends(str, "::");
  579. smart_str_append(str, Z_STR_P(zend_enum_fetch_case_name(obj)));
  580. } else {
  581. ZEND_ASSERT(Z_TYPE_P(value) == IS_CONSTANT_AST);
  582. zend_string *ast_str = zend_ast_export("", Z_ASTVAL_P(value), "");
  583. smart_str_append(str, ast_str);
  584. zend_string_release(ast_str);
  585. }
  586. return SUCCESS;
  587. }
  588. static inline bool has_internal_arg_info(const zend_function *fptr) {
  589. return fptr->type == ZEND_INTERNAL_FUNCTION
  590. && !(fptr->common.fn_flags & ZEND_ACC_USER_ARG_INFO);
  591. }
  592. /* {{{ _parameter_string */
  593. static void _parameter_string(smart_str *str, zend_function *fptr, struct _zend_arg_info *arg_info, uint32_t offset, bool required, char* indent)
  594. {
  595. smart_str_append_printf(str, "Parameter #%d [ ", offset);
  596. if (!required) {
  597. smart_str_append_printf(str, "<optional> ");
  598. } else {
  599. smart_str_append_printf(str, "<required> ");
  600. }
  601. if (ZEND_TYPE_IS_SET(arg_info->type)) {
  602. zend_string *type_str = zend_type_to_string(arg_info->type);
  603. smart_str_append(str, type_str);
  604. smart_str_appendc(str, ' ');
  605. zend_string_release(type_str);
  606. }
  607. if (ZEND_ARG_SEND_MODE(arg_info)) {
  608. smart_str_appendc(str, '&');
  609. }
  610. if (ZEND_ARG_IS_VARIADIC(arg_info)) {
  611. smart_str_appends(str, "...");
  612. }
  613. smart_str_append_printf(str, "$%s", has_internal_arg_info(fptr)
  614. ? ((zend_internal_arg_info*)arg_info)->name : ZSTR_VAL(arg_info->name));
  615. if (!required && !ZEND_ARG_IS_VARIADIC(arg_info)) {
  616. if (fptr->type == ZEND_INTERNAL_FUNCTION) {
  617. smart_str_appends(str, " = ");
  618. /* TODO: We don't have a way to fetch the default value for an internal function
  619. * with userland arg info. */
  620. if (has_internal_arg_info(fptr)
  621. && ((zend_internal_arg_info*)arg_info)->default_value) {
  622. smart_str_appends(str, ((zend_internal_arg_info*)arg_info)->default_value);
  623. } else {
  624. smart_str_appends(str, "<default>");
  625. }
  626. } else {
  627. zval *default_value = get_default_from_recv((zend_op_array*)fptr, offset);
  628. if (default_value) {
  629. smart_str_appends(str, " = ");
  630. if (format_default_value(str, default_value) == FAILURE) {
  631. return;
  632. }
  633. }
  634. }
  635. }
  636. smart_str_appends(str, " ]");
  637. }
  638. /* }}} */
  639. /* {{{ _function_parameter_string */
  640. static void _function_parameter_string(smart_str *str, zend_function *fptr, char* indent)
  641. {
  642. struct _zend_arg_info *arg_info = fptr->common.arg_info;
  643. uint32_t i, num_args, num_required = fptr->common.required_num_args;
  644. if (!arg_info) {
  645. return;
  646. }
  647. num_args = fptr->common.num_args;
  648. if (fptr->common.fn_flags & ZEND_ACC_VARIADIC) {
  649. num_args++;
  650. }
  651. smart_str_appendc(str, '\n');
  652. smart_str_append_printf(str, "%s- Parameters [%d] {\n", indent, num_args);
  653. for (i = 0; i < num_args; i++) {
  654. smart_str_append_printf(str, "%s ", indent);
  655. _parameter_string(str, fptr, arg_info, i, i < num_required, indent);
  656. smart_str_appendc(str, '\n');
  657. arg_info++;
  658. }
  659. smart_str_append_printf(str, "%s}\n", indent);
  660. }
  661. /* }}} */
  662. /* {{{ _function_closure_string */
  663. static void _function_closure_string(smart_str *str, zend_function *fptr, char* indent)
  664. {
  665. uint32_t i, count;
  666. zend_string *key;
  667. HashTable *static_variables;
  668. if (fptr->type != ZEND_USER_FUNCTION || !fptr->op_array.static_variables) {
  669. return;
  670. }
  671. static_variables = ZEND_MAP_PTR_GET(fptr->op_array.static_variables_ptr);
  672. count = zend_hash_num_elements(static_variables);
  673. if (!count) {
  674. return;
  675. }
  676. smart_str_append_printf(str, "\n");
  677. smart_str_append_printf(str, "%s- Bound Variables [%d] {\n", indent, zend_hash_num_elements(static_variables));
  678. i = 0;
  679. ZEND_HASH_FOREACH_STR_KEY(static_variables, key) {
  680. smart_str_append_printf(str, "%s Variable #%d [ $%s ]\n", indent, i++, ZSTR_VAL(key));
  681. } ZEND_HASH_FOREACH_END();
  682. smart_str_append_printf(str, "%s}\n", indent);
  683. }
  684. /* }}} */
  685. /* {{{ _function_string */
  686. static void _function_string(smart_str *str, zend_function *fptr, zend_class_entry *scope, char* indent)
  687. {
  688. smart_str param_indent = {0};
  689. zend_function *overwrites;
  690. zend_string *lc_name;
  691. /* TBD: Repair indenting of doc comment (or is this to be done in the parser?)
  692. * What's "wrong" is that any whitespace before the doc comment start is
  693. * swallowed, leading to an unaligned comment.
  694. */
  695. if (fptr->type == ZEND_USER_FUNCTION && fptr->op_array.doc_comment) {
  696. smart_str_append_printf(str, "%s%s\n", indent, ZSTR_VAL(fptr->op_array.doc_comment));
  697. }
  698. smart_str_appendl(str, indent, strlen(indent));
  699. smart_str_append_printf(str, fptr->common.fn_flags & ZEND_ACC_CLOSURE ? "Closure [ " : (fptr->common.scope ? "Method [ " : "Function [ "));
  700. smart_str_append_printf(str, (fptr->type == ZEND_USER_FUNCTION) ? "<user" : "<internal");
  701. if (fptr->common.fn_flags & ZEND_ACC_DEPRECATED) {
  702. smart_str_appends(str, ", deprecated");
  703. }
  704. if (fptr->type == ZEND_INTERNAL_FUNCTION && ((zend_internal_function*)fptr)->module) {
  705. smart_str_append_printf(str, ":%s", ((zend_internal_function*)fptr)->module->name);
  706. }
  707. if (scope && fptr->common.scope) {
  708. if (fptr->common.scope != scope) {
  709. smart_str_append_printf(str, ", inherits %s", ZSTR_VAL(fptr->common.scope->name));
  710. } else if (fptr->common.scope->parent) {
  711. lc_name = zend_string_tolower(fptr->common.function_name);
  712. if ((overwrites = zend_hash_find_ptr(&fptr->common.scope->parent->function_table, lc_name)) != NULL) {
  713. if (fptr->common.scope != overwrites->common.scope && !(overwrites->common.fn_flags & ZEND_ACC_PRIVATE)) {
  714. smart_str_append_printf(str, ", overwrites %s", ZSTR_VAL(overwrites->common.scope->name));
  715. }
  716. }
  717. zend_string_release_ex(lc_name, 0);
  718. }
  719. }
  720. if (fptr->common.prototype && fptr->common.prototype->common.scope) {
  721. smart_str_append_printf(str, ", prototype %s", ZSTR_VAL(fptr->common.prototype->common.scope->name));
  722. }
  723. if (fptr->common.fn_flags & ZEND_ACC_CTOR) {
  724. smart_str_appends(str, ", ctor");
  725. }
  726. smart_str_appends(str, "> ");
  727. if (fptr->common.fn_flags & ZEND_ACC_ABSTRACT) {
  728. smart_str_appends(str, "abstract ");
  729. }
  730. if (fptr->common.fn_flags & ZEND_ACC_FINAL) {
  731. smart_str_appends(str, "final ");
  732. }
  733. if (fptr->common.fn_flags & ZEND_ACC_STATIC) {
  734. smart_str_appends(str, "static ");
  735. }
  736. if (fptr->common.scope) {
  737. /* These are mutually exclusive */
  738. switch (fptr->common.fn_flags & ZEND_ACC_PPP_MASK) {
  739. case ZEND_ACC_PUBLIC:
  740. smart_str_appends(str, "public ");
  741. break;
  742. case ZEND_ACC_PRIVATE:
  743. smart_str_appends(str, "private ");
  744. break;
  745. case ZEND_ACC_PROTECTED:
  746. smart_str_appends(str, "protected ");
  747. break;
  748. default:
  749. smart_str_appends(str, "<visibility error> ");
  750. break;
  751. }
  752. smart_str_appends(str, "method ");
  753. } else {
  754. smart_str_appends(str, "function ");
  755. }
  756. if (fptr->op_array.fn_flags & ZEND_ACC_RETURN_REFERENCE) {
  757. smart_str_appendc(str, '&');
  758. }
  759. smart_str_append_printf(str, "%s ] {\n", ZSTR_VAL(fptr->common.function_name));
  760. /* The information where a function is declared is only available for user classes */
  761. if (fptr->type == ZEND_USER_FUNCTION) {
  762. smart_str_append_printf(str, "%s @@ %s %d - %d\n", indent,
  763. ZSTR_VAL(fptr->op_array.filename),
  764. fptr->op_array.line_start,
  765. fptr->op_array.line_end);
  766. }
  767. smart_str_append_printf(&param_indent, "%s ", indent);
  768. smart_str_0(&param_indent);
  769. if (fptr->common.fn_flags & ZEND_ACC_CLOSURE) {
  770. _function_closure_string(str, fptr, ZSTR_VAL(param_indent.s));
  771. }
  772. _function_parameter_string(str, fptr, ZSTR_VAL(param_indent.s));
  773. smart_str_free(&param_indent);
  774. if ((fptr->op_array.fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
  775. smart_str_append_printf(str, " %s- %s [ ", indent, ZEND_ARG_TYPE_IS_TENTATIVE(&fptr->common.arg_info[-1]) ? "Tentative return" : "Return");
  776. if (ZEND_TYPE_IS_SET(fptr->common.arg_info[-1].type)) {
  777. zend_string *type_str = zend_type_to_string(fptr->common.arg_info[-1].type);
  778. smart_str_append_printf(str, "%s ", ZSTR_VAL(type_str));
  779. zend_string_release(type_str);
  780. }
  781. smart_str_appends(str, "]\n");
  782. }
  783. smart_str_append_printf(str, "%s}\n", indent);
  784. }
  785. /* }}} */
  786. static zval *property_get_default(zend_property_info *prop_info) {
  787. zend_class_entry *ce = prop_info->ce;
  788. if (prop_info->flags & ZEND_ACC_STATIC) {
  789. zval *prop = &ce->default_static_members_table[prop_info->offset];
  790. ZVAL_DEINDIRECT(prop);
  791. return prop;
  792. } else {
  793. return &ce->default_properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
  794. }
  795. }
  796. /* {{{ _property_string */
  797. static void _property_string(smart_str *str, zend_property_info *prop, const char *prop_name, char* indent)
  798. {
  799. smart_str_append_printf(str, "%sProperty [ ", indent);
  800. if (!prop) {
  801. smart_str_append_printf(str, "<dynamic> public $%s", prop_name);
  802. } else {
  803. /* These are mutually exclusive */
  804. switch (prop->flags & ZEND_ACC_PPP_MASK) {
  805. case ZEND_ACC_PUBLIC:
  806. smart_str_appends(str, "public ");
  807. break;
  808. case ZEND_ACC_PRIVATE:
  809. smart_str_appends(str, "private ");
  810. break;
  811. case ZEND_ACC_PROTECTED:
  812. smart_str_appends(str, "protected ");
  813. break;
  814. }
  815. if (prop->flags & ZEND_ACC_STATIC) {
  816. smart_str_appends(str, "static ");
  817. }
  818. if (prop->flags & ZEND_ACC_READONLY) {
  819. smart_str_appends(str, "readonly ");
  820. }
  821. if (ZEND_TYPE_IS_SET(prop->type)) {
  822. zend_string *type_str = zend_type_to_string(prop->type);
  823. smart_str_append(str, type_str);
  824. smart_str_appendc(str, ' ');
  825. zend_string_release(type_str);
  826. }
  827. if (!prop_name) {
  828. const char *class_name;
  829. zend_unmangle_property_name(prop->name, &class_name, &prop_name);
  830. }
  831. smart_str_append_printf(str, "$%s", prop_name);
  832. zval *default_value = property_get_default(prop);
  833. if (!Z_ISUNDEF_P(default_value)) {
  834. smart_str_appends(str, " = ");
  835. if (format_default_value(str, default_value) == FAILURE) {
  836. return;
  837. }
  838. }
  839. }
  840. smart_str_appends(str, " ]\n");
  841. }
  842. /* }}} */
  843. static void _extension_ini_string(zend_ini_entry *ini_entry, smart_str *str, char *indent, int number) /* {{{ */
  844. {
  845. char *comma = "";
  846. if (number == ini_entry->module_number) {
  847. smart_str_append_printf(str, " %sEntry [ %s <", indent, ZSTR_VAL(ini_entry->name));
  848. if (ini_entry->modifiable == ZEND_INI_ALL) {
  849. smart_str_appends(str, "ALL");
  850. } else {
  851. if (ini_entry->modifiable & ZEND_INI_USER) {
  852. smart_str_appends(str, "USER");
  853. comma = ",";
  854. }
  855. if (ini_entry->modifiable & ZEND_INI_PERDIR) {
  856. smart_str_append_printf(str, "%sPERDIR", comma);
  857. comma = ",";
  858. }
  859. if (ini_entry->modifiable & ZEND_INI_SYSTEM) {
  860. smart_str_append_printf(str, "%sSYSTEM", comma);
  861. }
  862. }
  863. smart_str_appends(str, "> ]\n");
  864. smart_str_append_printf(str, " %s Current = '%s'\n", indent, ini_entry->value ? ZSTR_VAL(ini_entry->value) : "");
  865. if (ini_entry->modified) {
  866. smart_str_append_printf(str, " %s Default = '%s'\n", indent, ini_entry->orig_value ? ZSTR_VAL(ini_entry->orig_value) : "");
  867. }
  868. smart_str_append_printf(str, " %s}\n", indent);
  869. }
  870. }
  871. /* }}} */
  872. static void _extension_class_string(zend_class_entry *ce, zend_string *key, smart_str *str, char *indent, zend_module_entry *module, int *num_classes) /* {{{ */
  873. {
  874. if (ce->type == ZEND_INTERNAL_CLASS && ce->info.internal.module && !strcasecmp(ce->info.internal.module->name, module->name)) {
  875. /* dump class if it is not an alias */
  876. if (zend_string_equals_ci(ce->name, key)) {
  877. smart_str_append_printf(str, "\n");
  878. _class_string(str, ce, NULL, indent);
  879. (*num_classes)++;
  880. }
  881. }
  882. }
  883. /* }}} */
  884. static void _extension_string(smart_str *str, zend_module_entry *module, char *indent) /* {{{ */
  885. {
  886. smart_str_append_printf(str, "%sExtension [ ", indent);
  887. if (module->type == MODULE_PERSISTENT) {
  888. smart_str_appends(str, "<persistent>");
  889. }
  890. if (module->type == MODULE_TEMPORARY) {
  891. smart_str_appends(str, "<temporary>" );
  892. }
  893. smart_str_append_printf(str, " extension #%d %s version %s ] {\n",
  894. module->module_number, module->name,
  895. (module->version == NO_VERSION_YET) ? "<no_version>" : module->version);
  896. if (module->deps) {
  897. const zend_module_dep* dep = module->deps;
  898. smart_str_appends(str, "\n - Dependencies {\n");
  899. while(dep->name) {
  900. smart_str_append_printf(str, "%s Dependency [ %s (", indent, dep->name);
  901. switch(dep->type) {
  902. case MODULE_DEP_REQUIRED:
  903. smart_str_appends(str, "Required");
  904. break;
  905. case MODULE_DEP_CONFLICTS:
  906. smart_str_appends(str, "Conflicts");
  907. break;
  908. case MODULE_DEP_OPTIONAL:
  909. smart_str_appends(str, "Optional");
  910. break;
  911. default:
  912. smart_str_appends(str, "Error"); /* shouldn't happen */
  913. break;
  914. }
  915. if (dep->rel) {
  916. smart_str_append_printf(str, " %s", dep->rel);
  917. }
  918. if (dep->version) {
  919. smart_str_append_printf(str, " %s", dep->version);
  920. }
  921. smart_str_appends(str, ") ]\n");
  922. dep++;
  923. }
  924. smart_str_append_printf(str, "%s }\n", indent);
  925. }
  926. {
  927. smart_str str_ini = {0};
  928. zend_ini_entry *ini_entry;
  929. ZEND_HASH_FOREACH_PTR(EG(ini_directives), ini_entry) {
  930. _extension_ini_string(ini_entry, &str_ini, indent, module->module_number);
  931. } ZEND_HASH_FOREACH_END();
  932. if (smart_str_get_len(&str_ini) > 0) {
  933. smart_str_append_printf(str, "\n - INI {\n");
  934. smart_str_append_smart_str(str, &str_ini);
  935. smart_str_append_printf(str, "%s }\n", indent);
  936. }
  937. smart_str_free(&str_ini);
  938. }
  939. {
  940. smart_str str_constants = {0};
  941. zend_constant *constant;
  942. int num_constants = 0;
  943. ZEND_HASH_FOREACH_PTR(EG(zend_constants), constant) {
  944. if (ZEND_CONSTANT_MODULE_NUMBER(constant) == module->module_number) {
  945. _const_string(&str_constants, ZSTR_VAL(constant->name), &constant->value, indent);
  946. num_constants++;
  947. }
  948. } ZEND_HASH_FOREACH_END();
  949. if (num_constants) {
  950. smart_str_append_printf(str, "\n - Constants [%d] {\n", num_constants);
  951. smart_str_append_smart_str(str, &str_constants);
  952. smart_str_append_printf(str, "%s }\n", indent);
  953. }
  954. smart_str_free(&str_constants);
  955. }
  956. {
  957. zend_function *fptr;
  958. int first = 1;
  959. ZEND_HASH_FOREACH_PTR(CG(function_table), fptr) {
  960. if (fptr->common.type==ZEND_INTERNAL_FUNCTION
  961. && fptr->internal_function.module == module) {
  962. if (first) {
  963. smart_str_append_printf(str, "\n - Functions {\n");
  964. first = 0;
  965. }
  966. _function_string(str, fptr, NULL, " ");
  967. }
  968. } ZEND_HASH_FOREACH_END();
  969. if (!first) {
  970. smart_str_append_printf(str, "%s }\n", indent);
  971. }
  972. }
  973. {
  974. zend_string *sub_indent = strpprintf(0, "%s ", indent);
  975. smart_str str_classes = {0};
  976. zend_string *key;
  977. zend_class_entry *ce;
  978. int num_classes = 0;
  979. ZEND_HASH_FOREACH_STR_KEY_PTR(EG(class_table), key, ce) {
  980. _extension_class_string(ce, key, &str_classes, ZSTR_VAL(sub_indent), module, &num_classes);
  981. } ZEND_HASH_FOREACH_END();
  982. if (num_classes) {
  983. smart_str_append_printf(str, "\n - Classes [%d] {", num_classes);
  984. smart_str_append_smart_str(str, &str_classes);
  985. smart_str_append_printf(str, "%s }\n", indent);
  986. }
  987. smart_str_free(&str_classes);
  988. zend_string_release_ex(sub_indent, 0);
  989. }
  990. smart_str_append_printf(str, "%s}\n", indent);
  991. }
  992. /* }}} */
  993. /* {{{ reflection_attribute_factory */
  994. static void reflection_attribute_factory(zval *object, HashTable *attributes, zend_attribute *data,
  995. zend_class_entry *scope, uint32_t target, zend_string *filename)
  996. {
  997. reflection_object *intern;
  998. attribute_reference *reference;
  999. reflection_instantiate(reflection_attribute_ptr, object);
  1000. intern = Z_REFLECTION_P(object);
  1001. reference = (attribute_reference*) emalloc(sizeof(attribute_reference));
  1002. reference->attributes = attributes;
  1003. reference->data = data;
  1004. reference->scope = scope;
  1005. reference->filename = filename ? zend_string_copy(filename) : NULL;
  1006. reference->target = target;
  1007. intern->ptr = reference;
  1008. intern->ref_type = REF_TYPE_ATTRIBUTE;
  1009. }
  1010. /* }}} */
  1011. static int read_attributes(zval *ret, HashTable *attributes, zend_class_entry *scope,
  1012. uint32_t offset, uint32_t target, zend_string *name, zend_class_entry *base, zend_string *filename) /* {{{ */
  1013. {
  1014. ZEND_ASSERT(attributes != NULL);
  1015. zend_attribute *attr;
  1016. zval tmp;
  1017. if (name) {
  1018. // Name based filtering using lowercased key.
  1019. zend_string *filter = zend_string_tolower(name);
  1020. ZEND_HASH_FOREACH_PTR(attributes, attr) {
  1021. if (attr->offset == offset && zend_string_equals(attr->lcname, filter)) {
  1022. reflection_attribute_factory(&tmp, attributes, attr, scope, target, filename);
  1023. add_next_index_zval(ret, &tmp);
  1024. }
  1025. } ZEND_HASH_FOREACH_END();
  1026. zend_string_release(filter);
  1027. return SUCCESS;
  1028. }
  1029. ZEND_HASH_FOREACH_PTR(attributes, attr) {
  1030. if (attr->offset != offset) {
  1031. continue;
  1032. }
  1033. if (base) {
  1034. // Base type filtering.
  1035. zend_class_entry *ce = zend_lookup_class_ex(attr->name, attr->lcname, 0);
  1036. if (ce == NULL) {
  1037. // Bailout on error, otherwise ignore unavailable class.
  1038. if (EG(exception)) {
  1039. return FAILURE;
  1040. }
  1041. continue;
  1042. }
  1043. if (!instanceof_function(ce, base)) {
  1044. continue;
  1045. }
  1046. }
  1047. reflection_attribute_factory(&tmp, attributes, attr, scope, target, filename);
  1048. add_next_index_zval(ret, &tmp);
  1049. } ZEND_HASH_FOREACH_END();
  1050. return SUCCESS;
  1051. }
  1052. /* }}} */
  1053. static void reflect_attributes(INTERNAL_FUNCTION_PARAMETERS, HashTable *attributes,
  1054. uint32_t offset, zend_class_entry *scope, uint32_t target, zend_string *filename) /* {{{ */
  1055. {
  1056. zend_string *name = NULL;
  1057. zend_long flags = 0;
  1058. zend_class_entry *base = NULL;
  1059. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|S!l", &name, &flags) == FAILURE) {
  1060. RETURN_THROWS();
  1061. }
  1062. if (flags & ~REFLECTION_ATTRIBUTE_IS_INSTANCEOF) {
  1063. zend_argument_value_error(2, "must be a valid attribute filter flag");
  1064. RETURN_THROWS();
  1065. }
  1066. if (name && (flags & REFLECTION_ATTRIBUTE_IS_INSTANCEOF)) {
  1067. if (NULL == (base = zend_lookup_class(name))) {
  1068. if (!EG(exception)) {
  1069. zend_throw_error(NULL, "Class \"%s\" not found", ZSTR_VAL(name));
  1070. }
  1071. RETURN_THROWS();
  1072. }
  1073. name = NULL;
  1074. }
  1075. if (!attributes) {
  1076. RETURN_EMPTY_ARRAY();
  1077. }
  1078. array_init(return_value);
  1079. if (FAILURE == read_attributes(return_value, attributes, scope, offset, target, name, base, filename)) {
  1080. RETURN_THROWS();
  1081. }
  1082. }
  1083. /* }}} */
  1084. static void _zend_extension_string(smart_str *str, zend_extension *extension, char *indent) /* {{{ */
  1085. {
  1086. smart_str_append_printf(str, "%sZend Extension [ %s ", indent, extension->name);
  1087. if (extension->version) {
  1088. smart_str_append_printf(str, "%s ", extension->version);
  1089. }
  1090. if (extension->copyright) {
  1091. smart_str_append_printf(str, "%s ", extension->copyright);
  1092. }
  1093. if (extension->author) {
  1094. smart_str_append_printf(str, "by %s ", extension->author);
  1095. }
  1096. if (extension->URL) {
  1097. smart_str_append_printf(str, "<%s> ", extension->URL);
  1098. }
  1099. smart_str_appends(str, "]\n");
  1100. }
  1101. /* }}} */
  1102. /* {{{ _function_check_flag */
  1103. static void _function_check_flag(INTERNAL_FUNCTION_PARAMETERS, int mask)
  1104. {
  1105. reflection_object *intern;
  1106. zend_function *mptr;
  1107. if (zend_parse_parameters_none() == FAILURE) {
  1108. RETURN_THROWS();
  1109. }
  1110. GET_REFLECTION_OBJECT_PTR(mptr);
  1111. RETURN_BOOL(mptr->common.fn_flags & mask);
  1112. }
  1113. /* }}} */
  1114. /* {{{ zend_reflection_class_factory */
  1115. PHPAPI void zend_reflection_class_factory(zend_class_entry *ce, zval *object)
  1116. {
  1117. reflection_object *intern;
  1118. zend_class_entry *reflection_ce =
  1119. ce->ce_flags & ZEND_ACC_ENUM ? reflection_enum_ptr : reflection_class_ptr;
  1120. reflection_instantiate(reflection_ce, object);
  1121. intern = Z_REFLECTION_P(object);
  1122. intern->ptr = ce;
  1123. intern->ref_type = REF_TYPE_OTHER;
  1124. intern->ce = ce;
  1125. ZVAL_STR_COPY(reflection_prop_name(object), ce->name);
  1126. }
  1127. /* }}} */
  1128. /* {{{ reflection_extension_factory */
  1129. static void reflection_extension_factory(zval *object, const char *name_str)
  1130. {
  1131. reflection_object *intern;
  1132. size_t name_len = strlen(name_str);
  1133. zend_string *lcname;
  1134. struct _zend_module_entry *module;
  1135. lcname = zend_string_alloc(name_len, 0);
  1136. zend_str_tolower_copy(ZSTR_VAL(lcname), name_str, name_len);
  1137. module = zend_hash_find_ptr(&module_registry, lcname);
  1138. zend_string_efree(lcname);
  1139. if (!module) {
  1140. return;
  1141. }
  1142. reflection_instantiate(reflection_extension_ptr, object);
  1143. intern = Z_REFLECTION_P(object);
  1144. intern->ptr = module;
  1145. intern->ref_type = REF_TYPE_OTHER;
  1146. intern->ce = NULL;
  1147. ZVAL_STRINGL(reflection_prop_name(object), module->name, name_len);
  1148. }
  1149. /* }}} */
  1150. /* {{{ reflection_parameter_factory */
  1151. static void reflection_parameter_factory(zend_function *fptr, zval *closure_object, struct _zend_arg_info *arg_info, uint32_t offset, bool required, zval *object)
  1152. {
  1153. reflection_object *intern;
  1154. parameter_reference *reference;
  1155. zval *prop_name;
  1156. reflection_instantiate(reflection_parameter_ptr, object);
  1157. intern = Z_REFLECTION_P(object);
  1158. reference = (parameter_reference*) emalloc(sizeof(parameter_reference));
  1159. reference->arg_info = arg_info;
  1160. reference->offset = offset;
  1161. reference->required = required;
  1162. reference->fptr = fptr;
  1163. intern->ptr = reference;
  1164. intern->ref_type = REF_TYPE_PARAMETER;
  1165. intern->ce = fptr->common.scope;
  1166. if (closure_object) {
  1167. ZVAL_OBJ_COPY(&intern->obj, Z_OBJ_P(closure_object));
  1168. }
  1169. prop_name = reflection_prop_name(object);
  1170. if (has_internal_arg_info(fptr)) {
  1171. ZVAL_STRING(prop_name, ((zend_internal_arg_info*)arg_info)->name);
  1172. } else {
  1173. ZVAL_STR_COPY(prop_name, arg_info->name);
  1174. }
  1175. }
  1176. /* }}} */
  1177. typedef enum {
  1178. NAMED_TYPE = 0,
  1179. UNION_TYPE = 1,
  1180. INTERSECTION_TYPE = 2
  1181. } reflection_type_kind;
  1182. /* For backwards compatibility reasons, we need to return T|null style unions
  1183. * as a ReflectionNamedType. Here we determine what counts as a union type and
  1184. * what doesn't. */
  1185. static reflection_type_kind get_type_kind(zend_type type) {
  1186. uint32_t type_mask_without_null = ZEND_TYPE_PURE_MASK_WITHOUT_NULL(type);
  1187. if (ZEND_TYPE_HAS_LIST(type)) {
  1188. if (ZEND_TYPE_IS_INTERSECTION(type)) {
  1189. return INTERSECTION_TYPE;
  1190. }
  1191. ZEND_ASSERT(ZEND_TYPE_IS_UNION(type));
  1192. return UNION_TYPE;
  1193. }
  1194. if (ZEND_TYPE_IS_COMPLEX(type)) {
  1195. if (type_mask_without_null != 0) {
  1196. return UNION_TYPE;
  1197. }
  1198. return NAMED_TYPE;
  1199. }
  1200. if (type_mask_without_null == MAY_BE_BOOL || ZEND_TYPE_PURE_MASK(type) == MAY_BE_ANY) {
  1201. return NAMED_TYPE;
  1202. }
  1203. /* Check that only one bit is set. */
  1204. if ((type_mask_without_null & (type_mask_without_null - 1)) != 0) {
  1205. return UNION_TYPE;
  1206. }
  1207. return NAMED_TYPE;
  1208. }
  1209. /* {{{ reflection_type_factory */
  1210. static void reflection_type_factory(zend_type type, zval *object, bool legacy_behavior)
  1211. {
  1212. reflection_object *intern;
  1213. type_reference *reference;
  1214. reflection_type_kind type_kind = get_type_kind(type);
  1215. bool is_mixed = ZEND_TYPE_PURE_MASK(type) == MAY_BE_ANY;
  1216. switch (type_kind) {
  1217. case INTERSECTION_TYPE:
  1218. reflection_instantiate(reflection_intersection_type_ptr, object);
  1219. break;
  1220. case UNION_TYPE:
  1221. reflection_instantiate(reflection_union_type_ptr, object);
  1222. break;
  1223. case NAMED_TYPE:
  1224. reflection_instantiate(reflection_named_type_ptr, object);
  1225. break;
  1226. EMPTY_SWITCH_DEFAULT_CASE();
  1227. }
  1228. intern = Z_REFLECTION_P(object);
  1229. reference = (type_reference*) emalloc(sizeof(type_reference));
  1230. reference->type = type;
  1231. reference->legacy_behavior = legacy_behavior && type_kind == NAMED_TYPE && !is_mixed;
  1232. intern->ptr = reference;
  1233. intern->ref_type = REF_TYPE_TYPE;
  1234. /* Property types may be resolved during the lifetime of the ReflectionType.
  1235. * If we reference a string, make sure it doesn't get released. However, only
  1236. * do this for the top-level type, as resolutions inside type lists will be
  1237. * fully visible to us (we'd have to do a fully copy of the type if we wanted
  1238. * to prevent that). */
  1239. if (ZEND_TYPE_HAS_NAME(type)) {
  1240. zend_string_addref(ZEND_TYPE_NAME(type));
  1241. }
  1242. }
  1243. /* }}} */
  1244. /* {{{ reflection_function_factory */
  1245. static void reflection_function_factory(zend_function *function, zval *closure_object, zval *object)
  1246. {
  1247. reflection_object *intern;
  1248. reflection_instantiate(reflection_function_ptr, object);
  1249. intern = Z_REFLECTION_P(object);
  1250. intern->ptr = function;
  1251. intern->ref_type = REF_TYPE_FUNCTION;
  1252. intern->ce = NULL;
  1253. if (closure_object) {
  1254. ZVAL_OBJ_COPY(&intern->obj, Z_OBJ_P(closure_object));
  1255. }
  1256. ZVAL_STR_COPY(reflection_prop_name(object), function->common.function_name);
  1257. }
  1258. /* }}} */
  1259. /* {{{ reflection_method_factory */
  1260. static void reflection_method_factory(zend_class_entry *ce, zend_function *method, zval *closure_object, zval *object)
  1261. {
  1262. reflection_object *intern;
  1263. reflection_instantiate(reflection_method_ptr, object);
  1264. intern = Z_REFLECTION_P(object);
  1265. intern->ptr = method;
  1266. intern->ref_type = REF_TYPE_FUNCTION;
  1267. intern->ce = ce;
  1268. if (closure_object) {
  1269. ZVAL_OBJ_COPY(&intern->obj, Z_OBJ_P(closure_object));
  1270. }
  1271. ZVAL_STR_COPY(reflection_prop_name(object), method->common.function_name);
  1272. ZVAL_STR_COPY(reflection_prop_class(object), method->common.scope->name);
  1273. }
  1274. /* }}} */
  1275. /* {{{ reflection_property_factory */
  1276. static void reflection_property_factory(zend_class_entry *ce, zend_string *name, zend_property_info *prop, zval *object)
  1277. {
  1278. reflection_object *intern;
  1279. property_reference *reference;
  1280. reflection_instantiate(reflection_property_ptr, object);
  1281. intern = Z_REFLECTION_P(object);
  1282. reference = (property_reference*) emalloc(sizeof(property_reference));
  1283. reference->prop = prop;
  1284. reference->unmangled_name = zend_string_copy(name);
  1285. intern->ptr = reference;
  1286. intern->ref_type = REF_TYPE_PROPERTY;
  1287. intern->ce = ce;
  1288. ZVAL_STR_COPY(reflection_prop_name(object), name);
  1289. ZVAL_STR_COPY(reflection_prop_class(object), prop ? prop->ce->name : ce->name);
  1290. }
  1291. /* }}} */
  1292. static void reflection_property_factory_str(zend_class_entry *ce, const char *name_str, size_t name_len, zend_property_info *prop, zval *object)
  1293. {
  1294. zend_string *name = zend_string_init(name_str, name_len, 0);
  1295. reflection_property_factory(ce, name, prop, object);
  1296. zend_string_release(name);
  1297. }
  1298. /* {{{ reflection_class_constant_factory */
  1299. static void reflection_class_constant_factory(zend_string *name_str, zend_class_constant *constant, zval *object)
  1300. {
  1301. reflection_object *intern;
  1302. reflection_instantiate(reflection_class_constant_ptr, object);
  1303. intern = Z_REFLECTION_P(object);
  1304. intern->ptr = constant;
  1305. intern->ref_type = REF_TYPE_CLASS_CONSTANT;
  1306. intern->ce = constant->ce;
  1307. ZVAL_STR_COPY(reflection_prop_name(object), name_str);
  1308. ZVAL_STR_COPY(reflection_prop_class(object), constant->ce->name);
  1309. }
  1310. /* }}} */
  1311. static void reflection_enum_case_factory(zend_class_entry *ce, zend_string *name_str, zend_class_constant *constant, zval *object)
  1312. {
  1313. reflection_object *intern;
  1314. zend_class_entry *case_reflection_class = ce->backed_enum_table == IS_UNDEF
  1315. ? reflection_enum_unit_case_ptr
  1316. : reflection_enum_backed_case_ptr;
  1317. reflection_instantiate(case_reflection_class, object);
  1318. intern = Z_REFLECTION_P(object);
  1319. intern->ptr = constant;
  1320. intern->ref_type = REF_TYPE_CLASS_CONSTANT;
  1321. intern->ce = constant->ce;
  1322. ZVAL_STR_COPY(reflection_prop_name(object), name_str);
  1323. ZVAL_STR_COPY(reflection_prop_class(object), constant->ce->name);
  1324. }
  1325. static int get_parameter_default(zval *result, parameter_reference *param) {
  1326. if (param->fptr->type == ZEND_INTERNAL_FUNCTION) {
  1327. if (param->fptr->common.fn_flags & ZEND_ACC_USER_ARG_INFO) {
  1328. /* We don't have a way to determine the default value for this case right now. */
  1329. return FAILURE;
  1330. }
  1331. return zend_get_default_from_internal_arg_info(
  1332. result, (zend_internal_arg_info *) param->arg_info);
  1333. } else {
  1334. zval *default_value = get_default_from_recv((zend_op_array *) param->fptr, param->offset);
  1335. if (!default_value) {
  1336. return FAILURE;
  1337. }
  1338. ZVAL_COPY(result, default_value);
  1339. return SUCCESS;
  1340. }
  1341. }
  1342. /* {{{ Preventing __clone from being called */
  1343. ZEND_METHOD(ReflectionClass, __clone)
  1344. {
  1345. /* Should never be executable */
  1346. _DO_THROW("Cannot clone object using __clone()");
  1347. }
  1348. /* }}} */
  1349. /* {{{ Returns an array of modifier names */
  1350. ZEND_METHOD(Reflection, getModifierNames)
  1351. {
  1352. zend_long modifiers;
  1353. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &modifiers) == FAILURE) {
  1354. RETURN_THROWS();
  1355. }
  1356. array_init(return_value);
  1357. if (modifiers & (ZEND_ACC_ABSTRACT | ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
  1358. add_next_index_stringl(return_value, "abstract", sizeof("abstract")-1);
  1359. }
  1360. if (modifiers & ZEND_ACC_FINAL) {
  1361. add_next_index_stringl(return_value, "final", sizeof("final")-1);
  1362. }
  1363. /* These are mutually exclusive */
  1364. switch (modifiers & ZEND_ACC_PPP_MASK) {
  1365. case ZEND_ACC_PUBLIC:
  1366. add_next_index_stringl(return_value, "public", sizeof("public")-1);
  1367. break;
  1368. case ZEND_ACC_PRIVATE:
  1369. add_next_index_stringl(return_value, "private", sizeof("private")-1);
  1370. break;
  1371. case ZEND_ACC_PROTECTED:
  1372. add_next_index_stringl(return_value, "protected", sizeof("protected")-1);
  1373. break;
  1374. }
  1375. if (modifiers & ZEND_ACC_STATIC) {
  1376. add_next_index_stringl(return_value, "static", sizeof("static")-1);
  1377. }
  1378. if (modifiers & ZEND_ACC_READONLY) {
  1379. add_next_index_stringl(return_value, "readonly", sizeof("readonly")-1);
  1380. }
  1381. }
  1382. /* }}} */
  1383. /* {{{ Constructor. Throws an Exception in case the given function does not exist */
  1384. ZEND_METHOD(ReflectionFunction, __construct)
  1385. {
  1386. zval *object;
  1387. zend_object *closure_obj = NULL;
  1388. reflection_object *intern;
  1389. zend_function *fptr;
  1390. zend_string *fname, *lcname;
  1391. object = ZEND_THIS;
  1392. intern = Z_REFLECTION_P(object);
  1393. ZEND_PARSE_PARAMETERS_START(1, 1)
  1394. Z_PARAM_OBJ_OF_CLASS_OR_STR(closure_obj, zend_ce_closure, fname)
  1395. ZEND_PARSE_PARAMETERS_END();
  1396. if (closure_obj) {
  1397. fptr = (zend_function*)zend_get_closure_method_def(closure_obj);
  1398. } else {
  1399. if (UNEXPECTED(ZSTR_VAL(fname)[0] == '\\')) {
  1400. /* Ignore leading "\" */
  1401. ALLOCA_FLAG(use_heap)
  1402. ZSTR_ALLOCA_ALLOC(lcname, ZSTR_LEN(fname) - 1, use_heap);
  1403. zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(fname) + 1, ZSTR_LEN(fname) - 1);
  1404. fptr = zend_fetch_function(lcname);
  1405. ZSTR_ALLOCA_FREE(lcname, use_heap);
  1406. } else {
  1407. lcname = zend_string_tolower(fname);
  1408. fptr = zend_fetch_function(lcname);
  1409. zend_string_release(lcname);
  1410. }
  1411. if (fptr == NULL) {
  1412. zend_throw_exception_ex(reflection_exception_ptr, 0,
  1413. "Function %s() does not exist", ZSTR_VAL(fname));
  1414. RETURN_THROWS();
  1415. }
  1416. }
  1417. if (intern->ptr) {
  1418. zval_ptr_dtor(&intern->obj);
  1419. zval_ptr_dtor(reflection_prop_name(object));
  1420. }
  1421. ZVAL_STR_COPY(reflection_prop_name(object), fptr->common.function_name);
  1422. intern->ptr = fptr;
  1423. intern->ref_type = REF_TYPE_FUNCTION;
  1424. if (closure_obj) {
  1425. ZVAL_OBJ_COPY(&intern->obj, closure_obj);
  1426. } else {
  1427. ZVAL_UNDEF(&intern->obj);
  1428. }
  1429. intern->ce = NULL;
  1430. }
  1431. /* }}} */
  1432. /* {{{ Returns a string representation */
  1433. ZEND_METHOD(ReflectionFunction, __toString)
  1434. {
  1435. reflection_object *intern;
  1436. zend_function *fptr;
  1437. smart_str str = {0};
  1438. if (zend_parse_parameters_none() == FAILURE) {
  1439. RETURN_THROWS();
  1440. }
  1441. GET_REFLECTION_OBJECT_PTR(fptr);
  1442. _function_string(&str, fptr, intern->ce, "");
  1443. RETURN_STR(smart_str_extract(&str));
  1444. }
  1445. /* }}} */
  1446. /* {{{ Returns this function's name */
  1447. ZEND_METHOD(ReflectionFunctionAbstract, getName)
  1448. {
  1449. reflection_object *intern;
  1450. zend_function *fptr;
  1451. if (zend_parse_parameters_none() == FAILURE) {
  1452. RETURN_THROWS();
  1453. }
  1454. GET_REFLECTION_OBJECT_PTR(fptr);
  1455. RETURN_STR_COPY(fptr->common.function_name);
  1456. }
  1457. /* }}} */
  1458. /* {{{ Returns whether this is a closure */
  1459. ZEND_METHOD(ReflectionFunctionAbstract, isClosure)
  1460. {
  1461. reflection_object *intern;
  1462. zend_function *fptr;
  1463. if (zend_parse_parameters_none() == FAILURE) {
  1464. RETURN_THROWS();
  1465. }
  1466. GET_REFLECTION_OBJECT_PTR(fptr);
  1467. RETURN_BOOL(fptr->common.fn_flags & ZEND_ACC_CLOSURE);
  1468. }
  1469. /* }}} */
  1470. /* {{{ Returns this pointer bound to closure */
  1471. ZEND_METHOD(ReflectionFunctionAbstract, getClosureThis)
  1472. {
  1473. reflection_object *intern;
  1474. zval* closure_this;
  1475. if (zend_parse_parameters_none() == FAILURE) {
  1476. RETURN_THROWS();
  1477. }
  1478. GET_REFLECTION_OBJECT();
  1479. if (!Z_ISUNDEF(intern->obj)) {
  1480. closure_this = zend_get_closure_this_ptr(&intern->obj);
  1481. if (!Z_ISUNDEF_P(closure_this)) {
  1482. RETURN_OBJ_COPY(Z_OBJ_P(closure_this));
  1483. }
  1484. }
  1485. }
  1486. /* }}} */
  1487. /* {{{ Returns the scope associated to the closure */
  1488. ZEND_METHOD(ReflectionFunctionAbstract, getClosureScopeClass)
  1489. {
  1490. reflection_object *intern;
  1491. const zend_function *closure_func;
  1492. if (zend_parse_parameters_none() == FAILURE) {
  1493. RETURN_THROWS();
  1494. }
  1495. GET_REFLECTION_OBJECT();
  1496. if (!Z_ISUNDEF(intern->obj)) {
  1497. closure_func = zend_get_closure_method_def(Z_OBJ(intern->obj));
  1498. if (closure_func && closure_func->common.scope) {
  1499. zend_reflection_class_factory(closure_func->common.scope, return_value);
  1500. }
  1501. }
  1502. }
  1503. /* }}} */
  1504. /* {{{ Returns the called scope associated to the closure */
  1505. ZEND_METHOD(ReflectionFunctionAbstract, getClosureCalledClass)
  1506. {
  1507. reflection_object *intern;
  1508. if (zend_parse_parameters_none() == FAILURE) {
  1509. RETURN_THROWS();
  1510. }
  1511. GET_REFLECTION_OBJECT();
  1512. if (!Z_ISUNDEF(intern->obj)) {
  1513. zend_class_entry *called_scope;
  1514. zend_function *closure_func;
  1515. zend_object *object;
  1516. if (Z_OBJ_HANDLER(intern->obj, get_closure)
  1517. && Z_OBJ_HANDLER(intern->obj, get_closure)(Z_OBJ(intern->obj), &called_scope, &closure_func, &object, 1) == SUCCESS
  1518. && closure_func && (called_scope || closure_func->common.scope)) {
  1519. zend_reflection_class_factory(called_scope ? (zend_class_entry *) called_scope : closure_func->common.scope, return_value);
  1520. }
  1521. }
  1522. }
  1523. /* }}} */
  1524. /* {{{ Returns an associative array containing the closures lexical scope variables */
  1525. ZEND_METHOD(ReflectionFunctionAbstract, getClosureUsedVariables)
  1526. {
  1527. reflection_object *intern;
  1528. const zend_function *closure_func;
  1529. if (zend_parse_parameters_none() == FAILURE) {
  1530. RETURN_THROWS();
  1531. }
  1532. GET_REFLECTION_OBJECT();
  1533. array_init(return_value);
  1534. if (!Z_ISUNDEF(intern->obj)) {
  1535. closure_func = zend_get_closure_method_def(Z_OBJ(intern->obj));
  1536. if (closure_func == NULL ||
  1537. closure_func->type != ZEND_USER_FUNCTION ||
  1538. closure_func->op_array.static_variables == NULL) {
  1539. return;
  1540. }
  1541. const zend_op_array *ops = &closure_func->op_array;
  1542. HashTable *static_variables = ZEND_MAP_PTR_GET(ops->static_variables_ptr);
  1543. if (!static_variables) {
  1544. return;
  1545. }
  1546. zend_op *opline = ops->opcodes + ops->num_args;
  1547. for (; opline->opcode == ZEND_BIND_STATIC; opline++) {
  1548. if (!(opline->extended_value & (ZEND_BIND_IMPLICIT|ZEND_BIND_EXPLICIT))) {
  1549. continue;
  1550. }
  1551. Bucket *bucket = (Bucket*)
  1552. (((char*)static_variables->arData) +
  1553. (opline->extended_value & ~(ZEND_BIND_REF|ZEND_BIND_IMPLICIT|ZEND_BIND_EXPLICIT)));
  1554. if (Z_ISUNDEF(bucket->val)) {
  1555. continue;
  1556. }
  1557. zend_hash_add_new(Z_ARRVAL_P(return_value), bucket->key, &bucket->val);
  1558. Z_TRY_ADDREF(bucket->val);
  1559. }
  1560. }
  1561. } /* }}} */
  1562. /* {{{ Returns a dynamically created closure for the function */
  1563. ZEND_METHOD(ReflectionFunction, getClosure)
  1564. {
  1565. reflection_object *intern;
  1566. zend_function *fptr;
  1567. if (zend_parse_parameters_none() == FAILURE) {
  1568. RETURN_THROWS();
  1569. }
  1570. GET_REFLECTION_OBJECT_PTR(fptr);
  1571. if (!Z_ISUNDEF(intern->obj)) {
  1572. /* Closures are immutable objects */
  1573. RETURN_OBJ_COPY(Z_OBJ(intern->obj));
  1574. } else {
  1575. zend_create_fake_closure(return_value, fptr, NULL, NULL, NULL);
  1576. }
  1577. }
  1578. /* }}} */
  1579. /* {{{ Returns whether this is an internal function */
  1580. ZEND_METHOD(ReflectionFunctionAbstract, isInternal)
  1581. {
  1582. reflection_object *intern;
  1583. zend_function *fptr;
  1584. if (zend_parse_parameters_none() == FAILURE) {
  1585. RETURN_THROWS();
  1586. }
  1587. GET_REFLECTION_OBJECT_PTR(fptr);
  1588. RETURN_BOOL(fptr->type == ZEND_INTERNAL_FUNCTION);
  1589. }
  1590. /* }}} */
  1591. /* {{{ Returns whether this is a user-defined function */
  1592. ZEND_METHOD(ReflectionFunctionAbstract, isUserDefined)
  1593. {
  1594. reflection_object *intern;
  1595. zend_function *fptr;
  1596. if (zend_parse_parameters_none() == FAILURE) {
  1597. RETURN_THROWS();
  1598. }
  1599. GET_REFLECTION_OBJECT_PTR(fptr);
  1600. RETURN_BOOL(fptr->type == ZEND_USER_FUNCTION);
  1601. }
  1602. /* }}} */
  1603. /* {{{ Returns whether this function has been disabled or not */
  1604. ZEND_METHOD(ReflectionFunction, isDisabled)
  1605. {
  1606. if (zend_parse_parameters_none() == FAILURE) {
  1607. RETURN_THROWS();
  1608. }
  1609. /* A disabled function cannot be queried using Reflection. */
  1610. RETURN_FALSE;
  1611. }
  1612. /* }}} */
  1613. /* {{{ Returns the filename of the file this function was declared in */
  1614. ZEND_METHOD(ReflectionFunctionAbstract, getFileName)
  1615. {
  1616. reflection_object *intern;
  1617. zend_function *fptr;
  1618. if (zend_parse_parameters_none() == FAILURE) {
  1619. RETURN_THROWS();
  1620. }
  1621. GET_REFLECTION_OBJECT_PTR(fptr);
  1622. if (fptr->type == ZEND_USER_FUNCTION) {
  1623. RETURN_STR_COPY(fptr->op_array.filename);
  1624. }
  1625. RETURN_FALSE;
  1626. }
  1627. /* }}} */
  1628. /* {{{ Returns the line this function's declaration starts at */
  1629. ZEND_METHOD(ReflectionFunctionAbstract, getStartLine)
  1630. {
  1631. reflection_object *intern;
  1632. zend_function *fptr;
  1633. if (zend_parse_parameters_none() == FAILURE) {
  1634. RETURN_THROWS();
  1635. }
  1636. GET_REFLECTION_OBJECT_PTR(fptr);
  1637. if (fptr->type == ZEND_USER_FUNCTION) {
  1638. RETURN_LONG(fptr->op_array.line_start);
  1639. }
  1640. RETURN_FALSE;
  1641. }
  1642. /* }}} */
  1643. /* {{{ Returns the line this function's declaration ends at */
  1644. ZEND_METHOD(ReflectionFunctionAbstract, getEndLine)
  1645. {
  1646. reflection_object *intern;
  1647. zend_function *fptr;
  1648. if (zend_parse_parameters_none() == FAILURE) {
  1649. RETURN_THROWS();
  1650. }
  1651. GET_REFLECTION_OBJECT_PTR(fptr);
  1652. if (fptr->type == ZEND_USER_FUNCTION) {
  1653. RETURN_LONG(fptr->op_array.line_end);
  1654. }
  1655. RETURN_FALSE;
  1656. }
  1657. /* }}} */
  1658. /* {{{ Returns the doc comment for this function */
  1659. ZEND_METHOD(ReflectionFunctionAbstract, getDocComment)
  1660. {
  1661. reflection_object *intern;
  1662. zend_function *fptr;
  1663. if (zend_parse_parameters_none() == FAILURE) {
  1664. RETURN_THROWS();
  1665. }
  1666. GET_REFLECTION_OBJECT_PTR(fptr);
  1667. if (fptr->type == ZEND_USER_FUNCTION && fptr->op_array.doc_comment) {
  1668. RETURN_STR_COPY(fptr->op_array.doc_comment);
  1669. }
  1670. RETURN_FALSE;
  1671. }
  1672. /* }}} */
  1673. /* {{{ Returns the attributes of this function */
  1674. ZEND_METHOD(ReflectionFunctionAbstract, getAttributes)
  1675. {
  1676. reflection_object *intern;
  1677. zend_function *fptr;
  1678. uint32_t target;
  1679. GET_REFLECTION_OBJECT_PTR(fptr);
  1680. if (fptr->common.scope && (fptr->common.fn_flags & (ZEND_ACC_CLOSURE|ZEND_ACC_FAKE_CLOSURE)) != ZEND_ACC_CLOSURE) {
  1681. target = ZEND_ATTRIBUTE_TARGET_METHOD;
  1682. } else {
  1683. target = ZEND_ATTRIBUTE_TARGET_FUNCTION;
  1684. }
  1685. reflect_attributes(INTERNAL_FUNCTION_PARAM_PASSTHRU,
  1686. fptr->common.attributes, 0, fptr->common.scope, target,
  1687. fptr->type == ZEND_USER_FUNCTION ? fptr->op_array.filename : NULL);
  1688. }
  1689. /* }}} */
  1690. /* {{{ Returns an associative array containing this function's static variables and their values */
  1691. ZEND_METHOD(ReflectionFunctionAbstract, getStaticVariables)
  1692. {
  1693. reflection_object *intern;
  1694. zend_function *fptr;
  1695. zval *val;
  1696. if (zend_parse_parameters_none() == FAILURE) {
  1697. RETURN_THROWS();
  1698. }
  1699. GET_REFLECTION_OBJECT_PTR(fptr);
  1700. /* Return an empty array in case no static variables exist */
  1701. if (fptr->type == ZEND_USER_FUNCTION && fptr->op_array.static_variables != NULL) {
  1702. HashTable *ht;
  1703. array_init(return_value);
  1704. ht = ZEND_MAP_PTR_GET(fptr->op_array.static_variables_ptr);
  1705. if (!ht) {
  1706. ht = zend_array_dup(fptr->op_array.static_variables);
  1707. ZEND_MAP_PTR_SET(fptr->op_array.static_variables_ptr, ht);
  1708. }
  1709. ZEND_HASH_FOREACH_VAL(ht, val) {
  1710. if (UNEXPECTED(zval_update_constant_ex(val, fptr->common.scope) != SUCCESS)) {
  1711. RETURN_THROWS();
  1712. }
  1713. } ZEND_HASH_FOREACH_END();
  1714. zend_hash_copy(Z_ARRVAL_P(return_value), ht, zval_add_ref);
  1715. } else {
  1716. RETURN_EMPTY_ARRAY();
  1717. }
  1718. }
  1719. /* }}} */
  1720. /* {{{ Invokes the function */
  1721. ZEND_METHOD(ReflectionFunction, invoke)
  1722. {
  1723. zval retval;
  1724. zval *params;
  1725. int result, num_args;
  1726. HashTable *named_params;
  1727. zend_fcall_info fci;
  1728. zend_fcall_info_cache fcc;
  1729. reflection_object *intern;
  1730. zend_function *fptr;
  1731. ZEND_PARSE_PARAMETERS_START(0, -1)
  1732. Z_PARAM_VARIADIC_WITH_NAMED(params, num_args, named_params)
  1733. ZEND_PARSE_PARAMETERS_END();
  1734. GET_REFLECTION_OBJECT_PTR(fptr);
  1735. fci.size = sizeof(fci);
  1736. ZVAL_UNDEF(&fci.function_name);
  1737. fci.object = NULL;
  1738. fci.retval = &retval;
  1739. fci.param_count = num_args;
  1740. fci.params = params;
  1741. fci.named_params = named_params;
  1742. fcc.function_handler = fptr;
  1743. fcc.called_scope = NULL;
  1744. fcc.object = NULL;
  1745. if (!Z_ISUNDEF(intern->obj)) {
  1746. Z_OBJ_HT(intern->obj)->get_closure(
  1747. Z_OBJ(intern->obj), &fcc.called_scope, &fcc.function_handler, &fcc.object, 0);
  1748. }
  1749. result = zend_call_function(&fci, &fcc);
  1750. if (result == FAILURE) {
  1751. zend_throw_exception_ex(reflection_exception_ptr, 0,
  1752. "Invocation of function %s() failed", ZSTR_VAL(fptr->common.function_name));
  1753. RETURN_THROWS();
  1754. }
  1755. if (Z_TYPE(retval) != IS_UNDEF) {
  1756. if (Z_ISREF(retval)) {
  1757. zend_unwrap_reference(&retval);
  1758. }
  1759. ZVAL_COPY_VALUE(return_value, &retval);
  1760. }
  1761. }
  1762. /* }}} */
  1763. /* {{{ Invokes the function and pass its arguments as array. */
  1764. ZEND_METHOD(ReflectionFunction, invokeArgs)
  1765. {
  1766. zval retval;
  1767. int result;
  1768. zend_fcall_info fci;
  1769. zend_fcall_info_cache fcc;
  1770. reflection_object *intern;
  1771. zend_function *fptr;
  1772. HashTable *params;
  1773. GET_REFLECTION_OBJECT_PTR(fptr);
  1774. if (zend_parse_parameters(ZEND_NUM_ARGS(), "h", &params) == FAILURE) {
  1775. RETURN_THROWS();
  1776. }
  1777. fci.size = sizeof(fci);
  1778. ZVAL_UNDEF(&fci.function_name);
  1779. fci.object = NULL;
  1780. fci.retval = &retval;
  1781. fci.param_count = 0;
  1782. fci.params = NULL;
  1783. fci.named_params = params;
  1784. fcc.function_handler = fptr;
  1785. fcc.called_scope = NULL;
  1786. fcc.object = NULL;
  1787. if (!Z_ISUNDEF(intern->obj)) {
  1788. Z_OBJ_HT(intern->obj)->get_closure(
  1789. Z_OBJ(intern->obj), &fcc.called_scope, &fcc.function_handler, &fcc.object, 0);
  1790. }
  1791. result = zend_call_function(&fci, &fcc);
  1792. if (result == FAILURE) {
  1793. zend_throw_exception_ex(reflection_exception_ptr, 0,
  1794. "Invocation of function %s() failed", ZSTR_VAL(fptr->common.function_name));
  1795. RETURN_THROWS();
  1796. }
  1797. if (Z_TYPE(retval) != IS_UNDEF) {
  1798. if (Z_ISREF(retval)) {
  1799. zend_unwrap_reference(&retval);
  1800. }
  1801. ZVAL_COPY_VALUE(return_value, &retval);
  1802. }
  1803. }
  1804. /* }}} */
  1805. /* {{{ Gets whether this function returns a reference */
  1806. ZEND_METHOD(ReflectionFunctionAbstract, returnsReference)
  1807. {
  1808. reflection_object *intern;
  1809. zend_function *fptr;
  1810. GET_REFLECTION_OBJECT_PTR(fptr);
  1811. if (zend_parse_parameters_none() == FAILURE) {
  1812. RETURN_THROWS();
  1813. }
  1814. RETURN_BOOL((fptr->op_array.fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0);
  1815. }
  1816. /* }}} */
  1817. /* {{{ Gets the number of parameters */
  1818. ZEND_METHOD(ReflectionFunctionAbstract, getNumberOfParameters)
  1819. {
  1820. reflection_object *intern;
  1821. zend_function *fptr;
  1822. uint32_t num_args;
  1823. GET_REFLECTION_OBJECT_PTR(fptr);
  1824. if (zend_parse_parameters_none() == FAILURE) {
  1825. RETURN_THROWS();
  1826. }
  1827. num_args = fptr->common.num_args;
  1828. if (fptr->common.fn_flags & ZEND_ACC_VARIADIC) {
  1829. num_args++;
  1830. }
  1831. RETURN_LONG(num_args);
  1832. }
  1833. /* }}} */
  1834. /* {{{ Gets the number of required parameters */
  1835. ZEND_METHOD(ReflectionFunctionAbstract, getNumberOfRequiredParameters)
  1836. {
  1837. reflection_object *intern;
  1838. zend_function *fptr;
  1839. GET_REFLECTION_OBJECT_PTR(fptr);
  1840. if (zend_parse_parameters_none() == FAILURE) {
  1841. RETURN_THROWS();
  1842. }
  1843. RETURN_LONG(fptr->common.required_num_args);
  1844. }
  1845. /* }}} */
  1846. /* {{{ Returns an array of parameter objects for this function */
  1847. ZEND_METHOD(ReflectionFunctionAbstract, getParameters)
  1848. {
  1849. reflection_object *intern;
  1850. zend_function *fptr;
  1851. uint32_t i, num_args;
  1852. struct _zend_arg_info *arg_info;
  1853. GET_REFLECTION_OBJECT_PTR(fptr);
  1854. if (zend_parse_parameters_none() == FAILURE) {
  1855. RETURN_THROWS();
  1856. }
  1857. arg_info= fptr->common.arg_info;
  1858. num_args = fptr->common.num_args;
  1859. if (fptr->common.fn_flags & ZEND_ACC_VARIADIC) {
  1860. num_args++;
  1861. }
  1862. if (!num_args) {
  1863. RETURN_EMPTY_ARRAY();
  1864. }
  1865. array_init(return_value);
  1866. for (i = 0; i < num_args; i++) {
  1867. zval parameter;
  1868. reflection_parameter_factory(
  1869. _copy_function(fptr),
  1870. Z_ISUNDEF(intern->obj) ? NULL : &intern->obj,
  1871. arg_info,
  1872. i,
  1873. i < fptr->common.required_num_args,
  1874. &parameter
  1875. );
  1876. zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &parameter);
  1877. arg_info++;
  1878. }
  1879. }
  1880. /* }}} */
  1881. /* {{{ Returns NULL or the extension the function belongs to */
  1882. ZEND_METHOD(ReflectionFunctionAbstract, getExtension)
  1883. {
  1884. reflection_object *intern;
  1885. zend_function *fptr;
  1886. zend_internal_function *internal;
  1887. GET_REFLECTION_OBJECT_PTR(fptr);
  1888. if (zend_parse_parameters_none() == FAILURE) {
  1889. RETURN_THROWS();
  1890. }
  1891. if (fptr->type != ZEND_INTERNAL_FUNCTION) {
  1892. RETURN_NULL();
  1893. }
  1894. internal = (zend_internal_function *)fptr;
  1895. if (internal->module) {
  1896. reflection_extension_factory(return_value, internal->module->name);
  1897. } else {
  1898. RETURN_NULL();
  1899. }
  1900. }
  1901. /* }}} */
  1902. /* {{{ Returns false or the name of the extension the function belongs to */
  1903. ZEND_METHOD(ReflectionFunctionAbstract, getExtensionName)
  1904. {
  1905. reflection_object *intern;
  1906. zend_function *fptr;
  1907. zend_internal_function *internal;
  1908. GET_REFLECTION_OBJECT_PTR(fptr);
  1909. if (zend_parse_parameters_none() == FAILURE) {
  1910. RETURN_THROWS();
  1911. }
  1912. if (fptr->type != ZEND_INTERNAL_FUNCTION) {
  1913. RETURN_FALSE;
  1914. }
  1915. internal = (zend_internal_function *)fptr;
  1916. if (internal->module) {
  1917. RETURN_STRING(internal->module->name);
  1918. } else {
  1919. RETURN_FALSE;
  1920. }
  1921. }
  1922. /* }}} */
  1923. /* {{{ */
  1924. ZEND_METHOD(ReflectionGenerator, __construct)
  1925. {
  1926. zval *generator, *object;
  1927. reflection_object *intern;
  1928. zend_execute_data *ex;
  1929. object = ZEND_THIS;
  1930. intern = Z_REFLECTION_P(object);
  1931. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &generator, zend_ce_generator) == FAILURE) {
  1932. RETURN_THROWS();
  1933. }
  1934. ex = ((zend_generator *) Z_OBJ_P(generator))->execute_data;
  1935. if (!ex) {
  1936. _DO_THROW("Cannot create ReflectionGenerator based on a terminated Generator");
  1937. RETURN_THROWS();
  1938. }
  1939. if (intern->ce) {
  1940. zval_ptr_dtor(&intern->obj);
  1941. }
  1942. intern->ref_type = REF_TYPE_GENERATOR;
  1943. ZVAL_OBJ_COPY(&intern->obj, Z_OBJ_P(generator));
  1944. intern->ce = zend_ce_generator;
  1945. }
  1946. /* }}} */
  1947. #define REFLECTION_CHECK_VALID_GENERATOR(ex) \
  1948. if (!ex) { \
  1949. _DO_THROW("Cannot fetch information from a terminated Generator"); \
  1950. RETURN_THROWS(); \
  1951. }
  1952. /* {{{ */
  1953. ZEND_METHOD(ReflectionGenerator, getTrace)
  1954. {
  1955. zend_long options = DEBUG_BACKTRACE_PROVIDE_OBJECT;
  1956. zend_generator *generator = (zend_generator *) Z_OBJ(Z_REFLECTION_P(ZEND_THIS)->obj);
  1957. zend_generator *root_generator;
  1958. zend_execute_data *ex_backup = EG(current_execute_data);
  1959. zend_execute_data *ex = generator->execute_data;
  1960. zend_execute_data *root_prev = NULL, *cur_prev;
  1961. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &options) == FAILURE) {
  1962. RETURN_THROWS();
  1963. }
  1964. REFLECTION_CHECK_VALID_GENERATOR(ex)
  1965. root_generator = zend_generator_get_current(generator);
  1966. cur_prev = generator->execute_data->prev_execute_data;
  1967. if (generator == root_generator) {
  1968. generator->execute_data->prev_execute_data = NULL;
  1969. } else {
  1970. root_prev = root_generator->execute_data->prev_execute_data;
  1971. generator->execute_fake.prev_execute_data = NULL;
  1972. root_generator->execute_data->prev_execute_data = &generator->execute_fake;
  1973. }
  1974. EG(current_execute_data) = root_generator->execute_data;
  1975. zend_fetch_debug_backtrace(return_value, 0, options, 0);
  1976. EG(current_execute_data) = ex_backup;
  1977. root_generator->execute_data->prev_execute_data = root_prev;
  1978. generator->execute_data->prev_execute_data = cur_prev;
  1979. }
  1980. /* }}} */
  1981. /* {{{ */
  1982. ZEND_METHOD(ReflectionGenerator, getExecutingLine)
  1983. {
  1984. zend_generator *generator = (zend_generator *) Z_OBJ(Z_REFLECTION_P(ZEND_THIS)->obj);
  1985. zend_execute_data *ex = generator->execute_data;
  1986. if (zend_parse_parameters_none() == FAILURE) {
  1987. RETURN_THROWS();
  1988. }
  1989. REFLECTION_CHECK_VALID_GENERATOR(ex)
  1990. ZVAL_LONG(return_value, ex->opline->lineno);
  1991. }
  1992. /* }}} */
  1993. /* {{{ */
  1994. ZEND_METHOD(ReflectionGenerator, getExecutingFile)
  1995. {
  1996. zend_generator *generator = (zend_generator *) Z_OBJ(Z_REFLECTION_P(ZEND_THIS)->obj);
  1997. zend_execute_data *ex = generator->execute_data;
  1998. if (zend_parse_parameters_none() == FAILURE) {
  1999. RETURN_THROWS();
  2000. }
  2001. REFLECTION_CHECK_VALID_GENERATOR(ex)
  2002. ZVAL_STR_COPY(return_value, ex->func->op_array.filename);
  2003. }
  2004. /* }}} */
  2005. /* {{{ */
  2006. ZEND_METHOD(ReflectionGenerator, getFunction)
  2007. {
  2008. zend_generator *generator = (zend_generator *) Z_OBJ(Z_REFLECTION_P(ZEND_THIS)->obj);
  2009. zend_execute_data *ex = generator->execute_data;
  2010. if (zend_parse_parameters_none() == FAILURE) {
  2011. RETURN_THROWS();
  2012. }
  2013. REFLECTION_CHECK_VALID_GENERATOR(ex)
  2014. if (ex->func->common.fn_flags & ZEND_ACC_CLOSURE) {
  2015. zval closure;
  2016. ZVAL_OBJ(&closure, ZEND_CLOSURE_OBJECT(ex->func));
  2017. reflection_function_factory(ex->func, &closure, return_value);
  2018. } else if (ex->func->op_array.scope) {
  2019. reflection_method_factory(ex->func->op_array.scope, ex->func, NULL, return_value);
  2020. } else {
  2021. reflection_function_factory(ex->func, NULL, return_value);
  2022. }
  2023. }
  2024. /* }}} */
  2025. /* {{{ */
  2026. ZEND_METHOD(ReflectionGenerator, getThis)
  2027. {
  2028. zend_generator *generator = (zend_generator *) Z_OBJ(Z_REFLECTION_P(ZEND_THIS)->obj);
  2029. zend_execute_data *ex = generator->execute_data;
  2030. if (zend_parse_parameters_none() == FAILURE) {
  2031. RETURN_THROWS();
  2032. }
  2033. REFLECTION_CHECK_VALID_GENERATOR(ex)
  2034. if (Z_TYPE(ex->This) == IS_OBJECT) {
  2035. RETURN_OBJ_COPY(Z_OBJ(ex->This));
  2036. } else {
  2037. RETURN_NULL();
  2038. }
  2039. }
  2040. /* }}} */
  2041. /* {{{ */
  2042. ZEND_METHOD(ReflectionGenerator, getExecutingGenerator)
  2043. {
  2044. zend_generator *generator = (zend_generator *) Z_OBJ(Z_REFLECTION_P(ZEND_THIS)->obj);
  2045. zend_execute_data *ex = generator->execute_data;
  2046. zend_generator *current;
  2047. if (zend_parse_parameters_none() == FAILURE) {
  2048. RETURN_THROWS();
  2049. }
  2050. REFLECTION_CHECK_VALID_GENERATOR(ex)
  2051. current = zend_generator_get_current(generator);
  2052. RETURN_OBJ_COPY(&current->std);
  2053. }
  2054. /* }}} */
  2055. /* {{{ Constructor. Throws an Exception in case the given method does not exist */
  2056. ZEND_METHOD(ReflectionParameter, __construct)
  2057. {
  2058. parameter_reference *ref;
  2059. zval *reference;
  2060. zend_string *arg_name = NULL;
  2061. zend_long position;
  2062. zval *object;
  2063. zval *prop_name;
  2064. reflection_object *intern;
  2065. zend_function *fptr;
  2066. struct _zend_arg_info *arg_info;
  2067. uint32_t num_args;
  2068. zend_class_entry *ce = NULL;
  2069. bool is_closure = 0;
  2070. ZEND_PARSE_PARAMETERS_START(2, 2)
  2071. Z_PARAM_ZVAL(reference)
  2072. Z_PARAM_STR_OR_LONG(arg_name, position)
  2073. ZEND_PARSE_PARAMETERS_END();
  2074. object = ZEND_THIS;
  2075. intern = Z_REFLECTION_P(object);
  2076. /* First, find the function */
  2077. switch (Z_TYPE_P(reference)) {
  2078. case IS_STRING:
  2079. {
  2080. zend_string *lcname = zend_string_tolower(Z_STR_P(reference));
  2081. fptr = zend_hash_find_ptr(EG(function_table), lcname);
  2082. zend_string_release(lcname);
  2083. if (!fptr) {
  2084. zend_throw_exception_ex(reflection_exception_ptr, 0,
  2085. "Function %s() does not exist", Z_STRVAL_P(reference));
  2086. RETURN_THROWS();
  2087. }
  2088. ce = fptr->common.scope;
  2089. }
  2090. break;
  2091. case IS_ARRAY: {
  2092. zval *classref;
  2093. zval *method;
  2094. zend_string *name, *lcname;
  2095. if (((classref = zend_hash_index_find(Z_ARRVAL_P(reference), 0)) == NULL)
  2096. || ((method = zend_hash_index_find(Z_ARRVAL_P(reference), 1)) == NULL))
  2097. {
  2098. _DO_THROW("Expected array($object, $method) or array($classname, $method)");
  2099. RETURN_THROWS();
  2100. }
  2101. if (Z_TYPE_P(classref) == IS_OBJECT) {
  2102. ce = Z_OBJCE_P(classref);
  2103. } else {
  2104. name = zval_try_get_string(classref);
  2105. if (UNEXPECTED(!name)) {
  2106. return;
  2107. }
  2108. if ((ce = zend_lookup_class(name)) == NULL) {
  2109. zend_throw_exception_ex(reflection_exception_ptr, 0,
  2110. "Class \"%s\" does not exist", ZSTR_VAL(name));
  2111. zend_string_release(name);
  2112. RETURN_THROWS();
  2113. }
  2114. zend_string_release(name);
  2115. }
  2116. name = zval_try_get_string(method);
  2117. if (UNEXPECTED(!name)) {
  2118. return;
  2119. }
  2120. lcname = zend_string_tolower(name);
  2121. if (Z_TYPE_P(classref) == IS_OBJECT && is_closure_invoke(ce, lcname)
  2122. && (fptr = zend_get_closure_invoke_method(Z_OBJ_P(classref))) != NULL)
  2123. {
  2124. /* nothing to do. don't set is_closure since is the invoke handler,
  2125. not the closure itself */
  2126. } else if ((fptr = zend_hash_find_ptr(&ce->function_table, lcname)) == NULL) {
  2127. zend_throw_exception_ex(reflection_exception_ptr, 0,
  2128. "Method %s::%s() does not exist", ZSTR_VAL(ce->name), ZSTR_VAL(name));
  2129. zend_string_release(name);
  2130. zend_string_release(lcname);
  2131. RETURN_THROWS();
  2132. }
  2133. zend_string_release(name);
  2134. zend_string_release(lcname);
  2135. }
  2136. break;
  2137. case IS_OBJECT: {
  2138. ce = Z_OBJCE_P(reference);
  2139. if (instanceof_function(ce, zend_ce_closure)) {
  2140. fptr = (zend_function *)zend_get_closure_method_def(Z_OBJ_P(reference));
  2141. Z_ADDREF_P(reference);
  2142. is_closure = 1;
  2143. } else if ((fptr = zend_hash_find_ptr(&ce->function_table, ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE))) == NULL) {
  2144. zend_throw_exception_ex(reflection_exception_ptr, 0,
  2145. "Method %s::%s() does not exist", ZSTR_VAL(ce->name), ZEND_INVOKE_FUNC_NAME);
  2146. RETURN_THROWS();
  2147. }
  2148. }
  2149. break;
  2150. default:
  2151. zend_argument_error(reflection_exception_ptr, 1, "must be a string, an array(class, method), or a callable object, %s given", zend_zval_type_name(reference));
  2152. RETURN_THROWS();
  2153. }
  2154. /* Now, search for the parameter */
  2155. arg_info = fptr->common.arg_info;
  2156. num_args = fptr->common.num_args;
  2157. if (fptr->common.fn_flags & ZEND_ACC_VARIADIC) {
  2158. num_args++;
  2159. }
  2160. if (arg_name != NULL) {
  2161. uint32_t i;
  2162. position = -1;
  2163. if (has_internal_arg_info(fptr)) {
  2164. for (i = 0; i < num_args; i++) {
  2165. if (arg_info[i].name) {
  2166. if (strcmp(((zend_internal_arg_info*)arg_info)[i].name, ZSTR_VAL(arg_name)) == 0) {
  2167. position = i;
  2168. break;
  2169. }
  2170. }
  2171. }
  2172. } else {
  2173. for (i = 0; i < num_args; i++) {
  2174. if (arg_info[i].name) {
  2175. if (zend_string_equals(arg_name, arg_info[i].name)) {
  2176. position = i;
  2177. break;
  2178. }
  2179. }
  2180. }
  2181. }
  2182. if (position == -1) {
  2183. _DO_THROW("The parameter specified by its name could not be found");
  2184. goto failure;
  2185. }
  2186. } else {
  2187. if (position < 0) {
  2188. zend_argument_value_error(2, "must be greater than or equal to 0");
  2189. goto failure;
  2190. }
  2191. if (position >= num_args) {
  2192. _DO_THROW("The parameter specified by its offset could not be found");
  2193. goto failure;
  2194. }
  2195. }
  2196. ref = (parameter_reference*) emalloc(sizeof(parameter_reference));
  2197. ref->arg_info = &arg_info[position];
  2198. ref->offset = (uint32_t)position;
  2199. ref->required = (uint32_t)position < fptr->common.required_num_args;
  2200. ref->fptr = fptr;
  2201. /* TODO: copy fptr */
  2202. intern->ptr = ref;
  2203. intern->ref_type = REF_TYPE_PARAMETER;
  2204. intern->ce = ce;
  2205. if (reference && is_closure) {
  2206. ZVAL_COPY_VALUE(&intern->obj, reference);
  2207. }
  2208. prop_name = reflection_prop_name(object);
  2209. if (has_internal_arg_info(fptr)) {
  2210. ZVAL_STRING(prop_name, ((zend_internal_arg_info*)arg_info)[position].name);
  2211. } else {
  2212. ZVAL_STR_COPY(prop_name, arg_info[position].name);
  2213. }
  2214. return;
  2215. failure:
  2216. if (fptr->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
  2217. zend_string_release_ex(fptr->common.function_name, 0);
  2218. zend_free_trampoline(fptr);
  2219. }
  2220. if (is_closure) {
  2221. zval_ptr_dtor(reference);
  2222. }
  2223. RETURN_THROWS();
  2224. }
  2225. /* }}} */
  2226. /* {{{ Returns a string representation */
  2227. ZEND_METHOD(ReflectionParameter, __toString)
  2228. {
  2229. reflection_object *intern;
  2230. parameter_reference *param;
  2231. smart_str str = {0};
  2232. if (zend_parse_parameters_none() == FAILURE) {
  2233. RETURN_THROWS();
  2234. }
  2235. GET_REFLECTION_OBJECT_PTR(param);
  2236. _parameter_string(&str, param->fptr, param->arg_info, param->offset, param->required, "");
  2237. RETURN_STR(smart_str_extract(&str));
  2238. }
  2239. /* }}} */
  2240. /* {{{ Returns this parameters's name */
  2241. ZEND_METHOD(ReflectionParameter, getName)
  2242. {
  2243. reflection_object *intern;
  2244. parameter_reference *param;
  2245. if (zend_parse_parameters_none() == FAILURE) {
  2246. RETURN_THROWS();
  2247. }
  2248. GET_REFLECTION_OBJECT_PTR(param);
  2249. if (has_internal_arg_info(param->fptr)) {
  2250. RETURN_STRING(((zend_internal_arg_info *) param->arg_info)->name);
  2251. } else {
  2252. RETURN_STR_COPY(param->arg_info->name);
  2253. }
  2254. }
  2255. /* }}} */
  2256. /* {{{ Returns the ReflectionFunction for the function of this parameter */
  2257. ZEND_METHOD(ReflectionParameter, getDeclaringFunction)
  2258. {
  2259. reflection_object *intern;
  2260. parameter_reference *param;
  2261. if (zend_parse_parameters_none() == FAILURE) {
  2262. RETURN_THROWS();
  2263. }
  2264. GET_REFLECTION_OBJECT_PTR(param);
  2265. if (!param->fptr->common.scope) {
  2266. reflection_function_factory(_copy_function(param->fptr), Z_ISUNDEF(intern->obj)? NULL : &intern->obj, return_value);
  2267. } else {
  2268. reflection_method_factory(param->fptr->common.scope, _copy_function(param->fptr), Z_ISUNDEF(intern->obj)? NULL : &intern->obj, return_value);
  2269. }
  2270. }
  2271. /* }}} */
  2272. /* {{{ Returns in which class this parameter is defined (not the type of the parameter) */
  2273. ZEND_METHOD(ReflectionParameter, getDeclaringClass)
  2274. {
  2275. reflection_object *intern;
  2276. parameter_reference *param;
  2277. if (zend_parse_parameters_none() == FAILURE) {
  2278. RETURN_THROWS();
  2279. }
  2280. GET_REFLECTION_OBJECT_PTR(param);
  2281. if (param->fptr->common.scope) {
  2282. zend_reflection_class_factory(param->fptr->common.scope, return_value);
  2283. }
  2284. }
  2285. /* }}} */
  2286. /* {{{ Returns this parameters's class hint or NULL if there is none */
  2287. ZEND_METHOD(ReflectionParameter, getClass)
  2288. {
  2289. reflection_object *intern;
  2290. parameter_reference *param;
  2291. zend_class_entry *ce;
  2292. if (zend_parse_parameters_none() == FAILURE) {
  2293. RETURN_THROWS();
  2294. }
  2295. GET_REFLECTION_OBJECT_PTR(param);
  2296. // TODO: This is going to return null for union types, which is rather odd.
  2297. if (ZEND_TYPE_HAS_NAME(param->arg_info->type)) {
  2298. /* Class name is stored as a string, we might also get "self" or "parent"
  2299. * - For "self", simply use the function scope. If scope is NULL then
  2300. * the function is global and thus self does not make any sense
  2301. *
  2302. * - For "parent", use the function scope's parent. If scope is NULL then
  2303. * the function is global and thus parent does not make any sense.
  2304. * If the parent is NULL then the class does not extend anything and
  2305. * thus parent does not make any sense, either.
  2306. *
  2307. * TODO: Think about moving these checks to the compiler or some sort of
  2308. * lint-mode.
  2309. */
  2310. zend_string *class_name;
  2311. class_name = ZEND_TYPE_NAME(param->arg_info->type);
  2312. if (zend_string_equals_literal_ci(class_name, "self")) {
  2313. ce = param->fptr->common.scope;
  2314. if (!ce) {
  2315. zend_throw_exception_ex(reflection_exception_ptr, 0,
  2316. "Parameter uses \"self\" as type but function is not a class member");
  2317. RETURN_THROWS();
  2318. }
  2319. } else if (zend_string_equals_literal_ci(class_name, "parent")) {
  2320. ce = param->fptr->common.scope;
  2321. if (!ce) {
  2322. zend_throw_exception_ex(reflection_exception_ptr, 0,
  2323. "Parameter uses \"parent\" as type but function is not a class member");
  2324. RETURN_THROWS();
  2325. }
  2326. if (!ce->parent) {
  2327. zend_throw_exception_ex(reflection_exception_ptr, 0,
  2328. "Parameter uses \"parent\" as type although class does not have a parent");
  2329. RETURN_THROWS();
  2330. }
  2331. ce = ce->parent;
  2332. } else {
  2333. ce = zend_lookup_class(class_name);
  2334. if (!ce) {
  2335. zend_throw_exception_ex(reflection_exception_ptr, 0,
  2336. "Class \"%s\" does not exist", ZSTR_VAL(class_name));
  2337. RETURN_THROWS();
  2338. }
  2339. }
  2340. zend_reflection_class_factory(ce, return_value);
  2341. }
  2342. }
  2343. /* }}} */
  2344. /* {{{ Returns whether parameter has a type */
  2345. ZEND_METHOD(ReflectionParameter, hasType)
  2346. {
  2347. reflection_object *intern;
  2348. parameter_reference *param;
  2349. if (zend_parse_parameters_none() == FAILURE) {
  2350. RETURN_THROWS();
  2351. }
  2352. GET_REFLECTION_OBJECT_PTR(param);
  2353. RETVAL_BOOL(ZEND_TYPE_IS_SET(param->arg_info->type));
  2354. }
  2355. /* }}} */
  2356. /* {{{ Returns the type associated with the parameter */
  2357. ZEND_METHOD(ReflectionParameter, getType)
  2358. {
  2359. reflection_object *intern;
  2360. parameter_reference *param;
  2361. if (zend_parse_parameters_none() == FAILURE) {
  2362. RETURN_THROWS();
  2363. }
  2364. GET_REFLECTION_OBJECT_PTR(param);
  2365. if (!ZEND_TYPE_IS_SET(param->arg_info->type)) {
  2366. RETURN_NULL();
  2367. }
  2368. reflection_type_factory(param->arg_info->type, return_value, 1);
  2369. }
  2370. /* }}} */
  2371. /* {{{ Returns whether parameter MUST be an array */
  2372. ZEND_METHOD(ReflectionParameter, isArray)
  2373. {
  2374. reflection_object *intern;
  2375. parameter_reference *param;
  2376. uint32_t type_mask;
  2377. if (zend_parse_parameters_none() == FAILURE) {
  2378. RETURN_THROWS();
  2379. }
  2380. GET_REFLECTION_OBJECT_PTR(param);
  2381. type_mask = ZEND_TYPE_PURE_MASK_WITHOUT_NULL(param->arg_info->type);
  2382. RETVAL_BOOL(type_mask == MAY_BE_ARRAY);
  2383. }
  2384. /* }}} */
  2385. /* {{{ Returns whether parameter MUST be callable */
  2386. ZEND_METHOD(ReflectionParameter, isCallable)
  2387. {
  2388. reflection_object *intern;
  2389. parameter_reference *param;
  2390. uint32_t type_mask;
  2391. if (zend_parse_parameters_none() == FAILURE) {
  2392. RETURN_THROWS();
  2393. }
  2394. GET_REFLECTION_OBJECT_PTR(param);
  2395. type_mask = ZEND_TYPE_PURE_MASK_WITHOUT_NULL(param->arg_info->type);
  2396. RETVAL_BOOL(type_mask == MAY_BE_CALLABLE);
  2397. }
  2398. /* }}} */
  2399. /* {{{ Returns whether NULL is allowed as this parameters's value */
  2400. ZEND_METHOD(ReflectionParameter, allowsNull)
  2401. {
  2402. reflection_object *intern;
  2403. parameter_reference *param;
  2404. if (zend_parse_parameters_none() == FAILURE) {
  2405. RETURN_THROWS();
  2406. }
  2407. GET_REFLECTION_OBJECT_PTR(param);
  2408. RETVAL_BOOL(!ZEND_TYPE_IS_SET(param->arg_info->type)
  2409. || ZEND_TYPE_ALLOW_NULL(param->arg_info->type));
  2410. }
  2411. /* }}} */
  2412. /* {{{ Returns whether this parameters is passed to by reference */
  2413. ZEND_METHOD(ReflectionParameter, isPassedByReference)
  2414. {
  2415. reflection_object *intern;
  2416. parameter_reference *param;
  2417. if (zend_parse_parameters_none() == FAILURE) {
  2418. RETURN_THROWS();
  2419. }
  2420. GET_REFLECTION_OBJECT_PTR(param);
  2421. RETVAL_BOOL(ZEND_ARG_SEND_MODE(param->arg_info));
  2422. }
  2423. /* }}} */
  2424. /* {{{ Returns whether this parameter can be passed by value */
  2425. ZEND_METHOD(ReflectionParameter, canBePassedByValue)
  2426. {
  2427. reflection_object *intern;
  2428. parameter_reference *param;
  2429. if (zend_parse_parameters_none() == FAILURE) {
  2430. RETURN_THROWS();
  2431. }
  2432. GET_REFLECTION_OBJECT_PTR(param);
  2433. /* true if it's ZEND_SEND_BY_VAL or ZEND_SEND_PREFER_REF */
  2434. RETVAL_BOOL(ZEND_ARG_SEND_MODE(param->arg_info) != ZEND_SEND_BY_REF);
  2435. }
  2436. /* }}} */
  2437. /* {{{ Get parameter attributes. */
  2438. ZEND_METHOD(ReflectionParameter, getAttributes)
  2439. {
  2440. reflection_object *intern;
  2441. parameter_reference *param;
  2442. GET_REFLECTION_OBJECT_PTR(param);
  2443. HashTable *attributes = param->fptr->common.attributes;
  2444. zend_class_entry *scope = param->fptr->common.scope;
  2445. reflect_attributes(INTERNAL_FUNCTION_PARAM_PASSTHRU,
  2446. attributes, param->offset + 1, scope, ZEND_ATTRIBUTE_TARGET_PARAMETER,
  2447. param->fptr->type == ZEND_USER_FUNCTION ? param->fptr->op_array.filename : NULL);
  2448. }
  2449. /* {{{ Returns whether this parameter is an optional parameter */
  2450. ZEND_METHOD(ReflectionParameter, getPosition)
  2451. {
  2452. reflection_object *intern;
  2453. parameter_reference *param;
  2454. if (zend_parse_parameters_none() == FAILURE) {
  2455. RETURN_THROWS();
  2456. }
  2457. GET_REFLECTION_OBJECT_PTR(param);
  2458. RETVAL_LONG(param->offset);
  2459. }
  2460. /* }}} */
  2461. /* {{{ Returns whether this parameter is an optional parameter */
  2462. ZEND_METHOD(ReflectionParameter, isOptional)
  2463. {
  2464. reflection_object *intern;
  2465. parameter_reference *param;
  2466. if (zend_parse_parameters_none() == FAILURE) {
  2467. RETURN_THROWS();
  2468. }
  2469. GET_REFLECTION_OBJECT_PTR(param);
  2470. RETVAL_BOOL(!param->required);
  2471. }
  2472. /* }}} */
  2473. /* {{{ Returns whether the default value of this parameter is available */
  2474. ZEND_METHOD(ReflectionParameter, isDefaultValueAvailable)
  2475. {
  2476. reflection_object *intern;
  2477. parameter_reference *param;
  2478. if (zend_parse_parameters_none() == FAILURE) {
  2479. RETURN_THROWS();
  2480. }
  2481. GET_REFLECTION_OBJECT_PTR(param);
  2482. if (param->fptr->type == ZEND_INTERNAL_FUNCTION) {
  2483. RETURN_BOOL(!(param->fptr->common.fn_flags & ZEND_ACC_USER_ARG_INFO)
  2484. && ((zend_internal_arg_info*) (param->arg_info))->default_value);
  2485. } else {
  2486. zval *default_value = get_default_from_recv((zend_op_array *)param->fptr, param->offset);
  2487. RETURN_BOOL(default_value != NULL);
  2488. }
  2489. }
  2490. /* }}} */
  2491. /* {{{ Returns the default value of this parameter or throws an exception */
  2492. ZEND_METHOD(ReflectionParameter, getDefaultValue)
  2493. {
  2494. reflection_object *intern;
  2495. parameter_reference *param;
  2496. if (zend_parse_parameters_none() == FAILURE) {
  2497. RETURN_THROWS();
  2498. }
  2499. GET_REFLECTION_OBJECT_PTR(param);
  2500. if (get_parameter_default(return_value, param) == FAILURE) {
  2501. zend_throw_exception_ex(reflection_exception_ptr, 0,
  2502. "Internal error: Failed to retrieve the default value");
  2503. RETURN_THROWS();
  2504. }
  2505. if (Z_TYPE_P(return_value) == IS_CONSTANT_AST) {
  2506. zval_update_constant_ex(return_value, param->fptr->common.scope);
  2507. }
  2508. }
  2509. /* }}} */
  2510. /* {{{ Returns whether the default value of this parameter is constant */
  2511. ZEND_METHOD(ReflectionParameter, isDefaultValueConstant)
  2512. {
  2513. reflection_object *intern;
  2514. parameter_reference *param;
  2515. if (zend_parse_parameters_none() == FAILURE) {
  2516. RETURN_THROWS();
  2517. }
  2518. GET_REFLECTION_OBJECT_PTR(param);
  2519. zval default_value;
  2520. if (get_parameter_default(&default_value, param) == FAILURE) {
  2521. zend_throw_exception_ex(reflection_exception_ptr, 0,
  2522. "Internal error: Failed to retrieve the default value");
  2523. RETURN_THROWS();
  2524. }
  2525. if (Z_TYPE(default_value) == IS_CONSTANT_AST) {
  2526. zend_ast *ast = Z_ASTVAL(default_value);
  2527. RETVAL_BOOL(ast->kind == ZEND_AST_CONSTANT
  2528. || ast->kind == ZEND_AST_CONSTANT_CLASS
  2529. || ast->kind == ZEND_AST_CLASS_CONST);
  2530. } else {
  2531. RETVAL_FALSE;
  2532. }
  2533. zval_ptr_dtor_nogc(&default_value);
  2534. }
  2535. /* }}} */
  2536. /* {{{ Returns the default value's constant name if default value is constant or null */
  2537. ZEND_METHOD(ReflectionParameter, getDefaultValueConstantName)
  2538. {
  2539. reflection_object *intern;
  2540. parameter_reference *param;
  2541. if (zend_parse_parameters_none() == FAILURE) {
  2542. RETURN_THROWS();
  2543. }
  2544. GET_REFLECTION_OBJECT_PTR(param);
  2545. zval default_value;
  2546. if (get_parameter_default(&default_value, param) == FAILURE) {
  2547. zend_throw_exception_ex(reflection_exception_ptr, 0,
  2548. "Internal error: Failed to retrieve the default value");
  2549. RETURN_THROWS();
  2550. }
  2551. if (Z_TYPE(default_value) != IS_CONSTANT_AST) {
  2552. zval_ptr_dtor_nogc(&default_value);
  2553. RETURN_NULL();
  2554. }
  2555. zend_ast *ast = Z_ASTVAL(default_value);
  2556. if (ast->kind == ZEND_AST_CONSTANT) {
  2557. RETVAL_STR_COPY(zend_ast_get_constant_name(ast));
  2558. } else if (ast->kind == ZEND_AST_CONSTANT_CLASS) {
  2559. RETVAL_STRINGL("__CLASS__", sizeof("__CLASS__")-1);
  2560. } else if (ast->kind == ZEND_AST_CLASS_CONST) {
  2561. zend_string *class_name = zend_ast_get_str(ast->child[0]);
  2562. zend_string *const_name = zend_ast_get_str(ast->child[1]);
  2563. RETVAL_NEW_STR(zend_string_concat3(
  2564. ZSTR_VAL(class_name), ZSTR_LEN(class_name),
  2565. "::", sizeof("::")-1,
  2566. ZSTR_VAL(const_name), ZSTR_LEN(const_name)));
  2567. } else {
  2568. RETVAL_NULL();
  2569. }
  2570. zval_ptr_dtor_nogc(&default_value);
  2571. }
  2572. /* {{{ Returns whether this parameter is a variadic parameter */
  2573. ZEND_METHOD(ReflectionParameter, isVariadic)
  2574. {
  2575. reflection_object *intern;
  2576. parameter_reference *param;
  2577. if (zend_parse_parameters_none() == FAILURE) {
  2578. RETURN_THROWS();
  2579. }
  2580. GET_REFLECTION_OBJECT_PTR(param);
  2581. RETVAL_BOOL(ZEND_ARG_IS_VARIADIC(param->arg_info));
  2582. }
  2583. /* }}} */
  2584. /* {{{ Returns this constructor parameter has been promoted to a property */
  2585. ZEND_METHOD(ReflectionParameter, isPromoted)
  2586. {
  2587. reflection_object *intern;
  2588. parameter_reference *param;
  2589. if (zend_parse_parameters_none() == FAILURE) {
  2590. RETURN_THROWS();
  2591. }
  2592. GET_REFLECTION_OBJECT_PTR(param);
  2593. RETVAL_BOOL(ZEND_ARG_IS_PROMOTED(param->arg_info));
  2594. }
  2595. /* }}} */
  2596. /* {{{ Returns whether parameter MAY be null */
  2597. ZEND_METHOD(ReflectionType, allowsNull)
  2598. {
  2599. reflection_object *intern;
  2600. type_reference *param;
  2601. if (zend_parse_parameters_none() == FAILURE) {
  2602. RETURN_THROWS();
  2603. }
  2604. GET_REFLECTION_OBJECT_PTR(param);
  2605. RETVAL_BOOL(ZEND_TYPE_ALLOW_NULL(param->type));
  2606. }
  2607. /* }}} */
  2608. static zend_string *zend_type_to_string_without_null(zend_type type) {
  2609. ZEND_TYPE_FULL_MASK(type) &= ~MAY_BE_NULL;
  2610. return zend_type_to_string(type);
  2611. }
  2612. /* {{{ Return the text of the type hint */
  2613. ZEND_METHOD(ReflectionType, __toString)
  2614. {
  2615. reflection_object *intern;
  2616. type_reference *param;
  2617. if (zend_parse_parameters_none() == FAILURE) {
  2618. RETURN_THROWS();
  2619. }
  2620. GET_REFLECTION_OBJECT_PTR(param);
  2621. RETURN_STR(zend_type_to_string(param->type));
  2622. }
  2623. /* }}} */
  2624. /* {{{ Return the name of the type */
  2625. ZEND_METHOD(ReflectionNamedType, getName)
  2626. {
  2627. reflection_object *intern;
  2628. type_reference *param;
  2629. if (zend_parse_parameters_none() == FAILURE) {
  2630. RETURN_THROWS();
  2631. }
  2632. GET_REFLECTION_OBJECT_PTR(param);
  2633. if (param->legacy_behavior) {
  2634. RETURN_STR(zend_type_to_string_without_null(param->type));
  2635. }
  2636. RETURN_STR(zend_type_to_string(param->type));
  2637. }
  2638. /* }}} */
  2639. /* {{{ Returns whether type is a builtin type */
  2640. ZEND_METHOD(ReflectionNamedType, isBuiltin)
  2641. {
  2642. reflection_object *intern;
  2643. type_reference *param;
  2644. if (zend_parse_parameters_none() == FAILURE) {
  2645. RETURN_THROWS();
  2646. }
  2647. GET_REFLECTION_OBJECT_PTR(param);
  2648. /* Treat "static" as a class type for the purposes of reflection. */
  2649. RETVAL_BOOL(ZEND_TYPE_IS_ONLY_MASK(param->type)
  2650. && !(ZEND_TYPE_FULL_MASK(param->type) & MAY_BE_STATIC));
  2651. }
  2652. /* }}} */
  2653. static void append_type(zval *return_value, zend_type type) {
  2654. zval reflection_type;
  2655. reflection_type_factory(type, &reflection_type, 0);
  2656. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &reflection_type);
  2657. }
  2658. static void append_type_mask(zval *return_value, uint32_t type_mask) {
  2659. append_type(return_value, (zend_type) ZEND_TYPE_INIT_MASK(type_mask));
  2660. }
  2661. /* {{{ Returns the types that are part of this union type */
  2662. ZEND_METHOD(ReflectionUnionType, getTypes)
  2663. {
  2664. reflection_object *intern;
  2665. type_reference *param;
  2666. uint32_t type_mask;
  2667. if (zend_parse_parameters_none() == FAILURE) {
  2668. RETURN_THROWS();
  2669. }
  2670. GET_REFLECTION_OBJECT_PTR(param);
  2671. array_init(return_value);
  2672. if (ZEND_TYPE_HAS_LIST(param->type)) {
  2673. zend_type *list_type;
  2674. ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(param->type), list_type) {
  2675. append_type(return_value, *list_type);
  2676. } ZEND_TYPE_LIST_FOREACH_END();
  2677. } else if (ZEND_TYPE_HAS_NAME(param->type)) {
  2678. zend_string *name = ZEND_TYPE_NAME(param->type);
  2679. append_type(return_value, (zend_type) ZEND_TYPE_INIT_CLASS(name, 0, 0));
  2680. }
  2681. type_mask = ZEND_TYPE_PURE_MASK(param->type);
  2682. ZEND_ASSERT(!(type_mask & MAY_BE_VOID));
  2683. ZEND_ASSERT(!(type_mask & MAY_BE_NEVER));
  2684. if (type_mask & MAY_BE_STATIC) {
  2685. append_type_mask(return_value, MAY_BE_STATIC);
  2686. }
  2687. if (type_mask & MAY_BE_CALLABLE) {
  2688. append_type_mask(return_value, MAY_BE_CALLABLE);
  2689. }
  2690. if (type_mask & MAY_BE_ITERABLE) {
  2691. append_type_mask(return_value, MAY_BE_ITERABLE);
  2692. }
  2693. if (type_mask & MAY_BE_OBJECT) {
  2694. append_type_mask(return_value, MAY_BE_OBJECT);
  2695. }
  2696. if (type_mask & MAY_BE_ARRAY) {
  2697. append_type_mask(return_value, MAY_BE_ARRAY);
  2698. }
  2699. if (type_mask & MAY_BE_STRING) {
  2700. append_type_mask(return_value, MAY_BE_STRING);
  2701. }
  2702. if (type_mask & MAY_BE_LONG) {
  2703. append_type_mask(return_value, MAY_BE_LONG);
  2704. }
  2705. if (type_mask & MAY_BE_DOUBLE) {
  2706. append_type_mask(return_value, MAY_BE_DOUBLE);
  2707. }
  2708. if ((type_mask & MAY_BE_BOOL) == MAY_BE_BOOL) {
  2709. append_type_mask(return_value, MAY_BE_BOOL);
  2710. } else if (type_mask & MAY_BE_FALSE) {
  2711. append_type_mask(return_value, MAY_BE_FALSE);
  2712. }
  2713. if (type_mask & MAY_BE_NULL) {
  2714. append_type_mask(return_value, MAY_BE_NULL);
  2715. }
  2716. }
  2717. /* }}} */
  2718. /* {{{ Returns the types that are part of this intersection type */
  2719. ZEND_METHOD(ReflectionIntersectionType, getTypes)
  2720. {
  2721. reflection_object *intern;
  2722. type_reference *param;
  2723. zend_type *list_type;
  2724. if (zend_parse_parameters_none() == FAILURE) {
  2725. RETURN_THROWS();
  2726. }
  2727. GET_REFLECTION_OBJECT_PTR(param);
  2728. ZEND_ASSERT(ZEND_TYPE_HAS_LIST(param->type));
  2729. array_init(return_value);
  2730. ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(param->type), list_type) {
  2731. append_type(return_value, *list_type);
  2732. } ZEND_TYPE_LIST_FOREACH_END();
  2733. }
  2734. /* }}} */
  2735. /* {{{ Constructor. Throws an Exception in case the given method does not exist */
  2736. ZEND_METHOD(ReflectionMethod, __construct)
  2737. {
  2738. zend_object *arg1_obj;
  2739. zend_string *arg1_str;
  2740. zend_string *arg2_str = NULL;
  2741. zend_object *orig_obj = NULL;
  2742. zend_class_entry *ce = NULL;
  2743. zend_string *class_name = NULL;
  2744. char *method_name;
  2745. size_t method_name_len;
  2746. char *lcname;
  2747. zval *object;
  2748. reflection_object *intern;
  2749. zend_function *mptr;
  2750. ZEND_PARSE_PARAMETERS_START(1, 2)
  2751. Z_PARAM_OBJ_OR_STR(arg1_obj, arg1_str)
  2752. Z_PARAM_OPTIONAL
  2753. Z_PARAM_STR_OR_NULL(arg2_str)
  2754. ZEND_PARSE_PARAMETERS_END();
  2755. if (arg1_obj) {
  2756. if (!arg2_str) {
  2757. zend_argument_value_error(2, "cannot be null when argument #1 ($objectOrMethod) is an object");
  2758. RETURN_THROWS();
  2759. }
  2760. orig_obj = arg1_obj;
  2761. ce = arg1_obj->ce;
  2762. method_name = ZSTR_VAL(arg2_str);
  2763. method_name_len = ZSTR_LEN(arg2_str);
  2764. } else if (arg2_str) {
  2765. class_name = zend_string_copy(arg1_str);
  2766. method_name = ZSTR_VAL(arg2_str);
  2767. method_name_len = ZSTR_LEN(arg2_str);
  2768. } else {
  2769. char *tmp;
  2770. size_t tmp_len;
  2771. char *name = ZSTR_VAL(arg1_str);
  2772. if ((tmp = strstr(name, "::")) == NULL) {
  2773. zend_argument_error(reflection_exception_ptr, 1, "must be a valid method name");
  2774. RETURN_THROWS();
  2775. }
  2776. tmp_len = tmp - name;
  2777. class_name = zend_string_init(name, tmp_len, 0);
  2778. method_name = tmp + 2;
  2779. method_name_len = ZSTR_LEN(arg1_str) - tmp_len - 2;
  2780. }
  2781. if (class_name) {
  2782. if ((ce = zend_lookup_class(class_name)) == NULL) {
  2783. if (!EG(exception)) {
  2784. zend_throw_exception_ex(reflection_exception_ptr, 0, "Class \"%s\" does not exist", ZSTR_VAL(class_name));
  2785. }
  2786. zend_string_release(class_name);
  2787. RETURN_THROWS();
  2788. }
  2789. zend_string_release(class_name);
  2790. }
  2791. object = ZEND_THIS;
  2792. intern = Z_REFLECTION_P(object);
  2793. lcname = zend_str_tolower_dup(method_name, method_name_len);
  2794. if (ce == zend_ce_closure && orig_obj && (method_name_len == sizeof(ZEND_INVOKE_FUNC_NAME)-1)
  2795. && memcmp(lcname, ZEND_INVOKE_FUNC_NAME, sizeof(ZEND_INVOKE_FUNC_NAME)-1) == 0
  2796. && (mptr = zend_get_closure_invoke_method(orig_obj)) != NULL)
  2797. {
  2798. /* do nothing, mptr already set */
  2799. } else if ((mptr = zend_hash_str_find_ptr(&ce->function_table, lcname, method_name_len)) == NULL) {
  2800. efree(lcname);
  2801. zend_throw_exception_ex(reflection_exception_ptr, 0,
  2802. "Method %s::%s() does not exist", ZSTR_VAL(ce->name), method_name);
  2803. RETURN_THROWS();
  2804. }
  2805. efree(lcname);
  2806. ZVAL_STR_COPY(reflection_prop_name(object), mptr->common.function_name);
  2807. ZVAL_STR_COPY(reflection_prop_class(object), mptr->common.scope->name);
  2808. intern->ptr = mptr;
  2809. intern->ref_type = REF_TYPE_FUNCTION;
  2810. intern->ce = ce;
  2811. }
  2812. /* }}} */
  2813. /* {{{ Returns a string representation */
  2814. ZEND_METHOD(ReflectionMethod, __toString)
  2815. {
  2816. reflection_object *intern;
  2817. zend_function *mptr;
  2818. smart_str str = {0};
  2819. if (zend_parse_parameters_none() == FAILURE) {
  2820. RETURN_THROWS();
  2821. }
  2822. GET_REFLECTION_OBJECT_PTR(mptr);
  2823. _function_string(&str, mptr, intern->ce, "");
  2824. RETURN_STR(smart_str_extract(&str));
  2825. }
  2826. /* }}} */
  2827. /* {{{ Invokes the function */
  2828. ZEND_METHOD(ReflectionMethod, getClosure)
  2829. {
  2830. reflection_object *intern;
  2831. zval *obj = NULL;
  2832. zend_function *mptr;
  2833. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|o!", &obj) == FAILURE) {
  2834. RETURN_THROWS();
  2835. }
  2836. GET_REFLECTION_OBJECT_PTR(mptr);
  2837. if (mptr->common.fn_flags & ZEND_ACC_STATIC) {
  2838. zend_create_fake_closure(return_value, mptr, mptr->common.scope, mptr->common.scope, NULL);
  2839. } else {
  2840. if (!obj) {
  2841. zend_argument_value_error(1, "cannot be null for non-static methods");
  2842. RETURN_THROWS();
  2843. }
  2844. if (!instanceof_function(Z_OBJCE_P(obj), mptr->common.scope)) {
  2845. _DO_THROW("Given object is not an instance of the class this method was declared in");
  2846. RETURN_THROWS();
  2847. }
  2848. /* This is an original closure object and __invoke is to be called. */
  2849. if (Z_OBJCE_P(obj) == zend_ce_closure &&
  2850. (mptr->internal_function.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE))
  2851. {
  2852. RETURN_OBJ_COPY(Z_OBJ_P(obj));
  2853. } else {
  2854. zend_create_fake_closure(return_value, mptr, mptr->common.scope, Z_OBJCE_P(obj), obj);
  2855. }
  2856. }
  2857. }
  2858. /* }}} */
  2859. /* {{{ reflection_method_invoke */
  2860. static void reflection_method_invoke(INTERNAL_FUNCTION_PARAMETERS, int variadic)
  2861. {
  2862. zval retval;
  2863. zval *params = NULL, *object;
  2864. HashTable *named_params = NULL;
  2865. reflection_object *intern;
  2866. zend_function *mptr;
  2867. int argc = 0, result;
  2868. zend_fcall_info fci;
  2869. zend_fcall_info_cache fcc;
  2870. zend_class_entry *obj_ce;
  2871. GET_REFLECTION_OBJECT_PTR(mptr);
  2872. if (mptr->common.fn_flags & ZEND_ACC_ABSTRACT) {
  2873. zend_throw_exception_ex(reflection_exception_ptr, 0,
  2874. "Trying to invoke abstract method %s::%s()",
  2875. ZSTR_VAL(mptr->common.scope->name), ZSTR_VAL(mptr->common.function_name));
  2876. RETURN_THROWS();
  2877. }
  2878. if (variadic) {
  2879. ZEND_PARSE_PARAMETERS_START(1, -1)
  2880. Z_PARAM_OBJECT_OR_NULL(object)
  2881. Z_PARAM_VARIADIC_WITH_NAMED(params, argc, named_params)
  2882. ZEND_PARSE_PARAMETERS_END();
  2883. } else {
  2884. if (zend_parse_parameters(ZEND_NUM_ARGS(), "o!h", &object, &named_params) == FAILURE) {
  2885. RETURN_THROWS();
  2886. }
  2887. }
  2888. /* In case this is a static method, we shouldn't pass an object_ptr
  2889. * (which is used as calling context aka $this). We can thus ignore the
  2890. * first parameter.
  2891. *
  2892. * Else, we verify that the given object is an instance of the class.
  2893. */
  2894. if (mptr->common.fn_flags & ZEND_ACC_STATIC) {
  2895. object = NULL;
  2896. obj_ce = mptr->common.scope;
  2897. } else {
  2898. if (!object) {
  2899. zend_throw_exception_ex(reflection_exception_ptr, 0,
  2900. "Trying to invoke non static method %s::%s() without an object",
  2901. ZSTR_VAL(mptr->common.scope->name), ZSTR_VAL(mptr->common.function_name));
  2902. RETURN_THROWS();
  2903. }
  2904. obj_ce = Z_OBJCE_P(object);
  2905. if (!instanceof_function(obj_ce, mptr->common.scope)) {
  2906. if (!variadic) {
  2907. efree(params);
  2908. }
  2909. _DO_THROW("Given object is not an instance of the class this method was declared in");
  2910. RETURN_THROWS();
  2911. }
  2912. }
  2913. fci.size = sizeof(fci);
  2914. ZVAL_UNDEF(&fci.function_name);
  2915. fci.object = object ? Z_OBJ_P(object) : NULL;
  2916. fci.retval = &retval;
  2917. fci.param_count = argc;
  2918. fci.params = params;
  2919. fci.named_params = named_params;
  2920. fcc.function_handler = mptr;
  2921. fcc.called_scope = intern->ce;
  2922. fcc.object = object ? Z_OBJ_P(object) : NULL;
  2923. /*
  2924. * Copy the zend_function when calling via handler (e.g. Closure::__invoke())
  2925. */
  2926. if ((mptr->internal_function.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
  2927. fcc.function_handler = _copy_function(mptr);
  2928. }
  2929. result = zend_call_function(&fci, &fcc);
  2930. if (result == FAILURE) {
  2931. zend_throw_exception_ex(reflection_exception_ptr, 0,
  2932. "Invocation of method %s::%s() failed", ZSTR_VAL(mptr->common.scope->name), ZSTR_VAL(mptr->common.function_name));
  2933. RETURN_THROWS();
  2934. }
  2935. if (Z_TYPE(retval) != IS_UNDEF) {
  2936. if (Z_ISREF(retval)) {
  2937. zend_unwrap_reference(&retval);
  2938. }
  2939. ZVAL_COPY_VALUE(return_value, &retval);
  2940. }
  2941. }
  2942. /* }}} */
  2943. /* {{{ Invokes the method. */
  2944. ZEND_METHOD(ReflectionMethod, invoke)
  2945. {
  2946. reflection_method_invoke(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  2947. }
  2948. /* }}} */
  2949. /* {{{ Invokes the function and pass its arguments as array. */
  2950. ZEND_METHOD(ReflectionMethod, invokeArgs)
  2951. {
  2952. reflection_method_invoke(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  2953. }
  2954. /* }}} */
  2955. /* {{{ Returns whether this method is final */
  2956. ZEND_METHOD(ReflectionMethod, isFinal)
  2957. {
  2958. _function_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_FINAL);
  2959. }
  2960. /* }}} */
  2961. /* {{{ Returns whether this method is abstract */
  2962. ZEND_METHOD(ReflectionMethod, isAbstract)
  2963. {
  2964. _function_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_ABSTRACT);
  2965. }
  2966. /* }}} */
  2967. /* {{{ Returns whether this method is public */
  2968. ZEND_METHOD(ReflectionMethod, isPublic)
  2969. {
  2970. _function_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PUBLIC);
  2971. }
  2972. /* }}} */
  2973. /* {{{ Returns whether this method is private */
  2974. ZEND_METHOD(ReflectionMethod, isPrivate)
  2975. {
  2976. _function_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PRIVATE);
  2977. }
  2978. /* }}} */
  2979. /* {{{ Returns whether this method is protected */
  2980. ZEND_METHOD(ReflectionMethod, isProtected)
  2981. {
  2982. _function_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PROTECTED);
  2983. }
  2984. /* }}} */
  2985. /* {{{ Returns whether this function is deprecated */
  2986. ZEND_METHOD(ReflectionFunctionAbstract, isDeprecated)
  2987. {
  2988. _function_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_DEPRECATED);
  2989. }
  2990. /* }}} */
  2991. /* {{{ Returns whether this function is a generator */
  2992. ZEND_METHOD(ReflectionFunctionAbstract, isGenerator)
  2993. {
  2994. _function_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_GENERATOR);
  2995. }
  2996. /* }}} */
  2997. /* {{{ Returns whether this function is variadic */
  2998. ZEND_METHOD(ReflectionFunctionAbstract, isVariadic)
  2999. {
  3000. _function_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_VARIADIC);
  3001. }
  3002. /* }}} */
  3003. /* {{{ Returns whether this function is static */
  3004. ZEND_METHOD(ReflectionFunctionAbstract, isStatic)
  3005. {
  3006. _function_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_STATIC);
  3007. }
  3008. /* }}} */
  3009. /* {{{ Returns whether this function is defined in namespace */
  3010. ZEND_METHOD(ReflectionFunctionAbstract, inNamespace)
  3011. {
  3012. reflection_object *intern;
  3013. zend_function *fptr;
  3014. if (zend_parse_parameters_none() == FAILURE) {
  3015. RETURN_THROWS();
  3016. }
  3017. GET_REFLECTION_OBJECT_PTR(fptr);
  3018. zend_string *name = fptr->common.function_name;
  3019. const char *backslash = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
  3020. RETURN_BOOL(backslash && backslash > ZSTR_VAL(name));
  3021. }
  3022. /* }}} */
  3023. /* {{{ Returns the name of namespace where this function is defined */
  3024. ZEND_METHOD(ReflectionFunctionAbstract, getNamespaceName)
  3025. {
  3026. reflection_object *intern;
  3027. zend_function *fptr;
  3028. if (zend_parse_parameters_none() == FAILURE) {
  3029. RETURN_THROWS();
  3030. }
  3031. GET_REFLECTION_OBJECT_PTR(fptr);
  3032. zend_string *name = fptr->common.function_name;
  3033. const char *backslash = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
  3034. if (backslash && backslash > ZSTR_VAL(name)) {
  3035. RETURN_STRINGL(ZSTR_VAL(name), backslash - ZSTR_VAL(name));
  3036. }
  3037. RETURN_EMPTY_STRING();
  3038. }
  3039. /* }}} */
  3040. /* {{{ Returns the short name of the function (without namespace part) */
  3041. ZEND_METHOD(ReflectionFunctionAbstract, getShortName)
  3042. {
  3043. reflection_object *intern;
  3044. zend_function *fptr;
  3045. if (zend_parse_parameters_none() == FAILURE) {
  3046. RETURN_THROWS();
  3047. }
  3048. GET_REFLECTION_OBJECT_PTR(fptr);
  3049. zend_string *name = fptr->common.function_name;
  3050. const char *backslash = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
  3051. if (backslash && backslash > ZSTR_VAL(name)) {
  3052. RETURN_STRINGL(backslash + 1, ZSTR_LEN(name) - (backslash - ZSTR_VAL(name) + 1));
  3053. }
  3054. RETURN_STR_COPY(name);
  3055. }
  3056. /* }}} */
  3057. /* {{{ Return whether the function has a return type */
  3058. ZEND_METHOD(ReflectionFunctionAbstract, hasReturnType)
  3059. {
  3060. reflection_object *intern;
  3061. zend_function *fptr;
  3062. if (zend_parse_parameters_none() == FAILURE) {
  3063. RETURN_THROWS();
  3064. }
  3065. GET_REFLECTION_OBJECT_PTR(fptr);
  3066. RETVAL_BOOL((fptr->op_array.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) && !ZEND_ARG_TYPE_IS_TENTATIVE(&fptr->common.arg_info[-1]));
  3067. }
  3068. /* }}} */
  3069. /* {{{ Returns the return type associated with the function */
  3070. ZEND_METHOD(ReflectionFunctionAbstract, getReturnType)
  3071. {
  3072. reflection_object *intern;
  3073. zend_function *fptr;
  3074. if (zend_parse_parameters_none() == FAILURE) {
  3075. RETURN_THROWS();
  3076. }
  3077. GET_REFLECTION_OBJECT_PTR(fptr);
  3078. if (!(fptr->op_array.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) || ZEND_ARG_TYPE_IS_TENTATIVE(&fptr->common.arg_info[-1])) {
  3079. RETURN_NULL();
  3080. }
  3081. reflection_type_factory(fptr->common.arg_info[-1].type, return_value, 1);
  3082. }
  3083. /* }}} */
  3084. /* {{{ Return whether the function has a return type */
  3085. ZEND_METHOD(ReflectionFunctionAbstract, hasTentativeReturnType)
  3086. {
  3087. reflection_object *intern;
  3088. zend_function *fptr;
  3089. if (zend_parse_parameters_none() == FAILURE) {
  3090. RETURN_THROWS();
  3091. }
  3092. GET_REFLECTION_OBJECT_PTR(fptr);
  3093. RETVAL_BOOL(fptr->op_array.fn_flags & ZEND_ACC_HAS_RETURN_TYPE && ZEND_ARG_TYPE_IS_TENTATIVE(&fptr->common.arg_info[-1]));
  3094. }
  3095. /* }}} */
  3096. /* {{{ Returns the return type associated with the function */
  3097. ZEND_METHOD(ReflectionFunctionAbstract, getTentativeReturnType)
  3098. {
  3099. reflection_object *intern;
  3100. zend_function *fptr;
  3101. if (zend_parse_parameters_none() == FAILURE) {
  3102. RETURN_THROWS();
  3103. }
  3104. GET_REFLECTION_OBJECT_PTR(fptr);
  3105. if (!(fptr->op_array.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) || !ZEND_ARG_TYPE_IS_TENTATIVE(&fptr->common.arg_info[-1])) {
  3106. RETURN_NULL();
  3107. }
  3108. reflection_type_factory(fptr->common.arg_info[-1].type, return_value, 1);
  3109. }
  3110. /* }}} */
  3111. /* {{{ Returns whether this method is the constructor */
  3112. ZEND_METHOD(ReflectionMethod, isConstructor)
  3113. {
  3114. reflection_object *intern;
  3115. zend_function *mptr;
  3116. if (zend_parse_parameters_none() == FAILURE) {
  3117. RETURN_THROWS();
  3118. }
  3119. GET_REFLECTION_OBJECT_PTR(mptr);
  3120. /* we need to check if the ctor is the ctor of the class level we we
  3121. * looking at since we might be looking at an inherited old style ctor
  3122. * defined in base class. */
  3123. RETURN_BOOL((mptr->common.fn_flags & ZEND_ACC_CTOR) && intern->ce->constructor && intern->ce->constructor->common.scope == mptr->common.scope);
  3124. }
  3125. /* }}} */
  3126. /* {{{ Returns whether this method is a destructor */
  3127. ZEND_METHOD(ReflectionMethod, isDestructor)
  3128. {
  3129. reflection_object *intern;
  3130. zend_function *mptr;
  3131. if (zend_parse_parameters_none() == FAILURE) {
  3132. RETURN_THROWS();
  3133. }
  3134. GET_REFLECTION_OBJECT_PTR(mptr);
  3135. RETURN_BOOL(zend_string_equals_literal_ci(
  3136. mptr->common.function_name, ZEND_DESTRUCTOR_FUNC_NAME));
  3137. }
  3138. /* }}} */
  3139. /* {{{ Returns a bitfield of the access modifiers for this method */
  3140. ZEND_METHOD(ReflectionMethod, getModifiers)
  3141. {
  3142. reflection_object *intern;
  3143. zend_function *mptr;
  3144. uint32_t keep_flags = ZEND_ACC_PPP_MASK
  3145. | ZEND_ACC_STATIC | ZEND_ACC_ABSTRACT | ZEND_ACC_FINAL;
  3146. if (zend_parse_parameters_none() == FAILURE) {
  3147. RETURN_THROWS();
  3148. }
  3149. GET_REFLECTION_OBJECT_PTR(mptr);
  3150. RETURN_LONG((mptr->common.fn_flags & keep_flags));
  3151. }
  3152. /* }}} */
  3153. /* {{{ Get the declaring class */
  3154. ZEND_METHOD(ReflectionMethod, getDeclaringClass)
  3155. {
  3156. reflection_object *intern;
  3157. zend_function *mptr;
  3158. GET_REFLECTION_OBJECT_PTR(mptr);
  3159. if (zend_parse_parameters_none() == FAILURE) {
  3160. RETURN_THROWS();
  3161. }
  3162. zend_reflection_class_factory(mptr->common.scope, return_value);
  3163. }
  3164. /* }}} */
  3165. /* {{{ Get the prototype */
  3166. ZEND_METHOD(ReflectionMethod, getPrototype)
  3167. {
  3168. reflection_object *intern;
  3169. zend_function *mptr;
  3170. GET_REFLECTION_OBJECT_PTR(mptr);
  3171. if (zend_parse_parameters_none() == FAILURE) {
  3172. RETURN_THROWS();
  3173. }
  3174. if (!mptr->common.prototype) {
  3175. zend_throw_exception_ex(reflection_exception_ptr, 0,
  3176. "Method %s::%s does not have a prototype", ZSTR_VAL(intern->ce->name), ZSTR_VAL(mptr->common.function_name));
  3177. RETURN_THROWS();
  3178. }
  3179. reflection_method_factory(mptr->common.prototype->common.scope, mptr->common.prototype, NULL, return_value);
  3180. }
  3181. /* }}} */
  3182. /* {{{ Sets whether non-public methods can be invoked */
  3183. ZEND_METHOD(ReflectionMethod, setAccessible)
  3184. {
  3185. bool visible;
  3186. if (zend_parse_parameters(ZEND_NUM_ARGS(), "b", &visible) == FAILURE) {
  3187. RETURN_THROWS();
  3188. }
  3189. }
  3190. /* }}} */
  3191. /* {{{ Constructor. Throws an Exception in case the given class constant does not exist */
  3192. ZEND_METHOD(ReflectionClassConstant, __construct)
  3193. {
  3194. zval *object;
  3195. zend_string *classname_str;
  3196. zend_object *classname_obj;
  3197. zend_string *constname;
  3198. reflection_object *intern;
  3199. zend_class_entry *ce;
  3200. zend_class_constant *constant = NULL;
  3201. ZEND_PARSE_PARAMETERS_START(2, 2)
  3202. Z_PARAM_OBJ_OR_STR(classname_obj, classname_str)
  3203. Z_PARAM_STR(constname)
  3204. ZEND_PARSE_PARAMETERS_END();
  3205. if (classname_obj) {
  3206. ce = classname_obj->ce;
  3207. } else {
  3208. if ((ce = zend_lookup_class(classname_str)) == NULL) {
  3209. zend_throw_exception_ex(reflection_exception_ptr, 0, "Class \"%s\" does not exist", ZSTR_VAL(classname_str));
  3210. RETURN_THROWS();
  3211. }
  3212. }
  3213. object = ZEND_THIS;
  3214. intern = Z_REFLECTION_P(object);
  3215. if ((constant = zend_hash_find_ptr(CE_CONSTANTS_TABLE(ce), constname)) == NULL) {
  3216. zend_throw_exception_ex(reflection_exception_ptr, 0, "Constant %s::%s does not exist", ZSTR_VAL(ce->name), ZSTR_VAL(constname));
  3217. RETURN_THROWS();
  3218. }
  3219. intern->ptr = constant;
  3220. intern->ref_type = REF_TYPE_CLASS_CONSTANT;
  3221. intern->ce = constant->ce;
  3222. ZVAL_STR_COPY(reflection_prop_name(object), constname);
  3223. ZVAL_STR_COPY(reflection_prop_class(object), constant->ce->name);
  3224. }
  3225. /* }}} */
  3226. /* {{{ Returns a string representation */
  3227. ZEND_METHOD(ReflectionClassConstant, __toString)
  3228. {
  3229. reflection_object *intern;
  3230. zend_class_constant *ref;
  3231. smart_str str = {0};
  3232. if (zend_parse_parameters_none() == FAILURE) {
  3233. RETURN_THROWS();
  3234. }
  3235. GET_REFLECTION_OBJECT_PTR(ref);
  3236. zval *name = reflection_prop_name(ZEND_THIS);
  3237. if (Z_ISUNDEF_P(name)) {
  3238. zend_throw_error(NULL,
  3239. "Typed property ReflectionClassConstant::$name "
  3240. "must not be accessed before initialization");
  3241. RETURN_THROWS();
  3242. }
  3243. ZVAL_DEREF(name);
  3244. ZEND_ASSERT(Z_TYPE_P(name) == IS_STRING);
  3245. _class_const_string(&str, Z_STRVAL_P(name), ref, "");
  3246. RETURN_STR(smart_str_extract(&str));
  3247. }
  3248. /* }}} */
  3249. /* {{{ Returns the constant' name */
  3250. ZEND_METHOD(ReflectionClassConstant, getName)
  3251. {
  3252. if (zend_parse_parameters_none() == FAILURE) {
  3253. RETURN_THROWS();
  3254. }
  3255. zval *name = reflection_prop_name(ZEND_THIS);
  3256. if (Z_ISUNDEF_P(name)) {
  3257. zend_throw_error(NULL,
  3258. "Typed property ReflectionClassConstant::$name "
  3259. "must not be accessed before initialization");
  3260. RETURN_THROWS();
  3261. }
  3262. RETURN_COPY_DEREF(name);
  3263. }
  3264. /* }}} */
  3265. static void _class_constant_check_flag(INTERNAL_FUNCTION_PARAMETERS, int mask) /* {{{ */
  3266. {
  3267. reflection_object *intern;
  3268. zend_class_constant *ref;
  3269. if (zend_parse_parameters_none() == FAILURE) {
  3270. RETURN_THROWS();
  3271. }
  3272. GET_REFLECTION_OBJECT_PTR(ref);
  3273. RETURN_BOOL(ZEND_CLASS_CONST_FLAGS(ref) & mask);
  3274. }
  3275. /* }}} */
  3276. /* {{{ Returns whether this constant is public */
  3277. ZEND_METHOD(ReflectionClassConstant, isPublic)
  3278. {
  3279. _class_constant_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PUBLIC);
  3280. }
  3281. /* }}} */
  3282. /* {{{ Returns whether this constant is private */
  3283. ZEND_METHOD(ReflectionClassConstant, isPrivate)
  3284. {
  3285. _class_constant_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PRIVATE);
  3286. }
  3287. /* }}} */
  3288. /* {{{ Returns whether this constant is protected */
  3289. ZEND_METHOD(ReflectionClassConstant, isProtected)
  3290. {
  3291. _class_constant_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PROTECTED);
  3292. }
  3293. /* }}} */
  3294. /* Returns whether this constant is final */
  3295. ZEND_METHOD(ReflectionClassConstant, isFinal)
  3296. {
  3297. _class_constant_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_FINAL);
  3298. }
  3299. /* {{{ Returns a bitfield of the access modifiers for this constant */
  3300. ZEND_METHOD(ReflectionClassConstant, getModifiers)
  3301. {
  3302. reflection_object *intern;
  3303. zend_class_constant *ref;
  3304. uint32_t keep_flags = ZEND_ACC_FINAL | ZEND_ACC_PPP_MASK;
  3305. if (zend_parse_parameters_none() == FAILURE) {
  3306. RETURN_THROWS();
  3307. }
  3308. GET_REFLECTION_OBJECT_PTR(ref);
  3309. RETURN_LONG(ZEND_CLASS_CONST_FLAGS(ref) & keep_flags);
  3310. }
  3311. /* }}} */
  3312. /* {{{ Returns this constant's value */
  3313. ZEND_METHOD(ReflectionClassConstant, getValue)
  3314. {
  3315. reflection_object *intern;
  3316. zend_class_constant *ref;
  3317. if (zend_parse_parameters_none() == FAILURE) {
  3318. RETURN_THROWS();
  3319. }
  3320. GET_REFLECTION_OBJECT_PTR(ref);
  3321. if (Z_TYPE(ref->value) == IS_CONSTANT_AST) {
  3322. zval_update_constant_ex(&ref->value, ref->ce);
  3323. }
  3324. ZVAL_COPY_OR_DUP(return_value, &ref->value);
  3325. }
  3326. /* }}} */
  3327. /* {{{ Get the declaring class */
  3328. ZEND_METHOD(ReflectionClassConstant, getDeclaringClass)
  3329. {
  3330. reflection_object *intern;
  3331. zend_class_constant *ref;
  3332. if (zend_parse_parameters_none() == FAILURE) {
  3333. RETURN_THROWS();
  3334. }
  3335. GET_REFLECTION_OBJECT_PTR(ref);
  3336. zend_reflection_class_factory(ref->ce, return_value);
  3337. }
  3338. /* }}} */
  3339. /* {{{ Returns the doc comment for this constant */
  3340. ZEND_METHOD(ReflectionClassConstant, getDocComment)
  3341. {
  3342. reflection_object *intern;
  3343. zend_class_constant *ref;
  3344. if (zend_parse_parameters_none() == FAILURE) {
  3345. RETURN_THROWS();
  3346. }
  3347. GET_REFLECTION_OBJECT_PTR(ref);
  3348. if (ref->doc_comment) {
  3349. RETURN_STR_COPY(ref->doc_comment);
  3350. }
  3351. RETURN_FALSE;
  3352. }
  3353. /* }}} */
  3354. /* {{{ Returns the attributes of this constant */
  3355. ZEND_METHOD(ReflectionClassConstant, getAttributes)
  3356. {
  3357. reflection_object *intern;
  3358. zend_class_constant *ref;
  3359. GET_REFLECTION_OBJECT_PTR(ref);
  3360. reflect_attributes(INTERNAL_FUNCTION_PARAM_PASSTHRU,
  3361. ref->attributes, 0, ref->ce, ZEND_ATTRIBUTE_TARGET_CLASS_CONST,
  3362. ref->ce->type == ZEND_USER_CLASS ? ref->ce->info.user.filename : NULL);
  3363. }
  3364. /* }}} */
  3365. ZEND_METHOD(ReflectionClassConstant, isEnumCase)
  3366. {
  3367. reflection_object *intern;
  3368. zend_class_constant *ref;
  3369. GET_REFLECTION_OBJECT_PTR(ref);
  3370. RETURN_BOOL(ZEND_CLASS_CONST_FLAGS(ref) & ZEND_CLASS_CONST_IS_CASE);
  3371. }
  3372. /* {{{ reflection_class_object_ctor */
  3373. static void reflection_class_object_ctor(INTERNAL_FUNCTION_PARAMETERS, int is_object)
  3374. {
  3375. zval *object;
  3376. zend_string *arg_class = NULL;
  3377. zend_object *arg_obj;
  3378. reflection_object *intern;
  3379. zend_class_entry *ce;
  3380. if (is_object) {
  3381. ZEND_PARSE_PARAMETERS_START(1, 1)
  3382. Z_PARAM_OBJ(arg_obj)
  3383. ZEND_PARSE_PARAMETERS_END();
  3384. } else {
  3385. ZEND_PARSE_PARAMETERS_START(1, 1)
  3386. Z_PARAM_OBJ_OR_STR(arg_obj, arg_class)
  3387. ZEND_PARSE_PARAMETERS_END();
  3388. }
  3389. object = ZEND_THIS;
  3390. intern = Z_REFLECTION_P(object);
  3391. if (arg_obj) {
  3392. ZVAL_STR_COPY(reflection_prop_name(object), arg_obj->ce->name);
  3393. intern->ptr = arg_obj->ce;
  3394. if (is_object) {
  3395. ZVAL_OBJ_COPY(&intern->obj, arg_obj);
  3396. }
  3397. } else {
  3398. if ((ce = zend_lookup_class(arg_class)) == NULL) {
  3399. if (!EG(exception)) {
  3400. zend_throw_exception_ex(reflection_exception_ptr, -1, "Class \"%s\" does not exist", ZSTR_VAL(arg_class));
  3401. }
  3402. RETURN_THROWS();
  3403. }
  3404. ZVAL_STR_COPY(reflection_prop_name(object), ce->name);
  3405. intern->ptr = ce;
  3406. }
  3407. intern->ref_type = REF_TYPE_OTHER;
  3408. }
  3409. /* }}} */
  3410. /* {{{ Constructor. Takes a string or an instance as an argument */
  3411. ZEND_METHOD(ReflectionClass, __construct)
  3412. {
  3413. reflection_class_object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  3414. }
  3415. /* }}} */
  3416. /* {{{ add_class_vars */
  3417. static void add_class_vars(zend_class_entry *ce, bool statics, zval *return_value)
  3418. {
  3419. zend_property_info *prop_info;
  3420. zval *prop, prop_copy;
  3421. zend_string *key;
  3422. ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->properties_info, key, prop_info) {
  3423. if (((prop_info->flags & ZEND_ACC_PRIVATE) &&
  3424. prop_info->ce != ce)) {
  3425. continue;
  3426. }
  3427. bool is_static = (prop_info->flags & ZEND_ACC_STATIC) != 0;
  3428. if (statics != is_static) {
  3429. continue;
  3430. }
  3431. prop = property_get_default(prop_info);
  3432. if (Z_ISUNDEF_P(prop)) {
  3433. continue;
  3434. }
  3435. /* copy: enforce read only access */
  3436. ZVAL_DEREF(prop);
  3437. ZVAL_COPY_OR_DUP(&prop_copy, prop);
  3438. /* this is necessary to make it able to work with default array
  3439. * properties, returned to user */
  3440. if (Z_TYPE(prop_copy) == IS_CONSTANT_AST) {
  3441. if (UNEXPECTED(zval_update_constant_ex(&prop_copy, ce) != SUCCESS)) {
  3442. return;
  3443. }
  3444. }
  3445. zend_hash_update(Z_ARRVAL_P(return_value), key, &prop_copy);
  3446. } ZEND_HASH_FOREACH_END();
  3447. }
  3448. /* }}} */
  3449. /* {{{ Returns an associative array containing all static property values of the class */
  3450. ZEND_METHOD(ReflectionClass, getStaticProperties)
  3451. {
  3452. reflection_object *intern;
  3453. zend_class_entry *ce;
  3454. zend_property_info *prop_info;
  3455. zval *prop;
  3456. zend_string *key;
  3457. if (zend_parse_parameters_none() == FAILURE) {
  3458. RETURN_THROWS();
  3459. }
  3460. GET_REFLECTION_OBJECT_PTR(ce);
  3461. if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
  3462. RETURN_THROWS();
  3463. }
  3464. if (ce->default_static_members_count && !CE_STATIC_MEMBERS(ce)) {
  3465. zend_class_init_statics(ce);
  3466. }
  3467. array_init(return_value);
  3468. ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->properties_info, key, prop_info) {
  3469. if (((prop_info->flags & ZEND_ACC_PRIVATE) &&
  3470. prop_info->ce != ce)) {
  3471. continue;
  3472. }
  3473. if ((prop_info->flags & ZEND_ACC_STATIC) == 0) {
  3474. continue;
  3475. }
  3476. prop = &CE_STATIC_MEMBERS(ce)[prop_info->offset];
  3477. ZVAL_DEINDIRECT(prop);
  3478. if (ZEND_TYPE_IS_SET(prop_info->type) && Z_ISUNDEF_P(prop)) {
  3479. continue;
  3480. }
  3481. /* enforce read only access */
  3482. ZVAL_DEREF(prop);
  3483. Z_TRY_ADDREF_P(prop);
  3484. zend_hash_update(Z_ARRVAL_P(return_value), key, prop);
  3485. } ZEND_HASH_FOREACH_END();
  3486. }
  3487. /* }}} */
  3488. /* {{{ Returns the value of a static property */
  3489. ZEND_METHOD(ReflectionClass, getStaticPropertyValue)
  3490. {
  3491. reflection_object *intern;
  3492. zend_class_entry *ce, *old_scope;
  3493. zend_string *name;
  3494. zval *prop, *def_value = NULL;
  3495. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|z", &name, &def_value) == FAILURE) {
  3496. RETURN_THROWS();
  3497. }
  3498. GET_REFLECTION_OBJECT_PTR(ce);
  3499. if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
  3500. RETURN_THROWS();
  3501. }
  3502. old_scope = EG(fake_scope);
  3503. EG(fake_scope) = ce;
  3504. prop = zend_std_get_static_property(ce, name, BP_VAR_IS);
  3505. EG(fake_scope) = old_scope;
  3506. if (prop) {
  3507. RETURN_COPY_DEREF(prop);
  3508. }
  3509. if (def_value) {
  3510. RETURN_COPY(def_value);
  3511. }
  3512. zend_throw_exception_ex(reflection_exception_ptr, 0,
  3513. "Property %s::$%s does not exist", ZSTR_VAL(ce->name), ZSTR_VAL(name));
  3514. }
  3515. /* }}} */
  3516. /* {{{ Sets the value of a static property */
  3517. ZEND_METHOD(ReflectionClass, setStaticPropertyValue)
  3518. {
  3519. reflection_object *intern;
  3520. zend_class_entry *ce, *old_scope;
  3521. zend_property_info *prop_info;
  3522. zend_string *name;
  3523. zval *variable_ptr, *value;
  3524. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sz", &name, &value) == FAILURE) {
  3525. RETURN_THROWS();
  3526. }
  3527. GET_REFLECTION_OBJECT_PTR(ce);
  3528. if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
  3529. RETURN_THROWS();
  3530. }
  3531. old_scope = EG(fake_scope);
  3532. EG(fake_scope) = ce;
  3533. variable_ptr = zend_std_get_static_property_with_info(ce, name, BP_VAR_W, &prop_info);
  3534. EG(fake_scope) = old_scope;
  3535. if (!variable_ptr) {
  3536. zend_clear_exception();
  3537. zend_throw_exception_ex(reflection_exception_ptr, 0,
  3538. "Class %s does not have a property named %s", ZSTR_VAL(ce->name), ZSTR_VAL(name));
  3539. RETURN_THROWS();
  3540. }
  3541. if (Z_ISREF_P(variable_ptr)) {
  3542. zend_reference *ref = Z_REF_P(variable_ptr);
  3543. variable_ptr = Z_REFVAL_P(variable_ptr);
  3544. if (!zend_verify_ref_assignable_zval(ref, value, 0)) {
  3545. return;
  3546. }
  3547. }
  3548. if (ZEND_TYPE_IS_SET(prop_info->type) && !zend_verify_property_type(prop_info, value, 0)) {
  3549. return;
  3550. }
  3551. zval_ptr_dtor(variable_ptr);
  3552. ZVAL_COPY(variable_ptr, value);
  3553. }
  3554. /* }}} */
  3555. /* {{{ Returns an associative array containing copies of all default property values of the class */
  3556. ZEND_METHOD(ReflectionClass, getDefaultProperties)
  3557. {
  3558. reflection_object *intern;
  3559. zend_class_entry *ce;
  3560. if (zend_parse_parameters_none() == FAILURE) {
  3561. RETURN_THROWS();
  3562. }
  3563. GET_REFLECTION_OBJECT_PTR(ce);
  3564. array_init(return_value);
  3565. if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
  3566. RETURN_THROWS();
  3567. }
  3568. add_class_vars(ce, 1, return_value);
  3569. add_class_vars(ce, 0, return_value);
  3570. }
  3571. /* }}} */
  3572. /* {{{ Returns a string representation */
  3573. ZEND_METHOD(ReflectionClass, __toString)
  3574. {
  3575. reflection_object *intern;
  3576. zend_class_entry *ce;
  3577. smart_str str = {0};
  3578. if (zend_parse_parameters_none() == FAILURE) {
  3579. RETURN_THROWS();
  3580. }
  3581. GET_REFLECTION_OBJECT_PTR(ce);
  3582. _class_string(&str, ce, &intern->obj, "");
  3583. RETURN_STR(smart_str_extract(&str));
  3584. }
  3585. /* }}} */
  3586. /* {{{ Returns the class' name */
  3587. ZEND_METHOD(ReflectionClass, getName)
  3588. {
  3589. reflection_object *intern;
  3590. zend_class_entry *ce;
  3591. if (zend_parse_parameters_none() == FAILURE) {
  3592. RETURN_THROWS();
  3593. }
  3594. GET_REFLECTION_OBJECT_PTR(ce);
  3595. RETURN_STR_COPY(ce->name);
  3596. }
  3597. /* }}} */
  3598. /* {{{ Returns whether this class is an internal class */
  3599. ZEND_METHOD(ReflectionClass, isInternal)
  3600. {
  3601. reflection_object *intern;
  3602. zend_class_entry *ce;
  3603. if (zend_parse_parameters_none() == FAILURE) {
  3604. RETURN_THROWS();
  3605. }
  3606. GET_REFLECTION_OBJECT_PTR(ce);
  3607. RETURN_BOOL(ce->type == ZEND_INTERNAL_CLASS);
  3608. }
  3609. /* }}} */
  3610. /* {{{ Returns whether this class is user-defined */
  3611. ZEND_METHOD(ReflectionClass, isUserDefined)
  3612. {
  3613. reflection_object *intern;
  3614. zend_class_entry *ce;
  3615. if (zend_parse_parameters_none() == FAILURE) {
  3616. RETURN_THROWS();
  3617. }
  3618. GET_REFLECTION_OBJECT_PTR(ce);
  3619. RETURN_BOOL(ce->type == ZEND_USER_CLASS);
  3620. }
  3621. /* }}} */
  3622. /* {{{ Returns whether this class is anonymous */
  3623. ZEND_METHOD(ReflectionClass, isAnonymous)
  3624. {
  3625. reflection_object *intern;
  3626. zend_class_entry *ce;
  3627. if (zend_parse_parameters_none() == FAILURE) {
  3628. RETURN_THROWS();
  3629. }
  3630. GET_REFLECTION_OBJECT_PTR(ce);
  3631. RETURN_BOOL(ce->ce_flags & ZEND_ACC_ANON_CLASS);
  3632. }
  3633. /* }}} */
  3634. /* {{{ Returns the filename of the file this class was declared in */
  3635. ZEND_METHOD(ReflectionClass, getFileName)
  3636. {
  3637. reflection_object *intern;
  3638. zend_class_entry *ce;
  3639. if (zend_parse_parameters_none() == FAILURE) {
  3640. RETURN_THROWS();
  3641. }
  3642. GET_REFLECTION_OBJECT_PTR(ce);
  3643. if (ce->type == ZEND_USER_CLASS) {
  3644. RETURN_STR_COPY(ce->info.user.filename);
  3645. }
  3646. RETURN_FALSE;
  3647. }
  3648. /* }}} */
  3649. /* {{{ Returns the line this class' declaration starts at */
  3650. ZEND_METHOD(ReflectionClass, getStartLine)
  3651. {
  3652. reflection_object *intern;
  3653. zend_class_entry *ce;
  3654. if (zend_parse_parameters_none() == FAILURE) {
  3655. RETURN_THROWS();
  3656. }
  3657. GET_REFLECTION_OBJECT_PTR(ce);
  3658. if (ce->type == ZEND_USER_CLASS) {
  3659. RETURN_LONG(ce->info.user.line_start);
  3660. }
  3661. RETURN_FALSE;
  3662. }
  3663. /* }}} */
  3664. /* {{{ Returns the line this class' declaration ends at */
  3665. ZEND_METHOD(ReflectionClass, getEndLine)
  3666. {
  3667. reflection_object *intern;
  3668. zend_class_entry *ce;
  3669. if (zend_parse_parameters_none() == FAILURE) {
  3670. RETURN_THROWS();
  3671. }
  3672. GET_REFLECTION_OBJECT_PTR(ce);
  3673. if (ce->type == ZEND_USER_CLASS) {
  3674. RETURN_LONG(ce->info.user.line_end);
  3675. }
  3676. RETURN_FALSE;
  3677. }
  3678. /* }}} */
  3679. /* {{{ Returns the doc comment for this class */
  3680. ZEND_METHOD(ReflectionClass, getDocComment)
  3681. {
  3682. reflection_object *intern;
  3683. zend_class_entry *ce;
  3684. if (zend_parse_parameters_none() == FAILURE) {
  3685. RETURN_THROWS();
  3686. }
  3687. GET_REFLECTION_OBJECT_PTR(ce);
  3688. if (ce->type == ZEND_USER_CLASS && ce->info.user.doc_comment) {
  3689. RETURN_STR_COPY(ce->info.user.doc_comment);
  3690. }
  3691. RETURN_FALSE;
  3692. }
  3693. /* }}} */
  3694. /* {{{ Returns the attributes for this class */
  3695. ZEND_METHOD(ReflectionClass, getAttributes)
  3696. {
  3697. reflection_object *intern;
  3698. zend_class_entry *ce;
  3699. GET_REFLECTION_OBJECT_PTR(ce);
  3700. reflect_attributes(INTERNAL_FUNCTION_PARAM_PASSTHRU,
  3701. ce->attributes, 0, ce, ZEND_ATTRIBUTE_TARGET_CLASS,
  3702. ce->type == ZEND_USER_CLASS ? ce->info.user.filename : NULL);
  3703. }
  3704. /* }}} */
  3705. /* {{{ Returns the class' constructor if there is one, NULL otherwise */
  3706. ZEND_METHOD(ReflectionClass, getConstructor)
  3707. {
  3708. reflection_object *intern;
  3709. zend_class_entry *ce;
  3710. if (zend_parse_parameters_none() == FAILURE) {
  3711. RETURN_THROWS();
  3712. }
  3713. GET_REFLECTION_OBJECT_PTR(ce);
  3714. if (ce->constructor) {
  3715. reflection_method_factory(ce, ce->constructor, NULL, return_value);
  3716. } else {
  3717. RETURN_NULL();
  3718. }
  3719. }
  3720. /* }}} */
  3721. /* {{{ Returns whether a method exists or not */
  3722. ZEND_METHOD(ReflectionClass, hasMethod)
  3723. {
  3724. reflection_object *intern;
  3725. zend_class_entry *ce;
  3726. zend_string *name, *lc_name;
  3727. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
  3728. RETURN_THROWS();
  3729. }
  3730. GET_REFLECTION_OBJECT_PTR(ce);
  3731. lc_name = zend_string_tolower(name);
  3732. RETVAL_BOOL(zend_hash_exists(&ce->function_table, lc_name) || is_closure_invoke(ce, lc_name));
  3733. zend_string_release(lc_name);
  3734. }
  3735. /* }}} */
  3736. /* {{{ Returns the class' method specified by its name */
  3737. ZEND_METHOD(ReflectionClass, getMethod)
  3738. {
  3739. reflection_object *intern;
  3740. zend_class_entry *ce;
  3741. zend_function *mptr;
  3742. zval obj_tmp;
  3743. zend_string *name, *lc_name;
  3744. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
  3745. RETURN_THROWS();
  3746. }
  3747. GET_REFLECTION_OBJECT_PTR(ce);
  3748. lc_name = zend_string_tolower(name);
  3749. if (!Z_ISUNDEF(intern->obj) && is_closure_invoke(ce, lc_name)
  3750. && (mptr = zend_get_closure_invoke_method(Z_OBJ(intern->obj))) != NULL)
  3751. {
  3752. /* don't assign closure_object since we only reflect the invoke handler
  3753. method and not the closure definition itself */
  3754. reflection_method_factory(ce, mptr, NULL, return_value);
  3755. } else if (Z_ISUNDEF(intern->obj) && is_closure_invoke(ce, lc_name)
  3756. && object_init_ex(&obj_tmp, ce) == SUCCESS && (mptr = zend_get_closure_invoke_method(Z_OBJ(obj_tmp))) != NULL) {
  3757. /* don't assign closure_object since we only reflect the invoke handler
  3758. method and not the closure definition itself */
  3759. reflection_method_factory(ce, mptr, NULL, return_value);
  3760. zval_ptr_dtor(&obj_tmp);
  3761. } else if ((mptr = zend_hash_find_ptr(&ce->function_table, lc_name)) != NULL) {
  3762. reflection_method_factory(ce, mptr, NULL, return_value);
  3763. } else {
  3764. zend_throw_exception_ex(reflection_exception_ptr, 0,
  3765. "Method %s::%s() does not exist", ZSTR_VAL(ce->name), ZSTR_VAL(name));
  3766. }
  3767. zend_string_release(lc_name);
  3768. }
  3769. /* }}} */
  3770. /* {{{ _addmethod */
  3771. static bool _addmethod(zend_function *mptr, zend_class_entry *ce, HashTable *ht, zend_long filter)
  3772. {
  3773. if ((mptr->common.fn_flags & ZEND_ACC_PRIVATE) && mptr->common.scope != ce) {
  3774. return 0;
  3775. }
  3776. if (mptr->common.fn_flags & filter) {
  3777. zval method;
  3778. reflection_method_factory(ce, mptr, NULL, &method);
  3779. zend_hash_next_index_insert_new(ht, &method);
  3780. return 1;
  3781. }
  3782. return 0;
  3783. }
  3784. /* }}} */
  3785. /* {{{ Returns an array of this class' methods */
  3786. ZEND_METHOD(ReflectionClass, getMethods)
  3787. {
  3788. reflection_object *intern;
  3789. zend_class_entry *ce;
  3790. zend_function *mptr;
  3791. zend_long filter;
  3792. bool filter_is_null = 1;
  3793. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l!", &filter, &filter_is_null) == FAILURE) {
  3794. RETURN_THROWS();
  3795. }
  3796. if (filter_is_null) {
  3797. filter = ZEND_ACC_PPP_MASK | ZEND_ACC_ABSTRACT | ZEND_ACC_FINAL | ZEND_ACC_STATIC;
  3798. }
  3799. GET_REFLECTION_OBJECT_PTR(ce);
  3800. array_init(return_value);
  3801. ZEND_HASH_FOREACH_PTR(&ce->function_table, mptr) {
  3802. _addmethod(mptr, ce, Z_ARRVAL_P(return_value), filter);
  3803. } ZEND_HASH_FOREACH_END();
  3804. if (instanceof_function(ce, zend_ce_closure)) {
  3805. bool has_obj = Z_TYPE(intern->obj) != IS_UNDEF;
  3806. zval obj_tmp;
  3807. zend_object *obj;
  3808. if (!has_obj) {
  3809. object_init_ex(&obj_tmp, ce);
  3810. obj = Z_OBJ(obj_tmp);
  3811. } else {
  3812. obj = Z_OBJ(intern->obj);
  3813. }
  3814. zend_function *closure = zend_get_closure_invoke_method(obj);
  3815. if (closure) {
  3816. if (!_addmethod(closure, ce, Z_ARRVAL_P(return_value), filter)) {
  3817. _free_function(closure);
  3818. }
  3819. }
  3820. if (!has_obj) {
  3821. zval_ptr_dtor(&obj_tmp);
  3822. }
  3823. }
  3824. }
  3825. /* }}} */
  3826. /* {{{ Returns whether a property exists or not */
  3827. ZEND_METHOD(ReflectionClass, hasProperty)
  3828. {
  3829. reflection_object *intern;
  3830. zend_property_info *property_info;
  3831. zend_class_entry *ce;
  3832. zend_string *name;
  3833. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
  3834. RETURN_THROWS();
  3835. }
  3836. GET_REFLECTION_OBJECT_PTR(ce);
  3837. if ((property_info = zend_hash_find_ptr(&ce->properties_info, name)) != NULL) {
  3838. if ((property_info->flags & ZEND_ACC_PRIVATE) && property_info->ce != ce) {
  3839. RETURN_FALSE;
  3840. }
  3841. RETURN_TRUE;
  3842. } else {
  3843. if (Z_TYPE(intern->obj) != IS_UNDEF) {
  3844. if (Z_OBJ_HANDLER(intern->obj, has_property)(Z_OBJ(intern->obj), name, 2, NULL)) {
  3845. RETURN_TRUE;
  3846. }
  3847. }
  3848. RETURN_FALSE;
  3849. }
  3850. }
  3851. /* }}} */
  3852. /* {{{ Returns the class' property specified by its name */
  3853. ZEND_METHOD(ReflectionClass, getProperty)
  3854. {
  3855. reflection_object *intern;
  3856. zend_class_entry *ce, *ce2;
  3857. zend_property_info *property_info;
  3858. zend_string *name, *classname;
  3859. char *tmp, *str_name;
  3860. size_t classname_len, str_name_len;
  3861. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
  3862. RETURN_THROWS();
  3863. }
  3864. GET_REFLECTION_OBJECT_PTR(ce);
  3865. if ((property_info = zend_hash_find_ptr(&ce->properties_info, name)) != NULL) {
  3866. if (!(property_info->flags & ZEND_ACC_PRIVATE) || property_info->ce == ce) {
  3867. reflection_property_factory(ce, name, property_info, return_value);
  3868. return;
  3869. }
  3870. } else if (Z_TYPE(intern->obj) != IS_UNDEF) {
  3871. /* Check for dynamic properties */
  3872. if (zend_hash_exists(Z_OBJ_HT(intern->obj)->get_properties(Z_OBJ(intern->obj)), name)) {
  3873. reflection_property_factory(ce, name, NULL, return_value);
  3874. return;
  3875. }
  3876. }
  3877. str_name = ZSTR_VAL(name);
  3878. if ((tmp = strstr(ZSTR_VAL(name), "::")) != NULL) {
  3879. classname_len = tmp - ZSTR_VAL(name);
  3880. classname = zend_string_alloc(classname_len, 0);
  3881. zend_str_tolower_copy(ZSTR_VAL(classname), ZSTR_VAL(name), classname_len);
  3882. ZSTR_VAL(classname)[classname_len] = '\0';
  3883. str_name_len = ZSTR_LEN(name) - (classname_len + 2);
  3884. str_name = tmp + 2;
  3885. ce2 = zend_lookup_class(classname);
  3886. if (!ce2) {
  3887. if (!EG(exception)) {
  3888. zend_throw_exception_ex(reflection_exception_ptr, -1, "Class \"%s\" does not exist", ZSTR_VAL(classname));
  3889. }
  3890. zend_string_release_ex(classname, 0);
  3891. RETURN_THROWS();
  3892. }
  3893. zend_string_release_ex(classname, 0);
  3894. if (!instanceof_function(ce, ce2)) {
  3895. zend_throw_exception_ex(reflection_exception_ptr, -1, "Fully qualified property name %s::$%s does not specify a base class of %s", ZSTR_VAL(ce2->name), str_name, ZSTR_VAL(ce->name));
  3896. RETURN_THROWS();
  3897. }
  3898. ce = ce2;
  3899. property_info = zend_hash_str_find_ptr(&ce->properties_info, str_name, str_name_len);
  3900. if (property_info != NULL
  3901. && (!(property_info->flags & ZEND_ACC_PRIVATE)
  3902. || property_info->ce == ce)) {
  3903. reflection_property_factory_str(ce, str_name, str_name_len, property_info, return_value);
  3904. return;
  3905. }
  3906. }
  3907. zend_throw_exception_ex(reflection_exception_ptr, 0, "Property %s::$%s does not exist", ZSTR_VAL(ce->name), str_name);
  3908. }
  3909. /* }}} */
  3910. /* {{{ _addproperty */
  3911. static void _addproperty(zend_property_info *pptr, zend_string *key, zend_class_entry *ce, HashTable *ht, long filter)
  3912. {
  3913. if ((pptr->flags & ZEND_ACC_PRIVATE) && pptr->ce != ce) {
  3914. return;
  3915. }
  3916. if (pptr->flags & filter) {
  3917. zval property;
  3918. reflection_property_factory(ce, key, pptr, &property);
  3919. zend_hash_next_index_insert_new(ht, &property);
  3920. }
  3921. }
  3922. /* }}} */
  3923. /* {{{ _adddynproperty */
  3924. static void _adddynproperty(zval *ptr, zend_string *key, zend_class_entry *ce, zval *retval)
  3925. {
  3926. zval property;
  3927. /* under some circumstances, the properties hash table may contain numeric
  3928. * properties (e.g. when casting from array). This is a WON'T FIX bug, at
  3929. * least for the moment. Ignore these */
  3930. if (key == NULL) {
  3931. return;
  3932. }
  3933. /* Not a dynamic property */
  3934. if (Z_TYPE_P(ptr) == IS_INDIRECT) {
  3935. return;
  3936. }
  3937. reflection_property_factory(ce, key, NULL, &property);
  3938. add_next_index_zval(retval, &property);
  3939. }
  3940. /* }}} */
  3941. /* {{{ Returns an array of this class' properties */
  3942. ZEND_METHOD(ReflectionClass, getProperties)
  3943. {
  3944. reflection_object *intern;
  3945. zend_class_entry *ce;
  3946. zend_string *key;
  3947. zend_property_info *prop_info;
  3948. zend_long filter;
  3949. bool filter_is_null = 1;
  3950. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l!", &filter, &filter_is_null) == FAILURE) {
  3951. RETURN_THROWS();
  3952. }
  3953. if (filter_is_null) {
  3954. filter = ZEND_ACC_PPP_MASK | ZEND_ACC_STATIC;
  3955. }
  3956. GET_REFLECTION_OBJECT_PTR(ce);
  3957. array_init(return_value);
  3958. ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->properties_info, key, prop_info) {
  3959. _addproperty(prop_info, key, ce, Z_ARRVAL_P(return_value), filter);
  3960. } ZEND_HASH_FOREACH_END();
  3961. if (Z_TYPE(intern->obj) != IS_UNDEF && (filter & ZEND_ACC_PUBLIC) != 0) {
  3962. HashTable *properties = Z_OBJ_HT(intern->obj)->get_properties(Z_OBJ(intern->obj));
  3963. zval *prop;
  3964. ZEND_HASH_FOREACH_STR_KEY_VAL(properties, key, prop) {
  3965. _adddynproperty(prop, key, ce, return_value);
  3966. } ZEND_HASH_FOREACH_END();
  3967. }
  3968. }
  3969. /* }}} */
  3970. /* {{{ Returns whether a constant exists or not */
  3971. ZEND_METHOD(ReflectionClass, hasConstant)
  3972. {
  3973. reflection_object *intern;
  3974. zend_class_entry *ce;
  3975. zend_string *name;
  3976. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
  3977. RETURN_THROWS();
  3978. }
  3979. GET_REFLECTION_OBJECT_PTR(ce);
  3980. if (zend_hash_exists(&ce->constants_table, name)) {
  3981. RETURN_TRUE;
  3982. } else {
  3983. RETURN_FALSE;
  3984. }
  3985. }
  3986. /* }}} */
  3987. /* {{{ Returns an associative array containing this class' constants and their values */
  3988. ZEND_METHOD(ReflectionClass, getConstants)
  3989. {
  3990. reflection_object *intern;
  3991. zend_class_entry *ce;
  3992. zend_string *key;
  3993. zend_class_constant *constant;
  3994. zval val;
  3995. zend_long filter;
  3996. bool filter_is_null = 1;
  3997. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l!", &filter, &filter_is_null) == FAILURE) {
  3998. RETURN_THROWS();
  3999. }
  4000. if (filter_is_null) {
  4001. filter = ZEND_ACC_PPP_MASK;
  4002. }
  4003. GET_REFLECTION_OBJECT_PTR(ce);
  4004. array_init(return_value);
  4005. ZEND_HASH_FOREACH_STR_KEY_PTR(CE_CONSTANTS_TABLE(ce), key, constant) {
  4006. if (UNEXPECTED(zval_update_constant_ex(&constant->value, constant->ce) != SUCCESS)) {
  4007. RETURN_THROWS();
  4008. }
  4009. if (ZEND_CLASS_CONST_FLAGS(constant) & filter) {
  4010. ZVAL_COPY_OR_DUP(&val, &constant->value);
  4011. zend_hash_add_new(Z_ARRVAL_P(return_value), key, &val);
  4012. }
  4013. } ZEND_HASH_FOREACH_END();
  4014. }
  4015. /* }}} */
  4016. /* {{{ Returns an associative array containing this class' constants as ReflectionClassConstant objects */
  4017. ZEND_METHOD(ReflectionClass, getReflectionConstants)
  4018. {
  4019. reflection_object *intern;
  4020. zend_class_entry *ce;
  4021. zend_string *name;
  4022. zend_class_constant *constant;
  4023. zend_long filter;
  4024. bool filter_is_null = 1;
  4025. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l!", &filter, &filter_is_null) == FAILURE) {
  4026. RETURN_THROWS();
  4027. }
  4028. if (filter_is_null) {
  4029. filter = ZEND_ACC_PPP_MASK;
  4030. }
  4031. GET_REFLECTION_OBJECT_PTR(ce);
  4032. array_init(return_value);
  4033. ZEND_HASH_FOREACH_STR_KEY_PTR(CE_CONSTANTS_TABLE(ce), name, constant) {
  4034. if (ZEND_CLASS_CONST_FLAGS(constant) & filter) {
  4035. zval class_const;
  4036. reflection_class_constant_factory(name, constant, &class_const);
  4037. zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &class_const);
  4038. }
  4039. } ZEND_HASH_FOREACH_END();
  4040. }
  4041. /* }}} */
  4042. /* {{{ Returns the class' constant specified by its name */
  4043. ZEND_METHOD(ReflectionClass, getConstant)
  4044. {
  4045. reflection_object *intern;
  4046. zend_class_entry *ce;
  4047. HashTable *constants_table;
  4048. zend_class_constant *c;
  4049. zend_string *name;
  4050. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
  4051. RETURN_THROWS();
  4052. }
  4053. GET_REFLECTION_OBJECT_PTR(ce);
  4054. constants_table = CE_CONSTANTS_TABLE(ce);
  4055. ZEND_HASH_FOREACH_PTR(constants_table, c) {
  4056. if (UNEXPECTED(zval_update_constant_ex(&c->value, c->ce) != SUCCESS)) {
  4057. RETURN_THROWS();
  4058. }
  4059. } ZEND_HASH_FOREACH_END();
  4060. if ((c = zend_hash_find_ptr(constants_table, name)) == NULL) {
  4061. RETURN_FALSE;
  4062. }
  4063. ZVAL_COPY_OR_DUP(return_value, &c->value);
  4064. }
  4065. /* }}} */
  4066. /* {{{ Returns the class' constant as ReflectionClassConstant objects */
  4067. ZEND_METHOD(ReflectionClass, getReflectionConstant)
  4068. {
  4069. reflection_object *intern;
  4070. zend_class_entry *ce;
  4071. zend_class_constant *constant;
  4072. zend_string *name;
  4073. GET_REFLECTION_OBJECT_PTR(ce);
  4074. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
  4075. RETURN_THROWS();
  4076. }
  4077. if ((constant = zend_hash_find_ptr(CE_CONSTANTS_TABLE(ce), name)) == NULL) {
  4078. RETURN_FALSE;
  4079. }
  4080. reflection_class_constant_factory(name, constant, return_value);
  4081. }
  4082. /* }}} */
  4083. /* {{{ _class_check_flag */
  4084. static void _class_check_flag(INTERNAL_FUNCTION_PARAMETERS, int mask)
  4085. {
  4086. reflection_object *intern;
  4087. zend_class_entry *ce;
  4088. if (zend_parse_parameters_none() == FAILURE) {
  4089. RETURN_THROWS();
  4090. }
  4091. GET_REFLECTION_OBJECT_PTR(ce);
  4092. RETVAL_BOOL(ce->ce_flags & mask);
  4093. }
  4094. /* }}} */
  4095. /* {{{ Returns whether this class is instantiable */
  4096. ZEND_METHOD(ReflectionClass, isInstantiable)
  4097. {
  4098. reflection_object *intern;
  4099. zend_class_entry *ce;
  4100. if (zend_parse_parameters_none() == FAILURE) {
  4101. RETURN_THROWS();
  4102. }
  4103. GET_REFLECTION_OBJECT_PTR(ce);
  4104. if (ce->ce_flags & (ZEND_ACC_INTERFACE | ZEND_ACC_TRAIT | ZEND_ACC_EXPLICIT_ABSTRACT_CLASS | ZEND_ACC_IMPLICIT_ABSTRACT_CLASS | ZEND_ACC_ENUM)) {
  4105. RETURN_FALSE;
  4106. }
  4107. /* Basically, the class is instantiable. Though, if there is a constructor
  4108. * and it is not publicly accessible, it isn't! */
  4109. if (!ce->constructor) {
  4110. RETURN_TRUE;
  4111. }
  4112. RETURN_BOOL(ce->constructor->common.fn_flags & ZEND_ACC_PUBLIC);
  4113. }
  4114. /* }}} */
  4115. /* {{{ Returns whether this class is cloneable */
  4116. ZEND_METHOD(ReflectionClass, isCloneable)
  4117. {
  4118. reflection_object *intern;
  4119. zend_class_entry *ce;
  4120. zval obj;
  4121. if (zend_parse_parameters_none() == FAILURE) {
  4122. RETURN_THROWS();
  4123. }
  4124. GET_REFLECTION_OBJECT_PTR(ce);
  4125. if (ce->ce_flags & (ZEND_ACC_INTERFACE | ZEND_ACC_TRAIT | ZEND_ACC_EXPLICIT_ABSTRACT_CLASS | ZEND_ACC_IMPLICIT_ABSTRACT_CLASS | ZEND_ACC_ENUM)) {
  4126. RETURN_FALSE;
  4127. }
  4128. if (!Z_ISUNDEF(intern->obj)) {
  4129. if (ce->clone) {
  4130. RETURN_BOOL(ce->clone->common.fn_flags & ZEND_ACC_PUBLIC);
  4131. } else {
  4132. RETURN_BOOL(Z_OBJ_HANDLER(intern->obj, clone_obj) != NULL);
  4133. }
  4134. } else {
  4135. if (ce->clone) {
  4136. RETURN_BOOL(ce->clone->common.fn_flags & ZEND_ACC_PUBLIC);
  4137. } else {
  4138. if (UNEXPECTED(object_init_ex(&obj, ce) != SUCCESS)) {
  4139. return;
  4140. }
  4141. /* We're not calling the constructor, so don't call the destructor either. */
  4142. zend_object_store_ctor_failed(Z_OBJ(obj));
  4143. RETVAL_BOOL(Z_OBJ_HANDLER(obj, clone_obj) != NULL);
  4144. zval_ptr_dtor(&obj);
  4145. }
  4146. }
  4147. }
  4148. /* }}} */
  4149. /* {{{ Returns whether this is an interface or a class */
  4150. ZEND_METHOD(ReflectionClass, isInterface)
  4151. {
  4152. _class_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_INTERFACE);
  4153. }
  4154. /* }}} */
  4155. /* {{{ Returns whether this is a trait */
  4156. ZEND_METHOD(ReflectionClass, isTrait)
  4157. {
  4158. _class_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_TRAIT);
  4159. }
  4160. /* }}} */
  4161. ZEND_METHOD(ReflectionClass, isEnum)
  4162. {
  4163. _class_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_ENUM);
  4164. }
  4165. /* {{{ Returns whether this class is final */
  4166. ZEND_METHOD(ReflectionClass, isFinal)
  4167. {
  4168. _class_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_FINAL);
  4169. }
  4170. /* }}} */
  4171. /* {{{ Returns whether this class is abstract */
  4172. ZEND_METHOD(ReflectionClass, isAbstract)
  4173. {
  4174. _class_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS);
  4175. }
  4176. /* }}} */
  4177. /* {{{ Returns a bitfield of the access modifiers for this class */
  4178. ZEND_METHOD(ReflectionClass, getModifiers)
  4179. {
  4180. reflection_object *intern;
  4181. zend_class_entry *ce;
  4182. uint32_t keep_flags = ZEND_ACC_FINAL
  4183. | ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
  4184. if (zend_parse_parameters_none() == FAILURE) {
  4185. RETURN_THROWS();
  4186. }
  4187. GET_REFLECTION_OBJECT_PTR(ce);
  4188. RETURN_LONG((ce->ce_flags & keep_flags));
  4189. }
  4190. /* }}} */
  4191. /* {{{ Returns whether the given object is an instance of this class */
  4192. ZEND_METHOD(ReflectionClass, isInstance)
  4193. {
  4194. reflection_object *intern;
  4195. zend_class_entry *ce;
  4196. zval *object;
  4197. if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &object) == FAILURE) {
  4198. RETURN_THROWS();
  4199. }
  4200. GET_REFLECTION_OBJECT_PTR(ce);
  4201. RETURN_BOOL(instanceof_function(Z_OBJCE_P(object), ce));
  4202. }
  4203. /* }}} */
  4204. /* {{{ Returns an instance of this class */
  4205. ZEND_METHOD(ReflectionClass, newInstance)
  4206. {
  4207. reflection_object *intern;
  4208. zend_class_entry *ce, *old_scope;
  4209. zend_function *constructor;
  4210. GET_REFLECTION_OBJECT_PTR(ce);
  4211. if (UNEXPECTED(object_init_ex(return_value, ce) != SUCCESS)) {
  4212. return;
  4213. }
  4214. old_scope = EG(fake_scope);
  4215. EG(fake_scope) = ce;
  4216. constructor = Z_OBJ_HT_P(return_value)->get_constructor(Z_OBJ_P(return_value));
  4217. EG(fake_scope) = old_scope;
  4218. /* Run the constructor if there is one */
  4219. if (constructor) {
  4220. zval *params;
  4221. int num_args;
  4222. HashTable *named_params;
  4223. if (!(constructor->common.fn_flags & ZEND_ACC_PUBLIC)) {
  4224. zend_throw_exception_ex(reflection_exception_ptr, 0, "Access to non-public constructor of class %s", ZSTR_VAL(ce->name));
  4225. zval_ptr_dtor(return_value);
  4226. RETURN_NULL();
  4227. }
  4228. ZEND_PARSE_PARAMETERS_START(0, -1)
  4229. Z_PARAM_VARIADIC_WITH_NAMED(params, num_args, named_params)
  4230. ZEND_PARSE_PARAMETERS_END();
  4231. zend_call_known_function(
  4232. constructor, Z_OBJ_P(return_value), Z_OBJCE_P(return_value), NULL,
  4233. num_args, params, named_params);
  4234. if (EG(exception)) {
  4235. zend_object_store_ctor_failed(Z_OBJ_P(return_value));
  4236. }
  4237. } else if (ZEND_NUM_ARGS()) {
  4238. zend_throw_exception_ex(reflection_exception_ptr, 0, "Class %s does not have a constructor, so you cannot pass any constructor arguments", ZSTR_VAL(ce->name));
  4239. }
  4240. }
  4241. /* }}} */
  4242. /* {{{ Returns an instance of this class without invoking its constructor */
  4243. ZEND_METHOD(ReflectionClass, newInstanceWithoutConstructor)
  4244. {
  4245. reflection_object *intern;
  4246. zend_class_entry *ce;
  4247. GET_REFLECTION_OBJECT_PTR(ce);
  4248. if (zend_parse_parameters_none() == FAILURE) {
  4249. RETURN_THROWS();
  4250. }
  4251. if (ce->type == ZEND_INTERNAL_CLASS
  4252. && ce->create_object != NULL && (ce->ce_flags & ZEND_ACC_FINAL)) {
  4253. zend_throw_exception_ex(reflection_exception_ptr, 0, "Class %s is an internal class marked as final that cannot be instantiated without invoking its constructor", ZSTR_VAL(ce->name));
  4254. RETURN_THROWS();
  4255. }
  4256. object_init_ex(return_value, ce);
  4257. }
  4258. /* }}} */
  4259. /* {{{ Returns an instance of this class */
  4260. ZEND_METHOD(ReflectionClass, newInstanceArgs)
  4261. {
  4262. reflection_object *intern;
  4263. zend_class_entry *ce, *old_scope;
  4264. int argc = 0;
  4265. HashTable *args = NULL;
  4266. zend_function *constructor;
  4267. GET_REFLECTION_OBJECT_PTR(ce);
  4268. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|h", &args) == FAILURE) {
  4269. RETURN_THROWS();
  4270. }
  4271. if (args) {
  4272. argc = zend_hash_num_elements(args);
  4273. }
  4274. if (UNEXPECTED(object_init_ex(return_value, ce) != SUCCESS)) {
  4275. return;
  4276. }
  4277. old_scope = EG(fake_scope);
  4278. EG(fake_scope) = ce;
  4279. constructor = Z_OBJ_HT_P(return_value)->get_constructor(Z_OBJ_P(return_value));
  4280. EG(fake_scope) = old_scope;
  4281. /* Run the constructor if there is one */
  4282. if (constructor) {
  4283. if (!(constructor->common.fn_flags & ZEND_ACC_PUBLIC)) {
  4284. zend_throw_exception_ex(reflection_exception_ptr, 0, "Access to non-public constructor of class %s", ZSTR_VAL(ce->name));
  4285. zval_ptr_dtor(return_value);
  4286. RETURN_NULL();
  4287. }
  4288. zend_call_known_function(
  4289. constructor, Z_OBJ_P(return_value), Z_OBJCE_P(return_value), NULL, 0, NULL, args);
  4290. if (EG(exception)) {
  4291. zend_object_store_ctor_failed(Z_OBJ_P(return_value));
  4292. }
  4293. } else if (argc) {
  4294. zend_throw_exception_ex(reflection_exception_ptr, 0, "Class %s does not have a constructor, so you cannot pass any constructor arguments", ZSTR_VAL(ce->name));
  4295. }
  4296. }
  4297. /* }}} */
  4298. /* {{{ Returns an array of interfaces this class implements */
  4299. ZEND_METHOD(ReflectionClass, getInterfaces)
  4300. {
  4301. reflection_object *intern;
  4302. zend_class_entry *ce;
  4303. if (zend_parse_parameters_none() == FAILURE) {
  4304. RETURN_THROWS();
  4305. }
  4306. GET_REFLECTION_OBJECT_PTR(ce);
  4307. if (ce->num_interfaces) {
  4308. uint32_t i;
  4309. ZEND_ASSERT(ce->ce_flags & ZEND_ACC_LINKED);
  4310. array_init(return_value);
  4311. for (i=0; i < ce->num_interfaces; i++) {
  4312. zval interface;
  4313. zend_reflection_class_factory(ce->interfaces[i], &interface);
  4314. zend_hash_update(Z_ARRVAL_P(return_value), ce->interfaces[i]->name, &interface);
  4315. }
  4316. } else {
  4317. RETURN_EMPTY_ARRAY();
  4318. }
  4319. }
  4320. /* }}} */
  4321. /* {{{ Returns an array of names of interfaces this class implements */
  4322. ZEND_METHOD(ReflectionClass, getInterfaceNames)
  4323. {
  4324. reflection_object *intern;
  4325. zend_class_entry *ce;
  4326. uint32_t i;
  4327. if (zend_parse_parameters_none() == FAILURE) {
  4328. RETURN_THROWS();
  4329. }
  4330. GET_REFLECTION_OBJECT_PTR(ce);
  4331. if (!ce->num_interfaces) {
  4332. /* Return an empty array if this class implements no interfaces */
  4333. RETURN_EMPTY_ARRAY();
  4334. }
  4335. ZEND_ASSERT(ce->ce_flags & ZEND_ACC_LINKED);
  4336. array_init(return_value);
  4337. for (i=0; i < ce->num_interfaces; i++) {
  4338. add_next_index_str(return_value, zend_string_copy(ce->interfaces[i]->name));
  4339. }
  4340. }
  4341. /* }}} */
  4342. /* {{{ Returns an array of traits used by this class */
  4343. ZEND_METHOD(ReflectionClass, getTraits)
  4344. {
  4345. reflection_object *intern;
  4346. zend_class_entry *ce;
  4347. uint32_t i;
  4348. if (zend_parse_parameters_none() == FAILURE) {
  4349. RETURN_THROWS();
  4350. }
  4351. GET_REFLECTION_OBJECT_PTR(ce);
  4352. if (!ce->num_traits) {
  4353. RETURN_EMPTY_ARRAY();
  4354. }
  4355. array_init(return_value);
  4356. for (i=0; i < ce->num_traits; i++) {
  4357. zval trait;
  4358. zend_class_entry *trait_ce;
  4359. trait_ce = zend_fetch_class_by_name(ce->trait_names[i].name,
  4360. ce->trait_names[i].lc_name, ZEND_FETCH_CLASS_TRAIT);
  4361. ZEND_ASSERT(trait_ce);
  4362. zend_reflection_class_factory(trait_ce, &trait);
  4363. zend_hash_update(Z_ARRVAL_P(return_value), ce->trait_names[i].name, &trait);
  4364. }
  4365. }
  4366. /* }}} */
  4367. /* {{{ Returns an array of names of traits used by this class */
  4368. ZEND_METHOD(ReflectionClass, getTraitNames)
  4369. {
  4370. reflection_object *intern;
  4371. zend_class_entry *ce;
  4372. uint32_t i;
  4373. if (zend_parse_parameters_none() == FAILURE) {
  4374. RETURN_THROWS();
  4375. }
  4376. GET_REFLECTION_OBJECT_PTR(ce);
  4377. if (!ce->num_traits) {
  4378. RETURN_EMPTY_ARRAY();
  4379. }
  4380. array_init(return_value);
  4381. for (i=0; i < ce->num_traits; i++) {
  4382. add_next_index_str(return_value, zend_string_copy(ce->trait_names[i].name));
  4383. }
  4384. }
  4385. /* }}} */
  4386. /* {{{ Returns an array of trait aliases */
  4387. ZEND_METHOD(ReflectionClass, getTraitAliases)
  4388. {
  4389. reflection_object *intern;
  4390. zend_class_entry *ce;
  4391. if (zend_parse_parameters_none() == FAILURE) {
  4392. RETURN_THROWS();
  4393. }
  4394. GET_REFLECTION_OBJECT_PTR(ce);
  4395. if (ce->trait_aliases) {
  4396. uint32_t i = 0;
  4397. array_init(return_value);
  4398. while (ce->trait_aliases[i]) {
  4399. zend_string *mname;
  4400. zend_trait_method_reference *cur_ref = &ce->trait_aliases[i]->trait_method;
  4401. if (ce->trait_aliases[i]->alias) {
  4402. zend_string *class_name = cur_ref->class_name;
  4403. if (!class_name) {
  4404. uint32_t j = 0;
  4405. zend_string *lcname = zend_string_tolower(cur_ref->method_name);
  4406. for (j = 0; j < ce->num_traits; j++) {
  4407. zend_class_entry *trait =
  4408. zend_hash_find_ptr(CG(class_table), ce->trait_names[j].lc_name);
  4409. ZEND_ASSERT(trait && "Trait must exist");
  4410. if (zend_hash_exists(&trait->function_table, lcname)) {
  4411. class_name = trait->name;
  4412. break;
  4413. }
  4414. }
  4415. zend_string_release_ex(lcname, 0);
  4416. ZEND_ASSERT(class_name != NULL);
  4417. }
  4418. mname = zend_string_alloc(ZSTR_LEN(class_name) + ZSTR_LEN(cur_ref->method_name) + 2, 0);
  4419. snprintf(ZSTR_VAL(mname), ZSTR_LEN(mname) + 1, "%s::%s", ZSTR_VAL(class_name), ZSTR_VAL(cur_ref->method_name));
  4420. add_assoc_str_ex(return_value, ZSTR_VAL(ce->trait_aliases[i]->alias), ZSTR_LEN(ce->trait_aliases[i]->alias), mname);
  4421. }
  4422. i++;
  4423. }
  4424. } else {
  4425. RETURN_EMPTY_ARRAY();
  4426. }
  4427. }
  4428. /* }}} */
  4429. /* {{{ Returns the class' parent class, or, if none exists, FALSE */
  4430. ZEND_METHOD(ReflectionClass, getParentClass)
  4431. {
  4432. reflection_object *intern;
  4433. zend_class_entry *ce;
  4434. if (zend_parse_parameters_none() == FAILURE) {
  4435. RETURN_THROWS();
  4436. }
  4437. GET_REFLECTION_OBJECT_PTR(ce);
  4438. if (ce->parent) {
  4439. zend_reflection_class_factory(ce->parent, return_value);
  4440. } else {
  4441. RETURN_FALSE;
  4442. }
  4443. }
  4444. /* }}} */
  4445. /* {{{ Returns whether this class is a subclass of another class */
  4446. ZEND_METHOD(ReflectionClass, isSubclassOf)
  4447. {
  4448. reflection_object *intern, *argument;
  4449. zend_class_entry *ce, *class_ce;
  4450. zend_string *class_str;
  4451. zend_object *class_obj;
  4452. ZEND_PARSE_PARAMETERS_START(1, 1)
  4453. Z_PARAM_OBJ_OF_CLASS_OR_STR(class_obj, reflection_class_ptr, class_str)
  4454. ZEND_PARSE_PARAMETERS_END();
  4455. if (class_obj) {
  4456. argument = reflection_object_from_obj(class_obj);
  4457. if (argument->ptr == NULL) {
  4458. zend_throw_error(NULL, "Internal error: Failed to retrieve the argument's reflection object");
  4459. RETURN_THROWS();
  4460. }
  4461. class_ce = argument->ptr;
  4462. } else {
  4463. if ((class_ce = zend_lookup_class(class_str)) == NULL) {
  4464. zend_throw_exception_ex(reflection_exception_ptr, 0, "Class \"%s\" does not exist", ZSTR_VAL(class_str));
  4465. RETURN_THROWS();
  4466. }
  4467. }
  4468. GET_REFLECTION_OBJECT_PTR(ce);
  4469. RETURN_BOOL((ce != class_ce && instanceof_function(ce, class_ce)));
  4470. }
  4471. /* }}} */
  4472. /* {{{ Returns whether this class is a subclass of another class */
  4473. ZEND_METHOD(ReflectionClass, implementsInterface)
  4474. {
  4475. reflection_object *intern, *argument;
  4476. zend_string *interface_str;
  4477. zend_class_entry *ce, *interface_ce;
  4478. zend_object *interface_obj;
  4479. ZEND_PARSE_PARAMETERS_START(1, 1)
  4480. Z_PARAM_OBJ_OF_CLASS_OR_STR(interface_obj, reflection_class_ptr, interface_str)
  4481. ZEND_PARSE_PARAMETERS_END();
  4482. if (interface_obj) {
  4483. argument = reflection_object_from_obj(interface_obj);
  4484. if (argument->ptr == NULL) {
  4485. zend_throw_error(NULL, "Internal error: Failed to retrieve the argument's reflection object");
  4486. RETURN_THROWS();
  4487. }
  4488. interface_ce = argument->ptr;
  4489. } else {
  4490. if ((interface_ce = zend_lookup_class(interface_str)) == NULL) {
  4491. zend_throw_exception_ex(reflection_exception_ptr, 0, "Interface \"%s\" does not exist", ZSTR_VAL(interface_str));
  4492. RETURN_THROWS();
  4493. }
  4494. }
  4495. if (!(interface_ce->ce_flags & ZEND_ACC_INTERFACE)) {
  4496. zend_throw_exception_ex(reflection_exception_ptr, 0, "%s is not an interface", ZSTR_VAL(interface_ce->name));
  4497. RETURN_THROWS();
  4498. }
  4499. GET_REFLECTION_OBJECT_PTR(ce);
  4500. RETURN_BOOL(instanceof_function(ce, interface_ce));
  4501. }
  4502. /* }}} */
  4503. /* {{{ Returns whether this class is iterable (can be used inside foreach) */
  4504. ZEND_METHOD(ReflectionClass, isIterable)
  4505. {
  4506. reflection_object *intern;
  4507. zend_class_entry *ce;
  4508. if (zend_parse_parameters_none() == FAILURE) {
  4509. RETURN_THROWS();
  4510. }
  4511. GET_REFLECTION_OBJECT_PTR(ce);
  4512. if (ce->ce_flags & (ZEND_ACC_INTERFACE | ZEND_ACC_IMPLICIT_ABSTRACT_CLASS |
  4513. ZEND_ACC_TRAIT | ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
  4514. RETURN_FALSE;
  4515. }
  4516. RETURN_BOOL(ce->get_iterator || instanceof_function(ce, zend_ce_traversable));
  4517. }
  4518. /* }}} */
  4519. /* {{{ Returns NULL or the extension the class belongs to */
  4520. ZEND_METHOD(ReflectionClass, getExtension)
  4521. {
  4522. reflection_object *intern;
  4523. zend_class_entry *ce;
  4524. if (zend_parse_parameters_none() == FAILURE) {
  4525. RETURN_THROWS();
  4526. }
  4527. GET_REFLECTION_OBJECT_PTR(ce);
  4528. if ((ce->type == ZEND_INTERNAL_CLASS) && ce->info.internal.module) {
  4529. reflection_extension_factory(return_value, ce->info.internal.module->name);
  4530. }
  4531. }
  4532. /* }}} */
  4533. /* {{{ Returns false or the name of the extension the class belongs to */
  4534. ZEND_METHOD(ReflectionClass, getExtensionName)
  4535. {
  4536. reflection_object *intern;
  4537. zend_class_entry *ce;
  4538. if (zend_parse_parameters_none() == FAILURE) {
  4539. RETURN_THROWS();
  4540. }
  4541. GET_REFLECTION_OBJECT_PTR(ce);
  4542. if ((ce->type == ZEND_INTERNAL_CLASS) && ce->info.internal.module) {
  4543. RETURN_STRING(ce->info.internal.module->name);
  4544. } else {
  4545. RETURN_FALSE;
  4546. }
  4547. }
  4548. /* }}} */
  4549. /* {{{ Returns whether this class is defined in namespace */
  4550. ZEND_METHOD(ReflectionClass, inNamespace)
  4551. {
  4552. reflection_object *intern;
  4553. zend_class_entry *ce;
  4554. if (zend_parse_parameters_none() == FAILURE) {
  4555. RETURN_THROWS();
  4556. }
  4557. GET_REFLECTION_OBJECT_PTR(ce);
  4558. zend_string *name = ce->name;
  4559. const char *backslash = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
  4560. RETURN_BOOL(backslash && backslash > ZSTR_VAL(name));
  4561. }
  4562. /* }}} */
  4563. /* {{{ Returns the name of namespace where this class is defined */
  4564. ZEND_METHOD(ReflectionClass, getNamespaceName)
  4565. {
  4566. reflection_object *intern;
  4567. zend_class_entry *ce;
  4568. if (zend_parse_parameters_none() == FAILURE) {
  4569. RETURN_THROWS();
  4570. }
  4571. GET_REFLECTION_OBJECT_PTR(ce);
  4572. zend_string *name = ce->name;
  4573. const char *backslash = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
  4574. if (backslash && backslash > ZSTR_VAL(name)) {
  4575. RETURN_STRINGL(ZSTR_VAL(name), backslash - ZSTR_VAL(name));
  4576. }
  4577. RETURN_EMPTY_STRING();
  4578. }
  4579. /* }}} */
  4580. /* {{{ Returns the short name of the class (without namespace part) */
  4581. ZEND_METHOD(ReflectionClass, getShortName)
  4582. {
  4583. reflection_object *intern;
  4584. zend_class_entry *ce;
  4585. if (zend_parse_parameters_none() == FAILURE) {
  4586. RETURN_THROWS();
  4587. }
  4588. GET_REFLECTION_OBJECT_PTR(ce);
  4589. zend_string *name = ce->name;
  4590. const char *backslash = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
  4591. if (backslash && backslash > ZSTR_VAL(name)) {
  4592. RETURN_STRINGL(backslash + 1, ZSTR_LEN(name) - (backslash - ZSTR_VAL(name) + 1));
  4593. }
  4594. RETURN_STR_COPY(name);
  4595. }
  4596. /* }}} */
  4597. /* {{{ Constructor. Takes an instance as an argument */
  4598. ZEND_METHOD(ReflectionObject, __construct)
  4599. {
  4600. reflection_class_object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  4601. }
  4602. /* }}} */
  4603. /* {{{ Constructor. Throws an Exception in case the given property does not exist */
  4604. ZEND_METHOD(ReflectionProperty, __construct)
  4605. {
  4606. zend_string *classname_str;
  4607. zend_object *classname_obj;
  4608. zend_string *name;
  4609. int dynam_prop = 0;
  4610. zval *object;
  4611. reflection_object *intern;
  4612. zend_class_entry *ce;
  4613. zend_property_info *property_info = NULL;
  4614. property_reference *reference;
  4615. ZEND_PARSE_PARAMETERS_START(2, 2)
  4616. Z_PARAM_OBJ_OR_STR(classname_obj, classname_str)
  4617. Z_PARAM_STR(name)
  4618. ZEND_PARSE_PARAMETERS_END();
  4619. object = ZEND_THIS;
  4620. intern = Z_REFLECTION_P(object);
  4621. if (classname_obj) {
  4622. ce = classname_obj->ce;
  4623. } else {
  4624. if ((ce = zend_lookup_class(classname_str)) == NULL) {
  4625. zend_throw_exception_ex(reflection_exception_ptr, 0, "Class \"%s\" does not exist", ZSTR_VAL(classname_str));
  4626. RETURN_THROWS();
  4627. }
  4628. }
  4629. property_info = zend_hash_find_ptr(&ce->properties_info, name);
  4630. if (property_info == NULL
  4631. || ((property_info->flags & ZEND_ACC_PRIVATE)
  4632. && property_info->ce != ce)) {
  4633. /* Check for dynamic properties */
  4634. if (property_info == NULL && classname_obj) {
  4635. if (zend_hash_exists(classname_obj->handlers->get_properties(classname_obj), name)) {
  4636. dynam_prop = 1;
  4637. }
  4638. }
  4639. if (dynam_prop == 0) {
  4640. zend_throw_exception_ex(reflection_exception_ptr, 0, "Property %s::$%s does not exist", ZSTR_VAL(ce->name), ZSTR_VAL(name));
  4641. RETURN_THROWS();
  4642. }
  4643. }
  4644. ZVAL_STR_COPY(reflection_prop_name(object), name);
  4645. if (dynam_prop == 0) {
  4646. ZVAL_STR_COPY(reflection_prop_class(object), property_info->ce->name);
  4647. } else {
  4648. ZVAL_STR_COPY(reflection_prop_class(object), ce->name);
  4649. }
  4650. reference = (property_reference*) emalloc(sizeof(property_reference));
  4651. reference->prop = dynam_prop ? NULL : property_info;
  4652. reference->unmangled_name = zend_string_copy(name);
  4653. intern->ptr = reference;
  4654. intern->ref_type = REF_TYPE_PROPERTY;
  4655. intern->ce = ce;
  4656. }
  4657. /* }}} */
  4658. /* {{{ Returns a string representation */
  4659. ZEND_METHOD(ReflectionProperty, __toString)
  4660. {
  4661. reflection_object *intern;
  4662. property_reference *ref;
  4663. smart_str str = {0};
  4664. if (zend_parse_parameters_none() == FAILURE) {
  4665. RETURN_THROWS();
  4666. }
  4667. GET_REFLECTION_OBJECT_PTR(ref);
  4668. _property_string(&str, ref->prop, ZSTR_VAL(ref->unmangled_name), "");
  4669. RETURN_STR(smart_str_extract(&str));
  4670. }
  4671. /* }}} */
  4672. /* {{{ Returns the class' name */
  4673. ZEND_METHOD(ReflectionProperty, getName)
  4674. {
  4675. reflection_object *intern;
  4676. property_reference *ref;
  4677. if (zend_parse_parameters_none() == FAILURE) {
  4678. RETURN_THROWS();
  4679. }
  4680. GET_REFLECTION_OBJECT_PTR(ref);
  4681. RETURN_STR_COPY(ref->unmangled_name);
  4682. }
  4683. /* }}} */
  4684. static void _property_check_flag(INTERNAL_FUNCTION_PARAMETERS, int mask) /* {{{ */
  4685. {
  4686. reflection_object *intern;
  4687. property_reference *ref;
  4688. if (zend_parse_parameters_none() == FAILURE) {
  4689. RETURN_THROWS();
  4690. }
  4691. GET_REFLECTION_OBJECT_PTR(ref);
  4692. RETURN_BOOL(prop_get_flags(ref) & mask);
  4693. }
  4694. /* }}} */
  4695. /* {{{ Returns whether this property is public */
  4696. ZEND_METHOD(ReflectionProperty, isPublic)
  4697. {
  4698. _property_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PUBLIC);
  4699. }
  4700. /* }}} */
  4701. /* {{{ Returns whether this property is private */
  4702. ZEND_METHOD(ReflectionProperty, isPrivate)
  4703. {
  4704. _property_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PRIVATE);
  4705. }
  4706. /* }}} */
  4707. /* {{{ Returns whether this property is protected */
  4708. ZEND_METHOD(ReflectionProperty, isProtected)
  4709. {
  4710. _property_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PROTECTED);
  4711. }
  4712. /* }}} */
  4713. /* {{{ Returns whether this property is static */
  4714. ZEND_METHOD(ReflectionProperty, isStatic)
  4715. {
  4716. _property_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_STATIC);
  4717. }
  4718. /* }}} */
  4719. ZEND_METHOD(ReflectionProperty, isReadOnly)
  4720. {
  4721. _property_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_READONLY);
  4722. }
  4723. /* {{{ Returns whether this property is default (declared at compilation time). */
  4724. ZEND_METHOD(ReflectionProperty, isDefault)
  4725. {
  4726. reflection_object *intern;
  4727. property_reference *ref;
  4728. if (zend_parse_parameters_none() == FAILURE) {
  4729. RETURN_THROWS();
  4730. }
  4731. GET_REFLECTION_OBJECT_PTR(ref);
  4732. RETURN_BOOL(ref->prop != NULL);
  4733. }
  4734. /* }}} */
  4735. /* {{{ Returns whether this property has been promoted from a constructor */
  4736. ZEND_METHOD(ReflectionProperty, isPromoted)
  4737. {
  4738. _property_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PROMOTED);
  4739. }
  4740. /* }}} */
  4741. /* {{{ Returns a bitfield of the access modifiers for this property */
  4742. ZEND_METHOD(ReflectionProperty, getModifiers)
  4743. {
  4744. reflection_object *intern;
  4745. property_reference *ref;
  4746. uint32_t keep_flags = ZEND_ACC_PPP_MASK | ZEND_ACC_STATIC | ZEND_ACC_READONLY;
  4747. if (zend_parse_parameters_none() == FAILURE) {
  4748. RETURN_THROWS();
  4749. }
  4750. GET_REFLECTION_OBJECT_PTR(ref);
  4751. RETURN_LONG(prop_get_flags(ref) & keep_flags);
  4752. }
  4753. /* }}} */
  4754. /* {{{ Returns this property's value */
  4755. ZEND_METHOD(ReflectionProperty, getValue)
  4756. {
  4757. reflection_object *intern;
  4758. property_reference *ref;
  4759. zval *object = NULL;
  4760. zval *member_p = NULL;
  4761. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|o!", &object) == FAILURE) {
  4762. RETURN_THROWS();
  4763. }
  4764. GET_REFLECTION_OBJECT_PTR(ref);
  4765. if (prop_get_flags(ref) & ZEND_ACC_STATIC) {
  4766. member_p = zend_read_static_property_ex(intern->ce, ref->unmangled_name, 0);
  4767. if (member_p) {
  4768. RETURN_COPY_DEREF(member_p);
  4769. }
  4770. } else {
  4771. zval rv;
  4772. if (!object) {
  4773. zend_argument_type_error(1, "must be provided for instance properties");
  4774. RETURN_THROWS();
  4775. }
  4776. /* TODO: Should this always use intern->ce? */
  4777. if (!instanceof_function(Z_OBJCE_P(object), ref->prop ? ref->prop->ce : intern->ce)) {
  4778. _DO_THROW("Given object is not an instance of the class this property was declared in");
  4779. RETURN_THROWS();
  4780. }
  4781. member_p = zend_read_property_ex(intern->ce, Z_OBJ_P(object), ref->unmangled_name, 0, &rv);
  4782. if (member_p != &rv) {
  4783. RETURN_COPY_DEREF(member_p);
  4784. } else {
  4785. if (Z_ISREF_P(member_p)) {
  4786. zend_unwrap_reference(member_p);
  4787. }
  4788. RETURN_COPY_VALUE(member_p);
  4789. }
  4790. }
  4791. }
  4792. /* }}} */
  4793. /* {{{ Sets this property's value */
  4794. ZEND_METHOD(ReflectionProperty, setValue)
  4795. {
  4796. reflection_object *intern;
  4797. property_reference *ref;
  4798. zval *object;
  4799. zval *value;
  4800. zval *tmp;
  4801. GET_REFLECTION_OBJECT_PTR(ref);
  4802. if (prop_get_flags(ref) & ZEND_ACC_STATIC) {
  4803. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "z", &value) == FAILURE) {
  4804. if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &tmp, &value) == FAILURE) {
  4805. RETURN_THROWS();
  4806. }
  4807. }
  4808. zend_update_static_property_ex(intern->ce, ref->unmangled_name, value);
  4809. } else {
  4810. if (zend_parse_parameters(ZEND_NUM_ARGS(), "oz", &object, &value) == FAILURE) {
  4811. RETURN_THROWS();
  4812. }
  4813. zend_update_property_ex(intern->ce, Z_OBJ_P(object), ref->unmangled_name, value);
  4814. }
  4815. }
  4816. /* }}} */
  4817. /* {{{ Returns this property's value */
  4818. ZEND_METHOD(ReflectionProperty, isInitialized)
  4819. {
  4820. reflection_object *intern;
  4821. property_reference *ref;
  4822. zval *object = NULL;
  4823. zval *member_p = NULL;
  4824. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|o!", &object) == FAILURE) {
  4825. RETURN_THROWS();
  4826. }
  4827. GET_REFLECTION_OBJECT_PTR(ref);
  4828. if (prop_get_flags(ref) & ZEND_ACC_STATIC) {
  4829. member_p = zend_read_static_property_ex(intern->ce, ref->unmangled_name, 1);
  4830. if (member_p) {
  4831. RETURN_BOOL(!Z_ISUNDEF_P(member_p));
  4832. }
  4833. RETURN_FALSE;
  4834. } else {
  4835. zend_class_entry *old_scope;
  4836. int retval;
  4837. if (!object) {
  4838. zend_argument_type_error(1, "must be provided for instance properties");
  4839. RETURN_THROWS();
  4840. }
  4841. /* TODO: Should this always use intern->ce? */
  4842. if (!instanceof_function(Z_OBJCE_P(object), ref->prop ? ref->prop->ce : intern->ce)) {
  4843. _DO_THROW("Given object is not an instance of the class this property was declared in");
  4844. RETURN_THROWS();
  4845. }
  4846. old_scope = EG(fake_scope);
  4847. EG(fake_scope) = intern->ce;
  4848. retval = Z_OBJ_HT_P(object)->has_property(Z_OBJ_P(object), ref->unmangled_name, ZEND_PROPERTY_EXISTS, NULL);
  4849. EG(fake_scope) = old_scope;
  4850. RETVAL_BOOL(retval);
  4851. }
  4852. }
  4853. /* }}} */
  4854. /* {{{ Get the declaring class */
  4855. ZEND_METHOD(ReflectionProperty, getDeclaringClass)
  4856. {
  4857. reflection_object *intern;
  4858. property_reference *ref;
  4859. zend_class_entry *ce;
  4860. if (zend_parse_parameters_none() == FAILURE) {
  4861. RETURN_THROWS();
  4862. }
  4863. GET_REFLECTION_OBJECT_PTR(ref);
  4864. ce = ref->prop ? ref->prop->ce : intern->ce;
  4865. zend_reflection_class_factory(ce, return_value);
  4866. }
  4867. /* }}} */
  4868. /* {{{ Returns the doc comment for this property */
  4869. ZEND_METHOD(ReflectionProperty, getDocComment)
  4870. {
  4871. reflection_object *intern;
  4872. property_reference *ref;
  4873. if (zend_parse_parameters_none() == FAILURE) {
  4874. RETURN_THROWS();
  4875. }
  4876. GET_REFLECTION_OBJECT_PTR(ref);
  4877. if (ref->prop && ref->prop->doc_comment) {
  4878. RETURN_STR_COPY(ref->prop->doc_comment);
  4879. }
  4880. RETURN_FALSE;
  4881. }
  4882. /* }}} */
  4883. /* {{{ Returns the attributes of this property */
  4884. ZEND_METHOD(ReflectionProperty, getAttributes)
  4885. {
  4886. reflection_object *intern;
  4887. property_reference *ref;
  4888. GET_REFLECTION_OBJECT_PTR(ref);
  4889. if (ref->prop == NULL) {
  4890. RETURN_EMPTY_ARRAY();
  4891. }
  4892. reflect_attributes(INTERNAL_FUNCTION_PARAM_PASSTHRU,
  4893. ref->prop->attributes, 0, ref->prop->ce, ZEND_ATTRIBUTE_TARGET_PROPERTY,
  4894. ref->prop->ce->type == ZEND_USER_CLASS ? ref->prop->ce->info.user.filename : NULL);
  4895. }
  4896. /* }}} */
  4897. /* {{{ Sets whether non-public properties can be requested */
  4898. ZEND_METHOD(ReflectionProperty, setAccessible)
  4899. {
  4900. bool visible;
  4901. if (zend_parse_parameters(ZEND_NUM_ARGS(), "b", &visible) == FAILURE) {
  4902. RETURN_THROWS();
  4903. }
  4904. }
  4905. /* }}} */
  4906. /* {{{ Returns the type associated with the property */
  4907. ZEND_METHOD(ReflectionProperty, getType)
  4908. {
  4909. reflection_object *intern;
  4910. property_reference *ref;
  4911. if (zend_parse_parameters_none() == FAILURE) {
  4912. RETURN_THROWS();
  4913. }
  4914. GET_REFLECTION_OBJECT_PTR(ref);
  4915. if (!ref->prop || !ZEND_TYPE_IS_SET(ref->prop->type)) {
  4916. RETURN_NULL();
  4917. }
  4918. reflection_type_factory(ref->prop->type, return_value, 1);
  4919. }
  4920. /* }}} */
  4921. /* {{{ Returns whether property has a type */
  4922. ZEND_METHOD(ReflectionProperty, hasType)
  4923. {
  4924. reflection_object *intern;
  4925. property_reference *ref;
  4926. if (zend_parse_parameters_none() == FAILURE) {
  4927. RETURN_THROWS();
  4928. }
  4929. GET_REFLECTION_OBJECT_PTR(ref);
  4930. RETVAL_BOOL(ref->prop && ZEND_TYPE_IS_SET(ref->prop->type));
  4931. }
  4932. /* }}} */
  4933. /* {{{ Returns whether property has a default value */
  4934. ZEND_METHOD(ReflectionProperty, hasDefaultValue)
  4935. {
  4936. reflection_object *intern;
  4937. property_reference *ref;
  4938. zend_property_info *prop_info;
  4939. zval *prop;
  4940. if (zend_parse_parameters_none() == FAILURE) {
  4941. RETURN_THROWS();
  4942. }
  4943. GET_REFLECTION_OBJECT_PTR(ref);
  4944. prop_info = ref->prop;
  4945. if (prop_info == NULL) {
  4946. RETURN_FALSE;
  4947. }
  4948. prop = property_get_default(prop_info);
  4949. RETURN_BOOL(!Z_ISUNDEF_P(prop));
  4950. }
  4951. /* }}} */
  4952. /* {{{ Returns the default value of a property */
  4953. ZEND_METHOD(ReflectionProperty, getDefaultValue)
  4954. {
  4955. reflection_object *intern;
  4956. property_reference *ref;
  4957. zend_property_info *prop_info;
  4958. zval *prop;
  4959. if (zend_parse_parameters_none() == FAILURE) {
  4960. RETURN_THROWS();
  4961. }
  4962. GET_REFLECTION_OBJECT_PTR(ref);
  4963. prop_info = ref->prop;
  4964. if (prop_info == NULL) {
  4965. return; // throw exception?
  4966. }
  4967. prop = property_get_default(prop_info);
  4968. if (Z_ISUNDEF_P(prop)) {
  4969. return;
  4970. }
  4971. /* copy: enforce read only access */
  4972. ZVAL_DEREF(prop);
  4973. ZVAL_COPY_OR_DUP(return_value, prop);
  4974. /* this is necessary to make it able to work with default array
  4975. * properties, returned to user */
  4976. if (Z_TYPE_P(return_value) == IS_CONSTANT_AST) {
  4977. if (UNEXPECTED(zval_update_constant_ex(return_value, prop_info->ce) != SUCCESS)) {
  4978. RETURN_THROWS();
  4979. }
  4980. }
  4981. }
  4982. /* }}} */
  4983. /* {{{ Constructor. Throws an Exception in case the given extension does not exist */
  4984. ZEND_METHOD(ReflectionExtension, __construct)
  4985. {
  4986. zval *object;
  4987. char *lcname;
  4988. reflection_object *intern;
  4989. zend_module_entry *module;
  4990. char *name_str;
  4991. size_t name_len;
  4992. ALLOCA_FLAG(use_heap)
  4993. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name_str, &name_len) == FAILURE) {
  4994. RETURN_THROWS();
  4995. }
  4996. object = ZEND_THIS;
  4997. intern = Z_REFLECTION_P(object);
  4998. lcname = do_alloca(name_len + 1, use_heap);
  4999. zend_str_tolower_copy(lcname, name_str, name_len);
  5000. if ((module = zend_hash_str_find_ptr(&module_registry, lcname, name_len)) == NULL) {
  5001. free_alloca(lcname, use_heap);
  5002. zend_throw_exception_ex(reflection_exception_ptr, 0,
  5003. "Extension \"%s\" does not exist", name_str);
  5004. RETURN_THROWS();
  5005. }
  5006. free_alloca(lcname, use_heap);
  5007. ZVAL_STRING(reflection_prop_name(object), module->name);
  5008. intern->ptr = module;
  5009. intern->ref_type = REF_TYPE_OTHER;
  5010. intern->ce = NULL;
  5011. }
  5012. /* }}} */
  5013. /* {{{ Returns a string representation */
  5014. ZEND_METHOD(ReflectionExtension, __toString)
  5015. {
  5016. reflection_object *intern;
  5017. zend_module_entry *module;
  5018. smart_str str = {0};
  5019. if (zend_parse_parameters_none() == FAILURE) {
  5020. RETURN_THROWS();
  5021. }
  5022. GET_REFLECTION_OBJECT_PTR(module);
  5023. _extension_string(&str, module, "");
  5024. RETURN_STR(smart_str_extract(&str));
  5025. }
  5026. /* }}} */
  5027. /* {{{ Returns this extension's name */
  5028. ZEND_METHOD(ReflectionExtension, getName)
  5029. {
  5030. reflection_object *intern;
  5031. zend_module_entry *module;
  5032. if (zend_parse_parameters_none() == FAILURE) {
  5033. RETURN_THROWS();
  5034. }
  5035. GET_REFLECTION_OBJECT_PTR(module);
  5036. RETURN_STRING(module->name);
  5037. }
  5038. /* }}} */
  5039. /* {{{ Returns this extension's version */
  5040. ZEND_METHOD(ReflectionExtension, getVersion)
  5041. {
  5042. reflection_object *intern;
  5043. zend_module_entry *module;
  5044. if (zend_parse_parameters_none() == FAILURE) {
  5045. RETURN_THROWS();
  5046. }
  5047. GET_REFLECTION_OBJECT_PTR(module);
  5048. /* An extension does not necessarily have a version number */
  5049. if (module->version == NO_VERSION_YET) {
  5050. RETURN_NULL();
  5051. } else {
  5052. RETURN_STRING(module->version);
  5053. }
  5054. }
  5055. /* }}} */
  5056. /* {{{ Returns an array of this extension's functions */
  5057. ZEND_METHOD(ReflectionExtension, getFunctions)
  5058. {
  5059. reflection_object *intern;
  5060. zend_module_entry *module;
  5061. zval function;
  5062. zend_function *fptr;
  5063. if (zend_parse_parameters_none() == FAILURE) {
  5064. RETURN_THROWS();
  5065. }
  5066. GET_REFLECTION_OBJECT_PTR(module);
  5067. array_init(return_value);
  5068. ZEND_HASH_FOREACH_PTR(CG(function_table), fptr) {
  5069. if (fptr->common.type==ZEND_INTERNAL_FUNCTION
  5070. && fptr->internal_function.module == module) {
  5071. reflection_function_factory(fptr, NULL, &function);
  5072. zend_hash_update(Z_ARRVAL_P(return_value), fptr->common.function_name, &function);
  5073. }
  5074. } ZEND_HASH_FOREACH_END();
  5075. }
  5076. /* }}} */
  5077. /* {{{ Returns an associative array containing this extension's constants and their values */
  5078. ZEND_METHOD(ReflectionExtension, getConstants)
  5079. {
  5080. reflection_object *intern;
  5081. zend_module_entry *module;
  5082. zend_constant *constant;
  5083. if (zend_parse_parameters_none() == FAILURE) {
  5084. RETURN_THROWS();
  5085. }
  5086. GET_REFLECTION_OBJECT_PTR(module);
  5087. array_init(return_value);
  5088. ZEND_HASH_FOREACH_PTR(EG(zend_constants), constant) {
  5089. if (module->module_number == ZEND_CONSTANT_MODULE_NUMBER(constant)) {
  5090. zval const_val;
  5091. ZVAL_COPY_OR_DUP(&const_val, &constant->value);
  5092. zend_hash_update(Z_ARRVAL_P(return_value), constant->name, &const_val);
  5093. }
  5094. } ZEND_HASH_FOREACH_END();
  5095. }
  5096. /* }}} */
  5097. /* {{{ _addinientry */
  5098. static void _addinientry(zend_ini_entry *ini_entry, zval *retval, int number)
  5099. {
  5100. if (number == ini_entry->module_number) {
  5101. zval zv;
  5102. if (ini_entry->value) {
  5103. ZVAL_STR_COPY(&zv, ini_entry->value);
  5104. } else {
  5105. ZVAL_NULL(&zv);
  5106. }
  5107. zend_symtable_update(Z_ARRVAL_P(retval), ini_entry->name, &zv);
  5108. }
  5109. }
  5110. /* }}} */
  5111. /* {{{ Returns an associative array containing this extension's INI entries and their values */
  5112. ZEND_METHOD(ReflectionExtension, getINIEntries)
  5113. {
  5114. reflection_object *intern;
  5115. zend_module_entry *module;
  5116. zend_ini_entry *ini_entry;
  5117. if (zend_parse_parameters_none() == FAILURE) {
  5118. RETURN_THROWS();
  5119. }
  5120. GET_REFLECTION_OBJECT_PTR(module);
  5121. array_init(return_value);
  5122. ZEND_HASH_FOREACH_PTR(EG(ini_directives), ini_entry) {
  5123. _addinientry(ini_entry, return_value, module->module_number);
  5124. } ZEND_HASH_FOREACH_END();
  5125. }
  5126. /* }}} */
  5127. /* {{{ add_extension_class */
  5128. static void add_extension_class(zend_class_entry *ce, zend_string *key, zval *class_array, zend_module_entry *module, bool add_reflection_class)
  5129. {
  5130. if (ce->type == ZEND_INTERNAL_CLASS && ce->info.internal.module && !strcasecmp(ce->info.internal.module->name, module->name)) {
  5131. zend_string *name;
  5132. if (!zend_string_equals_ci(ce->name, key)) {
  5133. /* This is a class alias, use alias name */
  5134. name = key;
  5135. } else {
  5136. /* Use class name */
  5137. name = ce->name;
  5138. }
  5139. if (add_reflection_class) {
  5140. zval zclass;
  5141. zend_reflection_class_factory(ce, &zclass);
  5142. zend_hash_update(Z_ARRVAL_P(class_array), name, &zclass);
  5143. } else {
  5144. add_next_index_str(class_array, zend_string_copy(name));
  5145. }
  5146. }
  5147. }
  5148. /* }}} */
  5149. /* {{{ Returns an array containing ReflectionClass objects for all classes of this extension */
  5150. ZEND_METHOD(ReflectionExtension, getClasses)
  5151. {
  5152. reflection_object *intern;
  5153. zend_module_entry *module;
  5154. zend_string *key;
  5155. zend_class_entry *ce;
  5156. if (zend_parse_parameters_none() == FAILURE) {
  5157. RETURN_THROWS();
  5158. }
  5159. GET_REFLECTION_OBJECT_PTR(module);
  5160. array_init(return_value);
  5161. ZEND_HASH_FOREACH_STR_KEY_PTR(EG(class_table), key, ce) {
  5162. add_extension_class(ce, key, return_value, module, 1);
  5163. } ZEND_HASH_FOREACH_END();
  5164. }
  5165. /* }}} */
  5166. /* {{{ Returns an array containing all names of all classes of this extension */
  5167. ZEND_METHOD(ReflectionExtension, getClassNames)
  5168. {
  5169. reflection_object *intern;
  5170. zend_module_entry *module;
  5171. zend_string *key;
  5172. zend_class_entry *ce;
  5173. if (zend_parse_parameters_none() == FAILURE) {
  5174. RETURN_THROWS();
  5175. }
  5176. GET_REFLECTION_OBJECT_PTR(module);
  5177. array_init(return_value);
  5178. ZEND_HASH_FOREACH_STR_KEY_PTR(EG(class_table), key, ce) {
  5179. add_extension_class(ce, key, return_value, module, 0);
  5180. } ZEND_HASH_FOREACH_END();
  5181. }
  5182. /* }}} */
  5183. /* {{{ Returns an array containing all names of all extensions this extension depends on */
  5184. ZEND_METHOD(ReflectionExtension, getDependencies)
  5185. {
  5186. reflection_object *intern;
  5187. zend_module_entry *module;
  5188. const zend_module_dep *dep;
  5189. if (zend_parse_parameters_none() == FAILURE) {
  5190. RETURN_THROWS();
  5191. }
  5192. GET_REFLECTION_OBJECT_PTR(module);
  5193. dep = module->deps;
  5194. if (!dep)
  5195. {
  5196. RETURN_EMPTY_ARRAY();
  5197. }
  5198. array_init(return_value);
  5199. while(dep->name) {
  5200. zend_string *relation;
  5201. char *rel_type;
  5202. size_t len = 0;
  5203. switch(dep->type) {
  5204. case MODULE_DEP_REQUIRED:
  5205. rel_type = "Required";
  5206. len += sizeof("Required") - 1;
  5207. break;
  5208. case MODULE_DEP_CONFLICTS:
  5209. rel_type = "Conflicts";
  5210. len += sizeof("Conflicts") - 1;
  5211. break;
  5212. case MODULE_DEP_OPTIONAL:
  5213. rel_type = "Optional";
  5214. len += sizeof("Optional") - 1;
  5215. break;
  5216. default:
  5217. rel_type = "Error"; /* shouldn't happen */
  5218. len += sizeof("Error") - 1;
  5219. break;
  5220. }
  5221. if (dep->rel) {
  5222. len += strlen(dep->rel) + 1;
  5223. }
  5224. if (dep->version) {
  5225. len += strlen(dep->version) + 1;
  5226. }
  5227. relation = zend_string_alloc(len, 0);
  5228. snprintf(ZSTR_VAL(relation), ZSTR_LEN(relation) + 1, "%s%s%s%s%s",
  5229. rel_type,
  5230. dep->rel ? " " : "",
  5231. dep->rel ? dep->rel : "",
  5232. dep->version ? " " : "",
  5233. dep->version ? dep->version : "");
  5234. add_assoc_str(return_value, dep->name, relation);
  5235. dep++;
  5236. }
  5237. }
  5238. /* }}} */
  5239. /* {{{ Prints phpinfo block for the extension */
  5240. ZEND_METHOD(ReflectionExtension, info)
  5241. {
  5242. reflection_object *intern;
  5243. zend_module_entry *module;
  5244. if (zend_parse_parameters_none() == FAILURE) {
  5245. RETURN_THROWS();
  5246. }
  5247. GET_REFLECTION_OBJECT_PTR(module);
  5248. php_info_print_module(module);
  5249. }
  5250. /* }}} */
  5251. /* {{{ Returns whether this extension is persistent */
  5252. ZEND_METHOD(ReflectionExtension, isPersistent)
  5253. {
  5254. reflection_object *intern;
  5255. zend_module_entry *module;
  5256. if (zend_parse_parameters_none() == FAILURE) {
  5257. RETURN_THROWS();
  5258. }
  5259. GET_REFLECTION_OBJECT_PTR(module);
  5260. RETURN_BOOL(module->type == MODULE_PERSISTENT);
  5261. }
  5262. /* }}} */
  5263. /* {{{ Returns whether this extension is temporary */
  5264. ZEND_METHOD(ReflectionExtension, isTemporary)
  5265. {
  5266. reflection_object *intern;
  5267. zend_module_entry *module;
  5268. if (zend_parse_parameters_none() == FAILURE) {
  5269. RETURN_THROWS();
  5270. }
  5271. GET_REFLECTION_OBJECT_PTR(module);
  5272. RETURN_BOOL(module->type == MODULE_TEMPORARY);
  5273. }
  5274. /* }}} */
  5275. /* {{{ Constructor. Throws an Exception in case the given Zend extension does not exist */
  5276. ZEND_METHOD(ReflectionZendExtension, __construct)
  5277. {
  5278. zval *object;
  5279. reflection_object *intern;
  5280. zend_extension *extension;
  5281. char *name_str;
  5282. size_t name_len;
  5283. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name_str, &name_len) == FAILURE) {
  5284. RETURN_THROWS();
  5285. }
  5286. object = ZEND_THIS;
  5287. intern = Z_REFLECTION_P(object);
  5288. extension = zend_get_extension(name_str);
  5289. if (!extension) {
  5290. zend_throw_exception_ex(reflection_exception_ptr, 0,
  5291. "Zend Extension \"%s\" does not exist", name_str);
  5292. RETURN_THROWS();
  5293. }
  5294. ZVAL_STRING(reflection_prop_name(object), extension->name);
  5295. intern->ptr = extension;
  5296. intern->ref_type = REF_TYPE_OTHER;
  5297. intern->ce = NULL;
  5298. }
  5299. /* }}} */
  5300. /* {{{ Returns a string representation */
  5301. ZEND_METHOD(ReflectionZendExtension, __toString)
  5302. {
  5303. reflection_object *intern;
  5304. zend_extension *extension;
  5305. smart_str str = {0};
  5306. if (zend_parse_parameters_none() == FAILURE) {
  5307. RETURN_THROWS();
  5308. }
  5309. GET_REFLECTION_OBJECT_PTR(extension);
  5310. _zend_extension_string(&str, extension, "");
  5311. RETURN_STR(smart_str_extract(&str));
  5312. }
  5313. /* }}} */
  5314. /* {{{ Returns the name of this Zend extension */
  5315. ZEND_METHOD(ReflectionZendExtension, getName)
  5316. {
  5317. reflection_object *intern;
  5318. zend_extension *extension;
  5319. if (zend_parse_parameters_none() == FAILURE) {
  5320. RETURN_THROWS();
  5321. }
  5322. GET_REFLECTION_OBJECT_PTR(extension);
  5323. RETURN_STRING(extension->name);
  5324. }
  5325. /* }}} */
  5326. /* {{{ Returns the version information of this Zend extension */
  5327. ZEND_METHOD(ReflectionZendExtension, getVersion)
  5328. {
  5329. reflection_object *intern;
  5330. zend_extension *extension;
  5331. if (zend_parse_parameters_none() == FAILURE) {
  5332. RETURN_THROWS();
  5333. }
  5334. GET_REFLECTION_OBJECT_PTR(extension);
  5335. if (extension->version) {
  5336. RETURN_STRING(extension->version);
  5337. } else {
  5338. RETURN_EMPTY_STRING();
  5339. }
  5340. }
  5341. /* }}} */
  5342. /* {{{ Returns the name of this Zend extension's author */
  5343. ZEND_METHOD(ReflectionZendExtension, getAuthor)
  5344. {
  5345. reflection_object *intern;
  5346. zend_extension *extension;
  5347. if (zend_parse_parameters_none() == FAILURE) {
  5348. RETURN_THROWS();
  5349. }
  5350. GET_REFLECTION_OBJECT_PTR(extension);
  5351. if (extension->author) {
  5352. RETURN_STRING(extension->author);
  5353. } else {
  5354. RETURN_EMPTY_STRING();
  5355. }
  5356. }
  5357. /* }}} */
  5358. /* {{{ Returns this Zend extension's URL*/
  5359. ZEND_METHOD(ReflectionZendExtension, getURL)
  5360. {
  5361. reflection_object *intern;
  5362. zend_extension *extension;
  5363. if (zend_parse_parameters_none() == FAILURE) {
  5364. RETURN_THROWS();
  5365. }
  5366. GET_REFLECTION_OBJECT_PTR(extension);
  5367. if (extension->URL) {
  5368. RETURN_STRING(extension->URL);
  5369. } else {
  5370. RETURN_EMPTY_STRING();
  5371. }
  5372. }
  5373. /* }}} */
  5374. /* {{{ Returns this Zend extension's copyright information */
  5375. ZEND_METHOD(ReflectionZendExtension, getCopyright)
  5376. {
  5377. reflection_object *intern;
  5378. zend_extension *extension;
  5379. if (zend_parse_parameters_none() == FAILURE) {
  5380. RETURN_THROWS();
  5381. }
  5382. GET_REFLECTION_OBJECT_PTR(extension);
  5383. if (extension->copyright) {
  5384. RETURN_STRING(extension->copyright);
  5385. } else {
  5386. RETURN_EMPTY_STRING();
  5387. }
  5388. }
  5389. /* }}} */
  5390. /* {{{ Dummy constructor -- always throws ReflectionExceptions. */
  5391. ZEND_METHOD(ReflectionReference, __construct)
  5392. {
  5393. _DO_THROW(
  5394. "Cannot directly instantiate ReflectionReference. "
  5395. "Use ReflectionReference::fromArrayElement() instead"
  5396. );
  5397. }
  5398. /* }}} */
  5399. static bool is_ignorable_reference(HashTable *ht, zval *ref) {
  5400. if (Z_REFCOUNT_P(ref) != 1) {
  5401. return 0;
  5402. }
  5403. /* Directly self-referential arrays are treated as proper references
  5404. * in zend_array_dup() despite rc=1. */
  5405. return Z_TYPE_P(Z_REFVAL_P(ref)) != IS_ARRAY || Z_ARRVAL_P(Z_REFVAL_P(ref)) != ht;
  5406. }
  5407. /* {{{ Create ReflectionReference for array item. Returns null if not a reference. */
  5408. ZEND_METHOD(ReflectionReference, fromArrayElement)
  5409. {
  5410. HashTable *ht;
  5411. zval *item;
  5412. zend_string *string_key = NULL;
  5413. zend_long int_key = 0;
  5414. reflection_object *intern;
  5415. ZEND_PARSE_PARAMETERS_START(2, 2)
  5416. Z_PARAM_ARRAY_HT(ht)
  5417. Z_PARAM_STR_OR_LONG(string_key, int_key)
  5418. ZEND_PARSE_PARAMETERS_END();
  5419. if (string_key) {
  5420. item = zend_hash_find(ht, string_key);
  5421. } else {
  5422. item = zend_hash_index_find(ht, int_key);
  5423. }
  5424. if (!item) {
  5425. _DO_THROW("Array key not found");
  5426. RETURN_THROWS();
  5427. }
  5428. if (Z_TYPE_P(item) != IS_REFERENCE || is_ignorable_reference(ht, item)) {
  5429. RETURN_NULL();
  5430. }
  5431. object_init_ex(return_value, reflection_reference_ptr);
  5432. intern = Z_REFLECTION_P(return_value);
  5433. ZVAL_COPY(&intern->obj, item);
  5434. intern->ref_type = REF_TYPE_OTHER;
  5435. }
  5436. /* }}} */
  5437. /* {{{ Returns a unique identifier for the reference.
  5438. * The format of the return value is unspecified and may change. */
  5439. ZEND_METHOD(ReflectionReference, getId)
  5440. {
  5441. reflection_object *intern;
  5442. unsigned char digest[20];
  5443. PHP_SHA1_CTX context;
  5444. if (zend_parse_parameters_none() == FAILURE) {
  5445. RETURN_THROWS();
  5446. }
  5447. intern = Z_REFLECTION_P(getThis());
  5448. if (Z_TYPE(intern->obj) != IS_REFERENCE) {
  5449. _DO_THROW("Corrupted ReflectionReference object");
  5450. RETURN_THROWS();
  5451. }
  5452. if (!REFLECTION_G(key_initialized)) {
  5453. if (php_random_bytes_throw(&REFLECTION_G(key), 16) == FAILURE) {
  5454. RETURN_THROWS();
  5455. }
  5456. REFLECTION_G(key_initialized) = 1;
  5457. }
  5458. /* SHA1(ref || key) to avoid directly exposing memory addresses. */
  5459. PHP_SHA1Init(&context);
  5460. PHP_SHA1Update(&context, (unsigned char *) &Z_REF(intern->obj), sizeof(zend_reference *));
  5461. PHP_SHA1Update(&context, REFLECTION_G(key), REFLECTION_KEY_LEN);
  5462. PHP_SHA1Final(digest, &context);
  5463. RETURN_STRINGL((char *) digest, sizeof(digest));
  5464. }
  5465. /* }}} */
  5466. ZEND_METHOD(ReflectionAttribute, __construct)
  5467. {
  5468. }
  5469. ZEND_METHOD(ReflectionAttribute, __clone)
  5470. {
  5471. /* Should never be executable */
  5472. _DO_THROW("Cannot clone object using __clone()");
  5473. }
  5474. /* {{{ Returns a string representation */
  5475. ZEND_METHOD(ReflectionAttribute, __toString)
  5476. {
  5477. reflection_object *intern;
  5478. attribute_reference *attr;
  5479. if (zend_parse_parameters_none() == FAILURE) {
  5480. RETURN_THROWS();
  5481. }
  5482. GET_REFLECTION_OBJECT_PTR(attr);
  5483. smart_str str = {0};
  5484. smart_str_appends(&str, "Attribute [ ");
  5485. smart_str_append(&str, attr->data->name);
  5486. smart_str_appends(&str, " ]");
  5487. if (attr->data->argc > 0) {
  5488. smart_str_appends(&str, " {\n");
  5489. smart_str_append_printf(&str, " - Arguments [%d] {\n", attr->data->argc);
  5490. for (uint32_t i = 0; i < attr->data->argc; i++) {
  5491. smart_str_append_printf(&str, " Argument #%d [ ", i);
  5492. if (attr->data->args[i].name != NULL) {
  5493. smart_str_append(&str, attr->data->args[i].name);
  5494. smart_str_appends(&str, " = ");
  5495. }
  5496. if (format_default_value(&str, &attr->data->args[i].value) == FAILURE) {
  5497. smart_str_free(&str);
  5498. RETURN_THROWS();
  5499. }
  5500. smart_str_appends(&str, " ]\n");
  5501. }
  5502. smart_str_appends(&str, " }\n");
  5503. smart_str_appends(&str, "}\n");
  5504. } else {
  5505. smart_str_appendc(&str, '\n');
  5506. }
  5507. RETURN_STR(smart_str_extract(&str));
  5508. }
  5509. /* }}} */
  5510. /* {{{ * Returns the name of the attribute */
  5511. ZEND_METHOD(ReflectionAttribute, getName)
  5512. {
  5513. reflection_object *intern;
  5514. attribute_reference *attr;
  5515. if (zend_parse_parameters_none() == FAILURE) {
  5516. RETURN_THROWS();
  5517. }
  5518. GET_REFLECTION_OBJECT_PTR(attr);
  5519. RETURN_STR_COPY(attr->data->name);
  5520. }
  5521. /* }}} */
  5522. /* {{{ * Returns the target of the attribute */
  5523. ZEND_METHOD(ReflectionAttribute, getTarget)
  5524. {
  5525. reflection_object *intern;
  5526. attribute_reference *attr;
  5527. if (zend_parse_parameters_none() == FAILURE) {
  5528. RETURN_THROWS();
  5529. }
  5530. GET_REFLECTION_OBJECT_PTR(attr);
  5531. RETURN_LONG(attr->target);
  5532. }
  5533. /* }}} */
  5534. /* {{{ * Returns true if the attribute is repeated */
  5535. ZEND_METHOD(ReflectionAttribute, isRepeated)
  5536. {
  5537. reflection_object *intern;
  5538. attribute_reference *attr;
  5539. if (zend_parse_parameters_none() == FAILURE) {
  5540. RETURN_THROWS();
  5541. }
  5542. GET_REFLECTION_OBJECT_PTR(attr);
  5543. RETURN_BOOL(zend_is_attribute_repeated(attr->attributes, attr->data));
  5544. }
  5545. /* }}} */
  5546. /* {{{ * Returns the arguments passed to the attribute */
  5547. ZEND_METHOD(ReflectionAttribute, getArguments)
  5548. {
  5549. reflection_object *intern;
  5550. attribute_reference *attr;
  5551. zval tmp;
  5552. uint32_t i;
  5553. if (zend_parse_parameters_none() == FAILURE) {
  5554. RETURN_THROWS();
  5555. }
  5556. GET_REFLECTION_OBJECT_PTR(attr);
  5557. array_init(return_value);
  5558. for (i = 0; i < attr->data->argc; i++) {
  5559. if (FAILURE == zend_get_attribute_value(&tmp, attr->data, i, attr->scope)) {
  5560. RETURN_THROWS();
  5561. }
  5562. if (attr->data->args[i].name) {
  5563. /* We ensured at compile-time that there are no duplicate parameter names. */
  5564. zend_hash_add_new(Z_ARRVAL_P(return_value), attr->data->args[i].name, &tmp);
  5565. } else {
  5566. add_next_index_zval(return_value, &tmp);
  5567. }
  5568. }
  5569. }
  5570. /* }}} */
  5571. static int call_attribute_constructor(
  5572. zend_attribute *attr, zend_class_entry *ce, zend_object *obj,
  5573. zval *args, uint32_t argc, HashTable *named_params, zend_string *filename)
  5574. {
  5575. zend_function *ctor = ce->constructor;
  5576. zend_execute_data *call = NULL;
  5577. ZEND_ASSERT(ctor != NULL);
  5578. if (!(ctor->common.fn_flags & ZEND_ACC_PUBLIC)) {
  5579. zend_throw_error(NULL, "Attribute constructor of class %s must be public", ZSTR_VAL(ce->name));
  5580. return FAILURE;
  5581. }
  5582. if (filename) {
  5583. /* Set up dummy call frame that makes it look like the attribute was invoked
  5584. * from where it occurs in the code. */
  5585. zend_function dummy_func;
  5586. zend_op *opline;
  5587. memset(&dummy_func, 0, sizeof(zend_function));
  5588. call = zend_vm_stack_push_call_frame_ex(
  5589. ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_execute_data), sizeof(zval)) +
  5590. ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_op), sizeof(zval)) +
  5591. ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_function), sizeof(zval)),
  5592. 0, &dummy_func, 0, NULL);
  5593. opline = (zend_op*)(call + 1);
  5594. memset(opline, 0, sizeof(zend_op));
  5595. opline->opcode = ZEND_DO_FCALL;
  5596. opline->lineno = attr->lineno;
  5597. call->opline = opline;
  5598. call->call = NULL;
  5599. call->return_value = NULL;
  5600. call->func = (zend_function*)(call->opline + 1);
  5601. call->prev_execute_data = EG(current_execute_data);
  5602. memset(call->func, 0, sizeof(zend_function));
  5603. call->func->type = ZEND_USER_FUNCTION;
  5604. call->func->op_array.fn_flags =
  5605. attr->flags & ZEND_ATTRIBUTE_STRICT_TYPES ? ZEND_ACC_STRICT_TYPES : 0;
  5606. call->func->op_array.fn_flags |= ZEND_ACC_CALL_VIA_TRAMPOLINE;
  5607. call->func->op_array.filename = filename;
  5608. EG(current_execute_data) = call;
  5609. }
  5610. zend_call_known_function(ctor, obj, obj->ce, NULL, argc, args, named_params);
  5611. if (filename) {
  5612. EG(current_execute_data) = call->prev_execute_data;
  5613. zend_vm_stack_free_call_frame(call);
  5614. }
  5615. if (EG(exception)) {
  5616. zend_object_store_ctor_failed(obj);
  5617. return FAILURE;
  5618. }
  5619. return SUCCESS;
  5620. }
  5621. static void attribute_ctor_cleanup(
  5622. zval *obj, zval *args, uint32_t argc, HashTable *named_params) /* {{{ */
  5623. {
  5624. if (obj) {
  5625. zval_ptr_dtor(obj);
  5626. }
  5627. if (args) {
  5628. uint32_t i;
  5629. for (i = 0; i < argc; i++) {
  5630. zval_ptr_dtor(&args[i]);
  5631. }
  5632. efree(args);
  5633. }
  5634. if (named_params) {
  5635. zend_array_destroy(named_params);
  5636. }
  5637. }
  5638. /* }}} */
  5639. /* {{{ * Returns the attribute as an object */
  5640. ZEND_METHOD(ReflectionAttribute, newInstance)
  5641. {
  5642. reflection_object *intern;
  5643. attribute_reference *attr;
  5644. zend_attribute *marker;
  5645. zend_class_entry *ce;
  5646. zval obj;
  5647. zval *args = NULL;
  5648. HashTable *named_params = NULL;
  5649. if (zend_parse_parameters_none() == FAILURE) {
  5650. RETURN_THROWS();
  5651. }
  5652. GET_REFLECTION_OBJECT_PTR(attr);
  5653. if (NULL == (ce = zend_lookup_class(attr->data->name))) {
  5654. zend_throw_error(NULL, "Attribute class \"%s\" not found", ZSTR_VAL(attr->data->name));
  5655. RETURN_THROWS();
  5656. }
  5657. if (NULL == (marker = zend_get_attribute_str(ce->attributes, ZEND_STRL("attribute")))) {
  5658. zend_throw_error(NULL, "Attempting to use non-attribute class \"%s\" as attribute", ZSTR_VAL(attr->data->name));
  5659. RETURN_THROWS();
  5660. }
  5661. if (ce->type == ZEND_USER_CLASS) {
  5662. uint32_t flags = ZEND_ATTRIBUTE_TARGET_ALL;
  5663. if (marker->argc > 0) {
  5664. zval tmp;
  5665. if (FAILURE == zend_get_attribute_value(&tmp, marker, 0, ce)) {
  5666. RETURN_THROWS();
  5667. }
  5668. flags = (uint32_t) Z_LVAL(tmp);
  5669. }
  5670. if (!(attr->target & flags)) {
  5671. zend_string *location = zend_get_attribute_target_names(attr->target);
  5672. zend_string *allowed = zend_get_attribute_target_names(flags);
  5673. zend_throw_error(NULL, "Attribute \"%s\" cannot target %s (allowed targets: %s)",
  5674. ZSTR_VAL(attr->data->name), ZSTR_VAL(location), ZSTR_VAL(allowed)
  5675. );
  5676. zend_string_release(location);
  5677. zend_string_release(allowed);
  5678. RETURN_THROWS();
  5679. }
  5680. if (!(flags & ZEND_ATTRIBUTE_IS_REPEATABLE)) {
  5681. if (zend_is_attribute_repeated(attr->attributes, attr->data)) {
  5682. zend_throw_error(NULL, "Attribute \"%s\" must not be repeated", ZSTR_VAL(attr->data->name));
  5683. RETURN_THROWS();
  5684. }
  5685. }
  5686. }
  5687. if (SUCCESS != object_init_ex(&obj, ce)) {
  5688. RETURN_THROWS();
  5689. }
  5690. uint32_t argc = 0;
  5691. if (attr->data->argc) {
  5692. args = emalloc(attr->data->argc * sizeof(zval));
  5693. for (uint32_t i = 0; i < attr->data->argc; i++) {
  5694. zval val;
  5695. if (FAILURE == zend_get_attribute_value(&val, attr->data, i, attr->scope)) {
  5696. attribute_ctor_cleanup(&obj, args, argc, named_params);
  5697. RETURN_THROWS();
  5698. }
  5699. if (attr->data->args[i].name) {
  5700. if (!named_params) {
  5701. named_params = zend_new_array(0);
  5702. }
  5703. zend_hash_add_new(named_params, attr->data->args[i].name, &val);
  5704. } else {
  5705. ZVAL_COPY_VALUE(&args[i], &val);
  5706. argc++;
  5707. }
  5708. }
  5709. }
  5710. if (ce->constructor) {
  5711. if (FAILURE == call_attribute_constructor(attr->data, ce, Z_OBJ(obj), args, argc, named_params, attr->filename)) {
  5712. attribute_ctor_cleanup(&obj, args, argc, named_params);
  5713. RETURN_THROWS();
  5714. }
  5715. } else if (argc || named_params) {
  5716. attribute_ctor_cleanup(&obj, args, argc, named_params);
  5717. zend_throw_error(NULL, "Attribute class %s does not have a constructor, cannot pass arguments", ZSTR_VAL(ce->name));
  5718. RETURN_THROWS();
  5719. }
  5720. attribute_ctor_cleanup(NULL, args, argc, named_params);
  5721. RETURN_COPY_VALUE(&obj);
  5722. }
  5723. ZEND_METHOD(ReflectionEnum, __construct)
  5724. {
  5725. reflection_class_object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  5726. if (EG(exception)) {
  5727. RETURN_THROWS();
  5728. }
  5729. reflection_object *intern;
  5730. zend_class_entry *ce;
  5731. GET_REFLECTION_OBJECT_PTR(ce);
  5732. if (!(ce->ce_flags & ZEND_ACC_ENUM)) {
  5733. zend_throw_exception_ex(reflection_exception_ptr, -1, "Class \"%s\" is not an enum", ZSTR_VAL(ce->name));
  5734. RETURN_THROWS();
  5735. }
  5736. }
  5737. ZEND_METHOD(ReflectionEnum, hasCase)
  5738. {
  5739. reflection_object *intern;
  5740. zend_class_entry *ce;
  5741. zend_string *name;
  5742. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
  5743. RETURN_THROWS();
  5744. }
  5745. GET_REFLECTION_OBJECT_PTR(ce);
  5746. zend_class_constant *class_const = zend_hash_find_ptr(&ce->constants_table, name);
  5747. if (class_const == NULL) {
  5748. RETURN_FALSE;
  5749. }
  5750. RETURN_BOOL(ZEND_CLASS_CONST_FLAGS(class_const) & ZEND_CLASS_CONST_IS_CASE);
  5751. }
  5752. ZEND_METHOD(ReflectionEnum, getCase)
  5753. {
  5754. reflection_object *intern;
  5755. zend_class_entry *ce;
  5756. zend_string *name;
  5757. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
  5758. RETURN_THROWS();
  5759. }
  5760. GET_REFLECTION_OBJECT_PTR(ce);
  5761. zend_class_constant *constant = zend_hash_find_ptr(&ce->constants_table, name);
  5762. if (constant == NULL) {
  5763. zend_throw_exception_ex(reflection_exception_ptr, 0, "Case %s::%s does not exist", ZSTR_VAL(ce->name), ZSTR_VAL(name));
  5764. RETURN_THROWS();
  5765. }
  5766. if (!(ZEND_CLASS_CONST_FLAGS(constant) & ZEND_CLASS_CONST_IS_CASE)) {
  5767. zend_throw_exception_ex(reflection_exception_ptr, 0, "%s::%s is not a case", ZSTR_VAL(ce->name), ZSTR_VAL(name));
  5768. RETURN_THROWS();
  5769. }
  5770. reflection_enum_case_factory(ce, name, constant, return_value);
  5771. }
  5772. ZEND_METHOD(ReflectionEnum, getCases)
  5773. {
  5774. reflection_object *intern;
  5775. zend_class_entry *ce;
  5776. zend_string *name;
  5777. zend_class_constant *constant;
  5778. if (zend_parse_parameters_none() == FAILURE) {
  5779. RETURN_THROWS();
  5780. }
  5781. GET_REFLECTION_OBJECT_PTR(ce);
  5782. array_init(return_value);
  5783. ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->constants_table, name, constant) {
  5784. if (ZEND_CLASS_CONST_FLAGS(constant) & ZEND_CLASS_CONST_IS_CASE) {
  5785. zval class_const;
  5786. reflection_enum_case_factory(ce, name, constant, &class_const);
  5787. zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &class_const);
  5788. }
  5789. } ZEND_HASH_FOREACH_END();
  5790. }
  5791. ZEND_METHOD(ReflectionEnum, isBacked)
  5792. {
  5793. reflection_object *intern;
  5794. zend_class_entry *ce;
  5795. if (zend_parse_parameters_none() == FAILURE) {
  5796. RETURN_THROWS();
  5797. }
  5798. GET_REFLECTION_OBJECT_PTR(ce);
  5799. RETURN_BOOL(ce->enum_backing_type != IS_UNDEF);
  5800. }
  5801. ZEND_METHOD(ReflectionEnum, getBackingType)
  5802. {
  5803. reflection_object *intern;
  5804. zend_class_entry *ce;
  5805. if (zend_parse_parameters_none() == FAILURE) {
  5806. RETURN_THROWS();
  5807. }
  5808. GET_REFLECTION_OBJECT_PTR(ce);
  5809. if (ce->enum_backing_type == IS_UNDEF) {
  5810. RETURN_NULL();
  5811. } else {
  5812. zend_type type = ZEND_TYPE_INIT_CODE(ce->enum_backing_type, 0, 0);
  5813. reflection_type_factory(type, return_value, 0);
  5814. }
  5815. }
  5816. ZEND_METHOD(ReflectionEnumUnitCase, __construct)
  5817. {
  5818. ZEND_MN(ReflectionClassConstant___construct)(INTERNAL_FUNCTION_PARAM_PASSTHRU);
  5819. if (EG(exception)) {
  5820. RETURN_THROWS();
  5821. }
  5822. reflection_object *intern;
  5823. zend_class_constant *ref;
  5824. GET_REFLECTION_OBJECT_PTR(ref);
  5825. if (!(ZEND_CLASS_CONST_FLAGS(ref) & ZEND_CLASS_CONST_IS_CASE)) {
  5826. zval *case_name = reflection_prop_name(ZEND_THIS);
  5827. zend_throw_exception_ex(reflection_exception_ptr, 0, "Constant %s::%s is not a case", ZSTR_VAL(ref->ce->name), Z_STRVAL_P(case_name));
  5828. RETURN_THROWS();
  5829. }
  5830. }
  5831. ZEND_METHOD(ReflectionEnumUnitCase, getEnum)
  5832. {
  5833. reflection_object *intern;
  5834. zend_class_constant *ref;
  5835. if (zend_parse_parameters_none() == FAILURE) {
  5836. RETURN_THROWS();
  5837. }
  5838. GET_REFLECTION_OBJECT_PTR(ref);
  5839. zend_reflection_class_factory(ref->ce, return_value);
  5840. }
  5841. ZEND_METHOD(ReflectionEnumBackedCase, __construct)
  5842. {
  5843. ZEND_MN(ReflectionEnumUnitCase___construct)(INTERNAL_FUNCTION_PARAM_PASSTHRU);
  5844. if (EG(exception)) {
  5845. RETURN_THROWS();
  5846. }
  5847. reflection_object *intern;
  5848. zend_class_constant *ref;
  5849. GET_REFLECTION_OBJECT_PTR(ref);
  5850. if (ref->ce->enum_backing_type == IS_UNDEF) {
  5851. zval *case_name = reflection_prop_name(ZEND_THIS);
  5852. zend_throw_exception_ex(reflection_exception_ptr, 0, "Enum case %s::%s is not a backed case", ZSTR_VAL(ref->ce->name), Z_STRVAL_P(case_name));
  5853. RETURN_THROWS();
  5854. }
  5855. }
  5856. ZEND_METHOD(ReflectionEnumBackedCase, getBackingValue)
  5857. {
  5858. reflection_object *intern;
  5859. zend_class_constant *ref;
  5860. if (zend_parse_parameters_none() == FAILURE) {
  5861. RETURN_THROWS();
  5862. }
  5863. GET_REFLECTION_OBJECT_PTR(ref);
  5864. if (Z_TYPE(ref->value) == IS_CONSTANT_AST) {
  5865. zval_update_constant_ex(&ref->value, ref->ce);
  5866. }
  5867. ZEND_ASSERT(intern->ce->enum_backing_type != IS_UNDEF);
  5868. zval *member_p = zend_enum_fetch_case_value(Z_OBJ(ref->value));
  5869. ZVAL_COPY_OR_DUP(return_value, member_p);
  5870. }
  5871. /* {{{ proto ReflectionFiber::__construct(Fiber $fiber) */
  5872. ZEND_METHOD(ReflectionFiber, __construct)
  5873. {
  5874. zval *fiber, *object;
  5875. reflection_object *intern;
  5876. object = ZEND_THIS;
  5877. intern = Z_REFLECTION_P(object);
  5878. ZEND_PARSE_PARAMETERS_START(1, 1)
  5879. Z_PARAM_OBJECT_OF_CLASS(fiber, zend_ce_fiber)
  5880. ZEND_PARSE_PARAMETERS_END();
  5881. if (intern->ce) {
  5882. zval_ptr_dtor(&intern->obj);
  5883. }
  5884. intern->ref_type = REF_TYPE_FIBER;
  5885. ZVAL_OBJ_COPY(&intern->obj, Z_OBJ_P(fiber));
  5886. intern->ce = zend_ce_fiber;
  5887. }
  5888. /* }}} */
  5889. ZEND_METHOD(ReflectionFiber, getFiber)
  5890. {
  5891. ZEND_PARSE_PARAMETERS_NONE();
  5892. RETURN_OBJ_COPY(Z_OBJ(Z_REFLECTION_P(ZEND_THIS)->obj));
  5893. }
  5894. #define REFLECTION_CHECK_VALID_FIBER(fiber) do { \
  5895. if (fiber == NULL || fiber->context.status == ZEND_FIBER_STATUS_INIT || fiber->context.status == ZEND_FIBER_STATUS_DEAD) { \
  5896. zend_throw_error(NULL, "Cannot fetch information from a fiber that has not been started or is terminated"); \
  5897. RETURN_THROWS(); \
  5898. } \
  5899. } while (0)
  5900. ZEND_METHOD(ReflectionFiber, getTrace)
  5901. {
  5902. zend_fiber *fiber = (zend_fiber *) Z_OBJ(Z_REFLECTION_P(ZEND_THIS)->obj);
  5903. zend_long options = DEBUG_BACKTRACE_PROVIDE_OBJECT;
  5904. zend_execute_data *prev_execute_data;
  5905. ZEND_PARSE_PARAMETERS_START(0, 1)
  5906. Z_PARAM_OPTIONAL
  5907. Z_PARAM_LONG(options);
  5908. ZEND_PARSE_PARAMETERS_END();
  5909. REFLECTION_CHECK_VALID_FIBER(fiber);
  5910. prev_execute_data = fiber->stack_bottom->prev_execute_data;
  5911. fiber->stack_bottom->prev_execute_data = NULL;
  5912. if (EG(active_fiber) != fiber) {
  5913. // No need to replace current execute data if within the current fiber.
  5914. EG(current_execute_data) = fiber->execute_data;
  5915. }
  5916. zend_fetch_debug_backtrace(return_value, 0, options, 0);
  5917. EG(current_execute_data) = execute_data; // Restore original execute data.
  5918. fiber->stack_bottom->prev_execute_data = prev_execute_data; // Restore prev execute data on fiber stack.
  5919. }
  5920. ZEND_METHOD(ReflectionFiber, getExecutingLine)
  5921. {
  5922. zend_fiber *fiber = (zend_fiber *) Z_OBJ(Z_REFLECTION_P(ZEND_THIS)->obj);
  5923. zend_execute_data *prev_execute_data;
  5924. ZEND_PARSE_PARAMETERS_NONE();
  5925. REFLECTION_CHECK_VALID_FIBER(fiber);
  5926. if (EG(active_fiber) == fiber) {
  5927. prev_execute_data = execute_data->prev_execute_data;
  5928. } else {
  5929. prev_execute_data = fiber->execute_data->prev_execute_data;
  5930. }
  5931. RETURN_LONG(prev_execute_data->opline->lineno);
  5932. }
  5933. ZEND_METHOD(ReflectionFiber, getExecutingFile)
  5934. {
  5935. zend_fiber *fiber = (zend_fiber *) Z_OBJ(Z_REFLECTION_P(ZEND_THIS)->obj);
  5936. zend_execute_data *prev_execute_data;
  5937. ZEND_PARSE_PARAMETERS_NONE();
  5938. REFLECTION_CHECK_VALID_FIBER(fiber);
  5939. if (EG(active_fiber) == fiber) {
  5940. prev_execute_data = execute_data->prev_execute_data;
  5941. } else {
  5942. prev_execute_data = fiber->execute_data->prev_execute_data;
  5943. }
  5944. RETURN_STR_COPY(prev_execute_data->func->op_array.filename);
  5945. }
  5946. ZEND_METHOD(ReflectionFiber, getCallable)
  5947. {
  5948. zend_fiber *fiber = (zend_fiber *) Z_OBJ(Z_REFLECTION_P(ZEND_THIS)->obj);
  5949. ZEND_PARSE_PARAMETERS_NONE();
  5950. if (fiber == NULL || fiber->context.status == ZEND_FIBER_STATUS_DEAD) {
  5951. zend_throw_error(NULL, "Cannot fetch the callable from a fiber that has terminated"); \
  5952. RETURN_THROWS();
  5953. }
  5954. RETURN_COPY(&fiber->fci.function_name);
  5955. }
  5956. /* {{{ _reflection_write_property */
  5957. static zval *_reflection_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot)
  5958. {
  5959. if (zend_hash_exists(&object->ce->properties_info, name)
  5960. && (zend_string_equals_literal(name, "name") || zend_string_equals_literal(name, "class")))
  5961. {
  5962. zend_throw_exception_ex(reflection_exception_ptr, 0,
  5963. "Cannot set read-only property %s::$%s", ZSTR_VAL(object->ce->name), ZSTR_VAL(name));
  5964. return &EG(uninitialized_zval);
  5965. }
  5966. else
  5967. {
  5968. return zend_std_write_property(object, name, value, cache_slot);
  5969. }
  5970. }
  5971. /* }}} */
  5972. PHP_MINIT_FUNCTION(reflection) /* {{{ */
  5973. {
  5974. memcpy(&reflection_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
  5975. reflection_object_handlers.offset = XtOffsetOf(reflection_object, zo);
  5976. reflection_object_handlers.free_obj = reflection_free_objects_storage;
  5977. reflection_object_handlers.clone_obj = NULL;
  5978. reflection_object_handlers.write_property = _reflection_write_property;
  5979. reflection_object_handlers.get_gc = reflection_get_gc;
  5980. reflection_exception_ptr = register_class_ReflectionException(zend_ce_exception);
  5981. reflection_ptr = register_class_Reflection();
  5982. reflector_ptr = register_class_Reflector(zend_ce_stringable);
  5983. reflection_function_abstract_ptr = register_class_ReflectionFunctionAbstract(reflector_ptr);
  5984. reflection_function_abstract_ptr->create_object = reflection_objects_new;
  5985. reflection_function_ptr = register_class_ReflectionFunction(reflection_function_abstract_ptr);
  5986. reflection_function_ptr->create_object = reflection_objects_new;
  5987. REGISTER_REFLECTION_CLASS_CONST_LONG(function, "IS_DEPRECATED", ZEND_ACC_DEPRECATED);
  5988. reflection_generator_ptr = register_class_ReflectionGenerator();
  5989. reflection_generator_ptr->create_object = reflection_objects_new;
  5990. reflection_parameter_ptr = register_class_ReflectionParameter(reflector_ptr);
  5991. reflection_parameter_ptr->create_object = reflection_objects_new;
  5992. reflection_type_ptr = register_class_ReflectionType(zend_ce_stringable);
  5993. reflection_type_ptr->create_object = reflection_objects_new;
  5994. reflection_named_type_ptr = register_class_ReflectionNamedType(reflection_type_ptr);
  5995. reflection_named_type_ptr->create_object = reflection_objects_new;
  5996. reflection_union_type_ptr = register_class_ReflectionUnionType(reflection_type_ptr);
  5997. reflection_union_type_ptr->create_object = reflection_objects_new;
  5998. reflection_intersection_type_ptr = register_class_ReflectionIntersectionType(reflection_type_ptr);
  5999. reflection_intersection_type_ptr->create_object = reflection_objects_new;
  6000. reflection_method_ptr = register_class_ReflectionMethod(reflection_function_abstract_ptr);
  6001. reflection_method_ptr->create_object = reflection_objects_new;
  6002. REGISTER_REFLECTION_CLASS_CONST_LONG(method, "IS_STATIC", ZEND_ACC_STATIC);
  6003. REGISTER_REFLECTION_CLASS_CONST_LONG(method, "IS_PUBLIC", ZEND_ACC_PUBLIC);
  6004. REGISTER_REFLECTION_CLASS_CONST_LONG(method, "IS_PROTECTED", ZEND_ACC_PROTECTED);
  6005. REGISTER_REFLECTION_CLASS_CONST_LONG(method, "IS_PRIVATE", ZEND_ACC_PRIVATE);
  6006. REGISTER_REFLECTION_CLASS_CONST_LONG(method, "IS_ABSTRACT", ZEND_ACC_ABSTRACT);
  6007. REGISTER_REFLECTION_CLASS_CONST_LONG(method, "IS_FINAL", ZEND_ACC_FINAL);
  6008. reflection_class_ptr = register_class_ReflectionClass(reflector_ptr);
  6009. reflection_class_ptr->create_object = reflection_objects_new;
  6010. /* IS_IMPLICIT_ABSTRACT is not longer used */
  6011. REGISTER_REFLECTION_CLASS_CONST_LONG(class, "IS_IMPLICIT_ABSTRACT", ZEND_ACC_IMPLICIT_ABSTRACT_CLASS);
  6012. REGISTER_REFLECTION_CLASS_CONST_LONG(class, "IS_EXPLICIT_ABSTRACT", ZEND_ACC_EXPLICIT_ABSTRACT_CLASS);
  6013. REGISTER_REFLECTION_CLASS_CONST_LONG(class, "IS_FINAL", ZEND_ACC_FINAL);
  6014. reflection_object_ptr = register_class_ReflectionObject(reflection_class_ptr);
  6015. reflection_object_ptr->create_object = reflection_objects_new;
  6016. reflection_property_ptr = register_class_ReflectionProperty(reflector_ptr);
  6017. reflection_property_ptr->create_object = reflection_objects_new;
  6018. REGISTER_REFLECTION_CLASS_CONST_LONG(property, "IS_STATIC", ZEND_ACC_STATIC);
  6019. REGISTER_REFLECTION_CLASS_CONST_LONG(property, "IS_READONLY", ZEND_ACC_READONLY);
  6020. REGISTER_REFLECTION_CLASS_CONST_LONG(property, "IS_PUBLIC", ZEND_ACC_PUBLIC);
  6021. REGISTER_REFLECTION_CLASS_CONST_LONG(property, "IS_PROTECTED", ZEND_ACC_PROTECTED);
  6022. REGISTER_REFLECTION_CLASS_CONST_LONG(property, "IS_PRIVATE", ZEND_ACC_PRIVATE);
  6023. reflection_class_constant_ptr = register_class_ReflectionClassConstant(reflector_ptr);
  6024. reflection_class_constant_ptr->create_object = reflection_objects_new;
  6025. REGISTER_REFLECTION_CLASS_CONST_LONG(class_constant, "IS_PUBLIC", ZEND_ACC_PUBLIC);
  6026. REGISTER_REFLECTION_CLASS_CONST_LONG(class_constant, "IS_PROTECTED", ZEND_ACC_PROTECTED);
  6027. REGISTER_REFLECTION_CLASS_CONST_LONG(class_constant, "IS_PRIVATE", ZEND_ACC_PRIVATE);
  6028. REGISTER_REFLECTION_CLASS_CONST_LONG(class_constant, "IS_FINAL", ZEND_ACC_FINAL);
  6029. reflection_extension_ptr = register_class_ReflectionExtension(reflector_ptr);
  6030. reflection_extension_ptr->create_object = reflection_objects_new;
  6031. reflection_zend_extension_ptr = register_class_ReflectionZendExtension(reflector_ptr);
  6032. reflection_zend_extension_ptr->create_object = reflection_objects_new;
  6033. reflection_reference_ptr = register_class_ReflectionReference();
  6034. reflection_reference_ptr->create_object = reflection_objects_new;
  6035. reflection_attribute_ptr = register_class_ReflectionAttribute(reflector_ptr);
  6036. reflection_attribute_ptr->create_object = reflection_objects_new;
  6037. reflection_enum_ptr = register_class_ReflectionEnum(reflection_class_ptr);
  6038. reflection_enum_ptr->create_object = reflection_objects_new;
  6039. reflection_enum_unit_case_ptr = register_class_ReflectionEnumUnitCase(reflection_class_constant_ptr);
  6040. reflection_enum_unit_case_ptr->create_object = reflection_objects_new;
  6041. reflection_enum_backed_case_ptr = register_class_ReflectionEnumBackedCase(reflection_enum_unit_case_ptr);
  6042. reflection_enum_backed_case_ptr->create_object = reflection_objects_new;
  6043. reflection_fiber_ptr = register_class_ReflectionFiber();
  6044. reflection_fiber_ptr->create_object = reflection_objects_new;
  6045. REGISTER_REFLECTION_CLASS_CONST_LONG(attribute, "IS_INSTANCEOF", REFLECTION_ATTRIBUTE_IS_INSTANCEOF);
  6046. REFLECTION_G(key_initialized) = 0;
  6047. return SUCCESS;
  6048. } /* }}} */
  6049. PHP_MINFO_FUNCTION(reflection) /* {{{ */
  6050. {
  6051. php_info_print_table_start();
  6052. php_info_print_table_row(2, "Reflection", "enabled");
  6053. php_info_print_table_end();
  6054. } /* }}} */
  6055. zend_module_entry reflection_module_entry = { /* {{{ */
  6056. STANDARD_MODULE_HEADER,
  6057. "Reflection",
  6058. NULL,
  6059. PHP_MINIT(reflection),
  6060. NULL,
  6061. NULL,
  6062. NULL,
  6063. PHP_MINFO(reflection),
  6064. PHP_REFLECTION_VERSION,
  6065. ZEND_MODULE_GLOBALS(reflection),
  6066. NULL,
  6067. NULL,
  6068. NULL,
  6069. STANDARD_MODULE_PROPERTIES_EX
  6070. }; /* }}} */