Skip to content

Commit 4b9e03b

Browse files
committed
calculate encoding dynamically as it can change between execution of cached parsers
1 parent e75204b commit 4b9e03b

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

lib/compile_binary_parser.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function compile (fields, options, config) {
1515
var result = [];
1616
var i = 0;
1717
var nullBitmapLength = Math.floor((fields.length + 7 + 2) / 8);
18-
result.push('(function(){ return function BinaryRow(packet) {');
18+
result.push('(function(){ return function BinaryRow(packet, fields, options, CharsetToEncoding) {');
1919

2020
if (options.rowsAsArray) {
2121
result.push(' var result = new Array(' + fields.length + ');');
@@ -73,7 +73,7 @@ function compile (fields, options, config) {
7373
result.push(' if (nullBitmaskByte' + nullByteIndex + ' & ' + currentFieldNullBit + ')');
7474
result.push(' ' + lvalue + ' = null;');
7575
result.push(' else');
76-
result.push(' ' + lvalue + ' = ' + readCodeFor(fields[i], config, options));
76+
result.push(' ' + lvalue + ' = ' + readCodeFor(fields[i], config, options, i));
7777
// }
7878
currentFieldNullBit *= 2;
7979
if (currentFieldNullBit == 0x100) {
@@ -96,7 +96,7 @@ function compile (fields, options, config) {
9696
return vm.runInThisContext(src);
9797
}
9898

99-
function readCodeFor (field, config, options) {
99+
function readCodeFor (field, config, options, fieldNum) {
100100
var supportBigNumbers = options.supportBigNumbers || config.supportBigNumbers;
101101
var bigNumberStrings = options.bigNumberStrings || config.bigNumberStrings;
102102
var unsigned = field.flags & FieldFlags.UNSIGNED;
@@ -135,7 +135,7 @@ function readCodeFor (field, config, options) {
135135
case Types.GEOMETRY:
136136
return 'packet.parseGeometryValue();';
137137
case Types.JSON:
138-
return 'JSON.parse(packet.readLengthCodedString("' + CharsetToEncoding[field.characterSet] + '"));';
138+
return 'JSON.parse(packet.readLengthCodedString(CharsetToEncoding[fields[' + fieldNum + '].characterSet]));';
139139
case Types.LONGLONG:
140140
if (!supportBigNumbers) {
141141
return unsigned ? 'packet.readInt64JSNumber();' : 'packet.readSInt64JSNumber();';
@@ -150,7 +150,7 @@ function readCodeFor (field, config, options) {
150150
if (field.characterSet == Charsets.BINARY) {
151151
return 'packet.readLengthCodedBuffer();';
152152
} else {
153-
return 'packet.readLengthCodedString("' + CharsetToEncoding[field.characterSet] + '")';
153+
return 'packet.readLengthCodedString(CharsetToEncoding[fields[' + fieldNum + '].characterSet])';
154154
}
155155
}
156156
}

lib/compile_text_parser.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function compile (fields, options, config) {
3131
var i = 0;
3232
var lvalue = '';
3333

34-
result.push('(function() { return function TextRow(packet, fields, options) {');
34+
result.push('(function() { return function TextRow(packet, fields, options, CharsetToEncoding) {');
3535
if (options.rowsAsArray) {
3636
result.push(' var result = new Array(' + fields.length + ')');
3737
}
@@ -74,10 +74,10 @@ function compile (fields, options, config) {
7474
} else {
7575
lvalue = ' this[' + srcEscape(fields[i].name) + ']';
7676
}
77-
var encoding = CharsetToEncoding[fields[i].characterSet];
78-
var readCode = readCodeFor(fields[i].columnType, fields[i].characterSet, encoding, config, options);
77+
var encodingExpr = 'CharsetToEncoding[fields[' + i + '].characterSet]';
78+
var readCode = readCodeFor(fields[i].columnType, fields[i].characterSet, encodingExpr, config, options);
7979
if (typeof options.typeCast === 'function') {
80-
result.push(lvalue + ' = options.typeCast(wrap(fields[' + i + '], ' + srcEscape(typeNames[fields[i].columnType]) + ', packet, "' + encoding + '"), function() { return ' + readCode + ';})');
80+
result.push(lvalue + ' = options.typeCast(wrap(fields[' + i + '], ' + srcEscape(typeNames[fields[i].columnType]) + ', packet, ' + encodingExpr + '), function() { return ' + readCode + ';})');
8181
} else if (options.typeCast === false) {
8282
result.push(lvalue + ' = packet.readLengthCodedBuffer();');
8383
} else {
@@ -100,7 +100,7 @@ function compile (fields, options, config) {
100100
return vm.runInThisContext(src);
101101
}
102102

103-
function readCodeFor (type, charset, encoding, config, options) {
103+
function readCodeFor (type, charset, encodingExpr, config, options) {
104104
var supportBigNumbers = options.supportBigNumbers || config.supportBigNumbers;
105105
var bigNumberStrings = options.bigNumberStrings || config.bigNumberStrings;
106106

@@ -143,12 +143,12 @@ function readCodeFor (type, charset, encoding, config, options) {
143143
case Types.GEOMETRY:
144144
return 'packet.parseGeometryValue()';
145145
case Types.JSON:
146-
return 'JSON.parse(packet.readLengthCodedString("' + encoding + '"))';
146+
return 'JSON.parse(packet.readLengthCodedString(' + encodingExpr + '))';
147147
default:
148148
if (charset == Charsets.BINARY) {
149149
return 'packet.readLengthCodedBuffer()';
150150
} else {
151-
return 'packet.readLengthCodedString("' + encoding + '")';
151+
return 'packet.readLengthCodedString(' + encodingExpr + ')';
152152
}
153153
}
154154
}

0 commit comments

Comments
 (0)