MCPcopy Create free account
hub / github.com/Ray-D-Song/lexe / subarray

Function subarray

modules/llrt_buffer/src/buffer.rs:356–384  ·  view source on GitHub ↗
(
    this: This<Object<'js>>,
    ctx: Ctx<'js>,
    start: Opt<isize>,
    end: Opt<isize>,
)

Source from the content-addressed store, hash-verified

354}
355
356fn subarray<'js>(
357 this: This<Object<'js>>,
358 ctx: Ctx<'js>,
359 start: Opt<isize>,
360 end: Opt<isize>,
361) -> Result<Value<'js>> {
362 let typed_array = TypedArray::<u8>::from_object(this.0)?;
363 let array_buffer = typed_array.arraybuffer()?;
364 let ab_length = array_buffer.len() as isize;
365 let offset = start.map_or(0, |start| {
366 if start < 0 {
367 (ab_length + start).max(0) as usize
368 } else {
369 start.min(ab_length) as usize
370 }
371 });
372
373 let end_index = end.map_or(ab_length, |end| {
374 if end < 0 {
375 (ab_length + end).max(0)
376 } else {
377 end.min(ab_length)
378 }
379 });
380
381 let length = (end_index as usize).saturating_sub(offset);
382
383 Buffer::from_array_buffer_offset_length(&ctx, array_buffer, offset, length)
384}
385
386fn to_string(this: This<Object<'_>>, ctx: Ctx, encoding: Opt<String>) -> Result<String> {
387 let typed_array = TypedArray::<u8>::from_object(this.0)?;

Callers

nothing calls this directly

Calls 1

lenMethod · 0.45

Tested by

no test coverage detected