Skip to content

Commit 5ff7ba9

Browse files
committed
Add $cmd_get_arg_or() host function
1 parent 4a6b56f commit 5ff7ba9

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

docs/language.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ These host functions are defined in [`src/host.rs`](/src/host.rs):
242242
- `$time_current_ms()`: Returns the current time in milliseconds since the Unix epoch.
243243
- `$cmd_num_args()`: Get the number of command-line arguments available to the program.
244244
- `$cmd_get_arg(idx)`: Get the command-line argument at the given index. Returns `nil` if absent.
245+
- `$cmd_get_arg_or(idx, default)`: Get the command-line argument at the given index. Returns `default` if absent.
245246
- `$print(value)`: Prints a value to the console.
246247
- `$println(value)`: Prints a value to the console, followed by a newline.
247248
- `$readln()`: Read one line of input into a string.

src/host.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
181187
fn print(actor: &mut Actor, v: Value) -> Result<Value, String>
182188
{

0 commit comments

Comments
 (0)