MCPcopy Index your code
hub / github.com/aiscriptdev/aiscript / map

Function map

aiscript-vm/src/builtins/function.rs:3–51  ·  view source on GitHub ↗
(
    state: &mut State<'gc>,
    args: Vec<Value<'gc>>,
)

Source from the content-addressed store, hash-verified

1use crate::{Value, VmError, vm::State};
2
3pub(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
53pub(super) fn filter<'gc>(
54 state: &mut State<'gc>,

Callers

nothing calls this directly

Calls 5

RuntimeErrorClass · 0.85
lenMethod · 0.80
pushMethod · 0.80
borrowMethod · 0.45
eval_functionMethod · 0.45

Tested by

no test coverage detected