Commit ffefc0d0
ffefc0d06bfaMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Merge pull request #1025 from ryanseys/rs-extract-recv-class-idx-helper
refactor(analyze): extract recv_class_idx_for_rebind helper
Details
- Commit
- ffefc0d06bfa
- Parent
- f21ef72eb68d
- Merge parent
- e1ffc6e4aba0
spinel_analyze.rbmodifiedView file
@@ -8253,6 +8253,55 @@
82538253
-182548254
end82558255
8256+
# Resolve the receiver of a rebind-and-splice call (instance_eval,8257+
# instance_exec) to a class index using the same LocalVar / IVar8258+
# cascade that ieval_rewrite_call shipped with. Returns -1 if the8259+
# receiver type can't be statically classified.8260+
#8261+
# Factored from ieval_rewrite_call so iexec_rewrite_call (and any8262+
# future rebind-and-splice detector) can reuse it byte-identically.8263+
# The cascade is non-trivial -- find_var_type fallback for the8264+
# LocalVar branch, cls_ivar_type lookup for the IVar branch with8265+
# the obj_-prefix strip -- and duplicating it would silently8266+
# diverge as one path is patched without the other.8267+
def recv_class_idx_for_rebind(recv, local_class)8268+
if @nd_type[recv] == "LocalVariableReadNode"8269+
vname = @nd_name[recv]8270+
if local_class.key?(vname)8271+
return local_class[vname]8272+
end8273+
# Inside a class instance method, the top-level local_class8274+
# map is intentionally empty. Fall back to find_var_type so a8275+
# method param (or scan_locals-typed local) resolves through8276+
# the scope chain that ieval_walk_class_methods sets up. The8277+
# is_obj_type / base_type strip is the same shape used in the8278+
# ivar branch and at every other obj_-prefix site in this file.8279+
vt = find_var_type(vname)8280+
bt = base_type(vt)8281+
if is_obj_type(bt) == 18282+
return find_class_idx(bt[4, bt.length - 4])8283+
end8284+
return -18285+
end8286+
if @nd_type[recv] == "InstanceVariableReadNode"8287+
# `@ivar.instance_eval { }` inside a class method. ieval_walk_class_methods8288+
# sets @current_class_idx so cls_ivar_type returns the ivar's stored8289+
# type -- "obj_<Class>" when the ivar was bound to `Class.new` (and8290+
# not since widened to poly). Strip the "obj_" prefix to look up8291+
# the class index, the same shape `is_obj_type` / `base_type`8292+
# gates use elsewhere in the codegen for object-typed values.8293+
if @current_class_idx >= 08294+
it = cls_ivar_type(@current_class_idx, @nd_name[recv])8295+
bt = base_type(it)8296+
if is_obj_type(bt) == 18297+
return find_class_idx(bt[4, bt.length - 4])8298+
end8299+
end8300+
return -18301+
end8302+
-18303+
end8304+
82568305
def ieval_rewrite_call(nid, local_class)82578306
if @nd_name[nid] != "instance_eval"82588307
return@@ -8302,39 +8351,7 @@
83028351
end83038352
end83048353
end8305-
ci = -18306-
if @nd_type[recv] == "LocalVariableReadNode"8307-
vname = @nd_name[recv]8308-
if local_class.key?(vname)8309-
ci = local_class[vname]8310-
else8311-
# Inside a class instance method, the v1 top-level local_class8312-
# map is intentionally empty. Fall back to find_var_type so a8313-
# method param (or scan_locals-typed local) resolves through8314-
# the scope chain that ieval_walk_class_methods sets up. The8315-
# is_obj_type / base_type strip is the same shape used in the8316-
# ivar branch and at every other obj_-prefix site in this file.8317-
vt = find_var_type(vname)8318-
bt = base_type(vt)8319-
if is_obj_type(bt) == 18320-
ci = find_class_idx(bt[4, bt.length - 4])8321-
end8322-
end8323-
elsif @nd_type[recv] == "InstanceVariableReadNode"8324-
# `@ivar.instance_eval { }` inside a class method. ieval_walk_class_methods8325-
# sets @current_class_idx so cls_ivar_type returns the ivar's stored8326-
# type — "obj_<Class>" when the ivar was bound to `Class.new` (and8327-
# not since widened to poly). Strip the "obj_" prefix to look up8328-
# the class index, the same shape `is_obj_type` / `base_type`8329-
# gates use elsewhere in the codegen for object-typed values.8330-
if @current_class_idx >= 08331-
it = cls_ivar_type(@current_class_idx, @nd_name[recv])8332-
bt = base_type(it)8333-
if is_obj_type(bt) == 18334-
ci = find_class_idx(bt[4, bt.length - 4])8335-
end8336-
end8337-
end8354+
ci = recv_class_idx_for_rebind(recv, local_class)83388355
if ci < 083398356
return83408357
end