message.c 12 KB

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