message.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2014 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. #include <php.h>
  31. #include <stdlib.h>
  32. #include "protobuf.h"
  33. static zend_class_entry* message_type;
  34. zend_object_handlers* message_handlers;
  35. static zend_function_entry message_methods[] = {
  36. PHP_ME(Message, clear, NULL, ZEND_ACC_PUBLIC)
  37. PHP_ME(Message, serializeToString, NULL, ZEND_ACC_PUBLIC)
  38. PHP_ME(Message, mergeFromString, NULL, ZEND_ACC_PUBLIC)
  39. PHP_ME(Message, serializeToJsonString, NULL, ZEND_ACC_PUBLIC)
  40. PHP_ME(Message, mergeFromJsonString, NULL, ZEND_ACC_PUBLIC)
  41. PHP_ME(Message, mergeFrom, NULL, ZEND_ACC_PUBLIC)
  42. PHP_ME(Message, readOneof, NULL, ZEND_ACC_PROTECTED)
  43. PHP_ME(Message, writeOneof, NULL, ZEND_ACC_PROTECTED)
  44. PHP_ME(Message, whichOneof, NULL, ZEND_ACC_PROTECTED)
  45. PHP_ME(Message, __construct, NULL, ZEND_ACC_PROTECTED)
  46. {NULL, NULL, NULL}
  47. };
  48. // Forward declare static functions.
  49. #if PHP_MAJOR_VERSION < 7
  50. static void message_set_property(zval* object, zval* member, zval* value,
  51. php_proto_zend_literal key TSRMLS_DC);
  52. static zval* message_get_property(zval* object, zval* member, int type,
  53. const zend_literal* key TSRMLS_DC);
  54. static zval** message_get_property_ptr_ptr(zval* object, zval* member, int type,
  55. php_proto_zend_literal key TSRMLS_DC);
  56. static HashTable* message_get_gc(zval* object, zval*** table, int* n TSRMLS_DC);
  57. #else
  58. static void message_set_property(zval* object, zval* member, zval* value,
  59. void** cache_slot);
  60. static zval* message_get_property(zval* object, zval* member, int type,
  61. void** cache_slot, zval* rv);
  62. static zval* message_get_property_ptr_ptr(zval* object, zval* member, int type,
  63. void** cache_slot);
  64. static HashTable* message_get_gc(zval* object, zval** table, int* n);
  65. #endif
  66. static HashTable* message_get_properties(zval* object TSRMLS_DC);
  67. // -----------------------------------------------------------------------------
  68. // PHP Message Handlers
  69. // -----------------------------------------------------------------------------
  70. // Define object free method.
  71. PHP_PROTO_OBJECT_FREE_START(MessageHeader, message)
  72. FREE(intern->data);
  73. PHP_PROTO_OBJECT_FREE_END
  74. PHP_PROTO_OBJECT_DTOR_START(MessageHeader, message)
  75. PHP_PROTO_OBJECT_DTOR_END
  76. // Define object create method.
  77. PHP_PROTO_OBJECT_CREATE_START(MessageHeader, message)
  78. // Because php call this create func before calling the sub-message's
  79. // constructor defined in PHP, it's possible that the decriptor of this class
  80. // hasn't been added to descritpor pool (when the class is first
  81. // instantiated). In that case, we will defer the initialization of the custom
  82. // data to the parent Message's constructor, which will be called by
  83. // sub-message's constructors after the descriptor has been added.
  84. PHP_PROTO_OBJECT_CREATE_END(MessageHeader, message)
  85. // Init class entry.
  86. PHP_PROTO_INIT_CLASS_START("Google\\Protobuf\\Internal\\Message",
  87. MessageHeader, message)
  88. message_handlers->write_property = message_set_property;
  89. message_handlers->read_property = message_get_property;
  90. message_handlers->get_property_ptr_ptr = message_get_property_ptr_ptr;
  91. message_handlers->get_properties = message_get_properties;
  92. message_handlers->get_gc = message_get_gc;
  93. PHP_PROTO_INIT_CLASS_END
  94. #if PHP_MAJOR_VERSION < 7
  95. static void message_set_property(zval* object, zval* member, zval* value,
  96. php_proto_zend_literal key TSRMLS_DC) {
  97. #else
  98. static void message_set_property(zval* object, zval* member, zval* value,
  99. void** cache_slot) {
  100. #endif
  101. if (Z_TYPE_P(member) != IS_STRING) {
  102. zend_error(E_USER_ERROR, "Unexpected type for field name");
  103. return;
  104. }
  105. #if PHP_MAJOR_VERSION < 7 || (PHP_MAJOR_VERSION == 7 && PHP_MINOR_VERSION == 0)
  106. if (Z_OBJCE_P(object) != EG(scope)) {
  107. #else
  108. if (Z_OBJCE_P(object) != zend_get_executed_scope()) {
  109. #endif
  110. // User cannot set property directly (e.g., $m->a = 1)
  111. zend_error(E_USER_ERROR, "Cannot access private property.");
  112. return;
  113. }
  114. const upb_fielddef* field;
  115. MessageHeader* self = UNBOX(MessageHeader, object);
  116. field = upb_msgdef_ntofz(self->descriptor->msgdef, Z_STRVAL_P(member));
  117. if (field == NULL) {
  118. zend_error(E_USER_ERROR, "Unknown field: %s", Z_STRVAL_P(member));
  119. }
  120. layout_set(self->descriptor->layout, self, field, value TSRMLS_CC);
  121. }
  122. #if PHP_MAJOR_VERSION < 7
  123. static zval* message_get_property(zval* object, zval* member, int type,
  124. const zend_literal* key TSRMLS_DC) {
  125. #else
  126. static zval* message_get_property(zval* object, zval* member, int type,
  127. void** cache_slot, zval* rv) {
  128. #endif
  129. if (Z_TYPE_P(member) != IS_STRING) {
  130. zend_error(E_USER_ERROR, "Property name has to be a string.");
  131. return PHP_PROTO_GLOBAL_UNINITIALIZED_ZVAL;
  132. }
  133. #if PHP_MAJOR_VERSION < 7 || (PHP_MAJOR_VERSION == 7 && PHP_MINOR_VERSION == 0)
  134. if (Z_OBJCE_P(object) != EG(scope)) {
  135. #else
  136. if (Z_OBJCE_P(object) != zend_get_executed_scope()) {
  137. #endif
  138. // User cannot get property directly (e.g., $a = $m->a)
  139. zend_error(E_USER_ERROR, "Cannot access private property.");
  140. return PHP_PROTO_GLOBAL_UNINITIALIZED_ZVAL;
  141. }
  142. MessageHeader* self = UNBOX(MessageHeader, object);
  143. const upb_fielddef* field;
  144. field = upb_msgdef_ntofz(self->descriptor->msgdef, Z_STRVAL_P(member));
  145. if (field == NULL) {
  146. return PHP_PROTO_GLOBAL_UNINITIALIZED_ZVAL;
  147. }
  148. zend_property_info* property_info;
  149. #if PHP_MAJOR_VERSION < 7
  150. property_info =
  151. zend_get_property_info(Z_OBJCE_P(object), member, true TSRMLS_CC);
  152. return layout_get(
  153. self->descriptor->layout, message_data(self), field,
  154. &Z_OBJ_P(object)->properties_table[property_info->offset] TSRMLS_CC);
  155. #else
  156. property_info =
  157. zend_get_property_info(Z_OBJCE_P(object), Z_STR_P(member), true);
  158. return layout_get(
  159. self->descriptor->layout, message_data(self), field,
  160. OBJ_PROP(Z_OBJ_P(object), property_info->offset) TSRMLS_CC);
  161. #endif
  162. }
  163. #if PHP_MAJOR_VERSION < 7
  164. static zval** message_get_property_ptr_ptr(zval* object, zval* member, int type,
  165. php_proto_zend_literal key
  166. TSRMLS_DC) {
  167. #else
  168. static zval* message_get_property_ptr_ptr(zval* object, zval* member, int type,
  169. void** cache_slot) {
  170. #endif
  171. return NULL;
  172. }
  173. static HashTable* message_get_properties(zval* object TSRMLS_DC) {
  174. return NULL;
  175. }
  176. static HashTable* message_get_gc(zval* object, CACHED_VALUE** table,
  177. int* n TSRMLS_DC) {
  178. zend_object* zobj = Z_OBJ_P(object);
  179. *table = zobj->properties_table;
  180. *n = zobj->ce->default_properties_count;
  181. return NULL;
  182. }
  183. // -----------------------------------------------------------------------------
  184. // C Message Utilities
  185. // -----------------------------------------------------------------------------
  186. void* message_data(MessageHeader* msg) {
  187. return msg->data;
  188. }
  189. void custom_data_init(const zend_class_entry* ce,
  190. MessageHeader* intern PHP_PROTO_TSRMLS_DC) {
  191. Descriptor* desc = UNBOX_HASHTABLE_VALUE(Descriptor, get_ce_obj(ce));
  192. intern->data = ALLOC_N(uint8_t, desc->layout->size);
  193. memset(message_data(intern), 0, desc->layout->size);
  194. // We wrap first so that everything in the message object is GC-rooted in
  195. // case a collection happens during object creation in layout_init().
  196. intern->descriptor = desc;
  197. layout_init(desc->layout, message_data(intern),
  198. intern->std.properties_table PHP_PROTO_TSRMLS_CC);
  199. }
  200. void build_class_from_descriptor(
  201. PHP_PROTO_HASHTABLE_VALUE php_descriptor TSRMLS_DC) {
  202. Descriptor* desc = UNBOX_HASHTABLE_VALUE(Descriptor, php_descriptor);
  203. // Map entries don't have existing php class.
  204. if (upb_msgdef_mapentry(desc->msgdef)) {
  205. return;
  206. }
  207. zend_class_entry* registered_ce = desc->klass;
  208. if (desc->layout == NULL) {
  209. MessageLayout* layout = create_layout(desc->msgdef);
  210. desc->layout = layout;
  211. }
  212. registered_ce->create_object = message_create;
  213. }
  214. // -----------------------------------------------------------------------------
  215. // PHP Methods
  216. // -----------------------------------------------------------------------------
  217. // At the first time the message is created, the class entry hasn't been
  218. // modified. As a result, the first created instance will be a normal zend
  219. // object. Here, we manually modify it to our message in such a case.
  220. PHP_METHOD(Message, __construct) {
  221. zend_class_entry* ce = Z_OBJCE_P(getThis());
  222. if (EXPECTED(class_added(ce))) {
  223. MessageHeader* intern = UNBOX(MessageHeader, getThis());
  224. custom_data_init(ce, intern PHP_PROTO_TSRMLS_CC);
  225. }
  226. }
  227. PHP_METHOD(Message, clear) {
  228. MessageHeader* msg = UNBOX(MessageHeader, getThis());
  229. Descriptor* desc = msg->descriptor;
  230. zend_class_entry* ce = desc->klass;
  231. object_properties_init(&msg->std, ce);
  232. layout_init(desc->layout, message_data(msg),
  233. msg->std.properties_table TSRMLS_CC);
  234. }
  235. PHP_METHOD(Message, mergeFrom) {
  236. zval* value;
  237. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &value,
  238. message_type) == FAILURE) {
  239. return;
  240. }
  241. MessageHeader* from = UNBOX(MessageHeader, value);
  242. MessageHeader* to = UNBOX(MessageHeader, getThis());
  243. if(from->descriptor != to->descriptor) {
  244. zend_error(E_USER_ERROR, "Cannot merge messages with different class.");
  245. return;
  246. }
  247. layout_merge(from->descriptor->layout, from, to TSRMLS_CC);
  248. }
  249. PHP_METHOD(Message, readOneof) {
  250. PHP_PROTO_LONG index;
  251. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) ==
  252. FAILURE) {
  253. return;
  254. }
  255. MessageHeader* msg = UNBOX(MessageHeader, getThis());
  256. const upb_fielddef* field = upb_msgdef_itof(msg->descriptor->msgdef, index);
  257. int property_cache_index =
  258. msg->descriptor->layout->fields[upb_fielddef_index(field)].cache_index;
  259. zval* property_ptr = OBJ_PROP(Z_OBJ_P(getThis()), property_cache_index);
  260. // Unlike singular fields, oneof fields share cached property. So we cannot
  261. // let lay_get modify the cached property. Instead, we pass in the return
  262. // value directly.
  263. layout_get(msg->descriptor->layout, message_data(msg), field,
  264. ZVAL_PTR_TO_CACHED_PTR(return_value) TSRMLS_CC);
  265. }
  266. PHP_METHOD(Message, writeOneof) {
  267. PHP_PROTO_LONG index;
  268. zval* value;
  269. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lz", &index, &value) ==
  270. FAILURE) {
  271. return;
  272. }
  273. MessageHeader* msg = UNBOX(MessageHeader, getThis());
  274. const upb_fielddef* field = upb_msgdef_itof(msg->descriptor->msgdef, index);
  275. layout_set(msg->descriptor->layout, msg, field, value TSRMLS_CC);
  276. }
  277. PHP_METHOD(Message, whichOneof) {
  278. char* oneof_name;
  279. PHP_PROTO_SIZE length;
  280. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &oneof_name,
  281. &length) == FAILURE) {
  282. return;
  283. }
  284. MessageHeader* msg = UNBOX(MessageHeader, getThis());
  285. const upb_oneofdef* oneof =
  286. upb_msgdef_ntoo(msg->descriptor->msgdef, oneof_name, length);
  287. const char* oneof_case_name = layout_get_oneof_case(
  288. msg->descriptor->layout, message_data(msg), oneof TSRMLS_CC);
  289. PHP_PROTO_RETURN_STRING(oneof_case_name, 1);
  290. }