Skip to content

Commit a4ae7bf

Browse files
authored
Merge pull request #64 from JuliaDebug/teh/modules
Find modules in loaded packages
2 parents ca07ee9 + 2b7c63b commit a4ae7bf

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

src/JuliaInterpreter.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,16 @@ function split_expressions!(modexs, docexprs, lex::Expr, mod::Module, ex::Expr;
474474
if ex.head == :toplevel || ex.head == :block
475475
split_expressions!(modexs, docexprs, lex, mod, ex.args; extract_docexprs=extract_docexprs, filename=filename)
476476
elseif ex.head == :module
477-
modname = ex.args[2]::Symbol
478-
newmod = isdefined(mod, modname) ? getfield(mod, modname) : Core.eval(mod, :(module $modname end))
477+
newname = ex.args[2]::Symbol
478+
if isdefined(mod, newname)
479+
newmod = getfield(mod, newname)
480+
else
481+
if (id = Base.identify_package(mod, String(newname))) !== nothing
482+
newmod = Base.root_module(id)
483+
else
484+
newmod = Core.eval(mod, :(module $newname end))
485+
end
486+
end
479487
split_expressions!(modexs, docexprs, lex, newmod, ex.args[3]; extract_docexprs=extract_docexprs, filename=filename)
480488
elseif extract_docexprs && isdocexpr(ex)
481489
docexs = get(docexprs, mod, nothing)

test/toplevel.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
using JuliaInterpreter, Test
22

3+
module JIVisible
4+
module JIInvisible
5+
end
6+
end
7+
38
@testset "Basics" begin
49
@test JuliaInterpreter.isdocexpr(:(@doc "string" sum))
510
@test JuliaInterpreter.isdocexpr(:(Core.@doc "string" sum))
@@ -11,6 +16,38 @@ using JuliaInterpreter, Test
1116
end
1217
@test JuliaInterpreter.isdocexpr(ex.args[2])
1318
@test !JuliaInterpreter.isdocexpr(:(1+1))
19+
20+
@test !isdefined(Main, :JIInvisible)
21+
JuliaInterpreter.split_expressions(JIVisible, :(module JIInvisible f() = 1 end))
22+
@test !isdefined(Main, :JIInvisible)
23+
mktempdir() do path
24+
push!(LOAD_PATH, path)
25+
open(joinpath(path, "TmpPkg1.jl"), "w") do io
26+
println(io, """
27+
module TmpPkg1
28+
using TmpPkg2
29+
end
30+
""")
31+
end
32+
open(joinpath(path, "TmpPkg2.jl"), "w") do io
33+
println(io, """
34+
module TmpPkg2
35+
f() = 1
36+
end
37+
""")
38+
end
39+
@eval using TmpPkg1
40+
# Every package is technically parented in Main but the name may not be visible in Main
41+
@test isdefined(@__MODULE__, :TmpPkg1)
42+
@test !isdefined(@__MODULE__, :TmpPkg2)
43+
JuliaInterpreter.split_expressions(Main, quote
44+
module TmpPkg2
45+
f() = 2
46+
end
47+
end)
48+
@test isdefined(@__MODULE__, :TmpPkg1)
49+
@test !isdefined(@__MODULE__, :TmpPkg2)
50+
end
1451
end
1552

1653
module Toplevel end

0 commit comments

Comments
 (0)