protobuf.h 56 KB

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