Skip to content

Commit 36f1ab9

Browse files
nirvdrumtekknolagi
authored andcommitted
ZJIT: Add tests for opt_newarray_send with target methods redefined
1 parent 7d2f9ab commit 36f1ab9

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

test/ruby/test_zjit.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,22 @@ def test(x)
10401040
}, insns: [:opt_newarray_send], call_threshold: 1
10411041
end
10421042

1043+
def test_opt_newarray_send_include_p_redefinition
1044+
assert_compiles '[true, false]', %q{
1045+
class Array
1046+
alias_method :old_include?, :include?
1047+
def include?(x)
1048+
old_include?(x)
1049+
end
1050+
end
1051+
1052+
def test(x)
1053+
[:y, 1, Object.new].include?(x)
1054+
end
1055+
[test(1), test("n")]
1056+
}, insns: [:opt_newarray_send], call_threshold: 1
1057+
end
1058+
10431059
def test_opt_duparray_send_include_p
10441060
assert_compiles '[true, false]', %q{
10451061
def test(x)
@@ -1049,6 +1065,22 @@ def test(x)
10491065
}, insns: [:opt_duparray_send], call_threshold: 1
10501066
end
10511067

1068+
def test_opt_duparray_send_include_p_redefinition
1069+
assert_compiles '[true, false]', %q{
1070+
class Array
1071+
alias_method :old_include?, :include?
1072+
def include?(x)
1073+
old_include?(x)
1074+
end
1075+
end
1076+
1077+
def test(x)
1078+
[:y, 1].include?(x)
1079+
end
1080+
[test(1), test("n")]
1081+
}, insns: [:opt_duparray_send], call_threshold: 1
1082+
end
1083+
10521084
def test_opt_newarray_send_hash
10531085
assert_compiles 'Integer', %q{
10541086
def test(x)
@@ -1069,6 +1101,27 @@ def test(x)
10691101
}, insns: [:opt_newarray_send], call_threshold: 1
10701102
end
10711103

1104+
def test_opt_newarray_send_max
1105+
assert_compiles '[20, 40]', %q{
1106+
def test(a,b) = [a,b].max
1107+
[test(10, 20), test(40, 30)]
1108+
}, insns: [:opt_newarray_send], call_threshold: 1
1109+
end
1110+
1111+
def test_opt_newarray_send_max_redefinition
1112+
assert_compiles '[60, 90]', %q{
1113+
class Array
1114+
alias_method :old_max, :max
1115+
def max
1116+
old_max * 2
1117+
end
1118+
end
1119+
1120+
def test(a,b) = [a,b].max
1121+
[test(15, 30), test(45, 35)]
1122+
}, insns: [:opt_newarray_send], call_threshold: 1
1123+
end
1124+
10721125
def test_new_hash_empty
10731126
assert_compiles '{}', %q{
10741127
def test = {}

zjit/src/hir/tests.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1953,6 +1953,35 @@ pub mod hir_build_tests {
19531953
");
19541954
}
19551955

1956+
#[test]
1957+
fn test_opt_newarray_send_max_redefined() {
1958+
eval("
1959+
class Array
1960+
alias_method :old_max, :max
1961+
def max
1962+
old_max * 2
1963+
end
1964+
end
1965+
1966+
def test(a,b) = [a,b].max
1967+
");
1968+
assert_contains_opcode("test", YARVINSN_opt_newarray_send);
1969+
assert_snapshot!(hir_string("test"), @r"
1970+
fn test@<compiled>:9:
1971+
bb0():
1972+
EntryPoint interpreter
1973+
v1:BasicObject = LoadSelf
1974+
v2:BasicObject = GetLocal l0, SP@5
1975+
v3:BasicObject = GetLocal l0, SP@4
1976+
Jump bb2(v1, v2, v3)
1977+
bb1(v6:BasicObject, v7:BasicObject, v8:BasicObject):
1978+
EntryPoint JIT(0)
1979+
Jump bb2(v6, v7, v8)
1980+
bb2(v10:BasicObject, v11:BasicObject, v12:BasicObject):
1981+
SideExit PatchPoint(BOPRedefined(ARRAY_REDEFINED_OP_FLAG, BOP_MAX))
1982+
");
1983+
}
1984+
19561985
#[test]
19571986
fn test_opt_newarray_send_min() {
19581987
eval("
@@ -2135,6 +2164,45 @@ pub mod hir_build_tests {
21352164
");
21362165
}
21372166

2167+
#[test]
2168+
fn test_opt_newarray_send_include_p_redefined() {
2169+
eval("
2170+
class Array
2171+
alias_method :old_include?, :include?
2172+
def include?(x)
2173+
old_include?(x)
2174+
end
2175+
end
2176+
2177+
def test(a,b)
2178+
sum = a+b
2179+
result = [a,b].include? b
2180+
puts [1,2,3]
2181+
result
2182+
end
2183+
");
2184+
assert_contains_opcode("test", YARVINSN_opt_newarray_send);
2185+
assert_snapshot!(hir_string("test"), @r"
2186+
fn test@<compiled>:10:
2187+
bb0():
2188+
EntryPoint interpreter
2189+
v1:BasicObject = LoadSelf
2190+
v2:BasicObject = GetLocal l0, SP@7
2191+
v3:BasicObject = GetLocal l0, SP@6
2192+
v4:NilClass = Const Value(nil)
2193+
v5:NilClass = Const Value(nil)
2194+
Jump bb2(v1, v2, v3, v4, v5)
2195+
bb1(v8:BasicObject, v9:BasicObject, v10:BasicObject):
2196+
EntryPoint JIT(0)
2197+
v11:NilClass = Const Value(nil)
2198+
v12:NilClass = Const Value(nil)
2199+
Jump bb2(v8, v9, v10, v11, v12)
2200+
bb2(v14:BasicObject, v15:BasicObject, v16:BasicObject, v17:NilClass, v18:NilClass):
2201+
v25:BasicObject = SendWithoutBlock v15, :+, v16
2202+
SideExit PatchPoint(BOPRedefined(ARRAY_REDEFINED_OP_FLAG, BOP_INCLUDE_P))
2203+
");
2204+
}
2205+
21382206
#[test]
21392207
fn test_opt_duparray_send_include_p() {
21402208
eval("

0 commit comments

Comments
 (0)