Skip to content

Commit 2ed4327

Browse files
author
Daniel Wirtz
committed
Proper arithmetic casting
1 parent 92d3d63 commit 2ed4327

File tree

9 files changed

+72
-829
lines changed

9 files changed

+72
-829
lines changed

ByteBuffer.js

Lines changed: 7 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -111,70 +111,6 @@
111111
* @expose
112112
*/
113113
ByteBuffer.BIG_ENDIAN = false;
114-
115-
/**
116-
* Int8 type for use with {@link ByteBuffer.cast}.
117-
* @type {Int8Array}
118-
* @const
119-
* @expose
120-
*/
121-
ByteBuffer.INT8 = new Int8Array(1);
122-
123-
/**
124-
* Uint8 type for use with {@link ByteBuffer.cast}.
125-
* @type {Uint8Array}
126-
* @const
127-
* @expose
128-
*/
129-
ByteBuffer.UINT8 = new Uint8Array(1);
130-
131-
/**
132-
* Int16 type for use with {@link ByteBuffer.cast}.
133-
* @type {Int16Array}
134-
* @const
135-
* @expose
136-
*/
137-
ByteBuffer.INT16 = new Int16Array(1);
138-
139-
/**
140-
* Uint16 type for use with {@link ByteBuffer.cast}.
141-
* @type {Uint16Array}
142-
* @const
143-
* @expose
144-
*/
145-
ByteBuffer.UINT16 = new Uint16Array(1);
146-
147-
/**
148-
* Int32 type for use with {@link ByteBuffer.cast}.
149-
* @type {Int32Array}
150-
* @const
151-
* @expose
152-
*/
153-
ByteBuffer.INT32 = new Int32Array(1);
154-
155-
/**
156-
* Uint32 type for use with {@link ByteBuffer.cast}.
157-
* @type {Uint32Array}
158-
* @const
159-
* @expose
160-
*/
161-
ByteBuffer.UINT32 = new Uint32Array(1);
162-
163-
/**
164-
* Float32 type for use with {@link ByteBuffer.cast}.
165-
* @type {Float32Array}
166-
* @const
167-
* @expose
168-
*/
169-
ByteBuffer.FLOAT32 = new Float32Array(1);
170-
171-
/**
172-
* Float64 type for use with {@link ByteBuffer.cast}.
173-
* @type {Float64Array}
174-
* @const
175-
* @expose
176-
*/
177-
ByteBuffer.FLOAT64 = new Float64Array(1);
178114

179115
/**
180116
* Long class for int64 support. May be undefined if the Long class has not been loaded and int64 support is
@@ -185,18 +121,6 @@
185121
*/
186122
ByteBuffer.Long = Long;
187123

188-
/**
189-
* Casts the specified value to the given type.
190-
* @param {Uint8Array|Int8Array|Uint16Array|Int16Array|Uint32Array|Int32Array|Float32Array|Float64Array} type Type to cast to, e.g. {@link ByteBuffer.UINT32}.
191-
* @param {number} value Value to cast
192-
* @return {number} Casted value
193-
* @expose
194-
*/
195-
ByteBuffer.cast = function(type, value) {
196-
type[0] = value;
197-
return type[0];
198-
};
199-
200124
/**
201125
* Allocates a new ByteBuffer.
202126
* @param {number=} capacity Initial capacity. Defaults to {@link ByteBuffer.DEFAULT_CAPACITY}.
@@ -1055,8 +979,8 @@
1055979
var advance = typeof offset == 'undefined';
1056980
offset = typeof offset != 'undefined' ? offset : this.offset;
1057981
// ref: http://code.google.com/searchframe#WTeibokF6gE/trunk/src/google/protobuf/io/coded_stream.cc
1058-
ByteBuffer.UINT32[0]=value;
1059-
this.ensureCapacity(offset+ByteBuffer.calculateVarint32(value=ByteBuffer.UINT32[0]));
982+
value = value >>> 0;
983+
this.ensureCapacity(offset+ByteBuffer.calculateVarint32(value));
1060984
var dst = new Uint8Array(this.array),
1061985
size = 0;
1062986
dst[offset] = (value | 0x80);
@@ -1108,22 +1032,22 @@
11081032
var src = new Uint8Array(this.array),
11091033
count = 0,
11101034
b;
1111-
ByteBuffer.UINT32[0] = 0;
1035+
var value = 0 >>> 0;
11121036
do {
11131037
if (count == ByteBuffer.MAX_VARINT32_BYTES) {
11141038
throw(new Error("Cannot read Varint32 from "+this+"@"+offset+": Number of bytes is larger than "+ByteBuffer.MAX_VARINT32_BYTES));
11151039
}
11161040
b = src[offset+count];
1117-
ByteBuffer.UINT32[0] |= (b&0x7F)<<(7*count);
1041+
value |= ((b&0x7F)<<(7*count)) >>> 0;
11181042
++count;
11191043
} while (b & 0x80);
1120-
ByteBuffer.INT32[0] = ByteBuffer.UINT32[0];
1044+
value = value | 0;
11211045
if (advance) {
11221046
this.offset += count;
1123-
return ByteBuffer.INT32[0];
1047+
return value;
11241048
} else {
11251049
return {
1126-
"value": ByteBuffer.INT32[0],
1050+
"value": value,
11271051
"length": count
11281052
};
11291053
}

ByteBuffer.min.js

Lines changed: 43 additions & 44 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ByteBuffer.min.map

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.

ByteBuffer.noexpose.js

Lines changed: 7 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -101,62 +101,6 @@
101101
* @const
102102
*/
103103
ByteBuffer.BIG_ENDIAN = false;
104-
105-
/**
106-
* Int8 type for use with {@link ByteBuffer.cast}.
107-
* @type {Int8Array}
108-
* @const
109-
*/
110-
ByteBuffer.INT8 = new Int8Array(1);
111-
112-
/**
113-
* Uint8 type for use with {@link ByteBuffer.cast}.
114-
* @type {Uint8Array}
115-
* @const
116-
*/
117-
ByteBuffer.UINT8 = new Uint8Array(1);
118-
119-
/**
120-
* Int16 type for use with {@link ByteBuffer.cast}.
121-
* @type {Int16Array}
122-
* @const
123-
*/
124-
ByteBuffer.INT16 = new Int16Array(1);
125-
126-
/**
127-
* Uint16 type for use with {@link ByteBuffer.cast}.
128-
* @type {Uint16Array}
129-
* @const
130-
*/
131-
ByteBuffer.UINT16 = new Uint16Array(1);
132-
133-
/**
134-
* Int32 type for use with {@link ByteBuffer.cast}.
135-
* @type {Int32Array}
136-
* @const
137-
*/
138-
ByteBuffer.INT32 = new Int32Array(1);
139-
140-
/**
141-
* Uint32 type for use with {@link ByteBuffer.cast}.
142-
* @type {Uint32Array}
143-
* @const
144-
*/
145-
ByteBuffer.UINT32 = new Uint32Array(1);
146-
147-
/**
148-
* Float32 type for use with {@link ByteBuffer.cast}.
149-
* @type {Float32Array}
150-
* @const
151-
*/
152-
ByteBuffer.FLOAT32 = new Float32Array(1);
153-
154-
/**
155-
* Float64 type for use with {@link ByteBuffer.cast}.
156-
* @type {Float64Array}
157-
* @const
158-
*/
159-
ByteBuffer.FLOAT64 = new Float64Array(1);
160104

161105
/**
162106
* Long class for int64 support. May be undefined if the Long class has not been loaded and int64 support is
@@ -166,17 +110,6 @@
166110
*/
167111
ByteBuffer.Long = Long;
168112

169-
/**
170-
* Casts the specified value to the given type.
171-
* @param {Uint8Array|Int8Array|Uint16Array|Int16Array|Uint32Array|Int32Array|Float32Array|Float64Array} type Type to cast to, e.g. {@link ByteBuffer.UINT32}.
172-
* @param {number} value Value to cast
173-
* @return {number} Casted value
174-
*/
175-
ByteBuffer.cast = function(type, value) {
176-
type[0] = value;
177-
return type[0];
178-
};
179-
180113
/**
181114
* Allocates a new ByteBuffer.
182115
* @param {number=} capacity Initial capacity. Defaults to {@link ByteBuffer.DEFAULT_CAPACITY}.
@@ -981,8 +914,8 @@
981914
var advance = typeof offset == 'undefined';
982915
offset = typeof offset != 'undefined' ? offset : this.offset;
983916
// ref: http://code.google.com/searchframe#WTeibokF6gE/trunk/src/google/protobuf/io/coded_stream.cc
984-
ByteBuffer.UINT32[0]=value;
985-
this.ensureCapacity(offset+ByteBuffer.calculateVarint32(value=ByteBuffer.UINT32[0]));
917+
value = value >>> 0;
918+
this.ensureCapacity(offset+ByteBuffer.calculateVarint32(value));
986919
var dst = new Uint8Array(this.array),
987920
size = 0;
988921
dst[offset] = (value | 0x80);
@@ -1033,22 +966,22 @@
1033966
var src = new Uint8Array(this.array),
1034967
count = 0,
1035968
b;
1036-
ByteBuffer.UINT32[0] = 0;
969+
var value = 0 >>> 0;
1037970
do {
1038971
if (count == ByteBuffer.MAX_VARINT32_BYTES) {
1039972
throw(new Error("Cannot read Varint32 from "+this+"@"+offset+": Number of bytes is larger than "+ByteBuffer.MAX_VARINT32_BYTES));
1040973
}
1041974
b = src[offset+count];
1042-
ByteBuffer.UINT32[0] |= (b&0x7F)<<(7*count);
975+
value |= ((b&0x7F)<<(7*count)) >>> 0;
1043976
++count;
1044977
} while (b & 0x80);
1045-
ByteBuffer.INT32[0] = ByteBuffer.UINT32[0];
978+
value = value | 0;
1046979
if (advance) {
1047980
this.offset += count;
1048-
return ByteBuffer.INT32[0];
981+
return value;
1049982
} else {
1050983
return {
1051-
"value": ByteBuffer.INT32[0],
984+
"value": value,
1052985
"length": count
1053986
};
1054987
}

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@ ByteBuffer
3333
* `ByteBuffer#write/readByte`, `ByteBuffer#write/readShort`, `ByteBuffer#write/readInt`, `ByteBuffer#write/readLong`
3434
(all signed), `ByteBuffer#write/readVarint` and `ByteBuffer#write/readZigZagVarint` (both 32bit signed),
3535
`ByteBuffer#write/readFloat`, `ByteBuffer#write/readDouble` aliases for the above for convenience
36-
* `ByteBuffer#writeUTF8String(str[, offset])` and `ByteBuffer#readUTF8String(chars[, offset])` using the included UTF8
37-
en-/decoder (full 6 bytes, [ref](http://en.wikipedia.org/wiki/UTF-8#Description))
36+
* `ByteBuffer#writeUTF8String(str[, offset])`, `ByteBuffer#readUTF8String(chars[, offset])` and
37+
`ByteBuffer#readUTF8StringBytes(length[, offset])` using the included UTF8 en-/decoder (full 6 bytes,
38+
[ref](http://en.wikipedia.org/wiki/UTF-8#Description))
3839
* `ByteBuffer#writeLString(str[, offset]))` and `ByteBuffer#readLString([offset])` to write respectively read a
3940
length-prepended (number of characters as UTF8 char) string
4041
* `ByteBuffer#writeVString(str[, offset]))` and `ByteBuffer#readVString([offset])` to write respectively read a
41-
length-prepended (number of characters as base 128 variable-length 32bit integer) string
42+
length-prepended (number of bytes as base 128 variable-length 32bit integer) string
4243
* `ByteBuffer#writeCString(str[, offset])` and `ByteBuffer#readCString([offset])` to write respectively read a
4344
NULL-terminated (Uint8 0x00) string
4445
* `ByteBuffer#writeJSON(data[, offset[, stringify]])` and `ByteBuffer#readJSON([offset[, parse]])` to write respectively

0 commit comments

Comments
 (0)