Jakob Buchgraber | 8cc62f7 | 2018-07-09 16:45:04 +0200 | [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 Definition of jspb.Message. |
| 33 | * |
| 34 | * @author mwr@google.com (Mark Rawling) |
| 35 | */ |
| 36 | |
| 37 | goog.provide('jspb.ExtensionFieldBinaryInfo'); |
| 38 | goog.provide('jspb.ExtensionFieldInfo'); |
| 39 | goog.provide('jspb.Message'); |
| 40 | |
| 41 | goog.require('goog.array'); |
| 42 | goog.require('goog.asserts'); |
| 43 | goog.require('goog.crypt.base64'); |
| 44 | goog.require('jspb.Map'); |
| 45 | |
| 46 | // Not needed in compilation units that have no protos with xids. |
| 47 | goog.forwardDeclare('xid.String'); |
| 48 | |
| 49 | |
| 50 | |
| 51 | /** |
| 52 | * Stores information for a single extension field. |
| 53 | * |
| 54 | * For example, an extension field defined like so: |
| 55 | * |
| 56 | * extend BaseMessage { |
| 57 | * optional MyMessage my_field = 123; |
| 58 | * } |
| 59 | * |
| 60 | * will result in an ExtensionFieldInfo object with these properties: |
| 61 | * |
| 62 | * { |
| 63 | * fieldIndex: 123, |
| 64 | * fieldName: {my_field_renamed: 0}, |
| 65 | * ctor: proto.example.MyMessage, |
| 66 | * toObjectFn: proto.example.MyMessage.toObject, |
| 67 | * isRepeated: 0 |
| 68 | * } |
| 69 | * |
| 70 | * We include `toObjectFn` to allow the JSCompiler to perform dead-code removal |
| 71 | * on unused toObject() methods. |
| 72 | * |
| 73 | * If an extension field is primitive, ctor and toObjectFn will be null. |
| 74 | * isRepeated should be 0 or 1. |
| 75 | * |
| 76 | * binary{Reader,Writer}Fn and (if message type) binaryMessageSerializeFn are |
| 77 | * always provided. binaryReaderFn and binaryWriterFn are references to the |
| 78 | * appropriate methods on BinaryReader/BinaryWriter to read/write the value of |
| 79 | * this extension, and binaryMessageSerializeFn is a reference to the message |
| 80 | * class's .serializeBinary method, if available. |
| 81 | * |
| 82 | * @param {number} fieldNumber |
| 83 | * @param {Object} fieldName This has the extension field name as a property. |
| 84 | * @param {?function(new: jspb.Message, Array=)} ctor |
| 85 | * @param {?function((boolean|undefined),!jspb.Message):!Object} toObjectFn |
| 86 | * @param {number} isRepeated |
| 87 | * @constructor |
| 88 | * @struct |
| 89 | * @template T |
| 90 | */ |
| 91 | jspb.ExtensionFieldInfo = function(fieldNumber, fieldName, ctor, toObjectFn, |
| 92 | isRepeated) { |
| 93 | /** @const */ |
| 94 | this.fieldIndex = fieldNumber; |
| 95 | /** @const */ |
| 96 | this.fieldName = fieldName; |
| 97 | /** @const */ |
| 98 | this.ctor = ctor; |
| 99 | /** @const */ |
| 100 | this.toObjectFn = toObjectFn; |
| 101 | /** @const */ |
| 102 | this.isRepeated = isRepeated; |
| 103 | }; |
| 104 | |
| 105 | /** |
| 106 | * Stores binary-related information for a single extension field. |
| 107 | * @param {!jspb.ExtensionFieldInfo<T>} fieldInfo |
| 108 | * @param {function(this:jspb.BinaryReader,number,?)} binaryReaderFn |
| 109 | * @param {function(this:jspb.BinaryWriter,number,?) |
| 110 | * |function(this:jspb.BinaryWriter,number,?,?,?,?,?)} binaryWriterFn |
| 111 | * @param {function(?,?)=} opt_binaryMessageSerializeFn |
| 112 | * @param {function(?,?)=} opt_binaryMessageDeserializeFn |
| 113 | * @param {boolean=} opt_isPacked |
| 114 | * @constructor |
| 115 | * @struct |
| 116 | * @template T |
| 117 | */ |
| 118 | jspb.ExtensionFieldBinaryInfo = function(fieldInfo, binaryReaderFn, binaryWriterFn, |
| 119 | opt_binaryMessageSerializeFn, opt_binaryMessageDeserializeFn, opt_isPacked) { |
| 120 | /** @const */ |
| 121 | this.fieldInfo = fieldInfo; |
| 122 | /** @const */ |
| 123 | this.binaryReaderFn = binaryReaderFn; |
| 124 | /** @const */ |
| 125 | this.binaryWriterFn = binaryWriterFn; |
| 126 | /** @const */ |
| 127 | this.binaryMessageSerializeFn = opt_binaryMessageSerializeFn; |
| 128 | /** @const */ |
| 129 | this.binaryMessageDeserializeFn = opt_binaryMessageDeserializeFn; |
| 130 | /** @const */ |
| 131 | this.isPacked = opt_isPacked; |
| 132 | }; |
| 133 | |
| 134 | /** |
| 135 | * @return {boolean} Does this field represent a sub Message? |
| 136 | */ |
| 137 | jspb.ExtensionFieldInfo.prototype.isMessageType = function() { |
| 138 | return !!this.ctor; |
| 139 | }; |
| 140 | |
| 141 | |
| 142 | /** |
| 143 | * Base class for all JsPb messages. |
| 144 | * |
| 145 | * Several common methods (toObject, serializeBinary, in particular) are not |
| 146 | * defined on the prototype to encourage code patterns that minimize code bloat |
| 147 | * due to otherwise unused code on all protos contained in the project. |
| 148 | * |
| 149 | * If you want to call these methods on a generic message, either |
| 150 | * pass in your instance of method as a parameter: |
| 151 | * someFunction(instanceOfKnownProto, |
| 152 | * KnownProtoClass.prototype.serializeBinary); |
| 153 | * or use a lambda that knows the type: |
| 154 | * someFunction(()=>instanceOfKnownProto.serializeBinary()); |
| 155 | * or, if you don't care about code size, just suppress the |
| 156 | * WARNING - Property serializeBinary never defined on jspb.Message |
| 157 | * and call it the intuitive way. |
| 158 | * |
| 159 | * @constructor |
| 160 | * @struct |
| 161 | */ |
| 162 | jspb.Message = function() { |
| 163 | }; |
| 164 | |
| 165 | |
| 166 | /** |
| 167 | * @define {boolean} Whether to generate toObject methods for objects. Turn |
| 168 | * this off, if you do not want toObject to be ever used in your project. |
| 169 | * When turning off this flag, consider adding a conformance test that bans |
| 170 | * calling toObject. Enabling this will disable the JSCompiler's ability to |
| 171 | * dead code eliminate fields used in protocol buffers that are never used |
| 172 | * in an application. |
| 173 | */ |
| 174 | goog.define('jspb.Message.GENERATE_TO_OBJECT', true); |
| 175 | |
| 176 | |
| 177 | /** |
| 178 | * @define {boolean} Whether to generate fromObject methods for objects. Turn |
| 179 | * this off, if you do not want fromObject to be ever used in your project. |
| 180 | * When turning off this flag, consider adding a conformance test that bans |
| 181 | * calling fromObject. Enabling this might disable the JSCompiler's ability |
| 182 | * to dead code eliminate fields used in protocol buffers that are never |
| 183 | * used in an application. |
| 184 | * NOTE: By default no protos actually have a fromObject method. You need to |
| 185 | * add the jspb.generate_from_object options to the proto definition to |
| 186 | * activate the feature. |
| 187 | * By default this is enabled for test code only. |
| 188 | */ |
| 189 | goog.define('jspb.Message.GENERATE_FROM_OBJECT', !goog.DISALLOW_TEST_ONLY_CODE); |
| 190 | |
| 191 | |
| 192 | /** |
| 193 | * @define {boolean} Whether to generate toString methods for objects. Turn |
| 194 | * this off if you do not use toString in your project and want to trim it |
| 195 | * from the compiled JS. |
| 196 | */ |
| 197 | goog.define('jspb.Message.GENERATE_TO_STRING', true); |
| 198 | |
| 199 | |
| 200 | /** |
| 201 | * @define {boolean} Whether arrays passed to initialize() can be assumed to be |
| 202 | * local (e.g. not from another iframe) and thus safely classified with |
| 203 | * instanceof Array. |
| 204 | */ |
| 205 | goog.define('jspb.Message.ASSUME_LOCAL_ARRAYS', false); |
| 206 | |
| 207 | |
| 208 | /** |
| 209 | * @define {boolean} Turning on this flag does NOT change the behavior of JSPB |
| 210 | * and only affects private internal state. It may, however, break some |
| 211 | * tests that use naive deeply-equals algorithms, because using a proto |
| 212 | * mutates its internal state. |
| 213 | * Projects are advised to turn this flag always on. |
| 214 | */ |
| 215 | goog.define('jspb.Message.MINIMIZE_MEMORY_ALLOCATIONS', COMPILED); |
| 216 | // TODO(b/19419436) Turn this on by default. |
| 217 | |
| 218 | |
| 219 | /** |
| 220 | * Does this JavaScript environment support Uint8Aray typed arrays? |
| 221 | * @type {boolean} |
| 222 | * @private |
| 223 | */ |
| 224 | jspb.Message.SUPPORTS_UINT8ARRAY_ = (typeof Uint8Array == 'function'); |
| 225 | |
| 226 | |
| 227 | /** |
| 228 | * The internal data array. |
| 229 | * @type {!Array} |
| 230 | * @protected |
| 231 | */ |
| 232 | jspb.Message.prototype.array; |
| 233 | |
| 234 | |
| 235 | /** |
| 236 | * Wrappers are the constructed instances of message-type fields. They are built |
| 237 | * on demand from the raw array data. Includes message fields, repeated message |
| 238 | * fields and extension message fields. Indexed by field number. |
| 239 | * @type {Object} |
| 240 | * @private |
| 241 | */ |
| 242 | jspb.Message.prototype.wrappers_; |
| 243 | |
| 244 | |
| 245 | /** |
| 246 | * The object that contains extension fields, if any. This is an object that |
| 247 | * maps from a proto field number to the field's value. |
| 248 | * @type {Object} |
| 249 | * @private |
| 250 | */ |
| 251 | jspb.Message.prototype.extensionObject_; |
| 252 | |
| 253 | |
| 254 | /** |
| 255 | * Non-extension fields with a field number at or above the pivot are |
| 256 | * stored in the extension object (in addition to all extension fields). |
| 257 | * @type {number} |
| 258 | * @private |
| 259 | */ |
| 260 | jspb.Message.prototype.pivot_; |
| 261 | |
| 262 | |
| 263 | /** |
| 264 | * The JsPb message_id of this proto. |
| 265 | * @type {string|undefined} the message id or undefined if this message |
| 266 | * has no id. |
| 267 | * @private |
| 268 | */ |
| 269 | jspb.Message.prototype.messageId_; |
| 270 | |
| 271 | |
| 272 | /** |
| 273 | * Repeated float or double fields which have been converted to include only |
| 274 | * numbers and not strings holding "NaN", "Infinity" and "-Infinity". |
| 275 | * @private {!Object<number,boolean>|undefined} |
| 276 | */ |
| 277 | jspb.Message.prototype.convertedFloatingPointFields_; |
| 278 | |
| 279 | |
| 280 | /** |
| 281 | * The xid of this proto type (The same for all instances of a proto). Provides |
| 282 | * a way to identify a proto by stable obfuscated name. |
| 283 | * @see {xid}. |
| 284 | * Available if {@link jspb.generate_xid} is added as a Message option to |
| 285 | * a protocol buffer. |
| 286 | * @const {!xid.String|undefined} The xid or undefined if message is |
| 287 | * annotated to generate the xid. |
| 288 | */ |
| 289 | jspb.Message.prototype.messageXid; |
| 290 | |
| 291 | |
| 292 | |
| 293 | /** |
| 294 | * Returns the JsPb message_id of this proto. |
| 295 | * @return {string|undefined} the message id or undefined if this message |
| 296 | * has no id. |
| 297 | */ |
| 298 | jspb.Message.prototype.getJsPbMessageId = function() { |
| 299 | return this.messageId_; |
| 300 | }; |
| 301 | |
| 302 | |
| 303 | /** |
| 304 | * An offset applied to lookups into this.array to account for the presence or |
| 305 | * absence of a messageId at position 0. For response messages, this will be 0. |
| 306 | * Otherwise, it will be -1 so that the first array position is not wasted. |
| 307 | * @type {number} |
| 308 | * @private |
| 309 | */ |
| 310 | jspb.Message.prototype.arrayIndexOffset_; |
| 311 | |
| 312 | |
| 313 | /** |
| 314 | * Returns the index into msg.array at which the proto field with tag number |
| 315 | * fieldNumber will be located. |
| 316 | * @param {!jspb.Message} msg Message for which we're calculating an index. |
| 317 | * @param {number} fieldNumber The field number. |
| 318 | * @return {number} The index. |
| 319 | * @private |
| 320 | */ |
| 321 | jspb.Message.getIndex_ = function(msg, fieldNumber) { |
| 322 | return fieldNumber + msg.arrayIndexOffset_; |
| 323 | }; |
| 324 | |
| 325 | |
| 326 | /** |
| 327 | * Initializes a JsPb Message. |
| 328 | * @param {!jspb.Message} msg The JsPb proto to modify. |
| 329 | * @param {Array|undefined} data An initial data array. |
| 330 | * @param {string|number} messageId For response messages, the message id or '' |
| 331 | * if no message id is specified. For non-response messages, 0. |
| 332 | * @param {number} suggestedPivot The field number at which to start putting |
| 333 | * fields into the extension object. This is only used if data does not |
| 334 | * contain an extension object already. -1 if no extension object is |
| 335 | * required for this message type. |
| 336 | * @param {Array<number>} repeatedFields The message's repeated fields. |
| 337 | * @param {Array<!Array<number>>=} opt_oneofFields The fields belonging to |
| 338 | * each of the message's oneof unions. |
| 339 | * @protected |
| 340 | */ |
| 341 | jspb.Message.initialize = function( |
| 342 | msg, data, messageId, suggestedPivot, repeatedFields, opt_oneofFields) { |
| 343 | msg.wrappers_ = jspb.Message.MINIMIZE_MEMORY_ALLOCATIONS ? null : {}; |
| 344 | if (!data) { |
| 345 | data = messageId ? [messageId] : []; |
| 346 | } |
| 347 | msg.messageId_ = messageId ? String(messageId) : undefined; |
| 348 | // If the messageId is 0, this message is not a response message, so we shift |
| 349 | // array indices down by 1 so as not to waste the first position in the array, |
| 350 | // which would otherwise go unused. |
| 351 | msg.arrayIndexOffset_ = messageId === 0 ? -1 : 0; |
| 352 | msg.array = data; |
| 353 | jspb.Message.initPivotAndExtensionObject_(msg, suggestedPivot); |
| 354 | msg.convertedFloatingPointFields_ = {}; |
| 355 | |
| 356 | if (repeatedFields) { |
| 357 | for (var i = 0; i < repeatedFields.length; i++) { |
| 358 | var fieldNumber = repeatedFields[i]; |
| 359 | if (fieldNumber < msg.pivot_) { |
| 360 | var index = jspb.Message.getIndex_(msg, fieldNumber); |
| 361 | msg.array[index] = msg.array[index] || |
| 362 | (jspb.Message.MINIMIZE_MEMORY_ALLOCATIONS ? |
| 363 | jspb.Message.EMPTY_LIST_SENTINEL_ : |
| 364 | []); |
| 365 | } else { |
| 366 | jspb.Message.maybeInitEmptyExtensionObject_(msg); |
| 367 | msg.extensionObject_[fieldNumber] = |
| 368 | msg.extensionObject_[fieldNumber] || |
| 369 | (jspb.Message.MINIMIZE_MEMORY_ALLOCATIONS ? |
| 370 | jspb.Message.EMPTY_LIST_SENTINEL_ : |
| 371 | []); |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | if (opt_oneofFields && opt_oneofFields.length) { |
| 377 | // Compute the oneof case for each union. This ensures only one value is |
| 378 | // set in the union. |
| 379 | goog.array.forEach( |
| 380 | opt_oneofFields, goog.partial(jspb.Message.computeOneofCase, msg)); |
| 381 | } |
| 382 | }; |
| 383 | |
| 384 | |
| 385 | /** |
| 386 | * Used to mark empty repeated fields. Serializes to null when serialized |
| 387 | * to JSON. |
| 388 | * When reading a repeated field readers must check the return value against |
| 389 | * this value and return and replace it with a new empty array if it is |
| 390 | * present. |
| 391 | * @private @const {!Object} |
| 392 | */ |
| 393 | jspb.Message.EMPTY_LIST_SENTINEL_ = goog.DEBUG && Object.freeze ? |
| 394 | Object.freeze([]) : |
| 395 | []; |
| 396 | |
| 397 | |
| 398 | /** |
| 399 | * Returns true if the provided argument is an array. |
| 400 | * @param {*} o The object to classify as array or not. |
| 401 | * @return {boolean} True if the provided object is an array. |
| 402 | * @private |
| 403 | */ |
| 404 | jspb.Message.isArray_ = function(o) { |
| 405 | return jspb.Message.ASSUME_LOCAL_ARRAYS ? o instanceof Array : |
| 406 | goog.isArray(o); |
| 407 | }; |
| 408 | |
| 409 | |
| 410 | /** |
| 411 | * If the array contains an extension object in its last position, then the |
| 412 | * object is kept in place and its position is used as the pivot. If not, |
| 413 | * decides the pivot of the message based on suggestedPivot without |
| 414 | * materializing the extension object. |
| 415 | * |
| 416 | * @param {!jspb.Message} msg The JsPb proto to modify. |
| 417 | * @param {number} suggestedPivot See description for initialize(). |
| 418 | * @private |
| 419 | */ |
| 420 | jspb.Message.initPivotAndExtensionObject_ = function(msg, suggestedPivot) { |
| 421 | if (msg.array.length) { |
| 422 | var foundIndex = msg.array.length - 1; |
| 423 | var obj = msg.array[foundIndex]; |
| 424 | // Normal fields are never objects, so we can be sure that if we find an |
| 425 | // object here, then it's the extension object. However, we must ensure that |
| 426 | // the object is not an array, since arrays are valid field values. |
| 427 | // NOTE(lukestebbing): We avoid looking at .length to avoid a JIT bug |
| 428 | // in Safari on iOS 8. See the description of CL/86511464 for details. |
| 429 | if (obj && typeof obj == 'object' && !jspb.Message.isArray_(obj) && |
| 430 | !(jspb.Message.SUPPORTS_UINT8ARRAY_ && obj instanceof Uint8Array)) { |
| 431 | msg.pivot_ = foundIndex - msg.arrayIndexOffset_; |
| 432 | msg.extensionObject_ = obj; |
| 433 | return; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | if (suggestedPivot > -1) { |
| 438 | msg.pivot_ = suggestedPivot; |
| 439 | // Avoid changing the shape of the proto with an empty extension object by |
| 440 | // deferring the materialization of the extension object until the first |
| 441 | // time a field set into it (may be due to getting a repeated proto field |
| 442 | // from it, in which case a new empty array is set into it at first). |
| 443 | msg.extensionObject_ = null; |
| 444 | } else { |
| 445 | // suggestedPivot is -1, which means that we don't have an extension object |
| 446 | // at all, in which case all fields are stored in the array. |
| 447 | msg.pivot_ = Number.MAX_VALUE; |
| 448 | } |
| 449 | }; |
| 450 | |
| 451 | |
| 452 | /** |
| 453 | * Creates an empty extensionObject_ if non exists. |
| 454 | * @param {!jspb.Message} msg The JsPb proto to modify. |
| 455 | * @private |
| 456 | */ |
| 457 | jspb.Message.maybeInitEmptyExtensionObject_ = function(msg) { |
| 458 | var pivotIndex = jspb.Message.getIndex_(msg, msg.pivot_); |
| 459 | if (!msg.array[pivotIndex]) { |
| 460 | msg.extensionObject_ = msg.array[pivotIndex] = {}; |
| 461 | } |
| 462 | }; |
| 463 | |
| 464 | |
| 465 | /** |
| 466 | * Converts a JsPb repeated message field into an object list. |
| 467 | * @param {!Array<T>} field The repeated message field to be |
| 468 | * converted. |
| 469 | * @param {?function(boolean=): Object| |
| 470 | * function((boolean|undefined),T): Object} toObjectFn The toObject |
| 471 | * function for this field. We need to pass this for effective dead code |
| 472 | * removal. |
| 473 | * @param {boolean=} opt_includeInstance Whether to include the JSPB instance |
| 474 | * for transitional soy proto support: http://goto/soy-param-migration |
| 475 | * @return {!Array<Object>} An array of converted message objects. |
| 476 | * @template T |
| 477 | */ |
| 478 | jspb.Message.toObjectList = function(field, toObjectFn, opt_includeInstance) { |
| 479 | // Not using goog.array.map in the generated code to keep it small. |
| 480 | // And not using it here to avoid a function call. |
| 481 | var result = []; |
| 482 | for (var i = 0; i < field.length; i++) { |
| 483 | result[i] = toObjectFn.call(field[i], opt_includeInstance, |
| 484 | /** @type {!jspb.Message} */ (field[i])); |
| 485 | } |
| 486 | return result; |
| 487 | }; |
| 488 | |
| 489 | |
| 490 | /** |
| 491 | * Adds a proto's extension data to a Soy rendering object. |
| 492 | * @param {!jspb.Message} proto The proto whose extensions to convert. |
| 493 | * @param {!Object} obj The Soy object to add converted extension data to. |
| 494 | * @param {!Object} extensions The proto class' registered extensions. |
| 495 | * @param {function(this:?, jspb.ExtensionFieldInfo) : *} getExtensionFn |
| 496 | * The proto class' getExtension function. Passed for effective dead code |
| 497 | * removal. |
| 498 | * @param {boolean=} opt_includeInstance Whether to include the JSPB instance |
| 499 | * for transitional soy proto support: http://goto/soy-param-migration |
| 500 | */ |
| 501 | jspb.Message.toObjectExtension = function(proto, obj, extensions, |
| 502 | getExtensionFn, opt_includeInstance) { |
| 503 | for (var fieldNumber in extensions) { |
| 504 | var fieldInfo = extensions[fieldNumber]; |
| 505 | var value = getExtensionFn.call(proto, fieldInfo); |
| 506 | if (value != null) { |
| 507 | for (var name in fieldInfo.fieldName) { |
| 508 | if (fieldInfo.fieldName.hasOwnProperty(name)) { |
| 509 | break; // the compiled field name |
| 510 | } |
| 511 | } |
| 512 | if (!fieldInfo.toObjectFn) { |
| 513 | obj[name] = value; |
| 514 | } else { |
| 515 | if (fieldInfo.isRepeated) { |
| 516 | obj[name] = jspb.Message.toObjectList( |
| 517 | /** @type {!Array<jspb.Message>} */ (value), |
| 518 | fieldInfo.toObjectFn, opt_includeInstance); |
| 519 | } else { |
| 520 | obj[name] = fieldInfo.toObjectFn(opt_includeInstance, value); |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | }; |
| 526 | |
| 527 | |
| 528 | /** |
| 529 | * Writes a proto's extension data to a binary-format output stream. |
| 530 | * @param {!jspb.Message} proto The proto whose extensions to convert. |
| 531 | * @param {*} writer The binary-format writer to write to. |
| 532 | * @param {!Object} extensions The proto class' registered extensions. |
| 533 | * @param {function(this:jspb.Message,!jspb.ExtensionFieldInfo) : *} getExtensionFn The proto |
| 534 | * class' getExtension function. Passed for effective dead code removal. |
| 535 | */ |
| 536 | jspb.Message.serializeBinaryExtensions = function(proto, writer, extensions, |
| 537 | getExtensionFn) { |
| 538 | for (var fieldNumber in extensions) { |
| 539 | var binaryFieldInfo = extensions[fieldNumber]; |
| 540 | var fieldInfo = binaryFieldInfo.fieldInfo; |
| 541 | |
| 542 | // The old codegen doesn't add the extra fields to ExtensionFieldInfo, so we |
| 543 | // need to gracefully error-out here rather than produce a null dereference |
| 544 | // below. |
| 545 | if (!binaryFieldInfo.binaryWriterFn) { |
| 546 | throw new Error('Message extension present that was generated ' + |
| 547 | 'without binary serialization support'); |
| 548 | } |
| 549 | var value = getExtensionFn.call(proto, fieldInfo); |
| 550 | if (value != null) { |
| 551 | if (fieldInfo.isMessageType()) { |
| 552 | // If the message type of the extension was generated without binary |
| 553 | // support, there may not be a binary message serializer function, and |
| 554 | // we can't know when we codegen the extending message that the extended |
| 555 | // message may require binary support, so we can *only* catch this error |
| 556 | // here, at runtime (and this decoupled codegen is the whole point of |
| 557 | // extensions!). |
| 558 | if (binaryFieldInfo.binaryMessageSerializeFn) { |
| 559 | binaryFieldInfo.binaryWriterFn.call(writer, fieldInfo.fieldIndex, |
| 560 | value, binaryFieldInfo.binaryMessageSerializeFn); |
| 561 | } else { |
| 562 | throw new Error('Message extension present holding submessage ' + |
| 563 | 'without binary support enabled, and message is ' + |
| 564 | 'being serialized to binary format'); |
| 565 | } |
| 566 | } else { |
| 567 | binaryFieldInfo.binaryWriterFn.call( |
| 568 | writer, fieldInfo.fieldIndex, value); |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | }; |
| 573 | |
| 574 | |
| 575 | /** |
| 576 | * Reads an extension field from the given reader and, if a valid extension, |
| 577 | * sets the extension value. |
| 578 | * @param {!jspb.Message} msg A jspb proto. |
| 579 | * @param {{ |
| 580 | * skipField:function(this:jspb.BinaryReader), |
| 581 | * getFieldNumber:function(this:jspb.BinaryReader):number |
| 582 | * }} reader |
| 583 | * @param {!Object} extensions The extensions object. |
| 584 | * @param {function(this:jspb.Message,!jspb.ExtensionFieldInfo)} getExtensionFn |
| 585 | * @param {function(this:jspb.Message,!jspb.ExtensionFieldInfo, ?)} setExtensionFn |
| 586 | */ |
| 587 | jspb.Message.readBinaryExtension = function(msg, reader, extensions, |
| 588 | getExtensionFn, setExtensionFn) { |
| 589 | var binaryFieldInfo = extensions[reader.getFieldNumber()]; |
| 590 | if (!binaryFieldInfo) { |
| 591 | reader.skipField(); |
| 592 | return; |
| 593 | } |
| 594 | var fieldInfo = binaryFieldInfo.fieldInfo; |
| 595 | if (!binaryFieldInfo.binaryReaderFn) { |
| 596 | throw new Error('Deserializing extension whose generated code does not ' + |
| 597 | 'support binary format'); |
| 598 | } |
| 599 | |
| 600 | var value; |
| 601 | if (fieldInfo.isMessageType()) { |
| 602 | value = new fieldInfo.ctor(); |
| 603 | binaryFieldInfo.binaryReaderFn.call( |
| 604 | reader, value, binaryFieldInfo.binaryMessageDeserializeFn); |
| 605 | } else { |
| 606 | // All other types. |
| 607 | value = binaryFieldInfo.binaryReaderFn.call(reader); |
| 608 | } |
| 609 | |
| 610 | if (fieldInfo.isRepeated && !binaryFieldInfo.isPacked) { |
| 611 | var currentList = getExtensionFn.call(msg, fieldInfo); |
| 612 | if (!currentList) { |
| 613 | setExtensionFn.call(msg, fieldInfo, [value]); |
| 614 | } else { |
| 615 | currentList.push(value); |
| 616 | } |
| 617 | } else { |
| 618 | setExtensionFn.call(msg, fieldInfo, value); |
| 619 | } |
| 620 | }; |
| 621 | |
| 622 | |
| 623 | /** |
| 624 | * Gets the value of a non-extension field. |
| 625 | * @param {!jspb.Message} msg A jspb proto. |
| 626 | * @param {number} fieldNumber The field number. |
| 627 | * @return {string|number|boolean|Uint8Array|Array|null|undefined} |
| 628 | * The field's value. |
| 629 | * @protected |
| 630 | */ |
| 631 | jspb.Message.getField = function(msg, fieldNumber) { |
| 632 | if (fieldNumber < msg.pivot_) { |
| 633 | var index = jspb.Message.getIndex_(msg, fieldNumber); |
| 634 | var val = msg.array[index]; |
| 635 | if (val === jspb.Message.EMPTY_LIST_SENTINEL_) { |
| 636 | return msg.array[index] = []; |
| 637 | } |
| 638 | return val; |
| 639 | } else { |
| 640 | if (!msg.extensionObject_) { |
| 641 | return undefined; |
| 642 | } |
| 643 | var val = msg.extensionObject_[fieldNumber]; |
| 644 | if (val === jspb.Message.EMPTY_LIST_SENTINEL_) { |
| 645 | return msg.extensionObject_[fieldNumber] = []; |
| 646 | } |
| 647 | return val; |
| 648 | } |
| 649 | }; |
| 650 | |
| 651 | |
| 652 | /** |
| 653 | * Gets the value of a non-extension repeated field. |
| 654 | * @param {!jspb.Message} msg A jspb proto. |
| 655 | * @param {number} fieldNumber The field number. |
| 656 | * @return {!Array} |
| 657 | * The field's value. |
| 658 | * @protected |
| 659 | */ |
| 660 | jspb.Message.getRepeatedField = function(msg, fieldNumber) { |
| 661 | if (fieldNumber < msg.pivot_) { |
| 662 | var index = jspb.Message.getIndex_(msg, fieldNumber); |
| 663 | var val = msg.array[index]; |
| 664 | if (val === jspb.Message.EMPTY_LIST_SENTINEL_) { |
| 665 | return msg.array[index] = []; |
| 666 | } |
| 667 | return val; |
| 668 | } |
| 669 | |
| 670 | var val = msg.extensionObject_[fieldNumber]; |
| 671 | if (val === jspb.Message.EMPTY_LIST_SENTINEL_) { |
| 672 | return msg.extensionObject_[fieldNumber] = []; |
| 673 | } |
| 674 | return val; |
| 675 | }; |
| 676 | |
| 677 | |
| 678 | /** |
| 679 | * Gets the value of an optional float or double field. |
| 680 | * @param {!jspb.Message} msg A jspb proto. |
| 681 | * @param {number} fieldNumber The field number. |
| 682 | * @return {?number|undefined} The field's value. |
| 683 | * @protected |
| 684 | */ |
| 685 | jspb.Message.getOptionalFloatingPointField = function(msg, fieldNumber) { |
| 686 | var value = jspb.Message.getField(msg, fieldNumber); |
| 687 | // Converts "NaN", "Infinity" and "-Infinity" to their corresponding numbers. |
| 688 | return value == null ? value : +value; |
| 689 | }; |
| 690 | |
| 691 | |
| 692 | /** |
| 693 | * Gets the value of a repeated float or double field. |
| 694 | * @param {!jspb.Message} msg A jspb proto. |
| 695 | * @param {number} fieldNumber The field number. |
| 696 | * @return {!Array<number>} The field's value. |
| 697 | * @protected |
| 698 | */ |
| 699 | jspb.Message.getRepeatedFloatingPointField = function(msg, fieldNumber) { |
| 700 | var values = jspb.Message.getRepeatedField(msg, fieldNumber); |
| 701 | if (!msg.convertedFloatingPointFields_) { |
| 702 | msg.convertedFloatingPointFields_ = {}; |
| 703 | } |
| 704 | if (!msg.convertedFloatingPointFields_[fieldNumber]) { |
| 705 | for (var i = 0; i < values.length; i++) { |
| 706 | // Converts "NaN", "Infinity" and "-Infinity" to their corresponding |
| 707 | // numbers. |
| 708 | values[i] = +values[i]; |
| 709 | } |
| 710 | msg.convertedFloatingPointFields_[fieldNumber] = true; |
| 711 | } |
| 712 | return /** @type {!Array<number>} */ (values); |
| 713 | }; |
| 714 | |
| 715 | |
| 716 | /** |
| 717 | * Coerce a 'bytes' field to a base 64 string. |
| 718 | * @param {string|Uint8Array|null} value |
| 719 | * @return {?string} The field's coerced value. |
| 720 | */ |
| 721 | jspb.Message.bytesAsB64 = function(value) { |
| 722 | if (value == null || goog.isString(value)) { |
| 723 | return value; |
| 724 | } |
| 725 | if (jspb.Message.SUPPORTS_UINT8ARRAY_ && value instanceof Uint8Array) { |
| 726 | return goog.crypt.base64.encodeByteArray(value); |
| 727 | } |
| 728 | goog.asserts.fail('Cannot coerce to b64 string: ' + goog.typeOf(value)); |
| 729 | return null; |
| 730 | }; |
| 731 | |
| 732 | |
| 733 | /** |
| 734 | * Coerce a 'bytes' field to a Uint8Array byte buffer. |
| 735 | * Note that Uint8Array is not supported on IE versions before 10 nor on Opera |
| 736 | * Mini. @see http://caniuse.com/Uint8Array |
| 737 | * @param {string|Uint8Array|null} value |
| 738 | * @return {?Uint8Array} The field's coerced value. |
| 739 | */ |
| 740 | jspb.Message.bytesAsU8 = function(value) { |
| 741 | if (value == null || value instanceof Uint8Array) { |
| 742 | return value; |
| 743 | } |
| 744 | if (goog.isString(value)) { |
| 745 | return goog.crypt.base64.decodeStringToUint8Array(value); |
| 746 | } |
| 747 | goog.asserts.fail('Cannot coerce to Uint8Array: ' + goog.typeOf(value)); |
| 748 | return null; |
| 749 | }; |
| 750 | |
| 751 | |
| 752 | /** |
| 753 | * Coerce a repeated 'bytes' field to an array of base 64 strings. |
| 754 | * Note: the returned array should be treated as immutable. |
| 755 | * @param {!Array<string>|!Array<!Uint8Array>} value |
| 756 | * @return {!Array<string?>} The field's coerced value. |
| 757 | */ |
| 758 | jspb.Message.bytesListAsB64 = function(value) { |
| 759 | jspb.Message.assertConsistentTypes_(value); |
| 760 | if (!value.length || goog.isString(value[0])) { |
| 761 | return /** @type {!Array<string>} */ (value); |
| 762 | } |
| 763 | return goog.array.map(value, jspb.Message.bytesAsB64); |
| 764 | }; |
| 765 | |
| 766 | |
| 767 | /** |
| 768 | * Coerce a repeated 'bytes' field to an array of Uint8Array byte buffers. |
| 769 | * Note: the returned array should be treated as immutable. |
| 770 | * Note that Uint8Array is not supported on IE versions before 10 nor on Opera |
| 771 | * Mini. @see http://caniuse.com/Uint8Array |
| 772 | * @param {!Array<string>|!Array<!Uint8Array>} value |
| 773 | * @return {!Array<Uint8Array?>} The field's coerced value. |
| 774 | */ |
| 775 | jspb.Message.bytesListAsU8 = function(value) { |
| 776 | jspb.Message.assertConsistentTypes_(value); |
| 777 | if (!value.length || value[0] instanceof Uint8Array) { |
| 778 | return /** @type {!Array<!Uint8Array>} */ (value); |
| 779 | } |
| 780 | return goog.array.map(value, jspb.Message.bytesAsU8); |
| 781 | }; |
| 782 | |
| 783 | |
| 784 | /** |
| 785 | * Asserts that all elements of an array are of the same type. |
| 786 | * @param {Array?} array The array to test. |
| 787 | * @private |
| 788 | */ |
| 789 | jspb.Message.assertConsistentTypes_ = function(array) { |
| 790 | if (goog.DEBUG && array && array.length > 1) { |
| 791 | var expected = goog.typeOf(array[0]); |
| 792 | goog.array.forEach(array, function(e) { |
| 793 | if (goog.typeOf(e) != expected) { |
| 794 | goog.asserts.fail('Inconsistent type in JSPB repeated field array. ' + |
| 795 | 'Got ' + goog.typeOf(e) + ' expected ' + expected); |
| 796 | } |
| 797 | }); |
| 798 | } |
| 799 | }; |
| 800 | |
| 801 | |
| 802 | /** |
| 803 | * Gets the value of a non-extension primitive field, with proto3 (non-nullable |
| 804 | * primitives) semantics. Returns `defaultValue` if the field is not otherwise |
| 805 | * set. |
| 806 | * @template T |
| 807 | * @param {!jspb.Message} msg A jspb proto. |
| 808 | * @param {number} fieldNumber The field number. |
| 809 | * @param {T} defaultValue The default value. |
| 810 | * @return {T} The field's value. |
| 811 | * @protected |
| 812 | */ |
| 813 | jspb.Message.getFieldWithDefault = function(msg, fieldNumber, defaultValue) { |
| 814 | var value = jspb.Message.getField(msg, fieldNumber); |
| 815 | if (value == null) { |
| 816 | return defaultValue; |
| 817 | } else { |
| 818 | return value; |
| 819 | } |
| 820 | }; |
| 821 | |
| 822 | |
| 823 | /** |
| 824 | * Alias for getFieldWithDefault used by older generated code. |
| 825 | * @template T |
| 826 | * @param {!jspb.Message} msg A jspb proto. |
| 827 | * @param {number} fieldNumber The field number. |
| 828 | * @param {T} defaultValue The default value. |
| 829 | * @return {T} The field's value. |
| 830 | * @protected |
| 831 | */ |
| 832 | jspb.Message.getFieldProto3 = jspb.Message.getFieldWithDefault; |
| 833 | |
| 834 | |
| 835 | /** |
| 836 | * Gets the value of a map field, lazily creating the map container if |
| 837 | * necessary. |
| 838 | * |
| 839 | * This should only be called from generated code, because it requires knowledge |
| 840 | * of serialization/parsing callbacks (which are required by the map at |
| 841 | * construction time, and the map may be constructed here). |
| 842 | * |
| 843 | * @template K, V |
| 844 | * @param {!jspb.Message} msg |
| 845 | * @param {number} fieldNumber |
| 846 | * @param {boolean|undefined} noLazyCreate |
| 847 | * @param {?=} opt_valueCtor |
| 848 | * @return {!jspb.Map<K, V>|undefined} |
| 849 | * @protected |
| 850 | */ |
| 851 | jspb.Message.getMapField = function(msg, fieldNumber, noLazyCreate, |
| 852 | opt_valueCtor) { |
| 853 | if (!msg.wrappers_) { |
| 854 | msg.wrappers_ = {}; |
| 855 | } |
| 856 | // If we already have a map in the map wrappers, return that. |
| 857 | if (fieldNumber in msg.wrappers_) { |
| 858 | return msg.wrappers_[fieldNumber]; |
| 859 | } else if (noLazyCreate) { |
| 860 | return undefined; |
| 861 | } else { |
| 862 | // Wrap the underlying elements array with a Map. |
| 863 | var arr = jspb.Message.getField(msg, fieldNumber); |
| 864 | if (!arr) { |
| 865 | arr = []; |
| 866 | jspb.Message.setField(msg, fieldNumber, arr); |
| 867 | } |
| 868 | return msg.wrappers_[fieldNumber] = |
| 869 | new jspb.Map( |
| 870 | /** @type {!Array<!Array<!Object>>} */ (arr), opt_valueCtor); |
| 871 | } |
| 872 | }; |
| 873 | |
| 874 | |
| 875 | /** |
| 876 | * Sets the value of a non-extension field. |
| 877 | * @param {!jspb.Message} msg A jspb proto. |
| 878 | * @param {number} fieldNumber The field number. |
| 879 | * @param {string|number|boolean|Uint8Array|Array|undefined} value New value |
| 880 | * @protected |
| 881 | */ |
| 882 | jspb.Message.setField = function(msg, fieldNumber, value) { |
| 883 | if (fieldNumber < msg.pivot_) { |
| 884 | msg.array[jspb.Message.getIndex_(msg, fieldNumber)] = value; |
| 885 | } else { |
| 886 | jspb.Message.maybeInitEmptyExtensionObject_(msg); |
| 887 | msg.extensionObject_[fieldNumber] = value; |
| 888 | } |
| 889 | }; |
| 890 | |
| 891 | |
| 892 | /** |
| 893 | * Adds a value to a repeated, primitive field. |
| 894 | * @param {!jspb.Message} msg A jspb proto. |
| 895 | * @param {number} fieldNumber The field number. |
| 896 | * @param {string|number|boolean|!Uint8Array} value New value |
| 897 | * @param {number=} opt_index Index where to put new value. |
| 898 | * @protected |
| 899 | */ |
| 900 | jspb.Message.addToRepeatedField = function(msg, fieldNumber, value, opt_index) { |
| 901 | var arr = jspb.Message.getRepeatedField(msg, fieldNumber); |
| 902 | if (opt_index != undefined) { |
| 903 | arr.splice(opt_index, 0, value); |
| 904 | } else { |
| 905 | arr.push(value); |
| 906 | } |
| 907 | }; |
| 908 | |
| 909 | |
| 910 | /** |
| 911 | * Sets the value of a field in a oneof union and clears all other fields in |
| 912 | * the union. |
| 913 | * @param {!jspb.Message} msg A jspb proto. |
| 914 | * @param {number} fieldNumber The field number. |
| 915 | * @param {!Array<number>} oneof The fields belonging to the union. |
| 916 | * @param {string|number|boolean|Uint8Array|Array|undefined} value New value |
| 917 | * @protected |
| 918 | */ |
| 919 | jspb.Message.setOneofField = function(msg, fieldNumber, oneof, value) { |
| 920 | var currentCase = jspb.Message.computeOneofCase(msg, oneof); |
| 921 | if (currentCase && currentCase !== fieldNumber && value !== undefined) { |
| 922 | if (msg.wrappers_ && currentCase in msg.wrappers_) { |
| 923 | msg.wrappers_[currentCase] = undefined; |
| 924 | } |
| 925 | jspb.Message.setField(msg, currentCase, undefined); |
| 926 | } |
| 927 | jspb.Message.setField(msg, fieldNumber, value); |
| 928 | }; |
| 929 | |
| 930 | |
| 931 | /** |
| 932 | * Computes the selection in a oneof group for the given message, ensuring |
| 933 | * only one field is set in the process. |
| 934 | * |
| 935 | * According to the protobuf language guide ( |
| 936 | * https://developers.google.com/protocol-buffers/docs/proto#oneof), "if the |
| 937 | * parser encounters multiple members of the same oneof on the wire, only the |
| 938 | * last member seen is used in the parsed message." Since JSPB serializes |
| 939 | * messages to a JSON array, the "last member seen" will always be the field |
| 940 | * with the greatest field number (directly corresponding to the greatest |
| 941 | * array index). |
| 942 | * |
| 943 | * @param {!jspb.Message} msg A jspb proto. |
| 944 | * @param {!Array<number>} oneof The field numbers belonging to the union. |
| 945 | * @return {number} The field number currently set in the union, or 0 if none. |
| 946 | * @protected |
| 947 | */ |
| 948 | jspb.Message.computeOneofCase = function(msg, oneof) { |
| 949 | var oneofField; |
| 950 | var oneofValue; |
| 951 | |
| 952 | goog.array.forEach(oneof, function(fieldNumber) { |
| 953 | var value = jspb.Message.getField(msg, fieldNumber); |
| 954 | if (goog.isDefAndNotNull(value)) { |
| 955 | oneofField = fieldNumber; |
| 956 | oneofValue = value; |
| 957 | jspb.Message.setField(msg, fieldNumber, undefined); |
| 958 | } |
| 959 | }); |
| 960 | |
| 961 | if (oneofField) { |
| 962 | // NB: We know the value is unique, so we can call jspb.Message.setField |
| 963 | // directly instead of jpsb.Message.setOneofField. Also, setOneofField |
| 964 | // calls this function. |
| 965 | jspb.Message.setField(msg, oneofField, oneofValue); |
| 966 | return oneofField; |
| 967 | } |
| 968 | |
| 969 | return 0; |
| 970 | }; |
| 971 | |
| 972 | |
| 973 | /** |
| 974 | * Gets and wraps a proto field on access. |
| 975 | * @param {!jspb.Message} msg A jspb proto. |
| 976 | * @param {function(new:jspb.Message, Array)} ctor Constructor for the field. |
| 977 | * @param {number} fieldNumber The field number. |
| 978 | * @param {number=} opt_required True (1) if this is a required field. |
| 979 | * @return {jspb.Message} The field as a jspb proto. |
| 980 | * @protected |
| 981 | */ |
| 982 | jspb.Message.getWrapperField = function(msg, ctor, fieldNumber, opt_required) { |
| 983 | // TODO(mwr): Consider copying data and/or arrays. |
| 984 | if (!msg.wrappers_) { |
| 985 | msg.wrappers_ = {}; |
| 986 | } |
| 987 | if (!msg.wrappers_[fieldNumber]) { |
| 988 | var data = /** @type {Array} */ (jspb.Message.getField(msg, fieldNumber)); |
| 989 | if (opt_required || data) { |
| 990 | // TODO(mwr): Remove existence test for always valid default protos. |
| 991 | msg.wrappers_[fieldNumber] = new ctor(data); |
| 992 | } |
| 993 | } |
| 994 | return /** @type {jspb.Message} */ (msg.wrappers_[fieldNumber]); |
| 995 | }; |
| 996 | |
| 997 | |
| 998 | /** |
| 999 | * Gets and wraps a repeated proto field on access. |
| 1000 | * @param {!jspb.Message} msg A jspb proto. |
| 1001 | * @param {function(new:jspb.Message, Array)} ctor Constructor for the field. |
| 1002 | * @param {number} fieldNumber The field number. |
| 1003 | * @return {Array<!jspb.Message>} The repeated field as an array of protos. |
| 1004 | * @protected |
| 1005 | */ |
| 1006 | jspb.Message.getRepeatedWrapperField = function(msg, ctor, fieldNumber) { |
| 1007 | jspb.Message.wrapRepeatedField_(msg, ctor, fieldNumber); |
| 1008 | var val = msg.wrappers_[fieldNumber]; |
| 1009 | if (val == jspb.Message.EMPTY_LIST_SENTINEL_) { |
| 1010 | val = msg.wrappers_[fieldNumber] = []; |
| 1011 | } |
| 1012 | return /** @type {!Array<!jspb.Message>} */ (val); |
| 1013 | }; |
| 1014 | |
| 1015 | |
| 1016 | /** |
| 1017 | * Wraps underlying array into proto message representation if it wasn't done |
| 1018 | * before. |
| 1019 | * @param {!jspb.Message} msg A jspb proto. |
| 1020 | * @param {function(new:jspb.Message, ?Array)} ctor Constructor for the field. |
| 1021 | * @param {number} fieldNumber The field number. |
| 1022 | * @private |
| 1023 | */ |
| 1024 | jspb.Message.wrapRepeatedField_ = function(msg, ctor, fieldNumber) { |
| 1025 | if (!msg.wrappers_) { |
| 1026 | msg.wrappers_ = {}; |
| 1027 | } |
| 1028 | if (!msg.wrappers_[fieldNumber]) { |
| 1029 | var data = jspb.Message.getRepeatedField(msg, fieldNumber); |
| 1030 | for (var wrappers = [], i = 0; i < data.length; i++) { |
| 1031 | wrappers[i] = new ctor(data[i]); |
| 1032 | } |
| 1033 | msg.wrappers_[fieldNumber] = wrappers; |
| 1034 | } |
| 1035 | }; |
| 1036 | |
| 1037 | |
| 1038 | /** |
| 1039 | * Sets a proto field and syncs it to the backing array. |
| 1040 | * @param {!jspb.Message} msg A jspb proto. |
| 1041 | * @param {number} fieldNumber The field number. |
| 1042 | * @param {?jspb.Message|?jspb.Map|undefined} value A new value for this proto |
| 1043 | * field. |
| 1044 | * @protected |
| 1045 | */ |
| 1046 | jspb.Message.setWrapperField = function(msg, fieldNumber, value) { |
| 1047 | if (!msg.wrappers_) { |
| 1048 | msg.wrappers_ = {}; |
| 1049 | } |
| 1050 | var data = value ? value.toArray() : value; |
| 1051 | msg.wrappers_[fieldNumber] = value; |
| 1052 | jspb.Message.setField(msg, fieldNumber, data); |
| 1053 | }; |
| 1054 | |
| 1055 | |
| 1056 | /** |
| 1057 | * Sets a proto field in a oneof union and syncs it to the backing array. |
| 1058 | * @param {!jspb.Message} msg A jspb proto. |
| 1059 | * @param {number} fieldNumber The field number. |
| 1060 | * @param {!Array<number>} oneof The fields belonging to the union. |
| 1061 | * @param {jspb.Message|undefined} value A new value for this proto field. |
| 1062 | * @protected |
| 1063 | */ |
| 1064 | jspb.Message.setOneofWrapperField = function(msg, fieldNumber, oneof, value) { |
| 1065 | if (!msg.wrappers_) { |
| 1066 | msg.wrappers_ = {}; |
| 1067 | } |
| 1068 | var data = value ? value.toArray() : value; |
| 1069 | msg.wrappers_[fieldNumber] = value; |
| 1070 | jspb.Message.setOneofField(msg, fieldNumber, oneof, data); |
| 1071 | }; |
| 1072 | |
| 1073 | |
| 1074 | /** |
| 1075 | * Sets a repeated proto field and syncs it to the backing array. |
| 1076 | * @param {!jspb.Message} msg A jspb proto. |
| 1077 | * @param {number} fieldNumber The field number. |
| 1078 | * @param {Array<!jspb.Message>|undefined} value An array of protos. |
| 1079 | * @protected |
| 1080 | */ |
| 1081 | jspb.Message.setRepeatedWrapperField = function(msg, fieldNumber, value) { |
| 1082 | if (!msg.wrappers_) { |
| 1083 | msg.wrappers_ = {}; |
| 1084 | } |
| 1085 | value = value || []; |
| 1086 | for (var data = [], i = 0; i < value.length; i++) { |
| 1087 | data[i] = value[i].toArray(); |
| 1088 | } |
| 1089 | msg.wrappers_[fieldNumber] = value; |
| 1090 | jspb.Message.setField(msg, fieldNumber, data); |
| 1091 | }; |
| 1092 | |
| 1093 | |
| 1094 | /** |
| 1095 | * Add a message to a repeated proto field. |
| 1096 | * @param {!jspb.Message} msg A jspb proto. |
| 1097 | * @param {number} fieldNumber The field number. |
| 1098 | * @param {T_CHILD|undefined} value Proto that will be added to the |
| 1099 | * repeated field. |
| 1100 | * @param {function(new:T_CHILD, ?Array=)} ctor The constructor of the |
| 1101 | * message type. |
| 1102 | * @param {number|undefined} index Index at which to insert the value. |
| 1103 | * @return {T_CHILD_NOT_UNDEFINED} proto that was inserted to the repeated field |
| 1104 | * @template MessageType |
| 1105 | * Use go/closure-ttl to declare a non-undefined version of T_CHILD. Replace the |
| 1106 | * undefined in blah|undefined with none. This is necessary because the compiler |
| 1107 | * will infer T_CHILD to be |undefined. |
| 1108 | * @template T_CHILD |
| 1109 | * @template T_CHILD_NOT_UNDEFINED := |
| 1110 | * cond(isUnknown(T_CHILD), unknown(), |
| 1111 | * mapunion(T_CHILD, (X) => |
| 1112 | * cond(eq(X, 'undefined'), none(), X))) |
| 1113 | * =: |
| 1114 | * @protected |
| 1115 | */ |
| 1116 | jspb.Message.addToRepeatedWrapperField = function( |
| 1117 | msg, fieldNumber, value, ctor, index) { |
| 1118 | jspb.Message.wrapRepeatedField_(msg, ctor, fieldNumber); |
| 1119 | var wrapperArray = msg.wrappers_[fieldNumber]; |
| 1120 | if (!wrapperArray) { |
| 1121 | wrapperArray = msg.wrappers_[fieldNumber] = []; |
| 1122 | } |
| 1123 | var insertedValue = value ? value : new ctor(); |
| 1124 | var array = jspb.Message.getRepeatedField(msg, fieldNumber); |
| 1125 | if (index != undefined) { |
| 1126 | wrapperArray.splice(index, 0, insertedValue); |
| 1127 | array.splice(index, 0, insertedValue.toArray()); |
| 1128 | } else { |
| 1129 | wrapperArray.push(insertedValue); |
| 1130 | array.push(insertedValue.toArray()); |
| 1131 | } |
| 1132 | return insertedValue; |
| 1133 | }; |
| 1134 | |
| 1135 | |
| 1136 | /** |
| 1137 | * Converts a JsPb repeated message field into a map. The map will contain |
| 1138 | * protos unless an optional toObject function is given, in which case it will |
| 1139 | * contain objects suitable for Soy rendering. |
| 1140 | * @param {!Array<T>} field The repeated message field to be |
| 1141 | * converted. |
| 1142 | * @param {function() : string?} mapKeyGetterFn The function to get the key of |
| 1143 | * the map. |
| 1144 | * @param {?function(boolean=): Object| |
| 1145 | * function((boolean|undefined),T): Object} opt_toObjectFn The |
| 1146 | * toObject function for this field. We need to pass this for effective |
| 1147 | * dead code removal. |
| 1148 | * @param {boolean=} opt_includeInstance Whether to include the JSPB instance |
| 1149 | * for transitional soy proto support: http://goto/soy-param-migration |
| 1150 | * @return {!Object.<string, Object>} A map of proto or Soy objects. |
| 1151 | * @template T |
| 1152 | */ |
| 1153 | jspb.Message.toMap = function( |
| 1154 | field, mapKeyGetterFn, opt_toObjectFn, opt_includeInstance) { |
| 1155 | var result = {}; |
| 1156 | for (var i = 0; i < field.length; i++) { |
| 1157 | result[mapKeyGetterFn.call(field[i])] = opt_toObjectFn ? |
| 1158 | opt_toObjectFn.call(field[i], opt_includeInstance, |
| 1159 | /** @type {!jspb.Message} */ (field[i])) : field[i]; |
| 1160 | } |
| 1161 | return result; |
| 1162 | }; |
| 1163 | |
| 1164 | |
| 1165 | /** |
| 1166 | * Syncs all map fields' contents back to their underlying arrays. |
| 1167 | * @private |
| 1168 | */ |
| 1169 | jspb.Message.prototype.syncMapFields_ = function() { |
| 1170 | // This iterates over submessage, map, and repeated fields, which is intended. |
| 1171 | // Submessages can contain maps which also need to be synced. |
| 1172 | // |
| 1173 | // There is a lot of opportunity for optimization here. For example we could |
| 1174 | // statically determine that some messages have no submessages with maps and |
| 1175 | // optimize this method away for those just by generating one extra static |
| 1176 | // boolean per message type. |
| 1177 | if (this.wrappers_) { |
| 1178 | for (var fieldNumber in this.wrappers_) { |
| 1179 | var val = this.wrappers_[fieldNumber]; |
| 1180 | if (goog.isArray(val)) { |
| 1181 | for (var i = 0; i < val.length; i++) { |
| 1182 | if (val[i]) { |
| 1183 | val[i].toArray(); |
| 1184 | } |
| 1185 | } |
| 1186 | } else { |
| 1187 | // Works for submessages and maps. |
| 1188 | if (val) { |
| 1189 | val.toArray(); |
| 1190 | } |
| 1191 | } |
| 1192 | } |
| 1193 | } |
| 1194 | }; |
| 1195 | |
| 1196 | |
| 1197 | /** |
| 1198 | * Returns the internal array of this proto. |
| 1199 | * <p>Note: If you use this array to construct a second proto, the content |
| 1200 | * would then be partially shared between the two protos. |
| 1201 | * @return {!Array} The proto represented as an array. |
| 1202 | */ |
| 1203 | jspb.Message.prototype.toArray = function() { |
| 1204 | this.syncMapFields_(); |
| 1205 | return this.array; |
| 1206 | }; |
| 1207 | |
| 1208 | |
| 1209 | |
| 1210 | if (jspb.Message.GENERATE_TO_STRING) { |
| 1211 | |
| 1212 | /** |
| 1213 | * Creates a string representation of the internal data array of this proto. |
| 1214 | * <p>NOTE: This string is *not* suitable for use in server requests. |
| 1215 | * @return {string} A string representation of this proto. |
| 1216 | * @override |
| 1217 | */ |
| 1218 | jspb.Message.prototype.toString = function() { |
| 1219 | this.syncMapFields_(); |
| 1220 | return this.array.toString(); |
| 1221 | }; |
| 1222 | |
| 1223 | } |
| 1224 | |
| 1225 | /** |
| 1226 | * Gets the value of the extension field from the extended object. |
| 1227 | * @param {jspb.ExtensionFieldInfo.<T>} fieldInfo Specifies the field to get. |
| 1228 | * @return {T} The value of the field. |
| 1229 | * @template T |
| 1230 | */ |
| 1231 | jspb.Message.prototype.getExtension = function(fieldInfo) { |
| 1232 | if (!this.extensionObject_) { |
| 1233 | return undefined; |
| 1234 | } |
| 1235 | if (!this.wrappers_) { |
| 1236 | this.wrappers_ = {}; |
| 1237 | } |
| 1238 | var fieldNumber = fieldInfo.fieldIndex; |
| 1239 | if (fieldInfo.isRepeated) { |
| 1240 | if (fieldInfo.isMessageType()) { |
| 1241 | if (!this.wrappers_[fieldNumber]) { |
| 1242 | this.wrappers_[fieldNumber] = |
| 1243 | goog.array.map(this.extensionObject_[fieldNumber] || [], |
| 1244 | function(arr) { |
| 1245 | return new fieldInfo.ctor(arr); |
| 1246 | }); |
| 1247 | } |
| 1248 | return this.wrappers_[fieldNumber]; |
| 1249 | } else { |
| 1250 | return this.extensionObject_[fieldNumber]; |
| 1251 | } |
| 1252 | } else { |
| 1253 | if (fieldInfo.isMessageType()) { |
| 1254 | if (!this.wrappers_[fieldNumber] && this.extensionObject_[fieldNumber]) { |
| 1255 | this.wrappers_[fieldNumber] = new fieldInfo.ctor( |
| 1256 | /** @type {Array|undefined} */ ( |
| 1257 | this.extensionObject_[fieldNumber])); |
| 1258 | } |
| 1259 | return this.wrappers_[fieldNumber]; |
| 1260 | } else { |
| 1261 | return this.extensionObject_[fieldNumber]; |
| 1262 | } |
| 1263 | } |
| 1264 | }; |
| 1265 | |
| 1266 | |
| 1267 | /** |
| 1268 | * Sets the value of the extension field in the extended object. |
| 1269 | * @param {jspb.ExtensionFieldInfo} fieldInfo Specifies the field to set. |
| 1270 | * @param {jspb.Message|string|Uint8Array|number|boolean|Array?} value The value |
| 1271 | * to set. |
| 1272 | * @return {THIS} For chaining |
| 1273 | * @this {THIS} |
| 1274 | * @template THIS |
| 1275 | */ |
| 1276 | jspb.Message.prototype.setExtension = function(fieldInfo, value) { |
| 1277 | // Cast self, since the inferred THIS is unknown inside the function body. |
| 1278 | // https://github.com/google/closure-compiler/issues/1411#issuecomment-232442220 |
| 1279 | var self = /** @type {!jspb.Message} */ (this); |
| 1280 | if (!self.wrappers_) { |
| 1281 | self.wrappers_ = {}; |
| 1282 | } |
| 1283 | jspb.Message.maybeInitEmptyExtensionObject_(self); |
| 1284 | var fieldNumber = fieldInfo.fieldIndex; |
| 1285 | if (fieldInfo.isRepeated) { |
| 1286 | value = value || []; |
| 1287 | if (fieldInfo.isMessageType()) { |
| 1288 | self.wrappers_[fieldNumber] = value; |
| 1289 | self.extensionObject_[fieldNumber] = goog.array.map( |
| 1290 | /** @type {Array<jspb.Message>} */ (value), function(msg) { |
| 1291 | return msg.toArray(); |
| 1292 | }); |
| 1293 | } else { |
| 1294 | self.extensionObject_[fieldNumber] = value; |
| 1295 | } |
| 1296 | } else { |
| 1297 | if (fieldInfo.isMessageType()) { |
| 1298 | self.wrappers_[fieldNumber] = value; |
| 1299 | self.extensionObject_[fieldNumber] = value ? value.toArray() : value; |
| 1300 | } else { |
| 1301 | self.extensionObject_[fieldNumber] = value; |
| 1302 | } |
| 1303 | } |
| 1304 | return self; |
| 1305 | }; |
| 1306 | |
| 1307 | |
| 1308 | /** |
| 1309 | * Creates a difference object between two messages. |
| 1310 | * |
| 1311 | * The result will contain the top-level fields of m2 that differ from those of |
| 1312 | * m1 at any level of nesting. No data is cloned, the result object will |
| 1313 | * share its top-level elements with m2 (but not with m1). |
| 1314 | * |
| 1315 | * Note that repeated fields should not have null/undefined elements, but if |
| 1316 | * they do, this operation will treat repeated fields of different length as |
| 1317 | * the same if the only difference between them is due to trailing |
| 1318 | * null/undefined values. |
| 1319 | * |
| 1320 | * @param {!jspb.Message} m1 The first message object. |
| 1321 | * @param {!jspb.Message} m2 The second message object. |
| 1322 | * @return {!jspb.Message} The difference returned as a proto message. |
| 1323 | * Note that the returned message may be missing required fields. This is |
| 1324 | * currently tolerated in Js, but would cause an error if you tried to |
| 1325 | * send such a proto to the server. You can access the raw difference |
| 1326 | * array with result.toArray(). |
| 1327 | * @throws {Error} If the messages are responses with different types. |
| 1328 | */ |
| 1329 | jspb.Message.difference = function(m1, m2) { |
| 1330 | if (!(m1 instanceof m2.constructor)) { |
| 1331 | throw new Error('Messages have different types.'); |
| 1332 | } |
| 1333 | var arr1 = m1.toArray(); |
| 1334 | var arr2 = m2.toArray(); |
| 1335 | var res = []; |
| 1336 | var start = 0; |
| 1337 | var length = arr1.length > arr2.length ? arr1.length : arr2.length; |
| 1338 | if (m1.getJsPbMessageId()) { |
| 1339 | res[0] = m1.getJsPbMessageId(); |
| 1340 | start = 1; |
| 1341 | } |
| 1342 | for (var i = start; i < length; i++) { |
| 1343 | if (!jspb.Message.compareFields(arr1[i], arr2[i])) { |
| 1344 | res[i] = arr2[i]; |
| 1345 | } |
| 1346 | } |
| 1347 | return new m1.constructor(res); |
| 1348 | }; |
| 1349 | |
| 1350 | |
| 1351 | /** |
| 1352 | * Tests whether two messages are equal. |
| 1353 | * @param {jspb.Message|undefined} m1 The first message object. |
| 1354 | * @param {jspb.Message|undefined} m2 The second message object. |
| 1355 | * @return {boolean} true if both messages are null/undefined, or if both are |
| 1356 | * of the same type and have the same field values. |
| 1357 | */ |
| 1358 | jspb.Message.equals = function(m1, m2) { |
| 1359 | return m1 == m2 || (!!(m1 && m2) && (m1 instanceof m2.constructor) && |
| 1360 | jspb.Message.compareFields(m1.toArray(), m2.toArray())); |
| 1361 | }; |
| 1362 | |
| 1363 | |
| 1364 | /** |
| 1365 | * Compares two message extension fields recursively. |
| 1366 | * @param {!Object} extension1 The first field. |
| 1367 | * @param {!Object} extension2 The second field. |
| 1368 | * @return {boolean} true if the extensions are null/undefined, or otherwise |
| 1369 | * equal. |
| 1370 | */ |
| 1371 | jspb.Message.compareExtensions = function(extension1, extension2) { |
| 1372 | extension1 = extension1 || {}; |
| 1373 | extension2 = extension2 || {}; |
| 1374 | |
| 1375 | var keys = {}; |
| 1376 | for (var name in extension1) { |
| 1377 | keys[name] = 0; |
| 1378 | } |
| 1379 | for (var name in extension2) { |
| 1380 | keys[name] = 0; |
| 1381 | } |
| 1382 | for (name in keys) { |
| 1383 | if (!jspb.Message.compareFields(extension1[name], extension2[name])) { |
| 1384 | return false; |
| 1385 | } |
| 1386 | } |
| 1387 | return true; |
| 1388 | }; |
| 1389 | |
| 1390 | |
| 1391 | /** |
| 1392 | * Compares two message fields recursively. |
| 1393 | * @param {*} field1 The first field. |
| 1394 | * @param {*} field2 The second field. |
| 1395 | * @return {boolean} true if the fields are null/undefined, or otherwise equal. |
| 1396 | */ |
| 1397 | jspb.Message.compareFields = function(field1, field2) { |
| 1398 | // If the fields are trivially equal, they're equal. |
| 1399 | if (field1 == field2) return true; |
| 1400 | |
| 1401 | // If the fields aren't trivially equal and one of them isn't an object, |
| 1402 | // they can't possibly be equal. |
| 1403 | if (!goog.isObject(field1) || !goog.isObject(field2)) { |
| 1404 | return false; |
| 1405 | } |
| 1406 | |
| 1407 | // We have two objects. If they're different types, they're not equal. |
| 1408 | field1 = /** @type {!Object} */(field1); |
| 1409 | field2 = /** @type {!Object} */(field2); |
| 1410 | if (field1.constructor != field2.constructor) return false; |
| 1411 | |
| 1412 | // If both are Uint8Arrays, compare them element-by-element. |
| 1413 | if (jspb.Message.SUPPORTS_UINT8ARRAY_ && field1.constructor === Uint8Array) { |
| 1414 | var bytes1 = /** @type {!Uint8Array} */(field1); |
| 1415 | var bytes2 = /** @type {!Uint8Array} */(field2); |
| 1416 | if (bytes1.length != bytes2.length) return false; |
| 1417 | for (var i = 0; i < bytes1.length; i++) { |
| 1418 | if (bytes1[i] != bytes2[i]) return false; |
| 1419 | } |
| 1420 | return true; |
| 1421 | } |
| 1422 | |
| 1423 | // If they're both Arrays, compare them element by element except for the |
| 1424 | // optional extension objects at the end, which we compare separately. |
| 1425 | if (field1.constructor === Array) { |
| 1426 | var extension1 = undefined; |
| 1427 | var extension2 = undefined; |
| 1428 | |
| 1429 | var length = Math.max(field1.length, field2.length); |
| 1430 | for (var i = 0; i < length; i++) { |
| 1431 | var val1 = field1[i]; |
| 1432 | var val2 = field2[i]; |
| 1433 | |
| 1434 | if (val1 && (val1.constructor == Object)) { |
| 1435 | goog.asserts.assert(extension1 === undefined); |
| 1436 | goog.asserts.assert(i === field1.length - 1); |
| 1437 | extension1 = val1; |
| 1438 | val1 = undefined; |
| 1439 | } |
| 1440 | |
| 1441 | if (val2 && (val2.constructor == Object)) { |
| 1442 | goog.asserts.assert(extension2 === undefined); |
| 1443 | goog.asserts.assert(i === field2.length - 1); |
| 1444 | extension2 = val2; |
| 1445 | val2 = undefined; |
| 1446 | } |
| 1447 | |
| 1448 | if (!jspb.Message.compareFields(val1, val2)) { |
| 1449 | return false; |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | if (extension1 || extension2) { |
| 1454 | extension1 = extension1 || {}; |
| 1455 | extension2 = extension2 || {}; |
| 1456 | return jspb.Message.compareExtensions(extension1, extension2); |
| 1457 | } |
| 1458 | |
| 1459 | return true; |
| 1460 | } |
| 1461 | |
| 1462 | // If they're both plain Objects (i.e. extensions), compare them as |
| 1463 | // extensions. |
| 1464 | if (field1.constructor === Object) { |
| 1465 | return jspb.Message.compareExtensions(field1, field2); |
| 1466 | } |
| 1467 | |
| 1468 | throw new Error('Invalid type in JSPB array'); |
| 1469 | }; |
| 1470 | |
| 1471 | |
| 1472 | /** |
| 1473 | * Templated, type-safe cloneMessage definition. |
| 1474 | * @return {THIS} |
| 1475 | * @this {THIS} |
| 1476 | * @template THIS |
| 1477 | */ |
| 1478 | jspb.Message.prototype.cloneMessage = function() { |
| 1479 | return jspb.Message.cloneMessage(/** @type {!jspb.Message} */ (this)); |
| 1480 | }; |
| 1481 | |
| 1482 | /** |
| 1483 | * Alias clone to cloneMessage. goog.object.unsafeClone uses clone to |
| 1484 | * efficiently copy objects. Without this alias, copying jspb messages comes |
| 1485 | * with a large performance penalty. |
| 1486 | * @return {THIS} |
| 1487 | * @this {THIS} |
| 1488 | * @template THIS |
| 1489 | */ |
| 1490 | jspb.Message.prototype.clone = function() { |
| 1491 | return jspb.Message.cloneMessage(/** @type {!jspb.Message} */ (this)); |
| 1492 | }; |
| 1493 | |
| 1494 | /** |
| 1495 | * Static clone function. NOTE: A type-safe method called "cloneMessage" |
| 1496 | * exists |
| 1497 | * on each generated JsPb class. Do not call this function directly. |
| 1498 | * @param {!jspb.Message} msg A message to clone. |
| 1499 | * @return {!jspb.Message} A deep clone of the given message. |
| 1500 | */ |
| 1501 | jspb.Message.clone = function(msg) { |
| 1502 | // Although we could include the wrappers, we leave them out here. |
| 1503 | return jspb.Message.cloneMessage(msg); |
| 1504 | }; |
| 1505 | |
| 1506 | |
| 1507 | /** |
| 1508 | * @param {!jspb.Message} msg A message to clone. |
| 1509 | * @return {!jspb.Message} A deep clone of the given message. |
| 1510 | * @protected |
| 1511 | */ |
| 1512 | jspb.Message.cloneMessage = function(msg) { |
| 1513 | // Although we could include the wrappers, we leave them out here. |
| 1514 | return new msg.constructor(jspb.Message.clone_(msg.toArray())); |
| 1515 | }; |
| 1516 | |
| 1517 | |
| 1518 | /** |
| 1519 | * Takes 2 messages of the same type and copies the contents of the first |
| 1520 | * message into the second. After this the 2 messages will equals in terms of |
| 1521 | * value semantics but share no state. All data in the destination message will |
| 1522 | * be overridden. |
| 1523 | * |
| 1524 | * @param {MESSAGE} fromMessage Message that will be copied into toMessage. |
| 1525 | * @param {MESSAGE} toMessage Message which will receive a copy of fromMessage |
| 1526 | * as its contents. |
| 1527 | * @template MESSAGE |
| 1528 | */ |
| 1529 | jspb.Message.copyInto = function(fromMessage, toMessage) { |
| 1530 | goog.asserts.assertInstanceof(fromMessage, jspb.Message); |
| 1531 | goog.asserts.assertInstanceof(toMessage, jspb.Message); |
| 1532 | goog.asserts.assert(fromMessage.constructor == toMessage.constructor, |
| 1533 | 'Copy source and target message should have the same type.'); |
| 1534 | var copyOfFrom = jspb.Message.clone(fromMessage); |
| 1535 | |
| 1536 | var to = toMessage.toArray(); |
| 1537 | var from = copyOfFrom.toArray(); |
| 1538 | |
| 1539 | // Empty destination in case it has more values at the end of the array. |
| 1540 | to.length = 0; |
| 1541 | // and then copy everything from the new to the existing message. |
| 1542 | for (var i = 0; i < from.length; i++) { |
| 1543 | to[i] = from[i]; |
| 1544 | } |
| 1545 | |
| 1546 | // This is either null or empty for a fresh copy. |
| 1547 | toMessage.wrappers_ = copyOfFrom.wrappers_; |
| 1548 | // Just a reference into the shared array. |
| 1549 | toMessage.extensionObject_ = copyOfFrom.extensionObject_; |
| 1550 | }; |
| 1551 | |
| 1552 | |
| 1553 | /** |
| 1554 | * Helper for cloning an internal JsPb object. |
| 1555 | * @param {!Object} obj A JsPb object, eg, a field, to be cloned. |
| 1556 | * @return {!Object} A clone of the input object. |
| 1557 | * @private |
| 1558 | */ |
| 1559 | jspb.Message.clone_ = function(obj) { |
| 1560 | var o; |
| 1561 | if (goog.isArray(obj)) { |
| 1562 | // Allocate array of correct size. |
| 1563 | var clonedArray = new Array(obj.length); |
| 1564 | // Use array iteration where possible because it is faster than for-in. |
| 1565 | for (var i = 0; i < obj.length; i++) { |
| 1566 | if ((o = obj[i]) != null) { |
| 1567 | clonedArray[i] = typeof o == 'object' ? jspb.Message.clone_(o) : o; |
| 1568 | } |
| 1569 | } |
| 1570 | return clonedArray; |
| 1571 | } |
| 1572 | if (jspb.Message.SUPPORTS_UINT8ARRAY_ && obj instanceof Uint8Array) { |
| 1573 | return new Uint8Array(obj); |
| 1574 | } |
| 1575 | var clone = {}; |
| 1576 | for (var key in obj) { |
| 1577 | if ((o = obj[key]) != null) { |
| 1578 | clone[key] = typeof o == 'object' ? jspb.Message.clone_(o) : o; |
| 1579 | } |
| 1580 | } |
| 1581 | return clone; |
| 1582 | }; |
| 1583 | |
| 1584 | |
| 1585 | /** |
| 1586 | * Registers a JsPb message type id with its constructor. |
| 1587 | * @param {string} id The id for this type of message. |
| 1588 | * @param {Function} constructor The message constructor. |
| 1589 | */ |
| 1590 | jspb.Message.registerMessageType = function(id, constructor) { |
| 1591 | jspb.Message.registry_[id] = constructor; |
| 1592 | // This is needed so we can later access messageId directly on the contructor, |
| 1593 | // otherwise it is not available due to 'property collapsing' by the compiler. |
| 1594 | constructor.messageId = id; |
| 1595 | }; |
| 1596 | |
| 1597 | |
| 1598 | /** |
| 1599 | * The registry of message ids to message constructors. |
| 1600 | * @private |
| 1601 | */ |
| 1602 | jspb.Message.registry_ = {}; |
| 1603 | |
| 1604 | |
| 1605 | /** |
| 1606 | * The extensions registered on MessageSet. This is a map of extension |
| 1607 | * field number to field info object. This should be considered as a |
| 1608 | * private API. |
| 1609 | * |
| 1610 | * This is similar to [jspb class name].extensions object for |
| 1611 | * non-MessageSet. We special case MessageSet so that we do not need |
| 1612 | * to goog.require MessageSet from classes that extends MessageSet. |
| 1613 | * |
| 1614 | * @type {!Object.<number, jspb.ExtensionFieldInfo>} |
| 1615 | */ |
| 1616 | jspb.Message.messageSetExtensions = {}; |
| 1617 | jspb.Message.messageSetExtensionsBinary = {}; |