zend_object_handlers.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2016 Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Andi Gutmans <andi@zend.com> |
  16. | Zeev Suraski <zeev@zend.com> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* $Id$ */
  20. #include "zend.h"
  21. #include "zend_globals.h"
  22. #include "zend_variables.h"
  23. #include "zend_API.h"
  24. #include "zend_objects.h"
  25. #include "zend_objects_API.h"
  26. #include "zend_object_handlers.h"
  27. #include "zend_interfaces.h"
  28. #include "zend_closures.h"
  29. #include "zend_compile.h"
  30. #define DEBUG_OBJECT_HANDLERS 0
  31. #define Z_OBJ_P(zval_p) \
  32. ((zend_object*)(EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(zval_p)].bucket.obj.object))
  33. #define Z_OBJ_PROTECT_RECURSION(zval_p) \
  34. do { \
  35. if (EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(zval_p)].apply_count++ >= 3) { \
  36. zend_error(E_ERROR, "Nesting level too deep - recursive dependency?"); \
  37. } \
  38. } while (0)
  39. #define Z_OBJ_UNPROTECT_RECURSION(zval_p) \
  40. EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(zval_p)].apply_count--
  41. /*
  42. __X accessors explanation:
  43. if we have __get and property that is not part of the properties array is
  44. requested, we call __get handler. If it fails, we return uninitialized.
  45. if we have __set and property that is not part of the properties array is
  46. set, we call __set handler. If it fails, we do not change the array.
  47. for both handlers above, when we are inside __get/__set, no further calls for
  48. __get/__set for this property of this object will be made, to prevent endless
  49. recursion and enable accessors to change properties array.
  50. if we have __call and method which is not part of the class function table is
  51. called, we cal __call handler.
  52. */
  53. ZEND_API void rebuild_object_properties(zend_object *zobj) /* {{{ */
  54. {
  55. if (!zobj->properties) {
  56. HashPosition pos;
  57. zend_property_info *prop_info;
  58. zend_class_entry *ce = zobj->ce;
  59. ALLOC_HASHTABLE(zobj->properties);
  60. zend_hash_init(zobj->properties, 0, NULL, ZVAL_PTR_DTOR, 0);
  61. if (ce->default_properties_count) {
  62. for (zend_hash_internal_pointer_reset_ex(&ce->properties_info, &pos);
  63. zend_hash_get_current_data_ex(&ce->properties_info, (void**)&prop_info, &pos) == SUCCESS;
  64. zend_hash_move_forward_ex(&ce->properties_info, &pos)) {
  65. if (/*prop_info->ce == ce &&*/
  66. (prop_info->flags & ZEND_ACC_STATIC) == 0 &&
  67. prop_info->offset >= 0 &&
  68. zobj->properties_table[prop_info->offset]) {
  69. zend_hash_quick_add(zobj->properties, prop_info->name, prop_info->name_length+1, prop_info->h, (void**)&zobj->properties_table[prop_info->offset], sizeof(zval*), (void**)&zobj->properties_table[prop_info->offset]);
  70. }
  71. }
  72. while (ce->parent && ce->parent->default_properties_count) {
  73. ce = ce->parent;
  74. for (zend_hash_internal_pointer_reset_ex(&ce->properties_info, &pos);
  75. zend_hash_get_current_data_ex(&ce->properties_info, (void**)&prop_info, &pos) == SUCCESS;
  76. zend_hash_move_forward_ex(&ce->properties_info, &pos)) {
  77. if (prop_info->ce == ce &&
  78. (prop_info->flags & ZEND_ACC_STATIC) == 0 &&
  79. (prop_info->flags & ZEND_ACC_PRIVATE) != 0 &&
  80. prop_info->offset >= 0 &&
  81. zobj->properties_table[prop_info->offset]) {
  82. zend_hash_quick_add(zobj->properties, prop_info->name, prop_info->name_length+1, prop_info->h, (void**)&zobj->properties_table[prop_info->offset], sizeof(zval*), (void**)&zobj->properties_table[prop_info->offset]);
  83. }
  84. }
  85. }
  86. }
  87. }
  88. }
  89. /* }}} */
  90. ZEND_API HashTable *zend_std_get_properties(zval *object TSRMLS_DC) /* {{{ */
  91. {
  92. zend_object *zobj;
  93. zobj = Z_OBJ_P(object);
  94. if (!zobj->properties) {
  95. rebuild_object_properties(zobj);
  96. }
  97. return zobj->properties;
  98. }
  99. /* }}} */
  100. ZEND_API HashTable *zend_std_get_gc(zval *object, zval ***table, int *n TSRMLS_DC) /* {{{ */
  101. {
  102. if (Z_OBJ_HANDLER_P(object, get_properties) != zend_std_get_properties) {
  103. *table = NULL;
  104. *n = 0;
  105. return Z_OBJ_HANDLER_P(object, get_properties)(object TSRMLS_CC);
  106. } else {
  107. zend_object *zobj = Z_OBJ_P(object);
  108. if (zobj->properties) {
  109. *table = NULL;
  110. *n = 0;
  111. return zobj->properties;
  112. } else {
  113. *table = zobj->properties_table;
  114. *n = zobj->ce->default_properties_count;
  115. return NULL;
  116. }
  117. }
  118. }
  119. /* }}} */
  120. ZEND_API HashTable *zend_std_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
  121. {
  122. zend_class_entry *ce = Z_OBJCE_P(object);
  123. zval *retval = NULL;
  124. if (!ce->__debugInfo) {
  125. *is_temp = 0;
  126. return Z_OBJ_HANDLER_P(object, get_properties)
  127. ? Z_OBJ_HANDLER_P(object, get_properties)(object TSRMLS_CC)
  128. : NULL;
  129. }
  130. zend_call_method_with_0_params(&object, ce, &ce->__debugInfo, ZEND_DEBUGINFO_FUNC_NAME, &retval);
  131. if (retval && Z_TYPE_P(retval) == IS_ARRAY) {
  132. HashTable *ht = Z_ARRVAL_P(retval);
  133. if (Z_REFCOUNT_P(retval) <= 1) {
  134. *is_temp = 1;
  135. efree(retval);
  136. return ht;
  137. } else {
  138. *is_temp = 0;
  139. zval_ptr_dtor(&retval);
  140. }
  141. return ht;
  142. }
  143. if (retval && Z_TYPE_P(retval) == IS_NULL) {
  144. zval ret;
  145. array_init(&ret);
  146. *is_temp = 1;
  147. zval_ptr_dtor(&retval);
  148. return Z_ARRVAL(ret);
  149. }
  150. zend_error_noreturn(E_ERROR, ZEND_DEBUGINFO_FUNC_NAME "() must return an array");
  151. return NULL; /* Compilers are dumb and don't understand that noreturn means that the function does NOT need a return value... */
  152. }
  153. /* }}} */
  154. static zval *zend_std_call_getter(zval *object, zval *member TSRMLS_DC) /* {{{ */
  155. {
  156. zval *retval = NULL;
  157. zend_class_entry *ce = Z_OBJCE_P(object);
  158. /* __get handler is called with one argument:
  159. property name
  160. it should return whether the call was successfull or not
  161. */
  162. SEPARATE_ARG_IF_REF(member);
  163. zend_call_method_with_1_params(&object, ce, &ce->__get, ZEND_GET_FUNC_NAME, &retval, member);
  164. zval_ptr_dtor(&member);
  165. if (retval) {
  166. Z_DELREF_P(retval);
  167. }
  168. return retval;
  169. }
  170. /* }}} */
  171. static int zend_std_call_setter(zval *object, zval *member, zval *value TSRMLS_DC) /* {{{ */
  172. {
  173. zval *retval = NULL;
  174. int result;
  175. zend_class_entry *ce = Z_OBJCE_P(object);
  176. SEPARATE_ARG_IF_REF(member);
  177. Z_ADDREF_P(value);
  178. /* __set handler is called with two arguments:
  179. property name
  180. value to be set
  181. it should return whether the call was successfull or not
  182. */
  183. zend_call_method_with_2_params(&object, ce, &ce->__set, ZEND_SET_FUNC_NAME, &retval, member, value);
  184. zval_ptr_dtor(&member);
  185. zval_ptr_dtor(&value);
  186. if (retval) {
  187. result = i_zend_is_true(retval) ? SUCCESS : FAILURE;
  188. zval_ptr_dtor(&retval);
  189. return result;
  190. } else {
  191. return FAILURE;
  192. }
  193. }
  194. /* }}} */
  195. static void zend_std_call_unsetter(zval *object, zval *member TSRMLS_DC) /* {{{ */
  196. {
  197. zend_class_entry *ce = Z_OBJCE_P(object);
  198. /* __unset handler is called with one argument:
  199. property name
  200. */
  201. SEPARATE_ARG_IF_REF(member);
  202. zend_call_method_with_1_params(&object, ce, &ce->__unset, ZEND_UNSET_FUNC_NAME, NULL, member);
  203. zval_ptr_dtor(&member);
  204. }
  205. /* }}} */
  206. static zval *zend_std_call_issetter(zval *object, zval *member TSRMLS_DC) /* {{{ */
  207. {
  208. zval *retval = NULL;
  209. zend_class_entry *ce = Z_OBJCE_P(object);
  210. /* __isset handler is called with one argument:
  211. property name
  212. it should return whether the property is set or not
  213. */
  214. SEPARATE_ARG_IF_REF(member);
  215. zend_call_method_with_1_params(&object, ce, &ce->__isset, ZEND_ISSET_FUNC_NAME, &retval, member);
  216. zval_ptr_dtor(&member);
  217. return retval;
  218. }
  219. /* }}} */
  220. static zend_always_inline int zend_verify_property_access(zend_property_info *property_info, zend_class_entry *ce TSRMLS_DC) /* {{{ */
  221. {
  222. switch (property_info->flags & ZEND_ACC_PPP_MASK) {
  223. case ZEND_ACC_PUBLIC:
  224. return 1;
  225. case ZEND_ACC_PROTECTED:
  226. return zend_check_protected(property_info->ce, EG(scope));
  227. case ZEND_ACC_PRIVATE:
  228. if ((ce==EG(scope) || property_info->ce == EG(scope)) && EG(scope)) {
  229. return 1;
  230. } else {
  231. return 0;
  232. }
  233. break;
  234. }
  235. return 0;
  236. }
  237. /* }}} */
  238. static zend_always_inline zend_bool is_derived_class(zend_class_entry *child_class, zend_class_entry *parent_class) /* {{{ */
  239. {
  240. child_class = child_class->parent;
  241. while (child_class) {
  242. if (child_class == parent_class) {
  243. return 1;
  244. }
  245. child_class = child_class->parent;
  246. }
  247. return 0;
  248. }
  249. /* }}} */
  250. static zend_always_inline struct _zend_property_info *zend_get_property_info_quick(zend_class_entry *ce, zval *member, int silent, const zend_literal *key TSRMLS_DC) /* {{{ */
  251. {
  252. zend_property_info *property_info;
  253. zend_property_info *scope_property_info;
  254. zend_bool denied_access = 0;
  255. ulong h;
  256. if (key && (property_info = CACHED_POLYMORPHIC_PTR(key->cache_slot, ce)) != NULL) {
  257. return property_info;
  258. }
  259. if (UNEXPECTED(Z_STRVAL_P(member)[0] == '\0')) {
  260. if (!silent) {
  261. if (Z_STRLEN_P(member) == 0) {
  262. zend_error_noreturn(E_ERROR, "Cannot access empty property");
  263. } else {
  264. zend_error_noreturn(E_ERROR, "Cannot access property started with '\\0'");
  265. }
  266. }
  267. return NULL;
  268. }
  269. property_info = NULL;
  270. h = key ? key->hash_value : zend_get_hash_value(Z_STRVAL_P(member), Z_STRLEN_P(member) + 1);
  271. if (zend_hash_quick_find(&ce->properties_info, Z_STRVAL_P(member), Z_STRLEN_P(member)+1, h, (void **) &property_info)==SUCCESS) {
  272. if (UNEXPECTED((property_info->flags & ZEND_ACC_SHADOW) != 0)) {
  273. /* if it's a shadow - go to access it's private */
  274. property_info = NULL;
  275. } else {
  276. if (EXPECTED(zend_verify_property_access(property_info, ce TSRMLS_CC) != 0)) {
  277. if (EXPECTED((property_info->flags & ZEND_ACC_CHANGED) != 0)
  278. && EXPECTED(!(property_info->flags & ZEND_ACC_PRIVATE))) {
  279. /* We still need to make sure that we're not in a context
  280. * where the right property is a different 'statically linked' private
  281. * continue checking below...
  282. */
  283. } else {
  284. if (UNEXPECTED((property_info->flags & ZEND_ACC_STATIC) != 0) && !silent) {
  285. zend_error(E_STRICT, "Accessing static property %s::$%s as non static", ce->name, Z_STRVAL_P(member));
  286. }
  287. if (key) {
  288. CACHE_POLYMORPHIC_PTR(key->cache_slot, ce, property_info);
  289. }
  290. return property_info;
  291. }
  292. } else {
  293. /* Try to look in the scope instead */
  294. denied_access = 1;
  295. }
  296. }
  297. }
  298. if (EG(scope) != ce
  299. && EG(scope)
  300. && is_derived_class(ce, EG(scope))
  301. && zend_hash_quick_find(&EG(scope)->properties_info, Z_STRVAL_P(member), Z_STRLEN_P(member)+1, h, (void **) &scope_property_info)==SUCCESS
  302. && scope_property_info->flags & ZEND_ACC_PRIVATE) {
  303. if (key) {
  304. CACHE_POLYMORPHIC_PTR(key->cache_slot, ce, scope_property_info);
  305. }
  306. return scope_property_info;
  307. } else if (property_info) {
  308. if (UNEXPECTED(denied_access != 0)) {
  309. /* Information was available, but we were denied access. Error out. */
  310. if (!silent) {
  311. zend_error_noreturn(E_ERROR, "Cannot access %s property %s::$%s", zend_visibility_string(property_info->flags), ce->name, Z_STRVAL_P(member));
  312. }
  313. return NULL;
  314. } else {
  315. /* fall through, return property_info... */
  316. if (key) {
  317. CACHE_POLYMORPHIC_PTR(key->cache_slot, ce, property_info);
  318. }
  319. }
  320. } else {
  321. EG(std_property_info).flags = ZEND_ACC_PUBLIC;
  322. EG(std_property_info).name = Z_STRVAL_P(member);
  323. EG(std_property_info).name_length = Z_STRLEN_P(member);
  324. EG(std_property_info).h = h;
  325. EG(std_property_info).ce = ce;
  326. EG(std_property_info).offset = -1;
  327. property_info = &EG(std_property_info);
  328. }
  329. return property_info;
  330. }
  331. /* }}} */
  332. ZEND_API struct _zend_property_info *zend_get_property_info(zend_class_entry *ce, zval *member, int silent TSRMLS_DC) /* {{{ */
  333. {
  334. return zend_get_property_info_quick(ce, member, silent, NULL TSRMLS_CC);
  335. }
  336. /* }}} */
  337. ZEND_API int zend_check_property_access(zend_object *zobj, const char *prop_info_name, int prop_info_name_len TSRMLS_DC) /* {{{ */
  338. {
  339. zend_property_info *property_info;
  340. const char *class_name, *prop_name;
  341. zval member;
  342. int prop_name_len;
  343. zend_unmangle_property_name_ex(prop_info_name, prop_info_name_len, &class_name, &prop_name, &prop_name_len);
  344. ZVAL_STRINGL(&member, prop_name, prop_name_len, 0);
  345. property_info = zend_get_property_info_quick(zobj->ce, &member, 1, NULL TSRMLS_CC);
  346. if (!property_info) {
  347. return FAILURE;
  348. }
  349. if (class_name && class_name[0] != '*') {
  350. if (!(property_info->flags & ZEND_ACC_PRIVATE)) {
  351. /* we we're looking for a private prop but found a non private one of the same name */
  352. return FAILURE;
  353. } else if (strcmp(prop_info_name+1, property_info->name+1)) {
  354. /* we we're looking for a private prop but found a private one of the same name but another class */
  355. return FAILURE;
  356. }
  357. }
  358. return zend_verify_property_access(property_info, zobj->ce TSRMLS_CC) ? SUCCESS : FAILURE;
  359. }
  360. /* }}} */
  361. static int zend_get_property_guard(zend_object *zobj, zend_property_info *property_info, zval *member, zend_guard **pguard) /* {{{ */
  362. {
  363. zend_property_info info;
  364. zend_guard stub;
  365. if (!property_info) {
  366. property_info = &info;
  367. info.name = Z_STRVAL_P(member);
  368. info.name_length = Z_STRLEN_P(member);
  369. info.h = zend_get_hash_value(Z_STRVAL_P(member), Z_STRLEN_P(member) + 1);
  370. } else if(property_info->name[0] == '\0'){
  371. const char *class_name = NULL, *prop_name = NULL;
  372. zend_unmangle_property_name(property_info->name, property_info->name_length, &class_name, &prop_name);
  373. if(class_name) {
  374. /* use unmangled name for protected properties */
  375. info.name = prop_name;
  376. info.name_length = strlen(prop_name);
  377. info.h = zend_get_hash_value(info.name, info.name_length+1);
  378. property_info = &info;
  379. }
  380. }
  381. if (!zobj->guards) {
  382. ALLOC_HASHTABLE(zobj->guards);
  383. zend_hash_init(zobj->guards, 0, NULL, NULL, 0);
  384. } else if (zend_hash_quick_find(zobj->guards, property_info->name, property_info->name_length+1, property_info->h, (void **) pguard) == SUCCESS) {
  385. return SUCCESS;
  386. }
  387. stub.in_get = 0;
  388. stub.in_set = 0;
  389. stub.in_unset = 0;
  390. stub.in_isset = 0;
  391. return zend_hash_quick_add(zobj->guards, property_info->name, property_info->name_length+1, property_info->h, (void**)&stub, sizeof(stub), (void**) pguard);
  392. }
  393. /* }}} */
  394. zval *zend_std_read_property(zval *object, zval *member, int type, const zend_literal *key TSRMLS_DC) /* {{{ */
  395. {
  396. zend_object *zobj;
  397. zval *tmp_member = NULL;
  398. zval **retval;
  399. zval *rv = NULL;
  400. zend_property_info *property_info;
  401. int silent;
  402. silent = (type == BP_VAR_IS);
  403. zobj = Z_OBJ_P(object);
  404. if (UNEXPECTED(Z_TYPE_P(member) != IS_STRING)) {
  405. ALLOC_ZVAL(tmp_member);
  406. *tmp_member = *member;
  407. INIT_PZVAL(tmp_member);
  408. zval_copy_ctor(tmp_member);
  409. convert_to_string(tmp_member);
  410. member = tmp_member;
  411. key = NULL;
  412. }
  413. #if DEBUG_OBJECT_HANDLERS
  414. fprintf(stderr, "Read object #%d property: %s\n", Z_OBJ_HANDLE_P(object), Z_STRVAL_P(member));
  415. #endif
  416. /* make zend_get_property_info silent if we have getter - we may want to use it */
  417. property_info = zend_get_property_info_quick(zobj->ce, member, silent || (zobj->ce->__get != NULL), key TSRMLS_CC);
  418. if (UNEXPECTED(!property_info) ||
  419. ((EXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0) &&
  420. property_info->offset >= 0) ?
  421. (zobj->properties ?
  422. ((retval = (zval**)zobj->properties_table[property_info->offset]) == NULL) :
  423. (*(retval = &zobj->properties_table[property_info->offset]) == NULL)) :
  424. (UNEXPECTED(!zobj->properties) ||
  425. UNEXPECTED(zend_hash_quick_find(zobj->properties, property_info->name, property_info->name_length+1, property_info->h, (void **) &retval) == FAILURE)))) {
  426. zend_guard *guard = NULL;
  427. if (zobj->ce->__get &&
  428. zend_get_property_guard(zobj, property_info, member, &guard) == SUCCESS &&
  429. !guard->in_get) {
  430. /* have getter - try with it! */
  431. Z_ADDREF_P(object);
  432. if (PZVAL_IS_REF(object)) {
  433. SEPARATE_ZVAL(&object);
  434. }
  435. guard->in_get = 1; /* prevent circular getting */
  436. rv = zend_std_call_getter(object, member TSRMLS_CC);
  437. guard->in_get = 0;
  438. if (rv) {
  439. retval = &rv;
  440. if (!Z_ISREF_P(rv) &&
  441. (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET)) {
  442. if (Z_REFCOUNT_P(rv) > 0) {
  443. zval *tmp = rv;
  444. ALLOC_ZVAL(rv);
  445. *rv = *tmp;
  446. zval_copy_ctor(rv);
  447. Z_UNSET_ISREF_P(rv);
  448. Z_SET_REFCOUNT_P(rv, 0);
  449. }
  450. if (UNEXPECTED(Z_TYPE_P(rv) != IS_OBJECT)) {
  451. zend_error(E_NOTICE, "Indirect modification of overloaded property %s::$%s has no effect", zobj->ce->name, Z_STRVAL_P(member));
  452. }
  453. }
  454. } else {
  455. retval = &EG(uninitialized_zval_ptr);
  456. }
  457. if (EXPECTED(*retval != object)) {
  458. zval_ptr_dtor(&object);
  459. } else {
  460. Z_DELREF_P(object);
  461. }
  462. } else {
  463. if (zobj->ce->__get && guard && guard->in_get == 1) {
  464. if (Z_STRVAL_P(member)[0] == '\0') {
  465. if (Z_STRLEN_P(member) == 0) {
  466. zend_error(E_ERROR, "Cannot access empty property");
  467. } else {
  468. zend_error(E_ERROR, "Cannot access property started with '\\0'");
  469. }
  470. }
  471. }
  472. if (!silent) {
  473. zend_error(E_NOTICE,"Undefined property: %s::$%s", zobj->ce->name, Z_STRVAL_P(member));
  474. }
  475. retval = &EG(uninitialized_zval_ptr);
  476. }
  477. }
  478. if (UNEXPECTED(tmp_member != NULL)) {
  479. Z_ADDREF_PP(retval);
  480. zval_ptr_dtor(&tmp_member);
  481. Z_DELREF_PP(retval);
  482. }
  483. return *retval;
  484. }
  485. /* }}} */
  486. ZEND_API void zend_std_write_property(zval *object, zval *member, zval *value, const zend_literal *key TSRMLS_DC) /* {{{ */
  487. {
  488. zend_object *zobj;
  489. zval *tmp_member = NULL;
  490. zval **variable_ptr;
  491. zend_property_info *property_info;
  492. zobj = Z_OBJ_P(object);
  493. if (UNEXPECTED(Z_TYPE_P(member) != IS_STRING)) {
  494. ALLOC_ZVAL(tmp_member);
  495. *tmp_member = *member;
  496. INIT_PZVAL(tmp_member);
  497. zval_copy_ctor(tmp_member);
  498. convert_to_string(tmp_member);
  499. member = tmp_member;
  500. key = NULL;
  501. }
  502. property_info = zend_get_property_info_quick(zobj->ce, member, (zobj->ce->__set != NULL), key TSRMLS_CC);
  503. if (EXPECTED(property_info != NULL) &&
  504. ((EXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0) &&
  505. property_info->offset >= 0) ?
  506. (zobj->properties ?
  507. ((variable_ptr = (zval**)zobj->properties_table[property_info->offset]) != NULL) :
  508. (*(variable_ptr = &zobj->properties_table[property_info->offset]) != NULL)) :
  509. (EXPECTED(zobj->properties != NULL) &&
  510. EXPECTED(zend_hash_quick_find(zobj->properties, property_info->name, property_info->name_length+1, property_info->h, (void **) &variable_ptr) == SUCCESS)))) {
  511. /* if we already have this value there, we don't actually need to do anything */
  512. if (EXPECTED(*variable_ptr != value)) {
  513. /* if we are assigning reference, we shouldn't move it, but instead assign variable
  514. to the same pointer */
  515. if (PZVAL_IS_REF(*variable_ptr)) {
  516. zval garbage = **variable_ptr; /* old value should be destroyed */
  517. /* To check: can't *variable_ptr be some system variable like error_zval here? */
  518. Z_TYPE_PP(variable_ptr) = Z_TYPE_P(value);
  519. (*variable_ptr)->value = value->value;
  520. if (Z_REFCOUNT_P(value) > 0) {
  521. zval_copy_ctor(*variable_ptr);
  522. } else {
  523. efree(value);
  524. }
  525. zval_dtor(&garbage);
  526. } else {
  527. zval *garbage = *variable_ptr;
  528. /* if we assign referenced variable, we should separate it */
  529. Z_ADDREF_P(value);
  530. if (PZVAL_IS_REF(value)) {
  531. SEPARATE_ZVAL(&value);
  532. }
  533. *variable_ptr = value;
  534. zval_ptr_dtor(&garbage);
  535. }
  536. }
  537. } else {
  538. zend_guard *guard = NULL;
  539. if (zobj->ce->__set &&
  540. zend_get_property_guard(zobj, property_info, member, &guard) == SUCCESS &&
  541. !guard->in_set) {
  542. Z_ADDREF_P(object);
  543. if (PZVAL_IS_REF(object)) {
  544. SEPARATE_ZVAL(&object);
  545. }
  546. guard->in_set = 1; /* prevent circular setting */
  547. if (zend_std_call_setter(object, member, value TSRMLS_CC) != SUCCESS) {
  548. /* for now, just ignore it - __set should take care of warnings, etc. */
  549. }
  550. guard->in_set = 0;
  551. zval_ptr_dtor(&object);
  552. } else if (EXPECTED(property_info != NULL)) {
  553. /* if we assign referenced variable, we should separate it */
  554. Z_ADDREF_P(value);
  555. if (PZVAL_IS_REF(value)) {
  556. SEPARATE_ZVAL(&value);
  557. }
  558. if (EXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0) &&
  559. property_info->offset >= 0) {
  560. if (!zobj->properties) {
  561. zobj->properties_table[property_info->offset] = value;
  562. } else if (zobj->properties_table[property_info->offset]) {
  563. *(zval**)zobj->properties_table[property_info->offset] = value;
  564. } else {
  565. zend_hash_quick_update(zobj->properties, property_info->name, property_info->name_length+1, property_info->h, &value, sizeof(zval *), (void**)&zobj->properties_table[property_info->offset]);
  566. }
  567. } else {
  568. if (!zobj->properties) {
  569. rebuild_object_properties(zobj);
  570. }
  571. zend_hash_quick_update(zobj->properties, property_info->name, property_info->name_length+1, property_info->h, &value, sizeof(zval *), NULL);
  572. }
  573. } else if (zobj->ce->__set && guard && guard->in_set == 1) {
  574. if (Z_STRVAL_P(member)[0] == '\0') {
  575. if (Z_STRLEN_P(member) == 0) {
  576. zend_error(E_ERROR, "Cannot access empty property");
  577. } else {
  578. zend_error(E_ERROR, "Cannot access property started with '\\0'");
  579. }
  580. }
  581. }
  582. }
  583. if (UNEXPECTED(tmp_member != NULL)) {
  584. zval_ptr_dtor(&tmp_member);
  585. }
  586. }
  587. /* }}} */
  588. zval *zend_std_read_dimension(zval *object, zval *offset, int type TSRMLS_DC) /* {{{ */
  589. {
  590. zend_class_entry *ce = Z_OBJCE_P(object);
  591. zval *retval;
  592. if (EXPECTED(instanceof_function_ex(ce, zend_ce_arrayaccess, 1 TSRMLS_CC) != 0)) {
  593. if(offset == NULL) {
  594. /* [] construct */
  595. ALLOC_INIT_ZVAL(offset);
  596. } else {
  597. SEPARATE_ARG_IF_REF(offset);
  598. }
  599. zend_call_method_with_1_params(&object, ce, NULL, "offsetget", &retval, offset);
  600. zval_ptr_dtor(&offset);
  601. if (UNEXPECTED(!retval)) {
  602. if (UNEXPECTED(!EG(exception))) {
  603. zend_error_noreturn(E_ERROR, "Undefined offset for object of type %s used as array", ce->name);
  604. }
  605. return 0;
  606. }
  607. /* Undo PZVAL_LOCK() */
  608. Z_DELREF_P(retval);
  609. return retval;
  610. } else {
  611. zend_error_noreturn(E_ERROR, "Cannot use object of type %s as array", ce->name);
  612. return 0;
  613. }
  614. }
  615. /* }}} */
  616. static void zend_std_write_dimension(zval *object, zval *offset, zval *value TSRMLS_DC) /* {{{ */
  617. {
  618. zend_class_entry *ce = Z_OBJCE_P(object);
  619. if (EXPECTED(instanceof_function_ex(ce, zend_ce_arrayaccess, 1 TSRMLS_CC) != 0)) {
  620. if (!offset) {
  621. ALLOC_INIT_ZVAL(offset);
  622. } else {
  623. SEPARATE_ARG_IF_REF(offset);
  624. }
  625. zend_call_method_with_2_params(&object, ce, NULL, "offsetset", NULL, offset, value);
  626. zval_ptr_dtor(&offset);
  627. } else {
  628. zend_error_noreturn(E_ERROR, "Cannot use object of type %s as array", ce->name);
  629. }
  630. }
  631. /* }}} */
  632. static int zend_std_has_dimension(zval *object, zval *offset, int check_empty TSRMLS_DC) /* {{{ */
  633. {
  634. zend_class_entry *ce = Z_OBJCE_P(object);
  635. zval *retval;
  636. int result;
  637. if (EXPECTED(instanceof_function_ex(ce, zend_ce_arrayaccess, 1 TSRMLS_CC) != 0)) {
  638. SEPARATE_ARG_IF_REF(offset);
  639. zend_call_method_with_1_params(&object, ce, NULL, "offsetexists", &retval, offset);
  640. if (EXPECTED(retval != NULL)) {
  641. result = i_zend_is_true(retval);
  642. zval_ptr_dtor(&retval);
  643. if (check_empty && result && EXPECTED(!EG(exception))) {
  644. zend_call_method_with_1_params(&object, ce, NULL, "offsetget", &retval, offset);
  645. if (retval) {
  646. result = i_zend_is_true(retval);
  647. zval_ptr_dtor(&retval);
  648. }
  649. }
  650. } else {
  651. result = 0;
  652. }
  653. zval_ptr_dtor(&offset);
  654. } else {
  655. zend_error_noreturn(E_ERROR, "Cannot use object of type %s as array", ce->name);
  656. return 0;
  657. }
  658. return result;
  659. }
  660. /* }}} */
  661. static zval **zend_std_get_property_ptr_ptr(zval *object, zval *member, int type, const zend_literal *key TSRMLS_DC) /* {{{ */
  662. {
  663. zend_object *zobj;
  664. zval tmp_member;
  665. zval **retval;
  666. zend_property_info *property_info;
  667. zobj = Z_OBJ_P(object);
  668. if (UNEXPECTED(Z_TYPE_P(member) != IS_STRING)) {
  669. tmp_member = *member;
  670. zval_copy_ctor(&tmp_member);
  671. convert_to_string(&tmp_member);
  672. member = &tmp_member;
  673. key = NULL;
  674. }
  675. #if DEBUG_OBJECT_HANDLERS
  676. fprintf(stderr, "Ptr object #%d property: %s\n", Z_OBJ_HANDLE_P(object), Z_STRVAL_P(member));
  677. #endif
  678. property_info = zend_get_property_info_quick(zobj->ce, member, (zobj->ce->__get != NULL), key TSRMLS_CC);
  679. if (UNEXPECTED(!property_info) ||
  680. ((EXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0) &&
  681. property_info->offset >= 0) ?
  682. (zobj->properties ?
  683. ((retval = (zval**)zobj->properties_table[property_info->offset]) == NULL) :
  684. (*(retval = &zobj->properties_table[property_info->offset]) == NULL)) :
  685. (UNEXPECTED(!zobj->properties) ||
  686. UNEXPECTED(zend_hash_quick_find(zobj->properties, property_info->name, property_info->name_length+1, property_info->h, (void **) &retval) == FAILURE)))) {
  687. zval *new_zval;
  688. zend_guard *guard;
  689. if (!zobj->ce->__get ||
  690. zend_get_property_guard(zobj, property_info, member, &guard) != SUCCESS ||
  691. (property_info && guard->in_get)) {
  692. /* we don't have access controls - will just add it */
  693. new_zval = &EG(uninitialized_zval);
  694. Z_ADDREF_P(new_zval);
  695. if (EXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0) &&
  696. property_info->offset >= 0) {
  697. if (!zobj->properties) {
  698. zobj->properties_table[property_info->offset] = new_zval;
  699. retval = &zobj->properties_table[property_info->offset];
  700. } else if (zobj->properties_table[property_info->offset]) {
  701. *(zval**)zobj->properties_table[property_info->offset] = new_zval;
  702. retval = (zval**)zobj->properties_table[property_info->offset];
  703. } else {
  704. zend_hash_quick_update(zobj->properties, property_info->name, property_info->name_length+1, property_info->h, &new_zval, sizeof(zval *), (void**)&zobj->properties_table[property_info->offset]);
  705. retval = (zval**)zobj->properties_table[property_info->offset];
  706. }
  707. } else {
  708. if (!zobj->properties) {
  709. rebuild_object_properties(zobj);
  710. }
  711. zend_hash_quick_update(zobj->properties, property_info->name, property_info->name_length+1, property_info->h, &new_zval, sizeof(zval *), (void **) &retval);
  712. }
  713. /* Notice is thrown after creation of the property, to avoid EG(std_property_info)
  714. * being overwritten in an error handler. */
  715. if (UNEXPECTED(type == BP_VAR_RW || type == BP_VAR_R)) {
  716. zend_error(E_NOTICE, "Undefined property: %s::$%s", zobj->ce->name, Z_STRVAL_P(member));
  717. }
  718. } else {
  719. /* we do have getter - fail and let it try again with usual get/set */
  720. retval = NULL;
  721. }
  722. }
  723. if (UNEXPECTED(member == &tmp_member)) {
  724. zval_dtor(member);
  725. }
  726. return retval;
  727. }
  728. /* }}} */
  729. static void zend_std_unset_property(zval *object, zval *member, const zend_literal *key TSRMLS_DC) /* {{{ */
  730. {
  731. zend_object *zobj;
  732. zval *tmp_member = NULL;
  733. zend_property_info *property_info;
  734. zobj = Z_OBJ_P(object);
  735. if (UNEXPECTED(Z_TYPE_P(member) != IS_STRING)) {
  736. ALLOC_ZVAL(tmp_member);
  737. *tmp_member = *member;
  738. INIT_PZVAL(tmp_member);
  739. zval_copy_ctor(tmp_member);
  740. convert_to_string(tmp_member);
  741. member = tmp_member;
  742. key = NULL;
  743. }
  744. property_info = zend_get_property_info_quick(zobj->ce, member, (zobj->ce->__unset != NULL), key TSRMLS_CC);
  745. if (EXPECTED(property_info != NULL) &&
  746. EXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0) &&
  747. !zobj->properties &&
  748. property_info->offset >= 0 &&
  749. EXPECTED(zobj->properties_table[property_info->offset] != NULL)) {
  750. zval_ptr_dtor(&zobj->properties_table[property_info->offset]);
  751. zobj->properties_table[property_info->offset] = NULL;
  752. } else if (UNEXPECTED(!property_info) ||
  753. !zobj->properties ||
  754. UNEXPECTED(zend_hash_quick_del(zobj->properties, property_info->name, property_info->name_length+1, property_info->h) == FAILURE)) {
  755. zend_guard *guard = NULL;
  756. if (zobj->ce->__unset &&
  757. zend_get_property_guard(zobj, property_info, member, &guard) == SUCCESS &&
  758. !guard->in_unset) {
  759. /* have unseter - try with it! */
  760. Z_ADDREF_P(object);
  761. if (PZVAL_IS_REF(object)) {
  762. SEPARATE_ZVAL(&object);
  763. }
  764. guard->in_unset = 1; /* prevent circular unsetting */
  765. zend_std_call_unsetter(object, member TSRMLS_CC);
  766. guard->in_unset = 0;
  767. zval_ptr_dtor(&object);
  768. } else if (zobj->ce->__unset && guard && guard->in_unset == 1) {
  769. if (Z_STRVAL_P(member)[0] == '\0') {
  770. if (Z_STRLEN_P(member) == 0) {
  771. zend_error(E_ERROR, "Cannot access empty property");
  772. } else {
  773. zend_error(E_ERROR, "Cannot access property started with '\\0'");
  774. }
  775. }
  776. }
  777. } else if (EXPECTED(property_info != NULL) &&
  778. EXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0) &&
  779. property_info->offset >= 0) {
  780. zobj->properties_table[property_info->offset] = NULL;
  781. }
  782. if (UNEXPECTED(tmp_member != NULL)) {
  783. zval_ptr_dtor(&tmp_member);
  784. }
  785. }
  786. /* }}} */
  787. static void zend_std_unset_dimension(zval *object, zval *offset TSRMLS_DC) /* {{{ */
  788. {
  789. zend_class_entry *ce = Z_OBJCE_P(object);
  790. if (instanceof_function_ex(ce, zend_ce_arrayaccess, 1 TSRMLS_CC)) {
  791. SEPARATE_ARG_IF_REF(offset);
  792. zend_call_method_with_1_params(&object, ce, NULL, "offsetunset", NULL, offset);
  793. zval_ptr_dtor(&offset);
  794. } else {
  795. zend_error_noreturn(E_ERROR, "Cannot use object of type %s as array", ce->name);
  796. }
  797. }
  798. /* }}} */
  799. ZEND_API void zend_std_call_user_call(INTERNAL_FUNCTION_PARAMETERS) /* {{{ */
  800. {
  801. zend_internal_function *func = (zend_internal_function *)EG(current_execute_data)->function_state.function;
  802. zval *method_name_ptr, *method_args_ptr;
  803. zval *method_result_ptr = NULL;
  804. zend_class_entry *ce = Z_OBJCE_P(this_ptr);
  805. ALLOC_ZVAL(method_args_ptr);
  806. INIT_PZVAL(method_args_ptr);
  807. array_init_size(method_args_ptr, ZEND_NUM_ARGS());
  808. if (UNEXPECTED(zend_copy_parameters_array(ZEND_NUM_ARGS(), method_args_ptr TSRMLS_CC) == FAILURE)) {
  809. zval_dtor(method_args_ptr);
  810. zend_error_noreturn(E_ERROR, "Cannot get arguments for __call");
  811. RETURN_FALSE;
  812. }
  813. ALLOC_ZVAL(method_name_ptr);
  814. INIT_PZVAL(method_name_ptr);
  815. ZVAL_STRING(method_name_ptr, func->function_name, 0); /* no dup - it's a copy */
  816. /* __call handler is called with two arguments:
  817. method name
  818. array of method parameters
  819. */
  820. zend_call_method_with_2_params(&this_ptr, ce, &ce->__call, ZEND_CALL_FUNC_NAME, &method_result_ptr, method_name_ptr, method_args_ptr);
  821. if (method_result_ptr) {
  822. RETVAL_ZVAL_FAST(method_result_ptr);
  823. zval_ptr_dtor(&method_result_ptr);
  824. }
  825. /* now destruct all auxiliaries */
  826. zval_ptr_dtor(&method_args_ptr);
  827. zval_ptr_dtor(&method_name_ptr);
  828. /* destruct the function also, then - we have allocated it in get_method */
  829. efree(func);
  830. }
  831. /* }}} */
  832. /* Ensures that we're allowed to call a private method.
  833. * Returns the function address that should be called, or NULL
  834. * if no such function exists.
  835. */
  836. static inline zend_function *zend_check_private_int(zend_function *fbc, zend_class_entry *ce, char *function_name_strval, int function_name_strlen, ulong hash_value TSRMLS_DC) /* {{{ */
  837. {
  838. if (!ce) {
  839. return 0;
  840. }
  841. /* We may call a private function if:
  842. * 1. The class of our object is the same as the scope, and the private
  843. * function (EX(fbc)) has the same scope.
  844. * 2. One of our parent classes are the same as the scope, and it contains
  845. * a private function with the same name that has the same scope.
  846. */
  847. if (fbc->common.scope == ce && EG(scope) == ce) {
  848. /* rule #1 checks out ok, allow the function call */
  849. return fbc;
  850. }
  851. /* Check rule #2 */
  852. ce = ce->parent;
  853. while (ce) {
  854. if (ce == EG(scope)) {
  855. if (zend_hash_quick_find(&ce->function_table, function_name_strval, function_name_strlen+1, hash_value, (void **) &fbc)==SUCCESS
  856. && fbc->op_array.fn_flags & ZEND_ACC_PRIVATE
  857. && fbc->common.scope == EG(scope)) {
  858. return fbc;
  859. }
  860. break;
  861. }
  862. ce = ce->parent;
  863. }
  864. return NULL;
  865. }
  866. /* }}} */
  867. ZEND_API int zend_check_private(zend_function *fbc, zend_class_entry *ce, char *function_name_strval, int function_name_strlen TSRMLS_DC) /* {{{ */
  868. {
  869. return zend_check_private_int(fbc, ce, function_name_strval, function_name_strlen, zend_hash_func(function_name_strval, function_name_strlen+1) TSRMLS_CC) != NULL;
  870. }
  871. /* }}} */
  872. /* Ensures that we're allowed to call a protected method.
  873. */
  874. ZEND_API int zend_check_protected(zend_class_entry *ce, zend_class_entry *scope) /* {{{ */
  875. {
  876. zend_class_entry *fbc_scope = ce;
  877. /* Is the context that's calling the function, the same as one of
  878. * the function's parents?
  879. */
  880. while (fbc_scope) {
  881. if (fbc_scope==scope) {
  882. return 1;
  883. }
  884. fbc_scope = fbc_scope->parent;
  885. }
  886. /* Is the function's scope the same as our current object context,
  887. * or any of the parents of our context?
  888. */
  889. while (scope) {
  890. if (scope==ce) {
  891. return 1;
  892. }
  893. scope = scope->parent;
  894. }
  895. return 0;
  896. }
  897. /* }}} */
  898. static inline union _zend_function *zend_get_user_call_function(zend_class_entry *ce, const char *method_name, int method_len) /* {{{ */
  899. {
  900. zend_internal_function *call_user_call = emalloc(sizeof(zend_internal_function));
  901. call_user_call->type = ZEND_INTERNAL_FUNCTION;
  902. call_user_call->module = (ce->type == ZEND_INTERNAL_CLASS) ? ce->info.internal.module : NULL;
  903. call_user_call->handler = zend_std_call_user_call;
  904. call_user_call->arg_info = NULL;
  905. call_user_call->num_args = 0;
  906. call_user_call->scope = ce;
  907. call_user_call->fn_flags = ZEND_ACC_CALL_VIA_HANDLER;
  908. call_user_call->function_name = estrndup(method_name, method_len);
  909. return (union _zend_function *)call_user_call;
  910. }
  911. /* }}} */
  912. static union _zend_function *zend_std_get_method(zval **object_ptr, char *method_name, int method_len, const zend_literal *key TSRMLS_DC) /* {{{ */
  913. {
  914. zend_function *fbc;
  915. zval *object = *object_ptr;
  916. zend_object *zobj = Z_OBJ_P(object);
  917. ulong hash_value;
  918. char *lc_method_name;
  919. ALLOCA_FLAG(use_heap)
  920. if (EXPECTED(key != NULL)) {
  921. lc_method_name = Z_STRVAL(key->constant);
  922. hash_value = key->hash_value;
  923. } else {
  924. lc_method_name = do_alloca(method_len+1, use_heap);
  925. /* Create a zend_copy_str_tolower(dest, src, src_length); */
  926. zend_str_tolower_copy(lc_method_name, method_name, method_len);
  927. hash_value = zend_hash_func(lc_method_name, method_len+1);
  928. }
  929. if (UNEXPECTED(zend_hash_quick_find(&zobj->ce->function_table, lc_method_name, method_len+1, hash_value, (void **)&fbc) == FAILURE)) {
  930. if (UNEXPECTED(!key)) {
  931. free_alloca(lc_method_name, use_heap);
  932. }
  933. if (zobj->ce->__call) {
  934. return zend_get_user_call_function(zobj->ce, method_name, method_len);
  935. } else {
  936. return NULL;
  937. }
  938. }
  939. /* Check access level */
  940. if (fbc->op_array.fn_flags & ZEND_ACC_PRIVATE) {
  941. zend_function *updated_fbc;
  942. /* Ensure that if we're calling a private function, we're allowed to do so.
  943. * If we're not and __call() handler exists, invoke it, otherwise error out.
  944. */
  945. updated_fbc = zend_check_private_int(fbc, Z_OBJ_HANDLER_P(object, get_class_entry)(object TSRMLS_CC), lc_method_name, method_len, hash_value TSRMLS_CC);
  946. if (EXPECTED(updated_fbc != NULL)) {
  947. fbc = updated_fbc;
  948. } else {
  949. if (zobj->ce->__call) {
  950. fbc = zend_get_user_call_function(zobj->ce, method_name, method_len);
  951. } else {
  952. zend_error_noreturn(E_ERROR, "Call to %s method %s::%s() from context '%s'", zend_visibility_string(fbc->common.fn_flags), ZEND_FN_SCOPE_NAME(fbc), method_name, EG(scope) ? EG(scope)->name : "");
  953. }
  954. }
  955. } else {
  956. /* Ensure that we haven't overridden a private function and end up calling
  957. * the overriding public function...
  958. */
  959. if (EG(scope) &&
  960. is_derived_class(fbc->common.scope, EG(scope)) &&
  961. fbc->op_array.fn_flags & ZEND_ACC_CHANGED) {
  962. zend_function *priv_fbc;
  963. if (zend_hash_quick_find(&EG(scope)->function_table, lc_method_name, method_len+1, hash_value, (void **) &priv_fbc)==SUCCESS
  964. && priv_fbc->common.fn_flags & ZEND_ACC_PRIVATE
  965. && priv_fbc->common.scope == EG(scope)) {
  966. fbc = priv_fbc;
  967. }
  968. }
  969. if ((fbc->common.fn_flags & ZEND_ACC_PROTECTED)) {
  970. /* Ensure that if we're calling a protected function, we're allowed to do so.
  971. * If we're not and __call() handler exists, invoke it, otherwise error out.
  972. */
  973. if (UNEXPECTED(!zend_check_protected(zend_get_function_root_class(fbc), EG(scope)))) {
  974. if (zobj->ce->__call) {
  975. fbc = zend_get_user_call_function(zobj->ce, method_name, method_len);
  976. } else {
  977. zend_error_noreturn(E_ERROR, "Call to %s method %s::%s() from context '%s'", zend_visibility_string(fbc->common.fn_flags), ZEND_FN_SCOPE_NAME(fbc), method_name, EG(scope) ? EG(scope)->name : "");
  978. }
  979. }
  980. }
  981. }
  982. if (UNEXPECTED(!key)) {
  983. free_alloca(lc_method_name, use_heap);
  984. }
  985. return fbc;
  986. }
  987. /* }}} */
  988. ZEND_API void zend_std_callstatic_user_call(INTERNAL_FUNCTION_PARAMETERS) /* {{{ */
  989. {
  990. zend_internal_function *func = (zend_internal_function *)EG(current_execute_data)->function_state.function;
  991. zval *method_name_ptr, *method_args_ptr;
  992. zval *method_result_ptr = NULL;
  993. zend_class_entry *ce = EG(scope);
  994. ALLOC_ZVAL(method_args_ptr);
  995. INIT_PZVAL(method_args_ptr);
  996. array_init_size(method_args_ptr, ZEND_NUM_ARGS());
  997. if (UNEXPECTED(zend_copy_parameters_array(ZEND_NUM_ARGS(), method_args_ptr TSRMLS_CC) == FAILURE)) {
  998. zval_dtor(method_args_ptr);
  999. zend_error_noreturn(E_ERROR, "Cannot get arguments for " ZEND_CALLSTATIC_FUNC_NAME);
  1000. RETURN_FALSE;
  1001. }
  1002. ALLOC_ZVAL(method_name_ptr);
  1003. INIT_PZVAL(method_name_ptr);
  1004. ZVAL_STRING(method_name_ptr, func->function_name, 0); /* no dup - it's a copy */
  1005. /* __callStatic handler is called with two arguments:
  1006. method name
  1007. array of method parameters
  1008. */
  1009. zend_call_method_with_2_params(NULL, ce, &ce->__callstatic, ZEND_CALLSTATIC_FUNC_NAME, &method_result_ptr, method_name_ptr, method_args_ptr);
  1010. if (method_result_ptr) {
  1011. RETVAL_ZVAL_FAST(method_result_ptr);
  1012. zval_ptr_dtor(&method_result_ptr);
  1013. }
  1014. /* now destruct all auxiliaries */
  1015. zval_ptr_dtor(&method_args_ptr);
  1016. zval_ptr_dtor(&method_name_ptr);
  1017. /* destruct the function also, then - we have allocated it in get_method */
  1018. efree(func);
  1019. }
  1020. /* }}} */
  1021. static inline union _zend_function *zend_get_user_callstatic_function(zend_class_entry *ce, const char *method_name, int method_len) /* {{{ */
  1022. {
  1023. zend_internal_function *callstatic_user_call = emalloc(sizeof(zend_internal_function));
  1024. callstatic_user_call->type = ZEND_INTERNAL_FUNCTION;
  1025. callstatic_user_call->module = (ce->type == ZEND_INTERNAL_CLASS) ? ce->info.internal.module : NULL;
  1026. callstatic_user_call->handler = zend_std_callstatic_user_call;
  1027. callstatic_user_call->arg_info = NULL;
  1028. callstatic_user_call->num_args = 0;
  1029. callstatic_user_call->scope = ce;
  1030. callstatic_user_call->fn_flags = ZEND_ACC_STATIC | ZEND_ACC_PUBLIC | ZEND_ACC_CALL_VIA_HANDLER;
  1031. callstatic_user_call->function_name = estrndup(method_name, method_len);
  1032. return (zend_function *)callstatic_user_call;
  1033. }
  1034. /* }}} */
  1035. /* This is not (yet?) in the API, but it belongs in the built-in objects callbacks */
  1036. ZEND_API zend_function *zend_std_get_static_method(zend_class_entry *ce, const char *function_name_strval, int function_name_strlen, const zend_literal *key TSRMLS_DC) /* {{{ */
  1037. {
  1038. zend_function *fbc = NULL;
  1039. char *lc_class_name, *lc_function_name = NULL;
  1040. ulong hash_value;
  1041. ALLOCA_FLAG(use_heap)
  1042. if (EXPECTED(key != NULL)) {
  1043. #if (ZEND_GCC_VERSION == 4009) && !(defined(ZTS) && defined(NETWARE)) && !(defined(ZTS) && defined(HPUX)) && !defined(DARWIN)
  1044. /* This is a workaround for bug in GCC 4.9.2 */
  1045. use_heap = 0;
  1046. #endif
  1047. lc_function_name = Z_STRVAL(key->constant);
  1048. hash_value = key->hash_value;
  1049. } else {
  1050. lc_function_name = do_alloca(function_name_strlen+1, use_heap);
  1051. /* Create a zend_copy_str_tolower(dest, src, src_length); */
  1052. zend_str_tolower_copy(lc_function_name, function_name_strval, function_name_strlen);
  1053. hash_value = zend_hash_func(lc_function_name, function_name_strlen+1);
  1054. }
  1055. if (function_name_strlen == ce->name_length && ce->constructor) {
  1056. lc_class_name = zend_str_tolower_dup(ce->name, ce->name_length);
  1057. /* Only change the method to the constructor if the constructor isn't called __construct
  1058. * we check for __ so we can be binary safe for lowering, we should use ZEND_CONSTRUCTOR_FUNC_NAME
  1059. */
  1060. if (!memcmp(lc_class_name, lc_function_name, function_name_strlen) && memcmp(ce->constructor->common.function_name, "__", sizeof("__") - 1)) {
  1061. fbc = ce->constructor;
  1062. }
  1063. efree(lc_class_name);
  1064. }
  1065. if (EXPECTED(!fbc) &&
  1066. UNEXPECTED(zend_hash_quick_find(&ce->function_table, lc_function_name, function_name_strlen+1, hash_value, (void **) &fbc)==FAILURE)) {
  1067. if (UNEXPECTED(!key)) {
  1068. free_alloca(lc_function_name, use_heap);
  1069. }
  1070. if (ce->__call &&
  1071. EG(This) &&
  1072. Z_OBJ_HT_P(EG(This))->get_class_entry &&
  1073. instanceof_function(Z_OBJCE_P(EG(This)), ce TSRMLS_CC)) {
  1074. return zend_get_user_call_function(ce, function_name_strval, function_name_strlen);
  1075. } else if (ce->__callstatic) {
  1076. return zend_get_user_callstatic_function(ce, function_name_strval, function_name_strlen);
  1077. } else {
  1078. return NULL;
  1079. }
  1080. }
  1081. #if MBO_0
  1082. /* right now this function is used for non static method lookup too */
  1083. /* Is the function static */
  1084. if (UNEXPECTED(!(fbc->common.fn_flags & ZEND_ACC_STATIC))) {
  1085. zend_error_noreturn(E_ERROR, "Cannot call non static method %s::%s() without object", ZEND_FN_SCOPE_NAME(fbc), fbc->common.function_name);
  1086. }
  1087. #endif
  1088. if (fbc->op_array.fn_flags & ZEND_ACC_PUBLIC) {
  1089. /* No further checks necessary, most common case */
  1090. } else if (fbc->op_array.fn_flags & ZEND_ACC_PRIVATE) {
  1091. zend_function *updated_fbc;
  1092. /* Ensure that if we're calling a private function, we're allowed to do so.
  1093. */
  1094. updated_fbc = zend_check_private_int(fbc, EG(scope), lc_function_name, function_name_strlen, hash_value TSRMLS_CC);
  1095. if (EXPECTED(updated_fbc != NULL)) {
  1096. fbc = updated_fbc;
  1097. } else {
  1098. if (ce->__callstatic) {
  1099. fbc = zend_get_user_callstatic_function(ce, function_name_strval, function_name_strlen);
  1100. } else {
  1101. zend_error_noreturn(E_ERROR, "Call to %s method %s::%s() from context '%s'", zend_visibility_string(fbc->common.fn_flags), ZEND_FN_SCOPE_NAME(fbc), function_name_strval, EG(scope) ? EG(scope)->name : "");
  1102. }
  1103. }
  1104. } else if ((fbc->common.fn_flags & ZEND_ACC_PROTECTED)) {
  1105. /* Ensure that if we're calling a protected function, we're allowed to do so.
  1106. */
  1107. if (UNEXPECTED(!zend_check_protected(zend_get_function_root_class(fbc), EG(scope)))) {
  1108. if (ce->__callstatic) {
  1109. fbc = zend_get_user_callstatic_function(ce, function_name_strval, function_name_strlen);
  1110. } else {
  1111. zend_error_noreturn(E_ERROR, "Call to %s method %s::%s() from context '%s'", zend_visibility_string(fbc->common.fn_flags), ZEND_FN_SCOPE_NAME(fbc), function_name_strval, EG(scope) ? EG(scope)->name : "");
  1112. }
  1113. }
  1114. }
  1115. if (UNEXPECTED(!key)) {
  1116. free_alloca(lc_function_name, use_heap);
  1117. }
  1118. return fbc;
  1119. }
  1120. /* }}} */
  1121. ZEND_API zval **zend_std_get_static_property(zend_class_entry *ce, const char *property_name, int property_name_len, zend_bool silent, const zend_literal *key TSRMLS_DC) /* {{{ */
  1122. {
  1123. zend_property_info *property_info;
  1124. ulong hash_value;
  1125. if (UNEXPECTED(!key) ||
  1126. (property_info = CACHED_POLYMORPHIC_PTR(key->cache_slot, ce)) == NULL) {
  1127. if (EXPECTED(key != NULL)) {
  1128. hash_value = key->hash_value;
  1129. } else {
  1130. hash_value = zend_hash_func(property_name, property_name_len+1);
  1131. }
  1132. if (UNEXPECTED(zend_hash_quick_find(&ce->properties_info, property_name, property_name_len+1, hash_value, (void **) &property_info)==FAILURE)) {
  1133. if (!silent) {
  1134. zend_error_noreturn(E_ERROR, "Access to undeclared static property: %s::$%s", ce->name, property_name);
  1135. }
  1136. return NULL;
  1137. }
  1138. #if DEBUG_OBJECT_HANDLERS
  1139. zend_printf("Access type for %s::%s is %s\n", ce->name, property_name, zend_visibility_string(property_info->flags));
  1140. #endif
  1141. if (UNEXPECTED(!zend_verify_property_access(property_info, ce TSRMLS_CC))) {
  1142. if (!silent) {
  1143. zend_error_noreturn(E_ERROR, "Cannot access %s property %s::$%s", zend_visibility_string(property_info->flags), ce->name, property_name);
  1144. }
  1145. return NULL;
  1146. }
  1147. if (UNEXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0)) {
  1148. if (!silent) {
  1149. zend_error_noreturn(E_ERROR, "Access to undeclared static property: %s::$%s", ce->name, property_name);
  1150. }
  1151. return NULL;
  1152. }
  1153. zend_update_class_constants(ce TSRMLS_CC);
  1154. if (EXPECTED(key != NULL)) {
  1155. CACHE_POLYMORPHIC_PTR(key->cache_slot, ce, property_info);
  1156. }
  1157. }
  1158. if (UNEXPECTED(CE_STATIC_MEMBERS(ce) == NULL) ||
  1159. UNEXPECTED(CE_STATIC_MEMBERS(ce)[property_info->offset] == NULL)) {
  1160. if (!silent) {
  1161. zend_error_noreturn(E_ERROR, "Access to undeclared static property: %s::$%s", ce->name, property_name);
  1162. }
  1163. return NULL;
  1164. }
  1165. return &CE_STATIC_MEMBERS(ce)[property_info->offset];
  1166. }
  1167. /* }}} */
  1168. ZEND_API zend_bool zend_std_unset_static_property(zend_class_entry *ce, const char *property_name, int property_name_len, const zend_literal *key TSRMLS_DC) /* {{{ */
  1169. {
  1170. zend_error_noreturn(E_ERROR, "Attempt to unset static property %s::$%s", ce->name, property_name);
  1171. return 0;
  1172. }
  1173. /* }}} */
  1174. ZEND_API union _zend_function *zend_std_get_constructor(zval *object TSRMLS_DC) /* {{{ */
  1175. {
  1176. zend_object *zobj = Z_OBJ_P(object);
  1177. zend_function *constructor = zobj->ce->constructor;
  1178. if (constructor) {
  1179. if (constructor->op_array.fn_flags & ZEND_ACC_PUBLIC) {
  1180. /* No further checks necessary */
  1181. } else if (constructor->op_array.fn_flags & ZEND_ACC_PRIVATE) {
  1182. /* Ensure that if we're calling a private function, we're allowed to do so.
  1183. */
  1184. if (UNEXPECTED(constructor->common.scope != EG(scope))) {
  1185. if (EG(scope)) {
  1186. zend_error_noreturn(E_ERROR, "Call to private %s::%s() from context '%s'", constructor->common.scope->name, constructor->common.function_name, EG(scope)->name);
  1187. } else {
  1188. zend_error_noreturn(E_ERROR, "Call to private %s::%s() from invalid context", constructor->common.scope->name, constructor->common.function_name);
  1189. }
  1190. }
  1191. } else if ((constructor->common.fn_flags & ZEND_ACC_PROTECTED)) {
  1192. /* Ensure that if we're calling a protected function, we're allowed to do so.
  1193. * Constructors only have prototype if they are defined by an interface but
  1194. * it is the compilers responsibility to take care of the prototype.
  1195. */
  1196. if (UNEXPECTED(!zend_check_protected(zend_get_function_root_class(constructor), EG(scope)))) {
  1197. if (EG(scope)) {
  1198. zend_error_noreturn(E_ERROR, "Call to protected %s::%s() from context '%s'", constructor->common.scope->name, constructor->common.function_name, EG(scope)->name);
  1199. } else {
  1200. zend_error_noreturn(E_ERROR, "Call to protected %s::%s() from invalid context", constructor->common.scope->name, constructor->common.function_name);
  1201. }
  1202. }
  1203. }
  1204. }
  1205. return constructor;
  1206. }
  1207. /* }}} */
  1208. int zend_compare_symbol_tables_i(HashTable *ht1, HashTable *ht2 TSRMLS_DC);
  1209. static int zend_std_compare_objects(zval *o1, zval *o2 TSRMLS_DC) /* {{{ */
  1210. {
  1211. zend_object *zobj1, *zobj2;
  1212. zobj1 = Z_OBJ_P(o1);
  1213. zobj2 = Z_OBJ_P(o2);
  1214. if (zobj1->ce != zobj2->ce) {
  1215. return 1; /* different classes */
  1216. }
  1217. if (!zobj1->properties && !zobj2->properties) {
  1218. int i;
  1219. Z_OBJ_PROTECT_RECURSION(o1);
  1220. Z_OBJ_PROTECT_RECURSION(o2);
  1221. for (i = 0; i < zobj1->ce->default_properties_count; i++) {
  1222. if (zobj1->properties_table[i]) {
  1223. if (zobj2->properties_table[i]) {
  1224. zval result;
  1225. if (compare_function(&result, zobj1->properties_table[i], zobj2->properties_table[i] TSRMLS_CC)==FAILURE) {
  1226. Z_OBJ_UNPROTECT_RECURSION(o1);
  1227. Z_OBJ_UNPROTECT_RECURSION(o2);
  1228. return 1;
  1229. }
  1230. if (Z_LVAL(result) != 0) {
  1231. Z_OBJ_UNPROTECT_RECURSION(o1);
  1232. Z_OBJ_UNPROTECT_RECURSION(o2);
  1233. return Z_LVAL(result);
  1234. }
  1235. } else {
  1236. Z_OBJ_UNPROTECT_RECURSION(o1);
  1237. Z_OBJ_UNPROTECT_RECURSION(o2);
  1238. return 1;
  1239. }
  1240. } else {
  1241. if (zobj2->properties_table[i]) {
  1242. Z_OBJ_UNPROTECT_RECURSION(o1);
  1243. Z_OBJ_UNPROTECT_RECURSION(o2);
  1244. return 1;
  1245. }
  1246. }
  1247. }
  1248. Z_OBJ_UNPROTECT_RECURSION(o1);
  1249. Z_OBJ_UNPROTECT_RECURSION(o2);
  1250. return 0;
  1251. } else {
  1252. if (!zobj1->properties) {
  1253. rebuild_object_properties(zobj1);
  1254. }
  1255. if (!zobj2->properties) {
  1256. rebuild_object_properties(zobj2);
  1257. }
  1258. return zend_compare_symbol_tables_i(zobj1->properties, zobj2->properties TSRMLS_CC);
  1259. }
  1260. }
  1261. /* }}} */
  1262. static int zend_std_has_property(zval *object, zval *member, int has_set_exists, const zend_literal *key TSRMLS_DC) /* {{{ */
  1263. {
  1264. zend_object *zobj;
  1265. int result;
  1266. zval **value = NULL;
  1267. zval *tmp_member = NULL;
  1268. zend_property_info *property_info;
  1269. zobj = Z_OBJ_P(object);
  1270. if (UNEXPECTED(Z_TYPE_P(member) != IS_STRING)) {
  1271. ALLOC_ZVAL(tmp_member);
  1272. *tmp_member = *member;
  1273. INIT_PZVAL(tmp_member);
  1274. zval_copy_ctor(tmp_member);
  1275. convert_to_string(tmp_member);
  1276. member = tmp_member;
  1277. key = NULL;
  1278. }
  1279. #if DEBUG_OBJECT_HANDLERS
  1280. fprintf(stderr, "Read object #%d property: %s\n", Z_OBJ_HANDLE_P(object), Z_STRVAL_P(member));
  1281. #endif
  1282. property_info = zend_get_property_info_quick(zobj->ce, member, 1, key TSRMLS_CC);
  1283. if (UNEXPECTED(!property_info) ||
  1284. ((EXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0) &&
  1285. property_info->offset >= 0) ?
  1286. (zobj->properties ?
  1287. ((value = (zval**)zobj->properties_table[property_info->offset]) == NULL) :
  1288. (*(value = &zobj->properties_table[property_info->offset]) == NULL)) :
  1289. (UNEXPECTED(!zobj->properties) ||
  1290. UNEXPECTED(zend_hash_quick_find(zobj->properties, property_info->name, property_info->name_length+1, property_info->h, (void **) &value) == FAILURE)))) {
  1291. zend_guard *guard;
  1292. result = 0;
  1293. if ((has_set_exists != 2) &&
  1294. zobj->ce->__isset &&
  1295. zend_get_property_guard(zobj, property_info, member, &guard) == SUCCESS &&
  1296. !guard->in_isset) {
  1297. zval *rv;
  1298. /* have issetter - try with it! */
  1299. Z_ADDREF_P(object);
  1300. if (PZVAL_IS_REF(object)) {
  1301. SEPARATE_ZVAL(&object);
  1302. }
  1303. guard->in_isset = 1; /* prevent circular getting */
  1304. rv = zend_std_call_issetter(object, member TSRMLS_CC);
  1305. if (rv) {
  1306. result = zend_is_true(rv);
  1307. zval_ptr_dtor(&rv);
  1308. if (has_set_exists && result) {
  1309. if (EXPECTED(!EG(exception)) && zobj->ce->__get && !guard->in_get) {
  1310. guard->in_get = 1;
  1311. rv = zend_std_call_getter(object, member TSRMLS_CC);
  1312. guard->in_get = 0;
  1313. if (rv) {
  1314. Z_ADDREF_P(rv);
  1315. result = i_zend_is_true(rv);
  1316. zval_ptr_dtor(&rv);
  1317. } else {
  1318. result = 0;
  1319. }
  1320. } else {
  1321. result = 0;
  1322. }
  1323. }
  1324. }
  1325. guard->in_isset = 0;
  1326. zval_ptr_dtor(&object);
  1327. }
  1328. } else {
  1329. switch (has_set_exists) {
  1330. case 0:
  1331. result = (Z_TYPE_PP(value) != IS_NULL);
  1332. break;
  1333. default:
  1334. result = zend_is_true(*value);
  1335. break;
  1336. case 2:
  1337. result = 1;
  1338. break;
  1339. }
  1340. }
  1341. if (UNEXPECTED(tmp_member != NULL)) {
  1342. zval_ptr_dtor(&tmp_member);
  1343. }
  1344. return result;
  1345. }
  1346. /* }}} */
  1347. zend_class_entry *zend_std_object_get_class(const zval *object TSRMLS_DC) /* {{{ */
  1348. {
  1349. zend_object *zobj;
  1350. zobj = Z_OBJ_P(object);
  1351. return zobj->ce;
  1352. }
  1353. /* }}} */
  1354. int zend_std_object_get_class_name(const zval *object, const char **class_name, zend_uint *class_name_len, int parent TSRMLS_DC) /* {{{ */
  1355. {
  1356. zend_object *zobj;
  1357. zend_class_entry *ce;
  1358. zobj = Z_OBJ_P(object);
  1359. if (parent) {
  1360. if (!zobj->ce->parent) {
  1361. return FAILURE;
  1362. }
  1363. ce = zobj->ce->parent;
  1364. } else {
  1365. ce = zobj->ce;
  1366. }
  1367. *class_name_len = ce->name_length;
  1368. *class_name = estrndup(ce->name, ce->name_length);
  1369. return SUCCESS;
  1370. }
  1371. /* }}} */
  1372. ZEND_API int zend_std_cast_object_tostring(zval *readobj, zval *writeobj, int type TSRMLS_DC) /* {{{ */
  1373. {
  1374. zval *retval;
  1375. zend_class_entry *ce;
  1376. switch (type) {
  1377. case IS_STRING:
  1378. ce = Z_OBJCE_P(readobj);
  1379. if (ce->__tostring &&
  1380. (zend_call_method_with_0_params(&readobj, ce, &ce->__tostring, "__tostring", &retval) || EG(exception))) {
  1381. if (UNEXPECTED(EG(exception) != NULL)) {
  1382. if (retval) {
  1383. zval_ptr_dtor(&retval);
  1384. }
  1385. EG(exception) = NULL;
  1386. zend_error_noreturn(E_ERROR, "Method %s::__toString() must not throw an exception", ce->name);
  1387. return FAILURE;
  1388. }
  1389. if (EXPECTED(Z_TYPE_P(retval) == IS_STRING)) {
  1390. INIT_PZVAL(writeobj);
  1391. if (readobj == writeobj) {
  1392. zval_dtor(readobj);
  1393. }
  1394. ZVAL_ZVAL(writeobj, retval, 1, 1);
  1395. if (Z_TYPE_P(writeobj) != type) {
  1396. convert_to_explicit_type(writeobj, type);
  1397. }
  1398. return SUCCESS;
  1399. } else {
  1400. zval_ptr_dtor(&retval);
  1401. INIT_PZVAL(writeobj);
  1402. if (readobj == writeobj) {
  1403. zval_dtor(readobj);
  1404. }
  1405. ZVAL_EMPTY_STRING(writeobj);
  1406. zend_error(E_RECOVERABLE_ERROR, "Method %s::__toString() must return a string value", ce->name);
  1407. return SUCCESS;
  1408. }
  1409. }
  1410. return FAILURE;
  1411. case IS_BOOL:
  1412. INIT_PZVAL(writeobj);
  1413. ZVAL_BOOL(writeobj, 1);
  1414. return SUCCESS;
  1415. case IS_LONG:
  1416. ce = Z_OBJCE_P(readobj);
  1417. zend_error(E_NOTICE, "Object of class %s could not be converted to int", ce->name);
  1418. INIT_PZVAL(writeobj);
  1419. if (readobj == writeobj) {
  1420. zval_dtor(readobj);
  1421. }
  1422. ZVAL_LONG(writeobj, 1);
  1423. return SUCCESS;
  1424. case IS_DOUBLE:
  1425. ce = Z_OBJCE_P(readobj);
  1426. zend_error(E_NOTICE, "Object of class %s could not be converted to double", ce->name);
  1427. INIT_PZVAL(writeobj);
  1428. if (readobj == writeobj) {
  1429. zval_dtor(readobj);
  1430. }
  1431. ZVAL_DOUBLE(writeobj, 1);
  1432. return SUCCESS;
  1433. default:
  1434. INIT_PZVAL(writeobj);
  1435. Z_TYPE_P(writeobj) = IS_NULL;
  1436. break;
  1437. }
  1438. return FAILURE;
  1439. }
  1440. /* }}} */
  1441. int zend_std_get_closure(zval *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zval **zobj_ptr TSRMLS_DC) /* {{{ */
  1442. {
  1443. zend_class_entry *ce;
  1444. if (Z_TYPE_P(obj) != IS_OBJECT) {
  1445. return FAILURE;
  1446. }
  1447. ce = Z_OBJCE_P(obj);
  1448. if (zend_hash_find(&ce->function_table, ZEND_INVOKE_FUNC_NAME, sizeof(ZEND_INVOKE_FUNC_NAME), (void**)fptr_ptr) == FAILURE) {
  1449. return FAILURE;
  1450. }
  1451. *ce_ptr = ce;
  1452. if ((*fptr_ptr)->common.fn_flags & ZEND_ACC_STATIC) {
  1453. if (zobj_ptr) {
  1454. *zobj_ptr = NULL;
  1455. }
  1456. } else {
  1457. if (zobj_ptr) {
  1458. *zobj_ptr = obj;
  1459. }
  1460. }
  1461. return SUCCESS;
  1462. }
  1463. /* }}} */
  1464. ZEND_API zend_object_handlers std_object_handlers = {
  1465. zend_objects_store_add_ref, /* add_ref */
  1466. zend_objects_store_del_ref, /* del_ref */
  1467. zend_objects_clone_obj, /* clone_obj */
  1468. zend_std_read_property, /* read_property */
  1469. zend_std_write_property, /* write_property */
  1470. zend_std_read_dimension, /* read_dimension */
  1471. zend_std_write_dimension, /* write_dimension */
  1472. zend_std_get_property_ptr_ptr, /* get_property_ptr_ptr */
  1473. NULL, /* get */
  1474. NULL, /* set */
  1475. zend_std_has_property, /* has_property */
  1476. zend_std_unset_property, /* unset_property */
  1477. zend_std_has_dimension, /* has_dimension */
  1478. zend_std_unset_dimension, /* unset_dimension */
  1479. zend_std_get_properties, /* get_properties */
  1480. zend_std_get_method, /* get_method */
  1481. NULL, /* call_method */
  1482. zend_std_get_constructor, /* get_constructor */
  1483. zend_std_object_get_class, /* get_class_entry */
  1484. zend_std_object_get_class_name, /* get_class_name */
  1485. zend_std_compare_objects, /* compare_objects */
  1486. zend_std_cast_object_tostring, /* cast_object */
  1487. NULL, /* count_elements */
  1488. zend_std_get_debug_info, /* get_debug_info */
  1489. zend_std_get_closure, /* get_closure */
  1490. zend_std_get_gc, /* get_gc */
  1491. NULL, /* do_operation */
  1492. NULL, /* compare */
  1493. };
  1494. /*
  1495. * Local variables:
  1496. * tab-width: 4
  1497. * c-basic-offset: 4
  1498. * indent-tabs-mode: t
  1499. * End:
  1500. */