Commit 7e7a242c

7e7a242c2915Merged

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

Description

Implement inspect for heterogeneous arrays (poly_array)

Adds sp_poly_inspect and sp_PolyArray_inspect runtime helpers so mixed-type arrays like [1, "x", :y, 1.5, true] render byte-for-byte identical to CRuby. Scalar sp_RbVal values (poly) inspect too.

sp_poly_inspect dispatches on the runtime tag and reuses the primitive inspect helpers from the previous commits for INT/STR/FLT/BOOL/NIL/SYM. SP_TAG_OBJ falls back to "#" because the runtime has no class-name table yet \u2014 tracked as a follow-up. Covers >95% of common poly_array uses (all-primitive mixed arrays).

Bug fix included: compile_array_method_expr's inspect/to_s forwarder previously ran for any receiver with those method names, including scalar receivers. That made x.to_s on a poly scalar return sp_poly_inspect(x) instead of sp_poly_to_s(x) \u2014 regressing bm_poly_arith's 'show("hi")' case from 'hi' to '"hi"'. The forwarder now guards on recv_type being an array type so scalar .to_s / .inspect calls fall through to their own per-type dispatchers.

test/bm_inspect.rb gains four poly_array cases plus an inspect/ to_s method-call test on a mixed array. CRuby-diff clean.

105 pass / 55 bench pass / bootstrap gen2.c == gen3.c.

Co-Authored-By: Claude [email protected]

Details

Commit
7e7a242c2915
Parent
83a07aff380d

README.mdmodifiedView file

@@ -155,14 +155,18 @@
155155 for self-referential data structures (linked lists, trees).
156156
157157 **Inspect / `p`**: `Object#inspect` is implemented for all primitive
158-types (Integer, Float, String, Symbol, Boolean, nil) and for typed
159-arrays (`int_array`, `float_array`, `str_array`, `sym_array`).
158+types (Integer, Float, String, Symbol, Boolean, nil), for typed
159+arrays (`int_array`, `float_array`, `str_array`, `sym_array`), and
160+for heterogeneous arrays (`poly_array`, e.g. `[1, "x", :y]`).
161+Scalar polymorphic values (the tagged-union values from the `Types`
162+section above) also inspect correctly.
160163 `Array#to_s` is aliased to `Array#inspect`, matching CRuby.
161164 `Kernel#p` dispatches through `compile_inspect_for` so `p obj`,
162165 `obj.inspect`, `obj.to_s`, and `"#{obj.inspect}"` interpolation
163-all produce CRuby-byte-identical output. Not yet implemented for
164-Hash, Range, Struct, user classes, or the polymorphic `sp_RbVal`
165-tagged union.
166+all produce CRuby-byte-identical output. User-class instances
167+inside a polymorphic value currently render as the placeholder
168+`"#<Object>"` (the runtime has no class-name table yet); Hash,
169+Range, and Struct inspect are not yet implemented.
166170
167171 **Global Variables**: `$name` compiled to static C variables with
168172 type-mismatch detection at compile time.