Skip to content

Commit 10db904

Browse files
mlechuc42f
andcommitted
Hook tests
Co-authored-by: Claire Foster <[email protected]>
1 parent c03287e commit 10db904

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/hooks.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ function core_lowering_hook(@nospecialize(code), mod::Module,
1010
# e.g. LineNumberNode, integer...
1111
return Core.svec(code)
1212
end
13-
st0 = code isa Expr ? expr_to_syntaxtree(code) : code
13+
14+
# TODO: fix in base
15+
file = file isa Ptr{UInt8} ? unsafe_string(file) : file
16+
line = !(line isa Int64) ? Int64(line) : line
17+
18+
st0 = code isa Expr ? expr_to_syntaxtree(code, LineNumberNode(line, file)) : code
1419
try
1520
ctx1, st1 = expand_forms_1( mod, st0)
1621
ctx2, st2 = expand_forms_2( ctx1, st1)

test/hooks.jl

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const JL = JuliaLowering
2+
3+
@testset "hooks" begin
4+
test_mod = Module()
5+
6+
@testset "`core_lowering_hook`" begin
7+
# Non-AST types are often sent through lowering
8+
stuff = Any[LineNumberNode(1), 123, 123.123, true, "foo", test_mod]
9+
for s in stuff
10+
@test JL.core_lowering_hook(s, test_mod) == Core.svec(s)
11+
end
12+
13+
st = parseall(JL.SyntaxTree, "function f_st end")
14+
out = core_lowering_hook(st, test_mod)
15+
@test out isa Core.Svec && out[1] isa Expr
16+
Core.eval(test_mod, out)
17+
@test isdefined(test_mod, :f_st)
18+
19+
ex = parseall(Expr, "function f_ex end")
20+
@test core_lowering_hook(ex, test_mod)
21+
@test out isa Core.Svec && out[1] isa Expr
22+
Core.eval(test_mod, out)
23+
@test isdefined(test_mod, :f_ex)
24+
end
25+
26+
@testset "integration: `JuliaLowering.activate!`" begin
27+
prog = parseall(Expr, "global asdf = 1")
28+
JuliaLowering.activate!()
29+
out = Core.eval(test_mod, prog)
30+
JuliaLowering.activate!(false)
31+
@test out === 1
32+
@test isdefined(test_mod, :asdf)
33+
34+
prog = parseall(Expr, "module M; x = 1; end")
35+
JuliaLowering.activate!()
36+
out = Core.eval(test_mod, prog)
37+
JuliaLowering.activate!(false)
38+
@test out isa Module
39+
@test isdefined(test_mod, :M)
40+
@test isdefined(test_mod.M, :x)
41+
end
42+
end

0 commit comments

Comments
 (0)