Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2008 Google Inc. All rights reserved. |
| 3 | // https://developers.google.com/protocol-buffers/ |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
| 8 | // |
| 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | /** |
| 32 | * @fileoverview This file contains utilities for encoding Javascript objects |
| 33 | * into binary, wire-format protocol buffers (in the form of Uint8Arrays) that |
| 34 | * a server can consume directly. |
| 35 | * |
| 36 | * jspb's BinaryWriter class defines methods for efficiently encoding |
| 37 | * Javascript objects into binary, wire-format protocol buffers and supports |
| 38 | * all the fundamental field types used in protocol buffers. |
| 39 | * |
| 40 | * Major caveat 1 - Users of this library _must_ keep their Javascript proto |
| 41 | * parsing code in sync with the original .proto file - presumably you'll be |
| 42 | * using the typed jspb code generator, but if you bypass that you'll need |
| 43 | * to keep things in sync by hand. |
| 44 | * |
| 45 | * Major caveat 2 - Javascript is unable to accurately represent integers |
| 46 | * larger than 2^53 due to its use of a double-precision floating point format |
| 47 | * for all numbers. BinaryWriter does not make any special effort to preserve |
| 48 | * precision for values above this limit - if you need to pass 64-bit integers |
| 49 | * (hash codes, for example) between the client and server without precision |
| 50 | * loss, do _not_ use this library. |
| 51 | * |
| 52 | * Major caveat 3 - This class uses typed arrays and must not be used on older |
| 53 | * browsers that do not support them. |
| 54 | * |
| 55 | * @author aappleby@google.com (Austin Appleby) |
| 56 | */ |
| 57 | |
| 58 | goog.provide('jspb.BinaryWriter'); |
| 59 | |
| 60 | goog.require('goog.asserts'); |
| 61 | goog.require('goog.crypt.base64'); |
| 62 | goog.require('jspb.BinaryConstants'); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 63 | goog.require('jspb.BinaryEncoder'); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 64 | goog.require('jspb.arith.Int64'); |
| 65 | goog.require('jspb.arith.UInt64'); |
| 66 | goog.require('jspb.utils'); |
| 67 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 68 | |
| 69 | |
| 70 | /** |
| 71 | * BinaryWriter implements encoders for all the wire types specified in |
| 72 | * https://developers.google.com/protocol-buffers/docs/encoding. |
| 73 | * |
| 74 | * @constructor |
| 75 | * @struct |
| 76 | */ |
| 77 | jspb.BinaryWriter = function() { |
| 78 | /** |
| 79 | * Blocks of serialized data that will be concatenated once all messages have |
| 80 | * been written. |
| 81 | * @private {!Array<!Uint8Array|!Array<number>>} |
| 82 | */ |
| 83 | this.blocks_ = []; |
| 84 | |
| 85 | /** |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 86 | * Total number of bytes in the blocks_ array. Does _not_ include bytes in |
| 87 | * the encoder below. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 88 | * @private {number} |
| 89 | */ |
| 90 | this.totalLength_ = 0; |
| 91 | |
| 92 | /** |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 93 | * Binary encoder holding pieces of a message that we're still serializing. |
| 94 | * When we get to a stopping point (either the start of a new submessage, or |
| 95 | * when we need to append a raw Uint8Array), the encoder's buffer will be |
| 96 | * added to the block array above and the encoder will be reset. |
| 97 | * @private {!jspb.BinaryEncoder} |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 98 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 99 | this.encoder_ = new jspb.BinaryEncoder(); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * A stack of bookmarks containing the parent blocks for each message started |
| 103 | * via beginSubMessage(), needed as bookkeeping for endSubMessage(). |
| 104 | * TODO(aappleby): Deprecated, users should be calling writeMessage(). |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 105 | * @private {!Array<!Array<number>>} |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 106 | */ |
| 107 | this.bookmarks_ = []; |
| 108 | }; |
| 109 | |
| 110 | |
| 111 | /** |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 112 | * Append a typed array of bytes onto the buffer. |
| 113 | * |
| 114 | * @param {!Uint8Array} arr The byte array to append. |
| 115 | * @private |
| 116 | */ |
| 117 | jspb.BinaryWriter.prototype.appendUint8Array_ = function(arr) { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 118 | var temp = this.encoder_.end(); |
| 119 | this.blocks_.push(temp); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 120 | this.blocks_.push(arr); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 121 | this.totalLength_ += temp.length + arr.length; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | |
| 125 | /** |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 126 | * Begins a new message by writing the field header and returning a bookmark |
| 127 | * which we will use to patch in the message length to in endDelimited_ below. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 128 | * @param {number} field |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 129 | * @return {!Array<number>} |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 130 | * @private |
| 131 | */ |
| 132 | jspb.BinaryWriter.prototype.beginDelimited_ = function(field) { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 133 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); |
| 134 | var bookmark = this.encoder_.end(); |
| 135 | this.blocks_.push(bookmark); |
| 136 | this.totalLength_ += bookmark.length; |
| 137 | bookmark.push(this.totalLength_); |
| 138 | return bookmark; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | |
| 142 | /** |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 143 | * Ends a message by encoding the _change_ in length of the buffer to the |
| 144 | * parent block and adds the number of bytes needed to encode that length to |
| 145 | * the total byte length. |
| 146 | * @param {!Array<number>} bookmark |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 147 | * @private |
| 148 | */ |
| 149 | jspb.BinaryWriter.prototype.endDelimited_ = function(bookmark) { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 150 | var oldLength = bookmark.pop(); |
| 151 | var messageLength = this.totalLength_ + this.encoder_.length() - oldLength; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 152 | goog.asserts.assert(messageLength >= 0); |
| 153 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 154 | while (messageLength > 127) { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 155 | bookmark.push((messageLength & 0x7f) | 0x80); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 156 | messageLength = messageLength >>> 7; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 157 | this.totalLength_++; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 158 | } |
| 159 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 160 | bookmark.push(messageLength); |
| 161 | this.totalLength_++; |
| 162 | }; |
| 163 | |
| 164 | |
| 165 | /** |
| 166 | * Writes a pre-serialized message to the buffer. |
| 167 | * @param {!Uint8Array} bytes The array of bytes to write. |
| 168 | * @param {number} start The start of the range to write. |
| 169 | * @param {number} end The end of the range to write. |
| 170 | */ |
| 171 | jspb.BinaryWriter.prototype.writeSerializedMessage = function( |
| 172 | bytes, start, end) { |
| 173 | this.appendUint8Array_(bytes.subarray(start, end)); |
| 174 | }; |
| 175 | |
| 176 | |
| 177 | /** |
| 178 | * Writes a pre-serialized message to the buffer if the message and endpoints |
| 179 | * are non-null. |
| 180 | * @param {?Uint8Array} bytes The array of bytes to write. |
| 181 | * @param {?number} start The start of the range to write. |
| 182 | * @param {?number} end The end of the range to write. |
| 183 | */ |
| 184 | jspb.BinaryWriter.prototype.maybeWriteSerializedMessage = function( |
| 185 | bytes, start, end) { |
| 186 | if (bytes != null && start != null && end != null) { |
| 187 | this.writeSerializedMessage(bytes, start, end); |
| 188 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | |
| 192 | /** |
| 193 | * Resets the writer, throwing away any accumulated buffers. |
| 194 | */ |
| 195 | jspb.BinaryWriter.prototype.reset = function() { |
| 196 | this.blocks_ = []; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 197 | this.encoder_.end(); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 198 | this.totalLength_ = 0; |
| 199 | this.bookmarks_ = []; |
| 200 | }; |
| 201 | |
| 202 | |
| 203 | /** |
| 204 | * Converts the encoded data into a Uint8Array. |
| 205 | * @return {!Uint8Array} |
| 206 | */ |
| 207 | jspb.BinaryWriter.prototype.getResultBuffer = function() { |
| 208 | goog.asserts.assert(this.bookmarks_.length == 0); |
| 209 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 210 | var flat = new Uint8Array(this.totalLength_ + this.encoder_.length()); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 211 | |
| 212 | var blocks = this.blocks_; |
| 213 | var blockCount = blocks.length; |
| 214 | var offset = 0; |
| 215 | |
| 216 | for (var i = 0; i < blockCount; i++) { |
| 217 | var block = blocks[i]; |
| 218 | flat.set(block, offset); |
| 219 | offset += block.length; |
| 220 | } |
| 221 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 222 | var tail = this.encoder_.end(); |
| 223 | flat.set(tail, offset); |
| 224 | offset += tail.length; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 225 | |
| 226 | // Post condition: `flattened` must have had every byte written. |
| 227 | goog.asserts.assert(offset == flat.length); |
| 228 | |
| 229 | // Replace our block list with the flattened block, which lets GC reclaim |
| 230 | // the temp blocks sooner. |
| 231 | this.blocks_ = [flat]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 232 | |
| 233 | return flat; |
| 234 | }; |
| 235 | |
| 236 | |
| 237 | /** |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 238 | * Converts the encoded data into a base64-encoded string. |
| 239 | * @param {boolean=} opt_webSafe True indicates we should use a websafe |
| 240 | * alphabet, which does not require escaping for use in URLs. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 241 | * @return {string} |
| 242 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 243 | jspb.BinaryWriter.prototype.getResultBase64String = function(opt_webSafe) { |
| 244 | return goog.crypt.base64.encodeByteArray(this.getResultBuffer(), opt_webSafe); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | |
| 248 | /** |
| 249 | * Begins a new sub-message. The client must call endSubMessage() when they're |
| 250 | * done. |
| 251 | * TODO(aappleby): Deprecated. Move callers to writeMessage(). |
| 252 | * @param {number} field The field number of the sub-message. |
| 253 | */ |
| 254 | jspb.BinaryWriter.prototype.beginSubMessage = function(field) { |
| 255 | this.bookmarks_.push(this.beginDelimited_(field)); |
| 256 | }; |
| 257 | |
| 258 | |
| 259 | /** |
| 260 | * Finishes a sub-message and packs it into the parent messages' buffer. |
| 261 | * TODO(aappleby): Deprecated. Move callers to writeMessage(). |
| 262 | */ |
| 263 | jspb.BinaryWriter.prototype.endSubMessage = function() { |
| 264 | goog.asserts.assert(this.bookmarks_.length >= 0); |
| 265 | this.endDelimited_(this.bookmarks_.pop()); |
| 266 | }; |
| 267 | |
| 268 | |
| 269 | /** |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 270 | * Encodes a (field number, wire type) tuple into a wire-format field header |
| 271 | * and stores it in the buffer as a varint. |
| 272 | * @param {number} field The field number. |
| 273 | * @param {number} wireType The wire-type of the field, as specified in the |
| 274 | * protocol buffer documentation. |
| 275 | * @private |
| 276 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 277 | jspb.BinaryWriter.prototype.writeFieldHeader_ = |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 278 | function(field, wireType) { |
| 279 | goog.asserts.assert(field >= 1 && field == Math.floor(field)); |
| 280 | var x = field * 8 + wireType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 281 | this.encoder_.writeUnsignedVarint32(x); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 282 | }; |
| 283 | |
| 284 | |
| 285 | /** |
| 286 | * Writes a field of any valid scalar type to the binary stream. |
| 287 | * @param {jspb.BinaryConstants.FieldType} fieldType |
| 288 | * @param {number} field |
| 289 | * @param {jspb.AnyFieldType} value |
| 290 | */ |
| 291 | jspb.BinaryWriter.prototype.writeAny = function(fieldType, field, value) { |
| 292 | var fieldTypes = jspb.BinaryConstants.FieldType; |
| 293 | switch (fieldType) { |
| 294 | case fieldTypes.DOUBLE: |
| 295 | this.writeDouble(field, /** @type {number} */(value)); |
| 296 | return; |
| 297 | case fieldTypes.FLOAT: |
| 298 | this.writeFloat(field, /** @type {number} */(value)); |
| 299 | return; |
| 300 | case fieldTypes.INT64: |
| 301 | this.writeInt64(field, /** @type {number} */(value)); |
| 302 | return; |
| 303 | case fieldTypes.UINT64: |
| 304 | this.writeUint64(field, /** @type {number} */(value)); |
| 305 | return; |
| 306 | case fieldTypes.INT32: |
| 307 | this.writeInt32(field, /** @type {number} */(value)); |
| 308 | return; |
| 309 | case fieldTypes.FIXED64: |
| 310 | this.writeFixed64(field, /** @type {number} */(value)); |
| 311 | return; |
| 312 | case fieldTypes.FIXED32: |
| 313 | this.writeFixed32(field, /** @type {number} */(value)); |
| 314 | return; |
| 315 | case fieldTypes.BOOL: |
| 316 | this.writeBool(field, /** @type {boolean} */(value)); |
| 317 | return; |
| 318 | case fieldTypes.STRING: |
| 319 | this.writeString(field, /** @type {string} */(value)); |
| 320 | return; |
| 321 | case fieldTypes.GROUP: |
| 322 | goog.asserts.fail('Group field type not supported in writeAny()'); |
| 323 | return; |
| 324 | case fieldTypes.MESSAGE: |
| 325 | goog.asserts.fail('Message field type not supported in writeAny()'); |
| 326 | return; |
| 327 | case fieldTypes.BYTES: |
| 328 | this.writeBytes(field, /** @type {?Uint8Array} */(value)); |
| 329 | return; |
| 330 | case fieldTypes.UINT32: |
| 331 | this.writeUint32(field, /** @type {number} */(value)); |
| 332 | return; |
| 333 | case fieldTypes.ENUM: |
| 334 | this.writeEnum(field, /** @type {number} */(value)); |
| 335 | return; |
| 336 | case fieldTypes.SFIXED32: |
| 337 | this.writeSfixed32(field, /** @type {number} */(value)); |
| 338 | return; |
| 339 | case fieldTypes.SFIXED64: |
| 340 | this.writeSfixed64(field, /** @type {number} */(value)); |
| 341 | return; |
| 342 | case fieldTypes.SINT32: |
| 343 | this.writeSint32(field, /** @type {number} */(value)); |
| 344 | return; |
| 345 | case fieldTypes.SINT64: |
| 346 | this.writeSint64(field, /** @type {number} */(value)); |
| 347 | return; |
| 348 | case fieldTypes.FHASH64: |
| 349 | this.writeFixedHash64(field, /** @type {string} */(value)); |
| 350 | return; |
| 351 | case fieldTypes.VHASH64: |
| 352 | this.writeVarintHash64(field, /** @type {string} */(value)); |
| 353 | return; |
| 354 | default: |
| 355 | goog.asserts.fail('Invalid field type in writeAny()'); |
| 356 | return; |
| 357 | } |
| 358 | }; |
| 359 | |
| 360 | |
| 361 | /** |
| 362 | * Writes a varint field to the buffer without range checking. |
| 363 | * @param {number} field The field number. |
| 364 | * @param {number?} value The value to write. |
| 365 | * @private |
| 366 | */ |
| 367 | jspb.BinaryWriter.prototype.writeUnsignedVarint32_ = function(field, value) { |
| 368 | if (value == null) return; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 369 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); |
| 370 | this.encoder_.writeUnsignedVarint32(value); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 371 | }; |
| 372 | |
| 373 | |
| 374 | /** |
| 375 | * Writes a varint field to the buffer without range checking. |
| 376 | * @param {number} field The field number. |
| 377 | * @param {number?} value The value to write. |
| 378 | * @private |
| 379 | */ |
| 380 | jspb.BinaryWriter.prototype.writeSignedVarint32_ = function(field, value) { |
| 381 | if (value == null) return; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 382 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); |
| 383 | this.encoder_.writeSignedVarint32(value); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 384 | }; |
| 385 | |
| 386 | |
| 387 | /** |
| 388 | * Writes a varint field to the buffer without range checking. |
| 389 | * @param {number} field The field number. |
| 390 | * @param {number?} value The value to write. |
| 391 | * @private |
| 392 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 393 | jspb.BinaryWriter.prototype.writeUnsignedVarint64_ = function(field, value) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 394 | if (value == null) return; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 395 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); |
| 396 | this.encoder_.writeUnsignedVarint64(value); |
| 397 | }; |
| 398 | |
| 399 | |
| 400 | /** |
| 401 | * Writes a varint field to the buffer without range checking. |
| 402 | * @param {number} field The field number. |
| 403 | * @param {number?} value The value to write. |
| 404 | * @private |
| 405 | */ |
| 406 | jspb.BinaryWriter.prototype.writeSignedVarint64_ = function(field, value) { |
| 407 | if (value == null) return; |
| 408 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); |
| 409 | this.encoder_.writeSignedVarint64(value); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 410 | }; |
| 411 | |
| 412 | |
| 413 | /** |
| 414 | * Writes a zigzag varint field to the buffer without range checking. |
| 415 | * @param {number} field The field number. |
| 416 | * @param {number?} value The value to write. |
| 417 | * @private |
| 418 | */ |
| 419 | jspb.BinaryWriter.prototype.writeZigzagVarint32_ = function(field, value) { |
| 420 | if (value == null) return; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 421 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); |
| 422 | this.encoder_.writeZigzagVarint32(value); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 423 | }; |
| 424 | |
| 425 | |
| 426 | /** |
| 427 | * Writes a zigzag varint field to the buffer without range checking. |
| 428 | * @param {number} field The field number. |
| 429 | * @param {number?} value The value to write. |
| 430 | * @private |
| 431 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 432 | jspb.BinaryWriter.prototype.writeZigzagVarint64_ = function(field, value) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 433 | if (value == null) return; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 434 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); |
| 435 | this.encoder_.writeZigzagVarint64(value); |
| 436 | }; |
| 437 | |
| 438 | |
| 439 | /** |
| 440 | * Writes a zigzag varint field to the buffer without range checking. |
| 441 | * @param {number} field The field number. |
| 442 | * @param {string?} value The value to write. |
| 443 | * @private |
| 444 | */ |
| 445 | jspb.BinaryWriter.prototype.writeZigzagVarint64String_ = function( |
| 446 | field, value) { |
| 447 | if (value == null) return; |
| 448 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); |
| 449 | this.encoder_.writeZigzagVarint64String(value); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 450 | }; |
| 451 | |
| 452 | |
| 453 | /** |
| 454 | * Writes an int32 field to the buffer. Numbers outside the range [-2^31,2^31) |
| 455 | * will be truncated. |
| 456 | * @param {number} field The field number. |
| 457 | * @param {number?} value The value to write. |
| 458 | */ |
| 459 | jspb.BinaryWriter.prototype.writeInt32 = function(field, value) { |
| 460 | if (value == null) return; |
| 461 | goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) && |
| 462 | (value < jspb.BinaryConstants.TWO_TO_31)); |
| 463 | this.writeSignedVarint32_(field, value); |
| 464 | }; |
| 465 | |
| 466 | |
| 467 | /** |
| 468 | * Writes an int32 field represented as a string to the buffer. Numbers outside |
| 469 | * the range [-2^31,2^31) will be truncated. |
| 470 | * @param {number} field The field number. |
| 471 | * @param {string?} value The value to write. |
| 472 | */ |
| 473 | jspb.BinaryWriter.prototype.writeInt32String = function(field, value) { |
| 474 | if (value == null) return; |
| 475 | var intValue = /** {number} */ parseInt(value, 10); |
| 476 | goog.asserts.assert((intValue >= -jspb.BinaryConstants.TWO_TO_31) && |
| 477 | (intValue < jspb.BinaryConstants.TWO_TO_31)); |
| 478 | this.writeSignedVarint32_(field, intValue); |
| 479 | }; |
| 480 | |
| 481 | |
| 482 | /** |
| 483 | * Writes an int64 field to the buffer. Numbers outside the range [-2^63,2^63) |
| 484 | * will be truncated. |
| 485 | * @param {number} field The field number. |
| 486 | * @param {number?} value The value to write. |
| 487 | */ |
| 488 | jspb.BinaryWriter.prototype.writeInt64 = function(field, value) { |
| 489 | if (value == null) return; |
| 490 | goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) && |
| 491 | (value < jspb.BinaryConstants.TWO_TO_63)); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 492 | this.writeSignedVarint64_(field, value); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 493 | }; |
| 494 | |
| 495 | |
| 496 | /** |
| 497 | * Writes a int64 field (with value as a string) to the buffer. |
| 498 | * @param {number} field The field number. |
| 499 | * @param {string?} value The value to write. |
| 500 | */ |
| 501 | jspb.BinaryWriter.prototype.writeInt64String = function(field, value) { |
| 502 | if (value == null) return; |
| 503 | var num = jspb.arith.Int64.fromString(value); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 504 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); |
| 505 | this.encoder_.writeSplitVarint64(num.lo, num.hi); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 506 | }; |
| 507 | |
| 508 | |
| 509 | /** |
| 510 | * Writes a uint32 field to the buffer. Numbers outside the range [0,2^32) |
| 511 | * will be truncated. |
| 512 | * @param {number} field The field number. |
| 513 | * @param {number?} value The value to write. |
| 514 | */ |
| 515 | jspb.BinaryWriter.prototype.writeUint32 = function(field, value) { |
| 516 | if (value == null) return; |
| 517 | goog.asserts.assert((value >= 0) && |
| 518 | (value < jspb.BinaryConstants.TWO_TO_32)); |
| 519 | this.writeUnsignedVarint32_(field, value); |
| 520 | }; |
| 521 | |
| 522 | |
| 523 | /** |
| 524 | * Writes a uint32 field represented as a string to the buffer. Numbers outside |
| 525 | * the range [0,2^32) will be truncated. |
| 526 | * @param {number} field The field number. |
| 527 | * @param {string?} value The value to write. |
| 528 | */ |
| 529 | jspb.BinaryWriter.prototype.writeUint32String = function(field, value) { |
| 530 | if (value == null) return; |
| 531 | var intValue = /** {number} */ parseInt(value, 10); |
| 532 | goog.asserts.assert((intValue >= 0) && |
| 533 | (intValue < jspb.BinaryConstants.TWO_TO_32)); |
| 534 | this.writeUnsignedVarint32_(field, intValue); |
| 535 | }; |
| 536 | |
| 537 | |
| 538 | /** |
| 539 | * Writes a uint64 field to the buffer. Numbers outside the range [0,2^64) |
| 540 | * will be truncated. |
| 541 | * @param {number} field The field number. |
| 542 | * @param {number?} value The value to write. |
| 543 | */ |
| 544 | jspb.BinaryWriter.prototype.writeUint64 = function(field, value) { |
| 545 | if (value == null) return; |
| 546 | goog.asserts.assert((value >= 0) && |
| 547 | (value < jspb.BinaryConstants.TWO_TO_64)); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 548 | this.writeUnsignedVarint64_(field, value); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 549 | }; |
| 550 | |
| 551 | |
| 552 | /** |
| 553 | * Writes a uint64 field (with value as a string) to the buffer. |
| 554 | * @param {number} field The field number. |
| 555 | * @param {string?} value The value to write. |
| 556 | */ |
| 557 | jspb.BinaryWriter.prototype.writeUint64String = function(field, value) { |
| 558 | if (value == null) return; |
| 559 | var num = jspb.arith.UInt64.fromString(value); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 560 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); |
| 561 | this.encoder_.writeSplitVarint64(num.lo, num.hi); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 562 | }; |
| 563 | |
| 564 | |
| 565 | /** |
| 566 | * Writes a sint32 field to the buffer. Numbers outside the range [-2^31,2^31) |
| 567 | * will be truncated. |
| 568 | * @param {number} field The field number. |
| 569 | * @param {number?} value The value to write. |
| 570 | */ |
| 571 | jspb.BinaryWriter.prototype.writeSint32 = function(field, value) { |
| 572 | if (value == null) return; |
| 573 | goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) && |
| 574 | (value < jspb.BinaryConstants.TWO_TO_31)); |
| 575 | this.writeZigzagVarint32_(field, value); |
| 576 | }; |
| 577 | |
| 578 | |
| 579 | /** |
| 580 | * Writes a sint64 field to the buffer. Numbers outside the range [-2^63,2^63) |
| 581 | * will be truncated. |
| 582 | * @param {number} field The field number. |
| 583 | * @param {number?} value The value to write. |
| 584 | */ |
| 585 | jspb.BinaryWriter.prototype.writeSint64 = function(field, value) { |
| 586 | if (value == null) return; |
| 587 | goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) && |
| 588 | (value < jspb.BinaryConstants.TWO_TO_63)); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 589 | this.writeZigzagVarint64_(field, value); |
| 590 | }; |
| 591 | |
| 592 | |
| 593 | /** |
| 594 | * Writes a sint64 field to the buffer. Numbers outside the range [-2^63,2^63) |
| 595 | * will be truncated. |
| 596 | * @param {number} field The field number. |
| 597 | * @param {string?} value The decimal string to write. |
| 598 | */ |
| 599 | jspb.BinaryWriter.prototype.writeSint64String = function(field, value) { |
| 600 | if (value == null) return; |
| 601 | goog.asserts.assert((+value >= -jspb.BinaryConstants.TWO_TO_63) && |
| 602 | (+value < jspb.BinaryConstants.TWO_TO_63)); |
| 603 | this.writeZigzagVarint64String_(field, value); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 604 | }; |
| 605 | |
| 606 | |
| 607 | /** |
| 608 | * Writes a fixed32 field to the buffer. Numbers outside the range [0,2^32) |
| 609 | * will be truncated. |
| 610 | * @param {number} field The field number. |
| 611 | * @param {number?} value The value to write. |
| 612 | */ |
| 613 | jspb.BinaryWriter.prototype.writeFixed32 = function(field, value) { |
| 614 | if (value == null) return; |
| 615 | goog.asserts.assert((value >= 0) && |
| 616 | (value < jspb.BinaryConstants.TWO_TO_32)); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 617 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32); |
| 618 | this.encoder_.writeUint32(value); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 619 | }; |
| 620 | |
| 621 | |
| 622 | /** |
| 623 | * Writes a fixed64 field to the buffer. Numbers outside the range [0,2^64) |
| 624 | * will be truncated. |
| 625 | * @param {number} field The field number. |
| 626 | * @param {number?} value The value to write. |
| 627 | */ |
| 628 | jspb.BinaryWriter.prototype.writeFixed64 = function(field, value) { |
| 629 | if (value == null) return; |
| 630 | goog.asserts.assert((value >= 0) && |
| 631 | (value < jspb.BinaryConstants.TWO_TO_64)); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 632 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64); |
| 633 | this.encoder_.writeUint64(value); |
| 634 | }; |
| 635 | |
| 636 | |
| 637 | /** |
| 638 | * Writes a fixed64 field (with value as a string) to the buffer. |
| 639 | * @param {number} field The field number. |
| 640 | * @param {string?} value The value to write. |
| 641 | */ |
| 642 | jspb.BinaryWriter.prototype.writeFixed64String = function(field, value) { |
| 643 | if (value == null) return; |
| 644 | var num = jspb.arith.UInt64.fromString(value); |
| 645 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64); |
| 646 | this.encoder_.writeSplitFixed64(num.lo, num.hi); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 647 | }; |
| 648 | |
| 649 | |
| 650 | /** |
| 651 | * Writes a sfixed32 field to the buffer. Numbers outside the range |
| 652 | * [-2^31,2^31) will be truncated. |
| 653 | * @param {number} field The field number. |
| 654 | * @param {number?} value The value to write. |
| 655 | */ |
| 656 | jspb.BinaryWriter.prototype.writeSfixed32 = function(field, value) { |
| 657 | if (value == null) return; |
| 658 | goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) && |
| 659 | (value < jspb.BinaryConstants.TWO_TO_31)); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 660 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32); |
| 661 | this.encoder_.writeInt32(value); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 662 | }; |
| 663 | |
| 664 | |
| 665 | /** |
| 666 | * Writes a sfixed64 field to the buffer. Numbers outside the range |
| 667 | * [-2^63,2^63) will be truncated. |
| 668 | * @param {number} field The field number. |
| 669 | * @param {number?} value The value to write. |
| 670 | */ |
| 671 | jspb.BinaryWriter.prototype.writeSfixed64 = function(field, value) { |
| 672 | if (value == null) return; |
| 673 | goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) && |
| 674 | (value < jspb.BinaryConstants.TWO_TO_63)); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 675 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64); |
| 676 | this.encoder_.writeInt64(value); |
| 677 | }; |
| 678 | |
| 679 | |
| 680 | /** |
| 681 | * Writes a sfixed64 string field to the buffer. Numbers outside the range |
| 682 | * [-2^63,2^63) will be truncated. |
| 683 | * @param {number} field The field number. |
| 684 | * @param {string?} value The value to write. |
| 685 | */ |
| 686 | jspb.BinaryWriter.prototype.writeSfixed64String = function(field, value) { |
| 687 | if (value == null) return; |
| 688 | var num = jspb.arith.Int64.fromString(value); |
| 689 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64); |
| 690 | this.encoder_.writeSplitFixed64(num.lo, num.hi); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 691 | }; |
| 692 | |
| 693 | |
| 694 | /** |
| 695 | * Writes a single-precision floating point field to the buffer. Numbers |
| 696 | * requiring more than 32 bits of precision will be truncated. |
| 697 | * @param {number} field The field number. |
| 698 | * @param {number?} value The value to write. |
| 699 | */ |
| 700 | jspb.BinaryWriter.prototype.writeFloat = function(field, value) { |
| 701 | if (value == null) return; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 702 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32); |
| 703 | this.encoder_.writeFloat(value); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 704 | }; |
| 705 | |
| 706 | |
| 707 | /** |
| 708 | * Writes a double-precision floating point field to the buffer. As this is the |
| 709 | * native format used by JavaScript, no precision will be lost. |
| 710 | * @param {number} field The field number. |
| 711 | * @param {number?} value The value to write. |
| 712 | */ |
| 713 | jspb.BinaryWriter.prototype.writeDouble = function(field, value) { |
| 714 | if (value == null) return; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 715 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64); |
| 716 | this.encoder_.writeDouble(value); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 717 | }; |
| 718 | |
| 719 | |
| 720 | /** |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 721 | * Writes a boolean field to the buffer. We allow numbers as input |
| 722 | * because the JSPB code generator uses 0/1 instead of true/false to save space |
| 723 | * in the string representation of the proto. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 724 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 725 | * @param {boolean?|number?} value The value to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 726 | */ |
| 727 | jspb.BinaryWriter.prototype.writeBool = function(field, value) { |
| 728 | if (value == null) return; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 729 | goog.asserts.assert(goog.isBoolean(value) || goog.isNumber(value)); |
| 730 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); |
| 731 | this.encoder_.writeBool(value); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 732 | }; |
| 733 | |
| 734 | |
| 735 | /** |
| 736 | * Writes an enum field to the buffer. |
| 737 | * @param {number} field The field number. |
| 738 | * @param {number?} value The value to write. |
| 739 | */ |
| 740 | jspb.BinaryWriter.prototype.writeEnum = function(field, value) { |
| 741 | if (value == null) return; |
| 742 | goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) && |
| 743 | (value < jspb.BinaryConstants.TWO_TO_31)); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 744 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); |
| 745 | this.encoder_.writeSignedVarint32(value); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 746 | }; |
| 747 | |
| 748 | |
| 749 | /** |
| 750 | * Writes a string field to the buffer. |
| 751 | * @param {number} field The field number. |
| 752 | * @param {string?} value The string to write. |
| 753 | */ |
| 754 | jspb.BinaryWriter.prototype.writeString = function(field, value) { |
| 755 | if (value == null) return; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 756 | var bookmark = this.beginDelimited_(field); |
| 757 | this.encoder_.writeString(value); |
| 758 | this.endDelimited_(bookmark); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 759 | }; |
| 760 | |
| 761 | |
| 762 | /** |
| 763 | * Writes an arbitrary byte field to the buffer. Note - to match the behavior |
| 764 | * of the C++ implementation, empty byte arrays _are_ serialized. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 765 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 766 | * @param {?jspb.ByteSource} value The array of bytes to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 767 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 768 | jspb.BinaryWriter.prototype.writeBytes = function(field, value) { |
| 769 | if (value == null) return; |
| 770 | var bytes = jspb.utils.byteSourceToUint8Array(value); |
| 771 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); |
| 772 | this.encoder_.writeUnsignedVarint32(bytes.length); |
| 773 | this.appendUint8Array_(bytes); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 774 | }; |
| 775 | |
| 776 | |
| 777 | /** |
| 778 | * Writes a message to the buffer. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 779 | * @param {number} field The field number. |
| 780 | * @param {?MessageType} value The message to write. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 781 | * @param {function(MessageTypeNonNull, !jspb.BinaryWriter)} writerCallback |
| 782 | * Will be invoked with the value to write and the writer to write it with. |
| 783 | * @template MessageType |
| 784 | * Use go/closure-ttl to declare a non-nullable version of MessageType. Replace |
| 785 | * the null in blah|null with none. This is necessary because the compiler will |
| 786 | * infer MessageType to be nullable if the value parameter is nullable. |
| 787 | * @template MessageTypeNonNull := |
| 788 | * cond(isUnknown(MessageType), unknown(), |
| 789 | * mapunion(MessageType, (X) => |
| 790 | * cond(eq(X, 'null'), none(), X))) |
| 791 | * =: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 792 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 793 | jspb.BinaryWriter.prototype.writeMessage = function( |
| 794 | field, value, writerCallback) { |
| 795 | if (value == null) return; |
| 796 | var bookmark = this.beginDelimited_(field); |
| 797 | writerCallback(value, this); |
| 798 | this.endDelimited_(bookmark); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 799 | }; |
| 800 | |
| 801 | |
| 802 | /** |
| 803 | * Writes a group message to the buffer. |
| 804 | * |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 805 | * @param {number} field The field number. |
| 806 | * @param {?MessageType} value The message to write, wrapped with START_GROUP / |
| 807 | * END_GROUP tags. Will be a no-op if 'value' is null. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 808 | * @param {function(MessageTypeNonNull, !jspb.BinaryWriter)} writerCallback |
| 809 | * Will be invoked with the value to write and the writer to write it with. |
| 810 | * @template MessageType |
| 811 | * Use go/closure-ttl to declare a non-nullable version of MessageType. Replace |
| 812 | * the null in blah|null with none. This is necessary because the compiler will |
| 813 | * infer MessageType to be nullable if the value parameter is nullable. |
| 814 | * @template MessageTypeNonNull := |
| 815 | * cond(isUnknown(MessageType), unknown(), |
| 816 | * mapunion(MessageType, (X) => |
| 817 | * cond(eq(X, 'null'), none(), X))) |
| 818 | * =: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 819 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 820 | jspb.BinaryWriter.prototype.writeGroup = function( |
| 821 | field, value, writerCallback) { |
| 822 | if (value == null) return; |
| 823 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.START_GROUP); |
| 824 | writerCallback(value, this); |
| 825 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.END_GROUP); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 826 | }; |
| 827 | |
| 828 | |
| 829 | /** |
| 830 | * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to |
| 831 | * the buffer. |
| 832 | * @param {number} field The field number. |
| 833 | * @param {string?} value The hash string. |
| 834 | */ |
| 835 | jspb.BinaryWriter.prototype.writeFixedHash64 = function(field, value) { |
| 836 | if (value == null) return; |
| 837 | goog.asserts.assert(value.length == 8); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 838 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64); |
| 839 | this.encoder_.writeFixedHash64(value); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 840 | }; |
| 841 | |
| 842 | |
| 843 | /** |
| 844 | * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to |
| 845 | * the buffer. |
| 846 | * @param {number} field The field number. |
| 847 | * @param {string?} value The hash string. |
| 848 | */ |
| 849 | jspb.BinaryWriter.prototype.writeVarintHash64 = function(field, value) { |
| 850 | if (value == null) return; |
| 851 | goog.asserts.assert(value.length == 8); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 852 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); |
| 853 | this.encoder_.writeVarintHash64(value); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 854 | }; |
| 855 | |
| 856 | |
| 857 | /** |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 858 | * Writes an array of numbers to the buffer as a repeated 32-bit int field. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 859 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 860 | * @param {?Array<number>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 861 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 862 | jspb.BinaryWriter.prototype.writeRepeatedInt32 = function(field, value) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 863 | if (value == null) return; |
| 864 | for (var i = 0; i < value.length; i++) { |
| 865 | this.writeSignedVarint32_(field, value[i]); |
| 866 | } |
| 867 | }; |
| 868 | |
| 869 | |
| 870 | /** |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 871 | * Writes an array of numbers formatted as strings to the buffer as a repeated |
| 872 | * 32-bit int field. |
| 873 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 874 | * @param {?Array<string>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 875 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 876 | jspb.BinaryWriter.prototype.writeRepeatedInt32String = function(field, value) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 877 | if (value == null) return; |
| 878 | 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. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 887 | * @param {?Array<number>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 888 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 889 | jspb.BinaryWriter.prototype.writeRepeatedInt64 = function(field, value) { |
| 890 | if (value == null) return; |
| 891 | for (var i = 0; i < value.length; i++) { |
| 892 | this.writeSignedVarint64_(field, value[i]); |
| 893 | } |
| 894 | }; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 895 | |
| 896 | |
| 897 | /** |
| 898 | * Writes an array of numbers formatted as strings to the buffer as a repeated |
| 899 | * 64-bit int field. |
| 900 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 901 | * @param {?Array<string>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 902 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 903 | jspb.BinaryWriter.prototype.writeRepeatedInt64String = function(field, value) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 904 | if (value == null) return; |
| 905 | for (var i = 0; i < value.length; i++) { |
| 906 | this.writeInt64String(field, value[i]); |
| 907 | } |
| 908 | }; |
| 909 | |
| 910 | |
| 911 | /** |
| 912 | * Writes an array numbers to the buffer as a repeated unsigned 32-bit int |
| 913 | * field. |
| 914 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 915 | * @param {?Array<number>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 916 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 917 | jspb.BinaryWriter.prototype.writeRepeatedUint32 = function(field, value) { |
| 918 | if (value == null) return; |
| 919 | for (var i = 0; i < value.length; i++) { |
| 920 | this.writeUnsignedVarint32_(field, value[i]); |
| 921 | } |
| 922 | }; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 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. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 929 | * @param {?Array<string>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 930 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 931 | jspb.BinaryWriter.prototype.writeRepeatedUint32String = function(field, value) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 932 | if (value == null) return; |
| 933 | for (var i = 0; i < value.length; i++) { |
| 934 | this.writeUint32String(field, value[i]); |
| 935 | } |
| 936 | }; |
| 937 | |
| 938 | |
| 939 | /** |
| 940 | * Writes an array numbers to the buffer as a repeated unsigned 64-bit int |
| 941 | * field. |
| 942 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 943 | * @param {?Array<number>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 944 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 945 | jspb.BinaryWriter.prototype.writeRepeatedUint64 = function(field, value) { |
| 946 | if (value == null) return; |
| 947 | for (var i = 0; i < value.length; i++) { |
| 948 | this.writeUnsignedVarint64_(field, value[i]); |
| 949 | } |
| 950 | }; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 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. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 957 | * @param {?Array<string>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 958 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 959 | jspb.BinaryWriter.prototype.writeRepeatedUint64String = function(field, value) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 960 | if (value == null) return; |
| 961 | 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. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 970 | * @param {?Array<number>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 971 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 972 | jspb.BinaryWriter.prototype.writeRepeatedSint32 = function(field, value) { |
| 973 | if (value == null) return; |
| 974 | for (var i = 0; i < value.length; i++) { |
| 975 | this.writeZigzagVarint32_(field, value[i]); |
| 976 | } |
| 977 | }; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 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. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 983 | * @param {?Array<number>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 984 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 985 | jspb.BinaryWriter.prototype.writeRepeatedSint64 = function(field, value) { |
| 986 | if (value == null) return; |
| 987 | for (var i = 0; i < value.length; i++) { |
| 988 | this.writeZigzagVarint64_(field, value[i]); |
| 989 | } |
| 990 | }; |
| 991 | |
| 992 | |
| 993 | /** |
| 994 | * Writes an array numbers to the buffer as a repeated signed 64-bit int field. |
| 995 | * @param {number} field The field number. |
| 996 | * @param {?Array<string>} value The array of ints to write. |
| 997 | */ |
| 998 | jspb.BinaryWriter.prototype.writeRepeatedSint64String = function(field, value) { |
| 999 | if (value == null) return; |
| 1000 | for (var i = 0; i < value.length; i++) { |
| 1001 | this.writeZigzagVarint64String_(field, value[i]); |
| 1002 | } |
| 1003 | }; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1004 | |
| 1005 | |
| 1006 | /** |
| 1007 | * Writes an array of numbers to the buffer as a repeated fixed32 field. This |
| 1008 | * works for both signed and unsigned fixed32s. |
| 1009 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1010 | * @param {?Array<number>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1011 | */ |
| 1012 | jspb.BinaryWriter.prototype.writeRepeatedFixed32 = function(field, value) { |
| 1013 | if (value == null) return; |
| 1014 | for (var i = 0; i < value.length; i++) { |
| 1015 | this.writeFixed32(field, value[i]); |
| 1016 | } |
| 1017 | }; |
| 1018 | |
| 1019 | |
| 1020 | /** |
| 1021 | * Writes an array of numbers to the buffer as a repeated fixed64 field. This |
| 1022 | * works for both signed and unsigned fixed64s. |
| 1023 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1024 | * @param {?Array<number>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1025 | */ |
| 1026 | jspb.BinaryWriter.prototype.writeRepeatedFixed64 = function(field, value) { |
| 1027 | if (value == null) return; |
| 1028 | for (var i = 0; i < value.length; i++) { |
| 1029 | this.writeFixed64(field, value[i]); |
| 1030 | } |
| 1031 | }; |
| 1032 | |
| 1033 | |
| 1034 | /** |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1035 | * Writes an array of numbers to the buffer as a repeated fixed64 field. This |
| 1036 | * works for both signed and unsigned fixed64s. |
| 1037 | * @param {number} field The field number. |
| 1038 | * @param {?Array<string>} value The array of decimal strings to write. |
| 1039 | */ |
| 1040 | jspb.BinaryWriter.prototype.writeRepeatedFixed64String = function( |
| 1041 | field, value) { |
| 1042 | if (value == null) return; |
| 1043 | for (var i = 0; i < value.length; i++) { |
| 1044 | this.writeFixed64String(field, value[i]); |
| 1045 | } |
| 1046 | }; |
| 1047 | |
| 1048 | |
| 1049 | /** |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1050 | * Writes an array of numbers to the buffer as a repeated sfixed32 field. |
| 1051 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1052 | * @param {?Array<number>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1053 | */ |
| 1054 | jspb.BinaryWriter.prototype.writeRepeatedSfixed32 = function(field, value) { |
| 1055 | if (value == null) return; |
| 1056 | for (var i = 0; i < value.length; i++) { |
| 1057 | this.writeSfixed32(field, value[i]); |
| 1058 | } |
| 1059 | }; |
| 1060 | |
| 1061 | |
| 1062 | /** |
| 1063 | * Writes an array of numbers to the buffer as a repeated sfixed64 field. |
| 1064 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1065 | * @param {?Array<number>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1066 | */ |
| 1067 | jspb.BinaryWriter.prototype.writeRepeatedSfixed64 = function(field, value) { |
| 1068 | if (value == null) return; |
| 1069 | for (var i = 0; i < value.length; i++) { |
| 1070 | this.writeSfixed64(field, value[i]); |
| 1071 | } |
| 1072 | }; |
| 1073 | |
| 1074 | |
| 1075 | /** |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1076 | * Writes an array of decimal strings to the buffer as a repeated sfixed64 |
| 1077 | * field. |
| 1078 | * @param {number} field The field number. |
| 1079 | * @param {?Array<string>} value The array of decimal strings to write. |
| 1080 | */ |
| 1081 | jspb.BinaryWriter.prototype.writeRepeatedSfixed64String = function(field, value) { |
| 1082 | if (value == null) return; |
| 1083 | for (var i = 0; i < value.length; i++) { |
| 1084 | this.writeSfixed64String(field, value[i]); |
| 1085 | } |
| 1086 | }; |
| 1087 | |
| 1088 | |
| 1089 | /** |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1090 | * Writes an array of numbers to the buffer as a repeated float field. |
| 1091 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1092 | * @param {?Array<number>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1093 | */ |
| 1094 | jspb.BinaryWriter.prototype.writeRepeatedFloat = function(field, value) { |
| 1095 | if (value == null) return; |
| 1096 | for (var i = 0; i < value.length; i++) { |
| 1097 | this.writeFloat(field, value[i]); |
| 1098 | } |
| 1099 | }; |
| 1100 | |
| 1101 | |
| 1102 | /** |
| 1103 | * Writes an array of numbers to the buffer as a repeated double field. |
| 1104 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1105 | * @param {?Array<number>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1106 | */ |
| 1107 | jspb.BinaryWriter.prototype.writeRepeatedDouble = function(field, value) { |
| 1108 | if (value == null) return; |
| 1109 | for (var i = 0; i < value.length; i++) { |
| 1110 | this.writeDouble(field, value[i]); |
| 1111 | } |
| 1112 | }; |
| 1113 | |
| 1114 | |
| 1115 | /** |
| 1116 | * Writes an array of booleans to the buffer as a repeated bool field. |
| 1117 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1118 | * @param {?Array<boolean>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1119 | */ |
| 1120 | jspb.BinaryWriter.prototype.writeRepeatedBool = function(field, value) { |
| 1121 | if (value == null) return; |
| 1122 | for (var i = 0; i < value.length; i++) { |
| 1123 | this.writeBool(field, value[i]); |
| 1124 | } |
| 1125 | }; |
| 1126 | |
| 1127 | |
| 1128 | /** |
| 1129 | * Writes an array of enums to the buffer as a repeated enum field. |
| 1130 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1131 | * @param {?Array<number>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1132 | */ |
| 1133 | jspb.BinaryWriter.prototype.writeRepeatedEnum = function(field, value) { |
| 1134 | if (value == null) return; |
| 1135 | for (var i = 0; i < value.length; i++) { |
| 1136 | this.writeEnum(field, value[i]); |
| 1137 | } |
| 1138 | }; |
| 1139 | |
| 1140 | |
| 1141 | /** |
| 1142 | * Writes an array of strings to the buffer as a repeated string field. |
| 1143 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1144 | * @param {?Array<string>} value The array of strings to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1145 | */ |
| 1146 | jspb.BinaryWriter.prototype.writeRepeatedString = function(field, value) { |
| 1147 | if (value == null) return; |
| 1148 | for (var i = 0; i < value.length; i++) { |
| 1149 | this.writeString(field, value[i]); |
| 1150 | } |
| 1151 | }; |
| 1152 | |
| 1153 | |
| 1154 | /** |
| 1155 | * Writes an array of arbitrary byte fields to the buffer. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1156 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1157 | * @param {?Array<!jspb.ByteSource>} value The arrays of arrays of bytes to |
| 1158 | * write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1159 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1160 | jspb.BinaryWriter.prototype.writeRepeatedBytes = function(field, value) { |
| 1161 | if (value == null) return; |
| 1162 | for (var i = 0; i < value.length; i++) { |
| 1163 | this.writeBytes(field, value[i]); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1164 | } |
| 1165 | }; |
| 1166 | |
| 1167 | |
| 1168 | /** |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1169 | * Writes an array of messages to the buffer. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1170 | * @template MessageType |
| 1171 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1172 | * @param {?Array<MessageType>} value The array of messages to |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1173 | * write. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1174 | * @param {function(MessageType, !jspb.BinaryWriter)} writerCallback |
| 1175 | * Will be invoked with the value to write and the writer to write it with. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1176 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1177 | jspb.BinaryWriter.prototype.writeRepeatedMessage = function( |
| 1178 | field, value, writerCallback) { |
| 1179 | if (value == null) return; |
| 1180 | for (var i = 0; i < value.length; i++) { |
| 1181 | var bookmark = this.beginDelimited_(field); |
| 1182 | writerCallback(value[i], this); |
| 1183 | this.endDelimited_(bookmark); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1184 | } |
| 1185 | }; |
| 1186 | |
| 1187 | |
| 1188 | /** |
| 1189 | * Writes an array of group messages to the buffer. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1190 | * @template MessageType |
| 1191 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1192 | * @param {?Array<MessageType>} value The array of messages to |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1193 | * write. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1194 | * @param {function(MessageType, !jspb.BinaryWriter)} writerCallback |
| 1195 | * Will be invoked with the value to write and the writer to write it with. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1196 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1197 | jspb.BinaryWriter.prototype.writeRepeatedGroup = function( |
| 1198 | field, value, writerCallback) { |
| 1199 | if (value == null) return; |
| 1200 | for (var i = 0; i < value.length; i++) { |
| 1201 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.START_GROUP); |
| 1202 | writerCallback(value[i], this); |
| 1203 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.END_GROUP); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1204 | } |
| 1205 | }; |
| 1206 | |
| 1207 | |
| 1208 | /** |
| 1209 | * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to |
| 1210 | * the buffer. |
| 1211 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1212 | * @param {?Array<string>} value The array of hashes to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1213 | */ |
| 1214 | jspb.BinaryWriter.prototype.writeRepeatedFixedHash64 = |
| 1215 | function(field, value) { |
| 1216 | if (value == null) return; |
| 1217 | for (var i = 0; i < value.length; i++) { |
| 1218 | this.writeFixedHash64(field, value[i]); |
| 1219 | } |
| 1220 | }; |
| 1221 | |
| 1222 | |
| 1223 | /** |
| 1224 | * Writes a repeated 64-bit hash string field (8 characters @ 8 bits of data |
| 1225 | * each) to the buffer. |
| 1226 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1227 | * @param {?Array<string>} value The array of hashes to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1228 | */ |
| 1229 | jspb.BinaryWriter.prototype.writeRepeatedVarintHash64 = |
| 1230 | function(field, value) { |
| 1231 | if (value == null) return; |
| 1232 | for (var i = 0; i < value.length; i++) { |
| 1233 | this.writeVarintHash64(field, value[i]); |
| 1234 | } |
| 1235 | }; |
| 1236 | |
| 1237 | |
| 1238 | /** |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1239 | * Writes an array of numbers to the buffer as a packed 32-bit int field. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1240 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1241 | * @param {?Array<number>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1242 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1243 | jspb.BinaryWriter.prototype.writePackedInt32 = function(field, value) { |
| 1244 | if (value == null || !value.length) return; |
| 1245 | var bookmark = this.beginDelimited_(field); |
| 1246 | for (var i = 0; i < value.length; i++) { |
| 1247 | this.encoder_.writeSignedVarint32(value[i]); |
| 1248 | } |
| 1249 | this.endDelimited_(bookmark); |
| 1250 | }; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1251 | |
| 1252 | |
| 1253 | /** |
| 1254 | * Writes an array of numbers represented as strings to the buffer as a packed |
| 1255 | * 32-bit int field. |
| 1256 | * @param {number} field |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1257 | * @param {?Array<string>} value |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1258 | */ |
| 1259 | jspb.BinaryWriter.prototype.writePackedInt32String = function(field, value) { |
| 1260 | if (value == null || !value.length) return; |
| 1261 | var bookmark = this.beginDelimited_(field); |
| 1262 | for (var i = 0; i < value.length; i++) { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1263 | this.encoder_.writeSignedVarint32(parseInt(value[i], 10)); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 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. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1272 | * @param {?Array<number>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1273 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1274 | jspb.BinaryWriter.prototype.writePackedInt64 = function(field, value) { |
| 1275 | if (value == null || !value.length) return; |
| 1276 | var bookmark = this.beginDelimited_(field); |
| 1277 | for (var i = 0; i < value.length; i++) { |
| 1278 | this.encoder_.writeSignedVarint64(value[i]); |
| 1279 | } |
| 1280 | this.endDelimited_(bookmark); |
| 1281 | }; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1282 | |
| 1283 | |
| 1284 | /** |
| 1285 | * Writes an array of numbers represented as strings to the buffer as a packed |
| 1286 | * 64-bit int field. |
| 1287 | * @param {number} field |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1288 | * @param {?Array<string>} value |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1289 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1290 | jspb.BinaryWriter.prototype.writePackedInt64String = function(field, value) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1291 | if (value == null || !value.length) return; |
| 1292 | var bookmark = this.beginDelimited_(field); |
| 1293 | for (var i = 0; i < value.length; i++) { |
| 1294 | var num = jspb.arith.Int64.fromString(value[i]); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1295 | this.encoder_.writeSplitVarint64(num.lo, num.hi); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 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. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1303 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1304 | * @param {?Array<number>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1305 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1306 | jspb.BinaryWriter.prototype.writePackedUint32 = function(field, value) { |
| 1307 | if (value == null || !value.length) return; |
| 1308 | var bookmark = this.beginDelimited_(field); |
| 1309 | for (var i = 0; i < value.length; i++) { |
| 1310 | this.encoder_.writeUnsignedVarint32(value[i]); |
| 1311 | } |
| 1312 | this.endDelimited_(bookmark); |
| 1313 | }; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1314 | |
| 1315 | |
| 1316 | /** |
| 1317 | * Writes an array of numbers represented as strings to the buffer as a packed |
| 1318 | * unsigned 32-bit int field. |
| 1319 | * @param {number} field |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1320 | * @param {?Array<string>} value |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1321 | */ |
| 1322 | jspb.BinaryWriter.prototype.writePackedUint32String = |
| 1323 | function(field, value) { |
| 1324 | if (value == null || !value.length) return; |
| 1325 | var bookmark = this.beginDelimited_(field); |
| 1326 | for (var i = 0; i < value.length; i++) { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1327 | this.encoder_.writeUnsignedVarint32(parseInt(value[i], 10)); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 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. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1335 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1336 | * @param {?Array<number>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1337 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1338 | jspb.BinaryWriter.prototype.writePackedUint64 = function(field, value) { |
| 1339 | if (value == null || !value.length) return; |
| 1340 | var bookmark = this.beginDelimited_(field); |
| 1341 | for (var i = 0; i < value.length; i++) { |
| 1342 | this.encoder_.writeUnsignedVarint64(value[i]); |
| 1343 | } |
| 1344 | this.endDelimited_(bookmark); |
| 1345 | }; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1346 | |
| 1347 | |
| 1348 | /** |
| 1349 | * Writes an array of numbers represented as strings to the buffer as a packed |
| 1350 | * unsigned 64-bit int field. |
| 1351 | * @param {number} field |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1352 | * @param {?Array<string>} value |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1353 | */ |
| 1354 | jspb.BinaryWriter.prototype.writePackedUint64String = |
| 1355 | function(field, value) { |
| 1356 | if (value == null || !value.length) return; |
| 1357 | var bookmark = this.beginDelimited_(field); |
| 1358 | for (var i = 0; i < value.length; i++) { |
| 1359 | var num = jspb.arith.UInt64.fromString(value[i]); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1360 | this.encoder_.writeSplitVarint64(num.lo, num.hi); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1361 | } |
| 1362 | this.endDelimited_(bookmark); |
| 1363 | }; |
| 1364 | |
| 1365 | |
| 1366 | /** |
| 1367 | * Writes an array numbers to the buffer as a packed signed 32-bit int field. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1368 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1369 | * @param {?Array<number>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1370 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1371 | jspb.BinaryWriter.prototype.writePackedSint32 = function(field, value) { |
| 1372 | if (value == null || !value.length) return; |
| 1373 | var bookmark = this.beginDelimited_(field); |
| 1374 | for (var i = 0; i < value.length; i++) { |
| 1375 | this.encoder_.writeZigzagVarint32(value[i]); |
| 1376 | } |
| 1377 | this.endDelimited_(bookmark); |
| 1378 | }; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1379 | |
| 1380 | |
| 1381 | /** |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1382 | * Writes an array of numbers to the buffer as a packed signed 64-bit int field. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1383 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1384 | * @param {?Array<number>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1385 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1386 | jspb.BinaryWriter.prototype.writePackedSint64 = function(field, value) { |
| 1387 | if (value == null || !value.length) return; |
| 1388 | var bookmark = this.beginDelimited_(field); |
| 1389 | for (var i = 0; i < value.length; i++) { |
| 1390 | this.encoder_.writeZigzagVarint64(value[i]); |
| 1391 | } |
| 1392 | this.endDelimited_(bookmark); |
| 1393 | }; |
| 1394 | |
| 1395 | |
| 1396 | /** |
| 1397 | * Writes an array of decimal strings to the buffer as a packed signed 64-bit |
| 1398 | * int field. |
| 1399 | * @param {number} field The field number. |
| 1400 | * @param {?Array<string>} value The array of decimal strings to write. |
| 1401 | */ |
| 1402 | jspb.BinaryWriter.prototype.writePackedSint64String = function(field, value) { |
| 1403 | if (value == null || !value.length) return; |
| 1404 | var bookmark = this.beginDelimited_(field); |
| 1405 | for (var i = 0; i < value.length; i++) { |
| 1406 | // TODO(haberman): make lossless |
| 1407 | this.encoder_.writeZigzagVarint64(parseInt(value[i], 10)); |
| 1408 | } |
| 1409 | this.endDelimited_(bookmark); |
| 1410 | }; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1411 | |
| 1412 | |
| 1413 | /** |
| 1414 | * Writes an array of numbers to the buffer as a packed fixed32 field. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1415 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1416 | * @param {?Array<number>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1417 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1418 | jspb.BinaryWriter.prototype.writePackedFixed32 = function(field, value) { |
| 1419 | if (value == null || !value.length) return; |
| 1420 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); |
| 1421 | this.encoder_.writeUnsignedVarint32(value.length * 4); |
| 1422 | for (var i = 0; i < value.length; i++) { |
| 1423 | this.encoder_.writeUint32(value[i]); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1424 | } |
| 1425 | }; |
| 1426 | |
| 1427 | |
| 1428 | /** |
| 1429 | * Writes an array of numbers to the buffer as a packed fixed64 field. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1430 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1431 | * @param {?Array<number>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1432 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1433 | jspb.BinaryWriter.prototype.writePackedFixed64 = function(field, value) { |
| 1434 | if (value == null || !value.length) return; |
| 1435 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); |
| 1436 | this.encoder_.writeUnsignedVarint32(value.length * 8); |
| 1437 | for (var i = 0; i < value.length; i++) { |
| 1438 | this.encoder_.writeUint64(value[i]); |
| 1439 | } |
| 1440 | }; |
| 1441 | |
| 1442 | |
| 1443 | /** |
| 1444 | * Writes an array of numbers represented as strings to the buffer as a packed |
| 1445 | * fixed64 field. |
| 1446 | * @param {number} field The field number. |
| 1447 | * @param {?Array<string>} value The array of strings to write. |
| 1448 | */ |
| 1449 | jspb.BinaryWriter.prototype.writePackedFixed64String = function(field, value) { |
| 1450 | if (value == null || !value.length) return; |
| 1451 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); |
| 1452 | this.encoder_.writeUnsignedVarint32(value.length * 8); |
| 1453 | for (var i = 0; i < value.length; i++) { |
| 1454 | var num = jspb.arith.UInt64.fromString(value[i]); |
| 1455 | this.encoder_.writeSplitFixed64(num.lo, num.hi); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1456 | } |
| 1457 | }; |
| 1458 | |
| 1459 | |
| 1460 | /** |
| 1461 | * Writes an array of numbers to the buffer as a packed sfixed32 field. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1462 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1463 | * @param {?Array<number>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1464 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1465 | jspb.BinaryWriter.prototype.writePackedSfixed32 = function(field, value) { |
| 1466 | if (value == null || !value.length) return; |
| 1467 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); |
| 1468 | this.encoder_.writeUnsignedVarint32(value.length * 4); |
| 1469 | for (var i = 0; i < value.length; i++) { |
| 1470 | this.encoder_.writeInt32(value[i]); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1471 | } |
| 1472 | }; |
| 1473 | |
| 1474 | |
| 1475 | /** |
| 1476 | * Writes an array of numbers to the buffer as a packed sfixed64 field. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1477 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1478 | * @param {?Array<number>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1479 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1480 | jspb.BinaryWriter.prototype.writePackedSfixed64 = function(field, value) { |
| 1481 | if (value == null || !value.length) return; |
| 1482 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); |
| 1483 | this.encoder_.writeUnsignedVarint32(value.length * 8); |
| 1484 | for (var i = 0; i < value.length; i++) { |
| 1485 | this.encoder_.writeInt64(value[i]); |
| 1486 | } |
| 1487 | }; |
| 1488 | |
| 1489 | |
| 1490 | /** |
| 1491 | * Writes an array of numbers to the buffer as a packed sfixed64 field. |
| 1492 | * @param {number} field The field number. |
| 1493 | * @param {?Array<string>} value The array of decimal strings to write. |
| 1494 | */ |
| 1495 | jspb.BinaryWriter.prototype.writePackedSfixed64String = function(field, value) { |
| 1496 | if (value == null || !value.length) return; |
| 1497 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); |
| 1498 | this.encoder_.writeUnsignedVarint32(value.length * 8); |
| 1499 | for (var i = 0; i < value.length; i++) { |
| 1500 | this.encoder_.writeInt64String(value[i]); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1501 | } |
| 1502 | }; |
| 1503 | |
| 1504 | |
| 1505 | /** |
| 1506 | * Writes an array of numbers to the buffer as a packed float field. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1507 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1508 | * @param {?Array<number>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1509 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1510 | jspb.BinaryWriter.prototype.writePackedFloat = function(field, value) { |
| 1511 | if (value == null || !value.length) return; |
| 1512 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); |
| 1513 | this.encoder_.writeUnsignedVarint32(value.length * 4); |
| 1514 | for (var i = 0; i < value.length; i++) { |
| 1515 | this.encoder_.writeFloat(value[i]); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1516 | } |
| 1517 | }; |
| 1518 | |
| 1519 | |
| 1520 | /** |
| 1521 | * Writes an array of numbers to the buffer as a packed double field. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1522 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1523 | * @param {?Array<number>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1524 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1525 | jspb.BinaryWriter.prototype.writePackedDouble = function(field, value) { |
| 1526 | if (value == null || !value.length) return; |
| 1527 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); |
| 1528 | this.encoder_.writeUnsignedVarint32(value.length * 8); |
| 1529 | for (var i = 0; i < value.length; i++) { |
| 1530 | this.encoder_.writeDouble(value[i]); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1531 | } |
| 1532 | }; |
| 1533 | |
| 1534 | |
| 1535 | /** |
| 1536 | * Writes an array of booleans to the buffer as a packed bool field. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1537 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1538 | * @param {?Array<boolean>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1539 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1540 | jspb.BinaryWriter.prototype.writePackedBool = function(field, value) { |
| 1541 | if (value == null || !value.length) return; |
| 1542 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); |
| 1543 | this.encoder_.writeUnsignedVarint32(value.length); |
| 1544 | for (var i = 0; i < value.length; i++) { |
| 1545 | this.encoder_.writeBool(value[i]); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1546 | } |
| 1547 | }; |
| 1548 | |
| 1549 | |
| 1550 | /** |
| 1551 | * Writes an array of enums to the buffer as a packed enum field. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1552 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1553 | * @param {?Array<number>} value The array of ints to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1554 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1555 | jspb.BinaryWriter.prototype.writePackedEnum = function(field, value) { |
| 1556 | if (value == null || !value.length) return; |
| 1557 | var bookmark = this.beginDelimited_(field); |
| 1558 | for (var i = 0; i < value.length; i++) { |
| 1559 | this.encoder_.writeEnum(value[i]); |
| 1560 | } |
| 1561 | this.endDelimited_(bookmark); |
| 1562 | }; |
| 1563 | |
| 1564 | |
| 1565 | /** |
| 1566 | * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to |
| 1567 | * the buffer. |
| 1568 | * @param {number} field The field number. |
| 1569 | * @param {?Array<string>} value The array of hashes to write. |
| 1570 | */ |
| 1571 | jspb.BinaryWriter.prototype.writePackedFixedHash64 = function(field, value) { |
| 1572 | if (value == null || !value.length) return; |
| 1573 | this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); |
| 1574 | this.encoder_.writeUnsignedVarint32(value.length * 8); |
| 1575 | for (var i = 0; i < value.length; i++) { |
| 1576 | this.encoder_.writeFixedHash64(value[i]); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1577 | } |
| 1578 | }; |
| 1579 | |
| 1580 | |
| 1581 | /** |
| 1582 | * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to |
| 1583 | * the buffer. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1584 | * @param {number} field The field number. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1585 | * @param {?Array<string>} value The array of hashes to write. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1586 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1587 | jspb.BinaryWriter.prototype.writePackedVarintHash64 = function(field, value) { |
| 1588 | if (value == null || !value.length) return; |
| 1589 | var bookmark = this.beginDelimited_(field); |
| 1590 | for (var i = 0; i < value.length; i++) { |
| 1591 | this.encoder_.writeVarintHash64(value[i]); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1592 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1593 | this.endDelimited_(bookmark); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1594 | }; |