@@ -44,66 +44,98 @@ impl WasiFactor {
4444 }
4545}
4646
47- impl Factor for WasiFactor {
48- type RuntimeConfig = ( ) ;
49- type AppState = ( ) ;
50- type InstanceBuilder = InstanceBuilder ;
47+ /// Helper trait to extend `InitContext` with some more `link_*_bindings`
48+ /// methods related to `wasmtime-wasi` and `wasmtime-wasi-io`-specific
49+ /// signatures.
50+ #[ allow( clippy:: type_complexity, reason = "sorry, blame alex" ) ]
51+ trait InitContextExt : InitContext < WasiFactor > {
52+ fn get_io ( data : & mut Self :: StoreData ) -> IoImpl < WasiImplInner < ' _ > > {
53+ let ( state, table) = Self :: get_data_with_table ( data) ;
54+ IoImpl ( WasiImplInner {
55+ ctx : & mut state. ctx ,
56+ table,
57+ } )
58+ }
5159
52- fn init < C > ( & mut self , ctx : & mut C ) -> anyhow:: Result < ( ) >
60+ fn link_io_bindings (
61+ & mut self ,
62+ add_to_linker : fn (
63+ & mut wasmtime:: component:: Linker < Self :: StoreData > ,
64+ fn ( & mut Self :: StoreData ) -> IoImpl < WasiImplInner < ' _ > > ,
65+ ) -> anyhow:: Result < ( ) > ,
66+ ) -> anyhow:: Result < ( ) > {
67+ add_to_linker ( self . linker ( ) , Self :: get_io)
68+ }
69+
70+ fn get_wasi ( data : & mut Self :: StoreData ) -> WasiImpl < WasiImplInner < ' _ > > {
71+ WasiImpl ( Self :: get_io ( data) )
72+ }
73+
74+ fn link_wasi_bindings (
75+ & mut self ,
76+ add_to_linker : fn (
77+ & mut wasmtime:: component:: Linker < Self :: StoreData > ,
78+ fn ( & mut Self :: StoreData ) -> WasiImpl < WasiImplInner < ' _ > > ,
79+ ) -> anyhow:: Result < ( ) > ,
80+ ) -> anyhow:: Result < ( ) > {
81+ add_to_linker ( self . linker ( ) , Self :: get_wasi)
82+ }
83+
84+ fn link_wasi_default_bindings < O > (
85+ & mut self ,
86+ add_to_linker : fn (
87+ & mut wasmtime:: component:: Linker < Self :: StoreData > ,
88+ & O ,
89+ fn ( & mut Self :: StoreData ) -> WasiImpl < WasiImplInner < ' _ > > ,
90+ ) -> anyhow:: Result < ( ) > ,
91+ ) -> anyhow:: Result < ( ) >
5392 where
54- C : InitContext < Self > ,
93+ O : Default ,
5594 {
56- fn get_io < C : InitContext < WasiFactor > > (
57- data : & mut C :: StoreData ,
58- ) -> IoImpl < WasiImplInner < ' _ > > {
59- let ( state, table) = C :: get_data_with_table ( data) ;
60- IoImpl ( WasiImplInner {
61- ctx : & mut state. ctx ,
62- table,
63- } )
64- }
95+ add_to_linker ( self . linker ( ) , & O :: default ( ) , Self :: get_wasi)
96+ }
97+ }
6598
66- fn get_wasi < C : InitContext < WasiFactor > > (
67- data : & mut C :: StoreData ,
68- ) -> WasiImpl < WasiImplInner < ' _ > > {
69- WasiImpl ( get_io :: < C > ( data) )
70- }
99+ impl < T > InitContextExt for T where T : InitContext < WasiFactor > { }
100+
101+ impl Factor for WasiFactor {
102+ type RuntimeConfig = ( ) ;
103+ type AppState = ( ) ;
104+ type InstanceBuilder = InstanceBuilder ;
71105
72- let get_io = get_io :: < C > as fn ( & mut C :: StoreData ) -> IoImpl < WasiImplInner < ' _ > > ;
73- let get_wasi = get_wasi :: < C > as fn ( & mut C :: StoreData ) -> WasiImpl < WasiImplInner < ' _ > > ;
74- let linker = ctx. linker ( ) ;
106+ fn init ( & mut self , ctx : & mut impl InitContext < Self > ) -> anyhow:: Result < ( ) > {
75107 use wasmtime_wasi:: bindings;
76- bindings:: clocks:: wall_clock:: add_to_linker_get_host ( linker, get_wasi) ?;
77- bindings:: clocks:: monotonic_clock:: add_to_linker_get_host ( linker, get_wasi) ?;
78- bindings:: filesystem:: types:: add_to_linker_get_host ( linker, get_wasi) ?;
79- bindings:: filesystem:: preopens:: add_to_linker_get_host ( linker, get_wasi) ?;
80- bindings:: io:: error:: add_to_linker_get_host ( linker, get_io) ?;
81- bindings:: io:: poll:: add_to_linker_get_host ( linker, get_io) ?;
82- bindings:: io:: streams:: add_to_linker_get_host ( linker, get_io) ?;
83- bindings:: random:: random:: add_to_linker_get_host ( linker, get_wasi) ?;
84- bindings:: random:: insecure:: add_to_linker_get_host ( linker, get_wasi) ?;
85- bindings:: random:: insecure_seed:: add_to_linker_get_host ( linker, get_wasi) ?;
86- bindings:: cli:: exit:: add_to_linker_get_host ( linker, & Default :: default ( ) , get_wasi) ?;
87- bindings:: cli:: environment:: add_to_linker_get_host ( linker, get_wasi) ?;
88- bindings:: cli:: stdin:: add_to_linker_get_host ( linker, get_wasi) ?;
89- bindings:: cli:: stdout:: add_to_linker_get_host ( linker, get_wasi) ?;
90- bindings:: cli:: stderr:: add_to_linker_get_host ( linker, get_wasi) ?;
91- bindings:: cli:: terminal_input:: add_to_linker_get_host ( linker, get_wasi) ?;
92- bindings:: cli:: terminal_output:: add_to_linker_get_host ( linker, get_wasi) ?;
93- bindings:: cli:: terminal_stdin:: add_to_linker_get_host ( linker, get_wasi) ?;
94- bindings:: cli:: terminal_stdout:: add_to_linker_get_host ( linker, get_wasi) ?;
95- bindings:: cli:: terminal_stderr:: add_to_linker_get_host ( linker, get_wasi) ?;
96- bindings:: sockets:: tcp:: add_to_linker_get_host ( linker, get_wasi) ?;
97- bindings:: sockets:: tcp_create_socket:: add_to_linker_get_host ( linker, get_wasi) ?;
98- bindings:: sockets:: udp:: add_to_linker_get_host ( linker, get_wasi) ?;
99- bindings:: sockets:: udp_create_socket:: add_to_linker_get_host ( linker, get_wasi) ?;
100- bindings:: sockets:: instance_network:: add_to_linker_get_host ( linker, get_wasi) ?;
101- bindings:: sockets:: network:: add_to_linker_get_host ( linker, & Default :: default ( ) , get_wasi) ?;
102- bindings:: sockets:: ip_name_lookup:: add_to_linker_get_host ( linker, get_wasi) ?;
103-
104- wasi_2023_10_18:: add_to_linker ( linker, get_wasi) ?;
105- wasi_2023_11_10:: add_to_linker ( linker, get_wasi) ?;
106108
109+ ctx. link_wasi_bindings ( bindings:: clocks:: wall_clock:: add_to_linker_get_host) ?;
110+ ctx. link_wasi_bindings ( bindings:: clocks:: monotonic_clock:: add_to_linker_get_host) ?;
111+ ctx. link_wasi_bindings ( bindings:: filesystem:: types:: add_to_linker_get_host) ?;
112+ ctx. link_wasi_bindings ( bindings:: filesystem:: preopens:: add_to_linker_get_host) ?;
113+ ctx. link_io_bindings ( bindings:: io:: error:: add_to_linker_get_host) ?;
114+ ctx. link_io_bindings ( bindings:: io:: poll:: add_to_linker_get_host) ?;
115+ ctx. link_io_bindings ( bindings:: io:: streams:: add_to_linker_get_host) ?;
116+ ctx. link_wasi_bindings ( bindings:: random:: random:: add_to_linker_get_host) ?;
117+ ctx. link_wasi_bindings ( bindings:: random:: insecure:: add_to_linker_get_host) ?;
118+ ctx. link_wasi_bindings ( bindings:: random:: insecure_seed:: add_to_linker_get_host) ?;
119+ ctx. link_wasi_default_bindings ( bindings:: cli:: exit:: add_to_linker_get_host) ?;
120+ ctx. link_wasi_bindings ( bindings:: cli:: environment:: add_to_linker_get_host) ?;
121+ ctx. link_wasi_bindings ( bindings:: cli:: stdin:: add_to_linker_get_host) ?;
122+ ctx. link_wasi_bindings ( bindings:: cli:: stdout:: add_to_linker_get_host) ?;
123+ ctx. link_wasi_bindings ( bindings:: cli:: stderr:: add_to_linker_get_host) ?;
124+ ctx. link_wasi_bindings ( bindings:: cli:: terminal_input:: add_to_linker_get_host) ?;
125+ ctx. link_wasi_bindings ( bindings:: cli:: terminal_output:: add_to_linker_get_host) ?;
126+ ctx. link_wasi_bindings ( bindings:: cli:: terminal_stdin:: add_to_linker_get_host) ?;
127+ ctx. link_wasi_bindings ( bindings:: cli:: terminal_stdout:: add_to_linker_get_host) ?;
128+ ctx. link_wasi_bindings ( bindings:: cli:: terminal_stderr:: add_to_linker_get_host) ?;
129+ ctx. link_wasi_bindings ( bindings:: sockets:: tcp:: add_to_linker_get_host) ?;
130+ ctx. link_wasi_bindings ( bindings:: sockets:: tcp_create_socket:: add_to_linker_get_host) ?;
131+ ctx. link_wasi_bindings ( bindings:: sockets:: udp:: add_to_linker_get_host) ?;
132+ ctx. link_wasi_bindings ( bindings:: sockets:: udp_create_socket:: add_to_linker_get_host) ?;
133+ ctx. link_wasi_bindings ( bindings:: sockets:: instance_network:: add_to_linker_get_host) ?;
134+ ctx. link_wasi_default_bindings ( bindings:: sockets:: network:: add_to_linker_get_host) ?;
135+ ctx. link_wasi_bindings ( bindings:: sockets:: ip_name_lookup:: add_to_linker_get_host) ?;
136+
137+ ctx. link_wasi_bindings ( wasi_2023_10_18:: add_to_linker) ?;
138+ ctx. link_wasi_bindings ( wasi_2023_11_10:: add_to_linker) ?;
107139 Ok ( ( ) )
108140 }
109141
0 commit comments