Commit b301e8ef
b301e8efc327Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +4 added · ~1 modified
Description
Merge pull request #368 from mame/pr/fiber-body-self-capture
fix(codegen): capture self and ivars in Fiber.new { ... } body
Details
- Commit
- b301e8efc327
- Parent
- b7e3a90e8725
- Merge parent
- 92a2b8922e7e
spinel_codegen.rbmodifiedView file
@@ -8488,6 +8488,9 @@
84888488
if rname == "StringIO"84898489
return "stringio"84908490
end8491+
if rname == "Fiber"8492+
return "fiber"8493+
end84918494
return "obj_" + rname84928495
end84938496
end@@ -20937,7 +20940,7 @@
2093720940
return "0"2093820941
end2093920942
if t == "SelfNode"20940-
return "self"20943+
return self_expr2094120944
end2094220945
if t == "LocalVariableReadNode"2094320946
return fiber_var_ref(@nd_name[nid])@@ -21749,6 +21752,135 @@
2174921752
end2175021753
2175121754
21755+
# Detect whether a Fiber.new block body references `self` — directly21756+
# (`SelfNode`), via `@ivar` reads/writes, or via a no-receiver method21757+
# call inside a class body. Used by `compile_fiber_new` to decide21758+
# whether to thread an explicit `self` capture through the fiber's21759+
# `_cap` struct so the body's emitted C can resolve `self->iv_X` and21760+
# `sp_<Class>_<method>(self)` call sites without the surrounding21761+
# method's `self` parameter.21762+
def fiber_body_uses_self(nid)21763+
if nid < 021764+
return 021765+
end21766+
t = @nd_type[nid]21767+
if t == "SelfNode"21768+
return 121769+
end21770+
if t == "InstanceVariableReadNode" || t == "InstanceVariableWriteNode"21771+
return 121772+
end21773+
if t == "InstanceVariableOperatorWriteNode" || t == "InstanceVariableTargetNode"21774+
return 121775+
end21776+
if t == "InstanceVariableAndWriteNode" || t == "InstanceVariableOrWriteNode"21777+
return 121778+
end21779+
# Stop at nested lambda/fiber boundaries (their own body captures self separately)21780+
if t == "LambdaNode"21781+
return 021782+
end21783+
if t == "CallNode"21784+
mn = @nd_name[nid]21785+
if mn == "new"21786+
rv = @nd_receiver[nid]21787+
if rv >= 0 && constructor_class_name(rv) == "Fiber"21788+
return 021789+
end21790+
end21791+
# Bare (no receiver) call inside class context implicitly uses self21792+
if @nd_receiver[nid] < 0 && @current_class_idx >= 021793+
return 121794+
end21795+
end21796+
if @nd_body[nid] >= 021797+
if fiber_body_uses_self(@nd_body[nid]) == 121798+
return 121799+
end21800+
end21801+
if @nd_receiver[nid] >= 021802+
if fiber_body_uses_self(@nd_receiver[nid]) == 121803+
return 121804+
end21805+
end21806+
if @nd_expression[nid] >= 021807+
if fiber_body_uses_self(@nd_expression[nid]) == 121808+
return 121809+
end21810+
end21811+
if @nd_predicate[nid] >= 021812+
if fiber_body_uses_self(@nd_predicate[nid]) == 121813+
return 121814+
end21815+
end21816+
if @nd_subsequent[nid] >= 021817+
if fiber_body_uses_self(@nd_subsequent[nid]) == 121818+
return 121819+
end21820+
end21821+
if @nd_else_clause[nid] >= 021822+
if fiber_body_uses_self(@nd_else_clause[nid]) == 121823+
return 121824+
end21825+
end21826+
if @nd_arguments[nid] >= 021827+
if fiber_body_uses_self(@nd_arguments[nid]) == 121828+
return 121829+
end21830+
end21831+
if @nd_rescue_clause[nid] >= 021832+
if fiber_body_uses_self(@nd_rescue_clause[nid]) == 121833+
return 121834+
end21835+
end21836+
if @nd_ensure_clause[nid] >= 021837+
if fiber_body_uses_self(@nd_ensure_clause[nid]) == 121838+
return 121839+
end21840+
end21841+
stmts_list = parse_id_list(@nd_stmts[nid])21842+
k = 021843+
while k < stmts_list.length21844+
if fiber_body_uses_self(stmts_list[k]) == 121845+
return 121846+
end21847+
k = k + 121848+
end21849+
args_list = parse_id_list(@nd_args[nid])21850+
k = 021851+
while k < args_list.length21852+
if fiber_body_uses_self(args_list[k]) == 121853+
return 121854+
end21855+
k = k + 121856+
end21857+
conds_list = parse_id_list(@nd_conditions[nid])21858+
k = 021859+
while k < conds_list.length21860+
if fiber_body_uses_self(conds_list[k]) == 121861+
return 121862+
end21863+
k = k + 121864+
end21865+
elems_list = parse_id_list(@nd_elements[nid])21866+
k = 021867+
while k < elems_list.length21868+
if fiber_body_uses_self(elems_list[k]) == 121869+
return 121870+
end21871+
k = k + 121872+
end21873+
blk = @nd_block[nid]21874+
if blk >= 021875+
if @nd_body[blk] >= 021876+
if fiber_body_uses_self(@nd_body[blk]) == 121877+
return 121878+
end21879+
end21880+
end21881+
021882+
end21883+
2175221884
def scan_fiber_free_vars(nid, params, locals, free_vars, free_var_types)2175321885
if nid < 02175421886
return@@ -21933,6 +22065,23 @@
2193322065
scan_fiber_free_vars(body, all_plist, all_names, free_vars, free_var_types)2193422066
end2193522067
22068+
# Capture `self` when the body references the enclosing instance22069+
# (explicit `self`, ivar reads/writes, or no-receiver method calls22070+
# that resolve to `self.<m>`). Without this, `_fiber_body_N`'s22071+
# emitted body references a `self` that isn't a parameter and won't22072+
# compile. Only add the capture inside an instance-method context;22073+
# value-type classes pass `self` by value, and that path needs a22074+
# different capture shape we don't generate yet.22075+
self_captured = 022076+
if @current_class_idx >= 0 && @cls_is_value_type[@current_class_idx] != 122077+
if body >= 0 && fiber_body_uses_self(body) == 122078+
self_type = "obj_" + @cls_names[@current_class_idx]22079+
free_vars.push("self")22080+
free_var_types.push(self_type)22081+
self_captured = 122082+
end22083+
end22084+
2193622085
fid = @fiber_counter2193722086
@fiber_counter = @fiber_counter + 12193822087
fname = "_fiber_body_" + fid.to_s@@ -21967,11 +22116,15 @@
2196722116
saved_fiber_capture_types = @fiber_capture_types2196822117
saved_hp_names_len = @heap_promoted_names.length2196922118
saved_hp_cells_len = @heap_promoted_cells.length22119+
saved_self_override = @self_override2197022120
@out_lines = "".split(",")2197122121
@indent = 12197222122
@in_fiber_body = 12197322123
@fiber_captures = free_vars2197422124
@fiber_capture_types = free_var_types22125+
if self_captured == 122126+
@self_override = "(*_cap->self)"22127+
end2197522128
2197622129
push_scope2197722130
if bp != ""@@ -22001,11 +22154,16 @@
2200122154
compile_stmt(last)2200222155
emit(" _fb->yielded_value = sp_box_nil();")2200322156
else22004-
if @nd_type[last] == "LocalVariableWriteNode" || @nd_type[last] == "LocalVariableOperatorWriteNode"22157+
last_t_check = @nd_type[last]22158+
if last_t_check == "LocalVariableWriteNode" || last_t_check == "LocalVariableOperatorWriteNode" ||22159+
last_t_check == "InstanceVariableWriteNode" || last_t_check == "InstanceVariableOperatorWriteNode" ||22160+
last_t_check == "InstanceVariableOrWriteNode" || last_t_check == "InstanceVariableAndWriteNode"2200522161
compile_stmt(last)22162+
emit(" _fb->yielded_value = sp_box_nil();")22163+
else22164+
last_val = compile_expr(last)22165+
emit(" _fb->yielded_value = " + box_val_to_poly(last_val, last_type) + ";")2200622166
end22007-
last_val = compile_expr(last)22008-
emit(" _fb->yielded_value = " + box_val_to_poly(last_val, last_type) + ";")2200922167
end2201022168
end2201122169
end@@ -22021,6 +22179,7 @@
2202122179
@in_fiber_body = saved_in_fiber_body2202222180
@fiber_captures = saved_fiber_captures2202322181
@fiber_capture_types = saved_fiber_capture_types22182+
@self_override = saved_self_override2202422183
# Restore heap promoted lists to saved length2202522184
while @heap_promoted_names.length > saved_hp_names_len2202622185
@heap_promoted_names.pop@@ -22034,35 +22193,54 @@
2203422193
return "sp_Fiber_new(" + fname + ")"2203522194
end2203622195
22037-
# Heap-promote captured variables (allocate cells if not already promoted)22196+
# Heap-promote captured variables (allocate cells if not already promoted).22197+
# Track this fiber's cell-per-capture in a local map so a cross-method22198+
# `self` capture (different C function each time) doesn't accidentally22199+
# reuse a stale `_hcell_self_<n>` from a sibling fiber site that lives22200+
# in a different C scope.22201+
local_cells = "".split(",")2203822202
k = 02203922203
while k < free_vars.length2204022204
vn = free_vars[k]22041-
already_promoted = 022042-
# Check outer scope heap promotions22043-
ci = 022044-
while ci < @heap_promoted_names.length22045-
if @heap_promoted_names[ci] == vn22046-
already_promoted = 122047-
end22048-
ci = ci + 122049-
end22050-
# Check if variable is already a heap pointer from enclosing fiber capture22051-
if already_promoted == 0 && @in_fiber_body == 1 && fiber_capture_index(vn) >= 022052-
# Reuse the enclosing fiber's capture pointer (_cap->vn is already a heap cell)22053-
cell = "_cap->" + vn22054-
@heap_promoted_names.push(vn)22055-
@heap_promoted_cells.push(cell)22056-
already_promoted = 122057-
end22058-
if already_promoted == 022059-
cell = "_hcell_" + vn + "_" + fid.to_s22205+
cell = ""22206+
if vn == "self"22207+
# `self` resolves to the enclosing C function's parameter, which22208+
# differs per method (e.g. `sp_X_new`'s `self` and `sp_X_initialize`'s22209+
# `self` are distinct C variables). Skip the persistent22210+
# @heap_promoted_names lookup — its cells live in another scope.22211+
cell = "_hcell_self_" + fid.to_s2206022212
ct = c_type(free_var_types[k])2206122213
emit(" " + ct + " *" + cell + " = (" + ct + "*)sp_gc_alloc(sizeof(" + ct + "), NULL, NULL);")22062-
emit(" *" + cell + " = " + fiber_var_ref(vn) + ";")22063-
@heap_promoted_names.push(vn)22064-
@heap_promoted_cells.push(cell)22214+
emit(" *" + cell + " = self;")22215+
else22216+
already_promoted = 022217+
# Check outer scope heap promotions22218+
ci = 022219+
while ci < @heap_promoted_names.length22220+
if @heap_promoted_names[ci] == vn22221+
already_promoted = 122222+
cell = @heap_promoted_cells[ci]22223+
end22224+
ci = ci + 122225+
end22226+
# Check if variable is already a heap pointer from enclosing fiber capture22227+
if already_promoted == 0 && @in_fiber_body == 1 && fiber_capture_index(vn) >= 022228+
# Reuse the enclosing fiber's capture pointer (_cap->vn is already a heap cell)22229+
cell = "_cap->" + vn22230+
@heap_promoted_names.push(vn)22231+
@heap_promoted_cells.push(cell)22232+
already_promoted = 122233+
end22234+
if already_promoted == 022235+
cell = "_hcell_" + vn + "_" + fid.to_s22236+
ct = c_type(free_var_types[k])22237+
emit(" " + ct + " *" + cell + " = (" + ct + "*)sp_gc_alloc(sizeof(" + ct + "), NULL, NULL);")22238+
emit(" *" + cell + " = " + fiber_var_ref(vn) + ";")22239+
@heap_promoted_names.push(vn)22240+
@heap_promoted_cells.push(cell)22241+
end2206522242
end22243+
local_cells.push(cell)2206622244
k = k + 12206722245
end2206822246
@@ -22073,15 +22251,7 @@
2207322251
k = 02207422252
while k < free_vars.length2207522253
vn = free_vars[k]22076-
# Find the cell for this variable22077-
ci = 022078-
cell = ""22079-
while ci < @heap_promoted_names.length22080-
if @heap_promoted_names[ci] == vn22081-
cell = @heap_promoted_cells[ci]22082-
end22083-
ci = ci + 122084-
end22254+
cell = local_cells[k]2208522255
emit(" " + cap_ptr + "->" + vn + " = " + cell + ";")2208622256
k = k + 12208722257
endtest/fiber_ivar_persists_across_yield.rbaddedView file
Loading diff…
test/fiber_ivar_persists_across_yield.rb.expectedaddedView file
Loading diff…
test/fiber_yield_across_method_call.rbaddedView file
Loading diff…
test/fiber_yield_across_method_call.rb.expectedaddedView file
Loading diff…