php_reflection.c 192 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178
  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) {
  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 an associative array containing the closures lexical scope variables */
  1505. ZEND_METHOD(ReflectionFunctionAbstract, getClosureUsedVariables)
  1506. {
  1507. reflection_object *intern;
  1508. const zend_function *closure_func;
  1509. if (zend_parse_parameters_none() == FAILURE) {
  1510. RETURN_THROWS();
  1511. }
  1512. GET_REFLECTION_OBJECT();
  1513. array_init(return_value);
  1514. if (!Z_ISUNDEF(intern->obj)) {
  1515. closure_func = zend_get_closure_method_def(Z_OBJ(intern->obj));
  1516. if (closure_func == NULL ||
  1517. closure_func->type != ZEND_USER_FUNCTION ||
  1518. closure_func->op_array.static_variables == NULL) {
  1519. return;
  1520. }
  1521. const zend_op_array *ops = &closure_func->op_array;
  1522. HashTable *static_variables = ZEND_MAP_PTR_GET(ops->static_variables_ptr);
  1523. if (!static_variables) {
  1524. return;
  1525. }
  1526. zend_op *opline = ops->opcodes + ops->num_args;
  1527. for (; opline->opcode == ZEND_BIND_STATIC; opline++) {
  1528. if (!(opline->extended_value & (ZEND_BIND_IMPLICIT|ZEND_BIND_EXPLICIT))) {
  1529. continue;
  1530. }
  1531. Bucket *bucket = (Bucket*)
  1532. (((char*)static_variables->arData) +
  1533. (opline->extended_value & ~(ZEND_BIND_REF|ZEND_BIND_IMPLICIT|ZEND_BIND_EXPLICIT)));
  1534. if (Z_ISUNDEF(bucket->val)) {
  1535. continue;
  1536. }
  1537. zend_hash_add_new(Z_ARRVAL_P(return_value), bucket->key, &bucket->val);
  1538. Z_TRY_ADDREF(bucket->val);
  1539. }
  1540. }
  1541. } /* }}} */
  1542. /* {{{ Returns a dynamically created closure for the function */
  1543. ZEND_METHOD(ReflectionFunction, getClosure)
  1544. {
  1545. reflection_object *intern;
  1546. zend_function *fptr;
  1547. if (zend_parse_parameters_none() == FAILURE) {
  1548. RETURN_THROWS();
  1549. }
  1550. GET_REFLECTION_OBJECT_PTR(fptr);
  1551. if (!Z_ISUNDEF(intern->obj)) {
  1552. /* Closures are immutable objects */
  1553. RETURN_OBJ_COPY(Z_OBJ(intern->obj));
  1554. } else {
  1555. zend_create_fake_closure(return_value, fptr, NULL, NULL, NULL);
  1556. }
  1557. }
  1558. /* }}} */
  1559. /* {{{ Returns whether this is an internal function */
  1560. ZEND_METHOD(ReflectionFunctionAbstract, isInternal)
  1561. {
  1562. reflection_object *intern;
  1563. zend_function *fptr;
  1564. if (zend_parse_parameters_none() == FAILURE) {
  1565. RETURN_THROWS();
  1566. }
  1567. GET_REFLECTION_OBJECT_PTR(fptr);
  1568. RETURN_BOOL(fptr->type == ZEND_INTERNAL_FUNCTION);
  1569. }
  1570. /* }}} */
  1571. /* {{{ Returns whether this is a user-defined function */
  1572. ZEND_METHOD(ReflectionFunctionAbstract, isUserDefined)
  1573. {
  1574. reflection_object *intern;
  1575. zend_function *fptr;
  1576. if (zend_parse_parameters_none() == FAILURE) {
  1577. RETURN_THROWS();
  1578. }
  1579. GET_REFLECTION_OBJECT_PTR(fptr);
  1580. RETURN_BOOL(fptr->type == ZEND_USER_FUNCTION);
  1581. }
  1582. /* }}} */
  1583. /* {{{ Returns whether this function has been disabled or not */
  1584. ZEND_METHOD(ReflectionFunction, isDisabled)
  1585. {
  1586. if (zend_parse_parameters_none() == FAILURE) {
  1587. RETURN_THROWS();
  1588. }
  1589. /* A disabled function cannot be queried using Reflection. */
  1590. RETURN_FALSE;
  1591. }
  1592. /* }}} */
  1593. /* {{{ Returns the filename of the file this function was declared in */
  1594. ZEND_METHOD(ReflectionFunctionAbstract, getFileName)
  1595. {
  1596. reflection_object *intern;
  1597. zend_function *fptr;
  1598. if (zend_parse_parameters_none() == FAILURE) {
  1599. RETURN_THROWS();
  1600. }
  1601. GET_REFLECTION_OBJECT_PTR(fptr);
  1602. if (fptr->type == ZEND_USER_FUNCTION) {
  1603. RETURN_STR_COPY(fptr->op_array.filename);
  1604. }
  1605. RETURN_FALSE;
  1606. }
  1607. /* }}} */
  1608. /* {{{ Returns the line this function's declaration starts at */
  1609. ZEND_METHOD(ReflectionFunctionAbstract, getStartLine)
  1610. {
  1611. reflection_object *intern;
  1612. zend_function *fptr;
  1613. if (zend_parse_parameters_none() == FAILURE) {
  1614. RETURN_THROWS();
  1615. }
  1616. GET_REFLECTION_OBJECT_PTR(fptr);
  1617. if (fptr->type == ZEND_USER_FUNCTION) {
  1618. RETURN_LONG(fptr->op_array.line_start);
  1619. }
  1620. RETURN_FALSE;
  1621. }
  1622. /* }}} */
  1623. /* {{{ Returns the line this function's declaration ends at */
  1624. ZEND_METHOD(ReflectionFunctionAbstract, getEndLine)
  1625. {
  1626. reflection_object *intern;
  1627. zend_function *fptr;
  1628. if (zend_parse_parameters_none() == FAILURE) {
  1629. RETURN_THROWS();
  1630. }
  1631. GET_REFLECTION_OBJECT_PTR(fptr);
  1632. if (fptr->type == ZEND_USER_FUNCTION) {
  1633. RETURN_LONG(fptr->op_array.line_end);
  1634. }
  1635. RETURN_FALSE;
  1636. }
  1637. /* }}} */
  1638. /* {{{ Returns the doc comment for this function */
  1639. ZEND_METHOD(ReflectionFunctionAbstract, getDocComment)
  1640. {
  1641. reflection_object *intern;
  1642. zend_function *fptr;
  1643. if (zend_parse_parameters_none() == FAILURE) {
  1644. RETURN_THROWS();
  1645. }
  1646. GET_REFLECTION_OBJECT_PTR(fptr);
  1647. if (fptr->type == ZEND_USER_FUNCTION && fptr->op_array.doc_comment) {
  1648. RETURN_STR_COPY(fptr->op_array.doc_comment);
  1649. }
  1650. RETURN_FALSE;
  1651. }
  1652. /* }}} */
  1653. /* {{{ Returns the attributes of this function */
  1654. ZEND_METHOD(ReflectionFunctionAbstract, getAttributes)
  1655. {
  1656. reflection_object *intern;
  1657. zend_function *fptr;
  1658. uint32_t target;
  1659. GET_REFLECTION_OBJECT_PTR(fptr);
  1660. if (fptr->common.scope && (fptr->common.fn_flags & (ZEND_ACC_CLOSURE|ZEND_ACC_FAKE_CLOSURE)) != ZEND_ACC_CLOSURE) {
  1661. target = ZEND_ATTRIBUTE_TARGET_METHOD;
  1662. } else {
  1663. target = ZEND_ATTRIBUTE_TARGET_FUNCTION;
  1664. }
  1665. reflect_attributes(INTERNAL_FUNCTION_PARAM_PASSTHRU,
  1666. fptr->common.attributes, 0, fptr->common.scope, target,
  1667. fptr->type == ZEND_USER_FUNCTION ? fptr->op_array.filename : NULL);
  1668. }
  1669. /* }}} */
  1670. /* {{{ Returns an associative array containing this function's static variables and their values */
  1671. ZEND_METHOD(ReflectionFunctionAbstract, getStaticVariables)
  1672. {
  1673. reflection_object *intern;
  1674. zend_function *fptr;
  1675. zval *val;
  1676. if (zend_parse_parameters_none() == FAILURE) {
  1677. RETURN_THROWS();
  1678. }
  1679. GET_REFLECTION_OBJECT_PTR(fptr);
  1680. /* Return an empty array in case no static variables exist */
  1681. if (fptr->type == ZEND_USER_FUNCTION && fptr->op_array.static_variables != NULL) {
  1682. HashTable *ht;
  1683. array_init(return_value);
  1684. ht = ZEND_MAP_PTR_GET(fptr->op_array.static_variables_ptr);
  1685. if (!ht) {
  1686. ht = zend_array_dup(fptr->op_array.static_variables);
  1687. ZEND_MAP_PTR_SET(fptr->op_array.static_variables_ptr, ht);
  1688. }
  1689. ZEND_HASH_FOREACH_VAL(ht, val) {
  1690. if (UNEXPECTED(zval_update_constant_ex(val, fptr->common.scope) != SUCCESS)) {
  1691. RETURN_THROWS();
  1692. }
  1693. } ZEND_HASH_FOREACH_END();
  1694. zend_hash_copy(Z_ARRVAL_P(return_value), ht, zval_add_ref);
  1695. } else {
  1696. RETURN_EMPTY_ARRAY();
  1697. }
  1698. }
  1699. /* }}} */
  1700. /* {{{ Invokes the function */
  1701. ZEND_METHOD(ReflectionFunction, invoke)
  1702. {
  1703. zval retval;
  1704. zval *params;
  1705. int result, num_args;
  1706. HashTable *named_params;
  1707. zend_fcall_info fci;
  1708. zend_fcall_info_cache fcc;
  1709. reflection_object *intern;
  1710. zend_function *fptr;
  1711. ZEND_PARSE_PARAMETERS_START(0, -1)
  1712. Z_PARAM_VARIADIC_WITH_NAMED(params, num_args, named_params)
  1713. ZEND_PARSE_PARAMETERS_END();
  1714. GET_REFLECTION_OBJECT_PTR(fptr);
  1715. fci.size = sizeof(fci);
  1716. ZVAL_UNDEF(&fci.function_name);
  1717. fci.object = NULL;
  1718. fci.retval = &retval;
  1719. fci.param_count = num_args;
  1720. fci.params = params;
  1721. fci.named_params = named_params;
  1722. fcc.function_handler = fptr;
  1723. fcc.called_scope = NULL;
  1724. fcc.object = NULL;
  1725. if (!Z_ISUNDEF(intern->obj)) {
  1726. Z_OBJ_HT(intern->obj)->get_closure(
  1727. Z_OBJ(intern->obj), &fcc.called_scope, &fcc.function_handler, &fcc.object, 0);
  1728. }
  1729. result = zend_call_function(&fci, &fcc);
  1730. if (result == FAILURE) {
  1731. zend_throw_exception_ex(reflection_exception_ptr, 0,
  1732. "Invocation of function %s() failed", ZSTR_VAL(fptr->common.function_name));
  1733. RETURN_THROWS();
  1734. }
  1735. if (Z_TYPE(retval) != IS_UNDEF) {
  1736. if (Z_ISREF(retval)) {
  1737. zend_unwrap_reference(&retval);
  1738. }
  1739. ZVAL_COPY_VALUE(return_value, &retval);
  1740. }
  1741. }
  1742. /* }}} */
  1743. /* {{{ Invokes the function and pass its arguments as array. */
  1744. ZEND_METHOD(ReflectionFunction, invokeArgs)
  1745. {
  1746. zval retval;
  1747. int result;
  1748. zend_fcall_info fci;
  1749. zend_fcall_info_cache fcc;
  1750. reflection_object *intern;
  1751. zend_function *fptr;
  1752. HashTable *params;
  1753. GET_REFLECTION_OBJECT_PTR(fptr);
  1754. if (zend_parse_parameters(ZEND_NUM_ARGS(), "h", &params) == FAILURE) {
  1755. RETURN_THROWS();
  1756. }
  1757. fci.size = sizeof(fci);
  1758. ZVAL_UNDEF(&fci.function_name);
  1759. fci.object = NULL;
  1760. fci.retval = &retval;
  1761. fci.param_count = 0;
  1762. fci.params = NULL;
  1763. fci.named_params = params;
  1764. fcc.function_handler = fptr;
  1765. fcc.called_scope = NULL;
  1766. fcc.object = NULL;
  1767. if (!Z_ISUNDEF(intern->obj)) {
  1768. Z_OBJ_HT(intern->obj)->get_closure(
  1769. Z_OBJ(intern->obj), &fcc.called_scope, &fcc.function_handler, &fcc.object, 0);
  1770. }
  1771. result = zend_call_function(&fci, &fcc);
  1772. if (result == FAILURE) {
  1773. zend_throw_exception_ex(reflection_exception_ptr, 0,
  1774. "Invocation of function %s() failed", ZSTR_VAL(fptr->common.function_name));
  1775. RETURN_THROWS();
  1776. }
  1777. if (Z_TYPE(retval) != IS_UNDEF) {
  1778. if (Z_ISREF(retval)) {
  1779. zend_unwrap_reference(&retval);
  1780. }
  1781. ZVAL_COPY_VALUE(return_value, &retval);
  1782. }
  1783. }
  1784. /* }}} */
  1785. /* {{{ Gets whether this function returns a reference */
  1786. ZEND_METHOD(ReflectionFunctionAbstract, returnsReference)
  1787. {
  1788. reflection_object *intern;
  1789. zend_function *fptr;
  1790. GET_REFLECTION_OBJECT_PTR(fptr);
  1791. if (zend_parse_parameters_none() == FAILURE) {
  1792. RETURN_THROWS();
  1793. }
  1794. RETURN_BOOL((fptr->op_array.fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0);
  1795. }
  1796. /* }}} */
  1797. /* {{{ Gets the number of parameters */
  1798. ZEND_METHOD(ReflectionFunctionAbstract, getNumberOfParameters)
  1799. {
  1800. reflection_object *intern;
  1801. zend_function *fptr;
  1802. uint32_t num_args;
  1803. GET_REFLECTION_OBJECT_PTR(fptr);
  1804. if (zend_parse_parameters_none() == FAILURE) {
  1805. RETURN_THROWS();
  1806. }
  1807. num_args = fptr->common.num_args;
  1808. if (fptr->common.fn_flags & ZEND_ACC_VARIADIC) {
  1809. num_args++;
  1810. }
  1811. RETURN_LONG(num_args);
  1812. }
  1813. /* }}} */
  1814. /* {{{ Gets the number of required parameters */
  1815. ZEND_METHOD(ReflectionFunctionAbstract, getNumberOfRequiredParameters)
  1816. {
  1817. reflection_object *intern;
  1818. zend_function *fptr;
  1819. GET_REFLECTION_OBJECT_PTR(fptr);
  1820. if (zend_parse_parameters_none() == FAILURE) {
  1821. RETURN_THROWS();
  1822. }
  1823. RETURN_LONG(fptr->common.required_num_args);
  1824. }
  1825. /* }}} */
  1826. /* {{{ Returns an array of parameter objects for this function */
  1827. ZEND_METHOD(ReflectionFunctionAbstract, getParameters)
  1828. {
  1829. reflection_object *intern;
  1830. zend_function *fptr;
  1831. uint32_t i, num_args;
  1832. struct _zend_arg_info *arg_info;
  1833. GET_REFLECTION_OBJECT_PTR(fptr);
  1834. if (zend_parse_parameters_none() == FAILURE) {
  1835. RETURN_THROWS();
  1836. }
  1837. arg_info= fptr->common.arg_info;
  1838. num_args = fptr->common.num_args;
  1839. if (fptr->common.fn_flags & ZEND_ACC_VARIADIC) {
  1840. num_args++;
  1841. }
  1842. if (!num_args) {
  1843. RETURN_EMPTY_ARRAY();
  1844. }
  1845. array_init(return_value);
  1846. for (i = 0; i < num_args; i++) {
  1847. zval parameter;
  1848. reflection_parameter_factory(
  1849. _copy_function(fptr),
  1850. Z_ISUNDEF(intern->obj) ? NULL : &intern->obj,
  1851. arg_info,
  1852. i,
  1853. i < fptr->common.required_num_args,
  1854. &parameter
  1855. );
  1856. zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &parameter);
  1857. arg_info++;
  1858. }
  1859. }
  1860. /* }}} */
  1861. /* {{{ Returns NULL or the extension the function belongs to */
  1862. ZEND_METHOD(ReflectionFunctionAbstract, getExtension)
  1863. {
  1864. reflection_object *intern;
  1865. zend_function *fptr;
  1866. zend_internal_function *internal;
  1867. GET_REFLECTION_OBJECT_PTR(fptr);
  1868. if (zend_parse_parameters_none() == FAILURE) {
  1869. RETURN_THROWS();
  1870. }
  1871. if (fptr->type != ZEND_INTERNAL_FUNCTION) {
  1872. RETURN_NULL();
  1873. }
  1874. internal = (zend_internal_function *)fptr;
  1875. if (internal->module) {
  1876. reflection_extension_factory(return_value, internal->module->name);
  1877. } else {
  1878. RETURN_NULL();
  1879. }
  1880. }
  1881. /* }}} */
  1882. /* {{{ Returns false or the name of the extension the function belongs to */
  1883. ZEND_METHOD(ReflectionFunctionAbstract, getExtensionName)
  1884. {
  1885. reflection_object *intern;
  1886. zend_function *fptr;
  1887. zend_internal_function *internal;
  1888. GET_REFLECTION_OBJECT_PTR(fptr);
  1889. if (zend_parse_parameters_none() == FAILURE) {
  1890. RETURN_THROWS();
  1891. }
  1892. if (fptr->type != ZEND_INTERNAL_FUNCTION) {
  1893. RETURN_FALSE;
  1894. }
  1895. internal = (zend_internal_function *)fptr;
  1896. if (internal->module) {
  1897. RETURN_STRING(internal->module->name);
  1898. } else {
  1899. RETURN_FALSE;
  1900. }
  1901. }
  1902. /* }}} */
  1903. /* {{{ */
  1904. ZEND_METHOD(ReflectionGenerator, __construct)
  1905. {
  1906. zval *generator, *object;
  1907. reflection_object *intern;
  1908. zend_execute_data *ex;
  1909. object = ZEND_THIS;
  1910. intern = Z_REFLECTION_P(object);
  1911. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &generator, zend_ce_generator) == FAILURE) {
  1912. RETURN_THROWS();
  1913. }
  1914. ex = ((zend_generator *) Z_OBJ_P(generator))->execute_data;
  1915. if (!ex) {
  1916. _DO_THROW("Cannot create ReflectionGenerator based on a terminated Generator");
  1917. RETURN_THROWS();
  1918. }
  1919. if (intern->ce) {
  1920. zval_ptr_dtor(&intern->obj);
  1921. }
  1922. intern->ref_type = REF_TYPE_GENERATOR;
  1923. ZVAL_OBJ_COPY(&intern->obj, Z_OBJ_P(generator));
  1924. intern->ce = zend_ce_generator;
  1925. }
  1926. /* }}} */
  1927. #define REFLECTION_CHECK_VALID_GENERATOR(ex) \
  1928. if (!ex) { \
  1929. _DO_THROW("Cannot fetch information from a terminated Generator"); \
  1930. RETURN_THROWS(); \
  1931. }
  1932. /* {{{ */
  1933. ZEND_METHOD(ReflectionGenerator, getTrace)
  1934. {
  1935. zend_long options = DEBUG_BACKTRACE_PROVIDE_OBJECT;
  1936. zend_generator *generator = (zend_generator *) Z_OBJ(Z_REFLECTION_P(ZEND_THIS)->obj);
  1937. zend_generator *root_generator;
  1938. zend_execute_data *ex_backup = EG(current_execute_data);
  1939. zend_execute_data *ex = generator->execute_data;
  1940. zend_execute_data *root_prev = NULL, *cur_prev;
  1941. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &options) == FAILURE) {
  1942. RETURN_THROWS();
  1943. }
  1944. REFLECTION_CHECK_VALID_GENERATOR(ex)
  1945. root_generator = zend_generator_get_current(generator);
  1946. cur_prev = generator->execute_data->prev_execute_data;
  1947. if (generator == root_generator) {
  1948. generator->execute_data->prev_execute_data = NULL;
  1949. } else {
  1950. root_prev = root_generator->execute_data->prev_execute_data;
  1951. generator->execute_fake.prev_execute_data = NULL;
  1952. root_generator->execute_data->prev_execute_data = &generator->execute_fake;
  1953. }
  1954. EG(current_execute_data) = root_generator->execute_data;
  1955. zend_fetch_debug_backtrace(return_value, 0, options, 0);
  1956. EG(current_execute_data) = ex_backup;
  1957. root_generator->execute_data->prev_execute_data = root_prev;
  1958. generator->execute_data->prev_execute_data = cur_prev;
  1959. }
  1960. /* }}} */
  1961. /* {{{ */
  1962. ZEND_METHOD(ReflectionGenerator, getExecutingLine)
  1963. {
  1964. zend_generator *generator = (zend_generator *) Z_OBJ(Z_REFLECTION_P(ZEND_THIS)->obj);
  1965. zend_execute_data *ex = generator->execute_data;
  1966. if (zend_parse_parameters_none() == FAILURE) {
  1967. RETURN_THROWS();
  1968. }
  1969. REFLECTION_CHECK_VALID_GENERATOR(ex)
  1970. ZVAL_LONG(return_value, ex->opline->lineno);
  1971. }
  1972. /* }}} */
  1973. /* {{{ */
  1974. ZEND_METHOD(ReflectionGenerator, getExecutingFile)
  1975. {
  1976. zend_generator *generator = (zend_generator *) Z_OBJ(Z_REFLECTION_P(ZEND_THIS)->obj);
  1977. zend_execute_data *ex = generator->execute_data;
  1978. if (zend_parse_parameters_none() == FAILURE) {
  1979. RETURN_THROWS();
  1980. }
  1981. REFLECTION_CHECK_VALID_GENERATOR(ex)
  1982. ZVAL_STR_COPY(return_value, ex->func->op_array.filename);
  1983. }
  1984. /* }}} */
  1985. /* {{{ */
  1986. ZEND_METHOD(ReflectionGenerator, getFunction)
  1987. {
  1988. zend_generator *generator = (zend_generator *) Z_OBJ(Z_REFLECTION_P(ZEND_THIS)->obj);
  1989. zend_execute_data *ex = generator->execute_data;
  1990. if (zend_parse_parameters_none() == FAILURE) {
  1991. RETURN_THROWS();
  1992. }
  1993. REFLECTION_CHECK_VALID_GENERATOR(ex)
  1994. if (ex->func->common.fn_flags & ZEND_ACC_CLOSURE) {
  1995. zval closure;
  1996. ZVAL_OBJ(&closure, ZEND_CLOSURE_OBJECT(ex->func));
  1997. reflection_function_factory(ex->func, &closure, return_value);
  1998. } else if (ex->func->op_array.scope) {
  1999. reflection_method_factory(ex->func->op_array.scope, ex->func, NULL, return_value);
  2000. } else {
  2001. reflection_function_factory(ex->func, NULL, return_value);
  2002. }
  2003. }
  2004. /* }}} */
  2005. /* {{{ */
  2006. ZEND_METHOD(ReflectionGenerator, getThis)
  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 (Z_TYPE(ex->This) == IS_OBJECT) {
  2015. RETURN_OBJ_COPY(Z_OBJ(ex->This));
  2016. } else {
  2017. RETURN_NULL();
  2018. }
  2019. }
  2020. /* }}} */
  2021. /* {{{ */
  2022. ZEND_METHOD(ReflectionGenerator, getExecutingGenerator)
  2023. {
  2024. zend_generator *generator = (zend_generator *) Z_OBJ(Z_REFLECTION_P(ZEND_THIS)->obj);
  2025. zend_execute_data *ex = generator->execute_data;
  2026. zend_generator *current;
  2027. if (zend_parse_parameters_none() == FAILURE) {
  2028. RETURN_THROWS();
  2029. }
  2030. REFLECTION_CHECK_VALID_GENERATOR(ex)
  2031. current = zend_generator_get_current(generator);
  2032. RETURN_OBJ_COPY(&current->std);
  2033. }
  2034. /* }}} */
  2035. /* {{{ Constructor. Throws an Exception in case the given method does not exist */
  2036. ZEND_METHOD(ReflectionParameter, __construct)
  2037. {
  2038. parameter_reference *ref;
  2039. zval *reference;
  2040. zend_string *arg_name = NULL;
  2041. zend_long position;
  2042. zval *object;
  2043. zval *prop_name;
  2044. reflection_object *intern;
  2045. zend_function *fptr;
  2046. struct _zend_arg_info *arg_info;
  2047. uint32_t num_args;
  2048. zend_class_entry *ce = NULL;
  2049. bool is_closure = 0;
  2050. ZEND_PARSE_PARAMETERS_START(2, 2)
  2051. Z_PARAM_ZVAL(reference)
  2052. Z_PARAM_STR_OR_LONG(arg_name, position)
  2053. ZEND_PARSE_PARAMETERS_END();
  2054. object = ZEND_THIS;
  2055. intern = Z_REFLECTION_P(object);
  2056. /* First, find the function */
  2057. switch (Z_TYPE_P(reference)) {
  2058. case IS_STRING:
  2059. {
  2060. zend_string *lcname = zend_string_tolower(Z_STR_P(reference));
  2061. fptr = zend_hash_find_ptr(EG(function_table), lcname);
  2062. zend_string_release(lcname);
  2063. if (!fptr) {
  2064. zend_throw_exception_ex(reflection_exception_ptr, 0,
  2065. "Function %s() does not exist", Z_STRVAL_P(reference));
  2066. RETURN_THROWS();
  2067. }
  2068. ce = fptr->common.scope;
  2069. }
  2070. break;
  2071. case IS_ARRAY: {
  2072. zval *classref;
  2073. zval *method;
  2074. zend_string *name, *lcname;
  2075. if (((classref = zend_hash_index_find(Z_ARRVAL_P(reference), 0)) == NULL)
  2076. || ((method = zend_hash_index_find(Z_ARRVAL_P(reference), 1)) == NULL))
  2077. {
  2078. _DO_THROW("Expected array($object, $method) or array($classname, $method)");
  2079. RETURN_THROWS();
  2080. }
  2081. if (Z_TYPE_P(classref) == IS_OBJECT) {
  2082. ce = Z_OBJCE_P(classref);
  2083. } else {
  2084. name = zval_try_get_string(classref);
  2085. if (UNEXPECTED(!name)) {
  2086. return;
  2087. }
  2088. if ((ce = zend_lookup_class(name)) == NULL) {
  2089. zend_throw_exception_ex(reflection_exception_ptr, 0,
  2090. "Class \"%s\" does not exist", ZSTR_VAL(name));
  2091. zend_string_release(name);
  2092. RETURN_THROWS();
  2093. }
  2094. zend_string_release(name);
  2095. }
  2096. name = zval_try_get_string(method);
  2097. if (UNEXPECTED(!name)) {
  2098. return;
  2099. }
  2100. lcname = zend_string_tolower(name);
  2101. if (Z_TYPE_P(classref) == IS_OBJECT && is_closure_invoke(ce, lcname)
  2102. && (fptr = zend_get_closure_invoke_method(Z_OBJ_P(classref))) != NULL)
  2103. {
  2104. /* nothing to do. don't set is_closure since is the invoke handler,
  2105. not the closure itself */
  2106. } else if ((fptr = zend_hash_find_ptr(&ce->function_table, lcname)) == NULL) {
  2107. zend_throw_exception_ex(reflection_exception_ptr, 0,
  2108. "Method %s::%s() does not exist", ZSTR_VAL(ce->name), ZSTR_VAL(name));
  2109. zend_string_release(name);
  2110. zend_string_release(lcname);
  2111. RETURN_THROWS();
  2112. }
  2113. zend_string_release(name);
  2114. zend_string_release(lcname);
  2115. }
  2116. break;
  2117. case IS_OBJECT: {
  2118. ce = Z_OBJCE_P(reference);
  2119. if (instanceof_function(ce, zend_ce_closure)) {
  2120. fptr = (zend_function *)zend_get_closure_method_def(Z_OBJ_P(reference));
  2121. Z_ADDREF_P(reference);
  2122. is_closure = 1;
  2123. } else if ((fptr = zend_hash_find_ptr(&ce->function_table, ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE))) == NULL) {
  2124. zend_throw_exception_ex(reflection_exception_ptr, 0,
  2125. "Method %s::%s() does not exist", ZSTR_VAL(ce->name), ZEND_INVOKE_FUNC_NAME);
  2126. RETURN_THROWS();
  2127. }
  2128. }
  2129. break;
  2130. default:
  2131. 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));
  2132. RETURN_THROWS();
  2133. }
  2134. /* Now, search for the parameter */
  2135. arg_info = fptr->common.arg_info;
  2136. num_args = fptr->common.num_args;
  2137. if (fptr->common.fn_flags & ZEND_ACC_VARIADIC) {
  2138. num_args++;
  2139. }
  2140. if (arg_name != NULL) {
  2141. uint32_t i;
  2142. position = -1;
  2143. if (has_internal_arg_info(fptr)) {
  2144. for (i = 0; i < num_args; i++) {
  2145. if (arg_info[i].name) {
  2146. if (strcmp(((zend_internal_arg_info*)arg_info)[i].name, ZSTR_VAL(arg_name)) == 0) {
  2147. position = i;
  2148. break;
  2149. }
  2150. }
  2151. }
  2152. } else {
  2153. for (i = 0; i < num_args; i++) {
  2154. if (arg_info[i].name) {
  2155. if (zend_string_equals(arg_name, arg_info[i].name)) {
  2156. position = i;
  2157. break;
  2158. }
  2159. }
  2160. }
  2161. }
  2162. if (position == -1) {
  2163. _DO_THROW("The parameter specified by its name could not be found");
  2164. goto failure;
  2165. }
  2166. } else {
  2167. if (position < 0) {
  2168. zend_argument_value_error(2, "must be greater than or equal to 0");
  2169. goto failure;
  2170. }
  2171. if (position >= num_args) {
  2172. _DO_THROW("The parameter specified by its offset could not be found");
  2173. goto failure;
  2174. }
  2175. }
  2176. ref = (parameter_reference*) emalloc(sizeof(parameter_reference));
  2177. ref->arg_info = &arg_info[position];
  2178. ref->offset = (uint32_t)position;
  2179. ref->required = (uint32_t)position < fptr->common.required_num_args;
  2180. ref->fptr = fptr;
  2181. /* TODO: copy fptr */
  2182. intern->ptr = ref;
  2183. intern->ref_type = REF_TYPE_PARAMETER;
  2184. intern->ce = ce;
  2185. if (reference && is_closure) {
  2186. ZVAL_COPY_VALUE(&intern->obj, reference);
  2187. }
  2188. prop_name = reflection_prop_name(object);
  2189. if (has_internal_arg_info(fptr)) {
  2190. ZVAL_STRING(prop_name, ((zend_internal_arg_info*)arg_info)[position].name);
  2191. } else {
  2192. ZVAL_STR_COPY(prop_name, arg_info[position].name);
  2193. }
  2194. return;
  2195. failure:
  2196. if (fptr->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
  2197. zend_string_release_ex(fptr->common.function_name, 0);
  2198. zend_free_trampoline(fptr);
  2199. }
  2200. if (is_closure) {
  2201. zval_ptr_dtor(reference);
  2202. }
  2203. RETURN_THROWS();
  2204. }
  2205. /* }}} */
  2206. /* {{{ Returns a string representation */
  2207. ZEND_METHOD(ReflectionParameter, __toString)
  2208. {
  2209. reflection_object *intern;
  2210. parameter_reference *param;
  2211. smart_str str = {0};
  2212. if (zend_parse_parameters_none() == FAILURE) {
  2213. RETURN_THROWS();
  2214. }
  2215. GET_REFLECTION_OBJECT_PTR(param);
  2216. _parameter_string(&str, param->fptr, param->arg_info, param->offset, param->required, "");
  2217. RETURN_STR(smart_str_extract(&str));
  2218. }
  2219. /* }}} */
  2220. /* {{{ Returns this parameters's name */
  2221. ZEND_METHOD(ReflectionParameter, getName)
  2222. {
  2223. reflection_object *intern;
  2224. parameter_reference *param;
  2225. if (zend_parse_parameters_none() == FAILURE) {
  2226. RETURN_THROWS();
  2227. }
  2228. GET_REFLECTION_OBJECT_PTR(param);
  2229. if (has_internal_arg_info(param->fptr)) {
  2230. RETURN_STRING(((zend_internal_arg_info *) param->arg_info)->name);
  2231. } else {
  2232. RETURN_STR_COPY(param->arg_info->name);
  2233. }
  2234. }
  2235. /* }}} */
  2236. /* {{{ Returns the ReflectionFunction for the function of this parameter */
  2237. ZEND_METHOD(ReflectionParameter, getDeclaringFunction)
  2238. {
  2239. reflection_object *intern;
  2240. parameter_reference *param;
  2241. if (zend_parse_parameters_none() == FAILURE) {
  2242. RETURN_THROWS();
  2243. }
  2244. GET_REFLECTION_OBJECT_PTR(param);
  2245. if (!param->fptr->common.scope) {
  2246. reflection_function_factory(_copy_function(param->fptr), Z_ISUNDEF(intern->obj)? NULL : &intern->obj, return_value);
  2247. } else {
  2248. reflection_method_factory(param->fptr->common.scope, _copy_function(param->fptr), Z_ISUNDEF(intern->obj)? NULL : &intern->obj, return_value);
  2249. }
  2250. }
  2251. /* }}} */
  2252. /* {{{ Returns in which class this parameter is defined (not the type of the parameter) */
  2253. ZEND_METHOD(ReflectionParameter, getDeclaringClass)
  2254. {
  2255. reflection_object *intern;
  2256. parameter_reference *param;
  2257. if (zend_parse_parameters_none() == FAILURE) {
  2258. RETURN_THROWS();
  2259. }
  2260. GET_REFLECTION_OBJECT_PTR(param);
  2261. if (param->fptr->common.scope) {
  2262. zend_reflection_class_factory(param->fptr->common.scope, return_value);
  2263. }
  2264. }
  2265. /* }}} */
  2266. /* {{{ Returns this parameters's class hint or NULL if there is none */
  2267. ZEND_METHOD(ReflectionParameter, getClass)
  2268. {
  2269. reflection_object *intern;
  2270. parameter_reference *param;
  2271. zend_class_entry *ce;
  2272. if (zend_parse_parameters_none() == FAILURE) {
  2273. RETURN_THROWS();
  2274. }
  2275. GET_REFLECTION_OBJECT_PTR(param);
  2276. // TODO: This is going to return null for union types, which is rather odd.
  2277. if (ZEND_TYPE_HAS_NAME(param->arg_info->type)) {
  2278. /* Class name is stored as a string, we might also get "self" or "parent"
  2279. * - For "self", simply use the function scope. If scope is NULL then
  2280. * the function is global and thus self does not make any sense
  2281. *
  2282. * - For "parent", use the function scope's parent. If scope is NULL then
  2283. * the function is global and thus parent does not make any sense.
  2284. * If the parent is NULL then the class does not extend anything and
  2285. * thus parent does not make any sense, either.
  2286. *
  2287. * TODO: Think about moving these checks to the compiler or some sort of
  2288. * lint-mode.
  2289. */
  2290. zend_string *class_name;
  2291. class_name = ZEND_TYPE_NAME(param->arg_info->type);
  2292. if (zend_string_equals_literal_ci(class_name, "self")) {
  2293. ce = param->fptr->common.scope;
  2294. if (!ce) {
  2295. zend_throw_exception_ex(reflection_exception_ptr, 0,
  2296. "Parameter uses \"self\" as type but function is not a class member");
  2297. RETURN_THROWS();
  2298. }
  2299. } else if (zend_string_equals_literal_ci(class_name, "parent")) {
  2300. ce = param->fptr->common.scope;
  2301. if (!ce) {
  2302. zend_throw_exception_ex(reflection_exception_ptr, 0,
  2303. "Parameter uses \"parent\" as type but function is not a class member");
  2304. RETURN_THROWS();
  2305. }
  2306. if (!ce->parent) {
  2307. zend_throw_exception_ex(reflection_exception_ptr, 0,
  2308. "Parameter uses \"parent\" as type although class does not have a parent");
  2309. RETURN_THROWS();
  2310. }
  2311. ce = ce->parent;
  2312. } else {
  2313. ce = zend_lookup_class(class_name);
  2314. if (!ce) {
  2315. zend_throw_exception_ex(reflection_exception_ptr, 0,
  2316. "Class \"%s\" does not exist", ZSTR_VAL(class_name));
  2317. RETURN_THROWS();
  2318. }
  2319. }
  2320. zend_reflection_class_factory(ce, return_value);
  2321. }
  2322. }
  2323. /* }}} */
  2324. /* {{{ Returns whether parameter has a type */
  2325. ZEND_METHOD(ReflectionParameter, hasType)
  2326. {
  2327. reflection_object *intern;
  2328. parameter_reference *param;
  2329. if (zend_parse_parameters_none() == FAILURE) {
  2330. RETURN_THROWS();
  2331. }
  2332. GET_REFLECTION_OBJECT_PTR(param);
  2333. RETVAL_BOOL(ZEND_TYPE_IS_SET(param->arg_info->type));
  2334. }
  2335. /* }}} */
  2336. /* {{{ Returns the type associated with the parameter */
  2337. ZEND_METHOD(ReflectionParameter, getType)
  2338. {
  2339. reflection_object *intern;
  2340. parameter_reference *param;
  2341. if (zend_parse_parameters_none() == FAILURE) {
  2342. RETURN_THROWS();
  2343. }
  2344. GET_REFLECTION_OBJECT_PTR(param);
  2345. if (!ZEND_TYPE_IS_SET(param->arg_info->type)) {
  2346. RETURN_NULL();
  2347. }
  2348. reflection_type_factory(param->arg_info->type, return_value, 1);
  2349. }
  2350. /* }}} */
  2351. /* {{{ Returns whether parameter MUST be an array */
  2352. ZEND_METHOD(ReflectionParameter, isArray)
  2353. {
  2354. reflection_object *intern;
  2355. parameter_reference *param;
  2356. uint32_t type_mask;
  2357. if (zend_parse_parameters_none() == FAILURE) {
  2358. RETURN_THROWS();
  2359. }
  2360. GET_REFLECTION_OBJECT_PTR(param);
  2361. type_mask = ZEND_TYPE_PURE_MASK_WITHOUT_NULL(param->arg_info->type);
  2362. RETVAL_BOOL(type_mask == MAY_BE_ARRAY);
  2363. }
  2364. /* }}} */
  2365. /* {{{ Returns whether parameter MUST be callable */
  2366. ZEND_METHOD(ReflectionParameter, isCallable)
  2367. {
  2368. reflection_object *intern;
  2369. parameter_reference *param;
  2370. uint32_t type_mask;
  2371. if (zend_parse_parameters_none() == FAILURE) {
  2372. RETURN_THROWS();
  2373. }
  2374. GET_REFLECTION_OBJECT_PTR(param);
  2375. type_mask = ZEND_TYPE_PURE_MASK_WITHOUT_NULL(param->arg_info->type);
  2376. RETVAL_BOOL(type_mask == MAY_BE_CALLABLE);
  2377. }
  2378. /* }}} */
  2379. /* {{{ Returns whether NULL is allowed as this parameters's value */
  2380. ZEND_METHOD(ReflectionParameter, allowsNull)
  2381. {
  2382. reflection_object *intern;
  2383. parameter_reference *param;
  2384. if (zend_parse_parameters_none() == FAILURE) {
  2385. RETURN_THROWS();
  2386. }
  2387. GET_REFLECTION_OBJECT_PTR(param);
  2388. RETVAL_BOOL(!ZEND_TYPE_IS_SET(param->arg_info->type)
  2389. || ZEND_TYPE_ALLOW_NULL(param->arg_info->type));
  2390. }
  2391. /* }}} */
  2392. /* {{{ Returns whether this parameters is passed to by reference */
  2393. ZEND_METHOD(ReflectionParameter, isPassedByReference)
  2394. {
  2395. reflection_object *intern;
  2396. parameter_reference *param;
  2397. if (zend_parse_parameters_none() == FAILURE) {
  2398. RETURN_THROWS();
  2399. }
  2400. GET_REFLECTION_OBJECT_PTR(param);
  2401. RETVAL_BOOL(ZEND_ARG_SEND_MODE(param->arg_info));
  2402. }
  2403. /* }}} */
  2404. /* {{{ Returns whether this parameter can be passed by value */
  2405. ZEND_METHOD(ReflectionParameter, canBePassedByValue)
  2406. {
  2407. reflection_object *intern;
  2408. parameter_reference *param;
  2409. if (zend_parse_parameters_none() == FAILURE) {
  2410. RETURN_THROWS();
  2411. }
  2412. GET_REFLECTION_OBJECT_PTR(param);
  2413. /* true if it's ZEND_SEND_BY_VAL or ZEND_SEND_PREFER_REF */
  2414. RETVAL_BOOL(ZEND_ARG_SEND_MODE(param->arg_info) != ZEND_SEND_BY_REF);
  2415. }
  2416. /* }}} */
  2417. /* {{{ Get parameter attributes. */
  2418. ZEND_METHOD(ReflectionParameter, getAttributes)
  2419. {
  2420. reflection_object *intern;
  2421. parameter_reference *param;
  2422. GET_REFLECTION_OBJECT_PTR(param);
  2423. HashTable *attributes = param->fptr->common.attributes;
  2424. zend_class_entry *scope = param->fptr->common.scope;
  2425. reflect_attributes(INTERNAL_FUNCTION_PARAM_PASSTHRU,
  2426. attributes, param->offset + 1, scope, ZEND_ATTRIBUTE_TARGET_PARAMETER,
  2427. param->fptr->type == ZEND_USER_FUNCTION ? param->fptr->op_array.filename : NULL);
  2428. }
  2429. /* {{{ Returns whether this parameter is an optional parameter */
  2430. ZEND_METHOD(ReflectionParameter, getPosition)
  2431. {
  2432. reflection_object *intern;
  2433. parameter_reference *param;
  2434. if (zend_parse_parameters_none() == FAILURE) {
  2435. RETURN_THROWS();
  2436. }
  2437. GET_REFLECTION_OBJECT_PTR(param);
  2438. RETVAL_LONG(param->offset);
  2439. }
  2440. /* }}} */
  2441. /* {{{ Returns whether this parameter is an optional parameter */
  2442. ZEND_METHOD(ReflectionParameter, isOptional)
  2443. {
  2444. reflection_object *intern;
  2445. parameter_reference *param;
  2446. if (zend_parse_parameters_none() == FAILURE) {
  2447. RETURN_THROWS();
  2448. }
  2449. GET_REFLECTION_OBJECT_PTR(param);
  2450. RETVAL_BOOL(!param->required);
  2451. }
  2452. /* }}} */
  2453. /* {{{ Returns whether the default value of this parameter is available */
  2454. ZEND_METHOD(ReflectionParameter, isDefaultValueAvailable)
  2455. {
  2456. reflection_object *intern;
  2457. parameter_reference *param;
  2458. if (zend_parse_parameters_none() == FAILURE) {
  2459. RETURN_THROWS();
  2460. }
  2461. GET_REFLECTION_OBJECT_PTR(param);
  2462. if (param->fptr->type == ZEND_INTERNAL_FUNCTION) {
  2463. RETURN_BOOL(!(param->fptr->common.fn_flags & ZEND_ACC_USER_ARG_INFO)
  2464. && ((zend_internal_arg_info*) (param->arg_info))->default_value);
  2465. } else {
  2466. zval *default_value = get_default_from_recv((zend_op_array *)param->fptr, param->offset);
  2467. RETURN_BOOL(default_value != NULL);
  2468. }
  2469. }
  2470. /* }}} */
  2471. /* {{{ Returns the default value of this parameter or throws an exception */
  2472. ZEND_METHOD(ReflectionParameter, getDefaultValue)
  2473. {
  2474. reflection_object *intern;
  2475. parameter_reference *param;
  2476. if (zend_parse_parameters_none() == FAILURE) {
  2477. RETURN_THROWS();
  2478. }
  2479. GET_REFLECTION_OBJECT_PTR(param);
  2480. if (get_parameter_default(return_value, param) == FAILURE) {
  2481. zend_throw_exception_ex(reflection_exception_ptr, 0,
  2482. "Internal error: Failed to retrieve the default value");
  2483. RETURN_THROWS();
  2484. }
  2485. if (Z_TYPE_P(return_value) == IS_CONSTANT_AST) {
  2486. zval_update_constant_ex(return_value, param->fptr->common.scope);
  2487. }
  2488. }
  2489. /* }}} */
  2490. /* {{{ Returns whether the default value of this parameter is constant */
  2491. ZEND_METHOD(ReflectionParameter, isDefaultValueConstant)
  2492. {
  2493. reflection_object *intern;
  2494. parameter_reference *param;
  2495. if (zend_parse_parameters_none() == FAILURE) {
  2496. RETURN_THROWS();
  2497. }
  2498. GET_REFLECTION_OBJECT_PTR(param);
  2499. zval default_value;
  2500. if (get_parameter_default(&default_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(default_value) == IS_CONSTANT_AST) {
  2506. zend_ast *ast = Z_ASTVAL(default_value);
  2507. RETVAL_BOOL(ast->kind == ZEND_AST_CONSTANT
  2508. || ast->kind == ZEND_AST_CONSTANT_CLASS
  2509. || ast->kind == ZEND_AST_CLASS_CONST);
  2510. } else {
  2511. RETVAL_FALSE;
  2512. }
  2513. zval_ptr_dtor_nogc(&default_value);
  2514. }
  2515. /* }}} */
  2516. /* {{{ Returns the default value's constant name if default value is constant or null */
  2517. ZEND_METHOD(ReflectionParameter, getDefaultValueConstantName)
  2518. {
  2519. reflection_object *intern;
  2520. parameter_reference *param;
  2521. if (zend_parse_parameters_none() == FAILURE) {
  2522. RETURN_THROWS();
  2523. }
  2524. GET_REFLECTION_OBJECT_PTR(param);
  2525. zval default_value;
  2526. if (get_parameter_default(&default_value, param) == FAILURE) {
  2527. zend_throw_exception_ex(reflection_exception_ptr, 0,
  2528. "Internal error: Failed to retrieve the default value");
  2529. RETURN_THROWS();
  2530. }
  2531. if (Z_TYPE(default_value) != IS_CONSTANT_AST) {
  2532. zval_ptr_dtor_nogc(&default_value);
  2533. RETURN_NULL();
  2534. }
  2535. zend_ast *ast = Z_ASTVAL(default_value);
  2536. if (ast->kind == ZEND_AST_CONSTANT) {
  2537. RETVAL_STR_COPY(zend_ast_get_constant_name(ast));
  2538. } else if (ast->kind == ZEND_AST_CONSTANT_CLASS) {
  2539. RETVAL_STRINGL("__CLASS__", sizeof("__CLASS__")-1);
  2540. } else if (ast->kind == ZEND_AST_CLASS_CONST) {
  2541. zend_string *class_name = zend_ast_get_str(ast->child[0]);
  2542. zend_string *const_name = zend_ast_get_str(ast->child[1]);
  2543. RETVAL_NEW_STR(zend_string_concat3(
  2544. ZSTR_VAL(class_name), ZSTR_LEN(class_name),
  2545. "::", sizeof("::")-1,
  2546. ZSTR_VAL(const_name), ZSTR_LEN(const_name)));
  2547. } else {
  2548. RETVAL_NULL();
  2549. }
  2550. zval_ptr_dtor_nogc(&default_value);
  2551. }
  2552. /* {{{ Returns whether this parameter is a variadic parameter */
  2553. ZEND_METHOD(ReflectionParameter, isVariadic)
  2554. {
  2555. reflection_object *intern;
  2556. parameter_reference *param;
  2557. if (zend_parse_parameters_none() == FAILURE) {
  2558. RETURN_THROWS();
  2559. }
  2560. GET_REFLECTION_OBJECT_PTR(param);
  2561. RETVAL_BOOL(ZEND_ARG_IS_VARIADIC(param->arg_info));
  2562. }
  2563. /* }}} */
  2564. /* {{{ Returns this constructor parameter has been promoted to a property */
  2565. ZEND_METHOD(ReflectionParameter, isPromoted)
  2566. {
  2567. reflection_object *intern;
  2568. parameter_reference *param;
  2569. if (zend_parse_parameters_none() == FAILURE) {
  2570. RETURN_THROWS();
  2571. }
  2572. GET_REFLECTION_OBJECT_PTR(param);
  2573. RETVAL_BOOL(ZEND_ARG_IS_PROMOTED(param->arg_info));
  2574. }
  2575. /* }}} */
  2576. /* {{{ Returns whether parameter MAY be null */
  2577. ZEND_METHOD(ReflectionType, allowsNull)
  2578. {
  2579. reflection_object *intern;
  2580. type_reference *param;
  2581. if (zend_parse_parameters_none() == FAILURE) {
  2582. RETURN_THROWS();
  2583. }
  2584. GET_REFLECTION_OBJECT_PTR(param);
  2585. RETVAL_BOOL(ZEND_TYPE_ALLOW_NULL(param->type));
  2586. }
  2587. /* }}} */
  2588. static zend_string *zend_type_to_string_without_null(zend_type type) {
  2589. ZEND_TYPE_FULL_MASK(type) &= ~MAY_BE_NULL;
  2590. return zend_type_to_string(type);
  2591. }
  2592. /* {{{ Return the text of the type hint */
  2593. ZEND_METHOD(ReflectionType, __toString)
  2594. {
  2595. reflection_object *intern;
  2596. type_reference *param;
  2597. if (zend_parse_parameters_none() == FAILURE) {
  2598. RETURN_THROWS();
  2599. }
  2600. GET_REFLECTION_OBJECT_PTR(param);
  2601. RETURN_STR(zend_type_to_string(param->type));
  2602. }
  2603. /* }}} */
  2604. /* {{{ Return the name of the type */
  2605. ZEND_METHOD(ReflectionNamedType, getName)
  2606. {
  2607. reflection_object *intern;
  2608. type_reference *param;
  2609. if (zend_parse_parameters_none() == FAILURE) {
  2610. RETURN_THROWS();
  2611. }
  2612. GET_REFLECTION_OBJECT_PTR(param);
  2613. if (param->legacy_behavior) {
  2614. RETURN_STR(zend_type_to_string_without_null(param->type));
  2615. }
  2616. RETURN_STR(zend_type_to_string(param->type));
  2617. }
  2618. /* }}} */
  2619. /* {{{ Returns whether type is a builtin type */
  2620. ZEND_METHOD(ReflectionNamedType, isBuiltin)
  2621. {
  2622. reflection_object *intern;
  2623. type_reference *param;
  2624. if (zend_parse_parameters_none() == FAILURE) {
  2625. RETURN_THROWS();
  2626. }
  2627. GET_REFLECTION_OBJECT_PTR(param);
  2628. /* Treat "static" as a class type for the purposes of reflection. */
  2629. RETVAL_BOOL(ZEND_TYPE_IS_ONLY_MASK(param->type)
  2630. && !(ZEND_TYPE_FULL_MASK(param->type) & MAY_BE_STATIC));
  2631. }
  2632. /* }}} */
  2633. static void append_type(zval *return_value, zend_type type) {
  2634. zval reflection_type;
  2635. reflection_type_factory(type, &reflection_type, 0);
  2636. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &reflection_type);
  2637. }
  2638. static void append_type_mask(zval *return_value, uint32_t type_mask) {
  2639. append_type(return_value, (zend_type) ZEND_TYPE_INIT_MASK(type_mask));
  2640. }
  2641. /* {{{ Returns the types that are part of this union type */
  2642. ZEND_METHOD(ReflectionUnionType, getTypes)
  2643. {
  2644. reflection_object *intern;
  2645. type_reference *param;
  2646. uint32_t type_mask;
  2647. if (zend_parse_parameters_none() == FAILURE) {
  2648. RETURN_THROWS();
  2649. }
  2650. GET_REFLECTION_OBJECT_PTR(param);
  2651. array_init(return_value);
  2652. if (ZEND_TYPE_HAS_LIST(param->type)) {
  2653. zend_type *list_type;
  2654. ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(param->type), list_type) {
  2655. append_type(return_value, *list_type);
  2656. } ZEND_TYPE_LIST_FOREACH_END();
  2657. } else if (ZEND_TYPE_HAS_NAME(param->type)) {
  2658. zend_string *name = ZEND_TYPE_NAME(param->type);
  2659. append_type(return_value, (zend_type) ZEND_TYPE_INIT_CLASS(name, 0, 0));
  2660. }
  2661. type_mask = ZEND_TYPE_PURE_MASK(param->type);
  2662. ZEND_ASSERT(!(type_mask & MAY_BE_VOID));
  2663. ZEND_ASSERT(!(type_mask & MAY_BE_NEVER));
  2664. if (type_mask & MAY_BE_STATIC) {
  2665. append_type_mask(return_value, MAY_BE_STATIC);
  2666. }
  2667. if (type_mask & MAY_BE_CALLABLE) {
  2668. append_type_mask(return_value, MAY_BE_CALLABLE);
  2669. }
  2670. if (type_mask & MAY_BE_ITERABLE) {
  2671. append_type_mask(return_value, MAY_BE_ITERABLE);
  2672. }
  2673. if (type_mask & MAY_BE_OBJECT) {
  2674. append_type_mask(return_value, MAY_BE_OBJECT);
  2675. }
  2676. if (type_mask & MAY_BE_ARRAY) {
  2677. append_type_mask(return_value, MAY_BE_ARRAY);
  2678. }
  2679. if (type_mask & MAY_BE_STRING) {
  2680. append_type_mask(return_value, MAY_BE_STRING);
  2681. }
  2682. if (type_mask & MAY_BE_LONG) {
  2683. append_type_mask(return_value, MAY_BE_LONG);
  2684. }
  2685. if (type_mask & MAY_BE_DOUBLE) {
  2686. append_type_mask(return_value, MAY_BE_DOUBLE);
  2687. }
  2688. if ((type_mask & MAY_BE_BOOL) == MAY_BE_BOOL) {
  2689. append_type_mask(return_value, MAY_BE_BOOL);
  2690. } else if (type_mask & MAY_BE_FALSE) {
  2691. append_type_mask(return_value, MAY_BE_FALSE);
  2692. }
  2693. if (type_mask & MAY_BE_NULL) {
  2694. append_type_mask(return_value, MAY_BE_NULL);
  2695. }
  2696. }
  2697. /* }}} */
  2698. /* {{{ Returns the types that are part of this intersection type */
  2699. ZEND_METHOD(ReflectionIntersectionType, getTypes)
  2700. {
  2701. reflection_object *intern;
  2702. type_reference *param;
  2703. zend_type *list_type;
  2704. if (zend_parse_parameters_none() == FAILURE) {
  2705. RETURN_THROWS();
  2706. }
  2707. GET_REFLECTION_OBJECT_PTR(param);
  2708. ZEND_ASSERT(ZEND_TYPE_HAS_LIST(param->type));
  2709. array_init(return_value);
  2710. ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(param->type), list_type) {
  2711. append_type(return_value, *list_type);
  2712. } ZEND_TYPE_LIST_FOREACH_END();
  2713. }
  2714. /* }}} */
  2715. /* {{{ Constructor. Throws an Exception in case the given method does not exist */
  2716. ZEND_METHOD(ReflectionMethod, __construct)
  2717. {
  2718. zend_object *arg1_obj;
  2719. zend_string *arg1_str;
  2720. zend_string *arg2_str = NULL;
  2721. zend_object *orig_obj = NULL;
  2722. zend_class_entry *ce = NULL;
  2723. zend_string *class_name = NULL;
  2724. char *method_name;
  2725. size_t method_name_len;
  2726. char *lcname;
  2727. zval *object;
  2728. reflection_object *intern;
  2729. zend_function *mptr;
  2730. ZEND_PARSE_PARAMETERS_START(1, 2)
  2731. Z_PARAM_OBJ_OR_STR(arg1_obj, arg1_str)
  2732. Z_PARAM_OPTIONAL
  2733. Z_PARAM_STR_OR_NULL(arg2_str)
  2734. ZEND_PARSE_PARAMETERS_END();
  2735. if (arg1_obj) {
  2736. if (!arg2_str) {
  2737. zend_argument_value_error(2, "cannot be null when argument #1 ($objectOrMethod) is an object");
  2738. RETURN_THROWS();
  2739. }
  2740. orig_obj = arg1_obj;
  2741. ce = arg1_obj->ce;
  2742. method_name = ZSTR_VAL(arg2_str);
  2743. method_name_len = ZSTR_LEN(arg2_str);
  2744. } else if (arg2_str) {
  2745. class_name = zend_string_copy(arg1_str);
  2746. method_name = ZSTR_VAL(arg2_str);
  2747. method_name_len = ZSTR_LEN(arg2_str);
  2748. } else {
  2749. char *tmp;
  2750. size_t tmp_len;
  2751. char *name = ZSTR_VAL(arg1_str);
  2752. if ((tmp = strstr(name, "::")) == NULL) {
  2753. zend_argument_error(reflection_exception_ptr, 1, "must be a valid method name");
  2754. RETURN_THROWS();
  2755. }
  2756. tmp_len = tmp - name;
  2757. class_name = zend_string_init(name, tmp_len, 0);
  2758. method_name = tmp + 2;
  2759. method_name_len = ZSTR_LEN(arg1_str) - tmp_len - 2;
  2760. }
  2761. if (class_name) {
  2762. if ((ce = zend_lookup_class(class_name)) == NULL) {
  2763. if (!EG(exception)) {
  2764. zend_throw_exception_ex(reflection_exception_ptr, 0, "Class \"%s\" does not exist", ZSTR_VAL(class_name));
  2765. }
  2766. zend_string_release(class_name);
  2767. RETURN_THROWS();
  2768. }
  2769. zend_string_release(class_name);
  2770. }
  2771. object = ZEND_THIS;
  2772. intern = Z_REFLECTION_P(object);
  2773. lcname = zend_str_tolower_dup(method_name, method_name_len);
  2774. if (ce == zend_ce_closure && orig_obj && (method_name_len == sizeof(ZEND_INVOKE_FUNC_NAME)-1)
  2775. && memcmp(lcname, ZEND_INVOKE_FUNC_NAME, sizeof(ZEND_INVOKE_FUNC_NAME)-1) == 0
  2776. && (mptr = zend_get_closure_invoke_method(orig_obj)) != NULL)
  2777. {
  2778. /* do nothing, mptr already set */
  2779. } else if ((mptr = zend_hash_str_find_ptr(&ce->function_table, lcname, method_name_len)) == NULL) {
  2780. efree(lcname);
  2781. zend_throw_exception_ex(reflection_exception_ptr, 0,
  2782. "Method %s::%s() does not exist", ZSTR_VAL(ce->name), method_name);
  2783. RETURN_THROWS();
  2784. }
  2785. efree(lcname);
  2786. ZVAL_STR_COPY(reflection_prop_name(object), mptr->common.function_name);
  2787. ZVAL_STR_COPY(reflection_prop_class(object), mptr->common.scope->name);
  2788. intern->ptr = mptr;
  2789. intern->ref_type = REF_TYPE_FUNCTION;
  2790. intern->ce = ce;
  2791. }
  2792. /* }}} */
  2793. /* {{{ Returns a string representation */
  2794. ZEND_METHOD(ReflectionMethod, __toString)
  2795. {
  2796. reflection_object *intern;
  2797. zend_function *mptr;
  2798. smart_str str = {0};
  2799. if (zend_parse_parameters_none() == FAILURE) {
  2800. RETURN_THROWS();
  2801. }
  2802. GET_REFLECTION_OBJECT_PTR(mptr);
  2803. _function_string(&str, mptr, intern->ce, "");
  2804. RETURN_STR(smart_str_extract(&str));
  2805. }
  2806. /* }}} */
  2807. /* {{{ Invokes the function */
  2808. ZEND_METHOD(ReflectionMethod, getClosure)
  2809. {
  2810. reflection_object *intern;
  2811. zval *obj = NULL;
  2812. zend_function *mptr;
  2813. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|o!", &obj) == FAILURE) {
  2814. RETURN_THROWS();
  2815. }
  2816. GET_REFLECTION_OBJECT_PTR(mptr);
  2817. if (mptr->common.fn_flags & ZEND_ACC_STATIC) {
  2818. zend_create_fake_closure(return_value, mptr, mptr->common.scope, mptr->common.scope, NULL);
  2819. } else {
  2820. if (!obj) {
  2821. zend_argument_value_error(1, "cannot be null for non-static methods");
  2822. RETURN_THROWS();
  2823. }
  2824. if (!instanceof_function(Z_OBJCE_P(obj), mptr->common.scope)) {
  2825. _DO_THROW("Given object is not an instance of the class this method was declared in");
  2826. RETURN_THROWS();
  2827. }
  2828. /* This is an original closure object and __invoke is to be called. */
  2829. if (Z_OBJCE_P(obj) == zend_ce_closure &&
  2830. (mptr->internal_function.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE))
  2831. {
  2832. RETURN_OBJ_COPY(Z_OBJ_P(obj));
  2833. } else {
  2834. zend_create_fake_closure(return_value, mptr, mptr->common.scope, Z_OBJCE_P(obj), obj);
  2835. }
  2836. }
  2837. }
  2838. /* }}} */
  2839. /* {{{ reflection_method_invoke */
  2840. static void reflection_method_invoke(INTERNAL_FUNCTION_PARAMETERS, int variadic)
  2841. {
  2842. zval retval;
  2843. zval *params = NULL, *object;
  2844. HashTable *named_params = NULL;
  2845. reflection_object *intern;
  2846. zend_function *mptr;
  2847. int argc = 0, result;
  2848. zend_fcall_info fci;
  2849. zend_fcall_info_cache fcc;
  2850. zend_class_entry *obj_ce;
  2851. GET_REFLECTION_OBJECT_PTR(mptr);
  2852. if (mptr->common.fn_flags & ZEND_ACC_ABSTRACT) {
  2853. zend_throw_exception_ex(reflection_exception_ptr, 0,
  2854. "Trying to invoke abstract method %s::%s()",
  2855. ZSTR_VAL(mptr->common.scope->name), ZSTR_VAL(mptr->common.function_name));
  2856. RETURN_THROWS();
  2857. }
  2858. if (variadic) {
  2859. ZEND_PARSE_PARAMETERS_START(1, -1)
  2860. Z_PARAM_OBJECT_OR_NULL(object)
  2861. Z_PARAM_VARIADIC_WITH_NAMED(params, argc, named_params)
  2862. ZEND_PARSE_PARAMETERS_END();
  2863. } else {
  2864. if (zend_parse_parameters(ZEND_NUM_ARGS(), "o!h", &object, &named_params) == FAILURE) {
  2865. RETURN_THROWS();
  2866. }
  2867. }
  2868. /* In case this is a static method, we shouldn't pass an object_ptr
  2869. * (which is used as calling context aka $this). We can thus ignore the
  2870. * first parameter.
  2871. *
  2872. * Else, we verify that the given object is an instance of the class.
  2873. */
  2874. if (mptr->common.fn_flags & ZEND_ACC_STATIC) {
  2875. object = NULL;
  2876. obj_ce = mptr->common.scope;
  2877. } else {
  2878. if (!object) {
  2879. zend_throw_exception_ex(reflection_exception_ptr, 0,
  2880. "Trying to invoke non static method %s::%s() without an object",
  2881. ZSTR_VAL(mptr->common.scope->name), ZSTR_VAL(mptr->common.function_name));
  2882. RETURN_THROWS();
  2883. }
  2884. obj_ce = Z_OBJCE_P(object);
  2885. if (!instanceof_function(obj_ce, mptr->common.scope)) {
  2886. if (!variadic) {
  2887. efree(params);
  2888. }
  2889. _DO_THROW("Given object is not an instance of the class this method was declared in");
  2890. RETURN_THROWS();
  2891. }
  2892. }
  2893. fci.size = sizeof(fci);
  2894. ZVAL_UNDEF(&fci.function_name);
  2895. fci.object = object ? Z_OBJ_P(object) : NULL;
  2896. fci.retval = &retval;
  2897. fci.param_count = argc;
  2898. fci.params = params;
  2899. fci.named_params = named_params;
  2900. fcc.function_handler = mptr;
  2901. fcc.called_scope = intern->ce;
  2902. fcc.object = object ? Z_OBJ_P(object) : NULL;
  2903. /*
  2904. * Copy the zend_function when calling via handler (e.g. Closure::__invoke())
  2905. */
  2906. if ((mptr->internal_function.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
  2907. fcc.function_handler = _copy_function(mptr);
  2908. }
  2909. result = zend_call_function(&fci, &fcc);
  2910. if (result == FAILURE) {
  2911. zend_throw_exception_ex(reflection_exception_ptr, 0,
  2912. "Invocation of method %s::%s() failed", ZSTR_VAL(mptr->common.scope->name), ZSTR_VAL(mptr->common.function_name));
  2913. RETURN_THROWS();
  2914. }
  2915. if (Z_TYPE(retval) != IS_UNDEF) {
  2916. if (Z_ISREF(retval)) {
  2917. zend_unwrap_reference(&retval);
  2918. }
  2919. ZVAL_COPY_VALUE(return_value, &retval);
  2920. }
  2921. }
  2922. /* }}} */
  2923. /* {{{ Invokes the method. */
  2924. ZEND_METHOD(ReflectionMethod, invoke)
  2925. {
  2926. reflection_method_invoke(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  2927. }
  2928. /* }}} */
  2929. /* {{{ Invokes the function and pass its arguments as array. */
  2930. ZEND_METHOD(ReflectionMethod, invokeArgs)
  2931. {
  2932. reflection_method_invoke(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  2933. }
  2934. /* }}} */
  2935. /* {{{ Returns whether this method is final */
  2936. ZEND_METHOD(ReflectionMethod, isFinal)
  2937. {
  2938. _function_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_FINAL);
  2939. }
  2940. /* }}} */
  2941. /* {{{ Returns whether this method is abstract */
  2942. ZEND_METHOD(ReflectionMethod, isAbstract)
  2943. {
  2944. _function_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_ABSTRACT);
  2945. }
  2946. /* }}} */
  2947. /* {{{ Returns whether this method is public */
  2948. ZEND_METHOD(ReflectionMethod, isPublic)
  2949. {
  2950. _function_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PUBLIC);
  2951. }
  2952. /* }}} */
  2953. /* {{{ Returns whether this method is private */
  2954. ZEND_METHOD(ReflectionMethod, isPrivate)
  2955. {
  2956. _function_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PRIVATE);
  2957. }
  2958. /* }}} */
  2959. /* {{{ Returns whether this method is protected */
  2960. ZEND_METHOD(ReflectionMethod, isProtected)
  2961. {
  2962. _function_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PROTECTED);
  2963. }
  2964. /* }}} */
  2965. /* {{{ Returns whether this function is deprecated */
  2966. ZEND_METHOD(ReflectionFunctionAbstract, isDeprecated)
  2967. {
  2968. _function_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_DEPRECATED);
  2969. }
  2970. /* }}} */
  2971. /* {{{ Returns whether this function is a generator */
  2972. ZEND_METHOD(ReflectionFunctionAbstract, isGenerator)
  2973. {
  2974. _function_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_GENERATOR);
  2975. }
  2976. /* }}} */
  2977. /* {{{ Returns whether this function is variadic */
  2978. ZEND_METHOD(ReflectionFunctionAbstract, isVariadic)
  2979. {
  2980. _function_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_VARIADIC);
  2981. }
  2982. /* }}} */
  2983. /* {{{ Returns whether this function is static */
  2984. ZEND_METHOD(ReflectionFunctionAbstract, isStatic)
  2985. {
  2986. _function_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_STATIC);
  2987. }
  2988. /* }}} */
  2989. /* {{{ Returns whether this function is defined in namespace */
  2990. ZEND_METHOD(ReflectionFunctionAbstract, inNamespace)
  2991. {
  2992. reflection_object *intern;
  2993. zend_function *fptr;
  2994. if (zend_parse_parameters_none() == FAILURE) {
  2995. RETURN_THROWS();
  2996. }
  2997. GET_REFLECTION_OBJECT_PTR(fptr);
  2998. zend_string *name = fptr->common.function_name;
  2999. const char *backslash = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
  3000. RETURN_BOOL(backslash && backslash > ZSTR_VAL(name));
  3001. }
  3002. /* }}} */
  3003. /* {{{ Returns the name of namespace where this function is defined */
  3004. ZEND_METHOD(ReflectionFunctionAbstract, getNamespaceName)
  3005. {
  3006. reflection_object *intern;
  3007. zend_function *fptr;
  3008. if (zend_parse_parameters_none() == FAILURE) {
  3009. RETURN_THROWS();
  3010. }
  3011. GET_REFLECTION_OBJECT_PTR(fptr);
  3012. zend_string *name = fptr->common.function_name;
  3013. const char *backslash = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
  3014. if (backslash && backslash > ZSTR_VAL(name)) {
  3015. RETURN_STRINGL(ZSTR_VAL(name), backslash - ZSTR_VAL(name));
  3016. }
  3017. RETURN_EMPTY_STRING();
  3018. }
  3019. /* }}} */
  3020. /* {{{ Returns the short name of the function (without namespace part) */
  3021. ZEND_METHOD(ReflectionFunctionAbstract, getShortName)
  3022. {
  3023. reflection_object *intern;
  3024. zend_function *fptr;
  3025. if (zend_parse_parameters_none() == FAILURE) {
  3026. RETURN_THROWS();
  3027. }
  3028. GET_REFLECTION_OBJECT_PTR(fptr);
  3029. zend_string *name = fptr->common.function_name;
  3030. const char *backslash = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
  3031. if (backslash && backslash > ZSTR_VAL(name)) {
  3032. RETURN_STRINGL(backslash + 1, ZSTR_LEN(name) - (backslash - ZSTR_VAL(name) + 1));
  3033. }
  3034. RETURN_STR_COPY(name);
  3035. }
  3036. /* }}} */
  3037. /* {{{ Return whether the function has a return type */
  3038. ZEND_METHOD(ReflectionFunctionAbstract, hasReturnType)
  3039. {
  3040. reflection_object *intern;
  3041. zend_function *fptr;
  3042. if (zend_parse_parameters_none() == FAILURE) {
  3043. RETURN_THROWS();
  3044. }
  3045. GET_REFLECTION_OBJECT_PTR(fptr);
  3046. RETVAL_BOOL((fptr->op_array.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) && !ZEND_ARG_TYPE_IS_TENTATIVE(&fptr->common.arg_info[-1]));
  3047. }
  3048. /* }}} */
  3049. /* {{{ Returns the return type associated with the function */
  3050. ZEND_METHOD(ReflectionFunctionAbstract, getReturnType)
  3051. {
  3052. reflection_object *intern;
  3053. zend_function *fptr;
  3054. if (zend_parse_parameters_none() == FAILURE) {
  3055. RETURN_THROWS();
  3056. }
  3057. GET_REFLECTION_OBJECT_PTR(fptr);
  3058. if (!(fptr->op_array.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) || ZEND_ARG_TYPE_IS_TENTATIVE(&fptr->common.arg_info[-1])) {
  3059. RETURN_NULL();
  3060. }
  3061. reflection_type_factory(fptr->common.arg_info[-1].type, return_value, 1);
  3062. }
  3063. /* }}} */
  3064. /* {{{ Return whether the function has a return type */
  3065. ZEND_METHOD(ReflectionFunctionAbstract, hasTentativeReturnType)
  3066. {
  3067. reflection_object *intern;
  3068. zend_function *fptr;
  3069. if (zend_parse_parameters_none() == FAILURE) {
  3070. RETURN_THROWS();
  3071. }
  3072. GET_REFLECTION_OBJECT_PTR(fptr);
  3073. RETVAL_BOOL(fptr->op_array.fn_flags & ZEND_ACC_HAS_RETURN_TYPE && ZEND_ARG_TYPE_IS_TENTATIVE(&fptr->common.arg_info[-1]));
  3074. }
  3075. /* }}} */
  3076. /* {{{ Returns the return type associated with the function */
  3077. ZEND_METHOD(ReflectionFunctionAbstract, getTentativeReturnType)
  3078. {
  3079. reflection_object *intern;
  3080. zend_function *fptr;
  3081. if (zend_parse_parameters_none() == FAILURE) {
  3082. RETURN_THROWS();
  3083. }
  3084. GET_REFLECTION_OBJECT_PTR(fptr);
  3085. if (!(fptr->op_array.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) || !ZEND_ARG_TYPE_IS_TENTATIVE(&fptr->common.arg_info[-1])) {
  3086. RETURN_NULL();
  3087. }
  3088. reflection_type_factory(fptr->common.arg_info[-1].type, return_value, 1);
  3089. }
  3090. /* }}} */
  3091. /* {{{ Returns whether this method is the constructor */
  3092. ZEND_METHOD(ReflectionMethod, isConstructor)
  3093. {
  3094. reflection_object *intern;
  3095. zend_function *mptr;
  3096. if (zend_parse_parameters_none() == FAILURE) {
  3097. RETURN_THROWS();
  3098. }
  3099. GET_REFLECTION_OBJECT_PTR(mptr);
  3100. /* we need to check if the ctor is the ctor of the class level we we
  3101. * looking at since we might be looking at an inherited old style ctor
  3102. * defined in base class. */
  3103. RETURN_BOOL((mptr->common.fn_flags & ZEND_ACC_CTOR) && intern->ce->constructor && intern->ce->constructor->common.scope == mptr->common.scope);
  3104. }
  3105. /* }}} */
  3106. /* {{{ Returns whether this method is a destructor */
  3107. ZEND_METHOD(ReflectionMethod, isDestructor)
  3108. {
  3109. reflection_object *intern;
  3110. zend_function *mptr;
  3111. if (zend_parse_parameters_none() == FAILURE) {
  3112. RETURN_THROWS();
  3113. }
  3114. GET_REFLECTION_OBJECT_PTR(mptr);
  3115. RETURN_BOOL(zend_string_equals_literal_ci(
  3116. mptr->common.function_name, ZEND_DESTRUCTOR_FUNC_NAME));
  3117. }
  3118. /* }}} */
  3119. /* {{{ Returns a bitfield of the access modifiers for this method */
  3120. ZEND_METHOD(ReflectionMethod, getModifiers)
  3121. {
  3122. reflection_object *intern;
  3123. zend_function *mptr;
  3124. uint32_t keep_flags = ZEND_ACC_PPP_MASK
  3125. | ZEND_ACC_STATIC | ZEND_ACC_ABSTRACT | ZEND_ACC_FINAL;
  3126. if (zend_parse_parameters_none() == FAILURE) {
  3127. RETURN_THROWS();
  3128. }
  3129. GET_REFLECTION_OBJECT_PTR(mptr);
  3130. RETURN_LONG((mptr->common.fn_flags & keep_flags));
  3131. }
  3132. /* }}} */
  3133. /* {{{ Get the declaring class */
  3134. ZEND_METHOD(ReflectionMethod, getDeclaringClass)
  3135. {
  3136. reflection_object *intern;
  3137. zend_function *mptr;
  3138. GET_REFLECTION_OBJECT_PTR(mptr);
  3139. if (zend_parse_parameters_none() == FAILURE) {
  3140. RETURN_THROWS();
  3141. }
  3142. zend_reflection_class_factory(mptr->common.scope, return_value);
  3143. }
  3144. /* }}} */
  3145. /* {{{ Get the prototype */
  3146. ZEND_METHOD(ReflectionMethod, getPrototype)
  3147. {
  3148. reflection_object *intern;
  3149. zend_function *mptr;
  3150. GET_REFLECTION_OBJECT_PTR(mptr);
  3151. if (zend_parse_parameters_none() == FAILURE) {
  3152. RETURN_THROWS();
  3153. }
  3154. if (!mptr->common.prototype) {
  3155. zend_throw_exception_ex(reflection_exception_ptr, 0,
  3156. "Method %s::%s does not have a prototype", ZSTR_VAL(intern->ce->name), ZSTR_VAL(mptr->common.function_name));
  3157. RETURN_THROWS();
  3158. }
  3159. reflection_method_factory(mptr->common.prototype->common.scope, mptr->common.prototype, NULL, return_value);
  3160. }
  3161. /* }}} */
  3162. /* {{{ Sets whether non-public methods can be invoked */
  3163. ZEND_METHOD(ReflectionMethod, setAccessible)
  3164. {
  3165. bool visible;
  3166. if (zend_parse_parameters(ZEND_NUM_ARGS(), "b", &visible) == FAILURE) {
  3167. RETURN_THROWS();
  3168. }
  3169. }
  3170. /* }}} */
  3171. /* {{{ Constructor. Throws an Exception in case the given class constant does not exist */
  3172. ZEND_METHOD(ReflectionClassConstant, __construct)
  3173. {
  3174. zval *object;
  3175. zend_string *classname_str;
  3176. zend_object *classname_obj;
  3177. zend_string *constname;
  3178. reflection_object *intern;
  3179. zend_class_entry *ce;
  3180. zend_class_constant *constant = NULL;
  3181. ZEND_PARSE_PARAMETERS_START(2, 2)
  3182. Z_PARAM_OBJ_OR_STR(classname_obj, classname_str)
  3183. Z_PARAM_STR(constname)
  3184. ZEND_PARSE_PARAMETERS_END();
  3185. if (classname_obj) {
  3186. ce = classname_obj->ce;
  3187. } else {
  3188. if ((ce = zend_lookup_class(classname_str)) == NULL) {
  3189. zend_throw_exception_ex(reflection_exception_ptr, 0, "Class \"%s\" does not exist", ZSTR_VAL(classname_str));
  3190. RETURN_THROWS();
  3191. }
  3192. }
  3193. object = ZEND_THIS;
  3194. intern = Z_REFLECTION_P(object);
  3195. if ((constant = zend_hash_find_ptr(CE_CONSTANTS_TABLE(ce), constname)) == NULL) {
  3196. zend_throw_exception_ex(reflection_exception_ptr, 0, "Constant %s::%s does not exist", ZSTR_VAL(ce->name), ZSTR_VAL(constname));
  3197. RETURN_THROWS();
  3198. }
  3199. intern->ptr = constant;
  3200. intern->ref_type = REF_TYPE_CLASS_CONSTANT;
  3201. intern->ce = constant->ce;
  3202. ZVAL_STR_COPY(reflection_prop_name(object), constname);
  3203. ZVAL_STR_COPY(reflection_prop_class(object), constant->ce->name);
  3204. }
  3205. /* }}} */
  3206. /* {{{ Returns a string representation */
  3207. ZEND_METHOD(ReflectionClassConstant, __toString)
  3208. {
  3209. reflection_object *intern;
  3210. zend_class_constant *ref;
  3211. smart_str str = {0};
  3212. if (zend_parse_parameters_none() == FAILURE) {
  3213. RETURN_THROWS();
  3214. }
  3215. GET_REFLECTION_OBJECT_PTR(ref);
  3216. zval *name = reflection_prop_name(ZEND_THIS);
  3217. if (Z_ISUNDEF_P(name)) {
  3218. zend_throw_error(NULL,
  3219. "Typed property ReflectionClassConstant::$name "
  3220. "must not be accessed before initialization");
  3221. RETURN_THROWS();
  3222. }
  3223. ZVAL_DEREF(name);
  3224. ZEND_ASSERT(Z_TYPE_P(name) == IS_STRING);
  3225. _class_const_string(&str, Z_STRVAL_P(name), ref, "");
  3226. RETURN_STR(smart_str_extract(&str));
  3227. }
  3228. /* }}} */
  3229. /* {{{ Returns the constant' name */
  3230. ZEND_METHOD(ReflectionClassConstant, getName)
  3231. {
  3232. if (zend_parse_parameters_none() == FAILURE) {
  3233. RETURN_THROWS();
  3234. }
  3235. zval *name = reflection_prop_name(ZEND_THIS);
  3236. if (Z_ISUNDEF_P(name)) {
  3237. zend_throw_error(NULL,
  3238. "Typed property ReflectionClassConstant::$name "
  3239. "must not be accessed before initialization");
  3240. RETURN_THROWS();
  3241. }
  3242. RETURN_COPY_DEREF(name);
  3243. }
  3244. /* }}} */
  3245. static void _class_constant_check_flag(INTERNAL_FUNCTION_PARAMETERS, int mask) /* {{{ */
  3246. {
  3247. reflection_object *intern;
  3248. zend_class_constant *ref;
  3249. if (zend_parse_parameters_none() == FAILURE) {
  3250. RETURN_THROWS();
  3251. }
  3252. GET_REFLECTION_OBJECT_PTR(ref);
  3253. RETURN_BOOL(ZEND_CLASS_CONST_FLAGS(ref) & mask);
  3254. }
  3255. /* }}} */
  3256. /* {{{ Returns whether this constant is public */
  3257. ZEND_METHOD(ReflectionClassConstant, isPublic)
  3258. {
  3259. _class_constant_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PUBLIC);
  3260. }
  3261. /* }}} */
  3262. /* {{{ Returns whether this constant is private */
  3263. ZEND_METHOD(ReflectionClassConstant, isPrivate)
  3264. {
  3265. _class_constant_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PRIVATE);
  3266. }
  3267. /* }}} */
  3268. /* {{{ Returns whether this constant is protected */
  3269. ZEND_METHOD(ReflectionClassConstant, isProtected)
  3270. {
  3271. _class_constant_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PROTECTED);
  3272. }
  3273. /* }}} */
  3274. /* Returns whether this constant is final */
  3275. ZEND_METHOD(ReflectionClassConstant, isFinal)
  3276. {
  3277. _class_constant_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_FINAL);
  3278. }
  3279. /* {{{ Returns a bitfield of the access modifiers for this constant */
  3280. ZEND_METHOD(ReflectionClassConstant, getModifiers)
  3281. {
  3282. reflection_object *intern;
  3283. zend_class_constant *ref;
  3284. uint32_t keep_flags = ZEND_ACC_FINAL | ZEND_ACC_PPP_MASK;
  3285. if (zend_parse_parameters_none() == FAILURE) {
  3286. RETURN_THROWS();
  3287. }
  3288. GET_REFLECTION_OBJECT_PTR(ref);
  3289. RETURN_LONG(ZEND_CLASS_CONST_FLAGS(ref) & keep_flags);
  3290. }
  3291. /* }}} */
  3292. /* {{{ Returns this constant's value */
  3293. ZEND_METHOD(ReflectionClassConstant, getValue)
  3294. {
  3295. reflection_object *intern;
  3296. zend_class_constant *ref;
  3297. if (zend_parse_parameters_none() == FAILURE) {
  3298. RETURN_THROWS();
  3299. }
  3300. GET_REFLECTION_OBJECT_PTR(ref);
  3301. if (Z_TYPE(ref->value) == IS_CONSTANT_AST) {
  3302. zval_update_constant_ex(&ref->value, ref->ce);
  3303. }
  3304. ZVAL_COPY_OR_DUP(return_value, &ref->value);
  3305. }
  3306. /* }}} */
  3307. /* {{{ Get the declaring class */
  3308. ZEND_METHOD(ReflectionClassConstant, getDeclaringClass)
  3309. {
  3310. reflection_object *intern;
  3311. zend_class_constant *ref;
  3312. if (zend_parse_parameters_none() == FAILURE) {
  3313. RETURN_THROWS();
  3314. }
  3315. GET_REFLECTION_OBJECT_PTR(ref);
  3316. zend_reflection_class_factory(ref->ce, return_value);
  3317. }
  3318. /* }}} */
  3319. /* {{{ Returns the doc comment for this constant */
  3320. ZEND_METHOD(ReflectionClassConstant, getDocComment)
  3321. {
  3322. reflection_object *intern;
  3323. zend_class_constant *ref;
  3324. if (zend_parse_parameters_none() == FAILURE) {
  3325. RETURN_THROWS();
  3326. }
  3327. GET_REFLECTION_OBJECT_PTR(ref);
  3328. if (ref->doc_comment) {
  3329. RETURN_STR_COPY(ref->doc_comment);
  3330. }
  3331. RETURN_FALSE;
  3332. }
  3333. /* }}} */
  3334. /* {{{ Returns the attributes of this constant */
  3335. ZEND_METHOD(ReflectionClassConstant, getAttributes)
  3336. {
  3337. reflection_object *intern;
  3338. zend_class_constant *ref;
  3339. GET_REFLECTION_OBJECT_PTR(ref);
  3340. reflect_attributes(INTERNAL_FUNCTION_PARAM_PASSTHRU,
  3341. ref->attributes, 0, ref->ce, ZEND_ATTRIBUTE_TARGET_CLASS_CONST,
  3342. ref->ce->type == ZEND_USER_CLASS ? ref->ce->info.user.filename : NULL);
  3343. }
  3344. /* }}} */
  3345. ZEND_METHOD(ReflectionClassConstant, isEnumCase)
  3346. {
  3347. reflection_object *intern;
  3348. zend_class_constant *ref;
  3349. GET_REFLECTION_OBJECT_PTR(ref);
  3350. RETURN_BOOL(ZEND_CLASS_CONST_FLAGS(ref) & ZEND_CLASS_CONST_IS_CASE);
  3351. }
  3352. /* {{{ reflection_class_object_ctor */
  3353. static void reflection_class_object_ctor(INTERNAL_FUNCTION_PARAMETERS, int is_object)
  3354. {
  3355. zval *object;
  3356. zend_string *arg_class = NULL;
  3357. zend_object *arg_obj;
  3358. reflection_object *intern;
  3359. zend_class_entry *ce;
  3360. if (is_object) {
  3361. ZEND_PARSE_PARAMETERS_START(1, 1)
  3362. Z_PARAM_OBJ(arg_obj)
  3363. ZEND_PARSE_PARAMETERS_END();
  3364. } else {
  3365. ZEND_PARSE_PARAMETERS_START(1, 1)
  3366. Z_PARAM_OBJ_OR_STR(arg_obj, arg_class)
  3367. ZEND_PARSE_PARAMETERS_END();
  3368. }
  3369. object = ZEND_THIS;
  3370. intern = Z_REFLECTION_P(object);
  3371. if (arg_obj) {
  3372. ZVAL_STR_COPY(reflection_prop_name(object), arg_obj->ce->name);
  3373. intern->ptr = arg_obj->ce;
  3374. if (is_object) {
  3375. ZVAL_OBJ_COPY(&intern->obj, arg_obj);
  3376. }
  3377. } else {
  3378. if ((ce = zend_lookup_class(arg_class)) == NULL) {
  3379. if (!EG(exception)) {
  3380. zend_throw_exception_ex(reflection_exception_ptr, -1, "Class \"%s\" does not exist", ZSTR_VAL(arg_class));
  3381. }
  3382. RETURN_THROWS();
  3383. }
  3384. ZVAL_STR_COPY(reflection_prop_name(object), ce->name);
  3385. intern->ptr = ce;
  3386. }
  3387. intern->ref_type = REF_TYPE_OTHER;
  3388. }
  3389. /* }}} */
  3390. /* {{{ Constructor. Takes a string or an instance as an argument */
  3391. ZEND_METHOD(ReflectionClass, __construct)
  3392. {
  3393. reflection_class_object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  3394. }
  3395. /* }}} */
  3396. /* {{{ add_class_vars */
  3397. static void add_class_vars(zend_class_entry *ce, bool statics, zval *return_value)
  3398. {
  3399. zend_property_info *prop_info;
  3400. zval *prop, prop_copy;
  3401. zend_string *key;
  3402. ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->properties_info, key, prop_info) {
  3403. if (((prop_info->flags & ZEND_ACC_PRIVATE) &&
  3404. prop_info->ce != ce)) {
  3405. continue;
  3406. }
  3407. bool is_static = (prop_info->flags & ZEND_ACC_STATIC) != 0;
  3408. if (statics != is_static) {
  3409. continue;
  3410. }
  3411. prop = property_get_default(prop_info);
  3412. if (Z_ISUNDEF_P(prop)) {
  3413. continue;
  3414. }
  3415. /* copy: enforce read only access */
  3416. ZVAL_DEREF(prop);
  3417. ZVAL_COPY_OR_DUP(&prop_copy, prop);
  3418. /* this is necessary to make it able to work with default array
  3419. * properties, returned to user */
  3420. if (Z_TYPE(prop_copy) == IS_CONSTANT_AST) {
  3421. if (UNEXPECTED(zval_update_constant_ex(&prop_copy, ce) != SUCCESS)) {
  3422. return;
  3423. }
  3424. }
  3425. zend_hash_update(Z_ARRVAL_P(return_value), key, &prop_copy);
  3426. } ZEND_HASH_FOREACH_END();
  3427. }
  3428. /* }}} */
  3429. /* {{{ Returns an associative array containing all static property values of the class */
  3430. ZEND_METHOD(ReflectionClass, getStaticProperties)
  3431. {
  3432. reflection_object *intern;
  3433. zend_class_entry *ce;
  3434. zend_property_info *prop_info;
  3435. zval *prop;
  3436. zend_string *key;
  3437. if (zend_parse_parameters_none() == FAILURE) {
  3438. RETURN_THROWS();
  3439. }
  3440. GET_REFLECTION_OBJECT_PTR(ce);
  3441. if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
  3442. RETURN_THROWS();
  3443. }
  3444. if (ce->default_static_members_count && !CE_STATIC_MEMBERS(ce)) {
  3445. zend_class_init_statics(ce);
  3446. }
  3447. array_init(return_value);
  3448. ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->properties_info, key, prop_info) {
  3449. if (((prop_info->flags & ZEND_ACC_PRIVATE) &&
  3450. prop_info->ce != ce)) {
  3451. continue;
  3452. }
  3453. if ((prop_info->flags & ZEND_ACC_STATIC) == 0) {
  3454. continue;
  3455. }
  3456. prop = &CE_STATIC_MEMBERS(ce)[prop_info->offset];
  3457. ZVAL_DEINDIRECT(prop);
  3458. if (ZEND_TYPE_IS_SET(prop_info->type) && Z_ISUNDEF_P(prop)) {
  3459. continue;
  3460. }
  3461. /* enforce read only access */
  3462. ZVAL_DEREF(prop);
  3463. Z_TRY_ADDREF_P(prop);
  3464. zend_hash_update(Z_ARRVAL_P(return_value), key, prop);
  3465. } ZEND_HASH_FOREACH_END();
  3466. }
  3467. /* }}} */
  3468. /* {{{ Returns the value of a static property */
  3469. ZEND_METHOD(ReflectionClass, getStaticPropertyValue)
  3470. {
  3471. reflection_object *intern;
  3472. zend_class_entry *ce, *old_scope;
  3473. zend_string *name;
  3474. zval *prop, *def_value = NULL;
  3475. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|z", &name, &def_value) == FAILURE) {
  3476. RETURN_THROWS();
  3477. }
  3478. GET_REFLECTION_OBJECT_PTR(ce);
  3479. if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
  3480. RETURN_THROWS();
  3481. }
  3482. old_scope = EG(fake_scope);
  3483. EG(fake_scope) = ce;
  3484. prop = zend_std_get_static_property(ce, name, BP_VAR_IS);
  3485. EG(fake_scope) = old_scope;
  3486. if (prop) {
  3487. RETURN_COPY_DEREF(prop);
  3488. }
  3489. if (def_value) {
  3490. RETURN_COPY(def_value);
  3491. }
  3492. zend_throw_exception_ex(reflection_exception_ptr, 0,
  3493. "Property %s::$%s does not exist", ZSTR_VAL(ce->name), ZSTR_VAL(name));
  3494. }
  3495. /* }}} */
  3496. /* {{{ Sets the value of a static property */
  3497. ZEND_METHOD(ReflectionClass, setStaticPropertyValue)
  3498. {
  3499. reflection_object *intern;
  3500. zend_class_entry *ce, *old_scope;
  3501. zend_property_info *prop_info;
  3502. zend_string *name;
  3503. zval *variable_ptr, *value;
  3504. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sz", &name, &value) == FAILURE) {
  3505. RETURN_THROWS();
  3506. }
  3507. GET_REFLECTION_OBJECT_PTR(ce);
  3508. if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
  3509. RETURN_THROWS();
  3510. }
  3511. old_scope = EG(fake_scope);
  3512. EG(fake_scope) = ce;
  3513. variable_ptr = zend_std_get_static_property_with_info(ce, name, BP_VAR_W, &prop_info);
  3514. EG(fake_scope) = old_scope;
  3515. if (!variable_ptr) {
  3516. zend_clear_exception();
  3517. zend_throw_exception_ex(reflection_exception_ptr, 0,
  3518. "Class %s does not have a property named %s", ZSTR_VAL(ce->name), ZSTR_VAL(name));
  3519. RETURN_THROWS();
  3520. }
  3521. if (Z_ISREF_P(variable_ptr)) {
  3522. zend_reference *ref = Z_REF_P(variable_ptr);
  3523. variable_ptr = Z_REFVAL_P(variable_ptr);
  3524. if (!zend_verify_ref_assignable_zval(ref, value, 0)) {
  3525. return;
  3526. }
  3527. }
  3528. if (ZEND_TYPE_IS_SET(prop_info->type) && !zend_verify_property_type(prop_info, value, 0)) {
  3529. return;
  3530. }
  3531. zval_ptr_dtor(variable_ptr);
  3532. ZVAL_COPY(variable_ptr, value);
  3533. }
  3534. /* }}} */
  3535. /* {{{ Returns an associative array containing copies of all default property values of the class */
  3536. ZEND_METHOD(ReflectionClass, getDefaultProperties)
  3537. {
  3538. reflection_object *intern;
  3539. zend_class_entry *ce;
  3540. if (zend_parse_parameters_none() == FAILURE) {
  3541. RETURN_THROWS();
  3542. }
  3543. GET_REFLECTION_OBJECT_PTR(ce);
  3544. array_init(return_value);
  3545. if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
  3546. RETURN_THROWS();
  3547. }
  3548. add_class_vars(ce, 1, return_value);
  3549. add_class_vars(ce, 0, return_value);
  3550. }
  3551. /* }}} */
  3552. /* {{{ Returns a string representation */
  3553. ZEND_METHOD(ReflectionClass, __toString)
  3554. {
  3555. reflection_object *intern;
  3556. zend_class_entry *ce;
  3557. smart_str str = {0};
  3558. if (zend_parse_parameters_none() == FAILURE) {
  3559. RETURN_THROWS();
  3560. }
  3561. GET_REFLECTION_OBJECT_PTR(ce);
  3562. _class_string(&str, ce, &intern->obj, "");
  3563. RETURN_STR(smart_str_extract(&str));
  3564. }
  3565. /* }}} */
  3566. /* {{{ Returns the class' name */
  3567. ZEND_METHOD(ReflectionClass, getName)
  3568. {
  3569. reflection_object *intern;
  3570. zend_class_entry *ce;
  3571. if (zend_parse_parameters_none() == FAILURE) {
  3572. RETURN_THROWS();
  3573. }
  3574. GET_REFLECTION_OBJECT_PTR(ce);
  3575. RETURN_STR_COPY(ce->name);
  3576. }
  3577. /* }}} */
  3578. /* {{{ Returns whether this class is an internal class */
  3579. ZEND_METHOD(ReflectionClass, isInternal)
  3580. {
  3581. reflection_object *intern;
  3582. zend_class_entry *ce;
  3583. if (zend_parse_parameters_none() == FAILURE) {
  3584. RETURN_THROWS();
  3585. }
  3586. GET_REFLECTION_OBJECT_PTR(ce);
  3587. RETURN_BOOL(ce->type == ZEND_INTERNAL_CLASS);
  3588. }
  3589. /* }}} */
  3590. /* {{{ Returns whether this class is user-defined */
  3591. ZEND_METHOD(ReflectionClass, isUserDefined)
  3592. {
  3593. reflection_object *intern;
  3594. zend_class_entry *ce;
  3595. if (zend_parse_parameters_none() == FAILURE) {
  3596. RETURN_THROWS();
  3597. }
  3598. GET_REFLECTION_OBJECT_PTR(ce);
  3599. RETURN_BOOL(ce->type == ZEND_USER_CLASS);
  3600. }
  3601. /* }}} */
  3602. /* {{{ Returns whether this class is anonymous */
  3603. ZEND_METHOD(ReflectionClass, isAnonymous)
  3604. {
  3605. reflection_object *intern;
  3606. zend_class_entry *ce;
  3607. if (zend_parse_parameters_none() == FAILURE) {
  3608. RETURN_THROWS();
  3609. }
  3610. GET_REFLECTION_OBJECT_PTR(ce);
  3611. RETURN_BOOL(ce->ce_flags & ZEND_ACC_ANON_CLASS);
  3612. }
  3613. /* }}} */
  3614. /* {{{ Returns the filename of the file this class was declared in */
  3615. ZEND_METHOD(ReflectionClass, getFileName)
  3616. {
  3617. reflection_object *intern;
  3618. zend_class_entry *ce;
  3619. if (zend_parse_parameters_none() == FAILURE) {
  3620. RETURN_THROWS();
  3621. }
  3622. GET_REFLECTION_OBJECT_PTR(ce);
  3623. if (ce->type == ZEND_USER_CLASS) {
  3624. RETURN_STR_COPY(ce->info.user.filename);
  3625. }
  3626. RETURN_FALSE;
  3627. }
  3628. /* }}} */
  3629. /* {{{ Returns the line this class' declaration starts at */
  3630. ZEND_METHOD(ReflectionClass, getStartLine)
  3631. {
  3632. reflection_object *intern;
  3633. zend_class_entry *ce;
  3634. if (zend_parse_parameters_none() == FAILURE) {
  3635. RETURN_THROWS();
  3636. }
  3637. GET_REFLECTION_OBJECT_PTR(ce);
  3638. if (ce->type == ZEND_USER_CLASS) {
  3639. RETURN_LONG(ce->info.user.line_start);
  3640. }
  3641. RETURN_FALSE;
  3642. }
  3643. /* }}} */
  3644. /* {{{ Returns the line this class' declaration ends at */
  3645. ZEND_METHOD(ReflectionClass, getEndLine)
  3646. {
  3647. reflection_object *intern;
  3648. zend_class_entry *ce;
  3649. if (zend_parse_parameters_none() == FAILURE) {
  3650. RETURN_THROWS();
  3651. }
  3652. GET_REFLECTION_OBJECT_PTR(ce);
  3653. if (ce->type == ZEND_USER_CLASS) {
  3654. RETURN_LONG(ce->info.user.line_end);
  3655. }
  3656. RETURN_FALSE;
  3657. }
  3658. /* }}} */
  3659. /* {{{ Returns the doc comment for this class */
  3660. ZEND_METHOD(ReflectionClass, getDocComment)
  3661. {
  3662. reflection_object *intern;
  3663. zend_class_entry *ce;
  3664. if (zend_parse_parameters_none() == FAILURE) {
  3665. RETURN_THROWS();
  3666. }
  3667. GET_REFLECTION_OBJECT_PTR(ce);
  3668. if (ce->type == ZEND_USER_CLASS && ce->info.user.doc_comment) {
  3669. RETURN_STR_COPY(ce->info.user.doc_comment);
  3670. }
  3671. RETURN_FALSE;
  3672. }
  3673. /* }}} */
  3674. /* {{{ Returns the attributes for this class */
  3675. ZEND_METHOD(ReflectionClass, getAttributes)
  3676. {
  3677. reflection_object *intern;
  3678. zend_class_entry *ce;
  3679. GET_REFLECTION_OBJECT_PTR(ce);
  3680. reflect_attributes(INTERNAL_FUNCTION_PARAM_PASSTHRU,
  3681. ce->attributes, 0, ce, ZEND_ATTRIBUTE_TARGET_CLASS,
  3682. ce->type == ZEND_USER_CLASS ? ce->info.user.filename : NULL);
  3683. }
  3684. /* }}} */
  3685. /* {{{ Returns the class' constructor if there is one, NULL otherwise */
  3686. ZEND_METHOD(ReflectionClass, getConstructor)
  3687. {
  3688. reflection_object *intern;
  3689. zend_class_entry *ce;
  3690. if (zend_parse_parameters_none() == FAILURE) {
  3691. RETURN_THROWS();
  3692. }
  3693. GET_REFLECTION_OBJECT_PTR(ce);
  3694. if (ce->constructor) {
  3695. reflection_method_factory(ce, ce->constructor, NULL, return_value);
  3696. } else {
  3697. RETURN_NULL();
  3698. }
  3699. }
  3700. /* }}} */
  3701. /* {{{ Returns whether a method exists or not */
  3702. ZEND_METHOD(ReflectionClass, hasMethod)
  3703. {
  3704. reflection_object *intern;
  3705. zend_class_entry *ce;
  3706. zend_string *name, *lc_name;
  3707. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
  3708. RETURN_THROWS();
  3709. }
  3710. GET_REFLECTION_OBJECT_PTR(ce);
  3711. lc_name = zend_string_tolower(name);
  3712. RETVAL_BOOL(zend_hash_exists(&ce->function_table, lc_name) || is_closure_invoke(ce, lc_name));
  3713. zend_string_release(lc_name);
  3714. }
  3715. /* }}} */
  3716. /* {{{ Returns the class' method specified by its name */
  3717. ZEND_METHOD(ReflectionClass, getMethod)
  3718. {
  3719. reflection_object *intern;
  3720. zend_class_entry *ce;
  3721. zend_function *mptr;
  3722. zval obj_tmp;
  3723. zend_string *name, *lc_name;
  3724. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
  3725. RETURN_THROWS();
  3726. }
  3727. GET_REFLECTION_OBJECT_PTR(ce);
  3728. lc_name = zend_string_tolower(name);
  3729. if (!Z_ISUNDEF(intern->obj) && is_closure_invoke(ce, lc_name)
  3730. && (mptr = zend_get_closure_invoke_method(Z_OBJ(intern->obj))) != NULL)
  3731. {
  3732. /* don't assign closure_object since we only reflect the invoke handler
  3733. method and not the closure definition itself */
  3734. reflection_method_factory(ce, mptr, NULL, return_value);
  3735. } else if (Z_ISUNDEF(intern->obj) && is_closure_invoke(ce, lc_name)
  3736. && object_init_ex(&obj_tmp, ce) == SUCCESS && (mptr = zend_get_closure_invoke_method(Z_OBJ(obj_tmp))) != NULL) {
  3737. /* don't assign closure_object since we only reflect the invoke handler
  3738. method and not the closure definition itself */
  3739. reflection_method_factory(ce, mptr, NULL, return_value);
  3740. zval_ptr_dtor(&obj_tmp);
  3741. } else if ((mptr = zend_hash_find_ptr(&ce->function_table, lc_name)) != NULL) {
  3742. reflection_method_factory(ce, mptr, NULL, return_value);
  3743. } else {
  3744. zend_throw_exception_ex(reflection_exception_ptr, 0,
  3745. "Method %s::%s() does not exist", ZSTR_VAL(ce->name), ZSTR_VAL(name));
  3746. }
  3747. zend_string_release(lc_name);
  3748. }
  3749. /* }}} */
  3750. /* {{{ _addmethod */
  3751. static bool _addmethod(zend_function *mptr, zend_class_entry *ce, HashTable *ht, zend_long filter)
  3752. {
  3753. if ((mptr->common.fn_flags & ZEND_ACC_PRIVATE) && mptr->common.scope != ce) {
  3754. return 0;
  3755. }
  3756. if (mptr->common.fn_flags & filter) {
  3757. zval method;
  3758. reflection_method_factory(ce, mptr, NULL, &method);
  3759. zend_hash_next_index_insert_new(ht, &method);
  3760. return 1;
  3761. }
  3762. return 0;
  3763. }
  3764. /* }}} */
  3765. /* {{{ Returns an array of this class' methods */
  3766. ZEND_METHOD(ReflectionClass, getMethods)
  3767. {
  3768. reflection_object *intern;
  3769. zend_class_entry *ce;
  3770. zend_function *mptr;
  3771. zend_long filter;
  3772. bool filter_is_null = 1;
  3773. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l!", &filter, &filter_is_null) == FAILURE) {
  3774. RETURN_THROWS();
  3775. }
  3776. if (filter_is_null) {
  3777. filter = ZEND_ACC_PPP_MASK | ZEND_ACC_ABSTRACT | ZEND_ACC_FINAL | ZEND_ACC_STATIC;
  3778. }
  3779. GET_REFLECTION_OBJECT_PTR(ce);
  3780. array_init(return_value);
  3781. ZEND_HASH_FOREACH_PTR(&ce->function_table, mptr) {
  3782. _addmethod(mptr, ce, Z_ARRVAL_P(return_value), filter);
  3783. } ZEND_HASH_FOREACH_END();
  3784. if (instanceof_function(ce, zend_ce_closure)) {
  3785. bool has_obj = Z_TYPE(intern->obj) != IS_UNDEF;
  3786. zval obj_tmp;
  3787. zend_object *obj;
  3788. if (!has_obj) {
  3789. object_init_ex(&obj_tmp, ce);
  3790. obj = Z_OBJ(obj_tmp);
  3791. } else {
  3792. obj = Z_OBJ(intern->obj);
  3793. }
  3794. zend_function *closure = zend_get_closure_invoke_method(obj);
  3795. if (closure) {
  3796. if (!_addmethod(closure, ce, Z_ARRVAL_P(return_value), filter)) {
  3797. _free_function(closure);
  3798. }
  3799. }
  3800. if (!has_obj) {
  3801. zval_ptr_dtor(&obj_tmp);
  3802. }
  3803. }
  3804. }
  3805. /* }}} */
  3806. /* {{{ Returns whether a property exists or not */
  3807. ZEND_METHOD(ReflectionClass, hasProperty)
  3808. {
  3809. reflection_object *intern;
  3810. zend_property_info *property_info;
  3811. zend_class_entry *ce;
  3812. zend_string *name;
  3813. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
  3814. RETURN_THROWS();
  3815. }
  3816. GET_REFLECTION_OBJECT_PTR(ce);
  3817. if ((property_info = zend_hash_find_ptr(&ce->properties_info, name)) != NULL) {
  3818. if ((property_info->flags & ZEND_ACC_PRIVATE) && property_info->ce != ce) {
  3819. RETURN_FALSE;
  3820. }
  3821. RETURN_TRUE;
  3822. } else {
  3823. if (Z_TYPE(intern->obj) != IS_UNDEF) {
  3824. if (Z_OBJ_HANDLER(intern->obj, has_property)(Z_OBJ(intern->obj), name, 2, NULL)) {
  3825. RETURN_TRUE;
  3826. }
  3827. }
  3828. RETURN_FALSE;
  3829. }
  3830. }
  3831. /* }}} */
  3832. /* {{{ Returns the class' property specified by its name */
  3833. ZEND_METHOD(ReflectionClass, getProperty)
  3834. {
  3835. reflection_object *intern;
  3836. zend_class_entry *ce, *ce2;
  3837. zend_property_info *property_info;
  3838. zend_string *name, *classname;
  3839. char *tmp, *str_name;
  3840. size_t classname_len, str_name_len;
  3841. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
  3842. RETURN_THROWS();
  3843. }
  3844. GET_REFLECTION_OBJECT_PTR(ce);
  3845. if ((property_info = zend_hash_find_ptr(&ce->properties_info, name)) != NULL) {
  3846. if (!(property_info->flags & ZEND_ACC_PRIVATE) || property_info->ce == ce) {
  3847. reflection_property_factory(ce, name, property_info, return_value);
  3848. return;
  3849. }
  3850. } else if (Z_TYPE(intern->obj) != IS_UNDEF) {
  3851. /* Check for dynamic properties */
  3852. if (zend_hash_exists(Z_OBJ_HT(intern->obj)->get_properties(Z_OBJ(intern->obj)), name)) {
  3853. reflection_property_factory(ce, name, NULL, return_value);
  3854. return;
  3855. }
  3856. }
  3857. str_name = ZSTR_VAL(name);
  3858. if ((tmp = strstr(ZSTR_VAL(name), "::")) != NULL) {
  3859. classname_len = tmp - ZSTR_VAL(name);
  3860. classname = zend_string_alloc(classname_len, 0);
  3861. zend_str_tolower_copy(ZSTR_VAL(classname), ZSTR_VAL(name), classname_len);
  3862. ZSTR_VAL(classname)[classname_len] = '\0';
  3863. str_name_len = ZSTR_LEN(name) - (classname_len + 2);
  3864. str_name = tmp + 2;
  3865. ce2 = zend_lookup_class(classname);
  3866. if (!ce2) {
  3867. if (!EG(exception)) {
  3868. zend_throw_exception_ex(reflection_exception_ptr, -1, "Class \"%s\" does not exist", ZSTR_VAL(classname));
  3869. }
  3870. zend_string_release_ex(classname, 0);
  3871. RETURN_THROWS();
  3872. }
  3873. zend_string_release_ex(classname, 0);
  3874. if (!instanceof_function(ce, ce2)) {
  3875. 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));
  3876. RETURN_THROWS();
  3877. }
  3878. ce = ce2;
  3879. property_info = zend_hash_str_find_ptr(&ce->properties_info, str_name, str_name_len);
  3880. if (property_info != NULL
  3881. && (!(property_info->flags & ZEND_ACC_PRIVATE)
  3882. || property_info->ce == ce)) {
  3883. reflection_property_factory_str(ce, str_name, str_name_len, property_info, return_value);
  3884. return;
  3885. }
  3886. }
  3887. zend_throw_exception_ex(reflection_exception_ptr, 0, "Property %s::$%s does not exist", ZSTR_VAL(ce->name), str_name);
  3888. }
  3889. /* }}} */
  3890. /* {{{ _addproperty */
  3891. static void _addproperty(zend_property_info *pptr, zend_string *key, zend_class_entry *ce, HashTable *ht, long filter)
  3892. {
  3893. if ((pptr->flags & ZEND_ACC_PRIVATE) && pptr->ce != ce) {
  3894. return;
  3895. }
  3896. if (pptr->flags & filter) {
  3897. zval property;
  3898. reflection_property_factory(ce, key, pptr, &property);
  3899. zend_hash_next_index_insert_new(ht, &property);
  3900. }
  3901. }
  3902. /* }}} */
  3903. /* {{{ _adddynproperty */
  3904. static void _adddynproperty(zval *ptr, zend_string *key, zend_class_entry *ce, zval *retval)
  3905. {
  3906. zval property;
  3907. /* under some circumstances, the properties hash table may contain numeric
  3908. * properties (e.g. when casting from array). This is a WON'T FIX bug, at
  3909. * least for the moment. Ignore these */
  3910. if (key == NULL) {
  3911. return;
  3912. }
  3913. /* Not a dynamic property */
  3914. if (Z_TYPE_P(ptr) == IS_INDIRECT) {
  3915. return;
  3916. }
  3917. reflection_property_factory(ce, key, NULL, &property);
  3918. add_next_index_zval(retval, &property);
  3919. }
  3920. /* }}} */
  3921. /* {{{ Returns an array of this class' properties */
  3922. ZEND_METHOD(ReflectionClass, getProperties)
  3923. {
  3924. reflection_object *intern;
  3925. zend_class_entry *ce;
  3926. zend_string *key;
  3927. zend_property_info *prop_info;
  3928. zend_long filter;
  3929. bool filter_is_null = 1;
  3930. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l!", &filter, &filter_is_null) == FAILURE) {
  3931. RETURN_THROWS();
  3932. }
  3933. if (filter_is_null) {
  3934. filter = ZEND_ACC_PPP_MASK | ZEND_ACC_STATIC;
  3935. }
  3936. GET_REFLECTION_OBJECT_PTR(ce);
  3937. array_init(return_value);
  3938. ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->properties_info, key, prop_info) {
  3939. _addproperty(prop_info, key, ce, Z_ARRVAL_P(return_value), filter);
  3940. } ZEND_HASH_FOREACH_END();
  3941. if (Z_TYPE(intern->obj) != IS_UNDEF && (filter & ZEND_ACC_PUBLIC) != 0) {
  3942. HashTable *properties = Z_OBJ_HT(intern->obj)->get_properties(Z_OBJ(intern->obj));
  3943. zval *prop;
  3944. ZEND_HASH_FOREACH_STR_KEY_VAL(properties, key, prop) {
  3945. _adddynproperty(prop, key, ce, return_value);
  3946. } ZEND_HASH_FOREACH_END();
  3947. }
  3948. }
  3949. /* }}} */
  3950. /* {{{ Returns whether a constant exists or not */
  3951. ZEND_METHOD(ReflectionClass, hasConstant)
  3952. {
  3953. reflection_object *intern;
  3954. zend_class_entry *ce;
  3955. zend_string *name;
  3956. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
  3957. RETURN_THROWS();
  3958. }
  3959. GET_REFLECTION_OBJECT_PTR(ce);
  3960. if (zend_hash_exists(&ce->constants_table, name)) {
  3961. RETURN_TRUE;
  3962. } else {
  3963. RETURN_FALSE;
  3964. }
  3965. }
  3966. /* }}} */
  3967. /* {{{ Returns an associative array containing this class' constants and their values */
  3968. ZEND_METHOD(ReflectionClass, getConstants)
  3969. {
  3970. reflection_object *intern;
  3971. zend_class_entry *ce;
  3972. zend_string *key;
  3973. zend_class_constant *constant;
  3974. zval val;
  3975. zend_long filter;
  3976. bool filter_is_null = 1;
  3977. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l!", &filter, &filter_is_null) == FAILURE) {
  3978. RETURN_THROWS();
  3979. }
  3980. if (filter_is_null) {
  3981. filter = ZEND_ACC_PPP_MASK;
  3982. }
  3983. GET_REFLECTION_OBJECT_PTR(ce);
  3984. array_init(return_value);
  3985. ZEND_HASH_FOREACH_STR_KEY_PTR(CE_CONSTANTS_TABLE(ce), key, constant) {
  3986. if (UNEXPECTED(zval_update_constant_ex(&constant->value, constant->ce) != SUCCESS)) {
  3987. RETURN_THROWS();
  3988. }
  3989. if (ZEND_CLASS_CONST_FLAGS(constant) & filter) {
  3990. ZVAL_COPY_OR_DUP(&val, &constant->value);
  3991. zend_hash_add_new(Z_ARRVAL_P(return_value), key, &val);
  3992. }
  3993. } ZEND_HASH_FOREACH_END();
  3994. }
  3995. /* }}} */
  3996. /* {{{ Returns an associative array containing this class' constants as ReflectionClassConstant objects */
  3997. ZEND_METHOD(ReflectionClass, getReflectionConstants)
  3998. {
  3999. reflection_object *intern;
  4000. zend_class_entry *ce;
  4001. zend_string *name;
  4002. zend_class_constant *constant;
  4003. zend_long filter;
  4004. bool filter_is_null = 1;
  4005. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l!", &filter, &filter_is_null) == FAILURE) {
  4006. RETURN_THROWS();
  4007. }
  4008. if (filter_is_null) {
  4009. filter = ZEND_ACC_PPP_MASK;
  4010. }
  4011. GET_REFLECTION_OBJECT_PTR(ce);
  4012. array_init(return_value);
  4013. ZEND_HASH_FOREACH_STR_KEY_PTR(CE_CONSTANTS_TABLE(ce), name, constant) {
  4014. if (ZEND_CLASS_CONST_FLAGS(constant) & filter) {
  4015. zval class_const;
  4016. reflection_class_constant_factory(name, constant, &class_const);
  4017. zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &class_const);
  4018. }
  4019. } ZEND_HASH_FOREACH_END();
  4020. }
  4021. /* }}} */
  4022. /* {{{ Returns the class' constant specified by its name */
  4023. ZEND_METHOD(ReflectionClass, getConstant)
  4024. {
  4025. reflection_object *intern;
  4026. zend_class_entry *ce;
  4027. HashTable *constants_table;
  4028. zend_class_constant *c;
  4029. zend_string *name;
  4030. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
  4031. RETURN_THROWS();
  4032. }
  4033. GET_REFLECTION_OBJECT_PTR(ce);
  4034. constants_table = CE_CONSTANTS_TABLE(ce);
  4035. ZEND_HASH_FOREACH_PTR(constants_table, c) {
  4036. if (UNEXPECTED(zval_update_constant_ex(&c->value, c->ce) != SUCCESS)) {
  4037. RETURN_THROWS();
  4038. }
  4039. } ZEND_HASH_FOREACH_END();
  4040. if ((c = zend_hash_find_ptr(constants_table, name)) == NULL) {
  4041. RETURN_FALSE;
  4042. }
  4043. ZVAL_COPY_OR_DUP(return_value, &c->value);
  4044. }
  4045. /* }}} */
  4046. /* {{{ Returns the class' constant as ReflectionClassConstant objects */
  4047. ZEND_METHOD(ReflectionClass, getReflectionConstant)
  4048. {
  4049. reflection_object *intern;
  4050. zend_class_entry *ce;
  4051. zend_class_constant *constant;
  4052. zend_string *name;
  4053. GET_REFLECTION_OBJECT_PTR(ce);
  4054. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
  4055. RETURN_THROWS();
  4056. }
  4057. if ((constant = zend_hash_find_ptr(CE_CONSTANTS_TABLE(ce), name)) == NULL) {
  4058. RETURN_FALSE;
  4059. }
  4060. reflection_class_constant_factory(name, constant, return_value);
  4061. }
  4062. /* }}} */
  4063. /* {{{ _class_check_flag */
  4064. static void _class_check_flag(INTERNAL_FUNCTION_PARAMETERS, int mask)
  4065. {
  4066. reflection_object *intern;
  4067. zend_class_entry *ce;
  4068. if (zend_parse_parameters_none() == FAILURE) {
  4069. RETURN_THROWS();
  4070. }
  4071. GET_REFLECTION_OBJECT_PTR(ce);
  4072. RETVAL_BOOL(ce->ce_flags & mask);
  4073. }
  4074. /* }}} */
  4075. /* {{{ Returns whether this class is instantiable */
  4076. ZEND_METHOD(ReflectionClass, isInstantiable)
  4077. {
  4078. reflection_object *intern;
  4079. zend_class_entry *ce;
  4080. if (zend_parse_parameters_none() == FAILURE) {
  4081. RETURN_THROWS();
  4082. }
  4083. GET_REFLECTION_OBJECT_PTR(ce);
  4084. if (ce->ce_flags & (ZEND_ACC_INTERFACE | ZEND_ACC_TRAIT | ZEND_ACC_EXPLICIT_ABSTRACT_CLASS | ZEND_ACC_IMPLICIT_ABSTRACT_CLASS | ZEND_ACC_ENUM)) {
  4085. RETURN_FALSE;
  4086. }
  4087. /* Basically, the class is instantiable. Though, if there is a constructor
  4088. * and it is not publicly accessible, it isn't! */
  4089. if (!ce->constructor) {
  4090. RETURN_TRUE;
  4091. }
  4092. RETURN_BOOL(ce->constructor->common.fn_flags & ZEND_ACC_PUBLIC);
  4093. }
  4094. /* }}} */
  4095. /* {{{ Returns whether this class is cloneable */
  4096. ZEND_METHOD(ReflectionClass, isCloneable)
  4097. {
  4098. reflection_object *intern;
  4099. zend_class_entry *ce;
  4100. zval obj;
  4101. if (zend_parse_parameters_none() == FAILURE) {
  4102. RETURN_THROWS();
  4103. }
  4104. GET_REFLECTION_OBJECT_PTR(ce);
  4105. if (ce->ce_flags & (ZEND_ACC_INTERFACE | ZEND_ACC_TRAIT | ZEND_ACC_EXPLICIT_ABSTRACT_CLASS | ZEND_ACC_IMPLICIT_ABSTRACT_CLASS | ZEND_ACC_ENUM)) {
  4106. RETURN_FALSE;
  4107. }
  4108. if (!Z_ISUNDEF(intern->obj)) {
  4109. if (ce->clone) {
  4110. RETURN_BOOL(ce->clone->common.fn_flags & ZEND_ACC_PUBLIC);
  4111. } else {
  4112. RETURN_BOOL(Z_OBJ_HANDLER(intern->obj, clone_obj) != NULL);
  4113. }
  4114. } else {
  4115. if (ce->clone) {
  4116. RETURN_BOOL(ce->clone->common.fn_flags & ZEND_ACC_PUBLIC);
  4117. } else {
  4118. if (UNEXPECTED(object_init_ex(&obj, ce) != SUCCESS)) {
  4119. return;
  4120. }
  4121. /* We're not calling the constructor, so don't call the destructor either. */
  4122. zend_object_store_ctor_failed(Z_OBJ(obj));
  4123. RETVAL_BOOL(Z_OBJ_HANDLER(obj, clone_obj) != NULL);
  4124. zval_ptr_dtor(&obj);
  4125. }
  4126. }
  4127. }
  4128. /* }}} */
  4129. /* {{{ Returns whether this is an interface or a class */
  4130. ZEND_METHOD(ReflectionClass, isInterface)
  4131. {
  4132. _class_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_INTERFACE);
  4133. }
  4134. /* }}} */
  4135. /* {{{ Returns whether this is a trait */
  4136. ZEND_METHOD(ReflectionClass, isTrait)
  4137. {
  4138. _class_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_TRAIT);
  4139. }
  4140. /* }}} */
  4141. ZEND_METHOD(ReflectionClass, isEnum)
  4142. {
  4143. _class_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_ENUM);
  4144. }
  4145. /* {{{ Returns whether this class is final */
  4146. ZEND_METHOD(ReflectionClass, isFinal)
  4147. {
  4148. _class_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_FINAL);
  4149. }
  4150. /* }}} */
  4151. /* {{{ Returns whether this class is abstract */
  4152. ZEND_METHOD(ReflectionClass, isAbstract)
  4153. {
  4154. _class_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS);
  4155. }
  4156. /* }}} */
  4157. /* {{{ Returns a bitfield of the access modifiers for this class */
  4158. ZEND_METHOD(ReflectionClass, getModifiers)
  4159. {
  4160. reflection_object *intern;
  4161. zend_class_entry *ce;
  4162. uint32_t keep_flags = ZEND_ACC_FINAL
  4163. | ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
  4164. if (zend_parse_parameters_none() == FAILURE) {
  4165. RETURN_THROWS();
  4166. }
  4167. GET_REFLECTION_OBJECT_PTR(ce);
  4168. RETURN_LONG((ce->ce_flags & keep_flags));
  4169. }
  4170. /* }}} */
  4171. /* {{{ Returns whether the given object is an instance of this class */
  4172. ZEND_METHOD(ReflectionClass, isInstance)
  4173. {
  4174. reflection_object *intern;
  4175. zend_class_entry *ce;
  4176. zval *object;
  4177. if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &object) == FAILURE) {
  4178. RETURN_THROWS();
  4179. }
  4180. GET_REFLECTION_OBJECT_PTR(ce);
  4181. RETURN_BOOL(instanceof_function(Z_OBJCE_P(object), ce));
  4182. }
  4183. /* }}} */
  4184. /* {{{ Returns an instance of this class */
  4185. ZEND_METHOD(ReflectionClass, newInstance)
  4186. {
  4187. reflection_object *intern;
  4188. zend_class_entry *ce, *old_scope;
  4189. zend_function *constructor;
  4190. GET_REFLECTION_OBJECT_PTR(ce);
  4191. if (UNEXPECTED(object_init_ex(return_value, ce) != SUCCESS)) {
  4192. return;
  4193. }
  4194. old_scope = EG(fake_scope);
  4195. EG(fake_scope) = ce;
  4196. constructor = Z_OBJ_HT_P(return_value)->get_constructor(Z_OBJ_P(return_value));
  4197. EG(fake_scope) = old_scope;
  4198. /* Run the constructor if there is one */
  4199. if (constructor) {
  4200. zval *params;
  4201. int num_args;
  4202. HashTable *named_params;
  4203. if (!(constructor->common.fn_flags & ZEND_ACC_PUBLIC)) {
  4204. zend_throw_exception_ex(reflection_exception_ptr, 0, "Access to non-public constructor of class %s", ZSTR_VAL(ce->name));
  4205. zval_ptr_dtor(return_value);
  4206. RETURN_NULL();
  4207. }
  4208. ZEND_PARSE_PARAMETERS_START(0, -1)
  4209. Z_PARAM_VARIADIC_WITH_NAMED(params, num_args, named_params)
  4210. ZEND_PARSE_PARAMETERS_END();
  4211. zend_call_known_function(
  4212. constructor, Z_OBJ_P(return_value), Z_OBJCE_P(return_value), NULL,
  4213. num_args, params, named_params);
  4214. if (EG(exception)) {
  4215. zend_object_store_ctor_failed(Z_OBJ_P(return_value));
  4216. }
  4217. } else if (ZEND_NUM_ARGS()) {
  4218. 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));
  4219. }
  4220. }
  4221. /* }}} */
  4222. /* {{{ Returns an instance of this class without invoking its constructor */
  4223. ZEND_METHOD(ReflectionClass, newInstanceWithoutConstructor)
  4224. {
  4225. reflection_object *intern;
  4226. zend_class_entry *ce;
  4227. GET_REFLECTION_OBJECT_PTR(ce);
  4228. if (zend_parse_parameters_none() == FAILURE) {
  4229. RETURN_THROWS();
  4230. }
  4231. if (ce->type == ZEND_INTERNAL_CLASS
  4232. && ce->create_object != NULL && (ce->ce_flags & ZEND_ACC_FINAL)) {
  4233. 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));
  4234. RETURN_THROWS();
  4235. }
  4236. object_init_ex(return_value, ce);
  4237. }
  4238. /* }}} */
  4239. /* {{{ Returns an instance of this class */
  4240. ZEND_METHOD(ReflectionClass, newInstanceArgs)
  4241. {
  4242. reflection_object *intern;
  4243. zend_class_entry *ce, *old_scope;
  4244. int argc = 0;
  4245. HashTable *args = NULL;
  4246. zend_function *constructor;
  4247. GET_REFLECTION_OBJECT_PTR(ce);
  4248. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|h", &args) == FAILURE) {
  4249. RETURN_THROWS();
  4250. }
  4251. if (args) {
  4252. argc = zend_hash_num_elements(args);
  4253. }
  4254. if (UNEXPECTED(object_init_ex(return_value, ce) != SUCCESS)) {
  4255. return;
  4256. }
  4257. old_scope = EG(fake_scope);
  4258. EG(fake_scope) = ce;
  4259. constructor = Z_OBJ_HT_P(return_value)->get_constructor(Z_OBJ_P(return_value));
  4260. EG(fake_scope) = old_scope;
  4261. /* Run the constructor if there is one */
  4262. if (constructor) {
  4263. if (!(constructor->common.fn_flags & ZEND_ACC_PUBLIC)) {
  4264. zend_throw_exception_ex(reflection_exception_ptr, 0, "Access to non-public constructor of class %s", ZSTR_VAL(ce->name));
  4265. zval_ptr_dtor(return_value);
  4266. RETURN_NULL();
  4267. }
  4268. zend_call_known_function(
  4269. constructor, Z_OBJ_P(return_value), Z_OBJCE_P(return_value), NULL, 0, NULL, args);
  4270. if (EG(exception)) {
  4271. zend_object_store_ctor_failed(Z_OBJ_P(return_value));
  4272. }
  4273. } else if (argc) {
  4274. 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));
  4275. }
  4276. }
  4277. /* }}} */
  4278. /* {{{ Returns an array of interfaces this class implements */
  4279. ZEND_METHOD(ReflectionClass, getInterfaces)
  4280. {
  4281. reflection_object *intern;
  4282. zend_class_entry *ce;
  4283. if (zend_parse_parameters_none() == FAILURE) {
  4284. RETURN_THROWS();
  4285. }
  4286. GET_REFLECTION_OBJECT_PTR(ce);
  4287. if (ce->num_interfaces) {
  4288. uint32_t i;
  4289. ZEND_ASSERT(ce->ce_flags & ZEND_ACC_LINKED);
  4290. array_init(return_value);
  4291. for (i=0; i < ce->num_interfaces; i++) {
  4292. zval interface;
  4293. zend_reflection_class_factory(ce->interfaces[i], &interface);
  4294. zend_hash_update(Z_ARRVAL_P(return_value), ce->interfaces[i]->name, &interface);
  4295. }
  4296. } else {
  4297. RETURN_EMPTY_ARRAY();
  4298. }
  4299. }
  4300. /* }}} */
  4301. /* {{{ Returns an array of names of interfaces this class implements */
  4302. ZEND_METHOD(ReflectionClass, getInterfaceNames)
  4303. {
  4304. reflection_object *intern;
  4305. zend_class_entry *ce;
  4306. uint32_t i;
  4307. if (zend_parse_parameters_none() == FAILURE) {
  4308. RETURN_THROWS();
  4309. }
  4310. GET_REFLECTION_OBJECT_PTR(ce);
  4311. if (!ce->num_interfaces) {
  4312. /* Return an empty array if this class implements no interfaces */
  4313. RETURN_EMPTY_ARRAY();
  4314. }
  4315. ZEND_ASSERT(ce->ce_flags & ZEND_ACC_LINKED);
  4316. array_init(return_value);
  4317. for (i=0; i < ce->num_interfaces; i++) {
  4318. add_next_index_str(return_value, zend_string_copy(ce->interfaces[i]->name));
  4319. }
  4320. }
  4321. /* }}} */
  4322. /* {{{ Returns an array of traits used by this class */
  4323. ZEND_METHOD(ReflectionClass, getTraits)
  4324. {
  4325. reflection_object *intern;
  4326. zend_class_entry *ce;
  4327. uint32_t i;
  4328. if (zend_parse_parameters_none() == FAILURE) {
  4329. RETURN_THROWS();
  4330. }
  4331. GET_REFLECTION_OBJECT_PTR(ce);
  4332. if (!ce->num_traits) {
  4333. RETURN_EMPTY_ARRAY();
  4334. }
  4335. array_init(return_value);
  4336. for (i=0; i < ce->num_traits; i++) {
  4337. zval trait;
  4338. zend_class_entry *trait_ce;
  4339. trait_ce = zend_fetch_class_by_name(ce->trait_names[i].name,
  4340. ce->trait_names[i].lc_name, ZEND_FETCH_CLASS_TRAIT);
  4341. ZEND_ASSERT(trait_ce);
  4342. zend_reflection_class_factory(trait_ce, &trait);
  4343. zend_hash_update(Z_ARRVAL_P(return_value), ce->trait_names[i].name, &trait);
  4344. }
  4345. }
  4346. /* }}} */
  4347. /* {{{ Returns an array of names of traits used by this class */
  4348. ZEND_METHOD(ReflectionClass, getTraitNames)
  4349. {
  4350. reflection_object *intern;
  4351. zend_class_entry *ce;
  4352. uint32_t i;
  4353. if (zend_parse_parameters_none() == FAILURE) {
  4354. RETURN_THROWS();
  4355. }
  4356. GET_REFLECTION_OBJECT_PTR(ce);
  4357. if (!ce->num_traits) {
  4358. RETURN_EMPTY_ARRAY();
  4359. }
  4360. array_init(return_value);
  4361. for (i=0; i < ce->num_traits; i++) {
  4362. add_next_index_str(return_value, zend_string_copy(ce->trait_names[i].name));
  4363. }
  4364. }
  4365. /* }}} */
  4366. /* {{{ Returns an array of trait aliases */
  4367. ZEND_METHOD(ReflectionClass, getTraitAliases)
  4368. {
  4369. reflection_object *intern;
  4370. zend_class_entry *ce;
  4371. if (zend_parse_parameters_none() == FAILURE) {
  4372. RETURN_THROWS();
  4373. }
  4374. GET_REFLECTION_OBJECT_PTR(ce);
  4375. if (ce->trait_aliases) {
  4376. uint32_t i = 0;
  4377. array_init(return_value);
  4378. while (ce->trait_aliases[i]) {
  4379. zend_string *mname;
  4380. zend_trait_method_reference *cur_ref = &ce->trait_aliases[i]->trait_method;
  4381. if (ce->trait_aliases[i]->alias) {
  4382. zend_string *class_name = cur_ref->class_name;
  4383. if (!class_name) {
  4384. uint32_t j = 0;
  4385. zend_string *lcname = zend_string_tolower(cur_ref->method_name);
  4386. for (j = 0; j < ce->num_traits; j++) {
  4387. zend_class_entry *trait =
  4388. zend_hash_find_ptr(CG(class_table), ce->trait_names[j].lc_name);
  4389. ZEND_ASSERT(trait && "Trait must exist");
  4390. if (zend_hash_exists(&trait->function_table, lcname)) {
  4391. class_name = trait->name;
  4392. break;
  4393. }
  4394. }
  4395. zend_string_release_ex(lcname, 0);
  4396. ZEND_ASSERT(class_name != NULL);
  4397. }
  4398. mname = zend_string_alloc(ZSTR_LEN(class_name) + ZSTR_LEN(cur_ref->method_name) + 2, 0);
  4399. snprintf(ZSTR_VAL(mname), ZSTR_LEN(mname) + 1, "%s::%s", ZSTR_VAL(class_name), ZSTR_VAL(cur_ref->method_name));
  4400. add_assoc_str_ex(return_value, ZSTR_VAL(ce->trait_aliases[i]->alias), ZSTR_LEN(ce->trait_aliases[i]->alias), mname);
  4401. }
  4402. i++;
  4403. }
  4404. } else {
  4405. RETURN_EMPTY_ARRAY();
  4406. }
  4407. }
  4408. /* }}} */
  4409. /* {{{ Returns the class' parent class, or, if none exists, FALSE */
  4410. ZEND_METHOD(ReflectionClass, getParentClass)
  4411. {
  4412. reflection_object *intern;
  4413. zend_class_entry *ce;
  4414. if (zend_parse_parameters_none() == FAILURE) {
  4415. RETURN_THROWS();
  4416. }
  4417. GET_REFLECTION_OBJECT_PTR(ce);
  4418. if (ce->parent) {
  4419. zend_reflection_class_factory(ce->parent, return_value);
  4420. } else {
  4421. RETURN_FALSE;
  4422. }
  4423. }
  4424. /* }}} */
  4425. /* {{{ Returns whether this class is a subclass of another class */
  4426. ZEND_METHOD(ReflectionClass, isSubclassOf)
  4427. {
  4428. reflection_object *intern, *argument;
  4429. zend_class_entry *ce, *class_ce;
  4430. zend_string *class_str;
  4431. zend_object *class_obj;
  4432. ZEND_PARSE_PARAMETERS_START(1, 1)
  4433. Z_PARAM_OBJ_OF_CLASS_OR_STR(class_obj, reflection_class_ptr, class_str)
  4434. ZEND_PARSE_PARAMETERS_END();
  4435. if (class_obj) {
  4436. argument = reflection_object_from_obj(class_obj);
  4437. if (argument->ptr == NULL) {
  4438. zend_throw_error(NULL, "Internal error: Failed to retrieve the argument's reflection object");
  4439. RETURN_THROWS();
  4440. }
  4441. class_ce = argument->ptr;
  4442. } else {
  4443. if ((class_ce = zend_lookup_class(class_str)) == NULL) {
  4444. zend_throw_exception_ex(reflection_exception_ptr, 0, "Class \"%s\" does not exist", ZSTR_VAL(class_str));
  4445. RETURN_THROWS();
  4446. }
  4447. }
  4448. GET_REFLECTION_OBJECT_PTR(ce);
  4449. RETURN_BOOL((ce != class_ce && instanceof_function(ce, class_ce)));
  4450. }
  4451. /* }}} */
  4452. /* {{{ Returns whether this class is a subclass of another class */
  4453. ZEND_METHOD(ReflectionClass, implementsInterface)
  4454. {
  4455. reflection_object *intern, *argument;
  4456. zend_string *interface_str;
  4457. zend_class_entry *ce, *interface_ce;
  4458. zend_object *interface_obj;
  4459. ZEND_PARSE_PARAMETERS_START(1, 1)
  4460. Z_PARAM_OBJ_OF_CLASS_OR_STR(interface_obj, reflection_class_ptr, interface_str)
  4461. ZEND_PARSE_PARAMETERS_END();
  4462. if (interface_obj) {
  4463. argument = reflection_object_from_obj(interface_obj);
  4464. if (argument->ptr == NULL) {
  4465. zend_throw_error(NULL, "Internal error: Failed to retrieve the argument's reflection object");
  4466. RETURN_THROWS();
  4467. }
  4468. interface_ce = argument->ptr;
  4469. } else {
  4470. if ((interface_ce = zend_lookup_class(interface_str)) == NULL) {
  4471. zend_throw_exception_ex(reflection_exception_ptr, 0, "Interface \"%s\" does not exist", ZSTR_VAL(interface_str));
  4472. RETURN_THROWS();
  4473. }
  4474. }
  4475. if (!(interface_ce->ce_flags & ZEND_ACC_INTERFACE)) {
  4476. zend_throw_exception_ex(reflection_exception_ptr, 0, "%s is not an interface", ZSTR_VAL(interface_ce->name));
  4477. RETURN_THROWS();
  4478. }
  4479. GET_REFLECTION_OBJECT_PTR(ce);
  4480. RETURN_BOOL(instanceof_function(ce, interface_ce));
  4481. }
  4482. /* }}} */
  4483. /* {{{ Returns whether this class is iterable (can be used inside foreach) */
  4484. ZEND_METHOD(ReflectionClass, isIterable)
  4485. {
  4486. reflection_object *intern;
  4487. zend_class_entry *ce;
  4488. if (zend_parse_parameters_none() == FAILURE) {
  4489. RETURN_THROWS();
  4490. }
  4491. GET_REFLECTION_OBJECT_PTR(ce);
  4492. if (ce->ce_flags & (ZEND_ACC_INTERFACE | ZEND_ACC_IMPLICIT_ABSTRACT_CLASS |
  4493. ZEND_ACC_TRAIT | ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
  4494. RETURN_FALSE;
  4495. }
  4496. RETURN_BOOL(ce->get_iterator || instanceof_function(ce, zend_ce_traversable));
  4497. }
  4498. /* }}} */
  4499. /* {{{ Returns NULL or the extension the class belongs to */
  4500. ZEND_METHOD(ReflectionClass, getExtension)
  4501. {
  4502. reflection_object *intern;
  4503. zend_class_entry *ce;
  4504. if (zend_parse_parameters_none() == FAILURE) {
  4505. RETURN_THROWS();
  4506. }
  4507. GET_REFLECTION_OBJECT_PTR(ce);
  4508. if ((ce->type == ZEND_INTERNAL_CLASS) && ce->info.internal.module) {
  4509. reflection_extension_factory(return_value, ce->info.internal.module->name);
  4510. }
  4511. }
  4512. /* }}} */
  4513. /* {{{ Returns false or the name of the extension the class belongs to */
  4514. ZEND_METHOD(ReflectionClass, getExtensionName)
  4515. {
  4516. reflection_object *intern;
  4517. zend_class_entry *ce;
  4518. if (zend_parse_parameters_none() == FAILURE) {
  4519. RETURN_THROWS();
  4520. }
  4521. GET_REFLECTION_OBJECT_PTR(ce);
  4522. if ((ce->type == ZEND_INTERNAL_CLASS) && ce->info.internal.module) {
  4523. RETURN_STRING(ce->info.internal.module->name);
  4524. } else {
  4525. RETURN_FALSE;
  4526. }
  4527. }
  4528. /* }}} */
  4529. /* {{{ Returns whether this class is defined in namespace */
  4530. ZEND_METHOD(ReflectionClass, inNamespace)
  4531. {
  4532. reflection_object *intern;
  4533. zend_class_entry *ce;
  4534. if (zend_parse_parameters_none() == FAILURE) {
  4535. RETURN_THROWS();
  4536. }
  4537. GET_REFLECTION_OBJECT_PTR(ce);
  4538. zend_string *name = ce->name;
  4539. const char *backslash = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
  4540. RETURN_BOOL(backslash && backslash > ZSTR_VAL(name));
  4541. }
  4542. /* }}} */
  4543. /* {{{ Returns the name of namespace where this class is defined */
  4544. ZEND_METHOD(ReflectionClass, getNamespaceName)
  4545. {
  4546. reflection_object *intern;
  4547. zend_class_entry *ce;
  4548. if (zend_parse_parameters_none() == FAILURE) {
  4549. RETURN_THROWS();
  4550. }
  4551. GET_REFLECTION_OBJECT_PTR(ce);
  4552. zend_string *name = ce->name;
  4553. const char *backslash = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
  4554. if (backslash && backslash > ZSTR_VAL(name)) {
  4555. RETURN_STRINGL(ZSTR_VAL(name), backslash - ZSTR_VAL(name));
  4556. }
  4557. RETURN_EMPTY_STRING();
  4558. }
  4559. /* }}} */
  4560. /* {{{ Returns the short name of the class (without namespace part) */
  4561. ZEND_METHOD(ReflectionClass, getShortName)
  4562. {
  4563. reflection_object *intern;
  4564. zend_class_entry *ce;
  4565. if (zend_parse_parameters_none() == FAILURE) {
  4566. RETURN_THROWS();
  4567. }
  4568. GET_REFLECTION_OBJECT_PTR(ce);
  4569. zend_string *name = ce->name;
  4570. const char *backslash = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
  4571. if (backslash && backslash > ZSTR_VAL(name)) {
  4572. RETURN_STRINGL(backslash + 1, ZSTR_LEN(name) - (backslash - ZSTR_VAL(name) + 1));
  4573. }
  4574. RETURN_STR_COPY(name);
  4575. }
  4576. /* }}} */
  4577. /* {{{ Constructor. Takes an instance as an argument */
  4578. ZEND_METHOD(ReflectionObject, __construct)
  4579. {
  4580. reflection_class_object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  4581. }
  4582. /* }}} */
  4583. /* {{{ Constructor. Throws an Exception in case the given property does not exist */
  4584. ZEND_METHOD(ReflectionProperty, __construct)
  4585. {
  4586. zend_string *classname_str;
  4587. zend_object *classname_obj;
  4588. zend_string *name;
  4589. int dynam_prop = 0;
  4590. zval *object;
  4591. reflection_object *intern;
  4592. zend_class_entry *ce;
  4593. zend_property_info *property_info = NULL;
  4594. property_reference *reference;
  4595. ZEND_PARSE_PARAMETERS_START(2, 2)
  4596. Z_PARAM_OBJ_OR_STR(classname_obj, classname_str)
  4597. Z_PARAM_STR(name)
  4598. ZEND_PARSE_PARAMETERS_END();
  4599. object = ZEND_THIS;
  4600. intern = Z_REFLECTION_P(object);
  4601. if (classname_obj) {
  4602. ce = classname_obj->ce;
  4603. } else {
  4604. if ((ce = zend_lookup_class(classname_str)) == NULL) {
  4605. zend_throw_exception_ex(reflection_exception_ptr, 0, "Class \"%s\" does not exist", ZSTR_VAL(classname_str));
  4606. RETURN_THROWS();
  4607. }
  4608. }
  4609. property_info = zend_hash_find_ptr(&ce->properties_info, name);
  4610. if (property_info == NULL
  4611. || ((property_info->flags & ZEND_ACC_PRIVATE)
  4612. && property_info->ce != ce)) {
  4613. /* Check for dynamic properties */
  4614. if (property_info == NULL && classname_obj) {
  4615. if (zend_hash_exists(classname_obj->handlers->get_properties(classname_obj), name)) {
  4616. dynam_prop = 1;
  4617. }
  4618. }
  4619. if (dynam_prop == 0) {
  4620. zend_throw_exception_ex(reflection_exception_ptr, 0, "Property %s::$%s does not exist", ZSTR_VAL(ce->name), ZSTR_VAL(name));
  4621. RETURN_THROWS();
  4622. }
  4623. }
  4624. ZVAL_STR_COPY(reflection_prop_name(object), name);
  4625. if (dynam_prop == 0) {
  4626. ZVAL_STR_COPY(reflection_prop_class(object), property_info->ce->name);
  4627. } else {
  4628. ZVAL_STR_COPY(reflection_prop_class(object), ce->name);
  4629. }
  4630. reference = (property_reference*) emalloc(sizeof(property_reference));
  4631. reference->prop = dynam_prop ? NULL : property_info;
  4632. reference->unmangled_name = zend_string_copy(name);
  4633. intern->ptr = reference;
  4634. intern->ref_type = REF_TYPE_PROPERTY;
  4635. intern->ce = ce;
  4636. }
  4637. /* }}} */
  4638. /* {{{ Returns a string representation */
  4639. ZEND_METHOD(ReflectionProperty, __toString)
  4640. {
  4641. reflection_object *intern;
  4642. property_reference *ref;
  4643. smart_str str = {0};
  4644. if (zend_parse_parameters_none() == FAILURE) {
  4645. RETURN_THROWS();
  4646. }
  4647. GET_REFLECTION_OBJECT_PTR(ref);
  4648. _property_string(&str, ref->prop, ZSTR_VAL(ref->unmangled_name), "");
  4649. RETURN_STR(smart_str_extract(&str));
  4650. }
  4651. /* }}} */
  4652. /* {{{ Returns the class' name */
  4653. ZEND_METHOD(ReflectionProperty, getName)
  4654. {
  4655. reflection_object *intern;
  4656. property_reference *ref;
  4657. if (zend_parse_parameters_none() == FAILURE) {
  4658. RETURN_THROWS();
  4659. }
  4660. GET_REFLECTION_OBJECT_PTR(ref);
  4661. RETURN_STR_COPY(ref->unmangled_name);
  4662. }
  4663. /* }}} */
  4664. static void _property_check_flag(INTERNAL_FUNCTION_PARAMETERS, int mask) /* {{{ */
  4665. {
  4666. reflection_object *intern;
  4667. property_reference *ref;
  4668. if (zend_parse_parameters_none() == FAILURE) {
  4669. RETURN_THROWS();
  4670. }
  4671. GET_REFLECTION_OBJECT_PTR(ref);
  4672. RETURN_BOOL(prop_get_flags(ref) & mask);
  4673. }
  4674. /* }}} */
  4675. /* {{{ Returns whether this property is public */
  4676. ZEND_METHOD(ReflectionProperty, isPublic)
  4677. {
  4678. _property_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PUBLIC);
  4679. }
  4680. /* }}} */
  4681. /* {{{ Returns whether this property is private */
  4682. ZEND_METHOD(ReflectionProperty, isPrivate)
  4683. {
  4684. _property_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PRIVATE);
  4685. }
  4686. /* }}} */
  4687. /* {{{ Returns whether this property is protected */
  4688. ZEND_METHOD(ReflectionProperty, isProtected)
  4689. {
  4690. _property_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PROTECTED);
  4691. }
  4692. /* }}} */
  4693. /* {{{ Returns whether this property is static */
  4694. ZEND_METHOD(ReflectionProperty, isStatic)
  4695. {
  4696. _property_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_STATIC);
  4697. }
  4698. /* }}} */
  4699. ZEND_METHOD(ReflectionProperty, isReadOnly)
  4700. {
  4701. _property_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_READONLY);
  4702. }
  4703. /* {{{ Returns whether this property is default (declared at compilation time). */
  4704. ZEND_METHOD(ReflectionProperty, isDefault)
  4705. {
  4706. reflection_object *intern;
  4707. property_reference *ref;
  4708. if (zend_parse_parameters_none() == FAILURE) {
  4709. RETURN_THROWS();
  4710. }
  4711. GET_REFLECTION_OBJECT_PTR(ref);
  4712. RETURN_BOOL(ref->prop != NULL);
  4713. }
  4714. /* }}} */
  4715. /* {{{ Returns whether this property has been promoted from a constructor */
  4716. ZEND_METHOD(ReflectionProperty, isPromoted)
  4717. {
  4718. _property_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PROMOTED);
  4719. }
  4720. /* }}} */
  4721. /* {{{ Returns a bitfield of the access modifiers for this property */
  4722. ZEND_METHOD(ReflectionProperty, getModifiers)
  4723. {
  4724. reflection_object *intern;
  4725. property_reference *ref;
  4726. uint32_t keep_flags = ZEND_ACC_PPP_MASK | ZEND_ACC_STATIC | ZEND_ACC_READONLY;
  4727. if (zend_parse_parameters_none() == FAILURE) {
  4728. RETURN_THROWS();
  4729. }
  4730. GET_REFLECTION_OBJECT_PTR(ref);
  4731. RETURN_LONG(prop_get_flags(ref) & keep_flags);
  4732. }
  4733. /* }}} */
  4734. /* {{{ Returns this property's value */
  4735. ZEND_METHOD(ReflectionProperty, getValue)
  4736. {
  4737. reflection_object *intern;
  4738. property_reference *ref;
  4739. zval *object = NULL;
  4740. zval *member_p = NULL;
  4741. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|o!", &object) == FAILURE) {
  4742. RETURN_THROWS();
  4743. }
  4744. GET_REFLECTION_OBJECT_PTR(ref);
  4745. if (prop_get_flags(ref) & ZEND_ACC_STATIC) {
  4746. member_p = zend_read_static_property_ex(intern->ce, ref->unmangled_name, 0);
  4747. if (member_p) {
  4748. RETURN_COPY_DEREF(member_p);
  4749. }
  4750. } else {
  4751. zval rv;
  4752. if (!object) {
  4753. zend_argument_type_error(1, "must be provided for instance properties");
  4754. RETURN_THROWS();
  4755. }
  4756. /* TODO: Should this always use intern->ce? */
  4757. if (!instanceof_function(Z_OBJCE_P(object), ref->prop ? ref->prop->ce : intern->ce)) {
  4758. _DO_THROW("Given object is not an instance of the class this property was declared in");
  4759. RETURN_THROWS();
  4760. }
  4761. member_p = zend_read_property_ex(intern->ce, Z_OBJ_P(object), ref->unmangled_name, 0, &rv);
  4762. if (member_p != &rv) {
  4763. RETURN_COPY_DEREF(member_p);
  4764. } else {
  4765. if (Z_ISREF_P(member_p)) {
  4766. zend_unwrap_reference(member_p);
  4767. }
  4768. RETURN_COPY_VALUE(member_p);
  4769. }
  4770. }
  4771. }
  4772. /* }}} */
  4773. /* {{{ Sets this property's value */
  4774. ZEND_METHOD(ReflectionProperty, setValue)
  4775. {
  4776. reflection_object *intern;
  4777. property_reference *ref;
  4778. zval *object;
  4779. zval *value;
  4780. zval *tmp;
  4781. GET_REFLECTION_OBJECT_PTR(ref);
  4782. if (prop_get_flags(ref) & ZEND_ACC_STATIC) {
  4783. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "z", &value) == FAILURE) {
  4784. if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &tmp, &value) == FAILURE) {
  4785. RETURN_THROWS();
  4786. }
  4787. }
  4788. zend_update_static_property_ex(intern->ce, ref->unmangled_name, value);
  4789. } else {
  4790. if (zend_parse_parameters(ZEND_NUM_ARGS(), "oz", &object, &value) == FAILURE) {
  4791. RETURN_THROWS();
  4792. }
  4793. zend_update_property_ex(intern->ce, Z_OBJ_P(object), ref->unmangled_name, value);
  4794. }
  4795. }
  4796. /* }}} */
  4797. /* {{{ Returns this property's value */
  4798. ZEND_METHOD(ReflectionProperty, isInitialized)
  4799. {
  4800. reflection_object *intern;
  4801. property_reference *ref;
  4802. zval *object = NULL;
  4803. zval *member_p = NULL;
  4804. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|o!", &object) == FAILURE) {
  4805. RETURN_THROWS();
  4806. }
  4807. GET_REFLECTION_OBJECT_PTR(ref);
  4808. if (prop_get_flags(ref) & ZEND_ACC_STATIC) {
  4809. member_p = zend_read_static_property_ex(intern->ce, ref->unmangled_name, 1);
  4810. if (member_p) {
  4811. RETURN_BOOL(!Z_ISUNDEF_P(member_p));
  4812. }
  4813. RETURN_FALSE;
  4814. } else {
  4815. zend_class_entry *old_scope;
  4816. int retval;
  4817. if (!object) {
  4818. zend_argument_type_error(1, "must be provided for instance properties");
  4819. RETURN_THROWS();
  4820. }
  4821. /* TODO: Should this always use intern->ce? */
  4822. if (!instanceof_function(Z_OBJCE_P(object), ref->prop ? ref->prop->ce : intern->ce)) {
  4823. _DO_THROW("Given object is not an instance of the class this property was declared in");
  4824. RETURN_THROWS();
  4825. }
  4826. old_scope = EG(fake_scope);
  4827. EG(fake_scope) = intern->ce;
  4828. retval = Z_OBJ_HT_P(object)->has_property(Z_OBJ_P(object), ref->unmangled_name, ZEND_PROPERTY_EXISTS, NULL);
  4829. EG(fake_scope) = old_scope;
  4830. RETVAL_BOOL(retval);
  4831. }
  4832. }
  4833. /* }}} */
  4834. /* {{{ Get the declaring class */
  4835. ZEND_METHOD(ReflectionProperty, getDeclaringClass)
  4836. {
  4837. reflection_object *intern;
  4838. property_reference *ref;
  4839. zend_class_entry *ce;
  4840. if (zend_parse_parameters_none() == FAILURE) {
  4841. RETURN_THROWS();
  4842. }
  4843. GET_REFLECTION_OBJECT_PTR(ref);
  4844. ce = ref->prop ? ref->prop->ce : intern->ce;
  4845. zend_reflection_class_factory(ce, return_value);
  4846. }
  4847. /* }}} */
  4848. /* {{{ Returns the doc comment for this property */
  4849. ZEND_METHOD(ReflectionProperty, getDocComment)
  4850. {
  4851. reflection_object *intern;
  4852. property_reference *ref;
  4853. if (zend_parse_parameters_none() == FAILURE) {
  4854. RETURN_THROWS();
  4855. }
  4856. GET_REFLECTION_OBJECT_PTR(ref);
  4857. if (ref->prop && ref->prop->doc_comment) {
  4858. RETURN_STR_COPY(ref->prop->doc_comment);
  4859. }
  4860. RETURN_FALSE;
  4861. }
  4862. /* }}} */
  4863. /* {{{ Returns the attributes of this property */
  4864. ZEND_METHOD(ReflectionProperty, getAttributes)
  4865. {
  4866. reflection_object *intern;
  4867. property_reference *ref;
  4868. GET_REFLECTION_OBJECT_PTR(ref);
  4869. if (ref->prop == NULL) {
  4870. RETURN_EMPTY_ARRAY();
  4871. }
  4872. reflect_attributes(INTERNAL_FUNCTION_PARAM_PASSTHRU,
  4873. ref->prop->attributes, 0, ref->prop->ce, ZEND_ATTRIBUTE_TARGET_PROPERTY,
  4874. ref->prop->ce->type == ZEND_USER_CLASS ? ref->prop->ce->info.user.filename : NULL);
  4875. }
  4876. /* }}} */
  4877. /* {{{ Sets whether non-public properties can be requested */
  4878. ZEND_METHOD(ReflectionProperty, setAccessible)
  4879. {
  4880. bool visible;
  4881. if (zend_parse_parameters(ZEND_NUM_ARGS(), "b", &visible) == FAILURE) {
  4882. RETURN_THROWS();
  4883. }
  4884. }
  4885. /* }}} */
  4886. /* {{{ Returns the type associated with the property */
  4887. ZEND_METHOD(ReflectionProperty, getType)
  4888. {
  4889. reflection_object *intern;
  4890. property_reference *ref;
  4891. if (zend_parse_parameters_none() == FAILURE) {
  4892. RETURN_THROWS();
  4893. }
  4894. GET_REFLECTION_OBJECT_PTR(ref);
  4895. if (!ref->prop || !ZEND_TYPE_IS_SET(ref->prop->type)) {
  4896. RETURN_NULL();
  4897. }
  4898. reflection_type_factory(ref->prop->type, return_value, 1);
  4899. }
  4900. /* }}} */
  4901. /* {{{ Returns whether property has a type */
  4902. ZEND_METHOD(ReflectionProperty, hasType)
  4903. {
  4904. reflection_object *intern;
  4905. property_reference *ref;
  4906. if (zend_parse_parameters_none() == FAILURE) {
  4907. RETURN_THROWS();
  4908. }
  4909. GET_REFLECTION_OBJECT_PTR(ref);
  4910. RETVAL_BOOL(ref->prop && ZEND_TYPE_IS_SET(ref->prop->type));
  4911. }
  4912. /* }}} */
  4913. /* {{{ Returns whether property has a default value */
  4914. ZEND_METHOD(ReflectionProperty, hasDefaultValue)
  4915. {
  4916. reflection_object *intern;
  4917. property_reference *ref;
  4918. zend_property_info *prop_info;
  4919. zval *prop;
  4920. if (zend_parse_parameters_none() == FAILURE) {
  4921. RETURN_THROWS();
  4922. }
  4923. GET_REFLECTION_OBJECT_PTR(ref);
  4924. prop_info = ref->prop;
  4925. if (prop_info == NULL) {
  4926. RETURN_FALSE;
  4927. }
  4928. prop = property_get_default(prop_info);
  4929. RETURN_BOOL(!Z_ISUNDEF_P(prop));
  4930. }
  4931. /* }}} */
  4932. /* {{{ Returns the default value of a property */
  4933. ZEND_METHOD(ReflectionProperty, getDefaultValue)
  4934. {
  4935. reflection_object *intern;
  4936. property_reference *ref;
  4937. zend_property_info *prop_info;
  4938. zval *prop;
  4939. if (zend_parse_parameters_none() == FAILURE) {
  4940. RETURN_THROWS();
  4941. }
  4942. GET_REFLECTION_OBJECT_PTR(ref);
  4943. prop_info = ref->prop;
  4944. if (prop_info == NULL) {
  4945. return; // throw exception?
  4946. }
  4947. prop = property_get_default(prop_info);
  4948. if (Z_ISUNDEF_P(prop)) {
  4949. return;
  4950. }
  4951. /* copy: enforce read only access */
  4952. ZVAL_DEREF(prop);
  4953. ZVAL_COPY_OR_DUP(return_value, prop);
  4954. /* this is necessary to make it able to work with default array
  4955. * properties, returned to user */
  4956. if (Z_TYPE_P(return_value) == IS_CONSTANT_AST) {
  4957. if (UNEXPECTED(zval_update_constant_ex(return_value, prop_info->ce) != SUCCESS)) {
  4958. RETURN_THROWS();
  4959. }
  4960. }
  4961. }
  4962. /* }}} */
  4963. /* {{{ Constructor. Throws an Exception in case the given extension does not exist */
  4964. ZEND_METHOD(ReflectionExtension, __construct)
  4965. {
  4966. zval *object;
  4967. char *lcname;
  4968. reflection_object *intern;
  4969. zend_module_entry *module;
  4970. char *name_str;
  4971. size_t name_len;
  4972. ALLOCA_FLAG(use_heap)
  4973. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name_str, &name_len) == FAILURE) {
  4974. RETURN_THROWS();
  4975. }
  4976. object = ZEND_THIS;
  4977. intern = Z_REFLECTION_P(object);
  4978. lcname = do_alloca(name_len + 1, use_heap);
  4979. zend_str_tolower_copy(lcname, name_str, name_len);
  4980. if ((module = zend_hash_str_find_ptr(&module_registry, lcname, name_len)) == NULL) {
  4981. free_alloca(lcname, use_heap);
  4982. zend_throw_exception_ex(reflection_exception_ptr, 0,
  4983. "Extension \"%s\" does not exist", name_str);
  4984. RETURN_THROWS();
  4985. }
  4986. free_alloca(lcname, use_heap);
  4987. ZVAL_STRING(reflection_prop_name(object), module->name);
  4988. intern->ptr = module;
  4989. intern->ref_type = REF_TYPE_OTHER;
  4990. intern->ce = NULL;
  4991. }
  4992. /* }}} */
  4993. /* {{{ Returns a string representation */
  4994. ZEND_METHOD(ReflectionExtension, __toString)
  4995. {
  4996. reflection_object *intern;
  4997. zend_module_entry *module;
  4998. smart_str str = {0};
  4999. if (zend_parse_parameters_none() == FAILURE) {
  5000. RETURN_THROWS();
  5001. }
  5002. GET_REFLECTION_OBJECT_PTR(module);
  5003. _extension_string(&str, module, "");
  5004. RETURN_STR(smart_str_extract(&str));
  5005. }
  5006. /* }}} */
  5007. /* {{{ Returns this extension's name */
  5008. ZEND_METHOD(ReflectionExtension, getName)
  5009. {
  5010. reflection_object *intern;
  5011. zend_module_entry *module;
  5012. if (zend_parse_parameters_none() == FAILURE) {
  5013. RETURN_THROWS();
  5014. }
  5015. GET_REFLECTION_OBJECT_PTR(module);
  5016. RETURN_STRING(module->name);
  5017. }
  5018. /* }}} */
  5019. /* {{{ Returns this extension's version */
  5020. ZEND_METHOD(ReflectionExtension, getVersion)
  5021. {
  5022. reflection_object *intern;
  5023. zend_module_entry *module;
  5024. if (zend_parse_parameters_none() == FAILURE) {
  5025. RETURN_THROWS();
  5026. }
  5027. GET_REFLECTION_OBJECT_PTR(module);
  5028. /* An extension does not necessarily have a version number */
  5029. if (module->version == NO_VERSION_YET) {
  5030. RETURN_NULL();
  5031. } else {
  5032. RETURN_STRING(module->version);
  5033. }
  5034. }
  5035. /* }}} */
  5036. /* {{{ Returns an array of this extension's functions */
  5037. ZEND_METHOD(ReflectionExtension, getFunctions)
  5038. {
  5039. reflection_object *intern;
  5040. zend_module_entry *module;
  5041. zval function;
  5042. zend_function *fptr;
  5043. if (zend_parse_parameters_none() == FAILURE) {
  5044. RETURN_THROWS();
  5045. }
  5046. GET_REFLECTION_OBJECT_PTR(module);
  5047. array_init(return_value);
  5048. ZEND_HASH_FOREACH_PTR(CG(function_table), fptr) {
  5049. if (fptr->common.type==ZEND_INTERNAL_FUNCTION
  5050. && fptr->internal_function.module == module) {
  5051. reflection_function_factory(fptr, NULL, &function);
  5052. zend_hash_update(Z_ARRVAL_P(return_value), fptr->common.function_name, &function);
  5053. }
  5054. } ZEND_HASH_FOREACH_END();
  5055. }
  5056. /* }}} */
  5057. /* {{{ Returns an associative array containing this extension's constants and their values */
  5058. ZEND_METHOD(ReflectionExtension, getConstants)
  5059. {
  5060. reflection_object *intern;
  5061. zend_module_entry *module;
  5062. zend_constant *constant;
  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(EG(zend_constants), constant) {
  5069. if (module->module_number == ZEND_CONSTANT_MODULE_NUMBER(constant)) {
  5070. zval const_val;
  5071. ZVAL_COPY_OR_DUP(&const_val, &constant->value);
  5072. zend_hash_update(Z_ARRVAL_P(return_value), constant->name, &const_val);
  5073. }
  5074. } ZEND_HASH_FOREACH_END();
  5075. }
  5076. /* }}} */
  5077. /* {{{ _addinientry */
  5078. static void _addinientry(zend_ini_entry *ini_entry, zval *retval, int number)
  5079. {
  5080. if (number == ini_entry->module_number) {
  5081. zval zv;
  5082. if (ini_entry->value) {
  5083. ZVAL_STR_COPY(&zv, ini_entry->value);
  5084. } else {
  5085. ZVAL_NULL(&zv);
  5086. }
  5087. zend_symtable_update(Z_ARRVAL_P(retval), ini_entry->name, &zv);
  5088. }
  5089. }
  5090. /* }}} */
  5091. /* {{{ Returns an associative array containing this extension's INI entries and their values */
  5092. ZEND_METHOD(ReflectionExtension, getINIEntries)
  5093. {
  5094. reflection_object *intern;
  5095. zend_module_entry *module;
  5096. zend_ini_entry *ini_entry;
  5097. if (zend_parse_parameters_none() == FAILURE) {
  5098. RETURN_THROWS();
  5099. }
  5100. GET_REFLECTION_OBJECT_PTR(module);
  5101. array_init(return_value);
  5102. ZEND_HASH_FOREACH_PTR(EG(ini_directives), ini_entry) {
  5103. _addinientry(ini_entry, return_value, module->module_number);
  5104. } ZEND_HASH_FOREACH_END();
  5105. }
  5106. /* }}} */
  5107. /* {{{ add_extension_class */
  5108. static void add_extension_class(zend_class_entry *ce, zend_string *key, zval *class_array, zend_module_entry *module, bool add_reflection_class)
  5109. {
  5110. if (ce->type == ZEND_INTERNAL_CLASS && ce->info.internal.module && !strcasecmp(ce->info.internal.module->name, module->name)) {
  5111. zend_string *name;
  5112. if (!zend_string_equals_ci(ce->name, key)) {
  5113. /* This is a class alias, use alias name */
  5114. name = key;
  5115. } else {
  5116. /* Use class name */
  5117. name = ce->name;
  5118. }
  5119. if (add_reflection_class) {
  5120. zval zclass;
  5121. zend_reflection_class_factory(ce, &zclass);
  5122. zend_hash_update(Z_ARRVAL_P(class_array), name, &zclass);
  5123. } else {
  5124. add_next_index_str(class_array, zend_string_copy(name));
  5125. }
  5126. }
  5127. }
  5128. /* }}} */
  5129. /* {{{ Returns an array containing ReflectionClass objects for all classes of this extension */
  5130. ZEND_METHOD(ReflectionExtension, getClasses)
  5131. {
  5132. reflection_object *intern;
  5133. zend_module_entry *module;
  5134. zend_string *key;
  5135. zend_class_entry *ce;
  5136. if (zend_parse_parameters_none() == FAILURE) {
  5137. RETURN_THROWS();
  5138. }
  5139. GET_REFLECTION_OBJECT_PTR(module);
  5140. array_init(return_value);
  5141. ZEND_HASH_FOREACH_STR_KEY_PTR(EG(class_table), key, ce) {
  5142. add_extension_class(ce, key, return_value, module, 1);
  5143. } ZEND_HASH_FOREACH_END();
  5144. }
  5145. /* }}} */
  5146. /* {{{ Returns an array containing all names of all classes of this extension */
  5147. ZEND_METHOD(ReflectionExtension, getClassNames)
  5148. {
  5149. reflection_object *intern;
  5150. zend_module_entry *module;
  5151. zend_string *key;
  5152. zend_class_entry *ce;
  5153. if (zend_parse_parameters_none() == FAILURE) {
  5154. RETURN_THROWS();
  5155. }
  5156. GET_REFLECTION_OBJECT_PTR(module);
  5157. array_init(return_value);
  5158. ZEND_HASH_FOREACH_STR_KEY_PTR(EG(class_table), key, ce) {
  5159. add_extension_class(ce, key, return_value, module, 0);
  5160. } ZEND_HASH_FOREACH_END();
  5161. }
  5162. /* }}} */
  5163. /* {{{ Returns an array containing all names of all extensions this extension depends on */
  5164. ZEND_METHOD(ReflectionExtension, getDependencies)
  5165. {
  5166. reflection_object *intern;
  5167. zend_module_entry *module;
  5168. const zend_module_dep *dep;
  5169. if (zend_parse_parameters_none() == FAILURE) {
  5170. RETURN_THROWS();
  5171. }
  5172. GET_REFLECTION_OBJECT_PTR(module);
  5173. dep = module->deps;
  5174. if (!dep)
  5175. {
  5176. RETURN_EMPTY_ARRAY();
  5177. }
  5178. array_init(return_value);
  5179. while(dep->name) {
  5180. zend_string *relation;
  5181. char *rel_type;
  5182. size_t len = 0;
  5183. switch(dep->type) {
  5184. case MODULE_DEP_REQUIRED:
  5185. rel_type = "Required";
  5186. len += sizeof("Required") - 1;
  5187. break;
  5188. case MODULE_DEP_CONFLICTS:
  5189. rel_type = "Conflicts";
  5190. len += sizeof("Conflicts") - 1;
  5191. break;
  5192. case MODULE_DEP_OPTIONAL:
  5193. rel_type = "Optional";
  5194. len += sizeof("Optional") - 1;
  5195. break;
  5196. default:
  5197. rel_type = "Error"; /* shouldn't happen */
  5198. len += sizeof("Error") - 1;
  5199. break;
  5200. }
  5201. if (dep->rel) {
  5202. len += strlen(dep->rel) + 1;
  5203. }
  5204. if (dep->version) {
  5205. len += strlen(dep->version) + 1;
  5206. }
  5207. relation = zend_string_alloc(len, 0);
  5208. snprintf(ZSTR_VAL(relation), ZSTR_LEN(relation) + 1, "%s%s%s%s%s",
  5209. rel_type,
  5210. dep->rel ? " " : "",
  5211. dep->rel ? dep->rel : "",
  5212. dep->version ? " " : "",
  5213. dep->version ? dep->version : "");
  5214. add_assoc_str(return_value, dep->name, relation);
  5215. dep++;
  5216. }
  5217. }
  5218. /* }}} */
  5219. /* {{{ Prints phpinfo block for the extension */
  5220. ZEND_METHOD(ReflectionExtension, info)
  5221. {
  5222. reflection_object *intern;
  5223. zend_module_entry *module;
  5224. if (zend_parse_parameters_none() == FAILURE) {
  5225. RETURN_THROWS();
  5226. }
  5227. GET_REFLECTION_OBJECT_PTR(module);
  5228. php_info_print_module(module);
  5229. }
  5230. /* }}} */
  5231. /* {{{ Returns whether this extension is persistent */
  5232. ZEND_METHOD(ReflectionExtension, isPersistent)
  5233. {
  5234. reflection_object *intern;
  5235. zend_module_entry *module;
  5236. if (zend_parse_parameters_none() == FAILURE) {
  5237. RETURN_THROWS();
  5238. }
  5239. GET_REFLECTION_OBJECT_PTR(module);
  5240. RETURN_BOOL(module->type == MODULE_PERSISTENT);
  5241. }
  5242. /* }}} */
  5243. /* {{{ Returns whether this extension is temporary */
  5244. ZEND_METHOD(ReflectionExtension, isTemporary)
  5245. {
  5246. reflection_object *intern;
  5247. zend_module_entry *module;
  5248. if (zend_parse_parameters_none() == FAILURE) {
  5249. RETURN_THROWS();
  5250. }
  5251. GET_REFLECTION_OBJECT_PTR(module);
  5252. RETURN_BOOL(module->type == MODULE_TEMPORARY);
  5253. }
  5254. /* }}} */
  5255. /* {{{ Constructor. Throws an Exception in case the given Zend extension does not exist */
  5256. ZEND_METHOD(ReflectionZendExtension, __construct)
  5257. {
  5258. zval *object;
  5259. reflection_object *intern;
  5260. zend_extension *extension;
  5261. char *name_str;
  5262. size_t name_len;
  5263. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name_str, &name_len) == FAILURE) {
  5264. RETURN_THROWS();
  5265. }
  5266. object = ZEND_THIS;
  5267. intern = Z_REFLECTION_P(object);
  5268. extension = zend_get_extension(name_str);
  5269. if (!extension) {
  5270. zend_throw_exception_ex(reflection_exception_ptr, 0,
  5271. "Zend Extension \"%s\" does not exist", name_str);
  5272. RETURN_THROWS();
  5273. }
  5274. ZVAL_STRING(reflection_prop_name(object), extension->name);
  5275. intern->ptr = extension;
  5276. intern->ref_type = REF_TYPE_OTHER;
  5277. intern->ce = NULL;
  5278. }
  5279. /* }}} */
  5280. /* {{{ Returns a string representation */
  5281. ZEND_METHOD(ReflectionZendExtension, __toString)
  5282. {
  5283. reflection_object *intern;
  5284. zend_extension *extension;
  5285. smart_str str = {0};
  5286. if (zend_parse_parameters_none() == FAILURE) {
  5287. RETURN_THROWS();
  5288. }
  5289. GET_REFLECTION_OBJECT_PTR(extension);
  5290. _zend_extension_string(&str, extension, "");
  5291. RETURN_STR(smart_str_extract(&str));
  5292. }
  5293. /* }}} */
  5294. /* {{{ Returns the name of this Zend extension */
  5295. ZEND_METHOD(ReflectionZendExtension, getName)
  5296. {
  5297. reflection_object *intern;
  5298. zend_extension *extension;
  5299. if (zend_parse_parameters_none() == FAILURE) {
  5300. RETURN_THROWS();
  5301. }
  5302. GET_REFLECTION_OBJECT_PTR(extension);
  5303. RETURN_STRING(extension->name);
  5304. }
  5305. /* }}} */
  5306. /* {{{ Returns the version information of this Zend extension */
  5307. ZEND_METHOD(ReflectionZendExtension, getVersion)
  5308. {
  5309. reflection_object *intern;
  5310. zend_extension *extension;
  5311. if (zend_parse_parameters_none() == FAILURE) {
  5312. RETURN_THROWS();
  5313. }
  5314. GET_REFLECTION_OBJECT_PTR(extension);
  5315. if (extension->version) {
  5316. RETURN_STRING(extension->version);
  5317. } else {
  5318. RETURN_EMPTY_STRING();
  5319. }
  5320. }
  5321. /* }}} */
  5322. /* {{{ Returns the name of this Zend extension's author */
  5323. ZEND_METHOD(ReflectionZendExtension, getAuthor)
  5324. {
  5325. reflection_object *intern;
  5326. zend_extension *extension;
  5327. if (zend_parse_parameters_none() == FAILURE) {
  5328. RETURN_THROWS();
  5329. }
  5330. GET_REFLECTION_OBJECT_PTR(extension);
  5331. if (extension->author) {
  5332. RETURN_STRING(extension->author);
  5333. } else {
  5334. RETURN_EMPTY_STRING();
  5335. }
  5336. }
  5337. /* }}} */
  5338. /* {{{ Returns this Zend extension's URL*/
  5339. ZEND_METHOD(ReflectionZendExtension, getURL)
  5340. {
  5341. reflection_object *intern;
  5342. zend_extension *extension;
  5343. if (zend_parse_parameters_none() == FAILURE) {
  5344. RETURN_THROWS();
  5345. }
  5346. GET_REFLECTION_OBJECT_PTR(extension);
  5347. if (extension->URL) {
  5348. RETURN_STRING(extension->URL);
  5349. } else {
  5350. RETURN_EMPTY_STRING();
  5351. }
  5352. }
  5353. /* }}} */
  5354. /* {{{ Returns this Zend extension's copyright information */
  5355. ZEND_METHOD(ReflectionZendExtension, getCopyright)
  5356. {
  5357. reflection_object *intern;
  5358. zend_extension *extension;
  5359. if (zend_parse_parameters_none() == FAILURE) {
  5360. RETURN_THROWS();
  5361. }
  5362. GET_REFLECTION_OBJECT_PTR(extension);
  5363. if (extension->copyright) {
  5364. RETURN_STRING(extension->copyright);
  5365. } else {
  5366. RETURN_EMPTY_STRING();
  5367. }
  5368. }
  5369. /* }}} */
  5370. /* {{{ Dummy constructor -- always throws ReflectionExceptions. */
  5371. ZEND_METHOD(ReflectionReference, __construct)
  5372. {
  5373. _DO_THROW(
  5374. "Cannot directly instantiate ReflectionReference. "
  5375. "Use ReflectionReference::fromArrayElement() instead"
  5376. );
  5377. }
  5378. /* }}} */
  5379. static bool is_ignorable_reference(HashTable *ht, zval *ref) {
  5380. if (Z_REFCOUNT_P(ref) != 1) {
  5381. return 0;
  5382. }
  5383. /* Directly self-referential arrays are treated as proper references
  5384. * in zend_array_dup() despite rc=1. */
  5385. return Z_TYPE_P(Z_REFVAL_P(ref)) != IS_ARRAY || Z_ARRVAL_P(Z_REFVAL_P(ref)) != ht;
  5386. }
  5387. /* {{{ Create ReflectionReference for array item. Returns null if not a reference. */
  5388. ZEND_METHOD(ReflectionReference, fromArrayElement)
  5389. {
  5390. HashTable *ht;
  5391. zval *item;
  5392. zend_string *string_key = NULL;
  5393. zend_long int_key = 0;
  5394. reflection_object *intern;
  5395. ZEND_PARSE_PARAMETERS_START(2, 2)
  5396. Z_PARAM_ARRAY_HT(ht)
  5397. Z_PARAM_STR_OR_LONG(string_key, int_key)
  5398. ZEND_PARSE_PARAMETERS_END();
  5399. if (string_key) {
  5400. item = zend_hash_find(ht, string_key);
  5401. } else {
  5402. item = zend_hash_index_find(ht, int_key);
  5403. }
  5404. if (!item) {
  5405. _DO_THROW("Array key not found");
  5406. RETURN_THROWS();
  5407. }
  5408. if (Z_TYPE_P(item) != IS_REFERENCE || is_ignorable_reference(ht, item)) {
  5409. RETURN_NULL();
  5410. }
  5411. object_init_ex(return_value, reflection_reference_ptr);
  5412. intern = Z_REFLECTION_P(return_value);
  5413. ZVAL_COPY(&intern->obj, item);
  5414. intern->ref_type = REF_TYPE_OTHER;
  5415. }
  5416. /* }}} */
  5417. /* {{{ Returns a unique identifier for the reference.
  5418. * The format of the return value is unspecified and may change. */
  5419. ZEND_METHOD(ReflectionReference, getId)
  5420. {
  5421. reflection_object *intern;
  5422. unsigned char digest[20];
  5423. PHP_SHA1_CTX context;
  5424. if (zend_parse_parameters_none() == FAILURE) {
  5425. RETURN_THROWS();
  5426. }
  5427. intern = Z_REFLECTION_P(getThis());
  5428. if (Z_TYPE(intern->obj) != IS_REFERENCE) {
  5429. _DO_THROW("Corrupted ReflectionReference object");
  5430. RETURN_THROWS();
  5431. }
  5432. if (!REFLECTION_G(key_initialized)) {
  5433. if (php_random_bytes_throw(&REFLECTION_G(key), 16) == FAILURE) {
  5434. RETURN_THROWS();
  5435. }
  5436. REFLECTION_G(key_initialized) = 1;
  5437. }
  5438. /* SHA1(ref || key) to avoid directly exposing memory addresses. */
  5439. PHP_SHA1Init(&context);
  5440. PHP_SHA1Update(&context, (unsigned char *) &Z_REF(intern->obj), sizeof(zend_reference *));
  5441. PHP_SHA1Update(&context, REFLECTION_G(key), REFLECTION_KEY_LEN);
  5442. PHP_SHA1Final(digest, &context);
  5443. RETURN_STRINGL((char *) digest, sizeof(digest));
  5444. }
  5445. /* }}} */
  5446. ZEND_METHOD(ReflectionAttribute, __construct)
  5447. {
  5448. }
  5449. ZEND_METHOD(ReflectionAttribute, __clone)
  5450. {
  5451. /* Should never be executable */
  5452. _DO_THROW("Cannot clone object using __clone()");
  5453. }
  5454. /* {{{ Returns a string representation */
  5455. ZEND_METHOD(ReflectionAttribute, __toString)
  5456. {
  5457. reflection_object *intern;
  5458. attribute_reference *attr;
  5459. if (zend_parse_parameters_none() == FAILURE) {
  5460. RETURN_THROWS();
  5461. }
  5462. GET_REFLECTION_OBJECT_PTR(attr);
  5463. smart_str str = {0};
  5464. smart_str_appends(&str, "Attribute [ ");
  5465. smart_str_append(&str, attr->data->name);
  5466. smart_str_appends(&str, " ]");
  5467. if (attr->data->argc > 0) {
  5468. smart_str_appends(&str, " {\n");
  5469. smart_str_append_printf(&str, " - Arguments [%d] {\n", attr->data->argc);
  5470. for (uint32_t i = 0; i < attr->data->argc; i++) {
  5471. smart_str_append_printf(&str, " Argument #%d [ ", i);
  5472. if (attr->data->args[i].name != NULL) {
  5473. smart_str_append(&str, attr->data->args[i].name);
  5474. smart_str_appends(&str, " = ");
  5475. }
  5476. if (format_default_value(&str, &attr->data->args[i].value) == FAILURE) {
  5477. smart_str_free(&str);
  5478. RETURN_THROWS();
  5479. }
  5480. smart_str_appends(&str, " ]\n");
  5481. }
  5482. smart_str_appends(&str, " }\n");
  5483. smart_str_appends(&str, "}\n");
  5484. } else {
  5485. smart_str_appendc(&str, '\n');
  5486. }
  5487. RETURN_STR(smart_str_extract(&str));
  5488. }
  5489. /* }}} */
  5490. /* {{{ * Returns the name of the attribute */
  5491. ZEND_METHOD(ReflectionAttribute, getName)
  5492. {
  5493. reflection_object *intern;
  5494. attribute_reference *attr;
  5495. if (zend_parse_parameters_none() == FAILURE) {
  5496. RETURN_THROWS();
  5497. }
  5498. GET_REFLECTION_OBJECT_PTR(attr);
  5499. RETURN_STR_COPY(attr->data->name);
  5500. }
  5501. /* }}} */
  5502. /* {{{ * Returns the target of the attribute */
  5503. ZEND_METHOD(ReflectionAttribute, getTarget)
  5504. {
  5505. reflection_object *intern;
  5506. attribute_reference *attr;
  5507. if (zend_parse_parameters_none() == FAILURE) {
  5508. RETURN_THROWS();
  5509. }
  5510. GET_REFLECTION_OBJECT_PTR(attr);
  5511. RETURN_LONG(attr->target);
  5512. }
  5513. /* }}} */
  5514. /* {{{ * Returns true if the attribute is repeated */
  5515. ZEND_METHOD(ReflectionAttribute, isRepeated)
  5516. {
  5517. reflection_object *intern;
  5518. attribute_reference *attr;
  5519. if (zend_parse_parameters_none() == FAILURE) {
  5520. RETURN_THROWS();
  5521. }
  5522. GET_REFLECTION_OBJECT_PTR(attr);
  5523. RETURN_BOOL(zend_is_attribute_repeated(attr->attributes, attr->data));
  5524. }
  5525. /* }}} */
  5526. /* {{{ * Returns the arguments passed to the attribute */
  5527. ZEND_METHOD(ReflectionAttribute, getArguments)
  5528. {
  5529. reflection_object *intern;
  5530. attribute_reference *attr;
  5531. zval tmp;
  5532. uint32_t i;
  5533. if (zend_parse_parameters_none() == FAILURE) {
  5534. RETURN_THROWS();
  5535. }
  5536. GET_REFLECTION_OBJECT_PTR(attr);
  5537. array_init(return_value);
  5538. for (i = 0; i < attr->data->argc; i++) {
  5539. if (FAILURE == zend_get_attribute_value(&tmp, attr->data, i, attr->scope)) {
  5540. RETURN_THROWS();
  5541. }
  5542. if (attr->data->args[i].name) {
  5543. /* We ensured at compile-time that there are no duplicate parameter names. */
  5544. zend_hash_add_new(Z_ARRVAL_P(return_value), attr->data->args[i].name, &tmp);
  5545. } else {
  5546. add_next_index_zval(return_value, &tmp);
  5547. }
  5548. }
  5549. }
  5550. /* }}} */
  5551. static int call_attribute_constructor(
  5552. zend_attribute *attr, zend_class_entry *ce, zend_object *obj,
  5553. zval *args, uint32_t argc, HashTable *named_params, zend_string *filename)
  5554. {
  5555. zend_function *ctor = ce->constructor;
  5556. zend_execute_data *call = NULL;
  5557. ZEND_ASSERT(ctor != NULL);
  5558. if (!(ctor->common.fn_flags & ZEND_ACC_PUBLIC)) {
  5559. zend_throw_error(NULL, "Attribute constructor of class %s must be public", ZSTR_VAL(ce->name));
  5560. return FAILURE;
  5561. }
  5562. if (filename) {
  5563. /* Set up dummy call frame that makes it look like the attribute was invoked
  5564. * from where it occurs in the code. */
  5565. zend_function dummy_func;
  5566. zend_op *opline;
  5567. memset(&dummy_func, 0, sizeof(zend_function));
  5568. call = zend_vm_stack_push_call_frame_ex(
  5569. ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_execute_data), sizeof(zval)) +
  5570. ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_op), sizeof(zval)) +
  5571. ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_function), sizeof(zval)),
  5572. 0, &dummy_func, 0, NULL);
  5573. opline = (zend_op*)(call + 1);
  5574. memset(opline, 0, sizeof(zend_op));
  5575. opline->opcode = ZEND_DO_FCALL;
  5576. opline->lineno = attr->lineno;
  5577. call->opline = opline;
  5578. call->call = NULL;
  5579. call->return_value = NULL;
  5580. call->func = (zend_function*)(call->opline + 1);
  5581. call->prev_execute_data = EG(current_execute_data);
  5582. memset(call->func, 0, sizeof(zend_function));
  5583. call->func->type = ZEND_USER_FUNCTION;
  5584. call->func->op_array.fn_flags =
  5585. attr->flags & ZEND_ATTRIBUTE_STRICT_TYPES ? ZEND_ACC_STRICT_TYPES : 0;
  5586. call->func->op_array.fn_flags |= ZEND_ACC_CALL_VIA_TRAMPOLINE;
  5587. call->func->op_array.filename = filename;
  5588. EG(current_execute_data) = call;
  5589. }
  5590. zend_call_known_function(ctor, obj, obj->ce, NULL, argc, args, named_params);
  5591. if (filename) {
  5592. EG(current_execute_data) = call->prev_execute_data;
  5593. zend_vm_stack_free_call_frame(call);
  5594. }
  5595. if (EG(exception)) {
  5596. zend_object_store_ctor_failed(obj);
  5597. return FAILURE;
  5598. }
  5599. return SUCCESS;
  5600. }
  5601. static void attribute_ctor_cleanup(
  5602. zval *obj, zval *args, uint32_t argc, HashTable *named_params) /* {{{ */
  5603. {
  5604. if (obj) {
  5605. zval_ptr_dtor(obj);
  5606. }
  5607. if (args) {
  5608. uint32_t i;
  5609. for (i = 0; i < argc; i++) {
  5610. zval_ptr_dtor(&args[i]);
  5611. }
  5612. efree(args);
  5613. }
  5614. if (named_params) {
  5615. zend_array_destroy(named_params);
  5616. }
  5617. }
  5618. /* }}} */
  5619. /* {{{ * Returns the attribute as an object */
  5620. ZEND_METHOD(ReflectionAttribute, newInstance)
  5621. {
  5622. reflection_object *intern;
  5623. attribute_reference *attr;
  5624. zend_attribute *marker;
  5625. zend_class_entry *ce;
  5626. zval obj;
  5627. zval *args = NULL;
  5628. HashTable *named_params = NULL;
  5629. if (zend_parse_parameters_none() == FAILURE) {
  5630. RETURN_THROWS();
  5631. }
  5632. GET_REFLECTION_OBJECT_PTR(attr);
  5633. if (NULL == (ce = zend_lookup_class(attr->data->name))) {
  5634. zend_throw_error(NULL, "Attribute class \"%s\" not found", ZSTR_VAL(attr->data->name));
  5635. RETURN_THROWS();
  5636. }
  5637. if (NULL == (marker = zend_get_attribute_str(ce->attributes, ZEND_STRL("attribute")))) {
  5638. zend_throw_error(NULL, "Attempting to use non-attribute class \"%s\" as attribute", ZSTR_VAL(attr->data->name));
  5639. RETURN_THROWS();
  5640. }
  5641. if (ce->type == ZEND_USER_CLASS) {
  5642. uint32_t flags = ZEND_ATTRIBUTE_TARGET_ALL;
  5643. if (marker->argc > 0) {
  5644. zval tmp;
  5645. if (FAILURE == zend_get_attribute_value(&tmp, marker, 0, ce)) {
  5646. RETURN_THROWS();
  5647. }
  5648. flags = (uint32_t) Z_LVAL(tmp);
  5649. }
  5650. if (!(attr->target & flags)) {
  5651. zend_string *location = zend_get_attribute_target_names(attr->target);
  5652. zend_string *allowed = zend_get_attribute_target_names(flags);
  5653. zend_throw_error(NULL, "Attribute \"%s\" cannot target %s (allowed targets: %s)",
  5654. ZSTR_VAL(attr->data->name), ZSTR_VAL(location), ZSTR_VAL(allowed)
  5655. );
  5656. zend_string_release(location);
  5657. zend_string_release(allowed);
  5658. RETURN_THROWS();
  5659. }
  5660. if (!(flags & ZEND_ATTRIBUTE_IS_REPEATABLE)) {
  5661. if (zend_is_attribute_repeated(attr->attributes, attr->data)) {
  5662. zend_throw_error(NULL, "Attribute \"%s\" must not be repeated", ZSTR_VAL(attr->data->name));
  5663. RETURN_THROWS();
  5664. }
  5665. }
  5666. }
  5667. if (SUCCESS != object_init_ex(&obj, ce)) {
  5668. RETURN_THROWS();
  5669. }
  5670. uint32_t argc = 0;
  5671. if (attr->data->argc) {
  5672. args = emalloc(attr->data->argc * sizeof(zval));
  5673. for (uint32_t i = 0; i < attr->data->argc; i++) {
  5674. zval val;
  5675. if (FAILURE == zend_get_attribute_value(&val, attr->data, i, attr->scope)) {
  5676. attribute_ctor_cleanup(&obj, args, argc, named_params);
  5677. RETURN_THROWS();
  5678. }
  5679. if (attr->data->args[i].name) {
  5680. if (!named_params) {
  5681. named_params = zend_new_array(0);
  5682. }
  5683. zend_hash_add_new(named_params, attr->data->args[i].name, &val);
  5684. } else {
  5685. ZVAL_COPY_VALUE(&args[i], &val);
  5686. argc++;
  5687. }
  5688. }
  5689. }
  5690. if (ce->constructor) {
  5691. if (FAILURE == call_attribute_constructor(attr->data, ce, Z_OBJ(obj), args, argc, named_params, attr->filename)) {
  5692. attribute_ctor_cleanup(&obj, args, argc, named_params);
  5693. RETURN_THROWS();
  5694. }
  5695. } else if (argc || named_params) {
  5696. attribute_ctor_cleanup(&obj, args, argc, named_params);
  5697. zend_throw_error(NULL, "Attribute class %s does not have a constructor, cannot pass arguments", ZSTR_VAL(ce->name));
  5698. RETURN_THROWS();
  5699. }
  5700. attribute_ctor_cleanup(NULL, args, argc, named_params);
  5701. RETURN_COPY_VALUE(&obj);
  5702. }
  5703. ZEND_METHOD(ReflectionEnum, __construct)
  5704. {
  5705. reflection_class_object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  5706. if (EG(exception)) {
  5707. RETURN_THROWS();
  5708. }
  5709. reflection_object *intern;
  5710. zend_class_entry *ce;
  5711. GET_REFLECTION_OBJECT_PTR(ce);
  5712. if (!(ce->ce_flags & ZEND_ACC_ENUM)) {
  5713. zend_throw_exception_ex(reflection_exception_ptr, -1, "Class \"%s\" is not an enum", ZSTR_VAL(ce->name));
  5714. RETURN_THROWS();
  5715. }
  5716. }
  5717. ZEND_METHOD(ReflectionEnum, hasCase)
  5718. {
  5719. reflection_object *intern;
  5720. zend_class_entry *ce;
  5721. zend_string *name;
  5722. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
  5723. RETURN_THROWS();
  5724. }
  5725. GET_REFLECTION_OBJECT_PTR(ce);
  5726. zend_class_constant *class_const = zend_hash_find_ptr(&ce->constants_table, name);
  5727. if (class_const == NULL) {
  5728. RETURN_FALSE;
  5729. }
  5730. RETURN_BOOL(ZEND_CLASS_CONST_FLAGS(class_const) & ZEND_CLASS_CONST_IS_CASE);
  5731. }
  5732. ZEND_METHOD(ReflectionEnum, getCase)
  5733. {
  5734. reflection_object *intern;
  5735. zend_class_entry *ce;
  5736. zend_string *name;
  5737. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
  5738. RETURN_THROWS();
  5739. }
  5740. GET_REFLECTION_OBJECT_PTR(ce);
  5741. zend_class_constant *constant = zend_hash_find_ptr(&ce->constants_table, name);
  5742. if (constant == NULL) {
  5743. zend_throw_exception_ex(reflection_exception_ptr, 0, "Case %s::%s does not exist", ZSTR_VAL(ce->name), ZSTR_VAL(name));
  5744. RETURN_THROWS();
  5745. }
  5746. if (!(ZEND_CLASS_CONST_FLAGS(constant) & ZEND_CLASS_CONST_IS_CASE)) {
  5747. zend_throw_exception_ex(reflection_exception_ptr, 0, "%s::%s is not a case", ZSTR_VAL(ce->name), ZSTR_VAL(name));
  5748. RETURN_THROWS();
  5749. }
  5750. reflection_enum_case_factory(ce, name, constant, return_value);
  5751. }
  5752. ZEND_METHOD(ReflectionEnum, getCases)
  5753. {
  5754. reflection_object *intern;
  5755. zend_class_entry *ce;
  5756. zend_string *name;
  5757. zend_class_constant *constant;
  5758. if (zend_parse_parameters_none() == FAILURE) {
  5759. RETURN_THROWS();
  5760. }
  5761. GET_REFLECTION_OBJECT_PTR(ce);
  5762. array_init(return_value);
  5763. ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->constants_table, name, constant) {
  5764. if (ZEND_CLASS_CONST_FLAGS(constant) & ZEND_CLASS_CONST_IS_CASE) {
  5765. zval class_const;
  5766. reflection_enum_case_factory(ce, name, constant, &class_const);
  5767. zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &class_const);
  5768. }
  5769. } ZEND_HASH_FOREACH_END();
  5770. }
  5771. ZEND_METHOD(ReflectionEnum, isBacked)
  5772. {
  5773. reflection_object *intern;
  5774. zend_class_entry *ce;
  5775. if (zend_parse_parameters_none() == FAILURE) {
  5776. RETURN_THROWS();
  5777. }
  5778. GET_REFLECTION_OBJECT_PTR(ce);
  5779. RETURN_BOOL(ce->enum_backing_type != IS_UNDEF);
  5780. }
  5781. ZEND_METHOD(ReflectionEnum, getBackingType)
  5782. {
  5783. reflection_object *intern;
  5784. zend_class_entry *ce;
  5785. if (zend_parse_parameters_none() == FAILURE) {
  5786. RETURN_THROWS();
  5787. }
  5788. GET_REFLECTION_OBJECT_PTR(ce);
  5789. if (ce->enum_backing_type == IS_UNDEF) {
  5790. RETURN_NULL();
  5791. } else {
  5792. zend_type type = ZEND_TYPE_INIT_CODE(ce->enum_backing_type, 0, 0);
  5793. reflection_type_factory(type, return_value, 0);
  5794. }
  5795. }
  5796. ZEND_METHOD(ReflectionEnumUnitCase, __construct)
  5797. {
  5798. ZEND_MN(ReflectionClassConstant___construct)(INTERNAL_FUNCTION_PARAM_PASSTHRU);
  5799. if (EG(exception)) {
  5800. RETURN_THROWS();
  5801. }
  5802. reflection_object *intern;
  5803. zend_class_constant *ref;
  5804. GET_REFLECTION_OBJECT_PTR(ref);
  5805. if (!(ZEND_CLASS_CONST_FLAGS(ref) & ZEND_CLASS_CONST_IS_CASE)) {
  5806. zval *case_name = reflection_prop_name(ZEND_THIS);
  5807. 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));
  5808. RETURN_THROWS();
  5809. }
  5810. }
  5811. ZEND_METHOD(ReflectionEnumUnitCase, getEnum)
  5812. {
  5813. reflection_object *intern;
  5814. zend_class_constant *ref;
  5815. if (zend_parse_parameters_none() == FAILURE) {
  5816. RETURN_THROWS();
  5817. }
  5818. GET_REFLECTION_OBJECT_PTR(ref);
  5819. zend_reflection_class_factory(ref->ce, return_value);
  5820. }
  5821. ZEND_METHOD(ReflectionEnumBackedCase, __construct)
  5822. {
  5823. ZEND_MN(ReflectionEnumUnitCase___construct)(INTERNAL_FUNCTION_PARAM_PASSTHRU);
  5824. if (EG(exception)) {
  5825. RETURN_THROWS();
  5826. }
  5827. reflection_object *intern;
  5828. zend_class_constant *ref;
  5829. GET_REFLECTION_OBJECT_PTR(ref);
  5830. if (ref->ce->enum_backing_type == IS_UNDEF) {
  5831. zval *case_name = reflection_prop_name(ZEND_THIS);
  5832. 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));
  5833. RETURN_THROWS();
  5834. }
  5835. }
  5836. ZEND_METHOD(ReflectionEnumBackedCase, getBackingValue)
  5837. {
  5838. reflection_object *intern;
  5839. zend_class_constant *ref;
  5840. if (zend_parse_parameters_none() == FAILURE) {
  5841. RETURN_THROWS();
  5842. }
  5843. GET_REFLECTION_OBJECT_PTR(ref);
  5844. if (Z_TYPE(ref->value) == IS_CONSTANT_AST) {
  5845. zval_update_constant_ex(&ref->value, ref->ce);
  5846. }
  5847. ZEND_ASSERT(intern->ce->enum_backing_type != IS_UNDEF);
  5848. zval *member_p = zend_enum_fetch_case_value(Z_OBJ(ref->value));
  5849. ZVAL_COPY_OR_DUP(return_value, member_p);
  5850. }
  5851. /* {{{ proto ReflectionFiber::__construct(Fiber $fiber) */
  5852. ZEND_METHOD(ReflectionFiber, __construct)
  5853. {
  5854. zval *fiber, *object;
  5855. reflection_object *intern;
  5856. object = ZEND_THIS;
  5857. intern = Z_REFLECTION_P(object);
  5858. ZEND_PARSE_PARAMETERS_START(1, 1)
  5859. Z_PARAM_OBJECT_OF_CLASS(fiber, zend_ce_fiber)
  5860. ZEND_PARSE_PARAMETERS_END();
  5861. if (intern->ce) {
  5862. zval_ptr_dtor(&intern->obj);
  5863. }
  5864. intern->ref_type = REF_TYPE_FIBER;
  5865. ZVAL_OBJ_COPY(&intern->obj, Z_OBJ_P(fiber));
  5866. intern->ce = zend_ce_fiber;
  5867. }
  5868. /* }}} */
  5869. ZEND_METHOD(ReflectionFiber, getFiber)
  5870. {
  5871. ZEND_PARSE_PARAMETERS_NONE();
  5872. RETURN_OBJ_COPY(Z_OBJ(Z_REFLECTION_P(ZEND_THIS)->obj));
  5873. }
  5874. #define REFLECTION_CHECK_VALID_FIBER(fiber) do { \
  5875. if (fiber == NULL || fiber->context.status == ZEND_FIBER_STATUS_INIT || fiber->context.status == ZEND_FIBER_STATUS_DEAD) { \
  5876. zend_throw_error(NULL, "Cannot fetch information from a fiber that has not been started or is terminated"); \
  5877. RETURN_THROWS(); \
  5878. } \
  5879. } while (0)
  5880. ZEND_METHOD(ReflectionFiber, getTrace)
  5881. {
  5882. zend_fiber *fiber = (zend_fiber *) Z_OBJ(Z_REFLECTION_P(ZEND_THIS)->obj);
  5883. zend_long options = DEBUG_BACKTRACE_PROVIDE_OBJECT;
  5884. zend_execute_data *prev_execute_data;
  5885. ZEND_PARSE_PARAMETERS_START(0, 1)
  5886. Z_PARAM_OPTIONAL
  5887. Z_PARAM_LONG(options);
  5888. ZEND_PARSE_PARAMETERS_END();
  5889. REFLECTION_CHECK_VALID_FIBER(fiber);
  5890. prev_execute_data = fiber->stack_bottom->prev_execute_data;
  5891. fiber->stack_bottom->prev_execute_data = NULL;
  5892. if (EG(active_fiber) != fiber) {
  5893. // No need to replace current execute data if within the current fiber.
  5894. EG(current_execute_data) = fiber->execute_data;
  5895. }
  5896. zend_fetch_debug_backtrace(return_value, 0, options, 0);
  5897. EG(current_execute_data) = execute_data; // Restore original execute data.
  5898. fiber->stack_bottom->prev_execute_data = prev_execute_data; // Restore prev execute data on fiber stack.
  5899. }
  5900. ZEND_METHOD(ReflectionFiber, getExecutingLine)
  5901. {
  5902. zend_fiber *fiber = (zend_fiber *) Z_OBJ(Z_REFLECTION_P(ZEND_THIS)->obj);
  5903. zend_execute_data *prev_execute_data;
  5904. ZEND_PARSE_PARAMETERS_NONE();
  5905. REFLECTION_CHECK_VALID_FIBER(fiber);
  5906. if (EG(active_fiber) == fiber) {
  5907. prev_execute_data = execute_data->prev_execute_data;
  5908. } else {
  5909. prev_execute_data = fiber->execute_data->prev_execute_data;
  5910. }
  5911. RETURN_LONG(prev_execute_data->opline->lineno);
  5912. }
  5913. ZEND_METHOD(ReflectionFiber, getExecutingFile)
  5914. {
  5915. zend_fiber *fiber = (zend_fiber *) Z_OBJ(Z_REFLECTION_P(ZEND_THIS)->obj);
  5916. zend_execute_data *prev_execute_data;
  5917. ZEND_PARSE_PARAMETERS_NONE();
  5918. REFLECTION_CHECK_VALID_FIBER(fiber);
  5919. if (EG(active_fiber) == fiber) {
  5920. prev_execute_data = execute_data->prev_execute_data;
  5921. } else {
  5922. prev_execute_data = fiber->execute_data->prev_execute_data;
  5923. }
  5924. RETURN_STR_COPY(prev_execute_data->func->op_array.filename);
  5925. }
  5926. ZEND_METHOD(ReflectionFiber, getCallable)
  5927. {
  5928. zend_fiber *fiber = (zend_fiber *) Z_OBJ(Z_REFLECTION_P(ZEND_THIS)->obj);
  5929. ZEND_PARSE_PARAMETERS_NONE();
  5930. if (fiber == NULL || fiber->context.status == ZEND_FIBER_STATUS_DEAD) {
  5931. zend_throw_error(NULL, "Cannot fetch the callable from a fiber that has terminated"); \
  5932. RETURN_THROWS();
  5933. }
  5934. RETURN_COPY(&fiber->fci.function_name);
  5935. }
  5936. /* {{{ _reflection_write_property */
  5937. static zval *_reflection_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot)
  5938. {
  5939. if (zend_hash_exists(&object->ce->properties_info, name)
  5940. && (zend_string_equals_literal(name, "name") || zend_string_equals_literal(name, "class")))
  5941. {
  5942. zend_throw_exception_ex(reflection_exception_ptr, 0,
  5943. "Cannot set read-only property %s::$%s", ZSTR_VAL(object->ce->name), ZSTR_VAL(name));
  5944. return &EG(uninitialized_zval);
  5945. }
  5946. else
  5947. {
  5948. return zend_std_write_property(object, name, value, cache_slot);
  5949. }
  5950. }
  5951. /* }}} */
  5952. PHP_MINIT_FUNCTION(reflection) /* {{{ */
  5953. {
  5954. memcpy(&reflection_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
  5955. reflection_object_handlers.offset = XtOffsetOf(reflection_object, zo);
  5956. reflection_object_handlers.free_obj = reflection_free_objects_storage;
  5957. reflection_object_handlers.clone_obj = NULL;
  5958. reflection_object_handlers.write_property = _reflection_write_property;
  5959. reflection_object_handlers.get_gc = reflection_get_gc;
  5960. reflection_exception_ptr = register_class_ReflectionException(zend_ce_exception);
  5961. reflection_ptr = register_class_Reflection();
  5962. reflector_ptr = register_class_Reflector(zend_ce_stringable);
  5963. reflection_function_abstract_ptr = register_class_ReflectionFunctionAbstract(reflector_ptr);
  5964. reflection_function_abstract_ptr->create_object = reflection_objects_new;
  5965. reflection_function_ptr = register_class_ReflectionFunction(reflection_function_abstract_ptr);
  5966. reflection_function_ptr->create_object = reflection_objects_new;
  5967. REGISTER_REFLECTION_CLASS_CONST_LONG(function, "IS_DEPRECATED", ZEND_ACC_DEPRECATED);
  5968. reflection_generator_ptr = register_class_ReflectionGenerator();
  5969. reflection_generator_ptr->create_object = reflection_objects_new;
  5970. reflection_parameter_ptr = register_class_ReflectionParameter(reflector_ptr);
  5971. reflection_parameter_ptr->create_object = reflection_objects_new;
  5972. reflection_type_ptr = register_class_ReflectionType(zend_ce_stringable);
  5973. reflection_type_ptr->create_object = reflection_objects_new;
  5974. reflection_named_type_ptr = register_class_ReflectionNamedType(reflection_type_ptr);
  5975. reflection_named_type_ptr->create_object = reflection_objects_new;
  5976. reflection_union_type_ptr = register_class_ReflectionUnionType(reflection_type_ptr);
  5977. reflection_union_type_ptr->create_object = reflection_objects_new;
  5978. reflection_intersection_type_ptr = register_class_ReflectionIntersectionType(reflection_type_ptr);
  5979. reflection_intersection_type_ptr->create_object = reflection_objects_new;
  5980. reflection_method_ptr = register_class_ReflectionMethod(reflection_function_abstract_ptr);
  5981. reflection_method_ptr->create_object = reflection_objects_new;
  5982. REGISTER_REFLECTION_CLASS_CONST_LONG(method, "IS_STATIC", ZEND_ACC_STATIC);
  5983. REGISTER_REFLECTION_CLASS_CONST_LONG(method, "IS_PUBLIC", ZEND_ACC_PUBLIC);
  5984. REGISTER_REFLECTION_CLASS_CONST_LONG(method, "IS_PROTECTED", ZEND_ACC_PROTECTED);
  5985. REGISTER_REFLECTION_CLASS_CONST_LONG(method, "IS_PRIVATE", ZEND_ACC_PRIVATE);
  5986. REGISTER_REFLECTION_CLASS_CONST_LONG(method, "IS_ABSTRACT", ZEND_ACC_ABSTRACT);
  5987. REGISTER_REFLECTION_CLASS_CONST_LONG(method, "IS_FINAL", ZEND_ACC_FINAL);
  5988. reflection_class_ptr = register_class_ReflectionClass(reflector_ptr);
  5989. reflection_class_ptr->create_object = reflection_objects_new;
  5990. /* IS_IMPLICIT_ABSTRACT is not longer used */
  5991. REGISTER_REFLECTION_CLASS_CONST_LONG(class, "IS_IMPLICIT_ABSTRACT", ZEND_ACC_IMPLICIT_ABSTRACT_CLASS);
  5992. REGISTER_REFLECTION_CLASS_CONST_LONG(class, "IS_EXPLICIT_ABSTRACT", ZEND_ACC_EXPLICIT_ABSTRACT_CLASS);
  5993. REGISTER_REFLECTION_CLASS_CONST_LONG(class, "IS_FINAL", ZEND_ACC_FINAL);
  5994. reflection_object_ptr = register_class_ReflectionObject(reflection_class_ptr);
  5995. reflection_object_ptr->create_object = reflection_objects_new;
  5996. reflection_property_ptr = register_class_ReflectionProperty(reflector_ptr);
  5997. reflection_property_ptr->create_object = reflection_objects_new;
  5998. REGISTER_REFLECTION_CLASS_CONST_LONG(property, "IS_STATIC", ZEND_ACC_STATIC);
  5999. REGISTER_REFLECTION_CLASS_CONST_LONG(property, "IS_READONLY", ZEND_ACC_READONLY);
  6000. REGISTER_REFLECTION_CLASS_CONST_LONG(property, "IS_PUBLIC", ZEND_ACC_PUBLIC);
  6001. REGISTER_REFLECTION_CLASS_CONST_LONG(property, "IS_PROTECTED", ZEND_ACC_PROTECTED);
  6002. REGISTER_REFLECTION_CLASS_CONST_LONG(property, "IS_PRIVATE", ZEND_ACC_PRIVATE);
  6003. reflection_class_constant_ptr = register_class_ReflectionClassConstant(reflector_ptr);
  6004. reflection_class_constant_ptr->create_object = reflection_objects_new;
  6005. REGISTER_REFLECTION_CLASS_CONST_LONG(class_constant, "IS_PUBLIC", ZEND_ACC_PUBLIC);
  6006. REGISTER_REFLECTION_CLASS_CONST_LONG(class_constant, "IS_PROTECTED", ZEND_ACC_PROTECTED);
  6007. REGISTER_REFLECTION_CLASS_CONST_LONG(class_constant, "IS_PRIVATE", ZEND_ACC_PRIVATE);
  6008. REGISTER_REFLECTION_CLASS_CONST_LONG(class_constant, "IS_FINAL", ZEND_ACC_FINAL);
  6009. reflection_extension_ptr = register_class_ReflectionExtension(reflector_ptr);
  6010. reflection_extension_ptr->create_object = reflection_objects_new;
  6011. reflection_zend_extension_ptr = register_class_ReflectionZendExtension(reflector_ptr);
  6012. reflection_zend_extension_ptr->create_object = reflection_objects_new;
  6013. reflection_reference_ptr = register_class_ReflectionReference();
  6014. reflection_reference_ptr->create_object = reflection_objects_new;
  6015. reflection_attribute_ptr = register_class_ReflectionAttribute(reflector_ptr);
  6016. reflection_attribute_ptr->create_object = reflection_objects_new;
  6017. reflection_enum_ptr = register_class_ReflectionEnum(reflection_class_ptr);
  6018. reflection_enum_ptr->create_object = reflection_objects_new;
  6019. reflection_enum_unit_case_ptr = register_class_ReflectionEnumUnitCase(reflection_class_constant_ptr);
  6020. reflection_enum_unit_case_ptr->create_object = reflection_objects_new;
  6021. reflection_enum_backed_case_ptr = register_class_ReflectionEnumBackedCase(reflection_enum_unit_case_ptr);
  6022. reflection_enum_backed_case_ptr->create_object = reflection_objects_new;
  6023. reflection_fiber_ptr = register_class_ReflectionFiber();
  6024. reflection_fiber_ptr->create_object = reflection_objects_new;
  6025. REGISTER_REFLECTION_CLASS_CONST_LONG(attribute, "IS_INSTANCEOF", REFLECTION_ATTRIBUTE_IS_INSTANCEOF);
  6026. REFLECTION_G(key_initialized) = 0;
  6027. return SUCCESS;
  6028. } /* }}} */
  6029. PHP_MINFO_FUNCTION(reflection) /* {{{ */
  6030. {
  6031. php_info_print_table_start();
  6032. php_info_print_table_row(2, "Reflection", "enabled");
  6033. php_info_print_table_end();
  6034. } /* }}} */
  6035. zend_module_entry reflection_module_entry = { /* {{{ */
  6036. STANDARD_MODULE_HEADER,
  6037. "Reflection",
  6038. NULL,
  6039. PHP_MINIT(reflection),
  6040. NULL,
  6041. NULL,
  6042. NULL,
  6043. PHP_MINFO(reflection),
  6044. PHP_REFLECTION_VERSION,
  6045. ZEND_MODULE_GLOBALS(reflection),
  6046. NULL,
  6047. NULL,
  6048. NULL,
  6049. STANDARD_MODULE_PROPERTIES_EX
  6050. }; /* }}} */