Commit 10455559
10455559867fMerged
Ryan Seys <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
runtime: match Array#assoc/#rassoc against any pair's array kind
[[1, 2], [3, 4]].assoc(3) returned nil. sp_PolyArray_assoc/rassoc skipped
every element whose cls_id was not SP_BUILTIN_POLY_ARRAY, but a pair of
like-typed values is a homogeneous typed array ([1, 2] is an IntArray), so
those pairs were never compared. Mixed pairs (["a", 1]) are PolyArrays and
already worked.
Compare the keyed element with sp_poly_arr_get, which boxes an element from any array kind, and return the matched pair through a new sp_pair_to_poly that boxes a non-poly pair into a PolyArray so the functions' PolyArray* return type still holds. The matched pair round-trips through .inspect unchanged.
Guard both functions with a new sp_array_kind_len that reports the element
count of an array-kind value and -1 for anything that is not an array (a
non-object, a user object, a hash). assoc requires length >= 1 and rassoc
length >= 2 before indexing, so a nil search key no longer spuriously
matches an out-of-bounds (nil) read on an empty, too-short, or non-array
element -- [[], [1], (1..2)].assoc(nil) is nil, as in CRuby.
sp_poly_arr_get was missing its SP_BUILTIN_SYM_ARRAY arm and returned nil for
every symbol-array element, so [[:a, :b]].assoc(:c) could not match and a
matched symbol pair boxed to nils. Add the arm at the source (it boxes
ar->data[ar->start + i] with sp_box_sym) so assoc/rassoc and every other
sp_poly_arr_get caller handle symbol arrays.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 10455559867f
- Parent
- 3d4efd9a07a7
lib/sp_runtime.hmodifiedView file
if (a.tag != SP_TAG_OBJ) return sp_box_nil(); switch (a.cls_id) { case SP_BUILTIN_INT_ARRAY: { sp_IntArray *ar=(sp_IntArray*)a.v.p; if(!ar||i<0||i>=ar->len) return sp_box_nil(); return sp_box_int(ar->data[ar->start+i]); } case SP_BUILTIN_SYM_ARRAY: { sp_IntArray *ar=(sp_IntArray*)a.v.p; if(!ar||i<0||i>=ar->len) return sp_box_nil(); return sp_box_sym((sp_sym)ar->data[ar->start+i]); } case SP_BUILTIN_FLT_ARRAY: { sp_FloatArray *ar=(sp_FloatArray*)a.v.p; if(!ar||i<0||i>=ar->len) return sp_box_nil(); return sp_box_float(ar->data[i]); } case SP_BUILTIN_STR_ARRAY: { sp_StrArray *ar=(sp_StrArray*)a.v.p; if(!ar||i<0||i>=ar->len) return sp_box_nil(); return sp_box_str(ar->data[i]); } case SP_BUILTIN_POLY_ARRAY: { sp_PolyArray *ar=(sp_PolyArray*)a.v.p; if(!ar||i<0||i>=ar->len) return sp_box_nil(); return ar->data[i]; } char *r = sp_str_alloc(out); memcpy(r, buf, out); free(buf); return r;}/* Array#assoc — return the first sub-array (poly_array element) whose first element equals `key`. Returns NULL when no match so the caller's `.inspect` round-trips to "nil". *//* Element count of an array-kind value, or -1 if `el` is not an array (a non-object, a user object, a hash, etc.). Lets assoc/rassoc skip non-array and too-short pairs without indexing them, so a `nil` search key cannot spuriously match an out-of-bounds (nil) read. */static mrb_int sp_array_kind_len(sp_RbVal el) { if (el.tag != SP_TAG_OBJ || !el.v.p) return -1; switch (el.cls_id) { case SP_BUILTIN_INT_ARRAY: case SP_BUILTIN_SYM_ARRAY: return ((sp_IntArray *)el.v.p)->len; case SP_BUILTIN_FLT_ARRAY: return ((sp_FloatArray *)el.v.p)->len; case SP_BUILTIN_STR_ARRAY: return ((sp_StrArray *)el.v.p)->len; case SP_BUILTIN_POLY_ARRAY: return ((sp_PolyArray *)el.v.p)->len; default: return -1; }}/* Box any array-kind element into a PolyArray so assoc/rassoc can return it through their PolyArray* type regardless of the matched pair's own kind (a like-typed pair such as [1, 2] is an IntArray, not a PolyArray). */static sp_PolyArray *sp_pair_to_poly(sp_RbVal el) { if (el.tag == SP_TAG_OBJ && el.cls_id == SP_BUILTIN_POLY_ARRAY) return (sp_PolyArray *)el.v.p; mrb_int n = sp_array_kind_len(el); if (n < 0) n = 0; sp_PolyArray *r = sp_PolyArray_new(); SP_GC_ROOT(r); for (mrb_int j = 0; j < n; j++) sp_PolyArray_push(r, sp_poly_arr_get(el, j)); return r;}/* Array#assoc — return the first sub-array whose first element equals `key`. Returns NULL when no match so the caller's `.inspect` round-trips to "nil". Each pair may be any array kind, so compare element 0 via sp_poly_arr_get; a pair with no element 0 (a non-array or empty array) is skipped. */static sp_PolyArray *sp_PolyArray_assoc(sp_PolyArray *a, sp_RbVal key) { if (!a) return NULL; for (mrb_int i = 0; i < a->len; i++) { sp_RbVal el = a->data[i]; if (el.tag != SP_TAG_OBJ) continue; if (el.cls_id != SP_BUILTIN_POLY_ARRAY) continue; sp_PolyArray *sub = (sp_PolyArray *)el.v.p; if (sub && sub->len >= 1 && sp_poly_eq(sub->data[0], key)) return sub; if (sp_array_kind_len(el) >= 1 && sp_poly_eq(sp_poly_arr_get(el, 0), key)) return sp_pair_to_poly(el); } return NULL;}/* Array#rassoc — same as assoc but matches against the second element of each sub-array. */ element of each sub-array (a pair with fewer than 2 elements is skipped). */static sp_PolyArray *sp_PolyArray_rassoc(sp_PolyArray *a, sp_RbVal val) { if (!a) return NULL; for (mrb_int i = 0; i < a->len; i++) { sp_RbVal el = a->data[i]; if (el.tag != SP_TAG_OBJ) continue; if (el.cls_id != SP_BUILTIN_POLY_ARRAY) continue; sp_PolyArray *sub = (sp_PolyArray *)el.v.p; if (sub && sub->len >= 2 && sp_poly_eq(sub->data[1], val)) return sub; if (sp_array_kind_len(el) >= 2 && sp_poly_eq(sp_poly_arr_get(el, 1), val)) return sp_pair_to_poly(el); } return NULL;}test/array_assoc_rassoc.rbaddedView file
Loading diff…
test/array_assoc_rassoc.rb.expectedaddedView file
Loading diff…