1+ use crate :: cli:: WasiCliCtx ;
12use crate :: clocks:: { HostMonotonicClock , HostWallClock , WasiClocksCtx } ;
23use crate :: net:: { SocketAddrCheck , SocketAddrUse } ;
34use crate :: random:: WasiRandomCtx ;
@@ -16,17 +17,17 @@ use std::sync::Arc;
1617/// [p2::WasiCtxBuilder](crate::p2::WasiCtxBuilder)
1718///
1819/// [`Store`]: wasmtime::Store
19- pub ( crate ) struct WasiCtxBuilder {
20- pub ( crate ) env : Vec < ( String , String ) > ,
21- pub ( crate ) args : Vec < String > ,
20+ #[ derive( Default ) ]
21+ pub ( crate ) struct WasiCtxBuilder < I , O > {
2222 pub ( crate ) random : WasiRandomCtx ,
2323 pub ( crate ) clocks : WasiClocksCtx ,
24+ pub ( crate ) cli : WasiCliCtx < I , O > ,
2425 pub ( crate ) socket_addr_check : SocketAddrCheck ,
2526 pub ( crate ) allowed_network_uses : AllowedNetworkUses ,
2627 pub ( crate ) allow_blocking_current_thread : bool ,
2728}
2829
29- impl WasiCtxBuilder {
30+ impl < I , O > WasiCtxBuilder < I , O > {
3031 /// Creates a builder for a new context with default parameters set.
3132 ///
3233 /// The current defaults are:
@@ -44,20 +45,45 @@ impl WasiCtxBuilder {
4445 ///
4546 /// These defaults can all be updated via the various builder configuration
4647 /// methods below.
47- pub ( crate ) fn new ( ) -> Self {
48+ pub ( crate ) fn new ( stdin : I , stdout : O , stderr : O ) -> Self {
4849 let random = WasiRandomCtx :: default ( ) ;
4950 let clocks = WasiClocksCtx :: default ( ) ;
51+ let cli = WasiCliCtx {
52+ environment : Vec :: default ( ) ,
53+ arguments : Vec :: default ( ) ,
54+ initial_cwd : None ,
55+ stdin,
56+ stdout,
57+ stderr,
58+ } ;
5059 Self {
51- env : Vec :: new ( ) ,
52- args : Vec :: new ( ) ,
5360 random,
5461 clocks,
62+ cli,
5563 socket_addr_check : SocketAddrCheck :: default ( ) ,
5664 allowed_network_uses : AllowedNetworkUses :: default ( ) ,
5765 allow_blocking_current_thread : false ,
5866 }
5967 }
6068
69+ /// Provides a custom implementation of stdin to use.
70+ pub fn stdin ( & mut self , stdin : I ) -> & mut Self {
71+ self . cli . stdin = stdin;
72+ self
73+ }
74+
75+ /// Same as [`stdin`](WasiCtxBuilder::stdin), but for stdout.
76+ pub fn stdout ( & mut self , stdout : O ) -> & mut Self {
77+ self . cli . stdout = stdout;
78+ self
79+ }
80+
81+ /// Same as [`stdin`](WasiCtxBuilder::stdin), but for stderr.
82+ pub fn stderr ( & mut self , stderr : O ) -> & mut Self {
83+ self . cli . stderr = stderr;
84+ self
85+ }
86+
6187 /// Configures whether or not blocking operations made through this
6288 /// `WasiCtx` are allowed to block the current thread.
6389 ///
@@ -97,7 +123,7 @@ impl WasiCtxBuilder {
97123 /// At this time environment variables are not deduplicated and if the same
98124 /// key is set twice then the guest will see two entries for the same key.
99125 pub fn envs ( & mut self , env : & [ ( impl AsRef < str > , impl AsRef < str > ) ] ) -> & mut Self {
100- self . env . extend (
126+ self . cli . environment . extend (
101127 env. iter ( )
102128 . map ( |( k, v) | ( k. as_ref ( ) . to_owned ( ) , v. as_ref ( ) . to_owned ( ) ) ) ,
103129 ) ;
@@ -109,7 +135,8 @@ impl WasiCtxBuilder {
109135 /// At this time environment variables are not deduplicated and if the same
110136 /// key is set twice then the guest will see two entries for the same key.
111137 pub fn env ( & mut self , k : impl AsRef < str > , v : impl AsRef < str > ) -> & mut Self {
112- self . env
138+ self . cli
139+ . environment
113140 . push ( ( k. as_ref ( ) . to_owned ( ) , v. as_ref ( ) . to_owned ( ) ) ) ;
114141 self
115142 }
@@ -125,13 +152,15 @@ impl WasiCtxBuilder {
125152
126153 /// Appends a list of arguments to the argument array to pass to wasm.
127154 pub fn args ( & mut self , args : & [ impl AsRef < str > ] ) -> & mut Self {
128- self . args . extend ( args. iter ( ) . map ( |a| a. as_ref ( ) . to_owned ( ) ) ) ;
155+ self . cli
156+ . arguments
157+ . extend ( args. iter ( ) . map ( |a| a. as_ref ( ) . to_owned ( ) ) ) ;
129158 self
130159 }
131160
132161 /// Appends a single argument to get passed to wasm.
133162 pub fn arg ( & mut self , arg : impl AsRef < str > ) -> & mut Self {
134- self . args . push ( arg. as_ref ( ) . to_owned ( ) ) ;
163+ self . cli . arguments . push ( arg. as_ref ( ) . to_owned ( ) ) ;
135164 self
136165 }
137166
0 commit comments