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
end3109-
if mname == "respond_to?"3109+
if mname == "respond_to?" || mname == "method_defined?"31103110
return "bool"31113111
end31123112
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, attr10821+
# 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) == 01082310825
return 1@@ -10833,6 +10835,13 @@
1083310835
if not_in(mname, mnames) == 01083410836
return 11083510837
end10838+
return 010839+
end10840+
10841+
def class_has_method(ci, mname)10842+
if class_has_method_local(ci, mname) == 110843+
return 110844+
end1083610845
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 (covers24864+
# instance methods, attr readers/writers, ancestors). When the24865+
# second arg is the literal `false`, restrict the lookup to the24866+
# 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 >= 024870+
args_id_md = @nd_arguments[nid]24871+
if args_id_md >= 024872+
a_md = get_args(args_id_md)24873+
if a_md.length > 024874+
if @nd_type[a_md[0]] == "SymbolNode" || @nd_type[a_md[0]] == "StringNode"24875+
lit_name = @nd_content[a_md[0]]24876+
inherit_md = 124877+
if a_md.length >= 2 && @nd_type[a_md[1]] == "FalseNode"24878+
inherit_md = 024879+
end24880+
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+
end24883+
end24884+
end24885+
end24886+
end2485324887
# ARGV methods2485424888
if rcname == "ARGV"2485524889
if mname == "length"test/method_defined.rbaddedView file
Loading diff…
test/method_defined.rb.expectedaddedView file
Loading diff…