Skip to content

Commit

Permalink
refactor: use long to instead of bignum (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
XadillaX committed Jul 21, 2020
1 parent ca5efa3 commit 86566ea
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 22 deletions.
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
language: node_js
node_js:
- "0.10"
- "0.12"
- "4.2"
- "4"
- "6"
- "8"
- "10"

install:
- export CXX="g++-4.8" CXX="gcc-4.8"
Expand Down
4 changes: 2 additions & 2 deletions lib/base_tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Copyright (c) 2015 XadillaX' Gensokyo, all rights
* reserved
*/
var Bignum = require("bignum");
var Long = require("long");
var util = require("util");
var TAG_TYPE_OFFSET = 1;
var _tagIds;
Expand Down Expand Up @@ -181,7 +181,7 @@ BaseTag.prototype.toJSON = function() {
return val;
}

if(val instanceof Bignum) {
if(val instanceof Long) {
return val;
}

Expand Down
23 changes: 13 additions & 10 deletions lib/tags/long.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
var BaseTag = require("../base_tag");
var util = require("util");
var bignum = require("bignum");
var Long = require("long");

var TAGLong = function() {
BaseTag.call(this);
Expand All @@ -17,20 +17,23 @@ util.inherits(TAGLong, BaseTag);

TAGLong.prototype._readBodyFromBuffer = function(buff, offset) {
var sliced = buff.slice(offset, offset + 8);
this.value = bignum.fromBuffer(sliced, { size: 8 });
this.value = Long.fromBytesBE(sliced, true);
return 8;
};

TAGLong.prototype.calcBufferLength = BaseTag._returnSize(8);

TAGLong.prototype.writeBuffer = function(buff, offset) {
this.value.toBuffer({ endian: "big", size: 8 }).copy(buff, offset);
var bytes = this.value.toBytesBE();
for(var i = 0, j = offset; i < 8; i++, j++) {
buff.writeUInt8(bytes[i], j);
}
return 8;
};

var _longBound = {
min: bignum("-9223372036854775808"),
max: bignum("9223372036854775807")
min: Long.fromString("-9223372036854775808"),
max: Long.fromString("9223372036854775807")
};

TAGLong.prototype.toJSON = function() {
Expand All @@ -40,18 +43,18 @@ TAGLong.prototype.toJSON = function() {
TAGLong.prototype.setValue = function(value) {
var temp = -1;
if(typeof value === "string") {
temp = bignum(value);
} else if(value instanceof bignum) {
temp = value;
temp = new Long(value);
} else if(value instanceof Long) {
temp = Long.fromString(value.toString());
} else if(typeof value === "number" && !isNaN(value)) {
temp = bignum(value);
temp = Long.fromNumber(value);
}

if(-1 === temp) {
throw new Error("Wrong type to set TAG_Long's value.");
}

if(temp.lt(_longBound.min) || temp.gt(_longBound.max)) {
if(temp.lessThan(_longBound.min) || temp.greaterThan(_longBound.max)) {
throw new Error("Value of TAG_Long should between " +
"-9223372036854775808 and 9223372036854775807");
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"homepage": "https://github.com/XadillaX/mcnbt",
"dependencies": {
"bignum": "^0.11.0"
"long": "^4.0.0"
},
"devDependencies": {
"coveralls": "^2.11.4",
Expand Down
10 changes: 6 additions & 4 deletions test/archive.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
"use strict";

var bignum = require("bignum");
var Long = require("long");
var fs = require("fs");
var util = require("util");

Expand Down Expand Up @@ -62,10 +62,12 @@ describe("Real archive test", function() {
if(typeof result === "object" && !util.isArray(result)) {
node.getType().should.be.eql("TAG_Compound");
} else if(typeof result === "string") {
if(bignum(result).toString() === result) {
node.getType().should.match(/^TAG_(String|Long)$/);
if(result === "") {
node.getType().should.match(/^TAG_String/);
} else if(Long.fromString(result).toString() === result) {
node.getType().should.match(/^TAG_(Long|String)$/);
} else {
node.getType().should.be.eql("TAG_String");
node.getType().should.match(/^TAG_String$/);
}
} else if(typeof result === "number" && result < 256) {
node.getType().should.match(/^TAG_(Int|Byte|Double)$/);
Expand Down
4 changes: 2 additions & 2 deletions test/bigtest_archive.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
"use strict";

var bignum = require("bignum");
var Long = require("long");
var fs = require("fs");
var util = require("util");

Expand Down Expand Up @@ -63,7 +63,7 @@ describe("Real bugtest archive test", function() {
} else if(typeof result === "object" && !util.isArray(result)) {
node.getType().should.be.eql("TAG_Compound");
} else if(typeof result === "string") {
if(bignum(result).toString() === result) {
if(Long.fromString(result).toString() === result) {
node.getType().should.match(/^TAG_(String|Long)$/);
} else {
node.getType().should.be.eql("TAG_String");
Expand Down

0 comments on commit 86566ea

Please sign in to comment.