Commit 56b09e11

56b09e11c8f9Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

Merge pull request #476 from ryanseys/rs-is-a-descendant-runtime

fix(codegen): emit runtime cls_id check for is_a? / kind_of? / instance_of? on parent-typed recv

Details

Commit
56b09e11c8f9
Parent
f7d874e26137
Merge parent
28ccb0ee34ed

spinel_codegen.rbmodifiedView file

@@ -2871,6 +2871,31 @@
28712871 0
28722872 end
28732873
2874+ # `is_descendant(child, ancestor)` — 1 iff `child` is a strict
2875+ # (proper) descendant of `ancestor` in the class hierarchy; 0
2876+ # otherwise (including when they're identical). Wraps
2877+ # is_class_or_ancestor, which is descendant-OR-EQUAL.
2878+ def is_descendant(child, ancestor)
2879+ if child == ancestor
2880+ return 0
2881+ end
2882+ is_class_or_ancestor(child, ancestor)
2883+ end
2884+
2885+ # `is_value_type_class(cname)` — 1 iff `cname` is a user class
2886+ # marked value-type (stack-allocated, no subclasses by design,
2887+ # no `cls_id` field on the struct). emit_class_fields gates the
2888+ # cls_id emission on the inverse of this; runtime cls_id checks
2889+ # must guard against value-type recvs to avoid referencing a
2890+ # nonexistent struct member.
2891+ def is_value_type_class(cname)
2892+ ci = find_class_idx(cname)
2893+ if ci >= 0 && @cls_is_value_type[ci] == 1
2894+ return 1
2895+ end
2896+ 0
2897+ end
2898+
28742899 def is_operator_name(name)
28752900 if name == "+"
28762901 return 1
@@ -17261,16 +17286,41 @@
1726117286 end
1726217287 if is_class_or_ancestor(cname, arg0) == 1
1726317288 return "TRUE"
17264- else
17265- return "FALSE"
1726617289 end
17290+ # Descendant case: arg0 is a proper subclass of cname (recv's
17291+ # static type). The recv's static type only bounds the runtime
17292+ # class above; the instance can be cname or any descendant
17293+ # including arg0. Consult the recv's actual cls_id slot.
17294+ target_ci = find_class_idx(arg0)
17295+ if target_ci >= 0 && is_descendant(arg0, cname) == 1
17296+ @needs_class_table = 1
17297+ @needs_class_ancestors = 1
17298+ recv_id = @nd_receiver[nid]
17299+ if recv_id >= 0
17300+ return "sp_class_le((sp_Class){" + compile_expr(recv_id) + "->cls_id}, (sp_Class){" + cls_id_for_user_internal(target_ci).to_s + "LL})"
17301+ end
17302+ end
17303+ # Unrelated classes — neither ancestor nor descendant. Always FALSE.
17304+ return "FALSE"
1726717305 end
1726817306 return "FALSE"
1726917307 end
1727017308
17271- # instance_of? — exact class identity, no ancestor walk. A parent
17272- # instance is never an exact-subclass match, so descendant resolution
17273- # is not relevant here; static comparison suffices.
17309+ # instance_of? — exact class identity, no ancestor walk.
17310+ # The recv's static type only bounds the runtime class above; the
17311+ # instance can be the static type OR any descendant. The runtime
17312+ # class can therefore equal arg0 only if arg0 is in that set —
17313+ # i.e., arg0 is cname or a descendant of cname. Other shapes
17314+ # (arg0 is an ancestor of cname, or unrelated) make the equality
17315+ # statically impossible and collapse to FALSE.
17316+ #
17317+ # `cname == arg0` does NOT in general collapse to TRUE — a parent-
17318+ # typed local holding a descendant returns FALSE for
17319+ # `instance_of?(parent)`. The check is runtime via the recv's
17320+ # cls_id slot. EXCEPTION: value-type classes have no subclasses
17321+ # by design (no cls_id field on the struct, no polymorphism), so
17322+ # for value-type cname the runtime is always exactly cname and
17323+ # the cname == arg0 check collapses back to static TRUE.
1727417324 if mname == "instance_of?"
1727517325 if is_obj_type(recv_type) == 1
1727617326 cname = recv_type[4, recv_type.length - 4]
@@ -17282,11 +17332,18 @@
1728217332 arg0 = resolve_introspection_arg_name(a[0])
1728317333 end
1728417334 end
17285- if cname == arg0
17286- return "TRUE"
17287- else
17288- return "FALSE"
17335+ target_ci = find_class_idx(arg0)
17336+ if target_ci >= 0 && (cname == arg0 || is_descendant(arg0, cname) == 1)
17337+ if is_value_type_class(cname) == 1
17338+ return "TRUE"
17339+ end
17340+ @needs_class_table = 1
17341+ recv_id = @nd_receiver[nid]
17342+ if recv_id >= 0
17343+ return "(" + compile_expr(recv_id) + "->cls_id == " + cls_id_for_user_internal(target_ci).to_s + "LL)"
17344+ end
1728917345 end
17346+ return "FALSE"
1729017347 end
1729117348 return "FALSE"
1729217349 end