Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PHP] Native C well-known types #7944

Merged
merged 14 commits into from Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.