message.js 58 KB

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