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