map.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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. #include "utf8.h"
  35. ZEND_BEGIN_ARG_INFO_EX(arginfo_offsetGet, 0, 0, 1)
  36. ZEND_ARG_INFO(0, index)
  37. ZEND_END_ARG_INFO()
  38. ZEND_BEGIN_ARG_INFO_EX(arginfo_offsetSet, 0, 0, 2)
  39. ZEND_ARG_INFO(0, index)
  40. ZEND_ARG_INFO(0, newval)
  41. ZEND_END_ARG_INFO()
  42. ZEND_BEGIN_ARG_INFO(arginfo_void, 0)
  43. ZEND_END_ARG_INFO()
  44. // Utilities
  45. void* upb_value_memory(upb_value* v) {
  46. return (void*)(&v->val);
  47. }
  48. // -----------------------------------------------------------------------------
  49. // Basic map operations on top of upb's strtable.
  50. //
  51. // Note that we roll our own `Map` container here because, as for
  52. // `RepeatedField`, we want a strongly-typed container. This is so that any user
  53. // errors due to incorrect map key or value types are raised as close as
  54. // possible to the error site, rather than at some deferred point (e.g.,
  55. // serialization).
  56. //
  57. // We build our `Map` on top of upb_strtable so that we're able to take
  58. // advantage of the native_slot storage abstraction, as RepeatedField does.
  59. // (This is not quite a perfect mapping -- see the key conversions below -- but
  60. // gives us full support and error-checking for all value types for free.)
  61. // -----------------------------------------------------------------------------
  62. // Map values are stored using the native_slot abstraction (as with repeated
  63. // field values), but keys are a bit special. Since we use a strtable, we need
  64. // to store keys as sequences of bytes such that equality of those bytes maps
  65. // one-to-one to equality of keys. We store strings directly (i.e., they map to
  66. // their own bytes) and integers as native integers (using the native_slot
  67. // abstraction).
  68. // Note that there is another tradeoff here in keeping string keys as native
  69. // strings rather than PHP strings: traversing the Map requires conversion to
  70. // PHP string values on every traversal, potentially creating more garbage. We
  71. // should consider ways to cache a PHP version of the key if this becomes an
  72. // issue later.
  73. // Forms a key to use with the underlying strtable from a PHP key value. |buf|
  74. // must point to TABLE_KEY_BUF_LENGTH bytes of temporary space, used to
  75. // construct a key byte sequence if needed. |out_key| and |out_length| provide
  76. // the resulting key data/length.
  77. #define TABLE_KEY_BUF_LENGTH 8 // sizeof(uint64_t)
  78. static bool table_key(Map* self, zval* key,
  79. char* buf,
  80. const char** out_key,
  81. size_t* out_length TSRMLS_DC) {
  82. switch (self->key_type) {
  83. case UPB_TYPE_STRING:
  84. if (!protobuf_convert_to_string(key)) {
  85. return false;
  86. }
  87. if (!is_structurally_valid_utf8(Z_STRVAL_P(key), Z_STRLEN_P(key))) {
  88. zend_error(E_USER_ERROR, "Given key is not UTF8 encoded.");
  89. return false;
  90. }
  91. *out_key = Z_STRVAL_P(key);
  92. *out_length = Z_STRLEN_P(key);
  93. break;
  94. #define CASE_TYPE(upb_type, type, c_type, php_type) \
  95. case UPB_TYPE_##upb_type: { \
  96. c_type type##_value; \
  97. if (!protobuf_convert_to_##type(key, &type##_value)) { \
  98. return false; \
  99. } \
  100. native_slot_set_by_array(self->key_type, NULL, buf, key TSRMLS_CC); \
  101. *out_key = buf; \
  102. *out_length = native_slot_size(self->key_type); \
  103. break; \
  104. }
  105. CASE_TYPE(BOOL, bool, int8_t, BOOL)
  106. CASE_TYPE(INT32, int32, int32_t, LONG)
  107. CASE_TYPE(INT64, int64, int64_t, LONG)
  108. CASE_TYPE(UINT32, uint32, uint32_t, LONG)
  109. CASE_TYPE(UINT64, uint64, uint64_t, LONG)
  110. #undef CASE_TYPE
  111. default:
  112. // Map constructor should not allow a Map with another key type to be
  113. // constructed.
  114. assert(false);
  115. break;
  116. }
  117. return true;
  118. }
  119. // -----------------------------------------------------------------------------
  120. // MapField methods
  121. // -----------------------------------------------------------------------------
  122. static zend_function_entry map_field_methods[] = {
  123. PHP_ME(MapField, __construct, NULL, ZEND_ACC_PUBLIC)
  124. PHP_ME(MapField, offsetExists, arginfo_offsetGet, ZEND_ACC_PUBLIC)
  125. PHP_ME(MapField, offsetGet, arginfo_offsetGet, ZEND_ACC_PUBLIC)
  126. PHP_ME(MapField, offsetSet, arginfo_offsetSet, ZEND_ACC_PUBLIC)
  127. PHP_ME(MapField, offsetUnset, arginfo_offsetGet, ZEND_ACC_PUBLIC)
  128. PHP_ME(MapField, count, arginfo_void, ZEND_ACC_PUBLIC)
  129. PHP_ME(MapField, getIterator, arginfo_void, ZEND_ACC_PUBLIC)
  130. ZEND_FE_END
  131. };
  132. // Forward declare static functions.
  133. static void map_field_write_dimension(zval *object, zval *key,
  134. zval *value TSRMLS_DC);
  135. // -----------------------------------------------------------------------------
  136. // MapField creation/destruction
  137. // -----------------------------------------------------------------------------
  138. zend_class_entry* map_field_type;
  139. zend_class_entry* map_field_iter_type;
  140. zend_object_handlers* map_field_handlers;
  141. zend_object_handlers* map_field_iter_handlers;
  142. static void map_begin_internal(Map *map, MapIter *iter) {
  143. iter->self = map;
  144. upb_strtable_begin(&iter->it, &map->table);
  145. }
  146. static HashTable *map_field_get_gc(zval *object, CACHED_VALUE **table,
  147. int *n TSRMLS_DC) {
  148. // TODO(teboring): Unfortunately, zend engine does not support garbage
  149. // collection for custom array. We have to use zend engine's native array
  150. // instead.
  151. *table = NULL;
  152. *n = 0;
  153. return NULL;
  154. }
  155. // Define map value element free function.
  156. #if PHP_MAJOR_VERSION < 7
  157. static inline void php_proto_map_string_release(void *value) {
  158. zval_ptr_dtor(value);
  159. }
  160. static inline void php_proto_map_object_release(void *value) {
  161. zval_ptr_dtor(value);
  162. }
  163. #else
  164. static inline void php_proto_map_string_release(void *value) {
  165. zend_string* object = *(zend_string**)value;
  166. zend_string_release(object);
  167. }
  168. static inline void php_proto_map_object_release(void *value) {
  169. zend_object* object = *(zend_object**)value;
  170. GC_DELREF(object);
  171. if(GC_REFCOUNT(object) == 0) {
  172. zend_objects_store_del(object);
  173. }
  174. }
  175. #endif
  176. // Define object free method.
  177. PHP_PROTO_OBJECT_FREE_START(Map, map_field)
  178. MapIter it;
  179. int len;
  180. for (map_begin_internal(intern, &it); !map_done(&it); map_next(&it)) {
  181. upb_value value = map_iter_value(&it, &len);
  182. void *mem = upb_value_memory(&value);
  183. switch (intern->value_type) {
  184. case UPB_TYPE_MESSAGE:
  185. php_proto_map_object_release(mem);
  186. break;
  187. case UPB_TYPE_STRING:
  188. case UPB_TYPE_BYTES:
  189. php_proto_map_string_release(mem);
  190. break;
  191. default:
  192. break;
  193. }
  194. }
  195. upb_strtable_uninit(&intern->table);
  196. PHP_PROTO_OBJECT_FREE_END
  197. PHP_PROTO_OBJECT_EMPTY_DTOR_START(Map, map_field)
  198. PHP_PROTO_OBJECT_DTOR_END
  199. // Define object create method.
  200. PHP_PROTO_OBJECT_CREATE_START(Map, map_field)
  201. // Table value type is always UINT64: this ensures enough space to store the
  202. // native_slot value.
  203. if (!upb_strtable_init(&intern->table, UPB_CTYPE_UINT64)) {
  204. zend_error(E_USER_ERROR, "Could not allocate table.");
  205. }
  206. PHP_PROTO_OBJECT_CREATE_END(Map, map_field)
  207. // Init class entry.
  208. PHP_PROTO_INIT_CLASS_START("Google\\Protobuf\\Internal\\MapField", Map,
  209. map_field)
  210. zend_class_implements(map_field_type TSRMLS_CC, 3, spl_ce_ArrayAccess,
  211. zend_ce_aggregate, spl_ce_Countable);
  212. map_field_handlers->write_dimension = map_field_write_dimension;
  213. map_field_handlers->get_gc = map_field_get_gc;
  214. PHP_PROTO_INIT_CLASS_END
  215. void map_field_ensure_created(const upb_fielddef *field,
  216. CACHED_VALUE *map_field PHP_PROTO_TSRMLS_DC) {
  217. if (ZVAL_IS_NULL(CACHED_PTR_TO_ZVAL_PTR(map_field))) {
  218. zval_ptr_dtor(map_field);
  219. #if PHP_MAJOR_VERSION < 7
  220. MAKE_STD_ZVAL(CACHED_PTR_TO_ZVAL_PTR(map_field));
  221. #endif
  222. map_field_create_with_field(map_field_type, field,
  223. map_field PHP_PROTO_TSRMLS_CC);
  224. }
  225. }
  226. void map_field_create_with_field(const zend_class_entry *ce,
  227. const upb_fielddef *field,
  228. CACHED_VALUE *map_field PHP_PROTO_TSRMLS_DC) {
  229. const upb_fielddef *key_field = map_field_key(field);
  230. const upb_fielddef *value_field = map_field_value(field);
  231. map_field_create_with_type(
  232. ce, upb_fielddef_type(key_field), upb_fielddef_type(value_field),
  233. field_type_class(value_field TSRMLS_CC), map_field PHP_PROTO_TSRMLS_CC);
  234. }
  235. void map_field_create_with_type(const zend_class_entry *ce,
  236. upb_fieldtype_t key_type,
  237. upb_fieldtype_t value_type,
  238. const zend_class_entry *msg_ce,
  239. CACHED_VALUE *map_field PHP_PROTO_TSRMLS_DC) {
  240. CREATE_OBJ_ON_ALLOCATED_ZVAL_PTR(CACHED_PTR_TO_ZVAL_PTR(map_field),
  241. map_field_type);
  242. Map *intern = UNBOX(Map, CACHED_TO_ZVAL_PTR(*map_field));
  243. intern->key_type = key_type;
  244. intern->value_type = value_type;
  245. intern->msg_ce = msg_ce;
  246. }
  247. // -----------------------------------------------------------------------------
  248. // MapField Handlers
  249. // -----------------------------------------------------------------------------
  250. static bool map_field_read_dimension(zval *object, zval *key, int type,
  251. CACHED_VALUE *retval TSRMLS_DC) {
  252. Map *intern = UNBOX(Map, object);
  253. char keybuf[TABLE_KEY_BUF_LENGTH];
  254. const char* keyval = NULL;
  255. size_t length = 0;
  256. upb_value v;
  257. #ifndef NDEBUG
  258. v.ctype = UPB_CTYPE_UINT64;
  259. #endif
  260. if (!table_key(intern, key, keybuf, &keyval, &length TSRMLS_CC)) {
  261. return false;
  262. }
  263. if (upb_strtable_lookup2(&intern->table, keyval, length, &v)) {
  264. void* mem = upb_value_memory(&v);
  265. native_slot_get_by_map_value(intern->value_type, mem, retval TSRMLS_CC);
  266. return true;
  267. } else {
  268. zend_error(E_USER_ERROR, "Given key doesn't exist.");
  269. return false;
  270. }
  271. }
  272. static void map_index_unset(Map *intern, const char* keyval, int length) {
  273. upb_value old_value;
  274. if (upb_strtable_remove2(&intern->table, keyval, length, &old_value)) {
  275. switch (intern->value_type) {
  276. case UPB_TYPE_MESSAGE: {
  277. #if PHP_MAJOR_VERSION < 7
  278. zval_ptr_dtor(upb_value_memory(&old_value));
  279. #else
  280. zend_object* object = *(zend_object**)upb_value_memory(&old_value);
  281. GC_DELREF(object);
  282. if(GC_REFCOUNT(object) == 0) {
  283. zend_objects_store_del(object);
  284. }
  285. #endif
  286. break;
  287. }
  288. case UPB_TYPE_STRING:
  289. case UPB_TYPE_BYTES: {
  290. #if PHP_MAJOR_VERSION < 7
  291. zval_ptr_dtor(upb_value_memory(&old_value));
  292. #else
  293. zend_string* object = *(zend_string**)upb_value_memory(&old_value);
  294. zend_string_release(object);
  295. #endif
  296. break;
  297. }
  298. default:
  299. break;
  300. }
  301. }
  302. }
  303. bool map_index_set(Map *intern, const char* keyval, int length, upb_value v) {
  304. // Replace any existing value by issuing a 'remove' operation first.
  305. map_index_unset(intern, keyval, length);
  306. if (!upb_strtable_insert2(&intern->table, keyval, length, v)) {
  307. zend_error(E_USER_ERROR, "Could not insert into table");
  308. return false;
  309. }
  310. return true;
  311. }
  312. static void map_field_write_dimension(zval *object, zval *key,
  313. zval *value TSRMLS_DC) {
  314. Map *intern = UNBOX(Map, object);
  315. char keybuf[TABLE_KEY_BUF_LENGTH];
  316. const char* keyval = NULL;
  317. size_t length = 0;
  318. upb_value v;
  319. void* mem;
  320. if (!table_key(intern, key, keybuf, &keyval, &length TSRMLS_CC)) {
  321. return;
  322. }
  323. mem = upb_value_memory(&v);
  324. memset(mem, 0, native_slot_size(intern->value_type));
  325. if (!native_slot_set_by_map(intern->value_type, intern->msg_ce, mem,
  326. value TSRMLS_CC)) {
  327. return;
  328. }
  329. #ifndef NDEBUG
  330. v.ctype = UPB_CTYPE_UINT64;
  331. #endif
  332. map_index_set(intern, keyval, length, v);
  333. }
  334. static bool map_field_unset_dimension(zval *object, zval *key TSRMLS_DC) {
  335. Map *intern = UNBOX(Map, object);
  336. char keybuf[TABLE_KEY_BUF_LENGTH];
  337. const char* keyval = NULL;
  338. size_t length = 0;
  339. if (!table_key(intern, key, keybuf, &keyval, &length TSRMLS_CC)) {
  340. return false;
  341. }
  342. #ifndef NDEBUG
  343. v.ctype = UPB_CTYPE_UINT64;
  344. #endif
  345. map_index_unset(intern, keyval, length);
  346. return true;
  347. }
  348. // -----------------------------------------------------------------------------
  349. // PHP MapField Methods
  350. // -----------------------------------------------------------------------------
  351. PHP_METHOD(MapField, __construct) {
  352. long key_type, value_type;
  353. zend_class_entry* klass = NULL;
  354. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll|C", &key_type,
  355. &value_type, &klass) == FAILURE) {
  356. return;
  357. }
  358. Map *intern = UNBOX(Map, getThis());
  359. intern->key_type = to_fieldtype(key_type);
  360. intern->value_type = to_fieldtype(value_type);
  361. intern->msg_ce = klass;
  362. // Check that the key type is an allowed type.
  363. switch (intern->key_type) {
  364. case UPB_TYPE_INT32:
  365. case UPB_TYPE_INT64:
  366. case UPB_TYPE_UINT32:
  367. case UPB_TYPE_UINT64:
  368. case UPB_TYPE_BOOL:
  369. case UPB_TYPE_STRING:
  370. case UPB_TYPE_BYTES:
  371. // These are OK.
  372. break;
  373. default:
  374. zend_error(E_USER_ERROR, "Invalid key type for map.");
  375. }
  376. }
  377. PHP_METHOD(MapField, offsetExists) {
  378. zval *key;
  379. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &key) ==
  380. FAILURE) {
  381. return;
  382. }
  383. Map *intern = UNBOX(Map, getThis());
  384. char keybuf[TABLE_KEY_BUF_LENGTH];
  385. const char* keyval = NULL;
  386. size_t length = 0;
  387. upb_value v;
  388. #ifndef NDEBUG
  389. v.ctype = UPB_CTYPE_UINT64;
  390. #endif
  391. if (!table_key(intern, key, keybuf, &keyval, &length TSRMLS_CC)) {
  392. RETURN_BOOL(false);
  393. }
  394. RETURN_BOOL(upb_strtable_lookup2(&intern->table, keyval, length, &v));
  395. }
  396. PHP_METHOD(MapField, offsetGet) {
  397. zval *index;
  398. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &index) ==
  399. FAILURE) {
  400. return;
  401. }
  402. map_field_read_dimension(getThis(), index, BP_VAR_R,
  403. ZVAL_PTR_TO_CACHED_PTR(return_value) TSRMLS_CC);
  404. }
  405. PHP_METHOD(MapField, offsetSet) {
  406. zval *index, *value;
  407. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &index, &value) ==
  408. FAILURE) {
  409. return;
  410. }
  411. map_field_write_dimension(getThis(), index, value TSRMLS_CC);
  412. }
  413. PHP_METHOD(MapField, offsetUnset) {
  414. zval *index;
  415. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &index) ==
  416. FAILURE) {
  417. return;
  418. }
  419. map_field_unset_dimension(getThis(), index TSRMLS_CC);
  420. }
  421. PHP_METHOD(MapField, count) {
  422. Map *intern = UNBOX(Map, getThis());
  423. if (zend_parse_parameters_none() == FAILURE) {
  424. return;
  425. }
  426. RETURN_LONG(upb_strtable_count(&intern->table));
  427. }
  428. PHP_METHOD(MapField, getIterator) {
  429. CREATE_OBJ_ON_ALLOCATED_ZVAL_PTR(return_value,
  430. map_field_iter_type);
  431. MapIter *iter = UNBOX(MapIter, return_value);
  432. map_begin(getThis(), iter TSRMLS_CC);
  433. }
  434. // -----------------------------------------------------------------------------
  435. // Map Iterator
  436. // -----------------------------------------------------------------------------
  437. void map_begin(zval *map_php, MapIter *iter TSRMLS_DC) {
  438. Map *self = UNBOX(Map, map_php);
  439. map_begin_internal(self, iter);
  440. }
  441. void map_next(MapIter *iter) {
  442. upb_strtable_next(&iter->it);
  443. }
  444. bool map_done(MapIter *iter) {
  445. return upb_strtable_done(&iter->it);
  446. }
  447. const char *map_iter_key(MapIter *iter, int *len) {
  448. *len = upb_strtable_iter_keylength(&iter->it);
  449. return upb_strtable_iter_key(&iter->it);
  450. }
  451. upb_value map_iter_value(MapIter *iter, int *len) {
  452. *len = native_slot_size(iter->self->value_type);
  453. return upb_strtable_iter_value(&iter->it);
  454. }
  455. // -----------------------------------------------------------------------------
  456. // MapFieldIter methods
  457. // -----------------------------------------------------------------------------
  458. static zend_function_entry map_field_iter_methods[] = {
  459. PHP_ME(MapFieldIter, rewind, arginfo_void, ZEND_ACC_PUBLIC)
  460. PHP_ME(MapFieldIter, current, arginfo_void, ZEND_ACC_PUBLIC)
  461. PHP_ME(MapFieldIter, key, arginfo_void, ZEND_ACC_PUBLIC)
  462. PHP_ME(MapFieldIter, next, arginfo_void, ZEND_ACC_PUBLIC)
  463. PHP_ME(MapFieldIter, valid, arginfo_void, ZEND_ACC_PUBLIC)
  464. ZEND_FE_END
  465. };
  466. // -----------------------------------------------------------------------------
  467. // MapFieldIter creation/destruction
  468. // -----------------------------------------------------------------------------
  469. // Define object free method.
  470. PHP_PROTO_OBJECT_EMPTY_FREE_START(MapIter, map_field_iter)
  471. PHP_PROTO_OBJECT_FREE_END
  472. PHP_PROTO_OBJECT_EMPTY_DTOR_START(MapIter, map_field_iter)
  473. PHP_PROTO_OBJECT_DTOR_END
  474. // Define object create method.
  475. PHP_PROTO_OBJECT_CREATE_START(MapIter, map_field_iter)
  476. intern->self = NULL;
  477. PHP_PROTO_OBJECT_CREATE_END(MapIter, map_field_iter)
  478. // Init class entry.
  479. PHP_PROTO_INIT_CLASS_START("Google\\Protobuf\\Internal\\MapFieldIter",
  480. MapIter, map_field_iter)
  481. zend_class_implements(map_field_iter_type TSRMLS_CC, 1, zend_ce_iterator);
  482. PHP_PROTO_INIT_CLASS_END
  483. // -----------------------------------------------------------------------------
  484. // PHP MapFieldIter Methods
  485. // -----------------------------------------------------------------------------
  486. PHP_METHOD(MapFieldIter, rewind) {
  487. MapIter *intern = UNBOX(MapIter, getThis());
  488. map_begin_internal(intern->self, intern);
  489. }
  490. PHP_METHOD(MapFieldIter, current) {
  491. MapIter *intern = UNBOX(MapIter, getThis());
  492. Map *map_field = intern->self;
  493. int value_length = 0;
  494. upb_value value = map_iter_value(intern, &value_length);
  495. void* mem = upb_value_memory(&value);
  496. native_slot_get_by_map_value(map_field->value_type, mem,
  497. ZVAL_PTR_TO_CACHED_PTR(return_value) TSRMLS_CC);
  498. }
  499. PHP_METHOD(MapFieldIter, key) {
  500. MapIter *intern = UNBOX(MapIter, getThis());
  501. Map *map_field = intern->self;
  502. int key_length = 0;
  503. const char* key = map_iter_key(intern, &key_length);
  504. native_slot_get_by_map_key(map_field->key_type, key, key_length,
  505. ZVAL_PTR_TO_CACHED_PTR(return_value) TSRMLS_CC);
  506. }
  507. PHP_METHOD(MapFieldIter, next) {
  508. MapIter *intern = UNBOX(MapIter, getThis());
  509. map_next(intern);
  510. }
  511. PHP_METHOD(MapFieldIter, valid) {
  512. MapIter *intern = UNBOX(MapIter, getThis());
  513. RETURN_BOOL(!map_done(intern));
  514. }