public
Description: Rubinius, the Ruby VM
Homepage: http://rubini.us
Clone URL: git://github.com/evanphx/rubinius.git
Search Repo:
rubinius / kernel / core / marshal.rb
100644 819 lines (662 sloc) 17.826 kb
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
# depends on: module.rb class.rb
 
class Object
  def to_marshal(ms, strip_ivars = false)
    out = ms.serialize_extended_object self
    out << Marshal::TYPE_OBJECT
    out << ms.serialize(self.class.name.to_sym)
    out << ms.serialize_instance_variables_suffix(self, true, strip_ivars)
  end
end
 
class Range
  def to_marshal(ms)
    super(ms, true)
  end
end
 
class NilClass
  def to_marshal(ms)
    Marshal::TYPE_NIL
  end
end
 
class TrueClass
  def to_marshal(ms)
    Marshal::TYPE_TRUE
  end
end
 
class FalseClass
  def to_marshal(ms)
    Marshal::TYPE_FALSE
  end
end
 
class Class
  def to_marshal(ms)
    raise TypeError, "can't dump anonymous class #{self}" if self.name == ''
    Marshal::TYPE_CLASS + ms.serialize_integer(name.length) + name
  end
end
 
class Module
  def to_marshal(ms)
    raise TypeError, "can't dump anonymous module #{self}" if self.name == ''
    Marshal::TYPE_MODULE + ms.serialize_integer(name.length) + name
  end
end
 
class Symbol
  def to_marshal(ms)
    if idx = ms.find_symlink(self) then
      Marshal::TYPE_SYMLINK + ms.serialize_integer(idx)
    else
      ms.add_symlink self
 
      str = to_s
      Marshal::TYPE_SYMBOL + ms.serialize_integer(str.length) + str
    end
  end
end
 
class String
  def to_marshal(ms)
    out = ms.serialize_instance_variables_prefix(self)
    out << ms.serialize_extended_object(self)
    out << ms.serialize_user_class(self, String)
    out << Marshal::TYPE_STRING
    out << ms.serialize_integer(self.length) << self
    out << ms.serialize_instance_variables_suffix(self)
  end
end
 
class Fixnum
  def to_marshal(ms)
    Marshal::TYPE_FIXNUM + ms.serialize_integer(self)
  end
end
 
class Bignum
  def to_marshal(ms)
    str = Marshal::TYPE_BIGNUM + (self < 0 ? '-' : '+')
    cnt = 0
    num = self.abs
 
    while num != 0
      str << ms.to_byte(num)
      num >>= 8
      cnt += 1
    end
 
    if cnt % 2 == 1
      str << "\0"
      cnt += 1
    end
 
    str[0..1] + ms.serialize_integer(cnt / 2) + str[2..-1]
  end
end
 
class Regexp
  def to_marshal(ms)
    str = self.source
    out = ms.serialize_instance_variables_prefix(self)
    out << ms.serialize_extended_object(self)
    out << ms.serialize_user_class(self, Regexp)
    out << Marshal::TYPE_REGEXP
    out << ms.serialize_integer(str.length) + str
    out << ms.to_byte(options & 0x7)
    out << ms.serialize_instance_variables_suffix(self)
  end
end
 
class Struct
  def to_marshal(ms)
    out = ms.serialize_instance_variables_prefix(self)
    out << ms.serialize_extended_object(self)
 
    out << Marshal::TYPE_STRUCT
 
    out << ms.serialize(self.class.name.to_sym)
    out << ms.serialize_integer(self.length)
 
    self.each_pair do |name, value|
      out << ms.serialize(name)
      out << ms.serialize(value)
    end
 
    out << ms.serialize_instance_variables_suffix(self)
    out
  end
end
 
class Array
  def to_marshal(ms)
    out = ms.serialize_instance_variables_prefix(self)
    out << ms.serialize_extended_object(self)
    out << ms.serialize_user_class(self, Array)
    out << Marshal::TYPE_ARRAY
    out << ms.serialize_integer(self.length)
    unless empty? then
      each do |element|
        out << ms.serialize(element)
      end
    end
    out << ms.serialize_instance_variables_suffix(self)
  end
end
 
class Hash
  def to_marshal(ms)
    raise TypeError, "can't dump hash with default proc" if default_proc
    out = ms.serialize_instance_variables_prefix(self)
    out << ms.serialize_extended_object(self)
    out << ms.serialize_user_class(self, Hash)
    out << (self.default ? Marshal::TYPE_HASH_DEF : Marshal::TYPE_HASH)
    out << ms.serialize_integer(length)
    unless empty? then
      each_pair do |(key, val)|
        out << ms.serialize(key)
        out << ms.serialize(val)
      end
    end
    out << (default ? ms.serialize(default) : '')
    out << ms.serialize_instance_variables_suffix(self)
  end
end
 
class Float
  def to_marshal(ms)
    str = if nan? then
            "nan"
          elsif zero? then
            (1.0 / self) < 0 ? '-0' : '0'
          elsif infinite? then
            self < 0 ? "-inf" : "inf"
          else
            "%.*g" % [17, self] + ms.serialize_float_thing(self)
          end
    Marshal::TYPE_FLOAT + ms.serialize_integer(str.length) + str
  end
end
 
module Marshal
 
  MAJOR_VERSION = 4
  MINOR_VERSION = 8
 
  VERSION_STRING = "\x04\x08"
 
  TYPE_NIL = '0'
  TYPE_TRUE = 'T'
  TYPE_FALSE = 'F'
  TYPE_FIXNUM = 'i'
 
  TYPE_EXTENDED = 'e'
  TYPE_UCLASS = 'C'
  TYPE_OBJECT = 'o'
  TYPE_DATA = 'd' # no specs
  TYPE_USERDEF = 'u'
  TYPE_USRMARSHAL = 'U'
  TYPE_FLOAT = 'f'
  TYPE_BIGNUM = 'l'
  TYPE_STRING = '"'
  TYPE_REGEXP = '/'
  TYPE_ARRAY = '['
  TYPE_HASH = '{'
  TYPE_HASH_DEF = '}'
  TYPE_STRUCT = 'S'
  TYPE_MODULE_OLD = 'M' # no specs
  TYPE_CLASS = 'c'
  TYPE_MODULE = 'm'
 
  TYPE_SYMBOL = ':'
  TYPE_SYMLINK = ';'
 
  TYPE_IVAR = 'I'
  TYPE_LINK = '@'
 
  class State
 
    def initialize(stream, depth, proc)
      # shared
      @links = {}
      @symlinks = {}
      @symbols = []
      @objects = []
 
      # dumping
      @depth = depth
 
      # loading
      @stream = stream
      @consumed = 0
 
      consume 2 if @stream
 
      @modules = nil
      @has_ivar = []
      @proc = proc
      @call = true
    end
 
    def add_object(obj)
      return if obj.kind_of?(ImmediateValue)
      sz = @links.size
      @objects[sz] = obj
      @links[obj.object_id] = sz
    end
 
    def add_symlink(obj)
      sz = @symlinks.size
      @symbols[sz] = obj
      @symlinks[obj.object_id] = sz
    end
 
    def call(obj)
      @proc.call obj if @proc and @call
    end
 
    def construct(ivar_index = nil, call_proc = true)
      type = consume
      obj = case type
            when TYPE_NIL
              nil
            when TYPE_TRUE
              true
            when TYPE_FALSE
              false
            when TYPE_CLASS, TYPE_MODULE
              name = construct_symbol
              obj = Object.const_lookup name
 
              store_unique_object obj
 
              obj
            when TYPE_FIXNUM
              construct_integer
            when TYPE_BIGNUM
              construct_bignum
            when TYPE_FLOAT
              construct_float
            when TYPE_SYMBOL
              construct_symbol
            when TYPE_STRING
              construct_string
            when TYPE_REGEXP
              construct_regexp
            when TYPE_ARRAY
              construct_array
            when TYPE_HASH, TYPE_HASH_DEF
              construct_hash type
            when TYPE_STRUCT
              construct_struct
            when TYPE_OBJECT
              construct_object
            when TYPE_USERDEF
              construct_user_defined ivar_index
            when TYPE_USRMARSHAL
              construct_user_marshal
            when TYPE_LINK
              num = construct_integer
              obj = @objects[num]
 
              raise ArgumentError, "dump format error (unlinked)" if obj.nil?
 
              return obj
            when TYPE_SYMLINK
              num = construct_integer
              sym = @symbols[num]
 
              raise ArgumentError, "bad symbol" if sym.nil?
 
              return sym
            when TYPE_EXTENDED
              @modules ||= []
 
              name = get_symbol
              @modules << Object.const_lookup(name)
 
              obj = construct nil, false
 
              extend_object obj
 
              obj
            when TYPE_UCLASS
              name = get_symbol
              @user_class = name
 
              construct nil, false
 
            when TYPE_IVAR
              ivar_index = @has_ivar.length
              @has_ivar.push true
 
              obj = construct ivar_index, false
 
              set_instance_variables obj if @has_ivar.pop
 
              obj
            else
              raise ArgumentError, "load error, unknown type #{type}"
            end
 
      call obj if call_proc
 
      obj
    end
 
    def construct_array
      obj = @user_class ? get_user_class.new : []
      store_unique_object obj
 
      construct_integer.times do
        obj << construct
      end
 
      obj
    end
 
    def construct_bignum
      sign = consume == '-' ? -1 : 1
      size = construct_integer * 2
 
      result = 0
 
      data = consume size
      (0...size).each do |exp|
        result += (data[exp] * 2**(exp*8))
      end
 
      obj = result * sign
 
      store_unique_object obj
    end
 
    def construct_float
      s = get_byte_sequence
 
      if s == "nan"
        obj = 0.0 / 0.0
      elsif s == "inf"
        obj = 1.0 / 0.0
      elsif s == "-inf"
        obj = 1.0 / -0.0
      else
        obj = s.to_f
      end
 
      store_unique_object obj
 
      obj
    end
 
    def construct_hash(type)
      obj = @user_class ? get_user_class.allocate : {}
      store_unique_object obj
 
      construct_integer.times do
        key = construct
        val = construct
        obj[key] = val
      end
 
      obj.default = construct if type == TYPE_HASH_DEF
 
      obj
    end
 
    def construct_integer
      n = consume[0]
 
      if (n > 0 and n < 5) or n > 251
        size, signed = n > 251 ? [256 - n, 2**((256 - n)*8)] : [n, 0]
 
        result = 0
        data = consume size
 
        (0...size).each do |exp|
          result += (data[exp] * 2**(exp*8))
        end
 
        result - signed
      elsif n > 127
        (n - 256) + 5
      elsif n > 4
        n - 5
      else
        n
      end
    end
 
    def construct_object
      name = get_symbol
      klass = Object.const_lookup name
      obj = klass.allocate
 
      raise TypeError, 'dump format error' unless Object === obj
 
      store_unique_object obj
      set_instance_variables obj
 
      obj
    end
 
    def construct_regexp
      s = get_byte_sequence
      if @user_class
        obj = get_user_class.new s, consume[0]
      else
        obj = Regexp.new s, consume[0]
      end
 
      store_unique_object obj
    end
 
    def construct_string
      obj = get_byte_sequence
      obj = get_user_class.new obj if @user_class
 
      store_unique_object obj
    end
 
    def construct_struct
      symbols = []
      values = []
 
      name = get_symbol
      store_unique_object name
 
      klass = Object.const_lookup name
      members = klass.members
 
      obj = klass.allocate
      store_unique_object obj
 
      construct_integer.times do |i|
        slot = get_symbol
        unless members[i].intern == slot then
          raise TypeError, "struct %s is not compatible (%p for %p)" %
            [klass, slot, members[i]]
        end
 
        obj.instance_variable_set "@#{slot}", construct
      end
 
      obj
    end
 
    def construct_symbol
      obj = get_byte_sequence.to_sym
      store_unique_object obj
 
      obj
    end
 
    def construct_user_defined(ivar_index)
      name = get_symbol
      klass = Module.const_lookup name
 
      data = get_byte_sequence
 
      if ivar_index and @has_ivar[ivar_index] then
        set_instance_variables data
        @has_ivar[ivar_index] = false
      end
 
      obj = klass._load data
 
      store_unique_object obj
 
      obj
    end
 
    def construct_user_marshal
      name = get_symbol
      store_unique_object name
 
      klass = Module.const_lookup name
      obj = klass.allocate
 
      extend_object obj if @modules
 
      unless obj.respond_to? :marshal_load then
        raise TypeError, "instance of #{klass} needs to have method `marshal_load'"
      end
 
      store_unique_object obj
 
      data = construct
      obj.marshal_load data
 
      obj
    end
 
    def consume(bytes = 1)
      data = @stream[@consumed, bytes]
      @consumed += bytes
      data
    end
 
    def extend_object(obj)
      obj.extend(@modules.pop) until @modules.empty?
    end
 
    def find_link(obj)
      @links[obj.object_id]
    end
 
    def find_symlink(obj)
      @symlinks[obj.object_id]
    end