@@ -1062,12 +1062,14 @@ export class Parser {
10621062 parser . varName = ctx . generateVariable ( parser . varName ) ;
10631063 ctx . bitFields . push ( parser ) ;
10641064
1065- let sum = 0 ;
10661065 if (
10671066 ! this . next ||
10681067 ( this . next && [ 'bit' , 'nest' ] . indexOf ( this . next . type ) < 0 )
10691068 ) {
1070- ctx . bitFields . forEach ( parser => { sum += parser . options . length as number ; } ) ;
1069+ let sum = 0 ;
1070+ ctx . bitFields . forEach ( parser => {
1071+ sum += parser . options . length as number ;
1072+ } ) ;
10711073
10721074 if ( sum <= 8 ) {
10731075 sum = 8 ;
@@ -1082,35 +1084,36 @@ export class Parser {
10821084 'Currently, bit field sequences longer than 4-bytes is not supported.'
10831085 ) ;
10841086 }
1085- }
10861087
1087- const tmpVal = ctx . generateTmpVariable ( ) ;
1088- ctx . pushCode ( `var ${ tmpVal } = 0;` ) ;
1089- let bitOffset = 0 ;
1090- ctx . bitFields . forEach ( parser => {
1091- ctx . pushCode (
1092- `${ tmpVal } |= (${ parser . varName } << ${
1093- sum - ( parser . options . length as number ) - bitOffset } );`
1094- ) ;
1095- ctx . pushCode ( `${ tmpVal } = ${ tmpVal } >>> 0;` ) ;
1096- bitOffset += parser . options . length as number ;
1097- } ) ;
1098- if ( sum == 8 ) {
1099- ctx . pushCode ( `smartBuffer.writeUInt8(${ tmpVal } );` ) ;
1100- } else if ( sum == 16 ) {
1101- ctx . pushCode ( `smartBuffer.writeUInt16BE(${ tmpVal } );` ) ;
1102- } else if ( sum == 24 ) {
1103- const val1 = ctx . generateTmpVariable ( ) ;
1104- const val2 = ctx . generateTmpVariable ( ) ;
1105- ctx . pushCode ( `var ${ val1 } = (${ tmpVal } >>> 8);` ) ;
1106- ctx . pushCode ( `var ${ val2 } = (${ tmpVal } & 0x0ff);` ) ;
1107- ctx . pushCode ( `smartBuffer.writeUInt16BE(${ val1 } );` ) ;
1108- ctx . pushCode ( `smartBuffer.writeUInt8(${ val2 } );` ) ;
1109- } else if ( sum == 32 ) {
1110- ctx . pushCode ( `smartBuffer.writeUInt32BE(${ tmpVal } );` ) ;
1111- }
1112-
1113- ctx . bitFields = [ ] ;
1088+
1089+ const tmpVal = ctx . generateTmpVariable ( ) ;
1090+ ctx . pushCode ( `var ${ tmpVal } = 0;` ) ;
1091+ let bitOffset = 0 ;
1092+ ctx . bitFields . forEach ( parser => {
1093+ ctx . pushCode (
1094+ `${ tmpVal } |= (${ parser . varName } << ${
1095+ sum - ( parser . options . length as number ) - bitOffset } );`
1096+ ) ;
1097+ ctx . pushCode ( `${ tmpVal } = ${ tmpVal } >>> 0;` ) ;
1098+ bitOffset += parser . options . length as number ;
1099+ } ) ;
1100+ if ( sum == 8 ) {
1101+ ctx . pushCode ( `smartBuffer.writeUInt8(${ tmpVal } );` ) ;
1102+ } else if ( sum == 16 ) {
1103+ ctx . pushCode ( `smartBuffer.writeUInt16BE(${ tmpVal } );` ) ;
1104+ } else if ( sum == 24 ) {
1105+ const val1 = ctx . generateTmpVariable ( ) ;
1106+ const val2 = ctx . generateTmpVariable ( ) ;
1107+ ctx . pushCode ( `var ${ val1 } = (${ tmpVal } >>> 8);` ) ;
1108+ ctx . pushCode ( `var ${ val2 } = (${ tmpVal } & 0x0ff);` ) ;
1109+ ctx . pushCode ( `smartBuffer.writeUInt16BE(${ val1 } );` ) ;
1110+ ctx . pushCode ( `smartBuffer.writeUInt8(${ val2 } );` ) ;
1111+ } else if ( sum == 32 ) {
1112+ ctx . pushCode ( `smartBuffer.writeUInt32BE(${ tmpVal } );` ) ;
1113+ }
1114+
1115+ ctx . bitFields = [ ] ;
1116+ }
11141117 }
11151118
11161119 private generateSeek ( ctx : Context ) {
0 commit comments