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(source, serialized) ⇒ Loader

Returns a new instance of Loader.



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/yarp/serialize.rb', line 39

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

  @input = source.source.dup
  @serialized = serialized
  @io = StringIO.new(serialized)
  @io.set_encoding(Encoding::BINARY)

  @constant_pool_offset = nil
  @constant_pool = nil

  @source = source
end

Instance Attribute Details

#constant_poolObject (readonly)

Returns the value of attribute constant_pool.



37
38
39
# File 'lib/yarp/serialize.rb', line 37

def constant_pool
  @constant_pool
end

#constant_pool_offsetObject (readonly)

Returns the value of attribute constant_pool_offset.



37
38
39
# File 'lib/yarp/serialize.rb', line 37

def constant_pool_offset
  @constant_pool_offset
end

#encodingObject (readonly)

Returns the value of attribute encoding.



36
37
38
# File 'lib/yarp/serialize.rb', line 36

def encoding
  @encoding
end

#inputObject (readonly)

Returns the value of attribute input.



36
37
38
# File 'lib/yarp/serialize.rb', line 36

def input
  @input
end

#ioObject (readonly)

Returns the value of attribute io.



36
37
38
# File 'lib/yarp/serialize.rb', line 36

def io
  @io
end

#serializedObject (readonly)

Returns the value of attribute serialized.



36
37
38
# File 'lib/yarp/serialize.rb', line 36

def serialized
  @serialized
end

#sourceObject (readonly)

Returns the value of attribute source.



37
38
39
# File 'lib/yarp/serialize.rb', line 37

def source
  @source
end

Instance Method Details

#loadObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/yarp/serialize.rb', line 72

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

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

  comments = load_varint.times.map { Comment.new(Comment::TYPES.fetch(io.getbyte), load_location) }
  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, comments, errors, warnings, @source)
end

#load_tokensObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/yarp/serialize.rb', line 53

def load_tokens
  tokens = []
  while type = TOKEN_TYPES.fetch(load_varint)
    start = load_varint
    length = load_varint
    lex_state = load_varint
    location = Location.new(@source, start, length)
    tokens << [YARP::Token.new(type, location.slice, location), lex_state]
  end

  comments = load_varint.times.map { Comment.new(Comment::TYPES.fetch(load_varint), load_location) }
  errors = load_varint.times.map { ParseError.new(load_string, load_location) }
  warnings = load_varint.times.map { ParseWarning.new(load_string, load_location) }

  raise "Expected to consume all bytes while deserializing" unless @io.eof?

  YARP::ParseResult.new(tokens, comments, errors, warnings, @source)
end