constants.js 7.6 KB

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