(
state: &mut State<'gc>,
args: Vec<Value<'gc>>,
)
| 1 | use crate::{Value, VmError, vm::State}; |
| 2 | |
| 3 | pub(super) fn map<'gc>( |
| 4 | state: &mut State<'gc>, |
| 5 | args: Vec<Value<'gc>>, |
| 6 | ) -> Result<Value<'gc>, VmError> { |
| 7 | if args.len() != 2 { |
| 8 | return Err(VmError::RuntimeError( |
| 9 | "map() takes exactly 2 arguments.".into(), |
| 10 | )); |
| 11 | } |
| 12 | |
| 13 | // Get the iterable |
| 14 | let vec = match &args[0] { |
| 15 | Value::List(list) => &list.borrow().data, |
| 16 | _ => { |
| 17 | return Err(VmError::RuntimeError( |
| 18 | "map() first argument must be an array.".into(), |
| 19 | )); |
| 20 | } |
| 21 | }; |
| 22 | |
| 23 | // Get the function id |
| 24 | let function = match args[1] { |
| 25 | Value::Closure(ref f) => f.function, |
| 26 | Value::NativeFunction(_) => { |
| 27 | return Err(VmError::RuntimeError( |
| 28 | "map() doesn't support native functions yet.".into(), |
| 29 | )); |
| 30 | } |
| 31 | _ => { |
| 32 | return Err(VmError::RuntimeError( |
| 33 | "map() second argument must be a function.".into(), |
| 34 | )); |
| 35 | } |
| 36 | }; |
| 37 | |
| 38 | // Create result array |
| 39 | let mut result = Vec::with_capacity(vec.len()); |
| 40 | |
| 41 | // Apply function to each element |
| 42 | for value in vec.iter() { |
| 43 | // Prepare arguments for the function call |
| 44 | let call_args = vec![*value]; |
| 45 | |
| 46 | // Call the function and convert result |
| 47 | result.push(state.eval_function(function, &call_args)?); |
| 48 | } |
| 49 | |
| 50 | Ok(Value::array(state, result)) |
| 51 | } |
| 52 | |
| 53 | pub(super) fn filter<'gc>( |
| 54 | state: &mut State<'gc>, |
nothing calls this directly
no test coverage detected