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