writer.js 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724
  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 This file contains utilities for encoding Javascript objects
  32. * into binary, wire-format protocol buffers (in the form of Uint8Arrays) that
  33. * a server can consume directly.
  34. *
  35. * jspb's BinaryWriter class defines methods for efficiently encoding
  36. * Javascript objects into binary, wire-format protocol buffers and supports
  37. * all the fundamental field types used in protocol buffers.
  38. *
  39. * Major caveat 1 - Users of this library _must_ keep their Javascript proto
  40. * parsing code in sync with the original .proto file - presumably you'll be
  41. * using the typed jspb code generator, but if you bypass that you'll need
  42. * to keep things in sync by hand.
  43. *
  44. * Major caveat 2 - Javascript is unable to accurately represent integers
  45. * larger than 2^53 due to its use of a double-precision floating point format
  46. * for all numbers. BinaryWriter does not make any special effort to preserve
  47. * precision for values above this limit - if you need to pass 64-bit integers
  48. * (hash codes, for example) between the client and server without precision
  49. * loss, do _not_ use this library.
  50. *
  51. * Major caveat 3 - This class uses typed arrays and must not be used on older
  52. * browsers that do not support them.
  53. *
  54. * @author aappleby@google.com (Austin Appleby)
  55. */
  56. goog.provide('jspb.BinaryWriter');
  57. goog.require('goog.asserts');
  58. goog.require('goog.crypt.base64');
  59. goog.require('jspb.BinaryConstants');
  60. goog.require('jspb.BinaryEncoder');
  61. goog.require('jspb.arith.Int64');
  62. goog.require('jspb.arith.UInt64');
  63. goog.require('jspb.utils');
  64. /**
  65. * BinaryWriter implements encoders for all the wire types specified in
  66. * https://developers.google.com/protocol-buffers/docs/encoding.
  67. *
  68. * @constructor
  69. * @struct
  70. */
  71. jspb.BinaryWriter = function() {
  72. /**
  73. * Blocks of serialized data that will be concatenated once all messages have
  74. * been written.
  75. * @private {!Array<!Uint8Array|!Array<number>>}
  76. */
  77. this.blocks_ = [];
  78. /**
  79. * Total number of bytes in the blocks_ array. Does _not_ include bytes in
  80. * the encoder below.
  81. * @private {number}
  82. */
  83. this.totalLength_ = 0;
  84. /**
  85. * Binary encoder holding pieces of a message that we're still serializing.
  86. * When we get to a stopping point (either the start of a new submessage, or
  87. * when we need to append a raw Uint8Array), the encoder's buffer will be
  88. * added to the block array above and the encoder will be reset.
  89. * @private {!jspb.BinaryEncoder}
  90. */
  91. this.encoder_ = new jspb.BinaryEncoder();
  92. /**
  93. * A stack of bookmarks containing the parent blocks for each message started
  94. * via beginSubMessage(), needed as bookkeeping for endSubMessage().
  95. * TODO(aappleby): Deprecated, users should be calling writeMessage().
  96. * @private {!Array<!Array<number>>}
  97. */
  98. this.bookmarks_ = [];
  99. };
  100. /**
  101. * Append a typed array of bytes onto the buffer.
  102. *
  103. * @param {!Uint8Array} arr The byte array to append.
  104. * @private
  105. */
  106. jspb.BinaryWriter.prototype.appendUint8Array_ = function(arr) {
  107. var temp = this.encoder_.end();
  108. this.blocks_.push(temp);
  109. this.blocks_.push(arr);
  110. this.totalLength_ += temp.length + arr.length;
  111. };
  112. /**
  113. * Begins a new message by writing the field header and returning a bookmark
  114. * which we will use to patch in the message length to in endDelimited_ below.
  115. * @param {number} field
  116. * @return {!Array<number>}
  117. * @private
  118. */
  119. jspb.BinaryWriter.prototype.beginDelimited_ = function(field) {
  120. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  121. var bookmark = this.encoder_.end();
  122. this.blocks_.push(bookmark);
  123. this.totalLength_ += bookmark.length;
  124. bookmark.push(this.totalLength_);
  125. return bookmark;
  126. };
  127. /**
  128. * Ends a message by encoding the _change_ in length of the buffer to the
  129. * parent block and adds the number of bytes needed to encode that length to
  130. * the total byte length.
  131. * @param {!Array<number>} bookmark
  132. * @private
  133. */
  134. jspb.BinaryWriter.prototype.endDelimited_ = function(bookmark) {
  135. var oldLength = bookmark.pop();
  136. var messageLength = this.totalLength_ + this.encoder_.length() - oldLength;
  137. goog.asserts.assert(messageLength >= 0);
  138. while (messageLength > 127) {
  139. bookmark.push((messageLength & 0x7f) | 0x80);
  140. messageLength = messageLength >>> 7;
  141. this.totalLength_++;
  142. }
  143. bookmark.push(messageLength);
  144. this.totalLength_++;
  145. };
  146. /**
  147. * Writes a pre-serialized message to the buffer.
  148. * @param {!Uint8Array} bytes The array of bytes to write.
  149. * @param {number} start The start of the range to write.
  150. * @param {number} end The end of the range to write.
  151. */
  152. jspb.BinaryWriter.prototype.writeSerializedMessage = function(
  153. bytes, start, end) {
  154. this.appendUint8Array_(bytes.subarray(start, end));
  155. };
  156. /**
  157. * Writes a pre-serialized message to the buffer if the message and endpoints
  158. * are non-null.
  159. * @param {?Uint8Array} bytes The array of bytes to write.
  160. * @param {?number} start The start of the range to write.
  161. * @param {?number} end The end of the range to write.
  162. */
  163. jspb.BinaryWriter.prototype.maybeWriteSerializedMessage = function(
  164. bytes, start, end) {
  165. if (bytes != null && start != null && end != null) {
  166. this.writeSerializedMessage(bytes, start, end);
  167. }
  168. };
  169. /**
  170. * Resets the writer, throwing away any accumulated buffers.
  171. */
  172. jspb.BinaryWriter.prototype.reset = function() {
  173. this.blocks_ = [];
  174. this.encoder_.end();
  175. this.totalLength_ = 0;
  176. this.bookmarks_ = [];
  177. };
  178. /**
  179. * Converts the encoded data into a Uint8Array.
  180. * @return {!Uint8Array}
  181. */
  182. jspb.BinaryWriter.prototype.getResultBuffer = function() {
  183. goog.asserts.assert(this.bookmarks_.length == 0);
  184. var flat = new Uint8Array(this.totalLength_ + this.encoder_.length());
  185. var blocks = this.blocks_;
  186. var blockCount = blocks.length;
  187. var offset = 0;
  188. for (var i = 0; i < blockCount; i++) {
  189. var block = blocks[i];
  190. flat.set(block, offset);
  191. offset += block.length;
  192. }
  193. var tail = this.encoder_.end();
  194. flat.set(tail, offset);
  195. offset += tail.length;
  196. // Post condition: `flattened` must have had every byte written.
  197. goog.asserts.assert(offset == flat.length);
  198. // Replace our block list with the flattened block, which lets GC reclaim
  199. // the temp blocks sooner.
  200. this.blocks_ = [flat];
  201. return flat;
  202. };
  203. /**
  204. * Converts the encoded data into a base64-encoded string.
  205. * @param {boolean=} opt_webSafe True indicates we should use a websafe
  206. * alphabet, which does not require escaping for use in URLs.
  207. * @return {string}
  208. */
  209. jspb.BinaryWriter.prototype.getResultBase64String = function(opt_webSafe) {
  210. return goog.crypt.base64.encodeByteArray(this.getResultBuffer(), opt_webSafe);
  211. };
  212. /**
  213. * Begins a new sub-message. The client must call endSubMessage() when they're
  214. * done.
  215. * TODO(aappleby): Deprecated. Move callers to writeMessage().
  216. * @param {number} field The field number of the sub-message.
  217. */
  218. jspb.BinaryWriter.prototype.beginSubMessage = function(field) {
  219. this.bookmarks_.push(this.beginDelimited_(field));
  220. };
  221. /**
  222. * Finishes a sub-message and packs it into the parent messages' buffer.
  223. * TODO(aappleby): Deprecated. Move callers to writeMessage().
  224. */
  225. jspb.BinaryWriter.prototype.endSubMessage = function() {
  226. goog.asserts.assert(this.bookmarks_.length >= 0);
  227. this.endDelimited_(this.bookmarks_.pop());
  228. };
  229. /**
  230. * Encodes a (field number, wire type) tuple into a wire-format field header
  231. * and stores it in the buffer as a varint.
  232. * @param {number} field The field number.
  233. * @param {number} wireType The wire-type of the field, as specified in the
  234. * protocol buffer documentation.
  235. * @private
  236. */
  237. jspb.BinaryWriter.prototype.writeFieldHeader_ =
  238. function(field, wireType) {
  239. goog.asserts.assert(field >= 1 && field == Math.floor(field));
  240. var x = field * 8 + wireType;
  241. this.encoder_.writeUnsignedVarint32(x);
  242. };
  243. /**
  244. * Writes a field of any valid scalar type to the binary stream.
  245. * @param {jspb.BinaryConstants.FieldType} fieldType
  246. * @param {number} field
  247. * @param {jspb.AnyFieldType} value
  248. */
  249. jspb.BinaryWriter.prototype.writeAny = function(fieldType, field, value) {
  250. var fieldTypes = jspb.BinaryConstants.FieldType;
  251. switch (fieldType) {
  252. case fieldTypes.DOUBLE:
  253. this.writeDouble(field, /** @type {number} */(value));
  254. return;
  255. case fieldTypes.FLOAT:
  256. this.writeFloat(field, /** @type {number} */(value));
  257. return;
  258. case fieldTypes.INT64:
  259. this.writeInt64(field, /** @type {number} */(value));
  260. return;
  261. case fieldTypes.UINT64:
  262. this.writeUint64(field, /** @type {number} */(value));
  263. return;
  264. case fieldTypes.INT32:
  265. this.writeInt32(field, /** @type {number} */(value));
  266. return;
  267. case fieldTypes.FIXED64:
  268. this.writeFixed64(field, /** @type {number} */(value));
  269. return;
  270. case fieldTypes.FIXED32:
  271. this.writeFixed32(field, /** @type {number} */(value));
  272. return;
  273. case fieldTypes.BOOL:
  274. this.writeBool(field, /** @type {boolean} */(value));
  275. return;
  276. case fieldTypes.STRING:
  277. this.writeString(field, /** @type {string} */(value));
  278. return;
  279. case fieldTypes.GROUP:
  280. goog.asserts.fail('Group field type not supported in writeAny()');
  281. return;
  282. case fieldTypes.MESSAGE:
  283. goog.asserts.fail('Message field type not supported in writeAny()');
  284. return;
  285. case fieldTypes.BYTES:
  286. this.writeBytes(field, /** @type {?Uint8Array} */(value));
  287. return;
  288. case fieldTypes.UINT32:
  289. this.writeUint32(field, /** @type {number} */(value));
  290. return;
  291. case fieldTypes.ENUM:
  292. this.writeEnum(field, /** @type {number} */(value));
  293. return;
  294. case fieldTypes.SFIXED32:
  295. this.writeSfixed32(field, /** @type {number} */(value));
  296. return;
  297. case fieldTypes.SFIXED64:
  298. this.writeSfixed64(field, /** @type {number} */(value));
  299. return;
  300. case fieldTypes.SINT32:
  301. this.writeSint32(field, /** @type {number} */(value));
  302. return;
  303. case fieldTypes.SINT64:
  304. this.writeSint64(field, /** @type {number} */(value));
  305. return;
  306. case fieldTypes.FHASH64:
  307. this.writeFixedHash64(field, /** @type {string} */(value));
  308. return;
  309. case fieldTypes.VHASH64:
  310. this.writeVarintHash64(field, /** @type {string} */(value));
  311. return;
  312. default:
  313. goog.asserts.fail('Invalid field type in writeAny()');
  314. return;
  315. }
  316. };
  317. /**
  318. * Writes a varint field to the buffer without range checking.
  319. * @param {number} field The field number.
  320. * @param {number?} value The value to write.
  321. * @private
  322. */
  323. jspb.BinaryWriter.prototype.writeUnsignedVarint32_ = function(field, value) {
  324. if (value == null) return;
  325. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  326. this.encoder_.writeUnsignedVarint32(value);
  327. };
  328. /**
  329. * Writes a varint field to the buffer without range checking.
  330. * @param {number} field The field number.
  331. * @param {number?} value The value to write.
  332. * @private
  333. */
  334. jspb.BinaryWriter.prototype.writeSignedVarint32_ = function(field, value) {
  335. if (value == null) return;
  336. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  337. this.encoder_.writeSignedVarint32(value);
  338. };
  339. /**
  340. * Writes a varint field to the buffer without range checking.
  341. * @param {number} field The field number.
  342. * @param {number?} value The value to write.
  343. * @private
  344. */
  345. jspb.BinaryWriter.prototype.writeUnsignedVarint64_ = function(field, value) {
  346. if (value == null) return;
  347. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  348. this.encoder_.writeUnsignedVarint64(value);
  349. };
  350. /**
  351. * Writes a varint field to the buffer without range checking.
  352. * @param {number} field The field number.
  353. * @param {number?} value The value to write.
  354. * @private
  355. */
  356. jspb.BinaryWriter.prototype.writeSignedVarint64_ = function(field, value) {
  357. if (value == null) return;
  358. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  359. this.encoder_.writeSignedVarint64(value);
  360. };
  361. /**
  362. * Writes a zigzag varint field to the buffer without range checking.
  363. * @param {number} field The field number.
  364. * @param {number?} value The value to write.
  365. * @private
  366. */
  367. jspb.BinaryWriter.prototype.writeZigzagVarint32_ = function(field, value) {
  368. if (value == null) return;
  369. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  370. this.encoder_.writeZigzagVarint32(value);
  371. };
  372. /**
  373. * Writes a zigzag varint field to the buffer without range checking.
  374. * @param {number} field The field number.
  375. * @param {number?} value The value to write.
  376. * @private
  377. */
  378. jspb.BinaryWriter.prototype.writeZigzagVarint64_ = function(field, value) {
  379. if (value == null) return;
  380. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  381. this.encoder_.writeZigzagVarint64(value);
  382. };
  383. /**
  384. * Writes a zigzag varint field to the buffer without range checking.
  385. * @param {number} field The field number.
  386. * @param {string?} value The value to write.
  387. * @private
  388. */
  389. jspb.BinaryWriter.prototype.writeZigzagVarint64String_ = function(
  390. field, value) {
  391. if (value == null) return;
  392. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  393. this.encoder_.writeZigzagVarint64String(value);
  394. };
  395. /**
  396. * Writes an int32 field to the buffer. Numbers outside the range [-2^31,2^31)
  397. * will be truncated.
  398. * @param {number} field The field number.
  399. * @param {number?} value The value to write.
  400. */
  401. jspb.BinaryWriter.prototype.writeInt32 = function(field, value) {
  402. if (value == null) return;
  403. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
  404. (value < jspb.BinaryConstants.TWO_TO_31));
  405. this.writeSignedVarint32_(field, value);
  406. };
  407. /**
  408. * Writes an int32 field represented as a string to the buffer. Numbers outside
  409. * the range [-2^31,2^31) will be truncated.
  410. * @param {number} field The field number.
  411. * @param {string?} value The value to write.
  412. */
  413. jspb.BinaryWriter.prototype.writeInt32String = function(field, value) {
  414. if (value == null) return;
  415. var intValue = /** {number} */ parseInt(value, 10);
  416. goog.asserts.assert((intValue >= -jspb.BinaryConstants.TWO_TO_31) &&
  417. (intValue < jspb.BinaryConstants.TWO_TO_31));
  418. this.writeSignedVarint32_(field, intValue);
  419. };
  420. /**
  421. * Writes an int64 field to the buffer. Numbers outside the range [-2^63,2^63)
  422. * will be truncated.
  423. * @param {number} field The field number.
  424. * @param {number?} value The value to write.
  425. */
  426. jspb.BinaryWriter.prototype.writeInt64 = function(field, value) {
  427. if (value == null) return;
  428. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
  429. (value < jspb.BinaryConstants.TWO_TO_63));
  430. this.writeSignedVarint64_(field, value);
  431. };
  432. /**
  433. * Writes a int64 field (with value as a string) to the buffer.
  434. * @param {number} field The field number.
  435. * @param {string?} value The value to write.
  436. */
  437. jspb.BinaryWriter.prototype.writeInt64String = function(field, value) {
  438. if (value == null) return;
  439. var num = jspb.arith.Int64.fromString(value);
  440. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  441. this.encoder_.writeSplitVarint64(num.lo, num.hi);
  442. };
  443. /**
  444. * Writes a uint32 field to the buffer. Numbers outside the range [0,2^32)
  445. * will be truncated.
  446. * @param {number} field The field number.
  447. * @param {number?} value The value to write.
  448. */
  449. jspb.BinaryWriter.prototype.writeUint32 = function(field, value) {
  450. if (value == null) return;
  451. goog.asserts.assert((value >= 0) &&
  452. (value < jspb.BinaryConstants.TWO_TO_32));
  453. this.writeUnsignedVarint32_(field, value);
  454. };
  455. /**
  456. * Writes a uint32 field represented as a string to the buffer. Numbers outside
  457. * the range [0,2^32) will be truncated.
  458. * @param {number} field The field number.
  459. * @param {string?} value The value to write.
  460. */
  461. jspb.BinaryWriter.prototype.writeUint32String = function(field, value) {
  462. if (value == null) return;
  463. var intValue = /** {number} */ parseInt(value, 10);
  464. goog.asserts.assert((intValue >= 0) &&
  465. (intValue < jspb.BinaryConstants.TWO_TO_32));
  466. this.writeUnsignedVarint32_(field, intValue);
  467. };
  468. /**
  469. * Writes a uint64 field to the buffer. Numbers outside the range [0,2^64)
  470. * will be truncated.
  471. * @param {number} field The field number.
  472. * @param {number?} value The value to write.
  473. */
  474. jspb.BinaryWriter.prototype.writeUint64 = function(field, value) {
  475. if (value == null) return;
  476. goog.asserts.assert((value >= 0) &&
  477. (value < jspb.BinaryConstants.TWO_TO_64));
  478. this.writeUnsignedVarint64_(field, value);
  479. };
  480. /**
  481. * Writes a uint64 field (with value as a string) to the buffer.
  482. * @param {number} field The field number.
  483. * @param {string?} value The value to write.
  484. */
  485. jspb.BinaryWriter.prototype.writeUint64String = function(field, value) {
  486. if (value == null) return;
  487. var num = jspb.arith.UInt64.fromString(value);
  488. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  489. this.encoder_.writeSplitVarint64(num.lo, num.hi);
  490. };
  491. /**
  492. * Writes a sint32 field to the buffer. Numbers outside the range [-2^31,2^31)
  493. * will be truncated.
  494. * @param {number} field The field number.
  495. * @param {number?} value The value to write.
  496. */
  497. jspb.BinaryWriter.prototype.writeSint32 = function(field, value) {
  498. if (value == null) return;
  499. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
  500. (value < jspb.BinaryConstants.TWO_TO_31));
  501. this.writeZigzagVarint32_(field, value);
  502. };
  503. /**
  504. * Writes a sint64 field to the buffer. Numbers outside the range [-2^63,2^63)
  505. * will be truncated.
  506. * @param {number} field The field number.
  507. * @param {number?} value The value to write.
  508. */
  509. jspb.BinaryWriter.prototype.writeSint64 = function(field, value) {
  510. if (value == null) return;
  511. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
  512. (value < jspb.BinaryConstants.TWO_TO_63));
  513. this.writeZigzagVarint64_(field, value);
  514. };
  515. /**
  516. * Writes a sint64 field to the buffer. Numbers outside the range [-2^63,2^63)
  517. * will be truncated.
  518. * @param {number} field The field number.
  519. * @param {string?} value The decimal string to write.
  520. */
  521. jspb.BinaryWriter.prototype.writeSint64String = function(field, value) {
  522. if (value == null) return;
  523. goog.asserts.assert((+value >= -jspb.BinaryConstants.TWO_TO_63) &&
  524. (+value < jspb.BinaryConstants.TWO_TO_63));
  525. this.writeZigzagVarint64String_(field, value);
  526. };
  527. /**
  528. * Writes a fixed32 field to the buffer. Numbers outside the range [0,2^32)
  529. * will be truncated.
  530. * @param {number} field The field number.
  531. * @param {number?} value The value to write.
  532. */
  533. jspb.BinaryWriter.prototype.writeFixed32 = function(field, value) {
  534. if (value == null) return;
  535. goog.asserts.assert((value >= 0) &&
  536. (value < jspb.BinaryConstants.TWO_TO_32));
  537. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32);
  538. this.encoder_.writeUint32(value);
  539. };
  540. /**
  541. * Writes a fixed64 field to the buffer. Numbers outside the range [0,2^64)
  542. * will be truncated.
  543. * @param {number} field The field number.
  544. * @param {number?} value The value to write.
  545. */
  546. jspb.BinaryWriter.prototype.writeFixed64 = function(field, value) {
  547. if (value == null) return;
  548. goog.asserts.assert((value >= 0) &&
  549. (value < jspb.BinaryConstants.TWO_TO_64));
  550. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  551. this.encoder_.writeUint64(value);
  552. };
  553. /**
  554. * Writes a fixed64 field (with value as a string) to the buffer.
  555. * @param {number} field The field number.
  556. * @param {string?} value The value to write.
  557. */
  558. jspb.BinaryWriter.prototype.writeFixed64String = function(field, value) {
  559. if (value == null) return;
  560. var num = jspb.arith.UInt64.fromString(value);
  561. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  562. this.encoder_.writeSplitFixed64(num.lo, num.hi);
  563. };
  564. /**
  565. * Writes a sfixed32 field to the buffer. Numbers outside the range
  566. * [-2^31,2^31) will be truncated.
  567. * @param {number} field The field number.
  568. * @param {number?} value The value to write.
  569. */
  570. jspb.BinaryWriter.prototype.writeSfixed32 = function(field, value) {
  571. if (value == null) return;
  572. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
  573. (value < jspb.BinaryConstants.TWO_TO_31));
  574. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32);
  575. this.encoder_.writeInt32(value);
  576. };
  577. /**
  578. * Writes a sfixed64 field to the buffer. Numbers outside the range
  579. * [-2^63,2^63) will be truncated.
  580. * @param {number} field The field number.
  581. * @param {number?} value The value to write.
  582. */
  583. jspb.BinaryWriter.prototype.writeSfixed64 = function(field, value) {
  584. if (value == null) return;
  585. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
  586. (value < jspb.BinaryConstants.TWO_TO_63));
  587. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  588. this.encoder_.writeInt64(value);
  589. };
  590. /**
  591. * Writes a sfixed64 string field to the buffer. Numbers outside the range
  592. * [-2^63,2^63) will be truncated.
  593. * @param {number} field The field number.
  594. * @param {string?} value The value to write.
  595. */
  596. jspb.BinaryWriter.prototype.writeSfixed64String = function(field, value) {
  597. if (value == null) return;
  598. var num = jspb.arith.Int64.fromString(value);
  599. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  600. this.encoder_.writeSplitFixed64(num.lo, num.hi);
  601. };
  602. /**
  603. * Writes a single-precision floating point field to the buffer. Numbers
  604. * requiring more than 32 bits of precision will be truncated.
  605. * @param {number} field The field number.
  606. * @param {number?} value The value to write.
  607. */
  608. jspb.BinaryWriter.prototype.writeFloat = function(field, value) {
  609. if (value == null) return;
  610. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED32);
  611. this.encoder_.writeFloat(value);
  612. };
  613. /**
  614. * Writes a double-precision floating point field to the buffer. As this is the
  615. * native format used by JavaScript, no precision will be lost.
  616. * @param {number} field The field number.
  617. * @param {number?} value The value to write.
  618. */
  619. jspb.BinaryWriter.prototype.writeDouble = function(field, value) {
  620. if (value == null) return;
  621. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  622. this.encoder_.writeDouble(value);
  623. };
  624. /**
  625. * Writes a boolean field to the buffer. We allow numbers as input
  626. * because the JSPB code generator uses 0/1 instead of true/false to save space
  627. * in the string representation of the proto.
  628. * @param {number} field The field number.
  629. * @param {boolean?|number?} value The value to write.
  630. */
  631. jspb.BinaryWriter.prototype.writeBool = function(field, value) {
  632. if (value == null) return;
  633. goog.asserts.assert(goog.isBoolean(value) || goog.isNumber(value));
  634. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  635. this.encoder_.writeBool(value);
  636. };
  637. /**
  638. * Writes an enum field to the buffer.
  639. * @param {number} field The field number.
  640. * @param {number?} value The value to write.
  641. */
  642. jspb.BinaryWriter.prototype.writeEnum = function(field, value) {
  643. if (value == null) return;
  644. goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
  645. (value < jspb.BinaryConstants.TWO_TO_31));
  646. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  647. this.encoder_.writeSignedVarint32(value);
  648. };
  649. /**
  650. * Writes a string field to the buffer.
  651. * @param {number} field The field number.
  652. * @param {string?} value The string to write.
  653. */
  654. jspb.BinaryWriter.prototype.writeString = function(field, value) {
  655. if (value == null) return;
  656. var bookmark = this.beginDelimited_(field);
  657. this.encoder_.writeString(value);
  658. this.endDelimited_(bookmark);
  659. };
  660. /**
  661. * Writes an arbitrary byte field to the buffer. Note - to match the behavior
  662. * of the C++ implementation, empty byte arrays _are_ serialized.
  663. * @param {number} field The field number.
  664. * @param {?jspb.ByteSource} value The array of bytes to write.
  665. */
  666. jspb.BinaryWriter.prototype.writeBytes = function(field, value) {
  667. if (value == null) return;
  668. var bytes = jspb.utils.byteSourceToUint8Array(value);
  669. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  670. this.encoder_.writeUnsignedVarint32(bytes.length);
  671. this.appendUint8Array_(bytes);
  672. };
  673. /**
  674. * Writes a message to the buffer.
  675. * @param {number} field The field number.
  676. * @param {?MessageType} value The message to write.
  677. * @param {function(MessageTypeNonNull, !jspb.BinaryWriter)} writerCallback
  678. * Will be invoked with the value to write and the writer to write it with.
  679. * @template MessageType
  680. * Use go/closure-ttl to declare a non-nullable version of MessageType. Replace
  681. * the null in blah|null with none. This is necessary because the compiler will
  682. * infer MessageType to be nullable if the value parameter is nullable.
  683. * @template MessageTypeNonNull :=
  684. * cond(isUnknown(MessageType), unknown(),
  685. * mapunion(MessageType, (X) =>
  686. * cond(eq(X, 'null'), none(), X)))
  687. * =:
  688. */
  689. jspb.BinaryWriter.prototype.writeMessage = function(
  690. field, value, writerCallback) {
  691. if (value == null) return;
  692. var bookmark = this.beginDelimited_(field);
  693. writerCallback(value, this);
  694. this.endDelimited_(bookmark);
  695. };
  696. /**
  697. * Writes a message set extension to the buffer.
  698. * @param {number} field The field number for the extension.
  699. * @param {?MessageType} value The extension message object to write. Note that
  700. * message set can only have extensions with type of optional message.
  701. * @param {function(!MessageTypeNonNull, !jspb.BinaryWriter)} writerCallback
  702. * Will be invoked with the value to write and the writer to write it with.
  703. * @template MessageType
  704. * Use go/closure-ttl to declare a non-nullable version of MessageType. Replace
  705. * the null in blah|null with none. This is necessary because the compiler will
  706. * infer MessageType to be nullable if the value parameter is nullable.
  707. * @template MessageTypeNonNull :=
  708. * cond(isUnknown(MessageType), unknown(),
  709. * mapunion(MessageType, (X) =>
  710. * cond(eq(X, 'null'), none(), X)))
  711. * =:
  712. */
  713. jspb.BinaryWriter.prototype.writeMessageSet = function(
  714. field, value, writerCallback) {
  715. if (value == null) return;
  716. // The wire format for a message set is defined by
  717. // google3/net/proto/message_set.proto
  718. this.writeFieldHeader_(1, jspb.BinaryConstants.WireType.START_GROUP);
  719. this.writeFieldHeader_(2, jspb.BinaryConstants.WireType.VARINT);
  720. this.encoder_.writeSignedVarint32(field);
  721. var bookmark = this.beginDelimited_(3);
  722. writerCallback(value, this);
  723. this.endDelimited_(bookmark);
  724. this.writeFieldHeader_(1, jspb.BinaryConstants.WireType.END_GROUP);
  725. };
  726. /**
  727. * Writes a group message to the buffer.
  728. *
  729. * @param {number} field The field number.
  730. * @param {?MessageType} value The message to write, wrapped with START_GROUP /
  731. * END_GROUP tags. Will be a no-op if 'value' is null.
  732. * @param {function(MessageTypeNonNull, !jspb.BinaryWriter)} writerCallback
  733. * Will be invoked with the value to write and the writer to write it with.
  734. * @template MessageType
  735. * Use go/closure-ttl to declare a non-nullable version of MessageType. Replace
  736. * the null in blah|null with none. This is necessary because the compiler will
  737. * infer MessageType to be nullable if the value parameter is nullable.
  738. * @template MessageTypeNonNull :=
  739. * cond(isUnknown(MessageType), unknown(),
  740. * mapunion(MessageType, (X) =>
  741. * cond(eq(X, 'null'), none(), X)))
  742. * =:
  743. */
  744. jspb.BinaryWriter.prototype.writeGroup = function(
  745. field, value, writerCallback) {
  746. if (value == null) return;
  747. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.START_GROUP);
  748. writerCallback(value, this);
  749. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.END_GROUP);
  750. };
  751. /**
  752. * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
  753. * the buffer.
  754. * @param {number} field The field number.
  755. * @param {string?} value The hash string.
  756. */
  757. jspb.BinaryWriter.prototype.writeFixedHash64 = function(field, value) {
  758. if (value == null) return;
  759. goog.asserts.assert(value.length == 8);
  760. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  761. this.encoder_.writeFixedHash64(value);
  762. };
  763. /**
  764. * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
  765. * the buffer.
  766. * @param {number} field The field number.
  767. * @param {string?} value The hash string.
  768. */
  769. jspb.BinaryWriter.prototype.writeVarintHash64 = function(field, value) {
  770. if (value == null) return;
  771. goog.asserts.assert(value.length == 8);
  772. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  773. this.encoder_.writeVarintHash64(value);
  774. };
  775. /**
  776. * Writes a 64-bit field to the buffer as a fixed64.
  777. * @param {number} field The field number.
  778. * @param {number} lowBits The low 32 bits.
  779. * @param {number} highBits The high 32 bits.
  780. */
  781. jspb.BinaryWriter.prototype.writeSplitFixed64 = function(
  782. field, lowBits, highBits) {
  783. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64);
  784. this.encoder_.writeSplitFixed64(lowBits, highBits);
  785. };
  786. /**
  787. * Writes a 64-bit field to the buffer as a varint.
  788. * @param {number} field The field number.
  789. * @param {number} lowBits The low 32 bits.
  790. * @param {number} highBits The high 32 bits.
  791. */
  792. jspb.BinaryWriter.prototype.writeSplitVarint64 = function(
  793. field, lowBits, highBits) {
  794. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT);
  795. this.encoder_.writeSplitVarint64(lowBits, highBits);
  796. };
  797. /**
  798. * Writes an array of numbers to the buffer as a repeated 32-bit int field.
  799. * @param {number} field The field number.
  800. * @param {?Array<number>} value The array of ints to write.
  801. */
  802. jspb.BinaryWriter.prototype.writeRepeatedInt32 = function(field, value) {
  803. if (value == null) return;
  804. for (var i = 0; i < value.length; i++) {
  805. this.writeSignedVarint32_(field, value[i]);
  806. }
  807. };
  808. /**
  809. * Writes an array of numbers formatted as strings to the buffer as a repeated
  810. * 32-bit int field.
  811. * @param {number} field The field number.
  812. * @param {?Array<string>} value The array of ints to write.
  813. */
  814. jspb.BinaryWriter.prototype.writeRepeatedInt32String = function(field, value) {
  815. if (value == null) return;
  816. for (var i = 0; i < value.length; i++) {
  817. this.writeInt32String(field, value[i]);
  818. }
  819. };
  820. /**
  821. * Writes an array of numbers to the buffer as a repeated 64-bit int field.
  822. * @param {number} field The field number.
  823. * @param {?Array<number>} value The array of ints to write.
  824. */
  825. jspb.BinaryWriter.prototype.writeRepeatedInt64 = function(field, value) {
  826. if (value == null) return;
  827. for (var i = 0; i < value.length; i++) {
  828. this.writeSignedVarint64_(field, value[i]);
  829. }
  830. };
  831. /**
  832. * Writes an array of 64-bit values to the buffer as a fixed64.
  833. * @param {number} field The field number.
  834. * @param {?Array<T>} value The value.
  835. * @param {function(T): number} lo Function to get low bits.
  836. * @param {function(T): number} hi Function to get high bits.
  837. * @template T
  838. */
  839. jspb.BinaryWriter.prototype.writeRepeatedSplitFixed64 = function(
  840. field, value, lo, hi) {
  841. if (value == null) return;
  842. for (var i = 0; i < value.length; i++) {
  843. this.writeSplitFixed64(field, lo(value[i]), hi(value[i]));
  844. }
  845. };
  846. /**
  847. * Writes an array of 64-bit values to the buffer as a varint.
  848. * @param {number} field The field number.
  849. * @param {?Array<T>} value The value.
  850. * @param {function(T): number} lo Function to get low bits.
  851. * @param {function(T): number} hi Function to get high bits.
  852. * @template T
  853. */
  854. jspb.BinaryWriter.prototype.writeRepeatedSplitVarint64 = function(
  855. field, value, lo, hi) {
  856. if (value == null) return;
  857. for (var i = 0; i < value.length; i++) {
  858. this.writeSplitVarint64(field, lo(value[i]), hi(value[i]));
  859. }
  860. };
  861. /**
  862. * Writes an array of numbers formatted as strings to the buffer as a repeated
  863. * 64-bit int field.
  864. * @param {number} field The field number.
  865. * @param {?Array<string>} value The array of ints to write.
  866. */
  867. jspb.BinaryWriter.prototype.writeRepeatedInt64String = function(field, value) {
  868. if (value == null) return;
  869. for (var i = 0; i < value.length; i++) {
  870. this.writeInt64String(field, value[i]);
  871. }
  872. };
  873. /**
  874. * Writes an array numbers to the buffer as a repeated unsigned 32-bit int
  875. * field.
  876. * @param {number} field The field number.
  877. * @param {?Array<number>} value The array of ints to write.
  878. */
  879. jspb.BinaryWriter.prototype.writeRepeatedUint32 = function(field, value) {
  880. if (value == null) return;
  881. for (var i = 0; i < value.length; i++) {
  882. this.writeUnsignedVarint32_(field, value[i]);
  883. }
  884. };
  885. /**
  886. * Writes an array of numbers formatted as strings to the buffer as a repeated
  887. * unsigned 32-bit int field.
  888. * @param {number} field The field number.
  889. * @param {?Array<string>} value The array of ints to write.
  890. */
  891. jspb.BinaryWriter.prototype.writeRepeatedUint32String = function(field, value) {
  892. if (value == null) return;
  893. for (var i = 0; i < value.length; i++) {
  894. this.writeUint32String(field, value[i]);
  895. }
  896. };
  897. /**
  898. * Writes an array numbers to the buffer as a repeated unsigned 64-bit int
  899. * field.
  900. * @param {number} field The field number.
  901. * @param {?Array<number>} value The array of ints to write.
  902. */
  903. jspb.BinaryWriter.prototype.writeRepeatedUint64 = function(field, value) {
  904. if (value == null) return;
  905. for (var i = 0; i < value.length; i++) {
  906. this.writeUnsignedVarint64_(field, value[i]);
  907. }
  908. };
  909. /**
  910. * Writes an array of numbers formatted as strings to the buffer as a repeated
  911. * unsigned 64-bit int field.
  912. * @param {number} field The field number.
  913. * @param {?Array<string>} value The array of ints to write.
  914. */
  915. jspb.BinaryWriter.prototype.writeRepeatedUint64String = function(field, value) {
  916. if (value == null) return;
  917. for (var i = 0; i < value.length; i++) {
  918. this.writeUint64String(field, value[i]);
  919. }
  920. };
  921. /**
  922. * Writes an array numbers to the buffer as a repeated signed 32-bit int field.
  923. * @param {number} field The field number.
  924. * @param {?Array<number>} value The array of ints to write.
  925. */
  926. jspb.BinaryWriter.prototype.writeRepeatedSint32 = function(field, value) {
  927. if (value == null) return;
  928. for (var i = 0; i < value.length; i++) {
  929. this.writeZigzagVarint32_(field, value[i]);
  930. }
  931. };
  932. /**
  933. * Writes an array numbers to the buffer as a repeated signed 64-bit int field.
  934. * @param {number} field The field number.
  935. * @param {?Array<number>} value The array of ints to write.
  936. */
  937. jspb.BinaryWriter.prototype.writeRepeatedSint64 = function(field, value) {
  938. if (value == null) return;
  939. for (var i = 0; i < value.length; i++) {
  940. this.writeZigzagVarint64_(field, value[i]);
  941. }
  942. };
  943. /**
  944. * Writes an array numbers to the buffer as a repeated signed 64-bit int field.
  945. * @param {number} field The field number.
  946. * @param {?Array<string>} value The array of ints to write.
  947. */
  948. jspb.BinaryWriter.prototype.writeRepeatedSint64String = function(field, value) {
  949. if (value == null) return;
  950. for (var i = 0; i < value.length; i++) {
  951. this.writeZigzagVarint64String_(field, value[i]);
  952. }
  953. };
  954. /**
  955. * Writes an array of numbers to the buffer as a repeated fixed32 field. This
  956. * works for both signed and unsigned fixed32s.
  957. * @param {number} field The field number.
  958. * @param {?Array<number>} value The array of ints to write.
  959. */
  960. jspb.BinaryWriter.prototype.writeRepeatedFixed32 = function(field, value) {
  961. if (value == null) return;
  962. for (var i = 0; i < value.length; i++) {
  963. this.writeFixed32(field, value[i]);
  964. }
  965. };
  966. /**
  967. * Writes an array of numbers to the buffer as a repeated fixed64 field. This
  968. * works for both signed and unsigned fixed64s.
  969. * @param {number} field The field number.
  970. * @param {?Array<number>} value The array of ints to write.
  971. */
  972. jspb.BinaryWriter.prototype.writeRepeatedFixed64 = function(field, value) {
  973. if (value == null) return;
  974. for (var i = 0; i < value.length; i++) {
  975. this.writeFixed64(field, value[i]);
  976. }
  977. };
  978. /**
  979. * Writes an array of numbers to the buffer as a repeated fixed64 field. This
  980. * works for both signed and unsigned fixed64s.
  981. * @param {number} field The field number.
  982. * @param {?Array<string>} value The array of decimal strings to write.
  983. */
  984. jspb.BinaryWriter.prototype.writeRepeatedFixed64String = function(
  985. field, value) {
  986. if (value == null) return;
  987. for (var i = 0; i < value.length; i++) {
  988. this.writeFixed64String(field, value[i]);
  989. }
  990. };
  991. /**
  992. * Writes an array of numbers to the buffer as a repeated sfixed32 field.
  993. * @param {number} field The field number.
  994. * @param {?Array<number>} value The array of ints to write.
  995. */
  996. jspb.BinaryWriter.prototype.writeRepeatedSfixed32 = function(field, value) {
  997. if (value == null) return;
  998. for (var i = 0; i < value.length; i++) {
  999. this.writeSfixed32(field, value[i]);
  1000. }
  1001. };
  1002. /**
  1003. * Writes an array of numbers to the buffer as a repeated sfixed64 field.
  1004. * @param {number} field The field number.
  1005. * @param {?Array<number>} value The array of ints to write.
  1006. */
  1007. jspb.BinaryWriter.prototype.writeRepeatedSfixed64 = function(field, value) {
  1008. if (value == null) return;
  1009. for (var i = 0; i < value.length; i++) {
  1010. this.writeSfixed64(field, value[i]);
  1011. }
  1012. };
  1013. /**
  1014. * Writes an array of decimal strings to the buffer as a repeated sfixed64
  1015. * field.
  1016. * @param {number} field The field number.
  1017. * @param {?Array<string>} value The array of decimal strings to write.
  1018. */
  1019. jspb.BinaryWriter.prototype.writeRepeatedSfixed64String = function(field, value) {
  1020. if (value == null) return;
  1021. for (var i = 0; i < value.length; i++) {
  1022. this.writeSfixed64String(field, value[i]);
  1023. }
  1024. };
  1025. /**
  1026. * Writes an array of numbers to the buffer as a repeated float field.
  1027. * @param {number} field The field number.
  1028. * @param {?Array<number>} value The array of ints to write.
  1029. */
  1030. jspb.BinaryWriter.prototype.writeRepeatedFloat = function(field, value) {
  1031. if (value == null) return;
  1032. for (var i = 0; i < value.length; i++) {
  1033. this.writeFloat(field, value[i]);
  1034. }
  1035. };
  1036. /**
  1037. * Writes an array of numbers to the buffer as a repeated double field.
  1038. * @param {number} field The field number.
  1039. * @param {?Array<number>} value The array of ints to write.
  1040. */
  1041. jspb.BinaryWriter.prototype.writeRepeatedDouble = function(field, value) {
  1042. if (value == null) return;
  1043. for (var i = 0; i < value.length; i++) {
  1044. this.writeDouble(field, value[i]);
  1045. }
  1046. };
  1047. /**
  1048. * Writes an array of booleans to the buffer as a repeated bool field.
  1049. * @param {number} field The field number.
  1050. * @param {?Array<boolean>} value The array of ints to write.
  1051. */
  1052. jspb.BinaryWriter.prototype.writeRepeatedBool = function(field, value) {
  1053. if (value == null) return;
  1054. for (var i = 0; i < value.length; i++) {
  1055. this.writeBool(field, value[i]);
  1056. }
  1057. };
  1058. /**
  1059. * Writes an array of enums to the buffer as a repeated enum field.
  1060. * @param {number} field The field number.
  1061. * @param {?Array<number>} value The array of ints to write.
  1062. */
  1063. jspb.BinaryWriter.prototype.writeRepeatedEnum = function(field, value) {
  1064. if (value == null) return;
  1065. for (var i = 0; i < value.length; i++) {
  1066. this.writeEnum(field, value[i]);
  1067. }
  1068. };
  1069. /**
  1070. * Writes an array of strings to the buffer as a repeated string field.
  1071. * @param {number} field The field number.
  1072. * @param {?Array<string>} value The array of strings to write.
  1073. */
  1074. jspb.BinaryWriter.prototype.writeRepeatedString = function(field, value) {
  1075. if (value == null) return;
  1076. for (var i = 0; i < value.length; i++) {
  1077. this.writeString(field, value[i]);
  1078. }
  1079. };
  1080. /**
  1081. * Writes an array of arbitrary byte fields to the buffer.
  1082. * @param {number} field The field number.
  1083. * @param {?Array<!jspb.ByteSource>} value The arrays of arrays of bytes to
  1084. * write.
  1085. */
  1086. jspb.BinaryWriter.prototype.writeRepeatedBytes = function(field, value) {
  1087. if (value == null) return;
  1088. for (var i = 0; i < value.length; i++) {
  1089. this.writeBytes(field, value[i]);
  1090. }
  1091. };
  1092. /**
  1093. * Writes an array of messages to the buffer.
  1094. * @template MessageType
  1095. * @param {number} field The field number.
  1096. * @param {?Array<MessageType>} value The array of messages to
  1097. * write.
  1098. * @param {function(MessageType, !jspb.BinaryWriter)} writerCallback
  1099. * Will be invoked with the value to write and the writer to write it with.
  1100. */
  1101. jspb.BinaryWriter.prototype.writeRepeatedMessage = function(
  1102. field, value, writerCallback) {
  1103. if (value == null) return;
  1104. for (var i = 0; i < value.length; i++) {
  1105. var bookmark = this.beginDelimited_(field);
  1106. writerCallback(value[i], this);
  1107. this.endDelimited_(bookmark);
  1108. }
  1109. };
  1110. /**
  1111. * Writes an array of group messages to the buffer.
  1112. * @template MessageType
  1113. * @param {number} field The field number.
  1114. * @param {?Array<MessageType>} value The array of messages to
  1115. * write.
  1116. * @param {function(MessageType, !jspb.BinaryWriter)} writerCallback
  1117. * Will be invoked with the value to write and the writer to write it with.
  1118. */
  1119. jspb.BinaryWriter.prototype.writeRepeatedGroup = function(
  1120. field, value, writerCallback) {
  1121. if (value == null) return;
  1122. for (var i = 0; i < value.length; i++) {
  1123. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.START_GROUP);
  1124. writerCallback(value[i], this);
  1125. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.END_GROUP);
  1126. }
  1127. };
  1128. /**
  1129. * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
  1130. * the buffer.
  1131. * @param {number} field The field number.
  1132. * @param {?Array<string>} value The array of hashes to write.
  1133. */
  1134. jspb.BinaryWriter.prototype.writeRepeatedFixedHash64 =
  1135. function(field, value) {
  1136. if (value == null) return;
  1137. for (var i = 0; i < value.length; i++) {
  1138. this.writeFixedHash64(field, value[i]);
  1139. }
  1140. };
  1141. /**
  1142. * Writes a repeated 64-bit hash string field (8 characters @ 8 bits of data
  1143. * each) to the buffer.
  1144. * @param {number} field The field number.
  1145. * @param {?Array<string>} value The array of hashes to write.
  1146. */
  1147. jspb.BinaryWriter.prototype.writeRepeatedVarintHash64 =
  1148. function(field, value) {
  1149. if (value == null) return;
  1150. for (var i = 0; i < value.length; i++) {
  1151. this.writeVarintHash64(field, value[i]);
  1152. }
  1153. };
  1154. /**
  1155. * Writes an array of numbers to the buffer as a packed 32-bit int field.
  1156. * @param {number} field The field number.
  1157. * @param {?Array<number>} value The array of ints to write.
  1158. */
  1159. jspb.BinaryWriter.prototype.writePackedInt32 = function(field, value) {
  1160. if (value == null || !value.length) return;
  1161. var bookmark = this.beginDelimited_(field);
  1162. for (var i = 0; i < value.length; i++) {
  1163. this.encoder_.writeSignedVarint32(value[i]);
  1164. }
  1165. this.endDelimited_(bookmark);
  1166. };
  1167. /**
  1168. * Writes an array of numbers represented as strings to the buffer as a packed
  1169. * 32-bit int field.
  1170. * @param {number} field
  1171. * @param {?Array<string>} value
  1172. */
  1173. jspb.BinaryWriter.prototype.writePackedInt32String = function(field, value) {
  1174. if (value == null || !value.length) return;
  1175. var bookmark = this.beginDelimited_(field);
  1176. for (var i = 0; i < value.length; i++) {
  1177. this.encoder_.writeSignedVarint32(parseInt(value[i], 10));
  1178. }
  1179. this.endDelimited_(bookmark);
  1180. };
  1181. /**
  1182. * Writes an array of numbers to the buffer as a packed 64-bit int field.
  1183. * @param {number} field The field number.
  1184. * @param {?Array<number>} value The array of ints to write.
  1185. */
  1186. jspb.BinaryWriter.prototype.writePackedInt64 = function(field, value) {
  1187. if (value == null || !value.length) return;
  1188. var bookmark = this.beginDelimited_(field);
  1189. for (var i = 0; i < value.length; i++) {
  1190. this.encoder_.writeSignedVarint64(value[i]);
  1191. }
  1192. this.endDelimited_(bookmark);
  1193. };
  1194. /**
  1195. * Writes an array of 64-bit values to the buffer as a fixed64.
  1196. * @param {number} field The field number.
  1197. * @param {?Array<T>} value The value.
  1198. * @param {function(T): number} lo Function to get low bits.
  1199. * @param {function(T): number} hi Function to get high bits.
  1200. * @template T
  1201. */
  1202. jspb.BinaryWriter.prototype.writePackedSplitFixed64 = function(
  1203. field, value, lo, hi) {
  1204. if (value == null) return;
  1205. var bookmark = this.beginDelimited_(field);
  1206. for (var i = 0; i < value.length; i++) {
  1207. this.encoder_.writeSplitFixed64(lo(value[i]), hi(value[i]));
  1208. }
  1209. this.endDelimited_(bookmark);
  1210. };
  1211. /**
  1212. * Writes an array of 64-bit values to the buffer as a varint.
  1213. * @param {number} field The field number.
  1214. * @param {?Array<T>} value The value.
  1215. * @param {function(T): number} lo Function to get low bits.
  1216. * @param {function(T): number} hi Function to get high bits.
  1217. * @template T
  1218. */
  1219. jspb.BinaryWriter.prototype.writePackedSplitVarint64 = function(
  1220. field, value, lo, hi) {
  1221. if (value == null) return;
  1222. var bookmark = this.beginDelimited_(field);
  1223. for (var i = 0; i < value.length; i++) {
  1224. this.encoder_.writeSplitVarint64(lo(value[i]), hi(value[i]));
  1225. }
  1226. this.endDelimited_(bookmark);
  1227. };
  1228. /**
  1229. * Writes an array of numbers represented as strings to the buffer as a packed
  1230. * 64-bit int field.
  1231. * @param {number} field
  1232. * @param {?Array<string>} value
  1233. */
  1234. jspb.BinaryWriter.prototype.writePackedInt64String = function(field, value) {
  1235. if (value == null || !value.length) return;
  1236. var bookmark = this.beginDelimited_(field);
  1237. for (var i = 0; i < value.length; i++) {
  1238. var num = jspb.arith.Int64.fromString(value[i]);
  1239. this.encoder_.writeSplitVarint64(num.lo, num.hi);
  1240. }
  1241. this.endDelimited_(bookmark);
  1242. };
  1243. /**
  1244. * Writes an array numbers to the buffer as a packed unsigned 32-bit int field.
  1245. * @param {number} field The field number.
  1246. * @param {?Array<number>} value The array of ints to write.
  1247. */
  1248. jspb.BinaryWriter.prototype.writePackedUint32 = function(field, value) {
  1249. if (value == null || !value.length) return;
  1250. var bookmark = this.beginDelimited_(field);
  1251. for (var i = 0; i < value.length; i++) {
  1252. this.encoder_.writeUnsignedVarint32(value[i]);
  1253. }
  1254. this.endDelimited_(bookmark);
  1255. };
  1256. /**
  1257. * Writes an array of numbers represented as strings to the buffer as a packed
  1258. * unsigned 32-bit int field.
  1259. * @param {number} field
  1260. * @param {?Array<string>} value
  1261. */
  1262. jspb.BinaryWriter.prototype.writePackedUint32String =
  1263. function(field, value) {
  1264. if (value == null || !value.length) return;
  1265. var bookmark = this.beginDelimited_(field);
  1266. for (var i = 0; i < value.length; i++) {
  1267. this.encoder_.writeUnsignedVarint32(parseInt(value[i], 10));
  1268. }
  1269. this.endDelimited_(bookmark);
  1270. };
  1271. /**
  1272. * Writes an array numbers to the buffer as a packed unsigned 64-bit int field.
  1273. * @param {number} field The field number.
  1274. * @param {?Array<number>} value The array of ints to write.
  1275. */
  1276. jspb.BinaryWriter.prototype.writePackedUint64 = function(field, value) {
  1277. if (value == null || !value.length) return;
  1278. var bookmark = this.beginDelimited_(field);
  1279. for (var i = 0; i < value.length; i++) {
  1280. this.encoder_.writeUnsignedVarint64(value[i]);
  1281. }
  1282. this.endDelimited_(bookmark);
  1283. };
  1284. /**
  1285. * Writes an array of numbers represented as strings to the buffer as a packed
  1286. * unsigned 64-bit int field.
  1287. * @param {number} field
  1288. * @param {?Array<string>} value
  1289. */
  1290. jspb.BinaryWriter.prototype.writePackedUint64String =
  1291. function(field, value) {
  1292. if (value == null || !value.length) return;
  1293. var bookmark = this.beginDelimited_(field);
  1294. for (var i = 0; i < value.length; i++) {
  1295. var num = jspb.arith.UInt64.fromString(value[i]);
  1296. this.encoder_.writeSplitVarint64(num.lo, num.hi);
  1297. }
  1298. this.endDelimited_(bookmark);
  1299. };
  1300. /**
  1301. * Writes an array numbers to the buffer as a packed signed 32-bit int field.
  1302. * @param {number} field The field number.
  1303. * @param {?Array<number>} value The array of ints to write.
  1304. */
  1305. jspb.BinaryWriter.prototype.writePackedSint32 = function(field, value) {
  1306. if (value == null || !value.length) return;
  1307. var bookmark = this.beginDelimited_(field);
  1308. for (var i = 0; i < value.length; i++) {
  1309. this.encoder_.writeZigzagVarint32(value[i]);
  1310. }
  1311. this.endDelimited_(bookmark);
  1312. };
  1313. /**
  1314. * Writes an array of numbers to the buffer as a packed signed 64-bit int field.
  1315. * @param {number} field The field number.
  1316. * @param {?Array<number>} value The array of ints to write.
  1317. */
  1318. jspb.BinaryWriter.prototype.writePackedSint64 = function(field, value) {
  1319. if (value == null || !value.length) return;
  1320. var bookmark = this.beginDelimited_(field);
  1321. for (var i = 0; i < value.length; i++) {
  1322. this.encoder_.writeZigzagVarint64(value[i]);
  1323. }
  1324. this.endDelimited_(bookmark);
  1325. };
  1326. /**
  1327. * Writes an array of decimal strings to the buffer as a packed signed 64-bit
  1328. * int field.
  1329. * @param {number} field The field number.
  1330. * @param {?Array<string>} value The array of decimal strings to write.
  1331. */
  1332. jspb.BinaryWriter.prototype.writePackedSint64String = function(field, value) {
  1333. if (value == null || !value.length) return;
  1334. var bookmark = this.beginDelimited_(field);
  1335. for (var i = 0; i < value.length; i++) {
  1336. // TODO(haberman): make lossless
  1337. this.encoder_.writeZigzagVarint64(parseInt(value[i], 10));
  1338. }
  1339. this.endDelimited_(bookmark);
  1340. };
  1341. /**
  1342. * Writes an array of numbers to the buffer as a packed fixed32 field.
  1343. * @param {number} field The field number.
  1344. * @param {?Array<number>} value The array of ints to write.
  1345. */
  1346. jspb.BinaryWriter.prototype.writePackedFixed32 = function(field, value) {
  1347. if (value == null || !value.length) return;
  1348. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1349. this.encoder_.writeUnsignedVarint32(value.length * 4);
  1350. for (var i = 0; i < value.length; i++) {
  1351. this.encoder_.writeUint32(value[i]);
  1352. }
  1353. };
  1354. /**
  1355. * Writes an array of numbers to the buffer as a packed fixed64 field.
  1356. * @param {number} field The field number.
  1357. * @param {?Array<number>} value The array of ints to write.
  1358. */
  1359. jspb.BinaryWriter.prototype.writePackedFixed64 = function(field, value) {
  1360. if (value == null || !value.length) return;
  1361. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1362. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1363. for (var i = 0; i < value.length; i++) {
  1364. this.encoder_.writeUint64(value[i]);
  1365. }
  1366. };
  1367. /**
  1368. * Writes an array of numbers represented as strings to the buffer as a packed
  1369. * fixed64 field.
  1370. * @param {number} field The field number.
  1371. * @param {?Array<string>} value The array of strings to write.
  1372. */
  1373. jspb.BinaryWriter.prototype.writePackedFixed64String = function(field, value) {
  1374. if (value == null || !value.length) return;
  1375. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1376. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1377. for (var i = 0; i < value.length; i++) {
  1378. var num = jspb.arith.UInt64.fromString(value[i]);
  1379. this.encoder_.writeSplitFixed64(num.lo, num.hi);
  1380. }
  1381. };
  1382. /**
  1383. * Writes an array of numbers to the buffer as a packed sfixed32 field.
  1384. * @param {number} field The field number.
  1385. * @param {?Array<number>} value The array of ints to write.
  1386. */
  1387. jspb.BinaryWriter.prototype.writePackedSfixed32 = function(field, value) {
  1388. if (value == null || !value.length) return;
  1389. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1390. this.encoder_.writeUnsignedVarint32(value.length * 4);
  1391. for (var i = 0; i < value.length; i++) {
  1392. this.encoder_.writeInt32(value[i]);
  1393. }
  1394. };
  1395. /**
  1396. * Writes an array of numbers to the buffer as a packed sfixed64 field.
  1397. * @param {number} field The field number.
  1398. * @param {?Array<number>} value The array of ints to write.
  1399. */
  1400. jspb.BinaryWriter.prototype.writePackedSfixed64 = function(field, value) {
  1401. if (value == null || !value.length) return;
  1402. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1403. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1404. for (var i = 0; i < value.length; i++) {
  1405. this.encoder_.writeInt64(value[i]);
  1406. }
  1407. };
  1408. /**
  1409. * Writes an array of numbers to the buffer as a packed sfixed64 field.
  1410. * @param {number} field The field number.
  1411. * @param {?Array<string>} value The array of decimal strings to write.
  1412. */
  1413. jspb.BinaryWriter.prototype.writePackedSfixed64String = function(field, value) {
  1414. if (value == null || !value.length) return;
  1415. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1416. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1417. for (var i = 0; i < value.length; i++) {
  1418. this.encoder_.writeInt64String(value[i]);
  1419. }
  1420. };
  1421. /**
  1422. * Writes an array of numbers to the buffer as a packed float field.
  1423. * @param {number} field The field number.
  1424. * @param {?Array<number>} value The array of ints to write.
  1425. */
  1426. jspb.BinaryWriter.prototype.writePackedFloat = function(field, value) {
  1427. if (value == null || !value.length) return;
  1428. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1429. this.encoder_.writeUnsignedVarint32(value.length * 4);
  1430. for (var i = 0; i < value.length; i++) {
  1431. this.encoder_.writeFloat(value[i]);
  1432. }
  1433. };
  1434. /**
  1435. * Writes an array of numbers to the buffer as a packed double field.
  1436. * @param {number} field The field number.
  1437. * @param {?Array<number>} value The array of ints to write.
  1438. */
  1439. jspb.BinaryWriter.prototype.writePackedDouble = function(field, value) {
  1440. if (value == null || !value.length) return;
  1441. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1442. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1443. for (var i = 0; i < value.length; i++) {
  1444. this.encoder_.writeDouble(value[i]);
  1445. }
  1446. };
  1447. /**
  1448. * Writes an array of booleans to the buffer as a packed bool field.
  1449. * @param {number} field The field number.
  1450. * @param {?Array<boolean>} value The array of ints to write.
  1451. */
  1452. jspb.BinaryWriter.prototype.writePackedBool = function(field, value) {
  1453. if (value == null || !value.length) return;
  1454. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1455. this.encoder_.writeUnsignedVarint32(value.length);
  1456. for (var i = 0; i < value.length; i++) {
  1457. this.encoder_.writeBool(value[i]);
  1458. }
  1459. };
  1460. /**
  1461. * Writes an array of enums to the buffer as a packed enum field.
  1462. * @param {number} field The field number.
  1463. * @param {?Array<number>} value The array of ints to write.
  1464. */
  1465. jspb.BinaryWriter.prototype.writePackedEnum = function(field, value) {
  1466. if (value == null || !value.length) return;
  1467. var bookmark = this.beginDelimited_(field);
  1468. for (var i = 0; i < value.length; i++) {
  1469. this.encoder_.writeEnum(value[i]);
  1470. }
  1471. this.endDelimited_(bookmark);
  1472. };
  1473. /**
  1474. * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
  1475. * the buffer.
  1476. * @param {number} field The field number.
  1477. * @param {?Array<string>} value The array of hashes to write.
  1478. */
  1479. jspb.BinaryWriter.prototype.writePackedFixedHash64 = function(field, value) {
  1480. if (value == null || !value.length) return;
  1481. this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED);
  1482. this.encoder_.writeUnsignedVarint32(value.length * 8);
  1483. for (var i = 0; i < value.length; i++) {
  1484. this.encoder_.writeFixedHash64(value[i]);
  1485. }
  1486. };
  1487. /**
  1488. * Writes a 64-bit hash string field (8 characters @ 8 bits of data each) to
  1489. * the buffer.
  1490. * @param {number} field The field number.
  1491. * @param {?Array<string>} value The array of hashes to write.
  1492. */
  1493. jspb.BinaryWriter.prototype.writePackedVarintHash64 = function(field, value) {
  1494. if (value == null || !value.length) return;
  1495. var bookmark = this.beginDelimited_(field);
  1496. for (var i = 0; i < value.length; i++) {
  1497. this.encoder_.writeVarintHash64(value[i]);
  1498. }
  1499. this.endDelimited_(bookmark);
  1500. };