Ejemplo n.º 1
0
bool ExecutionContext::obFlush() {
  ASSERT(m_protectedLevel >= 0);
  if ((int)m_buffers.size() > m_protectedLevel) {
    list<OutputBuffer*>::const_iterator iter = m_buffers.end();
    OutputBuffer *last = *(--iter);
    if (iter != m_buffers.begin()) {
      OutputBuffer *prev = *(--iter);
      if (last->handler.isNull()) {
        prev->oss << last->oss.str();
      } else {
        try {
          string output = last->oss.str();
          String sout(output.data(), output.length(), AttachLiteral);
          Variant tout =
            f_call_user_func_array(last->handler, CREATE_VECTOR1(sout));
          sout = tout.toString();
          prev->oss << string(sout.data(), sout.size());
        } catch (...) {
          prev->oss << last->oss.str();
        }
      }
      last->oss.str("");
      return true;
    }
    cout << last->oss.str();
    last->oss.str("");
  }
  return false;
}
Ejemplo n.º 2
0
bool BaseExecutionContext::onUnhandledException(Object e) {
  String err = e.toString();
  if (RuntimeOption::AlwaysLogUnhandledExceptions) {
    Logger::Error("HipHop Fatal error: Uncaught %s", err.data());
  }

  if (e.instanceof("Exception")) {
    // user thrown exception
    if (!m_userExceptionHandlers.empty()) {
      if (!same(f_call_user_func_array
                (m_userExceptionHandlers.back(),
                 CREATE_VECTOR1(e)),
                false)) {
        return true;
      }
    }
  } else {
    ASSERT(false);
  }
  m_lastError = err;

  if (!RuntimeOption::AlwaysLogUnhandledExceptions) {
    Logger::Error("HipHop Fatal error: Uncaught %s", err.data());
  }
  return false;
}
Ejemplo n.º 3
0
void EventHook::RunUserProfiler(const ActRec* ar, int mode) {
  // Don't do anything if we are running the profiling function itself
  // or if we haven't set up a profiler.
  if (g_vmContext->m_executingSetprofileCallback ||
      g_vmContext->m_setprofileCallback.isNull()) {
    return;
  }
  Transl::VMRegAnchor _;
  ExecutingSetprofileCallbackGuard guard;

  Array params;
  Array frameinfo;

  if (mode == ProfileEnter) {
    params.append(s_enter);
    frameinfo.set(s_args, hhvm_get_frame_args(ar));
  } else {
    params.append(s_exit);
    if (!g_vmContext->m_faults.empty()) {
      Fault fault = g_vmContext->m_faults.back();
      if (fault.m_faultType == Fault::UserException) {
        frameinfo.set(s_exception, fault.m_userException);
      }
    } else if (!ar->m_func->isBuiltin() &&
               !ar->m_func->isGenerator()) {
      // TODO (#1131400) This is wrong for builtins
      frameinfo.set(s_return, tvAsCVarRef(g_vmContext->m_stack.topTV()));
    }
  }

  params.append(VarNR(ar->m_func->fullName()));
  params.append(frameinfo);

  f_call_user_func_array(g_vmContext->m_setprofileCallback, params);
}
Ejemplo n.º 4
0
static void sqlite3_do_callback(sqlite3_context *context, CVarRef callback,
                                int argc, sqlite3_value **argv, bool is_agg) {
  Array params = Array::Create();
  php_sqlite3_agg_context *agg_context = NULL;
  if (is_agg) {
    agg_context = (php_sqlite3_agg_context *)sqlite3_aggregate_context
      (context, sizeof(php_sqlite3_agg_context));
    params.append(ref(agg_context->context));
    params.append(agg_context->row_count);
  }
  for (int i = 0; i < argc; i++) {
    params.append(get_value(argv[i]));
  }
  Variant ret = f_call_user_func_array(callback, params);

  if (!is_agg || !argv) {
    /* only set the sqlite return value if we are a scalar function,
     * or if we are finalizing an aggregate */
    if (ret.isInteger()) {
      sqlite3_result_int(context, ret.toInt64());
    } else if (ret.isNull()) {
      sqlite3_result_null(context);
    } else if (ret.isDouble()) {
      sqlite3_result_double(context, ret.toDouble());
    } else {
      String sret = ret.toString();
      sqlite3_result_text(context, sret.data(), sret.size(), SQLITE_TRANSIENT);
    }
  } else {
    /* we're stepping in an aggregate; the return value goes into
     * the context */
    agg_context->context = ret;
  }
}
                // The default operator called from parallel_for
                void operator() (const tbb::blocked_range<int64> &range) const {
                		callerContext.EnteringOperator();

                        // Build arguments array
                        Array args = Array::Create();

                        // Pass the range start and end as parameters
                        args.append(range.begin());
                        args.append(range.end());

                        // If an input array is defined, copy it and pass as a parameter
                        if(inputArrayOfVariant.size() > 0) {
                        	Array inputPHPArray = Array::Create();
                        	for(size_t ai=0; ai<inputArrayOfVariant.size(); ai++)
                        		inputPHPArray.append(inputArrayOfVariant[ai]);

                        	Variant inputArrayArg(inputPHPArray.getArrayData());
                        	args.append(inputArrayArg);
                        }

                        // Call user supplied callback with arguments
                        // This is a PHP function of the form worker($begin, $end, $array), returning an array or nothing
                        // If an array is returned, it is expected to have elements which will be comingled into the output
                        // array in the elements defined by the input range.
                        Variant vres = f_call_user_func_array(this->callback, args);

                		callerContext.ExitingOperator();		// Call this straight after the callback completes

                		// Return if no result to pass back
                        if(vres.isNull() || !vres.isArray())
                        	return;

                        // Now we take the output array [0..N) and assign it into the overall output array at [begin..begin+N)

                        // Extract output array from result variant
                    	const Array aOutputArray = vres.asCArrRef();
                    	ArrayData *pdOutputArray = aOutputArray.getArrayData();

                    	Variant v = pdOutputArray->reset();

                    	// Check the output array is the same size or smaller than the range passed
                    	size_t outIdx = range.begin();
                    	if(pdOutputArray->size() > (range.end()-range.begin())) {
                    		raise_warning("Callback function returned array larger than passed range size");
                    		return;
                    	}

                    	// Copy each row
                    	while(!v.isBoolean() || v.toBoolean())
                    	{
                        	// printf(" outIdx=%d, v=%s\n", (int)outIdx, v.toString().c_str());
                        	( *pOutputArrayOfVariant ) [outIdx++] = v;

                        	v = pdOutputArray->next();
                    	}

                }
Ejemplo n.º 6
0
String f_call_user_func_serialized(CStrRef input) {
  Variant out;
  try {
    Variant in = f_unserialize(input);
    out.set("ret", f_call_user_func_array(in["func"], in["args"]));
  } catch (Object &e) {
    out.set("exception", e);
  }
  return f_serialize(out);
}
Ejemplo n.º 7
0
bool BaseExecutionContext::obFlush() {
  ASSERT(m_protectedLevel >= 0);
  if ((int)m_buffers.size() > m_protectedLevel) {
    std::list<OutputBuffer*>::const_iterator iter = m_buffers.end();
    OutputBuffer *last = *(--iter);
    const int flag = PHP_OUTPUT_HANDLER_START | PHP_OUTPUT_HANDLER_END;
    if (iter != m_buffers.begin()) {
      OutputBuffer *prev = *(--iter);
      if (last->handler.isNull()) {
        prev->oss.absorb(last->oss);
      } else {
        try {
          Variant tout =
            f_call_user_func_array(last->handler,
                                   CREATE_VECTOR2(last->oss.detach(), flag));
          prev->oss.append(tout.toString());
          last->oss.reset();
        } catch (...) {
          prev->oss.absorb(last->oss);
        }
      }
      return true;
    }

    if (!last->handler.isNull()) {
      try {
        Variant tout =
          f_call_user_func_array(last->handler,
                                 CREATE_VECTOR2(last->oss.detach(), flag));
        String sout = tout.toString();
        writeStdout(sout.data(), sout.size());
        last->oss.reset();
        return true;
      } catch (...) {}
    }

    writeStdout(last->oss.data(), last->oss.size());
    last->oss.reset();
    return true;
  }
  return false;
}
Ejemplo n.º 8
0
bool f_pcntl_signal_dispatch() {
  int *signaled = s_signal_handlers->signaled;
  for (int i = 0; i < _NSIG; i++) {
    if (signaled[i]) {
      signaled[i] = 0;
      if (s_signal_handlers->handlers.exists(i)) {
        f_call_user_func_array(s_signal_handlers->handlers[i],
                               CREATE_VECTOR1(i));
      }
    }
  }
  return true;
}
Ejemplo n.º 9
0
Variant f_forward_static_call(int _argc, CVarRef function, CArrRef _argv /* = null_array */) {
#ifdef ENABLE_LATE_STATIC_BINDING
  CStrRef cls = FrameInjection::GetClassName();
  if (cls.empty()) {
    raise_error("Cannot call forward_static_call() "
                "when no class scope is active");
    return null;
  }
  FrameInjection::StaticClassNameHelper h(ThreadInfo::s_threadInfo.get(), cls);
  return f_call_user_func_array(function, _argv, true);
#else
  throw NotSupportedException(__func__, "ENABLE_LATE_STATIC_BINDING is off");
#endif
}
Ejemplo n.º 10
0
 Variant do_callback(CVarRef cb, CArrRef args) {
   ASSERT(!m_exception);
   try {
     return f_call_user_func_array(cb, args);
   } catch (Object &e) {
     ObjectData *od = e.get();
     od->incRefCount();
     m_exception = od;
     m_phpException = true;
   } catch (Exception &e) {
     m_exception = e.clone();
     m_phpException = false;
   }
   return null;
 }
Ejemplo n.º 11
0
Variant f_iterator_apply(CVarRef obj, CVarRef func,
                         CArrRef params /* = null_array */) {
  if (!obj.instanceof("Traversable")) {
    return false;
  }
  Object pobj = obj.toObject();
  pobj->o_invoke("rewind", null_array, -1);
  int64 count = 0;
  while (same(pobj->o_invoke("valid", null_array, -1), true)) {
    if (!same(f_call_user_func_array(func, params), true)) {
      break;
    }
    ++count;
    pobj->o_invoke("next", null_array, -1);
  }
  return count;
}
Ejemplo n.º 12
0
bool ExecutionContext::callUserErrorHandler(const Exception &e, int64 errnum,
                                            bool swallowExceptions) {
  int errline = 0;
  String errfile;
  Array backtrace;
  const ExtendedException *ee = dynamic_cast<const ExtendedException*>(&e);
  if (ee) {
    ArrayPtr arr = ee->getBackTrace();
    if (arr) {
      backtrace = *arr;
      Array top = backtrace.rvalAt(0);
      if (!top.isNull()) {
        errfile = top.rvalAt("file");
        errline = top.rvalAt("line").toInt64();
      }
    }
  }
  if (backtrace.isNull()) {
    backtrace = stackTraceToBackTrace(e.getStackTrace());
  }

  bool retval = false;
  RequestData::Data *data = s_request_data->data;
  if (!data->systemExceptionHandlers.empty()) {
    // Avoid calling the user error handler recursively
    if (!data->insideUserHandler) {
      data->insideUserHandler = true;
      try {
        if (!same(f_call_user_func_array
                  (data->systemExceptionHandlers.back(),
                   CREATE_VECTOR6(errnum, String(e.getMessage()), errfile,
                                  errline, "", backtrace)),
                  false)) {
          retval = true;
        }
      } catch (...) {
        data->insideUserHandler = false;
        if (!swallowExceptions) throw;
      }
      data->insideUserHandler = false;
    }
  }

  return retval;
}
Ejemplo n.º 13
0
bool BaseExecutionContext::callUserErrorHandler(const Exception &e, int errnum,
                                                bool swallowExceptions) {
  switch (getErrorState()) {
  case ExecutingUserHandler:
  case ErrorRaisedByUserHandler:
    return false;
  default:
    break;
  }
  if (!m_userErrorHandlers.empty() &&
      (m_userErrorHandlers.back().second & errnum) != 0) {
    int errline = 0;
    String errfile;
    Array backtrace;
    const ExtendedException *ee = dynamic_cast<const ExtendedException*>(&e);
    if (ee) {
      ArrayPtr arr = ee->getBackTrace();
      if (arr) {
        backtrace = *arr;
        Array top = backtrace.rvalAt(0);
        if (!top.isNull()) {
          errfile = top.rvalAt("file");
          errline = top.rvalAt("line").toInt64();
        }
      }
    }
    if (backtrace.isNull()) {
      backtrace = stackTraceToBackTrace(e.getStackTrace());
    }
    try {
      ErrorStateHelper esh(this, ExecutingUserHandler);
      if (!same(f_call_user_func_array
                (m_userErrorHandlers.back().first,
                 CREATE_VECTOR6(errnum, String(e.getMessage()), errfile,
                                errline, "", backtrace)),
                false)) {
        return true;
      }
    } catch (...) {
      if (!swallowExceptions) throw;
    }
  }
  return false;
}
Ejemplo n.º 14
0
void ExecutionContext::onUnhandledException(Object e) {
  String err = e.toString();
  if (RuntimeOption::AlwaysLogUnhandledExceptions) {
    Logger::Error("HipHop Fatal error: Uncaught exception %s", err.data());
  }

  RequestData::Data *data = s_request_data->data;
  if (e.instanceof("exception")) {
    // user thrown exception
    if (!data->userExceptionHandlers.empty()) {
      f_call_user_func_array(data->userExceptionHandlers.back(),
                             CREATE_VECTOR1(e));
      return;
    }
  } else {
    ASSERT(false);
  }
  data->lastError = err;

  if (!RuntimeOption::AlwaysLogUnhandledExceptions) {
    Logger::Error("HipHop Fatal error: Uncaught exception: %s", err.data());
  }
}
Ejemplo n.º 15
0
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 str = v.toString();
    int c = str.find("::");
    if (c != 0 && c != String::npos && c + 2 < str.size()) {
      String classname = str.substr(0, c);
      String methodname = str.substr(c + 2);
      return f_call_user_func_array(s_class_exists,
                                    Array::Create(classname)) &&
        ClassInfo::HasAccess(classname, methodname, true, false);
    }
    return f_function_exists(str);
  }

  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_array(s_class_exists, Array::Create(v0))) {
          return false;
        }
        staticCall = true;
      }
      if (v1.isString()) {
        String str = v1.toString();
        int c = str.find("::");
        if (c != 0 && c != String::npos && c + 2 < str.size()) {
          String name1 = v0.toString();
          String name2 = str.substr(0, c);
          ASSERT(name1.get() && name2.get());
          if (name1->isame(name2.get()) ||
              ClassInfo::IsSubClass(name1, name2, false)) {
            staticCall = true;
            v0 = name2;
            v1 = str.substr(c + 2);
          }
        }
      }
      if (v0.isString() && v1.isString()) {
        if (!name.isNull()) {
          name = v0.toString() + "::" + v1.toString();
        }
        if (same(v0, s_self) || same(v0, s_parent)) {
          throw NotImplementedException("augmenting class scope info");
        }
        return ClassInfo::HasAccess(v0, v1, staticCall, !obj.isNull());
      }
    }
  }

  if (!name.isNull()) {
    name = v.toString();
  }
  return false;
}
Ejemplo n.º 16
0
void BaseExecutionContext::executeFunctions(CArrRef funcs) {
  for (ArrayIter iter(funcs); iter; ++iter) {
    Array callback = iter.second();
    f_call_user_func_array(callback["name"], callback["args"]);
  }
}
Ejemplo n.º 17
0
void AsioSession::OnFailed(CObjRef exception) {
  ObjectData* cb = s_on_failed_cb.get();
  if (cb) {
    f_call_user_func_array(cb, Array::Create(exception));
  }
}
Ejemplo n.º 18
0
Variant f_call_user_func(int _argc, CVarRef function, CArrRef _argv /* = null_array */) {
  return f_call_user_func_array(function, _argv);
}
Ejemplo n.º 19
0
static int cmp_func(CVarRef v1, CVarRef v2, const void *data) {
  Variant *callback = (Variant *)data;
  return f_call_user_func_array(*callback, CREATE_VECTOR2(v1, v2));
}