constants.js 9.1 KB

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