Skip to content

Commit 3548a31

Browse files
committed
Clean up fixed world age mechanism to use closures
Using a closure here to hold the fixed world age seems nicer than a global within the JuliaSyntax module.
1 parent 200145f commit 3548a31

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

src/hooks.jl

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ function _incomplete_tag(n::SyntaxNode, codelen)
7676
end
7777

7878
#-------------------------------------------------------------------------------
79-
if isdefined(Core, :_setparser!)
80-
const _set_core_parse_hook = Core._setparser!
81-
else
82-
function _set_core_parse_hook(parser)
79+
function _set_core_parse_hook(parser)
80+
@static if isdefined(Core, :_setparser!)
81+
Core._setparser!(parser)
82+
else
8383
# HACK! Fool the runtime into allowing us to set Core._parse, even during
8484
# incremental compilation. (Ideally we'd just arrange for Core._parse to be
8585
# set to the JuliaSyntax parser. But how do we signal that to the dumping
@@ -100,18 +100,20 @@ else
100100
end
101101
end
102102

103-
# Use caller's world age.
104-
const _latest_world = typemax(UInt)
105-
const _parser_world_age = Ref{UInt}(_latest_world)
106103

107-
function core_parser_hook(code, filename, lineno, offset, options)
108-
# NB: We need an inference barrier of one type or another here to prevent
109-
# invalidations. The invokes provide this currently.
110-
if _parser_world_age[] != _latest_world
111-
Base.invoke_in_world(_parser_world_age[], _core_parser_hook,
112-
code, filename, lineno, offset, options)
104+
# Wrap the function `f` so that it's always invoked in the given `world_age`
105+
#
106+
# NB: We need an inference barrier of one type or another here to prevent
107+
# invalidations. The invokes provide this currently.
108+
function fix_world_age(f, world_age::UInt)
109+
if world_age == typemax(UInt)
110+
function invoke_latestworld(args...; kws...)
111+
Base.invokelatest(f, args...; kws...)
112+
end
113113
else
114-
Base.invokelatest(_core_parser_hook, code, filename, lineno, offset, options)
114+
function invoke_fixedworld(args...; kws...)
115+
Base.invoke_in_world(world_age, f, args...; kws...)
116+
end
115117
end
116118
end
117119

@@ -293,14 +295,18 @@ function enable_in_core!(enable=true; freeze_world_age = true,
293295
if VERSION < v"1.6"
294296
error("Cannot use JuliaSyntax as the main Julia parser in Julia version $VERSION < 1.6")
295297
end
296-
_parser_world_age[] = freeze_world_age ? Base.get_world_counter() : _latest_world
297298
if enable && !isnothing(debug_filename)
298299
_debug_log[] = open(debug_filename, "w")
299300
elseif !enable && !isnothing(_debug_log[])
300301
close(_debug_log[])
301302
_debug_log[] = nothing
302303
end
303-
_set_core_parse_hook(enable ? core_parser_hook : _default_parser)
304+
if enable
305+
world_age = freeze_world_age ? Base.get_world_counter() : typemax(UInt)
306+
_set_core_parse_hook(fix_world_age(_core_parser_hook, world_age))
307+
else
308+
_set_core_parse_hook(_default_parser)
309+
end
304310
nothing
305311
end
306312

0 commit comments

Comments
 (0)