mgpuReadAsyncInt32 function

Future<void> mgpuReadAsyncInt32(
  1. MGPUBuffer buffer,
  2. Int32List outputData, {
  3. int readElements = 0,
  4. int elementOffset = 0,
})

Implementation

Future<void> mgpuReadAsyncInt32(
  MGPUBuffer buffer,
  Int32List outputData, {
  int readElements = 0,
  int elementOffset = 0,
}) async {
  final int bytesPerElem = Int32List.bytesPerElement;
  final int elementsToRead = (readElements > 0)
      ? readElements
      : (outputData.length - elementOffset);
  final int sizeToAllocate = elementsToRead * bytesPerElem;

  if (elementsToRead <= 0 ||
      elementOffset < 0 ||
      elementOffset >= outputData.length)
    return;

  final JSNumber ptr = _malloc(sizeToAllocate.toJS);
  final int startIndex = ptr.toDartInt ~/ bytesPerElem;

  try {
    // Add bounds check for the heap access
    if (startIndex < 0 || startIndex + elementsToRead > _heapI32.length) {
      throw StateError(
        'Int32 buffer read would exceed heap bounds: '
        'trying to read $elementsToRead elements at index $startIndex '
        'but heap size is ${_heapI32.length}',
      );
    }
    await ccall(
      "mgpuReadSyncInt32".toJS, // Use sync C++ function
      "void".toJS,
      ["number", "number", "number", "number"].toJSDeep,
      [buffer, ptr, elementsToRead.toJS, elementOffset.toJS].toJSDeep,
      {"async": true}.toJSDeep,
    ).toDart;

    final output = _heapI32.sublist(startIndex, startIndex + elementsToRead);
    outputData.setAll(elementOffset, output);
  } finally {
    _free(ptr);
  }
}