Class: RBS::Prototype::Runtime
- Inherits:
-
Object
- Object
- RBS::Prototype::Runtime
- Defined in:
- lib/rbs/prototype/runtime.rb
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#merge ⇒ Object
readonly
Returns the value of attribute merge.
-
#owners_included ⇒ Object
readonly
Returns the value of attribute owners_included.
-
#patterns ⇒ Object
readonly
Returns the value of attribute patterns.
Instance Method Summary collapse
- #builder ⇒ Object
- #decls ⇒ Object
- #each_mixin(mixins, *super_mixes) ⇒ Object
- #generate_class(mod) ⇒ Object
- #generate_constants(mod) ⇒ Object
- #generate_methods(mod, module_name, members) ⇒ Object
- #generate_module(mod) ⇒ Object
-
#initialize(patterns:, env:, merge:, owners_included: []) ⇒ Runtime
constructor
A new instance of Runtime.
- #merge_rbs(module_name, members, instance: nil, singleton: nil) ⇒ Object
- #method_type(method) ⇒ Object
- #parse(file) ⇒ Object
- #target?(const) ⇒ Boolean
- #target_method?(mod, instance: nil, singleton: nil) ⇒ Boolean
- #to_type_name(name) ⇒ Object
Constructor Details
#initialize(patterns:, env:, merge:, owners_included: []) ⇒ Runtime
Returns a new instance of Runtime.
9 10 11 12 13 14 15 16 17 |
# File 'lib/rbs/prototype/runtime.rb', line 9 def initialize(patterns:, env:, merge:, owners_included: []) @patterns = patterns @decls = nil @env = env @merge = merge @owners_included = owners_included.map do |name| Object.const_get(name) end end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
5 6 7 |
# File 'lib/rbs/prototype/runtime.rb', line 5 def env @env end |
#merge ⇒ Object (readonly)
Returns the value of attribute merge.
6 7 8 |
# File 'lib/rbs/prototype/runtime.rb', line 6 def merge @merge end |
#owners_included ⇒ Object (readonly)
Returns the value of attribute owners_included.
7 8 9 |
# File 'lib/rbs/prototype/runtime.rb', line 7 def owners_included @owners_included end |
#patterns ⇒ Object (readonly)
Returns the value of attribute patterns.
4 5 6 |
# File 'lib/rbs/prototype/runtime.rb', line 4 def patterns @patterns end |
Instance Method Details
#builder ⇒ Object
29 30 31 |
# File 'lib/rbs/prototype/runtime.rb', line 29 def builder @builder ||= DefinitionBuilder.new(env: env) end |
#decls ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rbs/prototype/runtime.rb', line 37 def decls unless @decls @decls = [] ObjectSpace.each_object(Module).select {|mod| target?(mod) }.sort_by(&:name).each do |mod| case mod when Class generate_class mod when Module generate_module mod end end end @decls end |
#each_mixin(mixins, *super_mixes) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/rbs/prototype/runtime.rb', line 62 def each_mixin(mixins, *super_mixes) supers = Set.new(super_mixes) mixins.each do |mix| unless supers.include?(mix) yield mix end end end |
#generate_class(mod) ⇒ Object
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/rbs/prototype/runtime.rb', line 305 def generate_class(mod) type_name = to_type_name(mod.name) super_class = if mod.superclass == ::Object nil else AST::Declarations::Class::Super.new(name: to_type_name(mod.superclass.name), args: []) end decl = AST::Declarations::Class.new( name: type_name, type_params: AST::Declarations::ModuleTypeParams.empty, super_class: super_class, members: [], annotations: [], location: nil, comment: nil ) each_mixin(mod.included_modules, *mod.superclass.included_modules, *mod.included_modules.flat_map(&:included_modules)) do |included_module| module_name = to_type_name(included_module.name) if module_name.namespace == type_name.namespace module_name = TypeName.new(name: module_name.name, namespace: Namespace.empty) end decl.members << AST::Members::Include.new( name: module_name, args: [], location: nil, comment: nil, annotations: [] ) end generate_methods(mod, type_name, decl.members) @decls << decl generate_constants mod end |
#generate_constants(mod) ⇒ Object
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/rbs/prototype/runtime.rb', line 279 def generate_constants(mod) mod.constants(false).sort.each do |name| value = mod.const_get(name) next if value.is_a?(Class) || value.is_a?(Module) type = case value when true, false Types::Bases::Bool.new(location: nil) when nil Types::Optional.new( type: Types::Bases::Any.new(location: nil), location: nil ) else Types::ClassInstance.new(name: to_type_name(value.class.to_s), args: [], location: nil) end @decls << AST::Declarations::Constant.new( name: "#{mod.to_s}::#{name}", type: type, location: nil, comment: nil ) end end |
#generate_methods(mod, module_name, members) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/rbs/prototype/runtime.rb', line 180 def generate_methods(mod, module_name, members) mod.singleton_methods.select {|name| target_method?(mod, singleton: name) }.sort.each do |name| method = mod.singleton_class.instance_method(name) if method.name == method.original_name merge_rbs(module_name, members, singleton: name) do RBS.logger.info "missing #{module_name}.#{name} #{method.source_location}" members << AST::Members::MethodDefinition.new( name: method.name, types: [method_type(method)], kind: :singleton, location: nil, comment: nil, annotations: [], attributes: [] ) end else members << AST::Members::Alias.new( new_name: method.name, old_name: method.original_name, kind: :singleton, location: nil, comment: nil, annotations: [], ) end end public_instance_methods = mod.public_instance_methods.select {|name| target_method?(mod, instance: name) } unless public_instance_methods.empty? members << AST::Members::Public.new(location: nil) public_instance_methods.sort.each do |name| method = mod.instance_method(name) if method.name == method.original_name merge_rbs(module_name, members, instance: name) do RBS.logger.info "missing #{module_name}##{name} #{method.source_location}" members << AST::Members::MethodDefinition.new( name: method.name, types: [method_type(method)], kind: :instance, location: nil, comment: nil, annotations: [], attributes: [] ) end else members << AST::Members::Alias.new( new_name: method.name, old_name: method.original_name, kind: :instance, location: nil, comment: nil, annotations: [], ) end end end private_instance_methods = mod.private_instance_methods.select {|name| target_method?(mod, instance: name) } unless private_instance_methods.empty? members << AST::Members::Private.new(location: nil) private_instance_methods.sort.each do |name| method = mod.instance_method(name) if method.name == method.original_name merge_rbs(module_name, members, instance: name) do RBS.logger.info "missing #{module_name}##{name} #{method.source_location}" members << AST::Members::MethodDefinition.new( name: method.name, types: [method_type(method)], kind: :instance, location: nil, comment: nil, annotations: [], attributes: [] ) end else members << AST::Members::Alias.new( new_name: method.name, old_name: method.original_name, kind: :instance, location: nil, comment: nil, annotations: [], ) end end end end |
#generate_module(mod) ⇒ Object
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
# File 'lib/rbs/prototype/runtime.rb', line 345 def generate_module(mod) type_name = to_type_name(mod.name) decl = AST::Declarations::Module.new( name: type_name, type_params: AST::Declarations::ModuleTypeParams.empty, self_type: nil, members: [], annotations: [], location: nil, comment: nil ) each_mixin(mod.included_modules, *mod.included_modules.flat_map(&:included_modules), namespace: type_name.namespace) do |included_module| module_name = to_type_name(included_module.name) if module_name.namespace == type_name.namespace module_name = TypeName.new(name: module_name.name, namespace: Namespace.empty) end decl.members << AST::Members::Include.new( name: module_name, args: [], location: nil, comment: nil, annotations: [] ) end generate_methods(mod, type_name, decl.members) @decls << decl generate_constants mod end |
#merge_rbs(module_name, members, instance: nil, singleton: nil) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/rbs/prototype/runtime.rb', line 129 def merge_rbs(module_name, members, instance: nil, singleton: nil) if merge if env.class?(module_name.absolute!) case when instance method = builder.build_instance(module_name.absolute!).methods[instance] method_name = instance kind = :instance when singleton method = builder.build_singleton(module_name.absolute!).methods[singleton] method_name = singleton kind = :singleton end if method members << AST::Members::MethodDefinition.new( name: method_name, types: method.method_types.map {|type| type.update.tap do |ty| def ty.to_s location.source end end }, kind: kind, location: nil, comment: method.comment, annotations: method.annotations, attributes: method.attributes ) return end end yield else yield end end |
#method_type(method) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/rbs/prototype/runtime.rb', line 71 def method_type(method) untyped = Types::Bases::Any.new(location: nil) required_positionals = [] optional_positionals = [] rest = nil trailing_positionals = [] required_keywords = {} optional_keywords = {} rest_keywords = nil requireds = required_positionals block = nil method.parameters.each do |kind, name| case kind when :req requireds << Types::Function::Param.new(name: name, type: untyped) when :opt requireds = trailing_positionals optional_positionals << Types::Function::Param.new(name: name, type: untyped) when :rest requireds = trailing_positionals rest = Types::Function::Param.new(name: name, type: untyped) when :keyreq required_keywords[name] = Types::Function::Param.new(name: nil, type: untyped) when :key optional_keywords[name] = Types::Function::Param.new(name: nil, type: untyped) when :keyrest rest_keywords = Types::Function::Param.new(name: nil, type: untyped) when :block block = MethodType::Block.new( type: Types::Function.empty(untyped).update(rest_positionals: Types::Function::Param.new(name: nil, type: untyped)), required: true ) end end method_type = Types::Function.new( required_positionals: required_positionals, optional_positionals: optional_positionals, rest_positionals: rest, trailing_positionals: trailing_positionals, required_keywords: required_keywords, optional_keywords: optional_keywords, rest_keywords: rest_keywords, return_type: untyped ) MethodType.new( location: nil, type_params: [], type: method_type, block: block ) end |
#parse(file) ⇒ Object
33 34 35 |
# File 'lib/rbs/prototype/runtime.rb', line 33 def parse(file) require file end |
#target?(const) ⇒ Boolean
19 20 21 22 23 24 25 26 27 |
# File 'lib/rbs/prototype/runtime.rb', line 19 def target?(const) patterns.any? do |pattern| if pattern.end_with?("*") (const.name || "").start_with?(pattern.chop) else const.name == pattern end end end |
#target_method?(mod, instance: nil, singleton: nil) ⇒ Boolean
169 170 171 172 173 174 175 176 177 178 |
# File 'lib/rbs/prototype/runtime.rb', line 169 def target_method?(mod, instance: nil, singleton: nil) case when instance method = mod.instance_method(instance) method.owner == mod || owners_included.any? {|m| method.owner == m } when singleton method = mod.singleton_class.instance_method(singleton) method.owner == mod.singleton_class || owners_included.any? {|m| method.owner == m.singleton_class } end end |
#to_type_name(name) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/rbs/prototype/runtime.rb', line 52 def to_type_name(name) *prefix, last = name.split(/::/) if prefix.empty? TypeName.new(name: last.to_sym, namespace: Namespace.empty) else TypeName.new(name: last.to_sym, namespace: Namespace.parse(prefix.join("::"))) end end |