readSparseArray method

  1. @override
VoltronArray readSparseArray()
override

Implementation

@override
VoltronArray readSparseArray() {
  var length = reader.getVarint();
  var array = VoltronArray();
  assignId(array);

  int tag;
  var read = 0;
  while ((tag = readTag()) != SerializationTag.kEndSparseJsArray) {
    read++;
    var key = readValue(tag: tag, location: StringLocation.sparseArrayKey);
    var value =
        readValue(location: StringLocation.sparseArrayItem, relatedKey: key);

    var index = -1;
    if (key is num) {
      index = key.toInt();
    } else if (key is String) {
      try {
        index = int.parse(key);
      } on FormatException {
        // ignore not parsable string
      }
    }
    if (index >= 0) {
      var spaceNeeded = (index + 1) - array.size();
      if (spaceNeeded == 1) {
        // Fast path, item are ordered in general ECMAScript(VM) implementation
        array.push(value);
      } else {
        // Slow path, universal
        for (var i = 0; i < spaceNeeded; i++) {
          array.pushNull();
        }
        array.set(index, value);
      }
    }
  }

  var expected = reader.getVarint();
  if (read != expected) {
    throw UnsupportedError("unexpected number of properties");
  }
  var length2 = reader.getVarint();
  if (length != length2) {
    throw AssertionError("length ambiguity");
  }
  return array;
}