@@ -37,24 +37,37 @@ export interface ExceptionEntry {
3737export class Code {
3838 readonly maxStack : number ;
3939 readonly maxLocals : number ;
40- readonly code : Uint8Array ;
4140 readonly handlers : ExceptionEntry [ ] ;
42- // raw LocalVariableTable attribute body, parsed on demand
43- readonly localVars : Uint8Array | null ;
4441 // the class's bytes, and where its LineNumberTable bodies start (-1 for none)
4542 private bytes : Uint8Array ;
43+ private codeAt : number ;
44+ private codeEnd : number ;
45+ private view : Uint8Array | null = null ;
46+ // where the LocalVariableTable body starts and ends (-1 for none)
47+ private varsAt : number ;
48+ private varsEnd : number ;
4649 private linesAt : number ;
4750 private moreLines : number [ ] | null ;
4851 private parsedLines : number [ ] | null | undefined ;
49- constructor ( maxStack : number , maxLocals : number , code : Uint8Array , handlers : ExceptionEntry [ ] , bytes : Uint8Array , linesAt : number , moreLines : number [ ] | null , localVars : Uint8Array | null ) {
52+ constructor ( maxStack : number , maxLocals : number , codeAt : number , codeEnd : number , handlers : ExceptionEntry [ ] , bytes : Uint8Array , linesAt : number , moreLines : number [ ] | null , varsAt : number , varsEnd : number ) {
5053 this . maxStack = maxStack ;
5154 this . maxLocals = maxLocals ;
52- this . code = code ;
55+ this . codeAt = codeAt ;
56+ this . codeEnd = codeEnd ;
5357 this . handlers = handlers ;
5458 this . bytes = bytes ;
5559 this . linesAt = linesAt ;
5660 this . moreLines = moreLines ;
57- this . localVars = localVars ;
61+ this . varsAt = varsAt ;
62+ this . varsEnd = varsEnd ;
63+ }
64+ // the bytecode, as a view made on first use: most methods never run
65+ get code ( ) : Uint8Array {
66+ return this . view ??= this . bytes . subarray ( this . codeAt , this . codeEnd ) ;
67+ }
68+ // the raw LocalVariableTable attribute body
69+ get localVars ( ) : Uint8Array | null {
70+ return this . varsAt < 0 ? null : this . bytes . subarray ( this . varsAt , this . varsEnd ) ;
5871 }
5972 // pairs of (start pc, line), sorted by pc
6073 get lines ( ) : number [ ] | null {
@@ -86,7 +99,8 @@ export interface Member {
8699 access : number ;
87100 name : string ;
88101 desc : string ;
89- attrs : Map < string , Uint8Array > ;
102+ // the attributes besides Code: a map, or where they start in the class file for memberAttrs
103+ attrs : Map < string , Uint8Array > | number ;
90104 code : Code | null ;
91105}
92106
@@ -191,7 +205,8 @@ function readMember(r: Reader, cp: ClassFile["cp"]): Member {
191205 const access = r . u2 ( ) ;
192206 const name = utf8 ( cp , r . u2 ( ) ) ;
193207 const desc = utf8 ( cp , r . u2 ( ) ) ;
194- let attrs = NO_ATTRS ;
208+ const at = r . pos ;
209+ let attrs : Map < string , Uint8Array > | number = NO_ATTRS ;
195210 let code : Code | null = null ;
196211 for ( let n = r . u2 ( ) ; n > 0 ; n -- ) {
197212 const attr = utf8 ( cp , r . u2 ( ) ) ;
@@ -201,13 +216,26 @@ function readMember(r: Reader, cp: ClassFile["cp"]): Member {
201216 code = readCode ( r , cp ) ;
202217 r . pos = end ;
203218 } else {
204- if ( attrs === NO_ATTRS ) attrs = new Map ( ) ;
205- attrs . set ( attr , r . slice ( length ) ) ;
219+ attrs = at ;
220+ r . pos += length ;
206221 }
207222 }
208223 return { access, name, desc, attrs, code } ;
209224}
210225
226+ // The attributes of the member whose attribute count is at `at`, but Code.
227+ export function memberAttrs ( cp : ClassFile [ "cp" ] , at : number ) : Map < string , Uint8Array > {
228+ const r = new Reader ( cp . bytes ) ;
229+ r . pos = at ;
230+ const attrs = new Map < string , Uint8Array > ( ) ;
231+ for ( let n = r . u2 ( ) ; n > 0 ; n -- ) {
232+ const name = utf8 ( cp , r . u2 ( ) ) ;
233+ const body = r . slice ( r . u4 ( ) ) ;
234+ if ( name !== "Code" ) attrs . set ( name , body ) ;
235+ }
236+ return attrs ;
237+ }
238+
211239function readAttrs ( r : Reader , cp : ClassFile [ "cp" ] ) {
212240 const attrs = new Map < string , Uint8Array > ( ) ;
213241 for ( let n = r . u2 ( ) ; n > 0 ; n -- ) {
@@ -220,15 +248,17 @@ function readAttrs(r: Reader, cp: ClassFile["cp"]) {
220248function readCode ( r : Reader , cp : ClassFile [ "cp" ] ) : Code {
221249 const maxStack = r . u2 ( ) ;
222250 const maxLocals = r . u2 ( ) ;
223- const code = r . slice ( r . u4 ( ) ) ;
251+ const codeAt = r . pos + 4 ;
252+ const codeEnd = codeAt + r . u4 ( ) ;
253+ r . pos = codeEnd ;
224254 let n = r . u2 ( ) ;
225255 const handlers : ExceptionEntry [ ] = n === 0 ? NO_HANDLERS : [ ] ;
226256 for ( ; n > 0 ; n -- ) {
227257 handlers . push ( { start : r . u2 ( ) , end : r . u2 ( ) , handler : r . u2 ( ) , catchType : r . u2 ( ) } ) ;
228258 }
229259 let linesAt = - 1 ;
230260 let moreLines : number [ ] | null = null ;
231- let localVars : Uint8Array | null = null ;
261+ let varsAt = - 1 , varsEnd = - 1 ;
232262 for ( n = r . u2 ( ) ; n > 0 ; n -- ) {
233263 const name = utf8 ( cp , r . u2 ( ) ) ;
234264 const length = r . u4 ( ) ;
@@ -237,11 +267,14 @@ function readCode(r: Reader, cp: ClassFile["cp"]): Code {
237267 else ( moreLines ??= [ ] ) . push ( r . pos ) ;
238268 r . pos += length ;
239269 } else {
240- const body = r . slice ( length ) ;
241- if ( name === "LocalVariableTable" ) localVars = body ;
270+ if ( name === "LocalVariableTable" ) {
271+ varsAt = r . pos ;
272+ varsEnd = r . pos + length ;
273+ }
274+ r . pos += length ;
242275 }
243276 }
244- return new Code ( maxStack , maxLocals , code , handlers , r . bytes , linesAt , moreLines , localVars ) ;
277+ return new Code ( maxStack , maxLocals , codeAt , codeEnd , handlers , r . bytes , linesAt , moreLines , varsAt , varsEnd ) ;
245278}
246279
247280export function cpEntry ( cp : ClassFile [ "cp" ] , i : number ) : CpEntry | undefined {
0 commit comments