@@ -98,10 +98,7 @@ impl From<&Import> for ExternName {
9898#[ derive( Default , Clone ) ]
9999#[ cfg_attr( feature = "debug" , derive( Debug ) ) ]
100100pub struct Imports {
101- globals : BTreeMap < ExternName , Global > ,
102- tables : BTreeMap < ExternName , Table > ,
103- memories : BTreeMap < ExternName , Memory > ,
104- function_handles : BTreeMap < ExternName , Function > ,
101+ externs : BTreeMap < ExternName , Extern > ,
105102 modules : BTreeMap < String , crate :: ModuleInstance > ,
106103}
107104
@@ -112,30 +109,15 @@ pub(crate) struct ResolvedImports {
112109 pub ( crate ) funcs : Vec < FuncAddr > ,
113110}
114111
115- impl ResolvedImports {
116- pub ( crate ) const fn new ( ) -> Self {
117- Self { globals : Vec :: new ( ) , tables : Vec :: new ( ) , memories : Vec :: new ( ) , funcs : Vec :: new ( ) }
118- }
119- }
120-
121112impl Imports {
122113 /// Create a new empty import set
123114 pub const fn new ( ) -> Self {
124- Self {
125- globals : BTreeMap :: new ( ) ,
126- tables : BTreeMap :: new ( ) ,
127- memories : BTreeMap :: new ( ) ,
128- function_handles : BTreeMap :: new ( ) ,
129- modules : BTreeMap :: new ( ) ,
130- }
115+ Self { externs : BTreeMap :: new ( ) , modules : BTreeMap :: new ( ) }
131116 }
132117
133118 /// Merge two import sets
134119 pub fn merge ( mut self , other : Self ) -> Self {
135- self . globals . extend ( other. globals ) ;
136- self . tables . extend ( other. tables ) ;
137- self . memories . extend ( other. memories ) ;
138- self . function_handles . extend ( other. function_handles ) ;
120+ self . externs . extend ( other. externs ) ;
139121 self . modules . extend ( other. modules ) ;
140122 self
141123 }
@@ -151,38 +133,13 @@ impl Imports {
151133 /// Define an import value.
152134 pub fn define ( & mut self , module : & str , name : & str , value : impl Into < Extern > ) -> & mut Self {
153135 let name = ExternName { module : module. to_string ( ) , name : name. to_string ( ) } ;
154- match value. into ( ) {
155- Extern :: Global ( v) => {
156- self . globals . insert ( name, v) ;
157- }
158- Extern :: Table ( v) => {
159- self . tables . insert ( name, v) ;
160- }
161- Extern :: Memory ( v) => {
162- self . memories . insert ( name, v) ;
163- }
164- Extern :: Function ( v) => {
165- self . function_handles . insert ( name, v) ;
166- }
167- }
136+ self . externs . insert ( name, value. into ( ) ) ;
168137 self
169138 }
170139
171140 pub ( crate ) fn take_defined ( & self , import : & Import ) -> Option < Extern > {
172141 let name = ExternName :: from ( import) ;
173- if let Some ( v) = self . globals . get ( & name) {
174- return Some ( Extern :: Global ( * v) ) ;
175- }
176- if let Some ( v) = self . tables . get ( & name) {
177- return Some ( Extern :: Table ( * v) ) ;
178- }
179- if let Some ( v) = self . memories . get ( & name) {
180- return Some ( Extern :: Memory ( * v) ) ;
181- }
182- if let Some ( v) = self . function_handles . get ( & name) {
183- return Some ( Extern :: Function ( v. clone ( ) ) ) ;
184- }
185- None
142+ self . externs . get ( & name) . cloned ( )
186143 }
187144
188145 #[ cfg( not( feature = "debug" ) ) ]
@@ -242,13 +199,21 @@ impl Imports {
242199 Ok ( ( ) )
243200 }
244201
245- pub ( crate ) fn link (
246- self ,
247- store : & mut crate :: Store ,
248- module : & Module ,
249- _idx : ModuleInstanceAddr ,
250- ) -> Result < ResolvedImports > {
251- let mut imports = ResolvedImports :: new ( ) ;
202+ pub ( crate ) fn link ( & self , store : & mut crate :: Store , module : & Module ) -> Result < ResolvedImports > {
203+ let ( global_count, table_count, mem_count, func_count) =
204+ module. imports . iter ( ) . fold ( ( 0 , 0 , 0 , 0 ) , |( g, t, m, f) , import| match import. kind {
205+ ImportKind :: Global ( _) => ( g + 1 , t, m, f) ,
206+ ImportKind :: Table ( _) => ( g, t + 1 , m, f) ,
207+ ImportKind :: Memory ( _) => ( g, t, m + 1 , f) ,
208+ ImportKind :: Function ( _) => ( g, t, m, f + 1 ) ,
209+ } ) ;
210+
211+ let mut imports = ResolvedImports {
212+ globals : Vec :: with_capacity ( global_count) ,
213+ tables : Vec :: with_capacity ( table_count) ,
214+ memories : Vec :: with_capacity ( mem_count) ,
215+ funcs : Vec :: with_capacity ( func_count) ,
216+ } ;
252217
253218 for import in & * module. imports {
254219 if let Some ( defined) = self . take_defined ( import) {
@@ -299,9 +264,8 @@ impl Imports {
299264 let Some ( instance) = self . modules . get ( & name. module ) else {
300265 return Err ( LinkingError :: unknown_import ( import) . into ( ) ) ;
301266 } ;
302- if instance. 0 . store_id != store. id ( ) {
303- return Err ( crate :: Error :: InvalidStore ) ;
304- }
267+ instance. validate_store ( store) ?;
268+
305269 let val = instance. export_addr ( & import. name ) . ok_or_else ( || LinkingError :: unknown_import ( import) ) ?;
306270
307271 {
0 commit comments