Skip to content

Commit

Permalink
Move mashalling of type from writeSimple into the simple marshallers
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Karas committed Apr 1, 2015
1 parent bb2cea3 commit e4b106e
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 82 deletions.
79 changes: 1 addition & 78 deletions lib/marshall.js
Expand Up @@ -109,83 +109,6 @@ function writeSimple(ps, type, data) {
var buff;

var simpleMarshaller = Marshallers.MakeSimpleMarshaller(type);
simpleMarshaller.check(data);

switch (type) {
case 'y':
// byte
ps.word8(data);
ps._offset++;
break;
case 'n':
// 16 bit signed bit
align(ps, 2);
buff = new Buffer(4);
buff.writeInt16LE(parseInt(data), 0);
ps.put(buff);
ps._offset += 2;
break;
case 'q':
// 16 bit unsigned int
align(ps, 2);
ps.word16le(data);
ps._offset += 2;
break;
case 'b':
// booleans serialised as 0/1 unsigned 32 bit int
// TODO: require boolean type? Require input to be exactly 0/1?
if (data) data = 1;
else data = 0;
case 'u':
// 32 bit unsigned int
align(ps, 4);
ps.word32le(data);
ps._offset += 4;
break;
case 'i':
align(ps, 4);
buff = new Buffer(4);
buff.writeInt32LE(parseInt(data), 0);
ps.put(buff);
ps._offset += 4;
break;
case 'g':
// signature
parseSignature(data); // validate, throws exeption
buff = new Buffer(data, 'ascii');
ps.word8(data.length).put(buff).word8(0);
ps._offset += 2 + buff.length;
break;
case 'o':
// object path
// TODO: verify object path here?
case 's':
// utf8 string
align(ps, 4);
buff = new Buffer(data, 'utf8');
ps.word32le(buff.length).put(buff).word8(0);
ps._offset += 5 + buff.length;
break;
case 'x':
align(ps, 8);
// TODO: replace with signed word64!
ps.word64le(data);
ps._offset += 8;
break;
case 't':
align(ps, 8);
ps.word64le(data);
ps._offset += 8;
break;
case 'd':
align(ps, 8);
buff = new Buffer(8);
buff.writeDoubleLE(parseFloat(data), 0);
ps.put(buff);
ps._offset += 8;
break;
default:
throw new Error('Unknown data type format: ' + type);
}
simpleMarshaller.marshall(ps, data);
return ps;
}
89 changes: 85 additions & 4 deletions lib/marshallers.js
Expand Up @@ -46,71 +46,147 @@ var MakeSimpleMarshaller = function(signature) {
}

switch(signature) {
case 'o':
// object path
// TODO: verify object path here?
case 's':
//STRING
marshaller.check = function(data) {
checkValidString(data);
};
marshaller.marshall = function(ps,data) {
this.check(data);
// utf8 string
align(ps, 4);
buff = new Buffer(data, 'utf8');
ps.word32le(buff.length).put(buff).word8(0);
ps._offset += 5 + buff.length;
};
break;
case 'g':
//SIGNATURE
marshaller.check = function(data) {
checkValidString(data);
checkValidSignature(data);
};
marshaller.marshall = function(ps,data) {
this.check(data);
// signature
buff = new Buffer(data, 'ascii');
ps.word8(data.length).put(buff).word8(0);
ps._offset += 2 + buff.length;
};
break;
case 'y':
//BYTE
marshaller.check = function(data) {
checkInteger(data);
checkRange(0x00,0xFF,data);
};
marshaller.marshall = function(ps, data) {
this.check(data);
ps.word8(data);
ps._offset++;
};
break;
case 'b':
//BOOLEAN
marshaller.check = function(data) {
checkBoolean(data);
};
marshaller.marshall = function(ps, data) {
this.check(data);
// booleans serialised as 0/1 unsigned 32 bit int
if (data) data = 1;
else data = 0;
align(ps, 4);
ps.word32le(data);
ps._offset += 4;
};
break;
case 'n':
//INT16
marshaller.check = function(data) {
checkInteger(data);
checkRange(-0x7FFF-1,0x7FFF,data);
};
marshaller.marshall = function(ps,data) {
this.check(data);
align(ps, 2);
buff = new Buffer(4);
buff.writeInt16LE(parseInt(data), 0);
ps.put(buff);
ps._offset += 2;
};
break;
case 'q':
//UINT16
marshaller.check = function(data) {
checkInteger(data);
checkRange(0,0xFFFF,data);
};
marshaller.marshall = function(ps,data) {
this.check(data);
align(ps, 2);
ps.word16le(data);
ps._offset += 2;
};
break;
case 'i':
//INT32
marshaller.check = function(data) {
checkInteger(data);
checkRange(-0x7FFFFFFF-1,0x7FFFFFFF,data);
};
marshaller.marshall = function(ps,data) {
this.check(data);
align(ps, 4);
buff = new Buffer(4);
buff.writeInt32LE(parseInt(data), 0);
ps.put(buff);
ps._offset += 4;
};
break;
case 'u':
//UINT32
marshaller.check = function(data) {
checkInteger(data);
checkRange(0,0xFFFFFFFF,data);
};
marshaller.marshall = function(ps,data) {
this.check(data);
// 32 t unsigned int
align(ps, 4);
ps.word32le(data);
ps._offset += 4;
};
break;
case 't':
//UINT64
marshaller.check = function(data) {
throw new Error("64 Bit integers not supported");
};
marshaller.marshall = function(ps,data) {
this.check(data);
// while the check method will throw an error - this code is left here
// for when 64 bits will become supported
align(ps, 8);
ps.word64le(data);
ps._offset += 8;
};
break;
case 'x':
//INT64
marshaller.check = function(data) {
throw new Error("64 Bit integers not supported");
};
marshaller.marshall = function(ps,data) {
this.check(data);
// while the check method will throw an error - this code is left here
// for when 64 bits will become supported
ps.word64le(data);
ps._offset += 8;
};
break;
case 'd':
//DOUBLE
Expand All @@ -125,12 +201,17 @@ var MakeSimpleMarshaller = function(signature) {
throw new Error("Number outside range");
}
};
marshaller.marshall = function(ps,data) {
this.check(data);
align(ps, 8);
buff = new Buffer(8);
buff.writeDoubleLE(parseFloat(data), 0);
ps.put(buff);
ps._offset += 8;
};
break;
default:
// TODO
// until all signatures are complete we will have a check
// that does nothing
marshaller.check = function() {};
throw new Error('Unknown data type format: ' + type);
}
return marshaller;
};
Expand Down

0 comments on commit e4b106e

Please sign in to comment.