(_state: &mut State<'gc>, args: Vec<Value<'gc>>)
| 233 | } |
| 234 | |
| 235 | fn max<'gc>(_state: &mut State<'gc>, args: Vec<Value<'gc>>) -> Result<Value<'gc>, VmError> { |
| 236 | if args.is_empty() { |
| 237 | return Err(VmError::RuntimeError( |
| 238 | "max() takes at least one argument.".into(), |
| 239 | )); |
| 240 | } |
| 241 | |
| 242 | if args.len() == 1 { |
| 243 | // If single argument, it should be an array |
| 244 | match &args[0] { |
| 245 | Value::List(arr) => { |
| 246 | let arr = &arr.borrow().data; |
| 247 | if arr.is_empty() { |
| 248 | return Err(VmError::RuntimeError("max() arg is an empty array".into())); |
| 249 | } |
| 250 | arr.iter() |
| 251 | .max_by(|a, b| { |
| 252 | if let (Value::Number(x), Value::Number(y)) = (a, b) { |
| 253 | x.partial_cmp(y).unwrap() |
| 254 | } else { |
| 255 | panic!("max() array elements must be numbers") |
| 256 | } |
| 257 | }) |
| 258 | .copied() |
| 259 | .ok_or_else(|| VmError::RuntimeError("max() array must not be empty".into())) |
| 260 | } |
| 261 | _ => Err(VmError::RuntimeError( |
| 262 | "single argument to max() must be an array.".into(), |
| 263 | )), |
| 264 | } |
| 265 | } else { |
| 266 | // Multiple arguments case |
| 267 | args.iter() |
| 268 | .max_by(|a, b| { |
| 269 | if let (Value::Number(x), Value::Number(y)) = (a, b) { |
| 270 | x.partial_cmp(y).unwrap() |
| 271 | } else { |
| 272 | panic!("max() arguments must be numbers") |
| 273 | } |
| 274 | }) |
| 275 | .copied() |
| 276 | .ok_or_else(|| VmError::RuntimeError("max() arguments must be numbers".into())) |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | fn round<'gc>(_state: &mut State<'gc>, args: Vec<Value<'gc>>) -> Result<Value<'gc>, VmError> { |
| 281 | if args.len() != 1 { |
nothing calls this directly
no test coverage detected