message.js 58 KB

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