(_state: &mut State<'gc>, args: Vec<Value<'gc>>)
| 188 | } |
| 189 | |
| 190 | fn min<'gc>(_state: &mut State<'gc>, args: Vec<Value<'gc>>) -> Result<Value<'gc>, VmError> { |
| 191 | if args.is_empty() { |
| 192 | return Err(VmError::RuntimeError( |
| 193 | "min() takes at least one argument.".into(), |
| 194 | )); |
| 195 | } |
| 196 | |
| 197 | if args.len() == 1 { |
| 198 | // If single argument, it should be an array |
| 199 | match &args[0] { |
| 200 | Value::List(arr) => { |
| 201 | let arr = &arr.borrow().data; |
| 202 | if arr.is_empty() { |
| 203 | return Err(VmError::RuntimeError("min() arg is an empty array".into())); |
| 204 | } |
| 205 | arr.iter() |
| 206 | .min_by(|a, b| { |
| 207 | if let (Value::Number(x), Value::Number(y)) = (a, b) { |
| 208 | x.partial_cmp(y).unwrap() |
| 209 | } else { |
| 210 | panic!("min() array elements must be numbers") |
| 211 | } |
| 212 | }) |
| 213 | .copied() |
| 214 | .ok_or_else(|| VmError::RuntimeError("min() array must not be empty".into())) |
| 215 | } |
| 216 | _ => Err(VmError::RuntimeError( |
| 217 | "single argument to min() must be an array.".into(), |
| 218 | )), |
| 219 | } |
| 220 | } else { |
| 221 | // Multiple arguments case |
| 222 | args.iter() |
| 223 | .min_by(|a, b| { |
| 224 | if let (Value::Number(x), Value::Number(y)) = (a, b) { |
| 225 | x.partial_cmp(y).unwrap() |
| 226 | } else { |
| 227 | panic!("min() arguments must be numbers") |
| 228 | } |
| 229 | }) |
| 230 | .copied() |
| 231 | .ok_or_else(|| VmError::RuntimeError("min() arguments must be numbers".into())) |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | fn max<'gc>(_state: &mut State<'gc>, args: Vec<Value<'gc>>) -> Result<Value<'gc>, VmError> { |
| 236 | if args.is_empty() { |
nothing calls this directly
no test coverage detected