message.js 58 KB

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