Commit 1e6cd661

1e6cd661640fMerged

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

Description

Implement Array#each_index

arr.each_index { |i| ... } was unresolved ("cannot resolve call to 'each_index'"). Add it alongside each_with_index: yield each index 0..length-1 (only the index bound, no element), for any typed array.

[10, 20, 30].each_index { |i| puts i } # => 0 1 2 (was: unresolved)

This completes #1240 (G2, values_at with a Range argument, landed in the range-args cluster).

Fixes #1240

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

Details

Commit
1e6cd661640f
Parent
b6110d8a8703

spinel_codegen.rbmodifiedView file

@@ -37642,6 +37642,13 @@
3764237642 end
3764337643 end
3764437644
37645+ if mname == "each_index"
37646+ if @nd_block[nid] >= 0
37647+ compile_each_index_block(nid)
37648+ return 1
37649+ end
37650+ end
37651+
3764537652 # `hash.each_value { |v| ... }` for poly_poly_hash. Other hash
3764637653 # shapes can be added here once they surface; the underlying
3764737654 # representation is the same `vals[order[i]]` walk.
@@ -41901,6 +41908,35 @@
4190141908 @in_loop = old
4190241909 end
4190341910
41911+ # Array#each_index { |i| ... }: yield each index 0..len-1. Like
41912+ # each_with_index but with only the index bound (no element). Returns
41913+ # the receiver in CRuby; the statement form discards that.
41914+ def compile_each_index_block(nid)
41915+ old = @in_loop
41916+ @in_loop = 1
41917+ rt = infer_type(@nd_receiver[nid])
41918+ rc = compile_expr_gc_rooted(@nd_receiver[nid])
41919+ bp1 = get_block_param(nid, 0)
41920+ if bp1 == ""
41921+ bp1 = "_i"
41922+ end
41923+ tmp = new_temp
41924+ pfx = array_c_prefix(rt)
41925+ emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < sp_" + pfx + "_length(" + rc + "); " + tmp + "++) {")
41926+ emit(" mrb_int lv_" + bp1 + " = " + tmp + ";")
41927+ @indent = @indent + 1
41928+ push_scope
41929+ declare_var(bp1, "int")
41930+ redo_label = push_redo_label
41931+ emit_redo_label(redo_label)
41932+ compile_stmts_body(@nd_body[@nd_block[nid]])
41933+ pop_redo_label
41934+ pop_scope
41935+ @indent = @indent - 1
41936+ emit(" }")
41937+ @in_loop = old
41938+ end
41939+
4190441940 def compile_group_by_each_fused(each_nid, gb_nid)
4190541941 @needs_gc = 1
4190641942 arr_nid = @nd_receiver[gb_nid]