@@ -65,11 +65,7 @@ class Structure {
6565 for ( var i = 0 ; i < this . fields . length ; i ++ ) {
6666 if ( i > 0 ) { fieldStr += ", " }
6767 fieldStr += this . fields [ i ] ;
68- } ;
69-
70- var util = require ( 'util' )
71- console . log ( util . inspect ( this ) ) ;
72-
68+ }
7369 return "Structure(" + this . signature + ", [" + this . fields + "])"
7470 }
7571}
@@ -128,25 +124,26 @@ class Packer {
128124 var high = x . high ,
129125 low = x . low ;
130126
131- this . _ch . writeUInt8 ( INT_64 ) ;
132- this . _ch . writeUInt32 ( high ) ;
133- this . _ch . writeUInt32 ( low ) ;
134- // TODO Use the branches below to sort out most efficient packed format
135- // if (-0x10 <= x && x < 0x80) {
136- // this._ch.writeUInt8(x);
137- // } else if (-0x80 <= x && x < -0x10) {
138- // this._ch.writeUInt8(INT_8);
139- // this._ch.writeUInt8(x);
140- // } else if (-0x8000 <= x && x < 0x8000) {
141- // this._ch.writeUInt8(INT_16);
142- // this._ch.writeInt16(x);
143- // } else if (-0x80000000 <= x && x < 0x80000000) {
144- // this._ch.writeUInt8(INT_32);
145- // this._ch.writeInt32(x);
146- // } else {
147- //
148- // }
149-
127+ if ( x . greaterThanOrEqual ( - 0x10 ) && x . lessThan ( 0x80 ) ) {
128+ this . _ch . writeInt8 ( low ) ;
129+ }
130+ else if ( x . greaterThanOrEqual ( - 0x80 ) && x . lessThan ( - 0x10 ) ) {
131+ this . _ch . writeUInt8 ( INT_8 ) ;
132+ this . _ch . writeInt8 ( low ) ;
133+ }
134+ else if ( x . greaterThanOrEqual ( - 0x8000 ) && x . lessThan ( 0x8000 ) ) {
135+ this . _ch . writeUInt8 ( INT_16 ) ;
136+ this . _ch . writeInt16 ( low ) ;
137+ }
138+ else if ( x . greaterThanOrEqual ( - 0x80000000 ) && x . lessThan ( 0x80000000 ) ) {
139+ this . _ch . writeUInt8 ( INT_32 ) ;
140+ this . _ch . writeInt32 ( low ) ;
141+ }
142+ else {
143+ this . _ch . writeUInt8 ( INT_64 ) ;
144+ this . _ch . writeInt32 ( high ) ;
145+ this . _ch . writeInt32 ( low ) ;
146+ }
150147 }
151148
152149 packFloat ( x ) {
@@ -171,7 +168,7 @@ class Packer {
171168 this . _ch . writeBytes ( bytes ) ;
172169 } else if ( size < 0x100000000 ) {
173170 this . _ch . writeUInt8 ( STRING_32 ) ;
174- this . _ch . writeUInt8 ( ( size / 16777216 >> 0 ) % 256 ) ; // TODO: Why is it shifting by 0 here?
171+ this . _ch . writeUInt8 ( ( size / 16777216 >> 0 ) % 256 ) ;
175172 this . _ch . writeUInt8 ( ( size / 65536 >> 0 ) % 256 ) ;
176173 this . _ch . writeUInt8 ( ( size / 256 >> 0 ) % 256 ) ;
177174 this . _ch . writeUInt8 ( size % 256 ) ;
@@ -188,7 +185,9 @@ class Packer {
188185 this . _ch . writeUInt8 ( LIST_8 )
189186 this . _ch . writeUInt8 ( size ) ;
190187 } else if ( size < 0x10000 ) {
191- this . _ch . writeUInt8 ( LIST_16 , size / 256 >> 0 , size % 256 ) ;
188+ this . _ch . writeUInt8 ( LIST_16 ) ;
189+ this . _ch . writeUInt8 ( ( size / 256 >> 0 ) % 256 ) ;
190+ this . _ch . writeUInt8 ( size % 256 ) ;
192191 } else if ( size < 0x100000000 ) {
193192 this . _ch . writeUInt8 ( LIST_32 ) ;
194193 this . _ch . writeUInt8 ( ( size / 16777216 >> 0 ) % 256 ) ;
@@ -301,10 +300,11 @@ class Unpacker {
301300 } else if ( marker == INT_16 ) {
302301 return int ( buffer . readInt16 ( ) ) ;
303302 } else if ( marker == INT_32 ) {
304- return int ( buffer . readInt32 ( ) ) ;
303+ let b = buffer . readInt32 ( ) ;
304+ return int ( b ) ;
305305 } else if ( marker == INT_64 ) {
306306 let high = buffer . readInt32 ( ) ;
307- let low = buffer . readUInt32 ( ) ;
307+ let low = buffer . readInt32 ( ) ;
308308 return new Integer ( low , high ) ;
309309 } else if ( marker == STRING_8 ) {
310310 return utf8 . decode ( buffer , buffer . readUInt8 ( ) ) ;
0 commit comments