protobuf.h 56 KB

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