Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix errors on 1.7 #102

Merged
merged 5 commits into from
Feb 12, 2022
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# BSON

[![Build Status](https://travis-ci.org/JuliaIO/BSON.jl.svg?branch=master)](https://travis-ci.org/JuliaIO/BSON.jl)
[![CI](https://github.com/JuliaIO/BSON.jl/actions/workflows/ci.yml/badge.svg)](https://github.com/JuliaIO/BSON.jl/actions/workflows/ci.yml)

BSON.jl is a Julia package for working with the [Binary JSON](http://bsonspec.org/) serialisation format. It can be used as a general store for Julia data structures, with the following features:

Expand Down
2 changes: 0 additions & 2 deletions src/BSON.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ module BSON
using Core: SimpleVector, TypeName
export bson

using Core: SimpleVector, TypeName

const BSONDict = Dict{Symbol,Any}
const BSONArray = Vector{Any}
const Primitive = Union{Nothing,Bool,Int32,Int64,Float64,String,Vector{UInt8},BSONDict,BSONArray}
Expand Down
54 changes: 53 additions & 1 deletion src/anonymous.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ function newstruct!(meth::Method, mod, name, file, line, sig,
end
end

# Type Names

if VERSION < v"1.7-"
function structdata(t::TypeName)
primary = Base.unwrap_unionall(t.wrapper)
mt = !isdefined(t, :mt) ? nothing :
Expand All @@ -69,11 +72,27 @@ function structdata(t::TypeName)
primary.types, isdefined(primary, :instance), isabstracttype(primary),
ismutabletype(primary), primary.ninitialized, mt]
end
else
# see https://github.com/JuliaLang/julia/pull/41018 for changes
function structdata(t::TypeName)
primary = Base.unwrap_unionall(t.wrapper)
mt = !isdefined(t, :mt) ? nothing :
[t.mt.name, collect(Base.MethodList(t.mt)), t.mt.max_args,
isdefined(t.mt, :kwsorter) ? t.mt.kwsorter : nothing]
[Base.string(VERSION), t.name, t.names, primary.super, primary.parameters,
primary.types, isdefined(primary, :instance), t.atomicfields, isabstracttype(primary),
ismutable(primary), Core.Compiler.datatype_min_ninitialized(primary), mt]
end
end

# Type Names
if VERSION >= v"1.7-"
structdata(x::Core.TypeofVararg) =
Any[getfield(x, f) for f in fieldnames(typeof(x)) if isdefined(x, f)]
end

baremodule __deserialized_types__ end

if VERSION < v"1.7-"
function newstruct_raw(cache, ::Type{TypeName}, d, init)
name = raise_recursive(d[:data][2], cache, init)
name = isdefined(__deserialized_types__, name) ? gensym() : name
Expand Down Expand Up @@ -105,6 +124,39 @@ function newstruct_raw(cache, ::Type{TypeName}, d, init)
end
return tn
end
else
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, atomicfields,
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, Any, Cint, Cint, Cint),
tn, tn.module, super, parameters, names, types, atomicfields,
abstr, mutabl, ninitialized)
ty = tn.wrapper = ndt.name.wrapper
ccall(:jl_set_const, Cvoid, (Any, Any, Any), tn.module, tn.name, ty)
if has_instance && !isdefined(ty, :instance)
# 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, init)
if mt != nothing
mtname, defs, maxa, kwsorter = mt
tn.mt = ccall(:jl_new_method_table, Any, (Any, Any), name, tn.module)
tn.mt.name = mtname
tn.mt.max_args = maxa
for def in defs
isdefined(def, :sig) || continue
ccall(:jl_method_table_insert, Cvoid, (Any, Any, Ptr{Cvoid}), tn.mt, def, C_NULL)
end
end
return tn
end
end

# Function Types

Expand Down
20 changes: 20 additions & 0 deletions src/extensions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ function newstruct!(x, fs...)
return x
end

if VERSION < v"1.7-"
function newstruct(T, xs...)
if !ismutabletype(T)
flds = Any[convert(fieldtype(T, i), x) for (i,x) in enumerate(xs)]
Expand All @@ -127,6 +128,25 @@ function newstruct(T, xs...)

end
end
else
function newstruct(T, xs...)
if !ismutabletype(T)
flds = Any[convert(fieldtype(T, i), x) for (i,x) in enumerate(xs)]
return ccall(:jl_new_structv, Any, (Any,Ptr{Cvoid},UInt32), T, flds, length(flds))
else
# Manual inline of newstruct! to work around bug
# https://github.com/MikeInnes/BSON.jl/issues/2#issuecomment-452204339
x = initstruct(T)

for (i, f) = enumerate(xs)
f = convert(fieldtype(typeof(x),i), f)
ccall(:jl_set_nth_field, Nothing, (Any, Csize_t, Any), x, i-1, f)
end
x

end
end
end

function newstruct_raw(cache, T, d, init)
x = cache[d] = initstruct(T)
Expand Down
4 changes: 4 additions & 0 deletions src/write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ lower(x::Primitive) = x

import Base: RefValue

if VERSION < v"1.7-"
ismutable(T) = !isa(T, DataType) || T.mutable
else
ismutable(T) = !isa(T, DataType) || ismutabletype(T)
end
ismutable(::Type{String}) = false

typeof_(x) = typeof(x)
Expand Down
43 changes: 30 additions & 13 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ module A
bson("test_25_dataframe.bson", Dict(:d=>d))
end

struct NoInit
x::Int

NoInit() = new()
end

@testset "BSON" begin

@testset "Primitive Types" begin
Expand Down Expand Up @@ -101,21 +107,32 @@ end
@test x.x === x
end

@testset "Undefined References" begin
# from Issue #3
d = Dict(:a => 1, :b => Dict(:c => 3, :d => Dict("e" => 5)))
@test roundtrip_equal(d)

# from Issue #43
x = Array{String, 1}(undef, 5)
x[1] = "a"
x[4] = "d"
@test_broken roundtrip_equal(Dict(:x => x))

x = NoInit()
@test roundtrip_equal(x)
end

@testset "Anonymous Functions" begin
f = x -> x+1
if VERSION < v"1.7-"
f2 = BSON.roundtrip(f)
@test f2(5) == f(5)
@test typeof(f2) !== typeof(f)

chicken_tikka_masala(y) = x -> x+y
f = chicken_tikka_masala(5)
f2 = BSON.roundtrip(f)
@test f2(6) == f(6)
@test typeof(f2) !== typeof(f)
else
@test_throws ErrorException f2 = BSON.roundtrip(f)
end
f2 = BSON.roundtrip(f)
@test f2(5) == f(5)
@test typeof(f2) !== typeof(f)

chicken_tikka_masala(y) = x -> x+y
f = chicken_tikka_masala(5)
f2 = BSON.roundtrip(f)
@test f2(6) == f(6)
@test typeof(f2) !== typeof(f)
end

@testset "Int Literals in Type Params #41" begin
Expand Down