constants.js 8.5 KB

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