aboutsummaryrefslogtreecommitdiffhomepage
path: root/php/ext/google
diff options
context:
space:
mode:
authorGravatar Paul Yang <TeBoring@users.noreply.github.com>2017-02-11 16:36:17 -0800
committerGravatar GitHub <noreply@github.com>2017-02-11 16:36:17 -0800
commit74eb9a0a304a3261f3c83e100f51081986ac8ba6 (patch)
treed7171743decd0b21fdf60fc76d5fd0509ebceeda /php/ext/google
parentef927cc428db7bf41d3a593a16a8f1a0fe6306c5 (diff)
Add clear method to PHP message (#2700)
Diffstat (limited to 'php/ext/google')
-rw-r--r--php/ext/google/protobuf/message.c19
-rw-r--r--php/ext/google/protobuf/protobuf.h1
-rw-r--r--php/ext/google/protobuf/storage.c21
3 files changed, 39 insertions, 2 deletions
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,13 +248,30 @@ 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:
case UPB_TYPE_BYTES: {
SEPARATE_ZVAL_IF_NOT_REF(cache);