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 * @suppress {missingRequire} TODO(b/152540451): this shouldn't be needed 35 * 36 * @author aappleby@google.com (Austin Appleby) 37 */ 38 39goog.provide('jspb.AnyFieldType'); 40goog.provide('jspb.BinaryConstants'); 41goog.provide('jspb.BinaryMessage'); 42goog.provide('jspb.BuilderFunction'); 43goog.provide('jspb.ByteSource'); 44goog.provide('jspb.ClonerFunction'); 45goog.provide('jspb.ComparerFunction'); 46goog.provide('jspb.ConstBinaryMessage'); 47goog.provide('jspb.PrunerFunction'); 48goog.provide('jspb.ReaderFunction'); 49goog.provide('jspb.RecyclerFunction'); 50goog.provide('jspb.RepeatedFieldType'); 51goog.provide('jspb.ScalarFieldType'); 52goog.provide('jspb.WriterFunction'); 53 54 55goog.forwardDeclare('jspb.BinaryMessage'); 56goog.forwardDeclare('jspb.BinaryReader'); 57goog.forwardDeclare('jspb.BinaryWriter'); 58goog.forwardDeclare('jspb.Message'); 59goog.forwardDeclare('jsprotolib.BinaryExtension'); 60 61 62 63/** 64 * Base interface class for all const messages. 65 * @interface 66 */ 67jspb.ConstBinaryMessage = function() {}; 68 69/** 70 * Generate a debug string for this proto that is in proto2 text format. 71 * @return {string} The debug string. 72 */ 73jspb.ConstBinaryMessage.prototype.toDebugString; 74 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 */ 82jspb.ConstBinaryMessage.prototype.toDebugStringInternal; 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 */ 90jspb.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 */ 98jspb.ByteSource; 99 100 101/** 102 * A scalar field in jspb can be a boolean, number, or string. 103 * @typedef {boolean|number|string} 104 */ 105jspb.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>| 112 !Array<!jspb.ConstBinaryMessage>| 113 !Array<!jspb.BinaryMessage>} 114 */ 115jspb.RepeatedFieldType; 116 117 118/** 119 * A field in jspb can be a scalar, a block of bytes, another proto, or an 120 * array of any of the above. 121 * @typedef {jspb.ScalarFieldType| 122 jspb.RepeatedFieldType| 123 !Uint8Array| 124 !jspb.ConstBinaryMessage| 125 !jspb.BinaryMessage| 126 !jsprotolib.BinaryExtension} 127 */ 128jspb.AnyFieldType; 129 130 131/** 132 * A builder function creates an instance of a message object. 133 * @typedef {function():!jspb.BinaryMessage} 134 */ 135jspb.BuilderFunction; 136 137 138/** 139 * A cloner function creates a deep copy of a message object. 140 * @typedef {function(jspb.ConstBinaryMessage):jspb.BinaryMessage} 141 */ 142jspb.ClonerFunction; 143 144 145/** 146 * A recycler function destroys an instance of a message object. 147 * @typedef {function(!jspb.BinaryMessage):void} 148 */ 149jspb.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 */ 156jspb.ReaderFunction; 157 158 159/** 160 * A writer function serializes a message to a BinaryWriter. 161 * @typedef {function((!jspb.Message|!jspb.ConstBinaryMessage), 162 * !jspb.BinaryWriter):void} 163 */ 164jspb.WriterFunction; 165 166 167/** 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 */ 173jspb.PrunerFunction; 174 175 176/** 177 * A comparer function returns true if two protos are equal. 178 * @typedef {function(?jspb.ConstBinaryMessage, 179 * ?jspb.ConstBinaryMessage):boolean} 180 */ 181jspb.ComparerFunction; 182 183 184/** 185 * Field type codes, taken from proto2/public/wire_format_lite.h. 186 * @enum {number} 187 */ 188jspb.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 */ 220jspb.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 */ 236jspb.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 */ 279jspb.BinaryConstants.INVALID_FIELD_NUMBER = -1; 280 281 282/** 283 * The smallest denormal float32 value. 284 * @const {number} 285 */ 286jspb.BinaryConstants.FLOAT32_EPS = 1.401298464324817e-45; 287 288 289/** 290 * The smallest normal float64 value. 291 * @const {number} 292 */ 293jspb.BinaryConstants.FLOAT32_MIN = 1.1754943508222875e-38; 294 295 296/** 297 * The largest finite float32 value. 298 * @const {number} 299 */ 300jspb.BinaryConstants.FLOAT32_MAX = 3.4028234663852886e+38; 301 302 303/** 304 * The smallest denormal float64 value. 305 * @const {number} 306 */ 307jspb.BinaryConstants.FLOAT64_EPS = 5e-324; 308 309 310/** 311 * The smallest normal float64 value. 312 * @const {number} 313 */ 314jspb.BinaryConstants.FLOAT64_MIN = 2.2250738585072014e-308; 315 316 317/** 318 * The largest finite float64 value. 319 * @const {number} 320 */ 321jspb.BinaryConstants.FLOAT64_MAX = 1.7976931348623157e+308; 322 323 324/** 325 * Convenience constant equal to 2^20. 326 * @const {number} 327 */ 328jspb.BinaryConstants.TWO_TO_20 = 1048576; 329 330 331/** 332 * Convenience constant equal to 2^23. 333 * @const {number} 334 */ 335jspb.BinaryConstants.TWO_TO_23 = 8388608; 336 337 338/** 339 * Convenience constant equal to 2^31. 340 * @const {number} 341 */ 342jspb.BinaryConstants.TWO_TO_31 = 2147483648; 343 344 345/** 346 * Convenience constant equal to 2^32. 347 * @const {number} 348 */ 349jspb.BinaryConstants.TWO_TO_32 = 4294967296; 350 351 352/** 353 * Convenience constant equal to 2^52. 354 * @const {number} 355 */ 356jspb.BinaryConstants.TWO_TO_52 = 4503599627370496; 357 358 359/** 360 * Convenience constant equal to 2^63. 361 * @const {number} 362 */ 363jspb.BinaryConstants.TWO_TO_63 = 9223372036854775808; 364 365 366/** 367 * Convenience constant equal to 2^64. 368 * @const {number} 369 */ 370jspb.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 */ 377jspb.BinaryConstants.ZERO_HASH = '\0\0\0\0\0\0\0\0'; 378