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

Add json encode/decode for php. (#3226)

* Add json encode/decode for php.

* Fix php conformance test on 32-bit machines.

* Fix conformance test for c extension.

* Fix comments
parent 5a52b358
......@@ -597,60 +597,63 @@ php_EXTRA_DIST= \
php/ext/google/protobuf/upb.c \
php/ext/google/protobuf/protobuf.c \
php/src/phpdoc.dist.xml \
php/src/Google/Protobuf/Internal/Descriptor.php \
php/src/Google/Protobuf/Internal/CodedInputStream.php \
php/src/Google/Protobuf/Internal/CodedOutputStream.php \
php/src/Google/Protobuf/Internal/DescriptorPool.php \
php/src/Google/Protobuf/Internal/GeneratedCodeInfo.php \
php/src/Google/Protobuf/Internal/OneofField.php \
php/src/Google/Protobuf/Internal/MessageOptions.php \
php/src/Google/Protobuf/Internal/FileDescriptor.php \
php/src/Google/Protobuf/Internal/FileDescriptorProto.php \
php/src/Google/Protobuf/Internal/MapEntry.php \
php/src/Google/Protobuf/Internal/FieldDescriptor.php \
php/src/Google/Protobuf/Internal/FieldDescriptorProto.php \
php/src/Google/Protobuf/Internal/InputStream.php \
php/src/Google/Protobuf/Internal/UninterpretedOption.php \
php/src/Google/Protobuf/Internal/ServiceOptions.php \
php/src/Google/Protobuf/Internal/MethodOptions_IdempotencyLevel.php \
php/src/Google/Protobuf/Internal/ServiceDescriptorProto.php \
php/src/Google/Protobuf/Internal/OneofDescriptor.php \
php/src/Google/Protobuf/Internal/OneofDescriptorProto.php \
php/src/Google/Protobuf/Internal/UninterpretedOption_NamePart.php \
php/src/Google/Protobuf/Internal/OutputStream.php \
php/src/Google/Protobuf/Internal/MessageBuilderContext.php \
php/src/Google/Protobuf/Internal/EnumValueDescriptor.php \
php/src/Google/Protobuf/Internal/EnumValueDescriptorProto.php \
php/src/Google/Protobuf/Internal/FileOptions_OptimizeMode.php \
php/src/Google/Protobuf/Internal/DescriptorProto.php \
php/src/Google/Protobuf/Internal/MapField.php \
php/src/Google/Protobuf/Internal/MapFieldIter.php \
php/src/Google/Protobuf/Internal/MethodDescriptorProto.php \
php/src/Google/Protobuf/Internal/DescriptorProto_ExtensionRange.php \
php/src/Google/Protobuf/Internal/DescriptorProto_ReservedRange.php \
php/src/Google/Protobuf/Internal/RepeatedField.php \
php/src/Google/Protobuf/Internal/RepeatedFieldIter.php \
php/src/Google/Protobuf/Internal/EnumValueOptions.php \
php/src/Google/Protobuf/Internal/MethodOptions.php \
php/src/Google/Protobuf/Internal/OneofOptions.php \
php/src/Google/Protobuf/Internal/Message.php \
php/src/Google/Protobuf/Internal/FileOptions.php \
php/src/Google/Protobuf/Internal/FileDescriptorSet.php \
php/src/Google/Protobuf/Internal/DescriptorProto.php \
php/src/Google/Protobuf/Internal/Descriptor.php \
php/src/Google/Protobuf/Internal/EnumBuilderContext.php \
php/src/Google/Protobuf/Internal/EnumDescriptor.php \
php/src/Google/Protobuf/Internal/EnumDescriptorProto.php \
php/src/Google/Protobuf/Internal/GPBWire.php \
php/src/Google/Protobuf/Internal/EnumOptions.php \
php/src/Google/Protobuf/Internal/EnumValueDescriptorProto.php \
php/src/Google/Protobuf/Internal/EnumValueDescriptor.php \
php/src/Google/Protobuf/Internal/EnumValueOptions.php \
php/src/Google/Protobuf/Internal/FieldDescriptorProto_Label.php \
php/src/Google/Protobuf/Internal/FieldDescriptorProto.php \
php/src/Google/Protobuf/Internal/FieldDescriptor.php \
php/src/Google/Protobuf/Internal/FieldDescriptorProto_Type.php \
php/src/Google/Protobuf/Internal/FieldOptions_CType.php \
php/src/Google/Protobuf/Internal/FieldOptions_JSType.php \
php/src/Google/Protobuf/Internal/FieldOptions.php \
php/src/Google/Protobuf/Internal/FileDescriptorProto.php \
php/src/Google/Protobuf/Internal/FileDescriptorSet.php \
php/src/Google/Protobuf/Internal/FileDescriptor.php \
php/src/Google/Protobuf/Internal/FileOptions_OptimizeMode.php \
php/src/Google/Protobuf/Internal/FileOptions.php \
php/src/Google/Protobuf/Internal/GeneratedCodeInfo_Annotation.php \
php/src/Google/Protobuf/Internal/FieldDescriptorProto_Type.php \
php/src/Google/Protobuf/Internal/GeneratedCodeInfo.php \
php/src/Google/Protobuf/Internal/GPBDecodeException.php \
php/src/Google/Protobuf/Internal/GPBJsonWire.php \
php/src/Google/Protobuf/Internal/GPBLabel.php \
php/src/Google/Protobuf/Internal/GPBType.php \
php/src/Google/Protobuf/Internal/FieldOptions_JSType.php \
php/src/Google/Protobuf/Internal/GPBUtil.php \
php/src/Google/Protobuf/Internal/GPBWireType.php \
php/src/Google/Protobuf/Internal/GPBWire.php \
php/src/Google/Protobuf/Internal/MapEntry.php \
php/src/Google/Protobuf/Internal/MapFieldIter.php \
php/src/Google/Protobuf/Internal/MapField.php \
php/src/Google/Protobuf/Internal/MessageBuilderContext.php \
php/src/Google/Protobuf/Internal/MessageOptions.php \
php/src/Google/Protobuf/Internal/Message.php \
php/src/Google/Protobuf/Internal/MethodDescriptorProto.php \
php/src/Google/Protobuf/Internal/MethodOptions_IdempotencyLevel.php \
php/src/Google/Protobuf/Internal/MethodOptions.php \
php/src/Google/Protobuf/Internal/OneofDescriptorProto.php \
php/src/Google/Protobuf/Internal/OneofDescriptor.php \
php/src/Google/Protobuf/Internal/OneofField.php \
php/src/Google/Protobuf/Internal/OneofOptions.php \
php/src/Google/Protobuf/Internal/RawInputStream.php \
php/src/Google/Protobuf/Internal/RepeatedFieldIter.php \
php/src/Google/Protobuf/Internal/RepeatedField.php \
php/src/Google/Protobuf/Internal/ServiceDescriptorProto.php \
php/src/Google/Protobuf/Internal/ServiceOptions.php \
php/src/Google/Protobuf/Internal/SourceCodeInfo_Location.php \
php/src/Google/Protobuf/Internal/SourceCodeInfo.php \
php/src/Google/Protobuf/Internal/EnumOptions.php \
php/src/Google/Protobuf/Internal/GPBLabel.php \
php/src/Google/Protobuf/Internal/EnumBuilderContext.php \
php/src/Google/Protobuf/Internal/GPBUtil.php \
php/src/Google/Protobuf/Internal/FieldOptions_CType.php \
php/src/Google/Protobuf/Internal/GPBDecodeException.php \
php/src/Google/Protobuf/Internal/UninterpretedOption_NamePart.php \
php/src/Google/Protobuf/Internal/UninterpretedOption.php \
php/src/GPBMetadata/Google/Protobuf/Internal/Descriptor.php \
php/tests/array_test.php \
php/tests/autoload.php \
......@@ -673,10 +676,11 @@ php_EXTRA_DIST= \
php/tests/test_base.php \
php/tests/test_util.php \
php/tests/well_known_test.php \
php/tests/undefined_test.php \
php/README.md \
php/phpunit.xml \
php/composer.json \
php/generate_descriptor_protos.sh \
php/phpunit.xml \
composer.json
python_EXTRA_DIST= \
......
......@@ -305,7 +305,7 @@ conformance-csharp: $(other_language_protoc_outputs)
conformance-php:
@echo "Writing shortcut script conformance-php..."
@echo '#! /bin/sh' > conformance-php
@echo 'php ./conformance_php.php' >> conformance-php
@echo 'php -d auto_prepend_file=autoload.php ./conformance_php.php' >> conformance-php
@chmod +x conformance-php
conformance-php-c:
......@@ -336,6 +336,9 @@ test_php: protoc_middleman conformance-test-runner conformance-php $(other_langu
test_php_c: protoc_middleman conformance-test-runner conformance-php-c $(other_language_protoc_outputs)
./conformance-test-runner --enforce_recommended --failure_list failure_list_php_c.txt ./conformance-php-c
test_php_zts_c: protoc_middleman conformance-test-runner conformance-php-c $(other_language_protoc_outputs)
./conformance-test-runner --enforce_recommended --failure_list failure_list_php_zts_c.txt ./conformance-php-c
# These depend on library paths being properly set up. The easiest way to
# run them is to just use "tox" from the python dir.
test_python: protoc_middleman conformance-test-runner
......
<?php
define("GOOGLE_INTERNAL_NAMESPACE", "Google\\Protobuf\\Internal\\");
define("GOOGLE_NAMESPACE", "Google\\Protobuf\\");
define("GOOGLE_GPBMETADATA_NAMESPACE", "GPBMetadata\\Google\\Protobuf\\Internal\\");
function protobuf_autoloader_impl($class, $prefix) {
$length = strlen($prefix);
if ((substr($class, 0, $length) === $prefix)) {
$path = '../php/src/' . implode('/', array_map('ucwords', explode('\\', $class))) . '.php';
include_once $path;
}
}
function protobuf_autoloader($class) {
protobuf_autoloader_impl($class, GOOGLE_INTERNAL_NAMESPACE);
protobuf_autoloader_impl($class, GOOGLE_NAMESPACE);
protobuf_autoloader_impl($class, GOOGLE_GPBMETADATA_NAMESPACE);
}
spl_autoload_register('protobuf_autoloader');
......@@ -53,7 +53,7 @@ function doTest($request)
}
} elseif ($request->getPayload() == "json_payload") {
try {
$test_message->jsonDecode($request->getJsonPayload());
$test_message->mergeFromJsonString($request->getJsonPayload());
} catch (Exception $e) {
$response->setParseError($e->getMessage());
return $response;
......@@ -67,7 +67,7 @@ function doTest($request)
} elseif ($request->getRequestedOutputFormat() == WireFormat::PROTOBUF) {
$response->setProtobufPayload($test_message->serializeToString());
} elseif ($request->getRequestedOutputFormat() == WireFormat::JSON) {
$response->setJsonPayload($test_message->jsonEncode());
$response->setJsonPayload($test_message->serializeToJsonString());
}
return $response;
......@@ -79,7 +79,8 @@ function doTestIO()
if (strlen($length_bytes) == 0) {
return false; # EOF
} elseif (strlen($length_bytes) != 4) {
trigger_error("I/O error", E_USER_ERROR);
fwrite(STDERR, "I/O error\n");
return false;
}
$length = unpack("V", $length_bytes)[1];
......
This diff is collapsed.
......@@ -9,22 +9,10 @@ Recommended.JsonInput.DurationHas9FractionalDigits.Validator
Recommended.JsonInput.DurationHasZeroFractionalDigit.Validator
Recommended.JsonInput.Int64FieldBeString.Validator
Recommended.JsonInput.MapFieldValueIsNull
Recommended.JsonInput.OneofZeroBool.JsonOutput
Recommended.JsonInput.OneofZeroBool.ProtobufOutput
Recommended.JsonInput.OneofZeroBytes.JsonOutput
Recommended.JsonInput.OneofZeroBytes.ProtobufOutput
Recommended.JsonInput.OneofZeroDouble.JsonOutput
Recommended.JsonInput.OneofZeroDouble.ProtobufOutput
Recommended.JsonInput.OneofZeroEnum.JsonOutput
Recommended.JsonInput.OneofZeroEnum.ProtobufOutput
Recommended.JsonInput.OneofZeroFloat.JsonOutput
Recommended.JsonInput.OneofZeroFloat.ProtobufOutput
Recommended.JsonInput.OneofZeroString.JsonOutput
Recommended.JsonInput.OneofZeroString.ProtobufOutput
Recommended.JsonInput.OneofZeroUint32.JsonOutput
Recommended.JsonInput.OneofZeroUint32.ProtobufOutput
Recommended.JsonInput.OneofZeroUint64.JsonOutput
Recommended.JsonInput.OneofZeroUint64.ProtobufOutput
Recommended.JsonInput.RepeatedFieldMessageElementIsNull
Recommended.JsonInput.RepeatedFieldPrimitiveElementIsNull
Recommended.JsonInput.StringEndsWithEscapeChar
......@@ -37,25 +25,12 @@ Recommended.JsonInput.TimestampHas9FractionalDigits.Validator
Recommended.JsonInput.TimestampHasZeroFractionalDigit.Validator
Recommended.JsonInput.TimestampZeroNormalized.Validator
Recommended.JsonInput.Uint64FieldBeString.Validator
Recommended.ProtobufInput.OneofZeroBool.JsonOutput
Recommended.ProtobufInput.OneofZeroBool.ProtobufOutput
Recommended.ProtobufInput.OneofZeroBytes.JsonOutput
Recommended.ProtobufInput.OneofZeroBytes.ProtobufOutput
Recommended.ProtobufInput.OneofZeroDouble.JsonOutput
Recommended.ProtobufInput.OneofZeroDouble.ProtobufOutput
Recommended.ProtobufInput.OneofZeroEnum.JsonOutput
Recommended.ProtobufInput.OneofZeroEnum.ProtobufOutput
Recommended.ProtobufInput.OneofZeroFloat.JsonOutput
Recommended.ProtobufInput.OneofZeroFloat.ProtobufOutput
Recommended.ProtobufInput.OneofZeroString.JsonOutput
Recommended.ProtobufInput.OneofZeroString.ProtobufOutput
Recommended.ProtobufInput.OneofZeroUint32.JsonOutput
Recommended.ProtobufInput.OneofZeroUint32.ProtobufOutput
Recommended.ProtobufInput.OneofZeroUint64.JsonOutput
Recommended.ProtobufInput.OneofZeroUint64.ProtobufOutput
Required.DurationProtoInputTooLarge.JsonOutput
Required.DurationProtoInputTooSmall.JsonOutput
Required.JsonInput.AllFieldAcceptNull.ProtobufOutput
Required.JsonInput.Any.JsonOutput
Required.JsonInput.Any.ProtobufOutput
Required.JsonInput.AnyNested.JsonOutput
......@@ -76,7 +51,6 @@ Required.JsonInput.AnyWithValueForInteger.JsonOutput
Required.JsonInput.AnyWithValueForInteger.ProtobufOutput
Required.JsonInput.AnyWithValueForJsonObject.JsonOutput
Required.JsonInput.AnyWithValueForJsonObject.ProtobufOutput
Required.JsonInput.BoolFieldFalse.ProtobufOutput
Required.JsonInput.BoolMapField.JsonOutput
Required.JsonInput.DoubleFieldInfinity.JsonOutput
Required.JsonInput.DoubleFieldInfinity.ProtobufOutput
......@@ -100,7 +74,6 @@ Required.JsonInput.DurationMinValue.JsonOutput
Required.JsonInput.DurationMinValue.ProtobufOutput
Required.JsonInput.DurationRepeatedValue.JsonOutput
Required.JsonInput.DurationRepeatedValue.ProtobufOutput
Required.JsonInput.EnumField.ProtobufOutput
Required.JsonInput.EnumFieldNumericValueNonZero.JsonOutput
Required.JsonInput.EnumFieldNumericValueNonZero.ProtobufOutput
Required.JsonInput.EnumFieldNumericValueZero.JsonOutput
......@@ -215,13 +188,10 @@ Required.JsonInput.ValueAcceptObject.JsonOutput
Required.JsonInput.ValueAcceptObject.ProtobufOutput
Required.JsonInput.ValueAcceptString.JsonOutput
Required.JsonInput.ValueAcceptString.ProtobufOutput
Required.JsonInput.WrapperTypesWithNullValue.ProtobufOutput
Required.ProtobufInput.DoubleFieldNormalizeQuietNan.JsonOutput
Required.ProtobufInput.DoubleFieldNormalizeSignalingNan.JsonOutput
Required.ProtobufInput.FloatFieldNormalizeQuietNan.JsonOutput
Required.ProtobufInput.FloatFieldNormalizeSignalingNan.JsonOutput
Required.ProtobufInput.RepeatedScalarSelectsLast.FIXED32.ProtobufOutput
Required.ProtobufInput.RepeatedScalarSelectsLast.FIXED64.ProtobufOutput
Required.ProtobufInput.RepeatedScalarSelectsLast.UINT64.ProtobufOutput
Required.ProtobufInput.ValidDataRepeated.FLOAT.JsonOutput
Required.TimestampProtoInputTooLarge.JsonOutput
Required.TimestampProtoInputTooSmall.JsonOutput
This diff is collapsed.
......@@ -164,18 +164,21 @@ typedef struct {
int property_ofs; // properties table cache
uint32_t oneof_case_num; // oneof-case number to place in oneof_case field
const upb_msgdef *md; // msgdef, for oneof submessage handler
const upb_msgdef *parent_md; // msgdef, for parent submessage
} oneof_handlerdata_t;
static const void *newoneofhandlerdata(upb_handlers *h,
uint32_t ofs,
uint32_t case_ofs,
int property_ofs,
const upb_msgdef *m,
const upb_fielddef *f) {
oneof_handlerdata_t* hd =
(oneof_handlerdata_t*)malloc(sizeof(oneof_handlerdata_t));
hd->ofs = ofs;
hd->case_ofs = case_ofs;
hd->property_ofs = property_ofs;
hd->parent_md = m;
// We reuse the field tag number as a oneof union discriminant tag. Note that
// we don't expose these numbers to the user, so the only requirement is that
// we have some unique ID for each union case/possibility. The field tag
......@@ -284,10 +287,19 @@ DEFINE_SINGULAR_HANDLER(double, double)
#if PHP_MAJOR_VERSION < 7
static void *empty_php_string(zval** value_ptr) {
SEPARATE_ZVAL_IF_NOT_REF(value_ptr);
if (Z_TYPE_PP(value_ptr) == IS_STRING &&
!IS_INTERNED(Z_STRVAL_PP(value_ptr))) {
FREE(Z_STRVAL_PP(value_ptr));
}
ZVAL_EMPTY_STRING(*value_ptr);
return (void*)(*value_ptr);
}
#else
static void *empty_php_string(zval* value_ptr) {
if (Z_TYPE_P(value_ptr) == IS_STRING) {
zend_string_release(Z_STR_P(value_ptr));
}
ZVAL_EMPTY_STRING(value_ptr);
return value_ptr;
}
#endif
......@@ -462,7 +474,7 @@ static void map_slot_init(void* memory, upb_fieldtype_t type, zval* cache) {
*(zval***)memory = holder;
#else
*(zval**)memory = cache;
PHP_PROTO_ZVAL_STRINGL(*(zval**)memory, "", 0, 1);
// PHP_PROTO_ZVAL_STRINGL(*(zval**)memory, "", 0, 1);
#endif
break;
}
......@@ -654,6 +666,44 @@ DEFINE_ONEOF_HANDLER(double, double)
#undef DEFINE_ONEOF_HANDLER
static void oneof_cleanup(MessageHeader* msg,
const oneof_handlerdata_t* oneofdata) {
uint32_t old_case_num =
DEREF(message_data(msg), oneofdata->case_ofs, uint32_t);
if (old_case_num == 0) {
return;
}
const upb_fielddef* old_field =
upb_msgdef_itof(oneofdata->parent_md, old_case_num);
bool need_clean = false;
switch (upb_fielddef_type(old_field)) {
case UPB_TYPE_STRING:
case UPB_TYPE_BYTES:
need_clean = true;
break;
case UPB_TYPE_MESSAGE:
if (oneofdata->oneof_case_num != old_case_num) {
need_clean = true;
}
break;
default:
break;
}
if (need_clean) {
#if PHP_MAJOR_VERSION < 7
SEPARATE_ZVAL_IF_NOT_REF(
DEREF(message_data(msg), oneofdata->ofs, CACHED_VALUE*));
php_proto_zval_ptr_dtor(
*DEREF(message_data(msg), oneofdata->ofs, CACHED_VALUE*));
MAKE_STD_ZVAL(*DEREF(message_data(msg), oneofdata->ofs, CACHED_VALUE*));
ZVAL_NULL(*DEREF(message_data(msg), oneofdata->ofs, CACHED_VALUE*));
#endif
}
}
// Handlers for string/bytes in a oneof.
static void *oneofbytes_handler(void *closure,
const void *hd,
......@@ -661,6 +711,8 @@ static void *oneofbytes_handler(void *closure,
MessageHeader* msg = closure;
const oneof_handlerdata_t *oneofdata = hd;
oneof_cleanup(msg, oneofdata);
DEREF(message_data(msg), oneofdata->case_ofs, uint32_t) =
oneofdata->oneof_case_num;
DEREF(message_data(msg), oneofdata->ofs, CACHED_VALUE*) =
......@@ -691,22 +743,11 @@ static void* oneofsubmsg_handler(void* closure, const void* hd) {
MessageHeader* submsg;
if (oldcase != oneofdata->oneof_case_num) {
// Ideally, we should clean up the old data. However, we don't even know the
// type of the old data. So, we will defer the desctruction of the old data
// to the time that containing message's destroyed or the same oneof field
// is accessed again and find that the old data hasn't been cleaned.
DEREF(message_data(msg), oneofdata->ofs, CACHED_VALUE*) =
&(msg->std.properties_table)[oneofdata->property_ofs];
// Old data was't cleaned when the oneof was accessed from another field.
if (Z_TYPE_P(CACHED_PTR_TO_ZVAL_PTR(DEREF(
message_data(msg), oneofdata->ofs, CACHED_VALUE*))) != IS_NULL) {
php_proto_zval_ptr_dtor(
CACHED_PTR_TO_ZVAL_PTR(
DEREF(message_data(msg), oneofdata->ofs, CACHED_VALUE*)));
}
oneof_cleanup(msg, oneofdata);
// Create new message.
DEREF(message_data(msg), oneofdata->ofs, CACHED_VALUE*) =
&(msg->std.properties_table)[oneofdata->property_ofs];
ZVAL_OBJ(CACHED_PTR_TO_ZVAL_PTR(
DEREF(message_data(msg), oneofdata->ofs, CACHED_VALUE*)),
subklass->create_object(subklass TSRMLS_CC));
......@@ -856,6 +897,7 @@ static void add_handlers_for_mapentry(const upb_msgdef* msgdef, upb_handlers* h,
// Set up handlers for a oneof field.
static void add_handlers_for_oneof_field(upb_handlers *h,
const upb_msgdef *m,
const upb_fielddef *f,
size_t offset,
size_t oneof_case_offset,
......@@ -864,7 +906,7 @@ static void add_handlers_for_oneof_field(upb_handlers *h,
upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
upb_handlerattr_sethandlerdata(
&attr, newoneofhandlerdata(h, offset, oneof_case_offset,
property_cache_offset, f));
property_cache_offset, m, f));
switch (upb_fielddef_type(f)) {
......@@ -936,8 +978,8 @@ static void add_handlers_for_message(const void* closure,
desc->layout->fields[upb_fielddef_index(f)].case_offset;
int property_cache_index =
desc->layout->fields[upb_fielddef_index(f)].cache_index;
add_handlers_for_oneof_field(h, f, offset, oneof_case_offset,
property_cache_index);
add_handlers_for_oneof_field(h, desc->msgdef, f, offset,
oneof_case_offset, property_cache_index);
} else if (is_map_field(f)) {
add_handlers_for_mapfield(h, f, offset, desc);
} else if (upb_fielddef_isseq(f)) {
......@@ -1198,7 +1240,7 @@ static void putrawmsg(MessageHeader* msg, const Descriptor* desc,
} else if (upb_fielddef_isstring(f)) {
zval* str = CACHED_PTR_TO_ZVAL_PTR(
DEREF(message_data(msg), offset, CACHED_VALUE*));
if (Z_STRLEN_P(str) > 0) {
if (containing_oneof || Z_STRLEN_P(str) > 0) {
putstr(str, f, sink);
}
} else if (upb_fielddef_issubmsg(f)) {
......@@ -1221,10 +1263,10 @@ static void putrawmsg(MessageHeader* msg, const Descriptor* desc,
T(UPB_TYPE_DOUBLE, double, double, 0.0)
T(UPB_TYPE_BOOL, bool, uint8_t, 0)
case UPB_TYPE_ENUM:
T(UPB_TYPE_INT32, int32, int32_t, 0)
T(UPB_TYPE_UINT32, uint32, uint32_t, 0)
T(UPB_TYPE_INT64, int64, int64_t, 0)
T(UPB_TYPE_UINT64, uint64, uint64_t, 0)
T(UPB_TYPE_INT32, int32, int32_t, 0)
T(UPB_TYPE_UINT32, uint32, uint32_t, 0)
T(UPB_TYPE_INT64, int64, int64_t, 0)
T(UPB_TYPE_UINT64, uint64, uint64_t, 0)
case UPB_TYPE_STRING:
case UPB_TYPE_BYTES:
......@@ -1246,18 +1288,23 @@ static void putstr(zval* str, const upb_fielddef *f, upb_sink *sink) {
assert(Z_TYPE_P(str) == IS_STRING);
// Ensure that the string has the correct encoding. We also check at field-set
// time, but the user may have mutated the string object since then.
if (upb_fielddef_type(f) == UPB_TYPE_STRING &&
!is_structurally_valid_utf8(Z_STRVAL_P(str), Z_STRLEN_P(str))) {
zend_error(E_USER_ERROR, "Given string is not UTF8 encoded.");
return;
}
upb_sink_startstr(sink, getsel(f, UPB_HANDLER_STARTSTR), Z_STRLEN_P(str),
&subsink);
upb_sink_putstring(&subsink, getsel(f, UPB_HANDLER_STRING), Z_STRVAL_P(str),
Z_STRLEN_P(str), NULL);
// For oneof string field, we may get here with string length is zero.
if (Z_STRLEN_P(str) > 0) {
// Ensure that the string has the correct encoding. We also check at
// field-set time, but the user may have mutated the string object since
// then.
if (upb_fielddef_type(f) == UPB_TYPE_STRING &&
!is_structurally_valid_utf8(Z_STRVAL_P(str), Z_STRLEN_P(str))) {
zend_error(E_USER_ERROR, "Given string is not UTF8 encoded.");
return;
}
upb_sink_putstring(&subsink, getsel(f, UPB_HANDLER_STRING), Z_STRVAL_P(str),
Z_STRLEN_P(str), NULL);
}
upb_sink_endstr(sink, getsel(f, UPB_HANDLER_ENDSTR));
}
......@@ -1452,7 +1499,7 @@ PHP_METHOD(Message, mergeFromString) {
}
}
PHP_METHOD(Message, jsonEncode) {
PHP_METHOD(Message, serializeToJsonString) {
Descriptor* desc =
UNBOX_HASHTABLE_VALUE(Descriptor, get_ce_obj(Z_OBJCE_P(getThis())));
......@@ -1483,13 +1530,14 @@ PHP_METHOD(Message, jsonEncode) {
}
}
PHP_METHOD(Message, jsonDecode) {
PHP_METHOD(Message, mergeFromJsonString) {
Descriptor* desc =
UNBOX_HASHTABLE_VALUE(Descriptor, get_ce_obj(Z_OBJCE_P(getThis())));
MessageHeader* msg = UNBOX(MessageHeader, getThis());
char *data = NULL;
int data_len;
PHP_PROTO_SIZE data_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len) ==
FAILURE) {
return;
......
......@@ -40,8 +40,8 @@ static zend_function_entry message_methods[] = {
PHP_ME(Message, clear, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Message, serializeToString, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Message, mergeFromString, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Message, jsonEncode, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Message, jsonDecode, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Message, serializeToJsonString, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Message, mergeFromJsonString, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Message, mergeFrom, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Message, readOneof, NULL, ZEND_ACC_PROTECTED)
PHP_ME(Message, writeOneof, NULL, ZEND_ACC_PROTECTED)
......
......@@ -593,8 +593,8 @@ const upb_pbdecodermethod *new_fillmsg_decodermethod(Descriptor *desc,
PHP_METHOD(Message, serializeToString);
PHP_METHOD(Message, mergeFromString);
PHP_METHOD(Message, jsonEncode);
PHP_METHOD(Message, jsonDecode);
PHP_METHOD(Message, serializeToJsonString);
PHP_METHOD(Message, mergeFromJsonString);
// -----------------------------------------------------------------------------
// Type check / conversion.
......
......@@ -2308,6 +2308,9 @@ bool upb_symtab_addfile(upb_symtab *s, upb_filedef *file, upb_status *status) {
bool ret;
n = upb_filedef_defcount(file);
if (n == 0) {
return true;
}
defs = upb_gmalloc(sizeof(*defs) * n);
if (defs == NULL) {
......
......@@ -34,7 +34,7 @@ namespace Google\Protobuf\Internal;
use Google\Protobuf\Internal\Uint64;
class InputStream
class CodedInputStream
{
private $buffer;
......@@ -73,7 +73,7 @@ class InputStream
$this->current += $amount;
}
private function bufferSize()
public function bufferSize()
{
return $this->buffer_end - $this->current;
}
......@@ -172,6 +172,9 @@ class InputStream
} while ($b & 0x80);
$var = GPBUtil::combineInt32ToInt64($high, $low);
if (bccomp($var, 0) < 0) {
$var = bcadd($var, "18446744073709551616");
}
} else {
$result = 0;
$shift = 0;
......
......@@ -32,7 +32,7 @@
namespace Google\Protobuf\Internal;
class OutputStream
class CodedOutputStream
{
private $buffer;
......@@ -53,10 +53,10 @@ class OutputStream
return $this->buffer;
}
public function writeVarint32($value)
public function writeVarint32($value, $trim)
{
$bytes = str_repeat(chr(0), self::MAX_VARINT64_BYTES);
$size = self::writeVarintToArray($value, $bytes);
$size = self::writeVarintToArray($value, $bytes, $trim);
return $this->writeRaw($bytes, $size);
}
......@@ -83,7 +83,7 @@ class OutputStream
public function writeTag($tag)
{
return $this->writeVarint32($tag);
return $this->writeVarint32($tag, true);
}
public function writeRaw($data, $size)
......@@ -101,19 +101,19 @@ class OutputStream
return true;
}
private static function writeVarintToArray($value, &$buffer)
private static function writeVarintToArray($value, &$buffer, $trim = false)
{
$current = 0;
$high = 0;
$low = 0;
if (PHP_INT_SIZE == 4) {
GPBUtil::divideInt64ToInt32($value, $high, $low);
GPBUtil::divideInt64ToInt32($value, $high, $low, $trim);
} else {
$low = $value;
}
while ($low >= 0x80 || $low < 0) {
while (($low >= 0x80 || $low < 0) || $high != 0) {
$buffer[$current] = chr($low | 0x80);
$value = ($value >> 7) & ~(0x7F << ((PHP_INT_SIZE << 3) - 7));
$carry = ($high & 0x7F) << ((PHP_INT_SIZE << 3) - 7);
......
......@@ -37,6 +37,8 @@ class Descriptor
private $full_name;
private $field = [];
private $json_to_field = [];
private $name_to_field = [];
private $nested_type = [];
private $enum_type = [];
private $klass;
......@@ -66,6 +68,8 @@ class Descriptor
public function addField($field)
{
$this->field[$field->getNumber()] = $field;
$this->json_to_field[$field->getJsonName()] = $field;
$this->name_to_field[$field->getName()] = $field;
}
public function getField()
......@@ -95,11 +99,29 @@ class Descriptor
public function getFieldByNumber($number)
{
if (!isset($this->field[$number])) {
return NULL;
} else {
return $this->field[$number];
}
if (!isset($this->field[$number])) {
return NULL;
} else {
return $this->field[$number];
}
}
public function getFieldByJsonName($json_name)
{
if (!isset($this->json_to_field[$json_name])) {
return NULL;
} else {
return $this->json_to_field[$json_name];
}
}
public function getFieldByName($name)
{
if (!isset($this->name_to_field[$name])) {
return NULL;
} else {
return $this->name_to_field[$name];
}
}
public function setClass($klass)
......
......@@ -8,6 +8,7 @@ class EnumDescriptor
private $klass;
private $full_name;
private $value;
private $name_to_value;
public function setFullName($full_name)
{
......@@ -22,6 +23,17 @@ class EnumDescriptor
public function addValue($number, $value)
{
$this->value[$number] = $value;
$this->name_to_value[$value->getName()] = $value;
}
public function getValueByNumber($number)
{
return $this->value[$number];
}
public function getValueByName($name)
{
return $this->name_to_value[$name];
}
public function setClass($klass)
......@@ -50,6 +62,10 @@ class EnumDescriptor
$fullname);
$desc->setFullName($fullname);
$desc->setClass($classname);
$values = $proto->getValue();
foreach ($values as $value) {
$desc->addValue($value->getNumber(), $value);
}
return $desc;
}
......
......@@ -34,4 +34,26 @@ namespace Google\Protobuf\Internal;
class EnumValueDescriptor
{
private $name;
private $number;
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
public function setNumber($number)
{
$this->number = $number;
}
public function getNumber()
{
return $this->number;
}
}
......@@ -36,6 +36,7 @@ class FieldDescriptor
{
private $name;
private $json_name;
private $setter;
private $getter;
private $number;
......@@ -67,6 +68,16 @@ class FieldDescriptor
return $this->name;
}
public function setJsonName($json_name)
{
$this->json_name = $json_name;
}
public function getJsonName()
{
return $this->json_name;
}
public function setSetter($setter)
{
$this->setter = $setter;
......@@ -172,23 +183,49 @@ class FieldDescriptor
$field_type !== GPBType::BYTES);
}
public static function getFieldDescriptor(
$name,
$label,
$type,
$number,
$oneof_index,
$packed,
$type_name = null)
public static function getFieldDescriptor($proto)
{
$type_name = null;
$type = $proto->getType();
switch ($type) {
case GPBType::MESSAGE:
case GPBType::GROUP:
case GPBType::ENUM:
$type_name = $proto->getTypeName();
break;
default:
break;
}
$oneof_index = $proto->hasOneofIndex() ? $proto->getOneofIndex() : -1;
$packed = false;
$options = $proto->getOptions();
if ($options !== null) {
$packed = $options->getPacked();
}
$field = new FieldDescriptor();
$field->setName($name);
$camel_name = implode('', array_map('ucwords', explode('_', $name)));
$field->setName($proto->getName());
$json_name = $proto->hasJsonName() ? $proto->getJsonName() :
lcfirst(implode('', array_map('ucwords', explode('_', $proto->getName()))));
if ($proto->hasJsonName()) {
$json_name = $proto->getJsonName();
} else {
$proto_name = $proto->getName();
$json_name = implode('', array_map('ucwords', explode('_', $proto_name)));
if ($proto_name[0] !== "_" && !ctype_upper($proto_name[0])) {
$json_name = lcfirst($json_name);
}
}
$field->setJsonName($json_name);
$camel_name = implode('', array_map('ucwords', explode('_', $proto->getName())));
$field->setGetter('get' . $camel_name);
$field->setSetter('set' . $camel_name);
$field->setType($type);
$field->setNumber($number);
$field->setLabel($label);
$field->setType($proto->getType());
$field->setNumber($proto->getNumber());
$field->setLabel($proto->getLabel());
$field->setPacked($packed);
$field->setOneofIndex($oneof_index);
......@@ -211,26 +248,6 @@ class FieldDescriptor
public static function buildFromProto($proto)
{
$type_name = null;
switch ($proto->getType()) {
case GPBType::MESSAGE:
case GPBType::GROUP:
case GPBType::ENUM:
$type_name = $proto->getTypeName();
break;
default:
break;
}
$oneof_index = $proto->hasOneofIndex() ? $proto->getOneofIndex() : -1;
$packed = false;
$options = $proto->getOptions();
if ($options !== null) {
$packed = $options->getPacked();
}
return FieldDescriptor::getFieldDescriptor(
$proto->getName(), $proto->getLabel(), $proto->getType(),
$proto->getNumber(), $oneof_index, $packed, $type_name);
return FieldDescriptor::getFieldDescriptor($proto);
}
}
This diff is collapsed.
......@@ -45,8 +45,13 @@ class GPBUtil
$value = bcsub(0, $value);
}
$high = (int) bcdiv(bcadd($value, 1), 4294967296);
$high = bcdiv($value, 4294967296);
$low = bcmod($value, 4294967296);
if (bccomp($high, 2147483647) > 0) {
$high = (int) bcsub($high, 4294967296);
} else {
$high = (int) $high;
}
if (bccomp($low, 2147483647) > 0) {
$low = (int) bcsub($low, 4294967296);
} else {
......@@ -58,7 +63,7 @@ class GPBUtil
$low = ~$low;
$low++;
if (!$low) {
$high++;
$high = (int)($high + 1);
}
}
......@@ -70,15 +75,13 @@ class GPBUtil
public static function checkString(&$var, $check_utf8)
{
if (is_array($var) || is_object($var)) {
trigger_error("Expect string.", E_USER_ERROR);
return;
throw new \InvalidArgumentException("Expect string.");
}
if (!is_string($var)) {
$var = strval($var);
}
if ($check_utf8 && !preg_match('//u', $var)) {
trigger_error("Expect utf-8 encoding.", E_USER_ERROR);
return;
throw new \Exception("Expect utf-8 encoding.");
}
}
......@@ -92,7 +95,7 @@ class GPBUtil
if (is_numeric($var)) {
$var = intval($var);
} else {
trigger_error("Expect integer.", E_USER_ERROR);
throw new \Exception("Expect integer.");
}
}
......@@ -109,7 +112,7 @@ class GPBUtil
$var = (int) $var;
}
} else {
trigger_error("Expect integer.", E_USER_ERROR);
throw new \Exception("Expect integer.");
}
}
......@@ -119,10 +122,15 @@ class GPBUtil
if (PHP_INT_SIZE == 8) {
$var = intval($var);
} else {
$var = bcdiv($var, 1, 0);
if (is_float($var) ||
is_integer($var) ||
(is_string($var) &&
bccomp($var, "9223372036854774784") < 0)) {
$var = number_format($var, 0, ".", "");
}
}
} else {
trigger_error("Expect integer.", E_USER_ERROR);
throw new \Exception("Expect integer.");
}
}
......@@ -132,10 +140,10 @@ class GPBUtil
if (PHP_INT_SIZE == 8) {
$var = intval($var);
} else {
$var = bcdiv($var, 1, 0);
$var = number_format($var, 0, ".", "");
}
} else {
trigger_error("Expect integer.", E_USER_ERROR);
throw new \Exception("Expect integer.");
}
}
......@@ -144,7 +152,7 @@ class GPBUtil
if (is_float($var) || is_numeric($var)) {
$var = floatval($var);
} else {
trigger_error("Expect float.", E_USER_ERROR);
throw new \Exception("Expect float.");
}
}
......@@ -153,15 +161,14 @@ class GPBUtil
if (is_float($var) || is_numeric($var)) {
$var = floatval($var);
} else {
trigger_error("Expect float.", E_USER_ERROR);
throw new \Exception("Expect float.");
}
}
public static function checkBool(&$var)
{
if (is_array($var) || is_object($var)) {
trigger_error("Expect boolean.", E_USER_ERROR);
return;
throw new \Exception("Expect boolean.");
}
$var = boolval($var);
}
......@@ -169,14 +176,14 @@ class GPBUtil
public static function checkMessage(&$var, $klass)
{
if (!$var instanceof $klass && !is_null($var)) {
trigger_error("Expect message.", E_USER_ERROR);
throw new \Exception("Expect message.");
}
}
public static function checkRepeatedField(&$var, $type, $klass = null)
{
if (!$var instanceof RepeatedField && !is_array($var)) {
trigger_error("Expect array.", E_USER_ERROR);
throw new \Exception("Expect array.");
}
if (is_array($var)) {
$tmp = new RepeatedField($type, $klass);
......@@ -186,15 +193,13 @@ class GPBUtil
return $tmp;
} else {
if ($var->getType() != $type) {
trigger_error(
"Expect repeated field of different type.",
E_USER_ERROR);
throw new \Exception(
"Expect repeated field of different type.");
}
if ($var->getType() === GPBType::MESSAGE &&
$var->getClass() !== $klass) {
trigger_error(
"Expect repeated field of different message.",
E_USER_ERROR);
throw new \Exception(
"Expect repeated field of different message.");
}
return $var;
}
......@@ -203,7 +208,7 @@ class GPBUtil
public static function checkMapField(&$var, $key_type, $value_type, $klass = null)
{
if (!$var instanceof MapField && !is_array($var)) {
trigger_error("Expect dict.", E_USER_ERROR);
throw new \Exception("Expect dict.");
}
if (is_array($var)) {
$tmp = new MapField($key_type, $value_type, $klass);
......@@ -213,20 +218,15 @@ class GPBUtil
return $tmp;
} else {
if ($var->getKeyType() != $key_type) {
trigger_error(
"Expect map field of key type.",
E_USER_ERROR);
throw new \Exception("Expect map field of key type.");
}
if ($var->getValueType() != $value_type) {
trigger_error(
"Expect map field of value type.",
E_USER_ERROR);
throw new \Exception("Expect map field of value type.");
}
if ($var->getValueType() === GPBType::MESSAGE &&
$var->getValueClass() !== $klass) {
trigger_error(
"Expect map field of different value message.",
E_USER_ERROR);
throw new \Exception(
"Expect map field of different value message.");
}
return $var;
}
......@@ -328,7 +328,7 @@ class GPBUtil
$low = ~$low;
$low++;
if (!$low) {
$high++;
$high = (int) ($high + 1);
}
}
$result = bcadd(bcmul($high, 4294967296), $low);
......
......@@ -117,19 +117,12 @@ class GPBWire
// << decode <<
public static function zigZagEncode32($int32)
{
// Fill high 32 bits.
if (PHP_INT_SIZE === 8) {
$int32 |= ((($int32 << 32) >> 31) & (0xFFFFFFFF << 32));
if (PHP_INT_SIZE == 8) {
$trim_int32 = $int32 & 0xFFFFFFFF;
return (($trim_int32 << 1) ^ ($int32 << 32 >> 63)) & 0xFFFFFFFF;
} else {
return ($int32 << 1) ^ ($int32 >> 31);
}
$uint32 = ($int32 << 1) ^ ($int32 >> 31);
// Fill high 32 bits.
if (PHP_INT_SIZE === 8) {
$uint32 |= ((($uint32 << 32) >> 31) & (0xFFFFFFFF << 32));
}
return $uint32;
}
public static function zigZagDecode32($uint32)
......@@ -177,7 +170,11 @@ class GPBWire
public static function readInt64(&$input, &$value)
{
return $input->readVarint64($value);
$success = $input->readVarint64($value);
if (PHP_INT_SIZE == 4 && bccomp($value, "9223372036854775807") > 0) {
$value = bcsub($value, "18446744073709551616");
}
return $success;
}
public static function readUint32(&$input, &$value)
......@@ -231,7 +228,11 @@ class GPBWire
public static function readSfixed64(&$input, &$value)
{
return $input->readLittleEndian64($value);
$success = $input->readLittleEndian64($value);
if (PHP_INT_SIZE == 4 && bccomp($value, "9223372036854775807") > 0) {
$value = bcsub($value, "18446744073709551616");
}
return $success;
}
public static function readFloat(&$input, &$value)
......@@ -298,7 +299,7 @@ class GPBWire
public static function writeInt32(&$output, $value)
{
return $output->writeVarint32($value);
return $output->writeVarint32($value, false);
}
public static function writeInt64(&$output, $value)
......@@ -308,7 +309,7 @@ class GPBWire
public static function writeUint32(&$output, $value)
{
return $output->writeVarint32($value);
return $output->writeVarint32($value, true);
}
public static function writeUint64(&$output, $value)
......@@ -319,7 +320,7 @@ class GPBWire
public static function writeSint32(&$output, $value)
{
$value = GPBWire::zigZagEncode32($value);
return $output->writeVarint64($value);
return $output->writeVarint32($value, true);
}
public static function writeSint64(&$output, $value)
......@@ -351,9 +352,9 @@ class GPBWire
public static function writeBool(&$output, $value)
{
if ($value) {
return $output->writeVarint32(1);
return $output->writeVarint32(1, true);
} else {
return $output->writeVarint32(0);
return $output->writeVarint32(0, true);
}
}
......@@ -377,7 +378,7 @@ class GPBWire
public static function writeBytes(&$output, $value)
{
$size = strlen($value);
if (!$output->writeVarint32($size)) {
if (!$output->writeVarint32($size, true)) {
return false;
}
return $output->writeRaw($value, $size);
......@@ -386,7 +387,7 @@ class GPBWire
public static function writeMessage(&$output, $value)
{
$size = $value->byteSize();
if (!$output->writeVarint32($size)) {
if (!$output->writeVarint32($size, true)) {
return false;
}
return $value->serializeToStream($output);
......@@ -442,7 +443,8 @@ class GPBWire
public static function varint64Size($value)
{
if (PHP_INT_SIZE == 4) {
if (bccomp($value, 0) < 0) {
if (bccomp($value, 0) < 0 ||
bccomp($value, "9223372036854775807") > 0) {
return 10;
}
if (bccomp($value, 1 << 7) < 0) {
......@@ -578,6 +580,9 @@ class GPBWire
}
break;
case GPBType::UINT32:
if (PHP_INT_SIZE === 8 && $value < 0) {
$value += 4294967296;
}
if (!GPBWire::writeUint32($output, $value)) {
return false;
}
......
<?php
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
namespace Google\Protobuf\Internal;
class GPBWireType
{
const VARINT = 0;
const FIXED64 = 1;
const LENGTH_DELIMITED = 2;
const START_GROUP = 3;
const END_GROUP = 4;
const FIXED32 = 5;
}
......@@ -205,7 +205,7 @@ class MapField implements \ArrayAccess, \IteratorAggregate, \Countable
*/
public function getIterator()
{
return new MapFieldIter($this->container);
return new MapFieldIter($this->container, $this->key_type);
}
/**
......
......@@ -54,11 +54,13 @@ class MapFieldIter implements \Iterator
*
* @param MapField The MapField instance for which this iterator is
* created.
* @param GPBType Map key type.
* @ignore
*/
public function __construct($container)
public function __construct($container, $key_type)
{
$this->container = $container;
$this->key_type = $key_type;
}
/**
......@@ -88,7 +90,13 @@ class MapFieldIter implements \Iterator
*/
public function key()
{
return key($this->container);
$key = key($this->container);
// PHP associative array stores bool as integer for key.
if ($this->key_type === GPBType::BOOL) {
return boolval($key);
} else {
return $key;
}
}
/**
......@@ -110,4 +118,4 @@ class MapFieldIter implements \Iterator
{
return key($this->container) !== null;
}
}
\ No newline at end of file
}
<?php
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
namespace Google\Protobuf\Internal;
class RawInputStream
{
private $buffer;
public function __construct($buffer)
{
$this->buffer = $buffer;
}
public function getData()
{
return $this->buffer;
}
}
This diff is collapsed.
#!/bin/bash
use_php() {
function use_php() {
VERSION=$1
PHP=`which php`
PHP_CONFIG=`which php-config`
......@@ -10,7 +10,7 @@ use_php() {
ln -sfn "/usr/local/php-${VERSION}/bin/phpize" $PHPIZE
}
generate_proto() {
function generate_proto() {
PROTOC1=$1
PROTOC2=$2
......@@ -25,6 +25,27 @@ generate_proto() {
popd
}
# Remove tests to expect error. These were added to API tests by mistake.
function remove_error_test() {
local TEMPFILE=`tempfile`
cat $1 | \
awk -v file=`basename $1` -v dir=`basename $(dirname $1)` '
BEGIN {
show = 1
}
/@expectedException PHPUnit_Framework_Error/ { show = 0; next; }
/ *\*\// { print; next; }
/ *}/ {
if (!show) {
show = 1;
next;
}
}
show { print }
' > $TEMPFILE
cp $TEMPFILE $1
}
set -ex
# Change to the script's directory.
......@@ -81,6 +102,15 @@ OLD_PROTOC=`pwd`/old_protoc
cd protobuf/php
cp -r /usr/local/vendor-5.5 vendor
wget https://phar.phpunit.de/phpunit-4.8.0.phar -O /usr/bin/phpunit
# Remove implementation detail tests.
tests=( array_test.php encode_decode_test.php generated_class_test.php map_field_test.php well_known_test.php )
sed -i.bak '/php_implementation_test.php/d' phpunit.xml
for t in "${tests[@]}"
do
remove_error_test tests/$t
done
cd tests
# Test A.1:
......
This diff is collapsed.
......@@ -3,7 +3,7 @@
# gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so `which
# phpunit` --bootstrap autoload.php tmp_test.php
#
gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so `which phpunit` --bootstrap autoload.php well_known_test.php
gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so `which phpunit` --bootstrap autoload.php encode_decode_test.php
#
# gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so memory_leak_test.php
#
......
This diff is collapsed.
This diff is collapsed.
......@@ -49,7 +49,7 @@ TestUtil::assertTestMessage($to);
$from->setRecursive($from);
$arr = new RepeatedField(GPBType::MESSAGE, TestMessage::class);
$arr []= new TestMessage;
$arr[] = new TestMessage;
$arr[0]->SetRepeatedRecursive($arr);
// Test oneof fields.
......
This diff is collapsed.
......@@ -19,6 +19,8 @@ class TestBase extends PHPUnit_Framework_TestCase
public function expectFields(TestMessage $m)
{
$this->assertSame(-42, $m->getOptionalInt32());
$this->assertSame(42, $m->getOptionalUint32());
$this->assertSame(-44, $m->getOptionalSint32());
$this->assertSame(46, $m->getOptionalFixed32());
$this->assertSame(-46, $m->getOptionalSfixed32());
......@@ -27,6 +29,7 @@ class TestBase extends PHPUnit_Framework_TestCase
$this->assertSame(true, $m->getOptionalBool());
$this->assertSame('a', $m->getOptionalString());
$this->assertSame('b', $m->getOptionalBytes());
$this->assertSame(TestEnum::ONE, $m->getOptionalEnum());
$this->assertSame(33, $m->getOptionalMessage()->getA());
if (PHP_INT_SIZE == 4) {
$this->assertSame('-43', $m->getOptionalInt64());
......
This diff is collapsed.
......@@ -397,27 +397,30 @@ build_php5.5() {
phpunit
popd
pushd conformance
# TODO(teboring): Add it back
# make test_php
make test_php
popd
}
build_php5.5_c() {
use_php 5.5
wget https://phar.phpunit.de/phpunit-4.8.0.phar -O /usr/bin/phpunit
cd php/tests && /bin/bash ./test.sh && cd ../..
pushd conformance
# make test_php_c
pushd php/tests
/bin/bash ./test.sh
popd
# TODO(teboring): Add it back
# pushd conformance
# make test_php_c
# popd
}
build_php5.5_zts_c() {
use_php_zts 5.5
wget https://phar.phpunit.de/phpunit-4.8.0.phar -O /usr/bin/phpunit
cd php/tests && /bin/bash ./test.sh && cd ../..
pushd conformance
# make test_php_c
popd
# TODO(teboring): Add it back
# pushd conformance
# make test_php_zts_c
# popd
}
build_php5.6() {
......@@ -429,8 +432,7 @@ build_php5.6() {
phpunit
popd
pushd conformance
# TODO(teboring): Add it back
# make test_php
make test_php
popd
}
......@@ -438,18 +440,20 @@ build_php5.6_c() {
use_php 5.6
wget https://phar.phpunit.de/phpunit-5.7.0.phar -O /usr/bin/phpunit
cd php/tests && /bin/bash ./test.sh && cd ../..
pushd conformance
# TODO(teboring): Add it back
# pushd conformance
# make test_php_c
popd
# popd
}
build_php5.6_zts_c() {
use_php_zts 5.6
wget https://phar.phpunit.de/phpunit-5.7.0.phar -O /usr/bin/phpunit
cd php/tests && /bin/bash ./test.sh && cd ../..
pushd conformance
# make test_php_c
popd
# TODO(teboring): Add it back
# pushd conformance
# make test_php_zts_c
# popd
}
build_php5.6_mac() {
......@@ -471,9 +475,10 @@ build_php5.6_mac() {
# Test
cd php/tests && /bin/bash ./test.sh && cd ../..
pushd conformance
# TODO(teboring): Add it back
# pushd conformance
# make test_php_c
popd
# popd
}
build_php7.0() {
......@@ -485,8 +490,7 @@ build_php7.0() {
phpunit
popd
pushd conformance
# TODO(teboring): Add it back
# make test_php
make test_php
popd
}
......@@ -494,18 +498,20 @@ build_php7.0_c() {
use_php 7.0
wget https://phar.phpunit.de/phpunit-5.6.0.phar -O /usr/bin/phpunit
cd php/tests && /bin/bash ./test.sh && cd ../..
pushd conformance
# TODO(teboring): Add it back
# pushd conformance
# make test_php_c
popd
# popd
}
build_php7.0_zts_c() {
use_php_zts 7.0
wget https://phar.phpunit.de/phpunit-5.6.0.phar -O /usr/bin/phpunit
cd php/tests && /bin/bash ./test.sh && cd ../..
pushd conformance
# make test_php_c
popd
# TODO(teboring): Add it back.
# pushd conformance
# make test_php_zts_c
# popd
}
build_php7.0_mac() {
......@@ -527,9 +533,10 @@ build_php7.0_mac() {
# Test
cd php/tests && /bin/bash ./test.sh && cd ../..
pushd conformance
# TODO(teboring): Add it back
# pushd conformance
# make test_php_c
popd
# popd
}
build_php_compatibility() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment