Skip to content

Commit

Permalink
use srcref attribute rather than R_SrcrefSymbol
Browse files Browse the repository at this point in the history
  • Loading branch information
jjallaire committed Oct 6, 2011
1 parent 5204546 commit d606faa
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
4 changes: 0 additions & 4 deletions src/cpp/r/RSexp.cpp
Expand Up @@ -206,10 +206,6 @@ SEXP getAttrib(SEXP object, SEXP attrib)
return Rf_getAttrib(object, attrib);
}

SEXP getSrcAttrib(SEXP object)
{
return R_NilValue;
}

Error extract(SEXP valueSEXP, int* pInt)
{
Expand Down
1 change: 0 additions & 1 deletion src/cpp/r/include/r/RSexp.hpp
Expand Up @@ -79,7 +79,6 @@ double asReal(SEXP object);
bool asLogical(SEXP object);

SEXP getAttrib(SEXP object, SEXP attrib);
SEXP getSrcAttrib(SEXP object);

// extract c++ type from R SEXP
core::Error extract(SEXP valueSEXP, int* pInt);
Expand Down
37 changes: 25 additions & 12 deletions src/cpp/session/modules/SessionCodeSearch.cpp
Expand Up @@ -737,6 +737,27 @@ bool findFunction(const std::string& token,
}


void getFunctionSource(SEXP functionSEXP,
std::vector<std::string>* pLines,
bool* pFromSrcAttrib)
{
// check if the function has a "srcref" attribute
*pFromSrcAttrib = false;
r::exec::RFunction getSrcRefFunc(".rs.functionHasSrcRef", functionSEXP);
Error error = getSrcRefFunc.call(pFromSrcAttrib);
if (error)
LOG_ERROR(error);

// deparse
r::exec::RFunction deparseFunc(".rs.deparseFunction");
deparseFunc.addParam(functionSEXP);
deparseFunc.addParam(*pFromSrcAttrib);
error = deparseFunc.call(pLines);
if (error)
LOG_ERROR(error);
}


json::Object createFunctionDefinition(const std::string& name,
const std::string& namespaceName)
{
Expand All @@ -746,7 +767,7 @@ json::Object createFunctionDefinition(const std::string& name,
funDef["namespace"] = namespaceName;

// function source code
bool hasSrcAttrib = false;
bool fromSrcAttrib = false;
std::vector<std::string> lines;

// get the function
Expand All @@ -759,16 +780,8 @@ json::Object createFunctionDefinition(const std::string& name,
// did we get a function
if (!r::sexp::isNull(functionSEXP))
{
// does it have a src attrib?
hasSrcAttrib = !r::sexp::isNull(r::sexp::getSrcAttrib(functionSEXP));

// get the code
r::exec::RFunction deparseFunc(".rs.deparseFunction",
functionSEXP,
hasSrcAttrib);
Error error = deparseFunc.call(&lines);
if (error)
LOG_ERROR(error);
// get the function source
getFunctionSource(functionSEXP, &lines, &fromSrcAttrib);
}
}
else
Expand All @@ -790,7 +803,7 @@ json::Object createFunctionDefinition(const std::string& name,
code.append("\n");
}
funDef["code"] = code;
funDef["from_src_attrib"] = hasSrcAttrib;
funDef["from_src_attrib"] = fromSrcAttrib;
}
else
{
Expand Down
5 changes: 5 additions & 0 deletions src/cpp/session/modules/SessionCodeTools.R
Expand Up @@ -92,6 +92,11 @@
eval(parse(text=paste(packageName, ":::", name, sep="")))
})

.rs.addFunction("functionHasSrcRef", function(func)
{
return (!is.null(attr(func, "srcref")))
})

.rs.addFunction("deparseFunction", function(func, useSource)
{
control <- c("keepInteger", "keepNA")
Expand Down

0 comments on commit d606faa

Please sign in to comment.