From bed436a00f4e4195f589841ccff71ab455a4401a Mon Sep 17 00:00:00 2001 From: Stephanie DiBenedetto Date: Thu, 13 Oct 2022 23:50:25 +0000 Subject: [PATCH] Re-enable advanced optimizations with binary/ tests in CommonJS --- binary/arith.js | 33 +++++++++-- binary/constants.js | 32 +++++++++++ binary/decoder.js | 59 +++++++++++++++++++ binary/decoder_test.js | 11 ++-- binary/encoder.js | 58 ++++++++++++++----- binary/message_test.js | 4 +- binary/reader.js | 62 ++++++++++++++++++++ binary/reader_test.js | 28 ++++----- binary/utils.js | 53 +++++++++++++++++ binary/utils_test.js | 16 +++--- binary/writer.js | 47 +++++++++++++++ binary/writer_test.js | 10 ++-- commonjs/export.js | 5 ++ commonjs/export_testdeps.js | 59 +++++++++++++++++-- commonjs/jasmine.json | 9 ++- commonjs/rewrite_tests_for_commonjs.js | 2 +- debug.js | 1 + debug_test.js | 4 +- gulpfile.js | 79 ++++++++++++++++++++------ package.json | 3 +- 20 files changed, 493 insertions(+), 82 deletions(-) diff --git a/binary/arith.js b/binary/arith.js index 62528a2..629c8d7 100644 --- a/binary/arith.js +++ b/binary/arith.js @@ -49,16 +49,19 @@ goog.provide('jspb.arith.UInt64'); * @param {number} lo The low 32 bits. * @param {number} hi The high 32 bits. * @constructor + * @export */ jspb.arith.UInt64 = function(lo, hi) { /** * The low 32 bits. - * @public {number} + * @type {number} + * @export */ this.lo = lo; /** * The high 32 bits. - * @public {number} + * @type {number} + * @export */ this.hi = hi; }; @@ -69,6 +72,7 @@ jspb.arith.UInt64 = function(lo, hi) { * less, +1 if the first is greater, or 0 if both are equal. * @param {!jspb.arith.UInt64} other * @return {number} + * @export */ jspb.arith.UInt64.prototype.cmp = function(other) { if (this.hi < other.hi || (this.hi == other.hi && this.lo < other.lo)) { @@ -84,6 +88,7 @@ jspb.arith.UInt64.prototype.cmp = function(other) { /** * Right-shift this number by one bit. * @return {!jspb.arith.UInt64} + * @export */ jspb.arith.UInt64.prototype.rightShift = function() { var hi = this.hi >>> 1; @@ -95,6 +100,7 @@ jspb.arith.UInt64.prototype.rightShift = function() { /** * Left-shift this number by one bit. * @return {!jspb.arith.UInt64} + * @export */ jspb.arith.UInt64.prototype.leftShift = function() { var lo = this.lo << 1; @@ -106,6 +112,7 @@ jspb.arith.UInt64.prototype.leftShift = function() { /** * Test the MSB. * @return {boolean} + * @export */ jspb.arith.UInt64.prototype.msb = function() { return !!(this.hi & 0x80000000); @@ -115,6 +122,7 @@ jspb.arith.UInt64.prototype.msb = function() { /** * Test the LSB. * @return {boolean} + * @export */ jspb.arith.UInt64.prototype.lsb = function() { return !!(this.lo & 1); @@ -124,6 +132,7 @@ jspb.arith.UInt64.prototype.lsb = function() { /** * Test whether this number is zero. * @return {boolean} + * @export */ jspb.arith.UInt64.prototype.zero = function() { return this.lo == 0 && this.hi == 0; @@ -134,6 +143,7 @@ jspb.arith.UInt64.prototype.zero = function() { * Add two 64-bit numbers to produce a 64-bit number. * @param {!jspb.arith.UInt64} other * @return {!jspb.arith.UInt64} + * @export */ jspb.arith.UInt64.prototype.add = function(other) { var lo = ((this.lo + other.lo) & 0xffffffff) >>> 0; @@ -148,6 +158,7 @@ jspb.arith.UInt64.prototype.add = function(other) { * Subtract two 64-bit numbers to produce a 64-bit number. * @param {!jspb.arith.UInt64} other * @return {!jspb.arith.UInt64} + * @export */ jspb.arith.UInt64.prototype.sub = function(other) { var lo = ((this.lo - other.lo) & 0xffffffff) >>> 0; @@ -163,6 +174,7 @@ jspb.arith.UInt64.prototype.sub = function(other) { * @param {number} a The first integer: must be in [0, 2^32-1). * @param {number} b The second integer: must be in [0, 2^32-1). * @return {!jspb.arith.UInt64} + * @export */ jspb.arith.UInt64.mul32x32 = function(a, b) { // Directly multiplying two 32-bit numbers may produce up to 64 bits of @@ -204,6 +216,7 @@ jspb.arith.UInt64.mul32x32 = function(a, b) { * truncate the top 32 bits. * @param {number} a The multiplier. * @return {!jspb.arith.UInt64} + * @export */ jspb.arith.UInt64.prototype.mul = function(a) { // Produce two parts: at bits 0-63, and 32-95. @@ -223,6 +236,7 @@ jspb.arith.UInt64.prototype.mul = function(a) { * @param {number} _divisor * @return {Array} array of [quotient, remainder], * unless divisor is 0, in which case an empty array is returned. + * @export */ jspb.arith.UInt64.prototype.div = function(_divisor) { if (_divisor == 0) { @@ -264,6 +278,7 @@ jspb.arith.UInt64.prototype.div = function(_divisor) { * Convert a 64-bit number to a string. * @return {string} * @override + * @export */ jspb.arith.UInt64.prototype.toString = function() { var result = ''; @@ -285,6 +300,7 @@ jspb.arith.UInt64.prototype.toString = function() { * Parse a string into a 64-bit number. Returns `null` on a parse error. * @param {string} s * @return {?jspb.arith.UInt64} + * @export */ jspb.arith.UInt64.fromString = function(s) { var result = new jspb.arith.UInt64(0, 0); @@ -305,6 +321,7 @@ jspb.arith.UInt64.fromString = function(s) { /** * Make a copy of the uint64. * @return {!jspb.arith.UInt64} + * @export */ jspb.arith.UInt64.prototype.clone = function() { return new jspb.arith.UInt64(this.lo, this.hi); @@ -324,16 +341,19 @@ jspb.arith.UInt64.prototype.clone = function() { * @param {number} lo The low 32 bits. * @param {number} hi The high 32 bits. * @constructor + * @export */ jspb.arith.Int64 = function(lo, hi) { /** * The low 32 bits. - * @public {number} + * @type {number} + * @export */ this.lo = lo; /** * The high 32 bits. - * @public {number} + * @type {number} + * @export */ this.hi = hi; }; @@ -343,6 +363,7 @@ jspb.arith.Int64 = function(lo, hi) { * Add two 64-bit numbers to produce a 64-bit number. * @param {!jspb.arith.Int64} other * @return {!jspb.arith.Int64} + * @export */ jspb.arith.Int64.prototype.add = function(other) { var lo = ((this.lo + other.lo) & 0xffffffff) >>> 0; @@ -357,6 +378,7 @@ jspb.arith.Int64.prototype.add = function(other) { * Subtract two 64-bit numbers to produce a 64-bit number. * @param {!jspb.arith.Int64} other * @return {!jspb.arith.Int64} + * @export */ jspb.arith.Int64.prototype.sub = function(other) { var lo = ((this.lo - other.lo) & 0xffffffff) >>> 0; @@ -370,6 +392,7 @@ jspb.arith.Int64.prototype.sub = function(other) { /** * Make a copy of the int64. * @return {!jspb.arith.Int64} + * @export */ jspb.arith.Int64.prototype.clone = function() { return new jspb.arith.Int64(this.lo, this.hi); @@ -380,6 +403,7 @@ jspb.arith.Int64.prototype.clone = function() { * Convert a 64-bit number to a string. * @return {string} * @override + * @export */ jspb.arith.Int64.prototype.toString = function() { // If the number is negative, find its twos-complement inverse. @@ -396,6 +420,7 @@ jspb.arith.Int64.prototype.toString = function() { * Parse a string into a 64-bit number. Returns `null` on a parse error. * @param {string} s * @return {?jspb.arith.Int64} + * @export */ jspb.arith.Int64.fromString = function(s) { var hasNegative = (s.length > 0 && s[0] == '-'); diff --git a/binary/constants.js b/binary/constants.js index d2e62e5..0559138 100644 --- a/binary/constants.js +++ b/binary/constants.js @@ -63,12 +63,14 @@ goog.forwardDeclare('jsprotolib.BinaryExtension'); /** * Base interface class for all const messages. * @interface + * @export */ jspb.ConstBinaryMessage = function() {}; /** * Generate a debug string for this proto that is in proto2 text format. * @return {string} The debug string. + * @export */ jspb.ConstBinaryMessage.prototype.toDebugString; @@ -86,6 +88,7 @@ jspb.ConstBinaryMessage.prototype.toDebugStringInternal; * doing so on a widely-used interface defeats dead-code elimination. * @interface * @extends {jspb.ConstBinaryMessage} + * @export */ jspb.BinaryMessage = function() {}; @@ -94,6 +97,7 @@ jspb.BinaryMessage = function() {}; * The types convertible to Uint8Arrays. Strings are assumed to be * base64-encoded. * @typedef {ArrayBuffer|Uint8Array|Array|string} + * @export */ jspb.ByteSource; @@ -101,6 +105,7 @@ jspb.ByteSource; /** * A scalar field in jspb can be a boolean, number, or string. * @typedef {boolean|number|string} + * @export */ jspb.ScalarFieldType; @@ -111,6 +116,7 @@ jspb.ScalarFieldType; !Array| !Array| !Array} +* @export */ jspb.RepeatedFieldType; @@ -124,6 +130,7 @@ jspb.RepeatedFieldType; !jspb.ConstBinaryMessage| !jspb.BinaryMessage| !jsprotolib.BinaryExtension} + * @export */ jspb.AnyFieldType; @@ -131,6 +138,7 @@ jspb.AnyFieldType; /** * A builder function creates an instance of a message object. * @typedef {function():!jspb.BinaryMessage} + * @export */ jspb.BuilderFunction; @@ -138,6 +146,7 @@ jspb.BuilderFunction; /** * A cloner function creates a deep copy of a message object. * @typedef {function(jspb.ConstBinaryMessage):jspb.BinaryMessage} + * @export */ jspb.ClonerFunction; @@ -145,6 +154,7 @@ jspb.ClonerFunction; /** * A recycler function destroys an instance of a message object. * @typedef {function(!jspb.BinaryMessage):void} + * @export */ jspb.RecyclerFunction; @@ -152,6 +162,7 @@ jspb.RecyclerFunction; /** * A reader function initializes a message using data from a BinaryReader. * @typedef {function(!jspb.BinaryMessage, !jspb.BinaryReader):void} + * @export */ jspb.ReaderFunction; @@ -160,6 +171,7 @@ jspb.ReaderFunction; * A writer function serializes a message to a BinaryWriter. * @typedef {function((!jspb.Message|!jspb.ConstBinaryMessage), * !jspb.BinaryWriter):void} + * @export */ jspb.WriterFunction; @@ -169,6 +181,7 @@ jspb.WriterFunction; * message and returns either the pruned message or null if the entire message * was pruned away. * @typedef {function(?jspb.BinaryMessage):?jspb.BinaryMessage} + * @export */ jspb.PrunerFunction; @@ -177,6 +190,7 @@ jspb.PrunerFunction; * A comparer function returns true if two protos are equal. * @typedef {function(?jspb.ConstBinaryMessage, * ?jspb.ConstBinaryMessage):boolean} + * @export */ jspb.ComparerFunction; @@ -184,6 +198,7 @@ jspb.ComparerFunction; /** * Field type codes, taken from proto2/public/wire_format_lite.h. * @enum {number} + * @export */ jspb.BinaryConstants.FieldType = { INVALID: -1, @@ -216,6 +231,7 @@ jspb.BinaryConstants.FieldType = { /** * Wire-format type codes, taken from proto2/public/wire_format_lite.h. * @enum {number} + * @export */ jspb.BinaryConstants.WireType = { INVALID: -1, @@ -232,6 +248,7 @@ jspb.BinaryConstants.WireType = { * Translates field type to wire type. * @param {jspb.BinaryConstants.FieldType} fieldType * @return {jspb.BinaryConstants.WireType} + * @export */ jspb.BinaryConstants.FieldTypeToWireType = function(fieldType) { var fieldTypes = jspb.BinaryConstants.FieldType; @@ -275,6 +292,7 @@ jspb.BinaryConstants.FieldTypeToWireType = function(fieldType) { /** * Flag to indicate a missing field. * @const {number} + * @export */ jspb.BinaryConstants.INVALID_FIELD_NUMBER = -1; @@ -282,6 +300,7 @@ jspb.BinaryConstants.INVALID_FIELD_NUMBER = -1; /** * The smallest denormal float32 value. * @const {number} + * @export */ jspb.BinaryConstants.FLOAT32_EPS = 1.401298464324817e-45; @@ -289,6 +308,7 @@ jspb.BinaryConstants.FLOAT32_EPS = 1.401298464324817e-45; /** * The smallest normal float64 value. * @const {number} + * @export */ jspb.BinaryConstants.FLOAT32_MIN = 1.1754943508222875e-38; @@ -296,6 +316,7 @@ jspb.BinaryConstants.FLOAT32_MIN = 1.1754943508222875e-38; /** * The largest finite float32 value. * @const {number} + * @export */ jspb.BinaryConstants.FLOAT32_MAX = 3.4028234663852886e+38; @@ -303,6 +324,7 @@ jspb.BinaryConstants.FLOAT32_MAX = 3.4028234663852886e+38; /** * The smallest denormal float64 value. * @const {number} + * @export */ jspb.BinaryConstants.FLOAT64_EPS = 5e-324; @@ -310,6 +332,7 @@ jspb.BinaryConstants.FLOAT64_EPS = 5e-324; /** * The smallest normal float64 value. * @const {number} + * @export */ jspb.BinaryConstants.FLOAT64_MIN = 2.2250738585072014e-308; @@ -317,6 +340,7 @@ jspb.BinaryConstants.FLOAT64_MIN = 2.2250738585072014e-308; /** * The largest finite float64 value. * @const {number} + * @export */ jspb.BinaryConstants.FLOAT64_MAX = 1.7976931348623157e+308; @@ -324,6 +348,7 @@ jspb.BinaryConstants.FLOAT64_MAX = 1.7976931348623157e+308; /** * Convenience constant equal to 2^20. * @const {number} + * @export */ jspb.BinaryConstants.TWO_TO_20 = 1048576; @@ -331,6 +356,7 @@ jspb.BinaryConstants.TWO_TO_20 = 1048576; /** * Convenience constant equal to 2^23. * @const {number} + * @export */ jspb.BinaryConstants.TWO_TO_23 = 8388608; @@ -338,6 +364,7 @@ jspb.BinaryConstants.TWO_TO_23 = 8388608; /** * Convenience constant equal to 2^31. * @const {number} + * @export */ jspb.BinaryConstants.TWO_TO_31 = 2147483648; @@ -345,6 +372,7 @@ jspb.BinaryConstants.TWO_TO_31 = 2147483648; /** * Convenience constant equal to 2^32. * @const {number} + * @export */ jspb.BinaryConstants.TWO_TO_32 = 4294967296; @@ -352,6 +380,7 @@ jspb.BinaryConstants.TWO_TO_32 = 4294967296; /** * Convenience constant equal to 2^52. * @const {number} + * @export */ jspb.BinaryConstants.TWO_TO_52 = 4503599627370496; @@ -359,6 +388,7 @@ jspb.BinaryConstants.TWO_TO_52 = 4503599627370496; /** * Convenience constant equal to 2^63. * @const {number} + * @export */ jspb.BinaryConstants.TWO_TO_63 = 9223372036854775808; @@ -366,6 +396,7 @@ jspb.BinaryConstants.TWO_TO_63 = 9223372036854775808; /** * Convenience constant equal to 2^64. * @const {number} + * @export */ jspb.BinaryConstants.TWO_TO_64 = 18446744073709551616; @@ -373,5 +404,6 @@ jspb.BinaryConstants.TWO_TO_64 = 18446744073709551616; /** * Eight-character string of zeros, used as the default 64-bit hash value. * @const {string} + * @export */ jspb.BinaryConstants.ZERO_HASH = '\0\0\0\0\0\0\0\0'; diff --git a/binary/decoder.js b/binary/decoder.js index eaa0082..d52a915 100644 --- a/binary/decoder.js +++ b/binary/decoder.js @@ -62,6 +62,7 @@ goog.require('jspb.utils'); * we'll throw an assertion if we go off the end of the block. * @constructor * @struct + * @export */ jspb.BinaryDecoder = function(opt_bytes, opt_start, opt_length) { /** @@ -107,6 +108,14 @@ jspb.BinaryDecoder = function(opt_bytes, opt_start, opt_length) { jspb.BinaryDecoder.instanceCache_ = []; +/** + * @return {number} + * @export + */ +jspb.BinaryDecoder.getInstanceCacheLength = function() { + return jspb.BinaryDecoder.instanceCache_.length; +} + /** * Pops an instance off the instance cache, or creates one if the cache is * empty. @@ -115,6 +124,7 @@ jspb.BinaryDecoder.instanceCache_ = []; * @param {number=} opt_length The optional length of the block to read - * we'll throw an assertion if we go off the end of the block. * @return {!jspb.BinaryDecoder} + * @export */ jspb.BinaryDecoder.alloc = function(opt_bytes, opt_start, opt_length) { if (jspb.BinaryDecoder.instanceCache_.length) { @@ -131,6 +141,7 @@ jspb.BinaryDecoder.alloc = function(opt_bytes, opt_start, opt_length) { /** * Puts this instance back in the instance cache. + * @export */ jspb.BinaryDecoder.prototype.free = function() { this.clear(); @@ -143,6 +154,7 @@ jspb.BinaryDecoder.prototype.free = function() { /** * Makes a copy of this decoder. * @return {!jspb.BinaryDecoder} + * @export */ jspb.BinaryDecoder.prototype.clone = function() { return jspb.BinaryDecoder.alloc( @@ -152,6 +164,7 @@ jspb.BinaryDecoder.prototype.clone = function() { /** * Clears the decoder. + * @export */ jspb.BinaryDecoder.prototype.clear = function() { this.bytes_ = null; @@ -165,6 +178,7 @@ jspb.BinaryDecoder.prototype.clear = function() { /** * Returns the raw buffer. * @return {?Uint8Array} The raw buffer. + * @export */ jspb.BinaryDecoder.prototype.getBuffer = function() { return this.bytes_; @@ -177,6 +191,7 @@ jspb.BinaryDecoder.prototype.getBuffer = function() { * @param {number=} opt_start The optional offset to start reading at. * @param {number=} opt_length The optional length of the block to read - * we'll throw an assertion if we go off the end of the block. + * @export */ jspb.BinaryDecoder.prototype.setBlock = function(data, opt_start, opt_length) { this.bytes_ = jspb.utils.byteSourceToUint8Array(data); @@ -189,6 +204,7 @@ jspb.BinaryDecoder.prototype.setBlock = function(data, opt_start, opt_length) { /** * @return {number} + * @export */ jspb.BinaryDecoder.prototype.getEnd = function() { return this.end_; @@ -197,6 +213,7 @@ jspb.BinaryDecoder.prototype.getEnd = function() { /** * @param {number} end + * @export */ jspb.BinaryDecoder.prototype.setEnd = function(end) { this.end_ = end; @@ -205,6 +222,7 @@ jspb.BinaryDecoder.prototype.setEnd = function(end) { /** * Moves the read cursor back to the start of the block. + * @export */ jspb.BinaryDecoder.prototype.reset = function() { this.cursor_ = this.start_; @@ -214,6 +232,7 @@ jspb.BinaryDecoder.prototype.reset = function() { /** * Returns the internal read cursor. * @return {number} The internal read cursor. + * @export */ jspb.BinaryDecoder.prototype.getCursor = function() { return this.cursor_; @@ -223,6 +242,7 @@ jspb.BinaryDecoder.prototype.getCursor = function() { /** * Returns the internal read cursor. * @param {number} cursor The new cursor. + * @export */ jspb.BinaryDecoder.prototype.setCursor = function(cursor) { this.cursor_ = cursor; @@ -232,6 +252,7 @@ jspb.BinaryDecoder.prototype.setCursor = function(cursor) { /** * Advances the stream cursor by the given number of bytes. * @param {number} count The number of bytes to advance by. + * @export */ jspb.BinaryDecoder.prototype.advance = function(count) { this.cursor_ += count; @@ -242,6 +263,7 @@ jspb.BinaryDecoder.prototype.advance = function(count) { /** * Returns true if this decoder is at the end of the block. * @return {boolean} + * @export */ jspb.BinaryDecoder.prototype.atEnd = function() { return this.cursor_ == this.end_; @@ -251,6 +273,7 @@ jspb.BinaryDecoder.prototype.atEnd = function() { /** * Returns true if this decoder is at the end of the block. * @return {boolean} + * @export */ jspb.BinaryDecoder.prototype.pastEnd = function() { return this.cursor_ > this.end_; @@ -260,6 +283,7 @@ jspb.BinaryDecoder.prototype.pastEnd = function() { /** * Returns true if this decoder encountered an error due to corrupt data. * @return {boolean} + * @export */ jspb.BinaryDecoder.prototype.getError = function() { return this.error_ || (this.cursor_ < 0) || (this.cursor_ > this.end_); @@ -283,6 +307,7 @@ jspb.BinaryDecoder.prototype.getError = function() { * the result value, takes parameters (lowBits, highBits). * @return {T} * @template T + * @export */ jspb.BinaryDecoder.prototype.readSplitVarint64 = function(convert) { var temp = 128; @@ -339,6 +364,7 @@ jspb.BinaryDecoder.prototype.readSplitVarint64 = function(convert) { * the result value, takes parameters (lowBits, highBits). * @return {T} * @template T + * @export */ jspb.BinaryDecoder.prototype.readSplitZigzagVarint64 = function(convert) { return this.readSplitVarint64(function(low, high) { @@ -356,6 +382,7 @@ jspb.BinaryDecoder.prototype.readSplitZigzagVarint64 = function(convert) { * the result value, takes parameters (lowBits, highBits). * @return {T} * @template T + * @export */ jspb.BinaryDecoder.prototype.readSplitFixed64 = function(convert) { var bytes = this.bytes_; @@ -373,6 +400,7 @@ jspb.BinaryDecoder.prototype.readSplitFixed64 = function(convert) { /** * Skips over a varint in the block without decoding it. + * @export */ jspb.BinaryDecoder.prototype.skipVarint = function() { while (this.bytes_[this.cursor_] & 0x80) { @@ -386,6 +414,7 @@ jspb.BinaryDecoder.prototype.skipVarint = function() { * Skips backwards over a varint in the block - to do this correctly, we have * to know the value we're skipping backwards over or things are ambiguous. * @param {number} value The varint value to unskip. + * @export */ jspb.BinaryDecoder.prototype.unskipVarint = function(value) { while (value > 128) { @@ -413,6 +442,7 @@ jspb.BinaryDecoder.prototype.unskipVarint = function(value) { * https://developers.google.com/protocol-buffers/docs/encoding * * @return {number} The decoded unsigned 32-bit varint. + * @export */ jspb.BinaryDecoder.prototype.readUnsignedVarint32 = function() { var temp; @@ -479,6 +509,7 @@ jspb.BinaryDecoder.prototype.readUnsignedVarint32 = function() { * Coerces the output of readUnsignedVarint32 to an int32. * * @return {number} The decoded signed 32-bit varint. + * @export */ jspb.BinaryDecoder.prototype.readSignedVarint32 = function() { @@ -507,6 +538,7 @@ jspb.BinaryDecoder.prototype.readSignedVarint32 = * Reads a 32-bit signed variant and returns its value as a string. * * @return {string} The decoded signed 32-bit varint as a string. + * @export */ jspb.BinaryDecoder.prototype.readSignedVarint32String = function() { // 32-bit integers fit in JavaScript numbers without loss of precision, so @@ -525,6 +557,7 @@ jspb.BinaryDecoder.prototype.readSignedVarint32String = function() { * format, see https://developers.google.com/protocol-buffers/docs/encoding * * @return {number} The decoded signed, zigzag-encoded 32-bit varint. + * @export */ jspb.BinaryDecoder.prototype.readZigzagVarint32 = function() { var result = this.readUnsignedVarint32(); @@ -539,6 +572,7 @@ jspb.BinaryDecoder.prototype.readZigzagVarint32 = function() { * * @return {number} The decoded unsigned varint. Precision will be lost if the * integer exceeds 2^53. + * @export */ jspb.BinaryDecoder.prototype.readUnsignedVarint64 = function() { return this.readSplitVarint64(jspb.utils.joinUint64); @@ -550,6 +584,7 @@ jspb.BinaryDecoder.prototype.readUnsignedVarint64 = function() { * as a decimal string. * * @return {string} The decoded unsigned varint as a decimal string. + * @export */ jspb.BinaryDecoder.prototype.readUnsignedVarint64String = function() { return this.readSplitVarint64(jspb.utils.joinUnsignedDecimalString); @@ -563,6 +598,7 @@ jspb.BinaryDecoder.prototype.readUnsignedVarint64String = function() { * * @return {number} The decoded signed varint. Precision will be lost if the * integer exceeds 2^53. + * @export */ jspb.BinaryDecoder.prototype.readSignedVarint64 = function() { return this.readSplitVarint64(jspb.utils.joinInt64); @@ -574,6 +610,7 @@ jspb.BinaryDecoder.prototype.readSignedVarint64 = function() { * as a decimal string. * * @return {string} The decoded signed varint as a decimal string. + * @export */ jspb.BinaryDecoder.prototype.readSignedVarint64String = function() { return this.readSplitVarint64(jspb.utils.joinSignedDecimalString); @@ -592,6 +629,7 @@ jspb.BinaryDecoder.prototype.readSignedVarint64String = function() { * * @return {number} The decoded zigzag varint. Precision will be lost if the * integer exceeds 2^53. + * @export */ jspb.BinaryDecoder.prototype.readZigzagVarint64 = function() { return this.readSplitVarint64(jspb.utils.joinZigzag64); @@ -608,6 +646,7 @@ jspb.BinaryDecoder.prototype.readZigzagVarint64 = function() { * format, see https://developers.google.com/protocol-buffers/docs/encoding * * @return {string} The decoded zigzag varint in hash64 format. + * @export */ jspb.BinaryDecoder.prototype.readZigzagVarintHash64 = function() { return this.readSplitZigzagVarint64(jspb.utils.joinHash64); @@ -624,6 +663,7 @@ jspb.BinaryDecoder.prototype.readZigzagVarintHash64 = function() { * * @return {string} The decoded signed, zigzag-encoded 64-bit varint as a * string. + * @export */ jspb.BinaryDecoder.prototype.readZigzagVarint64String = function() { return this.readSplitZigzagVarint64(jspb.utils.joinSignedDecimalString); @@ -634,6 +674,7 @@ jspb.BinaryDecoder.prototype.readZigzagVarint64String = function() { * Reads a raw unsigned 8-bit integer from the binary stream. * * @return {number} The unsigned 8-bit integer read from the binary stream. + * @export */ jspb.BinaryDecoder.prototype.readUint8 = function() { var a = this.bytes_[this.cursor_ + 0]; @@ -647,6 +688,7 @@ jspb.BinaryDecoder.prototype.readUint8 = function() { * Reads a raw unsigned 16-bit integer from the binary stream. * * @return {number} The unsigned 16-bit integer read from the binary stream. + * @export */ jspb.BinaryDecoder.prototype.readUint16 = function() { var a = this.bytes_[this.cursor_ + 0]; @@ -661,6 +703,7 @@ jspb.BinaryDecoder.prototype.readUint16 = function() { * Reads a raw unsigned 32-bit integer from the binary stream. * * @return {number} The unsigned 32-bit integer read from the binary stream. + * @export */ jspb.BinaryDecoder.prototype.readUint32 = function() { var a = this.bytes_[this.cursor_ + 0]; @@ -680,6 +723,7 @@ jspb.BinaryDecoder.prototype.readUint32 = function() { * * @return {number} The unsigned 64-bit integer read from the binary stream. * Precision will be lost if the integer exceeds 2^53. + * @export */ jspb.BinaryDecoder.prototype.readUint64 = function() { var bitsLow = this.readUint32(); @@ -694,6 +738,7 @@ jspb.BinaryDecoder.prototype.readUint64 = function() { * precision lost if the absolute value of the integer is larger than 2^53. * * @return {string} The unsigned 64-bit integer read from the binary stream. + * @export */ jspb.BinaryDecoder.prototype.readUint64String = function() { var bitsLow = this.readUint32(); @@ -706,6 +751,7 @@ jspb.BinaryDecoder.prototype.readUint64String = function() { * Reads a raw signed 8-bit integer from the binary stream. * * @return {number} The signed 8-bit integer read from the binary stream. + * @export */ jspb.BinaryDecoder.prototype.readInt8 = function() { var a = this.bytes_[this.cursor_ + 0]; @@ -719,6 +765,7 @@ jspb.BinaryDecoder.prototype.readInt8 = function() { * Reads a raw signed 16-bit integer from the binary stream. * * @return {number} The signed 16-bit integer read from the binary stream. + * @export */ jspb.BinaryDecoder.prototype.readInt16 = function() { var a = this.bytes_[this.cursor_ + 0]; @@ -733,6 +780,7 @@ jspb.BinaryDecoder.prototype.readInt16 = function() { * Reads a raw signed 32-bit integer from the binary stream. * * @return {number} The signed 32-bit integer read from the binary stream. + * @export */ jspb.BinaryDecoder.prototype.readInt32 = function() { var a = this.bytes_[this.cursor_ + 0]; @@ -752,6 +800,7 @@ jspb.BinaryDecoder.prototype.readInt32 = function() { * * @return {number} The signed 64-bit integer read from the binary stream. * Precision will be lost if the integer exceeds 2^53. + * @export */ jspb.BinaryDecoder.prototype.readInt64 = function() { var bitsLow = this.readUint32(); @@ -766,6 +815,7 @@ jspb.BinaryDecoder.prototype.readInt64 = function() { * * @return {string} The signed 64-bit integer read from the binary stream. * Precision will be lost if the integer exceeds 2^53. + * @export */ jspb.BinaryDecoder.prototype.readInt64String = function() { var bitsLow = this.readUint32(); @@ -779,6 +829,7 @@ jspb.BinaryDecoder.prototype.readInt64String = function() { * temporary buffer to realign the data. * * @return {number} The float read from the binary stream. + * @export */ jspb.BinaryDecoder.prototype.readFloat = function() { var bitsLow = this.readUint32(); @@ -792,6 +843,7 @@ jspb.BinaryDecoder.prototype.readFloat = function() { * temporary buffer to realign the data. * * @return {number} The double read from the binary stream. + * @export */ jspb.BinaryDecoder.prototype.readDouble = function() { var bitsLow = this.readUint32(); @@ -803,6 +855,7 @@ jspb.BinaryDecoder.prototype.readDouble = function() { /** * Reads a boolean value from the binary stream. * @return {boolean} The boolean read from the binary stream. + * @export */ jspb.BinaryDecoder.prototype.readBool = function() { return !!this.bytes_[this.cursor_++]; @@ -813,6 +866,7 @@ jspb.BinaryDecoder.prototype.readBool = function() { * Reads an enum value from the binary stream, which are always encoded as * signed varints. * @return {number} The enum value read from the binary stream. + * @export */ jspb.BinaryDecoder.prototype.readEnum = function() { return this.readSignedVarint32(); @@ -826,6 +880,7 @@ jspb.BinaryDecoder.prototype.readEnum = function() { * (http://en.wikipedia.org/wiki/UTF-8). * @param {number} length The length of the string to read. * @return {string} The decoded string. + * @export */ jspb.BinaryDecoder.prototype.readString = function(length) { var bytes = this.bytes_; @@ -885,6 +940,7 @@ jspb.BinaryDecoder.prototype.readString = function(length) { * Reads and parses a UTF-8 encoded unicode string (with length prefix) from * the stream. * @return {string} The decoded string. + * @export */ jspb.BinaryDecoder.prototype.readStringWithLength = function() { var length = this.readUnsignedVarint32(); @@ -898,6 +954,7 @@ jspb.BinaryDecoder.prototype.readStringWithLength = function() { * @param {number} length The number of bytes to read. * @return {!Uint8Array} The decoded block of bytes, or an empty block if the * length was invalid. + * @export */ jspb.BinaryDecoder.prototype.readBytes = function(length) { if (length < 0 || this.cursor_ + length > this.bytes_.length) { @@ -919,6 +976,7 @@ jspb.BinaryDecoder.prototype.readBytes = function(length) { * Unicode string for use as a hash table key. * * @return {string} The hash value. + * @export */ jspb.BinaryDecoder.prototype.readVarintHash64 = function() { return this.readSplitVarint64(jspb.utils.joinHash64); @@ -930,6 +988,7 @@ jspb.BinaryDecoder.prototype.readVarintHash64 = function() { * 8-character Unicode string for use as a hash table key. * * @return {string} The hash value. + * @export */ jspb.BinaryDecoder.prototype.readFixedHash64 = function() { var bytes = this.bytes_; diff --git a/binary/decoder_test.js b/binary/decoder_test.js index 20766b2..12e809b 100644 --- a/binary/decoder_test.js +++ b/binary/decoder_test.js @@ -167,7 +167,7 @@ describe('binaryDecoderTest', () => { // cache. jspb.BinaryDecoder.alloc().free(); - expect(jspb.BinaryDecoder.instanceCache_.length).toEqual(1); + expect(jspb.BinaryDecoder.getInstanceCacheLength()).toEqual(1); // Allocating and then freeing three decoders should leave us with three in // the cache. @@ -179,7 +179,7 @@ describe('binaryDecoderTest', () => { decoder2.free(); decoder3.free(); - expect(jspb.BinaryDecoder.instanceCache_.length).toEqual(3); + expect(jspb.BinaryDecoder.getInstanceCacheLength()).toEqual(3); }); @@ -229,8 +229,9 @@ describe('binaryDecoderTest', () => { (bitsLow >>> 0).toString(16)}`; } function hexJoinHash(hash64) { - jspb.utils.splitHash64(hash64); - return hexJoin(jspb.utils.split64Low, jspb.utils.split64High); + jspb.utils.splitHash64(hash64, true); + + return hexJoin(jspb.utils.getSplit64Low(), jspb.utils.getSplit64High()); } expect(decoder.readSplitVarint64(hexJoin)).toEqual(hexJoinHash(hashA)); @@ -293,7 +294,7 @@ describe('binaryDecoderTest', () => { } function hexJoinHash(hash64) { jspb.utils.splitHash64(hash64); - return hexJoin(jspb.utils.split64Low, jspb.utils.split64High); + return hexJoin(jspb.utils.getSplit64Low(), jspb.utils.getSplit64High()); } expect(decoder.readSplitZigzagVarint64(hexJoin)) diff --git a/binary/encoder.js b/binary/encoder.js index 6a47e24..7a7cae6 100644 --- a/binary/encoder.js +++ b/binary/encoder.js @@ -49,6 +49,7 @@ goog.require('jspb.utils'); * * @constructor * @struct + * @export */ jspb.BinaryEncoder = function() { /** @private {!Array} */ @@ -58,6 +59,7 @@ jspb.BinaryEncoder = function() { /** * @return {number} + * @export */ jspb.BinaryEncoder.prototype.length = function() { return this.buffer_.length; @@ -66,6 +68,7 @@ jspb.BinaryEncoder.prototype.length = function() { /** * @return {!Array} + * @export */ jspb.BinaryEncoder.prototype.end = function() { var buffer = this.buffer_; @@ -79,6 +82,7 @@ jspb.BinaryEncoder.prototype.end = function() { * varint representation and stores it in the buffer. * @param {number} lowBits The low 32 bits of the int. * @param {number} highBits The high 32 bits of the int. + * @export */ jspb.BinaryEncoder.prototype.writeSplitVarint64 = function(lowBits, highBits) { jspb.asserts.assert(lowBits == Math.floor(lowBits)); @@ -104,6 +108,7 @@ jspb.BinaryEncoder.prototype.writeSplitVarint64 = function(lowBits, highBits) { * fixed representation and stores it in the buffer. * @param {number} lowBits The low 32 bits of the int. * @param {number} highBits The high 32 bits of the int. + * @export */ jspb.BinaryEncoder.prototype.writeSplitFixed64 = function(lowBits, highBits) { jspb.asserts.assert(lowBits == Math.floor(lowBits)); @@ -121,6 +126,7 @@ jspb.BinaryEncoder.prototype.writeSplitFixed64 = function(lowBits, highBits) { * Encodes a 32-bit unsigned integer into its wire-format varint representation * and stores it in the buffer. * @param {number} value The integer to convert. + * @export */ jspb.BinaryEncoder.prototype.writeUnsignedVarint32 = function(value) { jspb.asserts.assert(value == Math.floor(value)); @@ -140,6 +146,7 @@ jspb.BinaryEncoder.prototype.writeUnsignedVarint32 = function(value) { * Encodes a 32-bit signed integer into its wire-format varint representation * and stores it in the buffer. * @param {number} value The integer to convert. + * @export */ jspb.BinaryEncoder.prototype.writeSignedVarint32 = function(value) { jspb.asserts.assert(value == Math.floor(value)); @@ -170,13 +177,14 @@ jspb.BinaryEncoder.prototype.writeSignedVarint32 = function(value) { * and stores it in the buffer. Integers that are not representable in 64 bits * will be truncated. * @param {number} value The integer to convert. + * @export */ jspb.BinaryEncoder.prototype.writeUnsignedVarint64 = function(value) { jspb.asserts.assert(value == Math.floor(value)); jspb.asserts.assert( (value >= 0) && (value < jspb.BinaryConstants.TWO_TO_64)); jspb.utils.splitInt64(value); - this.writeSplitVarint64(jspb.utils.split64Low, jspb.utils.split64High); + this.writeSplitVarint64(jspb.utils.getSplit64Low(), jspb.utils.getSplit64High()); }; @@ -185,6 +193,7 @@ jspb.BinaryEncoder.prototype.writeUnsignedVarint64 = function(value) { * and stores it in the buffer. Integers that are not representable in 64 bits * will be truncated. * @param {number} value The integer to convert. + * @export */ jspb.BinaryEncoder.prototype.writeSignedVarint64 = function(value) { jspb.asserts.assert(value == Math.floor(value)); @@ -192,7 +201,7 @@ jspb.BinaryEncoder.prototype.writeSignedVarint64 = function(value) { (value >= -jspb.BinaryConstants.TWO_TO_63) && (value < jspb.BinaryConstants.TWO_TO_63)); jspb.utils.splitInt64(value); - this.writeSplitVarint64(jspb.utils.split64Low, jspb.utils.split64High); + this.writeSplitVarint64(jspb.utils.getSplit64Low(), jspb.utils.getSplit64High()); }; @@ -200,6 +209,7 @@ jspb.BinaryEncoder.prototype.writeSignedVarint64 = function(value) { * Encodes a JavaScript integer into its wire-format, zigzag-encoded varint * representation and stores it in the buffer. * @param {number} value The integer to convert. + * @export */ jspb.BinaryEncoder.prototype.writeZigzagVarint32 = function(value) { jspb.asserts.assert(value == Math.floor(value)); @@ -215,6 +225,7 @@ jspb.BinaryEncoder.prototype.writeZigzagVarint32 = function(value) { * representation and stores it in the buffer. Integers not representable in 64 * bits will be truncated. * @param {number} value The integer to convert. + * @export */ jspb.BinaryEncoder.prototype.writeZigzagVarint64 = function(value) { jspb.asserts.assert(value == Math.floor(value)); @@ -222,7 +233,7 @@ jspb.BinaryEncoder.prototype.writeZigzagVarint64 = function(value) { (value >= -jspb.BinaryConstants.TWO_TO_63) && (value < jspb.BinaryConstants.TWO_TO_63)); jspb.utils.splitZigzag64(value); - this.writeSplitVarint64(jspb.utils.split64Low, jspb.utils.split64High); + this.writeSplitVarint64(jspb.utils.getSplit64Low(), jspb.utils.getSplit64High()); }; @@ -231,6 +242,7 @@ jspb.BinaryEncoder.prototype.writeZigzagVarint64 = function(value) { * varint representation and stores it in the buffer. Integers not representable * in 64 bits will be truncated. * @param {string} value The integer to convert. + * @export */ jspb.BinaryEncoder.prototype.writeZigzagVarint64String = function(value) { this.writeZigzagVarintHash64(jspb.utils.decimalStringToHash64(value)); @@ -241,12 +253,13 @@ jspb.BinaryEncoder.prototype.writeZigzagVarint64String = function(value) { * Writes a 64-bit hash string (8 characters @ 8 bits of data each) to the * buffer as a zigzag varint. * @param {string} hash The hash to write. + * @export */ jspb.BinaryEncoder.prototype.writeZigzagVarintHash64 = function(hash) { var self = this; jspb.utils.splitHash64(hash); jspb.utils.toZigzag64( - jspb.utils.split64Low, jspb.utils.split64High, function(lo, hi) { + jspb.utils.getSplit64Low(), jspb.utils.getSplit64High(), function(lo, hi) { self.writeSplitVarint64(lo >>> 0, hi >>> 0); }); }; @@ -256,6 +269,7 @@ jspb.BinaryEncoder.prototype.writeZigzagVarintHash64 = function(hash) { * Writes an 8-bit unsigned integer to the buffer. Numbers outside the range * [0,2^8) will be truncated. * @param {number} value The value to write. + * @export */ jspb.BinaryEncoder.prototype.writeUint8 = function(value) { jspb.asserts.assert(value == Math.floor(value)); @@ -268,6 +282,7 @@ jspb.BinaryEncoder.prototype.writeUint8 = function(value) { * Writes a 16-bit unsigned integer to the buffer. Numbers outside the * range [0,2^16) will be truncated. * @param {number} value The value to write. + * @export */ jspb.BinaryEncoder.prototype.writeUint16 = function(value) { jspb.asserts.assert(value == Math.floor(value)); @@ -281,6 +296,7 @@ jspb.BinaryEncoder.prototype.writeUint16 = function(value) { * Writes a 32-bit unsigned integer to the buffer. Numbers outside the * range [0,2^32) will be truncated. * @param {number} value The value to write. + * @export */ jspb.BinaryEncoder.prototype.writeUint32 = function(value) { jspb.asserts.assert(value == Math.floor(value)); @@ -297,14 +313,15 @@ jspb.BinaryEncoder.prototype.writeUint32 = function(value) { * Writes a 64-bit unsigned integer to the buffer. Numbers outside the * range [0,2^64) will be truncated. * @param {number} value The value to write. + * @export */ jspb.BinaryEncoder.prototype.writeUint64 = function(value) { jspb.asserts.assert(value == Math.floor(value)); jspb.asserts.assert( (value >= 0) && (value < jspb.BinaryConstants.TWO_TO_64)); jspb.utils.splitUint64(value); - this.writeUint32(jspb.utils.split64Low); - this.writeUint32(jspb.utils.split64High); + this.writeUint32(jspb.utils.getSplit64Low()); + this.writeUint32(jspb.utils.getSplit64High()); }; @@ -312,6 +329,7 @@ jspb.BinaryEncoder.prototype.writeUint64 = function(value) { * Writes an 8-bit integer to the buffer. Numbers outside the range * [-2^7,2^7) will be truncated. * @param {number} value The value to write. + * @export */ jspb.BinaryEncoder.prototype.writeInt8 = function(value) { jspb.asserts.assert(value == Math.floor(value)); @@ -324,6 +342,7 @@ jspb.BinaryEncoder.prototype.writeInt8 = function(value) { * Writes a 16-bit integer to the buffer. Numbers outside the range * [-2^15,2^15) will be truncated. * @param {number} value The value to write. + * @export */ jspb.BinaryEncoder.prototype.writeInt16 = function(value) { jspb.asserts.assert(value == Math.floor(value)); @@ -337,6 +356,7 @@ jspb.BinaryEncoder.prototype.writeInt16 = function(value) { * Writes a 32-bit integer to the buffer. Numbers outside the range * [-2^31,2^31) will be truncated. * @param {number} value The value to write. + * @export */ jspb.BinaryEncoder.prototype.writeInt32 = function(value) { jspb.asserts.assert(value == Math.floor(value)); @@ -354,6 +374,7 @@ jspb.BinaryEncoder.prototype.writeInt32 = function(value) { * Writes a 64-bit integer to the buffer. Numbers outside the range * [-2^63,2^63) will be truncated. * @param {number} value The value to write. + * @export */ jspb.BinaryEncoder.prototype.writeInt64 = function(value) { jspb.asserts.assert(value == Math.floor(value)); @@ -361,7 +382,7 @@ jspb.BinaryEncoder.prototype.writeInt64 = function(value) { (value >= -jspb.BinaryConstants.TWO_TO_63) && (value < jspb.BinaryConstants.TWO_TO_63)); jspb.utils.splitInt64(value); - this.writeSplitFixed64(jspb.utils.split64Low, jspb.utils.split64High); + this.writeSplitFixed64(jspb.utils.getSplit64Low(), jspb.utils.getSplit64High()); }; @@ -369,6 +390,7 @@ jspb.BinaryEncoder.prototype.writeInt64 = function(value) { * Writes a 64-bit integer decimal strings to the buffer. Numbers outside the * range [-2^63,2^63) will be truncated. * @param {string} value The value to write. + * @export */ jspb.BinaryEncoder.prototype.writeInt64String = function(value) { jspb.asserts.assert(value == Math.floor(value)); @@ -376,7 +398,7 @@ jspb.BinaryEncoder.prototype.writeInt64String = function(value) { (+value >= -jspb.BinaryConstants.TWO_TO_63) && (+value < jspb.BinaryConstants.TWO_TO_63)); jspb.utils.splitHash64(jspb.utils.decimalStringToHash64(value)); - this.writeSplitFixed64(jspb.utils.split64Low, jspb.utils.split64High); + this.writeSplitFixed64(jspb.utils.getSplit64Low(), jspb.utils.getSplit64High()); }; @@ -384,6 +406,7 @@ jspb.BinaryEncoder.prototype.writeInt64String = function(value) { * Writes a single-precision floating point value to the buffer. Numbers * requiring more than 32 bits of precision will be truncated. * @param {number} value The value to write. + * @export */ jspb.BinaryEncoder.prototype.writeFloat = function(value) { jspb.asserts.assert( @@ -391,7 +414,7 @@ jspb.BinaryEncoder.prototype.writeFloat = function(value) { ((value >= -jspb.BinaryConstants.FLOAT32_MAX) && (value <= jspb.BinaryConstants.FLOAT32_MAX))); jspb.utils.splitFloat32(value); - this.writeUint32(jspb.utils.split64Low); + this.writeUint32(jspb.utils.getSplit64Low()); }; @@ -399,6 +422,7 @@ jspb.BinaryEncoder.prototype.writeFloat = function(value) { * Writes a double-precision floating point value to the buffer. As this is * the native format used by JavaScript, no precision will be lost. * @param {number} value The value to write. + * @export */ jspb.BinaryEncoder.prototype.writeDouble = function(value) { jspb.asserts.assert( @@ -406,8 +430,8 @@ jspb.BinaryEncoder.prototype.writeDouble = function(value) { ((value >= -jspb.BinaryConstants.FLOAT64_MAX) && (value <= jspb.BinaryConstants.FLOAT64_MAX))); jspb.utils.splitFloat64(value); - this.writeUint32(jspb.utils.split64Low); - this.writeUint32(jspb.utils.split64High); + this.writeUint32(jspb.utils.getSplit64Low()); + this.writeUint32(jspb.utils.getSplit64High()); }; @@ -416,6 +440,7 @@ jspb.BinaryEncoder.prototype.writeDouble = function(value) { * because the JSPB code generator uses 0/1 instead of true/false to save space * in the string representation of the proto. * @param {boolean|number} value The value to write. + * @export */ jspb.BinaryEncoder.prototype.writeBool = function(value) { jspb.asserts.assert( @@ -427,6 +452,7 @@ jspb.BinaryEncoder.prototype.writeBool = function(value) { /** * Writes an enum value to the buffer as a varint. * @param {number} value The value to write. + * @export */ jspb.BinaryEncoder.prototype.writeEnum = function(value) { jspb.asserts.assert(value == Math.floor(value)); @@ -440,6 +466,7 @@ jspb.BinaryEncoder.prototype.writeEnum = function(value) { /** * Writes an arbitrary byte array to the buffer. * @param {!Uint8Array} bytes The array of bytes to write. + * @export */ jspb.BinaryEncoder.prototype.writeBytes = function(bytes) { this.buffer_.push.apply(this.buffer_, bytes); @@ -450,10 +477,11 @@ jspb.BinaryEncoder.prototype.writeBytes = function(bytes) { * Writes a 64-bit hash string (8 characters @ 8 bits of data each) to the * buffer as a varint. * @param {string} hash The hash to write. + * @export */ jspb.BinaryEncoder.prototype.writeVarintHash64 = function(hash) { jspb.utils.splitHash64(hash); - this.writeSplitVarint64(jspb.utils.split64Low, jspb.utils.split64High); + this.writeSplitVarint64(jspb.utils.getSplit64Low(), jspb.utils.getSplit64High()); }; @@ -461,11 +489,12 @@ jspb.BinaryEncoder.prototype.writeVarintHash64 = function(hash) { * Writes a 64-bit hash string (8 characters @ 8 bits of data each) to the * buffer as a fixed64. * @param {string} hash The hash to write. + * @export */ jspb.BinaryEncoder.prototype.writeFixedHash64 = function(hash) { jspb.utils.splitHash64(hash); - this.writeUint32(jspb.utils.split64Low); - this.writeUint32(jspb.utils.split64High); + this.writeUint32(jspb.utils.getSplit64Low()); + this.writeUint32(jspb.utils.getSplit64High()); }; @@ -474,6 +503,7 @@ jspb.BinaryEncoder.prototype.writeFixedHash64 = function(hash) { * TODO(aappleby): Add support for surrogate pairs, reject unpaired surrogates. * @param {string} value The string to write. * @return {number} The number of bytes used to encode the string. + * @export */ jspb.BinaryEncoder.prototype.writeString = function(value) { var oldLength = this.buffer_.length; diff --git a/binary/message_test.js b/binary/message_test.js index 807a0b7..39f7d36 100644 --- a/binary/message_test.js +++ b/binary/message_test.js @@ -32,10 +32,10 @@ goog.setTestOnly(); -// CommonJS-LoadFromFile: test_pb proto.jspb.test +// CommonJS-LoadFromFile: ../protos/test_pb proto.jspb.test goog.require('proto.jspb.test.Deeply.Nested.Message'); -// CommonJS-LoadFromFile: test2_pb proto.jspb.test +// CommonJS-LoadFromFile: ../protos/test2_pb proto.jspb.test goog.require('proto.jspb.test.ForeignNestedFieldMessage'); describe('Message test suite', () => { diff --git a/binary/reader.js b/binary/reader.js index f21b058..167d515 100644 --- a/binary/reader.js +++ b/binary/reader.js @@ -64,6 +64,7 @@ goog.require('jspb.utils'); * we'll throw an assertion if we go off the end of the block. * @constructor * @struct + * @export */ jspb.BinaryReader = function(opt_bytes, opt_start, opt_length) { /** @@ -112,6 +113,22 @@ jspb.BinaryReader = function(opt_bytes, opt_start, opt_length) { jspb.BinaryReader.instanceCache_ = []; +/** + * @export + */ +jspb.BinaryReader.clearInstanceCache = function() { + jspb.BinaryReader.instanceCache_ = []; +} + +/** + @return {number} + * @export + */ +jspb.BinaryReader.getInstanceCacheLength = function() { + return jspb.BinaryReader.instanceCache_.length; +} + + /** * Pops an instance off the instance cache, or creates one if the cache is * empty. @@ -120,6 +137,7 @@ jspb.BinaryReader.instanceCache_ = []; * @param {number=} opt_length The optional length of the block to read - * we'll throw an assertion if we go off the end of the block. * @return {!jspb.BinaryReader} + * @export */ jspb.BinaryReader.alloc = function(opt_bytes, opt_start, opt_length) { if (jspb.BinaryReader.instanceCache_.length) { @@ -141,12 +159,14 @@ jspb.BinaryReader.alloc = function(opt_bytes, opt_start, opt_length) { * @param {number=} opt_length The optional length of the block to read - * we'll throw an assertion if we go off the end of the block. * @return {!jspb.BinaryReader} + * @export */ jspb.BinaryReader.prototype.alloc = jspb.BinaryReader.alloc; /** * Puts this instance back in the instance cache. + * @export */ jspb.BinaryReader.prototype.free = function() { this.decoder_.clear(); @@ -164,6 +184,7 @@ jspb.BinaryReader.prototype.free = function() { /** * Returns the cursor immediately before the current field's tag. * @return {number} The internal read cursor. + * @export */ jspb.BinaryReader.prototype.getFieldCursor = function() { return this.fieldCursor_; @@ -173,6 +194,7 @@ jspb.BinaryReader.prototype.getFieldCursor = function() { /** * Returns the internal read cursor. * @return {number} The internal read cursor. + * @export */ jspb.BinaryReader.prototype.getCursor = function() { return this.decoder_.getCursor(); @@ -182,6 +204,7 @@ jspb.BinaryReader.prototype.getCursor = function() { /** * Returns the raw buffer. * @return {?Uint8Array} The raw buffer. + * @export */ jspb.BinaryReader.prototype.getBuffer = function() { return this.decoder_.getBuffer(); @@ -201,6 +224,7 @@ jspb.BinaryReader.prototype.getFieldNumber = function() { /** * @return {jspb.BinaryConstants.WireType} The wire type of the next field * in the stream, or WireType.INVALID if there is no next field. + * @export */ jspb.BinaryReader.prototype.getWireType = function() { return this.nextWireType_; @@ -230,6 +254,7 @@ jspb.BinaryReader.prototype.isEndGroup = function() { /** * Returns true if this reader hit an error due to corrupt data. * @return {boolean} + * @export */ jspb.BinaryReader.prototype.getError = function() { return this.error_ || this.decoder_.getError(); @@ -241,6 +266,7 @@ jspb.BinaryReader.prototype.getError = function() { * @param {!Uint8Array} bytes The block of bytes we're reading from. * @param {number} start The offset to start reading at. * @param {number} length The length of the block to read. + * @export */ jspb.BinaryReader.prototype.setBlock = function(bytes, start, length) { this.decoder_.setBlock(bytes, start, length); @@ -252,6 +278,7 @@ jspb.BinaryReader.prototype.setBlock = function(bytes, start, length) { /** * Rewinds the stream cursor to the beginning of the buffer and resets all * internal state. + * @export */ jspb.BinaryReader.prototype.reset = function() { this.decoder_.reset(); @@ -263,6 +290,7 @@ jspb.BinaryReader.prototype.reset = function() { /** * Advances the stream cursor by the given number of bytes. * @param {number} count The number of bytes to advance by. + * @export */ jspb.BinaryReader.prototype.advance = function(count) { this.decoder_.advance(count); @@ -320,6 +348,7 @@ jspb.BinaryReader.prototype.nextField = function() { /** * Winds the reader back to just before this field's header. + * @export */ jspb.BinaryReader.prototype.unskipHeader = function() { this.decoder_.unskipVarint((this.nextField_ << 3) | this.nextWireType_); @@ -328,6 +357,7 @@ jspb.BinaryReader.prototype.unskipHeader = function() { /** * Skips all contiguous fields whose header matches the one we just read. + * @export */ jspb.BinaryReader.prototype.skipMatchingFields = function() { var field = this.nextField_; @@ -345,6 +375,7 @@ jspb.BinaryReader.prototype.skipMatchingFields = function() { /** * Skips over the next varint field in the binary stream. + * @export */ jspb.BinaryReader.prototype.skipVarintField = function() { if (this.nextWireType_ != jspb.BinaryConstants.WireType.VARINT) { @@ -359,6 +390,7 @@ jspb.BinaryReader.prototype.skipVarintField = function() { /** * Skips over the next delimited field in the binary stream. + * @export */ jspb.BinaryReader.prototype.skipDelimitedField = function() { if (this.nextWireType_ != jspb.BinaryConstants.WireType.DELIMITED) { @@ -374,6 +406,7 @@ jspb.BinaryReader.prototype.skipDelimitedField = function() { /** * Skips over the next fixed32 field in the binary stream. + * @export */ jspb.BinaryReader.prototype.skipFixed32Field = function() { if (this.nextWireType_ != jspb.BinaryConstants.WireType.FIXED32) { @@ -388,6 +421,7 @@ jspb.BinaryReader.prototype.skipFixed32Field = function() { /** * Skips over the next fixed64 field in the binary stream. + * @export */ jspb.BinaryReader.prototype.skipFixed64Field = function() { if (this.nextWireType_ != jspb.BinaryConstants.WireType.FIXED64) { @@ -402,6 +436,7 @@ jspb.BinaryReader.prototype.skipFixed64Field = function() { /** * Skips over the next group field in the binary stream. + * @export */ jspb.BinaryReader.prototype.skipGroup = function() { var previousField = this.nextField_; @@ -428,6 +463,7 @@ jspb.BinaryReader.prototype.skipGroup = function() { /** * Skips over the next field in the binary stream - this is useful if we're * decoding a message that contain unknown fields. + * @export */ jspb.BinaryReader.prototype.skipField = function() { switch (this.nextWireType_) { @@ -456,6 +492,7 @@ jspb.BinaryReader.prototype.skipField = function() { * Registers a user-defined read callback. * @param {string} callbackName * @param {function(!jspb.BinaryReader):*} callback + * @export */ jspb.BinaryReader.prototype.registerReadCallback = function( callbackName, callback) { @@ -471,6 +508,7 @@ jspb.BinaryReader.prototype.registerReadCallback = function( * Runs a registered read callback. * @param {string} callbackName The name the callback is registered under. * @return {*} The value returned by the callback. + * @export */ jspb.BinaryReader.prototype.runReadCallback = function(callbackName) { jspb.asserts.assert(this.readCallbacks_ !== null); @@ -596,6 +634,7 @@ jspb.BinaryReader.prototype.readGroup = function(field, message, reader) { /** * Return a decoder that wraps the current delimited field. * @return {!jspb.BinaryDecoder} + * @export */ jspb.BinaryReader.prototype.getFieldDecoder = function() { jspb.asserts.assert( @@ -634,6 +673,7 @@ jspb.BinaryReader.prototype.readInt32 = function() { * * @return {string} The value of the signed 32-bit integer field as a decimal * string. + * @export */ jspb.BinaryReader.prototype.readInt32String = function() { jspb.asserts.assert( @@ -664,6 +704,7 @@ jspb.BinaryReader.prototype.readInt64 = function() { * * @return {string} The value of the signed 64-bit integer field as a decimal * string. + * @export */ jspb.BinaryReader.prototype.readInt64String = function() { jspb.asserts.assert( @@ -694,6 +735,7 @@ jspb.BinaryReader.prototype.readUint32 = function() { * * @return {string} The value of the unsigned 32-bit integer field as a decimal * string. + * @export */ jspb.BinaryReader.prototype.readUint32String = function() { jspb.asserts.assert( @@ -724,6 +766,7 @@ jspb.BinaryReader.prototype.readUint64 = function() { * * @return {string} The value of the unsigned 64-bit integer field as a decimal * string. + * @export */ jspb.BinaryReader.prototype.readUint64String = function() { jspb.asserts.assert( @@ -769,6 +812,7 @@ jspb.BinaryReader.prototype.readSint64 = function() { * * @return {string} The value of the signed 64-bit integer field as a decimal * string. + * @export */ jspb.BinaryReader.prototype.readSint64String = function() { jspb.asserts.assert( @@ -816,6 +860,7 @@ jspb.BinaryReader.prototype.readFixed64 = function() { * * @return {string} The value of the unsigned 64-bit integer field as a decimal * string. + * @export */ jspb.BinaryReader.prototype.readFixed64String = function() { jspb.asserts.assert( @@ -846,6 +891,7 @@ jspb.BinaryReader.prototype.readSfixed32 = function() { * * @return {string} The value of the signed 32-bit integer field as a decimal * string. + * @export */ jspb.BinaryReader.prototype.readSfixed32String = function() { jspb.asserts.assert( @@ -877,6 +923,7 @@ jspb.BinaryReader.prototype.readSfixed64 = function() { * Returns the value as a string. * * @return {string} The value of the sfixed64 field as a decimal string. + * @export */ jspb.BinaryReader.prototype.readSfixed64String = function() { jspb.asserts.assert( @@ -977,6 +1024,7 @@ jspb.BinaryReader.prototype.readBytes = function() { * if the next field in the stream is not of the correct wire type. * * @return {string} The hash value. + * @export */ jspb.BinaryReader.prototype.readVarintHash64 = function() { jspb.asserts.assert( @@ -991,6 +1039,7 @@ jspb.BinaryReader.prototype.readVarintHash64 = function() { * field in the stream is not of the correct wire type. * * @return {string} The hash value. + * @export */ jspb.BinaryReader.prototype.readSintHash64 = function() { jspb.asserts.assert( @@ -1008,6 +1057,7 @@ jspb.BinaryReader.prototype.readSintHash64 = function() { * the result value, takes parameters (lowBits, highBits). * @return {T} * @template T + * @export */ jspb.BinaryReader.prototype.readSplitVarint64 = function(convert) { jspb.asserts.assert( @@ -1025,6 +1075,7 @@ jspb.BinaryReader.prototype.readSplitVarint64 = function(convert) { * the result value, takes parameters (lowBits, highBits). * @return {T} * @template T + * @export */ jspb.BinaryReader.prototype.readSplitZigzagVarint64 = function(convert) { jspb.asserts.assert( @@ -1041,6 +1092,7 @@ jspb.BinaryReader.prototype.readSplitZigzagVarint64 = function(convert) { * if the next field in the stream is not of the correct wire type. * * @return {string} The hash value. + * @export */ jspb.BinaryReader.prototype.readFixedHash64 = function() { jspb.asserts.assert( @@ -1058,6 +1110,7 @@ jspb.BinaryReader.prototype.readFixedHash64 = function() { * the result value, takes parameters (lowBits, highBits). * @return {T} * @template T + * @export */ jspb.BinaryReader.prototype.readSplitFixed64 = function(convert) { jspb.asserts.assert( @@ -1101,6 +1154,7 @@ jspb.BinaryReader.prototype.readPackedInt32 = function() { * Reads a packed int32 field, which consists of a length header and a list of * signed varints. Returns a list of strings. * @return {!Array} + * @export */ jspb.BinaryReader.prototype.readPackedInt32String = function() { return this.readPackedField_(this.decoder_.readSignedVarint32String); @@ -1122,6 +1176,7 @@ jspb.BinaryReader.prototype.readPackedInt64 = function() { * Reads a packed int64 field, which consists of a length header and a list of * signed varints. Returns a list of strings. * @return {!Array} + * @export */ jspb.BinaryReader.prototype.readPackedInt64String = function() { return this.readPackedField_(this.decoder_.readSignedVarint64String); @@ -1143,6 +1198,7 @@ jspb.BinaryReader.prototype.readPackedUint32 = function() { * Reads a packed uint32 field, which consists of a length header and a list of * unsigned varints. Returns a list of strings. * @return {!Array} + * @export */ jspb.BinaryReader.prototype.readPackedUint32String = function() { return this.readPackedField_(this.decoder_.readUnsignedVarint32String); @@ -1164,6 +1220,7 @@ jspb.BinaryReader.prototype.readPackedUint64 = function() { * Reads a packed uint64 field, which consists of a length header and a list of * unsigned varints. Returns a list of strings. * @return {!Array} + * @export */ jspb.BinaryReader.prototype.readPackedUint64String = function() { return this.readPackedField_(this.decoder_.readUnsignedVarint64String); @@ -1196,6 +1253,7 @@ jspb.BinaryReader.prototype.readPackedSint64 = function() { * Reads a packed sint64 field, which consists of a length header and a list of * zigzag varints. Returns a list of strings. * @return {!Array} + * @export */ jspb.BinaryReader.prototype.readPackedSint64String = function() { return this.readPackedField_(this.decoder_.readZigzagVarint64String); @@ -1228,6 +1286,7 @@ jspb.BinaryReader.prototype.readPackedFixed64 = function() { * Reads a packed fixed64 field, which consists of a length header and a list * of unsigned 64-bit ints. Returns a list of strings. * @return {!Array} + * @export */ jspb.BinaryReader.prototype.readPackedFixed64String = function() { return this.readPackedField_(this.decoder_.readUint64String); @@ -1260,6 +1319,7 @@ jspb.BinaryReader.prototype.readPackedSfixed64 = function() { * Reads a packed sfixed64 field, which consists of a length header and a list * of 64-bit ints. Returns a list of strings. * @return {!Array} + * @export */ jspb.BinaryReader.prototype.readPackedSfixed64String = function() { return this.readPackedField_(this.decoder_.readInt64String); @@ -1314,6 +1374,7 @@ jspb.BinaryReader.prototype.readPackedEnum = function() { * Reads a packed varint hash64 field, which consists of a length header and a * list of varint hash64s. * @return {!Array} + * @export */ jspb.BinaryReader.prototype.readPackedVarintHash64 = function() { return this.readPackedField_(this.decoder_.readVarintHash64); @@ -1324,6 +1385,7 @@ jspb.BinaryReader.prototype.readPackedVarintHash64 = function() { * Reads a packed fixed hash64 field, which consists of a length header and a * list of fixed hash64s. * @return {!Array} + * @export */ jspb.BinaryReader.prototype.readPackedFixedHash64 = function() { return this.readPackedField_(this.decoder_.readFixedHash64); diff --git a/binary/reader_test.js b/binary/reader_test.js index dbbbb71..68acac6 100644 --- a/binary/reader_test.js +++ b/binary/reader_test.js @@ -63,7 +63,7 @@ describe('binaryReaderTest', () => { const buffer = writer.getResultBuffer(); // Empty the instance caches. - jspb.BinaryReader.instanceCache_ = []; + jspb.BinaryReader.clearInstanceCache(); // Allocating and then freeing three decoders should leave us with three in // the cache. @@ -75,46 +75,46 @@ describe('binaryReaderTest', () => { decoder2.free(); decoder3.free(); - expect(jspb.BinaryDecoder.instanceCache_.length).toEqual(3); - expect(jspb.BinaryReader.instanceCache_.length).toEqual(0); + expect(jspb.BinaryDecoder.getInstanceCacheLength()).toEqual(3); + expect(jspb.BinaryReader.getInstanceCacheLength()).toEqual(0); // Allocating and then freeing a reader should remove one decoder from its // cache, but it should stay stuck to the reader afterwards since we can't // have a reader without a decoder. jspb.BinaryReader.alloc().free(); - expect(jspb.BinaryDecoder.instanceCache_.length).toEqual(2); - expect(jspb.BinaryReader.instanceCache_.length).toEqual(1); + expect(jspb.BinaryDecoder.getInstanceCacheLength()).toEqual(2); + expect(jspb.BinaryReader.getInstanceCacheLength()).toEqual(1); // Allocating a reader should remove a reader from the cache. const reader = jspb.BinaryReader.alloc(buffer); - expect(jspb.BinaryDecoder.instanceCache_.length).toEqual(2); - expect(jspb.BinaryReader.instanceCache_.length).toEqual(0); + expect(jspb.BinaryDecoder.getInstanceCacheLength()).toEqual(2); + expect(jspb.BinaryReader.getInstanceCacheLength()).toEqual(0); // Processing the message reuses the current reader. reader.nextField(); expect(reader.getFieldNumber()).toEqual(1); reader.readMessage(dummyMessage, () => { - expect(jspb.BinaryReader.instanceCache_.length).toEqual(0); + expect(jspb.BinaryReader.getInstanceCacheLength()).toEqual(0); }); reader.nextField(); expect(reader.getFieldNumber()).toEqual(2); reader.readMessage(dummyMessage, () => { - expect(jspb.BinaryReader.instanceCache_.length).toEqual(0); + expect(jspb.BinaryReader.getInstanceCacheLength()).toEqual(0); }); expect(reader.nextField()).toEqual(false); - expect(jspb.BinaryDecoder.instanceCache_.length).toEqual(2); - expect(jspb.BinaryReader.instanceCache_.length).toEqual(0); + expect(jspb.BinaryDecoder.getInstanceCacheLength()).toEqual(2); + expect(jspb.BinaryReader.getInstanceCacheLength()).toEqual(0); // Freeing the reader should put it back into the cache. reader.free(); - expect(jspb.BinaryDecoder.instanceCache_.length).toEqual(2); - expect(jspb.BinaryReader.instanceCache_.length).toEqual(1); + expect(jspb.BinaryDecoder.getInstanceCacheLength()).toEqual(2); + expect(jspb.BinaryReader.getInstanceCacheLength()).toEqual(1); }); @@ -897,7 +897,7 @@ describe('binaryReaderTest', () => { // Create a proto consisting of two nested messages, with the inner one // containing a blob of bytes. - const fieldTag = (1 << 3) | jspb.BinaryConstants.WireType.DELIMITED; + const fieldTag = (1 << 3) | /* jspb.BinaryConstants.WireType.DELIMITED = */ 2; const blob = [1, 2, 3, 4, 5]; const writer = new jspb.BinaryWriter(); const dummyMessage = /** @type {!jspb.BinaryMessage} */ ({}); diff --git a/binary/utils.js b/binary/utils.js index ed60fed..e751ce3 100644 --- a/binary/utils.js +++ b/binary/utils.js @@ -57,6 +57,7 @@ goog.require('jspb.BinaryConstants'); * If the original value was a double, this temporary value will contain the * low 32 bits of the binary representation of that double, etcetera. * @type {number} + * @private */ jspb.utils.split64Low = 0; @@ -65,14 +66,33 @@ jspb.utils.split64Low = 0; * And correspondingly, this temporary variable will contain the high 32 bits * of whatever value was split. * @type {number} + * @private */ jspb.utils.split64High = 0; +/** + * @return {number} + * @export + */ +jspb.utils.getSplit64Low = function() { + return jspb.utils.split64Low; +} + +/** + * @return {number} + * @export + */ +jspb.utils.getSplit64High = function() { + return jspb.utils.split64High; +} + + /** * Splits an unsigned Javascript integer into two 32-bit halves and stores it * in the temp values above. * @param {number} value The number to split. + * @export */ jspb.utils.splitUint64 = function(value) { // Extract low 32 bits and high 32 bits as unsigned integers. @@ -89,6 +109,7 @@ jspb.utils.splitUint64 = function(value) { * Splits a signed Javascript integer into two 32-bit halves and stores it in * the temp values above. * @param {number} value The number to split. + * @export */ jspb.utils.splitInt64 = function(value) { // Convert to sign-magnitude representation. @@ -121,6 +142,7 @@ jspb.utils.splitInt64 = function(value) { * Converts a signed Javascript integer into zigzag format, splits it into two * 32-bit halves, and stores it in the temp values above. * @param {number} value The number to split. + * @export */ jspb.utils.splitZigzag64 = function(value) { // Convert to sign-magnitude and scale by 2 before we split the value. @@ -156,6 +178,7 @@ jspb.utils.splitZigzag64 = function(value) { * Converts a floating-point number into 32-bit IEEE representation and stores * it in the temp values above. * @param {number} value + * @export */ jspb.utils.splitFloat32 = function(value) { var sign = (value < 0) ? 1 : 0; @@ -217,6 +240,7 @@ jspb.utils.splitFloat32 = function(value) { * Converts a floating-point number into 64-bit IEEE representation and stores * it in the temp values above. * @param {number} value + * @export */ jspb.utils.splitFloat64 = function(value) { var sign = (value < 0) ? 1 : 0; @@ -294,6 +318,7 @@ jspb.utils.splitFloat64 = function(value) { * Converts an 8-character hash string into two 32-bit numbers and stores them * in the temp values above. * @param {string} hash + * @export */ jspb.utils.splitHash64 = function(hash) { var a = hash.charCodeAt(0); @@ -316,6 +341,7 @@ jspb.utils.splitHash64 = function(hash) { * @param {number} bitsLow * @param {number} bitsHigh * @return {number} + * @export */ jspb.utils.joinUint64 = function(bitsLow, bitsHigh) { return bitsHigh * jspb.BinaryConstants.TWO_TO_32 + (bitsLow >>> 0); @@ -328,6 +354,7 @@ jspb.utils.joinUint64 = function(bitsLow, bitsHigh) { * @param {number} bitsLow * @param {number} bitsHigh * @return {number} + * @export */ jspb.utils.joinInt64 = function(bitsLow, bitsHigh) { // If the high bit is set, do a manual two's complement conversion. @@ -354,6 +381,7 @@ jspb.utils.joinInt64 = function(bitsLow, bitsHigh) { * the result value, takes parameters (lowBits, highBits). * @return {T} * @template T + * @export */ jspb.utils.toZigzag64 = function(bitsLow, bitsHigh, convert) { // See @@ -378,6 +406,7 @@ jspb.utils.toZigzag64 = function(bitsLow, bitsHigh, convert) { * @param {number} bitsLow * @param {number} bitsHigh * @return {number} + * @export */ jspb.utils.joinZigzag64 = function(bitsLow, bitsHigh) { return jspb.utils.fromZigzag64(bitsLow, bitsHigh, jspb.utils.joinInt64); @@ -394,6 +423,7 @@ jspb.utils.joinZigzag64 = function(bitsLow, bitsHigh) { * the result value, takes parameters (lowBits, highBits). * @return {T} * @template T + * @export */ jspb.utils.fromZigzag64 = function(bitsLow, bitsHigh, convert) { // 64 bit math is: @@ -416,6 +446,7 @@ jspb.utils.fromZigzag64 = function(bitsLow, bitsHigh, convert) { * @param {number} bitsLow The low 32 bits of the binary number; * @param {number} bitsHigh The high 32 bits of the binary number. * @return {number} + * @export */ jspb.utils.joinFloat32 = function(bitsLow, bitsHigh) { var sign = ((bitsLow >> 31) * 2 + 1); @@ -445,6 +476,7 @@ jspb.utils.joinFloat32 = function(bitsLow, bitsHigh) { * @param {number} bitsLow The low 32 bits of the binary number; * @param {number} bitsHigh The high 32 bits of the binary number. * @return {number} + * @export */ jspb.utils.joinFloat64 = function(bitsLow, bitsHigh) { var sign = ((bitsHigh >> 31) * 2 + 1); @@ -474,6 +506,7 @@ jspb.utils.joinFloat64 = function(bitsLow, bitsHigh) { * @param {number} bitsLow * @param {number} bitsHigh * @return {string} + * @export */ jspb.utils.joinHash64 = function(bitsLow, bitsHigh) { var a = (bitsLow >>> 0) & 0xFF; @@ -491,6 +524,7 @@ jspb.utils.joinHash64 = function(bitsLow, bitsHigh) { /** * Individual digits for number->string conversion. * @const {!Array} + * @export */ jspb.utils.DIGITS = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' @@ -508,6 +542,7 @@ jspb.utils.A_CHAR_CODE_ = 97; * @param {number} bitsLow The low 32 bits of the binary number; * @param {number} bitsHigh The high 32 bits of the binary number. * @return {string} The binary number represented as a string. + * @export */ jspb.utils.joinUnsignedDecimalString = function(bitsLow, bitsHigh) { // Skip the expensive conversion if the number is small enough to use the @@ -573,6 +608,7 @@ jspb.utils.joinUnsignedDecimalString = function(bitsLow, bitsHigh) { * @param {number} bitsLow The low 32 bits of the binary number; * @param {number} bitsHigh The high 32 bits of the binary number. * @return {string} The binary number represented as a string. + * @export */ jspb.utils.joinSignedDecimalString = function(bitsLow, bitsHigh) { // If we're treating the input as a signed value and the high bit is set, do @@ -596,6 +632,7 @@ jspb.utils.joinSignedDecimalString = function(bitsLow, bitsHigh) { * @param {boolean} signed True if we should treat the hash string as encoding * a signed integer. * @return {string} + * @export */ jspb.utils.hash64ToDecimalString = function(hash, signed) { jspb.utils.splitHash64(hash); @@ -613,6 +650,7 @@ jspb.utils.hash64ToDecimalString = function(hash, signed) { * @param {boolean} signed True if we should treat the hash string as encoding * a signed integer. * @return {!Array} + * @export */ jspb.utils.hash64ArrayToDecimalStrings = function(hashes, signed) { var result = new Array(hashes.length); @@ -628,6 +666,7 @@ jspb.utils.hash64ArrayToDecimalStrings = function(hashes, signed) { * representation. * @param {string} dec * @return {string} + * @export */ jspb.utils.decimalStringToHash64 = function(dec) { jspb.asserts.assert(dec.length > 0); @@ -677,6 +716,7 @@ jspb.utils.decimalStringToHash64 = function(dec) { * Converts a signed or unsigned decimal string into two 32-bit halves, and * stores them in the temp variables listed above. * @param {string} value The decimal string to convert. + * @export */ jspb.utils.splitDecimalString = function(value) { jspb.utils.splitHash64(jspb.utils.decimalStringToHash64(value)); @@ -709,6 +749,7 @@ jspb.utils.fromHexCharCode_ = function(hexCharCode) { * Converts an 8-character hash string into its hexadecimal representation. * @param {string} hash * @return {string} + * @export */ jspb.utils.hash64ToHexString = function(hash) { var temp = new Array(18); @@ -730,6 +771,7 @@ jspb.utils.hash64ToHexString = function(hash) { * Converts a '0x<16 digits>' hex string into its hash string representation. * @param {string} hex * @return {string} + * @export */ jspb.utils.hexStringToHash64 = function(hex) { hex = hex.toLowerCase(); @@ -756,6 +798,7 @@ jspb.utils.hexStringToHash64 = function(hex) { * @param {boolean} signed True if the has should be interpreted as a signed * number. * @return {number} + * @export */ jspb.utils.hash64ToNumber = function(hash, signed) { jspb.utils.splitHash64(hash); @@ -771,6 +814,7 @@ jspb.utils.hash64ToNumber = function(hash, signed) { * precision if the value is non-integral or greater than 2^64. * @param {number} value The integer to convert. * @return {string} + * @export */ jspb.utils.numberToHash64 = function(value) { jspb.utils.splitInt64(value); @@ -784,6 +828,7 @@ jspb.utils.numberToHash64 = function(value) { * @param {number} start The starting point in the buffer to scan. * @param {number} end The end point in the buffer to scan. * @return {number} The number of varints in the buffer. + * @export */ jspb.utils.countVarints = function(buffer, start, end) { // Count how many high bits of each byte were set in the buffer. @@ -807,6 +852,7 @@ jspb.utils.countVarints = function(buffer, start, end) { * @param {number} end The end point in the buffer to scan. * @param {number} field The field number to count. * @return {number} The number of matching fields in the buffer. + * @export */ jspb.utils.countVarintFields = function(buffer, start, end, field) { var count = 0; @@ -909,6 +955,7 @@ jspb.utils.countFixedFields_ = function(buffer, start, end, tag, stride) { * @param {number} end The end point in the buffer to scan. * @param {number} field The field number to count. * @return {number} The number of matching fields in the buffer. + * @export */ jspb.utils.countFixed32Fields = function(buffer, start, end, field) { var tag = field * 8 + jspb.BinaryConstants.WireType.FIXED32; @@ -924,6 +971,7 @@ jspb.utils.countFixed32Fields = function(buffer, start, end, field) { * @param {number} end The end point in the buffer to scan. * @param {number} field The field number to count * @return {number} The number of matching fields in the buffer. + * @export */ jspb.utils.countFixed64Fields = function(buffer, start, end, field) { var tag = field * 8 + jspb.BinaryConstants.WireType.FIXED64; @@ -939,6 +987,7 @@ jspb.utils.countFixed64Fields = function(buffer, start, end, field) { * @param {number} end The end point in the buffer to scan. * @param {number} field The field number to count. * @return {number} The number of matching fields in the buffer. + * @export */ jspb.utils.countDelimitedFields = function(buffer, start, end, field) { var count = 0; @@ -980,6 +1029,7 @@ jspb.utils.countDelimitedFields = function(buffer, start, end, field) { * [1, 31] serializes to '"\x01\x1f"'. * @param {jspb.ByteSource} byteSource The bytes to serialize. * @return {string} Stringified bytes for text format. + * @export */ jspb.utils.debugBytesToTextFormat = function(byteSource) { var s = '"'; @@ -999,6 +1049,7 @@ jspb.utils.debugBytesToTextFormat = function(byteSource) { * String-ify a scalar for text format. Should be optimized away in non-debug. * @param {string|number|boolean} scalar The scalar to stringify. * @return {string} Stringified scalar for text format. + * @export */ jspb.utils.debugScalarToTextFormat = function(scalar) { if (typeof scalar === 'string') { @@ -1015,6 +1066,7 @@ jspb.utils.debugScalarToTextFormat = function(scalar) { * exception. * @param {string} str * @return {!Uint8Array} + * @export */ jspb.utils.stringToByteArray = function(str) { var arr = new Uint8Array(str.length); @@ -1036,6 +1088,7 @@ jspb.utils.stringToByteArray = function(str) { * @param {!jspb.ByteSource} data * @return {!Uint8Array} * @suppress {invalidCasts} + * @export */ jspb.utils.byteSourceToUint8Array = function(data) { if (data.constructor === Uint8Array) { diff --git a/binary/utils_test.js b/binary/utils_test.js index 2788c9a..fd22bb8 100644 --- a/binary/utils_test.js +++ b/binary/utils_test.js @@ -345,7 +345,7 @@ describe('binaryUtilsTest', () => { // NaN. jspb.utils.splitFloat32(NaN); expect(isNaN(jspb.utils.joinFloat32( - jspb.utils.split64Low, jspb.utils.split64High))) + jspb.utils.getSplit64Low(), jspb.utils.getSplit64High()))) .toEqual(true); /** @@ -355,11 +355,11 @@ describe('binaryUtilsTest', () => { function test(x, opt_bits) { jspb.utils.splitFloat32(x); if (opt_bits !== undefined) { - if (opt_bits != jspb.utils.split64Low) throw 'fail!'; + if (opt_bits != jspb.utils.getSplit64Low()) throw 'fail!'; } expect(truncate(x)) .toEqual(jspb.utils.joinFloat32( - jspb.utils.split64Low, jspb.utils.split64High)); + jspb.utils.getSplit64Low(), jspb.utils.getSplit64High())); } // Positive and negative infinity. @@ -432,15 +432,15 @@ describe('binaryUtilsTest', () => { function test(x, opt_highBits, opt_lowBits) { jspb.utils.splitFloat64(x); if (opt_highBits !== undefined) { - const split64High = jspb.utils.split64High; + const split64High = jspb.utils.getSplit64High(); expect(opt_highBits.toString(16)).toEqual(split64High.toString(16)); } if (opt_lowBits !== undefined) { - const split64Low = jspb.utils.split64Low; + const split64Low = jspb.utils.getSplit64Low(); expect(opt_lowBits.toString(16)).toEqual(split64Low.toString(16)); } expect( - jspb.utils.joinFloat64(jspb.utils.split64Low, jspb.utils.split64High)) + jspb.utils.joinFloat64(jspb.utils.getSplit64Low(), jspb.utils.getSplit64High())) .toEqual(x); } @@ -502,8 +502,8 @@ describe('binaryUtilsTest', () => { function stringToHiLoPair(str) { jspb.utils.splitDecimalString(str); return { - lo: jspb.utils.split64Low >>> 0, - hi: jspb.utils.split64High >>> 0 + lo: jspb.utils.getSplit64Low() >>> 0, + hi: jspb.utils.getSplit64High() >>> 0 }; } function makeHiLoPair(lo, hi) { diff --git a/binary/writer.js b/binary/writer.js index 4938aa4..a132a85 100644 --- a/binary/writer.js +++ b/binary/writer.js @@ -75,6 +75,7 @@ goog.require('jspb.utils'); * * @constructor * @struct + * @export */ jspb.BinaryWriter = function() { /** @@ -169,6 +170,7 @@ jspb.BinaryWriter.prototype.endDelimited_ = function(bookmark) { * @param {!Uint8Array} bytes The array of bytes to write. * @param {number} start The start of the range to write. * @param {number} end The end of the range to write. + * @export */ jspb.BinaryWriter.prototype.writeSerializedMessage = function( bytes, start, end) { @@ -182,6 +184,7 @@ jspb.BinaryWriter.prototype.writeSerializedMessage = function( * @param {?Uint8Array} bytes The array of bytes to write. * @param {?number} start The start of the range to write. * @param {?number} end The end of the range to write. + * @export */ jspb.BinaryWriter.prototype.maybeWriteSerializedMessage = function( bytes, start, end) { @@ -193,6 +196,7 @@ jspb.BinaryWriter.prototype.maybeWriteSerializedMessage = function( /** * Resets the writer, throwing away any accumulated buffers. + * @export */ jspb.BinaryWriter.prototype.reset = function() { this.blocks_ = []; @@ -241,6 +245,7 @@ jspb.BinaryWriter.prototype.getResultBuffer = function() { * Converts the encoded data into a base64-encoded string. * @param {!goog.crypt.base64.Alphabet=} alphabet Which flavor of base64 to use. * @return {string} + * @export */ jspb.BinaryWriter.prototype.getResultBase64String = function(alphabet) { return goog.crypt.base64.encodeByteArray(this.getResultBuffer(), alphabet); @@ -252,6 +257,7 @@ jspb.BinaryWriter.prototype.getResultBase64String = function(alphabet) { * done. * TODO(aappleby): Deprecated. Move callers to writeMessage(). * @param {number} field The field number of the sub-message. + * @export */ jspb.BinaryWriter.prototype.beginSubMessage = function(field) { this.bookmarks_.push(this.beginDelimited_(field)); @@ -261,6 +267,7 @@ jspb.BinaryWriter.prototype.beginSubMessage = function(field) { /** * Finishes a sub-message and packs it into the parent messages' buffer. * TODO(aappleby): Deprecated. Move callers to writeMessage(). + * @export */ jspb.BinaryWriter.prototype.endSubMessage = function() { jspb.asserts.assert(this.bookmarks_.length >= 0); @@ -288,6 +295,7 @@ jspb.BinaryWriter.prototype.writeFieldHeader_ = function(field, wireType) { * @param {jspb.BinaryConstants.FieldType} fieldType * @param {number} field * @param {jspb.AnyFieldType} value + * @export */ jspb.BinaryWriter.prototype.writeAny = function(fieldType, field, value) { var fieldTypes = jspb.BinaryConstants.FieldType; @@ -485,6 +493,7 @@ jspb.BinaryWriter.prototype.writeInt32 = function(field, value) { * the range [-2^31,2^31) will be truncated. * @param {number} field The field number. * @param {string?} value The value to write. + * @export */ jspb.BinaryWriter.prototype.writeInt32String = function(field, value) { if (value == null) return; @@ -516,6 +525,7 @@ jspb.BinaryWriter.prototype.writeInt64 = function(field, value) { * Writes a int64 field (with value as a string) to the buffer. * @param {number} field The field number. * @param {string?} value The value to write. + * @export */ jspb.BinaryWriter.prototype.writeInt64String = function(field, value) { if (value == null) return; @@ -545,6 +555,7 @@ jspb.BinaryWriter.prototype.writeUint32 = function(field, value) { * the range [0,2^32) will be truncated. * @param {number} field The field number. * @param {string?} value The value to write. + * @export */ jspb.BinaryWriter.prototype.writeUint32String = function(field, value) { if (value == null) return; @@ -574,6 +585,7 @@ jspb.BinaryWriter.prototype.writeUint64 = function(field, value) { * Writes a uint64 field (with value as a string) to the buffer. * @param {number} field The field number. * @param {string?} value The value to write. + * @export */ jspb.BinaryWriter.prototype.writeUint64String = function(field, value) { if (value == null) return; @@ -620,6 +632,7 @@ jspb.BinaryWriter.prototype.writeSint64 = function(field, value) { * outside the range [-2^63,2^63) will be truncated. * @param {number} field The field number. * @param {string?} value The hash64 string to write. + * @export */ jspb.BinaryWriter.prototype.writeSintHash64 = function(field, value) { if (value == null) return; @@ -632,6 +645,7 @@ jspb.BinaryWriter.prototype.writeSintHash64 = function(field, value) { * will be truncated. * @param {number} field The field number. * @param {string?} value The decimal string to write. + * @export */ jspb.BinaryWriter.prototype.writeSint64String = function(field, value) { if (value == null) return; @@ -675,6 +689,7 @@ jspb.BinaryWriter.prototype.writeFixed64 = function(field, value) { * Writes a fixed64 field (with value as a string) to the buffer. * @param {number} field The field number. * @param {string?} value The value to write. + * @export */ jspb.BinaryWriter.prototype.writeFixed64String = function(field, value) { if (value == null) return; @@ -723,6 +738,7 @@ jspb.BinaryWriter.prototype.writeSfixed64 = function(field, value) { * [-2^63,2^63) will be truncated. * @param {number} field The field number. * @param {string?} value The value to write. + * @export */ jspb.BinaryWriter.prototype.writeSfixed64String = function(field, value) { if (value == null) return; @@ -865,6 +881,7 @@ jspb.BinaryWriter.prototype.writeMessage = function( * mapunion(MessageType, (X) => * cond(eq(X, 'null'), none(), X))) * =: + * @export */ jspb.BinaryWriter.prototype.writeMessageSet = function( field, value, writerCallback) { @@ -914,6 +931,7 @@ jspb.BinaryWriter.prototype.writeGroup = function( * the buffer. * @param {number} field The field number. * @param {string?} value The hash string. + * @export */ jspb.BinaryWriter.prototype.writeFixedHash64 = function(field, value) { if (value == null) return; @@ -928,6 +946,7 @@ jspb.BinaryWriter.prototype.writeFixedHash64 = function(field, value) { * the buffer. * @param {number} field The field number. * @param {string?} value The hash string. + * @export */ jspb.BinaryWriter.prototype.writeVarintHash64 = function(field, value) { if (value == null) return; @@ -942,6 +961,7 @@ jspb.BinaryWriter.prototype.writeVarintHash64 = function(field, value) { * @param {number} field The field number. * @param {number} lowBits The low 32 bits. * @param {number} highBits The high 32 bits. + * @export */ jspb.BinaryWriter.prototype.writeSplitFixed64 = function( field, lowBits, highBits) { @@ -955,6 +975,7 @@ jspb.BinaryWriter.prototype.writeSplitFixed64 = function( * @param {number} field The field number. * @param {number} lowBits The low 32 bits. * @param {number} highBits The high 32 bits. + * @export */ jspb.BinaryWriter.prototype.writeSplitVarint64 = function( field, lowBits, highBits) { @@ -968,6 +989,7 @@ jspb.BinaryWriter.prototype.writeSplitVarint64 = function( * @param {number} field The field number. * @param {number} lowBits The low 32 bits. * @param {number} highBits The high 32 bits. + * @export */ jspb.BinaryWriter.prototype.writeSplitZigzagVarint64 = function( field, lowBits, highBits) { @@ -998,6 +1020,7 @@ jspb.BinaryWriter.prototype.writeRepeatedInt32 = function(field, value) { * 32-bit int field. * @param {number} field The field number. * @param {?Array} value The array of ints to write. + * @export */ jspb.BinaryWriter.prototype.writeRepeatedInt32String = function(field, value) { if (value == null) return; @@ -1028,6 +1051,7 @@ jspb.BinaryWriter.prototype.writeRepeatedInt64 = function(field, value) { * @param {function(T): number} lo Function to get low bits. * @param {function(T): number} hi Function to get high bits. * @template T + * @export */ jspb.BinaryWriter.prototype.writeRepeatedSplitFixed64 = function( field, value, lo, hi) { @@ -1045,6 +1069,7 @@ jspb.BinaryWriter.prototype.writeRepeatedSplitFixed64 = function( * @param {function(T): number} lo Function to get low bits. * @param {function(T): number} hi Function to get high bits. * @template T + * @export */ jspb.BinaryWriter.prototype.writeRepeatedSplitVarint64 = function( field, value, lo, hi) { @@ -1062,6 +1087,7 @@ jspb.BinaryWriter.prototype.writeRepeatedSplitVarint64 = function( * @param {function(T): number} lo Function to get low bits. * @param {function(T): number} hi Function to get high bits. * @template T + * @export */ jspb.BinaryWriter.prototype.writeRepeatedSplitZigzagVarint64 = function( field, value, lo, hi) { @@ -1077,6 +1103,7 @@ jspb.BinaryWriter.prototype.writeRepeatedSplitZigzagVarint64 = function( * 64-bit int field. * @param {number} field The field number. * @param {?Array} value The array of ints to write. + * @export */ jspb.BinaryWriter.prototype.writeRepeatedInt64String = function(field, value) { if (value == null) return; @@ -1106,6 +1133,7 @@ jspb.BinaryWriter.prototype.writeRepeatedUint32 = function(field, value) { * unsigned 32-bit int field. * @param {number} field The field number. * @param {?Array} value The array of ints to write. + * @export */ jspb.BinaryWriter.prototype.writeRepeatedUint32String = function(field, value) { if (value == null) return; @@ -1135,6 +1163,7 @@ jspb.BinaryWriter.prototype.writeRepeatedUint64 = function(field, value) { * unsigned 64-bit int field. * @param {number} field The field number. * @param {?Array} value The array of ints to write. + * @export */ jspb.BinaryWriter.prototype.writeRepeatedUint64String = function(field, value) { if (value == null) return; @@ -1176,6 +1205,7 @@ jspb.BinaryWriter.prototype.writeRepeatedSint64 = function(field, value) { * Writes an array numbers to the buffer as a repeated signed 64-bit int field. * @param {number} field The field number. * @param {?Array} value The array of ints to write. + * @export */ jspb.BinaryWriter.prototype.writeRepeatedSint64String = function(field, value) { if (value == null) return; @@ -1190,6 +1220,7 @@ jspb.BinaryWriter.prototype.writeRepeatedSint64String = function(field, value) { * int field. * @param {number} field The field number. * @param {?Array} value The array of ints to write. + * @export */ jspb.BinaryWriter.prototype.writeRepeatedSintHash64 = function(field, value) { if (value == null) return; @@ -1278,6 +1309,7 @@ jspb.BinaryWriter.prototype.writeRepeatedSfixed64 = function(field, value) { * field. * @param {number} field The field number. * @param {?Array} value The array of decimal strings to write. + * @export */ jspb.BinaryWriter.prototype.writeRepeatedSfixed64String = function( field, value) { @@ -1420,6 +1452,7 @@ jspb.BinaryWriter.prototype.writeRepeatedGroup = function( * the buffer. * @param {number} field The field number. * @param {?Array} value The array of hashes to write. + * @export */ jspb.BinaryWriter.prototype.writeRepeatedFixedHash64 = function(field, value) { if (value == null) return; @@ -1434,6 +1467,7 @@ jspb.BinaryWriter.prototype.writeRepeatedFixedHash64 = function(field, value) { * each) to the buffer. * @param {number} field The field number. * @param {?Array} value The array of hashes to write. + * @export */ jspb.BinaryWriter.prototype.writeRepeatedVarintHash64 = function(field, value) { if (value == null) return; @@ -1464,6 +1498,7 @@ jspb.BinaryWriter.prototype.writePackedInt32 = function(field, value) { * 32-bit int field. * @param {number} field * @param {?Array} value + * @export */ jspb.BinaryWriter.prototype.writePackedInt32String = function(field, value) { if (value == null || !value.length) return; @@ -1498,6 +1533,7 @@ jspb.BinaryWriter.prototype.writePackedInt64 = function(field, value) { * @param {function(T): number} lo Function to get low bits. * @param {function(T): number} hi Function to get high bits. * @template T + * @export */ jspb.BinaryWriter.prototype.writePackedSplitFixed64 = function( field, value, lo, hi) { @@ -1517,6 +1553,7 @@ jspb.BinaryWriter.prototype.writePackedSplitFixed64 = function( * @param {function(T): number} lo Function to get low bits. * @param {function(T): number} hi Function to get high bits. * @template T + * @export */ jspb.BinaryWriter.prototype.writePackedSplitVarint64 = function( field, value, lo, hi) { @@ -1536,6 +1573,7 @@ jspb.BinaryWriter.prototype.writePackedSplitVarint64 = function( * @param {function(T): number} lo Function to get low bits. * @param {function(T): number} hi Function to get high bits. * @template T + * @export */ jspb.BinaryWriter.prototype.writePackedSplitZigzagVarint64 = function( field, value, lo, hi) { @@ -1557,6 +1595,7 @@ jspb.BinaryWriter.prototype.writePackedSplitZigzagVarint64 = function( * 64-bit int field. * @param {number} field * @param {?Array} value + * @export */ jspb.BinaryWriter.prototype.writePackedInt64String = function(field, value) { if (value == null || !value.length) return; @@ -1590,6 +1629,7 @@ jspb.BinaryWriter.prototype.writePackedUint32 = function(field, value) { * unsigned 32-bit int field. * @param {number} field * @param {?Array} value + * @export */ jspb.BinaryWriter.prototype.writePackedUint32String = function(field, value) { if (value == null || !value.length) return; @@ -1622,6 +1662,7 @@ jspb.BinaryWriter.prototype.writePackedUint64 = function(field, value) { * unsigned 64-bit int field. * @param {number} field * @param {?Array} value + * @export */ jspb.BinaryWriter.prototype.writePackedUint64String = function(field, value) { if (value == null || !value.length) return; @@ -1671,6 +1712,7 @@ jspb.BinaryWriter.prototype.writePackedSint64 = function(field, value) { * int field. * @param {number} field The field number. * @param {?Array} value The array of decimal strings to write. + * @export */ jspb.BinaryWriter.prototype.writePackedSint64String = function(field, value) { if (value == null || !value.length) return; @@ -1688,6 +1730,7 @@ jspb.BinaryWriter.prototype.writePackedSint64String = function(field, value) { * int field. * @param {number} field The field number. * @param {?Array} value The array of decimal strings to write. + * @export */ jspb.BinaryWriter.prototype.writePackedSintHash64 = function(field, value) { if (value == null || !value.length) return; @@ -1736,6 +1779,7 @@ jspb.BinaryWriter.prototype.writePackedFixed64 = function(field, value) { * fixed64 field. * @param {number} field The field number. * @param {?Array} value The array of strings to write. + * @export */ jspb.BinaryWriter.prototype.writePackedFixed64String = function(field, value) { if (value == null || !value.length) return; @@ -1784,6 +1828,7 @@ jspb.BinaryWriter.prototype.writePackedSfixed64 = function(field, value) { * Writes an array of numbers to the buffer as a packed sfixed64 field. * @param {number} field The field number. * @param {?Array} value The array of decimal strings to write. + * @export */ jspb.BinaryWriter.prototype.writePackedSfixed64String = function(field, value) { if (value == null || !value.length) return; @@ -1864,6 +1909,7 @@ jspb.BinaryWriter.prototype.writePackedEnum = function(field, value) { * the buffer. * @param {number} field The field number. * @param {?Array} value The array of hashes to write. + * @export */ jspb.BinaryWriter.prototype.writePackedFixedHash64 = function(field, value) { if (value == null || !value.length) return; @@ -1880,6 +1926,7 @@ jspb.BinaryWriter.prototype.writePackedFixedHash64 = function(field, value) { * the buffer. * @param {number} field The field number. * @param {?Array} value The array of hashes to write. + * @export */ jspb.BinaryWriter.prototype.writePackedVarintHash64 = function(field, value) { if (value == null || !value.length) return; diff --git a/binary/writer_test.js b/binary/writer_test.js index 3663b70..a5f3392 100644 --- a/binary/writer_test.js +++ b/binary/writer_test.js @@ -168,9 +168,9 @@ describe('binaryWriterTest', () => { writer.writeBytes(1, new Uint8Array([127])); expect('CgF/').toEqual(writer.getResultBase64String()); expect('CgF/').toEqual( - writer.getResultBase64String(goog.crypt.base64.Alphabet.DEFAULT)); + writer.getResultBase64String(/* goog.crypt.base64.Alphabet.DEFAULT = */ 0)); expect('CgF_').toEqual(writer.getResultBase64String( - goog.crypt.base64.Alphabet.WEBSAFE_NO_PADDING)); + /* goog.crypt.base64.Alphabet.WEBSAFE_NO_PADDING = */ 4)); }); it('writes split 64 fields', () => { @@ -259,11 +259,11 @@ describe('binaryWriterTest', () => { ]; function decimalToLowBits(v) { jspb.utils.splitDecimalString(v); - return jspb.utils.split64Low >>> 0; + return jspb.utils.getSplit64Low() >>> 0; } function decimalToHighBits(v) { jspb.utils.splitDecimalString(v); - return jspb.utils.split64High >>> 0; + return jspb.utils.getSplit64High() >>> 0; } const writer = new jspb.BinaryWriter(); @@ -272,7 +272,7 @@ describe('binaryWriterTest', () => { writer.writeSintHash64(1, jspb.utils.decimalStringToHash64(c.original)); jspb.utils.splitDecimalString(c.original); writer.writeSplitZigzagVarint64( - 1, jspb.utils.split64Low, jspb.utils.split64High); + 1, jspb.utils.getSplit64Low(), jspb.utils.getSplit64High()); }); writer.writeRepeatedSint64String(2, testCases.map(function(c) { diff --git a/commonjs/export.js b/commonjs/export.js index 72788f4..986a481 100644 --- a/commonjs/export.js +++ b/commonjs/export.js @@ -8,6 +8,8 @@ goog.provide('jspb.Export'); goog.require('goog.object'); + +goog.require('jspb.debug'); goog.require('jspb.BinaryReader'); goog.require('jspb.BinaryWriter'); goog.require('jspb.ExtensionFieldBinaryInfo'); @@ -15,9 +17,12 @@ goog.require('jspb.ExtensionFieldInfo'); goog.require('jspb.Message'); goog.require('jspb.Map'); + if (typeof exports === 'object') { + exports['debug'] = jspb.debug; exports['Map'] = jspb.Map; exports['Message'] = jspb.Message; + exports['BinaryReader'] = jspb.BinaryReader; exports['BinaryWriter'] = jspb.BinaryWriter; exports['ExtensionFieldInfo'] = jspb.ExtensionFieldInfo; diff --git a/commonjs/export_testdeps.js b/commonjs/export_testdeps.js index 18f72ce..7bb5690 100644 --- a/commonjs/export_testdeps.js +++ b/commonjs/export_testdeps.js @@ -12,10 +12,57 @@ goog.provide('jspb.ExportTestDeps'); goog.require('goog.crypt.base64'); goog.require('goog.testing.PropertyReplacer'); -exports.goog = goog; +goog.require('jspb.debug'); +goog.require('jspb.BinaryReader'); +goog.require('jspb.BinaryWriter'); +goog.require('jspb.ExtensionFieldBinaryInfo'); +goog.require('jspb.ExtensionFieldInfo'); +goog.require('jspb.Message'); +goog.require('jspb.Map'); -// The COMPILED variable is set by Closure compiler to "true" when it compiles -// JavaScript, so in practice this is equivalent to "exports.COMPILED = true". -// This will disable some debugging functionality in debug.js, such as -// attempting to check names that have been optimized away. -exports.COMPILED = COMPILED +if (typeof exports === 'object') { + exports['goog'] = { + 'crypt': { + 'byteArrayToString': goog.crypt.byteArrayToString, + 'byteArrayToHex': goog.crypt.byteArrayToHex, + 'base64': { + 'Alphabet': goog.crypt.base64.Alphabet, + 'encodeByteArray': goog.crypt.base64.encodeByteArray, + 'encodeString': goog.crypt.base64.encodeString, + 'decodeStringToUint8Array': goog.crypt.base64.decodeStringToUint8Array + } + }, + + 'testing': { + 'PropertyReplacer': goog.testing.PropertyReplacer, + }, + + 'userAgent': goog.userAgent, + + 'exportSymbol': goog.exportSymbol, + 'array': goog.array, + 'object': goog.object, + 'requireType': goog.requireType, + }; + + exports['jspb'] = { + 'debug': jspb.debug, + 'BinaryReader': jspb.BinaryReader, + 'BinaryWriter': jspb.BinaryWriter, + 'ExtensionFieldBinaryInfo': jspb.ExtensionFieldBinaryInfo, + 'ExtensionFieldInfo': jspb.ExtensionFieldInfo, + 'Message': jspb.Message, + 'Map': jspb.Map, + }; + // exports['exportSymbol'] = goog.exportSymbol; + // exports['inherits'] = goog.inherits; + // exports['object'] = {extend: goog.object.extend}; + // exports['typeOf'] = goog.typeOf; + // exports['requireType'] = goog.requireType; + + // The COMPILED variable is set by Closure compiler to "true" when it compiles + // JavaScript, so in practice this is equivalent to "exports.COMPILED = true". + // This will disable some debugging functionality in debug.js, such as + // attempting to check names that have been optimized away. + exports['COMPILED'] = COMPILED; +} diff --git a/commonjs/jasmine.json b/commonjs/jasmine.json index 666b8ed..7501097 100644 --- a/commonjs/jasmine.json +++ b/commonjs/jasmine.json @@ -2,8 +2,13 @@ "spec_dir": "", "spec_files": [ "*_test.js", - "binary/proto_test.js" + "binary/*_test.js" ], "helpers": [ - ] + ], + "env": { + "stopSpecOnExpectationFailure": true, + "stopOnSpecFailure": true, + "random": false + } } diff --git a/commonjs/rewrite_tests_for_commonjs.js b/commonjs/rewrite_tests_for_commonjs.js index 53bc0bc..4487c42 100644 --- a/commonjs/rewrite_tests_for_commonjs.js +++ b/commonjs/rewrite_tests_for_commonjs.js @@ -68,7 +68,7 @@ console.log("var googleProtobuf = require('google-protobuf');"); console.log("var testdeps = require('testdeps_commonjs');"); console.log("global.COMPILED = testdeps.COMPILED;"); console.log("global.goog = testdeps.goog;"); -console.log("global.jspb = googleProtobuf;"); +console.log("global.jspb = googleProtobuf.jspb;"); console.log(""); lineReader.on('line', function(line) { diff --git a/debug.js b/debug.js index ed16a3f..14b5847 100644 --- a/debug.js +++ b/debug.js @@ -51,6 +51,7 @@ goog.require('jspb.Message'); * not available for code size reasons. * @param {jspb.Message} message A jspb.Message. * @return {Object} + * @export */ jspb.debug.dump = function(message) { if (!goog.DEBUG) { diff --git a/debug_test.js b/debug_test.js index 99d413c..65612fd 100644 --- a/debug_test.js +++ b/debug_test.js @@ -33,7 +33,7 @@ goog.setTestOnly(); // CommonJS-LoadFromFile: google-protobuf goog.require('jspb.debug'); -// CommonJS-LoadFromFile: test_pb +// CommonJS-LoadFromFile: protos/test_pb proto.jspb.test goog.require('proto.jspb.test.HasExtensions'); goog.require('proto.jspb.test.IsExtension'); goog.require('proto.jspb.test.MapValueMessageNoBinary'); @@ -41,7 +41,7 @@ goog.require('proto.jspb.test.Simple1'); goog.require('proto.jspb.test.TestMapFieldsNoBinary'); -// CommonJS-LoadFromFile: testbinary_pb +// CommonJS-LoadFromFile: protos/testbinary_pb proto.jspb.test goog.require('proto.jspb.test.TestAllTypes'); describe('debugTest', function () { diff --git a/gulpfile.js b/gulpfile.js index 6960e5b..e7f7511 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -10,6 +10,9 @@ const plugin = '--plugin=protoc-gen-js=bazel-bin/generator/protoc-gen-js'; const protoc = [(process.env.PROTOC || 'protoc'), plugin].join(' '); const protocInc = process.env.PROTOC_INC || '../src'; +// See https://github.com/google/closure-compiler/wiki/Flags-and-Options +let compilationLevel = 'SIMPLE'; + const wellKnownTypes = [ 'google/protobuf/any.proto', 'google/protobuf/api.proto', @@ -64,6 +67,16 @@ function make_exec_logging_callback(cb) { } } +function enableAdvancedOptimizations(cb) { + compilationLevel = 'ADVANCED'; + cb(); +} + +function enableSimpleOptimizations(cb) { + compilationLevel = 'SIMPLE'; + cb(); +} + function genproto_well_known_types_closure(cb) { exec(protoc + ' --js_out=one_output_file_per_input_file,binary:. -I ' + protocInc + ' -I . ' + wellKnownTypes.join(' '), make_exec_logging_callback(cb)); @@ -118,14 +131,13 @@ function genproto_group3_commonjs_strict(cb) { function getClosureCompilerCommand(exportsFile, outputFile) { - // Use the default optimization level: SIMPLE_OPTIMIZATIONS: - // https://developers.google.com/closure/compiler/docs/compilation_levels#simple_optimizations const closureLib = 'node_modules/google-closure-library'; return [ 'node_modules/.bin/google-closure-compiler', `--js=${closureLib}/closure/goog/**.js`, `--js=${closureLib}/third_party/closure/goog/**.js`, '--js=asserts.js', + '--js=debug.js', '--js=map.js', '--js=message.js', '--js=binary/arith.js', @@ -137,6 +149,7 @@ function getClosureCompilerCommand(exportsFile, outputFile) { '--js=binary/writer.js', `--js=${exportsFile}`, '--generate_exports', + `--compilation_level=${compilationLevel}`, '--export_local_property_definitions', `--entry_point=${exportsFile}`, `> ${outputFile}` ].join(' '); @@ -196,34 +209,64 @@ function test_commonjs(cb) { make_exec_logging_callback(cb)); } +function remove_gen_files(cb) { + exec('rm -rf commonjs_out google-protobuf.js deps.js', + make_exec_logging_callback(cb)); +} + exports.build_protoc_plugin = function (cb) { exec('bazel build generator:protoc-gen-js', make_exec_logging_callback(cb)); } -exports.dist = series(exports.build_protoc_plugin, - genproto_wellknowntypes, - gen_google_protobuf_js); +const dist = series(exports.build_protoc_plugin, + genproto_wellknowntypes, + gen_google_protobuf_js); + +exports.dist = series(enableAdvancedOptimizations, dist); -exports.make_commonjs_out = series( - exports.dist, +exports.build_commonjs = series( + dist, genproto_well_known_types_commonjs, genproto_group1_commonjs, genproto_group2_commonjs, genproto_commonjs_wellknowntypes, commonjs_testdeps, genproto_group3_commonjs_strict, commonjs_out); -exports.deps = series(exports.build_protoc_plugin, - genproto_well_known_types_closure, - genproto_group1_closure, - genproto_group2_closure, - closure_make_deps); +exports.build_closure = series(exports.build_protoc_plugin, + genproto_well_known_types_closure, + genproto_group1_closure, + genproto_group2_closure, + closure_make_deps); + +const test_closure_series = series( + exports.build_closure, + test_closure); + +exports.test_closure = series(enableSimpleOptimizations, + test_closure_series); + +exports.test_closure_opt = series(enableAdvancedOptimizations, + test_closure_series); + + +const test_commonjs_series = series( + exports.build_commonjs, + test_commonjs); + + +exports.test_commonjs = series(enableSimpleOptimizations, + test_commonjs_series); +exports.test_commonjs_opt = series(enableAdvancedOptimizations, + test_commonjs_series); + +const test_series = series(test_closure_series, + test_commonjs_series); -exports.test_closure = series(exports.deps, - test_closure); +exports.test = series(enableSimpleOptimizations, + test_series); -exports.test_commonjs = series(exports.make_commonjs_out, - test_commonjs); +exports.test_opt = series(enableAdvancedOptimizations, + test_series); -exports.test = series(exports.test_closure, - exports.test_commonjs); +exports.clean = series(remove_gen_files); diff --git a/package.json b/package.json index 3b69569..438bbe5 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,8 @@ }, "scripts": { "build": "node ./node_modules/gulp/bin/gulp.js dist", - "test": "node ./node_modules/gulp/bin/gulp.js test" + "test": "node ./node_modules/gulp/bin/gulp.js test", + "clean": "node ./node_modules/gulp/bin/gulp.js clean" }, "repository": { "type": "git",