From 74eb9a0a304a3261f3c83e100f51081986ac8ba6 Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Sat, 11 Feb 2017 16:36:17 -0800 Subject: Add clear method to PHP message (#2700) --- php/ext/google/protobuf/message.c | 19 +++++++++++++++++++ php/ext/google/protobuf/protobuf.h | 1 + php/ext/google/protobuf/storage.c | 21 +++++++++++++++++++-- 3 files changed, 39 insertions(+), 2 deletions(-) (limited to 'php/ext/google') diff --git a/php/ext/google/protobuf/message.c b/php/ext/google/protobuf/message.c index e8b8ae81..46da9024 100644 --- a/php/ext/google/protobuf/message.c +++ b/php/ext/google/protobuf/message.c @@ -38,6 +38,7 @@ static zend_class_entry* message_type; zend_object_handlers* message_handlers; static zend_function_entry message_methods[] = { + PHP_ME(Message, clear, NULL, ZEND_ACC_PUBLIC) PHP_ME(Message, encode, NULL, ZEND_ACC_PUBLIC) PHP_ME(Message, decode, NULL, ZEND_ACC_PUBLIC) PHP_ME(Message, jsonEncode, NULL, ZEND_ACC_PUBLIC) @@ -241,6 +242,24 @@ PHP_METHOD(Message, __construct) { } } +PHP_METHOD(Message, clear) { + MessageHeader* msg = + (MessageHeader*)zend_object_store_get_object(getThis() TSRMLS_CC); + Descriptor* desc = msg->descriptor; + zend_class_entry* ce = desc->klass; + int i; + + for (i = 0; i < msg->std.ce->default_properties_count; i++) { + zval_ptr_dtor(&msg->std.properties_table[i]); + } + efree(msg->std.properties_table); + + zend_object_std_init(&msg->std, ce TSRMLS_CC); + object_properties_init(&msg->std, ce); + layout_init(desc->layout, message_data(msg), + msg->std.properties_table TSRMLS_CC); +} + PHP_METHOD(Message, readOneof) { long index; diff --git a/php/ext/google/protobuf/protobuf.h b/php/ext/google/protobuf/protobuf.h index 678f2682..d4737fb9 100644 --- a/php/ext/google/protobuf/protobuf.h +++ b/php/ext/google/protobuf/protobuf.h @@ -244,6 +244,7 @@ const char* layout_get_oneof_case(MessageLayout* layout, const void* storage, const upb_oneofdef* oneof TSRMLS_DC); void free_layout(MessageLayout* layout); +PHP_METHOD(Message, clear); PHP_METHOD(Message, readOneof); PHP_METHOD(Message, writeOneof); PHP_METHOD(Message, whichOneof); diff --git a/php/ext/google/protobuf/storage.c b/php/ext/google/protobuf/storage.c index ba6ac59e..5e05b935 100644 --- a/php/ext/google/protobuf/storage.c +++ b/php/ext/google/protobuf/storage.c @@ -248,11 +248,28 @@ void native_slot_get_default(upb_fieldtype_t type, zval** cache TSRMLS_DC) { CASE(DOUBLE, DOUBLE) CASE(BOOL, BOOL) CASE(INT32, LONG) - CASE(INT64, LONG) CASE(UINT32, LONG) - CASE(UINT64, LONG) CASE(ENUM, LONG) +#undef CASE + +#if SIZEOF_LONG == 4 +#define CASE(upb_type) \ + case UPB_TYPE_##upb_type: { \ + SEPARATE_ZVAL_IF_NOT_REF(cache); \ + ZVAL_STRING(*cache, "0", 1); \ + return; \ + } +#else +#define CASE(upb_type) \ + case UPB_TYPE_##upb_type: { \ + SEPARATE_ZVAL_IF_NOT_REF(cache); \ + ZVAL_LONG(*cache, 0); \ + return; \ + } +#endif +CASE(UINT64) +CASE(INT64) #undef CASE case UPB_TYPE_STRING: -- cgit v1.2.3