Commit e4e800bf
e4e800bfc4c0Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
Merge pull request #142 from ryanseys/rs-fix-numbered-params-destructure
fix(codegen): destructure multi-arg numbered block params over sub-arrays
Details
- Commit
- e4e800bfc4c0
- Parent
- ab260076440e
- Merge parent
- 75fe106cc7d7
spinel_codegen.rbmodifiedView file
@@ -12996,7 +12996,17 @@
1299612996
elsif nrt == "sym_array"1299712997
types.push("symbol")1299812998
elsif is_ptr_array_type(nrt) == 112999-
types.push(ptr_array_elem_type(nrt))12999+
# When the iterated element is itself an array and the13000+
# block uses _1, _2, ... (max >= 2), Ruby destructures13001+
# the yielded sub-array into the numbered slots. Each13002+
# _i then takes the *inner* element type, not the13003+
# outer ptr_array element type.13004+
ptr_elem = ptr_array_elem_type(nrt)13005+
if nmax >= 2 && is_array_type(ptr_elem) == 113006+
types.push(elem_type_of_array(ptr_elem))13007+
else13008+
types.push(ptr_elem)13009+
end1300013010
else1300113011
types.push("int")1300213012
end@@ -24057,15 +24067,52 @@
2405724067
elem_type = ptr_array_elem_type(rt)2405824068
tmp = new_temp2405924069
bp_tmp = new_temp24070+
# Detect numbered-params auto-destructure: `[[1,10]].each { _1; _2 }`24071+
# binds _1=1, _2=10 in Ruby. Trigger when the block uses24072+
# NumberedParametersNode with maximum >= 2 over an element that is24073+
# itself an array we know how to index.24074+
blk = @nd_block[nid]24075+
bp = blk >= 0 ? @nd_parameters[blk] : -124076+
destruct_n = 024077+
if bp >= 0 && @nd_type[bp] == "NumberedParametersNode"24078+
if @nd_value[bp] >= 2 && is_array_type(elem_type) == 124079+
destruct_n = @nd_value[bp]24080+
end24081+
end2406024082
emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < sp_PtrArray_length(" + rc + "); " + tmp + "++) {")24061-
if has_bp == 124083+
if destruct_n > 024084+
elem_pfx = array_c_prefix(elem_type)2406224085
emit(" " + c_type(elem_type) + " " + bp_tmp + " = (" + c_type(elem_type) + ")sp_PtrArray_get(" + rc + ", " + tmp + ");")24063-
emit(" lv_" + bp1 + " = " + bp_tmp + ";")24086+
# Bounds-check missing slots: when the yielded sub-array is shorter24087+
# than `destruct_n`, CRuby fills with nil; the typed-array codegen24088+
# has no nil so we default to 0 (the typed-zero analogue). Without24089+
# this guard the slot read is OOB into the sub-array's data buffer.24090+
dlen_tmp = new_temp24091+
emit(" mrb_int " + dlen_tmp + " = sp_" + elem_pfx + "_length(" + bp_tmp + ");")24092+
di = 024093+
while di < destruct_n24094+
slot = "lv__" + (di + 1).to_s24095+
emit(" " + slot + " = (" + dlen_tmp + " > " + di.to_s + ") ? sp_" + elem_pfx + "_get(" + bp_tmp + ", " + di.to_s + ") : 0;")24096+
di = di + 124097+
end24098+
else24099+
if has_bp == 124100+
emit(" " + c_type(elem_type) + " " + bp_tmp + " = (" + c_type(elem_type) + ")sp_PtrArray_get(" + rc + ", " + tmp + ");")24101+
emit(" lv_" + bp1 + " = " + bp_tmp + ";")24102+
end2406424103
end2406524104
@indent = @indent + 12406624105
push_scope24067-
if has_bp == 124068-
declare_var(bp1, elem_type)24106+
if destruct_n > 024107+
di = 024108+
while di < destruct_n24109+
declare_var("_" + (di + 1).to_s, elem_type_of_array(elem_type))24110+
di = di + 124111+
end24112+
else24113+
if has_bp == 124114+
declare_var(bp1, elem_type)24115+
end2406924116
end2407024117
compile_stmts_body(@nd_body[@nd_block[nid]])2407124118
pop_scopetest/numbered_params_destructure.rbaddedView file
Loading diff…