Skip to content

Commit b2fc9f1

Browse files
committed
fix C++ handling (Exporter and generic interface set) of new nClass structure
1 parent abc2378 commit b2fc9f1

17 files changed

Lines changed: 681 additions & 274 deletions

File tree

nCompiler/R/NC.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ make_nClass_code <- function(internals,
295295
list(isCompiled = function() FALSE)),
296296
private = list(
297297
Cpublic_obj = NULL,
298-
init_Cpublic_obj_code = quote(.Cpub_class$new()),
298+
init_Cpublic_obj_code = quote(.Cpub_class$new(...)),
299299
initialize_Cpublic_obj = function(...) {
300300
private$Cpublic_obj <- eval(private$init_Cpublic_obj_code)
301301
}

nCompiler/R/NC_FullCompiledInterface.R

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -156,32 +156,32 @@ build_compiled_nClass <- function(NCgenerator,
156156
CnCgenerator
157157
}
158158

159-
buildActiveBinding_for_compiled_nClass <- function(NCI, fieldNames) {
159+
# buildActiveBinding_for_compiled_nClass <- function(NCI, fieldNames) {
160160
#fieldNames <- NCI$fieldNames
161-
symTab <- NCI$symbolTable
162-
activeBindings <- list()
163-
newFields <- list()
164-
for(name in fieldNames) {
165-
ans <- function(value) {}
166-
sym <- symTab$getSymbol(name)
167-
if(is.null(sym)) {
168-
warning(paste0("Could not find a way to build active binding for field ", name, "."))
169-
return(ans)
170-
}
171-
body(ans) <- substitute(
172-
{
173-
if(missing(value))
174-
private$DLLenv$get_value(`:::`("nCompiler", "getExtptr")(private$CppObj), NAME)
175-
else
176-
private$DLLenv$set_value(`:::`("nCompiler", "getExtptr")(private$CppObj), NAME, value)
177-
},
178-
list(NAME = name)
179-
)
180-
activeBindings[[name]] <- ans
181-
}
182-
list(activeBindings = activeBindings,
183-
newFields = newFields)
184-
}
161+
# symTab <- NCI$symbolTable
162+
# activeBindings <- list()
163+
# newFields <- list()
164+
# for(name in fieldNames) {
165+
# ans <- function(value) {}
166+
# sym <- symTab$getSymbol(name)
167+
# if(is.null(sym)) {
168+
# warning(paste0("Could not find a way to build active binding for field ", name, "."))
169+
# return(ans)
170+
# }
171+
# body(ans) <- substitute(
172+
# {
173+
# if(missing(value))
174+
# private$DLLenv$get_value(`:::`("nCompiler", "getExtptr")(private$CppObj), NAME)
175+
# else
176+
# private$DLLenv$set_value(`:::`("nCompiler", "getExtptr")(private$CppObj), NAME, value)
177+
# },
178+
# list(NAME = name)
179+
# )
180+
# activeBindings[[name]] <- ans
181+
# }
182+
# list(activeBindings = activeBindings,
183+
# newFields = newFields)
184+
# }
185185

186186
make_compiled_nClass_code <- function(NCgenerator) {
187187
classname <- NCinternals(NCgenerator)$compileInfo$classname
@@ -248,8 +248,10 @@ make_compiled_Cpub_class_code <- function(NCgenerator,
248248
initialize_fun <- NC_get_Cpub_class(NCgenerator)$public_methods[["initialize"]]
249249
if(is.null(initialize_fun)) {
250250
if(!omit_automatic_Cpp_construction) {
251+
# The ... argument to initialize is important because it will be called
252+
# with ... from the outer R6generator, which might have other arguments embedded in the ...
251253
Rmethods_code_list[["initialize"]] <- quote(
252-
function(CppObj) {self$initializeCpp(CppObj)}
254+
function(CppObj, ...) {self$initializeCpp(CppObj)}
253255
)
254256
}
255257
} else {
@@ -276,7 +278,7 @@ make_compiled_Cpub_class_code <- function(NCgenerator,
276278
private$DLLenv$set_value(`:::`("nCompiler", "getExtptr")(private$CppObj), NAME, value)
277279
},
278280
list(NAME = name)
279-
)
281+
) |> removeSource()
280282
activeBindings_code_list[[name]] <- ABcode
281283
}
282284
activeBindings_code <- do.call("call", c("list",
@@ -291,7 +293,7 @@ make_compiled_Cpub_class_code <- function(NCgenerator,
291293

292294
Cpub_inherit_arg <- if(package) {
293295
if(is.null(inheritName)) quote(nCompiler::CpubClass)
294-
else substitute(IRN$parent_env$.Cpub_class, list(IRN=as.name(paste0(inheritName, "_C_compiled"))))
296+
else substitute(IRN$parent_env$.Cpub_class, list(IRN=as.name(paste0(".", inheritName, "_CnCgenerator_CpubGen"))))
295297
} else {
296298
if(is.null(inheritName)) quote(nCompiler::CpubClass)
297299
else quote(.Cpub_base_class)

nCompiler/R/NC_Utils.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ isNCgenerator <- function(x) {
4040
#' @export
4141
isCompiledNCgenerator <- function(x) {
4242
if(inherits(x, "R6ClassGenerator"))
43-
exists(".newCobjFun", x)
43+
exists(".newCobjFun", x$parent_env)
4444
else
4545
FALSE
4646
}

nCompiler/R/NF_PassByReference.R

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
passByReference <- function(fun,
55
refArgs = character(),
66
blockRefArgs = character()) {
7+
# The format from an NFinternals object is a named list of TRUE/FALSE values,
8+
# so convert that to a character vector of names for the TRUE ones.
79
if(is.list(refArgs))
810
refArgs <- names(refArgs)[ unlist(lapply(refArgs, isTRUE)) ]
911

@@ -16,17 +18,22 @@ passByReference <- function(fun,
1618
if((length(refArgs)==0) & length(blockRefArgs)==0)
1719
return(fun)
1820

21+
# fun can be a function or the body of a function.
1922
passedAsFunction <- is.function(fun)
2023
code <- if(passedAsFunction)
2124
body(fun)
2225
else
2326
fun
2427

28+
# Helper to create a substitution list from argument names
29+
# e.g. ("x", "_suffix") -> list(x = as.name("x_suffix"))
2530
args_2_subList <- function(args, suffix)
2631
args |>
2732
lapply(function(x) as.name(paste0(x, suffix))) |>
2833
structure(names = args)
2934

35+
# Helper to create lines of code for active bindings
36+
# e.g. nCompiler::createRef("x_suffix", x) # either createRef or createBlockRef
3037
subList_2_lines <- function(subList,
3138
fun_name) {
3239
lines <- list()
@@ -41,12 +48,17 @@ passByReference <- function(fun,
4148
lines
4249
}
4350

51+
# From "x", create lines like
52+
# nCompiler::createRef("x_Ref__", x)
4453
subList <- args_2_subList(refArgs, "_Ref__")
4554
refArg_activeBinding_lines <- subList_2_lines(subList, "createRef")
4655

56+
# From "y", create lines like
57+
# nCompiler::createBlockRef("y_BlockRef__", y)
4758
blockSubList <- args_2_subList(blockRefArgs, "_BlockRef__")
4859
blockRefArg_activeBinding_lines <- subList_2_lines(blockSubList, "createBlockRef")
4960

61+
# In the original code, replace x with x_Ref__, y with y_BlockRef__, etc.
5062
code <-
5163
eval(
5264
substitute(
@@ -56,6 +68,7 @@ passByReference <- function(fun,
5668
)
5769
)
5870

71+
# Wrap in braces if not already
5972
if(code[[1]] != '{')
6073
code <- substitute({CODE}, list(CODE=code))
6174

@@ -102,8 +115,7 @@ createRef <- function(innerName,
102115
createBlockRef <- function(innerName,
103116
outerCode,
104117
env,
105-
innerEnv,
106-
dummyName = 'DUMMY_FOR_CREATE_BLOCK_REF_') {
118+
innerEnv) {
107119
# There is potential for more elaborate error-trapping.
108120
# E.g. we could determine the sizes (or net length) of outerCode (assignment target)
109121
# and check that v matches it.
@@ -117,19 +129,18 @@ createBlockRef <- function(innerName,
117129
stop("A block reference argument must be passed as a variable name, e.g. `x`, or an indexed block of a variable, e.g. `x[1:4, 2:3]` or `x[1:4, ]`.")
118130
}
119131
outerLen <- eval(substitute(length(OC), list(OC = outerCode)), envir=env)
120-
outer_dummy_assign_code <- substitute(L <- R,
121-
list(L = outerCode,
122-
R = as.name(dummyName)))
132+
assignment_code <- substitute(L <- NULL,
133+
list(L = outerCode))
123134
binding <-
124135
function(v)
125136
if(missing(v))
126137
eval(outerCode, env)
127138
else {
128139
if(outerLen != length(v))
129140
stop("blockRef assignment must match in length.")
130-
assign(dummyName, v, env)
131-
on.exit(rm(list = dummyName, envir = env))
132-
eval(outer_dummy_assign_code, env)
141+
assignment_code[[3]] <<- v
142+
eval(assignment_code, env)
143+
assignment_code[[3]] <<- NULL
133144
v
134145
}
135146
makeActiveBinding(innerName, binding, innerEnv)

nCompiler/R/compile_labelAbstractTypes.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,10 @@ inLabelAbstractTypesEnv(
654654
if(!symTab$symbolExists(LHS$name, TRUE)) {
655655
newSym <- RHStype$clone()
656656
newSym$isArg <- FALSE
657+
# Assignment from a reference (could be a refArg) or a blockRef (blockRefArg) should be a plain type
658+
newSym$isRef <- FALSE
659+
if(!is.null(newSym$isBlockRef))
660+
newSym$isBlockRef <- FALSE
657661
newSym$name <- LHS$name
658662
symTab$addSymbol(newSym)
659663
LHS$type <- newSym

nCompiler/R/nCompile.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,8 @@ nCompile <- function(...,
385385
memberData = list(),
386386
roxygen = list()
387387
)
388+
if(isTRUE(get_nOption('pause_after_writing_files')))
389+
browser()
388390
lib <- file.path(tempdir(), "templib")
389391
if(!dir.exists(lib)) dir.create(lib, recursive=TRUE)
390392
pkgDir <- file.path(dir, temppkgname)

0 commit comments

Comments
 (0)