Class: YARP::Serialize::Loader
- Inherits:
-
Object
- Object
- YARP::Serialize::Loader
- Defined in:
- lib/yarp/serialize.rb
Instance Attribute Summary collapse
-
#constant_pool ⇒ Object
readonly
Returns the value of attribute constant_pool.
-
#constant_pool_offset ⇒ Object
readonly
Returns the value of attribute constant_pool_offset.
-
#encoding ⇒ Object
readonly
Returns the value of attribute encoding.
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#io ⇒ Object
readonly
Returns the value of attribute io.
-
#serialized ⇒ Object
readonly
Returns the value of attribute serialized.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
-
#initialize(source, serialized) ⇒ Loader
constructor
A new instance of Loader.
- #load_encoding ⇒ Object
- #load_metadata ⇒ Object
- #load_nodes ⇒ Object
- #load_result ⇒ Object
- #load_tokens ⇒ Object
- #load_tokens_result ⇒ Object
Constructor Details
#initialize(source, serialized) ⇒ Loader
Returns a new instance of Loader.
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/yarp/serialize.rb', line 45 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_pool ⇒ Object (readonly)
Returns the value of attribute constant_pool.
43 44 45 |
# File 'lib/yarp/serialize.rb', line 43 def constant_pool @constant_pool end |
#constant_pool_offset ⇒ Object (readonly)
Returns the value of attribute constant_pool_offset.
43 44 45 |
# File 'lib/yarp/serialize.rb', line 43 def constant_pool_offset @constant_pool_offset end |
#encoding ⇒ Object (readonly)
Returns the value of attribute encoding.
42 43 44 |
# File 'lib/yarp/serialize.rb', line 42 def encoding @encoding end |
#input ⇒ Object (readonly)
Returns the value of attribute input.
42 43 44 |
# File 'lib/yarp/serialize.rb', line 42 def input @input end |
#io ⇒ Object (readonly)
Returns the value of attribute io.
42 43 44 |
# File 'lib/yarp/serialize.rb', line 42 def io @io end |
#serialized ⇒ Object (readonly)
Returns the value of attribute serialized.
42 43 44 |
# File 'lib/yarp/serialize.rb', line 42 def serialized @serialized end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
43 44 45 |
# File 'lib/yarp/serialize.rb', line 43 def source @source end |
Instance Method Details
#load_encoding ⇒ Object
59 60 61 |
# File 'lib/yarp/serialize.rb', line 59 def load_encoding Encoding.find(io.read(load_varint)) end |
#load_metadata ⇒ Object
63 64 65 66 67 68 |
# File 'lib/yarp/serialize.rb', line 63 def comments = load_varint.times.map { Comment.new(Comment::TYPES.fetch(load_varint), load_location) } errors = load_varint.times.map { ParseError.new(, load_location) } warnings = load_varint.times.map { ParseWarning.new(, load_location) } [comments, errors, warnings] end |
#load_nodes ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/yarp/serialize.rb', line 96 def load_nodes raise "Invalid serialization" if io.read(4) != "YARP" raise "Invalid serialization" if io.read(3).unpack("C3") != [MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION] @encoding = load_encoding @input = input.force_encoding(@encoding).freeze comments, errors, warnings = @constant_pool_offset = io.read(4).unpack1("L") @constant_pool = Array.new(load_varint, nil) [load_node, comments, errors, warnings] end |
#load_result ⇒ Object
111 112 113 114 |
# File 'lib/yarp/serialize.rb', line 111 def load_result node, comments, errors, warnings = load_nodes YARP::ParseResult.new(node, comments, errors, warnings, @source) end |
#load_tokens ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/yarp/serialize.rb', line 70 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 tokens end |
#load_tokens_result ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/yarp/serialize.rb', line 83 def load_tokens_result tokens = load_tokens encoding = load_encoding comments, errors, warnings = if encoding != @encoding tokens.each { |token,| token.value.force_encoding(encoding) } end raise "Expected to consume all bytes while deserializing" unless @io.eof? YARP::ParseResult.new(tokens, comments, errors, warnings, @source) end |