Commit 2fbb4325
2fbb4325ac48Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
Merge pull request #285 from mame/pr/array-slice-assign
feat(codegen): Array#[]=(start, length, src) slice assignment
Details
- Commit
- 2fbb4325ac48
- Parent
- 6567fefbf7fb
- Merge parent
- 3b77e0316cae
spinel_codegen.rbmodifiedView file
@@ -28976,6 +28976,20 @@
2897628976
if args_id >= 02897728977
arg_ids = get_args(args_id)2897828978
end28979+
# Slice assignment `arr[i, n] = src`: replace n elements28980+
# starting at index i with the elements of src. Same-length28981+
# only (n == src.length); resize semantics not yet supported.28982+
# Only fires when both sides are the same C kind — falls28983+
# through otherwise to avoid silent miscompiles on mistyped28984+
# inputs (e.g. an `int_array`-typed src that's actually an28985+
# array-of-IntArrays would copy the wrong shape).28986+
if arg_ids.length == 3 && (rt == "int_array" || rt == "float_array" || rt == "str_array" || rt == "sym_array" || is_ptr_array_type(rt) == 1 || rt == "poly_array")28987+
src_t_check = infer_type(arg_ids[2])28988+
if array_c_prefix(rt) == array_c_prefix(src_t_check) && (src_t_check == "int_array" || src_t_check == "float_array" || src_t_check == "str_array" || src_t_check == "sym_array" || is_ptr_array_type(src_t_check) == 1 || src_t_check == "poly_array")28989+
compile_slice_assign(nid, recv, rt, rc, arg_ids)28990+
return28991+
end28992+
end2897928993
idx = "0"2898028994
val = "0"2898128995
if arg_ids.length >= 1@@ -29052,6 +29066,44 @@
2905229066
end2905329067
end2905429068
29069+
# Compile `arr[start, len] = src` slice assignment. Replaces29070+
# `len` elements of `arr` starting at `start` with the elements29071+
# of `src`. Same-length only — `src.length` must equal `len` at29072+
# runtime; resize semantics not implemented. Each index `i in29073+
# 0...len` is emitted as `arr[start + i] = src[i]`.29074+
def compile_slice_assign(nid, recv, rt, rc, arg_ids)29075+
start_id = arg_ids[0]29076+
len_id = arg_ids[1]29077+
src_id = arg_ids[2]29078+
src_t = infer_type(src_id)29079+
start_e = compile_expr(start_id)29080+
len_e = compile_expr(len_id)29081+
src_e = compile_expr_gc_rooted(src_id)29082+
pfx = array_c_prefix(rt)29083+
src_pfx = array_c_prefix(src_t)29084+
if pfx == "" || src_pfx == ""29085+
return29086+
end29087+
rc_t = new_temp29088+
src_t_v = new_temp29089+
start_t = new_temp29090+
ii = new_temp29091+
emit(" {")29092+
emit(" sp_" + pfx + " *" + rc_t + " = " + rc + ";")29093+
emit(" sp_" + src_pfx + " *" + src_t_v + " = " + src_e + ";")29094+
emit(" mrb_int " + start_t + " = " + start_e + ";")29095+
emit(" mrb_int _n = " + len_e + ";")29096+
# Inner loop: copy n elements from src to dest. Casting between29097+
# array kinds isn't supported here (e.g. int_array ← float_array29098+
# would silently truncate); both must be the same C kind.29099+
if pfx == src_pfx29100+
emit(" for (mrb_int " + ii + " = 0; " + ii + " < _n; " + ii + "++) {")29101+
emit(" sp_" + pfx + "_set(" + rc_t + ", " + start_t + " + " + ii + ", sp_" + src_pfx + "_get(" + src_t_v + ", " + ii + "));")29102+
emit(" }")29103+
end29104+
emit(" }")29105+
end29106+
2905529107
# Compile `recv[idx] OP= value` (IndexOperatorWriteNode).2905629108
#2905729109
# Emitted as a get-modify-set against the appropriate typed container,test/array_slice_assign.rbaddedView file
Loading diff…