aboutsummaryrefslogtreecommitdiffhomepage
path: root/js/binary
diff options
context:
space:
mode:
Diffstat (limited to 'js/binary')
-rw-r--r--js/binary/decoder.js17
-rw-r--r--js/binary/decoder_test.js19
-rw-r--r--js/binary/encoder.js4
-rw-r--r--js/binary/reader.js2
-rw-r--r--js/binary/utils.js2
-rw-r--r--js/binary/writer.js4
-rw-r--r--js/binary/writer_test.js2
7 files changed, 37 insertions, 13 deletions
diff --git a/js/binary/decoder.js b/js/binary/decoder.js
index 26bf3594..ad9cb01b 100644
--- a/js/binary/decoder.js
+++ b/js/binary/decoder.js
@@ -71,7 +71,7 @@ jspb.BinaryIterator = function(opt_decoder, opt_next, opt_elements) {
*/
this.nextMethod_ = null;
- /** @private {Array.<number>} */
+ /** @private {?Array<number|boolean|string>} */
this.elements_ = null;
/** @private {number} */
@@ -100,7 +100,7 @@ jspb.BinaryIterator.prototype.init_ =
this.decoder_ = opt_decoder;
this.nextMethod_ = opt_next;
}
- this.elements_ = opt_elements ? opt_elements : null;
+ this.elements_ = opt_elements || null;
this.cursor_ = 0;
this.nextValue_ = null;
this.atEnd_ = !this.decoder_ && !this.elements_;
@@ -953,6 +953,7 @@ jspb.BinaryDecoder.prototype.readString = function(length) {
var end = cursor + length;
var codeUnits = [];
+ var result = '';
while (cursor < end) {
var c = bytes[cursor++];
if (c < 128) { // Regular 7-bit ASCII.
@@ -973,7 +974,7 @@ jspb.BinaryDecoder.prototype.readString = function(length) {
var c2 = bytes[cursor++];
var c3 = bytes[cursor++];
var c4 = bytes[cursor++];
- // Characters written on 4 bytes have 21 bits for a codepoint.
+ // Characters written on 4 bytes have 21 bits for a codepoint.
// We can't fit that on 16bit characters, so we use surrogates.
var codepoint = ((c & 7) << 18) | ((c2 & 63) << 12) | ((c3 & 63) << 6) | (c4 & 63);
// Surrogates formula from wikipedia.
@@ -986,10 +987,14 @@ jspb.BinaryDecoder.prototype.readString = function(length) {
var high = ((codepoint >> 10) & 1023) + 0xD800;
codeUnits.push(high, low);
}
+
+ // Avoid exceeding the maximum stack size when calling {@code apply}.
+ if (codeUnits.length >= 8192) {
+ result += String.fromCharCode.apply(null, codeUnits);
+ codeUnits.length = 0;
+ }
}
- // String.fromCharCode.apply is faster than manually appending characters on
- // Chrome 25+, and generates no additional cons string garbage.
- var result = String.fromCharCode.apply(null, codeUnits);
+ result += String.fromCharCode.apply(null, codeUnits);
this.cursor_ = cursor;
return result;
};
diff --git a/js/binary/decoder_test.js b/js/binary/decoder_test.js
index cb8aff96..d0139e29 100644
--- a/js/binary/decoder_test.js
+++ b/js/binary/decoder_test.js
@@ -211,6 +211,25 @@ describe('binaryDecoderTest', function() {
});
/**
+ * Tests reading and writing large strings
+ */
+ it('testLargeStrings', function() {
+ var encoder = new jspb.BinaryEncoder();
+
+ var len = 150000;
+ var long_string = '';
+ for (var i = 0; i < len; i++) {
+ long_string += 'a';
+ }
+
+ encoder.writeString(long_string);
+
+ var decoder = jspb.BinaryDecoder.alloc(encoder.end());
+
+ assertEquals(long_string, decoder.readString(len));
+ });
+
+ /**
* Test encoding and decoding utf-8.
*/
it('testUtf8', function() {
diff --git a/js/binary/encoder.js b/js/binary/encoder.js
index aee33e7e..f25935f1 100644
--- a/js/binary/encoder.js
+++ b/js/binary/encoder.js
@@ -355,8 +355,8 @@ jspb.BinaryEncoder.prototype.writeInt64 = function(value) {
*/
jspb.BinaryEncoder.prototype.writeInt64String = function(value) {
goog.asserts.assert(value == Math.floor(value));
- goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
- (value < jspb.BinaryConstants.TWO_TO_63));
+ goog.asserts.assert((+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);
};
diff --git a/js/binary/reader.js b/js/binary/reader.js
index 8c5a4e88..d5d698f7 100644
--- a/js/binary/reader.js
+++ b/js/binary/reader.js
@@ -971,7 +971,7 @@ jspb.BinaryReader.prototype.readFixedHash64 = function() {
/**
* Reads a packed scalar field using the supplied raw reader function.
- * @param {function()} decodeMethod
+ * @param {function(this:jspb.BinaryDecoder)} decodeMethod
* @return {!Array}
* @private
*/
diff --git a/js/binary/utils.js b/js/binary/utils.js
index 3ecd08e9..7702020b 100644
--- a/js/binary/utils.js
+++ b/js/binary/utils.js
@@ -430,7 +430,7 @@ jspb.utils.joinHash64 = function(bitsLow, bitsHigh) {
/**
* Individual digits for number->string conversion.
- * @const {!Array.<number>}
+ * @const {!Array.<string>}
*/
jspb.utils.DIGITS = [
'0', '1', '2', '3', '4', '5', '6', '7',
diff --git a/js/binary/writer.js b/js/binary/writer.js
index c3009dbb..672e94bd 100644
--- a/js/binary/writer.js
+++ b/js/binary/writer.js
@@ -596,8 +596,8 @@ jspb.BinaryWriter.prototype.writeSint64 = function(field, value) {
*/
jspb.BinaryWriter.prototype.writeSint64String = function(field, value) {
if (value == null) return;
- goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
- (value < jspb.BinaryConstants.TWO_TO_63));
+ goog.asserts.assert((+value >= -jspb.BinaryConstants.TWO_TO_63) &&
+ (+value < jspb.BinaryConstants.TWO_TO_63));
this.writeZigzagVarint64String_(field, value);
};
diff --git a/js/binary/writer_test.js b/js/binary/writer_test.js
index 83fcdf91..118eecfc 100644
--- a/js/binary/writer_test.js
+++ b/js/binary/writer_test.js
@@ -47,7 +47,7 @@ goog.require('jspb.BinaryWriter');
* @param {function()} func This function should throw an error when run.
*/
function assertFails(func) {
- var e = assertThrows(func);
+ assertThrows(func);
}