Skip to content

Commit 4abd80d

Browse files
authored
JS: parse (un)packed fields conditionally (#7379)
1 parent 40e0ed8 commit 4abd80d

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
@@ -206,6 +206,15 @@ jspb.BinaryReader.prototype.getWireType = function() {
206206
};
207207

208208

209+
/**
210+
* @return {boolean} Whether the current wire type is a delimited field. Used to
211+
* conditionally parse packed repeated fields.
212+
*/
213+
jspb.BinaryReader.prototype.isDelimited = function() {
214+
return this.nextWireType_ == jspb.BinaryConstants.WireType.DELIMITED;
215+
};
216+
217+
209218
/**
210219
* @return {boolean} Whether the current wire type is an end-group tag. Used as
211220
* 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,16 @@ 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",
3175+
JSBinaryReaderMethodType(field));
31663176
} else {
31673177
printer->Print(
31683178
" var value = /** @type {$fieldtype$} */ "
@@ -3174,7 +3184,13 @@ void Generator::GenerateClassDeserializeBinaryField(
31743184
JSBinaryReadWriteMethodName(field, /* is_writer = */ false));
31753185
}
31763186

3177-
if (field->is_repeated() && !field->is_packed()) {
3187+
if (field->is_packable()) {
3188+
printer->Print(
3189+
" for (var i = 0; i < values.length; i++) {\n"
3190+
" msg.add$name$(values[i]);\n"
3191+
" }\n", "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)