array.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #include <ext/spl/spl_iterators.h>
  31. #include <Zend/zend_API.h>
  32. #include <Zend/zend_interfaces.h>
  33. #include "protobuf.h"
  34. ZEND_BEGIN_ARG_INFO_EX(arginfo_offsetGet, 0, 0, 1)
  35. ZEND_ARG_INFO(0, index)
  36. ZEND_END_ARG_INFO()
  37. ZEND_BEGIN_ARG_INFO_EX(arginfo_offsetSet, 0, 0, 2)
  38. ZEND_ARG_INFO(0, index)
  39. ZEND_ARG_INFO(0, newval)
  40. ZEND_END_ARG_INFO()
  41. ZEND_BEGIN_ARG_INFO(arginfo_void, 0)
  42. ZEND_END_ARG_INFO()
  43. static zend_function_entry repeated_field_methods[] = {
  44. PHP_ME(RepeatedField, __construct, NULL, ZEND_ACC_PUBLIC)
  45. PHP_ME(RepeatedField, append, NULL, ZEND_ACC_PUBLIC)
  46. PHP_ME(RepeatedField, offsetExists, arginfo_offsetGet, ZEND_ACC_PUBLIC)
  47. PHP_ME(RepeatedField, offsetGet, arginfo_offsetGet, ZEND_ACC_PUBLIC)
  48. PHP_ME(RepeatedField, offsetSet, arginfo_offsetSet, ZEND_ACC_PUBLIC)
  49. PHP_ME(RepeatedField, offsetUnset, arginfo_offsetGet, ZEND_ACC_PUBLIC)
  50. PHP_ME(RepeatedField, count, arginfo_void, ZEND_ACC_PUBLIC)
  51. PHP_ME(RepeatedField, getIterator, arginfo_void, ZEND_ACC_PUBLIC)
  52. ZEND_FE_END
  53. };
  54. static zend_function_entry repeated_field_iter_methods[] = {
  55. PHP_ME(RepeatedFieldIter, rewind, arginfo_void, ZEND_ACC_PUBLIC)
  56. PHP_ME(RepeatedFieldIter, current, arginfo_void, ZEND_ACC_PUBLIC)
  57. PHP_ME(RepeatedFieldIter, key, arginfo_void, ZEND_ACC_PUBLIC)
  58. PHP_ME(RepeatedFieldIter, next, arginfo_void, ZEND_ACC_PUBLIC)
  59. PHP_ME(RepeatedFieldIter, valid, arginfo_void, ZEND_ACC_PUBLIC)
  60. ZEND_FE_END
  61. };
  62. // Forward declare static functions.
  63. static int repeated_field_array_init(zval *array, upb_fieldtype_t type,
  64. uint size ZEND_FILE_LINE_DC);
  65. static void repeated_field_write_dimension(zval *object, zval *offset,
  66. zval *value TSRMLS_DC);
  67. static int repeated_field_has_dimension(zval *object, zval *offset TSRMLS_DC);
  68. static HashTable *repeated_field_get_gc(zval *object, CACHED_VALUE **table,
  69. int *n TSRMLS_DC);
  70. #if PHP_MAJOR_VERSION < 7
  71. static zend_object_value repeated_field_create(zend_class_entry *ce TSRMLS_DC);
  72. static zend_object_value repeated_field_iter_create(zend_class_entry *ce TSRMLS_DC);
  73. #else
  74. static zend_object *repeated_field_create(zend_class_entry *ce TSRMLS_DC);
  75. static zend_object *repeated_field_iter_create(zend_class_entry *ce TSRMLS_DC);
  76. #endif
  77. // -----------------------------------------------------------------------------
  78. // RepeatedField creation/desctruction
  79. // -----------------------------------------------------------------------------
  80. zend_class_entry* repeated_field_type;
  81. zend_class_entry* repeated_field_iter_type;
  82. zend_object_handlers* repeated_field_handlers;
  83. zend_object_handlers* repeated_field_iter_handlers;
  84. // Define object free method.
  85. PHP_PROTO_OBJECT_FREE_START(RepeatedField, repeated_field)
  86. #if PHP_MAJOR_VERSION < 7
  87. php_proto_zval_ptr_dtor(intern->array);
  88. #else
  89. php_proto_zval_ptr_dtor(&intern->array);
  90. #endif
  91. PHP_PROTO_OBJECT_FREE_END
  92. PHP_PROTO_OBJECT_DTOR_START(RepeatedField, repeated_field)
  93. PHP_PROTO_OBJECT_DTOR_END
  94. // Define object create method.
  95. PHP_PROTO_OBJECT_CREATE_START(RepeatedField, repeated_field)
  96. #if PHP_MAJOR_VERSION < 7
  97. intern->array = NULL;
  98. #endif
  99. intern->type = 0;
  100. intern->msg_ce = NULL;
  101. PHP_PROTO_OBJECT_CREATE_END(RepeatedField, repeated_field)
  102. // Init class entry.
  103. PHP_PROTO_INIT_CLASS_START("Google\\Protobuf\\Internal\\RepeatedField",
  104. RepeatedField, repeated_field)
  105. zend_class_implements(repeated_field_type TSRMLS_CC, 3, spl_ce_ArrayAccess,
  106. zend_ce_aggregate, spl_ce_Countable);
  107. repeated_field_handlers->write_dimension = repeated_field_write_dimension;
  108. repeated_field_handlers->get_gc = repeated_field_get_gc;
  109. PHP_PROTO_INIT_CLASS_END
  110. // Define array element free function.
  111. #if PHP_MAJOR_VERSION < 7
  112. static inline void php_proto_array_string_release(void *value) {
  113. zval_ptr_dtor(value);
  114. }
  115. static inline void php_proto_array_object_release(void *value) {
  116. zval_ptr_dtor(value);
  117. }
  118. static inline void php_proto_array_default_release(void *value) {
  119. }
  120. #else
  121. static inline void php_proto_array_string_release(zval *value) {
  122. void* ptr = Z_PTR_P(value);
  123. zend_string* object = *(zend_string**)ptr;
  124. zend_string_release(object);
  125. efree(ptr);
  126. }
  127. static inline void php_proto_array_object_release(zval *value) {
  128. zval_ptr_dtor(value);
  129. }
  130. static void php_proto_array_default_release(zval* value) {
  131. void* ptr = Z_PTR_P(value);
  132. efree(ptr);
  133. }
  134. #endif
  135. static int repeated_field_array_init(zval *array, upb_fieldtype_t type,
  136. uint size ZEND_FILE_LINE_DC) {
  137. PHP_PROTO_ALLOC_ARRAY(array);
  138. switch (type) {
  139. case UPB_TYPE_STRING:
  140. case UPB_TYPE_BYTES:
  141. zend_hash_init(Z_ARRVAL_P(array), size, NULL,
  142. php_proto_array_string_release, 0);
  143. break;
  144. case UPB_TYPE_MESSAGE:
  145. zend_hash_init(Z_ARRVAL_P(array), size, NULL,
  146. php_proto_array_object_release, 0);
  147. break;
  148. default:
  149. zend_hash_init(Z_ARRVAL_P(array), size, NULL,
  150. php_proto_array_default_release, 0);
  151. }
  152. return SUCCESS;
  153. }
  154. // -----------------------------------------------------------------------------
  155. // RepeatedField Handlers
  156. // -----------------------------------------------------------------------------
  157. static void repeated_field_write_dimension(zval *object, zval *offset,
  158. zval *value TSRMLS_DC) {
  159. uint64_t index;
  160. RepeatedField *intern = UNBOX(RepeatedField, object);
  161. HashTable *ht = PHP_PROTO_HASH_OF(intern->array);
  162. int size = native_slot_size(intern->type);
  163. unsigned char memory[NATIVE_SLOT_MAX_SIZE];
  164. memset(memory, 0, NATIVE_SLOT_MAX_SIZE);
  165. if (!native_slot_set_by_array(intern->type, intern->msg_ce, memory,
  166. value TSRMLS_CC)) {
  167. return;
  168. }
  169. if (!offset || Z_TYPE_P(offset) == IS_NULL) {
  170. index = zend_hash_num_elements(PHP_PROTO_HASH_OF(intern->array));
  171. } else {
  172. if (protobuf_convert_to_uint64(offset, &index)) {
  173. if (!zend_hash_index_exists(ht, index)) {
  174. zend_error(E_USER_ERROR, "Element at %llu doesn't exist.\n",
  175. (long long unsigned int)index);
  176. return;
  177. }
  178. } else {
  179. return;
  180. }
  181. }
  182. if (intern->type == UPB_TYPE_MESSAGE) {
  183. php_proto_zend_hash_index_update_zval(ht, index, *(zval**)memory);
  184. } else {
  185. php_proto_zend_hash_index_update_mem(ht, index, memory, size, NULL);
  186. }
  187. }
  188. #if PHP_MAJOR_VERSION < 7
  189. static HashTable *repeated_field_get_gc(zval *object, zval ***table,
  190. int *n TSRMLS_DC) {
  191. #else
  192. static HashTable *repeated_field_get_gc(zval *object, zval **table, int *n) {
  193. #endif
  194. *table = NULL;
  195. *n = 0;
  196. RepeatedField *intern = UNBOX(RepeatedField, object);
  197. return PHP_PROTO_HASH_OF(intern->array);
  198. }
  199. // -----------------------------------------------------------------------------
  200. // C RepeatedField Utilities
  201. // -----------------------------------------------------------------------------
  202. void *repeated_field_index_native(RepeatedField *intern, int index TSRMLS_DC) {
  203. HashTable *ht = PHP_PROTO_HASH_OF(intern->array);
  204. void *value;
  205. if (intern->type == UPB_TYPE_MESSAGE) {
  206. if (php_proto_zend_hash_index_find_zval(ht, index, (void **)&value) ==
  207. FAILURE) {
  208. zend_error(E_USER_ERROR, "Element at %d doesn't exist.\n", index);
  209. return NULL;
  210. }
  211. } else {
  212. if (php_proto_zend_hash_index_find_mem(ht, index, (void **)&value) ==
  213. FAILURE) {
  214. zend_error(E_USER_ERROR, "Element at %d doesn't exist.\n", index);
  215. return NULL;
  216. }
  217. }
  218. return value;
  219. }
  220. void repeated_field_push_native(RepeatedField *intern, void *value) {
  221. HashTable *ht = PHP_PROTO_HASH_OF(intern->array);
  222. int size = native_slot_size(intern->type);
  223. if (intern->type == UPB_TYPE_MESSAGE) {
  224. php_proto_zend_hash_next_index_insert_zval(ht, value);
  225. } else {
  226. php_proto_zend_hash_next_index_insert_mem(ht, (void **)value, size, NULL);
  227. }
  228. }
  229. void repeated_field_create_with_field(
  230. zend_class_entry *ce, const upb_fielddef *field,
  231. CACHED_VALUE *repeated_field PHP_PROTO_TSRMLS_DC) {
  232. upb_fieldtype_t type = upb_fielddef_type(field);
  233. const zend_class_entry *msg_ce = field_type_class(field PHP_PROTO_TSRMLS_CC);
  234. repeated_field_create_with_type(ce, type, msg_ce,
  235. repeated_field PHP_PROTO_TSRMLS_CC);
  236. }
  237. void repeated_field_create_with_type(
  238. zend_class_entry *ce, upb_fieldtype_t type, const zend_class_entry *msg_ce,
  239. CACHED_VALUE *repeated_field PHP_PROTO_TSRMLS_DC) {
  240. CREATE_OBJ_ON_ALLOCATED_ZVAL_PTR(CACHED_PTR_TO_ZVAL_PTR(repeated_field),
  241. repeated_field_type);
  242. RepeatedField *intern =
  243. UNBOX(RepeatedField, CACHED_TO_ZVAL_PTR(*repeated_field));
  244. intern->type = type;
  245. intern->msg_ce = msg_ce;
  246. #if PHP_MAJOR_VERSION < 7
  247. MAKE_STD_ZVAL(intern->array);
  248. repeated_field_array_init(intern->array, intern->type, 0 ZEND_FILE_LINE_CC);
  249. #else
  250. repeated_field_array_init(&intern->array, intern->type, 0 ZEND_FILE_LINE_CC);
  251. #endif
  252. // TODO(teboring): Link class entry for message and enum
  253. }
  254. // -----------------------------------------------------------------------------
  255. // PHP RepeatedField Methods
  256. // -----------------------------------------------------------------------------
  257. /**
  258. * Constructs an instance of RepeatedField.
  259. * @param long Type of the stored element.
  260. * @param string Message/Enum class name (message/enum fields only).
  261. */
  262. PHP_METHOD(RepeatedField, __construct) {
  263. long type;
  264. zend_class_entry* klass = NULL;
  265. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|C", &type, &klass) ==
  266. FAILURE) {
  267. return;
  268. }
  269. RepeatedField *intern = UNBOX(RepeatedField, getThis());
  270. intern->type = to_fieldtype(type);
  271. intern->msg_ce = klass;
  272. #if PHP_MAJOR_VERSION < 7
  273. MAKE_STD_ZVAL(intern->array);
  274. repeated_field_array_init(intern->array, intern->type, 0 ZEND_FILE_LINE_CC);
  275. #else
  276. repeated_field_array_init(&intern->array, intern->type, 0 ZEND_FILE_LINE_CC);
  277. #endif
  278. if (intern->type == UPB_TYPE_MESSAGE && klass == NULL) {
  279. zend_error(E_USER_ERROR, "Message type must have concrete class.");
  280. return;
  281. }
  282. // TODO(teboring): Consider enum.
  283. }
  284. /**
  285. * Append element to the end of the repeated field.
  286. * @param object The element to be added.
  287. */
  288. PHP_METHOD(RepeatedField, append) {
  289. zval *value;
  290. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &value) ==
  291. FAILURE) {
  292. return;
  293. }
  294. repeated_field_write_dimension(getThis(), NULL, value TSRMLS_CC);
  295. }
  296. /**
  297. * Check whether the element at given index exists.
  298. * @param long The index to be checked.
  299. * @return bool True if the element at the given index exists.
  300. */
  301. PHP_METHOD(RepeatedField, offsetExists) {
  302. long index;
  303. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) ==
  304. FAILURE) {
  305. return;
  306. }
  307. RepeatedField *intern = UNBOX(RepeatedField, getThis());
  308. RETURN_BOOL(index >= 0 &&
  309. index < zend_hash_num_elements(PHP_PROTO_HASH_OF(intern->array)));
  310. }
  311. /**
  312. * Return the element at the given index.
  313. * This will also be called for: $ele = $arr[0]
  314. * @param long The index of the element to be fetched.
  315. * @return object The stored element at given index.
  316. * @exception Invalid type for index.
  317. * @exception Non-existing index.
  318. */
  319. PHP_METHOD(RepeatedField, offsetGet) {
  320. long index;
  321. void *memory;
  322. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) ==
  323. FAILURE) {
  324. return;
  325. }
  326. RepeatedField *intern = UNBOX(RepeatedField, getThis());
  327. HashTable *table = PHP_PROTO_HASH_OF(intern->array);
  328. if (intern->type == UPB_TYPE_MESSAGE) {
  329. if (php_proto_zend_hash_index_find_zval(table, index, (void **)&memory) ==
  330. FAILURE) {
  331. zend_error(E_USER_ERROR, "Element at %ld doesn't exist.\n", index);
  332. return;
  333. }
  334. } else {
  335. if (php_proto_zend_hash_index_find_mem(table, index, (void **)&memory) ==
  336. FAILURE) {
  337. zend_error(E_USER_ERROR, "Element at %ld doesn't exist.\n", index);
  338. return;
  339. }
  340. }
  341. native_slot_get_by_array(intern->type, memory,
  342. ZVAL_PTR_TO_CACHED_PTR(return_value) TSRMLS_CC);
  343. }
  344. /**
  345. * Assign the element at the given index.
  346. * This will also be called for: $arr []= $ele and $arr[0] = ele
  347. * @param long The index of the element to be assigned.
  348. * @param object The element to be assigned.
  349. * @exception Invalid type for index.
  350. * @exception Non-existing index.
  351. * @exception Incorrect type of the element.
  352. */
  353. PHP_METHOD(RepeatedField, offsetSet) {
  354. zval *index, *value;
  355. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &index, &value) ==
  356. FAILURE) {
  357. return;
  358. }
  359. repeated_field_write_dimension(getThis(), index, value TSRMLS_CC);
  360. }
  361. /**
  362. * Remove the element at the given index.
  363. * This will also be called for: unset($arr)
  364. * @param long The index of the element to be removed.
  365. * @exception Invalid type for index.
  366. * @exception The element to be removed is not at the end of the RepeatedField.
  367. */
  368. PHP_METHOD(RepeatedField, offsetUnset) {
  369. long index;
  370. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) ==
  371. FAILURE) {
  372. return;
  373. }
  374. RepeatedField *intern = UNBOX(RepeatedField, getThis());
  375. // Only the element at the end of the array can be removed.
  376. if (index == -1 ||
  377. index != (zend_hash_num_elements(PHP_PROTO_HASH_OF(intern->array)) - 1)) {
  378. zend_error(E_USER_ERROR, "Cannot remove element at %ld.\n", index);
  379. return;
  380. }
  381. zend_hash_index_del(PHP_PROTO_HASH_OF(intern->array), index);
  382. }
  383. /**
  384. * Return the number of stored elements.
  385. * This will also be called for: count($arr)
  386. * @return long The number of stored elements.
  387. */
  388. PHP_METHOD(RepeatedField, count) {
  389. RepeatedField *intern = UNBOX(RepeatedField, getThis());
  390. if (zend_parse_parameters_none() == FAILURE) {
  391. return;
  392. }
  393. RETURN_LONG(zend_hash_num_elements(PHP_PROTO_HASH_OF(intern->array)));
  394. }
  395. /**
  396. * Return the beginning iterator.
  397. * This will also be called for: foreach($arr)
  398. * @return object Beginning iterator.
  399. */
  400. PHP_METHOD(RepeatedField, getIterator) {
  401. CREATE_OBJ_ON_ALLOCATED_ZVAL_PTR(return_value,
  402. repeated_field_iter_type);
  403. RepeatedField *intern = UNBOX(RepeatedField, getThis());
  404. RepeatedFieldIter *iter = UNBOX(RepeatedFieldIter, return_value);
  405. iter->repeated_field = intern;
  406. iter->position = 0;
  407. }
  408. // -----------------------------------------------------------------------------
  409. // RepeatedFieldIter creation/desctruction
  410. // -----------------------------------------------------------------------------
  411. // Define object free method.
  412. PHP_PROTO_OBJECT_FREE_START(RepeatedFieldIter, repeated_field_iter)
  413. PHP_PROTO_OBJECT_FREE_END
  414. PHP_PROTO_OBJECT_DTOR_START(RepeatedFieldIter, repeated_field_iter)
  415. PHP_PROTO_OBJECT_DTOR_END
  416. // Define object create method.
  417. PHP_PROTO_OBJECT_CREATE_START(RepeatedFieldIter, repeated_field_iter)
  418. intern->repeated_field = NULL;
  419. intern->position = 0;
  420. PHP_PROTO_OBJECT_CREATE_END(RepeatedFieldIter, repeated_field_iter)
  421. // Init class entry.
  422. PHP_PROTO_INIT_CLASS_START("Google\\Protobuf\\Internal\\RepeatedFieldIter",
  423. RepeatedFieldIter, repeated_field_iter)
  424. zend_class_implements(repeated_field_iter_type TSRMLS_CC, 1, zend_ce_iterator);
  425. PHP_PROTO_INIT_CLASS_END
  426. // -----------------------------------------------------------------------------
  427. // PHP RepeatedFieldIter Methods
  428. // -----------------------------------------------------------------------------
  429. PHP_METHOD(RepeatedFieldIter, rewind) {
  430. RepeatedFieldIter *intern = UNBOX(RepeatedFieldIter, getThis());
  431. intern->position = 0;
  432. }
  433. PHP_METHOD(RepeatedFieldIter, current) {
  434. RepeatedFieldIter *intern = UNBOX(RepeatedFieldIter, getThis());
  435. RepeatedField *repeated_field = intern->repeated_field;
  436. long index;
  437. void *memory;
  438. HashTable *table = PHP_PROTO_HASH_OF(repeated_field->array);
  439. if (repeated_field->type == UPB_TYPE_MESSAGE) {
  440. if (php_proto_zend_hash_index_find_zval(table, intern->position,
  441. (void **)&memory) == FAILURE) {
  442. zend_error(E_USER_ERROR, "Element at %d doesn't exist.\n", index);
  443. return;
  444. }
  445. } else {
  446. if (php_proto_zend_hash_index_find_mem(table, intern->position,
  447. (void **)&memory) == FAILURE) {
  448. zend_error(E_USER_ERROR, "Element at %d doesn't exist.\n", index);
  449. return;
  450. }
  451. }
  452. native_slot_get_by_array(repeated_field->type, memory,
  453. ZVAL_PTR_TO_CACHED_PTR(return_value) TSRMLS_CC);
  454. }
  455. PHP_METHOD(RepeatedFieldIter, key) {
  456. RepeatedFieldIter *intern = UNBOX(RepeatedFieldIter, getThis());
  457. RETURN_LONG(intern->position);
  458. }
  459. PHP_METHOD(RepeatedFieldIter, next) {
  460. RepeatedFieldIter *intern = UNBOX(RepeatedFieldIter, getThis());
  461. ++intern->position;
  462. }
  463. PHP_METHOD(RepeatedFieldIter, valid) {
  464. RepeatedFieldIter *intern = UNBOX(RepeatedFieldIter, getThis());
  465. RETURN_BOOL(zend_hash_num_elements(PHP_PROTO_HASH_OF(
  466. intern->repeated_field->array)) > intern->position);
  467. }