[Rcpp-commits] r4319 - pkg/Rcpp/src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat May 18 03:39:14 CEST 2013


Author: jjallaire
Date: 2013-05-18 03:39:13 +0200 (Sat, 18 May 2013)
New Revision: 4319

Modified:
   pkg/Rcpp/src/attributes.cpp
Log:
prevent gc problems with RNGScope destructor in attributes generated functions; don't include package header file in generated cpp exports file

Modified: pkg/Rcpp/src/attributes.cpp
===================================================================
--- pkg/Rcpp/src/attributes.cpp	2013-05-18 01:32:08 UTC (rev 4318)
+++ pkg/Rcpp/src/attributes.cpp	2013-05-18 01:39:13 UTC (rev 4319)
@@ -1657,8 +1657,10 @@
                        << getCCallable(package() + "_" + function.name()) << ";" 
                        << std::endl;
                 ostr() << "        }" << std::endl;
-                ostr() << "        RNGScope __rngScope;" << std::endl;
-                ostr() << "        RObject __result = " << ptrName << "(";
+                ostr() << "        RObject __result;" << std::endl;
+                ostr() << "        {" << std::endl;
+                ostr() << "            RNGScope __rngScope;" << std::endl;
+                ostr() << "            __result = " << ptrName << "(";
                 
                 const std::vector<Argument>& args = function.arguments();
                 for (std::size_t i = 0; i<args.size(); i++) {
@@ -1668,6 +1670,7 @@
                 }
                        
                 ostr() << ");" << std::endl;
+                ostr() << "        }" << std::endl;
                 
                 ostr() << "        if (__result.inherits(\"try-error\"))" 
                        << std::endl
@@ -1707,7 +1710,14 @@
             // includes
             if (!includes.empty()) {
                 for (std::size_t i=0;i<includes.size(); i++)
-                    ostr << includes[i] << std::endl;
+                {
+                    // don't do inst/include here
+                    if (includes[i].find("#include \"../inst/include/")
+                                                       == std::string::npos)
+                    {
+                        ostr << includes[i] << std::endl;
+                    }
+                }
                 ostr << std::endl;
             }
             
@@ -2123,18 +2133,21 @@
             std::string args = ostrArgs.str();
             ostr << args << ") {" << std::endl;
             ostr << "BEGIN_RCPP" << std::endl;
+            if (!function.type().isVoid())
+                ostr << "    SEXP __sexp_result;" << std::endl;
+            ostr << "    {" << std::endl;
             if (!cppInterface)
-                ostr << "    Rcpp::RNGScope __rngScope;" << std::endl;
+                ostr << "        Rcpp::RNGScope __rngScope;" << std::endl;
             for (size_t i = 0; i<arguments.size(); i++) {
                 const Argument& argument = arguments[i];
                 
                 // Rcpp::as to c++ type
-                ostr << "    " << argument.type().name() << " " << argument.name() 
+                ostr << "        " << argument.type().name() << " " << argument.name() 
                      << " = " << "Rcpp::as<"  << argument.type().name() << " >(" 
                      << argument.name() << "SEXP);" << std::endl;
             }
             
-            ostr << "    ";
+            ostr << "        ";
             if (!function.type().isVoid())
                 ostr << function.type() << " __result = ";
             ostr << function.name() << "(";
@@ -2145,10 +2158,22 @@
                     ostr << ", ";
             }
             ostr << ");" << std::endl;
-            
-            std::string res = function.type().isVoid() ? "R_NilValue" : 
-                                                         "Rcpp::wrap(__result)";
-            ostr << "    return " << res << ";" << std::endl;
+        
+            if (!function.type().isVoid())
+            {
+                ostr << "        PROTECT(__sexp_result = Rcpp::wrap(__result));"
+                     << std::endl;
+            }
+            ostr << "    }" << std::endl;
+            if (!function.type().isVoid())
+            {
+                ostr << "    UNPROTECT(1);" << std::endl;
+                ostr << "    return __sexp_result;" << std::endl;
+            }
+            else
+            {
+                ostr << "    return R_NilValue;" << std::endl;
+            }
             ostr << (cppInterface ? "END_RCPP_RETURN_ERROR" : "END_RCPP")
                  << std::endl;
             ostr << "}" << std::endl;
@@ -2157,8 +2182,10 @@
             if (cppInterface) {
                 ostr << "RcppExport SEXP " << funcName << "(" << args << ") {"
                      << std::endl;
-                ostr << "    Rcpp::RNGScope __rngScope;" << std::endl;
-                ostr << "    SEXP __result = PROTECT(" << funcName 
+                ostr << "    SEXP __result;" << std::endl;
+                ostr << "    {" << std::endl;
+                ostr << "        Rcpp::RNGScope __rngScope;" << std::endl;
+                ostr << "        __result = PROTECT(" << funcName 
                      << kTrySuffix << "(";
                 for (size_t i = 0; i<arguments.size(); i++) {
                     const Argument& argument = arguments[i];
@@ -2167,12 +2194,15 @@
                         ostr << ", ";
                 }
                 ostr << "));" << std::endl;
-                ostr << "    "
-                     << "Rboolean __isError = Rf_inherits(__result, \"try-error\");"
-                     << std::endl;
-                ostr << "    UNPROTECT(1);" << std::endl
-                     << "    if (__isError)" << std::endl
-                     << "        Rf_error(CHAR(Rf_asChar(__result)));" << std::endl
+                ostr << "    }" << std::endl;
+                ostr << "    Rboolean __isError = Rf_inherits(__result, \"try-error\");"
+                     << std::endl
+                     << "    if (__isError) {" << std::endl
+                     << "        SEXP __msgSEXP = Rf_asChar(__result);" << std::endl
+                     << "        UNPROTECT(1);" << std::endl
+                     << "        Rf_error(CHAR(__msgSEXP));" << std::endl
+                     << "    }" << std::endl
+                     << "    UNPROTECT(1);" << std::endl
                      << "    return __result;" << std::endl
                      << "}" << std::endl;
             }



More information about the Rcpp-commits mailing list