forked from rubinius/rubinius
/
compiled_file.rb
325 lines (289 loc) · 8.92 KB
/
compiled_file.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
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
# -*- encoding: us-ascii -*-
module Rubinius
##
# A decode for the .rbc file format.
class CompiledFile
##
# Create a CompiledFile with +magic+ bytes, +signature+, and +version+.
# The optional +stream+ is used to lazy load the body.
def initialize(magic, signature, version, stream=nil)
@magic = magic
@signature = signature
@version = version
@stream = stream
@data = nil
end
attr_reader :magic
attr_reader :signature
attr_reader :version
attr_reader :stream
##
# From a stream-like object +stream+ load the data in and return a
# CompiledFile object.
def self.load(stream)
raise IOError, "attempted to load nil stream" unless stream
magic = stream.gets.strip
signature = Integer(stream.gets.strip)
version = Integer(stream.gets.strip)
return new(magic, signature, version, stream)
end
##
# Writes the CompiledFile +code+ to +file+.
def self.dump(code, file, signature, version)
File.open(file, "wb") do |f|
new("!RBIX", signature, version).encode_to(f, code)
end
rescue SystemCallError
# just skip writing the compiled file if we don't have permissions
end
##
# Encode the contets of this CompiledFile object to +stream+ with
# a body of +body+. Body use marshalled using CompiledFile::Marshal
def encode_to(stream, body)
stream.puts @magic
stream.puts @signature.to_s
stream.puts @version.to_s
mar = CompiledFile::Marshal.new
stream << mar.marshal(body)
end
##
# Return the body object by unmarshaling the data
def body
return @data if @data
mar = CompiledFile::Marshal.new
@data = mar.unmarshal(stream)
end
##
# A class used to convert an CompiledCode to and from
# a String.
class Marshal
##
# Read all data from +stream+ and invoke unmarshal_data
def unmarshal(stream)
if stream.kind_of? String
str = stream
else
str = stream.read
end
@start = 0
@size = str.size
@data = str.data
unmarshal_data
end
##
# Process a stream object +stream+ as as marshalled data and
# return an object representation of it.
def unmarshal_data
kind = next_type
case kind
when 116 # ?t
return true
when 102 # ?f
return false
when 110 # ?n
return nil
when 73 # ?I
return next_string.to_i(16)
when 100 # ?d
str = next_string.chop
# handle the special NaN, Infinity and -Infinity differently
if str[0] == ?\ # leading space
x = str.to_f
e = str[-5..-1].to_i
# This is necessary because (2**1024).to_f yields Infinity
if e == 1024
return x * 2 ** 512 * 2 ** 512
else
return x * 2 ** e
end
else
case str.downcase
when "infinity"
return 1.0 / 0.0
when "-infinity"
return -1.0 / 0.0
when "nan"
return 0.0 / 0.0
else
raise TypeError, "Invalid Float format: #{str}"
end
end
when 115 # ?s
enc = unmarshal_data
count = next_string.to_i
str = next_bytes count
str.force_encoding enc if enc and defined?(Encoding)
return str
when 120 # ?x
count = next_string.to_i
str = next_bytes count
return str.to_sym
when 99 # ?c
count = next_string.to_i
str = next_bytes count
return str.split("::").inject(Object) { |a,n| a.const_get(n) }
when 112 # ?p
count = next_string.to_i
obj = Tuple.new(count)
i = 0
while i < count
obj[i] = unmarshal_data
i += 1
end
return obj
when 105 # ?i
count = next_string.to_i
seq = InstructionSequence.new(count)
i = 0
while i < count
seq[i] = next_string.to_i
i += 1
end
return seq
when 69 # ?E
count = next_string.to_i
name = next_bytes count
return Encoding.find(name) if defined?(Encoding)
when 77 # ?M
version = next_string.to_i
if version != 1
raise "Unknown CompiledCode version #{version}"
end
code = CompiledCode.new
code.metadata = unmarshal_data
code.primitive = unmarshal_data
code.name = unmarshal_data
code.iseq = unmarshal_data
code.stack_size = unmarshal_data
code.local_count = unmarshal_data
code.required_args = unmarshal_data
code.post_args = unmarshal_data
code.total_args = unmarshal_data
code.splat = unmarshal_data
code.literals = unmarshal_data
code.lines = unmarshal_data
code.file = unmarshal_data
code.local_names = unmarshal_data
return code
else
raise "Unknown type '#{kind.chr}'"
end
end
private :unmarshal_data
##
# Returns the next character in _@data_ as a Fixnum.
#--
# The current format uses a one-character type indicator
# followed by a newline. If that format changes, this
# will break and we'll fix it.
#++
def next_type
chr = @data[@start]
@start += 2
chr
end
private :next_type
##
# Returns the next string in _@data_ including the trailing
# "\n" character.
def next_string
count = @data.locate "\n", @start, @size
count = @size unless count
str = String.from_bytearray @data, @start, count - @start
@start = count
str
end
private :next_string
##
# Returns the next _count_ bytes in _@data_, skipping the
# trailing "\n" character.
def next_bytes(count)
str = String.from_bytearray @data, @start, count
@start += count + 1
str
end
private :next_bytes
##
# For object +val+, return a String represetation.
def marshal(val)
case val
when TrueClass
"t\n"
when FalseClass
"f\n"
when NilClass
"n\n"
when Fixnum, Bignum
"I\n#{val.to_s(16)}\n"
when String
if defined?(Encoding)
# We manually construct the Encoding data to avoid recursion
# marshaling an Encoding name as a String.
name = val.encoding.name
enc_name = "E\n#{name.bytesize}\n#{name}\n"
else
# The kernel code is all US-ASCII. When building melbourne for 1.8
# Ruby, we fake a bunch of encoding stuff so force US-ASCII here.
enc_name = "E\n8\nUS-ASCII\n"
end
"s\n#{enc_name}#{val.bytesize}\n#{val}\n"
when Symbol
s = val.to_s
"x\n#{s.bytesize}\n#{s}\n"
when Tuple
str = "p\n#{val.size}\n"
val.each do |ele|
data = marshal(ele)
data.force_encoding(str.encoding) if defined?(Encoding)
str.append data
end
str
when Float
str = "d\n"
if val.infinite?
str.append "-" if val < 0.0
str.append "Infinity"
elsif val.nan?
str.append "NaN"
else
str.append " %+.54f %5d" % Math.frexp(val)
end
str.append "\n"
when InstructionSequence
str = "i\n#{val.size}\n"
val.opcodes.each do |op|
unless op.kind_of?(Fixnum)
raise TypeError, "InstructionSequence contains non Fixnum: #{op.inspect}"
end
str.append "#{op}\n"
end
str
when CompiledCode
str = "M\n1\n"
str.append marshal(val.metadata)
str.append marshal(val.primitive)
str.append marshal(val.name)
str.append marshal(val.iseq)
str.append marshal(val.stack_size)
str.append marshal(val.local_count)
str.append marshal(val.required_args)
str.append marshal(val.post_args)
str.append marshal(val.total_args)
str.append marshal(val.splat)
str.append marshal(val.literals)
str.append marshal(val.lines)
str.append marshal(val.file)
str.append marshal(val.local_names)
str
else
if val.respond_to? :rbx_marshal_constant
name = StringValue(val.rbx_marshal_constant)
"c\n#{name.size}\n#{name}\n"
else
raise ArgumentError, "Unknown type #{val.class}: #{val.inspect}"
end
end
end
end
end
end