Skip to content

Commit

Permalink
Allow caller to specify whether to eval an unlowerable thunk (#398)
Browse files Browse the repository at this point in the history
While I'm not aware of any problems caused by the former setting,
when frames are being created for the purpose of analysis rather than
execution it seems worth being able to control the evaluation.
  • Loading branch information
timholy authored May 29, 2020
1 parent 9773e9e commit 61f58ad
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/construct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ end
Prepare `expr` for evaluation in `mod`. `expr` should be a "straightforward" expression,
one that does not require special top-level handling (see [`JuliaInterpreter.split_expressions`](@ref)).
"""
function prepare_thunk(mod::Module, thunk::Expr, recursive::Bool=false)
function prepare_thunk(mod::Module, thunk::Expr, recursive::Bool=false; eval::Bool=true)
if isexpr(thunk, :thunk)
framecode = FrameCode(mod, thunk.args[1])
elseif isexpr(thunk, :error) || isexpr(thunk, :incomplete)
Expand All @@ -362,13 +362,13 @@ function prepare_thunk(mod::Module, thunk::Expr, recursive::Bool=false)
thunk = Meta.lower(mod, thunk)
if isa(thunk, Expr)
# If on 2nd attempt to lower it's still an Expr, just evaluate it
Core.eval(mod, thunk)
eval && Core.eval(mod, thunk)
return nothing
end
framecode = FrameCode(mod, thunk.args[1])
else
lwr = Meta.lower(mod, thunk)
isa(lwr, Expr) && return prepare_thunk(mod, lwr, true)
isa(lwr, Expr) && return prepare_thunk(mod, lwr, true; eval=eval)
return nothing
end
return Frame(framecode, prepare_framedata(framecode, []))
Expand Down
2 changes: 2 additions & 0 deletions test/toplevel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ end

@test JuliaInterpreter.prepare_thunk(Main, :(export foo)) === nothing
@test JuliaInterpreter.prepare_thunk(Base.Threads, :(global Condition)) === nothing
@test_throws ArgumentError JuliaInterpreter.prepare_thunk(Main, :(using NoPkgOfThisName))
@test JuliaInterpreter.prepare_thunk(Main, :(using NoPkgOfThisName); eval=false) === nothing

@test !isdefined(Main, :JIInvisible)
JuliaInterpreter.split_expressions(JIVisible, :(module JIInvisible f() = 1 end))
Expand Down

0 comments on commit 61f58ad

Please sign in to comment.