Class: RbsMacros::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs_macros/environment.rb

Overview

An environment for the Ruby program being analyzed.

Constant Summary collapse

HandlerParams =

rubocop:disable Naming/ConstantName

_ = Data.define(:env, :filename, :receiver, :name, :positional, :keyword, :block)
DeclarationEntry =

rubocop:disable Naming/ConstantName

_ = Data.define(:declaration, :mod, :file)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEnvironment

Returns a new instance of Environment.



10
11
12
13
14
15
# File 'lib/rbs_macros/environment.rb', line 10

def initialize
  @rbs = RBS::Environment.new
  @object_class = MetaClass.new(self, "Object", is_class: true)
  @decls = []
  @exact_handlers = {}
end

Instance Attribute Details

#declsObject (readonly)

Returns the value of attribute decls.



8
9
10
# File 'lib/rbs_macros/environment.rb', line 8

def decls
  @decls
end

#object_classObject (readonly)

Returns the value of attribute object_class.



8
9
10
# File 'lib/rbs_macros/environment.rb', line 8

def object_class
  @object_class
end

#rbsObject (readonly)

Returns the value of attribute rbs.



8
9
10
# File 'lib/rbs_macros/environment.rb', line 8

def rbs
  @rbs
end

Instance Method Details

#add_decl(decl, mod:, file:) ⇒ Object



42
43
44
# File 'lib/rbs_macros/environment.rb', line 42

def add_decl(decl, mod:, file:)
  @decls << DeclarationEntry.new(declaration: decl, mod:, file:)
end

#invoke(params) ⇒ Object



21
22
23
24
25
26
# File 'lib/rbs_macros/environment.rb', line 21

def invoke(params)
  handlers = @exact_handlers[params.name]
  handlers&.each do |handler|
    handler.(params)
  end
end

#meta_eval_ruby(code, filename:) ⇒ Object

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rbs_macros/environment.rb', line 28

def meta_eval_ruby(code, filename:)
  result = Prism.parse(code, filepath: "#{filename}.rb")
  raise ArgumentError, "Parse error: #{result.errors}" if result.failure?

  ExecCtx.new(
    env: self,
    filename:,
    self: nil, # TODO
    cref: @object_class,
    cref_dynamic: @object_class,
    locals: {}
  ).eval_node(result.value)
end

#register_handler(name, handler) ⇒ Object



17
18
19
# File 'lib/rbs_macros/environment.rb', line 17

def register_handler(name, handler)
  (@exact_handlers[name.to_sym] ||= []) << handler
end