Formatted the Protobuf PHP extension with clang-format, with clang-format comments added as necessary

This is a no-op change.  Since Copybara will automatically format PRs with ClangFormat, this can lead to spurious changes that make CLs harder to review, as in cl/549058046.  To avoid this, we format everything with clang-format in a single no-op change.

This CL also forces all pointer alignment to left pointers, for consistency (clang-format would normally deduce based on existing style in each file).

Also added `// clang-format off` comments in some places where PHP conventions lead to badly formatted code.

PiperOrigin-RevId: 558260317
pull/13560/head
Joshua Haberman 1 year ago committed by Copybara-Service
parent 9741b5ffc5
commit bfdd31fbbc
  1. 18
      php/ext/google/protobuf/arena.c
  2. 4
      php/ext/google/protobuf/arena.h
  3. 118
      php/ext/google/protobuf/array.c
  4. 10
      php/ext/google/protobuf/array.h
  5. 114
      php/ext/google/protobuf/convert.c
  6. 16
      php/ext/google/protobuf/convert.h
  7. 288
      php/ext/google/protobuf/def.c
  8. 23
      php/ext/google/protobuf/def.h
  9. 112
      php/ext/google/protobuf/map.c
  10. 10
      php/ext/google/protobuf/map.h
  11. 267
      php/ext/google/protobuf/message.c
  12. 21
      php/ext/google/protobuf/message.h
  13. 90
      php/ext/google/protobuf/names.c
  14. 3
      php/ext/google/protobuf/names.h
  15. 12
      php/ext/google/protobuf/php_protobuf.h
  16. 99
      php/ext/google/protobuf/protobuf.c
  17. 28
      php/ext/google/protobuf/protobuf.h

@ -41,13 +41,13 @@ typedef struct Arena {
upb_Arena* arena; upb_Arena* arena;
} Arena; } Arena;
zend_class_entry *Arena_class_entry; zend_class_entry* Arena_class_entry;
static zend_object_handlers Arena_object_handlers; static zend_object_handlers Arena_object_handlers;
// PHP Object Handlers ///////////////////////////////////////////////////////// // PHP Object Handlers /////////////////////////////////////////////////////////
static zend_object* Arena_Create(zend_class_entry *class_type) { static zend_object* Arena_Create(zend_class_entry* class_type) {
Arena *intern = emalloc(sizeof(Arena)); Arena* intern = emalloc(sizeof(Arena));
zend_object_std_init(&intern->std, class_type); zend_object_std_init(&intern->std, class_type);
intern->std.handlers = &Arena_object_handlers; intern->std.handlers = &Arena_object_handlers;
intern->arena = upb_Arena_New(); intern->arena = upb_Arena_New();
@ -63,12 +63,10 @@ static void Arena_Free(zend_object* obj) {
// C Functions from arena.h //////////////////////////////////////////////////// // C Functions from arena.h ////////////////////////////////////////////////////
void Arena_Init(zval* val) { void Arena_Init(zval* val) { ZVAL_OBJ(val, Arena_Create(Arena_class_entry)); }
ZVAL_OBJ(val, Arena_Create(Arena_class_entry));
}
upb_Arena *Arena_Get(zval *val) { upb_Arena* Arena_Get(zval* val) {
Arena *a = (Arena*)Z_OBJ_P(val); Arena* a = (Arena*)Z_OBJ_P(val);
return a->arena; return a->arena;
} }
@ -77,9 +75,7 @@ upb_Arena *Arena_Get(zval *val) {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// No public methods. // No public methods.
static const zend_function_entry Arena_methods[] = { static const zend_function_entry Arena_methods[] = {ZEND_FE_END};
ZEND_FE_END
};
void Arena_ModuleInit() { void Arena_ModuleInit() {
zend_class_entry tmp_ce; zend_class_entry tmp_ce;

@ -39,9 +39,9 @@
void Arena_ModuleInit(); void Arena_ModuleInit();
// Creates and returns a new arena object that wraps a new upb_Arena*. // Creates and returns a new arena object that wraps a new upb_Arena*.
void Arena_Init(zval *val); void Arena_Init(zval* val);
// Gets the underlying upb_Arena from this arena object. // Gets the underlying upb_Arena from this arena object.
upb_Arena *Arena_Get(zval *arena); upb_Arena* Arena_Get(zval* arena);
#endif // PHP_PROTOBUF_ARENA_H_ #endif // PHP_PROTOBUF_ARENA_H_

@ -45,7 +45,7 @@
#include "php-upb.h" #include "php-upb.h"
#include "protobuf.h" #include "protobuf.h"
static void RepeatedFieldIter_make(zval *val, zval *repeated_field); static void RepeatedFieldIter_make(zval* val, zval* repeated_field);
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// RepeatedField // RepeatedField
@ -54,11 +54,11 @@ static void RepeatedFieldIter_make(zval *val, zval *repeated_field);
typedef struct { typedef struct {
zend_object std; zend_object std;
zval arena; zval arena;
upb_Array *array; upb_Array* array;
TypeInfo type; TypeInfo type;
} RepeatedField; } RepeatedField;
zend_class_entry *RepeatedField_class_entry; zend_class_entry* RepeatedField_class_entry;
static zend_object_handlers RepeatedField_object_handlers; static zend_object_handlers RepeatedField_object_handlers;
// PHP Object Handlers ///////////////////////////////////////////////////////// // PHP Object Handlers /////////////////////////////////////////////////////////
@ -69,8 +69,8 @@ static zend_object_handlers RepeatedField_object_handlers;
* PHP class entry function to allocate and initialize a new RepeatedField * PHP class entry function to allocate and initialize a new RepeatedField
* object. * object.
*/ */
static zend_object* RepeatedField_create(zend_class_entry *class_type) { static zend_object* RepeatedField_create(zend_class_entry* class_type) {
RepeatedField *intern = emalloc(sizeof(RepeatedField)); RepeatedField* intern = emalloc(sizeof(RepeatedField));
zend_object_std_init(&intern->std, class_type); zend_object_std_init(&intern->std, class_type);
intern->std.handlers = &RepeatedField_object_handlers; intern->std.handlers = &RepeatedField_object_handlers;
Arena_Init(&intern->arena); Arena_Init(&intern->arena);
@ -101,7 +101,7 @@ static void RepeatedField_destructor(zend_object* obj) {
* *
* $rf1 == $rf2 * $rf1 == $rf2
*/ */
static int RepeatedField_compare_objects(zval *rf1, zval *rf2) { static int RepeatedField_compare_objects(zval* rf1, zval* rf2) {
RepeatedField* intern1 = (RepeatedField*)Z_OBJ_P(rf1); RepeatedField* intern1 = (RepeatedField*)Z_OBJ_P(rf1);
RepeatedField* intern2 = (RepeatedField*)Z_OBJ_P(rf2); RepeatedField* intern2 = (RepeatedField*)Z_OBJ_P(rf2);
@ -118,10 +118,10 @@ static int RepeatedField_compare_objects(zval *rf1, zval *rf2) {
* *
* $rf2 = clone $rf1; * $rf2 = clone $rf1;
*/ */
static zend_object *RepeatedField_clone_obj(PROTO_VAL *object) { static zend_object* RepeatedField_clone_obj(PROTO_VAL* object) {
RepeatedField* intern = PROTO_VAL_P(object); RepeatedField* intern = PROTO_VAL_P(object);
upb_Arena *arena = Arena_Get(&intern->arena); upb_Arena* arena = Arena_Get(&intern->arena);
upb_Array *clone = upb_Array_New(arena, intern->type.type); upb_Array* clone = upb_Array_New(arena, intern->type.type);
size_t n = upb_Array_Size(intern->array); size_t n = upb_Array_Size(intern->array);
size_t i; size_t i;
@ -135,13 +135,13 @@ static zend_object *RepeatedField_clone_obj(PROTO_VAL *object) {
return Z_OBJ_P(&ret); return Z_OBJ_P(&ret);
} }
static HashTable *RepeatedField_GetProperties(PROTO_VAL *object) { static HashTable* RepeatedField_GetProperties(PROTO_VAL* object) {
return NULL; // We do not have a properties table. return NULL; // We do not have a properties table.
} }
static zval *RepeatedField_GetPropertyPtrPtr(PROTO_VAL *object, static zval* RepeatedField_GetPropertyPtrPtr(PROTO_VAL* object,
PROTO_STR *member, PROTO_STR* member, int type,
int type, void **cache_slot) { void** cache_slot) {
return NULL; // We don't offer direct references to our properties. return NULL; // We don't offer direct references to our properties.
} }
@ -149,15 +149,15 @@ static zval *RepeatedField_GetPropertyPtrPtr(PROTO_VAL *object,
// These are documented in the header file. // These are documented in the header file.
void RepeatedField_GetPhpWrapper(zval *val, upb_Array *arr, TypeInfo type, void RepeatedField_GetPhpWrapper(zval* val, upb_Array* arr, TypeInfo type,
zval *arena) { zval* arena) {
if (!arr) { if (!arr) {
ZVAL_NULL(val); ZVAL_NULL(val);
return; return;
} }
if (!ObjCache_Get(arr, val)) { if (!ObjCache_Get(arr, val)) {
RepeatedField *intern = emalloc(sizeof(RepeatedField)); RepeatedField* intern = emalloc(sizeof(RepeatedField));
zend_object_std_init(&intern->std, RepeatedField_class_entry); zend_object_std_init(&intern->std, RepeatedField_class_entry);
intern->std.handlers = &RepeatedField_object_handlers; intern->std.handlers = &RepeatedField_object_handlers;
ZVAL_COPY(&intern->arena, arena); ZVAL_COPY(&intern->arena, arena);
@ -169,22 +169,22 @@ void RepeatedField_GetPhpWrapper(zval *val, upb_Array *arr, TypeInfo type,
} }
} }
upb_Array *RepeatedField_GetUpbArray(zval *val, TypeInfo type, upb_Array* RepeatedField_GetUpbArray(zval* val, TypeInfo type,
upb_Arena *arena) { upb_Arena* arena) {
if (Z_ISREF_P(val)) { if (Z_ISREF_P(val)) {
ZVAL_DEREF(val); ZVAL_DEREF(val);
} }
if (Z_TYPE_P(val) == IS_ARRAY) { if (Z_TYPE_P(val) == IS_ARRAY) {
// Auto-construct, eg. [1, 2, 3] -> upb_Array([1, 2, 3]). // Auto-construct, eg. [1, 2, 3] -> upb_Array([1, 2, 3]).
upb_Array *arr = upb_Array_New(arena, type.type); upb_Array* arr = upb_Array_New(arena, type.type);
HashTable *table = HASH_OF(val); HashTable* table = HASH_OF(val);
HashPosition pos; HashPosition pos;
zend_hash_internal_pointer_reset_ex(table, &pos); zend_hash_internal_pointer_reset_ex(table, &pos);
while (true) { while (true) {
zval *zv = zend_hash_get_current_data_ex(table, &pos); zval* zv = zend_hash_get_current_data_ex(table, &pos);
upb_MessageValue val; upb_MessageValue val;
if (!zv) return arr; if (!zv) return arr;
@ -199,7 +199,7 @@ upb_Array *RepeatedField_GetUpbArray(zval *val, TypeInfo type,
} else if (Z_TYPE_P(val) == IS_OBJECT && } else if (Z_TYPE_P(val) == IS_OBJECT &&
Z_OBJCE_P(val) == RepeatedField_class_entry) { Z_OBJCE_P(val) == RepeatedField_class_entry) {
// Unwrap existing RepeatedField object to get the upb_Array* inside. // Unwrap existing RepeatedField object to get the upb_Array* inside.
RepeatedField *intern = (RepeatedField*)Z_OBJ_P(val); RepeatedField* intern = (RepeatedField*)Z_OBJ_P(val);
if (!TypeInfo_Eq(intern->type, type)) { if (!TypeInfo_Eq(intern->type, type)) {
php_error_docref(NULL, E_USER_ERROR, php_error_docref(NULL, E_USER_ERROR,
@ -214,7 +214,7 @@ upb_Array *RepeatedField_GetUpbArray(zval *val, TypeInfo type,
} }
} }
bool ArrayEq(const upb_Array *a1, const upb_Array *a2, TypeInfo type) { bool ArrayEq(const upb_Array* a1, const upb_Array* a2, TypeInfo type) {
size_t i; size_t i;
size_t n; size_t n;
@ -233,7 +233,6 @@ bool ArrayEq(const upb_Array *a1, const upb_Array *a2, TypeInfo type) {
return true; return true;
} }
// RepeatedField PHP methods /////////////////////////////////////////////////// // RepeatedField PHP methods ///////////////////////////////////////////////////
/** /**
@ -244,8 +243,8 @@ bool ArrayEq(const upb_Array *a1, const upb_Array *a2, TypeInfo type) {
* @param string Message/Enum class. * @param string Message/Enum class.
*/ */
PHP_METHOD(RepeatedField, __construct) { PHP_METHOD(RepeatedField, __construct) {
RepeatedField *intern = (RepeatedField*)Z_OBJ_P(getThis()); RepeatedField* intern = (RepeatedField*)Z_OBJ_P(getThis());
upb_Arena *arena = Arena_Get(&intern->arena); upb_Arena* arena = Arena_Get(&intern->arena);
zend_long type; zend_long type;
zend_class_entry* klass = NULL; zend_class_entry* klass = NULL;
@ -273,9 +272,9 @@ PHP_METHOD(RepeatedField, __construct) {
* @param object The element to be added. * @param object The element to be added.
*/ */
PHP_METHOD(RepeatedField, append) { PHP_METHOD(RepeatedField, append) {
RepeatedField *intern = (RepeatedField*)Z_OBJ_P(getThis()); RepeatedField* intern = (RepeatedField*)Z_OBJ_P(getThis());
upb_Arena *arena = Arena_Get(&intern->arena); upb_Arena* arena = Arena_Get(&intern->arena);
zval *php_val; zval* php_val;
upb_MessageValue msgval; upb_MessageValue msgval;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &php_val) != SUCCESS || if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &php_val) != SUCCESS ||
@ -298,7 +297,7 @@ PHP_METHOD(RepeatedField, append) {
* @return bool True if the element at the given index exists. * @return bool True if the element at the given index exists.
*/ */
PHP_METHOD(RepeatedField, offsetExists) { PHP_METHOD(RepeatedField, offsetExists) {
RepeatedField *intern = (RepeatedField*)Z_OBJ_P(getThis()); RepeatedField* intern = (RepeatedField*)Z_OBJ_P(getThis());
zend_long index; zend_long index;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &index) == FAILURE) { if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &index) == FAILURE) {
@ -321,7 +320,7 @@ PHP_METHOD(RepeatedField, offsetExists) {
* @exception Non-existing index. * @exception Non-existing index.
*/ */
PHP_METHOD(RepeatedField, offsetGet) { PHP_METHOD(RepeatedField, offsetGet) {
RepeatedField *intern = (RepeatedField*)Z_OBJ_P(getThis()); RepeatedField* intern = (RepeatedField*)Z_OBJ_P(getThis());
zend_long index; zend_long index;
upb_MessageValue msgval; upb_MessageValue msgval;
zval ret; zval ret;
@ -355,8 +354,8 @@ PHP_METHOD(RepeatedField, offsetGet) {
* @exception Incorrect type of the element. * @exception Incorrect type of the element.
*/ */
PHP_METHOD(RepeatedField, offsetSet) { PHP_METHOD(RepeatedField, offsetSet) {
RepeatedField *intern = (RepeatedField*)Z_OBJ_P(getThis()); RepeatedField* intern = (RepeatedField*)Z_OBJ_P(getThis());
upb_Arena *arena = Arena_Get(&intern->arena); upb_Arena* arena = Arena_Get(&intern->arena);
size_t size = upb_Array_Size(intern->array); size_t size = upb_Array_Size(intern->array);
zval *offset, *val; zval *offset, *val;
int64_t index; int64_t index;
@ -397,7 +396,7 @@ PHP_METHOD(RepeatedField, offsetSet) {
* @exception The element to be removed is not at the end of the RepeatedField. * @exception The element to be removed is not at the end of the RepeatedField.
*/ */
PHP_METHOD(RepeatedField, offsetUnset) { PHP_METHOD(RepeatedField, offsetUnset) {
RepeatedField *intern = (RepeatedField*)Z_OBJ_P(getThis()); RepeatedField* intern = (RepeatedField*)Z_OBJ_P(getThis());
zend_long index; zend_long index;
zend_long size = upb_Array_Size(intern->array); zend_long size = upb_Array_Size(intern->array);
@ -426,7 +425,7 @@ PHP_METHOD(RepeatedField, offsetUnset) {
* @return long The number of stored elements. * @return long The number of stored elements.
*/ */
PHP_METHOD(RepeatedField, count) { PHP_METHOD(RepeatedField, count) {
RepeatedField *intern = (RepeatedField*)Z_OBJ_P(getThis()); RepeatedField* intern = (RepeatedField*)Z_OBJ_P(getThis());
if (zend_parse_parameters_none() == FAILURE) { if (zend_parse_parameters_none() == FAILURE) {
return; return;
@ -450,6 +449,7 @@ PHP_METHOD(RepeatedField, getIterator) {
RETURN_COPY_VALUE(&ret); RETURN_COPY_VALUE(&ret);
} }
// clang-format off
ZEND_BEGIN_ARG_INFO_EX(arginfo_construct, 0, 0, 1) ZEND_BEGIN_ARG_INFO_EX(arginfo_construct, 0, 0, 1)
ZEND_ARG_INFO(0, type) ZEND_ARG_INFO(0, type)
ZEND_ARG_INFO(0, class) ZEND_ARG_INFO(0, class)
@ -493,6 +493,7 @@ static zend_function_entry repeated_field_methods[] = {
PHP_ME(RepeatedField, getIterator, arginfo_getIterator, ZEND_ACC_PUBLIC) PHP_ME(RepeatedField, getIterator, arginfo_getIterator, ZEND_ACC_PUBLIC)
ZEND_FE_END ZEND_FE_END
}; };
// clang-format on
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// PHP RepeatedFieldIter // PHP RepeatedFieldIter
@ -504,7 +505,7 @@ typedef struct {
zend_long position; zend_long position;
} RepeatedFieldIter; } RepeatedFieldIter;
zend_class_entry *RepeatedFieldIter_class_entry; zend_class_entry* RepeatedFieldIter_class_entry;
static zend_object_handlers repeated_field_iter_object_handlers; static zend_object_handlers repeated_field_iter_object_handlers;
/** /**
@ -513,8 +514,8 @@ static zend_object_handlers repeated_field_iter_object_handlers;
* PHP class entry function to allocate and initialize a new RepeatedFieldIter * PHP class entry function to allocate and initialize a new RepeatedFieldIter
* object. * object.
*/ */
zend_object* RepeatedFieldIter_create(zend_class_entry *class_type) { zend_object* RepeatedFieldIter_create(zend_class_entry* class_type) {
RepeatedFieldIter *intern = emalloc(sizeof(RepeatedFieldIter)); RepeatedFieldIter* intern = emalloc(sizeof(RepeatedFieldIter));
zend_object_std_init(&intern->std, class_type); zend_object_std_init(&intern->std, class_type);
intern->std.handlers = &repeated_field_iter_object_handlers; intern->std.handlers = &repeated_field_iter_object_handlers;
ZVAL_NULL(&intern->repeated_field); ZVAL_NULL(&intern->repeated_field);
@ -541,8 +542,8 @@ static void RepeatedFieldIter_dtor(zend_object* obj) {
* *
* C function to create a RepeatedFieldIter. * C function to create a RepeatedFieldIter.
*/ */
static void RepeatedFieldIter_make(zval *val, zval *repeated_field) { static void RepeatedFieldIter_make(zval* val, zval* repeated_field) {
RepeatedFieldIter *iter; RepeatedFieldIter* iter;
ZVAL_OBJ(val, RepeatedFieldIter_class_entry->create_object( ZVAL_OBJ(val, RepeatedFieldIter_class_entry->create_object(
RepeatedFieldIter_class_entry)); RepeatedFieldIter_class_entry));
iter = (RepeatedFieldIter*)Z_OBJ_P(val); iter = (RepeatedFieldIter*)Z_OBJ_P(val);
@ -569,7 +570,7 @@ static void RepeatedFieldIter_make(zval *val, zval *repeated_field) {
* Implements the Iterator interface. Sets the iterator to the first element. * Implements the Iterator interface. Sets the iterator to the first element.
*/ */
PHP_METHOD(RepeatedFieldIter, rewind) { PHP_METHOD(RepeatedFieldIter, rewind) {
RepeatedFieldIter *intern = (RepeatedFieldIter*)Z_OBJ_P(getThis()); RepeatedFieldIter* intern = (RepeatedFieldIter*)Z_OBJ_P(getThis());
intern->position = 0; intern->position = 0;
} }
@ -579,9 +580,9 @@ PHP_METHOD(RepeatedFieldIter, rewind) {
* Implements the Iterator interface. Returns the current value. * Implements the Iterator interface. Returns the current value.
*/ */
PHP_METHOD(RepeatedFieldIter, current) { PHP_METHOD(RepeatedFieldIter, current) {
RepeatedFieldIter *intern = (RepeatedFieldIter*)Z_OBJ_P(getThis()); RepeatedFieldIter* intern = (RepeatedFieldIter*)Z_OBJ_P(getThis());
RepeatedField *field = (RepeatedField*)Z_OBJ_P(&intern->repeated_field); RepeatedField* field = (RepeatedField*)Z_OBJ_P(&intern->repeated_field);
upb_Array *array = field->array; upb_Array* array = field->array;
zend_long index = intern->position; zend_long index = intern->position;
upb_MessageValue msgval; upb_MessageValue msgval;
zval ret; zval ret;
@ -602,7 +603,7 @@ PHP_METHOD(RepeatedFieldIter, current) {
* Implements the Iterator interface. Returns the current key. * Implements the Iterator interface. Returns the current key.
*/ */
PHP_METHOD(RepeatedFieldIter, key) { PHP_METHOD(RepeatedFieldIter, key) {
RepeatedFieldIter *intern = (RepeatedFieldIter*)Z_OBJ_P(getThis()); RepeatedFieldIter* intern = (RepeatedFieldIter*)Z_OBJ_P(getThis());
RETURN_LONG(intern->position); RETURN_LONG(intern->position);
} }
@ -612,7 +613,7 @@ PHP_METHOD(RepeatedFieldIter, key) {
* Implements the Iterator interface. Advances to the next element. * Implements the Iterator interface. Advances to the next element.
*/ */
PHP_METHOD(RepeatedFieldIter, next) { PHP_METHOD(RepeatedFieldIter, next) {
RepeatedFieldIter *intern = (RepeatedFieldIter*)Z_OBJ_P(getThis()); RepeatedFieldIter* intern = (RepeatedFieldIter*)Z_OBJ_P(getThis());
++intern->position; ++intern->position;
} }
@ -622,15 +623,17 @@ PHP_METHOD(RepeatedFieldIter, next) {
* Implements the Iterator interface. Returns true if this is a valid element. * Implements the Iterator interface. Returns true if this is a valid element.
*/ */
PHP_METHOD(RepeatedFieldIter, valid) { PHP_METHOD(RepeatedFieldIter, valid) {
RepeatedFieldIter *intern = (RepeatedFieldIter*)Z_OBJ_P(getThis()); RepeatedFieldIter* intern = (RepeatedFieldIter*)Z_OBJ_P(getThis());
RepeatedField *field = (RepeatedField*)Z_OBJ_P(&intern->repeated_field); RepeatedField* field = (RepeatedField*)Z_OBJ_P(&intern->repeated_field);
RETURN_BOOL(intern->position < upb_Array_Size(field->array)); RETURN_BOOL(intern->position < upb_Array_Size(field->array));
} }
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_current, 0, 0, IS_MIXED, 0) ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_current, 0, 0,
IS_MIXED, 0)
ZEND_END_ARG_INFO() ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_key, 0, 0, IS_MIXED, 0) ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_key, 0, 0, IS_MIXED,
0)
ZEND_END_ARG_INFO() ZEND_END_ARG_INFO()
PROTOBUF_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_next, 0, 0, IS_VOID, 0) PROTOBUF_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_next, 0, 0, IS_VOID, 0)
@ -643,13 +646,12 @@ PROTOBUF_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_rewind, 0, 0, IS_VOID, 0)
ZEND_END_ARG_INFO() ZEND_END_ARG_INFO()
static zend_function_entry repeated_field_iter_methods[] = { static zend_function_entry repeated_field_iter_methods[] = {
PHP_ME(RepeatedFieldIter, rewind, arginfo_rewind, ZEND_ACC_PUBLIC) PHP_ME(RepeatedFieldIter, rewind, arginfo_rewind, ZEND_ACC_PUBLIC)
PHP_ME(RepeatedFieldIter, current, arginfo_current, ZEND_ACC_PUBLIC) PHP_ME(RepeatedFieldIter, current, arginfo_current, ZEND_ACC_PUBLIC)
PHP_ME(RepeatedFieldIter, key, arginfo_key, ZEND_ACC_PUBLIC) PHP_ME(RepeatedFieldIter, key, arginfo_key, ZEND_ACC_PUBLIC) PHP_ME(
PHP_ME(RepeatedFieldIter, next, arginfo_next, ZEND_ACC_PUBLIC) RepeatedFieldIter, next, arginfo_next, ZEND_ACC_PUBLIC)
PHP_ME(RepeatedFieldIter, valid, arginfo_valid, ZEND_ACC_PUBLIC) PHP_ME(RepeatedFieldIter, valid, arginfo_valid, ZEND_ACC_PUBLIC)
ZEND_FE_END ZEND_FE_END};
};
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Module init. // Module init.
@ -662,7 +664,7 @@ static zend_function_entry repeated_field_iter_methods[] = {
*/ */
void Array_ModuleInit() { void Array_ModuleInit() {
zend_class_entry tmp_ce; zend_class_entry tmp_ce;
zend_object_handlers *h; zend_object_handlers* h;
// RepeatedField. // RepeatedField.
INIT_CLASS_ENTRY(tmp_ce, "Google\\Protobuf\\Internal\\RepeatedField", INIT_CLASS_ENTRY(tmp_ce, "Google\\Protobuf\\Internal\\RepeatedField",

@ -49,19 +49,19 @@ void Array_ModuleInit();
// |arena| and add all of the PHP elements to it. // |arena| and add all of the PHP elements to it.
// //
// If an error occurs, we raise a PHP error and return NULL. // If an error occurs, we raise a PHP error and return NULL.
upb_Array *RepeatedField_GetUpbArray(zval *val, TypeInfo type, upb_Array* RepeatedField_GetUpbArray(zval* val, TypeInfo type,
upb_Arena *arena); upb_Arena* arena);
// Creates a PHP RepeatedField object for the given upb_Array* and |type| and // Creates a PHP RepeatedField object for the given upb_Array* and |type| and
// returns it in |val|. The PHP object will keep a reference to this |arena| to // returns it in |val|. The PHP object will keep a reference to this |arena| to
// ensure the underlying array data stays alive. // ensure the underlying array data stays alive.
// //
// If |arr| is NULL, this will return a PHP null object. // If |arr| is NULL, this will return a PHP null object.
void RepeatedField_GetPhpWrapper(zval *val, upb_Array *arr, TypeInfo type, void RepeatedField_GetPhpWrapper(zval* val, upb_Array* arr, TypeInfo type,
zval *arena); zval* arena);
// Returns true if the given arrays are equal. Both arrays must be of this // Returns true if the given arrays are equal. Both arrays must be of this
// |type| and, if the type is |kUpb_CType_Message|, must have the same |m|. // |type| and, if the type is |kUpb_CType_Message|, must have the same |m|.
bool ArrayEq(const upb_Array *a1, const upb_Array *a2, TypeInfo type); bool ArrayEq(const upb_Array* a1, const upb_Array* a2, TypeInfo type);
#endif // PHP_PROTOBUF_ARRAY_H_ #endif // PHP_PROTOBUF_ARRAY_H_

@ -92,6 +92,7 @@ PHP_METHOD(Util, checkRepeatedField) {
RETURN_COPY(val); RETURN_COPY(val);
} }
// clang-format off
ZEND_BEGIN_ARG_INFO_EX(arginfo_checkPrimitive, 0, 0, 1) ZEND_BEGIN_ARG_INFO_EX(arginfo_checkPrimitive, 0, 0, 1)
ZEND_ARG_INFO(0, value) ZEND_ARG_INFO(0, value)
ZEND_END_ARG_INFO() ZEND_END_ARG_INFO()
@ -148,6 +149,7 @@ static zend_function_entry util_methods[] = {
ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
ZEND_FE_END ZEND_FE_END
}; };
// clang-format on
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Conversion functions used from C // Conversion functions used from C
@ -155,38 +157,37 @@ static zend_function_entry util_methods[] = {
upb_CType pbphp_dtype_to_type(upb_FieldType type) { upb_CType pbphp_dtype_to_type(upb_FieldType type) {
switch (type) { switch (type) {
#define CASE(descriptor_type, type) \ #define CASE(descriptor_type, type) \
case kUpb_FieldType_##descriptor_type: \ case kUpb_FieldType_##descriptor_type: \
return kUpb_CType_##type; return kUpb_CType_##type;
CASE(Float, Float); CASE(Float, Float);
CASE(Double, Double); CASE(Double, Double);
CASE(Bool, Bool); CASE(Bool, Bool);
CASE(String, String); CASE(String, String);
CASE(Bytes, Bytes); CASE(Bytes, Bytes);
CASE(Message, Message); CASE(Message, Message);
CASE(Group, Message); CASE(Group, Message);
CASE(Enum, Enum); CASE(Enum, Enum);
CASE(Int32, Int32); CASE(Int32, Int32);
CASE(Int64, Int64); CASE(Int64, Int64);
CASE(UInt32, Int32); CASE(UInt32, Int32);
CASE(UInt64, UInt64); CASE(UInt64, UInt64);
CASE(SInt32, Int32); CASE(SInt32, Int32);
CASE(SInt64, Int64); CASE(SInt64, Int64);
CASE(Fixed32, UInt32); CASE(Fixed32, UInt32);
CASE(Fixed64, UInt64); CASE(Fixed64, UInt64);
CASE(SFixed32, Int32); CASE(SFixed32, Int32);
CASE(SFixed64, Int64); CASE(SFixed64, Int64);
#undef CASE #undef CASE
} }
zend_error(E_ERROR, "Unknown field type."); zend_error(E_ERROR, "Unknown field type.");
return 0; return 0;
} }
static bool buftouint64(const char *ptr, const char *end, uint64_t *val) { static bool buftouint64(const char* ptr, const char* end, uint64_t* val) {
uint64_t u64 = 0; uint64_t u64 = 0;
while (ptr < end) { while (ptr < end) {
unsigned ch = (unsigned)(*ptr - '0'); unsigned ch = (unsigned)(*ptr - '0');
@ -204,7 +205,7 @@ static bool buftouint64(const char *ptr, const char *end, uint64_t *val) {
// But we don't allow 'e', eg. '1.1e2' or any other non-numeric chars. // But we don't allow 'e', eg. '1.1e2' or any other non-numeric chars.
if (*ptr++ != '.') return false; if (*ptr++ != '.') return false;
for (;ptr < end; ptr++) { for (; ptr < end; ptr++) {
if (*ptr < '0' || *ptr > '9') { if (*ptr < '0' || *ptr > '9') {
return false; return false;
} }
@ -215,7 +216,7 @@ static bool buftouint64(const char *ptr, const char *end, uint64_t *val) {
return true; return true;
} }
static bool buftoint64(const char *ptr, const char *end, int64_t *val) { static bool buftoint64(const char* ptr, const char* end, int64_t* val) {
bool neg = false; bool neg = false;
uint64_t u64; uint64_t u64;
@ -224,8 +225,7 @@ static bool buftoint64(const char *ptr, const char *end, int64_t *val) {
neg = true; neg = true;
} }
if (!buftouint64(ptr, end, &u64) || if (!buftouint64(ptr, end, &u64) || u64 > (uint64_t)INT64_MAX + neg) {
u64 > (uint64_t)INT64_MAX + neg) {
return false; return false;
} }
@ -233,7 +233,7 @@ static bool buftoint64(const char *ptr, const char *end, int64_t *val) {
return true; return true;
} }
static void throw_conversion_exception(const char *to, const zval *zv) { static void throw_conversion_exception(const char* to, const zval* zv) {
zval tmp; zval tmp;
ZVAL_COPY(&tmp, zv); ZVAL_COPY(&tmp, zv);
convert_to_string(&tmp); convert_to_string(&tmp);
@ -244,7 +244,7 @@ static void throw_conversion_exception(const char *to, const zval *zv) {
zval_ptr_dtor(&tmp); zval_ptr_dtor(&tmp);
} }
bool Convert_PhpToInt64(const zval *php_val, int64_t *i64) { bool Convert_PhpToInt64(const zval* php_val, int64_t* i64) {
switch (Z_TYPE_P(php_val)) { switch (Z_TYPE_P(php_val)) {
case IS_LONG: case IS_LONG:
*i64 = Z_LVAL_P(php_val); *i64 = Z_LVAL_P(php_val);
@ -259,7 +259,7 @@ bool Convert_PhpToInt64(const zval *php_val, int64_t *i64) {
return true; return true;
} }
case IS_STRING: { case IS_STRING: {
const char *buf = Z_STRVAL_P(php_val); const char* buf = Z_STRVAL_P(php_val);
// PHP would accept scientific notation here, but we're going to be a // PHP would accept scientific notation here, but we're going to be a
// little more discerning and only accept pure integers. // little more discerning and only accept pure integers.
bool ok = buftoint64(buf, buf + Z_STRLEN_P(php_val), i64); bool ok = buftoint64(buf, buf + Z_STRLEN_P(php_val), i64);
@ -274,7 +274,7 @@ bool Convert_PhpToInt64(const zval *php_val, int64_t *i64) {
} }
} }
static bool to_double(zval *php_val, double *dbl) { static bool to_double(zval* php_val, double* dbl) {
switch (Z_TYPE_P(php_val)) { switch (Z_TYPE_P(php_val)) {
case IS_LONG: case IS_LONG:
*dbl = Z_LVAL_P(php_val); *dbl = Z_LVAL_P(php_val);
@ -296,7 +296,7 @@ static bool to_double(zval *php_val, double *dbl) {
} }
} }
default: default:
fail: fail:
throw_conversion_exception("double", php_val); throw_conversion_exception("double", php_val);
return false; return false;
} }
@ -353,8 +353,8 @@ static bool to_string(zval* from) {
} }
} }
bool Convert_PhpToUpb(zval *php_val, upb_MessageValue *upb_val, TypeInfo type, bool Convert_PhpToUpb(zval* php_val, upb_MessageValue* upb_val, TypeInfo type,
upb_Arena *arena) { upb_Arena* arena) {
int64_t i64; int64_t i64;
if (Z_ISREF_P(php_val)) { if (Z_ISREF_P(php_val)) {
@ -393,7 +393,7 @@ bool Convert_PhpToUpb(zval *php_val, upb_MessageValue *upb_val, TypeInfo type,
return to_bool(php_val, &upb_val->bool_val); return to_bool(php_val, &upb_val->bool_val);
case kUpb_CType_String: case kUpb_CType_String:
case kUpb_CType_Bytes: { case kUpb_CType_Bytes: {
char *ptr; char* ptr;
size_t size; size_t size;
if (!to_string(php_val)) return false; if (!to_string(php_val)) return false;
@ -401,7 +401,8 @@ bool Convert_PhpToUpb(zval *php_val, upb_MessageValue *upb_val, TypeInfo type,
size = Z_STRLEN_P(php_val); size = Z_STRLEN_P(php_val);
// If arena is NULL we reference the input zval. // If arena is NULL we reference the input zval.
// The resulting upb_StringView will only be value while the zval is alive. // The resulting upb_StringView will only be value while the zval is
// alive.
if (arena) { if (arena) {
ptr = upb_Arena_Malloc(arena, size); ptr = upb_Arena_Malloc(arena, size);
memcpy(ptr, Z_STRVAL_P(php_val), size); memcpy(ptr, Z_STRVAL_P(php_val), size);
@ -415,35 +416,35 @@ bool Convert_PhpToUpb(zval *php_val, upb_MessageValue *upb_val, TypeInfo type,
case kUpb_CType_Message: case kUpb_CType_Message:
PBPHP_ASSERT(type.desc); PBPHP_ASSERT(type.desc);
return Message_GetUpbMessage(php_val, type.desc, arena, return Message_GetUpbMessage(php_val, type.desc, arena,
(upb_Message **)&upb_val->msg_val); (upb_Message**)&upb_val->msg_val);
} }
return false; return false;
} }
void Convert_UpbToPhp(upb_MessageValue upb_val, zval *php_val, TypeInfo type, void Convert_UpbToPhp(upb_MessageValue upb_val, zval* php_val, TypeInfo type,
zval *arena) { zval* arena) {
switch (type.type) { switch (type.type) {
case kUpb_CType_Int64: case kUpb_CType_Int64:
#if SIZEOF_ZEND_LONG == 8 #if SIZEOF_ZEND_LONG == 8
ZVAL_LONG(php_val, upb_val.int64_val); ZVAL_LONG(php_val, upb_val.int64_val);
#else #else
{ {
char buf[20]; char buf[20];
int size = sprintf(buf, "%lld", upb_val.int64_val); int size = sprintf(buf, "%lld", upb_val.int64_val);
ZVAL_NEW_STR(php_val, zend_string_init(buf, size, 0)); ZVAL_NEW_STR(php_val, zend_string_init(buf, size, 0));
} }
#endif #endif
break; break;
case kUpb_CType_UInt64: case kUpb_CType_UInt64:
#if SIZEOF_ZEND_LONG == 8 #if SIZEOF_ZEND_LONG == 8
ZVAL_LONG(php_val, upb_val.uint64_val); ZVAL_LONG(php_val, upb_val.uint64_val);
#else #else
{ {
char buf[20]; char buf[20];
int size = sprintf(buf, "%lld", (int64_t)upb_val.uint64_val); int size = sprintf(buf, "%lld", (int64_t)upb_val.uint64_val);
ZVAL_NEW_STR(php_val, zend_string_init(buf, size, 0)); ZVAL_NEW_STR(php_val, zend_string_init(buf, size, 0));
} }
#endif #endif
break; break;
case kUpb_CType_Int32: case kUpb_CType_Int32:
@ -473,7 +474,7 @@ void Convert_UpbToPhp(upb_MessageValue upb_val, zval *php_val, TypeInfo type,
} }
case kUpb_CType_Message: case kUpb_CType_Message:
PBPHP_ASSERT(type.desc); PBPHP_ASSERT(type.desc);
Message_GetPhpWrapper(php_val, type.desc, (upb_Message *)upb_val.msg_val, Message_GetPhpWrapper(php_val, type.desc, (upb_Message*)upb_val.msg_val,
arena); arena);
break; break;
} }
@ -498,18 +499,19 @@ static bool IsWrapper(const upb_MessageDef* m) {
} }
} }
bool Convert_PhpToUpbAutoWrap(zval *val, upb_MessageValue *upb_val, TypeInfo type, bool Convert_PhpToUpbAutoWrap(zval* val, upb_MessageValue* upb_val,
upb_Arena *arena) { TypeInfo type, upb_Arena* arena) {
const upb_MessageDef *subm = type.desc ? type.desc->msgdef : NULL; const upb_MessageDef* subm = type.desc ? type.desc->msgdef : NULL;
if (subm && IsWrapper(subm) && Z_TYPE_P(val) != IS_OBJECT) { if (subm && IsWrapper(subm) && Z_TYPE_P(val) != IS_OBJECT) {
// Assigning a scalar to a wrapper-typed value. We will automatically wrap // Assigning a scalar to a wrapper-typed value. We will automatically wrap
// the value, so the user doesn't need to create a FooWrapper(['value': X]) // the value, so the user doesn't need to create a FooWrapper(['value': X])
// message manually. // message manually.
upb_MiniTable *t = upb_MessageDef_MiniTable(subm); upb_MiniTable* t = upb_MessageDef_MiniTable(subm);
upb_Message *wrapper = upb_Message_New(t, arena); upb_Message* wrapper = upb_Message_New(t, arena);
const upb_FieldDef *val_f = upb_MessageDef_FindFieldByNumber(subm, 1); const upb_FieldDef* val_f = upb_MessageDef_FindFieldByNumber(subm, 1);
upb_MessageValue msgval; upb_MessageValue msgval;
if (!Convert_PhpToUpb(val, &msgval, TypeInfo_Get(val_f), arena)) return false; if (!Convert_PhpToUpb(val, &msgval, TypeInfo_Get(val_f), arena))
return false;
upb_Message_SetFieldByDef(wrapper, val_f, msgval, arena); upb_Message_SetFieldByDef(wrapper, val_f, msgval, arena);
upb_val->msg_val = wrapper; upb_val->msg_val = wrapper;
return true; return true;
@ -524,7 +526,7 @@ bool Convert_PhpToUpbAutoWrap(zval *val, upb_MessageValue *upb_val, TypeInfo typ
} }
void Convert_ModuleInit(void) { void Convert_ModuleInit(void) {
const char *prefix_name = "TYPE_URL_PREFIX"; const char* prefix_name = "TYPE_URL_PREFIX";
zend_class_entry class_type; zend_class_entry class_type;
INIT_CLASS_ENTRY(class_type, "Google\\Protobuf\\Internal\\GPBUtil", INIT_CLASS_ENTRY(class_type, "Google\\Protobuf\\Internal\\GPBUtil",

@ -33,21 +33,21 @@
#include <php.h> #include <php.h>
#include "php-upb.h"
#include "def.h" #include "def.h"
#include "php-upb.h"
upb_CType pbphp_dtype_to_type(upb_FieldType type); upb_CType pbphp_dtype_to_type(upb_FieldType type);
// Converts |php_val| to an int64_t. Returns false if the value cannot be // Converts |php_val| to an int64_t. Returns false if the value cannot be
// converted. // converted.
bool Convert_PhpToInt64(const zval *php_val, int64_t *i64); bool Convert_PhpToInt64(const zval* php_val, int64_t* i64);
// Converts |php_val| to a upb_MessageValue according to |type|. If type is // Converts |php_val| to a upb_MessageValue according to |type|. If type is
// kUpb_CType_Message, then |desc| must be the Descriptor for this message type. // kUpb_CType_Message, then |desc| must be the Descriptor for this message type.
// If type is string, message, or bytes, then |arena| will be used to copy // If type is string, message, or bytes, then |arena| will be used to copy
// string data or fuse this arena to the given message's arena. // string data or fuse this arena to the given message's arena.
bool Convert_PhpToUpb(zval *php_val, upb_MessageValue *upb_val, TypeInfo type, bool Convert_PhpToUpb(zval* php_val, upb_MessageValue* upb_val, TypeInfo type,
upb_Arena *arena); upb_Arena* arena);
// Similar to Convert_PhpToUpb, but supports automatically wrapping the wrapper // Similar to Convert_PhpToUpb, but supports automatically wrapping the wrapper
// types if a primitive is specified: // types if a primitive is specified:
@ -56,16 +56,16 @@ bool Convert_PhpToUpb(zval *php_val, upb_MessageValue *upb_val, TypeInfo type,
// //
// We currently allow this implicit conversion in initializers, but not for // We currently allow this implicit conversion in initializers, but not for
// assignment. // assignment.
bool Convert_PhpToUpbAutoWrap(zval *val, upb_MessageValue *upb_val, TypeInfo type, bool Convert_PhpToUpbAutoWrap(zval* val, upb_MessageValue* upb_val,
upb_Arena *arena); TypeInfo type, upb_Arena* arena);
// Converts |upb_val| to a PHP zval according to |type|. This may involve // Converts |upb_val| to a PHP zval according to |type|. This may involve
// creating a PHP wrapper object. Any newly created wrapper object // creating a PHP wrapper object. Any newly created wrapper object
// will reference |arena|. // will reference |arena|.
// //
// The caller owns a reference to the returned value. // The caller owns a reference to the returned value.
void Convert_UpbToPhp(upb_MessageValue upb_val, zval *php_val, TypeInfo type, void Convert_UpbToPhp(upb_MessageValue upb_val, zval* php_val, TypeInfo type,
zval *arena); zval* arena);
// Registers the GPBUtil class. // Registers the GPBUtil class.
void Convert_ModuleInit(void); void Convert_ModuleInit(void);

@ -45,16 +45,18 @@ static void CheckUpbStatus(const upb_Status* status, const char* msg) {
} }
} }
static void FieldDescriptor_FromFieldDef(zval *val, const upb_FieldDef *f); static void FieldDescriptor_FromFieldDef(zval* val, const upb_FieldDef* f);
// We use this for objects that should not be created directly from PHP. // We use this for objects that should not be created directly from PHP.
static zend_object *CreateHandler_ReturnNull(zend_class_entry *class_type) { static zend_object* CreateHandler_ReturnNull(zend_class_entry* class_type) {
return NULL; // Nobody should call this. return NULL; // Nobody should call this.
} }
// clang-format off
ZEND_BEGIN_ARG_INFO_EX(arginfo_getByIndex, 0, 0, 1) ZEND_BEGIN_ARG_INFO_EX(arginfo_getByIndex, 0, 0, 1)
ZEND_ARG_INFO(0, index) ZEND_ARG_INFO(0, index)
ZEND_END_ARG_INFO() ZEND_END_ARG_INFO()
// clang-format on
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// EnumValueDescriptor // EnumValueDescriptor
@ -62,11 +64,11 @@ ZEND_END_ARG_INFO()
typedef struct { typedef struct {
zend_object std; zend_object std;
const char *name; const char* name;
int32_t number; int32_t number;
} EnumValueDescriptor; } EnumValueDescriptor;
zend_class_entry *EnumValueDescriptor_class_entry; zend_class_entry* EnumValueDescriptor_class_entry;
static zend_object_handlers EnumValueDescriptor_object_handlers; static zend_object_handlers EnumValueDescriptor_object_handlers;
/* /*
@ -74,9 +76,9 @@ static zend_object_handlers EnumValueDescriptor_object_handlers;
* *
* Function to create an EnumValueDescriptor object from C. * Function to create an EnumValueDescriptor object from C.
*/ */
static void EnumValueDescriptor_Make(zval *val, const char *name, static void EnumValueDescriptor_Make(zval* val, const char* name,
int32_t number) { int32_t number) {
EnumValueDescriptor *intern = emalloc(sizeof(EnumValueDescriptor)); EnumValueDescriptor* intern = emalloc(sizeof(EnumValueDescriptor));
zend_object_std_init(&intern->std, EnumValueDescriptor_class_entry); zend_object_std_init(&intern->std, EnumValueDescriptor_class_entry);
intern->std.handlers = &EnumValueDescriptor_object_handlers; intern->std.handlers = &EnumValueDescriptor_object_handlers;
intern->name = name; intern->name = name;
@ -91,7 +93,7 @@ static void EnumValueDescriptor_Make(zval *val, const char *name,
* Returns the name for this enum value. * Returns the name for this enum value.
*/ */
PHP_METHOD(EnumValueDescriptor, getName) { PHP_METHOD(EnumValueDescriptor, getName) {
EnumValueDescriptor *intern = (EnumValueDescriptor*)Z_OBJ_P(getThis()); EnumValueDescriptor* intern = (EnumValueDescriptor*)Z_OBJ_P(getThis());
RETURN_STRING(intern->name); RETURN_STRING(intern->name);
} }
@ -101,15 +103,17 @@ PHP_METHOD(EnumValueDescriptor, getName) {
* Returns the number for this enum value. * Returns the number for this enum value.
*/ */
PHP_METHOD(EnumValueDescriptor, getNumber) { PHP_METHOD(EnumValueDescriptor, getNumber) {
EnumValueDescriptor *intern = (EnumValueDescriptor*)Z_OBJ_P(getThis()); EnumValueDescriptor* intern = (EnumValueDescriptor*)Z_OBJ_P(getThis());
RETURN_LONG(intern->number); RETURN_LONG(intern->number);
} }
// clang-format off
static zend_function_entry EnumValueDescriptor_methods[] = { static zend_function_entry EnumValueDescriptor_methods[] = {
PHP_ME(EnumValueDescriptor, getName, arginfo_void, ZEND_ACC_PUBLIC) PHP_ME(EnumValueDescriptor, getName, arginfo_void, ZEND_ACC_PUBLIC)
PHP_ME(EnumValueDescriptor, getNumber, arginfo_void, ZEND_ACC_PUBLIC) PHP_ME(EnumValueDescriptor, getNumber, arginfo_void, ZEND_ACC_PUBLIC)
ZEND_FE_END ZEND_FE_END
}; };
// clang-format on
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// EnumDescriptor // EnumDescriptor
@ -117,20 +121,20 @@ static zend_function_entry EnumValueDescriptor_methods[] = {
typedef struct { typedef struct {
zend_object std; zend_object std;
const upb_EnumDef *enumdef; const upb_EnumDef* enumdef;
void *cache_key; void* cache_key;
} EnumDescriptor; } EnumDescriptor;
zend_class_entry *EnumDescriptor_class_entry; zend_class_entry* EnumDescriptor_class_entry;
static zend_object_handlers EnumDescriptor_object_handlers; static zend_object_handlers EnumDescriptor_object_handlers;
static void EnumDescriptor_destructor(zend_object* obj) { static void EnumDescriptor_destructor(zend_object* obj) {
EnumDescriptor *intern = (EnumDescriptor*)obj; EnumDescriptor* intern = (EnumDescriptor*)obj;
ObjCache_Delete(intern->cache_key); ObjCache_Delete(intern->cache_key);
} }
// Caller owns a ref on the returned zval. // Caller owns a ref on the returned zval.
static void EnumDescriptor_FromClassEntry(zval *val, zend_class_entry *ce) { static void EnumDescriptor_FromClassEntry(zval* val, zend_class_entry* ce) {
// To differentiate enums from classes, we pointer-tag the class entry. // To differentiate enums from classes, we pointer-tag the class entry.
void* key = (void*)((uintptr_t)ce | 1); void* key = (void*)((uintptr_t)ce | 1);
PBPHP_ASSERT(key != ce); PBPHP_ASSERT(key != ce);
@ -141,7 +145,7 @@ static void EnumDescriptor_FromClassEntry(zval *val, zend_class_entry *ce) {
} }
if (!ObjCache_Get(key, val)) { if (!ObjCache_Get(key, val)) {
const upb_EnumDef *e = NameMap_GetEnum(ce); const upb_EnumDef* e = NameMap_GetEnum(ce);
if (!e) { if (!e) {
ZVAL_NULL(val); ZVAL_NULL(val);
return; return;
@ -157,16 +161,16 @@ static void EnumDescriptor_FromClassEntry(zval *val, zend_class_entry *ce) {
} }
// Caller owns a ref on the returned zval. // Caller owns a ref on the returned zval.
static void EnumDescriptor_FromEnumDef(zval *val, const upb_EnumDef *m) { static void EnumDescriptor_FromEnumDef(zval* val, const upb_EnumDef* m) {
if (!m) { if (!m) {
ZVAL_NULL(val); ZVAL_NULL(val);
} else { } else {
char *classname = char* classname =
GetPhpClassname(upb_EnumDef_File(m), upb_EnumDef_FullName(m), false); GetPhpClassname(upb_EnumDef_File(m), upb_EnumDef_FullName(m), false);
zend_string *str = zend_string_init(classname, strlen(classname), 0); zend_string* str = zend_string_init(classname, strlen(classname), 0);
zend_class_entry *ce = zend_lookup_class(str); // May autoload the class. zend_class_entry* ce = zend_lookup_class(str); // May autoload the class.
zend_string_release (str); zend_string_release(str);
if (!ce) { if (!ce) {
zend_error(E_ERROR, "Couldn't load generated class %s", classname); zend_error(E_ERROR, "Couldn't load generated class %s", classname);
@ -184,7 +188,7 @@ static void EnumDescriptor_FromEnumDef(zval *val, const upb_EnumDef *m) {
* up by numeric enum value, but by the index in the list of enum values. * up by numeric enum value, but by the index in the list of enum values.
*/ */
PHP_METHOD(EnumDescriptor, getValue) { PHP_METHOD(EnumDescriptor, getValue) {
EnumDescriptor *intern = (EnumDescriptor*)Z_OBJ_P(getThis()); EnumDescriptor* intern = (EnumDescriptor*)Z_OBJ_P(getThis());
zend_long index; zend_long index;
zval ret; zval ret;
@ -210,7 +214,7 @@ PHP_METHOD(EnumDescriptor, getValue) {
* Returns the number of values in this enum. * Returns the number of values in this enum.
*/ */
PHP_METHOD(EnumDescriptor, getValueCount) { PHP_METHOD(EnumDescriptor, getValueCount) {
EnumDescriptor *intern = (EnumDescriptor*)Z_OBJ_P(getThis()); EnumDescriptor* intern = (EnumDescriptor*)Z_OBJ_P(getThis());
RETURN_LONG(upb_EnumDef_ValueCount(intern->enumdef)); RETURN_LONG(upb_EnumDef_ValueCount(intern->enumdef));
} }
@ -221,16 +225,16 @@ PHP_METHOD(EnumDescriptor, getValueCount) {
* have two separate EnumDescriptor classes. We use a single class for both * have two separate EnumDescriptor classes. We use a single class for both
* the public and private descriptor. * the public and private descriptor.
*/ */
PHP_METHOD(EnumDescriptor, getPublicDescriptor) { PHP_METHOD(EnumDescriptor, getPublicDescriptor) { RETURN_COPY(getThis()); }
RETURN_COPY(getThis());
}
// clang-format off
static zend_function_entry EnumDescriptor_methods[] = { static zend_function_entry EnumDescriptor_methods[] = {
PHP_ME(EnumDescriptor, getPublicDescriptor, arginfo_void, ZEND_ACC_PUBLIC) PHP_ME(EnumDescriptor, getPublicDescriptor, arginfo_void, ZEND_ACC_PUBLIC)
PHP_ME(EnumDescriptor, getValueCount, arginfo_void, ZEND_ACC_PUBLIC) PHP_ME(EnumDescriptor, getValueCount, arginfo_void, ZEND_ACC_PUBLIC)
PHP_ME(EnumDescriptor, getValue, arginfo_getByIndex, ZEND_ACC_PUBLIC) PHP_ME(EnumDescriptor, getValue, arginfo_getByIndex, ZEND_ACC_PUBLIC)
ZEND_FE_END ZEND_FE_END
}; };
// clang-format on
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Oneof // Oneof
@ -238,18 +242,18 @@ static zend_function_entry EnumDescriptor_methods[] = {
typedef struct { typedef struct {
zend_object std; zend_object std;
const upb_OneofDef *oneofdef; const upb_OneofDef* oneofdef;
} OneofDescriptor; } OneofDescriptor;
zend_class_entry *OneofDescriptor_class_entry; zend_class_entry* OneofDescriptor_class_entry;
static zend_object_handlers OneofDescriptor_object_handlers; static zend_object_handlers OneofDescriptor_object_handlers;
static void OneofDescriptor_destructor(zend_object* obj) { static void OneofDescriptor_destructor(zend_object* obj) {
OneofDescriptor *intern = (OneofDescriptor*)obj; OneofDescriptor* intern = (OneofDescriptor*)obj;
ObjCache_Delete(intern->oneofdef); ObjCache_Delete(intern->oneofdef);
} }
static void OneofDescriptor_FromOneofDef(zval *val, const upb_OneofDef *o) { static void OneofDescriptor_FromOneofDef(zval* val, const upb_OneofDef* o) {
if (o == NULL) { if (o == NULL) {
ZVAL_NULL(val); ZVAL_NULL(val);
return; return;
@ -271,7 +275,7 @@ static void OneofDescriptor_FromOneofDef(zval *val, const upb_OneofDef *o) {
* Returns the name of this oneof. * Returns the name of this oneof.
*/ */
PHP_METHOD(OneofDescriptor, getName) { PHP_METHOD(OneofDescriptor, getName) {
OneofDescriptor *intern = (OneofDescriptor*)Z_OBJ_P(getThis()); OneofDescriptor* intern = (OneofDescriptor*)Z_OBJ_P(getThis());
RETURN_STRING(upb_OneofDef_Name(intern->oneofdef)); RETURN_STRING(upb_OneofDef_Name(intern->oneofdef));
} }
@ -282,7 +286,7 @@ PHP_METHOD(OneofDescriptor, getName) {
* [0, getFieldCount() - 1]. * [0, getFieldCount() - 1].
*/ */
PHP_METHOD(OneofDescriptor, getField) { PHP_METHOD(OneofDescriptor, getField) {
OneofDescriptor *intern = (OneofDescriptor*)Z_OBJ_P(getThis()); OneofDescriptor* intern = (OneofDescriptor*)Z_OBJ_P(getThis());
zend_long index; zend_long index;
zval ret; zval ret;
@ -307,16 +311,18 @@ PHP_METHOD(OneofDescriptor, getField) {
* Returns the number of fields in this oneof. * Returns the number of fields in this oneof.
*/ */
PHP_METHOD(OneofDescriptor, getFieldCount) { PHP_METHOD(OneofDescriptor, getFieldCount) {
OneofDescriptor *intern = (OneofDescriptor*)Z_OBJ_P(getThis()); OneofDescriptor* intern = (OneofDescriptor*)Z_OBJ_P(getThis());
RETURN_LONG(upb_OneofDef_FieldCount(intern->oneofdef)); RETURN_LONG(upb_OneofDef_FieldCount(intern->oneofdef));
} }
// clang-format off
static zend_function_entry OneofDescriptor_methods[] = { static zend_function_entry OneofDescriptor_methods[] = {
PHP_ME(OneofDescriptor, getName, arginfo_void, ZEND_ACC_PUBLIC) PHP_ME(OneofDescriptor, getName, arginfo_void, ZEND_ACC_PUBLIC)
PHP_ME(OneofDescriptor, getField, arginfo_getByIndex, ZEND_ACC_PUBLIC) PHP_ME(OneofDescriptor, getField, arginfo_getByIndex, ZEND_ACC_PUBLIC)
PHP_ME(OneofDescriptor, getFieldCount, arginfo_void, ZEND_ACC_PUBLIC) PHP_ME(OneofDescriptor, getFieldCount, arginfo_void, ZEND_ACC_PUBLIC)
ZEND_FE_END ZEND_FE_END
}; };
// clang-format on
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// FieldDescriptor // FieldDescriptor
@ -324,19 +330,19 @@ static zend_function_entry OneofDescriptor_methods[] = {
typedef struct { typedef struct {
zend_object std; zend_object std;
const upb_FieldDef *fielddef; const upb_FieldDef* fielddef;
} FieldDescriptor; } FieldDescriptor;
zend_class_entry *FieldDescriptor_class_entry; zend_class_entry* FieldDescriptor_class_entry;
static zend_object_handlers FieldDescriptor_object_handlers; static zend_object_handlers FieldDescriptor_object_handlers;
static void FieldDescriptor_destructor(zend_object* obj) { static void FieldDescriptor_destructor(zend_object* obj) {
FieldDescriptor *intern = (FieldDescriptor*)obj; FieldDescriptor* intern = (FieldDescriptor*)obj;
ObjCache_Delete(intern->fielddef); ObjCache_Delete(intern->fielddef);
} }
// Caller owns a ref on the returned zval. // Caller owns a ref on the returned zval.
static void FieldDescriptor_FromFieldDef(zval *val, const upb_FieldDef *f) { static void FieldDescriptor_FromFieldDef(zval* val, const upb_FieldDef* f) {
if (f == NULL) { if (f == NULL) {
ZVAL_NULL(val); ZVAL_NULL(val);
return; return;
@ -354,31 +360,30 @@ static void FieldDescriptor_FromFieldDef(zval *val, const upb_FieldDef *f) {
upb_CType to_fieldtype(upb_FieldType type) { upb_CType to_fieldtype(upb_FieldType type) {
switch (type) { switch (type) {
#define CASE(descriptor_type, type) \ #define CASE(descriptor_type, type) \
case kUpb_FieldType_##descriptor_type: \ case kUpb_FieldType_##descriptor_type: \
return kUpb_CType_##type; return kUpb_CType_##type;
CASE(Float, Float); CASE(Float, Float);
CASE(Double, Double); CASE(Double, Double);
CASE(Bool, Bool); CASE(Bool, Bool);
CASE(String, String); CASE(String, String);
CASE(Bytes, Bytes); CASE(Bytes, Bytes);
CASE(Message, Message); CASE(Message, Message);
CASE(Group, Message); CASE(Group, Message);
CASE(Enum, Enum); CASE(Enum, Enum);
CASE(Int32, Int32); CASE(Int32, Int32);
CASE(Int64, Int64); CASE(Int64, Int64);
CASE(UInt32, UInt32); CASE(UInt32, UInt32);
CASE(UInt64, UInt64); CASE(UInt64, UInt64);
CASE(SInt32, Int32); CASE(SInt32, Int32);
CASE(SInt64, Int64); CASE(SInt64, Int64);
CASE(Fixed32, UInt32); CASE(Fixed32, UInt32);
CASE(Fixed64, UInt64); CASE(Fixed64, UInt64);
CASE(SFixed32, Int32); CASE(SFixed32, Int32);
CASE(SFixed64, Int64); CASE(SFixed64, Int64);
#undef CONVERT #undef CONVERT
} }
zend_error(E_ERROR, "Unknown field type."); zend_error(E_ERROR, "Unknown field type.");
@ -391,7 +396,7 @@ upb_CType to_fieldtype(upb_FieldType type) {
* Returns the name of this field. * Returns the name of this field.
*/ */
PHP_METHOD(FieldDescriptor, getName) { PHP_METHOD(FieldDescriptor, getName) {
FieldDescriptor *intern = (FieldDescriptor*)Z_OBJ_P(getThis()); FieldDescriptor* intern = (FieldDescriptor*)Z_OBJ_P(getThis());
RETURN_STRING(upb_FieldDef_Name(intern->fielddef)); RETURN_STRING(upb_FieldDef_Name(intern->fielddef));
} }
@ -401,7 +406,7 @@ PHP_METHOD(FieldDescriptor, getName) {
* Returns the number of this field. * Returns the number of this field.
*/ */
PHP_METHOD(FieldDescriptor, getNumber) { PHP_METHOD(FieldDescriptor, getNumber) {
FieldDescriptor *intern = (FieldDescriptor*)Z_OBJ_P(getThis()); FieldDescriptor* intern = (FieldDescriptor*)Z_OBJ_P(getThis());
RETURN_LONG(upb_FieldDef_Number(intern->fielddef)); RETURN_LONG(upb_FieldDef_Number(intern->fielddef));
} }
@ -411,7 +416,7 @@ PHP_METHOD(FieldDescriptor, getNumber) {
* Returns the label of this field as an integer. * Returns the label of this field as an integer.
*/ */
PHP_METHOD(FieldDescriptor, getLabel) { PHP_METHOD(FieldDescriptor, getLabel) {
FieldDescriptor *intern = (FieldDescriptor*)Z_OBJ_P(getThis()); FieldDescriptor* intern = (FieldDescriptor*)Z_OBJ_P(getThis());
RETURN_LONG(upb_FieldDef_Label(intern->fielddef)); RETURN_LONG(upb_FieldDef_Label(intern->fielddef));
} }
@ -421,7 +426,7 @@ PHP_METHOD(FieldDescriptor, getLabel) {
* Returns the type of this field as an integer. * Returns the type of this field as an integer.
*/ */
PHP_METHOD(FieldDescriptor, getType) { PHP_METHOD(FieldDescriptor, getType) {
FieldDescriptor *intern = (FieldDescriptor*)Z_OBJ_P(getThis()); FieldDescriptor* intern = (FieldDescriptor*)Z_OBJ_P(getThis());
RETURN_LONG(upb_FieldDef_Type(intern->fielddef)); RETURN_LONG(upb_FieldDef_Type(intern->fielddef));
} }
@ -431,7 +436,7 @@ PHP_METHOD(FieldDescriptor, getType) {
* Returns true if this field is a map. * Returns true if this field is a map.
*/ */
PHP_METHOD(FieldDescriptor, isMap) { PHP_METHOD(FieldDescriptor, isMap) {
FieldDescriptor *intern = (FieldDescriptor*)Z_OBJ_P(getThis()); FieldDescriptor* intern = (FieldDescriptor*)Z_OBJ_P(getThis());
RETURN_BOOL(upb_FieldDef_IsMap(intern->fielddef)); RETURN_BOOL(upb_FieldDef_IsMap(intern->fielddef));
} }
@ -441,8 +446,8 @@ PHP_METHOD(FieldDescriptor, isMap) {
* Returns the EnumDescriptor for this field, which must be an enum. * Returns the EnumDescriptor for this field, which must be an enum.
*/ */
PHP_METHOD(FieldDescriptor, getEnumType) { PHP_METHOD(FieldDescriptor, getEnumType) {
FieldDescriptor *intern = (FieldDescriptor*)Z_OBJ_P(getThis()); FieldDescriptor* intern = (FieldDescriptor*)Z_OBJ_P(getThis());
const upb_EnumDef *e = upb_FieldDef_EnumSubDef(intern->fielddef); const upb_EnumDef* e = upb_FieldDef_EnumSubDef(intern->fielddef);
zval ret; zval ret;
if (!e) { if (!e) {
@ -463,8 +468,8 @@ PHP_METHOD(FieldDescriptor, getEnumType) {
* a oneof. * a oneof.
*/ */
PHP_METHOD(FieldDescriptor, getContainingOneof) { PHP_METHOD(FieldDescriptor, getContainingOneof) {
FieldDescriptor *intern = (FieldDescriptor*)Z_OBJ_P(getThis()); FieldDescriptor* intern = (FieldDescriptor*)Z_OBJ_P(getThis());
const upb_OneofDef *o = upb_FieldDef_ContainingOneof(intern->fielddef); const upb_OneofDef* o = upb_FieldDef_ContainingOneof(intern->fielddef);
zval ret; zval ret;
if (!o) { if (!o) {
@ -482,8 +487,8 @@ PHP_METHOD(FieldDescriptor, getContainingOneof) {
* not inside a oneof. * not inside a oneof.
*/ */
PHP_METHOD(FieldDescriptor, getRealContainingOneof) { PHP_METHOD(FieldDescriptor, getRealContainingOneof) {
FieldDescriptor *intern = (FieldDescriptor*)Z_OBJ_P(getThis()); FieldDescriptor* intern = (FieldDescriptor*)Z_OBJ_P(getThis());
const upb_OneofDef *o = upb_FieldDef_RealContainingOneof(intern->fielddef); const upb_OneofDef* o = upb_FieldDef_RealContainingOneof(intern->fielddef);
zval ret; zval ret;
if (!o) { if (!o) {
@ -500,7 +505,7 @@ PHP_METHOD(FieldDescriptor, getRealContainingOneof) {
* Returns the Descriptor for this field, which must be a message. * Returns the Descriptor for this field, which must be a message.
*/ */
PHP_METHOD(FieldDescriptor, getMessageType) { PHP_METHOD(FieldDescriptor, getMessageType) {
FieldDescriptor *intern = (FieldDescriptor*)Z_OBJ_P(getThis()); FieldDescriptor* intern = (FieldDescriptor*)Z_OBJ_P(getThis());
Descriptor* desc = Descriptor_GetFromFieldDef(intern->fielddef); Descriptor* desc = Descriptor_GetFromFieldDef(intern->fielddef);
if (!desc) { if (!desc) {
@ -513,6 +518,7 @@ PHP_METHOD(FieldDescriptor, getMessageType) {
RETURN_OBJ_COPY(&desc->std); RETURN_OBJ_COPY(&desc->std);
} }
// clang-format off
static zend_function_entry FieldDescriptor_methods[] = { static zend_function_entry FieldDescriptor_methods[] = {
PHP_ME(FieldDescriptor, getName, arginfo_void, ZEND_ACC_PUBLIC) PHP_ME(FieldDescriptor, getName, arginfo_void, ZEND_ACC_PUBLIC)
PHP_ME(FieldDescriptor, getNumber, arginfo_void, ZEND_ACC_PUBLIC) PHP_ME(FieldDescriptor, getNumber, arginfo_void, ZEND_ACC_PUBLIC)
@ -525,12 +531,13 @@ static zend_function_entry FieldDescriptor_methods[] = {
PHP_ME(FieldDescriptor, getMessageType, arginfo_void, ZEND_ACC_PUBLIC) PHP_ME(FieldDescriptor, getMessageType, arginfo_void, ZEND_ACC_PUBLIC)
ZEND_FE_END ZEND_FE_END
}; };
// clang-format on
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Descriptor // Descriptor
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
zend_class_entry *Descriptor_class_entry; zend_class_entry* Descriptor_class_entry;
static zend_object_handlers Descriptor_object_handlers; static zend_object_handlers Descriptor_object_handlers;
static void Descriptor_destructor(zend_object* obj) { static void Descriptor_destructor(zend_object* obj) {
@ -538,14 +545,14 @@ static void Descriptor_destructor(zend_object* obj) {
// collected before the end of the request. // collected before the end of the request.
} }
static zend_class_entry *Descriptor_GetGeneratedClass(const upb_MessageDef *m) { static zend_class_entry* Descriptor_GetGeneratedClass(const upb_MessageDef* m) {
for (int i = 0; i < 2; ++i) { for (int i = 0; i < 2; ++i) {
char *classname = char* classname = GetPhpClassname(upb_MessageDef_File(m),
GetPhpClassname(upb_MessageDef_File(m), upb_MessageDef_FullName(m), (bool)i); upb_MessageDef_FullName(m), (bool)i);
zend_string *str = zend_string_init(classname, strlen(classname), 0); zend_string* str = zend_string_init(classname, strlen(classname), 0);
zend_class_entry *ce = zend_lookup_class(str); // May autoload the class. zend_class_entry* ce = zend_lookup_class(str); // May autoload the class.
zend_string_release (str); zend_string_release(str);
free(classname); free(classname);
if (ce) { if (ce) {
@ -553,20 +560,20 @@ static zend_class_entry *Descriptor_GetGeneratedClass(const upb_MessageDef *m) {
} }
} }
char *classname = char* classname = GetPhpClassname(upb_MessageDef_File(m),
GetPhpClassname(upb_MessageDef_File(m), upb_MessageDef_FullName(m), false); upb_MessageDef_FullName(m), false);
zend_error(E_ERROR, "Couldn't load generated class %s", classname); zend_error(E_ERROR, "Couldn't load generated class %s", classname);
return NULL; return NULL;
} }
void Descriptor_FromMessageDef(zval *val, const upb_MessageDef *m) { void Descriptor_FromMessageDef(zval* val, const upb_MessageDef* m) {
if (m == NULL) { if (m == NULL) {
ZVAL_NULL(val); ZVAL_NULL(val);
return; return;
} }
if (!ObjCache_Get(m, val)) { if (!ObjCache_Get(m, val)) {
zend_class_entry *ce = NULL; zend_class_entry* ce = NULL;
if (!upb_MessageDef_IsMapEntry(m)) { // Map entries don't have a class. if (!upb_MessageDef_IsMapEntry(m)) { // Map entries don't have a class.
ce = Descriptor_GetGeneratedClass(m); ce = Descriptor_GetGeneratedClass(m);
if (!ce) { if (!ce) {
@ -585,7 +592,7 @@ void Descriptor_FromMessageDef(zval *val, const upb_MessageDef *m) {
} }
} }
static void Descriptor_FromClassEntry(zval *val, zend_class_entry *ce) { static void Descriptor_FromClassEntry(zval* val, zend_class_entry* ce) {
if (ce) { if (ce) {
Descriptor_FromMessageDef(val, NameMap_GetMessage(ce)); Descriptor_FromMessageDef(val, NameMap_GetMessage(ce));
} else { } else {
@ -593,7 +600,7 @@ static void Descriptor_FromClassEntry(zval *val, zend_class_entry *ce) {
} }
} }
static Descriptor* Descriptor_GetFromZval(zval *val) { static Descriptor* Descriptor_GetFromZval(zval* val) {
if (Z_TYPE_P(val) == IS_NULL) { if (Z_TYPE_P(val) == IS_NULL) {
return NULL; return NULL;
} else { } else {
@ -607,19 +614,19 @@ static Descriptor* Descriptor_GetFromZval(zval *val) {
// These are documented in the header file. // These are documented in the header file.
Descriptor* Descriptor_GetFromClassEntry(zend_class_entry *ce) { Descriptor* Descriptor_GetFromClassEntry(zend_class_entry* ce) {
zval desc; zval desc;
Descriptor_FromClassEntry(&desc, ce); Descriptor_FromClassEntry(&desc, ce);
return Descriptor_GetFromZval(&desc); return Descriptor_GetFromZval(&desc);
} }
Descriptor* Descriptor_GetFromMessageDef(const upb_MessageDef *m) { Descriptor* Descriptor_GetFromMessageDef(const upb_MessageDef* m) {
zval desc; zval desc;
Descriptor_FromMessageDef(&desc, m); Descriptor_FromMessageDef(&desc, m);
return Descriptor_GetFromZval(&desc); return Descriptor_GetFromZval(&desc);
} }
Descriptor* Descriptor_GetFromFieldDef(const upb_FieldDef *f) { Descriptor* Descriptor_GetFromFieldDef(const upb_FieldDef* f) {
return Descriptor_GetFromMessageDef(upb_FieldDef_MessageSubDef(f)); return Descriptor_GetFromMessageDef(upb_FieldDef_MessageSubDef(f));
} }
@ -630,9 +637,7 @@ Descriptor* Descriptor_GetFromFieldDef(const upb_FieldDef *f) {
* have two separate EnumDescriptor classes. We use a single class for both * have two separate EnumDescriptor classes. We use a single class for both
* the public and private descriptor. * the public and private descriptor.
*/ */
PHP_METHOD(Descriptor, getPublicDescriptor) { PHP_METHOD(Descriptor, getPublicDescriptor) { RETURN_COPY(getThis()); }
RETURN_COPY(getThis());
}
/* /*
* Descriptor::getFullName() * Descriptor::getFullName()
@ -640,7 +645,7 @@ PHP_METHOD(Descriptor, getPublicDescriptor) {
* Returns the full name for this message type. * Returns the full name for this message type.
*/ */
PHP_METHOD(Descriptor, getFullName) { PHP_METHOD(Descriptor, getFullName) {
Descriptor *intern = (Descriptor*)Z_OBJ_P(getThis()); Descriptor* intern = (Descriptor*)Z_OBJ_P(getThis());
RETURN_STRING(upb_MessageDef_FullName(intern->msgdef)); RETURN_STRING(upb_MessageDef_FullName(intern->msgdef));
} }
@ -651,7 +656,7 @@ PHP_METHOD(Descriptor, getFullName) {
* [0, getFieldCount()-1]. * [0, getFieldCount()-1].
*/ */
PHP_METHOD(Descriptor, getField) { PHP_METHOD(Descriptor, getField) {
Descriptor *intern = (Descriptor*)Z_OBJ_P(getThis()); Descriptor* intern = (Descriptor*)Z_OBJ_P(getThis());
int count = upb_MessageDef_FieldCount(intern->msgdef); int count = upb_MessageDef_FieldCount(intern->msgdef);
zval ret; zval ret;
zend_long index; zend_long index;
@ -666,7 +671,8 @@ PHP_METHOD(Descriptor, getField) {
return; return;
} }
FieldDescriptor_FromFieldDef(&ret, upb_MessageDef_Field(intern->msgdef, index)); FieldDescriptor_FromFieldDef(&ret,
upb_MessageDef_Field(intern->msgdef, index));
RETURN_COPY_VALUE(&ret); RETURN_COPY_VALUE(&ret);
} }
@ -676,7 +682,7 @@ PHP_METHOD(Descriptor, getField) {
* Returns the number of fields in this message. * Returns the number of fields in this message.
*/ */
PHP_METHOD(Descriptor, getFieldCount) { PHP_METHOD(Descriptor, getFieldCount) {
Descriptor *intern = (Descriptor*)Z_OBJ_P(getThis()); Descriptor* intern = (Descriptor*)Z_OBJ_P(getThis());
RETURN_LONG(upb_MessageDef_FieldCount(intern->msgdef)); RETURN_LONG(upb_MessageDef_FieldCount(intern->msgdef));
} }
@ -687,7 +693,7 @@ PHP_METHOD(Descriptor, getFieldCount) {
* [0, getOneofDeclCount()]. * [0, getOneofDeclCount()].
*/ */
PHP_METHOD(Descriptor, getOneofDecl) { PHP_METHOD(Descriptor, getOneofDecl) {
Descriptor *intern = (Descriptor*)Z_OBJ_P(getThis()); Descriptor* intern = (Descriptor*)Z_OBJ_P(getThis());
zend_long index; zend_long index;
zval ret; zval ret;
@ -701,7 +707,8 @@ PHP_METHOD(Descriptor, getOneofDecl) {
return; return;
} }
OneofDescriptor_FromOneofDef(&ret, upb_MessageDef_Oneof(intern->msgdef, index)); OneofDescriptor_FromOneofDef(&ret,
upb_MessageDef_Oneof(intern->msgdef, index));
RETURN_COPY_VALUE(&ret); RETURN_COPY_VALUE(&ret);
} }
@ -711,7 +718,7 @@ PHP_METHOD(Descriptor, getOneofDecl) {
* Returns the number of oneofs in this message. * Returns the number of oneofs in this message.
*/ */
PHP_METHOD(Descriptor, getOneofDeclCount) { PHP_METHOD(Descriptor, getOneofDeclCount) {
Descriptor *intern = (Descriptor*)Z_OBJ_P(getThis()); Descriptor* intern = (Descriptor*)Z_OBJ_P(getThis());
RETURN_LONG(upb_MessageDef_OneofCount(intern->msgdef)); RETURN_LONG(upb_MessageDef_OneofCount(intern->msgdef));
} }
@ -721,12 +728,12 @@ PHP_METHOD(Descriptor, getOneofDeclCount) {
* Returns the name of the PHP class for this message. * Returns the name of the PHP class for this message.
*/ */
PHP_METHOD(Descriptor, getClass) { PHP_METHOD(Descriptor, getClass) {
Descriptor *intern = (Descriptor*)Z_OBJ_P(getThis()); Descriptor* intern = (Descriptor*)Z_OBJ_P(getThis());
const char* classname = ZSTR_VAL(intern->class_entry->name); const char* classname = ZSTR_VAL(intern->class_entry->name);
RETURN_STRING(classname); RETURN_STRING(classname);
} }
// clang-format off
static zend_function_entry Descriptor_methods[] = { static zend_function_entry Descriptor_methods[] = {
PHP_ME(Descriptor, getClass, arginfo_void, ZEND_ACC_PUBLIC) PHP_ME(Descriptor, getClass, arginfo_void, ZEND_ACC_PUBLIC)
PHP_ME(Descriptor, getFullName, arginfo_void, ZEND_ACC_PUBLIC) PHP_ME(Descriptor, getFullName, arginfo_void, ZEND_ACC_PUBLIC)
@ -737,6 +744,7 @@ static zend_function_entry Descriptor_methods[] = {
PHP_ME(Descriptor, getPublicDescriptor, arginfo_void, ZEND_ACC_PUBLIC) PHP_ME(Descriptor, getPublicDescriptor, arginfo_void, ZEND_ACC_PUBLIC)
ZEND_FE_END ZEND_FE_END
}; };
// clang-format on
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// DescriptorPool // DescriptorPool
@ -744,13 +752,13 @@ static zend_function_entry Descriptor_methods[] = {
typedef struct DescriptorPool { typedef struct DescriptorPool {
zend_object std; zend_object std;
upb_DefPool *symtab; upb_DefPool* symtab;
} DescriptorPool; } DescriptorPool;
zend_class_entry *DescriptorPool_class_entry; zend_class_entry* DescriptorPool_class_entry;
static zend_object_handlers DescriptorPool_object_handlers; static zend_object_handlers DescriptorPool_object_handlers;
static DescriptorPool *GetPool(const zval* this_ptr) { static DescriptorPool* GetPool(const zval* this_ptr) {
return (DescriptorPool*)Z_OBJ_P(this_ptr); return (DescriptorPool*)Z_OBJ_P(this_ptr);
} }
@ -767,8 +775,8 @@ static void DescriptorPool_destructor(zend_object* obj) {
zend_object_std_dtor(&intern->std); zend_object_std_dtor(&intern->std);
} }
void DescriptorPool_CreateWithSymbolTable(zval *zv, upb_DefPool *symtab) { void DescriptorPool_CreateWithSymbolTable(zval* zv, upb_DefPool* symtab) {
DescriptorPool *intern = emalloc(sizeof(DescriptorPool)); DescriptorPool* intern = emalloc(sizeof(DescriptorPool));
zend_object_std_init(&intern->std, DescriptorPool_class_entry); zend_object_std_init(&intern->std, DescriptorPool_class_entry);
intern->std.handlers = &DescriptorPool_object_handlers; intern->std.handlers = &DescriptorPool_object_handlers;
intern->symtab = symtab; intern->symtab = symtab;
@ -776,9 +784,7 @@ void DescriptorPool_CreateWithSymbolTable(zval *zv, upb_DefPool *symtab) {
ZVAL_OBJ(zv, &intern->std); ZVAL_OBJ(zv, &intern->std);
} }
upb_DefPool *DescriptorPool_GetSymbolTable() { upb_DefPool* DescriptorPool_GetSymbolTable() { return get_global_symtab(); }
return get_global_symtab();
}
/* /*
* DescriptorPool::getGeneratedPool() * DescriptorPool::getGeneratedPool()
@ -795,10 +801,10 @@ PHP_METHOD(DescriptorPool, getGeneratedPool) {
* Returns a Descriptor object for the given PHP class name. * Returns a Descriptor object for the given PHP class name.
*/ */
PHP_METHOD(DescriptorPool, getDescriptorByClassName) { PHP_METHOD(DescriptorPool, getDescriptorByClassName) {
char *classname = NULL; char* classname = NULL;
zend_long classname_len; zend_long classname_len;
zend_class_entry *ce; zend_class_entry* ce;
zend_string *str; zend_string* str;
zval ret; zval ret;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &classname, &classname_len) == if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &classname, &classname_len) ==
@ -808,7 +814,7 @@ PHP_METHOD(DescriptorPool, getDescriptorByClassName) {
str = zend_string_init(classname, strlen(classname), 0); str = zend_string_init(classname, strlen(classname), 0);
ce = zend_lookup_class(str); // May autoload the class. ce = zend_lookup_class(str); // May autoload the class.
zend_string_release (str); zend_string_release(str);
if (!ce) { if (!ce) {
RETURN_NULL(); RETURN_NULL();
@ -824,10 +830,10 @@ PHP_METHOD(DescriptorPool, getDescriptorByClassName) {
* Returns a EnumDescriptor object for the given PHP class name. * Returns a EnumDescriptor object for the given PHP class name.
*/ */
PHP_METHOD(DescriptorPool, getEnumDescriptorByClassName) { PHP_METHOD(DescriptorPool, getEnumDescriptorByClassName) {
char *classname = NULL; char* classname = NULL;
zend_long classname_len; zend_long classname_len;
zend_class_entry *ce; zend_class_entry* ce;
zend_string *str; zend_string* str;
zval ret; zval ret;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &classname, &classname_len) == if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &classname, &classname_len) ==
@ -837,7 +843,7 @@ PHP_METHOD(DescriptorPool, getEnumDescriptorByClassName) {
str = zend_string_init(classname, strlen(classname), 0); str = zend_string_init(classname, strlen(classname), 0);
ce = zend_lookup_class(str); // May autoload the class. ce = zend_lookup_class(str); // May autoload the class.
zend_string_release (str); zend_string_release(str);
if (!ce) { if (!ce) {
RETURN_NULL(); RETURN_NULL();
@ -853,10 +859,10 @@ PHP_METHOD(DescriptorPool, getEnumDescriptorByClassName) {
* Returns a Descriptor object for the given protobuf message name. * Returns a Descriptor object for the given protobuf message name.
*/ */
PHP_METHOD(DescriptorPool, getDescriptorByProtoName) { PHP_METHOD(DescriptorPool, getDescriptorByProtoName) {
DescriptorPool *intern = GetPool(getThis()); DescriptorPool* intern = GetPool(getThis());
char *protoname = NULL; char* protoname = NULL;
zend_long protoname_len; zend_long protoname_len;
const upb_MessageDef *m; const upb_MessageDef* m;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &protoname, &protoname_len) == if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &protoname, &protoname_len) ==
FAILURE) { FAILURE) {
@ -880,8 +886,9 @@ PHP_METHOD(DescriptorPool, getDescriptorByProtoName) {
* Returns true if this FileDescriptorProto depends on descriptor.proto. * Returns true if this FileDescriptorProto depends on descriptor.proto.
*/ */
bool depends_on_descriptor(const google_protobuf_FileDescriptorProto* file) { bool depends_on_descriptor(const google_protobuf_FileDescriptorProto* file) {
const upb_StringView *deps; const upb_StringView* deps;
upb_StringView name = upb_StringView_FromString("google/protobuf/descriptor.proto"); upb_StringView name =
upb_StringView_FromString("google/protobuf/descriptor.proto");
size_t i, n; size_t i, n;
deps = google_protobuf_FileDescriptorProto_dependency(file, &n); deps = google_protobuf_FileDescriptorProto_dependency(file, &n);
@ -894,7 +901,7 @@ bool depends_on_descriptor(const google_protobuf_FileDescriptorProto* file) {
return false; return false;
} }
static void add_message_name_mappings(const upb_MessageDef *message) { static void add_message_name_mappings(const upb_MessageDef* message) {
NameMap_AddMessage(message); NameMap_AddMessage(message);
int msg_n = upb_MessageDef_NestedMessageCount(message); int msg_n = upb_MessageDef_NestedMessageCount(message);
for (int i = 0; i < msg_n; i++) { for (int i = 0; i < msg_n; i++) {
@ -911,7 +918,7 @@ static void add_message_name_mappings(const upb_MessageDef *message) {
* *
* Adds the messages and enums in this file to the NameMap. * Adds the messages and enums in this file to the NameMap.
*/ */
static void add_name_mappings(const upb_FileDef *file) { static void add_name_mappings(const upb_FileDef* file) {
for (int i = 0; i < upb_FileDef_TopLevelMessageCount(file); i++) { for (int i = 0; i < upb_FileDef_TopLevelMessageCount(file); i++) {
add_message_name_mappings(upb_FileDef_TopLevelMessage(file, i)); add_message_name_mappings(upb_FileDef_TopLevelMessage(file, i));
} }
@ -921,11 +928,11 @@ static void add_name_mappings(const upb_FileDef *file) {
} }
} }
static void add_descriptor(upb_DefPool *symtab, static void add_descriptor(upb_DefPool* symtab,
const google_protobuf_FileDescriptorProto *file) { const google_protobuf_FileDescriptorProto* file) {
upb_StringView name = google_protobuf_FileDescriptorProto_name(file); upb_StringView name = google_protobuf_FileDescriptorProto_name(file);
upb_Status status; upb_Status status;
const upb_FileDef *file_def; const upb_FileDef* file_def;
upb_Status_Clear(&status); upb_Status_Clear(&status);
if (upb_DefPool_FindFileByNameWithSize(symtab, name.data, name.size)) { if (upb_DefPool_FindFileByNameWithSize(symtab, name.data, name.size)) {
@ -933,8 +940,8 @@ static void add_descriptor(upb_DefPool *symtab,
// TODO(teboring): Re-enable this warning when aggregate metadata is // TODO(teboring): Re-enable this warning when aggregate metadata is
// deprecated. // deprecated.
// zend_error(E_USER_WARNING, // zend_error(E_USER_WARNING,
// "proto descriptor was previously loaded (included in multiple " // "proto descriptor was previously loaded (included in multiple
// "metadata bundles?): " UPB_STRINGVIEW_FORMAT, // " "metadata bundles?): " UPB_STRINGVIEW_FORMAT,
// UPB_STRINGVIEW_ARGS(name)); // UPB_STRINGVIEW_ARGS(name));
return; return;
} }
@ -956,10 +963,10 @@ static void add_descriptor(upb_DefPool *symtab,
* *
* Adds the given descriptor data to this DescriptorPool. * Adds the given descriptor data to this DescriptorPool.
*/ */
static void add_descriptor_set(upb_DefPool *symtab, const char *data, static void add_descriptor_set(upb_DefPool* symtab, const char* data,
int data_len, upb_Arena *arena) { int data_len, upb_Arena* arena) {
size_t i, n; size_t i, n;
google_protobuf_FileDescriptorSet *set; google_protobuf_FileDescriptorSet* set;
const google_protobuf_FileDescriptorProto* const* files; const google_protobuf_FileDescriptorProto* const* files;
set = google_protobuf_FileDescriptorSet_parse(data, data_len, arena); set = google_protobuf_FileDescriptorSet_parse(data, data_len, arena);
@ -977,14 +984,14 @@ static void add_descriptor_set(upb_DefPool *symtab, const char *data,
} }
} }
bool DescriptorPool_HasFile(const char *filename) { bool DescriptorPool_HasFile(const char* filename) {
return upb_DefPool_FindFileByName(get_global_symtab(), filename) != NULL; return upb_DefPool_FindFileByName(get_global_symtab(), filename) != NULL;
} }
void DescriptorPool_AddDescriptor(const char *filename, const char *data, void DescriptorPool_AddDescriptor(const char* filename, const char* data,
int size) { int size) {
upb_Arena *arena = upb_Arena_New(); upb_Arena* arena = upb_Arena_New();
const google_protobuf_FileDescriptorProto *file = const google_protobuf_FileDescriptorProto* file =
google_protobuf_FileDescriptorProto_parse(data, size, arena); google_protobuf_FileDescriptorProto_parse(data, size, arena);
if (!file) { if (!file) {
@ -1002,11 +1009,11 @@ void DescriptorPool_AddDescriptor(const char *filename, const char *data,
* Adds the given descriptor data to this DescriptorPool. * Adds the given descriptor data to this DescriptorPool.
*/ */
PHP_METHOD(DescriptorPool, internalAddGeneratedFile) { PHP_METHOD(DescriptorPool, internalAddGeneratedFile) {
DescriptorPool *intern = GetPool(getThis()); DescriptorPool* intern = GetPool(getThis());
char *data = NULL; char* data = NULL;
zend_long data_len; zend_long data_len;
zend_bool use_nested_submsg = false; zend_bool use_nested_submsg = false;
upb_Arena *arena; upb_Arena* arena;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|b", &data, &data_len, if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|b", &data, &data_len,
&use_nested_submsg) != SUCCESS) { &use_nested_submsg) != SUCCESS) {
@ -1018,6 +1025,7 @@ PHP_METHOD(DescriptorPool, internalAddGeneratedFile) {
upb_Arena_Free(arena); upb_Arena_Free(arena);
} }
// clang-format off
ZEND_BEGIN_ARG_INFO_EX(arginfo_lookupByName, 0, 0, 1) ZEND_BEGIN_ARG_INFO_EX(arginfo_lookupByName, 0, 0, 1)
ZEND_ARG_INFO(0, name) ZEND_ARG_INFO(0, name)
ZEND_END_ARG_INFO() ZEND_END_ARG_INFO()
@ -1036,6 +1044,7 @@ static zend_function_entry DescriptorPool_methods[] = {
PHP_ME(DescriptorPool, internalAddGeneratedFile, arginfo_addgeneratedfile, ZEND_ACC_PUBLIC) PHP_ME(DescriptorPool, internalAddGeneratedFile, arginfo_addgeneratedfile, ZEND_ACC_PUBLIC)
ZEND_FE_END ZEND_FE_END
}; };
// clang-format on
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// InternalDescriptorPool // InternalDescriptorPool
@ -1045,7 +1054,7 @@ static zend_function_entry DescriptorPool_methods[] = {
// separate instantiable object, it just returns a // separate instantiable object, it just returns a
// Google\Protobuf\DescriptorPool. // Google\Protobuf\DescriptorPool.
zend_class_entry *InternalDescriptorPool_class_entry; zend_class_entry* InternalDescriptorPool_class_entry;
/* /*
* InternalDescriptorPool::getGeneratedPool() * InternalDescriptorPool::getGeneratedPool()
@ -1058,11 +1067,13 @@ PHP_METHOD(InternalDescriptorPool, getGeneratedPool) {
DescriptorPool_CreateWithSymbolTable(return_value, get_global_symtab()); DescriptorPool_CreateWithSymbolTable(return_value, get_global_symtab());
} }
// clang-format off
static zend_function_entry InternalDescriptorPool_methods[] = { static zend_function_entry InternalDescriptorPool_methods[] = {
PHP_ME(InternalDescriptorPool, getGeneratedPool, arginfo_void, PHP_ME(InternalDescriptorPool, getGeneratedPool, arginfo_void,
ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
ZEND_FE_END ZEND_FE_END
}; };
// clang-format on
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// GPBType // GPBType
@ -1070,9 +1081,7 @@ static zend_function_entry InternalDescriptorPool_methods[] = {
zend_class_entry* gpb_type_type; zend_class_entry* gpb_type_type;
static zend_function_entry gpb_type_methods[] = { static zend_function_entry gpb_type_methods[] = {ZEND_FE_END};
ZEND_FE_END
};
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Module Init // Module Init
@ -1080,7 +1089,7 @@ static zend_function_entry gpb_type_methods[] = {
void Def_ModuleInit() { void Def_ModuleInit() {
zend_class_entry tmp_ce; zend_class_entry tmp_ce;
zend_object_handlers *h; zend_object_handlers* h;
INIT_CLASS_ENTRY(tmp_ce, "Google\\Protobuf\\OneofDescriptor", INIT_CLASS_ENTRY(tmp_ce, "Google\\Protobuf\\OneofDescriptor",
OneofDescriptor_methods); OneofDescriptor_methods);
@ -1108,8 +1117,7 @@ void Def_ModuleInit() {
memcpy(h, &std_object_handlers, sizeof(zend_object_handlers)); memcpy(h, &std_object_handlers, sizeof(zend_object_handlers));
h->dtor_obj = &EnumDescriptor_destructor; h->dtor_obj = &EnumDescriptor_destructor;
INIT_CLASS_ENTRY(tmp_ce, "Google\\Protobuf\\Descriptor", INIT_CLASS_ENTRY(tmp_ce, "Google\\Protobuf\\Descriptor", Descriptor_methods);
Descriptor_methods);
Descriptor_class_entry = zend_register_internal_class(&tmp_ce); Descriptor_class_entry = zend_register_internal_class(&tmp_ce);
Descriptor_class_entry->ce_flags |= ZEND_ACC_FINAL; Descriptor_class_entry->ce_flags |= ZEND_ACC_FINAL;

@ -40,38 +40,39 @@ void Def_ModuleInit();
// Creates a new DescriptorPool to wrap the given symtab, which must not be // Creates a new DescriptorPool to wrap the given symtab, which must not be
// NULL. // NULL.
void DescriptorPool_CreateWithSymbolTable(zval *zv, upb_DefPool *symtab); void DescriptorPool_CreateWithSymbolTable(zval* zv, upb_DefPool* symtab);
upb_DefPool *DescriptorPool_GetSymbolTable(); upb_DefPool* DescriptorPool_GetSymbolTable();
// Returns true if the global descriptor pool already has the given filename. // Returns true if the global descriptor pool already has the given filename.
bool DescriptorPool_HasFile(const char *filename); bool DescriptorPool_HasFile(const char* filename);
// Adds the given descriptor with the given filename to the global pool. // Adds the given descriptor with the given filename to the global pool.
void DescriptorPool_AddDescriptor(const char *filename, const char *data, int size); void DescriptorPool_AddDescriptor(const char* filename, const char* data,
int size);
typedef struct Descriptor { typedef struct Descriptor {
zend_object std; zend_object std;
const upb_MessageDef *msgdef; const upb_MessageDef* msgdef;
zend_class_entry *class_entry; zend_class_entry* class_entry;
} Descriptor; } Descriptor;
// Gets or creates a Descriptor* for the given class entry, upb_MessageDef, or // Gets or creates a Descriptor* for the given class entry, upb_MessageDef, or
// upb_FieldDef. The returned Descriptor* will live for the entire request, // upb_FieldDef. The returned Descriptor* will live for the entire request,
// so no ref is necessary to keep it alive. The caller does *not* own a ref // so no ref is necessary to keep it alive. The caller does *not* own a ref
// on the returned object. // on the returned object.
Descriptor* Descriptor_GetFromClassEntry(zend_class_entry *ce); Descriptor* Descriptor_GetFromClassEntry(zend_class_entry* ce);
Descriptor* Descriptor_GetFromMessageDef(const upb_MessageDef *m); Descriptor* Descriptor_GetFromMessageDef(const upb_MessageDef* m);
Descriptor* Descriptor_GetFromFieldDef(const upb_FieldDef *f); Descriptor* Descriptor_GetFromFieldDef(const upb_FieldDef* f);
// Packages up a upb_CType with a Descriptor, since many functions need // Packages up a upb_CType with a Descriptor, since many functions need
// both. // both.
typedef struct { typedef struct {
upb_CType type; upb_CType type;
const Descriptor *desc; // When type == kUpb_CType_Message. const Descriptor* desc; // When type == kUpb_CType_Message.
} TypeInfo; } TypeInfo;
static inline TypeInfo TypeInfo_Get(const upb_FieldDef *f) { static inline TypeInfo TypeInfo_Get(const upb_FieldDef* f) {
TypeInfo ret = {upb_FieldDef_CType(f), Descriptor_GetFromFieldDef(f)}; TypeInfo ret = {upb_FieldDef_CType(f), Descriptor_GetFromFieldDef(f)};
return ret; return ret;
} }

@ -41,7 +41,7 @@
#include "php-upb.h" #include "php-upb.h"
#include "protobuf.h" #include "protobuf.h"
static void MapFieldIter_make(zval *val, zval *map_field); static void MapFieldIter_make(zval* val, zval* map_field);
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// MapField // MapField
@ -50,11 +50,11 @@ static void MapFieldIter_make(zval *val, zval *map_field);
typedef struct { typedef struct {
zend_object std; zend_object std;
zval arena; zval arena;
upb_Map *map; upb_Map* map;
MapField_Type type; MapField_Type type;
} MapField; } MapField;
zend_class_entry *MapField_class_entry; zend_class_entry* MapField_class_entry;
static zend_object_handlers MapField_object_handlers; static zend_object_handlers MapField_object_handlers;
static bool MapType_Eq(MapField_Type a, MapField_Type b) { static bool MapType_Eq(MapField_Type a, MapField_Type b) {
@ -66,10 +66,10 @@ static TypeInfo KeyType(MapField_Type type) {
return ret; return ret;
} }
MapField_Type MapType_Get(const upb_FieldDef *f) { MapField_Type MapType_Get(const upb_FieldDef* f) {
const upb_MessageDef *ent = upb_FieldDef_MessageSubDef(f); const upb_MessageDef* ent = upb_FieldDef_MessageSubDef(f);
const upb_FieldDef *key_f = upb_MessageDef_FindFieldByNumber(ent, 1); const upb_FieldDef* key_f = upb_MessageDef_FindFieldByNumber(ent, 1);
const upb_FieldDef *val_f = upb_MessageDef_FindFieldByNumber(ent, 2); const upb_FieldDef* val_f = upb_MessageDef_FindFieldByNumber(ent, 2);
MapField_Type type = { MapField_Type type = {
upb_FieldDef_CType(key_f), upb_FieldDef_CType(key_f),
{upb_FieldDef_CType(val_f), Descriptor_GetFromFieldDef(val_f)}}; {upb_FieldDef_CType(val_f), Descriptor_GetFromFieldDef(val_f)}};
@ -84,8 +84,8 @@ MapField_Type MapType_Get(const upb_FieldDef *f) {
* PHP class entry function to allocate and initialize a new MapField * PHP class entry function to allocate and initialize a new MapField
* object. * object.
*/ */
static zend_object* MapField_create(zend_class_entry *class_type) { static zend_object* MapField_create(zend_class_entry* class_type) {
MapField *intern = emalloc(sizeof(MapField)); MapField* intern = emalloc(sizeof(MapField));
zend_object_std_init(&intern->std, class_type); zend_object_std_init(&intern->std, class_type);
intern->std.handlers = &MapField_object_handlers; intern->std.handlers = &MapField_object_handlers;
Arena_Init(&intern->arena); Arena_Init(&intern->arena);
@ -116,7 +116,7 @@ static void MapField_destructor(zend_object* obj) {
* *
* $map1 == $map2 * $map1 == $map2
*/ */
static int MapField_compare_objects(zval *map1, zval *map2) { static int MapField_compare_objects(zval* map1, zval* map2) {
MapField* intern1 = (MapField*)Z_OBJ_P(map1); MapField* intern1 = (MapField*)Z_OBJ_P(map1);
MapField* intern2 = (MapField*)Z_OBJ_P(map2); MapField* intern2 = (MapField*)Z_OBJ_P(map2);
@ -133,10 +133,10 @@ static int MapField_compare_objects(zval *map1, zval *map2) {
* *
* $map2 = clone $map1; * $map2 = clone $map1;
*/ */
static zend_object *MapField_clone_obj(PROTO_VAL *object) { static zend_object* MapField_clone_obj(PROTO_VAL* object) {
MapField* intern = PROTO_VAL_P(object); MapField* intern = PROTO_VAL_P(object);
upb_Arena *arena = Arena_Get(&intern->arena); upb_Arena* arena = Arena_Get(&intern->arena);
upb_Map *clone = upb_Map* clone =
upb_Map_New(arena, intern->type.key_type, intern->type.val_type.type); upb_Map_New(arena, intern->type.key_type, intern->type.val_type.type);
size_t iter = kUpb_Map_Begin; size_t iter = kUpb_Map_Begin;
@ -151,12 +151,12 @@ static zend_object *MapField_clone_obj(PROTO_VAL *object) {
return Z_OBJ_P(&ret); return Z_OBJ_P(&ret);
} }
static zval *Map_GetPropertyPtrPtr(PROTO_VAL *object, PROTO_STR *member, static zval* Map_GetPropertyPtrPtr(PROTO_VAL* object, PROTO_STR* member,
int type, void **cache_slot) { int type, void** cache_slot) {
return NULL; // We don't offer direct references to our properties. return NULL; // We don't offer direct references to our properties.
} }
static HashTable *Map_GetProperties(PROTO_VAL *object) { static HashTable* Map_GetProperties(PROTO_VAL* object) {
return NULL; // We do not have a properties table. return NULL; // We do not have a properties table.
} }
@ -164,15 +164,15 @@ static HashTable *Map_GetProperties(PROTO_VAL *object) {
// These are documented in the header file. // These are documented in the header file.
void MapField_GetPhpWrapper(zval *val, upb_Map *map, MapField_Type type, void MapField_GetPhpWrapper(zval* val, upb_Map* map, MapField_Type type,
zval *arena) { zval* arena) {
if (!map) { if (!map) {
ZVAL_NULL(val); ZVAL_NULL(val);
return; return;
} }
if (!ObjCache_Get(map, val)) { if (!ObjCache_Get(map, val)) {
MapField *intern = emalloc(sizeof(MapField)); MapField* intern = emalloc(sizeof(MapField));
zend_object_std_init(&intern->std, MapField_class_entry); zend_object_std_init(&intern->std, MapField_class_entry);
intern->std.handlers = &MapField_object_handlers; intern->std.handlers = &MapField_object_handlers;
ZVAL_COPY(&intern->arena, arena); ZVAL_COPY(&intern->arena, arena);
@ -184,21 +184,21 @@ void MapField_GetPhpWrapper(zval *val, upb_Map *map, MapField_Type type,
} }
} }
upb_Map *MapField_GetUpbMap(zval *val, MapField_Type type, upb_Arena *arena) { upb_Map* MapField_GetUpbMap(zval* val, MapField_Type type, upb_Arena* arena) {
if (Z_ISREF_P(val)) { if (Z_ISREF_P(val)) {
ZVAL_DEREF(val); ZVAL_DEREF(val);
} }
if (Z_TYPE_P(val) == IS_ARRAY) { if (Z_TYPE_P(val) == IS_ARRAY) {
upb_Map *map = upb_Map_New(arena, type.key_type, type.val_type.type); upb_Map* map = upb_Map_New(arena, type.key_type, type.val_type.type);
HashTable *table = HASH_OF(val); HashTable* table = HASH_OF(val);
HashPosition pos; HashPosition pos;
zend_hash_internal_pointer_reset_ex(table, &pos); zend_hash_internal_pointer_reset_ex(table, &pos);
while (true) { while (true) {
zval php_key; zval php_key;
zval *php_val; zval* php_val;
upb_MessageValue upb_key; upb_MessageValue upb_key;
upb_MessageValue upb_val; upb_MessageValue upb_val;
@ -218,7 +218,7 @@ upb_Map *MapField_GetUpbMap(zval *val, MapField_Type type, upb_Arena *arena) {
} }
} else if (Z_TYPE_P(val) == IS_OBJECT && } else if (Z_TYPE_P(val) == IS_OBJECT &&
Z_OBJCE_P(val) == MapField_class_entry) { Z_OBJCE_P(val) == MapField_class_entry) {
MapField *intern = (MapField*)Z_OBJ_P(val); MapField* intern = (MapField*)Z_OBJ_P(val);
if (!MapType_Eq(intern->type, type)) { if (!MapType_Eq(intern->type, type)) {
php_error_docref(NULL, E_USER_ERROR, "Wrong type for this map field."); php_error_docref(NULL, E_USER_ERROR, "Wrong type for this map field.");
@ -233,7 +233,7 @@ upb_Map *MapField_GetUpbMap(zval *val, MapField_Type type, upb_Arena *arena) {
} }
} }
bool MapEq(const upb_Map *m1, const upb_Map *m2, MapField_Type type) { bool MapEq(const upb_Map* m1, const upb_Map* m2, MapField_Type type) {
size_t iter = kUpb_Map_Begin; size_t iter = kUpb_Map_Begin;
if ((m1 == NULL) != (m2 == NULL)) return false; if ((m1 == NULL) != (m2 == NULL)) return false;
@ -252,7 +252,6 @@ bool MapEq(const upb_Map *m1, const upb_Map *m2, MapField_Type type) {
return true; return true;
} }
// MapField PHP methods //////////////////////////////////////////////////////// // MapField PHP methods ////////////////////////////////////////////////////////
/** /**
@ -264,8 +263,8 @@ bool MapEq(const upb_Map *m1, const upb_Map *m2, MapField_Type type) {
* @param string Message/Enum class (message/enum value types only). * @param string Message/Enum class (message/enum value types only).
*/ */
PHP_METHOD(MapField, __construct) { PHP_METHOD(MapField, __construct) {
MapField *intern = (MapField*)Z_OBJ_P(getThis()); MapField* intern = (MapField*)Z_OBJ_P(getThis());
upb_Arena *arena = Arena_Get(&intern->arena); upb_Arena* arena = Arena_Get(&intern->arena);
zend_long key_type, val_type; zend_long key_type, val_type;
zend_class_entry* klass = NULL; zend_class_entry* klass = NULL;
@ -316,8 +315,8 @@ PHP_METHOD(MapField, __construct) {
* @return bool True if the element at the given index exists. * @return bool True if the element at the given index exists.
*/ */
PHP_METHOD(MapField, offsetExists) { PHP_METHOD(MapField, offsetExists) {
MapField *intern = (MapField*)Z_OBJ_P(getThis()); MapField* intern = (MapField*)Z_OBJ_P(getThis());
zval *key; zval* key;
upb_MessageValue upb_key; upb_MessageValue upb_key;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &key) != SUCCESS || if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &key) != SUCCESS ||
@ -341,8 +340,8 @@ PHP_METHOD(MapField, offsetExists) {
* @exception Non-existing index. * @exception Non-existing index.
*/ */
PHP_METHOD(MapField, offsetGet) { PHP_METHOD(MapField, offsetGet) {
MapField *intern = (MapField*)Z_OBJ_P(getThis()); MapField* intern = (MapField*)Z_OBJ_P(getThis());
zval *key; zval* key;
zval ret; zval ret;
upb_MessageValue upb_key, upb_val; upb_MessageValue upb_key, upb_val;
@ -374,8 +373,8 @@ PHP_METHOD(MapField, offsetGet) {
* @exception Incorrect type of the element. * @exception Incorrect type of the element.
*/ */
PHP_METHOD(MapField, offsetSet) { PHP_METHOD(MapField, offsetSet) {
MapField *intern = (MapField*)Z_OBJ_P(getThis()); MapField* intern = (MapField*)Z_OBJ_P(getThis());
upb_Arena *arena = Arena_Get(&intern->arena); upb_Arena* arena = Arena_Get(&intern->arena);
zval *key, *val; zval *key, *val;
upb_MessageValue upb_key, upb_val; upb_MessageValue upb_key, upb_val;
@ -400,8 +399,8 @@ PHP_METHOD(MapField, offsetSet) {
* @exception The element to be removed is not at the end of the MapField. * @exception The element to be removed is not at the end of the MapField.
*/ */
PHP_METHOD(MapField, offsetUnset) { PHP_METHOD(MapField, offsetUnset) {
MapField *intern = (MapField*)Z_OBJ_P(getThis()); MapField* intern = (MapField*)Z_OBJ_P(getThis());
zval *key; zval* key;
upb_MessageValue upb_key; upb_MessageValue upb_key;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &key) != SUCCESS || if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &key) != SUCCESS ||
@ -423,7 +422,7 @@ PHP_METHOD(MapField, offsetUnset) {
* @return long The number of stored elements. * @return long The number of stored elements.
*/ */
PHP_METHOD(MapField, count) { PHP_METHOD(MapField, count) {
MapField *intern = (MapField*)Z_OBJ_P(getThis()); MapField* intern = (MapField*)Z_OBJ_P(getThis());
if (zend_parse_parameters_none() == FAILURE) { if (zend_parse_parameters_none() == FAILURE) {
return; return;
@ -447,6 +446,7 @@ PHP_METHOD(MapField, getIterator) {
RETURN_COPY_VALUE(&ret); RETURN_COPY_VALUE(&ret);
} }
// clang-format off
ZEND_BEGIN_ARG_INFO_EX(arginfo_construct, 0, 0, 2) ZEND_BEGIN_ARG_INFO_EX(arginfo_construct, 0, 0, 2)
ZEND_ARG_INFO(0, key_type) ZEND_ARG_INFO(0, key_type)
ZEND_ARG_INFO(0, value_type) ZEND_ARG_INFO(0, value_type)
@ -487,6 +487,7 @@ static zend_function_entry MapField_methods[] = {
PHP_ME(MapField, getIterator, arginfo_getIterator, ZEND_ACC_PUBLIC) PHP_ME(MapField, getIterator, arginfo_getIterator, ZEND_ACC_PUBLIC)
ZEND_FE_END ZEND_FE_END
}; };
// clang-format on
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// MapFieldIter // MapFieldIter
@ -498,7 +499,7 @@ typedef struct {
size_t position; size_t position;
} MapFieldIter; } MapFieldIter;
zend_class_entry *MapFieldIter_class_entry; zend_class_entry* MapFieldIter_class_entry;
static zend_object_handlers MapFieldIter_object_handlers; static zend_object_handlers MapFieldIter_object_handlers;
/** /**
@ -507,8 +508,8 @@ static zend_object_handlers MapFieldIter_object_handlers;
* PHP class entry function to allocate and initialize a new MapFieldIter * PHP class entry function to allocate and initialize a new MapFieldIter
* object. * object.
*/ */
zend_object* MapFieldIter_create(zend_class_entry *class_type) { zend_object* MapFieldIter_create(zend_class_entry* class_type) {
MapFieldIter *intern = emalloc(sizeof(MapFieldIter)); MapFieldIter* intern = emalloc(sizeof(MapFieldIter));
zend_object_std_init(&intern->std, class_type); zend_object_std_init(&intern->std, class_type);
intern->std.handlers = &MapFieldIter_object_handlers; intern->std.handlers = &MapFieldIter_object_handlers;
ZVAL_NULL(&intern->map_field); ZVAL_NULL(&intern->map_field);
@ -535,8 +536,8 @@ static void map_field_iter_dtor(zend_object* obj) {
* *
* Function to create a MapFieldIter directly from C. * Function to create a MapFieldIter directly from C.
*/ */
static void MapFieldIter_make(zval *val, zval *map_field) { static void MapFieldIter_make(zval* val, zval* map_field) {
MapFieldIter *iter; MapFieldIter* iter;
ZVAL_OBJ(val, ZVAL_OBJ(val,
MapFieldIter_class_entry->create_object(MapFieldIter_class_entry)); MapFieldIter_class_entry->create_object(MapFieldIter_class_entry));
iter = (MapFieldIter*)Z_OBJ_P(val); iter = (MapFieldIter*)Z_OBJ_P(val);
@ -567,8 +568,8 @@ static void MapFieldIter_make(zval *val, zval *map_field) {
* Implements the Iterator interface. Sets the iterator to the first element. * Implements the Iterator interface. Sets the iterator to the first element.
*/ */
PHP_METHOD(MapFieldIter, rewind) { PHP_METHOD(MapFieldIter, rewind) {
MapFieldIter *intern = (MapFieldIter*)Z_OBJ_P(getThis()); MapFieldIter* intern = (MapFieldIter*)Z_OBJ_P(getThis());
MapField *map_field = (MapField*)Z_OBJ_P(&intern->map_field); MapField* map_field = (MapField*)Z_OBJ_P(&intern->map_field);
intern->position = kUpb_Map_Begin; intern->position = kUpb_Map_Begin;
upb_MapIterator_Next(map_field->map, &intern->position); upb_MapIterator_Next(map_field->map, &intern->position);
} }
@ -579,9 +580,10 @@ PHP_METHOD(MapFieldIter, rewind) {
* Implements the Iterator interface. Returns the current value. * Implements the Iterator interface. Returns the current value.
*/ */
PHP_METHOD(MapFieldIter, current) { PHP_METHOD(MapFieldIter, current) {
MapFieldIter *intern = (MapFieldIter*)Z_OBJ_P(getThis()); MapFieldIter* intern = (MapFieldIter*)Z_OBJ_P(getThis());
MapField *field = (MapField*)Z_OBJ_P(&intern->map_field); MapField* field = (MapField*)Z_OBJ_P(&intern->map_field);
upb_MessageValue upb_val = upb_MapIterator_Value(field->map, intern->position); upb_MessageValue upb_val =
upb_MapIterator_Value(field->map, intern->position);
zval ret; zval ret;
Convert_UpbToPhp(upb_val, &ret, field->type.val_type, &field->arena); Convert_UpbToPhp(upb_val, &ret, field->type.val_type, &field->arena);
RETURN_COPY_VALUE(&ret); RETURN_COPY_VALUE(&ret);
@ -593,8 +595,8 @@ PHP_METHOD(MapFieldIter, current) {
* Implements the Iterator interface. Returns the current key. * Implements the Iterator interface. Returns the current key.
*/ */
PHP_METHOD(MapFieldIter, key) { PHP_METHOD(MapFieldIter, key) {
MapFieldIter *intern = (MapFieldIter*)Z_OBJ_P(getThis()); MapFieldIter* intern = (MapFieldIter*)Z_OBJ_P(getThis());
MapField *field = (MapField*)Z_OBJ_P(&intern->map_field); MapField* field = (MapField*)Z_OBJ_P(&intern->map_field);
upb_MessageValue upb_key = upb_MapIterator_Key(field->map, intern->position); upb_MessageValue upb_key = upb_MapIterator_Key(field->map, intern->position);
zval ret; zval ret;
Convert_UpbToPhp(upb_key, &ret, KeyType(field->type), NULL); Convert_UpbToPhp(upb_key, &ret, KeyType(field->type), NULL);
@ -607,8 +609,8 @@ PHP_METHOD(MapFieldIter, key) {
* Implements the Iterator interface. Advances to the next element. * Implements the Iterator interface. Advances to the next element.
*/ */
PHP_METHOD(MapFieldIter, next) { PHP_METHOD(MapFieldIter, next) {
MapFieldIter *intern = (MapFieldIter*)Z_OBJ_P(getThis()); MapFieldIter* intern = (MapFieldIter*)Z_OBJ_P(getThis());
MapField *field = (MapField*)Z_OBJ_P(&intern->map_field); MapField* field = (MapField*)Z_OBJ_P(&intern->map_field);
upb_MapIterator_Next(field->map, &intern->position); upb_MapIterator_Next(field->map, &intern->position);
} }
@ -618,12 +620,13 @@ PHP_METHOD(MapFieldIter, next) {
* Implements the Iterator interface. Returns true if this is a valid element. * Implements the Iterator interface. Returns true if this is a valid element.
*/ */
PHP_METHOD(MapFieldIter, valid) { PHP_METHOD(MapFieldIter, valid) {
MapFieldIter *intern = (MapFieldIter*)Z_OBJ_P(getThis()); MapFieldIter* intern = (MapFieldIter*)Z_OBJ_P(getThis());
MapField *field = (MapField*)Z_OBJ_P(&intern->map_field); MapField* field = (MapField*)Z_OBJ_P(&intern->map_field);
bool done = upb_MapIterator_Done(field->map, intern->position); bool done = upb_MapIterator_Done(field->map, intern->position);
RETURN_BOOL(!done); RETURN_BOOL(!done);
} }
// clang-format off
PROTOBUF_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_rewind, 0, 0, IS_VOID, 0) PROTOBUF_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_rewind, 0, 0, IS_VOID, 0)
ZEND_END_ARG_INFO() ZEND_END_ARG_INFO()
@ -647,6 +650,7 @@ static zend_function_entry map_field_iter_methods[] = {
PHP_ME(MapFieldIter, valid, arginfo_valid, ZEND_ACC_PUBLIC) PHP_ME(MapFieldIter, valid, arginfo_valid, ZEND_ACC_PUBLIC)
ZEND_FE_END ZEND_FE_END
}; };
// clang-format on
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Module init. // Module init.
@ -660,7 +664,7 @@ static zend_function_entry map_field_iter_methods[] = {
void Map_ModuleInit() { void Map_ModuleInit() {
zend_class_entry tmp_ce; zend_class_entry tmp_ce;
zend_object_handlers *h; zend_object_handlers* h;
INIT_CLASS_ENTRY(tmp_ce, "Google\\Protobuf\\Internal\\MapField", INIT_CLASS_ENTRY(tmp_ce, "Google\\Protobuf\\Internal\\MapField",
MapField_methods); MapField_methods);

@ -43,7 +43,7 @@ typedef struct {
TypeInfo val_type; TypeInfo val_type;
} MapField_Type; } MapField_Type;
MapField_Type MapType_Get(const upb_FieldDef *f); MapField_Type MapType_Get(const upb_FieldDef* f);
// Gets a upb_Map* for the PHP object |val|: // Gets a upb_Map* for the PHP object |val|:
// * If |val| is a RepeatedField object, we first check its type and verify // * If |val| is a RepeatedField object, we first check its type and verify
@ -55,16 +55,16 @@ MapField_Type MapType_Get(const upb_FieldDef *f);
// |arena| and add all of the PHP elements to it. // |arena| and add all of the PHP elements to it.
// //
// If an error occurs, we raise a PHP error and return NULL. // If an error occurs, we raise a PHP error and return NULL.
upb_Map *MapField_GetUpbMap(zval *val, MapField_Type type, upb_Arena *arena); upb_Map* MapField_GetUpbMap(zval* val, MapField_Type type, upb_Arena* arena);
// Creates a PHP MapField object for the given upb_Map* and |f| and returns it // Creates a PHP MapField object for the given upb_Map* and |f| and returns it
// in |val|. The PHP object will keep a reference to this |arena| to ensure the // in |val|. The PHP object will keep a reference to this |arena| to ensure the
// underlying array data stays alive. // underlying array data stays alive.
// //
// If |map| is NULL, this will return a PHP null object. // If |map| is NULL, this will return a PHP null object.
void MapField_GetPhpWrapper(zval *val, upb_Map *arr, MapField_Type type, void MapField_GetPhpWrapper(zval* val, upb_Map* arr, MapField_Type type,
zval *arena); zval* arena);
bool MapEq(const upb_Map *m1, const upb_Map *m2, MapField_Type type); bool MapEq(const upb_Map* m1, const upb_Map* m2, MapField_Type type);
#endif // PHP_PROTOBUF_MAP_H_ #endif // PHP_PROTOBUF_MAP_H_

@ -54,13 +54,13 @@ typedef struct {
zend_object std; zend_object std;
zval arena; zval arena;
const Descriptor* desc; const Descriptor* desc;
upb_Message *msg; upb_Message* msg;
} Message; } Message;
zend_class_entry *message_ce; zend_class_entry* message_ce;
static zend_object_handlers message_object_handlers; static zend_object_handlers message_object_handlers;
static void Message_SuppressDefaultProperties(zend_class_entry *class_type) { static void Message_SuppressDefaultProperties(zend_class_entry* class_type) {
// We suppress all default properties, because all our properties are handled // We suppress all default properties, because all our properties are handled
// by our read_property handler. // by our read_property handler.
// //
@ -82,8 +82,8 @@ static void Message_SuppressDefaultProperties(zend_class_entry *class_type) {
* *
* PHP class entry function to allocate and initialize a new Message object. * PHP class entry function to allocate and initialize a new Message object.
*/ */
static zend_object* Message_create(zend_class_entry *class_type) { static zend_object* Message_create(zend_class_entry* class_type) {
Message *intern = emalloc(sizeof(Message)); Message* intern = emalloc(sizeof(Message));
Message_SuppressDefaultProperties(class_type); Message_SuppressDefaultProperties(class_type);
zend_object_std_init(&intern->std, class_type); zend_object_std_init(&intern->std, class_type);
intern->std.handlers = &message_object_handlers; intern->std.handlers = &message_object_handlers;
@ -110,10 +110,10 @@ static void Message_dtor(zend_object* obj) {
* *
* Helper function to look up a field given a member name (as a string). * Helper function to look up a field given a member name (as a string).
*/ */
static const upb_FieldDef *get_field(Message *msg, PROTO_STR *member) { static const upb_FieldDef* get_field(Message* msg, PROTO_STR* member) {
const upb_MessageDef *m = msg->desc->msgdef; const upb_MessageDef* m = msg->desc->msgdef;
const upb_FieldDef *f = const upb_FieldDef* f = upb_MessageDef_FindFieldByNameWithSize(
upb_MessageDef_FindFieldByNameWithSize(m, PROTO_STRVAL_P(member), PROTO_STRLEN_P(member)); m, PROTO_STRVAL_P(member), PROTO_STRLEN_P(member));
if (!f) { if (!f) {
zend_throw_exception_ex(NULL, 0, "No such property %s.", zend_throw_exception_ex(NULL, 0, "No such property %s.",
@ -142,8 +142,8 @@ static bool IsWrapper(const upb_MessageDef* m) {
} }
} }
static void Message_get(Message *intern, const upb_FieldDef *f, zval *rv) { static void Message_get(Message* intern, const upb_FieldDef* f, zval* rv) {
upb_Arena *arena = Arena_Get(&intern->arena); upb_Arena* arena = Arena_Get(&intern->arena);
if (upb_FieldDef_IsMap(f)) { if (upb_FieldDef_IsMap(f)) {
upb_MutableMessageValue msgval = upb_Message_Mutable(intern->msg, f, arena); upb_MutableMessageValue msgval = upb_Message_Mutable(intern->msg, f, arena);
@ -163,8 +163,8 @@ static void Message_get(Message *intern, const upb_FieldDef *f, zval *rv) {
} }
} }
static bool Message_set(Message *intern, const upb_FieldDef *f, zval *val) { static bool Message_set(Message* intern, const upb_FieldDef* f, zval* val) {
upb_Arena *arena = Arena_Get(&intern->arena); upb_Arena* arena = Arena_Get(&intern->arena);
upb_MessageValue msgval; upb_MessageValue msgval;
if (upb_FieldDef_IsMap(f)) { if (upb_FieldDef_IsMap(f)) {
@ -184,7 +184,8 @@ static bool Message_set(Message *intern, const upb_FieldDef *f, zval *val) {
return true; return true;
} }
static bool MessageEq(const upb_Message *m1, const upb_Message *m2, const upb_MessageDef *m); static bool MessageEq(const upb_Message* m1, const upb_Message* m2,
const upb_MessageDef* m);
/** /**
* ValueEq() * ValueEq()
@ -207,7 +208,8 @@ bool ValueEq(upb_MessageValue val1, upb_MessageValue val2, TypeInfo type) {
case kUpb_CType_String: case kUpb_CType_String:
case kUpb_CType_Bytes: case kUpb_CType_Bytes:
return val1.str_val.size == val2.str_val.size && return val1.str_val.size == val2.str_val.size &&
memcmp(val1.str_val.data, val2.str_val.data, val1.str_val.size) == 0; memcmp(val1.str_val.data, val2.str_val.data, val1.str_val.size) ==
0;
case kUpb_CType_Message: case kUpb_CType_Message:
return MessageEq(val1.msg_val, val2.msg_val, type.desc->msgdef); return MessageEq(val1.msg_val, val2.msg_val, type.desc->msgdef);
default: default:
@ -218,11 +220,12 @@ bool ValueEq(upb_MessageValue val1, upb_MessageValue val2, TypeInfo type) {
/** /**
* MessageEq() * MessageEq()
*/ */
static bool MessageEq(const upb_Message *m1, const upb_Message *m2, const upb_MessageDef *m) { static bool MessageEq(const upb_Message* m1, const upb_Message* m2,
const upb_MessageDef* m) {
int n = upb_MessageDef_FieldCount(m); int n = upb_MessageDef_FieldCount(m);
for(int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
const upb_FieldDef *f = upb_MessageDef_Field(m, i); const upb_FieldDef* f = upb_MessageDef_Field(m, i);
if (upb_FieldDef_HasPresence(f)) { if (upb_FieldDef_HasPresence(f)) {
if (upb_Message_HasFieldByDef(m1, f) != if (upb_Message_HasFieldByDef(m1, f) !=
@ -238,7 +241,8 @@ static bool MessageEq(const upb_Message *m1, const upb_Message *m2, const upb_Me
if (upb_FieldDef_IsMap(f)) { if (upb_FieldDef_IsMap(f)) {
if (!MapEq(val1.map_val, val2.map_val, MapType_Get(f))) return false; if (!MapEq(val1.map_val, val2.map_val, MapType_Get(f))) return false;
} else if (upb_FieldDef_IsRepeated(f)) { } else if (upb_FieldDef_IsRepeated(f)) {
if (!ArrayEq(val1.array_val, val2.array_val, TypeInfo_Get(f))) return false; if (!ArrayEq(val1.array_val, val2.array_val, TypeInfo_Get(f)))
return false;
} else { } else {
if (!ValueEq(val1, val2, TypeInfo_Get(f))) return false; if (!ValueEq(val1, val2, TypeInfo_Get(f))) return false;
} }
@ -255,10 +259,10 @@ static bool MessageEq(const upb_Message *m1, const upb_Message *m2, const upb_Me
* *
* $m1 == $m2 * $m1 == $m2
*/ */
static int Message_compare_objects(zval *m1, zval *m2) { static int Message_compare_objects(zval* m1, zval* m2) {
Message* intern1 = (Message*)Z_OBJ_P(m1); Message* intern1 = (Message*)Z_OBJ_P(m1);
Message* intern2 = (Message*)Z_OBJ_P(m2); Message* intern2 = (Message*)Z_OBJ_P(m2);
const upb_MessageDef *m = intern1->desc->msgdef; const upb_MessageDef* m = intern1->desc->msgdef;
if (intern2->desc->msgdef != m) return 1; if (intern2->desc->msgdef != m) return 1;
@ -283,11 +287,10 @@ static int Message_compare_objects(zval *m1, zval *m2) {
* return isset($this->optional_int32); * return isset($this->optional_int32);
* } * }
*/ */
static int Message_has_property(PROTO_VAL *obj, PROTO_STR *member, static int Message_has_property(PROTO_VAL* obj, PROTO_STR* member,
int has_set_exists, int has_set_exists, void** cache_slot) {
void **cache_slot) {
Message* intern = PROTO_VAL_P(obj); Message* intern = PROTO_VAL_P(obj);
const upb_FieldDef *f = get_field(intern, member); const upb_FieldDef* f = get_field(intern, member);
if (!f) return 0; if (!f) return 0;
@ -318,10 +321,10 @@ static int Message_has_property(PROTO_VAL *obj, PROTO_STR *member,
* unset($this->optional_int32); * unset($this->optional_int32);
* } * }
*/ */
static void Message_unset_property(PROTO_VAL *obj, PROTO_STR *member, static void Message_unset_property(PROTO_VAL* obj, PROTO_STR* member,
void **cache_slot) { void** cache_slot) {
Message* intern = PROTO_VAL_P(obj); Message* intern = PROTO_VAL_P(obj);
const upb_FieldDef *f = get_field(intern, member); const upb_FieldDef* f = get_field(intern, member);
if (!f) return; if (!f) return;
@ -336,7 +339,6 @@ static void Message_unset_property(PROTO_VAL *obj, PROTO_STR *member,
upb_Message_ClearFieldByDef(intern->msg, f); upb_Message_ClearFieldByDef(intern->msg, f);
} }
/** /**
* Message_read_property() * Message_read_property()
* *
@ -355,10 +357,10 @@ static void Message_unset_property(PROTO_VAL *obj, PROTO_STR *member,
* We lookup the field and return the scalar, RepeatedField, or MapField for * We lookup the field and return the scalar, RepeatedField, or MapField for
* this field. * this field.
*/ */
static zval *Message_read_property(PROTO_VAL *obj, PROTO_STR *member, static zval* Message_read_property(PROTO_VAL* obj, PROTO_STR* member, int type,
int type, void **cache_slot, zval *rv) { void** cache_slot, zval* rv) {
Message* intern = PROTO_VAL_P(obj); Message* intern = PROTO_VAL_P(obj);
const upb_FieldDef *f = get_field(intern, member); const upb_FieldDef* f = get_field(intern, member);
if (!f) return &EG(uninitialized_zval); if (!f) return &EG(uninitialized_zval);
Message_get(intern, f, rv); Message_get(intern, f, rv);
@ -386,10 +388,11 @@ static zval *Message_read_property(PROTO_VAL *obj, PROTO_STR *member,
* The C extension version of checkInt32() doesn't actually check anything, so * The C extension version of checkInt32() doesn't actually check anything, so
* we perform all checking and conversion in this function. * we perform all checking and conversion in this function.
*/ */
static PROTO_RETURN_VAL Message_write_property( static PROTO_RETURN_VAL Message_write_property(PROTO_VAL* obj,
PROTO_VAL *obj, PROTO_STR *member, zval *val, void **cache_slot) { PROTO_STR* member, zval* val,
void** cache_slot) {
Message* intern = PROTO_VAL_P(obj); Message* intern = PROTO_VAL_P(obj);
const upb_FieldDef *f = get_field(intern, member); const upb_FieldDef* f = get_field(intern, member);
if (f && Message_set(intern, f, val)) { if (f && Message_set(intern, f, val)) {
#if PHP_VERSION_ID < 70400 #if PHP_VERSION_ID < 70400
@ -413,9 +416,8 @@ static PROTO_RETURN_VAL Message_write_property(
* reference to our internal properties. We don't support this, so we return * reference to our internal properties. We don't support this, so we return
* NULL. * NULL.
*/ */
static zval *Message_get_property_ptr_ptr(PROTO_VAL *object, PROTO_STR *member, static zval* Message_get_property_ptr_ptr(PROTO_VAL* object, PROTO_STR* member,
int type, int type, void** cache_slot) {
void **cache_slot) {
return NULL; // We do not have a properties table. return NULL; // We do not have a properties table.
} }
@ -426,10 +428,10 @@ static zval *Message_get_property_ptr_ptr(PROTO_VAL *object, PROTO_STR *member,
* *
* $msg2 = clone $msg; * $msg2 = clone $msg;
*/ */
static zend_object *Message_clone_obj(PROTO_VAL *object) { static zend_object* Message_clone_obj(PROTO_VAL* object) {
Message* intern = PROTO_VAL_P(object); Message* intern = PROTO_VAL_P(object);
upb_MiniTable *t = upb_MessageDef_MiniTable(intern->desc->msgdef); upb_MiniTable* t = upb_MessageDef_MiniTable(intern->desc->msgdef);
upb_Message *clone = upb_Message_New(t, Arena_Get(&intern->arena)); upb_Message* clone = upb_Message_New(t, Arena_Get(&intern->arena));
// TODO: copy unknown fields? // TODO: copy unknown fields?
// TODO: use official upb msg copy function // TODO: use official upb msg copy function
@ -445,7 +447,7 @@ static zend_object *Message_clone_obj(PROTO_VAL *object) {
* Object handler for the get_properties event in PHP. This returns a HashTable * Object handler for the get_properties event in PHP. This returns a HashTable
* of our internal properties. We don't support this, so we return NULL. * of our internal properties. We don't support this, so we return NULL.
*/ */
static HashTable *Message_get_properties(PROTO_VAL *object) { static HashTable* Message_get_properties(PROTO_VAL* object) {
return NULL; // We don't offer direct references to our properties. return NULL; // We don't offer direct references to our properties.
} }
@ -453,15 +455,15 @@ static HashTable *Message_get_properties(PROTO_VAL *object) {
// These are documented in the header file. // These are documented in the header file.
void Message_GetPhpWrapper(zval *val, const Descriptor *desc, upb_Message *msg, void Message_GetPhpWrapper(zval* val, const Descriptor* desc, upb_Message* msg,
zval *arena) { zval* arena) {
if (!msg) { if (!msg) {
ZVAL_NULL(val); ZVAL_NULL(val);
return; return;
} }
if (!ObjCache_Get(msg, val)) { if (!ObjCache_Get(msg, val)) {
Message *intern = emalloc(sizeof(Message)); Message* intern = emalloc(sizeof(Message));
Message_SuppressDefaultProperties(desc->class_entry); Message_SuppressDefaultProperties(desc->class_entry);
zend_object_std_init(&intern->std, desc->class_entry); zend_object_std_init(&intern->std, desc->class_entry);
intern->std.handlers = &message_object_handlers; intern->std.handlers = &message_object_handlers;
@ -473,8 +475,8 @@ void Message_GetPhpWrapper(zval *val, const Descriptor *desc, upb_Message *msg,
} }
} }
bool Message_GetUpbMessage(zval *val, const Descriptor *desc, upb_Arena *arena, bool Message_GetUpbMessage(zval* val, const Descriptor* desc, upb_Arena* arena,
upb_Message **msg) { upb_Message** msg) {
PBPHP_ASSERT(desc); PBPHP_ASSERT(desc);
if (Z_ISREF_P(val)) { if (Z_ISREF_P(val)) {
@ -483,7 +485,7 @@ bool Message_GetUpbMessage(zval *val, const Descriptor *desc, upb_Arena *arena,
if (Z_TYPE_P(val) == IS_OBJECT && if (Z_TYPE_P(val) == IS_OBJECT &&
instanceof_function(Z_OBJCE_P(val), desc->class_entry)) { instanceof_function(Z_OBJCE_P(val), desc->class_entry)) {
Message *intern = (Message*)Z_OBJ_P(val); Message* intern = (Message*)Z_OBJ_P(val);
upb_Arena_Fuse(arena, Arena_Get(&intern->arena)); upb_Arena_Fuse(arena, Arena_Get(&intern->arena));
*msg = intern->msg; *msg = intern->msg;
return true; return true;
@ -521,8 +523,8 @@ bool Message_GetUpbMessage(zval *val, const Descriptor *desc, upb_Arena *arena,
* *
* The initializer must be an array. * The initializer must be an array.
*/ */
bool Message_InitFromPhp(upb_Message *msg, const upb_MessageDef *m, zval *init, bool Message_InitFromPhp(upb_Message* msg, const upb_MessageDef* m, zval* init,
upb_Arena *arena) { upb_Arena* arena) {
HashTable* table = HASH_OF(init); HashTable* table = HASH_OF(init);
HashPosition pos; HashPosition pos;
@ -541,8 +543,8 @@ bool Message_InitFromPhp(upb_Message *msg, const upb_MessageDef *m, zval *init,
while (true) { // Iterate over key/value pairs. while (true) { // Iterate over key/value pairs.
zval key; zval key;
zval *val; zval* val;
const upb_FieldDef *f; const upb_FieldDef* f;
upb_MessageValue msgval; upb_MessageValue msgval;
zend_hash_get_current_key_zval_ex(table, &key, &pos); zend_hash_get_current_key_zval_ex(table, &key, &pos);
@ -554,11 +556,11 @@ bool Message_InitFromPhp(upb_Message *msg, const upb_MessageDef *m, zval *init,
ZVAL_DEREF(val); ZVAL_DEREF(val);
} }
f = upb_MessageDef_FindFieldByNameWithSize(m, Z_STRVAL_P(&key), Z_STRLEN_P(&key)); f = upb_MessageDef_FindFieldByNameWithSize(m, Z_STRVAL_P(&key),
Z_STRLEN_P(&key));
if (!f) { if (!f) {
zend_throw_exception_ex(NULL, 0, zend_throw_exception_ex(NULL, 0, "No such field %s", Z_STRVAL_P(&key));
"No such field %s", Z_STRVAL_P(&key));
return false; return false;
} }
@ -580,9 +582,9 @@ bool Message_InitFromPhp(upb_Message *msg, const upb_MessageDef *m, zval *init,
} }
} }
static void Message_Initialize(Message *intern, const Descriptor *desc) { static void Message_Initialize(Message* intern, const Descriptor* desc) {
intern->desc = desc; intern->desc = desc;
upb_MiniTable *t = upb_MessageDef_MiniTable(desc->msgdef); upb_MiniTable* t = upb_MessageDef_MiniTable(desc->msgdef);
intern->msg = upb_Message_New(t, Arena_Get(&intern->arena)); intern->msg = upb_Message_New(t, Arena_Get(&intern->arena));
ObjCache_Add(intern->msg, &intern->std); ObjCache_Add(intern->msg, &intern->std);
} }
@ -596,9 +598,9 @@ static void Message_Initialize(Message *intern, const Descriptor *desc) {
PHP_METHOD(Message, __construct) { PHP_METHOD(Message, __construct) {
Message* intern = (Message*)Z_OBJ_P(getThis()); Message* intern = (Message*)Z_OBJ_P(getThis());
const Descriptor* desc; const Descriptor* desc;
zend_class_entry *ce = Z_OBJCE_P(getThis()); zend_class_entry* ce = Z_OBJCE_P(getThis());
upb_Arena *arena = Arena_Get(&intern->arena); upb_Arena* arena = Arena_Get(&intern->arena);
zval *init_arr = NULL; zval* init_arr = NULL;
// This descriptor should always be available, as the generated __construct // This descriptor should always be available, as the generated __construct
// method calls initOnce() to load the descriptor prior to calling us. // method calls initOnce() to load the descriptor prior to calling us.
@ -678,10 +680,10 @@ static bool Message_checkEncodeStatus(upb_EncodeStatus status) {
PHP_METHOD(Message, mergeFrom) { PHP_METHOD(Message, mergeFrom) {
Message* intern = (Message*)Z_OBJ_P(getThis()); Message* intern = (Message*)Z_OBJ_P(getThis());
Message* from; Message* from;
upb_Arena *arena = Arena_Get(&intern->arena); upb_Arena* arena = Arena_Get(&intern->arena);
const upb_MiniTable *l = upb_MessageDef_MiniTable(intern->desc->msgdef); const upb_MiniTable* l = upb_MessageDef_MiniTable(intern->desc->msgdef);
zval* value; zval* value;
char *pb; char* pb;
size_t size; size_t size;
bool ok; bool ok;
@ -713,11 +715,11 @@ PHP_METHOD(Message, mergeFrom) {
*/ */
PHP_METHOD(Message, mergeFromString) { PHP_METHOD(Message, mergeFromString) {
Message* intern = (Message*)Z_OBJ_P(getThis()); Message* intern = (Message*)Z_OBJ_P(getThis());
char *data = NULL; char* data = NULL;
char *data_copy = NULL; char* data_copy = NULL;
zend_long data_len; zend_long data_len;
const upb_MiniTable *l = upb_MessageDef_MiniTable(intern->desc->msgdef); const upb_MiniTable* l = upb_MessageDef_MiniTable(intern->desc->msgdef);
upb_Arena *arena = Arena_Get(&intern->arena); upb_Arena* arena = Arena_Get(&intern->arena);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &data, &data_len) == if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &data, &data_len) ==
FAILURE) { FAILURE) {
@ -743,9 +745,9 @@ PHP_METHOD(Message, mergeFromString) {
*/ */
PHP_METHOD(Message, serializeToString) { PHP_METHOD(Message, serializeToString) {
Message* intern = (Message*)Z_OBJ_P(getThis()); Message* intern = (Message*)Z_OBJ_P(getThis());
const upb_MiniTable *l = upb_MessageDef_MiniTable(intern->desc->msgdef); const upb_MiniTable* l = upb_MessageDef_MiniTable(intern->desc->msgdef);
upb_Arena *tmp_arena = upb_Arena_New(); upb_Arena* tmp_arena = upb_Arena_New();
char *data; char* data;
size_t size; size_t size;
upb_EncodeStatus status = upb_EncodeStatus status =
@ -770,10 +772,10 @@ PHP_METHOD(Message, serializeToString) {
*/ */
PHP_METHOD(Message, mergeFromJsonString) { PHP_METHOD(Message, mergeFromJsonString) {
Message* intern = (Message*)Z_OBJ_P(getThis()); Message* intern = (Message*)Z_OBJ_P(getThis());
char *data = NULL; char* data = NULL;
char *data_copy = NULL; char* data_copy = NULL;
zend_long data_len; zend_long data_len;
upb_Arena *arena = Arena_Get(&intern->arena); upb_Arena* arena = Arena_Get(&intern->arena);
upb_Status status; upb_Status status;
zend_bool ignore_json_unknown = false; zend_bool ignore_json_unknown = false;
int options = 0; int options = 0;
@ -794,8 +796,8 @@ PHP_METHOD(Message, mergeFromJsonString) {
upb_Status_Clear(&status); upb_Status_Clear(&status);
if (!upb_JsonDecode(data_copy, data_len, intern->msg, intern->desc->msgdef, if (!upb_JsonDecode(data_copy, data_len, intern->msg, intern->desc->msgdef,
DescriptorPool_GetSymbolTable(), options, arena, DescriptorPool_GetSymbolTable(), options, arena,
&status)) { &status)) {
zend_throw_exception_ex(NULL, 0, "Error occurred during parsing: %s", zend_throw_exception_ex(NULL, 0, "Error occurred during parsing: %s",
upb_Status_ErrorMessage(&status)); upb_Status_ErrorMessage(&status));
return; return;
@ -827,8 +829,8 @@ PHP_METHOD(Message, serializeToJsonString) {
upb_Status_Clear(&status); upb_Status_Clear(&status);
size = upb_JsonEncode(intern->msg, intern->desc->msgdef, size = upb_JsonEncode(intern->msg, intern->desc->msgdef,
DescriptorPool_GetSymbolTable(), options, buf, DescriptorPool_GetSymbolTable(), options, buf,
sizeof(buf), &status); sizeof(buf), &status);
if (!upb_Status_IsOk(&status)) { if (!upb_Status_IsOk(&status)) {
zend_throw_exception_ex(NULL, 0, zend_throw_exception_ex(NULL, 0,
@ -838,10 +840,10 @@ PHP_METHOD(Message, serializeToJsonString) {
} }
if (size >= sizeof(buf)) { if (size >= sizeof(buf)) {
char *buf2 = malloc(size + 1); char* buf2 = malloc(size + 1);
upb_JsonEncode(intern->msg, intern->desc->msgdef, upb_JsonEncode(intern->msg, intern->desc->msgdef,
DescriptorPool_GetSymbolTable(), options, buf2, size + 1, DescriptorPool_GetSymbolTable(), options, buf2, size + 1,
&status); &status);
RETVAL_STRINGL(buf2, size); RETVAL_STRINGL(buf2, size);
free(buf2); free(buf2);
} else { } else {
@ -865,26 +867,28 @@ PHP_METHOD(Message, serializeToJsonString) {
PHP_METHOD(Message, readWrapperValue) { PHP_METHOD(Message, readWrapperValue) {
Message* intern = (Message*)Z_OBJ_P(getThis()); Message* intern = (Message*)Z_OBJ_P(getThis());
char* member; char* member;
const upb_FieldDef *f; const upb_FieldDef* f;
zend_long size; zend_long size;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &member, &size) == FAILURE) { if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &member, &size) == FAILURE) {
return; return;
} }
f = upb_MessageDef_FindFieldByNameWithSize(intern->desc->msgdef, member, size); f = upb_MessageDef_FindFieldByNameWithSize(intern->desc->msgdef, member,
size);
if (!f || !IsWrapper(upb_FieldDef_MessageSubDef(f))) { if (!f || !IsWrapper(upb_FieldDef_MessageSubDef(f))) {
zend_throw_exception_ex(NULL, 0, "Message %s has no field %s", zend_throw_exception_ex(NULL, 0, "Message %s has no field %s",
upb_MessageDef_FullName(intern->desc->msgdef), member); upb_MessageDef_FullName(intern->desc->msgdef),
member);
return; return;
} }
if (upb_Message_HasFieldByDef(intern->msg, f)) { if (upb_Message_HasFieldByDef(intern->msg, f)) {
const upb_Message *wrapper = const upb_Message* wrapper =
upb_Message_GetFieldByDef(intern->msg, f).msg_val; upb_Message_GetFieldByDef(intern->msg, f).msg_val;
const upb_MessageDef *m = upb_FieldDef_MessageSubDef(f); const upb_MessageDef* m = upb_FieldDef_MessageSubDef(f);
const upb_FieldDef *val_f = upb_MessageDef_FindFieldByNumber(m, 1); const upb_FieldDef* val_f = upb_MessageDef_FindFieldByNumber(m, 1);
upb_MessageValue msgval = upb_Message_GetFieldByDef(wrapper, val_f); upb_MessageValue msgval = upb_Message_GetFieldByDef(wrapper, val_f);
zval ret; zval ret;
Convert_UpbToPhp(msgval, &ret, TypeInfo_Get(val_f), &intern->arena); Convert_UpbToPhp(msgval, &ret, TypeInfo_Get(val_f), &intern->arena);
@ -911,9 +915,9 @@ PHP_METHOD(Message, readWrapperValue) {
*/ */
PHP_METHOD(Message, writeWrapperValue) { PHP_METHOD(Message, writeWrapperValue) {
Message* intern = (Message*)Z_OBJ_P(getThis()); Message* intern = (Message*)Z_OBJ_P(getThis());
upb_Arena *arena = Arena_Get(&intern->arena); upb_Arena* arena = Arena_Get(&intern->arena);
char* member; char* member;
const upb_FieldDef *f; const upb_FieldDef* f;
upb_MessageValue msgval; upb_MessageValue msgval;
zend_long size; zend_long size;
zval* val; zval* val;
@ -923,11 +927,13 @@ PHP_METHOD(Message, writeWrapperValue) {
return; return;
} }
f = upb_MessageDef_FindFieldByNameWithSize(intern->desc->msgdef, member, size); f = upb_MessageDef_FindFieldByNameWithSize(intern->desc->msgdef, member,
size);
if (!f || !IsWrapper(upb_FieldDef_MessageSubDef(f))) { if (!f || !IsWrapper(upb_FieldDef_MessageSubDef(f))) {
zend_throw_exception_ex(NULL, 0, "Message %s has no field %s", zend_throw_exception_ex(NULL, 0, "Message %s has no field %s",
upb_MessageDef_FullName(intern->desc->msgdef), member); upb_MessageDef_FullName(intern->desc->msgdef),
member);
return; return;
} }
@ -938,9 +944,9 @@ PHP_METHOD(Message, writeWrapperValue) {
if (Z_TYPE_P(val) == IS_NULL) { if (Z_TYPE_P(val) == IS_NULL) {
upb_Message_ClearFieldByDef(intern->msg, f); upb_Message_ClearFieldByDef(intern->msg, f);
} else { } else {
const upb_MessageDef *m = upb_FieldDef_MessageSubDef(f); const upb_MessageDef* m = upb_FieldDef_MessageSubDef(f);
const upb_FieldDef *val_f = upb_MessageDef_FindFieldByNumber(m, 1); const upb_FieldDef* val_f = upb_MessageDef_FindFieldByNumber(m, 1);
upb_Message *wrapper; upb_Message* wrapper;
if (!Convert_PhpToUpb(val, &msgval, TypeInfo_Get(val_f), arena)) { if (!Convert_PhpToUpb(val, &msgval, TypeInfo_Get(val_f), arena)) {
return; // Error is already set. return; // Error is already set.
@ -970,11 +976,13 @@ PHP_METHOD(Message, whichOneof) {
return; return;
} }
oneof = upb_MessageDef_FindOneofByNameWithSize(intern->desc->msgdef, name, len); oneof =
upb_MessageDef_FindOneofByNameWithSize(intern->desc->msgdef, name, len);
if (!oneof) { if (!oneof) {
zend_throw_exception_ex(NULL, 0, "Message %s has no oneof %s", zend_throw_exception_ex(NULL, 0, "Message %s has no oneof %s",
upb_MessageDef_FullName(intern->desc->msgdef), name); upb_MessageDef_FullName(intern->desc->msgdef),
name);
return; return;
} }
@ -1083,7 +1091,7 @@ PHP_METHOD(Message, writeOneof) {
Message* intern = (Message*)Z_OBJ_P(getThis()); Message* intern = (Message*)Z_OBJ_P(getThis());
zend_long field_num; zend_long field_num;
const upb_FieldDef* f; const upb_FieldDef* f;
upb_Arena *arena = Arena_Get(&intern->arena); upb_Arena* arena = Arena_Get(&intern->arena);
upb_MessageValue msgval; upb_MessageValue msgval;
zval* val; zval* val;
@ -1104,6 +1112,7 @@ PHP_METHOD(Message, writeOneof) {
upb_Message_SetFieldByDef(intern->msg, f, msgval, arena); upb_Message_SetFieldByDef(intern->msg, f, msgval, arena);
} }
// clang-format off
ZEND_BEGIN_ARG_INFO_EX(arginfo_construct, 0, 0, 0) ZEND_BEGIN_ARG_INFO_EX(arginfo_construct, 0, 0, 0)
ZEND_ARG_INFO(0, data) ZEND_ARG_INFO(0, data)
ZEND_END_ARG_INFO() ZEND_END_ARG_INFO()
@ -1143,20 +1152,23 @@ static zend_function_entry Message_methods[] = {
PHP_ME(Message, __construct, arginfo_construct, ZEND_ACC_PROTECTED) PHP_ME(Message, __construct, arginfo_construct, ZEND_ACC_PROTECTED)
ZEND_FE_END ZEND_FE_END
}; };
// clang-format on
// Well-known types //////////////////////////////////////////////////////////// // Well-known types ////////////////////////////////////////////////////////////
static const char TYPE_URL_PREFIX[] = "type.googleapis.com/"; static const char TYPE_URL_PREFIX[] = "type.googleapis.com/";
static upb_MessageValue Message_getval(Message *intern, const char *field_name) { static upb_MessageValue Message_getval(Message* intern,
const upb_FieldDef *f = upb_MessageDef_FindFieldByName(intern->desc->msgdef, field_name); const char* field_name) {
const upb_FieldDef* f =
upb_MessageDef_FindFieldByName(intern->desc->msgdef, field_name);
PBPHP_ASSERT(f); PBPHP_ASSERT(f);
return upb_Message_GetFieldByDef(intern->msg, f); return upb_Message_GetFieldByDef(intern->msg, f);
} }
static void Message_setval(Message *intern, const char *field_name, static void Message_setval(Message* intern, const char* field_name,
upb_MessageValue val) { upb_MessageValue val) {
const upb_FieldDef *f = const upb_FieldDef* f =
upb_MessageDef_FindFieldByName(intern->desc->msgdef, field_name); upb_MessageDef_FindFieldByName(intern->desc->msgdef, field_name);
PBPHP_ASSERT(f); PBPHP_ASSERT(f);
upb_Message_SetFieldByDef(intern->msg, f, val, Arena_Get(&intern->arena)); upb_Message_SetFieldByDef(intern->msg, f, val, Arena_Get(&intern->arena));
@ -1168,7 +1180,7 @@ static upb_MessageValue StringVal(upb_StringView view) {
return ret; return ret;
} }
static bool TryStripUrlPrefix(upb_StringView *str) { static bool TryStripUrlPrefix(upb_StringView* str) {
size_t size = strlen(TYPE_URL_PREFIX); size_t size = strlen(TYPE_URL_PREFIX);
if (str->size < size || memcmp(TYPE_URL_PREFIX, str->data, size) != 0) { if (str->size < size || memcmp(TYPE_URL_PREFIX, str->data, size) != 0) {
return false; return false;
@ -1178,7 +1190,7 @@ static bool TryStripUrlPrefix(upb_StringView *str) {
return true; return true;
} }
static bool StrViewEq(upb_StringView view, const char *str) { static bool StrViewEq(upb_StringView view, const char* str) {
size_t size = strlen(str); size_t size = strlen(str);
return view.size == size && memcmp(view.data, str, size) == 0; return view.size == size && memcmp(view.data, str, size) == 0;
} }
@ -1187,20 +1199,20 @@ PHP_METHOD(google_protobuf_Any, unpack) {
Message* intern = (Message*)Z_OBJ_P(getThis()); Message* intern = (Message*)Z_OBJ_P(getThis());
upb_StringView type_url = Message_getval(intern, "type_url").str_val; upb_StringView type_url = Message_getval(intern, "type_url").str_val;
upb_StringView value = Message_getval(intern, "value").str_val; upb_StringView value = Message_getval(intern, "value").str_val;
upb_DefPool *symtab = DescriptorPool_GetSymbolTable(); upb_DefPool* symtab = DescriptorPool_GetSymbolTable();
const upb_MessageDef *m; const upb_MessageDef* m;
Descriptor *desc; Descriptor* desc;
zval ret; zval ret;
// Ensure that type_url has TYPE_URL_PREFIX as a prefix. // Ensure that type_url has TYPE_URL_PREFIX as a prefix.
if (!TryStripUrlPrefix(&type_url)) { if (!TryStripUrlPrefix(&type_url)) {
zend_throw_exception( zend_throw_exception(
NULL, "Type url needs to be type.googleapis.com/fully-qualified", NULL, "Type url needs to be type.googleapis.com/fully-qualified", 0);
0);
return; return;
} }
m = upb_DefPool_FindMessageByNameWithSize(symtab, type_url.data, type_url.size); m = upb_DefPool_FindMessageByNameWithSize(symtab, type_url.data,
type_url.size);
if (m == NULL) { if (m == NULL) {
zend_throw_exception( zend_throw_exception(
@ -1211,8 +1223,8 @@ PHP_METHOD(google_protobuf_Any, unpack) {
desc = Descriptor_GetFromMessageDef(m); desc = Descriptor_GetFromMessageDef(m);
PBPHP_ASSERT(desc->class_entry->create_object == Message_create); PBPHP_ASSERT(desc->class_entry->create_object == Message_create);
zend_object *obj = Message_create(desc->class_entry); zend_object* obj = Message_create(desc->class_entry);
Message *msg = (Message*)obj; Message* msg = (Message*)obj;
Message_Initialize(msg, desc); Message_Initialize(msg, desc);
ZVAL_OBJ(&ret, obj); ZVAL_OBJ(&ret, obj);
@ -1233,16 +1245,15 @@ PHP_METHOD(google_protobuf_Any, unpack) {
PHP_METHOD(google_protobuf_Any, pack) { PHP_METHOD(google_protobuf_Any, pack) {
Message* intern = (Message*)Z_OBJ_P(getThis()); Message* intern = (Message*)Z_OBJ_P(getThis());
upb_Arena *arena = Arena_Get(&intern->arena); upb_Arena* arena = Arena_Get(&intern->arena);
zval *val; zval* val;
Message *msg; Message* msg;
upb_StringView value; upb_StringView value;
upb_StringView type_url; upb_StringView type_url;
const char *full_name; const char* full_name;
char *buf; char* buf;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &val) == if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &val) == FAILURE) {
FAILURE) {
return; return;
} }
@ -1275,11 +1286,10 @@ PHP_METHOD(google_protobuf_Any, pack) {
PHP_METHOD(google_protobuf_Any, is) { PHP_METHOD(google_protobuf_Any, is) {
Message* intern = (Message*)Z_OBJ_P(getThis()); Message* intern = (Message*)Z_OBJ_P(getThis());
upb_StringView type_url = Message_getval(intern, "type_url").str_val; upb_StringView type_url = Message_getval(intern, "type_url").str_val;
zend_class_entry *klass = NULL; zend_class_entry* klass = NULL;
const upb_MessageDef *m; const upb_MessageDef* m;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "C", &klass) == if (zend_parse_parameters(ZEND_NUM_ARGS(), "C", &klass) == FAILURE) {
FAILURE) {
return; return;
} }
@ -1296,9 +1306,10 @@ PHP_METHOD(google_protobuf_Any, is) {
PHP_METHOD(google_protobuf_Timestamp, fromDateTime) { PHP_METHOD(google_protobuf_Timestamp, fromDateTime) {
Message* intern = (Message*)Z_OBJ_P(getThis()); Message* intern = (Message*)Z_OBJ_P(getThis());
zval* datetime; zval* datetime;
const char *classname = "\\DatetimeInterface"; const char* classname = "\\DatetimeInterface";
zend_string *classname_str = zend_string_init(classname, strlen(classname), 0); zend_string* classname_str =
zend_class_entry *date_interface_ce = zend_lookup_class(classname_str); zend_string_init(classname, strlen(classname), 0);
zend_class_entry* date_interface_ce = zend_lookup_class(classname_str);
zend_string_release(classname_str); zend_string_release(classname_str);
if (date_interface_ce == NULL) { if (date_interface_ce == NULL) {
@ -1387,8 +1398,8 @@ PHP_METHOD(google_protobuf_Timestamp, toDateTime) {
ZVAL_STRING(&formatted_time_php, formatted_time); ZVAL_STRING(&formatted_time_php, formatted_time);
zval params[2] = { zval params[2] = {
format_string, format_string,
formatted_time_php, formatted_time_php,
}; };
if (call_user_function(EG(function_table), NULL, &function_name, &datetime, 2, if (call_user_function(EG(function_table), NULL, &function_name, &datetime, 2,
@ -1413,7 +1424,7 @@ PHP_METHOD(google_protobuf_Timestamp, toDateTime) {
*/ */
void Message_ModuleInit() { void Message_ModuleInit() {
zend_class_entry tmp_ce; zend_class_entry tmp_ce;
zend_object_handlers *h = &message_object_handlers; zend_object_handlers* h = &message_object_handlers;
INIT_CLASS_ENTRY(tmp_ce, "Google\\Protobuf\\Internal\\Message", INIT_CLASS_ENTRY(tmp_ce, "Google\\Protobuf\\Internal\\Message",
Message_methods); Message_methods);
@ -1436,5 +1447,5 @@ void Message_ModuleInit() {
h->get_property_ptr_ptr = Message_get_property_ptr_ptr; h->get_property_ptr_ptr = Message_get_property_ptr_ptr;
h->clone_obj = Message_clone_obj; h->clone_obj = Message_clone_obj;
WellKnownTypes_ModuleInit(); /* From wkt.inc. */ WellKnownTypes_ModuleInit(); /* From wkt.inc. */
} }

@ -40,21 +40,22 @@ void Message_ModuleInit();
// Gets a upb_Message* for the PHP object |val|, which must either be a Message // Gets a upb_Message* for the PHP object |val|, which must either be a Message
// object or 'null'. Returns true and stores the message in |msg| if the // object or 'null'. Returns true and stores the message in |msg| if the
// conversion succeeded (we can't return upb_Message* because null->NULL is a valid // conversion succeeded (we can't return upb_Message* because null->NULL is a
// conversion). Returns false and raises a PHP error if this isn't a Message // valid conversion). Returns false and raises a PHP error if this isn't a
// object or null, or if the Message object doesn't match this Descriptor. // Message object or null, or if the Message object doesn't match this
// Descriptor.
// //
// The given |arena| will be fused to this message's arena. // The given |arena| will be fused to this message's arena.
bool Message_GetUpbMessage(zval *val, const Descriptor *desc, upb_Arena *arena, bool Message_GetUpbMessage(zval* val, const Descriptor* desc, upb_Arena* arena,
upb_Message **msg); upb_Message** msg);
// Gets or creates a PHP Message object to wrap the given upb_Message* and |desc| // Gets or creates a PHP Message object to wrap the given upb_Message* and
// and returns it in |val|. The PHP object will keep a reference to this |arena| // |desc| and returns it in |val|. The PHP object will keep a reference to this
// to ensure the underlying message data stays alive. // |arena| to ensure the underlying message data stays alive.
// //
// If |msg| is NULL, this will return a PHP null. // If |msg| is NULL, this will return a PHP null.
void Message_GetPhpWrapper(zval *val, const Descriptor *desc, upb_Message *msg, void Message_GetPhpWrapper(zval* val, const Descriptor* desc, upb_Message* msg,
zval *arena); zval* arena);
bool ValueEq(upb_MessageValue val1, upb_MessageValue val2, TypeInfo type); bool ValueEq(upb_MessageValue val1, upb_MessageValue val2, TypeInfo type);

@ -37,11 +37,11 @@
/* stringsink *****************************************************************/ /* stringsink *****************************************************************/
typedef struct { typedef struct {
char *ptr; char* ptr;
size_t len, size; size_t len, size;
} stringsink; } stringsink;
static size_t stringsink_string(stringsink *sink, const char *ptr, size_t len) { static size_t stringsink_string(stringsink* sink, const char* ptr, size_t len) {
size_t new_size = sink->size; size_t new_size = sink->size;
while (sink->len + len > new_size) { while (sink->len + len > new_size) {
@ -59,38 +59,37 @@ static size_t stringsink_string(stringsink *sink, const char *ptr, size_t len) {
return len; return len;
} }
static void stringsink_init(stringsink *sink) { static void stringsink_init(stringsink* sink) {
sink->size = 32; sink->size = 32;
sink->ptr = malloc(sink->size); sink->ptr = malloc(sink->size);
PBPHP_ASSERT(sink->ptr != NULL); PBPHP_ASSERT(sink->ptr != NULL);
sink->len = 0; sink->len = 0;
} }
static void stringsink_uninit(stringsink *sink) { free(sink->ptr); } static void stringsink_uninit(stringsink* sink) { free(sink->ptr); }
/* def name -> classname ******************************************************/ /* def name -> classname ******************************************************/
const char *const kReservedNames[] = { const char* const kReservedNames[] = {
"abstract", "and", "array", "as", "break", "abstract", "and", "array", "as", "break",
"callable", "case", "catch", "class", "clone", "callable", "case", "catch", "class", "clone",
"const", "continue", "declare", "default", "die", "const", "continue", "declare", "default", "die",
"do", "echo", "else", "elseif", "empty", "do", "echo", "else", "elseif", "empty",
"enddeclare", "endfor", "endforeach", "endif", "endswitch", "enddeclare", "endfor", "endforeach", "endif", "endswitch",
"endwhile", "eval", "exit", "extends", "final", "endwhile", "eval", "exit", "extends", "final",
"finally", "fn", "for", "foreach", "function", "finally", "fn", "for", "foreach", "function",
"if", "implements", "include", "include_once", "instanceof", "if", "implements", "include", "include_once", "instanceof",
"global", "goto", "insteadof", "interface", "isset", "global", "goto", "insteadof", "interface", "isset",
"list", "match", "namespace", "new", "object", "list", "match", "namespace", "new", "object",
"or", "parent", "print", "private", "protected", "or", "parent", "print", "private", "protected",
"public", "readonly", "require", "require_once", "return", "public", "readonly", "require", "require_once", "return",
"self", "static", "switch", "throw", "trait", "self", "static", "switch", "throw", "trait",
"try", "unset", "use", "var", "while", "try", "unset", "use", "var", "while",
"xor", "yield", "int", "float", "bool", "xor", "yield", "int", "float", "bool",
"string", "true", "false", "null", "void", "string", "true", "false", "null", "void",
"iterable", NULL}; "iterable", NULL};
const char *const kPreviouslyUnreservedNames[] = { const char* const kPreviouslyUnreservedNames[] = {"readonly", NULL};
"readonly", NULL};
bool is_reserved_name(const char* name) { bool is_reserved_name(const char* name) {
int i; int i;
@ -127,16 +126,16 @@ static char nolocale_toupper(char ch) {
} }
} }
static char *strdup_nolocale_lower(char *str, int length) { static char* strdup_nolocale_lower(char* str, int length) {
char* lower = malloc(length + 1); char* lower = malloc(length + 1);
lower[length] = '\0'; lower[length] = '\0';
for(int i = 0; i < length; ++i) { for (int i = 0; i < length; ++i) {
lower[i] = nolocale_tolower(str[i]); lower[i] = nolocale_tolower(str[i]);
} }
return lower; return lower;
} }
static bool is_reserved(const char *segment, int length, bool previous) { static bool is_reserved(const char* segment, int length, bool previous) {
bool result; bool result;
char* lower = strdup_nolocale_lower(segment, length); char* lower = strdup_nolocale_lower(segment, length);
result = is_reserved_name(lower); result = is_reserved_name(lower);
@ -147,11 +146,9 @@ static bool is_reserved(const char *segment, int length, bool previous) {
return result; return result;
} }
static void fill_prefix(const char *segment, int length, static void fill_prefix(const char* segment, int length,
const char *prefix_given, const char* prefix_given, const char* package_name,
const char *package_name, stringsink* classname, bool previous) {
stringsink *classname,
bool previous) {
if (prefix_given != NULL && strcmp(prefix_given, "") != 0) { if (prefix_given != NULL && strcmp(prefix_given, "") != 0) {
stringsink_string(classname, prefix_given, strlen(prefix_given)); stringsink_string(classname, prefix_given, strlen(prefix_given));
} else { } else {
@ -166,8 +163,8 @@ static void fill_prefix(const char *segment, int length,
} }
} }
static void fill_segment(const char *segment, int length, static void fill_segment(const char* segment, int length, stringsink* classname,
stringsink *classname, bool use_camel) { bool use_camel) {
if (use_camel && (segment[0] < 'A' || segment[0] > 'Z')) { if (use_camel && (segment[0] < 'A' || segment[0] > 'Z')) {
char first = nolocale_toupper(segment[0]); char first = nolocale_toupper(segment[0]);
stringsink_string(classname, &first, 1); stringsink_string(classname, &first, 1);
@ -177,8 +174,8 @@ static void fill_segment(const char *segment, int length,
} }
} }
static void fill_namespace(const char *package, const char *php_namespace, static void fill_namespace(const char* package, const char* php_namespace,
stringsink *classname, bool previous) { stringsink* classname, bool previous) {
if (php_namespace != NULL) { if (php_namespace != NULL) {
if (strlen(php_namespace) != 0) { if (strlen(php_namespace) != 0) {
stringsink_string(classname, php_namespace, strlen(php_namespace)); stringsink_string(classname, php_namespace, strlen(php_namespace));
@ -200,10 +197,8 @@ static void fill_namespace(const char *package, const char *php_namespace,
} }
} }
static void fill_classname(const char *fullname, static void fill_classname(const char* fullname, const char* package,
const char *package, const char* prefix, stringsink* classname,
const char *prefix,
stringsink *classname,
bool previous) { bool previous) {
int classname_start = 0; int classname_start = 0;
if (package != NULL) { if (package != NULL) {
@ -227,28 +222,29 @@ static void fill_classname(const char *fullname,
} }
} }
char *str_view_dup(upb_StringView str) { char* str_view_dup(upb_StringView str) {
char *ret = malloc(str.size + 1); char* ret = malloc(str.size + 1);
memcpy(ret, str.data, str.size); memcpy(ret, str.data, str.size);
ret[str.size] = '\0'; ret[str.size] = '\0';
return ret; return ret;
} }
char *GetPhpClassname(const upb_FileDef *file, const char *fullname, bool previous) { char* GetPhpClassname(const upb_FileDef* file, const char* fullname,
bool previous) {
// Prepend '.' to package name to make it absolute. In the 5 additional // Prepend '.' to package name to make it absolute. In the 5 additional
// bytes allocated, one for '.', one for trailing 0, and 3 for 'GPB' if // bytes allocated, one for '.', one for trailing 0, and 3 for 'GPB' if
// given message is google.protobuf.Empty. // given message is google.protobuf.Empty.
const google_protobuf_FileOptions* opts = upb_FileDef_Options(file); const google_protobuf_FileOptions* opts = upb_FileDef_Options(file);
const char *package = upb_FileDef_Package(file); const char* package = upb_FileDef_Package(file);
char *php_namespace = char* php_namespace =
google_protobuf_FileOptions_has_php_namespace(opts) google_protobuf_FileOptions_has_php_namespace(opts)
? str_view_dup(google_protobuf_FileOptions_php_namespace(opts)) ? str_view_dup(google_protobuf_FileOptions_php_namespace(opts))
: NULL; : NULL;
char *prefix = char* prefix =
google_protobuf_FileOptions_has_php_class_prefix(opts) google_protobuf_FileOptions_has_php_class_prefix(opts)
? str_view_dup(google_protobuf_FileOptions_php_class_prefix(opts)) ? str_view_dup(google_protobuf_FileOptions_php_class_prefix(opts))
: NULL; : NULL;
char *ret; char* ret;
stringsink namesink; stringsink namesink;
stringsink_init(&namesink); stringsink_init(&namesink);
@ -264,7 +260,7 @@ char *GetPhpClassname(const upb_FileDef *file, const char *fullname, bool previo
} }
bool IsPreviouslyUnreservedClassName(const char* fullname) { bool IsPreviouslyUnreservedClassName(const char* fullname) {
const char *classname = strrchr(fullname, '\\'); const char* classname = strrchr(fullname, '\\');
if (classname) { if (classname) {
classname += 1; classname += 1;
} else { } else {

@ -35,7 +35,8 @@
// Translates a protobuf symbol name (eg. foo.bar.Baz) into a PHP class name // Translates a protobuf symbol name (eg. foo.bar.Baz) into a PHP class name
// (eg. \Foo\Bar\Baz). // (eg. \Foo\Bar\Baz).
char *GetPhpClassname(const upb_FileDef *file, const char *fullname, bool previous); char* GetPhpClassname(const upb_FileDef* file, const char* fullname,
bool previous);
bool IsPreviouslyUnreservedClassName(const char* fullname); bool IsPreviouslyUnreservedClassName(const char* fullname);
#endif // PHP_PROTOBUF_NAMES_H_ #endif // PHP_PROTOBUF_NAMES_H_

@ -33,13 +33,13 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef PHP_PROTOBUF_H #ifndef PHP_PROTOBUF_H
# define PHP_PROTOBUF_H #define PHP_PROTOBUF_H
# ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
# include "config.h" #include "config.h"
# endif #endif
extern zend_module_entry protobuf_module_entry; extern zend_module_entry protobuf_module_entry;
# define phpext_protobuf_ptr &protobuf_module_entry #define phpext_protobuf_ptr &protobuf_module_entry
#endif /* PHP_PROTOBUF_H */ #endif /* PHP_PROTOBUF_H */

@ -30,8 +30,8 @@
#include "protobuf.h" #include "protobuf.h"
#include <php.h>
#include <Zend/zend_interfaces.h> #include <Zend/zend_interfaces.h>
#include <php.h>
#include "arena.h" #include "arena.h"
#include "array.h" #include "array.h"
@ -52,6 +52,7 @@
#define PROTOBUF_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(protobuf, v) #define PROTOBUF_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(protobuf, v)
// clang-format off
ZEND_BEGIN_MODULE_GLOBALS(protobuf) ZEND_BEGIN_MODULE_GLOBALS(protobuf)
// Set by the user to make the descriptor pool persist between requests. // Set by the user to make the descriptor pool persist between requests.
zend_bool keep_descriptor_pool_after_request; zend_bool keep_descriptor_pool_after_request;
@ -63,7 +64,7 @@ ZEND_BEGIN_MODULE_GLOBALS(protobuf)
// to rebuild it from scratch. When keep_descriptor_pool_after_request==true, // to rebuild it from scratch. When keep_descriptor_pool_after_request==true,
// we steal the upb_DefPool from the global DescriptorPool object just before // we steal the upb_DefPool from the global DescriptorPool object just before
// destroying it. // destroying it.
upb_DefPool *global_symtab; upb_DefPool* global_symtab;
// Object cache (see interface in protobuf.h). // Object cache (see interface in protobuf.h).
HashTable object_cache; HashTable object_cache;
@ -78,8 +79,9 @@ ZEND_BEGIN_MODULE_GLOBALS(protobuf)
// destroyed on request shutdown. // destroyed on request shutdown.
HashTable descriptors; HashTable descriptors;
ZEND_END_MODULE_GLOBALS(protobuf) ZEND_END_MODULE_GLOBALS(protobuf)
// clang-format on
void free_protobuf_globals(zend_protobuf_globals *globals) { void free_protobuf_globals(zend_protobuf_globals* globals) {
zend_hash_destroy(&globals->name_msg_cache); zend_hash_destroy(&globals->name_msg_cache);
zend_hash_destroy(&globals->name_enum_cache); zend_hash_destroy(&globals->name_enum_cache);
upb_DefPool_Free(globals->global_symtab); upb_DefPool_Free(globals->global_symtab);
@ -88,9 +90,7 @@ void free_protobuf_globals(zend_protobuf_globals *globals) {
ZEND_DECLARE_MODULE_GLOBALS(protobuf) ZEND_DECLARE_MODULE_GLOBALS(protobuf)
upb_DefPool *get_global_symtab() { upb_DefPool* get_global_symtab() { return PROTOBUF_G(global_symtab); }
return PROTOBUF_G(global_symtab);
}
// This is a PHP extension (not a Zend extension). What follows is a summary of // This is a PHP extension (not a Zend extension). What follows is a summary of
// a PHP extension's lifetime and when various handlers are called. // a PHP extension's lifetime and when various handlers are called.
@ -144,7 +144,8 @@ upb_DefPool *get_global_symtab() {
// * The GitHub master branch supports 7.2+, but this has not been released // * The GitHub master branch supports 7.2+, but this has not been released
// to PECL. // to PECL.
// * Its owner has disavowed it as "broken by design" and "in an untenable // * Its owner has disavowed it as "broken by design" and "in an untenable
// position for the future": https://github.com/krakjoe/pthreads/issues/929 // position for the future":
// https://github.com/krakjoe/pthreads/issues/929
// * The only way to use PHP with requests in different threads is to use the // * The only way to use PHP with requests in different threads is to use the
// Apache 2 mod_php with the "worker" MPM. But this is explicitly // Apache 2 mod_php with the "worker" MPM. But this is explicitly
// discouraged by the documentation: https://serverfault.com/a/231660 // discouraged by the documentation: https://serverfault.com/a/231660
@ -155,9 +156,7 @@ static PHP_GSHUTDOWN_FUNCTION(protobuf) {
} }
} }
static PHP_GINIT_FUNCTION(protobuf) { static PHP_GINIT_FUNCTION(protobuf) { protobuf_globals->global_symtab = NULL; }
protobuf_globals->global_symtab = NULL;
}
/** /**
* PHP_RINIT_FUNCTION(protobuf) * PHP_RINIT_FUNCTION(protobuf)
@ -167,7 +166,7 @@ static PHP_GINIT_FUNCTION(protobuf) {
static PHP_RINIT_FUNCTION(protobuf) { static PHP_RINIT_FUNCTION(protobuf) {
// Create the global generated pool. // Create the global generated pool.
// Reuse the symtab (if any) left to us by the last request. // Reuse the symtab (if any) left to us by the last request.
upb_DefPool *symtab = PROTOBUF_G(global_symtab); upb_DefPool* symtab = PROTOBUF_G(global_symtab);
if (!symtab) { if (!symtab) {
PROTOBUF_G(global_symtab) = symtab = upb_DefPool_New(); PROTOBUF_G(global_symtab) = symtab = upb_DefPool_New();
zend_hash_init(&PROTOBUF_G(name_msg_cache), 64, NULL, NULL, 0); zend_hash_init(&PROTOBUF_G(name_msg_cache), 64, NULL, NULL, 0);
@ -202,7 +201,7 @@ static PHP_RSHUTDOWN_FUNCTION(protobuf) {
// Object Cache. // Object Cache.
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void Descriptors_Add(zend_object *desc) { void Descriptors_Add(zend_object* desc) {
// The hash table will own a ref (it will destroy it when the table is // The hash table will own a ref (it will destroy it when the table is
// destroyed), but for some reason the insert operation does not add a ref, so // destroyed), but for some reason the insert operation does not add a ref, so
// we do that here with ZVAL_OBJ_COPY(). // we do that here with ZVAL_OBJ_COPY().
@ -211,12 +210,12 @@ void Descriptors_Add(zend_object *desc) {
zend_hash_next_index_insert(&PROTOBUF_G(descriptors), &zv); zend_hash_next_index_insert(&PROTOBUF_G(descriptors), &zv);
} }
void ObjCache_Add(const void *upb_obj, zend_object *php_obj) { void ObjCache_Add(const void* upb_obj, zend_object* php_obj) {
zend_ulong k = (zend_ulong)upb_obj; zend_ulong k = (zend_ulong)upb_obj;
zend_hash_index_add_ptr(&PROTOBUF_G(object_cache), k, php_obj); zend_hash_index_add_ptr(&PROTOBUF_G(object_cache), k, php_obj);
} }
void ObjCache_Delete(const void *upb_obj) { void ObjCache_Delete(const void* upb_obj) {
if (upb_obj) { if (upb_obj) {
zend_ulong k = (zend_ulong)upb_obj; zend_ulong k = (zend_ulong)upb_obj;
int ret = zend_hash_index_del(&PROTOBUF_G(object_cache), k); int ret = zend_hash_index_del(&PROTOBUF_G(object_cache), k);
@ -224,9 +223,9 @@ void ObjCache_Delete(const void *upb_obj) {
} }
} }
bool ObjCache_Get(const void *upb_obj, zval *val) { bool ObjCache_Get(const void* upb_obj, zval* val) {
zend_ulong k = (zend_ulong)upb_obj; zend_ulong k = (zend_ulong)upb_obj;
zend_object *obj = zend_hash_index_find_ptr(&PROTOBUF_G(object_cache), k); zend_object* obj = zend_hash_index_find_ptr(&PROTOBUF_G(object_cache), k);
if (obj) { if (obj) {
ZVAL_OBJ_COPY(val, obj); ZVAL_OBJ_COPY(val, obj);
@ -241,9 +240,10 @@ bool ObjCache_Get(const void *upb_obj, zval *val) {
// Name Cache. // Name Cache.
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void NameMap_AddMessage(const upb_MessageDef *m) { void NameMap_AddMessage(const upb_MessageDef* m) {
for (int i = 0; i < 2; ++i) { for (int i = 0; i < 2; ++i) {
char *k = GetPhpClassname(upb_MessageDef_File(m), upb_MessageDef_FullName(m), (bool)i); char* k = GetPhpClassname(upb_MessageDef_File(m),
upb_MessageDef_FullName(m), (bool)i);
zend_hash_str_add_ptr(&PROTOBUF_G(name_msg_cache), k, strlen(k), (void*)m); zend_hash_str_add_ptr(&PROTOBUF_G(name_msg_cache), k, strlen(k), (void*)m);
if (!IsPreviouslyUnreservedClassName(k)) { if (!IsPreviouslyUnreservedClassName(k)) {
free(k); free(k);
@ -253,14 +253,15 @@ void NameMap_AddMessage(const upb_MessageDef *m) {
} }
} }
void NameMap_AddEnum(const upb_EnumDef *e) { void NameMap_AddEnum(const upb_EnumDef* e) {
char *k = GetPhpClassname(upb_EnumDef_File(e), upb_EnumDef_FullName(e), false); char* k =
GetPhpClassname(upb_EnumDef_File(e), upb_EnumDef_FullName(e), false);
zend_hash_str_add_ptr(&PROTOBUF_G(name_enum_cache), k, strlen(k), (void*)e); zend_hash_str_add_ptr(&PROTOBUF_G(name_enum_cache), k, strlen(k), (void*)e);
free(k); free(k);
} }
const upb_MessageDef *NameMap_GetMessage(zend_class_entry *ce) { const upb_MessageDef* NameMap_GetMessage(zend_class_entry* ce) {
const upb_MessageDef *ret = const upb_MessageDef* ret =
zend_hash_find_ptr(&PROTOBUF_G(name_msg_cache), ce->name); zend_hash_find_ptr(&PROTOBUF_G(name_msg_cache), ce->name);
if (!ret && ce->create_object && ce != PROTOBUF_G(constructing_class)) { if (!ret && ce->create_object && ce != PROTOBUF_G(constructing_class)) {
@ -272,7 +273,7 @@ const upb_MessageDef *NameMap_GetMessage(zend_class_entry *ce) {
zval_ptr_dtor(&tmp); zval_ptr_dtor(&tmp);
#else #else
zval zv; zval zv;
zend_object *tmp = ce->create_object(ce); zend_object* tmp = ce->create_object(ce);
zend_call_method_with_0_params(tmp, ce, NULL, "__construct", &zv); zend_call_method_with_0_params(tmp, ce, NULL, "__construct", &zv);
OBJ_RELEASE(tmp); OBJ_RELEASE(tmp);
#endif #endif
@ -283,8 +284,8 @@ const upb_MessageDef *NameMap_GetMessage(zend_class_entry *ce) {
return ret; return ret;
} }
const upb_EnumDef *NameMap_GetEnum(zend_class_entry *ce) { const upb_EnumDef* NameMap_GetEnum(zend_class_entry* ce) {
const upb_EnumDef *ret = const upb_EnumDef* ret =
zend_hash_find_ptr(&PROTOBUF_G(name_enum_cache), ce->name); zend_hash_find_ptr(&PROTOBUF_G(name_enum_cache), ce->name);
return ret; return ret;
} }
@ -303,20 +304,15 @@ void NameMap_ExitConstructor(zend_class_entry* ce) {
// Module init. // Module init.
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
zend_function_entry protobuf_functions[] = { zend_function_entry protobuf_functions[] = {ZEND_FE_END};
ZEND_FE_END
};
static const zend_module_dep protobuf_deps[] = { static const zend_module_dep protobuf_deps[] = {ZEND_MOD_OPTIONAL("date")
ZEND_MOD_OPTIONAL("date") ZEND_MOD_END};
ZEND_MOD_END
};
PHP_INI_BEGIN() PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("protobuf.keep_descriptor_pool_after_request", "0", STD_PHP_INI_ENTRY("protobuf.keep_descriptor_pool_after_request", "0",
PHP_INI_ALL, OnUpdateBool, PHP_INI_ALL, OnUpdateBool, keep_descriptor_pool_after_request,
keep_descriptor_pool_after_request, zend_protobuf_globals, zend_protobuf_globals, protobuf_globals)
protobuf_globals)
PHP_INI_END() PHP_INI_END()
static PHP_MINIT_FUNCTION(protobuf) { static PHP_MINIT_FUNCTION(protobuf) {
@ -336,22 +332,21 @@ static PHP_MSHUTDOWN_FUNCTION(protobuf) {
} }
zend_module_entry protobuf_module_entry = { zend_module_entry protobuf_module_entry = {
STANDARD_MODULE_HEADER_EX, STANDARD_MODULE_HEADER_EX,
NULL, NULL,
protobuf_deps, protobuf_deps,
"protobuf", // extension name "protobuf", // extension name
protobuf_functions, // function list protobuf_functions, // function list
PHP_MINIT(protobuf), // process startup PHP_MINIT(protobuf), // process startup
PHP_MSHUTDOWN(protobuf), // process shutdown PHP_MSHUTDOWN(protobuf), // process shutdown
PHP_RINIT(protobuf), // request shutdown PHP_RINIT(protobuf), // request shutdown
PHP_RSHUTDOWN(protobuf), // request shutdown PHP_RSHUTDOWN(protobuf), // request shutdown
NULL, // extension info NULL, // extension info
PHP_PROTOBUF_VERSION, // extension version PHP_PROTOBUF_VERSION, // extension version
PHP_MODULE_GLOBALS(protobuf), // globals descriptor PHP_MODULE_GLOBALS(protobuf), // globals descriptor
PHP_GINIT(protobuf), // globals ctor PHP_GINIT(protobuf), // globals ctor
PHP_GSHUTDOWN(protobuf), // globals dtor PHP_GSHUTDOWN(protobuf), // globals dtor
NULL, // post deactivate NULL, // post deactivate
STANDARD_MODULE_PROPERTIES_EX STANDARD_MODULE_PROPERTIES_EX};
};
ZEND_GET_MODULE(protobuf) ZEND_GET_MODULE(protobuf)

@ -36,7 +36,7 @@
#include "php-upb.h" #include "php-upb.h"
upb_DefPool *get_global_symtab(); upb_DefPool* get_global_symtab();
#if PHP_VERSION_ID < 70300 #if PHP_VERSION_ID < 70300
#define GC_ADDREF(h) ++GC_REFCOUNT(h) #define GC_ADDREF(h) ++GC_REFCOUNT(h)
@ -49,7 +49,7 @@ upb_DefPool *get_global_symtab();
#if PHP_VERSION_ID < 70400 #if PHP_VERSION_ID < 70400
#define PROTO_RETURN_VAL void #define PROTO_RETURN_VAL void
#else #else
#define PROTO_RETURN_VAL zval * #define PROTO_RETURN_VAL zval*
#endif #endif
// Since php 8.0, the Object Handlers API was changed to receive zend_object* // Since php 8.0, the Object Handlers API was changed to receive zend_object*
@ -58,7 +58,7 @@ upb_DefPool *get_global_symtab();
#if PHP_VERSION_ID < 80000 #if PHP_VERSION_ID < 80000
#define PROTO_VAL zval #define PROTO_VAL zval
#define PROTO_STR zval #define PROTO_STR zval
#define PROTO_VAL_P(obj) (void *)Z_OBJ_P(obj) #define PROTO_VAL_P(obj) (void*)Z_OBJ_P(obj)
#define PROTO_STRVAL_P(obj) Z_STRVAL_P(obj) #define PROTO_STRVAL_P(obj) Z_STRVAL_P(obj)
#define PROTO_STRLEN_P(obj) Z_STRLEN_P(obj) #define PROTO_STRLEN_P(obj) Z_STRLEN_P(obj)
#define ZVAL_OBJ_COPY(z, o) \ #define ZVAL_OBJ_COPY(z, o) \
@ -85,7 +85,7 @@ upb_DefPool *get_global_symtab();
#else #else
#define PROTO_VAL zend_object #define PROTO_VAL zend_object
#define PROTO_STR zend_string #define PROTO_STR zend_string
#define PROTO_VAL_P(obj) (void *)(obj) #define PROTO_VAL_P(obj) (void*)(obj)
#define PROTO_STRVAL_P(obj) ZSTR_VAL(obj) #define PROTO_STRVAL_P(obj) ZSTR_VAL(obj)
#define PROTO_STRLEN_P(obj) ZSTR_LEN(obj) #define PROTO_STRLEN_P(obj) ZSTR_LEN(obj)
#endif #endif
@ -166,26 +166,26 @@ ZEND_END_ARG_INFO()
// remove itself from the map when it is destroyed. This is how we ensure that // remove itself from the map when it is destroyed. This is how we ensure that
// the map only contains live objects. The map is weak so it does not actually // the map only contains live objects. The map is weak so it does not actually
// take references to the cached objects. // take references to the cached objects.
void ObjCache_Add(const void *key, zend_object *php_obj); void ObjCache_Add(const void* key, zend_object* php_obj);
void ObjCache_Delete(const void *key); void ObjCache_Delete(const void* key);
bool ObjCache_Get(const void *key, zval *val); bool ObjCache_Get(const void* key, zval* val);
// PHP class name map. This is necessary because the pb_name->php_class_name // PHP class name map. This is necessary because the pb_name->php_class_name
// transformation is non-reversible, so when we need to look up a msgdef or // transformation is non-reversible, so when we need to look up a msgdef or
// enumdef by PHP class, we can't turn the class name into a pb_name. // enumdef by PHP class, we can't turn the class name into a pb_name.
// * php_class_name -> upb_MessageDef* // * php_class_name -> upb_MessageDef*
// * php_class_name -> upb_EnumDef* // * php_class_name -> upb_EnumDef*
void NameMap_AddMessage(const upb_MessageDef *m); void NameMap_AddMessage(const upb_MessageDef* m);
void NameMap_AddEnum(const upb_EnumDef *m); void NameMap_AddEnum(const upb_EnumDef* m);
const upb_MessageDef *NameMap_GetMessage(zend_class_entry *ce); const upb_MessageDef* NameMap_GetMessage(zend_class_entry* ce);
const upb_EnumDef *NameMap_GetEnum(zend_class_entry *ce); const upb_EnumDef* NameMap_GetEnum(zend_class_entry* ce);
void NameMap_EnterConstructor(zend_class_entry *ce); void NameMap_EnterConstructor(zend_class_entry* ce);
void NameMap_ExitConstructor(zend_class_entry *ce); void NameMap_ExitConstructor(zend_class_entry* ce);
// Add this descriptor object to the global list of descriptors that will be // Add this descriptor object to the global list of descriptors that will be
// kept alive for the duration of the request but destroyed when the request // kept alive for the duration of the request but destroyed when the request
// is ending. // is ending.
void Descriptors_Add(zend_object *desc); void Descriptors_Add(zend_object* desc);
// We need our own assert() because PHP takes control of NDEBUG in its headers. // We need our own assert() because PHP takes control of NDEBUG in its headers.
#ifdef PBPHP_ENABLE_ASSERTS #ifdef PBPHP_ENABLE_ASSERTS

Loading…
Cancel
Save