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 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_namesSet<String> of every class / module / alias name in the loaded RBS environment. Top-level prefixed ("::Hash"); plain queries normalise via #normalise.
  • instance_definitionsHash<String, RBS::Definition> keyed on RBS::TypeName#to_s (top-level prefixed).
  • singleton_definitions — same shape, singleton side.
  • type_param_namesHash<String, Array<Symbol>> of declared type parameters per class.
  • constant_typesHash<String, Rigor::Type> of translated constant declarations.
  • ancestor_namesHash<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.



38
39
40
41
42
43
44
45
46
47
# File 'lib/rigor/environment/reflection.rb', line 38

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.



35
36
37
# File 'lib/rigor/environment/reflection.rb', line 35

def ancestor_names
  @ancestor_names
end

#constant_typesObject (readonly)

Returns the value of attribute constant_types.



35
36
37
# File 'lib/rigor/environment/reflection.rb', line 35

def constant_types
  @constant_types
end

#instance_definitionsObject (readonly)

Returns the value of attribute instance_definitions.



35
36
37
# File 'lib/rigor/environment/reflection.rb', line 35

def instance_definitions
  @instance_definitions
end

#known_class_namesObject (readonly)

Returns the value of attribute known_class_names.



35
36
37
# File 'lib/rigor/environment/reflection.rb', line 35

def known_class_names
  @known_class_names
end

#singleton_definitionsObject (readonly)

Returns the value of attribute singleton_definitions.



35
36
37
# File 'lib/rigor/environment/reflection.rb', line 35

def singleton_definitions
  @singleton_definitions
end

#type_param_namesObject (readonly)

Returns the value of attribute type_param_names.



35
36
37
# File 'lib/rigor/environment/reflection.rb', line 35

def type_param_names
  @type_param_names
end

Instance Method Details

#class_known?(name) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/rigor/environment/reflection.rb', line 49

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.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rigor/environment/reflection.rb', line 72

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



61
62
63
# File 'lib/rigor/environment/reflection.rb', line 61

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

#constant_type(name) ⇒ Object



65
66
67
# File 'lib/rigor/environment/reflection.rb', line 65

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").



91
92
93
# File 'lib/rigor/environment/reflection.rb', line 91

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

#instance_definition(name) ⇒ Object



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

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

#singleton_definition(name) ⇒ Object



57
58
59
# File 'lib/rigor/environment/reflection.rb', line 57

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