Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evaluate calls in the current frame #168

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion inst/include/cpp11/function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@
#include "cpp11/sexp.hpp" // for sexp

namespace cpp11 {
namespace internal {
inline SEXP new_function(SEXP formals, SEXP body, SEXP env) {
SEXP fn = Rf_allocSExp(CLOSXP);
SET_FORMALS(fn, formals);
SET_BODY(fn, body);
SET_CLOENV(fn, env);
return fn;
}

inline sexp construct_current_frame_call() {
sexp body(safe[Rf_lang2](Rf_install("sys.frame"), Rf_ScalarInteger(-1)));
sexp fn = new_function(R_NilValue, body, R_BaseEnv);

return Rf_lang1(fn);
}

static sexp current_frame_call = construct_current_frame_call();

inline SEXP peek_frame() { return Rf_eval(current_frame_call, R_EmptyEnv); }

} // namespace internal

class function {
public:
Expand All @@ -26,7 +47,9 @@ class function {

construct_call(call, data_, std::forward<Args>(args)...);

return safe[Rf_eval](call, R_GlobalEnv);
SEXP env = internal::peek_frame();

return safe[Rf_eval](call, env);
}

private:
Expand Down