tlopex commented on code in PR #684:
URL: https://github.com/apache/tvm-ffi/pull/684#discussion_r3642494889


##########
rust/tvm-ffi-macros/src/object_macros.rs:
##########
@@ -192,23 +195,24 @@ pub fn derive_object_ref(input: proc_macro::TokenStream) 
-> TokenStream {
                 }
             }
 
+            #[inline(always)]
             unsafe fn move_to_any(
                 src: Self,
                 data: &mut  #tvm_ffi_crate::tvm_ffi_sys::TVMFFIAny
             ) {
                 type ContainerType = <#struct_name as 
#tvm_ffi_crate::object::ObjectRefCore>
                     ::ContainerType;
-                let type_index =
-                    <ContainerType as 
#tvm_ffi_crate::object::ObjectCore>::type_index();
-                data.type_index = type_index as i32;
-                data.small_str_len = 0;
                 let data_ptr = #tvm_ffi_crate::object::ObjectArc::into_raw(
                     src.data
                 );
-                data.data_union.v_obj =
-                    data_ptr as *mut ContainerType as *mut  
#tvm_ffi_crate::tvm_ffi_sys::TVMFFIObject;
+                let object_ptr =
+                    data_ptr as *mut ContainerType as *mut 
#tvm_ffi_crate::tvm_ffi_sys::TVMFFIObject;

Review Comment:
   We need the object pointer first because the type index is now read from the 
runtime object header instead of ContainerType::type_index(), which preserves 
the dynamic subtype after an upcast.



##########
rust/tvm-ffi/src/object.rs:
##########
@@ -95,13 +99,120 @@ pub unsafe trait ObjectCoreWithExtraItems: ObjectCore {
 /// used by the ffi Any system and not user facing
 ///
 /// We mark as unsafe since it moves out the internal of the ObjectRef
+///
+/// # Safety
+///
+/// `data`, `into_data`, and `from_data` must preserve the same object
+/// allocation and form an ownership-preserving round trip. That allocation 
must
+/// start with a valid `TVMFFIObject` header whose registered object-range
+/// runtime type index correctly describes its layout and inheritance.
+///
+/// When `Self` also implements [`AnyCompatible`], `copy_to_any_view` must
+/// produce a non-owning view, while `move_to_any` must transfer ownership of
+/// the same object pointer and dynamic type index. `move_from_any_after_check`
+/// must be able to reclaim that owned representation exactly once, and a true
+/// `check_any_strict` result must guarantee that both after-check constructors
+/// are valid for it.
 pub unsafe trait ObjectRefCore: Sized + Clone {
     type ContainerType: ObjectCore;
     fn data(this: &Self) -> &ObjectArc<Self::ContainerType>;
     fn into_data(this: Self) -> ObjectArc<Self::ContainerType>;
     fn from_data(data: ObjectArc<Self::ContainerType>) -> Self;
 }
 
+/// Check whether a runtime type index refers to `target_type_index` or one of
+/// its subtypes.
+///
+/// The subtype relation lives in the process-wide type table maintained by the
+/// tvm-ffi library: every registered type records its depth in the single
+/// inheritance tree together with the chain of its ancestors. The check is
+/// O(1) — if `target` really is an ancestor, it must appear in the candidate's
+/// ancestor array exactly at `target`'s depth.
+///
+/// This is a hidden support function for derive-generated object checks. 
Object
+/// indices in the registered range must refer to entries in the runtime type
+/// table.
+#[doc(hidden)]
+#[inline(always)]
+pub fn is_instance_of(object_type_index: i32, target_type_index: i32) -> bool {
+    if object_type_index == target_type_index {
+        return true;
+    }
+    let object_begin = TypeIndex::kTVMFFIStaticObjectBegin as i32;
+    // Every registered object is an instance of the root Object type.
+    if target_type_index == object_begin {

Review Comment:
   I think it is. this is the root object fast path. Every object-range type 
must cast to objectref, and the C++ uses the same range check while avoiding 
type-table lookups.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to