Пример #1
0
static Variant HHVM_FUNCTION(assert, const Variant& assertion,
                             const Variant& message /* = null */) {
  if (!s_option_data->assertActive) return true;

  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_not_supported("assert()",
          "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 : empty_string_variant_ref);
    HHVM_FN(call_user_func)(s_option_data->assertCallback, ai.toArray());
  }
  if (s_option_data->assertException) {
    if (message.isObject()) {
      Object exn = message.toObject();
      if (exn.instanceof(SystemLib::s_AssertionErrorClass)) {
        throw_object(exn);
      }
    }

    SystemLib::throwExceptionObject(message.toString());
  }
  if (s_option_data->assertWarning) {
    String name(message.isNull() ? "Assertion" : message.toString());
    auto const str = !assertion.isString()
      ? " failed"
      : concat3(" \"",  assertion.toString(), "\" failed");
    raise_warning("assert(): %s%s", name.data(),  str.data());
  }
  if (s_option_data->assertBail) {
    throw ExitException(1);
  }

  return init_null();
}
Пример #2
0
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();
}
Пример #3
0
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();
}