Commit 3eaa0a0d

3eaa0a0d7bdbMerged

Dmitry Bochkarev <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): propagate nested proc captures through string interpolation

Three fixes in spinel_codegen.rb:

  1. scan_lambda_free_vars / scan_fiber_free_vars: recurse into @nd_parts so LocalVariableReadNode inside InterpolatedStringNode (e.g. "first #{i}") is detected as a free variable.

  2. compile_proc_literal: reset @in_proc_body/@proc_captures to 0/[] on the no-captures path to prevent state leakage from outer proc.

  3. compile_proc_literal: when a nested proc captures a variable already captured by its enclosing proc, reuse the enclosing proc's capture struct field as the cell pointer instead of allocating a duplicate heap cell. Save/restore @heap_promoted_names length to prevent inner proc state leaking into outer proc state.

Details

Commit
3eaa0a0d7bdb
Parent
fdce862be17f

spinel_codegen.rbmodifiedView file

@@ -11343,6 +11343,13 @@
1134311343 scan_fiber_free_vars(elems_list[k], params, locals, free_vars, free_var_types)
1134411344 k = k + 1
1134511345 end
11346+ # InterpolatedStringNode parts
11347+ parts_list = parse_id_list(@nd_parts[nid])
11348+ k = 0
11349+ while k < parts_list.length
11350+ scan_fiber_free_vars(parts_list[k], params, locals, free_vars, free_var_types)
11351+ k = k + 1
11352+ end
1134611353 # Block body (for non-Fiber.new blocks)
1134711354 blk = @nd_block[nid]
1134811355 if blk >= 0
@@ -17148,17 +17155,6 @@
1714817155 end
1714917156 end
1715017157 end
17151- has_block_param_239 = (owner_pt.length > 0 && owner_pt.last == "proc") ? 1 : 0
17152- if has_block_param_239 == 1
17153- block_proc = block_forward_expr(nid)
17154- if block_proc != ""
17155- if ca != ""
17156- ca = ca + ", " + block_proc
17157- else
17158- ca = block_proc
17159- end
17160- end
17161- end
1716217158 end
1716317159 if ca != ""
1716417160 return "sp_" + owner_name + "_cls_" + sanitize_name(mname) + "(" + ca + ")"
@@ -24736,6 +24732,12 @@
2473624732 scan_lambda_free_vars(elems[k], params, locals, free_vars)
2473724733 k = k + 1
2473824734 end
24735+ parts = parse_id_list(@nd_parts[nid])
24736+ k = 0
24737+ while k < parts.length
24738+ scan_lambda_free_vars(parts[k], params, locals, free_vars)
24739+ k = k + 1
24740+ end
2473924741 if @nd_collection[nid] >= 0
2474024742 scan_lambda_free_vars(@nd_collection[nid], params, locals, free_vars)
2474124743 end
@@ -25503,10 +25505,16 @@
2550325505 saved_in_proc_body = @in_proc_body
2550425506 saved_proc_captures = @proc_captures
2550525507 saved_proc_capture_types = @proc_capture_types
25508+ saved_heap_hp_names_len = @heap_promoted_names.length
25509+ saved_heap_hp_cells_len = @heap_promoted_cells.length
2550625510 if has_captures == 1
2550725511 @in_proc_body = 1
2550825512 @proc_captures = captures
2550925513 @proc_capture_types = capture_types
25514+ else
25515+ @in_proc_body = 0
25516+ @proc_captures = "".split(",")
25517+ @proc_capture_types = "".split(",")
2551025518 end
2551125519 push_scope
2551225520 pts = "".split("|")
@@ -25562,6 +25570,12 @@
2556225570 @in_proc_body = saved_in_proc_body
2556325571 @proc_captures = saved_proc_captures
2556425572 @proc_capture_types = saved_proc_capture_types
25573+ while @heap_promoted_names.length > saved_heap_hp_names_len
25574+ @heap_promoted_names.pop
25575+ end
25576+ while @heap_promoted_cells.length > saved_heap_hp_cells_len
25577+ @heap_promoted_cells.pop
25578+ end
2556525579 @out_lines = save_out
2556625580
2556725581 if has_captures == 1
@@ -25630,9 +25644,13 @@
2563025644 ci = ci + 1
2563125645 end
2563225646 if already_promoted == 0
25633- cell = "_hcell_" + vn + "_p" + pid.to_s
25634- emit(" " + ct + " *" + cell + " = (" + ct + " *)sp_gc_alloc(sizeof(" + ct + "), NULL, NULL);")
25635- emit(" *" + cell + " = " + fiber_var_ref(vn) + ";")
25647+ if @in_proc_body == 1 && proc_capture_index(vn) >= 0
25648+ cell = "_cap->" + vn
25649+ else
25650+ cell = "_hcell_" + vn + "_p" + pid.to_s
25651+ emit(" " + ct + " *" + cell + " = (" + ct + " *)sp_gc_alloc(sizeof(" + ct + "), NULL, NULL);")
25652+ emit(" *" + cell + " = " + fiber_var_ref(vn) + ";")
25653+ end
2563625654 @heap_promoted_names.push(vn)
2563725655 @heap_promoted_cells.push(cell)
2563825656 end