Skip to content

Commit

Permalink
Add any support in php runtime. (protocolbuffers#3486)
Browse files Browse the repository at this point in the history
* Add any support in php runtime.

* Remove unused file in config.m4

* Fix comments

* Fix error for tsrmls build

* Add newly added file to Makefile.am
  • Loading branch information
TeBoring authored Aug 25, 2017
1 parent 98a3734 commit c7457ef
Show file tree
Hide file tree
Showing 10 changed files with 904 additions and 58 deletions.
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ php_EXTRA_DIST= \
php/ext/google/protobuf/upb.c \
php/ext/google/protobuf/protobuf.c \
php/src/phpdoc.dist.xml \
php/src/Google/Protobuf/Any.php \
php/src/Google/Protobuf/Descriptor.php \
php/src/Google/Protobuf/DescriptorPool.php \
php/src/Google/Protobuf/EnumDescriptor.php \
Expand Down Expand Up @@ -664,6 +665,7 @@ php_EXTRA_DIST= \
php/src/Google/Protobuf/Internal/SourceCodeInfo.php \
php/src/Google/Protobuf/Internal/UninterpretedOption_NamePart.php \
php/src/Google/Protobuf/Internal/UninterpretedOption.php \
php/src/GPBMetadata/Google/Protobuf/Any.php \
php/src/GPBMetadata/Google/Protobuf/Internal/Descriptor.php \
php/tests/array_test.php \
php/tests/autoload.php \
Expand Down
57 changes: 20 additions & 37 deletions php/ext/google/protobuf/def.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,33 +115,6 @@ static void append_map_entry_name(char *result, const char *field_name,
check_upb_status(&status, msg); \
} while (0)

// Define PHP class
#define DEFINE_PROTOBUF_INIT_CLASS(CLASSNAME, CAMELNAME, LOWERNAME) \
PHP_PROTO_INIT_CLASS_START(CLASSNAME, CAMELNAME, LOWERNAME) \
PHP_PROTO_INIT_CLASS_END

#define DEFINE_PROTOBUF_CREATE(NAME, LOWERNAME) \
PHP_PROTO_OBJECT_CREATE_START(NAME, LOWERNAME) \
LOWERNAME##_init_c_instance(intern TSRMLS_CC); \
PHP_PROTO_OBJECT_CREATE_END(NAME, LOWERNAME)

#define DEFINE_PROTOBUF_FREE(CAMELNAME, LOWERNAME) \
PHP_PROTO_OBJECT_FREE_START(CAMELNAME, LOWERNAME) \
LOWERNAME##_free_c(intern TSRMLS_CC); \
PHP_PROTO_OBJECT_FREE_END

#define DEFINE_PROTOBUF_DTOR(CAMELNAME, LOWERNAME) \
PHP_PROTO_OBJECT_DTOR_START(CAMELNAME, LOWERNAME) \
PHP_PROTO_OBJECT_DTOR_END

#define DEFINE_CLASS(NAME, LOWERNAME, string_name) \
zend_class_entry *LOWERNAME##_type; \
zend_object_handlers *LOWERNAME##_handlers; \
DEFINE_PROTOBUF_FREE(NAME, LOWERNAME) \
DEFINE_PROTOBUF_DTOR(NAME, LOWERNAME) \
DEFINE_PROTOBUF_CREATE(NAME, LOWERNAME) \
DEFINE_PROTOBUF_INIT_CLASS(string_name, NAME, LOWERNAME)

// -----------------------------------------------------------------------------
// GPBType
// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -657,7 +630,7 @@ zend_object *internal_generated_pool_php;
#endif
InternalDescriptorPool *generated_pool; // The actual generated pool

static void init_generated_pool_once(TSRMLS_D) {
void init_generated_pool_once(TSRMLS_D) {
if (generated_pool == NULL) {
#if PHP_MAJOR_VERSION < 7
MAKE_STD_ZVAL(generated_pool_php);
Expand Down Expand Up @@ -843,18 +816,11 @@ static void convert_to_class_name_inplace(const char *package,
memcpy(classname + i, prefix, prefix_len);
}

PHP_METHOD(InternalDescriptorPool, internalAddGeneratedFile) {
char *data = NULL;
PHP_PROTO_SIZE data_len;
void internal_add_generated_file(const char *data, PHP_PROTO_SIZE data_len,
InternalDescriptorPool *pool TSRMLS_DC) {
upb_filedef **files;
size_t i;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len) ==
FAILURE) {
return;
}

InternalDescriptorPool *pool = UNBOX(InternalDescriptorPool, getThis());
CHECK_UPB(files = upb_loaddescriptor(data, data_len, &pool, &status),
"Parse binary descriptors to internal descriptors failed");

Expand Down Expand Up @@ -913,6 +879,8 @@ PHP_METHOD(InternalDescriptorPool, internalAddGeneratedFile) {
desc->klass = PHP_PROTO_CE_UNREF(pce); \
} \
add_ce_obj(desc->klass, desc_php); \
add_proto_obj(upb_##def_type_lower##_fullname(desc->def_type_lower), \
desc_php); \
efree(classname); \
break; \
}
Expand All @@ -939,6 +907,21 @@ PHP_METHOD(InternalDescriptorPool, internalAddGeneratedFile) {
upb_gfree(files);
}

PHP_METHOD(InternalDescriptorPool, internalAddGeneratedFile) {
char *data = NULL;
PHP_PROTO_SIZE data_len;
upb_filedef **files;
size_t i;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len) ==
FAILURE) {
return;
}

InternalDescriptorPool *pool = UNBOX(InternalDescriptorPool, getThis());
internal_add_generated_file(data, data_len, pool TSRMLS_CC);
}

PHP_METHOD(DescriptorPool, getDescriptorByClassName) {
DescriptorPool *public_pool = UNBOX(DescriptorPool, getThis());
InternalDescriptorPool *pool = public_pool->intern;
Expand Down
41 changes: 24 additions & 17 deletions php/ext/google/protobuf/encode_decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -1445,9 +1445,9 @@ static const upb_handlers* msgdef_json_serialize_handlers(
// PHP encode/decode methods
// -----------------------------------------------------------------------------

PHP_METHOD(Message, serializeToString) {
void serialize_to_string(zval* val, zval* return_value TSRMLS_DC) {
Descriptor* desc =
UNBOX_HASHTABLE_VALUE(Descriptor, get_ce_obj(Z_OBJCE_P(getThis())));
UNBOX_HASHTABLE_VALUE(Descriptor, get_ce_obj(Z_OBJCE_P(val)));

stringsink sink;
stringsink_init(&sink);
Expand All @@ -1461,7 +1461,7 @@ PHP_METHOD(Message, serializeToString) {
stackenv_init(&se, "Error occurred during encoding: %s");
encoder = upb_pb_encoder_create(&se.env, serialize_handlers, &sink.sink);

putmsg(getThis(), desc, upb_pb_encoder_input(encoder), 0 TSRMLS_CC);
putmsg(val, desc, upb_pb_encoder_input(encoder), 0 TSRMLS_CC);

PHP_PROTO_RETVAL_STRINGL(sink.ptr, sink.len, 1);

Expand All @@ -1470,6 +1470,26 @@ PHP_METHOD(Message, serializeToString) {
}
}

PHP_METHOD(Message, serializeToString) {
serialize_to_string(getThis(), return_value TSRMLS_CC);
}

void merge_from_string(const char* data, int data_len, const Descriptor* desc,
MessageHeader* msg) {
const upb_pbdecodermethod* method = msgdef_decodermethod(desc);
const upb_handlers* h = upb_pbdecodermethod_desthandlers(method);
stackenv se;
upb_sink sink;
upb_pbdecoder* decoder;
stackenv_init(&se, "Error occurred during parsing: %s");

upb_sink_reset(&sink, h, msg);
decoder = upb_pbdecoder_create(&se.env, method, &sink);
upb_bufsrc_putbuf(data, data_len, upb_pbdecoder_input(decoder));

stackenv_uninit(&se);
}

PHP_METHOD(Message, mergeFromString) {
Descriptor* desc =
UNBOX_HASHTABLE_VALUE(Descriptor, get_ce_obj(Z_OBJCE_P(getThis())));
Expand All @@ -1483,20 +1503,7 @@ PHP_METHOD(Message, mergeFromString) {
return;
}

{
const upb_pbdecodermethod* method = msgdef_decodermethod(desc);
const upb_handlers* h = upb_pbdecodermethod_desthandlers(method);
stackenv se;
upb_sink sink;
upb_pbdecoder* decoder;
stackenv_init(&se, "Error occurred during parsing: %s");

upb_sink_reset(&sink, h, msg);
decoder = upb_pbdecoder_create(&se.env, method, &sink);
upb_bufsrc_putbuf(data, data_len, upb_pbdecoder_input(decoder));

stackenv_uninit(&se);
}
merge_from_string(data, data_len, desc, msg);
}

PHP_METHOD(Message, serializeToJsonString) {
Expand Down
Loading

0 comments on commit c7457ef

Please sign in to comment.