8
8
9
9
use std:: io:: Cursor ;
10
10
11
- use anyhow:: { anyhow, bail, Result } ;
11
+ use anyhow:: { anyhow, bail, Context , Result } ;
12
12
use walrus:: {
13
13
ir:: Value , ElementId , FunctionBuilder , FunctionId , FunctionKind , GlobalId , GlobalKind ,
14
14
InitExpr , MemoryId , Module , RawCustomSection , ValType ,
@@ -152,9 +152,9 @@ pub fn get_or_insert_start_builder(module: &mut Module) -> &mut FunctionBuilder
152
152
. builder_mut ( )
153
153
}
154
154
155
- pub fn insert_target_feature ( module : & mut Module , new_feature : & str ) {
155
+ pub fn insert_target_feature ( module : & mut Module , new_feature : & str ) -> Result < ( ) > {
156
156
// Taken from <https://github.com/bytecodealliance/wasm-tools/blob/f1898f46bb9d96f0f09682415cb6ccfd6a4dca79/crates/wasmparser/src/limits.rs#L27>.
157
- assert ! ( new_feature. len( ) <= 100_000 ) ;
157
+ anyhow :: ensure !( new_feature. len( ) <= 100_000 , "feature name too long" ) ;
158
158
159
159
// Try to find an existing section.
160
160
let section = module
@@ -164,19 +164,22 @@ pub fn insert_target_feature(module: &mut Module, new_feature: &str) {
164
164
165
165
// If one exists, check if the target feature is already present.
166
166
let section = if let Some ( ( _, section) ) = section {
167
- let section: & mut RawCustomSection = section. as_any_mut ( ) . downcast_mut ( ) . unwrap ( ) ;
167
+ let section: & mut RawCustomSection = section
168
+ . as_any_mut ( )
169
+ . downcast_mut ( )
170
+ . context ( "failed to read section" ) ?;
168
171
let mut reader = BinaryReader :: new ( & section. data ) ;
169
172
// The first integer contains the target feature count.
170
- let count = reader. read_var_u32 ( ) . unwrap ( ) ;
173
+ let count = reader. read_var_u32 ( ) ? ;
171
174
172
175
// Try to find if the target feature is already present.
173
176
for _ in 0 ..count {
174
177
// First byte is the prefix.
175
178
let prefix_index = reader. current_position ( ) ;
176
- let prefix = reader. read_u8 ( ) . unwrap ( ) as u8 ;
179
+ let prefix = reader. read_u8 ( ) ? as u8 ;
177
180
// Read the feature.
178
- let length = reader. read_var_u32 ( ) . unwrap ( ) ;
179
- let feature = reader. read_bytes ( length as usize ) . unwrap ( ) ;
181
+ let length = reader. read_var_u32 ( ) ? ;
182
+ let feature = reader. read_bytes ( length as usize ) ? ;
180
183
181
184
// If we found the target feature, we are done here.
182
185
if feature == new_feature. as_bytes ( ) {
@@ -185,7 +188,7 @@ pub fn insert_target_feature(module: &mut Module, new_feature: &str) {
185
188
section. data [ prefix_index] = b'+' ;
186
189
}
187
190
188
- return ;
191
+ return Ok ( ( ) ) ;
189
192
}
190
193
}
191
194
@@ -214,4 +217,6 @@ pub fn insert_target_feature(module: &mut Module, new_feature: &str) {
214
217
leb128:: write:: unsigned ( & mut section. data , new_feature. len ( ) as u64 ) . unwrap ( ) ;
215
218
// Lastly the target feature string is inserted.
216
219
section. data . extend ( new_feature. as_bytes ( ) ) ;
220
+
221
+ Ok ( ( ) )
217
222
}
0 commit comments