Skip to content

Commit

Permalink
update PHP and Ruby to use the new accessors, delete the old ones
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 493109199
  • Loading branch information
ericsalo authored and copybara-github committed Dec 5, 2022
1 parent 9e12e81 commit 3f36a91
Show file tree
Hide file tree
Showing 10 changed files with 544 additions and 403 deletions.
2 changes: 1 addition & 1 deletion php/ext/google/protobuf/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ if test "$PHP_PROTOBUF" != "no"; then
PHP_NEW_EXTENSION(
protobuf,
arena.c array.c convert.c def.c map.c message.c names.c php-upb.c protobuf.c third_party/utf8_range/naive.c third_party/utf8_range/range2-neon.c third_party/utf8_range/range2-sse.c,
$ext_shared, , -std=gnu99)
$ext_shared, , -std=gnu99 -I@ext_srcdir@/third_party/utf8_range)
PHP_ADD_BUILD_DIR($ext_builddir/third_party/utf8_range)

fi
52 changes: 28 additions & 24 deletions php/ext/google/protobuf/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,12 @@ static void Message_get(Message *intern, const upb_FieldDef *f, zval *rv) {
RepeatedField_GetPhpWrapper(rv, msgval.array, TypeInfo_Get(f),
&intern->arena);
} else {
if (upb_FieldDef_IsSubMessage(f) && !upb_Message_Has(intern->msg, f)) {
if (upb_FieldDef_IsSubMessage(f) &&
!upb_Message_HasFieldByDef(intern->msg, f)) {
ZVAL_NULL(rv);
return;
}
upb_MessageValue msgval = upb_Message_Get(intern->msg, f);
upb_MessageValue msgval = upb_Message_GetFieldByDef(intern->msg, f);
Convert_UpbToPhp(msgval, rv, TypeInfo_Get(f), &intern->arena);
}
}
Expand All @@ -173,13 +174,13 @@ static bool Message_set(Message *intern, const upb_FieldDef *f, zval *val) {
msgval.array_val = RepeatedField_GetUpbArray(val, TypeInfo_Get(f), arena);
if (!msgval.array_val) return false;
} else if (upb_FieldDef_IsSubMessage(f) && Z_TYPE_P(val) == IS_NULL) {
upb_Message_ClearField(intern->msg, f);
upb_Message_ClearFieldByDef(intern->msg, f);
return true;
} else {
if (!Convert_PhpToUpb(val, &msgval, TypeInfo_Get(f), arena)) return false;
}

upb_Message_Set(intern->msg, f, msgval, arena);
upb_Message_SetFieldByDef(intern->msg, f, msgval, arena);
return true;
}

Expand Down Expand Up @@ -224,14 +225,15 @@ static bool MessageEq(const upb_Message *m1, const upb_Message *m2, const upb_Me
const upb_FieldDef *f = upb_MessageDef_Field(m, i);

if (upb_FieldDef_HasPresence(f)) {
if (upb_Message_Has(m1, f) != upb_Message_Has(m2, f)) {
if (upb_Message_HasFieldByDef(m1, f) !=
upb_Message_HasFieldByDef(m2, f)) {
return false;
}
if (!upb_Message_Has(m1, f)) continue;
if (!upb_Message_HasFieldByDef(m1, f)) continue;
}

upb_MessageValue val1 = upb_Message_Get(m1, f);
upb_MessageValue val2 = upb_Message_Get(m2, f);
upb_MessageValue val1 = upb_Message_GetFieldByDef(m1, f);
upb_MessageValue val2 = upb_Message_GetFieldByDef(m2, f);

if (upb_FieldDef_IsMap(f)) {
if (!MapEq(val1.map_val, val2.map_val, MapType_Get(f))) return false;
Expand Down Expand Up @@ -297,7 +299,7 @@ static int Message_has_property(PROTO_VAL *obj, PROTO_STR *member,
return 0;
}

return upb_Message_Has(intern->msg, f);
return upb_Message_HasFieldByDef(intern->msg, f);
}

/**
Expand Down Expand Up @@ -331,7 +333,7 @@ static void Message_unset_property(PROTO_VAL *obj, PROTO_STR *member,
return;
}

upb_Message_ClearField(intern->msg, f);
upb_Message_ClearFieldByDef(intern->msg, f);
}


Expand Down Expand Up @@ -572,7 +574,7 @@ bool Message_InitFromPhp(upb_Message *msg, const upb_MessageDef *m, zval *init,
}
}

upb_Message_Set(msg, f, msgval, arena);
upb_Message_SetFieldByDef(msg, f, msgval, arena);
zend_hash_move_forward_ex(table, &pos);
zval_dtor(&key);
}
Expand Down Expand Up @@ -645,7 +647,7 @@ PHP_METHOD(Message, discardUnknownFields) {
*/
PHP_METHOD(Message, clear) {
Message* intern = (Message*)Z_OBJ_P(getThis());
upb_Message_Clear(intern->msg, intern->desc->msgdef);
upb_Message_ClearByDef(intern->msg, intern->desc->msgdef);
}

static bool Message_checkEncodeStatus(upb_EncodeStatus status) {
Expand Down Expand Up @@ -878,11 +880,12 @@ PHP_METHOD(Message, readWrapperValue) {
return;
}

if (upb_Message_Has(intern->msg, f)) {
const upb_Message *wrapper = upb_Message_Get(intern->msg, f).msg_val;
if (upb_Message_HasFieldByDef(intern->msg, f)) {
const upb_Message *wrapper =
upb_Message_GetFieldByDef(intern->msg, f).msg_val;
const upb_MessageDef *m = upb_FieldDef_MessageSubDef(f);
const upb_FieldDef *val_f = upb_MessageDef_FindFieldByNumber(m, 1);
upb_MessageValue msgval = upb_Message_Get(wrapper, val_f);
upb_MessageValue msgval = upb_Message_GetFieldByDef(wrapper, val_f);
zval ret;
Convert_UpbToPhp(msgval, &ret, TypeInfo_Get(val_f), &intern->arena);
RETURN_COPY_VALUE(&ret);
Expand Down Expand Up @@ -933,7 +936,7 @@ PHP_METHOD(Message, writeWrapperValue) {
}

if (Z_TYPE_P(val) == IS_NULL) {
upb_Message_ClearField(intern->msg, f);
upb_Message_ClearFieldByDef(intern->msg, f);
} else {
const upb_MessageDef *m = upb_FieldDef_MessageSubDef(f);
const upb_FieldDef *val_f = upb_MessageDef_FindFieldByNumber(m, 1);
Expand All @@ -944,7 +947,7 @@ PHP_METHOD(Message, writeWrapperValue) {
}

wrapper = upb_Message_Mutable(intern->msg, f, arena).msg;
upb_Message_Set(wrapper, val_f, msgval, arena);
upb_Message_SetFieldByDef(wrapper, val_f, msgval, arena);
}
}

Expand Down Expand Up @@ -1009,7 +1012,7 @@ PHP_METHOD(Message, hasOneof) {
(int)field_num);
}

RETVAL_BOOL(upb_Message_Has(intern->msg, f));
RETVAL_BOOL(upb_Message_HasFieldByDef(intern->msg, f));
}

/**
Expand Down Expand Up @@ -1043,12 +1046,13 @@ PHP_METHOD(Message, readOneof) {
(int)field_num);
}

if (upb_FieldDef_IsSubMessage(f) && !upb_Message_Has(intern->msg, f)) {
if (upb_FieldDef_IsSubMessage(f) &&
!upb_Message_HasFieldByDef(intern->msg, f)) {
RETURN_NULL();
}

{
upb_MessageValue msgval = upb_Message_Get(intern->msg, f);
upb_MessageValue msgval = upb_Message_GetFieldByDef(intern->msg, f);
Convert_UpbToPhp(msgval, &ret, TypeInfo_Get(f), &intern->arena);
}

Expand Down Expand Up @@ -1091,13 +1095,13 @@ PHP_METHOD(Message, writeOneof) {
f = upb_MessageDef_FindFieldByNumber(intern->desc->msgdef, field_num);

if (upb_FieldDef_IsSubMessage(f) && Z_TYPE_P(val) == IS_NULL) {
upb_Message_ClearField(intern->msg, f);
upb_Message_ClearFieldByDef(intern->msg, f);
return;
} else if (!Convert_PhpToUpb(val, &msgval, TypeInfo_Get(f), arena)) {
return;
}

upb_Message_Set(intern->msg, f, msgval, arena);
upb_Message_SetFieldByDef(intern->msg, f, msgval, arena);
}

ZEND_BEGIN_ARG_INFO_EX(arginfo_construct, 0, 0, 0)
Expand Down Expand Up @@ -1147,15 +1151,15 @@ static const char TYPE_URL_PREFIX[] = "type.googleapis.com/";
static upb_MessageValue Message_getval(Message *intern, const char *field_name) {
const upb_FieldDef *f = upb_MessageDef_FindFieldByName(intern->desc->msgdef, field_name);
PBPHP_ASSERT(f);
return upb_Message_Get(intern->msg, f);
return upb_Message_GetFieldByDef(intern->msg, f);
}

static void Message_setval(Message *intern, const char *field_name,
upb_MessageValue val) {
const upb_FieldDef *f =
upb_MessageDef_FindFieldByName(intern->desc->msgdef, field_name);
PBPHP_ASSERT(f);
upb_Message_Set(intern->msg, f, val, Arena_Get(&intern->arena));
upb_Message_SetFieldByDef(intern->msg, f, val, Arena_Get(&intern->arena));
}

static upb_MessageValue StringVal(upb_StringView view) {
Expand Down
Loading

0 comments on commit 3f36a91

Please sign in to comment.