Skip to content

Commit

Permalink
add support for preset current_scope
Browse files Browse the repository at this point in the history
There are cases where we invoke JuliaInterpreter from a context with
some scope is set (e.g. `@test_logs (...) (@InterPret ...)`).
  • Loading branch information
aviatesk committed Mar 20, 2024
1 parent 55e33d0 commit a708dcc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
6 changes: 5 additions & 1 deletion src/construct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,15 @@ function prepare_framedata(framecode, argvals::Vector{Any}, lenv::SimpleVector=e
ssavalues = Vector{Any}(undef, ng)
sparams = Vector{Any}(undef, 0)
exception_frames = Int[]
current_scopes = Any[]
current_scopes = Scope[]
last_reference = Vector{Int}(undef, ns)
callargs = Any[]
last_exception = Ref{Any}(_INACTIVE_EXCEPTION.instance)
end
@static if isdefined(Base, :current_scope)

Check warning on line 295 in src/construct.jl

View check run for this annotation

Codecov / codecov/patch

src/construct.jl#L295

Added line #L295 was not covered by tests
current_scope = Core.current_scope()
current_scope !== nothing && push!(current_scopes, current_scope)
end
fill!(last_reference, 0)
if isa(framecode.scope, Method)
meth = framecode.scope::Method
Expand Down
42 changes: 31 additions & 11 deletions test/interpret.jl
Original file line number Diff line number Diff line change
Expand Up @@ -998,29 +998,49 @@ func_arrayref(a, i) = Core.arrayref(true, a, i)
@test 2 == @interpret func_arrayref([1,2,3], 2)

@static if isdefined(Base, :ScopedValues)
const sval = ScopedValue(1)
@test 1 == @interpret getindex(sval)

const sval1 = Base.ScopedValue(1)
const sval2 = Base.ScopedValue(1)
@test 1 == @interpret getindex(sval1)

# current_scope support for interpretation
sval_func1() = @with sval => 2 begin
return sval[]
sval1_func1() = Base.@with sval1 => 2 begin
return sval1[]
end
@test 2 == @interpret sval1_func1()
sval12_func1() = Base.@with sval1 => 2 begin
Base.@with sval2 => 3 begin
return sval1[], sval2[]
end
end
@test 2 == @interpret sval_func1()
@test (2, 3) == @interpret sval12_func1()

# current_scope support for compiled calls
_sval_func2() = sval[]
sval_func2() = @with sval => 2 begin
return _sval_func2()
_sval1_func2() = sval1[]
sval1_func2() = Base.@with sval1 => 2 begin
return _sval1_func2()
end
let m = only(methods(_sval_func2))
let m = only(methods(_sval1_func2))
push!(JuliaInterpreter.compiled_methods, m)
try
@test 2 == @interpret sval_func2()
@test 2 == @interpret sval1_func2()
finally
delete!(JuliaInterpreter.compiled_methods, m)
end
end
let frame = JuliaInterpreter.enter_call(sval_func2)
let frame = JuliaInterpreter.enter_call(sval1_func2)
@test 2 == JuliaInterpreter.finish_and_return!(Compiled(), frame)
end

# preset `current_scope` support
@test 2 == Base.@with sval1 => 2 begin
@interpret getindex(sval1)
end
@test (2, 3) == Base.@with sval1 => 2 sval2 => 3 begin
@interpret(getindex(sval1)), @interpret(getindex(sval2))
end
@test (2, 3) == Base.@with sval1 => 2 begin Base.@with sval2 => 3 begin
@interpret(getindex(sval1)), @interpret(getindex(sval2))
end end

end

0 comments on commit a708dcc

Please sign in to comment.