Class: CodeTools::CompiledFile

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

Overview

A decode for the .rbc file format.

Defined Under Namespace

Classes: Marshal

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(magic, signature, version, stream = nil) ⇒ CompiledFile

Create a CompiledFile with magic bytes, signature, and version. The optional stream is used to lazy load the body.



12
13
14
15
16
17
18
# File 'lib/rubinius/code/compiler/compiled_file.rb', line 12

def initialize(magic, signature, version, stream=nil)
  @magic = magic
  @signature = signature
  @version = version
  @stream = stream
  @data = nil
end

Instance Attribute Details

#magicObject (readonly)

Returns the value of attribute magic.



20
21
22
# File 'lib/rubinius/code/compiler/compiled_file.rb', line 20

def magic
  @magic
end

#signatureObject (readonly)

Returns the value of attribute signature.



21
22
23
# File 'lib/rubinius/code/compiler/compiled_file.rb', line 21

def signature
  @signature
end

#streamObject (readonly)

Returns the value of attribute stream.



23
24
25
# File 'lib/rubinius/code/compiler/compiled_file.rb', line 23

def stream
  @stream
end

#versionObject (readonly)

Returns the value of attribute version.



22
23
24
# File 'lib/rubinius/code/compiler/compiled_file.rb', line 22

def version
  @version
end

Class Method Details

.dump(code, file, signature, version) ⇒ Object

Writes the CompiledFile code to file.



41
42
43
44
45
46
47
# File 'lib/rubinius/code/compiler/compiled_file.rb', line 41

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

.load(stream) ⇒ Object

From a stream-like object stream load the data in and return a CompiledFile object.

Raises:

  • (IOError)


29
30
31
32
33
34
35
36
37
# File 'lib/rubinius/code/compiler/compiled_file.rb', line 29

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

Instance Method Details

#bodyObject

Return the body object by unmarshaling the data



65
66
67
68
69
70
# File 'lib/rubinius/code/compiler/compiled_file.rb', line 65

def body
  return @data if @data

  mar = CompiledFile::Marshal.new
  @data = mar.unmarshal(stream)
end

#encode_to(stream, body) ⇒ Object

Encode the contets of this CompiledFile object to stream with a body of body. Body use marshalled using CompiledFile::Marshal



53
54
55
56
57
58
59
60
# File 'lib/rubinius/code/compiler/compiled_file.rb', line 53

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