Commit ba474cce

ba474cce9733Merged

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

Description

fix(codegen): Array#dig walks through nested array variants

emit_dig_step's int-key branch only handled SP_BUILTIN_INT_STR_HASH; poly_array / int_array / str_array / float_array elements collapsed to nil at every step, so a nested [[1, [2, "3"]]].dig(0, 1, 1) surfaced nil instead of "3".

Add emit_dig_arm_poly_array (sp_PolyArray_get returns sp_RbVal directly, no boxing wrapper) and emit_dig_arm_typed_array (boxes the typed element via sp_box_int / sp_box_str / sp_box_float). Both bounds-check the index and collapse to sp_box_nil on OOB -- sp_PolyArray_get / sp_IntArray_get / friends don't carry an upper-bound check, so the dig arm guards it inline rather than relying on the runtime function.

Refs #619 puzzle 6.

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

Details

Commit
ba474cce9733
Parent
c54d2f97deae

spinel_codegen.rbmodifiedView file

@@ -18975,11 +18975,34 @@
1897518975 emit_dig_arm_typed(acc, key_expr, "SP_BUILTIN_STR_STR_HASH", "sp_StrStrHash", "sp_box_str")
1897618976 elsif key_type == "int"
1897718977 emit_dig_arm_typed(acc, key_expr, "SP_BUILTIN_INT_STR_HASH", "sp_IntStrHash", "sp_box_str")
18978+ # Array variants for int-keyed dig: a nested array element
18979+ # ([[1, [2, "3"]]].dig(0, 1, 1)) walks through poly_array
18980+ # elements until it hits the final leaf. Without these arms
18981+ # the cls_id check landed only on IntStrHash and every step
18982+ # collapsed the accumulator to nil. Issue #619 puzzle 6.
18983+ emit_dig_arm_poly_array(acc, key_expr, "SP_BUILTIN_POLY_ARRAY", "sp_PolyArray")
18984+ emit_dig_arm_typed_array(acc, key_expr, "SP_BUILTIN_INT_ARRAY", "sp_IntArray", "sp_box_int")
18985+ emit_dig_arm_typed_array(acc, key_expr, "SP_BUILTIN_STR_ARRAY", "sp_StrArray", "sp_box_str")
18986+ emit_dig_arm_typed_array(acc, key_expr, "SP_BUILTIN_FLT_ARRAY", "sp_FloatArray", "sp_box_float")
1897818987 end
1897918988 emit(" " + acc + " = sp_box_nil();")
1898018989 emit(" }")
1898118990 end
1898218991
18992+ # Poly-array dig arm. sp_PolyArray_get already returns sp_RbVal,
18993+ # so no boxing wrapper; an explicit bounds check guards against
18994+ # the runtime function's missing upper-bound check (the dig
18995+ # contract is `nil on OOB`).
18996+ def emit_dig_arm_poly_array(acc, key_expr, cls_id, cstruct)
18997+ emit(" if (" + acc + ".cls_id == " + cls_id + ") { " + cstruct + " *_da = (" + cstruct + " *)" + acc + ".v.p; mrb_int _di = " + key_expr + "; if (_di < 0) _di += _da->len; " + acc + " = (_di >= 0 && _di < _da->len) ? " + cstruct + "_get(_da, _di) : sp_box_nil(); } else")
18998+ end
18999+
19000+ # Typed-array dig arm. Bounds-check + box the typed element so
19001+ # downstream steps (or the final `==`) see a uniform sp_RbVal.
19002+ def emit_dig_arm_typed_array(acc, key_expr, cls_id, cstruct, box_fn)
19003+ emit(" if (" + acc + ".cls_id == " + cls_id + ") { " + cstruct + " *_da = (" + cstruct + " *)" + acc + ".v.p; mrb_int _di = " + key_expr + "; if (_di < 0) _di += _da->len; " + acc + " = (_di >= 0 && _di < _da->len) ? " + box_fn + "(" + cstruct + "_get(_da, _di)) : sp_box_nil(); } else")
19004+ end
19005+
1898319006 def emit_dig_arm_poly(acc, key_expr, cls_id, cstruct)
1898419007 emit(" if (" + acc + ".cls_id == " + cls_id + ") { " + acc + " = " + cstruct + "_get((" + cstruct + " *)" + acc + ".v.p, " + key_expr + "); } else")
1898519008 end