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

Function slice

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

Returns a shallow copy of a portion of the array

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

Source from the content-addressed store, hash-verified

237
238// Returns a shallow copy of a portion of the array
239fn slice<'gc>(
240 mc: &'gc Mutation<'gc>,
241 receiver: Value<'gc>,
242 args: Vec<Value<'gc>>,
243) -> Result<Value<'gc>, VmError> {
244 let list = receiver.as_array()?;
245
246 if args.is_empty() {
247 return Err(VmError::RuntimeError(
248 "slice: expected at least 1 argument".into(),
249 ));
250 }
251
252 let start = float_arg!(&args, 0, "slice")? as isize;
253
254 let list_ref = list.borrow();
255 let list_len = list_ref.data.len() as isize;
256
257 // Calculate start index (handle negative indices)
258 let start_idx = if start < 0 {
259 (list_len + start).max(0) as usize
260 } else {
261 start.min(list_len) as usize
262 };
263
264 // Calculate end index (handle negative indices and optional end parameter)
265 let end_idx = if args.len() > 1 {
266 let end = float_arg!(&args, 1, "slice")? as isize;
267 if end < 0 {
268 (list_len + end).max(0) as usize
269 } else {
270 end.min(list_len) as usize
271 }
272 } else {
273 list_len as usize
274 };
275
276 // Create a new array with the sliced elements
277 let start_idx = start_idx.min(end_idx); // Ensure start <= end
278 let result = list_ref.data[start_idx..end_idx].to_vec();
279
280 Ok(Value::array(mc, result))
281}
282
283// Return the number of times x appears in the list
284fn count<'gc>(

Callers

nothing calls this directly

Calls 4

RuntimeErrorClass · 0.85
as_arrayMethod · 0.80
lenMethod · 0.80
borrowMethod · 0.45

Tested by

no test coverage detected