extending R API with support for contexts

Lukas Stadler


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?


Join Rconsortium-wg-api@lists.r-consortium.org to automatically receive all group messages.