1# This is the enclosing environment for all of the functions involved in
2# instantiating objects. It is also the binding environment for all these
3# functions, except for R6Class(). This is because a generator object can be
4# saved (in a built package, for example) and then restored in a different R
5# session which has a different version of the R6 package. With the capsule
6# environment, the generator object doesn't need to use any functions or objects
7# from the potentially different R6 namespace, and because the saved/restored
8# object also saves and restores the capsule environment (but not the R6
9# namespace).
10capsule <- new.env(hash = FALSE)
11attr(capsule, "name") <- "R6_capsule"
12
13# This function takes an expression and evaluates it in the capsule environment.
14encapsulate <- function(expr) {
15  expr <- substitute(expr)
16  eval(expr, capsule)
17}
18
19
20# This list contains functions that are copied to the generator environment and
21# are assigned as the generator env as their enclosing environment.
22# This is simpler than encapsulate, because these functions don't need to be
23# enclosed in a special environment now; when a class is created, they will be
24# copied into the generator environment and assigned it as their enclosing env.
25generator_funs <- list()
26