Commit c15a3269 authored by Paul Yang's avatar Paul Yang Committed by GitHub

Expose descriptor API in php c extension (#3422)

parent be73938d
......@@ -658,6 +658,7 @@ php_EXTRA_DIST= \
php/tests/array_test.php \
php/tests/autoload.php \
php/tests/compatibility_test.sh \
php/tests/descriptors_test.php \
php/tests/encode_decode_test.php \
php/tests/gdb_test.sh \
php/tests/generated_class_test.php \
......@@ -666,6 +667,7 @@ php_EXTRA_DIST= \
php/tests/map_field_test.php \
php/tests/memory_leak_test.php \
php/tests/php_implementation_test.php \
php/tests/proto/test_descriptors.proto \
php/tests/proto/test_empty_php_namespace.proto \
php/tests/proto/test_import_descriptor_proto.proto \
php/tests/proto/test_include.proto \
......
This diff is collapsed.
......@@ -63,7 +63,6 @@ static void* get_from_table(const HashTable* t, const void* def) {
void** value;
if (php_proto_zend_hash_index_find_mem(t, (zend_ulong)def, (void**)&value) ==
FAILURE) {
zend_error(E_ERROR, "PHP object not found for given definition.\n");
return NULL;
}
return *value;
......@@ -166,6 +165,7 @@ static PHP_RINIT_FUNCTION(protobuf) {
generated_pool = NULL;
generated_pool_php = NULL;
internal_generated_pool_php = NULL;
return 0;
}
......@@ -182,21 +182,40 @@ static PHP_RSHUTDOWN_FUNCTION(protobuf) {
zval_dtor(generated_pool_php);
FREE_ZVAL(generated_pool_php);
}
if (internal_generated_pool_php != NULL) {
zval_dtor(internal_generated_pool_php);
FREE_ZVAL(internal_generated_pool_php);
}
#else
if (generated_pool_php != NULL) {
zval tmp;
ZVAL_OBJ(&tmp, generated_pool_php);
zval_dtor(&tmp);
}
if (internal_generated_pool_php != NULL) {
zval tmp;
ZVAL_OBJ(&tmp, internal_generated_pool_php);
zval_dtor(&tmp);
}
#endif
return 0;
}
static PHP_MINIT_FUNCTION(protobuf) {
descriptor_pool_init(TSRMLS_C);
descriptor_init(TSRMLS_C);
enum_descriptor_init(TSRMLS_C);
enum_value_descriptor_init(TSRMLS_C);
field_descriptor_init(TSRMLS_C);
gpb_type_init(TSRMLS_C);
internal_descriptor_pool_init(TSRMLS_C);
map_field_init(TSRMLS_C);
map_field_iter_init(TSRMLS_C);
message_init(TSRMLS_C);
oneof_descriptor_init(TSRMLS_C);
repeated_field_init(TSRMLS_C);
repeated_field_iter_init(TSRMLS_C);
gpb_type_init(TSRMLS_C);
message_init(TSRMLS_C);
descriptor_pool_init(TSRMLS_C);
descriptor_init(TSRMLS_C);
enum_descriptor_init(TSRMLS_C);
util_init(TSRMLS_C);
return 0;
......
......@@ -185,6 +185,7 @@
#define HASHTABLE_VALUE_DTOR ZVAL_PTR_DTOR
#define PHP_PROTO_HASHTABLE_VALUE zval*
#define HASHTABLE_VALUE_CE(val) Z_OBJCE_P(val)
#define CREATE_HASHTABLE_VALUE(OBJ, WRAPPED_OBJ, OBJ_TYPE, OBJ_CLASS_ENTRY) \
OBJ_TYPE* OBJ; \
......@@ -369,6 +370,7 @@ static inline int php_proto_zend_hash_get_current_data_ex(HashTable* ht,
#define HASHTABLE_VALUE_DTOR php_proto_hashtable_descriptor_release
#define PHP_PROTO_HASHTABLE_VALUE zend_object*
#define HASHTABLE_VALUE_CE(val) val->ce
#define CREATE_HASHTABLE_VALUE(OBJ, WRAPPED_OBJ, OBJ_TYPE, OBJ_CLASS_ENTRY) \
OBJ_TYPE* OBJ; \
......@@ -397,7 +399,9 @@ static inline int php_proto_zend_lookup_class(
struct DescriptorPool;
struct Descriptor;
struct EnumDescriptor;
struct EnumValueDescriptor;
struct FieldDescriptor;
struct InternalDescriptorPool;
struct MessageField;
struct MessageHeader;
struct MessageLayout;
......@@ -410,7 +414,9 @@ struct Oneof;
typedef struct DescriptorPool DescriptorPool;
typedef struct Descriptor Descriptor;
typedef struct EnumDescriptor EnumDescriptor;
typedef struct EnumValueDescriptor EnumValueDescriptor;
typedef struct FieldDescriptor FieldDescriptor;
typedef struct InternalDescriptorPool InternalDescriptorPool;
typedef struct MessageField MessageField;
typedef struct MessageHeader MessageHeader;
typedef struct MessageLayout MessageLayout;
......@@ -431,9 +437,12 @@ ZEND_END_MODULE_GLOBALS(protobuf)
void descriptor_init(TSRMLS_D);
void enum_descriptor_init(TSRMLS_D);
void descriptor_pool_init(TSRMLS_D);
void internal_descriptor_pool_init(TSRMLS_D);
void field_descriptor_init(TSRMLS_D);
void gpb_type_init(TSRMLS_D);
void map_field_init(TSRMLS_D);
void map_field_iter_init(TSRMLS_D);
void oneof_descriptor_init(TSRMLS_D);
void repeated_field_init(TSRMLS_D);
void repeated_field_iter_init(TSRMLS_D);
void util_init(TSRMLS_D);
......@@ -458,22 +467,34 @@ extern zend_class_entry* repeated_field_type;
// -----------------------------------------------------------------------------
PHP_PROTO_WRAP_OBJECT_START(DescriptorPool)
InternalDescriptorPool* intern;
PHP_PROTO_WRAP_OBJECT_END
PHP_METHOD(DescriptorPool, getGeneratedPool);
PHP_METHOD(DescriptorPool, getDescriptorByClassName);
PHP_METHOD(DescriptorPool, getEnumDescriptorByClassName);
PHP_PROTO_WRAP_OBJECT_START(InternalDescriptorPool)
upb_symtab* symtab;
HashTable* pending_list;
PHP_PROTO_WRAP_OBJECT_END
PHP_METHOD(DescriptorPool, getGeneratedPool);
PHP_METHOD(DescriptorPool, internalAddGeneratedFile);
PHP_METHOD(InternalDescriptorPool, getGeneratedPool);
PHP_METHOD(InternalDescriptorPool, internalAddGeneratedFile);
// wrapper of generated pool
#if PHP_MAJOR_VERSION < 7
extern zval* generated_pool_php;
extern zval* internal_generated_pool_php;
void descriptor_pool_free(void* object TSRMLS_DC);
void internal_descriptor_pool_free(void* object TSRMLS_DC);
#else
extern zend_object *generated_pool_php;
extern zend_object *internal_generated_pool_php;
void descriptor_pool_free(zend_object* object);
void internal_descriptor_pool_free(zend_object* object);
#endif
extern DescriptorPool* generated_pool; // The actual generated pool
extern InternalDescriptorPool* generated_pool; // The actual generated pool
PHP_PROTO_WRAP_OBJECT_START(Descriptor)
const upb_msgdef* msgdef;
......@@ -487,6 +508,12 @@ PHP_PROTO_WRAP_OBJECT_START(Descriptor)
const upb_handlers* json_serialize_handlers_preserve;
PHP_PROTO_WRAP_OBJECT_END
PHP_METHOD(Descriptor, getFullName);
PHP_METHOD(Descriptor, getField);
PHP_METHOD(Descriptor, getFieldCount);
PHP_METHOD(Descriptor, getOneofDecl);
PHP_METHOD(Descriptor, getOneofDeclCount);
extern zend_class_entry* descriptor_type;
void descriptor_name_set(Descriptor *desc, const char *name);
......@@ -495,14 +522,36 @@ PHP_PROTO_WRAP_OBJECT_START(FieldDescriptor)
const upb_fielddef* fielddef;
PHP_PROTO_WRAP_OBJECT_END
PHP_METHOD(FieldDescriptor, getName);
PHP_METHOD(FieldDescriptor, getNumber);
PHP_METHOD(FieldDescriptor, getLabel);
PHP_METHOD(FieldDescriptor, getType);
PHP_METHOD(FieldDescriptor, isMap);
PHP_METHOD(FieldDescriptor, getEnumType);
PHP_METHOD(FieldDescriptor, getMessageType);
extern zend_class_entry* field_descriptor_type;
PHP_PROTO_WRAP_OBJECT_START(EnumDescriptor)
const upb_enumdef* enumdef;
zend_class_entry* klass; // begins as NULL
// VALUE module; // begins as nil
PHP_PROTO_WRAP_OBJECT_END
PHP_METHOD(EnumDescriptor, getValue);
PHP_METHOD(EnumDescriptor, getValueCount);
extern zend_class_entry* enum_descriptor_type;
PHP_PROTO_WRAP_OBJECT_START(EnumValueDescriptor)
const char* name;
int32_t number;
PHP_PROTO_WRAP_OBJECT_END
PHP_METHOD(EnumValueDescriptor, getName);
PHP_METHOD(EnumValueDescriptor, getNumber);
extern zend_class_entry* enum_value_descriptor_type;
// -----------------------------------------------------------------------------
// Message class creation.
// -----------------------------------------------------------------------------
......@@ -819,6 +868,12 @@ PHP_PROTO_WRAP_OBJECT_START(Oneof)
char value[NATIVE_SLOT_MAX_SIZE];
PHP_PROTO_WRAP_OBJECT_END
PHP_METHOD(Oneof, getName);
PHP_METHOD(Oneof, getField);
PHP_METHOD(Oneof, getFieldCount);
extern zend_class_entry* oneof_descriptor_type;
// Oneof case slot value to indicate that no oneof case is set. The value `0` is
// safe because field numbers are used as case identifiers, and no field can
// have a number of 0.
......
<?php
require_once('generated/Descriptors/TestDescriptorsEnum.php');
require_once('generated/Descriptors/TestDescriptorsMessage.php');
require_once('test_base.php');
require_once('test_util.php');
use Google\Protobuf\DescriptorPool;
use Google\Protobuf\Internal\RepeatedField;
use Google\Protobuf\Internal\MapField;
use Descriptors\TestDescriptorsEnum;
use Descriptors\TestDescriptorsMessage;
use Descriptors\TestDescriptorsMessage_Sub;
class DescriptorsTest extends TestBase
{
// Redefine these here for compatibility with c extension
const GPBLABEL_OPTIONAL = 1;
const GPBLABEL_REQUIRED = 2;
const GPBLABEL_REPEATED = 3;
const GPBTYPE_DOUBLE = 1;
const GPBTYPE_FLOAT = 2;
const GPBTYPE_INT64 = 3;
const GPBTYPE_UINT64 = 4;
const GPBTYPE_INT32 = 5;
const GPBTYPE_FIXED64 = 6;
const GPBTYPE_FIXED32 = 7;
const GPBTYPE_BOOL = 8;
const GPBTYPE_STRING = 9;
const GPBTYPE_GROUP = 10;
const GPBTYPE_MESSAGE = 11;
const GPBTYPE_BYTES = 12;
const GPBTYPE_UINT32 = 13;
const GPBTYPE_ENUM = 14;
const GPBTYPE_SFIXED32 = 15;
const GPBTYPE_SFIXED64 = 16;
const GPBTYPE_SINT32 = 17;
const GPBTYPE_SINT64 = 18;
#########################################################
# Test descriptor pool.
#########################################################
public function testDescriptorPool()
{
$pool = DescriptorPool::getGeneratedPool();
$desc = $pool->getDescriptorByClassName(get_class(new TestDescriptorsMessage()));
$this->assertInstanceOf('\Google\Protobuf\Descriptor', $desc);
$enumDesc = $pool->getEnumDescriptorByClassName(get_class(new TestDescriptorsEnum()));
$this->assertInstanceOf('\Google\Protobuf\EnumDescriptor', $enumDesc);
}
public function testDescriptorPoolIncorrectArgs()
{
$pool = DescriptorPool::getGeneratedPool();
$desc = $pool->getDescriptorByClassName('NotAClass');
$this->assertNull($desc);
$desc = $pool->getDescriptorByClassName(get_class(new TestDescriptorsEnum()));
$this->assertNull($desc);
$enumDesc = $pool->getEnumDescriptorByClassName(get_class(new TestDescriptorsMessage()));
$this->assertNull($enumDesc);
}
#########################################################
# Test descriptor.
#########################################################
public function testDescriptor()
{
$pool = DescriptorPool::getGeneratedPool();
$desc = $pool->getDescriptorByClassName(get_class(new TestDescriptorsMessage()));
$this->assertSame('descriptors.TestDescriptorsMessage', $desc->getFullName());
$this->assertInstanceOf('\Google\Protobuf\FieldDescriptor', $desc->getField(0));
$this->assertSame(7, $desc->getFieldCount());
$this->assertInstanceOf('\Google\Protobuf\OneofDescriptor', $desc->getOneofDecl(0));
$this->assertSame(1, $desc->getOneofDeclCount());
}
#########################################################
# Test enum descriptor.
#########################################################
public function testEnumDescriptor()
{
// WARNINIG - we need to do this so that TestDescriptorsEnum is registered!!?
new TestDescriptorsMessage();
$pool = DescriptorPool::getGeneratedPool();
$enumDesc = $pool->getEnumDescriptorByClassName(get_class(new TestDescriptorsEnum()));
// Build map of enum values
$enumDescMap = [];
for ($i = 0; $i < $enumDesc->getValueCount(); $i++) {
$enumValueDesc = $enumDesc->getValue($i);
$this->assertInstanceOf('\Google\Protobuf\EnumValueDescriptor', $enumValueDesc);
$enumDescMap[$enumValueDesc->getNumber()] = $enumValueDesc->getName();
}
$this->assertSame('ZERO', $enumDescMap[0]);
$this->assertSame('ONE', $enumDescMap[1]);
$this->assertSame(2, $enumDesc->getValueCount());
}
#########################################################
# Test field descriptor.
#########################################################
public function testFieldDescriptor()
{
$pool = DescriptorPool::getGeneratedPool();
$desc = $pool->getDescriptorByClassName(get_class(new TestDescriptorsMessage()));
$fieldDescMap = $this->buildFieldMap($desc);
// Optional int field
$fieldDesc = $fieldDescMap[1];
$this->assertSame('optional_int32', $fieldDesc->getName());
$this->assertSame(1, $fieldDesc->getNumber());
$this->assertSame(self::GPBLABEL_OPTIONAL, $fieldDesc->getLabel());
$this->assertSame(self::GPBTYPE_INT32, $fieldDesc->getType());
$this->assertFalse($fieldDesc->isMap());
// Optional enum field
$fieldDesc = $fieldDescMap[16];
$this->assertSame('optional_enum', $fieldDesc->getName());
$this->assertSame(16, $fieldDesc->getNumber());
$this->assertSame(self::GPBLABEL_OPTIONAL, $fieldDesc->getLabel());
$this->assertSame(self::GPBTYPE_ENUM, $fieldDesc->getType());
$this->assertInstanceOf('\Google\Protobuf\EnumDescriptor', $fieldDesc->getEnumType());
$this->assertFalse($fieldDesc->isMap());
// Optional message field
$fieldDesc = $fieldDescMap[17];
$this->assertSame('optional_message', $fieldDesc->getName());
$this->assertSame(17, $fieldDesc->getNumber());
$this->assertSame(self::GPBLABEL_OPTIONAL, $fieldDesc->getLabel());
$this->assertSame(self::GPBTYPE_MESSAGE, $fieldDesc->getType());
$this->assertInstanceOf('\Google\Protobuf\Descriptor', $fieldDesc->getMessageType());
$this->assertFalse($fieldDesc->isMap());
// Repeated int field
$fieldDesc = $fieldDescMap[31];
$this->assertSame('repeated_int32', $fieldDesc->getName());
$this->assertSame(31, $fieldDesc->getNumber());
$this->assertSame(self::GPBLABEL_REPEATED, $fieldDesc->getLabel());
$this->assertSame(self::GPBTYPE_INT32, $fieldDesc->getType());
$this->assertFalse($fieldDesc->isMap());
// Repeated message field
$fieldDesc = $fieldDescMap[47];
$this->assertSame('repeated_message', $fieldDesc->getName());
$this->assertSame(47, $fieldDesc->getNumber());
$this->assertSame(self::GPBLABEL_REPEATED, $fieldDesc->getLabel());
$this->assertSame(self::GPBTYPE_MESSAGE, $fieldDesc->getType());
$this->assertInstanceOf('\Google\Protobuf\Descriptor', $fieldDesc->getMessageType());
$this->assertFalse($fieldDesc->isMap());
// Oneof int field
// Tested further in testOneofDescriptor()
$fieldDesc = $fieldDescMap[51];
$this->assertSame('oneof_int32', $fieldDesc->getName());
$this->assertSame(51, $fieldDesc->getNumber());
$this->assertSame(self::GPBLABEL_OPTIONAL, $fieldDesc->getLabel());
$this->assertSame(self::GPBTYPE_INT32, $fieldDesc->getType());
$this->assertFalse($fieldDesc->isMap());
// Map int-enum field
$fieldDesc = $fieldDescMap[71];
$this->assertSame('map_int32_enum', $fieldDesc->getName());
$this->assertSame(71, $fieldDesc->getNumber());
$this->assertSame(self::GPBLABEL_REPEATED, $fieldDesc->getLabel());
$this->assertSame(self::GPBTYPE_MESSAGE, $fieldDesc->getType());
$this->assertTrue($fieldDesc->isMap());
$mapDesc = $fieldDesc->getMessageType();
$this->assertSame('descriptors.TestDescriptorsMessage.MapInt32EnumEntry', $mapDesc->getFullName());
$this->assertSame(self::GPBTYPE_INT32, $mapDesc->getField(0)->getType());
$this->assertSame(self::GPBTYPE_ENUM, $mapDesc->getField(1)->getType());
}
/**
* @expectedException \Exception
*/
public function testFieldDescriptorEnumException()
{
$pool = DescriptorPool::getGeneratedPool();
$desc = $pool->getDescriptorByClassName(get_class(new TestDescriptorsMessage()));
$fieldDesc = $desc->getField(0);
$fieldDesc->getEnumType();
}
/**
* @expectedException \Exception
*/
public function testFieldDescriptorMessageException()
{
$pool = DescriptorPool::getGeneratedPool();
$desc = $pool->getDescriptorByClassName(get_class(new TestDescriptorsMessage()));
$fieldDesc = $desc->getField(0);
$fieldDesc->getMessageType();
}
#########################################################
# Test oneof descriptor.
#########################################################
public function testOneofDescriptor()
{
$pool = DescriptorPool::getGeneratedPool();
$desc = $pool->getDescriptorByClassName(get_class(new TestDescriptorsMessage()));
$fieldDescMap = $this->buildFieldMap($desc);
$fieldDesc = $fieldDescMap[51];
$oneofDesc = $desc->getOneofDecl(0);
$this->assertSame('my_oneof', $oneofDesc->getName());
$fieldDescFromOneof = $oneofDesc->getField(0);
$this->assertSame($fieldDesc, $fieldDescFromOneof);
$this->assertSame(1, $oneofDesc->getFieldCount());
}
private function buildFieldMap($desc)
{
$fieldDescMap = [];
for ($i = 0; $i < $desc->getFieldCount(); $i++) {
$fieldDesc = $desc->getField($i);
$fieldDescMap[$fieldDesc->getNumber()] = $fieldDesc;
}
return $fieldDescMap;
}
}
syntax = "proto3";
package descriptors;
message TestDescriptorsMessage {
int32 optional_int32 = 1;
TestDescriptorsEnum optional_enum = 16;
Sub optional_message = 17;
// Repeated
repeated int32 repeated_int32 = 31;
repeated Sub repeated_message = 47;
oneof my_oneof {
int32 oneof_int32 = 51;
}
map<int32, EnumSub> map_int32_enum = 71;
message Sub {
int32 a = 1;
repeated int32 b = 2;
}
enum EnumSub {
ZERO = 0;
ONE = 1;
}
}
enum TestDescriptorsEnum {
ZERO = 0;
ONE = 1;
}
......@@ -8,7 +8,7 @@ set -e
phpize && ./configure CFLAGS='-g -O0' && make
popd
tests=( array_test.php encode_decode_test.php generated_class_test.php generated_phpdoc_test.php map_field_test.php well_known_test.php generated_service_test.php )
tests=( array_test.php encode_decode_test.php generated_class_test.php generated_phpdoc_test.php map_field_test.php well_known_test.php generated_service_test.php descriptors_test.php )
for t in "${tests[@]}"
do
......
......@@ -347,7 +347,16 @@ generate_php_test_proto() {
# Generate test file
rm -rf generated
mkdir generated
../../src/protoc --php_out=generated proto/test.proto proto/test_include.proto proto/test_no_namespace.proto proto/test_prefix.proto proto/test_php_namespace.proto proto/test_empty_php_namespace.proto proto/test_service.proto proto/test_service_namespace.proto
../../src/protoc --php_out=generated \
proto/test.proto \
proto/test_include.proto \
proto/test_no_namespace.proto \
proto/test_prefix.proto \
proto/test_php_namespace.proto \
proto/test_empty_php_namespace.proto \
proto/test_service.proto \
proto/test_service_namespace.proto \
proto/test_descriptors.proto
pushd ../../src
./protoc --php_out=../php/tests/generated google/protobuf/empty.proto
./protoc --php_out=../php/tests/generated -I../php/tests -I. ../php/tests/proto/test_import_descriptor_proto.proto
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment