@@ -44,66 +44,97 @@ impl WasiFactor {
44
44
}
45
45
}
46
46
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
+ trait InitContextExt : InitContext < WasiFactor > {
51
+ fn get_io ( data : & mut Self :: StoreData ) -> IoImpl < WasiImplInner < ' _ > > {
52
+ let ( state, table) = Self :: get_data_with_table ( data) ;
53
+ IoImpl ( WasiImplInner {
54
+ ctx : & mut state. ctx ,
55
+ table,
56
+ } )
57
+ }
51
58
52
- fn init < C > ( & mut self , ctx : & mut C ) -> anyhow:: Result < ( ) >
59
+ fn link_io_bindings (
60
+ & mut self ,
61
+ add_to_linker : impl Fn (
62
+ & mut wasmtime:: component:: Linker < Self :: StoreData > ,
63
+ fn ( & mut Self :: StoreData ) -> IoImpl < WasiImplInner < ' _ > > ,
64
+ ) -> anyhow:: Result < ( ) > ,
65
+ ) -> anyhow:: Result < ( ) > {
66
+ add_to_linker ( self . linker ( ) , Self :: get_io)
67
+ }
68
+
69
+ fn get_wasi ( data : & mut Self :: StoreData ) -> WasiImpl < WasiImplInner < ' _ > > {
70
+ WasiImpl ( Self :: get_io ( data) )
71
+ }
72
+
73
+ fn link_wasi_bindings (
74
+ & mut self ,
75
+ add_to_linker : impl Fn (
76
+ & mut wasmtime:: component:: Linker < Self :: StoreData > ,
77
+ fn ( & mut Self :: StoreData ) -> WasiImpl < WasiImplInner < ' _ > > ,
78
+ ) -> anyhow:: Result < ( ) > ,
79
+ ) -> anyhow:: Result < ( ) > {
80
+ add_to_linker ( self . linker ( ) , Self :: get_wasi)
81
+ }
82
+
83
+ fn link_wasi_default_bindings < O > (
84
+ & mut self ,
85
+ add_to_linker : impl Fn (
86
+ & mut wasmtime:: component:: Linker < Self :: StoreData > ,
87
+ & O ,
88
+ fn ( & mut Self :: StoreData ) -> WasiImpl < WasiImplInner < ' _ > > ,
89
+ ) -> anyhow:: Result < ( ) > ,
90
+ ) -> anyhow:: Result < ( ) >
53
91
where
54
- C : InitContext < Self > ,
92
+ O : Default ,
55
93
{
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
- }
94
+ add_to_linker ( self . linker ( ) , & O :: default ( ) , Self :: get_wasi)
95
+ }
96
+ }
65
97
66
- fn get_wasi < C : InitContext < WasiFactor > > (
67
- data : & mut C :: StoreData ,
68
- ) -> WasiImpl < WasiImplInner < ' _ > > {
69
- WasiImpl ( get_io :: < C > ( data) )
70
- }
98
+ impl < T > InitContextExt for T where T : InitContext < WasiFactor > { }
99
+
100
+ impl Factor for WasiFactor {
101
+ type RuntimeConfig = ( ) ;
102
+ type AppState = ( ) ;
103
+ type InstanceBuilder = InstanceBuilder ;
71
104
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 ( ) ;
105
+ fn init ( & mut self , ctx : & mut impl InitContext < Self > ) -> anyhow:: Result < ( ) > {
75
106
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) ?;
106
107
108
+ ctx. link_wasi_bindings ( bindings:: clocks:: wall_clock:: add_to_linker_get_host) ?;
109
+ ctx. link_wasi_bindings ( bindings:: clocks:: monotonic_clock:: add_to_linker_get_host) ?;
110
+ ctx. link_wasi_bindings ( bindings:: filesystem:: types:: add_to_linker_get_host) ?;
111
+ ctx. link_wasi_bindings ( bindings:: filesystem:: preopens:: add_to_linker_get_host) ?;
112
+ ctx. link_io_bindings ( bindings:: io:: error:: add_to_linker_get_host) ?;
113
+ ctx. link_io_bindings ( bindings:: io:: poll:: add_to_linker_get_host) ?;
114
+ ctx. link_io_bindings ( bindings:: io:: streams:: add_to_linker_get_host) ?;
115
+ ctx. link_wasi_bindings ( bindings:: random:: random:: add_to_linker_get_host) ?;
116
+ ctx. link_wasi_bindings ( bindings:: random:: insecure:: add_to_linker_get_host) ?;
117
+ ctx. link_wasi_bindings ( bindings:: random:: insecure_seed:: add_to_linker_get_host) ?;
118
+ ctx. link_wasi_default_bindings ( bindings:: cli:: exit:: add_to_linker_get_host) ?;
119
+ ctx. link_wasi_bindings ( bindings:: cli:: environment:: add_to_linker_get_host) ?;
120
+ ctx. link_wasi_bindings ( bindings:: cli:: stdin:: add_to_linker_get_host) ?;
121
+ ctx. link_wasi_bindings ( bindings:: cli:: stdout:: add_to_linker_get_host) ?;
122
+ ctx. link_wasi_bindings ( bindings:: cli:: stderr:: add_to_linker_get_host) ?;
123
+ ctx. link_wasi_bindings ( bindings:: cli:: terminal_input:: add_to_linker_get_host) ?;
124
+ ctx. link_wasi_bindings ( bindings:: cli:: terminal_output:: add_to_linker_get_host) ?;
125
+ ctx. link_wasi_bindings ( bindings:: cli:: terminal_stdin:: add_to_linker_get_host) ?;
126
+ ctx. link_wasi_bindings ( bindings:: cli:: terminal_stdout:: add_to_linker_get_host) ?;
127
+ ctx. link_wasi_bindings ( bindings:: cli:: terminal_stderr:: add_to_linker_get_host) ?;
128
+ ctx. link_wasi_bindings ( bindings:: sockets:: tcp:: add_to_linker_get_host) ?;
129
+ ctx. link_wasi_bindings ( bindings:: sockets:: tcp_create_socket:: add_to_linker_get_host) ?;
130
+ ctx. link_wasi_bindings ( bindings:: sockets:: udp:: add_to_linker_get_host) ?;
131
+ ctx. link_wasi_bindings ( bindings:: sockets:: udp_create_socket:: add_to_linker_get_host) ?;
132
+ ctx. link_wasi_bindings ( bindings:: sockets:: instance_network:: add_to_linker_get_host) ?;
133
+ ctx. link_wasi_default_bindings ( bindings:: sockets:: network:: add_to_linker_get_host) ?;
134
+ ctx. link_wasi_bindings ( bindings:: sockets:: ip_name_lookup:: add_to_linker_get_host) ?;
135
+
136
+ ctx. link_wasi_bindings ( wasi_2023_10_18:: add_to_linker) ?;
137
+ ctx. link_wasi_bindings ( wasi_2023_11_10:: add_to_linker) ?;
107
138
Ok ( ( ) )
108
139
}
109
140
0 commit comments