Skip to content
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,10 @@ From a constructed RuntimeGeneratedFunction, you can retrieve the expressions us
`RuntimeGeneratedFunctions.get_expression` command. For example:

```julia
julia> RuntimeGeneratedFunctions.get_expression(rgf)
ex = :((x) -> x^2)
rgf = @RuntimeGeneratedFunction(ex)
julia> RuntimeGeneratedFunctions.get_expression(rgf)
:((x,) -> x ^ 2)
```

This can be used to get the expression even if `drop_expr` has been performed.
Expand Down
5 changes: 3 additions & 2 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ builds functions in a way that avoids `eval`.

Note that `RuntimeGeneratedFunction` does not handle closures. Please use the
[GeneralizedGenerated.jl](https://github.com/JuliaStaging/GeneralizedGenerated.jl)
package for more fixable staged programming. While `GeneralizedGenerated.jl` is
package for more flexible staged programming. While `GeneralizedGenerated.jl` is
more powerful, `RuntimeGeneratedFunctions.jl` handles large expressions better.

## Tutorials and Documentation
Expand Down Expand Up @@ -108,9 +108,10 @@ From a constructed RuntimeGeneratedFunction, you can retrieve the expressions us
`RuntimeGeneratedFunctions.get_expression` command. For example:

```julia
julia> RuntimeGeneratedFunctions.get_expression(rgf)
ex = :((x) -> x^2)
rgf = @RuntimeGeneratedFunction(ex)
julia> RuntimeGeneratedFunctions.get_expression(rgf)
:((x,) -> x ^ 2)
```

This can be used to get the expression even if `drop_expr` has been performed.
Expand Down
35 changes: 35 additions & 0 deletions src/RuntimeGeneratedFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ struct RuntimeGeneratedFunction{argnames, cache_tag, context_tag, id, B} <: Func
end
end

"""
drop_expr(rgf::RuntimeGeneratedFunction)

Return a new `RuntimeGeneratedFunction` that does not hold a reference to the
function body expression. This allows the expression AST to be garbage collected
while keeping the function callable.

The expression can still be retrieved later using [`get_expression`](@ref) as long
as at least one `RuntimeGeneratedFunction` with the same body exists.

# Examples
```julia
ex = :((x) -> x^2)
rgf = @RuntimeGeneratedFunction(ex)
rgf_dropped = drop_expr(rgf)
rgf_dropped(2) # Still works, returns 4
```
"""
function drop_expr(::RuntimeGeneratedFunction{
a,
cache_tag,
Expand Down Expand Up @@ -334,6 +352,23 @@ function closures_to_opaque(ex::Expr, return_type = nothing)
return Expr(head, Any[closures_to_opaque(x, return_type) for x in args]...)
end

"""
get_expression(rgf::RuntimeGeneratedFunction)

Retrieve the function expression from a `RuntimeGeneratedFunction`.

This works even if [`drop_expr`](@ref) has been called on the function, as long as
the expression is still in the cache (i.e., at least one `RuntimeGeneratedFunction`
with the same body exists).

# Examples
```julia
ex = :((x) -> x^2)
rgf = @RuntimeGeneratedFunction(ex)
RuntimeGeneratedFunctions.get_expression(rgf)
# Returns: :((x,) -> x ^ 2)
```
"""
function get_expression(rgf::RuntimeGeneratedFunction{argnames, cache_tag,
context_tag, id, B}) where {
argnames,
Expand Down
Loading