Skip to content

Commit f0a3b87

Browse files
committed
tests and cleanup for user-defined opDefs
1 parent f4a47b9 commit f0a3b87

6 files changed

Lines changed: 340 additions & 81 deletions

File tree

nCompiler/R/compile_eigenization.R

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,18 @@ compile_eigenize <- function(code,
8282
if(!is.null(handlingInfo)) {
8383
beforeHandler <- handlingInfo[['beforeHandler']]
8484
if(!is.null(beforeHandler)) {
85-
setupExprs <- c(setupExprs,
86-
eval(call(beforeHandler,
87-
code,
88-
symTab,
89-
auxEnv,
90-
workEnv,
91-
handlingInfo),
92-
envir = eigenizeEnv))
85+
if(is.function(beforeHandler))
86+
setupExprs <- c(setupExprs,
87+
beforeHandler(code, symTab, auxEnv, workEnv, handlingInfo))
88+
else
89+
setupExprs <- c(setupExprs,
90+
eval(call(beforeHandler,
91+
code,
92+
symTab,
93+
auxEnv,
94+
workEnv,
95+
handlingInfo),
96+
envir = eigenizeEnv))
9397
# return(if(length(setupExprs) == 0) NULL else setupExprs)
9498
}
9599
}
@@ -112,14 +116,18 @@ compile_eigenize <- function(code,
112116
if(!is.null(handlingInfo)) {
113117
handler <- handlingInfo[['handler']]
114118
if(!is.null(handler)) {
115-
setupExprs <- c(setupExprs,
116-
eval(call(handler,
117-
code,
118-
symTab,
119-
auxEnv,
120-
workEnv,
121-
handlingInfo),
122-
envir = eigenizeEnv))
119+
if(is.function(handler))
120+
setupExprs <- c(setupExprs,
121+
handler(code, symTab, auxEnv, workEnv, handlingInfo))
122+
else
123+
setupExprs <- c(setupExprs,
124+
eval(call(handler,
125+
code,
126+
symTab,
127+
auxEnv,
128+
workEnv,
129+
handlingInfo),
130+
envir = eigenizeEnv))
123131
}
124132
}
125133
# }

nCompiler/R/compile_generateCpp.R

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,13 @@ compile_generateCpp <- function(code,
105105
if(!is.null(handler)) {
106106
if (logging)
107107
appendToLog(paste('Calling handler', handler, 'for', code$name))
108-
res <- eval(call(handler,
109-
code,
110-
symTab),
111-
envir = genCppEnv)
108+
if(is.function(handler))
109+
res <- handler(code, symTab)
110+
else
111+
res <- eval(call(handler,
112+
code,
113+
symTab),
114+
envir = genCppEnv)
112115
if (logging) {
113116
appendToLog(paste('Finished handling', handler, 'for',
114117
code$name, 'with result:'))

nCompiler/R/compile_labelAbstractTypes.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,11 @@ compile_labelAbstractTypes <- function(code,
112112
if(!is.null(handler)) {
113113
if (logging)
114114
appendToLog(paste('Calling handler', handler, 'for', code$name))
115-
ans <- eval(call(handler, code, symTab, auxEnv, handlingInfo),
116-
envir = labelAbstractTypesEnv)
115+
if(is.function(handler))
116+
ans <- handler(code, symTab, auxEnv, handlingInfo)
117+
else
118+
ans <- eval(call(handler, code, symTab, auxEnv, handlingInfo),
119+
envir = labelAbstractTypesEnv)
117120
nErrorEnv$stateInfo <- character()
118121
if (logging) {
119122
appendToLog(paste('Finished handling', handler, 'for', code$name))

nCompiler/R/compile_normalizeCalls.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ update_cachedOpInfo <- function(code, where, allowFail=FALSE) {
169169
if(is.null(opDef)) {
170170
## At this point, we have not found an nFunction or nClass method.
171171
if(is_NCgenerator) {
172-
opDef <- where$compileInfo$opDefs[[code$name]]
172+
opDef <- NCinternals(where)$compileInfo$opDefs[[code$name]]
173173
if(!is.null(opDef)) {
174174
cachedOpInfo$case <- "nClass method" # this could be a pure keyword or an nFunction with opDef provided at the nClass level
175175
# a pure keyword will have obj_internals == NULL, providing a way to

nCompiler/tests/testthat/nCompile_tests/test-compileNimble.R

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -33,61 +33,3 @@ test_that("compileNimble bridge works for one nimbleFunction object", {
3333
## add nClass to nCompiler:::compileNimble
3434
##
3535
## document, document, document
36-
37-
test_that("registering a user-defined operator definition (opDef) works", {
38-
## first version: provide a function
39-
nimArrayHandler <- function(code,...) {
40-
code$name <- 'nArray'
41-
NULL
42-
}
43-
# This test works by:
44-
# providing a handler to relpace "nimArray" with "nArray"
45-
# and a handler to replace "nimArray2" with "nArray" to
46-
# check on handling multiple cases.
47-
registerOpDef(
48-
list(nimArray =
49-
list(
50-
matchDef = function(value=0, dim=c(1,1), init=TRUE,
51-
fillZeros=TRUE, recycle=TRUE, nDim,
52-
type="double") {},
53-
# normalizeCalls=list(handler='skip'),
54-
simpleTransformations=list(handler = nimArrayHandler))))
55-
expect_equal(ls(`:::`("nCompiler", "operatorDefUserEnv")), "nimArray")
56-
57-
registerOpDef(
58-
list(nimArray2 =
59-
list(
60-
matchDef = function(value=0, dim=c(1,1), init=TRUE,
61-
fillZeros=TRUE, recycle=TRUE, nDim,
62-
type="double") {},
63-
simpleTransformations=list(handler = 'replace',
64-
replacement = 'nArray'))))
65-
expect_equal(ls(`:::`("nCompiler", "operatorDefUserEnv")), c("nimArray", "nimArray2"))
66-
67-
nc <- nClass(
68-
Cpublic = list(
69-
foo = nFunction(
70-
function() {
71-
ans <- nimArray( 6, dim = 2)
72-
ans2 <- nArray(value = 5, dim = 2)
73-
return(ans)
74-
returnType('double(1)')
75-
}
76-
),
77-
foo2 = nFunction(
78-
function() {
79-
ans <- nimArray2(3,dim = 2)
80-
return(ans)
81-
returnType('double(1)')
82-
})
83-
))
84-
Cnc <- nCompile(nc)
85-
obj <- Cnc$new()
86-
expect_identical(obj$foo(), c(6, 6))
87-
expect_identical(obj$foo2(), c(3, 3))
88-
rm(obj); gc()
89-
#
90-
deregisterOpDef("nimArray")
91-
deregisterOpDef("nimArray2")
92-
expect_equal(length(ls(`:::`("nCompiler", "operatorDefUserEnv"))), 0)
93-
})

0 commit comments

Comments
 (0)