Пример #1
0
Variant f_constant(CStrRef name) {
  if (!name.get()) return null;
  const char *data = name.data();
  int len = name.length();
  char *colon;
  if ((colon = (char*)memchr(data, ':', len)) && colon[1] == ':') {
    // class constant
    int classNameLen = colon - data;
    char *constantName = colon + 2;
    String className(data, classNameLen, CopyString);

    // translate "self" or "parent"
    if (className == "self") {
      String this_class = hhvm
        ? g_vmContext->getContextClassName()
        : FrameInjection::GetClassName(true);
      if (this_class.empty()) {
        throw FatalErrorException("Cannot access self:: "
                                  "when no class scope is active");
      } else {
        className = this_class;
      }
    } else if (className == "parent") {
      String parent_class = hhvm
        ? g_vmContext->getParentContextClassName()
        : FrameInjection::GetParentClassName(true);
      if (parent_class.empty()) {
        throw FatalErrorException("Cannot access parent");
      } else {
        className = parent_class;
      }
    }
    // taking care of volatile class
    if (class_exists(className)) {
      return get_class_constant(className, constantName, false);
    } else {
      return null;
    }
  } else {
    if (hhvm) {
      TypedValue* cns = g_vmContext->getCns(name.get());
      if (cns == NULL) {
        if (AutoloadHandler::s_instance->autoloadConstant(name)) {
          cns = g_vmContext->getCns(name.get());
        }
      }
      if (cns) return tvAsVariant(cns);
      return null;
    } else {
      const ClassInfo::ConstantInfo *cinfo = ClassInfo::FindConstant(name);
      // system/uniquely defined scalar constant (must be valid)
      if (cinfo) return cinfo->getValue();
      if (!((Globals*)get_global_variables())->defined(name)) {
        AutoloadHandler::s_instance->autoloadConstant(name);
      }
      // dynamic/redeclared constant
      return ((Globals*)get_global_variables())->getConstant(name.data());
    }
  }
}
Пример #2
0
bool f_defined(CStrRef name, bool autoload /* = true */) {
  if (!name.get()) return false;
  const char *data = name.data();
  int len = name.length();
  char *colon;
  if ((colon = (char*)memchr(data, ':', len)) && colon[1] == ':') {
    // class constant
    int classNameLen = colon - data;
    char *constantName = colon + 2;
    String className(data, classNameLen, CopyString);

    // translate "self" or "parent"
    if (className == "self") {
      String this_class = g_vmContext->getContextClassName();
      if (this_class.empty()) {
        throw FatalErrorException("Cannot access self:: "
          "when no class scope is active");
      } else {
        className = this_class;
      }
    } else if (className == "parent") {
      String parent_class = g_vmContext->getParentContextClassName();
      if (parent_class.empty()) {
        throw FatalErrorException("Cannot access parent");
      } else {
        className = parent_class;
      }
    }
    if (class_exists(className)) { // taking care of volatile class
      const ClassInfo *info;
      for (String parentClass = className;
           !parentClass.empty();
           parentClass = info->getParentClass()) {
        info = ClassInfo::FindClass(parentClass);
        if (!info) {
          assert(false);
        }
        if (info->hasConstant(constantName)) return true;
      }
      return false;
    } else {
      return false;
    }
  } else {
    // system/uniquely defined scalar constant
    if (ClassInfo::FindConstant(name)) return true;
    if (g_vmContext->defined(name)) {
      return true;
    }
    if (!autoload || !AutoloadHandler::s_instance->autoloadConstant(name)) {
      return false;
    }
    return g_vmContext->defined(name);
  }
}