Class: Rigor::Environment::Reflection

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

Overview

Frozen, ‘Ractor.shareable?` read-only RBS query facade. [ADR-15](../../../docs/adr/15-ractor-concurrency.md) Phase 2b extracts the read-only surface of RbsLoader into this carrier so future Ractor-isolated workers can share one Reflection across the pool while keeping the per-Ractor mutable accelerator state (per-process memo Hashes) where it belongs.

Backing tables (all frozen at construction):

  • ‘known_class_names` — `Set<String>` of every class / module / alias name in the loaded RBS environment. Top-level prefixed (`“::Hash”`); plain queries normalise via #normalise.

  • ‘instance_definitions` —`Hash<String, RBS::Definition>` keyed on `RBS::TypeName#to_s` (top-level prefixed).

  • ‘singleton_definitions` — same shape, singleton side.

  • ‘type_param_names` —`Hash<String, Array<Symbol>>` of declared type parameters per class.

  • ‘constant_types` — `Hash<String, Rigor::Type>` of translated constant declarations.

  • ‘ancestor_names` — `Hash<String, Array<String>>` of normalised ancestor chains per class.

Each ‘Reflection` instance is `frozen?` at construction — every cached table is frozen, `self` is frozen. NOT `Ractor.shareable?`: the `instance_definitions` / `singleton_definitions` tables hold upstream `RBS::Definition` objects that transitively reference `RBS::Location` (C-extension state that `Ractor.make_shareable` rejects).

The Ractor worker pool (ADR-15 Phase 4) sidesteps this by having each worker build ITS OWN ‘Reflection` from the shared `Cache::Store`. The cross-Ractor sharing point is the Store’s on-disk + in-process memo layer, NOT the Reflection itself. Each Reflection is a per- Ractor immutable read-side view; this carrier exists to GUARANTEE the per-worker view never mutates after construction.

If a future RBS release makes ‘RBS::Location` Ractor-shareable, swapping the `freeze` call below for `Ractor.make_shareable(self)` makes the whole carrier cross-Ractor-shareable in one line. Until then, the frozen-read-only contract is the deliverable.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(known_class_names:, instance_definitions:, singleton_definitions:, type_param_names:, constant_types:, ancestor_names:) ⇒ Reflection

Returns a new instance of Reflection.



57
58
59
60
61
62
63
64
65
66
# File 'lib/rigor/environment/reflection.rb', line 57

def initialize(known_class_names:, instance_definitions:, singleton_definitions:,
               type_param_names:, constant_types:, ancestor_names:)
  @known_class_names = freeze_set(known_class_names)
  @instance_definitions = freeze_hash(instance_definitions)
  @singleton_definitions = freeze_hash(singleton_definitions)
  @type_param_names = freeze_hash(type_param_names)
  @constant_types = freeze_hash(constant_types)
  @ancestor_names = freeze_hash(ancestor_names)
  freeze
end

Instance Attribute Details

#ancestor_namesObject (readonly)

Returns the value of attribute ancestor_names.



54
55
56
# File 'lib/rigor/environment/reflection.rb', line 54

def ancestor_names
  @ancestor_names
end

#constant_typesObject (readonly)

Returns the value of attribute constant_types.



54
55
56
# File 'lib/rigor/environment/reflection.rb', line 54

def constant_types
  @constant_types
end

#instance_definitionsObject (readonly)

Returns the value of attribute instance_definitions.



54
55
56
# File 'lib/rigor/environment/reflection.rb', line 54

def instance_definitions
  @instance_definitions
end

#known_class_namesObject (readonly)

Returns the value of attribute known_class_names.



54
55
56
# File 'lib/rigor/environment/reflection.rb', line 54

def known_class_names
  @known_class_names
end

#singleton_definitionsObject (readonly)

Returns the value of attribute singleton_definitions.



54
55
56
# File 'lib/rigor/environment/reflection.rb', line 54

def singleton_definitions
  @singleton_definitions
end

#type_param_namesObject (readonly)

Returns the value of attribute type_param_names.



54
55
56
# File 'lib/rigor/environment/reflection.rb', line 54

def type_param_names
  @type_param_names
end

Instance Method Details

#class_known?(name) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/rigor/environment/reflection.rb', line 68

def class_known?(name)
  @known_class_names.include?(rooted(name))
end

#class_ordering(lhs, rhs) ⇒ Object

Three-valued ‘(lhs, rhs)` relation: `:equal` / `:subclass` / `:superclass` / `:disjoint` / `:unknown`. Mirrors Rigor::Environment::RbsHierarchy#class_ordering’s contract; the Reflection’s frozen ancestor table supports the same queries without any in-process mutation.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rigor/environment/reflection.rb', line 94

def class_ordering(lhs, rhs)
  lhs = unrooted(lhs)
  rhs = unrooted(rhs)
  return :equal if lhs == rhs

  lhs_ancestors = @ancestor_names[lhs]
  rhs_ancestors = @ancestor_names[rhs]
  return :unknown if lhs_ancestors.nil? || rhs_ancestors.nil? || lhs_ancestors.empty? || rhs_ancestors.empty?

  if lhs_ancestors.include?(rhs)
    :subclass
  elsif rhs_ancestors.include?(lhs)
    :superclass
  else
    :disjoint
  end
end

#class_type_param_names(name) ⇒ Object



80
81
82
# File 'lib/rigor/environment/reflection.rb', line 80

def class_type_param_names(name)
  @type_param_names.fetch(unrooted(name), [])
end

#constant_type(name) ⇒ Object



84
85
86
# File 'lib/rigor/environment/reflection.rb', line 84

def constant_type(name)
  @constant_types[rooted(name)]
end

#each_known_class_nameObject

Yields every known class / module / alias name in the loader’s canonical rooted form (‘“::Hash”`).



114
115
116
# File 'lib/rigor/environment/reflection.rb', line 114

def each_known_class_name(&)
  @known_class_names.each(&)
end

#instance_definition(name) ⇒ Object



72
73
74
# File 'lib/rigor/environment/reflection.rb', line 72

def instance_definition(name)
  @instance_definitions[rooted(name)]
end

#singleton_definition(name) ⇒ Object



76
77
78
# File 'lib/rigor/environment/reflection.rb', line 76

def singleton_definition(name)
  @singleton_definitions[rooted(name)]
end