diff --git a/lib/parsers/binary_parser.js b/lib/parsers/binary_parser.js index 5a08df7b7c..1083204f04 100644 --- a/lib/parsers/binary_parser.js +++ b/lib/parsers/binary_parser.js @@ -169,6 +169,10 @@ function compile(fields, options, config) { lvalue = `result[${fieldName}]`; } + parserFn(`if (nullBitmaskByte${nullByteIndex} & ${currentFieldNullBit}) `); + parserFn(`${lvalue} = null;`); + parserFn('else {'); + if (options.typeCast === false) { parserFn(`${lvalue} = packet.readLengthCodedBuffer();`); } else { @@ -176,9 +180,6 @@ function compile(fields, options, config) { parserFn(`const ${fieldWrapperVar} = wrap(fields[${i}], packet);`); const readCode = readCodeFor(fields[i], config, options, i); - parserFn(`if (nullBitmaskByte${nullByteIndex} & ${currentFieldNullBit})`); - parserFn(`${lvalue} = null;`); - parserFn('else {'); if (typeof options.typeCast === 'function') { parserFn( `${lvalue} = options.typeCast(${fieldWrapperVar}, function() { return ${readCode} });`, @@ -186,8 +187,9 @@ function compile(fields, options, config) { } else { parserFn(`${lvalue} = ${readCode};`); } - parserFn('}'); } + parserFn('}'); + currentFieldNullBit *= 2; if (currentFieldNullBit === 0x100) {