Commit b2587aed
b2587aed6e91Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
Deep-compare nested arrays (ptr_array) for Array#==
Array#== handled int/str/float/poly arrays but not ptr_array -- an array
of arrays/objects -- so [[1]] == [[1]] fell through to FALSE. The
elements are bare void* with no runtime tag, so no single runtime helper
can deep-compare them.
Compare element-wise inline using the statically-known element type's
own equality (sp_IntArray_eq / sp_StrArray_eq / ...), guarded on both
operands sharing an element type. A differing element type is != per
CRuby and falls through to FALSE.
Fixes #1322
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- b2587aed6e91
- Parent
- de8cf15e3c97
spinel_codegen.rbmodifiedView file
@@ -16547,6 +16547,28 @@
1654716547
"0"1654816548
end1654916549
16550+
# Equality of two ptr_array elements (each a `void*` from16551+
# sp_PtrArray_get) given the static element type. Typed-array elements16552+
# deep-compare via their own _eq; a nested ptr_array recurses one16553+
# level then falls back to identity; objects/unknown use pointer16554+
# identity (matches CRuby's default `==` for objects without a custom16555+
# `==`). Cast through the element struct so the helper type-checks.16556+
def ptr_array_elem_eq(elem_t, ea, eb)16557+
if elem_t == "int_array" || elem_t == "sym_array"16558+
return "sp_IntArray_eq((sp_IntArray *)(" + ea + "), (sp_IntArray *)(" + eb + "))"16559+
end16560+
if elem_t == "str_array"16561+
return "sp_StrArray_eq((sp_StrArray *)(" + ea + "), (sp_StrArray *)(" + eb + "))"16562+
end16563+
if elem_t == "float_array"16564+
return "sp_FloatArray_eq((sp_FloatArray *)(" + ea + "), (sp_FloatArray *)(" + eb + "))"16565+
end16566+
if elem_t == "poly_array"16567+
return "sp_PolyArray_eq((sp_PolyArray *)(" + ea + "), (sp_PolyArray *)(" + eb + "))"16568+
end16569+
"((" + ea + ") == (" + eb + "))"16570+
end16571+
1655016572
# Coerce one push/append argument to the array's element-slot type.1655116573
def push_arg_expr(aid, pfx)1655216574
if pfx == "IntArray"@@ -32207,6 +32229,23 @@
3220732229
return "(!sp_PolyArray_eq(" + lc + ", " + rc + "))"3220832230
end3220932231
end32232+
# ptr_array (array of arrays / objects): no single runtime helper can32233+
# deep-compare it -- the elements are bare void* with no runtime tag --32234+
# so compare element-wise inline using the statically-known element32235+
# type's own equality. Restricted to same-element-type operands; a32236+
# differing element type is `!=` per CRuby and falls through to FALSE.32237+
if is_ptr_array_type(lt) == 1 && is_ptr_array_type(at) == 1 && ptr_array_elem_type(lt) == ptr_array_elem_type(at)32238+
pe_eq = ptr_array_elem_type(lt)32239+
la_eq = new_temp32240+
ra_eq = new_temp32241+
i_eq = new_temp32242+
r_eq = new_temp32243+
ge_a = "sp_PtrArray_get(" + la_eq + ", " + i_eq + ")"32244+
ge_b = "sp_PtrArray_get(" + ra_eq + ", " + i_eq + ")"32245+
cmp_eq = ptr_array_elem_eq(pe_eq, ge_a, ge_b)32246+
pae = "({ sp_PtrArray *" + la_eq + " = (sp_PtrArray *)(" + lc + "); sp_PtrArray *" + ra_eq + " = (sp_PtrArray *)(" + rc + "); mrb_bool " + r_eq + "; if (!" + la_eq + " || !" + ra_eq + ") " + r_eq + " = (" + la_eq + " == " + ra_eq + "); else if (" + la_eq + "->len != " + ra_eq + "->len) " + r_eq + " = FALSE; else { " + r_eq + " = TRUE; for (mrb_int " + i_eq + " = 0; " + i_eq + " < " + la_eq + "->len; " + i_eq + "++) { if (!(" + cmp_eq + ")) { " + r_eq + " = FALSE; break; } } } " + r_eq + "; })"32247+
return op == "==" ? pae : "(!" + pae + ")"32248+
end3221032249
# `<poly_array> == [nil, nil, ...]` shape (Array.new(N) ==3221132250
# [nil, nil, nil] etc.). The RHS array literal would3221232251
# otherwise lower to int_array of zeros and fall through to atest/nested_array_eq.rbaddedView file
Loading diff…
test/nested_array_eq.rb.expectedaddedView file
Loading diff…