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

Function pop

aiscript-vm/src/builtins/array.rs:142–174  ·  view source on GitHub ↗

Remove the item at the given position and return it

(
    mc: &'gc Mutation<'gc>,
    receiver: Value<'gc>,
    args: Vec<Value<'gc>>,
)

Source from the content-addressed store, hash-verified

140
141// Remove the item at the given position and return it
142fn 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
177fn clear<'gc>(

Callers

nothing calls this directly

Calls 4

RuntimeErrorClass · 0.85
as_arrayMethod · 0.80
lenMethod · 0.80
borrow_mutMethod · 0.45

Tested by

no test coverage detected