protobuf.h 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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. #ifndef __GOOGLE_PROTOBUF_PHP_PROTOBUF_H__
  31. #define __GOOGLE_PROTOBUF_PHP_PROTOBUF_H__
  32. #include <php.h>
  33. // ubp.h has to be placed after php.h. Othwise, php.h will introduce NDEBUG.
  34. #include "upb.h"
  35. #define PHP_PROTOBUF_EXTNAME "protobuf"
  36. #define PHP_PROTOBUF_VERSION "3.4.0"
  37. #define MAX_LENGTH_OF_INT64 20
  38. #define SIZEOF_INT64 8
  39. // -----------------------------------------------------------------------------
  40. // PHP7 Wrappers
  41. // ----------------------------------------------------------------------------
  42. #if PHP_MAJOR_VERSION < 7
  43. #define php_proto_zend_literal const zend_literal*
  44. #define PHP_PROTO_CASE_IS_BOOL IS_BOOL
  45. #define PHP_PROTO_SIZE int
  46. #define PHP_PROTO_LONG long
  47. #define PHP_PROTO_TSRMLS_DC TSRMLS_DC
  48. #define PHP_PROTO_TSRMLS_CC TSRMLS_CC
  49. // PHP String
  50. #define PHP_PROTO_ZVAL_STRING(zval_ptr, s, copy) \
  51. ZVAL_STRING(zval_ptr, s, copy)
  52. #define PHP_PROTO_ZVAL_STRINGL(zval_ptr, s, len, copy) \
  53. ZVAL_STRINGL(zval_ptr, s, len, copy)
  54. #define PHP_PROTO_RETURN_STRING(s, copy) RETURN_STRING(s, copy)
  55. #define PHP_PROTO_RETURN_STRINGL(s, len, copy) RETURN_STRINGL(s, len, copy)
  56. #define PHP_PROTO_RETVAL_STRINGL(s, len, copy) RETVAL_STRINGL(s, len, copy)
  57. #define php_proto_zend_make_printable_zval(from, to) \
  58. { \
  59. int use_copy; \
  60. zend_make_printable_zval(from, to, &use_copy); \
  61. }
  62. // PHP Array
  63. #define PHP_PROTO_HASH_OF(array) Z_ARRVAL_P(array)
  64. #define php_proto_zend_hash_index_update_zval(ht, h, pData) \
  65. zend_hash_index_update(ht, h, &(pData), sizeof(void*), NULL)
  66. #define php_proto_zend_hash_update_zval(ht, key, key_len, value) \
  67. zend_hash_update(ht, key, key_len, value, sizeof(void*), NULL)
  68. #define php_proto_zend_hash_index_update_mem(ht, h, pData, nDataSize, pDest) \
  69. zend_hash_index_update(ht, h, pData, nDataSize, pDest)
  70. #define php_proto_zend_hash_update_mem(ht, key, key_len, pData, nDataSize, \
  71. pDest) \
  72. zend_hash_update(ht, key, key_len, pData, nDataSize, pDest)
  73. #define php_proto_zend_hash_index_find_zval(ht, h, pDest) \
  74. zend_hash_index_find(ht, h, pDest)
  75. #define php_proto_zend_hash_index_find_mem(ht, h, pDest) \
  76. zend_hash_index_find(ht, h, pDest)
  77. #define php_proto_zend_hash_find_zval(ht, key, key_len, pDest) \
  78. zend_hash_find(ht, key, key_len, pDest)
  79. #define php_proto_zend_hash_find_mem(ht, key, key_len, pDest) \
  80. zend_hash_find(ht, key, key_len, pDest)
  81. #define php_proto_zend_hash_next_index_insert_zval(ht, pData) \
  82. zend_hash_next_index_insert(ht, pData, sizeof(void*), NULL)
  83. #define php_proto_zend_hash_next_index_insert_mem(ht, pData, nDataSize, pDest) \
  84. zend_hash_next_index_insert(ht, pData, nDataSize, pDest)
  85. #define php_proto_zend_hash_get_current_data_ex(ht, pDest, pos) \
  86. zend_hash_get_current_data_ex(ht, pDest, pos)
  87. // PHP Object
  88. #define PHP_PROTO_WRAP_OBJECT_START(name) \
  89. struct name { \
  90. zend_object std;
  91. #define PHP_PROTO_WRAP_OBJECT_END \
  92. };
  93. #define PHP_PROTO_INIT_SUBMSGCLASS_START(CLASSNAME, CAMELNAME, LOWWERNAME) \
  94. void LOWWERNAME##_init(TSRMLS_D) { \
  95. zend_class_entry class_type; \
  96. const char* class_name = CLASSNAME; \
  97. INIT_CLASS_ENTRY_EX(class_type, CLASSNAME, strlen(CLASSNAME), \
  98. LOWWERNAME##_methods); \
  99. LOWWERNAME##_type = zend_register_internal_class(&class_type TSRMLS_CC); \
  100. LOWWERNAME##_type->create_object = message_create;
  101. #define PHP_PROTO_INIT_SUBMSGCLASS_END \
  102. }
  103. #define PHP_PROTO_INIT_CLASS_START(CLASSNAME, CAMELNAME, LOWWERNAME) \
  104. void LOWWERNAME##_init(TSRMLS_D) { \
  105. zend_class_entry class_type; \
  106. const char* class_name = CLASSNAME; \
  107. INIT_CLASS_ENTRY_EX(class_type, CLASSNAME, strlen(CLASSNAME), \
  108. LOWWERNAME##_methods); \
  109. LOWWERNAME##_type = zend_register_internal_class(&class_type TSRMLS_CC); \
  110. LOWWERNAME##_type->create_object = LOWWERNAME##_create; \
  111. LOWWERNAME##_handlers = PEMALLOC(zend_object_handlers); \
  112. memcpy(LOWWERNAME##_handlers, zend_get_std_object_handlers(), \
  113. sizeof(zend_object_handlers));
  114. #define PHP_PROTO_INIT_CLASS_END \
  115. }
  116. #define PHP_PROTO_OBJECT_CREATE_START(NAME, LOWWERNAME) \
  117. static zend_object_value LOWWERNAME##_create( \
  118. zend_class_entry* ce TSRMLS_DC) { \
  119. PHP_PROTO_ALLOC_CLASS_OBJECT(NAME, ce); \
  120. zend_object_std_init(&intern->std, ce TSRMLS_CC); \
  121. object_properties_init(&intern->std, ce);
  122. #define PHP_PROTO_OBJECT_CREATE_END(NAME, LOWWERNAME) \
  123. PHP_PROTO_FREE_CLASS_OBJECT(NAME, LOWWERNAME##_free, LOWWERNAME##_handlers); \
  124. }
  125. #define PHP_PROTO_OBJECT_FREE_START(classname, lowername) \
  126. void lowername##_free(void* object TSRMLS_DC) { \
  127. classname* intern = object;
  128. #define PHP_PROTO_OBJECT_FREE_END \
  129. zend_object_std_dtor(&intern->std TSRMLS_CC); \
  130. efree(intern); \
  131. }
  132. #define PHP_PROTO_OBJECT_DTOR_START(classname, lowername)
  133. #define PHP_PROTO_OBJECT_DTOR_END
  134. #define CACHED_VALUE zval*
  135. #define CACHED_TO_ZVAL_PTR(VALUE) (VALUE)
  136. #define CACHED_PTR_TO_ZVAL_PTR(VALUE) (*VALUE)
  137. #define ZVAL_PTR_TO_CACHED_PTR(VALUE) (&VALUE)
  138. #define CREATE_OBJ_ON_ALLOCATED_ZVAL_PTR(zval_ptr, class_type) \
  139. ZVAL_OBJ(zval_ptr, class_type->create_object(class_type TSRMLS_CC));
  140. #define PHP_PROTO_SEPARATE_ZVAL_IF_NOT_REF(value) \
  141. SEPARATE_ZVAL_IF_NOT_REF(value)
  142. #define PHP_PROTO_GLOBAL_UNINITIALIZED_ZVAL EG(uninitialized_zval_ptr)
  143. #define OBJ_PROP(OBJECT, OFFSET) &((OBJECT)->properties_table[OFFSET])
  144. #define php_proto_zval_ptr_dtor(zval_ptr) \
  145. zval_ptr_dtor(&(zval_ptr))
  146. #define PHP_PROTO_ALLOC_CLASS_OBJECT(class_object, class_type) \
  147. class_object* intern; \
  148. intern = (class_object*)emalloc(sizeof(class_object)); \
  149. memset(intern, 0, sizeof(class_object));
  150. #define PHP_PROTO_FREE_CLASS_OBJECT(class_object, class_object_free, handler) \
  151. zend_object_value retval = {0}; \
  152. retval.handle = zend_objects_store_put( \
  153. intern, (zend_objects_store_dtor_t)zend_objects_destroy_object, \
  154. class_object_free, NULL TSRMLS_CC); \
  155. retval.handlers = handler; \
  156. return retval;
  157. #define PHP_PROTO_ALLOC_ARRAY(zval_ptr) \
  158. ALLOC_HASHTABLE(Z_ARRVAL_P(zval_ptr)); \
  159. Z_TYPE_P(zval_ptr) = IS_ARRAY;
  160. #define ZVAL_OBJ(zval_ptr, call_create) \
  161. Z_TYPE_P(zval_ptr) = IS_OBJECT; \
  162. Z_OBJVAL_P(zval_ptr) = call_create;
  163. #define UNBOX(class_name, val) \
  164. (class_name*)zend_object_store_get_object(val TSRMLS_CC);
  165. #define UNBOX_HASHTABLE_VALUE(class_name, val) UNBOX(class_name, val)
  166. #define HASHTABLE_VALUE_DTOR ZVAL_PTR_DTOR
  167. #define PHP_PROTO_HASHTABLE_VALUE zval*
  168. #define HASHTABLE_VALUE_CE(val) Z_OBJCE_P(val)
  169. #define CREATE_HASHTABLE_VALUE(OBJ, WRAPPED_OBJ, OBJ_TYPE, OBJ_CLASS_ENTRY) \
  170. OBJ_TYPE* OBJ; \
  171. PHP_PROTO_HASHTABLE_VALUE WRAPPED_OBJ; \
  172. MAKE_STD_ZVAL(WRAPPED_OBJ); \
  173. ZVAL_OBJ(WRAPPED_OBJ, \
  174. OBJ_CLASS_ENTRY->create_object(OBJ_CLASS_ENTRY TSRMLS_CC)); \
  175. OBJ = UNBOX_HASHTABLE_VALUE(OBJ_TYPE, WRAPPED_OBJ); \
  176. Z_DELREF_P(desc_php);
  177. #define PHP_PROTO_CE_DECLARE zend_class_entry**
  178. #define PHP_PROTO_CE_UNREF(ce) (*ce)
  179. #define php_proto_zend_lookup_class(name, name_length, ce) \
  180. zend_lookup_class(name, name_length, ce TSRMLS_CC)
  181. #define PHP_PROTO_RETVAL_ZVAL(value) ZVAL_ZVAL(return_value, value, 1, 0)
  182. #else // PHP_MAJOR_VERSION >= 7
  183. #define php_proto_zend_literal void**
  184. #define PHP_PROTO_CASE_IS_BOOL IS_TRUE: case IS_FALSE
  185. #define PHP_PROTO_SIZE size_t
  186. #define PHP_PROTO_LONG zend_long
  187. #define PHP_PROTO_TSRMLS_DC
  188. #define PHP_PROTO_TSRMLS_CC
  189. // PHP String
  190. #define PHP_PROTO_ZVAL_STRING(zval_ptr, s, copy) \
  191. ZVAL_STRING(zval_ptr, s)
  192. #define PHP_PROTO_ZVAL_STRINGL(zval_ptr, s, len, copy) \
  193. ZVAL_STRINGL(zval_ptr, s, len)
  194. #define PHP_PROTO_RETURN_STRING(s, copy) RETURN_STRING(s)
  195. #define PHP_PROTO_RETURN_STRINGL(s, len, copy) RETURN_STRINGL(s, len)
  196. #define PHP_PROTO_RETVAL_STRINGL(s, len, copy) RETVAL_STRINGL(s, len)
  197. #define php_proto_zend_make_printable_zval(from, to) \
  198. zend_make_printable_zval(from, to)
  199. // PHP Array
  200. #define PHP_PROTO_HASH_OF(array) Z_ARRVAL_P(&array)
  201. static inline int php_proto_zend_hash_index_update_zval(HashTable* ht, ulong h,
  202. zval* pData) {
  203. void* result = NULL;
  204. result = zend_hash_index_update(ht, h, pData);
  205. return result != NULL ? SUCCESS : FAILURE;
  206. }
  207. static inline int php_proto_zend_hash_index_update_mem(HashTable* ht, ulong h,
  208. void* pData, uint nDataSize,
  209. void** pDest) {
  210. void* result = NULL;
  211. result = zend_hash_index_update_mem(ht, h, pData, nDataSize);
  212. if (pDest != NULL) *pDest = result;
  213. return result != NULL ? SUCCESS : FAILURE;
  214. }
  215. static inline int php_proto_zend_hash_update_zval(HashTable* ht,
  216. const char* key, uint key_len,
  217. zval* pData) {
  218. zend_string* internal_key = zend_string_init(key, key_len, 0);
  219. zend_hash_update(ht, internal_key, pData);
  220. }
  221. static inline int php_proto_zend_hash_update_mem(HashTable* ht, const char* key,
  222. uint key_len, void* pData,
  223. uint nDataSize, void** pDest) {
  224. zend_string* internal_key = zend_string_init(key, key_len, 0);
  225. void* result = zend_hash_update_mem(ht, internal_key, pData, nDataSize);
  226. zend_string_release(internal_key);
  227. if (pDest != NULL) *pDest = result;
  228. return result != NULL ? SUCCESS : FAILURE;
  229. }
  230. static inline int php_proto_zend_hash_index_find_zval(const HashTable* ht,
  231. ulong h, void** pDest) {
  232. zval* result = zend_hash_index_find(ht, h);
  233. if (pDest != NULL) *pDest = result;
  234. return result != NULL ? SUCCESS : FAILURE;
  235. }
  236. static inline int php_proto_zend_hash_index_find_mem(const HashTable* ht,
  237. ulong h, void** pDest) {
  238. void* result = NULL;
  239. result = zend_hash_index_find_ptr(ht, h);
  240. if (pDest != NULL) *pDest = result;
  241. return result != NULL ? SUCCESS : FAILURE;
  242. }
  243. static inline int php_proto_zend_hash_find_zval(const HashTable* ht,
  244. const char* key, uint key_len,
  245. void** pDest) {
  246. zend_string* internal_key = zend_string_init(key, key_len, 1);
  247. zval* result = zend_hash_find(ht, internal_key);
  248. if (pDest != NULL) *pDest = result;
  249. return result != NULL ? SUCCESS : FAILURE;
  250. }
  251. static inline int php_proto_zend_hash_find_mem(const HashTable* ht,
  252. const char* key, uint key_len,
  253. void** pDest) {
  254. zend_string* internal_key = zend_string_init(key, key_len, 1);
  255. void* result = zend_hash_find_ptr(ht, internal_key);
  256. zend_string_release(internal_key);
  257. if (pDest != NULL) *pDest = result;
  258. return result != NULL ? SUCCESS : FAILURE;
  259. }
  260. static inline int php_proto_zend_hash_next_index_insert_zval(HashTable* ht,
  261. void* pData) {
  262. zval tmp;
  263. ZVAL_OBJ(&tmp, *(zend_object**)pData);
  264. zval* result = zend_hash_next_index_insert(ht, &tmp);
  265. return result != NULL ? SUCCESS : FAILURE;
  266. }
  267. static inline int php_proto_zend_hash_next_index_insert_mem(HashTable* ht,
  268. void* pData,
  269. uint nDataSize,
  270. void** pDest) {
  271. void* result = NULL;
  272. result = zend_hash_next_index_insert_mem(ht, pData, nDataSize);
  273. if (pDest != NULL) *pDest = result;
  274. return result != NULL ? SUCCESS : FAILURE;
  275. }
  276. static inline int php_proto_zend_hash_get_current_data_ex(HashTable* ht,
  277. void** pDest,
  278. HashPosition* pos) {
  279. void* result = NULL;
  280. result = zend_hash_get_current_data_ex(ht, pos);
  281. if (pDest != NULL) *pDest = result;
  282. return result != NULL ? SUCCESS : FAILURE;
  283. }
  284. // PHP Object
  285. #define PHP_PROTO_WRAP_OBJECT_START(name) struct name {
  286. #define PHP_PROTO_WRAP_OBJECT_END \
  287. zend_object std; \
  288. };
  289. #define PHP_PROTO_INIT_SUBMSGCLASS_START(CLASSNAME, CAMELNAME, LOWWERNAME) \
  290. void LOWWERNAME##_init(TSRMLS_D) { \
  291. zend_class_entry class_type; \
  292. const char* class_name = CLASSNAME; \
  293. INIT_CLASS_ENTRY_EX(class_type, CLASSNAME, strlen(CLASSNAME), \
  294. LOWWERNAME##_methods); \
  295. LOWWERNAME##_type = zend_register_internal_class(&class_type TSRMLS_CC); \
  296. LOWWERNAME##_type->create_object = message_create;
  297. #define PHP_PROTO_INIT_SUBMSGCLASS_END \
  298. }
  299. #define PHP_PROTO_INIT_CLASS_START(CLASSNAME, CAMELNAME, LOWWERNAME) \
  300. void LOWWERNAME##_init(TSRMLS_D) { \
  301. zend_class_entry class_type; \
  302. const char* class_name = CLASSNAME; \
  303. INIT_CLASS_ENTRY_EX(class_type, CLASSNAME, strlen(CLASSNAME), \
  304. LOWWERNAME##_methods); \
  305. LOWWERNAME##_type = zend_register_internal_class(&class_type TSRMLS_CC); \
  306. LOWWERNAME##_type->create_object = LOWWERNAME##_create; \
  307. LOWWERNAME##_handlers = PEMALLOC(zend_object_handlers); \
  308. memcpy(LOWWERNAME##_handlers, zend_get_std_object_handlers(), \
  309. sizeof(zend_object_handlers)); \
  310. LOWWERNAME##_handlers->free_obj = LOWWERNAME##_free; \
  311. LOWWERNAME##_handlers->dtor_obj = LOWWERNAME##_dtor; \
  312. LOWWERNAME##_handlers->offset = XtOffsetOf(CAMELNAME, std);
  313. #define PHP_PROTO_INIT_CLASS_END \
  314. }
  315. #define PHP_PROTO_OBJECT_FREE_START(classname, lowername) \
  316. void lowername##_free(zend_object* object) { \
  317. classname* intern = \
  318. (classname*)((char*)object - XtOffsetOf(classname, std));
  319. #define PHP_PROTO_OBJECT_FREE_END \
  320. }
  321. #define PHP_PROTO_OBJECT_DTOR_START(classname, lowername) \
  322. void lowername##_dtor(zend_object* object) { \
  323. classname* intern = \
  324. (classname*)((char*)object - XtOffsetOf(classname, std));
  325. #define PHP_PROTO_OBJECT_DTOR_END \
  326. zend_object_std_dtor(object TSRMLS_CC); \
  327. }
  328. #define PHP_PROTO_OBJECT_CREATE_START(NAME, LOWWERNAME) \
  329. static zend_object* LOWWERNAME##_create(zend_class_entry* ce TSRMLS_DC) { \
  330. PHP_PROTO_ALLOC_CLASS_OBJECT(NAME, ce); \
  331. zend_object_std_init(&intern->std, ce TSRMLS_CC); \
  332. object_properties_init(&intern->std, ce);
  333. #define PHP_PROTO_OBJECT_CREATE_END(NAME, LOWWERNAME) \
  334. PHP_PROTO_FREE_CLASS_OBJECT(NAME, LOWWERNAME##_free, LOWWERNAME##_handlers); \
  335. }
  336. #define CACHED_VALUE zval
  337. #define CACHED_TO_ZVAL_PTR(VALUE) (&VALUE)
  338. #define CACHED_PTR_TO_ZVAL_PTR(VALUE) (VALUE)
  339. #define ZVAL_PTR_TO_CACHED_PTR(VALUE) (VALUE)
  340. #define CREATE_OBJ_ON_ALLOCATED_ZVAL_PTR(zval_ptr, class_type) \
  341. ZVAL_OBJ(zval_ptr, class_type->create_object(class_type));
  342. #define PHP_PROTO_SEPARATE_ZVAL_IF_NOT_REF(value) ;
  343. #define PHP_PROTO_GLOBAL_UNINITIALIZED_ZVAL &EG(uninitialized_zval)
  344. #define php_proto_zval_ptr_dtor(zval_ptr) \
  345. zval_ptr_dtor(zval_ptr)
  346. #define PHP_PROTO_ALLOC_CLASS_OBJECT(class_object, class_type) \
  347. class_object* intern; \
  348. int size = sizeof(class_object) + zend_object_properties_size(class_type); \
  349. intern = ecalloc(1, size); \
  350. memset(intern, 0, size);
  351. #define PHP_PROTO_FREE_CLASS_OBJECT(class_object, class_object_free, handler) \
  352. intern->std.handlers = handler; \
  353. return &intern->std;
  354. #define PHP_PROTO_ALLOC_ARRAY(zval_ptr) \
  355. ZVAL_NEW_ARR(zval_ptr)
  356. #define UNBOX(class_name, val) \
  357. (class_name*)((char*)Z_OBJ_P(val) - XtOffsetOf(class_name, std));
  358. #define UNBOX_HASHTABLE_VALUE(class_name, val) \
  359. (class_name*)((char*)val - XtOffsetOf(class_name, std))
  360. #define HASHTABLE_VALUE_DTOR php_proto_hashtable_descriptor_release
  361. #define PHP_PROTO_HASHTABLE_VALUE zend_object*
  362. #define HASHTABLE_VALUE_CE(val) val->ce
  363. #define CREATE_HASHTABLE_VALUE(OBJ, WRAPPED_OBJ, OBJ_TYPE, OBJ_CLASS_ENTRY) \
  364. OBJ_TYPE* OBJ; \
  365. PHP_PROTO_HASHTABLE_VALUE WRAPPED_OBJ; \
  366. WRAPPED_OBJ = OBJ_CLASS_ENTRY->create_object(OBJ_CLASS_ENTRY); \
  367. OBJ = UNBOX_HASHTABLE_VALUE(OBJ_TYPE, WRAPPED_OBJ); \
  368. --GC_REFCOUNT(WRAPPED_OBJ);
  369. #define PHP_PROTO_CE_DECLARE zend_class_entry*
  370. #define PHP_PROTO_CE_UNREF(ce) (ce)
  371. static inline int php_proto_zend_lookup_class(
  372. const char* name, int name_length, zend_class_entry** ce TSRMLS_DC) {
  373. zend_string *zstr_name = zend_string_init(name, name_length, 0);
  374. *ce = zend_lookup_class(zstr_name);
  375. zend_string_release(zstr_name);
  376. return *ce != NULL ? SUCCESS : FAILURE;
  377. }
  378. #define PHP_PROTO_RETVAL_ZVAL(value) ZVAL_COPY(return_value, value)
  379. #endif // PHP_MAJOR_VERSION >= 7
  380. #if PHP_MAJOR_VERSION < 7 || (PHP_MAJOR_VERSION == 7 && PHP_MINOR_VERSION == 0)
  381. #define PHP_PROTO_FAKE_SCOPE_BEGIN(klass) \
  382. zend_class_entry* old_scope = EG(scope); \
  383. EG(scope) = klass;
  384. #define PHP_PROTO_FAKE_SCOPE_RESTART(klass) \
  385. old_scope = EG(scope); \
  386. EG(scope) = klass;
  387. #define PHP_PROTO_FAKE_SCOPE_END EG(scope) = old_scope;
  388. #else
  389. #define PHP_PROTO_FAKE_SCOPE_BEGIN(klass) \
  390. zend_class_entry* old_scope = EG(fake_scope); \
  391. EG(fake_scope) = klass;
  392. #define PHP_PROTO_FAKE_SCOPE_RESTART(klass) \
  393. old_scope = EG(fake_scope); \
  394. EG(fake_scope) = klass;
  395. #define PHP_PROTO_FAKE_SCOPE_END EG(fake_scope) = old_scope;
  396. #endif
  397. // Define PHP class
  398. #define DEFINE_PROTOBUF_INIT_CLASS(CLASSNAME, CAMELNAME, LOWERNAME) \
  399. PHP_PROTO_INIT_CLASS_START(CLASSNAME, CAMELNAME, LOWERNAME) \
  400. PHP_PROTO_INIT_CLASS_END
  401. #define DEFINE_PROTOBUF_CREATE(NAME, LOWERNAME) \
  402. PHP_PROTO_OBJECT_CREATE_START(NAME, LOWERNAME) \
  403. LOWERNAME##_init_c_instance(intern TSRMLS_CC); \
  404. PHP_PROTO_OBJECT_CREATE_END(NAME, LOWERNAME)
  405. #define DEFINE_PROTOBUF_FREE(CAMELNAME, LOWERNAME) \
  406. PHP_PROTO_OBJECT_FREE_START(CAMELNAME, LOWERNAME) \
  407. LOWERNAME##_free_c(intern TSRMLS_CC); \
  408. PHP_PROTO_OBJECT_FREE_END
  409. #define DEFINE_PROTOBUF_DTOR(CAMELNAME, LOWERNAME) \
  410. PHP_PROTO_OBJECT_DTOR_START(CAMELNAME, LOWERNAME) \
  411. PHP_PROTO_OBJECT_DTOR_END
  412. #define DEFINE_CLASS(NAME, LOWERNAME, string_name) \
  413. zend_class_entry *LOWERNAME##_type; \
  414. zend_object_handlers *LOWERNAME##_handlers; \
  415. DEFINE_PROTOBUF_FREE(NAME, LOWERNAME) \
  416. DEFINE_PROTOBUF_DTOR(NAME, LOWERNAME) \
  417. DEFINE_PROTOBUF_CREATE(NAME, LOWERNAME) \
  418. DEFINE_PROTOBUF_INIT_CLASS(string_name, NAME, LOWERNAME)
  419. // -----------------------------------------------------------------------------
  420. // Forward Declaration
  421. // ----------------------------------------------------------------------------
  422. struct Any;
  423. struct DescriptorPool;
  424. struct Descriptor;
  425. struct EnumDescriptor;
  426. struct EnumValueDescriptor;
  427. struct FieldDescriptor;
  428. struct InternalDescriptorPool;
  429. struct MessageField;
  430. struct MessageHeader;
  431. struct MessageLayout;
  432. struct RepeatedField;
  433. struct RepeatedFieldIter;
  434. struct Map;
  435. struct MapIter;
  436. struct Oneof;
  437. typedef struct Any Any;
  438. typedef struct DescriptorPool DescriptorPool;
  439. typedef struct Descriptor Descriptor;
  440. typedef struct EnumDescriptor EnumDescriptor;
  441. typedef struct EnumValueDescriptor EnumValueDescriptor;
  442. typedef struct FieldDescriptor FieldDescriptor;
  443. typedef struct InternalDescriptorPool InternalDescriptorPool;
  444. typedef struct MessageField MessageField;
  445. typedef struct MessageHeader MessageHeader;
  446. typedef struct MessageLayout MessageLayout;
  447. typedef struct RepeatedField RepeatedField;
  448. typedef struct RepeatedFieldIter RepeatedFieldIter;
  449. typedef struct Map Map;
  450. typedef struct MapIter MapIter;
  451. typedef struct Oneof Oneof;
  452. // -----------------------------------------------------------------------------
  453. // Globals.
  454. // -----------------------------------------------------------------------------
  455. ZEND_BEGIN_MODULE_GLOBALS(protobuf)
  456. ZEND_END_MODULE_GLOBALS(protobuf)
  457. // Init module and PHP classes.
  458. void any_init(TSRMLS_D);
  459. void descriptor_init(TSRMLS_D);
  460. void enum_descriptor_init(TSRMLS_D);
  461. void descriptor_pool_init(TSRMLS_D);
  462. void internal_descriptor_pool_init(TSRMLS_D);
  463. void field_descriptor_init(TSRMLS_D);
  464. void gpb_type_init(TSRMLS_D);
  465. void map_field_init(TSRMLS_D);
  466. void map_field_iter_init(TSRMLS_D);
  467. void message_init(TSRMLS_D);
  468. void oneof_descriptor_init(TSRMLS_D);
  469. void repeated_field_init(TSRMLS_D);
  470. void repeated_field_iter_init(TSRMLS_D);
  471. void util_init(TSRMLS_D);
  472. // Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
  473. // instances.
  474. void add_def_obj(const void* def, PHP_PROTO_HASHTABLE_VALUE value);
  475. PHP_PROTO_HASHTABLE_VALUE get_def_obj(const void* def);
  476. // Global map from PHP class entries to wrapper Descriptor/EnumDescriptor
  477. // instances.
  478. void add_ce_obj(const void* ce, PHP_PROTO_HASHTABLE_VALUE value);
  479. PHP_PROTO_HASHTABLE_VALUE get_ce_obj(const void* ce);
  480. bool class_added(const void* ce);
  481. // Global map from message/enum's proto fully-qualified name to corresponding
  482. // wrapper Descriptor/EnumDescriptor instances.
  483. void add_proto_obj(const char* proto, PHP_PROTO_HASHTABLE_VALUE value);
  484. PHP_PROTO_HASHTABLE_VALUE get_proto_obj(const char* proto);
  485. extern zend_class_entry* map_field_type;
  486. extern zend_class_entry* repeated_field_type;
  487. // -----------------------------------------------------------------------------
  488. // Descriptor.
  489. // -----------------------------------------------------------------------------
  490. PHP_PROTO_WRAP_OBJECT_START(DescriptorPool)
  491. InternalDescriptorPool* intern;
  492. PHP_PROTO_WRAP_OBJECT_END
  493. PHP_METHOD(DescriptorPool, getGeneratedPool);
  494. PHP_METHOD(DescriptorPool, getDescriptorByClassName);
  495. PHP_METHOD(DescriptorPool, getEnumDescriptorByClassName);
  496. PHP_PROTO_WRAP_OBJECT_START(InternalDescriptorPool)
  497. upb_symtab* symtab;
  498. HashTable* pending_list;
  499. PHP_PROTO_WRAP_OBJECT_END
  500. PHP_METHOD(InternalDescriptorPool, getGeneratedPool);
  501. PHP_METHOD(InternalDescriptorPool, internalAddGeneratedFile);
  502. void internal_add_generated_file(const char* data, PHP_PROTO_SIZE data_len,
  503. InternalDescriptorPool* pool TSRMLS_DC);
  504. void init_generated_pool_once(TSRMLS_D);
  505. // wrapper of generated pool
  506. #if PHP_MAJOR_VERSION < 7
  507. extern zval* generated_pool_php;
  508. extern zval* internal_generated_pool_php;
  509. void descriptor_pool_free(void* object TSRMLS_DC);
  510. void internal_descriptor_pool_free(void* object TSRMLS_DC);
  511. #else
  512. extern zend_object *generated_pool_php;
  513. extern zend_object *internal_generated_pool_php;
  514. void descriptor_pool_free(zend_object* object);
  515. void internal_descriptor_pool_free(zend_object* object);
  516. #endif
  517. extern InternalDescriptorPool* generated_pool; // The actual generated pool
  518. PHP_PROTO_WRAP_OBJECT_START(Descriptor)
  519. const upb_msgdef* msgdef;
  520. MessageLayout* layout;
  521. zend_class_entry* klass; // begins as NULL
  522. const upb_handlers* fill_handlers;
  523. const upb_pbdecodermethod* fill_method;
  524. const upb_json_parsermethod* json_fill_method;
  525. const upb_handlers* pb_serialize_handlers;
  526. const upb_handlers* json_serialize_handlers;
  527. const upb_handlers* json_serialize_handlers_preserve;
  528. PHP_PROTO_WRAP_OBJECT_END
  529. PHP_METHOD(Descriptor, getClass);
  530. PHP_METHOD(Descriptor, getFullName);
  531. PHP_METHOD(Descriptor, getField);
  532. PHP_METHOD(Descriptor, getFieldCount);
  533. PHP_METHOD(Descriptor, getOneofDecl);
  534. PHP_METHOD(Descriptor, getOneofDeclCount);
  535. extern zend_class_entry* descriptor_type;
  536. void descriptor_name_set(Descriptor *desc, const char *name);
  537. PHP_PROTO_WRAP_OBJECT_START(FieldDescriptor)
  538. const upb_fielddef* fielddef;
  539. PHP_PROTO_WRAP_OBJECT_END
  540. PHP_METHOD(FieldDescriptor, getName);
  541. PHP_METHOD(FieldDescriptor, getNumber);
  542. PHP_METHOD(FieldDescriptor, getLabel);
  543. PHP_METHOD(FieldDescriptor, getType);
  544. PHP_METHOD(FieldDescriptor, isMap);
  545. PHP_METHOD(FieldDescriptor, getEnumType);
  546. PHP_METHOD(FieldDescriptor, getMessageType);
  547. extern zend_class_entry* field_descriptor_type;
  548. PHP_PROTO_WRAP_OBJECT_START(EnumDescriptor)
  549. const upb_enumdef* enumdef;
  550. zend_class_entry* klass; // begins as NULL
  551. PHP_PROTO_WRAP_OBJECT_END
  552. PHP_METHOD(EnumDescriptor, getValue);
  553. PHP_METHOD(EnumDescriptor, getValueCount);
  554. extern zend_class_entry* enum_descriptor_type;
  555. PHP_PROTO_WRAP_OBJECT_START(EnumValueDescriptor)
  556. const char* name;
  557. int32_t number;
  558. PHP_PROTO_WRAP_OBJECT_END
  559. PHP_METHOD(EnumValueDescriptor, getName);
  560. PHP_METHOD(EnumValueDescriptor, getNumber);
  561. extern zend_class_entry* enum_value_descriptor_type;
  562. // -----------------------------------------------------------------------------
  563. // Message class creation.
  564. // -----------------------------------------------------------------------------
  565. void* message_data(MessageHeader* msg);
  566. void custom_data_init(const zend_class_entry* ce,
  567. MessageHeader* msg PHP_PROTO_TSRMLS_DC);
  568. // Build PHP class for given descriptor. Instead of building from scratch, this
  569. // function modifies existing class which has been partially defined in PHP
  570. // code.
  571. void build_class_from_descriptor(
  572. PHP_PROTO_HASHTABLE_VALUE php_descriptor TSRMLS_DC);
  573. extern zend_class_entry* message_type;
  574. extern zend_object_handlers* message_handlers;
  575. // -----------------------------------------------------------------------------
  576. // Message layout / storage.
  577. // -----------------------------------------------------------------------------
  578. /*
  579. * In c extension, each protobuf message is a zval instance. The zval instance
  580. * is like union, which can be used to store int, string, zend_object_value and
  581. * etc. For protobuf message, the zval instance is used to store the
  582. * zend_object_value.
  583. *
  584. * The zend_object_value is composed of handlers and a handle to look up the
  585. * actual stored data. The handlers are pointers to functions, e.g., read,
  586. * write, and etc, to access properties.
  587. *
  588. * The actual data of protobuf messages is stored as MessageHeader in zend
  589. * engine's central repository. Each MessageHeader instance is composed of a
  590. * zend_object, a Descriptor instance and the real message data.
  591. *
  592. * For the reason that PHP's native types may not be large enough to store
  593. * protobuf message's field (e.g., int64), all message's data is stored in
  594. * custom memory layout and is indexed by the Descriptor instance.
  595. *
  596. * The zend_object contains the zend class entry and the properties table. The
  597. * zend class entry contains all information about protobuf message's
  598. * corresponding PHP class. The most useful information is the offset table of
  599. * properties. Because read access to properties requires returning zval
  600. * instance, we need to convert data from the custom layout to zval instance.
  601. * Instead of creating zval instance for every read access, we use the zval
  602. * instances in the properties table in the zend_object as cache. When
  603. * accessing properties, the offset is needed to find the zval property in
  604. * zend_object's properties table. These properties will be updated using the
  605. * data from custom memory layout only when reading these properties.
  606. *
  607. * zval
  608. * |-zend_object_value obj
  609. * |-zend_object_handlers* handlers -> |-read_property_handler
  610. * | |-write_property_handler
  611. * | ++++++++++++++++++++++
  612. * |-zend_object_handle handle -> + central repository +
  613. * ++++++++++++++++++++++
  614. * MessageHeader <-----------------|
  615. * |-zend_object std
  616. * | |-class_entry* ce -> class_entry
  617. * | | |-HashTable properties_table (name->offset)
  618. * | |-zval** properties_table <------------------------------|
  619. * | |------> zval* property(cache)
  620. * |-Descriptor* desc (name->offset)
  621. * |-void** data <-----------|
  622. * |-----------------------> void* property(data)
  623. *
  624. */
  625. #define MESSAGE_FIELD_NO_CASE ((size_t)-1)
  626. struct MessageField {
  627. size_t offset;
  628. int cache_index; // Each field except oneof field has a zval cache to avoid
  629. // multiple creation when being accessed.
  630. size_t case_offset; // for oneofs, a uint32. Else, MESSAGE_FIELD_NO_CASE.
  631. };
  632. struct MessageLayout {
  633. const upb_msgdef* msgdef;
  634. MessageField* fields;
  635. size_t size;
  636. };
  637. PHP_PROTO_WRAP_OBJECT_START(MessageHeader)
  638. void* data; // Point to the real message data.
  639. // Place needs to be consistent with map_parse_frame_data_t.
  640. Descriptor* descriptor; // Kept alive by self.class.descriptor reference.
  641. PHP_PROTO_WRAP_OBJECT_END
  642. MessageLayout* create_layout(const upb_msgdef* msgdef);
  643. void layout_init(MessageLayout* layout, void* storage,
  644. zend_object* object PHP_PROTO_TSRMLS_DC);
  645. zval* layout_get(MessageLayout* layout, const void* storage,
  646. const upb_fielddef* field, CACHED_VALUE* cache TSRMLS_DC);
  647. void layout_set(MessageLayout* layout, MessageHeader* header,
  648. const upb_fielddef* field, zval* val TSRMLS_DC);
  649. void layout_merge(MessageLayout* layout, MessageHeader* from,
  650. MessageHeader* to TSRMLS_DC);
  651. const char* layout_get_oneof_case(MessageLayout* layout, const void* storage,
  652. const upb_oneofdef* oneof TSRMLS_DC);
  653. void free_layout(MessageLayout* layout);
  654. PHP_METHOD(Message, clear);
  655. PHP_METHOD(Message, mergeFrom);
  656. PHP_METHOD(Message, readOneof);
  657. PHP_METHOD(Message, writeOneof);
  658. PHP_METHOD(Message, whichOneof);
  659. PHP_METHOD(Message, __construct);
  660. // -----------------------------------------------------------------------------
  661. // Encode / Decode.
  662. // -----------------------------------------------------------------------------
  663. // Maximum depth allowed during encoding, to avoid stack overflows due to
  664. // cycles.
  665. #define ENCODE_MAX_NESTING 63
  666. // Constructs the upb decoder method for parsing messages of this type.
  667. // This is called from the message class creation code.
  668. const upb_pbdecodermethod *new_fillmsg_decodermethod(Descriptor *desc,
  669. const void *owner);
  670. void serialize_to_string(zval* val, zval* return_value TSRMLS_DC);
  671. void merge_from_string(const char* data, int data_len, const Descriptor* desc,
  672. MessageHeader* msg);
  673. PHP_METHOD(Message, serializeToString);
  674. PHP_METHOD(Message, mergeFromString);
  675. PHP_METHOD(Message, serializeToJsonString);
  676. PHP_METHOD(Message, mergeFromJsonString);
  677. // -----------------------------------------------------------------------------
  678. // Type check / conversion.
  679. // -----------------------------------------------------------------------------
  680. bool protobuf_convert_to_int32(zval* from, int32_t* to);
  681. bool protobuf_convert_to_uint32(zval* from, uint32_t* to);
  682. bool protobuf_convert_to_int64(zval* from, int64_t* to);
  683. bool protobuf_convert_to_uint64(zval* from, uint64_t* to);
  684. bool protobuf_convert_to_float(zval* from, float* to);
  685. bool protobuf_convert_to_double(zval* from, double* to);
  686. bool protobuf_convert_to_bool(zval* from, int8_t* to);
  687. bool protobuf_convert_to_string(zval* from);
  688. PHP_METHOD(Util, checkInt32);
  689. PHP_METHOD(Util, checkUint32);
  690. PHP_METHOD(Util, checkInt64);
  691. PHP_METHOD(Util, checkUint64);
  692. PHP_METHOD(Util, checkEnum);
  693. PHP_METHOD(Util, checkFloat);
  694. PHP_METHOD(Util, checkDouble);
  695. PHP_METHOD(Util, checkBool);
  696. PHP_METHOD(Util, checkString);
  697. PHP_METHOD(Util, checkBytes);
  698. PHP_METHOD(Util, checkMessage);
  699. PHP_METHOD(Util, checkMapField);
  700. PHP_METHOD(Util, checkRepeatedField);
  701. // -----------------------------------------------------------------------------
  702. // Native slot storage abstraction.
  703. // -----------------------------------------------------------------------------
  704. #define NATIVE_SLOT_MAX_SIZE sizeof(uint64_t)
  705. size_t native_slot_size(upb_fieldtype_t type);
  706. bool native_slot_set(upb_fieldtype_t type, const zend_class_entry* klass,
  707. void* memory, zval* value TSRMLS_DC);
  708. // String/Message is stored differently in array/map from normal message fields.
  709. // So we need to make a special method to handle that.
  710. bool native_slot_set_by_array(upb_fieldtype_t type,
  711. const zend_class_entry* klass, void* memory,
  712. zval* value TSRMLS_DC);
  713. bool native_slot_set_by_map(upb_fieldtype_t type, const zend_class_entry* klass,
  714. void* memory, zval* value TSRMLS_DC);
  715. void native_slot_init(upb_fieldtype_t type, void* memory, CACHED_VALUE* cache);
  716. // For each property, in order to avoid conversion between the zval object and
  717. // the actual data type during parsing/serialization, the containing message
  718. // object use the custom memory layout to store the actual data type for each
  719. // property inside of it. To access a property from php code, the property
  720. // needs to be converted to a zval object. The message object is not responsible
  721. // for providing such a zval object. Instead the caller needs to provide one
  722. // (cache) and update it with the actual data (memory).
  723. void native_slot_get(upb_fieldtype_t type, const void* memory,
  724. CACHED_VALUE* cache TSRMLS_DC);
  725. // String/Message is stored differently in array/map from normal message fields.
  726. // So we need to make a special method to handle that.
  727. void native_slot_get_by_array(upb_fieldtype_t type, const void* memory,
  728. CACHED_VALUE* cache TSRMLS_DC);
  729. void native_slot_get_by_map_key(upb_fieldtype_t type, const void* memory,
  730. int length, CACHED_VALUE* cache TSRMLS_DC);
  731. void native_slot_get_by_map_value(upb_fieldtype_t type, const void* memory,
  732. CACHED_VALUE* cache TSRMLS_DC);
  733. void native_slot_get_default(upb_fieldtype_t type,
  734. CACHED_VALUE* cache TSRMLS_DC);
  735. // -----------------------------------------------------------------------------
  736. // Map Field.
  737. // -----------------------------------------------------------------------------
  738. extern zend_object_handlers* map_field_handlers;
  739. extern zend_object_handlers* map_field_iter_handlers;
  740. PHP_PROTO_WRAP_OBJECT_START(Map)
  741. upb_fieldtype_t key_type;
  742. upb_fieldtype_t value_type;
  743. const zend_class_entry* msg_ce; // class entry for value message
  744. upb_strtable table;
  745. PHP_PROTO_WRAP_OBJECT_END
  746. PHP_PROTO_WRAP_OBJECT_START(MapIter)
  747. Map* self;
  748. upb_strtable_iter it;
  749. PHP_PROTO_WRAP_OBJECT_END
  750. void map_begin(zval* self, MapIter* iter TSRMLS_DC);
  751. void map_next(MapIter* iter);
  752. bool map_done(MapIter* iter);
  753. const char* map_iter_key(MapIter* iter, int* len);
  754. upb_value map_iter_value(MapIter* iter, int* len);
  755. // These operate on a map-entry msgdef.
  756. const upb_fielddef* map_entry_key(const upb_msgdef* msgdef);
  757. const upb_fielddef* map_entry_value(const upb_msgdef* msgdef);
  758. void map_field_create_with_field(const zend_class_entry* ce,
  759. const upb_fielddef* field,
  760. CACHED_VALUE* map_field PHP_PROTO_TSRMLS_DC);
  761. void map_field_create_with_type(const zend_class_entry* ce,
  762. upb_fieldtype_t key_type,
  763. upb_fieldtype_t value_type,
  764. const zend_class_entry* msg_ce,
  765. CACHED_VALUE* map_field PHP_PROTO_TSRMLS_DC);
  766. void* upb_value_memory(upb_value* v);
  767. #define MAP_KEY_FIELD 1
  768. #define MAP_VALUE_FIELD 2
  769. // These operate on a map field (i.e., a repeated field of submessages whose
  770. // submessage type is a map-entry msgdef).
  771. bool is_map_field(const upb_fielddef* field);
  772. const upb_fielddef* map_field_key(const upb_fielddef* field);
  773. const upb_fielddef* map_field_value(const upb_fielddef* field);
  774. bool map_index_set(Map *intern, const char* keyval, int length, upb_value v);
  775. PHP_METHOD(MapField, __construct);
  776. PHP_METHOD(MapField, offsetExists);
  777. PHP_METHOD(MapField, offsetGet);
  778. PHP_METHOD(MapField, offsetSet);
  779. PHP_METHOD(MapField, offsetUnset);
  780. PHP_METHOD(MapField, count);
  781. PHP_METHOD(MapField, getIterator);
  782. PHP_METHOD(MapFieldIter, rewind);
  783. PHP_METHOD(MapFieldIter, current);
  784. PHP_METHOD(MapFieldIter, key);
  785. PHP_METHOD(MapFieldIter, next);
  786. PHP_METHOD(MapFieldIter, valid);
  787. // -----------------------------------------------------------------------------
  788. // Repeated Field.
  789. // -----------------------------------------------------------------------------
  790. extern zend_object_handlers* repeated_field_handlers;
  791. extern zend_object_handlers* repeated_field_iter_handlers;
  792. PHP_PROTO_WRAP_OBJECT_START(RepeatedField)
  793. #if PHP_MAJOR_VERSION < 7
  794. zval* array;
  795. #else
  796. zval array;
  797. #endif
  798. upb_fieldtype_t type;
  799. const zend_class_entry* msg_ce; // class entry for containing message
  800. // (for message field only).
  801. PHP_PROTO_WRAP_OBJECT_END
  802. PHP_PROTO_WRAP_OBJECT_START(RepeatedFieldIter)
  803. RepeatedField* repeated_field;
  804. long position;
  805. PHP_PROTO_WRAP_OBJECT_END
  806. void repeated_field_create_with_field(
  807. zend_class_entry* ce, const upb_fielddef* field,
  808. CACHED_VALUE* repeated_field PHP_PROTO_TSRMLS_DC);
  809. void repeated_field_create_with_type(
  810. zend_class_entry* ce, upb_fieldtype_t type, const zend_class_entry* msg_ce,
  811. CACHED_VALUE* repeated_field PHP_PROTO_TSRMLS_DC);
  812. // Return the element at the index position from the repeated field. There is
  813. // not restriction on the type of stored elements.
  814. void *repeated_field_index_native(RepeatedField *intern, int index TSRMLS_DC);
  815. // Add the element to the end of the repeated field. There is not restriction on
  816. // the type of stored elements.
  817. void repeated_field_push_native(RepeatedField *intern, void *value);
  818. PHP_METHOD(RepeatedField, __construct);
  819. PHP_METHOD(RepeatedField, append);
  820. PHP_METHOD(RepeatedField, offsetExists);
  821. PHP_METHOD(RepeatedField, offsetGet);
  822. PHP_METHOD(RepeatedField, offsetSet);
  823. PHP_METHOD(RepeatedField, offsetUnset);
  824. PHP_METHOD(RepeatedField, count);
  825. PHP_METHOD(RepeatedField, getIterator);
  826. PHP_METHOD(RepeatedFieldIter, rewind);
  827. PHP_METHOD(RepeatedFieldIter, current);
  828. PHP_METHOD(RepeatedFieldIter, key);
  829. PHP_METHOD(RepeatedFieldIter, next);
  830. PHP_METHOD(RepeatedFieldIter, valid);
  831. // -----------------------------------------------------------------------------
  832. // Oneof Field.
  833. // -----------------------------------------------------------------------------
  834. PHP_PROTO_WRAP_OBJECT_START(Oneof)
  835. upb_oneofdef* oneofdef;
  836. int index; // Index of field in oneof. -1 if not set.
  837. char value[NATIVE_SLOT_MAX_SIZE];
  838. PHP_PROTO_WRAP_OBJECT_END
  839. PHP_METHOD(Oneof, getName);
  840. PHP_METHOD(Oneof, getField);
  841. PHP_METHOD(Oneof, getFieldCount);
  842. extern zend_class_entry* oneof_descriptor_type;
  843. // Oneof case slot value to indicate that no oneof case is set. The value `0` is
  844. // safe because field numbers are used as case identifiers, and no field can
  845. // have a number of 0.
  846. #define ONEOF_CASE_NONE 0
  847. // -----------------------------------------------------------------------------
  848. // Well Known Type.
  849. // -----------------------------------------------------------------------------
  850. PHP_METHOD(Any, __construct);
  851. PHP_METHOD(Any, getTypeUrl);
  852. PHP_METHOD(Any, setTypeUrl);
  853. PHP_METHOD(Any, getValue);
  854. PHP_METHOD(Any, setValue);
  855. PHP_METHOD(Any, unpack);
  856. PHP_METHOD(Any, pack);
  857. PHP_METHOD(Any, is);
  858. extern zend_class_entry* any_type;
  859. // -----------------------------------------------------------------------------
  860. // Upb.
  861. // -----------------------------------------------------------------------------
  862. upb_fieldtype_t to_fieldtype(upb_descriptortype_t type);
  863. const zend_class_entry* field_type_class(
  864. const upb_fielddef* field PHP_PROTO_TSRMLS_DC);
  865. // -----------------------------------------------------------------------------
  866. // Utilities.
  867. // -----------------------------------------------------------------------------
  868. // Memory management
  869. #define ALLOC(class_name) (class_name*) emalloc(sizeof(class_name))
  870. #define PEMALLOC(class_name) (class_name*) pemalloc(sizeof(class_name), 1)
  871. #define ALLOC_N(class_name, n) (class_name*) emalloc(sizeof(class_name) * n)
  872. #define FREE(object) efree(object)
  873. #define PEFREE(object) pefree(object, 1)
  874. // String argument.
  875. #define STR(str) (str), strlen(str)
  876. // Zend Value
  877. #if PHP_MAJOR_VERSION < 7
  878. #define Z_OBJ_P(zval_p) \
  879. ((zend_object*)(EG(objects_store) \
  880. .object_buckets[Z_OBJ_HANDLE_P(zval_p)] \
  881. .bucket.obj.object))
  882. #endif
  883. // Message handler
  884. static inline zval* php_proto_message_read_property(
  885. zval* msg, zval* member PHP_PROTO_TSRMLS_DC) {
  886. #if PHP_MAJOR_VERSION < 7
  887. return message_handlers->read_property(msg, member, BP_VAR_R,
  888. NULL PHP_PROTO_TSRMLS_CC);
  889. #else
  890. return message_handlers->read_property(msg, member, BP_VAR_R, NULL,
  891. NULL PHP_PROTO_TSRMLS_CC);
  892. #endif
  893. }
  894. #endif // __GOOGLE_PROTOBUF_PHP_PROTOBUF_H__