MCPcopy Create free account
hub / github.com/aiscriptdev/aiscript / max

Function max

aiscript-vm/src/builtins/mod.rs:235–278  ·  view source on GitHub ↗
(_state: &mut State<'gc>, args: Vec<Value<'gc>>)

Source from the content-addressed store, hash-verified

233}
234
235fn 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
280fn round<'gc>(_state: &mut State<'gc>, args: Vec<Value<'gc>>) -> Result<Value<'gc>, VmError> {
281 if args.len() != 1 {

Callers

nothing calls this directly

Calls 4

RuntimeErrorClass · 0.85
lenMethod · 0.80
borrowMethod · 0.45
partial_cmpMethod · 0.45

Tested by

no test coverage detected