Skip to content

Commit

Permalink
Add support for context entry, exit and jump callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
aviralg committed Feb 19, 2021
1 parent 4bd52d1 commit eb80545
Show file tree
Hide file tree
Showing 24 changed files with 810 additions and 15 deletions.
27 changes: 27 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ S3method(create_closure_call_entry_callback,"function")
S3method(create_closure_call_entry_callback,externalptr)
S3method(create_closure_call_exit_callback,"function")
S3method(create_closure_call_exit_callback,externalptr)
S3method(create_context_entry_callback,"function")
S3method(create_context_entry_callback,externalptr)
S3method(create_context_exit_callback,"function")
S3method(create_context_exit_callback,externalptr)
S3method(create_context_jump_callback,"function")
S3method(create_context_jump_callback,externalptr)
S3method(create_eval_entry_callback,"function")
S3method(create_eval_entry_callback,externalptr)
S3method(create_eval_exit_callback,"function")
Expand Down Expand Up @@ -78,6 +84,9 @@ S3method(get_caller,instrumentr_call)
S3method(get_closure_call_entry_callback,instrumentr_context)
S3method(get_closure_call_exit_callback,instrumentr_context)
S3method(get_code,instrumentr_application)
S3method(get_context_entry_callback,instrumentr_context)
S3method(get_context_exit_callback,instrumentr_context)
S3method(get_context_jump_callback,instrumentr_context)
S3method(get_data,instrumentr_object)
S3method(get_default_argument,instrumentr_function)
S3method(get_definition,instrumentr_function)
Expand Down Expand Up @@ -144,6 +153,9 @@ S3method(has_call_entry_callback,instrumentr_context)
S3method(has_call_exit_callback,instrumentr_context)
S3method(has_closure_call_entry_callback,instrumentr_context)
S3method(has_closure_call_exit_callback,instrumentr_context)
S3method(has_context_entry_callback,instrumentr_context)
S3method(has_context_exit_callback,instrumentr_context)
S3method(has_context_jump_callback,instrumentr_context)
S3method(has_data,instrumentr_object)
S3method(has_eval_entry_callback,instrumentr_context)
S3method(has_eval_exit_callback,instrumentr_context)
Expand Down Expand Up @@ -203,6 +215,9 @@ S3method(set_call_entry_callback,instrumentr_context)
S3method(set_call_exit_callback,instrumentr_context)
S3method(set_closure_call_entry_callback,instrumentr_context)
S3method(set_closure_call_exit_callback,instrumentr_context)
S3method(set_context_entry_callback,instrumentr_context)
S3method(set_context_exit_callback,instrumentr_context)
S3method(set_context_jump_callback,instrumentr_context)
S3method(set_data,instrumentr_object)
S3method(set_eval_entry_callback,instrumentr_context)
S3method(set_eval_exit_callback,instrumentr_context)
Expand Down Expand Up @@ -258,6 +273,9 @@ export(create_call_exit_callback)
export(create_closure_call_entry_callback)
export(create_closure_call_exit_callback)
export(create_context)
export(create_context_entry_callback)
export(create_context_exit_callback)
export(create_context_jump_callback)
export(create_eval_entry_callback)
export(create_eval_exit_callback)
export(create_function)
Expand Down Expand Up @@ -297,6 +315,9 @@ export(get_caller)
export(get_closure_call_entry_callback)
export(get_closure_call_exit_callback)
export(get_code)
export(get_context_entry_callback)
export(get_context_exit_callback)
export(get_context_jump_callback)
export(get_data)
export(get_default_argument)
export(get_definition)
Expand Down Expand Up @@ -354,6 +375,9 @@ export(has_call_entry_callback)
export(has_call_exit_callback)
export(has_closure_call_entry_callback)
export(has_closure_call_exit_callback)
export(has_context_entry_callback)
export(has_context_exit_callback)
export(has_context_jump_callback)
export(has_data)
export(has_eval_entry_callback)
export(has_eval_exit_callback)
Expand Down Expand Up @@ -404,6 +428,9 @@ export(set_call_entry_callback)
export(set_call_exit_callback)
export(set_closure_call_entry_callback)
export(set_closure_call_exit_callback)
export(set_context_entry_callback)
export(set_context_exit_callback)
export(set_context_jump_callback)
export(set_data)
export(set_eval_entry_callback)
export(set_eval_exit_callback)
Expand Down
219 changes: 219 additions & 0 deletions R/callbacks.R
Original file line number Diff line number Diff line change
Expand Up @@ -2186,3 +2186,222 @@ create_variable_removal_callback.externalptr <- function(object) { # nolint
is_instrumentr_variable_removal_callback <- function(object) { # nolint
inherits(object, 'instrumentr_variable_removal_callback')
}


################################################################################
## context_entry
################################################################################

## GET #########################################################################

#' @export
get_context_entry_callback <- function(context, ...) {
UseMethod("get_context_entry_callback")
}

#' @export
get_context_entry_callback.instrumentr_context <- function(context, ...) { # nolint
.Call(C_context_get_context_entry_callback, context)
}

## SET #########################################################################

#' @export
set_context_entry_callback <- function(context, callback, ...) {
UseMethod("set_context_entry_callback")
}

#' @export
set_context_entry_callback.instrumentr_context <- function(context, callback, ...) { # nolint

if (!is_instrumentr_context_entry_callback(callback)) {
callback <- create_context_entry_callback(callback)
}

.Call(C_context_set_context_entry_callback, context, callback)

invisible(NULL)
}

## HAS #########################################################################

#' @export
has_context_entry_callback <- function(object, ...) { # nolint
UseMethod("has_context_entry_callback")
}

#' @export
has_context_entry_callback.instrumentr_context <- function(object, ...) { # nolint
.Call(C_context_has_context_entry_callback, object)
}

## CREATE ######################################################################

#' @export
create_context_entry_callback <- function(object) { # nolint
UseMethod("create_context_entry_callback")
}

#' @export
create_context_entry_callback.function <- function(object) { # nolint
stopifnot(is_closure(object) && has_parameters(object, 3))

.Call(C_context_entry_callback_create_from_r_function, object)
}

#' @export
create_context_entry_callback.externalptr <- function(object) { # nolint
.Call(C_context_entry_callback_create_from_c_function, object)
}

## IS ##########################################################################

is_instrumentr_context_entry_callback <- function(object) { # nolint
inherits(object, 'instrumentr_context_entry_callback')
}


################################################################################
## context_exit
################################################################################

## GET #########################################################################

#' @export
get_context_exit_callback <- function(context, ...) {
UseMethod("get_context_exit_callback")
}

#' @export
get_context_exit_callback.instrumentr_context <- function(context, ...) { # nolint
.Call(C_context_get_context_exit_callback, context)
}

## SET #########################################################################

#' @export
set_context_exit_callback <- function(context, callback, ...) {
UseMethod("set_context_exit_callback")
}

#' @export
set_context_exit_callback.instrumentr_context <- function(context, callback, ...) { # nolint

if (!is_instrumentr_context_exit_callback(callback)) {
callback <- create_context_exit_callback(callback)
}

.Call(C_context_set_context_exit_callback, context, callback)

invisible(NULL)
}

## HAS #########################################################################

#' @export
has_context_exit_callback <- function(object, ...) { # nolint
UseMethod("has_context_exit_callback")
}

#' @export
has_context_exit_callback.instrumentr_context <- function(object, ...) { # nolint
.Call(C_context_has_context_exit_callback, object)
}

## CREATE ######################################################################

#' @export
create_context_exit_callback <- function(object) { # nolint
UseMethod("create_context_exit_callback")
}

#' @export
create_context_exit_callback.function <- function(object) { # nolint
stopifnot(is_closure(object) && has_parameters(object, 3))

.Call(C_context_exit_callback_create_from_r_function, object)
}

#' @export
create_context_exit_callback.externalptr <- function(object) { # nolint
.Call(C_context_exit_callback_create_from_c_function, object)
}

## IS ##########################################################################

is_instrumentr_context_exit_callback <- function(object) { # nolint
inherits(object, 'instrumentr_context_exit_callback')
}


################################################################################
## context_jump
################################################################################

## GET #########################################################################

#' @export
get_context_jump_callback <- function(context, ...) {
UseMethod("get_context_jump_callback")
}

#' @export
get_context_jump_callback.instrumentr_context <- function(context, ...) { # nolint
.Call(C_context_get_context_jump_callback, context)
}

## SET #########################################################################

#' @export
set_context_jump_callback <- function(context, callback, ...) {
UseMethod("set_context_jump_callback")
}

#' @export
set_context_jump_callback.instrumentr_context <- function(context, callback, ...) { # nolint

if (!is_instrumentr_context_jump_callback(callback)) {
callback <- create_context_jump_callback(callback)
}

.Call(C_context_set_context_jump_callback, context, callback)

invisible(NULL)
}

## HAS #########################################################################

#' @export
has_context_jump_callback <- function(object, ...) { # nolint
UseMethod("has_context_jump_callback")
}

#' @export
has_context_jump_callback.instrumentr_context <- function(object, ...) { # nolint
.Call(C_context_has_context_jump_callback, object)
}

## CREATE ######################################################################

#' @export
create_context_jump_callback <- function(object) { # nolint
UseMethod("create_context_jump_callback")
}

#' @export
create_context_jump_callback.function <- function(object) { # nolint
stopifnot(is_closure(object) && has_parameters(object, 3))

.Call(C_context_jump_callback_create_from_r_function, object)
}

#' @export
create_context_jump_callback.externalptr <- function(object) { # nolint
.Call(C_context_jump_callback_create_from_c_function, object)
}

## IS ##########################################################################

is_instrumentr_context_jump_callback <- function(object) { # nolint
inherits(object, 'instrumentr_context_jump_callback')
}
15 changes: 15 additions & 0 deletions R/create_context.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ create_context <- function(application_load_callback, # nolint
variable_assignment_callback,
variable_removal_callback,
variable_lookup_callback,
context_entry_callback,
context_exit_callback,
context_jump_callback,
packages = character(0),
functions = character(0)) {

Expand Down Expand Up @@ -165,5 +168,17 @@ create_context <- function(application_load_callback, # nolint
set_variable_lookup_callback(context, variable_lookup_callback)
}

if (!missing(context_entry_callback)) {
set_context_entry_callback(context, context_entry_callback)
}

if (!missing(context_exit_callback)) {
set_context_exit_callback(context, context_exit_callback)
}

if (!missing(context_jump_callback)) {
set_context_jump_callback(context, context_jump_callback)
}

context
}
5 changes: 4 additions & 1 deletion inst/include/Callback.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ class Callback: public Object {
VariableDefinition,
VariableAssignment,
VariableRemoval,
VariableLookup
VariableLookup,
ContextEntry,
ContextExit,
ContextJump
};

Callback(Type type, void* function, bool is_r_function);
Expand Down
15 changes: 13 additions & 2 deletions inst/include/Context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
#include "ObjectDuplicateCallback.hpp"
#include "VectorCopyCallback.hpp"
#include "MatrixCopyCallback.hpp"
#include "ContextEntryCallback.hpp"
#include "ContextExitCallback.hpp"
#include "ContextJumpCallback.hpp"
#include <unordered_map>
#include <unordered_set>
#include <vector>
Expand Down Expand Up @@ -67,15 +70,14 @@ class Context: public Object {
#define INSTRUMENTR_GENERATE_CALLBACK_PREDICATE(CALLBACK_CLASS, \
CALLBACK_VARIABLE) \
bool has_##CALLBACK_VARIABLE() const { \
return (bool) ( CALLBACK_VARIABLE##_ ); \
return (bool) (CALLBACK_VARIABLE##_); \
}

#define INSTRUMENTR_GENERATE_CALLBACK_API(CALLBACK_CLASS, CALLBACK_VARIABLE) \
INSTRUMENTR_GENERATE_CALLBACK_GETTER(CALLBACK_CLASS, CALLBACK_VARIABLE) \
INSTRUMENTR_GENERATE_CALLBACK_SETTER(CALLBACK_CLASS, CALLBACK_VARIABLE) \
INSTRUMENTR_GENERATE_CALLBACK_PREDICATE(CALLBACK_CLASS, CALLBACK_VARIABLE)


INSTRUMENTR_GENERATE_CALLBACK_API(ApplicationLoadCallback,
application_load_callback)
INSTRUMENTR_GENERATE_CALLBACK_API(ApplicationUnloadCallback,
Expand Down Expand Up @@ -129,6 +131,12 @@ class Context: public Object {
variable_removal_callback)
INSTRUMENTR_GENERATE_CALLBACK_API(VariableLookupCallback,
variable_lookup_callback)
INSTRUMENTR_GENERATE_CALLBACK_API(ContextEntryCallback,
context_entry_callback)
INSTRUMENTR_GENERATE_CALLBACK_API(ContextExitCallback,
context_exit_callback)
INSTRUMENTR_GENERATE_CALLBACK_API(ContextJumpCallback,
context_jump_callback)

void set_environment(SEXP r_environment) {
R_ReleaseObject(r_environment_);
Expand Down Expand Up @@ -314,6 +322,9 @@ class Context: public Object {
VariableAssignmentCallbackSPtr variable_assignment_callback_;
VariableRemovalCallbackSPtr variable_removal_callback_;
VariableLookupCallbackSPtr variable_lookup_callback_;
ContextEntryCallbackSPtr context_entry_callback_;
ContextExitCallbackSPtr context_exit_callback_;
ContextJumpCallbackSPtr context_jump_callback_;
ApplicationSPtr application_;
SEXP r_environment_;
std::unordered_map<std::string, std::unordered_set<std::string>> packages_;
Expand Down
Loading

0 comments on commit eb80545

Please sign in to comment.