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

Function insert

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

Insert an item at a given position

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

Source from the content-addressed store, hash-verified

77
78// Insert an item at a given position
79fn 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
112fn remove<'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