Commit b13c605b
b13c605b20bdMerged
Yusuke Endoh <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): PtrArray accumulator for map blocks returning typed arrays
recv.map { ... [...] } whose block returns a typed array
(int_array/float_array/str_array/sym_array) needs to accumulate
into a PtrArray, not an IntArray. The IntArray accumulator path
casts each pushed value through mrb_int — for an array element
that's a heap pointer, the slot has the right bit pattern but no
GC-traceable type, and the inner array can be collected mid-use.
Detect array-typed block return on the (Range/Integer-step)
recv path: emit sp_PtrArray * accumulator and sp_PtrArray_push
per element, falling back to IntArray for scalar block returns
(the existing path).
Details
- Commit
- b13c605b20bd
- Parent
- 1cf6a187efa1
spinel_codegen.rbmodifiedView file
end @needs_int_array = 1 @needs_gc = 1 ret_is_arr = false if block_ret_r == "string" @needs_str_array = 1 emit(" sp_StrArray *" + tmp_arr + " = sp_StrArray_new();") pop_scope return tmp_arr else emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();") # Block returning a homogeneous typed array (e.g. # `[1, 6].map { (0..n).map { ... } }`) — accumulator must # be a PtrArray so GC keeps each element alive (an IntArray # accumulator with pointer-payload mrb_ints would let the # inner arrays be collected). ret_is_arr2 = (block_ret == "int_array" || block_ret == "float_array" || block_ret == "str_array" || block_ret == "sym_array") if ret_is_arr2 @needs_ptr_array = 1 emit(" sp_PtrArray *" + tmp_arr + " = sp_PtrArray_new();") else emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();") end emit(" SP_GC_ROOT(" + tmp_arr + ");") emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {") if bp_is_lambda == 1 if stmts3.length > 0 last = stmts3[stmts3.length - 1] val = compile_expr(last) emit(" sp_IntArray_push(" + tmp_arr + ", " + val + ");") if ret_is_arr2 emit(" sp_PtrArray_push(" + tmp_arr + ", (void *)(" + val + "));") else emit(" sp_IntArray_push(" + tmp_arr + ", " + val + ");") end else emit_map_default_push(tmp_arr, "int_array") emit_map_default_push(tmp_arr, ret_is_arr2 ? "ptr_array" : "int_array") end end @indent = @indent - 1test/map_array_block_result.rbaddedView file
Loading diff…
test/map_array_block_result.rb.expectedaddedView file
Loading diff…