-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathcalls.rb
667 lines (516 loc) · 10.3 KB
/
calls.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
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
344
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
def foo
puts "foo"
end
foo
def self.bar
puts "bar1"
end
self.bar
def self.bar
puts "bar2"
end
self.bar
self.foo
module M
def instance_m
singleton_m # NoMethodError
end
def self.singleton_m
instance_m # NoMethodError
end
instance_m # NoMethodError
self.instance_m # NoMethodError
singleton_m
self.singleton_m
end
M.instance_m # NoMethodError
M.singleton_m
def call_instance_m
instance_m # NoMethodError
end
class C
include M
instance_m # NoMethodError
self.instance_m # NoMethodError
singleton_m # NoMethodError
self.singleton_m # NoMethodError
def baz
instance_m
self.instance_m
singleton_m # NoMethodError
self.singleton_m # NoMethodError
end
end
c = C.new
c.baz
c.singleton_m # NoMethodError
c.instance_m
class D < C
def baz
super
end
end
d = D.new
d.baz
d.singleton_m # NoMethodError
d.instance_m
def optional_arg(a = 4, b: 5)
a.bit_length
b.bit_length
end
def call_block
yield 1
end
def foo()
var = Hash.new
var[1]
call_block { |x| var[x] }
end
class Integer
def bit_length; end
def abs; end
end
class String
def capitalize; end
end
module Kernel
alias :old_puts :puts
def puts x; old_puts x end
end
class Module
alias :old_include :include
def module_eval; end
def include x
old_include x
end
def prepend; end
def private; end
end
class Object < Module
include Kernel
def new; end
end
class Hash
alias :old_lookup :[]
def [] x; old_lookup(x) end
end
class Array
alias :old_lookup :[]
def [] x; old_lookup(x) end
def length; end
def foreach &body
x = 0
while x < self.length
yield x, self[x]
x += 1
end
end
end
def funny
yield "prefix: "
end
funny { |i| puts i.capitalize}
"a".capitalize
1.bit_length
1.abs
["a","b","c"].foreach { |i, v| puts "#{i} -> #{v.capitalize}"} # TODO should resolve to String.capitalize
[1,2,3].foreach { |i| i.bit_length}
[1,2,3].foreach { |i| puts i.capitalize} # NoMethodError
[1,-2,3].foreach { |_, v| puts v.abs} # TODO should resolve to Integer.abs
def indirect &b
call_block &b
end
indirect { |i| i.bit_length}
class S
def s_method
self.to_s
end
end
class A < S
def to_s
end
end
class B < S
def to_s
end
end
S.new.s_method
A.new.s_method
B.new.s_method
def private_on_main
end
private_on_main
class Singletons
def self.singleton_a
puts "singleton_a"
self.singleton_b
end
def self.singleton_b
puts "singleton_b"
self.singleton_c
end
def self.singleton_c
puts "singleton_c"
end
def self.singleton_d
puts "singleton_d"
self.singleton_a
end
def instance
def self.singleton_e
puts "singleton_e"
end
singleton_e
end
class << self
def singleton_f
puts "singleton_f"
end
end
def call_singleton_g
self.singleton_g
end
end
Singletons.singleton_a
Singletons.singleton_f
c1 = Singletons.new
c1.instance
c1.singleton_e
def c1.singleton_g;
puts "singleton_g_1"
end
c1.singleton_g
c1.call_singleton_g
def c1.singleton_g;
puts "singleton_g_2"
end
c1.singleton_g
c1.call_singleton_g
class << c1
def singleton_g;
puts "singleton_g_3"
end
end
c1.singleton_g
c1.call_singleton_g
c2 = Singletons.new
c2.singleton_e # NoMethodError
c2.singleton_g # NoMethodError
self.new # NoMethodError
puts "top-level"
def Singletons.singleton_g
puts "singleton_g"
end
Singletons.singleton_g
c1.singleton_g
c1.call_singleton_g
c2.singleton_g # NoMethodError
c3 = Singletons.new
c3.singleton_g # NoMethodError
def create(type)
type.new.instance
def type.singleton_h
puts "singleton_h"
end
type.singleton_h
end
create Singletons
Singletons.singleton_h
x = Singletons
class << x
def singleton_i
puts "singleton_i"
end
end
x.singleton_i
Singletons.singleton_i
class << Singletons
def singleton_j
puts "singleton_j"
end
end
Singletons.singleton_j
class SelfNew
def instance
puts "SelfNew#instance"
new.instance # NoMethodError
end
def self.singleton
new.instance
end
new.instance
end
SelfNew.singleton
class C1
def instance
puts "C1#instance"
end
def return_self
self
end
end
class C2 < C1
def instance
puts "C2#instance"
end
end
class C3 < C2
def instance
puts "C3#instance"
end
end
def pattern_dispatch x
case x
when C3
x.instance
when C2
x.instance
when C1
x.instance
else
end
case x
in C3 then x.instance
in C2 => c2 then c2.instance
in C1 => c1 then c1.instance
end
end
c1 = C1.new
c1.instance
pattern_dispatch (C1.new)
pattern_dispatch (C2.new)
pattern_dispatch (C3.new)
C3.new.return_self.instance
def add_singleton x
def x.instance
puts "instance_on x"
end
end
c3 = C1.new
add_singleton c3
c3.instance
c3.return_self.instance
class SingletonOverride1
class << self
def singleton1
puts "SingletonOverride1#singleton1"
end
def call_singleton1
singleton1
end
def factory
self.new.instance1
end
end
def self.singleton2
puts "SingletonOverride1#singleton2"
end
def self.call_singleton2
singleton2
end
singleton2
def instance1
puts "SingletonOverride1#instance1"
end
end
SingletonOverride1.singleton1
SingletonOverride1.singleton2
SingletonOverride1.call_singleton1
SingletonOverride1.call_singleton2
class SingletonOverride2 < SingletonOverride1
class << self
def singleton1
puts "SingletonOverride2#singleton1"
end
end
def self.singleton2
puts "SingletonOverride2#singleton2"
end
def instance1
puts "SingletonOverride2#instance1"
end
end
SingletonOverride2.singleton1
SingletonOverride2.singleton2
SingletonOverride2.call_singleton1
SingletonOverride2.call_singleton2
class ConditionalInstanceMethods
if rand() > 0 then
def m1
puts "ConditionalInstanceMethods#m1"
end
end
def m2
puts "ConditionalInstanceMethods#m2"
def m3
puts "ConditionalInstanceMethods#m3"
def m4
puts "ConditionalInstanceMethods#m4"
end
end
m3
end
if rand() > 0 then
Class.new do
def m5
puts "AnonymousClass#m5"
end
end.new.m5
end
end
ConditionalInstanceMethods.new.m1
ConditionalInstanceMethods.new.m3 # NoMethodError
ConditionalInstanceMethods.new.m2
ConditionalInstanceMethods.new.m3 # currently unable to resolve
ConditionalInstanceMethods.new.m4 # currently unable to resolve
ConditionalInstanceMethods.new.m5 # NoMethodError
EsotericInstanceMethods = Class.new do
[0,1,2].each do
def foo
puts "foo"
end
end
Class.new do
def bar
puts "bar"
end
end.new.bar
[0,1,2].each do |i|
define_method("baz_#{i}") do
puts "baz_#{i}"
end
end
end
EsotericInstanceMethods.new.foo # currently unable to resolve
EsotericInstanceMethods.new.bar # NoMethodError
EsotericInstanceMethods.new.baz_0 # currently unable to resolve
EsotericInstanceMethods.new.baz_1 # currently unable to resolve
EsotericInstanceMethods.new.baz_2 # currently unable to resolve
module ExtendSingletonMethod
def singleton
puts "ExtendSingletonMethod#singleton"
end
extend self
end
ExtendSingletonMethod.singleton
module ExtendSingletonMethod2
extend ExtendSingletonMethod
end
ExtendSingletonMethod2.singleton
module ExtendSingletonMethod3
end
ExtendSingletonMethod3.extend ExtendSingletonMethod
ExtendSingletonMethod3.singleton
foo = "hello"
foo.singleton # NoMethodError
foo.extend ExtendSingletonMethod
foo.singleton
module ProtectedMethodInModule
protected def foo
puts "ProtectedMethodInModule#foo"
end
end
class ProtectedMethods
include ProtectedMethodInModule
protected def bar
puts "ProtectedMethods#bar"
end
def baz
foo
bar
ProtectedMethods.new.foo
ProtectedMethods.new.bar
end
end
ProtectedMethods.new.foo # NoMethodError
ProtectedMethods.new.bar # NoMethodError
ProtectedMethods.new.baz
class ProtectedMethodsSub < ProtectedMethods
def baz
foo
ProtectedMethodsSub.new.foo
end
end
ProtectedMethodsSub.new.foo # NoMethodError
ProtectedMethodsSub.new.bar # NoMethodError
ProtectedMethodsSub.new.baz
[C.new].each { |c| c.baz }
["a","b","c"].each { |s| s.capitalize }
class SingletonUpCall_Base
def self.singleton
end
end
class SingletonUpCall_Sub < SingletonUpCall_Base
singleton
singleton2 # should not resolve
def self.mid_method
singleton
singleton2 # should resolve
end
end
class SingletonUpCall_SubSub < SingletonUpCall_Sub
def self.singleton2
end
mid_method
end
class SingletonA
def self.singleton1
end
def self.call_singleton1
singleton1
end
def self.call_call_singleton1
call_singleton1
end
end
class SingletonB < SingletonA
def self.singleton1
end
def self.call_singleton1
singleton1 # should not be able to target `SingletonA:::singleton1` and `SingletonC:::singleton1`
end
end
class SingletonC < SingletonA
def self.singleton1
end
def self.call_singleton1
singleton1 # should not be able to target `SingletonA:::singleton1` and `SingletonB:::singleton1`
end
end
SingletonA.call_call_singleton1
SingletonB.call_call_singleton1
SingletonC.call_call_singleton1
module Included
def foo
self.bar
end
def bar
end
end
class IncludesIncluded
include Included
def bar
super
end
end
class CustomNew1
def self.new
C1.new
end
end
CustomNew1.new.instance
class CustomNew2
def self.new
self.allocate
end
def instance
puts "CustomNew2#instance"
end
end
CustomNew2.new.instance
def capture_parameter x
[0,1,2].each do
x
end
end
(capture_parameter C1.new).instance # NoMethodError