Class: Rubinius::ToolSet.current::TS::CompiledFile::Marshal

Inherits:
Object
  • Object
show all
Defined in:
lib/rubinius/compiler/compiled_file.rb

Overview

A class used to convert an CompiledCode to and from a String.

Instance Method Summary collapse

Instance Method Details

#marshal(val) ⇒ Object

For object val, return a String represetation.



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
# File 'lib/rubinius/compiler/compiled_file.rb', line 244

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
    if defined?(Encoding)
      # We manually construct the Encoding data to avoid recursion
      # marshaling an Encoding name as a String.
      name = s.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

    "x\n#{enc_name}#{s.bytesize}\n#{s}\n"
  when Rubinius::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 Rubinius::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 Rubinius::CompiledCode
    str = "M\n1\n"
    str.append marshal(val.)
    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

#unmarshal(stream) ⇒ Object

Read all data from stream and invoke unmarshal_data



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rubinius/compiler/compiled_file.rb', line 81

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