Skip to content

Instantly share code, notes, and snippets.

@romainfrancois
Created September 22, 2014 10:02
Show Gist options
  • Save romainfrancois/a5c078bbddfbac8533ae to your computer and use it in GitHub Desktop.
Save romainfrancois/a5c078bbddfbac8533ae to your computer and use it in GitHub Desktop.
get some forbidden C/R API fruits
#include <Rinternals.h>
#include <Rinterface.h>
extern void* R_GlobalContext ;
#define R_NO_REMAP
#define USE_RINTERNALS
#include <R.h>
#include <Rinternals.h>
#include <R_ext/Complex.h>
#include <R_ext/Parse.h>
#include <R_ext/Rdynload.h>
#include <R_ext/Callbacks.h>
#include <Rversion.h>
typedef SEXP (*CCODE)(SEXP, SEXP, SEXP, SEXP);
/* Information for Deparsing Expressions */
typedef enum {
PP_INVALID = 0,
PP_ASSIGN = 1,
PP_ASSIGN2 = 2,
PP_BINARY = 3,
PP_BINARY2 = 4,
PP_BREAK = 5,
PP_CURLY = 6,
PP_FOR = 7,
PP_FUNCALL = 8,
PP_FUNCTION = 9,
PP_IF = 10,
PP_NEXT = 11,
PP_PAREN = 12,
PP_RETURN = 13,
PP_SUBASS = 14,
PP_SUBSET = 15,
PP_WHILE = 16,
PP_UNARY = 17,
PP_DOLLAR = 18,
PP_FOREIGN = 19,
PP_REPEAT = 20
} PPkind;
typedef enum {
PREC_FN = 0,
PREC_LEFT = 1,
PREC_EQ = 2,
PREC_RIGHT = 3,
PREC_TILDE = 4,
PREC_OR = 5,
PREC_AND = 6,
PREC_NOT = 7,
PREC_COMPARE = 8,
PREC_SUM = 9,
PREC_PROD = 10,
PREC_PERCENT = 11,
PREC_COLON = 12,
PREC_SIGN = 13,
PREC_POWER = 14,
PREC_DOLLAR = 15,
PREC_NS = 16,
PREC_SUBSET = 17
} PPprec;
typedef struct {
PPkind kind; /* deparse kind */
PPprec precedence; /* operator precedence */
unsigned int rightassoc; /* right associative? */
} PPinfo;
typedef struct {
char *name; /* print name */
CCODE cfun; /* c-code address */
int code; /* offset within c-code */
int eval; /* evaluate args? */
int arity; /* function arity */
PPinfo gram; /* pretty-print info */
} FUNTAB;
extern FUNTAB R_FunTab[]; /* Built in functions */
SEXP get_forbidden_fruits(){
SEXP res = PROTECT(allocVector(VECSXP,2)) ;
// first : R_GlobalContext
SEXP global_context = PROTECT(R_MakeExternalPtr(R_GlobalContext,R_NilValue,R_NilValue)) ;
SET_VECTOR_ELT(res,0,global_context) ;
// then R_FunTab
SEXP fun_tab = PROTECT(R_MakeExternalPtr(R_FunTab,R_NilValue,R_NilValue)) ;
SET_VECTOR_ELT(res,1,fun_tab) ;
UNPROTECT(3) ;
return res ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment