static Variant HHVM_FUNCTION(assert, const Variant& assertion) { if (!s_option_data->assertActive) return true; JIT::CallerFrame cf; Offset callerOffset; auto const fp = cf(&callerOffset); auto const passed = [&]() -> bool { if (assertion.isString()) { if (RuntimeOption::EvalAuthoritativeMode) { // We could support this with compile-time string literals, // but it's not yet implemented. throw NotSupportedException(__func__, "assert with strings argument in RepoAuthoritative mode"); } return eval_for_assert(fp, assertion.toString()).toBoolean(); } return assertion.toBoolean(); }(); if (passed) return true; if (!s_option_data->assertCallback.isNull()) { auto const unit = fp->m_func->unit(); PackedArrayInit ai(3); ai.append(String(const_cast<StringData*>(unit->filepath()))); ai.append(Variant(unit->getLineNumber(callerOffset))); ai.append(assertion.isString() ? assertion.toString() : static_cast<String>(empty_string)); f_call_user_func(1, s_option_data->assertCallback, ai.toArray()); } if (s_option_data->assertWarning) { auto const str = !assertion.isString() ? String("Assertion failed") : concat3("Assertion \"", assertion.toString(), "\" failed"); raise_warning("%s", str.data()); } if (s_option_data->assertBail) { throw Assertion(); } return uninit_null(); }
Variant f_assert(CVarRef assertion) { if (!s_option_data->assertActive) return true; Transl::CallerFrame cf; Offset callerOffset; auto const fp = cf(&callerOffset); auto const passed = [&]() -> bool { if (assertion.isString()) { if (RuntimeOption::RepoAuthoritative) { // We could support this with compile-time string literals, // but it's not yet implemented. throw NotSupportedException(__func__, "assert with strings argument in RepoAuthoritative mode"); } return eval_for_assert(fp, assertion.toString()).toBoolean(); } return assertion.toBoolean(); }(); if (passed) return true; if (!s_option_data->assertCallback.isNull()) { auto const unit = fp->m_func->unit(); ArrayInit ai(3, ArrayInit::vectorInit); ai.set(String(unit->filepath())); ai.set(Variant(unit->getLineNumber(callerOffset))); ai.set(assertion.isString() ? assertion.toString() : String("")); f_call_user_func(1, s_option_data->assertCallback, ai.toArray()); } if (s_option_data->assertWarning) { auto const str = !assertion.isString() ? String("Assertion failed") : concat3("Assertion \"", assertion.toString(), "\" failed"); raise_warning("%s", str.data()); } if (s_option_data->assertBail) { throw Assertion(); } return uninit_null(); }
bool f_is_callable(CVarRef v, bool syntax /* = false */, Variant name /* = null */) { if (v.isString()) { if (!name.isNull()) name = v; if (syntax) return true; string lowered = Util::toLower((const char *)v.toString()); size_t c = lowered.find("::"); if (c != 0 && c != string::npos && c+2 < lowered.size()) { string classname = lowered.substr(0, c); string methodname = lowered.substr(c+2); return f_call_user_func(2, "class_exists", Array::Create(String(classname))) && ClassInfo::hasAccess(classname, methodname, true, false); } return f_function_exists(v.toString()); } if (v.is(KindOfArray)) { Array arr = v.toArray(); if (arr.size() == 2 && arr.exists(0LL) && arr.exists(1LL)) { Variant v0 = arr.rvalAt(0LL); Variant v1 = arr.rvalAt(1LL); Object obj; bool staticCall = false; if (v0.is(KindOfObject)) { obj = v0.toObject(); v0 = obj->o_getClassName(); } else if (v0.isString()) { if (!f_call_user_func(2, "class_exists", Array::Create(v0))) { return false; } staticCall = true; } if (v1.isString()) { string lowered = Util::toLower((const char *)v1.toString()); size_t c = lowered.find("::"); if (c != 0 && c != string::npos && c+2 < lowered.size()) { string name1 = Util::toLower((const char *)v0.toString()); string name2 = lowered.substr(0, c); if (name1 == name2 || ClassInfo::isSubClass(name1, name2, false)) { staticCall = true; v0 = name2; v1 = lowered.substr(c+2); } } } if (v0.isString() && v1.isString()) { if (!name.isNull()) { name = v0.toString() + "::" + v1.toString(); } if (same(v0, "self") || same(v0, "parent")) { throw NotImplementedException("augmenting class scope info"); } return ClassInfo::hasAccess(v0, v1, staticCall, !obj.isNull()); } } } if (!name.isNull()) { name = v.toString(); } return false; }
bool TestExtFunction::test_call_user_func() { Variant ret = f_call_user_func(1, "TEst", CREATE_VECTOR1("param")); VS(ret, "param"); return Count(true); }