Variant HHVM_FUNCTION(constant, const String& name) { if (!name.get()) return init_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; Class* cls = getClassByName(data, classNameLen); if (cls) { String cnsName(constantName, data + len - constantName, CopyString); Cell cns = cls->clsCnsGet(cnsName.get()); if (cns.m_type != KindOfUninit) { return cellAsCVarRef(cns); } } } else { auto const cns = Unit::loadCns(name.get()); if (cns) return tvAsCVarRef(cns); } raise_warning("constant(): Couldn't find constant %s", data); return init_null(); }
bool f_defined(const String& name, bool autoload /* = true */) { if (!name.get()) return false; const char *data = name.data(); int len = name.length(); // slice off starting backslash bool hadInitialBackslash = false; if (len > 0 && data[0] == '\\') { data += 1; len -= 1; hadInitialBackslash = true; } char *colon; if ((colon = (char*)memchr(data, ':', len)) && colon[1] == ':') { // class constant int classNameLen = colon - data; char *constantName = colon + 2; Class* cls = getClassByName(data, classNameLen); if (cls) { String cnsName(constantName, data + len - constantName, CopyString); return cls->clsCnsGet(cnsName.get()).m_type != KindOfUninit; } return false; } else { auto* cb = autoload ? Unit::loadCns : Unit::lookupCns; if (hadInitialBackslash) { String s(data, len, CopyString); return cb(s.get()); } else { return cb(name.get()); } } }
Array vm_get_class_constants(CStrRef className) { Class* cls = Unit::loadClass(className.get()); if (cls == NULL) { return ArrayData::Make(0); } size_t numConstants = cls->numConstants(); auto retVal = ArrayData::Make(numConstants); const Class::Const* consts = cls->constants(); for (size_t i = 0; i < numConstants; i++) { // Note: hphpc doesn't include inherited constants in // get_class_constants(), so mimic that behavior if (consts[i].m_class == cls) { StringData* name = const_cast<StringData*>(consts[i].m_name); const TypedValue* value = &consts[i].m_val; // Handle dynamically set constants if (value->m_type == KindOfUninit) { value = cls->clsCnsGet(consts[i].m_name); } retVal->set(name, tvAsCVarRef(value), false); } } return retVal; }
bool HHVM_FUNCTION(defined, const String& 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; Class* cls = getClassByName(data, classNameLen); if (cls) { String cnsName(constantName, data + len - constantName, CopyString); return cls->clsCnsGet(cnsName.get()).m_type != KindOfUninit; } return false; } else { auto* cb = autoload ? Unit::loadCns : Unit::lookupCns; return cb(name.get()); } }
Variant f_constant(const String& name) { if (!name.get()) return uninit_null(); const char *data = name.data(); int len = name.length(); // slice off starting backslash bool hadInitialBackslash = false; if (len > 0 && data[0] == '\\') { data += 1; len -= 1; hadInitialBackslash = true; } char *colon; if ((colon = (char*)memchr(data, ':', len)) && colon[1] == ':') { // class constant int classNameLen = colon - data; char *constantName = colon + 2; Class* cls = getClassByName(data, classNameLen); if (cls) { String cnsName(constantName, data + len - constantName, CopyString); Cell cns = cls->clsCnsGet(cnsName.get()); if (cns.m_type != KindOfUninit) { return cellAsCVarRef(cns); } } raise_warning("Couldn't find constant %s", data); } else { TypedValue* cns; if (hadInitialBackslash) { String s(data, len, CopyString); cns = Unit::loadCns(s.get()); } else { cns = Unit::loadCns(name.get()); } if (cns) return tvAsVariant(cns); } return uninit_null(); }