@@ -71,6 +71,7 @@ pub fn get_host_const(name: &str, fun: &Function, prog: &Program) -> Expr
7171 static TIME_CURRENT_MS : HostFn = HostFn { name : "time_current_ms" , f : Fn0 ( time_current_ms) } ;
7272 static CMD_NUM_ARGS : HostFn = HostFn { name : "cmd_num_args" , f : Fn0 ( cmd_num_args) } ;
7373 static CMD_GET_ARG : HostFn = HostFn { name : "cmd_get_arg" , f : Fn1 ( cmd_get_arg) } ;
74+ static CMD_GET_ARG_OR : HostFn = HostFn { name : "cmd_get_arg_or" , f : Fn2 ( cmd_get_arg_or) } ;
7475 static PRINT : HostFn = HostFn { name : "print" , f : Fn1 ( print) } ;
7576 static PRINTLN : HostFn = HostFn { name : "println" , f : Fn1 ( println) } ;
7677 static READLN : HostFn = HostFn { name : "readln" , f : Fn0 ( readln) } ;
@@ -99,6 +100,7 @@ pub fn get_host_const(name: &str, fun: &Function, prog: &Program) -> Expr
99100
100101 "cmd_num_args" => & CMD_NUM_ARGS ,
101102 "cmd_get_arg" => & CMD_GET_ARG ,
103+ "cmd_get_arg_or" => & CMD_GET_ARG_OR ,
102104
103105 "print" => & PRINT ,
104106 "println" => & PRINTLN ,
@@ -155,16 +157,14 @@ pub fn cmd_num_args(actor: &mut Actor) -> Result<Value, String>
155157}
156158
157159/// Get a command-line argument string by index
158- /// Note: if we allocate just one object then we can be
159- /// guaranteed that object won't be GC'd while this function runs
160- pub fn cmd_get_arg ( actor : & mut Actor , idx : Value ) -> Result < Value , String >
160+ pub fn cmd_get_arg_or ( actor : & mut Actor , idx : Value , default : Value ) -> Result < Value , String >
161161{
162162 let idx = idx. unwrap_usize ( ) ;
163163
164164 let args = crate :: REST_ARGS . lock ( ) . unwrap ( ) ;
165165
166166 if idx >= args. len ( ) {
167- return Ok ( Value :: Nil ) ;
167+ return Ok ( default ) ;
168168 }
169169
170170 let arg_str = & args[ idx] ;
@@ -177,6 +177,12 @@ pub fn cmd_get_arg(actor: &mut Actor, idx: Value) -> Result<Value, String>
177177 Ok ( actor. alloc . str_val ( arg_str) . unwrap ( ) )
178178}
179179
180+ /// Get a command-line argument string by index
181+ pub fn cmd_get_arg ( actor : & mut Actor , idx : Value ) -> Result < Value , String >
182+ {
183+ cmd_get_arg_or ( actor, idx, Value :: Nil )
184+ }
185+
180186/// Print a value to stdout
181187fn print ( actor : & mut Actor , v : Value ) -> Result < Value , String >
182188{
0 commit comments