Skip to content

Commit 9c22fc6

Browse files
author
Daniel Wirtz
committed
Uint64 support, Long.js>=1.1.0
1 parent 152f5b3 commit 9c22fc6

File tree

9 files changed

+631
-67
lines changed

9 files changed

+631
-67
lines changed

ByteBuffer.js

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@
928928
throw(new Error("Long support is not available: See https://github.com/dcodeIO/ByteBuffer.js#on-long-int64-support for details"))
929929
}
930930
offset = typeof offset != 'undefined' ? offset : (this.offset+=8)-8;
931-
if (!(typeof value == 'object' && value instanceof Long)) value = Long.fromNumber(value);
931+
if (!(typeof value == 'object' && value instanceof Long)) value = Long.fromNumber(value, false);
932932
this.ensureCapacity(offset+8);
933933
if (this.littleEndian) {
934934
this.view.setInt32(offset, value.getLowBits(), true);
@@ -958,14 +958,63 @@
958958
}
959959
var value;
960960
if (this.littleEndian) {
961-
value = new Long(this.view.getInt32(offset, true), this.view.getInt32(offset+4, true));
961+
value = Long.fromBits(this.view.getInt32(offset, true), this.view.getInt32(offset+4, true), false);
962962
} else {
963-
value = new Long(this.view.getInt32(offset+4, false), this.view.getInt32(offset, false));
963+
value = Long.fromBits(this.view.getInt32(offset+4, false), this.view.getInt32(offset, false), false);
964+
}
965+
return value;
966+
};
967+
968+
/**
969+
* Writes a 64bit unsigned integer. Utilizes Long.js to write the low and high 32 bits separately.
970+
* @function
971+
* @param {number|Long} value Value to write
972+
* @param {number=} offset Offset to write to. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
973+
* @return {ByteBuffer} this
974+
* @throws {Error} If long support is not available
975+
* @expose
976+
*/
977+
ByteBuffer.prototype.writeUint64 = function(value, offset) {
978+
if (!Long) {
979+
throw(new Error("Long support is not available: See https://github.com/dcodeIO/ByteBuffer.js#on-long-int64-support for details"))
980+
}
981+
offset = typeof offset != 'undefined' ? offset : (this.offset+=8)-8;
982+
if (!(typeof value == 'object' && value instanceof Long)) value = Long.fromNumber(value, true);
983+
this.ensureCapacity(offset+8);
984+
if (this.littleEndian) {
985+
this.view.setUint32(offset, value.getLowBitsUnsigned(), true);
986+
this.view.setUint32(offset+4, value.getHighBitsUnsigned(), true);
987+
} else {
988+
this.view.setUint32(offset, value.getHighBitsUnsigned(), false);
989+
this.view.setUint32(offset+4, value.getLowBitsUnsigned(), false);
990+
}
991+
return this;
992+
};
993+
994+
/**
995+
* Reads a 64bit unsigned integer. Utilizes Long.js to construct a new Long from the low and high 32 bits.
996+
* @param {number=} offset Offset to read from. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
997+
* @return {Long}
998+
* @throws {Error} If offset+8 is larger than the capacity or long support is not available
999+
* @expose
1000+
*/
1001+
ByteBuffer.prototype.readUint64 = function(offset) {
1002+
if (!Long) {
1003+
throw(new Error("Long support is not available: See https://github.com/dcodeIO/ByteBuffer.js#on-long-int64-support for details"))
1004+
}
1005+
offset = typeof offset != 'undefined' ? offset : (this.offset+=8)-8;
1006+
if (this.array == null || offset+8 > this.array.byteLength) {
1007+
this.offset -= 8;
1008+
throw(new Error("Cannot read int64 from "+this+": Capacity overflow"));
1009+
}
1010+
var value;
1011+
if (this.littleEndian) {
1012+
value = Long.fromBits(this.view.getUint32(offset, true), this.view.getUint32(offset+4, true), true);
1013+
} else {
1014+
value = Long.fromBits(this.view.getUint32(offset+4, false), this.view.getUint32(offset, false), true);
9641015
}
9651016
return value;
9661017
};
967-
968-
// TODO: Do we need uint64?
9691018

9701019
/**
9711020
* Writes a long. This is an alias of {@link ByteBuffer#writeInt64}.

ByteBuffer.min.js

Lines changed: 33 additions & 31 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: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@
861861
throw(new Error("Long support is not available: See https://github.com/dcodeIO/ByteBuffer.js#on-long-int64-support for details"))
862862
}
863863
offset = typeof offset != 'undefined' ? offset : (this.offset+=8)-8;
864-
if (!(typeof value == 'object' && value instanceof Long)) value = Long.fromNumber(value);
864+
if (!(typeof value == 'object' && value instanceof Long)) value = Long.fromNumber(value, false);
865865
this.ensureCapacity(offset+8);
866866
if (this.littleEndian) {
867867
this.view.setInt32(offset, value.getLowBits(), true);
@@ -890,14 +890,61 @@
890890
}
891891
var value;
892892
if (this.littleEndian) {
893-
value = new Long(this.view.getInt32(offset, true), this.view.getInt32(offset+4, true));
893+
value = Long.fromBits(this.view.getInt32(offset, true), this.view.getInt32(offset+4, true), false);
894894
} else {
895-
value = new Long(this.view.getInt32(offset+4, false), this.view.getInt32(offset, false));
895+
value = Long.fromBits(this.view.getInt32(offset+4, false), this.view.getInt32(offset, false), false);
896+
}
897+
return value;
898+
};
899+
900+
/**
901+
* Writes a 64bit unsigned integer. Utilizes Long.js to write the low and high 32 bits separately.
902+
* @function
903+
* @param {number|Long} value Value to write
904+
* @param {number=} offset Offset to write to. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
905+
* @return {ByteBuffer} this
906+
* @throws {Error} If long support is not available
907+
*/
908+
ByteBuffer.prototype.writeUint64 = function(value, offset) {
909+
if (!Long) {
910+
throw(new Error("Long support is not available: See https://github.com/dcodeIO/ByteBuffer.js#on-long-int64-support for details"))
911+
}
912+
offset = typeof offset != 'undefined' ? offset : (this.offset+=8)-8;
913+
if (!(typeof value == 'object' && value instanceof Long)) value = Long.fromNumber(value, true);
914+
this.ensureCapacity(offset+8);
915+
if (this.littleEndian) {
916+
this.view.setUint32(offset, value.getLowBitsUnsigned(), true);
917+
this.view.setUint32(offset+4, value.getHighBitsUnsigned(), true);
918+
} else {
919+
this.view.setUint32(offset, value.getHighBitsUnsigned(), false);
920+
this.view.setUint32(offset+4, value.getLowBitsUnsigned(), false);
921+
}
922+
return this;
923+
};
924+
925+
/**
926+
* Reads a 64bit unsigned integer. Utilizes Long.js to construct a new Long from the low and high 32 bits.
927+
* @param {number=} offset Offset to read from. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
928+
* @return {Long}
929+
* @throws {Error} If offset+8 is larger than the capacity or long support is not available
930+
*/
931+
ByteBuffer.prototype.readUint64 = function(offset) {
932+
if (!Long) {
933+
throw(new Error("Long support is not available: See https://github.com/dcodeIO/ByteBuffer.js#on-long-int64-support for details"))
934+
}
935+
offset = typeof offset != 'undefined' ? offset : (this.offset+=8)-8;
936+
if (this.array == null || offset+8 > this.array.byteLength) {
937+
this.offset -= 8;
938+
throw(new Error("Cannot read int64 from "+this+": Capacity overflow"));
939+
}
940+
var value;
941+
if (this.littleEndian) {
942+
value = Long.fromBits(this.view.getUint32(offset, true), this.view.getUint32(offset+4, true), true);
943+
} else {
944+
value = Long.fromBits(this.view.getUint32(offset+4, false), this.view.getUint32(offset, false), true);
896945
}
897946
return value;
898947
};
899-
900-
// TODO: Do we need uint64?
901948

902949
/**
903950
* Writes a long. This is an alias of {@link ByteBuffer#writeInt64}.

0 commit comments

Comments
 (0)