constants.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 constants and typedefs used by
  32. * jspb.BinaryReader and BinaryWriter.
  33. * @suppress {missingRequire} TODO(b/152540451): this shouldn't be needed
  34. *
  35. * @author aappleby@google.com (Austin Appleby)
  36. */
  37. goog.provide('jspb.AnyFieldType');
  38. goog.provide('jspb.BinaryConstants');
  39. goog.provide('jspb.BinaryMessage');
  40. goog.provide('jspb.BuilderFunction');
  41. goog.provide('jspb.ByteSource');
  42. goog.provide('jspb.ClonerFunction');
  43. goog.provide('jspb.ComparerFunction');
  44. goog.provide('jspb.ConstBinaryMessage');
  45. goog.provide('jspb.PrunerFunction');
  46. goog.provide('jspb.ReaderFunction');
  47. goog.provide('jspb.RecyclerFunction');
  48. goog.provide('jspb.RepeatedFieldType');
  49. goog.provide('jspb.ScalarFieldType');
  50. goog.provide('jspb.WriterFunction');
  51. goog.forwardDeclare('jspb.BinaryMessage');
  52. goog.forwardDeclare('jspb.BinaryReader');
  53. goog.forwardDeclare('jspb.BinaryWriter');
  54. goog.forwardDeclare('jspb.Message');
  55. goog.forwardDeclare('jsprotolib.BinaryExtension');
  56. /**
  57. * Base interface class for all const messages.
  58. * @interface
  59. */
  60. jspb.ConstBinaryMessage = function() {};
  61. /**
  62. * Generate a debug string for this proto that is in proto2 text format.
  63. * @return {string} The debug string.
  64. */
  65. jspb.ConstBinaryMessage.prototype.toDebugString;
  66. /**
  67. * Helper to generate a debug string for this proto at some indent level. The
  68. * first line is not indented.
  69. * @param {number} indentLevel The number of spaces by which to indent lines.
  70. * @return {string} The debug string.
  71. * @protected
  72. */
  73. jspb.ConstBinaryMessage.prototype.toDebugStringInternal;
  74. /**
  75. * Base interface class for all messages. Does __not__ define any methods, as
  76. * doing so on a widely-used interface defeats dead-code elimination.
  77. * @interface
  78. * @extends {jspb.ConstBinaryMessage}
  79. */
  80. jspb.BinaryMessage = function() {};
  81. /**
  82. * The types convertible to Uint8Arrays. Strings are assumed to be
  83. * base64-encoded.
  84. * @typedef {ArrayBuffer|Uint8Array|Array<number>|string}
  85. */
  86. jspb.ByteSource;
  87. /**
  88. * A scalar field in jspb can be a boolean, number, or string.
  89. * @typedef {boolean|number|string}
  90. */
  91. jspb.ScalarFieldType;
  92. /**
  93. * A repeated field in jspb is an array of scalars, blobs, or messages.
  94. * @typedef {!Array<jspb.ScalarFieldType>|
  95. !Array<!Uint8Array>|
  96. !Array<!jspb.ConstBinaryMessage>|
  97. !Array<!jspb.BinaryMessage>}
  98. */
  99. jspb.RepeatedFieldType;
  100. /**
  101. * A field in jspb can be a scalar, a block of bytes, another proto, or an
  102. * array of any of the above.
  103. * @typedef {jspb.ScalarFieldType|
  104. jspb.RepeatedFieldType|
  105. !Uint8Array|
  106. !jspb.ConstBinaryMessage|
  107. !jspb.BinaryMessage|
  108. !jsprotolib.BinaryExtension}
  109. */
  110. jspb.AnyFieldType;
  111. /**
  112. * A builder function creates an instance of a message object.
  113. * @typedef {function():!jspb.BinaryMessage}
  114. */
  115. jspb.BuilderFunction;
  116. /**
  117. * A cloner function creates a deep copy of a message object.
  118. * @typedef {function(jspb.ConstBinaryMessage):jspb.BinaryMessage}
  119. */
  120. jspb.ClonerFunction;
  121. /**
  122. * A recycler function destroys an instance of a message object.
  123. * @typedef {function(!jspb.BinaryMessage):void}
  124. */
  125. jspb.RecyclerFunction;
  126. /**
  127. * A reader function initializes a message using data from a BinaryReader.
  128. * @typedef {function(!jspb.BinaryMessage, !jspb.BinaryReader):void}
  129. */
  130. jspb.ReaderFunction;
  131. /**
  132. * A writer function serializes a message to a BinaryWriter.
  133. * @typedef {function((!jspb.Message|!jspb.ConstBinaryMessage),
  134. * !jspb.BinaryWriter):void}
  135. */
  136. jspb.WriterFunction;
  137. /**
  138. * A pruner function removes default-valued fields and empty submessages from a
  139. * message and returns either the pruned message or null if the entire message
  140. * was pruned away.
  141. * @typedef {function(?jspb.BinaryMessage):?jspb.BinaryMessage}
  142. */
  143. jspb.PrunerFunction;
  144. /**
  145. * A comparer function returns true if two protos are equal.
  146. * @typedef {function(?jspb.ConstBinaryMessage,
  147. * ?jspb.ConstBinaryMessage):boolean}
  148. */
  149. jspb.ComparerFunction;
  150. /**
  151. * Field type codes, taken from proto2/public/wire_format_lite.h.
  152. * @enum {number}
  153. */
  154. jspb.BinaryConstants.FieldType = {
  155. INVALID: -1,
  156. DOUBLE: 1,
  157. FLOAT: 2,
  158. INT64: 3,
  159. UINT64: 4,
  160. INT32: 5,
  161. FIXED64: 6,
  162. FIXED32: 7,
  163. BOOL: 8,
  164. STRING: 9,
  165. GROUP: 10,
  166. MESSAGE: 11,
  167. BYTES: 12,
  168. UINT32: 13,
  169. ENUM: 14,
  170. SFIXED32: 15,
  171. SFIXED64: 16,
  172. SINT32: 17,
  173. SINT64: 18,
  174. // Extended types for Javascript
  175. FHASH64: 30, // 64-bit hash string, fixed-length encoding.
  176. VHASH64: 31 // 64-bit hash string, varint encoding.
  177. };
  178. /**
  179. * Wire-format type codes, taken from proto2/public/wire_format_lite.h.
  180. * @enum {number}
  181. */
  182. jspb.BinaryConstants.WireType = {
  183. INVALID: -1,
  184. VARINT: 0,
  185. FIXED64: 1,
  186. DELIMITED: 2,
  187. START_GROUP: 3,
  188. END_GROUP: 4,
  189. FIXED32: 5
  190. };
  191. /**
  192. * Translates field type to wire type.
  193. * @param {jspb.BinaryConstants.FieldType} fieldType
  194. * @return {jspb.BinaryConstants.WireType}
  195. */
  196. jspb.BinaryConstants.FieldTypeToWireType = function(fieldType) {
  197. var fieldTypes = jspb.BinaryConstants.FieldType;
  198. var wireTypes = jspb.BinaryConstants.WireType;
  199. switch (fieldType) {
  200. case fieldTypes.INT32:
  201. case fieldTypes.INT64:
  202. case fieldTypes.UINT32:
  203. case fieldTypes.UINT64:
  204. case fieldTypes.SINT32:
  205. case fieldTypes.SINT64:
  206. case fieldTypes.BOOL:
  207. case fieldTypes.ENUM:
  208. case fieldTypes.VHASH64:
  209. return wireTypes.VARINT;
  210. case fieldTypes.DOUBLE:
  211. case fieldTypes.FIXED64:
  212. case fieldTypes.SFIXED64:
  213. case fieldTypes.FHASH64:
  214. return wireTypes.FIXED64;
  215. case fieldTypes.STRING:
  216. case fieldTypes.MESSAGE:
  217. case fieldTypes.BYTES:
  218. return wireTypes.DELIMITED;
  219. case fieldTypes.FLOAT:
  220. case fieldTypes.FIXED32:
  221. case fieldTypes.SFIXED32:
  222. return wireTypes.FIXED32;
  223. case fieldTypes.INVALID:
  224. case fieldTypes.GROUP:
  225. default:
  226. return wireTypes.INVALID;
  227. }
  228. };
  229. /**
  230. * Flag to indicate a missing field.
  231. * @const {number}
  232. */
  233. jspb.BinaryConstants.INVALID_FIELD_NUMBER = -1;
  234. /**
  235. * The smallest denormal float32 value.
  236. * @const {number}
  237. */
  238. jspb.BinaryConstants.FLOAT32_EPS = 1.401298464324817e-45;
  239. /**
  240. * The smallest normal float64 value.
  241. * @const {number}
  242. */
  243. jspb.BinaryConstants.FLOAT32_MIN = 1.1754943508222875e-38;
  244. /**
  245. * The largest finite float32 value.
  246. * @const {number}
  247. */
  248. jspb.BinaryConstants.FLOAT32_MAX = 3.4028234663852886e+38;
  249. /**
  250. * The smallest denormal float64 value.
  251. * @const {number}
  252. */
  253. jspb.BinaryConstants.FLOAT64_EPS = 5e-324;
  254. /**
  255. * The smallest normal float64 value.
  256. * @const {number}
  257. */
  258. jspb.BinaryConstants.FLOAT64_MIN = 2.2250738585072014e-308;
  259. /**
  260. * The largest finite float64 value.
  261. * @const {number}
  262. */
  263. jspb.BinaryConstants.FLOAT64_MAX = 1.7976931348623157e+308;
  264. /**
  265. * Convenience constant equal to 2^20.
  266. * @const {number}
  267. */
  268. jspb.BinaryConstants.TWO_TO_20 = 1048576;
  269. /**
  270. * Convenience constant equal to 2^23.
  271. * @const {number}
  272. */
  273. jspb.BinaryConstants.TWO_TO_23 = 8388608;
  274. /**
  275. * Convenience constant equal to 2^31.
  276. * @const {number}
  277. */
  278. jspb.BinaryConstants.TWO_TO_31 = 2147483648;
  279. /**
  280. * Convenience constant equal to 2^32.
  281. * @const {number}
  282. */
  283. jspb.BinaryConstants.TWO_TO_32 = 4294967296;
  284. /**
  285. * Convenience constant equal to 2^52.
  286. * @const {number}
  287. */
  288. jspb.BinaryConstants.TWO_TO_52 = 4503599627370496;
  289. /**
  290. * Convenience constant equal to 2^63.
  291. * @const {number}
  292. */
  293. jspb.BinaryConstants.TWO_TO_63 = 9223372036854775808;
  294. /**
  295. * Convenience constant equal to 2^64.
  296. * @const {number}
  297. */
  298. jspb.BinaryConstants.TWO_TO_64 = 18446744073709551616;
  299. /**
  300. * Eight-character string of zeros, used as the default 64-bit hash value.
  301. * @const {string}
  302. */
  303. jspb.BinaryConstants.ZERO_HASH = '\0\0\0\0\0\0\0\0';