array.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. ZEND_FE_END
  52. };
  53. // Forward declare static functions.
  54. static zend_object_value repeated_field_create(zend_class_entry *ce TSRMLS_DC);
  55. static void repeated_field_free(void *object TSRMLS_DC);
  56. static int repeated_field_array_init(zval *array, upb_fieldtype_t type,
  57. uint size ZEND_FILE_LINE_DC);
  58. static void repeated_field_free_element(void *object);
  59. static void repeated_field_write_dimension(zval *object, zval *offset,
  60. zval *value TSRMLS_DC);
  61. static int repeated_field_has_dimension(zval *object, zval *offset TSRMLS_DC);
  62. static HashTable *repeated_field_get_gc(zval *object, zval ***table,
  63. int *n TSRMLS_DC);
  64. // -----------------------------------------------------------------------------
  65. // RepeatedField creation/desctruction
  66. // -----------------------------------------------------------------------------
  67. zend_class_entry* repeated_field_type;
  68. zend_object_handlers* repeated_field_handlers;
  69. void repeated_field_init(TSRMLS_D) {
  70. zend_class_entry class_type;
  71. const char* class_name = "Google\\Protobuf\\Internal\\RepeatedField";
  72. INIT_CLASS_ENTRY_EX(class_type, class_name, strlen(class_name),
  73. repeated_field_methods);
  74. repeated_field_type = zend_register_internal_class(&class_type TSRMLS_CC);
  75. repeated_field_type->create_object = repeated_field_create;
  76. zend_class_implements(repeated_field_type TSRMLS_CC, 2, spl_ce_ArrayAccess,
  77. spl_ce_Countable);
  78. repeated_field_handlers = PEMALLOC(zend_object_handlers);
  79. memcpy(repeated_field_handlers, zend_get_std_object_handlers(),
  80. sizeof(zend_object_handlers));
  81. repeated_field_handlers->get_gc = repeated_field_get_gc;
  82. }
  83. static zend_object_value repeated_field_create(zend_class_entry *ce TSRMLS_DC) {
  84. zend_object_value retval = {0};
  85. RepeatedField *intern;
  86. intern = emalloc(sizeof(RepeatedField));
  87. memset(intern, 0, sizeof(RepeatedField));
  88. zend_object_std_init(&intern->std, ce TSRMLS_CC);
  89. object_properties_init(&intern->std, ce);
  90. intern->array = NULL;
  91. intern->type = 0;
  92. intern->msg_ce = NULL;
  93. retval.handle = zend_objects_store_put(
  94. intern, (zend_objects_store_dtor_t)zend_objects_destroy_object,
  95. (zend_objects_free_object_storage_t)repeated_field_free, NULL TSRMLS_CC);
  96. retval.handlers = repeated_field_handlers;
  97. return retval;
  98. }
  99. static void repeated_field_free(void *object TSRMLS_DC) {
  100. RepeatedField *intern = object;
  101. zend_object_std_dtor(&intern->std TSRMLS_CC);
  102. zval_ptr_dtor(&intern->array);
  103. efree(object);
  104. }
  105. static int repeated_field_array_init(zval *array, upb_fieldtype_t type,
  106. uint size ZEND_FILE_LINE_DC) {
  107. ALLOC_HASHTABLE(Z_ARRVAL_P(array));
  108. switch (type) {
  109. case UPB_TYPE_STRING:
  110. case UPB_TYPE_BYTES:
  111. case UPB_TYPE_MESSAGE:
  112. zend_hash_init(Z_ARRVAL_P(array), size, NULL, ZVAL_PTR_DTOR, 0);
  113. break;
  114. default:
  115. zend_hash_init(Z_ARRVAL_P(array), size, NULL, repeated_field_free_element,
  116. 0);
  117. }
  118. Z_TYPE_P(array) = IS_ARRAY;
  119. return SUCCESS;
  120. }
  121. static void repeated_field_free_element(void *object) {
  122. }
  123. // -----------------------------------------------------------------------------
  124. // RepeatedField Handlers
  125. // -----------------------------------------------------------------------------
  126. static void repeated_field_write_dimension(zval *object, zval *offset,
  127. zval *value TSRMLS_DC) {
  128. uint64_t index;
  129. RepeatedField *intern = zend_object_store_get_object(object TSRMLS_CC);
  130. HashTable *ht = HASH_OF(intern->array);
  131. int size = native_slot_size(intern->type);
  132. unsigned char memory[NATIVE_SLOT_MAX_SIZE];
  133. memset(memory, 0, NATIVE_SLOT_MAX_SIZE);
  134. if (!native_slot_set(intern->type, intern->msg_ce, memory, value)) {
  135. return;
  136. }
  137. if (!offset || Z_TYPE_P(offset) == IS_NULL) {
  138. index = zend_hash_num_elements(HASH_OF(intern->array));
  139. } else {
  140. if (protobuf_convert_to_uint64(offset, &index)) {
  141. if (!zend_hash_index_exists(ht, index)) {
  142. zend_error(E_USER_ERROR, "Element at %d doesn't exist.\n", index);
  143. return;
  144. }
  145. } else {
  146. return;
  147. }
  148. }
  149. zend_hash_index_update(ht, index, memory, size, NULL);
  150. }
  151. static HashTable *repeated_field_get_gc(zval *object, zval ***table,
  152. int *n TSRMLS_DC) {
  153. *table = NULL;
  154. *n = 0;
  155. RepeatedField *intern = zend_object_store_get_object(object TSRMLS_CC);
  156. return HASH_OF(intern->array);
  157. }
  158. // -----------------------------------------------------------------------------
  159. // C RepeatedField Utilities
  160. // -----------------------------------------------------------------------------
  161. void *repeated_field_index_native(RepeatedField *intern, int index) {
  162. HashTable *ht = HASH_OF(intern->array);
  163. void *value;
  164. if (zend_hash_index_find(ht, index, (void **)&value) == FAILURE) {
  165. zend_error(E_USER_ERROR, "Element at %d doesn't exist.\n", index);
  166. return NULL;
  167. }
  168. return value;
  169. }
  170. void repeated_field_push_native(RepeatedField *intern, void *value TSRMLS_DC) {
  171. HashTable *ht = HASH_OF(intern->array);
  172. int size = native_slot_size(intern->type);
  173. zend_hash_next_index_insert(ht, (void **)value, size, NULL);
  174. }
  175. void repeated_field_create_with_type(zend_class_entry *ce,
  176. const upb_fielddef *field,
  177. zval **repeated_field TSRMLS_DC) {
  178. MAKE_STD_ZVAL(*repeated_field);
  179. Z_TYPE_PP(repeated_field) = IS_OBJECT;
  180. Z_OBJVAL_PP(repeated_field) =
  181. repeated_field_type->create_object(repeated_field_type TSRMLS_CC);
  182. RepeatedField *intern =
  183. zend_object_store_get_object(*repeated_field TSRMLS_CC);
  184. intern->type = upb_fielddef_type(field);
  185. if (intern->type == UPB_TYPE_MESSAGE) {
  186. upb_msgdef *msg = upb_fielddef_msgsubdef(field);
  187. zval *desc_php = get_def_obj(msg);
  188. Descriptor *desc = zend_object_store_get_object(desc_php TSRMLS_CC);
  189. intern->msg_ce = desc->klass;
  190. }
  191. MAKE_STD_ZVAL(intern->array);
  192. repeated_field_array_init(intern->array, intern->type, 0 ZEND_FILE_LINE_CC);
  193. // TODO(teboring): Link class entry for message and enum
  194. }
  195. // -----------------------------------------------------------------------------
  196. // PHP RepeatedField Methods
  197. // -----------------------------------------------------------------------------
  198. /**
  199. * Constructs an instance of RepeatedField.
  200. * @param long Type of the stored element.
  201. * @param string Message/Enum class name (message/enum fields only).
  202. */
  203. PHP_METHOD(RepeatedField, __construct) {
  204. long type;
  205. zend_class_entry* klass = NULL;
  206. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|C", &type, &klass) ==
  207. FAILURE) {
  208. return;
  209. }
  210. RepeatedField *intern = zend_object_store_get_object(getThis() TSRMLS_CC);
  211. intern->type = to_fieldtype(type);
  212. intern->msg_ce = klass;
  213. MAKE_STD_ZVAL(intern->array);
  214. repeated_field_array_init(intern->array, intern->type, 0 ZEND_FILE_LINE_CC);
  215. if (intern->type == UPB_TYPE_MESSAGE && klass == NULL) {
  216. zend_error(E_USER_ERROR, "Message type must have concrete class.");
  217. return;
  218. }
  219. // TODO(teboring): Consider enum.
  220. }
  221. /**
  222. * Append element to the end of the repeated field.
  223. * @param object The element to be added.
  224. */
  225. PHP_METHOD(RepeatedField, append) {
  226. zval *value;
  227. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &value) ==
  228. FAILURE) {
  229. return;
  230. }
  231. repeated_field_write_dimension(getThis(), NULL, value TSRMLS_CC);
  232. }
  233. /**
  234. * Check whether the element at given index exists.
  235. * @param long The index to be checked.
  236. * @return bool True if the element at the given index exists.
  237. */
  238. PHP_METHOD(RepeatedField, offsetExists) {
  239. long index;
  240. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) ==
  241. FAILURE) {
  242. return;
  243. }
  244. RepeatedField *intern = zend_object_store_get_object(getThis() TSRMLS_CC);
  245. RETURN_BOOL(index >= 0 &&
  246. index < zend_hash_num_elements(HASH_OF(intern->array)));
  247. }
  248. /**
  249. * Return the element at the given index.
  250. * This will also be called for: $ele = $arr[0]
  251. * @param long The index of the element to be fetched.
  252. * @return object The stored element at given index.
  253. * @exception Invalid type for index.
  254. * @exception Non-existing index.
  255. */
  256. PHP_METHOD(RepeatedField, offsetGet) {
  257. long index;
  258. void *memory;
  259. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) ==
  260. FAILURE) {
  261. return;
  262. }
  263. RepeatedField *intern = zend_object_store_get_object(getThis() TSRMLS_CC);
  264. HashTable *table = HASH_OF(intern->array);
  265. if (zend_hash_index_find(table, index, (void **)&memory) == FAILURE) {
  266. zend_error(E_USER_ERROR, "Element at %d doesn't exist.\n", index);
  267. return;
  268. }
  269. native_slot_get(intern->type, memory, return_value_ptr TSRMLS_CC);
  270. }
  271. /**
  272. * Assign the element at the given index.
  273. * This will also be called for: $arr []= $ele and $arr[0] = ele
  274. * @param long The index of the element to be assigned.
  275. * @param object The element to be assigned.
  276. * @exception Invalid type for index.
  277. * @exception Non-existing index.
  278. * @exception Incorrect type of the element.
  279. */
  280. PHP_METHOD(RepeatedField, offsetSet) {
  281. zval *index, *value;
  282. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &index, &value) ==
  283. FAILURE) {
  284. return;
  285. }
  286. repeated_field_write_dimension(getThis(), index, value TSRMLS_CC);
  287. }
  288. /**
  289. * Remove the element at the given index.
  290. * This will also be called for: unset($arr)
  291. * @param long The index of the element to be removed.
  292. * @exception Invalid type for index.
  293. * @exception The element to be removed is not at the end of the RepeatedField.
  294. */
  295. PHP_METHOD(RepeatedField, offsetUnset) {
  296. long index;
  297. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) ==
  298. FAILURE) {
  299. return;
  300. }
  301. RepeatedField *intern = zend_object_store_get_object(getThis() TSRMLS_CC);
  302. // Only the element at the end of the array can be removed.
  303. if (index == -1 ||
  304. index != (zend_hash_num_elements(HASH_OF(intern->array)) - 1)) {
  305. zend_error(E_USER_ERROR, "Cannot remove element at %d.\n", index);
  306. return;
  307. }
  308. zend_hash_index_del(HASH_OF(intern->array), index);
  309. }
  310. /**
  311. * Return the number of stored elements.
  312. * This will also be called for: count($arr)
  313. * @return long The number of stored elements.
  314. */
  315. PHP_METHOD(RepeatedField, count) {
  316. RepeatedField *intern = zend_object_store_get_object(getThis() TSRMLS_CC);
  317. if (zend_parse_parameters_none() == FAILURE) {
  318. return;
  319. }
  320. RETURN_LONG(zend_hash_num_elements(HASH_OF(intern->array)));
  321. }