@@ -80,13 +80,10 @@ class Packer {
8080 /**
8181 * @constructor
8282 * @param {Chunker } channel the chunker backed by a network channel.
83- * @param {boolean } disableLosslessIntegers if this packer should convert all received integers to native JS numbers
84- * (including native {@link Number} type or our own {@link Integer}) as native {@link Number}.
8583 */
86- constructor ( channel , disableLosslessIntegers = false ) {
84+ constructor ( channel ) {
8785 this . _ch = channel ;
8886 this . _byteArraysSupported = true ;
89- this . _disableLosslessIntegers = disableLosslessIntegers ;
9087 }
9188
9289 /**
@@ -107,7 +104,7 @@ class Packer {
107104 } else if ( typeof ( x ) == "string" ) {
108105 return ( ) => this . packString ( x , onError ) ;
109106 } else if ( isInt ( x ) ) {
110- return this . _packableInteger ( x , onError ) ;
107+ return ( ) => this . packInteger ( x ) ;
111108 } else if ( x instanceof Int8Array ) {
112109 return ( ) => this . packBytes ( x , onError ) ;
113110 } else if ( x instanceof Array ) {
@@ -150,28 +147,6 @@ class Packer {
150147 }
151148 }
152149
153- /**
154- * Creates a packable function out of the provided {@link Integer} value.
155- * @param {Integer } x the value to pack.
156- * @param {function } onError the callback for the case when value cannot be packed.
157- * @return {function }
158- * @private
159- */
160- _packableInteger ( x , onError ) {
161- if ( this . _disableLosslessIntegers ) {
162- // pack Integer objects only when native numbers are not used, fail otherwise
163- // Integer can't represent special values like Number.NEGATIVE_INFINITY
164- // and should not be used at all when native numbers are enabled
165- if ( onError ) {
166- onError ( newError ( `Cannot pack Integer value ${ x } (${ JSON . stringify ( x ) } ) when native numbers are enabled. ` +
167- `Please use native Number instead or disable native number support on the driver level.` ) ) ;
168- }
169- return ( ) => undefined ;
170- } else {
171- return ( ) => this . packInteger ( x ) ;
172- }
173- }
174-
175150 /**
176151 * Packs a struct
177152 * @param signature the signature of the struct
@@ -209,6 +184,7 @@ class Packer {
209184 this . _ch . writeInt32 ( low ) ;
210185 }
211186 }
187+
212188 packFloat ( x ) {
213189 this . _ch . writeUInt8 ( FLOAT_64 ) ;
214190 this . _ch . writeFloat64 ( x ) ;
@@ -343,8 +319,7 @@ class Unpacker {
343319
344320 /**
345321 * @constructor
346- * @param {boolean } disableLosslessIntegers if this unpacker should convert all received integers to native JS numbers
347- * (including native {@link Number} type or our own {@link Integer}) as native {@link Number}.
322+ * @param {boolean } disableLosslessIntegers if this unpacker should convert all received integers to native JS numbers.
348323 */
349324 constructor ( disableLosslessIntegers = false ) {
350325 // Higher level layers can specify how to map structs to higher-level objects.
@@ -368,9 +343,12 @@ class Unpacker {
368343 return boolean ;
369344 }
370345
371- const number = this . _unpackNumber ( marker , buffer ) ;
372- if ( number !== null ) {
373- return number ;
346+ const numberOrInteger = this . _unpackNumberOrInteger ( marker , buffer ) ;
347+ if ( numberOrInteger !== null ) {
348+ if ( this . _disableLosslessIntegers && isInt ( numberOrInteger ) ) {
349+ return numberOrInteger . toNumberOrInfinity ( ) ;
350+ }
351+ return numberOrInteger ;
374352 }
375353
376354 const string = this . _unpackString ( marker , markerHigh , markerLow , buffer ) ;
@@ -411,7 +389,7 @@ class Unpacker {
411389 }
412390 }
413391
414- _unpackNumber ( marker , buffer ) {
392+ _unpackNumberOrInteger ( marker , buffer ) {
415393 if ( marker == FLOAT_64 ) {
416394 return buffer . readFloat64 ( ) ;
417395 } else if ( marker >= 0 && marker < 128 ) {
@@ -428,8 +406,7 @@ class Unpacker {
428406 } else if ( marker == INT_64 ) {
429407 const high = buffer . readInt32 ( ) ;
430408 const low = buffer . readInt32 ( ) ;
431- const integer = new Integer ( low , high ) ;
432- return this . _disableLosslessIntegers ? integer . toNumberOrInfinity ( ) : integer ;
409+ return new Integer ( low , high ) ;
433410 } else {
434411 return null ;
435412 }
0 commit comments