Insert an item at a given position
(
mc: &'gc Mutation<'gc>,
receiver: Value<'gc>,
args: Vec<Value<'gc>>,
)
| 77 | |
| 78 | // Insert an item at a given position |
| 79 | fn insert<'gc>( |
| 80 | mc: &'gc Mutation<'gc>, |
| 81 | receiver: Value<'gc>, |
| 82 | args: Vec<Value<'gc>>, |
| 83 | ) -> Result<Value<'gc>, VmError> { |
| 84 | let list = receiver.as_array()?; |
| 85 | |
| 86 | if args.len() < 2 { |
| 87 | return Err(VmError::RuntimeError( |
| 88 | "insert: expected 2 arguments (index, value)".into(), |
| 89 | )); |
| 90 | } |
| 91 | |
| 92 | let index = float_arg!(&args, 0, "insert")? as usize; |
| 93 | let value = args[1]; |
| 94 | |
| 95 | let mut list_mut = list.borrow_mut(mc); |
| 96 | |
| 97 | // Check if index is valid |
| 98 | if index > list_mut.data.len() { |
| 99 | return Err(VmError::RuntimeError(format!( |
| 100 | "insert: index {} out of range", |
| 101 | index |
| 102 | ))); |
| 103 | } |
| 104 | |
| 105 | // Insert the value at the specified position |
| 106 | list_mut.data.insert(index, value); |
| 107 | |
| 108 | Ok(receiver) |
| 109 | } |
| 110 | |
| 111 | // Remove the first item from the list whose value is equal to x |
| 112 | fn remove<'gc>( |
nothing calls this directly
no test coverage detected