Skip to content

Commit

Permalink
Merge pull request #7944 from haberman/php-wkt
Browse files Browse the repository at this point in the history
[PHP] Native C well-known types
  • Loading branch information
haberman committed Oct 29, 2020
2 parents fdc3584 + e0151ad commit c5f7b4b
Show file tree
Hide file tree
Showing 50 changed files with 4,508 additions and 761 deletions.
5 changes: 3 additions & 2 deletions Makefile.am
Expand Up @@ -772,13 +772,11 @@ php_EXTRA_DIST= \
php/ext/google/protobuf/arena.h \
php/ext/google/protobuf/array.c \
php/ext/google/protobuf/array.h \
php/ext/google/protobuf/bundled_php.h \
php/ext/google/protobuf/config.m4 \
php/ext/google/protobuf/convert.c \
php/ext/google/protobuf/convert.h \
php/ext/google/protobuf/def.c \
php/ext/google/protobuf/def.h \
php/ext/google/protobuf/make-preload.php \
php/ext/google/protobuf/map.c \
php/ext/google/protobuf/map.h \
php/ext/google/protobuf/message.c \
Expand All @@ -790,6 +788,7 @@ php_EXTRA_DIST= \
php/ext/google/protobuf/php-upb.h \
php/ext/google/protobuf/protobuf.c \
php/ext/google/protobuf/protobuf.h \
php/ext/google/protobuf/wkt.inc \
php/generate_descriptor_protos.sh \
php/phpunit.xml \
php/release.sh \
Expand Down Expand Up @@ -827,6 +826,7 @@ php_EXTRA_DIST= \
php/src/Google/Protobuf/GPBEmpty.php \
php/src/Google/Protobuf/Int32Value.php \
php/src/Google/Protobuf/Int64Value.php \
php/src/Google/Protobuf/Internal/AnyBase.php \
php/src/Google/Protobuf/Internal/CodedInputStream.php \
php/src/Google/Protobuf/Internal/CodedOutputStream.php \
php/src/Google/Protobuf/Internal/Descriptor.php \
Expand Down Expand Up @@ -886,6 +886,7 @@ php_EXTRA_DIST= \
php/src/Google/Protobuf/Internal/ServiceOptions.php \
php/src/Google/Protobuf/Internal/SourceCodeInfo.php \
php/src/Google/Protobuf/Internal/SourceCodeInfo/Location.php \
php/src/Google/Protobuf/Internal/TimestampBase.php \
php/src/Google/Protobuf/Internal/UninterpretedOption.php \
php/src/Google/Protobuf/Internal/UninterpretedOption/NamePart.php \
php/src/Google/Protobuf/Internal/DescriptorProto_ExtensionRange.php \
Expand Down
46 changes: 0 additions & 46 deletions php/ext/google/protobuf/bundled_php.h

This file was deleted.

2 changes: 1 addition & 1 deletion php/ext/google/protobuf/config.m4
Expand Up @@ -4,7 +4,7 @@ if test "$PHP_PROTOBUF" != "no"; then

PHP_NEW_EXTENSION(
protobuf,
arena.c array.c bundled_php.c convert.c def.c map.c message.c names.c php-upb.c protobuf.c,
arena.c array.c convert.c def.c map.c message.c names.c php-upb.c protobuf.c,
$ext_shared)

fi
106 changes: 83 additions & 23 deletions php/ext/google/protobuf/def.c
Expand Up @@ -772,6 +772,7 @@ upb_symtab *DescriptorPool_GetSymbolTable() {
return intern->symtab;
}


/*
* DescriptorPool::getGeneratedPool()
*
Expand Down Expand Up @@ -906,13 +907,38 @@ static void add_name_mappings(const upb_filedef *file) {
}
}

static void add_descriptor(DescriptorPool *pool,
const google_protobuf_FileDescriptorProto *file) {
upb_strview name = google_protobuf_FileDescriptorProto_name(file);
upb_status status;
const upb_filedef *file_def;
upb_status_clear(&status);

if (upb_symtab_lookupfile2(pool->symtab, name.data, name.size)) {
// Already added.
fprintf(stderr, "WARNING: file was already added\n");
return;
}

// The PHP code generator currently special-cases descriptor.proto. It
// doesn't add it as a dependency even if the proto file actually does
// depend on it.
if (depends_on_descriptor(file)) {
google_protobuf_FileDescriptorProto_getmsgdef(pool->symtab);
}

file_def = upb_symtab_addfile(pool->symtab, file, &status);
CheckUpbStatus(&status, "Unable to load descriptor");
add_name_mappings(file_def);
}

/*
* add_name_mappings()
* add_descriptor()
*
* Adds the given descriptor data to this DescriptorPool.
*/
static void add_descriptor(DescriptorPool *pool, const char *data,
int data_len, upb_arena *arena) {
static void add_descriptor_set(DescriptorPool *pool, const char *data,
int data_len, upb_arena *arena) {
size_t i, n;
google_protobuf_FileDescriptorSet *set;
const google_protobuf_FileDescriptorProto* const* files;
Expand All @@ -928,27 +954,28 @@ static void add_descriptor(DescriptorPool *pool, const char *data,

for (i = 0; i < n; i++) {
const google_protobuf_FileDescriptorProto* file = files[i];
upb_strview name = google_protobuf_FileDescriptorProto_name(file);
upb_status status;
const upb_filedef *file_def;
upb_status_clear(&status);

if (upb_symtab_lookupfile2(pool->symtab, name.data, name.size)) {
// Already added.
continue;
}
add_descriptor(pool, file);
}
}

// The PHP code generator currently special-cases descriptor.proto. It
// doesn't add it as a dependency even if the proto file actually does
// depend on it.
if (depends_on_descriptor(file)) {
google_protobuf_FileDescriptorProto_getmsgdef(pool->symtab);
}
bool DescriptorPool_HasFile(const char *filename) {
DescriptorPool *intern = GetPool(get_generated_pool());
return upb_symtab_lookupfile(intern->symtab, filename) != NULL;
}

void DescriptorPool_AddDescriptor(const char *filename, const char *data,
int size) {
upb_arena *arena = upb_arena_new();
const google_protobuf_FileDescriptorProto *file =
google_protobuf_FileDescriptorProto_parse(data, size, arena);

file_def = upb_symtab_addfile(pool->symtab, file, &status);
CheckUpbStatus(&status, "Unable to load descriptor");
add_name_mappings(file_def);
if (!file) {
zend_error(E_ERROR, "Failed to parse binary descriptor for %s\n", filename);
return;
}

add_descriptor(GetPool(get_generated_pool()), file);
upb_arena_free(arena);
}

/*
Expand All @@ -969,7 +996,7 @@ PHP_METHOD(DescriptorPool, internalAddGeneratedFile) {
}

arena = upb_arena_new();
add_descriptor(intern, data, data_len, arena);
add_descriptor_set(intern, data, data_len, arena);
upb_arena_free(arena);
}

Expand All @@ -983,6 +1010,35 @@ static zend_function_entry DescriptorPool_methods[] = {
ZEND_FE_END
};

// -----------------------------------------------------------------------------
// InternalDescriptorPool
// -----------------------------------------------------------------------------

// For the C extension, Google\Protobuf\Internal\DescriptorPool is not a
// separate instantiable object, it just returns a
// Google\Protobuf\DescriptorPool.

zend_class_entry *InternalDescriptorPool_class_entry;

/*
* InternalDescriptorPool::getGeneratedPool()
*
* Returns the generated DescriptorPool. Note that this is identical to
* DescriptorPool::getGeneratedPool(), and in fact returns a DescriptorPool
* instance.
*/
PHP_METHOD(InternalDescriptorPool, getGeneratedPool) {
zval ret;
ZVAL_COPY(&ret, get_generated_pool());
RETURN_ZVAL(&ret, 0, 1);
}

static zend_function_entry InternalDescriptorPool_methods[] = {
PHP_ME(InternalDescriptorPool, getGeneratedPool, NULL,
ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
ZEND_FE_END
};

// -----------------------------------------------------------------------------
// GPBType
// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -1044,7 +1100,7 @@ void Def_ModuleInit() {
h = &FieldDescriptor_object_handlers;
memcpy(h, &std_object_handlers, sizeof(zend_object_handlers));

INIT_CLASS_ENTRY(tmp_ce, "Google\\Protobuf\\Internal\\DescriptorPool",
INIT_CLASS_ENTRY(tmp_ce, "Google\\Protobuf\\DescriptorPool",
DescriptorPool_methods);
DescriptorPool_class_entry = zend_register_internal_class(&tmp_ce);
DescriptorPool_class_entry->ce_flags |= ZEND_ACC_FINAL;
Expand All @@ -1053,6 +1109,10 @@ void Def_ModuleInit() {
memcpy(h, &std_object_handlers, sizeof(zend_object_handlers));
h->dtor_obj = DescriptorPool_destructor;

INIT_CLASS_ENTRY(tmp_ce, "Google\\Protobuf\\Internal\\DescriptorPool",
InternalDescriptorPool_methods);
InternalDescriptorPool_class_entry = zend_register_internal_class(&tmp_ce);

// GPBType.
#define STR(str) (str), strlen(str)
zend_class_entry class_type;
Expand Down
6 changes: 6 additions & 0 deletions php/ext/google/protobuf/def.h
Expand Up @@ -49,6 +49,12 @@ upb_symtab *DescriptorPool_Steal(zval *zv);

upb_symtab *DescriptorPool_GetSymbolTable();

// Returns true if the global descriptor pool already has the given filename.
bool DescriptorPool_HasFile(const char *filename);

// Adds the given descriptor with the given filename to the global pool.
void DescriptorPool_AddDescriptor(const char *filename, const char *data, int size);

typedef struct Descriptor {
zend_object std;
const upb_msgdef *msgdef;
Expand Down
62 changes: 0 additions & 62 deletions php/ext/google/protobuf/make-preload.php

This file was deleted.

0 comments on commit c5f7b4b

Please sign in to comment.