treeSitterCopyWasmStrings function

List<Uint8List?> treeSitterCopyWasmStrings(
  1. Uint8List memory,
  2. int arrayAddress,
  3. int count
)

Copies a table of wasm32 string pointers as independent NUL-free bytes.

Implementation

List<Uint8List?> treeSitterCopyWasmStrings(
  Uint8List memory,
  int arrayAddress,
  int count,
) {
  if (arrayAddress < 0 ||
      count < 0 ||
      arrayAddress + count * 4 > memory.length) {
    throw const FormatException('Wasm string table is outside linear memory');
  }
  final data = ByteData.sublistView(memory);
  return List<Uint8List?>.generate(count, (index) {
    final address = data.getUint32(arrayAddress + index * 4, Endian.little);
    if (address == 0) return null;
    if (address >= memory.length) {
      throw const FormatException('Wasm string is outside linear memory');
    }
    var end = address;
    while (end < memory.length && memory[end] != 0) {
      end++;
    }
    if (end == memory.length) {
      throw const FormatException('Unterminated Wasm string');
    }
    return Uint8List.fromList(memory.sublist(address, end));
  }, growable: false);
}