Re: extending R API with support for contexts

Karl Millar

I think we need to be careful here if we want to maintain implementation flexibility.  IIRC, we've seen packages break due to incompatibilities between the way the AST interpreter and bytecode interpreter handle contexts.  In particular, this could cause issues with function inlining etc.

Changes I'd suggest:
  - Only function contexts are ever visible in the API.  R_getParentContext silently skips non-function contexts.
  - This should be placed in Rinterface.h, with clear comments stating that it's only intended for building interactive interfaces to R, and that code should not rely on the results having any particular values.
 - R_getContextSrcRef's documentation should state that it returns the srcref 'if available' and returns R_NilValue otherwise.
 - Rename R_getGlobal* to R_getCurrent* or R_getActive* for clarity
 - Top level context and global context aren't quite the same thing (the global context is a top level context, but not all top level contexts are the global context).  Which one(s) do we need to expose in the API?



On Mon, Jan 30, 2017 at 5:15 AM, Lukas Stadler <lukas.stadler@...> wrote:

I'd like to propose an extension of the native API to expose some information about context objects.
Although you could consider them to be implementation details of R, you need at least some introspection to be able to write proper IDEs.
It should be possible, e.g., to write a tool like RStudio without copy-and-paste from internal header files.

I'd like to propose something along the lines of:
typedef void *RCNTXT;

// returns a handle to the current context object
RCNTXT R_getGlobalContext();

// returns the closest enclosing function context (even if, say, in the browser context) or NULL if no functions are being executed
RCNTXT R_getGlobalFunctionContext();

// returns caller context of a given context or NULL if there is no caller
RCNTXT R_getParentContext(RCNTXT c);

// returns environment of a given context
SEXP R_getContextEnvironment(RCNTXT c);

// return function of a given (function) context or NULL if given context is not function context
SEXP R_getContextFunction(RCNTXT c);

// returns a call site (language object) of a given context
SEXP R_getContextCall(RCNTXT c);

// returns a source ref of a given context
SEXP R_getContextSrcRef(RCNTXT c);

// are we currently debugging via browser function?
bool R_insideBrowser();

// is this a top level context
bool R_isGlobal(RCNTXT c);

// compares two context objects
bool R_isEqual(RCNTXT x, RCNTXT y);

Do you think that makes sense?
Any reason not to expose this information?
Were there previous attempts to add such an interface that I'm not aware of?


Rconsortium-wg-api mailing list

Join to automatically receive all group messages.