@@ -212,16 +212,15 @@ def sections(self):
212
212
213
213
def parse_features_section (self ):
214
214
features = []
215
- for sec in self .sections ():
216
- if sec .type == SecType .CUSTOM and sec .name == 'target_features' :
217
- self .seek (sec .offset )
218
- self .readString () # name
219
- feature_count = self .readULEB ()
220
- while feature_count :
221
- prefix = self .readByte ()
222
- features .append ((chr (prefix ), self .readString ()))
223
- feature_count -= 1
224
- break
215
+ sec = self .get_custom_section ('target_features' )
216
+ if sec :
217
+ self .seek (sec .offset )
218
+ self .readString () # name
219
+ feature_count = self .readULEB ()
220
+ while feature_count :
221
+ prefix = self .readByte ()
222
+ features .append ((chr (prefix ), self .readString ()))
223
+ feature_count -= 1
225
224
return features
226
225
227
226
def parse_dylink_section (self ):
@@ -289,7 +288,7 @@ def parse_dylink_section(self):
289
288
return Dylink (mem_size , mem_align , table_size , table_align , needed , export_info , import_info )
290
289
291
290
def get_exports (self ):
292
- export_section = next (( s for s in self .sections () if s . type == SecType .EXPORT ), None )
291
+ export_section = self .get_section ( SecType .EXPORT )
293
292
if not export_section :
294
293
return []
295
294
@@ -305,7 +304,7 @@ def get_exports(self):
305
304
return exports
306
305
307
306
def get_imports (self ):
308
- import_section = next (( s for s in self .sections () if s . type == SecType .IMPORT ), None )
307
+ import_section = self .get_section ( SecType .IMPORT )
309
308
if not import_section :
310
309
return []
311
310
@@ -336,7 +335,7 @@ def get_imports(self):
336
335
return imports
337
336
338
337
def get_globals (self ):
339
- global_section = next (( s for s in self .sections () if s . type == SecType .GLOBAL ), None )
338
+ global_section = self .get_section ( SecType .GLOBAL )
340
339
if not global_section :
341
340
return []
342
341
globls = []
@@ -350,7 +349,7 @@ def get_globals(self):
350
349
return globls
351
350
352
351
def get_functions (self ):
353
- code_section = next (( s for s in self .sections () if s . type == SecType .CODE ), None )
352
+ code_section = self .get_section ( SecType .CODE )
354
353
if not code_section :
355
354
return []
356
355
functions = []
@@ -363,9 +362,18 @@ def get_functions(self):
363
362
self .seek (start + body_size )
364
363
return functions
365
364
365
+ def get_section (self , section_code ):
366
+ return next ((s for s in self .sections () if s .type == section_code ), None )
367
+
368
+ def get_custom_section (self , name ):
369
+ for section in self .sections ():
370
+ if section .type == SecType .CUSTOM and section .name == name :
371
+ return section
372
+ return None
373
+
366
374
def get_segments (self ):
367
375
segments = []
368
- data_section = next (( s for s in self .sections () if s . type == SecType .DATA ), None )
376
+ data_section = self .get_section ( SecType .DATA )
369
377
self .seek (data_section .offset )
370
378
num_segments = self .readULEB ()
371
379
for i in range (num_segments ):
@@ -381,7 +389,7 @@ def get_segments(self):
381
389
return segments
382
390
383
391
def get_tables (self ):
384
- table_section = next (( s for s in self .sections () if s . type == SecType .TABLE ), None )
392
+ table_section = self .get_section ( SecType .TABLE )
385
393
if not table_section :
386
394
return []
387
395
@@ -396,10 +404,7 @@ def get_tables(self):
396
404
return tables
397
405
398
406
def has_name_section (self ):
399
- for section in self .sections ():
400
- if section .type == SecType .CUSTOM and section .name == 'name' :
401
- return True
402
- return False
407
+ return self .get_custom_section ('name' ) is not None
403
408
404
409
405
410
def parse_dylink_section (wasm_file ):
0 commit comments