@@ -127,15 +127,13 @@ const pointerReads = new WeakMap();
127127/*
128128 Reads a value from the specified bytes at the specified offset, given a type
129129 Returns the value that was read and the number of bytes consumed (excepting any values being pointed to)
130- Pointer start refers to the position in the buffer where the root value starts
131- (pointers will be relative to this location)
132130*/
133- function consumeValue ( { buffer, pointerStart , offset, type : readType , baseValue } ) {
131+ function consumeValue ( { buffer, offset, type : readType , baseValue } ) {
134132 let readValue , length ;
135133 switch ( readType . constructor ) {
136134 case t . ByteType : {
137135 length = 1 ;
138- assert_1 . default ( buffer . byteLength >= offset + length , NOT_LONG_ENOUGH ) ;
136+ assert_1 . default ( buffer . byteLength > offset , NOT_LONG_ENOUGH ) ;
139137 readValue = new Int8Array ( buffer ) [ offset ] ; //endianness doesn't matter because there is only 1 byte
140138 break ;
141139 }
@@ -304,7 +302,7 @@ function consumeValue({ buffer, pointerStart, offset, type: readType, baseValue
304302 const castType = readType ;
305303 readValue = baseValue || makeBaseValue ( castType ) ;
306304 for ( let i = 0 ; i < castType . length ; i ++ ) {
307- const element = consumeValue ( { buffer, pointerStart , offset : offset + length , type : castType . type } ) ;
305+ const element = consumeValue ( { buffer, offset : offset + length , type : castType . type } ) ;
308306 length += element . length ;
309307 readValue [ i ] = element . value ;
310308 }
@@ -315,7 +313,7 @@ function consumeValue({ buffer, pointerStart, offset, type: readType, baseValue
315313 const castType = readType ;
316314 readValue = baseValue || makeBaseValue ( castType ) ;
317315 for ( const field of castType . fields ) {
318- const readField = consumeValue ( { buffer, pointerStart , offset : offset + length , type : field . type } ) ;
316+ const readField = consumeValue ( { buffer, offset : offset + length , type : field . type } ) ;
319317 readValue [ field . name ] = readField . value ;
320318 length += readField . length ;
321319 }
@@ -328,7 +326,7 @@ function consumeValue({ buffer, pointerStart, offset, type: readType, baseValue
328326 const castType = readType ;
329327 readValue = baseValue || makeBaseValue ( castType , arrayLength ) ;
330328 for ( let i = 0 ; i < arrayLength ; i ++ ) {
331- const element = consumeValue ( { buffer, pointerStart , offset : offset + length , type : castType . type } ) ;
329+ const element = consumeValue ( { buffer, offset : offset + length , type : castType . type } ) ;
332330 length += element . length ;
333331 readValue [ i ] = element . value ;
334332 }
@@ -341,7 +339,7 @@ function consumeValue({ buffer, pointerStart, offset, type: readType, baseValue
341339 const castType = readType ;
342340 readValue = baseValue || makeBaseValue ( castType ) ;
343341 for ( let i = 0 ; i < setSize ; i ++ ) {
344- const element = consumeValue ( { buffer, pointerStart , offset : offset + length , type : castType . type } ) ;
342+ const element = consumeValue ( { buffer, offset : offset + length , type : castType . type } ) ;
345343 length += element . length ;
346344 readValue . add ( element . value ) ;
347345 }
@@ -353,9 +351,9 @@ function consumeValue({ buffer, pointerStart, offset, type: readType, baseValue
353351 const castType = readType ;
354352 readValue = baseValue || makeBaseValue ( castType ) ;
355353 for ( let i = 0 ; i < size . value ; i ++ ) {
356- const keyElement = consumeValue ( { buffer, pointerStart , offset : offset + length , type : castType . keyType } ) ;
354+ const keyElement = consumeValue ( { buffer, offset : offset + length , type : castType . keyType } ) ;
357355 length += keyElement . length ;
358- const valueElement = consumeValue ( { buffer, pointerStart , offset : offset + length , type : castType . valueType } ) ;
356+ const valueElement = consumeValue ( { buffer, offset : offset + length , type : castType . valueType } ) ;
359357 length += valueElement . length ;
360358 readValue . set ( keyElement . value , valueElement . value ) ;
361359 }
@@ -376,7 +374,6 @@ function consumeValue({ buffer, pointerStart, offset, type: readType, baseValue
376374 const typeIndex = new Uint8Array ( buffer ) [ offset ] ;
377375 const subValue = consumeValue ( {
378376 buffer,
379- pointerStart,
380377 offset : offset + length ,
381378 type : readType . types [ typeIndex ]
382379 } ) ;
@@ -395,7 +392,6 @@ function consumeValue({ buffer, pointerStart, offset, type: readType, baseValue
395392 const constructor = constructorRegistry . get ( typeConstructor . name ) ;
396393 const subValue = consumeValue ( {
397394 buffer,
398- pointerStart,
399395 offset : offset + length ,
400396 type : castType . constructorTypes [ typeIndex ] . type ,
401397 baseValue : new constructor
@@ -416,7 +412,7 @@ function consumeValue({ buffer, pointerStart, offset, type: readType, baseValue
416412 readRecursives . set ( buffer , bufferReadRecursives ) ;
417413 }
418414 bufferReadRecursives . set ( offset + length , readValue ) ;
419- length += consumeValue ( { buffer, pointerStart , offset : offset + length , type : subType , baseValue : readValue } ) . length ;
415+ length += consumeValue ( { buffer, offset : offset + length , type : subType , baseValue : readValue } ) . length ;
420416 }
421417 else {
422418 const indexOffset = readFlexInt ( buffer , offset + length ) ;
@@ -433,7 +429,6 @@ function consumeValue({ buffer, pointerStart, offset, type: readType, baseValue
433429 if ( nonNull ) {
434430 const subValue = consumeValue ( {
435431 buffer,
436- pointerStart,
437432 offset : offset + length ,
438433 type : readType . type
439434 } ) ;
@@ -445,8 +440,18 @@ function consumeValue({ buffer, pointerStart, offset, type: readType, baseValue
445440 break ;
446441 }
447442 case t . PointerType : {
448- length = 4 ;
449- assert_1 . default ( buffer . byteLength >= offset + length , NOT_LONG_ENOUGH ) ;
443+ length = 1 ;
444+ let explicitValue = true ;
445+ while ( true ) {
446+ const offsetDiff = readFlexInt ( buffer , offset ) ;
447+ if ( ! offsetDiff . value )
448+ break ;
449+ if ( explicitValue ) {
450+ ( { length } = offsetDiff ) ;
451+ explicitValue = false ;
452+ }
453+ offset -= offsetDiff . value ;
454+ }
450455 let bufferPointerReads = pointerReads . get ( buffer ) ;
451456 if ( ! bufferPointerReads ) {
452457 bufferPointerReads = new Map ;
@@ -458,17 +463,16 @@ function consumeValue({ buffer, pointerStart, offset, type: readType, baseValue
458463 bufferTypePointerReads = new Map ;
459464 bufferPointerReads . set ( castType , bufferTypePointerReads ) ;
460465 }
461- const location = new DataView ( buffer ) . getUint32 ( offset ) ;
462- if ( bufferTypePointerReads . has ( location ) )
463- readValue = bufferTypePointerReads . get ( location ) ;
464- else {
465- readValue = consumeValue ( {
466+ readValue = bufferTypePointerReads . get ( offset ) ;
467+ if ( readValue === undefined ) {
468+ const explicitRead = consumeValue ( {
466469 buffer,
467- pointerStart,
468- offset : pointerStart + location ,
470+ offset : offset + length ,
469471 type : castType . type
470- } ) . value ;
471- bufferTypePointerReads . set ( location , readValue ) ;
472+ } ) ;
473+ readValue = explicitRead . value ;
474+ length += explicitRead . length ;
475+ bufferTypePointerReads . set ( offset , readValue ) ;
472476 }
473477 break ;
474478 }
@@ -564,7 +568,6 @@ function consumeType(typeBuffer, offset) {
564568 const valueLocation = offset + length ;
565569 const enumValue = consumeValue ( {
566570 buffer : typeBuffer ,
567- pointerStart : valueLocation ,
568571 offset : valueLocation ,
569572 type : valueType . value
570573 } ) ;
@@ -726,8 +729,7 @@ function value(params) {
726729 const readValue = consumeValue ( {
727730 buffer,
728731 offset,
729- type : readType ,
730- pointerStart : offset
732+ type : readType
731733 } ) . value ;
732734 //no length validation because bytes being pointed to don't get counted in the length
733735 return readValue ;
0 commit comments