passing unevaluated expressions to C/C++

前端 未结 1 1494
囚心锁ツ
囚心锁ツ 2020-12-10 03:18

I\'d like to pass a variable number of arguments from a function to C/C++, but would like to leave the arguments unevaluated and at the same time don\'t want to do any compu

相关标签:
1条回答
  • 2020-12-10 04:09

    One possibility is to do what match.call does (thanks to Ricardo Saporta for pointing me in that direction). This requires copy-pasting a few definitions from R source code that I won't do here, but the basic idea is to get the calling function from R_GlobalContext and then extract the function arguments from there. The rough sketch is as follows:

    R_fn = function(...) .Call("cpp_fn")
    
    // and in C++ code
    Language cpp_fn() {
      SEXP sysp = ((RCNTXT*)R_GlobalContext)->sysparent;
      RCNTXT *cptr = (RCNTXT*)R_GlobalContext;
    
      while (cptr != NULL) {
        if (cptr->callflag & CTXT_FUNCTION && cptr->cloenv == sysp)
          break;
        cptr = cptr->nextcontext;
      }
      cptr = cptr->nextcontext; // because this is called from .Call and not from R_fn
    
      // and now cptr->promargs has the unevaluated arguments to do as one pleases
      // e.g.
      Language firstArg(R_PromiseExpr(CAR(cptr->promargs)));
    
      return firstArg;
    }
    
    0 讨论(0)
提交回复
热议问题