|
16 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17 | 17 | */
|
18 | 18 |
|
19 |
| -import {ClassSchema, getClassSchema, getGlobalStore, JitStack, PropertySchema} from '@deepkit/type'; |
| 19 | +import {ClassSchema, getClassSchema, getGlobalStore, JitStack, PropertySchema, reserveVariable} from '@deepkit/type'; |
20 | 20 | import {ClassType, isArray, isObject, toFastProperties} from '@deepkit/core';
|
21 | 21 | import {seekElementSize} from './continuation';
|
22 | 22 | import {
|
@@ -167,24 +167,26 @@ function getPropertySizer(context: Map<string, any>, property: PropertySchema, a
|
167 | 167 |
|
168 | 168 | if (property.type === 'array') {
|
169 | 169 | context.set('digitByteSize', digitByteSize);
|
| 170 | + const i = reserveVariable(context, 'i'); |
170 | 171 | code = `
|
171 | 172 | size += 4; //array size
|
172 |
| - for (let i = 0; i < ${accessor}.length; i++) { |
| 173 | + for (let ${i} = 0; ${i} < ${accessor}.length; ${i}++) { |
173 | 174 | size += 1; //element type
|
174 |
| - size += digitByteSize(i); //element name |
175 |
| - ${getPropertySizer(context, property.getSubType(), `${accessor}[i]`, jitStack)} |
| 175 | + size += digitByteSize(${i}); //element name |
| 176 | + ${getPropertySizer(context, property.getSubType(), `${accessor}[${i}]`, jitStack)} |
176 | 177 | }
|
177 | 178 | size += 1; //null
|
178 | 179 | `;
|
179 | 180 | } else if (property.type === 'map') {
|
180 | 181 | context.set('stringByteLength', stringByteLength);
|
| 182 | + const i = reserveVariable(context, 'i'); |
181 | 183 | code = `
|
182 | 184 | size += 4; //object size
|
183 |
| - for (let i in ${accessor}) { |
184 |
| - if (!${accessor}.hasOwnProperty(i)) continue; |
| 185 | + for (${i} in ${accessor}) { |
| 186 | + if (!${accessor}.hasOwnProperty(${i})) continue; |
185 | 187 | size += 1; //element type
|
186 |
| - size += stringByteLength(i) + 1; //element name + null |
187 |
| - ${getPropertySizer(context, property.getSubType(), `${accessor}[i]`, jitStack)} |
| 188 | + size += stringByteLength(${i}) + 1; //element name + null; |
| 189 | + ${getPropertySizer(context, property.getSubType(), `${accessor}[${i}]`, jitStack)} |
188 | 190 | }
|
189 | 191 | size += 1; //null
|
190 | 192 | `;
|
@@ -249,10 +251,15 @@ export function createBSONSizer(classSchema: ClassSchema, jitStack: JitStack = n
|
249 | 251 | }
|
250 | 252 | `;
|
251 | 253 |
|
252 |
| - const compiled = new Function('Buffer', 'seekElementSize', ...context.keys(), functionCode); |
253 |
| - const fn = compiled.bind(undefined, Buffer, seekElementSize, ...context.values())(); |
254 |
| - prepared(fn); |
255 |
| - return fn; |
| 254 | + try { |
| 255 | + const compiled = new Function('Buffer', 'seekElementSize', ...context.keys(), functionCode); |
| 256 | + const fn = compiled.bind(undefined, Buffer, seekElementSize, ...context.values())(); |
| 257 | + prepared(fn); |
| 258 | + return fn; |
| 259 | + } catch (error) { |
| 260 | + console.log('Error compiling BSON sizer', functionCode); |
| 261 | + throw error; |
| 262 | + } |
256 | 263 | }
|
257 | 264 |
|
258 | 265 | export class Writer {
|
@@ -667,30 +674,32 @@ function getPropertySerializerCode(
|
667 | 674 | } else if (property.type === 'number') {
|
668 | 675 | code = numberSerializer();
|
669 | 676 | } else if (property.type === 'array') {
|
| 677 | + const i = reserveVariable(context, 'i'); |
670 | 678 | code = `
|
671 | 679 | writer.writeByte(${BSON_DATA_ARRAY});
|
672 | 680 | ${nameWriter}
|
673 | 681 | const start = writer.offset;
|
674 | 682 | writer.offset += 4; //size
|
675 | 683 |
|
676 |
| - for (let i = 0; i < ${accessor}.length; i++) { |
| 684 | + for (let ${i} = 0; ${i} < ${accessor}.length; ${i}++) { |
677 | 685 | //${property.getSubType().type}
|
678 |
| - ${getPropertySerializerCode(property.getSubType(), context, `${accessor}[i]`, jitStack, `''+i`)} |
| 686 | + ${getPropertySerializerCode(property.getSubType(), context, `${accessor}[${i}]`, jitStack, `''+${i}`)} |
679 | 687 | }
|
680 | 688 | writer.writeNull();
|
681 | 689 | writer.writeDelayedSize(writer.offset - start, start);
|
682 | 690 | `;
|
683 | 691 | } else if (property.type === 'map') {
|
| 692 | + const i = reserveVariable(context, 'i'); |
684 | 693 | code = `
|
685 | 694 | writer.writeByte(${BSON_DATA_OBJECT});
|
686 | 695 | ${nameWriter}
|
687 | 696 | const start = writer.offset;
|
688 | 697 | writer.offset += 4; //size
|
689 | 698 |
|
690 |
| - for (let i in ${accessor}) { |
691 |
| - if (!${accessor}.hasOwnProperty(i)) continue; |
| 699 | + for (let ${i} in ${accessor}) { |
| 700 | + if (!${accessor}.hasOwnProperty(${i})) continue; |
692 | 701 | //${property.getSubType().type}
|
693 |
| - ${getPropertySerializerCode(property.getSubType(), context, `${accessor}[i]`, jitStack, `i`)} |
| 702 | + ${getPropertySerializerCode(property.getSubType(), context, `${accessor}[${i}]`, jitStack, `${i}`)} |
694 | 703 | }
|
695 | 704 | writer.writeNull();
|
696 | 705 | writer.writeDelayedSize(writer.offset - start, start);
|
|
0 commit comments