array.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. void* ptr = Z_PTR_P(value);
  129. zend_object* object = *(zend_object**)ptr;
  130. if(--GC_REFCOUNT(object) == 0) {
  131. zend_objects_store_del(object);
  132. }
  133. efree(ptr);
  134. }
  135. static void php_proto_array_default_release(zval* value) {
  136. void* ptr = Z_PTR_P(value);
  137. efree(ptr);
  138. }
  139. #endif
  140. static int repeated_field_array_init(zval *array, upb_fieldtype_t type,
  141. uint size ZEND_FILE_LINE_DC) {
  142. PHP_PROTO_ALLOC_ARRAY(array);
  143. switch (type) {
  144. case UPB_TYPE_STRING:
  145. case UPB_TYPE_BYTES:
  146. zend_hash_init(Z_ARRVAL_P(array), size, NULL,
  147. php_proto_array_string_release, 0);
  148. break;
  149. case UPB_TYPE_MESSAGE:
  150. zend_hash_init(Z_ARRVAL_P(array), size, NULL,
  151. php_proto_array_object_release, 0);
  152. break;
  153. default:
  154. zend_hash_init(Z_ARRVAL_P(array), size, NULL,
  155. php_proto_array_default_release, 0);
  156. }
  157. return SUCCESS;
  158. }
  159. // -----------------------------------------------------------------------------
  160. // RepeatedField Handlers
  161. // -----------------------------------------------------------------------------
  162. static void repeated_field_write_dimension(zval *object, zval *offset,
  163. zval *value TSRMLS_DC) {
  164. uint64_t index;
  165. RepeatedField *intern = UNBOX(RepeatedField, object);
  166. HashTable *ht = PHP_PROTO_HASH_OF(intern->array);
  167. int size = native_slot_size(intern->type);
  168. unsigned char memory[NATIVE_SLOT_MAX_SIZE];
  169. memset(memory, 0, NATIVE_SLOT_MAX_SIZE);
  170. if (!native_slot_set_by_array(intern->type, intern->msg_ce, memory,
  171. value TSRMLS_CC)) {
  172. return;
  173. }
  174. if (!offset || Z_TYPE_P(offset) == IS_NULL) {
  175. index = zend_hash_num_elements(PHP_PROTO_HASH_OF(intern->array));
  176. } else {
  177. if (protobuf_convert_to_uint64(offset, &index)) {
  178. if (!zend_hash_index_exists(ht, index)) {
  179. zend_error(E_USER_ERROR, "Element at %llu doesn't exist.\n",
  180. (long long unsigned int)index);
  181. return;
  182. }
  183. } else {
  184. return;
  185. }
  186. }
  187. php_proto_zend_hash_index_update(ht, index, memory, size, NULL);
  188. }
  189. #if PHP_MAJOR_VERSION < 7
  190. static HashTable *repeated_field_get_gc(zval *object, zval ***table,
  191. int *n TSRMLS_DC) {
  192. #else
  193. static HashTable *repeated_field_get_gc(zval *object, zval **table, int *n) {
  194. #endif
  195. *table = NULL;
  196. *n = 0;
  197. RepeatedField *intern = UNBOX(RepeatedField, object);
  198. return PHP_PROTO_HASH_OF(intern->array);
  199. }
  200. // -----------------------------------------------------------------------------
  201. // C RepeatedField Utilities
  202. // -----------------------------------------------------------------------------
  203. void *repeated_field_index_native(RepeatedField *intern, int index TSRMLS_DC) {
  204. HashTable *ht = PHP_PROTO_HASH_OF(intern->array);
  205. void *value;
  206. if (php_proto_zend_hash_index_find(ht, index, (void **)&value) == FAILURE) {
  207. zend_error(E_USER_ERROR, "Element at %d doesn't exist.\n", index);
  208. return NULL;
  209. }
  210. return value;
  211. }
  212. void repeated_field_push_native(RepeatedField *intern, void *value) {
  213. HashTable *ht = PHP_PROTO_HASH_OF(intern->array);
  214. int size = native_slot_size(intern->type);
  215. php_proto_zend_hash_next_index_insert(ht, (void **)value, size, NULL);
  216. }
  217. void repeated_field_create_with_field(
  218. zend_class_entry *ce, const upb_fielddef *field,
  219. CACHED_VALUE *repeated_field PHP_PROTO_TSRMLS_DC) {
  220. upb_fieldtype_t type = upb_fielddef_type(field);
  221. const zend_class_entry *msg_ce = field_type_class(field PHP_PROTO_TSRMLS_CC);
  222. repeated_field_create_with_type(ce, type, msg_ce,
  223. repeated_field PHP_PROTO_TSRMLS_CC);
  224. }
  225. void repeated_field_create_with_type(
  226. zend_class_entry *ce, upb_fieldtype_t type, const zend_class_entry *msg_ce,
  227. CACHED_VALUE *repeated_field PHP_PROTO_TSRMLS_DC) {
  228. CREATE_OBJ_ON_ALLOCATED_ZVAL_PTR(CACHED_PTR_TO_ZVAL_PTR(repeated_field),
  229. repeated_field_type);
  230. RepeatedField *intern =
  231. UNBOX(RepeatedField, CACHED_TO_ZVAL_PTR(*repeated_field));
  232. intern->type = type;
  233. intern->msg_ce = msg_ce;
  234. #if PHP_MAJOR_VERSION < 7
  235. MAKE_STD_ZVAL(intern->array);
  236. repeated_field_array_init(intern->array, intern->type, 0 ZEND_FILE_LINE_CC);
  237. #else
  238. repeated_field_array_init(&intern->array, intern->type, 0 ZEND_FILE_LINE_CC);
  239. #endif
  240. // TODO(teboring): Link class entry for message and enum
  241. }
  242. // -----------------------------------------------------------------------------
  243. // PHP RepeatedField Methods
  244. // -----------------------------------------------------------------------------
  245. /**
  246. * Constructs an instance of RepeatedField.
  247. * @param long Type of the stored element.
  248. * @param string Message/Enum class name (message/enum fields only).
  249. */
  250. PHP_METHOD(RepeatedField, __construct) {
  251. long type;
  252. zend_class_entry* klass = NULL;
  253. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|C", &type, &klass) ==
  254. FAILURE) {
  255. return;
  256. }
  257. RepeatedField *intern = UNBOX(RepeatedField, getThis());
  258. intern->type = to_fieldtype(type);
  259. intern->msg_ce = klass;
  260. #if PHP_MAJOR_VERSION < 7
  261. MAKE_STD_ZVAL(intern->array);
  262. repeated_field_array_init(intern->array, intern->type, 0 ZEND_FILE_LINE_CC);
  263. #else
  264. repeated_field_array_init(&intern->array, intern->type, 0 ZEND_FILE_LINE_CC);
  265. #endif
  266. if (intern->type == UPB_TYPE_MESSAGE && klass == NULL) {
  267. zend_error(E_USER_ERROR, "Message type must have concrete class.");
  268. return;
  269. }
  270. // TODO(teboring): Consider enum.
  271. }
  272. /**
  273. * Append element to the end of the repeated field.
  274. * @param object The element to be added.
  275. */
  276. PHP_METHOD(RepeatedField, append) {
  277. zval *value;
  278. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &value) ==
  279. FAILURE) {
  280. return;
  281. }
  282. repeated_field_write_dimension(getThis(), NULL, value TSRMLS_CC);
  283. }
  284. /**
  285. * Check whether the element at given index exists.
  286. * @param long The index to be checked.
  287. * @return bool True if the element at the given index exists.
  288. */
  289. PHP_METHOD(RepeatedField, offsetExists) {
  290. long index;
  291. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) ==
  292. FAILURE) {
  293. return;
  294. }
  295. RepeatedField *intern = UNBOX(RepeatedField, getThis());
  296. RETURN_BOOL(index >= 0 &&
  297. index < zend_hash_num_elements(PHP_PROTO_HASH_OF(intern->array)));
  298. }
  299. /**
  300. * Return the element at the given index.
  301. * This will also be called for: $ele = $arr[0]
  302. * @param long The index of the element to be fetched.
  303. * @return object The stored element at given index.
  304. * @exception Invalid type for index.
  305. * @exception Non-existing index.
  306. */
  307. PHP_METHOD(RepeatedField, offsetGet) {
  308. long index;
  309. void *memory;
  310. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) ==
  311. FAILURE) {
  312. return;
  313. }
  314. RepeatedField *intern = UNBOX(RepeatedField, getThis());
  315. HashTable *table = PHP_PROTO_HASH_OF(intern->array);
  316. if (php_proto_zend_hash_index_find(table, index, (void **)&memory) == FAILURE) {
  317. zend_error(E_USER_ERROR, "Element at %ld doesn't exist.\n", index);
  318. return;
  319. }
  320. native_slot_get_by_array(intern->type, memory,
  321. ZVAL_PTR_TO_CACHED_PTR(return_value) TSRMLS_CC);
  322. }
  323. /**
  324. * Assign the element at the given index.
  325. * This will also be called for: $arr []= $ele and $arr[0] = ele
  326. * @param long The index of the element to be assigned.
  327. * @param object The element to be assigned.
  328. * @exception Invalid type for index.
  329. * @exception Non-existing index.
  330. * @exception Incorrect type of the element.
  331. */
  332. PHP_METHOD(RepeatedField, offsetSet) {
  333. zval *index, *value;
  334. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &index, &value) ==
  335. FAILURE) {
  336. return;
  337. }
  338. repeated_field_write_dimension(getThis(), index, value TSRMLS_CC);
  339. }
  340. /**
  341. * Remove the element at the given index.
  342. * This will also be called for: unset($arr)
  343. * @param long The index of the element to be removed.
  344. * @exception Invalid type for index.
  345. * @exception The element to be removed is not at the end of the RepeatedField.
  346. */
  347. PHP_METHOD(RepeatedField, offsetUnset) {
  348. long index;
  349. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) ==
  350. FAILURE) {
  351. return;
  352. }
  353. RepeatedField *intern = UNBOX(RepeatedField, getThis());
  354. // Only the element at the end of the array can be removed.
  355. if (index == -1 ||
  356. index != (zend_hash_num_elements(PHP_PROTO_HASH_OF(intern->array)) - 1)) {
  357. zend_error(E_USER_ERROR, "Cannot remove element at %ld.\n", index);
  358. return;
  359. }
  360. zend_hash_index_del(PHP_PROTO_HASH_OF(intern->array), index);
  361. }
  362. /**
  363. * Return the number of stored elements.
  364. * This will also be called for: count($arr)
  365. * @return long The number of stored elements.
  366. */
  367. PHP_METHOD(RepeatedField, count) {
  368. RepeatedField *intern = UNBOX(RepeatedField, getThis());
  369. if (zend_parse_parameters_none() == FAILURE) {
  370. return;
  371. }
  372. RETURN_LONG(zend_hash_num_elements(PHP_PROTO_HASH_OF(intern->array)));
  373. }
  374. /**
  375. * Return the beginning iterator.
  376. * This will also be called for: foreach($arr)
  377. * @return object Beginning iterator.
  378. */
  379. PHP_METHOD(RepeatedField, getIterator) {
  380. CREATE_OBJ_ON_ALLOCATED_ZVAL_PTR(return_value,
  381. repeated_field_iter_type);
  382. RepeatedField *intern = UNBOX(RepeatedField, getThis());
  383. RepeatedFieldIter *iter = UNBOX(RepeatedFieldIter, return_value);
  384. iter->repeated_field = intern;
  385. iter->position = 0;
  386. }
  387. // -----------------------------------------------------------------------------
  388. // RepeatedFieldIter creation/desctruction
  389. // -----------------------------------------------------------------------------
  390. // Define object free method.
  391. PHP_PROTO_OBJECT_FREE_START(RepeatedFieldIter, repeated_field_iter)
  392. PHP_PROTO_OBJECT_FREE_END
  393. PHP_PROTO_OBJECT_DTOR_START(RepeatedFieldIter, repeated_field_iter)
  394. PHP_PROTO_OBJECT_DTOR_END
  395. // Define object create method.
  396. PHP_PROTO_OBJECT_CREATE_START(RepeatedFieldIter, repeated_field_iter)
  397. intern->repeated_field = NULL;
  398. intern->position = 0;
  399. PHP_PROTO_OBJECT_CREATE_END(RepeatedFieldIter, repeated_field_iter)
  400. // Init class entry.
  401. PHP_PROTO_INIT_CLASS_START("Google\\Protobuf\\Internal\\RepeatedFieldIter",
  402. RepeatedFieldIter, repeated_field_iter)
  403. zend_class_implements(repeated_field_iter_type TSRMLS_CC, 1, zend_ce_iterator);
  404. PHP_PROTO_INIT_CLASS_END
  405. // -----------------------------------------------------------------------------
  406. // PHP RepeatedFieldIter Methods
  407. // -----------------------------------------------------------------------------
  408. PHP_METHOD(RepeatedFieldIter, rewind) {
  409. RepeatedFieldIter *intern = UNBOX(RepeatedFieldIter, getThis());
  410. intern->position = 0;
  411. }
  412. PHP_METHOD(RepeatedFieldIter, current) {
  413. RepeatedFieldIter *intern = UNBOX(RepeatedFieldIter, getThis());
  414. RepeatedField *repeated_field = intern->repeated_field;
  415. long index;
  416. void *memory;
  417. HashTable *table = PHP_PROTO_HASH_OF(repeated_field->array);
  418. if (php_proto_zend_hash_index_find(table, intern->position, (void **)&memory) ==
  419. FAILURE) {
  420. zend_error(E_USER_ERROR, "Element at %ld doesn't exist.\n", index);
  421. return;
  422. }
  423. native_slot_get_by_array(repeated_field->type, memory,
  424. ZVAL_PTR_TO_CACHED_PTR(return_value) TSRMLS_CC);
  425. }
  426. PHP_METHOD(RepeatedFieldIter, key) {
  427. RepeatedFieldIter *intern = UNBOX(RepeatedFieldIter, getThis());
  428. RETURN_LONG(intern->position);
  429. }
  430. PHP_METHOD(RepeatedFieldIter, next) {
  431. RepeatedFieldIter *intern = UNBOX(RepeatedFieldIter, getThis());
  432. ++intern->position;
  433. }
  434. PHP_METHOD(RepeatedFieldIter, valid) {
  435. RepeatedFieldIter *intern = UNBOX(RepeatedFieldIter, getThis());
  436. RETURN_BOOL(zend_hash_num_elements(PHP_PROTO_HASH_OF(
  437. intern->repeated_field->array)) > intern->position);
  438. }