Commit be8666c6

be8666c66f8dMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

Merge pull request #329 from ryanseys/fix/module-method-defined

fix(codegen): Module#method_defined? compile-time

Details

Commit
be8666c66f8d
Parent
e586e70b8fad
Merge parent
d5d9d0f252f6

spinel_codegen.rbmodifiedView file

@@ -3106,7 +3106,7 @@
31063106 if mname == "is_a?" || mname == "kind_of?" || mname == "instance_of?"
31073107 return "bool"
31083108 end
3109- if mname == "respond_to?"
3109+ if mname == "respond_to?" || mname == "method_defined?"
31103110 return "bool"
31113111 end
31123112 if mname == "chr"
@@ -10817,7 +10817,9 @@
1081710817
1081810818 # Does class `ci` provide `mname` as a reader, writer, or method?
1081910819 # Walks parent classes for inherited members.
10820- def class_has_method(ci, mname)
10820+ # Does class `ci` define `mname` directly — instance method, attr
10821+ # reader, or attr writer — without walking the parent chain?
10822+ def class_has_method_local(ci, mname)
1082110823 readers = @cls_attr_readers[ci].split(";")
1082210824 if not_in(mname, readers) == 0
1082310825 return 1
@@ -10833,6 +10835,13 @@
1083310835 if not_in(mname, mnames) == 0
1083410836 return 1
1083510837 end
10838+ return 0
10839+ end
10840+
10841+ def class_has_method(ci, mname)
10842+ if class_has_method_local(ci, mname) == 1
10843+ return 1
10844+ end
1083610845 if @cls_parents[ci] != ""
1083710846 pi = find_class_idx(@cls_parents[ci])
1083810847 if pi >= 0
@@ -24850,6 +24859,31 @@
2485024859 def compile_constant_recv_expr(nid, mname, recv, rc)
2485124860 rcname = constructor_class_name(recv)
2485224861 if rcname != ""
24862+ # Foo.method_defined?(:sym[, inherit=true]) — compile-time decide.
24863+ # Default arm walks the parent chain via class_has_method (covers
24864+ # instance methods, attr readers/writers, ancestors). When the
24865+ # second arg is the literal `false`, restrict the lookup to the
24866+ # receiver's own methods (no parent walk) via class_has_method_local.
24867+ if mname == "method_defined?"
24868+ ci_md = find_class_idx(rcname)
24869+ if ci_md >= 0
24870+ args_id_md = @nd_arguments[nid]
24871+ if args_id_md >= 0
24872+ a_md = get_args(args_id_md)
24873+ if a_md.length > 0
24874+ if @nd_type[a_md[0]] == "SymbolNode" || @nd_type[a_md[0]] == "StringNode"
24875+ lit_name = @nd_content[a_md[0]]
24876+ inherit_md = 1
24877+ if a_md.length >= 2 && @nd_type[a_md[1]] == "FalseNode"
24878+ inherit_md = 0
24879+ end
24880+ hit = inherit_md == 1 ? class_has_method(ci_md, lit_name) : class_has_method_local(ci_md, lit_name)
24881+ return hit == 1 ? "TRUE" : "FALSE"
24882+ end
24883+ end
24884+ end
24885+ end
24886+ end
2485324887 # ARGV methods
2485424888 if rcname == "ARGV"
2485524889 if mname == "length"