Commit cb27068f

cb27068fe139Merged

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

Description

codegen: str_array#sort_by — explicit block and &:length / &:to_i

["bb", "a", "ccc"].sort_by(&:length) bombed at C compile time: the str_array arm had no sort_by handler, so the unresolved-call fallback emitted (mrb_int)0 into an sp_StrArray * slot.

Mirror the int_array bubble-sort: clone via new+replace, iterate in pairs, materialise the block result twice per iteration as an int sort key, and swap when out of order. The block param is declared const char * so s.length etc dispatch as String methods. sp_StrArray has no start field so the index expressions are data[_j] rather than the IntArray data[start + _j] shape.

Symbol-to-proc shortcut handled inline for the common idioms (&:length, &:bytesize, &:size, &:to_i); other &:sym block shapes are still a wider Symbol#to_proc gap and fall through.

Fixes #978

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

Details

Commit
cb27068fe139
Parent
e987ab57f8e8

spinel_codegen.rbmodifiedView file

@@ -22687,6 +22687,68 @@
2268722687 return tmp_sa_mb
2268822688 end
2268922689 end
22690+ # str_array.sort_by — mirrors the int_array bubble-sort but with
22691+ # const char * elements and a string block param. The block
22692+ # returns an int sort key (`.length`, `.bytesize`, `.to_i`, ...).
22693+ # Supports explicit `{ |s| ... }` blocks and the `&:length` /
22694+ # `&:bytesize` symbol-to-proc shortcuts inline. Other &:sym
22695+ # shapes fall through to the unresolved-call path.
22696+ if mname == "sort_by" && @nd_block[nid] >= 0
22697+ blk_sb_s = @nd_block[nid]
22698+ sb_s_kind = @nd_type[blk_sb_s]
22699+ sb_s_sym = ""
22700+ if sb_s_kind == "BlockArgumentNode"
22701+ inner_sb_s = @nd_expression[blk_sb_s]
22702+ if inner_sb_s >= 0 && @nd_type[inner_sb_s] == "SymbolNode"
22703+ sb_s_sym = @nd_unescaped[inner_sb_s]
22704+ else
22705+ return ""
22706+ end
22707+ end
22708+ if sb_s_kind != "BlockNode" && sb_s_sym == ""
22709+ return ""
22710+ end
22711+ @needs_str_array = 1
22712+ @needs_gc = 1
22713+ tmp_sb_s = new_temp
22714+ emit(" sp_StrArray *" + tmp_sb_s + " = sp_StrArray_new(); sp_StrArray_replace(" + tmp_sb_s + ", " + rc + ");")
22715+ bp_sb_s = "_s"
22716+ if sb_s_kind == "BlockNode"
22717+ bp_sb_s = get_block_param(nid, 0)
22718+ if bp_sb_s == ""
22719+ bp_sb_s = "_s"
22720+ end
22721+ end
22722+ emit(" { mrb_int _n = " + tmp_sb_s + "->len;")
22723+ emit(" for (mrb_int _i = 0; _i < _n - 1; _i++)")
22724+ emit(" for (mrb_int _j = 0; _j < _n - 1 - _i; _j++) {")
22725+ emit(" const char *lv_" + bp_sb_s + " = " + tmp_sb_s + "->data[_j];")
22726+ bexpr_sb_s = "0"
22727+ if sb_s_kind == "BlockNode"
22728+ bbody_sb_s = @nd_body[blk_sb_s]
22729+ if bbody_sb_s >= 0
22730+ bs_sb_s = get_stmts(bbody_sb_s)
22731+ if bs_sb_s.length > 0
22732+ bexpr_sb_s = compile_expr(bs_sb_s.last)
22733+ end
22734+ end
22735+ elsif sb_s_sym == "length"
22736+ bexpr_sb_s = "sp_str_length(lv_" + bp_sb_s + ")"
22737+ elsif sb_s_sym == "bytesize" || sb_s_sym == "size"
22738+ bexpr_sb_s = "(mrb_int)strlen(lv_" + bp_sb_s + ")"
22739+ elsif sb_s_sym == "to_i"
22740+ bexpr_sb_s = "(mrb_int)atoll(lv_" + bp_sb_s + ")"
22741+ else
22742+ return ""
22743+ end
22744+ emit(" mrb_int _ka = " + bexpr_sb_s + ";")
22745+ emit(" lv_" + bp_sb_s + " = " + tmp_sb_s + "->data[_j + 1];")
22746+ emit(" mrb_int _kb = " + bexpr_sb_s + ";")
22747+ emit(" if (_ka > _kb) { const char *_tt = " + tmp_sb_s + "->data[_j]; " + tmp_sb_s + "->data[_j] = " + tmp_sb_s + "->data[_j + 1]; " + tmp_sb_s + "->data[_j + 1] = _tt; }")
22748+ emit(" }")
22749+ emit(" }")
22750+ return tmp_sb_s
22751+ end
2269022752 end
2269122753
2269222754 # PolyArray methods