getPropertyAddress method

int getPropertyAddress(
  1. int? pnum
)

Implementation

int getPropertyAddress(int? pnum) {
  if (pnum == 0) return 0;

  var propNum = 999999;
  int addr = propertyTableStart;

  while (propNum > pnum!) {
    var len = ZMachine.verToInt(Z.engine.version) <= 3 ||
            !BinaryHelper.isSet(Z.engine.mem.loadb(addr), 7)
        ? propertyLength(addr)
        : propertyLength(addr + 1);

    propNum = propertyNumber(addr);

    //not found (ref 12.4.1)
    if (len == 0) {
      return 0;
    }

    if (propNum == pnum) {
      if (ZMachine.verToInt(Z.engine.version) <= 3) {
        return addr + 1;
      } else {
        return addr + (len > 2 ? 2 : 1);
      }
    }

    //skip to the next property
    if (ZMachine.verToInt(Z.engine.version) <= 3) {
      addr += (len + 1);
    } else {
      //if property len > 2, account for the second
      //size byte in the header
      addr += (len + ((len > 2) ? 2 : 1));
    }
  }

  //return 0 if not found
  return 0;
}