Skip to content

Commit 2ebdf33

Browse files
committed
Sync from Piper @343521567
PROTOBUF_SYNC_PIPER
2 parents ab32afe + 2e28bfe commit 2ebdf33

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

binary/reader.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,15 @@ jspb.BinaryReader.prototype.getWireType = function() {
207207
};
208208

209209

210+
/**
211+
* @return {boolean} Whether the current wire type is a delimited field. Used to
212+
* conditionally parse packed repeated fields.
213+
*/
214+
jspb.BinaryReader.prototype.isDelimited = function() {
215+
return this.nextWireType_ == jspb.BinaryConstants.WireType.DELIMITED;
216+
};
217+
218+
210219
/**
211220
* @return {boolean} Whether the current wire type is an end-group tag. Used as
212221
* an exit condition in decoder loops in generated code.

src/google/protobuf/compiler/js/js_generator.cc

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3163,6 +3163,15 @@ void Generator::GenerateClassDeserializeBinaryField(
31633163
(field->type() == FieldDescriptor::TYPE_GROUP)
31643164
? (StrCat(field->number()) + ", ")
31653165
: "");
3166+
} else if (field->is_packable()) {
3167+
printer->Print(
3168+
" var values = /** @type {$fieldtype$} */ "
3169+
"(reader.isDelimited() "
3170+
"? reader.readPacked$reader$() : [reader.read$reader$()]);\n",
3171+
"fieldtype",
3172+
JSFieldTypeAnnotation(options, field, false, true,
3173+
/* singular_if_not_packed */ false, BYTES_U8),
3174+
"reader", JSBinaryReaderMethodType(field));
31663175
} else {
31673176
printer->Print(
31683177
" var value = /** @type {$fieldtype$} */ "
@@ -3174,7 +3183,14 @@ void Generator::GenerateClassDeserializeBinaryField(
31743183
JSBinaryReadWriteMethodName(field, /* is_writer = */ false));
31753184
}
31763185

3177-
if (field->is_repeated() && !field->is_packed()) {
3186+
if (field->is_packable()) {
3187+
printer->Print(
3188+
" for (var i = 0; i < values.length; i++) {\n"
3189+
" msg.add$name$(values[i]);\n"
3190+
" }\n",
3191+
"name",
3192+
JSGetterName(options, field, BYTES_DEFAULT, /* drop_list = */ true));
3193+
} else if (field->is_repeated()) {
31783194
printer->Print(
31793195
" msg.add$name$(value);\n", "name",
31803196
JSGetterName(options, field, BYTES_DEFAULT, /* drop_list = */ true));

0 commit comments

Comments
 (0)