1
- new_constructor <- function (parent , properties ) {
1
+ new_constructor <- function (parent , properties ,
2
+ envir = asNamespace(" S7" ), package = NULL ) {
2
3
properties <- as_properties(properties )
3
- arg_info <- constructor_args(parent , properties )
4
+ arg_info <- constructor_args(parent , properties , envir , package )
4
5
self_args <- as_names(names(arg_info $ self ), named = TRUE )
5
6
6
7
if (identical(parent , S7_object ) || (is_class(parent ) && parent @ abstract )) {
8
+ new_object_call <-
9
+ if (has_S7_symbols(envir , " new_object" , " S7_object" )) {
10
+ bquote(new_object(S7_object(), ..(self_args )), splice = TRUE )
11
+ } else {
12
+ bquote(S7 :: new_object(S7 :: S7_object(), ..(self_args )), splice = TRUE )
13
+ }
14
+
7
15
return (new_function(
8
16
args = arg_info $ self ,
9
17
body = as.call(c(quote(`{` ),
10
18
# Force all promises here so that any errors are signaled from
11
19
# the constructor() call instead of the new_object() call.
12
20
unname(self_args ),
13
- new_call( " new_object " , c( list (quote(S7_object())), self_args ))
21
+ new_object_call
14
22
)),
15
- env = asNamespace( " S7 " )
23
+ env = envir
16
24
))
17
25
}
18
26
@@ -42,15 +50,19 @@ new_constructor <- function(parent, properties) {
42
50
parent_args <- as_names(names(arg_info $ parent ), named = TRUE )
43
51
names(parent_args )[names(parent_args ) == " ..." ] <- " "
44
52
parent_call <- new_call(parent_name , parent_args )
45
- body <- new_call(" new_object" , c(parent_call , self_args ))
53
+ body <- new_call(
54
+ if (has_S7_symbols(envir , " new_object" )) " new_object" else c(" S7" , " new_object" ),
55
+ c(parent_call , self_args )
56
+ )
46
57
47
- env <- new.env(parent = asNamespace( " S7 " ) )
58
+ env <- new.env(parent = envir )
48
59
env [[parent_name ]] <- parent_fun
49
60
50
61
new_function(args , body , env )
51
62
}
52
63
53
- constructor_args <- function (parent , properties = list ()) {
64
+ constructor_args <- function (parent , properties = list (),
65
+ envir = asNamespace(" S7" ), package = NULL ) {
54
66
parent_args <- formals(class_constructor(parent ))
55
67
56
68
# Remove read-only properties
@@ -66,7 +78,7 @@ constructor_args <- function(parent, properties = list()) {
66
78
67
79
self_args <- as.pairlist(lapply(
68
80
setNames(, self_arg_nms ),
69
- function (name ) prop_default(properties [[name ]]))
81
+ function (name ) prop_default(properties [[name ]], envir , package ))
70
82
)
71
83
72
84
list (parent = parent_args ,
@@ -81,8 +93,14 @@ is_property_dynamic <- function(x) is.function(x$getter)
81
93
missing_args <- function (names ) {
82
94
lapply(setNames(, names ), function (i ) quote(class_missing ))
83
95
}
96
+
84
97
new_call <- function (call , args ) {
85
- as.call(c(list (as.name(call )), args ))
98
+ if (is.character(call )) {
99
+ call <- switch (length(call ),
100
+ as.name(call ),
101
+ as.call(c(quote(`::` ), lapply(call , as.name ))))
102
+ }
103
+ as.call(c(list (call ), args ))
86
104
}
87
105
88
106
as_names <- function (x , named = FALSE ) {
@@ -91,3 +109,14 @@ as_names <- function(x, named = FALSE) {
91
109
}
92
110
lapply(x , as.name )
93
111
}
112
+
113
+ has_S7_symbols <- function (env , ... ) {
114
+ env <- topenv(env )
115
+ if (identical(env , asNamespace(" S7" )))
116
+ return (TRUE )
117
+ if (! isNamespace(env ))
118
+ return (FALSE )
119
+ imports <- getNamespaceImports(env )[[" S7" ]]
120
+ symbols <- c(... ) %|| % getNamespaceExports(" S7" )
121
+ all(symbols %in% imports )
122
+ }
0 commit comments