Skip to content

Commit 169bab4

Browse files
authored
Merge pull request #10 from Roboneet/update
Updates for 0.7 and 1.0
2 parents 40794f4 + ff636b3 commit 169bab4

File tree

7 files changed

+43
-37
lines changed

7 files changed

+43
-37
lines changed

REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
julia 0.6
1+
julia 0.7-beta

src/BSON.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ module BSON
22

33
export bson
44

5+
using Core: SimpleVector, TypeName
6+
57
const BSONDict = Dict{Symbol,Any}
68
const BSONArray = Vector{Any}
7-
const Primitive = Union{Void,Bool,Int32,Int64,Float64,String,Vector{UInt8},BSONDict,BSONArray}
9+
const Primitive = Union{Nothing,Bool,Int32,Int64,Float64,String,Vector{UInt8},BSONDict,BSONArray}
810

911
@enum(BSONType::UInt8,
1012
eof, double, string, document, array, binary, undefined, objectid, boolean,

src/anonymous.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
structdata(meth::Method) =
44
[meth.module, meth.name, meth.file, meth.line, meth.sig, meth.sparam_syms,
5-
meth.ambig, meth.nargs, meth.isva, meth.isstaged,
5+
meth.ambig, meth.nargs, meth.isva, meth.nospecialize,
66
Base.uncompressed_ast(meth, meth.source)]
77

8-
initstruct(::Type{Method}) = ccall(:jl_new_method_uninit, Ref{Method}, ())
8+
initstruct(::Type{Method}) = ccall(:jl_new_method_uninit, Ref{Method}, (Any,), Main)
99

1010
function newstruct!(meth::Method, mod, name, file, line, sig,
11-
sparam_syms, ambig, nargs, isva, isstaged, ast)
11+
sparam_syms, ambig, nargs, isva, nospecialize, ast)
1212
meth.module = mod
1313
meth.name = name
1414
meth.file = file
1515
meth.line = line
1616
meth.sig = sig
1717
meth.sparam_syms = sparam_syms
1818
meth.ambig = ambig
19-
meth.isstaged = isstaged
19+
meth.nospecialize = nospecialize
2020
meth.nargs = nargs
2121
meth.isva = isva
2222
meth.source = ast
@@ -41,17 +41,17 @@ baremodule __deserialized_types__ end
4141
function newstruct_raw(cache, ::Type{TypeName}, d)
4242
name = raise_recursive(d[:data][2], cache)
4343
name = isdefined(__deserialized_types__, name) ? gensym() : name
44-
tn = ccall(:jl_new_typename_in, Ref{TypeName}, (Any, Any),
44+
tn = ccall(:jl_new_typename_in, Ref{Core.TypeName}, (Any, Any),
4545
name, __deserialized_types__)
4646
cache[d] = tn
4747
names, super, parameters, types, has_instance,
4848
abstr, mutabl, ninitialized = map(x -> raise_recursive(x, cache), d[:data][3:end-1])
4949
tn.names = names
50-
ndt = ccall(:jl_new_datatype, Any, (Any, Any, Any, Any, Any, Cint, Cint, Cint),
51-
tn, super, parameters, names, types,
50+
ndt = ccall(:jl_new_datatype, Any, (Any, Any, Any, Any, Any, Any, Cint, Cint, Cint),
51+
tn, tn.module, super, parameters, names, types,
5252
abstr, mutabl, ninitialized)
5353
ty = tn.wrapper = ndt.name.wrapper
54-
ccall(:jl_set_const, Void, (Any, Any, Any), tn.module, tn.name, ty)
54+
ccall(:jl_set_const, Cvoid, (Any, Any, Any), tn.module, tn.name, ty)
5555
if has_instance && !isdefined(ty, :instance)
5656
# use setfield! directly to avoid `fieldtype` lowering expecting to see a Singleton object already on ty
5757
Core.setfield!(ty, :instance, ccall(:jl_new_struct, Any, (Any, Any...), ty))
@@ -64,7 +64,7 @@ function newstruct_raw(cache, ::Type{TypeName}, d)
6464
tn.mt.max_args = maxa
6565
for def in defs
6666
isdefined(def, :sig) || continue
67-
ccall(:jl_method_table_insert, Void, (Any, Any, Ptr{Void}), tn.mt, def, C_NULL)
67+
ccall(:jl_method_table_insert, Cvoid, (Any, Any, Ptr{Cvoid}), tn.mt, def, C_NULL)
6868
end
6969
end
7070
return tn

src/extensions.jl

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ tags[:svec] = d -> Core.svec(d[:data]...)
1717

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

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

2222
tags[:ref] = d -> resolve(d[:path])
2323

24-
modpath(x::Module) = x == Main ? [] : [modpath(module_parent(x))..., module_name(x)]
24+
modpath(x::Module) = x == Main ? [] : [modpath(parentmodule(x))..., nameof(x)]
2525

2626
ismutable(::Type{Module}) = false
2727
lower(m::Module) = ref(modpath(m)...)
@@ -56,22 +56,26 @@ tags[:unionall] = d -> UnionAll(d[:var], d[:body])
5656
lower(x::Vector{Any}) = copy(x)
5757
lower(x::Vector{UInt8}) = x
5858

59+
reinterpret_(::Type{T}, x) where T =
60+
T[reinterpret(T, x)...]
61+
5962
function lower(x::Array)
60-
ndims(x) == 1 && !isbits(eltype(x)) && return Any[x...]
63+
ndims(x) == 1 && !isbitstype(eltype(x)) && return Any[x...]
6164
BSONDict(:tag => "array", :type => eltype(x), :size => Any[size(x)...],
62-
:data => isbits(eltype(x)) ? reinterpret(UInt8, reshape(x, :)) : Any[x...])
65+
:data => isbitstype(eltype(x)) ? reinterpret_(UInt8, reshape(x, :)) : Any[x...])
6366
end
6467

6568
tags[:array] = d ->
66-
isbits(d[:type]) ?
67-
reshape(reinterpret(d[:type], d[:data]), d[:size]...) :
69+
isbitstype(d[:type]) ?
70+
reshape(reinterpret_(d[:type], d[:data]), d[:size]...) :
6871
Array{d[:type]}(reshape(d[:data], d[:size]...))
6972

7073
# Structs
7174

72-
isprimitive(T) = nfields(T) == 0 && T.size > 0
75+
isprimitive(T) = fieldcount(T) == 0 && T.size > 0
7376

74-
structdata(x) = isprimitive(typeof(x)) ? reinterpret(UInt8, [x]) : Any[getfield(x, f) for f in fieldnames(x)]
77+
structdata(x) = isprimitive(typeof(x)) ? reinterpret_(UInt8, [x]) :
78+
Any[getfield(x, f) for f in fieldnames(typeof(x))]
7579

7680
function lower(x)
7781
BSONDict(:tag => "struct", :type => typeof(x), :data => structdata(x))
@@ -82,15 +86,15 @@ initstruct(T) = ccall(:jl_new_struct_uninit, Any, (Any,), T)
8286
function newstruct!(x, fs...)
8387
for (i, f) = enumerate(fs)
8488
f = convert(fieldtype(typeof(x),i), f)
85-
ccall(:jl_set_nth_field, Void, (Any, Csize_t, Any), x, i-1, f)
89+
ccall(:jl_set_nth_field, Nothing, (Any, Csize_t, Any), x, i-1, f)
8690
end
8791
return x
8892
end
8993

9094
function newstruct(T, xs...)
91-
if isbits(T)
95+
if isbitstype(T)
9296
flds = Any[convert(fieldtype(T, i), x) for (i,x) in enumerate(xs)]
93-
return ccall(:jl_new_structv, Any, (Any,Ptr{Void},UInt32), T, flds, length(flds))
97+
return ccall(:jl_new_structv, Any, (Any,Ptr{Cvoid},UInt32), T, flds, length(flds))
9498
else
9599
newstruct!(initstruct(T), xs...)
96100
end
@@ -102,7 +106,7 @@ function newstruct_raw(cache, T, d)
102106
return newstruct!(x, fs...)
103107
end
104108

105-
newprimitive(T, data) = reinterpret(T, data)[1]
109+
newprimitive(T, data) = reinterpret_(T, data)[1]
106110

107111
tags[:struct] = d ->
108112
isprimitive(d[:type]) ?

src/read.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
jtype(tag) =
2-
tag == null ? Void :
2+
tag == null ? Nothing :
33
tag == boolean ? Bool :
44
tag == int32 ? Int32 :
55
tag == int64 ? Int64 :
@@ -66,7 +66,7 @@ const tags = Dict{Symbol,Function}()
6666

6767
const raise = Dict{Symbol,Function}()
6868

69-
function _raise_recursive(d::Associative, cache)
69+
function _raise_recursive(d::AbstractDict, cache)
7070
if haskey(d, :tag) && haskey(tags, Symbol(d[:tag]))
7171
cache[d] = tags[Symbol(d[:tag])](applychildren!(x -> raise_recursive(x, cache), d))
7272
else
@@ -75,10 +75,10 @@ function _raise_recursive(d::Associative, cache)
7575
end
7676
end
7777

78-
function raise_recursive(d::Associative, cache)
78+
function raise_recursive(d::AbstractDict, cache)
7979
haskey(cache, d) && return cache[d]
8080
haskey(d, :tag) && haskey(raise, Symbol(d[:tag])) && return raise[Symbol(d[:tag])](d, cache)
81-
_raise_recursive(d::Associative, cache)
81+
_raise_recursive(d::AbstractDict, cache)
8282
end
8383

8484
function raise_recursive(v::BSONArray, cache)
@@ -88,7 +88,7 @@ end
8888

8989
raise_recursive(x, cache) = x
9090

91-
raise_recursive(x) = raise_recursive(x, ObjectIdDict())
91+
raise_recursive(x) = raise_recursive(x, IdDict())
9292

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

src/write.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
bson_type(::Void) = null
1+
bson_type(::Nothing) = null
22
bson_type(::Bool) = boolean
33
bson_type(::Int32) = int32
44
bson_type(::Int64) = int64
@@ -8,7 +8,7 @@ bson_type(::Vector{UInt8}) = binary
88
bson_type(::BSONDict) = document
99
bson_type(::BSONArray) = array
1010

11-
bson_primitive(io::IO, ::Void) = return
11+
bson_primitive(io::IO, ::Nothing) = return
1212
bson_primitive(io::IO, x::Union{Bool,Int32,Int64,Float64}) = write(io, x)
1313
bson_primitive(io::IO, x::Float64) = write(io, x)
1414
bson_primitive(io::IO, x::Vector{UInt8}) = write(io, Int32(length(x)), 0x00, x)
@@ -43,7 +43,7 @@ lower(x::Primitive) = x
4343

4444
import Base: RefValue
4545

46-
ismutable(T) = !isbits(T)
46+
ismutable(T) = !isbitstype(T)
4747
ismutable(::Type{String}) = false
4848

4949
typeof_(x) = typeof(x)
@@ -67,19 +67,19 @@ end
6767
stripref(x::RefValue) = stripref(x.x)
6868
stripref(x) = applychildren!(stripref, x)
6969

70-
function lower_recursive(x)
71-
cache = ObjectIdDict()
70+
function lower_recursive(y)
71+
cache = IdDict()
7272
backrefs = []
73-
x = _lower_recursive(x, cache, backrefs).x
73+
x = _lower_recursive(y, cache, backrefs).x
7474
isempty(backrefs) || (x[:_backrefs] = Any[x.x for x in backrefs])
7575
foreach((ix) -> (ix[2].x = BSONDict(:tag=>"backref",:ref=>ix[1])), enumerate(backrefs))
7676
return stripref(x)
7777
end
7878

7979
# Interface
8080

81-
bson(io::IO, doc::Associative) = bson_primitive(io, lower_recursive(doc))
81+
bson(io::IO, doc::AbstractDict) = bson_primitive(io, lower_recursive(doc))
8282

83-
bson(path::String, doc::Associative) = open(io -> bson(io, doc), path, "w")
83+
bson(path::String, doc::AbstractDict) = open(io -> bson(io, doc), path, "w")
8484

8585
bson(path::String; kws...) = bson(path, Dict(kws))

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using BSON
2-
using Base.Test
2+
using Test
33

44
roundtrip_equal(x) = BSON.roundtrip(x) == x
55

0 commit comments

Comments
 (0)