message.js 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782
  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. // There are 3 variants that need to be dealt with which are the
  384. // combination of whether there exists an extension object (EO) and
  385. // whether there is a suggested pivot (SP).
  386. //
  387. // EO, ? : pivot is the index of the EO
  388. // no-EO, no-SP: pivot is MAX_INT
  389. // no-EO, SP : pivot is the max(lastindex + 1, SP)
  390. var msgLength = msg.array.length;
  391. var lastIndex = -1;
  392. if (msgLength) {
  393. lastIndex = msgLength - 1;
  394. var obj = msg.array[lastIndex];
  395. // Normal fields are never objects, so we can be sure that if we find an
  396. // object here, then it's the extension object. However, we must ensure that
  397. // the object is not an array, since arrays are valid field values.
  398. // NOTE(lukestebbing): We avoid looking at .length to avoid a JIT bug
  399. // in Safari on iOS 8. See the description of CL/86511464 for details.
  400. if (obj && typeof obj == 'object' && !jspb.Message.isArray_(obj) &&
  401. !(jspb.Message.SUPPORTS_UINT8ARRAY_ && obj instanceof Uint8Array)) {
  402. msg.pivot_ = jspb.Message.getFieldNumber_(msg, lastIndex);
  403. msg.extensionObject_ = obj;
  404. return;
  405. }
  406. }
  407. if (suggestedPivot > -1) {
  408. // If a extension object is not present, set the pivot value as being
  409. // after the last value in the array to avoid overwriting values, etc.
  410. msg.pivot_ = Math.max(
  411. suggestedPivot, jspb.Message.getFieldNumber_(msg, lastIndex + 1));
  412. // Avoid changing the shape of the proto with an empty extension object by
  413. // deferring the materialization of the extension object until the first
  414. // time a field set into it (may be due to getting a repeated proto field
  415. // from it, in which case a new empty array is set into it at first).
  416. msg.extensionObject_ = null;
  417. } else {
  418. // suggestedPivot is -1, which means that we don't have an extension object
  419. // at all, in which case all fields are stored in the array.
  420. msg.pivot_ = Number.MAX_VALUE;
  421. }
  422. };
  423. /**
  424. * Creates an empty extensionObject_ if non exists.
  425. * @param {!jspb.Message} msg The JsPb proto to modify.
  426. * @private
  427. */
  428. jspb.Message.maybeInitEmptyExtensionObject_ = function(msg) {
  429. var pivotIndex = jspb.Message.getIndex_(msg, msg.pivot_);
  430. if (!msg.array[pivotIndex]) {
  431. msg.extensionObject_ = msg.array[pivotIndex] = {};
  432. }
  433. };
  434. /**
  435. * Converts a JsPb repeated message field into an object list.
  436. * @param {!Array<T>} field The repeated message field to be
  437. * converted.
  438. * @param {?function(boolean=): Object|
  439. * function((boolean|undefined),T): Object} toObjectFn The toObject
  440. * function for this field. We need to pass this for effective dead code
  441. * removal.
  442. * @param {boolean=} opt_includeInstance Whether to include the JSPB instance
  443. * for transitional soy proto support: http://goto/soy-param-migration
  444. * @return {!Array<Object>} An array of converted message objects.
  445. * @template T
  446. */
  447. jspb.Message.toObjectList = function(field, toObjectFn, opt_includeInstance) {
  448. // Not using goog.array.map in the generated code to keep it small.
  449. // And not using it here to avoid a function call.
  450. var result = [];
  451. for (var i = 0; i < field.length; i++) {
  452. result[i] = toObjectFn.call(field[i], opt_includeInstance, field[i]);
  453. }
  454. return result;
  455. };
  456. /**
  457. * Adds a proto's extension data to a Soy rendering object.
  458. * @param {!jspb.Message} proto The proto whose extensions to convert.
  459. * @param {!Object} obj The Soy object to add converted extension data to.
  460. * @param {!Object} extensions The proto class' registered extensions.
  461. * @param {function(this:?, jspb.ExtensionFieldInfo) : *} getExtensionFn
  462. * The proto class' getExtension function. Passed for effective dead code
  463. * removal.
  464. * @param {boolean=} opt_includeInstance Whether to include the JSPB instance
  465. * for transitional soy proto support: http://goto/soy-param-migration
  466. */
  467. jspb.Message.toObjectExtension = function(proto, obj, extensions,
  468. getExtensionFn, opt_includeInstance) {
  469. for (var fieldNumber in extensions) {
  470. var fieldInfo = extensions[fieldNumber];
  471. var value = getExtensionFn.call(proto, fieldInfo);
  472. if (value != null) {
  473. for (var name in fieldInfo.fieldName) {
  474. if (fieldInfo.fieldName.hasOwnProperty(name)) {
  475. break; // the compiled field name
  476. }
  477. }
  478. if (!fieldInfo.toObjectFn) {
  479. obj[name] = value;
  480. } else {
  481. if (fieldInfo.isRepeated) {
  482. obj[name] = jspb.Message.toObjectList(
  483. /** @type {!Array<!jspb.Message>} */ (value),
  484. fieldInfo.toObjectFn, opt_includeInstance);
  485. } else {
  486. obj[name] = fieldInfo.toObjectFn(
  487. opt_includeInstance, /** @type {!jspb.Message} */ (value));
  488. }
  489. }
  490. }
  491. }
  492. };
  493. /**
  494. * Writes a proto's extension data to a binary-format output stream.
  495. * @param {!jspb.Message} proto The proto whose extensions to convert.
  496. * @param {*} writer The binary-format writer to write to.
  497. * @param {!Object} extensions The proto class' registered extensions.
  498. * @param {function(this:jspb.Message,!jspb.ExtensionFieldInfo) : *} getExtensionFn The proto
  499. * class' getExtension function. Passed for effective dead code removal.
  500. */
  501. jspb.Message.serializeBinaryExtensions = function(proto, writer, extensions,
  502. getExtensionFn) {
  503. for (var fieldNumber in extensions) {
  504. var binaryFieldInfo = extensions[fieldNumber];
  505. var fieldInfo = binaryFieldInfo.fieldInfo;
  506. // The old codegen doesn't add the extra fields to ExtensionFieldInfo, so we
  507. // need to gracefully error-out here rather than produce a null dereference
  508. // below.
  509. if (!binaryFieldInfo.binaryWriterFn) {
  510. throw new Error('Message extension present that was generated ' +
  511. 'without binary serialization support');
  512. }
  513. var value = getExtensionFn.call(proto, fieldInfo);
  514. if (value != null) {
  515. if (fieldInfo.isMessageType()) {
  516. // If the message type of the extension was generated without binary
  517. // support, there may not be a binary message serializer function, and
  518. // we can't know when we codegen the extending message that the extended
  519. // message may require binary support, so we can *only* catch this error
  520. // here, at runtime (and this decoupled codegen is the whole point of
  521. // extensions!).
  522. if (binaryFieldInfo.binaryMessageSerializeFn) {
  523. binaryFieldInfo.binaryWriterFn.call(writer, fieldInfo.fieldIndex,
  524. value, binaryFieldInfo.binaryMessageSerializeFn);
  525. } else {
  526. throw new Error('Message extension present holding submessage ' +
  527. 'without binary support enabled, and message is ' +
  528. 'being serialized to binary format');
  529. }
  530. } else {
  531. binaryFieldInfo.binaryWriterFn.call(
  532. writer, fieldInfo.fieldIndex, value);
  533. }
  534. }
  535. }
  536. };
  537. /**
  538. * Reads an extension field from the given reader and, if a valid extension,
  539. * sets the extension value.
  540. * @param {!jspb.Message} msg A jspb proto.
  541. * @param {{
  542. * skipField:function(this:jspb.BinaryReader),
  543. * getFieldNumber:function(this:jspb.BinaryReader):number
  544. * }} reader
  545. * @param {!Object} extensions The extensions object.
  546. * @param {function(this:jspb.Message,!jspb.ExtensionFieldInfo)} getExtensionFn
  547. * @param {function(this:jspb.Message,!jspb.ExtensionFieldInfo, ?)} setExtensionFn
  548. */
  549. jspb.Message.readBinaryExtension = function(msg, reader, extensions,
  550. getExtensionFn, setExtensionFn) {
  551. var binaryFieldInfo = extensions[reader.getFieldNumber()];
  552. if (!binaryFieldInfo) {
  553. reader.skipField();
  554. return;
  555. }
  556. var fieldInfo = binaryFieldInfo.fieldInfo;
  557. if (!binaryFieldInfo.binaryReaderFn) {
  558. throw new Error('Deserializing extension whose generated code does not ' +
  559. 'support binary format');
  560. }
  561. var value;
  562. if (fieldInfo.isMessageType()) {
  563. value = new fieldInfo.ctor();
  564. binaryFieldInfo.binaryReaderFn.call(
  565. reader, value, binaryFieldInfo.binaryMessageDeserializeFn);
  566. } else {
  567. // All other types.
  568. value = binaryFieldInfo.binaryReaderFn.call(reader);
  569. }
  570. if (fieldInfo.isRepeated && !binaryFieldInfo.isPacked) {
  571. var currentList = getExtensionFn.call(msg, fieldInfo);
  572. if (!currentList) {
  573. setExtensionFn.call(msg, fieldInfo, [value]);
  574. } else {
  575. currentList.push(value);
  576. }
  577. } else {
  578. setExtensionFn.call(msg, fieldInfo, value);
  579. }
  580. };
  581. /**
  582. * Gets the value of a non-extension field.
  583. * @param {!jspb.Message} msg A jspb proto.
  584. * @param {number} fieldNumber The field number.
  585. * @return {string|number|boolean|Uint8Array|Array|null|undefined}
  586. * The field's value.
  587. * @protected
  588. */
  589. jspb.Message.getField = function(msg, fieldNumber) {
  590. if (fieldNumber < msg.pivot_) {
  591. var index = jspb.Message.getIndex_(msg, fieldNumber);
  592. var val = msg.array[index];
  593. if (val === jspb.Message.EMPTY_LIST_SENTINEL_) {
  594. return msg.array[index] = [];
  595. }
  596. return val;
  597. } else {
  598. if (!msg.extensionObject_) {
  599. return undefined;
  600. }
  601. var val = msg.extensionObject_[fieldNumber];
  602. if (val === jspb.Message.EMPTY_LIST_SENTINEL_) {
  603. return msg.extensionObject_[fieldNumber] = [];
  604. }
  605. return val;
  606. }
  607. };
  608. /**
  609. * Gets the value of a non-extension repeated field.
  610. * @param {!jspb.Message} msg A jspb proto.
  611. * @param {number} fieldNumber The field number.
  612. * @return {!Array}
  613. * The field's value.
  614. * @protected
  615. */
  616. jspb.Message.getRepeatedField = function(msg, fieldNumber) {
  617. if (fieldNumber < msg.pivot_) {
  618. var index = jspb.Message.getIndex_(msg, fieldNumber);
  619. var val = msg.array[index];
  620. if (val === jspb.Message.EMPTY_LIST_SENTINEL_) {
  621. return msg.array[index] = [];
  622. }
  623. return val;
  624. }
  625. var val = msg.extensionObject_[fieldNumber];
  626. if (val === jspb.Message.EMPTY_LIST_SENTINEL_) {
  627. return msg.extensionObject_[fieldNumber] = [];
  628. }
  629. return val;
  630. };
  631. /**
  632. * Gets the value of an optional float or double field.
  633. * @param {!jspb.Message} msg A jspb proto.
  634. * @param {number} fieldNumber The field number.
  635. * @return {?number|undefined} The field's value.
  636. * @protected
  637. */
  638. jspb.Message.getOptionalFloatingPointField = function(msg, fieldNumber) {
  639. var value = jspb.Message.getField(msg, fieldNumber);
  640. // Converts "NaN", "Infinity" and "-Infinity" to their corresponding numbers.
  641. return value == null ? value : +value;
  642. };
  643. /**
  644. * Gets the value of a repeated float or double field.
  645. * @param {!jspb.Message} msg A jspb proto.
  646. * @param {number} fieldNumber The field number.
  647. * @return {!Array<number>} The field's value.
  648. * @protected
  649. */
  650. jspb.Message.getRepeatedFloatingPointField = function(msg, fieldNumber) {
  651. var values = jspb.Message.getRepeatedField(msg, fieldNumber);
  652. if (!msg.convertedFloatingPointFields_) {
  653. msg.convertedFloatingPointFields_ = {};
  654. }
  655. if (!msg.convertedFloatingPointFields_[fieldNumber]) {
  656. for (var i = 0; i < values.length; i++) {
  657. // Converts "NaN", "Infinity" and "-Infinity" to their corresponding
  658. // numbers.
  659. values[i] = +values[i];
  660. }
  661. msg.convertedFloatingPointFields_[fieldNumber] = true;
  662. }
  663. return /** @type {!Array<number>} */ (values);
  664. };
  665. /**
  666. * Coerce a 'bytes' field to a base 64 string.
  667. * @param {string|Uint8Array|null} value
  668. * @return {?string} The field's coerced value.
  669. */
  670. jspb.Message.bytesAsB64 = function(value) {
  671. if (value == null || goog.isString(value)) {
  672. return value;
  673. }
  674. if (jspb.Message.SUPPORTS_UINT8ARRAY_ && value instanceof Uint8Array) {
  675. return goog.crypt.base64.encodeByteArray(value);
  676. }
  677. goog.asserts.fail('Cannot coerce to b64 string: ' + goog.typeOf(value));
  678. return null;
  679. };
  680. /**
  681. * Coerce a 'bytes' field to a Uint8Array byte buffer.
  682. * Note that Uint8Array is not supported on IE versions before 10 nor on Opera
  683. * Mini. @see http://caniuse.com/Uint8Array
  684. * @param {string|Uint8Array|null} value
  685. * @return {?Uint8Array} The field's coerced value.
  686. */
  687. jspb.Message.bytesAsU8 = function(value) {
  688. if (value == null || value instanceof Uint8Array) {
  689. return value;
  690. }
  691. if (goog.isString(value)) {
  692. return goog.crypt.base64.decodeStringToUint8Array(value);
  693. }
  694. goog.asserts.fail('Cannot coerce to Uint8Array: ' + goog.typeOf(value));
  695. return null;
  696. };
  697. /**
  698. * Coerce a repeated 'bytes' field to an array of base 64 strings.
  699. * Note: the returned array should be treated as immutable.
  700. * @param {!Array<string>|!Array<!Uint8Array>} value
  701. * @return {!Array<string?>} The field's coerced value.
  702. */
  703. jspb.Message.bytesListAsB64 = function(value) {
  704. jspb.Message.assertConsistentTypes_(value);
  705. if (!value.length || goog.isString(value[0])) {
  706. return /** @type {!Array<string>} */ (value);
  707. }
  708. return goog.array.map(value, jspb.Message.bytesAsB64);
  709. };
  710. /**
  711. * Coerce a repeated 'bytes' field to an array of Uint8Array byte buffers.
  712. * Note: the returned array should be treated as immutable.
  713. * Note that Uint8Array is not supported on IE versions before 10 nor on Opera
  714. * Mini. @see http://caniuse.com/Uint8Array
  715. * @param {!Array<string>|!Array<!Uint8Array>} value
  716. * @return {!Array<Uint8Array?>} The field's coerced value.
  717. */
  718. jspb.Message.bytesListAsU8 = function(value) {
  719. jspb.Message.assertConsistentTypes_(value);
  720. if (!value.length || value[0] instanceof Uint8Array) {
  721. return /** @type {!Array<!Uint8Array>} */ (value);
  722. }
  723. return goog.array.map(value, jspb.Message.bytesAsU8);
  724. };
  725. /**
  726. * Asserts that all elements of an array are of the same type.
  727. * @param {Array?} array The array to test.
  728. * @private
  729. */
  730. jspb.Message.assertConsistentTypes_ = function(array) {
  731. if (goog.DEBUG && array && array.length > 1) {
  732. var expected = goog.typeOf(array[0]);
  733. goog.array.forEach(array, function(e) {
  734. if (goog.typeOf(e) != expected) {
  735. goog.asserts.fail('Inconsistent type in JSPB repeated field array. ' +
  736. 'Got ' + goog.typeOf(e) + ' expected ' + expected);
  737. }
  738. });
  739. }
  740. };
  741. /**
  742. * Gets the value of a non-extension primitive field, with proto3 (non-nullable
  743. * primitives) semantics. Returns `defaultValue` if the field is not otherwise
  744. * set.
  745. * @template T
  746. * @param {!jspb.Message} msg A jspb proto.
  747. * @param {number} fieldNumber The field number.
  748. * @param {T} defaultValue The default value.
  749. * @return {T} The field's value.
  750. * @protected
  751. */
  752. jspb.Message.getFieldWithDefault = function(msg, fieldNumber, defaultValue) {
  753. var value = jspb.Message.getField(msg, fieldNumber);
  754. if (value == null) {
  755. return defaultValue;
  756. } else {
  757. return value;
  758. }
  759. };
  760. /**
  761. * Alias for getFieldWithDefault used by older generated code.
  762. * @template T
  763. * @param {!jspb.Message} msg A jspb proto.
  764. * @param {number} fieldNumber The field number.
  765. * @param {T} defaultValue The default value.
  766. * @return {T} The field's value.
  767. * @protected
  768. */
  769. jspb.Message.getFieldProto3 = jspb.Message.getFieldWithDefault;
  770. /**
  771. * Gets the value of a map field, lazily creating the map container if
  772. * necessary.
  773. *
  774. * This should only be called from generated code, because it requires knowledge
  775. * of serialization/parsing callbacks (which are required by the map at
  776. * construction time, and the map may be constructed here).
  777. *
  778. * @template K, V
  779. * @param {!jspb.Message} msg
  780. * @param {number} fieldNumber
  781. * @param {boolean|undefined} noLazyCreate
  782. * @param {?=} opt_valueCtor
  783. * @return {!jspb.Map<K, V>|undefined}
  784. * @protected
  785. */
  786. jspb.Message.getMapField = function(msg, fieldNumber, noLazyCreate,
  787. opt_valueCtor) {
  788. if (!msg.wrappers_) {
  789. msg.wrappers_ = {};
  790. }
  791. // If we already have a map in the map wrappers, return that.
  792. if (fieldNumber in msg.wrappers_) {
  793. return msg.wrappers_[fieldNumber];
  794. } else if (noLazyCreate) {
  795. return undefined;
  796. } else {
  797. // Wrap the underlying elements array with a Map.
  798. var arr = jspb.Message.getField(msg, fieldNumber);
  799. if (!arr) {
  800. arr = [];
  801. jspb.Message.setField(msg, fieldNumber, arr);
  802. }
  803. return msg.wrappers_[fieldNumber] =
  804. new jspb.Map(
  805. /** @type {!Array<!Array<!Object>>} */ (arr), opt_valueCtor);
  806. }
  807. };
  808. /**
  809. * Sets the value of a non-extension field.
  810. * @param {!jspb.Message} msg A jspb proto.
  811. * @param {number} fieldNumber The field number.
  812. * @param {string|number|boolean|Uint8Array|Array|undefined} value New value
  813. * @protected
  814. */
  815. jspb.Message.setField = function(msg, fieldNumber, value) {
  816. if (fieldNumber < msg.pivot_) {
  817. msg.array[jspb.Message.getIndex_(msg, fieldNumber)] = value;
  818. } else {
  819. jspb.Message.maybeInitEmptyExtensionObject_(msg);
  820. msg.extensionObject_[fieldNumber] = value;
  821. }
  822. };
  823. /**
  824. * Sets the value of a non-extension integer field of a proto3
  825. * @param {!jspb.Message} msg A jspb proto.
  826. * @param {number} fieldNumber The field number.
  827. * @param {number} value New value
  828. * @protected
  829. */
  830. jspb.Message.setProto3IntField = function(msg, fieldNumber, value) {
  831. jspb.Message.setFieldIgnoringDefault_(msg, fieldNumber, value, 0);
  832. };
  833. /**
  834. * Sets the value of a non-extension floating point field of a proto3
  835. * @param {!jspb.Message} msg A jspb proto.
  836. * @param {number} fieldNumber The field number.
  837. * @param {number} value New value
  838. * @protected
  839. */
  840. jspb.Message.setProto3FloatField = function(msg, fieldNumber, value) {
  841. jspb.Message.setFieldIgnoringDefault_(msg, fieldNumber, value, 0.0);
  842. };
  843. /**
  844. * Sets the value of a non-extension boolean field of a proto3
  845. * @param {!jspb.Message} msg A jspb proto.
  846. * @param {number} fieldNumber The field number.
  847. * @param {boolean} value New value
  848. * @protected
  849. */
  850. jspb.Message.setProto3BooleanField = function(msg, fieldNumber, value) {
  851. jspb.Message.setFieldIgnoringDefault_(msg, fieldNumber, value, false);
  852. };
  853. /**
  854. * Sets the value of a non-extension String field of a proto3
  855. * @param {!jspb.Message} msg A jspb proto.
  856. * @param {number} fieldNumber The field number.
  857. * @param {string} value New value
  858. * @protected
  859. */
  860. jspb.Message.setProto3StringField = function(msg, fieldNumber, value) {
  861. jspb.Message.setFieldIgnoringDefault_(msg, fieldNumber, value, "");
  862. };
  863. /**
  864. * Sets the value of a non-extension Bytes field of a proto3
  865. * @param {!jspb.Message} msg A jspb proto.
  866. * @param {number} fieldNumber The field number.
  867. * @param {!Uint8Array|string} value New value
  868. * @protected
  869. */
  870. jspb.Message.setProto3BytesField = function(msg, fieldNumber, value) {
  871. jspb.Message.setFieldIgnoringDefault_(msg, fieldNumber, value, "");
  872. };
  873. /**
  874. * Sets the value of a non-extension enum field of a proto3
  875. * @param {!jspb.Message} msg A jspb proto.
  876. * @param {number} fieldNumber The field number.
  877. * @param {number} value New value
  878. * @protected
  879. */
  880. jspb.Message.setProto3EnumField = function(msg, fieldNumber, value) {
  881. jspb.Message.setFieldIgnoringDefault_(msg, fieldNumber, value, 0);
  882. };
  883. /**
  884. * Sets the value of a non-extension int field of a proto3 that has jstype set
  885. * to String.
  886. * @param {!jspb.Message} msg A jspb proto.
  887. * @param {number} fieldNumber The field number.
  888. * @param {string} value New value
  889. * @protected
  890. */
  891. jspb.Message.setProto3StringIntField = function(msg, fieldNumber, value) {
  892. jspb.Message.setFieldIgnoringDefault_(msg, fieldNumber, value, "0");
  893. };
  894. /**
  895. * Sets the value of a non-extension primitive field, with proto3 (non-nullable
  896. * primitives) semantics of ignoring values that are equal to the type's
  897. * default.
  898. * @param {!jspb.Message} msg A jspb proto.
  899. * @param {number} fieldNumber The field number.
  900. * @param {!Uint8Array|string|number|boolean|undefined} value New value
  901. * @param {!Uint8Array|string|number|boolean} defaultValue The default value.
  902. * @private
  903. */
  904. jspb.Message.setFieldIgnoringDefault_ = function(
  905. msg, fieldNumber, value, defaultValue) {
  906. if (value !== defaultValue) {
  907. jspb.Message.setField(msg, fieldNumber, value);
  908. } else {
  909. msg.array[jspb.Message.getIndex_(msg, fieldNumber)] = null;
  910. }
  911. };
  912. /**
  913. * Adds a value to a repeated, primitive field.
  914. * @param {!jspb.Message} msg A jspb proto.
  915. * @param {number} fieldNumber The field number.
  916. * @param {string|number|boolean|!Uint8Array} value New value
  917. * @param {number=} opt_index Index where to put new value.
  918. * @protected
  919. */
  920. jspb.Message.addToRepeatedField = function(msg, fieldNumber, value, opt_index) {
  921. var arr = jspb.Message.getRepeatedField(msg, fieldNumber);
  922. if (opt_index != undefined) {
  923. arr.splice(opt_index, 0, value);
  924. } else {
  925. arr.push(value);
  926. }
  927. };
  928. /**
  929. * Sets the value of a field in a oneof union and clears all other fields in
  930. * the union.
  931. * @param {!jspb.Message} msg A jspb proto.
  932. * @param {number} fieldNumber The field number.
  933. * @param {!Array<number>} oneof The fields belonging to the union.
  934. * @param {string|number|boolean|Uint8Array|Array|undefined} value New value
  935. * @protected
  936. */
  937. jspb.Message.setOneofField = function(msg, fieldNumber, oneof, value) {
  938. var currentCase = jspb.Message.computeOneofCase(msg, oneof);
  939. if (currentCase && currentCase !== fieldNumber && value !== undefined) {
  940. if (msg.wrappers_ && currentCase in msg.wrappers_) {
  941. msg.wrappers_[currentCase] = undefined;
  942. }
  943. jspb.Message.setField(msg, currentCase, undefined);
  944. }
  945. jspb.Message.setField(msg, fieldNumber, value);
  946. };
  947. /**
  948. * Computes the selection in a oneof group for the given message, ensuring
  949. * only one field is set in the process.
  950. *
  951. * According to the protobuf language guide (
  952. * https://developers.google.com/protocol-buffers/docs/proto#oneof), "if the
  953. * parser encounters multiple members of the same oneof on the wire, only the
  954. * last member seen is used in the parsed message." Since JSPB serializes
  955. * messages to a JSON array, the "last member seen" will always be the field
  956. * with the greatest field number (directly corresponding to the greatest
  957. * array index).
  958. *
  959. * @param {!jspb.Message} msg A jspb proto.
  960. * @param {!Array<number>} oneof The field numbers belonging to the union.
  961. * @return {number} The field number currently set in the union, or 0 if none.
  962. * @protected
  963. */
  964. jspb.Message.computeOneofCase = function(msg, oneof) {
  965. var oneofField;
  966. var oneofValue;
  967. for (var i = 0; i < oneof.length; i++) {
  968. var fieldNumber = oneof[i];
  969. var value = jspb.Message.getField(msg, fieldNumber);
  970. if (value != null) {
  971. oneofField = fieldNumber;
  972. oneofValue = value;
  973. jspb.Message.setField(msg, fieldNumber, undefined);
  974. }
  975. }
  976. if (oneofField) {
  977. // NB: We know the value is unique, so we can call jspb.Message.setField
  978. // directly instead of jpsb.Message.setOneofField. Also, setOneofField
  979. // calls this function.
  980. jspb.Message.setField(msg, oneofField, oneofValue);
  981. return oneofField;
  982. }
  983. return 0;
  984. };
  985. /**
  986. * Gets and wraps a proto field on access.
  987. * @param {!jspb.Message} msg A jspb proto.
  988. * @param {function(new:jspb.Message, Array)} ctor Constructor for the field.
  989. * @param {number} fieldNumber The field number.
  990. * @param {number=} opt_required True (1) if this is a required field.
  991. * @return {jspb.Message} The field as a jspb proto.
  992. * @protected
  993. */
  994. jspb.Message.getWrapperField = function(msg, ctor, fieldNumber, opt_required) {
  995. // TODO(mwr): Consider copying data and/or arrays.
  996. if (!msg.wrappers_) {
  997. msg.wrappers_ = {};
  998. }
  999. if (!msg.wrappers_[fieldNumber]) {
  1000. var data = /** @type {Array} */ (jspb.Message.getField(msg, fieldNumber));
  1001. if (opt_required || data) {
  1002. // TODO(mwr): Remove existence test for always valid default protos.
  1003. msg.wrappers_[fieldNumber] = new ctor(data);
  1004. }
  1005. }
  1006. return /** @type {jspb.Message} */ (msg.wrappers_[fieldNumber]);
  1007. };
  1008. /**
  1009. * Gets and wraps a repeated proto field on access.
  1010. * @param {!jspb.Message} msg A jspb proto.
  1011. * @param {function(new:jspb.Message, Array)} ctor Constructor for the field.
  1012. * @param {number} fieldNumber The field number.
  1013. * @return {!Array<!jspb.Message>} The repeated field as an array of protos.
  1014. * @protected
  1015. */
  1016. jspb.Message.getRepeatedWrapperField = function(msg, ctor, fieldNumber) {
  1017. jspb.Message.wrapRepeatedField_(msg, ctor, fieldNumber);
  1018. var val = msg.wrappers_[fieldNumber];
  1019. if (val == jspb.Message.EMPTY_LIST_SENTINEL_) {
  1020. val = msg.wrappers_[fieldNumber] = [];
  1021. }
  1022. return /** @type {!Array<!jspb.Message>} */ (val);
  1023. };
  1024. /**
  1025. * Wraps underlying array into proto message representation if it wasn't done
  1026. * before.
  1027. * @param {!jspb.Message} msg A jspb proto.
  1028. * @param {function(new:jspb.Message, ?Array)} ctor Constructor for the field.
  1029. * @param {number} fieldNumber The field number.
  1030. * @private
  1031. */
  1032. jspb.Message.wrapRepeatedField_ = function(msg, ctor, fieldNumber) {
  1033. if (!msg.wrappers_) {
  1034. msg.wrappers_ = {};
  1035. }
  1036. if (!msg.wrappers_[fieldNumber]) {
  1037. var data = jspb.Message.getRepeatedField(msg, fieldNumber);
  1038. for (var wrappers = [], i = 0; i < data.length; i++) {
  1039. wrappers[i] = new ctor(data[i]);
  1040. }
  1041. msg.wrappers_[fieldNumber] = wrappers;
  1042. }
  1043. };
  1044. /**
  1045. * Sets a proto field and syncs it to the backing array.
  1046. * @param {!jspb.Message} msg A jspb proto.
  1047. * @param {number} fieldNumber The field number.
  1048. * @param {?jspb.Message|?jspb.Map|undefined} value A new value for this proto
  1049. * field.
  1050. * @protected
  1051. */
  1052. jspb.Message.setWrapperField = function(msg, fieldNumber, value) {
  1053. if (!msg.wrappers_) {
  1054. msg.wrappers_ = {};
  1055. }
  1056. var data = value ? value.toArray() : value;
  1057. msg.wrappers_[fieldNumber] = value;
  1058. jspb.Message.setField(msg, fieldNumber, data);
  1059. };
  1060. /**
  1061. * Sets a proto field in a oneof union and syncs it to the backing array.
  1062. * @param {!jspb.Message} msg A jspb proto.
  1063. * @param {number} fieldNumber The field number.
  1064. * @param {!Array<number>} oneof The fields belonging to the union.
  1065. * @param {jspb.Message|undefined} value A new value for this proto field.
  1066. * @protected
  1067. */
  1068. jspb.Message.setOneofWrapperField = function(msg, fieldNumber, oneof, value) {
  1069. if (!msg.wrappers_) {
  1070. msg.wrappers_ = {};
  1071. }
  1072. var data = value ? value.toArray() : value;
  1073. msg.wrappers_[fieldNumber] = value;
  1074. jspb.Message.setOneofField(msg, fieldNumber, oneof, data);
  1075. };
  1076. /**
  1077. * Sets a repeated proto field and syncs it to the backing array.
  1078. * @param {!jspb.Message} msg A jspb proto.
  1079. * @param {number} fieldNumber The field number.
  1080. * @param {Array<!jspb.Message>|undefined} value An array of protos.
  1081. * @protected
  1082. */
  1083. jspb.Message.setRepeatedWrapperField = function(msg, fieldNumber, value) {
  1084. if (!msg.wrappers_) {
  1085. msg.wrappers_ = {};
  1086. }
  1087. value = value || [];
  1088. for (var data = [], i = 0; i < value.length; i++) {
  1089. data[i] = value[i].toArray();
  1090. }
  1091. msg.wrappers_[fieldNumber] = value;
  1092. jspb.Message.setField(msg, fieldNumber, data);
  1093. };
  1094. /**
  1095. * Add a message to a repeated proto field.
  1096. * @param {!jspb.Message} msg A jspb proto.
  1097. * @param {number} fieldNumber The field number.
  1098. * @param {T_CHILD|undefined} value Proto that will be added to the
  1099. * repeated field.
  1100. * @param {function(new:T_CHILD, ?Array=)} ctor The constructor of the
  1101. * message type.
  1102. * @param {number|undefined} index Index at which to insert the value.
  1103. * @return {T_CHILD_NOT_UNDEFINED} proto that was inserted to the repeated field
  1104. * @template MessageType
  1105. * Use go/closure-ttl to declare a non-undefined version of T_CHILD. Replace the
  1106. * undefined in blah|undefined with none. This is necessary because the compiler
  1107. * will infer T_CHILD to be |undefined.
  1108. * @template T_CHILD
  1109. * @template T_CHILD_NOT_UNDEFINED :=
  1110. * cond(isUnknown(T_CHILD), unknown(),
  1111. * mapunion(T_CHILD, (X) =>
  1112. * cond(eq(X, 'undefined'), none(), X)))
  1113. * =:
  1114. * @protected
  1115. */
  1116. jspb.Message.addToRepeatedWrapperField = function(
  1117. msg, fieldNumber, value, ctor, index) {
  1118. jspb.Message.wrapRepeatedField_(msg, ctor, fieldNumber);
  1119. var wrapperArray = msg.wrappers_[fieldNumber];
  1120. if (!wrapperArray) {
  1121. wrapperArray = msg.wrappers_[fieldNumber] = [];
  1122. }
  1123. var insertedValue = value ? value : new ctor();
  1124. var array = jspb.Message.getRepeatedField(msg, fieldNumber);
  1125. if (index != undefined) {
  1126. wrapperArray.splice(index, 0, insertedValue);
  1127. array.splice(index, 0, insertedValue.toArray());
  1128. } else {
  1129. wrapperArray.push(insertedValue);
  1130. array.push(insertedValue.toArray());
  1131. }
  1132. return insertedValue;
  1133. };
  1134. /**
  1135. * Converts a JsPb repeated message field into a map. The map will contain
  1136. * protos unless an optional toObject function is given, in which case it will
  1137. * contain objects suitable for Soy rendering.
  1138. * @param {!Array<T>} field The repeated message field to be
  1139. * converted.
  1140. * @param {function() : string?} mapKeyGetterFn The function to get the key of
  1141. * the map.
  1142. * @param {?function(boolean=): Object|
  1143. * function((boolean|undefined),T): Object} opt_toObjectFn The
  1144. * toObject function for this field. We need to pass this for effective
  1145. * dead code removal.
  1146. * @param {boolean=} opt_includeInstance Whether to include the JSPB instance
  1147. * for transitional soy proto support: http://goto/soy-param-migration
  1148. * @return {!Object<string, Object>} A map of proto or Soy objects.
  1149. * @template T
  1150. */
  1151. jspb.Message.toMap = function(
  1152. field, mapKeyGetterFn, opt_toObjectFn, opt_includeInstance) {
  1153. var result = {};
  1154. for (var i = 0; i < field.length; i++) {
  1155. result[mapKeyGetterFn.call(field[i])] = opt_toObjectFn ?
  1156. opt_toObjectFn.call(field[i], opt_includeInstance,
  1157. /** @type {!jspb.Message} */ (field[i])) : field[i];
  1158. }
  1159. return result;
  1160. };
  1161. /**
  1162. * Syncs all map fields' contents back to their underlying arrays.
  1163. * @private
  1164. */
  1165. jspb.Message.prototype.syncMapFields_ = function() {
  1166. // This iterates over submessage, map, and repeated fields, which is intended.
  1167. // Submessages can contain maps which also need to be synced.
  1168. //
  1169. // There is a lot of opportunity for optimization here. For example we could
  1170. // statically determine that some messages have no submessages with maps and
  1171. // optimize this method away for those just by generating one extra static
  1172. // boolean per message type.
  1173. if (this.wrappers_) {
  1174. for (var fieldNumber in this.wrappers_) {
  1175. var val = this.wrappers_[fieldNumber];
  1176. if (goog.isArray(val)) {
  1177. for (var i = 0; i < val.length; i++) {
  1178. if (val[i]) {
  1179. val[i].toArray();
  1180. }
  1181. }
  1182. } else {
  1183. // Works for submessages and maps.
  1184. if (val) {
  1185. val.toArray();
  1186. }
  1187. }
  1188. }
  1189. }
  1190. };
  1191. /**
  1192. * Returns the internal array of this proto.
  1193. * <p>Note: If you use this array to construct a second proto, the content
  1194. * would then be partially shared between the two protos.
  1195. * @return {!Array} The proto represented as an array.
  1196. */
  1197. jspb.Message.prototype.toArray = function() {
  1198. this.syncMapFields_();
  1199. return this.array;
  1200. };
  1201. if (jspb.Message.GENERATE_TO_STRING) {
  1202. /**
  1203. * Creates a string representation of the internal data array of this proto.
  1204. * <p>NOTE: This string is *not* suitable for use in server requests.
  1205. * @return {string} A string representation of this proto.
  1206. * @override
  1207. */
  1208. jspb.Message.prototype.toString = function() {
  1209. this.syncMapFields_();
  1210. return this.array.toString();
  1211. };
  1212. }
  1213. /**
  1214. * Gets the value of the extension field from the extended object.
  1215. * @param {jspb.ExtensionFieldInfo<T>} fieldInfo Specifies the field to get.
  1216. * @return {T} The value of the field.
  1217. * @template T
  1218. */
  1219. jspb.Message.prototype.getExtension = function(fieldInfo) {
  1220. if (!this.extensionObject_) {
  1221. return undefined;
  1222. }
  1223. if (!this.wrappers_) {
  1224. this.wrappers_ = {};
  1225. }
  1226. var fieldNumber = fieldInfo.fieldIndex;
  1227. if (fieldInfo.isRepeated) {
  1228. if (fieldInfo.isMessageType()) {
  1229. if (!this.wrappers_[fieldNumber]) {
  1230. this.wrappers_[fieldNumber] =
  1231. goog.array.map(this.extensionObject_[fieldNumber] || [],
  1232. function(arr) {
  1233. return new fieldInfo.ctor(arr);
  1234. });
  1235. }
  1236. return this.wrappers_[fieldNumber];
  1237. } else {
  1238. return this.extensionObject_[fieldNumber];
  1239. }
  1240. } else {
  1241. if (fieldInfo.isMessageType()) {
  1242. if (!this.wrappers_[fieldNumber] && this.extensionObject_[fieldNumber]) {
  1243. this.wrappers_[fieldNumber] = new fieldInfo.ctor(
  1244. /** @type {Array|undefined} */ (
  1245. this.extensionObject_[fieldNumber]));
  1246. }
  1247. return this.wrappers_[fieldNumber];
  1248. } else {
  1249. return this.extensionObject_[fieldNumber];
  1250. }
  1251. }
  1252. };
  1253. /**
  1254. * Sets the value of the extension field in the extended object.
  1255. * @param {jspb.ExtensionFieldInfo} fieldInfo Specifies the field to set.
  1256. * @param {jspb.Message|string|Uint8Array|number|boolean|Array?} value The value
  1257. * to set.
  1258. * @return {THIS} For chaining
  1259. * @this {THIS}
  1260. * @template THIS
  1261. */
  1262. jspb.Message.prototype.setExtension = function(fieldInfo, value) {
  1263. // Cast self, since the inferred THIS is unknown inside the function body.
  1264. // https://github.com/google/closure-compiler/issues/1411#issuecomment-232442220
  1265. var self = /** @type {!jspb.Message} */ (this);
  1266. if (!self.wrappers_) {
  1267. self.wrappers_ = {};
  1268. }
  1269. jspb.Message.maybeInitEmptyExtensionObject_(self);
  1270. var fieldNumber = fieldInfo.fieldIndex;
  1271. if (fieldInfo.isRepeated) {
  1272. value = value || [];
  1273. if (fieldInfo.isMessageType()) {
  1274. self.wrappers_[fieldNumber] = value;
  1275. self.extensionObject_[fieldNumber] = goog.array.map(
  1276. /** @type {!Array<!jspb.Message>} */ (value), function(msg) {
  1277. return msg.toArray();
  1278. });
  1279. } else {
  1280. self.extensionObject_[fieldNumber] = value;
  1281. }
  1282. } else {
  1283. if (fieldInfo.isMessageType()) {
  1284. self.wrappers_[fieldNumber] = value;
  1285. self.extensionObject_[fieldNumber] =
  1286. value ? /** @type {!jspb.Message} */ (value).toArray() : value;
  1287. } else {
  1288. self.extensionObject_[fieldNumber] = value;
  1289. }
  1290. }
  1291. return self;
  1292. };
  1293. /**
  1294. * Creates a difference object between two messages.
  1295. *
  1296. * The result will contain the top-level fields of m2 that differ from those of
  1297. * m1 at any level of nesting. No data is cloned, the result object will
  1298. * share its top-level elements with m2 (but not with m1).
  1299. *
  1300. * Note that repeated fields should not have null/undefined elements, but if
  1301. * they do, this operation will treat repeated fields of different length as
  1302. * the same if the only difference between them is due to trailing
  1303. * null/undefined values.
  1304. *
  1305. * @param {!jspb.Message} m1 The first message object.
  1306. * @param {!jspb.Message} m2 The second message object.
  1307. * @return {!jspb.Message} The difference returned as a proto message.
  1308. * Note that the returned message may be missing required fields. This is
  1309. * currently tolerated in Js, but would cause an error if you tried to
  1310. * send such a proto to the server. You can access the raw difference
  1311. * array with result.toArray().
  1312. * @throws {Error} If the messages are responses with different types.
  1313. */
  1314. jspb.Message.difference = function(m1, m2) {
  1315. if (!(m1 instanceof m2.constructor)) {
  1316. throw new Error('Messages have different types.');
  1317. }
  1318. var arr1 = m1.toArray();
  1319. var arr2 = m2.toArray();
  1320. var res = [];
  1321. var start = 0;
  1322. var length = arr1.length > arr2.length ? arr1.length : arr2.length;
  1323. if (m1.getJsPbMessageId()) {
  1324. res[0] = m1.getJsPbMessageId();
  1325. start = 1;
  1326. }
  1327. for (var i = start; i < length; i++) {
  1328. if (!jspb.Message.compareFields(arr1[i], arr2[i])) {
  1329. res[i] = arr2[i];
  1330. }
  1331. }
  1332. return new m1.constructor(res);
  1333. };
  1334. /**
  1335. * Tests whether two messages are equal.
  1336. * @param {jspb.Message|undefined} m1 The first message object.
  1337. * @param {jspb.Message|undefined} m2 The second message object.
  1338. * @return {boolean} true if both messages are null/undefined, or if both are
  1339. * of the same type and have the same field values.
  1340. */
  1341. jspb.Message.equals = function(m1, m2) {
  1342. return m1 == m2 || (!!(m1 && m2) && (m1 instanceof m2.constructor) &&
  1343. jspb.Message.compareFields(m1.toArray(), m2.toArray()));
  1344. };
  1345. /**
  1346. * Compares two message extension fields recursively.
  1347. * @param {!Object} extension1 The first field.
  1348. * @param {!Object} extension2 The second field.
  1349. * @return {boolean} true if the extensions are null/undefined, or otherwise
  1350. * equal.
  1351. */
  1352. jspb.Message.compareExtensions = function(extension1, extension2) {
  1353. extension1 = extension1 || {};
  1354. extension2 = extension2 || {};
  1355. var keys = {};
  1356. for (var name in extension1) {
  1357. keys[name] = 0;
  1358. }
  1359. for (var name in extension2) {
  1360. keys[name] = 0;
  1361. }
  1362. for (name in keys) {
  1363. if (!jspb.Message.compareFields(extension1[name], extension2[name])) {
  1364. return false;
  1365. }
  1366. }
  1367. return true;
  1368. };
  1369. /**
  1370. * Compares two message fields recursively.
  1371. * @param {*} field1 The first field.
  1372. * @param {*} field2 The second field.
  1373. * @return {boolean} true if the fields are null/undefined, or otherwise equal.
  1374. */
  1375. jspb.Message.compareFields = function(field1, field2) {
  1376. // If the fields are trivially equal, they're equal.
  1377. if (field1 == field2) return true;
  1378. if (!goog.isObject(field1) || !goog.isObject(field2)) {
  1379. // NaN != NaN so we cover this case.
  1380. if ((goog.isNumber(field1) && isNaN(field1)) ||
  1381. (goog.isNumber(field2) && isNaN(field2))) {
  1382. // One of the fields might be a string 'NaN'.
  1383. return String(field1) == String(field2);
  1384. }
  1385. // If the fields aren't trivially equal and one of them isn't an object,
  1386. // they can't possibly be equal.
  1387. return false;
  1388. }
  1389. // We have two objects. If they're different types, they're not equal.
  1390. field1 = /** @type {!Object} */(field1);
  1391. field2 = /** @type {!Object} */(field2);
  1392. if (field1.constructor != field2.constructor) return false;
  1393. // If both are Uint8Arrays, compare them element-by-element.
  1394. if (jspb.Message.SUPPORTS_UINT8ARRAY_ && field1.constructor === Uint8Array) {
  1395. var bytes1 = /** @type {!Uint8Array} */(field1);
  1396. var bytes2 = /** @type {!Uint8Array} */(field2);
  1397. if (bytes1.length != bytes2.length) return false;
  1398. for (var i = 0; i < bytes1.length; i++) {
  1399. if (bytes1[i] != bytes2[i]) return false;
  1400. }
  1401. return true;
  1402. }
  1403. // If they're both Arrays, compare them element by element except for the
  1404. // optional extension objects at the end, which we compare separately.
  1405. if (field1.constructor === Array) {
  1406. var typedField1 = /** @type {!Array<?>} */ (field1);
  1407. var typedField2 = /** @type {!Array<?>} */ (field2);
  1408. var extension1 = undefined;
  1409. var extension2 = undefined;
  1410. var length = Math.max(typedField1.length, typedField2.length);
  1411. for (var i = 0; i < length; i++) {
  1412. var val1 = typedField1[i];
  1413. var val2 = typedField2[i];
  1414. if (val1 && (val1.constructor == Object)) {
  1415. goog.asserts.assert(extension1 === undefined);
  1416. goog.asserts.assert(i === typedField1.length - 1);
  1417. extension1 = val1;
  1418. val1 = undefined;
  1419. }
  1420. if (val2 && (val2.constructor == Object)) {
  1421. goog.asserts.assert(extension2 === undefined);
  1422. goog.asserts.assert(i === typedField2.length - 1);
  1423. extension2 = val2;
  1424. val2 = undefined;
  1425. }
  1426. if (!jspb.Message.compareFields(val1, val2)) {
  1427. return false;
  1428. }
  1429. }
  1430. if (extension1 || extension2) {
  1431. extension1 = extension1 || {};
  1432. extension2 = extension2 || {};
  1433. return jspb.Message.compareExtensions(extension1, extension2);
  1434. }
  1435. return true;
  1436. }
  1437. // If they're both plain Objects (i.e. extensions), compare them as
  1438. // extensions.
  1439. if (field1.constructor === Object) {
  1440. return jspb.Message.compareExtensions(field1, field2);
  1441. }
  1442. throw new Error('Invalid type in JSPB array');
  1443. };
  1444. /**
  1445. * Templated, type-safe cloneMessage definition.
  1446. * @return {THIS}
  1447. * @this {THIS}
  1448. * @template THIS
  1449. */
  1450. jspb.Message.prototype.cloneMessage = function() {
  1451. return jspb.Message.cloneMessage(/** @type {!jspb.Message} */ (this));
  1452. };
  1453. /**
  1454. * Alias clone to cloneMessage. goog.object.unsafeClone uses clone to
  1455. * efficiently copy objects. Without this alias, copying jspb messages comes
  1456. * with a large performance penalty.
  1457. * @return {THIS}
  1458. * @this {THIS}
  1459. * @template THIS
  1460. */
  1461. jspb.Message.prototype.clone = function() {
  1462. return jspb.Message.cloneMessage(/** @type {!jspb.Message} */ (this));
  1463. };
  1464. /**
  1465. * Static clone function. NOTE: A type-safe method called "cloneMessage"
  1466. * exists
  1467. * on each generated JsPb class. Do not call this function directly.
  1468. * @param {!jspb.Message} msg A message to clone.
  1469. * @return {!jspb.Message} A deep clone of the given message.
  1470. */
  1471. jspb.Message.clone = function(msg) {
  1472. // Although we could include the wrappers, we leave them out here.
  1473. return jspb.Message.cloneMessage(msg);
  1474. };
  1475. /**
  1476. * @param {!jspb.Message} msg A message to clone.
  1477. * @return {!jspb.Message} A deep clone of the given message.
  1478. * @protected
  1479. */
  1480. jspb.Message.cloneMessage = function(msg) {
  1481. // Although we could include the wrappers, we leave them out here.
  1482. return new msg.constructor(jspb.Message.clone_(msg.toArray()));
  1483. };
  1484. /**
  1485. * Takes 2 messages of the same type and copies the contents of the first
  1486. * message into the second. After this the 2 messages will equals in terms of
  1487. * value semantics but share no state. All data in the destination message will
  1488. * be overridden.
  1489. *
  1490. * @param {MESSAGE} fromMessage Message that will be copied into toMessage.
  1491. * @param {MESSAGE} toMessage Message which will receive a copy of fromMessage
  1492. * as its contents.
  1493. * @template MESSAGE
  1494. */
  1495. jspb.Message.copyInto = function(fromMessage, toMessage) {
  1496. goog.asserts.assertInstanceof(fromMessage, jspb.Message);
  1497. goog.asserts.assertInstanceof(toMessage, jspb.Message);
  1498. goog.asserts.assert(fromMessage.constructor == toMessage.constructor,
  1499. 'Copy source and target message should have the same type.');
  1500. var copyOfFrom = jspb.Message.clone(fromMessage);
  1501. var to = toMessage.toArray();
  1502. var from = copyOfFrom.toArray();
  1503. // Empty destination in case it has more values at the end of the array.
  1504. to.length = 0;
  1505. // and then copy everything from the new to the existing message.
  1506. for (var i = 0; i < from.length; i++) {
  1507. to[i] = from[i];
  1508. }
  1509. // This is either null or empty for a fresh copy.
  1510. toMessage.wrappers_ = copyOfFrom.wrappers_;
  1511. // Just a reference into the shared array.
  1512. toMessage.extensionObject_ = copyOfFrom.extensionObject_;
  1513. };
  1514. /**
  1515. * Helper for cloning an internal JsPb object.
  1516. * @param {!Object} obj A JsPb object, eg, a field, to be cloned.
  1517. * @return {!Object} A clone of the input object.
  1518. * @private
  1519. */
  1520. jspb.Message.clone_ = function(obj) {
  1521. var o;
  1522. if (goog.isArray(obj)) {
  1523. // Allocate array of correct size.
  1524. var clonedArray = new Array(obj.length);
  1525. // Use array iteration where possible because it is faster than for-in.
  1526. for (var i = 0; i < obj.length; i++) {
  1527. o = obj[i];
  1528. if (o != null) {
  1529. // NOTE:redundant null check existing for NTI compatibility.
  1530. // see b/70515949
  1531. clonedArray[i] = (typeof o == 'object') ?
  1532. jspb.Message.clone_(goog.asserts.assert(o)) :
  1533. o;
  1534. }
  1535. }
  1536. return clonedArray;
  1537. }
  1538. if (jspb.Message.SUPPORTS_UINT8ARRAY_ && obj instanceof Uint8Array) {
  1539. return new Uint8Array(obj);
  1540. }
  1541. var clone = {};
  1542. for (var key in obj) {
  1543. o = obj[key];
  1544. if (o != null) {
  1545. // NOTE:redundant null check existing for NTI compatibility.
  1546. // see b/70515949
  1547. clone[key] = (typeof o == 'object') ?
  1548. jspb.Message.clone_(goog.asserts.assert(o)) :
  1549. o;
  1550. }
  1551. }
  1552. return clone;
  1553. };
  1554. /**
  1555. * Registers a JsPb message type id with its constructor.
  1556. * @param {string} id The id for this type of message.
  1557. * @param {Function} constructor The message constructor.
  1558. */
  1559. jspb.Message.registerMessageType = function(id, constructor) {
  1560. jspb.Message.registry_[id] = constructor;
  1561. // This is needed so we can later access messageId directly on the contructor,
  1562. // otherwise it is not available due to 'property collapsing' by the compiler.
  1563. /**
  1564. * @suppress {strictMissingProperties} messageId is not defined on Function
  1565. */
  1566. constructor.messageId = id;
  1567. };
  1568. /**
  1569. * The registry of message ids to message constructors.
  1570. * @private
  1571. */
  1572. jspb.Message.registry_ = {};
  1573. /**
  1574. * The extensions registered on MessageSet. This is a map of extension
  1575. * field number to field info object. This should be considered as a
  1576. * private API.
  1577. *
  1578. * This is similar to [jspb class name].extensions object for
  1579. * non-MessageSet. We special case MessageSet so that we do not need
  1580. * to goog.require MessageSet from classes that extends MessageSet.
  1581. *
  1582. * @type {!Object<number, jspb.ExtensionFieldInfo>}
  1583. */
  1584. jspb.Message.messageSetExtensions = {};
  1585. /**
  1586. * @type {!Object<number, jspb.ExtensionFieldBinaryInfo>}
  1587. */
  1588. jspb.Message.messageSetExtensionsBinary = {};