SEXP RDebugger::doBegin()

in src/debugger/RDebugger.cpp [337:406]


SEXP RDebugger::doBegin(SEXP call, SEXP op, SEXP args, SEXP rho) {
  SEXP s = R_NilValue;
  RContext* ctx = getCurrentCallContext();
  SEXP function = R_NilValue;
  SEXP functionSrcref = R_NilValue;
  if (ctx != nullptr) {
    function = getFunction(ctx);
    CPP_BEGIN
    functionSrcref = sourceFileManager.getFunctionSrcref(function, [&] { return getCallFunctionName(getCall(ctx)); });
    CPP_END_VOID
  }
  if (Rf_getAttrib(functionSrcref, RI->doNotStopRecursiveFlag) != R_NilValue) {
    disable();
    RI->onExit.invokeUnsafeInEnv(rho, RI->jetbrainsDebuggerEnable.lang());
    return rDebugger.defaultDoBegin(call, op, args, rho);
  }
  if (Rf_getAttrib(functionSrcref, RI->doNotStopFlag) != R_NilValue) {
    return rDebugger.defaultDoBegin(call, op, args, rho);
  }

  bool stopOnFirst = false;
  if (BODY_EXPR(function) == call) {
    if (Rf_getAttrib(call, RI->functionDebugOnceFlag) != R_NilValue) {
      SET_BODY(function, Rf_duplicate(call));
      Rf_setAttrib(BODY_EXPR(function), RI->functionDebugOnceFlag, R_NilValue);
      removeBlockBodyIfNotNeeded(function);
      stopOnFirst = true;
    } else {
      stopOnFirst = Rf_getAttrib(call, RI->functionDebugFlag) != R_NilValue;
    }
    if (stopOnFirst && args == R_NilValue) {
      PROTECT(R_Srcref = getSrcref(getBlockSrcrefs(call), 0));
      CPP_BEGIN
        rDebugger.sendDebugPrompt(R_NilValue);
      CPP_END_VOID
      UNPROTECT(1);
      return R_NilValue;
    }
  }

  if (Rf_getAttrib(call, RI->generatedBlockFlag) != R_NilValue && Rf_length(call) == 2) {
    PROTECT(R_Srcref = getSrcref(getBlockSrcrefs(call), 1));
    if (stopOnFirst) {
      CPP_BEGIN
        rDebugger.sendDebugPrompt(CADR(call));
      CPP_END_VOID
    }
    s = Rf_eval(CADR(call), rho);
    UNPROTECT(1);
    R_Srcref = R_NilValue;
    return s;
  }

  if (args != R_NilValue) {
    SourceFileManager::preprocessSrcrefs(call);
    SEXP srcrefs = getBlockSrcrefs(call);
    PROTECT(srcrefs);
    int i = 1;
    while (args != R_NilValue) {
      PROTECT(R_Srcref = getSrcref(srcrefs, i++));
      s = doStep(CAR(args), rho, R_Srcref, stopOnFirst);
      stopOnFirst = false;
      UNPROTECT(1);
      args = CDR(args);
    }
    R_Srcref = R_NilValue;
    UNPROTECT(1);
  }
  return s;
}