Commit a0c43409
a0c43409025aMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +2 added · ~3 modified
Description
fix(runtime,codegen,analyze): Array#flatten for poly + int_array_ptr_array (#739)
[[1, [2, [3]]]].flatten segfaulted: poly_array recv had no flatten
arm so the unresolved-call warning emitted 0, then the caller
dereferenced 0 as a poly_array pointer. The uniform [[1,2],[3,4]]
shape (typed as int_array_ptr_array) hit the same fallback for a
different reason.
Two emitters:
- poly_array: lib/sp_runtime.h gets sp_PolyArray_flatten + sp_PolyArray_flatten_into. The walker pushes scalars (SP_TAG_INT, string, sym, float, ...) as-is and recurses into array-tagged elements (SP_BUILTIN_INT_ARRAY, _STR_ARRAY, _POLY_ARRAY). Codegen's poly_array recv arm routes flatten to it.
- int_array_ptr_array: codegen's ptr_array arm gains a flatten arm for the int_array element case -- emits an inline loop that concatenates each inner IntArray into a fresh IntArray. analyze.rb's flatten arm returns int_array for this shape so callers see the right return type.
Test: test/poly_array_flatten.rb covers deep nesting, heterogeneous elements, uniform [[int...]], and empty.
475 tests pass; optcarrot checksum 59662.
Closes #739
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- a0c43409025a
- Parent
- feb3f07ede4a
lib/sp_runtime.hmodifiedView file
@@ -2002,6 +2002,20 @@
20022002
static sp_PolyArray *sp_PolyArray_dup(sp_PolyArray *a) { sp_PolyArray *b = sp_PolyArray_new(); for (mrb_int i = 0; i < a->len; i++) sp_PolyArray_push(b, a->data[i]); return b; }20032003
/* Array#compact for poly_array: keep elements whose tag is not SP_TAG_NIL. */20042004
static sp_PolyArray *sp_PolyArray_compact(sp_PolyArray *a) { sp_PolyArray *b = sp_PolyArray_new(); if (!a) return b; for (mrb_int i = 0; i < a->len; i++) { if (a->data[i].tag != SP_TAG_NIL) sp_PolyArray_push(b, a->data[i]); } return b; }2005+
/* Array#flatten -- walk into nested array values recursively. Each2006+
array-tagged element (IntArray / StrArray / SymArray / FloatArray /2007+
PolyArray) is expanded inline; scalars are appended as-is. Issue2008+
#739. */2009+
static void sp_PolyArray_flatten_into(sp_PolyArray *dst, sp_RbVal v) {2010+
if (v.tag != SP_TAG_OBJ) { sp_PolyArray_push(dst, v); return; }2011+
if (v.cls_id == SP_BUILTIN_INT_ARRAY) { sp_IntArray *ia = (sp_IntArray *)v.v.p; for (mrb_int i = 0; i < ia->len; i++) sp_PolyArray_push(dst, sp_box_int(ia->data[ia->start + i])); return; }2012+
if (v.cls_id == SP_BUILTIN_STR_ARRAY) { sp_StrArray *sa = (sp_StrArray *)v.v.p; for (mrb_int i = 0; i < sa->len; i++) sp_PolyArray_push(dst, sp_box_str(sa->data[i])); return; }2013+
if (v.cls_id == SP_BUILTIN_POLY_ARRAY) { sp_PolyArray *pa = (sp_PolyArray *)v.v.p; for (mrb_int i = 0; i < pa->len; i++) sp_PolyArray_flatten_into(dst, pa->data[i]); return; }2014+
/* Other array variants fall through as opaque elements; rare for2015+
deep-flatten use cases. */2016+
sp_PolyArray_push(dst, v);2017+
}2018+
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; }20052019
/* Sum the integer-tagged elements of a poly_array. Used by20062020
`Array#sum` on a poly_array whose runtime tags are uniform int20072021
(e.g. the result of `arr.map { _1[:int_key] }`). Non-int tagsspinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/poly_array_flatten.rbaddedView file
Loading diff…
test/poly_array_flatten.rb.expectedaddedView file
Loading diff…