-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathreflection.rb
282 lines (235 loc) · 9.74 KB
/
reflection.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
128
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
168
169
170
171
172
173
174
175
176
177
178
179
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
278
279
280
281
282
# typed: strict
# frozen_string_literal: true
# On Ruby 3.2 or newer, Class defines an attached_object method that returns the
# attached class of a singleton class without iterating ObjectSpace. On older
# versions of Ruby, we fall back to iterating ObjectSpace.
if Class.method_defined?(:attached_object)
require "tapioca/runtime/attached_class_of_32"
else
require "tapioca/runtime/attached_class_of_legacy"
end
module Tapioca
module Runtime
module Reflection
include AttachedClassOf
extend T::Sig
extend self
CLASS_METHOD = T.let(Kernel.instance_method(:class), UnboundMethod)
CONSTANTS_METHOD = T.let(Module.instance_method(:constants), UnboundMethod)
NAME_METHOD = T.let(Module.instance_method(:name), UnboundMethod)
SINGLETON_CLASS_METHOD = T.let(Object.instance_method(:singleton_class), UnboundMethod)
ANCESTORS_METHOD = T.let(Module.instance_method(:ancestors), UnboundMethod)
SUPERCLASS_METHOD = T.let(Class.instance_method(:superclass), UnboundMethod)
OBJECT_ID_METHOD = T.let(BasicObject.instance_method(:__id__), UnboundMethod)
EQUAL_METHOD = T.let(BasicObject.instance_method(:equal?), UnboundMethod)
PUBLIC_INSTANCE_METHODS_METHOD = T.let(Module.instance_method(:public_instance_methods), UnboundMethod)
PROTECTED_INSTANCE_METHODS_METHOD = T.let(Module.instance_method(:protected_instance_methods), UnboundMethod)
PRIVATE_INSTANCE_METHODS_METHOD = T.let(Module.instance_method(:private_instance_methods), UnboundMethod)
METHOD_METHOD = T.let(Kernel.instance_method(:method), UnboundMethod)
UNDEFINED_CONSTANT = T.let(Module.new.freeze, Module)
REQUIRED_FROM_LABELS = T.let(["<top (required)>", "<main>"].freeze, T::Array[String])
T::Sig::WithoutRuntime.sig { params(constant: BasicObject).returns(T::Boolean) }
def constant_defined?(constant)
!UNDEFINED_CONSTANT.eql?(constant)
end
sig do
params(
symbol: String,
inherit: T::Boolean,
namespace: Module,
).returns(BasicObject).checked(:never)
end
def constantize(symbol, inherit: false, namespace: Object)
namespace.const_get(symbol, inherit)
rescue NameError, LoadError, RuntimeError, ArgumentError, TypeError
UNDEFINED_CONSTANT
end
sig { params(object: BasicObject).returns(T::Class[T.anything]).checked(:never) }
def class_of(object)
CLASS_METHOD.bind_call(object)
end
sig { params(constant: Module).returns(T::Array[Symbol]) }
def constants_of(constant)
CONSTANTS_METHOD.bind_call(constant, false)
end
sig { params(constant: Module).returns(T.nilable(String)) }
def name_of(constant)
name = NAME_METHOD.bind_call(constant)
name&.start_with?("#<") ? nil : name
end
sig { params(constant: Module).returns(T::Class[T.anything]) }
def singleton_class_of(constant)
SINGLETON_CLASS_METHOD.bind_call(constant)
end
sig { params(constant: Module).returns(T::Array[Module]) }
def ancestors_of(constant)
ANCESTORS_METHOD.bind_call(constant)
end
sig { params(constant: T::Class[T.anything]).returns(T.nilable(T::Class[T.anything])) }
def superclass_of(constant)
SUPERCLASS_METHOD.bind_call(constant)
end
sig { params(object: BasicObject).returns(Integer).checked(:never) }
def object_id_of(object)
OBJECT_ID_METHOD.bind_call(object)
end
sig { params(object: BasicObject, other: BasicObject).returns(T::Boolean).checked(:never) }
def are_equal?(object, other)
EQUAL_METHOD.bind_call(object, other)
end
sig { params(constant: Module).returns(T::Array[Symbol]) }
def public_instance_methods_of(constant)
PUBLIC_INSTANCE_METHODS_METHOD.bind_call(constant)
end
sig { params(constant: Module).returns(T::Array[Symbol]) }
def protected_instance_methods_of(constant)
PROTECTED_INSTANCE_METHODS_METHOD.bind_call(constant)
end
sig { params(constant: Module).returns(T::Array[Symbol]) }
def private_instance_methods_of(constant)
PRIVATE_INSTANCE_METHODS_METHOD.bind_call(constant)
end
sig { params(constant: Module).returns(T::Array[Module]) }
def inherited_ancestors_of(constant)
if Class === constant
ancestors_of(superclass_of(constant) || Object)
else
Module.new.ancestors
end
end
sig { params(constant: Module).returns(T.nilable(String)) }
def qualified_name_of(constant)
name = name_of(constant)
return if name.nil?
if name.start_with?("::")
name
else
"::#{name}"
end
end
sig { params(method: T.any(UnboundMethod, Method)).returns(T.untyped) }
def signature_of!(method)
T::Utils.signature_for_method(method)
end
sig { params(method: T.any(UnboundMethod, Method)).returns(T.untyped) }
def signature_of(method)
signature_of!(method)
rescue LoadError, StandardError
nil
end
sig { params(type: T::Types::Base).returns(String) }
def name_of_type(type)
type.to_s
end
sig { params(constant: Module, method: Symbol).returns(Method) }
def method_of(constant, method)
METHOD_METHOD.bind_call(constant, method)
end
# Returns an array with all classes that are < than the supplied class.
#
# class C; end
# descendants_of(C) # => []
#
# class B < C; end
# descendants_of(C) # => [B]
#
# class A < B; end
# descendants_of(C) # => [B, A]
#
# class D < C; end
# descendants_of(C) # => [B, A, D]
sig do
type_parameters(:U)
.params(klass: T.all(T::Class[T.anything], T.type_parameter(:U)))
.returns(T::Array[T.type_parameter(:U)])
end
def descendants_of(klass)
result = ObjectSpace.each_object(klass.singleton_class).reject do |k|
k.singleton_class? || k == klass
end
T.unsafe(result)
end
# Examines the call stack to identify the closest location where a "require" is performed
# by searching for the label "<top (required)>". If none is found, it returns the location
# labeled "<main>", which is the original call site.
sig { params(locations: T.nilable(T::Array[Thread::Backtrace::Location])).returns(String) }
def resolve_loc(locations)
return "" unless locations
resolved_loc = locations.find { |loc| REQUIRED_FROM_LABELS.include?(loc.label) }
return "" unless resolved_loc
resolved_loc.absolute_path || ""
end
sig { params(constant: Module).returns(T::Set[String]) }
def file_candidates_for(constant)
relevant_methods_for(constant).filter_map do |method|
method.source_location&.first
end.to_set
end
sig { params(constant: Module).returns(T.untyped) }
def abstract_type_of(constant)
T::Private::Abstract::Data.get(constant, :abstract_type) ||
T::Private::Abstract::Data.get(singleton_class_of(constant), :abstract_type)
end
sig { params(constant: Module).returns(T::Boolean) }
def final_module?(constant)
T::Private::Final.final_module?(constant)
end
sig { params(constant: Module).returns(T::Boolean) }
def sealed_module?(constant)
T::Private::Sealed.sealed_module?(constant)
end
private
sig { params(constant: Module).returns(T::Array[UnboundMethod]) }
def relevant_methods_for(constant)
methods = methods_for(constant).select(&:source_location)
.reject { |x| method_defined_by_forwardable_module?(x) }
return methods unless methods.empty?
constants_of(constant).flat_map do |const_name|
if (mod = child_module_for_parent_with_name(constant, const_name.to_s))
relevant_methods_for(mod)
else
[]
end
end
end
sig { params(constant: Module).returns(T::Array[UnboundMethod]) }
def methods_for(constant)
modules = [constant, singleton_class_of(constant)]
method_list_methods = [
PUBLIC_INSTANCE_METHODS_METHOD,
PROTECTED_INSTANCE_METHODS_METHOD,
PRIVATE_INSTANCE_METHODS_METHOD,
]
modules.product(method_list_methods).flat_map do |mod, method_list_method|
method_list_method.bind_call(mod, false).map { |name| mod.instance_method(name) }
end
end
sig { params(parent: Module, name: String).returns(T.nilable(Module)) }
def child_module_for_parent_with_name(parent, name)
return if parent.autoload?(name)
child = constantize(name, inherit: true, namespace: parent)
return unless Module === child
return unless name_of(child) == "#{name_of(parent)}::#{name}"
child
end
sig { params(method: UnboundMethod).returns(T::Boolean) }
def method_defined_by_forwardable_module?(method)
method.source_location&.first == Object.const_source_location(:Forwardable)&.first
end
sig { params(name: String).returns(T::Boolean) }
def has_aliased_namespace?(name)
name_parts = name.split("::")
name_parts.pop # drop the constant name, leaving just the namespace
name_parts.each_with_object([]) do |name_part, namespaces|
namespaces << "#{namespaces.last}::#{name_part}".delete_prefix("::")
end.any? do |namespace|
constant = constantize(namespace)
next unless Module === constant
# If the constant name doesn't match the namespace,
# the namespace must contain an alias
name_of(constant) != namespace
end
end
end
end
end