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 -1
82548254 end
82558255
8256+ # Resolve the receiver of a rebind-and-splice call (instance_eval,
8257+ # instance_exec) to a class index using the same LocalVar / IVar
8258+ # cascade that ieval_rewrite_call shipped with. Returns -1 if the
8259+ # receiver type can't be statically classified.
8260+ #
8261+ # Factored from ieval_rewrite_call so iexec_rewrite_call (and any
8262+ # future rebind-and-splice detector) can reuse it byte-identically.
8263+ # The cascade is non-trivial -- find_var_type fallback for the
8264+ # LocalVar branch, cls_ivar_type lookup for the IVar branch with
8265+ # the obj_-prefix strip -- and duplicating it would silently
8266+ # 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+ end
8273+ # Inside a class instance method, the top-level local_class
8274+ # map is intentionally empty. Fall back to find_var_type so a
8275+ # method param (or scan_locals-typed local) resolves through
8276+ # the scope chain that ieval_walk_class_methods sets up. The
8277+ # is_obj_type / base_type strip is the same shape used in the
8278+ # 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) == 1
8282+ return find_class_idx(bt[4, bt.length - 4])
8283+ end
8284+ return -1
8285+ end
8286+ if @nd_type[recv] == "InstanceVariableReadNode"
8287+ # `@ivar.instance_eval { }` inside a class method. ieval_walk_class_methods
8288+ # sets @current_class_idx so cls_ivar_type returns the ivar's stored
8289+ # type -- "obj_<Class>" when the ivar was bound to `Class.new` (and
8290+ # not since widened to poly). Strip the "obj_" prefix to look up
8291+ # 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 >= 0
8294+ it = cls_ivar_type(@current_class_idx, @nd_name[recv])
8295+ bt = base_type(it)
8296+ if is_obj_type(bt) == 1
8297+ return find_class_idx(bt[4, bt.length - 4])
8298+ end
8299+ end
8300+ return -1
8301+ end
8302+ -1
8303+ end
8304+
82568305 def ieval_rewrite_call(nid, local_class)
82578306 if @nd_name[nid] != "instance_eval"
82588307 return
@@ -8302,39 +8351,7 @@
83028351 end
83038352 end
83048353 end
8305- ci = -1
8306- if @nd_type[recv] == "LocalVariableReadNode"
8307- vname = @nd_name[recv]
8308- if local_class.key?(vname)
8309- ci = local_class[vname]
8310- else
8311- # Inside a class instance method, the v1 top-level local_class
8312- # map is intentionally empty. Fall back to find_var_type so a
8313- # method param (or scan_locals-typed local) resolves through
8314- # the scope chain that ieval_walk_class_methods sets up. The
8315- # is_obj_type / base_type strip is the same shape used in the
8316- # 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) == 1
8320- ci = find_class_idx(bt[4, bt.length - 4])
8321- end
8322- end
8323- elsif @nd_type[recv] == "InstanceVariableReadNode"
8324- # `@ivar.instance_eval { }` inside a class method. ieval_walk_class_methods
8325- # sets @current_class_idx so cls_ivar_type returns the ivar's stored
8326- # type — "obj_<Class>" when the ivar was bound to `Class.new` (and
8327- # not since widened to poly). Strip the "obj_" prefix to look up
8328- # 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 >= 0
8331- it = cls_ivar_type(@current_class_idx, @nd_name[recv])
8332- bt = base_type(it)
8333- if is_obj_type(bt) == 1
8334- ci = find_class_idx(bt[4, bt.length - 4])
8335- end
8336- end
8337- end
8354+ ci = recv_class_idx_for_rebind(recv, local_class)
83388355 if ci < 0
83398356 return
83408357 end