class RBS::WASM::Deserializer
Rebuilds RBS::AST objects from the binary buffer produced by rbs_serialize_node (src/serialize.c), driven by the generated SerializationSchema. This is the pure-Ruby counterpart of the C extensionโs ast_translation.c, used when the parser runs inside WebAssembly (JRuby).
All locations are reconstructed through the public RBS::Location API, so the same code works whether RBS::Location is backed by the C extension (CRuby) or by a pure-Ruby implementation (JRuby).
Rebuilds RBS::AST objects from the binary buffer produced by rbs_serialize_node (src/serialize.c), driven by SerializationSchema.
Public Class Methods
(String bytes, Buffer buffer) → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/lib/rbs/wasm/deserializer.rb, line 18 def self.deserialize(bytes, buffer) new(bytes, buffer).read_node end
Deserialize a buffer produced for a whole signature, returning โ[directives, declarations]` to match RBS::Parser._parse_signature.
Deserialize a buffer produced for a whole signature, returning [directives, declarations] to match RBS::Parser._parse_signature.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/lib/rbs/wasm/deserializer.rb, line 24 def self.deserialize_node_list(bytes, buffer) new(bytes, buffer).read_node_list end
Deserialize a bare node list (rbs_serialize_node_list), e.g. the result of RBS::Parser._parse_type_params.
Deserialize a bare node list (RBS::Parser._parse_type_params).
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/lib/rbs/wasm/deserializer.rb, line 30 def self.deserialize_tokens(bytes, buffer) new(bytes, buffer).read_tokens end
Deserialize the token stream produced by rbs_wasm_lex into the
- type, location
-
pairs
RBS::Parser._lexreturns.
Deserialize the token stream from rbs_wasm_lex (RBS::Parser._lex).
(String bytes, Buffer buffer) → void
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/lib/rbs/wasm/deserializer.rb, line 34 def initialize(bytes, buffer) @bytes = bytes @buffer = buffer # Symbols and rbs_string fields (comments, annotations) inherit the # source encoding, matching ast_translation.c. String/Integer literal # nodes are always UTF-8 (see read_node). @encoding = buffer.content.encoding @pos = 0 @class_cache = {} #: Hash[String, untyped] end
Public Instance Methods
() → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/lib/rbs/wasm/deserializer.rb, line 45 def read_node tag = read_u8 return nil if tag == 0 return read_string(@encoding).to_sym if tag == SerializationSchema::SYMBOL_TAG entry = SerializationSchema::SCHEMA[tag] or raise "Unknown node tag: #{tag}" case entry[0] when :node then read_struct(entry) when :bool then read_u8 != 0 when :integer then read_string(Encoding::UTF_8).to_i when :string_value then read_string(Encoding::UTF_8) when :record_field then [read_node, read_u8 != 0] when :signature then [read_node_list, read_node_list] when :namespace then RBS::Namespace[read_node_list, read_u8 != 0] when :type_name then RBS::TypeName[read_node, read_node] else raise "Unknown schema entry kind: #{entry[0].inspect}" end end
Reads the next node and returns the reconstructed Ruby value.
() → Array[untyped]
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/lib/rbs/wasm/deserializer.rb, line 65 def read_node_list Array.new(read_count) { read_node } end
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/lib/rbs/wasm/deserializer.rb, line 71 def read_tokens tokens = [] #: Array[[ Symbol, Location ]] until @pos >= @bytes.bytesize type = read_string(Encoding::UTF_8).to_sym start_char = read_i32 end_char = read_i32 tokens << [type, RBS::Location.new(@buffer, start_char, end_char)] end tokens end
The lex stream has no leading count: read records until the buffer is exhausted. Each is a token type name followed by its character range.