protobuf.h 57 KB

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