writer.js 51.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
// 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.

/**
 * @fileoverview This file contains utilities for encoding Javascript objects
 * into binary, wire-format protocol buffers (in the form of Uint8Arrays) that
 * a server can consume directly.
 *
 * jspb's BinaryWriter class defines methods for efficiently encoding
 * Javascript objects into binary, wire-format protocol buffers and supports
 * all the fundamental field types used in protocol buffers.
 *
 * Major caveat 1 - Users of this library _must_ keep their Javascript proto
 * parsing code in sync with the original .proto file - presumably you'll be
 * using the typed jspb code generator, but if you bypass that you'll need
 * to keep things in sync by hand.
 *
 * Major caveat 2 - Javascript is unable to accurately represent integers
 * larger than 2^53 due to its use of a double-precision floating point format
 * for all numbers. BinaryWriter does not make any special effort to preserve
 * precision for values above this limit - if you need to pass 64-bit integers
 * (hash codes, for example) between the client and server without precision
 * loss, do _not_ use this library.
 *
 * Major caveat 3 - This class uses typed arrays and must not be used on older
 * browsers that do not support them.
 *
 * @author aappleby@google.com (Austin Appleby)
 */

goog.provide('jspb.BinaryWriter');

goog.require('goog.asserts');
goog.require('goog.crypt.base64');
goog.require('jspb.BinaryConstants');
63
goog.require('jspb.BinaryEncoder');
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
goog.require('jspb.arith.Int64');
goog.require('jspb.arith.UInt64');
goog.require('jspb.utils');



/**
 * BinaryWriter implements encoders for all the wire types specified in
 * https://developers.google.com/protocol-buffers/docs/encoding.
 *
 * @constructor
 * @struct
 */
jspb.BinaryWriter = function() {
  /**
   * Blocks of serialized data that will be concatenated once all messages have
   * been written.
   * @private {!Array<!Uint8Array|!Array<number>>}
   */
  this.blocks_ = [];

  /**
86 87
   * Total number of bytes in the blocks_ array. Does _not_ include bytes in
   * the encoder below.
88 89 90 91 92
   * @private {number}
   */
  this.totalLength_ = 0;

  /**
93 94 95 96 97
   * Binary encoder holding pieces of a message that we're still serializing.
   * When we get to a stopping point (either the start of a new submessage, or
   * when we need to append a raw Uint8Array), the encoder's buffer will be
   * added to the block array above and the encoder will be reset.
   * @private {!jspb.BinaryEncoder}
98
   */
99
  this.encoder_ = new jspb.BinaryEncoder();
100 101 102 103 104

  /**
   * A stack of bookmarks containing the parent blocks for each message started
   * via beginSubMessage(), needed as bookkeeping for endSubMessage().
   * TODO(aappleby): Deprecated, users should be calling writeMessage().
105
   * @private {!Array<!Array<number>>}
106 107 108 109 110 111 112 113 114 115 116 117
   */
  this.bookmarks_ = [];
};


/**
 * Append a typed array of bytes onto the buffer.
 *
 * @param {!Uint8Array} arr The byte array to append.
 * @private
 */
jspb.BinaryWriter.prototype.appendUint8Array_ = function(arr) {
118 119
  var temp = this.encoder_.end();
  this.blocks_.push(temp);
120
  this.blocks_.push(arr);
121
  this.totalLength_ += temp.length + arr.length;
122 123 124 125
};


/**
126 127
 * Begins a new message by writing the field header and returning a bookmark
 * which we will use to patch in the message length to in endDelimited_ below.
128
 * @param {number} field
129
 * @return {!Array<number>}
130 131 132
 * @private
 */
jspb.BinaryWriter.prototype.beginDelimited_ = function(field) {
133 134 135 136 137 138
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  var bookmark = this.encoder_.end();
  this.blocks_.push(bookmark);
  this.totalLength_ += bookmark.length;
  bookmark.push(this.totalLength_);
  return bookmark;
139 140 141 142
};


/**
143 144 145
 * Ends a message by encoding the _change_ in length of the buffer to the
 * parent block and adds the number of bytes needed to encode that length to
 * the total byte length.
146
 * @param {!Array<number>} bookmark
147 148 149
 * @private
 */
jspb.BinaryWriter.prototype.endDelimited_ = function(bookmark) {
150 151
  var oldLength = bookmark.pop();
  var messageLength = this.totalLength_ + this.encoder_.length() - oldLength;
152 153 154
  goog.asserts.assert(messageLength >= 0);

  while (messageLength > 127) {
155
    bookmark.push((messageLength & 0x7f) | 0x80);
156
    messageLength = messageLength >>> 7;
157
    this.totalLength_++;
158 159
  }

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
  bookmark.push(messageLength);
  this.totalLength_++;
};


/**
 * Writes a pre-serialized message to the buffer.
 * @param {!Uint8Array} bytes The array of bytes to write.
 * @param {number} start The start of the range to write.
 * @param {number} end The end of the range to write.
 */
jspb.BinaryWriter.prototype.writeSerializedMessage = function(
    bytes, start, end) {
  this.appendUint8Array_(bytes.subarray(start, end));
};


/**
 * Writes a pre-serialized message to the buffer if the message and endpoints
 * are non-null.
 * @param {?Uint8Array} bytes The array of bytes to write.
 * @param {?number} start The start of the range to write.
 * @param {?number} end The end of the range to write.
 */
jspb.BinaryWriter.prototype.maybeWriteSerializedMessage = function(
    bytes, start, end) {
  if (bytes != null && start != null && end != null) {
    this.writeSerializedMessage(bytes, start, end);
  }
189 190 191 192 193 194 195 196
};


/**
 * Resets the writer, throwing away any accumulated buffers.
 */
jspb.BinaryWriter.prototype.reset = function() {
  this.blocks_ = [];
197
  this.encoder_.end();
198 199 200 201 202 203 204 205 206 207 208 209
  this.totalLength_ = 0;
  this.bookmarks_ = [];
};


/**
 * Converts the encoded data into a Uint8Array.
 * @return {!Uint8Array}
 */
jspb.BinaryWriter.prototype.getResultBuffer = function() {
  goog.asserts.assert(this.bookmarks_.length == 0);

210
  var flat = new Uint8Array(this.totalLength_ + this.encoder_.length());
211 212 213 214 215 216 217 218 219 220 221

  var blocks = this.blocks_;
  var blockCount = blocks.length;
  var offset = 0;

  for (var i = 0; i < blockCount; i++) {
    var block = blocks[i];
    flat.set(block, offset);
    offset += block.length;
  }

222 223 224
  var tail = this.encoder_.end();
  flat.set(tail, offset);
  offset += tail.length;
225 226 227 228 229 230 231 232 233 234 235 236 237

  // Post condition: `flattened` must have had every byte written.
  goog.asserts.assert(offset == flat.length);

  // Replace our block list with the flattened block, which lets GC reclaim
  // the temp blocks sooner.
  this.blocks_ = [flat];

  return flat;
};


/**
238
 * Converts the encoded data into a base64-encoded string.
239 240
 * @param {boolean=} opt_webSafe True indicates we should use a websafe
 *     alphabet, which does not require escaping for use in URLs.
241 242
 * @return {string}
 */
243 244
jspb.BinaryWriter.prototype.getResultBase64String = function(opt_webSafe) {
  return goog.crypt.base64.encodeByteArray(this.getResultBuffer(), opt_webSafe);
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
};


/**
 * Begins a new sub-message. The client must call endSubMessage() when they're
 * done.
 * TODO(aappleby): Deprecated. Move callers to writeMessage().
 * @param {number} field The field number of the sub-message.
 */
jspb.BinaryWriter.prototype.beginSubMessage = function(field) {
  this.bookmarks_.push(this.beginDelimited_(field));
};


/**
 * Finishes a sub-message and packs it into the parent messages' buffer.
 * TODO(aappleby): Deprecated. Move callers to writeMessage().
 */
jspb.BinaryWriter.prototype.endSubMessage = function() {
  goog.asserts.assert(this.bookmarks_.length >= 0);
  this.endDelimited_(this.bookmarks_.pop());
};


/**
 * Encodes a (field number, wire type) tuple into a wire-format field header
 * and stores it in the buffer as a varint.
 * @param {number} field The field number.
 * @param {number} wireType The wire-type of the field, as specified in the
 *     protocol buffer documentation.
 * @private
 */
277
jspb.BinaryWriter.prototype.writeFieldHeader_ =
278 279 280
    function(field, wireType) {
  goog.asserts.assert(field >= 1 && field == Math.floor(field));
  var x = field * 8 + wireType;
281
  this.encoder_.writeUnsignedVarint32(x);
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
};


/**
 * Writes a field of any valid scalar type to the binary stream.
 * @param {jspb.BinaryConstants.FieldType} fieldType
 * @param {number} field
 * @param {jspb.AnyFieldType} value
 */
jspb.BinaryWriter.prototype.writeAny = function(fieldType, field, value) {
  var fieldTypes = jspb.BinaryConstants.FieldType;
  switch (fieldType) {
    case fieldTypes.DOUBLE:
      this.writeDouble(field, /** @type {number} */(value));
      return;
    case fieldTypes.FLOAT:
      this.writeFloat(field, /** @type {number} */(value));
      return;
    case fieldTypes.INT64:
      this.writeInt64(field, /** @type {number} */(value));
      return;
    case fieldTypes.UINT64:
      this.writeUint64(field, /** @type {number} */(value));
      return;
    case fieldTypes.INT32:
      this.writeInt32(field, /** @type {number} */(value));
      return;
    case fieldTypes.FIXED64:
      this.writeFixed64(field, /** @type {number} */(value));
      return;
    case fieldTypes.FIXED32:
      this.writeFixed32(field, /** @type {number} */(value));
      return;
    case fieldTypes.BOOL:
      this.writeBool(field, /** @type {boolean} */(value));
      return;
    case fieldTypes.STRING:
      this.writeString(field, /** @type {string} */(value));
      return;
    case fieldTypes.GROUP:
      goog.asserts.fail('Group field type not supported in writeAny()');
      return;
    case fieldTypes.MESSAGE:
      goog.asserts.fail('Message field type not supported in writeAny()');
      return;
    case fieldTypes.BYTES:
      this.writeBytes(field, /** @type {?Uint8Array} */(value));
      return;
    case fieldTypes.UINT32:
      this.writeUint32(field, /** @type {number} */(value));
      return;
    case fieldTypes.ENUM:
      this.writeEnum(field, /** @type {number} */(value));
      return;
    case fieldTypes.SFIXED32:
      this.writeSfixed32(field, /** @type {number} */(value));
      return;
    case fieldTypes.SFIXED64:
      this.writeSfixed64(field, /** @type {number} */(value));
      return;
    case fieldTypes.SINT32:
      this.writeSint32(field, /** @type {number} */(value));
      return;
    case fieldTypes.SINT64:
      this.writeSint64(field, /** @type {number} */(value));
      return;
    case fieldTypes.FHASH64:
      this.writeFixedHash64(field, /** @type {string} */(value));
      return;
    case fieldTypes.VHASH64:
      this.writeVarintHash64(field, /** @type {string} */(value));
      return;
    default:
      goog.asserts.fail('Invalid field type in writeAny()');
      return;
  }
};


/**
 * Writes a varint field to the buffer without range checking.
 * @param {number} field The field number.
 * @param {number?} value The value to write.
 * @private
 */
jspb.BinaryWriter.prototype.writeUnsignedVarint32_ = function(field, value) {
  if (value == null) return;
369 370
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  this.encoder_.writeUnsignedVarint32(value);
371 372 373 374 375 376 377 378 379 380 381
};


/**
 * Writes a varint field to the buffer without range checking.
 * @param {number} field The field number.
 * @param {number?} value The value to write.
 * @private
 */
jspb.BinaryWriter.prototype.writeSignedVarint32_ = function(field, value) {
  if (value == null) return;
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  this.encoder_.writeSignedVarint32(value);
};


/**
 * Writes a varint field to the buffer without range checking.
 * @param {number} field The field number.
 * @param {number?} value The value to write.
 * @private
 */
jspb.BinaryWriter.prototype.writeUnsignedVarint64_ = function(field, value) {
  if (value == null) return;
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  this.encoder_.writeUnsignedVarint64(value);
397 398 399 400 401 402 403 404 405
};


/**
 * Writes a varint field to the buffer without range checking.
 * @param {number} field The field number.
 * @param {number?} value The value to write.
 * @private
 */
406
jspb.BinaryWriter.prototype.writeSignedVarint64_ = function(field, value) {
407
  if (value == null) return;
408 409
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  this.encoder_.writeSignedVarint64(value);
410 411 412 413 414 415 416 417 418 419 420
};


/**
 * Writes a zigzag varint field to the buffer without range checking.
 * @param {number} field The field number.
 * @param {number?} value The value to write.
 * @private
 */
jspb.BinaryWriter.prototype.writeZigzagVarint32_ = function(field, value) {
  if (value == null) return;
421 422
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  this.encoder_.writeZigzagVarint32(value);
423 424 425 426 427 428 429 430 431
};


/**
 * Writes a zigzag varint field to the buffer without range checking.
 * @param {number} field The field number.
 * @param {number?} value The value to write.
 * @private
 */
432
jspb.BinaryWriter.prototype.writeZigzagVarint64_ = function(field, value) {
433
  if (value == null) return;
434 435
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  this.encoder_.writeZigzagVarint64(value);
436 437 438
};


439 440 441 442 443 444 445 446 447 448 449 450 451 452
/**
 * Writes a zigzag varint field to the buffer without range checking.
 * @param {number} field The field number.
 * @param {string?} value The value to write.
 * @private
 */
jspb.BinaryWriter.prototype.writeZigzagVarint64String_ = function(
    field, value) {
  if (value == null) return;
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  this.encoder_.writeZigzagVarint64String(value);
};


453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
/**
 * Writes an int32 field to the buffer. Numbers outside the range [-2^31,2^31)
 * will be truncated.
 * @param {number} field The field number.
 * @param {number?} value The value to write.
 */
jspb.BinaryWriter.prototype.writeInt32 = function(field, value) {
  if (value == null) return;
  goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
                      (value < jspb.BinaryConstants.TWO_TO_31));
  this.writeSignedVarint32_(field, value);
};


/**
 * Writes an int32 field represented as a string to the buffer. Numbers outside
 * the range [-2^31,2^31) will be truncated.
 * @param {number} field The field number.
 * @param {string?} value The value to write.
 */
jspb.BinaryWriter.prototype.writeInt32String = function(field, value) {
  if (value == null) return;
  var intValue = /** {number} */ parseInt(value, 10);
  goog.asserts.assert((intValue >= -jspb.BinaryConstants.TWO_TO_31) &&
                      (intValue < jspb.BinaryConstants.TWO_TO_31));
  this.writeSignedVarint32_(field, intValue);
};


/**
 * Writes an int64 field to the buffer. Numbers outside the range [-2^63,2^63)
 * will be truncated.
 * @param {number} field The field number.
 * @param {number?} value The value to write.
 */
jspb.BinaryWriter.prototype.writeInt64 = function(field, value) {
  if (value == null) return;
  goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
                      (value < jspb.BinaryConstants.TWO_TO_63));
492
  this.writeSignedVarint64_(field, value);
493 494 495 496 497 498 499 500 501 502 503
};


/**
 * Writes a int64 field (with value as a string) to the buffer.
 * @param {number} field The field number.
 * @param {string?} value The value to write.
 */
jspb.BinaryWriter.prototype.writeInt64String = function(field, value) {
  if (value == null) return;
  var num = jspb.arith.Int64.fromString(value);
504 505
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  this.encoder_.writeSplitVarint64(num.lo, num.hi);
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
};


/**
 * Writes a uint32 field to the buffer. Numbers outside the range [0,2^32)
 * will be truncated.
 * @param {number} field The field number.
 * @param {number?} value The value to write.
 */
jspb.BinaryWriter.prototype.writeUint32 = function(field, value) {
  if (value == null) return;
  goog.asserts.assert((value >= 0) &&
                      (value < jspb.BinaryConstants.TWO_TO_32));
  this.writeUnsignedVarint32_(field, value);
};


/**
 * Writes a uint32 field represented as a string to the buffer. Numbers outside
 * the range [0,2^32) will be truncated.
 * @param {number} field The field number.
 * @param {string?} value The value to write.
 */
jspb.BinaryWriter.prototype.writeUint32String = function(field, value) {
  if (value == null) return;
  var intValue = /** {number} */ parseInt(value, 10);
  goog.asserts.assert((intValue >= 0) &&
                      (intValue < jspb.BinaryConstants.TWO_TO_32));
  this.writeUnsignedVarint32_(field, intValue);
};


/**
 * Writes a uint64 field to the buffer. Numbers outside the range [0,2^64)
 * will be truncated.
 * @param {number} field The field number.
 * @param {number?} value The value to write.
 */
jspb.BinaryWriter.prototype.writeUint64 = function(field, value) {
  if (value == null) return;
  goog.asserts.assert((value >= 0) &&
                      (value < jspb.BinaryConstants.TWO_TO_64));
548
  this.writeUnsignedVarint64_(field, value);
549 550 551 552 553 554 555 556 557 558 559
};


/**
 * Writes a uint64 field (with value as a string) to the buffer.
 * @param {number} field The field number.
 * @param {string?} value The value to write.
 */
jspb.BinaryWriter.prototype.writeUint64String = function(field, value) {
  if (value == null) return;
  var num = jspb.arith.UInt64.fromString(value);
560 561
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  this.encoder_.writeSplitVarint64(num.lo, num.hi);
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
};


/**
 * Writes a sint32 field to the buffer. Numbers outside the range [-2^31,2^31)
 * will be truncated.
 * @param {number} field The field number.
 * @param {number?} value The value to write.
 */
jspb.BinaryWriter.prototype.writeSint32 = function(field, value) {
  if (value == null) return;
  goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
                      (value < jspb.BinaryConstants.TWO_TO_31));
  this.writeZigzagVarint32_(field, value);
};


/**
 * Writes a sint64 field to the buffer. Numbers outside the range [-2^63,2^63)
 * will be truncated.
 * @param {number} field The field number.
 * @param {number?} value The value to write.
 */
jspb.BinaryWriter.prototype.writeSint64 = function(field, value) {
  if (value == null) return;
  goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
                      (value < jspb.BinaryConstants.TWO_TO_63));
589
  this.writeZigzagVarint64_(field, value);
590 591 592
};


593 594 595 596 597 598 599 600
/**
 * Writes a sint64 field to the buffer. Numbers outside the range [-2^63,2^63)
 * will be truncated.
 * @param {number} field The field number.
 * @param {string?} value The decimal string to write.
 */
jspb.BinaryWriter.prototype.writeSint64String = function(field, value) {
  if (value == null) return;
601 602
  goog.asserts.assert((+value >= -jspb.BinaryConstants.TWO_TO_63) &&
                      (+value < jspb.BinaryConstants.TWO_TO_63));
603 604 605 606
  this.writeZigzagVarint64String_(field, value);
};


607 608 609 610 611 612 613 614 615 616
/**
 * Writes a fixed32 field to the buffer. Numbers outside the range [0,2^32)
 * will be truncated.
 * @param {number} field The field number.
 * @param {number?} value The value to write.
 */
jspb.BinaryWriter.prototype.writeFixed32 = function(field, value) {
  if (value == null) return;
  goog.asserts.assert((value >= 0) &&
                      (value < jspb.BinaryConstants.TWO_TO_32));
617 618
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32);
  this.encoder_.writeUint32(value);
619 620 621 622 623 624 625 626 627 628 629 630 631
};


/**
 * Writes a fixed64 field to the buffer. Numbers outside the range [0,2^64)
 * will be truncated.
 * @param {number} field The field number.
 * @param {number?} value The value to write.
 */
jspb.BinaryWriter.prototype.writeFixed64 = function(field, value) {
  if (value == null) return;
  goog.asserts.assert((value >= 0) &&
                      (value < jspb.BinaryConstants.TWO_TO_64));
632 633
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  this.encoder_.writeUint64(value);
634 635 636
};


637 638 639 640 641 642 643 644 645 646 647 648 649
/**
 * Writes a fixed64 field (with value as a string) to the buffer.
 * @param {number} field The field number.
 * @param {string?} value The value to write.
 */
jspb.BinaryWriter.prototype.writeFixed64String = function(field, value) {
  if (value == null) return;
  var num = jspb.arith.UInt64.fromString(value);
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  this.encoder_.writeSplitFixed64(num.lo, num.hi);
};


650 651 652 653 654 655 656 657 658 659
/**
 * Writes a sfixed32 field to the buffer. Numbers outside the range
 * [-2^31,2^31) will be truncated.
 * @param {number} field The field number.
 * @param {number?} value The value to write.
 */
jspb.BinaryWriter.prototype.writeSfixed32 = function(field, value) {
  if (value == null) return;
  goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
                      (value < jspb.BinaryConstants.TWO_TO_31));
660 661
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32);
  this.encoder_.writeInt32(value);
662 663 664 665 666 667 668 669 670 671 672 673 674
};


/**
 * Writes a sfixed64 field to the buffer. Numbers outside the range
 * [-2^63,2^63) will be truncated.
 * @param {number} field The field number.
 * @param {number?} value The value to write.
 */
jspb.BinaryWriter.prototype.writeSfixed64 = function(field, value) {
  if (value == null) return;
  goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
                      (value < jspb.BinaryConstants.TWO_TO_63));
675 676
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  this.encoder_.writeInt64(value);
677 678 679
};


680 681 682 683 684 685 686 687 688 689 690 691 692 693
/**
 * Writes a sfixed64 string field to the buffer. Numbers outside the range
 * [-2^63,2^63) will be truncated.
 * @param {number} field The field number.
 * @param {string?} value The value to write.
 */
jspb.BinaryWriter.prototype.writeSfixed64String = function(field, value) {
  if (value == null) return;
  var num = jspb.arith.Int64.fromString(value);
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  this.encoder_.writeSplitFixed64(num.lo, num.hi);
};


694 695 696 697 698 699 700 701
/**
 * Writes a single-precision floating point field to the buffer. Numbers
 * requiring more than 32 bits of precision will be truncated.
 * @param {number} field The field number.
 * @param {number?} value The value to write.
 */
jspb.BinaryWriter.prototype.writeFloat = function(field, value) {
  if (value == null) return;
702 703
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32);
  this.encoder_.writeFloat(value);
704 705 706 707 708 709 710 711 712 713 714
};


/**
 * Writes a double-precision floating point field to the buffer. As this is the
 * native format used by JavaScript, no precision will be lost.
 * @param {number} field The field number.
 * @param {number?} value The value to write.
 */
jspb.BinaryWriter.prototype.writeDouble = function(field, value) {
  if (value == null) return;
715 716
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  this.encoder_.writeDouble(value);
717 718 719 720
};


/**
721 722 723
 * Writes a boolean field to the buffer. We allow numbers as input
 * because the JSPB code generator uses 0/1 instead of true/false to save space
 * in the string representation of the proto.
724
 * @param {number} field The field number.
725
 * @param {boolean?|number?} value The value to write.
726 727 728
 */
jspb.BinaryWriter.prototype.writeBool = function(field, value) {
  if (value == null) return;
729
  goog.asserts.assert(goog.isBoolean(value) || goog.isNumber(value));
730 731
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  this.encoder_.writeBool(value);
732 733 734 735 736 737 738 739 740 741 742 743
};


/**
 * Writes an enum field to the buffer.
 * @param {number} field The field number.
 * @param {number?} value The value to write.
 */
jspb.BinaryWriter.prototype.writeEnum = function(field, value) {
  if (value == null) return;
  goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
                      (value < jspb.BinaryConstants.TWO_TO_31));
744 745
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  this.encoder_.writeSignedVarint32(value);
746 747 748 749 750 751 752 753 754 755
};


/**
 * Writes a string field to the buffer.
 * @param {number} field The field number.
 * @param {string?} value The string to write.
 */
jspb.BinaryWriter.prototype.writeString = function(field, value) {
  if (value == null) return;
756 757 758
  var bookmark = this.beginDelimited_(field);
  this.encoder_.writeString(value);
  this.endDelimited_(bookmark);
759 760 761 762 763 764 765
};


/**
 * Writes an arbitrary byte field to the buffer. Note - to match the behavior
 * of the C++ implementation, empty byte arrays _are_ serialized.
 * @param {number} field The field number.
766
 * @param {?jspb.ByteSource} value The array of bytes to write.
767
 */
768 769 770 771 772 773
jspb.BinaryWriter.prototype.writeBytes = function(field, value) {
  if (value == null) return;
  var bytes = jspb.utils.byteSourceToUint8Array(value);
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  this.encoder_.writeUnsignedVarint32(bytes.length);
  this.appendUint8Array_(bytes);
774 775 776 777 778 779 780
};


/**
 * Writes a message to the buffer.
 * @param {number} field The field number.
 * @param {?MessageType} value The message to write.
781 782 783 784 785 786 787 788 789 790 791
 * @param {function(MessageTypeNonNull, !jspb.BinaryWriter)} writerCallback
 *     Will be invoked with the value to write and the writer to write it with.
 * @template MessageType
 * Use go/closure-ttl to declare a non-nullable version of MessageType.  Replace
 * the null in blah|null with none.  This is necessary because the compiler will
 * infer MessageType to be nullable if the value parameter is nullable.
 * @template MessageTypeNonNull :=
 *     cond(isUnknown(MessageType), unknown(),
 *       mapunion(MessageType, (X) =>
 *         cond(eq(X, 'null'), none(), X)))
 * =:
792
 */
793 794 795 796 797 798
jspb.BinaryWriter.prototype.writeMessage = function(
    field, value, writerCallback) {
  if (value == null) return;
  var bookmark = this.beginDelimited_(field);
  writerCallback(value, this);
  this.endDelimited_(bookmark);
799 800 801 802 803 804 805 806 807
};


/**
 * Writes a group message to the buffer.
 *
 * @param {number} field The field number.
 * @param {?MessageType} value The message to write, wrapped with START_GROUP /
 *     END_GROUP tags. Will be a no-op if 'value' is null.
808 809 810 811 812 813 814 815 816 817 818
 * @param {function(MessageTypeNonNull, !jspb.BinaryWriter)} writerCallback
 *     Will be invoked with the value to write and the writer to write it with.
 * @template MessageType
 * Use go/closure-ttl to declare a non-nullable version of MessageType.  Replace
 * the null in blah|null with none.  This is necessary because the compiler will
 * infer MessageType to be nullable if the value parameter is nullable.
 * @template MessageTypeNonNull :=
 *     cond(isUnknown(MessageType), unknown(),
 *       mapunion(MessageType, (X) =>
 *         cond(eq(X, 'null'), none(), X)))
 * =:
819
 */
820 821 822 823 824 825
jspb.BinaryWriter.prototype.writeGroup = function(
    field, value, writerCallback) {
  if (value == null) return;
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.START_GROUP);
  writerCallback(value, this);
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.END_GROUP);
826 827 828 829 830 831 832 833 834 835 836 837
};


/**
 * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
 * the buffer.
 * @param {number} field The field number.
 * @param {string?} value The hash string.
 */
jspb.BinaryWriter.prototype.writeFixedHash64 = function(field, value) {
  if (value == null) return;
  goog.asserts.assert(value.length == 8);
838 839
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  this.encoder_.writeFixedHash64(value);
840 841 842 843 844 845 846 847 848 849 850 851
};


/**
 * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
 * the buffer.
 * @param {number} field The field number.
 * @param {string?} value The hash string.
 */
jspb.BinaryWriter.prototype.writeVarintHash64 = function(field, value) {
  if (value == null) return;
  goog.asserts.assert(value.length == 8);
852 853
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  this.encoder_.writeVarintHash64(value);
854 855 856 857
};


/**
858
 * Writes an array of numbers to the buffer as a repeated 32-bit int field.
859
 * @param {number} field The field number.
860
 * @param {?Array<number>} value The array of ints to write.
861
 */
862
jspb.BinaryWriter.prototype.writeRepeatedInt32 = function(field, value) {
863 864 865 866 867 868 869 870
  if (value == null) return;
  for (var i = 0; i < value.length; i++) {
    this.writeSignedVarint32_(field, value[i]);
  }
};


/**
871 872
 * Writes an array of numbers formatted as strings to the buffer as a repeated
 * 32-bit int field.
873
 * @param {number} field The field number.
874
 * @param {?Array<string>} value The array of ints to write.
875
 */
876
jspb.BinaryWriter.prototype.writeRepeatedInt32String = function(field, value) {
877 878
  if (value == null) return;
  for (var i = 0; i < value.length; i++) {
879
    this.writeInt32String(field, value[i]);
880 881 882 883 884
  }
};


/**
885
 * Writes an array of numbers to the buffer as a repeated 64-bit int field.
886
 * @param {number} field The field number.
887
 * @param {?Array<number>} value The array of ints to write.
888
 */
889
jspb.BinaryWriter.prototype.writeRepeatedInt64 = function(field, value) {
890 891
  if (value == null) return;
  for (var i = 0; i < value.length; i++) {
892
    this.writeSignedVarint64_(field, value[i]);
893 894 895 896 897
  }
};


/**
898 899
 * Writes an array of numbers formatted as strings to the buffer as a repeated
 * 64-bit int field.
900
 * @param {number} field The field number.
901
 * @param {?Array<string>} value The array of ints to write.
902
 */
903
jspb.BinaryWriter.prototype.writeRepeatedInt64String = function(field, value) {
904 905
  if (value == null) return;
  for (var i = 0; i < value.length; i++) {
906
    this.writeInt64String(field, value[i]);
907 908 909 910 911
  }
};


/**
912 913
 * Writes an array numbers to the buffer as a repeated unsigned 32-bit int
 *     field.
914
 * @param {number} field The field number.
915
 * @param {?Array<number>} value The array of ints to write.
916
 */
917
jspb.BinaryWriter.prototype.writeRepeatedUint32 = function(field, value) {
918 919
  if (value == null) return;
  for (var i = 0; i < value.length; i++) {
920
    this.writeUnsignedVarint32_(field, value[i]);
921 922 923 924 925 926
  }
};


/**
 * Writes an array of numbers formatted as strings to the buffer as a repeated
927
 * unsigned 32-bit int field.
928
 * @param {number} field The field number.
929
 * @param {?Array<string>} value The array of ints to write.
930
 */
931
jspb.BinaryWriter.prototype.writeRepeatedUint32String = function(field, value) {
932 933
  if (value == null) return;
  for (var i = 0; i < value.length; i++) {
934
    this.writeUint32String(field, value[i]);
935 936 937 938 939
  }
};


/**
940 941
 * Writes an array numbers to the buffer as a repeated unsigned 64-bit int
 *     field.
942
 * @param {number} field The field number.
943
 * @param {?Array<number>} value The array of ints to write.
944
 */
945
jspb.BinaryWriter.prototype.writeRepeatedUint64 = function(field, value) {
946 947
  if (value == null) return;
  for (var i = 0; i < value.length; i++) {
948
    this.writeUnsignedVarint64_(field, value[i]);
949 950 951 952 953 954
  }
};


/**
 * Writes an array of numbers formatted as strings to the buffer as a repeated
955
 * unsigned 64-bit int field.
956
 * @param {number} field The field number.
957
 * @param {?Array<string>} value The array of ints to write.
958
 */
959
jspb.BinaryWriter.prototype.writeRepeatedUint64String = function(field, value) {
960 961
  if (value == null) return;
  for (var i = 0; i < value.length; i++) {
962
    this.writeUint64String(field, value[i]);
963 964 965 966 967
  }
};


/**
968
 * Writes an array numbers to the buffer as a repeated signed 32-bit int field.
969
 * @param {number} field The field number.
970
 * @param {?Array<number>} value The array of ints to write.
971
 */
972
jspb.BinaryWriter.prototype.writeRepeatedSint32 = function(field, value) {
973 974
  if (value == null) return;
  for (var i = 0; i < value.length; i++) {
975
    this.writeZigzagVarint32_(field, value[i]);
976 977 978 979 980
  }
};


/**
981
 * Writes an array numbers to the buffer as a repeated signed 64-bit int field.
982
 * @param {number} field The field number.
983
 * @param {?Array<number>} value The array of ints to write.
984
 */
985 986 987 988 989 990
jspb.BinaryWriter.prototype.writeRepeatedSint64 = function(field, value) {
  if (value == null) return;
  for (var i = 0; i < value.length; i++) {
    this.writeZigzagVarint64_(field, value[i]);
  }
};
991 992 993 994 995


/**
 * Writes an array numbers to the buffer as a repeated signed 64-bit int field.
 * @param {number} field The field number.
996
 * @param {?Array<string>} value The array of ints to write.
997
 */
998 999 1000 1001 1002 1003
jspb.BinaryWriter.prototype.writeRepeatedSint64String = function(field, value) {
  if (value == null) return;
  for (var i = 0; i < value.length; i++) {
    this.writeZigzagVarint64String_(field, value[i]);
  }
};
1004 1005 1006 1007 1008 1009


/**
 * Writes an array of numbers to the buffer as a repeated fixed32 field. This
 * works for both signed and unsigned fixed32s.
 * @param {number} field The field number.
1010
 * @param {?Array<number>} value The array of ints to write.
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
 */
jspb.BinaryWriter.prototype.writeRepeatedFixed32 = function(field, value) {
  if (value == null) return;
  for (var i = 0; i < value.length; i++) {
    this.writeFixed32(field, value[i]);
  }
};


/**
 * Writes an array of numbers to the buffer as a repeated fixed64 field. This
 * works for both signed and unsigned fixed64s.
 * @param {number} field The field number.
1024
 * @param {?Array<number>} value The array of ints to write.
1025 1026 1027 1028 1029 1030 1031 1032 1033
 */
jspb.BinaryWriter.prototype.writeRepeatedFixed64 = function(field, value) {
  if (value == null) return;
  for (var i = 0; i < value.length; i++) {
    this.writeFixed64(field, value[i]);
  }
};


1034 1035 1036 1037
/**
 * Writes an array of numbers to the buffer as a repeated fixed64 field. This
 * works for both signed and unsigned fixed64s.
 * @param {number} field The field number.
1038
 * @param {?Array<string>} value The array of decimal strings to write.
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
 */
jspb.BinaryWriter.prototype.writeRepeatedFixed64String = function(
    field, value) {
  if (value == null) return;
  for (var i = 0; i < value.length; i++) {
    this.writeFixed64String(field, value[i]);
  }
};


1049 1050 1051
/**
 * Writes an array of numbers to the buffer as a repeated sfixed32 field.
 * @param {number} field The field number.
1052
 * @param {?Array<number>} value The array of ints to write.
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
 */
jspb.BinaryWriter.prototype.writeRepeatedSfixed32 = function(field, value) {
  if (value == null) return;
  for (var i = 0; i < value.length; i++) {
    this.writeSfixed32(field, value[i]);
  }
};


/**
 * Writes an array of numbers to the buffer as a repeated sfixed64 field.
 * @param {number} field The field number.
1065
 * @param {?Array<number>} value The array of ints to write.
1066 1067 1068 1069 1070 1071 1072 1073 1074
 */
jspb.BinaryWriter.prototype.writeRepeatedSfixed64 = function(field, value) {
  if (value == null) return;
  for (var i = 0; i < value.length; i++) {
    this.writeSfixed64(field, value[i]);
  }
};


1075 1076 1077 1078
/**
 * Writes an array of decimal strings to the buffer as a repeated sfixed64
 * field.
 * @param {number} field The field number.
1079
 * @param {?Array<string>} value The array of decimal strings to write.
1080 1081 1082 1083 1084 1085 1086 1087 1088
 */
jspb.BinaryWriter.prototype.writeRepeatedSfixed64String = function(field, value) {
  if (value == null) return;
  for (var i = 0; i < value.length; i++) {
    this.writeSfixed64String(field, value[i]);
  }
};


1089 1090 1091
/**
 * Writes an array of numbers to the buffer as a repeated float field.
 * @param {number} field The field number.
1092
 * @param {?Array<number>} value The array of ints to write.
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
 */
jspb.BinaryWriter.prototype.writeRepeatedFloat = function(field, value) {
  if (value == null) return;
  for (var i = 0; i < value.length; i++) {
    this.writeFloat(field, value[i]);
  }
};


/**
 * Writes an array of numbers to the buffer as a repeated double field.
 * @param {number} field The field number.
1105
 * @param {?Array<number>} value The array of ints to write.
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
 */
jspb.BinaryWriter.prototype.writeRepeatedDouble = function(field, value) {
  if (value == null) return;
  for (var i = 0; i < value.length; i++) {
    this.writeDouble(field, value[i]);
  }
};


/**
 * Writes an array of booleans to the buffer as a repeated bool field.
 * @param {number} field The field number.
1118
 * @param {?Array<boolean>} value The array of ints to write.
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
 */
jspb.BinaryWriter.prototype.writeRepeatedBool = function(field, value) {
  if (value == null) return;
  for (var i = 0; i < value.length; i++) {
    this.writeBool(field, value[i]);
  }
};


/**
 * Writes an array of enums to the buffer as a repeated enum field.
 * @param {number} field The field number.
1131
 * @param {?Array<number>} value The array of ints to write.
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
 */
jspb.BinaryWriter.prototype.writeRepeatedEnum = function(field, value) {
  if (value == null) return;
  for (var i = 0; i < value.length; i++) {
    this.writeEnum(field, value[i]);
  }
};


/**
 * Writes an array of strings to the buffer as a repeated string field.
 * @param {number} field The field number.
1144
 * @param {?Array<string>} value The array of strings to write.
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
 */
jspb.BinaryWriter.prototype.writeRepeatedString = function(field, value) {
  if (value == null) return;
  for (var i = 0; i < value.length; i++) {
    this.writeString(field, value[i]);
  }
};


/**
 * Writes an array of arbitrary byte fields to the buffer.
 * @param {number} field The field number.
1157
 * @param {?Array<!jspb.ByteSource>} value The arrays of arrays of bytes to
1158
 *     write.
1159
 */
1160 1161 1162 1163 1164
jspb.BinaryWriter.prototype.writeRepeatedBytes = function(field, value) {
  if (value == null) return;
  for (var i = 0; i < value.length; i++) {
    this.writeBytes(field, value[i]);
  }
1165 1166 1167 1168 1169 1170 1171
};


/**
 * Writes an array of messages to the buffer.
 * @template MessageType
 * @param {number} field The field number.
1172
 * @param {?Array<MessageType>} value The array of messages to
1173
 *    write.
1174 1175
 * @param {function(MessageType, !jspb.BinaryWriter)} writerCallback
 *     Will be invoked with the value to write and the writer to write it with.
1176 1177 1178 1179 1180 1181 1182 1183
 */
jspb.BinaryWriter.prototype.writeRepeatedMessage = function(
    field, value, writerCallback) {
  if (value == null) return;
  for (var i = 0; i < value.length; i++) {
    var bookmark = this.beginDelimited_(field);
    writerCallback(value[i], this);
    this.endDelimited_(bookmark);
1184 1185 1186 1187 1188 1189 1190 1191
  }
};


/**
 * Writes an array of group messages to the buffer.
 * @template MessageType
 * @param {number} field The field number.
1192
 * @param {?Array<MessageType>} value The array of messages to
1193
 *    write.
1194 1195
 * @param {function(MessageType, !jspb.BinaryWriter)} writerCallback
 *     Will be invoked with the value to write and the writer to write it with.
1196
 */
1197 1198 1199 1200 1201 1202 1203
jspb.BinaryWriter.prototype.writeRepeatedGroup = function(
    field, value, writerCallback) {
  if (value == null) return;
  for (var i = 0; i < value.length; i++) {
    this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.START_GROUP);
    writerCallback(value[i], this);
    this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.END_GROUP);
1204 1205 1206 1207 1208 1209 1210 1211
  }
};


/**
 * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
 * the buffer.
 * @param {number} field The field number.
1212
 * @param {?Array<string>} value The array of hashes to write.
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
 */
jspb.BinaryWriter.prototype.writeRepeatedFixedHash64 =
    function(field, value) {
  if (value == null) return;
  for (var i = 0; i < value.length; i++) {
    this.writeFixedHash64(field, value[i]);
  }
};


/**
 * Writes a repeated 64-bit hash string field (8 characters @ 8 bits of data
 * each) to the buffer.
 * @param {number} field The field number.
1227
 * @param {?Array<string>} value The array of hashes to write.
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
 */
jspb.BinaryWriter.prototype.writeRepeatedVarintHash64 =
    function(field, value) {
  if (value == null) return;
  for (var i = 0; i < value.length; i++) {
    this.writeVarintHash64(field, value[i]);
  }
};


/**
1239
 * Writes an array of numbers to the buffer as a packed 32-bit int field.
1240
 * @param {number} field The field number.
1241
 * @param {?Array<number>} value The array of ints to write.
1242
 */
1243
jspb.BinaryWriter.prototype.writePackedInt32 = function(field, value) {
1244 1245 1246
  if (value == null || !value.length) return;
  var bookmark = this.beginDelimited_(field);
  for (var i = 0; i < value.length; i++) {
1247
    this.encoder_.writeSignedVarint32(value[i]);
1248
  }
1249
  this.endDelimited_(bookmark);
1250 1251 1252 1253
};


/**
1254 1255 1256
 * Writes an array of numbers represented as strings to the buffer as a packed
 * 32-bit int field.
 * @param {number} field
1257
 * @param {?Array<string>} value
1258
 */
1259
jspb.BinaryWriter.prototype.writePackedInt32String = function(field, value) {
1260 1261 1262
  if (value == null || !value.length) return;
  var bookmark = this.beginDelimited_(field);
  for (var i = 0; i < value.length; i++) {
1263
    this.encoder_.writeSignedVarint32(parseInt(value[i], 10));
1264
  }
1265
  this.endDelimited_(bookmark);
1266 1267 1268 1269
};


/**
1270
 * Writes an array of numbers to the buffer as a packed 64-bit int field.
1271
 * @param {number} field The field number.
1272
 * @param {?Array<number>} value The array of ints to write.
1273
 */
1274
jspb.BinaryWriter.prototype.writePackedInt64 = function(field, value) {
1275 1276 1277
  if (value == null || !value.length) return;
  var bookmark = this.beginDelimited_(field);
  for (var i = 0; i < value.length; i++) {
1278
    this.encoder_.writeSignedVarint64(value[i]);
1279
  }
1280 1281 1282 1283 1284
  this.endDelimited_(bookmark);
};


/**
1285 1286 1287
 * Writes an array of numbers represented as strings to the buffer as a packed
 * 64-bit int field.
 * @param {number} field
1288
 * @param {?Array<string>} value
1289
 */
1290
jspb.BinaryWriter.prototype.writePackedInt64String = function(field, value) {
1291 1292 1293
  if (value == null || !value.length) return;
  var bookmark = this.beginDelimited_(field);
  for (var i = 0; i < value.length; i++) {
1294 1295
    var num = jspb.arith.Int64.fromString(value[i]);
    this.encoder_.writeSplitVarint64(num.lo, num.hi);
1296 1297
  }
  this.endDelimited_(bookmark);
1298 1299 1300 1301
};


/**
1302
 * Writes an array numbers to the buffer as a packed unsigned 32-bit int field.
1303
 * @param {number} field The field number.
1304
 * @param {?Array<number>} value The array of ints to write.
1305
 */
1306
jspb.BinaryWriter.prototype.writePackedUint32 = function(field, value) {
1307 1308 1309
  if (value == null || !value.length) return;
  var bookmark = this.beginDelimited_(field);
  for (var i = 0; i < value.length; i++) {
1310
    this.encoder_.writeUnsignedVarint32(value[i]);
1311 1312 1313 1314 1315 1316
  }
  this.endDelimited_(bookmark);
};


/**
1317 1318 1319
 * Writes an array of numbers represented as strings to the buffer as a packed
 * unsigned 32-bit int field.
 * @param {number} field
1320
 * @param {?Array<string>} value
1321
 */
1322 1323
jspb.BinaryWriter.prototype.writePackedUint32String =
    function(field, value) {
1324 1325 1326
  if (value == null || !value.length) return;
  var bookmark = this.beginDelimited_(field);
  for (var i = 0; i < value.length; i++) {
1327
    this.encoder_.writeUnsignedVarint32(parseInt(value[i], 10));
1328
  }
1329
  this.endDelimited_(bookmark);
1330 1331 1332 1333
};


/**
1334
 * Writes an array numbers to the buffer as a packed unsigned 64-bit int field.
1335
 * @param {number} field The field number.
1336
 * @param {?Array<number>} value The array of ints to write.
1337
 */
1338
jspb.BinaryWriter.prototype.writePackedUint64 = function(field, value) {
1339 1340 1341
  if (value == null || !value.length) return;
  var bookmark = this.beginDelimited_(field);
  for (var i = 0; i < value.length; i++) {
1342
    this.encoder_.writeUnsignedVarint64(value[i]);
1343 1344 1345 1346 1347 1348 1349
  }
  this.endDelimited_(bookmark);
};


/**
 * Writes an array of numbers represented as strings to the buffer as a packed
1350
 * unsigned 64-bit int field.
1351
 * @param {number} field
1352
 * @param {?Array<string>} value
1353
 */
1354
jspb.BinaryWriter.prototype.writePackedUint64String =
1355 1356 1357 1358
    function(field, value) {
  if (value == null || !value.length) return;
  var bookmark = this.beginDelimited_(field);
  for (var i = 0; i < value.length; i++) {
1359
    var num = jspb.arith.UInt64.fromString(value[i]);
1360
    this.encoder_.writeSplitVarint64(num.lo, num.hi);
1361 1362 1363 1364 1365 1366
  }
  this.endDelimited_(bookmark);
};


/**
1367
 * Writes an array numbers to the buffer as a packed signed 32-bit int field.
1368
 * @param {number} field The field number.
1369
 * @param {?Array<number>} value The array of ints to write.
1370
 */
1371
jspb.BinaryWriter.prototype.writePackedSint32 = function(field, value) {
1372 1373 1374
  if (value == null || !value.length) return;
  var bookmark = this.beginDelimited_(field);
  for (var i = 0; i < value.length; i++) {
1375
    this.encoder_.writeZigzagVarint32(value[i]);
1376 1377 1378 1379 1380 1381
  }
  this.endDelimited_(bookmark);
};


/**
1382
 * Writes an array of numbers to the buffer as a packed signed 64-bit int field.
1383
 * @param {number} field The field number.
1384
 * @param {?Array<number>} value The array of ints to write.
1385
 */
1386
jspb.BinaryWriter.prototype.writePackedSint64 = function(field, value) {
1387 1388 1389
  if (value == null || !value.length) return;
  var bookmark = this.beginDelimited_(field);
  for (var i = 0; i < value.length; i++) {
1390
    this.encoder_.writeZigzagVarint64(value[i]);
1391 1392 1393 1394 1395 1396
  }
  this.endDelimited_(bookmark);
};


/**
1397 1398
 * Writes an array of decimal strings to the buffer as a packed signed 64-bit
 * int field.
1399
 * @param {number} field The field number.
1400
 * @param {?Array<string>} value The array of decimal strings to write.
1401
 */
1402 1403 1404 1405 1406 1407 1408 1409 1410
jspb.BinaryWriter.prototype.writePackedSint64String = function(field, value) {
  if (value == null || !value.length) return;
  var bookmark = this.beginDelimited_(field);
  for (var i = 0; i < value.length; i++) {
    // TODO(haberman): make lossless
    this.encoder_.writeZigzagVarint64(parseInt(value[i], 10));
  }
  this.endDelimited_(bookmark);
};
1411 1412 1413 1414 1415


/**
 * Writes an array of numbers to the buffer as a packed fixed32 field.
 * @param {number} field The field number.
1416
 * @param {?Array<number>} value The array of ints to write.
1417 1418 1419 1420 1421 1422 1423
 */
jspb.BinaryWriter.prototype.writePackedFixed32 = function(field, value) {
  if (value == null || !value.length) return;
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  this.encoder_.writeUnsignedVarint32(value.length * 4);
  for (var i = 0; i < value.length; i++) {
    this.encoder_.writeUint32(value[i]);
1424 1425 1426 1427 1428 1429 1430
  }
};


/**
 * Writes an array of numbers to the buffer as a packed fixed64 field.
 * @param {number} field The field number.
1431
 * @param {?Array<number>} value The array of ints to write.
1432 1433 1434 1435 1436 1437 1438
 */
jspb.BinaryWriter.prototype.writePackedFixed64 = function(field, value) {
  if (value == null || !value.length) return;
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  this.encoder_.writeUnsignedVarint32(value.length * 8);
  for (var i = 0; i < value.length; i++) {
    this.encoder_.writeUint64(value[i]);
1439 1440 1441 1442
  }
};


1443 1444 1445 1446
/**
 * Writes an array of numbers represented as strings to the buffer as a packed
 * fixed64 field.
 * @param {number} field The field number.
1447
 * @param {?Array<string>} value The array of strings to write.
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
 */
jspb.BinaryWriter.prototype.writePackedFixed64String = function(field, value) {
  if (value == null || !value.length) return;
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  this.encoder_.writeUnsignedVarint32(value.length * 8);
  for (var i = 0; i < value.length; i++) {
    var num = jspb.arith.UInt64.fromString(value[i]);
    this.encoder_.writeSplitFixed64(num.lo, num.hi);
  }
};


1460 1461 1462
/**
 * Writes an array of numbers to the buffer as a packed sfixed32 field.
 * @param {number} field The field number.
1463
 * @param {?Array<number>} value The array of ints to write.
1464 1465 1466 1467 1468 1469 1470
 */
jspb.BinaryWriter.prototype.writePackedSfixed32 = function(field, value) {
  if (value == null || !value.length) return;
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  this.encoder_.writeUnsignedVarint32(value.length * 4);
  for (var i = 0; i < value.length; i++) {
    this.encoder_.writeInt32(value[i]);
1471 1472 1473 1474 1475 1476 1477
  }
};


/**
 * Writes an array of numbers to the buffer as a packed sfixed64 field.
 * @param {number} field The field number.
1478
 * @param {?Array<number>} value The array of ints to write.
1479 1480 1481 1482 1483 1484 1485
 */
jspb.BinaryWriter.prototype.writePackedSfixed64 = function(field, value) {
  if (value == null || !value.length) return;
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  this.encoder_.writeUnsignedVarint32(value.length * 8);
  for (var i = 0; i < value.length; i++) {
    this.encoder_.writeInt64(value[i]);
1486 1487 1488 1489
  }
};


1490 1491 1492
/**
 * Writes an array of numbers to the buffer as a packed sfixed64 field.
 * @param {number} field The field number.
1493
 * @param {?Array<string>} value The array of decimal strings to write.
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
 */
jspb.BinaryWriter.prototype.writePackedSfixed64String = function(field, value) {
  if (value == null || !value.length) return;
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  this.encoder_.writeUnsignedVarint32(value.length * 8);
  for (var i = 0; i < value.length; i++) {
    this.encoder_.writeInt64String(value[i]);
  }
};


1505 1506 1507
/**
 * Writes an array of numbers to the buffer as a packed float field.
 * @param {number} field The field number.
1508
 * @param {?Array<number>} value The array of ints to write.
1509 1510 1511 1512 1513 1514 1515
 */
jspb.BinaryWriter.prototype.writePackedFloat = function(field, value) {
  if (value == null || !value.length) return;
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  this.encoder_.writeUnsignedVarint32(value.length * 4);
  for (var i = 0; i < value.length; i++) {
    this.encoder_.writeFloat(value[i]);
1516 1517 1518 1519 1520 1521 1522
  }
};


/**
 * Writes an array of numbers to the buffer as a packed double field.
 * @param {number} field The field number.
1523
 * @param {?Array<number>} value The array of ints to write.
1524 1525 1526 1527 1528 1529 1530
 */
jspb.BinaryWriter.prototype.writePackedDouble = function(field, value) {
  if (value == null || !value.length) return;
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  this.encoder_.writeUnsignedVarint32(value.length * 8);
  for (var i = 0; i < value.length; i++) {
    this.encoder_.writeDouble(value[i]);
1531 1532 1533 1534 1535 1536 1537
  }
};


/**
 * Writes an array of booleans to the buffer as a packed bool field.
 * @param {number} field The field number.
1538
 * @param {?Array<boolean>} value The array of ints to write.
1539 1540 1541 1542 1543 1544 1545
 */
jspb.BinaryWriter.prototype.writePackedBool = function(field, value) {
  if (value == null || !value.length) return;
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  this.encoder_.writeUnsignedVarint32(value.length);
  for (var i = 0; i < value.length; i++) {
    this.encoder_.writeBool(value[i]);
1546 1547 1548 1549 1550 1551 1552
  }
};


/**
 * Writes an array of enums to the buffer as a packed enum field.
 * @param {number} field The field number.
1553
 * @param {?Array<number>} value The array of ints to write.
1554
 */
1555 1556 1557 1558 1559
jspb.BinaryWriter.prototype.writePackedEnum = function(field, value) {
  if (value == null || !value.length) return;
  var bookmark = this.beginDelimited_(field);
  for (var i = 0; i < value.length; i++) {
    this.encoder_.writeEnum(value[i]);
1560
  }
1561
  this.endDelimited_(bookmark);
1562 1563 1564 1565 1566 1567 1568
};


/**
 * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
 * the buffer.
 * @param {number} field The field number.
1569
 * @param {?Array<string>} value The array of hashes to write.
1570 1571 1572 1573 1574 1575 1576
 */
jspb.BinaryWriter.prototype.writePackedFixedHash64 = function(field, value) {
  if (value == null || !value.length) return;
  this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  this.encoder_.writeUnsignedVarint32(value.length * 8);
  for (var i = 0; i < value.length; i++) {
    this.encoder_.writeFixedHash64(value[i]);
1577 1578 1579 1580 1581 1582 1583 1584
  }
};


/**
 * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
 * the buffer.
 * @param {number} field The field number.
1585
 * @param {?Array<string>} value The array of hashes to write.
1586
 */
1587 1588 1589 1590 1591
jspb.BinaryWriter.prototype.writePackedVarintHash64 = function(field, value) {
  if (value == null || !value.length) return;
  var bookmark = this.beginDelimited_(field);
  for (var i = 0; i < value.length; i++) {
    this.encoder_.writeVarintHash64(value[i]);
1592
  }
1593
  this.endDelimited_(bookmark);
1594
};