Skip to content

Commit 8516156

Browse files
authored
Make enable_in_core! safe for use in precompilation (#50)
Move the hacky code for overriding Core._parse from the sysimage init module into JuliaSyntax.jl proper, to allow it to be reused as necessary.
1 parent b1fa39e commit 8516156

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

src/hooks.jl

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
# This file provides an adaptor to match the API expected by the Julia runtime
22
# code in the binding Core._parse
33

4+
function _set_core_parse_hook(parser)
5+
# HACK! Fool the runtime into allowing us to set Core._parse, even during
6+
# incremental compilation. (Ideally we'd just arrange for Core._parse to be
7+
# set to the JuliaSyntax parser. But how do we signal that to the dumping
8+
# code outside of the initial creation of Core?)
9+
i = findfirst(==(:incremental), fieldnames(Base.JLOptions))
10+
ptr = convert(Ptr{fieldtype(Base.JLOptions, i)},
11+
cglobal(:jl_options, Base.JLOptions) + fieldoffset(Base.JLOptions, i))
12+
incremental = unsafe_load(ptr)
13+
if incremental != 0
14+
unsafe_store!(ptr, 0)
15+
end
16+
17+
Base.eval(Core, :(_parse = $parser))
18+
19+
if incremental != 0
20+
unsafe_store!(ptr, incremental)
21+
end
22+
end
23+
424
# Use caller's world age.
525
const _caller_world = typemax(UInt)
626
const _parser_world_age = Ref{UInt}(_caller_world)
@@ -152,8 +172,7 @@ function enable_in_core!(enable=true; freeze_world_age = true,
152172
close(_debug_log[])
153173
_debug_log[] = nothing
154174
end
155-
parser = enable ? core_parser_hook : _default_parser
156-
Base.eval(Core, :(_parse = $parser))
175+
_set_core_parse_hook(enable ? core_parser_hook : _default_parser)
157176
nothing
158177
end
159178

sysimage/JuliaSyntaxCore/src/JuliaSyntaxCore.jl

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,8 @@ module JuliaSyntaxCore
55

66
using JuliaSyntax
77

8-
import Base: JLOptions
9-
108
function __init__()
11-
# HACK! Fool the runtime into allowing us to set Core._parse, even during
12-
# incremental compilation. (Ideally we'd just arrange for Core._parse to be
13-
# set to the JuliaSyntax parser. But how do we signal that to the dumping
14-
# code outside of the initial creation of Core?)
15-
i = findfirst(==(:incremental), fieldnames(JLOptions))
16-
ptr = convert(Ptr{fieldtype(JLOptions, i)},
17-
cglobal(:jl_options, JLOptions) + fieldoffset(JLOptions, i))
18-
incremental = unsafe_load(ptr)
19-
incremental == 0 || unsafe_store!(ptr, 0)
20-
219
JuliaSyntax.enable_in_core!()
22-
23-
incremental == 0 || unsafe_store!(ptr, incremental)
2410
end
2511

2612
end

0 commit comments

Comments
 (0)