Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 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 | /** |
| 32 | * @fileoverview This file contains constants and typedefs used by |
| 33 | * jspb.BinaryReader and BinaryWriter. |
| 34 | * |
| 35 | * @author aappleby@google.com (Austin Appleby) |
| 36 | */ |
| 37 | |
| 38 | goog.provide('jspb.AnyFieldType'); |
| 39 | goog.provide('jspb.BinaryConstants'); |
| 40 | goog.provide('jspb.BinaryMessage'); |
| 41 | goog.provide('jspb.BuilderFunction'); |
| 42 | goog.provide('jspb.ByteSource'); |
| 43 | goog.provide('jspb.ClonerFunction'); |
| 44 | goog.provide('jspb.ConstBinaryMessage'); |
| 45 | goog.provide('jspb.ReaderFunction'); |
| 46 | goog.provide('jspb.RecyclerFunction'); |
| 47 | goog.provide('jspb.WriterFunction'); |
| 48 | |
| 49 | goog.forwardDeclare('jspb.Message'); |
| 50 | goog.forwardDeclare('jsproto.BinaryExtension'); |
| 51 | |
| 52 | |
| 53 | |
| 54 | /** |
| 55 | * Base interface class for all const messages. Does __not__ define any |
| 56 | * methods, as doing so on a widely-used interface defeats dead-code |
| 57 | * elimination. |
| 58 | * @interface |
| 59 | */ |
| 60 | jspb.ConstBinaryMessage = function() {}; |
| 61 | |
| 62 | |
| 63 | |
| 64 | /** |
| 65 | * Base interface class for all messages. Does __not__ define any methods, as |
| 66 | * doing so on a widely-used interface defeats dead-code elimination. |
| 67 | * @interface |
| 68 | * @extends {jspb.ConstBinaryMessage} |
| 69 | */ |
| 70 | jspb.BinaryMessage = function() {}; |
| 71 | |
| 72 | |
| 73 | /** |
| 74 | * The types convertible to Uint8Arrays. Strings are assumed to be |
| 75 | * base64-encoded. |
| 76 | * @typedef {ArrayBuffer|Uint8Array|Array<number>|string} |
| 77 | */ |
| 78 | jspb.ByteSource; |
| 79 | |
| 80 | |
| 81 | /** |
| 82 | * A field in jspb can be a scalar, a block of bytes, another proto, or an |
| 83 | * array of any of the above. |
| 84 | * @typedef {boolean|number|string|Uint8Array| |
| 85 | jspb.BinaryMessage|jsproto.BinaryExtension| |
| 86 | Array<jspb.AnyFieldType>} |
| 87 | */ |
| 88 | jspb.AnyFieldType; |
| 89 | |
| 90 | |
| 91 | /** |
| 92 | * A builder function creates an instance of a message object. |
| 93 | * @typedef {function():!jspb.BinaryMessage} |
| 94 | */ |
| 95 | jspb.BuilderFunction; |
| 96 | |
| 97 | |
| 98 | /** |
| 99 | * A cloner function creates a deep copy of a message object. |
| 100 | * @typedef {function(jspb.ConstBinaryMessage):jspb.BinaryMessage} |
| 101 | */ |
| 102 | jspb.ClonerFunction; |
| 103 | |
| 104 | |
| 105 | /** |
| 106 | * A recycler function destroys an instance of a message object. |
| 107 | * @typedef {function(!jspb.BinaryMessage):void} |
| 108 | */ |
| 109 | jspb.RecyclerFunction; |
| 110 | |
| 111 | |
| 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 | |
| 119 | /** |
| 120 | * A writer function serializes a message to a BinaryWriter. |
| 121 | * @typedef {!function(!jspb.Message, !jspb.BinaryWriter):void | |
| 122 | * !function(!jspb.ConstBinaryMessage, !jspb.BinaryWriter):void} |
| 123 | */ |
| 124 | jspb.WriterFunction; |
| 125 | |
| 126 | |
| 127 | /** |
| 128 | * Field type codes, taken from proto2/public/wire_format_lite.h. |
| 129 | * @enum {number} |
| 130 | */ |
| 131 | jspb.BinaryConstants.FieldType = { |
| 132 | INVALID: -1, |
| 133 | DOUBLE: 1, |
| 134 | FLOAT: 2, |
| 135 | INT64: 3, |
| 136 | UINT64: 4, |
| 137 | INT32: 5, |
| 138 | FIXED64: 6, |
| 139 | FIXED32: 7, |
| 140 | BOOL: 8, |
| 141 | STRING: 9, |
| 142 | GROUP: 10, |
| 143 | MESSAGE: 11, |
| 144 | BYTES: 12, |
| 145 | UINT32: 13, |
| 146 | ENUM: 14, |
| 147 | SFIXED32: 15, |
| 148 | SFIXED64: 16, |
| 149 | SINT32: 17, |
| 150 | SINT64: 18, |
| 151 | |
| 152 | // Extended types for Javascript |
| 153 | |
| 154 | FHASH64: 30, // 64-bit hash string, fixed-length encoding. |
| 155 | VHASH64: 31 // 64-bit hash string, varint encoding. |
| 156 | }; |
| 157 | |
| 158 | |
| 159 | /** |
| 160 | * Wire-format type codes, taken from proto2/public/wire_format_lite.h. |
| 161 | * @enum {number} |
| 162 | */ |
| 163 | jspb.BinaryConstants.WireType = { |
| 164 | INVALID: -1, |
| 165 | VARINT: 0, |
| 166 | FIXED64: 1, |
| 167 | DELIMITED: 2, |
| 168 | START_GROUP: 3, |
| 169 | END_GROUP: 4, |
| 170 | FIXED32: 5 |
| 171 | }; |
| 172 | |
| 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 | |
| 194 | case fieldTypes.DOUBLE: |
| 195 | case fieldTypes.FIXED64: |
| 196 | case fieldTypes.SFIXED64: |
| 197 | case fieldTypes.FHASH64: |
| 198 | return wireTypes.FIXED64; |
| 199 | |
| 200 | case fieldTypes.STRING: |
| 201 | case fieldTypes.MESSAGE: |
| 202 | case fieldTypes.BYTES: |
| 203 | return wireTypes.DELIMITED; |
| 204 | |
| 205 | case fieldTypes.FLOAT: |
| 206 | case fieldTypes.FIXED32: |
| 207 | case fieldTypes.SFIXED32: |
| 208 | return wireTypes.FIXED32; |
| 209 | |
| 210 | case fieldTypes.INVALID: |
| 211 | case fieldTypes.GROUP: |
| 212 | default: |
| 213 | return wireTypes.INVALID; |
| 214 | } |
| 215 | }; |
| 216 | |
| 217 | |
| 218 | /** |
| 219 | * Flag to indicate a missing field. |
| 220 | * @const {number} |
| 221 | */ |
| 222 | jspb.BinaryConstants.INVALID_FIELD_NUMBER = -1; |
| 223 | |
| 224 | |
| 225 | /** |
| 226 | * The smallest denormal float32 value. |
| 227 | * @const {number} |
| 228 | */ |
| 229 | jspb.BinaryConstants.FLOAT32_EPS = 1.401298464324817e-45; |
| 230 | |
| 231 | |
| 232 | /** |
| 233 | * The smallest normal float64 value. |
| 234 | * @const {number} |
| 235 | */ |
| 236 | jspb.BinaryConstants.FLOAT32_MIN = 1.1754943508222875e-38; |
| 237 | |
| 238 | |
| 239 | /** |
| 240 | * The largest finite float32 value. |
| 241 | * @const {number} |
| 242 | */ |
| 243 | jspb.BinaryConstants.FLOAT32_MAX = 3.4028234663852886e+38; |
| 244 | |
| 245 | |
| 246 | /** |
| 247 | * The smallest denormal float64 value. |
| 248 | * @const {number} |
| 249 | */ |
| 250 | jspb.BinaryConstants.FLOAT64_EPS = 5e-324; |
| 251 | |
| 252 | |
| 253 | /** |
| 254 | * The smallest normal float64 value. |
| 255 | * @const {number} |
| 256 | */ |
| 257 | jspb.BinaryConstants.FLOAT64_MIN = 2.2250738585072014e-308; |
| 258 | |
| 259 | |
| 260 | /** |
| 261 | * The largest finite float64 value. |
| 262 | * @const {number} |
| 263 | */ |
| 264 | jspb.BinaryConstants.FLOAT64_MAX = 1.7976931348623157e+308; |
| 265 | |
| 266 | |
| 267 | /** |
| 268 | * Convenience constant equal to 2^20. |
| 269 | * @const {number} |
| 270 | */ |
| 271 | jspb.BinaryConstants.TWO_TO_20 = 1048576; |
| 272 | |
| 273 | |
| 274 | /** |
| 275 | * Convenience constant equal to 2^23. |
| 276 | * @const {number} |
| 277 | */ |
| 278 | jspb.BinaryConstants.TWO_TO_23 = 8388608; |
| 279 | |
| 280 | |
| 281 | /** |
| 282 | * Convenience constant equal to 2^31. |
| 283 | * @const {number} |
| 284 | */ |
| 285 | jspb.BinaryConstants.TWO_TO_31 = 2147483648; |
| 286 | |
| 287 | |
| 288 | /** |
| 289 | * Convenience constant equal to 2^32. |
| 290 | * @const {number} |
| 291 | */ |
| 292 | jspb.BinaryConstants.TWO_TO_32 = 4294967296; |
| 293 | |
| 294 | |
| 295 | /** |
| 296 | * Convenience constant equal to 2^52. |
| 297 | * @const {number} |
| 298 | */ |
| 299 | jspb.BinaryConstants.TWO_TO_52 = 4503599627370496; |
| 300 | |
| 301 | |
| 302 | /** |
| 303 | * Convenience constant equal to 2^63. |
| 304 | * @const {number} |
| 305 | */ |
| 306 | jspb.BinaryConstants.TWO_TO_63 = 9223372036854775808; |
| 307 | |
| 308 | |
| 309 | /** |
| 310 | * Convenience constant equal to 2^64. |
| 311 | * @const {number} |
| 312 | */ |
| 313 | jspb.BinaryConstants.TWO_TO_64 = 18446744073709551616; |
| 314 | |
| 315 | |
| 316 | /** |
| 317 | * Eight-character string of zeros, used as the default 64-bit hash value. |
| 318 | * @const {string} |
| 319 | */ |
| 320 | jspb.BinaryConstants.ZERO_HASH = '\0\0\0\0\0\0\0\0'; |