@@ -7,16 +7,14 @@ use crate::component::types;
77use crate :: component:: {
88 Component , ComponentNamedList , Instance , InstancePre , Lift , Lower , ResourceType , Val ,
99} ;
10- use crate :: hash_map:: HashMap ;
1110use crate :: prelude:: * ;
1211use crate :: { AsContextMut , Engine , Module , StoreContextMut } ;
1312use alloc:: sync:: Arc ;
1413use core:: marker;
1514#[ cfg( feature = "component-model-async" ) ]
1615use core:: pin:: Pin ;
17- use wasmtime_environ:: PrimaryMap ;
18- use wasmtime_environ:: collections:: TryCow ;
19- use wasmtime_environ:: component:: { NameMap , NameMapIntern } ;
16+ use wasmtime_environ:: component:: NameMap ;
17+ use wasmtime_environ:: { Atom , PrimaryMap , StringPool } ;
2018
2119/// A type used to instantiate [`Component`]s.
2220///
@@ -61,9 +59,9 @@ use wasmtime_environ::component::{NameMap, NameMapIntern};
6159/// be instantiated.
6260pub struct Linker < T : ' static > {
6361 engine : Engine ,
64- strings : Strings ,
65- map : NameMap < usize , Definition > ,
66- path : Vec < usize > ,
62+ strings : StringPool ,
63+ map : NameMap < Atom , Definition > ,
64+ path : Vec < Atom > ,
6765 allow_shadowing : bool ,
6866 _marker : marker:: PhantomData < fn ( ) -> T > ,
6967}
@@ -72,7 +70,7 @@ impl<T: 'static> Clone for Linker<T> {
7270 fn clone ( & self ) -> Linker < T > {
7371 Linker {
7472 engine : self . engine . clone ( ) ,
75- strings : self . strings . clone ( ) ,
73+ strings : self . strings . clone_panic_on_oom ( ) ,
7674 map : self . map . clone_panic_on_oom ( ) ,
7775 path : self . path . clone ( ) ,
7876 allow_shadowing : self . allow_shadowing ,
@@ -81,30 +79,24 @@ impl<T: 'static> Clone for Linker<T> {
8179 }
8280}
8381
84- #[ derive( Clone , Default ) ]
85- pub struct Strings {
86- string2idx : HashMap < Arc < str > , usize > ,
87- strings : Vec < Arc < str > > ,
88- }
89-
9082/// Structure representing an "instance" being defined within a linker.
9183///
9284/// Instances do not need to be actual [`Instance`]s and instead are defined by
9385/// a "bag of named items", so each [`LinkerInstance`] can further define items
9486/// internally.
9587pub struct LinkerInstance < ' a , T : ' static > {
9688 engine : & ' a Engine ,
97- path : & ' a mut Vec < usize > ,
89+ path : & ' a mut Vec < Atom > ,
9890 path_len : usize ,
99- strings : & ' a mut Strings ,
100- map : & ' a mut NameMap < usize , Definition > ,
91+ strings : & ' a mut StringPool ,
92+ map : & ' a mut NameMap < Atom , Definition > ,
10193 allow_shadowing : bool ,
10294 _marker : marker:: PhantomData < fn ( ) -> T > ,
10395}
10496
10597#[ derive( Debug ) ]
10698pub ( crate ) enum Definition {
107- Instance ( NameMap < usize , Definition > ) ,
99+ Instance ( NameMap < Atom , Definition > ) ,
108100 Func ( Arc < HostFunc > ) ,
109101 Module ( Module ) ,
110102 Resource ( ResourceType , Arc < crate :: func:: HostFunc > ) ,
@@ -127,7 +119,7 @@ impl<T: 'static> Linker<T> {
127119 pub fn new ( engine : & Engine ) -> Linker < T > {
128120 Linker {
129121 engine : engine. clone ( ) ,
130- strings : Strings :: default ( ) ,
122+ strings : StringPool :: default ( ) ,
131123 map : NameMap :: default ( ) ,
132124 allow_shadowing : false ,
133125 path : Vec :: new ( ) ,
@@ -345,9 +337,20 @@ impl<T: 'static> Linker<T> {
345337
346338 match item_def {
347339 TypeDef :: ComponentFunc ( _) => {
348- let fully_qualified_name = parent_instance
349- . map ( |parent| format ! ( "{parent}#{item_name}" ) )
350- . unwrap_or_else ( || item_name. to_owned ( ) ) ;
340+ let fully_qualified_name = match parent_instance {
341+ Some ( parent) => {
342+ let mut s = TryString :: new ( ) ;
343+ s. push_str ( parent) ?;
344+ s. push ( '#' ) ?;
345+ s. push_str ( item_name) ?;
346+ s
347+ }
348+ None => {
349+ let mut s = TryString :: new ( ) ;
350+ s. push_str ( item_name) ?;
351+ s
352+ }
353+ } ;
351354 linker. func_new ( & item_name, move |_, _, _, _| {
352355 bail ! ( "unknown import: `{fully_qualified_name}` has not been defined" )
353356 } ) ?;
@@ -438,7 +441,7 @@ impl<T: 'static> LinkerInstance<'_, T> {
438441 Params : ComponentNamedList + Lift + ' static ,
439442 Return : ComponentNamedList + Lower + ' static ,
440443 {
441- self . insert ( name, Definition :: Func ( HostFunc :: func_wrap ( func) ) ) ?;
444+ self . insert ( name, Definition :: Func ( HostFunc :: func_wrap ( func) ? ) ) ?;
442445 Ok ( ( ) )
443446 }
444447
@@ -485,7 +488,7 @@ impl<T: 'static> LinkerInstance<'_, T> {
485488 Params : ComponentNamedList + Lift + ' static ,
486489 Return : ComponentNamedList + Lower + ' static ,
487490 {
488- self . insert ( name, Definition :: Func ( HostFunc :: func_wrap_async ( f) ) ) ?;
491+ self . insert ( name, Definition :: Func ( HostFunc :: func_wrap_async ( f) ? ) ) ?;
489492 Ok ( ( ) )
490493 }
491494
@@ -550,7 +553,7 @@ impl<T: 'static> LinkerInstance<'_, T> {
550553 if !self . engine . tunables ( ) . concurrency_support {
551554 bail ! ( "concurrent host functions require `Config::concurrency_support`" ) ;
552555 }
553- self . insert ( name, Definition :: Func ( HostFunc :: func_wrap_concurrent ( f) ) ) ?;
556+ self . insert ( name, Definition :: Func ( HostFunc :: func_wrap_concurrent ( f) ? ) ) ?;
554557 Ok ( ( ) )
555558 }
556559
@@ -661,7 +664,7 @@ impl<T: 'static> LinkerInstance<'_, T> {
661664 + Sync
662665 + ' static ,
663666 ) -> Result < ( ) > {
664- self . insert ( name, Definition :: Func ( HostFunc :: func_new ( func) ) ) ?;
667+ self . insert ( name, Definition :: Func ( HostFunc :: func_new ( func) ? ) ) ?;
665668 Ok ( ( ) )
666669 }
667670
@@ -684,7 +687,7 @@ impl<T: 'static> LinkerInstance<'_, T> {
684687 + Sync
685688 + ' static ,
686689 {
687- self . insert ( name, Definition :: Func ( HostFunc :: func_new_async ( func) ) ) ?;
690+ self . insert ( name, Definition :: Func ( HostFunc :: func_new_async ( func) ? ) ) ?;
688691 Ok ( ( ) )
689692 }
690693
@@ -712,7 +715,7 @@ impl<T: 'static> LinkerInstance<'_, T> {
712715 if !self . engine . tunables ( ) . concurrency_support {
713716 bail ! ( "concurrent host functions require `Config::concurrency_support`" ) ;
714717 }
715- self . insert ( name, Definition :: Func ( HostFunc :: func_new_concurrent ( f) ) ) ?;
718+ self . insert ( name, Definition :: Func ( HostFunc :: func_new_concurrent ( f) ? ) ) ?;
716719 Ok ( ( ) )
717720 }
718721
@@ -841,7 +844,7 @@ impl<T: 'static> LinkerInstance<'_, T> {
841844 Ok ( self )
842845 }
843846
844- fn insert ( & mut self , name : & str , item : Definition ) -> Result < usize > {
847+ fn insert ( & mut self , name : & str , item : Definition ) -> Result < Atom > {
845848 self . map
846849 . insert ( name, self . strings , self . allow_shadowing , item)
847850 }
@@ -850,23 +853,3 @@ impl<T: 'static> LinkerInstance<'_, T> {
850853 self . map . get ( name, self . strings )
851854 }
852855}
853-
854- impl NameMapIntern for Strings {
855- type Key = usize ;
856- type BorrowedKey = usize ;
857-
858- fn intern ( & mut self , string : & str ) -> Result < usize , OutOfMemory > {
859- if let Some ( idx) = self . string2idx . get ( string) {
860- return Ok ( * idx) ;
861- }
862- let string: Arc < str > = string. into ( ) ;
863- let idx = self . strings . len ( ) ;
864- self . strings . push ( string. clone ( ) ) ;
865- self . string2idx . insert ( string, idx) ;
866- Ok ( idx)
867- }
868-
869- fn lookup ( & self , string : & str ) -> Option < TryCow < ' _ , usize > > {
870- self . string2idx . get ( string) . copied ( ) . map ( TryCow :: Owned )
871- }
872- }
0 commit comments