tqchen commented on code in PR #443:
URL: https://github.com/apache/tvm-ffi/pull/443#discussion_r2801775596


##########
src/ffi/extra/serialization.cc:
##########
@@ -58,92 +62,121 @@ class ObjectGraphSerializer {
     if (it != node_index_map_.end()) {
       return (*it).second;
     }
+    const bool supports_cycle = value.type_index() == TypeIndex::kTVMFFIList;
+    int64_t node_index = -1;
+    if (supports_cycle) {
+      // Pre-register List nodes so self-references can resolve to a stable 
index.
+      node_index = static_cast<int64_t>(nodes_.size());
+      node_index_map_.emplace(value, node_index);
+      nodes_.push_back(json::Object{});
+    }
+    if (active_nodes_.find(value) != active_nodes_.end()) {
+      TVM_FFI_THROW(RuntimeError) << "Cyclic references are not supported in 
ToJSONGraph";
+    }
+    active_nodes_.insert(value);
     json::Object node;
-    switch (value.type_index()) {
-      case TypeIndex::kTVMFFINone: {
-        node.Set("type", ffi::StaticTypeKey::kTVMFFINone);
-        break;
-      }
-      case TypeIndex::kTVMFFIBool: {
-        node.Set("type", ffi::StaticTypeKey::kTVMFFIBool);
-        node.Set("data", 
details::AnyUnsafe::CopyFromAnyViewAfterCheck<bool>(value));
-        break;
-      }
-      case TypeIndex::kTVMFFIInt: {
-        node.Set("type", ffi::StaticTypeKey::kTVMFFIInt);
-        node.Set("data", 
details::AnyUnsafe::CopyFromAnyViewAfterCheck<int64_t>(value));
-        break;
-      }
-      case TypeIndex::kTVMFFIFloat: {
-        node.Set("type", ffi::StaticTypeKey::kTVMFFIFloat);
-        node.Set("data", 
details::AnyUnsafe::CopyFromAnyViewAfterCheck<double>(value));
-        break;
-      }
-      case TypeIndex::kTVMFFIDataType: {
-        DLDataType dtype = 
details::AnyUnsafe::CopyFromAnyViewAfterCheck<DLDataType>(value);
-        node.Set("type", ffi::StaticTypeKey::kTVMFFIDataType);
-        node.Set("data", DLDataTypeToString(dtype));
-        break;
-      }
-      case TypeIndex::kTVMFFIDevice: {
-        DLDevice device = 
details::AnyUnsafe::CopyFromAnyViewAfterCheck<DLDevice>(value);
-        node.Set("type", ffi::StaticTypeKey::kTVMFFIDevice);
-        node.Set("data", json::Array{
-                             static_cast<int64_t>(device.device_type),
-                             static_cast<int64_t>(device.device_id),
-                         });
-        break;
-      }
-      case TypeIndex::kTVMFFISmallStr:
-      case TypeIndex::kTVMFFIStr: {
-        String str = 
details::AnyUnsafe::CopyFromAnyViewAfterCheck<String>(value);
-        node.Set("type", ffi::StaticTypeKey::kTVMFFIStr);
-        node.Set("data", str);
-        break;
-      }
-      case TypeIndex::kTVMFFISmallBytes:
-      case TypeIndex::kTVMFFIBytes: {
-        Bytes bytes = 
details::AnyUnsafe::CopyFromAnyViewAfterCheck<Bytes>(value);
-        node.Set("type", ffi::StaticTypeKey::kTVMFFIBytes);
-        node.Set("data", Base64Encode(bytes));
-        break;
-      }
-      case TypeIndex::kTVMFFIArray: {
-        Array<Any> array = 
details::AnyUnsafe::CopyFromAnyViewAfterCheck<Array<Any>>(value);
-        node.Set("type", ffi::StaticTypeKey::kTVMFFIArray);
-        node.Set("data", CreateArrayData(array));
-        break;
-      }
-      case TypeIndex::kTVMFFIMap: {
-        Map<Any, Any> map = 
details::AnyUnsafe::CopyFromAnyViewAfterCheck<Map<Any, Any>>(value);
-        node.Set("type", ffi::StaticTypeKey::kTVMFFIMap);
-        node.Set("data", CreateMapData(map));
-        break;
-      }
-      case TypeIndex::kTVMFFIShape: {
-        ffi::Shape shape = 
details::AnyUnsafe::CopyFromAnyViewAfterCheck<ffi::Shape>(value);
-        node.Set("type", ffi::StaticTypeKey::kTVMFFIShape);
-        node.Set("data", Array<int64_t>(shape->data, shape->data + 
shape->size));
-        break;
-      }
-      default: {
-        if (value.type_index() >= TypeIndex::kTVMFFIStaticObjectBegin) {
-          // serialize type key since type index is runtime dependent
-          node.Set("type", value.GetTypeKey());
-          node.Set("data", CreateObjectData(value));
-        } else {
-          TVM_FFI_THROW(RuntimeError) << "Cannot serialize type `" << 
value.GetTypeKey() << "`";
-          TVM_FFI_UNREACHABLE();
+    try {

Review Comment:
   use RAII scope instead of try catch here



-- 
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