@@ -177,6 +177,50 @@ pub fn get_or_insert_start_builder(module: &mut Module) -> &mut FunctionBuilder
177
177
. builder_mut ( )
178
178
}
179
179
180
+ pub fn target_feature ( module : & Module , feature : & str ) -> Result < bool > {
181
+ // Taken from <https://github.com/bytecodealliance/wasm-tools/blob/f1898f46bb9d96f0f09682415cb6ccfd6a4dca79/crates/wasmparser/src/limits.rs#L27>.
182
+ anyhow:: ensure!( feature. len( ) <= 100_000 , "feature name too long" ) ;
183
+
184
+ // Try to find an existing section.
185
+ let section = module
186
+ . customs
187
+ . iter ( )
188
+ . find ( |( _, custom) | custom. name ( ) == "target_features" ) ;
189
+
190
+ if let Some ( ( _, section) ) = section {
191
+ let section: & RawCustomSection = section
192
+ . as_any ( )
193
+ . downcast_ref ( )
194
+ . context ( "failed to read section" ) ?;
195
+ let mut reader = BinaryReader :: new ( & section. data , 0 , WasmFeatures :: default ( ) ) ;
196
+ // The first integer contains the target feature count.
197
+ let count = reader. read_var_u32 ( ) ?;
198
+
199
+ // Try to find if the target feature is already present.
200
+ for _ in 0 ..count {
201
+ // First byte is the prefix.
202
+ let prefix = reader. read_u8 ( ) ?;
203
+ // Read the feature.
204
+ let length = reader. read_var_u32 ( ) ?;
205
+ let this_feature = reader. read_bytes ( length as usize ) ?;
206
+
207
+ // If we found the target feature, we are done here.
208
+ if this_feature == feature. as_bytes ( ) {
209
+ // Make sure we set any existing prefix to "enabled".
210
+ if prefix == b'-' {
211
+ return Ok ( false ) ;
212
+ }
213
+
214
+ return Ok ( true ) ;
215
+ }
216
+ }
217
+
218
+ Ok ( false )
219
+ } else {
220
+ Ok ( false )
221
+ }
222
+ }
223
+
180
224
pub fn insert_target_feature ( module : & mut Module , new_feature : & str ) -> Result < ( ) > {
181
225
// Taken from <https://github.com/bytecodealliance/wasm-tools/blob/f1898f46bb9d96f0f09682415cb6ccfd6a4dca79/crates/wasmparser/src/limits.rs#L27>.
182
226
anyhow:: ensure!( new_feature. len( ) <= 100_000 , "feature name too long" ) ;
0 commit comments