array.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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 zend_object_value repeated_field_create(zend_class_entry *ce TSRMLS_DC);
  64. static void repeated_field_free(void *object TSRMLS_DC);
  65. static int repeated_field_array_init(zval *array, upb_fieldtype_t type,
  66. uint size ZEND_FILE_LINE_DC);
  67. static void repeated_field_free_element(void *object);
  68. static void repeated_field_write_dimension(zval *object, zval *offset,
  69. zval *value TSRMLS_DC);
  70. static int repeated_field_has_dimension(zval *object, zval *offset TSRMLS_DC);
  71. static HashTable *repeated_field_get_gc(zval *object, zval ***table,
  72. int *n TSRMLS_DC);
  73. static zend_object_value repeated_field_iter_create(zend_class_entry *ce TSRMLS_DC);
  74. static void repeated_field_iter_free(void *object TSRMLS_DC);
  75. // -----------------------------------------------------------------------------
  76. // RepeatedField creation/desctruction
  77. // -----------------------------------------------------------------------------
  78. zend_class_entry* repeated_field_type;
  79. zend_class_entry* repeated_field_iter_type;
  80. zend_object_handlers* repeated_field_handlers;
  81. void repeated_field_init(TSRMLS_D) {
  82. zend_class_entry class_type;
  83. const char* class_name = "Google\\Protobuf\\Internal\\RepeatedField";
  84. INIT_CLASS_ENTRY_EX(class_type, class_name, strlen(class_name),
  85. repeated_field_methods);
  86. repeated_field_type = zend_register_internal_class(&class_type TSRMLS_CC);
  87. repeated_field_type->create_object = repeated_field_create;
  88. zend_class_implements(repeated_field_type TSRMLS_CC, 3, spl_ce_ArrayAccess,
  89. zend_ce_aggregate, spl_ce_Countable);
  90. repeated_field_handlers = PEMALLOC(zend_object_handlers);
  91. memcpy(repeated_field_handlers, zend_get_std_object_handlers(),
  92. sizeof(zend_object_handlers));
  93. repeated_field_handlers->get_gc = repeated_field_get_gc;
  94. }
  95. static zend_object_value repeated_field_create(zend_class_entry *ce TSRMLS_DC) {
  96. zend_object_value retval = {0};
  97. RepeatedField *intern;
  98. intern = emalloc(sizeof(RepeatedField));
  99. memset(intern, 0, sizeof(RepeatedField));
  100. zend_object_std_init(&intern->std, ce TSRMLS_CC);
  101. object_properties_init(&intern->std, ce);
  102. intern->array = NULL;
  103. intern->type = 0;
  104. intern->msg_ce = NULL;
  105. retval.handle = zend_objects_store_put(
  106. intern, (zend_objects_store_dtor_t)zend_objects_destroy_object,
  107. (zend_objects_free_object_storage_t)repeated_field_free, NULL TSRMLS_CC);
  108. retval.handlers = repeated_field_handlers;
  109. return retval;
  110. }
  111. static void repeated_field_free(void *object TSRMLS_DC) {
  112. RepeatedField *intern = object;
  113. zend_object_std_dtor(&intern->std TSRMLS_CC);
  114. zval_ptr_dtor(&intern->array);
  115. efree(object);
  116. }
  117. static int repeated_field_array_init(zval *array, upb_fieldtype_t type,
  118. uint size ZEND_FILE_LINE_DC) {
  119. ALLOC_HASHTABLE(Z_ARRVAL_P(array));
  120. switch (type) {
  121. case UPB_TYPE_STRING:
  122. case UPB_TYPE_BYTES:
  123. case UPB_TYPE_MESSAGE:
  124. zend_hash_init(Z_ARRVAL_P(array), size, NULL, ZVAL_PTR_DTOR, 0);
  125. break;
  126. default:
  127. zend_hash_init(Z_ARRVAL_P(array), size, NULL, repeated_field_free_element,
  128. 0);
  129. }
  130. Z_TYPE_P(array) = IS_ARRAY;
  131. return SUCCESS;
  132. }
  133. static void repeated_field_free_element(void *object) {
  134. }
  135. // -----------------------------------------------------------------------------
  136. // RepeatedField Handlers
  137. // -----------------------------------------------------------------------------
  138. static void repeated_field_write_dimension(zval *object, zval *offset,
  139. zval *value TSRMLS_DC) {
  140. uint64_t index;
  141. RepeatedField *intern = zend_object_store_get_object(object TSRMLS_CC);
  142. HashTable *ht = HASH_OF(intern->array);
  143. int size = native_slot_size(intern->type);
  144. unsigned char memory[NATIVE_SLOT_MAX_SIZE];
  145. memset(memory, 0, NATIVE_SLOT_MAX_SIZE);
  146. if (!native_slot_set(intern->type, intern->msg_ce, memory, value TSRMLS_CC)) {
  147. return;
  148. }
  149. if (!offset || Z_TYPE_P(offset) == IS_NULL) {
  150. index = zend_hash_num_elements(HASH_OF(intern->array));
  151. } else {
  152. if (protobuf_convert_to_uint64(offset, &index)) {
  153. if (!zend_hash_index_exists(ht, index)) {
  154. zend_error(E_USER_ERROR, "Element at %llu doesn't exist.\n", index);
  155. return;
  156. }
  157. } else {
  158. return;
  159. }
  160. }
  161. zend_hash_index_update(ht, index, memory, size, NULL);
  162. }
  163. static HashTable *repeated_field_get_gc(zval *object, zval ***table,
  164. int *n TSRMLS_DC) {
  165. *table = NULL;
  166. *n = 0;
  167. RepeatedField *intern = zend_object_store_get_object(object TSRMLS_CC);
  168. return HASH_OF(intern->array);
  169. }
  170. // -----------------------------------------------------------------------------
  171. // C RepeatedField Utilities
  172. // -----------------------------------------------------------------------------
  173. void *repeated_field_index_native(RepeatedField *intern, int index TSRMLS_DC) {
  174. HashTable *ht = HASH_OF(intern->array);
  175. void *value;
  176. if (zend_hash_index_find(ht, index, (void **)&value) == FAILURE) {
  177. zend_error(E_USER_ERROR, "Element at %d doesn't exist.\n", index);
  178. return NULL;
  179. }
  180. return value;
  181. }
  182. void repeated_field_push_native(RepeatedField *intern, void *value TSRMLS_DC) {
  183. HashTable *ht = HASH_OF(intern->array);
  184. int size = native_slot_size(intern->type);
  185. zend_hash_next_index_insert(ht, (void **)value, size, NULL);
  186. }
  187. void repeated_field_create_with_type(zend_class_entry *ce,
  188. const upb_fielddef *field,
  189. zval **repeated_field TSRMLS_DC) {
  190. MAKE_STD_ZVAL(*repeated_field);
  191. Z_TYPE_PP(repeated_field) = IS_OBJECT;
  192. Z_OBJVAL_PP(repeated_field) =
  193. repeated_field_type->create_object(repeated_field_type TSRMLS_CC);
  194. RepeatedField *intern =
  195. zend_object_store_get_object(*repeated_field TSRMLS_CC);
  196. intern->type = upb_fielddef_type(field);
  197. if (intern->type == UPB_TYPE_MESSAGE) {
  198. const upb_msgdef *msg = upb_fielddef_msgsubdef(field);
  199. zval *desc_php = get_def_obj(msg);
  200. Descriptor *desc = zend_object_store_get_object(desc_php TSRMLS_CC);
  201. intern->msg_ce = desc->klass;
  202. }
  203. MAKE_STD_ZVAL(intern->array);
  204. repeated_field_array_init(intern->array, intern->type, 0 ZEND_FILE_LINE_CC);
  205. // TODO(teboring): Link class entry for message and enum
  206. }
  207. // -----------------------------------------------------------------------------
  208. // PHP RepeatedField Methods
  209. // -----------------------------------------------------------------------------
  210. /**
  211. * Constructs an instance of RepeatedField.
  212. * @param long Type of the stored element.
  213. * @param string Message/Enum class name (message/enum fields only).
  214. */
  215. PHP_METHOD(RepeatedField, __construct) {
  216. long type;
  217. zend_class_entry* klass = NULL;
  218. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|C", &type, &klass) ==
  219. FAILURE) {
  220. return;
  221. }
  222. RepeatedField *intern = zend_object_store_get_object(getThis() TSRMLS_CC);
  223. intern->type = to_fieldtype(type);
  224. intern->msg_ce = klass;
  225. MAKE_STD_ZVAL(intern->array);
  226. repeated_field_array_init(intern->array, intern->type, 0 ZEND_FILE_LINE_CC);
  227. if (intern->type == UPB_TYPE_MESSAGE && klass == NULL) {
  228. zend_error(E_USER_ERROR, "Message type must have concrete class.");
  229. return;
  230. }
  231. // TODO(teboring): Consider enum.
  232. }
  233. /**
  234. * Append element to the end of the repeated field.
  235. * @param object The element to be added.
  236. */
  237. PHP_METHOD(RepeatedField, append) {
  238. zval *value;
  239. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &value) ==
  240. FAILURE) {
  241. return;
  242. }
  243. repeated_field_write_dimension(getThis(), NULL, value TSRMLS_CC);
  244. }
  245. /**
  246. * Check whether the element at given index exists.
  247. * @param long The index to be checked.
  248. * @return bool True if the element at the given index exists.
  249. */
  250. PHP_METHOD(RepeatedField, offsetExists) {
  251. long index;
  252. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) ==
  253. FAILURE) {
  254. return;
  255. }
  256. RepeatedField *intern = zend_object_store_get_object(getThis() TSRMLS_CC);
  257. RETURN_BOOL(index >= 0 &&
  258. index < zend_hash_num_elements(HASH_OF(intern->array)));
  259. }
  260. /**
  261. * Return the element at the given index.
  262. * This will also be called for: $ele = $arr[0]
  263. * @param long The index of the element to be fetched.
  264. * @return object The stored element at given index.
  265. * @exception Invalid type for index.
  266. * @exception Non-existing index.
  267. */
  268. PHP_METHOD(RepeatedField, offsetGet) {
  269. long index;
  270. void *memory;
  271. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) ==
  272. FAILURE) {
  273. return;
  274. }
  275. RepeatedField *intern = zend_object_store_get_object(getThis() TSRMLS_CC);
  276. HashTable *table = HASH_OF(intern->array);
  277. if (zend_hash_index_find(table, index, (void **)&memory) == FAILURE) {
  278. zend_error(E_USER_ERROR, "Element at %ld doesn't exist.\n", index);
  279. return;
  280. }
  281. native_slot_get(intern->type, memory, return_value_ptr TSRMLS_CC);
  282. }
  283. /**
  284. * Assign the element at the given index.
  285. * This will also be called for: $arr []= $ele and $arr[0] = ele
  286. * @param long The index of the element to be assigned.
  287. * @param object The element to be assigned.
  288. * @exception Invalid type for index.
  289. * @exception Non-existing index.
  290. * @exception Incorrect type of the element.
  291. */
  292. PHP_METHOD(RepeatedField, offsetSet) {
  293. zval *index, *value;
  294. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &index, &value) ==
  295. FAILURE) {
  296. return;
  297. }
  298. repeated_field_write_dimension(getThis(), index, value TSRMLS_CC);
  299. }
  300. /**
  301. * Remove the element at the given index.
  302. * This will also be called for: unset($arr)
  303. * @param long The index of the element to be removed.
  304. * @exception Invalid type for index.
  305. * @exception The element to be removed is not at the end of the RepeatedField.
  306. */
  307. PHP_METHOD(RepeatedField, offsetUnset) {
  308. long index;
  309. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) ==
  310. FAILURE) {
  311. return;
  312. }
  313. RepeatedField *intern = zend_object_store_get_object(getThis() TSRMLS_CC);
  314. // Only the element at the end of the array can be removed.
  315. if (index == -1 ||
  316. index != (zend_hash_num_elements(HASH_OF(intern->array)) - 1)) {
  317. zend_error(E_USER_ERROR, "Cannot remove element at %ld.\n", index);
  318. return;
  319. }
  320. zend_hash_index_del(HASH_OF(intern->array), index);
  321. }
  322. /**
  323. * Return the number of stored elements.
  324. * This will also be called for: count($arr)
  325. * @return long The number of stored elements.
  326. */
  327. PHP_METHOD(RepeatedField, count) {
  328. RepeatedField *intern = zend_object_store_get_object(getThis() TSRMLS_CC);
  329. if (zend_parse_parameters_none() == FAILURE) {
  330. return;
  331. }
  332. RETURN_LONG(zend_hash_num_elements(HASH_OF(intern->array)));
  333. }
  334. /**
  335. * Return the beginning iterator.
  336. * This will also be called for: foreach($arr)
  337. * @return object Beginning iterator.
  338. */
  339. PHP_METHOD(RepeatedField, getIterator) {
  340. zval *iter_php = NULL;
  341. MAKE_STD_ZVAL(iter_php);
  342. Z_TYPE_P(iter_php) = IS_OBJECT;
  343. Z_OBJVAL_P(iter_php) = repeated_field_iter_type->create_object(
  344. repeated_field_iter_type TSRMLS_CC);
  345. RepeatedField *intern = zend_object_store_get_object(getThis() TSRMLS_CC);
  346. RepeatedFieldIter *iter = zend_object_store_get_object(iter_php TSRMLS_CC);
  347. iter->repeated_field = intern;
  348. iter->position = 0;
  349. RETURN_ZVAL(iter_php, 1, 1);
  350. }
  351. // -----------------------------------------------------------------------------
  352. // RepeatedFieldIter creation/desctruction
  353. // -----------------------------------------------------------------------------
  354. void repeated_field_iter_init(TSRMLS_D) {
  355. zend_class_entry class_type;
  356. const char* class_name = "Google\\Protobuf\\Internal\\RepeatedFieldIter";
  357. INIT_CLASS_ENTRY_EX(class_type, class_name, strlen(class_name),
  358. repeated_field_iter_methods);
  359. repeated_field_iter_type =
  360. zend_register_internal_class(&class_type TSRMLS_CC);
  361. repeated_field_iter_type->create_object = repeated_field_iter_create;
  362. zend_class_implements(repeated_field_iter_type TSRMLS_CC, 1,
  363. zend_ce_iterator);
  364. }
  365. static zend_object_value repeated_field_iter_create(
  366. zend_class_entry *ce TSRMLS_DC) {
  367. zend_object_value retval = {0};
  368. RepeatedFieldIter *intern;
  369. intern = emalloc(sizeof(RepeatedFieldIter));
  370. memset(intern, 0, sizeof(RepeatedFieldIter));
  371. zend_object_std_init(&intern->std, ce TSRMLS_CC);
  372. object_properties_init(&intern->std, ce);
  373. intern->repeated_field = NULL;
  374. intern->position = 0;
  375. retval.handle = zend_objects_store_put(
  376. intern, (zend_objects_store_dtor_t)zend_objects_destroy_object,
  377. (zend_objects_free_object_storage_t)repeated_field_iter_free,
  378. NULL TSRMLS_CC);
  379. retval.handlers = zend_get_std_object_handlers();
  380. return retval;
  381. }
  382. static void repeated_field_iter_free(void *object TSRMLS_DC) {
  383. RepeatedFieldIter *intern = object;
  384. zend_object_std_dtor(&intern->std TSRMLS_CC);
  385. efree(object);
  386. }
  387. // -----------------------------------------------------------------------------
  388. // PHP RepeatedFieldIter Methods
  389. // -----------------------------------------------------------------------------
  390. PHP_METHOD(RepeatedFieldIter, rewind) {
  391. RepeatedFieldIter *intern = zend_object_store_get_object(getThis() TSRMLS_CC);
  392. intern->position = 0;
  393. }
  394. PHP_METHOD(RepeatedFieldIter, current) {
  395. RepeatedFieldIter *intern = zend_object_store_get_object(getThis() TSRMLS_CC);
  396. RepeatedField *repeated_field = intern->repeated_field;
  397. long index;
  398. void *memory;
  399. HashTable *table = HASH_OF(repeated_field->array);
  400. if (zend_hash_index_find(table, intern->position, (void **)&memory) ==
  401. FAILURE) {
  402. zend_error(E_USER_ERROR, "Element at %ld doesn't exist.\n", index);
  403. return;
  404. }
  405. native_slot_get(repeated_field->type, memory, return_value_ptr TSRMLS_CC);
  406. }
  407. PHP_METHOD(RepeatedFieldIter, key) {
  408. RepeatedFieldIter *intern = zend_object_store_get_object(getThis() TSRMLS_CC);
  409. RETURN_LONG(intern->position);
  410. }
  411. PHP_METHOD(RepeatedFieldIter, next) {
  412. RepeatedFieldIter *intern = zend_object_store_get_object(getThis() TSRMLS_CC);
  413. ++intern->position;
  414. }
  415. PHP_METHOD(RepeatedFieldIter, valid) {
  416. RepeatedFieldIter *intern = zend_object_store_get_object(getThis() TSRMLS_CC);
  417. RETURN_BOOL(zend_hash_num_elements(HASH_OF(intern->repeated_field->array)) >
  418. intern->position);
  419. }