Skip to content

Commit 0f00d97

Browse files
authored
Simplify tools/webassembly.py. NFC (#16530)
1 parent 758b9b8 commit 0f00d97

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

emsymbolizer.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ class Error(BaseException):
2929

3030

3131
def get_codesec_offset(module):
32-
for sec in module.sections():
33-
if sec.type == webassembly.SecType.CODE:
34-
return sec.offset
35-
raise Error(f'No code section found in {module.filename}')
32+
sec = module.get_section(webassembly.SecType.CODE)
33+
if not sec:
34+
raise Error(f'No code section found in {module.filename}')
35+
return sec.offset
3636

3737

3838
def has_debug_line_section(module):

tools/webassembly.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -212,16 +212,15 @@ def sections(self):
212212

213213
def parse_features_section(self):
214214
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
225224
return features
226225

227226
def parse_dylink_section(self):
@@ -289,7 +288,7 @@ def parse_dylink_section(self):
289288
return Dylink(mem_size, mem_align, table_size, table_align, needed, export_info, import_info)
290289

291290
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)
293292
if not export_section:
294293
return []
295294

@@ -305,7 +304,7 @@ def get_exports(self):
305304
return exports
306305

307306
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)
309308
if not import_section:
310309
return []
311310

@@ -336,7 +335,7 @@ def get_imports(self):
336335
return imports
337336

338337
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)
340339
if not global_section:
341340
return []
342341
globls = []
@@ -350,7 +349,7 @@ def get_globals(self):
350349
return globls
351350

352351
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)
354353
if not code_section:
355354
return []
356355
functions = []
@@ -363,9 +362,18 @@ def get_functions(self):
363362
self.seek(start + body_size)
364363
return functions
365364

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+
366374
def get_segments(self):
367375
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)
369377
self.seek(data_section.offset)
370378
num_segments = self.readULEB()
371379
for i in range(num_segments):
@@ -381,7 +389,7 @@ def get_segments(self):
381389
return segments
382390

383391
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)
385393
if not table_section:
386394
return []
387395

@@ -396,10 +404,7 @@ def get_tables(self):
396404
return tables
397405

398406
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
403408

404409

405410
def parse_dylink_section(wasm_file):

0 commit comments

Comments
 (0)