Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/anonymous.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ end

baremodule __deserialized_types__ end

function newstruct_raw(cache, ::Type{TypeName}, d)
name = raise_recursive(d[:data][2], cache)
function newstruct_raw(cache, ::Type{TypeName}, d, init)
name = raise_recursive(d[:data][2], cache, init)
name = isdefined(__deserialized_types__, name) ? gensym() : name
tn = ccall(:jl_new_typename_in, Ref{Core.TypeName}, (Any, Any),
name, __deserialized_types__)
cache[d] = tn
names, super, parameters, types, has_instance,
abstr, mutabl, ninitialized = map(x -> raise_recursive(x, cache), d[:data][3:end-1])
abstr, mutabl, ninitialized = map(x -> raise_recursive(x, cache, init), d[:data][3:end-1])
tn.names = names
ndt = ccall(:jl_new_datatype, Any, (Any, Any, Any, Any, Any, Any, Cint, Cint, Cint),
tn, tn.module, super, parameters, names, types,
Expand All @@ -56,7 +56,7 @@ function newstruct_raw(cache, ::Type{TypeName}, d)
# use setfield! directly to avoid `fieldtype` lowering expecting to see a Singleton object already on ty
Core.setfield!(ty, :instance, ccall(:jl_new_struct, Any, (Any, Any...), ty))
end
mt = raise_recursive(d[:data][end], cache)
mt = raise_recursive(d[:data][end], cache, init)
if mt != nothing
mtname, defs, maxa, kwsorter = mt
tn.mt = ccall(:jl_new_method_table, Any, (Any, Any), name, tn.module)
Expand Down
18 changes: 9 additions & 9 deletions src/extensions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ tags[:svec] = d -> Core.svec(d[:data]...)

ref(path::Symbol...) = BSONDict(:tag => "ref", :path => Base.string.([path...]))

resolve(fs) = reduce((m, f) -> getfield(m, Symbol(f)), fs; init = Main)
resolve(fs, init) = reduce((m, f) -> getfield(m, Symbol(f)), fs; init = init)

tags[:ref] = d -> resolve(d[:path])
tags[:ref] = (d, init) -> resolve(d[:path], init)

function modpath(x::Module)
y = parentmodule(x)
Expand All @@ -45,7 +45,7 @@ end
constructtype(T, Ts) = (length(Ts) == 0) ? T : T{Ts...}
constructtype(T::Type{Tuple}, Ts) = T{Ts...}

tags[:datatype] = d -> constructtype(resolve(d[:name]), d[:params])
tags[:datatype] = (d, init) -> constructtype(resolve(d[:name], init), d[:params])

lower(v::UnionAll) =
BSONDict(:tag => "unionall",
Expand Down Expand Up @@ -112,9 +112,9 @@ function newstruct(T, xs...)
end
end

function newstruct_raw(cache, T, d)
function newstruct_raw(cache, T, d, init)
x = cache[d] = initstruct(T)
fs = map(x -> raise_recursive(x, cache), d[:data])
fs = map(x -> raise_recursive(x, cache, init), d[:data])
return newstruct!(x, fs...)
end

Expand All @@ -127,10 +127,10 @@ tags[:struct] = d ->

iscyclic(T) = ismutable(T)

raise[:struct] = function (d, cache)
T = d[:type] = raise_recursive(d[:type], cache)
iscyclic(T) || return _raise_recursive(d, cache)
return newstruct_raw(cache, T, d)
raise[:struct] = function (d, cache, init)
T = d[:type] = raise_recursive(d[:type], cache, init)
iscyclic(T) || return _raise_recursive(d, cache, init)
return newstruct_raw(cache, T, d, init)
end

lower(v::Type{Union{}}) = BSONDict(:tag=>"jl_bottom_type")
Expand Down
26 changes: 15 additions & 11 deletions src/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,34 +74,38 @@ const tags = Dict{Symbol,Function}()

const raise = Dict{Symbol,Function}()

function _raise_recursive(d::AbstractDict, cache)
function _raise_recursive(d::AbstractDict, cache, init)
if haskey(d, :tag) && haskey(tags, Symbol(d[:tag]))
cache[d] = tags[Symbol(d[:tag])](applychildren!(x -> raise_recursive(x, cache), d))
if Symbol(d[:tag]) in (:ref, :datatype)
cache[d] = tags[Symbol(d[:tag])](applychildren!(x -> raise_recursive(x, cache, init), d), init)
else
cache[d] = tags[Symbol(d[:tag])](applychildren!(x -> raise_recursive(x, cache, init), d))
end
else
cache[d] = d
applychildren!(x -> raise_recursive(x, cache), d)
applychildren!(x -> raise_recursive(x, cache, init), d)
end
end

function raise_recursive(d::AbstractDict, cache)
function raise_recursive(d::AbstractDict, cache, init)
haskey(cache, d) && return cache[d]
haskey(d, :tag) && haskey(raise, Symbol(d[:tag])) && return raise[Symbol(d[:tag])](d, cache)
_raise_recursive(d::AbstractDict, cache)
haskey(d, :tag) && haskey(raise, Symbol(d[:tag])) && return raise[Symbol(d[:tag])](d, cache, init)
_raise_recursive(d::AbstractDict, cache, init)
end

function raise_recursive(v::BSONArray, cache)
function raise_recursive(v::BSONArray, cache, init)
cache[v] = v
applychildren!(x -> raise_recursive(x, cache), v)
applychildren!(x -> raise_recursive(x, cache, init), v)
end

raise_recursive(x, cache) = x
raise_recursive(x, cache, init) = x

raise_recursive(x) = raise_recursive(x, IdDict())
raise_recursive(x, init) = raise_recursive(x, IdDict(), init)

parse(io::IO) = backrefs!(parse_doc(io))
parse(path::String) = open(parse, path)

load(x) = raise_recursive(parse(x))
load(x, init=Main) = raise_recursive(parse(x), init)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be init=@__MODULE__?


function roundtrip(x)
buf = IOBuffer()
Expand Down