From 18096818f39788b1ca7c557a3b0389bb71761960 Mon Sep 17 00:00:00 2001 From: deathcap Date: Fri, 28 Mar 2014 19:10:25 -0700 Subject: [PATCH] Use node-int64 for 64-bit long integers. Closes GH-1 --- nbt-spec.js | 16 +++++++++++----- nbt.js | 4 ++-- package.json | 54 +++++++++++++++++++++++++++++----------------------- 3 files changed, 43 insertions(+), 31 deletions(-) diff --git a/nbt-spec.js b/nbt-spec.js index 66ef03f..b787cf0 100644 --- a/nbt-spec.js +++ b/nbt-spec.js @@ -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() { diff --git a/nbt.js b/nbt.js index 94d9862..c222624 100644 --- a/nbt.js +++ b/nbt.js @@ -15,6 +15,7 @@ var nbt = this; var zlib = require('zlib'); + var Int64 = require('node-int64'); nbt.tagTypes = { 'end': 0, @@ -56,10 +57,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() { diff --git a/package.json b/package.json index c5ba30c..05af7e8 100644 --- a/package.json +++ b/package.json @@ -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 ", - "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 ", + "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" + } }