示例#1
0
int64_t f_extract(CArrRef var_array, int extract_type /* = EXTR_OVERWRITE */,
                  CStrRef prefix /* = "" */) {
  bool reference = extract_type & EXTR_REFS;
  extract_type &= ~EXTR_REFS;

  VarEnv* v = g_vmContext->getVarEnv();
  if (!v) return 0;
  int count = 0;
  for (ArrayIter iter(var_array); iter; ++iter) {
    String name = iter.first();
    StringData* nameData = name.get();
    switch (extract_type) {
      case EXTR_SKIP:
        if (v->lookup(nameData) != NULL) {
          continue;
        }
        break;
      case EXTR_IF_EXISTS:
        if (v->lookup(nameData) == NULL) {
          continue;
        }
        break;
      case EXTR_PREFIX_SAME:
        if (v->lookup(nameData) != NULL) {
          name = prefix + "_" + name;
        }
        break;
      case EXTR_PREFIX_ALL:
        name = prefix + "_" + name;
        break;
      case EXTR_PREFIX_INVALID:
        if (!is_valid_var_name(nameData->data(), nameData->size())) {
          name = prefix + "_" + name;
        }
        break;
      case EXTR_PREFIX_IF_EXISTS:
        if (v->lookup(nameData) == NULL) {
          continue;
        }
        name = prefix + "_" + name;
        break;
      default:
        break;
    }
    nameData = name.get();
    // skip invalid variable names, as in PHP
    if (!is_valid_var_name(nameData->data(), nameData->size())) {
      continue;
    }
    g_vmContext->setVar(nameData, iter.nvSecond(), reference);
    count++;
  }
  return count;
}
示例#2
0
bool WddxPacket::add_var(const String& varName, bool hasVarTag) {
  VarEnv* v = g_context->getOrCreateVarEnv();
  if (!v) return false;
  Variant varVariant = *reinterpret_cast<Variant*>(v->lookup(varName.get()));
  return recursiveAddVar(varName, varVariant, hasVarTag);
}