@@ -222,26 +222,28 @@ Module <- function( module, PACKAGE = methods::getPackageName(where), where = to
222222
223223 fields <- cpp_fields( CLASS , where )
224224 methods <- cpp_refMethods(CLASS , where )
225- generator <- methods :: setRefClass( clname ,
226- fields = fields ,
227- contains = " C++Object" ,
228- methods = methods ,
229- where = where
230- )
231225 # just to make codetools happy
232226 .self <- .refClassDef <- NULL
233- generator $ methods(initialize =
227+ # Supply 'initialize' together with the other methods so that the
228+ # reference class is analysed once, instead of a second full pass
229+ # through refClassInformation() in generator$methods(...)
230+ methods [[" initialize" ]] <-
234231 if (cpp_hasDefaultConstructor(CLASS ))
235232 function (... ) Rcpp :: cpp_object_initializer(.self ,.refClassDef , ... )
236233 else
237234 function (... ) {
238235 if (nargs()) Rcpp :: cpp_object_initializer(.self ,.refClassDef , ... )
239236 else Rcpp :: cpp_object_dummy(.self , .refClassDef ) # #nocov
240237 }
241- )
242238 rm( .self , .refClassDef )
239+ generator <- methods :: setRefClass( clname ,
240+ fields = fields ,
241+ contains = " C++Object" ,
242+ methods = methods ,
243+ where = where
244+ )
243245
244- classDef <- methods :: getClass( clname )
246+ classDef <- generator $ def
245247 # # non-public (static) fields in class representation
246248 # # <fixme> Should these become real fields? </fixme>
247249 fields <- classDef @ fieldPrototypes
@@ -268,7 +270,7 @@ Module <- function( module, PACKAGE = methods::getPackageName(where), where = to
268270 } # #nocov end
269271
270272 # promoting show to S4
271- if ( any( grepl( " show" , names(CLASS @ methods ) ) ) ){
273+ if ( " show" %in% names(CLASS @ methods ) ){
272274 setMethod ( "show ", clname, function(object) object$show(), where = where ) # #nocov
273275 }
274276
@@ -281,7 +283,10 @@ Module <- function( module, PACKAGE = methods::getPackageName(where), where = to
281283 CLASS <- classes [[i ]]
282284 clname <- CLASS @ .Data
283285 demangled_name <- sub( " ^Rcpp_" , " " , clname )
284- .classes_map [[ CLASS @ typeid ]] <- storage [[ demangled_name ]] <- .get_Module_Class( module , demangled_name , xp )
286+ # reuse the C++Class object already built by Module__classes_info
287+ # rather than rebuilding it (and all of its method/field objects)
288+ CLASS @ generator <- generators [[ clname ]]
289+ .classes_map [[ CLASS @ typeid ]] <- storage [[ demangled_name ]] <- CLASS
285290
286291 # exposing enums values as CLASS.VALUE
287292 # (should really be CLASS$value but I don't know how to do it)
@@ -299,24 +304,21 @@ Module <- function( module, PACKAGE = methods::getPackageName(where), where = to
299304 functions <- .Call( Module__functions_names , xp )
300305 for ( fun in functions ){
301306 storage [[ fun ]] <- .get_Module_function( module , fun , xp )
302-
303- # register as(FROM, TO) methods
304- converter_rx <- " ^[.]___converter___(.*)___(.*)$"
305- if ( length( matches <- grep( converter_rx , functions ) ) ){ # #nocov start
306- for ( i in matches ){
307- fun <- function s [i ]
308- from <- sub( converter_rx , " \\ 1" , fun )
309- to <- sub( converter_rx , " \\ 2" , fun )
310- converter <- function ( from ){}
311- body( converter ) <- substitute( { CONVERT(from ) },
312- list ( CONVERT = storage [[fun ]] )
313- )
314- setAs( from , to , converter , where = where )
315- }
316- } # #nocov end
317-
318307 }
319308
309+ # register as(FROM, TO) methods once storage holds every function: the
310+ # converter body captures storage[[fun]] by value via substitute()
311+ converter_rx <- " ^[.]___converter___(.*)___(.*)$"
312+ for ( fun in grep( converter_rx , functions , value = TRUE ) ){ # #nocov start
313+ from <- sub( converter_rx , " \\ 1" , fun )
314+ to <- sub( converter_rx , " \\ 2" , fun )
315+ converter <- function ( from ){}
316+ body( converter ) <- substitute( { CONVERT(from ) },
317+ list ( CONVERT = storage [[fun ]] )
318+ )
319+ setAs( from , to , converter , where = where )
320+ } # #nocov end
321+
320322 assign( " storage" , storage , envir = as.environment(module ) )
321323 module
322324}
0 commit comments