Commit 3042b632

3042b6324115Merged

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

Description

fix(codegen,runtime): 2-arg poly [] dispatches slice form + SP_TAG_STR arm

Fixes #659. The single-arg [] dispatch in emit_poly_builtin_dispatch dropped the second arg silently: each typed-array branch called *_Array_get(p, 0) and no SP_TAG_STR branch existed at all. A String-typed runtime value returned the empty string; an Array-typed value returned just element 0 instead of a length-N slice.

  • compile_poly_method_call: add SP_TAG_STR arm for the 2-arg form, dispatching to sp_str_sub_range(s, start, length).
  • emit_poly_builtin_dispatch: branch on arg_compiled.length == 2 and emit Array_slice for IntArray / FloatArray / StrArray / SymArray / PtrArray, boxing the resulting Array back through sp_box_array. Only fires under is_poly_ret since the slice's static type is an Array, which a concretely-typed (mrb_int / const char *) result tmp can't hold.

While here, fix sp_poly_puts to iterate Array tags one element per line (matching MRI's puts arr semantics) instead of printing the array's inspect form. The issue's expected output relies on this.

Closes #659.

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

Details

Commit
3042b6324115
Parent
b5903e3d38df

lib/sp_runtime.hmodifiedView file

@@ -1822,16 +1822,39 @@
18221822 case SP_TAG_NIL: putchar('\n'); break;
18231823 case SP_TAG_SYM: { const char *_ss = sp_sym_to_s((sp_sym)v.v.i); fputs(_ss, stdout); putchar('\n'); break; }
18241824 case SP_TAG_OBJ: {
1825- /* Built-in pointer types route through their inspect helpers
1826- (one line per array, matching `puts arr`'s short form). User
1827- classes (cls_id >= 0) fall through to the raw-pointer path
1828- since we don't have per-class inspect dispatch here. */
1825+ /* MRI's `puts arr` iterates an Array, printing one element per
1826+ line (using to_s on each); a non-Array OBJ falls back to
1827+ inspect / class-name. */
18291828 switch (v.cls_id) {
1830- case SP_BUILTIN_INT_ARRAY: puts(sp_IntArray_inspect((sp_IntArray *)v.v.p)); break;
1831- case SP_BUILTIN_FLT_ARRAY: puts(sp_FloatArray_inspect((sp_FloatArray *)v.v.p)); break;
1832- case SP_BUILTIN_STR_ARRAY: puts(sp_StrArray_inspect((sp_StrArray *)v.v.p)); break;
1833- case SP_BUILTIN_SYM_ARRAY: puts(sp_SymArray_inspect((sp_IntArray *)v.v.p)); break;
1834- case SP_BUILTIN_PTR_ARRAY: puts(sp_PtrArray_inspect((sp_PtrArray *)v.v.p)); break;
1829+ case SP_BUILTIN_INT_ARRAY: {
1830+ sp_IntArray *_a = (sp_IntArray *)v.v.p;
1831+ for (mrb_int _i = 0; _i < _a->len; _i++)
1832+ printf("%lld\n", (long long)_a->data[_a->start + _i]);
1833+ break;
1834+ }
1835+ case SP_BUILTIN_FLT_ARRAY: {
1836+ sp_FloatArray *_a = (sp_FloatArray *)v.v.p;
1837+ for (mrb_int _i = 0; _i < _a->len; _i++) {
1838+ fputs(sp_float_to_s(_a->data[_i]), stdout); putchar('\n');
1839+ }
1840+ break;
1841+ }
1842+ case SP_BUILTIN_STR_ARRAY: {
1843+ sp_StrArray *_a = (sp_StrArray *)v.v.p;
1844+ for (mrb_int _i = 0; _i < _a->len; _i++) {
1845+ const char *_s = _a->data[_i];
1846+ if (_s) { fputs(_s, stdout); if (!*_s || _s[strlen(_s)-1] != '\n') putchar('\n'); } else putchar('\n');
1847+ }
1848+ break;
1849+ }
1850+ case SP_BUILTIN_SYM_ARRAY: {
1851+ sp_IntArray *_a = (sp_IntArray *)v.v.p;
1852+ for (mrb_int _i = 0; _i < _a->len; _i++) {
1853+ const char *_s = sp_sym_to_s((sp_sym)_a->data[_a->start + _i]);
1854+ fputs(_s, stdout); putchar('\n');
1855+ }
1856+ break;
1857+ }
18351858 case SP_BUILTIN_RANGE: puts(sp_Range_inspect((sp_Range *)v.v.p)); break;
18361859 case SP_BUILTIN_TIME: puts(sp_Time_inspect((sp_Time *)v.v.p)); break;
18371860 default: printf("#<Object:0x%p>\n", v.v.p); break;