Commit 95ae83ab

95ae83abcecbMerged

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

Description

codegen,runtime: Class#< builtin-parent, string range cover/include, PolyArray#assoc/rassoc, String#dump

Five small adjacent dispatch fixes:

  • Class#< with a built-in parent: compute_ancestors_for_unified now falls back to unified_cls_id_for_name when a user class's declared parent isn't in the user table — the built-in ancestor chain (StandardError → Exception → Object, ...) is spliced into the user class's MRO so `A < StandardError` is true again.

  • Range#include? / cover? / === on a literal string range (`("a".."z")`) inlines strcmp bounds rather than trying to fit the receiver into sp_Range's int-only struct.

  • PolyArray#assoc / #rassoc walk the array for the first sub-array whose first / second element matches the arg. sp_PolyArray_inspect now NULL-guards to "nil" so an unmatched assoc round-trips through .inspect.

  • String#dump aliases to sp_str_inspect (same quoting subset).

Fixes #931 Fixes #930 Fixes #941 Fixes #946 Fixes #928

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
95ae83abcecb
Parent
86d91371b008

lib/sp_runtime.hmodifiedView file

@@ -2630,6 +2630,35 @@
26302630 sp_PolyArray_push(dst, v);
26312631 }
26322632 static sp_PolyArray *sp_PolyArray_flatten(sp_PolyArray *a) { sp_PolyArray *b = sp_PolyArray_new(); if (!a) return b; for (mrb_int i = 0; i < a->len; i++) sp_PolyArray_flatten_into(b, a->data[i]); return b; }
2633+
2634+/* Array#assoc — return the first sub-array (poly_array element)
2635+ whose first element equals `key`. Returns NULL when no match
2636+ so the caller's `.inspect` round-trips to "nil". */
2637+static sp_PolyArray *sp_PolyArray_assoc(sp_PolyArray *a, sp_RbVal key) {
2638+ if (!a) return NULL;
2639+ for (mrb_int i = 0; i < a->len; i++) {
2640+ sp_RbVal el = a->data[i];
2641+ if (el.tag != SP_TAG_OBJ) continue;
2642+ if (el.cls_id != SP_BUILTIN_POLY_ARRAY) continue;
2643+ sp_PolyArray *sub = (sp_PolyArray *)el.v.p;
2644+ if (sub && sub->len >= 1 && sp_poly_eq(sub->data[0], key)) return sub;
2645+ }
2646+ return NULL;
2647+}
2648+
2649+/* Array#rassoc — same as assoc but matches against the second
2650+ element of each sub-array. */
2651+static sp_PolyArray *sp_PolyArray_rassoc(sp_PolyArray *a, sp_RbVal val) {
2652+ if (!a) return NULL;
2653+ for (mrb_int i = 0; i < a->len; i++) {
2654+ sp_RbVal el = a->data[i];
2655+ if (el.tag != SP_TAG_OBJ) continue;
2656+ if (el.cls_id != SP_BUILTIN_POLY_ARRAY) continue;
2657+ sp_PolyArray *sub = (sp_PolyArray *)el.v.p;
2658+ if (sub && sub->len >= 2 && sp_poly_eq(sub->data[1], val)) return sub;
2659+ }
2660+ return NULL;
2661+}
26332662 /* Depth-bounded variant. depth==0 returns a shallow copy; each
26342663 recursive step decrements depth, and a negative depth means
26352664 "unlimited" (same as flatten without arg). Used by
@@ -2700,8 +2729,11 @@
27002729 }
27012730 /* Array#inspect for heterogeneous poly arrays. Each element dispatches
27022731 through sp_poly_inspect, so a mixed `[1, "x", :y]` renders
2703- `[1, "x", :y]` byte-for-byte identical to CRuby. */
2732+ `[1, "x", :y]` byte-for-byte identical to CRuby. NULL renders
2733+ "nil" so callers that store a nil-returning slot (assoc/rassoc
2734+ miss, etc.) round-trip cleanly through `.inspect`. */
27042735 static const char *sp_PolyArray_inspect(sp_PolyArray *a) {
2736+ if (!a) { char *r = sp_str_alloc(3); r[0] = 'n'; r[1] = 'i'; r[2] = 'l'; r[3] = 0; sp_str_set_len(r, 3); return r; }
27052737 sp_String *s = sp_String_new("[");
27062738 for (mrb_int i = 0; i < a->len; i++) {
27072739 if (i > 0) sp_String_append(s, ", ");