int SharedVariant::getIndex(CVarRef key) {
  ASSERT(is(KindOfArray));
  switch (key.getType()) {
  case KindOfByte:
  case KindOfInt16:
  case KindOfInt32:
  case KindOfInt64: {
    int64 num = key.getNumData();
    if (getIsVector()) {
      if (num < 0 || (size_t) num >= m_data.vec->size) return -1;
      return num;
    }
    return m_data.map->indexOf(num);
  }
  case KindOfStaticString:
  case KindOfString: {
    if (getIsVector()) return -1;
    StringData *sd = key.getStringData();
    return m_data.map->indexOf(sd);
  }
  default:
    // No other types are legitimate keys
    break;
  }
  return -1;
}
Example #2
0
CVarRef Array::add(CVarRef key, CVarRef v, bool isKey /* = false */) {
  if (key.getRawType() == KindOfInt64) {
    return addImpl(key.getNumData(), v);
  }
  if (isKey) return addImpl(key, v);
  VarNR k(key.toKey());
  if (!k.isNull()) {
    return addImpl(k, v);
  }
  return Variant::lvalBlackHole();
}
int ThreadSharedVariant::getIndex(CVarRef key) {
  ASSERT(is(KindOfArray));
  switch (key.getType()) {
  case KindOfByte:
  case KindOfInt16:
  case KindOfInt32:
  case KindOfInt64: {
    int64 num = key.getNumData();
    if (getIsVector()) {
      if (num < 0 || (size_t) num >= m_data.vec->size) return -1;
      return num;
    }
    if (RuntimeOption::ApcUseGnuMap) {
      Int64ToIntMap::const_iterator it = m_data.gnuMap->intMap->find(num);
      if (it == m_data.gnuMap->intMap->end()) return -1;
      return it->second;
    }
    return m_data.map->indexOf(num);
  }
  case KindOfStaticString:
  case KindOfString: {
    if (getIsVector()) return -1;
    StringData *sd = key.getStringData();
    if (RuntimeOption::ApcUseGnuMap) {
      StringDataToIntMap::const_iterator it = m_data.gnuMap->strMap->find(sd);
      if (it == m_data.gnuMap->strMap->end()) return -1;
      return it->second;
    }
    return m_data.map->indexOf(sd);
  }
  default:
    // No other types are legitimate keys
    break;
  }
  return -1;
}