@@ -178,6 +178,7 @@ export class Parser {
178178 endian : Endianess = 'be' ;
179179 constructorFn : Function | null = null ;
180180 alias : string | null = null ;
181+ useContextVariables : boolean = false ;
181182
182183 constructor ( ) { }
183184
@@ -637,6 +638,12 @@ export class Parser {
637638 return this ;
638639 }
639640
641+ useContextVars ( useContextVariables : boolean = true ) {
642+ this . useContextVariables = useContextVariables ;
643+
644+ return this ;
645+ }
646+
640647 create ( constructorFn : Function ) {
641648 if ( ! ( constructorFn instanceof Function ) ) {
642649 throw new Error ( 'Constructor must be a Function object.' ) ;
@@ -648,7 +655,7 @@ export class Parser {
648655 }
649656
650657 private getContext ( importPath ?: string ) {
651- const ctx = new Context ( importPath ) ;
658+ const ctx = new Context ( importPath , this . useContextVariables ) ;
652659
653660 ctx . pushCode (
654661 'var dataView = new DataView(buffer.buffer, buffer.byteOffset, buffer.length);'
@@ -673,15 +680,16 @@ export class Parser {
673680 ctx . pushCode (
674681 `var vars = ${ this . constructorFn ? 'new constructorFn()' : '{}' } ;`
675682 ) ;
683+
676684 ctx . pushCode ( 'vars.$parent = null;' ) ;
677685 ctx . pushCode ( 'vars.$root = vars;' ) ;
678686
679687 this . generate ( ctx ) ;
680-
681688 this . resolveReferences ( ctx ) ;
682689
683690 ctx . pushCode ( 'delete vars.$parent;' ) ;
684691 ctx . pushCode ( 'delete vars.$root;' ) ;
692+
685693 ctx . pushCode ( 'return vars;' ) ;
686694 }
687695
@@ -691,7 +699,7 @@ export class Parser {
691699 `var vars = ${ this . constructorFn ? 'new constructorFn()' : '{}' } ;`
692700 ) ;
693701 ctx . pushCode (
694- ` var ctx = Object.assign({$parent: null, $root: vars}, context || {});`
702+ ' var ctx = Object.assign({$parent: null, $root: vars}, context || {});'
695703 ) ;
696704 ctx . pushCode ( `vars = Object.assign(vars, ctx);` ) ;
697705
@@ -1093,13 +1101,15 @@ export class Parser {
10931101 ) ;
10941102 ctx . pushCode ( `offset += ${ PRIMITIVE_SIZES [ type as PrimitiveTypes ] } ;` ) ;
10951103 } else {
1096- const parentVar = ctx . generateVariable ( ) ;
10971104 const tempVar = ctx . generateTmpVariable ( ) ;
10981105 ctx . pushCode ( `var ${ tempVar } = ${ FUNCTION_PREFIX + type } (offset, {` ) ;
1099- ctx . pushCode ( `$parent: ${ parentVar } ,` ) ;
1100- ctx . pushCode ( `$root: ${ parentVar } .$root,` ) ;
1101- if ( ! this . options . readUntil && lengthInBytes === undefined ) {
1102- ctx . pushCode ( `$index: ${ length } - ${ counter } ,` ) ;
1106+ if ( ctx . useContextVariables ) {
1107+ const parentVar = ctx . generateVariable ( ) ;
1108+ ctx . pushCode ( `$parent: ${ parentVar } ,` ) ;
1109+ ctx . pushCode ( `$root: ${ parentVar } .$root,` ) ;
1110+ if ( ! this . options . readUntil && lengthInBytes === undefined ) {
1111+ ctx . pushCode ( `$index: ${ length } - ${ counter } ,` ) ;
1112+ }
11031113 }
11041114 ctx . pushCode ( `});` ) ;
11051115 ctx . pushCode (
@@ -1108,19 +1118,25 @@ export class Parser {
11081118 if ( type !== this . alias ) ctx . addReference ( type ) ;
11091119 }
11101120 } else if ( type instanceof Parser ) {
1111- const parentVar = ctx . generateVariable ( ) ;
11121121 ctx . pushCode ( `var ${ item } = {};` ) ;
1113-
1122+ const parentVar = ctx . generateVariable ( ) ;
11141123 ctx . pushScope ( item ) ;
1115- ctx . pushCode ( `${ item } .$parent = ${ parentVar } ;` ) ;
1116- ctx . pushCode ( `${ item } .$root = ${ parentVar } .$root;` ) ;
1117- if ( ! this . options . readUntil && lengthInBytes === undefined ) {
1118- ctx . pushCode ( `${ item } .$index = ${ length } - ${ counter } ;` ) ;
1124+
1125+ if ( ctx . useContextVariables ) {
1126+ ctx . pushCode ( `${ item } .$parent = ${ parentVar } ;` ) ;
1127+ ctx . pushCode ( `${ item } .$root = ${ parentVar } .$root;` ) ;
1128+ if ( ! this . options . readUntil && lengthInBytes === undefined ) {
1129+ ctx . pushCode ( `${ item } .$index = ${ length } - ${ counter } ;` ) ;
1130+ }
11191131 }
1132+
11201133 type . generate ( ctx ) ;
1121- ctx . pushCode ( `delete ${ item } .$parent;` ) ;
1122- ctx . pushCode ( `delete ${ item } .$root;` ) ;
1123- ctx . pushCode ( `delete ${ item } .$index;` ) ;
1134+
1135+ if ( ctx . useContextVariables ) {
1136+ ctx . pushCode ( `delete ${ item } .$parent;` ) ;
1137+ ctx . pushCode ( `delete ${ item } .$root;` ) ;
1138+ ctx . pushCode ( `delete ${ item } .$index;` ) ;
1139+ }
11241140 ctx . popScope ( ) ;
11251141 }
11261142
@@ -1158,8 +1174,10 @@ export class Parser {
11581174 } else {
11591175 const tempVar = ctx . generateTmpVariable ( ) ;
11601176 ctx . pushCode ( `var ${ tempVar } = ${ FUNCTION_PREFIX + type } (offset, {` ) ;
1161- ctx . pushCode ( `$parent: ${ varName } .$parent,` ) ;
1162- ctx . pushCode ( `$root: ${ varName } .$root,` ) ;
1177+ if ( ctx . useContextVariables ) {
1178+ ctx . pushCode ( `$parent: ${ varName } .$parent,` ) ;
1179+ ctx . pushCode ( `$root: ${ varName } .$root,` ) ;
1180+ }
11631181 ctx . pushCode ( `});` ) ;
11641182 ctx . pushCode (
11651183 `${ varName } = ${ tempVar } .result; offset = ${ tempVar } .offset;`
@@ -1180,9 +1198,11 @@ export class Parser {
11801198 if ( this . varName ) {
11811199 ctx . pushCode ( `${ nestVar } = {};` ) ;
11821200
1183- const parentVar = ctx . generateVariable ( ) ;
1184- ctx . pushCode ( `${ nestVar } .$parent = ${ parentVar } ;` ) ;
1185- ctx . pushCode ( `${ nestVar } .$root = ${ parentVar } .$root;` ) ;
1201+ if ( ctx . useContextVariables ) {
1202+ const parentVar = ctx . generateVariable ( ) ;
1203+ ctx . pushCode ( `${ nestVar } .$parent = ${ parentVar } ;` ) ;
1204+ ctx . pushCode ( `${ nestVar } .$root = ${ parentVar } .$root;` ) ;
1205+ }
11861206 }
11871207 ctx . pushCode ( `switch(${ tag } ) {` ) ;
11881208 Object . keys ( this . options . choices ) . forEach ( ( tag ) => {
@@ -1200,7 +1220,7 @@ export class Parser {
12001220 }
12011221 ctx . pushCode ( '}' ) ;
12021222
1203- if ( this . varName ) {
1223+ if ( this . varName && ctx . useContextVariables ) {
12041224 ctx . pushCode ( `delete ${ nestVar } .$parent;` ) ;
12051225 ctx . pushCode ( `delete ${ nestVar } .$root;` ) ;
12061226 }
@@ -1211,26 +1231,35 @@ export class Parser {
12111231
12121232 if ( this . options . type instanceof Parser ) {
12131233 if ( this . varName ) {
1214- const parentVar = ctx . generateVariable ( ) ;
12151234 ctx . pushCode ( `${ nestVar } = {};` ) ;
1216- ctx . pushCode ( `${ nestVar } .$parent = ${ parentVar } ;` ) ;
1217- ctx . pushCode ( `${ nestVar } .$root = ${ parentVar } .$root;` ) ;
1235+
1236+ if ( ctx . useContextVariables ) {
1237+ const parentVar = ctx . generateVariable ( ) ;
1238+ ctx . pushCode ( `${ nestVar } .$parent = ${ parentVar } ;` ) ;
1239+ ctx . pushCode ( `${ nestVar } .$root = ${ parentVar } .$root;` ) ;
1240+ }
12181241 }
1242+
12191243 ctx . pushPath ( this . varName ) ;
12201244 this . options . type . generate ( ctx ) ;
12211245 ctx . popPath ( this . varName ) ;
1222- if ( this . varName ) {
1223- ctx . pushCode ( `delete ${ nestVar } .$parent;` ) ;
1224- ctx . pushCode ( `delete ${ nestVar } .$root;` ) ;
1246+
1247+ if ( this . varName && ctx . useContextVariables ) {
1248+ if ( ctx . useContextVariables ) {
1249+ ctx . pushCode ( `delete ${ nestVar } .$parent;` ) ;
1250+ ctx . pushCode ( `delete ${ nestVar } .$root;` ) ;
1251+ }
12251252 }
12261253 } else if ( aliasRegistry [ this . options . type ] ) {
1227- const parentVar = ctx . generateVariable ( ) ;
12281254 const tempVar = ctx . generateTmpVariable ( ) ;
12291255 ctx . pushCode (
12301256 `var ${ tempVar } = ${ FUNCTION_PREFIX + this . options . type } (offset, {`
12311257 ) ;
1232- ctx . pushCode ( `$parent: ${ parentVar } ,` ) ;
1233- ctx . pushCode ( `$root: ${ parentVar } .$root,` ) ;
1258+ if ( ctx . useContextVariables ) {
1259+ const parentVar = ctx . generateVariable ( ) ;
1260+ ctx . pushCode ( `$parent: ${ parentVar } ,` ) ;
1261+ ctx . pushCode ( `$root: ${ parentVar } .$root,` ) ;
1262+ }
12341263 ctx . pushCode ( `});` ) ;
12351264 ctx . pushCode (
12361265 `${ nestVar } = ${ tempVar } .result; offset = ${ tempVar } .offset;`
@@ -1331,23 +1360,32 @@ export class Parser {
13311360 ctx . pushCode ( `offset = ${ offset } ;` ) ;
13321361
13331362 if ( this . options . type instanceof Parser ) {
1334- const parentVar = ctx . generateVariable ( ) ;
13351363 ctx . pushCode ( `${ nestVar } = {};` ) ;
1336- ctx . pushCode ( `${ nestVar } .$parent = ${ parentVar } ;` ) ;
1337- ctx . pushCode ( `${ nestVar } .$root = ${ parentVar } .$root;` ) ;
1364+
1365+ if ( ctx . useContextVariables ) {
1366+ const parentVar = ctx . generateVariable ( ) ;
1367+ ctx . pushCode ( `${ nestVar } .$parent = ${ parentVar } ;` ) ;
1368+ ctx . pushCode ( `${ nestVar } .$root = ${ parentVar } .$root;` ) ;
1369+ }
1370+
13381371 ctx . pushPath ( this . varName ) ;
13391372 this . options . type . generate ( ctx ) ;
13401373 ctx . popPath ( this . varName ) ;
1341- ctx . pushCode ( `delete ${ nestVar } .$parent;` ) ;
1342- ctx . pushCode ( `delete ${ nestVar } .$root;` ) ;
1374+
1375+ if ( ctx . useContextVariables ) {
1376+ ctx . pushCode ( `delete ${ nestVar } .$parent;` ) ;
1377+ ctx . pushCode ( `delete ${ nestVar } .$root;` ) ;
1378+ }
13431379 } else if ( aliasRegistry [ this . options . type ] ) {
1344- const parentVar = ctx . generateVariable ( ) ;
13451380 const tempVar = ctx . generateTmpVariable ( ) ;
13461381 ctx . pushCode (
13471382 `var ${ tempVar } = ${ FUNCTION_PREFIX + this . options . type } (offset, {`
13481383 ) ;
1349- ctx . pushCode ( `$parent: ${ parentVar } ,` ) ;
1350- ctx . pushCode ( `$root: ${ parentVar } .$root,` ) ;
1384+ if ( ctx . useContextVariables ) {
1385+ const parentVar = ctx . generateVariable ( ) ;
1386+ ctx . pushCode ( `$parent: ${ parentVar } ,` ) ;
1387+ ctx . pushCode ( `$root: ${ parentVar } .$root,` ) ;
1388+ }
13511389 ctx . pushCode ( `});` ) ;
13521390 ctx . pushCode (
13531391 `${ nestVar } = ${ tempVar } .result; offset = ${ tempVar } .offset;`
0 commit comments