Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support to Bignum #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ var MARSHAL_SYM_REF = ';'.charCodeAt(0);
var MARSHAL_INSTANCEVAR = 'I'.charCodeAt(0);
var MARSHAL_IVAR_STR = '"'.charCodeAt(0);
var MARSHAL_FLOAT = 'f'.charCodeAt(0);
var MARSHAL_BIGNUM = 'l'.charCodeAt(0);

var ints = require('./lib/ints');
var bigInt = require('big-integer');

var _parse = function(buffer) {
var offset = 0;
Expand Down Expand Up @@ -106,7 +108,16 @@ var _parse = function(buffer) {
hashOut[key] = val;
}
return hashOut;
break;
case MARSHAL_BIGNUM:
var sign = buffer[offset + 1] == 43 ? 1 : -1;
var length = ints.load(buffer.slice(offset + 2, offset + 3)) * 2;
offset += 3;
var bignum = bigInt.zero;
for(var i = 0; i < length; i ++) {
bignum = bignum.add(bigInt(buffer[offset + i]).multiply(bigInt(2).pow(i * 8)))
offset ++
}
return bignum.multiply(sign)
default:
throw new Error('Unexpected data, value ' + buffer[offset].toString(16) + ' at offset ' + offset + ' on ' + buffer.toString('hex') + '. Parsing this sort of data is probably not yet implemented!');
}
Expand Down Expand Up @@ -187,6 +198,10 @@ var _dump = function(value) {
);
}

if(bigInt.isInstance(value)) {
throw new Error('Bignum dump is not yet implemented')
}

if(value === Object(value)) {
return Buffer.concat(
[ new Buffer([MARSHAL_HASH]),
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"serialize",
"unserialize"
],
"dependencies": {
"big-integer": "1.6.0"
},
"devDependencies": {
"mocha": "1.18.2"
},
Expand Down
21 changes: 21 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var marsha = require('../');
var assert = require('assert');
var fs = require('fs');
var bigInt = require('big-integer');
var cases = JSON.parse(fs.readFileSync('./test/bidirectional_examples.json'));
var complexCases = JSON.parse(fs.readFileSync('./test/other_examples.json'));

Expand Down Expand Up @@ -32,6 +33,16 @@ describe('marsha', function() {
assert.strictEqual(marsha.load(buffer), -Infinity);
});

it('parses Bignum', function() {
var buffer = new Buffer('04086c2b0968a0bf5909934051', 'hex');
assert.ok(marsha.load(buffer).equals(bigInt("5854841183951364200")));
});

it('parses negative Bignum', function() {
var buffer = new Buffer('04086c2d09fcecf6f9944a2b6a', 'hex');
assert.ok(marsha.load(buffer).equals(bigInt("-7650290395728243964")));
});

Object.keys(cases).forEach(function(hex) {
it('unserializes ' + hex + ' to ' + JSON.stringify(cases[hex]), function() {
var buffer = new Buffer(hex, 'hex');
Expand Down Expand Up @@ -66,6 +77,16 @@ describe('marsha', function() {
assert.equal(buffer.toString('hex'), '040866092d696e66');
});

xit('outputs Bignum', function() {
var buffer = marsha.dump(bigInt("5854841183951364200"));
assert.equal(buffer.toString('hex'), '04086c2b0968a0bf5909934051');
});

xit('outputs negative Bignum', function() {
var buffer = marsha.dump(bigInt("-7650290395728243964"));
assert.equal(buffer.toString('hex'), '04086c2d09fcecf6f9944a2b6a');
});

Object.keys(cases).forEach(function(hex) {
it('serializes ' + JSON.stringify(cases[hex]) + ' to ' + hex, function() {
var buffer = marsha.dump(cases[hex]);
Expand Down