@@ -81,26 +81,39 @@ struct EnvErrorAlreadySet : public std::exception {};
8181 */
8282class ErrorObj : public Object , public TVMFFIErrorCell {
8383 public:
84- /* !
85- * \brief Update the traceback of the error object.
86- * \param traceback The traceback to update.
87- */
88- void UpdateTraceback (const TVMFFIByteArray* traceback_str) {
89- this ->traceback_data_ = std::string (traceback_str->data , traceback_str->size );
90- this ->traceback = TVMFFIByteArray{this ->traceback_data_ .data (), this ->traceback_data_ .length ()};
91- }
92-
9384 static constexpr const int32_t _type_index = TypeIndex::kTVMFFIError ;
9485 static constexpr const char * _type_key = " object.Error" ;
9586
9687 TVM_FFI_DECLARE_STATIC_OBJECT_INFO (ErrorObj, Object);
88+ };
89+
90+ namespace details {
91+ class ErrorObjFromStd : public ErrorObj {
92+ public:
93+ ErrorObjFromStd (std::string kind, std::string message, std::string traceback)
94+ : kind_data_(kind), message_data_(message), traceback_data_(traceback) {
95+ this ->kind = TVMFFIByteArray{kind_data_.data (), kind_data_.length ()};
96+ this ->message = TVMFFIByteArray{message_data_.data (), message_data_.length ()};
97+ this ->traceback = TVMFFIByteArray{traceback_data_.data (), traceback_data_.length ()};
98+ this ->update_traceback = UpdateTraceback;
99+ }
97100
98101 private:
99- friend class Error ;
102+ /* !
103+ * \brief Update the traceback of the error object.
104+ * \param traceback The traceback to update.
105+ */
106+ static void UpdateTraceback (TVMFFIObjectHandle self, const TVMFFIByteArray* traceback_str) {
107+ ErrorObjFromStd* obj = static_cast <ErrorObjFromStd*>(self);
108+ obj->traceback_data_ = std::string (traceback_str->data , traceback_str->size );
109+ obj->traceback = TVMFFIByteArray{obj->traceback_data_ .data (), obj->traceback_data_ .length ()};
110+ }
111+
100112 std::string kind_data_;
101113 std::string message_data_;
102114 std::string traceback_data_;
103115};
116+ } // namespace details
104117
105118/* !
106119 * \brief Managed reference to ErrorObj
@@ -109,14 +122,7 @@ class ErrorObj : public Object, public TVMFFIErrorCell {
109122class Error : public ObjectRef , public std ::exception {
110123 public:
111124 Error (std::string kind, std::string message, std::string traceback) {
112- ObjectPtr<ErrorObj> n = make_object<ErrorObj>();
113- n->kind_data_ = std::move (kind);
114- n->message_data_ = std::move (message);
115- n->traceback_data_ = std::move (traceback);
116- n->kind = TVMFFIByteArray{n->kind_data_ .data (), n->kind_data_ .length ()};
117- n->message = TVMFFIByteArray{n->message_data_ .data (), n->message_data_ .length ()};
118- n->traceback = TVMFFIByteArray{n->traceback_data_ .data (), n->traceback_data_ .length ()};
119- data_ = std::move (n);
125+ data_ = make_object<details::ErrorObjFromStd>(kind, message, traceback);
120126 }
121127
122128 Error (std::string kind, std::string message, const TVMFFIByteArray* traceback)
@@ -137,6 +143,11 @@ class Error : public ObjectRef, public std::exception {
137143 return std::string (obj->traceback .data , obj->traceback .size );
138144 }
139145
146+ void UpdateTraceback (const TVMFFIByteArray* traceback_str) {
147+ ErrorObj* obj = static_cast <ErrorObj*>(data_.get ());
148+ obj->update_traceback (obj, traceback_str);
149+ }
150+
140151 const char * what () const noexcept (true ) override {
141152 thread_local std::string what_data;
142153 ErrorObj* obj = static_cast <ErrorObj*>(data_.get ());
0 commit comments