Commit 354bacbc

354bacbc4edcMerged

Richard Ramsden <[email protected]> · 6 months ago · 2 files · ~2 modified

Description

Implement Array#inspect for typed arrays

Adds sp_{Int,Float,Str,Sym}Array_inspect runtime helpers that build a CRuby-compatible bracketed representation by delegating each element to the matching primitive inspect (added in the previous commit). Four typed arrays share a single code pattern; sym arrays piggyback on the IntArray representation since sp_sym is mrb_int.

compile_inspect_for gains four new type branches and is now the canonical inspect dispatcher for every type Spinel supports. Array method dispatch forwards 'inspect' and 'to_s' to it (CRuby aliases Array#to_s to Array#inspect, same text output).

No changes to compile_p or compile_puts_single: Array#inspect flows through the same fputs(compile_inspect_for(...), stdout) path as primitive inspect.

Partial green for test/bm_inspect.rb: 'p arr', 'arr.inspect', 'arr.to_s', and "#{arr.inspect}" interpolation now all produce CRuby-identical output for int, float, string, and symbol arrays. Only 'puts arr' still fails \u2014 that needs the flatten-one-per-line behavior, coming in the next commit.

Co-Authored-By: Claude [email protected]

Details

Commit
354bacbc4edc
Parent
77a93ad54b88

lib/sp_runtime.hmodifiedView file

@@ -425,6 +425,15 @@
425425 static inline const char*sp_String_cstr(sp_String*s){return s->data;}
426426 static inline int64_t sp_String_length(sp_String*s){return s->len;}
427427 static sp_String*sp_String_dup(sp_String*s){return sp_String_new(s->data);}
428+/* Array#inspect for each typed array: `[elem1, elem2, ...]` with each
429+ element rendered via its own primitive inspect. Matches CRuby's
430+ Array#inspect output byte-for-byte. Returns a GC-managed C string. */
431+static const char*sp_IntArray_inspect(sp_IntArray*a){sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,sp_int_to_s(a->data[a->start+i]));}sp_String_append(s,"]");return s->data;}
432+static const char*sp_FloatArray_inspect(sp_FloatArray*a){sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,sp_float_inspect(a->data[i]));}sp_String_append(s,"]");return s->data;}
433+static const char*sp_StrArray_inspect(sp_StrArray*a){sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,sp_str_inspect(a->data[i]));}sp_String_append(s,"]");return s->data;}
434+/* Symbol arrays share the IntArray representation (sp_sym = mrb_int),
435+ but each element is rendered as ":name" via sp_sym_to_s. */
436+static const char*sp_SymArray_inspect(sp_IntArray*a){sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,":");sp_String_append(s,sp_sym_to_s((sp_sym)a->data[a->start+i]));}sp_String_append(s,"]");return s->data;}
428437
429438 /* Regexp engine (link with libspre.a from lib/regexp/) */
430439 typedef struct mrb_regexp_pattern mrb_regexp_pattern;