Skip to content

Commit 6f01050

Browse files
committed
add test
1 parent 31240ea commit 6f01050

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

test/test_other.py

+24
Original file line numberDiff line numberDiff line change
@@ -10265,6 +10265,30 @@ def test_wasm_features_section(self, args):
1026510265
self.run_process([EMCC, test_file('hello_world.c'), '-O2'] + args)
1026610266
self.verify_custom_sec_existence('a.out.wasm', 'target_features', False)
1026710267

10268+
def test_wasm_features(self):
10269+
# Test that wasm features are explicitly enabled or disabled based on target engine version
10270+
def verify_features_sec(feature, expect_in):
10271+
with webassembly.Module('hello_world.o') as module:
10272+
features = module.get_target_features()
10273+
if expect_in:
10274+
self.assertTrue(feature in features and features[feature] == webassembly.TargetFeaturePrefix.USED)
10275+
else:
10276+
self.assertFalse(feature in features)
10277+
10278+
def compile(flag):
10279+
self.run_process([EMCC, test_file('hello_world.c'), '-c', flag])
10280+
10281+
compile('')
10282+
verify_features_sec('bulk-memory', False)
10283+
verify_features_sec('nontrapping-fptoint', False)
10284+
verify_features_sec('sign-ext', True)
10285+
verify_features_sec('mutable-globals', True)
10286+
verify_features_sec('multivalue', True)
10287+
10288+
compile('-sMIN_SAFARI_VERSION=150000')
10289+
verify_features_sec('bulk-memory', True)
10290+
verify_features_sec('nontrapping-fptoint', True)
10291+
1026810292
def test_js_preprocess(self):
1026910293
# Use stderr rather than stdout here because stdout is redirected to the output JS file itself.
1027010294
create_file('lib.js', '''

tools/webassembly.py

+18
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ class DylinkType(IntEnum):
162162
IMPORT_INFO = 4
163163

164164

165+
class TargetFeaturePrefix(IntEnum):
166+
USED = 0x2b
167+
DISALLOWED = 0x2d
168+
REQUIRED = 0x3d
169+
170+
165171
class InvalidWasmError(BaseException):
166172
pass
167173

@@ -561,6 +567,18 @@ def get_function_type(self, idx):
561567
func_type = self.get_function_types()[idx - self.num_imported_funcs()]
562568
return self.get_types()[func_type]
563569

570+
def get_target_features(self):
571+
section = self.get_custom_section('target_features')
572+
self.seek(section.offset)
573+
assert self.read_string() == 'target_features'
574+
features = {}
575+
feature_count = self.read_byte()
576+
while self.tell() < section.offset + section.size:
577+
prefix = TargetFeaturePrefix(self.read_byte())
578+
feature = self.read_string()
579+
features[feature] = prefix
580+
return features
581+
564582

565583
def parse_dylink_section(wasm_file):
566584
with Module(wasm_file) as module:

0 commit comments

Comments
 (0)