mirascript-vm 0.1.105

Native Rust virtual machine for MiraScript
Documentation
use super::*;

pub(super) fn install(context: &mut Runtime) {
    global_builtin!(context, fn find(call, args) {
        let data = Data::from_value(call, *required(args, 0, "data")?)?;
        let predicate = required(args, 1, "predicate")?;
        let callable = predicate.as_function();
        let original = data.original(call)?;
        let found = iterate_data(
            call,
            &data,
            |_, _| Ok(None),
            |call, key, value, found| {
                call.checkpoint()?;
                let matched = if let Some(callable) = callable {
                    operations::to_boolean(callable.call(call, &[value, key, original])?)?
                } else {
                    operations::same_value(call, value, *predicate)?
                };
                if matched {
                    *found = Some((key, value));
                }
                Ok(!matched)
            },
        )?;
        match found {
            Some((key, value)) => pair(call, key, value),
            None => Ok(MiraValue::NIL),
        }
    });
}