Skip to content

Commit 8caab54

Browse files
committed
Utilities for compiling a JuliaSyntax sysimage
1 parent 958a224 commit 8caab54

File tree

6 files changed

+93
-6
lines changed

6 files changed

+93
-6
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,22 @@ julia> parseall(Expr, "(x + y)*z")
103103
:($(Expr(:toplevel, :((x + y) * z))))
104104
```
105105

106+
# Using JuliaSyntax as the default parser
107+
108+
To use JuliaSyntax as the default Julia parser to `include()` files,
109+
parse code with `Meta.parse()`, etc, call
110+
111+
```
112+
julia> JuliaSyntax.enable_in_core!()
113+
```
114+
115+
This causes some startup latency, so to reduce that you can create a custom
116+
system image by running the code in `./sysimage/compile.jl` as a Julia script
117+
(or directly using the shell, on unix). Then use `julia -J $resulting_sysimage`.
118+
119+
Using a custom sysimage has the advantage that package precompilation will also
120+
go through the JuliaSyntax parser.
121+
106122
# Parser implementation
107123

108124
Our goal is to losslessly represent the source text with a tree; this may be

src/hooks.jl

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,8 @@ flisp parser for all parsing work.
6767
That is, JuliaSyntax will be used for `include()` `Meta.parse()`, the REPL, etc.
6868
"""
6969
function enable_in_core!(enable=true)
70-
if enable
71-
# TODO: Use invoke_in_world to freeze the world age at the time this was enabled.
72-
Base.eval(Core, :(_parse = $core_parser_hook))
73-
else
74-
Base.eval(Core, :(_parse = Core.Compiler.fl_parse))
75-
end
70+
parser = enable ? core_parser_hook : Core.Compiler.fl_parse
71+
Base.eval(Core, :(_parse = $parser))
7672
nothing
7773
end
7874

sysimage/JuliaSyntaxCore/Project.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name = "JuliaSyntaxCore"
2+
uuid = "05e5f68f-ccd0-4d84-a81a-f557a333a331"
3+
authors = ["Chris Foster <[email protected]> and contributors"]
4+
version = "0.1.0"
5+
6+
[compat]
7+
julia = "1.6"
8+
9+
[deps]
10+
JuliaSyntax = "70703baa-626e-46a2-a12c-08ffd08c73b4"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module JuliaSyntaxCore
2+
3+
# A tiny module to hold initialization code for JuliaSyntax.jl integration with
4+
# the runtime.
5+
6+
using JuliaSyntax
7+
8+
import Base: JLOptions
9+
10+
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+
21+
JuliaSyntax.enable_in_core!()
22+
23+
incremental == 0 || unsafe_store!(ptr, incremental)
24+
end
25+
26+
end

sysimage/compile.jl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
#=
3+
[[ $1 == +* ]] && juliaup_arg=$1 && shift # release channel for juliaup
4+
exec julia ${juliaup_arg} --startup-file=no -e 'include(popfirst!(ARGS))' "$0" "$@"
5+
=#
6+
7+
imgs_base_path = joinpath(first(DEPOT_PATH), "sysimages", "v$VERSION")
8+
mkpath(imgs_base_path)
9+
10+
using Libdl
11+
12+
cd(@__DIR__)
13+
14+
using Pkg
15+
Pkg.activate(".")
16+
Pkg.develop("JuliaSyntax")
17+
Pkg.develop(path="./JuliaSyntaxCore")
18+
19+
image_path = joinpath(imgs_base_path, "juliasyntax_sysimage."*Libdl.dlext)
20+
21+
using PackageCompiler
22+
PackageCompiler.create_sysimage(
23+
["JuliaSyntaxCore"],
24+
project=".",
25+
sysimage_path=image_path,
26+
precompile_execution_file="precompile_exec.jl",
27+
incremental=true,
28+
)
29+
30+
@info """## System image compiled!
31+
32+
Use it with `julia -J "$image_path"`
33+
"""
34+

sysimage/precompile_exec.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import JuliaSyntax
2+
Base.include(@__MODULE__(), joinpath(pkgdir(JuliaSyntax), "test", "test_utils.jl"))
3+
Base.include(@__MODULE__(), joinpath(pkgdir(JuliaSyntax), "test", "parser.jl"))
4+
JuliaSyntax.enable_in_core!()
5+
@info "Some parsing" Meta.parse("x+y+z-w .+ [a b c]")

0 commit comments

Comments
 (0)