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:
-
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.
-
compile_proc_literal: reset @in_proc_body/@proc_captures to 0/[] on the no-captures path to prevent state leakage from outer proc.
-
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
scan_fiber_free_vars(elems_list[k], params, locals, free_vars, free_var_types) k = k + 1 end # InterpolatedStringNode parts parts_list = parse_id_list(@nd_parts[nid]) k = 0 while k < parts_list.length scan_fiber_free_vars(parts_list[k], params, locals, free_vars, free_var_types) k = k + 1 end # Block body (for non-Fiber.new blocks) blk = @nd_block[nid] if blk >= 0 end end end has_block_param_239 = (owner_pt.length > 0 && owner_pt.last == "proc") ? 1 : 0 if has_block_param_239 == 1 block_proc = block_forward_expr(nid) if block_proc != "" if ca != "" ca = ca + ", " + block_proc else ca = block_proc end end end end if ca != "" return "sp_" + owner_name + "_cls_" + sanitize_name(mname) + "(" + ca + ")" scan_lambda_free_vars(elems[k], params, locals, free_vars) k = k + 1 end parts = parse_id_list(@nd_parts[nid]) k = 0 while k < parts.length scan_lambda_free_vars(parts[k], params, locals, free_vars) k = k + 1 end if @nd_collection[nid] >= 0 scan_lambda_free_vars(@nd_collection[nid], params, locals, free_vars) end saved_in_proc_body = @in_proc_body saved_proc_captures = @proc_captures saved_proc_capture_types = @proc_capture_types saved_heap_hp_names_len = @heap_promoted_names.length saved_heap_hp_cells_len = @heap_promoted_cells.length if has_captures == 1 @in_proc_body = 1 @proc_captures = captures @proc_capture_types = capture_types else @in_proc_body = 0 @proc_captures = "".split(",") @proc_capture_types = "".split(",") end push_scope pts = "".split("|") @in_proc_body = saved_in_proc_body @proc_captures = saved_proc_captures @proc_capture_types = saved_proc_capture_types while @heap_promoted_names.length > saved_heap_hp_names_len @heap_promoted_names.pop end while @heap_promoted_cells.length > saved_heap_hp_cells_len @heap_promoted_cells.pop end @out_lines = save_out if has_captures == 1 ci = ci + 1 end if already_promoted == 0 cell = "_hcell_" + vn + "_p" + pid.to_s emit(" " + ct + " *" + cell + " = (" + ct + " *)sp_gc_alloc(sizeof(" + ct + "), NULL, NULL);") emit(" *" + cell + " = " + fiber_var_ref(vn) + ";") if @in_proc_body == 1 && proc_capture_index(vn) >= 0 cell = "_cap->" + vn else cell = "_hcell_" + vn + "_p" + pid.to_s emit(" " + ct + " *" + cell + " = (" + ct + " *)sp_gc_alloc(sizeof(" + ct + "), NULL, NULL);") emit(" *" + cell + " = " + fiber_var_ref(vn) + ";") end @heap_promoted_names.push(vn) @heap_promoted_cells.push(cell) endtest/nested_proc_interp_capture.rbaddedView file
Loading diff…
test/nested_proc_interp_capture.rb.expectedaddedView file
Loading diff…