protobuf.h 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507
  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.7.0"
  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,
  654. bool use_nested_submsg TSRMLS_DC);
  655. void init_generated_pool_once(TSRMLS_D);
  656. // wrapper of generated pool
  657. #if PHP_MAJOR_VERSION < 7
  658. extern zval* generated_pool_php;
  659. extern zval* internal_generated_pool_php;
  660. void descriptor_pool_free(void* object TSRMLS_DC);
  661. void internal_descriptor_pool_free(void* object TSRMLS_DC);
  662. #else
  663. extern zend_object *generated_pool_php;
  664. extern zend_object *internal_generated_pool_php;
  665. void descriptor_pool_free(zend_object* object);
  666. void internal_descriptor_pool_free(zend_object* object);
  667. #endif
  668. extern InternalDescriptorPool* generated_pool; // The actual generated pool
  669. PHP_PROTO_WRAP_OBJECT_START(Descriptor)
  670. const upb_msgdef* msgdef;
  671. MessageLayout* layout;
  672. zend_class_entry* klass; // begins as NULL
  673. const upb_handlers* fill_handlers;
  674. const upb_pbdecodermethod* fill_method;
  675. const upb_json_parsermethod* json_fill_method;
  676. const upb_handlers* pb_serialize_handlers;
  677. const upb_handlers* json_serialize_handlers;
  678. const upb_handlers* json_serialize_handlers_preserve;
  679. PHP_PROTO_WRAP_OBJECT_END
  680. PHP_METHOD(Descriptor, getClass);
  681. PHP_METHOD(Descriptor, getFullName);
  682. PHP_METHOD(Descriptor, getField);
  683. PHP_METHOD(Descriptor, getFieldCount);
  684. PHP_METHOD(Descriptor, getOneofDecl);
  685. PHP_METHOD(Descriptor, getOneofDeclCount);
  686. extern zend_class_entry* descriptor_type;
  687. void descriptor_name_set(Descriptor *desc, const char *name);
  688. PHP_PROTO_WRAP_OBJECT_START(FieldDescriptor)
  689. const upb_fielddef* fielddef;
  690. PHP_PROTO_WRAP_OBJECT_END
  691. PHP_METHOD(FieldDescriptor, getName);
  692. PHP_METHOD(FieldDescriptor, getNumber);
  693. PHP_METHOD(FieldDescriptor, getLabel);
  694. PHP_METHOD(FieldDescriptor, getType);
  695. PHP_METHOD(FieldDescriptor, isMap);
  696. PHP_METHOD(FieldDescriptor, getEnumType);
  697. PHP_METHOD(FieldDescriptor, getMessageType);
  698. extern zend_class_entry* field_descriptor_type;
  699. PHP_PROTO_WRAP_OBJECT_START(EnumDescriptor)
  700. const upb_enumdef* enumdef;
  701. zend_class_entry* klass; // begins as NULL
  702. PHP_PROTO_WRAP_OBJECT_END
  703. PHP_METHOD(EnumDescriptor, getValue);
  704. PHP_METHOD(EnumDescriptor, getValueCount);
  705. extern zend_class_entry* enum_descriptor_type;
  706. PHP_PROTO_WRAP_OBJECT_START(EnumValueDescriptor)
  707. const char* name;
  708. int32_t number;
  709. PHP_PROTO_WRAP_OBJECT_END
  710. PHP_METHOD(EnumValueDescriptor, getName);
  711. PHP_METHOD(EnumValueDescriptor, getNumber);
  712. extern zend_class_entry* enum_value_descriptor_type;
  713. // -----------------------------------------------------------------------------
  714. // Message class creation.
  715. // -----------------------------------------------------------------------------
  716. void* message_data(MessageHeader* msg);
  717. void custom_data_init(const zend_class_entry* ce,
  718. MessageHeader* msg PHP_PROTO_TSRMLS_DC);
  719. // Build PHP class for given descriptor. Instead of building from scratch, this
  720. // function modifies existing class which has been partially defined in PHP
  721. // code.
  722. void build_class_from_descriptor(
  723. PHP_PROTO_HASHTABLE_VALUE php_descriptor TSRMLS_DC);
  724. extern zend_class_entry* message_type;
  725. extern zend_object_handlers* message_handlers;
  726. // -----------------------------------------------------------------------------
  727. // Message layout / storage.
  728. // -----------------------------------------------------------------------------
  729. /*
  730. * In c extension, each protobuf message is a zval instance. The zval instance
  731. * is like union, which can be used to store int, string, zend_object_value and
  732. * etc. For protobuf message, the zval instance is used to store the
  733. * zend_object_value.
  734. *
  735. * The zend_object_value is composed of handlers and a handle to look up the
  736. * actual stored data. The handlers are pointers to functions, e.g., read,
  737. * write, and etc, to access properties.
  738. *
  739. * The actual data of protobuf messages is stored as MessageHeader in zend
  740. * engine's central repository. Each MessageHeader instance is composed of a
  741. * zend_object, a Descriptor instance and the real message data.
  742. *
  743. * For the reason that PHP's native types may not be large enough to store
  744. * protobuf message's field (e.g., int64), all message's data is stored in
  745. * custom memory layout and is indexed by the Descriptor instance.
  746. *
  747. * The zend_object contains the zend class entry and the properties table. The
  748. * zend class entry contains all information about protobuf message's
  749. * corresponding PHP class. The most useful information is the offset table of
  750. * properties. Because read access to properties requires returning zval
  751. * instance, we need to convert data from the custom layout to zval instance.
  752. * Instead of creating zval instance for every read access, we use the zval
  753. * instances in the properties table in the zend_object as cache. When
  754. * accessing properties, the offset is needed to find the zval property in
  755. * zend_object's properties table. These properties will be updated using the
  756. * data from custom memory layout only when reading these properties.
  757. *
  758. * zval
  759. * |-zend_object_value obj
  760. * |-zend_object_handlers* handlers -> |-read_property_handler
  761. * | |-write_property_handler
  762. * | ++++++++++++++++++++++
  763. * |-zend_object_handle handle -> + central repository +
  764. * ++++++++++++++++++++++
  765. * MessageHeader <-----------------|
  766. * |-zend_object std
  767. * | |-class_entry* ce -> class_entry
  768. * | | |-HashTable properties_table (name->offset)
  769. * | |-zval** properties_table <------------------------------|
  770. * | |------> zval* property(cache)
  771. * |-Descriptor* desc (name->offset)
  772. * |-void** data <-----------|
  773. * |-----------------------> void* property(data)
  774. *
  775. */
  776. #define MESSAGE_FIELD_NO_CASE ((size_t)-1)
  777. struct MessageField {
  778. size_t offset;
  779. int cache_index; // Each field except oneof field has a zval cache to avoid
  780. // multiple creation when being accessed.
  781. size_t case_offset; // for oneofs, a uint32. Else, MESSAGE_FIELD_NO_CASE.
  782. };
  783. struct MessageLayout {
  784. const upb_msgdef* msgdef;
  785. MessageField* fields;
  786. size_t size;
  787. };
  788. PHP_PROTO_WRAP_OBJECT_START(MessageHeader)
  789. void* data; // Point to the real message data.
  790. // Place needs to be consistent with map_parse_frame_data_t.
  791. Descriptor* descriptor; // Kept alive by self.class.descriptor reference.
  792. PHP_PROTO_WRAP_OBJECT_END
  793. MessageLayout* create_layout(const upb_msgdef* msgdef);
  794. void layout_init(MessageLayout* layout, void* storage,
  795. zend_object* object PHP_PROTO_TSRMLS_DC);
  796. zval* layout_get(MessageLayout* layout, const void* storage,
  797. const upb_fielddef* field, CACHED_VALUE* cache TSRMLS_DC);
  798. void layout_set(MessageLayout* layout, MessageHeader* header,
  799. const upb_fielddef* field, zval* val TSRMLS_DC);
  800. void layout_merge(MessageLayout* layout, MessageHeader* from,
  801. MessageHeader* to TSRMLS_DC);
  802. const char* layout_get_oneof_case(MessageLayout* layout, const void* storage,
  803. const upb_oneofdef* oneof TSRMLS_DC);
  804. void free_layout(MessageLayout* layout);
  805. void* slot_memory(MessageLayout* layout, const void* storage,
  806. const upb_fielddef* field);
  807. PHP_METHOD(Message, clear);
  808. PHP_METHOD(Message, mergeFrom);
  809. PHP_METHOD(Message, readOneof);
  810. PHP_METHOD(Message, writeOneof);
  811. PHP_METHOD(Message, whichOneof);
  812. PHP_METHOD(Message, __construct);
  813. // -----------------------------------------------------------------------------
  814. // Encode / Decode.
  815. // -----------------------------------------------------------------------------
  816. // Maximum depth allowed during encoding, to avoid stack overflows due to
  817. // cycles.
  818. #define ENCODE_MAX_NESTING 63
  819. // Constructs the upb decoder method for parsing messages of this type.
  820. // This is called from the message class creation code.
  821. const upb_pbdecodermethod *new_fillmsg_decodermethod(Descriptor *desc,
  822. const void *owner);
  823. void serialize_to_string(zval* val, zval* return_value TSRMLS_DC);
  824. void merge_from_string(const char* data, int data_len, Descriptor* desc,
  825. MessageHeader* msg);
  826. PHP_METHOD(Message, serializeToString);
  827. PHP_METHOD(Message, mergeFromString);
  828. PHP_METHOD(Message, serializeToJsonString);
  829. PHP_METHOD(Message, mergeFromJsonString);
  830. PHP_METHOD(Message, discardUnknownFields);
  831. // -----------------------------------------------------------------------------
  832. // Type check / conversion.
  833. // -----------------------------------------------------------------------------
  834. bool protobuf_convert_to_int32(zval* from, int32_t* to);
  835. bool protobuf_convert_to_uint32(zval* from, uint32_t* to);
  836. bool protobuf_convert_to_int64(zval* from, int64_t* to);
  837. bool protobuf_convert_to_uint64(zval* from, uint64_t* to);
  838. bool protobuf_convert_to_float(zval* from, float* to);
  839. bool protobuf_convert_to_double(zval* from, double* to);
  840. bool protobuf_convert_to_bool(zval* from, int8_t* to);
  841. bool protobuf_convert_to_string(zval* from);
  842. void check_repeated_field(const zend_class_entry* klass, PHP_PROTO_LONG type,
  843. zval* val, zval* return_value);
  844. void check_map_field(const zend_class_entry* klass, PHP_PROTO_LONG key_type,
  845. PHP_PROTO_LONG value_type, zval* val, zval* return_value);
  846. PHP_METHOD(Util, checkInt32);
  847. PHP_METHOD(Util, checkUint32);
  848. PHP_METHOD(Util, checkInt64);
  849. PHP_METHOD(Util, checkUint64);
  850. PHP_METHOD(Util, checkEnum);
  851. PHP_METHOD(Util, checkFloat);
  852. PHP_METHOD(Util, checkDouble);
  853. PHP_METHOD(Util, checkBool);
  854. PHP_METHOD(Util, checkString);
  855. PHP_METHOD(Util, checkBytes);
  856. PHP_METHOD(Util, checkMessage);
  857. PHP_METHOD(Util, checkMapField);
  858. PHP_METHOD(Util, checkRepeatedField);
  859. // -----------------------------------------------------------------------------
  860. // Native slot storage abstraction.
  861. // -----------------------------------------------------------------------------
  862. #define NATIVE_SLOT_MAX_SIZE sizeof(uint64_t)
  863. size_t native_slot_size(upb_fieldtype_t type);
  864. bool native_slot_set(upb_fieldtype_t type, const zend_class_entry* klass,
  865. void* memory, zval* value TSRMLS_DC);
  866. // String/Message is stored differently in array/map from normal message fields.
  867. // So we need to make a special method to handle that.
  868. bool native_slot_set_by_array(upb_fieldtype_t type,
  869. const zend_class_entry* klass, void* memory,
  870. zval* value TSRMLS_DC);
  871. bool native_slot_set_by_map(upb_fieldtype_t type, const zend_class_entry* klass,
  872. void* memory, zval* value TSRMLS_DC);
  873. void native_slot_init(upb_fieldtype_t type, void* memory, CACHED_VALUE* cache);
  874. // For each property, in order to avoid conversion between the zval object and
  875. // the actual data type during parsing/serialization, the containing message
  876. // object use the custom memory layout to store the actual data type for each
  877. // property inside of it. To access a property from php code, the property
  878. // needs to be converted to a zval object. The message object is not responsible
  879. // for providing such a zval object. Instead the caller needs to provide one
  880. // (cache) and update it with the actual data (memory).
  881. void native_slot_get(upb_fieldtype_t type, const void* memory,
  882. CACHED_VALUE* cache TSRMLS_DC);
  883. // String/Message is stored differently in array/map from normal message fields.
  884. // So we need to make a special method to handle that.
  885. void native_slot_get_by_array(upb_fieldtype_t type, const void* memory,
  886. CACHED_VALUE* cache TSRMLS_DC);
  887. void native_slot_get_by_map_key(upb_fieldtype_t type, const void* memory,
  888. int length, CACHED_VALUE* cache TSRMLS_DC);
  889. void native_slot_get_by_map_value(upb_fieldtype_t type, const void* memory,
  890. CACHED_VALUE* cache TSRMLS_DC);
  891. void native_slot_get_default(upb_fieldtype_t type,
  892. CACHED_VALUE* cache TSRMLS_DC);
  893. // -----------------------------------------------------------------------------
  894. // Map Field.
  895. // -----------------------------------------------------------------------------
  896. extern zend_object_handlers* map_field_handlers;
  897. extern zend_object_handlers* map_field_iter_handlers;
  898. PHP_PROTO_WRAP_OBJECT_START(Map)
  899. upb_fieldtype_t key_type;
  900. upb_fieldtype_t value_type;
  901. const zend_class_entry* msg_ce; // class entry for value message
  902. upb_strtable table;
  903. PHP_PROTO_WRAP_OBJECT_END
  904. PHP_PROTO_WRAP_OBJECT_START(MapIter)
  905. Map* self;
  906. upb_strtable_iter it;
  907. PHP_PROTO_WRAP_OBJECT_END
  908. void map_begin(zval* self, MapIter* iter TSRMLS_DC);
  909. void map_next(MapIter* iter);
  910. bool map_done(MapIter* iter);
  911. const char* map_iter_key(MapIter* iter, int* len);
  912. upb_value map_iter_value(MapIter* iter, int* len);
  913. // These operate on a map-entry msgdef.
  914. const upb_fielddef* map_entry_key(const upb_msgdef* msgdef);
  915. const upb_fielddef* map_entry_value(const upb_msgdef* msgdef);
  916. void map_field_create_with_field(const zend_class_entry* ce,
  917. const upb_fielddef* field,
  918. CACHED_VALUE* map_field PHP_PROTO_TSRMLS_DC);
  919. void map_field_create_with_type(const zend_class_entry* ce,
  920. upb_fieldtype_t key_type,
  921. upb_fieldtype_t value_type,
  922. const zend_class_entry* msg_ce,
  923. CACHED_VALUE* map_field PHP_PROTO_TSRMLS_DC);
  924. void* upb_value_memory(upb_value* v);
  925. #define MAP_KEY_FIELD 1
  926. #define MAP_VALUE_FIELD 2
  927. // These operate on a map field (i.e., a repeated field of submessages whose
  928. // submessage type is a map-entry msgdef).
  929. bool is_map_field(const upb_fielddef* field);
  930. const upb_fielddef* map_field_key(const upb_fielddef* field);
  931. const upb_fielddef* map_field_value(const upb_fielddef* field);
  932. bool map_index_set(Map *intern, const char* keyval, int length, upb_value v);
  933. PHP_METHOD(MapField, __construct);
  934. PHP_METHOD(MapField, offsetExists);
  935. PHP_METHOD(MapField, offsetGet);
  936. PHP_METHOD(MapField, offsetSet);
  937. PHP_METHOD(MapField, offsetUnset);
  938. PHP_METHOD(MapField, count);
  939. PHP_METHOD(MapField, getIterator);
  940. PHP_METHOD(MapFieldIter, rewind);
  941. PHP_METHOD(MapFieldIter, current);
  942. PHP_METHOD(MapFieldIter, key);
  943. PHP_METHOD(MapFieldIter, next);
  944. PHP_METHOD(MapFieldIter, valid);
  945. // -----------------------------------------------------------------------------
  946. // Repeated Field.
  947. // -----------------------------------------------------------------------------
  948. extern zend_object_handlers* repeated_field_handlers;
  949. extern zend_object_handlers* repeated_field_iter_handlers;
  950. PHP_PROTO_WRAP_OBJECT_START(RepeatedField)
  951. #if PHP_MAJOR_VERSION < 7
  952. zval* array;
  953. #else
  954. zval array;
  955. #endif
  956. upb_fieldtype_t type;
  957. const zend_class_entry* msg_ce; // class entry for containing message
  958. // (for message field only).
  959. PHP_PROTO_WRAP_OBJECT_END
  960. PHP_PROTO_WRAP_OBJECT_START(RepeatedFieldIter)
  961. RepeatedField* repeated_field;
  962. long position;
  963. PHP_PROTO_WRAP_OBJECT_END
  964. void repeated_field_create_with_field(
  965. zend_class_entry* ce, const upb_fielddef* field,
  966. CACHED_VALUE* repeated_field PHP_PROTO_TSRMLS_DC);
  967. void repeated_field_create_with_type(
  968. zend_class_entry* ce, upb_fieldtype_t type, const zend_class_entry* msg_ce,
  969. CACHED_VALUE* repeated_field PHP_PROTO_TSRMLS_DC);
  970. // Return the element at the index position from the repeated field. There is
  971. // not restriction on the type of stored elements.
  972. void *repeated_field_index_native(RepeatedField *intern, int index TSRMLS_DC);
  973. // Add the element to the end of the repeated field. There is not restriction on
  974. // the type of stored elements.
  975. void repeated_field_push_native(RepeatedField *intern, void *value);
  976. PHP_METHOD(RepeatedField, __construct);
  977. PHP_METHOD(RepeatedField, append);
  978. PHP_METHOD(RepeatedField, offsetExists);
  979. PHP_METHOD(RepeatedField, offsetGet);
  980. PHP_METHOD(RepeatedField, offsetSet);
  981. PHP_METHOD(RepeatedField, offsetUnset);
  982. PHP_METHOD(RepeatedField, count);
  983. PHP_METHOD(RepeatedField, getIterator);
  984. PHP_METHOD(RepeatedFieldIter, rewind);
  985. PHP_METHOD(RepeatedFieldIter, current);
  986. PHP_METHOD(RepeatedFieldIter, key);
  987. PHP_METHOD(RepeatedFieldIter, next);
  988. PHP_METHOD(RepeatedFieldIter, valid);
  989. // -----------------------------------------------------------------------------
  990. // Oneof Field.
  991. // -----------------------------------------------------------------------------
  992. PHP_PROTO_WRAP_OBJECT_START(Oneof)
  993. upb_oneofdef* oneofdef;
  994. int index; // Index of field in oneof. -1 if not set.
  995. char value[NATIVE_SLOT_MAX_SIZE];
  996. PHP_PROTO_WRAP_OBJECT_END
  997. PHP_METHOD(Oneof, getName);
  998. PHP_METHOD(Oneof, getField);
  999. PHP_METHOD(Oneof, getFieldCount);
  1000. extern zend_class_entry* oneof_descriptor_type;
  1001. // Oneof case slot value to indicate that no oneof case is set. The value `0` is
  1002. // safe because field numbers are used as case identifiers, and no field can
  1003. // have a number of 0.
  1004. #define ONEOF_CASE_NONE 0
  1005. // -----------------------------------------------------------------------------
  1006. // Well Known Type.
  1007. // -----------------------------------------------------------------------------
  1008. extern bool is_inited_file_any;
  1009. extern bool is_inited_file_api;
  1010. extern bool is_inited_file_duration;
  1011. extern bool is_inited_file_field_mask;
  1012. extern bool is_inited_file_empty;
  1013. extern bool is_inited_file_source_context;
  1014. extern bool is_inited_file_struct;
  1015. extern bool is_inited_file_timestamp;
  1016. extern bool is_inited_file_type;
  1017. extern bool is_inited_file_wrappers;
  1018. PHP_METHOD(GPBMetadata_Any, initOnce);
  1019. PHP_METHOD(GPBMetadata_Api, initOnce);
  1020. PHP_METHOD(GPBMetadata_Duration, initOnce);
  1021. PHP_METHOD(GPBMetadata_FieldMask, initOnce);
  1022. PHP_METHOD(GPBMetadata_Empty, initOnce);
  1023. PHP_METHOD(GPBMetadata_SourceContext, initOnce);
  1024. PHP_METHOD(GPBMetadata_Struct, initOnce);
  1025. PHP_METHOD(GPBMetadata_Timestamp, initOnce);
  1026. PHP_METHOD(GPBMetadata_Type, initOnce);
  1027. PHP_METHOD(GPBMetadata_Wrappers, initOnce);
  1028. PHP_METHOD(Any, __construct);
  1029. PHP_METHOD(Any, getTypeUrl);
  1030. PHP_METHOD(Any, setTypeUrl);
  1031. PHP_METHOD(Any, getValue);
  1032. PHP_METHOD(Any, setValue);
  1033. PHP_METHOD(Any, unpack);
  1034. PHP_METHOD(Any, pack);
  1035. PHP_METHOD(Any, is);
  1036. PHP_METHOD(Duration, __construct);
  1037. PHP_METHOD(Duration, getSeconds);
  1038. PHP_METHOD(Duration, setSeconds);
  1039. PHP_METHOD(Duration, getNanos);
  1040. PHP_METHOD(Duration, setNanos);
  1041. PHP_METHOD(Timestamp, __construct);
  1042. PHP_METHOD(Timestamp, fromDateTime);
  1043. PHP_METHOD(Timestamp, toDateTime);
  1044. PHP_METHOD(Timestamp, getSeconds);
  1045. PHP_METHOD(Timestamp, setSeconds);
  1046. PHP_METHOD(Timestamp, getNanos);
  1047. PHP_METHOD(Timestamp, setNanos);
  1048. PHP_METHOD(Api, __construct);
  1049. PHP_METHOD(Api, getName);
  1050. PHP_METHOD(Api, setName);
  1051. PHP_METHOD(Api, getMethods);
  1052. PHP_METHOD(Api, setMethods);
  1053. PHP_METHOD(Api, getOptions);
  1054. PHP_METHOD(Api, setOptions);
  1055. PHP_METHOD(Api, getVersion);
  1056. PHP_METHOD(Api, setVersion);
  1057. PHP_METHOD(Api, getSourceContext);
  1058. PHP_METHOD(Api, setSourceContext);
  1059. PHP_METHOD(Api, getMixins);
  1060. PHP_METHOD(Api, setMixins);
  1061. PHP_METHOD(Api, getSyntax);
  1062. PHP_METHOD(Api, setSyntax);
  1063. PHP_METHOD(BoolValue, __construct);
  1064. PHP_METHOD(BoolValue, getValue);
  1065. PHP_METHOD(BoolValue, setValue);
  1066. PHP_METHOD(BytesValue, __construct);
  1067. PHP_METHOD(BytesValue, getValue);
  1068. PHP_METHOD(BytesValue, setValue);
  1069. PHP_METHOD(DoubleValue, __construct);
  1070. PHP_METHOD(DoubleValue, getValue);
  1071. PHP_METHOD(DoubleValue, setValue);
  1072. PHP_METHOD(Enum, __construct);
  1073. PHP_METHOD(Enum, getName);
  1074. PHP_METHOD(Enum, setName);
  1075. PHP_METHOD(Enum, getEnumvalue);
  1076. PHP_METHOD(Enum, setEnumvalue);
  1077. PHP_METHOD(Enum, getOptions);
  1078. PHP_METHOD(Enum, setOptions);
  1079. PHP_METHOD(Enum, getSourceContext);
  1080. PHP_METHOD(Enum, setSourceContext);
  1081. PHP_METHOD(Enum, getSyntax);
  1082. PHP_METHOD(Enum, setSyntax);
  1083. PHP_METHOD(EnumValue, __construct);
  1084. PHP_METHOD(EnumValue, getName);
  1085. PHP_METHOD(EnumValue, setName);
  1086. PHP_METHOD(EnumValue, getNumber);
  1087. PHP_METHOD(EnumValue, setNumber);
  1088. PHP_METHOD(EnumValue, getOptions);
  1089. PHP_METHOD(EnumValue, setOptions);
  1090. PHP_METHOD(FieldMask, __construct);
  1091. PHP_METHOD(FieldMask, getPaths);
  1092. PHP_METHOD(FieldMask, setPaths);
  1093. PHP_METHOD(Field, __construct);
  1094. PHP_METHOD(Field, getKind);
  1095. PHP_METHOD(Field, setKind);
  1096. PHP_METHOD(Field, getCardinality);
  1097. PHP_METHOD(Field, setCardinality);
  1098. PHP_METHOD(Field, getNumber);
  1099. PHP_METHOD(Field, setNumber);
  1100. PHP_METHOD(Field, getName);
  1101. PHP_METHOD(Field, setName);
  1102. PHP_METHOD(Field, getTypeUrl);
  1103. PHP_METHOD(Field, setTypeUrl);
  1104. PHP_METHOD(Field, getOneofIndex);
  1105. PHP_METHOD(Field, setOneofIndex);
  1106. PHP_METHOD(Field, getPacked);
  1107. PHP_METHOD(Field, setPacked);
  1108. PHP_METHOD(Field, getOptions);
  1109. PHP_METHOD(Field, setOptions);
  1110. PHP_METHOD(Field, getJsonName);
  1111. PHP_METHOD(Field, setJsonName);
  1112. PHP_METHOD(Field, getDefaultValue);
  1113. PHP_METHOD(Field, setDefaultValue);
  1114. PHP_METHOD(Field_Cardinality, name);
  1115. PHP_METHOD(Field_Cardinality, value);
  1116. PHP_METHOD(Field_Kind, name);
  1117. PHP_METHOD(Field_Kind, value);
  1118. PHP_METHOD(FloatValue, __construct);
  1119. PHP_METHOD(FloatValue, getValue);
  1120. PHP_METHOD(FloatValue, setValue);
  1121. PHP_METHOD(GPBEmpty, __construct);
  1122. PHP_METHOD(Int32Value, __construct);
  1123. PHP_METHOD(Int32Value, getValue);
  1124. PHP_METHOD(Int32Value, setValue);
  1125. PHP_METHOD(Int64Value, __construct);
  1126. PHP_METHOD(Int64Value, getValue);
  1127. PHP_METHOD(Int64Value, setValue);
  1128. PHP_METHOD(ListValue, __construct);
  1129. PHP_METHOD(ListValue, getValues);
  1130. PHP_METHOD(ListValue, setValues);
  1131. PHP_METHOD(Method, __construct);
  1132. PHP_METHOD(Method, getName);
  1133. PHP_METHOD(Method, setName);
  1134. PHP_METHOD(Method, getRequestTypeUrl);
  1135. PHP_METHOD(Method, setRequestTypeUrl);
  1136. PHP_METHOD(Method, getRequestStreaming);
  1137. PHP_METHOD(Method, setRequestStreaming);
  1138. PHP_METHOD(Method, getResponseTypeUrl);
  1139. PHP_METHOD(Method, setResponseTypeUrl);
  1140. PHP_METHOD(Method, getResponseStreaming);
  1141. PHP_METHOD(Method, setResponseStreaming);
  1142. PHP_METHOD(Method, getOptions);
  1143. PHP_METHOD(Method, setOptions);
  1144. PHP_METHOD(Method, getSyntax);
  1145. PHP_METHOD(Method, setSyntax);
  1146. PHP_METHOD(Mixin, __construct);
  1147. PHP_METHOD(Mixin, getName);
  1148. PHP_METHOD(Mixin, setName);
  1149. PHP_METHOD(Mixin, getRoot);
  1150. PHP_METHOD(Mixin, setRoot);
  1151. PHP_METHOD(NullValue, name);
  1152. PHP_METHOD(NullValue, value);
  1153. PHP_METHOD(Option, __construct);
  1154. PHP_METHOD(Option, getName);
  1155. PHP_METHOD(Option, setName);
  1156. PHP_METHOD(Option, getValue);
  1157. PHP_METHOD(Option, setValue);
  1158. PHP_METHOD(SourceContext, __construct);
  1159. PHP_METHOD(SourceContext, getFileName);
  1160. PHP_METHOD(SourceContext, setFileName);
  1161. PHP_METHOD(StringValue, __construct);
  1162. PHP_METHOD(StringValue, getValue);
  1163. PHP_METHOD(StringValue, setValue);
  1164. PHP_METHOD(Struct, __construct);
  1165. PHP_METHOD(Struct, getFields);
  1166. PHP_METHOD(Struct, setFields);
  1167. PHP_METHOD(Syntax, name);
  1168. PHP_METHOD(Syntax, value);
  1169. PHP_METHOD(Type, __construct);
  1170. PHP_METHOD(Type, getName);
  1171. PHP_METHOD(Type, setName);
  1172. PHP_METHOD(Type, getFields);
  1173. PHP_METHOD(Type, setFields);
  1174. PHP_METHOD(Type, getOneofs);
  1175. PHP_METHOD(Type, setOneofs);
  1176. PHP_METHOD(Type, getOptions);
  1177. PHP_METHOD(Type, setOptions);
  1178. PHP_METHOD(Type, getSourceContext);
  1179. PHP_METHOD(Type, setSourceContext);
  1180. PHP_METHOD(Type, getSyntax);
  1181. PHP_METHOD(Type, setSyntax);
  1182. PHP_METHOD(UInt32Value, __construct);
  1183. PHP_METHOD(UInt32Value, getValue);
  1184. PHP_METHOD(UInt32Value, setValue);
  1185. PHP_METHOD(UInt64Value, __construct);
  1186. PHP_METHOD(UInt64Value, getValue);
  1187. PHP_METHOD(UInt64Value, setValue);
  1188. PHP_METHOD(Value, __construct);
  1189. PHP_METHOD(Value, getNullValue);
  1190. PHP_METHOD(Value, setNullValue);
  1191. PHP_METHOD(Value, getNumberValue);
  1192. PHP_METHOD(Value, setNumberValue);
  1193. PHP_METHOD(Value, getStringValue);
  1194. PHP_METHOD(Value, setStringValue);
  1195. PHP_METHOD(Value, getBoolValue);
  1196. PHP_METHOD(Value, setBoolValue);
  1197. PHP_METHOD(Value, getStructValue);
  1198. PHP_METHOD(Value, setStructValue);
  1199. PHP_METHOD(Value, getListValue);
  1200. PHP_METHOD(Value, setListValue);
  1201. PHP_METHOD(Value, getKind);
  1202. extern zend_class_entry* any_type;
  1203. extern zend_class_entry* api_type;
  1204. extern zend_class_entry* bool_value_type;
  1205. extern zend_class_entry* bytes_value_type;
  1206. extern zend_class_entry* double_value_type;
  1207. extern zend_class_entry* duration_type;
  1208. extern zend_class_entry* empty_type;
  1209. extern zend_class_entry* enum_type;
  1210. extern zend_class_entry* enum_value_type;
  1211. extern zend_class_entry* field_cardinality_type;
  1212. extern zend_class_entry* field_kind_type;
  1213. extern zend_class_entry* field_mask_type;
  1214. extern zend_class_entry* field_type;
  1215. extern zend_class_entry* float_value_type;
  1216. extern zend_class_entry* int32_value_type;
  1217. extern zend_class_entry* int64_value_type;
  1218. extern zend_class_entry* list_value_type;
  1219. extern zend_class_entry* method_type;
  1220. extern zend_class_entry* mixin_type;
  1221. extern zend_class_entry* null_value_type;
  1222. extern zend_class_entry* option_type;
  1223. extern zend_class_entry* source_context_type;
  1224. extern zend_class_entry* string_value_type;
  1225. extern zend_class_entry* struct_type;
  1226. extern zend_class_entry* syntax_type;
  1227. extern zend_class_entry* timestamp_type;
  1228. extern zend_class_entry* type_type;
  1229. extern zend_class_entry* uint32_value_type;
  1230. extern zend_class_entry* uint64_value_type;
  1231. extern zend_class_entry* value_type;
  1232. // -----------------------------------------------------------------------------
  1233. // Upb.
  1234. // -----------------------------------------------------------------------------
  1235. upb_fieldtype_t to_fieldtype(upb_descriptortype_t type);
  1236. const zend_class_entry* field_type_class(
  1237. const upb_fielddef* field PHP_PROTO_TSRMLS_DC);
  1238. void stringsink_uninit_opaque(void *sink);
  1239. typedef struct {
  1240. upb_byteshandler handler;
  1241. upb_bytessink sink;
  1242. char *ptr;
  1243. size_t len, size;
  1244. } stringsink;
  1245. void stringsink_init(stringsink *sink);
  1246. void stringsink_uninit(stringsink *sink);
  1247. size_t stringsink_string(void *_sink, const void *hd, const char *ptr,
  1248. size_t len, const upb_bufhandle *handle);
  1249. // -----------------------------------------------------------------------------
  1250. // Utilities.
  1251. // -----------------------------------------------------------------------------
  1252. // Memory management
  1253. #define ALLOC(class_name) (class_name*) emalloc(sizeof(class_name))
  1254. #define PEMALLOC(class_name) (class_name*) pemalloc(sizeof(class_name), 1)
  1255. #define ALLOC_N(class_name, n) (class_name*) emalloc(sizeof(class_name) * n)
  1256. #define FREE(object) efree(object)
  1257. #define PEFREE(object) pefree(object, 1)
  1258. // String argument.
  1259. #define STR(str) (str), strlen(str)
  1260. // Zend Value
  1261. #if PHP_MAJOR_VERSION < 7
  1262. #define Z_OBJ_P(zval_p) \
  1263. ((zend_object*)(EG(objects_store) \
  1264. .object_buckets[Z_OBJ_HANDLE_P(zval_p)] \
  1265. .bucket.obj.object))
  1266. #endif
  1267. // Message handler
  1268. static inline zval* php_proto_message_read_property(
  1269. zval* msg, zval* member PHP_PROTO_TSRMLS_DC) {
  1270. #if PHP_MAJOR_VERSION < 7
  1271. return message_handlers->read_property(msg, member, BP_VAR_R,
  1272. NULL PHP_PROTO_TSRMLS_CC);
  1273. #else
  1274. return message_handlers->read_property(msg, member, BP_VAR_R, NULL,
  1275. NULL PHP_PROTO_TSRMLS_CC);
  1276. #endif
  1277. }
  1278. // Reserved name
  1279. bool is_reserved_name(const char* name);
  1280. bool is_valid_constant_name(const char* name);
  1281. #endif // __GOOGLE_PROTOBUF_PHP_PROTOBUF_H__