1
module CacheStore
2
using ..SymbolServer: VarRef, FakeTypeName, FakeTypeofBottom, FakeTypeVar, FakeUnion, FakeUnionAll
3
using ..SymbolServer: ModuleStore, Package, FunctionStore, MethodStore, DataTypeStore, GenericStore
4

5
const NothingHeader = 0x01
6
const SymbolHeader = 0x02
7
const CharHeader = 0x03
8
const IntegerHeader = 0x04
9
const StringHeader = 0x05
10
const VarRefHeader = 0x06
11
const FakeTypeNameHeader = 0x07
12
const FakeTypeofBottomHeader = 0x08
13
const FakeTypeVarHeader = 0x09
14
const FakeUnionHeader = 0x0a
15
const FakeUnionAllHeader = 0xb
16
const ModuleStoreHeader = 0x0c
17
const MethodStoreHeader = 0x0d
18
const FunctionStoreHeader = 0x0e
19
const DataTypeStoreHeader = 0x0f
20
const GenericStoreHeader = 0x10
21
const PackageHeader = 0x11
22
const TrueHeader = 0x12
23
const FalseHeader = 0x13
24
const TupleHeader = 0x14
25

26

27
function write(io, x::VarRef)
28 30
    Base.write(io, VarRefHeader)
29 30
    write(io, x.parent)
30 30
    write(io, x.name)
31
end
32
function write(io, x::Nothing)
33 30
    Base.write(io, NothingHeader)
34
end
35 0
function write(io, x::Char)
36 0
    Base.write(io, CharHeader)
37 0
    Base.write(io, UInt32(x))
38
end
39
function write(io, x::Bool)
40 30
    x ? Base.write(io, TrueHeader) : Base.write(io, FalseHeader)
41
end
42
function write(io, x::Int)
43 30
    Base.write(io, IntegerHeader)
44 30
    Base.write(io, x)
45
end
46
function write(io, x::Symbol)
47 30
    Base.write(io, SymbolHeader)
48 25
    Base.write(io, sizeof(x))
49 30
    Base.write(io, String(x))
50
end
51 0
function write(io, x::NTuple{N,Any}) where N
52 0
    Base.write(io, TupleHeader)
53 0
    Base.write(io, N)
54 0
    for i = 1:N
55 0
        write(io, x[i])
56
    end
57
end
58
function write(io, x::String)
59 30
    Base.write(io, StringHeader)
60 25
    Base.write(io, sizeof(x))
61 30
    Base.write(io, x)
62
end
63
function write(io, x::FakeTypeName)
64 30
    Base.write(io, FakeTypeNameHeader)
65 30
    write(io, x.name)
66 30
    write_vector(io, x.parameters)
67
end
68 30
write(io, x::FakeTypeofBottom) = Base.write(io, FakeTypeofBottomHeader)
69
function write(io, x::FakeTypeVar)
70 30
    Base.write(io, FakeTypeVarHeader)
71 30
    write(io, x.name)
72 30
    write(io, x.lb)
73 30
    write(io, x.ub)
74
end
75
function write(io, x::FakeUnion)
76 30
    Base.write(io, FakeUnionHeader)
77 30
    write(io, x.a)
78 30
    write(io, x.b)
79
end
80
function write(io, x::FakeUnionAll)
81 30
    Base.write(io, FakeUnionAllHeader)
82 30
    write(io, x.var)
83 30
    write(io, x.body)
84
end
85

86
function write(io, x::MethodStore)
87 30
    Base.write(io, MethodStoreHeader)
88 30
    write(io, x.name)
89 30
    write(io, x.mod)
90 30
    write(io, x.file)
91 25
    Base.write(io, x.line)
92 25
    Base.write(io, length(x.sig))
93 30
    for p in x.sig
94 30
        write(io, p[1])
95 30
        write(io, p[2])
96
    end
97 30
    write_vector(io, x.kws)
98 30
    write(io, x.rt)
99
end
100

101
function write(io, x::FunctionStore)
102 30
    Base.write(io, FunctionStoreHeader)
103 30
    write(io, x.name)
104 30
    write_vector(io, x.methods)
105 30
    write(io, x.doc)
106 30
    write(io, x.extends)
107 30
    write(io, x.exported)
108
end
109

110
function write(io, x::DataTypeStore)
111 30
    Base.write(io, DataTypeStoreHeader)
112 30
    write(io, x.name)
113 30
    write(io, x.super)
114 30
    write_vector(io, x.parameters)
115 30
    write_vector(io, x.types)
116 30
    write_vector(io, x.fieldnames)
117 30
    write_vector(io, x.methods)
118 30
    write(io, x.doc)
119 30
    write(io, x.exported)
120
end
121

122
function write(io, x::GenericStore)
123 30
    Base.write(io, GenericStoreHeader)
124 30
    write(io, x.name)
125 30
    write(io, x.typ)
126 30
    write(io, x.doc)
127 30
    write(io, x.exported)
128
end
129

130
function write(io, x::ModuleStore)
131 30
    Base.write(io, ModuleStoreHeader)
132 30
    write(io, x.name)
133 25
    Base.write(io, length(x.vals))
134 30
    for p in x.vals
135 30
        write(io, p[1])
136 30
        write(io, p[2])
137
    end
138 30
    write(io, x.doc)
139 30
    write(io, x.exported)
140 30
    write_vector(io, x.exportednames)
141 30
    write_vector(io, x.used_modules)
142
end
143

144
function write(io, x::Package)
145 30
    Base.write(io, PackageHeader)
146 30
    write(io, x.name)
147 30
    write(io, x.val)
148 25
    Base.write(io, UInt128(x.uuid))
149 30
    Base.write(io, x.sha === nothing ? zeros(UInt8, 32) : x.sha)
150
end
151

152
function write_vector(io, x)
153 30
    Base.write(io, length(x))
154 30
    for p in x
155 30
        write(io, p)
156
    end
157
end
158

159
function read(io, t = Base.read(io, UInt8))
160 30
    if t === VarRefHeader
161 30
        VarRef(read(io), read(io))
162 30
    elseif t === NothingHeader
163 30
        nothing
164 30
    elseif t === SymbolHeader
165 25
        n = Base.read(io, Int)
166 25
        out = UInt8[]
167 25
        readbytes!(io, out, n)
168 30
        Symbol(String(out))
169 30
    elseif t === StringHeader
170 25
        n = Base.read(io, Int)
171 25
        out = UInt8[]
172 25
        readbytes!(io, out, n)
173 30
        String(out)
174 30
    elseif t === CharHeader
175 0
        Char(Base.read(io, UInt32))
176 30
    elseif t === IntegerHeader
177 30
        Base.read(io, Int)
178 30
    elseif t === FakeTypeNameHeader
179 30
        FakeTypeName(read(io), read_vector(io, Any))
180 30
    elseif t === FakeTypeofBottomHeader
181 30
        FakeTypeofBottom()
182 30
    elseif t === FakeTypeVarHeader
183 30
        FakeTypeVar(read(io), read(io), read(io))
184 30
    elseif t === FakeUnionHeader
185 30
        FakeUnion(read(io), read(io))
186 30
    elseif t === FakeUnionAllHeader
187 30
        FakeUnionAll(read(io), read(io))
188 30
    elseif t === MethodStoreHeader
189 30
        name = read(io)
190 30
        mod = read(io)
191 30
        file = read(io)
192 25
        line = Base.read(io, UInt32)
193 25
        nsig = Base.read(io, Int)
194 25
        sig = Any[]
195 30
        for i = 1:nsig
196 30
            push!(sig, read(io)=>read(io))
197
        end
198 30
        kws = read_vector(io, Symbol)
199 30
        rt = read(io)
200 30
        MethodStore(name, mod, file, line, sig, kws, rt)
201 30
    elseif t === FunctionStoreHeader
202 30
        FunctionStore(read(io), read_vector(io, MethodStore), read(io), read(io), read(io))
203 30
    elseif t === DataTypeStoreHeader
204 30
        DataTypeStore(read(io), read(io), read_vector(io, Any), read_vector(io, Any), read_vector(io, Any), read_vector(io, MethodStore), read(io), read(io))
205 30
    elseif t === GenericStoreHeader
206 30
        GenericStore(read(io), read(io), read(io), read(io))
207 30
    elseif t === ModuleStoreHeader
208 30
        name = read(io)
209 25
        n = Base.read(io, Int)
210 30
        vals = Dict{Symbol,Any}()
211 30
        for i = 1:n
212 30
            k = read(io)
213 30
            v = read(io)
214 30
            vals[k] = v
215
        end
216 30
        doc = read(io)
217 30
        exported = read(io)
218 30
        exportednames = read_vector(io, Symbol)
219 30
        used_modules = read_vector(io, Symbol)
220 30
        ModuleStore(name, vals, doc, exported, exportednames, used_modules)
221 30
    elseif t === TrueHeader
222 30
        true
223 30
    elseif t === FalseHeader
224 30
        false
225 30
    elseif t === TupleHeader
226 0
        N = Base.read(io, Int)
227 0
        ntuple(i->read(io), N)
228 30
    elseif t === PackageHeader
229 30
        name = read(io)
230 30
        val = read(io)
231 25
        uuid = Base.UUID(Base.read(io, UInt128))
232 25
        sha = Base.read(io, 32)
233 30
        Package(name, val, uuid, all(x == 0x00 for x in sha) ? nothing : sha)
234
    else
235 0
        error("HIYA: $t")
236
    end
237
end
238

239
function read_vector(io, T)
240 30
    n = Base.read(io, Int)
241 30
    v = T[]
242 30
    for i = 1:n
243 30
        push!(v, read(io))
244
    end
245 30
    v
246
end
247

248 0
function storeunstore(x)
249 0
    io = IOBuffer()
250 0
    write(io, x)
251 0
    bs = take!(io)    
252 0
    read(IOBuffer(bs))
253
end
254
end

Read our documentation on viewing source code .

Loading