Commit 32d326c6
32d326c660ebMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
Merge pull request #459 from ryanseys/rs-introspection-const-path
fix(codegen): resolve qualified-constant arg in is_a?/kind_of?/instance_of?
Details
- Commit
- 32d326c660eb
- Parent
- b90c30c3b0d9
- Merge parent
- a64a0cb5c09d
spinel_codegen.rbmodifiedView file
@@ -17147,9 +17147,29 @@
1714717147
""1714817148
end1714917149
17150+
def is_static_const_ref(aid)17151+
if @nd_type[aid] == "ConstantReadNode" || @nd_type[aid] == "ConstantPathNode"17152+
return 117153+
end17154+
017155+
end17156+
17157+
# Resolve an is_a?/kind_of?/instance_of? argument to its registered17158+
# class name. ConstantPathNode (`Foo::Bar`) walks the path via17159+
# resolve_const_ref_name; ConstantReadNode (`Foo`) carries the leaf17160+
# in @nd_name. Other shapes fall back to @nd_name so static lookup17161+
# still has a name to try.17162+
def resolve_introspection_arg_name(aid)17163+
if @nd_type[aid] == "ConstantPathNode"17164+
return resolve_const_ref_name(aid)17165+
end17166+
@nd_name[aid]17167+
end17168+
1715017169
def compile_introspection_expr(nid, mname, rc, recv_type)17151-
# is_a? - check class hierarchy17152-
if mname == "is_a?"17170+
# is_a? / kind_of? — kind_of? is an exact alias of is_a? in MRI.17171+
# Both walk the class hierarchy: cname is or inherits from arg0 → TRUE.17172+
if mname == "is_a?" || mname == "kind_of?"1715317173
if is_obj_type(recv_type) == 11715417174
cname = recv_type[4, recv_type.length - 4]1715517175
arg0 = ""@@ -17157,12 +17177,12 @@
1715717177
if args_id >= 01715817178
a = get_args(args_id)1715917179
if a.length > 017160-
# dynamic Class arg. The17161-
# statically-typed recv knows its own cls_id; the arg17162-
# carries the target cls_id at runtime. sp_class_le walks17163-
# the precomputed ancestors table to decide membership.17180+
# Dynamic Class arg (a non-const expression evaluating to a class).17181+
# The statically-typed recv knows its own cls_id; the arg carries17182+
# the target cls_id at runtime. sp_class_le walks the precomputed17183+
# ancestors table to decide membership.1716417184
arg_t = infer_type(a[0])17165-
if arg_t == "class" && @nd_type[a[0]] != "ConstantReadNode"17185+
if arg_t == "class" && is_static_const_ref(a[0]) == 01716617186
ci = find_class_idx(cname)1716717187
if ci >= 01716817188
@needs_class_table = 1@@ -17170,10 +17190,9 @@
1717017190
return "sp_class_le((sp_Class){" + cls_id_for_user_internal(ci).to_s + "LL}, " + compile_expr(a[0]) + ")"1717117191
end1717217192
end17173-
arg0 = @nd_name[a[0]]17193+
arg0 = resolve_introspection_arg_name(a[0])1717417194
end1717517195
end17176-
# Check if cname is or inherits from arg01717717196
if is_class_or_ancestor(cname, arg0) == 11717817197
return "TRUE"1717917198
else@@ -17183,6 +17202,29 @@
1718317202
return "FALSE"1718417203
end1718517204
17205+
# instance_of? — exact class identity, no ancestor walk. A parent17206+
# instance is never an exact-subclass match, so descendant resolution17207+
# is not relevant here; static comparison suffices.17208+
if mname == "instance_of?"17209+
if is_obj_type(recv_type) == 117210+
cname = recv_type[4, recv_type.length - 4]17211+
arg0 = ""17212+
args_id = @nd_arguments[nid]17213+
if args_id >= 017214+
a = get_args(args_id)17215+
if a.length > 017216+
arg0 = resolve_introspection_arg_name(a[0])17217+
end17218+
end17219+
if cname == arg017220+
return "TRUE"17221+
else17222+
return "FALSE"17223+
end17224+
end17225+
return "FALSE"17226+
end17227+
1718617228
# respond_to? - check if method exists1718717229
if mname == "respond_to?"1718817230
if is_obj_type(recv_type) == 1test/introspection_constant_path.rbaddedView file
Loading diff…
test/introspection_constant_path.rb.expectedaddedView file
Loading diff…