message.js 58 KB

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