Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #9 from deathcap/int64
Browse files Browse the repository at this point in the history
Use node-int64 for 64-bit long integers. Closes GH-1
  • Loading branch information
sjmulder committed Mar 31, 2014
2 parents b215572 + 1809681 commit df0c2b3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 31 deletions.
16 changes: 11 additions & 5 deletions nbt-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,17 @@ describe('nbt.Reader', function() {
0,0,0,0,0,0,0,255,
-127,0,0,0,0,0,0,0
]));
expect(reader.long()).toEqual(0);
expect(reader.long()).toEqual(255);

/* false pass - JS only has 53 bit precision */
expect(reader.long()).toEqual(-127 << 56);
expect(+reader.long()).toEqual(0);
expect(+reader.long()).toEqual(255);

/* node-int64 integer */
var big = reader.long();
expect(big.toOctetString()).toEqual('8100000000000000'); // exact
expect(+big).toEqual(-Infinity); // clamped
expect(big.valueOf()).toEqual(-Infinity);
expect(big.toNumber()).toEqual(-Infinity);
expect(big.toNumber(false)).toEqual(-Infinity);
expect(big.toNumber(true)).toEqual(-9151314442816848000); // rounded
});

it('reads 32-bit floats', function() {
Expand Down
4 changes: 2 additions & 2 deletions nbt.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

var nbt = this;
var zlib = require('zlib');
var Int64 = require('node-int64');

nbt.tagTypes = {
'end': 0,
Expand Down Expand Up @@ -63,10 +64,9 @@
this[nbt.tagTypes.double] = read.bind(this, 'DoubleBE', 8);

this[nbt.tagTypes.long] = function() {
/* FIXME: this can overflow, JS has 53 bit precision */
var upper = this.int();
var lower = this.int();
return (upper << 32) + lower;
return new Int64(upper, lower);
};

this[nbt.tagTypes.byteArray] = function() {
Expand Down
54 changes: 30 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
{
"name": "nbt",
"version": "v0.3.0",
"description": "A parser for NBT archives",
"keywords": ["nbt", "minecraft"],
"homepage": "https://github.com/sjmulder/nbt-js",
"author": "Sijmen Mulder <sjmulder@gmail.com>",
"main": "nbt",
"repository": {
"type": "git",
"url": "https://github.com/sjmulder/nbt-js.git"
},
"engines": [
"node >=0.10"
],
"devDependencies": {
"grunt": "~0.4.4",
"grunt-cli": "~0.1.13",
"grunt-jasmine-node": "~0.2.1",
"grunt-contrib-jshint": "~0.9.2",
"grunt-contrib-watch": "~0.6.0"
},
"scripts": {
"test": "grunt test"
}
"name": "nbt",
"version": "v0.3.0",
"description": "A parser for NBT archives",
"keywords": [
"nbt",
"minecraft"
],
"homepage": "https://github.com/sjmulder/nbt-js",
"author": "Sijmen Mulder <sjmulder@gmail.com>",
"main": "nbt",
"repository": {
"type": "git",
"url": "https://github.com/sjmulder/nbt-js.git"
},
"engines": [
"node >=0.10"
],
"devDependencies": {
"grunt": "~0.4.4",
"grunt-cli": "~0.1.13",
"grunt-jasmine-node": "~0.2.1",
"grunt-contrib-jshint": "~0.9.2",
"grunt-contrib-watch": "~0.6.0"
},
"scripts": {
"test": "grunt test"
},
"dependencies": {
"node-int64": "^0.3.1"
}
}

0 comments on commit df0c2b3

Please sign in to comment.