Remove the item at the given position and return it
(
mc: &'gc Mutation<'gc>,
receiver: Value<'gc>,
args: Vec<Value<'gc>>,
)
| 140 | |
| 141 | // Remove the item at the given position and return it |
| 142 | fn pop<'gc>( |
| 143 | mc: &'gc Mutation<'gc>, |
| 144 | receiver: Value<'gc>, |
| 145 | args: Vec<Value<'gc>>, |
| 146 | ) -> Result<Value<'gc>, VmError> { |
| 147 | let list = receiver.as_array()?; |
| 148 | let mut list_mut = list.borrow_mut(mc); |
| 149 | |
| 150 | // If list is empty, return an error |
| 151 | if list_mut.data.is_empty() { |
| 152 | return Err(VmError::RuntimeError( |
| 153 | "pop: cannot pop from empty list".into(), |
| 154 | )); |
| 155 | } |
| 156 | |
| 157 | let index = if args.is_empty() { |
| 158 | // Default to the last element if no index is provided |
| 159 | list_mut.data.len() - 1 |
| 160 | } else { |
| 161 | float_arg!(&args, 0, "pop")? as usize |
| 162 | }; |
| 163 | |
| 164 | // Check if index is valid |
| 165 | if index >= list_mut.data.len() { |
| 166 | return Err(VmError::RuntimeError(format!( |
| 167 | "pop: index {} out of range", |
| 168 | index |
| 169 | ))); |
| 170 | } |
| 171 | |
| 172 | // Remove and return the value at the specified position |
| 173 | Ok(list_mut.data.remove(index)) |
| 174 | } |
| 175 | |
| 176 | // Remove all items from the list |
| 177 | fn clear<'gc>( |
nothing calls this directly
no test coverage detected