zend_iterators.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 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. | Author: Wez Furlong <wez@thebrainroom.com> |
  16. | Marcus Boerger <helly@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #include "zend.h"
  20. #include "zend_API.h"
  21. static zend_class_entry zend_iterator_class_entry;
  22. static void iter_wrapper_free(zend_object *object);
  23. static void iter_wrapper_dtor(zend_object *object);
  24. static HashTable *iter_wrapper_get_gc(zend_object *object, zval **table, int *n);
  25. static const zend_object_handlers iterator_object_handlers = {
  26. 0,
  27. iter_wrapper_free,
  28. iter_wrapper_dtor,
  29. NULL, /* clone_obj */
  30. NULL, /* prop read */
  31. NULL, /* prop write */
  32. NULL, /* read dim */
  33. NULL, /* write dim */
  34. NULL, /* get_property_ptr_ptr */
  35. NULL, /* has prop */
  36. NULL, /* unset prop */
  37. NULL, /* has dim */
  38. NULL, /* unset dim */
  39. NULL, /* props get */
  40. NULL, /* method get */
  41. NULL, /* get ctor */
  42. NULL, /* get class name */
  43. NULL, /* cast */
  44. NULL, /* count */
  45. NULL, /* get_debug_info */
  46. NULL, /* get_closure */
  47. iter_wrapper_get_gc,
  48. NULL, /* do_operation */
  49. NULL, /* compare */
  50. NULL /* get_properties_for */
  51. };
  52. ZEND_API void zend_register_iterator_wrapper(void)
  53. {
  54. INIT_CLASS_ENTRY(zend_iterator_class_entry, "__iterator_wrapper", NULL);
  55. }
  56. static void iter_wrapper_free(zend_object *object)
  57. {
  58. zend_object_iterator *iter = (zend_object_iterator*)object;
  59. iter->funcs->dtor(iter);
  60. }
  61. static void iter_wrapper_dtor(zend_object *object)
  62. {
  63. }
  64. static HashTable *iter_wrapper_get_gc(zend_object *object, zval **table, int *n) {
  65. zend_object_iterator *iter = (zend_object_iterator*)object;
  66. if (iter->funcs->get_gc) {
  67. return iter->funcs->get_gc(iter, table, n);
  68. }
  69. *table = NULL;
  70. *n = 0;
  71. return NULL;
  72. }
  73. ZEND_API void zend_iterator_init(zend_object_iterator *iter)
  74. {
  75. zend_object_std_init(&iter->std, &zend_iterator_class_entry);
  76. iter->std.handlers = &iterator_object_handlers;
  77. }
  78. ZEND_API void zend_iterator_dtor(zend_object_iterator *iter)
  79. {
  80. if (GC_DELREF(&iter->std) > 0) {
  81. return;
  82. }
  83. zend_objects_store_del(&iter->std);
  84. }
  85. ZEND_API zend_object_iterator* zend_iterator_unwrap(zval *array_ptr)
  86. {
  87. ZEND_ASSERT(Z_TYPE_P(array_ptr) == IS_OBJECT);
  88. if (Z_OBJ_HT_P(array_ptr) == &iterator_object_handlers) {
  89. return (zend_object_iterator *)Z_OBJ_P(array_ptr);
  90. }
  91. return NULL;
  92. }