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
for self-referential data structures (linked lists, trees).**Inspect / `p`**: `Object#inspect` is implemented for all primitivetypes (Integer, Float, String, Symbol, Boolean, nil) and for typedarrays (`int_array`, `float_array`, `str_array`, `sym_array`).types (Integer, Float, String, Symbol, Boolean, nil), for typedarrays (`int_array`, `float_array`, `str_array`, `sym_array`), andfor heterogeneous arrays (`poly_array`, e.g. `[1, "x", :y]`).Scalar polymorphic values (the tagged-union values from the `Types`section above) also inspect correctly.`Array#to_s` is aliased to `Array#inspect`, matching CRuby.`Kernel#p` dispatches through `compile_inspect_for` so `p obj`,`obj.inspect`, `obj.to_s`, and `"#{obj.inspect}"` interpolationall produce CRuby-byte-identical output. Not yet implemented forHash, Range, Struct, user classes, or the polymorphic `sp_RbVal`tagged union.all produce CRuby-byte-identical output. User-class instancesinside a polymorphic value currently render as the placeholder`"#<Object>"` (the runtime has no class-name table yet); Hash,Range, and Struct inspect are not yet implemented.**Global Variables**: `$name` compiled to static C variables withtype-mismatch detection at compile time.lib/sp_runtime.hmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/bm_inspect.rbmodifiedView file
Loading diff…