@@ -81,6 +81,24 @@ struct RuntimeGeneratedFunction{argnames, cache_tag, context_tag, id, B} <: Func
8181 end
8282end
8383
84+ """
85+ drop_expr(rgf::RuntimeGeneratedFunction)
86+
87+ Return a new `RuntimeGeneratedFunction` that does not hold a reference to the
88+ function body expression. This allows the expression AST to be garbage collected
89+ while keeping the function callable.
90+
91+ The expression can still be retrieved later using [`get_expression`](@ref) as long
92+ as at least one `RuntimeGeneratedFunction` with the same body exists.
93+
94+ # Examples
95+ ```julia
96+ ex = :((x) -> x^2)
97+ rgf = @RuntimeGeneratedFunction(ex)
98+ rgf_dropped = drop_expr(rgf)
99+ rgf_dropped(2) # Still works, returns 4
100+ ```
101+ """
84102function drop_expr (:: RuntimeGeneratedFunction {
85103 a,
86104 cache_tag,
@@ -334,6 +352,23 @@ function closures_to_opaque(ex::Expr, return_type = nothing)
334352 return Expr (head, Any[closures_to_opaque (x, return_type) for x in args]. .. )
335353end
336354
355+ """
356+ get_expression(rgf::RuntimeGeneratedFunction)
357+
358+ Retrieve the function expression from a `RuntimeGeneratedFunction`.
359+
360+ This works even if [`drop_expr`](@ref) has been called on the function, as long as
361+ the expression is still in the cache (i.e., at least one `RuntimeGeneratedFunction`
362+ with the same body exists).
363+
364+ # Examples
365+ ```julia
366+ ex = :((x) -> x^2)
367+ rgf = @RuntimeGeneratedFunction(ex)
368+ RuntimeGeneratedFunctions.get_expression(rgf)
369+ # Returns: :((x,) -> x ^ 2)
370+ ```
371+ """
337372function get_expression (rgf:: RuntimeGeneratedFunction {argnames, cache_tag,
338373 context_tag, id, B}) where {
339374 argnames,
0 commit comments