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) == 1
12999- types.push(ptr_array_elem_type(nrt))
12999+ # When the iterated element is itself an array and the
13000+ # block uses _1, _2, ... (max >= 2), Ruby destructures
13001+ # the yielded sub-array into the numbered slots. Each
13002+ # _i then takes the *inner* element type, not the
13003+ # outer ptr_array element type.
13004+ ptr_elem = ptr_array_elem_type(nrt)
13005+ if nmax >= 2 && is_array_type(ptr_elem) == 1
13006+ types.push(elem_type_of_array(ptr_elem))
13007+ else
13008+ types.push(ptr_elem)
13009+ end
1300013010 else
1300113011 types.push("int")
1300213012 end
@@ -24057,15 +24067,52 @@
2405724067 elem_type = ptr_array_elem_type(rt)
2405824068 tmp = new_temp
2405924069 bp_tmp = new_temp
24070+ # Detect numbered-params auto-destructure: `[[1,10]].each { _1; _2 }`
24071+ # binds _1=1, _2=10 in Ruby. Trigger when the block uses
24072+ # NumberedParametersNode with maximum >= 2 over an element that is
24073+ # itself an array we know how to index.
24074+ blk = @nd_block[nid]
24075+ bp = blk >= 0 ? @nd_parameters[blk] : -1
24076+ destruct_n = 0
24077+ if bp >= 0 && @nd_type[bp] == "NumberedParametersNode"
24078+ if @nd_value[bp] >= 2 && is_array_type(elem_type) == 1
24079+ destruct_n = @nd_value[bp]
24080+ end
24081+ end
2406024082 emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < sp_PtrArray_length(" + rc + "); " + tmp + "++) {")
24061- if has_bp == 1
24083+ if destruct_n > 0
24084+ 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 shorter
24087+ # than `destruct_n`, CRuby fills with nil; the typed-array codegen
24088+ # has no nil so we default to 0 (the typed-zero analogue). Without
24089+ # this guard the slot read is OOB into the sub-array's data buffer.
24090+ dlen_tmp = new_temp
24091+ emit(" mrb_int " + dlen_tmp + " = sp_" + elem_pfx + "_length(" + bp_tmp + ");")
24092+ di = 0
24093+ while di < destruct_n
24094+ slot = "lv__" + (di + 1).to_s
24095+ emit(" " + slot + " = (" + dlen_tmp + " > " + di.to_s + ") ? sp_" + elem_pfx + "_get(" + bp_tmp + ", " + di.to_s + ") : 0;")
24096+ di = di + 1
24097+ end
24098+ else
24099+ if has_bp == 1
24100+ emit(" " + c_type(elem_type) + " " + bp_tmp + " = (" + c_type(elem_type) + ")sp_PtrArray_get(" + rc + ", " + tmp + ");")
24101+ emit(" lv_" + bp1 + " = " + bp_tmp + ";")
24102+ end
2406424103 end
2406524104 @indent = @indent + 1
2406624105 push_scope
24067- if has_bp == 1
24068- declare_var(bp1, elem_type)
24106+ if destruct_n > 0
24107+ di = 0
24108+ while di < destruct_n
24109+ declare_var("_" + (di + 1).to_s, elem_type_of_array(elem_type))
24110+ di = di + 1
24111+ end
24112+ else
24113+ if has_bp == 1
24114+ declare_var(bp1, elem_type)
24115+ end
2406924116 end
2407024117 compile_stmts_body(@nd_body[@nd_block[nid]])
2407124118 pop_scope