protobuf.h 60 KB

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