@@ -10268,6 +10268,51 @@ def test_wasm_features_section(self, args):
10268
10268
self.run_process([EMCC, test_file('hello_world.c'), '-O2'] + args)
10269
10269
self.verify_custom_sec_existence('a.out.wasm', 'target_features', False)
10270
10270
10271
+ def test_wasm_features(self):
10272
+ # Test that wasm features are explicitly enabled or disabled based on target engine version
10273
+ def verify_features_sec(feature, expect_in, linked=False):
10274
+ with webassembly.Module('a.out.wasm' if linked else 'hello_world.o') as module:
10275
+ features = module.get_target_features()
10276
+ if expect_in:
10277
+ self.assertTrue(feature in features and
10278
+ features[feature] == webassembly.TargetFeaturePrefix.USED,
10279
+ f'{feature} missing from wasm file')
10280
+ else:
10281
+ self.assertFalse(feature in features,
10282
+ f'{feature} unexpectedly found in wasm file')
10283
+
10284
+ def verify_features_sec_linked(feature, expect_in):
10285
+ return verify_features_sec(feature, expect_in, linked=True)
10286
+
10287
+ def compile(flags):
10288
+ self.run_process([EMCC, test_file('hello_world.c')] + flags)
10289
+
10290
+ compile(['-c'])
10291
+ verify_features_sec('bulk-memory', False)
10292
+ verify_features_sec('nontrapping-fptoint', False)
10293
+ verify_features_sec('sign-ext', True)
10294
+ verify_features_sec('mutable-globals', True)
10295
+ verify_features_sec('multivalue', True)
10296
+ verify_features_sec('reference-types', True)
10297
+
10298
+ compile(['-mnontrapping-fptoint', '-c'])
10299
+ verify_features_sec('nontrapping-fptoint', True)
10300
+
10301
+ # BIGINT causes binaryen to not run, and keeps the target_features section after link
10302
+ # Setting this SAFARI_VERSION should enable bulk memory because it links in emscripten_memcpy_bulkmem
10303
+ # However it does not enable nontrapping-fptoint yet because it has no effect at compile time and
10304
+ # no libraries include nontrapping yet.
10305
+ compile(['-sMIN_SAFARI_VERSION=150000', '-sWASM_BIGINT'])
10306
+ verify_features_sec_linked('sign-ext', True)
10307
+ verify_features_sec_linked('mutable-globals', True)
10308
+ verify_features_sec_linked('multivalue', True)
10309
+ verify_features_sec_linked('bulk-memory', True)
10310
+ verify_features_sec_linked('nontrapping-fptoint', False)
10311
+
10312
+ compile(['-sMIN_SAFARI_VERSION=150000', '-mno-bulk-memory', '-sWASM_BIGINT'])
10313
+ # FIXME? -mno-bulk-memory at link time does not override MIN_SAFARI_VERSION. it probably should?
10314
+ verify_features_sec_linked('bulk-memory', True)
10315
+
10271
10316
def test_js_preprocess(self):
10272
10317
# Use stderr rather than stdout here because stdout is redirected to the output JS file itself.
10273
10318
create_file('lib.js', '''
0 commit comments