@@ -113,18 +113,22 @@ fn build_ir<'data>(
113
113
) ) ) ;
114
114
}
115
115
module_instances_source. push ( * static_module_idx) ;
116
+ let module = & parsed_modules[ * static_module_idx] . module ;
117
+ // dbg!(&module);
116
118
let mut module_args: Vec < ModuleArgument > = Vec :: new ( ) ;
117
- for arg in args. iter ( ) {
119
+ for ( idx , arg) in args. iter ( ) . enumerate ( ) {
118
120
match arg {
119
121
CoreDef :: Export ( export) => {
120
122
match export. item {
121
123
ExportItem :: Index ( entity_idx) => match entity_idx {
122
124
EntityIndex :: Function ( func_idx) => {
123
125
let module_id =
124
126
module_instances_source[ export. instance ] ;
125
- let module = & parsed_modules[ module_id] . module ;
126
- let func_name = module. func_name ( func_idx) ;
127
- let module_name = module. name ( ) ;
127
+ let exporting_module =
128
+ & parsed_modules[ module_id] . module ;
129
+ let func_name =
130
+ exporting_module. func_name ( func_idx) ;
131
+ let module_name = exporting_module. name ( ) ;
128
132
let function_id = FunctionIdent {
129
133
module : Ident :: with_empty_span ( Symbol :: intern (
130
134
module_name,
@@ -166,14 +170,28 @@ fn build_ir<'data>(
166
170
runtime_import_index : import,
167
171
signature : * lower_ty,
168
172
} ;
169
- let ( func_id, component_import) = build_import (
173
+ let module_import = module
174
+ . imports
175
+ . get ( idx)
176
+ . expect ( "module import not found" ) ;
177
+ let func_name =
178
+ module. func_name ( module_import. index . unwrap_func ( ) ) ;
179
+ let module_name = module. name ( ) ;
180
+ let function_id = FunctionIdent {
181
+ module : Ident :: with_empty_span ( Symbol :: intern (
182
+ module_name,
183
+ ) ) ,
184
+ function : Ident :: with_empty_span ( Symbol :: intern (
185
+ func_name,
186
+ ) ) ,
187
+ } ;
188
+ let component_import = build_import (
170
189
& import,
171
- & parsed_modules,
172
190
& component_types,
173
191
component,
174
192
config,
175
193
) ?;
176
- cb. add_import ( func_id , component_import. clone ( ) ) ;
194
+ cb. add_import ( function_id , component_import. clone ( ) ) ;
177
195
module_args. push ( ModuleArgument :: ComponentImport (
178
196
component_import,
179
197
) ) ;
@@ -184,19 +202,15 @@ fn build_ir<'data>(
184
202
}
185
203
}
186
204
let module_types = component_types. module_types ( ) ;
187
- let func_env = FuncEnvironment :: new (
188
- & parsed_modules[ * static_module_idx] . module ,
189
- module_types,
190
- module_args,
191
- ) ;
192
- let module = build_ir_module (
205
+ let func_env = FuncEnvironment :: new ( module, module_types, module_args) ;
206
+ let ir_module = build_ir_module (
193
207
parsed_modules. get_mut ( * static_module_idx) . unwrap ( ) ,
194
208
module_types,
195
209
func_env,
196
210
config,
197
211
diagnostics,
198
212
) ?;
199
- cb. add_module ( module . into ( ) )
213
+ cb. add_module ( ir_module . into ( ) )
200
214
. expect ( "module is already added" ) ;
201
215
}
202
216
InstantiateModule :: Import ( _, _) => todo ! ( ) ,
@@ -241,11 +255,10 @@ pub fn ensure_module_names(modules: &mut PrimaryMap<StaticModuleIndex, ParsedMod
241
255
242
256
fn build_import (
243
257
import : & ComponentImport ,
244
- parsed_modules : & PrimaryMap < StaticModuleIndex , ParsedModule > ,
245
258
component_types : & ComponentTypes ,
246
259
component : & LinearComponent ,
247
260
config : & WasmTranslationConfig ,
248
- ) -> WasmResult < ( FunctionIdent , miden_hir:: ComponentImport ) > {
261
+ ) -> WasmResult < miden_hir:: ComponentImport > {
249
262
let ( import_idx, import_names) = & component. imports [ import. runtime_import_index ] ;
250
263
if import_names. len ( ) != 1 {
251
264
return Err ( crate :: WasmError :: Unsupported (
@@ -272,33 +285,7 @@ fn build_import(
272
285
invoke_method : import_metadata. invoke_method ,
273
286
digest : import_metadata. digest . clone ( ) ,
274
287
} ;
275
- let function_id =
276
- find_module_import_function ( parsed_modules, full_interface_name, import_func_name) ?;
277
- Ok ( ( function_id, component_import) )
278
- }
279
-
280
- fn find_module_import_function (
281
- parsed_modules : & PrimaryMap < StaticModuleIndex , ParsedModule > ,
282
- full_interface_name : String ,
283
- import_func_name : & String ,
284
- ) -> WasmResult < FunctionIdent > {
285
- for ( _idx, parsed_module) in parsed_modules {
286
- for import in & parsed_module. module . imports {
287
- if import. module == full_interface_name && & import. field == import_func_name {
288
- let func_idx = import. index . unwrap_func ( ) ;
289
- let func_name = parsed_module. module . func_name ( func_idx) ;
290
- let module_instance_name = parsed_module. module . name ( ) ;
291
- return Ok ( FunctionIdent {
292
- module : Ident :: with_empty_span ( Symbol :: intern ( module_instance_name) ) ,
293
- function : Ident :: with_empty_span ( Symbol :: intern ( func_name) ) ,
294
- } ) ;
295
- }
296
- }
297
- }
298
- Err ( WasmError :: Unexpected ( format ! (
299
- "failed to find module import for interface {} and function {}" ,
300
- full_interface_name, import_func_name
301
- ) ) )
288
+ Ok ( component_import)
302
289
}
303
290
304
291
fn build_export < ' data > (
0 commit comments