Skip to content

Commit dc6be92

Browse files
committed
Add a special handling for obj.class
`self.class` will return the class of its context
1 parent 1ec0ab1 commit dc6be92

3 files changed

Lines changed: 51 additions & 5 deletions

File tree

lib/typeprof/core/builtin.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,21 @@ def initialize(genv)
55
end
66

77
def class_new(changes, node, ty, a_args, ret)
8-
ty = ty.get_instance_type(@genv)
9-
recv = Source.new(ty)
10-
changes.add_method_call_box(@genv, recv, :initialize, a_args, false)
11-
changes.add_edge(@genv, Source.new(ty), ret)
8+
if ty.is_a?(Type::Singleton)
9+
ty = ty.get_instance_type(@genv)
10+
recv = Source.new(ty)
11+
changes.add_method_call_box(@genv, recv, :initialize, a_args, false)
12+
changes.add_edge(@genv, Source.new(ty), ret)
13+
end
14+
true
15+
end
16+
17+
def object_class(changes, node, ty, a_args, ret)
18+
ty = ty.base_type(@genv)
19+
mod = ty.is_a?(Type::Instance) ? ty.mod : @genv.mod_class
20+
ty = Type::Singleton.new(@genv, mod)
21+
vtx = Source.new(ty)
22+
changes.add_edge(@genv, vtx, ret)
1223
true
1324
end
1425

@@ -119,6 +130,7 @@ def hash_aset(changes, node, ty, a_args, ret)
119130
def deploy
120131
{
121132
class_new: [[:Class], false, :new],
133+
object_class: [[:Object], false, :class],
122134
proc_call: [[:Proc], false, :call],
123135
array_aref: [[:Array], false, :[]],
124136
array_aset: [[:Array], false, :[]=],

lib/typeprof/core/env.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def initialize
4242

4343
attr_reader :type_table
4444

45-
attr_reader :mod_object, :mod_ary, :mod_hash, :mod_range, :mod_str
45+
attr_reader :mod_class, :mod_object, :mod_ary, :mod_hash, :mod_range, :mod_str
4646
attr_reader :cls_type, :mod_type
4747
attr_reader :obj_type, :nil_type, :true_type, :false_type, :str_type
4848
attr_reader :int_type, :float_type, :rational_type, :complex_type

scenario/misc/class_method.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## update: test.rb
2+
def self_class
3+
self.class
4+
end
5+
6+
def int_class
7+
1.class
8+
end
9+
10+
def array_class
11+
[1].class
12+
end
13+
14+
def hash_class
15+
{ 1 => "str" }.class
16+
end
17+
18+
def class_class
19+
Object.class
20+
end
21+
22+
def unknown_class(x)
23+
x.class
24+
end
25+
26+
## assert
27+
class Object
28+
def self_class: -> singleton(Object)
29+
def int_class: -> singleton(Integer)
30+
def array_class: -> singleton(Array)
31+
def hash_class: -> singleton(Hash)
32+
def class_class: -> singleton(Class)
33+
def unknown_class: (untyped) -> untyped
34+
end

0 commit comments

Comments
 (0)