protobuf.h 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  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.6.1"
  37. #define MAX_LENGTH_OF_INT64 20
  38. #define SIZEOF_INT64 8
  39. /* From Chromium. */
  40. #define ARRAY_SIZE(x) \
  41. ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
  42. // -----------------------------------------------------------------------------
  43. // PHP7 Wrappers
  44. // ----------------------------------------------------------------------------
  45. #if PHP_VERSION_ID < 70300
  46. #define GC_ADDREF(h) ++GC_REFCOUNT(h)
  47. #define GC_DELREF(h) --GC_REFCOUNT(h)
  48. #endif
  49. #if PHP_MAJOR_VERSION < 7
  50. #define php_proto_zend_literal const zend_literal*
  51. #define PHP_PROTO_CASE_IS_BOOL IS_BOOL
  52. #define PHP_PROTO_SIZE int
  53. #define PHP_PROTO_LONG long
  54. #define PHP_PROTO_TSRMLS_DC TSRMLS_DC
  55. #define PHP_PROTO_TSRMLS_CC TSRMLS_CC
  56. // PHP String
  57. #define PHP_PROTO_ZVAL_STRING(zval_ptr, s, copy) \
  58. ZVAL_STRING(zval_ptr, s, copy)
  59. #define PHP_PROTO_ZVAL_STRINGL(zval_ptr, s, len, copy) \
  60. ZVAL_STRINGL(zval_ptr, s, len, copy)
  61. #define PHP_PROTO_RETURN_STRING(s, copy) RETURN_STRING(s, copy)
  62. #define PHP_PROTO_RETURN_STRINGL(s, len, copy) RETURN_STRINGL(s, len, copy)
  63. #define PHP_PROTO_RETVAL_STRINGL(s, len, copy) RETVAL_STRINGL(s, len, copy)
  64. #define php_proto_zend_make_printable_zval(from, to) \
  65. { \
  66. int use_copy; \
  67. zend_make_printable_zval(from, to, &use_copy); \
  68. }
  69. // PHP Array
  70. #define PHP_PROTO_HASH_OF(array) Z_ARRVAL_P(array)
  71. #define php_proto_zend_hash_index_update_zval(ht, h, pData) \
  72. zend_hash_index_update(ht, h, &(pData), sizeof(void*), NULL)
  73. #define php_proto_zend_hash_update_zval(ht, key, key_len, value) \
  74. zend_hash_update(ht, key, key_len, value, sizeof(void*), NULL)
  75. #define php_proto_zend_hash_update(ht, key, key_len) \
  76. zend_hash_update(ht, key, key_len, 0, 0, NULL)
  77. #define php_proto_zend_hash_index_update_mem(ht, h, pData, nDataSize, pDest) \
  78. zend_hash_index_update(ht, h, pData, nDataSize, pDest)
  79. #define php_proto_zend_hash_update_mem(ht, key, key_len, pData, nDataSize, \
  80. pDest) \
  81. zend_hash_update(ht, key, key_len, pData, nDataSize, pDest)
  82. #define php_proto_zend_hash_index_find_zval(ht, h, pDest) \
  83. zend_hash_index_find(ht, h, pDest)
  84. #define php_proto_zend_hash_find(ht, key, key_len, pDest) \
  85. zend_hash_find(ht, key, key_len, pDest)
  86. #define php_proto_zend_hash_index_find_mem(ht, h, pDest) \
  87. zend_hash_index_find(ht, h, pDest)
  88. #define php_proto_zend_hash_find_zval(ht, key, key_len, pDest) \
  89. zend_hash_find(ht, key, key_len, pDest)
  90. #define php_proto_zend_hash_find_mem(ht, key, key_len, pDest) \
  91. zend_hash_find(ht, key, key_len, pDest)
  92. #define php_proto_zend_hash_next_index_insert_zval(ht, pData) \
  93. zend_hash_next_index_insert(ht, pData, sizeof(void*), NULL)
  94. #define php_proto_zend_hash_next_index_insert_mem(ht, pData, nDataSize, pDest) \
  95. zend_hash_next_index_insert(ht, pData, nDataSize, pDest)
  96. #define php_proto_zend_hash_get_current_data_ex(ht, pDest, pos) \
  97. zend_hash_get_current_data_ex(ht, pDest, pos)
  98. // PHP Object
  99. #define PHP_PROTO_WRAP_OBJECT_START(name) \
  100. struct name { \
  101. zend_object std;
  102. #define PHP_PROTO_WRAP_OBJECT_END \
  103. };
  104. #define PHP_PROTO_INIT_SUBMSGCLASS_START(CLASSNAME, CAMELNAME, LOWWERNAME) \
  105. void LOWWERNAME##_init(TSRMLS_D) { \
  106. zend_class_entry class_type; \
  107. const char* class_name = CLASSNAME; \
  108. INIT_CLASS_ENTRY_EX(class_type, CLASSNAME, strlen(CLASSNAME), \
  109. LOWWERNAME##_methods); \
  110. LOWWERNAME##_type = zend_register_internal_class(&class_type TSRMLS_CC); \
  111. LOWWERNAME##_type->create_object = message_create; \
  112. zend_do_inheritance(LOWWERNAME##_type, message_type TSRMLS_CC);
  113. #define PHP_PROTO_INIT_SUBMSGCLASS_END \
  114. }
  115. #define PHP_PROTO_INIT_ENUMCLASS_START(CLASSNAME, CAMELNAME, LOWWERNAME) \
  116. void LOWWERNAME##_init(TSRMLS_D) { \
  117. zend_class_entry class_type; \
  118. const char* class_name = CLASSNAME; \
  119. INIT_CLASS_ENTRY_EX(class_type, CLASSNAME, strlen(CLASSNAME), \
  120. LOWWERNAME##_methods); \
  121. LOWWERNAME##_type = zend_register_internal_class(&class_type TSRMLS_CC);
  122. #define PHP_PROTO_INIT_ENUMCLASS_END \
  123. }
  124. #define PHP_PROTO_INIT_CLASS_START(CLASSNAME, CAMELNAME, LOWWERNAME) \
  125. void LOWWERNAME##_init(TSRMLS_D) { \
  126. zend_class_entry class_type; \
  127. const char* class_name = CLASSNAME; \
  128. INIT_CLASS_ENTRY_EX(class_type, CLASSNAME, strlen(CLASSNAME), \
  129. LOWWERNAME##_methods); \
  130. LOWWERNAME##_type = zend_register_internal_class(&class_type TSRMLS_CC); \
  131. LOWWERNAME##_type->create_object = LOWWERNAME##_create; \
  132. LOWWERNAME##_handlers = PEMALLOC(zend_object_handlers); \
  133. memcpy(LOWWERNAME##_handlers, zend_get_std_object_handlers(), \
  134. sizeof(zend_object_handlers));
  135. #define PHP_PROTO_INIT_CLASS_END \
  136. }
  137. #define PHP_PROTO_OBJECT_CREATE_START(NAME, LOWWERNAME) \
  138. static zend_object_value LOWWERNAME##_create( \
  139. zend_class_entry* ce TSRMLS_DC) { \
  140. PHP_PROTO_ALLOC_CLASS_OBJECT(NAME, ce); \
  141. zend_object_std_init(&intern->std, ce TSRMLS_CC); \
  142. object_properties_init(&intern->std, ce);
  143. #define PHP_PROTO_OBJECT_CREATE_END(NAME, LOWWERNAME) \
  144. PHP_PROTO_FREE_CLASS_OBJECT(NAME, LOWWERNAME##_free, LOWWERNAME##_handlers); \
  145. }
  146. #define PHP_PROTO_OBJECT_FREE_START(classname, lowername) \
  147. void lowername##_free(void* object TSRMLS_DC) { \
  148. classname* intern = object;
  149. #define PHP_PROTO_OBJECT_FREE_END \
  150. zend_object_std_dtor(&intern->std TSRMLS_CC); \
  151. efree(intern); \
  152. }
  153. #define PHP_PROTO_OBJECT_DTOR_START(classname, lowername)
  154. #define PHP_PROTO_OBJECT_DTOR_END
  155. #define CACHED_VALUE zval*
  156. #define CACHED_TO_ZVAL_PTR(VALUE) (VALUE)
  157. #define CACHED_PTR_TO_ZVAL_PTR(VALUE) (*VALUE)
  158. #define ZVAL_PTR_TO_CACHED_PTR(VALUE) (&VALUE)
  159. #define ZVAL_PTR_TO_CACHED_VALUE(VALUE) (VALUE)
  160. #define ZVAL_TO_CACHED_VALUE(VALUE) (&VALUE)
  161. #define CREATE_OBJ_ON_ALLOCATED_ZVAL_PTR(zval_ptr, class_type) \
  162. ZVAL_OBJ(zval_ptr, class_type->create_object(class_type TSRMLS_CC));
  163. #define PHP_PROTO_SEPARATE_ZVAL_IF_NOT_REF(value) \
  164. SEPARATE_ZVAL_IF_NOT_REF(value)
  165. #define PHP_PROTO_GLOBAL_UNINITIALIZED_ZVAL EG(uninitialized_zval_ptr)
  166. #define OBJ_PROP(OBJECT, OFFSET) &((OBJECT)->properties_table[OFFSET])
  167. #define php_proto_zval_ptr_dtor(zval_ptr) \
  168. zval_ptr_dtor(&(zval_ptr))
  169. #define PHP_PROTO_ALLOC_CLASS_OBJECT(class_object, class_type) \
  170. class_object* intern; \
  171. intern = (class_object*)emalloc(sizeof(class_object)); \
  172. memset(intern, 0, sizeof(class_object));
  173. #define PHP_PROTO_FREE_CLASS_OBJECT(class_object, class_object_free, handler) \
  174. zend_object_value retval = {0}; \
  175. retval.handle = zend_objects_store_put( \
  176. intern, (zend_objects_store_dtor_t)zend_objects_destroy_object, \
  177. class_object_free, NULL TSRMLS_CC); \
  178. retval.handlers = handler; \
  179. return retval;
  180. #define PHP_PROTO_ALLOC_ARRAY(zval_ptr) \
  181. ALLOC_HASHTABLE(Z_ARRVAL_P(zval_ptr)); \
  182. Z_TYPE_P(zval_ptr) = IS_ARRAY;
  183. #define ZVAL_OBJ(zval_ptr, call_create) \
  184. Z_TYPE_P(zval_ptr) = IS_OBJECT; \
  185. Z_OBJVAL_P(zval_ptr) = call_create;
  186. #define UNBOX(class_name, val) \
  187. (class_name*)zend_object_store_get_object(val TSRMLS_CC);
  188. #define UNBOX_HASHTABLE_VALUE(class_name, val) UNBOX(class_name, val)
  189. #define HASHTABLE_VALUE_DTOR ZVAL_PTR_DTOR
  190. #define PHP_PROTO_HASHTABLE_VALUE zval*
  191. #define HASHTABLE_VALUE_CE(val) Z_OBJCE_P(val)
  192. #define CREATE_HASHTABLE_VALUE(OBJ, WRAPPED_OBJ, OBJ_TYPE, OBJ_CLASS_ENTRY) \
  193. OBJ_TYPE* OBJ; \
  194. PHP_PROTO_HASHTABLE_VALUE WRAPPED_OBJ; \
  195. MAKE_STD_ZVAL(WRAPPED_OBJ); \
  196. ZVAL_OBJ(WRAPPED_OBJ, \
  197. OBJ_CLASS_ENTRY->create_object(OBJ_CLASS_ENTRY TSRMLS_CC)); \
  198. OBJ = UNBOX_HASHTABLE_VALUE(OBJ_TYPE, WRAPPED_OBJ); \
  199. Z_DELREF_P(desc_php);
  200. #define PHP_PROTO_CE_DECLARE zend_class_entry**
  201. #define PHP_PROTO_CE_UNREF(ce) (*ce)
  202. #define php_proto_zend_lookup_class(name, name_length, ce) \
  203. zend_lookup_class(name, name_length, ce TSRMLS_CC)
  204. #define PHP_PROTO_RETVAL_ZVAL(value) ZVAL_ZVAL(return_value, value, 1, 0)
  205. #else // PHP_MAJOR_VERSION >= 7
  206. #define php_proto_zend_literal void**
  207. #define PHP_PROTO_CASE_IS_BOOL IS_TRUE: case IS_FALSE
  208. #define PHP_PROTO_SIZE size_t
  209. #define PHP_PROTO_LONG zend_long
  210. #define PHP_PROTO_TSRMLS_DC
  211. #define PHP_PROTO_TSRMLS_CC
  212. // PHP String
  213. #define PHP_PROTO_ZVAL_STRING(zval_ptr, s, copy) \
  214. ZVAL_STRING(zval_ptr, s)
  215. #define PHP_PROTO_ZVAL_STRINGL(zval_ptr, s, len, copy) \
  216. ZVAL_STRINGL(zval_ptr, s, len)
  217. #define PHP_PROTO_RETURN_STRING(s, copy) RETURN_STRING(s)
  218. #define PHP_PROTO_RETURN_STRINGL(s, len, copy) RETURN_STRINGL(s, len)
  219. #define PHP_PROTO_RETVAL_STRINGL(s, len, copy) RETVAL_STRINGL(s, len)
  220. #define php_proto_zend_make_printable_zval(from, to) \
  221. zend_make_printable_zval(from, to)
  222. // PHP Array
  223. #define PHP_PROTO_HASH_OF(array) Z_ARRVAL_P(&array)
  224. static inline int php_proto_zend_hash_index_update_zval(HashTable* ht, ulong h,
  225. zval* pData) {
  226. void* result = NULL;
  227. result = zend_hash_index_update(ht, h, pData);
  228. return result != NULL ? SUCCESS : FAILURE;
  229. }
  230. static inline int php_proto_zend_hash_update(HashTable* ht, const char* key,
  231. size_t key_len) {
  232. void* result = NULL;
  233. zval temp;
  234. ZVAL_LONG(&temp, 0);
  235. result = zend_hash_str_update(ht, key, key_len, &temp);
  236. return result != NULL ? SUCCESS : FAILURE;
  237. }
  238. static inline int php_proto_zend_hash_index_update_mem(HashTable* ht, ulong h,
  239. void* pData, uint nDataSize,
  240. void** pDest) {
  241. void* result = NULL;
  242. result = zend_hash_index_update_mem(ht, h, pData, nDataSize);
  243. if (pDest != NULL) *pDest = result;
  244. return result != NULL ? SUCCESS : FAILURE;
  245. }
  246. static inline int php_proto_zend_hash_update_zval(HashTable* ht,
  247. const char* key, uint key_len,
  248. zval* pData) {
  249. zend_string* internal_key = zend_string_init(key, key_len, 0);
  250. zend_hash_update(ht, internal_key, pData);
  251. }
  252. static inline int php_proto_zend_hash_update_mem(HashTable* ht, const char* key,
  253. uint key_len, void* pData,
  254. uint nDataSize, void** pDest) {
  255. zend_string* internal_key = zend_string_init(key, key_len, 0);
  256. void* result = zend_hash_update_mem(ht, internal_key, pData, nDataSize);
  257. zend_string_release(internal_key);
  258. if (pDest != NULL) *pDest = result;
  259. return result != NULL ? SUCCESS : FAILURE;
  260. }
  261. static inline int php_proto_zend_hash_index_find_zval(const HashTable* ht,
  262. ulong h, void** pDest) {
  263. zval* result = zend_hash_index_find(ht, h);
  264. if (pDest != NULL) *pDest = result;
  265. return result != NULL ? SUCCESS : FAILURE;
  266. }
  267. static inline int php_proto_zend_hash_find(const HashTable* ht, const char* key,
  268. size_t key_len, void** pDest) {
  269. void* result = NULL;
  270. result = zend_hash_str_find(ht, key, key_len);
  271. return result != NULL ? SUCCESS : FAILURE;
  272. }
  273. static inline int php_proto_zend_hash_index_find_mem(const HashTable* ht,
  274. ulong h, void** pDest) {
  275. void* result = NULL;
  276. result = zend_hash_index_find_ptr(ht, h);
  277. if (pDest != NULL) *pDest = result;
  278. return result != NULL ? SUCCESS : FAILURE;
  279. }
  280. static inline int php_proto_zend_hash_find_zval(const HashTable* ht,
  281. const char* key, uint key_len,
  282. void** pDest) {
  283. zend_string* internal_key = zend_string_init(key, key_len, 1);
  284. zval* result = zend_hash_find(ht, internal_key);
  285. if (pDest != NULL) *pDest = result;
  286. return result != NULL ? SUCCESS : FAILURE;
  287. }
  288. static inline int php_proto_zend_hash_find_mem(const HashTable* ht,
  289. const char* key, uint key_len,
  290. void** pDest) {
  291. zend_string* internal_key = zend_string_init(key, key_len, 1);
  292. void* result = zend_hash_find_ptr(ht, internal_key);
  293. zend_string_release(internal_key);
  294. if (pDest != NULL) *pDest = result;
  295. return result != NULL ? SUCCESS : FAILURE;
  296. }
  297. static inline int php_proto_zend_hash_next_index_insert_zval(HashTable* ht,
  298. void* pData) {
  299. zval tmp;
  300. ZVAL_OBJ(&tmp, *(zend_object**)pData);
  301. zval* result = zend_hash_next_index_insert(ht, &tmp);
  302. return result != NULL ? SUCCESS : FAILURE;
  303. }
  304. static inline int php_proto_zend_hash_next_index_insert_mem(HashTable* ht,
  305. void* pData,
  306. uint nDataSize,
  307. void** pDest) {
  308. void* result = NULL;
  309. result = zend_hash_next_index_insert_mem(ht, pData, nDataSize);
  310. if (pDest != NULL) *pDest = result;
  311. return result != NULL ? SUCCESS : FAILURE;
  312. }
  313. static inline int php_proto_zend_hash_get_current_data_ex(HashTable* ht,
  314. void** pDest,
  315. HashPosition* pos) {
  316. void* result = NULL;
  317. result = zend_hash_get_current_data_ex(ht, pos);
  318. if (pDest != NULL) *pDest = result;
  319. return result != NULL ? SUCCESS : FAILURE;
  320. }
  321. // PHP Object
  322. #define PHP_PROTO_WRAP_OBJECT_START(name) struct name {
  323. #define PHP_PROTO_WRAP_OBJECT_END \
  324. zend_object std; \
  325. };
  326. #define PHP_PROTO_INIT_SUBMSGCLASS_START(CLASSNAME, CAMELNAME, LOWWERNAME) \
  327. void LOWWERNAME##_init(TSRMLS_D) { \
  328. zend_class_entry class_type; \
  329. const char* class_name = CLASSNAME; \
  330. INIT_CLASS_ENTRY_EX(class_type, CLASSNAME, strlen(CLASSNAME), \
  331. LOWWERNAME##_methods); \
  332. LOWWERNAME##_type = zend_register_internal_class(&class_type TSRMLS_CC); \
  333. zend_do_inheritance(LOWWERNAME##_type, message_type TSRMLS_CC);
  334. #define PHP_PROTO_INIT_SUBMSGCLASS_END \
  335. }
  336. #define PHP_PROTO_INIT_ENUMCLASS_START(CLASSNAME, CAMELNAME, LOWWERNAME) \
  337. void LOWWERNAME##_init(TSRMLS_D) { \
  338. zend_class_entry class_type; \
  339. const char* class_name = CLASSNAME; \
  340. INIT_CLASS_ENTRY_EX(class_type, CLASSNAME, strlen(CLASSNAME), \
  341. LOWWERNAME##_methods); \
  342. LOWWERNAME##_type = zend_register_internal_class(&class_type TSRMLS_CC);
  343. #define PHP_PROTO_INIT_ENUMCLASS_END \
  344. }
  345. #define PHP_PROTO_INIT_CLASS_START(CLASSNAME, CAMELNAME, LOWWERNAME) \
  346. void LOWWERNAME##_init(TSRMLS_D) { \
  347. zend_class_entry class_type; \
  348. const char* class_name = CLASSNAME; \
  349. INIT_CLASS_ENTRY_EX(class_type, CLASSNAME, strlen(CLASSNAME), \
  350. LOWWERNAME##_methods); \
  351. LOWWERNAME##_type = zend_register_internal_class(&class_type TSRMLS_CC); \
  352. LOWWERNAME##_type->create_object = LOWWERNAME##_create; \
  353. LOWWERNAME##_handlers = PEMALLOC(zend_object_handlers); \
  354. memcpy(LOWWERNAME##_handlers, zend_get_std_object_handlers(), \
  355. sizeof(zend_object_handlers)); \
  356. LOWWERNAME##_handlers->free_obj = LOWWERNAME##_free; \
  357. LOWWERNAME##_handlers->dtor_obj = LOWWERNAME##_dtor; \
  358. LOWWERNAME##_handlers->offset = XtOffsetOf(CAMELNAME, std);
  359. #define PHP_PROTO_INIT_CLASS_END \
  360. }
  361. #define PHP_PROTO_OBJECT_FREE_START(classname, lowername) \
  362. void lowername##_free(zend_object* object) { \
  363. classname* intern = \
  364. (classname*)((char*)object - XtOffsetOf(classname, std));
  365. #define PHP_PROTO_OBJECT_FREE_END \
  366. }
  367. #define PHP_PROTO_OBJECT_DTOR_START(classname, lowername) \
  368. void lowername##_dtor(zend_object* object) { \
  369. classname* intern = \
  370. (classname*)((char*)object - XtOffsetOf(classname, std));
  371. #define PHP_PROTO_OBJECT_DTOR_END \
  372. zend_object_std_dtor(object TSRMLS_CC); \
  373. }
  374. #define PHP_PROTO_OBJECT_CREATE_START(NAME, LOWWERNAME) \
  375. static zend_object* LOWWERNAME##_create(zend_class_entry* ce TSRMLS_DC) { \
  376. PHP_PROTO_ALLOC_CLASS_OBJECT(NAME, ce); \
  377. zend_object_std_init(&intern->std, ce TSRMLS_CC); \
  378. object_properties_init(&intern->std, ce);
  379. #define PHP_PROTO_OBJECT_CREATE_END(NAME, LOWWERNAME) \
  380. PHP_PROTO_FREE_CLASS_OBJECT(NAME, LOWWERNAME##_free, LOWWERNAME##_handlers); \
  381. }
  382. #define CACHED_VALUE zval
  383. #define CACHED_TO_ZVAL_PTR(VALUE) (&VALUE)
  384. #define CACHED_PTR_TO_ZVAL_PTR(VALUE) (VALUE)
  385. #define ZVAL_PTR_TO_CACHED_PTR(VALUE) (VALUE)
  386. #define ZVAL_PTR_TO_CACHED_VALUE(VALUE) (*VALUE)
  387. #define ZVAL_TO_CACHED_VALUE(VALUE) (VALUE)
  388. #define CREATE_OBJ_ON_ALLOCATED_ZVAL_PTR(zval_ptr, class_type) \
  389. ZVAL_OBJ(zval_ptr, class_type->create_object(class_type));
  390. #define PHP_PROTO_SEPARATE_ZVAL_IF_NOT_REF(value) ;
  391. #define PHP_PROTO_GLOBAL_UNINITIALIZED_ZVAL &EG(uninitialized_zval)
  392. #define php_proto_zval_ptr_dtor(zval_ptr) \
  393. zval_ptr_dtor(zval_ptr)
  394. #define PHP_PROTO_ALLOC_CLASS_OBJECT(class_object, class_type) \
  395. class_object* intern; \
  396. int size = sizeof(class_object) + zend_object_properties_size(class_type); \
  397. intern = ecalloc(1, size); \
  398. memset(intern, 0, size);
  399. #define PHP_PROTO_FREE_CLASS_OBJECT(class_object, class_object_free, handler) \
  400. intern->std.handlers = handler; \
  401. return &intern->std;
  402. #define PHP_PROTO_ALLOC_ARRAY(zval_ptr) \
  403. ZVAL_NEW_ARR(zval_ptr)
  404. #define UNBOX(class_name, val) \
  405. (class_name*)((char*)Z_OBJ_P(val) - XtOffsetOf(class_name, std));
  406. #define UNBOX_HASHTABLE_VALUE(class_name, val) \
  407. (class_name*)((char*)val - XtOffsetOf(class_name, std))
  408. #define HASHTABLE_VALUE_DTOR php_proto_hashtable_descriptor_release
  409. #define PHP_PROTO_HASHTABLE_VALUE zend_object*
  410. #define HASHTABLE_VALUE_CE(val) val->ce
  411. #define CREATE_HASHTABLE_VALUE(OBJ, WRAPPED_OBJ, OBJ_TYPE, OBJ_CLASS_ENTRY) \
  412. OBJ_TYPE* OBJ; \
  413. PHP_PROTO_HASHTABLE_VALUE WRAPPED_OBJ; \
  414. WRAPPED_OBJ = OBJ_CLASS_ENTRY->create_object(OBJ_CLASS_ENTRY); \
  415. OBJ = UNBOX_HASHTABLE_VALUE(OBJ_TYPE, WRAPPED_OBJ); \
  416. GC_DELREF(WRAPPED_OBJ);
  417. #define PHP_PROTO_CE_DECLARE zend_class_entry*
  418. #define PHP_PROTO_CE_UNREF(ce) (ce)
  419. static inline int php_proto_zend_lookup_class(
  420. const char* name, int name_length, zend_class_entry** ce TSRMLS_DC) {
  421. zend_string *zstr_name = zend_string_init(name, name_length, 0);
  422. *ce = zend_lookup_class(zstr_name);
  423. zend_string_release(zstr_name);
  424. return *ce != NULL ? SUCCESS : FAILURE;
  425. }
  426. #define PHP_PROTO_RETVAL_ZVAL(value) ZVAL_COPY(return_value, value)
  427. #endif // PHP_MAJOR_VERSION >= 7
  428. #if PHP_MAJOR_VERSION < 7 || (PHP_MAJOR_VERSION == 7 && PHP_MINOR_VERSION == 0)
  429. #define PHP_PROTO_FAKE_SCOPE_BEGIN(klass) \
  430. zend_class_entry* old_scope = EG(scope); \
  431. EG(scope) = klass;
  432. #define PHP_PROTO_FAKE_SCOPE_RESTART(klass) \
  433. old_scope = EG(scope); \
  434. EG(scope) = klass;
  435. #define PHP_PROTO_FAKE_SCOPE_END EG(scope) = old_scope;
  436. #else
  437. #define PHP_PROTO_FAKE_SCOPE_BEGIN(klass) \
  438. zend_class_entry* old_scope = EG(fake_scope); \
  439. EG(fake_scope) = klass;
  440. #define PHP_PROTO_FAKE_SCOPE_RESTART(klass) \
  441. old_scope = EG(fake_scope); \
  442. EG(fake_scope) = klass;
  443. #define PHP_PROTO_FAKE_SCOPE_END EG(fake_scope) = old_scope;
  444. #endif
  445. // Define PHP class
  446. #define DEFINE_PROTOBUF_INIT_CLASS(CLASSNAME, CAMELNAME, LOWERNAME) \
  447. PHP_PROTO_INIT_CLASS_START(CLASSNAME, CAMELNAME, LOWERNAME) \
  448. PHP_PROTO_INIT_CLASS_END
  449. #define DEFINE_PROTOBUF_CREATE(NAME, LOWERNAME) \
  450. PHP_PROTO_OBJECT_CREATE_START(NAME, LOWERNAME) \
  451. LOWERNAME##_init_c_instance(intern TSRMLS_CC); \
  452. PHP_PROTO_OBJECT_CREATE_END(NAME, LOWERNAME)
  453. #define DEFINE_PROTOBUF_FREE(CAMELNAME, LOWERNAME) \
  454. PHP_PROTO_OBJECT_FREE_START(CAMELNAME, LOWERNAME) \
  455. LOWERNAME##_free_c(intern TSRMLS_CC); \
  456. PHP_PROTO_OBJECT_FREE_END
  457. #define DEFINE_PROTOBUF_DTOR(CAMELNAME, LOWERNAME) \
  458. PHP_PROTO_OBJECT_DTOR_START(CAMELNAME, LOWERNAME) \
  459. PHP_PROTO_OBJECT_DTOR_END
  460. #define DEFINE_CLASS(NAME, LOWERNAME, string_name) \
  461. zend_class_entry *LOWERNAME##_type; \
  462. zend_object_handlers *LOWERNAME##_handlers; \
  463. DEFINE_PROTOBUF_FREE(NAME, LOWERNAME) \
  464. DEFINE_PROTOBUF_DTOR(NAME, LOWERNAME) \
  465. DEFINE_PROTOBUF_CREATE(NAME, LOWERNAME) \
  466. DEFINE_PROTOBUF_INIT_CLASS(string_name, NAME, LOWERNAME)
  467. // -----------------------------------------------------------------------------
  468. // Forward Declaration
  469. // ----------------------------------------------------------------------------
  470. struct Any;
  471. struct Api;
  472. struct BoolValue;
  473. struct BytesValue;
  474. struct Descriptor;
  475. struct DescriptorPool;
  476. struct DoubleValue;
  477. struct Duration;
  478. struct Enum;
  479. struct EnumDescriptor;
  480. struct EnumValue;
  481. struct EnumValueDescriptor;
  482. struct Field;
  483. struct FieldDescriptor;
  484. struct FieldMask;
  485. struct Field_Cardinality;
  486. struct Field_Kind;
  487. struct FloatValue;
  488. struct GPBEmpty;
  489. struct Int32Value;
  490. struct Int64Value;
  491. struct InternalDescriptorPool;
  492. struct ListValue;
  493. struct Map;
  494. struct MapIter;
  495. struct MessageField;
  496. struct MessageHeader;
  497. struct MessageLayout;
  498. struct Method;
  499. struct Mixin;
  500. struct NullValue;
  501. struct Oneof;
  502. struct Option;
  503. struct RepeatedField;
  504. struct RepeatedFieldIter;
  505. struct SourceContext;
  506. struct StringValue;
  507. struct Struct;
  508. struct Syntax;
  509. struct Timestamp;
  510. struct Type;
  511. struct UInt32Value;
  512. struct UInt64Value;
  513. struct Value;
  514. typedef struct Any Any;
  515. typedef struct Api Api;
  516. typedef struct BoolValue BoolValue;
  517. typedef struct BytesValue BytesValue;
  518. typedef struct Descriptor Descriptor;
  519. typedef struct DescriptorPool DescriptorPool;
  520. typedef struct DoubleValue DoubleValue;
  521. typedef struct Duration Duration;
  522. typedef struct EnumDescriptor EnumDescriptor;
  523. typedef struct Enum Enum;
  524. typedef struct EnumValueDescriptor EnumValueDescriptor;
  525. typedef struct EnumValue EnumValue;
  526. typedef struct Field_Cardinality Field_Cardinality;
  527. typedef struct FieldDescriptor FieldDescriptor;
  528. typedef struct Field Field;
  529. typedef struct Field_Kind Field_Kind;
  530. typedef struct FieldMask FieldMask;
  531. typedef struct FloatValue FloatValue;
  532. typedef struct GPBEmpty GPBEmpty;
  533. typedef struct Int32Value Int32Value;
  534. typedef struct Int64Value Int64Value;
  535. typedef struct InternalDescriptorPool InternalDescriptorPool;
  536. typedef struct ListValue ListValue;
  537. typedef struct MapIter MapIter;
  538. typedef struct Map Map;
  539. typedef struct MessageField MessageField;
  540. typedef struct MessageHeader MessageHeader;
  541. typedef struct MessageLayout MessageLayout;
  542. typedef struct Method Method;
  543. typedef struct Mixin Mixin;
  544. typedef struct NullValue NullValue;
  545. typedef struct Oneof Oneof;
  546. typedef struct Option Option;
  547. typedef struct RepeatedFieldIter RepeatedFieldIter;
  548. typedef struct RepeatedField RepeatedField;
  549. typedef struct SourceContext SourceContext;
  550. typedef struct StringValue StringValue;
  551. typedef struct Struct Struct;
  552. typedef struct Syntax Syntax;
  553. typedef struct Timestamp Timestamp;
  554. typedef struct Type Type;
  555. typedef struct UInt32Value UInt32Value;
  556. typedef struct UInt64Value UInt64Value;
  557. typedef struct Value Value;
  558. // -----------------------------------------------------------------------------
  559. // Globals.
  560. // -----------------------------------------------------------------------------
  561. ZEND_BEGIN_MODULE_GLOBALS(protobuf)
  562. ZEND_END_MODULE_GLOBALS(protobuf)
  563. // Init module and PHP classes.
  564. void any_init(TSRMLS_D);
  565. void api_init(TSRMLS_D);
  566. void bool_value_init(TSRMLS_D);
  567. void bytes_value_init(TSRMLS_D);
  568. void descriptor_init(TSRMLS_D);
  569. void descriptor_pool_init(TSRMLS_D);
  570. void double_value_init(TSRMLS_D);
  571. void duration_init(TSRMLS_D);
  572. void empty_init(TSRMLS_D);
  573. void enum_descriptor_init(TSRMLS_D);
  574. void enum_init(TSRMLS_D);
  575. void enum_value_init(TSRMLS_D);
  576. void field_cardinality_init(TSRMLS_D);
  577. void field_descriptor_init(TSRMLS_D);
  578. void field_init(TSRMLS_D);
  579. void field_kind_init(TSRMLS_D);
  580. void field_mask_init(TSRMLS_D);
  581. void float_value_init(TSRMLS_D);
  582. void gpb_type_init(TSRMLS_D);
  583. void int32_value_init(TSRMLS_D);
  584. void int64_value_init(TSRMLS_D);
  585. void internal_descriptor_pool_init(TSRMLS_D);
  586. void list_value_init(TSRMLS_D);
  587. void map_field_init(TSRMLS_D);
  588. void map_field_iter_init(TSRMLS_D);
  589. void message_init(TSRMLS_D);
  590. void method_init(TSRMLS_D);
  591. void mixin_init(TSRMLS_D);
  592. void null_value_init(TSRMLS_D);
  593. void oneof_descriptor_init(TSRMLS_D);
  594. void option_init(TSRMLS_D);
  595. void repeated_field_init(TSRMLS_D);
  596. void repeated_field_iter_init(TSRMLS_D);
  597. void source_context_init(TSRMLS_D);
  598. void string_value_init(TSRMLS_D);
  599. void struct_init(TSRMLS_D);
  600. void syntax_init(TSRMLS_D);
  601. void timestamp_init(TSRMLS_D);
  602. void type_init(TSRMLS_D);
  603. void uint32_value_init(TSRMLS_D);
  604. void uint64_value_init(TSRMLS_D);
  605. void util_init(TSRMLS_D);
  606. void value_init(TSRMLS_D);
  607. void gpb_metadata_any_init(TSRMLS_D);
  608. void gpb_metadata_api_init(TSRMLS_D);
  609. void gpb_metadata_duration_init(TSRMLS_D);
  610. void gpb_metadata_field_mask_init(TSRMLS_D);
  611. void gpb_metadata_empty_init(TSRMLS_D);
  612. void gpb_metadata_source_context_init(TSRMLS_D);
  613. void gpb_metadata_struct_init(TSRMLS_D);
  614. void gpb_metadata_timestamp_init(TSRMLS_D);
  615. void gpb_metadata_type_init(TSRMLS_D);
  616. void gpb_metadata_wrappers_init(TSRMLS_D);
  617. // Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
  618. // instances.
  619. void add_def_obj(const void* def, PHP_PROTO_HASHTABLE_VALUE value);
  620. PHP_PROTO_HASHTABLE_VALUE get_def_obj(const void* def);
  621. // Global map from PHP class entries to wrapper Descriptor/EnumDescriptor
  622. // instances.
  623. void add_ce_obj(const void* ce, PHP_PROTO_HASHTABLE_VALUE value);
  624. PHP_PROTO_HASHTABLE_VALUE get_ce_obj(const void* ce);
  625. bool class_added(const void* ce);
  626. // Global map from message/enum's proto fully-qualified name to corresponding
  627. // wrapper Descriptor/EnumDescriptor instances.
  628. void add_proto_obj(const char* proto, PHP_PROTO_HASHTABLE_VALUE value);
  629. PHP_PROTO_HASHTABLE_VALUE get_proto_obj(const char* proto);
  630. extern zend_class_entry* map_field_type;
  631. extern zend_class_entry* repeated_field_type;
  632. // -----------------------------------------------------------------------------
  633. // Descriptor.
  634. // -----------------------------------------------------------------------------
  635. PHP_PROTO_WRAP_OBJECT_START(DescriptorPool)
  636. InternalDescriptorPool* intern;
  637. PHP_PROTO_WRAP_OBJECT_END
  638. PHP_METHOD(DescriptorPool, getGeneratedPool);
  639. PHP_METHOD(DescriptorPool, getDescriptorByClassName);
  640. PHP_METHOD(DescriptorPool, getEnumDescriptorByClassName);
  641. PHP_PROTO_WRAP_OBJECT_START(InternalDescriptorPool)
  642. upb_symtab* symtab;
  643. HashTable* pending_list;
  644. PHP_PROTO_WRAP_OBJECT_END
  645. PHP_METHOD(InternalDescriptorPool, getGeneratedPool);
  646. PHP_METHOD(InternalDescriptorPool, internalAddGeneratedFile);
  647. void internal_add_generated_file(const char* data, PHP_PROTO_SIZE data_len,
  648. InternalDescriptorPool* pool TSRMLS_DC);
  649. void init_generated_pool_once(TSRMLS_D);
  650. // wrapper of generated pool
  651. #if PHP_MAJOR_VERSION < 7
  652. extern zval* generated_pool_php;
  653. extern zval* internal_generated_pool_php;
  654. void descriptor_pool_free(void* object TSRMLS_DC);
  655. void internal_descriptor_pool_free(void* object TSRMLS_DC);
  656. #else
  657. extern zend_object *generated_pool_php;
  658. extern zend_object *internal_generated_pool_php;
  659. void descriptor_pool_free(zend_object* object);
  660. void internal_descriptor_pool_free(zend_object* object);
  661. #endif
  662. extern InternalDescriptorPool* generated_pool; // The actual generated pool
  663. PHP_PROTO_WRAP_OBJECT_START(Descriptor)
  664. const upb_msgdef* msgdef;
  665. MessageLayout* layout;
  666. zend_class_entry* klass; // begins as NULL
  667. const upb_handlers* fill_handlers;
  668. const upb_pbdecodermethod* fill_method;
  669. const upb_json_parsermethod* json_fill_method;
  670. const upb_handlers* pb_serialize_handlers;
  671. const upb_handlers* json_serialize_handlers;
  672. const upb_handlers* json_serialize_handlers_preserve;
  673. PHP_PROTO_WRAP_OBJECT_END
  674. PHP_METHOD(Descriptor, getClass);
  675. PHP_METHOD(Descriptor, getFullName);
  676. PHP_METHOD(Descriptor, getField);
  677. PHP_METHOD(Descriptor, getFieldCount);
  678. PHP_METHOD(Descriptor, getOneofDecl);
  679. PHP_METHOD(Descriptor, getOneofDeclCount);
  680. extern zend_class_entry* descriptor_type;
  681. void descriptor_name_set(Descriptor *desc, const char *name);
  682. PHP_PROTO_WRAP_OBJECT_START(FieldDescriptor)
  683. const upb_fielddef* fielddef;
  684. PHP_PROTO_WRAP_OBJECT_END
  685. PHP_METHOD(FieldDescriptor, getName);
  686. PHP_METHOD(FieldDescriptor, getNumber);
  687. PHP_METHOD(FieldDescriptor, getLabel);
  688. PHP_METHOD(FieldDescriptor, getType);
  689. PHP_METHOD(FieldDescriptor, isMap);
  690. PHP_METHOD(FieldDescriptor, getEnumType);
  691. PHP_METHOD(FieldDescriptor, getMessageType);
  692. extern zend_class_entry* field_descriptor_type;
  693. PHP_PROTO_WRAP_OBJECT_START(EnumDescriptor)
  694. const upb_enumdef* enumdef;
  695. zend_class_entry* klass; // begins as NULL
  696. PHP_PROTO_WRAP_OBJECT_END
  697. PHP_METHOD(EnumDescriptor, getValue);
  698. PHP_METHOD(EnumDescriptor, getValueCount);
  699. extern zend_class_entry* enum_descriptor_type;
  700. PHP_PROTO_WRAP_OBJECT_START(EnumValueDescriptor)
  701. const char* name;
  702. int32_t number;
  703. PHP_PROTO_WRAP_OBJECT_END
  704. PHP_METHOD(EnumValueDescriptor, getName);
  705. PHP_METHOD(EnumValueDescriptor, getNumber);
  706. extern zend_class_entry* enum_value_descriptor_type;
  707. // -----------------------------------------------------------------------------
  708. // Message class creation.
  709. // -----------------------------------------------------------------------------
  710. void* message_data(MessageHeader* msg);
  711. void custom_data_init(const zend_class_entry* ce,
  712. MessageHeader* msg PHP_PROTO_TSRMLS_DC);
  713. // Build PHP class for given descriptor. Instead of building from scratch, this
  714. // function modifies existing class which has been partially defined in PHP
  715. // code.
  716. void build_class_from_descriptor(
  717. PHP_PROTO_HASHTABLE_VALUE php_descriptor TSRMLS_DC);
  718. extern zend_class_entry* message_type;
  719. extern zend_object_handlers* message_handlers;
  720. // -----------------------------------------------------------------------------
  721. // Message layout / storage.
  722. // -----------------------------------------------------------------------------
  723. /*
  724. * In c extension, each protobuf message is a zval instance. The zval instance
  725. * is like union, which can be used to store int, string, zend_object_value and
  726. * etc. For protobuf message, the zval instance is used to store the
  727. * zend_object_value.
  728. *
  729. * The zend_object_value is composed of handlers and a handle to look up the
  730. * actual stored data. The handlers are pointers to functions, e.g., read,
  731. * write, and etc, to access properties.
  732. *
  733. * The actual data of protobuf messages is stored as MessageHeader in zend
  734. * engine's central repository. Each MessageHeader instance is composed of a
  735. * zend_object, a Descriptor instance and the real message data.
  736. *
  737. * For the reason that PHP's native types may not be large enough to store
  738. * protobuf message's field (e.g., int64), all message's data is stored in
  739. * custom memory layout and is indexed by the Descriptor instance.
  740. *
  741. * The zend_object contains the zend class entry and the properties table. The
  742. * zend class entry contains all information about protobuf message's
  743. * corresponding PHP class. The most useful information is the offset table of
  744. * properties. Because read access to properties requires returning zval
  745. * instance, we need to convert data from the custom layout to zval instance.
  746. * Instead of creating zval instance for every read access, we use the zval
  747. * instances in the properties table in the zend_object as cache. When
  748. * accessing properties, the offset is needed to find the zval property in
  749. * zend_object's properties table. These properties will be updated using the
  750. * data from custom memory layout only when reading these properties.
  751. *
  752. * zval
  753. * |-zend_object_value obj
  754. * |-zend_object_handlers* handlers -> |-read_property_handler
  755. * | |-write_property_handler
  756. * | ++++++++++++++++++++++
  757. * |-zend_object_handle handle -> + central repository +
  758. * ++++++++++++++++++++++
  759. * MessageHeader <-----------------|
  760. * |-zend_object std
  761. * | |-class_entry* ce -> class_entry
  762. * | | |-HashTable properties_table (name->offset)
  763. * | |-zval** properties_table <------------------------------|
  764. * | |------> zval* property(cache)
  765. * |-Descriptor* desc (name->offset)
  766. * |-void** data <-----------|
  767. * |-----------------------> void* property(data)
  768. *
  769. */
  770. #define MESSAGE_FIELD_NO_CASE ((size_t)-1)
  771. struct MessageField {
  772. size_t offset;
  773. int cache_index; // Each field except oneof field has a zval cache to avoid
  774. // multiple creation when being accessed.
  775. size_t case_offset; // for oneofs, a uint32. Else, MESSAGE_FIELD_NO_CASE.
  776. };
  777. struct MessageLayout {
  778. const upb_msgdef* msgdef;
  779. MessageField* fields;
  780. size_t size;
  781. };
  782. PHP_PROTO_WRAP_OBJECT_START(MessageHeader)
  783. void* data; // Point to the real message data.
  784. // Place needs to be consistent with map_parse_frame_data_t.
  785. Descriptor* descriptor; // Kept alive by self.class.descriptor reference.
  786. PHP_PROTO_WRAP_OBJECT_END
  787. MessageLayout* create_layout(const upb_msgdef* msgdef);
  788. void layout_init(MessageLayout* layout, void* storage,
  789. zend_object* object PHP_PROTO_TSRMLS_DC);
  790. zval* layout_get(MessageLayout* layout, const void* storage,
  791. const upb_fielddef* field, CACHED_VALUE* cache TSRMLS_DC);
  792. void layout_set(MessageLayout* layout, MessageHeader* header,
  793. const upb_fielddef* field, zval* val TSRMLS_DC);
  794. void layout_merge(MessageLayout* layout, MessageHeader* from,
  795. MessageHeader* to TSRMLS_DC);
  796. const char* layout_get_oneof_case(MessageLayout* layout, const void* storage,
  797. const upb_oneofdef* oneof TSRMLS_DC);
  798. void free_layout(MessageLayout* layout);
  799. void* slot_memory(MessageLayout* layout, const void* storage,
  800. const upb_fielddef* field);
  801. PHP_METHOD(Message, clear);
  802. PHP_METHOD(Message, mergeFrom);
  803. PHP_METHOD(Message, readOneof);
  804. PHP_METHOD(Message, writeOneof);
  805. PHP_METHOD(Message, whichOneof);
  806. PHP_METHOD(Message, __construct);
  807. // -----------------------------------------------------------------------------
  808. // Encode / Decode.
  809. // -----------------------------------------------------------------------------
  810. // Maximum depth allowed during encoding, to avoid stack overflows due to
  811. // cycles.
  812. #define ENCODE_MAX_NESTING 63
  813. // Constructs the upb decoder method for parsing messages of this type.
  814. // This is called from the message class creation code.
  815. const upb_pbdecodermethod *new_fillmsg_decodermethod(Descriptor *desc,
  816. const void *owner);
  817. void serialize_to_string(zval* val, zval* return_value TSRMLS_DC);
  818. void merge_from_string(const char* data, int data_len, const Descriptor* desc,
  819. MessageHeader* msg);
  820. PHP_METHOD(Message, serializeToString);
  821. PHP_METHOD(Message, mergeFromString);
  822. PHP_METHOD(Message, serializeToJsonString);
  823. PHP_METHOD(Message, mergeFromJsonString);
  824. PHP_METHOD(Message, discardUnknownFields);
  825. // -----------------------------------------------------------------------------
  826. // Type check / conversion.
  827. // -----------------------------------------------------------------------------
  828. bool protobuf_convert_to_int32(zval* from, int32_t* to);
  829. bool protobuf_convert_to_uint32(zval* from, uint32_t* to);
  830. bool protobuf_convert_to_int64(zval* from, int64_t* to);
  831. bool protobuf_convert_to_uint64(zval* from, uint64_t* to);
  832. bool protobuf_convert_to_float(zval* from, float* to);
  833. bool protobuf_convert_to_double(zval* from, double* to);
  834. bool protobuf_convert_to_bool(zval* from, int8_t* to);
  835. bool protobuf_convert_to_string(zval* from);
  836. void check_repeated_field(const zend_class_entry* klass, PHP_PROTO_LONG type,
  837. zval* val, zval* return_value);
  838. void check_map_field(const zend_class_entry* klass, PHP_PROTO_LONG key_type,
  839. PHP_PROTO_LONG value_type, zval* val, zval* return_value);
  840. PHP_METHOD(Util, checkInt32);
  841. PHP_METHOD(Util, checkUint32);
  842. PHP_METHOD(Util, checkInt64);
  843. PHP_METHOD(Util, checkUint64);
  844. PHP_METHOD(Util, checkEnum);
  845. PHP_METHOD(Util, checkFloat);
  846. PHP_METHOD(Util, checkDouble);
  847. PHP_METHOD(Util, checkBool);
  848. PHP_METHOD(Util, checkString);
  849. PHP_METHOD(Util, checkBytes);
  850. PHP_METHOD(Util, checkMessage);
  851. PHP_METHOD(Util, checkMapField);
  852. PHP_METHOD(Util, checkRepeatedField);
  853. // -----------------------------------------------------------------------------
  854. // Native slot storage abstraction.
  855. // -----------------------------------------------------------------------------
  856. #define NATIVE_SLOT_MAX_SIZE sizeof(uint64_t)
  857. size_t native_slot_size(upb_fieldtype_t type);
  858. bool native_slot_set(upb_fieldtype_t type, const zend_class_entry* klass,
  859. void* memory, zval* value TSRMLS_DC);
  860. // String/Message is stored differently in array/map from normal message fields.
  861. // So we need to make a special method to handle that.
  862. bool native_slot_set_by_array(upb_fieldtype_t type,
  863. const zend_class_entry* klass, void* memory,
  864. zval* value TSRMLS_DC);
  865. bool native_slot_set_by_map(upb_fieldtype_t type, const zend_class_entry* klass,
  866. void* memory, zval* value TSRMLS_DC);
  867. void native_slot_init(upb_fieldtype_t type, void* memory, CACHED_VALUE* cache);
  868. // For each property, in order to avoid conversion between the zval object and
  869. // the actual data type during parsing/serialization, the containing message
  870. // object use the custom memory layout to store the actual data type for each
  871. // property inside of it. To access a property from php code, the property
  872. // needs to be converted to a zval object. The message object is not responsible
  873. // for providing such a zval object. Instead the caller needs to provide one
  874. // (cache) and update it with the actual data (memory).
  875. void native_slot_get(upb_fieldtype_t type, const void* memory,
  876. CACHED_VALUE* cache TSRMLS_DC);
  877. // String/Message is stored differently in array/map from normal message fields.
  878. // So we need to make a special method to handle that.
  879. void native_slot_get_by_array(upb_fieldtype_t type, const void* memory,
  880. CACHED_VALUE* cache TSRMLS_DC);
  881. void native_slot_get_by_map_key(upb_fieldtype_t type, const void* memory,
  882. int length, CACHED_VALUE* cache TSRMLS_DC);
  883. void native_slot_get_by_map_value(upb_fieldtype_t type, const void* memory,
  884. CACHED_VALUE* cache TSRMLS_DC);
  885. void native_slot_get_default(upb_fieldtype_t type,
  886. CACHED_VALUE* cache TSRMLS_DC);
  887. // -----------------------------------------------------------------------------
  888. // Map Field.
  889. // -----------------------------------------------------------------------------
  890. extern zend_object_handlers* map_field_handlers;
  891. extern zend_object_handlers* map_field_iter_handlers;
  892. PHP_PROTO_WRAP_OBJECT_START(Map)
  893. upb_fieldtype_t key_type;
  894. upb_fieldtype_t value_type;
  895. const zend_class_entry* msg_ce; // class entry for value message
  896. upb_strtable table;
  897. PHP_PROTO_WRAP_OBJECT_END
  898. PHP_PROTO_WRAP_OBJECT_START(MapIter)
  899. Map* self;
  900. upb_strtable_iter it;
  901. PHP_PROTO_WRAP_OBJECT_END
  902. void map_begin(zval* self, MapIter* iter TSRMLS_DC);
  903. void map_next(MapIter* iter);
  904. bool map_done(MapIter* iter);
  905. const char* map_iter_key(MapIter* iter, int* len);
  906. upb_value map_iter_value(MapIter* iter, int* len);
  907. // These operate on a map-entry msgdef.
  908. const upb_fielddef* map_entry_key(const upb_msgdef* msgdef);
  909. const upb_fielddef* map_entry_value(const upb_msgdef* msgdef);
  910. void map_field_create_with_field(const zend_class_entry* ce,
  911. const upb_fielddef* field,
  912. CACHED_VALUE* map_field PHP_PROTO_TSRMLS_DC);
  913. void map_field_create_with_type(const zend_class_entry* ce,
  914. upb_fieldtype_t key_type,
  915. upb_fieldtype_t value_type,
  916. const zend_class_entry* msg_ce,
  917. CACHED_VALUE* map_field PHP_PROTO_TSRMLS_DC);
  918. void* upb_value_memory(upb_value* v);
  919. #define MAP_KEY_FIELD 1
  920. #define MAP_VALUE_FIELD 2
  921. // These operate on a map field (i.e., a repeated field of submessages whose
  922. // submessage type is a map-entry msgdef).
  923. bool is_map_field(const upb_fielddef* field);
  924. const upb_fielddef* map_field_key(const upb_fielddef* field);
  925. const upb_fielddef* map_field_value(const upb_fielddef* field);
  926. bool map_index_set(Map *intern, const char* keyval, int length, upb_value v);
  927. PHP_METHOD(MapField, __construct);
  928. PHP_METHOD(MapField, offsetExists);
  929. PHP_METHOD(MapField, offsetGet);
  930. PHP_METHOD(MapField, offsetSet);
  931. PHP_METHOD(MapField, offsetUnset);
  932. PHP_METHOD(MapField, count);
  933. PHP_METHOD(MapField, getIterator);
  934. PHP_METHOD(MapFieldIter, rewind);
  935. PHP_METHOD(MapFieldIter, current);
  936. PHP_METHOD(MapFieldIter, key);
  937. PHP_METHOD(MapFieldIter, next);
  938. PHP_METHOD(MapFieldIter, valid);
  939. // -----------------------------------------------------------------------------
  940. // Repeated Field.
  941. // -----------------------------------------------------------------------------
  942. extern zend_object_handlers* repeated_field_handlers;
  943. extern zend_object_handlers* repeated_field_iter_handlers;
  944. PHP_PROTO_WRAP_OBJECT_START(RepeatedField)
  945. #if PHP_MAJOR_VERSION < 7
  946. zval* array;
  947. #else
  948. zval array;
  949. #endif
  950. upb_fieldtype_t type;
  951. const zend_class_entry* msg_ce; // class entry for containing message
  952. // (for message field only).
  953. PHP_PROTO_WRAP_OBJECT_END
  954. PHP_PROTO_WRAP_OBJECT_START(RepeatedFieldIter)
  955. RepeatedField* repeated_field;
  956. long position;
  957. PHP_PROTO_WRAP_OBJECT_END
  958. void repeated_field_create_with_field(
  959. zend_class_entry* ce, const upb_fielddef* field,
  960. CACHED_VALUE* repeated_field PHP_PROTO_TSRMLS_DC);
  961. void repeated_field_create_with_type(
  962. zend_class_entry* ce, upb_fieldtype_t type, const zend_class_entry* msg_ce,
  963. CACHED_VALUE* repeated_field PHP_PROTO_TSRMLS_DC);
  964. // Return the element at the index position from the repeated field. There is
  965. // not restriction on the type of stored elements.
  966. void *repeated_field_index_native(RepeatedField *intern, int index TSRMLS_DC);
  967. // Add the element to the end of the repeated field. There is not restriction on
  968. // the type of stored elements.
  969. void repeated_field_push_native(RepeatedField *intern, void *value);
  970. PHP_METHOD(RepeatedField, __construct);
  971. PHP_METHOD(RepeatedField, append);
  972. PHP_METHOD(RepeatedField, offsetExists);
  973. PHP_METHOD(RepeatedField, offsetGet);
  974. PHP_METHOD(RepeatedField, offsetSet);
  975. PHP_METHOD(RepeatedField, offsetUnset);
  976. PHP_METHOD(RepeatedField, count);
  977. PHP_METHOD(RepeatedField, getIterator);
  978. PHP_METHOD(RepeatedFieldIter, rewind);
  979. PHP_METHOD(RepeatedFieldIter, current);
  980. PHP_METHOD(RepeatedFieldIter, key);
  981. PHP_METHOD(RepeatedFieldIter, next);
  982. PHP_METHOD(RepeatedFieldIter, valid);
  983. // -----------------------------------------------------------------------------
  984. // Oneof Field.
  985. // -----------------------------------------------------------------------------
  986. PHP_PROTO_WRAP_OBJECT_START(Oneof)
  987. upb_oneofdef* oneofdef;
  988. int index; // Index of field in oneof. -1 if not set.
  989. char value[NATIVE_SLOT_MAX_SIZE];
  990. PHP_PROTO_WRAP_OBJECT_END
  991. PHP_METHOD(Oneof, getName);
  992. PHP_METHOD(Oneof, getField);
  993. PHP_METHOD(Oneof, getFieldCount);
  994. extern zend_class_entry* oneof_descriptor_type;
  995. // Oneof case slot value to indicate that no oneof case is set. The value `0` is
  996. // safe because field numbers are used as case identifiers, and no field can
  997. // have a number of 0.
  998. #define ONEOF_CASE_NONE 0
  999. // -----------------------------------------------------------------------------
  1000. // Well Known Type.
  1001. // -----------------------------------------------------------------------------
  1002. extern bool is_inited_file_any;
  1003. extern bool is_inited_file_api;
  1004. extern bool is_inited_file_duration;
  1005. extern bool is_inited_file_field_mask;
  1006. extern bool is_inited_file_empty;
  1007. extern bool is_inited_file_source_context;
  1008. extern bool is_inited_file_struct;
  1009. extern bool is_inited_file_timestamp;
  1010. extern bool is_inited_file_type;
  1011. extern bool is_inited_file_wrappers;
  1012. PHP_METHOD(GPBMetadata_Any, initOnce);
  1013. PHP_METHOD(GPBMetadata_Api, initOnce);
  1014. PHP_METHOD(GPBMetadata_Duration, initOnce);
  1015. PHP_METHOD(GPBMetadata_FieldMask, initOnce);
  1016. PHP_METHOD(GPBMetadata_Empty, initOnce);
  1017. PHP_METHOD(GPBMetadata_SourceContext, initOnce);
  1018. PHP_METHOD(GPBMetadata_Struct, initOnce);
  1019. PHP_METHOD(GPBMetadata_Timestamp, initOnce);
  1020. PHP_METHOD(GPBMetadata_Type, initOnce);
  1021. PHP_METHOD(GPBMetadata_Wrappers, initOnce);
  1022. PHP_METHOD(Any, __construct);
  1023. PHP_METHOD(Any, getTypeUrl);
  1024. PHP_METHOD(Any, setTypeUrl);
  1025. PHP_METHOD(Any, getValue);
  1026. PHP_METHOD(Any, setValue);
  1027. PHP_METHOD(Any, unpack);
  1028. PHP_METHOD(Any, pack);
  1029. PHP_METHOD(Any, is);
  1030. PHP_METHOD(Duration, __construct);
  1031. PHP_METHOD(Duration, getSeconds);
  1032. PHP_METHOD(Duration, setSeconds);
  1033. PHP_METHOD(Duration, getNanos);
  1034. PHP_METHOD(Duration, setNanos);
  1035. PHP_METHOD(Timestamp, __construct);
  1036. PHP_METHOD(Timestamp, fromDateTime);
  1037. PHP_METHOD(Timestamp, toDateTime);
  1038. PHP_METHOD(Timestamp, getSeconds);
  1039. PHP_METHOD(Timestamp, setSeconds);
  1040. PHP_METHOD(Timestamp, getNanos);
  1041. PHP_METHOD(Timestamp, setNanos);
  1042. PHP_METHOD(Api, __construct);
  1043. PHP_METHOD(Api, getName);
  1044. PHP_METHOD(Api, setName);
  1045. PHP_METHOD(Api, getMethods);
  1046. PHP_METHOD(Api, setMethods);
  1047. PHP_METHOD(Api, getOptions);
  1048. PHP_METHOD(Api, setOptions);
  1049. PHP_METHOD(Api, getVersion);
  1050. PHP_METHOD(Api, setVersion);
  1051. PHP_METHOD(Api, getSourceContext);
  1052. PHP_METHOD(Api, setSourceContext);
  1053. PHP_METHOD(Api, getMixins);
  1054. PHP_METHOD(Api, setMixins);
  1055. PHP_METHOD(Api, getSyntax);
  1056. PHP_METHOD(Api, setSyntax);
  1057. PHP_METHOD(BoolValue, __construct);
  1058. PHP_METHOD(BoolValue, getValue);
  1059. PHP_METHOD(BoolValue, setValue);
  1060. PHP_METHOD(BytesValue, __construct);
  1061. PHP_METHOD(BytesValue, getValue);
  1062. PHP_METHOD(BytesValue, setValue);
  1063. PHP_METHOD(DoubleValue, __construct);
  1064. PHP_METHOD(DoubleValue, getValue);
  1065. PHP_METHOD(DoubleValue, setValue);
  1066. PHP_METHOD(Enum, __construct);
  1067. PHP_METHOD(Enum, getName);
  1068. PHP_METHOD(Enum, setName);
  1069. PHP_METHOD(Enum, getEnumvalue);
  1070. PHP_METHOD(Enum, setEnumvalue);
  1071. PHP_METHOD(Enum, getOptions);
  1072. PHP_METHOD(Enum, setOptions);
  1073. PHP_METHOD(Enum, getSourceContext);
  1074. PHP_METHOD(Enum, setSourceContext);
  1075. PHP_METHOD(Enum, getSyntax);
  1076. PHP_METHOD(Enum, setSyntax);
  1077. PHP_METHOD(EnumValue, __construct);
  1078. PHP_METHOD(EnumValue, getName);
  1079. PHP_METHOD(EnumValue, setName);
  1080. PHP_METHOD(EnumValue, getNumber);
  1081. PHP_METHOD(EnumValue, setNumber);
  1082. PHP_METHOD(EnumValue, getOptions);
  1083. PHP_METHOD(EnumValue, setOptions);
  1084. PHP_METHOD(FieldMask, __construct);
  1085. PHP_METHOD(FieldMask, getPaths);
  1086. PHP_METHOD(FieldMask, setPaths);
  1087. PHP_METHOD(Field, __construct);
  1088. PHP_METHOD(Field, getKind);
  1089. PHP_METHOD(Field, setKind);
  1090. PHP_METHOD(Field, getCardinality);
  1091. PHP_METHOD(Field, setCardinality);
  1092. PHP_METHOD(Field, getNumber);
  1093. PHP_METHOD(Field, setNumber);
  1094. PHP_METHOD(Field, getName);
  1095. PHP_METHOD(Field, setName);
  1096. PHP_METHOD(Field, getTypeUrl);
  1097. PHP_METHOD(Field, setTypeUrl);
  1098. PHP_METHOD(Field, getOneofIndex);
  1099. PHP_METHOD(Field, setOneofIndex);
  1100. PHP_METHOD(Field, getPacked);
  1101. PHP_METHOD(Field, setPacked);
  1102. PHP_METHOD(Field, getOptions);
  1103. PHP_METHOD(Field, setOptions);
  1104. PHP_METHOD(Field, getJsonName);
  1105. PHP_METHOD(Field, setJsonName);
  1106. PHP_METHOD(Field, getDefaultValue);
  1107. PHP_METHOD(Field, setDefaultValue);
  1108. PHP_METHOD(FloatValue, __construct);
  1109. PHP_METHOD(FloatValue, getValue);
  1110. PHP_METHOD(FloatValue, setValue);
  1111. PHP_METHOD(GPBEmpty, __construct);
  1112. PHP_METHOD(Int32Value, __construct);
  1113. PHP_METHOD(Int32Value, getValue);
  1114. PHP_METHOD(Int32Value, setValue);
  1115. PHP_METHOD(Int64Value, __construct);
  1116. PHP_METHOD(Int64Value, getValue);
  1117. PHP_METHOD(Int64Value, setValue);
  1118. PHP_METHOD(ListValue, __construct);
  1119. PHP_METHOD(ListValue, getValues);
  1120. PHP_METHOD(ListValue, setValues);
  1121. PHP_METHOD(Method, __construct);
  1122. PHP_METHOD(Method, getName);
  1123. PHP_METHOD(Method, setName);
  1124. PHP_METHOD(Method, getRequestTypeUrl);
  1125. PHP_METHOD(Method, setRequestTypeUrl);
  1126. PHP_METHOD(Method, getRequestStreaming);
  1127. PHP_METHOD(Method, setRequestStreaming);
  1128. PHP_METHOD(Method, getResponseTypeUrl);
  1129. PHP_METHOD(Method, setResponseTypeUrl);
  1130. PHP_METHOD(Method, getResponseStreaming);
  1131. PHP_METHOD(Method, setResponseStreaming);
  1132. PHP_METHOD(Method, getOptions);
  1133. PHP_METHOD(Method, setOptions);
  1134. PHP_METHOD(Method, getSyntax);
  1135. PHP_METHOD(Method, setSyntax);
  1136. PHP_METHOD(Mixin, __construct);
  1137. PHP_METHOD(Mixin, getName);
  1138. PHP_METHOD(Mixin, setName);
  1139. PHP_METHOD(Mixin, getRoot);
  1140. PHP_METHOD(Mixin, setRoot);
  1141. PHP_METHOD(Option, __construct);
  1142. PHP_METHOD(Option, getName);
  1143. PHP_METHOD(Option, setName);
  1144. PHP_METHOD(Option, getValue);
  1145. PHP_METHOD(Option, setValue);
  1146. PHP_METHOD(SourceContext, __construct);
  1147. PHP_METHOD(SourceContext, getFileName);
  1148. PHP_METHOD(SourceContext, setFileName);
  1149. PHP_METHOD(StringValue, __construct);
  1150. PHP_METHOD(StringValue, getValue);
  1151. PHP_METHOD(StringValue, setValue);
  1152. PHP_METHOD(Struct, __construct);
  1153. PHP_METHOD(Struct, getFields);
  1154. PHP_METHOD(Struct, setFields);
  1155. PHP_METHOD(Type, __construct);
  1156. PHP_METHOD(Type, getName);
  1157. PHP_METHOD(Type, setName);
  1158. PHP_METHOD(Type, getFields);
  1159. PHP_METHOD(Type, setFields);
  1160. PHP_METHOD(Type, getOneofs);
  1161. PHP_METHOD(Type, setOneofs);
  1162. PHP_METHOD(Type, getOptions);
  1163. PHP_METHOD(Type, setOptions);
  1164. PHP_METHOD(Type, getSourceContext);
  1165. PHP_METHOD(Type, setSourceContext);
  1166. PHP_METHOD(Type, getSyntax);
  1167. PHP_METHOD(Type, setSyntax);
  1168. PHP_METHOD(UInt32Value, __construct);
  1169. PHP_METHOD(UInt32Value, getValue);
  1170. PHP_METHOD(UInt32Value, setValue);
  1171. PHP_METHOD(UInt64Value, __construct);
  1172. PHP_METHOD(UInt64Value, getValue);
  1173. PHP_METHOD(UInt64Value, setValue);
  1174. PHP_METHOD(Value, __construct);
  1175. PHP_METHOD(Value, getNullValue);
  1176. PHP_METHOD(Value, setNullValue);
  1177. PHP_METHOD(Value, getNumberValue);
  1178. PHP_METHOD(Value, setNumberValue);
  1179. PHP_METHOD(Value, getStringValue);
  1180. PHP_METHOD(Value, setStringValue);
  1181. PHP_METHOD(Value, getBoolValue);
  1182. PHP_METHOD(Value, setBoolValue);
  1183. PHP_METHOD(Value, getStructValue);
  1184. PHP_METHOD(Value, setStructValue);
  1185. PHP_METHOD(Value, getListValue);
  1186. PHP_METHOD(Value, setListValue);
  1187. PHP_METHOD(Value, getKind);
  1188. extern zend_class_entry* any_type;
  1189. extern zend_class_entry* api_type;
  1190. extern zend_class_entry* bool_value_type;
  1191. extern zend_class_entry* bytes_value_type;
  1192. extern zend_class_entry* double_value_type;
  1193. extern zend_class_entry* duration_type;
  1194. extern zend_class_entry* empty_type;
  1195. extern zend_class_entry* enum_type;
  1196. extern zend_class_entry* enum_value_type;
  1197. extern zend_class_entry* field_cardinality_type;
  1198. extern zend_class_entry* field_kind_type;
  1199. extern zend_class_entry* field_mask_type;
  1200. extern zend_class_entry* field_type;
  1201. extern zend_class_entry* float_value_type;
  1202. extern zend_class_entry* int32_value_type;
  1203. extern zend_class_entry* int64_value_type;
  1204. extern zend_class_entry* list_value_type;
  1205. extern zend_class_entry* method_type;
  1206. extern zend_class_entry* mixin_type;
  1207. extern zend_class_entry* null_value_type;
  1208. extern zend_class_entry* option_type;
  1209. extern zend_class_entry* source_context_type;
  1210. extern zend_class_entry* string_value_type;
  1211. extern zend_class_entry* struct_type;
  1212. extern zend_class_entry* syntax_type;
  1213. extern zend_class_entry* timestamp_type;
  1214. extern zend_class_entry* type_type;
  1215. extern zend_class_entry* uint32_value_type;
  1216. extern zend_class_entry* uint64_value_type;
  1217. extern zend_class_entry* value_type;
  1218. // -----------------------------------------------------------------------------
  1219. // Upb.
  1220. // -----------------------------------------------------------------------------
  1221. upb_fieldtype_t to_fieldtype(upb_descriptortype_t type);
  1222. const zend_class_entry* field_type_class(
  1223. const upb_fielddef* field PHP_PROTO_TSRMLS_DC);
  1224. // -----------------------------------------------------------------------------
  1225. // Utilities.
  1226. // -----------------------------------------------------------------------------
  1227. // Memory management
  1228. #define ALLOC(class_name) (class_name*) emalloc(sizeof(class_name))
  1229. #define PEMALLOC(class_name) (class_name*) pemalloc(sizeof(class_name), 1)
  1230. #define ALLOC_N(class_name, n) (class_name*) emalloc(sizeof(class_name) * n)
  1231. #define FREE(object) efree(object)
  1232. #define PEFREE(object) pefree(object, 1)
  1233. // String argument.
  1234. #define STR(str) (str), strlen(str)
  1235. // Zend Value
  1236. #if PHP_MAJOR_VERSION < 7
  1237. #define Z_OBJ_P(zval_p) \
  1238. ((zend_object*)(EG(objects_store) \
  1239. .object_buckets[Z_OBJ_HANDLE_P(zval_p)] \
  1240. .bucket.obj.object))
  1241. #endif
  1242. // Message handler
  1243. static inline zval* php_proto_message_read_property(
  1244. zval* msg, zval* member PHP_PROTO_TSRMLS_DC) {
  1245. #if PHP_MAJOR_VERSION < 7
  1246. return message_handlers->read_property(msg, member, BP_VAR_R,
  1247. NULL PHP_PROTO_TSRMLS_CC);
  1248. #else
  1249. return message_handlers->read_property(msg, member, BP_VAR_R, NULL,
  1250. NULL PHP_PROTO_TSRMLS_CC);
  1251. #endif
  1252. }
  1253. // Reserved name
  1254. bool is_reserved_name(const char* name);
  1255. bool is_valid_constant_name(const char* name);
  1256. #endif // __GOOGLE_PROTOBUF_PHP_PROTOBUF_H__