-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
Copy pathserialize.jl
915 lines (834 loc) · 26.5 KB
/
serialize.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
# This file is a part of Julia. License is MIT: http://julialang.org/license
module Serializer
import Base: GMP, Bottom, unsafe_convert, uncompressed_ast
import Core: svec
using Base: ViewIndex, index_lengths
export serialize, deserialize
## serializing values ##
# types AbstractSerializer and Serializer # defined in dict.jl
const TAGS = Any[
Symbol, Int8, UInt8, Int16, UInt16, Int32, UInt32,
Int64, UInt64, Int128, UInt128, Float32, Float64, Char, Ptr,
DataType, Union, TypeName,
Tuple, Array, Expr,
#LongSymbol, LongTuple, LongExpr,
Symbol, Tuple, Expr, # dummy entries, intentionally shadowed by earlier ones
LineNumberNode, Slot, LabelNode, GotoNode,
QuoteNode, :reserved23 #=was TopNode=#, TypeVar, Core.Box, LambdaInfo,
Module, #=UndefRefTag=#Symbol, Task, String,
UTF16String, UTF32String, Float16,
SimpleVector, #=BackrefTag=#Symbol, Method, GlobalRef,
(), Bool, Any, :Any, Bottom, :reserved21, :reserved22, Type,
:Array, :TypeVar, :Box,
:lambda, :body, :return, :call, Symbol("::"),
:(=), :null, :gotoifnot, :A, :B, :C, :M, :N, :T, :S, :X, :Y,
:a, :b, :c, :d, :e, :f, :g, :h, :i, :j, :k, :l, :m, :n, :o,
:p, :q, :r, :s, :t, :u, :v, :w, :x, :y, :z,
:add_int, :sub_int, :mul_int, :add_float, :sub_float,
:mul_float, :unbox, :box,
:eq_int, :slt_int, :sle_int, :ne_int,
:arrayset, :arrayref,
:Core, :Base, svec(), Tuple{},
:reserved17, :reserved18, :reserved19, :reserved20,
false, true, nothing, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
28, 29, 30, 31, 32
]
const ser_version = 3 # do not make changes without bumping the version #!
const NTAGS = length(TAGS)
function sertag(v::ANY)
ptr = pointer_from_objref(v)
ptags = convert(Ptr{Ptr{Void}}, pointer(TAGS))
@inbounds for i in 1:NTAGS
ptr == unsafe_load(ptags,i) && return (i+1)%Int32
end
return Int32(-1)
end
desertag(i::Int32) = TAGS[i-1]
# tags >= this just represent themselves, their whole representation is 1 byte
const VALUE_TAGS = sertag(())
const ZERO_TAG = sertag(0)
const TRUE_TAG = sertag(true)
const FALSE_TAG = sertag(false)
const EMPTYTUPLE_TAG = sertag(())
const TUPLE_TAG = sertag(Tuple)
const LONGTUPLE_TAG = Int32(sertag(Expr)+2)
const SIMPLEVECTOR_TAG = sertag(SimpleVector)
const SYMBOL_TAG = sertag(Symbol)
const LONGSYMBOL_TAG = Int32(sertag(Expr)+1)
const ARRAY_TAG = sertag(Array)
const UNDEFREF_TAG = Int32(sertag(Module)+1)
const BACKREF_TAG = Int32(sertag(SimpleVector)+1)
const EXPR_TAG = sertag(Expr)
const LONGEXPR_TAG = Int32(sertag(Expr)+3)
const MODULE_TAG = sertag(Module)
const LAMBDAINFO_TAG = sertag(LambdaInfo)
const METHOD_TAG = sertag(Method)
const TASK_TAG = sertag(Task)
const DATATYPE_TAG = sertag(DataType)
const TYPENAME_TAG = sertag(TypeName)
const INT_TAG = sertag(Int)
const GLOBALREF_TAG = sertag(GlobalRef)
writetag(s::IO, tag) = write(s, UInt8(tag))
function write_as_tag(s::IO, tag)
tag < VALUE_TAGS && write(s, UInt8(0))
write(s, UInt8(tag))
end
# cycle handling
function serialize_cycle(s::AbstractSerializer, x)
if !isimmutable(x) && !typeof(x).pointerfree
offs = get(s.table, x, -1)
if offs != -1
writetag(s.io, BACKREF_TAG)
write(s.io, Int(offs))
return true
end
s.table[x] = s.counter
s.counter += 1
end
return false
end
function reset_state(s::AbstractSerializer)
s.counter = 0
s.table = ObjectIdDict()
s
end
serialize(s::AbstractSerializer, x::Bool) = x ? writetag(s.io, TRUE_TAG) :
writetag(s.io, FALSE_TAG)
serialize(s::AbstractSerializer, p::Ptr) = serialize_any(s, oftype(p, C_NULL))
serialize(s::AbstractSerializer, ::Tuple{}) = writetag(s.io, EMPTYTUPLE_TAG)
function serialize(s::AbstractSerializer, t::Tuple)
l = length(t)
if l <= 255
writetag(s.io, TUPLE_TAG)
write(s.io, UInt8(l))
else
writetag(s.io, LONGTUPLE_TAG)
write(s.io, Int32(l))
end
for x in t
serialize(s, x)
end
end
function serialize(s::AbstractSerializer, v::SimpleVector)
writetag(s.io, SIMPLEVECTOR_TAG)
write(s.io, Int32(length(v)))
for x in v
serialize(s, x)
end
end
function serialize(s::AbstractSerializer, x::Symbol)
tag = sertag(x)
if tag > 0
return write_as_tag(s.io, tag)
end
pname = unsafe_convert(Ptr{UInt8}, x)
ln = Int(ccall(:strlen, Csize_t, (Cstring,), pname))
if ln <= 255
writetag(s.io, SYMBOL_TAG)
write(s.io, UInt8(ln))
else
writetag(s.io, LONGSYMBOL_TAG)
write(s.io, Int32(ln))
end
unsafe_write(s.io, pname, ln)
end
function serialize_array_data(s::IO, a)
elty = eltype(a)
if elty === Bool && !isempty(a)
last = a[1]
count = 1
for i = 2:length(a)
if a[i] != last || count == 127
write(s, UInt8((UInt8(last)<<7) | count))
last = a[i]
count = 1
else
count += 1
end
end
write(s, UInt8((UInt8(last)<<7) | count))
else
write(s, a)
end
end
function serialize(s::AbstractSerializer, a::Array)
elty = eltype(a)
if !isbits(elty)
# This is subtle: whether Arrays are put in the table depends on
# the eltype, so we need to be able to deserialize the eltype first.
# However deserializing the eltype might also use the table.
offs = get(s.table, a, -1)
if offs != -1
writetag(s.io, BACKREF_TAG)
write(s.io, Int(offs))
return
end
end
writetag(s.io, ARRAY_TAG)
if elty !== UInt8
serialize(s, elty)
end
if !isbits(elty)
s.table[a] = s.counter
s.counter += 1
end
if ndims(a) != 1
serialize(s, size(a))
else
serialize(s, length(a))
end
if isbits(elty)
serialize_array_data(s.io, a)
else
for i in eachindex(a)
if isdefined(a, i)
serialize(s, a[i])
else
writetag(s.io, UNDEFREF_TAG)
end
end
end
end
function serialize{T,N,A<:Array}(s::AbstractSerializer, a::SubArray{T,N,A})
b = trimmedsubarray(a)
serialize_any(s, b)
end
function trimmedsubarray{T,N,A<:Array}(V::SubArray{T,N,A})
dest = Array{eltype(V)}(trimmedsize(V))
copy!(dest, V)
_trimmedsubarray(dest, V, (), V.indexes...)
end
trimmedsize(V) = index_lengths(V.parent, V.indexes...)
function _trimmedsubarray{T,N,P,I,LD}(A, V::SubArray{T,N,P,I,LD}, newindexes)
LD && return SubArray{T,N,P,I,LD}(A, newindexes, size(V), Base.compute_offset1(A, 1, newindexes), 1)
SubArray{T,N,P,I,LD}(A, newindexes, size(V), 0, 0)
end
_trimmedsubarray(A, V, newindexes, index::ViewIndex, indexes...) = _trimmedsubarray(A, V, (newindexes..., trimmedindex(V.parent, length(newindexes)+1, index)), indexes...)
trimmedindex(P, d, i::Real) = oftype(i, 1)
trimmedindex(P, d, i::Colon) = i
trimmedindex(P, d, i::AbstractVector) = oftype(i, 1:length(i))
function serialize{T<:AbstractString}(s::AbstractSerializer, ss::SubString{T})
# avoid saving a copy of the parent string, keeping the type of ss
serialize_any(s, convert(SubString{T}, convert(T,ss)))
end
# Don't serialize the pointers
function serialize(s::AbstractSerializer, r::Regex)
serialize_type(s, typeof(r))
serialize(s, r.pattern)
serialize(s, r.compile_options)
serialize(s, r.match_options)
end
function serialize(s::AbstractSerializer, n::BigInt)
serialize_type(s, BigInt)
serialize(s, base(62,n))
end
function serialize(s::AbstractSerializer, n::BigFloat)
serialize_type(s, BigFloat)
serialize(s, string(n))
end
function serialize(s::AbstractSerializer, ex::Expr)
serialize_cycle(s, ex) && return
l = length(ex.args)
if l <= 255
writetag(s.io, EXPR_TAG)
write(s.io, UInt8(l))
else
writetag(s.io, LONGEXPR_TAG)
write(s.io, Int32(l))
end
serialize(s, ex.head)
serialize(s, ex.typ)
for a in ex.args
serialize(s, a)
end
end
function serialize(s::AbstractSerializer, t::Dict)
serialize_cycle(s, t) && return
serialize_type(s, typeof(t))
write(s.io, Int32(length(t)))
for (k,v) in t
serialize(s, k)
serialize(s, v)
end
end
function serialize_mod_names(s::AbstractSerializer, m::Module)
p = module_parent(m)
if m !== p
serialize_mod_names(s, p)
serialize(s, module_name(m))
end
end
function serialize(s::AbstractSerializer, m::Module)
writetag(s.io, MODULE_TAG)
serialize_mod_names(s, m)
writetag(s.io, EMPTYTUPLE_TAG)
end
# TODO: make this bidirectional, so objects can be sent back via the same key
const object_numbers = WeakKeyDict()
obj_number_salt = 0
function object_number(l::ANY)
global obj_number_salt, object_numbers
if haskey(object_numbers, l)
return object_numbers[l]
end
# a hash function that always gives the same number to the same
# object on the same machine, and is unique over all machines.
ln = obj_number_salt+(UInt64(myid())<<44)
obj_number_salt += 1
object_numbers[l] = ln
return ln::UInt64
end
function serialize(s::AbstractSerializer, meth::Method)
serialize_cycle(s, meth) && return
writetag(s.io, METHOD_TAG)
write(s.io, object_number(meth))
serialize(s, meth.module)
serialize(s, meth.name)
serialize(s, meth.file)
serialize(s, meth.line)
serialize(s, meth.sig)
serialize(s, meth.tvars)
serialize(s, meth.ambig)
serialize(s, meth.isstaged)
serialize(s, meth.lambda_template)
nothing
end
function serialize(s::AbstractSerializer, linfo::LambdaInfo)
serialize_cycle(s, linfo) && return
writetag(s.io, LAMBDAINFO_TAG)
serialize(s, uncompressed_ast(linfo))
serialize(s, linfo.slotnames)
serialize(s, linfo.slottypes)
serialize(s, linfo.slotflags)
serialize(s, linfo.ssavaluetypes)
serialize(s, linfo.sparam_syms)
serialize(s, linfo.sparam_vals)
serialize(s, linfo.rettype)
serialize(s, linfo.specTypes)
serialize(s, linfo.inferred)
if isdefined(linfo, :def)
serialize(s, linfo.def)
else
writetag(s.io, UNDEFREF_TAG)
end
serialize(s, linfo.pure)
serialize(s, linfo.nargs)
serialize(s, linfo.isva)
end
function serialize(s::AbstractSerializer, t::Task)
serialize_cycle(s, t) && return
if istaskstarted(t) && !istaskdone(t)
error("cannot serialize a running Task")
end
state = [t.code,
t.storage,
t.state == :queued || t.state == :runnable ? (:runnable) : t.state,
t.result,
t.exception]
writetag(s.io, TASK_TAG)
for fld in state
serialize(s, fld)
end
end
function serialize(s::AbstractSerializer, g::GlobalRef)
writetag(s.io, GLOBALREF_TAG)
if g.mod === Main && isdefined(g.mod, g.name) && isconst(g.mod, g.name)
v = eval(g)
if isa(v, DataType) && v === v.name.primary && should_send_whole_type(s, v)
# handle references to types in Main by sending the whole type.
# needed to be able to send nested functions (#15451).
write(s.io, UInt8(1))
serialize(s, v)
return
end
end
write(s.io, UInt8(0))
serialize(s, g.mod)
serialize(s, g.name)
end
function serialize(s::AbstractSerializer, t::TypeName)
serialize_cycle(s, t) && return
writetag(s.io, TYPENAME_TAG)
write(s.io, object_number(t))
serialize(s, t.name)
serialize(s, t.module)
serialize_typename_body(s, t)
end
function serialize_typename_body(s::AbstractSerializer, t::TypeName)
serialize(s, t.names)
serialize(s, t.primary.super)
serialize(s, t.primary.parameters)
serialize(s, t.primary.types)
serialize(s, t.primary.size)
serialize(s, t.primary.abstract)
serialize(s, t.primary.mutable)
serialize(s, t.primary.ninitialized)
if isdefined(t, :mt)
serialize(s, t.mt.name)
serialize(s, t.mt.defs)
serialize(s, t.mt.max_args)
if isdefined(t.mt, :kwsorter)
serialize(s, t.mt.kwsorter)
else
writetag(s.io, UNDEFREF_TAG)
end
else
writetag(s.io, UNDEFREF_TAG)
end
end
# decide whether to send all data for a type (instead of just its name)
function should_send_whole_type(s, t::ANY)
tn = t.name
if isdefined(tn, :mt)
# TODO improve somehow
# send whole type for anonymous functions in Main
fname = tn.mt.name
mod = tn.module
toplevel = isdefined(mod, fname) && isdefined(t, :instance) &&
getfield(mod, fname) === t.instance
ishidden = unsafe_load(unsafe_convert(Ptr{UInt8}, fname))==UInt8('#')
return mod === __deserialized_types__ || (mod === Main && (ishidden || !toplevel))
end
return false
end
# `type_itself` means we are serializing a type object. when it's false, we are
# sending the type tag part of some other object's representation.
function serialize_type_data(s, t::ANY, type_itself::Bool)
whole = should_send_whole_type(s, t)
form = type_itself ? UInt8(0) : UInt8(1)
if whole
form |= UInt8(2)
end
writetag(s.io, DATATYPE_TAG)
write(s.io, form)
if whole
serialize(s, t.name)
else
tname = t.name.name
serialize(s, tname)
mod = t.name.module
serialize(s, mod)
end
if !isempty(t.parameters)
if (whole ? (t === t.name.primary) : (isdefined(mod,tname) && t === getfield(mod,tname)))
serialize(s, svec())
else
serialize(s, t.parameters)
end
end
end
function serialize(s::AbstractSerializer, t::DataType)
tag = sertag(t)
tag > 0 && return write_as_tag(s.io, tag)
if t === Tuple
# `sertag` is not able to find types === to `Tuple` because they
# will not have been hash-consed. Plus `serialize_type_data` does not
# handle this case correctly, since Tuple{} != Tuple. `Tuple` is the
# only type with this property. issue #15849
return write_as_tag(s.io, TUPLE_TAG)
end
serialize_type_data(s, t, true)
end
function serialize_type(s::AbstractSerializer, t::DataType)
tag = sertag(t)
tag > 0 && return writetag(s.io, tag)
serialize_type_data(s, t, false)
end
function serialize(s::AbstractSerializer, n::Int)
if 0 <= n <= 32
write(s.io, UInt8(ZERO_TAG+n))
return
end
writetag(s.io, INT_TAG)
write(s.io, n)
end
serialize(s::AbstractSerializer, x::ANY) = serialize_any(s, x)
function serialize_any(s::AbstractSerializer, x::ANY)
tag = sertag(x)
if tag > 0
return write_as_tag(s.io, tag)
end
t = typeof(x)::DataType
nf = nfields(t)
if nf == 0 && t.size > 0
serialize_type(s, t)
write(s.io, x)
else
t.mutable && haskey(s.table, x) && serialize_cycle(s, x) && return
serialize_type(s, t)
t.mutable && serialize_cycle(s, x)
for i in 1:nf
if isdefined(x, i)
serialize(s, getfield(x, i))
else
writetag(s.io, UNDEFREF_TAG)
end
end
end
end
serialize(s::IO, x) = serialize(SerializationState(s), x)
## deserializing values ##
deserialize(s::IO) = deserialize(SerializationState(s))
function deserialize(s::AbstractSerializer)
handle_deserialize(s, Int32(read(s.io, UInt8)::UInt8))
end
function deserialize_cycle(s::AbstractSerializer, x::ANY)
if !isimmutable(x) && !typeof(x).pointerfree
s.table[s.counter] = x
s.counter += 1
end
nothing
end
# deserialize_ is an internal function to dispatch on the tag
# describing the serialized representation. the number of
# representations is fixed, so deserialize_ does not get extended.
function handle_deserialize(s::AbstractSerializer, b::Int32)
if b == 0
return desertag(Int32(read(s.io, UInt8)::UInt8))
end
if b >= VALUE_TAGS
return desertag(b)
elseif b == TUPLE_TAG
return deserialize_tuple(s, Int(read(s.io, UInt8)::UInt8))
elseif b == LONGTUPLE_TAG
return deserialize_tuple(s, Int(read(s.io, Int32)::Int32))
elseif b == BACKREF_TAG
id = read(s.io, Int)::Int
return s.table[id]
elseif b == ARRAY_TAG
return deserialize_array(s)
elseif b == DATATYPE_TAG
return deserialize_datatype(s)
elseif b == SYMBOL_TAG
return Symbol(read(s.io, UInt8, Int(read(s.io, UInt8)::UInt8)))
elseif b == LONGSYMBOL_TAG
return Symbol(read(s.io, UInt8, Int(read(s.io, Int32)::Int32)))
elseif b == EXPR_TAG
return deserialize_expr(s, Int(read(s.io, UInt8)::UInt8))
elseif b == LONGEXPR_TAG
return deserialize_expr(s, Int(read(s.io, Int32)::Int32))
end
return deserialize(s, desertag(b))
end
deserialize_tuple(s::AbstractSerializer, len) = ntuple(i->deserialize(s), len)
function deserialize(s::AbstractSerializer, ::Type{SimpleVector})
n = read(s.io, Int32)
svec([ deserialize(s) for i=1:n ]...)
end
function deserialize(s::AbstractSerializer, ::Type{Module})
path = deserialize(s)
m = Main
if isa(path,Tuple) && path !== ()
# old version
for mname in path
if !isdefined(m,mname)
warn("Module $mname not defined on process $(myid())") # an error seemingly fails
end
m = getfield(m,mname)::Module
end
else
mname = path
while mname !== ()
if !isdefined(m,mname)
warn("Module $mname not defined on process $(myid())") # an error seemingly fails
end
m = getfield(m,mname)::Module
mname = deserialize(s)
end
end
m
end
const known_object_data = Dict()
function deserialize(s::AbstractSerializer, ::Type{Method})
lnumber = read(s.io, UInt64)
if haskey(known_object_data, lnumber)
meth = known_object_data[lnumber]::Method
makenew = false
else
meth = ccall(:jl_new_method_uninit, Ref{Method}, ())
makenew = true
end
deserialize_cycle(s, meth)
mod = deserialize(s)::Module
name = deserialize(s)::Symbol
file = deserialize(s)::Symbol
line = deserialize(s)
sig = deserialize(s)
tvars = deserialize(s)
ambig = deserialize(s)
isstaged = deserialize(s)::Bool
template = deserialize(s)::LambdaInfo
if makenew
meth.module = mod
meth.name = name
meth.file = file
meth.line = line
meth.sig = sig
meth.tvars = tvars
meth.ambig = ambig
meth.isstaged = isstaged
meth.lambda_template = template
ccall(:jl_method_init_properties, Void, (Any,), meth)
known_object_data[lnumber] = meth
end
return meth
end
function deserialize(s::AbstractSerializer, ::Type{LambdaInfo})
linfo = ccall(:jl_new_lambda_info_uninit, Ref{LambdaInfo}, (Ptr{Void},), C_NULL)
deserialize_cycle(s, linfo)
linfo.code = deserialize(s)
linfo.slotnames = deserialize(s)
linfo.slottypes = deserialize(s)
linfo.slotflags = deserialize(s)
linfo.ssavaluetypes = deserialize(s)
linfo.sparam_syms = deserialize(s)::SimpleVector
linfo.sparam_vals = deserialize(s)::SimpleVector
linfo.rettype = deserialize(s)
linfo.specTypes = deserialize(s)
linfo.inferred = deserialize(s)::Bool
tag = Int32(read(s.io, UInt8)::UInt8)
if tag != UNDEFREF_TAG
linfo.def = handle_deserialize(s, tag)::Method
end
linfo.pure = deserialize(s)::Bool
linfo.nargs = deserialize(s)
linfo.isva = deserialize(s)::Bool
return linfo
end
function deserialize_array(s::AbstractSerializer)
d1 = deserialize(s)
if isa(d1,Type)
elty = d1
d1 = deserialize(s)
else
elty = UInt8
end
if isa(d1,Integer)
if elty !== Bool && isbits(elty)
return read!(s.io, Array{elty}(d1))
end
dims = (Int(d1),)
else
dims = convert(Dims, d1)::Dims
end
if isbits(elty)
n = prod(dims)::Int
if elty === Bool && n>0
A = Array{Bool}(dims)
i = 1
while i <= n
b = read(s.io, UInt8)::UInt8
v = (b>>7) != 0
count = b&0x7f
nxt = i+count
while i < nxt
A[i] = v; i+=1
end
end
else
A = read(s.io, elty, dims)
end
return A
end
A = Array{elty}(dims)
deserialize_cycle(s, A)
for i = eachindex(A)
tag = Int32(read(s.io, UInt8)::UInt8)
if tag != UNDEFREF_TAG
A[i] = handle_deserialize(s, tag)
end
end
return A
end
function deserialize_expr(s::AbstractSerializer, len)
hd = deserialize(s)::Symbol
e = Expr(hd)
deserialize_cycle(s, e)
ty = deserialize(s)
e.args = Any[ deserialize(s) for i=1:len ]
e.typ = ty
e
end
function deserialize(s::AbstractSerializer, ::Type{GlobalRef})
kind = read(s.io, UInt8)
if kind == 0
return GlobalRef(deserialize(s)::Module, deserialize(s)::Symbol)
else
ty = deserialize(s)
return GlobalRef(ty.name.module, ty.name.name)
end
end
function deserialize(s::AbstractSerializer, ::Type{Union})
types = deserialize(s)
Union{types...}
end
module __deserialized_types__
end
function deserialize(s::AbstractSerializer, ::Type{TypeName})
number = read(s.io, UInt64)
name = deserialize(s)
mod = deserialize(s)
if haskey(known_object_data, number)
tn = known_object_data[number]::TypeName
name = tn.name
mod = tn.module
makenew = false
elseif isdefined(mod, name)
tn = getfield(mod, name).name
# TODO: confirm somehow that the types match
name = tn.name
mod = tn.module
makenew = false
else
name = gensym()
mod = __deserialized_types__
tn = ccall(:jl_new_typename_in, Ref{TypeName}, (Any, Any), name, mod)
makenew = true
end
deserialize_cycle(s, tn)
deserialize_typename_body(s, tn, number, name, mod, makenew)
makenew && (known_object_data[number] = tn)
return tn
end
function deserialize_typename_body(s::AbstractSerializer, tn, number, name, mod, makenew)
names = deserialize(s)
super = deserialize(s)
parameters = deserialize(s)
types = deserialize(s)
size = deserialize(s)
abstr = deserialize(s)
mutable = deserialize(s)
ninitialized = deserialize(s)
if makenew
tn.names = names
tn.primary = ccall(:jl_new_datatype, Any, (Any, Any, Any, Any, Any, Cint, Cint, Cint),
tn, super, parameters, names, types,
abstr, mutable, ninitialized)
ty = tn.primary
ccall(:jl_set_const, Void, (Any, Any, Any), mod, name, ty)
if !isdefined(ty,:instance)
if isempty(parameters) && !abstr && size == 0 && (!mutable || isempty(names))
setfield!(ty, :instance, ccall(:jl_new_struct, Any, (Any,Any...), ty))
end
end
end
tag = Int32(read(s.io, UInt8)::UInt8)
if tag != UNDEFREF_TAG
mtname = handle_deserialize(s, tag)
defs = deserialize(s)
maxa = deserialize(s)
if makenew
tn.mt = ccall(:jl_new_method_table, Any, (Any, Any), name, mod)
tn.mt.name = mtname
tn.mt.defs = defs
tn.mt.max_args = maxa
end
tag = Int32(read(s.io, UInt8)::UInt8)
if tag != UNDEFREF_TAG
kws = handle_deserialize(s, tag)
if makenew
tn.mt.kwsorter = kws
end
end
end
end
function deserialize_datatype(s::AbstractSerializer)
form = read(s.io, UInt8)::UInt8
if (form&2) != 0
tname = deserialize(s)::TypeName
ty = tname.primary
else
name = deserialize(s)::Symbol
mod = deserialize(s)::Module
ty = getfield(mod,name)
end
assert(isa(ty,DataType))
if isempty(ty.parameters)
t = ty
else
params = deserialize(s)
t = ty{params...}
end
if (form&1) == 0
return t
end
deserialize(s, t)
end
function deserialize(s::AbstractSerializer, ::Type{Task})
t = Task(()->nothing)
deserialize_cycle(s, t)
t.code = deserialize(s)
t.storage = deserialize(s)
t.state = deserialize(s)
t.result = deserialize(s)
t.exception = deserialize(s)
t
end
# default DataType deserializer
function deserialize(s::AbstractSerializer, t::DataType)
nf = nfields(t)
if nf == 0 && t.size > 0
# bits type
return read(s.io, t)
end
if nf == 0
return ccall(:jl_new_struct, Any, (Any,Any...), t)
elseif isbits(t)
if nf == 1
return ccall(:jl_new_struct, Any, (Any,Any...), t, deserialize(s))
elseif nf == 2
f1 = deserialize(s)
f2 = deserialize(s)
return ccall(:jl_new_struct, Any, (Any,Any...), t, f1, f2)
elseif nf == 3
f1 = deserialize(s)
f2 = deserialize(s)
f3 = deserialize(s)
return ccall(:jl_new_struct, Any, (Any,Any...), t, f1, f2, f3)
else
flds = Any[ deserialize(s) for i = 1:nf ]
return ccall(:jl_new_structv, Any, (Any,Ptr{Void},UInt32), t, flds, nf)
end
else
x = ccall(:jl_new_struct_uninit, Any, (Any,), t)
t.mutable && deserialize_cycle(s, x)
for i in 1:nf
tag = Int32(read(s.io, UInt8)::UInt8)
if tag != UNDEFREF_TAG
ccall(:jl_set_nth_field, Void, (Any, Csize_t, Any), x, i-1, handle_deserialize(s, tag))
end
end
return x
end
end
function deserialize{K,V}(s::AbstractSerializer, T::Type{Dict{K,V}})
n = read(s.io, Int32)
t = T(); sizehint!(t, n)
deserialize_cycle(s, t)
for i = 1:n
k = deserialize(s)
v = deserialize(s)
t[k] = v
end
return t
end
deserialize(s::AbstractSerializer, ::Type{BigFloat}) = parse(BigFloat, deserialize(s))
deserialize(s::AbstractSerializer, ::Type{BigInt}) = get(GMP.tryparse_internal(BigInt, deserialize(s), 62, true))
function deserialize(s::AbstractSerializer, t::Type{Regex})
pattern = deserialize(s)
compile_options = deserialize(s)
match_options = deserialize(s)
Regex(pattern, compile_options, match_options)
end
if !is_windows()
function serialize(s::AbstractSerializer, rd::RandomDevice)
serialize_type(s, typeof(rd))
serialize(s, rd.unlimited)
end
function deserialize(s::AbstractSerializer, t::Type{RandomDevice})
unlimited = deserialize(s)
return RandomDevice(unlimited)
end
end
end