Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export package contents as LangSxp and make them callable in v0.4.x #30

Merged
merged 2 commits into from
Mar 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/functions.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@doc "Create a function call from a list of arguments"->
function lang(f::Union(RFunction,SymSxp),args...;kwargs...)
function lang(f::SEXPREC,args...;kwargs...)
argn = length(args)+length(kwargs)
l = preserve(sexp(ccall((:Rf_allocVector,libR),Ptr{Void},(Cint,Int),6,argn+1)))
s = l.p
Expand All @@ -8,15 +8,15 @@ function lang(f::Union(RFunction,SymSxp),args...;kwargs...)
if typeof(argv) <: Symbol
argv = sexp(argv)
end
typeof(argv) <: SEXPREC || throw(MethodError("expect SEXPREC arguments"))
typeof(argv) <: SEXPREC || throw(MethodError("unexpected arguments"))
s = ccall((:CDR,libR),Ptr{Void},(Ptr{Void},),s)
ccall((:SETCAR,libR),Ptr{Void},(Ptr{Void},Ptr{Void}),s,argv)
end
for (key,argv) in kwargs
if typeof(argv) <: Symbol
argv = sexp(argv)
end
typeof(argv) <: SEXPREC || throw(MethodError("expect SEXPREC arguments"))
typeof(argv) <: SEXPREC || throw(MethodError("unexpected arguments"))
s = ccall((:CDR,libR),Ptr{Void},(Ptr{Void},),s)
ccall((:SET_TAG,libR),Ptr{Void},(Ptr{Void},Ptr{Void}),s,sexp(key))
ccall((:SETCAR,libR),Ptr{Void},(Ptr{Void},Ptr{Void}),s,argv)
Expand All @@ -29,4 +29,9 @@ lang(s::Symbol,args...;kwargs...) = lang(sexp(s),args...;kwargs...)
Evaluate a function in the global environment. The first argument corresponds
to the function to be called. It can be either a RFunction type, a SymSxp or
a Symbol."""->
rcall(f::Union(RFunction,SymSxp,Symbol),args...;kwargs...) = reval(lang(f,args...,kwargs...))
rcall(f::SEXPREC,args...;kwargs...) = reval(lang(f,args...,kwargs...))
rcall(f::Symbol,args...;kwargs...) = reval(lang(f,args...,kwargs...))

if VERSION >= v"v0.4-"
Base.call(f::Union(SymSxp,LangSxp,RFunction),args...;kwargs...) = rcall(f,args...;kwargs...)
end
14 changes: 9 additions & 5 deletions src/library.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ for w in ("while", "if", "for", "try", "return", "break",
"global", "const", "abstract", "typealias", "type",
"bitstype", "immutable", "ccall", "do", "module",
"baremodule", "using", "import", "export", "importall",
"pymember", "false", "true")
"false", "true")
push!(reserved, w) # construct Set this way for compat with Julia 0.2/0.3
end

function rwrap(pkg::ASCIIString,s::Symbol)
reval("library($pkg)")
env = rcall(symbol("as.environment"),sexp("package:$pkg"))
!isNull(env) || error("The package has nothing!")
members = [(n, reval(env[symbol(n)])) for n in rcopy(rcall(:ls,env))]
filter!(x -> !(x[1] in reserved), members)
members = rcopy(rcall(:ls,env))
filter!(x -> !(x in reserved), members)
m = Module(s)
consts = [Expr(:const, Expr(:(=), symbol(x[1]), x[2])) for x in members]
consts = [Expr(:const,
Expr(:(=),
symbol(x),
lang(symbol("::"),symbol(pkg),symbol(x)))
) for x in members]
id = Expr(:(=), :__package__, pkg)
exports = [symbol(x[1]) for x in members]
exports = [symbol(x) for x in members]
eval(m, Expr(:toplevel, consts..., Expr(:export, exports...), id))
m
end
Expand Down