@@ -686,22 +686,23 @@ export class Parser {
686686 }
687687
688688 private addAliasedCode ( ctx : Context ) {
689+ ctx . pushCode ( `function ${ FUNCTION_PREFIX + this . alias } (offset, context) {` ) ;
689690 ctx . pushCode (
690- `function ${ FUNCTION_PREFIX + this . alias } (offset, parent, root) { `
691+ `var vars = ${ this . constructorFn ? 'new constructorFn()' : '{}' } ; `
691692 ) ;
692693 ctx . pushCode (
693- `var vars = ${ this . constructorFn ? 'new constructorFn()' : '{}' } ; `
694+ `var ctx = Object.assign({$parent: null, $root: vars}, context || {}) `
694695 ) ;
695- ctx . pushCode ( 'vars.$parent = parent || null;' ) ;
696- ctx . pushCode ( 'vars.$root = root || vars;' ) ;
696+ ctx . pushCode ( `vars = Object.assign(vars, ctx)` ) ;
697697
698698 this . generate ( ctx ) ;
699699
700700 ctx . markResolved ( this . alias ) ;
701701 this . resolveReferences ( ctx ) ;
702702
703- ctx . pushCode ( 'delete vars.$parent;' ) ;
704- ctx . pushCode ( 'delete vars.$root;' ) ;
703+ ctx . pushCode (
704+ 'Object.keys(ctx).forEach(function (item) { delete vars[item]; });'
705+ ) ;
705706 ctx . pushCode ( 'return { offset: offset, result: vars };' ) ;
706707 ctx . pushCode ( '}' ) ;
707708
@@ -1094,11 +1095,13 @@ export class Parser {
10941095 } else {
10951096 const parentVar = ctx . generateVariable ( ) ;
10961097 const tempVar = ctx . generateTmpVariable ( ) ;
1097- ctx . pushCode (
1098- `var ${ tempVar } = ${
1099- FUNCTION_PREFIX + type
1100- } (offset, ${ parentVar } , ${ parentVar } .$root);`
1101- ) ;
1098+ 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 } ,` ) ;
1103+ }
1104+ ctx . pushCode ( `});` ) ;
11021105 ctx . pushCode (
11031106 `var ${ item } = ${ tempVar } .result; offset = ${ tempVar } .offset;`
11041107 ) ;
@@ -1111,9 +1114,13 @@ export class Parser {
11111114 ctx . pushScope ( item ) ;
11121115 ctx . pushCode ( `${ item } .$parent = ${ parentVar } ;` ) ;
11131116 ctx . pushCode ( `${ item } .$root = ${ parentVar } .$root;` ) ;
1117+ if ( ! this . options . readUntil && lengthInBytes === undefined ) {
1118+ ctx . pushCode ( `${ item } .$index = ${ length } - ${ counter } ,` ) ;
1119+ }
11141120 type . generate ( ctx ) ;
11151121 ctx . pushCode ( `delete ${ item } .$parent` ) ;
11161122 ctx . pushCode ( `delete ${ item } .$root` ) ;
1123+ ctx . pushCode ( `delete ${ item } .$index` ) ;
11171124 ctx . popScope ( ) ;
11181125 }
11191126
@@ -1150,11 +1157,10 @@ export class Parser {
11501157 ctx . pushCode ( `offset += ${ PRIMITIVE_SIZES [ type as PrimitiveTypes ] } ` ) ;
11511158 } else {
11521159 const tempVar = ctx . generateTmpVariable ( ) ;
1153- ctx . pushCode (
1154- `var ${ tempVar } = ${
1155- FUNCTION_PREFIX + type
1156- } (offset, ${ varName } .$parent, ${ varName } .$root);`
1157- ) ;
1160+ ctx . pushCode ( `var ${ tempVar } = ${ FUNCTION_PREFIX + type } (offset, {` ) ;
1161+ ctx . pushCode ( `$parent: ${ varName } .$parent,` ) ;
1162+ ctx . pushCode ( `$root: ${ varName } .$root,` ) ;
1163+ ctx . pushCode ( `});` ) ;
11581164 ctx . pushCode (
11591165 `${ varName } = ${ tempVar } .result; offset = ${ tempVar } .offset;`
11601166 ) ;
@@ -1221,10 +1227,11 @@ export class Parser {
12211227 const parentVar = ctx . generateVariable ( ) ;
12221228 const tempVar = ctx . generateTmpVariable ( ) ;
12231229 ctx . pushCode (
1224- `var ${ tempVar } = ${
1225- FUNCTION_PREFIX + this . options . type
1226- } (offset, ${ parentVar } , ${ parentVar } .$root);`
1230+ `var ${ tempVar } = ${ FUNCTION_PREFIX + this . options . type } (offset, {`
12271231 ) ;
1232+ ctx . pushCode ( `$parent: ${ parentVar } ,` ) ;
1233+ ctx . pushCode ( `$root: ${ parentVar } .$root,` ) ;
1234+ ctx . pushCode ( `});` ) ;
12281235 ctx . pushCode (
12291236 `${ nestVar } = ${ tempVar } .result; offset = ${ tempVar } .offset;`
12301237 ) ;
@@ -1337,10 +1344,11 @@ export class Parser {
13371344 const parentVar = ctx . generateVariable ( ) ;
13381345 const tempVar = ctx . generateTmpVariable ( ) ;
13391346 ctx . pushCode (
1340- `var ${ tempVar } = ${
1341- FUNCTION_PREFIX + this . options . type
1342- } (offset, ${ parentVar } , ${ parentVar } .$root);`
1347+ `var ${ tempVar } = ${ FUNCTION_PREFIX + this . options . type } (offset, {`
13431348 ) ;
1349+ ctx . pushCode ( `$parent: ${ parentVar } ,` ) ;
1350+ ctx . pushCode ( `$root: ${ parentVar } .$root,` ) ;
1351+ ctx . pushCode ( `});` ) ;
13441352 ctx . pushCode (
13451353 `${ nestVar } = ${ tempVar } .result; offset = ${ tempVar } .offset;`
13461354 ) ;
0 commit comments