Commit 2ce8581a

2ce8581a6b9aMerged

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

Description

Merge PR #1522: match Array#assoc/#rassoc against any pair's array kind

[[1, 2], [3, 4]].assoc(3) returned nil (same for #rassoc): the runtime skipped every pair whose cls_id was not SP_BUILTIN_POLY_ARRAY, but a like-typed pair is a homogeneous typed array (IntArray). assoc/rassoc now compare the keyed element via sp_poly_arr_get (boxes from any array kind) and return the match through a new sp_pair_to_poly helper (boxes a non-poly pair into a PolyArray) so the PolyArray* return type holds. Adds the SYM_ARRAY arm to sp_poly_arr_get and an sp_array_kind_len guard so a nil search key cannot match an out-of-bounds read.

Combined-state gates: test 1007/0/0, bench 57/57, optcarrot checksum 59662.

Closes #1522

Co-authored-by: Ryan Seys [email protected] Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
2ce8581a6b9a
Parent
bc26201f4e60
Merge parent
10455559867f

lib/sp_runtime.hmodifiedView file

@@ -3786,6 +3786,7 @@
37863786 if (a.tag != SP_TAG_OBJ) return sp_box_nil();
37873787 switch (a.cls_id) {
37883788 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]); }
3789+ 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]); }
37893790 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]); }
37903791 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]); }
37913792 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]; }
@@ -4021,31 +4022,57 @@
40214022 char *r = sp_str_alloc(out); memcpy(r, buf, out); free(buf); return r;
40224023 }
40234024
4024-/* Array#assoc — return the first sub-array (poly_array element)
4025- whose first element equals `key`. Returns NULL when no match
4026- so the caller's `.inspect` round-trips to "nil". */
4025+/* Element count of an array-kind value, or -1 if `el` is not an array (a
4026+ non-object, a user object, a hash, etc.). Lets assoc/rassoc skip non-array
4027+ and too-short pairs without indexing them, so a `nil` search key cannot
4028+ spuriously match an out-of-bounds (nil) read. */
4029+static mrb_int sp_array_kind_len(sp_RbVal el) {
4030+ if (el.tag != SP_TAG_OBJ || !el.v.p) return -1;
4031+ switch (el.cls_id) {
4032+ case SP_BUILTIN_INT_ARRAY:
4033+ case SP_BUILTIN_SYM_ARRAY: return ((sp_IntArray *)el.v.p)->len;
4034+ case SP_BUILTIN_FLT_ARRAY: return ((sp_FloatArray *)el.v.p)->len;
4035+ case SP_BUILTIN_STR_ARRAY: return ((sp_StrArray *)el.v.p)->len;
4036+ case SP_BUILTIN_POLY_ARRAY: return ((sp_PolyArray *)el.v.p)->len;
4037+ default: return -1;
4038+ }
4039+}
4040+
4041+/* Box any array-kind element into a PolyArray so assoc/rassoc can return it
4042+ through their PolyArray* type regardless of the matched pair's own kind
4043+ (a like-typed pair such as [1, 2] is an IntArray, not a PolyArray). */
4044+static sp_PolyArray *sp_pair_to_poly(sp_RbVal el) {
4045+ if (el.tag == SP_TAG_OBJ && el.cls_id == SP_BUILTIN_POLY_ARRAY) return (sp_PolyArray *)el.v.p;
4046+ mrb_int n = sp_array_kind_len(el);
4047+ if (n < 0) n = 0;
4048+ sp_PolyArray *r = sp_PolyArray_new();
4049+ SP_GC_ROOT(r);
4050+ for (mrb_int j = 0; j < n; j++) sp_PolyArray_push(r, sp_poly_arr_get(el, j));
4051+ return r;
4052+}
4053+
4054+/* Array#assoc — return the first sub-array whose first element equals `key`.
4055+ Returns NULL when no match so the caller's `.inspect` round-trips to "nil".
4056+ Each pair may be any array kind, so compare element 0 via sp_poly_arr_get;
4057+ a pair with no element 0 (a non-array or empty array) is skipped. */
40274058 static sp_PolyArray *sp_PolyArray_assoc(sp_PolyArray *a, sp_RbVal key) {
40284059 if (!a) return NULL;
40294060 for (mrb_int i = 0; i < a->len; i++) {
40304061 sp_RbVal el = a->data[i];
4031- if (el.tag != SP_TAG_OBJ) continue;
4032- if (el.cls_id != SP_BUILTIN_POLY_ARRAY) continue;
4033- sp_PolyArray *sub = (sp_PolyArray *)el.v.p;
4034- if (sub && sub->len >= 1 && sp_poly_eq(sub->data[0], key)) return sub;
4062+ if (sp_array_kind_len(el) >= 1 && sp_poly_eq(sp_poly_arr_get(el, 0), key))
4063+ return sp_pair_to_poly(el);
40354064 }
40364065 return NULL;
40374066 }
40384067
40394068 /* Array#rassoc — same as assoc but matches against the second
4040- element of each sub-array. */
4069+ element of each sub-array (a pair with fewer than 2 elements is skipped). */
40414070 static sp_PolyArray *sp_PolyArray_rassoc(sp_PolyArray *a, sp_RbVal val) {
40424071 if (!a) return NULL;
40434072 for (mrb_int i = 0; i < a->len; i++) {
40444073 sp_RbVal el = a->data[i];
4045- if (el.tag != SP_TAG_OBJ) continue;
4046- if (el.cls_id != SP_BUILTIN_POLY_ARRAY) continue;
4047- sp_PolyArray *sub = (sp_PolyArray *)el.v.p;
4048- if (sub && sub->len >= 2 && sp_poly_eq(sub->data[1], val)) return sub;
4074+ if (sp_array_kind_len(el) >= 2 && sp_poly_eq(sp_poly_arr_get(el, 1), val))
4075+ return sp_pair_to_poly(el);
40494076 }
40504077 return NULL;
40514078 }