Class: YARP::Serialize::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/yarp/serialize.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, serialized, io) ⇒ Loader

Returns a new instance of Loader.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/yarp/serialize.rb', line 34

def initialize(input, serialized, io)
  @encoding = Encoding::UTF_8

  @input = input.dup
  @serialized = serialized
  @io = io

  @constant_pool_offset = nil
  @constant_pool = nil

  offsets = [0]
  input.b.scan("\n") { offsets << $~.end(0) }
  @source = Source.new(input, offsets)
end

Instance Attribute Details

#constant_poolObject (readonly)

Returns the value of attribute constant_pool.



32
33
34
# File 'lib/yarp/serialize.rb', line 32

def constant_pool
  @constant_pool
end

#constant_pool_offsetObject (readonly)

Returns the value of attribute constant_pool_offset.



32
33
34
# File 'lib/yarp/serialize.rb', line 32

def constant_pool_offset
  @constant_pool_offset
end

#encodingObject (readonly)

Returns the value of attribute encoding.



31
32
33
# File 'lib/yarp/serialize.rb', line 31

def encoding
  @encoding
end

#inputObject (readonly)

Returns the value of attribute input.



31
32
33
# File 'lib/yarp/serialize.rb', line 31

def input
  @input
end

#ioObject (readonly)

Returns the value of attribute io.



31
32
33
# File 'lib/yarp/serialize.rb', line 31

def io
  @io
end

#serializedObject (readonly)

Returns the value of attribute serialized.



31
32
33
# File 'lib/yarp/serialize.rb', line 31

def serialized
  @serialized
end

#sourceObject (readonly)

Returns the value of attribute source.



32
33
34
# File 'lib/yarp/serialize.rb', line 32

def source
  @source
end

Instance Method Details

#loadObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/yarp/serialize.rb', line 49

def load
  raise "Invalid serialization" if io.read(4) != "YARP"
  raise "Invalid serialization" if io.read(3).unpack("C3") != [0, 6, 0]

  @encoding = Encoding.find(io.read(load_varint))
  @input = input.force_encoding(@encoding).freeze

  errors = load_varint.times.map { ParseError.new(load_string, load_location) }
  warnings = load_varint.times.map { ParseWarning.new(load_string, load_location) }

  @constant_pool_offset = io.read(4).unpack1("L")
  @constant_pool = Array.new(load_varint, nil)

  ast = load_node

  YARP::ParseResult.new(ast, [], errors, warnings, @source)
end