/
marshal_data.rb
237 lines (204 loc) · 6.34 KB
/
marshal_data.rb
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
class UserDefined
class Nested
def ==(other)
other.kind_of? self.class
end
end
attr_reader :a, :b
def initialize
@a = 'stuff'
@b = @a
end
def _dump(depth)
Marshal.dump [@a, @b]
end
def self._load(data)
a, b = Marshal.load data
obj = allocate
obj.instance_variable_set :@a, a
obj.instance_variable_set :@b, b
obj
end
def ==(other)
self.class === other and
@a == other.a and
@b == other.b
end
end
class UserDefinedWithIvar
attr_reader :a, :b, :c
def initialize
@a = 'stuff'
@a.instance_variable_set :@foo, :UserDefinedWithIvar
@b = 'more'
@c = @b
end
def _dump(depth)
Marshal.dump [@a, @b, @c]
end
def self._load(data)
a, b, c = Marshal.load data
obj = allocate
obj.instance_variable_set :@a, a
obj.instance_variable_set :@b, b
obj.instance_variable_set :@c, c
obj
end
def ==(other)
self.class === other and
@a == other.a and
@b == other.b and
@c == other.c and
@a.instance_variable_get(:@foo) == other.a.instance_variable_get(:@foo)
end
end
class UserMarshal
attr_reader :data
def initialize
@data = 'stuff'
end
def marshal_dump() @data end
def marshal_load(data) @data = data end
def ==(other) self.class === other and @data == other.data end
end
class UserMarshalWithIvar
attr_reader :data
def initialize
@data = 'my data'
end
def marshal_dump
[@data]
end
def marshal_load(o)
@data = o.first
end
def ==(other)
self.class === other and
@data = other.data
end
end
class UserArray < Array
end
class UserHash < Hash
end
class UserObject
end
class UserRegexp < Regexp
end
class UserString < String
end
module Meths
def meths_method() end
end
module MethsMore
def meths_more_method() end
end
Struct.new "Pyramid"
Struct.new "Useful", :a, :b
module MarshalSpec
DATA = {
"nil" => [nil, "\004\b0"],
"1..2" => [(1..2),
"\004\bo:\nRange\b:\nbegini\006:\texclF:\bendi\a"],
"1...2" => [(1...2),
"\004\bo:\nRange\b:\nbegini\006:\texclT:\bendi\a"],
"'a'..'b'" => [('a'..'b'),
"\004\bo:\nRange\b:\nbegin\"\006a:\texclF:\bend\"\006b"],
"Struct" => [Struct::Useful.new(1, 2),
"\004\bS:\023Struct::Useful\a:\006ai\006:\006bi\a"],
"Symbol" => [:symbol,
"\004\b:\vsymbol"],
"true" => [true,
"\004\bT"],
"false" => [false,
"\004\bF"],
"String empty" => ['',
"\004\b\"\000"],
"String small" => ['small',
"\004\b\"\012small"],
"String big" => ['big' * 100,
"\004\b\"\002,\001#{'big' * 100}"],
"String extended" => [''.extend(Meths), # TODO: check for module on load
"\004\be:\nMeths\"\000"],
"String subclass" => [UserString.new,
"\004\bC:\017UserString\"\000"],
"String subclass extended" => [UserString.new.extend(Meths),
"\004\be:\nMethsC:\017UserString\"\000"],
"Symbol small" => [:big,
"\004\b:\012big"],
"Symbol big" => [('big' * 100).to_sym,
"\004\b:\002,\001#{'big' * 100}"],
"Fixnum 0" => [0,
"\004\bi\000"],
"Fixnum 5" => [5,
"\004\bi\n"],
"Fixnum -123" => [-123,
"\004\bi\200"],
"Fixnum 2**8" => [2**8,
"\004\bi\002\000\001"],
"Fixnum -2**8" => [-2**8,
"\004\bi\377\000"],
"Fixnum 2**24" => [2**24,
"\004\bi\004\000\000\000\001"],
"Fixnum (2**30 - 1)" => [2**30 - 1,
"\004\bi\004\377\377\377?"],
"Fixnum 2**16" => [2**16,
"\004\bi\003\000\000\001"],
"Fixnum -2**16" => [-2**16,
"\004\bi\376\000\000"],
"Fixnum -(2**16 - 1)" => [-(2**16 - 1),
"\004\bi\376\001\000"],
"Fixnum -2**30" => [-2**30,
"\004\bi\374\000\000\000\300"],
"Bignum 2**30" => [2**30,
"\004\bl+\a\000\000\000@"],
"Bignum -(2**31 - 1)" => [-(2**31 - 1),
"\004\bl-\a\377\377\377\177"],
"Bignum 2**40" => [2**40,
"\004\bl+\b\000\000\000\000\000\001"],
"Bignum -2**63" => [-2**63,
"\004\bl-\t\000\000\000\000\000\000\000\200"],
"Bignum 2**64" => [2**64,
"\004\bl+\n\000\000\000\000\000\000\000\000\001\000"],
"Bignum -2**64" => [-2**64,
"\004\bl-\n\000\000\000\000\000\000\000\000\001\000"],
"Bignum 2**90" => [2**90,
"\004\bl+\v#{"\000" * 11}\004"],
"Class" => [String,
"\004\bc\vString"],
"Module" => [Marshal,
"\004\bm\fMarshal"],
"Module nested" => [UserDefined::Nested.new,
"\004\bo:\030UserDefined::Nested\000"],
"_dump object" => [UserDefinedWithIvar.new,
"\004\bu:\030UserDefinedWithIvar5\004\b[\bI\"\nstuff\006:\t@foo:\030UserDefinedWithIvar\"\tmore@\a"],
"_dump object extended" => [UserDefined.new.extend(Meths),
"\004\bu:\020UserDefined\022\004\b[\a\"\nstuff@\006"],
"marshal_dump object" => [UserMarshalWithIvar.new,
"\004\bU:\030UserMarshalWithIvar[\006\"\fmy data"],
"Regexp" => [/\A.\Z/,
"\004\b/\n\\A.\\Z\000"],
"Regexp subclass /i" => [UserRegexp.new('', Regexp::IGNORECASE),
"\004\bC:\017UserRegexp/\000\001"],
"Float 0.0" => [0.0,
"\004\bf\0060"],
"Float -0.0" => [-0.0,
"\004\bf\a-0"],
"Float Infinity" => [(1.0 / 0.0),
"\004\bf\binf"],
"Float -Infinity" => [(-1.0 / 0.0),
"\004\bf\t-inf"],
"Float 1.0" => [1.0,
"\004\bf\0061"],
"Hash" => [Hash.new,
"\004\b{\000"],
"Hash subclass" => [UserHash.new,
"\004\bC:\rUserHash{\000"],
"Array" => [Array.new,
"\004\b[\000"],
"Array subclass" => [UserArray.new,
"\004\bC:\016UserArray[\000"],
"Struct" => [Struct.new("Ure0").new,
"\004\bS:\021Struct::Ure0\000"],
}
end