39 Function_Impl(SEXP x){
47 const char* fmt =
"Cannot convert object to a function: "
48 "[type=%s; target=CLOSXP, SPECIALSXP, or "
50 throw not_compatible(fmt, Rf_type2char(TYPEOF(x)));
61 Function_Impl(
const std::string& name) {
62 get_function(name, R_GlobalEnv);
65 Function_Impl(
const std::string& name,
const SEXP env) {
66 if (!Rf_isEnvironment(env)) {
67 stop(
"env is not an environment");
69 get_function(name, env);
72 Function_Impl(
const std::string& name,
const std::string& ns) {
73#if R_VERSION < R_Version(4,5,0)
75 Shield<SEXP> env(Rf_findVarInFrame(R_NamespaceRegistry, Rf_install(ns.c_str())));
76 if (env == R_UnboundValue)
77 stop(
"there is no namespace called \"%s\"", ns);
78#elif R_VERSION < R_Version(4,6,0) || R_SVN_REVISION < 89746
81 Shield<SEXP> env(R_getVarEx(Rf_install(ns.c_str()), R_NamespaceRegistry, FALSE, R_NilValue));
82 if (env == R_NilValue)
83 stop(
"there is no namespace called \"%s\"", ns);
87 if (env == R_NilValue)
88 stop(
"there is no namespace called \"%s\"", ns);
90 get_function(name, env);
93 SEXP operator()()
const {
98 template <
typename... T>
99 SEXP operator()(
const T&... args)
const {
100 return invoke(
pairlist(args...), R_GlobalEnv);
106 SEXP environment()
const {
107 SEXP fun = Storage::get__() ;
108 if( TYPEOF(fun) != CLOSXP ) {
109 throw not_a_closure(Rf_type2char(TYPEOF(fun)));
111 #if (defined(R_VERSION) && R_VERSION >= R_Version(4,5,0))
112 return R_ClosureEnv(fun);
122 return BODY( Storage::get__() ) ;
129 void get_function(
const std::string& name,
const SEXP env) {
130 SEXP nameSym = Rf_install( name.c_str() );
135 SEXP invoke(SEXP args_, SEXP env)
const {