constants.js 8.7 KB

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