forked from RConsortium/S7
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethod.R
More file actions
209 lines (177 loc) · 6.38 KB
/
method.R
File metadata and controls
209 lines (177 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#' Retrieve or register an R7 method for a generic
#'
#' @param generic The generic to retrieve or register
#' @param signature The method signature
#' @param method,value The new function to use as the method.
#' @importFrom utils getS3method
#' @export
method <- function(generic, signature) {
method_impl(generic, signature, ignore = NULL)
}
method_impl <- function(generic, signature, ignore) {
# This slows down the method dispatch too much
#generic <- as_generic(generic)
out <- .Call(method_, generic, signature, ignore)
if (is.null(out)) {
# If no R7 method is found, see if there are any S3 methods registered
if (inherits(generic, "R7_generic")) {
args <- generic@signature
generic <- generic@name
} else {
generic <- find_function_name(generic, topenv(environment(generic)))
args <- args(formals(generic))
}
args <- args[names(args) != "..."]
out <- getS3method(generic, signature[[1]][[1]], optional = TRUE)
# If no method found check if the generic has a default method
out <- getS3method(generic, "default", optional = TRUE)
}
if (is.null(out)) {
stop(sprintf("Can't find method for generic '%s' with arguments of type:\n%s", generic, paste0("- ", names(args), ": ", vcapply(signature, paste0, collapse = ", "), collapse = "\n"), call. = FALSE))
}
out
}
find_function_name <- function(x, env) {
nms <- ls(env, all.names = TRUE, sorted = FALSE)
for (name in nms) {
if (identical(get0(name, envir = env, mode = "function", inherits = FALSE), x)) {
return(name)
}
}
return(NULL)
}
#' Retrieve the next applicable method after the current one
#'
#' @export
next_method <- function() {
current_method <- sys.function(sys.parent(1))
methods <- list()
i <- 1
while (!inherits(current_method, "R7_generic")) {
methods <- c(methods, current_method)
i <- i + 1
current_method <- sys.function(sys.parent(i))
}
generic <- current_method
signature <- eval(generic_generate_signature_call(generic@signature), parent.frame())
method_impl(generic, signature, ignore = methods)
}
#' Register R7 methods
#'
#' When registering methods for R7 generics defined in other packages you must
#' put `method_register()` in your packages [.onLoad] function.
#'
#' @importFrom utils getFromNamespace packageName
#' @export
method_register <- function() {
package <- packageName(parent.frame())
tbl <- asNamespace(package)[[".__S3MethodsTable__."]][[".R7_methods"]]
for (x in tbl) {
if (isNamespaceLoaded(x$package)) {
ns <- asNamespace(x$package)
new_method(getFromNamespace(x$generic, ns), x$signature, x$value)
} else {
setHook(packageEvent(x$package, "onLoad"),
local({x <- x
function(...) {
ns <- asNamespace(x$package)
if (is.null(x$version) || getNamespaceVersion(ns) >= x$version) {
new_method(getFromNamespace(x$generic, ns), x$signature, x$value)
}
}
})
)
}
}
}
arg_to_string <- function(arg) {
if (is.na(names(arg)[[1]])) {
return("does not exist")
}
sprintf("is `%s = %s`", names(arg), deparse(arg[[1]]))
}
method_compatible <- function(method, generic) {
generic_formals <- formals(generic)
method_formals <- formals(method)
for (i in seq_len(length(generic_formals) - 1)) {
if (!identical(generic_formals[i], method_formals[i])) {
stop(sprintf("`method` must be consistent with <R7_generic> %s.\n- Argument %i in generic %s\n- Argument %i in method %s", generic@name, i, arg_to_string(generic_formals[i]), i, arg_to_string(method_formals[i])), call. = FALSE)
}
}
if ("..." %in% names(generic_formals) && !"..." %in% names(method_formals)) {
stop(sprintf("`method` must be consistent with <R7_generic> %s.\n- `generic` has `...`\n- `method` does not have `...`", generic@name), call. = FALSE)
}
if (!"..." %in% names(generic_formals) && "..." %in% names(method_formals)) {
stop(sprintf("`method` must be consistent with <R7_generic> %s.\n- `generic` does not have `...`\n- `method` has `...`", generic@name), call. = FALSE)
}
TRUE
}
#' @rdname method
#' @export
new_method <- function(generic, signature, method) {
if (inherits(generic, "R7_external_generic")) {
package <- generic$package
generic <- generic$generic
# Get current package, if any
current_package <- packageName(parent.frame())
if (!is.null(current_package)) {
tbl <- asNamespace(current_package)[[".__S3MethodsTable__."]]
if (is.null(tbl[[".R7_methods"]])) {
tbl[[".R7_methods"]] <- list()
}
tbl[[".R7_methods"]] <- append(tbl[[".R7_methods"]], list(list(generic = generic$generic, package = generic$package, signature = signature, method = method, version = generic$version)))
return(invisible())
}
generic <- getFromNamespace(generic, asNamespace(package))
}
generic <- as_generic(generic)
method_compatible(method, generic)
if (!is.character(signature) && !inherits(signature, "list")) {
signature <- list(signature)
}
if (!inherits(method, "R7_method")) {
method <- R7_method(generic, signature, method)
}
generic_name <- generic@name
p_tbl <- generic@methods
for (i in seq_along(signature)) {
if (inherits(signature[[i]], "R7_union")) {
for (class in signature[[1]]@classes) {
new_method(generic, c(signature[seq_len(i - 1)], class@name), method)
}
return(invisible(generic))
} else if (inherits(signature[[i]], "R7_class")) {
signature[[i]] <- signature[[i]]@name
}
if (i == length(signature)) {
p_tbl[[signature[[i]]]] <- method
} else {
tbl <- p_tbl[[signature[[i]]]]
if (is.null(tbl)) {
tbl <- new.env(hash = TRUE, parent = emptyenv())
p_tbl[[signature[[i]]]] <- tbl
}
p_tbl <- tbl
}
}
invisible(generic)
}
#' @rdname method
#' @export
`method<-` <- function(generic, signature, value) {
new_method(generic, signature, value)
}
as_generic <- function(generic) {
if (length(generic) == 1 && is.character(generic)) {
generic <- match.fun(generic)
}
if (!inherits(generic, "R7_generic")) {
stop("`generic` must be a 'R7_generic':\n- `generic` is a '", class(generic)[[1]], "'", call. = FALSE)
}
generic
}
#' Lookup the R7 method for the current generic and call it.
#' @export
method_call <- function() {
.Call(method_call_, sys.call(sys.parent(1)), sys.function(sys.parent(1)), parent.frame())
}