Exemplo n.º 1
0
bool MutableArrayIter::advance() {
  ArrayData *data = m_var ? getData() : m_data;
  if (!data) return false;
  // If the foreach loop's array changed since the previous iteration,
  // we recover by creating a new strong iterator for the new array,
  // starting with at the position indicated by the new array's internal
  // pointer.
  if (m_fp.container != data) {
    // Free the current strong iterator if its valid
    if (m_fp.container != NULL) {
      m_fp.container->freeFullPos(m_fp);
    }
    // Create a new strong iterator for the new array
    ASSERT(m_fp.container == NULL);
    data->newFullPos(m_fp);
  }
  ASSERT(m_fp.container == data);
  if (!data->setFullPos(m_fp)) return false;
  CVarRef curr = data->currentRef();
  curr.setContagious();
  m_val = curr;
  if (m_key) *m_key = data->key();
  data->next();
  data->getFullPos(m_fp);
  return true;
}
Exemplo n.º 2
0
bool MutableArrayIter::advance() {
  ArrayData *data = getData();
  if (!data) return false;
  if (!data->setFullPos(m_pos)) return false;
  CVarRef curr = data->currentRef();
  curr.setContagious();
  m_val = curr;
  if (m_key) *m_key = data->key();
  data->next();
  data->getFullPos(m_pos);
  return true;
}
Exemplo n.º 3
0
void Parameter::bind(VariableEnvironment &env, CVarRef val,
                     bool ref /* = false */) const {
  if (m_kind != KindOfNull) {
    DataType otype = val.getType();
    if (!(m_nullDefault && otype == KindOfNull ||
          otype == m_kind &&
          (m_kind != KindOfObject ||
           m_kind == KindOfObject &&
           val.toObject().instanceof(m_type.c_str())))) {
      throw_unexpected_argument_type(m_argNum, m_fnName, m_type.c_str(), val);
    }
  }
  if (ref) val.setContagious();
  env.getIdx(m_idx) = val;
}
Exemplo n.º 4
0
Array ArrayUtil::Slice(CArrRef input, int offset, int length,
                       bool preserve_keys) {
  int num_in = input.size();
  if (offset > num_in) {
    offset = num_in;
  } else if (offset < 0 && (offset = (num_in + offset)) < 0) {
    offset = 0;
  }

  if (length < 0) {
    length = num_in - offset + length;
  } else if (((unsigned)offset + (unsigned)length) > (unsigned)num_in) {
    length = num_in - offset;
  }

  Array out_hash = Array::Create();
  int pos = 0;
  ArrayIter iter(input);
  bool supportRef = input->supportValueRef();
  for (; pos < offset && iter; ++pos, ++iter) {}
  for (; pos < offset + length && iter; ++pos, ++iter) {
    bool doAppend = !preserve_keys && iter.first().isNumeric();
    if (supportRef) {
      CVarRef v = iter.secondRef();
      if (v.isReferenced()) v.setContagious();
      if (doAppend) {
        out_hash.append(v);
      } else {
        out_hash.set(iter.first(), v);
      }
    } else {
      if (doAppend) {
        out_hash.append(iter.second());
      } else {
        out_hash.set(iter.first(), iter.second());
      }
    }
  }
  return out_hash;
}